Merge pull request #4647 from radarhere/blocklist

Allow libtiff to write COLORMAP tag
This commit is contained in:
Hugo van Kemenade 2020-06-25 23:04:29 +03:00 committed by GitHub
commit 7dd8837967
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 4 deletions

View File

@ -207,6 +207,7 @@ class TestFileLibTiff(LibTiffTestCase):
del core_items[tag]
except KeyError:
pass
del core_items[320] # colormap is special, tested below
# Type codes:
# 2: "ascii",
@ -491,6 +492,18 @@ class TestFileLibTiff(LibTiffTestCase):
with Image.open(out) as im2:
assert_image_equal(im, im2)
def test_palette_save(self, tmp_path):
im = hopper("P")
out = str(tmp_path / "temp.tif")
TiffImagePlugin.WRITE_LIBTIFF = True
im.save(out)
TiffImagePlugin.WRITE_LIBTIFF = False
with Image.open(out) as reloaded:
# colormap/palette tag
assert len(reloaded.tag_v2[320]) == 768
def xtest_bw_compression_w_rgb(self, tmp_path):
""" This test passes, but when running all tests causes a failure due
to output on stderr from the error thrown by libtiff. We need to

View File

@ -1530,7 +1530,6 @@ def _save(im, fp, filename):
# BITSPERSAMPLE, etc), passing arrays with a different length will result in
# segfaults. Block these tags until we add extra validation.
blocklist = [
COLORMAP,
REFERENCEBLACKWHITE,
SAMPLEFORMAT,
STRIPBYTECOUNTS,

View File

@ -483,7 +483,6 @@ LIBTIFF_CORE = {
65537,
}
LIBTIFF_CORE.remove(320) # Array of short, crashes
LIBTIFF_CORE.remove(301) # Array of short, crashes
LIBTIFF_CORE.remove(532) # Array of long, crashes

View File

@ -671,7 +671,7 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
// This list also exists in TiffTags.py
const int core_tags[] = {
256, 257, 258, 259, 262, 263, 266, 269, 274, 277, 278, 280, 281, 340,
341, 282, 283, 284, 286, 287, 296, 297, 321, 338, 32995, 32998, 32996,
341, 282, 283, 284, 286, 287, 296, 297, 320, 321, 338, 32995, 32998, 32996,
339, 32997, 330, 531, 530, 65537
};
@ -801,7 +801,26 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
TRACE(("Setting from Tuple: %d \n", key_int));
len = PyTuple_Size(value);
if (type == TIFF_SHORT) {
if (key_int == TIFFTAG_COLORMAP) {
int stride = 256;
if (len != 768) {
PyErr_SetString(PyExc_ValueError, "Requiring 768 items for for Colormap");
return NULL;
}
UINT16 *av;
/* malloc check ok, calloc checks for overflow */
av = calloc(len, sizeof(UINT16));
if (av) {
for (i=0;i<len;i++) {
av[i] = (UINT16)PyLong_AsLong(PyTuple_GetItem(value,i));
}
status = ImagingLibTiffSetField(&encoder->state, (ttag_t) key_int,
av,
av + stride,
av + stride * 2);
free(av);
}
} else if (type == TIFF_SHORT) {
UINT16 *av;
/* malloc check ok, calloc checks for overflow */
av = calloc(len, sizeof(UINT16));