From 48de7651cee7b240de267e4491186f39eb64f4cd Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Sun, 17 Aug 2014 18:53:50 -0700 Subject: [PATCH] Apply the GRAYSCALE_1BPP optimization when `needsDecode` is set. The scanned, black-and-white document at https://bugzilla.mozilla.org/show_bug.cgi?id=835380 doesn't benefit from the critical GRAYSCALE_1BPP optimization because the optimization is skipped if `needsDecode` is set. This change addresses that, and reduces both rendering time and memory usage for that document by almost 10x. --- src/core/image.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/core/image.js b/src/core/image.js index fbcce33f0..ec01437d5 100644 --- a/src/core/image.js +++ b/src/core/image.js @@ -14,8 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* globals ColorSpace, DecodeStream, error, info, isArray, ImageKind, isStream, - JpegStream, JpxImage, Name, Promise, Stream, warn */ +/* globals assert, ColorSpace, DecodeStream, error, info, isArray, ImageKind, + isStream, JpegStream, JpxImage, Name, Promise, Stream, warn */ 'use strict'; @@ -531,10 +531,11 @@ var PDFImage = (function PDFImageClosure() { var kind; if (this.colorSpace.name === 'DeviceGray' && bpc === 1) { kind = ImageKind.GRAYSCALE_1BPP; - } else if (this.colorSpace.name === 'DeviceRGB' && bpc === 8) { + } else if (this.colorSpace.name === 'DeviceRGB' && bpc === 8 && + !this.needsDecode) { kind = ImageKind.RGB_24BPP; } - if (kind && !this.smask && !this.mask && !this.needsDecode && + if (kind && !this.smask && !this.mask && drawWidth === originalWidth && drawHeight === originalHeight) { imgData.kind = kind; @@ -551,6 +552,14 @@ var PDFImage = (function PDFImageClosure() { newArray.set(imgArray); imgData.data = newArray; } + if (this.needsDecode) { + // Invert the buffer (which must be grayscale if we reached here). + assert(kind === ImageKind.GRAYSCALE_1BPP); + var buffer = imgData.data; + for (var i = 0, ii = buffer.length; i < ii; i++) { + buffer[i] ^= 0xff; + } + } return imgData; } if (this.image instanceof JpegStream && !this.smask && !this.mask) {