2

我最近开始使用 LibTiff.NET 来编写 tiff IPTC 标签,并在我这里的一些文件上发现了奇怪的行为。我正在使用 LibTiff.NET 二进制文件附带的示例代码,它适用于大多数图像,但在这些行之后,某些文件的图像数据损坏:

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);
    }
}

打开后,我看到的大多是空白的白色图像或多条黑白线,而不是已经写在那里的文本(我不需要读\写标签来产生这种行为)。我注意到当图像已经有一个自定义标签(控制台窗口警告它)或标签之一有“坏值”时会发生这种情况(在这种情况下,控制台窗口显示“vsetfield:%pathToTiffFile%: bad value 0 for "%TagName% “ 标签')。

原图:http ://dl.dropbox.com/u/1476402/00000641.tif

LibTiff.NET 之后的图像:http: //dl.dropbox.com/u/1476402/00000641%28tiffed%29.tif

我将不胜感激提供的任何帮助。

4

1 回答 1

4

您可能不应该CheckpointDirectory对以附加模式打开的文件使用方法。尝试改用RewriteDirectory方法。

它将重写目录,但不是将其放置在旧位置(如 WriteDirectory() 那样),而是将它们放置在文件的末尾,更正来自前一个目录或文件头的指针以指向它的新位置。这在目录和指向数据的大小已经增长的情况下尤其重要,因此它不适合旧位置的可用空间。请注意,这将导致以前使用的目录空间丢失。

于 2012-03-22T18:04:03.520 回答