Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
VSI/CPLMalloc/Free replaced by QgsMalloc/Free where feasible
  • Loading branch information
blazek committed Oct 16, 2012
1 parent 6b55ee4 commit 5a6401c
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 197 deletions.
2 changes: 1 addition & 1 deletion python/core/raster/qgsrasteriterator.sip
Expand Up @@ -11,7 +11,7 @@ class QgsRasterIterator
int currentRow;
int nCols;
int nRows;
void* data; //data (can be in oversampled/undersampled resolution)
QgsRasterBlock *block;
QgsRasterProjector* prj; //raster projector (or 0 if no reprojection is done)
};

Expand Down
8 changes: 0 additions & 8 deletions src/core/raster/qgspalettedrasterrenderer.cpp
Expand Up @@ -140,14 +140,6 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
alphaBlock = inputBlock;
}

//create image
//QImage img( width, height, QImage::Format_ARGB32_Premultiplied );
//if ( img.isNull() )
//{
//QgsDebugMsg( "Could not create QImage" );
//VSIFree( rasterData );
//return 0;
//}
if ( !outputBlock->reset( QgsRasterBlock::ARGB32_Premultiplied, width, height ) )
{
delete inputBlock;
Expand Down
165 changes: 4 additions & 161 deletions src/core/raster/qgsrasterdataprovider.cpp
Expand Up @@ -383,163 +383,6 @@ bool QgsRasterDataProvider::hasPyramids()
return false;
}

