Index: trunk/psLib/src/imageops/psImageCovariance.c
===================================================================
--- trunk/psLib/src/imageops/psImageCovariance.c	(revision 21288)
+++ trunk/psLib/src/imageops/psImageCovariance.c	(revision 21291)
@@ -46,8 +46,7 @@
     // covariance matrix and another kernel.  This means that the resultant covariance matrix has twice the
     // size of the kernel plus the size of the input covariance matrix.
-
-    // Covariance matrix for output
-    psKernel *out = psKernelAlloc(2 * kernel->xMin + covar->xMin, 2 * kernel->xMax + covar->xMax,
-                                  2 * kernel->yMin + covar->yMin, 2 * kernel->yMax + covar->yMax);
+    int xMin = kernel->xMin - kernel->xMax + covar->xMin, xMax = kernel->xMax - kernel->xMin + covar->xMax;
+    int yMin = kernel->yMin - kernel->yMax + covar->yMin, yMax = kernel->yMax - kernel->yMin + covar->yMax;
+    psKernel *out = psKernelAlloc(xMin, xMax, yMin, yMax); // Covariance matrix for output
 
     // Need to go:
@@ -66,9 +65,9 @@
 
     double total = 0.0;                 // Total covariance
-    for (int y = out->yMin; y <= out->yMax; y++) {
+    for (int y = yMin; y <= yMax; y++) {
         // Range for v
         int vMin = PS_MAX(kernel->yMin + covar->yMin, y + kernel->yMin);
         int vMax = PS_MIN(kernel->yMax + covar->yMax, y + kernel->yMax);
-        for (int x = out->xMin; x <= out->xMax; x++) {
+        for (int x = xMin; x <= xMax; x++) {
             // Range for u
             int uMin = PS_MAX(kernel->xMin + covar->xMin, x + kernel->xMin);
