Index: gimp/app/paint-funcs/scale-funcs.c
===================================================================
RCS file: /cvs/gnome/gimp/app/paint-funcs/scale-funcs.c,v
retrieving revision 1.9
diff -u -r1.9 scale-funcs.c
--- gimp/app/paint-funcs/scale-funcs.c	6 Sep 2006 15:29:45 -0000	1.9
+++ gimp/app/paint-funcs/scale-funcs.c	20 Sep 2006 09:16:50 -0000
@@ -661,7 +661,7 @@
   gint           i, j;
   gint           b;
   gint           frac;
-  gint           advance_dest;
+  gint           adsrc_yance_dest;
 
 #if 0
   g_printerr ("subsample_region: (%d x %d) -> (%d x %d)\n",
@@ -721,13 +721,13 @@
         {
           y_cum += y_rat;
           y_frac = y_cum - y_last;
-          advance_dest = TRUE;
+          adsrc_yance_dest = TRUE;
         }
       else
         {
           src_row ++;
           y_frac = src_row - y_last;
-          advance_dest = FALSE;
+          adsrc_yance_dest = FALSE;
         }
 
       y_last += y_frac;
@@ -761,7 +761,7 @@
             }
         }
 
-      if (advance_dest)
+      if (adsrc_yance_dest)
         {
           tot_frac = 1.0 / (x_rat * y_rat);
 
@@ -816,7 +816,7 @@
 
 static inline gdouble
 lanczos_sum (guchar         *ptr,
-             const gdouble  *lu,
+             const gdouble  *kernel,  /* 1-D kernel of transform coeffs */
              gint            u,
              gint            bytes,
              gint            byte)
@@ -825,14 +825,14 @@
   gint    i;
 
   for (i = 0; i < LANCZOS_WIDTH2 ; i++)
-    sum += lu[i] * ptr[ (u + i - LANCZOS_WIDTH) * bytes + byte ];
+    sum += kernel[i] * ptr[ (u + i - LANCZOS_WIDTH) * bytes + byte ];
 
   return sum;
 }
 
 static inline gdouble
 lanczos_sum_mul (guchar         *ptr,
-                 const gdouble  *lu,
+                 const gdouble  *kernel,
                  gint            u,
                  gint            bytes,
                  gint            byte,
@@ -842,7 +842,7 @@
   gint    i;
 
   for (i = 0; i < LANCZOS_WIDTH2 ; i++ )
-    sum += lu[i] * ptr[ (u + i - LANCZOS_WIDTH) * bytes + byte ]
+    sum += kernel[i] * ptr[ (u + i - LANCZOS_WIDTH) * bytes + byte ]
                  * ptr[ (u + i - LANCZOS_WIDTH) * bytes + alpha];
 
   return sum;
@@ -867,23 +867,24 @@
   return TRUE;
 }
 
