Fix loading huge JPEGs

They fell back to gdk-pixbuf, then misrendered in the thumbnailer,
and crashed the program when loaded directly.

The second best we can do is scale them down, right after tiling,
which is a complex feature to add.
This commit is contained in:
Přemysl Eric Janouch 2022-03-09 17:58:19 +01:00
parent da507edd05
commit 5c34a6846a
Signed by: p
GPG Key ID: A0420B94F92B9493
2 changed files with 31 additions and 0 deletions

View File

@ -1112,6 +1112,28 @@ open_libjpeg_turbo(
? TJPF_CMYK ? TJPF_CMYK
: (G_BYTE_ORDER == G_LITTLE_ENDIAN ? TJPF_BGRX : TJPF_XRGB); : (G_BYTE_ORDER == G_LITTLE_ENDIAN ? TJPF_BGRX : TJPF_XRGB);
// The limit of Cairo/pixman is 32767. but JPEG can go as high as 65535.
// Prevent Cairo from throwing an error, and make use of libjpeg's scaling.
// gdk-pixbuf circumvents this check, producing unrenderable surfaces.
const int max = 32767;
int nfs = 0;
tjscalingfactor *fs = tjGetScalingFactors(&nfs), f = {0, 1};
if (fs && (width > max || height > max)) {
for (int i = 0; i < nfs; i++) {
if (TJSCALED(width, fs[i]) <= max &&
TJSCALED(height, fs[i]) <= max &&
fs[i].num * f.denom > f.num * fs[i].denom)
f = fs[i];
}
add_warning(ctx,
"the image is too large, and had to be scaled by %d/%d",
f.num, f.denom);
width = TJSCALED(width, f);
height = TJSCALED(height, f);
}
cairo_surface_t *surface = cairo_surface_t *surface =
cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height); cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
cairo_status_t surface_status = cairo_surface_status(surface); cairo_status_t surface_status = cairo_surface_status(surface);

View File

@ -163,6 +163,15 @@ adjust_thumbnail(cairo_surface_t *thumbnail, double row_height)
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr); cairo_paint(cr);
// Note that this doesn't get triggered with oversize input surfaces,
// even though nothing will be rendered.
if (cairo_surface_status(thumbnail) != CAIRO_STATUS_SUCCESS ||
cairo_surface_status(scaled) != CAIRO_STATUS_SUCCESS ||
cairo_pattern_status(pattern) != CAIRO_STATUS_SUCCESS ||
cairo_status(cr) != CAIRO_STATUS_SUCCESS)
g_warning("thumbnail scaling failed");
cairo_destroy(cr); cairo_destroy(cr);
return scaled; return scaled;
} }