From 8c4b7d0439bd74e8e70384a40f8db22227e41a57 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 29 Dec 2017 17:15:08 +0100 Subject: [PATCH] Avoid truncating JPEG images with DeviceGray ColourSpaces when using the `src/core/jpg.js` built-in decoder The bug that this patch fixes is limited to the built-in JPEG decoder, and was unearthed by PR 9260. The underlying issue has existed since PR 6984, where the contents of this patch ought to have been included (if it weren't for the fact that we had no *easy* way to test `src/core/jpg.js` back then). *Please note:* The slight movement in the reference test is a result of using the `src/core/jpg.js` decoder, rather than the native browser one. --- src/core/image.js | 23 +++++++++++++++-------- test/test_manifest.json | 3 ++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/core/image.js b/src/core/image.js index cfc6555c8..7121d83ce 100644 --- a/src/core/image.js +++ b/src/core/image.js @@ -546,14 +546,21 @@ var PDFImage = (function PDFImageClosure() { } return imgData; } - if (this.image instanceof JpegStream && !this.smask && !this.mask && - (this.colorSpace.name === 'DeviceGray' || - this.colorSpace.name === 'DeviceRGB' || - this.colorSpace.name === 'DeviceCMYK')) { - imgData.kind = ImageKind.RGB_24BPP; - imgData.data = this.getImageBytes(originalHeight * rowBytes, - drawWidth, drawHeight, true); - return imgData; + if (this.image instanceof JpegStream && !this.smask && !this.mask) { + let imageLength = originalHeight * rowBytes; + switch (this.colorSpace.name) { + case 'DeviceGray': + // Avoid truncating the image, since `JpegImage.getData` + // will expand the image data when `forceRGB === true`. + imageLength *= 3; + /* falls through */ + case 'DeviceRGB': + case 'DeviceCMYK': + imgData.kind = ImageKind.RGB_24BPP; + imgData.data = this.getImageBytes(imageLength, + drawWidth, drawHeight, /* forceRGB = */ true); + return imgData; + } } } diff --git a/test/test_manifest.json b/test/test_manifest.json index 227f1c3a7..3958b19c6 100644 --- a/test/test_manifest.json +++ b/test/test_manifest.json @@ -1318,7 +1318,8 @@ "link": true, "firstPage": 2, "lastPage": 2, - "type": "eq" + "type": "eq", + "nativeImageDecoderSupport": "none" }, { "id": "issue6071", "file": "pdfs/issue6071.pdf",