diff --git a/src/core/jpeg_stream.js b/src/core/jpeg_stream.js index 4af24eeb8e733..c11266ac187fa 100644 --- a/src/core/jpeg_stream.js +++ b/src/core/jpeg_stream.js @@ -44,6 +44,16 @@ class JpegStream extends DecodeStream { ); } + static get canUseImageDecoderCMYK() { + return shadow( + this, + "canUseImageDecoderCMYK", + this.#isImageDecoderSupported + ? ImageDecoder.isTypeSupported("image/x-jpeg-pdf") + : Promise.resolve(false) + ); + } + static setOptions({ isImageDecoderSupported = false }) { this.#isImageDecoderSupported = isImageDecoderSupported; } @@ -171,6 +181,14 @@ class JpegStream extends DecodeStream { if (!useImageDecoder) { return null; } + let type = "image/jpeg"; + if (useImageDecoder.cmyk) { + if (await JpegStream.canUseImageDecoderCMYK) { + type = "image/x-jpeg-pdf"; + } else { + return null; + } + } if (useImageDecoder.exifStart) { // Replace the entire EXIF-block with dummy data, to ensure that a // non-default EXIF orientation won't cause the image to be rotated @@ -182,7 +200,7 @@ class JpegStream extends DecodeStream { } decoder = new ImageDecoder({ data, - type: "image/jpeg", + type, preferAnimation: false, }); diff --git a/src/core/jpg.js b/src/core/jpg.js index 90ef8ade97c9a..34b423de15015 100644 --- a/src/core/jpg.js +++ b/src/core/jpg.js @@ -810,7 +810,7 @@ class JpegImage { } static canUseImageDecoder(data, colorTransform = -1) { - let exifOffsets = null; + const jpegInfo = Object.create(null); let offset = 0; let numComponents = null; let fileMarker = readUint16(data, offset); @@ -838,12 +838,13 @@ class JpegImage { appData[4] === 0 && appData[5] === 0 ) { - if (exifOffsets) { + if (jpegInfo.exifStart) { throw new JpegError("Duplicate EXIF-blocks found."); } // Don't do the EXIF-block replacement here, see `JpegStream`, // since that can modify the original PDF document. - exifOffsets = { exifStart: oldOffset + 6, exifEnd: newOffset }; + jpegInfo.exifStart = oldOffset + 6; + jpegInfo.exifEnd = newOffset; } fileMarker = readUint16(data, offset); offset += 2; @@ -868,13 +869,13 @@ class JpegImage { fileMarker = readUint16(data, offset); offset += 2; } - if (numComponents === 4) { - return null; - } if (numComponents === 3 && colorTransform === 0) { return null; } - return exifOffsets || {}; + if (numComponents === 4) { + jpegInfo.cmyk = true; + } + return jpegInfo; } parse(data, { dnlScanLines = null } = {}) {