summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorSebastian Rasmussen <sebras@gmail.com>2016-10-13 20:03:33 +0800
committerSebastian Rasmussen <sebras@gmail.com>2016-10-14 01:19:07 +0800
commit160369e81e6675d122b210b45844c0dc86ec8876 (patch)
tree2b8ea723245ec7428bf7069ae696819c443cec7f /source
parented229f9ac6c2d85e2cdd789aba03e1dfd3cd55fc (diff)
downloadmupdf-160369e81e6675d122b210b45844c0dc86ec8876.tar.xz
pnm: Workaround for b/w PAM images with packed samples.
Diffstat (limited to 'source')
-rw-r--r--source/fitz/load-pnm.c26
1 files changed, 24 insertions, 2 deletions
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++)