r4960 broke support for pixel formats with depth < 24. This corrects that and also forces a full framebuffer update whenever the format is changed.

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@4986 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/java/com/tigervnc/vncviewer/BIPixelBuffer.java b/java/com/tigervnc/vncviewer/BIPixelBuffer.java
index 88c9396..6690c79 100644
--- a/java/com/tigervnc/vncviewer/BIPixelBuffer.java
+++ b/java/com/tigervnc/vncviewer/BIPixelBuffer.java
@@ -34,30 +34,33 @@
 
   public void setPF(PixelFormat pf) {
     super.setPF(pf);
+    createImage(width(), height());
   }
 
   public void updateColourMap() {
     cm = new IndexColorModel(8, nColours, reds, greens, blues);
+    createImage(width_, height_);
   }
   
   // resize() resizes the image, preserving the image data where possible.
   public void resize(int w, int h) {
-    if (w == width() && h == height()) return;
+    if (w == width() && h == height())
+      return;
 
     width_ = w;
     height_ = h;
-    GraphicsEnvironment ge =
-      GraphicsEnvironment.getLocalGraphicsEnvironment();
-    GraphicsDevice gd = ge.getDefaultScreenDevice();
-    GraphicsConfiguration gc = gd.getDefaultConfiguration();
-    image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
-    image.setAccelerationPriority(1);
-    image.createGraphics();
-    WritableRaster wr = image.getRaster();
-    SinglePixelPackedSampleModel sm =
-      (SinglePixelPackedSampleModel)image.getSampleModel();
-    DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
-    data = db.getData();
+    createImage(w, h);
+  }
+
+  private void createImage(int w, int h) {
+    if (w == 0 || h == 0) return;
+    WritableRaster wr;
+    if (cm instanceof IndexColorModel)
+      wr = ((IndexColorModel)cm).createCompatibleWritableRaster(w, h);
+    else
+      wr = ((DirectColorModel)cm).createCompatibleWritableRaster(w, h);
+    image = new BufferedImage(cm, wr, true, null);
+    db = wr.getDataBuffer();
   }
 
   public void fillRect(int x, int y, int w, int h, int pix) {
@@ -92,8 +95,13 @@
       clip = null;
       img.flush();
     } else {
-      for (int j = 0; j < h; j++)
-        System.arraycopy(pix, (w*j), data, width_ * (y + j) + x, w);
+      if (image.getSampleModel().getTransferType() == DataBuffer.TYPE_BYTE) {
+        byte[] bytes = new byte[((int[])pix).length];
+        for (int i = 0; i < bytes.length; i++)
+          bytes[i] = (byte)((int[])pix)[i];
+        pix = bytes;
+      }
+      image.getSampleModel().setDataElements(x, y, w, h, pix, db); 
     }
   }
 
@@ -126,6 +134,7 @@
   }
 
   BufferedImage image;
+  DataBuffer db;
   Rectangle clip;
 
   static LogWriter vlog = new LogWriter("BIPixelBuffer");
diff --git a/java/com/tigervnc/vncviewer/CConn.java b/java/com/tigervnc/vncviewer/CConn.java
index 0ad6906..498b4c8 100644
--- a/java/com/tigervnc/vncviewer/CConn.java
+++ b/java/com/tigervnc/vncviewer/CConn.java
@@ -625,6 +625,7 @@
   	            (newFullColour ? "enabled" : "disabled"));
       fullColour = newFullColour;
       formatChange = true;
+      forceNonincremental = true;
     } 
   }
 
@@ -680,7 +681,7 @@
     if (forceNonincremental || !continuousUpdates) {
       pendingUpdate = true;
       writer().writeFramebufferUpdateRequest(new Rect(0,0,cp.width,cp.height),
-                                                 !formatChange && !forceNonincremental);
+                                                 !forceNonincremental);
     }
 
     forceNonincremental = false;
@@ -921,8 +922,10 @@
 
   public void getOptions() {
     autoSelect = options.autoSelect.isSelected();
-    if (fullColour != options.fullColour.isSelected())
+    if (fullColour != options.fullColour.isSelected()) {
       formatChange = true;
+      forceNonincremental = true;
+    }
     fullColour = options.fullColour.isSelected();
     if (!fullColour) {
       int newLowColourLevel = (options.veryLowColour.isSelected() ? 0 :
@@ -930,6 +933,7 @@
       if (newLowColourLevel != lowColourLevel) {
         lowColourLevel = newLowColourLevel;
         formatChange = true;
+        forceNonincremental = true;
       }
     }
     int newEncoding = (options.zrle.isSelected() ?  Encodings.encodingZRLE :