From 9c55bd618934de4170382cafdadb036203b261d7 Mon Sep 17 00:00:00 2001
From: Roderick Colenbrander <thunderbird2k@gmail.com>
Date: Sun, 30 Aug 2009 17:20:12 +0200
Subject: [PATCH] Refactor the palette code to work on a new PixelFormat structure instead of separate ColorShift ones. This prepares for next patches which will pass a PixelFormat to the ToPhysical and ToLogical color conversion functions this is needed for 32-bit dibsection support.

---
 dlls/winex11.drv/palette.c |  102 +++++++++++++++++++++-----------------------
 dlls/winex11.drv/x11drv.h  |   13 ++++++
 2 files changed, 62 insertions(+), 53 deletions(-)

diff --git a/dlls/winex11.drv/palette.c b/dlls/winex11.drv/palette.c
index 356cb95..72f5a0d 100644
--- a/dlls/winex11.drv/palette.c
+++ b/dlls/winex11.drv/palette.c
@@ -59,19 +59,8 @@ static int COLOR_gapFilled = 0;
 Colormap X11DRV_PALETTE_PaletteXColormap = 0;
 UINT16   X11DRV_PALETTE_PaletteFlags     = 0;
 
-typedef struct {
-    int shift;
-    int scale;
-    int max;
-} ColorShifts;
-
 /* initialize to zero to handle abortive X11DRV_PALETTE_VIRTUAL visuals */
-static ColorShifts X11DRV_PALETTE_PRed   = {0,0,0};
-static ColorShifts X11DRV_PALETTE_LRed   = {0,0,0};
-static ColorShifts X11DRV_PALETTE_PGreen = {0,0,0};
-static ColorShifts X11DRV_PALETTE_LGreen = {0,0,0};
-static ColorShifts X11DRV_PALETTE_PBlue  = {0,0,0};
-static ColorShifts X11DRV_PALETTE_LBlue  = {0,0,0};
+static PixelFormat X11DRV_PALETTE_default_format = { {0,0,0,}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
 static int X11DRV_PALETTE_Graymax        = 0;
 
 static int palette_size;
@@ -234,9 +223,7 @@ int X11DRV_PALETTE_Init(void)
             X11DRV_PALETTE_PaletteXColormap = XCreateColormap(gdi_display, root_window,
                                                               visual, AllocNone);
             X11DRV_PALETTE_PaletteFlags |= X11DRV_PALETTE_FIXED;
-            X11DRV_PALETTE_ComputeShifts(visual->red_mask, &X11DRV_PALETTE_PRed, &X11DRV_PALETTE_LRed);
-            X11DRV_PALETTE_ComputeShifts(visual->green_mask, &X11DRV_PALETTE_PGreen, &X11DRV_PALETTE_LGreen);
-            X11DRV_PALETTE_ComputeShifts(visual->blue_mask, &X11DRV_PALETTE_PBlue, &X11DRV_PALETTE_LBlue);
+            X11DRV_PALETTE_SetPixelFormat(&X11DRV_PALETTE_default_format, visual->red_mask, visual->green_mask, visual->blue_mask);
         }
         XFree(depths);
         wine_tsx11_unlock();
@@ -337,6 +324,13 @@ static void X11DRV_PALETTE_ComputeShifts(unsigned long maskbits, ColorShifts *ph
     }
 }
 
