From 4fde8c8cbc6a019f1519a086e14ca79291439a48 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bert=20M=C3=BCnnich?= <ber.t@posteo.de>
Date: Thu, 6 Feb 2014 22:04:19 +0100
Subject: [PATCH] Use separate background image for alpha layer; fixes issue
 #132

---
 Makefile |  2 +-
 image.c  | 38 ++++++++++++++++++++++++++++++--------
 2 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/Makefile b/Makefile
index cea7b60..ba8cfe5 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-VERSION = git-20140205
+VERSION = git-20140206
 
 PREFIX    = /usr/local
 MANPREFIX = $(PREFIX)/share/man
diff --git a/image.c b/image.c
index b56ebbc..62b3c08 100644
--- a/image.c
+++ b/image.c
@@ -441,6 +441,8 @@ void img_render(img_t *img)
 	win_t *win;
 	int sx, sy, sw, sh;
 	int dx, dy, dw, dh;
+	Imlib_Image bg;
+	unsigned long c;
 
 	if (img == NULL || img->im == NULL || img->win == NULL)
 		return;
@@ -456,8 +458,11 @@ void img_render(img_t *img)
 	if (!img->dirty)
 		return;
 
-	/* calculate source and destination offsets */
-	if (img->x < 0) {
+	/* calculate source and destination offsets:
+	 *   - part of image drawn on full window, or
+	 *   - full image drawn on part of window
+	 */
+	if (img->x <= 0) {
 		sx = -img->x / img->zoom;
 		sw = win->w / img->zoom;
 		dx = 0;
@@ -468,7 +473,7 @@ void img_render(img_t *img)
 		dx = img->x;
 		dw = img->w * img->zoom;
 	}
-	if (img->y < 0) {
+	if (img->y <= 0) {
 		sy = -img->y / img->zoom;
 		sh = win->h / img->zoom;
 		dy = 0;
@@ -484,13 +489,30 @@ void img_render(img_t *img)
 
 	imlib_context_set_image(img->im);
 	imlib_context_set_anti_alias(img->aa);
-
-	if (!img->alpha && imlib_image_has_alpha())
-		win_draw_rect(win, win->pm, dx, dy, dw, dh, True, 0, win->white);
-	
 	imlib_context_set_drawable(win->pm);
-	imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
 
+	if (imlib_image_has_alpha()) {
+		bg = imlib_create_image(dw, dh);
+		imlib_context_set_image(bg);
+		imlib_image_set_has_alpha(0);
+
+		if (img->alpha)
+			c = win->fullscreen ? win->fscol : win->bgcol;
+		else
+			c = win->white;
+		imlib_context_set_color(c >> 16 & 0xFF, c >> 8 & 0xFF, c & 0xFF, 0xFF);
+		imlib_image_fill_rectangle(0, 0, dw, dh);
+
+		imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
+		imlib_context_set_color_modifier(NULL);
+		imlib_render_image_on_drawable(dx, dy);
+
+		imlib_free_image();
+		if (img->gamma != 0)
+			imlib_context_set_color_modifier(img->cmod);
+	} else {
+		imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
+	}
 	img->dirty = false;
 }