1. pygame
  2. Untitled project
  3. pygame

Commits

marcus  committed 684d96c

Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.

  • Participants
  • Parent commits 404e3a0
  • Branches default

Comments (0)

Files changed (7)

File docs/ref/index.html

View file
 </ul>
 <li><a href="pixelarray.html#pygame.PixelArray">pygame.PixelArray</a> - <font size=-1>pygame Object for direct pixel access of surfaces</font></li>
 <ul>
-<li><a href="pixelarray.html#PixelArray.extract">PixelArray.extract</a> - <font size=-1>Extracts the passed color in the PixelArray.</font></li>
+<li><a href="pixelarray.html#PixelArray.extract">PixelArray.extract</a> - <font size=-1>Extracts the passed color from the PixelArray.</font></li>
 <li><a href="pixelarray.html#PixelArray.make_surface">PixelArray.make_surface</a> - <font size=-1>Creates a new Surface from the current PixelArray.</font></li>
 <li><a href="pixelarray.html#PixelArray.replace">PixelArray.replace</a> - <font size=-1>Replaces the passed color in the PixelArray with another one.</font></li>
 <li><a href="pixelarray.html#PixelArray.surface">PixelArray.surface</a> - <font size=-1>Gets the Surface the PixelArray uses.</font></li>

File docs/ref/pixelarray.html

View file
   <tr><td><a href="pixelarray.html#PixelArray.surface">PixelArray.surface</a> - <font size=-1>Gets the Surface the PixelArray uses.</font></td><td>Gets the Surface the PixelArray uses.</td></tr>
   <tr><td><a href="pixelarray.html#PixelArray.make_surface">PixelArray.make_surface</a> - <font size=-1>Creates a new Surface from the current PixelArray.</font></td><td>Creates a new Surface from the current PixelArray.</td></tr>
   <tr><td><a href="pixelarray.html#PixelArray.replace">PixelArray.replace</a> - <font size=-1>Replaces the passed color in the PixelArray with another one.</font></td><td>Replaces the passed color in the PixelArray with another one.</td></tr>
-  <tr><td><a href="pixelarray.html#PixelArray.extract">PixelArray.extract</a> - <font size=-1>Extracts the passed color in the PixelArray.</font></td><td>Extracts the passed color in the PixelArray.</td></tr>
+  <tr><td><a href="pixelarray.html#PixelArray.extract">PixelArray.extract</a> - <font size=-1>Extracts the passed color from the PixelArray.</font></td><td>Extracts the passed color from the PixelArray.</td></tr>
 </table></small></ul>
 <p>The PixelArray wraps up a Surface and provides a direct <tt>2D</tt> array access to its pixels using the surface its rows as first and its columns as second axis. It supports slicing, row and pixel manipluation, slicing and slice assignments while inplace operations such as addition, subtraction, multiplication, division and so forth are not allowed. </p>
 <p>While it is possible to assign both, integer color values and <tt>RGB(A)</tt> color tuples, the PixelArray will only use integers for the color representation. Thus, checking for certain colors has to be done using the <tt><a href="surface.html#Surface.map_rgb">Surface.map_rgb</a> - <font size=-1>convert a color into a mapped color value</font></tt> method of the surface, the PixelArray was created for. </p>
 
 <a name="PixelArray.extract">
 <big><b>PixelArray.extract</big></b><br><ul>
-  <i>Extracts the passed color in the PixelArray.</i><br>
-  <tt>PixelArray.extract (color): Return None</tt><br>
-<p>Extracts the passed color by changing all matching pixels to white, while non-matching pixels are changed to black. The extraction is done in place on the PixelArray. </p>
+  <i>Extracts the passed color from the PixelArray.</i><br>
+  <tt>PixelArray.extract (color): Return PixelArray</tt><br>
+<p>Extracts the passed color by changing all matching pixels to white, while non-matching pixels are changed to black. This returns a new PixelArray with the black/white color mask. </p>
 <p>New in <tt>pygame.1.8.1</tt>. </p>
 <!--COMMENTS:PixelArray.extract--> &nbsp;<br> 
 <br></ul>

File src/pixelarray.c

View file
 #define SURFACE_EQUALS(x,y) \
     (((PyPixelArray *)x)->surface == ((PyPixelArray *)y)->surface)
 
-
 static PyPixelArray*
 _pxarray_new_internal (PyTypeObject *type, PyObject *surface,
     Uint32 xstart, Uint32 ystart, Uint32 xlen, Uint32 ylen,

File src/pixelarray.doc

View file
 <END>
 
 extract
-Extracts the passed color in the PixelArray.
-PixelArray.extract (color): Return None
+Extracts the passed color from the PixelArray.
+PixelArray.extract (color): Return PixelArray
 
 Extracts the passed color by changing all matching pixels to white,
-while non-matching pixels are changed to black. The extraction is
-done in place on the PixelArray.
+while non-matching pixels are changed to black. This returns a new
+PixelArray with the black/white color mask.
 
 New in pygame.1.8.1.
 <END>

File src/pixelarray_methods.c

View file
 static PyObject*
 _extract_color (PyPixelArray *array, PyObject *args)
 {
+    PyObject *sf = NULL;
     PyObject *excolor = NULL;
+    PyPixelArray *newarray = NULL;
     Uint32 color;
     Uint32 white;
     Uint32 black;
     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)
     {
         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;
     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;
         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
                     *(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;
     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;
 }

File src/pygamedocs.h

View file
 
 #define DOC_PIXELARRAYREPLACE "PixelArray.replace (color, replacecolor): Return None\nReplaces the passed color in the PixelArray with another one."
 
-#define DOC_PIXELARRAYEXTRACT "PixelArray.extract (color): Return None\nExtracts the passed color in the PixelArray."
+#define DOC_PIXELARRAYEXTRACT "PixelArray.extract (color): Return PixelArray\nExtracts the passed color from the PixelArray."
 
 #define DOC_PYGAMERECT "pygame.Rect(left, top, width, height): return Rect\npygame.Rect((left, top), (width, height)): return Rect\npygame.Rect(object): return Rect\npygame object for storing rectangular coordinates"
 

File test/pixelarray_test.py

View file
             black = sf.map_rgb ((0, 0, 0))
 
             ar = pygame.PixelArray (sf)
-            ar.extract ((255, 0, 0))
+            newar = ar.extract ((255, 0, 0))
 
-            self.assertEqual (ar[0][0], black)
-            self.assertEqual (ar[1][0], black)
-            self.assertEqual (ar[2][3], white)
-            self.assertEqual (ar[3][6], white)
-            self.assertEqual (ar[8][9], black)
-            self.assertEqual (ar[9][9], black)
+            self.assertEqual (newar[0][0], black)
+            self.assertEqual (newar[1][0], black)
+            self.assertEqual (newar[2][3], white)
+            self.assertEqual (newar[3][6], white)
+            self.assertEqual (newar[8][9], black)
+            self.assertEqual (newar[9][9], black)
 
 if __name__ == '__main__':
     unittest.main()