Merge pull request #4918 from gofr/4825-jpeg-16-bit-qt

This commit is contained in:
Hugo van Kemenade 2020-10-14 17:58:12 +03:00 committed by GitHub
commit 1cb3e2f742
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 16 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -446,6 +446,7 @@ class TestFileJpeg:
assert len(im.quantization) == n assert len(im.quantization) == n
reloaded = self.roundtrip(im, qtables="keep") reloaded = self.roundtrip(im, qtables="keep")
assert im.quantization == reloaded.quantization assert im.quantization == reloaded.quantization
assert reloaded.quantization[0].typecode == "B"
with Image.open("Tests/images/hopper.jpg") as im: with Image.open("Tests/images/hopper.jpg") as im:
qtables = im.quantization qtables = im.quantization
@ -544,6 +545,30 @@ class TestFileJpeg:
with pytest.raises(ValueError): with pytest.raises(ValueError):
self.roundtrip(im, qtables=[[1, 2, 3, 4]]) self.roundtrip(im, qtables=[[1, 2, 3, 4]])
def test_load_16bit_qtables(self):
with Image.open("Tests/images/hopper_16bit_qtables.jpg") as im:
assert len(im.quantization) == 2
assert len(im.quantization[0]) == 64
assert max(im.quantization[0]) > 255
def test_save_multiple_16bit_qtables(self):
with Image.open("Tests/images/hopper_16bit_qtables.jpg") as im:
im2 = self.roundtrip(im, qtables="keep")
assert im.quantization == im2.quantization
def test_save_single_16bit_qtable(self):
with Image.open("Tests/images/hopper_16bit_qtables.jpg") as im:
im2 = self.roundtrip(im, qtables={0: im.quantization[0]})
assert len(im2.quantization) == 1
assert im2.quantization[0] == im.quantization[0]
def test_save_low_quality_baseline_qtables(self):
with Image.open(TEST_FILE) as im:
im2 = self.roundtrip(im, quality=10)
assert len(im2.quantization) == 2
assert max(im2.quantization[0]) <= 255
assert max(im2.quantization[1]) <= 255
@pytest.mark.skipif(not djpeg_available(), reason="djpeg not available") @pytest.mark.skipif(not djpeg_available(), reason="djpeg not available")
def test_load_djpeg(self): def test_load_djpeg(self):
with Image.open(TEST_FILE) as img: with Image.open(TEST_FILE) as img:

View File

@ -36,6 +36,7 @@ import io
import os import os
import struct import struct
import subprocess import subprocess
import sys
import tempfile import tempfile
import warnings import warnings
@ -234,9 +235,8 @@ def SOF(self, marker):
def DQT(self, marker): def DQT(self, marker):
# #
# Define quantization table. Support baseline 8-bit tables # Define quantization table. Note that there might be more
# only. Note that there might be more than one table in # than one table in each marker.
# each marker.
# FIXME: The quantization tables can be used to estimate the # FIXME: The quantization tables can be used to estimate the
# compression quality. # compression quality.
@ -244,15 +244,16 @@ def DQT(self, marker):
n = i16(self.fp.read(2)) - 2 n = i16(self.fp.read(2)) - 2
s = ImageFile._safe_read(self.fp, n) s = ImageFile._safe_read(self.fp, n)
while len(s): while len(s):
if len(s) < 65:
raise SyntaxError("bad quantization table marker")
v = i8(s[0]) v = i8(s[0])
if v // 16 == 0: precision = 1 if (v // 16 == 0) else 2 # in bytes
self.quantization[v & 15] = array.array("B", s[1:65]) qt_length = 1 + precision * 64
s = s[65:] if len(s) < qt_length:
else: raise SyntaxError("bad quantization table marker")
return # FIXME: add code to read 16-bit tables! data = array.array("B" if precision == 1 else "H", s[1:qt_length])
# raise SyntaxError, "bad quantization table element size" if sys.byteorder == "little" and precision > 1:
data.byteswap() # the values are always big-endian
self.quantization[v & 15] = data
s = s[qt_length:]
# #
@ -676,7 +677,7 @@ def _save(im, fp, filename):
try: try:
if len(table) != 64: if len(table) != 64:
raise TypeError raise TypeError
table = array.array("B", table) table = array.array("H", table)
except TypeError as e: except TypeError as e:
raise ValueError("Invalid quantization table") from e raise ValueError("Invalid quantization table") from e
else: else:

View File

@ -159,22 +159,21 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
quality = context->quality; quality = context->quality;
} }
for (i = 0; i < context->qtablesLen; i++) { for (i = 0; i < context->qtablesLen; i++) {
// TODO: Should add support for none baseline
jpeg_add_quant_table(&context->cinfo, i, &context->qtables[i * DCTSIZE2], jpeg_add_quant_table(&context->cinfo, i, &context->qtables[i * DCTSIZE2],
quality, TRUE); quality, FALSE);
context->cinfo.comp_info[i].quant_tbl_no = i; context->cinfo.comp_info[i].quant_tbl_no = i;
last_q = i; last_q = i;
} }
if (context->qtablesLen == 1) { if (context->qtablesLen == 1) {
// jpeg_set_defaults created two qtables internally, but we only wanted one. // jpeg_set_defaults created two qtables internally, but we only wanted one.
jpeg_add_quant_table(&context->cinfo, 1, &context->qtables[0], jpeg_add_quant_table(&context->cinfo, 1, &context->qtables[0],
quality, TRUE); quality, FALSE);
} }
for (i = last_q; i < context->cinfo.num_components; i++) { for (i = last_q; i < context->cinfo.num_components; i++) {
context->cinfo.comp_info[i].quant_tbl_no = last_q; context->cinfo.comp_info[i].quant_tbl_no = last_q;
} }
} else if (context->quality != -1) { } else if (context->quality != -1) {
jpeg_set_quality(&context->cinfo, context->quality, 1); jpeg_set_quality(&context->cinfo, context->quality, TRUE);
} }
/* Set subsampling options */ /* Set subsampling options */