From ca4b4baf5feafcb63258876946b3cd18bc793dd5 Mon Sep 17 00:00:00 2001
From: Roderick Colenbrander <thunderbird2k@gmail.com>
Date: Sun, 30 Aug 2009 17:30:55 +0200
Subject: [PATCH] Pass the pixel format of a physBitmap/physDev to X11DRV_PALETTE_ToLogical. This prepares for 32-bit dibsections. The next patch will do the same for ToPhysical.

---
 dlls/winex11.drv/bitmap.c   |    3 +++
 dlls/winex11.drv/dib.c      |   27 ++++++++++++++++-----------
 dlls/winex11.drv/graphics.c |    4 ++--
 dlls/winex11.drv/init.c     |    2 ++
 dlls/winex11.drv/palette.c  |    3 +--
 dlls/winex11.drv/x11drv.h   |    4 +++-
 6 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/dlls/winex11.drv/bitmap.c b/dlls/winex11.drv/bitmap.c
index a288352..79ae30f 100644
--- a/dlls/winex11.drv/bitmap.c
+++ b/dlls/winex11.drv/bitmap.c
@@ -56,6 +56,7 @@ void X11DRV_BITMAP_Init(void)
     wine_tsx11_lock();
     bitmap_context = XUniqueContext();
     BITMAP_stock_phys_bitmap.pixmap_depth = 1;
+    X11DRV_PALETTE_SetPixelFormat(&BITMAP_stock_phys_bitmap.pixmap_format, visual->red_mask, visual->green_mask, visual->blue_mask);
     BITMAP_stock_phys_bitmap.pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
     bitmap_gc[0] = XCreateGC( gdi_display, BITMAP_stock_phys_bitmap.pixmap, 0, NULL );
     XSetGraphicsExposures( gdi_display, bitmap_gc[0], False );
