Anonymous avatar Anonymous committed cf2bd97

surfarray.blit_array supports all depths

Comments (0)

Files changed (3)

 	Release the Python Global Interpreter Lock on delays and IO
 	Added timer events to pygame.time (vgrade example demonstrates)
 	Documentation reorganized, 1st tutorial added
+	surfarray.blit_array() supports all bit depths
 
      Nov 11, 2000
 	BUG: pygame.display.set_mode() uses int, not short, for size

examples/vgrade.py

     column = topcolor + (diff * column).astype(Int)
     # make the column a 3d image column by adding X
     column = column.astype(UnsignedInt8)[NewAxis,:,:]
+    #3d array into 2d array
+    column = pygame.surfarray.map_array(surf, column)
     # stretch the column into a full image
-    return resize(column, (width, height, 3))
+    return resize(column, (width, height))
 
 
 
 
 def main():
     pygame.init()
-    size = (640, 480)
-    screen = pygame.display.set_mode(size, 0, 32)
-    finished = 0
+    size = 640, 480
+    screen = pygame.display.set_mode(size, 0, 24)
+
     pygame.event.set_blocked(MOUSEMOTION) #keep our queue cleaner
-    pygame.time.set_timer(500, USEREVENT+5)
+    pygame.time.set_timer(500, USEREVENT)
+
     while 1:
         event = pygame.event.poll()
         if event.type in (QUIT, KEYDOWN, MOUSEBUTTONDOWN):
             break
-        elif event.type == USEREVENT+5:
+        elif event.type == USEREVENT:
             DisplayGradient(screen)
 
     
 
 
 
