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,
34 #define PI 3.14159265358979323846
37 static RImage
*rotateImage(RImage
* image
, float angle
);
39 RImage
*RRotateImage(RImage
* image
, float angle
)
44 int bpp
= image
->format
== RRGBAFormat
? 4 : 3;
47 * Angle steps below this value would represent a rotation
48 * of less than 1 pixel for a 4k wide image, so not worth
49 * bothering the difference. That makes it a perfect
50 * candidate for an Epsilon when trying to compare angle
53 static const float min_usable_angle
= 0.00699;
55 angle
= fmod(angle
, 360.0);
59 if (angle
< min_usable_angle
) {
60 /* Rotate by 0 degree */
61 return RCloneImage(image
);
63 } else if ((angle
> 90.0 - min_usable_angle
) &&
64 (angle
< 90.0 + min_usable_angle
)) {
65 nwidth
= image
->height
;
66 nheight
= image
->width
;
68 img
= RCreateImage(nwidth
, nheight
, True
);
74 unsigned char *optr
, *nptr
;
82 for (x
= 0; x
< nwidth
; x
++) {
83 nptr
= img
->data
+ x
* 4;
84 for (y
= nheight
; y
; y
--) {
94 unsigned *optr
, *nptr
;
97 optr
= (unsigned *)image
->data
;
98 p
= (unsigned *)img
->data
;
99 for (x
= 0; x
< nwidth
; x
++) {
101 for (y
= nheight
; y
; y
--) {
107 } else if ((angle
> 180.0 - min_usable_angle
) &&
108 (angle
< 180.0 + min_usable_angle
)) {
110 nwidth
= image
->width
;
111 nheight
= image
->height
;
112 img
= RCreateImage(nwidth
, nheight
, True
);
118 unsigned char *optr
, *nptr
;
121 nptr
= img
->data
+ nwidth
* nheight
* 4 - 4;
123 for (y
= 0; y
< nheight
; y
++) {
124 for (x
= 0; x
< nwidth
; x
++) {
135 unsigned *optr
, *nptr
;
137 optr
= (unsigned *)image
->data
;
138 nptr
= (unsigned *)img
->data
+ nwidth
* nheight
- 1;
140 for (y
= nheight
* nwidth
- 1; y
>= 0; y
--) {
146 } else if ((angle
> 270.0 - min_usable_angle
) &&
147 (angle
< 270.0 + min_usable_angle
)) {
148 nwidth
= image
->height
;
149 nheight
= image
->width
;
151 img
= RCreateImage(nwidth
, nheight
, True
);
157 unsigned char *optr
, *nptr
;
165 for (x
= 0; x
< nwidth
; x
++) {
166 nptr
= img
->data
+ x
* 4;
167 for (y
= nheight
; y
; y
--) {
177 unsigned *optr
, *nptr
;
180 optr
= (unsigned *)image
->data
;
181 p
= (unsigned *)img
->data
+ nwidth
* nheight
;
182 for (x
= 0; x
< nwidth
; x
++) {
184 for (y
= nheight
; y
; y
--) {
191 img
= rotateImage(image
, angle
);
198 * Image rotation through Bresenham's line algorithm:
200 * If a square must be rotate by angle a, like in:
206 * |A1 6 / | A_______B
207 * | \5 / a| <--- |1 2 3 4|
208 * |__C/_)_| |5 6 7 8|
211 * for each point P1 in the line from C to A
212 * for each point P2 in the perpendicular line starting at P1
213 * get pixel from the source and plot at P2
214 * increment pixel location from source
219 copyLine(int x1
, int y1
, int x2
, int y2
, int nwidth
, int format
, unsigned char *dst
, unsigned char **src
)
221 unsigned char *s
= *src
;
242 dpru
= dpr
- (dx
<< 1);
246 /* fetch and draw the pixel */
247 offset
= (x1
+ y1
* nwidth
) << 2;
248 dst
[offset
++] = *s
++;
249 dst
[offset
++] = *s
++;
250 dst
[offset
++] = *s
++;
251 if (format
== RRGBAFormat
)
252 dst
[offset
++] = *s
++;
269 dpru
= dpr
- (dy
<< 1);
273 /* fetch and draw the pixel */
274 offset
= (x1
+ y1
* nwidth
) << 2;
275 dst
[offset
++] = *s
++;
276 dst
[offset
++] = *s
++;
277 dst
[offset
++] = *s
++;
278 if (format
== RRGBAFormat
)
279 dst
[offset
++] = *s
++;
298 static RImage
*rotateImage(RImage
* image
, float angle
)
307 unsigned char *src
, *dst
;
310 /* only 180o for now */
314 angle
= (angle
* PI
) / 180.0;
316 nwidth
= ceil(abs(cos(angle
) * image
->width
))
317 + ceil(abs(cos(PI
/ 2 - angle
) * image
->width
));
319 nheight
= ceil(abs(sin(angle
) * image
->height
))
320 + ceil(abs(cos(PI
/ 2 - angle
) * image
->height
));
322 img
= RCreateImage(nwidth
, nheight
, True
);
329 x1
= floor(abs(cos(PI
/ 2 - angle
) * image
->width
));
333 y2
= floor(abs(sin(PI
/ 2 - angle
) * image
->width
));
335 xx
= floor(abs(cos(angle
) * image
->height
)) - 1;
338 printf("%ix%i, %i %i %i %i %i\n", nwidth
, nheight
, x1
, y1
, x2
, y2
, (int)((angle
* 180.0) / PI
));
354 dpru
= dpr
- (dx
<< 1);
359 copyLine(x1
, y1
, xx
, yy
, nwidth
, image
->format
, dst
, &src
);
376 puts("NOT IMPLEMTENED");
379 dpru
= dpr
- (dy
<< 1);
383 xx
= abs(x1
* sin(angle
* PI
/ 180.0));
384 yy
= abs(y1
* cos(angle
* PI
/ 180.0));
386 copyLine(x1
, y1
, xx
, yy
, nwidth
, image
->format
, dst
, &src
);