Skip to content

Commit 11c5683

Browse files
committed
Use ImageDecoder to decode cmyk jpeg images (bug 1965870)
1 parent 3f1ecc1 commit 11c5683

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/core/jpeg_stream.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ class JpegStream extends DecodeStream {
4444
);
4545
}
4646

47+
static get canUseImageDecoderCMYK() {
48+
return shadow(
49+
this,
50+
"canUseImageDecoderCMYK",
51+
this.#isImageDecoderSupported
52+
? ImageDecoder.isTypeSupported("image/x-jpeg-pdf")
53+
: Promise.resolve(false)
54+
);
55+
}
56+
4757
static setOptions({ isImageDecoderSupported = false }) {
4858
this.#isImageDecoderSupported = isImageDecoderSupported;
4959
}
@@ -171,6 +181,14 @@ class JpegStream extends DecodeStream {
171181
if (!useImageDecoder) {
172182
return null;
173183
}
184+
let type = "image/jpeg";
185+
if (useImageDecoder.cmyk) {
186+
if (await JpegStream.canUseImageDecoderCMYK) {
187+
type = "image/x-jpeg-pdf";
188+
} else {
189+
return null;
190+
}
191+
}
174192
if (useImageDecoder.exifStart) {
175193
// Replace the entire EXIF-block with dummy data, to ensure that a
176194
// non-default EXIF orientation won't cause the image to be rotated
@@ -182,7 +200,7 @@ class JpegStream extends DecodeStream {
182200
}
183201
decoder = new ImageDecoder({
184202
data,
185-
type: "image/jpeg",
203+
type,
186204
preferAnimation: false,
187205
});
188206

src/core/jpg.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ class JpegImage {
810810
}
811811

812812
static canUseImageDecoder(data, colorTransform = -1) {
813-
let exifOffsets = null;
813+
let jpegInfo = null;
814814
let offset = 0;
815815
let numComponents = null;
816816
let fileMarker = readUint16(data, offset);
@@ -838,12 +838,12 @@ class JpegImage {
838838
appData[4] === 0 &&
839839
appData[5] === 0
840840
) {
841-
if (exifOffsets) {
841+
if (jpegInfo) {
842842
throw new JpegError("Duplicate EXIF-blocks found.");
843843
}
844844
// Don't do the EXIF-block replacement here, see `JpegStream`,
845845
// since that can modify the original PDF document.
846-
exifOffsets = { exifStart: oldOffset + 6, exifEnd: newOffset };
846+
jpegInfo = { exifStart: oldOffset + 6, exifEnd: newOffset };
847847
}
848848
fileMarker = readUint16(data, offset);
849849
offset += 2;
@@ -868,13 +868,17 @@ class JpegImage {
868868
fileMarker = readUint16(data, offset);
869869
offset += 2;
870870
}
871-
if (numComponents === 4) {
872-
return null;
873-
}
874871
if (numComponents === 3 && colorTransform === 0) {
875872
return null;
876873
}
877-
return exifOffsets || {};
874+
if (numComponents === 4) {
875+
if (jpegInfo) {
876+
jpegInfo.cmyk = true;
877+
} else {
878+
jpegInfo = { cmyk: true };
879+
}
880+
}
881+
return jpegInfo || {};
878882
}
879883

880884
parse(data, { dnlScanLines = null } = {}) {

0 commit comments

Comments
 (0)