如何使用LibTiff.Net 2.3库删除tiff标签

5

我似乎找不到使用LibTiff.Net库删除tiff标签的文档。我非常喜欢这个库,但是这个方法对我需要做的事情非常重要。曾经有一段时间,我希望可以设置一个标签并将其值设置为空,但是没有成功。

有人知道如何使用LibTiff.Net库删除tiff标签吗?

3个回答

4
请查看随LibTiff.Net一起提供的TiffCP实用程序(特别是其源代码)。
LibTiff.Net不提供删除标签的方法(LibTiff也是如此)。您需要实现TiffCP功能的一部分才能实现它。
基本上,您需要复制所有要保留的标签,并复制像素数据,而无需对其进行解码和重新编码。
还请查看将多条带状TIFF图像转换为单条带状图像示例。它展示了如何从一个图像复制标签和复制原始(未解码的)数据到另一个图像。该示例在某些情况下实际上会解码数据,因为它需要更改条带数量,但您不需要解码数据。

哇!这看起来正是我想要的。我宁愿处理公开方法,而不是挖掘私有方法。非常感谢你指出这一点,Bobrovsky!我刚刚获得了“投票”特权...猜猜我会在哪里使用我的第一个投票?;) - Arvo Bowen

1

我认为你需要将输入文件基本上复制到一个新的TIFF图像中,在此过程中过滤掉你不想要的标签。看一下tiffcp实用程序,它是常规libtiff发行版的一部分。它有点这样做,减去了过滤。

免责声明:我从未使用过LibTiff.Net,并且假设它与LibTiff非常相似。

看一下tiffcp.c

首先,它手动复制/设置一些已知的标签,例如分辨率、压缩、颜色等。然后,它复制所有可以在没有预处理的情况下复制的一组标签:

for (p = tags; p < &tags[NTAGS]; p++)
    CopyTag(p->tag, p->count, p->type);

然后它会复制实际的像素数据。据我回忆,这将删除任何未知于 tiffcp 的标签。如果您想要删除的标签在列表中,则可以通过将其从该列表中删除来轻松删除它。


“过滤”部分是关键。我不确定该如何做到这一点。查看了tiffcp.exe,它似乎只是简单地复制带有不同压缩类型的tiff文件。 - Arvo Bowen
你应该看源代码,不是exe文件?我已经在上面的答案中更新了一些细节。 - MK.
谢谢MK,我现在正在研究C#代码。找到了与你在C中发布的代码类似的区域。是时候深入挖掘了。;) - Arvo Bowen

1
注意:一开始可能看起来很长,但我想确保任何查看此内容的人都能看到我创建的所有“专有类”,以保持代码整洁。为了让答案尽可能简短并且具有信息性,我只会粘贴DeleteTiffTags方法的代码。其余的代码可以通过这里下载。
现在进入正题...我花了大约一天的时间来实现这个功能,这要归功于伟大的stackoverflow社区回答了各种问题。我在我的一个类中编写了两个小(非常详细的)方法来删除tiff标签。第一个方法用于删除给定标签列表,第二个方法用于删除单个标签,它是基于前面提到的方法工作的。在这个示例中,我添加了几行代码来支持我的自定义tiff标签...它们都将以//ADDED注释开头。
类:

公共静态类TIFFTAGS - 这个类是主要的类,只需像TIFFTAGS.DeleteTiffTags()这样调用即可。由于它是一个静态类,因此不需要创建对象来使用其方法。

私有类TIFFTAGS_PAGE - 这个类是位于TIFFTAGS类内部的私有类。它的目的是包含所有可能在tiff中的单页信息。它是私有的,仅用于内部目的。

公共类TIFFTAGS_TAG - 这是我制作的一个类,用于将标签封装成我更喜欢的形式。使用标准标签类型名称,如ASCII、SHORT、LONG和RATIONAL。

方法/函数:

TagExtender()-这个小宝石是一个回调函数,允许您实际上将自定义标签保留在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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接