From b7b005a395ff64a16e71e70c68562ddc46710661 Mon Sep 17 00:00:00 2001 From: David Rose Date: Mon, 31 Jan 2005 22:09:49 +0000 Subject: [PATCH] fix incorrect TIFF alpha information on write --- panda/src/pnmimagetypes/pnmFileTypeTIFF.cxx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/panda/src/pnmimagetypes/pnmFileTypeTIFF.cxx b/panda/src/pnmimagetypes/pnmFileTypeTIFF.cxx index 9f2fdde126..74c91c36db 100644 --- a/panda/src/pnmimagetypes/pnmFileTypeTIFF.cxx +++ b/panda/src/pnmimagetypes/pnmFileTypeTIFF.cxx @@ -422,6 +422,18 @@ Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) : _num_channels++; } + } else if ((_num_channels == 2 || _num_channels == 4) && num_extra_samples == 0) { + // If we have a 2- or 4-channel image but the extra samples are + // not declared, assume it was written out by a broken TIFF + // implementation and that the extra channel is really mean to + // be alpha. + unassoc_alpha_sample = _num_channels - 1; + if (pnmimage_tiff_cat.is_debug()) { + pnmimage_tiff_cat.debug() + << "Assuming last channel of " << spp + << "-color image is meant to represent alpha.\n"; + } + } else { pnmimage_tiff_cat.error() << "Cannot handle " << spp << "-color image (with " @@ -771,6 +783,7 @@ write_data(xel *array, xelval *alpha) { struct tiff * tif; short photometric = 0; short samplesperpixel = 0; + unsigned short extra_samples[1] = { EXTRASAMPLE_UNASSALPHA }; short bitspersample = 0; int bytesperrow = 0; unsigned char* buf; @@ -896,6 +909,9 @@ write_data(xel *array, xelval *alpha) { TIFFSetField( tif, TIFFTAG_IMAGEDESCRIPTION, "Generated via pnmimage.\n" ); TIFFSetField( tif, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel ); + if (has_alpha()) { + TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, 1, extra_samples); + } TIFFSetField( tif, TIFFTAG_ROWSPERSTRIP, tiff_rowsperstrip ); /* TIFFSetField( tif, TIFFTAG_STRIPBYTECOUNTS, _y_size / tiff_rowsperstrip ); */ TIFFSetField( tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );