Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle 0x00E1 / 0x00E0 segments from Pixel phones #84

Merged
merged 3 commits into from Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/decoder.js
Expand Up @@ -623,6 +623,7 @@ var JpegImage = (function jpegImage() {
var quantizationTables = [], frames = [];
var huffmanTablesAC = [], huffmanTablesDC = [];
var fileMarker = readUint16();
var malformedDataOffset = -1;
this.comments = [];
if (fileMarker != 0xFFD8) { // SOI (Start of Image)
throw new Error("SOI not found");
Expand Down Expand Up @@ -813,7 +814,6 @@ var JpegImage = (function jpegImage() {
offset--;
}
break;

default:
if (data[offset - 3] == 0xFF &&
data[offset - 2] >= 0xC0 && data[offset - 2] <= 0xFE) {
Expand All @@ -822,6 +822,19 @@ var JpegImage = (function jpegImage() {
offset -= 3;
break;
}
else if (fileMarker === 0xE0 || fileMarker == 0xE1) {
// Recover from malformed APP1 markers popular in some phone models.
// See https://github.com/eugeneware/jpeg-js/issues/82
if (malformedDataOffset !== -1) {
throw new Error(`first unknown JPEG marker at offset ${malformedDataOffset.toString(16)}, second unknown JPEG marker ${fileMarker.toString(16)} at offset ${(offset - 1).toString(16)}`);
}
malformedDataOffset = offset - 1;
const nextOffset = readUint16();
if (data[offset + nextOffset - 2] === 0xFF) {
offset += nextOffset - 2;
break;
}
}
throw new Error("unknown JPEG marker " + fileMarker.toString(16));
}
fileMarker = readUint16();
Expand Down
Binary file added test/fixtures/table-with-bad-e1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/table-with-good-e1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions test/index.js
Expand Up @@ -12,6 +12,14 @@ const SUPER_LARGE_JPEG_BASE64 =

const SUPER_LARGE_JPEG_BUFFER = Buffer.from(SUPER_LARGE_JPEG_BASE64, 'base64');

it('should be able read image with a bad e1 marker not preceeded by ff', function () {
var jpegData = fixture('table-with-bad-e1.jpg');
var rawImageData = jpeg.decode(jpegData);
var expected = fixture('table-with-good-e1.jpg');
var rawExpectedImageData = jpeg.decode(expected);
expect(rawImageData.data).toEqual(rawExpectedImageData.data);
});

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