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();
     }
-
   }
 }
 
diff --git a/win/rfb_win32/DeviceContext.cxx b/win/rfb_win32/DeviceContext.cxx
index 3b97e44..26721a6 100644
--- a/win/rfb_win32/DeviceContext.cxx
+++ b/win/rfb_win32/DeviceContext.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
@@ -33,7 +34,11 @@
 }
 
 PixelFormat DeviceContext::getPF(HDC dc) {
-  PixelFormat format;
+  bool trueColour, bigEndian;
+  int bpp, depth;
+  int redMax, greenMax, blueMax;
+  int redShift, greenShift, blueShift;
+
   CompatibleBitmap bitmap(dc, 1, 1);
 
   // -=- Get the bitmap format information
@@ -49,11 +54,11 @@
   }
 
   // Set the initial format information
-  format.trueColour = bi.bmiHeader.biBitCount > 8;
-  format.bigEndian = 0;
-  format.bpp = bi.bmiHeader.biBitCount;
+  trueColour = bi.bmiHeader.biBitCount > 8;
+  bigEndian = 0;
+  bpp = bi.bmiHeader.biBitCount;
 
-  if (format.trueColour) {
+  if (trueColour) {
     DWORD rMask=0, gMask=0, bMask=0;
 
     // Which true colour format is the DIB section using?
@@ -92,44 +97,42 @@
     };
 
     // Convert the data we just retrieved
-    initMaxAndShift(rMask, &format.redMax, &format.redShift);
-    initMaxAndShift(gMask, &format.greenMax, &format.greenShift);
-    initMaxAndShift(bMask, &format.blueMax, &format.blueShift);
+    initMaxAndShift(rMask, &redMax, &redShift);
+    initMaxAndShift(gMask, &greenMax, &greenShift);
+    initMaxAndShift(bMask, &blueMax, &blueShift);
 
     // Calculate the depth from the colour shifts
-    format.depth = 0;
+    depth = 0;
     Pixel bits = rMask | gMask | bMask;
     while (bits) {
-      format.depth++;
+      depth++;
       bits = bits >> 1;
     }
 
     // Check that the depth & bpp are valid
-    if (format.depth > format.bpp) {
+    if (depth > bpp) {
       vlog.error("depth exceeds bits per pixel!");
-      format.bpp = format.depth;
+      bpp = depth;
     }
 
     // Correct the bits-per-pixel to something we're happy with
-    if (format.bpp <= 16)
-      format.bpp = 16;
-    else if (format.bpp <= 32)
-      format.bpp = 32;
+    if (bpp <= 16)
+      bpp = 16;
+    else if (bpp <= 32)
+      bpp = 32;
   } else {
     // Palettised format - depth reflects number of colours,
     // but bits-per-pixel is ALWAYS 8
-    format.depth = format.bpp;
-    if (format.bpp < 8)
-      format.bpp = 8;
-    vlog.info("%d-colour palettised", 1<<format.depth);
+    depth = bpp;
+    if (bpp < 8)
+      bpp = 8;
+    vlog.info("%d-colour palettised", 1<<depth);
   }
 
 
-  // Use 10 arguments constructor to trigger PixelFormat::updateState()
-  return PixelFormat(format.bpp, format.depth,
-		     format.bigEndian, format.trueColour,
-		     format.redMax, format.greenMax, format.blueMax,
-		     format.redShift, format.greenShift, format.blueShift);
+  return PixelFormat(bpp, depth, bigEndian, trueColour,
+		                 redMax, greenMax, blueMax,
+		                 redShift, greenShift, blueShift);
 }
 
 Rect DeviceContext::getClipBox() const {