From 8d52a1e92a450f5b43049b3f1527bad8aaf79bfe Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Tue, 8 Nov 2011 13:53:56 -0800 Subject: [PATCH] Add another way to detect cmyk images. --- src/parser.js | 2 +- src/stream.js | 28 ++++++++++++++++++++++++---- src/worker_loader.js | 3 ++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/parser.js b/src/parser.js index a740615ed..93a3f21b5 100644 --- a/src/parser.js +++ b/src/parser.js @@ -236,7 +236,7 @@ var Parser = (function parserParser() { return new LZWStream(stream, earlyChange); } else if (name == 'DCTDecode' || name == 'DCT') { var bytes = stream.getBytes(length); - return new JpegStream(bytes, stream.dict); + return new JpegStream(bytes, stream.dict, this.xref); } else if (name == 'ASCII85Decode' || name == 'A85') { return new Ascii85Stream(stream); } else if (name == 'ASCIIHexDecode' || name == 'AHx') { diff --git a/src/stream.js b/src/stream.js index d8a681a88..7634e50bb 100644 --- a/src/stream.js +++ b/src/stream.js @@ -776,6 +776,25 @@ var JpegStream = (function jpegStream() { return false; } + function isCmykAdobe(bytes) { + var maxBytesScanned = Math.max(bytes.length - 16, 1024); + // Looking for APP14, 'Adobe' + for (var i = 0; i < maxBytesScanned; ++i) { + if (bytes[i] == 0xFF && bytes[i + 1] == 0xEE && + bytes[i + 2] == 0x00 && bytes[i + 3] == 0x0E && + bytes[i + 4] == 0x41 && bytes[i + 5] == 0x64 && + bytes[i + 6] == 0x6F && bytes[i + 7] == 0x62 && + bytes[i + 8] == 0x65 && bytes[i + 9] == 0 && + bytes[i + 15] == 2 ) { + return true; + } + // scanning until frame tag + if (bytes[i] == 0xFF && bytes[i + 1] == 0xC0) + break; + } + return false; + } + function fixAdobeImage(bytes) { // Inserting 'EMBED' marker after JPEG signature var embedMarker = new Uint8Array([0xFF, 0xEC, 0, 8, 0x45, 0x4D, 0x42, 0x45, @@ -789,19 +808,20 @@ var JpegStream = (function jpegStream() { return newBytes; } - function constructor(bytes, dict) { + function constructor(bytes, dict, xref) { // TODO: per poppler, some images may have 'junk' before that // need to be removed this.dict = dict; - + // Flag indicating wether the image can be natively loaded. this.isNative = true; if (isAdobeImage(bytes)) { // when bug 674619 land, let's check if browser can do // normal cmyk and then we won't have to the following - var cs = dict.get('ColorSpace'); + var cs = xref.fetchIfRef(dict.get('ColorSpace')); if (isName(cs) && cs.name === 'DeviceCMYK') { + //if (isCmykAdobe(bytes)) { this.isNative = false; this.bytes = bytes; } else { @@ -826,7 +846,7 @@ var JpegStream = (function jpegStream() { var height = jpegImage.height; var dataLength = width * height * 4; var data = new Uint8Array(dataLength); - jpegImage.getData(data, width, height); + jpegImage.getData(data, width, height, true); this.buffer = data; this.bufferLength = data.length; }; diff --git a/src/worker_loader.js b/src/worker_loader.js index cb0a91071..7141fa3e3 100644 --- a/src/worker_loader.js +++ b/src/worker_loader.js @@ -40,7 +40,8 @@ function onMessageLoader(evt) { 'parser.js', 'pattern.js', 'stream.js', - 'worker.js' + 'worker.js', + '../external/jpgjs/jpg.js' ]; // Load all the files.