Skip to content

Commit 1f65f71

Browse files
committedApr 15, 2013
raster cleanup
1 parent 2ef7e49 commit 1f65f71

File tree

6 files changed

+70
-94
lines changed

6 files changed

+70
-94
lines changed
 

‎python/core/raster/qgsrasterblock.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class QgsRasterBlock
1818

1919
int typeSize( int dataType ) const;
2020

21-
int dataTypeSize( int bandNo ) const;
21+
int dataTypeSize() const;
2222

2323
/** Returns true if data type is numeric */
2424
bool typeIsNumeric( QGis::DataType type ) const;

‎src/core/raster/qgspalettedrasterrenderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ QgsRasterBlock * QgsPalettedRasterRenderer::block( int bandNo, QgsRectangle con
210210

211211
//use direct data access instead of QgsRasterBlock::setValue
212212
//because of performance
213-
unsigned int* outputData = ( unsigned int* )( outputBlock->data() );
213+
unsigned int* outputData = ( unsigned int* )( outputBlock->bits() );
214214

215215
size_t rasterSize = ( size_t )width * height;
216216
for ( size_t i = 0; i < rasterSize; ++i )

‎src/core/raster/qgsrasterblock.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,20 @@ char * QgsRasterBlock::bits( int row, int column )
636636
return bits(( size_t )row*mWidth + column );
637637
}
638638

639+
char * QgsRasterBlock::bits()
640+
{
641+
if ( mData )
642+
{
643+
return ( char* )mData;
644+
}
645+
if ( mImage && mImage->bits() )
646+
{
647+
return ( char* )( mImage->bits() );
648+
}
649+
650+
return 0;
651+
}
652+
639653
bool QgsRasterBlock::convert( QGis::DataType destDataType )
640654
{
641655
if ( isEmpty() ) return false;
@@ -714,22 +728,6 @@ bool QgsRasterBlock::setImage( const QImage * image )
714728
return true;
715729
}
716730

