Skip to content

Commit 612810b

Browse files
authored
Merge pull request #10979 from rouault/fix_10978
JP2ECW: report JPEG2000 tile size as GDAL block size for ECW SDK >= 51 (up to 2048x2048 and dataset dimensions)
2 parents 62816a7 + 96c0616 commit 612810b

3 files changed

Lines changed: 57 additions & 7 deletions

File tree

autotest/gdrivers/ecw.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def startup_and_cleanup():
9595
else:
9696
gdaltest.ecw_drv.major_version = 3
9797
gdaltest.ecw_drv.minor_version = 3
98+
gdaltest.ecw_drv.version = (
99+
gdaltest.ecw_drv.major_version,
100+
gdaltest.ecw_drv.minor_version,
101+
)
98102

99103
# we set ECW to not resolve projection and datum strings to get 3.x behavior.
100104
with gdal.config_option("ECW_DO_NOT_RESOLVE_DATUM_PROJECTION", "YES"):
@@ -2465,3 +2469,16 @@ def test_ecw_online_7():
24652469
expected_band_count,
24662470
ds.RasterCount,
24672471
)
2472+
2473+
2474+
###############################################################################
2475+
# Test reading a dataset whose tile dimensions are larger than dataset ones
2476+
2477+
2478+
def test_ecw_byte_tile_2048():
2479+
2480+
ds = gdal.Open("data/jpeg2000/byte_tile_2048.jp2")
2481+
(blockxsize, blockysize) = ds.GetRasterBand(1).GetBlockSize()
2482+
assert (blockxsize, blockysize) == (
2483+
(20, 20) if gdaltest.ecw_drv.version >= (5, 1) else (256, 256)
2484+
)

autotest/gdrivers/nitf.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,10 +880,11 @@ def test_nitf_28_jp2ecw(tmp_path):
880880
blockxsize, blockysize = ds.GetRasterBand(1).GetBlockSize()
881881
ds = None
882882
gdal.Unlink(tmpfilename)
883-
assert (blockxsize, blockysize) == (
884-
256,
885-
256,
886-
) # 256 since this is hardcoded as such in the ECW driver
883+
# 256 for ECW < 5.1, 1024 for ECW >= 5.1
884+
assert (blockxsize, blockysize) == (256, 256) or (blockxsize, blockysize) == (
885+
1024,
886+
1024,
887+
)
887888
finally:
888889
gdaltest.reregister_all_jpeg2000_drivers()
889890

frmts/ecw/ecwdataset.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "ecwdrivercore.h"
2626

27+
#include <algorithm>
2728
#include <cmath>
2829

2930
#undef NOISY_DEBUG
@@ -33,7 +34,7 @@ static int bNCSInitialized = FALSE;
3334

3435
void ECWInitialize(void);
3536

36-
#define BLOCK_SIZE 256
37+
constexpr int DEFAULT_BLOCK_SIZE = 256;
3738

3839
GDALDataset *ECWDatasetOpenJPEG2000(GDALOpenInfo *poOpenInfo);
3940

@@ -71,8 +72,39 @@ ECWRasterBand::ECWRasterBand(ECWDataset *poDSIn, int nBandIn, int iOverviewIn,
7172
nRasterXSize = poDS->GetRasterXSize() / (1 << (iOverview + 1));
7273
nRasterYSize = poDS->GetRasterYSize() / (1 << (iOverview + 1));
7374

74-
nBlockXSize = BLOCK_SIZE;
75-
nBlockYSize = BLOCK_SIZE;
75+
#if ECWSDK_VERSION >= 51
76+
if (poDSIn->bIsJPEG2000 && poDSIn->poFileView)
77+
{
78+
UINT32 nTileWidth = 0;
79+
poDSIn->poFileView->GetParameter(
80+
const_cast<char *>("JPC:DECOMPRESS:TILESIZE:X"), &nTileWidth);
81+
if (nTileWidth <= static_cast<UINT32>(INT_MAX))
82+
{
83+
nBlockXSize = static_cast<int>(nTileWidth);
84+
}
85+
nBlockXSize = std::min(nBlockXSize, nRasterXSize);
86+
87+
UINT32 nTileHeight = 0;
88+
poDSIn->poFileView->GetParameter(
89+
const_cast<char *>("JPC:DECOMPRESS:TILESIZE:Y"), &nTileHeight);
90+
if (nTileHeight <= static_cast<UINT32>(INT_MAX))
91+
{
92+
nBlockYSize = static_cast<int>(nTileHeight);
93+
}
94+
nBlockYSize = std::min(nBlockYSize, nRasterYSize);
95+
}
96+
#endif
97+
98+
// Slightly arbitrary value. Too large values would defeat the purpose
99+
// of the block concept.
100+
constexpr int LIMIT_FOR_BLOCK_SIZE = 2048;
101+
if (nBlockXSize <= 0 || nBlockYSize <= 0 ||
102+
nBlockXSize > LIMIT_FOR_BLOCK_SIZE ||
103+
nBlockYSize > LIMIT_FOR_BLOCK_SIZE)
104+
{
105+
nBlockXSize = DEFAULT_BLOCK_SIZE;
106+
nBlockYSize = DEFAULT_BLOCK_SIZE;
107+
}
76108

77109
/* -------------------------------------------------------------------- */
78110
/* Work out band color interpretation. */

0 commit comments

Comments
 (0)