#if 0
QgsRasterBandStats QgsRasterDataProvider::bandStatistics( int theBandNo )
{
// TODO: cache stats here in provider
double myNoDataValue = noDataValue();
QgsRasterBandStats myRasterBandStats;
myRasterBandStats.elementCount = 0; // because we'll be counting only VALID pixels later
myRasterBandStats.bandName = generateBandName( theBandNo );
myRasterBandStats.bandNumber = theBandNo;

int myDataType = dataType( theBandNo );

if ( xBlockSize() == 0 || yBlockSize() == 0 )
{
QgsDebugMsg( "Cannot collect statistics (raster size or block size) are unknown" );
return QgsRasterBandStats(); //invalid raster band stats
}

int myNXBlocks = ( xSize() + xBlockSize() - 1 ) / xBlockSize();
int myNYBlocks = ( ySize() + yBlockSize() - 1 ) / yBlockSize();

void *myData = CPLMalloc( xBlockSize() * yBlockSize() * ( dataTypeSize( theBandNo ) ) );

// unfortunately we need to make two passes through the data to calculate stddev
bool myFirstIterationFlag = true;

int myBandXSize = xSize();
int myBandYSize = ySize();
int maxCount = xSize() * ySize();
for ( int iYBlock = 0; iYBlock < myNYBlocks; iYBlock++ )
{
for ( int iXBlock = 0; iXBlock < myNXBlocks; iXBlock++ )
{
int nXValid, nYValid;
readBlock( theBandNo, iXBlock, iYBlock, myData );

// Compute the portion of the block that is valid
// for partial edge blocks.
if (( iXBlock + 1 ) * xBlockSize() > myBandXSize )
nXValid = myBandXSize - iXBlock * xBlockSize();
else
nXValid = xBlockSize();

if (( iYBlock + 1 ) * yBlockSize() > myBandYSize )
nYValid = myBandYSize - iYBlock * yBlockSize();
else
nYValid = yBlockSize();

// Collect the histogram counts.
for ( int iY = 0; iY < nYValid; iY++ )
{
for ( int iX = 0; iX < nXValid; iX++ )
{
double myValue = readValue( myData, myDataType, iX + ( iY * xBlockSize() ) );
//QgsDebugMsg ( QString ( "%1 %2 value %3" ).arg (iX).arg(iY).arg( myValue ) );

if ( mValidNoDataValue &&
(( std::isnan( myNoDataValue ) && std::isnan( myValue ) ) || qAbs( myValue - myNoDataValue ) <= TINY_VALUE ) )
{
continue; // NULL
}

myRasterBandStats.sum += myValue;
// sum can easily run out of limits
myRasterBandStats.mean += myValue / maxCount;
++myRasterBandStats.elementCount;
//only use this element if we have a non null element
if ( myFirstIterationFlag )
{
//this is the first iteration so initialise vars
myFirstIterationFlag = false;
myRasterBandStats.minimumValue = myValue;
myRasterBandStats.maximumValue = myValue;
} //end of true part for first iteration check
else
{
//this is done for all subsequent iterations
if ( myValue < myRasterBandStats.minimumValue )
{
myRasterBandStats.minimumValue = myValue;
}
if ( myValue > myRasterBandStats.maximumValue )
{
myRasterBandStats.maximumValue = myValue;
}
} //end of false part for first iteration check
}
}
} //end of column wise loop
} //end of row wise loop


//end of first pass through data now calculate the range
myRasterBandStats.range = myRasterBandStats.maximumValue - myRasterBandStats.minimumValue;
//calculate the mean
//myRasterBandStats.mean = myRasterBandStats.sum / myRasterBandStats.elementCount;
myRasterBandStats.mean = maxCount * ( myRasterBandStats.mean / myRasterBandStats.elementCount );

//for the second pass we will get the sum of the squares / mean
for ( int iYBlock = 0; iYBlock < myNYBlocks; iYBlock++ )
{
for ( int iXBlock = 0; iXBlock < myNXBlocks; iXBlock++ )
{
int nXValid, nYValid;

readBlock( theBandNo, iXBlock, iYBlock, myData );

// Compute the portion of the block that is valid
// for partial edge blocks.
if (( iXBlock + 1 ) * xBlockSize() > myBandXSize )
nXValid = myBandXSize - iXBlock * xBlockSize();
else
nXValid = xBlockSize();

if (( iYBlock + 1 ) * yBlockSize() > myBandYSize )
nYValid = myBandYSize - iYBlock * yBlockSize();
else
nYValid = yBlockSize();

// Collect the histogram counts.
for ( int iY = 0; iY < nYValid; iY++ )
{
for ( int iX = 0; iX < nXValid; iX++ )
{
double myValue = readValue( myData, myDataType, iX + ( iY * xBlockSize() ) );
//QgsDebugMsg ( "myValue = " + QString::number(myValue) );

if ( mValidNoDataValue &&
(( std::isnan( myNoDataValue ) && std::isnan( myValue ) ) || qAbs( myValue - myNoDataValue ) <= TINY_VALUE ) )
{
continue; // NULL
}

myRasterBandStats.sumOfSquares += static_cast < double >
( qPow( myValue - myRasterBandStats.mean, 2 ) );
}
}
} //end of column wise loop
} //end of row wise loop

//divide result by sample size - 1 and get square root to get stdev
myRasterBandStats.stdDev = static_cast < double >( sqrt( myRasterBandStats.sumOfSquares / ( myRasterBandStats.elementCount - 1 ) ) );

QgsDebugMsg( "************ STATS **************" );
QgsDebugMsg( QString( "VALID NODATA %1" ).arg( mValidNoDataValue ) );
QgsDebugMsg( QString( "MIN %1" ).arg( myRasterBandStats.minimumValue ) );
QgsDebugMsg( QString( "MAX %1" ).arg( myRasterBandStats.maximumValue ) );
QgsDebugMsg( QString( "RANGE %1" ).arg( myRasterBandStats.range ) );
QgsDebugMsg( QString( "MEAN %1" ).arg( myRasterBandStats.mean ) );
QgsDebugMsg( QString( "STDDEV %1" ).arg( myRasterBandStats.stdDev ) );

CPLFree( myData );
myRasterBandStats.statsGathered = true;
return myRasterBandStats;
}
#endif

