Save Tiff with color palette

Hello!

I am trying to save an image to a .tif file. When saving as an RGB standard image everything is ok. But when I try to save the file as a LUT/palette color image the app crashes.

Here is the piece of code

int _tmain(int argc, _TCHAR* argv[])
{
TIFF* tif = TIFFOpen("test.tif", "w");

TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, 100);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, 100);

TIFFSetField(tif, TIFFTAG_XRESOLUTION, 96);
TIFFSetField(tif, TIFFTAG_YRESOLUTION, 96);

TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, 2);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);


TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, (unsigned long)-1));


unsigned short *r, *g, *b;
r = (unsigned short *) _TIFFmalloc(sizeof(unsigned short) * 3 * 10);
g = r + 10;
b = g + 10;

srand(time(0));

for (int i = 0; i < 10; ++i)
{
r[i] = (unsigned short) rand() % 255;//((*(pImageLUT->_pColorTable))[i]._R);
g[i] = (unsigned short) rand() % 255;//((*(pImageLUT->_pColorTable))[i]._G);
b[i] = (unsigned short) rand() % 255;//((*(pImageLUT->_pColorTable))[i]._B);
}


TIFFSetField(tif, TIFFTAG_COLORMAP, r, g, b);

unsigned char* lineBuffLUT = new unsigned char[100] ;

srand(time(0));

for(int y = 0; y < 100; y++)
{
for(int i = 0; i < 100; i++)
lineBuffLUT[i] = rand()%10;

TIFFWriteScanline(tif, lineBuffLUT, y) ;
}

_TIFFfree(r);

TIFFClose(tif);
return 0;
}

The app crashes on TiffClose(tif), to be more accurate, in crashes on TiffCleanup().

Do you have any idea why this might happen?


Thank you,
Cip
Topic archived. No new replies allowed.