Commits

pygame  committed 4339b0d

transform module finished

  • Participants
  • Parent commits 0387ebd

Comments (0)

Files changed (7)

 At some point it would be nice to add support for more external
 python libraries, here are some ideas...
 
- - more drawing primitives in the draw module
- - GUI library. There's a few out there
- - Image transforms like flip, rotate, scale
  - MPEG video playback with smpeg
-
+ - more drawing primitives in the draw module: circle, poly
+ - more collision detection stuff: pixel2pixel, point2poly, etc
 
 
 

File docs/index.html

 <a href=ref/pygame_time.html#delay>pygame.time.delay</a> - delay for a number of milliseconds<br>
 <a href=ref/pygame_time.html#get_ticks>pygame.time.get_ticks</a> - milliseconds since startup<br>
 <a href=ref/pygame_time.html#set_timer>pygame.time.set_timer</a> - control timer events<br>
+<a href=ref/pygame_transform.html#flip>pygame.transform.flip</a> - flips a surface on either axis<br>
 <a href=ref/pygame_transform.html#rotate>pygame.transform.rotate</a> - rotate a Surface<br>
 <a href=ref/pygame_transform.html#scale>pygame.transform.scale</a> - scale a Surface to an arbitrary size<br>
 <a href=ref/pygame_version.html#ver>pygame.version.ver</a> - The current pygame version info<br>

File docs/ref/pygame_image.html

 <br>
 <h2 align=center>pygame.image</h2>
 This module contains functions to transfer images in and out
-of Surfaces. At the minimum the included <a href=#load>load()</a> function will
+of Surfaces. At the minimum the included <a href=pygame_mixer_music.html#load>load()</a> function will
 support BMP files. If SDL_image is properly installed when
 pygame is installed, it will support all the formats included
 with SDL_image. You can call the <a href=#get_extended>get_extended()</a> function to test
 if the SDL_image support is available.
 <br>&nbsp;<br>
 Some functions that communicate with other libraries will require
-that those libraries are properly installed. For example, the <a href=#save>save()</a>
+that those libraries are properly installed. For example, the <a href=Surface.html#save>save()</a>
 function can only save OPENGL surfaces if pyopengl is available.
 
 <hr>

File docs/ref/pygame_mixer_music.html

 <br>&nbsp;<br>
 The music module has many of the same types of functions as the
 Sound objects. The main difference is only one music object can
-be loaded at a time, with the <a href=pygame_image.html#load>load()</a> function. Music
+be loaded at a time, with the <a href=#load>load()</a> function. Music
 must be stored in an individual file on the system, it cannot be
 loaded from special file-like objects through python.
 

File docs/ref/pygame_transform.html

