|
190
|
1 /*
|
|
|
2 pygame - Python Game Library
|
|
|
3 Copyright (C) 2000-2001 Pete Shinners
|
|
|
4
|
|
|
5 This library is free software; you can redistribute it and/or
|
|
|
6 modify it under the terms of the GNU Library General Public
|
|
|
7 License as published by the Free Software Foundation; either
|
|
|
8 version 2 of the License, or (at your option) any later version.
|
|
|
9
|
|
|
10 This library is distributed in the hope that it will be useful,
|
|
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
13 Library General Public License for more details.
|
|
|
14
|
|
|
15 You should have received a copy of the GNU Library General Public
|
|
|
16 License along with this library; if not, write to the Free
|
|
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
18
|
|
|
19 Pete Shinners
|
|
|
20 pete@shinners.org
|
|
|
21 */
|
|
|
22
|
|
|
23 /*
|
|
|
24 * surface transformations for pygame
|
|
|
25 */
|
|
|
26 #include "pygame.h"
|
|
217
|
27 #include <math.h>
|
|
190
|
28
|
|
|
29
|
|
432
|
30 void scale2x(SDL_Surface *src, SDL_Surface *dst);
|
|
|
31
|
|
|
32
|
|
|
33
|
|
190
|
34 static SDL_Surface* newsurf_fromsurf(SDL_Surface* surf, int width, int height)
|
|
|
35 {
|
|
|
36 SDL_Surface* newsurf;
|
|
|
37
|
|
|
38 if(surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4)
|
|
|
39 return (SDL_Surface*)(RAISE(PyExc_ValueError, "unsupport Surface bit depth for transform"));
|
|
|
40
|
|
|
41 newsurf = SDL_CreateRGBSurface(surf->flags, width, height, surf->format->BitsPerPixel,
|
|
|
42 surf->format->Rmask, surf->format->Gmask, surf->format->Bmask, surf->format->Amask);
|
|
|
43 if(!newsurf)
|
|
|
44 return (SDL_Surface*)(RAISE(PyExc_SDLError, SDL_GetError()));
|
|
|
45
|
|
|
46 /* Copy palette, colorkey, etc info */
|
|
|
47 if(surf->format->BytesPerPixel==1 && surf->format->palette)
|
|
|
48 SDL_SetColors(newsurf, surf->format->palette->colors, 0, surf->format->palette->ncolors);
|
|
|
49 if(surf->flags & SDL_SRCCOLORKEY)
|
|
|
50 SDL_SetColorKey(newsurf, (surf->flags&SDL_RLEACCEL)|SDL_SRCCOLORKEY, surf->format->colorkey);
|
|
|
51
|
|
|
52 return newsurf;
|
|
|
53 }
|
|
|
54
|
|
|
55
|
|
321
|
56 static SDL_Surface* rotate90(SDL_Surface *src, int angle)
|
|
|
57 {
|
|
|
58 int numturns = (angle / 90) % 4;
|
|
|
59 int dstwidth, dstheight;
|
|
|
60 SDL_Surface* dst;
|
|
|
61 char *srcpix, *dstpix, *srcrow, *dstrow;
|
|
|
62 int srcstepx, srcstepy, dststepx, dststepy;
|
|
|
63 int loopx, loopy;
|
|
190
|
64
|
|
321
|
65 if(numturns<0)
|
|
|
66 numturns = 4+numturns;
|
|
|
67 if(!(numturns % 2))
|
|
|
68 {
|
|
|
69 dstwidth = src->w;
|
|
|
70 dstheight = src->h;
|
|
|
71 }
|
|
|
72 else
|
|
|
73 {
|
|
|
74 dstwidth = src->h;
|
|
|
75 dstheight = src->w;
|
|
|
76 }
|
|
|
77
|
|
|
78 dst = newsurf_fromsurf(src, dstwidth, dstheight);
|
|
|
79 if(!dst)
|
|
|
80 return NULL;
|
|
341
|
81 SDL_LockSurface(dst);
|
|
321
|
82 srcrow = (char*)src->pixels;
|
|
|
83 dstrow = (char*)dst->pixels;
|
|
|
84 srcstepx = dststepx = src->format->BytesPerPixel;
|
|
|
85 srcstepy = src->pitch;
|
|
|
86 dststepy = dst->pitch;
|
|
|
87
|
|
|
88 switch(numturns)
|
|
|
89 {
|
|
|
90 /*case 0: we don't need to change anything*/
|
|
|
91 case 1:
|
|
|
92 srcrow += ((src->w-1)*srcstepx);
|
|
|
93 srcstepy = -srcstepx;
|
|
|
94 srcstepx = src->pitch;
|
|
|
95 break;
|
|
|
96 case 2:
|
|
|
97 srcrow += ((src->h-1)*srcstepy) + ((src->w-1)*srcstepx);
|
|
|
98 srcstepx = -srcstepx;
|
|
|
99 srcstepy = -srcstepy;
|
|
|
100 break;
|
|
|
101 case 3:
|
|
|
102 srcrow += ((src->h-1)*srcstepy);
|
|
|
103 srcstepx = -srcstepy;
|
|
|
104 srcstepy = src->format->BytesPerPixel;
|
|
|
105 break;
|
|
|
106 }
|
|
|
107
|
|
|
108 switch(src->format->BytesPerPixel)
|
|
|
109 {
|
|
|
110 case 1:
|
|
|
111 for(loopy=0; loopy<dstheight; ++loopy)
|
|
|
112 {
|
|
|
113 dstpix = dstrow; srcpix = srcrow;
|
|
|
114 for(loopx=0; loopx<dstwidth; ++loopx)
|
|
|
115 {
|
|
|
116 *dstpix = *srcpix;
|
|
|
117 srcpix += srcstepx; dstpix += dststepx;
|
|
|
118 }
|
|
|
119 dstrow += dststepy; srcrow += srcstepy;
|
|
|
120 }break;
|
|
|
121 case 2:
|
|
|
122 for(loopy=0; loopy<dstheight; ++loopy)
|
|
|
123 {
|
|
|
124 dstpix = dstrow; srcpix = srcrow;
|
|
|
125 for(loopx=0; loopx<dstwidth; ++loopx)
|
|
|
126 {
|
|
|
127 *(Uint16*)dstpix = *(Uint16*)srcpix;
|
|
|
128 srcpix += srcstepx; dstpix += dststepx;
|
|
|
129 }
|
|
|
130 dstrow += dststepy; srcrow += srcstepy;
|
|
|
131 }break;
|
|
|
132 case 3:
|
|
|
133 for(loopy=0; loopy<dstheight; ++loopy)
|
|
|
134 {
|
|
|
135 dstpix = dstrow; srcpix = srcrow;
|
|
|
136 for(loopx=0; loopx<dstwidth; ++loopx)
|
|
|
137 {
|
|
|
138 dstpix[0] = srcpix[0]; dstpix[1] = srcpix[1]; dstpix[2] = srcpix[2];
|
|
|
139 srcpix += srcstepx; dstpix += dststepx;
|
|
|
140 }
|
|
|
141 dstrow += dststepy; srcrow += srcstepy;
|
|
|
142 }break;
|
|
|
143 case 4:
|
|
|
144 for(loopy=0; loopy<dstheight; ++loopy)
|
|
|
145 {
|
|
|
146 dstpix = dstrow; srcpix = srcrow;
|
|
|
147 for(loopx=0; loopx<dstwidth; ++loopx)
|
|
|
148 {
|
|
|
149 *(Uint32*)dstpix = *(Uint32*)srcpix;
|
|
|
150 srcpix += srcstepx; dstpix += dststepx;
|
|
|
151 }
|
|
|
152 dstrow += dststepy; srcrow += srcstepy;
|
|
341
|
153 }
|
|
321
|
154 }
|
|
341
|
155 SDL_UnlockSurface(dst);
|
|
321
|
156 return dst;
|
|
|
157 }
|
|
190
|
158
|
|
|
159
|
|
270
|
160 static void rotate(SDL_Surface *src, SDL_Surface *dst, Uint32 bgcolor, double sangle, double cangle)
|
|
190
|
161 {
|
|
191
|
162 int x, y, dx, dy;
|
|
270
|
163
|
|
190
|
164 Uint8 *srcpix = (Uint8*)src->pixels;
|
|
|
165 Uint8 *dstrow = (Uint8*)dst->pixels;
|
|
|
166 int srcpitch = src->pitch;
|
|
|
167 int dstpitch = dst->pitch;
|
|
|
168
|
|
318
|
169 int cy = dst->h / 2;
|
|
317
|
170 int xd = ((src->w - dst->w) << 15);
|
|
|
171 int yd = ((src->h - dst->h) << 15);
|
|
270
|
172
|
|
318
|
173 int isin = (int)(sangle*65536);
|
|
|
174 int icos = (int)(cangle*65536);
|
|
270
|
175
|
|
317
|
176 int ax = ((dst->w) << 15) - (int)(cangle * ((dst->w-1) << 15));
|
|
|
177 int ay = ((dst->h) << 15) - (int)(sangle * ((dst->w-1) << 15));
|
|
190
|
178
|
|
270
|
179 int xmaxval = ((src->w) << 16) - 1;
|
|
|
180 int ymaxval = ((src->h) << 16) - 1;
|
|
|
181
|
|
191
|
182 switch(src->format->BytesPerPixel)
|
|
190
|
183 {
|
|
191
|
184 case 1:
|
|
|
185 for(y = 0; y < dst->h; y++) {
|
|
|
186 Uint8 *dstpos = (Uint8*)dstrow;
|
|
270
|
187 dx = (ax + (isin * (cy - y))) + xd;
|
|
|
188 dy = (ay - (icos * (cy - y))) + yd;
|
|
191
|
189 for(x = 0; x < dst->w; x++) {
|
|
270
|
190 if(dx<0 || dy<0 || dx>xmaxval || dy>ymaxval) *dstpos++ = bgcolor;
|
|
191
|
191 else *dstpos++ = *(Uint8*)(srcpix + ((dy>>16) * srcpitch) + (dx>>16));
|
|
|
192 dx += icos; dy += isin;
|
|
|
193 }
|
|
|
194 dstrow += dstpitch;
|
|
|
195 }break;
|
|
270
|
196 case 2:
|
|
191
|
197 for(y = 0; y < dst->h; y++) {
|
|
|
198 Uint16 *dstpos = (Uint16*)dstrow;
|
|
270
|
199 dx = (ax + (isin * (cy - y))) + xd;
|
|
|
200 dy = (ay - (icos * (cy - y))) + yd;
|
|
191
|
201 for(x = 0; x < dst->w; x++) {
|
|
270
|
202 if(dx<0 || dy<0 || dx>xmaxval || dy>ymaxval) *dstpos++ = bgcolor;
|
|
191
|
203 else *dstpos++ = *(Uint16*)(srcpix + ((dy>>16) * srcpitch) + (dx>>16<<1));
|
|
|
204 dx += icos; dy += isin;
|
|
190
|
205 }
|
|
191
|
206 dstrow += dstpitch;
|
|
|
207 }break;
|
|
|
208 case 4:
|
|
|
209 for(y = 0; y < dst->h; y++) {
|
|
|
210 Uint32 *dstpos = (Uint32*)dstrow;
|
|
270
|
211 dx = (ax + (isin * (cy - y))) + xd;
|
|
|
212 dy = (ay - (icos * (cy - y))) + yd;
|
|
191
|
213 for(x = 0; x < dst->w; x++) {
|
|
270
|
214 if(dx<0 || dy<0 || dx>xmaxval || dy>ymaxval) *dstpos++ = bgcolor;
|
|
191
|
215 else *dstpos++ = *(Uint32*)(srcpix + ((dy>>16) * srcpitch) + (dx>>16<<2));
|
|
|
216 dx += icos; dy += isin;
|
|
|
217 }
|
|
|
218 dstrow += dstpitch;
|
|
|
219 }break;
|
|
|
220 default: /*case 3:*/
|
|
|
221 for(y = 0; y < dst->h; y++) {
|
|
|
222 Uint8 *dstpos = (Uint8*)dstrow;
|
|
270
|
223 dx = (ax + (isin * (cy - y))) + xd;
|
|
|
224 dy = (ay - (icos * (cy - y))) + yd;
|
|
191
|
225 for(x = 0; x < dst->w; x++) {
|
|
270
|
226 if(dx<0 || dy<0 || dx>xmaxval || dy>ymaxval)
|
|
191
|
227 {
|
|
|
228 dstpos[0] = ((Uint8*)&bgcolor)[0]; dstpos[1] = ((Uint8*)&bgcolor)[1]; dstpos[2] = ((Uint8*)&bgcolor)[2];
|
|
|
229 dstpos += 3;
|
|
|
230 }
|
|
|
231 else {
|
|
|
232 Uint8* srcpos = (Uint8*)(srcpix + ((dy>>16) * srcpitch) + ((dx>>16) * 3));
|
|
|
233 dstpos[0] = srcpos[0]; dstpos[1] = srcpos[1]; dstpos[2] = srcpos[2];
|
|
|
234 dstpos += 3;
|
|
|
235 }
|
|
|
236 dx += icos; dy += isin;
|
|
|
237 }
|
|
|
238 dstrow += dstpitch;
|
|
|
239 }break;
|
|
|
240 }
|
|
190
|
241 }
|
|
|
242
|
|
|
243
|
|
|
244
|
|
|
245 static void stretch(SDL_Surface *src, SDL_Surface *dst)
|
|
|
246 {
|
|
|
247 int looph, loopw;
|
|
|
248
|
|
|
249 Uint8* srcrow = (Uint8*)src->pixels;
|
|
|
250 Uint8* dstrow = (Uint8*)dst->pixels;
|
|
|
251
|
|
|
252 int srcpitch = src->pitch;
|
|
|
253 int dstpitch = dst->pitch;
|
|
|
254
|
|
|
255 int dstwidth = dst->w;
|
|
|
256 int dstheight = dst->h;
|
|
|
257 int dstwidth2 = dst->w << 1;
|
|
|
258 int dstheight2 = dst->h << 1;
|
|
|
259
|
|
|
260 int srcwidth2 = src->w << 1;
|
|
|
261 int srcheight2 = src->h << 1;
|
|
|
262
|
|
|
263 int w_err, h_err = srcheight2 - dstheight2;
|
|
|
264
|
|
|
265
|
|
|
266 switch(src->format->BytesPerPixel)
|
|
|
267 {
|
|
|
268 case 1:
|
|
|
269 for(looph = 0; looph < dstheight; ++looph)
|
|
|
270 {
|
|
|
271 Uint8 *srcpix = (Uint8*)srcrow, *dstpix = (Uint8*)dstrow;
|
|
|
272 w_err = srcwidth2 - dstwidth2;
|
|
|
273 for(loopw = 0; loopw < dstwidth; ++ loopw)
|
|
|
274 {
|
|
|
275 *dstpix++ = *srcpix;
|
|
|
276 while(w_err >= 0) {++srcpix; w_err -= dstwidth2;}
|
|
|
277 w_err += srcwidth2;
|
|
|
278 }
|
|
|
279 while(h_err >= 0) {srcrow += srcpitch; h_err -= dstheight2;}
|
|
|
280 dstrow += dstpitch;
|
|
|
281 h_err += srcheight2;
|
|
|
282 }break;
|
|
|
283 case 2:
|
|
|
284 for(looph = 0; looph < dstheight; ++looph)
|
|
|
285 {
|
|
|
286 Uint16 *srcpix = (Uint16*)srcrow, *dstpix = (Uint16*)dstrow;
|
|
|
287 w_err = srcwidth2 - dstwidth2;
|
|
|
288 for(loopw = 0; loopw < dstwidth; ++ loopw)
|
|
|
289 {
|
|
|
290 *dstpix++ = *srcpix;
|
|
|
291 while(w_err >= 0) {++srcpix; w_err -= dstwidth2;}
|
|
|
292 w_err += srcwidth2;
|
|
|
293 }
|
|
|
294 while(h_err >= 0) {srcrow += srcpitch; h_err -= dstheight2;}
|
|
|
295 dstrow += dstpitch;
|
|
|
296 h_err += srcheight2;
|
|
|
297 }break;
|
|
|
298 case 3:
|
|
|
299 for(looph = 0; looph < dstheight; ++looph)
|
|
|
300 {
|
|
|
301 Uint8 *srcpix = (Uint8*)srcrow, *dstpix = (Uint8*)dstrow;
|
|
|
302 w_err = srcwidth2 - dstwidth2;
|
|
|
303 for(loopw = 0; loopw < dstwidth; ++ loopw)
|
|
|
304 {
|
|
|
305 dstpix[0] = srcpix[0]; dstpix[1] = srcpix[1]; dstpix[2] = srcpix[2];
|
|
|
306 dstpix += 3;
|
|
|
307 while(w_err >= 0) {srcpix+=3; w_err -= dstwidth2;}
|
|
|
308 w_err += srcwidth2;
|
|
|
309 }
|
|
|
310 while(h_err >= 0) {srcrow += srcpitch; h_err -= dstheight2;}
|
|
|
311 dstrow += dstpitch;
|
|
|
312 h_err += srcheight2;
|
|
|
313 }break;
|
|
|
314 default: /*case 4:*/
|
|
|
315 for(looph = 0; looph < dstheight; ++looph)
|
|
|
316 {
|
|
|
317 Uint32 *srcpix = (Uint32*)srcrow, *dstpix = (Uint32*)dstrow;
|
|
|
318 w_err = srcwidth2 - dstwidth2;
|
|
|
319 for(loopw = 0; loopw < dstwidth; ++ loopw)
|
|
|
320 {
|
|
|
321 *dstpix++ = *srcpix;
|
|
|
322 while(w_err >= 0) {++srcpix; w_err -= dstwidth2;}
|
|
|
323 w_err += srcwidth2;
|
|
|
324 }
|
|
|
325 while(h_err >= 0) {srcrow += srcpitch; h_err -= dstheight2;}
|
|
|
326 dstrow += dstpitch;
|
|
|
327 h_err += srcheight2;
|
|
|
328 }break;
|
|
|
329 }
|
|
|
330 }
|
|
|
331
|
|
|
332
|
|
|
333
|
|
|
334
|
|
|
335
|
|
|
336 /*DOC*/ static char doc_scale[] =
|
|
|
337 /*DOC*/ "pygame.transform.scale(Surface, size) -> Surface\n"
|
|
|
338 /*DOC*/ "scale a Surface to an arbitrary size\n"
|
|
|
339 /*DOC*/ "\n"
|
|
192
|
340 /*DOC*/ "This will resize a surface to the given resolution.\n"
|
|
195
|
341 /*DOC*/ "The size is simply any 2 number sequence representing\n"
|
|
|
342 /*DOC*/ "the width and height.\n"
|
|
199
|
343 /*DOC*/ "\n"
|
|
|
344 /*DOC*/ "This transformation is not filtered.\n"
|
|
190
|
345 /*DOC*/ ;
|
|
|
346
|
|
|
347 static PyObject* surf_scale(PyObject* self, PyObject* arg)
|
|
|
348 {
|
|
|
349 PyObject *surfobj;
|
|
|
350 SDL_Surface* surf, *newsurf;
|
|
|
351 int width, height;
|
|
|
352
|
|
|
353 /*get all the arguments*/
|
|
|
354 if(!PyArg_ParseTuple(arg, "O!(ii)", &PySurface_Type, &surfobj, &width, &height))
|
|
|
355 return NULL;
|
|
|
356 surf = PySurface_AsSurface(surfobj);
|
|
|
357
|
|
|
358 newsurf = newsurf_fromsurf(surf, width, height);
|
|
|
359 if(!newsurf) return NULL;
|
|
|
360
|
|
|
361 SDL_LockSurface(newsurf);
|
|
|
362 PySurface_Lock(surfobj);
|
|
|
363
|
|
|
364 stretch(surf, newsurf);
|
|
|
365
|
|
|
366 PySurface_Unlock(surfobj);
|
|
|
367 SDL_UnlockSurface(newsurf);
|
|
|
368
|
|
|
369 return PySurface_New(newsurf);
|
|
|
370 }
|
|
|
371
|
|
|
372
|
|
|
373
|
|
|
374
|
|
432
|
375 /*DOC*/ static char doc_scale2x[] =
|
|
|
376 /*DOC*/ "pygame.transform.scale2x(Surface) -> Surface\n"
|
|
|
377 /*DOC*/ "doubles the size of the image with advanced scaling\n"
|
|
|
378 /*DOC*/ "\n"
|
|
|
379 /*DOC*/ "This will return a new image that is double the size of\n"
|
|
|
380 /*DOC*/ "the original. It uses the AdvanceMAME Scale2X algorithm\n"
|
|
|
381 /*DOC*/ "which does a 'jaggie-less' scale of bitmap graphics.\n"
|
|
|
382 /*DOC*/ "\n"
|
|
|
383 /*DOC*/ "This really only has an effect on simple images with solid\n"
|
|
|
384 /*DOC*/ "colors. On photographic and antialiased images it will look\n"
|
|
|
385 /*DOC*/ "like a regular unfiltered scale.\n"
|
|
|
386 /*DOC*/ ;
|
|
|
387
|
|
|
388 static PyObject* surf_scale2x(PyObject* self, PyObject* arg)
|
|
|
389 {
|
|
|
390 PyObject *surfobj;
|
|
|
391 SDL_Surface* surf, *newsurf;
|
|
|
392 int width, height;
|
|
|
393
|
|
|
394 /*get all the arguments*/
|
|
|
395 if(!PyArg_ParseTuple(arg, "O!", &PySurface_Type, &surfobj))
|
|
|
396 return NULL;
|
|
|
397 surf = PySurface_AsSurface(surfobj);
|
|
|
398
|
|
|
399 width = surf->w * 2;
|
|
|
400 height = surf->h * 2;
|
|
|
401
|
|
|
402 newsurf = newsurf_fromsurf(surf, width, height);
|
|
|
403 if(!newsurf) return NULL;
|
|
|
404
|
|
|
405 SDL_LockSurface(newsurf);
|
|
|
406 PySurface_Lock(surfobj);
|
|
|
407
|
|
|
408 scale2x(surf, newsurf);
|
|
|
409
|
|
|
410 PySurface_Unlock(surfobj);
|
|
|
411 SDL_UnlockSurface(newsurf);
|
|
|
412
|
|
|
413 return PySurface_New(newsurf);
|
|
|
414 }
|
|
|
415
|
|
|
416
|
|
|
417
|
|
|
418
|
|
190
|
419 /*DOC*/ static char doc_rotate[] =
|
|
|
420 /*DOC*/ "pygame.transform.rotate(Surface, angle) -> Surface\n"
|
|
|
421 /*DOC*/ "rotate a Surface\n"
|
|
|
422 /*DOC*/ "\n"
|
|
214
|
423 /*DOC*/ "Rotates the image counterclockwise with the given angle\n"
|
|
|
424 /*DOC*/ "(in degrees). The angle can be any floating point value\n"
|
|
|
425 /*DOC*/ "(negative rotation amounts will do clockwise rotations)\n"
|
|
192
|
426 /*DOC*/ "\n"
|
|
|
427 /*DOC*/ "Unless rotating by 90 degree increments, the resulting image\n"
|
|
|
428 /*DOC*/ "size will be larger than the original. There will be newly\n"
|
|
|
429 /*DOC*/ "uncovered areas in the image. These will filled with either\n"
|
|
|
430 /*DOC*/ "the current colorkey for the Surface, or the topleft pixel value.\n"
|
|
|
431 /*DOC*/ "(with the alpha channel zeroed out if available)\n"
|
|
199
|
432 /*DOC*/ "\n"
|
|
|
433 /*DOC*/ "This transformation is not filtered.\n"
|
|
190
|
434 /*DOC*/ ;
|
|
|
435
|
|
|
436 static PyObject* surf_rotate(PyObject* self, PyObject* arg)
|
|
|
437 {
|
|
|
438 PyObject *surfobj;
|
|
|
439 SDL_Surface* surf, *newsurf;
|
|
|
440 float angle;
|
|
|
441
|
|
|
442 double radangle, sangle, cangle;
|
|
|
443 double x, y, cx, cy, sx, sy;
|
|
|
444 int nxmax,nymax;
|
|
|
445 Uint32 bgcolor;
|
|
|
446
|
|
|
447 /*get all the arguments*/
|
|
|
448 if(!PyArg_ParseTuple(arg, "O!f", &PySurface_Type, &surfobj, &angle))
|
|
|
449 return NULL;
|
|
|
450 surf = PySurface_AsSurface(surfobj);
|
|
|
451
|
|
|
452
|
|
|
453 if(surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4)
|
|
|
454 return RAISE(PyExc_ValueError, "unsupport Surface bit depth for transform");
|
|
|
455
|
|
321
|
456 if(!(((int)angle)%90))
|
|
|
457 {
|
|
341
|
458 PySurface_Lock(surfobj);
|
|
321
|
459 newsurf = rotate90(surf, (int)angle);
|
|
341
|
460 PySurface_Unlock(surfobj);
|
|
321
|
461 if(!newsurf) return NULL;
|
|
|
462 return PySurface_New(newsurf);
|
|
|
463 }
|
|
|
464
|
|
|
465
|
|
190
|
466 radangle = angle*.01745329251994329;
|
|
|
467 sangle = sin(radangle);
|
|
|
468 cangle = cos(radangle);
|
|
270
|
469
|
|
|
470 x = surf->w;
|
|
|
471 y = surf->h;
|
|
190
|
472 cx = cangle*x;
|
|
|
473 cy = cangle*y;
|
|
|
474 sx = sangle*x;
|
|
|
475 sy = sangle*y;
|
|
270
|
476 nxmax = (int)(max(max(max(fabs(cx+sy), fabs(cx-sy)), fabs(-cx+sy)), fabs(-cx-sy)));
|
|
|
477 nymax = (int)(max(max(max(fabs(sx+cy), fabs(sx-cy)), fabs(-sx+cy)), fabs(-sx-cy)));
|
|
|
478
|
|
|
479 newsurf = newsurf_fromsurf(surf, nxmax, nymax);
|
|
190
|
480 if(!newsurf) return NULL;
|
|
|
481
|
|
|
482 /* get the background color */
|
|
|
483 if(surf->flags & SDL_SRCCOLORKEY)
|
|
192
|
484 {
|
|
190
|
485 bgcolor = surf->format->colorkey;
|
|
192
|
486 }
|
|
190
|
487 else
|
|
|
488 {
|
|
|
489 switch(surf->format->BytesPerPixel)
|
|
|
490 {
|
|
|
491 case 1: bgcolor = *(Uint8*)surf->pixels; break;
|
|
|
492 case 2: bgcolor = *(Uint16*)surf->pixels; break;
|
|
|
493 case 4: bgcolor = *(Uint32*)surf->pixels; break;
|
|
|
494 default: /*case 3:*/
|
|
|
495 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
|
|
|
496 bgcolor = (((Uint8*)surf->pixels)[0]) + (((Uint8*)surf->pixels)[1]<<8) + (((Uint8*)surf->pixels)[2]<<16);
|
|
|
497 #else
|
|
|
498 bgcolor = (((Uint8*)surf->pixels)[2]) + (((Uint8*)surf->pixels)[1]<<8) + (((Uint8*)surf->pixels)[0]<<16);
|
|
|
499 #endif
|
|
|
500 }
|
|
317
|
501 bgcolor &= ~surf->format->Amask;
|
|
190
|
502 }
|
|
|
503
|
|
|
504 SDL_LockSurface(newsurf);
|
|
|
505 PySurface_Lock(surfobj);
|
|
|
506
|
|
270
|
507 rotate(surf, newsurf, bgcolor, sangle, cangle);
|
|
190
|
508
|
|
|
509 PySurface_Unlock(surfobj);
|
|
|
510 SDL_UnlockSurface(newsurf);
|
|
|
511
|
|
|
512 return PySurface_New(newsurf);
|
|
|
513 }
|
|
|
514
|
|
|
515
|
|
|
516
|
|
|
517
|
|
191
|
518 /*DOC*/ static char doc_flip[] =
|
|
|
519 /*DOC*/ "pygame.transform.flip(Surface, xaxis, yaxis) -> Surface\n"
|
|
|
520 /*DOC*/ "flips a surface on either axis\n"
|
|
|
521 /*DOC*/ "\n"
|
|
|
522 /*DOC*/ "Flips the image on the x-axis or the y-axis if the argument\n"
|
|
|
523 /*DOC*/ "for that axis is true.\n"
|
|
194
|
524 /*DOC*/ "\n"
|
|
|
525 /*DOC*/ "The flip operation is nondestructive, you may flip the image\n"
|
|
|
526 /*DOC*/ "as many times as you like, and always be able to recreate the\n"
|
|
|
527 /*DOC*/ "exact original image.\n"
|
|
191
|
528 /*DOC*/ ;
|
|
|
529
|
|
|
530 static PyObject* surf_flip(PyObject* self, PyObject* arg)
|
|
|
531 {
|
|
|
532 PyObject *surfobj;
|
|
|
533 SDL_Surface* surf, *newsurf;
|
|
|
534 int xaxis, yaxis;
|
|
|
535 int loopx, loopy;
|
|
|
536 int pixsize, srcpitch, dstpitch;
|
|
|
537 Uint8 *srcpix, *dstpix;
|
|
|
538
|
|
|
539 /*get all the arguments*/
|
|
|
540 if(!PyArg_ParseTuple(arg, "O!ii", &PySurface_Type, &surfobj, &xaxis, &yaxis))
|
|
|
541 return NULL;
|
|
|
542 surf = PySurface_AsSurface(surfobj);
|
|
|
543
|
|
|
544 newsurf = newsurf_fromsurf(surf, surf->w, surf->h);
|
|
|
545 if(!newsurf) return NULL;
|
|
|
546
|
|
|
547 pixsize = surf->format->BytesPerPixel;
|
|
|
548 srcpitch = surf->pitch;
|
|
|
549 dstpitch = newsurf->pitch;
|
|
|
550
|
|
|
551 SDL_LockSurface(newsurf);
|
|
|
552 PySurface_Lock(surfobj);
|
|
|
553
|
|
|
554 srcpix = (Uint8*)surf->pixels;
|
|
|
555 dstpix = (Uint8*)newsurf->pixels;
|
|
|
556
|
|
|
557 if(!xaxis)
|
|
|
558 {
|
|
|
559 if(!yaxis)
|
|
|
560 {
|
|
|
561 for(loopy = 0; loopy < surf->h; ++loopy)
|
|
|
562 memcpy(dstpix+loopy*dstpitch, srcpix+loopy*srcpitch, surf->w*surf->format->BytesPerPixel);
|
|
|
563 }
|
|
|
564 else
|
|
|
565 {
|
|
|
566 for(loopy = 0; loopy < surf->h; ++loopy)
|
|
|
567 memcpy(dstpix+loopy*dstpitch, srcpix+(surf->h-1-loopy)*srcpitch, surf->w*surf->format->BytesPerPixel);
|
|
|
568 }
|
|
|
569 }
|
|
|
570 else /*if (xaxis)*/
|
|
|
571 {
|
|
|
572 if(yaxis)
|
|
|
573 {
|
|
|
574 switch(surf->format->BytesPerPixel)
|
|
|
575 {
|
|
|
576 case 1:
|
|
|
577 for(loopy = 0; loopy < surf->h; ++loopy) {
|
|
|
578 Uint8* dst = (Uint8*)(dstpix+loopy*dstpitch);
|
|
215
|
579 Uint8* src = ((Uint8*)(srcpix+(surf->h-1-loopy)*srcpitch)) + surf->w - 1;
|
|
191
|
580 for(loopx = 0; loopx < surf->w; ++loopx)
|
|
|
581 *dst++ = *src--;
|
|
|
582 }break;
|
|
|
583 case 2:
|
|
|
584 for(loopy = 0; loopy < surf->h; ++loopy) {
|
|
|
585 Uint16* dst = (Uint16*)(dstpix+loopy*dstpitch);
|
|
215
|
586 Uint16* src = ((Uint16*)(srcpix+(surf->h-1-loopy)*srcpitch)) + surf->w - 1;
|
|
191
|
587 for(loopx = 0; loopx < surf->w; ++loopx)
|
|
|
588 *dst++ = *src--;
|
|
|
589 }break;
|
|
|
590 case 4:
|
|
|
591 for(loopy = 0; loopy < surf->h; ++loopy) {
|
|
|
592 Uint32* dst = (Uint32*)(dstpix+loopy*dstpitch);
|
|
215
|
593 Uint32* src = ((Uint32*)(srcpix+(surf->h-1-loopy)*srcpitch)) + surf->w - 1;
|
|
191
|
594 for(loopx = 0; loopx < surf->w; ++loopx)
|
|
|
595 *dst++ = *src--;
|
|
|
596 }break;
|
|
|
597 case 3:
|
|
|
598 for(loopy = 0; loopy < surf->h; ++loopy) {
|
|
|
599 Uint8* dst = (Uint8*)(dstpix+loopy*dstpitch);
|
|
215
|
600 Uint8* src = ((Uint8*)(srcpix+(surf->h-1-loopy)*srcpitch)) + surf->w*3 - 3;
|
|
191
|
601 for(loopx = 0; loopx < surf->w; ++loopx)
|
|
|
602 {
|
|
|
603 dst[0] = src[0]; dst[1] = src[1]; dst[2] = src[2];
|
|
|
604 dst += 3;
|
|
|
605 src -= 3;
|
|
|
606 }
|
|
|
607 }break;
|
|
|
608 }
|
|
|
609 }
|
|
|
610 else
|
|
|
611 {
|
|
|
612 switch(surf->format->BytesPerPixel)
|
|
|
613 {
|
|
|
614 case 1:
|
|
|
615 for(loopy = 0; loopy < surf->h; ++loopy) {
|
|
|
616 Uint8* dst = (Uint8*)(dstpix+loopy*dstpitch);
|
|
215
|
617 Uint8* src = ((Uint8*)(srcpix+loopy*srcpitch)) + surf->w - 1;
|
|
191
|
618 for(loopx = 0; loopx < surf->w; ++loopx)
|
|
|
619 *dst++ = *src--;
|
|
|
620 }break;
|
|
|
621 case 2:
|
|
|
622 for(loopy = 0; loopy < surf->h; ++loopy) {
|
|
|
623 Uint16* dst = (Uint16*)(dstpix+loopy*dstpitch);
|
|
215
|
624 Uint16* src = ((Uint16*)(srcpix+loopy*srcpitch)) + surf->w - 1;
|
|
191
|
625 for(loopx = 0; loopx < surf->w; ++loopx)
|
|
|
626 *dst++ = *src--;
|
|
|
627 }break;
|
|
|
628 case 4:
|
|
|
629 for(loopy = 0; loopy < surf->h; ++loopy) {
|
|
|
630 Uint32* dst = (Uint32*)(dstpix+loopy*dstpitch);
|
|
215
|
631 Uint32* src = ((Uint32*)(srcpix+loopy*srcpitch)) + surf->w - 1;
|
|
191
|
632 for(loopx = 0; loopx < surf->w; ++loopx)
|
|
|
633 *dst++ = *src--;
|
|
|
634 }break;
|
|
|
635 case 3:
|
|
|
636 for(loopy = 0; loopy < surf->h; ++loopy) {
|
|
|
637 Uint8* dst = (Uint8*)(dstpix+loopy*dstpitch);
|
|
215
|
638 Uint8* src = ((Uint8*)(srcpix+loopy*srcpitch)) + surf->w*3 - 3;
|
|
191
|
639 for(loopx = 0; loopx < surf->w; ++loopx)
|
|
|
640 {
|
|
|
641 dst[0] = src[0]; dst[1] = src[1]; dst[2] = src[2];
|
|
|
642 dst += 3;
|
|
|
643 src -= 3;
|
|
|
644 }
|
|
|
645 }break;
|
|
|
646 }
|
|
|
647 }
|
|
|
648 }
|
|
|
649
|
|
|
650 PySurface_Unlock(surfobj);
|
|
|
651 SDL_UnlockSurface(newsurf);
|
|
|
652
|
|
|
653 return PySurface_New(newsurf);
|
|
|
654 }
|
|
|
655
|
|
|
656
|
|
|
657
|
|
|
658
|
|
199
|
659 extern SDL_Surface *rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth);
|
|
190
|
660
|
|
199
|
661 /*DOC*/ static char doc_rotozoom[] =
|
|
|
662 /*DOC*/ "pygame.transform.rotozoom(Surface, angle, zoom) -> Surface\n"
|
|
|
663 /*DOC*/ "smoothly scale and/or rotate an image\n"
|
|
|
664 /*DOC*/ "\n"
|
|
316
|
665 /*DOC*/ "The angle argument is the number of degrees to rotate\n"
|
|
|
666 /*DOC*/ "counter-clockwise. The angle can be any floating point value.\n"
|
|
|
667 /*DOC*/ "(negative rotation amounts will do clockwise rotations)\n"
|
|
|
668 /*DOC*/ "\n"
|
|
199
|
669 /*DOC*/ "This will smoothly rotate and scale an image in one pass.\n"
|
|
|
670 /*DOC*/ "The resulting image will always be a 32bit version of the\n"
|
|
|
671 /*DOC*/ "original surface. The scale is a multiplier for the image\n"
|
|
214
|
672 /*DOC*/ "size, and angle is the degrees to rotate counter clockwise.\n"
|
|
199
|
673 /*DOC*/ "\n"
|
|
|
674 /*DOC*/ "It calls the SDL_rotozoom library which is compiled in.\n"
|
|
319
|
675 /*DOC*/ "Note that the code in SDL_rotozoom is fairly messy and your\n"
|
|
|
676 /*DOC*/ "resulting image could be shifted and contain artifacts.\n"
|
|
199
|
677 /*DOC*/ ;
|
|
|
678
|
|
|
679 static PyObject* surf_rotozoom(PyObject* self, PyObject* arg)
|
|
|
680 {
|
|
|
681 PyObject *surfobj;
|
|
332
|
682 SDL_Surface *surf, *newsurf, *surf32;
|
|
199
|
683 float scale, angle;
|
|
|
684
|
|
|
685 /*get all the arguments*/
|
|
|
686 if(!PyArg_ParseTuple(arg, "O!ff", &PySurface_Type, &surfobj, &angle, &scale))
|
|
|
687 return NULL;
|
|
|
688 surf = PySurface_AsSurface(surfobj);
|
|
|
689
|
|
332
|
690 if(surf->format->BitsPerPixel == 32)
|
|
|
691 {
|
|
|
692 surf32 = surf;
|
|
|
693 PySurface_Lock(surfobj);
|
|
|
694 }
|
|
|
695 else
|
|
|
696 {
|
|
|
697 surf32 = SDL_CreateRGBSurface(SDL_SWSURFACE, surf->w, surf->h, 32,
|
|
|
698 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
|
|
|
699 SDL_BlitSurface(surf, NULL, surf32, NULL);
|
|
|
700 }
|
|
199
|
701
|
|
333
|
702 /* don't special case the 90 degrees, makes the rotating image pop
|
|
332
|
703 if(scale == 1.0 && !(((int)angle)%90))
|
|
|
704 newsurf = rotate90(surf32, (int)angle);
|
|
|
705 else
|
|
333
|
706 */ newsurf = rotozoomSurface(surf32, angle, scale, 1);
|
|
332
|
707
|
|
|
708 if(surf32 == surf)
|
|
|
709 PySurface_Unlock(surfobj);
|
|
|
710 else
|
|
|
711 SDL_FreeSurface(surf32);
|
|
199
|
712 return PySurface_New(newsurf);
|
|
|
713 }
|
|
190
|
714
|
|
|
715
|
|
|
716
|
|
|
717 static PyMethodDef transform_builtins[] =
|
|
|
718 {
|
|
|
719 { "scale", surf_scale, 1, doc_scale },
|
|
|
720 { "rotate", surf_rotate, 1, doc_rotate },
|
|
191
|
721 { "flip", surf_flip, 1, doc_flip },
|
|
199
|
722 { "rotozoom", surf_rotozoom, 1, doc_rotozoom},
|
|
432
|
723 { "scale2x", surf_scale2x, 1, doc_scale2x},
|
|
|
724
|
|
190
|
725 { NULL, NULL }
|
|
|
726 };
|
|
|
727
|
|
|
728
|
|
|
729
|
|
|
730 /*DOC*/ static char doc_pygame_transform_MODULE[] =
|
|
|
731 /*DOC*/ "Contains routines to transform a Surface image.\n"
|
|
|
732 /*DOC*/ "\n"
|
|
|
733 /*DOC*/ "All transformation functions take a source Surface and\n"
|
|
|
734 /*DOC*/ "return a new copy of that surface in the same format as\n"
|
|
|
735 /*DOC*/ "the original.\n"
|
|
|
736 /*DOC*/ "\n"
|
|
192
|
737 /*DOC*/ "Some of the\n"
|
|
|
738 /*DOC*/ "filters are 'destructive', which means if you transform\n"
|
|
|
739 /*DOC*/ "the image one way, you can't transform the image back to\n"
|
|
|
740 /*DOC*/ "the exact same way as it was before. If you plan on doing\n"
|
|
|
741 /*DOC*/ "many transforms, it is good practice to keep the original\n"
|
|
|
742 /*DOC*/ "untransformed image, and only translate that image.\n"
|
|
190
|
743 /*DOC*/ ;
|
|
|
744
|
|
|
745 PYGAME_EXPORT
|
|
|
746 void inittransform(void)
|
|
|
747 {
|
|
|
748 PyObject *module;
|
|
|
749 module = Py_InitModule3("transform", transform_builtins, doc_pygame_transform_MODULE);
|
|
|
750
|
|
|
751 /*imported needed apis*/
|
|
|
752 import_pygame_base();
|
|
|
753 import_pygame_rect();
|
|
|
754 import_pygame_surface();
|
|
|
755 }
|
|
|
756
|
|
|
757
|