@@ -110,6 +111,7 @@ HBITMAP CDECL X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
     if (physDev->depth != physBitmap->pixmap_depth)
     {
         physDev->depth = physBitmap->pixmap_depth;
+        physDev->format = physBitmap->pixmap_format;
         wine_tsx11_lock();
         XFreeGC( gdi_display, physDev->gc );
         physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
@@ -159,6 +161,7 @@ BOOL CDECL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, LPVOID
       /* Create the pixmap */
     wine_tsx11_lock();
     physBitmap->pixmap_depth = (bitmap.bmBitsPixel == 1) ? 1 : screen_depth;
+    X11DRV_PALETTE_SetPixelFormat(&physBitmap->pixmap_format, visual->red_mask, visual->green_mask, visual->blue_mask);
     physBitmap->pixmap = XCreatePixmap(gdi_display, root_window,
                                        bitmap.bmWidth, bitmap.bmHeight, physBitmap->pixmap_depth);
     wine_tsx11_unlock();
diff --git a/dlls/winex11.drv/dib.c b/dlls/winex11.drv/dib.c
index 09df53f..657f9f8 100644
--- a/dlls/winex11.drv/dib.c
+++ b/dlls/winex11.drv/dib.c
@@ -70,6 +70,7 @@ typedef struct
     DWORD                   infoWidth;
     WORD                    depth;
     WORD                    infoBpp;
+    PixelFormat            *format;
     WORD                    compression;
     RGBQUAD                *colorMap;
     int                     nColorMap;
@@ -348,7 +349,6 @@ static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
                              const void *colorPtr, int start, int end )
 {
     int i;
-
     if (coloruse == DIB_RGB_COLORS)
     {
         if (quads)
@@ -2203,7 +2203,7 @@ static void X11DRV_DIB_SetImageBits_16( int lines, const BYTE *srcbits,
  */
 static void X11DRV_DIB_GetImageBits_16( int lines, BYTE *dstbits,
 					DWORD dstwidth, DWORD srcwidth,
-					PALETTEENTRY *srccolors,
+					PALETTEENTRY *srccolors, PixelFormat *srcFormat,
 					DWORD rDst, DWORD gDst, DWORD bDst,
 					XImage *bmpImage, DWORD dibpitch )
 {
@@ -2511,7 +2511,7 @@ static void X11DRV_DIB_GetImageBits_16( int lines, BYTE *dstbits,
                 for (x = 0; x < width; x++) {
                     COLORREF srcval;
                     DWORD dstval;
-                    srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
+                    srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h), srcFormat);
                     dstval=((GetRValue(srcval) << rShift) & rDst) |
                            ((GetGValue(srcval) << gShift) & gDst) |
                            ((GetBValue(srcval) << bShift) & bDst);
@@ -2698,7 +2698,7 @@ static void X11DRV_DIB_SetImageBits_24( int lines, const BYTE *srcbits,
  */
 static void X11DRV_DIB_GetImageBits_24( int lines, BYTE *dstbits,
 					DWORD dstwidth, DWORD srcwidth,
-					PALETTEENTRY *srccolors,
+					PALETTEENTRY *srccolors, PixelFormat *srcFormat,
                                         DWORD rDst, DWORD gDst, DWORD bDst,
 					XImage *bmpImage, DWORD linebytes )
 {
@@ -2893,7 +2893,7 @@ static void X11DRV_DIB_GetImageBits_24( int lines, BYTE *dstbits,
                 dstbyte=dstbits;
                 for (x = 0; x < width; x++) {
                     COLORREF srcval=X11DRV_PALETTE_ToLogical
-                        (XGetPixel( bmpImage, x, h ));
+                        (XGetPixel( bmpImage, x, h ), srcFormat);
                     dstbyte[0]=GetBValue(srcval);
                     dstbyte[1]=GetGValue(srcval);
                     dstbyte[2]=GetRValue(srcval);
@@ -3180,7 +3180,7 @@ static void X11DRV_DIB_SetImageBits_32(int lines, const BYTE *srcbits,
  */
 static void X11DRV_DIB_GetImageBits_32( int lines, BYTE *dstbits,
 					DWORD dstwidth, DWORD srcwidth,
-					PALETTEENTRY *srccolors,
+					PALETTEENTRY *srccolors, PixelFormat *srcFormat,
 					DWORD rDst, DWORD gDst, DWORD bDst,
 					XImage *bmpImage, DWORD linebytes )
 {
@@ -3477,7 +3477,7 @@ static void X11DRV_DIB_GetImageBits_32( int lines, BYTE *dstbits,
                 dstpixel=(DWORD*)dstbits;
                 for (x = 0; x < width; x++) {
                     COLORREF srcval;
-                    srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
+                    srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h), srcFormat);
                     *dstpixel++=(GetRValue(srcval) << rShift) |
                                 (GetGValue(srcval) << gShift) |
                                 (GetBValue(srcval) << bShift);
@@ -3802,7 +3802,7 @@ static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
     case 16:
        X11DRV_DIB_GetImageBits_16( descr->lines, (LPVOID)descr->bits,
 				   descr->infoWidth,descr->width,
-				   descr->palentry,
+				   descr->palentry, descr->format,
 				   descr->rMask, descr->gMask, descr->bMask,
 				   bmpImage, descr->dibpitch );
        break;
@@ -3810,7 +3810,7 @@ static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
     case 24:
        X11DRV_DIB_GetImageBits_24( descr->lines, (LPVOID)descr->bits,
 				   descr->infoWidth,descr->width,
-				   descr->palentry,
+				   descr->palentry, descr->format,
 				   descr->rMask, descr->gMask, descr->bMask,
                                    bmpImage, descr->dibpitch);
        break;
@@ -3818,7 +3818,7 @@ static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
     case 32:
        X11DRV_DIB_GetImageBits_32( descr->lines, (LPVOID)descr->bits,
 				   descr->infoWidth, descr->width,
-				   descr->palentry,
+				   descr->palentry, descr->format,
                                    descr->rMask, descr->gMask, descr->bMask,
                                    bmpImage, descr->dibpitch);
        break;
@@ -3938,6 +3938,7 @@ INT CDECL X11DRV_SetDIBitsToDevice( X11DRV_PDEVICE *physDev, INT xDest, INT yDes
     descr.lines      = top_down ? -lines : lines;
     descr.infoWidth  = width;
     descr.depth      = physDev->depth;
+    descr.format     = &physDev->format;
     descr.drawable   = physDev->drawable;
     descr.gc         = physDev->gc;
     descr.xSrc       = xSrc;
@@ -4027,6 +4028,7 @@ INT CDECL X11DRV_SetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT start
   descr.infoWidth  = width;
   descr.lines      = tmpheight >= 0 ? lines : -lines;
   descr.depth      = physBitmap->pixmap_depth;
+  descr.format     = &physBitmap->pixmap_format;
   descr.drawable   = physBitmap->pixmap;
   descr.gc         = get_bitmap_gc(physBitmap->pixmap_depth);
   descr.xSrc       = 0;
@@ -4153,7 +4155,7 @@ INT CDECL X11DRV_GetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT start
               WORD *index = colorPtr;
               descr.colorMap = rgb = HeapAlloc(GetProcessHeap(), 0, num_colors * sizeof(RGBQUAD));
               for(i = 0; i < num_colors; i++, rgb++, index++) {
-                  colref = X11DRV_PALETTE_ToLogical(X11DRV_PALETTE_ToPhysical(physDev, PALETTEINDEX(*index)));
+                  colref = X11DRV_PALETTE_ToLogical(X11DRV_PALETTE_ToPhysical(physDev, PALETTEINDEX(*index)), &physDev->format);
                   rgb->rgbRed = GetRValue(colref);
                   rgb->rgbGreen = GetGValue(colref);
                   rgb->rgbBlue = GetBValue(colref);
@@ -4182,6 +4184,7 @@ INT CDECL X11DRV_GetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT start
   descr.infoWidth  = width;
   descr.lines      = lines;
   descr.depth      = physBitmap->pixmap_depth;
+  descr.format     = &physBitmap->pixmap_format;
   descr.drawable   = physBitmap->pixmap;
   descr.gc         = get_bitmap_gc(physBitmap->pixmap_depth);
   descr.width      = dib.dsBm.bmWidth;
@@ -4257,6 +4260,7 @@ static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB,
   descr.nColorMap   = nColorMap;
   descr.bits        = dibSection.dsBm.bmBits;
   descr.depth       = physBitmap->pixmap_depth;
+  descr.format      = &physBitmap->pixmap_format;
   descr.compression = dibSection.dsBmih.biCompression;
   descr.physBitmap  = physBitmap;
 
@@ -4749,6 +4753,7 @@ HBITMAP CDECL X11DRV_CreateDIBSection( X11DRV_PDEVICE *physDev, HBITMAP hbitmap,
     /* create pixmap and X image */
     wine_tsx11_lock();
     physBitmap->pixmap_depth = (dib.dsBm.bmBitsPixel == 1) ? 1 : screen_depth;
+    X11DRV_PALETTE_SetPixelFormat(&physBitmap->pixmap_format, visual->red_mask, visual->green_mask, visual->blue_mask);
 #ifdef HAVE_LIBXXSHM
     physBitmap->shminfo.shmid = -1;
 
diff --git a/dlls/winex11.drv/graphics.c b/dlls/winex11.drv/graphics.c
index 7a64022..f06adf7 100644
--- a/dlls/winex11.drv/graphics.c
+++ b/dlls/winex11.drv/graphics.c
@@ -1036,7 +1036,7 @@ X11DRV_SetPixel( X11DRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
     /* Update the DIBSection from the pixmap */
     X11DRV_UnlockDIBSection(physDev, TRUE);
 
-    return X11DRV_PALETTE_ToLogical(pixel);
+    return X11DRV_PALETTE_ToLogical(pixel, &physDev->format);
 }
 
 
@@ -1083,7 +1083,7 @@ X11DRV_GetPixel( X11DRV_PDEVICE *physDev, INT x, INT y )
     /* Update the DIBSection from the pixmap */
     X11DRV_UnlockDIBSection(physDev, FALSE);
     if( physDev->depth > 1)
-        pixel = X11DRV_PALETTE_ToLogical(pixel);
+        pixel = X11DRV_PALETTE_ToLogical(pixel, &physDev->format);
     else
         /* monochrome bitmaps return black or white */
         if( pixel) pixel = 0xffffff;
diff --git a/dlls/winex11.drv/init.c b/dlls/winex11.drv/init.c
index 480486f..5d211db 100644
--- a/dlls/winex11.drv/init.c
+++ b/dlls/winex11.drv/init.c
@@ -138,6 +138,7 @@ BOOL CDECL X11DRV_CreateDC( HDC hdc, X11DRV_PDEVICE **pdev, LPCWSTR driver, LPCW
         physDev->bitmap    = &BITMAP_stock_phys_bitmap;
         physDev->drawable  = BITMAP_stock_phys_bitmap.pixmap;
         physDev->depth     = 1;
+        X11DRV_PALETTE_SetPixelFormat(&physDev->format, visual->red_mask, visual->green_mask, visual->blue_mask);
         SetRect( &physDev->drawable_rect, 0, 0, 1, 1 );
         physDev->dc_rect = physDev->drawable_rect;
     }
@@ -146,6 +147,7 @@ BOOL CDECL X11DRV_CreateDC( HDC hdc, X11DRV_PDEVICE **pdev, LPCWSTR driver, LPCW
         physDev->bitmap    = NULL;
         physDev->drawable  = root_window;
         physDev->depth     = screen_depth;
+        X11DRV_PALETTE_SetPixelFormat(&physDev->format, visual->red_mask, visual->green_mask, visual->blue_mask);
         physDev->drawable_rect = virtual_screen_rect;
         SetRect( &physDev->dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
                  virtual_screen_rect.bottom - virtual_screen_rect.top );
diff --git a/dlls/winex11.drv/palette.c b/dlls/winex11.drv/palette.c
index 72f5a0d..5680f56 100644
--- a/dlls/winex11.drv/palette.c
+++ b/dlls/winex11.drv/palette.c
@@ -783,11 +783,10 @@ BOOL X11DRV_IsSolidColor( COLORREF color )
  *
  * Return RGB color for given X pixel.
  */
-COLORREF X11DRV_PALETTE_ToLogical(int pixel)
+COLORREF X11DRV_PALETTE_ToLogical(int pixel, PixelFormat *pf)
 {
     XColor color;
 
-    PixelFormat *pf = &X11DRV_PALETTE_default_format;
 #if 0
     /* truecolor visual */
 
diff --git a/dlls/winex11.drv/x11drv.h b/dlls/winex11.drv/x11drv.h
index 29f8e95..927ee0b 100644
--- a/dlls/winex11.drv/x11drv.h
+++ b/dlls/winex11.drv/x11drv.h
@@ -119,6 +119,7 @@ typedef struct
     Pixmap       pixmap;
     XID          glxpixmap;
     int          pixmap_depth;
+    PixelFormat  pixmap_format;
     /* the following fields are only used for DIB section bitmaps */
     int          status, p_status;  /* mapping status */
     XImage      *image;             /* cached XImage */
@@ -156,6 +157,7 @@ typedef struct
     int           backgroundPixel;
     int           textPixel;
     int           depth;       /* bit depth of the DC */
+    PixelFormat   format;
     int           exposures;   /* count of graphics exposures operations */
     int           current_pf;
     Drawable      gl_drawable;
@@ -488,7 +490,7 @@ extern int X11DRV_PALETTE_Init(void);
 extern void X11DRV_PALETTE_Cleanup(void);
 extern BOOL X11DRV_IsSolidColor(COLORREF color);
 
-extern COLORREF X11DRV_PALETTE_ToLogical(int pixel);
+extern COLORREF X11DRV_PALETTE_ToLogical(int pixel, PixelFormat *pf);
 extern int X11DRV_PALETTE_ToPhysical(X11DRV_PDEVICE *physDev, COLORREF color);
 
 extern unsigned int depth_to_bpp( unsigned int depth );
-- 
1.6.0.4

