Commits

Ondřej Surý committed 32b1307

Use custom error handler instead of fprintf(stderr, ...)

  • Participants
  • Parent commits 21efcc3

Comments (0)

Files changed (33)

 	(void)argv;
 
 	/* 2.0.12 */
-	fprintf (stderr, "annotate is not useful without freetype.\n"
+	fprintf(stderr, "annotate is not useful without freetype.\n"
 	         "Install freetype, then './configure; make clean; make install'\n"
 	         "the gd library again.\n"
 	        );

src/fontsizetest.c

 		                     buf);
 #endif
 		if (cp)
-			fprintf (stderr, "%s\n", cp);
+			fprintf(stderr, "%s\n", cp);
 		y += cursize + 4;
 
 		/* render the same fontsize with antialiasing turned off */
 		                     y, buf);
 #endif
 		if (cp)
-			fprintf (stderr, "%s\n", cp);
+			fprintf(stderr, "%s\n", cp);
 		y += cursize + 4;
 	}
 }
 	dotest ("arial", 400, 600, ".....Hello, there!", "fontsizetest3.jpeg");
 	dotest ("luximr", 400, 600, ".....Hello, there!", "fontsizetest4.jpeg");
 #else
-	fprintf (stderr, "no PNG or JPEG support\n");
+	fprintf(stderr, "no PNG or JPEG support\n");
 #endif
 
 	return 0;

src/fontwheeltest.c

 	dotest ("arial", 16, 90.0, 400, 400, "Hello\nthere!",
 	        "fontwheeltest4.jpeg");
 #else
-	fprintf (stderr, "no PNG or JPEG support\n");
+	fprintf(stderr, "no PNG or JPEG support\n");
 #endif
 
 	return 0;
 #include <math.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stdarg.h>
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #include "gd.h"
 #include "gdhelpers.h"
 #include "gd_color.h"
+#include "gd_errors.h"
 
 /* 2.0.12: this now checks the clipping rectangle */
 #define gdImageBoundsSafeMacro(im, x, y) (!((((y) < (im)->cy1) || ((y) > (im)->cy2)) || (((x) < (im)->cx1) || ((x) > (im)->cx2))))
 extern const int gdCosT[];
 extern const int gdSinT[];
 
+void gd_stderr_error(int priority, const char *format, ...)
+{
+        va_list args;
+
+        va_start(args, format);
+	switch (priority) {
+	case E_ERROR:
+		fputs("GD Error: ", stderr);
+		break;
+	case E_WARNING:
+		fputs("GD Warning: ", stderr);
+		break;
+	case E_NOTICE:
+		fputs("GD Notice: ", stderr);
+		break;
+	case E_INFO:
+		fputs("GD Info: ", stderr);
+		break;
+	case E_DEBUG:
+		fputs("GD Debug: ", stderr);
+		break;
+	}
+	va_start(args, format);
+	fprintf(stderr, format, args);
+	va_end(args);
+	fflush(stderr);
+}
+
+static gdErrorMethod gd_error_method = gd_stderr_error;
+
+void gd_error(const char *format, ...)
+{
+        va_list args;
+
+        va_start(args, format);
+	gd_error_ex(E_WARNING, format, args);
+        va_end(args);
+}
+void gd_error_ex(int priority, const char *format, ...)
+{
+        va_list args;
+
+        va_start(args, format);
+	if (gd_error_method) {
+		gd_error_method(priority, format, args);
+	}
+        va_end(args);
+}
+
+BGD_DECLARE(void) gdSetErrorMethod(gdErrorMethod error_method)
+{
+	gd_error_method = error_method;
+}
+
+BGD_DECLARE(void) gdClearErrorMethod(void)
+{
+	gd_error_method = gd_stderr_error;
+}
+
 static void gdImageBrushApply (gdImagePtr im, int x, int y);
 static void gdImageTileApply (gdImagePtr im, int x, int y);
 BGD_DECLARE(int) gdImageGetTrueColorPixel (gdImagePtr im, int x, int y);
 		}
 	}
 	/* Shouldn't happen */
-	fprintf (stderr, "Error: bug in gdImageCreateFromXbm!\n");
+	gd_error("Error: bug in gdImageCreateFromXbm!\n");
 fail:
 	gdImageDestroy (im);
 	return 0;
 /* Text functions take these. */
 typedef gdFont *gdFontPtr;
 
+typedef void(*gdErrorMethod)(int, const char *, ...);
+
+BGD_DECLARE(void) gdSetErrorMethod(gdErrorMethod);
+BGD_DECLARE(void) gdClearErrorMethod(void);
+
 /* For backwards compatibility only. Use gdImageSetStyle()
    for MUCH more flexible line drawing. Also see
    gdImageSetBrush(). */
 	gdImagePtr pal;
 	FILE *in, *out;
 	if (argc != 3) {
-		fprintf (stderr, "Usage: gd2copypal palettefile.gd2 filename.gd2\n");
+		fprintf(stderr, "Usage: gd2copypal palettefile.gd2 filename.gd2\n");
 		exit (1);
 	}
 	in = fopen (argv[1], "rb");
 	if (!in) {
-		fprintf (stderr, "Palette file does not exist!\n");
+		fprintf(stderr, "Palette file does not exist!\n");
 		exit (1);
 	}
 	pal = gdImageCreateFromGd2 (in);
 	fclose (in);
 	if (!pal) {
-		fprintf (stderr, "Palette is not in GD2 format!\n");
+		fprintf(stderr, "Palette is not in GD2 format!\n");
 		exit (1);
 	}
 
 	in = fopen (argv[2], "rb");
 	if (!in) {
-		fprintf (stderr, "Input file does not exist!\n");
+		fprintf(stderr, "Input file does not exist!\n");
 		exit (1);
 	}
 	im = gdImageCreateFromGd2 (in);
 	fclose (in);
 	if (!im) {
-		fprintf (stderr, "Input is not in GD2 format!\n");
+		fprintf(stderr, "Input is not in GD2 format!\n");
 		exit (1);
 	}
 
 
 	out = fopen (argv[2], "wb");
 	if (!out) {
-		fprintf (stderr, "Output file cannot be written to!\n");
+		fprintf(stderr, "Output file cannot be written to!\n");
 		gdImageDestroy (im);
 		exit (1);
 	}
 	int t0;
 
 	if (argc != 7) {
-		fprintf (stderr, "Usage: gd2time filename.gd count x y w h\n");
+		fprintf(stderr, "Usage: gd2time filename.gd count x y w h\n");
 		exit (1);
 	}
 
 	for (i = 0; i < c; i++) {
 		in = fopen (argv[1], "rb");
 		if (!in) {
-			fprintf (stderr, "Input file does not exist!\n");
+			fprintf(stderr, "Input file does not exist!\n");
 			exit (1);
 		}
 
 		fclose (in);
 
 		if (!im) {
-			fprintf (stderr, "Error reading source file!\n");
+			fprintf(stderr, "Error reading source file!\n");
 			exit (1);
 		}
 		gdImageDestroy (im);
 	}
 	in = fopen (argv[1], "rb");
 	if (!in) {
-		fprintf (stderr, "Input file does not exist!\n");
+		fprintf(stderr, "Input file does not exist!\n");
 		exit (1);
 	}
 	if (argc == 7) {
 	}
 	fclose (in);
 	if (!im) {
-		fprintf (stderr, "Input is not in GD2 format!\n");
+		fprintf(stderr, "Input is not in GD2 format!\n");
 		exit (1);
 	}
 	out = fopen (argv[2], "wb");
 	if (!out) {
-		fprintf (stderr, "Output file cannot be written to!\n");
+		fprintf(stderr, "Output file cannot be written to!\n");
 		gdImageDestroy (im);
 		exit (1);
 	}
 #ifdef HAVE_LIBPNG
 	gdImagePng (im, out);
 #else
-	fprintf (stderr, "No PNG library support available.\n");
+	fprintf(stderr, "No PNG library support available.\n");
 #endif
 	fclose (out);
 	gdImageDestroy (im);
+#ifndef GD_ERRORS_H
+#define GD_ERRORS_H
+
+#include <syslog.h>
+
+/*
+LOG_EMERG      system is unusable
+LOG_ALERT      action must be taken immediately
+LOG_CRIT       critical conditions
+LOG_ERR        error conditions
+LOG_WARNING    warning conditions
+LOG_NOTICE     normal, but significant, condition
+LOG_INFO       informational message
+LOG_DEBUG      debug-level message
+*/
+
+#define E_ERROR LOG_ERR
+#define E_WARNING LOG_WARNING
+#define E_NOTICE LOG_NOTICE
+#define E_INFO LOG_INFO
+#define E_DEBUG LOG_DEBUG
+
+void gd_error(const char *format, ...);
+void gd_error_ex(int priority, const char *format, ...);
+
+#endif
 				/* gd 2.0.11: gdSeek returns TRUE on success, not 0.
 				   Longstanding bug. 01/16/03 */
 				if (!gdSeek (in, dpos)) {
-					fprintf (stderr, "Seek error\n");
+					gd_error("Seek error\n");
 					goto fail2;
 				};
 				GD2_DBG (printf
 					          chunkIdx[chunkNum - 1].offset));
 
 					if (gdPutBuf (compData, compLen, out) <= 0) {
-						fprintf(stderr, "gd write error\n");
+						gd_error("gd write error\n");
 					};
 				};
 			};
 #else /* no HAVE_LIBZ */
 BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2 (FILE * inFile)
 {
-	fprintf (stderr, "GD2 support is not available - no libz\n");
+	gd_error("GD2 support is not available - no libz\n");
 	return NULL;
 }
 
 BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2Ctx (gdIOCtxPtr in)
 {
-	fprintf (stderr, "GD2 support is not available - no libz\n");
+	gd_error("GD2 support is not available - no libz\n");
 	return NULL;
 }
 #endif /* HAVE_LIBZ */
 #include <string.h>
 
 #include "gd.h"
