18#ifndef QGSRASTERBLOCK_H
19#define QGSRASTERBLOCK_H
33using namespace Qt::StringLiterals;
183 return mHasNoDataValue || mNoDataBitmap;
192 void setNoDataValue(
double noDataValue )
SIP_HOLDGIL;
219 static QByteArray valueBytes(
Qgis::DataType dataType,
double value );
231 return value(
static_cast< qgssize >( row ) * mWidth + column );
272 inline double valueAndNoData(
qgssize index,
bool &isNoData )
const SIP_SKIP;
285 return static_cast< const quint8 *
>( mData );
296 if ( !mImage )
return NO_DATA_COLOR;
298 return mImage->pixel( column, row );
308 const int row =
static_cast< int >( std::floor(
static_cast< double >( index ) / mWidth ) );
309 const int column = index % mWidth;
310 return color( row, column );
345 if ( !mHasNoDataValue && !mNoDataBitmap )
347 if ( index >=
static_cast< qgssize >( mWidth )*mHeight )
349 QgsDebugError( u
"Index %1 out of range (%2 x %3)"_s.arg( index ).arg( mWidth ).arg( mHeight ) );
352 if ( mHasNoDataValue )
355 return isNoDataValue(
value );
358 if ( !mNoDataBitmap )
364 const int row =
static_cast< int >( index ) / mWidth;
365 const int column = index % mWidth;
366 const qgssize byte =
static_cast< qgssize >( row ) * mNoDataBitmapWidth + column / 8;
367 const int bit = column % 8;
368 const int mask = 0x80 >> bit;
371 return mNoDataBitmap[byte] & mask;
399 if ( index >=
static_cast< qgssize >( mWidth ) *mHeight )
401 QgsDebugError( u
"Index %1 out of range (%2 x %3)"_s.arg( index ).arg( mWidth ).arg( mHeight ) );
434 if ( index >=
static_cast< qgssize >( mImage->width() ) * mImage->height() )
441 QRgb *
bits =
reinterpret_cast< QRgb *
>( mImage->bits() );
457 return reinterpret_cast< QRgb *
>( mImage->bits() );
478 if ( mHasNoDataValue )
480 return setValue( index, mNoDataValue );
484 if ( !mNoDataBitmap )
486 if ( !createNoDataBitmap() )
492 const int row =
static_cast< int >( index ) / mWidth;
493 const int column = index % mWidth;
494 const qgssize byte =
static_cast< qgssize >( row ) * mNoDataBitmapWidth + column / 8;
495 const int bit = column % 8;
496 const int nodata = 0x80 >> bit;
498 mNoDataBitmap[byte] = mNoDataBitmap[byte] | nodata;
513 bool setIsNoDataExcept( QRect exceptRect );
537 if ( mHasNoDataValue )
543 if ( !mNoDataBitmap )
549 const int row =
static_cast< int >( index ) / mWidth;
550 const int column = index % mWidth;
551 const qgssize byte =
static_cast< qgssize >( row ) * mNoDataBitmapWidth + column / 8;
552 const int bit = column % 8;
553 const int nodata = 0x80 >> bit;
554 mNoDataBitmap[byte] = mNoDataBitmap[byte] & ~nodata;
566 bool fill(
double value );
578 void fill(
double value );
582 PyErr_SetString( PyExc_ValueError, u
"Cannot fill a block with %1 data type"_s.arg(
qgsEnumValueToKey( sipCpp->dataType() ) ).toUtf8().constData() );
587 PyErr_SetString( PyExc_ValueError, u
"Cannot fill a block with %1 complex data type"_s.arg(
qgsEnumValueToKey( sipCpp->dataType() ) ).toUtf8().constData() );
590 else if ( sipCpp->isEmpty() )
592 PyErr_SetString( PyExc_ValueError, u
"Cannot fill an empty block"_s.toUtf8().constData() );
611 QByteArray data()
const;
621 void setData(
const QByteArray &data,
int offset = 0 );
629 char *bits(
int row,
int column )
SIP_SKIP;
666 const
char *constBits() const
SIP_SKIP;
675 static QString printValue(
double value,
bool localized = false );
685 static QString printValue(
float value,
bool localized = false )
SIP_SKIP;
692 bool convert( Qgis::DataType destDataType );
697 QImage image() const;
703 bool setImage( const QImage *image );
706 inline static
double readValue(
void *data, Qgis::DataType type,
qgssize index )
SIP_SKIP;
709 inline static
void writeValue(
void *data, Qgis::DataType type,
qgssize index,
double value )
SIP_SKIP;
716 void applyScaleOffset(
double scale,
double offset );
724 QString toString()
const;
822 static bool isNoDataValue(
double value,
double noDataValue )
827 return std::isnan( value ) ||
836 inline bool isNoDataValue(
double value )
const;
842 bool createNoDataBitmap();
871 bool mHasNoDataValue =
false;
876 static const QRgb NO_DATA_COLOR;
880 void *mData =
nullptr;
883 QImage *mImage =
nullptr;
888 char *mNoDataBitmap =
nullptr;
891 int mNoDataBitmapWidth = 0;
904 return std::numeric_limits<double>::quiet_NaN();
910 return static_cast< double >( (
static_cast< quint8 *
>(
data ) )[index] );
912 return static_cast< double >( (
static_cast< qint8 *
>(
data ) )[index] );
914 return static_cast< double >( (
static_cast< quint16 *
>(
data ) )[index] );
916 return static_cast< double >( (
static_cast< qint16 *
>(
data ) )[index] );
918 return static_cast< double >( (
static_cast< quint32 *
>(
data ) )[index] );
920 return static_cast< double >( (
static_cast< qint32 *
>(
data ) )[index] );
922 return static_cast< double >( (
static_cast< float *
>(
data ) )[index] );
924 return static_cast< double >( (
static_cast< double *
>(
data ) )[index] );
936 return std::numeric_limits<double>::quiet_NaN();
946 (
static_cast< quint8 *
>(
data ) )[index] =
static_cast< quint8
>(
value );
949 (
static_cast< qint8 *
>(
data ) )[index] =
static_cast< qint8
>(
value );
952 (
static_cast< quint16 *
>(
data ) )[index] =
static_cast< quint16
>(
value );
955 (
static_cast< qint16 *
>(
data ) )[index] =
static_cast< qint16
>(
value );
958 (
static_cast< quint32 *
>(
data ) )[index] =
static_cast< quint32
>(
value );
961 (
static_cast< qint32 *
>(
data ) )[index] =
static_cast< qint32
>(
value );
964 (
static_cast< float *
>(
data ) )[index] =
static_cast< float >(
value );
967 (
static_cast< double *
>(
data ) )[index] =
value;
986 return std::numeric_limits<double>::quiet_NaN();
988 return readValue( mData, mDataType, index );
997 return std::numeric_limits<double>::quiet_NaN();
999 if ( index >=
static_cast< qgssize >( mWidth )*mHeight )
1001 QgsDebugError( u
"Index %1 out of range (%2 x %3)"_s.arg( index ).arg( mWidth ).arg( mHeight ) );
1003 return std::numeric_limits<double>::quiet_NaN();
1006 const double val =
readValue( mData, mDataType, index );
1008 if ( !mHasNoDataValue && !mNoDataBitmap )
1014 if ( mHasNoDataValue )
1020 if ( !mNoDataBitmap )
1032inline bool QgsRasterBlock::isNoDataValue(
double value )
const SIP_SKIP
1034 return std::isnan( value ) ||
qgsDoubleNear( value, mNoDataValue );
DataType
Raster data types.
@ Float32
Thirty two bit floating point (float).
@ CFloat64
Complex Float64.
@ Int16
Sixteen bit signed integer (qint16).
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
@ Int8
Eight bit signed integer (qint8) (added in QGIS 3.30).
@ UInt16
Sixteen bit unsigned integer (quint16).
@ Byte
Eight bit unsigned integer (quint8).
@ UnknownDataType
Unknown or unspecified type.
@ ARGB32
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
@ Int32
Thirty two bit signed integer (qint32).
@ Float64
Sixty four bit floating point (double).
@ CFloat32
Complex Float32.
@ UInt32
Thirty two bit unsigned integer (quint32).
A container for error messages.
bool isValid() const
Returns true if the block is valid (correctly filled with data).
QRgb color(int row, int column) const
Read a single color.
double value(int row, int column) const
Read a single value if type of block is numeric.
static bool typeIsNumeric(Qgis::DataType type)
Returns true if a data type is numeric.
void setValid(bool valid)
Mark block as valid or invalid.
int height() const
Returns the height (number of rows) of the raster block.
bool isNoData(qgssize row, qgssize column) const
Check if value at position is no data.
bool isNoData(qgssize index) const
Check if value at position is no data.
bool setColor(qgssize index, QRgb color)
Set color on index (indexed line by line).
int dataTypeSize() const
Data type size in bytes.
bool hasNoData() const
Returns true if the block may contain no data.
char * bits(int row, int column)
Returns a pointer to block data.
double valueAndNoData(int row, int column, bool &isNoData) const
Reads a single value from the pixel at row and column, if type of block is numeric.
static int typeSize(Qgis::DataType dataType)
Returns the size in bytes for the specified dataType.
QByteArray data() const
Gets access to raw data.
bool setIsNoData(qgssize index)
Set no data on pixel.
bool setValue(qgssize index, double value)
Set value on index (indexed line by line).
double noDataValue() const
Returns no data value.
QRgb * colorData()
Gives direct read/write access to the raster RGB data.
void setIsData(qgssize index)
Remove no data flag on pixel.
bool setValue(int row, int column, double value)
Set value on position.
bool setColor(int row, int column, QRgb color)
Set color on position.
bool isNoData(int row, int column) const
Checks if value at position is no data.
void setIsData(int row, int column)
Remove no data flag on pixel.
Qgis::DataType dataType() const
Returns data type.
bool hasNoDataValue() const
true if the block has no data value.
static bool typeIsComplex(Qgis::DataType type)
Returns true if a data type is a complex number type.
const quint8 * byteData() const
Gives direct access to the raster block data.
static void writeValue(void *data, Qgis::DataType type, qgssize index, double value)
int width() const
Returns the width (number of columns) of the raster block.
QgsError error() const
Returns the last error.
void setError(const QgsError &error)
Sets the last error.
static double readValue(void *data, Qgis::DataType type, qgssize index)
bool setIsNoData(int row, int column)
Set no data on pixel.
QRgb color(qgssize index) const
Read a single value.
bool reset(Qgis::DataType dataType, int width, int height)
Reset block.
A rectangle specified with double values.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
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...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
#define QgsDebugError(str)
QList< QgsRasterRange > QgsRasterRangeList