Skip to content

Commit

Permalink
VRT warp: fix issue with partial blocks at the right/bottom and dest …
Browse files Browse the repository at this point in the history
…nodata values that are different per band (fixes OSGeo#6581)

git-svn-id: https://svn.osgeo.org/gdal/trunk@34551 f0d54148-0727-0410-94bb-9a71ac55c965
  • Loading branch information
rouault committed Jul 5, 2016
1 parent 2a21ddd commit a3197c7
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 24 deletions.
50 changes: 50 additions & 0 deletions autotest/gdrivers/data/bug6581.vrt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<VRTDataset rasterXSize="1280" rasterYSize="512" subClass="VRTWarpedDataset">
<VRTRasterBand dataType="Byte" band="1" subClass="VRTWarpedRasterBand">
<NoDataValue>255</NoDataValue>
<ColorInterp>Red</ColorInterp>
</VRTRasterBand>
<VRTRasterBand dataType="Byte" band="2" subClass="VRTWarpedRasterBand">
<NoDataValue>0</NoDataValue>
<ColorInterp>Green</ColorInterp>
</VRTRasterBand>
<VRTRasterBand dataType="Byte" band="3" subClass="VRTWarpedRasterBand">
<NoDataValue>255</NoDataValue>
<ColorInterp>Blue</ColorInterp>
</VRTRasterBand>
<BlockXSize>512</BlockXSize>
<BlockYSize>128</BlockYSize>
<GDALWarpOptions>
<WarpMemoryLimit>6.71089e+07</WarpMemoryLimit>
<ResampleAlg>NearestNeighbour</ResampleAlg>
<WorkingDataType>Byte</WorkingDataType>
<Option name="INIT_DEST">NO_DATA</Option>
<SourceDataset relativeToVRT="1">bug6581_src.vrt</SourceDataset>
<Transformer>
<ApproxTransformer>
<MaxError>0.125</MaxError>
<BaseTransformer>
<GenImgProjTransformer>
<SrcGeoTransform>573452.527314628824,63.9710696713882214,0,4387441.90074732807,0,-63.9710696713883777</SrcGeoTransform>
<SrcInvGeoTransform>-8964.24790550457146,0.0156320662627165846,0,68584.7825163012749,0,-0.0156320662627165465</SrcInvGeoTransform>
<DstGeoTransform>557056,128,0,4325376,0,-128</DstGeoTransform>
<DstInvGeoTransform>-4352,0.0078125,0,33792,0,-0.0078125</DstInvGeoTransform>
</GenImgProjTransformer>
</BaseTransformer>
</ApproxTransformer>
</Transformer>
<BandList>
<BandMapping src="1" dst="1">
<DstNoDataReal>255</DstNoDataReal>
<DstNoDataImag>0</DstNoDataImag>
</BandMapping>
<BandMapping src="2" dst="2">
<DstNoDataReal>0</DstNoDataReal>
<DstNoDataImag>0</DstNoDataImag>
</BandMapping>
<BandMapping src="3" dst="3">
<DstNoDataReal>255</DstNoDataReal>
<DstNoDataImag>0</DstNoDataImag>
</BandMapping>
</BandList>
</GDALWarpOptions>
</VRTDataset>
38 changes: 38 additions & 0 deletions autotest/gdrivers/data/bug6581_src.vrt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<VRTDataset rasterXSize="1853" rasterYSize="1343">
<VRTRasterBand dataType="Byte" band="1">
<ColorInterp>Red</ColorInterp>
<ComplexSource>
<SourceFilename relativeToVRT="1">byte.tif</SourceFilename>
<SourceBand>1</SourceBand>
<SourceProperties RasterXSize="20" RasterYSize="20" DataType="Byte" BlockXSize="20" BlockYSize="20" />
<SrcRect xOff="0" yOff="0" xSize="20" ySize="20" />
<DstRect xOff="0" yOff="0" xSize="1853" ySize="1343" />
<ScaleOffset>127</ScaleOffset>
<ScaleRatio>0</ScaleRatio>
</ComplexSource>
</VRTRasterBand>
<VRTRasterBand dataType="Byte" band="2">
<ColorInterp>Green</ColorInterp>
<ComplexSource>
<SourceFilename relativeToVRT="1">byte.tif</SourceFilename>
<SourceBand>2</SourceBand>
<SourceProperties RasterXSize="20" RasterYSize="20" DataType="Byte" BlockXSize="20" BlockYSize="20" />
<SrcRect xOff="0" yOff="0" xSize="20" ySize="20" />
<DstRect xOff="0" yOff="0" xSize="1853" ySize="1343" />
<ScaleOffset>127</ScaleOffset>
<ScaleRatio>0</ScaleRatio>
</ComplexSource>
</VRTRasterBand>
<VRTRasterBand dataType="Byte" band="3">
<ColorInterp>Blue</ColorInterp>
<ComplexSource>
<SourceFilename relativeToVRT="1">byte.tif</SourceFilename>
<SourceBand>3</SourceBand>
<SourceProperties RasterXSize="20" RasterYSize="20" DataType="Byte" BlockXSize="20" BlockYSize="20" />
<SrcRect xOff="0" yOff="0" xSize="20" ySize="20" />
<DstRect xOff="0" yOff="0" xSize="1853" ySize="1343" />
<ScaleOffset>127</ScaleOffset>
<ScaleRatio>0</ScaleRatio>
</ComplexSource>
</VRTRasterBand>
</VRTDataset>
21 changes: 9 additions & 12 deletions autotest/gdrivers/vrtwarp.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,27 +470,24 @@ def vrtwarp_10():

def vrtwarp_11():

src_ds = gdal.Open('../gcore/data/byte.tif')
tmp_ds = gdal.GetDriverByName('GTiff').CreateCopy('tmp/vrtwarp_11.tif', src_ds)
tmp_ds.BuildOverviews( 'NEAR', overviewlist = [2] )
tmp_ds = None

ds = gdal.Warp('tmp/vrtwarp_11.vrt', 'tmp/vrtwarp_11.tif', options = '-dstalpha -of VRT')
cs1 = ds.GetRasterBand(1).GetOverview(0).Checksum()
cs2 = ds.GetRasterBand(2).GetOverview(0).Checksum()
ds = gdal.Open('data/bug6581.vrt')
cs1 = ds.GetRasterBand(1).Checksum()
cs2 = ds.GetRasterBand(2).Checksum()
cs3 = ds.GetRasterBand(3).Checksum()
ds = None

gdal.Unlink('tmp/vrtwarp_11.tif')
gdal.Unlink('tmp/vrtwarp_11.vrt')

if cs1 != 1087 or cs2 != 1218:
if cs1 != 22122 or cs2 != 56685 or cs3 != 22122:
gdaltest.post_reason('fail')
print(cs1)
print(cs2)
print(cs3)
return 'fail'

return 'success'

###############################################################################
# Test different nodata values on bands and partial blocks (#6581)

gdaltest_list = [
vrtwarp_1,
vrtwarp_2,
Expand Down
25 changes: 13 additions & 12 deletions gdal/frmts/vrt/vrtwarped.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1353,10 +1353,17 @@ CPLErr VRTWarpedDataset::ProcessBlock( int iBlockX, int iBlockY )
/* -------------------------------------------------------------------- */
const int nWordSize = GDALGetDataTypeSizeBytes(psWO->eWorkingDataType);

// FIXME? : risk of overflow in multiplication if nBlockXSize or
// nBlockYSize are very large.
int nReqXSize = m_nBlockXSize;
if( iBlockX * m_nBlockXSize + nReqXSize > nRasterXSize )
nReqXSize = nRasterXSize - iBlockX * m_nBlockXSize;
int nReqYSize = m_nBlockYSize;
if( iBlockY * m_nBlockYSize + nReqYSize > nRasterYSize )
nReqYSize = nRasterYSize - iBlockY * m_nBlockYSize;

// FIXME? : risk of overflow in multiplication if nReqXSize or
// nReqYSize are very large.
const int nDstBufferSize
= m_nBlockXSize * m_nBlockYSize * psWO->nBandCount * nWordSize;
= nReqXSize * nReqYSize * psWO->nBandCount * nWordSize;

GByte *pabyDstBuffer = static_cast<GByte *>(
VSI_MALLOC_VERBOSE(nDstBufferSize) );
Expand All @@ -1382,10 +1389,10 @@ CPLErr VRTWarpedDataset::ProcessBlock( int iBlockX, int iBlockY )
char **papszInitValues =
CSLTokenizeStringComplex( pszInitDest, ",", FALSE, FALSE );
const int nInitCount = CSLCount(papszInitValues);
const int nBandSize = nReqXSize * nReqYSize * nWordSize;

for( int iBand = 0; iBand < psWO->nBandCount; iBand++ )
{
const int nBandSize = m_nBlockXSize * m_nBlockYSize * nWordSize;
const char *pszBandInit
= papszInitValues[std::min( iBand, nInitCount - 1 )];

Expand Down Expand Up @@ -1424,13 +1431,13 @@ CPLErr VRTWarpedDataset::ProcessBlock( int iBlockX, int iBlockY )
{
GDALCopyWords( &adfInitRealImag, GDT_Float64, 0,
pBandData,psWO->eWorkingDataType,nWordSize,
m_nBlockXSize * m_nBlockYSize );
nReqXSize * nReqYSize );
}
else
{
GDALCopyWords( &adfInitRealImag, GDT_CFloat64, 0,
pBandData,psWO->eWorkingDataType,nWordSize,
m_nBlockXSize * m_nBlockYSize );
nReqXSize * nReqYSize );
}
}

Expand All @@ -1440,12 +1447,6 @@ CPLErr VRTWarpedDataset::ProcessBlock( int iBlockX, int iBlockY )
/* -------------------------------------------------------------------- */
/* Warp into this buffer. */
/* -------------------------------------------------------------------- */
int nReqXSize = m_nBlockXSize;
if( iBlockX * m_nBlockXSize + nReqXSize > nRasterXSize )
nReqXSize = nRasterXSize - iBlockX * m_nBlockXSize;
int nReqYSize = m_nBlockYSize;
if( iBlockY * m_nBlockYSize + nReqYSize > nRasterYSize )
nReqYSize = nRasterYSize - iBlockY * m_nBlockYSize;

const CPLErr eErr =
m_poWarper->WarpRegionToBuffer(
Expand Down

0 comments on commit a3197c7

Please sign in to comment.