2003.11.17 00:45 "[Tiff] TIFF, v3.6.0. Fetching "custom" tags", by Chris Losinger

2003.11.17 15:16 "[Tiff] TIFF, v3.6.0. Fetching "custom" tags", by Andrey Kiselev

"Custom tags can be set and fetched normally using TIFFSetField() and TIFFGetField(), and appear pretty much like normal tags to application code. "

I assume I can get rid of the tags/xtiff stuff and LibTiff will happily read and store any tags that it finds, even non-core tags, like 34665.

But, it doesn't seem to be working. When reading the TIFF dir, i get warnings about "unknown field with tag 34665 (0x8769) encountered", and when i call TIFFGetField, nothing is returned. It looks like v3.6.0. is behaving pretty much exactly like 3.5.7: complain about strange tags, then ignore them. But that's not what the documentation says is supposed to happen.

You should register all extended tags before you can work with them. See a sample code attached.

Andrey

Andrey V. Kiselev
Home phone: +7 812 5274898 ICQ# 26871517

#include <tiffio.h>
#include <stdio.h>

#define TIFFTAG_ASCIITAG 666
#define TIFFTAG_LONGTAG 667
#define TIFFTAG_SHORTTAG 668
#define TIFFTAG_RATIONALTAG 669
#define TIFFTAG_FLOATTAG 670
#define TIFFTAG_DOUBLETAG 671
#define TIFFTAG_BYTE 672

static void TagExtender(TIFF *tiff)
{
    static const TIFFFieldInfo xtiffFieldInfo[] = {
        { TIFFTAG_ASCIITAG,    -1,-1, TIFF_ASCII,        FIELD_CUSTOM,
          1,       0,      "MyTag" },
        { TIFFTAG_SHORTTAG, 2, 2, TIFF_SHORT,  FIELD_CUSTOM, 0, 1,
            "ShortTag" },
        { TIFFTAG_LONGTAG, 2, 2, TIFF_LONG,  FIELD_CUSTOM, 0, 1,
            "LongTag" },
        { TIFFTAG_RATIONALTAG, 2, 2, TIFF_RATIONAL,  FIELD_CUSTOM, 0, 1,
            "RationalTag" },
        { TIFFTAG_FLOATTAG, 2, 2, TIFF_FLOAT,  FIELD_CUSTOM, 0, 1,
            "FloatTag" },
        { TIFFTAG_DOUBLETAG, 2, 2, TIFF_DOUBLE,  FIELD_CUSTOM, 0, 1,
            "DoubleTag" },
        { TIFFTAG_BYTE, 2, 2, TIFF_BYTE,  FIELD_CUSTOM, 0, 1,
            "DoubleTag" },
    };

    TIFFMergeFieldInfo( tiff, xtiffFieldInfo,
                    sizeof(xtiffFieldInfo) / sizeof(xtiffFieldInfo[0]) );
}

int main()
{
    TIFF    *tiff;
    char    *pszString;
    long    aiLong[2], *paiLong;
    short   aiShort[2], *paiShort;
    float   afFloat[2], *pafFloat;
    double  adfDouble[2], *padfDouble;
    char    aiByte[2], *paiByte;

    unsigned short iCount;

    char *buf;

    /* Register the custom tag hadler */
    TIFFSetTagExtender(TagExtender);

    /* Create a new TIFF with the bunch of custom tags */
    tiff = TIFFOpen("new.tif", "w");
    TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, 256);
    TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, 256);
    TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 8);
    TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 3);
    TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(tiff, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);

    TIFFSetField(tiff, TIFFTAG_ASCIITAG, "Tag contents");
    aiShort[0] = 263; aiShort[1] = 264;
    TIFFSetField(tiff, TIFFTAG_SHORTTAG, 2, aiShort);
    aiLong[0] = 117; aiLong[1] = 118;
    TIFFSetField(tiff, TIFFTAG_LONGTAG, 2, aiLong);
    afFloat[0] = 0.333333; afFloat[1] = 0.444444;
    TIFFSetField(tiff, TIFFTAG_RATIONALTAG, 2, afFloat);
    afFloat[0] = 0.666666; afFloat[1] = 0.777777;
    TIFFSetField(tiff, TIFFTAG_FLOATTAG, 2, afFloat);
    adfDouble[0] = 0.1234567; adfDouble[1] = 0.7654321;
    TIFFSetField(tiff, TIFFTAG_DOUBLETAG, 2, adfDouble);
    aiByte[0] = 89; aiByte[1] = 90;
    TIFFSetField(tiff, TIFFTAG_BYTE, 2, aiByte);
    buf = _TIFFmalloc(3 * 256 * 256);
    TIFFWriteEncodedStrip(tiff, 0, buf, 3 * 256 * 256);
    _TIFFfree(buf);
    TIFFClose(tiff);

    /* Now open that TIFF back and read new tags */
    tiff = TIFFOpen("new.tif", "r");
    TIFFGetField(tiff, TIFFTAG_ASCIITAG, &pszString);
    printf("ASCII tag: %s\n", pszString);
    TIFFGetField(tiff, TIFFTAG_SHORTTAG, &iCount, &paiShort);
    if (iCount == 2)
            printf("Short tag: %d\n", paiShort[0]);
    TIFFGetField(tiff, TIFFTAG_LONGTAG, &iCount, &paiLong);
    if (iCount == 2)
            printf("Long tag: %ld\n", paiLong[0]);
    TIFFGetField(tiff, TIFFTAG_RATIONALTAG, &iCount, &pafFloat);
    if (iCount == 2)
            printf("Rational tag: %f\n", pafFloat[0]);
    TIFFGetField(tiff, TIFFTAG_FLOATTAG, &iCount, &pafFloat);
    if (iCount == 2)
            printf("Float tag: %f\n", pafFloat[0]);
    TIFFGetField(tiff, TIFFTAG_DOUBLETAG, &iCount, &padfDouble);
    if (iCount == 2)
            printf("Double tag: %lf\n", padfDouble[0]);
    TIFFGetField(tiff, TIFFTAG_BYTE, &iCount, &paiByte);
    if (iCount == 2)
            printf("Byte tag: %d\n", paiByte[0]);
    TIFFClose(tiff);

    return 0;
}