+#include "gd_errors.h"
 /* TBB: move this up so include files are not brought in */
 /* JCE: arrange HAVE_LIBJPEG so that it can be set in gd.h */
 #ifdef HAVE_LIBJPEG
 {
 	jmpbuf_wrapper *jmpbufw;
 
-	fprintf(stderr, "gd-jpeg: JPEG library reports unrecoverable error: ");
+	gd_error("gd-jpeg: JPEG library reports unrecoverable error: ");
 	(*cinfo->err->output_message)(cinfo);
 	fflush(stderr);
 
 
 	if(jmpbufw != 0) {
 		longjmp(jmpbufw->jmpbuf, 1);
-		fprintf(stderr, "gd-jpeg: EXTREMELY fatal error: longjmp returned control; terminating\n");
+		gd_error("gd-jpeg: EXTREMELY fatal error: longjmp returned control; terminating\n");
 	} else {
-		fprintf(stderr, "gd-jpeg: EXTREMELY fatal error: jmpbuf unrecoverable; terminating\n");
+		gd_error("gd-jpeg: EXTREMELY fatal error: jmpbuf unrecoverable; terminating\n");
 	}
 
 	fflush(stderr);
 
 	row = (JSAMPROW)gdCalloc(1, cinfo.image_width * cinfo.input_components * sizeof(JSAMPLE));
 	if(row == 0) {
-		fprintf (stderr, "gd-jpeg: error: unable to allocate JPEG row structure: gdCalloc returns NULL\n");
+		gd_error("gd-jpeg: error: unable to allocate JPEG row structure: gdCalloc returns NULL\n");
 		jpeg_destroy_compress(&cinfo);
 		return;
 	}
 
 	if(im->trueColor) {
 #if BITS_IN_JSAMPLE == 12
-		fprintf(stderr,
+		gd_error(
 		        "gd-jpeg: error: jpeg library was compiled for 12-bit\n"
 		        "precision. This is mostly useless, because JPEGs on the web are\n"
 		        "8-bit and such versions of the jpeg library won't read or write\n"
 			nlines = jpeg_write_scanlines(&cinfo, rowptr, 1);
 
 			if(nlines != 1) {
-				fprintf (stderr, "gd_jpeg: warning: jpeg_write_scanlines returns %u -- expected 1\n", nlines);
+				gd_error("gd_jpeg: warning: jpeg_write_scanlines returns %u -- expected 1\n", nlines);
 			}
 		}
 	} else {
 
 			nlines = jpeg_write_scanlines(&cinfo, rowptr, 1);
 			if(nlines != 1) {
-				fprintf (stderr, "gd_jpeg: warning: jpeg_write_scanlines"
+				gd_error("gd_jpeg: warning: jpeg_write_scanlines"
 				         " returns %u -- expected 1\n", nlines);
 			}
 		}
 
 	retval = jpeg_read_header(&cinfo, TRUE);
 	if(retval != JPEG_HEADER_OK) {
-		fprintf (stderr, "gd-jpeg: warning: jpeg_read_header returns"
+		gd_error("gd-jpeg: warning: jpeg_read_header returns"
 		         " %d, expected %d\n", retval, JPEG_HEADER_OK);
 	}
 
 	if(cinfo.image_height > INT_MAX) {
-		fprintf (stderr, "gd-jpeg: warning: JPEG image height (%u) is"
+		gd_error("gd-jpeg: warning: JPEG image height (%u) is"
 		         " greater than INT_MAX (%d) (and thus greater than"
 		         " gd can handle)", cinfo.image_height, INT_MAX);
 	}
 
 	if(cinfo.image_width > INT_MAX) {
-		fprintf (stderr, "gd-jpeg: warning: JPEG image width (%u) is"
+		gd_error("gd-jpeg: warning: JPEG image width (%u) is"
 		         " greater than INT_MAX (%d) (and thus greater than"
 		         " gd can handle)\n", cinfo.image_width, INT_MAX);
 	}
 
 	im = gdImageCreateTrueColor((int)cinfo.image_width, (int)cinfo.image_height);
 	if(im == 0) {
-		fprintf (stderr, "gd-jpeg error: cannot allocate gdImage struct\n");
+		gd_error("gd-jpeg error: cannot allocate gdImage struct\n");
 		goto error;
 	}
 
 	}
 
 	if(jpeg_start_decompress(&cinfo) != TRUE) {
-		fprintf(stderr, "gd-jpeg: warning: jpeg_start_decompress"
+		gd_error("gd-jpeg: warning: jpeg_start_decompress"
 		        " reports suspended data source\n");
 	}
 
 #endif
 	if(cinfo.out_color_space == JCS_RGB) {
 		if(cinfo.output_components != 3) {
-			fprintf (stderr, "gd-jpeg: error: JPEG color quantization"
+			gd_error("gd-jpeg: error: JPEG color quantization"
 			         " request resulted in output_components == %d"
 			         " (expected 3 for RGB)\n", cinfo.output_components);
 			goto error;
 	} else if(cinfo.out_color_space == JCS_CMYK) {
 		jpeg_saved_marker_ptr marker;
 		if(cinfo.output_components != 4) {
-			fprintf (stderr, "gd-jpeg: error: JPEG color quantization"
+			gd_error("gd-jpeg: error: JPEG color quantization"
 			         " request resulted in output_components == %d"
 			         " (expected 4 for CMYK)\n", cinfo.output_components);
 			goto error;
 			marker = marker->next;
 		}
 	} else {
-		fprintf(stderr, "gd-jpeg: error: unexpected colorspace\n");
+		gd_error("gd-jpeg: error: unexpected colorspace\n");
 		goto error;
 	}
 #if BITS_IN_JSAMPLE == 12
-	fprintf(stderr, "gd-jpeg: error: jpeg library was compiled for 12-bit\n"
-	        "precision. This is mostly useless, because JPEGs on the web are\n"
-	        "8-bit and such versions of the jpeg library won't read or write\n"
-	        "them. GD doesn't support these unusual images. Edit your\n"
-	        "jmorecfg.h file to specify the correct precision and completely\n"
-	        "'make clean' and 'make install' libjpeg again. Sorry.\n");
+	gd_error_ex(E_ERROR,
+		    "gd-jpeg: error: jpeg library was compiled for 12-bit\n"
+		    "precision. This is mostly useless, because JPEGs on the web are\n"
+		    "8-bit and such versions of the jpeg library won't read or write\n"
+		    "them. GD doesn't support these unusual images. Edit your\n"
+		    "jmorecfg.h file to specify the correct precision and completely\n"
+		    "'make clean' and 'make install' libjpeg again. Sorry.\n");
 	goto error;
 #endif /* BITS_IN_JSAMPLE == 12 */
 
 	row = gdCalloc(cinfo.output_width *channels, sizeof(JSAMPLE));
 	if(row == 0) {
-		fprintf (stderr, "gd-jpeg: error: unable to allocate row for"
+		gd_error("gd-jpeg: error: unable to allocate row for"
 		         " JPEG scanline: gdCalloc returns NULL\n");
 		goto error;
 	}
 			register int *tpix = im->tpixels[i];
 			nrows = jpeg_read_scanlines(&cinfo, rowptr, 1);
 			if(nrows != 1) {
-				fprintf (stderr, "gd-jpeg: error: jpeg_read_scanlines"
+				gd_error("gd-jpeg: error: jpeg_read_scanlines"
 				         " returns %u, expected 1\n", nrows);
 				goto error;
 			}
 			register int *tpix = im->tpixels[i];
 			nrows = jpeg_read_scanlines(&cinfo, rowptr, 1);
 			if(nrows != 1) {
-				fprintf (stderr, "gd-jpeg: error: jpeg_read_scanlines"
+				gd_error("gd-jpeg: error: jpeg_read_scanlines"
 				         " returns %u, expected 1\n", nrows);
 				goto error;
 			}
 	}
 
 	if(jpeg_finish_decompress (&cinfo) != TRUE) {
-		fprintf(stderr, "gd-jpeg: warning: jpeg_finish_decompress"
-		        " reports suspended data source\n");
+		gd_error("gd-jpeg: warning: jpeg_finish_decompress"
+		         " reports suspended data source\n");
 	}
 	/* TBB 2.0.29: we should do our best to read whatever we can read, and a
 	 * warning is a warning. A fatal error on warnings doesn't make sense. */
 #include <string.h>
 #include "gd.h"
 #include "gdhelpers.h"
+#include "gd_errors.h"
 
 #include "gd_nnquant.h"
 
 	for (i=0; i<rad; i++)
 		nnq->radpower[i] = alpha*(((rad*rad - i*i)*radbias)/(rad*rad));
 
-	if(verbose) fprintf(stderr,"beginning 1D learning: initial radius=%d\n", rad);
+	if (verbose) gd_error_ex(E_NOTICE, "beginning 1D learning: initial radius=%d\n", rad);
 
 	if ((nnq->lengthcount%prime1) != 0) step = 4*prime1;
 	else {
 				nnq->radpower[j] = alpha*(((rad*rad - j*j)*radbias)/(rad*rad));
 		}
 	}
-	if(verbose) fprintf(stderr,"finished 1D learning: final alpha=%f !\n",((float)alpha)/initalpha);
+	if (verbose) gd_error_ex(E_NOTICE, "finished 1D learning: final alpha=%f !\n",((float)alpha)/initalpha);
 }
 
 BGD_DECLARE(gdImagePtr) gdImageNeuQuant(gdImagePtr im, const int max_color, int sample_factor)
 		}
 	}
 	if (bot_idx != top_idx + 1) {
-		fprintf(stderr,
-		        "  internal logic error: remapped bot_idx = %d, top_idx = %d\n",
-		        bot_idx, top_idx);
-		fflush(stderr);
+		gd_error("  internal logic error: remapped bot_idx = %d, top_idx = %d\n",
+			 bot_idx, top_idx);
 		goto done;
 	}
 
 	 * regardless of whether _BSD_SOURCE or anything else has (or has not)
 	 * been defined. */
 
-	fprintf (stderr, "gd-png:  fatal libpng error: %s\n", msg);
+	gd_error("gd-png:  fatal libpng error: %s\n", msg);
 	fflush (stderr);
 
 	jmpbuf_ptr = png_get_error_ptr (png_ptr);
 	if (jmpbuf_ptr == NULL) {				/* we are completely hosed now */
-		fprintf (stderr, "gd-png:  EXTREMELY fatal error: jmpbuf unrecoverable; terminating.\n");
+		gd_error("gd-png:  EXTREMELY fatal error: jmpbuf unrecoverable; terminating.\n");
 		fflush (stderr);
 		exit (99);
 	}
 	png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
 #endif
 	if (png_ptr == NULL) {
-		fprintf (stderr, "gd-png error: cannot allocate libpng main struct\n");
+		gd_error("gd-png error: cannot allocate libpng main struct\n");
 		return NULL;
 	}
 
 	info_ptr = png_create_info_struct (png_ptr);
 	if (info_ptr == NULL) {
-		fprintf (stderr, "gd-png error: cannot allocate libpng info struct\n");
+		gd_error("gd-png error: cannot allocate libpng info struct\n");
 		png_destroy_read_struct (&png_ptr, NULL, NULL);
 
 		return NULL;
 	 * PNG-reading libpng function */
 #ifdef PNG_SETJMP_SUPPORTED
 	if (setjmp(jbw.jmpbuf)) {
-		fprintf (stderr, "gd-png error: setjmp returns error condition 1\n");
+		gd_error("gd-png error: setjmp returns error condition 1\n");
 		png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
 
 		return NULL;
 		im = gdImageCreate ((int) width, (int) height);
 	}
 	if (im == NULL) {
-		fprintf (stderr, "gd-png error: cannot allocate gdImage struct\n");
+		gd_error("gd-png error: cannot allocate gdImage struct\n");
 		png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
 
 		return NULL;
 	 */
 #ifdef PNG_SETJMP_SUPPORTED
 	if (setjmp(jbw.jmpbuf)) {
-		fprintf(stderr, "gd-png error: setjmp returns error condition 2\n");
+		gd_error("gd-png error: setjmp returns error condition 2\n");
 		png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
 		gdFree(image_data);
 		gdFree(row_pointers);
 	case PNG_COLOR_TYPE_PALETTE:
 		png_get_PLTE (png_ptr, info_ptr, &palette, &num_palette);
 #ifdef DEBUG
-		fprintf (stderr, "gd-png color_type is palette, colors: %d\n", num_palette);
+		gd_error("gd-png color_type is palette, colors: %d\n", num_palette);
 #endif /* DEBUG */
 		if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) {
 			/* gd 2.0: we support this rather thoroughly now. Grab the
 	case PNG_COLOR_TYPE_GRAY:
 		/* create a fake palette and check for single-shade transparency */
 		if ((palette = (png_colorp) gdMalloc (256 * sizeof (png_color))) == NULL) {
-			fprintf (stderr, "gd-png error: cannot allocate gray palette\n");
+			gd_error("gd-png error: cannot allocate gray palette\n");
 			png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
 			return NULL;
 		}
 	}
 	image_data = (png_bytep) gdMalloc (rowbytes * height);
 	if (!image_data) {
-		fprintf (stderr, "gd-png error: cannot allocate image data\n");
+		gd_error("gd-png error: cannot allocate image data\n");
 		png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
 		if (im) {
 			gdImageDestroy(im);
 
 	row_pointers = (png_bytepp) gdMalloc (height * sizeof (png_bytep));
 	if (!row_pointers) {
-		fprintf (stderr, "gd-png error: cannot allocate row pointers\n");
+		gd_error("gd-png error: cannot allocate row pointers\n");
 		png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
 		if (im) {
 			gdImageDestroy(im);
 	png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
 #endif
 	if (png_ptr == NULL) {
-		fprintf (stderr, "gd-png error: cannot allocate libpng main struct\n");
+		gd_error("gd-png error: cannot allocate libpng main struct\n");
 		return;
 	}
 
 	info_ptr = png_create_info_struct (png_ptr);
 	if (info_ptr == NULL) {
-		fprintf (stderr, "gd-png error: cannot allocate libpng info struct\n");
+		gd_error("gd-png error: cannot allocate libpng info struct\n");
 		png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
 		return;
 	}
 
 #ifdef PNG_SETJMP_SUPPORTED
 	if (setjmp(jbw.jmpbuf)) {
-		fprintf (stderr, "gd-png error: setjmp returns error condition\n");
+		gd_error("gd-png error: setjmp returns error condition\n");
 		png_destroy_write_struct (&png_ptr, &info_ptr);
 		return;
 	}
 			}
 		}
 		if (colors == 0) {
-			fprintf(stderr, "gd-png error: no colors in palette\n");
+			gd_error("gd-png error: no colors in palette\n");
 			goto bail;
 		}
 		if (colors < im->colorsTotal) {
 		}
 		row_pointers = gdMalloc (sizeof (png_bytep) * height);
 		if (row_pointers == NULL) {
-			fprintf (stderr, "gd-png error: unable to allocate row_pointers\n");
+			gd_error("gd-png error: unable to allocate row_pointers\n");
 			goto bail;
 		}
 		prow_pointers = row_pointers;
 		for (j = 0; j < height; ++j) {
 			if (overflow2(width, channels) || ((*prow_pointers =
 			                                        (png_bytep) gdMalloc (width * channels)) == NULL)) {
-				fprintf (stderr, "gd-png error: unable to allocate rows\n");
+				gd_error("gd-png error: unable to allocate rows\n");
 				for (i = 0; i < j; ++i)
 					gdFree (row_pointers[i]);
 				/* 2.0.29: memory leak TBB */
 			}
 			row_pointers = gdMalloc (sizeof (png_bytep) * height);
 			if (row_pointers == NULL) {
-				fprintf (stderr, "gd-png error: unable to allocate row_pointers\n");
+				gd_error("gd-png error: unable to allocate row_pointers\n");
 				goto bail;
 			}
 			for (j = 0; j < height; ++j) {
 				if ((row_pointers[j] = (png_bytep) gdMalloc (width)) == NULL) {
-					fprintf (stderr, "gd-png error: unable to allocate rows\n");
+					gd_error("gd-png error: unable to allocate rows\n");
 					for (i = 0; i < j; ++i)
 						gdFree (row_pointers[i]);
 					/* TBB: memory leak */

src/gd_security.c

 #include <stdlib.h>
 #include <limits.h>
 #include "gd.h"
+#include "gd_errors.h"
 
 int overflow2(int a, int b)
 {
 	if(a <= 0 || b <= 0) {
-		fprintf(stderr, "gd warning: one parameter to a memory allocation multiplication is negative or zero, failing operation gracefully\n");
+		gd_error(E_WARNING, "one parameter to a memory allocation multiplication is negative or zero, failing operation gracefully\n");
 		return 1;
 	}
 	if(a > INT_MAX / b) {
-		fprintf(stderr, "gd warning: product of memory allocation multiplication would exceed INT_MAX, failing operation gracefully\n");
+		gd_error_ex(E_WARNING, "product of memory allocation multiplication would exceed INT_MAX, failing operation gracefully\n");
 		return 1;
 	}
 	return 0;
 {
 	(void)im;
 	(void)outSink;
-	fprintf (stderr, "PNG support is not available\n");
+	gd_error("PNG support is not available\n");
 }
 
 BGD_DECLARE(gdImagePtr) gdImageCreateFromPngSource (gdSourcePtr inSource)
 {
 	(void)inSource;
-	fprintf (stderr, "PNG support is not available\n");
+	gd_error("PNG support is not available\n");
 	return NULL;
 }
 #endif /* HAVE_LIBPNG */
 	unsigned char header[18];
 
 	if (gdGetBuf(header, sizeof(header), ctx) < 18) {
-		fprintf(stderr, "fail to read header");
+		gd_error("fail to read header");
 		return -1;
 	}
 
 	case 32:
 		break;
 	default:
-		fprintf(stderr, "bps %i not supported", tga->bits);
+		gd_error("bps %i not supported", tga->bits);
 		return -1;
 		break;
 	}
 	tiff_handle * t;
 
 	if (!g) {
-		fprintf(stderr, "Cannot create a new tiff handle, missing Ctx argument");
+		gd_error("Cannot create a new tiff handle, missing Ctx argument");
 		return NULL;
 	}
 
 	t = (tiff_handle *) gdMalloc(sizeof(tiff_handle));
 	if (!t) {
-		fprintf(stderr, "Failed to allocate a new tiff handle");
+		gd_error("Failed to allocate a new tiff handle");
 		return NULL;
 	}
 
 		/* Write the scan line to the tiff */
 		if(TIFFWriteEncodedStrip(tiff, y, scan, width * samplesPerPixel) == -1) {
 			/* error handler here */
-			fprintf(stderr, "Could not create TIFF\n");
+			gd_error("Could not create TIFF\n");
 			return;
 		}
 	}
 			return GD_SUCCESS;
 
 		} else if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &redcmap, &greencmap, &bluecmap)) {
-			fprintf(stderr, "Cannot read the color map");
+			gd_error("Cannot read the color map");
 			return GD_FAILURE;
 		}
 
 	}
 
 	if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &im_height)) {
-		fprintf(stderr, "Can't fetch TIFF height\n");
+		gd_error("Can't fetch TIFF height\n");
 		return FALSE;
 	}
 
 	if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &im_width)) {
-		fprintf(stderr, "Can't fetch TIFF width \n");
+		gd_error("Can't fetch TIFF width \n");
 		return FALSE;
 	}
 
 		case 8:
 			for (y = 0; y < im_height; y++ ) {
 				if (!TIFFReadScanline (tif, buffer, y, 0)) {
-					fprintf(stderr, "Error while reading scanline %i", y);
+					gd_error("Error while reading scanline %i", y);
 					break;
 				}
 				/* reading one line at a time */
 			if (is_bw) {
 				for (y = 0; y < im_height; y++ ) {
 					if (!TIFFReadScanline (tif, buffer, y, 0)) {
-						fprintf(stderr, "Error while reading scanline %i", y);
+						gd_error("Error while reading scanline %i", y);
 						break;
 					}
 					/* reading one line at a time */
 	                     tiff_unmapproc);
 
 	if (!tif) {
-		fprintf(stderr, "Cannot open TIFF image");
+		gd_error("Cannot open TIFF image");
 		return NULL;
 	}
 
 	if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width)) {
-		fprintf(stderr, "TIFF error, Cannot read image width");
+		gd_error("TIFF error, Cannot read image width");
 		goto error;
 	}
 
 	if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height)) {
-		fprintf(stderr, "TIFF error, Cannot read image width");
+		gd_error("TIFF error, Cannot read image width");
 		goto error;
 	}
 
 		         compression == COMPRESSION_CCITTFAX4 ||
 		         compression == COMPRESSION_CCITTRLE ||
 		         compression == COMPRESSION_CCITTRLEW)) {
-			fprintf(stderr, "Could not get photometric. "
+			gd_error("Could not get photometric. "
 			        "Image is CCITT compressed, assuming min-is-white");
 			photometric = PHOTOMETRIC_MINISWHITE;
 		} else {
-			fprintf(stderr, "Could not get photometric. "
+			gd_error("Could not get photometric. "
 			        "Assuming min-is-black");
 
 			photometric = PHOTOMETRIC_MINISBLACK;
 		--extra;
 	} else if (extra > 0 && (extra_types[0] == EXTRASAMPLE_UNSPECIFIED)) {
 		/* assuming unassociated alpha if unspecified */
-		fprintf(stderr, "alpha channel type not defined, assuming alpha is not premultiplied");
+		gd_error("alpha channel type not defined, assuming alpha is not premultiplied");
 		has_alpha = TRUE;
 		save_transparent = TRUE;
 		--extra;
 			break;
 
 		default:
-			fprintf (stderr, "Orientation %d not handled yet!", orientation);
+			gd_error("Orientation %d not handled yet!", orientation);
 			break;
 		}
 	}
 
 	/* create the WBMP */
 	if((wbmp = createwbmp(gdImageSX(image), gdImageSY(image), WBMP_WHITE)) == NULL) {
-		fprintf(stderr, "Could not create WBMP\n");
+		gd_error("Could not create WBMP\n");
 		return;
 	}
 
 
 	/* write the WBMP to a gd file descriptor */
 	if(writewbmp(wbmp, &gd_putout, out)) {
-		fprintf(stderr, "Could not save WBMP\n");
+		gd_error("Could not save WBMP\n");
 	}
 
 	/* des submitted this bugfix: gdFree the memory. */
 		if (Y) free(Y);
 		if (U) free(U);
 		if (V) free(V);
-		fprintf(stderr, "WebP decode: fail to decode input data");
+		gd_error("WebP decode: fail to decode input data");
 		return NULL;
 	}
 	im = gdImageCreateTrueColor(width, height);
 
 	filedata = gdMalloc(size);
 	if  (!filedata) {
-		fprintf(stderr, "WebP decode: alloc failed");
+		gd_error("WebP decode: alloc failed");
 		return NULL;
 	}
 	gdGetBuf(filedata, size, infile);
 		if (Y) free(Y);
 		if (U) free(U);
 		if (V) free(V);
-		fprintf(stderr, "WebP decode: fail to decode input data");
+		gd_error("WebP decode: fail to decode input data");
 		return NULL;
 	}
 	im = gdImageCreateTrueColor(width, height);
 	const float vp8qp =
 	scale * (MAX_QUALITY - quality) / (MAX_QUALITY - MIN_QUALITY) + MIN_VP8QP;
 	if (quality < MIN_QUALITY || quality > MAX_QUALITY) {
-		fprintf(stderr, "Wrong quality value %d.", quality);
+		gd_error("Wrong quality value %d.", quality);
 		return -1;
 	}
 
 	yuv_nbytes = width * height + 2 * yuv_width * yuv_height;
 
 	if ((Y = (unsigned char *)gdCalloc(yuv_nbytes, sizeof(unsigned char))) == NULL) {
-		fprintf(stderr, "gd-webp error: cannot allocate Y buffer");
+		gd_error("gd-webp error: cannot allocate Y buffer");
 		return;
 	}
 	vp8_quality = mapQualityToVP8QP(quantization);
 		if (filedata) {
 			free(filedata);
 		}