717-
// To give to an image preallocated memory is the only way to avoid memcpy
718-
// when we want to keep data but delete QImage
719-
QImage * QgsRasterBlock::createImage( int width, int height, QImage::Format format )
720-
{
721-
// Qt has its own internal function depthForFormat(), unfortunately it is not public
722-
723-
QImage img( 1, 1, format );
724-
725-
// We ignore QImage::Format_Mono and QImage::Format_MonoLSB ( depth 1)
726-
int size = width * height * img.bytesPerLine();
727-
uchar * data = ( uchar * ) malloc( size );
728-
return new QImage( data, width, height, format );
729-
}
730-
731-
732-
733731
QString QgsRasterBlock::printValue( double value )
734732
{
735733
/*

‎src/core/raster/qgsrasterblock.h

Lines changed: 51 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,8 @@ class CORE_EXPORT QgsRasterBlock
122122
}
123123

124124
// Data type in bytes
125-
int dataTypeSize( int bandNo ) const
125+
int dataTypeSize( ) const
126126
{
127-
Q_UNUSED( bandNo );
128127
return typeSize( mDataType );
129128
}
130129

@@ -150,31 +149,26 @@ class CORE_EXPORT QgsRasterBlock
150149
* @return true if the block may contain no data */
151150
bool hasNoData() const;
152151

153-
/** Return no data value.
152+
/** Return no data value. If the block does not have a no data value the
153+
* returned value is undefined.
154154
* @return No data value */
155155
double noDataValue() const { return mNoDataValue; }
156156

157-
/** Set no data value.
158-
* @param noDataValue the value to be considered no data
159-
*/
160-
//void setNoDataValue( double noDataValue ) { mNoDataValue = noDataValue; }
161-
162-
/** Test if value is nodata comparing to noDataValue
163-
* @param value tested value
164-
* @param noDataValue no data value
165-
* @return true if value is nodata */
166-
static bool isNoDataValue( double value, double noDataValue );
167-
168-
// get byte array representing no data value
157+
/** Get byte array representing a value.
158+
* @param theDataType data type
159+
* @param theValue value
160+
* @return byte array representing the value */
169161
static QByteArray valueBytes( QGis::DataType theDataType, double theValue );
170162

171-
/** \brief Read a single value
163+
/** \brief Read a single value if type of block is numeric. If type is color,
164+
* returned value is undefined.
172165
* @param row row index
173166
* @param column column index
174167
* @return value */
175168
double value( int row, int column ) const;
176169

177-
/** \brief Read a single value
170+
/** \brief Read a single value if type of block is numeric. If type is color,
171+
* returned value is undefined.
178172
* @param index data matrix index
179173
* @return value */
180174
double value( size_t index ) const;
@@ -221,6 +215,12 @@ class CORE_EXPORT QgsRasterBlock
221215
* @return true on success */
222216
bool setColor( int row, int column, QRgb color );
223217

218+
/** \brief Set color on index (indexed line by line)
219+
* @param index data matrix index
220+
* @param color the color to be set, QRgb value
221+
* @return true on success */
222+
bool setColor( size_t index, QRgb color );
223+
224224
/** \brief Set no data on pixel
225225
* @param row row index
226226
* @param column column index
@@ -240,12 +240,6 @@ class CORE_EXPORT QgsRasterBlock
240240
* @return true on success */
241241
bool setIsNoDataExcept( const QRect & theExceptRect );
242242

243-
/** \brief Set color on index (indexed line by line)
244-
* @param index data matrix index
245-
* @param color the color to be set, QRgb value
246-
* @return true on success */
247-
bool setColor( size_t index, QRgb color );
248-
249243
/** \brief Get pointer to data
250244
* @param row row index
251245
* @param column column index
@@ -255,10 +249,13 @@ class CORE_EXPORT QgsRasterBlock
255249

256250
/** \brief Get pointer to data
257251
* @param index data matrix index
258-
* @return pointer to data
259-
*/
252+
* @return pointer to data */
260253
char * bits( size_t index );
261254

255+
/** \brief Get pointer to data
256+
* @return pointer to data */
257+
char * bits();
258+
262259
/** \brief Print double value with all necessary significant digits.
263260
* It is ensured that conversion back to double gives the same number.
264261
* @param value the value to be printed
@@ -267,32 +264,17 @@ class CORE_EXPORT QgsRasterBlock
267264

268265
/** \brief Convert data to different type.
269266
* @param destDataType dest data type
270-
* @return true on success
271-
*/
267+
* @return true on success */
272268
bool convert( QGis::DataType destDataType );
273269

270+
/** \brief Get image if type is color.
271+
* @return image */
274272
QImage image() const;
275-
bool setImage( const QImage * image );
276-
277-
/** Create a new image with extraneous data, such data may be used
278-
* after the image is destroyed. The memory is not initialized.
279-
*/
280-
// TODO: remove, no more necessary with QgsRasterBlock
281-
QImage * createImage( int width, int height, QImage::Format format );
282-
283-
// TODO: remove this direct access to data, it was used in transition period
284-
void * data() { if ( mData ) return mData; return mImage->bits(); }
285-
286273

287-
// TODO: move to private, currently used by file writer
288-
/** \brief Convert block of data from one type to another. Original block memory
289-
* is not release.
290-
* @param srcData source data
291-
* @param srcDataType source data type
292-
* @param destDataType dest data type
293-
* @param size block size (width * height)
294-
* @return block of data in destDataType */
295-
static void * convert( void *srcData, QGis::DataType srcDataType, QGis::DataType destDataType, size_t size );
274+
/** \brief set image.
275+
* @param image image
276+
* @return true on success */
277+
bool setImage( const QImage * image );
296278

297279
inline static double readValue( void *data, QGis::DataType type, size_t index );
298280

@@ -321,13 +303,30 @@ class CORE_EXPORT QgsRasterBlock
321303
static QImage::Format imageFormat( QGis::DataType theDataType );
322304
static QGis::DataType dataType( QImage::Format theFormat );
323305

306+
/** Test if value is nodata comparing to noDataValue
307+
* @param value tested value
308+
* @param noDataValue no data value
309+
* @return true if value is nodata */
310+
static bool isNoDataValue( double value, double noDataValue );
311+
324312
/** Test if value is nodata for specific band
325313
* @param value tested value
326314
* @return true if value is nodata */
327315
bool isNoDataValue( double value ) const;
328316

317+
/** Allocate no data bitmap
318+
* @return true on success */
329319
bool createNoDataBitmap();
330320

321+
/** \brief Convert block of data from one type to another. Original block memory
322+
* is not release.
323+
* @param srcData source data
324+
* @param srcDataType source data type
325+
* @param destDataType dest data type
326+
* @param size block size (width * height)
327+
* @return block of data in destDataType */
328+
static void * convert( void *srcData, QGis::DataType srcDataType, QGis::DataType destDataType, size_t size );
329+
331330
// Valid
332331
bool mValid;
333332

@@ -363,6 +362,7 @@ class CORE_EXPORT QgsRasterBlock
363362

364363
// number of bytes in mNoDataBitmap row
365364
int mNoDataBitmapWidth;
365+
366366
// total size in bytes of mNoDataBitmap
367367
size_t mNoDataBitmapSize;
368368

@@ -372,19 +372,11 @@ class CORE_EXPORT QgsRasterBlock
372372

373373
inline double QgsRasterBlock::readValue( void *data, QGis::DataType type, size_t index )
374374
{
375-
#if 0
376-
if ( !mInput )
377-
{
378-
return 0;
379-
}
380-
381375
if ( !data )
382376
{
383-
return mInput->noDataValue();
377+
return std::numeric_limits<double>::quiet_NaN();
384378
}
385-
#endif
386379

387-
// TODO: define QGIS types to avoid cpl_port.h
388380
switch ( type )
389381
{
390382
case QGis::Byte:
@@ -409,12 +401,10 @@ inline double QgsRasterBlock::readValue( void *data, QGis::DataType type, size_t
409401
return ( double )(( double * )data )[index];
410402
break;
411403
default:
412-
//QgsMessageLog::logMessage( tr( "GDAL data type %1 is not supported" ).arg( type ), tr( "Raster" ) );
404+
QgsDebugMsg( QString( "Data type %1 is not supported" ).arg( type ) );
413405
break;
414406
}
415407

416-
// TODO: noDataValue is per band
417-
//return mInput->noDataValue();
418408
return std::numeric_limits<double>::quiet_NaN();
419409
}
420410

@@ -446,31 +436,19 @@ inline void QgsRasterBlock::writeValue( void *data, QGis::DataType type, size_t
446436
(( double * )data )[index] = value;
447437
break;
448438
default:
449-
//QgsMessageLog::logMessage( tr( "GDAL data type %1 is not supported" ).arg( type ), tr( "Raster" ) );
439+
QgsDebugMsg( QString( "Data type %1 is not supported" ).arg( type ) );
450440
break;
451441
}
452442
}
453443

454444
inline double QgsRasterBlock::value( size_t index ) const
455445
{
456-
/*if ( index >= ( size_t )mWidth*mHeight )
457-
{
458-
QgsDebugMsg( QString( "Index %1 out of range (%2 x %3)" ).arg( index ).arg( mWidth ).arg( mHeight ) );
459-
return mNoDataValue;
460-
}*/
461446
return readValue( mData, mDataType, index );
462447
}
463448

464449
inline bool QgsRasterBlock::isNoDataValue( double value ) const
465450
{
466-
// More precise would be qIsNaN(value) && qIsNaN(noDataValue(bandNo)), but probably
467-
// not important and slower
468-
if ( qIsNaN( value ) ||
469-
qgsDoubleNear( value, mNoDataValue ) )
470-
{
471-
return true;
472-
}
473-
return false;
451+
return qIsNaN( value ) || qgsDoubleNear( value, mNoDataValue );
474452
}
475453

476454
#endif

‎src/core/raster/qgsrasterdataprovider.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ QgsRasterBlock * QgsRasterDataProvider::block( int theBandNo, QgsRectangle cons
178178
tmpBlock = new QgsRasterBlock( dataType( theBandNo ), tmpWidth, tmpHeight );
179179
}
180180

181-
readBlock( theBandNo, tmpExtent, tmpWidth, tmpHeight, tmpBlock->data() );
181+
readBlock( theBandNo, tmpExtent, tmpWidth, tmpHeight, tmpBlock->bits() );
182182

183183
int pixelSize = dataTypeSize( theBandNo );
184184

@@ -228,7 +228,7 @@ QgsRasterBlock * QgsRasterDataProvider::block( int theBandNo, QgsRectangle cons
228228
}
229229
else
230230
{
231-
readBlock( theBandNo, theExtent, theWidth, theHeight, block->data() );
231+
readBlock( theBandNo, theExtent, theWidth, theHeight, block->bits() );
232232
}
233233

234234
// apply user no data values

‎src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ QgsRasterBlock* QgsGdalProvider::block( int theBandNo, const QgsRectangle &theEx
385385
QRect subRect = QgsRasterBlock::subRect( theExtent, theWidth, theHeight, mExtent );
386386
block->setIsNoDataExcept( subRect );
387387
}
388-
readBlock( theBandNo, theExtent, theWidth, theHeight, block->data() );
388+
readBlock( theBandNo, theExtent, theWidth, theHeight, block->bits() );
389389
block->applyNoDataValues( userNoDataValue( theBandNo ) );
390390
return block;
391391
}

0 commit comments

Comments
 (0)
Please sign in to comment.