WPrefs: add an image to represent the window in the Window Placement frame
[wmaker-crm.git] / wrlib / rotate.c
blob482b3174db81782e9599ee5b8a08486c3051ad30
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>
37 static RImage *rotate_image_90(RImage *source);
38 static RImage *rotate_image_270(RImage *source);
39 static RImage *rotate_image_any(RImage *source, float angle);
42 RImage *RRotateImage(RImage *image, float angle)
45 * Angle steps below this value would represent a rotation
46 * of less than 1 pixel for a 4k wide image, so not worth
47 * bothering the difference. That makes it a perfect
48 * candidate for an Epsilon when trying to compare angle
49 * to known values
51 static const float min_usable_angle = 0.00699;
53 angle = fmod(angle, 360.0);
54 if (angle < 0.0)
55 angle += 360.0;
57 if (angle < min_usable_angle) {
58 /* Rotate by 0 degree */
59 return RCloneImage(image);
61 } else if ((angle > 90.0 - min_usable_angle) &&
62 (angle < 90.0 + min_usable_angle)) {
63 return rotate_image_90(image);
65 } else if ((angle > 180.0 - min_usable_angle) &&
66 (angle < 180.0 + min_usable_angle)) {
67 return wraster_rotate_image_180(image);
69 } else if ((angle > 270.0 - min_usable_angle) &&
70 (angle < 270.0 + min_usable_angle)) {
71 return rotate_image_270(image);
73 } else {
74 return rotate_image_any(image, angle);
78 static RImage *rotate_image_90(RImage *source)
80 RImage *target;
81 int nwidth, nheight;
82 int x, y;
84 nwidth = source->height;
85 nheight = source->width;
87 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
88 if (!target)
89 return NULL;
91 if (source->format == RRGBFormat) {
92 unsigned char *optr, *nptr;
94 optr = source->data;
95 for (x = nwidth; x; x--) {
96 nptr = target->data + 3 * (x - 1);
97 for (y = nheight; y; y--) {
98 nptr[0] = *optr++;
99 nptr[1] = *optr++;
100 nptr[2] = *optr++;
102 nptr += 3 * nwidth;
106 } else {
107 unsigned char *optr, *nptr;
109 optr = source->data;
110 for (x = nwidth; x; x--) {
111 nptr = target->data + 4 * (x - 1);
112 for (y = nheight; y; y--) {
113 nptr[0] = *optr++;
114 nptr[1] = *optr++;
115 nptr[2] = *optr++;
116 nptr[3] = *optr++;
118 nptr += 4 * nwidth;
123 return target;
126 RImage *wraster_rotate_image_180(RImage *source)
128 RImage *target;
129 int nwidth, nheight;
130 int x, y;
132 nwidth = source->width;
133 nheight = source->height;
135 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
136 if (!target)
137 return NULL;
139 if (source->format == RRGBFormat) {
140 unsigned char *optr, *nptr;
142 optr = source->data;
143 nptr = target->data + nwidth * nheight * 3 - 3;
145 for (y = 0; y < nheight; y++) {
146 for (x = 0; x < nwidth; x++) {
147 nptr[0] = optr[0];
148 nptr[1] = optr[1];
149 nptr[2] = optr[2];
151 optr += 3;
152 nptr -= 3;
156 } else {
157 unsigned char *optr, *nptr;
159 optr = source->data;
160 nptr = target->data + nwidth * nheight * 4 - 4;
162 for (y = nheight * nwidth - 1; y >= 0; y--) {
163 nptr[0] = optr[0];
164 nptr[1] = optr[1];
165 nptr[2] = optr[2];
166 nptr[3] = optr[3];
168 optr += 4;
169 nptr -= 4;
173 return target;
176 static RImage *rotate_image_270(RImage *source)
178 RImage *target;
179 int nwidth, nheight;
180 int x, y;
182 nwidth = source->height;
183 nheight = source->width;
185 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
186 if (!target)
187 return NULL;
189 if (source->format == RRGBFormat) {
190 unsigned char *optr, *nptr;
192 optr = source->data;
193 for (x = nwidth; x; x--) {
194 nptr = target->data + 3 * nwidth * nheight - x * 3;
195 for (y = nheight; y; y--) {
196 nptr[0] = *optr++;
197 nptr[1] = *optr++;
198 nptr[2] = *optr++;
200 nptr -= 3 * nwidth;
204 } else {
205 unsigned char *optr, *nptr;
207 optr = source->data;
208 for (x = nwidth; x; x--) {
209 nptr = target->data + 4 * nwidth * nheight - x * 4;
210 for (y = nheight; y; y--) {
211 nptr[0] = *optr++;
212 nptr[1] = *optr++;
213 nptr[2] = *optr++;
214 nptr[3] = *optr++;
216 nptr -= 4 * nwidth;
221 return target;
225 * Image rotation through Bresenham's line algorithm:
227 * If a square must be rotate by angle a, like in:
228 * _______
229 * | B |
230 * | /4\ |
231 * | /3 8\|
232 * | /2 7 /|
233 * |A1 6 / | A_______B
234 * | \5 / a| <--- |1 2 3 4|
235 * |__C/_)_| |5 6 7 8|
236 * C-------
238 * for each point P1 in the line from C to A
239 * for each point P2 in the perpendicular line starting at P1
240 * get pixel from the source and plot at P2
241 * increment pixel location from source
245 #if 0
246 static void
247 copyLine(int x1, int y1, int x2, int y2, int nwidth, int format, unsigned char *dst, unsigned char **src)
249 unsigned char *s = *src;
250 int dx, dy;
251 int xi, yi;
252 int offset;
253 int dpr, dpru, p;
255 dx = abs(x2 - x1);
256 dy = abs(y2 - y1);
258 if (x1 > x2)
259 xi = -1;
260 else
261 xi = 1;
262 if (y1 > y2)
263 yi = -1;
264 else
265 yi = 1;
267 if (dx >= dy) {
269 dpr = dy << 1;
270 dpru = dpr - (dx << 1);
271 p = dpr - dx;
273 while (dx-- >= 0) {
274 /* fetch and draw the pixel */
275 offset = (x1 + y1 * nwidth) << 2;
276 dst[offset++] = *s++;
277 dst[offset++] = *s++;
278 dst[offset++] = *s++;
279 if (format == RRGBAFormat)
280 dst[offset++] = *s++;
281 else
282 dst[offset++] = 255;
284 /* calc next step */
285 if (p > 0) {
286 x1 += xi;
287 y1 += yi;
288 p += dpru;
289 } else {
290 x1 += xi;
291 p += dpr;
294 } else {
296 dpr = dx << 1;
297 dpru = dpr - (dy << 1);
298 p = dpr - dy;
300 while (dy-- >= 0) {
301 /* fetch and draw the pixel */
302 offset = (x1 + y1 * nwidth) << 2;
303 dst[offset++] = *s++;
304 dst[offset++] = *s++;
305 dst[offset++] = *s++;
306 if (format == RRGBAFormat)
307 dst[offset++] = *s++;
308 else
309 dst[offset++] = 255;
311 /* calc next step */
312 if (p > 0) {
313 x1 += xi;
314 y1 += yi;
315 p += dpru;
316 } else {
317 y1 += yi;
318 p += dpr;
323 *src = s;
325 #endif
327 static RImage *rotate_image_any(RImage *source, float angle)
329 (void) angle;
330 puts("NOT FULLY IMPLEMENTED");
331 return RCloneImage(source);
332 #if 0
333 RImage *img;
334 int nwidth, nheight;
335 int x1, y1;
336 int x2, y2;
337 int dx, dy;
338 int xi, yi;
339 int xx, yy;
340 unsigned char *src, *dst;
341 int dpr, dpru, p;
343 /* only 180o for now */
344 if (angle > 180.0)
345 angle -= 180.0;
347 angle = (angle * WM_PI) / 180.0;
349 nwidth = ceil(abs(cos(angle) * image->width))
350 + ceil(abs(cos(WM_PI / 2 - angle) * image->width));
352 nheight = ceil(abs(sin(angle) * image->height))
353 + ceil(abs(cos(WM_PI / 2 - angle) * image->height));
355 img = RCreateImage(nwidth, nheight, True);
356 if (!img)
357 return NULL;
359 src = image->data;
360 dst = img->data;
362 x1 = floor(abs(cos(WM_PI / 2 - angle) * image->width));
363 y1 = 0;
365 x2 = 0;
366 y2 = floor(abs(sin(WM_PI / 2 - angle) * image->width));
368 xx = floor(abs(cos(angle) * image->height)) - 1;
369 yy = nheight - 1;
371 printf("%ix%i, %i %i %i %i %i\n", nwidth, nheight, x1, y1, x2, y2, (int)((angle * 180.0) / WM_PI));
373 dx = abs(x2 - x1);
374 dy = abs(y2 - y1);
376 if (x1 > x2)
377 xi = -1;
378 else
379 xi = 1;
380 if (y1 > y2)
381 yi = -1;
382 else
383 yi = 1;
385 if (dx >= dy) {
386 dpr = dy << 1;
387 dpru = dpr - (dx << 1);
388 p = dpr - dx;
390 while (dx-- >= 0) {
392 copyLine(x1, y1, xx, yy, nwidth, image->format, dst, &src);
394 /* calc next step */
396 if (p > 0) {
397 x1 += xi;
398 y1 += yi;
399 xx += xi;
400 yy += yi;
401 p += dpru;
402 } else {
403 x1 += xi;
404 xx += xi;
405 p += dpr;
408 } else {
409 dpr = dx << 1;
410 dpru = dpr - (dy << 1);
411 p = dpr - dy;
413 while (dy-- >= 0) {
414 xx = abs(x1 * sin(angle * WM_PI / 180.0));
415 yy = abs(y1 * cos(angle * WM_PI / 180.0));
417 copyLine(x1, y1, xx, yy, nwidth, image->format, dst, &src);
419 /* calc next step */
420 if (p > 0) {
421 x1 += xi;
422 y1 += yi;
423 p += dpru;
424 } else {
425 y1 += yi;
426 p += dpr;
431 return img;
432 #endif