@@ -215,50 +215,38 @@ CPLErr RS2RasterBand::IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage)
215215 /* over-requesting. We also need to initialize the extra part */
216216 /* of the block to zero. */
217217 /* -------------------------------------------------------------------- */
218- int nRequestYSize;
219- if ((nBlockYOff + 1 ) * nBlockYSize > nRasterYSize)
220- {
221- nRequestYSize = nRasterYSize - nBlockYOff * nBlockYSize;
222- memset (pImage, 0 ,
223- static_cast <size_t >(GDALGetDataTypeSizeBytes (eDataType)) *
224- nBlockXSize * nBlockYSize);
225- }
226- else
227- {
228- nRequestYSize = nBlockYSize;
229- }
218+ const int nYOff = nBlockYOff * nBlockYSize;
219+ const int nRequestYSize = std::min (nBlockYSize, nRasterYSize - nYOff);
230220
231221 /* -------------------------------------------------------------------- */
232222 /* If the input imagery is tiled, also need to avoid over- */
233223 /* requesting in the X-direction. */
234224 /* ------------------------------------------------------------------- */
235- int nRequestXSize;
236- if ((nBlockXOff + 1 ) * nBlockXSize > nRasterXSize)
225+ const int nXOff = nBlockXOff * nBlockXSize;
226+ const int nRequestXSize = std::min (nBlockXSize, nRasterXSize - nXOff);
227+
228+ if (nRequestXSize < nBlockXSize || nRequestYSize < nBlockYSize)
237229 {
238- nRequestXSize = nRasterXSize - nBlockXOff * nBlockXSize;
239230 memset (pImage, 0 ,
240231 static_cast <size_t >(GDALGetDataTypeSizeBytes (eDataType)) *
241232 nBlockXSize * nBlockYSize);
242233 }
243- else
244- {
245- nRequestXSize = nBlockXSize;
246- }
234+
247235 if (eDataType == GDT_CInt16 && poBandFile->GetRasterCount () == 2 )
248- return poBandFile->RasterIO (
249- GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize ,
250- nRequestXSize, nRequestYSize, pImage, nRequestXSize, nRequestYSize ,
251- GDT_Int16, 2 , nullptr , 4 , nBlockXSize * 4 , 2 , nullptr );
236+ return poBandFile->RasterIO (GF_Read, nXOff, nYOff, nRequestXSize,
237+ nRequestYSize, pImage, nRequestXSize ,
238+ nRequestYSize, GDT_Int16, 2 , nullptr , 4 ,
239+ nBlockXSize * 4 , 2 , nullptr );
252240
253241 /* -------------------------------------------------------------------- */
254242 /* File has one sample marked as sample format void, a 32bits. */
255243 /* -------------------------------------------------------------------- */
256244 else if (eDataType == GDT_CInt16 && poBandFile->GetRasterCount () == 1 )
257245 {
258246 CPLErr eErr = poBandFile->RasterIO (
259- GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize ,
260- nRequestXSize, nRequestYSize, pImage, nRequestXSize, nRequestYSize ,
261- GDT_UInt32, 1 , nullptr , 4 , nBlockXSize * 4 , 0 , nullptr );
247+ GF_Read, nXOff, nYOff, nRequestXSize, nRequestYSize, pImage ,
248+ nRequestXSize, nRequestYSize, GDT_UInt32, 1 , nullptr , 4 ,
249+ nBlockXSize * 4 , 0 , nullptr );
262250
263251#ifdef CPL_LSB
264252 /* First, undo the 32bit swap. */
@@ -276,16 +264,16 @@ CPLErr RS2RasterBand::IReadBlock(int nBlockXOff, int nBlockYOff, void *pImage)
276264 /* looks like a 16bit unsigned data too. */
277265 /* -------------------------------------------------------------------- */
278266 else if (eDataType == GDT_UInt16)
279- return poBandFile->RasterIO (
280- GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize ,
281- nRequestXSize, nRequestYSize, pImage, nRequestXSize, nRequestYSize ,
282- GDT_UInt16, 1 , nullptr , 2 , nBlockXSize * 2 , 0 , nullptr );
267+ return poBandFile->RasterIO (GF_Read, nXOff, nYOff, nRequestXSize,
268+ nRequestYSize, pImage, nRequestXSize ,
269+ nRequestYSize, GDT_UInt16, 1 , nullptr , 2 ,
270+ nBlockXSize * 2 , 0 , nullptr );
283271 else if (eDataType == GDT_UInt8)
284272 /* Ticket #2104: Support for ScanSAR products */
285- return poBandFile->RasterIO (
286- GF_Read, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize ,
287- nRequestXSize, nRequestYSize, pImage, nRequestXSize, nRequestYSize ,
288- GDT_UInt8, 1 , nullptr , 1 , nBlockXSize, 0 , nullptr );
273+ return poBandFile->RasterIO (GF_Read, nXOff, nYOff, nRequestXSize,
274+ nRequestYSize, pImage, nRequestXSize ,
275+ nRequestYSize, GDT_UInt8, 1 , nullptr , 1 ,
276+ nBlockXSize, 0 , nullptr );
289277
290278 CPLAssert (false );
291279 return CE_Failure;
0 commit comments