Skip to content

Commit

Permalink
Merge pull request OSGeo#11553 from rouault/png_16bit_interlaced
Browse files Browse the repository at this point in the history
PNG: fix reading 16-bit interlaced images (on little-endian machines)
  • Loading branch information
rouault authored Dec 28, 2024
2 parents 8d5ca35 + 73839c4 commit a6fed4d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Binary file added autotest/gdrivers/data/png/uint16_interlaced.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions autotest/gdrivers/png.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions frmts/png/pngdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a6fed4d

Please sign in to comment.