Image.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/graphics/Image.hpp"
30 
31 namespace nom {
32 
33 Image::Image ( void ) : image_ ( nullptr, priv::FreeSurface )
34 {
35 NOM_LOG_TRACE ( NOM );
36 
37  if ( IMG_Init ( IMG_INIT_PNG ) != IMG_INIT_PNG )
38  {
39 NOM_LOG_ERR ( NOM, IMG_GetError() );
40  }
41 
42  atexit ( IMG_Quit );
43 }
44 
45 Image::~Image ( void )
46 {
47 NOM_LOG_TRACE ( NOM );
48 }
49 
50 Image::Image ( uint32 flags ) : image_ ( nullptr, priv::FreeSurface )
51 {
52 NOM_LOG_TRACE ( NOM );
53 
54  if ( IMG_Init ( flags ) != flags )
55  {
56 NOM_LOG_ERR ( NOM, IMG_GetError() );
57  }
58 
59  atexit ( IMG_Quit );
60 }
61 
62 Image::Image ( const Image& other ) : image_ { other.image(), priv::FreeSurface }
63 {
64 NOM_LOG_TRACE ( NOM );
65 }
66 
67 Image& Image::operator = ( const Image& other )
68 {
69  this->image_ = other.image_;
70 
71  return *this;
72 }
73 
74 bool Image::initialize( void* pixels, int32 width, int32 height,
75  int32 bpp, uint16 pitch,
76  uint32 Rmask, uint32 Gmask, uint32 Bmask, uint32 Amask
77  )
78 {
79 NOM_LOG_TRACE ( NOM );
80 
81  this->image_.reset ( SDL_CreateRGBSurfaceFrom ( pixels, width, height, bpp, pitch, Rmask, Gmask, Bmask, Amask ), priv::FreeSurface );
82  //this->bounds_.setSize ( width, height );
83 
84  if ( this->valid() == false )
85  {
86  NOM_LOG_ERR ( NOM, "Image buffer created from existing SDL_Surface is null." );
87  return false;
88  }
89 
90  return true;
91 }
92 
93 SDL_SURFACE::RawPtr Image::image ( void ) const
94 {
95  return this->image_.get();
96 }
97 
98 bool Image::valid ( void ) const
99 {
100  if ( this->image() != nullptr ) return true;
101 
102  return false;
103 }
104 
105 int32 Image::width ( void ) const
106 {
107  SDL_Surface* buffer = this->image();
108  return buffer->w;
109 }
110 
111 int32 Image::height ( void ) const
112 {
113  SDL_Surface* buffer = this->image();
114  return buffer->h;
115 }
116 
117 void* Image::pixels ( void ) const
118 {
119  SDL_Surface* buffer = this->image();
120  return buffer->pixels;
121 }
122 
123 uint16 Image::pitch ( void ) const
124 {
125  SDL_Surface* buffer = this->image();
126  return buffer->pitch;
127 }
128 
129 uint8 Image::bits_per_pixel ( void ) const
130 {
131  SDL_Surface* buffer = this->image();
132 
133  return buffer->format->BitsPerPixel;
134 }
135 
136 const SDL_PIXELFORMAT::RawPtr Image::pixel_format ( void ) const
137 {
138  SDL_Surface* buffer = this->image();
139  return buffer->format;
140 }
141 
142 const uint32 Image::red_mask ( void ) const
143 {
144  return this->pixel_format()->Rmask;
145 }
146 
147 const uint32 Image::green_mask ( void ) const
148 {
149  return this->pixel_format()->Gmask;
150 }
151 
152 const uint32 Image::blue_mask ( void ) const
153 {
154  return this->pixel_format()->Bmask;
155 }
156 
157 const uint32 Image::alpha_mask ( void ) const
158 {
159  return this->pixel_format()->Amask;
160 }
161 
162 const Coords Image::bounds ( void ) const
163 {
164  SDL_Rect clip_buffer; // temporary storage struct
165  Coords clip_bounds; // transferred values from SDL_Rect clip_buffer
166 
167  // Return values are put into the clip_buffer SDL_Rect after executing:
168  SDL_GetClipRect ( this->image(), &clip_buffer );
169 
170  // Now transfer the values into our preferred data container type
171  clip_bounds = Coords ( clip_buffer.x, clip_buffer.y, clip_buffer.w, clip_buffer.h );
172 
173  return clip_bounds;
174 }
175 
176 void Image::set_bounds ( const Coords& clip_bounds )
177 {
178  SDL_Rect clip = SDL_RECT ( clip_bounds );
179 
180  // As per libSDL docs, if SDL_Rect is nullptr, the clipping rectangle is set
181  // to the full size of the surface
182  SDL_SetClipRect ( this->image(), &clip );
183 }
184 
185 bool Image::load ( const std::string& filename )
186 {
187  SDL_Surface *buffer = IMG_Load ( filename.c_str() );
188 
189  if ( buffer == nullptr )
190  {
191 NOM_LOG_ERR ( NOM, IMG_GetError() );
192  priv::FreeSurface ( buffer );
193  return false;
194  }
195 
196  this->image_.reset ( SDL_ConvertSurfaceFormat ( buffer, SDL_PIXELFORMAT_RGBA8888, 0 ), priv::FreeSurface );
197  priv::FreeSurface ( buffer );
198 
199  return true;
200 }
201 
202 bool Image::load_bmp ( const std::string& filename )
203 {
204  SDL_Surface *buffer = IMG_Load ( filename.c_str() );
205  if ( buffer == nullptr )
206  {
207  NOM_LOG_ERR ( NOM, IMG_GetError() );
208  priv::FreeSurface ( buffer );
209  return false;
210  }
211 
212  this->image_.reset ( SDL_ConvertSurfaceFormat ( buffer, SDL_PIXELFORMAT_RGBA8888, 0 ), priv::FreeSurface );
213  priv::FreeSurface ( buffer );
214 
215  return true;
216 }
217 
218 bool Image::save ( const std::string& filename, SDL_SURFACE::RawPtr video_buffer ) const
219 {
220  if ( SDL_SaveBMP ( video_buffer, filename.c_str() ) != 0 )
221  {
222 NOM_LOG_ERR ( NOM, SDL_GetError() );
223  return false;
224  }
225 
226  return true;
227 }
228 
229 bool Image::save ( const std::string& filename ) const
230 {
231  if ( this->save( filename, this->image() ) == false ) return false;
232 
233  // Success!
234  return true;
235 }
236 
237 const Point2i Image::size ( void ) const
238 {
239  SDL_Surface* buffer = this->image();
240  Point2i image_pos ( buffer->w, buffer->h );
241 
242  return image_pos;
243 }
244 
245 const Color Image::colorkey ( void ) const
246 {
247  SDL_Surface* buffer = this->image();
248  uint32 transparent_color = 0;
249  Color key;
250 
251  if ( SDL_GetColorKey ( this->image(), &transparent_color ) != 0 )
252  {
253 NOM_LOG_ERR ( NOM, SDL_GetError() );
254  priv::FreeSurface ( buffer );
255  return Color::null;
256  }
257 
258  return nom::pixel ( transparent_color, buffer->format );
259 }
260 
261 const SDL_BlendMode Image::blend_mode ( void ) const
262 {
263  SDL_BlendMode blend;
264 
265  if ( SDL_GetSurfaceBlendMode ( this->image(), &blend ) != 0 )
266  {
267 NOM_LOG_ERR ( NOM, SDL_GetError() );
268  return blend;
269  }
270 
271  return blend;
272 }
273 
274 bool Image::set_colorkey ( const Color& colorkey, bool flag )
275 {
276  SDL_Surface* buffer = this->image();
277  uint32 transparent_color = RGB ( colorkey, buffer->format );
278 
279  if ( this->valid() == false )
280  {
281 NOM_LOG_ERR ( NOM, "Could not set color key: invalid image buffer." );
282  priv::FreeSurface ( buffer );
283  return false;
284  }
285 
286  if ( SDL_SetColorKey ( buffer, SDL_BOOL(flag), transparent_color ) != 0 )
287  {
288 NOM_LOG_ERR ( NOM, SDL_GetError() );
289  priv::FreeSurface ( buffer );
290  return false;
291  }
292 
293  return true;
294 }
295 
296 bool Image::RLE ( bool flag )
297 {
298  if ( SDL_SetSurfaceRLE ( this->image(), SDL_BOOL(flag) ) != 0 )
299  {
300 NOM_LOG_ERR ( NOM, SDL_GetError() );
301  return false;
302  }
303  return true;
304 }
305 
306 uint32 Image::pixel ( int32 x, int32 y )
307 {
308  switch ( this->bits_per_pixel() )
309  {
310  default: return -1; break; // Unknown
311 
312  case 8:
313  {
314  uint8* pixels = static_cast<uint8*> ( this->pixels() );
315 
316  return pixels[ ( y * this->pitch() ) + x ];
317  }
318  break;
319 
320  case 16:
321  {
322  uint16* pixels = static_cast<uint16*> ( this->pixels() );
323 
324  return pixels[ ( y * this->pitch() / 2 ) + x ];
325  }
326  break;
327 
328  case 24:
329  {
330  uint8* pixels = static_cast<uint8*> ( this->pixels() );
331 
332  return pixels[ ( y * this->pitch() ) + x ];
333  }
334  break;
335 
336  case 32:
337  {
338  uint32* pixels = static_cast<uint32*> ( this->pixels() );
339 
340  return pixels[ ( y * this->pitch()/4 ) + x ];
341  }
342  break;
343  } // end switch
344 }
345 
346 bool Image::set_blend_mode ( const SDL_BlendMode blend )
347 {
348  if ( SDL_SetSurfaceBlendMode ( this->image(), blend ) != 0 )
349  {
350 NOM_LOG_ERR ( NOM, SDL_GetError() );
351  return false;
352  }
353 
354  return true;
355 }
356 
357 
358 } // namespace nom
bool load_bmp(const std::string &filename)
Definition: Image.cpp:202
Image & operator=(const Image &other)
Copy assignment constructor.
Definition: Image.cpp:67
virtual ~Image(void)
Destructor.
Definition: Image.cpp:45
const Point2i size(void) const
Obtain the width and height (in pixels) of the stored bitmap buffer.
Definition: Image.cpp:237
const uint32 green_mask(void) const
Obtain the video surface&#39;s green alpha mask.
Definition: Image.cpp:147
Image(void)
Default constructor – initializes to sane defaults.
Definition: Image.cpp:33
static const Color null
Convenience object that will always contain a value of -1.
Definition: Color.hpp:74
uint32 pixel(int32 x, int32 y)
Definition: Image.cpp:306
SDL_SURFACE::RawPtr image(void) const
Obtain the SDL_Surface struct used in this object instance.
Definition: Image.cpp:93
bool RLE(bool flag)
Definition: Image.cpp:296
bool set_colorkey(const Color &colorkey, bool flag)
Definition: Image.cpp:274
const Coords bounds(void) const
Definition: Image.cpp:162
bool load(const std::string &filename)
Definition: Image.cpp:185
const uint32 alpha_mask(void) const
Obtain the video surface&#39;s alpha mask.
Definition: Image.cpp:157
const uint32 blue_mask(void) const
Obtain the video surface&#39;s blue alpha mask.
Definition: Image.cpp:152
Bitmap storage container.
Definition: Image.hpp:49
const SDL_BlendMode blend_mode(void) const
Obtain the blend mode used for blitting.
Definition: Image.cpp:261
bool set_blend_mode(const SDL_BlendMode blend)
Set a new blending mode for blitting.
Definition: Image.cpp:346
bool initialize(void *pixels, int32 width, int32 height, int32 bpp, uint16 pitch, uint32 Rmask, uint32 Gmask, uint32 Bmask, uint32 Amask)
Definition: Image.cpp:74
bool valid(void) const
Is this object initialized? Valid when NOT nullptr.
Definition: Image.cpp:98
uint8 bits_per_pixel(void) const
Definition: Image.cpp:129
Coordinate values (x, y, z, width, height ) wrapper.
Definition: Coords.hpp:44
bool save(const std::string &filename, SDL_SURFACE::RawPtr video_buffer) const
Definition: Image.cpp:218
const Color colorkey(void) const
Definition: Image.cpp:245
const uint32 red_mask(void) const
Obtain the video surface&#39;s red alpha mask.
Definition: Image.cpp:142
Utility class container for RGBA colors.
Definition: Color.hpp:43