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
3435void ECWInitialize (void );
3536
36- # define BLOCK_SIZE 256
37+ constexpr int DEFAULT_BLOCK_SIZE = 256 ;
3738
3839GDALDataset *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