Skip to content

Commit 04399ac

Browse files
committed
Removed support for libtiff < 4
1 parent eee633c commit 04399ac

File tree

4 files changed

+46
-83
lines changed

4 files changed

+46
-83
lines changed

Tests/test_file_libtiff.py

+45-63
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,12 @@ def test_additional_metadata(
242242

243243
im.save(out, tiffinfo=new_ifd)
244244

245-
def test_custom_metadata(self, tmp_path: Path) -> None:
245+
@pytest.mark.parametrize("libtiff", (True, False))
246+
def test_custom_metadata(
247+
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path, libtiff: bool
248+
) -> None:
249+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", libtiff)
250+
246251
class Tc(NamedTuple):
247252
value: Any
248253
type: int
@@ -281,53 +286,43 @@ class Tc(NamedTuple):
281286
)
282287
}
283288

284-
libtiffs = [False]
285-
if Image.core.libtiff_support_custom_tags:
286-
libtiffs.append(True)
287-
288-
for libtiff in libtiffs:
289-
TiffImagePlugin.WRITE_LIBTIFF = libtiff
290-
291-
def check_tags(
292-
tiffinfo: TiffImagePlugin.ImageFileDirectory_v2 | dict[int, str]
293-
) -> None:
294-
im = hopper()
295-
296-
out = str(tmp_path / "temp.tif")
297-
im.save(out, tiffinfo=tiffinfo)
298-
299-
with Image.open(out) as reloaded:
300-
for tag, value in tiffinfo.items():
301-
reloaded_value = reloaded.tag_v2[tag]
302-
if (
303-
isinstance(reloaded_value, TiffImagePlugin.IFDRational)
304-
and libtiff
305-
):
306-
# libtiff does not support real RATIONALS
307-
assert (
308-
round(abs(float(reloaded_value) - float(value)), 7) == 0
309-
)
310-
continue
311-
312-
assert reloaded_value == value
313-
314-
# Test with types
315-
ifd = TiffImagePlugin.ImageFileDirectory_v2()
316-
for tag, tagdata in custom.items():
317-
ifd[tag] = tagdata.value
318-
ifd.tagtype[tag] = tagdata.type
319-
check_tags(ifd)
320-
321-
# Test without types. This only works for some types, int for example are
322-
# always encoded as LONG and not SIGNED_LONG.
323-
check_tags(
324-
{
325-
tag: tagdata.value
326-
for tag, tagdata in custom.items()
327-
if tagdata.supported_by_default
328-
}
329-
)
330-
TiffImagePlugin.WRITE_LIBTIFF = False
289+
def check_tags(
290+
tiffinfo: TiffImagePlugin.ImageFileDirectory_v2 | dict[int, str]
291+
) -> None:
292+
im = hopper()
293+
294+
out = str(tmp_path / "temp.tif")
295+
im.save(out, tiffinfo=tiffinfo)
296+
297+
with Image.open(out) as reloaded:
298+
for tag, value in tiffinfo.items():
299+
reloaded_value = reloaded.tag_v2[tag]
300+
if (
301+
isinstance(reloaded_value, TiffImagePlugin.IFDRational)
302+
and libtiff
303+
):
304+
# libtiff does not support real RATIONALS
305+
assert round(abs(float(reloaded_value) - float(value)), 7) == 0
306+
continue
307+
308+
assert reloaded_value == value
309+
310+
# Test with types
311+
ifd = TiffImagePlugin.ImageFileDirectory_v2()
312+
for tag, tagdata in custom.items():
313+
ifd[tag] = tagdata.value
314+
ifd.tagtype[tag] = tagdata.type
315+
check_tags(ifd)
316+
317+
# Test without types. This only works for some types, int for example are
318+
# always encoded as LONG and not SIGNED_LONG.
319+
check_tags(
320+
{
321+
tag: tagdata.value
322+
for tag, tagdata in custom.items()
323+
if tagdata.supported_by_default
324+
}
325+
)
331326

