added support for loading images with alpha channel to TIFF handler
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@47204 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
parent
5c4ed98d99
commit
b6ac40dc77
@ -267,6 +267,14 @@ bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbos
|
|||||||
TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
|
TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &w );
|
||||||
TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h );
|
TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &h );
|
||||||
|
|
||||||
|
uint16 extraSamples;
|
||||||
|
uint16* samplesInfo;
|
||||||
|
TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
|
||||||
|
&extraSamples, &samplesInfo);
|
||||||
|
const bool hasAlpha = (extraSamples == 1 &&
|
||||||
|
(samplesInfo[0] == EXTRASAMPLE_ASSOCALPHA ||
|
||||||
|
samplesInfo[0] == EXTRASAMPLE_UNASSALPHA));
|
||||||
|
|
||||||
npixels = w * h;
|
npixels = w * h;
|
||||||
|
|
||||||
raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
|
raster = (uint32*) _TIFFmalloc( npixels * sizeof(uint32) );
|
||||||
@ -293,6 +301,9 @@ bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbos
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( hasAlpha )
|
||||||
|
image->SetAlpha();
|
||||||
|
|
||||||
if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
|
if (!TIFFReadRGBAImage( tif, w, h, raster, 0 ))
|
||||||
{
|
{
|
||||||
if (verbose)
|
if (verbose)
|
||||||
@ -305,47 +316,38 @@ bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbos
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasmask = false;
|
|
||||||
|
|
||||||
unsigned char *ptr = image->GetData();
|
unsigned char *ptr = image->GetData();
|
||||||
ptr += w*3*(h-1);
|
ptr += w*3*(h-1);
|
||||||
|
|
||||||
|
unsigned char *alpha = hasAlpha ? image->GetAlpha() : NULL;
|
||||||
|
if ( hasAlpha )
|
||||||
|
alpha += w*(h-1);
|
||||||
|
|
||||||
uint32 pos = 0;
|
uint32 pos = 0;
|
||||||
|
|
||||||
for (uint32 i = 0; i < h; i++)
|
for (uint32 i = 0; i < h; i++)
|
||||||
{
|
{
|
||||||
for (uint32 j = 0; j < w; j++)
|
for (uint32 j = 0; j < w; j++)
|
||||||
{
|
{
|
||||||
unsigned char alpha = (unsigned char)TIFFGetA(raster[pos]);
|
*(ptr++) = (unsigned char)TIFFGetR(raster[pos]);
|
||||||
if (alpha < 127)
|
*(ptr++) = (unsigned char)TIFFGetG(raster[pos]);
|
||||||
{
|
*(ptr++) = (unsigned char)TIFFGetB(raster[pos]);
|
||||||
hasmask = true;
|
if ( hasAlpha )
|
||||||
ptr[0] = image->GetMaskRed();
|
*(alpha++) = (unsigned char)TIFFGetA(raster[pos]);
|
||||||
ptr++;
|
|
||||||
ptr[0] = image->GetMaskGreen();
|
|
||||||
ptr++;
|
|
||||||
ptr[0] = image->GetMaskBlue();
|
|
||||||
ptr++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ptr[0] = (unsigned char)TIFFGetR(raster[pos]);
|
|
||||||
ptr++;
|
|
||||||
ptr[0] = (unsigned char)TIFFGetG(raster[pos]);
|
|
||||||
ptr++;
|
|
||||||
ptr[0] = (unsigned char)TIFFGetB(raster[pos]);
|
|
||||||
ptr++;
|
|
||||||
}
|
|
||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
ptr -= 2*w*3; // subtract line we just added plus one line
|
|
||||||
|
// subtract line we just added plus one line:
|
||||||
|
ptr -= 2*w*3;
|
||||||
|
if ( hasAlpha )
|
||||||
|
alpha -= 2*w;
|
||||||
}
|
}
|
||||||
|
|
||||||
_TIFFfree( raster );
|
_TIFFfree( raster );
|
||||||
|
|
||||||
TIFFClose( tif );
|
TIFFClose( tif );
|
||||||
|
|
||||||
image->SetMask( hasmask );
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user