winex11.drv: Add on-screen PS_USERSTYLE support to pens.
[wine] / dlls / winex11.drv / dib.c
1 /*
2  * X11DRV device-independent bitmaps
3  *
4  * Copyright 1993,1994  Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <X11/Xlib.h>
25 #ifdef HAVE_LIBXXSHM
26 #include <X11/extensions/XShm.h>
27 # ifdef HAVE_SYS_SHM_H
28 #  include <sys/shm.h>
29 # endif
30 # ifdef HAVE_SYS_IPC_H
31 #  include <sys/ipc.h>
32 # endif
33 #endif /* defined(HAVE_LIBXXSHM) */
34
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wingdi.h"
41 #include "x11drv.h"
42 #include "wine/exception.h"
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
46
47 static struct list dibs_list = LIST_INIT(dibs_list);
48
49 static CRITICAL_SECTION dibs_cs;
50 static CRITICAL_SECTION_DEBUG dibs_cs_debug =
51 {
52     0, 0, &dibs_cs,
53     { &dibs_cs_debug.ProcessLocksList, &dibs_cs_debug.ProcessLocksList },
54       0, 0, { (DWORD_PTR)(__FILE__ ": dibs_cs") }
55 };
56 static CRITICAL_SECTION dibs_cs = { &dibs_cs_debug, -1, 0, 0, 0, 0 };
57
58 static PVOID dibs_handler;
59
60 static int ximageDepthTable[32];
61
62 /* This structure holds the arguments for DIB_SetImageBits() */
63 typedef struct
64 {
65     X11DRV_PDEVICE         *physDev;
66     LPCVOID                 bits;
67     XImage                 *image;
68     PALETTEENTRY           *palentry;
69     int                     lines;
70     DWORD                   infoWidth;
71     WORD                    depth;
72     WORD                    infoBpp;
73     WORD                    compression;
74     ColorShifts            *shifts;
75     RGBQUAD                *colorMap;
76     int                     nColorMap;
77     Drawable                drawable;
78     GC                      gc;
79     int                     xSrc;
80     int                     ySrc;
81     int                     xDest;
82     int                     yDest;
83     int                     width;
84     int                     height;
85     DWORD                   rMask;
86     DWORD                   gMask;
87     DWORD                   bMask;
88     enum x11drv_shm_mode    shm_mode;
89     int                     dibpitch;
90     DWORD                   sizeImage;
91     X_PHYSBITMAP           *physBitmap;
92 } X11DRV_DIB_IMAGEBITS_DESCR;
93
94
95 enum Rle_EscapeCodes
96 {
97   RLE_EOL   = 0, /* End of line */
98   RLE_END   = 1, /* End of bitmap */
99   RLE_DELTA = 2  /* Delta */
100 };
101
102
103 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *,INT);
104
105 /* 
106   Some of the following helper functions are duplicated in
107   dlls/gdi/dib.c
108 */
109
110 /***********************************************************************
111  *           DIB_DoProtectDIBSection
112  */
113 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP *physBitmap, DWORD new_prot )
114 {
115     DWORD old_prot;
116
117     VirtualProtect(physBitmap->base, physBitmap->size, new_prot, &old_prot);
118     TRACE("Changed protection from %d to %d\n", old_prot, new_prot);
119 }
120
121 /***********************************************************************
122  *           X11DRV_DIB_GetXImageWidthBytes
123  *
124  * Return the width of an X image in bytes
125  */
126 static inline int X11DRV_DIB_GetXImageWidthBytes( int width, int depth )
127 {
128     if (!depth || depth > 32) goto error;
129
130     if (!ximageDepthTable[depth-1])
131     {
132         XImage *testimage = XCreateImage( gdi_display, visual, depth,
133                                           ZPixmap, 0, NULL, 1, 1, 32, 20 );
134         if (testimage)
135         {
136             ximageDepthTable[depth-1] = testimage->bits_per_pixel;
137             XDestroyImage( testimage );
138         }
139         else ximageDepthTable[depth-1] = -1;
140     }
141     if (ximageDepthTable[depth-1] != -1)
142         return (4 * ((width * ximageDepthTable[depth-1] + 31) / 32));
143
144  error:
145     WARN( "(%d): Unsupported depth\n", depth );
146     return 4 * width;
147 }
148
149
150 /***********************************************************************
151  *           X11DRV_DIB_GetDIBWidthBytes
152  *
153  * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
154  */
155 static int X11DRV_DIB_GetDIBWidthBytes( int width, int depth )
156 {
157     return ((width * depth + 31) / 8) & ~3;
158 }
159
160
161 /***********************************************************************
162  *           bitmap_info_size
163  *
164  * Return the size of the bitmap info structure including color table.
165  */
166 int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
167 {
168     unsigned int colors, size, masks = 0;
169
170     if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
171     {
172         const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
173         colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
174         return sizeof(BITMAPCOREHEADER) + colors *
175              ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
176     }
177     else  /* assume BITMAPINFOHEADER */
178     {
179         colors = info->bmiHeader.biClrUsed;
180         if (!colors && (info->bmiHeader.biBitCount <= 8))
181             colors = 1 << info->bmiHeader.biBitCount;
182         if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
183         size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
184         return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
185     }
186 }
187
188
189 /***********************************************************************
190  *           X11DRV_DIB_CreateXImage
191  *
192  * Create an X image.
193  */
194 XImage *X11DRV_DIB_CreateXImage( int width, int height, int depth )
195 {
196     int width_bytes;
197     XImage *image = NULL;
198     void *data;
199
200     wine_tsx11_lock();
201     width_bytes = X11DRV_DIB_GetXImageWidthBytes( width, depth );
202     data = HeapAlloc( GetProcessHeap(), 0, height * width_bytes );
203     if (data) image = XCreateImage( gdi_display, visual, depth, ZPixmap, 0,
204                                     data, width, height, 32, width_bytes );
205     if (!image) HeapFree( GetProcessHeap(), 0, data );
206     wine_tsx11_unlock();
207     return image;
208 }
209
210
211 /***********************************************************************
212  *           X11DRV_DIB_DestroyXImage
213  *
214  * Destroy an X image created with X11DRV_DIB_CreateXImage.
215  */
216 void X11DRV_DIB_DestroyXImage( XImage *image )
217 {
218     HeapFree( GetProcessHeap(), 0, image->data );
219     image->data = NULL;
220     wine_tsx11_lock();
221     XDestroyImage( image );
222     wine_tsx11_unlock();
223 }
224
225
226 /***********************************************************************
227  *           X11DRV_DIB_GetColorCount
228  *
229  * Computes the number of colors for the bitmap palette.
230  * Should not be called for a >8-bit deep bitmap.
231  */
232 static unsigned int X11DRV_DIB_GetColorCount(const BITMAPINFO *info)
233 {
234     unsigned int colors = min( info->bmiHeader.biClrUsed, 256 );
235     if (!colors) colors = 1 << info->bmiHeader.biBitCount;
236     return colors;
237 }
238
239
240 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
241 {
242     return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) > 
243         (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
244 }
245
246 /***********************************************************************
247  *           X11DRV_DIB_GenColorMap
248  *
249  * Fills the color map of a bitmap palette. Should not be called
250  * for a >8-bit deep bitmap.
251  */
252 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
253                                     WORD coloruse, WORD depth, const void *colorPtr, int start, int end )
254 {
255     int i;
256
257     if (coloruse == DIB_RGB_COLORS)
258     {
259         const RGBQUAD * rgb = colorPtr;
260
261         if (depth == 1)  /* Monochrome */
262         {
263             BOOL invert = FALSE;
264             RGBQUAD table[2];
265
266             if (GetDIBColorTable( physDev->dev.hdc, 0, 2, table ) == 2)
267                 invert = !colour_is_brighter(table[1], table[0]);
268
269             for (i = start; i < end; i++, rgb++)
270                 colorMapping[i] = ((rgb->rgbRed + rgb->rgbGreen +
271                                     rgb->rgbBlue > 255*3/2 && !invert) ||
272                                    (rgb->rgbRed + rgb->rgbGreen +
273                                     rgb->rgbBlue <= 255*3/2 && invert));
274         }
275         else
276             for (i = start; i < end; i++, rgb++)
277                 colorMapping[i] = X11DRV_PALETTE_LookupPixel(physDev->color_shifts, RGB(rgb->rgbRed,
278                                                                                         rgb->rgbGreen,
279                                                                                         rgb->rgbBlue));
280     }
281     else  /* DIB_PAL_COLORS */
282     {
283         const WORD * index = colorPtr;
284
285         for (i = start; i < end; i++, index++)
286             colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(*index) );
287     }
288
289     return colorMapping;
290 }
291
292 /***********************************************************************
293  *           X11DRV_DIB_BuildColorMap
294  *
295  * Build the color map from the bitmap palette. Should not be called
296  * for a >8-bit deep bitmap.
297  */
298 static int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE *physDev, WORD coloruse, WORD depth,
299                                       const BITMAPINFO *info, int *nColors )
300 {
301     const void *colorPtr;
302     int *colorMapping;
303
304
305     *nColors = X11DRV_DIB_GetColorCount(info);
306     if (!*nColors) return NULL;
307
308     colorPtr = (const BYTE*)info + (WORD)info->bmiHeader.biSize;
309     if (!(colorMapping = HeapAlloc(GetProcessHeap(), 0, *nColors * sizeof(int) )))
310         return NULL;
311
312     return X11DRV_DIB_GenColorMap( physDev, colorMapping, coloruse, depth,
313                                    colorPtr, 0, *nColors);
314 }
315
316 /***********************************************************************
317  *           X11DRV_DIB_MapColor
318  */
319 static int X11DRV_DIB_MapColor( int *physMap, int nPhysMap, int phys, int oldcol )
320 {
321     int color;
322
323     if ((oldcol < nPhysMap) && (physMap[oldcol] == phys))
324         return oldcol;
325
326     for (color = 0; color < nPhysMap; color++)
327         if (physMap[color] == phys)
328             return color;
329
330     WARN("Strange color %08x\n", phys);
331     return 0;
332 }
333
334
335 /*********************************************************************
336  *         X11DRV_DIB_GetNearestIndex
337  *
338  * Helper for X11DRV_DIB_GetDIBits.
339  * Returns the nearest colour table index for a given RGB.
340  * Nearest is defined by minimizing the sum of the squares.
341  */
342 static INT X11DRV_DIB_GetNearestIndex(RGBQUAD *colormap, int numColors, BYTE r, BYTE g, BYTE b)
343 {
344     INT i, best = -1, diff, bestdiff = -1;
345     RGBQUAD *color;
346
347     for(color = colormap, i = 0; i < numColors; color++, i++) {
348         diff = (r - color->rgbRed) * (r - color->rgbRed) +
349                (g - color->rgbGreen) * (g - color->rgbGreen) +
350                (b - color->rgbBlue) * (b - color->rgbBlue);
351         if(diff == 0)
352             return i;
353         if(best == -1 || diff < bestdiff) {
354             best = i;
355             bestdiff = diff;
356         }
357     }
358     return best;
359 }
360 /*********************************************************************
361  *         X11DRV_DIB_MaskToShift
362  *
363  * Helper for X11DRV_DIB_GetDIBits.
364  * Returns the by how many bits to shift a given color so that it is
365  * in the proper position.
366  */
367 INT X11DRV_DIB_MaskToShift(DWORD mask)
368 {
369     int shift;
370
371     if (mask==0)
372         return 0;
373
374     shift=0;
375     while ((mask&1)==0) {
376         mask>>=1;
377         shift++;
378     }
379     return shift;
380 }
381
382 /***********************************************************************
383  *           X11DRV_DIB_CheckMask
384  *
385  * Check RGB mask if it is either 0 or matches visual's mask.
386  */
387 static inline int X11DRV_DIB_CheckMask(int red_mask, int green_mask, int blue_mask)
388 {
389     return ( red_mask == 0 && green_mask == 0 && blue_mask == 0 ) ||
390            ( red_mask == visual->red_mask && green_mask == visual->green_mask &&
391              blue_mask == visual->blue_mask );
392 }
393
394 /***********************************************************************
395  *           X11DRV_DIB_SetImageBits_1
396  *
397  * SetDIBits for a 1-bit deep DIB.
398  */
399 static void X11DRV_DIB_SetImageBits_1( int lines, const BYTE *srcbits,
400                                 DWORD srcwidth, DWORD dstwidth, int left,
401                                 int *colors, XImage *bmpImage, int linebytes)
402 {
403     int h, width;
404     const BYTE* srcbyte;
405     BYTE srcval, extra;
406     DWORD i, x;
407
408     if (lines < 0 ) {
409         lines = -lines;
410         srcbits = srcbits + linebytes * (lines - 1);
411         linebytes = -linebytes;
412     }
413
414     if ((extra = (left & 7)) != 0) {
415         left &= ~7;
416         dstwidth += extra;
417     }
418     srcbits += left >> 3;
419     width = min(srcwidth, dstwidth);
420
421     /* ==== pal 1 dib -> any bmp format ==== */
422     for (h = lines-1; h >=0; h--) {
423         srcbyte=srcbits;
424         for (i = width/8, x = left; i > 0; i--) {
425             srcval=*srcbyte++;
426             XPutPixel( bmpImage, x++, h, colors[ srcval >> 7] );
427             XPutPixel( bmpImage, x++, h, colors[(srcval >> 6) & 1] );
428             XPutPixel( bmpImage, x++, h, colors[(srcval >> 5) & 1] );
429             XPutPixel( bmpImage, x++, h, colors[(srcval >> 4) & 1] );
430             XPutPixel( bmpImage, x++, h, colors[(srcval >> 3) & 1] );
431             XPutPixel( bmpImage, x++, h, colors[(srcval >> 2) & 1] );
432             XPutPixel( bmpImage, x++, h, colors[(srcval >> 1) & 1] );
433             XPutPixel( bmpImage, x++, h, colors[ srcval       & 1] );
434         }
435         if (width % 8){
436             srcval=*srcbyte;
437             switch (width & 7)
438             {
439             case 7: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
440             case 6: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
441             case 5: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
442             case 4: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
443             case 3: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
444             case 2: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
445             case 1: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]);
446             }
447         }
448         srcbits += linebytes;
449     }
450 }
451
452 /***********************************************************************
453  *           X11DRV_DIB_GetImageBits_1
454  *
455  * GetDIBits for a 1-bit deep DIB.
456  */
457 static void X11DRV_DIB_GetImageBits_1( int lines, BYTE *dstbits,
458                                        DWORD dstwidth, DWORD srcwidth,
459                                        RGBQUAD *colors, PALETTEENTRY *srccolors,
460                                        XImage *bmpImage, int linebytes )
461 {
462     DWORD x;
463     int h, width = min(dstwidth, srcwidth);
464
465     if (lines < 0 ) {
466         lines = -lines;
467         dstbits = dstbits + linebytes * (lines - 1);
468         linebytes = -linebytes;
469     }
470
471     switch (bmpImage->depth)
472     {
473     case 1:
474     case 4:
475         if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
476             && srccolors) {
477             /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
478             BYTE* dstbyte;
479
480             for (h=lines-1; h>=0; h--) {
481                 BYTE dstval;
482                 dstbyte=dstbits;
483                 dstval=0;
484                 for (x=0; x<width; x++) {
485                     PALETTEENTRY srcval;
486                     srcval=srccolors[XGetPixel(bmpImage, x, h)];
487                     dstval|=(X11DRV_DIB_GetNearestIndex
488                              (colors, 2,
489                               srcval.peRed,
490                               srcval.peGreen,
491                               srcval.peBlue) << (7 - (x & 7)));
492                     if ((x&7)==7) {
493                         *dstbyte++=dstval;
494                         dstval=0;
495                     }
496                 }
497                 if ((width&7)!=0) {
498                     *dstbyte++=dstval;
499                 }
500                 /* pad with 0 to DWORD alignment */
501                 for (x = (x+7)&~7; x < ((width + 31) & ~31); x+=8)
502                     *dstbyte++ = 0;
503                 dstbits += linebytes;
504             }
505         } else {
506             goto notsupported;
507         }
508         break;
509
510     case 8:
511         if (X11DRV_DIB_CheckMask(bmpImage->red_mask, bmpImage->green_mask, bmpImage->blue_mask)
512             && srccolors) {
513             /* ==== pal 8 bmp -> pal 1 dib ==== */
514             const void* srcbits;
515             const BYTE* srcpixel;
516             BYTE* dstbyte;
517
518             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
519
520             for (h=0; h<lines; h++) {
521                 BYTE dstval;
522                 srcpixel=srcbits;
523                 dstbyte=dstbits;
524                 dstval=0;
525                 for (x=0; x<width; x++) {
526                     PALETTEENTRY srcval;
527                     srcval=srccolors[*srcpixel++];
528                     dstval|=(X11DRV_DIB_GetNearestIndex
529                              (colors, 2,
530                               srcval.peRed,
531                               srcval.peGreen,
532                               srcval.peBlue) << (7-(x&7)) );
533                     if ((x&7)==7) {
534                         *dstbyte++=dstval;
535                         dstval=0;
536                     }
537                 }
538                 if ((width&7)!=0) {
539                     *dstbyte=dstval;
540                 }
541                 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
542                 dstbits += linebytes;
543             }
544         } else {
545             goto notsupported;
546         }
547         break;
548
549     case 15:
550     case 16:
551         {
552             const void* srcbits;
553             const WORD* srcpixel;
554             BYTE* dstbyte;
555
556             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
557
558             if (bmpImage->green_mask==0x03e0) {
559                 if (bmpImage->red_mask==0x7c00) {
560                     /* ==== rgb 555 bmp -> pal 1 dib ==== */
561                     for (h=0; h<lines; h++) {
562                         BYTE dstval;
563                         srcpixel=srcbits;
564                         dstbyte=dstbits;
565                         dstval=0;
566                         for (x=0; x<width; x++) {
567                             WORD srcval;
568                             srcval=*srcpixel++;
569                             dstval|=(X11DRV_DIB_GetNearestIndex
570                                      (colors, 2,
571                                       ((srcval >>  7) & 0xf8) | /* r */
572                                       ((srcval >> 12) & 0x07),
573                                       ((srcval >>  2) & 0xf8) | /* g */
574                                       ((srcval >>  7) & 0x07),
575                                       ((srcval <<  3) & 0xf8) | /* b */
576                                       ((srcval >>  2) & 0x07) ) << (7-(x&7)) );
577                             if ((x&7)==7) {
578                                 *dstbyte++=dstval;
579                                 dstval=0;
580                             }
581                         }
582                         if ((width&7)!=0) {
583                             *dstbyte=dstval;
584                         }
585                         srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
586                         dstbits += linebytes;
587                     }
588                 } else if (bmpImage->blue_mask==0x7c00) {
589                     /* ==== bgr 555 bmp -> pal 1 dib ==== */
590                     for (h=0; h<lines; h++) {
591                         WORD dstval;
592                         srcpixel=srcbits;
593                         dstbyte=dstbits;
594                         dstval=0;
595                         for (x=0; x<width; x++) {
596                             BYTE srcval;
597                             srcval=*srcpixel++;
598                             dstval|=(X11DRV_DIB_GetNearestIndex
599                                      (colors, 2,
600                                       ((srcval <<  3) & 0xf8) | /* r */
601                                       ((srcval >>  2) & 0x07),
602                                       ((srcval >>  2) & 0xf8) | /* g */
603                                       ((srcval >>  7) & 0x07),
604                                       ((srcval >>  7) & 0xf8) | /* b */
605                                       ((srcval >> 12) & 0x07) ) << (7-(x&7)) );
606                             if ((x&7)==7) {
607                                 *dstbyte++=dstval;
608                                 dstval=0;
609                             }
610                         }
611                         if ((width&7)!=0) {
612                             *dstbyte=dstval;
613                         }
614                         srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
615                         dstbits += linebytes;
616                     }
617                 } else {
618                     goto notsupported;
619                 }
620             } else if (bmpImage->green_mask==0x07e0) {
621                 if (bmpImage->red_mask==0xf800) {
622                     /* ==== rgb 565 bmp -> pal 1 dib ==== */
623                     for (h=0; h<lines; h++) {
624                         BYTE dstval;
625                         srcpixel=srcbits;
626                         dstbyte=dstbits;
627                         dstval=0;
628                         for (x=0; x<width; x++) {
629                             WORD srcval;
630                             srcval=*srcpixel++;
631                             dstval|=(X11DRV_DIB_GetNearestIndex
632                                      (colors, 2,
633                                       ((srcval >>  8) & 0xf8) | /* r */
634                                       ((srcval >> 13) & 0x07),
635                                       ((srcval >>  3) & 0xfc) | /* g */
636                                       ((srcval >>  9) & 0x03),
637                                       ((srcval <<  3) & 0xf8) | /* b */
638                                       ((srcval >>  2) & 0x07) ) << (7-(x&7)) );
639                             if ((x&7)==7) {
640                                 *dstbyte++=dstval;
641                                 dstval=0;
642                             }
643                         }
644                         if ((width&7)!=0) {
645                             *dstbyte=dstval;
646                         }
647                         srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
648                         dstbits += linebytes;
649                     }
650                 } else if (bmpImage->blue_mask==0xf800) {
651                     /* ==== bgr 565 bmp -> pal 1 dib ==== */
652                     for (h=0; h<lines; h++) {
653                         BYTE dstval;
654                         srcpixel=srcbits;
655                         dstbyte=dstbits;
656                         dstval=0;
657                         for (x=0; x<width; x++) {
658                             WORD srcval;
659                             srcval=*srcpixel++;
660                             dstval|=(X11DRV_DIB_GetNearestIndex
661                                      (colors, 2,
662                                       ((srcval <<  3) & 0xf8) | /* r */
663                                       ((srcval >>  2) & 0x07),
664                                       ((srcval >>  3) & 0xfc) | /* g */
665                                       ((srcval >>  9) & 0x03),
666                                       ((srcval >>  8) & 0xf8) | /* b */
667                                       ((srcval >> 13) & 0x07) ) << (7-(x&7)) );
668                             if ((x&7)==7) {
669                                 *dstbyte++=dstval;
670                                 dstval=0;
671                             }
672                         }
673                         if ((width&7)!=0) {
674                             *dstbyte=dstval;
675                         }
676                         srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
677                         dstbits += linebytes;
678                     }
679                 } else {
680                     goto notsupported;
681                 }
682             } else {
683                 goto notsupported;
684             }
685         }
686         break;
687
688     case 24:
689     case 32:
690         {
691             const void* srcbits;
692             const BYTE *srcbyte;
693             BYTE* dstbyte;
694             int bytes_per_pixel;
695
696             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
697             bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
698
699             if (bmpImage->green_mask!=0x00ff00 ||
700                 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
701                 goto notsupported;
702             } else if (bmpImage->blue_mask==0xff) {
703                 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
704                 for (h=0; h<lines; h++) {
705                     BYTE dstval;
706                     srcbyte=srcbits;
707                     dstbyte=dstbits;
708                     dstval=0;
709                     for (x=0; x<width; x++) {
710                         dstval|=(X11DRV_DIB_GetNearestIndex
711                                  (colors, 2,
712                                   srcbyte[2],
713                                   srcbyte[1],
714                                   srcbyte[0]) << (7-(x&7)) );
715                         srcbyte+=bytes_per_pixel;
716                         if ((x&7)==7) {
717                             *dstbyte++=dstval;
718                             dstval=0;
719                         }
720                     }
721                     if ((width&7)!=0) {
722                         *dstbyte=dstval;
723                     }
724                     srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
725                     dstbits += linebytes;
726                 }
727             } else {
728                 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
729                 for (h=0; h<lines; h++) {
730                     BYTE dstval;
731                     srcbyte=srcbits;
732                     dstbyte=dstbits;
733                     dstval=0;
734                     for (x=0; x<width; x++) {
735                         dstval|=(X11DRV_DIB_GetNearestIndex
736                                  (colors, 2,
737                                   srcbyte[0],
738                                   srcbyte[1],
739                                   srcbyte[2]) << (7-(x&7)) );
740                         srcbyte+=bytes_per_pixel;
741                         if ((x&7)==7) {
742                             *dstbyte++=dstval;
743                             dstval=0;
744                         }
745                     }
746                     if ((width&7)!=0) {
747                         *dstbyte=dstval;
748                     }
749                     srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
750                     dstbits += linebytes;
751                 }
752             }
753         }
754         break;
755
756     default:
757     notsupported:
758         {
759             BYTE* dstbyte;
760             BYTE neg = 0;
761             unsigned long white = (1 << bmpImage->bits_per_pixel) - 1;
762
763             /* ==== any bmp format -> pal 1 dib ==== */
764             if ((unsigned)colors[0].rgbRed+colors[0].rgbGreen+colors[0].rgbBlue >=
765                 (unsigned)colors[1].rgbRed+colors[1].rgbGreen+colors[1].rgbBlue )
766                 neg = 1;
767
768             WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB, "
769                  "%s color mapping\n",
770                   bmpImage->bits_per_pixel, bmpImage->red_mask,
771                   bmpImage->green_mask, bmpImage->blue_mask,
772                   neg?"negative":"direct" );
773
774             for (h=lines-1; h>=0; h--) {
775                 BYTE dstval;
776                 dstbyte=dstbits;
777                 dstval=0;
778                 for (x=0; x<width; x++) {
779                     dstval|=((XGetPixel( bmpImage, x, h) >= white) ^ neg) << (7 - (x&7));
780                     if ((x&7)==7) {
781                         *dstbyte++=dstval;
782                         dstval=0;
783                     }
784                 }
785                 if ((width&7)!=0) {
786                     *dstbyte=dstval;
787                 }
788                 dstbits += linebytes;
789             }
790         }
791         break;
792     }
793 }
794
795 /***********************************************************************
796  *           X11DRV_DIB_SetImageBits_4
797  *
798  * SetDIBits for a 4-bit deep DIB.
799  */
800 static void X11DRV_DIB_SetImageBits_4( int lines, const BYTE *srcbits,
801                                 DWORD srcwidth, DWORD dstwidth, int left,
802                                 int *colors, XImage *bmpImage, int linebytes)
803 {
804     int h, width;
805     const BYTE* srcbyte;
806     DWORD i, x;
807
808     if (lines < 0 ) {
809         lines = -lines;
810         srcbits = srcbits + linebytes * (lines - 1);
811         linebytes = -linebytes;
812     }
813
814     if (left & 1) {
815         left--;
816         dstwidth++;
817     }
818     srcbits += left >> 1;
819     width = min(srcwidth, dstwidth);
820
821     /* ==== pal 4 dib -> any bmp format ==== */
822     for (h = lines-1; h >= 0; h--) {
823         srcbyte=srcbits;
824         for (i = width/2, x = left; i > 0; i--) {
825             BYTE srcval=*srcbyte++;
826             XPutPixel( bmpImage, x++, h, colors[srcval >> 4] );
827             XPutPixel( bmpImage, x++, h, colors[srcval & 0x0f] );
828         }
829         if (width & 1)
830             XPutPixel( bmpImage, x, h, colors[*srcbyte >> 4] );
831         srcbits += linebytes;
832     }
833 }
834
835
836
837 /***********************************************************************
838  *           X11DRV_DIB_GetImageBits_4
839  *
840  * GetDIBits for a 4-bit deep DIB.
841  */
842 static void X11DRV_DIB_GetImageBits_4( int lines, BYTE *dstbits,
843                                        DWORD srcwidth, DWORD dstwidth,
844                                        RGBQUAD *colors, PALETTEENTRY *srccolors,
845                                        XImage *bmpImage, int linebytes )
846 {
847     DWORD x;
848     int h, width = min(srcwidth, dstwidth);
849
850     if (lines < 0 )
851     {
852        lines = -lines;
853        dstbits = dstbits + ( linebytes * (lines-1) );
854        linebytes = -linebytes;
855     }
856
857     switch (bmpImage->depth) {
858     case 1:
859     case 4:
860         if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
861             && srccolors) {
862             /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
863             BYTE* dstbyte;
864
865             for (h = lines-1; h >= 0; h--) {
866                 BYTE dstval;
867                 dstbyte=dstbits;
868                 dstval=0;
869                 for (x = 0; x < width; x++) {
870                     PALETTEENTRY srcval;
871                     srcval=srccolors[XGetPixel(bmpImage, x, h)];
872                     dstval|=(X11DRV_DIB_GetNearestIndex
873                              (colors, 16,
874                               srcval.peRed,
875                               srcval.peGreen,
876                               srcval.peBlue) << (4-((x&1)<<2)));
877                     if ((x&1)==1) {
878                         *dstbyte++=dstval;
879                         dstval=0;
880                     }
881                 }
882                 if ((width&1)!=0) {
883                     *dstbyte=dstval;
884                 }
885                 dstbits += linebytes;
886             }
887         } else {
888             goto notsupported;
889         }
890         break;
891
892     case 8:
893         if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
894             && srccolors) {
895             /* ==== pal 8 bmp -> pal 4 dib ==== */
896             const void* srcbits;
897             const BYTE *srcpixel;
898             BYTE* dstbyte;
899
900             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
901             for (h=0; h<lines; h++) {
902                 BYTE dstval;
903                 srcpixel=srcbits;
904                 dstbyte=dstbits;
905                 dstval=0;
906                 for (x=0; x<width; x++) {
907                     PALETTEENTRY srcval;
908                     srcval = srccolors[*srcpixel++];
909                     dstval|=(X11DRV_DIB_GetNearestIndex
910                              (colors, 16,
911                               srcval.peRed,
912                               srcval.peGreen,
913                               srcval.peBlue) << (4*(1-(x&1))) );
914                     if ((x&1)==1) {
915                         *dstbyte++=dstval;
916                         dstval=0;
917                     }
918                 }
919                 if ((width&1)!=0) {
920                     *dstbyte=dstval;
921                 }
922                 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
923                 dstbits += linebytes;
924             }
925         } else {
926             goto notsupported;
927         }
928         break;
929
930     case 15:
931     case 16:
932         {
933             const void* srcbits;
934             const WORD* srcpixel;
935             BYTE* dstbyte;
936
937             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
938
939             if (bmpImage->green_mask==0x03e0) {
940                 if (bmpImage->red_mask==0x7c00) {
941                     /* ==== rgb 555 bmp -> pal 4 dib ==== */
942                     for (h=0; h<lines; h++) {
943                         BYTE dstval;
944                         srcpixel=srcbits;
945                         dstbyte=dstbits;
946                         dstval=0;
947                         for (x=0; x<width; x++) {
948                             WORD srcval;
949                             srcval=*srcpixel++;
950                             dstval|=(X11DRV_DIB_GetNearestIndex
951                                      (colors, 16,
952                                       ((srcval >>  7) & 0xf8) | /* r */
953                                       ((srcval >> 12) & 0x07),
954                                       ((srcval >>  2) & 0xf8) | /* g */
955                                       ((srcval >>  7) & 0x07),
956                                       ((srcval <<  3) & 0xf8) | /* b */
957                                       ((srcval >>  2) & 0x07) ) << ((1-(x&1))<<2) );
958                             if ((x&1)==1) {
959                                 *dstbyte++=dstval;
960                                 dstval=0;
961                             }
962                         }
963                         if ((width&1)!=0) {
964                             *dstbyte=dstval;
965                         }
966                         srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
967                         dstbits += linebytes;
968                     }
969                 } else if (bmpImage->blue_mask==0x7c00) {
970                     /* ==== bgr 555 bmp -> pal 4 dib ==== */
971                     for (h=0; h<lines; h++) {
972                         WORD dstval;
973                         srcpixel=srcbits;
974                         dstbyte=dstbits;
975                         dstval=0;
976                         for (x=0; x<width; x++) {
977                             WORD srcval;
978                             srcval=*srcpixel++;
979                             dstval|=(X11DRV_DIB_GetNearestIndex
980                                      (colors, 16,
981                                       ((srcval <<  3) & 0xf8) | /* r */
982                                       ((srcval >>  2) & 0x07),
983                                       ((srcval >>  2) & 0xf8) | /* g */
984                                       ((srcval >>  7) & 0x07),
985                                       ((srcval >>  7) & 0xf8) | /* b */
986                                       ((srcval >> 12) & 0x07) ) << ((1-(x&1))<<2) );
987                             if ((x&1)==1) {
988                                 *dstbyte++=dstval;
989                                 dstval=0;
990                             }
991                         }
992                         if ((width&1)!=0) {
993                             *dstbyte=dstval;
994                         }
995                         srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
996                         dstbits += linebytes;
997                     }
998                 } else {
999                     goto notsupported;
1000                 }
1001             } else if (bmpImage->green_mask==0x07e0) {
1002                 if (bmpImage->red_mask==0xf800) {
1003                     /* ==== rgb 565 bmp -> pal 4 dib ==== */
1004                     for (h=0; h<lines; h++) {
1005                         BYTE dstval;
1006                         srcpixel=srcbits;
1007                         dstbyte=dstbits;
1008                         dstval=0;
1009                         for (x=0; x<width; x++) {
1010                             WORD srcval;
1011                             srcval=*srcpixel++;
1012                             dstval|=(X11DRV_DIB_GetNearestIndex
1013                                      (colors, 16,
1014                                       ((srcval >>  8) & 0xf8) | /* r */
1015                                       ((srcval >> 13) & 0x07),
1016                                       ((srcval >>  3) & 0xfc) | /* g */
1017                                       ((srcval >>  9) & 0x03),
1018                                       ((srcval <<  3) & 0xf8) | /* b */
1019                                       ((srcval >>  2) & 0x07) ) << ((1-(x&1))<<2) );
1020                             if ((x&1)==1) {
1021                                 *dstbyte++=dstval;
1022                                 dstval=0;
1023                             }
1024                         }
1025                         if ((width&1)!=0) {
1026                             *dstbyte=dstval;
1027                         }
1028                         srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1029                         dstbits += linebytes;
1030                     }
1031                 } else if (bmpImage->blue_mask==0xf800) {
1032                     /* ==== bgr 565 bmp -> pal 4 dib ==== */
1033                     for (h=0; h<lines; h++) {
1034                         WORD dstval;
1035                         srcpixel=srcbits;
1036                         dstbyte=dstbits;
1037                         dstval=0;
1038                         for (x=0; x<width; x++) {
1039                             WORD srcval;
1040                             srcval=*srcpixel++;
1041                             dstval|=(X11DRV_DIB_GetNearestIndex
1042                                      (colors, 16,
1043                                       ((srcval <<  3) & 0xf8) | /* r */
1044                                       ((srcval >>  2) & 0x07),
1045                                       ((srcval >>  3) & 0xfc) | /* g */
1046                                       ((srcval >>  9) & 0x03),
1047                                       ((srcval >>  8) & 0xf8) | /* b */
1048                                       ((srcval >> 13) & 0x07) ) << ((1-(x&1))<<2) );
1049                             if ((x&1)==1) {
1050                                 *dstbyte++=dstval;
1051                                 dstval=0;
1052                             }
1053                         }
1054                         if ((width&1)!=0) {
1055                             *dstbyte=dstval;
1056                         }
1057                         srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1058                         dstbits += linebytes;
1059                     }
1060                 } else {
1061                     goto notsupported;
1062                 }
1063             } else {
1064                 goto notsupported;
1065             }
1066         }
1067         break;
1068
1069     case 24:
1070         if (bmpImage->bits_per_pixel==24) {
1071             const void* srcbits;
1072             const BYTE *srcbyte;
1073             BYTE* dstbyte;
1074
1075             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1076
1077             if (bmpImage->green_mask!=0x00ff00 ||
1078                 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1079                 goto notsupported;
1080             } else if (bmpImage->blue_mask==0xff) {
1081                 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1082                 for (h=0; h<lines; h++) {
1083                     srcbyte=srcbits;
1084                     dstbyte=dstbits;
1085                     for (x=0; x<width/2; x++) {
1086                         /* Do 2 pixels at a time */
1087                         *dstbyte++=(X11DRV_DIB_GetNearestIndex
1088                                     (colors, 16,
1089                                      srcbyte[2],
1090                                      srcbyte[1],
1091                                      srcbyte[0]) << 4) |
1092                                     X11DRV_DIB_GetNearestIndex
1093                                     (colors, 16,
1094                                      srcbyte[5],
1095                                      srcbyte[4],
1096                                      srcbyte[3]);
1097                         srcbyte+=6;
1098                     }
1099                     if (width&1) {
1100                         /* And then the odd pixel */
1101                         *dstbyte++=(X11DRV_DIB_GetNearestIndex
1102                                     (colors, 16,
1103                                      srcbyte[2],
1104                                      srcbyte[1],
1105                                      srcbyte[0]) << 4);
1106                     }
1107                     srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1108                     dstbits += linebytes;
1109                 }
1110             } else {
1111                 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1112                 for (h=0; h<lines; h++) {
1113                     srcbyte=srcbits;
1114                     dstbyte=dstbits;
1115                     for (x=0; x<width/2; x++) {
1116                         /* Do 2 pixels at a time */
1117                         *dstbyte++=(X11DRV_DIB_GetNearestIndex
1118                                     (colors, 16,
1119                                      srcbyte[0],
1120                                      srcbyte[1],
1121                                      srcbyte[2]) << 4) |
1122                                     X11DRV_DIB_GetNearestIndex
1123                                     (colors, 16,
1124                                      srcbyte[3],
1125                                      srcbyte[4],
1126                                      srcbyte[5]);
1127                         srcbyte+=6;
1128                     }
1129                     if (width&1) {
1130                         /* And then the odd pixel */
1131                         *dstbyte++=(X11DRV_DIB_GetNearestIndex
1132                                     (colors, 16,
1133                                      srcbyte[0],
1134                                      srcbyte[1],
1135                                      srcbyte[2]) << 4);
1136                     }
1137                     srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1138                     dstbits += linebytes;
1139                 }
1140             }
1141             break;
1142         }
1143         /* Fall through */
1144
1145     case 32:
1146         {
1147             const void* srcbits;
1148             const BYTE *srcbyte;
1149             BYTE* dstbyte;
1150
1151             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1152
1153             if (bmpImage->green_mask!=0x00ff00 ||
1154                 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1155                 goto notsupported;
1156             } else if (bmpImage->blue_mask==0xff) {
1157                 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1158                 for (h=0; h<lines; h++) {
1159                     srcbyte=srcbits;
1160                     dstbyte=dstbits;
1161                     for (x=0; x<width/2; x++) {
1162                         /* Do 2 pixels at a time */
1163                         *dstbyte++=(X11DRV_DIB_GetNearestIndex
1164                                     (colors, 16,
1165                                      srcbyte[2],
1166                                      srcbyte[1],
1167                                      srcbyte[0]) << 4) |
1168                                     X11DRV_DIB_GetNearestIndex
1169                                     (colors, 16,
1170                                      srcbyte[6],
1171                                      srcbyte[5],
1172                                      srcbyte[4]);
1173                         srcbyte+=8;
1174                     }
1175                     if (width&1) {
1176                         /* And then the odd pixel */
1177                         *dstbyte++=(X11DRV_DIB_GetNearestIndex
1178                                     (colors, 16,
1179                                      srcbyte[2],
1180                                      srcbyte[1],
1181                                      srcbyte[0]) << 4);
1182                     }
1183                     srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1184                     dstbits += linebytes;
1185                 }
1186             } else {
1187                 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1188                 for (h=0; h<lines; h++) {
1189                     srcbyte=srcbits;
1190                     dstbyte=dstbits;
1191                     for (x=0; x<width/2; x++) {
1192                         /* Do 2 pixels at a time */
1193                         *dstbyte++=(X11DRV_DIB_GetNearestIndex
1194                                     (colors, 16,
1195                                      srcbyte[0],
1196                                      srcbyte[1],
1197                                      srcbyte[2]) << 4) |
1198                                     X11DRV_DIB_GetNearestIndex
1199                                     (colors, 16,
1200                                      srcbyte[4],
1201                                      srcbyte[5],
1202                                      srcbyte[6]);
1203                         srcbyte+=8;
1204                     }
1205                     if (width&1) {
1206                         /* And then the odd pixel */
1207                         *dstbyte++=(X11DRV_DIB_GetNearestIndex
1208                                     (colors, 16,
1209                                      srcbyte[0],
1210                                      srcbyte[1],
1211                                      srcbyte[2]) << 4);
1212                     }
1213                     srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1214                     dstbits += linebytes;
1215                 }
1216             }
1217         }
1218         break;
1219
1220     default:
1221     notsupported:
1222         {
1223             BYTE* dstbyte;
1224
1225             /* ==== any bmp format -> pal 4 dib ==== */
1226             WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1227                   bmpImage->bits_per_pixel, bmpImage->red_mask,
1228                   bmpImage->green_mask, bmpImage->blue_mask );
1229             for (h=lines-1; h>=0; h--) {
1230                 dstbyte=dstbits;
1231                 for (x=0; x<(width & ~1); x+=2) {
1232                     *dstbyte++=(X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4) |
1233                         X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x+1, h), 0);
1234                 }
1235                 if (width & 1) {
1236                     *dstbyte=(X11DRV_DIB_MapColor((int *)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4);
1237                 }
1238                 dstbits += linebytes;
1239             }
1240         }
1241         break;
1242     }
1243 }
1244
1245 /***********************************************************************
1246  *           X11DRV_DIB_SetImageBits_8
1247  *
1248  * SetDIBits for an 8-bit deep DIB.
1249  */
1250 static void X11DRV_DIB_SetImageBits_8( int lines, const BYTE *srcbits,
1251                                 DWORD srcwidth, DWORD dstwidth, int left,
1252                                 const int *colors, XImage *bmpImage,
1253                                 int linebytes )
1254 {
1255     DWORD x;
1256     int h, width = min(srcwidth, dstwidth);
1257     const BYTE* srcbyte;
1258     BYTE* dstbits;
1259
1260     if (lines < 0 )
1261     {
1262         lines = -lines;
1263         srcbits = srcbits + linebytes * (lines-1);
1264         linebytes = -linebytes;
1265     }
1266     srcbits += left;
1267     srcbyte = srcbits;
1268
1269     switch (bmpImage->depth) {
1270     case 15:
1271     case 16:
1272         /* Some X servers might have 32 bit/ 16bit deep pixel */
1273         if (lines && width && (bmpImage->bits_per_pixel == 16) &&
1274             (ImageByteOrder(gdi_display)==LSBFirst) )
1275         {
1276             /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1277             dstbits=(BYTE*)bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1278             for (h = lines ; h--; ) {
1279                 DWORD* dstpixel=(DWORD*)dstbits;
1280                 for (x=0; x<width/2; x++) {
1281                     /* Do 2 pixels at a time */
1282                     *dstpixel++=(colors[srcbyte[1]] << 16) | colors[srcbyte[0]];
1283                     srcbyte+=2;
1284                 }
1285                 if (width&1) {
1286                     /* And then the odd pixel */
1287                     *((WORD*)dstpixel)=colors[srcbyte[0]];
1288                 }
1289                 srcbyte = (srcbits += linebytes);
1290                 dstbits -= bmpImage->bytes_per_line;
1291             }
1292             return;
1293         }
1294         break;
1295     case 24:
1296     case 32:
1297         if (lines && width && (bmpImage->bits_per_pixel == 32) &&
1298             (ImageByteOrder(gdi_display)==LSBFirst) )
1299         {
1300             dstbits=(BYTE*)bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
1301             /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1302             for (h = lines ; h--; ) {
1303                 DWORD* dstpixel=(DWORD*)dstbits;
1304                 for (x=0; x<width; x++) {
1305                     *dstpixel++=colors[*srcbyte++];
1306                 }
1307                 srcbyte = (srcbits += linebytes);
1308                 dstbits -= bmpImage->bytes_per_line;
1309             }
1310             return;
1311         }
1312         break;
1313     default:
1314         break; /* use slow generic case below */
1315     }
1316
1317     /* ==== pal 8 dib -> any bmp format ==== */
1318     for (h=lines-1; h>=0; h--) {
1319         for (x=left; x<width+left; x++) {
1320             XPutPixel(bmpImage, x, h, colors[*srcbyte++]);
1321         }
1322         srcbyte = (srcbits += linebytes);
1323     }
1324 }
1325
1326 /***********************************************************************
1327  *           X11DRV_DIB_GetImageBits_8
1328  *
1329  * GetDIBits for an 8-bit deep DIB.
1330  */
1331 static void X11DRV_DIB_GetImageBits_8( int lines, BYTE *dstbits,
1332                                        DWORD srcwidth, DWORD dstwidth,
1333                                        RGBQUAD *colors, PALETTEENTRY *srccolors,
1334                                        XImage *bmpImage, int linebytes )
1335 {
1336     DWORD x;
1337     int h, width = min(srcwidth, dstwidth);
1338     BYTE* dstbyte;
1339
1340     if (lines < 0 )
1341     {
1342        lines = -lines;
1343        dstbits = dstbits + ( linebytes * (lines-1) );
1344        linebytes = -linebytes;
1345     }
1346
1347     /*
1348      * Hack for now
1349      * This condition is true when GetImageBits has been called by
1350      * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1351      * 256 colormaps, so we'll just use it for GetDIBits calls.
1352      * (In some cases, in an updateDIBSection, the returned colors are bad too)
1353      */
1354     if (!srccolors) goto updatesection;
1355
1356     switch (bmpImage->depth) {
1357     case 1:
1358     case 4:
1359         if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1360             && srccolors) {
1361
1362             /* ==== pal 1 bmp -> pal 8 dib ==== */
1363             /* ==== pal 4 bmp -> pal 8 dib ==== */
1364             for (h=lines-1; h>=0; h--) {
1365                 dstbyte=dstbits;
1366                 for (x=0; x<width; x++) {
1367                     PALETTEENTRY srcval;
1368                     srcval=srccolors[XGetPixel(bmpImage, x, h)];
1369                     *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1370                                                           srcval.peRed,
1371                                                           srcval.peGreen,
1372                                                           srcval.peBlue);
1373                 }
1374                 dstbits += linebytes;
1375             }
1376         } else {
1377             goto notsupported;
1378         }
1379         break;
1380
1381     case 8:
1382        if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1383            && srccolors) {
1384             /* ==== pal 8 bmp -> pal 8 dib ==== */
1385            const void* srcbits;
1386            const BYTE* srcpixel;
1387
1388            srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1389            for (h=0; h<lines; h++) {
1390                srcpixel=srcbits;
1391                dstbyte=dstbits;
1392                for (x = 0; x < width; x++) {
1393                    PALETTEENTRY srcval;
1394                    srcval=srccolors[*srcpixel++];
1395                    *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1396                                                          srcval.peRed,
1397                                                          srcval.peGreen,
1398                                                          srcval.peBlue);
1399                }
1400                srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1401                dstbits += linebytes;
1402            }
1403        } else {
1404            goto notsupported;
1405        }
1406        break;
1407
1408     case 15:
1409     case 16:
1410         {
1411             const void* srcbits;
1412             const WORD* srcpixel;
1413             BYTE* dstbyte;
1414
1415             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1416
1417             if (bmpImage->green_mask==0x03e0) {
1418                 if (bmpImage->red_mask==0x7c00) {
1419                     /* ==== rgb 555 bmp -> pal 8 dib ==== */
1420                     for (h=0; h<lines; h++) {
1421                         srcpixel=srcbits;
1422                         dstbyte=dstbits;
1423                         for (x=0; x<width; x++) {
1424                             WORD srcval;
1425                             srcval=*srcpixel++;
1426                             *dstbyte++=X11DRV_DIB_GetNearestIndex
1427                                 (colors, 256,
1428                                  ((srcval >>  7) & 0xf8) | /* r */
1429                                  ((srcval >> 12) & 0x07),
1430                                  ((srcval >>  2) & 0xf8) | /* g */
1431                                  ((srcval >>  7) & 0x07),
1432                                  ((srcval <<  3) & 0xf8) | /* b */
1433                                  ((srcval >>  2) & 0x07) );
1434                         }
1435                         srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1436                         dstbits += linebytes;
1437                     }
1438                 } else if (bmpImage->blue_mask==0x7c00) {
1439                     /* ==== bgr 555 bmp -> pal 8 dib ==== */
1440                     for (h=0; h<lines; h++) {
1441                         srcpixel=srcbits;
1442                         dstbyte=dstbits;
1443                         for (x=0; x<width; x++) {
1444                             WORD srcval;
1445                             srcval=*srcpixel++;
1446                             *dstbyte++=X11DRV_DIB_GetNearestIndex
1447                                 (colors, 256,
1448                                  ((srcval <<  3) & 0xf8) | /* r */
1449                                  ((srcval >>  2) & 0x07),
1450                                  ((srcval >>  2) & 0xf8) | /* g */
1451                                  ((srcval >>  7) & 0x07),
1452                                  ((srcval >>  7) & 0xf8) | /* b */
1453                                  ((srcval >> 12) & 0x07) );
1454                         }
1455                         srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1456                         dstbits += linebytes;
1457                     }
1458                 } else {
1459                     goto notsupported;
1460                 }
1461             } else if (bmpImage->green_mask==0x07e0) {
1462                 if (bmpImage->red_mask==0xf800) {
1463                     /* ==== rgb 565 bmp -> pal 8 dib ==== */
1464                     for (h=0; h<lines; h++) {
1465                         srcpixel=srcbits;
1466                         dstbyte=dstbits;
1467                         for (x=0; x<width; x++) {
1468                             WORD srcval;
1469                             srcval=*srcpixel++;
1470                             *dstbyte++=X11DRV_DIB_GetNearestIndex
1471                                 (colors, 256,
1472                                  ((srcval >>  8) & 0xf8) | /* r */
1473                                  ((srcval >> 13) & 0x07),
1474                                  ((srcval >>  3) & 0xfc) | /* g */
1475                                  ((srcval >>  9) & 0x03),
1476                                  ((srcval <<  3) & 0xf8) | /* b */
1477                                  ((srcval >>  2) & 0x07) );
1478                         }
1479                         srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1480                         dstbits += linebytes;
1481                     }
1482                 } else if (bmpImage->blue_mask==0xf800) {
1483                     /* ==== bgr 565 bmp -> pal 8 dib ==== */
1484                     for (h=0; h<lines; h++) {
1485                         srcpixel=srcbits;
1486                         dstbyte=dstbits;
1487                         for (x=0; x<width; x++) {
1488                             WORD srcval;
1489                             srcval=*srcpixel++;
1490                             *dstbyte++=X11DRV_DIB_GetNearestIndex
1491                                 (colors, 256,
1492                                  ((srcval <<  3) & 0xf8) | /* r */
1493                                  ((srcval >>  2) & 0x07),
1494                                  ((srcval >>  3) & 0xfc) | /* g */
1495                                  ((srcval >>  9) & 0x03),
1496                                  ((srcval >>  8) & 0xf8) | /* b */
1497                                  ((srcval >> 13) & 0x07) );
1498                         }
1499                         srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1500                         dstbits += linebytes;
1501                     }
1502                 } else {
1503                     goto notsupported;
1504                 }
1505             } else {
1506                 goto notsupported;
1507             }
1508         }
1509         break;
1510
1511     case 24:
1512     case 32:
1513         {
1514             const void* srcbits;
1515             const BYTE *srcbyte;
1516             BYTE* dstbyte;
1517             int bytes_per_pixel;
1518
1519             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1520             bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
1521
1522             if (bmpImage->green_mask!=0x00ff00 ||
1523                 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1524                 goto notsupported;
1525             } else if (bmpImage->blue_mask==0xff) {
1526                 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1527                 for (h=0; h<lines; h++) {
1528                     srcbyte=srcbits;
1529                     dstbyte=dstbits;
1530                     for (x=0; x<width; x++) {
1531                         *dstbyte++=X11DRV_DIB_GetNearestIndex
1532                             (colors, 256,
1533                              srcbyte[2],
1534                              srcbyte[1],
1535                              srcbyte[0]);
1536                         srcbyte+=bytes_per_pixel;
1537                     }
1538                     srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1539                     dstbits += linebytes;
1540                 }
1541             } else {
1542                 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1543                 for (h=0; h<lines; h++) {
1544                     srcbyte=srcbits;
1545                     dstbyte=dstbits;
1546                     for (x=0; x<width; x++) {
1547                         *dstbyte++=X11DRV_DIB_GetNearestIndex
1548                             (colors, 256,
1549                              srcbyte[0],
1550                              srcbyte[1],
1551                              srcbyte[2]);
1552                         srcbyte+=bytes_per_pixel;
1553                     }
1554                     srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1555                     dstbits += linebytes;
1556                 }
1557             }
1558         }
1559         break;
1560
1561     default:
1562     notsupported:
1563         WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1564               bmpImage->depth, bmpImage->red_mask,
1565               bmpImage->green_mask, bmpImage->blue_mask );
1566     updatesection:
1567         /* ==== any bmp format -> pal 8 dib ==== */
1568         for (h=lines-1; h>=0; h--) {
1569             dstbyte=dstbits;
1570             for (x=0; x<width; x++) {
1571                 *dstbyte=X11DRV_DIB_MapColor
1572                     ((int*)colors, 256,
1573                      XGetPixel(bmpImage, x, h), *dstbyte);
1574                 dstbyte++;
1575             }
1576             dstbits += linebytes;
1577         }
1578         break;
1579     }
1580 }
1581
1582 /***********************************************************************
1583  *           X11DRV_DIB_SetImageBits_16
1584  *
1585  * SetDIBits for a 16-bit deep DIB.
1586  */
1587 static void X11DRV_DIB_SetImageBits_16( int lines, const BYTE *srcbits,
1588                                         DWORD srcwidth, DWORD dstwidth, int left,
1589                                         X11DRV_PDEVICE *physDev, DWORD rSrc, DWORD gSrc, DWORD bSrc,
1590                                         XImage *bmpImage, int linebytes )
1591 {
1592     DWORD x;
1593     int h, width = min(srcwidth, dstwidth);
1594     const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
1595
1596     if (lines < 0 )
1597     {
1598         lines = -lines;
1599         srcbits = srcbits + ( linebytes * (lines-1));
1600         linebytes = -linebytes;
1601     }
1602
1603     switch (bmpImage->depth)
1604     {
1605     case 15:
1606     case 16:
1607         {
1608             char* dstbits;
1609
1610             srcbits=srcbits+left*2;
1611             dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1612
1613             if (bmpImage->green_mask==0x03e0) {
1614                 if (gSrc==bmpImage->green_mask) {
1615                     if (rSrc==bmpImage->red_mask) {
1616                         /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1617                         /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1618                         convs->Convert_5x5_asis
1619                             (width,lines,
1620                              srcbits,linebytes,
1621                              dstbits,-bmpImage->bytes_per_line);
1622                     } else if (rSrc==bmpImage->blue_mask) {
1623                         /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1624                         /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1625                         convs->Convert_555_reverse
1626                             (width,lines,
1627                              srcbits,linebytes,
1628                              dstbits,-bmpImage->bytes_per_line);
1629                     }
1630                 } else {
1631                     if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1632                         /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1633                         /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1634                         convs->Convert_565_to_555_asis
1635                             (width,lines,
1636                              srcbits,linebytes,
1637                              dstbits,-bmpImage->bytes_per_line);
1638                     } else {
1639                         /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1640                         /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1641                         convs->Convert_565_to_555_reverse
1642                             (width,lines,
1643                              srcbits,linebytes,
1644                              dstbits,-bmpImage->bytes_per_line);
1645                     }
1646                 }
1647             } else if (bmpImage->green_mask==0x07e0) {
1648                 if (gSrc==bmpImage->green_mask) {
1649                     if (rSrc==bmpImage->red_mask) {
1650                         /* ==== rgb 565 dib -> rgb 565 bmp ==== */
1651                         /* ==== bgr 565 dib -> bgr 565 bmp ==== */
1652                         convs->Convert_5x5_asis
1653                             (width,lines,
1654                              srcbits,linebytes,
1655                              dstbits,-bmpImage->bytes_per_line);
1656                     } else {
1657                         /* ==== rgb 565 dib -> bgr 565 bmp ==== */
1658                         /* ==== bgr 565 dib -> rgb 565 bmp ==== */
1659                         convs->Convert_565_reverse
1660                             (width,lines,
1661                              srcbits,linebytes,
1662                              dstbits,-bmpImage->bytes_per_line);
1663                     }
1664                 } else {
1665                     if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1666                         /* ==== rgb 555 dib -> rgb 565 bmp ==== */
1667                         /* ==== bgr 555 dib -> bgr 565 bmp ==== */
1668                         convs->Convert_555_to_565_asis
1669                             (width,lines,
1670                              srcbits,linebytes,
1671                              dstbits,-bmpImage->bytes_per_line);
1672                     } else {
1673                         /* ==== rgb 555 dib -> bgr 565 bmp ==== */
1674                         /* ==== bgr 555 dib -> rgb 565 bmp ==== */
1675                         convs->Convert_555_to_565_reverse
1676                             (width,lines,
1677                              srcbits,linebytes,
1678                              dstbits,-bmpImage->bytes_per_line);
1679                     }
1680                 }
1681             } else {
1682                 goto notsupported;
1683             }
1684         }
1685         break;
1686
1687     case 24:
1688         if (bmpImage->bits_per_pixel==24) {
1689             char* dstbits;
1690
1691             srcbits=srcbits+left*2;
1692             dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
1693
1694             if (bmpImage->green_mask!=0x00ff00 ||
1695                 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1696                 goto notsupported;
1697             } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
1698                        (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
1699                 if (gSrc==0x03e0) {
1700                     /* ==== rgb 555 dib -> rgb 888 bmp ==== */
1701                     /* ==== bgr 555 dib -> bgr 888 bmp ==== */
1702                     convs->Convert_555_to_888_asis
1703                         (width,lines,
1704                          srcbits,linebytes,
1705                          dstbits,-bmpImage->bytes_per_line);
1706                 } else {
1707                     /* ==== rgb 565 dib -> rgb 888 bmp ==== */
1708                     /* ==== bgr 565 dib -> bgr 888 bmp ==== */
1709                     convs->Convert_565_to_888_asis
1710                         (width,lines,
1711                          srcbits,linebytes,
1712                          dstbits,-bmpImage->bytes_per_line);
1713                 }
1714             } else {
1715                 if (gSrc==0x03e0) {
1716                     /* ==== rgb 555 dib -> bgr 888 bmp ==== */
1717                     /* ==== bgr 555 dib -> rgb 888 bmp ==== */
1718                     convs->Convert_555_to_888_reverse
1719                         (width,lines,
1720                          srcbits,linebytes,
1721                          dstbits,-bmpImage->bytes_per_line);
1722                 } else {
1723                     /* ==== rgb 565 dib -> bgr 888 bmp ==== */
1724                     /* ==== bgr 565 dib -> rgb 888 bmp ==== */
1725                     convs->Convert_565_to_888_reverse
1726                         (width,lines,
1727                          srcbits,linebytes,
1728                          dstbits,-bmpImage->bytes_per_line);
1729                 }
1730             }
1731             break;
1732         }
1733         /* Fall through */
1734
1735     case 32:
1736         {
1737             char* dstbits;
1738
1739             srcbits=srcbits+left*2;
1740             dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
1741
1742             if (bmpImage->green_mask!=0x00ff00 ||
1743                 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1744                 goto notsupported;
1745             } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
1746                        (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
1747                 if (gSrc==0x03e0) {
1748                     /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
1749                     /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
1750                     convs->Convert_555_to_0888_asis
1751                         (width,lines,
1752                          srcbits,linebytes,
1753                          dstbits,-bmpImage->bytes_per_line);
1754                 } else {
1755                     /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
1756                     /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
1757                     convs->Convert_565_to_0888_asis
1758                         (width,lines,
1759                          srcbits,linebytes,
1760                          dstbits,-bmpImage->bytes_per_line);
1761                 }
1762             } else {
1763                 if (gSrc==0x03e0) {
1764                     /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
1765                     /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
1766                     convs->Convert_555_to_0888_reverse
1767                         (width,lines,
1768                          srcbits,linebytes,
1769                          dstbits,-bmpImage->bytes_per_line);
1770                 } else {
1771                     /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
1772                     /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
1773                     convs->Convert_565_to_0888_reverse
1774                         (width,lines,
1775                          srcbits,linebytes,
1776                          dstbits,-bmpImage->bytes_per_line);
1777                 }
1778             }
1779         }
1780         break;
1781
1782     default:
1783     notsupported:
1784         WARN("from 16 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
1785               rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
1786               bmpImage->green_mask, bmpImage->blue_mask );
1787         /* fall through */
1788     case 1:
1789     case 4:
1790     case 8:
1791         {
1792             /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
1793             const WORD* srcpixel;
1794             int rShift1,gShift1,bShift1;
1795             int rShift2,gShift2,bShift2;
1796             BYTE gMask1,gMask2;
1797
1798             /* Set color scaling values */
1799             rShift1=16+X11DRV_DIB_MaskToShift(rSrc)-3;
1800             gShift1=16+X11DRV_DIB_MaskToShift(gSrc)-3;
1801             bShift1=16+X11DRV_DIB_MaskToShift(bSrc)-3;
1802             rShift2=rShift1+5;
1803             gShift2=gShift1+5;
1804             bShift2=bShift1+5;
1805             if (gSrc==0x03e0) {
1806                 /* Green has 5 bits, like the others */
1807                 gMask1=0xf8;
1808                 gMask2=0x07;
1809             } else {
1810                 /* Green has 6 bits, not 5. Compensate. */
1811                 gShift1++;
1812                 gShift2+=2;
1813                 gMask1=0xfc;
1814                 gMask2=0x03;
1815             }
1816
1817             srcbits+=2*left;
1818
1819             /* We could split it into four separate cases to optimize
1820              * but it is probably not worth it.
1821              */
1822             for (h=lines-1; h>=0; h--) {
1823                 srcpixel=(const WORD*)srcbits;
1824                 for (x=left; x<width+left; x++) {
1825                     DWORD srcval;
1826                     BYTE red,green,blue;
1827                     srcval=*srcpixel++ << 16;
1828                     red=  ((srcval >> rShift1) & 0xf8) |
1829                         ((srcval >> rShift2) & 0x07);
1830                     green=((srcval >> gShift1) & gMask1) |
1831                         ((srcval >> gShift2) & gMask2);
1832                     blue= ((srcval >> bShift1) & 0xf8) |
1833                         ((srcval >> bShift2) & 0x07);
1834                     XPutPixel(bmpImage, x, h,
1835                               X11DRV_PALETTE_ToPhysical
1836                               (physDev, RGB(red,green,blue)));
1837                 }
1838                 srcbits += linebytes;
1839             }
1840         }
1841         break;
1842     }
1843 }
1844
1845
1846 /***********************************************************************
1847  *           X11DRV_DIB_GetImageBits_16
1848  *
1849  * GetDIBits for an 16-bit deep DIB.
1850  */
1851 static void X11DRV_DIB_GetImageBits_16( X11DRV_PDEVICE *physDev, int lines, BYTE *dstbits,
1852                                         DWORD dstwidth, DWORD srcwidth,
1853                                         PALETTEENTRY *srccolors,
1854                                         DWORD rDst, DWORD gDst, DWORD bDst,
1855                                         XImage *bmpImage, int linebytes )
1856 {
1857     DWORD x;
1858     int h, width = min(srcwidth, dstwidth);
1859     const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
1860
1861     if (lines < 0 )
1862     {
1863         lines = -lines;
1864         dstbits = dstbits + ( linebytes * (lines-1));
1865         linebytes = -linebytes;
1866     }
1867
1868     switch (bmpImage->depth)
1869     {
1870     case 15:
1871     case 16:
1872         {
1873             const char* srcbits;
1874
1875             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1876
1877             if (bmpImage->green_mask==0x03e0) {
1878                 if (gDst==bmpImage->green_mask) {
1879                     if (rDst==bmpImage->red_mask) {
1880                         /* ==== rgb 555 bmp -> rgb 555 dib ==== */
1881                         /* ==== bgr 555 bmp -> bgr 555 dib ==== */
1882                         convs->Convert_5x5_asis
1883                             (width,lines,
1884                              srcbits,-bmpImage->bytes_per_line,
1885                              dstbits,linebytes);
1886                     } else {
1887                         /* ==== rgb 555 bmp -> bgr 555 dib ==== */
1888                         /* ==== bgr 555 bmp -> rgb 555 dib ==== */
1889                         convs->Convert_555_reverse
1890                             (width,lines,
1891                              srcbits,-bmpImage->bytes_per_line,
1892                              dstbits,linebytes);
1893                     }
1894                 } else {
1895                     if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
1896                         /* ==== rgb 555 bmp -> rgb 565 dib ==== */
1897                         /* ==== bgr 555 bmp -> bgr 565 dib ==== */
1898                         convs->Convert_555_to_565_asis
1899                             (width,lines,
1900                              srcbits,-bmpImage->bytes_per_line,
1901                              dstbits,linebytes);
1902                     } else {
1903                         /* ==== rgb 555 bmp -> bgr 565 dib ==== */
1904                         /* ==== bgr 555 bmp -> rgb 565 dib ==== */
1905                         convs->Convert_555_to_565_reverse
1906                             (width,lines,
1907                              srcbits,-bmpImage->bytes_per_line,
1908                              dstbits,linebytes);
1909                     }
1910                 }
1911             } else if (bmpImage->green_mask==0x07e0) {
1912                 if (gDst==bmpImage->green_mask) {
1913                     if (rDst == bmpImage->red_mask) {
1914                         /* ==== rgb 565 bmp -> rgb 565 dib ==== */
1915                         /* ==== bgr 565 bmp -> bgr 565 dib ==== */
1916                         convs->Convert_5x5_asis
1917                             (width,lines,
1918                              srcbits,-bmpImage->bytes_per_line,
1919                              dstbits,linebytes);
1920                     } else {
1921                         /* ==== rgb 565 bmp -> bgr 565 dib ==== */
1922                         /* ==== bgr 565 bmp -> rgb 565 dib ==== */
1923                         convs->Convert_565_reverse
1924                             (width,lines,
1925                              srcbits,-bmpImage->bytes_per_line,
1926                              dstbits,linebytes);
1927                     }
1928                 } else {
1929                     if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
1930                         /* ==== rgb 565 bmp -> rgb 555 dib ==== */
1931                         /* ==== bgr 565 bmp -> bgr 555 dib ==== */
1932                         convs->Convert_565_to_555_asis
1933                             (width,lines,
1934                              srcbits,-bmpImage->bytes_per_line,
1935                              dstbits,linebytes);
1936                     } else {
1937                         /* ==== rgb 565 bmp -> bgr 555 dib ==== */
1938                         /* ==== bgr 565 bmp -> rgb 555 dib ==== */
1939                         convs->Convert_565_to_555_reverse
1940                             (width,lines,
1941                              srcbits,-bmpImage->bytes_per_line,
1942                              dstbits,linebytes);
1943                     }
1944                 }
1945             } else {
1946                 goto notsupported;
1947             }
1948         }
1949         break;
1950
1951     case 24:
1952         if (bmpImage->bits_per_pixel == 24) {
1953             const char* srcbits;
1954
1955             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1956
1957             if (bmpImage->green_mask!=0x00ff00 ||
1958                 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1959                 goto notsupported;
1960             } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
1961                     (bDst==0x1f && bmpImage->blue_mask==0xff)) {
1962                 if (gDst==0x03e0) {
1963                     /* ==== rgb 888 bmp -> rgb 555 dib ==== */
1964                     /* ==== bgr 888 bmp -> bgr 555 dib ==== */
1965                     convs->Convert_888_to_555_asis
1966                         (width,lines,
1967                          srcbits,-bmpImage->bytes_per_line,
1968                          dstbits,linebytes);
1969                 } else {
1970                     /* ==== rgb 888 bmp -> rgb 565 dib ==== */
1971                     /* ==== rgb 888 bmp -> rgb 565 dib ==== */
1972                     convs->Convert_888_to_565_asis
1973                         (width,lines,
1974                          srcbits,-bmpImage->bytes_per_line,
1975                          dstbits,linebytes);
1976                 }
1977             } else {
1978                 if (gDst==0x03e0) {
1979                     /* ==== rgb 888 bmp -> bgr 555 dib ==== */
1980                     /* ==== bgr 888 bmp -> rgb 555 dib ==== */
1981                     convs->Convert_888_to_555_reverse
1982                         (width,lines,
1983                          srcbits,-bmpImage->bytes_per_line,
1984                          dstbits,linebytes);
1985                 } else {
1986                     /* ==== rgb 888 bmp -> bgr 565 dib ==== */
1987                     /* ==== bgr 888 bmp -> rgb 565 dib ==== */
1988                     convs->Convert_888_to_565_reverse
1989                         (width,lines,
1990                          srcbits,-bmpImage->bytes_per_line,
1991                          dstbits,linebytes);
1992                 }
1993             }
1994             break;
1995         }
1996         /* Fall through */
1997
1998     case 32:
1999         {
2000             const char* srcbits;
2001
2002             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2003
2004             if (bmpImage->green_mask!=0x00ff00 ||
2005                 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2006                 goto notsupported;
2007             } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2008                        (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2009                 if (gDst==0x03e0) {
2010                     /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2011                     /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2012                     convs->Convert_0888_to_555_asis
2013                         (width,lines,
2014                          srcbits,-bmpImage->bytes_per_line,
2015                          dstbits,linebytes);
2016                 } else {
2017                     /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2018                     /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2019                     convs->Convert_0888_to_565_asis
2020                         (width,lines,
2021                          srcbits,-bmpImage->bytes_per_line,
2022                          dstbits,linebytes);
2023                 }
2024             } else {
2025                 if (gDst==0x03e0) {
2026                     /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2027                     /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2028                     convs->Convert_0888_to_555_reverse
2029                         (width,lines,
2030                          srcbits,-bmpImage->bytes_per_line,
2031                          dstbits,linebytes);
2032                 } else {
2033                     /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2034                     /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2035                     convs->Convert_0888_to_565_reverse
2036                         (width,lines,
2037                          srcbits,-bmpImage->bytes_per_line,
2038                          dstbits,linebytes);
2039                 }
2040             }
2041         }
2042         break;
2043
2044     case 1:
2045     case 4:
2046         if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2047             && srccolors) {
2048             /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2049             int rShift,gShift,bShift;
2050             WORD* dstpixel;
2051
2052             /* Shift everything 16 bits left so that all shifts are >0,
2053              * even for BGR DIBs. Then a single >> 16 will bring everything
2054              * back into place.
2055              */
2056             rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2057             gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2058             bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2059             if (gDst==0x07e0) {
2060                 /* 6 bits for the green */
2061                 gShift++;
2062             }
2063             rDst=rDst << 16;
2064             gDst=gDst << 16;
2065             bDst=bDst << 16;
2066             for (h = lines - 1; h >= 0; h--) {
2067                 dstpixel=(LPWORD)dstbits;
2068                 for (x = 0; x < width; x++) {
2069                     PALETTEENTRY srcval;
2070                     DWORD dstval;
2071                     srcval=srccolors[XGetPixel(bmpImage, x, h)];
2072                     dstval=((srcval.peRed   << rShift) & rDst) |
2073                            ((srcval.peGreen << gShift) & gDst) |
2074                            ((srcval.peBlue  << bShift) & bDst);
2075                     *dstpixel++=dstval >> 16;
2076                 }
2077                 dstbits += linebytes;
2078             }
2079         } else {
2080             goto notsupported;
2081         }
2082         break;
2083
2084     case 8:
2085         if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2086             && srccolors) {
2087             /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2088             int rShift,gShift,bShift;
2089             const BYTE* srcbits;
2090             const BYTE* srcpixel;
2091             WORD* dstpixel;
2092
2093             /* Shift everything 16 bits left so that all shifts are >0,
2094              * even for BGR DIBs. Then a single >> 16 will bring everything
2095              * back into place.
2096              */
2097             rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2098             gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2099             bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2100             if (gDst==0x07e0) {
2101                 /* 6 bits for the green */
2102                 gShift++;
2103             }
2104             rDst=rDst << 16;
2105             gDst=gDst << 16;
2106             bDst=bDst << 16;
2107             srcbits=(BYTE*)bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2108             for (h=0; h<lines; h++) {
2109                 srcpixel=srcbits;
2110                 dstpixel=(LPWORD)dstbits;
2111                 for (x = 0; x < width; x++) {
2112                     PALETTEENTRY srcval;
2113                     DWORD dstval;
2114                     srcval=srccolors[*srcpixel++];
2115                     dstval=((srcval.peRed   << rShift) & rDst) |
2116                            ((srcval.peGreen << gShift) & gDst) |
2117                            ((srcval.peBlue  << bShift) & bDst);
2118                     *dstpixel++=dstval >> 16;
2119                 }
2120                 srcbits -= bmpImage->bytes_per_line;
2121                 dstbits += linebytes;
2122             }
2123         } else {
2124             goto notsupported;
2125         }
2126         break;
2127
2128     default:
2129     notsupported:
2130         {
2131             /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2132             int rShift,gShift,bShift;
2133             WORD* dstpixel;
2134
2135             WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%x,%x,%x)\n",
2136                   bmpImage->depth, bmpImage->red_mask,
2137                   bmpImage->green_mask, bmpImage->blue_mask,
2138                   rDst, gDst, bDst);
2139
2140             /* Shift everything 16 bits left so that all shifts are >0,
2141              * even for BGR DIBs. Then a single >> 16 will bring everything
2142              * back into place.
2143              */
2144             rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2145             gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2146             bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2147             if (gDst==0x07e0) {
2148                 /* 6 bits for the green */
2149                 gShift++;
2150             }
2151             rDst=rDst << 16;
2152             gDst=gDst << 16;
2153             bDst=bDst << 16;
2154             for (h = lines - 1; h >= 0; h--) {
2155                 dstpixel=(LPWORD)dstbits;
2156                 for (x = 0; x < width; x++) {
2157                     COLORREF srcval;
2158                     DWORD dstval;
2159                     srcval=X11DRV_PALETTE_ToLogical(physDev, XGetPixel(bmpImage, x, h));
2160                     dstval=((GetRValue(srcval) << rShift) & rDst) |
2161                            ((GetGValue(srcval) << gShift) & gDst) |
2162                            ((GetBValue(srcval) << bShift) & bDst);
2163                     *dstpixel++=dstval >> 16;
2164                 }
2165                 dstbits += linebytes;
2166             }
2167         }
2168         break;
2169     }
2170 }
2171
2172
2173 /***********************************************************************
2174  *           X11DRV_DIB_SetImageBits_24
2175  *
2176  * SetDIBits for a 24-bit deep DIB.
2177  */
2178 static void X11DRV_DIB_SetImageBits_24( int lines, const BYTE *srcbits,
2179                                  DWORD srcwidth, DWORD dstwidth, int left,
2180                                  X11DRV_PDEVICE *physDev,
2181                                  DWORD rSrc, DWORD gSrc, DWORD bSrc,
2182                                  XImage *bmpImage, DWORD linebytes )
2183 {
2184     DWORD x;
2185     int h, width = min(srcwidth, dstwidth);
2186     const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2187
2188     if (lines < 0 )
2189     {
2190         lines = -lines;
2191         srcbits = srcbits + linebytes * (lines - 1);
2192         linebytes = -linebytes;
2193     }
2194
2195     switch (bmpImage->depth)
2196     {
2197     case 24:
2198         if (bmpImage->bits_per_pixel==24) {
2199             char* dstbits;
2200
2201             srcbits=srcbits+left*3;
2202             dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2203
2204             if (bmpImage->green_mask!=0x00ff00 ||
2205                 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2206                 goto notsupported;
2207             } else if (rSrc==bmpImage->red_mask) {
2208                 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2209                 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2210                 convs->Convert_888_asis
2211                     (width,lines,
2212                      srcbits,linebytes,
2213                      dstbits,-bmpImage->bytes_per_line);
2214             } else {
2215                 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2216                 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2217                 convs->Convert_888_reverse
2218                     (width,lines,
2219                      srcbits,linebytes,
2220                      dstbits,-bmpImage->bytes_per_line);
2221             }
2222             break;
2223         }
2224         /* fall through */
2225
2226     case 32:
2227         {
2228             char* dstbits;
2229
2230             srcbits=srcbits+left*3;
2231             dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2232
2233             if (bmpImage->green_mask!=0x00ff00 ||
2234                 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2235                 goto notsupported;
2236             } else if (rSrc==bmpImage->red_mask) {
2237                 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2238                 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2239                 convs->Convert_888_to_0888_asis
2240                     (width,lines,
2241                      srcbits,linebytes,
2242                      dstbits,-bmpImage->bytes_per_line);
2243             } else {
2244                 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2245                 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2246                 convs->Convert_888_to_0888_reverse
2247                     (width,lines,
2248                      srcbits,linebytes,
2249                      dstbits,-bmpImage->bytes_per_line);
2250             }
2251             break;
2252         }
2253
2254     case 15:
2255     case 16:
2256         {
2257             char* dstbits;
2258
2259             srcbits=srcbits+left*3;
2260             dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
2261
2262             if (bmpImage->green_mask==0x03e0) {
2263                 if ((rSrc==0xff0000 && bmpImage->red_mask==0x7f00) ||
2264                     (bSrc==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2265                     /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2266                     /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2267                     convs->Convert_888_to_555_asis
2268                         (width,lines,
2269                          srcbits,linebytes,
2270                          dstbits,-bmpImage->bytes_per_line);
2271                 } else if ((rSrc==0xff && bmpImage->red_mask==0x7f00) ||
2272                            (bSrc==0xff && bmpImage->blue_mask==0x7f00)) {
2273                     /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2274                     /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2275                     convs->Convert_888_to_555_reverse
2276                         (width,lines,
2277                          srcbits,linebytes,
2278                          dstbits,-bmpImage->bytes_per_line);
2279                 } else {
2280                     goto notsupported;
2281                 }
2282             } else if (bmpImage->green_mask==0x07e0) {
2283                 if ((rSrc==0xff0000 && bmpImage->red_mask==0xf800) ||
2284                     (bSrc==0xff0000 && bmpImage->blue_mask==0xf800)) {
2285                     /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2286                     /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2287                     convs->Convert_888_to_565_asis
2288                         (width,lines,
2289                          srcbits,linebytes,
2290                          dstbits,-bmpImage->bytes_per_line);
2291                 } else if ((rSrc==0xff && bmpImage->red_mask==0xf800) ||
2292                            (bSrc==0xff && bmpImage->blue_mask==0xf800)) {
2293                     /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2294                     /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2295                     convs->Convert_888_to_565_reverse
2296                         (width,lines,
2297                          srcbits,linebytes,
2298                          dstbits,-bmpImage->bytes_per_line);
2299                 } else {
2300                     goto notsupported;
2301                 }
2302             } else {
2303                 goto notsupported;
2304             }
2305         }
2306         break;
2307
2308     default:
2309     notsupported:
2310         WARN("from 24 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2311               rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2312               bmpImage->green_mask, bmpImage->blue_mask );
2313         /* fall through */
2314     case 1:
2315     case 4:
2316     case 8:
2317         {
2318             /* ==== rgb 888 dib -> any bmp format ==== */
2319             const BYTE* srcbyte;
2320
2321             /* Windows only supports one 24bpp DIB format: RGB888 */
2322             srcbits+=left*3;
2323             for (h = lines - 1; h >= 0; h--) {
2324                 srcbyte = srcbits;
2325                 for (x = left; x < width+left; x++) {
2326                     XPutPixel(bmpImage, x, h,
2327                               X11DRV_PALETTE_ToPhysical
2328                               (physDev, RGB(srcbyte[2], srcbyte[1], srcbyte[0])));
2329                     srcbyte+=3;
2330                 }
2331                 srcbits += linebytes;
2332             }
2333         }
2334         break;
2335     }
2336 }
2337
2338
2339 /***********************************************************************
2340  *           X11DRV_DIB_GetImageBits_24
2341  *
2342  * GetDIBits for an 24-bit deep DIB.
2343  */
2344 static void X11DRV_DIB_GetImageBits_24( X11DRV_PDEVICE *physDev, int lines, BYTE *dstbits,
2345                                         DWORD dstwidth, DWORD srcwidth,
2346                                         PALETTEENTRY *srccolors,
2347                                         DWORD rDst, DWORD gDst, DWORD bDst,
2348                                         XImage *bmpImage, DWORD linebytes )
2349 {
2350     DWORD x;
2351     int h, width = min(srcwidth, dstwidth);
2352     const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2353
2354     if (lines < 0 )
2355     {
2356         lines = -lines;
2357         dstbits = dstbits + ( linebytes * (lines-1) );
2358         linebytes = -linebytes;
2359     }
2360
2361     switch (bmpImage->depth)
2362     {
2363     case 24:
2364         if (bmpImage->bits_per_pixel==24) {
2365             const char* srcbits;
2366
2367             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2368
2369             if (bmpImage->green_mask!=0x00ff00 ||
2370                 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2371                 goto notsupported;
2372             } else if (rDst==bmpImage->red_mask) {
2373                 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2374                 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2375                 convs->Convert_888_asis
2376                     (width,lines,
2377                      srcbits,-bmpImage->bytes_per_line,
2378                      dstbits,linebytes);
2379             } else {
2380                 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2381                 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2382                 convs->Convert_888_reverse
2383                     (width,lines,
2384                      srcbits,-bmpImage->bytes_per_line,
2385                      dstbits,linebytes);
2386             }
2387             break;
2388         }
2389         /* fall through */
2390
2391     case 32:
2392         {
2393             const char* srcbits;
2394
2395             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2396
2397             if (bmpImage->green_mask!=0x00ff00 ||
2398                 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2399                 goto notsupported;
2400             } else if (rDst==bmpImage->red_mask) {
2401                 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2402                 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2403                 convs->Convert_0888_to_888_asis
2404                     (width,lines,
2405                      srcbits,-bmpImage->bytes_per_line,
2406                      dstbits,linebytes);
2407             } else {
2408                 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2409                 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2410                 convs->Convert_0888_to_888_reverse
2411                     (width,lines,
2412                      srcbits,-bmpImage->bytes_per_line,
2413                      dstbits,linebytes);
2414             }
2415             break;
2416         }
2417
2418     case 15:
2419     case 16:
2420         {
2421             const char* srcbits;
2422
2423             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2424
2425             if (bmpImage->green_mask==0x03e0) {
2426                 if ((rDst==0xff0000 && bmpImage->red_mask==0x7f00) ||
2427                     (bDst==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2428                     /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2429                     /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2430                     convs->Convert_555_to_888_asis
2431                         (width,lines,
2432                          srcbits,-bmpImage->bytes_per_line,
2433                          dstbits,linebytes);
2434                 } else if ((rDst==0xff && bmpImage->red_mask==0x7f00) ||
2435                            (bDst==0xff && bmpImage->blue_mask==0x7f00)) {
2436                     /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2437                     /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2438                     convs->Convert_555_to_888_reverse
2439                         (width,lines,
2440                          srcbits,-bmpImage->bytes_per_line,
2441                          dstbits,linebytes);
2442                 } else {
2443                     goto notsupported;
2444                 }
2445             } else if (bmpImage->green_mask==0x07e0) {
2446                 if ((rDst==0xff0000 && bmpImage->red_mask==0xf800) ||
2447                     (bDst==0xff0000 && bmpImage->blue_mask==0xf800)) {
2448                     /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2449                     /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2450                     convs->Convert_565_to_888_asis
2451                         (width,lines,
2452                          srcbits,-bmpImage->bytes_per_line,
2453                          dstbits,linebytes);
2454                 } else if ((rDst==0xff && bmpImage->red_mask==0xf800) ||
2455                            (bDst==0xff && bmpImage->blue_mask==0xf800)) {
2456                     /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2457                     /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2458                     convs->Convert_565_to_888_reverse
2459                         (width,lines,
2460                          srcbits,-bmpImage->bytes_per_line,
2461                          dstbits,linebytes);
2462                 } else {
2463                     goto notsupported;
2464                 }
2465             } else {
2466                 goto notsupported;
2467             }
2468         }
2469         break;
2470
2471     case 1:
2472     case 4:
2473         if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2474             && srccolors) {
2475             /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2476             BYTE* dstbyte;
2477
2478             /* Windows only supports one 24bpp DIB format: rgb 888 */
2479             for (h = lines - 1; h >= 0; h--) {
2480                 dstbyte=dstbits;
2481                 for (x = 0; x < width; x++) {
2482                     PALETTEENTRY srcval;
2483                     srcval=srccolors[XGetPixel(bmpImage, x, h)];
2484                     dstbyte[0]=srcval.peBlue;
2485                     dstbyte[1]=srcval.peGreen;
2486                     dstbyte[2]=srcval.peRed;
2487                     dstbyte+=3;
2488                 }
2489                 dstbits += linebytes;
2490             }
2491         } else {
2492             goto notsupported;
2493         }
2494         break;
2495
2496     case 8:
2497         if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2498             && srccolors) {
2499             /* ==== pal 8 bmp -> rgb 888 dib ==== */
2500             const void* srcbits;
2501             const BYTE* srcpixel;
2502             BYTE* dstbyte;
2503
2504             /* Windows only supports one 24bpp DIB format: rgb 888 */
2505             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2506             for (h = lines - 1; h >= 0; h--) {
2507                 srcpixel=srcbits;
2508                 dstbyte=dstbits;
2509                 for (x = 0; x < width; x++ ) {
2510                     PALETTEENTRY srcval;
2511                     srcval=srccolors[*srcpixel++];
2512                     dstbyte[0]=srcval.peBlue;
2513                     dstbyte[1]=srcval.peGreen;
2514                     dstbyte[2]=srcval.peRed;
2515                     dstbyte+=3;
2516                 }
2517                 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
2518                 dstbits += linebytes;
2519             }
2520         } else {
2521             goto notsupported;
2522         }
2523         break;
2524
2525     default:
2526     notsupported:
2527         {
2528             /* ==== any bmp format -> 888 dib ==== */
2529             BYTE* dstbyte;
2530
2531             WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%x,%x,%x)\n",
2532                   bmpImage->depth, bmpImage->red_mask,
2533                   bmpImage->green_mask, bmpImage->blue_mask,
2534                   rDst, gDst, bDst );
2535
2536             /* Windows only supports one 24bpp DIB format: rgb 888 */
2537             for (h = lines - 1; h >= 0; h--) {
2538                 dstbyte=dstbits;
2539                 for (x = 0; x < width; x++) {
2540                     COLORREF srcval=X11DRV_PALETTE_ToLogical
2541                         (physDev, XGetPixel( bmpImage, x, h ));
2542                     dstbyte[0]=GetBValue(srcval);
2543                     dstbyte[1]=GetGValue(srcval);
2544                     dstbyte[2]=GetRValue(srcval);
2545                     dstbyte+=3;
2546                 }
2547                 dstbits += linebytes;
2548             }
2549         }
2550         break;
2551     }
2552 }
2553
2554
2555 /***********************************************************************
2556  *           X11DRV_DIB_SetImageBits_32
2557  *
2558  * SetDIBits for a 32-bit deep DIB.
2559  */
2560 static void X11DRV_DIB_SetImageBits_32(int lines, const BYTE *srcbits,
2561                                        DWORD srcwidth, DWORD dstwidth, int left,
2562                                        X11DRV_PDEVICE *physDev,
2563                                        DWORD rSrc, DWORD gSrc, DWORD bSrc,
2564                                        XImage *bmpImage,
2565                                        DWORD linebytes)
2566 {
2567     DWORD x;
2568     int h, width = min(srcwidth, dstwidth);
2569     const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2570
2571     if (lines < 0 )
2572     {
2573        lines = -lines;
2574        srcbits = srcbits + ( linebytes * (lines-1) );
2575        linebytes = -linebytes;
2576     }
2577
2578     switch (bmpImage->depth)
2579     {
2580     case 24:
2581         if (bmpImage->bits_per_pixel==24) {
2582             char* dstbits;
2583
2584             srcbits=srcbits+left*4;
2585             dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2586
2587             if (rSrc==bmpImage->red_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->blue_mask) {
2588                 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2589                 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2590                 convs->Convert_0888_to_888_asis
2591                     (width,lines,
2592                      srcbits,linebytes,
2593                      dstbits,-bmpImage->bytes_per_line);
2594             } else if (bmpImage->green_mask!=0x00ff00 ||
2595                        (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2596                 goto notsupported;
2597                 /* the tests below assume sane bmpImage masks */
2598             } else if (rSrc==bmpImage->blue_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->red_mask) {
2599                 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2600                 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2601                 convs->Convert_0888_to_888_reverse
2602                     (width,lines,
2603                      srcbits,linebytes,
2604                      dstbits,-bmpImage->bytes_per_line);
2605             } else if (bmpImage->blue_mask==0xff) {
2606                 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2607                 convs->Convert_any0888_to_rgb888
2608                     (width,lines,
2609                      srcbits,linebytes,
2610                      rSrc,gSrc,bSrc,
2611                      dstbits,-bmpImage->bytes_per_line);
2612             } else {
2613                 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2614                 convs->Convert_any0888_to_bgr888
2615                     (width,lines,
2616                      srcbits,linebytes,
2617                      rSrc,gSrc,bSrc,
2618                      dstbits,-bmpImage->bytes_per_line);
2619             }
2620             break;
2621         }
2622         /* fall through */
2623
2624     case 32:
2625         {
2626             char* dstbits;
2627
2628             srcbits=srcbits+left*4;
2629             dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2630
2631             if (gSrc==bmpImage->green_mask) {
2632                 if (rSrc==bmpImage->red_mask && bSrc==bmpImage->blue_mask) {
2633                     /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2634                     /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2635                     convs->Convert_0888_asis
2636                         (width,lines,
2637                          srcbits,linebytes,
2638                          dstbits,-bmpImage->bytes_per_line);
2639                 } else if (bmpImage->green_mask!=0x00ff00 ||
2640                            (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2641                     goto notsupported;
2642                     /* the tests below assume sane bmpImage masks */
2643                 } else if (rSrc==bmpImage->blue_mask && bSrc==bmpImage->red_mask) {
2644                     /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2645                     /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2646                     convs->Convert_0888_reverse
2647                         (width,lines,
2648                          srcbits,linebytes,
2649                          dstbits,-bmpImage->bytes_per_line);
2650                 } else {
2651                     /* ==== any 0888 dib -> any 0888 bmp ==== */
2652                     convs->Convert_0888_any
2653                         (width,lines,
2654                          srcbits,linebytes,
2655                          rSrc,gSrc,bSrc,
2656                          dstbits,-bmpImage->bytes_per_line,
2657                          bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
2658                 }
2659             } else if (bmpImage->green_mask!=0x00ff00 ||
2660                        (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2661                 goto notsupported;
2662                 /* the tests below assume sane bmpImage masks */
2663             } else {
2664                 /* ==== any 0888 dib -> any 0888 bmp ==== */
2665                 convs->Convert_0888_any
2666                     (width,lines,
2667                      srcbits,linebytes,
2668                      rSrc,gSrc,bSrc,
2669                      dstbits,-bmpImage->bytes_per_line,
2670                      bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
2671             }
2672         }
2673         break;
2674
2675     case 15:
2676     case 16:
2677         {
2678             char* dstbits;
2679
2680             srcbits=srcbits+left*4;
2681             dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
2682
2683             if (rSrc==0xff0000 && gSrc==0x00ff00 && bSrc==0x0000ff) {
2684                 if (bmpImage->green_mask==0x03e0) {
2685                     if (bmpImage->red_mask==0x7f00) {
2686                         /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
2687                         convs->Convert_0888_to_555_asis
2688                             (width,lines,
2689                              srcbits,linebytes,
2690                              dstbits,-bmpImage->bytes_per_line);
2691                     } else if (bmpImage->blue_mask==0x7f00) {
2692                         /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
2693                         convs->Convert_0888_to_555_reverse
2694                             (width,lines,
2695                              srcbits,linebytes,
2696                              dstbits,-bmpImage->bytes_per_line);
2697                     } else {
2698                         goto notsupported;
2699                     }
2700                 } else if (bmpImage->green_mask==0x07e0) {
2701                     if (bmpImage->red_mask==0xf800) {
2702                         /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
2703                         convs->Convert_0888_to_565_asis
2704                             (width,lines,
2705                              srcbits,linebytes,
2706                              dstbits,-bmpImage->bytes_per_line);
2707                     } else if (bmpImage->blue_mask==0xf800) {
2708                         /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
2709                         convs->Convert_0888_to_565_reverse
2710                             (width,lines,
2711                              srcbits,linebytes,
2712                              dstbits,-bmpImage->bytes_per_line);
2713                     } else {
2714                         goto notsupported;
2715                     }
2716                 } else {
2717                     goto notsupported;
2718                 }
2719             } else if (rSrc==0x0000ff && gSrc==0x00ff00 && bSrc==0xff0000) {
2720                 if (bmpImage->green_mask==0x03e0) {
2721                     if (bmpImage->blue_mask==0x7f00) {
2722                         /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
2723                         convs->Convert_0888_to_555_asis
2724                             (width,lines,
2725                              srcbits,linebytes,
2726                              dstbits,-bmpImage->bytes_per_line);
2727                     } else if (bmpImage->red_mask==0x7f00) {
2728                         /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
2729                         convs->Convert_0888_to_555_reverse
2730                             (width,lines,
2731                              srcbits,linebytes,
2732                              dstbits,-bmpImage->bytes_per_line);
2733                     } else {
2734                         goto notsupported;
2735                     }
2736                 } else if (bmpImage->green_mask==0x07e0) {
2737                     if (bmpImage->blue_mask==0xf800) {
2738                         /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
2739                         convs->Convert_0888_to_565_asis
2740                             (width,lines,
2741                              srcbits,linebytes,
2742                              dstbits,-bmpImage->bytes_per_line);
2743                     } else if (bmpImage->red_mask==0xf800) {
2744                         /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
2745                         convs->Convert_0888_to_565_reverse
2746                             (width,lines,
2747                              srcbits,linebytes,
2748                              dstbits,-bmpImage->bytes_per_line);
2749                     } else {
2750                         goto notsupported;
2751                     }
2752                 } else {
2753                     goto notsupported;
2754                 }
2755             } else {
2756                 if (bmpImage->green_mask==0x03e0 &&
2757                     (bmpImage->red_mask==0x7f00 ||
2758                      bmpImage->blue_mask==0x7f00)) {
2759                     /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
2760                     convs->Convert_any0888_to_5x5
2761                         (width,lines,
2762                          srcbits,linebytes,
2763                          rSrc,gSrc,bSrc,
2764                          dstbits,-bmpImage->bytes_per_line,
2765                          bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
2766                 } else if (bmpImage->green_mask==0x07e0 &&
2767                            (bmpImage->red_mask==0xf800 ||
2768                             bmpImage->blue_mask==0xf800)) {
2769                     /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
2770                     convs->Convert_any0888_to_5x5
2771                         (width,lines,
2772                          srcbits,linebytes,
2773                          rSrc,gSrc,bSrc,
2774                          dstbits,-bmpImage->bytes_per_line,
2775                          bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
2776                 } else {
2777                     goto notsupported;
2778                 }
2779             }
2780         }
2781         break;
2782
2783     default:
2784     notsupported:
2785         WARN("from 32 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2786               rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2787               bmpImage->green_mask, bmpImage->blue_mask );
2788         /* fall through */
2789     case 1:
2790     case 4:
2791     case 8:
2792         {
2793             /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
2794             const DWORD* srcpixel;
2795             int rShift,gShift,bShift;
2796
2797             rShift=X11DRV_DIB_MaskToShift(rSrc);
2798             gShift=X11DRV_DIB_MaskToShift(gSrc);
2799             bShift=X11DRV_DIB_MaskToShift(bSrc);
2800             srcbits+=left*4;
2801             for (h = lines - 1; h >= 0; h--) {
2802                 srcpixel=(const DWORD*)srcbits;
2803                 for (x = left; x < width+left; x++) {
2804                     DWORD srcvalue;
2805                     BYTE red,green,blue;
2806                     srcvalue=*srcpixel++;
2807                     red=  (srcvalue >> rShift) & 0xff;
2808                     green=(srcvalue >> gShift) & 0xff;
2809                     blue= (srcvalue >> bShift) & 0xff;
2810                     XPutPixel(bmpImage, x, h, X11DRV_PALETTE_ToPhysical
2811                               (physDev, RGB(red,green,blue)));
2812                 }
2813                 srcbits += linebytes;
2814             }
2815         }
2816         break;
2817     }
2818
2819 }
2820
2821 /***********************************************************************
2822  *           X11DRV_DIB_GetImageBits_32
2823  *
2824  * GetDIBits for an 32-bit deep DIB.
2825  */
2826 static void X11DRV_DIB_GetImageBits_32( X11DRV_PDEVICE *physDev, int lines, BYTE *dstbits,
2827                                         DWORD dstwidth, DWORD srcwidth,
2828                                         PALETTEENTRY *srccolors,
2829                                         DWORD rDst, DWORD gDst, DWORD bDst,
2830                                         XImage *bmpImage, int linebytes )
2831 {
2832     DWORD x;
2833     int h, width = min(srcwidth, dstwidth);
2834     const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2835
2836     if (lines < 0 )
2837     {
2838         lines = -lines;
2839         dstbits = dstbits + ( linebytes * (lines-1) );
2840         linebytes = -linebytes;
2841     }
2842
2843     switch (bmpImage->depth)
2844     {
2845     case 24:
2846         if (bmpImage->bits_per_pixel==24) {
2847             const void* srcbits;
2848
2849             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2850
2851             if (rDst==bmpImage->red_mask && gDst==bmpImage->green_mask && bDst==bmpImage->blue_mask) {
2852                 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2853                 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2854                 convs->Convert_888_to_0888_asis
2855                     (width,lines,
2856                      srcbits,-bmpImage->bytes_per_line,
2857                      dstbits,linebytes);
2858             } else if (bmpImage->green_mask!=0x00ff00 ||
2859                        (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2860                 goto notsupported;
2861                 /* the tests below assume sane bmpImage masks */
2862             } else if (rDst==bmpImage->blue_mask && gDst==bmpImage->green_mask && bDst==bmpImage->red_mask) {
2863                 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2864                 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2865                 convs->Convert_888_to_0888_reverse
2866                     (width,lines,
2867                      srcbits,-bmpImage->bytes_per_line,
2868                      dstbits,linebytes);
2869             } else if (bmpImage->blue_mask==0xff) {
2870                 /* ==== rgb 888 bmp -> any 0888 dib ==== */
2871                 convs->Convert_rgb888_to_any0888
2872                     (width,lines,
2873                      srcbits,-bmpImage->bytes_per_line,
2874                      dstbits,linebytes,
2875                      rDst,gDst,bDst);
2876             } else {
2877                 /* ==== bgr 888 bmp -> any 0888 dib ==== */
2878                 convs->Convert_bgr888_to_any0888
2879                     (width,lines,
2880                      srcbits,-bmpImage->bytes_per_line,
2881                      dstbits,linebytes,
2882                      rDst,gDst,bDst);
2883             }
2884             break;
2885         }
2886         /* fall through */
2887
2888     case 32:
2889         {
2890             const char* srcbits;
2891
2892             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2893
2894             if (gDst==bmpImage->green_mask) {
2895                 if (rDst==bmpImage->red_mask && bDst==bmpImage->blue_mask) {
2896                     /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
2897                     /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
2898                     convs->Convert_0888_asis
2899                         (width,lines,
2900                          srcbits,-bmpImage->bytes_per_line,
2901                          dstbits,linebytes);
2902                 } else if (bmpImage->green_mask!=0x00ff00 ||
2903                            (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2904                     goto notsupported;
2905                     /* the tests below assume sane bmpImage masks */
2906                 } else if (rDst==bmpImage->blue_mask && bDst==bmpImage->red_mask) {
2907                     /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
2908                     /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
2909                     convs->Convert_0888_reverse
2910                         (width,lines,
2911                          srcbits,-bmpImage->bytes_per_line,
2912                          dstbits,linebytes);
2913                 } else {
2914                     /* ==== any 0888 bmp -> any 0888 dib ==== */
2915                     convs->Convert_0888_any
2916                         (width,lines,
2917                          srcbits,-bmpImage->bytes_per_line,
2918                          bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
2919                          dstbits,linebytes,
2920                          rDst,gDst,bDst);
2921                 }
2922             } else if (bmpImage->green_mask!=0x00ff00 ||
2923                        (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2924                 goto notsupported;
2925                 /* the tests below assume sane bmpImage masks */
2926             } else {
2927                 /* ==== any 0888 bmp -> any 0888 dib ==== */
2928                 convs->Convert_0888_any
2929                     (width,lines,
2930                      srcbits,-bmpImage->bytes_per_line,
2931                      bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
2932                      dstbits,linebytes,
2933                      rDst,gDst,bDst);
2934             }
2935         }
2936         break;
2937
2938     case 15:
2939     case 16:
2940         {
2941             const char* srcbits;
2942
2943             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2944
2945             if (rDst==0xff0000 && gDst==0x00ff00 && bDst==0x0000ff) {
2946                 if (bmpImage->green_mask==0x03e0) {
2947                     if (bmpImage->red_mask==0x7f00) {
2948                         /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
2949                         convs->Convert_555_to_0888_asis
2950                             (width,lines,
2951                              srcbits,-bmpImage->bytes_per_line,
2952                              dstbits,linebytes);
2953                     } else if (bmpImage->blue_mask==0x7f00) {
2954                         /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
2955                         convs->Convert_555_to_0888_reverse
2956                             (width,lines,
2957                              srcbits,-bmpImage->bytes_per_line,
2958                              dstbits,linebytes);
2959                     } else {
2960                         goto notsupported;
2961                     }
2962                 } else if (bmpImage->green_mask==0x07e0) {
2963                     if (bmpImage->red_mask==0xf800) {
2964                         /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
2965                         convs->Convert_565_to_0888_asis
2966                             (width,lines,
2967                              srcbits,-bmpImage->bytes_per_line,
2968                              dstbits,linebytes);
2969                     } else if (bmpImage->blue_mask==0xf800) {
2970                         /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
2971                         convs->Convert_565_to_0888_reverse
2972                             (width,lines,
2973                              srcbits,-bmpImage->bytes_per_line,
2974                              dstbits,linebytes);
2975                     } else {
2976                         goto notsupported;
2977                     }
2978                 } else {
2979                     goto notsupported;
2980                 }
2981             } else if (rDst==0x0000ff && gDst==0x00ff00 && bDst==0xff0000) {
2982                 if (bmpImage->green_mask==0x03e0) {
2983                     if (bmpImage->blue_mask==0x7f00) {
2984                         /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
2985                         convs->Convert_555_to_0888_asis
2986                             (width,lines,
2987                              srcbits,-bmpImage->bytes_per_line,
2988                              dstbits,linebytes);
2989                     } else if (bmpImage->red_mask==0x7f00) {
2990                         /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
2991                         convs->Convert_555_to_0888_reverse
2992                             (width,lines,
2993                              srcbits,-bmpImage->bytes_per_line,
2994                              dstbits,linebytes);
2995                     } else {
2996                         goto notsupported;
2997                     }
2998                 } else if (bmpImage->green_mask==0x07e0) {
2999                     if (bmpImage->blue_mask==0xf800) {
3000                         /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3001                         convs->Convert_565_to_0888_asis
3002                             (width,lines,
3003                              srcbits,-bmpImage->bytes_per_line,
3004                              dstbits,linebytes);
3005                     } else if (bmpImage->red_mask==0xf800) {
3006                         /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3007                         convs->Convert_565_to_0888_reverse
3008                             (width,lines,
3009                              srcbits,-bmpImage->bytes_per_line,
3010                              dstbits,linebytes);
3011                     } else {
3012                         goto notsupported;
3013                     }
3014                 } else {
3015                     goto notsupported;
3016                 }
3017             } else {
3018                 if (bmpImage->green_mask==0x03e0 &&
3019                     (bmpImage->red_mask==0x7f00 ||
3020                      bmpImage->blue_mask==0x7f00)) {
3021                     /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3022                     convs->Convert_5x5_to_any0888
3023                         (width,lines,
3024                          srcbits,-bmpImage->bytes_per_line,
3025                          bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3026                          dstbits,linebytes,
3027                          rDst,gDst,bDst);
3028                 } else if (bmpImage->green_mask==0x07e0 &&
3029                            (bmpImage->red_mask==0xf800 ||
3030                             bmpImage->blue_mask==0xf800)) {
3031                     /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3032                     convs->Convert_5x5_to_any0888
3033                         (width,lines,
3034                          srcbits,-bmpImage->bytes_per_line,
3035                          bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3036                          dstbits,linebytes,
3037                          rDst,gDst,bDst);
3038                 } else {
3039                     goto notsupported;
3040                 }
3041             }
3042         }
3043         break;
3044
3045     case 1:
3046     case 4:
3047         if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3048             && srccolors) {
3049             /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3050             int rShift,gShift,bShift;
3051             DWORD* dstpixel;
3052
3053             rShift=X11DRV_DIB_MaskToShift(rDst);
3054             gShift=X11DRV_DIB_MaskToShift(gDst);
3055             bShift=X11DRV_DIB_MaskToShift(bDst);
3056             for (h = lines - 1; h >= 0; h--) {
3057                 dstpixel=(DWORD*)dstbits;
3058                 for (x = 0; x < width; x++) {
3059                     PALETTEENTRY srcval;
3060                     srcval = srccolors[XGetPixel(bmpImage, x, h)];
3061                     *dstpixel++=(srcval.peRed   << rShift) |
3062                                 (srcval.peGreen << gShift) |
3063                                 (srcval.peBlue  << bShift);
3064                 }
3065                 dstbits += linebytes;
3066             }
3067         } else {
3068             goto notsupported;
3069         }
3070         break;
3071
3072     case 8:
3073         if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3074             && srccolors) {
3075             /* ==== pal 8 bmp -> any 0888 dib ==== */
3076             int rShift,gShift,bShift;
3077             const void* srcbits;
3078             const BYTE* srcpixel;
3079             DWORD* dstpixel;
3080
3081             rShift=X11DRV_DIB_MaskToShift(rDst);
3082             gShift=X11DRV_DIB_MaskToShift(gDst);
3083             bShift=X11DRV_DIB_MaskToShift(bDst);
3084             srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3085             for (h = lines - 1; h >= 0; h--) {
3086                 srcpixel=srcbits;
3087                 dstpixel=(DWORD*)dstbits;
3088                 for (x = 0; x < width; x++) {
3089                     PALETTEENTRY srcval;
3090                     srcval=srccolors[*srcpixel++];
3091                     *dstpixel++=(srcval.peRed   << rShift) |
3092                                 (srcval.peGreen << gShift) |
3093                                 (srcval.peBlue  << bShift);
3094                 }
3095                 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
3096                 dstbits += linebytes;
3097             }
3098         } else {
3099             goto notsupported;
3100         }
3101         break;
3102
3103     default:
3104     notsupported:
3105         {
3106             /* ==== any bmp format -> any 0888 dib ==== */
3107             int rShift,gShift,bShift;
3108             DWORD* dstpixel;
3109
3110             WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%x,%x,%x)\n",
3111                   bmpImage->depth, bmpImage->red_mask,
3112                   bmpImage->green_mask, bmpImage->blue_mask,
3113                   rDst,gDst,bDst);
3114
3115             rShift=X11DRV_DIB_MaskToShift(rDst);
3116             gShift=X11DRV_DIB_MaskToShift(gDst);
3117             bShift=X11DRV_DIB_MaskToShift(bDst);
3118             for (h = lines - 1; h >= 0; h--) {
3119                 dstpixel=(DWORD*)dstbits;
3120                 for (x = 0; x < width; x++) {
3121                     COLORREF srcval;
3122                     srcval=X11DRV_PALETTE_ToLogical(physDev, XGetPixel(bmpImage, x, h));
3123                     *dstpixel++=(GetRValue(srcval) << rShift) |
3124                                 (GetGValue(srcval) << gShift) |
3125                                 (GetBValue(srcval) << bShift);
3126                 }
3127                 dstbits += linebytes;
3128             }
3129         }
3130         break;
3131     }
3132 }
3133
3134 /***********************************************************************
3135  *           X11DRV_DIB_SetImageBits
3136  *
3137  * Transfer the bits to an X image.
3138  * Helper function for SetDIBits() and SetDIBitsToDevice().
3139  */
3140 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3141 {
3142     int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3143     void *old_data = NULL;
3144     XImage *bmpImage;
3145
3146     wine_tsx11_lock();
3147     if (descr->image)
3148         bmpImage = descr->image;
3149     else {
3150         bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3151                                  descr->infoWidth, lines, 32, 0 );
3152         bmpImage->data = HeapAlloc( GetProcessHeap(), 0, lines * bmpImage->bytes_per_line );
3153         if(bmpImage->data == NULL) {
3154             ERR("Out of memory!\n");
3155             XDestroyImage( bmpImage );
3156             wine_tsx11_unlock();
3157             return 0;
3158         }
3159         if (descr->shifts)
3160         {
3161             bmpImage->red_mask = descr->shifts->physicalRed.max << descr->shifts->physicalRed.shift;
3162             bmpImage->green_mask = descr->shifts->physicalGreen.max << descr->shifts->physicalGreen.shift;
3163             bmpImage->blue_mask = descr->shifts->physicalBlue.max << descr->shifts->physicalBlue.shift;
3164         }
3165     }
3166     wine_tsx11_unlock();
3167
3168     TRACE("Dib: depth=%d r=%x g=%x b=%x\n",
3169           descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3170     TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3171           bmpImage->depth,bmpImage->bits_per_pixel,
3172           bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3173
3174 #ifdef HAVE_LIBXXSHM
3175     if (descr->shm_mode == X11DRV_SHM_PIXMAP
3176             && descr->xSrc == 0 && descr->ySrc == 0
3177             && descr->xDest == 0 && descr->yDest == 0)
3178     {
3179         TRACE("Using the shared pixmap data.\n");
3180
3181         wine_tsx11_lock();
3182         XSync( gdi_display, False );
3183         wine_tsx11_unlock();
3184
3185         old_data = descr->image->data;
3186         descr->image->data = descr->physBitmap->shminfo.shmaddr;
3187     }
3188 #endif
3189
3190       /* Transfer the pixels */
3191     __TRY
3192     {
3193         switch(descr->infoBpp)
3194         {
3195         case 1:
3196             X11DRV_DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
3197                                        descr->width, descr->xSrc, (int *)(descr->colorMap),
3198                                        bmpImage, descr->dibpitch );
3199             break;
3200         case 4:
3201             X11DRV_DIB_SetImageBits_4( descr->lines, descr->bits,
3202                                        descr->infoWidth, descr->width,
3203                                        descr->xSrc, (int*)(descr->colorMap),
3204                                        bmpImage, descr->dibpitch );
3205             break;
3206         case 8:
3207             X11DRV_DIB_SetImageBits_8( descr->lines, descr->bits,
3208                                        descr->infoWidth, descr->width,
3209                                        descr->xSrc, (int *)(descr->colorMap),
3210                                        bmpImage, descr->dibpitch );
3211             break;
3212         case 16:
3213             X11DRV_DIB_SetImageBits_16( descr->lines, descr->bits,
3214                                         descr->infoWidth, descr->width,
3215                                         descr->xSrc, descr->physDev,
3216                                         descr->rMask, descr->gMask, descr->bMask,
3217                                         bmpImage, descr->dibpitch);
3218             break;
3219         case 24:
3220             X11DRV_DIB_SetImageBits_24( descr->lines, descr->bits,
3221                                         descr->infoWidth, descr->width,
3222                                         descr->xSrc, descr->physDev,
3223                                         descr->rMask, descr->gMask, descr->bMask,
3224                                         bmpImage, descr->dibpitch);
3225             break;
3226         case 32:
3227             X11DRV_DIB_SetImageBits_32( descr->lines, descr->bits,
3228                                         descr->infoWidth, descr->width,
3229                                         descr->xSrc, descr->physDev,
3230                                         descr->rMask, descr->gMask, descr->bMask,
3231                                         bmpImage, descr->dibpitch);
3232             break;
3233         default:
3234             WARN("(%d): Invalid depth\n", descr->infoBpp );
3235             break;
3236         }
3237     }
3238     __EXCEPT_PAGE_FAULT
3239     {
3240         WARN( "invalid bits pointer %p\n", descr->bits );
3241         lines = 0;
3242     }
3243     __ENDTRY
3244
3245     TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3246      descr->drawable, descr->gc, bmpImage,
3247      descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3248      descr->width, descr->height);
3249
3250     wine_tsx11_lock();
3251     if (lines)
3252     {
3253 #ifdef HAVE_LIBXXSHM
3254         if (descr->shm_mode == X11DRV_SHM_PIXMAP
3255                 && descr->xSrc == 0 && descr->ySrc == 0
3256                 && descr->xDest == 0 && descr->yDest == 0)
3257         {
3258             XSync( gdi_display, False );
3259         }
3260         else if (descr->shm_mode == X11DRV_SHM_IMAGE && descr->image)
3261         {
3262             XShmPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3263                           descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3264                           descr->width, descr->height, FALSE );
3265             XSync( gdi_display, 0 );
3266         }
3267         else
3268 #endif
3269         {
3270             XPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3271                        descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3272                        descr->width, descr->height );
3273         }
3274     }
3275
3276     if (old_data) descr->image->data = old_data;
3277
3278     if (!descr->image) X11DRV_DIB_DestroyXImage( bmpImage );
3279     wine_tsx11_unlock();
3280     return lines;
3281 }
3282
3283 /***********************************************************************
3284  *           X11DRV_DIB_GetImageBits
3285  *
3286  * Transfer the bits from an X image.
3287  */
3288 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3289 {
3290     int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3291     void *old_data = NULL;
3292     XImage *bmpImage;
3293
3294     wine_tsx11_lock();
3295    if (descr->image)
3296         bmpImage = descr->image;
3297     else {
3298         bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3299                                  descr->infoWidth, lines, 32, 0 );
3300         bmpImage->data = HeapAlloc( GetProcessHeap(), 0, lines * bmpImage->bytes_per_line );
3301         if(bmpImage->data == NULL) {
3302             ERR("Out of memory!\n");
3303             XDestroyImage( bmpImage );
3304             wine_tsx11_unlock();
3305             return 0;
3306         }
3307         if (descr->shifts)
3308         {
3309             bmpImage->red_mask = descr->shifts->physicalRed.max << descr->shifts->physicalRed.shift;
3310             bmpImage->green_mask = descr->shifts->physicalGreen.max << descr->shifts->physicalGreen.shift;
3311             bmpImage->blue_mask = descr->shifts->physicalBlue.max << descr->shifts->physicalBlue.shift;
3312         }
3313     }
3314
3315 #ifdef HAVE_LIBXXSHM
3316
3317     /* We must not call XShmGetImage() with a bitmap which is bigger than the available area.
3318        If we do, XShmGetImage() will fail (X exception), as it checks for this internally. */
3319     if (descr->shm_mode == X11DRV_SHM_PIXMAP && descr->image
3320             && descr->xSrc == 0 && descr->ySrc == 0
3321             && descr->xDest == 0 && descr->yDest == 0
3322             && bmpImage->width <= (descr->width - descr->xSrc)
3323             && bmpImage->height <= (descr->height - descr->ySrc))
3324     {
3325         XSync( gdi_display, False );
3326         old_data = bmpImage->data;
3327         bmpImage->data = descr->physBitmap->shminfo.shmaddr;
3328         TRACE("Using shared pixmap data.\n");
3329     }
3330     else if (descr->shm_mode == X11DRV_SHM_IMAGE && descr->image
3331             && bmpImage->width <= (descr->width - descr->xSrc)
3332             && bmpImage->height <= (descr->height - descr->ySrc))
3333     {
3334         int saveRed, saveGreen, saveBlue;
3335
3336         TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3337                             gdi_display, descr->drawable, bmpImage,
3338                             descr->xSrc, descr->ySrc, AllPlanes);
3339
3340         /* We must save and restore the bmpImage's masks in order
3341          * to preserve them across the call to XShmGetImage, which
3342          * decides to eliminate them since it doesn't happen to know
3343          * what the format of the image is supposed to be, even though
3344          * we do. */
3345         saveRed = bmpImage->red_mask;
3346         saveBlue= bmpImage->blue_mask;
3347         saveGreen = bmpImage->green_mask;
3348
3349         XShmGetImage( gdi_display, descr->drawable, bmpImage,
3350                       descr->xSrc, descr->ySrc, AllPlanes);
3351
3352         bmpImage->red_mask = saveRed;
3353         bmpImage->blue_mask = saveBlue;
3354         bmpImage->green_mask = saveGreen;
3355     }
3356     else
3357 #endif /* HAVE_LIBXXSHM */
3358     {
3359         TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3360               gdi_display, descr->drawable, descr->xSrc, descr->ySrc, descr->width,
3361               lines, AllPlanes, ZPixmap, bmpImage, descr->xDest, descr->yDest);
3362         XGetSubImage( gdi_display, descr->drawable, descr->xSrc, descr->ySrc,
3363                       descr->width, lines, AllPlanes, ZPixmap,
3364                       bmpImage, descr->xDest, descr->yDest );
3365     }
3366     wine_tsx11_unlock();
3367
3368     TRACE("Dib: depth=%2d r=%x g=%x b=%x\n",
3369           descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3370     TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3371           bmpImage->depth,bmpImage->bits_per_pixel,
3372           bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3373       /* Transfer the pixels */
3374     switch(descr->infoBpp)
3375     {
3376     case 1:
3377           X11DRV_DIB_GetImageBits_1( descr->lines,(LPVOID)descr->bits,
3378                                      descr->infoWidth, descr->width,
3379                                      descr->colorMap, descr->palentry,
3380                                      bmpImage, descr->dibpitch );
3381        break;
3382
3383     case 4:
3384         X11DRV_DIB_GetImageBits_4( descr->lines,(LPVOID)descr->bits,
3385                                    descr->infoWidth, descr->width,
3386                                    descr->colorMap, descr->palentry,
3387                                    bmpImage, descr->dibpitch );
3388         break;
3389     case 8:
3390         X11DRV_DIB_GetImageBits_8( descr->lines, (LPVOID)descr->bits,
3391                                    descr->infoWidth, descr->width,
3392                                    descr->colorMap, descr->palentry,
3393                                    bmpImage, descr->dibpitch );
3394         break;
3395     case 16:
3396        X11DRV_DIB_GetImageBits_16( descr->physDev, descr->lines, (LPVOID)descr->bits,
3397                                    descr->infoWidth,descr->width,
3398                                    descr->palentry,
3399                                    descr->rMask, descr->gMask, descr->bMask,
3400                                    bmpImage, descr->dibpitch );
3401        break;
3402
3403     case 24:
3404        X11DRV_DIB_GetImageBits_24( descr->physDev, descr->lines, (LPVOID)descr->bits,
3405                                    descr->infoWidth,descr->width,
3406                                    descr->palentry,
3407                                    descr->rMask, descr->gMask, descr->bMask,
3408                                    bmpImage, descr->dibpitch);
3409        break;
3410
3411     case 32:
3412        X11DRV_DIB_GetImageBits_32( descr->physDev, descr->lines, (LPVOID)descr->bits,
3413                                    descr->infoWidth, descr->width,
3414                                    descr->palentry,
3415                                    descr->rMask, descr->gMask, descr->bMask,
3416                                    bmpImage, descr->dibpitch);
3417        break;
3418
3419     default:
3420         WARN("(%d): Invalid depth\n", descr->infoBpp );
3421         break;
3422     }
3423
3424     if (old_data) bmpImage->data = old_data;
3425     if (!descr->image) X11DRV_DIB_DestroyXImage( bmpImage );
3426     return lines;
3427 }
3428
3429 /***********************************************************************
3430  *           X11DRV_DIB_DoCopyDIBSection
3431  */
3432 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB,
3433                                         void *colorMap, int nColorMap,
3434                                         Drawable dest, GC gc,
3435                                         DWORD xSrc, DWORD ySrc,
3436                                         DWORD xDest, DWORD yDest,
3437                                         DWORD width, DWORD height)
3438 {
3439   DIBSECTION dibSection;
3440   X11DRV_DIB_IMAGEBITS_DESCR descr;
3441   int identity[2] = {0,1};
3442
3443   if (!GetObjectW( physBitmap->hbitmap, sizeof(dibSection), &dibSection )) return;
3444
3445   descr.physDev     = NULL;
3446   descr.palentry    = NULL;
3447   descr.infoWidth   = dibSection.dsBmih.biWidth;
3448   descr.infoBpp     = dibSection.dsBmih.biBitCount;
3449   descr.lines       = physBitmap->topdown ? -dibSection.dsBmih.biHeight : dibSection.dsBmih.biHeight;
3450   descr.image       = physBitmap->image;
3451   descr.colorMap    = colorMap;
3452   descr.nColorMap   = nColorMap;
3453   descr.bits        = dibSection.dsBm.bmBits;
3454   descr.depth       = physBitmap->depth;
3455   descr.shifts      = physBitmap->trueColor ? &physBitmap->color_shifts : NULL;
3456   descr.compression = dibSection.dsBmih.biCompression;
3457   descr.physBitmap  = physBitmap;
3458
3459   if(descr.infoBpp == 1)
3460       descr.colorMap = (void*)identity;
3461
3462   switch (descr.infoBpp)
3463   {
3464     case 1:
3465     case 4:
3466     case 8:
3467       descr.rMask = descr.gMask = descr.bMask = 0;
3468       break;
3469     case 15:
3470     case 16:
3471       descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0x7c00;
3472       descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x03e0;
3473       descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x001f;
3474       break;
3475
3476     case 24:
3477     case 32:
3478       descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0xff0000;
3479       descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x00ff00;
3480       descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x0000ff;
3481       break;
3482   }
3483
3484   /* Hack for now */
3485   descr.drawable  = dest;
3486   descr.gc        = gc;
3487   descr.xSrc      = xSrc;
3488   descr.ySrc      = ySrc;
3489   descr.xDest     = xDest;
3490   descr.yDest     = yDest;
3491   descr.width     = width;
3492   descr.height    = height;
3493   descr.sizeImage = 0;
3494
3495   descr.shm_mode = physBitmap->shm_mode;
3496 #ifdef HAVE_LIBXXSHM
3497   if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP && physBitmap->pixmap != dest)
3498   {
3499     descr.shm_mode = X11DRV_SHM_NONE;
3500   }
3501 #endif
3502   descr.dibpitch = dibSection.dsBm.bmWidthBytes;
3503
3504   if (toDIB)
3505     {
3506       TRACE("Copying from Pixmap to DIB bits\n");
3507       X11DRV_DIB_GetImageBits( &descr );
3508     }
3509   else
3510     {
3511       TRACE("Copying from DIB bits to Pixmap\n");
3512       X11DRV_DIB_SetImageBits( &descr );
3513     }
3514 }
3515
3516 /***********************************************************************
3517  *           X11DRV_DIB_DoUpdateDIBSection
3518  */
3519 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB)
3520 {
3521     BITMAP bitmap;
3522
3523     GetObjectW( physBitmap->hbitmap, sizeof(bitmap), &bitmap );
3524     X11DRV_DIB_DoCopyDIBSection(physBitmap, toDIB,
3525                                 physBitmap->colorMap, physBitmap->nColorMap,
3526                                 physBitmap->pixmap, get_bitmap_gc(physBitmap->depth),
3527                                 0, 0, 0, 0, bitmap.bmWidth, bitmap.bmHeight);
3528 }
3529
3530 /***********************************************************************
3531  *           X11DRV_DIB_FaultHandler
3532  */
3533 static LONG CALLBACK X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep )
3534 {
3535     X_PHYSBITMAP *physBitmap = NULL;
3536     BOOL found = FALSE;
3537     BYTE *addr;
3538     struct list *ptr;
3539     const size_t pagemask = getpagesize() - 1;
3540
3541     if (ep->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
3542         return EXCEPTION_CONTINUE_SEARCH;
3543
3544     addr = (BYTE *)ep->ExceptionRecord->ExceptionInformation[1];
3545
3546     EnterCriticalSection(&dibs_cs);
3547     LIST_FOR_EACH( ptr, &dibs_list )
3548     {
3549         physBitmap = LIST_ENTRY( ptr, X_PHYSBITMAP, entry );
3550         if ((physBitmap->base <= addr) &&
3551             (addr < physBitmap->base + ((physBitmap->size + pagemask) & ~pagemask)))
3552         {
3553             found = TRUE;
3554             break;
3555         }
3556     }
3557     LeaveCriticalSection(&dibs_cs);
3558
3559     if (!found) return EXCEPTION_CONTINUE_SEARCH;
3560
3561     if (addr >= physBitmap->base + physBitmap->size)
3562         WARN( "%p: access to %p beyond the end of the DIB\n", physBitmap->hbitmap, addr );
3563
3564     X11DRV_DIB_Lock( physBitmap, DIB_Status_None );
3565     if (ep->ExceptionRecord->ExceptionInformation[0] == EXCEPTION_WRITE_FAULT) {
3566         /* the app tried to write the DIB bits */
3567         X11DRV_DIB_Coerce( physBitmap, DIB_Status_AppMod);
3568     } else {
3569         /* the app tried to read the DIB bits */
3570         X11DRV_DIB_Coerce( physBitmap, DIB_Status_InSync);
3571     }
3572     X11DRV_DIB_Unlock( physBitmap, TRUE );
3573
3574     return EXCEPTION_CONTINUE_EXECUTION;
3575 }
3576
3577 /***********************************************************************
3578  *           X11DRV_DIB_Coerce
3579  */
3580 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *physBitmap, INT req)
3581 {
3582     INT ret = DIB_Status_None;
3583
3584     if (!physBitmap->image) return ret;  /* not a DIB section */
3585     EnterCriticalSection(&physBitmap->lock);
3586     ret = physBitmap->status;
3587     switch (req) {
3588     case DIB_Status_GdiMod:
3589       /* GDI access - request to draw on pixmap */
3590       switch (physBitmap->status)
3591       {
3592         default:
3593         case DIB_Status_None:
3594           physBitmap->p_status = DIB_Status_GdiMod;
3595           X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
3596           break;
3597
3598         case DIB_Status_GdiMod:
3599           TRACE("GdiMod requested in status GdiMod\n" );
3600           physBitmap->p_status = DIB_Status_GdiMod;
3601           break;
3602
3603         case DIB_Status_InSync:
3604           TRACE("GdiMod requested in status InSync\n" );
3605           X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
3606           physBitmap->status = DIB_Status_GdiMod;
3607           physBitmap->p_status = DIB_Status_InSync;
3608           break;
3609
3610         case DIB_Status_AppMod:
3611           TRACE("GdiMod requested in status AppMod\n" );
3612           /* make it readonly to avoid app changing data while we copy */
3613           X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
3614           X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
3615           X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
3616           physBitmap->p_status = DIB_Status_AppMod;
3617           physBitmap->status = DIB_Status_GdiMod;
3618           break;
3619       }
3620       break;
3621
3622     case DIB_Status_InSync:
3623       /* App access - request access to read DIB surface */
3624       /* (typically called from signal handler) */
3625       switch (physBitmap->status)
3626       {
3627         default:
3628         case DIB_Status_None:
3629           /* shouldn't happen from signal handler */
3630           break;
3631
3632         case DIB_Status_GdiMod:
3633           TRACE("InSync requested in status GdiMod\n" );
3634           X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
3635           X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
3636           X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
3637           physBitmap->status = DIB_Status_InSync;
3638           break;
3639
3640         case DIB_Status_InSync:
3641           TRACE("InSync requested in status InSync\n" );
3642           /* shouldn't happen from signal handler */
3643           break;
3644
3645         case DIB_Status_AppMod:
3646           TRACE("InSync requested in status AppMod\n" );
3647           /* no reason to do anything here, and this
3648            * shouldn't happen from signal handler */
3649           break;
3650       }
3651       break;
3652
3653     case DIB_Status_AppMod:
3654       /* App access - request access to write DIB surface */
3655       /* (typically called from signal handler) */
3656       switch (physBitmap->status)
3657       {
3658         default:
3659         case DIB_Status_None:
3660           /* shouldn't happen from signal handler */
3661           break;
3662
3663         case DIB_Status_GdiMod:
3664           TRACE("AppMod requested in status GdiMod\n" );
3665           X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
3666           X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
3667           physBitmap->status = DIB_Status_AppMod;
3668           break;
3669
3670         case DIB_Status_InSync:
3671           TRACE("AppMod requested in status InSync\n" );
3672           X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
3673           physBitmap->status = DIB_Status_AppMod;
3674           break;
3675
3676         case DIB_Status_AppMod:
3677           TRACE("AppMod requested in status AppMod\n" );
3678           /* shouldn't happen from signal handler */
3679           break;
3680       }
3681       break;
3682
3683       /* it is up to the caller to do the copy/conversion, probably
3684        * using the return value to decide where to copy from */
3685     }
3686     LeaveCriticalSection(&physBitmap->lock);
3687     return ret;
3688 }
3689
3690 /***********************************************************************
3691  *           X11DRV_DIB_Lock
3692  */
3693 INT X11DRV_DIB_Lock(X_PHYSBITMAP *physBitmap, INT req)
3694 {
3695     INT ret = DIB_Status_None;
3696
3697     if (!physBitmap->image) return ret;  /* not a DIB section */
3698     TRACE("Locking %p from thread %04x\n", physBitmap->hbitmap, GetCurrentThreadId());
3699     EnterCriticalSection(&physBitmap->lock);
3700     ret = physBitmap->status;
3701     if (req != DIB_Status_None)
3702       X11DRV_DIB_Coerce(physBitmap, req);
3703     return ret;
3704 }
3705
3706 /***********************************************************************
3707  *           X11DRV_DIB_Unlock
3708  */
3709 void X11DRV_DIB_Unlock(X_PHYSBITMAP *physBitmap, BOOL commit)
3710 {
3711     if (!physBitmap->image) return;  /* not a DIB section */
3712     switch (physBitmap->status)
3713     {
3714       default:
3715       case DIB_Status_None:
3716         /* in case anyone is wondering, this is the "signal handler doesn't
3717          * work" case, where we always have to be ready for app access */
3718         if (commit) {
3719           switch (physBitmap->p_status)
3720           {
3721             case DIB_Status_GdiMod:
3722               TRACE("Unlocking and syncing from GdiMod\n" );
3723               X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
3724               break;
3725
3726             default:
3727               TRACE("Unlocking without needing to sync\n" );
3728               break;
3729           }
3730         }
3731         else TRACE("Unlocking with no changes\n");
3732         physBitmap->p_status = DIB_Status_None;
3733         break;
3734
3735       case DIB_Status_GdiMod:
3736         TRACE("Unlocking in status GdiMod\n" );
3737         /* DIB was protected in Coerce */
3738         if (!commit) {
3739           /* no commit, revert to InSync if applicable */
3740           if ((physBitmap->p_status == DIB_Status_InSync) ||
3741               (physBitmap->p_status == DIB_Status_AppMod)) {
3742             X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
3743             physBitmap->status = DIB_Status_InSync;
3744           }
3745         }
3746         break;
3747
3748       case DIB_Status_InSync:
3749         TRACE("Unlocking in status InSync\n" );
3750         /* DIB was already protected in Coerce */
3751         break;
3752
3753       case DIB_Status_AppMod:
3754         TRACE("Unlocking in status AppMod\n" );
3755         /* DIB was already protected in Coerce */
3756         /* this case is ordinary only called from the signal handler,
3757          * so we don't bother to check for !commit */
3758         break;
3759     }
3760     LeaveCriticalSection(&physBitmap->lock);
3761     TRACE("Unlocked %p\n", physBitmap->hbitmap);
3762 }
3763
3764 /***********************************************************************
3765  *           X11DRV_CoerceDIBSection
3766  */
3767 INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req)
3768 {
3769     if (!physDev || !physDev->bitmap) return DIB_Status_None;
3770     return X11DRV_DIB_Coerce(physDev->bitmap, req);
3771 }
3772
3773 /***********************************************************************
3774  *           X11DRV_LockDIBSection
3775  */
3776 INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req)
3777 {
3778     if (!physDev || !physDev->bitmap) return DIB_Status_None;
3779     return X11DRV_DIB_Lock(physDev->bitmap, req);
3780 }
3781
3782 /***********************************************************************
3783  *           X11DRV_UnlockDIBSection
3784  */
3785 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
3786 {
3787     if (!physDev || !physDev->bitmap) return;
3788     X11DRV_DIB_Unlock(physDev->bitmap, commit);
3789 }
3790
3791
3792 #ifdef HAVE_LIBXXSHM
3793 /***********************************************************************
3794  *           X11DRV_XShmErrorHandler
3795  *
3796  */
3797 static int XShmErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
3798 {
3799     return 1;  /* FIXME: should check event contents */
3800 }
3801
3802 /***********************************************************************
3803  *           X11DRV_XShmCreateImage
3804  *
3805  */
3806 static XImage *X11DRV_XShmCreateImage( int width, int height, int bpp,
3807                                        XShmSegmentInfo* shminfo)
3808 {
3809     XImage *image;
3810
3811     image = XShmCreateImage(gdi_display, visual, bpp, ZPixmap, NULL, shminfo, width, height);
3812     if (image)
3813     {
3814         shminfo->shmid = shmget(IPC_PRIVATE, image->bytes_per_line * height,
3815                                   IPC_CREAT|0700);
3816         if( shminfo->shmid != -1 )
3817         {
3818             shminfo->shmaddr = shmat( shminfo->shmid, 0, 0 );
3819             if( shminfo->shmaddr != (char*)-1 )
3820             {
3821                 BOOL ok;
3822
3823                 shminfo->readOnly = FALSE;
3824                 X11DRV_expect_error( gdi_display, XShmErrorHandler, NULL );
3825                 ok = (XShmAttach( gdi_display, shminfo ) != 0);
3826                 XSync( gdi_display, False );
3827                 if (X11DRV_check_error()) ok = FALSE;
3828                 if (ok)
3829                 {
3830                     shmctl(shminfo->shmid, IPC_RMID, 0);
3831                     return image; /* Success! */
3832                 }
3833                 /* An error occurred */
3834                 shmdt(shminfo->shmaddr);
3835             }
3836             shmctl(shminfo->shmid, IPC_RMID, 0);
3837             shminfo->shmid = -1;
3838         }
3839         XFlush(gdi_display);
3840         XDestroyImage(image);
3841         image = NULL;
3842     }
3843     return image;
3844 }
3845 #endif /* HAVE_LIBXXSHM */
3846
3847 static Bool X11DRV_DIB_QueryXShm( Bool *pixmaps )
3848 {
3849     static Bool have_xshm, have_xshm_pixmaps;
3850     static BOOL initialized;
3851
3852     if (!initialized)
3853     {
3854 #ifdef HAVE_LIBXXSHM
3855         int major, minor;
3856
3857         have_xshm = XShmQueryVersion( gdi_display, &major, &minor, &have_xshm_pixmaps );
3858 #endif
3859         initialized = TRUE;
3860     }
3861
3862     *pixmaps = have_xshm_pixmaps;
3863     return have_xshm;
3864 }
3865
3866 /***********************************************************************
3867  *           X11DRV_CreateDIBSection   (X11DRV.@)
3868  */
3869 HBITMAP X11DRV_CreateDIBSection( PHYSDEV dev, HBITMAP hbitmap, BITMAPINFO *bmi, UINT usage )
3870 {
3871     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
3872     X_PHYSBITMAP *physBitmap;
3873     DIBSECTION dib;
3874 #ifdef HAVE_LIBXXSHM
3875     Bool pixmaps;
3876 #endif
3877
3878     if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return 0;
3879     physBitmap->topdown = bmi->bmiHeader.biHeight < 0;
3880     physBitmap->status = DIB_Status_None;
3881
3882     GetObjectW( hbitmap, sizeof(dib), &dib );
3883
3884     /* create color map */
3885     if (dib.dsBm.bmBitsPixel <= 8)
3886     {
3887         physBitmap->colorMap = X11DRV_DIB_BuildColorMap( physDev,
3888                                                          usage, dib.dsBm.bmBitsPixel, bmi,
3889                                                          &physBitmap->nColorMap );
3890     }
3891
3892     if (!X11DRV_XRender_SetPhysBitmapDepth( physBitmap, dib.dsBm.bmBitsPixel, &dib ))
3893     {
3894         if (dib.dsBm.bmBitsPixel == 1)
3895         {
3896             physBitmap->depth = 1;
3897             physBitmap->trueColor = FALSE;
3898         }
3899         else
3900         {
3901             physBitmap->depth = screen_depth;
3902             physBitmap->color_shifts = X11DRV_PALETTE_default_shifts;
3903             physBitmap->trueColor = (visual->class == TrueColor || visual->class == DirectColor);
3904         }
3905     }
3906
3907     /* create pixmap and X image */
3908     wine_tsx11_lock();
3909 #ifdef HAVE_LIBXXSHM
3910     physBitmap->shminfo.shmid = -1;
3911
3912     if (X11DRV_DIB_QueryXShm( &pixmaps )
3913             && (physBitmap->image = X11DRV_XShmCreateImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
3914                                                             physBitmap->depth, &physBitmap->shminfo )))
3915     {
3916         if (pixmaps)
3917         {
3918             physBitmap->shm_mode = X11DRV_SHM_PIXMAP;
3919             physBitmap->image->data = HeapAlloc( GetProcessHeap(), 0,
3920                     dib.dsBm.bmHeight * physBitmap->image->bytes_per_line );
3921         }
3922         else
3923         {
3924             physBitmap->shm_mode = X11DRV_SHM_IMAGE;
3925             physBitmap->image->data = physBitmap->shminfo.shmaddr;
3926         }
3927     }
3928     else
3929 #endif
3930     {
3931         physBitmap->shm_mode = X11DRV_SHM_NONE;
3932         physBitmap->image = X11DRV_DIB_CreateXImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
3933                                                      physBitmap->depth );
3934     }
3935
3936 #ifdef HAVE_LIBXXSHM
3937     if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP)
3938     {
3939         TRACE("Creating shared pixmap for bmp %p.\n", physBitmap->hbitmap);
3940         physBitmap->pixmap = XShmCreatePixmap( gdi_display, root_window,
3941                                                physBitmap->shminfo.shmaddr, &physBitmap->shminfo,
3942                                                dib.dsBm.bmWidth, dib.dsBm.bmHeight,
3943                                                physBitmap->depth );
3944     }
3945     else
3946 #endif
3947     {
3948         physBitmap->pixmap = XCreatePixmap( gdi_display, root_window, dib.dsBm.bmWidth,
3949                                             dib.dsBm.bmHeight, physBitmap->depth );
3950     }
3951
3952     wine_tsx11_unlock();
3953     if (!physBitmap->pixmap || !physBitmap->image) return 0;
3954
3955     if (physBitmap->trueColor)
3956     {
3957         ColorShifts *shifts = &physBitmap->color_shifts;
3958
3959         /* When XRender is around and used, we also support dibsections in other formats like 16-bit. In these
3960          * cases we need to override the mask of XImages. The reason is that during XImage creation the masks are
3961          * derived from a 24-bit visual (no 16-bit ones are around when X runs at 24-bit). SetImageBits and other
3962          * functions rely on the color masks for proper color conversion, so we need to override the masks here. */
3963         physBitmap->image->red_mask = shifts->physicalRed.max << shifts->physicalRed.shift;
3964         physBitmap->image->green_mask = shifts->physicalGreen.max << shifts->physicalGreen.shift;
3965         physBitmap->image->blue_mask = shifts->physicalBlue.max << shifts->physicalBlue.shift;
3966     }
3967
3968       /* install fault handler */
3969     InitializeCriticalSection( &physBitmap->lock );
3970     physBitmap->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": X_PHYSBITMAP.lock");
3971
3972     physBitmap->base   = dib.dsBm.bmBits;
3973     physBitmap->size   = dib.dsBmih.biSizeImage;
3974     physBitmap->status = DIB_Status_AppMod;
3975
3976     if (!dibs_handler)
3977         dibs_handler = AddVectoredExceptionHandler( TRUE, X11DRV_DIB_FaultHandler );
3978     EnterCriticalSection( &dibs_cs );
3979     list_add_head( &dibs_list, &physBitmap->entry );
3980     LeaveCriticalSection( &dibs_cs );
3981
3982     X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
3983
3984     return hbitmap;
3985 }
3986
3987 /***********************************************************************
3988  *           X11DRV_DIB_DeleteDIBSection
3989  */
3990 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP *physBitmap, DIBSECTION *dib)
3991 {
3992   BOOL last;
3993
3994   EnterCriticalSection( &dibs_cs );
3995   list_remove( &physBitmap->entry );
3996   last = list_empty( &dibs_list );
3997   LeaveCriticalSection( &dibs_cs );
3998
3999   if (last)
4000   {
4001       RemoveVectoredExceptionHandler( dibs_handler );
4002       dibs_handler = NULL;
4003   }
4004
4005   if (dib->dshSection)
4006       X11DRV_DIB_Coerce(physBitmap, DIB_Status_InSync);
4007
4008   if (physBitmap->image)
4009   {
4010       wine_tsx11_lock();
4011 #ifdef HAVE_LIBXXSHM
4012       if (physBitmap->shminfo.shmid != -1)
4013       {
4014           XShmDetach( gdi_display, &(physBitmap->shminfo) );
4015           if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP) X11DRV_DIB_DestroyXImage( physBitmap->image );
4016           else XDestroyImage( physBitmap->image );
4017           shmdt( physBitmap->shminfo.shmaddr );
4018           physBitmap->shminfo.shmid = -1;
4019           physBitmap->shm_mode = X11DRV_SHM_NONE;
4020       }
4021       else
4022 #endif
4023           X11DRV_DIB_DestroyXImage( physBitmap->image );
4024       wine_tsx11_unlock();
4025   }
4026
4027   HeapFree(GetProcessHeap(), 0, physBitmap->colorMap);
4028   physBitmap->lock.DebugInfo->Spare[0] = 0;
4029   DeleteCriticalSection(&physBitmap->lock);
4030 }
4031
4032 /***********************************************************************
4033  *           SetDIBColorTable   (X11DRV.@)
4034  */
4035 UINT X11DRV_SetDIBColorTable( PHYSDEV dev, UINT start, UINT count, const RGBQUAD *colors )
4036 {
4037     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
4038     DIBSECTION dib;
4039     UINT ret = 0;
4040     X_PHYSBITMAP *physBitmap = physDev->bitmap;
4041
4042     if (!physBitmap) return 0;
4043     GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib );
4044
4045     if (physBitmap->colorMap && start < physBitmap->nColorMap) {
4046         UINT end = count + start;
4047         if (end > physBitmap->nColorMap) end = physBitmap->nColorMap;
4048         /*
4049          * Changing color table might change the mapping between
4050          * DIB colors and X11 colors and thus alter the visible state
4051          * of the bitmap object.
4052          */
4053         /*
4054          * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4055          * at least for a 1 bpp dibsection
4056          */
4057         X11DRV_DIB_Lock( physBitmap, DIB_Status_AppMod );
4058         X11DRV_DIB_GenColorMap( physDev, physBitmap->colorMap, DIB_RGB_COLORS,
4059                                 dib.dsBm.bmBitsPixel, colors, start, end );
4060         X11DRV_DIB_Unlock( physBitmap, TRUE );
4061         ret = end - start;
4062     }
4063     return ret;
4064 }
4065
4066
4067 /***********************************************************************
4068  *           X11DRV_DIB_CreateDIBFromBitmap
4069  *
4070  *  Allocates a packed DIB and copies the bitmap data into it.
4071  */
4072 HGLOBAL X11DRV_DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
4073 {
4074     BITMAP bmp;
4075     HGLOBAL hPackedDIB;
4076     LPBYTE pPackedDIB;
4077     LPBITMAPINFOHEADER pbmiHeader;
4078     unsigned int cDataSize, cPackedSize, OffsetBits;
4079     int nLinesCopied;
4080
4081     if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
4082
4083     /*
4084      * A packed DIB contains a BITMAPINFO structure followed immediately by
4085      * an optional color palette and the pixel data.
4086      */
4087
4088     /* Calculate the size of the packed DIB */
4089     cDataSize = X11DRV_DIB_GetDIBWidthBytes( bmp.bmWidth, bmp.bmBitsPixel ) * abs( bmp.bmHeight );
4090     cPackedSize = sizeof(BITMAPINFOHEADER)
4091                   + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
4092                   + cDataSize;
4093     /* Get the offset to the bits */
4094     OffsetBits = cPackedSize - cDataSize;
4095
4096     /* Allocate the packed DIB */
4097     TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
4098     hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
4099                              cPackedSize );
4100     if ( !hPackedDIB )
4101     {
4102         WARN("Could not allocate packed DIB!\n");
4103         return 0;
4104     }
4105
4106     /* A packed DIB starts with a BITMAPINFOHEADER */
4107     pPackedDIB = GlobalLock(hPackedDIB);
4108     pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
4109
4110     /* Init the BITMAPINFOHEADER */
4111     pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
4112     pbmiHeader->biWidth = bmp.bmWidth;
4113     pbmiHeader->biHeight = bmp.bmHeight;
4114     pbmiHeader->biPlanes = 1;
4115     pbmiHeader->biBitCount = bmp.bmBitsPixel;
4116     pbmiHeader->biCompression = BI_RGB;
4117     pbmiHeader->biSizeImage = 0;
4118     pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
4119     pbmiHeader->biClrUsed = 0;
4120     pbmiHeader->biClrImportant = 0;
4121
4122     /* Retrieve the DIB bits from the bitmap and fill in the
4123      * DIB color table if present */
4124
4125     nLinesCopied = GetDIBits(hdc,                       /* Handle to device context */
4126                              hBmp,                      /* Handle to bitmap */
4127                              0,                         /* First scan line to set in dest bitmap */
4128                              bmp.bmHeight,              /* Number of scan lines to copy */
4129                              pPackedDIB + OffsetBits,   /* [out] Address of array for bitmap bits */
4130                              (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
4131                              0);                        /* RGB or palette index */
4132     GlobalUnlock(hPackedDIB);
4133
4134     /* Cleanup if GetDIBits failed */
4135     if (nLinesCopied != bmp.bmHeight)
4136     {
4137         TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
4138         GlobalFree(hPackedDIB);
4139         hPackedDIB = 0;
4140     }
4141     return hPackedDIB;
4142 }
4143
4144
4145 /**************************************************************************
4146  *              X11DRV_DIB_CreateDIBFromPixmap
4147  *
4148  *  Allocates a packed DIB and copies the Pixmap data into it.
4149  *  The Pixmap passed in is deleted after the conversion.
4150  */
4151 HGLOBAL X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap, HDC hdc)
4152 {
4153     HDC hdcMem;
4154     X_PHYSBITMAP *physBitmap;
4155     Pixmap orig_pixmap;
4156     HBITMAP hBmp = 0;
4157     HGLOBAL hPackedDIB = 0;
4158     Window root;
4159     int x,y;               /* Unused */
4160     unsigned border_width; /* Unused */
4161     unsigned int depth, width, height;
4162
4163     /* Get the Pixmap dimensions and bit depth */
4164     wine_tsx11_lock();
4165     if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
4166                       &border_width, &depth)) depth = 0;
4167     wine_tsx11_unlock();
4168     if (!pixmap_formats[depth]) return 0;
4169
4170     TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
4171           width, height, depth);
4172
4173     /*
4174      * Create an HBITMAP with the same dimensions and BPP as the pixmap,
4175      * and make it a container for the pixmap passed.
4176      */
4177     if (!(hBmp = CreateBitmap( width, height, 1, pixmap_formats[depth]->bits_per_pixel, NULL ))) return 0;
4178
4179     /* force bitmap to be owned by a screen DC */
4180     hdcMem = CreateCompatibleDC( hdc );
4181     SelectObject( hdcMem, SelectObject( hdcMem, hBmp ));
4182     DeleteDC( hdcMem );
4183
4184     physBitmap = X11DRV_get_phys_bitmap( hBmp );
4185
4186     /* swap the new pixmap in */
4187     orig_pixmap = physBitmap->pixmap;
4188     physBitmap->pixmap = pixmap;
4189
4190     /*
4191      * Create a packed DIB from the Pixmap wrapper bitmap created above.
4192      * A packed DIB contains a BITMAPINFO structure followed immediately by
4193      * an optional color palette and the pixel data.
4194      */
4195     hPackedDIB = X11DRV_DIB_CreateDIBFromBitmap(hdc, hBmp);
4196
4197     /* we can now get rid of the HBITMAP and its original pixmap */
4198     physBitmap->pixmap = orig_pixmap;
4199     DeleteObject(hBmp);
4200
4201     TRACE("\tReturning packed DIB %p\n", hPackedDIB);
4202     return hPackedDIB;
4203 }
4204
4205
4206 /**************************************************************************
4207  *                 X11DRV_DIB_CreatePixmapFromDIB
4208  *
4209  *    Creates a Pixmap from a packed DIB
4210  */
4211 Pixmap X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB, HDC hdc )
4212 {
4213     Pixmap pixmap = 0;
4214     X_PHYSBITMAP *physBitmap;
4215     HBITMAP hBmp;
4216     LPBITMAPINFO pbmi;
4217     HDC memdc;
4218
4219     /* Create a DDB from the DIB */
4220
4221     pbmi = GlobalLock(hPackedDIB);
4222     hBmp = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT,
4223                           (LPBYTE)pbmi + bitmap_info_size( pbmi, DIB_RGB_COLORS ),
4224                           pbmi, DIB_RGB_COLORS);
4225     GlobalUnlock(hPackedDIB);
4226
4227     /* make sure it's owned by x11drv */
4228     memdc = CreateCompatibleDC( hdc );
4229     SelectObject( memdc, hBmp );
4230     DeleteDC( memdc );
4231
4232     /* clear the physBitmap so that we can steal its pixmap */
4233     if ((physBitmap = X11DRV_get_phys_bitmap( hBmp )))
4234     {
4235         pixmap = physBitmap->pixmap;
4236         physBitmap->pixmap = 0;
4237     }
4238
4239     /* Delete the DDB we created earlier now that we have stolen its pixmap */
4240     DeleteObject(hBmp);
4241
4242     TRACE("Returning Pixmap %lx\n", pixmap);
4243     return pixmap;
4244 }