我最近开始使用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%:‘%TagName%’的坏值0‘)时,就会发生这种情况。
原图:http://dl.dropbox.com/u/1476402/00000641.tif
LibTiff.NET之后的图片:http://dl.dropbox.com/u/1476402/00000641%28tiffed%29.tif
如果能提供任何帮助,我将不胜感激。
发布于 2012-03-23 02:04:04
对于以追加模式打开的文件,您可能不应使用CheckpointDirectory方法。请尝试使用RewriteDirectory方法。
它会重写目录,但不是像WriteDirectory()那样把它放在原来的位置(就像WriteDirectory()那样),而是把它们放在文件的末尾,纠正前一个目录或文件头中的指针指向它的新位置。当目录和指向数据的大小增长了时,这一点尤其重要,因此它不能放在旧位置的可用空间中。请注意,这将导致丢失以前使用的目录空间。
https://stackoverflow.com/questions/9789246
复制相似问题