Skip to content

Commit

Permalink
Prefer the Width/Height of the image data, rather than the image dict…
Browse files Browse the repository at this point in the history
…ionary, for JPEG 2000 images (issue 9650)

According to the PDF specification, see https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf#page=45
> When using the JPXDecode filter with image XObjects, the following changes to and constraints on some entries in the image dictionary shall apply (see 8.9.5, "Image Dictionaries" for details on these entries):
>
>  - Width and Height shall match the corresponding width and height values in the JPEG2000 data.
>
>  - . . .

Hence it seems reasonable to use the Width/Height of the image data *itself*, rather than the image dictionary when there's a mismatch. Given that JPEG 2000 images are already being parsed, in order to obtain basic parameters, the actual Width/Height is readily available in the `PDFImage` constructor.
  • Loading branch information
Snuffleupagus committed Jul 23, 2018
1 parent 5b74df3 commit 379f1c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/core/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ var PDFImage = (function PDFImageClosure() {
switch (filter.name) {
case 'JPXDecode':
const jpxProperties = JpxImage.parseImageProperties(image.stream);
image.width = jpxProperties.width;
image.height = jpxProperties.height;
image.bitsPerComponent = jpxProperties.bitsPerComponent;
image.numComps = jpxProperties.componentsCount;
break;
Expand All @@ -103,13 +105,23 @@ var PDFImage = (function PDFImageClosure() {
}
// TODO cache rendered images?

this.width = dict.get('Width', 'W');
this.height = dict.get('Height', 'H');
let width = dict.get('Width', 'W');
let height = dict.get('Height', 'H');

if (this.width < 1 || this.height < 1) {
throw new FormatError(`Invalid image width: ${this.width} or ` +
`height: ${this.height}`);
if ((Number.isInteger(image.width) && image.width > 0) &&
(Number.isInteger(image.height) && image.height > 0) &&
(image.width !== width || image.height !== height)) {
warn('PDFImage - using the Width/Height of the image data, ' +
'rather than the image dictionary.');
width = image.width;
height = image.height;
}
if (width < 1 || height < 1) {
throw new FormatError(`Invalid image width: ${width} or ` +
`height: ${height}`);
}
this.width = width;
this.height = height;

this.interpolate = dict.get('Interpolate', 'I') || false;
this.imageMask = dict.get('ImageMask', 'IM') || false;
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/issue9650.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/mozilla/pdf.js/files/1898753/Kred.Eingangsrechnungen.pdf
8 changes: 8 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3259,6 +3259,14 @@
"lastPage": 1,
"type": "eq"
},
{ "id": "issue9650",
"file": "pdfs/issue9650.pdf",
"md5": "20d50bda6b1080b6d9088811299c791e",
"rounds": 1,
"link": true,
"lastPage": 1,
"type": "eq"
},
{ "id": "issue9679",
"file": "pdfs/issue9679.pdf",
"md5": "3077d06add3875705aa1021c7b116023",
Expand Down

0 comments on commit 379f1c8

Please sign in to comment.