-		fprintf(stderr, "gd-webp error: WebP Encoder failed");
+		gd_error("gd-webp error: WebP Encoder failed");
 		return;
 	}
 
 	   output image. */
 	in = fopen ("demoin.png", "rb");
 	if (!in) {
-		fprintf (stderr, "Can't load source image; this demo\n");
-		fprintf (stderr, "is much more impressive if demoin.png\n");
-		fprintf (stderr, "is available.\n");
+		fprintf(stderr, "Can't load source image; this demo\n");
+		fprintf(stderr, "is much more impressive if demoin.png\n");
+		fprintf(stderr, "is available.\n");
 		im_in = 0;
 	} else {
 		int a;
 		gdImageDestroy (im_in);
 	}
 #else
-	fprintf (stderr, "No PNG library support.\n");
+	fprintf(stderr, "No PNG library support.\n");
 #endif /* HAVE_LIBPNG */
 	return 0;
 }
 		return NULL;
 	}
 
-#if 0
-	fprintf(stderr,"fontpathname=%s\n",fullname);
-#endif
-
 	err = FT_New_Face(*b->library, a->fontpath, 0, &a->face);
 
 	/* Read kerning metrics for Postscript fonts. */
 	}
 #endif
 
-#if 0
-	fprintf(stderr,"dpi=%d,%d metric_res=%d ptsize=%g\n",hdpi,vdpi,METRIC_RES,ptsize);
-#endif
-
 	oldpenf.x = oldpenf.y = 0; /* for postscript xshow operator */
 	penf.x = penf.y = 0;	/* running position of non-rotated glyphs */
 	previous = 0;		/* index of previous glyph for kerning calculations */
 	                       "bottom text", gdTrueColorAlpha (240, 240, 255, 32));
 	out = fopen ("gdfx.png", "wb");
 	if (!out) {
-		fprintf (stderr, "Can't create gdfx.png\n");
+		gd_error("Can't create gdfx.png\n");
 		return 1;
 	}
 	gdImagePng (im, out);
 #include <string.h>
 #include "gd.h"
 #include "gdhelpers.h"