+
+/*macros used to blit arrays*/
+
+
+#define COPYMACRO_2D(DST, SRC) \
+	for(loopy = 0; loopy < sizey; ++loopy) { \
+		DST* imgrow = (DST*)(((char*)surf->pixels)+loopy*surf->pitch); \
+		char* datarow = array->data + stridey * loopy; \
+		for(loopx = 0; loopx < sizex; ++loopx) \
+			*(imgrow + loopx) = (DST)*(SRC*)(datarow + stridex * loopx); \
+	}
+
+
+#define COPYMACRO_2D_24(SRC) \
+	for(loopy = 0; loopy < sizey-1; ++loopy) { \
+		char* imgrow = ((char*)surf->pixels)+loopy*surf->pitch; \
+		char* datarow = array->data + stridey * loopy; \
+		for(loopx = 0; loopx < sizex; ++loopx) \
+			*(int*)(imgrow + loopx*3) = (int)*(SRC*)(datarow + stridex * loopx)<<8; \
+	}{ \
+	char* imgrow = ((char*)surf->pixels)+loopy*surf->pitch; \
+	char* datarow = array->data + stridey * loopy; \
+	for(loopx = 0; loopx < sizex-1; ++loopx) \
+		*(int*)(imgrow + loopx*3) = ((int)*(SRC*)(datarow + stridex * loopx))<<8; \
+	}
+
+
+#define COPYMACRO_3D(DST, SRC) \
+	for(loopy = 0; loopy < sizey; ++loopy) { \
+		DST* data = (DST*)(((char*)surf->pixels) + surf->pitch * loopy); \
+		char* pix = array->data + stridey * loopy; \
+		for(loopx = 0; loopx < sizex; ++loopx) { \
+			*data++ = (DST)(*(SRC*)(pix) >> Rloss << Rshift) | \
+					(*(SRC*)(pix+stridez) >> Gloss << Gshift) | \
+					(*(SRC*)(pix+stridez2) >> Bloss << Bshift); \
+			pix += stridex; \
+	}	}
+
+
+#define COPYMACRO_3D_24(SRC) \
+	for(loopy = 0; loopy < sizey; ++loopy) { \
+		char* data = ((char*)surf->pixels) + surf->pitch * loopy; \
+		char* pix = array->data + stridey * loopy; \
+		for(loopx = 0; loopx < sizex; ++loopx) { \
+			*data++ = (char)*(SRC*)(pix); \
+			*data++ = (char)*(SRC*)(pix+stridez); \
+			*data++ = (char)*(SRC*)(pix+stridez2); \
+			pix += stridex; \
+	}	}
+
+
+
+
     /*DOC*/ static char doc_blit_array[] =
     /*DOC*/    "pygame.surfarray.blit_array(surf, array) -> None\n"
     /*DOC*/    "quickly transfer an array to a Surface\n"
 	switch(surf->format->BytesPerPixel)
 	{
 	case 1:
-	case 2:
-	case 3:
-		return RAISE(PyExc_RuntimeError, "unsupported bit depth for image");
-	case 4:
-		{
-			if(array->nd == 2)
-			{
-				if(array->descr->elsize == sizeof(char))
-				{
-					for(loopy = 0; loopy < sizey; ++loopy)
-					{
-						int* imgrow = (int*)(((char*)surf->pixels)+loopy*surf->pitch);
-						char* datarow = array->data + stridey * loopy;
-						for(loopx = 0; loopx < sizex; ++loopx)
-						{
-							int* pix = imgrow + loopx;
-							char* data = datarow + stridex * loopx;
-							*(imgrow + loopx) = (int)*(unsigned char*)(datarow + stridex * loopx);
-						}
-					}
-				}
-				else if(array->descr->elsize == sizeof(int))
-				{
-					for(loopy = 0; loopy < sizey; ++loopy)
-					{
-						int* imgrow = (int*)(((char*)surf->pixels)+loopy*surf->pitch);
-						char* datarow = array->data + stridey * loopy;
-						for(loopx = 0; loopx < sizex; ++loopx)
-						{
-							int* pix = imgrow + loopx;
-							char* data = datarow + stridex * loopx;
-							*(imgrow + loopx) = (int)*(int*)(datarow + stridex * loopx);
-						}
-					}
-				}
-			}
-			else
-			{
-				switch(array->descr->elsize)
-				{
-				case sizeof(char):
-					for(loopy = 0; loopy < sizey; ++loopy)
-					{
-						int* data = (int*)(((char*)surf->pixels) + surf->pitch * loopy);
-						char* pix = array->data + stridey * loopy;
-						for(loopx = 0; loopx < sizex; ++loopx)
-						{
-							*data++ = (*((unsigned char*)(pix)) >> Rloss << Rshift) |
-									((*((unsigned char*)(pix+stridez))) >> Gloss << Gshift) |
-									((*((unsigned char*)(pix+stridez2))) >> Bloss << Bshift);
-							pix += stridex;
-						}
-					}break;
-				case sizeof(int):
-					for(loopy = 0; loopy < sizey; ++loopy)
-					{
-						int* data = (int*)(((char*)surf->pixels) + surf->pitch * loopy);
-						char* pix = array->data + stridey * loopy;
-						for(loopx = 0; loopx < sizex; ++loopx)
-						{
-							*data++ = (*((unsigned int*)(pix)) >> Rloss << Rshift) |
-									((*((unsigned int*)(pix+stridez))) >> Gloss << Gshift) |
-									((*((unsigned int*)(pix+stridez2))) >> Bloss << Bshift);
-							pix += stridex;
-						}
-					}break;
+		if(array->nd == 2) {
+			switch(array->descr->elsize) {
+				case sizeof(char):  COPYMACRO_2D(unsigned char, unsigned char)  break;
+				case sizeof(short): COPYMACRO_2D(unsigned char, short)  break;
+				case sizeof(int):   COPYMACRO_2D(unsigned char, int)  break;
 				default: 
 					return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
-				}
 			}
-		}break;
+		}
+		break;
+	case 2:
+		if(array->nd == 2) {
+			switch(array->descr->elsize) {
+				case sizeof(char):  COPYMACRO_2D(short, unsigned char)  break;
+				case sizeof(short): COPYMACRO_2D(short, short)  break;
+				case sizeof(int):   COPYMACRO_2D(short, int)  break;
+				default: 
+					return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
+			}
+		} else {
+			switch(array->descr->elsize) {
+				case sizeof(char): COPYMACRO_3D(short, unsigned char)  break;
+				case sizeof(short):COPYMACRO_3D(short, short)  break;
+				case sizeof(int):  COPYMACRO_3D(short, int)  break;
+				default: 
+					return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
+			}
+		}
+		break;
+	case 3:
+		if(array->nd == 2) {
+			switch(array->descr->elsize) {
+				case sizeof(char):  COPYMACRO_2D_24(unsigned char)  break;
+				case sizeof(short): COPYMACRO_2D_24(short)  break;
+				case sizeof(int):   COPYMACRO_2D_24(int)  break;
+				default: 
+					return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
+			}
+		} else {
+			switch(array->descr->elsize) {
+				case sizeof(char): COPYMACRO_3D_24(unsigned char)  break;
+				case sizeof(short):COPYMACRO_3D_24(short)  break;
+				case sizeof(int):  COPYMACRO_3D_24(int)  break;
+				default: 
+					return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
+			}
+		}
+		break;
+	case 4:
+		if(array->nd == 2) {
+			switch(array->descr->elsize) {
+				case sizeof(char):  COPYMACRO_2D(int, unsigned char)  break;
+				case sizeof(short): COPYMACRO_2D(int, short)  break;
+				case sizeof(int):   COPYMACRO_2D(int, int)  break;
+			default: 
+					return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
+			}
+		} else {
+			switch(array->descr->elsize) {
+				case sizeof(char): COPYMACRO_3D(int, unsigned char)  break;
+				case sizeof(short):COPYMACRO_3D(int, short)  break;
+				case sizeof(int):  COPYMACRO_3D(int, int)  break;
+				default: 
+					return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
+			}
+		}
+		break;
+	default:
+		return RAISE(PyExc_RuntimeError, "unsupported bit depth for image");
 	}
-
 	RETURN_NONE;
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.