[gdal-dev] Fwd: Re: [Tiff] Libtiff will be released soon

Even Rouault even.rouault at spatialys.com
Sat Nov 10 07:57:56 PST 2018


On samedi 10 novembre 2018 16:21:45 CET Tobias Wendorff wrote:
> Am Sa, 10.11.2018, 16:13 schrieb Even Rouault:
> > I imagine we could come with a hack to still recognize the old values,
> > but I don't plan to go to that complication unless really needed.
> 
> Not knowing the full topic: can't users with a problem just patch the
> magic byte in the header to port them to the new values?

Sure. That's just what I've done with the few golden files in GDAL autotest.

You can use the following Python script to do the patching.

{{{
#!/usr/bin/env python
import os
import struct
import sys

def patch(filename):
    with open(filename, "rb+") as f:
        magic = f.read(4)
        if magic == b'\x49\x49\x2A\x00':
            # Classic TIFF little endian
            while True:
                dir_offset = struct.unpack('<I', f.read(4))[0]
                if dir_offset == 0:
                    break
                f.seek(dir_offset, os.SEEK_SET)
                num_tags = struct.unpack('<H', f.read(2))[0]
                for i in range(num_tags):
                    tag_name = struct.unpack('<H', f.read(2))[0]
                    f.read(2 + 4) # skip type and count
                    tag_value = struct.unpack('<I', f.read(4))[0]
                    if tag_name == 259: # TIFFTAG_COMPRESSION
                        if tag_value == 34926:
                            print('Patching COMPRESSION_ZSTD to new value')
                            f.seek(-4, os.SEEK_CUR)
                            f.write(struct.pack('<I', (50000)))
                            f.seek(0, os.SEEK_CUR)
                        elif tag_value == 34927:
                            print('Patching COMPRESSION_WEBP to new value')
                            f.seek(-4, os.SEEK_CUR)
                            f.write(struct.pack('<I', (50001)))
                            f.seek(0, os.SEEK_CUR)

        elif magic == b'\x49\x49\x2B\x00':
            # BigTIFF little endian
            f.read(4) # skip
            while True:
                dir_offset = struct.unpack('<Q', f.read(8))[0]
                if dir_offset == 0:
                    break
                f.seek(dir_offset, os.SEEK_SET)
                num_tags = struct.unpack('<Q', f.read(8))[0]
                for i in range(num_tags):
                    tag_name = struct.unpack('<H', f.read(2))[0]
                    f.read(2 + 8) # skip type and count
                    tag_value = struct.unpack('<Q', f.read(8))[0]
                    if tag_name == 259: # TIFFTAG_COMPRESSION
                        if tag_value == 34926:
                            print('Patching COMPRESSION_ZSTD to new value')
                            f.seek(-8, os.SEEK_CUR)
                            f.write(struct.pack('<Q', (50000)))
                            f.seek(0, os.SEEK_CUR)
                        elif tag_value == 34927:
                            print('Patching COMPRESSION_WEBP to new value')
                            f.seek(-8, os.SEEK_CUR)
                            f.write(struct.pack('<Q', (50001)))
                            f.seek(0, os.SEEK_CUR)

        else:
            print('Unrecognized file type')

if __name__ == '__main__':
    patch(sys.argv[1])

}}}



-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list