From 160369e81e6675d122b210b45844c0dc86ec8876 Mon Sep 17 00:00:00 2001 From: Sebastian Rasmussen Date: Thu, 13 Oct 2016 20:03:33 +0800 Subject: pnm: Workaround for b/w PAM images with packed samples. --- source/fitz/load-pnm.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'source') diff --git a/source/fitz/load-pnm.c b/source/fitz/load-pnm.c index 3f572907..1bb5098b 100644 --- a/source/fitz/load-pnm.c +++ b/source/fitz/load-pnm.c @@ -500,7 +500,7 @@ pam_binary_read_image(fz_context *ctx, struct info *pnm, unsigned char *p, unsig if (!onlymeta) { unsigned char *dp; - int x, y, k; + int x, y, k, packed = 0; int w, h, n; img = fz_new_pixmap(ctx, pnm->cs, pnm->width, pnm->height, pnm->alpha); @@ -510,11 +510,33 @@ pam_binary_read_image(fz_context *ctx, struct info *pnm, unsigned char *p, unsig h = img->h; n = img->n; - if (e - p < w * h * n * (pnm->maxval < 256 ? 1 : 2)) + if (pnm->maxval == 1 && e - p < w * h * n) + { + if (e - p < w * h * n / 8) + fz_throw(ctx, FZ_ERROR_GENERIC, "truncated image"); + packed = 1; + } + else if (e - p < w * h * n * (pnm->maxval < 256 ? 1 : 2)) fz_throw(ctx, FZ_ERROR_GENERIC, "truncated image"); if (pnm->maxval == 255) memcpy(dp, p, w * h * n); + else if (bitmap && packed) + { + /* some encoders incorrectly pack bits into bytes and inverts the image */ + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + { + for (k = 0; k < n; k++) + { + *dp++ = (*p & (1 << (7 - (x & 0x7)))) ? 0x00 : 0xff; + if ((x & 0x7) == 7) + p++; + } + if (w & 0x7) + p++; + } + } else if (bitmap) { for (y = 0; y < h; y++) -- cgit v1.2.3