util: clarify a bit of the code for parsing commands in wmgenmenu
[wmaker-crm.git] / wrlib / rotate.c
blob84b34b7a415e09fe816739f8c524ad7f3b8126be
1 /* rotate.c - image rotation
3 * Raster graphics library
5 * Copyright (c) 2000-2003 Alfredo K. Kojima
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>
29 #include <X11/Xlib.h>
31 #include "wraster.h"
32 #include "rotate.h"
34 #include <math.h>
36 #ifndef PI
37 #define PI 3.14159265358979323846
38 #endif
40 static RImage *rotate_image_90(RImage *source);
41 static RImage *rotate_image_270(RImage *source);
42 static RImage *rotate_image_any(RImage *source, float angle);
45 RImage *RRotateImage(RImage *image, float angle)
48 * Angle steps below this value would represent a rotation
49 * of less than 1 pixel for a 4k wide image, so not worth
50 * bothering the difference. That makes it a perfect
51 * candidate for an Epsilon when trying to compare angle
52 * to known values
54 static const float min_usable_angle = 0.00699;
56 angle = fmod(angle, 360.0);
57 if (angle < 0.0)
58 angle += 360.0;
60 if (angle < min_usable_angle) {
61 /* Rotate by 0 degree */
62 return RCloneImage(image);
64 } else if ((angle > 90.0 - min_usable_angle) &&
65 (angle < 90.0 + min_usable_angle)) {
66 return rotate_image_90(image);
68 } else if ((angle > 180.0 - min_usable_angle) &&
69 (angle < 180.0 + min_usable_angle)) {
70 return wraster_rotate_image_180(image);
72 } else if ((angle > 270.0 - min_usable_angle) &&
73 (angle < 270.0 + min_usable_angle)) {
74 return rotate_image_270(image);
76 } else {
77 return rotate_image_any(image, angle);
81 static RImage *rotate_image_90(RImage *source)
83 RImage *target;
84 int nwidth, nheight;
85 int x, y;
87 nwidth = source->height;
88 nheight = source->width;
90 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
91 if (!target)
92 return NULL;
94 if (source->format == RRGBFormat) {
95 unsigned char *optr, *nptr;
97 optr = source->data;
98 for (x = nwidth; x; x--) {
99 nptr = target->data + 3 * (x - 1);
100 for (y = nheight; y; y--) {
101 nptr[0] = *optr++;
102 nptr[1] = *optr++;
103 nptr[2] = *optr++;
105 nptr += 3 * nwidth;
109 } else {
110 unsigned char *optr, *nptr;
112 optr = source->data;
113 for (x = nwidth; x; x--) {
114 nptr = target->data + 4 * (x - 1);
115 for (y = nheight; y; y--) {
116 nptr[0] = *optr++;
117 nptr[1] = *optr++;
118 nptr[2] = *optr++;
119 nptr[3] = *optr++;
121 nptr += 4 * nwidth;
126 return target;
129 RImage *wraster_rotate_image_180(RImage *source)
131 RImage *target;
132 int nwidth, nheight;
133 int x, y;
135 nwidth = source->width;
136 nheight = source->height;
138 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
139 if (!target)
140 return NULL;
142 if (source->format == RRGBFormat) {
143 unsigned char *optr, *nptr;
145 optr = source->data;
146 nptr = target->data + nwidth * nheight * 3 - 3;
148 for (y = 0; y < nheight; y++) {
149 for (x = 0; x < nwidth; x++) {
150 nptr[0] = optr[0];
151 nptr[1] = optr[1];
152 nptr[2] = optr[2];
154 optr += 3;
155 nptr -= 3;
159 } else {
160 unsigned char *optr, *nptr;
162 optr = source->data;
163 nptr = target->data + nwidth * nheight * 4 - 4;
165 for (y = nheight * nwidth - 1; y >= 0; y--) {
166 nptr[0] = optr[0];
167 nptr[1] = optr[1];
168 nptr[2] = optr[2];
169 nptr[3] = optr[3];
171 optr += 4;
172 nptr -= 4;
176 return target;
179 static RImage *rotate_image_270(RImage *source)
181 RImage *target;
182 int nwidth, nheight;
183 int x, y;
185 nwidth = source->height;
186 nheight = source->width;
188 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
189 if (!target)
190 return NULL;
192 if (source->format == RRGBFormat) {
193 unsigned char *optr, *nptr;
195 optr = source->data;
196 for (x = nwidth; x; x--) {
197 nptr = target->data + 3 * nwidth * nheight - x * 3;
198 for (y = nheight; y; y--) {
199 nptr[0] = *optr++;
200 nptr[1] = *optr++;
201 nptr[2] = *optr++;
203 nptr -= 3 * nwidth;
207 } else {
208 unsigned char *optr, *nptr;
210 optr = source->data;
211 for (x = nwidth; x; x--) {
212 nptr = target->data + 4 * nwidth * nheight - x * 4;
213 for (y = nheight; y; y--) {
214 nptr[0] = *optr++;
215 nptr[1] = *optr++;
216 nptr[2] = *optr++;
217 nptr[3] = *optr++;
219 nptr -= 4 * nwidth;
224 return target;
228 * Image rotation through Bresenham's line algorithm:
230 * If a square must be rotate by angle a, like in:
231 * _______
232 * | B |
233 * | /4\ |
234 * | /3 8\|
235 * | /2 7 /|
236 * |A1 6 / | A_______B
237 * | \5 / a| <--- |1 2 3 4|
238 * |__C/_)_| |5 6 7 8|
239 * C-------
241 * for each point P1 in the line from C to A
242 * for each point P2 in the perpendicular line starting at P1
243 * get pixel from the source and plot at P2
244 * increment pixel location from source
248 #if 0
249 static void
250 copyLine(int x1, int y1, int x2, int y2, int nwidth, int format, unsigned char *dst, unsigned char **src)
252 unsigned char *s = *src;
253 int dx, dy;
254 int xi, yi;
255 int offset;
256 int dpr, dpru, p;
258 dx = abs(x2 - x1);
259 dy = abs(y2 - y1);
261 if (x1 > x2)
262 xi = -1;
263 else
264 xi = 1;
265 if (y1 > y2)
266 yi = -1;
267 else
268 yi = 1;
270 if (dx >= dy) {
272 dpr = dy << 1;
273 dpru = dpr - (dx << 1);
274 p = dpr - dx;
276 while (dx-- >= 0) {
277 /* fetch and draw the pixel */
278 offset = (x1 + y1 * nwidth) << 2;
279 dst[offset++] = *s++;
280 dst[offset++] = *s++;
281 dst[offset++] = *s++;
282 if (format == RRGBAFormat)
283 dst[offset++] = *s++;
284 else
285 dst[offset++] = 255;
287 /* calc next step */
288 if (p > 0) {
289 x1 += xi;
290 y1 += yi;
291 p += dpru;
292 } else {
293 x1 += xi;
294 p += dpr;
297 } else {
299 dpr = dx << 1;
300 dpru = dpr - (dy << 1);
301 p = dpr - dy;
303 while (dy-- >= 0) {
304 /* fetch and draw the pixel */
305 offset = (x1 + y1 * nwidth) << 2;
306 dst[offset++] = *s++;
307 dst[offset++] = *s++;
308 dst[offset++] = *s++;
309 if (format == RRGBAFormat)
310 dst[offset++] = *s++;
311 else
312 dst[offset++] = 255;
314 /* calc next step */
315 if (p > 0) {
316 x1 += xi;
317 y1 += yi;
318 p += dpru;
319 } else {
320 y1 += yi;
321 p += dpr;
326 *src = s;
328 #endif
330 static RImage *rotate_image_any(RImage *source, float angle)
332 (void) angle;
333 puts("NOT FULLY IMPLEMENTED");
334 return RCloneImage(source);
335 #if 0
336 RImage *img;
337 int nwidth, nheight;
338 int x1, y1;
339 int x2, y2;
340 int dx, dy;
341 int xi, yi;
342 int xx, yy;
343 unsigned char *src, *dst;
344 int dpr, dpru, p;
346 /* only 180o for now */
347 if (angle > 180.0)
348 angle -= 180.0;
350 angle = (angle * PI) / 180.0;
352 nwidth = ceil(abs(cos(angle) * image->width))
353 + ceil(abs(cos(PI / 2 - angle) * image->width));
355 nheight = ceil(abs(sin(angle) * image->height))
356 + ceil(abs(cos(PI / 2 - angle) * image->height));
358 img = RCreateImage(nwidth, nheight, True);
359 if (!img)
360 return NULL;
362 src = image->data;
363 dst = img->data;
365 x1 = floor(abs(cos(PI / 2 - angle) * image->width));
366 y1 = 0;
368 x2 = 0;
369 y2 = floor(abs(sin(PI / 2 - angle) * image->width));
371 xx = floor(abs(cos(angle) * image->height)) - 1;
372 yy = nheight - 1;
374 printf("%ix%i, %i %i %i %i %i\n", nwidth, nheight, x1, y1, x2, y2, (int)((angle * 180.0) / PI));
376 dx = abs(x2 - x1);
377 dy = abs(y2 - y1);
379 if (x1 > x2)
380 xi = -1;
381 else
382 xi = 1;
383 if (y1 > y2)
384 yi = -1;
385 else
386 yi = 1;
388 if (dx >= dy) {
389 dpr = dy << 1;
390 dpru = dpr - (dx << 1);
391 p = dpr - dx;
393 while (dx-- >= 0) {
395 copyLine(x1, y1, xx, yy, nwidth, image->format, dst, &src);
397 /* calc next step */
399 if (p > 0) {
400 x1 += xi;
401 y1 += yi;
402 xx += xi;
403 yy += yi;
404 p += dpru;
405 } else {
406 x1 += xi;
407 xx += xi;
408 p += dpr;
411 } else {
412 dpr = dx << 1;
413 dpru = dpr - (dy << 1);
414 p = dpr - dy;
416 while (dy-- >= 0) {
417 xx = abs(x1 * sin(angle * PI / 180.0));
418 yy = abs(y1 * cos(angle * PI / 180.0));
420 copyLine(x1, y1, xx, yy, nwidth, image->format, dst, &src);
422 /* calc next step */
423 if (p > 0) {
424 x1 += xi;
425 y1 += yi;
426 p += dpru;
427 } else {
428 y1 += yi;
429 p += dpr;
434 return img;
435 #endif