[Bugfix] Make sure the video selection is always a multiple of 16x8 pixels. Previously, the selection could cross the framebuffer boundaries and clipping it back to screen could break 16x8 alignment.

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@2746 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/java/src/com/tightvnc/vncviewer/VncCanvas.java b/java/src/com/tightvnc/vncviewer/VncCanvas.java
index 332efa3..5422137 100644
--- a/java/src/com/tightvnc/vncviewer/VncCanvas.java
+++ b/java/src/com/tightvnc/vncviewer/VncCanvas.java
@@ -2015,9 +2015,18 @@
       w = (w * 100 + scalingFactor/2) / scalingFactor;
       h = (h * 100 + scalingFactor/2) / scalingFactor;
     }
+    // Clip the selection to framebuffer.
+    if (x < 0)
+      x = 0;
+    if (y < 0)
+      y = 0;
+    if (x + w > rfb.framebufferWidth)
+      w = rfb.framebufferWidth - x;
+    if (y + h > rfb.framebufferHeight)
+      h = rfb.framebufferHeight - y;
     // Make width a multiple of 16.
     int widthCorrection = w % 16;
-    if (widthCorrection >= 8) {
+    if (widthCorrection >= 8 && x + (w / 16 + 1) * 16 <= rfb.framebufferWidth) {
       widthCorrection -= 16;
     }
     w -= widthCorrection;
@@ -2026,7 +2035,7 @@
     }
     // Make height a multiple of 8.
     int heightCorrection = h % 8;
-    if (heightCorrection >= 4) {
+    if (heightCorrection >= 4 && y + (h / 8 + 1) * 8 <= rfb.framebufferHeight) {
       heightCorrection -= 8;
     }
     h -= heightCorrection;
@@ -2034,20 +2043,14 @@
       y += heightCorrection;
     }
     // Translate the selection back to screen coordinates if requested.
-    int clipWidth = rfb.framebufferWidth;
-    int clipHeight = rfb.framebufferHeight;
     if (useScreenCoords && rfb.framebufferWidth != scaledWidth) {
       x = (x * scalingFactor + 50) / 100;
       y = (y * scalingFactor + 50) / 100;
       w = (w * scalingFactor + 50) / 100;
       h = (h * scalingFactor + 50) / 100;
-      clipWidth = scaledWidth;
-      clipHeight = scaledHeight;
     }
     // Clip the selection to screen/framebuffer and return the result.
-    Rectangle selection = new Rectangle(x, y, w, h);
-    Rectangle clip = new Rectangle(0, 0, clipWidth, clipHeight);
-    return selection.intersection(clip);
+    return new Rectangle(x, y, w, h);
   }
 
   /**