QGIS API Documentation  2.9.0-Master
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 <limits>
22 #include <QImage>
23 #include "qgis.h"
24 #include "qgserror.h"
25 #include "qgslogger.h"
26 #include "qgsrasterrange.h"
27 #include "qgsrectangle.h"
28 
32 class CORE_EXPORT QgsRasterBlock
33 {
34  public:
36 
43  QgsRasterBlock( QGis::DataType theDataType, int theWidth, int theHeight );
44 
51  QgsRasterBlock( QGis::DataType theDataType, int theWidth, int theHeight, double theNoDataValue );
52 
53  virtual ~QgsRasterBlock();
54 
62  bool reset( QGis::DataType theDataType, int theWidth, int theHeight );
63 
71  bool reset( QGis::DataType theDataType, int theWidth, int theHeight, double theNoDataValue );
72 
73  // TODO: consider if use isValid() at all, isEmpty() should be sufficient
74  // and works also if block is valid but empty - difference between valid and empty?
79  bool isValid() const { return mValid; }
80 
82  void setValid( bool valid ) { mValid = valid; }
83 
88  bool isEmpty() const;
89 
90  // Return data type size in bytes
91  static int typeSize( int dataType )
92  {
93  // Modified and extended copy from GDAL
94  switch ( dataType )
95  {
96  case QGis::Byte:
97  return 1;
98 
99  case QGis::UInt16:
100  case QGis::Int16:
101  return 2;
102 
103  case QGis::UInt32:
104  case QGis::Int32:
105  case QGis::Float32:
106  case QGis::CInt16:
107  return 4;
108 
109  case QGis::Float64:
110  case QGis::CInt32:
111  case QGis::CFloat32:
112  return 8;
113 
114  case QGis::CFloat64:
115  return 16;
116 
117  case QGis::ARGB32:
119  return 4;
120 
121  default:
122  return 0;
123  }
124  }
125 
126  // Data type in bytes
127  int dataTypeSize() const
128  {
129  return typeSize( mDataType );
130  }
131 
133  static bool typeIsNumeric( QGis::DataType type );
134 
136  static bool typeIsColor( QGis::DataType type );
137 
139  QGis::DataType dataType() const { return mDataType; }
140 
142  static QGis::DataType typeWithNoDataValue( QGis::DataType dataType, double *noDataValue );
143 
146  bool hasNoDataValue() const { return mHasNoDataValue; }
147 
152  bool hasNoData() const;
153 
157  double noDataValue() const { return mNoDataValue; }
158 
163  static QByteArray valueBytes( QGis::DataType theDataType, double theValue );
164 
170  double value( int row, int column ) const;
171 
176  double value( qgssize index ) const;
177 
182  QRgb color( int row, int column ) const;
183 
187  QRgb color( qgssize index ) const;
188 
193  bool isNoData( int row, int column );
194 
198  bool isNoData( qgssize index );
199 
205  bool setValue( int row, int column, double value );
206 
211  bool setValue( qgssize index, double value );
212 
218  bool setColor( int row, int column, QRgb color );
219 
224  bool setColor( qgssize index, QRgb color );
225 
230  bool setIsNoData( int row, int column );
231 
235  bool setIsNoData( qgssize index );
236 
239  bool setIsNoData();
240 
243  bool setIsNoDataExcept( const QRect & theExceptRect );
244 
251  char * bits( int row, int column );
252 
258  char * bits( qgssize index );
259 
264  char * bits();
265 
270  static QString printValue( double value );
271 
275  bool convert( QGis::DataType destDataType );
276 
279  QImage image() const;
280 
284  bool setImage( const QImage * image );
285 
286  // @note not available in python bindings
287  inline static double readValue( void *data, QGis::DataType type, qgssize index );
288 
289  // @note not available in python bindings
290  inline static void writeValue( void *data, QGis::DataType type, qgssize index, double value );
291 
292  void applyNoDataValues( const QgsRasterRangeList & rangeList );
293 
296  void applyScaleOffset( double scale, double offset );
297 
299  QgsError error() const { return mError; }
300 
302  void setError( const QgsError & theError ) { mError = theError;}
303 
304  QString toString() const;
305 
315  static QRect subRect( const QgsRectangle &theExtent, int theWidth, int theHeight, const QgsRectangle &theSubExtent );
316 
317  private:
318  static QImage::Format imageFormat( QGis::DataType theDataType );
319  static QGis::DataType dataType( QImage::Format theFormat );
320 
325  static bool isNoDataValue( double value, double noDataValue );
326 
330  bool isNoDataValue( double value ) const;
331 
334  bool createNoDataBitmap();
335 
343  static void * convert( void *srcData, QGis::DataType srcDataType, QGis::DataType destDataType, qgssize size );
344 
345  // Valid
346  bool mValid;
347 
348  // Data type
349  QGis::DataType mDataType;
350 
351  // Data type size in bytes, to make bits() fast
352  int mTypeSize;
353 
354  // Width
355  int mWidth;
356 
357  // Height
358  int mHeight;
359 
360  // Has no data value
361  bool mHasNoDataValue;
362 
363  // No data value
364  double mNoDataValue;
365 
366  static const QRgb mNoDataColor;
367 
368  // Data block for numerical data types, not used with image data types
369  // QByteArray does not seem to be intended for large data blocks, does it?
370  void * mData;
371 
372  // Image for image data types, not used with numerical data types
373  QImage *mImage;
374 
375  // Bitmap of no data. One bit for each pixel. Bit is 1 if a pixels is no data.
376  // Each row is represented by whole number of bytes (last bits may be unused)
377  // to make processing rows easy.
378  char *mNoDataBitmap;
379 
380  // number of bytes in mNoDataBitmap row
381  int mNoDataBitmapWidth;
382 
383  // total size in bytes of mNoDataBitmap
384  qgssize mNoDataBitmapSize;
385 
386  // Error
387  QgsError mError;
388 };
389 
390 inline double QgsRasterBlock::readValue( void *data, QGis::DataType type, qgssize index )
391 {
392  if ( !data )
393  {
394  return std::numeric_limits<double>::quiet_NaN();
395  }
396 
397  switch ( type )
398  {
399  case QGis::Byte:
400  return ( double )(( quint8 * )data )[index];
401  break;
402  case QGis::UInt16:
403  return ( double )(( quint16 * )data )[index];
404  break;
405  case QGis::Int16:
406  return ( double )(( qint16 * )data )[index];
407  break;
408  case QGis::UInt32:
409  return ( double )(( quint32 * )data )[index];
410  break;
411  case QGis::Int32:
412  return ( double )(( qint32 * )data )[index];
413  break;
414  case QGis::Float32:
415  return ( double )(( float * )data )[index];
416  break;
417  case QGis::Float64:
418  return ( double )(( double * )data )[index];
419  break;
420  default:
421  QgsDebugMsg( QString( "Data type %1 is not supported" ).arg( type ) );
422  break;
423  }
424 
425  return std::numeric_limits<double>::quiet_NaN();
426 }
427 
428 inline void QgsRasterBlock::writeValue( void *data, QGis::DataType type, qgssize index, double value )
429 {
430  if ( !data ) return;
431 
432  switch ( type )
433  {
434  case QGis::Byte:
435  (( quint8 * )data )[index] = ( quint8 ) value;
436  break;
437  case QGis::UInt16:
438  (( quint16 * )data )[index] = ( quint16 ) value;
439  break;
440  case QGis::Int16:
441  (( qint16 * )data )[index] = ( qint16 ) value;
442  break;
443  case QGis::UInt32:
444  (( quint32 * )data )[index] = ( quint32 ) value;
445  break;
446  case QGis::Int32:
447  (( qint32 * )data )[index] = ( qint32 ) value;
448  break;
449  case QGis::Float32:
450  (( float * )data )[index] = ( float ) value;
451  break;
452  case QGis::Float64:
453  (( double * )data )[index] = value;
454  break;
455  default:
456  QgsDebugMsg( QString( "Data type %1 is not supported" ).arg( type ) );
457  break;
458  }
459 }
460 
461 inline double QgsRasterBlock::value( qgssize index ) const
462 {
463  if ( !mData )
464  {
465  QgsDebugMsg( "Data block not allocated" );
466  return std::numeric_limits<double>::quiet_NaN();
467  }
468  return readValue( mData, mDataType, index );
469 }
470 
471 inline bool QgsRasterBlock::isNoDataValue( double value ) const
472 {
473  return qIsNaN( value ) || qgsDoubleNear( value, mNoDataValue );
474 }
475 
476 #endif
477 
478 
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
bool isValid() const
Returns true if the block is valid (correctly filled with data).
void setError(const QgsError &theError)
Set error.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
double noDataValue() const
Return no data value.
QGis::DataType dataType() const
Returns data type.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:347
Raster data container.
double value(int row, int column) const
Read a single value if type of block is numeric.
static int typeSize(int dataType)
DataType
Raster data types.
Definition: qgis.h:204
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:446
int dataTypeSize() const
static void writeValue(void *data, QGis::DataType type, qgssize index, double value)
void setValid(bool valid)
Mark block as valid or invalid.
bool hasNoDataValue() const
True if the block has no data value.
QList< QgsRasterRange > QgsRasterRangeList
QgsError is container for error messages (report).
Definition: qgserror.h:77
static double readValue(void *data, QGis::DataType type, qgssize index)
QgsError error() const
Get error.
double size
Definition: qgssvgcache.cpp:77