File: img_image.h

package info (click to toggle)
meshlab 1.3.0a%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 23,416 kB
  • sloc: cpp: 214,034; ansic: 4,493; makefile: 72
file content (512 lines) | stat: -rwxr-xr-x 17,749 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#ifndef IMG_IMAGE_H_
#define IMG_IMAGE_H_
/*! \file img_image.h
    \brief definition of the generic image class

    This header contains the image class definition.
*/

#include "img/img_base.h"
#include "img/img_attributes.h"

namespace img {

/*! \brief the generic image class

  The image class is templated over three parameters:
   - The number of the image channels: an image can have from one to an arbitrary large number of channels, i.e. values per pixel. The default  is to have 3 color channels.
   - The type of the values: the image stores the pixels' values in floating-point variables. The default is to have double floating point precision, the other option is to have single floating point precision. Static assertions are used to ensure that the value type of an image is a floating  point number.
   - The safeness: the image throws runtime exceptions when its member functions are called with "wrong" parameters. This behavior, active by default, can be disabled in order to speed up the computation. The parameter correctness is also independently checked with the dynamic assertions mechanism that can be disabled by compiling the code in "release" mode.

  The image data is packed by the pixel, to optimize multichannel processing efficiency. The image class interface is restricted to pixel and metadata access, processing and I/O are entirely delegated to external functions. The data is accessible in multiple ways: value and pixel wise, using float coordinates with nearest and bilinear interpolation, viewing the image with clamping, direct access to the data and so on.

  An auxiliary structured attribute contains all the metadata information, currently this data consists in the specification of the numeric range of the values, the image color space, the white point and the gamma compression of the image. The various functions checks these data before processing, failing if the image is not compatible with the operation in course.
*/
template<int Channels=3, typename ScalarType=double, bool Safe=true> 
class Image //TODO: Safe=false prima di mettere in vcg
{
private:
  // Data is private but controlled data wiews are
  // accessible from accessors

  /// width of the image
  int _width; 
  /// height of the image
  int _height; 
  /// data buffer
  ScalarType *_data; 

// constructors
public:
  /// the auxiliary structured attribute that contains all the metadata information.
  ImgAttributes<ScalarType> attributes;


  /*! \brief default image constructor

    Creates a 0 x 0 pixel image with no data and default attributes
  */
  Image() // creates a 0 x 0 image
  :_width(0),_height(0),_data(NULL),attributes()
  {
    STATIC_ASSERT( Channels>0 );
    STATIC_FLOAT_OR_DOUBLE_TYPECHECK( ScalarType );
  }

  /*! \brief (deep) copy constructor when all template parameters matches

    An explicit copy constructor is needed because when all template parameters matches the templated copy constructor is not consideredand and a wrong copy constructor is synthetized by compiler.

    \param image the image to be copied
  */
  Image(const Image<Channels,ScalarType,Safe> &image) // copy constructor (deep copy)
  {
    STATIC_ASSERT( Channels>0 );
    STATIC_FLOAT_OR_DOUBLE_TYPECHECK( ScalarType );

    assert(image._width > 0);
    assert(image._height > 0);
    assert(image._data != NULL);
    if(Safe) {
      if(image._width <= 0) throw ImageException("Image(Image): Nonpositive width");
      if(image._height <= 0) throw ImageException("Image(Image): Nonpositive height");
      if(image._data == NULL) throw ImageException("Image(Image): NULL data");     
    }    
    _width = image._width;
    _height = image._height;
    _data = new ScalarType[Channels * _width * _height];
    attributes=image.attributes;

    memcpy(_data, image._data, sizeof(ScalarType) * Channels * _width * _height);
  }

  /*! \brief (deep) copy constructor when some template parameters differs

    \param image the image to be copied
  */
  template<typename OtherScalarType, bool OtherSafe> 
  Image(const Image<Channels,OtherScalarType,OtherSafe> &image) // templated copy constructor (deep copy)
  :_width(0),_height(0),_data(NULL)
  {
    STATIC_ASSERT( Channels>0 );
    STATIC_FLOAT_OR_DOUBLE_TYPECHECK( ScalarType );

    assert(image._width > 0);
    assert(image._height > 0);
    assert(image._data != NULL);
    if(Safe || OtherSafe) {
      if(image._width <= 0) throw ImageException("Image(Image): Nonpositive width");
      if(image._height <= 0) throw ImageException("Image(Image): Nonpositive height");
      if(image._data == NULL) throw ImageException("Image(Image): NULL data");     
    }    
    _width = image._width;
    _height = image._height;
    _data = new ScalarType[Channels * _width * _height];
    attributes=image.attributes;

    if(typeid( ScalarType ) == typeid( OtherScalarType ))
      memcpy(_data, image._data, sizeof(ScalarType) * Channels * _width * _height);
    else
      for(int offset=0;offset< Channels * _width * _height; ++offset)
        _data[offset] = static_cast<ScalarType>(image._data[offset]);
  }

