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,
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
51 static const float min_usable_angle
= 0.00699;
53 angle
= fmod(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
);
74 return rotate_image_any(image
, angle
);
78 static RImage
*rotate_image_90(RImage
*source
)
84 nwidth
= source
->height
;
85 nheight
= source
->width
;
87 target
= RCreateImage(nwidth
, nheight
, (source
->format
!= RRGBFormat
));
91 if (source
->format
== RRGBFormat
) {
92 unsigned char *optr
, *nptr
;
95 for (x
= nwidth
; x
; x
--) {
96 nptr
= target
->data
+ 3 * (x
- 1);
97 for (y
= nheight
; y
; y
--) {
107 unsigned char *optr
, *nptr
;
110 for (x
= nwidth
; x
; x
--) {
111 nptr
= target
->data
+ 4 * (x
- 1);
112 for (y
= nheight
; y
; y
--) {
126 RImage
*wraster_rotate_image_180(RImage
*source
)
132 nwidth
= source
->width
;
133 nheight
= source
->height
;
135 target
= RCreateImage(nwidth
, nheight
, (source
->format
!= RRGBFormat
));
139 if (source
->format
== RRGBFormat
) {
140 unsigned char *optr
, *nptr
;
143 nptr
= target
->data
+ nwidth
* nheight
* 3 - 3;
145 for (y
= 0; y
< nheight
; y
++) {
146 for (x
= 0; x
< nwidth
; x
++) {
157 unsigned char *optr
, *nptr
;
160 nptr
= target
->data
+ nwidth
* nheight
* 4 - 4;
162 for (y
= nheight
* nwidth
- 1; y
>= 0; y
--) {
176 static RImage
*rotate_image_270(RImage
*source
)
182 nwidth
= source
->height
;
183 nheight
= source
->width
;
185 target
= RCreateImage(nwidth
, nheight
, (source
->format
!= RRGBFormat
));
189 if (source
->format
== RRGBFormat
) {
190 unsigned char *optr
, *nptr
;
193 for (x
= nwidth
; x
; x
--) {
194 nptr
= target
->data
+ 3 * nwidth
* nheight
- x
* 3;
195 for (y
= nheight
; y
; y
--) {
205 unsigned char *optr
, *nptr
;
208 for (x
= nwidth
; x
; x
--) {
209 nptr
= target
->data
+ 4 * nwidth
* nheight
- x
* 4;
210 for (y
= nheight
; y
; y
--) {
225 * Image rotation through Bresenham's line algorithm:
227 * If a square must be rotate by angle a, like in:
233 * |A1 6 / | A_______B
234 * | \5 / a| <--- |1 2 3 4|
235 * |__C/_)_| |5 6 7 8|
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
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
;
270 dpru
= dpr
- (dx
<< 1);
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
++;
297 dpru
= dpr
- (dy
<< 1);
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
++;
327 static RImage
*rotate_image_any(RImage
*source
, float angle
)
330 puts("NOT FULLY IMPLEMENTED");
331 return RCloneImage(source
);
340 unsigned char *src
, *dst
;
343 /* only 180o for now */
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
);
362 x1
= floor(abs(cos(WM_PI
/ 2 - angle
) * image
->width
));
366 y2
= floor(abs(sin(WM_PI
/ 2 - angle
) * image
->width
));
368 xx
= floor(abs(cos(angle
) * image
->height
)) - 1;
371 printf("%ix%i, %i %i %i %i %i\n", nwidth
, nheight
, x1
, y1
, x2
, y2
, (int)((angle
* 180.0) / WM_PI
));
387 dpru
= dpr
- (dx
<< 1);
392 copyLine(x1
, y1
, xx
, yy
, nwidth
, image
->format
, dst
, &src
);
410 dpru
= dpr
- (dy
<< 1);
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
);