Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer the Width/Height of the image data, rather than the image dictionary, for JPEG 2000 images (issue 9650) #9897

Merged
merged 2 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions src/core/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -84,27 +84,47 @@ 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.width = jpxImage.width;
image.height = jpxImage.height;
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?

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 Expand Up @@ -139,7 +159,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.');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/jpx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
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