gdal.input: optimize nodata handling in RGB case

When the data type is Byte, instead of reading a first time
the red band to deduce the value of the alpha component of
the output image, read the RGB bands first.
This commit is contained in:
Even Rouault 2015-02-05 20:38:27 +01:00
parent 073bad21c0
commit efdca26f2d

View file

@ -309,7 +309,10 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
raster_nodata = red->GetNoDataValue(&raster_has_nodata);
GDALColorTable *color_table = red->GetColorTable();
bool has_nodata = nodata_value_ || raster_has_nodata;
if (has_nodata && !color_table)
// we can deduce the alpha channel from nodata in the Byte case
// by reusing the reading of R,G,B bands directly
if (has_nodata && !color_table && red->GetRasterDataType() != GDT_Byte)
{
double apply_nodata = nodata_value_ ? *nodata_value_ : raster_nodata;
// read the data in and create an alpha channel from the nodata values
@ -372,10 +375,26 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
throw datasource_exception(CPLGetLastErrorMsg());
}
}
raster_io_error = blue->RasterIO(GF_Read, x_off, y_off, width, height, image.getBytes() + 2,
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
if (raster_io_error == CE_Failure) {
throw datasource_exception(CPLGetLastErrorMsg());
// In the case we skipped initializing the alpha channel
if (has_nodata && !color_table && red->GetRasterDataType() == GDT_Byte)
{
double apply_nodata = nodata_value_ ? *nodata_value_ : raster_nodata;
if( apply_nodata >= 0 && apply_nodata <= 255 )
{
int len = image.width() * image.height();
GByte bNoData = (GByte)(apply_nodata + 0.5);
GByte* pabyBytes = (GByte*) image.getBytes();
for (int i = 0; i < len; ++i)
{
// TODO - we assume here the nodata value for the red band applies to all bands
// more details about this at http://trac.osgeo.org/gdal/ticket/2734
if( pabyBytes[4*i] == bNoData )
pabyBytes[4*i + 3] = 0;
else
pabyBytes[4*i + 3] = 255;
}
}
}
}
else if (grey)