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$ |
---|
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 | #define BORDER 64 |
---|
33 | |
---|
34 | pipi_image_t *pipi_wave(pipi_image_t *src, double dw, double dh, |
---|
35 | double d, double a) |
---|
36 | { |
---|
37 | pipi_image_t *dst; |
---|
38 | pipi_pixels_t *srcp, *dstp; |
---|
39 | float *srcdata, *dstdata; |
---|
40 | double sina, cosa; |
---|
41 | int x, y, w, h, i, gray; |
---|
42 | |
---|
43 | w = src->w; |
---|
44 | h = src->h; |
---|
45 | |
---|
46 | gray = (src->last_modified == PIPI_PIXELS_Y_F32); |
---|
47 | |
---|
48 | srcp = gray ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32) |
---|
49 | : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32); |
---|
50 | srcdata = (float *)srcp->pixels; |
---|
51 | |
---|
52 | dst = pipi_new(w, h); |
---|
53 | dstp = gray ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32) |
---|
54 | : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32); |
---|
55 | dstdata = (float *)dstp->pixels; |
---|
56 | |
---|
57 | sina = sin(a); |
---|
58 | cosa = cos(a); |
---|
59 | |
---|
60 | for(y = 0; y < h; y++) |
---|
61 | { |
---|
62 | for(x = 0; x < w; x++) |
---|
63 | { |
---|
64 | double angle = 2 * M_PI / dw * ((x - w / 2) * cosa |
---|
65 | + (y - h / 2) * sina - d); |
---|
66 | double displacement = dh * sin(angle); |
---|
67 | double dx, dy; |
---|
68 | int x2, y2; |
---|
69 | |
---|
70 | dx = cosa * displacement; |
---|
71 | dy = sina * displacement; |
---|
72 | |
---|
73 | if(x < BORDER) dx = dx * x / BORDER; |
---|
74 | if(x > w - 1 - BORDER) dx = dx * (w - 1 - x) / BORDER; |
---|
75 | if(y < BORDER) dy = dy * y / BORDER; |
---|
76 | if(y > h - 1 - BORDER) dy = dy * (h - 1 - y) / BORDER; |
---|
77 | |
---|
78 | x2 = x + dx; |
---|
79 | y2 = y + dy; |
---|
80 | |
---|
81 | /* Just in case... */ |
---|
82 | if(x2 < 0) x2 = 0; |
---|
83 | else if(x2 >= w) x2 = w - 1; |
---|
84 | |
---|
85 | if(y2 < 0) y2 = 0; |
---|
86 | else if(y2 >= h) y2 = h - 1; |
---|
87 | |
---|
88 | if(gray) |
---|
89 | { |
---|
90 | dstdata[y * w + x] = srcdata[y2 * w + x2]; |
---|
91 | } |
---|
92 | else |
---|
93 | { |
---|
94 | for(i = 0; i < 4; i++) |
---|
95 | { |
---|
96 | dstdata[4 * (y * w + x) + i] |
---|
97 | = srcdata[4 * (y2 * w + x2) + i]; |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | return dst; |
---|
104 | } |
---|
105 | |
---|
106 | pipi_image_t *pipi_sine(pipi_image_t *src, double dw, double dh, |
---|
107 | double d, double a) |
---|
108 | { |
---|
109 | pipi_image_t *dst; |
---|
110 | pipi_pixels_t *srcp, *dstp; |
---|
111 | float *srcdata, *dstdata; |
---|
112 | double sina, cosa; |
---|
113 | int x, y, w, h, i, gray; |
---|
114 | |
---|
115 | w = src->w; |
---|
116 | h = src->h; |
---|
117 | |
---|
118 | gray = (src->last_modified == PIPI_PIXELS_Y_F32); |
---|
119 | |
---|
120 | srcp = gray ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32) |
---|
121 | : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32); |
---|
122 | srcdata = (float *)srcp->pixels; |
---|
123 | |
---|
124 | dst = pipi_new(w, h); |
---|
125 | dstp = gray ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32) |
---|
126 | : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32); |
---|
127 | dstdata = (float *)dstp->pixels; |
---|
128 | |
---|
129 | sina = sin(a); |
---|
130 | cosa = cos(a); |
---|
131 | |
---|
132 | for(y = 0; y < h; y++) |
---|
133 | { |
---|
134 | for(x = 0; x < w; x++) |
---|
135 | { |
---|
136 | double angle = 2 * M_PI / dw * ((x - w / 2) * cosa |
---|
137 | + (y - h / 2) * sina - d); |
---|
138 | double displacement = dh * sin(angle); |
---|
139 | double dx, dy; |
---|
140 | int x2, y2; |
---|
141 | |
---|
142 | dx = -sina * displacement; |
---|
143 | dy = cosa * displacement; |
---|
144 | |
---|
145 | if(x < BORDER) dx = dx * x / BORDER; |
---|
146 | if(x > w - 1 - BORDER) dx = dx * (w - 1 - x) / BORDER; |
---|
147 | if(y < BORDER) dy = dy * y / BORDER; |
---|
148 | if(y > h - 1 - BORDER) dy = dy * (h - 1 - y) / BORDER; |
---|
149 | |
---|
150 | x2 = x + dx; |
---|
151 | y2 = y + dy; |
---|
152 | |
---|
153 | /* Just in case... */ |
---|
154 | if(x2 < 0) x2 = 0; |
---|
155 | else if(x2 >= w) x2 = w - 1; |
---|
156 | |
---|
157 | if(y2 < 0) y2 = 0; |
---|
158 | else if(y2 >= h) y2 = h - 1; |
---|
159 | |
---|
160 | if(gray) |
---|
161 | { |
---|
162 | dstdata[y * w + x] = srcdata[y2 * w + x2]; |
---|
163 | } |
---|
164 | else |
---|
165 | { |
---|
166 | for(i = 0; i < 4; i++) |
---|
167 | { |
---|
168 | dstdata[4 * (y * w + x) + i] |
---|
169 | = srcdata[4 * (y2 * w + x2) + i]; |
---|
170 | } |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | return dst; |
---|
176 | } |
---|
177 | |
---|