Skip to content

Commit 546fe45

Browse files
committed
raster save as image transparency fix
1 parent 6aa7524 commit 546fe45

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/core/raster/qgsrasterfilewriter.cpp

+16-4
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,9 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
418418
}
419419

420420
const QgsRasterInterface* iface = iter->input();
421-
if ( !iface || ( iface->dataType( 1 ) != QgsRasterInterface::ARGB32 &&
422-
iface->dataType( 1 ) != QgsRasterInterface::ARGB32_Premultiplied ) )
421+
QgsRasterInterface::DataType inputDataType = iface->dataType( 1 );
422+
if ( !iface || ( inputDataType != QgsRasterInterface::ARGB32 &&
423+
inputDataType != QgsRasterInterface::ARGB32_Premultiplied ) )
423424
{
424425
return SourceProviderError;
425426
}
@@ -434,7 +435,7 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
434435
iter->setMaximumTileWidth( mMaxTileWidth );
435436
iter->setMaximumTileHeight( mMaxTileHeight );
436437

437-
void* data = VSIMalloc( iface->typeSize( iface->dataType( 1 ) ) / 8 * mMaxTileWidth * mMaxTileHeight );
438+
void* data = VSIMalloc( iface->typeSize( inputDataType ) / 8 * mMaxTileWidth * mMaxTileHeight );
438439
void* redData = VSIMalloc( mMaxTileWidth * mMaxTileHeight );
439440
void* greenData = VSIMalloc( mMaxTileWidth * mMaxTileHeight );
440441
void* blueData = VSIMalloc( mMaxTileWidth * mMaxTileHeight );
@@ -488,14 +489,25 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
488489
//fill into red/green/blue/alpha channels
489490
uint* p = ( uint* ) data;
490491
int nPixels = iterCols * iterRows;
492+
// TODO: should be char not int? we are then copying 1 byte
491493
int red = 0;
492494
int green = 0;
493495
int blue = 0;
494496
int alpha = 255;
495497
for ( int i = 0; i < nPixels; ++i )
496498
{
497499
QRgb c( *p++ );
498-
red = qRed( c ); green = qGreen( c ); blue = qBlue( c ); alpha = qAlpha( c );
500+
alpha = qAlpha( c );
501+
red = qRed( c ); green = qGreen( c ); blue = qBlue( c );
502+
503+
if ( inputDataType == QgsRasterInterface::ARGB32_Premultiplied )
504+
{
505+
double a = alpha / 255.;
506+
QgsDebugMsg( QString( "red = %1 green = %2 blue = %3 alpha = %4 p = %5 a = %6" ).arg( red ).arg( green ).arg( blue ).arg( alpha ).arg(( int )*p, 0, 16 ).arg( a ) );
507+
red /= a;
508+
green /= a;
509+
blue /= a;
510+
}
499511
memcpy(( char* )redData + i, &red, 1 );
500512
memcpy(( char* )greenData + i, &green, 1 );
501513
memcpy(( char* )blueData + i, &blue, 1 );

0 commit comments

Comments
 (0)