PNG: fix reading 16-bit interlaced images (on little-endian machines)

Fixes https://lists.osgeo.org/pipermail/gdal-dev/2024-December/059989.html
This commit is contained in:
Even Rouault 2024-12-28 18:01:55 +01:00
parent 8d5ca35da0
commit 73839c499a
No known key found for this signature in database
GPG Key ID: 33EBBFC47B3DD87D
3 changed files with 25 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 775 B

View File

@ -471,3 +471,13 @@ def test_png_copy_mdd():
ds = None
gdal.Unlink(filename)
###############################################################################
# Read test of 16-bit interlaced
def test_png_read_interlace_16_bit():
ds = gdal.Open("data/png/uint16_interlaced.png")
assert ds.GetRasterBand(1).Checksum() == 4672

View File

@ -1405,6 +1405,21 @@ CPLErr PNGDataset::LoadInterlacedChunk(int iLine)
bool bRet = safe_png_read_image(hPNG, png_rows, sSetJmpContext);
// Do swap on LSB machines. 16-bit PNG data is stored in MSB format.
#ifdef CPL_LSB
if (bRet && nBitDepth == 16)
{
for (int i = 0; i < GetRasterYSize(); i++)
{
if (i >= nBufferStartLine && i < nBufferStartLine + nBufferLines)
{
GDALSwapWords(png_rows[i], 2,
GetRasterXSize() * GetRasterCount(), 2);
}
}
}
#endif
CPLFree(png_rows);
CPLFree(dummy_row);
if (!bRet)