annotate window/SDL_rotozoom.cc @ 15:38226842bac8

Fixed new_pt in global calls
author thib
date Mon, 11 Aug 2008 15:52:41 +0000
parents 223b71206888
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
223b71206888 Initial import
thib
parents:
diff changeset
1 /*
223b71206888 Initial import
thib
parents:
diff changeset
2
223b71206888 Initial import
thib
parents:
diff changeset
3 SDL_rotozoom.c - rotozoomer for 32bit or 8bit surfaces
223b71206888 Initial import
thib
parents:
diff changeset
4
223b71206888 Initial import
thib
parents:
diff changeset
5 LGPL (c) A. Schiffler
223b71206888 Initial import
thib
parents:
diff changeset
6
223b71206888 Initial import
thib
parents:
diff changeset
7 */
223b71206888 Initial import
thib
parents:
diff changeset
8
223b71206888 Initial import
thib
parents:
diff changeset
9 #ifdef WIN32
223b71206888 Initial import
thib
parents:
diff changeset
10 #include <windows.h>
223b71206888 Initial import
thib
parents:
diff changeset
11 #endif
223b71206888 Initial import
thib
parents:
diff changeset
12
223b71206888 Initial import
thib
parents:
diff changeset
13 #include <stdlib.h>
223b71206888 Initial import
thib
parents:
diff changeset
14 #include <string.h>
223b71206888 Initial import
thib
parents:
diff changeset
15
223b71206888 Initial import
thib
parents:
diff changeset
16 #include "SDL_rotozoom.h"
223b71206888 Initial import
thib
parents:
diff changeset
17
223b71206888 Initial import
thib
parents:
diff changeset
18 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
223b71206888 Initial import
thib
parents:
diff changeset
19
223b71206888 Initial import
thib
parents:
diff changeset
20 /*
223b71206888 Initial import
thib
parents:
diff changeset
21
223b71206888 Initial import
thib
parents:
diff changeset
22 32bit Zoomer with optional anti-aliasing by bilinear interpolation.
223b71206888 Initial import
thib
parents:
diff changeset
23
223b71206888 Initial import
thib
parents:
diff changeset
24 Zoomes 32bit RGBA/ABGR 'src' surface to 'dst' surface.
223b71206888 Initial import
thib
parents:
diff changeset
25
223b71206888 Initial import
thib
parents:
diff changeset
26 */
223b71206888 Initial import
thib
parents:
diff changeset
27
223b71206888 Initial import
thib
parents:
diff changeset
28 int zoomSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int smooth)
223b71206888 Initial import
thib
parents:
diff changeset
29 {
223b71206888 Initial import
thib
parents:
diff changeset
30 int x, y, sx, sy, *sax, *say, *csax, *csay, csx, csy, ex, ey, t1, t2, sstep;
223b71206888 Initial import
thib
parents:
diff changeset
31 tColorRGBA *c00, *c01, *c10, *c11;
223b71206888 Initial import
thib
parents:
diff changeset
32 tColorRGBA *sp, *csp, *dp;
223b71206888 Initial import
thib
parents:
diff changeset
33 int sgap, dgap;
223b71206888 Initial import
thib
parents:
diff changeset
34
223b71206888 Initial import
thib
parents:
diff changeset
35 /*
223b71206888 Initial import
thib
parents:
diff changeset
36 * Variable setup
223b71206888 Initial import
thib
parents:
diff changeset
37 */
223b71206888 Initial import
thib
parents:
diff changeset
38 if (smooth) {
223b71206888 Initial import
thib
parents:
diff changeset
39 /*
223b71206888 Initial import
thib
parents:
diff changeset
40 * For interpolation: assume source dimension is one pixel
223b71206888 Initial import
thib
parents:
diff changeset
41 */
223b71206888 Initial import
thib
parents:
diff changeset
42 /*
223b71206888 Initial import
thib
parents:
diff changeset
43 * smaller to avoid overflow on right and bottom edge.
223b71206888 Initial import
thib
parents:
diff changeset
44 */
223b71206888 Initial import
thib
parents:
diff changeset
45 sx = (int) (65536.0 * (float) (src->w - 1) / (float) dst->w);
223b71206888 Initial import
thib
parents:
diff changeset
46 sy = (int) (65536.0 * (float) (src->h - 1) / (float) dst->h);
223b71206888 Initial import
thib
parents:
diff changeset
47 } else {
223b71206888 Initial import
thib
parents:
diff changeset
48 sx = (int) (65536.0 * (float) src->w / (float) dst->w);
223b71206888 Initial import
thib
parents:
diff changeset
49 sy = (int) (65536.0 * (float) src->h / (float) dst->h);
223b71206888 Initial import
thib
parents:
diff changeset
50 }
223b71206888 Initial import
thib
parents:
diff changeset
51
223b71206888 Initial import
thib
parents:
diff changeset
52 /*
223b71206888 Initial import
thib
parents:
diff changeset
53 * Allocate memory for row increments
223b71206888 Initial import
thib
parents:
diff changeset
54 */
223b71206888 Initial import
thib
parents:
diff changeset
55 if ((sax = (int *) malloc((dst->w + 1) * sizeof(Uint32))) == NULL) {
223b71206888 Initial import
thib
parents:
diff changeset
56 return (-1);
223b71206888 Initial import
thib
parents:
diff changeset
57 }
223b71206888 Initial import
thib
parents:
diff changeset
58 if ((say = (int *) malloc((dst->h + 1) * sizeof(Uint32))) == NULL) {
223b71206888 Initial import
thib
parents:
diff changeset
59 free(sax);
223b71206888 Initial import
thib
parents:
diff changeset
60 return (-1);
223b71206888 Initial import
thib
parents:
diff changeset
61 }
223b71206888 Initial import
thib
parents:
diff changeset
62
223b71206888 Initial import
thib
parents:
diff changeset
63 /*
223b71206888 Initial import
thib
parents:
diff changeset
64 * Precalculate row increments
223b71206888 Initial import
thib
parents:
diff changeset
65 */
223b71206888 Initial import
thib
parents:
diff changeset
66 csx = 0;
223b71206888 Initial import
thib
parents:
diff changeset
67 csax = sax;
223b71206888 Initial import
thib
parents:
diff changeset
68 for (x = 0; x <= dst->w; x++) {
223b71206888 Initial import
thib
parents:
diff changeset
69 *csax = csx;
223b71206888 Initial import
thib
parents:
diff changeset
70 csax++;
223b71206888 Initial import
thib
parents:
diff changeset
71 csx &= 0xffff;
223b71206888 Initial import
thib
parents:
diff changeset
72 csx += sx;
223b71206888 Initial import
thib
parents:
diff changeset
73 }
223b71206888 Initial import
thib
parents:
diff changeset
74 csy = 0;
223b71206888 Initial import
thib
parents:
diff changeset
75 csay = say;
223b71206888 Initial import
thib
parents:
diff changeset
76 for (y = 0; y <= dst->h; y++) {
223b71206888 Initial import
thib
parents:
diff changeset
77 *csay = csy;
223b71206888 Initial import
thib
parents:
diff changeset
78 csay++;
223b71206888 Initial import
thib
parents:
diff changeset
79 csy &= 0xffff;
223b71206888 Initial import
thib
parents:
diff changeset
80 csy += sy;
223b71206888 Initial import
thib
parents:
diff changeset
81 }
223b71206888 Initial import
thib
parents:
diff changeset
82
223b71206888 Initial import
thib
parents:
diff changeset
83 /*
223b71206888 Initial import
thib
parents:
diff changeset
84 * Pointer setup
223b71206888 Initial import
thib
parents:
diff changeset
85 */
223b71206888 Initial import
thib
parents:
diff changeset
86 sp = csp = (tColorRGBA *) src->pixels;
223b71206888 Initial import
thib
parents:
diff changeset
87 dp = (tColorRGBA *) dst->pixels;
223b71206888 Initial import
thib
parents:
diff changeset
88 sgap = src->pitch - src->w * 4;
223b71206888 Initial import
thib
parents:
diff changeset
89 dgap = dst->pitch - dst->w * 4;
223b71206888 Initial import
thib
parents:
diff changeset
90
223b71206888 Initial import
thib
parents:
diff changeset
91 /*
223b71206888 Initial import
thib
parents:
diff changeset
92 * Switch between interpolating and non-interpolating code
223b71206888 Initial import
thib
parents:
diff changeset
93 */
223b71206888 Initial import
thib
parents:
diff changeset
94 if (smooth) {
223b71206888 Initial import
thib
parents:
diff changeset
95
223b71206888 Initial import
thib
parents:
diff changeset
96 /*
223b71206888 Initial import
thib
parents:
diff changeset
97 * Interpolating Zoom
223b71206888 Initial import
thib
parents:
diff changeset
98 */
223b71206888 Initial import
thib
parents:
diff changeset
99
223b71206888 Initial import
thib
parents:
diff changeset
100 /*
223b71206888 Initial import
thib
parents:
diff changeset
101 * Scan destination
223b71206888 Initial import
thib
parents:
diff changeset
102 */
223b71206888 Initial import
thib
parents:
diff changeset
103 csay = say;
223b71206888 Initial import
thib
parents:
diff changeset
104 for (y = 0; y < dst->h; y++) {
223b71206888 Initial import
thib
parents:
diff changeset
105 /*
223b71206888 Initial import
thib
parents:
diff changeset
106 * Setup color source pointers
223b71206888 Initial import
thib
parents:
diff changeset
107 */
223b71206888 Initial import
thib
parents:
diff changeset
108 c00 = csp;
223b71206888 Initial import
thib
parents:
diff changeset
109 c01 = csp;
223b71206888 Initial import
thib
parents:
diff changeset
110 c01++;
223b71206888 Initial import
thib
parents:
diff changeset
111 c10 = (tColorRGBA *) ((Uint8 *) csp + src->pitch);
223b71206888 Initial import
thib
parents:
diff changeset
112 c11 = c10;
223b71206888 Initial import
thib
parents:
diff changeset
113 c11++;
223b71206888 Initial import
thib
parents:
diff changeset
114 csax = sax;
223b71206888 Initial import
thib
parents:
diff changeset
115 for (x = 0; x < dst->w; x++) {
223b71206888 Initial import
thib
parents:
diff changeset
116
223b71206888 Initial import
thib
parents:
diff changeset
117 /*
223b71206888 Initial import
thib
parents:
diff changeset
118 * Interpolate colors
223b71206888 Initial import
thib
parents:
diff changeset
119 */
223b71206888 Initial import
thib
parents:
diff changeset
120 ex = (*csax & 0xffff);
223b71206888 Initial import
thib
parents:
diff changeset
121 ey = (*csay & 0xffff);
223b71206888 Initial import
thib
parents:
diff changeset
122 t1 = ((((c01->r - c00->r) * ex) >> 16) + c00->r) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
123 t2 = ((((c11->r - c10->r) * ex) >> 16) + c10->r) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
124 dp->r = (((t2 - t1) * ey) >> 16) + t1;
223b71206888 Initial import
thib
parents:
diff changeset
125 t1 = ((((c01->g - c00->g) * ex) >> 16) + c00->g) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
126 t2 = ((((c11->g - c10->g) * ex) >> 16) + c10->g) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
127 dp->g = (((t2 - t1) * ey) >> 16) + t1;
223b71206888 Initial import
thib
parents:
diff changeset
128 t1 = ((((c01->b - c00->b) * ex) >> 16) + c00->b) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
129 t2 = ((((c11->b - c10->b) * ex) >> 16) + c10->b) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
130 dp->b = (((t2 - t1) * ey) >> 16) + t1;
223b71206888 Initial import
thib
parents:
diff changeset
131 t1 = ((((c01->a - c00->a) * ex) >> 16) + c00->a) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
132 t2 = ((((c11->a - c10->a) * ex) >> 16) + c10->a) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
133 dp->a = (((t2 - t1) * ey) >> 16) + t1;
223b71206888 Initial import
thib
parents:
diff changeset
134
223b71206888 Initial import
thib
parents:
diff changeset
135 /*
223b71206888 Initial import
thib
parents:
diff changeset
136 * Advance source pointers
223b71206888 Initial import
thib
parents:
diff changeset
137 */
223b71206888 Initial import
thib
parents:
diff changeset
138 csax++;
223b71206888 Initial import
thib
parents:
diff changeset
139 sstep = (*csax >> 16);
223b71206888 Initial import
thib
parents:
diff changeset
140 c00 += sstep;
223b71206888 Initial import
thib
parents:
diff changeset
141 c01 += sstep;
223b71206888 Initial import
thib
parents:
diff changeset
142 c10 += sstep;
223b71206888 Initial import
thib
parents:
diff changeset
143 c11 += sstep;
223b71206888 Initial import
thib
parents:
diff changeset
144 /*
223b71206888 Initial import
thib
parents:
diff changeset
145 * Advance destination pointer
223b71206888 Initial import
thib
parents:
diff changeset
146 */
223b71206888 Initial import
thib
parents:
diff changeset
147 dp++;
223b71206888 Initial import
thib
parents:
diff changeset
148 }
223b71206888 Initial import
thib
parents:
diff changeset
149 /*
223b71206888 Initial import
thib
parents:
diff changeset
150 * Advance source pointer
223b71206888 Initial import
thib
parents:
diff changeset
151 */
223b71206888 Initial import
thib
parents:
diff changeset
152 csay++;
223b71206888 Initial import
thib
parents:
diff changeset
153 csp = (tColorRGBA *) ((Uint8 *) csp + (*csay >> 16) * src->pitch);
223b71206888 Initial import
thib
parents:
diff changeset
154 /*
223b71206888 Initial import
thib
parents:
diff changeset
155 * Advance destination pointers
223b71206888 Initial import
thib
parents:
diff changeset
156 */
223b71206888 Initial import
thib
parents:
diff changeset
157 dp = (tColorRGBA *) ((Uint8 *) dp + dgap);
223b71206888 Initial import
thib
parents:
diff changeset
158 }
223b71206888 Initial import
thib
parents:
diff changeset
159
223b71206888 Initial import
thib
parents:
diff changeset
160 } else {
223b71206888 Initial import
thib
parents:
diff changeset
161
223b71206888 Initial import
thib
parents:
diff changeset
162 /*
223b71206888 Initial import
thib
parents:
diff changeset
163 * Non-Interpolating Zoom
223b71206888 Initial import
thib
parents:
diff changeset
164 */
223b71206888 Initial import
thib
parents:
diff changeset
165
223b71206888 Initial import
thib
parents:
diff changeset
166 csay = say;
223b71206888 Initial import
thib
parents:
diff changeset
167 for (y = 0; y < dst->h; y++) {
223b71206888 Initial import
thib
parents:
diff changeset
168 sp = csp;
223b71206888 Initial import
thib
parents:
diff changeset
169 csax = sax;
223b71206888 Initial import
thib
parents:
diff changeset
170 for (x = 0; x < dst->w; x++) {
223b71206888 Initial import
thib
parents:
diff changeset
171 /*
223b71206888 Initial import
thib
parents:
diff changeset
172 * Draw
223b71206888 Initial import
thib
parents:
diff changeset
173 */
223b71206888 Initial import
thib
parents:
diff changeset
174 *dp = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
175 /*
223b71206888 Initial import
thib
parents:
diff changeset
176 * Advance source pointers
223b71206888 Initial import
thib
parents:
diff changeset
177 */
223b71206888 Initial import
thib
parents:
diff changeset
178 csax++;
223b71206888 Initial import
thib
parents:
diff changeset
179 sp += (*csax >> 16);
223b71206888 Initial import
thib
parents:
diff changeset
180 /*
223b71206888 Initial import
thib
parents:
diff changeset
181 * Advance destination pointer
223b71206888 Initial import
thib
parents:
diff changeset
182 */
223b71206888 Initial import
thib
parents:
diff changeset
183 dp++;
223b71206888 Initial import
thib
parents:
diff changeset
184 }
223b71206888 Initial import
thib
parents:
diff changeset
185 /*
223b71206888 Initial import
thib
parents:
diff changeset
186 * Advance source pointer
223b71206888 Initial import
thib
parents:
diff changeset
187 */
223b71206888 Initial import
thib
parents:
diff changeset
188 csay++;
223b71206888 Initial import
thib
parents:
diff changeset
189 csp = (tColorRGBA *) ((Uint8 *) csp + (*csay >> 16) * src->pitch);
223b71206888 Initial import
thib
parents:
diff changeset
190 /*
223b71206888 Initial import
thib
parents:
diff changeset
191 * Advance destination pointers
223b71206888 Initial import
thib
parents:
diff changeset
192 */
223b71206888 Initial import
thib
parents:
diff changeset
193 dp = (tColorRGBA *) ((Uint8 *) dp + dgap);
223b71206888 Initial import
thib
parents:
diff changeset
194 }
223b71206888 Initial import
thib
parents:
diff changeset
195
223b71206888 Initial import
thib
parents:
diff changeset
196 }
223b71206888 Initial import
thib
parents:
diff changeset
197
223b71206888 Initial import
thib
parents:
diff changeset
198 /*
223b71206888 Initial import
thib
parents:
diff changeset
199 * Remove temp arrays
223b71206888 Initial import
thib
parents:
diff changeset
200 */
223b71206888 Initial import
thib
parents:
diff changeset
201 free(sax);
223b71206888 Initial import
thib
parents:
diff changeset
202 free(say);
223b71206888 Initial import
thib
parents:
diff changeset
203
223b71206888 Initial import
thib
parents:
diff changeset
204 return (0);
223b71206888 Initial import
thib
parents:
diff changeset
205 }
223b71206888 Initial import
thib
parents:
diff changeset
206
223b71206888 Initial import
thib
parents:
diff changeset
207 /*
223b71206888 Initial import
thib
parents:
diff changeset
208
223b71206888 Initial import
thib
parents:
diff changeset
209 8bit Zoomer without smoothing.
223b71206888 Initial import
thib
parents:
diff changeset
210
223b71206888 Initial import
thib
parents:
diff changeset
211 Zoomes 8bit palette/Y 'src' surface to 'dst' surface.
223b71206888 Initial import
thib
parents:
diff changeset
212
223b71206888 Initial import
thib
parents:
diff changeset
213 */
223b71206888 Initial import
thib
parents:
diff changeset
214
223b71206888 Initial import
thib
parents:
diff changeset
215 int zoomSurfaceY(SDL_Surface * src, SDL_Surface * dst)
223b71206888 Initial import
thib
parents:
diff changeset
216 {
223b71206888 Initial import
thib
parents:
diff changeset
217 Uint32 x, y, sx, sy, *sax, *say, *csax, *csay, csx, csy;
223b71206888 Initial import
thib
parents:
diff changeset
218 Uint8 *sp, *dp, *csp;
223b71206888 Initial import
thib
parents:
diff changeset
219 int dgap;
223b71206888 Initial import
thib
parents:
diff changeset
220
223b71206888 Initial import
thib
parents:
diff changeset
221 /*
223b71206888 Initial import
thib
parents:
diff changeset
222 * Variable setup
223b71206888 Initial import
thib
parents:
diff changeset
223 */
223b71206888 Initial import
thib
parents:
diff changeset
224 sx = (Uint32) (65536.0 * (float) src->w / (float) dst->w);
223b71206888 Initial import
thib
parents:
diff changeset
225 sy = (Uint32) (65536.0 * (float) src->h / (float) dst->h);
223b71206888 Initial import
thib
parents:
diff changeset
226
223b71206888 Initial import
thib
parents:
diff changeset
227 /*
223b71206888 Initial import
thib
parents:
diff changeset
228 * Allocate memory for row increments
223b71206888 Initial import
thib
parents:
diff changeset
229 */
223b71206888 Initial import
thib
parents:
diff changeset
230 if ((sax = (Uint32 *) malloc(dst->w * sizeof(Uint32))) == NULL) {
223b71206888 Initial import
thib
parents:
diff changeset
231 return (-1);
223b71206888 Initial import
thib
parents:
diff changeset
232 }
223b71206888 Initial import
thib
parents:
diff changeset
233 if ((say = (Uint32 *) malloc(dst->h * sizeof(Uint32))) == NULL) {
223b71206888 Initial import
thib
parents:
diff changeset
234 if (sax != NULL) {
223b71206888 Initial import
thib
parents:
diff changeset
235 free(sax);
223b71206888 Initial import
thib
parents:
diff changeset
236 }
223b71206888 Initial import
thib
parents:
diff changeset
237 return (-1);
223b71206888 Initial import
thib
parents:
diff changeset
238 }
223b71206888 Initial import
thib
parents:
diff changeset
239
223b71206888 Initial import
thib
parents:
diff changeset
240 /*
223b71206888 Initial import
thib
parents:
diff changeset
241 * Precalculate row increments
223b71206888 Initial import
thib
parents:
diff changeset
242 */
223b71206888 Initial import
thib
parents:
diff changeset
243 csx = 0;
223b71206888 Initial import
thib
parents:
diff changeset
244 csax = sax;
223b71206888 Initial import
thib
parents:
diff changeset
245 for (x = 0; x < dst->w; x++) {
223b71206888 Initial import
thib
parents:
diff changeset
246 csx += sx;
223b71206888 Initial import
thib
parents:
diff changeset
247 *csax = (csx >> 16);
223b71206888 Initial import
thib
parents:
diff changeset
248 csx &= 0xffff;
223b71206888 Initial import
thib
parents:
diff changeset
249 csax++;
223b71206888 Initial import
thib
parents:
diff changeset
250 }
223b71206888 Initial import
thib
parents:
diff changeset
251 csy = 0;
223b71206888 Initial import
thib
parents:
diff changeset
252 csay = say;
223b71206888 Initial import
thib
parents:
diff changeset
253 for (y = 0; y < dst->h; y++) {
223b71206888 Initial import
thib
parents:
diff changeset
254 csy += sy;
223b71206888 Initial import
thib
parents:
diff changeset
255 *csay = (csy >> 16);
223b71206888 Initial import
thib
parents:
diff changeset
256 csy &= 0xffff;
223b71206888 Initial import
thib
parents:
diff changeset
257 csay++;
223b71206888 Initial import
thib
parents:
diff changeset
258 }
223b71206888 Initial import
thib
parents:
diff changeset
259
223b71206888 Initial import
thib
parents:
diff changeset
260 csx = 0;
223b71206888 Initial import
thib
parents:
diff changeset
261 csax = sax;
223b71206888 Initial import
thib
parents:
diff changeset
262 for (x = 0; x < dst->w; x++) {
223b71206888 Initial import
thib
parents:
diff changeset
263 csx += (*csax);
223b71206888 Initial import
thib
parents:
diff changeset
264 csax++;
223b71206888 Initial import
thib
parents:
diff changeset
265 }
223b71206888 Initial import
thib
parents:
diff changeset
266 csy = 0;
223b71206888 Initial import
thib
parents:
diff changeset
267 csay = say;
223b71206888 Initial import
thib
parents:
diff changeset
268 for (y = 0; y < dst->h; y++) {
223b71206888 Initial import
thib
parents:
diff changeset
269 csy += (*csay);
223b71206888 Initial import
thib
parents:
diff changeset
270 csay++;
223b71206888 Initial import
thib
parents:
diff changeset
271 }
223b71206888 Initial import
thib
parents:
diff changeset
272
223b71206888 Initial import
thib
parents:
diff changeset
273 /*
223b71206888 Initial import
thib
parents:
diff changeset
274 * Pointer setup
223b71206888 Initial import
thib
parents:
diff changeset
275 */
223b71206888 Initial import
thib
parents:
diff changeset
276 sp = csp = (Uint8 *) src->pixels;
223b71206888 Initial import
thib
parents:
diff changeset
277 dp = (Uint8 *) dst->pixels;
223b71206888 Initial import
thib
parents:
diff changeset
278 dgap = dst->pitch - dst->w;
223b71206888 Initial import
thib
parents:
diff changeset
279
223b71206888 Initial import
thib
parents:
diff changeset
280 /*
223b71206888 Initial import
thib
parents:
diff changeset
281 * Draw
223b71206888 Initial import
thib
parents:
diff changeset
282 */
223b71206888 Initial import
thib
parents:
diff changeset
283 csay = say;
223b71206888 Initial import
thib
parents:
diff changeset
284 for (y = 0; y < dst->h; y++) {
223b71206888 Initial import
thib
parents:
diff changeset
285 csax = sax;
223b71206888 Initial import
thib
parents:
diff changeset
286 sp = csp;
223b71206888 Initial import
thib
parents:
diff changeset
287 for (x = 0; x < dst->w; x++) {
223b71206888 Initial import
thib
parents:
diff changeset
288 /*
223b71206888 Initial import
thib
parents:
diff changeset
289 * Draw
223b71206888 Initial import
thib
parents:
diff changeset
290 */
223b71206888 Initial import
thib
parents:
diff changeset
291 *dp = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
292 /*
223b71206888 Initial import
thib
parents:
diff changeset
293 * Advance source pointers
223b71206888 Initial import
thib
parents:
diff changeset
294 */
223b71206888 Initial import
thib
parents:
diff changeset
295 sp += (*csax);
223b71206888 Initial import
thib
parents:
diff changeset
296 csax++;
223b71206888 Initial import
thib
parents:
diff changeset
297 /*
223b71206888 Initial import
thib
parents:
diff changeset
298 * Advance destination pointer
223b71206888 Initial import
thib
parents:
diff changeset
299 */
223b71206888 Initial import
thib
parents:
diff changeset
300 dp++;
223b71206888 Initial import
thib
parents:
diff changeset
301 }
223b71206888 Initial import
thib
parents:
diff changeset
302 /*
223b71206888 Initial import
thib
parents:
diff changeset
303 * Advance source pointer (for row)
223b71206888 Initial import
thib
parents:
diff changeset
304 */
223b71206888 Initial import
thib
parents:
diff changeset
305 csp += ((*csay) * src->pitch);
223b71206888 Initial import
thib
parents:
diff changeset
306 csay++;
223b71206888 Initial import
thib
parents:
diff changeset
307 /*
223b71206888 Initial import
thib
parents:
diff changeset
308 * Advance destination pointers
223b71206888 Initial import
thib
parents:
diff changeset
309 */
223b71206888 Initial import
thib
parents:
diff changeset
310 dp += dgap;
223b71206888 Initial import
thib
parents:
diff changeset
311 }
223b71206888 Initial import
thib
parents:
diff changeset
312
223b71206888 Initial import
thib
parents:
diff changeset
313 /*
223b71206888 Initial import
thib
parents:
diff changeset
314 * Remove temp arrays
223b71206888 Initial import
thib
parents:
diff changeset
315 */
223b71206888 Initial import
thib
parents:
diff changeset
316 free(sax);
223b71206888 Initial import
thib
parents:
diff changeset
317 free(say);
223b71206888 Initial import
thib
parents:
diff changeset
318
223b71206888 Initial import
thib
parents:
diff changeset
319 return (0);
223b71206888 Initial import
thib
parents:
diff changeset
320 }
223b71206888 Initial import
thib
parents:
diff changeset
321
223b71206888 Initial import
thib
parents:
diff changeset
322 /*
223b71206888 Initial import
thib
parents:
diff changeset
323
223b71206888 Initial import
thib
parents:
diff changeset
324 32bit Rotozoomer with optional anti-aliasing by bilinear interpolation.
223b71206888 Initial import
thib
parents:
diff changeset
325
223b71206888 Initial import
thib
parents:
diff changeset
326 Rotates and zoomes 32bit RGBA/ABGR 'src' surface to 'dst' surface.
223b71206888 Initial import
thib
parents:
diff changeset
327
223b71206888 Initial import
thib
parents:
diff changeset
328 */
223b71206888 Initial import
thib
parents:
diff changeset
329
223b71206888 Initial import
thib
parents:
diff changeset
330 void transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int smooth)
223b71206888 Initial import
thib
parents:
diff changeset
331 {
223b71206888 Initial import
thib
parents:
diff changeset
332 int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh;
223b71206888 Initial import
thib
parents:
diff changeset
333 tColorRGBA c00, c01, c10, c11;
223b71206888 Initial import
thib
parents:
diff changeset
334 tColorRGBA *pc, *sp, *spb;
223b71206888 Initial import
thib
parents:
diff changeset
335 int gap;
223b71206888 Initial import
thib
parents:
diff changeset
336
223b71206888 Initial import
thib
parents:
diff changeset
337 /*
223b71206888 Initial import
thib
parents:
diff changeset
338 * Variable setup
223b71206888 Initial import
thib
parents:
diff changeset
339 */
223b71206888 Initial import
thib
parents:
diff changeset
340 xd = ((src->w - dst->w) << 15);
223b71206888 Initial import
thib
parents:
diff changeset
341 yd = ((src->h - dst->h) << 15);
223b71206888 Initial import
thib
parents:
diff changeset
342 ax = (cx << 16) - (icos * cx);
223b71206888 Initial import
thib
parents:
diff changeset
343 ay = (cy << 16) - (isin * cx);
223b71206888 Initial import
thib
parents:
diff changeset
344 sw = src->w - 1;
223b71206888 Initial import
thib
parents:
diff changeset
345 sh = src->h - 1;
223b71206888 Initial import
thib
parents:
diff changeset
346 pc = (tColorRGBA*)(dst->pixels);
223b71206888 Initial import
thib
parents:
diff changeset
347 gap = dst->pitch - dst->w * 4;
223b71206888 Initial import
thib
parents:
diff changeset
348
223b71206888 Initial import
thib
parents:
diff changeset
349 /*
223b71206888 Initial import
thib
parents:
diff changeset
350 * Switch between interpolating and non-interpolating code
223b71206888 Initial import
thib
parents:
diff changeset
351 */
223b71206888 Initial import
thib
parents:
diff changeset
352 if (smooth) {
223b71206888 Initial import
thib
parents:
diff changeset
353 for (y = 0; y < dst->h; y++) {
223b71206888 Initial import
thib
parents:
diff changeset
354 dy = cy - y;
223b71206888 Initial import
thib
parents:
diff changeset
355 sdx = (ax + (isin * dy)) + xd;
223b71206888 Initial import
thib
parents:
diff changeset
356 sdy = (ay - (icos * dy)) + yd;
223b71206888 Initial import
thib
parents:
diff changeset
357 for (x = 0; x < dst->w; x++) {
223b71206888 Initial import
thib
parents:
diff changeset
358 dx = (sdx >> 16);
223b71206888 Initial import
thib
parents:
diff changeset
359 dy = (sdy >> 16);
223b71206888 Initial import
thib
parents:
diff changeset
360 if ((dx >= -1) && (dy >= -1) && (dx < src->w) && (dy < src->h)) {
223b71206888 Initial import
thib
parents:
diff changeset
361 if ((dx >= 0) && (dy >= 0) && (dx < sw) && (dy < sh)) {
223b71206888 Initial import
thib
parents:
diff changeset
362 sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
223b71206888 Initial import
thib
parents:
diff changeset
363 sp += dx;
223b71206888 Initial import
thib
parents:
diff changeset
364 c00 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
365 sp += 1;
223b71206888 Initial import
thib
parents:
diff changeset
366 c01 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
367 sp = (tColorRGBA *) ((Uint8 *) sp + src->pitch);
223b71206888 Initial import
thib
parents:
diff changeset
368 sp -= 1;
223b71206888 Initial import
thib
parents:
diff changeset
369 c10 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
370 sp += 1;
223b71206888 Initial import
thib
parents:
diff changeset
371 c11 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
372 } else if ((dx == sw) && (dy == sh)) {
223b71206888 Initial import
thib
parents:
diff changeset
373 sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
223b71206888 Initial import
thib
parents:
diff changeset
374 sp += dx;
223b71206888 Initial import
thib
parents:
diff changeset
375 c00 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
376 c01 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
377 c10 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
378 c11 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
379 } else if ((dx == -1) && (dy == -1)) {
223b71206888 Initial import
thib
parents:
diff changeset
380 sp = (tColorRGBA *) (src->pixels);
223b71206888 Initial import
thib
parents:
diff changeset
381 c00 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
382 c01 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
383 c10 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
384 c11 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
385 } else if ((dx == -1) && (dy == sh)) {
223b71206888 Initial import
thib
parents:
diff changeset
386 sp = (tColorRGBA *) (src->pixels);
223b71206888 Initial import
thib
parents:
diff changeset
387 sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
223b71206888 Initial import
thib
parents:
diff changeset
388 c00 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
389 c01 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
390 c10 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
391 c11 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
392 } else if ((dx == sw) && (dy == -1)) {
223b71206888 Initial import
thib
parents:
diff changeset
393 sp = (tColorRGBA *) (src->pixels);
223b71206888 Initial import
thib
parents:
diff changeset
394 sp += dx;
223b71206888 Initial import
thib
parents:
diff changeset
395 c00 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
396 c01 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
397 c10 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
398 c11 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
399 } else if (dx == -1) {
223b71206888 Initial import
thib
parents:
diff changeset
400 sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
223b71206888 Initial import
thib
parents:
diff changeset
401 c00 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
402 c01 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
403 c10 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
404 sp = (tColorRGBA *) ((Uint8 *) sp + src->pitch);
223b71206888 Initial import
thib
parents:
diff changeset
405 c11 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
406 } else if (dy == -1) {
223b71206888 Initial import
thib
parents:
diff changeset
407 sp = (tColorRGBA *) (src->pixels);
223b71206888 Initial import
thib
parents:
diff changeset
408 sp += dx;
223b71206888 Initial import
thib
parents:
diff changeset
409 c00 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
410 c01 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
411 c10 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
412 sp += 1;
223b71206888 Initial import
thib
parents:
diff changeset
413 c11 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
414 } else if (dx == sw) {
223b71206888 Initial import
thib
parents:
diff changeset
415 sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
223b71206888 Initial import
thib
parents:
diff changeset
416 sp += dx;
223b71206888 Initial import
thib
parents:
diff changeset
417 c00 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
418 c01 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
419 sp = (tColorRGBA *) ((Uint8 *) sp + src->pitch);
223b71206888 Initial import
thib
parents:
diff changeset
420 c10 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
421 c11 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
422 } else if (dy == sh) {
223b71206888 Initial import
thib
parents:
diff changeset
423 sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
223b71206888 Initial import
thib
parents:
diff changeset
424 sp += dx;
223b71206888 Initial import
thib
parents:
diff changeset
425 c00 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
426 sp += 1;
223b71206888 Initial import
thib
parents:
diff changeset
427 c01 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
428 c10 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
429 c11 = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
430 }
223b71206888 Initial import
thib
parents:
diff changeset
431 /*
223b71206888 Initial import
thib
parents:
diff changeset
432 * Interpolate colors
223b71206888 Initial import
thib
parents:
diff changeset
433 */
223b71206888 Initial import
thib
parents:
diff changeset
434 ex = (sdx & 0xffff);
223b71206888 Initial import
thib
parents:
diff changeset
435 ey = (sdy & 0xffff);
223b71206888 Initial import
thib
parents:
diff changeset
436 t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
437 t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
438 pc->r = (((t2 - t1) * ey) >> 16) + t1;
223b71206888 Initial import
thib
parents:
diff changeset
439 t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
440 t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
441 pc->g = (((t2 - t1) * ey) >> 16) + t1;
223b71206888 Initial import
thib
parents:
diff changeset
442 t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
443 t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
444 pc->b = (((t2 - t1) * ey) >> 16) + t1;
223b71206888 Initial import
thib
parents:
diff changeset
445 t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
446 t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
447 pc->a = (((t2 - t1) * ey) >> 16) + t1;
223b71206888 Initial import
thib
parents:
diff changeset
448 }
223b71206888 Initial import
thib
parents:
diff changeset
449 sdx += icos;
223b71206888 Initial import
thib
parents:
diff changeset
450 sdy += isin;
223b71206888 Initial import
thib
parents:
diff changeset
451 pc++;
223b71206888 Initial import
thib
parents:
diff changeset
452 }
223b71206888 Initial import
thib
parents:
diff changeset
453 pc = (tColorRGBA *) ((Uint8 *) pc + gap);
223b71206888 Initial import
thib
parents:
diff changeset
454 }
223b71206888 Initial import
thib
parents:
diff changeset
455 } else {
223b71206888 Initial import
thib
parents:
diff changeset
456 for (y = 0; y < dst->h; y++) {
223b71206888 Initial import
thib
parents:
diff changeset
457 dy = cy - y;
223b71206888 Initial import
thib
parents:
diff changeset
458 sdx = (ax + (isin * dy)) + xd;
223b71206888 Initial import
thib
parents:
diff changeset
459 sdy = (ay - (icos * dy)) + yd;
223b71206888 Initial import
thib
parents:
diff changeset
460 for (x = 0; x < dst->w; x++) {
223b71206888 Initial import
thib
parents:
diff changeset
461 dx = (short) (sdx >> 16);
223b71206888 Initial import
thib
parents:
diff changeset
462 dy = (short) (sdy >> 16);
223b71206888 Initial import
thib
parents:
diff changeset
463 if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) {
223b71206888 Initial import
thib
parents:
diff changeset
464 sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
223b71206888 Initial import
thib
parents:
diff changeset
465 sp += dx;
223b71206888 Initial import
thib
parents:
diff changeset
466 *pc = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
467 }
223b71206888 Initial import
thib
parents:
diff changeset
468 sdx += icos;
223b71206888 Initial import
thib
parents:
diff changeset
469 sdy += isin;
223b71206888 Initial import
thib
parents:
diff changeset
470 pc++;
223b71206888 Initial import
thib
parents:
diff changeset
471 }
223b71206888 Initial import
thib
parents:
diff changeset
472 pc = (tColorRGBA *) ((Uint8 *) pc + gap);
223b71206888 Initial import
thib
parents:
diff changeset
473 }
223b71206888 Initial import
thib
parents:
diff changeset
474 }
223b71206888 Initial import
thib
parents:
diff changeset
475 }
223b71206888 Initial import
thib
parents:
diff changeset
476
223b71206888 Initial import
thib
parents:
diff changeset
477 /*
223b71206888 Initial import
thib
parents:
diff changeset
478
223b71206888 Initial import
thib
parents:
diff changeset
479 8bit Rotozoomer without smoothing
223b71206888 Initial import
thib
parents:
diff changeset
480
223b71206888 Initial import
thib
parents:
diff changeset
481 Rotates and zoomes 8bit palette/Y 'src' surface to 'dst' surface.
223b71206888 Initial import
thib
parents:
diff changeset
482
223b71206888 Initial import
thib
parents:
diff changeset
483 */
223b71206888 Initial import
thib
parents:
diff changeset
484
223b71206888 Initial import
thib
parents:
diff changeset
485 void transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos)
223b71206888 Initial import
thib
parents:
diff changeset
486 {
223b71206888 Initial import
thib
parents:
diff changeset
487 int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay, sw, sh;
223b71206888 Initial import
thib
parents:
diff changeset
488 tColorY *pc, *sp;
223b71206888 Initial import
thib
parents:
diff changeset
489 int gap;
223b71206888 Initial import
thib
parents:
diff changeset
490
223b71206888 Initial import
thib
parents:
diff changeset
491 /*
223b71206888 Initial import
thib
parents:
diff changeset
492 * Variable setup
223b71206888 Initial import
thib
parents:
diff changeset
493 */
223b71206888 Initial import
thib
parents:
diff changeset
494 xd = ((src->w - dst->w) << 15);
223b71206888 Initial import
thib
parents:
diff changeset
495 yd = ((src->h - dst->h) << 15);
223b71206888 Initial import
thib
parents:
diff changeset
496 ax = (cx << 16) - (icos * cx);
223b71206888 Initial import
thib
parents:
diff changeset
497 ay = (cy << 16) - (isin * cx);
223b71206888 Initial import
thib
parents:
diff changeset
498 sw = src->w - 1;
223b71206888 Initial import
thib
parents:
diff changeset
499 sh = src->h - 1;
223b71206888 Initial import
thib
parents:
diff changeset
500 pc = (tColorY*)(dst->pixels);
223b71206888 Initial import
thib
parents:
diff changeset
501 gap = dst->pitch - dst->w;
223b71206888 Initial import
thib
parents:
diff changeset
502 /*
223b71206888 Initial import
thib
parents:
diff changeset
503 * Clear surface to colorkey
223b71206888 Initial import
thib
parents:
diff changeset
504 */
223b71206888 Initial import
thib
parents:
diff changeset
505 memset(pc, (unsigned char) (src->format->colorkey & 0xff), dst->pitch * dst->h);
223b71206888 Initial import
thib
parents:
diff changeset
506 /*
223b71206888 Initial import
thib
parents:
diff changeset
507 * Iterate through destination surface
223b71206888 Initial import
thib
parents:
diff changeset
508 */
223b71206888 Initial import
thib
parents:
diff changeset
509 for (y = 0; y < dst->h; y++) {
223b71206888 Initial import
thib
parents:
diff changeset
510 dy = cy - y;
223b71206888 Initial import
thib
parents:
diff changeset
511 sdx = (ax + (isin * dy)) + xd;
223b71206888 Initial import
thib
parents:
diff changeset
512 sdy = (ay - (icos * dy)) + yd;
223b71206888 Initial import
thib
parents:
diff changeset
513 for (x = 0; x < dst->w; x++) {
223b71206888 Initial import
thib
parents:
diff changeset
514 dx = (short) (sdx >> 16);
223b71206888 Initial import
thib
parents:
diff changeset
515 dy = (short) (sdy >> 16);
223b71206888 Initial import
thib
parents:
diff changeset
516 if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) {
223b71206888 Initial import
thib
parents:
diff changeset
517 sp = (tColorY *) (src->pixels);
223b71206888 Initial import
thib
parents:
diff changeset
518 sp += (src->pitch * dy + dx);
223b71206888 Initial import
thib
parents:
diff changeset
519 *pc = *sp;
223b71206888 Initial import
thib
parents:
diff changeset
520 }
223b71206888 Initial import
thib
parents:
diff changeset
521 sdx += icos;
223b71206888 Initial import
thib
parents:
diff changeset
522 sdy += isin;
223b71206888 Initial import
thib
parents:
diff changeset
523 pc++;
223b71206888 Initial import
thib
parents:
diff changeset
524 }
223b71206888 Initial import
thib
parents:
diff changeset
525 pc += gap;
223b71206888 Initial import
thib
parents:
diff changeset
526 }
223b71206888 Initial import
thib
parents:
diff changeset
527 }
223b71206888 Initial import
thib
parents:
diff changeset
528
223b71206888 Initial import
thib
parents:
diff changeset
529 /*
223b71206888 Initial import
thib
parents:
diff changeset
530
223b71206888 Initial import
thib
parents:
diff changeset
531 rotozoomSurface()
223b71206888 Initial import
thib
parents:
diff changeset
532
223b71206888 Initial import
thib
parents:
diff changeset
533 Rotates and zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface.
223b71206888 Initial import
thib
parents:
diff changeset
534 'angle' is the rotation in degrees. 'zoom' a scaling factor. If 'smooth' is 1
223b71206888 Initial import
thib
parents:
diff changeset
535 then the destination 32bit surface is anti-aliased. If the surface is not 8bit
223b71206888 Initial import
thib
parents:
diff changeset
536 or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
223b71206888 Initial import
thib
parents:
diff changeset
537
223b71206888 Initial import
thib
parents:
diff changeset
538 */
223b71206888 Initial import
thib
parents:
diff changeset
539
223b71206888 Initial import
thib
parents:
diff changeset
540 #define VALUE_LIMIT 0.001
223b71206888 Initial import
thib
parents:
diff changeset
541
223b71206888 Initial import
thib
parents:
diff changeset
542
223b71206888 Initial import
thib
parents:
diff changeset
543 /* Local rotozoom-size function with trig result return */
223b71206888 Initial import
thib
parents:
diff changeset
544
223b71206888 Initial import
thib
parents:
diff changeset
545 void rotozoomSurfaceSizeTrig(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight,
223b71206888 Initial import
thib
parents:
diff changeset
546 double *canglezoom, double *sanglezoom)
223b71206888 Initial import
thib
parents:
diff changeset
547 {
223b71206888 Initial import
thib
parents:
diff changeset
548 double x, y, cx, cy, sx, sy;
223b71206888 Initial import
thib
parents:
diff changeset
549 double radangle;
223b71206888 Initial import
thib
parents:
diff changeset
550 int dstwidthhalf, dstheighthalf;
223b71206888 Initial import
thib
parents:
diff changeset
551
223b71206888 Initial import
thib
parents:
diff changeset
552 /*
223b71206888 Initial import
thib
parents:
diff changeset
553 * Determine destination width and height by rotating a centered source box
223b71206888 Initial import
thib
parents:
diff changeset
554 */
223b71206888 Initial import
thib
parents:
diff changeset
555 radangle = angle * (M_PI / 180.0);
223b71206888 Initial import
thib
parents:
diff changeset
556 *sanglezoom = sin(radangle);
223b71206888 Initial import
thib
parents:
diff changeset
557 *canglezoom = cos(radangle);
223b71206888 Initial import
thib
parents:
diff changeset
558 *sanglezoom *= zoom;
223b71206888 Initial import
thib
parents:
diff changeset
559 *canglezoom *= zoom;
223b71206888 Initial import
thib
parents:
diff changeset
560 x = width / 2;
223b71206888 Initial import
thib
parents:
diff changeset
561 y = height / 2;
223b71206888 Initial import
thib
parents:
diff changeset
562 cx = *canglezoom * x;
223b71206888 Initial import
thib
parents:
diff changeset
563 cy = *canglezoom * y;
223b71206888 Initial import
thib
parents:
diff changeset
564 sx = *sanglezoom * x;
223b71206888 Initial import
thib
parents:
diff changeset
565 sy = *sanglezoom * y;
223b71206888 Initial import
thib
parents:
diff changeset
566 dstwidthhalf = MAX((int)
223b71206888 Initial import
thib
parents:
diff changeset
567 ceil(MAX(MAX(MAX(fabs(cx + sy), fabs(cx - sy)), fabs(-cx + sy)), fabs(-cx - sy))), 1);
223b71206888 Initial import
thib
parents:
diff changeset
568 dstheighthalf = MAX((int)
223b71206888 Initial import
thib
parents:
diff changeset
569 ceil(MAX(MAX(MAX(fabs(sx + cy), fabs(sx - cy)), fabs(-sx + cy)), fabs(-sx - cy))), 1);
223b71206888 Initial import
thib
parents:
diff changeset
570 *dstwidth = 2 * dstwidthhalf;
223b71206888 Initial import
thib
parents:
diff changeset
571 *dstheight = 2 * dstheighthalf;
223b71206888 Initial import
thib
parents:
diff changeset
572 }
223b71206888 Initial import
thib
parents:
diff changeset
573
223b71206888 Initial import
thib
parents:
diff changeset
574
223b71206888 Initial import
thib
parents:
diff changeset
575 /* Publically available rotozoom-size function */
223b71206888 Initial import
thib
parents:
diff changeset
576
223b71206888 Initial import
thib
parents:
diff changeset
577 void rotozoomSurfaceSize(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight)
223b71206888 Initial import
thib
parents:
diff changeset
578 {
223b71206888 Initial import
thib
parents:
diff changeset
579 double dummy_sanglezoom, dummy_canglezoom;
223b71206888 Initial import
thib
parents:
diff changeset
580
223b71206888 Initial import
thib
parents:
diff changeset
581 rotozoomSurfaceSizeTrig(width, height, angle, zoom, dstwidth, dstheight, &dummy_sanglezoom, &dummy_canglezoom);
223b71206888 Initial import
thib
parents:
diff changeset
582 }
223b71206888 Initial import
thib
parents:
diff changeset
583
223b71206888 Initial import
thib
parents:
diff changeset
584
223b71206888 Initial import
thib
parents:
diff changeset
585 /* Publically available rotozoom function */
223b71206888 Initial import
thib
parents:
diff changeset
586
223b71206888 Initial import
thib
parents:
diff changeset
587 SDL_Surface *rotozoomSurface(SDL_Surface * src, double angle, double zoom, int smooth)
223b71206888 Initial import
thib
parents:
diff changeset
588 {
223b71206888 Initial import
thib
parents:
diff changeset
589 SDL_Surface *rz_src;
223b71206888 Initial import
thib
parents:
diff changeset
590 SDL_Surface *rz_dst;
223b71206888 Initial import
thib
parents:
diff changeset
591 double zoominv;
223b71206888 Initial import
thib
parents:
diff changeset
592 double sanglezoom, canglezoom, sanglezoominv, canglezoominv;
223b71206888 Initial import
thib
parents:
diff changeset
593 int dstwidthhalf, dstwidth, dstheighthalf, dstheight;
223b71206888 Initial import
thib
parents:
diff changeset
594 double x, y, cx, cy, sx, sy;
223b71206888 Initial import
thib
parents:
diff changeset
595 int is32bit;
223b71206888 Initial import
thib
parents:
diff changeset
596 int i, src_converted;
223b71206888 Initial import
thib
parents:
diff changeset
597
223b71206888 Initial import
thib
parents:
diff changeset
598 /*
223b71206888 Initial import
thib
parents:
diff changeset
599 * Sanity check
223b71206888 Initial import
thib
parents:
diff changeset
600 */
223b71206888 Initial import
thib
parents:
diff changeset
601 if (src == NULL)
223b71206888 Initial import
thib
parents:
diff changeset
602 return (NULL);
223b71206888 Initial import
thib
parents:
diff changeset
603
223b71206888 Initial import
thib
parents:
diff changeset
604 /*
223b71206888 Initial import
thib
parents:
diff changeset
605 * Determine if source surface is 32bit or 8bit
223b71206888 Initial import
thib
parents:
diff changeset
606 */
223b71206888 Initial import
thib
parents:
diff changeset
607 is32bit = (src->format->BitsPerPixel == 32);
223b71206888 Initial import
thib
parents:
diff changeset
608 if ((is32bit) || (src->format->BitsPerPixel == 8)) {
223b71206888 Initial import
thib
parents:
diff changeset
609 /*
223b71206888 Initial import
thib
parents:
diff changeset
610 * Use source surface 'as is'
223b71206888 Initial import
thib
parents:
diff changeset
611 */
223b71206888 Initial import
thib
parents:
diff changeset
612 rz_src = src;
223b71206888 Initial import
thib
parents:
diff changeset
613 src_converted = 0;
223b71206888 Initial import
thib
parents:
diff changeset
614 } else {
223b71206888 Initial import
thib
parents:
diff changeset
615 /*
223b71206888 Initial import
thib
parents:
diff changeset
616 * New source surface is 32bit with a defined RGBA ordering
223b71206888 Initial import
thib
parents:
diff changeset
617 */
223b71206888 Initial import
thib
parents:
diff changeset
618 rz_src =
223b71206888 Initial import
thib
parents:
diff changeset
619 SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
223b71206888 Initial import
thib
parents:
diff changeset
620 SDL_BlitSurface(src, NULL, rz_src, NULL);
223b71206888 Initial import
thib
parents:
diff changeset
621 src_converted = 1;
223b71206888 Initial import
thib
parents:
diff changeset
622 is32bit = 1;
223b71206888 Initial import
thib
parents:
diff changeset
623 }
223b71206888 Initial import
thib
parents:
diff changeset
624
223b71206888 Initial import
thib
parents:
diff changeset
625 /*
223b71206888 Initial import
thib
parents:
diff changeset
626 * Sanity check zoom factor
223b71206888 Initial import
thib
parents:
diff changeset
627 */
223b71206888 Initial import
thib
parents:
diff changeset
628 if (zoom < VALUE_LIMIT) {
223b71206888 Initial import
thib
parents:
diff changeset
629 zoom = VALUE_LIMIT;
223b71206888 Initial import
thib
parents:
diff changeset
630 }
223b71206888 Initial import
thib
parents:
diff changeset
631 zoominv = 65536.0 / (zoom * zoom);
223b71206888 Initial import
thib
parents:
diff changeset
632
223b71206888 Initial import
thib
parents:
diff changeset
633 /*
223b71206888 Initial import
thib
parents:
diff changeset
634 * Check if we have a rotozoom or just a zoom
223b71206888 Initial import
thib
parents:
diff changeset
635 */
223b71206888 Initial import
thib
parents:
diff changeset
636 if (fabs(angle) > VALUE_LIMIT) {
223b71206888 Initial import
thib
parents:
diff changeset
637
223b71206888 Initial import
thib
parents:
diff changeset
638 /*
223b71206888 Initial import
thib
parents:
diff changeset
639 * Angle!=0: full rotozoom
223b71206888 Initial import
thib
parents:
diff changeset
640 */
223b71206888 Initial import
thib
parents:
diff changeset
641 /*
223b71206888 Initial import
thib
parents:
diff changeset
642 * -----------------------
223b71206888 Initial import
thib
parents:
diff changeset
643 */
223b71206888 Initial import
thib
parents:
diff changeset
644
223b71206888 Initial import
thib
parents:
diff changeset
645 /* Determine target size */
223b71206888 Initial import
thib
parents:
diff changeset
646 rotozoomSurfaceSizeTrig(rz_src->w, rz_src->h, angle, zoom, &dstwidth, &dstheight, &canglezoom, &sanglezoom);
223b71206888 Initial import
thib
parents:
diff changeset
647
223b71206888 Initial import
thib
parents:
diff changeset
648 /*
223b71206888 Initial import
thib
parents:
diff changeset
649 * Calculate target factors from sin/cos and zoom
223b71206888 Initial import
thib
parents:
diff changeset
650 */
223b71206888 Initial import
thib
parents:
diff changeset
651 sanglezoominv = sanglezoom;
223b71206888 Initial import
thib
parents:
diff changeset
652 canglezoominv = canglezoom;
223b71206888 Initial import
thib
parents:
diff changeset
653 sanglezoominv *= zoominv;
223b71206888 Initial import
thib
parents:
diff changeset
654 canglezoominv *= zoominv;
223b71206888 Initial import
thib
parents:
diff changeset
655
223b71206888 Initial import
thib
parents:
diff changeset
656 /* Calculate half size */
223b71206888 Initial import
thib
parents:
diff changeset
657 dstwidthhalf = dstwidth / 2;
223b71206888 Initial import
thib
parents:
diff changeset
658 dstheighthalf = dstheight / 2;
223b71206888 Initial import
thib
parents:
diff changeset
659
223b71206888 Initial import
thib
parents:
diff changeset
660 /*
223b71206888 Initial import
thib
parents:
diff changeset
661 * Alloc space to completely contain the rotated surface
223b71206888 Initial import
thib
parents:
diff changeset
662 */
223b71206888 Initial import
thib
parents:
diff changeset
663 rz_dst = NULL;
223b71206888 Initial import
thib
parents:
diff changeset
664 if (is32bit) {
223b71206888 Initial import
thib
parents:
diff changeset
665 /*
223b71206888 Initial import
thib
parents:
diff changeset
666 * Target surface is 32bit with source RGBA/ABGR ordering
223b71206888 Initial import
thib
parents:
diff changeset
667 */
223b71206888 Initial import
thib
parents:
diff changeset
668 rz_dst =
223b71206888 Initial import
thib
parents:
diff changeset
669 SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 32,
223b71206888 Initial import
thib
parents:
diff changeset
670 rz_src->format->Rmask, rz_src->format->Gmask,
223b71206888 Initial import
thib
parents:
diff changeset
671 rz_src->format->Bmask, rz_src->format->Amask);
223b71206888 Initial import
thib
parents:
diff changeset
672 } else {
223b71206888 Initial import
thib
parents:
diff changeset
673 /*
223b71206888 Initial import
thib
parents:
diff changeset
674 * Target surface is 8bit
223b71206888 Initial import
thib
parents:
diff changeset
675 */
223b71206888 Initial import
thib
parents:
diff changeset
676 rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 8, 0, 0, 0, 0);
223b71206888 Initial import
thib
parents:
diff changeset
677 }
223b71206888 Initial import
thib
parents:
diff changeset
678
223b71206888 Initial import
thib
parents:
diff changeset
679 /*
223b71206888 Initial import
thib
parents:
diff changeset
680 * Lock source surface
223b71206888 Initial import
thib
parents:
diff changeset
681 */
223b71206888 Initial import
thib
parents:
diff changeset
682 SDL_LockSurface(rz_src);
223b71206888 Initial import
thib
parents:
diff changeset
683 /*
223b71206888 Initial import
thib
parents:
diff changeset
684 * Check which kind of surface we have
223b71206888 Initial import
thib
parents:
diff changeset
685 */
223b71206888 Initial import
thib
parents:
diff changeset
686 if (is32bit) {
223b71206888 Initial import
thib
parents:
diff changeset
687 /*
223b71206888 Initial import
thib
parents:
diff changeset
688 * Call the 32bit transformation routine to do the rotation (using alpha)
223b71206888 Initial import
thib
parents:
diff changeset
689 */
223b71206888 Initial import
thib
parents:
diff changeset
690 transformSurfaceRGBA(rz_src, rz_dst, dstwidthhalf, dstheighthalf,
223b71206888 Initial import
thib
parents:
diff changeset
691 (int) (sanglezoominv), (int) (canglezoominv), smooth);
223b71206888 Initial import
thib
parents:
diff changeset
692 /*
223b71206888 Initial import
thib
parents:
diff changeset
693 * Turn on source-alpha support
223b71206888 Initial import
thib
parents:
diff changeset
694 */
223b71206888 Initial import
thib
parents:
diff changeset
695 SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
223b71206888 Initial import
thib
parents:
diff changeset
696 } else {
223b71206888 Initial import
thib
parents:
diff changeset
697 /*
223b71206888 Initial import
thib
parents:
diff changeset
698 * Copy palette and colorkey info
223b71206888 Initial import
thib
parents:
diff changeset
699 */
223b71206888 Initial import
thib
parents:
diff changeset
700 for (i = 0; i < rz_src->format->palette->ncolors; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
701 rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
223b71206888 Initial import
thib
parents:
diff changeset
702 }
223b71206888 Initial import
thib
parents:
diff changeset
703 rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;
223b71206888 Initial import
thib
parents:
diff changeset
704 /*
223b71206888 Initial import
thib
parents:
diff changeset
705 * Call the 8bit transformation routine to do the rotation
223b71206888 Initial import
thib
parents:
diff changeset
706 */
223b71206888 Initial import
thib
parents:
diff changeset
707 transformSurfaceY(rz_src, rz_dst, dstwidthhalf, dstheighthalf,
223b71206888 Initial import
thib
parents:
diff changeset
708 (int) (sanglezoominv), (int) (canglezoominv));
223b71206888 Initial import
thib
parents:
diff changeset
709 SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey);
223b71206888 Initial import
thib
parents:
diff changeset
710 }
223b71206888 Initial import
thib
parents:
diff changeset
711 /*
223b71206888 Initial import
thib
parents:
diff changeset
712 * Unlock source surface
223b71206888 Initial import
thib
parents:
diff changeset
713 */
223b71206888 Initial import
thib
parents:
diff changeset
714 SDL_UnlockSurface(rz_src);
223b71206888 Initial import
thib
parents:
diff changeset
715
223b71206888 Initial import
thib
parents:
diff changeset
716 } else {
223b71206888 Initial import
thib
parents:
diff changeset
717
223b71206888 Initial import
thib
parents:
diff changeset
718 /*
223b71206888 Initial import
thib
parents:
diff changeset
719 * Angle=0: Just a zoom
223b71206888 Initial import
thib
parents:
diff changeset
720 */
223b71206888 Initial import
thib
parents:
diff changeset
721 /*
223b71206888 Initial import
thib
parents:
diff changeset
722 * --------------------
223b71206888 Initial import
thib
parents:
diff changeset
723 */
223b71206888 Initial import
thib
parents:
diff changeset
724
223b71206888 Initial import
thib
parents:
diff changeset
725 /*
223b71206888 Initial import
thib
parents:
diff changeset
726 * Calculate target size
223b71206888 Initial import
thib
parents:
diff changeset
727 */
223b71206888 Initial import
thib
parents:
diff changeset
728 zoomSurfaceSize(rz_src->w, rz_src->h, zoom, zoom, &dstwidth, &dstheight);
223b71206888 Initial import
thib
parents:
diff changeset
729
223b71206888 Initial import
thib
parents:
diff changeset
730 /*
223b71206888 Initial import
thib
parents:
diff changeset
731 * Alloc space to completely contain the zoomed surface
223b71206888 Initial import
thib
parents:
diff changeset
732 */
223b71206888 Initial import
thib
parents:
diff changeset
733 rz_dst = NULL;
223b71206888 Initial import
thib
parents:
diff changeset
734 if (is32bit) {
223b71206888 Initial import
thib
parents:
diff changeset
735 /*
223b71206888 Initial import
thib
parents:
diff changeset
736 * Target surface is 32bit with source RGBA/ABGR ordering
223b71206888 Initial import
thib
parents:
diff changeset
737 */
223b71206888 Initial import
thib
parents:
diff changeset
738 rz_dst =
223b71206888 Initial import
thib
parents:
diff changeset
739 SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 32,
223b71206888 Initial import
thib
parents:
diff changeset
740 rz_src->format->Rmask, rz_src->format->Gmask,
223b71206888 Initial import
thib
parents:
diff changeset
741 rz_src->format->Bmask, rz_src->format->Amask);
223b71206888 Initial import
thib
parents:
diff changeset
742 } else {
223b71206888 Initial import
thib
parents:
diff changeset
743 /*
223b71206888 Initial import
thib
parents:
diff changeset
744 * Target surface is 8bit
223b71206888 Initial import
thib
parents:
diff changeset
745 */
223b71206888 Initial import
thib
parents:
diff changeset
746 rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 8, 0, 0, 0, 0);
223b71206888 Initial import
thib
parents:
diff changeset
747 }
223b71206888 Initial import
thib
parents:
diff changeset
748
223b71206888 Initial import
thib
parents:
diff changeset
749 /*
223b71206888 Initial import
thib
parents:
diff changeset
750 * Lock source surface
223b71206888 Initial import
thib
parents:
diff changeset
751 */
223b71206888 Initial import
thib
parents:
diff changeset
752 SDL_LockSurface(rz_src);
223b71206888 Initial import
thib
parents:
diff changeset
753 /*
223b71206888 Initial import
thib
parents:
diff changeset
754 * Check which kind of surface we have
223b71206888 Initial import
thib
parents:
diff changeset
755 */
223b71206888 Initial import
thib
parents:
diff changeset
756 if (is32bit) {
223b71206888 Initial import
thib
parents:
diff changeset
757 /*
223b71206888 Initial import
thib
parents:
diff changeset
758 * Call the 32bit transformation routine to do the zooming (using alpha)
223b71206888 Initial import
thib
parents:
diff changeset
759 */
223b71206888 Initial import
thib
parents:
diff changeset
760 zoomSurfaceRGBA(rz_src, rz_dst, smooth);
223b71206888 Initial import
thib
parents:
diff changeset
761 /*
223b71206888 Initial import
thib
parents:
diff changeset
762 * Turn on source-alpha support
223b71206888 Initial import
thib
parents:
diff changeset
763 */
223b71206888 Initial import
thib
parents:
diff changeset
764 SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
223b71206888 Initial import
thib
parents:
diff changeset
765 } else {
223b71206888 Initial import
thib
parents:
diff changeset
766 /*
223b71206888 Initial import
thib
parents:
diff changeset
767 * Copy palette and colorkey info
223b71206888 Initial import
thib
parents:
diff changeset
768 */
223b71206888 Initial import
thib
parents:
diff changeset
769 for (i = 0; i < rz_src->format->palette->ncolors; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
770 rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
223b71206888 Initial import
thib
parents:
diff changeset
771 }
223b71206888 Initial import
thib
parents:
diff changeset
772 rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;
223b71206888 Initial import
thib
parents:
diff changeset
773 /*
223b71206888 Initial import
thib
parents:
diff changeset
774 * Call the 8bit transformation routine to do the zooming
223b71206888 Initial import
thib
parents:
diff changeset
775 */
223b71206888 Initial import
thib
parents:
diff changeset
776 zoomSurfaceY(rz_src, rz_dst);
223b71206888 Initial import
thib
parents:
diff changeset
777 SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey);
223b71206888 Initial import
thib
parents:
diff changeset
778 }
223b71206888 Initial import
thib
parents:
diff changeset
779 /*
223b71206888 Initial import
thib
parents:
diff changeset
780 * Unlock source surface
223b71206888 Initial import
thib
parents:
diff changeset
781 */
223b71206888 Initial import
thib
parents:
diff changeset
782 SDL_UnlockSurface(rz_src);
223b71206888 Initial import
thib
parents:
diff changeset
783 }
223b71206888 Initial import
thib
parents:
diff changeset
784
223b71206888 Initial import
thib
parents:
diff changeset
785 /*
223b71206888 Initial import
thib
parents:
diff changeset
786 * Cleanup temp surface
223b71206888 Initial import
thib
parents:
diff changeset
787 */
223b71206888 Initial import
thib
parents:
diff changeset
788 if (src_converted) {
223b71206888 Initial import
thib
parents:
diff changeset
789 SDL_FreeSurface(rz_src);
223b71206888 Initial import
thib
parents:
diff changeset
790 }
223b71206888 Initial import
thib
parents:
diff changeset
791
223b71206888 Initial import
thib
parents:
diff changeset
792 /*
223b71206888 Initial import
thib
parents:
diff changeset
793 * Return destination surface
223b71206888 Initial import
thib
parents:
diff changeset
794 */
223b71206888 Initial import
thib
parents:
diff changeset
795 return (rz_dst);
223b71206888 Initial import
thib
parents:
diff changeset
796 }
223b71206888 Initial import
thib
parents:
diff changeset
797
223b71206888 Initial import
thib
parents:
diff changeset
798 /*
223b71206888 Initial import
thib
parents:
diff changeset
799
223b71206888 Initial import
thib
parents:
diff changeset
800 zoomSurface()
223b71206888 Initial import
thib
parents:
diff changeset
801
223b71206888 Initial import
thib
parents:
diff changeset
802 Zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface.
223b71206888 Initial import
thib
parents:
diff changeset
803 'zoomx' and 'zoomy' are scaling factors for width and height. If 'smooth' is 1
223b71206888 Initial import
thib
parents:
diff changeset
804 then the destination 32bit surface is anti-aliased. If the surface is not 8bit
223b71206888 Initial import
thib
parents:
diff changeset
805 or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
223b71206888 Initial import
thib
parents:
diff changeset
806
223b71206888 Initial import
thib
parents:
diff changeset
807 */
223b71206888 Initial import
thib
parents:
diff changeset
808
223b71206888 Initial import
thib
parents:
diff changeset
809 #define VALUE_LIMIT 0.001
223b71206888 Initial import
thib
parents:
diff changeset
810
223b71206888 Initial import
thib
parents:
diff changeset
811 void zoomSurfaceSize(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight)
223b71206888 Initial import
thib
parents:
diff changeset
812 {
223b71206888 Initial import
thib
parents:
diff changeset
813 /*
223b71206888 Initial import
thib
parents:
diff changeset
814 * Sanity check zoom factors
223b71206888 Initial import
thib
parents:
diff changeset
815 */
223b71206888 Initial import
thib
parents:
diff changeset
816 if (zoomx < VALUE_LIMIT) {
223b71206888 Initial import
thib
parents:
diff changeset
817 zoomx = VALUE_LIMIT;
223b71206888 Initial import
thib
parents:
diff changeset
818 }
223b71206888 Initial import
thib
parents:
diff changeset
819 if (zoomy < VALUE_LIMIT) {
223b71206888 Initial import
thib
parents:
diff changeset
820 zoomy = VALUE_LIMIT;
223b71206888 Initial import
thib
parents:
diff changeset
821 }
223b71206888 Initial import
thib
parents:
diff changeset
822
223b71206888 Initial import
thib
parents:
diff changeset
823 /*
223b71206888 Initial import
thib
parents:
diff changeset
824 * Calculate target size
223b71206888 Initial import
thib
parents:
diff changeset
825 */
223b71206888 Initial import
thib
parents:
diff changeset
826 *dstwidth = (int) ((double) width * zoomx);
223b71206888 Initial import
thib
parents:
diff changeset
827 *dstheight = (int) ((double) height * zoomy);
223b71206888 Initial import
thib
parents:
diff changeset
828 if (*dstwidth < 1) {
223b71206888 Initial import
thib
parents:
diff changeset
829 *dstwidth = 1;
223b71206888 Initial import
thib
parents:
diff changeset
830 }
223b71206888 Initial import
thib
parents:
diff changeset
831 if (*dstheight < 1) {
223b71206888 Initial import
thib
parents:
diff changeset
832 *dstheight = 1;
223b71206888 Initial import
thib
parents:
diff changeset
833 }
223b71206888 Initial import
thib
parents:
diff changeset
834 }
223b71206888 Initial import
thib
parents:
diff changeset
835
223b71206888 Initial import
thib
parents:
diff changeset
836 SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smooth)
223b71206888 Initial import
thib
parents:
diff changeset
837 {
223b71206888 Initial import
thib
parents:
diff changeset
838 SDL_Surface *rz_src;
223b71206888 Initial import
thib
parents:
diff changeset
839 SDL_Surface *rz_dst;
223b71206888 Initial import
thib
parents:
diff changeset
840 int dstwidth, dstheight;
223b71206888 Initial import
thib
parents:
diff changeset
841 int is32bit;
223b71206888 Initial import
thib
parents:
diff changeset
842 int i, src_converted;
223b71206888 Initial import
thib
parents:
diff changeset
843
223b71206888 Initial import
thib
parents:
diff changeset
844 /*
223b71206888 Initial import
thib
parents:
diff changeset
845 * Sanity check
223b71206888 Initial import
thib
parents:
diff changeset
846 */
223b71206888 Initial import
thib
parents:
diff changeset
847 if (src == NULL)
223b71206888 Initial import
thib
parents:
diff changeset
848 return (NULL);
223b71206888 Initial import
thib
parents:
diff changeset
849
223b71206888 Initial import
thib
parents:
diff changeset
850 /*
223b71206888 Initial import
thib
parents:
diff changeset
851 * Determine if source surface is 32bit or 8bit
223b71206888 Initial import
thib
parents:
diff changeset
852 */
223b71206888 Initial import
thib
parents:
diff changeset
853 is32bit = (src->format->BitsPerPixel == 32);
223b71206888 Initial import
thib
parents:
diff changeset
854 if ((is32bit) || (src->format->BitsPerPixel == 8)) {
223b71206888 Initial import
thib
parents:
diff changeset
855 /*
223b71206888 Initial import
thib
parents:
diff changeset
856 * Use source surface 'as is'
223b71206888 Initial import
thib
parents:
diff changeset
857 */
223b71206888 Initial import
thib
parents:
diff changeset
858 rz_src = src;
223b71206888 Initial import
thib
parents:
diff changeset
859 src_converted = 0;
223b71206888 Initial import
thib
parents:
diff changeset
860 } else {
223b71206888 Initial import
thib
parents:
diff changeset
861 /*
223b71206888 Initial import
thib
parents:
diff changeset
862 * New source surface is 32bit with a defined RGBA ordering
223b71206888 Initial import
thib
parents:
diff changeset
863 */
223b71206888 Initial import
thib
parents:
diff changeset
864 rz_src =
223b71206888 Initial import
thib
parents:
diff changeset
865 SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
223b71206888 Initial import
thib
parents:
diff changeset
866 SDL_BlitSurface(src, NULL, rz_src, NULL);
223b71206888 Initial import
thib
parents:
diff changeset
867 src_converted = 1;
223b71206888 Initial import
thib
parents:
diff changeset
868 is32bit = 1;
223b71206888 Initial import
thib
parents:
diff changeset
869 }
223b71206888 Initial import
thib
parents:
diff changeset
870
223b71206888 Initial import
thib
parents:
diff changeset
871 /* Get size if target */
223b71206888 Initial import
thib
parents:
diff changeset
872 zoomSurfaceSize(rz_src->w, rz_src->h, zoomx, zoomy, &dstwidth, &dstheight);
223b71206888 Initial import
thib
parents:
diff changeset
873
223b71206888 Initial import
thib
parents:
diff changeset
874 /*
223b71206888 Initial import
thib
parents:
diff changeset
875 * Alloc space to completely contain the zoomed surface
223b71206888 Initial import
thib
parents:
diff changeset
876 */
223b71206888 Initial import
thib
parents:
diff changeset
877 rz_dst = NULL;
223b71206888 Initial import
thib
parents:
diff changeset
878 if (is32bit) {
223b71206888 Initial import
thib
parents:
diff changeset
879 /*
223b71206888 Initial import
thib
parents:
diff changeset
880 * Target surface is 32bit with source RGBA/ABGR ordering
223b71206888 Initial import
thib
parents:
diff changeset
881 */
223b71206888 Initial import
thib
parents:
diff changeset
882 rz_dst =
223b71206888 Initial import
thib
parents:
diff changeset
883 SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 32,
223b71206888 Initial import
thib
parents:
diff changeset
884 rz_src->format->Rmask, rz_src->format->Gmask,
223b71206888 Initial import
thib
parents:
diff changeset
885 rz_src->format->Bmask, rz_src->format->Amask);
223b71206888 Initial import
thib
parents:
diff changeset
886 } else {
223b71206888 Initial import
thib
parents:
diff changeset
887 /*
223b71206888 Initial import
thib
parents:
diff changeset
888 * Target surface is 8bit
223b71206888 Initial import
thib
parents:
diff changeset
889 */
223b71206888 Initial import
thib
parents:
diff changeset
890 rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 8, 0, 0, 0, 0);
223b71206888 Initial import
thib
parents:
diff changeset
891 }
223b71206888 Initial import
thib
parents:
diff changeset
892
223b71206888 Initial import
thib
parents:
diff changeset
893 /*
223b71206888 Initial import
thib
parents:
diff changeset
894 * Lock source surface
223b71206888 Initial import
thib
parents:
diff changeset
895 */
223b71206888 Initial import
thib
parents:
diff changeset
896 SDL_LockSurface(rz_src);
223b71206888 Initial import
thib
parents:
diff changeset
897 /*
223b71206888 Initial import
thib
parents:
diff changeset
898 * Check which kind of surface we have
223b71206888 Initial import
thib
parents:
diff changeset
899 */
223b71206888 Initial import
thib
parents:
diff changeset
900 if (is32bit) {
223b71206888 Initial import
thib
parents:
diff changeset
901 /*
223b71206888 Initial import
thib
parents:
diff changeset
902 * Call the 32bit transformation routine to do the zooming (using alpha)
223b71206888 Initial import
thib
parents:
diff changeset
903 */
223b71206888 Initial import
thib
parents:
diff changeset
904 zoomSurfaceRGBA(rz_src, rz_dst, smooth);
223b71206888 Initial import
thib
parents:
diff changeset
905 /*
223b71206888 Initial import
thib
parents:
diff changeset
906 * Turn on source-alpha support
223b71206888 Initial import
thib
parents:
diff changeset
907 */
223b71206888 Initial import
thib
parents:
diff changeset
908 SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
223b71206888 Initial import
thib
parents:
diff changeset
909 } else {
223b71206888 Initial import
thib
parents:
diff changeset
910 /*
223b71206888 Initial import
thib
parents:
diff changeset
911 * Copy palette and colorkey info
223b71206888 Initial import
thib
parents:
diff changeset
912 */
223b71206888 Initial import
thib
parents:
diff changeset
913 for (i = 0; i < rz_src->format->palette->ncolors; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
914 rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
223b71206888 Initial import
thib
parents:
diff changeset
915 }
223b71206888 Initial import
thib
parents:
diff changeset
916 rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;
223b71206888 Initial import
thib
parents:
diff changeset
917 /*
223b71206888 Initial import
thib
parents:
diff changeset
918 * Call the 8bit transformation routine to do the zooming
223b71206888 Initial import
thib
parents:
diff changeset
919 */
223b71206888 Initial import
thib
parents:
diff changeset
920 zoomSurfaceY(rz_src, rz_dst);
223b71206888 Initial import
thib
parents:
diff changeset
921 SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey);
223b71206888 Initial import
thib
parents:
diff changeset
922 }
223b71206888 Initial import
thib
parents:
diff changeset
923 /*
223b71206888 Initial import
thib
parents:
diff changeset
924 * Unlock source surface
223b71206888 Initial import
thib
parents:
diff changeset
925 */
223b71206888 Initial import
thib
parents:
diff changeset
926 SDL_UnlockSurface(rz_src);
223b71206888 Initial import
thib
parents:
diff changeset
927
223b71206888 Initial import
thib
parents:
diff changeset
928 /*
223b71206888 Initial import
thib
parents:
diff changeset
929 * Cleanup temp surface
223b71206888 Initial import
thib
parents:
diff changeset
930 */
223b71206888 Initial import
thib
parents:
diff changeset
931 if (src_converted) {
223b71206888 Initial import
thib
parents:
diff changeset
932 SDL_FreeSurface(rz_src);
223b71206888 Initial import
thib
parents:
diff changeset
933 }
223b71206888 Initial import
thib
parents:
diff changeset
934
223b71206888 Initial import
thib
parents:
diff changeset
935 /*
223b71206888 Initial import
thib
parents:
diff changeset
936 * Return destination surface
223b71206888 Initial import
thib
parents:
diff changeset
937 */
223b71206888 Initial import
thib
parents:
diff changeset
938 return (rz_dst);
223b71206888 Initial import
thib
parents:
diff changeset
939 }