+<html>
+<title>pygame.transform</title>
+<body bgcolor=#dddddd text=#333377 link=#7744cc vlink=#8833dd>
+
+<table border=0 width=100% cellpadding=0 cellspacing=0 bgcolor=#f5f5f5><tr valign=top>
+<td rowspan=2><table border=0 cellpadding=5 cellspacing=0 bgcolor=#333377>
+<tr height=86 align=left><td valign=middle><font color=#ffffff size=+5>
+	<a href=../index.html><font size=+5 color=#ffffff><i><b>
+      pygame</b></i></font></a>&nbsp;&nbsp;</td>
+<td valign=middle><tt><font color=#dddddd><br>
+	pygame<br>documentation</font>
+</td></tr></table></td><td width=100% align=center valign=middle>
+
+	||&nbsp;
+	<a href=http://www.pygame.org>Home</a> &nbsp;||&nbsp;
+	<a href=../index.html>Help Contents</a> &nbsp;||
+	<br>&nbsp;<br>
+
+|| <a href=CD.html>CD</a> || 
+<a href=Channel.html>Channel</a> || 
+<a href=Font.html>Font</a> || 
+<a href=Joystick.html>Joystick</a> || 
+<a href=Rect.html>Rect</a> || 
+<a href=Sound.html>Sound</a> || 
+<a href=Surface.html>Surface</a> ||<br>
+|| <a href=pygame.html>pygame</a> || 
+<a href=pygame_UserRect.html>UserRect</a> || 
+<a href=pygame_cdrom.html>cdrom</a> || 
+<a href=pygame_constants.html>constants</a> || 
+<a href=pygame_cursors.html>cursors</a> || 
+<a href=pygame_display.html>display</a> || 
+<a href=pygame_draw.html>draw</a> ||<br>
+|| <a href=pygame_event.html>event</a> || 
+<a href=pygame_font.html>font</a> || 
+<a href=pygame_image.html>image</a> || 
+<a href=pygame_joystick.html>joystick</a> || 
+<a href=pygame_key.html>key</a> || 
+<a href=pygame_mixer.html>mixer</a> || 
+<a href=pygame_mixer_music.html>mixer_music</a> ||<br>
+|| <a href=pygame_mouse.html>mouse</a> || 
+<a href=pygame_surfarray.html>surfarray</a> || 
+<a href=pygame_time.html>time</a> || 
+<a href=pygame_transform.html>transform</a> || 
+<a href=pygame_version.html>version</a> ||<br>
+
+
+</td></tr></table>
+<br>
+<h2 align=center>pygame.transform</h2>
+Contains routines to transform a Surface image.
+<br>&nbsp;<br>
+All transformation functions take a source Surface and
+return a new copy of that surface in the same format as
+the original.
+<br>&nbsp;<br>
+These routines are not filtered or smoothed.
+
+<hr>
+
+<table>
+<tr><td><a href=#flip>flip</a></td><td> -
+flips a surface on either axis</td></tr>
+
+
+<tr><td><a href=#rotate>rotate</a></td><td> -
+rotate a Surface</td></tr>
+
+
+<tr><td><a href=#scale>scale</a></td><td> -
+scale a Surface to an arbitrary size</td></tr>
+
+
+</table>
+
+<hr>
+
+<a name=flip><font size=+2><b>flip
+</b></font><br><font size=+1><tt>
+pygame.transform.flip(Surface, xaxis, yaxis) -> Surface
+</tt></font><ul>
+Flips the image on the x-axis or the y-axis if the argument
+for that axis is true.
+</ul><br>&nbsp;<br>
+
+<a name=rotate><font size=+2><b>rotate
+</b></font><br><font size=+1><tt>
+pygame.transform.rotate(Surface, angle) -> Surface
+</tt></font><ul>
+Rotates the image clockwise with the given angle (in degrees).
+The result size will likely be a different resolution than the
+original.
+</ul><br>&nbsp;<br>
+
+<a name=scale><font size=+2><b>scale
+</b></font><br><font size=+1><tt>
+pygame.transform.scale(Surface, size) -> Surface
+</tt></font><ul>
+Scale the image to the new resolution.
+</ul><br>&nbsp;<br>
+
+
+<hr>
+</body></html>

File src/surface.c

 
 	if(x < 0 || x >= surf->w || y < 0 || y >= surf->h)
 	{
-		PyErr_SetString(PyExc_IndexError, "pixel index out of range");
+		PyErr_SetString(PyExc_IndexError, "pixel coordinate out of range");
 		return NULL;
 	}
 