+void X11DRV_PALETTE_SetPixelFormat(PixelFormat *pf, unsigned long redMask, unsigned long greenMask, unsigned long blueMask)
+{
+    X11DRV_PALETTE_ComputeShifts(redMask, &pf->physicalRed, &pf->logicalRed);
+    X11DRV_PALETTE_ComputeShifts(greenMask, &pf->physicalGreen, &pf->logicalGreen);
+    X11DRV_PALETTE_ComputeShifts(blueMask, &pf->physicalBlue, &pf->logicalBlue);
+}
+
 /***********************************************************************
  *           X11DRV_PALETTE_BuildPrivateMap
  *
@@ -698,11 +692,11 @@ static void X11DRV_PALETTE_FillDefaultColors( const PALETTEENTRY *sys_pal_templa
 
 	 if( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
 	 {
-            if (X11DRV_PALETTE_PRed.max != 255) no_r = (red * X11DRV_PALETTE_PRed.max) / 255;
-            if (X11DRV_PALETTE_PGreen.max != 255) no_g = (green * X11DRV_PALETTE_PGreen.max) / 255;
-            if (X11DRV_PALETTE_PBlue.max != 255) no_b = (blue * X11DRV_PALETTE_PBlue.max) / 255;
+            if (X11DRV_PALETTE_default_format.physicalRed.max != 255) no_r = (red * X11DRV_PALETTE_default_format.physicalRed.max) / 255;
+            if (X11DRV_PALETTE_default_format.physicalGreen.max != 255) no_g = (green * X11DRV_PALETTE_default_format.physicalGreen.max) / 255;
+            if (X11DRV_PALETTE_default_format.physicalBlue.max != 255) no_b = (blue * X11DRV_PALETTE_default_format.physicalBlue.max) / 255;
 
-            X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << X11DRV_PALETTE_PRed.shift) | (no_g << X11DRV_PALETTE_PGreen.shift) | (no_b << X11DRV_PALETTE_PBlue.shift);
+            X11DRV_PALETTE_PaletteToXPixel[idx] = (no_r << X11DRV_PALETTE_default_format.physicalRed.shift) | (no_g << X11DRV_PALETTE_default_format.physicalGreen.shift) | (no_b << X11DRV_PALETTE_default_format.physicalBlue.shift);
 	 }
 	 else if( !(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) )
 	 {
@@ -793,6 +787,7 @@ COLORREF X11DRV_PALETTE_ToLogical(int pixel)
 {
     XColor color;
 
+    PixelFormat *pf = &X11DRV_PALETTE_default_format;
 #if 0
     /* truecolor visual */
 
@@ -803,18 +798,18 @@ COLORREF X11DRV_PALETTE_ToLogical(int pixel)
 
     if ( (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED) && !X11DRV_PALETTE_Graymax )
     {
-         color.red = (pixel >> X11DRV_PALETTE_LRed.shift) & X11DRV_PALETTE_LRed.max;
-         if (X11DRV_PALETTE_LRed.scale<8)
-             color.red=  color.red   << (8-X11DRV_PALETTE_LRed.scale) |
-                         color.red   >> (2*X11DRV_PALETTE_LRed.scale-8);
-         color.green = (pixel >> X11DRV_PALETTE_LGreen.shift) & X11DRV_PALETTE_LGreen.max;
-         if (X11DRV_PALETTE_LGreen.scale<8)
-             color.green=color.green << (8-X11DRV_PALETTE_LGreen.scale) |
-                         color.green >> (2*X11DRV_PALETTE_LGreen.scale-8);
-         color.blue = (pixel >> X11DRV_PALETTE_LBlue.shift) & X11DRV_PALETTE_LBlue.max;
-         if (X11DRV_PALETTE_LBlue.scale<8)
-             color.blue= color.blue  << (8-X11DRV_PALETTE_LBlue.scale)  |
-                         color.blue  >> (2*X11DRV_PALETTE_LBlue.scale-8);
+         color.red = (pixel >> pf->logicalRed.shift) & pf->logicalRed.max;
+         if (pf->logicalRed.scale<8)
+             color.red=  color.red   << (8-pf->logicalRed.scale) |
+                         color.red   >> (2*pf->logicalRed.scale-8);
+         color.green = (pixel >> pf->logicalGreen.shift) & pf->logicalGreen.max;
+         if (pf->logicalGreen.scale<8)
+             color.green=color.green << (8-pf->logicalGreen.scale) |
+                         color.green >> (2*pf->logicalGreen.scale-8);
+         color.blue = (pixel >> pf->logicalBlue.shift) & pf->logicalBlue.max;
+         if (pf->logicalBlue.scale<8)
+             color.blue= color.blue  << (8-pf->logicalBlue.scale)  |
+                         color.blue  >> (2*pf->logicalBlue.scale-8);
                  return RGB(color.red,color.green,color.blue);
     }
 
