Fixed the bug with very low quality of the remote desktop
scaling.

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@2165 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/common/rfb/ScaleFilters.cxx b/common/rfb/ScaleFilters.cxx
index e90e1e3..cd08f67 100644
--- a/common/rfb/ScaleFilters.cxx
+++ b/common/rfb/ScaleFilters.cxx
@@ -87,9 +87,10 @@
   return filter;

 }

 

-void ScaleFilters::makeWeightTabs(int filter_id, int src_x, int dst_x, SFilterWeightTab **pWeightTabs) {

-  double sx;

-  double ratio = double(dst_x) / src_x;

+void ScaleFilters::makeWeightTabs(int filter_id, int src_x, int dst_x, double ratio, SFilterWeightTab **pWeightTabs) {

+  double sxc;

+  double offset = 0.5;

+

   SFilter sFilter = filters[filter_id];

   

   *pWeightTabs = new SFilterWeightTab[dst_x];

@@ -97,17 +98,18 @@
 

   // Make the weight tab for the each dest x position

   for (int x = 0; x < dst_x; x++) {

-    sx = double(x) / ratio;

+    sxc = (double(x)+offset) / ratio;

 

     // Calculate the scale filter interval, [i0, i1)

-    int i0 = int(__rfbmax(ceil(sx-sFilter.radius), 0));

-    int i1 = int(__rfbmin(ceil(sx+sFilter.radius), src_x));

+    int i0 = int(__rfbmax(sxc-sFilter.radius+0.5, 0));

+    int i1 = int(__rfbmin(sxc+sFilter.radius+0.5, src_x));

+

     weightTabs[x].i0 = i0; weightTabs[x].i1 = i1;

-    weightTabs[x].weight = new float[i1-i0];

+    weightTabs[x].weight = new double[i1-i0];

 

     // Calculate the weight coeffs on the scale filter interval

     for (int ci = 0, i = i0; i < i1; i++) {

-      weightTabs[x].weight[ci++] = (float)sFilter.func(float(i)-sx);

+      weightTabs[x].weight[ci++] = (double)sFilter.func(double(i)-sxc+0.5);

     }

   }

 }

diff --git a/common/rfb/ScaleFilters.h b/common/rfb/ScaleFilters.h
index c2555a7..75b1e7f 100644
--- a/common/rfb/ScaleFilters.h
+++ b/common/rfb/ScaleFilters.h
@@ -49,7 +49,7 @@
   // Scale filter weight table

   typedef struct {

     short int i0, i1;  // Filter function interval, [i0..i1)

-    float *weight;     // Weight coefficients on the filter function interval

+    double *weight;    // Weight coefficients on the filter function interval

   } SFilterWeightTab;

 

 

@@ -60,7 +60,7 @@
 

     SFilter &operator[](unsigned int filter_id);

 

-    void makeWeightTabs(int filter, int src_x, int dst_x, SFilterWeightTab **weightTabs);

+    void makeWeightTabs(int filter, int src_x, int dst_x, double ratio, SFilterWeightTab **weightTabs);

 

   protected:

     void initFilters();

diff --git a/common/rfb/ScaledPixelBuffer.cxx b/common/rfb/ScaledPixelBuffer.cxx
index c38a254..b5b3ea3 100644
--- a/common/rfb/ScaledPixelBuffer.cxx
+++ b/common/rfb/ScaledPixelBuffer.cxx
@@ -67,8 +67,8 @@
   src_width  = w;
   src_height = h;
   calculateScaledBufferSize();
-  scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
-  scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
+  scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, scale_ratio, &xWeightTabs);
+  scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, scale_ratio, &yWeightTabs);
 }
 
 void ScaledPixelBuffer::setPF(const PixelFormat &pf_) {
@@ -81,8 +81,8 @@
     freeWeightTabs();
     scale_ratio = scale_ratio_;
     calculateScaledBufferSize();
-    scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
-    scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
+    scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, scale_ratio, &xWeightTabs);
+    scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, scale_ratio, &yWeightTabs);
   }
 }
 
@@ -111,8 +111,8 @@
 
 void ScaledPixelBuffer::scaleRect(const Rect& rect) {
   Rect changed_rect;
-  U8 *ptr, *pxs, *px;
-  float rx, gx, bx, red, green, blue, *xweight, *yweight, xWeight, yWeight;
+  U8 *ptr;
+  double rx, gx, bx, red, green, blue, *xweight, *yweight, xWeight, yWeight;
   int r, g, b, xwi, ywi;
 
   // Calculate the changed pixel rect in the scaled image
@@ -126,7 +126,6 @@
 
     for (int x = changed_rect.tl.x; x < changed_rect.br.x; x++) {
       ywi = 0; red = 0; green = 0; blue = 0;
-      pxs = &(*src_data)[(xWeightTabs[x].i0 + yWeightTabs[y].i0*src_width) * bytesPerPixel];
       xweight = xWeightTabs[x].weight;
     
       // Calculate the scaled pixel value at (x, y) coordinates by
@@ -136,14 +135,13 @@
       // [(xWeight.i0,yWeight.i1-1)..(xWeight.i1-1,yWeight.i1-1)],
       // where [i0, i1) is the scaled filter interval.
       for (int ys = yWeightTabs[y].i0; ys < yWeightTabs[y].i1; ys++) {
-        xwi = 0; rx = 0; gx = 0; bx = 0; px = pxs;
+        xwi = 0; rx = 0; gx = 0; bx = 0;
         for (int xs = xWeightTabs[x].i0; xs < xWeightTabs[x].i1; xs++) {
-          rgbFromPixel(*(U32*)(px), r, g, b);
+          rgbFromPixel(getSourcePixel(xs, ys), r, g, b);
           xWeight = xweight[xwi++];
           rx += r * xWeight;
           gx += g * xWeight;
           bx += b * xWeight;
-          px += bytesPerPixel;
         }
         yWeight = yweight[ywi++];
         red += rx * yWeight;
diff --git a/win/rfb_win32/ScaledDIBSectionBuffer.cxx b/win/rfb_win32/ScaledDIBSectionBuffer.cxx
index aa52e07..1a287c0 100644
--- a/win/rfb_win32/ScaledDIBSectionBuffer.cxx
+++ b/win/rfb_win32/ScaledDIBSectionBuffer.cxx
@@ -50,8 +50,8 @@
   // Calculate the scale weight tabs must be in the ScalePixelBuffer class
   freeWeightTabs();
   calculateScaledBufferSize();
-  scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
-  scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
+  scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, scale_ratio, &xWeightTabs);
+  scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, scale_ratio, &yWeightTabs);
 
   recreateBuffers();
 }
@@ -86,8 +86,8 @@
   // Calculate the scale weight tabs must be in the ScalePixelBuffer class
   freeWeightTabs();
   calculateScaledBufferSize();
-  scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, &xWeightTabs);
-  scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, &yWeightTabs);
+  scaleFilters.makeWeightTabs(scaleFilterID, src_width, scaled_width, scale_ratio, &xWeightTabs);
+  scaleFilters.makeWeightTabs(scaleFilterID, src_height, scaled_height, scale_ratio, &yWeightTabs);
 
   recreateBuffers();
 }