1 /* rotate.c - image rotation
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 2000-2003 Alfredo K. Kojima
6 *
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.
11 *
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.
16 *
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.
21 */
22
23 #include <config.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <X11/Xlib.h>
30
31 #include "wraster.h"
32 #include "rotate.h"
33
34 #include <math.h>
35
36
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);
40
41
RRotateImage(RImage * image,float angle)42 RImage *RRotateImage(RImage *image, float angle)
43 {
44 /*
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
50 */
51 static const float min_usable_angle = 0.00699F;
52
53 angle = fmod(angle, 360.0);
54 if (angle < 0.0F)
55 angle += 360.0F;
56
57 if (angle < min_usable_angle) {
58 /* Rotate by 0 degree */
59 return RCloneImage(image);
60
61 } else if ((angle > 90.0F - min_usable_angle) &&
62 (angle < 90.0F + min_usable_angle)) {
63 return rotate_image_90(image);
64
65 } else if ((angle > 180.0F - min_usable_angle) &&
66 (angle < 180.0F + min_usable_angle)) {
67 return wraster_rotate_image_180(image);
68
69 } else if ((angle > 270.0F - min_usable_angle) &&
70 (angle < 270.0F + min_usable_angle)) {
71 return rotate_image_270(image);
72
73 } else {
74 return rotate_image_any(image, angle);
75 }
76 }
77
rotate_image_90(RImage * source)78 static RImage *rotate_image_90(RImage *source)
79 {
80 RImage *target;
81 int nwidth, nheight;
82 int x, y;
83
84 nwidth = source->height;
85 nheight = source->width;
86
87 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
88 if (!target)
89 return NULL;
90
91 if (source->format == RRGBFormat) {
92 unsigned char *optr, *nptr;
93
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++;
101
102 nptr += 3 * nwidth;
103 }
104 }
105
106 } else {
107 unsigned char *optr, *nptr;
108
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++;
117
118 nptr += 4 * nwidth;
119 }
120 }
121 }
122
123 return target;
124 }
125
wraster_rotate_image_180(RImage * source)126 RImage *wraster_rotate_image_180(RImage *source)
127 {
128 RImage *target;
129 int nwidth, nheight;
130 int x, y;
131
132 nwidth = source->width;
133 nheight = source->height;
134
135 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
136 if (!target)
137 return NULL;
138
139 if (source->format == RRGBFormat) {
140 unsigned char *optr, *nptr;
141
142 optr = source->data;
143 nptr = target->data + nwidth * nheight * 3 - 3;
144
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];
150
151 optr += 3;
152 nptr -= 3;
153 }
154 }
155
156 } else {
157 unsigned char *optr, *nptr;
158
159 optr = source->data;
160 nptr = target->data + nwidth * nheight * 4 - 4;
161
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];
167
168 optr += 4;
169 nptr -= 4;
170 }
171 }
172
173 return target;
174 }
175
rotate_image_270(RImage * source)176 static RImage *rotate_image_270(RImage *source)
177 {
178 RImage *target;
179 int nwidth, nheight;
180 int x, y;
181
182 nwidth = source->height;
183 nheight = source->width;
184
185 target = RCreateImage(nwidth, nheight, (source->format != RRGBFormat));
186 if (!target)
187 return NULL;
188
189 if (source->format == RRGBFormat) {
190 unsigned char *optr, *nptr;
191
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++;
199
200 nptr -= 3 * nwidth;
201 }
202 }
203
204 } else {
205 unsigned char *optr, *nptr;
206
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++;
215
216 nptr -= 4 * nwidth;
217 }
218 }
219 }
220
221 return target;
222 }
223
224 /*
225 * Image rotation through Bresenham's line algorithm:
226 *
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-------
237 *
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
242 *
243 */
244
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)
248 {
249 unsigned char *s = *src;
250 int dx, dy;
251 int xi, yi;
252 int offset;
253 int dpr, dpru, p;
254
255 dx = abs(x2 - x1);
256 dy = abs(y2 - y1);
257
258 if (x1 > x2)
259 xi = -1;
260 else
261 xi = 1;
262 if (y1 > y2)
263 yi = -1;
264 else
265 yi = 1;
266
267 if (dx >= dy) {
268
269 dpr = dy << 1;
270 dpru = dpr - (dx << 1);
271 p = dpr - dx;
272
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;
283
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;
292 }
293 }
294 } else {
295
296 dpr = dx << 1;
297 dpru = dpr - (dy << 1);
298 p = dpr - dy;
299
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;
310
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;
319 }
320 }
321 }
322
323 *src = s;
324 }
325 #endif
326
rotate_image_any(RImage * source,float angle)327 static RImage *rotate_image_any(RImage *source, float angle)
328 {
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;
342
343 /* only 180o for now */
344 if (angle > 180.0F)
345 angle -= 180.0F;
346
347 angle = (angle * WM_PI) / 180.0;
348
349 nwidth = ceil(abs(cos(angle) * image->width))
350 + ceil(abs(cos(WM_PI / 2 - angle) * image->width));
351
352 nheight = ceil(abs(sin(angle) * image->height))
353 + ceil(abs(cos(WM_PI / 2 - angle) * image->height));
354
355 img = RCreateImage(nwidth, nheight, True);
356 if (!img)
357 return NULL;
358
359 src = image->data;
360 dst = img->data;
361
362 x1 = floor(abs(cos(WM_PI / 2 - angle) * image->width));
363 y1 = 0;
364
365 x2 = 0;
366 y2 = floor(abs(sin(WM_PI / 2 - angle) * image->width));
367
368 xx = floor(abs(cos(angle) * image->height)) - 1;
369 yy = nheight - 1;
370
371 printf("%ix%i, %i %i %i %i %i\n", nwidth, nheight, x1, y1, x2, y2, (int)((angle * 180.0) / WM_PI));
372
373 dx = abs(x2 - x1);
374 dy = abs(y2 - y1);
375
376 if (x1 > x2)
377 xi = -1;
378 else
379 xi = 1;
380 if (y1 > y2)
381 yi = -1;
382 else
383 yi = 1;
384
385 if (dx >= dy) {
386 dpr = dy << 1;
387 dpru = dpr - (dx << 1);
388 p = dpr - dx;
389
390 while (dx-- >= 0) {
391
392 copyLine(x1, y1, xx, yy, nwidth, image->format, dst, &src);
393
394 /* calc next step */
395
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;
406 }
407 }
408 } else {
409 dpr = dx << 1;
410 dpru = dpr - (dy << 1);
411 p = dpr - dy;
412
413 while (dy-- >= 0) {
414 xx = abs(x1 * sin(angle * WM_PI / 180.0));
415 yy = abs(y1 * cos(angle * WM_PI / 180.0));
416
417 copyLine(x1, y1, xx, yy, nwidth, image->format, dst, &src);
418
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;
427 }
428 }
429 }
430
431 return img;
432 #endif
433 }
434