Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LibTIFF: Extract all tags from a TIFF image

Tags:

c++

c

tiff

libtiff

I'm currently working on a project which requires me to split a TIFF image into a file containing all tags and a file containing all image data and to reconstruct a TIFF image from these files. The only problem is that it seems that LibTIFF provides no easy way to grab all tags from an image. I've tried using TIFFGetTagListCount and then TIFFGetField to retrieve the tag, but this only returns a small subset of the tags. I've started rolling my own version, but I just want to double check and make sure I'm not overlooking something as this seems like a pretty obvious feature that should be included in the library.

like image 997
Eric Scrivner Avatar asked Dec 15 '09 02:12

Eric Scrivner


1 Answers

Here is the closes you can get for scanning all tags:

 #include "LibTIFF/tif_dir.h"
 ...

 TIFFDirectory *td = &tif->tif_dir;

 for (int fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) {
    const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi];

    // test if tag value is set
    // (lifted directly form LibTiff _TIFFWriteDirectory)

        if( fip->field_bit == FIELD_CUSTOM ) {
            int ci, is_set = FALSE;

            for( ci = 0; ci < td->td_customValueCount; ci++ )
                is_set |= (td->td_customValues[ci].info == fip);

            if( !is_set )
                continue;
        } else if(!TIFFFieldSet(tif, fip->field_bit))
            continue;

        // else: process the fip->field_tag


    }

Note that you will have to take into account that some tags will appear twice (LONG and SHORT version), but only one of these will have value. The correct type to use can be looked up in the included header (the TIFFDirectory struct).

There also other catches on how to read the tags, but this at least will make you loop over all of them (the standard ones). See tif_dirinfo.c for pointers if you get stuck.

like image 125
user362515 Avatar answered Sep 18 '22 01:09

user362515