From ed727150743339d0d3bcafdb733ddd48c14463d4 Mon Sep 17 00:00:00 2001
From: David Maciejak <david.maciejak@gmail.com>
Date: Mon, 26 May 2014 22:42:44 +0800
Subject: [PATCH] wrlib: fixed transformation functions

With a set of images i was able to detect that
the flip functions was shifting the image by 1px.

The 90 and 270 degrees rotation were not working as
the functions were also mirroring the img.

The patch is also fixing the C style based on checkpatch.
---
 wrlib/flip.c   | 116 +++++++++++++++++++++++++++++----------------------------
 wrlib/rotate.c |  75 ++++++++++++++++++-------------------
 2 files changed, 97 insertions(+), 94 deletions(-)

diff --git a/wrlib/flip.c b/wrlib/flip.c
index 6801783..f49bfd9 100644
--- a/wrlib/flip.c
+++ b/wrlib/flip.c
@@ -43,42 +43,42 @@ RImage *RVerticalFlipImage(RImage *image)
 		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;
+		unsigned char *optr, *nptr;
+
+		optr = image->data;
+		nptr = img->data + 4 * (nwidth * nheight - nwidth);
+
+		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;
 			}
-		} 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;
+			nptr -= nwidth*8;
+		}
+	} else {
+		unsigned char *optr, *nptr;
+
+		optr = image->data;
+		nptr = img->data + 4 * (nwidth * nheight - nwidth);
+
+		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;
 }
 
@@ -97,37 +97,41 @@ RImage *RHorizontalFlipImage(RImage *image)
 		return NULL;
 
 	if (bpp == 3) {
-			unsigned char *optr, *nptr;
+		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;
+		optr = image->data;
+		nptr = img->data + 4 * (nwidth - 1);
 
-					nptr[0] = optr[0];
-					nptr[1] = optr[1];
-					nptr[2] = optr[2];
-					nptr[3] = 255;
+		for (y = nheight; y; y--) {
+			for (x = 0; x < nwidth; x++) {
+				nptr[0] = optr[0];
+				nptr[1] = optr[1];
+				nptr[2] = optr[2];
+				nptr[3] = 255;
 
-					nptr -= 4;
-				}
+				optr += 3;
+				nptr -= 4;
 			}
-		} else {
-			unsigned char *optr, *nptr;
+			nptr += 8 * nwidth;
+		}
+	} 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;
+		optr = image->data;
+		nptr = img->data + 4 * (nwidth - 1);
 
-					nptr[0] = optr[0];
-					nptr[1] = optr[1];
-					nptr[2] = optr[2];
-					nptr[3] = optr[3];
+		for (y = nheight; y; y--) {
+			for (x = 0; x < nwidth; x++) {
+				nptr[0] = optr[0];
+				nptr[1] = optr[1];
+				nptr[2] = optr[2];
+				nptr[3] = optr[3];
 
-					nptr -= 4;
-				}
+				optr += 4;
+				nptr -= 4;
 			}
+			nptr += 8 * nwidth;
 		}
+	}
 	return img;
 }
diff --git a/wrlib/rotate.c b/wrlib/rotate.c
index 09e8a0a..f2f92b9 100644
--- a/wrlib/rotate.c
+++ b/wrlib/rotate.c
@@ -34,9 +34,9 @@
 #define PI 3.14159265358979323846
 #endif
 
-static RImage *rotateImage(RImage * image, float angle);
+static RImage *rotateImage(RImage *image, float angle);
 
