Index: trunk/psLib/src/imageops/psImageConvolve.c
===================================================================
--- trunk/psLib/src/imageops/psImageConvolve.c	(revision 7076)
+++ trunk/psLib/src/imageops/psImageConvolve.c	(revision 7077)
@@ -5,6 +5,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.33 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-05-05 22:32:35 $
+ *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-05-05 23:55:56 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -588,10 +588,8 @@
 
     // relevant terms
-    int Nrange = sigma*Nsigma + 0.5;
-    int Npixel = 2*Nrange + 1;
-    double factor = -0.5/(sigma*sigma);
-
-    int Nx = image->numCols;
-    int Ny = image->numRows;
+    int Nrange = sigma*Nsigma + 0.5;    // Number of pixels either side
+    int Npixel = 2*Nrange + 1;          // Total number of pixels in convolution kernel
+    int Nx = image->numCols;            // Number of columns
+    int Ny = image->numRows;            // Number of rows
 
     #define IMAGESMOOTH_CASE(TYPE) \
@@ -601,4 +599,5 @@
         gaussnorm->n = gaussnorm->nalloc; \
         double sum = 0.0; \
+        double factor = -0.5/(sigma*sigma); \
         for (int i = -Nrange; i < Nrange + 1; i++) { \
             gaussnorm->data.TYPE[i+Nrange] = exp (factor*i*i); \
@@ -617,6 +616,6 @@
             ps##TYPE *vo = temp->data.TYPE; \
             /* smooth first Nrange pixels, with renorm */ \
-            /* XXX need to check that this does not run over end for narrow images */ \
-            for (int i = 0; i < Nrange; i++, vi++, vo++) { \
+            int xMax = PS_MIN(Nrange, Nx); \
+            for (int i = 0; i < xMax; i++, vi++, vo++) { \
                 ps##TYPE *vr = vi - i; \
                 ps##TYPE *vg = gauss - i; \
@@ -641,5 +640,5 @@
             /* smooth last Nrange pixels, with renorm */ \
             /* XXX does this miss the last column? */ \
-            for (int i = Nx - Nrange; i < Nx; i++, vi++, vo++) { \
+            for (int i = PS_MAX(Nx - Nrange, 0); i < Nx; i++, vi++, vo++) { \
                 ps##TYPE *vr = vi - Nrange; \
                 ps##TYPE *vg = gauss - Nrange; \
@@ -659,5 +658,6 @@
         psArray *temprows = psArrayAlloc (Nrange); \
         temprows->n = Nrange; \
-        for (int j = 0; j < Nrange; j++) { \
+        int yMax = PS_MIN(Nrange, Ny); \
+        for (int j = 0; j < yMax; j++) { \
             temp = psVectorAlloc (Nx, PS_TYPE_##TYPE); \
             temp->n = temp->nalloc; \
@@ -691,5 +691,5 @@
             } \
         } \
-        for (int j = Ny - Nrange; j < Ny; j++) { \
+        for (int j = PS_MAX(Ny - Nrange, 0); j < Ny; j++) { \
             /* save the Nrange-offset output row, then zero */ \
             int Nr = j % Nrange; \
