如何读取自定义 TIFF 标签 (w/o TIFFFieldInfo)

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

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

关于这个主题的说明很少,但 AFAIK 他们正在使用一个名为 TIFFFieldInfo 的接口(结构)。我已经阅读了 documentation,TIFFFieldInfo 再次出现。 我可以接受它,但他们 (the library) 说,该接口已过时。你能建议我合理的选择吗? 或者我只是误读了头文件?

我终于找到了解决办法。 手册 (TIFFGetField(3tiff)) 说明了我们所需要的一切。请参阅自动注册标签会话。以下为复制粘贴

AUTOREGISTERED TAGS If you can't find the tag in the table above that means this is an unsupported tag and is not directly supported by libtiff(3TIFF) library. You will still be able to read it's value if you know the data type of that tag. For example, if you want to read the LONG value from the tag 33424 and ASCII string from the tag 36867 you can use the following code:

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);

比如我需要读取一个double的标签,所以我使用了下面的代码(但我没有检查它):

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");