-static gdouble *
-kernel_lanczos (void)
+
+gdouble *
+create_lanczos_lookup (void)
 {
   const gdouble dx = (gdouble) LANCZOS_WIDTH / (gdouble) (LANCZOS_SAMPLES - 1);
 
-  gdouble *kernel = g_new (gdouble, LANCZOS_SAMPLES);
+  gdouble *lanczos = g_new (gdouble, LANCZOS_SAMPLES);
   gdouble  x      = 0.0;
   gint     i;
 
   for (i = 0; i < LANCZOS_SAMPLES; i++)
     {
-      kernel[i] = ((ABS (x) < LANCZOS_WIDTH) ?
+      lanczos[i] = ((ABS (x) < LANCZOS_WIDTH) ?
                    (sinc (x) * sinc (x / LANCZOS_WIDTH)) : 0.0);
       x += dx;
     }
 
-  return kernel;
+  return lanczos;
 }
 
 static void
@@ -893,24 +894,23 @@
                       gpointer               progress_data)
 
 {
-  gdouble       *kernel  = NULL;        /* Lanczos kernel                    */
-  gdouble        lu[LANCZOS_WIDTH2],    /* Lanczos sample value              */
-                 lv[LANCZOS_WIDTH2];    /* Lanczos sample value              */
-  gdouble        lusum, lvsum, weight;  /* Lanczos weighting vars            */
+  gdouble       *lanczos  = NULL;             /* Lanczos lookup table                */
+  gdouble        x_kernel[LANCZOS_WIDTH2],    /* 1-D kernels of Lanczos window coeffs */
+                 y_kernel[LANCZOS_WIDTH2];
+  gdouble        x_sum, y_sum;                /* sum of Lanczos weights              */
 
-  gdouble        newval;                /* new interpolated RGB value */
+  gdouble        newval;                      /* new interpolated RGB value          */
 
-  guchar        *win_buf = NULL;        /* Sliding window buffer             */
-  guchar        *win_ptr[LANCZOS_WIDTH2];
-                                        /* Ponters to sliding window rows    */
+  guchar        *win_buf = NULL;              /* Sliding window buffer               */
+  guchar        *win_ptr[LANCZOS_WIDTH2];     /* Ponters to sliding window rows      */
 
-  guchar        *dst_buf = NULL;        /* Pointer to destination image data */
+  guchar        *dst_buf = NULL;              /* Pointer to destination image data   */
 
-  gint           x, y;                  /* Position in destination image     */
-  gint           i, byte;            /* loop vars to fill source window   */
+  gint           x, y;                        /* Position in destination image       */
+  gint           i, byte;                     /* loop vars  */
   gint           row;
 
-  gdouble        trans[6], itrans[6];   /* Scale transformations             */
+  gdouble        trans[6], itrans[6];         /* Scale transformations               */
 
   const gint     dst_width     = dstPR->w;
   const gint     dst_height    = dstPR->h;
@@ -918,18 +918,18 @@
   const gint     src_width     = srcPR->w;
   const gint     src_height    = srcPR->h;
 
-  const gint     src_rowstride = src_width * bytes;
-  const gint     dst_rowstride = dst_width * bytes;
-  const gint     win_rowstride = (src_width + LANCZOS_WIDTH2) * bytes;
+  const gint     src_row_span = src_width * bytes;
+  const gint     dst_row_span = dst_width * bytes;
+  const gint     win_row_span = (src_width + LANCZOS_WIDTH2) * bytes;
 
-  const gdouble  sx            = (gdouble) dst_width / (gdouble) src_width;
-  const gdouble  sy            = (gdouble) dst_height / (gdouble) src_height;
+  const gdouble  scale_x            = (gdouble) dst_width / (gdouble) src_width;
+  const gdouble  scale_y            = (gdouble) dst_height / (gdouble) src_height;
 
   for (i = 0; i < 6; i++)
     trans[i] = 0.0;
 
-  trans[0] = sx;
-  trans[4] = sy;
+  trans[0] = scale_x;
+  trans[4] = scale_y;
 
   if (! inv_lin_trans (trans, itrans))
     {
@@ -938,7 +938,7 @@
     }
 
   /* allocate buffer for destination row */
-  dst_buf = g_new0 (guchar, dst_rowstride);
+  dst_buf = g_new0 (guchar, dst_row_span);
   /* if no scaling needed copy data */
   if ( dst_width == src_width && dst_height == src_height )
     {
@@ -951,15 +951,15 @@
        return;
     }
 
-  /* Calculate kernel */
-  kernel = kernel_lanczos ();
+  /* allocate and fill lanczos lookup table */
+  lanczos = create_lanczos_lookup ();
 
   /* allocate buffer for source rows */
-  win_buf = g_new0 (guchar, win_rowstride * LANCZOS_WIDTH2);
+  win_buf = g_new0 (guchar, win_row_span * LANCZOS_WIDTH2);
 
   /* Set the window pointers */
   for ( i = 0 ; i < LANCZOS_WIDTH2 ; i++ )
-    win_ptr[i] = win_buf + ( win_rowstride * i ) + LANCZOS_WIDTH * bytes;
+    win_ptr[i] = win_buf + ( win_row_span * i ) + LANCZOS_WIDTH * bytes;
 
   /* fill the data for the first loop */
   for ( i = 0 ; i <= LANCZOS_WIDTH && i < src_height ; i++)
@@ -973,55 +973,61 @@
       pixel_region_get_row (dstPR, 0, y, dst_width, dst_buf, 1);
       for  (x = 0; x < dst_width; x++)
         {
-          gdouble  du ,dv;   /* Transformed position in source image */
-          gint     u, v;     /* Position in source image */
-          gint     su, sv;   /* Lanczos kernel position  */
+          gdouble  dsrc_x ,dsrc_y;   /* corresponding scaled position in source image */
+          gint     src_x, src_y;     /* int coordinates in source image */
+          gint     x_shift, y_shift; /* index into Lanczos lookup  */
           /*
              Use linear trans. for determining source coordinates from
              destination. Coefficient -0.5 fixes the offset error.
            */
-          du = itrans[0] * ((gdouble) x) + itrans[1] * ((gdouble) y) + itrans[2] - 0.5;
-          dv = itrans[3] * ((gdouble) x) + itrans[4] * ((gdouble) y) + itrans[5] - 0.5;
+          dsrc_x = itrans[0] * ((gdouble) x) + itrans[1] * ((gdouble) y) + itrans[2] - 0.5;
+          dsrc_y = itrans[3] * ((gdouble) x) + itrans[4] * ((gdouble) y) + itrans[5] - 0.5;
 
           /* Coordinates in source image */
-          u = (gint) du;
-          v = (gint) dv;
+          src_x = (gint) dsrc_x;
+          src_y = (gint) dsrc_y;
 
           /* get weight for fractional error */
-          su = (gint) ((du - u) * LANCZOS_SPP);
-          sv = (gint) ((dv - v) * LANCZOS_SPP);
+          x_shift = (gint) ((dsrc_x - src_x) * LANCZOS_SPP);
+          y_shift = (gint) ((dsrc_y - src_y) * LANCZOS_SPP);
 
-          /* Fill multipliers in lu[] and lv[]
+          /* Fill x_kernel[] and y_kernel[] with lanczos coeffs
            *
-           *  kernel = Is a lookup table that contains half of the sinc func.
+           *  lanczos = Is a lookup table that contains half of the sinc func.
            *
-           *  su, sv = shift from kernel center due to fractional part
+           *  x_shift, y_shift = shift from kernel center due to fractional part
            *           of interpollation
            *
-           *  The for loop creates 2 1D kernels for convolution.
+           *  The for loop creates two 1-D kernels for convolution.
            *    - If the center position +/- LANCZOS_WIDTH is out of
            *      the source image coordinates set the value to 0.0
            *    - If the kernel index is out of range set value to 0.0
            *      ( caused by offset coef.)
            */
-          lusum = lvsum = 0.0;
+          x_sum = y_sum = 0.0;
 
           for (i = LANCZOS_WIDTH; i >= -LANCZOS_WIDTH; i--)
             {
               gint pos = i * LANCZOS_SPP;
 
-              if ( u + i >= 0 && u + i < src_width)
-                lusum += lu[LANCZOS_WIDTH + i] = kernel[ABS (su - pos)];
+              if ( src_x + i >= 0 && src_x + i < src_width)
+                x_sum += x_kernel[LANCZOS_WIDTH + i] = lanczos[ABS (x_shift - pos)];
               else
-                lusum += lu[LANCZOS_WIDTH + i] = 0.0;
+                x_kernel[LANCZOS_WIDTH + i] = 0.0;
 
-              if ( v + i >= 0 && v + i < src_height)
-                lvsum += lv[LANCZOS_WIDTH + i] = kernel[ABS (sv - pos)];
+              if ( src_y + i >= 0 && src_y + i < src_height)
+                y_sum += y_kernel[LANCZOS_WIDTH + i] = lanczos[ABS (y_shift - pos)];
               else
-                lvsum += lv[LANCZOS_WIDTH + i] = 0.0;
+                y_kernel[LANCZOS_WIDTH + i] = 0.0;
             }
 
-          weight = lvsum*lusum;
+          /* normalise the wieghted arrays */
+          for (i = -LANCZOS_WIDTH; i <= LANCZOS_WIDTH; i++)
+          {
+             x_kernel[LANCZOS_WIDTH +i] /= x_sum;
+             y_kernel[LANCZOS_WIDTH +i] /= y_sum;
+          }
+
           /*
             Scaling up
             New determined source row is > than last read row
@@ -1029,7 +1035,7 @@
             If no more source rows are available fill buffer with 0
             ( Probably not necessary because multipliers should be 0).
           */
-          for ( ; row < v ; )
+          for ( ; row < src_y ; )
             {
               row++;
               rotate_pointers (win_ptr, LANCZOS_WIDTH2);
@@ -1039,12 +1045,12 @@
                                       win_ptr[LANCZOS_WIDTH2 - 1], 1);
               else
                 memset (win_ptr[LANCZOS_WIDTH2 - 1], 0,
-                        sizeof (guchar) * src_rowstride);
+                        sizeof (guchar) * src_row_span);
             }
            /*
               Scaling down
             */
