如何读取自定义TIFF标记(不带TIFFFieldInfo)

How to read custom TIFF tags (w/o TIFFFieldInfo)

本文关键字:标记 不带 TIFFFieldInfo TIFF 自定义 读取 何读取      更新时间:2023-10-16

我正在尝试读取tiff文件中的自定义标记。

关于这个主题的指令很少,但AFAIK他们使用的是一个名为TIFFieldInfo的接口(结构)。我已经阅读了文档,TIFFFieldInfo再次出现。我可以同意,但他们(图书馆)说,接口已经过时了。你能给我推荐一些合理的替代方案吗?还是我看错了头文件?

终于找到了解决方案。手册(TIFFGetField(3tiff))说明了我们所需要的一切。请参阅AUTOREGISTED TAGS会话。下面是复制粘贴的。

AUTOREGISTED TAGS如果你在上表中找不到标签意味着这是一个不受支持的标记,并且不是直接的由libtiff(3TIFF)库支持。你仍然可以阅读如果您知道该标记的数据类型,则它是有价值的。例如,如果想要从标记33424中读取LONG值,并从中读取ASCII字符串标签36867您可以使用以下代码:

uint32  count;
void    *data;
TIFFGetField(tiff, 33424, &count, &data);
printf("Tag %d: %d, count %d0", 33424, *(uint32 *)data, count);
TIFFGetField(tiff, 36867, &count, &data);
printf("Tag %d: %s, count %d0", 36867, (char *)data, count);

例如,我需要读取一个双标签,所以我使用了以下代码(但我没有检查IT):

tiff *tif = TIFFOpen("ex_file.tif", "rc");   // read tif
static ttag_t const TIFFTAG_SOMETAG = 34362; // some custom tag
if(tif != nullptr) // if the file is open
{
    uint count; // get count
    double *data; // get data
    if(TIFFGetField(tif, TIFFTAG_SOMETAG, &count, &data) == 1) // read tag
        throw std::logic_error("the tag does not exist.");
    // print the values (caution: count is in bytes)
    for(int index = 0; index < count / sizeof(double); ++index)
        std::cout << data[index];
    TIFFClose(tif); // close the file
}
else
    throw std::runtime_error("cannot open the file");