2003.09.12 16:04 "[Tiff] writing out custom tags", by Chris Padwick

2003.09.12 16:04 "[Tiff] writing out custom tags", by Chris Padwick

Hi,

I'm trying to copy a custom tag from an input file to an output file. I'm using version 3.6.0 of the tiff library, grabbed from cvs last night. First, tiffcp doesn't copy the tag I'm interested in. The input file is tiled and has 5 directories and each directory has a private tag. Here is a tiffdump of the first directory:

Tiffdump:

Magic: 0x4d4d <big-endian> Version: 0x2a
Directory 0: offset 881328152 (0x34880018) next 881437232 (0x3489aa30)
ImageWidth (256) SHORT (3) 1<6290>
ImageLength (257) SHORT (3) 1<6780>
<snip>
.
.
.
34595 (0x8723) LONG (4) 137<1769472 262144 65536 65537 262144 65536 6619141 589824 406664 222298118 65536 5256328 223870986 131072 341128 229113867 131072 341128 229507084 65536 7353480 229900301 65536 3683464...
<snip>

The tag I'm interested in is 34595. I can read this tag no problem, but when I go to write it out I get a segfault. Here is the code I'm using:

//=========================================================================
// test_write_tags.cpp : a function to test the writing of custom tags to tiff images.
//
//=========================================================================
//Includes
#include <stdio.h>
#include <stdlib.h>
#include "tiffio.h"

#define           TIFFTAG_PRIVATE         34595

static void TagExtender(TIFF *tiff)
{
    static const TIFFFieldInfo xtiffFieldInfo[] = {
        { TIFFTAG_PRIVATE, TIFF_VARIABLE, TIFF_VARIABLE, TIFF_LONG, FIELD_CUSTOM,
          0,      1,    "Private Tag" }
    };

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

int main( int argc, char* argv[] )
{
      TIFF *intif=(TIFF*)0;
      intif=TIFFOpen("inputfile.tif","r");
      if (!intif)
      {
            fprintf( stderr, "Unable to open input file.  Quitting.\n" );
            return 1;
      }

      TIFF *outtif=(TIFF*)0;
      outtif = TIFFOpen("outfile.tif", "w");
      if( !outtif )
      {
            fprintf( stderr, "\n\nUnable to open output file.  Quitting\n" );
            return( 1 );
      }

      int dir_count = 0;
      do
      {
            dir_count++;
      } while( TIFFReadDirectory( intif ) );

      printf( "\n\n%d directories in input file", dir_count );

      TIFFSetTagExtender(TagExtender);

      tdata_t buf;
      for( int dir_ndx = 0; dir_ndx < dir_count; dir_ndx++ )
      {
            TIFFSetDirectory( intif, dir_ndx );
            fprintf( stderr, "Processing directory %d ...\n", dir_ndx );

            uint32 tileWidth, tileLength, imageLength, imageWidth;
            uint32 x, y;

            TIFFGetField(intif, TIFFTAG_TILEWIDTH, &tileWidth);
            TIFFGetField(intif, TIFFTAG_TILELENGTH, &tileLength);
            TIFFGetField(intif, TIFFTAG_IMAGELENGTH, &imageLength);
            TIFFGetField(intif, TIFFTAG_IMAGEWIDTH, &imageWidth);

            TIFFCreateDirectory( outtif );
            TIFFSetDirectory( outtif, dir_ndx );

            TagExtender(outtif);
            unsigned long* lng;
            unsigned short iCount;
            int result = TIFFGetField(intif, TIFFTAG_PRIVATE, &iCount, &lng );
            if( result )
            {
                  for( unsigned short ii = 0; ii < iCount; ii++ )
                  {
                        printf("PRIVATE Long tag: %u\n", lng[ii]);
                        result = TIFFSetField(outtif, TIFFTAG_PRIVATE, ii, lng[ii]);
                  }
            }


            buf = _TIFFmalloc(TIFFTileSize(intif));
            for (y = 0; y < imageLength; y += tileLength)
            {
                  for (x = 0; x < imageWidth; x += tileWidth)
                  {
                        TIFFReadTile(intif, buf, x, y, 0, 0);
                        TIFFWriteTile(outtif, buf, x, y, 0, 0);
                  }
            }

            TIFFWriteDirectory( outtif );
      }


      _TIFFfree( buf );
    TIFFClose(intif);
    TIFFClose(outtif);

      return( 0 );
}

Interestingly enough, it writes the tag fine when ii = 0, but segfaults when ii = 1. Any thoughts on what could be wrong?

Thanks,

Chris Padwick