aboutsummaryrefslogtreecommitdiff
path: root/fiv-io.c
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2022-03-09 17:58:19 +0100
committerPřemysl Eric Janouch <p@janouch.name>2022-03-09 18:04:36 +0100
commit5c34a6846ac874ada6b64eec9e140cb4e9b04291 (patch)
treee515e7b99f4fbd088f21451e168001a7698b23f3 /fiv-io.c
parentda507edd055e39fa4f9f703371b8c857031cef8c (diff)
downloadfiv-5c34a6846ac874ada6b64eec9e140cb4e9b04291.tar.gz
fiv-5c34a6846ac874ada6b64eec9e140cb4e9b04291.tar.xz
fiv-5c34a6846ac874ada6b64eec9e140cb4e9b04291.zip
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.
Diffstat (limited to 'fiv-io.c')
-rw-r--r--fiv-io.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/fiv-io.c b/fiv-io.c
index 870624f..1968a80 100644
--- a/fiv-io.c
+++ b/fiv-io.c
@@ -1112,6 +1112,28 @@ open_libjpeg_turbo(
? TJPF_CMYK
: (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_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
cairo_status_t surface_status = cairo_surface_status(surface);