Skip to content

Commit

Permalink
jpeg: implement custom message codes and table
Browse files Browse the repository at this point in the history
Use the "add-on" message table to ensure compatibility with jpegli,
which lacks its own message codes and table.

Resolves: libvips#4206.
  • Loading branch information
kleisauke committed Oct 18, 2024
1 parent a697fe7 commit 1832801
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
8 changes: 8 additions & 0 deletions libvips/foreign/jpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ extern "C" {
#include <jpeglib.h>
#include <jerror.h>

/* 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 {
Expand All @@ -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);

Expand Down
11 changes: 7 additions & 4 deletions libvips/foreign/jpeg2vips.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion libvips/foreign/tiff2vips.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions libvips/foreign/vips2jpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion libvips/foreign/vips2tiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1832801

Please sign in to comment.