From 17f65908aee7a2e581b2cfd5d7766454e9b9fc1d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 15 Jul 2018 18:32:05 +0200 Subject: [PATCH] Add more validation of the /Filter entry, in image dictionaries, to the `PDFImage` constructor Given that the code is currently assuming that the /Filter entry is a `Name`, it cannot hurt to actually ensure that's the case. Also fixes an error message, for JPEG 2000 images with unsupported ColorSpaces, since `this.numComps` hasn't been initialized when it's accessed during the `throw new Error()` invocation. --- src/core/image.js | 31 +++++++++++++++++++------------ src/core/jpx.js | 2 +- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/core/image.js b/src/core/image.js index 32a26d157..fd87b4271 100644 --- a/src/core/image.js +++ b/src/core/image.js @@ -14,7 +14,7 @@ */ import { assert, FormatError, ImageKind, info, warn } from '../shared/util'; -import { isStream, Name } from './primitives'; +import { isName, isStream, Name } from './primitives'; import { ColorSpace } from './colorspace'; import { DecodeStream } from './stream'; import { JpegStream } from './jpeg_stream'; @@ -84,16 +84,23 @@ var PDFImage = (function PDFImageClosure() { this.image = image; var dict = image.dict; if (dict.has('Filter')) { - var filter = dict.get('Filter').name; - if (filter === 'JPXDecode') { - var jpxImage = new JpxImage(); - jpxImage.parseImageProperties(image.stream); - image.stream.reset(); - image.bitsPerComponent = jpxImage.bitsPerComponent; - image.numComps = jpxImage.componentsCount; - } else if (filter === 'JBIG2Decode') { - image.bitsPerComponent = 1; - image.numComps = 1; + const filter = dict.get('Filter'); + if (isName(filter)) { + switch (filter.name) { + case 'JPXDecode': + var jpxImage = new JpxImage(); + jpxImage.parseImageProperties(image.stream); + image.stream.reset(); + image.bitsPerComponent = jpxImage.bitsPerComponent; + image.numComps = jpxImage.componentsCount; + break; + case 'JBIG2Decode': + image.bitsPerComponent = 1; + image.numComps = 1; + break; + } + } else { + warn(`PDFImage - invalid /Filter entry in dictionary: "${filter}".`); } } // TODO cache rendered images? @@ -139,7 +146,7 @@ var PDFImage = (function PDFImageClosure() { colorSpace = Name.get('DeviceCMYK'); break; default: - throw new Error(`JPX images with ${this.numComps} ` + + throw new Error(`JPX images with ${image.numComps} ` + 'color components not supported.'); } } diff --git a/src/core/jpx.js b/src/core/jpx.js index b484f5ba4..da80ad2a3 100644 --- a/src/core/jpx.js +++ b/src/core/jpx.js @@ -142,7 +142,7 @@ var JpxImage = (function JpxImageClosure() { this.width = Xsiz - XOsiz; this.height = Ysiz - YOsiz; this.componentsCount = Csiz; - // Results are always returned as Uint8Arrays + // Results are always returned as `Uint8ClampedArray`s. this.bitsPerComponent = 8; return; }