File: flip.c

package info (click to toggle)
wmaker 0.96.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,416 kB
  • sloc: ansic: 99,483; sh: 7,068; perl: 3,423; makefile: 1,647; lisp: 219; python: 34
file content (163 lines) | stat: -rw-r--r-- 3,507 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* flip.c - image flip
 *
 * Raster graphics library
 *
 * Copyright (c) 2014-2021 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"
#include "rotate.h"
#include "wr_i18n.h"


static RImage *r_flip_vertically(RImage *source);
static RImage *r_flip_horizontally(RImage *source);

/* Flip an image in the direction(s) specified */
RImage *RFlipImage(RImage *source, int mode)
{
	/* Security */
	if (source == NULL)
		return NULL;

	switch (mode & (RVerticalFlip | RHorizontalFlip)) {
	case RHorizontalFlip:
		return r_flip_horizontally(source);

	case RVerticalFlip:
		return r_flip_vertically(source);

	case RHorizontalFlip | RVerticalFlip:
		return wraster_rotate_image_180(source);

	default:
		return RRetainImage(source);
	}
}

RImage *r_flip_vertically(RImage *source)
{
	RImage *target;
	int nwidth, nheight;
	int x, y;

	nwidth = source->width;
	nheight = source->height;

	target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
	if (!target)
		return NULL;

	if (source->format == RRGBFormat) {
		unsigned char *optr, *nptr;

		optr = source->data;
		nptr = target->data + 3 * (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];

				optr += 3;
				nptr += 3;
			}
			nptr -= (nwidth * 3) * 2;
		}
	} else {
		unsigned char *optr, *nptr;

		optr = source->data;
		nptr = target->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 * 4) * 2;
		}
	}
	return target;
}

RImage *r_flip_horizontally(RImage *source)
{
	RImage *target;
	int nwidth, nheight;
	int x, y;

	nwidth = source->width;
	nheight = source->height;

	target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
	if (!target)
		return NULL;

	if (source->format == RRGBFormat) {
		unsigned char *optr, *nptr;

		optr = source->data;
		nptr = target->data + 3 * (nwidth - 1);

		for (y = nheight; y; y--) {
			for (x = 0; x < nwidth; x++) {
				nptr[0] = optr[0];
				nptr[1] = optr[1];
				nptr[2] = optr[2];

				optr += 3;
				nptr -= 3;
			}
			nptr += (nwidth * 3) * 2;
		}
	} else {
		unsigned char *optr, *nptr;

		optr = source->data;
		nptr = target->data + 4 * (nwidth - 1);

		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];

				optr += 4;
				nptr -= 4;
			}
			nptr += (nwidth * 4) * 2;
		}
	}
	return target;
}