From a89de98dce68e4ed256cb00c8a6b78815d5b1cd5 Mon Sep 17 00:00:00 2001
From: David Maciejak <david.maciejak@gmail.com>
Date: Wed, 21 May 2014 11:59:48 +0700
Subject: [PATCH] wrlib: add image flip functions

This patch adds RVerticalFlipImage and RHorizontalFlipImage functions.
---
 wrlib/Makefile.am    |   1 +
 wrlib/flip.c         | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++
 wrlib/libwraster.map |   2 +
 wrlib/wraster.h      |   3 ++
 4 files changed, 141 insertions(+)
 create mode 100644 wrlib/flip.c

diff --git a/wrlib/Makefile.am b/wrlib/Makefile.am
index 3f229c5..489b4a4 100644
--- a/wrlib/Makefile.am
+++ b/wrlib/Makefile.am
@@ -34,6 +34,7 @@ libwraster_la_SOURCES = 	\
 	misc.c 		\
 	scale.c		\
 	rotate.c	\
+	flip.c		\
 	convolve.c	\
 	save_xpm.c	\
 	xutil.c		\
diff --git a/wrlib/flip.c b/wrlib/flip.c
new file mode 100644
index 0000000..e0aeb5c
--- /dev/null
+++ b/wrlib/flip.c
@@ -0,0 +1,135 @@
+/* flip.c - image flip
+ *
+ * Raster graphics library
+ *
+ * Copyright (c) 2014 Window Maker Team
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Library General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Library General Public
+ *  License along with this library; if not, write to the Free
+ *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+ *  MA 02110-1301, USA.
+ */
+
+#include <config.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <X11/Xlib.h>
+#include "wraster.h"
+
+RImage *RVerticalFlipImage(RImage *image)
+{
+	RImage *img;
+	int nwidth, nheight;
+	int x, y;
+	int bpp = image->format == RRGBAFormat ? 4 : 3;
+
+	nwidth = image->width;
+	nheight = image->height;
+
+	img = RCreateImage(nwidth, nheight, True);
+	if (!img) {
+		return NULL;
+	}
+
+	if (bpp == 3) {
+			unsigned char *optr, *nptr;
+
+			optr = image->data;
+			nptr = img->data + 4 * (nwidth * nheight - nwidth - 1);
+
+			for (y = 0; y < nheight; y++) {
+				for (x = 0; x < nwidth; x++) {
+					nptr[0] = optr[0];
+					nptr[1] = optr[1];
+					nptr[2] = optr[2];
+					nptr[3] = 255;
+
+					optr += 3;
+					nptr += 4;
+				}
+				nptr -= nwidth*8;
+			}
+		} else {
+			unsigned char *optr, *nptr;
+
+			optr = image->data;
+			nptr = img->data + 4 * (nwidth * nheight - nwidth - 1);
+
+			for (y = 0; y < nheight; y++) {
+				for (x = 0; x < nwidth; x++) {
+					nptr[0] = optr[0];
+					nptr[1] = optr[1];
+					nptr[2] = optr[2];
+					nptr[3] = optr[3];
+
+					optr += 4;
+					nptr += 4;
+				}
+				nptr -= nwidth*8;
+			}
+		}
+	return img;
+}
+
+RImage *RHorizontalFlipImage(RImage *image)
+{
+	RImage *img;
+	int nwidth, nheight;
+	int x, y;
+	int bpp = image->format == RRGBAFormat ? 4 : 3;
+
+	nwidth = image->width;
+	nheight = image->height;
+
+	img = RCreateImage(nwidth, nheight, True);
+	if (!img) {
+		return NULL;
+	}
+
+	if (bpp == 3) {
+			unsigned char *optr, *nptr;
+
+			nptr = img->data + nwidth * nheight * 4 - 4;
+			for (y = nheight; y; y--) {
+				for (x = 0; x < nwidth; x++) {
+					optr = image->data + (y*nwidth + x) * 3;
+
+					nptr[0] = optr[0];
+					nptr[1] = optr[1];
+					nptr[2] = optr[2];
+					nptr[3] = 255;
+
+					nptr -= 4;
+				}
+			}
+		} else {
+			unsigned char *optr, *nptr;
+
+			nptr = img->data + nwidth * nheight * 4 - 4;
+			for (y = nheight; y; y--) {
+				for (x = 0; x < nwidth; x++) {
+					optr = image->data + (y*nwidth + x) * 4;
+
+					nptr[0] = optr[0];
+					nptr[1] = optr[1];
+					nptr[2] = optr[2];
+					nptr[3] = optr[3];
+
+					nptr -= 4;
+				}
+			}
+		}
+	return img;
+}
diff --git a/wrlib/libwraster.map b/wrlib/libwraster.map
index 062dcd9..cf36401 100644
--- a/wrlib/libwraster.map
+++ b/wrlib/libwraster.map
@@ -72,6 +72,8 @@ LIBWRASTER3
     RRetainImage;
     RRGBtoHSV;
     RRotateImage;
+    RVerticalFlipImage;
+    RHorizontalFlipImage;
     RSaveImage;
     RScaleImage;
     RShutdown;
diff --git a/wrlib/wraster.h b/wrlib/wraster.h
index 2003869..45cbcc6 100644
--- a/wrlib/wraster.h
+++ b/wrlib/wraster.h
@@ -371,6 +371,9 @@ RImage *RSmoothScaleImage(RImage *src, unsigned new_width,
 
 RImage *RRotateImage(RImage *image, float angle);
 
+RImage *RVerticalFlipImage(RImage *image);
+
+RImage *RHorizontalFlipImage(RImage *image);
 
 RImage *RMakeTiledImage(RImage *tile, unsigned width, unsigned height);
 
-- 
1.8.3.2

