29 #include "nomlib/graphics/Image.hpp"
35 NOM_LOG_TRACE ( NOM );
37 if ( IMG_Init ( IMG_INIT_PNG ) != IMG_INIT_PNG )
39 NOM_LOG_ERR ( NOM, IMG_GetError() );
47 NOM_LOG_TRACE ( NOM );
50 Image::Image ( uint32 flags ) : image_ ( nullptr, priv::FreeSurface )
52 NOM_LOG_TRACE ( NOM );
54 if ( IMG_Init ( flags ) != flags )
56 NOM_LOG_ERR ( NOM, IMG_GetError() );
64 NOM_LOG_TRACE ( NOM );
69 this->image_ = other.image_;
75 int32 bpp, uint16 pitch,
76 uint32 Rmask, uint32 Gmask, uint32 Bmask, uint32 Amask
79 NOM_LOG_TRACE ( NOM );
81 this->image_.reset ( SDL_CreateRGBSurfaceFrom ( pixels, width, height, bpp, pitch, Rmask, Gmask, Bmask, Amask ), priv::FreeSurface );
84 if ( this->
valid() ==
false )
86 NOM_LOG_ERR ( NOM,
"Image buffer created from existing SDL_Surface is null." );
95 return this->image_.get();
100 if ( this->
image() !=
nullptr )
return true;
105 int32 Image::width (
void )
const
107 SDL_Surface* buffer = this->
image();
111 int32 Image::height (
void )
const
113 SDL_Surface* buffer = this->
image();
117 void* Image::pixels (
void )
const
119 SDL_Surface* buffer = this->
image();
120 return buffer->pixels;
123 uint16 Image::pitch (
void )
const
125 SDL_Surface* buffer = this->
image();
126 return buffer->pitch;
131 SDL_Surface* buffer = this->
image();
133 return buffer->format->BitsPerPixel;
136 const SDL_PIXELFORMAT::RawPtr Image::pixel_format (
void )
const
138 SDL_Surface* buffer = this->
image();
139 return buffer->format;
144 return this->pixel_format()->Rmask;
149 return this->pixel_format()->Gmask;
154 return this->pixel_format()->Bmask;
159 return this->pixel_format()->Amask;
164 SDL_Rect clip_buffer;
168 SDL_GetClipRect ( this->
image(), &clip_buffer );
171 clip_bounds =
Coords ( clip_buffer.x, clip_buffer.y, clip_buffer.w, clip_buffer.h );
176 void Image::set_bounds (
const Coords& clip_bounds )
178 SDL_Rect clip = SDL_RECT ( clip_bounds );
182 SDL_SetClipRect ( this->
image(), &clip );
187 SDL_Surface *buffer = IMG_Load ( filename.c_str() );
189 if ( buffer ==
nullptr )
191 NOM_LOG_ERR ( NOM, IMG_GetError() );
192 priv::FreeSurface ( buffer );
196 this->image_.reset ( SDL_ConvertSurfaceFormat ( buffer, SDL_PIXELFORMAT_RGBA8888, 0 ), priv::FreeSurface );
197 priv::FreeSurface ( buffer );
204 SDL_Surface *buffer = IMG_Load ( filename.c_str() );
205 if ( buffer ==
nullptr )
207 NOM_LOG_ERR ( NOM, IMG_GetError() );
208 priv::FreeSurface ( buffer );
212 this->image_.reset ( SDL_ConvertSurfaceFormat ( buffer, SDL_PIXELFORMAT_RGBA8888, 0 ), priv::FreeSurface );
213 priv::FreeSurface ( buffer );
218 bool Image::save (
const std::string& filename, SDL_SURFACE::RawPtr video_buffer )
const
220 if ( SDL_SaveBMP ( video_buffer, filename.c_str() ) != 0 )
222 NOM_LOG_ERR ( NOM, SDL_GetError() );
231 if ( this->
save( filename, this->
image() ) ==
false )
return false;
239 SDL_Surface* buffer = this->
image();
240 Point2i image_pos ( buffer->w, buffer->h );
247 SDL_Surface* buffer = this->
image();
248 uint32 transparent_color = 0;
251 if ( SDL_GetColorKey ( this->
image(), &transparent_color ) != 0 )
253 NOM_LOG_ERR ( NOM, SDL_GetError() );
254 priv::FreeSurface ( buffer );
258 return nom::pixel ( transparent_color, buffer->format );
265 if ( SDL_GetSurfaceBlendMode ( this->
image(), &blend ) != 0 )
267 NOM_LOG_ERR ( NOM, SDL_GetError() );
276 SDL_Surface* buffer = this->
image();
277 uint32 transparent_color = RGB ( colorkey, buffer->format );
279 if ( this->
valid() ==
false )
281 NOM_LOG_ERR ( NOM,
"Could not set color key: invalid image buffer." );
282 priv::FreeSurface ( buffer );
286 if ( SDL_SetColorKey ( buffer, SDL_BOOL(flag), transparent_color ) != 0 )
288 NOM_LOG_ERR ( NOM, SDL_GetError() );
289 priv::FreeSurface ( buffer );
298 if ( SDL_SetSurfaceRLE ( this->
image(), SDL_BOOL(flag) ) != 0 )
300 NOM_LOG_ERR ( NOM, SDL_GetError() );
310 default:
return -1;
break;
314 uint8* pixels =
static_cast<uint8*
> ( this->pixels() );
316 return pixels[ ( y * this->pitch() ) + x ];
322 uint16* pixels =
static_cast<uint16*
> ( this->pixels() );
324 return pixels[ ( y * this->pitch() / 2 ) + x ];
330 uint8* pixels =
static_cast<uint8*
> ( this->pixels() );
332 return pixels[ ( y * this->pitch() ) + x ];
338 uint32* pixels =
static_cast<uint32*
> ( this->pixels() );
340 return pixels[ ( y * this->pitch()/4 ) + x ];
348 if ( SDL_SetSurfaceBlendMode ( this->
image(), blend ) != 0 )
350 NOM_LOG_ERR ( NOM, SDL_GetError() );
bool load_bmp(const std::string &filename)
Image & operator=(const Image &other)
Copy assignment constructor.
virtual ~Image(void)
Destructor.
const Point2i size(void) const
Obtain the width and height (in pixels) of the stored bitmap buffer.
const uint32 green_mask(void) const
Obtain the video surface's green alpha mask.
Image(void)
Default constructor – initializes to sane defaults.
static const Color null
Convenience object that will always contain a value of -1.
uint32 pixel(int32 x, int32 y)
SDL_SURFACE::RawPtr image(void) const
Obtain the SDL_Surface struct used in this object instance.
bool set_colorkey(const Color &colorkey, bool flag)
const Coords bounds(void) const
bool load(const std::string &filename)
const uint32 alpha_mask(void) const
Obtain the video surface's alpha mask.
const uint32 blue_mask(void) const
Obtain the video surface's blue alpha mask.
Bitmap storage container.
const SDL_BlendMode blend_mode(void) const
Obtain the blend mode used for blitting.
bool set_blend_mode(const SDL_BlendMode blend)
Set a new blending mode for blitting.
bool initialize(void *pixels, int32 width, int32 height, int32 bpp, uint16 pitch, uint32 Rmask, uint32 Gmask, uint32 Bmask, uint32 Amask)
bool valid(void) const
Is this object initialized? Valid when NOT nullptr.
uint8 bits_per_pixel(void) const
Coordinate values (x, y, z, width, height ) wrapper.
bool save(const std::string &filename, SDL_SURFACE::RawPtr video_buffer) const
const Color colorkey(void) const
const uint32 red_mask(void) const
Obtain the video surface's red alpha mask.
Utility class container for RGBA colors.