diff options
Diffstat (limited to 'src/common/image.cpp')
-rw-r--r-- | src/common/image.cpp | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/common/image.cpp b/src/common/image.cpp index db14797..a9587ef 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -17,6 +17,8 @@ #include "common/image.h" +#include "math/func.h" + #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -108,7 +110,8 @@ bool PNGSaveSurface(const char *filename, SDL_Surface *surf) return false; } - if (setjmp(png_jmpbuf(png_ptr))) { + if (setjmp(png_jmpbuf(png_ptr))) + { png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); return false; @@ -200,6 +203,33 @@ void CImage::Fill(Gfx::IntColor color) } /** + * Image must be valid. + * + * The dimensions are increased to nearest even power of two values. + * If image is already in power-of-two format, nothing is done. + */ +void CImage::PadToNearestPowerOfTwo() +{ + assert(m_data != nullptr); + + if (Math::IsPowerOfTwo(m_data->surface->w) && Math::IsPowerOfTwo(m_data->surface->h)) + return; + + int w = Math::NextPowerOfTwo(m_data->surface->w); + int h = Math::NextPowerOfTwo(m_data->surface->h); + + m_data->surface->flags &= (~SDL_SRCALPHA); + SDL_Surface* resizedSurface = SDL_CreateRGBSurface(0, w, h, 32, 0x00ff0000, 0x0000ff00, + 0x000000ff, 0xff000000); + assert(resizedSurface != NULL); + SDL_BlitSurface(m_data->surface, NULL, resizedSurface, NULL); + + SDL_FreeSurface(m_data->surface); + + m_data->surface = resizedSurface; +} + +/** * Image must be valid and pixel coords in valid range. * * \param pixel pixel coords (range x: 0..width-1 y: 0..height-1) @@ -367,3 +397,4 @@ bool CImage::SavePNG(const std::string& fileName) return true; } + |