@@ -1181,6 +1181,7 @@ __FUNCTION__, __LINE__);
1181
1181
myRasterViewPort->drawableAreaYDimInt =
1182
1182
abs (static_cast <int > (myRasterViewPort->clippedHeightInt / theQgsMapToPixel->mapUnitsPerPixel () * adfGeoTransform[5 ]));
1183
1183
1184
+
1184
1185
#ifdef QGISDEBUG
1185
1186
QgsLogger::debug (" QgsRasterLayer::draw: mapUnitsPerPixel" , theQgsMapToPixel->mapUnitsPerPixel (), 1 , __FILE__,\
1186
1187
__FUNCTION__, __LINE__);
@@ -1240,7 +1241,15 @@ __FUNCTION__, __LINE__);
1240
1241
1241
1242
// /\/\/\ - added to handle zoomed-in rasters
1242
1243
1243
-
1244
+ if ((myRasterViewPort->drawableAreaXDimInt ) > 4000 && (myRasterViewPort->drawableAreaYDimInt > 4000 ))
1245
+ {
1246
+ // We have scale one raster pixel to more than 4000 screen pixels. What's the point of showing this layer?
1247
+ // Instead, we just stop displaying the layer. Prevents allocating the entire world of memory for showing
1248
+ // The pixel in all its glory.
1249
+ QgsDebugMsg (" Too zoomed out! Raster will not display" );
1250
+ return TRUE ;
1251
+ }
1252
+
1244
1253
// Provider mode: See if a provider key is specified, and if so use the provider instead
1245
1254
1246
1255
QgsDebugMsg (" QgsRasterLayer::draw: Checking for provider key." );
@@ -1337,7 +1346,6 @@ void QgsRasterLayer::draw(QPainter * theQPainter,
1337
1346
// procedure to use :
1338
1347
//
1339
1348
1340
-
1341
1349
switch (drawingStyle)
1342
1350
{
1343
1351
// a "Gray" or "Undefined" layer drawn as a range of gray colors
@@ -1445,7 +1453,7 @@ void QgsRasterLayer::draw(QPainter * theQPainter,
1445
1453
break ;
1446
1454
1447
1455
}
1448
-
1456
+
1449
1457
// see if debug info is wanted
1450
1458
if (showDebugOverlayFlag)
1451
1459
{
@@ -1463,6 +1471,12 @@ void QgsRasterLayer::drawSingleBandGray(QPainter * theQPainter, QgsRasterViewPor
1463
1471
GDALDataType myDataType = myGdalBand->GetRasterDataType ();
1464
1472
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
1465
1473
1474
+ /* Check for out of memory error */
1475
+ if (myGdalScanData == NULL )
1476
+ {
1477
+ return ;
1478
+ }
1479
+
1466
1480
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
1467
1481
// myQImage.fill(0);
1468
1482
myQImage.setAlphaBuffer (true );
@@ -1500,6 +1514,8 @@ void QgsRasterLayer::drawSingleBandGray(QPainter * theQPainter, QgsRasterViewPor
1500
1514
myQImage.setPixel (myRowInt, myColumnInt, qRgba (myGrayValInt, myGrayValInt, myGrayValInt, transparencyLevelInt));
1501
1515
}
1502
1516
}
1517
+
1518
+ /* TODO: Should readData be freed here? */
1503
1519
1504
1520
// render any inline filters
1505
1521
filterLayer (&myQImage);
@@ -1555,6 +1571,12 @@ void QgsRasterLayer::drawSingleBandPseudoColor(QPainter * theQPainter,
1555
1571
GDALDataType myDataType = myGdalBand->GetRasterDataType ();
1556
1572
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
1557
1573
1574
+ /* Check for out of memory error */
1575
+ if (myGdalScanData == NULL )
1576
+ {
1577
+ return ;
1578
+ }
1579
+
1558
1580
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
1559
1581
// myQImage.fill(0);
1560
1582
myQImage.setAlphaBuffer (true );
@@ -1772,6 +1794,13 @@ void QgsRasterLayer::drawPalettedSingleBandColor(QPainter * theQPainter, QgsRast
1772
1794
GDALRasterBand *myGdalBand = gdalDataset->GetRasterBand (theBandNoInt);
1773
1795
GDALDataType myDataType = myGdalBand->GetRasterDataType ();
1774
1796
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
1797
+
1798
+ /* Check for out of memory error */
1799
+ if (myGdalScanData == NULL )
1800
+ {
1801
+ return ;
1802
+ }
1803
+
1775
1804
QgsColorTable *myColorTable = colorTable ( theBandNoInt );
1776
1805
1777
1806
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
@@ -1802,6 +1831,9 @@ void QgsRasterLayer::drawPalettedSingleBandColor(QPainter * theQPainter, QgsRast
1802
1831
myQImage.setPixel (myRowInt, myColumnInt, qRgba (c1, c2, c3, transparencyLevelInt));
1803
1832
}
1804
1833
}
1834
+
1835
+ /* TODO: Should readData be freed here? */
1836
+
1805
1837
// render any inline filters
1806
1838
filterLayer (&myQImage);
1807
1839
@@ -1862,6 +1894,13 @@ void QgsRasterLayer::drawPalettedSingleBandGray(QPainter * theQPainter, QgsRaste
1862
1894
GDALRasterBand *myGdalBand = gdalDataset->GetRasterBand (theBandNoInt);
1863
1895
GDALDataType myDataType = myGdalBand->GetRasterDataType ();
1864
1896
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
1897
+
1898
+ /* Check for out of memory error */
1899
+ if (myGdalScanData == NULL )
1900
+ {
1901
+ return ;
1902
+ }
1903
+
1865
1904
QgsColorTable *myColorTable = &(myRasterBandStats.colorTable );
1866
1905
1867
1906
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
@@ -1966,6 +2005,13 @@ void QgsRasterLayer::drawPalettedSingleBandPseudoColor(QPainter * theQPainter, Q
1966
2005
GDALRasterBand *myGdalBand = gdalDataset->GetRasterBand (theBandNoInt);
1967
2006
GDALDataType myDataType = myGdalBand->GetRasterDataType ();
1968
2007
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
2008
+
2009
+ /* Check for out of memory error */
2010
+ if (myGdalScanData == NULL )
2011
+ {
2012
+ return ;
2013
+ }
2014
+
1969
2015
QgsColorTable *myColorTable = &(myRasterBandStats.colorTable );
1970
2016
1971
2017
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
@@ -2202,6 +2248,13 @@ void QgsRasterLayer::drawPalettedMultiBandColor(QPainter * theQPainter, QgsRaste
2202
2248
GDALRasterBand *myGdalBand = gdalDataset->GetRasterBand (theBandNoInt);
2203
2249
GDALDataType myDataType = myGdalBand->GetRasterDataType ();
2204
2250
void *myGdalScanData = readData ( myGdalBand, theRasterViewPort );
2251
+
2252
+ /* Check for out of memory error */
2253
+ if (myGdalScanData == NULL )
2254
+ {
2255
+ return ;
2256
+ }
2257
+
2205
2258
QgsColorTable *myColorTable = colorTable ( theBandNoInt );
2206
2259
2207
2260
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
@@ -2340,6 +2393,16 @@ void QgsRasterLayer::drawMultiBandColor(QPainter * theQPainter, QgsRasterViewPor
2340
2393
void *myGdalGreenData = readData ( myGdalGreenBand, theRasterViewPort );
2341
2394
void *myGdalBlueData = readData ( myGdalBlueBand, theRasterViewPort );
2342
2395
2396
+ /* Check for out of memory error */
2397
+ if (myGdalRedData == NULL || myGdalGreenData == NULL || myGdalBlueData == NULL )
2398
+ {
2399
+ // Safe to free NULL-pointer */
2400
+ VSIFree (myGdalRedData);
2401
+ VSIFree (myGdalGreenData);
2402
+ VSIFree (myGdalBlueData);
2403
+ return ;
2404
+ }
2405
+
2343
2406
bool haveTransparencyBand (false );
2344
2407
GDALRasterBand *myGdalTransparentBand;
2345
2408
GDALDataType myTransparentType;
@@ -2352,6 +2415,14 @@ void QgsRasterLayer::drawMultiBandColor(QPainter * theQPainter, QgsRasterViewPor
2352
2415
myGdalTransparentBand = gdalDataset->GetRasterBand (myTransparentBandNoInt);
2353
2416
myTransparentType = myGdalTransparentBand->GetRasterDataType ();
2354
2417
myGdalTransparentData = readData ( myGdalTransparentBand, theRasterViewPort );
2418
+ if (myGdalTransparentData == NULL )
2419
+ {
2420
+ // Safe to free NULL-pointer */
2421
+ VSIFree (myGdalRedData);
2422
+ VSIFree (myGdalGreenData);
2423
+ VSIFree (myGdalBlueData);
2424
+ return ;
2425
+ }
2355
2426
}
2356
2427
2357
2428
QImage myQImage = QImage (theRasterViewPort->drawableAreaXDimInt , theRasterViewPort->drawableAreaYDimInt , 32 );
@@ -4410,8 +4481,6 @@ void *QgsRasterLayer::readData ( GDALRasterBand *gdalBand, QgsRasterViewPort *vi
4410
4481
GDALDataType type = gdalBand->GetRasterDataType ();
4411
4482
int size = GDALGetDataTypeSize ( type ) / 8 ;
4412
4483
4413
- void *data = CPLMalloc ( size * viewPort->drawableAreaXDimInt * viewPort->drawableAreaYDimInt );
4414
-
4415
4484
QgsDebugMsg (" QgsRasterLayer::readData: calling RasterIO with " +\
4416
4485
QString (" , source NW corner: " ) + QString::number (viewPort->rectXOffsetInt )+\
4417
4486
" , " + QString::number (viewPort->rectYOffsetInt )+\
@@ -4420,16 +4489,26 @@ void *QgsRasterLayer::readData ( GDALRasterBand *gdalBand, QgsRasterViewPort *vi
4420
4489
" , dest size: " + QString::number (viewPort->drawableAreaXDimInt )+\
4421
4490
" , " + QString::number (viewPort->drawableAreaYDimInt ));
4422
4491
4423
- CPLErr myErr = gdalBand->RasterIO ( GF_Read,
4424
- viewPort->rectXOffsetInt ,
4425
- viewPort->rectYOffsetInt ,
4426
- viewPort->clippedWidthInt ,
4427
- viewPort->clippedHeightInt ,
4428
- data,
4429
- viewPort->drawableAreaXDimInt ,
4430
- viewPort->drawableAreaYDimInt ,
4431
- type, 0 , 0 );
4492
+ void *data = VSIMalloc ( size * viewPort->drawableAreaXDimInt * viewPort->drawableAreaYDimInt );
4493
+
4494
+ /* Abort if out of memory */
4495
+ if (data == NULL )
4496
+ {
4497
+ QgsDebugMsg (" Layer " + this ->name () + " couldn't allocate enough memory. Ignoring" );
4498
+ }
4499
+ else
4500
+ {
4501
+ CPLErr myErr = gdalBand->RasterIO ( GF_Read,
4502
+ viewPort->rectXOffsetInt ,
4503
+ viewPort->rectYOffsetInt ,
4504
+ viewPort->clippedWidthInt ,
4505
+ viewPort->clippedHeightInt ,
4506
+ data,
4507
+ viewPort->drawableAreaXDimInt ,
4508
+ viewPort->drawableAreaYDimInt ,
4509
+ type, 0 , 0 );
4432
4510
4511
+ }
4433
4512
return data;
4434
4513
}
4435
4514
0 commit comments