From e685859a30560e076db4de003fba4e4500ade2c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bert=20M=C3=BCnnich?= <ber.t@posteo.de>
Date: Sun, 6 Apr 2014 22:47:42 +0200
Subject: [PATCH] Use a checkerboard background for alpha layer; fixes issue
 #138

---
 Makefile     |  2 +-
 README.md    |  2 +-
 commands.c   | 17 ++++++++---------
 commands.h   |  2 +-
 config.def.h | 25 +++++++++++--------------
 image.c      | 30 +++++++++++++++++++++---------
 sxiv.1       |  6 +++---
 thumbs.c     |  6 ------
 thumbs.h     |  1 -
 window.c     |  1 -
 window.h     |  1 -
 11 files changed, 46 insertions(+), 47 deletions(-)

diff --git a/Makefile b/Makefile
index 3eab873..ff79029 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-VERSION = git-20140317
+VERSION = git-20140406
 
 PREFIX    = /usr/local
 MANPREFIX = $(PREFIX)/share/man
diff --git a/README.md b/README.md
index e2d5e81..bdb4cb4 100644
--- a/README.md
+++ b/README.md
@@ -102,7 +102,6 @@ of small previews is displayed, making it easy to choose an image to open.
     f            Toggle fullscreen mode (requires an EWMH/NetWM compliant
                  window manager)
     b            Toggle visibility of info bar on bottom of window
-    A            Toggle visibility of alpha-channel, i.e. transparency
 
     r            Reload image
     R            Reload all thumbnails
@@ -149,6 +148,7 @@ of small previews is displayed, making it easy to choose an image to open.
     Ctrl-g       Reset gamma
 
     a            Toggle anti-aliasing
+    A            Toggle visibility of alpha-channel, i.e. transparency
 
     s            Toggle slideshow or set delay to [count] seconds
 
diff --git a/commands.c b/commands.c
index 1d1c8b3..ecc3c4c 100644
--- a/commands.c
+++ b/commands.c
@@ -75,10 +75,8 @@ cmdreturn_t it_quit(arg_t a)
 cmdreturn_t it_switch_mode(arg_t a)
 {
 	if (mode == MODE_IMAGE) {
-		if (tns.thumbs == NULL) {
+		if (tns.thumbs == NULL)
 			tns_init(&tns, filecnt, &win);
-			tns.alpha = img.alpha;
-		}
 		img_close(&img, false);
 		reset_timeout(reset_cursor);
 		if (img.ss.on) {
@@ -494,14 +492,15 @@ cmdreturn_t i_toggle_antialias(arg_t a)
 	}
 }
 
-cmdreturn_t it_toggle_alpha(arg_t a)
+cmdreturn_t i_toggle_alpha(arg_t a)
 {
-	img.alpha = tns.alpha = !img.alpha;
-	if (mode == MODE_IMAGE)
+	if (mode == MODE_IMAGE) {
+		img.alpha = !img.alpha;
 		img.dirty = true;
-	else
-		tns.dirty = true;
-	return CMD_DIRTY;
+		return CMD_DIRTY;
+	} else {
+		return CMD_INVALID;
+	}
 }
 
 cmdreturn_t i_change_gamma(arg_t a)
diff --git a/commands.h b/commands.h
index dcc2e0f..0593a3f 100644
--- a/commands.h
+++ b/commands.h
@@ -74,7 +74,7 @@ cmdreturn_t i_rotate(arg_t);
 cmdreturn_t i_flip(arg_t);
 cmdreturn_t i_slideshow(arg_t);
 cmdreturn_t i_toggle_antialias(arg_t);
-cmdreturn_t it_toggle_alpha(arg_t);
+cmdreturn_t i_toggle_alpha(arg_t);
 cmdreturn_t i_change_gamma(arg_t);
 
 #endif /* COMMANDS_H */
diff --git a/config.def.h b/config.def.h
index 4236302..03d05bb 100644
--- a/config.def.h
+++ b/config.def.h
@@ -47,25 +47,22 @@ enum {
 static const double GAMMA_MAX   = 10.0;
 static const int    GAMMA_RANGE = 32;
 
+/* if false, pixelate images at zoom level != 100%,
+ * toggled with 'a' key binding
+ */
+static const bool ANTI_ALIAS = true;
+
+/* if true, use a checkerboard background for alpha layer,
+ * toggled with 'A' key binding
+ */
+static const bool ALPHA_LAYER = false;
+
 #endif
 #ifdef _THUMBS_CONFIG
 
 /* default dimension of thumbnails (width == height): */
 enum { THUMB_SIZE = 60 };
 
-#endif
-#ifdef _RENDER_CONFIG
-
-/* if false, pixelate images at zoom level != 100%,
- * toggled with 'a' key binding
- */
-static const bool RENDER_ANTI_ALIAS = true;
-
-/* if true, use white background for alpha layer,
- * toggled with 'A' key binding
- */
-static const bool RENDER_WHITE_ALPHA = false;
-
 #endif
 #ifdef _MAPPINGS_CONFIG
 
@@ -145,7 +142,7 @@ static const keymap_t keys[] = {
 	{ 0,            XK_s,             i_slideshow,          (arg_t) None },
 
 	{ 0,            XK_a,             i_toggle_antialias,   (arg_t) None },
-	{ 0,            XK_A,             it_toggle_alpha,      (arg_t) None },
+	{ 0,            XK_A,             i_toggle_alpha,       (arg_t) None },
 
 	{ 0,            XK_braceleft,     i_change_gamma,       (arg_t) -1 },
 	{ 0,            XK_braceright,    i_change_gamma,       (arg_t) +1 },
diff --git a/image.c b/image.c
index 3eedf52..e62a69f 100644
--- a/image.c
+++ b/image.c
@@ -18,7 +18,6 @@
 
 #define _POSIX_C_SOURCE 200112L
 #define _IMAGE_CONFIG
-#define _RENDER_CONFIG
 
 #include <stdlib.h>
 #include <string.h>
@@ -80,8 +79,8 @@ void img_init(img_t *img, win_t *win)
 	img->zoom = MIN(img->zoom, zoom_max);
 	img->checkpan = false;
 	img->dirty = false;
-	img->aa = RENDER_ANTI_ALIAS;
-	img->alpha = !RENDER_WHITE_ALPHA;
+	img->aa = ANTI_ALIAS;
+	img->alpha = ALPHA_LAYER;
 	img->multi.cap = img->multi.cnt = 0;
 	img->multi.animate = false;
 	img->multi.length = img->multi.repeat = 0;
@@ -497,13 +496,26 @@ void img_render(img_t *img)
 		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);
+		if (img->alpha) {
+			int i, c, r;
+			DATA32 col[2] = { 0xFF666666, 0xFF999999 };
+			DATA32 * data = imlib_image_get_data();
 
+			for (r = 0; r < dh; r++) {
+				i = r * dw;
+				if (r == 0 || r == 8) {
+					for (c = 0; c < dw; c++)
+						data[i++] = col[!(c & 8) ^ !r];
+				} else {
+					memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0]));
+				}
+			}
+			imlib_image_put_back_data(data);
+		} else {
+			c = win->fullscreen ? win->fscol : win->bgcol;
+			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);
diff --git a/sxiv.1 b/sxiv.1
index a14e1a7..6b74c13 100644
--- a/sxiv.1
+++ b/sxiv.1
@@ -121,9 +121,6 @@ Toggle visibility of info bar on bottom of window.
 .B Ctrl-x
 Send the next key to the external key-handler.
 .TP
