Anonymous avatar Anonymous committed e186f4e

Fixed mask.from_threshold and surface locks.

Comments (0)

Files changed (2)

src/mask/maskmod.c

 static PyObject*
 _mask_fromthreshold (PyObject* self, PyObject* args)
 {
-    PyObject *surfobj, *surfobj2;
+    PyObject *surfobj, *surfobj2, *lock1, *lock2 = NULL;
     PyMask *maskobj;
     bitmask_t* m;
     SDL_Surface* surf, *surf2;
         PyErr_SetString (PyExc_MemoryError, "memory allocation failed");
         return NULL;
     }
-	
-    if (!PySurface_AddRefLock (surfobj, self))
+    
+    lock1 = PySurface_AcquireLockObj (surfobj, self);
+    if (!lock1)
     {
         bitmask_free (m);
         return NULL;
 
     if (surfobj2)
     {
-        if (!PySurface_AddRefLock(surfobj2, self))
+        lock2 = PySurface_AcquireLockObj (surfobj2, self);
+        if (!lock2)
         {
-            PySurface_RemoveRefLock (surfobj, self);
+            Py_DECREF (lock1);
             bitmask_free (m);
             return NULL;
         }
     _bitmask_threshold (m, surf, surf2, color,  color_threshold);
     Py_END_ALLOW_THREADS;
 
-    PySurface_RemoveRefLock(surfobj, self);
-    if (surfobj2)
-    {
-        PySurface_RemoveRefLock (surfobj2, self);
-    }
+    Py_DECREF (lock1);
+    Py_XDECREF (lock2);
 
     maskobj = (PyMask*)PyMask_Type.tp_new (&PyMask_Type, NULL, NULL);
     if(maskobj)

test/mask_test.py

         amask = mask_from_surface(surf)
         self.assertEqual(amask.get_at(0, 0), 0)
 
-    def todo_test_pygame2_mask_from_threshold(self):
+    def test_pygame2_mask_from_threshold(self):
 
         # __doc__ (as of 2008-11-03) for pygame2.mask.from_threshold:
 
         # TODO
-
-        self.fail() 
+        video.init ()
+        a = [16, 24, 32]
+        
+        for i in a:
+            surf = video.Surface (70, 70, i)
+            surf.fill (pygame2.Color (100,50,200), pygame2.Rect (20,20,20,20))
+            mask = pygame2.mask.from_threshold (surf,
+                                                pygame2.Color (100,50,200,255),
+                                                pygame2.Color (10,10,10,255))
+            
+            self.assertEqual (mask.count, 400)
+            self.assertEqual (mask.get_bounding_rects (),
+                              [pygame2.Rect (20, 20, 20, 20)])
+            
+        for i in a:
+            surf = video.Surface (70, 70, i)
+            surf2 = video.Surface (70,70, i)
+            surf.fill (pygame2.Color (100, 100, 100))
+            surf2.fill (pygame2.Color (150, 150, 150))
+            surf2.fill (pygame2.Color (100, 100, 100),
+                        pygame2.Rect (40, 40, 10, 10))
+            mask = pygame2.mask.from_threshold(surf, pygame2.Color (0, 0, 0, 0),
+                                               pygame2.Color (10, 10, 10, 255),
+                                               surf2)
+            
+            self.assertEqual (mask.count, 100)
+            self.assertEqual (mask.get_bounding_rects(),
+                              [pygame2.Rect (40, 40, 10, 10)])
 
 if __name__ == "__main__":
     unittest.main ()
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.