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

Implement streamtype=1 option for tables-only JPEG encoding #7491

Merged
merged 1 commit into from
Nov 11, 2023

Conversation

bgilbert
Copy link
Contributor

We already support streamtype=2 to skip producing JPEG tables, but streamtype=1, which skips everything but the tables, was never implemented. The streamtype=1 stub 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 with JERR_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 first jpeg_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 goto rather than refactoring the state machine. If the goto is 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 streamtype option isn't documented and this PR doesn't change that. It does add a test though.

We already support streamtype=2 to skip producing JPEG tables, but
streamtype=1, which skips everything but the tables, was never implemented.
The streamtype=1 stub 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 with
JERR_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 first jpeg_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.

The streamtype option isn't documented and this commit doesn't change that.
It does add a test though.

Co-authored-by: Andrew Murray <[email protected]>
@bgilbert
Copy link
Contributor Author

Updated based on suggestions in bgilbert#1.

@radarhere radarhere merged commit bf76320 into python-pillow:main Nov 11, 2023
55 checks passed
@bgilbert bgilbert deleted the jpeg-tables-only branch November 11, 2023 05:19
radarhere added a commit to radarhere/Pillow that referenced this pull request Dec 8, 2023
radarhere added a commit to radarhere/Pillow that referenced this pull request Dec 8, 2023
@radarhere radarhere mentioned this pull request Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants