I'm trying to change the values of a few custom ASCII tags in an existing TIFF image, as shown in the sample at http://bitmiracle.com/libtiff/help/add-custom-tiff-tags-to-an-existing-tiff-image.aspx.
The changes essentially involve adding to the strings of existing tags, or adding custom tags that didn't exist.
However, when adding new tags, LibTiff seems to add a completely new directory at the end of the image, instead of changing the existing one, which is left in tact. I've verified this with WinHex and also tried a few different approaches such as WriteDirectory, CheckpointDirectory. The file is being opened for appending.
The duplicate directory causes a problem with another program out of our control which has to process the images.
Is there a way to just update the existing directory? Or, if it is necessary to rewrite the whole file, could I get some pointers please on how to do this? If the file is rewritten, there are other custom tags which will need to be preserved.
If you add new tags to existing directory or changing some existing tags then you better replace existing directory with a new one. Otherwise you might get your tags overwrite raster data (because the updated directory needs more space than the older one).
So, you are right assuming that you need to rewrite the whole file if a program can't properly process files with updated directory chain.
You might want to look at source code of the TiffCP utility that comes with the library. The source code should give you enough clues for a start. The utility recompresses data in a file but you probably not required to do that. You could just copy compressed data with ReadRawStrip and WriteRawStrip methods. The utility also copies all the tags. This is what you definitely need to do in your code (changing/adding tags at the same time).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With