edwardforgacs
edwardforgacs

Reputation: 61

Updating Tags in an Existing Image with LibTiff.Net duplicates directory

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.

Upvotes: 3

Views: 1546

Answers (2)

zhaorufei
zhaorufei

Reputation: 2193

According to libbit.net documentation: Tiff.Open Method

only "r" "w" "a" mode is supported, plus some minor mode, there's no "r+" as in fopen to support both read and write to tiff.

The native libtiff library has a utility which can be used to change specified tiff tag:

tiffset -s XResolution 400 test.tif

tiffinfo test.tif can be used to verify the result.

Upvotes: 0

Bobrovsky
Bobrovsky

Reputation: 14236

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).

Upvotes: 1

Related Questions