27
27
#include " cpl_conv.h"
28
28
29
29
QgsRasterBlock::QgsRasterBlock ()
30
- // mValid (false)
31
- : mDataType ( UnknownDataType )
32
- , mTypeSize(0 )
33
- , mWidth(0 )
34
- , mHeight(0 )
35
- , mNoDataValue(std::numeric_limits<double >::quiet_NaN())
36
- , mData(0 )
37
- , mImage(0 )
30
+ // mValid (false)
31
+ : mDataType( UnknownDataType )
32
+ , mTypeSize( 0 )
33
+ , mWidth( 0 )
34
+ , mHeight( 0 )
35
+ , mNoDataValue( std::numeric_limits<double >::quiet_NaN() )
36
+ , mData( 0 )
37
+ , mImage( 0 )
38
38
{
39
39
}
40
40
41
41
QgsRasterBlock::QgsRasterBlock ( DataType theDataType, int theWidth, int theHeight, double theNoDataValue )
42
- // mValid(true)
43
- : mDataType(theDataType)
44
- , mTypeSize(0 )
45
- , mWidth(theWidth)
46
- , mHeight(theHeight)
47
- , mNoDataValue(theNoDataValue)
48
- , mData(0 )
49
- , mImage(0 )
42
+ // mValid(true)
43
+ : mDataType( theDataType )
44
+ , mTypeSize( 0 )
45
+ , mWidth( theWidth )
46
+ , mHeight( theHeight )
47
+ , mNoDataValue( theNoDataValue )
48
+ , mData( 0 )
49
+ , mImage( 0 )
50
50
{
51
- reset ( mDataType , mWidth , mHeight , mNoDataValue );
51
+ reset ( mDataType , mWidth , mHeight , mNoDataValue );
52
52
}
53
53
54
54
QgsRasterBlock::~QgsRasterBlock ()
55
55
{
56
- QgsFree (mData );
57
-
56
+ QgsFree ( mData );
57
+
58
58
}
59
59
60
60
bool QgsRasterBlock::reset ( DataType theDataType, int theWidth, int theHeight, double theNoDataValue )
61
61
{
62
- QgsDebugMsg ( QString (" theWidth= %1 theHeight = %2 theDataType = %3 theNoDataValue = %4" ).arg (theWidth).arg (theHeight).arg (theDataType).arg (theNoDataValue) );
62
+ QgsDebugMsg ( QString ( " theWidth= %1 theHeight = %2 theDataType = %3 theNoDataValue = %4" ).arg ( theWidth ).arg ( theHeight ).arg ( theDataType ).arg ( theNoDataValue ) );
63
63
64
- QgsFree (mData );
64
+ QgsFree ( mData );
65
65
mData = 0 ;
66
66
delete mImage ;
67
67
mImage = 0 ;
@@ -72,9 +72,9 @@ bool QgsRasterBlock::reset( DataType theDataType, int theWidth, int theHeight, d
72
72
mNoDataValue = std::numeric_limits<double >::quiet_NaN ();
73
73
// mValid = false;
74
74
75
- if ( typeIsNumeric (theDataType) )
75
+ if ( typeIsNumeric ( theDataType ) )
76
76
{
77
- QgsDebugMsg ( " Numeric type" );
77
+ QgsDebugMsg ( " Numeric type" );
78
78
int tSize = typeSize ( theDataType ) / 8 ;
79
79
mData = QgsMalloc ( tSize * theWidth * theHeight );
80
80
if ( mData == 0 )
@@ -85,23 +85,23 @@ bool QgsRasterBlock::reset( DataType theDataType, int theWidth, int theHeight, d
85
85
}
86
86
else if ( typeIsColor ( theDataType ) )
87
87
{
88
- QgsDebugMsg ( " Color type" );
89
- QImage::Format format = imageFormat ( theDataType );
88
+ QgsDebugMsg ( " Color type" );
89
+ QImage::Format format = imageFormat ( theDataType );
90
90
mImage = new QImage ( theWidth, theHeight, format );
91
91
}
92
92
else
93
93
{
94
- QgsDebugMsg ( " Wrong data type" );
94
+ QgsDebugMsg ( " Wrong data type" );
95
95
return false ;
96
96
}
97
-
97
+
98
98
// mValid = true;
99
99
mDataType = theDataType;
100
- mTypeSize = QgsRasterBlock::typeSize (mDataType );
100
+ mTypeSize = QgsRasterBlock::typeSize ( mDataType );
101
101
mWidth = theWidth;
102
102
mHeight = theHeight;
103
103
mNoDataValue = theNoDataValue;
104
- QgsDebugMsg ( QString (" mWidth= %1 mHeight = %2 mDataType = %3 mData = %4 mImage = %5" ).arg (mWidth ).arg (mHeight ).arg (mDataType ).arg ((ulong)mData ).arg ((ulong)mImage ) );
104
+ QgsDebugMsg ( QString ( " mWidth= %1 mHeight = %2 mDataType = %3 mData = %4 mImage = %5" ).arg ( mWidth ).arg ( mHeight ).arg ( mDataType ).arg (( ulong )mData ).arg (( ulong )mImage ) );
105
105
return true ;
106
106
}
107
107
@@ -112,15 +112,15 @@ QImage::Format QgsRasterBlock::imageFormat( QgsRasterBlock::DataType theDataType
112
112
return QImage::Format_ARGB32;
113
113
}
114
114
else if ( theDataType == QgsRasterBlock::ARGB32_Premultiplied )
115
- {
115
+ {
116
116
return QImage::Format_ARGB32_Premultiplied;
117
117
}
118
118
return QImage::Format_Invalid;
119
119
}
120
120
121
- QgsRasterBlock::DataType QgsRasterBlock::dataType ( QImage::Format theFormat )
121
+ QgsRasterBlock::DataType QgsRasterBlock::dataType ( QImage::Format theFormat )
122
122
{
123
- if ( theFormat == QImage::Format_ARGB32 )
123
+ if ( theFormat == QImage::Format_ARGB32 )
124
124
{
125
125
return QgsRasterBlock::ARGB32;
126
126
}
@@ -133,9 +133,9 @@ QgsRasterBlock::DataType QgsRasterBlock::dataType ( QImage::Format theFormat )
133
133
134
134
bool QgsRasterBlock::isEmpty () const
135
135
{
136
- QgsDebugMsg ( QString (" mWidth= %1 mHeight = %2 mDataType = %3 mData = %4 mImage = %5" ).arg (mWidth ).arg (mHeight ).arg (mDataType ).arg ((ulong)mData ).arg ((ulong)mImage ) );
137
- if ( mWidth == 0 || mHeight == 0 ||
138
- ( typeIsNumeric (mDataType ) && mData == 0 ) ||
136
+ QgsDebugMsg ( QString ( " mWidth= %1 mHeight = %2 mDataType = %3 mData = %4 mImage = %5" ).arg ( mWidth ).arg ( mHeight ).arg ( mDataType ).arg (( ulong )mData ).arg (( ulong )mImage ) );
137
+ if ( mWidth == 0 || mHeight == 0 ||
138
+ ( typeIsNumeric ( mDataType ) && mData == 0 ) ||
139
139
( typeIsColor ( mDataType ) && mImage == 0 ) )
140
140
{
141
141
return true ;
@@ -250,147 +250,153 @@ bool QgsRasterBlock::isNoDataValue( double value ) const
250
250
return false ;
251
251
}
252
252
253
- double QgsRasterBlock::value ( size_t index) const
253
+ double QgsRasterBlock::value ( size_t index ) const
254
254
{
255
- if ( index < 0 || index >= (size_t )mWidth *mHeight )
255
+ if ( index < 0 || index >= ( size_t )mWidth *mHeight )
256
256
{
257
- QgsDebugMsg ( QString (" Index %1 out of range (%2 x %3)" ).arg (index).arg (mWidth ).arg (mHeight ) );
257
+ QgsDebugMsg ( QString ( " Index %1 out of range (%2 x %3)" ).arg ( index ).arg ( mWidth ).arg ( mHeight ) );
258
258
return mNoDataValue ;
259
259
}
260
- return readValue ( mData , mDataType , index );
260
+ return readValue ( mData , mDataType , index );
261
261
}
262
262
263
- double QgsRasterBlock::value ( int row, int column) const
263
+ double QgsRasterBlock::value ( int row, int column ) const
264
264
{
265
- return value ( ( size_t )row*mWidth + column);
265
+ return value (( size_t )row*mWidth + column );
266
266
}
267
267
268
- QRgb QgsRasterBlock::color ( size_t index) const
268
+ QRgb QgsRasterBlock::color ( size_t index ) const
269
269
{
270
- int row = floor ( ( double )index / mWidth );
270
+ int row = floor (( double )index / mWidth );
271
271
int column = index % mWidth ;
272
- return color ( row, column);
272
+ return color ( row, column );
273
273
}
274
274
275
- QRgb QgsRasterBlock::color ( int row, int column) const
275
+ QRgb QgsRasterBlock::color ( int row, int column ) const
276
276
{
277
277
if ( !mImage ) return qRgba ( 255 , 255 , 255 , 0 );
278
278
279
- return mImage ->pixel ( column, row );
279
+ return mImage ->pixel ( column, row );
280
280
}
281
281
282
282
bool QgsRasterBlock::isNoData ( size_t index )
283
283
{
284
- if ( index < 0 || index >= (size_t )mWidth *mHeight )
284
+ if ( index < 0 || index >= ( size_t )mWidth *mHeight )
285
285
{
286
- QgsDebugMsg ( QString (" Index %1 out of range (%2 x %3)" ).arg (index).arg (mWidth ).arg (mHeight ) );
286
+ QgsDebugMsg ( QString ( " Index %1 out of range (%2 x %3)" ).arg ( index ).arg ( mWidth ).arg ( mHeight ) );
287
287
return true ; // we consider no data if outside
288
288
}
289
- double value = readValue ( mData , mDataType , index );
289
+ double value = readValue ( mData , mDataType , index );
290
290
return isNoDataValue ( value );
291
291
}
292
292
293
293
bool QgsRasterBlock::isNoData ( int row, int column )
294
294
{
295
- return isNoData ( ( size_t )row*mWidth + column );
295
+ return isNoData (( size_t )row*mWidth + column );
296
296
}
297
297
298
298
bool QgsRasterBlock::setValue ( size_t index, double value )
299
299
{
300
- if ( !mData )
300
+ if ( !mData )
301
301
{
302
302
QgsDebugMsg ( " Data block not allocated" );
303
303
return false ;
304
304
}
305
- if ( index < 0 || index >= (size_t )mWidth *mHeight )
305
+ if ( index < 0 || index >= ( size_t )mWidth *mHeight )
306
306
{
307
- QgsDebugMsg ( QString (" Index %1 out of range (%2 x %3)" ).arg (index).arg (mWidth ).arg (mHeight ) );
307
+ QgsDebugMsg ( QString ( " Index %1 out of range (%2 x %3)" ).arg ( index ).arg ( mWidth ).arg ( mHeight ) );
308
308
return false ;
309
309
}
310
- writeValue ( mData , mDataType , index, value );
310
+ writeValue ( mData , mDataType , index, value );
311
311
return true ;
312
312
}
313
313
314
314
bool QgsRasterBlock::setValue ( int row, int column, double value )
315
315
{
316
- return setValue ( ( size_t )row*mWidth + column, value );
316
+ return setValue (( size_t )row*mWidth + column, value );
317
317
}
318
318
319
319
bool QgsRasterBlock::setColor ( int row, int column, QRgb color )
320
320
{
321
- return setColor ( ( size_t )row*column, color );
321
+ return setColor (( size_t )row*column, color );
322
322
}
323
323
324
324
bool QgsRasterBlock::setColor ( size_t index, QRgb color )
325
325
{
326
- if ( !mImage )
326
+ if ( !mImage )
327
327
{
328
328
QgsDebugMsg ( " Image not allocated" );
329
329
return false ;
330
330
}
331
331
332
- if ( index >= (size_t )mImage ->width ()* mImage ->height () )
332
+ if ( index >= ( size_t )mImage ->width ()* mImage ->height () )
333
333
{
334
- QgsDebugMsg ( QString (" index %1 out of range" ).arg (index) );
334
+ QgsDebugMsg ( QString ( " index %1 out of range" ).arg ( index ) );
335
335
return false ;
336
336
}
337
-
337
+
338
338
// setPixel() is slow, see Qt doc -> use direct access
339
- QRgb* bits = (QRgb*)mImage ->bits ();
339
+ QRgb* bits = ( QRgb* )mImage ->bits ();
340
340
bits[index] = color;
341
341
return true ;
342
342
}
343
343
344
344
char * QgsRasterBlock::bits ( size_t index )
345
345
{
346
- if ( mData )
346
+ // Not testing type to avoid too much overhead because this method is called per pixel
347
+ if ( index < 0 || index >= ( size_t )mWidth *mHeight )
347
348
{
348
- return (char *)mData + index * mTypeSize ;
349
+ QgsDebugMsg ( QString ( " Index %1 out of range (%2 x %3)" ).arg ( index ).arg ( mWidth ).arg ( mHeight ) );
350
+ return 0 ;
349
351
}
350
- if ( mImage )
352
+ if ( mData )
351
353
{
352
- return (char *) (mImage ->bits () + index * 4 );
354
+ return ( char * )mData + index * mTypeSize ;
355
+ }
356
+ if ( mImage && mImage ->bits () )
357
+ {
358
+ return ( char * )( mImage ->bits () + index * 4 );
353
359
}
354
360
355
361
return 0 ;
356
362
}
357
363
358
364
char * QgsRasterBlock::bits ( int row, int column )
359
365
{
360
- return bits ( ( size_t )row*mWidth + column );
366
+ return bits (( size_t )row*mWidth + column );
361
367
}
362
368
363
369
bool QgsRasterBlock::convert ( QgsRasterBlock::DataType destDataType )
364
370
{
365
371
if ( isEmpty () ) return false ;
366
372
if ( destDataType == mDataType ) return true ;
367
373
368
- if ( typeIsNumeric (mDataType ) && typeIsNumeric (destDataType) )
374
+ if ( typeIsNumeric ( mDataType ) && typeIsNumeric ( destDataType ) )
369
375
{
370
- void *data = convert ( mData , mDataType , destDataType, mWidth * mHeight );
376
+ void *data = convert ( mData , mDataType , destDataType, mWidth * mHeight );
371
377
372
378
if ( data == 0 )
373
379
{
374
- QgsDebugMsg ( " Cannot convert raster block" );
380
+ QgsDebugMsg ( " Cannot convert raster block" );
375
381
return false ;
376
382
}
377
- QgsFree ( mData );
383
+ QgsFree ( mData );
378
384
mData = data;
379
385
}
380
- else if ( typeIsColor (mDataType ) && typeIsColor (destDataType) )
386
+ else if ( typeIsColor ( mDataType ) && typeIsColor ( destDataType ) )
381
387
{
382
388
// It would be probably faster to convert value by value here instead of
383
389
// creating new image, QImage (4.8) does not have any method to convert in place
384
- QImage::Format format = imageFormat ( destDataType );
385
- QImage image = mImage ->convertToFormat ( format );
386
- memcpy ( mImage ->bits (), image.bits (), mImage ->byteCount () );
390
+ QImage::Format format = imageFormat ( destDataType );
391
+ QImage image = mImage ->convertToFormat ( format );
392
+ memcpy ( mImage ->bits (), image.bits (), mImage ->byteCount () );
387
393
// mImage = new QImage( mWidth, mHeight, format );
388
394
}
389
395
else
390
396
{
391
397
return false ;
392
398
}
393
-
399
+
394
400
return true ;
395
401
}
396
402
@@ -409,11 +415,11 @@ bool QgsRasterBlock::setImage( const QImage * image )
409
415
mData = 0 ;
410
416
delete mImage ;
411
417
mImage = 0 ;
412
- mImage = new QImage ( *image );
418
+ mImage = new QImage ( *image );
413
419
mWidth = mImage ->width ();
414
420
mHeight = mImage ->height ();
415
- mDataType = dataType ( mImage ->format () );
416
- mTypeSize = QgsRasterBlock::typeSize (mDataType );
421
+ mDataType = dataType ( mImage ->format () );
422
+ mTypeSize = QgsRasterBlock::typeSize ( mDataType );
417
423
mNoDataValue = std::numeric_limits<double >::quiet_NaN ();
418
424
return true ;
419
425
}
0 commit comments