wrlib: add image flip functions
[wmaker-crm.git] / wrlib / flip.c
blob6801783c706edb45e7545b4bd44f594cbcf71834
1 /* flip.c - image flip
3 * Raster graphics library
5 * Copyright (c) 2014 Window Maker Team
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 #include <config.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <X11/Xlib.h>
29 #include "wraster.h"
31 RImage *RVerticalFlipImage(RImage *image)
33 RImage *img;
34 int nwidth, nheight;
35 int x, y;
36 int bpp = image->format == RRGBAFormat ? 4 : 3;
38 nwidth = image->width;
39 nheight = image->height;
41 img = RCreateImage(nwidth, nheight, True);
42 if (!img)
43 return NULL;
45 if (bpp == 3) {
46 unsigned char *optr, *nptr;
48 optr = image->data;
49 nptr = img->data + 4 * (nwidth * nheight - nwidth - 1);
51 for (y = 0; y < nheight; y++) {
52 for (x = 0; x < nwidth; x++) {
53 nptr[0] = optr[0];
54 nptr[1] = optr[1];
55 nptr[2] = optr[2];
56 nptr[3] = 255;
58 optr += 3;
59 nptr += 4;
61 nptr -= nwidth*8;
63 } else {
64 unsigned char *optr, *nptr;
66 optr = image->data;
67 nptr = img->data + 4 * (nwidth * nheight - nwidth - 1);
69 for (y = 0; y < nheight; y++) {
70 for (x = 0; x < nwidth; x++) {
71 nptr[0] = optr[0];
72 nptr[1] = optr[1];
73 nptr[2] = optr[2];
74 nptr[3] = optr[3];
76 optr += 4;
77 nptr += 4;
79 nptr -= nwidth*8;
82 return img;
85 RImage *RHorizontalFlipImage(RImage *image)
87 RImage *img;
88 int nwidth, nheight;
89 int x, y;
90 int bpp = image->format == RRGBAFormat ? 4 : 3;
92 nwidth = image->width;
93 nheight = image->height;
95 img = RCreateImage(nwidth, nheight, True);
96 if (!img)
97 return NULL;
99 if (bpp == 3) {
100 unsigned char *optr, *nptr;
102 nptr = img->data + nwidth * nheight * 4 - 4;
103 for (y = nheight; y; y--) {
104 for (x = 0; x < nwidth; x++) {
105 optr = image->data + (y*nwidth + x) * 3;
107 nptr[0] = optr[0];
108 nptr[1] = optr[1];
109 nptr[2] = optr[2];
110 nptr[3] = 255;
112 nptr -= 4;
115 } else {
116 unsigned char *optr, *nptr;
118 nptr = img->data + nwidth * nheight * 4 - 4;
119 for (y = nheight; y; y--) {
120 for (x = 0; x < nwidth; x++) {
121 optr = image->data + (y*nwidth + x) * 4;
123 nptr[0] = optr[0];
124 nptr[1] = optr[1];
125 nptr[2] = optr[2];
126 nptr[3] = optr[3];
128 nptr -= 4;
132 return img;