2004.08.26 17:38 "[Tiff] (no subject)", by Graf Martin

2004.08.29 19:08 "Re: [Tiff] (no subject)", by Andrey Kiselev

Does one know, how I can add some special tags to a existing *.tiff file?

Hello,

You can found a short sample code attached. This program shows how to set and read custom tags of different types.

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, 1, 1, TIFF_SHORT,  FIELD_CUSTOM, 0, 1,
            "ShortTag" },
        { TIFFTAG_LONGTAG, 1, 1, TIFF_LONG,  FIELD_CUSTOM, 0, 1,
            "LongTag" },
        { TIFFTAG_RATIONALTAG, 1, 1, TIFF_RATIONAL,  FIELD_CUSTOM, 0, 1,
            "RationalTag" },
        { TIFFTAG_FLOATTAG, 1, 1, TIFF_FLOAT,  FIELD_CUSTOM, 0, 1,
            "FloatTag" },
        { TIFFTAG_DOUBLETAG, 1, 1, TIFF_DOUBLE,  FIELD_CUSTOM, 0, 1,
            "DoubleTag" },
        { TIFFTAG_BYTE, 1, 1, TIFF_BYTE,  FIELD_CUSTOM, 0, 1,
            "DoubleTag" },
    };

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

int main()
{
    TIFF    *tiff;
    char    *pszString;
    long    iLong, *paiLong;
    short   iShort, *paiShort;
    float   fFloat, *pafFloat;
    double  dfDouble, *padfDouble;
    char    iByte, *paiByte;

    unsigned short iCount;

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

    /* Open existing TIFF file in update mode */
    tiff = TIFFOpen("test.tif", "r+");
    TIFFSetField(tiff, TIFFTAG_ASCIITAG, "Tag contents");
    iShort = 263;
    TIFFSetField(tiff, TIFFTAG_SHORTTAG, 1, &iShort);
    iLong = 117;
    TIFFSetField(tiff, TIFFTAG_LONGTAG, 1, &iLong);
    fFloat = 0.333333;
    TIFFSetField(tiff, TIFFTAG_RATIONALTAG, 1, &fFloat);
    fFloat = 0.666666;
    TIFFSetField(tiff, TIFFTAG_FLOATTAG, 1, &fFloat);
    dfDouble = 0.1234567;
    TIFFSetField(tiff, TIFFTAG_DOUBLETAG, 1, &dfDouble);
    iByte = 89;
    TIFFSetField(tiff, TIFFTAG_BYTE, 1, &iByte);
    TIFFRewriteDirectory(tiff);
    TIFFClose(tiff);

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

    return 0;
}