Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Tests/test_file_libtiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def test_additional_metadata(self, tmp_path):
del core_items[tag]
except KeyError:
pass
del core_items[320] # colormap is special, tested below

# Type codes:
# 2: "ascii",
Expand Down Expand Up @@ -479,6 +480,18 @@ def test_cmyk_save(self, tmp_path):
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
Expand Down
1 change: 0 additions & 1 deletion src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,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,
Expand Down
1 change: 0 additions & 1 deletion src/PIL/TiffTags.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,6 @@ def _populate():
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

Expand Down
23 changes: 21 additions & 2 deletions src/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -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));
Expand Down