+#include "gd_errors.h"
 
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
 #define ESC 27
 #define SS2 142
 
-static void
-debug (const char *format, ...)
-{
-#ifdef DEBUG
-	va_list args;
-
-	va_start (args, format);
-	fprintf (stdout, "%s: ", LIBNAME);
-	vfprintf (stdout, format, args);
-	fprintf (stdout, "\n");
-	va_end (args);
-#else
-	(void)format;
-#endif
-}
-
-static void
-error (const char *format, ...)
-{
-	va_list args;
-
-	va_start (args, format);
-	fprintf (stderr, "%s: ", LIBNAME);
-	vfprintf (stderr, format, args);
-	fprintf (stderr, "\n");
-	va_end (args);
-}
-
 /* DetectKanjiCode() derived from DetectCodeType() by Ken Lunde. */
 
 static int
 
 #ifdef DEBUG
 	if (whatcode == ASCII)
-		debug ("Kanji code not included.");
+		gd_error_ex(E_DEBUG, "Kanji code not included.");
 	else if (whatcode == EUCORSJIS)
-		debug ("Kanji code not detected.");
+		gd_error_ex(E_DEBUG, "Kanji code not detected.");
 	else
-		debug ("Kanji code detected at %d byte.", i);
+		gd_error_ex(E_DEBUG, "Kanji code detected at %d byte.", i);
 #endif
 
 	if (whatcode == EUCORSJIS && oldcode != ASCII)
 	size_t from_len, to_len;
 
 	if ((cd = iconv_open (EUCSTR, code)) == (iconv_t) - 1) {
-		error ("iconv_open() error");
+		gd_error ("iconv_open() error");
 #ifdef HAVE_ERRNO_H
 		if (errno == EINVAL)
 			error ("invalid code specification: \"%s\" or \"%s\"", EUCSTR, code);
 	        == -1) {
 #ifdef HAVE_ERRNO_H
 		if (errno == EINVAL)
-			error ("invalid end of input string");
+			gd_error ("invalid end of input string");
 		else if (errno == EILSEQ)
-			error ("invalid code in input string");
+			gd_error ("invalid code in input string");
 		else if (errno == E2BIG)
-			error ("output buffer overflow at do_convert()");
+			gd_error ("output buffer overflow at do_convert()");
 		else
 #endif
-			error ("something happen");
+			gd_error ("something happen");
 		ustrcpy (to, from);
 		return;
 	}
 
 	if (iconv_close (cd) != 0) {
-		error ("iconv_close() error");
+		gd_error ("iconv_close() error");
 	}
 #else
 	int p1, p2, i, j;
 			}
 		}
 	} else {
-		error ("invalid code specification: \"%s\"", code);
+		gd_error ("invalid code specification: \"%s\"", code);
 		return;
 	}
 
 	if (j >= BUFSIZ) {
-		error ("output buffer overflow at do_convert()");
+		gd_error ("output buffer overflow at do_convert()");
 		ustrcpy (to, from);
 	} else
 		to[j] = '\0';
 
 	switch (DetectKanjiCode (from)) {
 	case NEW:
-		debug ("Kanji code is New JIS.");
+		gd_error_ex(E_DEBUG, "Kanji code is New JIS.");
 		do_convert (&tmp_p, &from, NEWJISSTR);
 		break;
 	case OLD:
-		debug ("Kanji code is Old JIS.");
+		gd_error_ex(E_DEBUG, "Kanji code is Old JIS.");
 		do_convert (&tmp_p, &from, OLDJISSTR);
 		break;
 	case ESCI:
-		debug
-		("This string includes Hankaku-Kana (jisx0201) escape sequence [ESC] + ( + I.");
+		gd_error_ex(E_DEBUG, "This string includes Hankaku-Kana (jisx0201) escape sequence [ESC] + ( + I.");
 		do_convert (&tmp_p, &from, NEWJISSTR);
 		break;
 	case NEC:
-		debug ("Kanji code is NEC Kanji.");
-		error ("cannot convert NEC Kanji.");
+		gd_error_ex(E_DEBUG, "Kanji code is NEC Kanji.");
+		gd_error("cannot convert NEC Kanji.");
 		ustrcpy (tmp, from);
 		kanji = FALSE;
 		break;
 	case EUC:
-		debug ("Kanji code is EUC.");
+		gd_error_ex(E_DEBUG, "Kanji code is EUC.");
 		ustrcpy (tmp, from);
 		break;
 	case SJIS:
-		debug ("Kanji code is SJIS.");
+		gd_error_ex(E_DEBUG, "Kanji code is SJIS.");
 		do_convert (&tmp_p, &from, SJISSTR);
 		break;
 	case EUCORSJIS:
-		debug ("Kanji code is EUC or SJIS.");
+		gd_error_ex(E_DEBUG, "Kanji code is EUC or SJIS.");
 		ustrcpy (tmp, from);
 		kanji = FALSE;
 		break;
 	case ASCII:
-		debug ("This is ASCII string.");
+		gd_error_ex(E_DEBUG, "This is ASCII string.");
 		ustrcpy (tmp, from);
 		kanji = FALSE;
 		break;
 	default:
-		debug ("This string includes unknown code.");
+		gd_error_ex(E_DEBUG, "This string includes unknown code.");
 		ustrcpy (tmp, from);
 		kanji = FALSE;
 		break;
 		}
 
 		if (j >= BUFSIZ) {
-			error ("output buffer overflow at Hankaku --> Zenkaku");
+			gd_error("output buffer overflow at Hankaku --> Zenkaku");
 			ustrcpy (to, tmp);
 		} else
 			to[j] = '\0';
 	int ret;
 
 	if (strlen ((const char *) src) >= BUFSIZ) {
-		error ("input string too large");
+		gd_error("input string too large");
 		return -1;
 	}
 	if (dest_max > BUFSIZ) {
 	}
 	ret = do_check_and_conv (tmp_dest, src);
 	if (strlen ((const char *) tmp_dest) >= dest_max) {
-		error ("output buffer overflow");
+		gd_error("output buffer overflow");
 		ustrcpy (dest, src);
 		return -1;
 	}

