1 | /* |
---|
2 | * libpipi Pathetic image processing interface library |
---|
3 | * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: rotate.c 3546 2009-07-08 23:38:39Z sam $ |
---|
7 | * |
---|
8 | * This library is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * wave.c: wave and other warping effects |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | |
---|
21 | #include <stdlib.h> |
---|
22 | #include <stdio.h> |
---|
23 | #include <string.h> |
---|
24 | #include <math.h> |
---|
25 | #ifndef M_PI |
---|
26 | # define M_PI 3.14159265358979323846 |
---|
27 | #endif |
---|
28 | |
---|
29 | #include "pipi.h" |
---|
30 | #include "pipi_internals.h" |
---|
31 | |
---|
32 | pipi_image_t *pipi_rotate(pipi_image_t *src, double a) |
---|
33 | { |
---|
34 | pipi_image_t *dst; |
---|
35 | pipi_pixels_t *srcp, *dstp; |
---|
36 | float *srcdata, *dstdata; |
---|
37 | double sina, cosa, cx, cy; |
---|
38 | int x, y, w, h, i, gray; |
---|
39 | |
---|
40 | w = src->w; |
---|
41 | h = src->h; |
---|
42 | |
---|
43 | gray = (src->last_modified == PIPI_PIXELS_Y_F32); |
---|
44 | |
---|
45 | srcp = gray ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32) |
---|
46 | : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32); |
---|
47 | srcdata = (float *)srcp->pixels; |
---|
48 | |
---|
49 | dst = pipi_new(w, h); |
---|
50 | dstp = gray ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32) |
---|
51 | : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32); |
---|
52 | dstdata = (float *)dstp->pixels; |
---|
53 | |
---|
54 | sina = sin(a * M_PI / 180.0); |
---|
55 | cosa = cos(a * M_PI / 180.0); |
---|
56 | |
---|
57 | cx = (double)w / 2.0; |
---|
58 | cy = (double)h / 2.0; |
---|
59 | |
---|
60 | for(y = 0; y < h; y++) |
---|
61 | { |
---|
62 | for(x = 0; x < w; x++) |
---|
63 | { |
---|
64 | double dx, dy; |
---|
65 | int x2, y2; |
---|
66 | |
---|
67 | dx = ((double)x - cx) * cosa - ((double)y - cy) * sina; |
---|
68 | dy = ((double)y - cy) * cosa + ((double)x - cx) * sina; |
---|
69 | |
---|
70 | x2 = (int)(cx + dx + 0.5); |
---|
71 | y2 = (int)(cy + dy + 0.5); |
---|
72 | |
---|
73 | if(gray) |
---|
74 | { |
---|
75 | if(x2 < 0 || y2 < 0 || x2 >= w || y2 >= h) |
---|
76 | ; |
---|
77 | else |
---|
78 | dstdata[y * w + x] = srcdata[y2 * w + x2]; |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | if(x2 < 0 || y2 < 0 || x2 >= w || y2 >= h) |
---|
83 | { |
---|
84 | dstdata[4 * (y * w + x) + 3] = 0.0f; |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | for(i = 0; i < 4; i++) |
---|
89 | { |
---|
90 | dstdata[4 * (y * w + x) + i] |
---|
91 | = srcdata[4 * (y2 * w + x2) + i]; |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | return dst; |
---|
99 | } |
---|
100 | |
---|