diff src/pixelarray_methods.c @ 991:684d96c73d0d

Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
author marcus
date Fri, 04 Apr 2008 10:40:09 +0000
parents 404e3a0be4f8
children 1577bcf02ed5
line wrap: on
line diff
--- a/src/pixelarray_methods.c	Fri Apr 04 10:15:24 2008 +0000
+++ b/src/pixelarray_methods.c	Fri Apr 04 10:40:09 2008 +0000
@@ -441,7 +441,9 @@
 static PyObject*
 _extract_color (PyPixelArray *array, PyObject *args)
 {
+    PyObject *sf = NULL;
     PyObject *excolor = NULL;
+    PyPixelArray *newarray = NULL;
     Uint32 color;
     Uint32 white;
     Uint32 black;
@@ -462,14 +464,22 @@
     if (!_get_color_from_object (excolor, surface->format, &color))
         return NULL;
 
+    /* Create the b/w mask surface. */
+    sf = _make_surface (array);
+    if (!sf)
+        return NULL;
+    newarray = (PyPixelArray *) PyPixelArray_New (sf);
+    if (!newarray)
+        return NULL;
+    surface = PySurface_AsSurface (newarray->surface);
+
     black = SDL_MapRGBA (surface->format, 0, 0, 0, 255);
     white = SDL_MapRGBA (surface->format, 255, 255, 255, 255);
 
-    surface = PySurface_AsSurface (array->surface);
     pixels = surface->pixels;
-    absxstep = ABS (array->xstep);
-    absystep = ABS (array->ystep);
-    y = array->ystart;
+    absxstep = ABS (newarray->xstep);
+    absystep = ABS (newarray->ystep);
+    y = newarray->ystart;
 
     switch (surface->format->BytesPerPixel)
     {
@@ -478,16 +488,16 @@
         Uint8 *pixel;
         while (posy < array->ylen)
         {
-            x = array->xstart;
+            x = newarray->xstart;
             posx = 0;
-            while (posx < array->xlen)
+            while (posx < newarray->xlen)
             {
                 pixel = ((Uint8 *) pixels + y * surface->pitch + x);
                 *pixel = (*pixel == color) ? (Uint8) white : (Uint8) black;
-                x += array->xstep;
+                x += newarray->xstep;
                 posx += absxstep;
             }
-            y += array->ystep;
+            y += newarray->ystep;
             posy += absystep;
         }
         break;
@@ -495,18 +505,18 @@
     case 2:
     {
         Uint16 *pixel;
-        while (posy < array->ylen)
+        while (posy < newarray->ylen)
         {
             x = array->xstart;
             posx = 0;
-            while (posx < array->xlen)
+            while (posx < newarray->xlen)
             {
                 pixel = ((Uint16 *) (pixels + y * surface->pitch) + x);
                 *pixel = (*pixel == color) ? (Uint16) white : (Uint16) black;
-                x += array->xstep;
+                x += newarray->xstep;
                 posx += absxstep;
             }
-            y += array->ystep;
+            y += newarray->ystep;
             posy += absystep;
         }
         break;
@@ -516,11 +526,11 @@
         Uint8 *px;
         Uint32 pxcolor;
         SDL_PixelFormat *format = surface->format;
-        while (posy < array->ylen)
+        while (posy < newarray->ylen)
         {
-            x = array->xstart;
+            x = newarray->xstart;
             posx = 0;
-            while (posx < array->xlen)
+            while (posx < newarray->xlen)
             {
                 px = (Uint8 *) (pixels + y * surface->pitch) + x * 3;
 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
@@ -552,10 +562,10 @@
                     *(px + 2 - (format->Bshift >> 3)) = (Uint8) black;
                 }
 #endif
-                x += array->xstep;
+                x += newarray->xstep;
                 posx += absxstep;
             }
-            y += array->ystep;
+            y += newarray->ystep;
             posy += absystep;
         }
         break;
@@ -563,22 +573,22 @@
     default:
     {
         Uint32 *pixel;
-        while (posy < array->ylen)
+        while (posy < newarray->ylen)
         {
-            x = array->xstart;
+            x = newarray->xstart;
             posx = 0;
-            while (posx < array->xlen)
+            while (posx < newarray->xlen)
             {
                 pixel = ((Uint32 *) (pixels + y * surface->pitch) + x);
                 *pixel = (*pixel == color) ? white : black;
-                x += array->xstep;
+                x += newarray->xstep;
                 posx += absxstep;
             }
-            y += array->ystep;
+            y += newarray->ystep;
             posy += absystep;
         }
         break;
     }
     }
-    Py_RETURN_NONE;
+    return (PyObject *) newarray;
 }