QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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( size_t index ) const;
177 
182  QRgb color( int row, int column ) const;
183 
187  QRgb color( size_t index ) const;
188 
193  bool isNoData( int row, int column );
194 
198  bool isNoData( size_t index );
199 
205  bool setValue( int row, int column, double value );
206 
211  bool setValue( size_t index, double value );
212 
218  bool setColor( int row, int column, QRgb color );
219 
224  bool setColor( size_t index, QRgb color );
225 
230  bool setIsNoData( int row, int column );
231 
235  bool setIsNoData( size_t index );
236 
239  bool setIsNoData();
240 
243  bool setIsNoDataExcept( const QRect & theExceptRect );
244 
251  char * bits( int row, int column );
252 
258  char * bits( size_t 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, size_t index );
288 
289  // @note not available in python bindings
290  inline static void writeValue( void *data, QGis::DataType type, size_t index, double value );
291 
292  void applyNoDataValues( const QgsRasterRangeList & rangeList );
293 
295  QgsError error() const { return mError; }
296 
298  void setError( const QgsError & theError ) { mError = theError;}
299 
309  static QRect subRect( const QgsRectangle &theExtent, int theWidth, int theHeight, const QgsRectangle &theSubExtent );
310 
311  private:
312  static QImage::Format imageFormat( QGis::DataType theDataType );
313  static QGis::DataType dataType( QImage::Format theFormat );
314 
319  static bool isNoDataValue( double value, double noDataValue );
320 
324  bool isNoDataValue( double value ) const;
325 
328  bool createNoDataBitmap();
329 
337  static void * convert( void *srcData, QGis::DataType srcDataType, QGis::DataType destDataType, size_t size );
338 
339  // Valid
340  bool mValid;
341 
342  // Data type
344 
345  // Data type size in bytes, to make bits() fast
347 
348  // Width
349  int mWidth;
350 
351  // Height
352  int mHeight;
353 
354  // Has no data value
356 
357  // No data value
358  double mNoDataValue;
359 
360  // Data block for numerical data types, not used with image data types
361  // QByteArray does not seem to be intended for large data blocks, does it?
362  void * mData;
363 
364  // Image for image data types, not used with numerical data types
365  QImage *mImage;
366 
367  // Bitmap of no data. One bit for each pixel. Bit is 1 if a pixels is no data.
368  // Each row is represented by whole number of bytes (last bits may be unused)
369  // to make processing rows easy.
371 
372  // number of bytes in mNoDataBitmap row
374 
375  // total size in bytes of mNoDataBitmap
377 
378  // Error
380 };
381 
382 inline double QgsRasterBlock::readValue( void *data, QGis::DataType type, size_t index )
383 {
384  if ( !data )
385  {
386  return std::numeric_limits<double>::quiet_NaN();
387  }
388 
389  switch ( type )
390  {
391  case QGis::Byte:
392  return ( double )(( quint8 * )data )[index];
393  break;
394  case QGis::UInt16:
395  return ( double )(( quint16 * )data )[index];
396  break;
397  case QGis::Int16:
398  return ( double )(( qint16 * )data )[index];
399  break;
400  case QGis::UInt32:
401  return ( double )(( quint32 * )data )[index];
402  break;
403  case QGis::Int32:
404  return ( double )(( qint32 * )data )[index];
405  break;
406  case QGis::Float32:
407  return ( double )(( float * )data )[index];
408  break;
409  case QGis::Float64:
410  return ( double )(( double * )data )[index];
411  break;
412  default:
413  QgsDebugMsg( QString( "Data type %1 is not supported" ).arg( type ) );
414  break;
415  }
416 
417  return std::numeric_limits<double>::quiet_NaN();
418 }
419 
420 inline void QgsRasterBlock::writeValue( void *data, QGis::DataType type, size_t index, double value )
421 {
422  if ( !data ) return;
423 
424  switch ( type )
425  {
426  case QGis::Byte:
427  (( quint8 * )data )[index] = ( quint8 ) value;
428  break;
429  case QGis::UInt16:
430  (( quint16 * )data )[index] = ( quint16 ) value;
431  break;
432  case QGis::Int16:
433  (( qint16 * )data )[index] = ( qint16 ) value;
434  break;
435  case QGis::UInt32:
436  (( quint32 * )data )[index] = ( quint32 ) value;
437  break;
438  case QGis::Int32:
439  (( qint32 * )data )[index] = ( qint32 ) value;
440  break;
441  case QGis::Float32:
442  (( float * )data )[index] = ( float ) value;
443  break;
444  case QGis::Float64:
445  (( double * )data )[index] = value;
446  break;
447  default:
448  QgsDebugMsg( QString( "Data type %1 is not supported" ).arg( type ) );
449  break;
450  }
451 }
452 
453 inline double QgsRasterBlock::value( size_t index ) const
454 {
455  if ( !mData )
456  {
457  QgsDebugMsg( "Data block not allocated" );
458  return std::numeric_limits<double>::quiet_NaN();
459  }
460  return readValue( mData, mDataType, index );
461 }
462 
463 inline bool QgsRasterBlock::isNoDataValue( double value ) const
464 {
465  return qIsNaN( value ) || qgsDoubleNear( value, mNoDataValue );
466 }
467 
468 #endif
469 
470