  /*! \brief blank image constructor

    Creates an arg_width x arg_height pixel image with 0-valued data and default attributes
    \param arg_width the width of the blank image
    \param arg_height the height of the blank image
  */
  Image(int arg_width,int arg_height)
  :_width(arg_width),_height(arg_height),_data(NULL),attributes()
  {
    STATIC_ASSERT( Channels>0 );
    STATIC_FLOAT_OR_DOUBLE_TYPECHECK( ScalarType );

    assert(arg_width>0);
    assert(arg_height>0);
    if(Safe) {
      if(arg_width <= 0) throw ImageException("Image(int,int): Nonpositive width");
      if(arg_height <= 0) throw ImageException("Image(int,int): Nonpositive height");
    }
    _data = new ScalarType[Channels * _width * _height];
    for(int offset=0;offset< Channels * _width * _height; ++offset)
      _data[offset] = 0.0f;
  }  

  /// the destructor
  ~Image()
  {
    attributes.reset();
    if(_data!=NULL){
      delete [] _data;
      _data=NULL;
    }
  }

// public functions
public:
  /*! \brief assignment operator (deep copy)

    \param image the image to be assigned to this instance
    \return a pointer to this instance
  */
  template<typename OtherScalarType, bool OtherSafe> 
  inline Image< Channels,ScalarType,Safe> & operator =(const Image<Channels,OtherScalarType,OtherSafe> &image)
  {
    assert(image._width > 0);
    assert(image._height > 0);
    assert(image._data != NULL);
    if(Safe || OtherSafe) {
      if(image._width <= 0) throw ImageException("operator =: Nonpositive width");
      if(image._height <= 0) throw ImageException("operator =: Nonpositive height");
      if(image._data == NULL) throw ImageException("operator =: NULL data");     
    }    
    _width = image._width;
    _height = image._height;
    _data = new ScalarType[Channels * _width * _height];
    attributes=image.attributes;

    if(typeid( ScalarType ) == typeid( OtherSafe ))
      memcpy(_data, image._data, sizeof(ScalarType) * Channels * _width * _height);
    else
      for(int offset=0;offset < Channels * _width * _height; ++offset)
        _data[offset] = static_cast<ScalarType>(image._data[offset]);
    return *this;
  }

  /*! \brief blanks and change the image dimensions

    Delete the current image data and create an arg_width x arg_height pixel image with 0-valued data and default attributes
    \param arg_width the width of the blank image
    \param arg_height the height of the blank image
  */
  inline void setZero(int arg_width, int arg_height)
  {
    assert(arg_width>0);
    assert(arg_height>0);
    if(Safe) {
      if(arg_width <= 0) throw ImageException("setZero: Nonpositive width");
      if(arg_height <= 0) throw ImageException("setZero: Nonpositive height");
    }
    if(_data!=NULL){
      delete [] _data;
      _data=NULL;
    }    
    _width = arg_width;
    _height = arg_height;
    _data = new ScalarType[Channels * _width * _height];
    attributes.reset();
    
    for(int offset=0;offset< Channels * _width * _height; ++offset)
      _data[offset] = 0.0f;
  }

  /*! \brief delete the image data

    Delete the current image data and create an 0 x 0 pixel image with no data.
  */
  inline void deleteData()
  {
    if(_data!=NULL){
      delete [] _data;
      _data=NULL;
    }
    _width = 0;
    _height = 0;    
  } 


  /*! \brief get all the values of a pixel

    \param x the horizontal coordinate of the pixel
    \param y the vertical coordinate of the pixel
    \param ret_pixel return parameter that is filled with the pixel values
  */
  inline void getPixel(int x, int y, ScalarType (& ret_pixel)[Channels]) const
  {
    assert( _data != NULL );
    assert( x >= 0 && x < _width );
    assert( y >= 0 && y < _height );
    if( Safe ){
      if ( _data == NULL ) throw ImageException("getPixel: NULL data");
      if ( !( x >= 0 && x < _width ) ) throw ImageException("getPixel: x out of bounds");
      if ( !( y >= 0 && y < _height ) ) throw ImageException("getPixel: y out of bounds");      
    }
    for (int channel=0;channel<Channels;++channel)
      ret_pixel[channel] = _data[ (x + y * _width) * Channels + channel ];
  }

