2007.08.07 14:41 "[Tiff] Indexed Images", by Christian Henning

2007.08.07 15:23 "Re: [Tiff] Indexed Images", by Frank Warmerdam

Hi there, I'm trying to write a 4-bit indexed image using libtiff
3.8.2 on a Windows machine. For some reasons writing the color map
always crashes and I have no idea why. Here is my code, it's very
straight forward:

int main()
{
   tsize_t width = 100;
   tsize_t height = 100;
   tsize_t bits_per_sample = 4;
   tsize_t samples_per_pixel = 1;

tsize_t image_size_in_bytes = ( width * height ) / 2;
tsize_t palette_size = 16 * 3;

TIFF* file = TIFFOpen( "palette.tif", "w" );

   TIFFSetField( file, TIFFTAG_IMAGEWIDTH     , width               );
   TIFFSetField( file, TIFFTAG_IMAGELENGTH    , height              );
   TIFFSetField( file, TIFFTAG_BITSPERSAMPLE  , bits_per_sample     );
   TIFFSetField( file, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel   );
   TIFFSetField( file, TIFFTAG_ROWSPERSTRIP   , height              );

std::vector< short > palette( palette_size ); std::fill( palette.begin(), palette.end(), 0 );

TIFFSetField( file, TIFFTAG_COLORMAP, &palette.front() );

std::vector< unsigned char > image( image_size_in_bytes ); std::fill( image.begin(), image.end(), 0 );

TIFFWriteEncodedStrip( file
                     , 0
                     , &image.front()
                     , image_size_in_bytes );

   TIFFClose( file );
}

Can anyone tell me why the line

TIFFSetField( file, TIFFTAG_COLORMAP, &palette.front() );

always crashes?

Christian,

Because TIFFSetField() expects three short arrays as an argument for key TIFFTAG_COLORMAP. (red, green and blue portions of the palette).

Contrary to what Toby says, I'm fairly confident that taking the address of the reference returned by palette.front() should work. Oddly, I setup some similar code just last night using std::vector<> for TIFF palettes. A simple example of passing three short arrays looks like this:

         TIFFSetField( hTIFF, TIFFTAG_COLORMAP, anTRed, anTGreen, anTBlue );

Best regards,
--
---------------------------------------+--------------------------------------

I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam@pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam

and watch the world go round - Rush    | President OSGeo, http://osgeo.org