File src/transform.c

 
 static void rotate(SDL_Surface *src, SDL_Surface *dst, Uint32 bgcolor, int cx, int cy, int isin, int icos)
 {
-	int x, y, dx, dy, sdx, sdy;
+	int x, y, dx, dy;
 
 	Uint8 *srcpix = (Uint8*)src->pixels;
 	Uint8 *dstrow = (Uint8*)dst->pixels;
 
-	int pixsize = src->format->BytesPerPixel;
 	int srcpitch = src->pitch;
 	int dstpitch = dst->pitch;
 
 	int ax = (cx << 16) - (icos * cx);
 	int ay = (cy << 16) - (isin * cx);
 
-	for(y = 0; y < dst->h; y++)
+	int minval = (1 << 16) - 1;
+	int xmaxval = src->w << 16;
+	int ymaxval = src->h << 16;
+
+	switch(src->format->BytesPerPixel)
 	{
-		Uint8 *srcpos, *dstpos = (Uint8*)dstrow;
-		dy = cy - y;
-		sdx = (ax + (isin * dy)) + xd;
-		sdy = (ay - (icos * dy)) + yd;
-		for(x = 0; x < dst->w; x++)
-		{
-			dx = sdx >> 16;
-			dy = sdy >> 16;
-			if((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h))
-			{
-				srcpos = (Uint8*)(srcpix + (dy * srcpitch) + (dx * pixsize));
-				*dstpos++ = *srcpos;
+	case 1:
+		for(y = 0; y < dst->h; y++) {
+			Uint8 *dstpos = (Uint8*)dstrow;
+			dy = cy - y;
+			dx = (ax + (isin * dy)) + xd;
+			dy = (ay - (icos * dy)) + yd;
+			for(x = 0; x < dst->w; x++) {
+				if(dx<minval || dy < minval || dx > xmaxval || dy > ymaxval) *dstpos++ = bgcolor;
+				else *dstpos++ = *(Uint8*)(srcpix + ((dy>>16) * srcpitch) + (dx>>16));
+				dx += icos; dy += isin;
 			}
-			else
-				*dstpos++ = bgcolor;
-			sdx += icos;
-			sdy += isin;
-		}
-		dstrow += dstpitch;
-	}  
+			dstrow += dstpitch;
+		}break;
+	case 2:
+		for(y = 0; y < dst->h; y++) {
+			Uint16 *dstpos = (Uint16*)dstrow;
+			dy = cy - y;
+			dx = (ax + (isin * dy)) + xd;
+			dy = (ay - (icos * dy)) + yd;
+			for(x = 0; x < dst->w; x++) {
+				if(dx<minval || dy < minval || dx > xmaxval || dy > ymaxval) *dstpos++ = bgcolor;
+				else *dstpos++ = *(Uint16*)(srcpix + ((dy>>16) * srcpitch) + (dx>>16<<1));
+				dx += icos; dy += isin;
+			}
+			dstrow += dstpitch;
+		}break;
+	case 4:
+		for(y = 0; y < dst->h; y++) {
+			Uint32 *dstpos = (Uint32*)dstrow;
+			dy = cy - y;
+			dx = (ax + (isin * dy)) + xd;
+			dy = (ay - (icos * dy)) + yd;
+			for(x = 0; x < dst->w; x++) {
+				if(dx<minval || dy < minval || dx > xmaxval || dy > ymaxval) *dstpos++ = bgcolor;
+				else *dstpos++ = *(Uint32*)(srcpix + ((dy>>16) * srcpitch) + (dx>>16<<2));
+				dx += icos; dy += isin;
+			}
+			dstrow += dstpitch;
+		}break;
+	default: /*case 3:*/
+		for(y = 0; y < dst->h; y++) {
+			Uint8 *dstpos = (Uint8*)dstrow;
+			dy = cy - y;
+			dx = (ax + (isin * dy)) + xd;
+			dy = (ay - (icos * dy)) + yd;
+			for(x = 0; x < dst->w; x++) {
+				if(dx<minval || dy < minval || dx > xmaxval || dy > ymaxval)
+				{
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+					dstpos[0] = ((Uint8*)&bgcolor)[0]; dstpos[1] = ((Uint8*)&bgcolor)[1]; dstpos[2] = ((Uint8*)&bgcolor)[2];
+#else
+					dstpos[0] = ((Uint8*)&bgcolor)[4]; dstpos[3] = ((Uint8*)&bgcolor)[1]; dstpos[2] = ((Uint8*)&bgcolor)[2];
+#endif
+					dstpos += 3;
+				}
+				else {
+					Uint8* srcpos = (Uint8*)(srcpix + ((dy>>16) * srcpitch) + ((dx>>16) * 3));
+					dstpos[0] = srcpos[0]; dstpos[1] = srcpos[1]; dstpos[2] = srcpos[2];
+					dstpos += 3;
+				}
+				dx += icos; dy += isin;
+			}
+			dstrow += dstpitch;
+		}break;
+	}
 }
 
 
 
 
 
