OpenWalnut  1.5.0dev
WGEImage.cpp
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 #include <algorithm>
26 #include <string>
27 
28 #include <osgDB/ReadFile>
29 
30 #include "core/common/WLogger.h"
31 
32 #include "WGEImage.h"
33 
35  m_image()
36 {
37  // initialize members
38 }
39 
40 WGEImage::WGEImage( const osg::Image& image ):
41  m_image( new osg::Image( image, osg::CopyOp::DEEP_COPY_IMAGES ) )
42 {
43 }
44 
45 WGEImage::WGEImage( const WGEImage& image ):
46  m_image( new osg::Image( *image.m_image, osg::CopyOp::DEEP_COPY_IMAGES ) )
47 {
48 }
49 
51 {
52  std::swap( m_image, other.m_image );
53  return *this;
54 }
55 
57 {
58  // cleanup
59 }
60 
62 {
63  try
64  {
65  osg::ref_ptr< osg::Image > image = osgDB::readImageFile( file );
66  if( !image )
67  {
68  wlog::error( "WGEImage" ) << "Failed to load image \"" << file << "\".";
69  return WGEImage::SPtr();
70  }
71  return WGEImage::SPtr( new WGEImage( *image ) );
72  }
73  catch( ... )
74  {
75  wlog::error( "WGEImage" ) << "Failed to load image \"" << file << "\".";
76  }
77  return WGEImage::SPtr();
78 }
79 
80 WGEImage::SPtr WGEImage::createFromFile( boost::filesystem::path file )
81 {
82  return WGEImage::createFromFile( file.string() );
83 }
84 
85 osg::ref_ptr< osg::Image > WGEImage::getAsOSGImage() const
86 {
87  return m_image;
88 }
89 
90 int WGEImage::getWidth() const
91 {
92  return m_image->s();
93 }
94 
96 {
97  return m_image->t();
98 }
99 
101 {
102  return m_image->r();
103 }
104 
105 unsigned char* WGEImage::data()
106 {
107  return m_image->data();
108 }
109 
110 const unsigned char* WGEImage::data() const
111 {
112  return m_image->data();
113 }
114 
115 WColor WGEImage::getColor( unsigned int x, unsigned int y, unsigned int z )
116 {
117  return m_image->getColor( x, y, z );
118 }
119 
121 {
122  switch( m_image->getOrigin() )
123  {
124  case osg::Image::BOTTOM_LEFT:
125  return BOTTOM_LEFT;
126  case osg::Image::TOP_LEFT:
127  return TOP_LEFT;
128  }
129 
130  return BOTTOM_LEFT;
131 }
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
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
WStreamedLogger error(const std::string &source)
Logging an error message.
Definition: WLogger.h:298