Update Serbian translation from master branch
[wmaker-crm.git] / wrlib / rotate.c
blob12cd41fdd85c56f7b9844f4d3e28be5c76e16ac4
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"
33 #include "wr_i18n.h"
35 #include <math.h>
38 static RImage *rotate_image_90(RImage *source);
39 static RImage *rotate_image_270(RImage *source);
40 static RImage *rotate_image_any(RImage *source, float angle);
43 RImage *RRotateImage(RImage *image, float angle)
46 * Angle steps below this value would represent a rotation
47 * of less than 1 pixel for a 4k wide image, so not worth
48 * bothering the difference. That makes it a perfect
49 * candidate for an Epsilon when trying to compare angle
50 * to known values
52 static const float min_usable_angle = 0.00699F;
54 angle = fmod(angle, 360.0);
55 if (angle < 0.0F)
56 angle += 360.0F;
58 if (angle < min_usable_angle) {
59 /* Rotate by 0 degree */
60 return RCloneImage(image);
62 } else if ((angle > 90.0F - min_usable_angle) &&
63 (angle < 90.0F + min_usable_angle)) {
64 return rotate_image_90(image);
66 } else if ((angle > 180.0F - min_usable_angle) &&
67 (angle < 180.0F + min_usable_angle)) {
68 return wraster_rotate_image_180(image);
70 } else if ((angle > 270.0F - min_usable_angle) &&
71 (angle < 270.0F + min_usable_angle)) {
72 return rotate_image_270(image);
74 } else {
75 return rotate_image_any(image, angle);
79 static RImage *rotate_image_90(RImage *source)
81 RImage *target;
82 int nwidth, nheight;
83 int x, y;
85 nwidth = source->height;
86 nheight = source->width;
88 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
89 if (!target)
90 return NULL;
92 if (source->format == RRGBFormat) {
93 unsigned char *optr, *nptr;
95 optr = source->data;
96 for (x = nwidth; x; x--) {
97 nptr = target->data + 3 * (x - 1);
98 for (y = nheight; y; y--) {
99 nptr[0] = *optr++;
100 nptr[1] = *optr++;
101 nptr[2] = *optr++;
103 nptr += 3 * nwidth;
107 } else {
108 unsigned char *optr, *nptr;
110 optr = source->data;
111 for (x = nwidth; x; x--) {
112 nptr = target->data + 4 * (x - 1);
113 for (y = nheight; y; y--) {
114 nptr[0] = *optr++;
115 nptr[1] = *optr++;
116 nptr[2] = *optr++;
117 nptr[3] = *optr++;
119 nptr += 4 * nwidth;
124 return target;
127 RImage *wraster_rotate_image_180(RImage *source)
129 RImage *target;
130 int nwidth, nheight;
131 int x, y;
133 nwidth = source->width;
134 nheight = source->height;
136 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
137 if (!target)
138 return NULL;
140 if (source->format == RRGBFormat) {
141 unsigned char *optr, *nptr;
143 optr = source->data;
144 nptr = target->data + nwidth * nheight * 3 - 3;
146 for (y = 0; y < nheight; y++) {
147 for (x = 0; x < nwidth; x++) {
148 nptr[0] = optr[0];
149 nptr[1] = optr[1];
150 nptr[2] = optr[2];
152 optr += 3;
153 nptr -= 3;
157 } else {
158 unsigned char *optr, *nptr;
160 optr = source->data;
161 nptr = target->data + nwidth * nheight * 4 - 4;
163 for (y = nheight * nwidth - 1; y >= 0; y--) {
164 nptr[0] = optr[0];
165 nptr[1] = optr[1];
166 nptr[2] = optr[2];
167 nptr[3] = optr[3];
169 optr += 4;
170 nptr -= 4;
174 return target;
177 static RImage *rotate_image_270(RImage *source)
179 RImage *target;
180 int nwidth, nheight;
181 int x, y;
183 nwidth = source->height;
184 nheight = source->width;
186 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
187 if (!target)
188 return NULL;
190 if (source->format == RRGBFormat) {
191 unsigned char *optr, *nptr;
193 optr = source->data;
194 for (x = nwidth; x; x--) {
195 nptr = target->data + 3 * nwidth * nheight - x * 3;
196 for (y = nheight; y; y--) {
197 nptr[0] = *optr++;
198 nptr[1] = *optr++;
199 nptr[2] = *optr++;
201 nptr -= 3 * nwidth;
205 } else {
206 unsigned char *optr, *nptr;
208 optr = source->data;
209 for (x = nwidth; x; x--) {
210 nptr = target->data + 4 * nwidth * nheight - x * 4;
211 for (y = nheight; y; y--) {
212 nptr[0] = *optr++;
213 nptr[1] = *optr++;
214 nptr[2] = *optr++;
215 nptr[3] = *optr++;
217 nptr -= 4 * nwidth;
222 return target;
226 * Image rotation through Bresenham's line algorithm:
228 * If a square must be rotate by angle a, like in:
229 * _______
230 * | B |
231 * | /4\ |
232 * | /3 8\|
233 * | /2 7 /|
234 * |A1 6 / | A_______B
235 * | \5 / a| <--- |1 2 3 4|
236 * |__C/_)_| |5 6 7 8|
237 * C-------
239 * for each point P1 in the line from C to A
240 * for each point P2 in the perpendicular line starting at P1
241 * get pixel from the source and plot at P2
242 * increment pixel location from source
246 #if 0
247 static void
248 copyLine(int x1, int y1, int x2, int y2, int nwidth, int format, unsigned char *dst, unsigned char **src)
250 unsigned char *s = *src;
251 int dx, dy;
252 int xi, yi;
253 int offset;
254 int dpr, dpru, p;
256 dx = abs(x2 - x1);
257 dy = abs(y2 - y1);
259 if (x1 > x2)
260 xi = -1;
261 else
262 xi = 1;
263 if (y1 > y2)
264 yi = -1;
265 else
266 yi = 1;
268 if (dx >= dy) {
270 dpr = dy << 1;
271 dpru = dpr - (dx << 1);
272 p = dpr - dx;
274 while (dx-- >= 0) {
275 /* fetch and draw the pixel */
276 offset = (x1 + y1 * nwidth) << 2;
277 dst[offset++] = *s++;
278 dst[offset++] = *s++;
279 dst[offset++] = *s++;
280 if (format == RRGBAFormat)
281 dst[offset++] = *s++;
282 else
283 dst[offset++] = 255;
285 /* calc next step */
286 if (p > 0) {
287 x1 += xi;
288 y1 += yi;
289 p += dpru;
290 } else {
291 x1 += xi;
292 p += dpr;
295 } else {
297 dpr = dx << 1;
298 dpru = dpr - (dy << 1);
299 p = dpr - dy;
301 while (dy-- >= 0) {
302 /* fetch and draw the pixel */
303 offset = (x1 + y1 * nwidth) << 2;
304 dst[offset++] = *s++;
305 dst[offset++] = *s++;
306 dst[offset++] = *s++;
307 if (format == RRGBAFormat)
308 dst[offset++] = *s++;
309 else
310 dst[offset++] = 255;
312 /* calc next step */
313 if (p > 0) {
314 x1 += xi;
315 y1 += yi;
316 p += dpru;
317 } else {
318 y1 += yi;
319 p += dpr;
324 *src = s;
326 #endif
328 static RImage *rotate_image_any(RImage *source, float angle)
330 (void) angle;
331 puts("NOT FULLY IMPLEMENTED");
332 return RCloneImage(source);
333 #if 0
334 RImage *img;
335 int nwidth, nheight;
336 int x1, y1;
337 int x2, y2;
338 int dx, dy;
339 int xi, yi;
340 int xx, yy;
341 unsigned char *src, *dst;
342 int dpr, dpru, p;
344 /* only 180o for now */
345 if (angle > 180.0F)
346 angle -= 180.0F;
348 angle = (angle * WM_PI) / 180.0;
350 nwidth = ceil(abs(cos(angle) * image->width))
351 + ceil(abs(cos(WM_PI / 2 - angle) * image->width));
353 nheight = ceil(abs(sin(angle) * image->height))
354 + ceil(abs(cos(WM_PI / 2 - angle) * image->height));
356 img = RCreateImage(nwidth, nheight, True);
357 if (!img)
358 return NULL;
360 src = image->data;
361 dst = img->data;
363 x1 = floor(abs(cos(WM_PI / 2 - angle) * image->width));
364 y1 = 0;
366 x2 = 0;
367 y2 = floor(abs(sin(WM_PI / 2 - angle) * image->width));
369 xx = floor(abs(cos(angle) * image->height)) - 1;
370 yy = nheight - 1;
372 printf("%ix%i, %i %i %i %i %i\n", nwidth, nheight, x1, y1, x2, y2, (int)((angle * 180.0) / WM_PI));
374 dx = abs(x2 - x1);
375 dy = abs(y2 - y1);
377 if (x1 > x2)
378 xi = -1;
379 else
380 xi = 1;
381 if (y1 > y2)
382 yi = -1;
383 else
384 yi = 1;
386 if (dx >= dy) {
387 dpr = dy << 1;
388 dpru = dpr - (dx << 1);
389 p = dpr - dx;
391 while (dx-- >= 0) {
393 copyLine(x1, y1, xx, yy, nwidth, image->format, dst, &src);
395 /* calc next step */
397 if (p > 0) {
398 x1 += xi;
399 y1 += yi;
400 xx += xi;
401 yy += yi;
402 p += dpru;
403 } else {
404 x1 += xi;
405 xx += xi;
406 p += dpr;
409 } else {
410 dpr = dx << 1;
411 dpru = dpr - (dy << 1);
412 p = dpr - dy;
414 while (dy-- >= 0) {
415 xx = abs(x1 * sin(angle * WM_PI / 180.0));
416 yy = abs(y1 * cos(angle * WM_PI / 180.0));
418 copyLine(x1, y1, xx, yy, nwidth, image->format, dst, &src);
420 /* calc next step */
421 if (p > 0) {
422 x1 += xi;
423 y1 += yi;
424 p += dpru;
425 } else {
426 y1 += yi;
427 p += dpr;
432 return img;
433 #endif