Implement streamtype=1 option for tables-only JPEG encoding
#7491
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.
We already support
streamtype=2to skip producing JPEG tables, butstreamtype=1, which skips everything but the tables, was never implemented. Thestreamtype=1stub code dates to Git pre-history, so it's not immediately clear why. Implement the missing support.jpeg_write_tables()can't resume after a full output buffer (it fails withJERR_CANT_SUSPEND), so it might seem that Pillow needs to pre-compute the necessary buffer size. However, in the normal case of producing an interchange stream, the tables are written via the same libjpeg codepath during the firstjpeg_write_scanlines()call, and table writes aren't resumable there either. Thus, any buffer large enough for the normal case will also be large enough for a tables-only file.I've opted to implement the early exit with a
gotorather than refactoring the state machine. If thegotois not desired, it would be possible to add a dedicated cleanup state, do an early return after writing tables, and require a second call into the state machine to finish cleanup.The
streamtypeoption isn't documented and this PR doesn't change that. It does add a test though.