Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support writing LONG8 offsets in AppendingTiffWriter #8417

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

radarhere
Copy link
Member

with Image.open("Tests/images/hopper_bigtiff.tif") as im:
# The data type of this file's StripOffsets tag is LONG8,
# which is not yet supported for offset data when saving multiple frames.
del im.tag_v2[273]
outfile = str(tmp_path / "temp.tif")
im.save(outfile, save_all=True, append_images=[im], tiffinfo=im.tag_v2)

if you try and run the test without removing this tag, an error is raised from AppendingTiffWriter.

    def fixOffsets(
        self, count: int, isShort: bool = False, isLong: bool = False
    ) -> None:
        if not isShort and not isLong:
            msg = "offset is neither short nor long"
>           raise RuntimeError(msg)
E           RuntimeError: offset is neither short nor long

Extending AppendingTiffWriter to handle LONG8 offsets is made complicated as it explicitly only handles shorts and longs, and has duplicated code to achieve that.

def rewriteLastShort(self, value: int) -> None:
self.f.seek(-2, os.SEEK_CUR)
bytes_written = self.f.write(struct.pack(self.shortFmt, value))
self._verify_bytes_written(bytes_written, 2)
def rewriteLastLong(self, value: int) -> None:
self.f.seek(-4, os.SEEK_CUR)
bytes_written = self.f.write(struct.pack(self.longFmt, value))
self._verify_bytes_written(bytes_written, 4)

def writeShort(self, value: int) -> None:
bytes_written = self.f.write(struct.pack(self.shortFmt, value))
self._verify_bytes_written(bytes_written, 2)
def writeLong(self, value: int) -> None:
bytes_written = self.f.write(struct.pack(self.longFmt, value))
self._verify_bytes_written(bytes_written, 4)

def fixOffsets(
self, count: int, isShort: bool = False, isLong: bool = False
) -> None:

Rather than just adding in rewriteLastLong8(), readLong8(), and adding an isLong8 parameter, I've reworked the internals of the class to pass around the field size in my first commit. That made adding support for LONG8 very straightforward in my second commit.

@radarhere radarhere added the TIFF label Sep 25, 2024
offset += self.offsetOfNewPage
if isShort and offset >= 65536:
if field_size == 2 and offset >= 65536:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should probably now also be a check for field_size == 4 and offset >= 2**32.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is conceivable that some users might not wish for us to silently upgrade tags to the LONG8 type, since it would be changing the saved image to rely on the BigTIFF specification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants