When both a custom quantization table and a quality value are provided,
the quantization table should be scaled using the JPEG quality scaling factor.
If quality is not explicitly set, the default base quality of 50 is used to preserve the original table.
This ensures consistent behavior when saving JPEGs with custom qtables.
Fixes part of the issue with applying 'quality' to 'qtables'.
When both `qtables` and `quality` are provided, Pillow previously passed
the raw `quality` value directly to `jpeg_add_quant_table()` as the scale
factor, which caused incorrect quantization.
This commit corrects the behavior by using `jpeg_quality_scaling(quality)`
to compute the proper JPEG quantization scaling factor, consistent with
`jpeg_set_quality()`.
This ensures expected compression behavior when custom qtables are used
alongside a specified quality level.
libjpeg allows specifying the marker interval either in MCU blocks or in
MCU rows. Support both, via separate parameters, rather than requiring
callers to do the math.
Co-authored-by: Andrew Murray <radarhere@users.noreply.github.com>
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 <radarhere@users.noreply.github.com>
16-bit quantization tables can appear at quality values below
about 25. These may cause compatibility problems. Maintain
baseline compatibility and avoid confusing users with warning
messages.
Don't force JPEG quantization to be baseline-compatible
Quantization values will not be limited to values 1..255 and may
be 16 bits if needed. This may cause compatibility issues.