diff --git a/libvips/foreign/jpeg.h b/libvips/foreign/jpeg.h index a36725bad..3eb654c23 100644 --- a/libvips/foreign/jpeg.h +++ b/libvips/foreign/jpeg.h @@ -62,6 +62,12 @@ extern "C" { #include #include +/* Our custom error message codes. + */ +#define JERR_VIPS_IMAGE_EOF (1000) +#define JWRN_VIPS_IMAGE_EOF JERR_VIPS_IMAGE_EOF +#define JERR_VIPS_TARGET_WRITE (1001) + /* Define a new error handler for when we bomb out. */ typedef struct { @@ -75,6 +81,8 @@ typedef struct { FILE *fp; /* fclose() if non-NULL */ } ErrorManager; +extern const char *vips__jpeg_message_table[]; + void vips__new_output_message(j_common_ptr cinfo); void vips__new_error_exit(j_common_ptr cinfo); diff --git a/libvips/foreign/jpeg2vips.c b/libvips/foreign/jpeg2vips.c index 17b3a7032..2a4c28d60 100644 --- a/libvips/foreign/jpeg2vips.c +++ b/libvips/foreign/jpeg2vips.c @@ -245,10 +245,10 @@ source_fill_input_buffer(j_decompress_ptr cinfo) /* Knock the output out of cache. */ vips_foreign_load_invalidate(src->jpeg->out); - ERREXIT(cinfo, JERR_INPUT_EOF); + ERREXIT(cinfo, JERR_VIPS_IMAGE_EOF); } else - WARNMS(cinfo, JWRN_JPEG_EOF); + WARNMS(cinfo, JWRN_VIPS_IMAGE_EOF); /* Insert a fake EOI marker. */ @@ -272,10 +272,10 @@ source_fill_input_buffer_mappable(j_decompress_ptr cinfo) /* Knock the output out of cache. */ vips_foreign_load_invalidate(src->jpeg->out); - ERREXIT(cinfo, JERR_INPUT_EOF); + ERREXIT(cinfo, JERR_VIPS_IMAGE_EOF); } else - WARNMS(cinfo, JWRN_JPEG_EOF); + WARNMS(cinfo, JWRN_VIPS_IMAGE_EOF); /* Insert a fake EOI marker. */ @@ -463,6 +463,9 @@ readjpeg_new(VipsSource *source, VipsImage *out, jpeg->shrink = shrink; jpeg->fail_on = fail_on; jpeg->cinfo.err = jpeg_std_error(&jpeg->eman.pub); + jpeg->cinfo.err->addon_message_table = vips__jpeg_message_table; + jpeg->cinfo.err->first_addon_message = 1000; + jpeg->cinfo.err->last_addon_message = 1001; jpeg->eman.pub.error_exit = vips__new_error_exit; jpeg->eman.pub.emit_message = readjpeg_emit_message; jpeg->eman.pub.output_message = vips__new_output_message; diff --git a/libvips/foreign/tiff2vips.c b/libvips/foreign/tiff2vips.c index 9b553d9d8..397703181 100644 --- a/libvips/foreign/tiff2vips.c +++ b/libvips/foreign/tiff2vips.c @@ -1973,7 +1973,7 @@ rtiff_decompress_jpeg_fill_input_buffer(j_decompress_ptr cinfo) * buffer, so any request for more data beyond the given buffer size * is treated as an error. */ - WARNMS(cinfo, JWRN_JPEG_EOF); + WARNMS(cinfo, JWRN_VIPS_IMAGE_EOF); /* Insert a fake EOI marker */ @@ -2163,6 +2163,9 @@ rtiff_decompress_jpeg(Rtiff *rtiff, void *data, size_t data_len, void *out) if (setjmp(eman.jmp) == 0) { cinfo.err = jpeg_std_error(&eman.pub); + cinfo.err->addon_message_table = vips__jpeg_message_table; + cinfo.err->first_addon_message = 1000; + cinfo.err->last_addon_message = 1001; eman.pub.error_exit = vips__new_error_exit; eman.pub.emit_message = rtiff_decompress_jpeg_emit_message; eman.pub.output_message = vips__new_output_message; diff --git a/libvips/foreign/vips2jpeg.c b/libvips/foreign/vips2jpeg.c index 7757e368a..abeb2e537 100644 --- a/libvips/foreign/vips2jpeg.c +++ b/libvips/foreign/vips2jpeg.c @@ -159,6 +159,12 @@ #define MAX_BYTES_IN_MARKER 65533 /* maximum data len of a JPEG marker */ #define MAX_DATA_BYTES_IN_MARKER (MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN) +const char *vips__jpeg_message_table[] = { + "premature end of JPEG image", + "unable to write to target", + NULL +}; + /* New output message method - send to VIPS. */ void @@ -229,6 +235,9 @@ write_new(void) write->row_pointer = NULL; write->cinfo.err = jpeg_std_error(&write->eman.pub); + write->cinfo.err->addon_message_table = vips__jpeg_message_table; + write->cinfo.err->first_addon_message = 1000; + write->cinfo.err->last_addon_message = 1001; write->cinfo.dest = NULL; write->eman.pub.error_exit = vips__new_error_exit; write->eman.pub.output_message = vips__new_output_message; @@ -811,7 +820,7 @@ empty_output_buffer(j_compress_ptr cinfo) if (vips_target_write(dest->target, dest->buf, TARGET_BUFFER_SIZE)) - ERREXIT(cinfo, JERR_FILE_WRITE); + ERREXIT(cinfo, JERR_VIPS_TARGET_WRITE); dest->pub.next_output_byte = dest->buf; dest->pub.free_in_buffer = TARGET_BUFFER_SIZE; @@ -839,7 +848,7 @@ term_destination(j_compress_ptr cinfo) if (vips_target_write(dest->target, dest->buf, TARGET_BUFFER_SIZE - dest->pub.free_in_buffer)) - ERREXIT(cinfo, JERR_FILE_WRITE); + ERREXIT(cinfo, JERR_VIPS_TARGET_WRITE); } /* Set dest to one of our objects. diff --git a/libvips/foreign/vips2tiff.c b/libvips/foreign/vips2tiff.c index e80fc3db6..7a93d0f3d 100644 --- a/libvips/foreign/vips2tiff.c +++ b/libvips/foreign/vips2tiff.c @@ -732,6 +732,9 @@ wtiff_compress_jpeg(Wtiff *wtiff, // we could have one of these per thread and reuse it for a small speedup cinfo.err = jpeg_std_error(&eman.pub); + cinfo.err->addon_message_table = vips__jpeg_message_table; + cinfo.err->first_addon_message = 1000; + cinfo.err->last_addon_message = 1001; cinfo.dest = NULL; eman.pub.error_exit = vips__new_error_exit; eman.pub.output_message = vips__new_output_message; @@ -740,7 +743,7 @@ wtiff_compress_jpeg(Wtiff *wtiff, // we need a line buffer to pad edge tiles line = VIPS_MALLOC(NULL, wtiff->tilew * sizeof_pel); - /* Error handling. The error message will have ben set by our handlers. + /* Error handling. The error message will have been set by our handlers. */ if (setjmp(eman.jmp)) { jpeg_destroy_compress(&cinfo); @@ -805,6 +808,9 @@ wtiff_compress_jpeg_tables(Wtiff *wtiff, struct jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); + cinfo.err->addon_message_table = vips__jpeg_message_table; + cinfo.err->first_addon_message = 1000; + cinfo.err->last_addon_message = 1001; jpeg_create_compress(&cinfo); /* Attach output.