src/gdparttopng.c

 	}
 	in = fopen (argv[1], "rb");
 	if (!in) {
-		fprintf (stderr, "Input file does not exist!\n");
+		fprintf(stderr, "Input file does not exist!\n");
 		exit (1);
 	}
 
 	im = gdImageCreateFromGd2Part (in, x, y, w, h);
 	fclose (in);
 	if (!im) {
-		fprintf (stderr, "Input is not in GD2 format!\n");
+		fprintf(stderr, "Input is not in GD2 format!\n");
 		exit (1);
 	}
 	out = fopen (argv[2], "wb");
 	if (!out) {
-		fprintf (stderr, "Output file cannot be written to!\n");
+		fprintf(stderr, "Output file cannot be written to!\n");
 		gdImageDestroy (im);
 		exit (1);
 	}
 #ifdef HAVE_LIBPNG
 	gdImagePng (im, out);
 #else
-	fprintf (stderr, "No PNG library support.\n");
+	fprintf(stderr, "No PNG library support.\n");
 #endif
 	fclose (out);
 	gdImageDestroy (im);
-
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 	int foreground;
 	int i;
 	if (argc != 2) {
-		fprintf (stderr, "Usage: gdtest filename.png\n");
+		fprintf(stderr, "Usage: gdtest filename.png\n");
 		exit (1);
 	}
 	in = fopen (argv[1], "rb");
 	if (!in) {
-		fprintf (stderr, "Input file does not exist!\n");
+		fprintf(stderr, "Input file does not exist!\n");
 		exit (1);
 	}
 	im = gdImageCreateFromPng (in);
 
 	in = fopen (of, "rb");
 	if (!in) {
-		fprintf (stderr, "PNG Output file does not exist!\n");
+		fprintf(stderr, "PNG Output file does not exist!\n");
 		exit (1);
 	}
 	im2 = gdImageCreateFromPng (in);
 
 	in = fopen (of, "rb");
 	if (!in) {
-		fprintf (stderr, "GD2 Output file does not exist!\n");
+		fprintf(stderr, "GD2 Output file does not exist!\n");
 		exit (1);
 	}
 	im2 = gdImageCreateFromGd2 (in);
 
 	in = fopen (of, "rb");
 	if (!in) {
-		fprintf (stderr, "GD Output file does not exist!\n");
+		fprintf(stderr, "GD Output file does not exist!\n");
 		exit (1);
 	}
 	im2 = gdImageCreateFromGd (in);
 	/* */
 	in = fopen ("test/gdtest_200_300_150_100.png", "rb");
 	if (!in) {
-		fprintf (stderr, "gdtest_200_300_150_100.png does not exist!\n");
+		fprintf(stderr, "gdtest_200_300_150_100.png does not exist!\n");
 		exit (1);
 	}
 	im2 = gdImageCreateFromPng (in);
 
 	in = fopen ("test/gdtest.gd2", "rb");
 	if (!in) {
-		fprintf (stderr, "gdtest.gd2 does not exist!\n");
+		fprintf(stderr, "gdtest.gd2 does not exist!\n");
 		exit (1);
 	}
 	im3 = gdImageCreateFromGd2Part (in, 200, 300, 150, 100);
 	/* */
 	in = fopen ("test/gdtest.png", "rb");
 	if (!in) {
-		fprintf (stderr, "gdtest.png does not exist!\n");
+		fprintf(stderr, "gdtest.png does not exist!\n");
 		exit (1);
 	}
 	im2 = gdImageCreateFromPng (in);
 
 	in = fopen ("test/gdtest_merge.png", "rb");
 	if (!in) {
-		fprintf (stderr, "gdtest_merge.png does not exist!\n");
+		fprintf(stderr, "gdtest_merge.png does not exist!\n");
 		exit (1);
 	}
 	im3 = gdImageCreateFromPng (in);
 #ifdef HAVE_LIBJPEG
 	out = fopen ("test/gdtest.jpg", "wb");
 	if (!out) {
-		fprintf (stderr, "Can't create file test/gdtest.jpg.\n");
+		fprintf(stderr, "Can't create file test/gdtest.jpg.\n");
 		exit (1);
 	}
 	gdImageJpeg (im, out, -1);
 	fclose (out);
 	in = fopen ("test/gdtest.jpg", "rb");
 	if (!in) {
-		fprintf (stderr, "Can't open file test/gdtest.jpg.\n");
+		fprintf(stderr, "Can't open file test/gdtest.jpg.\n");
 		exit (1);
 	}
 	im2 = gdImageCreateFromJpeg (in);
 	fclose (in);
 	if (!im2) {
-		fprintf (stderr, "gdImageCreateFromJpeg failed.\n");
+		fprintf(stderr, "gdImageCreateFromJpeg failed.\n");
 		exit (1);
 	}
 	gdImageDestroy (im2);
 	         "NOTE: the WBMP output image will NOT match the original unless the original\n"
 	         "is also black and white. This is OK!\n");
 	foreground = gdImageColorClosest (im, 0, 0, 0);
