Commits

Anonymous committed fb657ea

Added mask.Mask.copy() method and copy-module support to mask.Mask.

  • Participants
  • Parent commits 6f25b30
  • Branches pgreloaded

Comments (0)

Files changed (6)

 
 build:
 	@if test -n $(COMPILER); then \
-        echo "Running build with $(COMPILER)..."; \
+		echo "Running build with $(COMPILER)..."; \
 		WITH_EXPERIMENTAL=$(EXPERIMENTAL) $(PYTHON) setup.py build -c $(COMPILER); \
 	else \
-        echo "Running build"; \
+		echo "Running build"; \
 		WITH_EXPERIMENTAL=$(EXPERIMENTAL) $(PYTHON) setup.py build; \
 	fi
 	@echo "Build finished, invoke 'make install' to install."
         Resets the state of all bits in the Mask to 0.
       </desc>
     </method>
+    <method name="copy">
+      <call>copy () -> Mask</call>
+      <desc>Creates an exact copy of the Mask.</desc>
+    </method>
     <method name="connected_component">
       <call>connected_component ([x, y]) -> Mask
       connected_component ([point]) -> Mask</call>

src/base/basemod.c

         return 0;
     }
 
-    
     if (PyNumber_Check (obj))
     {
         if (!(intobj = PyNumber_Int (obj)))
     
     if (!(PyColor_Check(o1) && PyColor_Check (o2)))
     {
-		Py_INCREF (Py_NotImplemented);
-		return Py_NotImplemented;
-	}
+        Py_INCREF (Py_NotImplemented);
+        return Py_NotImplemented;
+    }
     c1 = (PyColor*) o1;
     c2 = (PyColor*) o2;
     
 static PyObject* _mask_connectedcomponents (PyObject* self, PyObject* args);
 static PyObject* _mask_getboundingrects (PyObject* self);
 static PyObject* _mask_convolve (PyObject* self, PyObject* args);
+static PyObject* _mask_copy (PyObject *self, PyObject *unused);
 
 static PyGetSetDef _mask_getsets[] = {
     { "size", _mask_getsize, NULL, DOC_MASK_MASK, NULL },
     { "get_bounding_rects",(PyCFunction) _mask_getboundingrects, METH_NOARGS,
       DOC_MASK_MASK_GET_BOUNDING_RECTS },
     { "convolve", _mask_convolve, METH_VARARGS, DOC_MASK_MASK_CONVOLVE },
+    { "copy", (PyCFunction)_mask_copy, METH_NOARGS, DOC_MASK_MASK_COPY },
+    { "__copy__", (PyCFunction)_mask_copy, METH_NOARGS, DOC_MASK_MASK_COPY },
+    { "__deepcopy__", (PyCFunction)_mask_copy, METH_O, DOC_MASK_MASK_COPY },
     { NULL, NULL, 0, NULL }
 };
 
 static int
 _mask_init (PyObject *mask, PyObject *args, PyObject *kwds)
 {
-    pgint32 w, h;
+    pgint32 w = 0, h = 0;
     bitmask_t *m;
 
     if (!PyArg_ParseTuple (args, "ii", &w, &h))
     return outmask;
 }
 
+static PyObject*
+_mask_copy (PyObject *self, PyObject *unused)
+{
+    PyMask *selfm = (PyMask*) self;
+    PyMask *mask = (PyMask*) PyMask_New (selfm->mask->w, selfm->mask->h);
+    if (!mask)
+        return NULL;
+
+    memcpy (mask->mask->bits, selfm->mask->bits,
+        selfm->mask->h * ((selfm->mask->w - 1)/BITMASK_W_LEN + 1) *
+        sizeof(BITMASK_W));
+
+    return (PyObject*) mask;
+}
+
 /* Connected component labeling based on the SAUF algorithm by Kesheng Wu,
    Ekow Otoo, and Kenji Suzuki.  The algorithm is best explained by their paper,
    "Two Strategies to Speed up Connected Component Labeling Algorithms", but in

test/mask_test.py

         m.clear ()
         self.assertEqual (m.count, 0)
 
+    def test_pygame2_mask_Mask_copy (self):
+        m = Mask (10, 10)
+        for x in range (0, 10, 2):
+            for y in range (0, 10, 2):
+                m.set_at (x, y)
+        self.assertEqual (m.count, 25)
+
+        m2 = m.copy ()
+        for x in range (0, 10, 2):
+            for y in range (0, 10, 2):
+                self.assertEqual (m2.get_at (x, y), 1)
+
     def test_pygame2_mask_Mask_convolve(self):
 
         # __doc__ (as of 2009-05-11) for pygame2.mask.Mask.convolve: