我似乎找不到任何关于如何使用LibTiff.Net库删除tiff标记的文档。我喜欢这个库,但这一方法对我需要做的事情很重要。有一次,我希望我可以只设置一个标签,并将它的值设置为零。我曾希望这会奏效,但这是一个负面的结果。
有人知道如何使用LibTiff.Net库删除tiff标签吗?
发布于 2012-02-26 05:10:00
注意:首先,这可能看起来像是一个很大的答案,但我想确保看这篇文章的人都是我为了保持干净而创建的所有“专有类”。为了尽可能保持答案的排序和提供信息,我只粘贴DeleteTiffTags方法的代码。其余的代码可以通过here下载。
现在让我们来看一些好东西。我最终花了大约一天的时间来实现这一点,多亏了伟大的stackoverflow社区回答了各种问题,这才有可能实现。我在我的一个类中编写了两个小的(非常详细的)方法来删除tiff标记。第一个是删除给定标签的列表,第二个是删除单个标签,这与前面提到的方法不同。同样在这个例子中,我添加了几行代码来支持我的自定义tiff标记……它们的前面都会有//添加的注释。
类:
公共静态类TIFFTAGS -这个类是主类,可以简单地通过执行类似TIFFTAGS.DeleteTiffTags()的操作来调用;因为它是一个静态类,所以不需要创建它的对象来使用它的方法。
私有类TIFFTAGS -这个类是驻留在TIFFTAGS_PAGE类中的私有类。它的目的是包含所有可能在tiff中的页面的所有单页信息。它是私有的,仅用于内部目的。
公共类TIFFTAGS_TAG -这是我创建的一个类,用我喜欢的方式包装标签。使用标准的标记类型名称,例如ASCII、SHORT、LONG和RATIONAL。
方法/函数:
TagExtender() -这个小gem是一个回调函数,允许您实际将自定义标记保留在tiff中。如果没有它,当您删除任何标签时,您所有的自定义标签都将消失(即使您只删除了一个标签)。
DeleteTiffTags() -这是删除标记列表的主要方法。只需传入一个ushort标签号列表,所有这些都将被删除。请记住,不使用TagExtender函数会导致您的自定义标记失效!
DeleteTiffTag() -这只是用来删除单个tiff标记。它调用DeleteTiffTags()来处理繁琐的工作。
public static bool DeleteTiffTags(string sFileName, List<ushort> ushortTagNumbers)
{
//Deletes a list of tiff tag from the given image
//Returns true if successful or false if error occured
//Define variables
List<TIFFTAGS_PAGE> ttPage = new List<TIFFTAGS_PAGE>();
//Check for empty list
if (ushortTagNumbers.Count == 0) return false;
try
{
//ADDED
m_lTagsToWrite = new List<TIFFTAGS_TAG>();
m_lTagsToWrite.Add(new TIFFTAGS_TAG("", 38001, Convert.ToString("")));
m_lTagsToWrite.Add(new TIFFTAGS_TAG("", 38002, Convert.ToString("")));
m_parentExtender = Tiff.SetTagExtender(TagExtender);
//Open the file for reading
using (Tiff input = Tiff.Open(sFileName, "r"))
{
if (input == null) return false;
//Get page count
int numberOfDirectories = input.NumberOfDirectories();
//Go through all the pages
for (short i = 0; i < numberOfDirectories; ++i)
{
//Set the page
input.SetDirectory(i);
//Create a new tags dictionary to store all my tags
Dictionary<ushort, FieldValue[]> dTags = new Dictionary<ushort, FieldValue[]>();
//Get all the tags for the page
for (ushort t = ushort.MinValue; t < ushort.MaxValue; ++t)
{
TiffTag tag = (TiffTag)t;
FieldValue[] tagValue = input.GetField(tag);
if (tagValue != null)
{
dTags.Add(t, tagValue);
}
}
//Check if the page is encoded
bool encoded = false;
FieldValue[] compressionTagValue = input.GetField(TiffTag.COMPRESSION);
if (compressionTagValue != null)
encoded = (compressionTagValue[0].ToInt() != (int)Compression.NONE);
//Create a new byte array to store all my image data
int numberOfStrips = input.NumberOfStrips();
byte[] byteImageData = new byte[numberOfStrips * input.StripSize()];
int offset = 0;
//Get all the image data for the page
for (int n = 0; n < numberOfStrips; ++n)
{
int bytesRead;
if (encoded)
bytesRead = input.ReadEncodedStrip(n, byteImageData, offset, byteImageData.Length - offset);
else
bytesRead = input.ReadRawStrip(n, byteImageData, offset, byteImageData.Length - offset);
//Add to the offset keeping up with where we are
offset += bytesRead;
}
//Save all the tags, image data, and height, etc for the page
TIFFTAGS_PAGE tiffPage = new TIFFTAGS_PAGE();
tiffPage.Height = input.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
tiffPage.Tags = dTags;
tiffPage.PageData = byteImageData;
tiffPage.Encoded = encoded;
tiffPage.StripSize = input.StripSize();
tiffPage.StripOffset = input.GetField(TiffTag.STRIPOFFSETS)[0].ToIntArray()[0];
ttPage.Add(tiffPage);
}
}
//Open the file for writing
using (Tiff output = Tiff.Open(sFileName + "-new.tif", "w"))
{
if (output == null) return false;
//Go through all the pages
for (short i = 0; i < ttPage.Count(); ++i)
{
//Write all the tags for the page
foreach (KeyValuePair<ushort, FieldValue[]> tagValue in ttPage[i].Tags)
{
//Write all the tags except the one's needing to be deleted
if (!ushortTagNumbers.Contains(tagValue.Key))
{
TiffTag tag = (TiffTag)tagValue.Key;
output.GetTagMethods().SetField(output, tag, tagValue.Value);
}
}
//Set the height for the page
output.SetField(TiffTag.ROWSPERSTRIP, ttPage[i].Height);
//Set the offset for the page
output.SetField(TiffTag.STRIPOFFSETS, ttPage[i].StripOffset);
//Save page data along with tags
output.CheckpointDirectory();
//Write each strip one at a time using the same orginal strip size
int numberOfStrips = ttPage[i].PageData.Length / ttPage[i].StripSize;
int offset = 0;
for (int n = 0; n < numberOfStrips; ++n)
{
//Write all the image data (strips) for the page
if (ttPage[i].Encoded)
//output.WriteEncodedStrip(n, byteStrip, offset, byteStrip.Length - offset);
output.WriteEncodedStrip(0, ttPage[i].PageData, offset, ttPage[i].StripSize - offset);
else
output.WriteRawStrip(n, ttPage[i].PageData, offset, ttPage[i].StripSize - offset);
//Add to the offset keeping up with where we are
offset += ttPage[i].StripOffset;
}
//Save the image page
output.WriteDirectory();
}
}
//ADDED
Tiff.SetTagExtender(m_parentExtender);
}
catch
{
//ADDED
Tiff.SetTagExtender(m_parentExtender);
//Error occured
return false;
}
//Return success
return true;
}发布于 2012-02-25 02:02:23
请看一下LibTiff.Net附带的TiffCP实用程序(特别是它的源代码)。
LibTiff.Net不提供删除标记的方法(LibTiff也是如此)。为了实现这一点,您需要实现部分TiffCP功能。
基本上,您需要复制您希望保留的所有标签,并复制像素数据,而无需对其进行解码和重新编码。
也请看一下Convert a multi-strip TIFF image to a single-strip one示例。它展示了如何从一个图像复制标签和复制原始(未解码的数据)到另一个图像。在某些情况下,示例实际上会对数据进行解码,因为它需要更改条带的数量,但您不需要解码数据。
发布于 2012-02-24 10:18:02
我认为基本上必须将输入文件复制到新的TIFF图像中,在此过程中过滤掉您不想要的标签。看看tiffcp实用程序,它是常规libtiff发行版的一部分。它在某种程度上做到了这一点,减去过滤。
免责声明:我从未使用过LibTiff.Net,我假设它与LibTiff非常相似。
查看tiffcp.c
首先,它手动复制/设置一些已知的标签,如分辨率、压缩、颜色等。然后,它复制所有可以在没有预处理的情况下复制的标签集:
for (p = tags; p < &tags[NTAGS]; p++)
CopyTag(p->tag, p->count, p->type);然后它复制实际的像素数据。根据我的记忆,这将删除任何tiffcp不知道的标记。如果您要删除的标记在列表中,则可以通过从该列表中删除它来轻松删除它。
https://stackoverflow.com/questions/9424436
复制相似问题