Skip to content

Commit

Permalink
JP2OpenJPEG: CreateCopy(): limit number of resolutions taking into ac…
Browse files Browse the repository at this point in the history
…count minimum block width/height (fixes OSGeo#9236)
  • Loading branch information
rouault authored and craigds committed Feb 20, 2024
1 parent 6c7ffa4 commit d01ccea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 14 additions & 0 deletions autotest/gdrivers/jp2openjpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3842,3 +3842,17 @@ def test_jp2openjpeg_reversible_quality_not_100():
ds = None

gdaltest.jp2openjpeg_drv.Delete(filename)


###############################################################################
# Test fix for https://github.com/OSGeo/gdal/issues/9236


def test_jp2openjpeg_limit_resolution_count_from_image_size(tmp_vsimem):

filename = str(tmp_vsimem / "out.jp2")
assert gdal.Translate(filename, "data/byte.tif", width=1024, height=7)

# Check number of resolutions
ret = gdal.GetJPEG2000StructureAsString(filename, ["ALL=YES"])
assert '<Field name="SPcod_NumDecompositions" type="uint8">2</Field>' in ret
5 changes: 4 additions & 1 deletion frmts/openjpeg/openjpegdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2725,9 +2725,11 @@ GDALDataset *JP2OpenJPEGDataset::CreateCopy(
}

const int nMaxTileDim = std::max(nBlockXSize, nBlockYSize);
const int nMinTileDim = std::min(nBlockXSize, nBlockYSize);
int nNumResolutions = 1;
/* Pickup a reasonable value compatible with PROFILE_1 requirements */
while ((nMaxTileDim >> (nNumResolutions - 1)) > 128)
while ((nMaxTileDim >> (nNumResolutions - 1)) > 128 &&
(nMinTileDim >> nNumResolutions) > 0)
nNumResolutions++;
int nMinProfile1Resolutions = nNumResolutions;
const char *pszResolutions =
Expand All @@ -2736,6 +2738,7 @@ GDALDataset *JP2OpenJPEGDataset::CreateCopy(
{
nNumResolutions = atoi(pszResolutions);
if (nNumResolutions <= 0 || nNumResolutions >= 32 ||
(nMinTileDim >> nNumResolutions) == 0 ||
(nMaxTileDim >> nNumResolutions) == 0)
{
CPLError(CE_Warning, CPLE_NotSupported,
Expand Down

0 comments on commit d01ccea

Please sign in to comment.