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 2 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
5 changes: 4 additions & 1 deletion lib/decoder.js
Expand Up @@ -813,7 +813,10 @@ var JpegImage = (function jpegImage() {
offset--;
}
break;

case 0xE0:
case 0xE1: // Unused bytes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about moving this into default block and something like...

// Recover from malformed APP1 markers popular in some phone models.
// See https://github.com/eugeneware/jpeg-js/issues/82
if (data[offset - 2] === 0x00 && data[offset - 1] === 0xE1) {
  const nextOffset = readUint16();
  if (data[nextOffset] === 0xFF) {
    offset = nextOffset;
    break;
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree with you. In hindsight I should have seen that some of the files contained more than 1 0xFFE1 segment header and concluded that they aren't unique. Headers should also always start with 0xFF according to the JFIF standards, so you make a good point that 0x00E1 is definitely an error.

I've implemented the changes and added a new error that specifies the locations of 2 malformed headers like 0x00E1. Let me know if there's anything else that can be improved!

offset += readUint16()
break;
default:
if (data[offset - 3] == 0xFF &&
data[offset - 2] >= 0xC0 && data[offset - 2] <= 0xFE) {
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