From 3e1b5216ac2722e6b834b1c5dc622ff2e12c8faa Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 20 Nov 2014 21:39:35 +0100 Subject: [PATCH] Refactor searching for the SOI marker of inline JPEG image streams --- src/core/parser.js | 16 ---------------- src/core/stream.js | 11 +++++++++-- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/core/parser.js b/src/core/parser.js index 73eb75622..4af57fb26 100644 --- a/src/core/parser.js +++ b/src/core/parser.js @@ -372,22 +372,6 @@ var Parser = (function ParserClosure() { return new LZWStream(stream, maybeLength, earlyChange); } if (name === 'DCTDecode' || name === 'DCT') { - // According to the specification: for inline images, the ID operator - // shall be followed by a single whitespace character (unless it uses - // ASCII85Decode or ASCIIHexDecode filters). - // In practice this only seems to be followed for inline JPEG images, - // and generally ignoring the first byte of the stream if it is a - // whitespace char can even *cause* issues (e.g. in the CCITTFaxDecode - // filters used in issue2984.pdf). - // Hence when the first byte of the stream of an inline JPEG image is - // a whitespace character, we thus simply skip over it. - if (isCmd(this.buf1, 'ID')) { - var firstByte = stream.peekByte(); - if (firstByte === 0x0A /* LF */ || firstByte === 0x0D /* CR */ || - firstByte === 0x20 /* SPACE */) { - stream.skip(); - } - } xrefStreamStats[StreamType.DCT] = true; return new JpegStream(stream, maybeLength, stream.dict, this.xref); } diff --git a/src/core/stream.js b/src/core/stream.js index a84295697..4145b416b 100644 --- a/src/core/stream.js +++ b/src/core/stream.js @@ -857,8 +857,15 @@ var PredictorStream = (function PredictorStreamClosure() { */ var JpegStream = (function JpegStreamClosure() { function JpegStream(stream, maybeLength, dict, xref) { - // TODO: per poppler, some images may have 'junk' before that - // need to be removed + // Some images may contain 'junk' before the SOI (start-of-image) marker. + // Note: this seems to mainly affect inline images. + var ch; + while ((ch = stream.getByte()) !== -1) { + if (ch === 0xFF) { // Find the first byte of the SOI marker (0xFFD8). + stream.skip(-1); // Reset the stream position to the SOI. + break; + } + } this.stream = stream; this.maybeLength = maybeLength; this.dict = dict;