-.B A
-Toggle visibility of alpha-channel, i.e. image transparency.
-.TP
 .B r
 Reload image.
 .TP
@@ -309,6 +306,9 @@ Reset gamma.
 .B a
 Toggle anti-aliasing.
 .TP
+.B A
+Toggle visibility of alpha-channel, i.e. image transparency.
+.TP
 .B s
 Toggle slideshow mode and/or set the delay between images to
 .I count
diff --git a/thumbs.c b/thumbs.c
index fe0ad84..80249d7 100644
--- a/thumbs.c
+++ b/thumbs.c
@@ -18,7 +18,6 @@
 
 #define _POSIX_C_SOURCE 200112L
 #define _THUMBS_CONFIG
-#define _RENDER_CONFIG
 
 #include <stdlib.h>
 #include <string.h>
@@ -177,7 +176,6 @@ void tns_init(tns_t *tns, int cnt, win_t *win)
 	tns->cap = cnt;
 	tns->cnt = tns->first = tns->sel = 0;
 	tns->win = win;
-	tns->alpha = !RENDER_WHITE_ALPHA;
 	tns->dirty = false;
 
 	if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
@@ -360,10 +358,6 @@ void tns_render(tns_t *tns)
 		t->x = x + (THUMB_SIZE - t->w) / 2;
 		t->y = y + (THUMB_SIZE - t->h) / 2;
 		imlib_context_set_image(t->im);
-
-		if (!tns->alpha && imlib_image_has_alpha())
-			win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
-
 		imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
 		                                            t->x, t->y, t->w, t->h);
 		if (t->file->marked)
diff --git a/thumbs.h b/thumbs.h
index 758ae87..e11ac3c 100644
--- a/thumbs.h
+++ b/thumbs.h
@@ -47,7 +47,6 @@ typedef struct {
 	int cols;
 	int rows;
 
-	bool alpha;
 	bool dirty;
 } tns_t;
 
diff --git a/window.c b/window.c
index a1fe122..ae169a4 100644
--- a/window.c
+++ b/window.c
@@ -163,7 +163,6 @@ void win_init(win_t *win)
 
 	win_init_font(e->dpy, BAR_FONT);
 
-	win->white     = WhitePixel(e->dpy, e->scr);
 	win->bgcol     = win_alloc_color(win, WIN_BG_COLOR);
 	win->fscol     = win_alloc_color(win, WIN_FS_COLOR);
 	win->selcol    = win_alloc_color(win, SEL_COLOR);
diff --git a/window.h b/window.h
index d274a93..475ae1a 100644
--- a/window.h
+++ b/window.h
@@ -53,7 +53,6 @@ typedef struct {
 	Window xwin;
 	win_env_t env;
 
-	unsigned long white;
 	unsigned long bgcol;
 	unsigned long fscol;
 	unsigned long selcol;