-          for ( ; row > v ; )
+          for ( ; row > src_y ; )
             {
               row--;
               for ( i = 0 ; i < LANCZOS_WIDTH2 - 1 ; i++ )
@@ -1055,7 +1061,7 @@
                                       win_ptr[0], 1);
               else
                 memset (win_ptr[0], 0,
-                        sizeof (guchar) * src_rowstride);
+                        sizeof (guchar) * src_row_span);
 
             }
 
@@ -1069,10 +1075,8 @@
 
                aval = 0.0;
                for (i = 0; i < LANCZOS_WIDTH2 ; i++ )
-                 aval += lv[i] * lanczos_sum (win_ptr[i], lu,
-                                                u, bytes, alpha);
-               /* calculate alpha of result */
-               aval /= weight;
+                 aval += y_kernel[i] * lanczos_sum (win_ptr[i], x_kernel,
+                                                src_x, bytes, alpha);
 
                if (aval <= 0.0)
                  {
@@ -1094,8 +1098,8 @@
                  {
                    newval = 0.0;
                    for (i = 0; i < LANCZOS_WIDTH2; i++ )
-                     newval += lv[i] * lanczos_sum_mul (win_ptr[i], lu,
-                                                        u, bytes, byte, alpha);
+                     newval += y_kernel[i] * lanczos_sum_mul (win_ptr[i], x_kernel,
+                                                        src_x, bytes, byte, alpha);
                    newval *= arecip;
                    dst_buf[x * bytes + byte] = CLAMP (newval, 0, 255);
                  }
@@ -1107,9 +1111,8 @@
                    /* Calculate new value */
                    newval = 0.0;
                    for (i = 0; i < LANCZOS_WIDTH2; i++ )
-                     newval += lv[i] * lanczos_sum (win_ptr[i], lu,
-                                                    u, bytes, byte);
-                   newval /= weight;
+                     newval += y_kernel[i] * lanczos_sum (win_ptr[i], x_kernel,
+                                                    src_x, bytes, byte);
                    dst_buf[x * bytes + byte] = CLAMP ((gint) newval, 0, 255);
                  }
              }
@@ -1120,6 +1123,6 @@
 
   g_free (dst_buf);
   g_free (win_buf);
-  g_free (kernel);
+  g_free (lanczos);
 }
 
Index: gimp/app/paint-funcs/scale-funcs.h
===================================================================
RCS file: /cvs/gnome/gimp/app/paint-funcs/scale-funcs.h,v
retrieving revision 1.3
diff -u -r1.3 scale-funcs.h
--- gimp/app/paint-funcs/scale-funcs.h	6 Sep 2006 15:29:45 -0000	1.3
+++ gimp/app/paint-funcs/scale-funcs.h	20 Sep 2006 09:16:50 -0000
@@ -33,6 +33,7 @@
                     GimpProgressFunc       progress_callback,
                     gpointer               progress_data);
 
+gdouble *   create_lanczos_lookup (void);
 
 #endif  /*  __SCALE_FUNCS_H__  */
 
