18 #ifndef QGSRASTERBLOCK_H 19 #define QGSRASTERBLOCK_H 21 #include "qgis_core.h" 71 void setValid(
bool valid ) { mValid = valid; }
119 return typeSize( mDataType );
148 return mHasNoDataValue || mNoDataBitmap;
156 void setNoDataValue(
double noDataValue );
164 void resetNoDataValue();
179 static QByteArray valueBytes(
Qgis::DataType dataType,
double value );
187 double value(
int row,
int column )
const 189 return value( static_cast< qgssize >( row ) * mWidth + column );
197 double value(
qgssize index )
const;
204 QRgb
color(
int row,
int column )
const 206 if ( !mImage )
return NO_DATA_COLOR;
208 return mImage->pixel( column, row );
217 int row =
static_cast< int >( std::floor( static_cast< double >( index ) / mWidth ) );
218 int column = index % mWidth;
219 return color( row, column );
229 return isNoData( static_cast< qgssize >( row ) * mWidth + column );
238 if ( !mHasNoDataValue && !mNoDataBitmap )
240 if ( index >= static_cast< qgssize >( mWidth )*mHeight )
242 QgsDebugMsg( QStringLiteral(
"Index %1 out of range (%2 x %3)" ).arg( index ).arg( mWidth ).arg( mHeight ) );
245 if ( mHasNoDataValue )
247 double value = readValue( mData, mDataType, index );
248 return isNoDataValue( value );
251 if ( !mNoDataBitmap )
257 int row =
static_cast< int >( index ) / mWidth;
258 int column = index % mWidth;
259 qgssize byte =
static_cast< qgssize >( row ) * mNoDataBitmapWidth + column / 8;
260 int bit = column % 8;
261 int mask = 0x80 >> bit;
264 return mNoDataBitmap[byte] & mask;
275 return setValue( static_cast< qgssize >( row ) * mWidth + column, value );
287 QgsDebugMsg( QStringLiteral(
"Data block not allocated" ) );
290 if ( index >= static_cast< qgssize >( mWidth ) *mHeight )
292 QgsDebugMsg( QStringLiteral(
"Index %1 out of range (%2 x %3)" ).arg( index ).arg( mWidth ).arg( mHeight ) );
295 writeValue( mData, mDataType, index, value );
307 return setColor( static_cast< qgssize >( row ) * mWidth + column, color );
319 QgsDebugMsg( QStringLiteral(
"Image not allocated" ) );
323 if ( index >= static_cast< qgssize >( mImage->width() ) * mImage->height() )
325 QgsDebugMsg( QStringLiteral(
"index %1 out of range" ).arg( index ) );
330 QRgb *bits =
reinterpret_cast< QRgb *
>( mImage->bits() );
342 return setIsNoData( static_cast< qgssize >( row ) * mWidth + column );
351 if ( mHasNoDataValue )
353 return setValue( index, mNoDataValue );
357 if ( !mNoDataBitmap )
359 if ( !createNoDataBitmap() )
365 int row =
static_cast< int >( index ) / mWidth;
366 int column = index % mWidth;
367 qgssize byte =
static_cast< qgssize >( row ) * mNoDataBitmapWidth + column / 8;
368 int bit = column % 8;
369 int nodata = 0x80 >> bit;
371 mNoDataBitmap[byte] = mNoDataBitmap[byte] | nodata;
384 bool setIsNoDataExcept( QRect exceptRect );
396 setIsData( static_cast< qgssize >( row )*mWidth + column );
408 if ( mHasNoDataValue )
414 if ( !mNoDataBitmap )
420 int row =
static_cast< int >( index ) / mWidth;
421 int column = index % mWidth;
422 qgssize byte =
static_cast< qgssize >( row ) * mNoDataBitmapWidth + column / 8;
423 int bit = column % 8;
424 int nodata = 0x80 >> bit;
425 mNoDataBitmap[byte] = mNoDataBitmap[byte] & ~nodata;
437 QByteArray data()
const;
448 void setData(
const QByteArray &data,
int offset = 0 );
456 char *bits(
int row,
int column )
SIP_SKIP;
476 static QString printValue(
double value );
486 static QString printValue(
float value )
SIP_SKIP;
497 QImage image()
const;
503 bool setImage(
const QImage *image );
517 void applyScaleOffset(
double scale,
double offset );
525 QString toString()
const;
544 int width()
const {
return mWidth; }
562 static bool isNoDataValue(
double value,
double noDataValue )
567 return std::isnan( value ) ||
575 bool isNoDataValue(
double value )
const;
580 bool createNoDataBitmap();
608 bool mHasNoDataValue =
false;
613 static const QRgb NO_DATA_COLOR;
617 void *mData =
nullptr;
620 QImage *mImage =
nullptr;
625 char *mNoDataBitmap =
nullptr;
628 int mNoDataBitmapWidth = 0;
641 return std::numeric_limits<double>::quiet_NaN();
647 return static_cast< double >( (
static_cast< quint8 *
>( data ) )[index] );
650 return static_cast< double >( (
static_cast< quint16 *
>( data ) )[index] );
653 return static_cast< double >( (
static_cast< qint16 *
>( data ) )[index] );
656 return static_cast< double >( (
static_cast< quint32 *
>( data ) )[index] );
659 return static_cast< double >( (
static_cast< qint32 *
>( data ) )[index] );
662 return static_cast< double >( (
static_cast< float *
>( data ) )[index] );
665 return static_cast< double >( (
static_cast< double *
>( data ) )[index] );
668 QgsDebugMsg( QString(
"Data type %1 is not supported" ).arg( type ) );
672 return std::numeric_limits<double>::quiet_NaN();
682 (
static_cast< quint8 *
>( data ) )[index] =
static_cast< quint8
>( value );
685 (
static_cast< quint16 *
>( data ) )[index] =
static_cast< quint16
>( value );
688 (
static_cast< qint16 *
>( data ) )[index] =
static_cast< qint16
>( value );
691 (
static_cast< quint32 *
>( data ) )[index] =
static_cast< quint32
>( value );
694 (
static_cast< qint32 *
>( data ) )[index] =
static_cast< qint32
>( value );
697 (
static_cast< float *
>( data ) )[index] =
static_cast< float >( value );
700 (
static_cast< double *
>( data ) )[index] = value;
703 QgsDebugMsg( QString(
"Data type %1 is not supported" ).arg( type ) );
712 QgsDebugMsg( QStringLiteral(
"Data block not allocated" ) );
713 return std::numeric_limits<double>::quiet_NaN();
715 return readValue( mData, mDataType, index );
718 inline bool QgsRasterBlock::isNoDataValue(
double value )
const SIP_SKIP 720 return std::isnan( value ) ||
qgsDoubleNear( value, mNoDataValue );
A rectangle specified with double values.
Thirty two bit signed integer (qint32)
bool isValid() const
Returns true if the block is valid (correctly filled with data).
static double readValue(void *data, Qgis::DataType type, qgssize index)
Qgis::DataType dataType() const
Returns data type.
bool setValue(int row, int column, double value)
Set value on position.
int height() const
Returns the height (number of rows) of the raster block.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Thirty two bit unsigned integer (quint32)
DataType
Raster data types.
bool setColor(qgssize index, QRgb color)
Set color on index (indexed line by line)
Thirty two bit floating point (float)
Sixteen bit signed integer (qint16)
bool isNoData(int row, int column)
Check if value at position is no data.
Sixty four bit floating point (double)
QgsError error() const
Returns the last error.
bool isNoData(qgssize index)
Check if value at position is no data.
bool setIsNoData(int row, int column)
Set no data on pixel.
bool setColor(int row, int column, QRgb color)
Set color on position.
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
void setIsData(qgssize index)
Remove no data flag on pixel.
void setError(const QgsError &error)
Sets the last error.
bool setIsNoData(qgssize index)
Set no data on pixel.
QRgb color(int row, int column) const
Read a single color.
void setIsData(int row, int column)
Remove no data flag on pixel.
Unknown or unspecified type.
static int typeSize(int dataType)
Sixteen bit unsigned integer (quint16)
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
int width() const
Returns the width (number of columns) of the raster block.
void setValid(bool valid)
Mark block as valid or invalid.
QRgb color(qgssize index) const
Read a single value.
static void writeValue(void *data, Qgis::DataType type, qgssize index, double value)
QList< QgsRasterRange > QgsRasterRangeList
bool setValue(qgssize index, double value)
Set value on index (indexed line by line)
QgsError is container for error messages (report).
double value(int row, int column) const
Read a single value if type of block is numeric.
bool hasNoDataValue() const
True if the block has no data value.
bool hasNoData() const
Returns true if the block may contain no data.
double noDataValue() const
Returns no data value.
Eight bit unsigned integer (quint8)
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.