Skip to content

Commit

Permalink
VRT: fix reading from virtual overviews when SrcRect / DstRect elemen…
Browse files Browse the repository at this point in the history
…ts are missing
  • Loading branch information
rouault committed Aug 12, 2024
1 parent 151e0e8 commit dc1b3f3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
30 changes: 30 additions & 0 deletions autotest/gcore/vrt_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,36 @@ def test_vrt_read_21():
gdal.Unlink("/vsimem/byte.tif")


###############################################################################
# Test implicit virtual overviews


def test_vrt_read_virtual_overview_no_srcrect_dstrect(tmp_vsimem):

ds = gdal.Open("data/byte.tif")
data = ds.ReadRaster(0, 0, 20, 20, 400, 400)
ds = None
tmp_tif = str(tmp_vsimem / "tmp.tif")
ds = gdal.GetDriverByName("GTiff").Create(tmp_tif, 400, 400)
ds.WriteRaster(0, 0, 400, 400, data)
ds.BuildOverviews("NEAR", [2])
ref_cs = ds.GetRasterBand(1).GetOverview(0).Checksum()
ds = None

ds = gdal.Open(
f"""<VRTDataset rasterXSize="400" rasterYSize="400">
<VRTRasterBand dataType="Byte" band="1">
<SimpleSource>
<SourceFilename>{tmp_tif}</SourceFilename>
<SourceBand>1</SourceBand>
</SimpleSource>
</VRTRasterBand>
</VRTDataset>"""
)
assert ds.GetRasterBand(1).GetOverviewCount() == 1
assert ds.GetRasterBand(1).GetOverview(0).Checksum() == ref_cs


###############################################################################
# Test that we honour NBITS with SimpleSource and ComplexSource

Expand Down
27 changes: 23 additions & 4 deletions frmts/vrt/vrtsources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,34 @@ VRTSimpleSource::VRTSimpleSource(const VRTSimpleSource *poSrcSource,
m_dfSrcYOff(poSrcSource->m_dfSrcYOff),
m_dfSrcXSize(poSrcSource->m_dfSrcXSize),
m_dfSrcYSize(poSrcSource->m_dfSrcYSize),
m_dfDstXOff(poSrcSource->m_dfDstXOff * dfXDstRatio),
m_dfDstYOff(poSrcSource->m_dfDstYOff * dfYDstRatio),
m_dfDstXSize(poSrcSource->m_dfDstXSize * dfXDstRatio),
m_dfDstYSize(poSrcSource->m_dfDstYSize * dfYDstRatio),
m_nMaxValue(poSrcSource->m_nMaxValue), m_bRelativeToVRTOri(-1),
m_nExplicitSharedStatus(poSrcSource->m_nExplicitSharedStatus),
m_osSrcDSName(poSrcSource->m_osSrcDSName),
m_bDropRefOnSrcBand(poSrcSource->m_bDropRefOnSrcBand)
{
if (!poSrcSource->IsSrcWinSet() && !poSrcSource->IsDstWinSet() &&
(dfXDstRatio != 1.0 || dfYDstRatio != 1.0))
{
auto l_band = GetRasterBand();
if (l_band)
{
m_dfSrcXOff = 0;
m_dfSrcYOff = 0;
m_dfSrcXSize = l_band->GetXSize();
m_dfSrcYSize = l_band->GetYSize();
m_dfDstXOff = 0;
m_dfDstYOff = 0;
m_dfDstXSize = l_band->GetXSize() * dfXDstRatio;
m_dfDstYSize = l_band->GetYSize() * dfYDstRatio;
}
}
else if (poSrcSource->IsDstWinSet())
{
m_dfDstXOff = poSrcSource->m_dfDstXOff * dfXDstRatio;
m_dfDstYOff = poSrcSource->m_dfDstYOff * dfYDstRatio;
m_dfDstXSize = poSrcSource->m_dfDstXSize * dfXDstRatio;
m_dfDstYSize = poSrcSource->m_dfDstYSize * dfYDstRatio;
}
}

/************************************************************************/
Expand Down

0 comments on commit dc1b3f3

Please sign in to comment.