Repository navigation
fix: decode a copy of JPEG data - #3603
Open
iliev-blnk wants to merge 1 commit into
Open
iliev-blnk wants to merge 1 commit into
iliev-blnk wants to merge 1 commit into
Conversation
jay-peg reverses the tag bytes of little-endian EXIF data in place, so after @react-pdf/image read the orientation, pdfkit could no longer find the orientation tag and drew rotated photos unrotated into a box sized for the rotated image. Fixes diegomura#3567
|
@iliev-blnk is attempting to deploy a commit to the diegomura Team on Vercel. A member of the Team first needs to authorize it. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3567. Credit to @fuunnx for tracking down the root cause.
jay-peg's EXIF parser reads each tag ID through
buffer.slice(), which returns a view for a NodeBuffer, and then calls.reverse()on it for little-endian (II) data. So everydecode()swaps the tag bytes in the image data in place:12 01(orientation) becomes01 12.@react-pdf/imagedecodes during resolution and then passes the same buffer to pdfkit. pdfkit'sparseExifOrientationcan't find tag0x0112anymore and falls back to 1. Layout has already swapped width and height for the rotated image, so the photo is drawn unrotated and stretched.The fix decodes a copy in
JPEG's constructor.Repro
A 40脳20 JPEG with little-endian EXIF orientation 6, rendered 4 times in one process, logging
image.orientationfromPDFDocument.prototype.openImage:1,1,1,1(pdfkit reads6from the untouched file)6,6,6,6With pdfkit 0.20.1 this hits every render, not only the later ones as in the issue. The issue was on the older
@react-pdf/pdfkit. pdfkit 0.20.1's orientation parser only reads the buffer, so theIMAGE_CACHEclone proposed in the issue isn't needed anymore, and this PR doesn't copy cached buffers on every render.Tests
packages/image/tests/jpeg.test.tsuses the fixture above. It checks that the dimensions are swapped, that the image data is unchanged after parsing, and that a second parse of the same data still reads the orientation. Without the fix, the last two tests fail. All image tests pass (55).jay-peg could also be fixed at the source by reversing a copy of the tag bytes (
Uint8Array.prototype.slice.call(tagAddress).reverse()). I'm happy to open that PR as well.