  /*! \brief set all the values of a pixel

    \param x the horizontal coordinate of the pixel
    \param y the vertical coordinate of the pixel
    \param pixel the pixel values that are assigned to the pixel
  */
  inline void setPixel(int x, int y, const ScalarType (& pixel)[Channels])
  {
    assert( _data != NULL );
    assert( x >= 0 && x < _width );
    assert( y >= 0 && y < _height );
    if( Safe ){
      if ( _data == NULL ) throw ImageException("setPixel: NULL data");
      if ( !( x >= 0 && x < _width ) ) throw ImageException("setPixel: x out of bounds");
      if ( !( y >= 0 && y < _height ) ) throw ImageException("setPixel: y out of bounds");      
    }
    for (int channel=0;channel<Channels;++channel)
      _data[ (x + y * _width) * Channels + channel ] = pixel[channel];
  }

  /*! \brief get a single value of a pixel

    \param x the horizontal coordinate of the pixel
    \param y the vertical coordinate of the pixel
    \param channel the channel index
    \return the value of the channel at the pixel
  */
  inline ScalarType getValue(int x, int y, int channel) const
  {
    assert( _data != NULL );
    assert( x >= 0 && x < _width );
    assert( y >= 0 && y < _height );
    assert( channel >=0 && channel < Channels );
    if( Safe ){
      if ( _data == NULL ) throw ImageException("getFloat: NULL data");
      if ( !( x >= 0 && x < _width ) ) throw ImageException("getFloat: x out of bounds");
      if ( !( y >= 0 && y < _height ) ) throw ImageException("getFloat: y out of bounds");
      if ( !( channel >=0 && channel < Channels ) ) throw ImageException("channel out of bounds");
    }
    return _data[ (x + y * _width) * Channels + channel ];
  }

  /*! \brief set a single value of a pixel

    \param x the horizontal coordinate of the pixel
    \param y the vertical coordinate of the pixel
    \param channel the channel index
    \param value the value that is assigned to the channel at the pixel
  */
  inline void setValue(int x, int y, int channel, ScalarType value)
  {
    assert( _data != NULL );
    assert( x >= 0 && x < _width );
    assert( y >= 0 && y < _height );
    assert( channel >=0 && channel < Channels );
    if( Safe ){
      if ( _data == NULL ) throw ImageException("setFloat: NULL data");
      if ( !( x >= 0 && x < _width ) ) throw ImageException("setFloat: x out of bounds");
      if ( !( y >= 0 && y < _height ) ) throw ImageException("setFloat: y out of bounds");      
      if ( !( channel >=0 && channel < Channels ) ) throw ImageException("channel out of bounds");
    }
    _data[(x + y * _width) * Channels + channel] = value;
  }

  /*! \brief get all the values of a pixel, clamping if the coordinates are out of bounds

    \param x the horizontal coordinate of the pixel
    \param y the vertical coordinate of the pixel
    \param ret_pixel return parameter that is filled with the pixel values
  */
  inline void getPixelAsClamped(int x, int y, ScalarType (& ret_pixel)[Channels]) const
  {
    getPixel(x<0?0:(x<_width?x:_width-1), y<0?0:(y<_height?y:_height-1), ret_pixel);
  }

  /*! \brief get a single value of a pixel, clamping if the coordinates are out of bounds

    \param x the horizontal coordinate of the pixel
    \param y the vertical coordinate of the pixel
    \param channel the channel index
    \return the value of the channel at the pixel
  */
  inline float getValueAsClamped(int x, int y, int channel) const
  {
    return getValue(x<0?0:(x<_width?x:_width-1), y<0?0:(y<_height?y:_height-1),channel);
  }

