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;
210 return static_cast< const quint8 *
>( mData );
218 QRgb
color(
int row,
int column )
const 220 if ( !mImage )
return NO_DATA_COLOR;
222 return mImage->pixel( column, row );
231 int row =
static_cast< int >( std::floor( static_cast< double >( index ) / mWidth ) );
232 int column = index % mWidth;
233 return color( row, column );
243 return isNoData( static_cast< qgssize >( row ) * mWidth + column );
253 return isNoData( row * static_cast< qgssize >( mWidth ) + column );
262 if ( !mHasNoDataValue && !mNoDataBitmap )
264 if ( index >= static_cast< qgssize >( mWidth )*mHeight )
266 QgsDebugMsg( QStringLiteral(
"Index %1 out of range (%2 x %3)" ).arg( index ).arg( mWidth ).arg( mHeight ) );
269 if ( mHasNoDataValue )
271 double value = readValue( mData, mDataType, index );
272 return isNoDataValue( value );
275 if ( !mNoDataBitmap )
281 int row =
static_cast< int >( index ) / mWidth;
282 int column = index % mWidth;
283 qgssize byte =
static_cast< qgssize >( row ) * mNoDataBitmapWidth + column / 8;
284 int bit = column % 8;
285 int mask = 0x80 >> bit;
288 return mNoDataBitmap[byte] & mask;
299 return setValue( static_cast< qgssize >( row ) * mWidth + column, value );
311 QgsDebugMsg( QStringLiteral(
"Data block not allocated" ) );
314 if ( index >= static_cast< qgssize >( mWidth ) *mHeight )
316 QgsDebugMsg( QStringLiteral(
"Index %1 out of range (%2 x %3)" ).arg( index ).arg( mWidth ).arg( mHeight ) );
319 writeValue( mData, mDataType, index, value );
331 return setColor( static_cast< qgssize >( row ) * mWidth + column, color );
343 QgsDebugMsg( QStringLiteral(
"Image not allocated" ) );
347 if ( index >= static_cast< qgssize >( mImage->width() ) * mImage->height() )
349 QgsDebugMsg( QStringLiteral(
"index %1 out of range" ).arg( index ) );
354 QRgb *bits =
reinterpret_cast< QRgb *
>( mImage->bits() );
370 return reinterpret_cast< QRgb *
>( mImage->bits() );
380 return setIsNoData( static_cast< qgssize >( row ) * mWidth + column );
389 if ( mHasNoDataValue )
391 return setValue( index, mNoDataValue );
395 if ( !mNoDataBitmap )
397 if ( !createNoDataBitmap() )
403 int row =
static_cast< int >( index ) / mWidth;
404 int column = index % mWidth;
405 qgssize byte =
static_cast< qgssize >( row ) * mNoDataBitmapWidth + column / 8;
406 int bit = column % 8;
407 int nodata = 0x80 >> bit;
409 mNoDataBitmap[byte] = mNoDataBitmap[byte] | nodata;
422 bool setIsNoDataExcept( QRect exceptRect );
434 setIsData( static_cast< qgssize >( row )*mWidth + column );
446 if ( mHasNoDataValue )
452 if ( !mNoDataBitmap )
458 int row =
static_cast< int >( index ) / mWidth;
459 int column = index % mWidth;
460 qgssize byte =
static_cast< qgssize >( row ) * mNoDataBitmapWidth + column / 8;
461 int bit = column % 8;
462 int nodata = 0x80 >> bit;
463 mNoDataBitmap[byte] = mNoDataBitmap[byte] & ~nodata;
475 QByteArray data()
const;
486 void setData(
const QByteArray &data,
int offset = 0 );
494 char *bits(
int row,
int column )
SIP_SKIP;
514 static QString printValue(
double value );
524 static QString printValue(
float value )
SIP_SKIP;
535 QImage image()
const;
541 bool setImage(
const QImage *image );
555 void applyScaleOffset(
double scale,
double offset );
563 QString toString()
const;
582 int width()
const {
return mWidth; }
600 static bool isNoDataValue(
double value,
double noDataValue )
605 return std::isnan( value ) ||
613 bool isNoDataValue(
double value )
const;
618 bool createNoDataBitmap();
646 bool mHasNoDataValue =
false;
651 static const QRgb NO_DATA_COLOR;
655 void *mData =
nullptr;
658 QImage *mImage =
nullptr;
663 char *mNoDataBitmap =
nullptr;
666 int mNoDataBitmapWidth = 0;
679 return std::numeric_limits<double>::quiet_NaN();
685 return static_cast< double >( (
static_cast< quint8 *
>( data ) )[index] );
688 return static_cast< double >( (
static_cast< quint16 *
>( data ) )[index] );
691 return static_cast< double >( (
static_cast< qint16 *
>( data ) )[index] );
694 return static_cast< double >( (
static_cast< quint32 *
>( data ) )[index] );
697 return static_cast< double >( (
static_cast< qint32 *
>( data ) )[index] );
700 return static_cast< double >( (
static_cast< float *
>( data ) )[index] );
703 return static_cast< double >( (
static_cast< double *
>( data ) )[index] );
706 QgsDebugMsg( QStringLiteral(
"Data type %1 is not supported" ).arg( type ) );
710 return std::numeric_limits<double>::quiet_NaN();
720 (
static_cast< quint8 *
>( data ) )[index] =
static_cast< quint8
>( value );
723 (
static_cast< quint16 *
>( data ) )[index] =
static_cast< quint16
>( value );
726 (
static_cast< qint16 *
>( data ) )[index] =
static_cast< qint16
>( value );
729 (
static_cast< quint32 *
>( data ) )[index] =
static_cast< quint32
>( value );
732 (
static_cast< qint32 *
>( data ) )[index] =
static_cast< qint32
>( value );
735 (
static_cast< float *
>( data ) )[index] =
static_cast< float >( value );
738 (
static_cast< double *
>( data ) )[index] = value;
741 QgsDebugMsg( QStringLiteral(
"Data type %1 is not supported" ).arg( type ) );
750 QgsDebugMsg( QStringLiteral(
"Data block not allocated" ) );
751 return std::numeric_limits<double>::quiet_NaN();
753 return readValue( mData, mDataType, index );
756 inline bool QgsRasterBlock::isNoDataValue(
double value )
const SIP_SKIP 758 return std::isnan( value ) ||
qgsDoubleNear( value, mNoDataValue );
A rectangle specified with double values.
Thirty two bit signed integer (qint32)
Qgis::DataType dataType() const
Returns data type.
int height() const
Returns the height (number of rows) of the raster block.
bool isValid() const
Returns true if the block is valid (correctly filled with data).
static double readValue(void *data, Qgis::DataType type, qgssize index)
bool setValue(int row, int column, double value)
Set value on position.
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)
QRgb * colorData()
Gives direct read/write access to the raster RGB data.
double noDataValue() const
Returns no data value.
DataType
Raster data types.
int width() const
Returns the width (number of columns) of the raster block.
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)
Sixty four bit floating point (double)
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.
bool hasNoData() const
Returns true if the block may contain no data.
void setIsData(qgssize index)
Remove no data flag on pixel.
double value(int row, int column) const
Read a single value if type of block is numeric.
void setError(const QgsError &error)
Sets the last error.
bool setIsNoData(qgssize index)
Set no data on pixel.
void setIsData(int row, int column)
Remove no data flag on pixel.
const quint8 * byteData() const
Gives direct access to the raster block data.
Unknown or unspecified type.
static int typeSize(int dataType)
Sixteen bit unsigned integer (quint16)
bool isNoData(qgssize index) const
Check if value at position is no data.
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...
void setValid(bool valid)
Mark block as valid or invalid.
bool hasNoDataValue() const
True if the block has no data 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).
bool isNoData(int row, int column) const
Check if value at position is no data.
QRgb color(int row, int column) const
Read a single color.
QgsError error() const
Returns the last error.
bool isNoData(qgssize row, qgssize column) const
Check if value at position is no data.
QRgb color(qgssize index) const
Read a single value.
Eight bit unsigned integer (quint8)
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.