Repository navigation
Do not store malformed PhotometricInterpretation tag data - #10274
saintparish4 wants to merge 2 commits into
Conversation
| and count != 1 | ||
| and self.group is None | ||
| ): | ||
| # Ignore a malformed PhotometricInterpretation tag, like libtiff |
There was a problem hiding this comment.
like libtiff
I'm intrigued. Could you expand on how you know this, link to source code, or something?
There was a problem hiding this comment.
It's from libtiff's directory reading. PhotometricInterpretation is defined as a single SHORT (tif_dirinfo.c#L104) and has no special case in TIFFReadDirectory, so it's read by TIFFFetchNormalTag(tif, dp, TRUE) (tif_dirread.c#L4896-4897). That calls TIFFReadDirEntryShort, which returns TIFFReadDirEntryErrCount if the count isn't 1 (#L485-489), so TIFFSetField is never called (#L6517-6529). Because recover is set, it only warns, Incorrect count for "PhotometricInterpretation"; tag ignored (#L4063-4067).
Looking into this also answered my question about duplicates. libtiff marks every later duplicate of a tag as ignored before reading any values (#L4389-4408), so a malformed PhotometricInterpretation followed by a valid one ends up with neither. I've pushed a commit so that Pillow does the same, with a test.
This only covers PhotometricInterpretation, though. When Pillow skips the first copy of another tag (unsupported type, empty data, etc.), a later duplicate is still used, unlike libtiff. I'm happy to make that general, here or in a follow-up, if you'd prefer.
Regression from #10210
#10210 started ignoring a PhotometricInterpretation tag with more than one value, by not setting it in
_setitem(). However, the raw data is still kept in_tagdata, so the tag is reported byinand when iterating, butKeyError: 262is raised when it is read.This means that an image with such a tag can be opened and loaded, but reading its EXIF data or saving it as a TIFF fails.
Changes proposed in this pull request:
If a malformed PhotometricInterpretation tag is followed by a valid duplicate, the valid one is now used, whereas after #10210 neither was. Let me know if you would prefer to keep ignoring both.