-	fprintf (stderr, "Foreground index is %d\n", foreground);
+	fprintf(stderr, "Foreground index is %d\n", foreground);
 	if (foreground == -1) {
-		fprintf (stderr, "Source image has no colors, skipping wbmp test.\n");
+		fprintf(stderr, "Source image has no colors, skipping wbmp test.\n");
 	} else {
 		out = fopen ("test/gdtest.wbmp", "wb");
 		if (!out) {
-			fprintf (stderr, "Can't create file test/gdtest.wbmp.\n");
+			fprintf(stderr, "Can't create file test/gdtest.wbmp.\n");
 			exit (1);
 		}
 		gdImageWBMP (im, foreground, out);
 		fclose (out);
 		in = fopen ("test/gdtest.wbmp", "rb");
 		if (!in) {
-			fprintf (stderr, "Can't open file test/gdtest.wbmp.\n");
+			fprintf(stderr, "Can't open file test/gdtest.wbmp.\n");
 			exit (1);
 		}
 		im2 = gdImageCreateFromWBMP (in);
-		fprintf (stderr, "WBMP has %d colors\n", gdImageColorsTotal (im2));
-		fprintf (stderr, "WBMP colors are:\n");
+		fprintf(stderr, "WBMP has %d colors\n", gdImageColorsTotal (im2));
+		fprintf(stderr, "WBMP colors are:\n");
 		for (i = 0; (i < gdImageColorsTotal (im2)); i++) {
-			fprintf (stderr, "%02X%02X%02X\n",
+			fprintf(stderr, "%02X%02X%02X\n",
 			         gdImageRed (im2, i),
 			         gdImageGreen (im2, i), gdImageBlue (im2, i));
 		}
 		fclose (in);
 		if (!im2) {
-			fprintf (stderr, "gdImageCreateFromWBMP failed.\n");
+			fprintf(stderr, "gdImageCreateFromWBMP failed.\n");
 			exit (1);
 		}
 		CompareImages ("WBMP test (gdtest.png, gdtest.wbmp)", ref, im2);
 	gdImageDestroy (im);
 	gdImageDestroy (ref);
 #else
-	fprintf (stderr, "No PNG library support.\n");
+	fprintf(stderr, "No PNG library support.\n");
 #endif /* HAVE_LIBPNG */
 
 	return 0;
 main (int argc, char *argv[])
 {
 #ifndef HAVE_LIBFREETYPE
-	fprintf (stderr, "gd was not compiled with HAVE_LIBFREETYPE defined.\n");
-	fprintf (stderr, "Install the FreeType library, including the\n");
-	fprintf (stderr, "header files. Then edit the gd Makefile, type\n");
-	fprintf (stderr, "make clean, and type make again.\n");
+	fprintf(stderr, "gd was not compiled with HAVE_LIBFREETYPE defined.\n");
+	fprintf(stderr, "Install the FreeType library, including the\n");
+	fprintf(stderr, "header files. Then edit the gd Makefile, type\n");
+	fprintf(stderr, "make clean, and type make again.\n");
 	return 1;
 #else
 	gdImagePtr im;
 	} else {
 		/* 2.02: usage message. Defaulting to Times wasn't working well for the
 		   many people with no /usr/share/fonts/truetype. */
-		fprintf (stderr, "Usage: gdtestft fontfilename\n");
-		fprintf (stderr,
-		         "If fontfilename is not a full or relative path, GDFONTPATH is searched for\n"
+		fprintf(stderr, "Usage: gdtestft fontfilename\n"
+			 "If fontfilename is not a full or relative path, GDFONTPATH is searched for\n"
 		         "it. If GDFONTPATH is not set, /usr/share/fonts/truetype is searched.\n");
 		exit (1);
 	}
 	err =
 	    gdImageStringFT ((gdImagePtr) NULL, &brect[0], 0, f, sz, angle, 0, 0, s);
 	if (err) {
-		fprintf (stderr, "%s\n", err);
+		fprintf(stderr, "%s\n", err);
 		return 1;
 	}
 
 #ifdef HAVE_LIBJPEG
 			imb = gdImageCreateFromJpeg (in);
 #else
-			fprintf (stderr, "No JPEG library support.\n");
+			fprintf(stderr, "No JPEG library support.\n");
 #endif
 			if (!imb) {
-				fprintf (stderr, "gdImageCreateFromJpeg failed\n");
+				fprintf(stderr, "gdImageCreateFromJpeg failed\n");
 				exit (1);
 			}
 			if (!im->trueColor) {
 	/* With antialiasing (positive color value) */
 	err = gdImageStringFT (im, NULL, blue, f, sz, angle, x, y, s);
 	if (err) {
-		fprintf (stderr, "%s\n", err);
+		fprintf(stderr, "%s\n", err);
 		return 1;
 	}
 	/* Without antialiasing (negative color value) */
 	err = gdImageStringFT (im, NULL, -blue, f, sz, angle, sx + x, y, s);
 	if (err) {
-		fprintf (stderr, "%s\n", err);
+		fprintf(stderr, "%s\n", err);
 		return 1;
 	}
 	/* With antialiasing, and 50% alpha blending (truecolor only) */
 	err = gdImageStringFT (im, NULL, blueAlpha, f, sz, angle, sx * 2 + x, y, s);
 	if (err) {
-		fprintf (stderr, "%s\n", err);
+		fprintf(stderr, "%s\n", err);
 		return 1;
 	}
 	/* TBB: Write img to test/fttest.jpg or test/fttest.png */
 	if (im->trueColor) {
 		out = fopen ("test/fttest.jpg", "wb");
 		if (!out) {
-			fprintf (stderr, "Can't create test/fttest.jpg\n");
+			fprintf(stderr, "Can't create test/fttest.jpg\n");
 			exit (1);
 		}
 		/* Fairly high JPEG quality setting */
 #ifdef HAVE_LIBJPEG
 		gdImageJpeg (im, out, 90);
 #else
-		fprintf (stderr, "No JPEG library support.\n");
+		fprintf(stderr, "No JPEG library support.\n");
 #endif
 		fclose (out);
-		fprintf (stderr, "Test image written to test/fttest.jpg\n");
+		fprintf(stderr, "Test image written to test/fttest.jpg\n");
 	} else {
 		out = fopen ("test/fttest.png", "wb");
 		if (!out) {
-			fprintf (stderr, "Can't create test/fttest.png\n");
+			fprintf(stderr, "Can't create test/fttest.png\n");
 			exit (1);
 		}
 		/* 2.0.10: correct ifdef, thanks to Gabriele Verzeletti */
 #ifdef HAVE_LIBPNG
 		gdImagePng (im, out);
 #else
-		fprintf (stderr, "No PNG library support.\n");
+		fprintf(stderr, "No PNG library support.\n");
 #endif
 		fclose (out);
-		fprintf (stderr, "Test image written to test/fttest.png\n");
+		fprintf(stderr, "Test image written to test/fttest.png\n");
 	}
 	/* Destroy it */
 	gdImageDestroy (im);
 	gdImagePtr im;
 	FILE *in, *out;
 	if (argc != 3) {
-		fprintf (stderr, "Usage: gdtopng filename.gd filename.png\n");
+		fprintf(stderr, "Usage: gdtopng filename.gd filename.png\n");
 		exit (1);
 	}
 	in = fopen (argv[1], "rb");
 	if (!in) {
-		fprintf (stderr, "Input file does not exist!\n");
+		fprintf(stderr, "Input file does not exist!\n");
 		exit (1);
 	}
 	im = gdImageCreateFromGd (in);
 	fclose (in);
 	if (!im) {
-		fprintf (stderr, "Input is not in GD format!\n");
+		fprintf(stderr, "Input is not in GD format!\n");
 		exit (1);
 	}
 	out = fopen (argv[2], "wb");
 	if (!out) {
-		fprintf (stderr, "Output file cannot be written to!\n");
+		fprintf(stderr, "Output file cannot be written to!\n");
 		gdImageDestroy (im);
 		exit (1);
 	}
 #ifdef HAVE_LIBPNG
 	gdImagePng (im, out);
 #else
-	fprintf (stderr, "No PNG library support.\n");
+	fprintf(stderr, "No PNG library support.\n");
 #endif
 	fclose (out);
 	gdImageDestroy (im);
 #include "gd.h"
 #include "gdhelpers.h"
 #include "gd_color_map.h"
+#include "gd_errors.h"
 
 #ifndef HAVE_LIBXPM
 BGD_DECLARE(gdImagePtr) gdImageCreateFromXpm(char *filename)
 {
 	(void)filename;
-	fprintf(stderr, "libgd was not built with xpm support\n");
+	gd_error_ex(E_ERROR, "libgd was not built with xpm support\n");
 	return NULL;
 }
 #else
-
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 	int cs, fmt;
 
 	if (argc != 5) {
-		fprintf (stderr, "Usage: pngtogd2 filename.png filename.gd2 cs fmt\n");
-		fprintf (stderr, "    where cs is the chunk size\n");
-		fprintf (stderr, "          fmt is 1 for raw, 2 for compressed\n");
+		fprintf(stderr, "Usage: pngtogd2 filename.png filename.gd2 cs fmt\n");
+		fprintf(stderr, "    where cs is the chunk size\n");
+		fprintf(stderr, "          fmt is 1 for raw, 2 for compressed\n");
 		exit (1);
 	}
 	in = fopen (argv[1], "rb");
 	if (!in) {
-		fprintf (stderr, "Input file does not exist!\n");
+		fprintf(stderr, "Input file does not exist!\n");
 		exit (1);
 	}
 #ifdef HAVE_LIBPNG
 	im = gdImageCreateFromPng (in);
 #else
 	im = NULL;
-	fprintf (stderr, "No PNG library support available.\n");
+	fprintf(stderr, "No PNG library support available.\n");
 #endif
 	fclose (in);
 	if (!im) {
-		fprintf (stderr, "Input is not in PNG format!\n");
+		fprintf(stderr, "Input is not in PNG format!\n");
 		exit (1);
 	}
 	out = fopen (argv[2], "wb");
 	if (!out) {
-		fprintf (stderr, "Output file cannot be written to!\n");
+		fprintf(stderr, "Output file cannot be written to!\n");
 		gdImageDestroy (im);
 		exit (1);
 	}
 	gdImagePng (im, out);
 	fclose (out);
 #else
-	fprintf(stderr, "Compiled without libpng support\n");
+	gd_error("Compiled without libpng support\n");
 #endif /* HAVE_LIBPNG */
 	gdImageDestroy (im);
 
 	return 0;
 #else
-	fprintf(stderr, "Compiled without freetype support\n");
+	gd_error("Compiled without freetype support\n");
 	return 0;
 #endif /* HAVE_LIBFREETYPE */
 }
   	   palette image. */
   	im = gdImageCreateTrueColor(im2->sx, im2->sy);
   	if (!im) {
-  		fprintf(stderr, "gd-webp error: cannot convert palette input to truecolor");
+  		gd_error("gd-webp error: cannot convert palette input to truecolor");
   		return;
 	}
   	gdImageCopy(im, im2, 0, 0, 0, 0, im->sx, im->sy);
 		in = fopen (argv[argc - 1], "rb");
 	}
 	if (!in) {
-		fprintf (stderr, "Error: can't open file %s.\n", argv[argc - 1]);
+		fprintf(stderr, "can't open file %s.\n", argv[argc - 1]);
 		exit (1);
 	}
 	/* Now load the image. */
 #ifdef HAVE_LIBPNG
 	im = gdImageCreateFromPng (in);
 #else
-	fprintf (stderr, "No PNG library support.\n");
+	fprintf(stderr, "No PNG library support.\n");
 #endif
 	fclose (in);
 	/* If the load failed, it must not be a PNG file. */
 			goto usage;
 		} else if (!strcmp (argv[i], "-i")) {
 			if (i == (argc - 2)) {
-				fprintf (stderr, "Error: -i specified without y or n.\n");
+				fprintf(stderr, "-i specified without y or n.\n");
 				no = 1;
 				goto usage;
 			}
 				/* Clear interlace. */
 				gdImageInterlace (im, 0);
 			} else {
-				fprintf (stderr, "Error: -i specified without y or n.\n");
+				fprintf(stderr, "Error: -i specified without y or n.\n");
 				no = 1;
 				goto usage;
 			}
 			printf ("%d alpha channels\n", nalpha);
 
 		} else {
-			fprintf (stderr, "Unknown argument: %s\n", argv[i]);
+			fprintf(stderr, "Unknown argument: %s\n", argv[i]);
 			break;
 		}
 	}
 			out = fopen (outFn, "wb");
 
 			if (!out) {
-				fprintf (stderr, "Unable to write to %s -- exiting\n", outFn);
+				fprintf(stderr, "Unable to write to %s -- exiting\n", outFn);
 				exit (1);
 			}
 		}
 #ifdef HAVE_LIBPNG
 		gdImagePng (im, out);
 #else
-		fprintf (stderr, "No PNG library support.\n");
+		fprintf(stderr, "No PNG library support.\n");
 #endif
 		if (!useStdinStdout) {
 			fclose (out);