summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/image.cpp29
-rw-r--r--src/common/image.h3
2 files changed, 32 insertions, 0 deletions
diff --git a/src/common/image.cpp b/src/common/image.cpp
index db14797..fd55217 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>
@@ -200,6 +202,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)
diff --git a/src/common/image.h b/src/common/image.h
index d9da75b..f0b50a3 100644
--- a/src/common/image.h
+++ b/src/common/image.h
@@ -94,6 +94,9 @@ public:
//! Returns the precise color at given pixel
Gfx::IntColor GetPixelInt(Math::IntPoint pixel);
+ //! Pads the image to nearest power of 2 dimensions
+ void PadToNearestPowerOfTwo();
+
//! Loads an image from the specified file
bool Load(const std::string &fileName);