我最近开始使用 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
我将不胜感激提供的任何帮助。