From 2dcdb0c8b6325f8d72a09d50768984891f9659ca Mon Sep 17 00:00:00 2001
From: Roderick Colenbrander <thunderbird2k@gmail.com>
Date: Tue, 1 Sep 2009 20:10:32 +0200
Subject: [PATCH] Introduce a new function for looking up the physical color of a pixel for use with colormap generation-like functions. This prepares to a rewrite of X11DRV_PALETTE_ToPhysical which requires the physDev for retrieving the color shifts.

---
 dlls/winex11.drv/dib.c     |    4 +-
 dlls/winex11.drv/palette.c |   66 ++++++++++++++++++++++++++++++++++++++++++-
 dlls/winex11.drv/x11drv.h  |    1 +
 3 files changed, 67 insertions(+), 4 deletions(-)

diff --git a/dlls/winex11.drv/dib.c b/dlls/winex11.drv/dib.c
index 794029a..b40215a 100644
--- a/dlls/winex11.drv/dib.c
+++ b/dlls/winex11.drv/dib.c
@@ -371,7 +371,7 @@ static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
             }
             else
                 for (i = start; i < end; i++, rgb++)
-                    colorMapping[i] = X11DRV_PALETTE_ToPhysical( NULL, RGB(rgb->rgbRed,
+                    colorMapping[i] = X11DRV_PALETTE_LookupPixel(RGB(rgb->rgbRed,
                                                                 rgb->rgbGreen,
                                                                 rgb->rgbBlue));
         }
@@ -395,7 +395,7 @@ static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
             }
             else
                 for (i = start; i < end; i++, rgb++)
-                    colorMapping[i] = X11DRV_PALETTE_ToPhysical( NULL, RGB(rgb->rgbtRed,
+                    colorMapping[i] = X11DRV_PALETTE_LookupPixel(RGB(rgb->rgbtRed,
                                                                rgb->rgbtGreen,
                                                                rgb->rgbtBlue));
         }
diff --git a/dlls/winex11.drv/palette.c b/dlls/winex11.drv/palette.c
index 356cb95..4c94cda 100644
--- a/dlls/winex11.drv/palette.c
+++ b/dlls/winex11.drv/palette.c
@@ -1016,6 +1016,69 @@ int X11DRV_PALETTE_ToPhysical( X11DRV_PDEVICE *physDev, COLORREF color )
 }
 
 /***********************************************************************
+ *           X11DRV_PALETTE_LookupPixel
+ */
+int X11DRV_PALETTE_LookupPixel(COLORREF color )
+{
+    WORD         index = 0;
+    HPALETTE hPal = GetStockObject(DEFAULT_PALETTE);
+    unsigned char    spec_type = color >> 24;
+    int *mapping = palette_get_mapping( hPal );
+
+    /* Only accept RGB which has spec_type = 0 */
+    if(spec_type)
+        return 0;
+
+    color &= 0xffffff;
+
+    if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_FIXED )
+    {
+        unsigned long red, green, blue;
+        red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
+
+        if (X11DRV_PALETTE_Graymax)
+        {
+            /* grayscale only; return scaled value */
+            return ( (red * 30 + green * 59 + blue * 11) * X11DRV_PALETTE_Graymax) / 25500;
+        }
+        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);
+        }
+    }
+    else
+    {
+        if (!mapping)
+            WARN("Palette %p is not realized\n", hPal);
+
+        EnterCriticalSection( &palette_cs );
+        index = X11DRV_SysPaletteLookupPixel( color, FALSE);
+        if (X11DRV_PALETTE_PaletteToXPixel)
+            index = X11DRV_PALETTE_PaletteToXPixel[index];
+        LeaveCriticalSection( &palette_cs );
+    }
+    return index;
+}
+
+
+/***********************************************************************
  *           X11DRV_PALETTE_LookupSystemXPixel
  */
 static int X11DRV_PALETTE_LookupSystemXPixel(COLORREF col)
@@ -1192,8 +1255,7 @@ UINT X11DRV_RealizePalette( X11DRV_PDEVICE *physDev, HPALETTE hpal, BOOL primary
                 }
                 else if ( X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL )
                 {
-                    index = X11DRV_PALETTE_ToPhysical( NULL,
-                                                       RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ));
+                    index = X11DRV_PALETTE_LookupPixel( RGB( entries[i].peRed, entries[i].peGreen, entries[i].peBlue ));
                 }
 
                 /* we have to map to existing entry in the system palette */
diff --git a/dlls/winex11.drv/x11drv.h b/dlls/winex11.drv/x11drv.h
index 26671eb..7f94153 100644
--- a/dlls/winex11.drv/x11drv.h
+++ b/dlls/winex11.drv/x11drv.h
@@ -477,6 +477,7 @@ extern BOOL X11DRV_IsSolidColor(COLORREF color);
 
 extern COLORREF X11DRV_PALETTE_ToLogical(int pixel);
 extern int X11DRV_PALETTE_ToPhysical(X11DRV_PDEVICE *physDev, COLORREF color);
+extern int X11DRV_PALETTE_LookupPixel(COLORREF color);
 
 extern unsigned int depth_to_bpp( unsigned int depth );
 
-- 
1.6.0.4

