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 #define PI 3.14159265358979323846
40 static RImage
*rotate_image_90(RImage
*source
);
41 static RImage
*rotate_image_270(RImage
*source
);
42 static RImage
*rotate_image_any(RImage
*source
, float angle
);
45 RImage
*RRotateImage(RImage
*image
, float angle
)
48 * Angle steps below this value would represent a rotation
49 * of less than 1 pixel for a 4k wide image, so not worth
50 * bothering the difference. That makes it a perfect
51 * candidate for an Epsilon when trying to compare angle
54 static const float min_usable_angle
= 0.00699;
56 angle
= fmod(angle
, 360.0);
60 if (angle
< min_usable_angle
) {
61 /* Rotate by 0 degree */
62 return RCloneImage(image
);
64 } else if ((angle
> 90.0 - min_usable_angle
) &&
65 (angle
< 90.0 + min_usable_angle
)) {
66 return rotate_image_90(image
);
68 } else if ((angle
> 180.0 - min_usable_angle
) &&
69 (angle
< 180.0 + min_usable_angle
)) {
70 return wraster_rotate_image_180(image
);
72 } else if ((angle
> 270.0 - min_usable_angle
) &&
73 (angle
< 270.0 + min_usable_angle
)) {
74 return rotate_image_270(image
);
77 return rotate_image_any(image
, angle
);
81 static RImage
*rotate_image_90(RImage
*source
)
87 nwidth
= source
->height
;
88 nheight
= source
->width
;
90 target
= RCreateImage(nwidth
, nheight
, (source
->format
!= RRGBFormat
));
94 if (source
->format
== RRGBFormat
) {
95 unsigned char *optr
, *nptr
;
98 for (x
= nwidth
; x
; x
--) {
99 nptr
= target
->data
+ 3 * (x
- 1);
100 for (y
= nheight
; y
; y
--) {
110 unsigned char *optr
, *nptr
;
113 for (x
= nwidth
; x
; x
--) {
114 nptr
= target
->data
+ 4 * (x
- 1);
115 for (y
= nheight
; y
; y
--) {
129 RImage
*wraster_rotate_image_180(RImage
*source
)
135 nwidth
= source
->width
;
136 nheight
= source
->height
;
138 target
= RCreateImage(nwidth
, nheight
, (source
->format
!= RRGBFormat
));
142 if (source
->format
== RRGBFormat
) {
143 unsigned char *optr
, *nptr
;
146 nptr
= target
->data
+ nwidth
* nheight
* 3 - 3;
148 for (y
= 0; y
< nheight
; y
++) {
149 for (x
= 0; x
< nwidth
; x
++) {
160 unsigned char *optr
, *nptr
;
163 nptr
= target
->data
+ nwidth
* nheight
* 4 - 4;
165 for (y
= nheight
* nwidth
- 1; y
>= 0; y
--) {
179 static RImage
*rotate_image_270(RImage
*source
)
185 nwidth
= source
->height
;
186 nheight
= source
->width
;
188 target
= RCreateImage(nwidth
, nheight
, (source
->format
!= RRGBFormat
));
192 if (source
->format
== RRGBFormat
) {
193 unsigned char *optr
, *nptr
;
196 for (x
= nwidth
; x
; x
--) {
197 nptr
= target
->data
+ 3 * nwidth
* nheight
- x
* 3;
198 for (y
= nheight
; y
; y
--) {
208 unsigned char *optr
, *nptr
;
211 for (x
= nwidth
; x
; x
--) {
212 nptr
= target
->data
+ 4 * nwidth
* nheight
- x
* 4;
213 for (y
= nheight
; y
; y
--) {
228 * Image rotation through Bresenham's line algorithm:
230 * If a square must be rotate by angle a, like in:
236 * |A1 6 / | A_______B
237 * | \5 / a| <--- |1 2 3 4|
238 * |__C/_)_| |5 6 7 8|
241 * for each point P1 in the line from C to A
242 * for each point P2 in the perpendicular line starting at P1
243 * get pixel from the source and plot at P2
244 * increment pixel location from source
250 copyLine(int x1
, int y1
, int x2
, int y2
, int nwidth
, int format
, unsigned char *dst
, unsigned char **src
)
252 unsigned char *s
= *src
;
273 dpru
= dpr
- (dx
<< 1);
277 /* fetch and draw the pixel */
278 offset
= (x1
+ y1
* nwidth
) << 2;
279 dst
[offset
++] = *s
++;
280 dst
[offset
++] = *s
++;
281 dst
[offset
++] = *s
++;
282 if (format
== RRGBAFormat
)
283 dst
[offset
++] = *s
++;
300 dpru
= dpr
- (dy
<< 1);
304 /* fetch and draw the pixel */
305 offset
= (x1
+ y1
* nwidth
) << 2;
306 dst
[offset
++] = *s
++;
307 dst
[offset
++] = *s
++;
308 dst
[offset
++] = *s
++;
309 if (format
== RRGBAFormat
)
310 dst
[offset
++] = *s
++;
330 static RImage
*rotate_image_any(RImage
*source
, float angle
)
333 puts("NOT FULLY IMPLEMENTED");
334 return RCloneImage(source
);
343 unsigned char *src
, *dst
;
346 /* only 180o for now */
350 angle
= (angle
* PI
) / 180.0;
352 nwidth
= ceil(abs(cos(angle
) * image
->width
))
353 + ceil(abs(cos(PI
/ 2 - angle
) * image
->width
));
355 nheight
= ceil(abs(sin(angle
) * image
->height
))
356 + ceil(abs(cos(PI
/ 2 - angle
) * image
->height
));
358 img
= RCreateImage(nwidth
, nheight
, True
);
365 x1
= floor(abs(cos(PI
/ 2 - angle
) * image
->width
));
369 y2
= floor(abs(sin(PI
/ 2 - angle
) * image
->width
));
371 xx
= floor(abs(cos(angle
) * image
->height
)) - 1;
374 printf("%ix%i, %i %i %i %i %i\n", nwidth
, nheight
, x1
, y1
, x2
, y2
, (int)((angle
* 180.0) / PI
));
390 dpru
= dpr
- (dx
<< 1);
395 copyLine(x1
, y1
, xx
, yy
, nwidth
, image
->format
, dst
, &src
);
413 dpru
= dpr
- (dy
<< 1);
417 xx
= abs(x1
* sin(angle
* PI
/ 180.0));
418 yy
= abs(y1
* cos(angle
* PI
/ 180.0));
420 copyLine(x1
, y1
, xx
, yy
, nwidth
, image
->format
, dst
, &src
);