QGIS API Documentation  2.99.0-Master (314842d)
qgsrasterblock.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterblock.h - Class representing a block of raster data
3  --------------------------------------
4  Date : Oct 9, 2012
5  Copyright : (C) 2012 by Radim Blazek
6  email : radim dot blazek at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSRASTERBLOCK_H
19 #define QGSRASTERBLOCK_H
20 
21 #include "qgis_core.h"
22 #include <limits>
23 #include <QImage>
24 #include "qgis.h"
25 #include "qgserror.h"
26 #include "qgslogger.h"
27 #include "qgsrasterrange.h"
28 
29 class QgsRectangle;
30 
34 class CORE_EXPORT QgsRasterBlock
35 {
36  public:
38 
44  QgsRasterBlock( Qgis::DataType dataType, int width, int height );
45 
46  virtual ~QgsRasterBlock();
47 
54  bool reset( Qgis::DataType dataType, int width, int height );
55 
56  // TODO: consider if use isValid() at all, isEmpty() should be sufficient
57  // and works also if block is valid but empty - difference between valid and empty?
58 
63  bool isValid() const { return mValid; }
64 
66  void setValid( bool valid ) { mValid = valid; }
67 
72  bool isEmpty() const;
73 
74  // Return data type size in bytes
75  static int typeSize( int dataType )
76  {
77  // Modified and extended copy from GDAL
78  switch ( dataType )
79  {
80  case Qgis::Byte:
81  return 1;
82 
83  case Qgis::UInt16:
84  case Qgis::Int16:
85  return 2;
86 
87  case Qgis::UInt32:
88  case Qgis::Int32:
89  case Qgis::Float32:
90  case Qgis::CInt16:
91  return 4;
92 
93  case Qgis::Float64:
94  case Qgis::CInt32:
95  case Qgis::CFloat32:
96  return 8;
97 
98  case Qgis::CFloat64:
99  return 16;
100 
101  case Qgis::ARGB32:
103  return 4;
104 
105  default:
106  return 0;
107  }
108  }
109 
110  // Data type in bytes
111  int dataTypeSize() const
112  {
113  return typeSize( mDataType );
114  }
115 
117  static bool typeIsNumeric( Qgis::DataType type );
118 
120  static bool typeIsColor( Qgis::DataType type );
121 
123  Qgis::DataType dataType() const { return mDataType; }
124 
126  static Qgis::DataType typeWithNoDataValue( Qgis::DataType dataType, double *noDataValue );
127 
132  bool hasNoDataValue() const { return mHasNoDataValue; }
133 
138  bool hasNoData() const;
139 
144  void setNoDataValue( double noDataValue );
145 
151  void resetNoDataValue();
152 
158  double noDataValue() const { return mNoDataValue; }
159 
164  static QByteArray valueBytes( Qgis::DataType dataType, double value );
165 
171  double value( int row, int column ) const;
172 
177  double value( qgssize index ) const;
178 
183  QRgb color( int row, int column ) const;
184 
188  QRgb color( qgssize index ) const;
189 
194  bool isNoData( int row, int column );
195 
199  bool isNoData( qgssize index );
200 
206  bool setValue( int row, int column, double value );
207 
212  bool setValue( qgssize index, double value );
213 
219  bool setColor( int row, int column, QRgb color );
220 
225  bool setColor( qgssize index, QRgb color );
226 
231  bool setIsNoData( int row, int column );
232 
236  bool setIsNoData( qgssize index );
237 
240  bool setIsNoData();
241 
244  bool setIsNoDataExcept( QRect exceptRect );
245 
253  void setIsData( int row, int column );
254 
261  void setIsData( qgssize index );
262 
271  QByteArray data() const;
272 
281  void setData( const QByteArray &data, int offset = 0 );
282 
289  char *bits( int row, int column );
290 
296  char *bits( qgssize index );
297 
302  char *bits();
303 
308  static QString printValue( double value );
309 
317  static QString printValue( float value );
318 
322  bool convert( Qgis::DataType destDataType );
323 
326  QImage image() const;
327 
331  bool setImage( const QImage *image );
332 
334  inline static double readValue( void *data, Qgis::DataType type, qgssize index );
335 
337  inline static void writeValue( void *data, Qgis::DataType type, qgssize index, double value );
338 
339  void applyNoDataValues( const QgsRasterRangeList &rangeList );
340 
343  void applyScaleOffset( double scale, double offset );
344 
346  QgsError error() const { return mError; }
347 
349  void setError( const QgsError &error ) { mError = error;}
350 
351  QString toString() const;
352 
362  static QRect subRect( const QgsRectangle &extent, int width, int height, const QgsRectangle &subExtent );
363 
368  int width() const { return mWidth; }
369 
374  int height() const { return mHeight; }
375 
376  private:
377  static QImage::Format imageFormat( Qgis::DataType dataType );
378  static Qgis::DataType dataType( QImage::Format format );
379 
384  static bool isNoDataValue( double value, double noDataValue );
385 
389  bool isNoDataValue( double value ) const;
390 
393  bool createNoDataBitmap();
394 
402  static void *convert( void *srcData, Qgis::DataType srcDataType, Qgis::DataType destDataType, qgssize size );
403 
404  // Valid
405  bool mValid;
406 
407  // Data type
408  Qgis::DataType mDataType;
409 
410  // Data type size in bytes, to make bits() fast
411  int mTypeSize;
412 
413  // Width
414  int mWidth;
415 
416  // Height
417  int mHeight;
418 
419  // Has no data value
420  bool mHasNoDataValue;
421 
422  // No data value
423  double mNoDataValue;
424 
425  static const QRgb NO_DATA_COLOR;
426 
427  // Data block for numerical data types, not used with image data types
428  // QByteArray does not seem to be intended for large data blocks, does it?
429  void *mData = nullptr;
430 
431  // Image for image data types, not used with numerical data types
432  QImage *mImage = nullptr;
433 
434  // Bitmap of no data. One bit for each pixel. Bit is 1 if a pixels is no data.
435  // Each row is represented by whole number of bytes (last bits may be unused)
436  // to make processing rows easy.
437  char *mNoDataBitmap = nullptr;
438 
439  // number of bytes in mNoDataBitmap row
440  int mNoDataBitmapWidth;
441 
442  // total size in bytes of mNoDataBitmap
443  qgssize mNoDataBitmapSize;
444 
445  // Error
446  QgsError mError;
447 };
448 
449 inline double QgsRasterBlock::readValue( void *data, Qgis::DataType type, qgssize index )
450 {
451  if ( !data )
452  {
453  return std::numeric_limits<double>::quiet_NaN();
454  }
455 
456  switch ( type )
457  {
458  case Qgis::Byte:
459  return static_cast< double >( ( static_cast< quint8 * >( data ) )[index] );
460  break;
461  case Qgis::UInt16:
462  return static_cast< double >( ( static_cast< quint16 * >( data ) )[index] );
463  break;
464  case Qgis::Int16:
465  return static_cast< double >( ( static_cast< qint16 * >( data ) )[index] );
466  break;
467  case Qgis::UInt32:
468  return static_cast< double >( ( static_cast< quint32 * >( data ) )[index] );
469  break;
470  case Qgis::Int32:
471  return static_cast< double >( ( static_cast< qint32 * >( data ) )[index] );
472  break;
473  case Qgis::Float32:
474  return static_cast< double >( ( static_cast< float * >( data ) )[index] );
475  break;
476  case Qgis::Float64:
477  return static_cast< double >( ( static_cast< double * >( data ) )[index] );
478  break;
479  default:
480  QgsDebugMsg( QString( "Data type %1 is not supported" ).arg( type ) );
481  break;
482  }
483 
484  return std::numeric_limits<double>::quiet_NaN();
485 }
486 
487 inline void QgsRasterBlock::writeValue( void *data, Qgis::DataType type, qgssize index, double value )
488 {
489  if ( !data ) return;
490 
491  switch ( type )
492  {
493  case Qgis::Byte:
494  ( static_cast< quint8 * >( data ) )[index] = static_cast< quint8 >( value );
495  break;
496  case Qgis::UInt16:
497  ( static_cast< quint16 * >( data ) )[index] = static_cast< quint16 >( value );
498  break;
499  case Qgis::Int16:
500  ( static_cast< qint16 * >( data ) )[index] = static_cast< qint16 >( value );
501  break;
502  case Qgis::UInt32:
503  ( static_cast< quint32 * >( data ) )[index] = static_cast< quint32 >( value );
504  break;
505  case Qgis::Int32:
506  ( static_cast< qint32 * >( data ) )[index] = static_cast< qint32 >( value );
507  break;
508  case Qgis::Float32:
509  ( static_cast< float * >( data ) )[index] = static_cast< float >( value );
510  break;
511  case Qgis::Float64:
512  ( static_cast< double * >( data ) )[index] = value;
513  break;
514  default:
515  QgsDebugMsg( QString( "Data type %1 is not supported" ).arg( type ) );
516  break;
517  }
518 }
519 
520 inline double QgsRasterBlock::value( qgssize index ) const
521 {
522  if ( !mData )
523  {
524  QgsDebugMsg( "Data block not allocated" );
525  return std::numeric_limits<double>::quiet_NaN();
526  }
527  return readValue( mData, mDataType, index );
528 }
529 
530 inline bool QgsRasterBlock::isNoDataValue( double value ) const
531 {
532  return qIsNaN( value ) || qgsDoubleNear( value, mNoDataValue );
533 }
534 
535 #endif
536 
537 
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:36
Thirty two bit signed integer (qint32)
Definition: qgis.h:68
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.
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
int height() const
Returns the height (number of rows) of the raster block.
Thirty two bit unsigned integer (quint32)
Definition: qgis.h:67
DataType
Raster data types.
Definition: qgis.h:61
Thirty two bit floating point (float)
Definition: qgis.h:69
Sixteen bit signed integer (qint16)
Definition: qgis.h:66
Complex Int16.
Definition: qgis.h:71
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:198
Sixty four bit floating point (double)
Definition: qgis.h:70
QgsError error() const
Get error.
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:76
Raster data container.
void setError(const QgsError &error)
Set error.
Complex Float32.
Definition: qgis.h:73
Complex Int32.
Definition: qgis.h:72
static int typeSize(int dataType)
Sixteen bit unsigned integer (quint16)
Definition: qgis.h:65
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...
Definition: qgis.h:349
int width() const
Returns the width (number of columns) of the raster block.
void setValid(bool valid)
Mark block as valid or invalid.
static void writeValue(void *data, Qgis::DataType type, qgssize index, double value)
QList< QgsRasterRange > QgsRasterRangeList
QgsError is container for error messages (report).
Definition: qgserror.h:82
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.
int dataTypeSize() const
Complex Float64.
Definition: qgis.h:74
double noDataValue() const
Return no data value.
Eight bit unsigned integer (quint8)
Definition: qgis.h:64
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
Definition: qgis.h:75