Commits

Ondřej Surý  committed 06ed9f6

Add gdImageCreate*Ex functions to support extra PHP call

  • Participants
  • Parent commits 5f32d7d
  • Branches jpeg_ignore_warning

Comments (0)

Files changed (2)

 /* Text functions take these. */
 typedef gdFont *gdFontPtr;
 
+typedef void (*jpeg_message_handler)(void *, int);
+
 /* For backwards compatibility only. Use gdImageSetStyle()
 	for MUCH more flexible line drawing. Also see
 	gdImageSetBrush(). */
 BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMPCtx (gdIOCtx * infile);
 BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMPPtr (int size, void *data);
 BGD_DECLARE(gdImagePtr) gdImageCreateFromJpeg (FILE * infile);
-BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegEx (FILE * infile, int ignore_warning);
+BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegEx (FILE * infile, int ignore_warning, jpeg_message_handler message_handler);
 BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx (gdIOCtx * infile);
-BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtxEx (gdIOCtx * infile, int ignore_warning);
+BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtxEx (gdIOCtx * infile, int ignore_warning, jpeg_message_handler message_handler);
 BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegPtr (int size, void *data);
-BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegPtrEx (int size, void *data, int ignore_warning);
+BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegPtrEx (int size, void *data, int ignore_warning, jpeg_message_handler message_handler);
 BGD_DECLARE(gdImagePtr) gdImageCreateFromWebp (FILE * inFile);
 BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpPtr (int size, void *data);
 BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpCtx (gdIOCtx * infile);

File src/gd_jpeg.c

 
 typedef struct _jmpbuf_wrapper {
 	jmp_buf jmpbuf;
+        int ignore_warning;
 }
 jmpbuf_wrapper;
 
+static long jpeg_emit_message(j_common_ptr jpeg_info, int level)
+{
+	char message[JMSG_LENGTH_MAX];
+	jmpbuf_wrapper *jmpbufw;
+	int ignore_warning = 0;
+
+	jmpbufw = (jmpbuf_wrapper *) jpeg_info->client_data;
+
+	if (jmpbufw != 0) {
+		ignore_warning = jmpbufw->ignore_warning;
+	}
+
+	(jpeg_info->err->format_message)(jpeg_info,message);
+
+	/* It is a warning message */
+	if (level < 0) {
+		/* display only the 1st warning, as would do a default libjpeg
+		 * unless strace_level >= 3
+		 */
+		if ((jpeg_info->err->num_warnings == 0) || (jpeg_info->err->trace_level >= 3)) {
+			if (!ignore_warning) {
+				fprintf(stderr, "gd-jpeg, libjpeg: recoverable error: %s\n", message);
+			}
+		}
+
+		jpeg_info->err->num_warnings++;
+	} else {
+		/* strace msg, Show it if trace_level >= level. */
+		if (jpeg_info->err->trace_level >= level) {
+			if (!ignore_warning) {
+				fprintf(stderr, "gd-jpeg, libjpeg: strace message: %s\n", message);
+			}
+		}
+	}
+	return 1;
+}
+
 /* Called by the IJG JPEG library upon encountering a fatal error */
 static void fatal_jpeg_error(j_common_ptr cinfo)
 {
 	jmpbuf_wrapper *jmpbufw;
+	int ignore_warning = 0;
 
 	fprintf(stderr, "gd-jpeg: JPEG library reports unrecoverable error: ");
 	(*cinfo->err->output_message)(cinfo);
 	fflush(stderr);
 
 	jmpbufw = (jmpbuf_wrapper *)cinfo->client_data;
+
 	jpeg_destroy(cinfo);
 
 	if(jmpbufw != 0) {
 
 BGD_DECLARE(gdImagePtr) gdImageCreateFromJpeg(FILE *inFile)
 {
-	return gdImageCreateFromJpegEx(inFile, 1);
+	return gdImageCreateFromJpegEx(inFile, 1, NULL);
 }
-BGD_DECLARE(gdImagePtr) gdImageCreateFromJpeg(FILE *inFile, int ignore_warning)
+BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegEx(FILE *inFile, int ignore_warning, void (*message_handler)(void *,int))
 {
 	gdImagePtr im;
 	gdIOCtx *in = gdNewFileCtx(inFile);
 	if (in == NULL) return NULL;
-	im = gdImageCreateFromJpegCtxEx(in, ignore_warning);
+	im = gdImageCreateFromJpegCtxEx(in, ignore_warning, message_handler);
 	in->gd_free(in);
 	return im;
 }
 
 BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegPtr(int size, void *data)
 {
-	return gdImageCreateFromJpegPtrEx(size, data, 1);
+	return gdImageCreateFromJpegPtrEx(size, data, 1, NULL);
 }
-BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegPtr(int size, void *data, int ignore_warning)
+BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegPtrEx(int size, void *data, int ignore_warning, jpeg_message_handler message_handler)
 {
 	gdImagePtr im;
 	gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);
 	if(!in) {
 		return 0;
 	}
-	im = gdImageCreateFromJpegCtxEx(in, ignore_warning);
+	im = gdImageCreateFromJpegCtxEx(in, ignore_warning, message_handler);
 	in->gd_free(in);
 	return im;
 }
  */
 BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx(gdIOCtx *infile)
 {
-	return gdImageCreateFromJpegCtxEx(infile, 1);
+	return gdImageCreateFromJpegCtxEx(infile, 1, NULL);
 }
-BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtxEx(gdIOCtx *infile, int ignore_warning)
+BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtxEx(gdIOCtx *infile, int ignore_warning, jpeg_message_handler message_handler)
 {
 	struct jpeg_decompress_struct cinfo;
 	struct jpeg_error_mgr jerr;
 	memset(&cinfo, 0, sizeof(cinfo));
 	memset(&jerr, 0, sizeof(jerr));
 
+	jmpbufw.ignore_warning = ignore_warning;
+
 	cinfo.err = jpeg_std_error(&jerr);
 	cinfo.client_data = &jmpbufw;
 
+	cinfo.err->emit_message = message_handler ? message_handler : jpeg_emit_message;
+
 	if(setjmp(jmpbufw.jmpbuf) != 0) {
 		/* we're here courtesy of longjmp */
 		if(row) {