Skip to content

Commit

Permalink
fix: don’t fail if scan contains trailing bytes (jpeg-js#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
rluba committed Apr 21, 2020
1 parent ce5433c commit 5737c99
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,18 @@ var JpegImage = (function jpegImage() {
}
}

if (mcu === mcuExpected) {
// Skip trailing bytes at the end of the scan - until we reach the next marker
do {
if (data[offset] === 0xFF) {
if (data[offset + 1] !== 0x00) {
break;
}
}
offset += 1;
} while (offset < data.length - 2);
}

// find marker
bitsCount = 0;
marker = (data[offset] << 8) | data[offset + 1];
Expand Down
Binary file added test/fixtures/redbox-with-trailing-bytes.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ it('should be able to decode a JPEG with RST intervals', function(t) {
t.end();
});

it('should be able to decode a JPEG with trailing bytes', function(t) {
var jpegData = fixture('redbox-with-trailing-bytes.jpg');
var rawImageData = jpeg.decode(jpegData);
var expected = fixture('redbox.jpg');
var rawExpectedImageData = jpeg.decode(expected);
t.deepEqual(rawImageData.data, rawExpectedImageData.data);
t.end();
});

it('should be able to decode a grayscale JPEG', function(t) {
var jpegData = fixture('apsara.jpg');
var rawImageData = jpeg.decode(jpegData);
Expand Down

0 comments on commit 5737c99

Please sign in to comment.