  /*! \brief get all the values of a pixel, rounding the floating coordinates to the nearest pixel

    \param x the horizontal coordinate of the pixel
    \param y the vertical coordinate of the pixel
    \param ret_pixel return parameter that is filled with the pixel values
  */
  inline void nearestPixel(float x, float y, ScalarType (& ret_pixel)[Channels]) const
  {
  getPixel(static_cast<int>(floor(x+0.5f)),static_cast<int>(floor(y+0.5f)), ret_pixel);
  }

///// chi gli serve se la implementa, ora non c'ho tempo
//  inline void bilinearPixel(float x, float y, const ScalarType[] &pixel) const
//  {
//    assert( _data != NULL );
//    assert( x >= 0.0f && x <= _width-1.0f );
//    assert( y >= 0.0f && y <= _height-1.0f );
//    if( SAFE ){
//      if ( _data == NULL ) throw ImageException("bilinearPixel: NULL data");
//      if ( !( x >= 0.0f && x <= _width-1.0f ) ) throw ImageException("bilinearPixel: x out of bounds");
//      if ( !( y >= 0.0f && y <= _height-1.0f ) ) throw ImageException("bilinearPixel: y out of bounds");      
//    }    
//    
//  int x1 = static_cast<int>(floor(x));
//    int y1 = static_cast<int>(floor(y));
//  float a = 1.0f;
//  float b = 1.0f;
//  
//  if( x1 == _width )
//      x1--;
//  else
//      a = x - x1;
//  if( y1 == _height )
//      y1--;
//  else
//      b = y - y1;
//    
//  float a1 = (1.0f - a);
//    float b1 = (1.0f - b);
//    
//  int i = x1 + y1 * _width;
//
//  return _data[i] * a1 * b1
//         + _data[i + 1] * a * b1 
//         + _data[i + _width] * a1 * b + 
//         _data[i + _width + 1] * a * b;  
//  }

// accessors
public:
  /*! \brief get the width of the image

    \return the width of the image
  */
  inline int width() const
  {
    return _width;
  }

  /*! \brief get the height of the image

    \return the height of the image
  */
  inline int height() const
  {
    return _height;
  }

  /*! \brief set the width of the image

    \warning changing the image size without changing the data buffer accordingly can lead to memory errors
    \param width the new width of the image
  */
  inline void setWidth(int width)
  {
     assert(width > 0);
     if(Safe){
       if(width <= 0) throw ImageException("setWidth: Nonpositive width");
    }
    _width=width;
  }

  /*! \brief set the height of the image

    \warning changing the image size without changing the data buffer accordingly can lead to memory errors
    \param height the new height of the image
  */
   inline void setHeight(int height)
  {
    assert(height > 0);
    if(Safe){
       if(height <= 0) throw ImageException("setHeight: Nonpositive height");
    }
    _height=height;
  }

  /*! \brief get a const pointer to the image databuffer

    \warning this function exposes the internal structure of the image, mind your accesses.
    \return a const pointer to the image databuffer
  */
  inline ScalarType* dataValues() const
  {
    return _data;
  }

  /*! \brief get the size of the image databuffer

    \return the size of the image databuffer
  */
  inline int dataValuesSize() const
  {
    return _width * _height * Channels;
  }


  /*! \brief set the image databuffer

    \warning this function modifies the internal structure of the image!
    \param data a pointer to the new image databuffer
  */
  inline void setValues(ScalarType* data)
  {
    assert(data!=NULL);
    if(Safe){
      if(data == NULL) throw ImageException("setValues: NULL data");
    }
    _data = data;
  }

  /*! \brief checks if the given coordinates are inside the image bounds

    \param x the horizontal coordinate
    \param y the vertical coordinate
    \return true if the given coordinates are inside the image bounds, false otherwise
  */
  inline bool isInside(int x, int y) const
  {
    return x >= 0 && x < _width && y >= 0 && y < _height;
  }

  /*! \brief checks if the given float coordinates are inside the image bounds

    \param x the horizontal coordinate
    \param y the vertical coordinate
    \return true if the given coordinates are inside the image bounds, false otherwise
  */
  inline bool isInside(float x, float y) const
  {
    return x >= 0.0f && x <= _width-1.0f && y >= 0.0f && y <= _height-1.0f;
  }

  /*! \brief checks if the image has been initialized

    \return true if the data is not null and the minimun side is longer than 0 pixel, false otherwise
  */
  inline bool isValid() const
  {
    return _data != NULL && _width > 0 && _height > 0;
  }

  /*! \brief get the number of channels of the image

    \return the number of channels of the image
  */
  inline int channels() const
  {
    return Channels;
  }

  /*! \brief set a value in the image databuffer

    \warning this function modifies the internal structure of the image!
    \param i the index of the databuffer
    \param value the value to set in the i position of the databuffer
  */
  inline void setRawValue(int i, ScalarType value)
  {
     _data[i] = value;
  }
  
};

} //end namespace img

#endif /*IMG_IMAGE_H_*/