@@ -1121,11 +1121,19 @@ bool QgsRasterLayer::draw(QPainter * theQPainter,
1121
1121
1122
1122
// /\/\/\ - added to handle zoomed-in rasters
1123
1123
1124
+ if ((myRasterViewPort->drawableAreaXDimInt ) > 4000 && (myRasterViewPort->drawableAreaYDimInt > 4000 ))
1125
+ {
1126
+ // We have scale one raster pixel to more than 4000 screen pixels. What's the point of showing this layer?
1127
+ // Instead, we just stop displaying the layer. Prevents allocating the entire world of memory for showing
1128
+ // The pixel in all its glory.
1129
+ QgsDebugMsg (" Too zoomed out! Raster will not display" );
1130
+ return TRUE ;
1131
+ }
1124
1132
1125
1133
// Provider mode: See if a provider key is specified, and if so use the provider instead
1126
-
1134
+
1127
1135
QgsDebugMsg (" QgsRasterLayer::draw: Checking for provider key." );
1128
-
1136
+
1129
1137
if (!mProviderKey .isEmpty ())
1130
1138
{
1131
1139
QgsDebugMsg (" QgsRasterLayer::draw: Wanting a '" + mProviderKey + " ' provider to draw this." );
@@ -1326,7 +1334,7 @@ void QgsRasterLayer::draw (QPainter * theQPainter,
1326
1334
break ;
1327
1335
1328
1336
}
1329
-
1337
+
1330
1338
// see if debug info is wanted
1331
1339
if (showDebugOverlayFlag)
1332
1340
{
@@ -1344,6 +1352,12 @@ void QgsRasterLayer::drawSingleBandGray(QPainter * theQPainter, QgsRasterViewPor
1344
1352
GDALDataType myDataType = myGdalBand->GetRasterDataType ();
1345
1353
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
1346
1354
1355
+ /* Check for out of memory error */
1356
+ if (myGdalScanData == NULL )
1357
+ {
1358
+ return ;
1359
+ }
1360
+
1347
1361
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
1348
1362
// myQImage.fill(0);
1349
1363
myQImage.setAlphaBuffer (true );
@@ -1432,6 +1446,8 @@ void QgsRasterLayer::drawSingleBandGray(QPainter * theQPainter, QgsRasterViewPor
1432
1446
myQImage.setPixel (myRowInt, myColumnInt, qRgba (myGrayValInt, myGrayValInt, myGrayValInt, mTransparencyLevel ));
1433
1447
}
1434
1448
}
1449
+
1450
+ /* TODO: Should readData be freed here? */
1435
1451
1436
1452
// render any inline filters
1437
1453
filterLayer (&myQImage);
@@ -1487,6 +1503,12 @@ void QgsRasterLayer::drawSingleBandPseudoColor(QPainter * theQPainter,
1487
1503
GDALDataType myDataType = myGdalBand->GetRasterDataType ();
1488
1504
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
1489
1505
1506
+ /* Check for out of memory error */
1507
+ if (myGdalScanData == NULL )
1508
+ {
1509
+ return ;
1510
+ }
1511
+
1490
1512
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
1491
1513
// myQImage.fill(0);
1492
1514
myQImage.setAlphaBuffer (true );
@@ -1704,6 +1726,13 @@ void QgsRasterLayer::drawPalettedSingleBandColor(QPainter * theQPainter, QgsRast
1704
1726
GDALRasterBand *myGdalBand = gdalDataset->GetRasterBand (theBandNoInt);
1705
1727
GDALDataType myDataType = myGdalBand->GetRasterDataType ();
1706
1728
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
1729
+
1730
+ /* Check for out of memory error */
1731
+ if (myGdalScanData == NULL )
1732
+ {
1733
+ return ;
1734
+ }
1735
+
1707
1736
QgsColorTable *myColorTable = colorTable ( theBandNoInt );
1708
1737
1709
1738
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
@@ -1734,6 +1763,9 @@ void QgsRasterLayer::drawPalettedSingleBandColor(QPainter * theQPainter, QgsRast
1734
1763
myQImage.setPixel (myRowInt, myColumnInt, qRgba (c1, c2, c3, mTransparencyLevel ));
1735
1764
}
1736
1765
}
1766
+
1767
+ /* TODO: Should readData be freed here? */
1768
+
1737
1769
// render any inline filters
1738
1770
filterLayer (&myQImage);
1739
1771
@@ -1794,6 +1826,13 @@ void QgsRasterLayer::drawPalettedSingleBandGray(QPainter * theQPainter, QgsRaste
1794
1826
GDALRasterBand *myGdalBand = gdalDataset->GetRasterBand (theBandNoInt);
1795
1827
GDALDataType myDataType = myGdalBand->GetRasterDataType ();
1796
1828
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
1829
+
1830
+ /* Check for out of memory error */
1831
+ if (myGdalScanData == NULL )
1832
+ {
1833
+ return ;
1834
+ }
1835
+
1797
1836
QgsColorTable *myColorTable = &(myRasterBandStats.colorTable );
1798
1837
1799
1838
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
@@ -1898,6 +1937,13 @@ void QgsRasterLayer::drawPalettedSingleBandPseudoColor(QPainter * theQPainter, Q
1898
1937
GDALRasterBand *myGdalBand = gdalDataset->GetRasterBand (theBandNoInt);
1899
1938
GDALDataType myDataType = myGdalBand->GetRasterDataType ();
1900
1939
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
1940
+
1941
+ /* Check for out of memory error */
1942
+ if (myGdalScanData == NULL )
1943
+ {
1944
+ return ;
1945
+ }
1946
+
1901
1947
QgsColorTable *myColorTable = &(myRasterBandStats.colorTable );
1902
1948
1903
1949
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
@@ -2134,6 +2180,13 @@ void QgsRasterLayer::drawPalettedMultiBandColor(QPainter * theQPainter, QgsRaste
2134
2180
GDALRasterBand *myGdalBand = gdalDataset->GetRasterBand (theBandNoInt);
2135
2181
GDALDataType myDataType = myGdalBand->GetRasterDataType ();
2136
2182
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
2183
+
2184
+ /* Check for out of memory error */
2185
+ if (myGdalScanData == NULL )
2186
+ {
2187
+ return ;
2188
+ }
2189
+
2137
2190
QgsColorTable *myColorTable = colorTable ( theBandNoInt );
2138
2191
2139
2192
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
@@ -2272,6 +2325,16 @@ void QgsRasterLayer::drawMultiBandColor(QPainter * theQPainter, QgsRasterViewPor
2272
2325
void *myGdalGreenData = readData ( myGdalGreenBand, theRasterViewPort );
2273
2326
void *myGdalBlueData = readData ( myGdalBlueBand, theRasterViewPort );
2274
2327
2328
+ /* Check for out of memory error */
2329
+ if (myGdalRedData == NULL || myGdalGreenData == NULL || myGdalBlueData == NULL )
2330
+ {
2331
+ // Safe to free NULL-pointer */
2332
+ VSIFree (myGdalRedData);
2333
+ VSIFree (myGdalGreenData);
2334
+ VSIFree (myGdalBlueData);
2335
+ return ;
2336
+ }
2337
+
2275
2338
bool haveTransparencyBand (false );
2276
2339
GDALRasterBand *myGdalTransparentBand;
2277
2340
GDALDataType myTransparentType;
@@ -2284,6 +2347,14 @@ void QgsRasterLayer::drawMultiBandColor(QPainter * theQPainter, QgsRasterViewPor
2284
2347
myGdalTransparentBand = gdalDataset->GetRasterBand (myTransparentBandNoInt);
2285
2348
myTransparentType = myGdalTransparentBand->GetRasterDataType ();
2286
2349
myGdalTransparentData = readData ( myGdalTransparentBand, theRasterViewPort );
2350
+ if (myGdalTransparentData == NULL )
2351
+ {
2352
+ // Safe to free NULL-pointer */
2353
+ VSIFree (myGdalRedData);
2354
+ VSIFree (myGdalGreenData);
2355
+ VSIFree (myGdalBlueData);
2356
+ return ;
2357
+ }
2287
2358
}
2288
2359
2289
2360
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
@@ -4377,8 +4448,6 @@ void *QgsRasterLayer::readData ( GDALRasterBand *gdalBand, QgsRasterViewPort *vi
4377
4448
GDALDataType type = gdalBand->GetRasterDataType ();
4378
4449
int size = GDALGetDataTypeSize ( type ) / 8 ;
4379
4450
4380
- void *data = CPLMalloc ( size * viewPort->drawableAreaXDimInt * viewPort->drawableAreaYDimInt );
4381
-
4382
4451
QgsDebugMsg (" QgsRasterLayer::readData: calling RasterIO with " +\
4383
4452
QString (" , source NW corner: " ) + QString::number (viewPort->rectXOffsetInt )+\
4384
4453
" , " + QString::number (viewPort->rectYOffsetInt )+\
@@ -4387,20 +4456,29 @@ void *QgsRasterLayer::readData ( GDALRasterBand *gdalBand, QgsRasterViewPort *vi
4387
4456
" , dest size: " + QString::number (viewPort->drawableAreaXDimInt )+\
4388
4457
" , " + QString::number (viewPort->drawableAreaYDimInt ));
4389
4458
4390
- CPLErr myErr = gdalBand->RasterIO ( GF_Read,
4391
- viewPort->rectXOffsetInt ,
4392
- viewPort->rectYOffsetInt ,
4393
- viewPort->clippedWidthInt ,
4394
- viewPort->clippedHeightInt ,
4395
- data,
4396
- viewPort->drawableAreaXDimInt ,
4397
- viewPort->drawableAreaYDimInt ,
4398
- type, 0 , 0 );
4399
- if (myErr != CPLE_None)
4459
+ void *data = VSIMalloc ( size * viewPort->drawableAreaXDimInt * viewPort->drawableAreaYDimInt );
4460
+
4461
+ /* Abort if out of memory */
4462
+ if (data == NULL )
4400
4463
{
4401
- QgsLogger::warning (" RaterIO error: " + QString (CPLGetLastErrorMsg ()));
4464
+ QgsDebugMsg (" Layer " + this ->name () + " couldn't allocate enough memory. Ignoring" );
4465
+ }
4466
+ else
4467
+ {
4468
+ CPLErr myErr = gdalBand->RasterIO ( GF_Read,
4469
+ viewPort->rectXOffsetInt ,
4470
+ viewPort->rectYOffsetInt ,
4471
+ viewPort->clippedWidthInt ,
4472
+ viewPort->clippedHeightInt ,
4473
+ data,
4474
+ viewPort->drawableAreaXDimInt ,
4475
+ viewPort->drawableAreaYDimInt ,
4476
+ type, 0 , 0 );
4477
+ if (myErr != CPLE_None)
4478
+ {
4479
+ QgsLogger::warning (" RaterIO error: " + QString (CPLGetLastErrorMsg ()));
4480
+ }
4402
4481
}
4403
-
4404
4482
return data;
4405
4483
}
4406
4484
0 commit comments