@@ -881,6 +876,7 @@ int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
     unsigned char	 spec_type = color >> 24;
     int *mapping = palette_get_mapping( hPal );
     PALETTEENTRY entry;
+    PixelFormat *pf = &X11DRV_PALETTE_default_format;
 
     if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
     {
@@ -934,31 +930,31 @@ int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
 
         red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
 
-	if (X11DRV_PALETTE_Graymax)
+        if (X11DRV_PALETTE_Graymax)
         {
-	    /* grayscale only; return scaled value */
+            /* grayscale only; return scaled value */
             return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
-	}
-	else
+        }
+        else
         {
-	    /* scale each individually and construct the TrueColor pixel value */
-	    if (X11DRV_PALETTE_PRed.scale < 8)
-		red = red >> (8-X11DRV_PALETTE_PRed.scale);
-	    else if (X11DRV_PALETTE_PRed.scale > 8)
-		red =   red   << (X11DRV_PALETTE_PRed.scale-8) |
-                        red   >> (16-X11DRV_PALETTE_PRed.scale);
-	    if (X11DRV_PALETTE_PGreen.scale < 8)
-		green = green >> (8-X11DRV_PALETTE_PGreen.scale);
-	    else if (X11DRV_PALETTE_PGreen.scale > 8)
-		green = green << (X11DRV_PALETTE_PGreen.scale-8) |
-                        green >> (16-X11DRV_PALETTE_PGreen.scale);
-	    if (X11DRV_PALETTE_PBlue.scale < 8)
-		blue =  blue  >> (8-X11DRV_PALETTE_PBlue.scale);
-	    else if (X11DRV_PALETTE_PBlue.scale > 8)
-		blue =  blue  << (X11DRV_PALETTE_PBlue.scale-8) |
-                        blue  >> (16-X11DRV_PALETTE_PBlue.scale);
-
-            return (red << X11DRV_PALETTE_PRed.shift) | (green << X11DRV_PALETTE_PGreen.shift) | (blue << X11DRV_PALETTE_PBlue.shift);
+            /* scale each individually and construct the TrueColor pixel value */
+            if (pf->physicalRed.scale < 8)
+                red = red >> (8-pf->physicalRed.scale);
+            else if (pf->physicalRed.scale > 8)
+                red = red << (pf->physicalRed.scale-8) | 
+                      red >> (16-pf->physicalRed.scale);
+            if (pf->physicalGreen.scale < 8)
+                green = green >> (8-pf->physicalGreen.scale);
+            else if (pf->physicalGreen.scale > 8)
+                green = green << (pf->physicalGreen.scale-8) |
+                        green >> (16-pf->physicalGreen.scale);
+            if (pf->physicalBlue.scale < 8)
+                blue =  blue >> (8-pf->physicalBlue.scale);
+            else if (pf->physicalBlue.scale > 8)
+                blue =  blue << (pf->physicalBlue.scale-8) |
+                        blue >> (16-pf->physicalBlue.scale);
+
+            return (red << pf->physicalRed.shift) | (green << pf->physicalGreen.shift) | (blue << pf->physicalBlue.shift);
         }
     }
     else
diff --git a/dlls/winex11.drv/x11drv.h b/dlls/winex11.drv/x11drv.h
index 26671eb..29f8e95 100644
--- a/dlls/winex11.drv/x11drv.h
+++ b/dlls/winex11.drv/x11drv.h
@@ -69,6 +69,19 @@ struct tagCURSORICONINFO;
 extern void CDECL wine_tsx11_lock(void);
 extern void CDECL wine_tsx11_unlock(void);
 
+typedef struct {
+    int shift;
+    int scale;
+    int max;
+} ColorShifts;
+
+typedef struct
+{
+    ColorShifts physicalRed, physicalBlue, physicalGreen;
+    ColorShifts logicalRed, logicalBlue, logicalGreen;
+} PixelFormat;
+void X11DRV_PALETTE_SetPixelFormat(PixelFormat *pf, unsigned long redMask, unsigned long greenMask, unsigned long blueMask);
+
   /* X physical pen */
 typedef struct
 {
-- 
1.6.0.4

