Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ImageBase.hh
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #ifndef _IMAGE_BASE_H_INCLUDED_
26 #define _IMAGE_BASE_H_INCLUDED_
27 
28 #include "bias_config.h"
29 #include <exception>
30 #include <string>
31 
32 #ifdef __APPLE__
33 # include <sys/malloc.h>
34 #else // __APPLE__
35 # include <malloc.h>
36 #endif // __APPLE__
37 #include <string.h>
38 #include <stdio.h>
39 
40 #include <Base/Debug/Debug.hh>
41 #include <Base/Debug/Error.hh>
42 #include <Base/Common/CompareFloatingPoint.hh>
43 #include "MetaData.hh"
44 #include "UUID.hh"
45 #include "ROI.hh"
46 
47 #ifdef BIAS_HAVE_OPENEXR
48 # ifdef WIN32
49 # pragma warning( push, 1)
50 # define PLATFORM_WINDOWS
51 # endif // WIN32
52 # include <half.h>
53 # ifdef WIN32
54 # pragma warning( pop)
55 # endif // WIN32
56 #endif
57 
58 
59 // Begin namespace BIAS
60 namespace BIAS
61 {
62 
63 #define MIP_DV_WIDTH 500
64 #define MIP_DV_HEIGHT 288
65 
66 #define D_COPYIN 1<<1
67 #define D_CONVERT 1<<2
68 #define D_CAST_CHECK 1<<3
69 #define D_MI_TOPLANAR 1<<4
70 #define D_MI_IOSTREAM 1<<5
71 #define D_MI_CONVGREY 1<<6
72 
73  // global debug level
74 #define D_IMAGE_TRACE 1<<25
75 
76 #define VERSION_NUMBER 320
77 
78 #define IDENTIFIER "MIP"
79 #define IDENTIFIER_LENGTH 3
80 
81  // used only by Display float to Byte range conversion
82 #define DEFAULT_32to8_scale 255.0f
83  // used by Display if waitforkey is specicied
84 #define DEFAULT_Display_delay 3000
85  // used by Display routine
86 #define DEFAULT_WrapBias2Ipl_WINNAME "WrapBias2Ipl_0"
87 
88  // forward declarations
89  class BIASImageBase_EXPORT ImageConvert;
90  template<class StorageType>
91  class BIASImageBase_EXPORT Image;
92 
93  /** @class ImageBase
94  @ingroup g_image_base
95  \brief This is the base class for images in BIAS.
96 
97  It is a non template class, so you can write image type
98  independet functions.
99  Use this class if you deal with different storage types or
100  you don't know the storage type, eg. when loading an image from disk.
101  */
102  class BIASImageBase_EXPORT ImageBase : public Debug
103  {
104  friend class ImageConvert;
105  friend class ImageIO;
106  friend class WrapBias2Ipl; //JW
107 
108  public:
110  {
111  ST_invalid = -1, ///< invalid not set image storage type
112  ST_unsignedchar, ///< (8bit) unsigned char image storage type
113  ST_char, ///< (8bit) signed char image storage type
114  ST_unsignedshortint, ///< (16bit) unsigned integer image storage type
115  ST_shortint, ///< (16bit) signed integer image storage type
116  ST_unsignedint, ///< (32bit) unsigned integer image storage type
117  ST_int, ///< (32bit) signed integer image storage type
118  ST_float, ///< float image storage type
119  ST_double
120  ///< double image storage type
121  };
122 
123  /** @enum EColorModel
124  \brief These are the most often used color models.
125  Determines the data storage size per channel per pixel.
126  */
128  {
129  CM_invalid = -1, ///< invalid (not set) image format
130  CM_Grey, ///< gray values, 1 channel
131  CM_RGB, ///< color values, 3 channels, order: red,green,blue
132  CM_BGR, ///< color values, 3 channels, order: blue,green,red
133  CM_YUYV422, ///< YUYV422, 2 channels, full luminance Y, subsampled half U,V
134  CM_UYVY422, ///< UYVY422, 2 channels, full luminance Y, subsampled half U,V inverse order
135  CM_YUV420P, ///< YUV420P, 2 channels, full luminance Y, 1 U, 1 V. Y, U and V are grouped together for better compression
136  CM_YUV444, ///< CM_YUV444, 3 channels, all channels have full data
137  CM_YUV411, ///< YUV411, 2 channles, full luminance, 1 U, 1 V.
138  CM_HSV, ///< HSV, 3 channels, order: hue, sat , value
139  CM_HSI_OBS, ///< obsolete, HSI is unused and identical to HSL
140  CM_DV, ///< DV, color model used for digital video cameras such as Mini-DV
141  CM_RGBA, ///< RGBA, 4 channels, order: red,green,blue,alpha
142  CM_GreyA, ///< GreyA, 2 channels, grey plus Alpha
143  CM_Bayer_RGGB, ///< Bayer_RGGB, 1 channel RGB image Bayer tile
144  CM_Bayer_GBRG, ///< Bayer_GBRG, 1 channel RGB image Bayer tile
145  CM_Bayer_GRBG, ///< Bayer_GRBG, 1 channel RGB image Bayer tile
146  CM_Bayer_BGGR, ///< Bayer_BGGR, 1 channel RGB image Bayer tile
147  CM_HSL, ///< HSL, similar to HSV but space is a double tipped cone
148  CM_hsL, ///< hsl, similar to HSL but euclidean (h,s) for CNCC
149  CM_SymTensor2x2,///< SymTensor2x2 The image contains a 2x2 symmetric tensor
150  CM_BGRA, ///< BGRA color values, 4 channels, order: blue,green,red,alpha
151  CM_RGBE, ///< RGBE color values, 4 channels, RADIANCE hdr format, four low dynamic channels meaning: 3x mantissa (red,green,blue) +1x (exponent)
152  CM_PGR_XB3_F7M3_GBRG, ///< PGR XB3 in format 7 mode 3 delivers an image that consists of 3 channels with 8bbp (overal 24bpp), each channel codes an whole color image in GBRG bayer pattern, ch0 = left image, ch1 = right image, ch3 = center image
153  CM_DepthAndVariance, ///< Todo: Unclear, I think one channel float, why isn't grey used for that?
154  CM_YUYV, ///< Todo: Conflict with YUVU model, what does it do?
155  CM_LUV, ///< CIELUV color space, 3 channels, http://en.wikipedia.org/wiki/CIELUV_color_space
156  CM_XYZ, ///< XYZ, 3 channels, http://en.wikipedia.org/wiki/Xyz_color_space
157  CM_LAB, ///< LAB, 3 channels, http://en.wikipedia.org/wiki/Lab_color_space
158  CM_Disparity, ///< Disparity images Q: should disp and depth be treated separately, if not what would be a good name to cover both?
159  CM_Depth, ///< Depth images A: separated for now.
160  CM_I1I2I3, ///< Othe's principle component generalization for RGB based segmentation
161  CM_DOES_NOT_EXIST
162  //< end of list marker for loops. >= is invalid
163  };
164 
165  ImageBase();
166  virtual
167  ~ImageBase();
169  ImageBase(const ImageBase& im);
170 
171  /** @brief Constructor, initializing image size and channels
172  @author Felix Woelk
173  @status tested
174  @param width image width of the new image
175  @param height image height of the new image
176  @param channels number of color planes
177  @note Example: Defining an RGB image.
178  @verbatim
179  ImageBase im;
180  im.init(width, height, 3);
181  @endverbatim **/
182  ImageBase(unsigned int width, unsigned int height, unsigned int channels =
183  1, enum EStorageType storageType = ST_unsignedchar,
184  const bool interleaved = true);
185 
186  /** virtual covariant copy constructor which produces the same type of
187  object as the template object (Image, Camera, ...).
188  Must be implemented in all derived classes and since some people do
189  work with ImageBase objects, it is also implemented there.
190  The caller is responsible to delete the clone.
191  @author koeser 06/2006 */
192  virtual ImageBase*
193  Clone() const
194  {
195  return new ImageBase(*this);
196  }
197 
198  /** @brief (Re-)Initialize Image data if required.
199  Contains logic to re-allocate only if required.
200  Keep existing data area if data layout fits.
201  The idea is to reuse a previously initialized image
202  with no need to check it has been initialized exactly once.
203  @author Jan Woetzel */
204  void
205  ReInit(const unsigned int & width, const unsigned int & height,
206  const unsigned int nChannels = 1, const enum EStorageType storageType =
207  ST_unsignedchar, const bool interleaved = true,
208  const EColorModel colormodel = CM_Grey);
209 
210  /** @brief Initialize image size and channels
211 
212  @warning call this only for empty images,
213  calls new for the image data array. Overwrites the color model with
214  some kind of best guess
215  @author Felix Woelk
216  @status tested
217  @param width image width of the new image
218  @param height image height of the new image
219  @param channels number of color planes **/
220  void Init(unsigned int width, unsigned int height, unsigned int nChannels=1,
221  enum EStorageType storageType = ST_unsignedchar,
222  const bool interleaved=true);
223 
224  /** @brief Initialize image size and channels using a foreign data pointer
225 
226  @warning call this only for empty images,
227  calls new for the image data array. Overwrites the color model with
228  some kind of best guess, data is not released!
229  @author Stefan Reinhold
230  @status untested
231  @param width image width of the new image
232  @param height image height of the new image
233  @param channels number of color planes
234  @param data pointer to image data
235  @param shoudRelease if true the ownership of data is transfered to
236  ImageBase, i.e. ImageBase will release data, otherwise not **/
237  void InitWithForeignData(unsigned int width, unsigned int height,
238  unsigned int nChannels,
239  void* data,
240  enum EStorageType storageType = ST_unsignedchar,
241  const bool interleaved = true,
242  const bool shouldRelease = true);
243 
244  /** @brief check if ImageData_ points to allocated image buffer or not */
245  inline bool IsEmpty() const
246  { return (ImageData_ == NULL); }
247 
248  /** @brief Take some data and fill it into the Image
249  @warning Image must be initialized already !
250  @author Evers **/
251  void CopyIn_NoInit(void *data);
252 
253  /** @brief Free the allocated data structures
254  Hands off: Do !!NOT!! change the default of reset_storage_type:
255  ImageBase::Release() is also implicitly called for templated images
256  when releasing the storage type here, it becomes possible to assign
257  a float image to an unsigned char image via ImageBase:
258  Image<float> f;
259  Image<unsigned char> uc;
260  ImageBase *ibf = dynamic_cast<ImageBase *>(f);
261  ImageBase *ibuc = dynamic_cast<ImageBase *>(uc);
262  (*ibf) = (*ibuc); */
263  void Release(const bool reset_storage_type = false);
264 
265  /** @brief steals the image data array from source,
266  after releasing the actual image data
267  and sets source image data to NULL
268 
269  @warning Important for template: Steal images only, if u know the
270  storageTypes are euqal!
271  @author Daniel Grest
272  @status tested */
273  int StealImage(ImageBase &source);
274 
275  /////////////////////Get Functions ///////////////////////////////////////
276  /** @name Get functions
277  @{
278  */
279  inline const void*
280  GetImageData() const
281  {
282  return ImageData_;
283  }
284  ;
285 
286  inline void*
288  {
289  return ImageData_;
290  }
291  ;
292 
293  /** @brief Get an array of pointers to image data.
294 
295  A pixel can be accessed by
296  GetImageDataArray()[row][col] or GetImageDataArray()[y][x] <br>
297  For multi channel images with interleaved data order, access with
298  GetImageDataArray()[row][col * GetChannelCount() + Channel] <br>
299  For multi channel images with planar data order, access with
300  GetImageDataArray()[row + GetHeight * Channel][col] <br>
301 
302  @author Felix Woelk
303  @return array with pointer to the first pixels in every row. **/
304  inline void**
306  {
307  return ImageDataArray_;
308  }
309  ;
310 
311  inline unsigned int
312  GetWidth() const
313  {
314  return Width_;
315  }
316  ;
317 
318  inline unsigned int
319  GetHeight() const
320  {
321  return Height_;
322  }
323  ;
324 
325  /** returns the bytes per channel, which is the sizeof(StorageType)
326  Should match GetSizeDepth(GetStorageType()). */
327  inline unsigned int
328  GetDepth() const
329  {
330  return Depth_;
331  }
332  ;
333  inline unsigned int
334  GetByteDepth() const
335  {
336  return GetDepth();
337  }
338  ;
339 
340  /** returns the bits per channel
341  Is not necessairily 8*sizeof(StorageType), could be fewer bits, eg. 12/16.
342  @author woelk 12/2004 */
343  inline unsigned int
344  GetBitDepth() const
345  {
346  return BitDepth_;
347  }
348  ;
349 
350  /** returns the nr. of image data Bytes - NOT the dimension */
351  inline unsigned int
352  GetSizeByte() const
353  {
354  return GetPixelCount() * Depth_ * ChannelCount_;
355  }
356  ;
357 
358  /** returns the image size in bytes = count - NOT the dimension
359  DEPRECATED Please use GetSizeByte instead to avoid storage type confusion on pointers and char/wideChar unicode */
360  inline unsigned int
361  GetSize() const
362  {
363  return GetSizeByte();
364  }
365  ;
366 
367  /** @return true if the image dimension w x h of this and other
368  match = are equal, values of descriptions Fit.
369  @author JW */
370  bool
371  DimensionMatch(const BIAS::ImageBase & other) const;
372 
373  /** @return true if (all) format descriptions between this and dest image match/fit are equal.
374  @author JW */
375  bool
376  FormatMatch(const BIAS::ImageBase & d) const;
377 
378  /** @brief returns the number of Color channels, e.g. RGB 3
379 
380  the value returned is independent of the StorageType */
381  inline unsigned int
383  {
384  return ChannelCount_;
385  }
386  ;
387 
388  /** @brief returns the number of bytes per line
389 
390  returns the number of bytes per line, not the number of StorageTypes
391 
392  interleaved data order: = Width * Depth * ChannelCount
393  planar data order: = Width * Depth
394 
395  Useful for memory aligned images (e.g. 4/8 Byte)
396  and packing of textures.
397  JW
398  */
399  inline unsigned int
400  GetWidthStep() const
401  {
402  return WidthStep_;
403  }
404  ;
405 
406  inline enum EColorModel
407  GetColorModel() const
408  {
409  return ColorModel_;
410  }
411  ;
412 
413  inline enum EStorageType
414  GetStorageType() const
415  {
416  return StorageType_;
417  }
418  ;
419 
420  /** @brief returns number of pixels in image */
421  inline unsigned long int
423  {
424  return Width_ * Height_;
425  }
426  ;
427 
428  /**
429  returns the Position (index from array start) in ImageData
430  Warning: until 2005-01-04 the position inbyte was returned
431  but used as position in <StorageType> (ImageDraw).
432  Changed this method to return position in StorageType
433  Jan-Friso Evers
434  */
435  inline unsigned int
436  GetPixelPosition(unsigned int x, unsigned int y,
437  unsigned short int channel = 0) const;
438 
439  /** copies one specific channel from source to Image
440  can only be called from an planar image, so call ToPlanar() before
441  so far only implemented for CM_YUYV422, BGR and RGB
442  @author Felix Woelk
443  @status untested (17/04/2002) */
444  int
445  GetChannel(const ImageBase & source, const unsigned int channel);
446 
447  /** @brief returns the UUID of the image */
448  inline const BIAS::UUID &
449  GetUID() const
450  {
451  return _UID;
452  }
453  ;
454 
455  inline MetaData *
457  {
458  return &_MetaData;
459  }
460  ;
461 
462  inline const MetaData *
463  GetMetaData() const
464  {
465  return &_MetaData;
466  }
467  ;
468 
469  inline void
471  {
472  _MetaData = m;
473  }
474  ;
475 
476  inline int
478  {
479  return Version_;
480  }
481  ;
482 
483  inline bool
484  IsPlanar() const
485  {
486  return (!InterleavedDataOrder_);
487  }
488  ;
489 
490  inline bool
492  {
493  return (InterleavedDataOrder_);
494  }
495  ;
496 
497  friend BIASImageBase_EXPORT
498  std::ostream&
499  operator<<(std::ostream& os, const ImageBase &img);
500  friend BIASImageBase_EXPORT
501  std::istream&
502  operator>>(std::istream& is, ImageBase& img);
503 
504  /** @brief print the (typed) pixel value to stream.
505  Implemented using a big switch with casting to BIAS::Image internally
506  to work with all image formats w.r.t StorageTypes, planar, interleaved etc.
507  Jan Woetzel*/
508  static void
509  PrintPixelValue(const ImageBase & im, const unsigned int x,
510  const unsigned int y, const unsigned short channel = 0,
511  std::ostream & os = std::cout);
512 
513  //@}
514 
515  /** Determines the internale ImageBase type and casts it to the output type.
516  * \attention im and input coordinates are not checked for validity!
517  * Only for interleaved images.
518  **/
519  template<class castType>
520  static castType GetValue(const ImageBase & im, const unsigned int x,
521  const unsigned int y, const unsigned int channel = 0);
522 
523  /** Determines the internal ImageBase type and casts the input type to the type foreseen in im.
524  * \attention im and input coordinates are not checked for validity!
525  * Only for interleaved images.
526  **/
527  template<class inputType>
528  static void SetValue(const ImageBase & im,
529  const unsigned int x,
530  const unsigned int y,
531  const unsigned int channel,
532  const inputType val);
533 
534  /** Copy channel, determines the internal ImageBase type and casts the input type to the type foreseen in im.
535  * \attention im and channel both have to be initialized and of compatible size!
536  * Only for interleaved images.
537  **/
538  template<class inputType>
539  static void SetChannel(const ImageBase & im,
540  const unsigned int channelId,
541  const inputType* channelIn);
542 
543  /** Copy channel, determines the internal ImageBase type and casts it to the output type.
544  * \attention im and channel both have to be initialized and of compatible size!
545  * Only for interleaved images.
546  **/
547  template<class outputType>
548  static void GetChannel(const ImageBase & im,
549  const unsigned int channelId,
550  outputType* channelOut);
551 #ifdef BIAS_HAVE_OPENEXR
552  static void GetChannel(const ImageBase & im,
553  const unsigned int channelId,
554  half* channelOut);
555 #endif
556 
557  /** @name Set functions
558  @{
559  */
560  inline void
562  {
563  ColorModel_ = Model;
564  }
565  ;
566 
567  inline void
568  SetInterleaved(bool interleaved)
569  {
570  InterleavedDataOrder_ = interleaved;
571  if (interleaved)
572  WidthStep_ = Width_ * Depth_ * ChannelCount_;
573  else
574  WidthStep_ = Width_ * Depth_;
575  UpdateImageDataArrayFromImageData_();
576  }
577  ;
578 
579  /// needed by ImageIO
580  inline void
581  SetBitDepth(unsigned bitdepth)
582  {
583  BIASASSERT(bitdepth<=(8*Depth_));
584  BitDepth_ = bitdepth;
585  }
586  ;
587 
588  inline void
589  SetUID(const BIAS::UUID& id)
590  {
591  _UID = id;
592  }
593  ;
594 
595  /** @brief sets the image's uid to invalid */
596  inline void
598  {
599  _UID.Invalidate();
600  }
601  ;
602 
603  //@}
604 
605 
606  /** @name Region of Interest Functions
607  Important: the ROI has a size of (lrx-ulx)*(lry-uly) that means
608  the lower right point is not part of the ROI!
609  @{
610  */
611 
612  /** Returns a pointer to the roi object.
613  @author woelk 07/2005 */
614  inline ROI*
616  {
617  return &Roi_;
618  }
619 
620  inline const ROI*
621  GetROI() const
622  {
623  return &Roi_;
624  }
625 
626  /** @brief deprecated, use SetROICorners() */
627  int
628  SetROI(unsigned int UpperLeftX, unsigned int UpperLeftY,
629  unsigned int LowerRightX, unsigned int LowerRightY);
630 
631  int
632  SetROI(const ROI& roi);
633 
634 
635  int
636  SetROICorners(unsigned int UpperLeftX, unsigned int UpperLeftY,
637  unsigned int LowerRightX, unsigned int LowerRightY);
638 
639  /** @brief deprecated, use GetROI()->UnsetROI() */
640  void
641  UnsetROI();
642 
643  /** @brief deprecated, use GetROICorners() */
644  void
645  GetROI(unsigned int& UpperLeftX, unsigned int& UpperLeftY,
646  unsigned int& LowerRightX, unsigned int& LowerRightY) const;
647  /** @brief deprecated, use GetROICorners() */
648  void
649  GetROI(int& UpperLeftX, int& UpperLeftY, int& LowerRightX,
650  int& LowerRightY) const;
651 
652  /** access region of interest rectangle JW */
653  void
654  GetROICorners(unsigned int& UpperLeftX, unsigned int& UpperLeftY,
655  unsigned int& LowerRightX, unsigned int& LowerRightY) const;
656  /** access region of interest rectangle JW */
657  void
658  GetROICorners(int& UpperLeftX, int& UpperLeftY, int& LowerRightX,
659  int& LowerRightY) const;
660 
661  /** @brief deprecated, use 'os << *GetROI()' */
662  void
663  PrintROI(std::ostream& os = std::cout) const;
664 
665  /** @brief returns a copy of ROI (a new image) lower right point excluded,
666  only interleaved images !
667  @author Felix Woelk, grest
668  @status untested **/
669  int
670  GetCopyOfROI(ImageBase& copy) const;
671 
672  /** @brief like GetCopyOfROI, but with lower right point included
673  only interleaved images !
674  @note GetCopyOfROI returns an image where the lower right ROI point
675  is not included. This version does it. Correction are not made in
676  the original method, because extensive corrections would become
677  necessary in other methods.
678  @author Ingo Thomsen **/
679  int
680  GetCopyOfROI2(ImageBase& copy) const;
681 
682  /** @brief paste Image to current ROI
683  @author Felix Woelk 02 2003 **/
684  int
685  Paste2ROI(const ImageBase& Image);
686 
687  /** @brief reduces image to current ROI,
688  !!! image size changes !!!
689  @author Felix Woelk
690  @status untested
691  @todo Make it work on colored Images **/
692  int
693  Cut2ROI();
694 
695  /** @brief sets all pixel not in ROI to zero
696  @author woelk 10/2004 */
697  void
698  SetOutsideROIZero();
699 
700  inline const bool
701  IsInROI(const double &x, const double& y) const
702  {
703  return Roi_.IsInROI(x, y);
704  }
705 
706  /// deprecated, use GetROI()->GetCorners()
707  const unsigned int
708  GetROIUpperLeftX() const;
709 
710  /// deprecated, use GetROI()->GetCorners()
711  const unsigned int
712  GetROIUpperLeftY() const;
713 
714  /// deprecated, use GetROI()->GetCorners()
715  const unsigned int
716  GetROILowerRightX() const;
717 
718  /// deprecated, use GetROI()->GetCorners()
719  const unsigned int
720  GetROILowerRightY() const;
721 
722  //@}
723 
724  /** @name Operators
725  @{
726  */
727  /** @brief assignment operator,
728  allocates memory structure via Init only if necessary
729  @author Felix Woelk **/
730  ImageBase&
731  operator=(const ImageBase& Source);
732 
733  /** @brief comparison operator,
734  based upon the adresses of the actual image data
735  @author Ingo Thomsen
736  @status tested **/
737  inline bool
738  operator==(const ImageBase &img) const
739  {
740  return (GetImageData() == img.GetImageData());
741  }
742 
743  ///@}
744 
745 
746  /** @name Padding and Appending
747  @{
748  */
749 
750  /** @brief flips the image vertically (row order is inverted)
751  In place function
752  return 0 in case of success, -1 otherwise
753  @author Daniel Grest
754  @date Jun 2002
755  @status Interleaved tested, planar untested
756  Aug 2002: Jan-Friso Evers: fixed interleaved for float and other */
757  int
758  Flip();
759 
760  /** @brief flips the image horizontal (column order is inverted)
761  In place function
762  return 0 in case of success, -1 otherwise
763  @author woelk 10/2004 */
764  int
765  FlipHorizontal();
766 
767  /// interface for the above JW
768  inline int
770  {
771  return FlipHorizontal();
772  }
773  ;
774 
775  /* enlarge image size keeping old pixel data 1:1
776  Fill new pixel with padVal which may be zero (for black).
777  @author Jan Woetzel */
778  int
779  Pad(BIAS::ImageBase & dest, const unsigned int & newwidth,
780  const unsigned int & newheight, const int & padVal = 0) const;
781 
782  /* in place Padding @author Jan Woetzel */
783  int
784  Pad(const unsigned int & newwidth, const unsigned int & newheight,
785  const int & padVal = 0);
786 
787  /** backward compatibility interface for Pad.
788  DEPRECATED
789  @author Jan Woetzel 2005 */
790  int
791  ZeroPad(const unsigned int newwidth, const unsigned int newheight,
792  unsigned char bgcolor = 0);
793 
794  /** increase the size of this image to next power of two
795  (e.g. 384x100>512x128)
796  the original image S is set to upper left rectangle
797  @param padVal used by memset to pad the right and lower added pixels
798  No scale, just padding!
799  \verbatim
800  orig xx
801  xxxxxxx
802  \endverbatim
803  @author Jan Woetzel 09/2003 - 11/2005 */
804  int
805  PadToPowerOfTwo(BIAS::ImageBase & dest, const int & padVal = 0) const;
806 
807  /// in place version of the above @author JW
808  int
809  PadToPowerOfTwo(const int & padVal = 0);
810 
811  /** first pad, then flip.
812  useful for OpenGL texture image which must be pow2 size and
813  Y-flipped ics.
814  example:
815  \verbatim
816  0 1 2
817  3 4 5
818  -->
819  x x x x
820  x x x x
821  3 4 5 x
822  0 1 2 x
823  \endverbatim
824  \todo more efficient in place with memmove
825  @author Jan Woetzel */
826  int
827  PadToPowerOfTwoAndFlip(const int & padVal = 0);
828 
829  /// @}
830 
831 
832  /** @name Misc functions
833  @{ */
834 
835  /** @brief checks if data area has same "size" as Image of other type
836  */
837  inline bool
838  SamePixelAndChannelCount(const ImageBase& Image) const;
839 
840  /** @brief checks if data area has bigger or the same "size" as Image
841  of other type
842  */
843  inline bool
844  NotBiggerPixelAndSameChannelCount(const ImageBase& Image) const;
845 
846  /** @brief Releases ImageData_
847  (to be used together with RedirectImageDataPointer)
848  @author Felix woelk */
849  inline void
850  ReleaseImageDataPointer();
851 
852  /** @brief This method takes data and set the internal
853  image data pointer to this.
854 
855  You should really know what you are doing using this method.
856  Don't say you haven't been warned.
857  @author Friso Evers **/
858  inline void
860  {
861  ImageData_ = data;
862  UpdateImageDataArrayFromImageData_();
863  }
864  ;
865 
866  /** @brief set data pointer to NULL, such that image IsEmpty
867  _without_ deletion of data.
868  @author evers
869  */
870  inline void
872  {
873  ImageData_ = NULL;
874  delete[] ImageDataArray_;
875  ImageDataArray_ = NULL;
876  }
877 
878  /** @return true if image size is a power of two JW */
879  bool
880  IsPowerOfTwoSize() const;
881  static bool
882  IsPowerOfTwoSize(const unsigned int w, const unsigned int h);
883 
884  /** @return next pow2 >= val JW */
885  static unsigned int
886  PowerOfTwoSize(const unsigned int & val);
887 
888  /** @return true if both, width and height are already a power of two.
889  The next power of two size >= actual size is computed and stored
890  in args.
891  useful for OpenGL texture enlargement for 'power of two' textures.
892  @author Jan Woetzel 09/2003 */
893  bool
894  PowerOfTwoSize(unsigned int & width2, unsigned int & height2) const;
895 
896  void *
897  PixelValueBase(unsigned x, unsigned y, unsigned channel = 0);
898 
899  void
900  PrintHeader(std::ostream& os = std::cout) const;
901 
902  /// zeroes the image
903  inline void
904  SetZero();
905 
906  /** @brief check if image contains that pixel position */
907  inline bool
908  IsPositionInImage(const int& x, const int& y) const;
909 
910  /** @brief find closest valid pixel position to x,y
911  @author koeser 01/2005
912  @return 0=was correct 3,4=y out of range, 1,2 x out of range */
913  inline int
914  MoveToClosestPositionInImage(int& x, int& y) const;
915 
916  /** @brief transfer BIAS image coordinates [0..w-1] x [0..h-1]
917  to GL texture coordinates [0..1[ x [0..1[
918 
919  GL coordinates are relative to image size of *this (must be valid) !
920 
921  Please note that this is only one of many possible bias to gl coord
922  transformations which may be particularly different for pow2, non-pow2,
923  rectangle, padded, scaled, bordered, offset etc. OpenGL textures. */
924  inline void
925  BIASToTextureCoordinates(const double& biasx, const double& biasy,
926  double& gl_x, double& gl_y) const;
927 
928  /** @brief transfer GL texture coordinates [0..1[ x [0..1[
929  to BIAS image coordinates [0..w-1] x [0..h-1]
930 
931  GL coordinates are relative to image size of *this (must be valid) !
932 
933  Please note that this is only one of many possible bias to gl coord
934  transformations which may be particularly different for pow2, non-pow2,
935  rectangle, padded, scaled, bordered, offset etc. OpenGL textures. */
936  inline void
937  TextureToBIASCoordinates(const double& gl_x, const double& gl_y,
938  double& biasx, double& biasy) const;
939 
940  /** get the size in Byte of the type corresponding to a storagetype
941  uses sizeof(type) internally.
942  Useful for allocation calculations.
943  @author Jan Woetzel */
944  static int
945  GetSizeByte(const enum BIAS::ImageBase::EStorageType & storagetype);
946 
947  /** get the number of (packed) data values of color model
948  E.g:
949  CM_Grey : 1
950  CM_YUYV422 : 2 packs 3 channel YUV into 2 Bytes
951  CM_RGB : 3
952  CM_BGRA : 4
953  CM_Bayer_GBRG : 1 packed Bayer pattern
954  CM_PGR_XB3_F7M3_GBRG : 3 packed three Bayer patterns w. 9 values in tri-image
955  Useful for allocation calculations.
956  Returns the number of data units, not Bytes!
957  @author Jan Woetzel */
958  static int
959  GetSizeUnits(const enum BIAS::ImageBase::EColorModel & colormodel);
960 
961  /** get the number of channels corresponding to the enum ColorModel
962  Determines the number of (packed) data values.
963  Is typically consistent with GetSizeByte.
964  E.g.
965  CM_Grey : 1
966  CM_YUYV422 : packs 3 channel YUV into 2 Bytes
967  CM_RGB : 3
968  CM_BGRA : 4
969  CM_Bayer_GBRG : 1 packs 3 channel RGB into 1 Byte Bayer pattern
970  @author Jan Woetzel */
971  static int
972  GetChannelcount(const enum BIAS::ImageBase::EColorModel & colormodel);
973 
974  /** @brief computes the storage data size in Byte required for a given video format.
975  @author Jan Woetzel */
976  static int
977  GetStorageSizeByte(const unsigned int & width, const unsigned int & height,
978  const unsigned int nChannels, const enum EStorageType storageType
979  /*,const EColorModel colormodel*/);
980 
981 #ifdef BIAS_HAVE_OPENCV
982 
983  /// OpenCV onscreen popup display, very useful for fast debugging. (JW)
984 
985  /** display image as popup to screnn sing OpenCv IplImage using Highgui functions.
986  Very useful to easily display an image, e.g. for debugging.
987  Can be used inside MSVS debugger watch to inspect intermediate Images wit a popup.
988  Can be used with waiting for user interaction (for delayMsec).
989  @return positive nr. of pressed key or negative if no key pressed or error occured.
990  @param waitForKey if true delayMsec is waited for keypres. 0=forever.
991  @param delayMsec if waitForKey is true wait for key with 0=forever, see cvWaitkey.
992  @return pressed key (if any)
993  @author Jan Woetzel 2005 */
994  int
995  Display(const std::string & DestWin, const bool & autoresize,
996  const bool & moveToTopLeft, const bool & waitForKey,
997  const unsigned int & delayMsec = DEFAULT_Display_delay,
998  const float & scale = DEFAULT_32to8_scale,
999  const bool & allowAlphaWindow = false) const;
1000 
1001  /** simpler interface for the above.
1002  shows image in window named "DestWin" without. Does not wait for key,
1003  you may want to use WrapBias2Ipl::WaitKey after calling this.
1004  */
1005  int
1006  Display(const std::string & DestWin) const;
1007 
1008  /** interface for the above @author Jan Woetzel 2005 */
1009  int
1010  Display(const bool & waitForKey, const unsigned int & delayMsec,
1011  const float &scale) const;
1012 
1013  /** @brief display image simple signature interface for Debugger usage
1014  This function can be used to display an (intermediate) image
1015  "interactively" with the MS Visual Studio Debugger
1016  in "Watch+Debug" expert mode.
1017  This signature is required because MSVS debugger
1018  does not support default params.
1019  @author Jan Woetzel */
1020  int
1021  Display() const;
1022 
1023 #endif // BIAS_HAVE_OPENCV
1024 
1025  static void StringToColorModel(const std::string& str,
1027  static void StringToStorageType(const std::string& str,
1029 
1030 
1031  // @}
1032 
1033 
1034 
1035  protected:
1036  /** @brief recomputes pointer array returned by GetImageDataArray */
1037  int
1038  UpdateImageDataArrayFromImageData_();
1039 
1040  /** changes StorageType data mmeber */
1041  inline void
1043  {
1044  StorageType_ = st;
1045  }
1046 
1047  // ---------------------------------------
1048  // --- data members:
1049  // ---------------------------------------
1050 
1051  /// version number of imagebase class
1052  static const int Version_ = VERSION_NUMBER;
1053  /// the color model of the pixels
1054  enum EColorModel ColorModel_;
1055  /// the storage type in a pixel channel
1056  enum EStorageType StorageType_;
1057  /// number of channels per pixel
1058  unsigned int ChannelCount_;
1059  /// size of one channel of one pixel in bytes
1060  unsigned int Depth_;
1061  /// relevant bits per pixel per channel
1062  unsigned int BitDepth_;
1063  /// image width in pixels
1064  unsigned int Width_;
1065  /// image height in pixels
1066  unsigned int Height_;
1067  /// planar or interleaved: planar means we have several image planes,
1068  /// e.g. for a 3x3 RGB image data is RRRRRRRRRGGGGGGGGGGBBBBBBBB
1069  /// while interleaved means RGBRGBRGBRGBRGBRGBRGBRGBRGB
1071  /// size of a line in bytes for interleaved, NOT nr . of pixels (but size a line in one plane for planar)
1072  unsigned int WidthStep_;
1073  /// array of pointers to the first byte in an image row
1075  /// a pointer to the image data
1076  void* ImageData_;
1077  /// unique id for every image
1079  /// additional data block, handled by derived classes
1081  /// roi object
1083  /// shoud ImageBase release the data pointer?
1085 
1086  }; // end of class
1087 
1088  //// fixes for building dlls
1089  BIASImageBase_EXPORT std::ostream&
1090  operator<<(std::ostream& os, const ImageBase::EStorageType &st);
1091  BIASImageBase_EXPORT std::ostream&
1092  operator<<(std::ostream& os, const ImageBase::EColorModel &st);
1093  BIASImageBase_EXPORT std::ostream&
1094  operator<<(std::ostream& os, const ImageBase &img);
1095  BIASImageBase_EXPORT std::istream&
1096  operator>>(std::istream& is, ImageBase& img);
1097 
1098 
1099 #include "ImageBaseInline.hh"
1100 
1101 
1102 } // end namespace
1103 
1104 #endif // _IMAGE_BASE_H_INCLUDED_
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
YUYV422, 2 channels, full luminance Y, subsampled half U,V.
Definition: ImageBase.hh:133
Bayer_GRBG, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:145
void SetBitDepth(unsigned bitdepth)
needed by ImageIO
Definition: ImageBase.hh:581
hsl, similar to HSL but euclidean (h,s) for CNCC
Definition: ImageBase.hh:148
LAB, 3 channels, http://en.wikipedia.org/wiki/Lab_color_space.
Definition: ImageBase.hh:157
unsigned int BitDepth_
relevant bits per pixel per channel
Definition: ImageBase.hh:1062
class for handling different region of interest (ROI) representations...
Definition: ROI.hh:118
const bool IsInROI(const double &x, const double &y) const
Definition: ImageBase.hh:701
(16bit) unsigned integer image storage type
Definition: ImageBase.hh:114
Routines for loading and writing all kinds of image formats.
Definition: ImageIO.hh:69
void * GetImageData()
Definition: ImageBase.hh:287
unsigned int GetDepth() const
returns the bytes per channel, which is the sizeof(StorageType) Should match GetSizeDepth(GetStorageT...
Definition: ImageBase.hh:328
bool InterleavedDataOrder_
planar or interleaved: planar means we have several image planes, e.g.
Definition: ImageBase.hh:1070
HSL, similar to HSV but space is a double tipped cone.
Definition: ImageBase.hh:147
gray values, 1 channel
Definition: ImageBase.hh:130
unsigned int Depth_
size of one channel of one pixel in bytes
Definition: ImageBase.hh:1060
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
bool IsInterleaved() const
Definition: ImageBase.hh:491
DV, color model used for digital video cameras such as Mini-DV.
Definition: ImageBase.hh:140
(8bit) signed char image storage type
Definition: ImageBase.hh:113
unsigned int GetWidthStep() const
returns the number of bytes per line
Definition: ImageBase.hh:400
Othe&#39;s principle component generalization for RGB based segmentation.
Definition: ImageBase.hh:160
YUV411, 2 channles, full luminance, 1 U, 1 V.
Definition: ImageBase.hh:137
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
int Mirror()
interface for the above JW
Definition: ImageBase.hh:769
unsigned int GetSizeByte() const
returns the nr.
Definition: ImageBase.hh:352
void ** ImageDataArray_
array of pointers to the first byte in an image row
Definition: ImageBase.hh:1074
Bayer_RGGB, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:143
MetaData * GetMetaData()
Definition: ImageBase.hh:456
float image storage type
Definition: ImageBase.hh:118
unsigned int WidthStep_
size of a line in bytes for interleaved, NOT nr . of pixels (but size a line in one plane for planar)...
Definition: ImageBase.hh:1072
bool IsPlanar() const
Definition: ImageBase.hh:484
XYZ, 3 channels, http://en.wikipedia.org/wiki/Xyz_color_space.
Definition: ImageBase.hh:156
unsigned int GetWidth() const
Definition: ImageBase.hh:312
unsigned int GetBitDepth() const
returns the bits per channel Is not necessairily 8*sizeof(StorageType), could be fewer bits...
Definition: ImageBase.hh:344
YUV420P, 2 channels, full luminance Y, 1 U, 1 V. Y, U and V are grouped together for better compressi...
Definition: ImageBase.hh:135
Todo: Conflict with YUVU model, what does it do?
Definition: ImageBase.hh:154
PGR XB3 in format 7 mode 3 delivers an image that consists of 3 channels with 8bbp (overal 24bpp)...
Definition: ImageBase.hh:152
const BIAS::UUID & GetUID() const
returns the UUID of the image
Definition: ImageBase.hh:449
void SetInterleaved(bool interleaved)
Definition: ImageBase.hh:568
void SetStorageType(const EStorageType st)
changes StorageType data mmeber
Definition: ImageBase.hh:1042
BIAS::UUID _UID
unique id for every image
Definition: ImageBase.hh:1078
(16bit) signed integer image storage type
Definition: ImageBase.hh:115
color values, 3 channels, order: blue,green,red
Definition: ImageBase.hh:132
Bayer_BGGR, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:146
const void * GetImageData() const
Definition: ImageBase.hh:280
Disparity images Q: should disp and depth be treated separately, if not what would be a good name to ...
Definition: ImageBase.hh:158
const MetaData * GetMetaData() const
Definition: ImageBase.hh:463
Bayer_GBRG, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:144
MetaData _MetaData
additional data block, handled by derived classes
Definition: ImageBase.hh:1080
ROI * GetROI()
Returns a pointer to the roi object.
Definition: ImageBase.hh:615
unsigned int Height_
image height in pixels
Definition: ImageBase.hh:1066
void ** GetImageDataArray() const
Get an array of pointers to image data.
Definition: ImageBase.hh:305
CM_YUV444, 3 channels, all channels have full data.
Definition: ImageBase.hh:136
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
CIELUV color space, 3 channels, http://en.wikipedia.org/wiki/CIELUV_color_space.
Definition: ImageBase.hh:155
ROI Roi_
roi object
Definition: ImageBase.hh:1082
unsigned int GetHeight() const
Definition: ImageBase.hh:319
class with static functions for converting all kinds of color formats and storage types ...
Definition: ImageConvert.hh:89
UYVY422, 2 channels, full luminance Y, subsampled half U,V inverse order.
Definition: ImageBase.hh:134
The image template class for specific storage types.
Definition: Image.hh:78
obsolete, HSI is unused and identical to HSL
Definition: ImageBase.hh:139
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
SymTensor2x2 The image contains a 2x2 symmetric tensor.
Definition: ImageBase.hh:149
void InvalidateUID()
sets the image&#39;s uid to invalid
Definition: ImageBase.hh:597
void SetUID(const BIAS::UUID &id)
Definition: ImageBase.hh:589
bool shouldReleaseData_
shoud ImageBase release the data pointer?
Definition: ImageBase.hh:1084
RGBA, 4 channels, order: red,green,blue,alpha.
Definition: ImageBase.hh:141
unsigned int ChannelCount_
number of channels per pixel
Definition: ImageBase.hh:1058
(32bit) signed integer image storage type
Definition: ImageBase.hh:117
int GetVersionNumber() const
Definition: ImageBase.hh:477
RGBE color values, 4 channels, RADIANCE hdr format, four low dynamic channels meaning: 3x mantissa (r...
Definition: ImageBase.hh:151
this class collects all additional data chunks of type AppData to be written into/read from an image ...
Definition: MetaData.hh:121
void ClearDataPointer()
set data pointer to NULL, such that image IsEmpty without deletion of data.
Definition: ImageBase.hh:871
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
virtual ImageBase * Clone() const
virtual covariant copy constructor which produces the same type of object as the template object (Ima...
Definition: ImageBase.hh:193
const ROI * GetROI() const
Definition: ImageBase.hh:621
HSV, 3 channels, order: hue, sat , value.
Definition: ImageBase.hh:138
void * ImageData_
a pointer to the image data
Definition: ImageBase.hh:1076
interface class for producing/storing Universally Unique IDentifiers
Definition: UUID.hh:98
void SetMetaData(const MetaData &m)
Definition: ImageBase.hh:470
Todo: Unclear, I think one channel float, why isn&#39;t grey used for that?
Definition: ImageBase.hh:153
wrapper around a BIAS image to be used as an OpenCv IPlimage with shared data area.
Definition: WrapBias2Ipl.hh:26
unsigned long int GetPixelCount() const
returns number of pixels in image
Definition: ImageBase.hh:422
GreyA, 2 channels, grey plus Alpha.
Definition: ImageBase.hh:142
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
void RedirectImageDataPointer(void *data)
This method takes data and set the internal image data pointer to this.
Definition: ImageBase.hh:859
Depth images A: separated for now.
Definition: ImageBase.hh:159
unsigned int GetByteDepth() const
Definition: ImageBase.hh:334
bool operator==(const ImageBase &img) const
comparison operator, based upon the adresses of the actual image data
Definition: ImageBase.hh:738
unsigned int GetSize() const
returns the image size in bytes = count - NOT the dimension DEPRECATED Please use GetSizeByte instead...
Definition: ImageBase.hh:361
unsigned int Width_
image width in pixels
Definition: ImageBase.hh:1064
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157
BGRA color values, 4 channels, order: blue,green,red,alpha.
Definition: ImageBase.hh:150
(32bit) unsigned integer image storage type
Definition: ImageBase.hh:116