2 * X11DRV device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
26 #include <X11/extensions/XShm.h>
27 # ifdef HAVE_SYS_SHM_H
30 # ifdef HAVE_SYS_IPC_H
33 #endif /* defined(HAVE_LIBXXSHM) */
42 #include "wine/exception.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
47 static struct list dibs_list = LIST_INIT(dibs_list);
49 static CRITICAL_SECTION dibs_cs;
50 static CRITICAL_SECTION_DEBUG dibs_cs_debug =
53 { &dibs_cs_debug.ProcessLocksList, &dibs_cs_debug.ProcessLocksList },
54 0, 0, { (DWORD_PTR)(__FILE__ ": dibs_cs") }
56 static CRITICAL_SECTION dibs_cs = { &dibs_cs_debug, -1, 0, 0, 0, 0 };
58 static PVOID dibs_handler;
60 static int ximageDepthTable[32];
62 /* This structure holds the arguments for DIB_SetImageBits() */
65 X11DRV_PDEVICE *physDev;
68 PALETTEENTRY *palentry;
88 enum x11drv_shm_mode shm_mode;
91 X_PHYSBITMAP *physBitmap;
92 } X11DRV_DIB_IMAGEBITS_DESCR;
97 RLE_EOL = 0, /* End of line */
98 RLE_END = 1, /* End of bitmap */
99 RLE_DELTA = 2 /* Delta */
103 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *,INT);
106 Some of the following helper functions are duplicated in
110 /***********************************************************************
111 * DIB_DoProtectDIBSection
113 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP *physBitmap, DWORD new_prot )
117 VirtualProtect(physBitmap->base, physBitmap->size, new_prot, &old_prot);
118 TRACE("Changed protection from %d to %d\n", old_prot, new_prot);
121 /***********************************************************************
122 * X11DRV_DIB_GetXImageWidthBytes
124 * Return the width of an X image in bytes
126 static inline int X11DRV_DIB_GetXImageWidthBytes( int width, int depth )
128 if (!depth || depth > 32) goto error;
130 if (!ximageDepthTable[depth-1])
132 XImage *testimage = XCreateImage( gdi_display, visual, depth,
133 ZPixmap, 0, NULL, 1, 1, 32, 20 );
136 ximageDepthTable[depth-1] = testimage->bits_per_pixel;
137 XDestroyImage( testimage );
139 else ximageDepthTable[depth-1] = -1;
141 if (ximageDepthTable[depth-1] != -1)
142 return (4 * ((width * ximageDepthTable[depth-1] + 31) / 32));
145 WARN( "(%d): Unsupported depth\n", depth );
150 /***********************************************************************
151 * X11DRV_DIB_GetDIBWidthBytes
153 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
155 static int X11DRV_DIB_GetDIBWidthBytes( int width, int depth )
157 return ((width * depth + 31) / 8) & ~3;
161 /***********************************************************************
164 * Return the size of the bitmap info structure including color table.
166 int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
168 unsigned int colors, size, masks = 0;
170 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
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));
177 else /* assume BITMAPINFOHEADER */
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));
189 /***********************************************************************
190 * X11DRV_DIB_CreateXImage
194 XImage *X11DRV_DIB_CreateXImage( int width, int height, int depth )
197 XImage *image = NULL;
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 );
211 /***********************************************************************
212 * X11DRV_DIB_DestroyXImage
214 * Destroy an X image created with X11DRV_DIB_CreateXImage.
216 void X11DRV_DIB_DestroyXImage( XImage *image )
218 HeapFree( GetProcessHeap(), 0, image->data );
221 XDestroyImage( image );
226 /***********************************************************************
227 * X11DRV_DIB_GetColorCount
229 * Computes the number of colors for the bitmap palette.
230 * Should not be called for a >8-bit deep bitmap.
232 static unsigned int X11DRV_DIB_GetColorCount(const BITMAPINFO *info)
234 unsigned int colors = min( info->bmiHeader.biClrUsed, 256 );
235 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
240 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
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);
246 /***********************************************************************
247 * X11DRV_DIB_GenColorMap
249 * Fills the color map of a bitmap palette. Should not be called
250 * for a >8-bit deep bitmap.
252 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
253 WORD coloruse, WORD depth, const void *colorPtr, int start, int end )
257 if (coloruse == DIB_RGB_COLORS)
259 const RGBQUAD * rgb = colorPtr;
261 if (depth == 1) /* Monochrome */
266 if (GetDIBColorTable( physDev->dev.hdc, 0, 2, table ) == 2)
267 invert = !colour_is_brighter(table[1], table[0]);
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));
276 for (i = start; i < end; i++, rgb++)
277 colorMapping[i] = X11DRV_PALETTE_LookupPixel(physDev->color_shifts, RGB(rgb->rgbRed,
281 else /* DIB_PAL_COLORS */
283 const WORD * index = colorPtr;
285 for (i = start; i < end; i++, index++)
286 colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(*index) );
292 /***********************************************************************
293 * X11DRV_DIB_BuildColorMap
295 * Build the color map from the bitmap palette. Should not be called
296 * for a >8-bit deep bitmap.
298 static int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE *physDev, WORD coloruse, WORD depth,
299 const BITMAPINFO *info, int *nColors )
301 const void *colorPtr;
305 *nColors = X11DRV_DIB_GetColorCount(info);
306 if (!*nColors) return NULL;
308 colorPtr = (const BYTE*)info + (WORD)info->bmiHeader.biSize;
309 if (!(colorMapping = HeapAlloc(GetProcessHeap(), 0, *nColors * sizeof(int) )))
312 return X11DRV_DIB_GenColorMap( physDev, colorMapping, coloruse, depth,
313 colorPtr, 0, *nColors);
316 /***********************************************************************
317 * X11DRV_DIB_MapColor
319 static int X11DRV_DIB_MapColor( int *physMap, int nPhysMap, int phys, int oldcol )
323 if ((oldcol < nPhysMap) && (physMap[oldcol] == phys))
326 for (color = 0; color < nPhysMap; color++)
327 if (physMap[color] == phys)
330 WARN("Strange color %08x\n", phys);
335 /*********************************************************************
336 * X11DRV_DIB_GetNearestIndex
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.
342 static INT X11DRV_DIB_GetNearestIndex(RGBQUAD *colormap, int numColors, BYTE r, BYTE g, BYTE b)
344 INT i, best = -1, diff, bestdiff = -1;
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);
353 if(best == -1 || diff < bestdiff) {
360 /*********************************************************************
361 * X11DRV_DIB_MaskToShift
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.
367 INT X11DRV_DIB_MaskToShift(DWORD mask)
375 while ((mask&1)==0) {
382 /***********************************************************************
383 * X11DRV_DIB_CheckMask
385 * Check RGB mask if it is either 0 or matches visual's mask.
387 static inline int X11DRV_DIB_CheckMask(int red_mask, int green_mask, int blue_mask)
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 );
394 /***********************************************************************
395 * X11DRV_DIB_SetImageBits_1
397 * SetDIBits for a 1-bit deep DIB.
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)
410 srcbits = srcbits + linebytes * (lines - 1);
411 linebytes = -linebytes;
414 if ((extra = (left & 7)) != 0) {
418 srcbits += left >> 3;
419 width = min(srcwidth, dstwidth);
421 /* ==== pal 1 dib -> any bmp format ==== */
422 for (h = lines-1; h >=0; h--) {
424 for (i = width/8, x = left; i > 0; i--) {
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] );
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]);
448 srcbits += linebytes;
452 /***********************************************************************
453 * X11DRV_DIB_GetImageBits_1
455 * GetDIBits for a 1-bit deep DIB.
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 )
463 int h, width = min(dstwidth, srcwidth);
467 dstbits = dstbits + linebytes * (lines - 1);
468 linebytes = -linebytes;
471 switch (bmpImage->depth)
475 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
477 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
480 for (h=lines-1; h>=0; h--) {
484 for (x=0; x<width; x++) {
486 srcval=srccolors[XGetPixel(bmpImage, x, h)];
487 dstval|=(X11DRV_DIB_GetNearestIndex
491 srcval.peBlue) << (7 - (x & 7)));
500 /* pad with 0 to DWORD alignment */
501 for (x = (x+7)&~7; x < ((width + 31) & ~31); x+=8)
503 dstbits += linebytes;
511 if (X11DRV_DIB_CheckMask(bmpImage->red_mask, bmpImage->green_mask, bmpImage->blue_mask)
513 /* ==== pal 8 bmp -> pal 1 dib ==== */
515 const BYTE* srcpixel;
518 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
520 for (h=0; h<lines; h++) {
525 for (x=0; x<width; x++) {
527 srcval=srccolors[*srcpixel++];
528 dstval|=(X11DRV_DIB_GetNearestIndex
532 srcval.peBlue) << (7-(x&7)) );
541 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
542 dstbits += linebytes;
553 const WORD* srcpixel;
556 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
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++) {
566 for (x=0; x<width; x++) {
569 dstval|=(X11DRV_DIB_GetNearestIndex
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)) );
585 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
586 dstbits += linebytes;
588 } else if (bmpImage->blue_mask==0x7c00) {
589 /* ==== bgr 555 bmp -> pal 1 dib ==== */
590 for (h=0; h<lines; h++) {
595 for (x=0; x<width; x++) {
598 dstval|=(X11DRV_DIB_GetNearestIndex
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)) );
614 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
615 dstbits += linebytes;
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++) {
628 for (x=0; x<width; x++) {
631 dstval|=(X11DRV_DIB_GetNearestIndex
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)) );
647 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
648 dstbits += linebytes;
650 } else if (bmpImage->blue_mask==0xf800) {
651 /* ==== bgr 565 bmp -> pal 1 dib ==== */
652 for (h=0; h<lines; h++) {
657 for (x=0; x<width; x++) {
660 dstval|=(X11DRV_DIB_GetNearestIndex
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)) );
676 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
677 dstbits += linebytes;
696 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
697 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
699 if (bmpImage->green_mask!=0x00ff00 ||
700 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
702 } else if (bmpImage->blue_mask==0xff) {
703 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
704 for (h=0; h<lines; h++) {
709 for (x=0; x<width; x++) {
710 dstval|=(X11DRV_DIB_GetNearestIndex
714 srcbyte[0]) << (7-(x&7)) );
715 srcbyte+=bytes_per_pixel;
724 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
725 dstbits += linebytes;
728 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
729 for (h=0; h<lines; h++) {
734 for (x=0; x<width; x++) {
735 dstval|=(X11DRV_DIB_GetNearestIndex
739 srcbyte[2]) << (7-(x&7)) );
740 srcbyte+=bytes_per_pixel;
749 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
750 dstbits += linebytes;
761 unsigned long white = (1 << bmpImage->bits_per_pixel) - 1;
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 )
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" );
774 for (h=lines-1; h>=0; h--) {
778 for (x=0; x<width; x++) {
779 dstval|=((XGetPixel( bmpImage, x, h) >= white) ^ neg) << (7 - (x&7));
788 dstbits += linebytes;
795 /***********************************************************************
796 * X11DRV_DIB_SetImageBits_4
798 * SetDIBits for a 4-bit deep DIB.
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)
810 srcbits = srcbits + linebytes * (lines - 1);
811 linebytes = -linebytes;
818 srcbits += left >> 1;
819 width = min(srcwidth, dstwidth);
821 /* ==== pal 4 dib -> any bmp format ==== */
822 for (h = lines-1; h >= 0; h--) {
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] );
830 XPutPixel( bmpImage, x, h, colors[*srcbyte >> 4] );
831 srcbits += linebytes;
837 /***********************************************************************
838 * X11DRV_DIB_GetImageBits_4
840 * GetDIBits for a 4-bit deep DIB.
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 )
848 int h, width = min(srcwidth, dstwidth);
853 dstbits = dstbits + ( linebytes * (lines-1) );
854 linebytes = -linebytes;
857 switch (bmpImage->depth) {
860 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
862 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
865 for (h = lines-1; h >= 0; h--) {
869 for (x = 0; x < width; x++) {
871 srcval=srccolors[XGetPixel(bmpImage, x, h)];
872 dstval|=(X11DRV_DIB_GetNearestIndex
876 srcval.peBlue) << (4-((x&1)<<2)));
885 dstbits += linebytes;
893 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
895 /* ==== pal 8 bmp -> pal 4 dib ==== */
897 const BYTE *srcpixel;
900 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
901 for (h=0; h<lines; h++) {
906 for (x=0; x<width; x++) {
908 srcval = srccolors[*srcpixel++];
909 dstval|=(X11DRV_DIB_GetNearestIndex
913 srcval.peBlue) << (4*(1-(x&1))) );
922 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
923 dstbits += linebytes;
934 const WORD* srcpixel;
937 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
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++) {
947 for (x=0; x<width; x++) {
950 dstval|=(X11DRV_DIB_GetNearestIndex
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) );
966 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
967 dstbits += linebytes;
969 } else if (bmpImage->blue_mask==0x7c00) {
970 /* ==== bgr 555 bmp -> pal 4 dib ==== */
971 for (h=0; h<lines; h++) {
976 for (x=0; x<width; x++) {
979 dstval|=(X11DRV_DIB_GetNearestIndex
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) );
995 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
996 dstbits += linebytes;
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++) {
1009 for (x=0; x<width; x++) {
1012 dstval|=(X11DRV_DIB_GetNearestIndex
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) );
1028 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1029 dstbits += linebytes;
1031 } else if (bmpImage->blue_mask==0xf800) {
1032 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1033 for (h=0; h<lines; h++) {
1038 for (x=0; x<width; x++) {
1041 dstval|=(X11DRV_DIB_GetNearestIndex
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) );
1057 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1058 dstbits += linebytes;
1070 if (bmpImage->bits_per_pixel==24) {
1071 const void* srcbits;
1072 const BYTE *srcbyte;
1075 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1077 if (bmpImage->green_mask!=0x00ff00 ||
1078 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1080 } else if (bmpImage->blue_mask==0xff) {
1081 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1082 for (h=0; h<lines; h++) {
1085 for (x=0; x<width/2; x++) {
1086 /* Do 2 pixels at a time */
1087 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1092 X11DRV_DIB_GetNearestIndex
1100 /* And then the odd pixel */
1101 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1107 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1108 dstbits += linebytes;
1111 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1112 for (h=0; h<lines; h++) {
1115 for (x=0; x<width/2; x++) {
1116 /* Do 2 pixels at a time */
1117 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1122 X11DRV_DIB_GetNearestIndex
1130 /* And then the odd pixel */
1131 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1137 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1138 dstbits += linebytes;
1147 const void* srcbits;
1148 const BYTE *srcbyte;
1151 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1153 if (bmpImage->green_mask!=0x00ff00 ||
1154 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1156 } else if (bmpImage->blue_mask==0xff) {
1157 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1158 for (h=0; h<lines; h++) {
1161 for (x=0; x<width/2; x++) {
1162 /* Do 2 pixels at a time */
1163 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1168 X11DRV_DIB_GetNearestIndex
1176 /* And then the odd pixel */
1177 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1183 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1184 dstbits += linebytes;
1187 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1188 for (h=0; h<lines; h++) {
1191 for (x=0; x<width/2; x++) {
1192 /* Do 2 pixels at a time */
1193 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1198 X11DRV_DIB_GetNearestIndex
1206 /* And then the odd pixel */
1207 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1213 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1214 dstbits += linebytes;
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--) {
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);
1236 *dstbyte=(X11DRV_DIB_MapColor((int *)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4);
1238 dstbits += linebytes;
1245 /***********************************************************************
1246 * X11DRV_DIB_SetImageBits_8
1248 * SetDIBits for an 8-bit deep DIB.
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,
1256 int h, width = min(srcwidth, dstwidth);
1257 const BYTE* srcbyte;
1263 srcbits = srcbits + linebytes * (lines-1);
1264 linebytes = -linebytes;
1269 switch (bmpImage->depth) {
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) )
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]];
1286 /* And then the odd pixel */
1287 *((WORD*)dstpixel)=colors[srcbyte[0]];
1289 srcbyte = (srcbits += linebytes);
1290 dstbits -= bmpImage->bytes_per_line;
1297 if (lines && width && (bmpImage->bits_per_pixel == 32) &&
1298 (ImageByteOrder(gdi_display)==LSBFirst) )
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++];
1307 srcbyte = (srcbits += linebytes);
1308 dstbits -= bmpImage->bytes_per_line;
1314 break; /* use slow generic case below */
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++]);
1322 srcbyte = (srcbits += linebytes);
1326 /***********************************************************************
1327 * X11DRV_DIB_GetImageBits_8
1329 * GetDIBits for an 8-bit deep DIB.
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 )
1337 int h, width = min(srcwidth, dstwidth);
1343 dstbits = dstbits + ( linebytes * (lines-1) );
1344 linebytes = -linebytes;
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)
1354 if (!srccolors) goto updatesection;
1356 switch (bmpImage->depth) {
1359 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1362 /* ==== pal 1 bmp -> pal 8 dib ==== */
1363 /* ==== pal 4 bmp -> pal 8 dib ==== */
1364 for (h=lines-1; h>=0; h--) {
1366 for (x=0; x<width; x++) {
1367 PALETTEENTRY srcval;
1368 srcval=srccolors[XGetPixel(bmpImage, x, h)];
1369 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1374 dstbits += linebytes;
1382 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1384 /* ==== pal 8 bmp -> pal 8 dib ==== */
1385 const void* srcbits;
1386 const BYTE* srcpixel;
1388 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1389 for (h=0; h<lines; h++) {
1392 for (x = 0; x < width; x++) {
1393 PALETTEENTRY srcval;
1394 srcval=srccolors[*srcpixel++];
1395 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1400 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1401 dstbits += linebytes;
1411 const void* srcbits;
1412 const WORD* srcpixel;
1415 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
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++) {
1423 for (x=0; x<width; x++) {
1426 *dstbyte++=X11DRV_DIB_GetNearestIndex
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) );
1435 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1436 dstbits += linebytes;
1438 } else if (bmpImage->blue_mask==0x7c00) {
1439 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1440 for (h=0; h<lines; h++) {
1443 for (x=0; x<width; x++) {
1446 *dstbyte++=X11DRV_DIB_GetNearestIndex
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) );
1455 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1456 dstbits += linebytes;
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++) {
1467 for (x=0; x<width; x++) {
1470 *dstbyte++=X11DRV_DIB_GetNearestIndex
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) );
1479 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1480 dstbits += linebytes;
1482 } else if (bmpImage->blue_mask==0xf800) {
1483 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1484 for (h=0; h<lines; h++) {
1487 for (x=0; x<width; x++) {
1490 *dstbyte++=X11DRV_DIB_GetNearestIndex
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) );
1499 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1500 dstbits += linebytes;
1514 const void* srcbits;
1515 const BYTE *srcbyte;
1517 int bytes_per_pixel;
1519 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1520 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
1522 if (bmpImage->green_mask!=0x00ff00 ||
1523 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1525 } else if (bmpImage->blue_mask==0xff) {
1526 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1527 for (h=0; h<lines; h++) {
1530 for (x=0; x<width; x++) {
1531 *dstbyte++=X11DRV_DIB_GetNearestIndex
1536 srcbyte+=bytes_per_pixel;
1538 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1539 dstbits += linebytes;
1542 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1543 for (h=0; h<lines; h++) {
1546 for (x=0; x<width; x++) {
1547 *dstbyte++=X11DRV_DIB_GetNearestIndex
1552 srcbyte+=bytes_per_pixel;
1554 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1555 dstbits += linebytes;
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 );
1567 /* ==== any bmp format -> pal 8 dib ==== */
1568 for (h=lines-1; h>=0; h--) {
1570 for (x=0; x<width; x++) {
1571 *dstbyte=X11DRV_DIB_MapColor
1573 XGetPixel(bmpImage, x, h), *dstbyte);
1576 dstbits += linebytes;
1582 /***********************************************************************
1583 * X11DRV_DIB_SetImageBits_16
1585 * SetDIBits for a 16-bit deep DIB.
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 )
1593 int h, width = min(srcwidth, dstwidth);
1594 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
1599 srcbits = srcbits + ( linebytes * (lines-1));
1600 linebytes = -linebytes;
1603 switch (bmpImage->depth)
1610 srcbits=srcbits+left*2;
1611 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
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
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
1628 dstbits,-bmpImage->bytes_per_line);
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
1637 dstbits,-bmpImage->bytes_per_line);
1639 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1640 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1641 convs->Convert_565_to_555_reverse
1644 dstbits,-bmpImage->bytes_per_line);
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
1655 dstbits,-bmpImage->bytes_per_line);
1657 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
1658 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
1659 convs->Convert_565_reverse
1662 dstbits,-bmpImage->bytes_per_line);
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
1671 dstbits,-bmpImage->bytes_per_line);
1673 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
1674 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
1675 convs->Convert_555_to_565_reverse
1678 dstbits,-bmpImage->bytes_per_line);
1688 if (bmpImage->bits_per_pixel==24) {
1691 srcbits=srcbits+left*2;
1692 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
1694 if (bmpImage->green_mask!=0x00ff00 ||
1695 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1697 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
1698 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
1700 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
1701 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
1702 convs->Convert_555_to_888_asis
1705 dstbits,-bmpImage->bytes_per_line);
1707 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
1708 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
1709 convs->Convert_565_to_888_asis
1712 dstbits,-bmpImage->bytes_per_line);
1716 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
1717 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
1718 convs->Convert_555_to_888_reverse
1721 dstbits,-bmpImage->bytes_per_line);
1723 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
1724 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
1725 convs->Convert_565_to_888_reverse
1728 dstbits,-bmpImage->bytes_per_line);
1739 srcbits=srcbits+left*2;
1740 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
1742 if (bmpImage->green_mask!=0x00ff00 ||
1743 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1745 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
1746 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
1748 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
1749 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
1750 convs->Convert_555_to_0888_asis
1753 dstbits,-bmpImage->bytes_per_line);
1755 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
1756 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
1757 convs->Convert_565_to_0888_asis
1760 dstbits,-bmpImage->bytes_per_line);
1764 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
1765 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
1766 convs->Convert_555_to_0888_reverse
1769 dstbits,-bmpImage->bytes_per_line);
1771 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
1772 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
1773 convs->Convert_565_to_0888_reverse
1776 dstbits,-bmpImage->bytes_per_line);
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 );
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;
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;
1806 /* Green has 5 bits, like the others */
1810 /* Green has 6 bits, not 5. Compensate. */
1819 /* We could split it into four separate cases to optimize
1820 * but it is probably not worth it.
1822 for (h=lines-1; h>=0; h--) {
1823 srcpixel=(const WORD*)srcbits;
1824 for (x=left; x<width+left; x++) {
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)));
1838 srcbits += linebytes;
1846 /***********************************************************************
1847 * X11DRV_DIB_GetImageBits_16
1849 * GetDIBits for an 16-bit deep DIB.
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 )
1858 int h, width = min(srcwidth, dstwidth);
1859 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
1864 dstbits = dstbits + ( linebytes * (lines-1));
1865 linebytes = -linebytes;
1868 switch (bmpImage->depth)
1873 const char* srcbits;
1875 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
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
1884 srcbits,-bmpImage->bytes_per_line,
1887 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
1888 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
1889 convs->Convert_555_reverse
1891 srcbits,-bmpImage->bytes_per_line,
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
1900 srcbits,-bmpImage->bytes_per_line,
1903 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
1904 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
1905 convs->Convert_555_to_565_reverse
1907 srcbits,-bmpImage->bytes_per_line,
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
1918 srcbits,-bmpImage->bytes_per_line,
1921 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
1922 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
1923 convs->Convert_565_reverse
1925 srcbits,-bmpImage->bytes_per_line,
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
1934 srcbits,-bmpImage->bytes_per_line,
1937 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
1938 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
1939 convs->Convert_565_to_555_reverse
1941 srcbits,-bmpImage->bytes_per_line,
1952 if (bmpImage->bits_per_pixel == 24) {
1953 const char* srcbits;
1955 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1957 if (bmpImage->green_mask!=0x00ff00 ||
1958 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1960 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
1961 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
1963 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
1964 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
1965 convs->Convert_888_to_555_asis
1967 srcbits,-bmpImage->bytes_per_line,
1970 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
1971 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
1972 convs->Convert_888_to_565_asis
1974 srcbits,-bmpImage->bytes_per_line,
1979 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
1980 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
1981 convs->Convert_888_to_555_reverse
1983 srcbits,-bmpImage->bytes_per_line,
1986 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
1987 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
1988 convs->Convert_888_to_565_reverse
1990 srcbits,-bmpImage->bytes_per_line,
2000 const char* srcbits;
2002 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2004 if (bmpImage->green_mask!=0x00ff00 ||
2005 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2007 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2008 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2010 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2011 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2012 convs->Convert_0888_to_555_asis
2014 srcbits,-bmpImage->bytes_per_line,
2017 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2018 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2019 convs->Convert_0888_to_565_asis
2021 srcbits,-bmpImage->bytes_per_line,
2026 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2027 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2028 convs->Convert_0888_to_555_reverse
2030 srcbits,-bmpImage->bytes_per_line,
2033 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2034 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2035 convs->Convert_0888_to_565_reverse
2037 srcbits,-bmpImage->bytes_per_line,
2046 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2048 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2049 int rShift,gShift,bShift;
2052 /* Shift everything 16 bits left so that all shifts are >0,
2053 * even for BGR DIBs. Then a single >> 16 will bring everything
2056 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2057 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2058 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2060 /* 6 bits for the green */
2066 for (h = lines - 1; h >= 0; h--) {
2067 dstpixel=(LPWORD)dstbits;
2068 for (x = 0; x < width; x++) {
2069 PALETTEENTRY srcval;
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;
2077 dstbits += linebytes;
2085 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2087 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2088 int rShift,gShift,bShift;
2089 const BYTE* srcbits;
2090 const BYTE* srcpixel;
2093 /* Shift everything 16 bits left so that all shifts are >0,
2094 * even for BGR DIBs. Then a single >> 16 will bring everything
2097 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2098 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2099 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2101 /* 6 bits for the green */
2107 srcbits=(BYTE*)bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2108 for (h=0; h<lines; h++) {
2110 dstpixel=(LPWORD)dstbits;
2111 for (x = 0; x < width; x++) {
2112 PALETTEENTRY srcval;
2114 srcval=srccolors[*srcpixel++];
2115 dstval=((srcval.peRed << rShift) & rDst) |
2116 ((srcval.peGreen << gShift) & gDst) |
2117 ((srcval.peBlue << bShift) & bDst);
2118 *dstpixel++=dstval >> 16;
2120 srcbits -= bmpImage->bytes_per_line;
2121 dstbits += linebytes;
2131 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2132 int rShift,gShift,bShift;
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,
2140 /* Shift everything 16 bits left so that all shifts are >0,
2141 * even for BGR DIBs. Then a single >> 16 will bring everything
2144 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2145 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2146 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2148 /* 6 bits for the green */
2154 for (h = lines - 1; h >= 0; h--) {
2155 dstpixel=(LPWORD)dstbits;
2156 for (x = 0; x < width; x++) {
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;
2165 dstbits += linebytes;
2173 /***********************************************************************
2174 * X11DRV_DIB_SetImageBits_24
2176 * SetDIBits for a 24-bit deep DIB.
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 )
2185 int h, width = min(srcwidth, dstwidth);
2186 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2191 srcbits = srcbits + linebytes * (lines - 1);
2192 linebytes = -linebytes;
2195 switch (bmpImage->depth)
2198 if (bmpImage->bits_per_pixel==24) {
2201 srcbits=srcbits+left*3;
2202 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2204 if (bmpImage->green_mask!=0x00ff00 ||
2205 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
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
2213 dstbits,-bmpImage->bytes_per_line);
2215 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2216 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2217 convs->Convert_888_reverse
2220 dstbits,-bmpImage->bytes_per_line);
2230 srcbits=srcbits+left*3;
2231 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2233 if (bmpImage->green_mask!=0x00ff00 ||
2234 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
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
2242 dstbits,-bmpImage->bytes_per_line);
2244 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2245 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2246 convs->Convert_888_to_0888_reverse
2249 dstbits,-bmpImage->bytes_per_line);
2259 srcbits=srcbits+left*3;
2260 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
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
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
2278 dstbits,-bmpImage->bytes_per_line);
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
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
2298 dstbits,-bmpImage->bytes_per_line);
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 );
2318 /* ==== rgb 888 dib -> any bmp format ==== */
2319 const BYTE* srcbyte;
2321 /* Windows only supports one 24bpp DIB format: RGB888 */
2323 for (h = lines - 1; h >= 0; h--) {
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])));
2331 srcbits += linebytes;
2339 /***********************************************************************
2340 * X11DRV_DIB_GetImageBits_24
2342 * GetDIBits for an 24-bit deep DIB.
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 )
2351 int h, width = min(srcwidth, dstwidth);
2352 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2357 dstbits = dstbits + ( linebytes * (lines-1) );
2358 linebytes = -linebytes;
2361 switch (bmpImage->depth)
2364 if (bmpImage->bits_per_pixel==24) {
2365 const char* srcbits;
2367 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2369 if (bmpImage->green_mask!=0x00ff00 ||
2370 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
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
2377 srcbits,-bmpImage->bytes_per_line,
2380 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2381 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2382 convs->Convert_888_reverse
2384 srcbits,-bmpImage->bytes_per_line,
2393 const char* srcbits;
2395 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2397 if (bmpImage->green_mask!=0x00ff00 ||
2398 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
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
2405 srcbits,-bmpImage->bytes_per_line,
2408 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2409 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2410 convs->Convert_0888_to_888_reverse
2412 srcbits,-bmpImage->bytes_per_line,
2421 const char* srcbits;
2423 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
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
2432 srcbits,-bmpImage->bytes_per_line,
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
2440 srcbits,-bmpImage->bytes_per_line,
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
2452 srcbits,-bmpImage->bytes_per_line,
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
2460 srcbits,-bmpImage->bytes_per_line,
2473 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2475 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2478 /* Windows only supports one 24bpp DIB format: rgb 888 */
2479 for (h = lines - 1; h >= 0; h--) {
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;
2489 dstbits += linebytes;
2497 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2499 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2500 const void* srcbits;
2501 const BYTE* srcpixel;
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--) {
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;
2517 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
2518 dstbits += linebytes;
2528 /* ==== any bmp format -> 888 dib ==== */
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,
2536 /* Windows only supports one 24bpp DIB format: rgb 888 */
2537 for (h = lines - 1; h >= 0; h--) {
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);
2547 dstbits += linebytes;
2555 /***********************************************************************
2556 * X11DRV_DIB_SetImageBits_32
2558 * SetDIBits for a 32-bit deep DIB.
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,
2568 int h, width = min(srcwidth, dstwidth);
2569 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2574 srcbits = srcbits + ( linebytes * (lines-1) );
2575 linebytes = -linebytes;
2578 switch (bmpImage->depth)
2581 if (bmpImage->bits_per_pixel==24) {
2584 srcbits=srcbits+left*4;
2585 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
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
2593 dstbits,-bmpImage->bytes_per_line);
2594 } else if (bmpImage->green_mask!=0x00ff00 ||
2595 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
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
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
2611 dstbits,-bmpImage->bytes_per_line);
2613 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2614 convs->Convert_any0888_to_bgr888
2618 dstbits,-bmpImage->bytes_per_line);
2628 srcbits=srcbits+left*4;
2629 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
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
2638 dstbits,-bmpImage->bytes_per_line);
2639 } else if (bmpImage->green_mask!=0x00ff00 ||
2640 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
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
2649 dstbits,-bmpImage->bytes_per_line);
2651 /* ==== any 0888 dib -> any 0888 bmp ==== */
2652 convs->Convert_0888_any
2656 dstbits,-bmpImage->bytes_per_line,
2657 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
2659 } else if (bmpImage->green_mask!=0x00ff00 ||
2660 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2662 /* the tests below assume sane bmpImage masks */
2664 /* ==== any 0888 dib -> any 0888 bmp ==== */
2665 convs->Convert_0888_any
2669 dstbits,-bmpImage->bytes_per_line,
2670 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
2680 srcbits=srcbits+left*4;
2681 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
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
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
2696 dstbits,-bmpImage->bytes_per_line);
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
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
2712 dstbits,-bmpImage->bytes_per_line);
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
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
2732 dstbits,-bmpImage->bytes_per_line);
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
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
2748 dstbits,-bmpImage->bytes_per_line);
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
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
2774 dstbits,-bmpImage->bytes_per_line,
2775 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
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 );
2793 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
2794 const DWORD* srcpixel;
2795 int rShift,gShift,bShift;
2797 rShift=X11DRV_DIB_MaskToShift(rSrc);
2798 gShift=X11DRV_DIB_MaskToShift(gSrc);
2799 bShift=X11DRV_DIB_MaskToShift(bSrc);
2801 for (h = lines - 1; h >= 0; h--) {
2802 srcpixel=(const DWORD*)srcbits;
2803 for (x = left; x < width+left; x++) {
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)));
2813 srcbits += linebytes;
2821 /***********************************************************************
2822 * X11DRV_DIB_GetImageBits_32
2824 * GetDIBits for an 32-bit deep DIB.
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 )
2833 int h, width = min(srcwidth, dstwidth);
2834 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2839 dstbits = dstbits + ( linebytes * (lines-1) );
2840 linebytes = -linebytes;
2843 switch (bmpImage->depth)
2846 if (bmpImage->bits_per_pixel==24) {
2847 const void* srcbits;
2849 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
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
2856 srcbits,-bmpImage->bytes_per_line,
2858 } else if (bmpImage->green_mask!=0x00ff00 ||
2859 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
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
2867 srcbits,-bmpImage->bytes_per_line,
2869 } else if (bmpImage->blue_mask==0xff) {
2870 /* ==== rgb 888 bmp -> any 0888 dib ==== */
2871 convs->Convert_rgb888_to_any0888
2873 srcbits,-bmpImage->bytes_per_line,
2877 /* ==== bgr 888 bmp -> any 0888 dib ==== */
2878 convs->Convert_bgr888_to_any0888
2880 srcbits,-bmpImage->bytes_per_line,
2890 const char* srcbits;
2892 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
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
2900 srcbits,-bmpImage->bytes_per_line,
2902 } else if (bmpImage->green_mask!=0x00ff00 ||
2903 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
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
2911 srcbits,-bmpImage->bytes_per_line,
2914 /* ==== any 0888 bmp -> any 0888 dib ==== */
2915 convs->Convert_0888_any
2917 srcbits,-bmpImage->bytes_per_line,
2918 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
2922 } else if (bmpImage->green_mask!=0x00ff00 ||
2923 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2925 /* the tests below assume sane bmpImage masks */
2927 /* ==== any 0888 bmp -> any 0888 dib ==== */
2928 convs->Convert_0888_any
2930 srcbits,-bmpImage->bytes_per_line,
2931 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
2941 const char* srcbits;
2943 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
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
2951 srcbits,-bmpImage->bytes_per_line,
2953 } else if (bmpImage->blue_mask==0x7f00) {
2954 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
2955 convs->Convert_555_to_0888_reverse
2957 srcbits,-bmpImage->bytes_per_line,
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
2967 srcbits,-bmpImage->bytes_per_line,
2969 } else if (bmpImage->blue_mask==0xf800) {
2970 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
2971 convs->Convert_565_to_0888_reverse
2973 srcbits,-bmpImage->bytes_per_line,
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
2987 srcbits,-bmpImage->bytes_per_line,
2989 } else if (bmpImage->red_mask==0x7f00) {
2990 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
2991 convs->Convert_555_to_0888_reverse
2993 srcbits,-bmpImage->bytes_per_line,
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
3003 srcbits,-bmpImage->bytes_per_line,
3005 } else if (bmpImage->red_mask==0xf800) {
3006 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3007 convs->Convert_565_to_0888_reverse
3009 srcbits,-bmpImage->bytes_per_line,
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
3024 srcbits,-bmpImage->bytes_per_line,
3025 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
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
3034 srcbits,-bmpImage->bytes_per_line,
3035 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3047 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3049 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3050 int rShift,gShift,bShift;
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);
3065 dstbits += linebytes;
3073 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3075 /* ==== pal 8 bmp -> any 0888 dib ==== */
3076 int rShift,gShift,bShift;
3077 const void* srcbits;
3078 const BYTE* srcpixel;
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--) {
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);
3095 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
3096 dstbits += linebytes;
3106 /* ==== any bmp format -> any 0888 dib ==== */
3107 int rShift,gShift,bShift;
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,
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++) {
3122 srcval=X11DRV_PALETTE_ToLogical(physDev, XGetPixel(bmpImage, x, h));
3123 *dstpixel++=(GetRValue(srcval) << rShift) |
3124 (GetGValue(srcval) << gShift) |
3125 (GetBValue(srcval) << bShift);
3127 dstbits += linebytes;
3134 /***********************************************************************
3135 * X11DRV_DIB_SetImageBits
3137 * Transfer the bits to an X image.
3138 * Helper function for SetDIBits() and SetDIBitsToDevice().
3140 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3142 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3143 void *old_data = NULL;
3148 bmpImage = descr->image;
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();
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;
3166 wine_tsx11_unlock();
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);
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)
3179 TRACE("Using the shared pixmap data.\n");
3182 XSync( gdi_display, False );
3183 wine_tsx11_unlock();
3185 old_data = descr->image->data;
3186 descr->image->data = descr->physBitmap->shminfo.shmaddr;
3190 /* Transfer the pixels */
3193 switch(descr->infoBpp)
3196 X11DRV_DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
3197 descr->width, descr->xSrc, (int *)(descr->colorMap),
3198 bmpImage, descr->dibpitch );
3201 X11DRV_DIB_SetImageBits_4( descr->lines, descr->bits,
3202 descr->infoWidth, descr->width,
3203 descr->xSrc, (int*)(descr->colorMap),
3204 bmpImage, descr->dibpitch );
3207 X11DRV_DIB_SetImageBits_8( descr->lines, descr->bits,
3208 descr->infoWidth, descr->width,
3209 descr->xSrc, (int *)(descr->colorMap),
3210 bmpImage, descr->dibpitch );
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);
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);
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);
3234 WARN("(%d): Invalid depth\n", descr->infoBpp );
3240 WARN( "invalid bits pointer %p\n", descr->bits );
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);
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)
3258 XSync( gdi_display, False );
3260 else if (descr->shm_mode == X11DRV_SHM_IMAGE && descr->image)
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 );
3270 XPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3271 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3272 descr->width, descr->height );
3276 if (old_data) descr->image->data = old_data;
3278 if (!descr->image) X11DRV_DIB_DestroyXImage( bmpImage );
3279 wine_tsx11_unlock();
3283 /***********************************************************************
3284 * X11DRV_DIB_GetImageBits
3286 * Transfer the bits from an X image.
3288 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3290 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3291 void *old_data = NULL;
3296 bmpImage = descr->image;
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();
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;
3315 #ifdef HAVE_LIBXXSHM
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))
3325 XSync( gdi_display, False );
3326 old_data = bmpImage->data;
3327 bmpImage->data = descr->physBitmap->shminfo.shmaddr;
3328 TRACE("Using shared pixmap data.\n");
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))
3334 int saveRed, saveGreen, saveBlue;
3336 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3337 gdi_display, descr->drawable, bmpImage,
3338 descr->xSrc, descr->ySrc, AllPlanes);
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
3345 saveRed = bmpImage->red_mask;
3346 saveBlue= bmpImage->blue_mask;
3347 saveGreen = bmpImage->green_mask;
3349 XShmGetImage( gdi_display, descr->drawable, bmpImage,
3350 descr->xSrc, descr->ySrc, AllPlanes);
3352 bmpImage->red_mask = saveRed;
3353 bmpImage->blue_mask = saveBlue;
3354 bmpImage->green_mask = saveGreen;
3357 #endif /* HAVE_LIBXXSHM */
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 );
3366 wine_tsx11_unlock();
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)
3377 X11DRV_DIB_GetImageBits_1( descr->lines,(LPVOID)descr->bits,
3378 descr->infoWidth, descr->width,
3379 descr->colorMap, descr->palentry,
3380 bmpImage, descr->dibpitch );
3384 X11DRV_DIB_GetImageBits_4( descr->lines,(LPVOID)descr->bits,
3385 descr->infoWidth, descr->width,
3386 descr->colorMap, descr->palentry,
3387 bmpImage, descr->dibpitch );
3390 X11DRV_DIB_GetImageBits_8( descr->lines, (LPVOID)descr->bits,
3391 descr->infoWidth, descr->width,
3392 descr->colorMap, descr->palentry,
3393 bmpImage, descr->dibpitch );
3396 X11DRV_DIB_GetImageBits_16( descr->physDev, descr->lines, (LPVOID)descr->bits,
3397 descr->infoWidth,descr->width,
3399 descr->rMask, descr->gMask, descr->bMask,
3400 bmpImage, descr->dibpitch );
3404 X11DRV_DIB_GetImageBits_24( descr->physDev, descr->lines, (LPVOID)descr->bits,
3405 descr->infoWidth,descr->width,
3407 descr->rMask, descr->gMask, descr->bMask,
3408 bmpImage, descr->dibpitch);
3412 X11DRV_DIB_GetImageBits_32( descr->physDev, descr->lines, (LPVOID)descr->bits,
3413 descr->infoWidth, descr->width,
3415 descr->rMask, descr->gMask, descr->bMask,
3416 bmpImage, descr->dibpitch);
3420 WARN("(%d): Invalid depth\n", descr->infoBpp );
3424 if (old_data) bmpImage->data = old_data;
3425 if (!descr->image) X11DRV_DIB_DestroyXImage( bmpImage );
3429 /***********************************************************************
3430 * X11DRV_DIB_DoCopyDIBSection
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)
3439 DIBSECTION dibSection;
3440 X11DRV_DIB_IMAGEBITS_DESCR descr;
3441 int identity[2] = {0,1};
3443 if (!GetObjectW( physBitmap->hbitmap, sizeof(dibSection), &dibSection )) return;
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;
3459 if(descr.infoBpp == 1)
3460 descr.colorMap = (void*)identity;
3462 switch (descr.infoBpp)
3467 descr.rMask = descr.gMask = descr.bMask = 0;
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;
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;
3485 descr.drawable = dest;
3489 descr.xDest = xDest;
3490 descr.yDest = yDest;
3491 descr.width = width;
3492 descr.height = height;
3493 descr.sizeImage = 0;
3495 descr.shm_mode = physBitmap->shm_mode;
3496 #ifdef HAVE_LIBXXSHM
3497 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP && physBitmap->pixmap != dest)
3499 descr.shm_mode = X11DRV_SHM_NONE;
3502 descr.dibpitch = dibSection.dsBm.bmWidthBytes;
3506 TRACE("Copying from Pixmap to DIB bits\n");
3507 X11DRV_DIB_GetImageBits( &descr );
3511 TRACE("Copying from DIB bits to Pixmap\n");
3512 X11DRV_DIB_SetImageBits( &descr );
3516 /***********************************************************************
3517 * X11DRV_DIB_DoUpdateDIBSection
3519 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB)
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);
3530 /***********************************************************************
3531 * X11DRV_DIB_FaultHandler
3533 static LONG CALLBACK X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep )
3535 X_PHYSBITMAP *physBitmap = NULL;
3539 const size_t pagemask = getpagesize() - 1;
3541 if (ep->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
3542 return EXCEPTION_CONTINUE_SEARCH;
3544 addr = (BYTE *)ep->ExceptionRecord->ExceptionInformation[1];
3546 EnterCriticalSection(&dibs_cs);
3547 LIST_FOR_EACH( ptr, &dibs_list )
3549 physBitmap = LIST_ENTRY( ptr, X_PHYSBITMAP, entry );
3550 if ((physBitmap->base <= addr) &&
3551 (addr < physBitmap->base + ((physBitmap->size + pagemask) & ~pagemask)))
3557 LeaveCriticalSection(&dibs_cs);
3559 if (!found) return EXCEPTION_CONTINUE_SEARCH;
3561 if (addr >= physBitmap->base + physBitmap->size)
3562 WARN( "%p: access to %p beyond the end of the DIB\n", physBitmap->hbitmap, addr );
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);
3569 /* the app tried to read the DIB bits */
3570 X11DRV_DIB_Coerce( physBitmap, DIB_Status_InSync);
3572 X11DRV_DIB_Unlock( physBitmap, TRUE );
3574 return EXCEPTION_CONTINUE_EXECUTION;
3577 /***********************************************************************
3580 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *physBitmap, INT req)
3582 INT ret = DIB_Status_None;
3584 if (!physBitmap->image) return ret; /* not a DIB section */
3585 EnterCriticalSection(&physBitmap->lock);
3586 ret = physBitmap->status;
3588 case DIB_Status_GdiMod:
3589 /* GDI access - request to draw on pixmap */
3590 switch (physBitmap->status)
3593 case DIB_Status_None:
3594 physBitmap->p_status = DIB_Status_GdiMod;
3595 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
3598 case DIB_Status_GdiMod:
3599 TRACE("GdiMod requested in status GdiMod\n" );
3600 physBitmap->p_status = DIB_Status_GdiMod;
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;
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;
3622 case DIB_Status_InSync:
3623 /* App access - request access to read DIB surface */
3624 /* (typically called from signal handler) */
3625 switch (physBitmap->status)
3628 case DIB_Status_None:
3629 /* shouldn't happen from signal handler */
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;
3640 case DIB_Status_InSync:
3641 TRACE("InSync requested in status InSync\n" );
3642 /* shouldn't happen from signal handler */
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 */
3653 case DIB_Status_AppMod:
3654 /* App access - request access to write DIB surface */
3655 /* (typically called from signal handler) */
3656 switch (physBitmap->status)
3659 case DIB_Status_None:
3660 /* shouldn't happen from signal handler */
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;
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;
3676 case DIB_Status_AppMod:
3677 TRACE("AppMod requested in status AppMod\n" );
3678 /* shouldn't happen from signal handler */
3683 /* it is up to the caller to do the copy/conversion, probably
3684 * using the return value to decide where to copy from */
3686 LeaveCriticalSection(&physBitmap->lock);
3690 /***********************************************************************
3693 INT X11DRV_DIB_Lock(X_PHYSBITMAP *physBitmap, INT req)
3695 INT ret = DIB_Status_None;
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);
3706 /***********************************************************************
3709 void X11DRV_DIB_Unlock(X_PHYSBITMAP *physBitmap, BOOL commit)
3711 if (!physBitmap->image) return; /* not a DIB section */
3712 switch (physBitmap->status)
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 */
3719 switch (physBitmap->p_status)
3721 case DIB_Status_GdiMod:
3722 TRACE("Unlocking and syncing from GdiMod\n" );
3723 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
3727 TRACE("Unlocking without needing to sync\n" );
3731 else TRACE("Unlocking with no changes\n");
3732 physBitmap->p_status = DIB_Status_None;
3735 case DIB_Status_GdiMod:
3736 TRACE("Unlocking in status GdiMod\n" );
3737 /* DIB was protected in Coerce */
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;
3748 case DIB_Status_InSync:
3749 TRACE("Unlocking in status InSync\n" );
3750 /* DIB was already protected in Coerce */
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 */
3760 LeaveCriticalSection(&physBitmap->lock);
3761 TRACE("Unlocked %p\n", physBitmap->hbitmap);
3764 /***********************************************************************
3765 * X11DRV_CoerceDIBSection
3767 INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req)
3769 if (!physDev || !physDev->bitmap) return DIB_Status_None;
3770 return X11DRV_DIB_Coerce(physDev->bitmap, req);
3773 /***********************************************************************
3774 * X11DRV_LockDIBSection
3776 INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req)
3778 if (!physDev || !physDev->bitmap) return DIB_Status_None;
3779 return X11DRV_DIB_Lock(physDev->bitmap, req);
3782 /***********************************************************************
3783 * X11DRV_UnlockDIBSection
3785 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
3787 if (!physDev || !physDev->bitmap) return;
3788 X11DRV_DIB_Unlock(physDev->bitmap, commit);
3792 #ifdef HAVE_LIBXXSHM
3793 /***********************************************************************
3794 * X11DRV_XShmErrorHandler
3797 static int XShmErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
3799 return 1; /* FIXME: should check event contents */
3802 /***********************************************************************
3803 * X11DRV_XShmCreateImage
3806 static XImage *X11DRV_XShmCreateImage( int width, int height, int bpp,
3807 XShmSegmentInfo* shminfo)
3811 image = XShmCreateImage(gdi_display, visual, bpp, ZPixmap, NULL, shminfo, width, height);
3814 shminfo->shmid = shmget(IPC_PRIVATE, image->bytes_per_line * height,
3816 if( shminfo->shmid != -1 )
3818 shminfo->shmaddr = shmat( shminfo->shmid, 0, 0 );
3819 if( shminfo->shmaddr != (char*)-1 )
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;
3830 shmctl(shminfo->shmid, IPC_RMID, 0);
3831 return image; /* Success! */
3833 /* An error occurred */
3834 shmdt(shminfo->shmaddr);
3836 shmctl(shminfo->shmid, IPC_RMID, 0);
3837 shminfo->shmid = -1;
3839 XFlush(gdi_display);
3840 XDestroyImage(image);
3845 #endif /* HAVE_LIBXXSHM */
3847 static Bool X11DRV_DIB_QueryXShm( Bool *pixmaps )
3849 static Bool have_xshm, have_xshm_pixmaps;
3850 static BOOL initialized;
3854 #ifdef HAVE_LIBXXSHM
3857 have_xshm = XShmQueryVersion( gdi_display, &major, &minor, &have_xshm_pixmaps );
3862 *pixmaps = have_xshm_pixmaps;
3866 /***********************************************************************
3867 * X11DRV_CreateDIBSection (X11DRV.@)
3869 HBITMAP X11DRV_CreateDIBSection( PHYSDEV dev, HBITMAP hbitmap, BITMAPINFO *bmi, UINT usage )
3871 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
3872 X_PHYSBITMAP *physBitmap;
3874 #ifdef HAVE_LIBXXSHM
3878 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return 0;
3879 physBitmap->topdown = bmi->bmiHeader.biHeight < 0;
3880 physBitmap->status = DIB_Status_None;
3882 GetObjectW( hbitmap, sizeof(dib), &dib );
3884 /* create color map */
3885 if (dib.dsBm.bmBitsPixel <= 8)
3887 physBitmap->colorMap = X11DRV_DIB_BuildColorMap( physDev,
3888 usage, dib.dsBm.bmBitsPixel, bmi,
3889 &physBitmap->nColorMap );
3892 if (!X11DRV_XRender_SetPhysBitmapDepth( physBitmap, dib.dsBm.bmBitsPixel, &dib ))
3894 if (dib.dsBm.bmBitsPixel == 1)
3896 physBitmap->depth = 1;
3897 physBitmap->trueColor = FALSE;
3901 physBitmap->depth = screen_depth;
3902 physBitmap->color_shifts = X11DRV_PALETTE_default_shifts;
3903 physBitmap->trueColor = (visual->class == TrueColor || visual->class == DirectColor);
3907 /* create pixmap and X image */
3909 #ifdef HAVE_LIBXXSHM
3910 physBitmap->shminfo.shmid = -1;
3912 if (X11DRV_DIB_QueryXShm( &pixmaps )
3913 && (physBitmap->image = X11DRV_XShmCreateImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
3914 physBitmap->depth, &physBitmap->shminfo )))
3918 physBitmap->shm_mode = X11DRV_SHM_PIXMAP;
3919 physBitmap->image->data = HeapAlloc( GetProcessHeap(), 0,
3920 dib.dsBm.bmHeight * physBitmap->image->bytes_per_line );
3924 physBitmap->shm_mode = X11DRV_SHM_IMAGE;
3925 physBitmap->image->data = physBitmap->shminfo.shmaddr;
3931 physBitmap->shm_mode = X11DRV_SHM_NONE;
3932 physBitmap->image = X11DRV_DIB_CreateXImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
3933 physBitmap->depth );
3936 #ifdef HAVE_LIBXXSHM
3937 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP)
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 );
3948 physBitmap->pixmap = XCreatePixmap( gdi_display, root_window, dib.dsBm.bmWidth,
3949 dib.dsBm.bmHeight, physBitmap->depth );
3952 wine_tsx11_unlock();
3953 if (!physBitmap->pixmap || !physBitmap->image) return 0;
3955 if (physBitmap->trueColor)
3957 ColorShifts *shifts = &physBitmap->color_shifts;
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;
3968 /* install fault handler */
3969 InitializeCriticalSection( &physBitmap->lock );
3970 physBitmap->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": X_PHYSBITMAP.lock");
3972 physBitmap->base = dib.dsBm.bmBits;
3973 physBitmap->size = dib.dsBmih.biSizeImage;
3974 physBitmap->status = DIB_Status_AppMod;
3977 dibs_handler = AddVectoredExceptionHandler( TRUE, X11DRV_DIB_FaultHandler );
3978 EnterCriticalSection( &dibs_cs );
3979 list_add_head( &dibs_list, &physBitmap->entry );
3980 LeaveCriticalSection( &dibs_cs );
3982 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
3987 /***********************************************************************
3988 * X11DRV_DIB_DeleteDIBSection
3990 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP *physBitmap, DIBSECTION *dib)
3994 EnterCriticalSection( &dibs_cs );
3995 list_remove( &physBitmap->entry );
3996 last = list_empty( &dibs_list );
3997 LeaveCriticalSection( &dibs_cs );
4001 RemoveVectoredExceptionHandler( dibs_handler );
4002 dibs_handler = NULL;
4005 if (dib->dshSection)
4006 X11DRV_DIB_Coerce(physBitmap, DIB_Status_InSync);
4008 if (physBitmap->image)
4011 #ifdef HAVE_LIBXXSHM
4012 if (physBitmap->shminfo.shmid != -1)
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;
4023 X11DRV_DIB_DestroyXImage( physBitmap->image );
4024 wine_tsx11_unlock();
4027 HeapFree(GetProcessHeap(), 0, physBitmap->colorMap);
4028 physBitmap->lock.DebugInfo->Spare[0] = 0;
4029 DeleteCriticalSection(&physBitmap->lock);
4032 /***********************************************************************
4033 * SetDIBColorTable (X11DRV.@)
4035 UINT X11DRV_SetDIBColorTable( PHYSDEV dev, UINT start, UINT count, const RGBQUAD *colors )
4037 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
4040 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4042 if (!physBitmap) return 0;
4043 GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib );
4045 if (physBitmap->colorMap && start < physBitmap->nColorMap) {
4046 UINT end = count + start;
4047 if (end > physBitmap->nColorMap) end = physBitmap->nColorMap;
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.
4054 * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4055 * at least for a 1 bpp dibsection
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 );
4067 /***********************************************************************
4068 * X11DRV_DIB_CreateDIBFromBitmap
4070 * Allocates a packed DIB and copies the bitmap data into it.
4072 HGLOBAL X11DRV_DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
4077 LPBITMAPINFOHEADER pbmiHeader;
4078 unsigned int cDataSize, cPackedSize, OffsetBits;
4081 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
4084 * A packed DIB contains a BITMAPINFO structure followed immediately by
4085 * an optional color palette and the pixel data.
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 )
4093 /* Get the offset to the bits */
4094 OffsetBits = cPackedSize - cDataSize;
4096 /* Allocate the packed DIB */
4097 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
4098 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
4102 WARN("Could not allocate packed DIB!\n");
4106 /* A packed DIB starts with a BITMAPINFOHEADER */
4107 pPackedDIB = GlobalLock(hPackedDIB);
4108 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
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;
4122 /* Retrieve the DIB bits from the bitmap and fill in the
4123 * DIB color table if present */
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);
4134 /* Cleanup if GetDIBits failed */
4135 if (nLinesCopied != bmp.bmHeight)
4137 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
4138 GlobalFree(hPackedDIB);
4145 /**************************************************************************
4146 * X11DRV_DIB_CreateDIBFromPixmap
4148 * Allocates a packed DIB and copies the Pixmap data into it.
4149 * The Pixmap passed in is deleted after the conversion.
4151 HGLOBAL X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap, HDC hdc)
4154 X_PHYSBITMAP *physBitmap;
4157 HGLOBAL hPackedDIB = 0;
4159 int x,y; /* Unused */
4160 unsigned border_width; /* Unused */
4161 unsigned int depth, width, height;
4163 /* Get the Pixmap dimensions and bit depth */
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;
4170 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
4171 width, height, depth);
4174 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
4175 * and make it a container for the pixmap passed.
4177 if (!(hBmp = CreateBitmap( width, height, 1, pixmap_formats[depth]->bits_per_pixel, NULL ))) return 0;
4179 /* force bitmap to be owned by a screen DC */
4180 hdcMem = CreateCompatibleDC( hdc );
4181 SelectObject( hdcMem, SelectObject( hdcMem, hBmp ));
4184 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4186 /* swap the new pixmap in */
4187 orig_pixmap = physBitmap->pixmap;
4188 physBitmap->pixmap = pixmap;
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.
4195 hPackedDIB = X11DRV_DIB_CreateDIBFromBitmap(hdc, hBmp);
4197 /* we can now get rid of the HBITMAP and its original pixmap */
4198 physBitmap->pixmap = orig_pixmap;
4201 TRACE("\tReturning packed DIB %p\n", hPackedDIB);
4206 /**************************************************************************
4207 * X11DRV_DIB_CreatePixmapFromDIB
4209 * Creates a Pixmap from a packed DIB
4211 Pixmap X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB, HDC hdc )
4214 X_PHYSBITMAP *physBitmap;
4219 /* Create a DDB from the DIB */
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);
4227 /* make sure it's owned by x11drv */
4228 memdc = CreateCompatibleDC( hdc );
4229 SelectObject( memdc, hBmp );
4232 /* clear the physBitmap so that we can steal its pixmap */
4233 if ((physBitmap = X11DRV_get_phys_bitmap( hBmp )))
4235 pixmap = physBitmap->pixmap;
4236 physBitmap->pixmap = 0;
4239 /* Delete the DDB we created earlier now that we have stolen its pixmap */
4242 TRACE("Returning Pixmap %lx\n", pixmap);