void QgsRasterDataProvider::initStatistics( QgsRasterBandStats &theStatistics,
int theBandNo,
int theStats,
Expand Down Expand Up @@ -662,7 +505,7 @@ QgsRasterBandStats QgsRasterDataProvider::bandStatistics( int theBandNo,
int myNXBlocks = ( myWidth + myXBlockSize - 1 ) / myXBlockSize;
int myNYBlocks = ( myHeight + myYBlockSize - 1 ) / myYBlockSize;

void *myData = CPLMalloc( myXBlockSize * myYBlockSize * ( QgsRasterBlock::typeSize( dataType( theBandNo ) ) ) );
void *myData = QgsMalloc( myXBlockSize * myYBlockSize * ( QgsRasterBlock::typeSize( dataType( theBandNo ) ) ) );

double myXRes = myExtent.width() / myWidth;
double myYRes = myExtent.height() / myHeight;
Expand Down Expand Up @@ -754,7 +597,7 @@ QgsRasterBandStats QgsRasterDataProvider::bandStatistics( int theBandNo,
QgsDebugMsg( QString( "MEAN %1" ).arg( myRasterBandStats.mean ) );
QgsDebugMsg( QString( "STDDEV %1" ).arg( myRasterBandStats.stdDev ) );

CPLFree( myData );
QgsFree( myData );

myRasterBandStats.statsGathered = QgsRasterBandStats::All;
mStatistics.append( myRasterBandStats );
Expand Down Expand Up @@ -938,7 +781,7 @@ QgsRasterHistogram QgsRasterDataProvider::histogram( int theBandNo,
int myNXBlocks = ( myWidth + myXBlockSize - 1 ) / myXBlockSize;
int myNYBlocks = ( myHeight + myYBlockSize - 1 ) / myYBlockSize;

void *myData = CPLMalloc( myXBlockSize * myYBlockSize * ( QgsRasterBlock::typeSize( dataType( theBandNo ) ) ) );
void *myData = QgsMalloc( myXBlockSize * myYBlockSize * ( QgsRasterBlock::typeSize( dataType( theBandNo ) ) ) );

double myXRes = myExtent.width() / myWidth;
double myYRes = myExtent.height() / myHeight;
Expand Down Expand Up @@ -1004,7 +847,7 @@ QgsRasterHistogram QgsRasterDataProvider::histogram( int theBandNo,
}
}

CPLFree( myData );
QgsFree( myData );

myHistogram.valid = true;
mHistograms.append( myHistogram );
Expand Down
16 changes: 8 additions & 8 deletions src/core/raster/qgsrasterfilewriter.cpp
Expand Up @@ -299,7 +299,7 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster(
{
iter->startRasterRead( i, nCols, nRows, outputExtent );
// TODO: no need to alloc memory, change to readBlock() returning the allocated block
//blockList.push_back( VSIMalloc( dataTypeSize * mMaxTileWidth * mMaxTileHeight ) );
//blockList.push_back( QgsMalloc( dataTypeSize * mMaxTileWidth * mMaxTileHeight ) );
blockList.push_back( 0 );
// TODO - fix segfault here when using tiles+vrt (reported by Etienne)
destProvider->setNoDataValue( i, destNoDataValueList.value( i - 1 ) );
Expand Down Expand Up @@ -373,7 +373,7 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster(
// TODO: this conversion should go to QgsRasterDataProvider::write with additional input data type param
//void *destData = QgsRasterBlock::convert( blockList[i-1], srcProvider->dataType( i ), destDataType, iterCols * iterRows );
//destBlockList.push_back( destData );
//CPLFree( blockList[i-1] );
//QgsFree( blockList[i-1] );
blockList[i-1]->convert( destDataType );
destBlockList.push_back( blockList[i-1] );
}
Expand Down Expand Up @@ -437,11 +437,11 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
iter->setMaximumTileWidth( mMaxTileWidth );
iter->setMaximumTileHeight( mMaxTileHeight );

//void* data = VSIMalloc( QgsRasterBlock::typeSize( inputDataType ) * mMaxTileWidth * mMaxTileHeight );
void* redData = VSIMalloc( mMaxTileWidth * mMaxTileHeight );
void* greenData = VSIMalloc( mMaxTileWidth * mMaxTileHeight );
void* blueData = VSIMalloc( mMaxTileWidth * mMaxTileHeight );
void* alphaData = VSIMalloc( mMaxTileWidth * mMaxTileHeight );
//void* data = QgsMalloc( QgsRasterBlock::typeSize( inputDataType ) * mMaxTileWidth * mMaxTileHeight );
void* redData = QgsMalloc( mMaxTileWidth * mMaxTileHeight );
void* greenData = QgsMalloc( mMaxTileWidth * mMaxTileHeight );
void* blueData = QgsMalloc( mMaxTileWidth * mMaxTileHeight );
void* alphaData = QgsMalloc( mMaxTileWidth * mMaxTileHeight );
QgsRectangle mapRect;
int iterLeft = 0, iterTop = 0, iterCols = 0, iterRows = 0;
int fileIndex = 0;
Expand Down Expand Up @@ -547,7 +547,7 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
}

delete destProvider;
CPLFree( redData ); CPLFree( greenData ); CPLFree( blueData ); CPLFree( alphaData );
QgsFree( redData ); QgsFree( greenData ); QgsFree( blueData ); QgsFree( alphaData );

if ( progressDialog )
{
Expand Down
9 changes: 4 additions & 5 deletions src/core/raster/qgsrasteriterator.cpp
Expand Up @@ -44,7 +44,7 @@ void QgsRasterIterator::startRasterRead( int bandNumber, int nCols, int nRows, c
pInfo.nRows = nRows;
pInfo.currentCol = 0;
pInfo.currentRow = 0;
pInfo.data = 0;
pInfo.block = 0;
pInfo.prj = 0;
mRasterPartInfos.insert( bandNumber, pInfo );
}
Expand Down Expand Up @@ -73,8 +73,8 @@ bool QgsRasterIterator::readNextRasterPart( int bandNumber,
}

//remove last data block
// TODO: data are released somewhere else (check)
//free( pInfo.data );
// TODO: block is released somewhere else (check)
//delete pInfo.block;
pInfo.block = 0;
delete pInfo.prj;
pInfo.prj = 0;
Expand Down Expand Up @@ -129,8 +129,7 @@ void QgsRasterIterator::removePartInfo( int bandNumber )
if ( partIt != mRasterPartInfos.end() )
{
RasterPartInfo& pInfo = partIt.value();
//CPLFree( pInfo.data );
free( pInfo.data );
delete pInfo.block;
delete pInfo.prj;
mRasterPartInfos.remove( bandNumber );
}
Expand Down
1 change: 0 additions & 1 deletion src/core/raster/qgsrasteriterator.h
Expand Up @@ -37,7 +37,6 @@ class CORE_EXPORT QgsRasterIterator
int currentRow;
int nCols;
int nRows;
void* data; //data (can be in oversampled/undersampled resolution)
QgsRasterBlock *block;
QgsRasterProjector* prj; //raster projector (or 0 if no reprojection is done)
};
Expand Down
2 changes: 1 addition & 1 deletion src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -2830,7 +2830,7 @@ void *QgsRasterLayer::readData( int bandNo, QgsRasterViewPort *viewPort )
", dest size: " + QString::number( viewPort->drawableAreaXDim ) +
", " + QString::number( viewPort->drawableAreaYDim ) );
#endif
void *data = VSIMalloc( size * viewPort->drawableAreaXDim * viewPort->drawableAreaYDim );
void *data = QgsMalloc( size * viewPort->drawableAreaXDim * viewPort->drawableAreaYDim );

/* Abort if out of memory */
if ( data == NULL )
Expand Down
20 changes: 10 additions & 10 deletions src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -643,10 +643,10 @@ void QgsGdalProvider::readBlock( int theBandNo, QgsRectangle const & theExtent,

myWarpOptions->nBandCount = 1;
myWarpOptions->panSrcBands =
( int * ) CPLMalloc( sizeof( int ) * myWarpOptions->nBandCount );
( int * ) QgsMalloc( sizeof( int ) * myWarpOptions->nBandCount );
myWarpOptions->panSrcBands[0] = theBandNo;
myWarpOptions->panDstBands =
( int * ) CPLMalloc( sizeof( int ) * myWarpOptions->nBandCount );
( int * ) QgsMalloc( sizeof( int ) * myWarpOptions->nBandCount );
myWarpOptions->panDstBands[0] = 1;

// TODO move here progressCallback and use it
Expand Down Expand Up @@ -682,8 +682,8 @@ void QgsGdalProvider::readBlock( int theBandNo, QgsRectangle const & theExtent,
//CPLAssert( myWarpOptions->pTransformerArg != NULL );
myWarpOptions->pfnTransformer = GDALGenImgProjTransform;

myWarpOptions->padfDstNoDataReal = ( double * ) CPLMalloc( myWarpOptions->nBandCount * sizeof( double ) );
myWarpOptions->padfDstNoDataImag = ( double * ) CPLMalloc( myWarpOptions->nBandCount * sizeof( double ) );
myWarpOptions->padfDstNoDataReal = ( double * ) QgsMalloc( myWarpOptions->nBandCount * sizeof( double ) );
myWarpOptions->padfDstNoDataImag = ( double * ) QgsMalloc( myWarpOptions->nBandCount * sizeof( double ) );

myWarpOptions->padfDstNoDataReal[0] = mNoDataValue[theBandNo-1];
myWarpOptions->padfDstNoDataImag[0] = 0.0;
Expand Down Expand Up @@ -835,7 +835,7 @@ QMap<int, void *> QgsGdalProvider::identify( const QgsPoint & point )
// Outside the raster
for ( int i = 1; i <= GDALGetRasterCount( mGdalDataset ); i++ )
{
void * data = VSIMalloc( dataTypeSize( i ) );
void * data = QgsMalloc( dataTypeSize( i ) );
QgsRasterBlock::writeValue( data, dataType( i ), 0, noDataValue( i ) );
results.insert( i, data );
}
Expand Down Expand Up @@ -885,7 +885,7 @@ QMap<int, void *> QgsGdalProvider::identify( const QgsPoint & point )
}
#endif
int typeSize = dataTypeSize( i );
void * tmpData = VSIMalloc( typeSize * width * height );
void * tmpData = QgsMalloc( typeSize * width * height );

CPLErr err = GDALRasterIO( gdalBand, GF_Read, col, row, width, height,
tmpData, width, height,
Expand All @@ -895,11 +895,11 @@ QMap<int, void *> QgsGdalProvider::identify( const QgsPoint & point )
{
QgsLogger::warning( "RasterIO error: " + QString::fromUtf8( CPLGetLastErrorMsg() ) );
}
void * data = VSIMalloc( typeSize );
void * data = QgsMalloc( typeSize );
memcpy( data, ( void* )(( char* )tmpData + ( r*width + c )*typeSize ), typeSize );
results.insert( i, data );

CPLFree( tmpData );
QgsFree( tmpData );
}
}

Expand Down Expand Up @@ -1127,7 +1127,7 @@ bool QgsGdalProvider::hasHistogram( int theBandNo,
NULL, NULL );

if ( myHistogramArray )
VSIFree( myHistogramArray );
VSIFree( myHistogramArray ); // use VSIFree because allocated by GDAL

// if there was any error/warning assume the histogram is not valid or non-existent
if ( myError != CE_None )
Expand Down Expand Up @@ -2507,7 +2507,7 @@ QGISEXTERN QString helpCreationOptionsFormat( QString format )
if ( psCOL )
CPLDestroyXMLNode( psCOL );
if ( pszFormattedXML )
CPLFree( pszFormattedXML );
QgsFree( pszFormattedXML );
}
return message;
}
Expand Down

0 comments on commit 5a6401c

Please sign in to comment.