+    /*DOC*/ static char doc_flip[] =
+    /*DOC*/    "pygame.transform.flip(Surface, xaxis, yaxis) -> Surface\n"
+    /*DOC*/    "flips a surface on either axis\n"
+    /*DOC*/    "\n"
+    /*DOC*/    "Flips the image on the x-axis or the y-axis if the argument\n"
+    /*DOC*/    "for that axis is true.\n"
+    /*DOC*/ ;
+
+static PyObject* surf_flip(PyObject* self, PyObject* arg)
+{
+	PyObject *surfobj;
+	SDL_Surface* surf, *newsurf;
+	int xaxis, yaxis;
+	int loopx, loopy;
+	int pixsize, srcpitch, dstpitch;
+	Uint8 *srcpix, *dstpix;
+
+	/*get all the arguments*/
+	if(!PyArg_ParseTuple(arg, "O!ii", &PySurface_Type, &surfobj, &xaxis, &yaxis))
+		return NULL;
+	surf = PySurface_AsSurface(surfobj);
+
+	newsurf = newsurf_fromsurf(surf, surf->w, surf->h);
+	if(!newsurf) return NULL;
+
+	pixsize = surf->format->BytesPerPixel;
+	srcpitch = surf->pitch;
+	dstpitch = newsurf->pitch;
+
+	SDL_LockSurface(newsurf);
+	PySurface_Lock(surfobj);
+
+	srcpix = (Uint8*)surf->pixels;
+	dstpix = (Uint8*)newsurf->pixels;
+
+	if(!xaxis)
+	{
+		if(!yaxis)
+		{
+			for(loopy = 0; loopy < surf->h; ++loopy)
+				memcpy(dstpix+loopy*dstpitch, srcpix+loopy*srcpitch, surf->w*surf->format->BytesPerPixel);
+		}
+		else
+		{
+			for(loopy = 0; loopy < surf->h; ++loopy)
+				memcpy(dstpix+loopy*dstpitch, srcpix+(surf->h-1-loopy)*srcpitch, surf->w*surf->format->BytesPerPixel);
+		}
+	}
+	else /*if (xaxis)*/
+	{
+		if(yaxis)
+		{
+			switch(surf->format->BytesPerPixel)
+			{
+			case 1:
+				for(loopy = 0; loopy < surf->h; ++loopy) {
+					Uint8* dst = (Uint8*)(dstpix+loopy*dstpitch);
+					Uint8* src = ((Uint8*)(srcpix+(surf->h-1-loopy)*srcpitch)) + surf->w;
+					for(loopx = 0; loopx < surf->w; ++loopx)
+						*dst++ = *src--;
+				}break;
+			case 2:
+				for(loopy = 0; loopy < surf->h; ++loopy) {
+					Uint16* dst = (Uint16*)(dstpix+loopy*dstpitch);
+					Uint16* src = ((Uint16*)(srcpix+(surf->h-1-loopy)*srcpitch)) + surf->w;
+					for(loopx = 0; loopx < surf->w; ++loopx)
+						*dst++ = *src--;
+				}break;
+			case 4:
+				for(loopy = 0; loopy < surf->h; ++loopy) {
+					Uint32* dst = (Uint32*)(dstpix+loopy*dstpitch);
+					Uint32* src = ((Uint32*)(srcpix+(surf->h-1-loopy)*srcpitch)) + surf->w;
+					for(loopx = 0; loopx < surf->w; ++loopx)
+						*dst++ = *src--;
+				}break;
+			case 3:
+				for(loopy = 0; loopy < surf->h; ++loopy) {
+					Uint8* dst = (Uint8*)(dstpix+loopy*dstpitch);
+					Uint8* src = ((Uint8*)(srcpix+(surf->h-1-loopy)*srcpitch)) + surf->w*3;
+					for(loopx = 0; loopx < surf->w; ++loopx)
+					{
+						dst[0] = src[0]; dst[1] = src[1]; dst[2] = src[2];
+						dst += 3;
+						src -= 3;
+					}
+				}break;
+			}
+		}
+		else
+		{
+			switch(surf->format->BytesPerPixel)
+			{
+			case 1:
+				for(loopy = 0; loopy < surf->h; ++loopy) {
+					Uint8* dst = (Uint8*)(dstpix+loopy*dstpitch);
+					Uint8* src = ((Uint8*)(srcpix+loopy*srcpitch)) + surf->w;
+					for(loopx = 0; loopx < surf->w; ++loopx)
+						*dst++ = *src--;
+				}break;
+			case 2:
+				for(loopy = 0; loopy < surf->h; ++loopy) {
+					Uint16* dst = (Uint16*)(dstpix+loopy*dstpitch);
+					Uint16* src = ((Uint16*)(srcpix+loopy*srcpitch)) + surf->w;
+					for(loopx = 0; loopx < surf->w; ++loopx)
+						*dst++ = *src--;
+				}break;
+			case 4:
+				for(loopy = 0; loopy < surf->h; ++loopy) {
+					Uint32* dst = (Uint32*)(dstpix+loopy*dstpitch);
+					Uint32* src = ((Uint32*)(srcpix+loopy*srcpitch)) + surf->w;
+					for(loopx = 0; loopx < surf->w; ++loopx)
+						*dst++ = *src--;
+				}break;
+			case 3:
+				for(loopy = 0; loopy < surf->h; ++loopy) {
+					Uint8* dst = (Uint8*)(dstpix+loopy*dstpitch);
+					Uint8* src = ((Uint8*)(srcpix+loopy*srcpitch)) + surf->w*3;
+					for(loopx = 0; loopx < surf->w; ++loopx)
+					{
+						dst[0] = src[0]; dst[1] = src[1]; dst[2] = src[2];
+						dst += 3;
+						src -= 3;
+					}
+				}break;
+			}
+		}
+	}
+
+	PySurface_Unlock(surfobj);
+	SDL_UnlockSurface(newsurf);
+
+	return PySurface_New(newsurf);
+}
+
+
+
+
 
 
 
 {
 	{ "scale", surf_scale, 1, doc_scale },
 	{ "rotate", surf_rotate, 1, doc_rotate },
+	{ "flip", surf_flip, 1, doc_flip },
 
 	{ NULL, NULL }
 };