OpenWalnut  1.5.0dev
WGEImage.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut 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 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut 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 OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGEIMAGE_H
26 #define WGEIMAGE_H
27 
28 #include <memory>
29 #include <string>
30 
31 #include <boost/filesystem.hpp>
32 #include <osg/Image>
33 
34 #include "core/common/WColor.h"
35 
36 /**
37  * Image data object. Encapsulate 1D, 2D, and 3D images. Also provides static load function. This basically encapsulates an osg::Image. The
38  * osg::Image can contain a huge variety of image data of different data types, formats, and so on.
39  *
40  * \note If the API of WGEImage is not sufficient, you can use getAsOSGImage() and use theirs.
41  */
42 class WGEImage
43 {
44 public:
45  /**
46  * Convenience typedef for a std::shared_ptr< WGEImage >.
47  */
48  typedef std::shared_ptr< WGEImage > SPtr;
49 
50  /**
51  * Convenience typedef for a std::shared_ptr< const WGEImage >.
52  */
53  typedef std::shared_ptr< const WGEImage > ConstSPtr;
54 
55  /**
56  * Default constructor.
57  */
58  WGEImage();
59 
60  /**
61  * Construct from a given osg::Image.
62  *
63  * \param image the image
64  */
65  explicit WGEImage( const osg::Image& image );
66 
67  /**
68  * Copy construct from given image
69  *
70  * \param image the image
71  */
72  explicit WGEImage( const WGEImage& image );
73 
74  /**
75  * Destructor.
76  */
77  virtual ~WGEImage();
78 
79  /**
80  * Copy assignment operator.
81  *
82  * \param other the other instance
83  *
84  * \return this
85  */
86  WGEImage& operator= ( WGEImage other );
87 
88  /**
89  * Load an image from a file. This is very fault tolerant. Just returns NULL on error.
90  *
91  * \param file the file to load
92  *
93  * \return the image. Can be NULL on error.
94  */
95  static WGEImage::SPtr createFromFile( boost::filesystem::path file );
96 
97  /**
98  * Load an image from a file. This is very fault tolerant. Just returns NULL on error.
99  *
100  * \param file the file to load
101  *
102  * \return the image. Can be NULL on error.
103  */
104  static WGEImage::SPtr createFromFile( std::string file );
105 
106  /**
107  * Get size in X direction.
108  *
109  * \return the width
110  */
111  int getWidth() const;
112 
113  /**
114  * Get size in Y direction. This is 1 for 1D images.
115  *
116  * \return the height
117  */
118  int getHeight() const;
119 
120  /**
121  * Get size in Z direction. This is 1 for 2D & 1D images.
122  *
123  * \return the depth
124  */
125  int getDepth() const;
126 
127  /**
128  * Return the underlying osg::Image. Should rarely be used and is mostly useful when working directly with OSG.
129  *
130  * \return the osg::Image instance
131  */
132  osg::ref_ptr< osg::Image > getAsOSGImage() const;
133 
134  /**
135  * Get the raw image data.
136  *
137  * \return the raw data.
138  */
139  unsigned char* data();
140 
141  /**
142  * Get the raw image data.
143  *
144  * \return the raw data.
145  */
146  const unsigned char* data() const;
147 
148  /**
149  * Grab color at specified pixel/voxel. Please note that you should consider the image's origin. Query with \ref getOrigin().
150  *
151  * \param x X coord
152  * \param y Y coord, optional in 1D images
153  * \param z Z coord, optional in 1D,2D images.
154  *
155  * \return color at given position
156  */
157  WColor getColor( unsigned int x, unsigned int y = 0, unsigned int z = 0 );
158 
159  /**
160  * Where is the origin?
161  */
162  enum Origin
163  {
164  BOTTOM_LEFT = 0, //!< bottom left origin
165  TOP_LEFT //!< top left origin
166  };
167 
168  /**
169  * Query origin.
170  *
171  * \return the origin.
172  */
173  Origin getOrigin() const;
174 protected:
175 private:
176  /**
177  * The osg image we use.
178  */
179  osg::ref_ptr< osg::Image > m_image;
180 };
181 
182 #endif // WGEIMAGE_H
183 
Image data object.
Definition: WGEImage.h:43
osg::ref_ptr< osg::Image > getAsOSGImage() const
Return the underlying osg::Image.
Definition: WGEImage.cpp:85
Origin
Where is the origin?
Definition: WGEImage.h:163
@ BOTTOM_LEFT
bottom left origin
Definition: WGEImage.h:164
@ TOP_LEFT
top left origin
Definition: WGEImage.h:165
WColor getColor(unsigned int x, unsigned int y=0, unsigned int z=0)
Grab color at specified pixel/voxel.
Definition: WGEImage.cpp:115
std::shared_ptr< const WGEImage > ConstSPtr
Convenience typedef for a std::shared_ptr< const WGEImage >.
Definition: WGEImage.h:53
unsigned char * data()
Get the raw image data.
Definition: WGEImage.cpp:105
WGEImage()
Default constructor.
Definition: WGEImage.cpp:34
osg::ref_ptr< osg::Image > m_image
The osg image we use.
Definition: WGEImage.h:179
int getWidth() const
Get size in X direction.
Definition: WGEImage.cpp:90
int getDepth() const
Get size in Z direction.
Definition: WGEImage.cpp:100
WGEImage & operator=(WGEImage other)
Copy assignment operator.
Definition: WGEImage.cpp:50
static WGEImage::SPtr createFromFile(boost::filesystem::path file)
Load an image from a file.
Definition: WGEImage.cpp:80
Origin getOrigin() const
Query origin.
Definition: WGEImage.cpp:120
int getHeight() const
Get size in Y direction.
Definition: WGEImage.cpp:95
virtual ~WGEImage()
Destructor.
Definition: WGEImage.cpp:56
std::shared_ptr< WGEImage > SPtr
Convenience typedef for a std::shared_ptr< WGEImage >.
Definition: WGEImage.h:48