Index: trunk/psLib/src/imageops/psImageConvolve.c
===================================================================
--- trunk/psLib/src/imageops/psImageConvolve.c	(revision 12186)
+++ trunk/psLib/src/imageops/psImageConvolve.c	(revision 12187)
@@ -7,6 +7,6 @@
 /// @author Eugene Magnier, IfA
 ///
-/// @version $Revision: 1.49 $ $Name: not supported by cvs2svn $
-/// @date $Date: 2007-03-02 21:48:12 $
+/// @version $Revision: 1.50 $ $Name: not supported by cvs2svn $
+/// @date $Date: 2007-03-02 22:19:21 $
 ///
 /// Copyright 2004-2007 Institute for Astronomy, University of Hawaii
@@ -274,5 +274,5 @@
 }
 
-psImage *psImageMaskConvolve(const psImage *mask, psMaskType maskVal, const psKernel *kernel)
+psImage *psImageConvolveMask(const psImage *mask, psMaskType maskVal, const psKernel *kernel)
 {
     PS_ASSERT_IMAGE_NON_NULL(mask, NULL);
@@ -290,21 +290,30 @@
     int numCols = mask->numCols;        // Number of columns
 
+    psImage *out = psImageAlloc(numCols, numRows, PS_TYPE_MASK); // Output mask
+
+    // Dereference mask images
     psMaskType **maskData = mask->data.PS_TYPE_MASK_DATA;
-    psImage *out = psImageAlloc(numCols, numRows, PS_TYPE_MASK);
-    psImageInit(out, 0);
-
-    for (int kRow = yMin; kRow <= yMax; kRow++) {
-        for (int kCol = xMin; kCol <= xMax; kCol++) {
-            psMaskType kValue = kernel->kernel[kRow][kCol]; // Value of the kernel
-            if (kValue == 0) {
+    psMaskType **outData = out->data.PS_TYPE_MASK_DATA;
+
+    // Propagate the non-masked values
+    for (int row = 0; row < numRows; row++) {
+        for (int col = 0; col < numCols; col++) {
+            outData[row][col] = maskData[row][col] & ~maskVal;
+        }
+    }
+
+    for (int row = 0; row < numRows; row++) {
+        for (int col = 0; col < numCols; col++) {
+            if (maskData[row][col] & maskVal) {
+                // It's already masked --- move on
                 continue;
             }
-            for (int row = PS_MAX(kRow, 0); row < PS_MIN(numRows, numRows - kRow); row++) {
-                for (int col = PS_MAX(kCol, 0); col < PS_MIN(numCols, numCols - kCol); col++) {
-                    if (maskData[row][col] & maskVal) {
-                        out->data.PS_TYPE_MASK_DATA[row + kRow][col + kCol] = maskVal;
-                    }
+            psMaskType pixel = 0;       // New pixel value
+            for (int kRow = PS_MAX(yMin, -row); kRow <= PS_MIN(yMax, numRows - row - 1); kRow++) {
+                for (int kCol = PS_MAX(xMin, -col); kCol <= PS_MIN(xMax, numCols - col - 1); kCol++) {
+                    pixel |= maskData[row + kRow][col + kCol] & maskVal;
                 }
             }
+            outData[row][col] |= pixel;
         }
     }
