summaryrefslogtreecommitdiff
path: root/src/resources/image.cpp
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2009-03-23 21:59:21 +0100
committerBjørn Lindeijer <bjorn@lindeijer.nl>2009-03-23 22:00:09 +0100
commit99e8a3fd77b63a029fe02dcf771b6af1aad252ed (patch)
tree03c296d1f89859aae35336dfe2f58df09d256fd3 /src/resources/image.cpp
parentfa8a4bf49100c0a1d5b96e00803f43bbbb861100 (diff)
parent347452b9b69ef3af29c577b7751082822e900c01 (diff)
downloadmana-99e8a3fd77b63a029fe02dcf771b6af1aad252ed.tar.gz
mana-99e8a3fd77b63a029fe02dcf771b6af1aad252ed.tar.bz2
mana-99e8a3fd77b63a029fe02dcf771b6af1aad252ed.tar.xz
mana-99e8a3fd77b63a029fe02dcf771b6af1aad252ed.zip
Merge branch 'aethyra/master'
Conflicts: Many files.
Diffstat (limited to 'src/resources/image.cpp')
-rw-r--r--src/resources/image.cpp79
1 files changed, 77 insertions, 2 deletions
diff --git a/src/resources/image.cpp b/src/resources/image.cpp
index 7a7e6ac8..b696389f 100644
--- a/src/resources/image.cpp
+++ b/src/resources/image.cpp
@@ -25,6 +25,7 @@
#include "image.h"
#include "../log.h"
+#include "../position.h"
#ifdef USE_OPENGL
bool Image::mUseOpenGL = false;
@@ -46,8 +47,7 @@ Image::Image(SDL_Surface *image):
}
#ifdef USE_OPENGL
-Image::Image(GLuint glimage, int width, int height,
- int texWidth, int texHeight):
+Image::Image(GLuint glimage, int width, int height, int texWidth, int texHeight):
mGLImage(glimage),
mTexWidth(texWidth),
mTexHeight(texHeight),
@@ -315,6 +315,81 @@ void Image::setAlpha(float a)
}
}
+Image* Image::merge(Image* image, const Position& pos)
+{
+ SDL_Surface* surface = new SDL_Surface(*(image->mImage));
+
+ Uint32 surface_pix, cur_pix;
+ Uint8 r, g, b, a, p_r, p_g, p_b, p_a;
+ double f_a, f_ca, f_pa;
+ SDL_PixelFormat *current_fmt = mImage->format;
+ SDL_PixelFormat *surface_fmt = surface->format;
+ int current_offset, surface_offset;
+ Position offset(0, 0);
+
+ SDL_LockSurface(surface);
+ SDL_LockSurface(mImage);
+ // for each pixel lines of a source image
+ for (offset.x = (pos.x > 0 ? 0 : -pos.x); offset.x < image->getWidth() &&
+ pos.x + offset.x < getWidth(); offset.x++)
+ {
+ for (offset.y = (pos.y > 0 ? 0 : -pos.y); offset.y < image->getHeight()
+ && pos.y + offset.y < getHeight(); offset.y++)
+ {
+ // Computing offset on both images
+ current_offset = (pos.y + offset.y) * getWidth() + pos.x + offset.x;
+ surface_offset = offset.y * surface->w + offset.x;
+
+ // Retrieving a pixel to merge
+ surface_pix = ((Uint32*) surface->pixels)[surface_offset];
+ cur_pix = ((Uint32*) mImage->pixels)[current_offset];
+
+ // Retreiving each channel of the pixel using pixel format
+ r = (Uint8)(((surface_pix & surface_fmt->Rmask) >>
+ surface_fmt->Rshift) << surface_fmt->Rloss);
+ g = (Uint8)(((surface_pix & surface_fmt->Gmask) >>
+ surface_fmt->Gshift) << surface_fmt->Gloss);
+ b = (Uint8)(((surface_pix & surface_fmt->Bmask) >>
+ surface_fmt->Bshift) << surface_fmt->Bloss);
+ a = (Uint8)(((surface_pix & surface_fmt->Amask) >>
+ surface_fmt->Ashift) << surface_fmt->Aloss);
+
+ // Retreiving previous alpha value
+ p_a = (Uint8)(((cur_pix & current_fmt->Amask) >>
+ current_fmt->Ashift) << current_fmt->Aloss);
+
+ // new pixel with no alpha or nothing on previous pixel
+ if (a == SDL_ALPHA_OPAQUE || (p_a == 0 && a > 0))
+ ((Uint32 *)(surface->pixels))[current_offset] =
+ SDL_MapRGBA(current_fmt, r, g, b, a);
+ else if (a > 0)
+ { // alpha is lower => merge color with previous value
+ f_a = (double) a / 255.0;
+ f_ca = 1.0 - f_a;
+ f_pa = (double) p_a / 255.0;
+ p_r = (Uint8)(((cur_pix & current_fmt->Rmask) >>
+ current_fmt->Rshift) << current_fmt->Rloss);
+ p_g = (Uint8)(((cur_pix & current_fmt->Gmask) >>
+ current_fmt->Gshift) << current_fmt->Gloss);
+ p_b = (Uint8)(((cur_pix & current_fmt->Bmask) >>
+ current_fmt->Bshift) << current_fmt->Bloss);
+ r = (Uint8)((double) p_r * f_ca * f_pa + (double)r * f_a);
+ g = (Uint8)((double) p_g * f_ca * f_pa + (double)g * f_a);
+ b = (Uint8)((double) p_b * f_ca * f_pa + (double)b * f_a);
+ a = (a > p_a ? a : p_a);
+ ((Uint32 *)(surface->pixels))[current_offset] =
+ SDL_MapRGBA(current_fmt, r, g, b, a);
+ }
+ }
+ }
+ SDL_UnlockSurface(surface);
+ SDL_UnlockSurface(mImage);
+
+ Image* newImage = new Image(surface);
+
+ return newImage;
+}
+
float Image::getAlpha()
{
return mAlpha;