Question

I've started using LibTiff.NET for writing tiff IPTC tags lately and discovered strange behavior on some files that i have here. I'm using sample code that ships with LibTiff.NET binaries, and it works fine with most of the images, but some files are having image data corruption after these lines:

class Program
{
    private const TiffTag TIFFTAG_GDAL_METADATA = (TiffTag)42112;

    private static Tiff.TiffExtendProc m_parentExtender;

    public static void TagExtender(Tiff tif)
    {
        TiffFieldInfo[] tiffFieldInfo =
        {
            new TiffFieldInfo(TIFFTAG_GDAL_METADATA, -1, -1, TiffType.ASCII,
                              FieldBit.Custom, true, false, "GDALMetadata"),
        };

        tif.MergeFieldInfo(tiffFieldInfo, tiffFieldInfo.Length);

        if (m_parentExtender != null)
            m_parentExtender(tif);
    }

    public static void Main(string[] args)
    {
        // Register the extender callback
        // It's a good idea to keep track of the previous tag extender (if any) so that we can call it
        // from our extender allowing a chain of customizations to take effect.
        m_parentExtender = Tiff.SetTagExtender(TagExtender);

        string destFile = @"d:\00000641(tiffed).tif";

        File.Copy(@"d:\00000641.tif", destFile);

        //Console.WriteLine("Hello World!");

        // TODO: Implement Functionality Here
        using (Tiff image = Tiff.Open(destFile, "a"))
    {
        // we should rewind to first directory (first image) because of append mode
        image.SetDirectory(0);

        // set the custom tag 
        string value = "<GDALMetadata>\n<Item name=\"IMG_GUID\">" + 
            "817C0168-0688-45CD-B799-CF8C4DE9AB2B</Item>\n<Item" + 
            " name=\"LAYER_TYPE\" sample=\"0\">athematic</Item>\n</GDALMetadata>";
        image.SetField(TIFFTAG_GDAL_METADATA, value);

        // rewrites directory saving new tag
        image.CheckpointDirectory();
    }

    // restore previous tag extender
    Tiff.SetTagExtender(m_parentExtender);
        Console.Write("Press any key to continue . . . ");
        Console.ReadKey(true);
    }
}

After opening i see mostly blank white image or multiple black and white lines instead of text that have been written there (i don't need to read\write tags to produce this behavior). I noticed this happens when image already has a custom tag (console window alerts about it) or one of tags have got 'bad value' (console window in this case says 'vsetfield:%pathToTiffFile%: bad value 0 for "%TagName%" tag').

Original image: http://dl.dropbox.com/u/1476402/00000641.tif

Image after LibTiff.NET: http://dl.dropbox.com/u/1476402/00000641%28tiffed%29.tif

I would be grateful for any help provided.

Was it helpful?

Solution

You probably should not use CheckpointDirectory method for files opened in append mode. Try using RewriteDirectory method instead.

It will rewrite the directory, but instead of place it at it's old location (as WriteDirectory() would) it will place them at the end of the file, correcting the pointer from the preceeding directory or file header to point to it's new location. This is particularly important in cases where the size of the directory and pointed to data has grown, so it won’t fit in the space available at the old location. Note that this will result in the loss of the previously used directory space.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top