Commits

illume committed 5dd2dc1

mask.from_surface works now... and is 128x faster than the python version.

  • Participants
  • Parent commits 27d57af

Comments (0)

Files changed (2)

 import pygame, pygame.image, pygame.surface, pygame.time, pygame.display
 
 def maskFromSurface(surface, threshold = 127):
-    return pygame.mask.from_surface(surface, threshold)
+    #return pygame.mask.from_surface(surface, threshold)
 
     mask = pygame.mask.Mask(surface.get_size())
     key = surface.get_colorkey()
     for impath in argv[1:]:
         images.append(pygame.image.load(impath).convert_alpha())
         masks.append(maskFromSurface(images[-1]))
+
+    numtimes = 10
+    import time
+    t1 = time.time()
+    for x in range(numtimes):
+        m = maskFromSurface(images[-1])
+    t2 = time.time()
+
+    print "python maskFromSurface :%s" % (t2-t1)
+
+    t1 = time.time()
+    for x in range(numtimes):
+        m = pygame.mask.from_surface(images[-1])
+    t2 = time.time()
+
+    print "C pygame.mask.from_surface :%s" % (t2-t1)
+
     sprites = []
     for i in range(20):
         j = i % len(images)
         Uint8 *pix;
         Uint8 r, g, b, a;
 
-        printf("hi there1\n");
-
-        //TODO: set threshold as 127 default argument.
+        /* set threshold as 127 default argument. */
         threshold = 127;
 
         /* get the surface from the passed in arguments. 
          *   surface, threshold
          */
 
-        if (!PyArg_ParseTuple (args, "O!|(i)", &PySurface_Type, &surfobj, &threshold))
+        if (!PyArg_ParseTuple (args, "O!|i", &PySurface_Type, &surfobj, &threshold))
             return NULL;
 
-        printf("hi there2\n");
 	surf = PySurface_AsSurface(surfobj);
 
-        printf("hi there3\n");
-
         /* lock the surface, release the GIL. */
         PySurface_Lock (surfobj);
 
             }
         }
 
-
-
         /* unlock the surface, release the GIL.
          */
         PySurface_Unlock (surfobj);
   module = Py_InitModule3("mask", mask_builtins, DOC_PYGAMEMASK);
   dict = PyModule_GetDict(module);
   PyDict_SetItemString(dict, "MaskType", (PyObject *)&PyMask_Type);
+  import_pygame_base ();
+  import_pygame_surface ();
 }