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 21, 2024
1 parent 35a0725 commit a56f7af
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 @@ -3850,3 +3850,17 @@ def test_jp2openjpeg_vrt_protocol():
webserver.server_stop(webserver_process, webserver_port)

gdal.VSICurlClearCache()


###############################################################################
# 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/opjlike/jp2opjlikedataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2174,9 +2174,11 @@ GDALDataset *JP2OPJLikeDataset<CODEC, BASE>::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 @@ -2185,6 +2187,7 @@ GDALDataset *JP2OPJLikeDataset<CODEC, BASE>::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 a56f7af

Please sign in to comment.