-RImage *RRotateImage(RImage * image, float angle)
+RImage *RRotateImage(RImage *image, float angle)
 {
 	RImage *img;
 	int nwidth, nheight;
@@ -66,40 +66,41 @@ RImage *RRotateImage(RImage * image, float angle)
 		nheight = image->width;
 
 		img = RCreateImage(nwidth, nheight, True);
-		if (!img) {
+		if (!img)
 			return NULL;
-		}
 
 		if (bpp == 3) {
 			unsigned char *optr, *nptr;
-			unsigned offs;
-
-			offs = nwidth * 4;
 
 			optr = image->data;
+			nptr = img->data;
 
-			for (x = 0; x < nwidth; x++) {
-				nptr = img->data + x * 4;
+			for (x = nwidth; x; x--) {
+				nptr = img->data + 4 * (x - 1);
 				for (y = nheight; y; y--) {
 					nptr[0] = *optr++;
 					nptr[1] = *optr++;
 					nptr[2] = *optr++;
 					nptr[3] = 255;
 
-					nptr += offs;
+					nptr += 4 * nwidth;
 				}
 			}
 		} else {
-			unsigned *optr, *nptr;
-			unsigned *p;
+			unsigned char *optr, *nptr;
 
-			optr = (unsigned *)image->data;
-			p = (unsigned *)img->data;
-			for (x = 0; x < nwidth; x++) {
-				nptr = p++;
+			optr = image->data;
+			nptr = img->data;
+
+			for (x = nwidth; x; x--) {
+				nptr = img->data + 4 * (x - 1);
 				for (y = nheight; y; y--) {
-					*nptr = *optr++;
-					nptr += nwidth;
+					nptr[0] = *optr++;
+					nptr[1] = *optr++;
+					nptr[2] = *optr++;
+					nptr[3] = *optr++;
+
+					nptr += 4 * nwidth;
 				}
 			}
 		}
@@ -109,9 +110,8 @@ RImage *RRotateImage(RImage * image, float angle)
 		nwidth = image->width;
 		nheight = image->height;
 		img = RCreateImage(nwidth, nheight, True);
-		if (!img) {
+		if (!img)
 			return NULL;
-		}
 
 		if (bpp == 3) {
 			unsigned char *optr, *nptr;
@@ -148,40 +148,39 @@ RImage *RRotateImage(RImage * image, float angle)
 		nheight = image->width;
 
 		img = RCreateImage(nwidth, nheight, True);
-		if (!img) {
+		if (!img)
 			return NULL;
-		}
 
 		if (bpp == 3) {
 			unsigned char *optr, *nptr;
-			unsigned offs;
-
-			offs = nwidth * 4;
 
 			optr = image->data;
 
-			for (x = 0; x < nwidth; x++) {
-				nptr = img->data + x * 4;
+			for (x = nwidth; x; x--) {
+				nptr = img->data + 4 * nwidth * nheight - x * 4;
 				for (y = nheight; y; y--) {
 					nptr[0] = *optr++;
 					nptr[1] = *optr++;
 					nptr[2] = *optr++;
 					nptr[3] = 255;
 
-					nptr += offs;
+					nptr -= 4 * nwidth;
 				}
 			}
 		} else {
-			unsigned *optr, *nptr;
-			unsigned *p;
+			unsigned char *optr, *nptr;
 
-			optr = (unsigned *)image->data;
-			p = (unsigned *)img->data + nwidth * nheight;
-			for (x = 0; x < nwidth; x++) {
-				nptr = p--;
+			optr = image->data;
+
+			for (x = nwidth; x; x--) {
+				nptr = img->data + 4 * nwidth * nheight - x * 4;
 				for (y = nheight; y; y--) {
-					*nptr = *optr++;
-					nptr -= nwidth;
+					nptr[0] = *optr++;
+					nptr[1] = *optr++;
+					nptr[2] = *optr++;
+					nptr[3] = *optr++;
+
+					nptr -= 4 * nwidth;
 				}
 			}
 		}
@@ -209,7 +208,7 @@ RImage *RRotateImage(RImage * image, float angle)
  * for each point P1 in the line from C to A
  *	for each point P2 in the perpendicular line starting at P1
  *		get pixel from the source and plot at P2
- * 		increment pixel location from source
+ *		increment pixel location from source
  *
  */
 
@@ -295,7 +294,7 @@ copyLine(int x1, int y1, int x2, int y2, int nwidth, int format, unsigned char *
 }
 #endif
 
-static RImage *rotateImage(RImage * image, float angle)
+static RImage *rotateImage(RImage *image, float angle)
 {
 	(void) angle;
 	puts("NOT FULLY IMPLEMENTED");
-- 
1.8.3.2