332327
def test_osubfiletype(self, tmp_path: Path) -> None:
333328
outfile = str(tmp_path / "temp.tif")
@@ -682,8 +677,7 @@ def test_exif_ifd(self, tmp_path: Path) -> None:
682677
im.save(outfile)
683678

684679
with Image.open(outfile) as reloaded:
685-
if Image.core.libtiff_support_custom_tags:
686-
assert reloaded.tag_v2[34665] == 125456
680+
assert reloaded.tag_v2[34665] == 125456
687681

688682
def test_crashing_metadata(
689683
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
@@ -735,19 +729,7 @@ def test_read_icc(self, monkeypatch: pytest.MonkeyPatch) -> None:
735729
assert icc_libtiff is not None
736730
assert icc == icc_libtiff
737731

738-
@pytest.mark.parametrize(
739-
"libtiff",
740-
(
741-
pytest.param(
742-
True,
743-
marks=pytest.mark.skipif(
744-
not Image.core.libtiff_support_custom_tags,
745-
reason="Custom tags not supported by older libtiff",
746-
),
747-
),
748-
False,
749-
),
750-
)
732+
@pytest.mark.parametrize("libtiff", (True, False))
751733
def test_write_icc(
752734
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path, libtiff: bool
753735
) -> None:

src/PIL/TiffImagePlugin.py

-3
Original file line numberDiff line numberDiff line change
@@ -1823,9 +1823,6 @@ def _save(im, fp, filename):
18231823
# Custom items are supported for int, float, unicode, string and byte
18241824
# values. Other types and tuples require a tagtype.
18251825
if tag not in TiffTags.LIBTIFF_CORE:
1826-
if not getattr(Image.core, "libtiff_support_custom_tags", False):
1827-
continue
1828-
18291826
if tag in ifd.tagtype:
18301827
types[tag] = ifd.tagtype[tag]
18311828
elif not (isinstance(value, (int, float, str, bytes))):

src/_imaging.c

-10
Original file line numberDiff line numberDiff line change
@@ -4388,16 +4388,6 @@ setup_module(PyObject *m) {
43884388
PyObject *v = PyUnicode_FromString(ImagingTiffVersion());
43894389
PyDict_SetItemString(d, "libtiff_version", v ? v : Py_None);
43904390
Py_XDECREF(v);
4391-
4392-
// Test for libtiff 4.0 or later, excluding libtiff 3.9.6 and 3.9.7
4393-
PyObject *support_custom_tags;
4394-
#if TIFFLIB_VERSION >= 20111221 && TIFFLIB_VERSION != 20120218 && \
4395-
TIFFLIB_VERSION != 20120922
4396-
support_custom_tags = Py_True;
4397-
#else
4398-
support_custom_tags = Py_False;
4399-
#endif
4400-
PyDict_SetItemString(d, "libtiff_support_custom_tags", support_custom_tags);
44014391
}
44024392
#endif
44034393

src/libImaging/TiffDecode.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ ImagingLibTiffMergeFieldInfo(
824824
// Refer to libtiff docs (http://www.simplesystems.org/libtiff/addingtags.html)
825825
TIFFSTATE *clientstate = (TIFFSTATE *)state->context;
826826
uint32_t n;
827-
int status = 0;
827+
int status;
828828

829829
// custom fields added with ImagingLibTiffMergeFieldInfo are only used for
830830
// decoding, ignore readcount;
@@ -846,13 +846,7 @@ ImagingLibTiffMergeFieldInfo(
846846

847847
n = sizeof(info) / sizeof(info[0]);
848848

849-
// Test for libtiff 4.0 or later, excluding libtiff 3.9.6 and 3.9.7
850-
#if TIFFLIB_VERSION >= 20111221 && TIFFLIB_VERSION != 20120218 && \
851-
TIFFLIB_VERSION != 20120922
852849
status = TIFFMergeFieldInfo(clientstate->tiff, info, n);
853-
#else
854-
TIFFMergeFieldInfo(clientstate->tiff, info, n);
855-
#endif
856850
return status;
857851
}
858852

0 commit comments

Comments
 (0)