Fix some offenders that poke around in the PixelFormat internals
diff --git a/win/rfb_win32/DIBSectionBuffer.cxx b/win/rfb_win32/DIBSectionBuffer.cxx
index 3c2f509..0c266b0 100644
--- a/win/rfb_win32/DIBSectionBuffer.cxx
+++ b/win/rfb_win32/DIBSectionBuffer.cxx
@@ -1,4 +1,5 @@
 /* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
+ * Copyright 2014 Pierre Ossman for Cendio AB
  * 
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -55,9 +56,11 @@
   if ((pf.bpp <= 8) && pf.trueColour) {
     vlog.info("creating %d-bit TrueColour palette", pf.depth);
     for (int i=0; i < (1<<(pf.depth)); i++) {
-      palette[i].b = ((((i >> pf.blueShift) & pf.blueMax) * 65535) + pf.blueMax/2) / pf.blueMax;
-      palette[i].g = ((((i >> pf.greenShift) & pf.greenMax) * 65535) + pf.greenMax/2) / pf.greenMax;
-      palette[i].r = ((((i >> pf.redShift) & pf.redMax) * 65535) + pf.redMax/2) / pf.redMax;
+      rdr::U16 r, g, b;
+      pf.rgbFromPixel(i, NULL, &r, &g, &b);
+      palette[i].r = r;
+      palette[i].g = g;
+      palette[i].b = b;
     }
     refreshPalette();
   }
@@ -110,9 +113,9 @@
     bi.bmiHeader.biWidth = width_;
     bi.bmiHeader.biHeight = -height_;
     bi.bmiHeader.biCompression = (format.bpp > 8) ? BI_BITFIELDS : BI_RGB;
-    bi.mask.red = format.redMax << format.redShift;
-    bi.mask.green = format.greenMax << format.greenShift;
-    bi.mask.blue = format.blueMax << format.blueShift;
+    bi.mask.red = format.pixelFromRGB((rdr::U16)~0, 0, 0);
+    bi.mask.green = format.pixelFromRGB(0, (rdr::U16)~0, 0);
+    bi.mask.blue = format.pixelFromRGB(0, 0, (rdr::U16)~0);
 
     // Create a DIBSection to draw into
     if (device)
@@ -160,6 +163,11 @@
   }
 
   if (new_bitmap) {
+    int bpp, depth;
+    bool trueColour;
+    int redMax, greenMax, blueMax;
+    int redShift, greenShift, blueShift;
+
     // Set up the new bitmap
     bitmap = new_bitmap;
     data = new_data;
@@ -180,30 +188,34 @@
     }
 
     // Calculate the PixelFormat for the DIB
-    format.bigEndian = 0;
-    format.bpp = format.depth = ds.dsBm.bmBitsPixel;
-    format.trueColour = format.trueColour || format.bpp > 8;
-    if (format.bpp > 8) {
+    bpp = depth = ds.dsBm.bmBitsPixel;
+    trueColour = format.trueColour || format.bpp > 8;
 
+    if (trueColour) {
       // Get the truecolour format used by the DIBSection
-      initMaxAndShift(ds.dsBitfields[0], &format.redMax, &format.redShift);
-      initMaxAndShift(ds.dsBitfields[1], &format.greenMax, &format.greenShift);
-      initMaxAndShift(ds.dsBitfields[2], &format.blueMax, &format.blueShift);
+      initMaxAndShift(ds.dsBitfields[0], &redMax, &redShift);
+      initMaxAndShift(ds.dsBitfields[1], &greenMax, &greenShift);
+      initMaxAndShift(ds.dsBitfields[2], &blueMax, &blueShift);
 
       // Calculate the effective depth
-      format.depth = 0;
+      depth = 0;
       Pixel bits = ds.dsBitfields[0] | ds.dsBitfields[1] | ds.dsBitfields[2];
       while (bits) {
-        format.depth++;
+        depth++;
         bits = bits >> 1;
       }
-      if (format.depth > format.bpp)
+      if (depth > bpp)
         throw Exception("Bad DIBSection format (depth exceeds bpp)");
-    } else {
+    }
+
+    format = PixelFormat(bpp, depth, false, trueColour,
+                         redMax, greenMax, blueMax,
+                         redShift, greenShift, blueShift);
+
+    if (!trueColour) {
       // Set the DIBSection's palette
       refreshPalette();
     }
-
   }
 }