winex11: Sync a dib-section before accessing the image bits.
[wine/multimedia.git] / dlls / winex11.drv / dib.c
bloba1176d95342738d5578730bd51ec7981ddf5bd19
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <X11/Xlib.h>
25 #ifdef HAVE_LIBXXSHM
26 #include <X11/extensions/XShm.h>
27 # ifdef HAVE_SYS_SHM_H
28 # include <sys/shm.h>
29 # endif
30 # ifdef HAVE_SYS_IPC_H
31 # include <sys/ipc.h>
32 # endif
33 #endif /* defined(HAVE_LIBXXSHM) */
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wingdi.h"
41 #include "x11drv.h"
42 #include "wine/exception.h"
43 #include "wine/debug.h"
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 =
52 0, 0, &dibs_cs,
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() */
63 typedef struct
65 X11DRV_PDEVICE *physDev;
66 LPCVOID bits;
67 XImage *image;
68 PALETTEENTRY *palentry;
69 int lines;
70 DWORD infoWidth;
71 WORD depth;
72 WORD infoBpp;
73 WORD compression;
74 ColorShifts *shifts;
75 RGBQUAD *colorMap;
76 int nColorMap;
77 Drawable drawable;
78 GC gc;
79 int xSrc;
80 int ySrc;
81 int xDest;
82 int yDest;
83 int width;
84 int height;
85 DWORD rMask;
86 DWORD gMask;
87 DWORD bMask;
88 enum x11drv_shm_mode shm_mode;
89 int dibpitch;
90 DWORD sizeImage;
91 X_PHYSBITMAP *physBitmap;
92 } X11DRV_DIB_IMAGEBITS_DESCR;
95 enum Rle_EscapeCodes
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
107 dlls/gdi/dib.c
110 /***********************************************************************
111 * DIB_DoProtectDIBSection
113 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP *physBitmap, DWORD new_prot )
115 DWORD old_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 );
134 if (testimage)
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));
144 error:
145 WARN( "(%d): Unsupported depth\n", depth );
146 return 4 * width;
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 int words;
159 switch(depth)
161 case 1: words = (width + 31) / 32; break;
162 case 4: words = (width + 7) / 8; break;
163 case 8: words = (width + 3) / 4; break;
164 case 15:
165 case 16: words = (width + 1) / 2; break;
166 case 24: words = (width * 3 + 3) / 4; break;
167 default:
168 WARN("(%d): Unsupported depth\n", depth );
169 /* fall through */
170 case 32:
171 words = width;
173 return 4 * words;
177 /***********************************************************************
178 * X11DRV_DIB_GetDIBImageBytes
180 * Return the number of bytes used to hold the image in a DIB bitmap.
182 static int X11DRV_DIB_GetDIBImageBytes( int width, int height, int depth )
184 return X11DRV_DIB_GetDIBWidthBytes( width, depth ) * abs( height );
188 /***********************************************************************
189 * bitmap_info_size
191 * Return the size of the bitmap info structure including color table.
193 int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
195 unsigned int colors, size, masks = 0;
197 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
199 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
200 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
201 return sizeof(BITMAPCOREHEADER) + colors *
202 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
204 else /* assume BITMAPINFOHEADER */
206 colors = info->bmiHeader.biClrUsed;
207 if (!colors && (info->bmiHeader.biBitCount <= 8))
208 colors = 1 << info->bmiHeader.biBitCount;
209 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
210 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
211 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
216 /***********************************************************************
217 * X11DRV_DIB_CreateXImage
219 * Create an X image.
221 XImage *X11DRV_DIB_CreateXImage( int width, int height, int depth )
223 int width_bytes;
224 XImage *image = NULL;
225 void *data;
227 wine_tsx11_lock();
228 width_bytes = X11DRV_DIB_GetXImageWidthBytes( width, depth );
229 data = HeapAlloc( GetProcessHeap(), 0, height * width_bytes );
230 if (data) image = XCreateImage( gdi_display, visual, depth, ZPixmap, 0,
231 data, width, height, 32, width_bytes );
232 if (!image) HeapFree( GetProcessHeap(), 0, data );
233 wine_tsx11_unlock();
234 return image;
238 /***********************************************************************
239 * X11DRV_DIB_DestroyXImage
241 * Destroy an X image created with X11DRV_DIB_CreateXImage.
243 void X11DRV_DIB_DestroyXImage( XImage *image )
245 HeapFree( GetProcessHeap(), 0, image->data );
246 image->data = NULL;
247 wine_tsx11_lock();
248 XDestroyImage( image );
249 wine_tsx11_unlock();
253 /***********************************************************************
254 * DIB_GetBitmapInfoEx
256 * Get the info from a bitmap header.
257 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
259 static int DIB_GetBitmapInfoEx( const BITMAPINFOHEADER *header, LONG *width,
260 LONG *height, WORD *planes, WORD *bpp,
261 WORD *compr, DWORD *size )
263 if (header->biSize == sizeof(BITMAPCOREHEADER))
265 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
266 *width = core->bcWidth;
267 *height = core->bcHeight;
268 *planes = core->bcPlanes;
269 *bpp = core->bcBitCount;
270 *compr = 0;
271 *size = 0;
272 return 0;
274 if (header->biSize >= sizeof(BITMAPINFOHEADER))
276 *width = header->biWidth;
277 *height = header->biHeight;
278 *planes = header->biPlanes;
279 *bpp = header->biBitCount;
280 *compr = header->biCompression;
281 *size = header->biSizeImage;
282 return 1;
284 ERR("(%d): unknown/wrong size for header\n", header->biSize );
285 return -1;
289 /***********************************************************************
290 * X11DRV_DIB_GetColorCount
292 * Computes the number of colors for the bitmap palette.
293 * Should not be called for a >8-bit deep bitmap.
295 static unsigned int X11DRV_DIB_GetColorCount(const BITMAPINFO *info)
297 unsigned int colors;
298 BOOL core_info = info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER);
300 if (core_info)
302 colors = 1 << ((const BITMAPCOREINFO*)info)->bmciHeader.bcBitCount;
304 else
306 colors = info->bmiHeader.biClrUsed;
307 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
309 if (colors > 256)
311 ERR("called with >256 colors!\n");
312 colors = 0;
314 return colors;
317 /***********************************************************************
318 * DIB_GetBitmapInfo
320 * Get the info from a bitmap header.
321 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
323 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
324 LONG *height, WORD *bpp, WORD *compr )
326 WORD planes;
327 DWORD size;
329 return DIB_GetBitmapInfoEx( header, width, height, &planes, bpp, compr, &size);
333 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
335 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
336 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
339 /***********************************************************************
340 * X11DRV_DIB_GenColorMap
342 * Fills the color map of a bitmap palette. Should not be called
343 * for a >8-bit deep bitmap.
345 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
346 WORD coloruse, WORD depth, BOOL quads,
347 const void *colorPtr, int start, int end )
349 int i;
351 if (coloruse == DIB_RGB_COLORS)
353 if (quads)
355 const RGBQUAD * rgb = colorPtr;
357 if (depth == 1) /* Monochrome */
359 BOOL invert = FALSE;
360 RGBQUAD table[2];
362 if (GetDIBColorTable( physDev->dev.hdc, 0, 2, table ) == 2)
363 invert = !colour_is_brighter(table[1], table[0]);
365 for (i = start; i < end; i++, rgb++)
366 colorMapping[i] = ((rgb->rgbRed + rgb->rgbGreen +
367 rgb->rgbBlue > 255*3/2 && !invert) ||
368 (rgb->rgbRed + rgb->rgbGreen +
369 rgb->rgbBlue <= 255*3/2 && invert));
371 else
372 for (i = start; i < end; i++, rgb++)
373 colorMapping[i] = X11DRV_PALETTE_LookupPixel(physDev->color_shifts, RGB(rgb->rgbRed,
374 rgb->rgbGreen,
375 rgb->rgbBlue));
377 else
379 const RGBTRIPLE * rgb = colorPtr;
381 if (depth == 1) /* Monochrome */
383 BOOL invert = FALSE;
384 RGBQUAD table[2];
386 if (GetDIBColorTable( physDev->dev.hdc, 0, 2, table ) == 2)
387 invert = !colour_is_brighter(table[1], table[0]);
389 for (i = start; i < end; i++, rgb++)
390 colorMapping[i] = ((rgb->rgbtRed + rgb->rgbtGreen +
391 rgb->rgbtBlue > 255*3/2 && !invert) ||
392 (rgb->rgbtRed + rgb->rgbtGreen +
393 rgb->rgbtBlue <= 255*3/2 && invert));
395 else
396 for (i = start; i < end; i++, rgb++)
397 colorMapping[i] = X11DRV_PALETTE_LookupPixel(physDev->color_shifts, RGB(rgb->rgbtRed,
398 rgb->rgbtGreen,
399 rgb->rgbtBlue));
402 else /* DIB_PAL_COLORS */
404 const WORD * index = colorPtr;
406 for (i = start; i < end; i++, index++)
407 colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(*index) );
410 return colorMapping;
413 /***********************************************************************
414 * X11DRV_DIB_BuildColorMap
416 * Build the color map from the bitmap palette. Should not be called
417 * for a >8-bit deep bitmap.
419 static int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE *physDev, WORD coloruse, WORD depth,
420 const BITMAPINFO *info, int *nColors )
422 BOOL isInfo;
423 const void *colorPtr;
424 int *colorMapping;
427 *nColors = X11DRV_DIB_GetColorCount(info);
428 if (!*nColors) return NULL;
430 isInfo = info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER);
431 colorPtr = (const BYTE*)info + (WORD)info->bmiHeader.biSize;
432 if (!(colorMapping = HeapAlloc(GetProcessHeap(), 0, *nColors * sizeof(int) )))
433 return NULL;
435 return X11DRV_DIB_GenColorMap( physDev, colorMapping, coloruse, depth,
436 isInfo, colorPtr, 0, *nColors);
439 /***********************************************************************
440 * X11DRV_DIB_MapColor
442 static int X11DRV_DIB_MapColor( int *physMap, int nPhysMap, int phys, int oldcol )
444 int color;
446 if ((oldcol < nPhysMap) && (physMap[oldcol] == phys))
447 return oldcol;
449 for (color = 0; color < nPhysMap; color++)
450 if (physMap[color] == phys)
451 return color;
453 WARN("Strange color %08x\n", phys);
454 return 0;
458 /*********************************************************************
459 * X11DRV_DIB_GetNearestIndex
461 * Helper for X11DRV_DIB_GetDIBits.
462 * Returns the nearest colour table index for a given RGB.
463 * Nearest is defined by minimizing the sum of the squares.
465 static INT X11DRV_DIB_GetNearestIndex(RGBQUAD *colormap, int numColors, BYTE r, BYTE g, BYTE b)
467 INT i, best = -1, diff, bestdiff = -1;
468 RGBQUAD *color;
470 for(color = colormap, i = 0; i < numColors; color++, i++) {
471 diff = (r - color->rgbRed) * (r - color->rgbRed) +
472 (g - color->rgbGreen) * (g - color->rgbGreen) +
473 (b - color->rgbBlue) * (b - color->rgbBlue);
474 if(diff == 0)
475 return i;
476 if(best == -1 || diff < bestdiff) {
477 best = i;
478 bestdiff = diff;
481 return best;
483 /*********************************************************************
484 * X11DRV_DIB_MaskToShift
486 * Helper for X11DRV_DIB_GetDIBits.
487 * Returns the by how many bits to shift a given color so that it is
488 * in the proper position.
490 INT X11DRV_DIB_MaskToShift(DWORD mask)
492 int shift;
494 if (mask==0)
495 return 0;
497 shift=0;
498 while ((mask&1)==0) {
499 mask>>=1;
500 shift++;
502 return shift;
505 /***********************************************************************
506 * X11DRV_DIB_CheckMask
508 * Check RGB mask if it is either 0 or matches visual's mask.
510 static inline int X11DRV_DIB_CheckMask(int red_mask, int green_mask, int blue_mask)
512 return ( red_mask == 0 && green_mask == 0 && blue_mask == 0 ) ||
513 ( red_mask == visual->red_mask && green_mask == visual->green_mask &&
514 blue_mask == visual->blue_mask );
517 /***********************************************************************
518 * X11DRV_DIB_SetImageBits_1
520 * SetDIBits for a 1-bit deep DIB.
522 static void X11DRV_DIB_SetImageBits_1( int lines, const BYTE *srcbits,
523 DWORD srcwidth, DWORD dstwidth, int left,
524 int *colors, XImage *bmpImage, int linebytes)
526 int h, width;
527 const BYTE* srcbyte;
528 BYTE srcval, extra;
529 DWORD i, x;
531 if (lines < 0 ) {
532 lines = -lines;
533 srcbits = srcbits + linebytes * (lines - 1);
534 linebytes = -linebytes;
537 if ((extra = (left & 7)) != 0) {
538 left &= ~7;
539 dstwidth += extra;
541 srcbits += left >> 3;
542 width = min(srcwidth, dstwidth);
544 /* ==== pal 1 dib -> any bmp format ==== */
545 for (h = lines-1; h >=0; h--) {
546 srcbyte=srcbits;
547 for (i = width/8, x = left; i > 0; i--) {
548 srcval=*srcbyte++;
549 XPutPixel( bmpImage, x++, h, colors[ srcval >> 7] );
550 XPutPixel( bmpImage, x++, h, colors[(srcval >> 6) & 1] );
551 XPutPixel( bmpImage, x++, h, colors[(srcval >> 5) & 1] );
552 XPutPixel( bmpImage, x++, h, colors[(srcval >> 4) & 1] );
553 XPutPixel( bmpImage, x++, h, colors[(srcval >> 3) & 1] );
554 XPutPixel( bmpImage, x++, h, colors[(srcval >> 2) & 1] );
555 XPutPixel( bmpImage, x++, h, colors[(srcval >> 1) & 1] );
556 XPutPixel( bmpImage, x++, h, colors[ srcval & 1] );
558 if (width % 8){
559 srcval=*srcbyte;
560 switch (width & 7)
562 case 7: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
563 case 6: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
564 case 5: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
565 case 4: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
566 case 3: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
567 case 2: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
568 case 1: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]);
571 srcbits += linebytes;
575 /***********************************************************************
576 * X11DRV_DIB_GetImageBits_1
578 * GetDIBits for a 1-bit deep DIB.
580 static void X11DRV_DIB_GetImageBits_1( int lines, BYTE *dstbits,
581 DWORD dstwidth, DWORD srcwidth,
582 RGBQUAD *colors, PALETTEENTRY *srccolors,
583 XImage *bmpImage, int linebytes )
585 DWORD x;
586 int h, width = min(dstwidth, srcwidth);
588 if (lines < 0 ) {
589 lines = -lines;
590 dstbits = dstbits + linebytes * (lines - 1);
591 linebytes = -linebytes;
594 switch (bmpImage->depth)
596 case 1:
597 case 4:
598 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
599 && srccolors) {
600 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
601 BYTE* dstbyte;
603 for (h=lines-1; h>=0; h--) {
604 BYTE dstval;
605 dstbyte=dstbits;
606 dstval=0;
607 for (x=0; x<width; x++) {
608 PALETTEENTRY srcval;
609 srcval=srccolors[XGetPixel(bmpImage, x, h)];
610 dstval|=(X11DRV_DIB_GetNearestIndex
611 (colors, 2,
612 srcval.peRed,
613 srcval.peGreen,
614 srcval.peBlue) << (7 - (x & 7)));
615 if ((x&7)==7) {
616 *dstbyte++=dstval;
617 dstval=0;
620 if ((width&7)!=0) {
621 *dstbyte++=dstval;
623 /* pad with 0 to DWORD alignment */
624 for (x = (x+7)&~7; x < ((width + 31) & ~31); x+=8)
625 *dstbyte++ = 0;
626 dstbits += linebytes;
628 } else {
629 goto notsupported;
631 break;
633 case 8:
634 if (X11DRV_DIB_CheckMask(bmpImage->red_mask, bmpImage->green_mask, bmpImage->blue_mask)
635 && srccolors) {
636 /* ==== pal 8 bmp -> pal 1 dib ==== */
637 const void* srcbits;
638 const BYTE* srcpixel;
639 BYTE* dstbyte;
641 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
643 for (h=0; h<lines; h++) {
644 BYTE dstval;
645 srcpixel=srcbits;
646 dstbyte=dstbits;
647 dstval=0;
648 for (x=0; x<width; x++) {
649 PALETTEENTRY srcval;
650 srcval=srccolors[*srcpixel++];
651 dstval|=(X11DRV_DIB_GetNearestIndex
652 (colors, 2,
653 srcval.peRed,
654 srcval.peGreen,
655 srcval.peBlue) << (7-(x&7)) );
656 if ((x&7)==7) {
657 *dstbyte++=dstval;
658 dstval=0;
661 if ((width&7)!=0) {
662 *dstbyte=dstval;
664 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
665 dstbits += linebytes;
667 } else {
668 goto notsupported;
670 break;
672 case 15:
673 case 16:
675 const void* srcbits;
676 const WORD* srcpixel;
677 BYTE* dstbyte;
679 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
681 if (bmpImage->green_mask==0x03e0) {
682 if (bmpImage->red_mask==0x7c00) {
683 /* ==== rgb 555 bmp -> pal 1 dib ==== */
684 for (h=0; h<lines; h++) {
685 BYTE dstval;
686 srcpixel=srcbits;
687 dstbyte=dstbits;
688 dstval=0;
689 for (x=0; x<width; x++) {
690 WORD srcval;
691 srcval=*srcpixel++;
692 dstval|=(X11DRV_DIB_GetNearestIndex
693 (colors, 2,
694 ((srcval >> 7) & 0xf8) | /* r */
695 ((srcval >> 12) & 0x07),
696 ((srcval >> 2) & 0xf8) | /* g */
697 ((srcval >> 7) & 0x07),
698 ((srcval << 3) & 0xf8) | /* b */
699 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
700 if ((x&7)==7) {
701 *dstbyte++=dstval;
702 dstval=0;
705 if ((width&7)!=0) {
706 *dstbyte=dstval;
708 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
709 dstbits += linebytes;
711 } else if (bmpImage->blue_mask==0x7c00) {
712 /* ==== bgr 555 bmp -> pal 1 dib ==== */
713 for (h=0; h<lines; h++) {
714 WORD dstval;
715 srcpixel=srcbits;
716 dstbyte=dstbits;
717 dstval=0;
718 for (x=0; x<width; x++) {
719 BYTE srcval;
720 srcval=*srcpixel++;
721 dstval|=(X11DRV_DIB_GetNearestIndex
722 (colors, 2,
723 ((srcval << 3) & 0xf8) | /* r */
724 ((srcval >> 2) & 0x07),
725 ((srcval >> 2) & 0xf8) | /* g */
726 ((srcval >> 7) & 0x07),
727 ((srcval >> 7) & 0xf8) | /* b */
728 ((srcval >> 12) & 0x07) ) << (7-(x&7)) );
729 if ((x&7)==7) {
730 *dstbyte++=dstval;
731 dstval=0;
734 if ((width&7)!=0) {
735 *dstbyte=dstval;
737 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
738 dstbits += linebytes;
740 } else {
741 goto notsupported;
743 } else if (bmpImage->green_mask==0x07e0) {
744 if (bmpImage->red_mask==0xf800) {
745 /* ==== rgb 565 bmp -> pal 1 dib ==== */
746 for (h=0; h<lines; h++) {
747 BYTE dstval;
748 srcpixel=srcbits;
749 dstbyte=dstbits;
750 dstval=0;
751 for (x=0; x<width; x++) {
752 WORD srcval;
753 srcval=*srcpixel++;
754 dstval|=(X11DRV_DIB_GetNearestIndex
755 (colors, 2,
756 ((srcval >> 8) & 0xf8) | /* r */
757 ((srcval >> 13) & 0x07),
758 ((srcval >> 3) & 0xfc) | /* g */
759 ((srcval >> 9) & 0x03),
760 ((srcval << 3) & 0xf8) | /* b */
761 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
762 if ((x&7)==7) {
763 *dstbyte++=dstval;
764 dstval=0;
767 if ((width&7)!=0) {
768 *dstbyte=dstval;
770 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
771 dstbits += linebytes;
773 } else if (bmpImage->blue_mask==0xf800) {
774 /* ==== bgr 565 bmp -> pal 1 dib ==== */
775 for (h=0; h<lines; h++) {
776 BYTE dstval;
777 srcpixel=srcbits;
778 dstbyte=dstbits;
779 dstval=0;
780 for (x=0; x<width; x++) {
781 WORD srcval;
782 srcval=*srcpixel++;
783 dstval|=(X11DRV_DIB_GetNearestIndex
784 (colors, 2,
785 ((srcval << 3) & 0xf8) | /* r */
786 ((srcval >> 2) & 0x07),
787 ((srcval >> 3) & 0xfc) | /* g */
788 ((srcval >> 9) & 0x03),
789 ((srcval >> 8) & 0xf8) | /* b */
790 ((srcval >> 13) & 0x07) ) << (7-(x&7)) );
791 if ((x&7)==7) {
792 *dstbyte++=dstval;
793 dstval=0;
796 if ((width&7)!=0) {
797 *dstbyte=dstval;
799 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
800 dstbits += linebytes;
802 } else {
803 goto notsupported;
805 } else {
806 goto notsupported;
809 break;
811 case 24:
812 case 32:
814 const void* srcbits;
815 const BYTE *srcbyte;
816 BYTE* dstbyte;
817 int bytes_per_pixel;
819 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
820 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
822 if (bmpImage->green_mask!=0x00ff00 ||
823 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
824 goto notsupported;
825 } else if (bmpImage->blue_mask==0xff) {
826 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
827 for (h=0; h<lines; h++) {
828 BYTE dstval;
829 srcbyte=srcbits;
830 dstbyte=dstbits;
831 dstval=0;
832 for (x=0; x<width; x++) {
833 dstval|=(X11DRV_DIB_GetNearestIndex
834 (colors, 2,
835 srcbyte[2],
836 srcbyte[1],
837 srcbyte[0]) << (7-(x&7)) );
838 srcbyte+=bytes_per_pixel;
839 if ((x&7)==7) {
840 *dstbyte++=dstval;
841 dstval=0;
844 if ((width&7)!=0) {
845 *dstbyte=dstval;
847 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
848 dstbits += linebytes;
850 } else {
851 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
852 for (h=0; h<lines; h++) {
853 BYTE dstval;
854 srcbyte=srcbits;
855 dstbyte=dstbits;
856 dstval=0;
857 for (x=0; x<width; x++) {
858 dstval|=(X11DRV_DIB_GetNearestIndex
859 (colors, 2,
860 srcbyte[0],
861 srcbyte[1],
862 srcbyte[2]) << (7-(x&7)) );
863 srcbyte+=bytes_per_pixel;
864 if ((x&7)==7) {
865 *dstbyte++=dstval;
866 dstval=0;
869 if ((width&7)!=0) {
870 *dstbyte=dstval;
872 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
873 dstbits += linebytes;
877 break;
879 default:
880 notsupported:
882 BYTE* dstbyte;
883 BYTE neg = 0;
884 unsigned long white = (1 << bmpImage->bits_per_pixel) - 1;
886 /* ==== any bmp format -> pal 1 dib ==== */
887 if ((unsigned)colors[0].rgbRed+colors[0].rgbGreen+colors[0].rgbBlue >=
888 (unsigned)colors[1].rgbRed+colors[1].rgbGreen+colors[1].rgbBlue )
889 neg = 1;
891 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB, "
892 "%s color mapping\n",
893 bmpImage->bits_per_pixel, bmpImage->red_mask,
894 bmpImage->green_mask, bmpImage->blue_mask,
895 neg?"negative":"direct" );
897 for (h=lines-1; h>=0; h--) {
898 BYTE dstval;
899 dstbyte=dstbits;
900 dstval=0;
901 for (x=0; x<width; x++) {
902 dstval|=((XGetPixel( bmpImage, x, h) >= white) ^ neg) << (7 - (x&7));
903 if ((x&7)==7) {
904 *dstbyte++=dstval;
905 dstval=0;
908 if ((width&7)!=0) {
909 *dstbyte=dstval;
911 dstbits += linebytes;
914 break;
918 /***********************************************************************
919 * X11DRV_DIB_SetImageBits_4
921 * SetDIBits for a 4-bit deep DIB.
923 static void X11DRV_DIB_SetImageBits_4( int lines, const BYTE *srcbits,
924 DWORD srcwidth, DWORD dstwidth, int left,
925 int *colors, XImage *bmpImage, int linebytes)
927 int h, width;
928 const BYTE* srcbyte;
929 DWORD i, x;
931 if (lines < 0 ) {
932 lines = -lines;
933 srcbits = srcbits + linebytes * (lines - 1);
934 linebytes = -linebytes;
937 if (left & 1) {
938 left--;
939 dstwidth++;
941 srcbits += left >> 1;
942 width = min(srcwidth, dstwidth);
944 /* ==== pal 4 dib -> any bmp format ==== */
945 for (h = lines-1; h >= 0; h--) {
946 srcbyte=srcbits;
947 for (i = width/2, x = left; i > 0; i--) {
948 BYTE srcval=*srcbyte++;
949 XPutPixel( bmpImage, x++, h, colors[srcval >> 4] );
950 XPutPixel( bmpImage, x++, h, colors[srcval & 0x0f] );
952 if (width & 1)
953 XPutPixel( bmpImage, x, h, colors[*srcbyte >> 4] );
954 srcbits += linebytes;
960 /***********************************************************************
961 * X11DRV_DIB_GetImageBits_4
963 * GetDIBits for a 4-bit deep DIB.
965 static void X11DRV_DIB_GetImageBits_4( int lines, BYTE *dstbits,
966 DWORD srcwidth, DWORD dstwidth,
967 RGBQUAD *colors, PALETTEENTRY *srccolors,
968 XImage *bmpImage, int linebytes )
970 DWORD x;
971 int h, width = min(srcwidth, dstwidth);
973 if (lines < 0 )
975 lines = -lines;
976 dstbits = dstbits + ( linebytes * (lines-1) );
977 linebytes = -linebytes;
980 switch (bmpImage->depth) {
981 case 1:
982 case 4:
983 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
984 && srccolors) {
985 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
986 BYTE* dstbyte;
988 for (h = lines-1; h >= 0; h--) {
989 BYTE dstval;
990 dstbyte=dstbits;
991 dstval=0;
992 for (x = 0; x < width; x++) {
993 PALETTEENTRY srcval;
994 srcval=srccolors[XGetPixel(bmpImage, x, h)];
995 dstval|=(X11DRV_DIB_GetNearestIndex
996 (colors, 16,
997 srcval.peRed,
998 srcval.peGreen,
999 srcval.peBlue) << (4-((x&1)<<2)));
1000 if ((x&1)==1) {
1001 *dstbyte++=dstval;
1002 dstval=0;
1005 if ((width&1)!=0) {
1006 *dstbyte=dstval;
1008 dstbits += linebytes;
1010 } else {
1011 goto notsupported;
1013 break;
1015 case 8:
1016 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1017 && srccolors) {
1018 /* ==== pal 8 bmp -> pal 4 dib ==== */
1019 const void* srcbits;
1020 const BYTE *srcpixel;
1021 BYTE* dstbyte;
1023 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1024 for (h=0; h<lines; h++) {
1025 BYTE dstval;
1026 srcpixel=srcbits;
1027 dstbyte=dstbits;
1028 dstval=0;
1029 for (x=0; x<width; x++) {
1030 PALETTEENTRY srcval;
1031 srcval = srccolors[*srcpixel++];
1032 dstval|=(X11DRV_DIB_GetNearestIndex
1033 (colors, 16,
1034 srcval.peRed,
1035 srcval.peGreen,
1036 srcval.peBlue) << (4*(1-(x&1))) );
1037 if ((x&1)==1) {
1038 *dstbyte++=dstval;
1039 dstval=0;
1042 if ((width&1)!=0) {
1043 *dstbyte=dstval;
1045 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1046 dstbits += linebytes;
1048 } else {
1049 goto notsupported;
1051 break;
1053 case 15:
1054 case 16:
1056 const void* srcbits;
1057 const WORD* srcpixel;
1058 BYTE* dstbyte;
1060 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1062 if (bmpImage->green_mask==0x03e0) {
1063 if (bmpImage->red_mask==0x7c00) {
1064 /* ==== rgb 555 bmp -> pal 4 dib ==== */
1065 for (h=0; h<lines; h++) {
1066 BYTE dstval;
1067 srcpixel=srcbits;
1068 dstbyte=dstbits;
1069 dstval=0;
1070 for (x=0; x<width; x++) {
1071 WORD srcval;
1072 srcval=*srcpixel++;
1073 dstval|=(X11DRV_DIB_GetNearestIndex
1074 (colors, 16,
1075 ((srcval >> 7) & 0xf8) | /* r */
1076 ((srcval >> 12) & 0x07),
1077 ((srcval >> 2) & 0xf8) | /* g */
1078 ((srcval >> 7) & 0x07),
1079 ((srcval << 3) & 0xf8) | /* b */
1080 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1081 if ((x&1)==1) {
1082 *dstbyte++=dstval;
1083 dstval=0;
1086 if ((width&1)!=0) {
1087 *dstbyte=dstval;
1089 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1090 dstbits += linebytes;
1092 } else if (bmpImage->blue_mask==0x7c00) {
1093 /* ==== bgr 555 bmp -> pal 4 dib ==== */
1094 for (h=0; h<lines; h++) {
1095 WORD dstval;
1096 srcpixel=srcbits;
1097 dstbyte=dstbits;
1098 dstval=0;
1099 for (x=0; x<width; x++) {
1100 WORD srcval;
1101 srcval=*srcpixel++;
1102 dstval|=(X11DRV_DIB_GetNearestIndex
1103 (colors, 16,
1104 ((srcval << 3) & 0xf8) | /* r */
1105 ((srcval >> 2) & 0x07),
1106 ((srcval >> 2) & 0xf8) | /* g */
1107 ((srcval >> 7) & 0x07),
1108 ((srcval >> 7) & 0xf8) | /* b */
1109 ((srcval >> 12) & 0x07) ) << ((1-(x&1))<<2) );
1110 if ((x&1)==1) {
1111 *dstbyte++=dstval;
1112 dstval=0;
1115 if ((width&1)!=0) {
1116 *dstbyte=dstval;
1118 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1119 dstbits += linebytes;
1121 } else {
1122 goto notsupported;
1124 } else if (bmpImage->green_mask==0x07e0) {
1125 if (bmpImage->red_mask==0xf800) {
1126 /* ==== rgb 565 bmp -> pal 4 dib ==== */
1127 for (h=0; h<lines; h++) {
1128 BYTE dstval;
1129 srcpixel=srcbits;
1130 dstbyte=dstbits;
1131 dstval=0;
1132 for (x=0; x<width; x++) {
1133 WORD srcval;
1134 srcval=*srcpixel++;
1135 dstval|=(X11DRV_DIB_GetNearestIndex
1136 (colors, 16,
1137 ((srcval >> 8) & 0xf8) | /* r */
1138 ((srcval >> 13) & 0x07),
1139 ((srcval >> 3) & 0xfc) | /* g */
1140 ((srcval >> 9) & 0x03),
1141 ((srcval << 3) & 0xf8) | /* b */
1142 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1143 if ((x&1)==1) {
1144 *dstbyte++=dstval;
1145 dstval=0;
1148 if ((width&1)!=0) {
1149 *dstbyte=dstval;
1151 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1152 dstbits += linebytes;
1154 } else if (bmpImage->blue_mask==0xf800) {
1155 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1156 for (h=0; h<lines; h++) {
1157 WORD dstval;
1158 srcpixel=srcbits;
1159 dstbyte=dstbits;
1160 dstval=0;
1161 for (x=0; x<width; x++) {
1162 WORD srcval;
1163 srcval=*srcpixel++;
1164 dstval|=(X11DRV_DIB_GetNearestIndex
1165 (colors, 16,
1166 ((srcval << 3) & 0xf8) | /* r */
1167 ((srcval >> 2) & 0x07),
1168 ((srcval >> 3) & 0xfc) | /* g */
1169 ((srcval >> 9) & 0x03),
1170 ((srcval >> 8) & 0xf8) | /* b */
1171 ((srcval >> 13) & 0x07) ) << ((1-(x&1))<<2) );
1172 if ((x&1)==1) {
1173 *dstbyte++=dstval;
1174 dstval=0;
1177 if ((width&1)!=0) {
1178 *dstbyte=dstval;
1180 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1181 dstbits += linebytes;
1183 } else {
1184 goto notsupported;
1186 } else {
1187 goto notsupported;
1190 break;
1192 case 24:
1193 if (bmpImage->bits_per_pixel==24) {
1194 const void* srcbits;
1195 const BYTE *srcbyte;
1196 BYTE* dstbyte;
1198 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1200 if (bmpImage->green_mask!=0x00ff00 ||
1201 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1202 goto notsupported;
1203 } else if (bmpImage->blue_mask==0xff) {
1204 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1205 for (h=0; h<lines; h++) {
1206 srcbyte=srcbits;
1207 dstbyte=dstbits;
1208 for (x=0; x<width/2; x++) {
1209 /* Do 2 pixels at a time */
1210 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1211 (colors, 16,
1212 srcbyte[2],
1213 srcbyte[1],
1214 srcbyte[0]) << 4) |
1215 X11DRV_DIB_GetNearestIndex
1216 (colors, 16,
1217 srcbyte[5],
1218 srcbyte[4],
1219 srcbyte[3]);
1220 srcbyte+=6;
1222 if (width&1) {
1223 /* And then the odd pixel */
1224 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1225 (colors, 16,
1226 srcbyte[2],
1227 srcbyte[1],
1228 srcbyte[0]) << 4);
1230 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1231 dstbits += linebytes;
1233 } else {
1234 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1235 for (h=0; h<lines; h++) {
1236 srcbyte=srcbits;
1237 dstbyte=dstbits;
1238 for (x=0; x<width/2; x++) {
1239 /* Do 2 pixels at a time */
1240 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1241 (colors, 16,
1242 srcbyte[0],
1243 srcbyte[1],
1244 srcbyte[2]) << 4) |
1245 X11DRV_DIB_GetNearestIndex
1246 (colors, 16,
1247 srcbyte[3],
1248 srcbyte[4],
1249 srcbyte[5]);
1250 srcbyte+=6;
1252 if (width&1) {
1253 /* And then the odd pixel */
1254 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1255 (colors, 16,
1256 srcbyte[0],
1257 srcbyte[1],
1258 srcbyte[2]) << 4);
1260 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1261 dstbits += linebytes;
1264 break;
1266 /* Fall through */
1268 case 32:
1270 const void* srcbits;
1271 const BYTE *srcbyte;
1272 BYTE* dstbyte;
1274 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1276 if (bmpImage->green_mask!=0x00ff00 ||
1277 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1278 goto notsupported;
1279 } else if (bmpImage->blue_mask==0xff) {
1280 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1281 for (h=0; h<lines; h++) {
1282 srcbyte=srcbits;
1283 dstbyte=dstbits;
1284 for (x=0; x<width/2; x++) {
1285 /* Do 2 pixels at a time */
1286 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1287 (colors, 16,
1288 srcbyte[2],
1289 srcbyte[1],
1290 srcbyte[0]) << 4) |
1291 X11DRV_DIB_GetNearestIndex
1292 (colors, 16,
1293 srcbyte[6],
1294 srcbyte[5],
1295 srcbyte[4]);
1296 srcbyte+=8;
1298 if (width&1) {
1299 /* And then the odd pixel */
1300 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1301 (colors, 16,
1302 srcbyte[2],
1303 srcbyte[1],
1304 srcbyte[0]) << 4);
1306 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1307 dstbits += linebytes;
1309 } else {
1310 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1311 for (h=0; h<lines; h++) {
1312 srcbyte=srcbits;
1313 dstbyte=dstbits;
1314 for (x=0; x<width/2; x++) {
1315 /* Do 2 pixels at a time */
1316 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1317 (colors, 16,
1318 srcbyte[0],
1319 srcbyte[1],
1320 srcbyte[2]) << 4) |
1321 X11DRV_DIB_GetNearestIndex
1322 (colors, 16,
1323 srcbyte[4],
1324 srcbyte[5],
1325 srcbyte[6]);
1326 srcbyte+=8;
1328 if (width&1) {
1329 /* And then the odd pixel */
1330 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1331 (colors, 16,
1332 srcbyte[0],
1333 srcbyte[1],
1334 srcbyte[2]) << 4);
1336 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1337 dstbits += linebytes;
1341 break;
1343 default:
1344 notsupported:
1346 BYTE* dstbyte;
1348 /* ==== any bmp format -> pal 4 dib ==== */
1349 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1350 bmpImage->bits_per_pixel, bmpImage->red_mask,
1351 bmpImage->green_mask, bmpImage->blue_mask );
1352 for (h=lines-1; h>=0; h--) {
1353 dstbyte=dstbits;
1354 for (x=0; x<(width & ~1); x+=2) {
1355 *dstbyte++=(X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4) |
1356 X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x+1, h), 0);
1358 if (width & 1) {
1359 *dstbyte=(X11DRV_DIB_MapColor((int *)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4);
1361 dstbits += linebytes;
1364 break;
1368 /***********************************************************************
1369 * X11DRV_DIB_SetImageBits_RLE4
1371 * SetDIBits for a 4-bit deep compressed DIB.
1373 static void X11DRV_DIB_SetImageBits_RLE4( int lines, const BYTE *bits,
1374 DWORD srcwidth, DWORD dstwidth,
1375 int left, int *colors,
1376 XImage *bmpImage )
1378 unsigned int x = 0, width = min(srcwidth, dstwidth);
1379 int y = lines - 1, c, length;
1380 const BYTE *begin = bits;
1382 while (y >= 0)
1384 length = *bits++;
1385 if (length) { /* encoded */
1386 c = *bits++;
1387 while (length--) {
1388 if (x >= (left + width)) break;
1389 if( x >= left) XPutPixel(bmpImage, x, y, colors[c >> 4]);
1390 x++;
1391 if (!length--) break;
1392 if (x >= (left + width)) break;
1393 if( x >= left) XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1394 x++;
1396 } else {
1397 length = *bits++;
1398 switch (length)
1400 case RLE_EOL:
1401 x = 0;
1402 y--;
1403 break;
1405 case RLE_END:
1406 return;
1408 case RLE_DELTA:
1409 x += *bits++;
1410 y -= *bits++;
1411 break;
1413 default: /* absolute */
1414 while (length--) {
1415 c = *bits++;
1416 if (x >= left && x < (left + width))
1417 XPutPixel(bmpImage, x, y, colors[c >> 4]);
1418 x++;
1419 if (!length--) break;
1420 if (x >= left && x < (left + width))
1421 XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1422 x++;
1424 if ((bits - begin) & 1)
1425 bits++;
1433 /***********************************************************************
1434 * X11DRV_DIB_SetImageBits_8
1436 * SetDIBits for an 8-bit deep DIB.
1438 static void X11DRV_DIB_SetImageBits_8( int lines, const BYTE *srcbits,
1439 DWORD srcwidth, DWORD dstwidth, int left,
1440 const int *colors, XImage *bmpImage,
1441 int linebytes )
1443 DWORD x;
1444 int h, width = min(srcwidth, dstwidth);
1445 const BYTE* srcbyte;
1446 BYTE* dstbits;
1448 if (lines < 0 )
1450 lines = -lines;
1451 srcbits = srcbits + linebytes * (lines-1);
1452 linebytes = -linebytes;
1454 srcbits += left;
1455 srcbyte = srcbits;
1457 switch (bmpImage->depth) {
1458 case 15:
1459 case 16:
1460 /* Some X servers might have 32 bit/ 16bit deep pixel */
1461 if (lines && width && (bmpImage->bits_per_pixel == 16) &&
1462 (ImageByteOrder(gdi_display)==LSBFirst) )
1464 /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1465 dstbits=(BYTE*)bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1466 for (h = lines ; h--; ) {
1467 #if defined(__i386__) && defined(__GNUC__)
1468 int _cl1,_cl2; /* temp outputs for asm below */
1469 /* Borrowed from DirectDraw */
1470 __asm__ __volatile__(
1471 "xor %%eax,%%eax\n"
1472 "cld\n"
1473 "1:\n"
1474 " lodsb\n"
1475 " movw (%%edx,%%eax,4),%%ax\n"
1476 " stosw\n"
1477 " xor %%eax,%%eax\n"
1478 " loop 1b\n"
1479 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1480 :"S" (srcbyte),
1481 "D" (dstbits),
1482 "c" (width),
1483 "d" (colors)
1484 :"eax", "cc", "memory"
1486 #else
1487 DWORD* dstpixel=(DWORD*)dstbits;
1488 for (x=0; x<width/2; x++) {
1489 /* Do 2 pixels at a time */
1490 *dstpixel++=(colors[srcbyte[1]] << 16) | colors[srcbyte[0]];
1491 srcbyte+=2;
1493 if (width&1) {
1494 /* And then the odd pixel */
1495 *((WORD*)dstpixel)=colors[srcbyte[0]];
1497 #endif
1498 srcbyte = (srcbits += linebytes);
1499 dstbits -= bmpImage->bytes_per_line;
1501 return;
1503 break;
1504 case 24:
1505 case 32:
1506 if (lines && width && (bmpImage->bits_per_pixel == 32) &&
1507 (ImageByteOrder(gdi_display)==LSBFirst) )
1509 dstbits=(BYTE*)bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
1510 /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1511 for (h = lines ; h--; ) {
1512 #if defined(__i386__) && defined(__GNUC__)
1513 int _cl1,_cl2; /* temp outputs for asm below */
1514 /* Borrowed from DirectDraw */
1515 __asm__ __volatile__(
1516 "xor %%eax,%%eax\n"
1517 "cld\n"
1518 "1:\n"
1519 " lodsb\n"
1520 " movl (%%edx,%%eax,4),%%eax\n"
1521 " stosl\n"
1522 " xor %%eax,%%eax\n"
1523 " loop 1b\n"
1524 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1525 :"S" (srcbyte),
1526 "D" (dstbits),
1527 "c" (width),
1528 "d" (colors)
1529 :"eax", "cc", "memory"
1531 #else
1532 DWORD* dstpixel=(DWORD*)dstbits;
1533 for (x=0; x<width; x++) {
1534 *dstpixel++=colors[*srcbyte++];
1536 #endif
1537 srcbyte = (srcbits += linebytes);
1538 dstbits -= bmpImage->bytes_per_line;
1540 return;
1542 break;
1543 default:
1544 break; /* use slow generic case below */
1547 /* ==== pal 8 dib -> any bmp format ==== */
1548 for (h=lines-1; h>=0; h--) {
1549 for (x=left; x<width+left; x++) {
1550 XPutPixel(bmpImage, x, h, colors[*srcbyte++]);
1552 srcbyte = (srcbits += linebytes);
1556 /***********************************************************************
1557 * X11DRV_DIB_GetImageBits_8
1559 * GetDIBits for an 8-bit deep DIB.
1561 static void X11DRV_DIB_GetImageBits_8( int lines, BYTE *dstbits,
1562 DWORD srcwidth, DWORD dstwidth,
1563 RGBQUAD *colors, PALETTEENTRY *srccolors,
1564 XImage *bmpImage, int linebytes )
1566 DWORD x;
1567 int h, width = min(srcwidth, dstwidth);
1568 BYTE* dstbyte;
1570 if (lines < 0 )
1572 lines = -lines;
1573 dstbits = dstbits + ( linebytes * (lines-1) );
1574 linebytes = -linebytes;
1578 * Hack for now
1579 * This condition is true when GetImageBits has been called by
1580 * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1581 * 256 colormaps, so we'll just use it for GetDIBits calls.
1582 * (In some cases, in an updateDIBSection, the returned colors are bad too)
1584 if (!srccolors) goto updatesection;
1586 switch (bmpImage->depth) {
1587 case 1:
1588 case 4:
1589 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1590 && srccolors) {
1592 /* ==== pal 1 bmp -> pal 8 dib ==== */
1593 /* ==== pal 4 bmp -> pal 8 dib ==== */
1594 for (h=lines-1; h>=0; h--) {
1595 dstbyte=dstbits;
1596 for (x=0; x<width; x++) {
1597 PALETTEENTRY srcval;
1598 srcval=srccolors[XGetPixel(bmpImage, x, h)];
1599 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1600 srcval.peRed,
1601 srcval.peGreen,
1602 srcval.peBlue);
1604 dstbits += linebytes;
1606 } else {
1607 goto notsupported;
1609 break;
1611 case 8:
1612 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1613 && srccolors) {
1614 /* ==== pal 8 bmp -> pal 8 dib ==== */
1615 const void* srcbits;
1616 const BYTE* srcpixel;
1618 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1619 for (h=0; h<lines; h++) {
1620 srcpixel=srcbits;
1621 dstbyte=dstbits;
1622 for (x = 0; x < width; x++) {
1623 PALETTEENTRY srcval;
1624 srcval=srccolors[*srcpixel++];
1625 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1626 srcval.peRed,
1627 srcval.peGreen,
1628 srcval.peBlue);
1630 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1631 dstbits += linebytes;
1633 } else {
1634 goto notsupported;
1636 break;
1638 case 15:
1639 case 16:
1641 const void* srcbits;
1642 const WORD* srcpixel;
1643 BYTE* dstbyte;
1645 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1647 if (bmpImage->green_mask==0x03e0) {
1648 if (bmpImage->red_mask==0x7c00) {
1649 /* ==== rgb 555 bmp -> pal 8 dib ==== */
1650 for (h=0; h<lines; h++) {
1651 srcpixel=srcbits;
1652 dstbyte=dstbits;
1653 for (x=0; x<width; x++) {
1654 WORD srcval;
1655 srcval=*srcpixel++;
1656 *dstbyte++=X11DRV_DIB_GetNearestIndex
1657 (colors, 256,
1658 ((srcval >> 7) & 0xf8) | /* r */
1659 ((srcval >> 12) & 0x07),
1660 ((srcval >> 2) & 0xf8) | /* g */
1661 ((srcval >> 7) & 0x07),
1662 ((srcval << 3) & 0xf8) | /* b */
1663 ((srcval >> 2) & 0x07) );
1665 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1666 dstbits += linebytes;
1668 } else if (bmpImage->blue_mask==0x7c00) {
1669 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1670 for (h=0; h<lines; h++) {
1671 srcpixel=srcbits;
1672 dstbyte=dstbits;
1673 for (x=0; x<width; x++) {
1674 WORD srcval;
1675 srcval=*srcpixel++;
1676 *dstbyte++=X11DRV_DIB_GetNearestIndex
1677 (colors, 256,
1678 ((srcval << 3) & 0xf8) | /* r */
1679 ((srcval >> 2) & 0x07),
1680 ((srcval >> 2) & 0xf8) | /* g */
1681 ((srcval >> 7) & 0x07),
1682 ((srcval >> 7) & 0xf8) | /* b */
1683 ((srcval >> 12) & 0x07) );
1685 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1686 dstbits += linebytes;
1688 } else {
1689 goto notsupported;
1691 } else if (bmpImage->green_mask==0x07e0) {
1692 if (bmpImage->red_mask==0xf800) {
1693 /* ==== rgb 565 bmp -> pal 8 dib ==== */
1694 for (h=0; h<lines; h++) {
1695 srcpixel=srcbits;
1696 dstbyte=dstbits;
1697 for (x=0; x<width; x++) {
1698 WORD srcval;
1699 srcval=*srcpixel++;
1700 *dstbyte++=X11DRV_DIB_GetNearestIndex
1701 (colors, 256,
1702 ((srcval >> 8) & 0xf8) | /* r */
1703 ((srcval >> 13) & 0x07),
1704 ((srcval >> 3) & 0xfc) | /* g */
1705 ((srcval >> 9) & 0x03),
1706 ((srcval << 3) & 0xf8) | /* b */
1707 ((srcval >> 2) & 0x07) );
1709 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1710 dstbits += linebytes;
1712 } else if (bmpImage->blue_mask==0xf800) {
1713 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1714 for (h=0; h<lines; h++) {
1715 srcpixel=srcbits;
1716 dstbyte=dstbits;
1717 for (x=0; x<width; x++) {
1718 WORD srcval;
1719 srcval=*srcpixel++;
1720 *dstbyte++=X11DRV_DIB_GetNearestIndex
1721 (colors, 256,
1722 ((srcval << 3) & 0xf8) | /* r */
1723 ((srcval >> 2) & 0x07),
1724 ((srcval >> 3) & 0xfc) | /* g */
1725 ((srcval >> 9) & 0x03),
1726 ((srcval >> 8) & 0xf8) | /* b */
1727 ((srcval >> 13) & 0x07) );
1729 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1730 dstbits += linebytes;
1732 } else {
1733 goto notsupported;
1735 } else {
1736 goto notsupported;
1739 break;
1741 case 24:
1742 case 32:
1744 const void* srcbits;
1745 const BYTE *srcbyte;
1746 BYTE* dstbyte;
1747 int bytes_per_pixel;
1749 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1750 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
1752 if (bmpImage->green_mask!=0x00ff00 ||
1753 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1754 goto notsupported;
1755 } else if (bmpImage->blue_mask==0xff) {
1756 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1757 for (h=0; h<lines; h++) {
1758 srcbyte=srcbits;
1759 dstbyte=dstbits;
1760 for (x=0; x<width; x++) {
1761 *dstbyte++=X11DRV_DIB_GetNearestIndex
1762 (colors, 256,
1763 srcbyte[2],
1764 srcbyte[1],
1765 srcbyte[0]);
1766 srcbyte+=bytes_per_pixel;
1768 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1769 dstbits += linebytes;
1771 } else {
1772 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1773 for (h=0; h<lines; h++) {
1774 srcbyte=srcbits;
1775 dstbyte=dstbits;
1776 for (x=0; x<width; x++) {
1777 *dstbyte++=X11DRV_DIB_GetNearestIndex
1778 (colors, 256,
1779 srcbyte[0],
1780 srcbyte[1],
1781 srcbyte[2]);
1782 srcbyte+=bytes_per_pixel;
1784 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1785 dstbits += linebytes;
1789 break;
1791 default:
1792 notsupported:
1793 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1794 bmpImage->depth, bmpImage->red_mask,
1795 bmpImage->green_mask, bmpImage->blue_mask );
1796 updatesection:
1797 /* ==== any bmp format -> pal 8 dib ==== */
1798 for (h=lines-1; h>=0; h--) {
1799 dstbyte=dstbits;
1800 for (x=0; x<width; x++) {
1801 *dstbyte=X11DRV_DIB_MapColor
1802 ((int*)colors, 256,
1803 XGetPixel(bmpImage, x, h), *dstbyte);
1804 dstbyte++;
1806 dstbits += linebytes;
1808 break;
1812 /***********************************************************************
1813 * X11DRV_DIB_SetImageBits_RLE8
1815 * SetDIBits for an 8-bit deep compressed DIB.
1817 * This function rewritten 941113 by James Youngman. WINE blew out when I
1818 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
1820 * This was because the algorithm assumed that all RLE8 bitmaps end with the
1821 * 'End of bitmap' escape code. This code is very much laxer in what it
1822 * allows to end the expansion. Possibly too lax. See the note by
1823 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
1824 * bitmap should end with RleEnd, but on the other hand, software exists
1825 * that produces ones that don't and Windows 3.1 doesn't complain a bit
1826 * about it.
1828 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
1829 * James A. Youngman <mbcstjy@afs.man.ac.uk>
1830 * [JAY]
1832 static void X11DRV_DIB_SetImageBits_RLE8( int lines, const BYTE *bits,
1833 DWORD srcwidth, DWORD dstwidth,
1834 int left, int *colors,
1835 XImage *bmpImage )
1837 unsigned int x; /* X-position on each line. Increases. */
1838 int y; /* Line #. Starts at lines-1, decreases */
1839 const BYTE *pIn = bits; /* Pointer to current position in bits */
1840 BYTE length; /* The length pf a run */
1841 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
1844 * Note that the bitmap data is stored by Windows starting at the
1845 * bottom line of the bitmap and going upwards. Within each line,
1846 * the data is stored left-to-right. That's the reason why line
1847 * goes from lines-1 to 0. [JAY]
1850 x = 0;
1851 y = lines - 1;
1852 while (y >= 0)
1854 length = *pIn++;
1857 * If the length byte is not zero (which is the escape value),
1858 * We have a run of length pixels all the same colour. The colour
1859 * index is stored next.
1861 * If the length byte is zero, we need to read the next byte to
1862 * know what to do. [JAY]
1864 if (length != 0)
1867 * [Run-Length] Encoded mode
1869 int color = colors[*pIn++];
1870 while (length-- && x < (left + dstwidth)) {
1871 if( x >= left) XPutPixel(bmpImage, x, y, color);
1872 x++;
1875 else
1878 * Escape codes (may be an absolute sequence though)
1880 escape_code = (*pIn++);
1881 switch(escape_code)
1883 case RLE_EOL:
1884 x = 0;
1885 y--;
1886 break;
1888 case RLE_END:
1889 /* Not all RLE8 bitmaps end with this code. For
1890 * example, Paint Shop Pro produces some that don't.
1891 * That's (I think) what caused the previous
1892 * implementation to fail. [JAY]
1894 return;
1896 case RLE_DELTA:
1897 x += (*pIn++);
1898 y -= (*pIn++);
1899 break;
1901 default: /* switch to absolute mode */
1902 length = escape_code;
1903 while (length--)
1905 int color = colors[*pIn++];
1906 if (x >= (left + dstwidth))
1908 pIn += length;
1909 break;
1911 if( x >= left) XPutPixel(bmpImage, x, y, color);
1912 x++;
1915 * If you think for a moment you'll realise that the
1916 * only time we could ever possibly read an odd
1917 * number of bytes is when there is a 0x00 (escape),
1918 * a value >0x02 (absolute mode) and then an odd-
1919 * length run. Therefore this is the only place we
1920 * need to worry about it. Everywhere else the
1921 * bytes are always read in pairs. [JAY]
1923 if (escape_code & 1) pIn++; /* Throw away the pad byte. */
1924 break;
1925 } /* switch (escape_code) : Escape sequence */
1931 /***********************************************************************
1932 * X11DRV_DIB_SetImageBits_16
1934 * SetDIBits for a 16-bit deep DIB.
1936 static void X11DRV_DIB_SetImageBits_16( int lines, const BYTE *srcbits,
1937 DWORD srcwidth, DWORD dstwidth, int left,
1938 X11DRV_PDEVICE *physDev, DWORD rSrc, DWORD gSrc, DWORD bSrc,
1939 XImage *bmpImage, int linebytes )
1941 DWORD x;
1942 int h, width = min(srcwidth, dstwidth);
1943 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
1945 if (lines < 0 )
1947 lines = -lines;
1948 srcbits = srcbits + ( linebytes * (lines-1));
1949 linebytes = -linebytes;
1952 switch (bmpImage->depth)
1954 case 15:
1955 case 16:
1957 char* dstbits;
1959 srcbits=srcbits+left*2;
1960 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1962 if (bmpImage->green_mask==0x03e0) {
1963 if (gSrc==bmpImage->green_mask) {
1964 if (rSrc==bmpImage->red_mask) {
1965 /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1966 /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1967 convs->Convert_5x5_asis
1968 (width,lines,
1969 srcbits,linebytes,
1970 dstbits,-bmpImage->bytes_per_line);
1971 } else if (rSrc==bmpImage->blue_mask) {
1972 /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1973 /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1974 convs->Convert_555_reverse
1975 (width,lines,
1976 srcbits,linebytes,
1977 dstbits,-bmpImage->bytes_per_line);
1979 } else {
1980 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1981 /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1982 /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1983 convs->Convert_565_to_555_asis
1984 (width,lines,
1985 srcbits,linebytes,
1986 dstbits,-bmpImage->bytes_per_line);
1987 } else {
1988 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1989 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1990 convs->Convert_565_to_555_reverse
1991 (width,lines,
1992 srcbits,linebytes,
1993 dstbits,-bmpImage->bytes_per_line);
1996 } else if (bmpImage->green_mask==0x07e0) {
1997 if (gSrc==bmpImage->green_mask) {
1998 if (rSrc==bmpImage->red_mask) {
1999 /* ==== rgb 565 dib -> rgb 565 bmp ==== */
2000 /* ==== bgr 565 dib -> bgr 565 bmp ==== */
2001 convs->Convert_5x5_asis
2002 (width,lines,
2003 srcbits,linebytes,
2004 dstbits,-bmpImage->bytes_per_line);
2005 } else {
2006 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
2007 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
2008 convs->Convert_565_reverse
2009 (width,lines,
2010 srcbits,linebytes,
2011 dstbits,-bmpImage->bytes_per_line);
2013 } else {
2014 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
2015 /* ==== rgb 555 dib -> rgb 565 bmp ==== */
2016 /* ==== bgr 555 dib -> bgr 565 bmp ==== */
2017 convs->Convert_555_to_565_asis
2018 (width,lines,
2019 srcbits,linebytes,
2020 dstbits,-bmpImage->bytes_per_line);
2021 } else {
2022 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
2023 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
2024 convs->Convert_555_to_565_reverse
2025 (width,lines,
2026 srcbits,linebytes,
2027 dstbits,-bmpImage->bytes_per_line);
2030 } else {
2031 goto notsupported;
2034 break;
2036 case 24:
2037 if (bmpImage->bits_per_pixel==24) {
2038 char* dstbits;
2040 srcbits=srcbits+left*2;
2041 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2043 if (bmpImage->green_mask!=0x00ff00 ||
2044 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2045 goto notsupported;
2046 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2047 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2048 if (gSrc==0x03e0) {
2049 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
2050 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
2051 convs->Convert_555_to_888_asis
2052 (width,lines,
2053 srcbits,linebytes,
2054 dstbits,-bmpImage->bytes_per_line);
2055 } else {
2056 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
2057 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
2058 convs->Convert_565_to_888_asis
2059 (width,lines,
2060 srcbits,linebytes,
2061 dstbits,-bmpImage->bytes_per_line);
2063 } else {
2064 if (gSrc==0x03e0) {
2065 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
2066 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
2067 convs->Convert_555_to_888_reverse
2068 (width,lines,
2069 srcbits,linebytes,
2070 dstbits,-bmpImage->bytes_per_line);
2071 } else {
2072 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
2073 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
2074 convs->Convert_565_to_888_reverse
2075 (width,lines,
2076 srcbits,linebytes,
2077 dstbits,-bmpImage->bytes_per_line);
2080 break;
2082 /* Fall through */
2084 case 32:
2086 char* dstbits;
2088 srcbits=srcbits+left*2;
2089 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2091 if (bmpImage->green_mask!=0x00ff00 ||
2092 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2093 goto notsupported;
2094 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2095 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2096 if (gSrc==0x03e0) {
2097 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
2098 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
2099 convs->Convert_555_to_0888_asis
2100 (width,lines,
2101 srcbits,linebytes,
2102 dstbits,-bmpImage->bytes_per_line);
2103 } else {
2104 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
2105 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
2106 convs->Convert_565_to_0888_asis
2107 (width,lines,
2108 srcbits,linebytes,
2109 dstbits,-bmpImage->bytes_per_line);
2111 } else {
2112 if (gSrc==0x03e0) {
2113 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
2114 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
2115 convs->Convert_555_to_0888_reverse
2116 (width,lines,
2117 srcbits,linebytes,
2118 dstbits,-bmpImage->bytes_per_line);
2119 } else {
2120 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
2121 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
2122 convs->Convert_565_to_0888_reverse
2123 (width,lines,
2124 srcbits,linebytes,
2125 dstbits,-bmpImage->bytes_per_line);
2129 break;
2131 default:
2132 notsupported:
2133 WARN("from 16 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2134 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2135 bmpImage->green_mask, bmpImage->blue_mask );
2136 /* fall through */
2137 case 1:
2138 case 4:
2139 case 8:
2141 /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
2142 const WORD* srcpixel;
2143 int rShift1,gShift1,bShift1;
2144 int rShift2,gShift2,bShift2;
2145 BYTE gMask1,gMask2;
2147 /* Set color scaling values */
2148 rShift1=16+X11DRV_DIB_MaskToShift(rSrc)-3;
2149 gShift1=16+X11DRV_DIB_MaskToShift(gSrc)-3;
2150 bShift1=16+X11DRV_DIB_MaskToShift(bSrc)-3;
2151 rShift2=rShift1+5;
2152 gShift2=gShift1+5;
2153 bShift2=bShift1+5;
2154 if (gSrc==0x03e0) {
2155 /* Green has 5 bits, like the others */
2156 gMask1=0xf8;
2157 gMask2=0x07;
2158 } else {
2159 /* Green has 6 bits, not 5. Compensate. */
2160 gShift1++;
2161 gShift2+=2;
2162 gMask1=0xfc;
2163 gMask2=0x03;
2166 srcbits+=2*left;
2168 /* We could split it into four separate cases to optimize
2169 * but it is probably not worth it.
2171 for (h=lines-1; h>=0; h--) {
2172 srcpixel=(const WORD*)srcbits;
2173 for (x=left; x<width+left; x++) {
2174 DWORD srcval;
2175 BYTE red,green,blue;
2176 srcval=*srcpixel++ << 16;
2177 red= ((srcval >> rShift1) & 0xf8) |
2178 ((srcval >> rShift2) & 0x07);
2179 green=((srcval >> gShift1) & gMask1) |
2180 ((srcval >> gShift2) & gMask2);
2181 blue= ((srcval >> bShift1) & 0xf8) |
2182 ((srcval >> bShift2) & 0x07);
2183 XPutPixel(bmpImage, x, h,
2184 X11DRV_PALETTE_ToPhysical
2185 (physDev, RGB(red,green,blue)));
2187 srcbits += linebytes;
2190 break;
2195 /***********************************************************************
2196 * X11DRV_DIB_GetImageBits_16
2198 * GetDIBits for an 16-bit deep DIB.
2200 static void X11DRV_DIB_GetImageBits_16( X11DRV_PDEVICE *physDev, int lines, BYTE *dstbits,
2201 DWORD dstwidth, DWORD srcwidth,
2202 PALETTEENTRY *srccolors,
2203 DWORD rDst, DWORD gDst, DWORD bDst,
2204 XImage *bmpImage, int linebytes )
2206 DWORD x;
2207 int h, width = min(srcwidth, dstwidth);
2208 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2210 if (lines < 0 )
2212 lines = -lines;
2213 dstbits = dstbits + ( linebytes * (lines-1));
2214 linebytes = -linebytes;
2217 switch (bmpImage->depth)
2219 case 15:
2220 case 16:
2222 const char* srcbits;
2224 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2226 if (bmpImage->green_mask==0x03e0) {
2227 if (gDst==bmpImage->green_mask) {
2228 if (rDst==bmpImage->red_mask) {
2229 /* ==== rgb 555 bmp -> rgb 555 dib ==== */
2230 /* ==== bgr 555 bmp -> bgr 555 dib ==== */
2231 convs->Convert_5x5_asis
2232 (width,lines,
2233 srcbits,-bmpImage->bytes_per_line,
2234 dstbits,linebytes);
2235 } else {
2236 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
2237 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
2238 convs->Convert_555_reverse
2239 (width,lines,
2240 srcbits,-bmpImage->bytes_per_line,
2241 dstbits,linebytes);
2243 } else {
2244 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2245 /* ==== rgb 555 bmp -> rgb 565 dib ==== */
2246 /* ==== bgr 555 bmp -> bgr 565 dib ==== */
2247 convs->Convert_555_to_565_asis
2248 (width,lines,
2249 srcbits,-bmpImage->bytes_per_line,
2250 dstbits,linebytes);
2251 } else {
2252 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
2253 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
2254 convs->Convert_555_to_565_reverse
2255 (width,lines,
2256 srcbits,-bmpImage->bytes_per_line,
2257 dstbits,linebytes);
2260 } else if (bmpImage->green_mask==0x07e0) {
2261 if (gDst==bmpImage->green_mask) {
2262 if (rDst == bmpImage->red_mask) {
2263 /* ==== rgb 565 bmp -> rgb 565 dib ==== */
2264 /* ==== bgr 565 bmp -> bgr 565 dib ==== */
2265 convs->Convert_5x5_asis
2266 (width,lines,
2267 srcbits,-bmpImage->bytes_per_line,
2268 dstbits,linebytes);
2269 } else {
2270 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
2271 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
2272 convs->Convert_565_reverse
2273 (width,lines,
2274 srcbits,-bmpImage->bytes_per_line,
2275 dstbits,linebytes);
2277 } else {
2278 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2279 /* ==== rgb 565 bmp -> rgb 555 dib ==== */
2280 /* ==== bgr 565 bmp -> bgr 555 dib ==== */
2281 convs->Convert_565_to_555_asis
2282 (width,lines,
2283 srcbits,-bmpImage->bytes_per_line,
2284 dstbits,linebytes);
2285 } else {
2286 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
2287 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
2288 convs->Convert_565_to_555_reverse
2289 (width,lines,
2290 srcbits,-bmpImage->bytes_per_line,
2291 dstbits,linebytes);
2294 } else {
2295 goto notsupported;
2298 break;
2300 case 24:
2301 if (bmpImage->bits_per_pixel == 24) {
2302 const char* srcbits;
2304 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2306 if (bmpImage->green_mask!=0x00ff00 ||
2307 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2308 goto notsupported;
2309 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2310 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2311 if (gDst==0x03e0) {
2312 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
2313 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
2314 convs->Convert_888_to_555_asis
2315 (width,lines,
2316 srcbits,-bmpImage->bytes_per_line,
2317 dstbits,linebytes);
2318 } else {
2319 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2320 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2321 convs->Convert_888_to_565_asis
2322 (width,lines,
2323 srcbits,-bmpImage->bytes_per_line,
2324 dstbits,linebytes);
2326 } else {
2327 if (gDst==0x03e0) {
2328 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
2329 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
2330 convs->Convert_888_to_555_reverse
2331 (width,lines,
2332 srcbits,-bmpImage->bytes_per_line,
2333 dstbits,linebytes);
2334 } else {
2335 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
2336 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
2337 convs->Convert_888_to_565_reverse
2338 (width,lines,
2339 srcbits,-bmpImage->bytes_per_line,
2340 dstbits,linebytes);
2343 break;
2345 /* Fall through */
2347 case 32:
2349 const char* srcbits;
2351 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2353 if (bmpImage->green_mask!=0x00ff00 ||
2354 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2355 goto notsupported;
2356 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2357 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2358 if (gDst==0x03e0) {
2359 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2360 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2361 convs->Convert_0888_to_555_asis
2362 (width,lines,
2363 srcbits,-bmpImage->bytes_per_line,
2364 dstbits,linebytes);
2365 } else {
2366 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2367 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2368 convs->Convert_0888_to_565_asis
2369 (width,lines,
2370 srcbits,-bmpImage->bytes_per_line,
2371 dstbits,linebytes);
2373 } else {
2374 if (gDst==0x03e0) {
2375 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2376 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2377 convs->Convert_0888_to_555_reverse
2378 (width,lines,
2379 srcbits,-bmpImage->bytes_per_line,
2380 dstbits,linebytes);
2381 } else {
2382 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2383 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2384 convs->Convert_0888_to_565_reverse
2385 (width,lines,
2386 srcbits,-bmpImage->bytes_per_line,
2387 dstbits,linebytes);
2391 break;
2393 case 1:
2394 case 4:
2395 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2396 && srccolors) {
2397 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2398 int rShift,gShift,bShift;
2399 WORD* dstpixel;
2401 /* Shift everything 16 bits left so that all shifts are >0,
2402 * even for BGR DIBs. Then a single >> 16 will bring everything
2403 * back into place.
2405 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2406 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2407 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2408 if (gDst==0x07e0) {
2409 /* 6 bits for the green */
2410 gShift++;
2412 rDst=rDst << 16;
2413 gDst=gDst << 16;
2414 bDst=bDst << 16;
2415 for (h = lines - 1; h >= 0; h--) {
2416 dstpixel=(LPWORD)dstbits;
2417 for (x = 0; x < width; x++) {
2418 PALETTEENTRY srcval;
2419 DWORD dstval;
2420 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2421 dstval=((srcval.peRed << rShift) & rDst) |
2422 ((srcval.peGreen << gShift) & gDst) |
2423 ((srcval.peBlue << bShift) & bDst);
2424 *dstpixel++=dstval >> 16;
2426 dstbits += linebytes;
2428 } else {
2429 goto notsupported;
2431 break;
2433 case 8:
2434 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2435 && srccolors) {
2436 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2437 int rShift,gShift,bShift;
2438 const BYTE* srcbits;
2439 const BYTE* srcpixel;
2440 WORD* dstpixel;
2442 /* Shift everything 16 bits left so that all shifts are >0,
2443 * even for BGR DIBs. Then a single >> 16 will bring everything
2444 * back into place.
2446 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2447 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2448 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2449 if (gDst==0x07e0) {
2450 /* 6 bits for the green */
2451 gShift++;
2453 rDst=rDst << 16;
2454 gDst=gDst << 16;
2455 bDst=bDst << 16;
2456 srcbits=(BYTE*)bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2457 for (h=0; h<lines; h++) {
2458 srcpixel=srcbits;
2459 dstpixel=(LPWORD)dstbits;
2460 for (x = 0; x < width; x++) {
2461 PALETTEENTRY srcval;
2462 DWORD dstval;
2463 srcval=srccolors[*srcpixel++];
2464 dstval=((srcval.peRed << rShift) & rDst) |
2465 ((srcval.peGreen << gShift) & gDst) |
2466 ((srcval.peBlue << bShift) & bDst);
2467 *dstpixel++=dstval >> 16;
2469 srcbits -= bmpImage->bytes_per_line;
2470 dstbits += linebytes;
2472 } else {
2473 goto notsupported;
2475 break;
2477 default:
2478 notsupported:
2480 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2481 int rShift,gShift,bShift;
2482 WORD* dstpixel;
2484 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%x,%x,%x)\n",
2485 bmpImage->depth, bmpImage->red_mask,
2486 bmpImage->green_mask, bmpImage->blue_mask,
2487 rDst, gDst, bDst);
2489 /* Shift everything 16 bits left so that all shifts are >0,
2490 * even for BGR DIBs. Then a single >> 16 will bring everything
2491 * back into place.
2493 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2494 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2495 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2496 if (gDst==0x07e0) {
2497 /* 6 bits for the green */
2498 gShift++;
2500 rDst=rDst << 16;
2501 gDst=gDst << 16;
2502 bDst=bDst << 16;
2503 for (h = lines - 1; h >= 0; h--) {
2504 dstpixel=(LPWORD)dstbits;
2505 for (x = 0; x < width; x++) {
2506 COLORREF srcval;
2507 DWORD dstval;
2508 srcval=X11DRV_PALETTE_ToLogical(physDev, XGetPixel(bmpImage, x, h));
2509 dstval=((GetRValue(srcval) << rShift) & rDst) |
2510 ((GetGValue(srcval) << gShift) & gDst) |
2511 ((GetBValue(srcval) << bShift) & bDst);
2512 *dstpixel++=dstval >> 16;
2514 dstbits += linebytes;
2517 break;
2522 /***********************************************************************
2523 * X11DRV_DIB_SetImageBits_24
2525 * SetDIBits for a 24-bit deep DIB.
2527 static void X11DRV_DIB_SetImageBits_24( int lines, const BYTE *srcbits,
2528 DWORD srcwidth, DWORD dstwidth, int left,
2529 X11DRV_PDEVICE *physDev,
2530 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2531 XImage *bmpImage, DWORD linebytes )
2533 DWORD x;
2534 int h, width = min(srcwidth, dstwidth);
2535 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2537 if (lines < 0 )
2539 lines = -lines;
2540 srcbits = srcbits + linebytes * (lines - 1);
2541 linebytes = -linebytes;
2544 switch (bmpImage->depth)
2546 case 24:
2547 if (bmpImage->bits_per_pixel==24) {
2548 char* dstbits;
2550 srcbits=srcbits+left*3;
2551 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2553 if (bmpImage->green_mask!=0x00ff00 ||
2554 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2555 goto notsupported;
2556 } else if (rSrc==bmpImage->red_mask) {
2557 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2558 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2559 convs->Convert_888_asis
2560 (width,lines,
2561 srcbits,linebytes,
2562 dstbits,-bmpImage->bytes_per_line);
2563 } else {
2564 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2565 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2566 convs->Convert_888_reverse
2567 (width,lines,
2568 srcbits,linebytes,
2569 dstbits,-bmpImage->bytes_per_line);
2571 break;
2573 /* fall through */
2575 case 32:
2577 char* dstbits;
2579 srcbits=srcbits+left*3;
2580 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2582 if (bmpImage->green_mask!=0x00ff00 ||
2583 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2584 goto notsupported;
2585 } else if (rSrc==bmpImage->red_mask) {
2586 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2587 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2588 convs->Convert_888_to_0888_asis
2589 (width,lines,
2590 srcbits,linebytes,
2591 dstbits,-bmpImage->bytes_per_line);
2592 } else {
2593 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2594 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2595 convs->Convert_888_to_0888_reverse
2596 (width,lines,
2597 srcbits,linebytes,
2598 dstbits,-bmpImage->bytes_per_line);
2600 break;
2603 case 15:
2604 case 16:
2606 char* dstbits;
2608 srcbits=srcbits+left*3;
2609 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
2611 if (bmpImage->green_mask==0x03e0) {
2612 if ((rSrc==0xff0000 && bmpImage->red_mask==0x7f00) ||
2613 (bSrc==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2614 /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2615 /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2616 convs->Convert_888_to_555_asis
2617 (width,lines,
2618 srcbits,linebytes,
2619 dstbits,-bmpImage->bytes_per_line);
2620 } else if ((rSrc==0xff && bmpImage->red_mask==0x7f00) ||
2621 (bSrc==0xff && bmpImage->blue_mask==0x7f00)) {
2622 /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2623 /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2624 convs->Convert_888_to_555_reverse
2625 (width,lines,
2626 srcbits,linebytes,
2627 dstbits,-bmpImage->bytes_per_line);
2628 } else {
2629 goto notsupported;
2631 } else if (bmpImage->green_mask==0x07e0) {
2632 if ((rSrc==0xff0000 && bmpImage->red_mask==0xf800) ||
2633 (bSrc==0xff0000 && bmpImage->blue_mask==0xf800)) {
2634 /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2635 /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2636 convs->Convert_888_to_565_asis
2637 (width,lines,
2638 srcbits,linebytes,
2639 dstbits,-bmpImage->bytes_per_line);
2640 } else if ((rSrc==0xff && bmpImage->red_mask==0xf800) ||
2641 (bSrc==0xff && bmpImage->blue_mask==0xf800)) {
2642 /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2643 /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2644 convs->Convert_888_to_565_reverse
2645 (width,lines,
2646 srcbits,linebytes,
2647 dstbits,-bmpImage->bytes_per_line);
2648 } else {
2649 goto notsupported;
2651 } else {
2652 goto notsupported;
2655 break;
2657 default:
2658 notsupported:
2659 WARN("from 24 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2660 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2661 bmpImage->green_mask, bmpImage->blue_mask );
2662 /* fall through */
2663 case 1:
2664 case 4:
2665 case 8:
2667 /* ==== rgb 888 dib -> any bmp format ==== */
2668 const BYTE* srcbyte;
2670 /* Windows only supports one 24bpp DIB format: RGB888 */
2671 srcbits+=left*3;
2672 for (h = lines - 1; h >= 0; h--) {
2673 srcbyte = srcbits;
2674 for (x = left; x < width+left; x++) {
2675 XPutPixel(bmpImage, x, h,
2676 X11DRV_PALETTE_ToPhysical
2677 (physDev, RGB(srcbyte[2], srcbyte[1], srcbyte[0])));
2678 srcbyte+=3;
2680 srcbits += linebytes;
2683 break;
2688 /***********************************************************************
2689 * X11DRV_DIB_GetImageBits_24
2691 * GetDIBits for an 24-bit deep DIB.
2693 static void X11DRV_DIB_GetImageBits_24( X11DRV_PDEVICE *physDev, int lines, BYTE *dstbits,
2694 DWORD dstwidth, DWORD srcwidth,
2695 PALETTEENTRY *srccolors,
2696 DWORD rDst, DWORD gDst, DWORD bDst,
2697 XImage *bmpImage, DWORD linebytes )
2699 DWORD x;
2700 int h, width = min(srcwidth, dstwidth);
2701 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2703 if (lines < 0 )
2705 lines = -lines;
2706 dstbits = dstbits + ( linebytes * (lines-1) );
2707 linebytes = -linebytes;
2710 switch (bmpImage->depth)
2712 case 24:
2713 if (bmpImage->bits_per_pixel==24) {
2714 const char* srcbits;
2716 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2718 if (bmpImage->green_mask!=0x00ff00 ||
2719 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2720 goto notsupported;
2721 } else if (rDst==bmpImage->red_mask) {
2722 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2723 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2724 convs->Convert_888_asis
2725 (width,lines,
2726 srcbits,-bmpImage->bytes_per_line,
2727 dstbits,linebytes);
2728 } else {
2729 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2730 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2731 convs->Convert_888_reverse
2732 (width,lines,
2733 srcbits,-bmpImage->bytes_per_line,
2734 dstbits,linebytes);
2736 break;
2738 /* fall through */
2740 case 32:
2742 const char* srcbits;
2744 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2746 if (bmpImage->green_mask!=0x00ff00 ||
2747 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2748 goto notsupported;
2749 } else if (rDst==bmpImage->red_mask) {
2750 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2751 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2752 convs->Convert_0888_to_888_asis
2753 (width,lines,
2754 srcbits,-bmpImage->bytes_per_line,
2755 dstbits,linebytes);
2756 } else {
2757 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2758 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2759 convs->Convert_0888_to_888_reverse
2760 (width,lines,
2761 srcbits,-bmpImage->bytes_per_line,
2762 dstbits,linebytes);
2764 break;
2767 case 15:
2768 case 16:
2770 const char* srcbits;
2772 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2774 if (bmpImage->green_mask==0x03e0) {
2775 if ((rDst==0xff0000 && bmpImage->red_mask==0x7f00) ||
2776 (bDst==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2777 /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2778 /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2779 convs->Convert_555_to_888_asis
2780 (width,lines,
2781 srcbits,-bmpImage->bytes_per_line,
2782 dstbits,linebytes);
2783 } else if ((rDst==0xff && bmpImage->red_mask==0x7f00) ||
2784 (bDst==0xff && bmpImage->blue_mask==0x7f00)) {
2785 /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2786 /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2787 convs->Convert_555_to_888_reverse
2788 (width,lines,
2789 srcbits,-bmpImage->bytes_per_line,
2790 dstbits,linebytes);
2791 } else {
2792 goto notsupported;
2794 } else if (bmpImage->green_mask==0x07e0) {
2795 if ((rDst==0xff0000 && bmpImage->red_mask==0xf800) ||
2796 (bDst==0xff0000 && bmpImage->blue_mask==0xf800)) {
2797 /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2798 /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2799 convs->Convert_565_to_888_asis
2800 (width,lines,
2801 srcbits,-bmpImage->bytes_per_line,
2802 dstbits,linebytes);
2803 } else if ((rDst==0xff && bmpImage->red_mask==0xf800) ||
2804 (bDst==0xff && bmpImage->blue_mask==0xf800)) {
2805 /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2806 /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2807 convs->Convert_565_to_888_reverse
2808 (width,lines,
2809 srcbits,-bmpImage->bytes_per_line,
2810 dstbits,linebytes);
2811 } else {
2812 goto notsupported;
2814 } else {
2815 goto notsupported;
2818 break;
2820 case 1:
2821 case 4:
2822 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2823 && srccolors) {
2824 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2825 BYTE* dstbyte;
2827 /* Windows only supports one 24bpp DIB format: rgb 888 */
2828 for (h = lines - 1; h >= 0; h--) {
2829 dstbyte=dstbits;
2830 for (x = 0; x < width; x++) {
2831 PALETTEENTRY srcval;
2832 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2833 dstbyte[0]=srcval.peBlue;
2834 dstbyte[1]=srcval.peGreen;
2835 dstbyte[2]=srcval.peRed;
2836 dstbyte+=3;
2838 dstbits += linebytes;
2840 } else {
2841 goto notsupported;
2843 break;
2845 case 8:
2846 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2847 && srccolors) {
2848 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2849 const void* srcbits;
2850 const BYTE* srcpixel;
2851 BYTE* dstbyte;
2853 /* Windows only supports one 24bpp DIB format: rgb 888 */
2854 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2855 for (h = lines - 1; h >= 0; h--) {
2856 srcpixel=srcbits;
2857 dstbyte=dstbits;
2858 for (x = 0; x < width; x++ ) {
2859 PALETTEENTRY srcval;
2860 srcval=srccolors[*srcpixel++];
2861 dstbyte[0]=srcval.peBlue;
2862 dstbyte[1]=srcval.peGreen;
2863 dstbyte[2]=srcval.peRed;
2864 dstbyte+=3;
2866 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
2867 dstbits += linebytes;
2869 } else {
2870 goto notsupported;
2872 break;
2874 default:
2875 notsupported:
2877 /* ==== any bmp format -> 888 dib ==== */
2878 BYTE* dstbyte;
2880 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%x,%x,%x)\n",
2881 bmpImage->depth, bmpImage->red_mask,
2882 bmpImage->green_mask, bmpImage->blue_mask,
2883 rDst, gDst, bDst );
2885 /* Windows only supports one 24bpp DIB format: rgb 888 */
2886 for (h = lines - 1; h >= 0; h--) {
2887 dstbyte=dstbits;
2888 for (x = 0; x < width; x++) {
2889 COLORREF srcval=X11DRV_PALETTE_ToLogical
2890 (physDev, XGetPixel( bmpImage, x, h ));
2891 dstbyte[0]=GetBValue(srcval);
2892 dstbyte[1]=GetGValue(srcval);
2893 dstbyte[2]=GetRValue(srcval);
2894 dstbyte+=3;
2896 dstbits += linebytes;
2899 break;
2904 /***********************************************************************
2905 * X11DRV_DIB_SetImageBits_32
2907 * SetDIBits for a 32-bit deep DIB.
2909 static void X11DRV_DIB_SetImageBits_32(int lines, const BYTE *srcbits,
2910 DWORD srcwidth, DWORD dstwidth, int left,
2911 X11DRV_PDEVICE *physDev,
2912 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2913 XImage *bmpImage,
2914 DWORD linebytes)
2916 DWORD x;
2917 int h, width = min(srcwidth, dstwidth);
2918 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2920 if (lines < 0 )
2922 lines = -lines;
2923 srcbits = srcbits + ( linebytes * (lines-1) );
2924 linebytes = -linebytes;
2927 switch (bmpImage->depth)
2929 case 24:
2930 if (bmpImage->bits_per_pixel==24) {
2931 char* dstbits;
2933 srcbits=srcbits+left*4;
2934 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2936 if (rSrc==bmpImage->red_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->blue_mask) {
2937 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2938 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2939 convs->Convert_0888_to_888_asis
2940 (width,lines,
2941 srcbits,linebytes,
2942 dstbits,-bmpImage->bytes_per_line);
2943 } else if (bmpImage->green_mask!=0x00ff00 ||
2944 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2945 goto notsupported;
2946 /* the tests below assume sane bmpImage masks */
2947 } else if (rSrc==bmpImage->blue_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->red_mask) {
2948 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2949 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2950 convs->Convert_0888_to_888_reverse
2951 (width,lines,
2952 srcbits,linebytes,
2953 dstbits,-bmpImage->bytes_per_line);
2954 } else if (bmpImage->blue_mask==0xff) {
2955 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2956 convs->Convert_any0888_to_rgb888
2957 (width,lines,
2958 srcbits,linebytes,
2959 rSrc,gSrc,bSrc,
2960 dstbits,-bmpImage->bytes_per_line);
2961 } else {
2962 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2963 convs->Convert_any0888_to_bgr888
2964 (width,lines,
2965 srcbits,linebytes,
2966 rSrc,gSrc,bSrc,
2967 dstbits,-bmpImage->bytes_per_line);
2969 break;
2971 /* fall through */
2973 case 32:
2975 char* dstbits;
2977 srcbits=srcbits+left*4;
2978 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2980 if (gSrc==bmpImage->green_mask) {
2981 if (rSrc==bmpImage->red_mask && bSrc==bmpImage->blue_mask) {
2982 /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2983 /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2984 convs->Convert_0888_asis
2985 (width,lines,
2986 srcbits,linebytes,
2987 dstbits,-bmpImage->bytes_per_line);
2988 } else if (bmpImage->green_mask!=0x00ff00 ||
2989 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2990 goto notsupported;
2991 /* the tests below assume sane bmpImage masks */
2992 } else if (rSrc==bmpImage->blue_mask && bSrc==bmpImage->red_mask) {
2993 /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2994 /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2995 convs->Convert_0888_reverse
2996 (width,lines,
2997 srcbits,linebytes,
2998 dstbits,-bmpImage->bytes_per_line);
2999 } else {
3000 /* ==== any 0888 dib -> any 0888 bmp ==== */
3001 convs->Convert_0888_any
3002 (width,lines,
3003 srcbits,linebytes,
3004 rSrc,gSrc,bSrc,
3005 dstbits,-bmpImage->bytes_per_line,
3006 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3008 } else if (bmpImage->green_mask!=0x00ff00 ||
3009 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3010 goto notsupported;
3011 /* the tests below assume sane bmpImage masks */
3012 } else {
3013 /* ==== any 0888 dib -> any 0888 bmp ==== */
3014 convs->Convert_0888_any
3015 (width,lines,
3016 srcbits,linebytes,
3017 rSrc,gSrc,bSrc,
3018 dstbits,-bmpImage->bytes_per_line,
3019 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3022 break;
3024 case 15:
3025 case 16:
3027 char* dstbits;
3029 srcbits=srcbits+left*4;
3030 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
3032 if (rSrc==0xff0000 && gSrc==0x00ff00 && bSrc==0x0000ff) {
3033 if (bmpImage->green_mask==0x03e0) {
3034 if (bmpImage->red_mask==0x7f00) {
3035 /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
3036 convs->Convert_0888_to_555_asis
3037 (width,lines,
3038 srcbits,linebytes,
3039 dstbits,-bmpImage->bytes_per_line);
3040 } else if (bmpImage->blue_mask==0x7f00) {
3041 /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
3042 convs->Convert_0888_to_555_reverse
3043 (width,lines,
3044 srcbits,linebytes,
3045 dstbits,-bmpImage->bytes_per_line);
3046 } else {
3047 goto notsupported;
3049 } else if (bmpImage->green_mask==0x07e0) {
3050 if (bmpImage->red_mask==0xf800) {
3051 /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
3052 convs->Convert_0888_to_565_asis
3053 (width,lines,
3054 srcbits,linebytes,
3055 dstbits,-bmpImage->bytes_per_line);
3056 } else if (bmpImage->blue_mask==0xf800) {
3057 /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
3058 convs->Convert_0888_to_565_reverse
3059 (width,lines,
3060 srcbits,linebytes,
3061 dstbits,-bmpImage->bytes_per_line);
3062 } else {
3063 goto notsupported;
3065 } else {
3066 goto notsupported;
3068 } else if (rSrc==0x0000ff && gSrc==0x00ff00 && bSrc==0xff0000) {
3069 if (bmpImage->green_mask==0x03e0) {
3070 if (bmpImage->blue_mask==0x7f00) {
3071 /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
3072 convs->Convert_0888_to_555_asis
3073 (width,lines,
3074 srcbits,linebytes,
3075 dstbits,-bmpImage->bytes_per_line);
3076 } else if (bmpImage->red_mask==0x7f00) {
3077 /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
3078 convs->Convert_0888_to_555_reverse
3079 (width,lines,
3080 srcbits,linebytes,
3081 dstbits,-bmpImage->bytes_per_line);
3082 } else {
3083 goto notsupported;
3085 } else if (bmpImage->green_mask==0x07e0) {
3086 if (bmpImage->blue_mask==0xf800) {
3087 /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
3088 convs->Convert_0888_to_565_asis
3089 (width,lines,
3090 srcbits,linebytes,
3091 dstbits,-bmpImage->bytes_per_line);
3092 } else if (bmpImage->red_mask==0xf800) {
3093 /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
3094 convs->Convert_0888_to_565_reverse
3095 (width,lines,
3096 srcbits,linebytes,
3097 dstbits,-bmpImage->bytes_per_line);
3098 } else {
3099 goto notsupported;
3101 } else {
3102 goto notsupported;
3104 } else {
3105 if (bmpImage->green_mask==0x03e0 &&
3106 (bmpImage->red_mask==0x7f00 ||
3107 bmpImage->blue_mask==0x7f00)) {
3108 /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
3109 convs->Convert_any0888_to_5x5
3110 (width,lines,
3111 srcbits,linebytes,
3112 rSrc,gSrc,bSrc,
3113 dstbits,-bmpImage->bytes_per_line,
3114 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3115 } else if (bmpImage->green_mask==0x07e0 &&
3116 (bmpImage->red_mask==0xf800 ||
3117 bmpImage->blue_mask==0xf800)) {
3118 /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
3119 convs->Convert_any0888_to_5x5
3120 (width,lines,
3121 srcbits,linebytes,
3122 rSrc,gSrc,bSrc,
3123 dstbits,-bmpImage->bytes_per_line,
3124 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3125 } else {
3126 goto notsupported;
3130 break;
3132 default:
3133 notsupported:
3134 WARN("from 32 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
3135 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
3136 bmpImage->green_mask, bmpImage->blue_mask );
3137 /* fall through */
3138 case 1:
3139 case 4:
3140 case 8:
3142 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
3143 const DWORD* srcpixel;
3144 int rShift,gShift,bShift;
3146 rShift=X11DRV_DIB_MaskToShift(rSrc);
3147 gShift=X11DRV_DIB_MaskToShift(gSrc);
3148 bShift=X11DRV_DIB_MaskToShift(bSrc);
3149 srcbits+=left*4;
3150 for (h = lines - 1; h >= 0; h--) {
3151 srcpixel=(const DWORD*)srcbits;
3152 for (x = left; x < width+left; x++) {
3153 DWORD srcvalue;
3154 BYTE red,green,blue;
3155 srcvalue=*srcpixel++;
3156 red= (srcvalue >> rShift) & 0xff;
3157 green=(srcvalue >> gShift) & 0xff;
3158 blue= (srcvalue >> bShift) & 0xff;
3159 XPutPixel(bmpImage, x, h, X11DRV_PALETTE_ToPhysical
3160 (physDev, RGB(red,green,blue)));
3162 srcbits += linebytes;
3165 break;
3170 /***********************************************************************
3171 * X11DRV_DIB_GetImageBits_32
3173 * GetDIBits for an 32-bit deep DIB.
3175 static void X11DRV_DIB_GetImageBits_32( X11DRV_PDEVICE *physDev, int lines, BYTE *dstbits,
3176 DWORD dstwidth, DWORD srcwidth,
3177 PALETTEENTRY *srccolors,
3178 DWORD rDst, DWORD gDst, DWORD bDst,
3179 XImage *bmpImage, int linebytes )
3181 DWORD x;
3182 int h, width = min(srcwidth, dstwidth);
3183 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
3185 if (lines < 0 )
3187 lines = -lines;
3188 dstbits = dstbits + ( linebytes * (lines-1) );
3189 linebytes = -linebytes;
3192 switch (bmpImage->depth)
3194 case 24:
3195 if (bmpImage->bits_per_pixel==24) {
3196 const void* srcbits;
3198 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3200 if (rDst==bmpImage->red_mask && gDst==bmpImage->green_mask && bDst==bmpImage->blue_mask) {
3201 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
3202 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
3203 convs->Convert_888_to_0888_asis
3204 (width,lines,
3205 srcbits,-bmpImage->bytes_per_line,
3206 dstbits,linebytes);
3207 } else if (bmpImage->green_mask!=0x00ff00 ||
3208 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3209 goto notsupported;
3210 /* the tests below assume sane bmpImage masks */
3211 } else if (rDst==bmpImage->blue_mask && gDst==bmpImage->green_mask && bDst==bmpImage->red_mask) {
3212 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
3213 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
3214 convs->Convert_888_to_0888_reverse
3215 (width,lines,
3216 srcbits,-bmpImage->bytes_per_line,
3217 dstbits,linebytes);
3218 } else if (bmpImage->blue_mask==0xff) {
3219 /* ==== rgb 888 bmp -> any 0888 dib ==== */
3220 convs->Convert_rgb888_to_any0888
3221 (width,lines,
3222 srcbits,-bmpImage->bytes_per_line,
3223 dstbits,linebytes,
3224 rDst,gDst,bDst);
3225 } else {
3226 /* ==== bgr 888 bmp -> any 0888 dib ==== */
3227 convs->Convert_bgr888_to_any0888
3228 (width,lines,
3229 srcbits,-bmpImage->bytes_per_line,
3230 dstbits,linebytes,
3231 rDst,gDst,bDst);
3233 break;
3235 /* fall through */
3237 case 32:
3239 const char* srcbits;
3241 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3243 if (gDst==bmpImage->green_mask) {
3244 if (rDst==bmpImage->red_mask && bDst==bmpImage->blue_mask) {
3245 /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
3246 /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
3247 convs->Convert_0888_asis
3248 (width,lines,
3249 srcbits,-bmpImage->bytes_per_line,
3250 dstbits,linebytes);
3251 } else if (bmpImage->green_mask!=0x00ff00 ||
3252 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3253 goto notsupported;
3254 /* the tests below assume sane bmpImage masks */
3255 } else if (rDst==bmpImage->blue_mask && bDst==bmpImage->red_mask) {
3256 /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
3257 /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
3258 convs->Convert_0888_reverse
3259 (width,lines,
3260 srcbits,-bmpImage->bytes_per_line,
3261 dstbits,linebytes);
3262 } else {
3263 /* ==== any 0888 bmp -> any 0888 dib ==== */
3264 convs->Convert_0888_any
3265 (width,lines,
3266 srcbits,-bmpImage->bytes_per_line,
3267 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3268 dstbits,linebytes,
3269 rDst,gDst,bDst);
3271 } else if (bmpImage->green_mask!=0x00ff00 ||
3272 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3273 goto notsupported;
3274 /* the tests below assume sane bmpImage masks */
3275 } else {
3276 /* ==== any 0888 bmp -> any 0888 dib ==== */
3277 convs->Convert_0888_any
3278 (width,lines,
3279 srcbits,-bmpImage->bytes_per_line,
3280 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3281 dstbits,linebytes,
3282 rDst,gDst,bDst);
3285 break;
3287 case 15:
3288 case 16:
3290 const char* srcbits;
3292 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3294 if (rDst==0xff0000 && gDst==0x00ff00 && bDst==0x0000ff) {
3295 if (bmpImage->green_mask==0x03e0) {
3296 if (bmpImage->red_mask==0x7f00) {
3297 /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
3298 convs->Convert_555_to_0888_asis
3299 (width,lines,
3300 srcbits,-bmpImage->bytes_per_line,
3301 dstbits,linebytes);
3302 } else if (bmpImage->blue_mask==0x7f00) {
3303 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
3304 convs->Convert_555_to_0888_reverse
3305 (width,lines,
3306 srcbits,-bmpImage->bytes_per_line,
3307 dstbits,linebytes);
3308 } else {
3309 goto notsupported;
3311 } else if (bmpImage->green_mask==0x07e0) {
3312 if (bmpImage->red_mask==0xf800) {
3313 /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
3314 convs->Convert_565_to_0888_asis
3315 (width,lines,
3316 srcbits,-bmpImage->bytes_per_line,
3317 dstbits,linebytes);
3318 } else if (bmpImage->blue_mask==0xf800) {
3319 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
3320 convs->Convert_565_to_0888_reverse
3321 (width,lines,
3322 srcbits,-bmpImage->bytes_per_line,
3323 dstbits,linebytes);
3324 } else {
3325 goto notsupported;
3327 } else {
3328 goto notsupported;
3330 } else if (rDst==0x0000ff && gDst==0x00ff00 && bDst==0xff0000) {
3331 if (bmpImage->green_mask==0x03e0) {
3332 if (bmpImage->blue_mask==0x7f00) {
3333 /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
3334 convs->Convert_555_to_0888_asis
3335 (width,lines,
3336 srcbits,-bmpImage->bytes_per_line,
3337 dstbits,linebytes);
3338 } else if (bmpImage->red_mask==0x7f00) {
3339 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
3340 convs->Convert_555_to_0888_reverse
3341 (width,lines,
3342 srcbits,-bmpImage->bytes_per_line,
3343 dstbits,linebytes);
3344 } else {
3345 goto notsupported;
3347 } else if (bmpImage->green_mask==0x07e0) {
3348 if (bmpImage->blue_mask==0xf800) {
3349 /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3350 convs->Convert_565_to_0888_asis
3351 (width,lines,
3352 srcbits,-bmpImage->bytes_per_line,
3353 dstbits,linebytes);
3354 } else if (bmpImage->red_mask==0xf800) {
3355 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3356 convs->Convert_565_to_0888_reverse
3357 (width,lines,
3358 srcbits,-bmpImage->bytes_per_line,
3359 dstbits,linebytes);
3360 } else {
3361 goto notsupported;
3363 } else {
3364 goto notsupported;
3366 } else {
3367 if (bmpImage->green_mask==0x03e0 &&
3368 (bmpImage->red_mask==0x7f00 ||
3369 bmpImage->blue_mask==0x7f00)) {
3370 /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3371 convs->Convert_5x5_to_any0888
3372 (width,lines,
3373 srcbits,-bmpImage->bytes_per_line,
3374 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3375 dstbits,linebytes,
3376 rDst,gDst,bDst);
3377 } else if (bmpImage->green_mask==0x07e0 &&
3378 (bmpImage->red_mask==0xf800 ||
3379 bmpImage->blue_mask==0xf800)) {
3380 /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3381 convs->Convert_5x5_to_any0888
3382 (width,lines,
3383 srcbits,-bmpImage->bytes_per_line,
3384 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3385 dstbits,linebytes,
3386 rDst,gDst,bDst);
3387 } else {
3388 goto notsupported;
3392 break;
3394 case 1:
3395 case 4:
3396 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3397 && srccolors) {
3398 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3399 int rShift,gShift,bShift;
3400 DWORD* dstpixel;
3402 rShift=X11DRV_DIB_MaskToShift(rDst);
3403 gShift=X11DRV_DIB_MaskToShift(gDst);
3404 bShift=X11DRV_DIB_MaskToShift(bDst);
3405 for (h = lines - 1; h >= 0; h--) {
3406 dstpixel=(DWORD*)dstbits;
3407 for (x = 0; x < width; x++) {
3408 PALETTEENTRY srcval;
3409 srcval = srccolors[XGetPixel(bmpImage, x, h)];
3410 *dstpixel++=(srcval.peRed << rShift) |
3411 (srcval.peGreen << gShift) |
3412 (srcval.peBlue << bShift);
3414 dstbits += linebytes;
3416 } else {
3417 goto notsupported;
3419 break;
3421 case 8:
3422 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3423 && srccolors) {
3424 /* ==== pal 8 bmp -> any 0888 dib ==== */
3425 int rShift,gShift,bShift;
3426 const void* srcbits;
3427 const BYTE* srcpixel;
3428 DWORD* dstpixel;
3430 rShift=X11DRV_DIB_MaskToShift(rDst);
3431 gShift=X11DRV_DIB_MaskToShift(gDst);
3432 bShift=X11DRV_DIB_MaskToShift(bDst);
3433 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3434 for (h = lines - 1; h >= 0; h--) {
3435 srcpixel=srcbits;
3436 dstpixel=(DWORD*)dstbits;
3437 for (x = 0; x < width; x++) {
3438 PALETTEENTRY srcval;
3439 srcval=srccolors[*srcpixel++];
3440 *dstpixel++=(srcval.peRed << rShift) |
3441 (srcval.peGreen << gShift) |
3442 (srcval.peBlue << bShift);
3444 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
3445 dstbits += linebytes;
3447 } else {
3448 goto notsupported;
3450 break;
3452 default:
3453 notsupported:
3455 /* ==== any bmp format -> any 0888 dib ==== */
3456 int rShift,gShift,bShift;
3457 DWORD* dstpixel;
3459 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%x,%x,%x)\n",
3460 bmpImage->depth, bmpImage->red_mask,
3461 bmpImage->green_mask, bmpImage->blue_mask,
3462 rDst,gDst,bDst);
3464 rShift=X11DRV_DIB_MaskToShift(rDst);
3465 gShift=X11DRV_DIB_MaskToShift(gDst);
3466 bShift=X11DRV_DIB_MaskToShift(bDst);
3467 for (h = lines - 1; h >= 0; h--) {
3468 dstpixel=(DWORD*)dstbits;
3469 for (x = 0; x < width; x++) {
3470 COLORREF srcval;
3471 srcval=X11DRV_PALETTE_ToLogical(physDev, XGetPixel(bmpImage, x, h));
3472 *dstpixel++=(GetRValue(srcval) << rShift) |
3473 (GetGValue(srcval) << gShift) |
3474 (GetBValue(srcval) << bShift);
3476 dstbits += linebytes;
3479 break;
3483 static int XGetSubImageErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
3485 return (event->request_code == X_GetImage && event->error_code == BadMatch);
3488 /***********************************************************************
3489 * X11DRV_DIB_SetImageBits_GetSubImage
3491 * Helper for X11DRV_DIB_SetImageBits
3493 static void X11DRV_DIB_SetImageBits_GetSubImage(
3494 const X11DRV_DIB_IMAGEBITS_DESCR *descr, XImage *bmpImage)
3496 /* compressed bitmaps may contain gaps in them. So make a copy
3497 * of the existing pixels first */
3498 RECT bmprc, rc;
3500 SetRect( &bmprc, descr->xDest, descr->yDest,
3501 descr->xDest + descr->width , descr->yDest + descr->height );
3502 GetRgnBox( descr->physDev->region, &rc );
3503 /* convert from dc to drawable origin */
3504 OffsetRect( &rc, descr->physDev->dc_rect.left, descr->physDev->dc_rect.top);
3505 /* clip visible rect with bitmap */
3506 if( IntersectRect( &rc, &rc, &bmprc))
3508 X11DRV_expect_error( gdi_display, XGetSubImageErrorHandler, NULL );
3509 XGetSubImage( gdi_display, descr->drawable, rc.left, rc.top,
3510 rc.right - rc.left, rc.bottom - rc.top, AllPlanes,
3511 ZPixmap, bmpImage,
3512 descr->xSrc + rc.left - bmprc.left,
3513 descr->ySrc + rc.top - bmprc.top);
3514 X11DRV_check_error();
3518 /***********************************************************************
3519 * X11DRV_DIB_SetImageBits
3521 * Transfer the bits to an X image.
3522 * Helper function for SetDIBits() and SetDIBitsToDevice().
3524 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3526 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3527 void *old_data = NULL;
3528 XImage *bmpImage;
3530 wine_tsx11_lock();
3531 if (descr->image)
3532 bmpImage = descr->image;
3533 else {
3534 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3535 descr->infoWidth, lines, 32, 0 );
3536 bmpImage->data = HeapAlloc( GetProcessHeap(), 0, lines * bmpImage->bytes_per_line );
3537 if(bmpImage->data == NULL) {
3538 ERR("Out of memory!\n");
3539 XDestroyImage( bmpImage );
3540 wine_tsx11_unlock();
3541 return 0;
3543 if (descr->shifts)
3545 bmpImage->red_mask = descr->shifts->physicalRed.max << descr->shifts->physicalRed.shift;
3546 bmpImage->green_mask = descr->shifts->physicalGreen.max << descr->shifts->physicalGreen.shift;
3547 bmpImage->blue_mask = descr->shifts->physicalBlue.max << descr->shifts->physicalBlue.shift;
3550 wine_tsx11_unlock();
3552 TRACE("Dib: depth=%d r=%x g=%x b=%x\n",
3553 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3554 TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3555 bmpImage->depth,bmpImage->bits_per_pixel,
3556 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3558 #ifdef HAVE_LIBXXSHM
3559 if (descr->shm_mode == X11DRV_SHM_PIXMAP
3560 && descr->xSrc == 0 && descr->ySrc == 0
3561 && descr->xDest == 0 && descr->yDest == 0)
3563 TRACE("Using the shared pixmap data.\n");
3565 wine_tsx11_lock();
3566 XSync( gdi_display, False );
3567 wine_tsx11_unlock();
3569 old_data = descr->image->data;
3570 descr->image->data = descr->physBitmap->shminfo.shmaddr;
3572 #endif
3574 /* Transfer the pixels */
3575 __TRY
3577 switch(descr->infoBpp)
3579 case 1:
3580 X11DRV_DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
3581 descr->width, descr->xSrc, (int *)(descr->colorMap),
3582 bmpImage, descr->dibpitch );
3583 break;
3584 case 4:
3585 if (descr->compression) {
3586 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3587 X11DRV_DIB_SetImageBits_RLE4( descr->lines, descr->bits,
3588 descr->infoWidth, descr->width,
3589 descr->xSrc, (int *)(descr->colorMap),
3590 bmpImage );
3591 } else
3592 X11DRV_DIB_SetImageBits_4( descr->lines, descr->bits,
3593 descr->infoWidth, descr->width,
3594 descr->xSrc, (int*)(descr->colorMap),
3595 bmpImage, descr->dibpitch );
3596 break;
3597 case 8:
3598 if (descr->compression) {
3599 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3600 X11DRV_DIB_SetImageBits_RLE8( descr->lines, descr->bits,
3601 descr->infoWidth, descr->width,
3602 descr->xSrc, (int *)(descr->colorMap),
3603 bmpImage );
3604 } else
3605 X11DRV_DIB_SetImageBits_8( descr->lines, descr->bits,
3606 descr->infoWidth, descr->width,
3607 descr->xSrc, (int *)(descr->colorMap),
3608 bmpImage, descr->dibpitch );
3609 break;
3610 case 15:
3611 case 16:
3612 X11DRV_DIB_SetImageBits_16( descr->lines, descr->bits,
3613 descr->infoWidth, descr->width,
3614 descr->xSrc, descr->physDev,
3615 descr->rMask, descr->gMask, descr->bMask,
3616 bmpImage, descr->dibpitch);
3617 break;
3618 case 24:
3619 X11DRV_DIB_SetImageBits_24( descr->lines, descr->bits,
3620 descr->infoWidth, descr->width,
3621 descr->xSrc, descr->physDev,
3622 descr->rMask, descr->gMask, descr->bMask,
3623 bmpImage, descr->dibpitch);
3624 break;
3625 case 32:
3626 X11DRV_DIB_SetImageBits_32( descr->lines, descr->bits,
3627 descr->infoWidth, descr->width,
3628 descr->xSrc, descr->physDev,
3629 descr->rMask, descr->gMask, descr->bMask,
3630 bmpImage, descr->dibpitch);
3631 break;
3632 default:
3633 WARN("(%d): Invalid depth\n", descr->infoBpp );
3634 break;
3637 __EXCEPT_PAGE_FAULT
3639 WARN( "invalid bits pointer %p\n", descr->bits );
3640 lines = 0;
3642 __ENDTRY
3644 TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3645 descr->drawable, descr->gc, bmpImage,
3646 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3647 descr->width, descr->height);
3649 wine_tsx11_lock();
3650 if (lines)
3652 #ifdef HAVE_LIBXXSHM
3653 if (descr->shm_mode == X11DRV_SHM_PIXMAP
3654 && descr->xSrc == 0 && descr->ySrc == 0
3655 && descr->xDest == 0 && descr->yDest == 0)
3657 XSync( gdi_display, False );
3659 else if (descr->shm_mode == X11DRV_SHM_IMAGE && descr->image)
3661 XShmPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3662 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3663 descr->width, descr->height, FALSE );
3664 XSync( gdi_display, 0 );
3666 else
3667 #endif
3669 XPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3670 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3671 descr->width, descr->height );
3675 if (old_data) descr->image->data = old_data;
3677 if (!descr->image) X11DRV_DIB_DestroyXImage( bmpImage );
3678 wine_tsx11_unlock();
3679 return lines;
3682 /***********************************************************************
3683 * X11DRV_DIB_GetImageBits
3685 * Transfer the bits from an X image.
3687 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3689 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3690 void *old_data = NULL;
3691 XImage *bmpImage;
3693 wine_tsx11_lock();
3694 if (descr->image)
3695 bmpImage = descr->image;
3696 else {
3697 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3698 descr->infoWidth, lines, 32, 0 );
3699 bmpImage->data = HeapAlloc( GetProcessHeap(), 0, lines * bmpImage->bytes_per_line );
3700 if(bmpImage->data == NULL) {
3701 ERR("Out of memory!\n");
3702 XDestroyImage( bmpImage );
3703 wine_tsx11_unlock();
3704 return 0;
3706 if (descr->shifts)
3708 bmpImage->red_mask = descr->shifts->physicalRed.max << descr->shifts->physicalRed.shift;
3709 bmpImage->green_mask = descr->shifts->physicalGreen.max << descr->shifts->physicalGreen.shift;
3710 bmpImage->blue_mask = descr->shifts->physicalBlue.max << descr->shifts->physicalBlue.shift;
3714 #ifdef HAVE_LIBXXSHM
3716 /* We must not call XShmGetImage() with a bitmap which is bigger than the available area.
3717 If we do, XShmGetImage() will fail (X exception), as it checks for this internally. */
3718 if (descr->shm_mode == X11DRV_SHM_PIXMAP && descr->image
3719 && descr->xSrc == 0 && descr->ySrc == 0
3720 && descr->xDest == 0 && descr->yDest == 0
3721 && bmpImage->width <= (descr->width - descr->xSrc)
3722 && bmpImage->height <= (descr->height - descr->ySrc))
3724 XSync( gdi_display, False );
3725 old_data = bmpImage->data;
3726 bmpImage->data = descr->physBitmap->shminfo.shmaddr;
3727 TRACE("Using shared pixmap data.\n");
3729 else if (descr->shm_mode == X11DRV_SHM_IMAGE && descr->image
3730 && bmpImage->width <= (descr->width - descr->xSrc)
3731 && bmpImage->height <= (descr->height - descr->ySrc))
3733 int saveRed, saveGreen, saveBlue;
3735 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3736 gdi_display, descr->drawable, bmpImage,
3737 descr->xSrc, descr->ySrc, AllPlanes);
3739 /* We must save and restore the bmpImage's masks in order
3740 * to preserve them across the call to XShmGetImage, which
3741 * decides to eliminate them since it doesn't happen to know
3742 * what the format of the image is supposed to be, even though
3743 * we do. */
3744 saveRed = bmpImage->red_mask;
3745 saveBlue= bmpImage->blue_mask;
3746 saveGreen = bmpImage->green_mask;
3748 XShmGetImage( gdi_display, descr->drawable, bmpImage,
3749 descr->xSrc, descr->ySrc, AllPlanes);
3751 bmpImage->red_mask = saveRed;
3752 bmpImage->blue_mask = saveBlue;
3753 bmpImage->green_mask = saveGreen;
3755 else
3756 #endif /* HAVE_LIBXXSHM */
3758 TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3759 gdi_display, descr->drawable, descr->xSrc, descr->ySrc, descr->width,
3760 lines, AllPlanes, ZPixmap, bmpImage, descr->xDest, descr->yDest);
3761 XGetSubImage( gdi_display, descr->drawable, descr->xSrc, descr->ySrc,
3762 descr->width, lines, AllPlanes, ZPixmap,
3763 bmpImage, descr->xDest, descr->yDest );
3765 wine_tsx11_unlock();
3767 TRACE("Dib: depth=%2d r=%x g=%x b=%x\n",
3768 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3769 TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3770 bmpImage->depth,bmpImage->bits_per_pixel,
3771 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3772 /* Transfer the pixels */
3773 switch(descr->infoBpp)
3775 case 1:
3776 X11DRV_DIB_GetImageBits_1( descr->lines,(LPVOID)descr->bits,
3777 descr->infoWidth, descr->width,
3778 descr->colorMap, descr->palentry,
3779 bmpImage, descr->dibpitch );
3780 break;
3782 case 4:
3783 if (descr->compression) {
3784 FIXME("Compression not yet supported!\n");
3785 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 4 ) * abs(descr->lines))
3786 break;
3788 X11DRV_DIB_GetImageBits_4( descr->lines,(LPVOID)descr->bits,
3789 descr->infoWidth, descr->width,
3790 descr->colorMap, descr->palentry,
3791 bmpImage, descr->dibpitch );
3792 break;
3793 case 8:
3794 if (descr->compression) {
3795 FIXME("Compression not yet supported!\n");
3796 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 8 ) * abs(descr->lines))
3797 break;
3799 X11DRV_DIB_GetImageBits_8( descr->lines, (LPVOID)descr->bits,
3800 descr->infoWidth, descr->width,
3801 descr->colorMap, descr->palentry,
3802 bmpImage, descr->dibpitch );
3803 break;
3804 case 15:
3805 case 16:
3806 X11DRV_DIB_GetImageBits_16( descr->physDev, descr->lines, (LPVOID)descr->bits,
3807 descr->infoWidth,descr->width,
3808 descr->palentry,
3809 descr->rMask, descr->gMask, descr->bMask,
3810 bmpImage, descr->dibpitch );
3811 break;
3813 case 24:
3814 X11DRV_DIB_GetImageBits_24( descr->physDev, descr->lines, (LPVOID)descr->bits,
3815 descr->infoWidth,descr->width,
3816 descr->palentry,
3817 descr->rMask, descr->gMask, descr->bMask,
3818 bmpImage, descr->dibpitch);
3819 break;
3821 case 32:
3822 X11DRV_DIB_GetImageBits_32( descr->physDev, descr->lines, (LPVOID)descr->bits,
3823 descr->infoWidth, descr->width,
3824 descr->palentry,
3825 descr->rMask, descr->gMask, descr->bMask,
3826 bmpImage, descr->dibpitch);
3827 break;
3829 default:
3830 WARN("(%d): Invalid depth\n", descr->infoBpp );
3831 break;
3834 if (old_data) bmpImage->data = old_data;
3835 if (!descr->image) X11DRV_DIB_DestroyXImage( bmpImage );
3836 return lines;
3839 /*************************************************************************
3840 * X11DRV_SetDIBitsToDevice
3843 INT X11DRV_SetDIBitsToDevice( PHYSDEV dev, INT xDest, INT yDest, DWORD cx, DWORD cy,
3844 INT xSrc, INT ySrc, UINT startscan, UINT lines, LPCVOID bits,
3845 const BITMAPINFO *info, UINT coloruse )
3847 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
3848 X11DRV_DIB_IMAGEBITS_DESCR descr;
3849 INT result;
3850 LONG width, height;
3851 BOOL top_down;
3852 POINT pt;
3853 int rop = X11DRV_XROPfunction[GetROP2(dev->hdc) - 1];
3855 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3856 &descr.infoBpp, &descr.compression ) == -1)
3857 return 0;
3859 top_down = (height < 0);
3860 if (top_down) height = -height;
3862 pt.x = xDest;
3863 pt.y = yDest;
3864 LPtoDP(dev->hdc, &pt, 1);
3866 if (!lines || (startscan >= height)) return 0;
3867 if (!top_down && startscan + lines > height) lines = height - startscan;
3869 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3870 * and clamp all values to fit inside [startscan,startscan+lines]
3872 if (ySrc + cy <= startscan + lines)
3874 UINT y = startscan + lines - (ySrc + cy);
3875 if (ySrc < startscan) cy -= (startscan - ySrc);
3876 if (!top_down)
3878 /* avoid getting unnecessary lines */
3879 ySrc = 0;
3880 if (y >= lines) return 0;
3881 lines -= y;
3883 else
3885 if (y >= lines) return lines;
3886 ySrc = y; /* need to get all lines in top down mode */
3889 else
3891 if (ySrc >= startscan + lines) return lines;
3892 pt.y += ySrc + cy - (startscan + lines);
3893 cy = startscan + lines - ySrc;
3894 ySrc = 0;
3895 if (cy > lines) cy = lines;
3897 if (xSrc >= width) return lines;
3898 if (xSrc + cx >= width) cx = width - xSrc;
3899 if (!cx || !cy) return lines;
3901 /* Update the pixmap from the DIB section */
3902 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
3904 X11DRV_SetupGCForText( physDev ); /* To have the correct colors */
3905 wine_tsx11_lock();
3906 XSetFunction(gdi_display, physDev->gc, rop);
3907 wine_tsx11_unlock();
3909 switch (descr.infoBpp)
3911 case 1:
3912 case 4:
3913 case 8:
3914 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3915 physDev, coloruse,
3916 physDev->depth, info, &descr.nColorMap );
3917 if (!descr.colorMap) return 0;
3918 descr.rMask = descr.gMask = descr.bMask = 0;
3919 break;
3920 case 15:
3921 case 16:
3922 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3923 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3924 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3925 descr.colorMap = 0;
3926 break;
3928 case 24:
3929 case 32:
3930 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3931 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3932 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3933 descr.colorMap = 0;
3934 break;
3937 descr.physDev = physDev;
3938 descr.bits = bits;
3939 descr.image = NULL;
3940 descr.palentry = NULL;
3941 descr.lines = top_down ? -lines : lines;
3942 descr.infoWidth = width;
3943 descr.depth = physDev->depth;
3944 descr.shifts = physDev->color_shifts;
3945 descr.drawable = physDev->drawable;
3946 descr.gc = physDev->gc;
3947 descr.xSrc = xSrc;
3948 descr.ySrc = ySrc;
3949 descr.xDest = physDev->dc_rect.left + pt.x;
3950 descr.yDest = physDev->dc_rect.top + pt.y;
3951 descr.width = cx;
3952 descr.height = cy;
3953 descr.shm_mode = X11DRV_SHM_NONE;
3954 descr.dibpitch = ((width * descr.infoBpp + 31) &~31) / 8;
3955 descr.physBitmap = NULL;
3957 result = X11DRV_DIB_SetImageBits( &descr );
3959 if (descr.infoBpp <= 8)
3960 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3962 /* Update the DIBSection of the pixmap */
3963 X11DRV_UnlockDIBSection(physDev, TRUE);
3965 return result;
3968 /***********************************************************************
3969 * SetDIBits (X11DRV.@)
3971 INT X11DRV_SetDIBits( PHYSDEV dev, HBITMAP hbitmap, UINT startscan,
3972 UINT lines, LPCVOID bits, const BITMAPINFO *info, UINT coloruse )
3974 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
3975 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3976 X11DRV_DIB_IMAGEBITS_DESCR descr;
3977 DIBSECTION ds;
3978 LONG width, height, tmpheight;
3979 INT result;
3981 descr.physDev = physDev;
3983 if (!physBitmap) return 0;
3985 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3986 &descr.infoBpp, &descr.compression ) == -1)
3987 return 0;
3989 tmpheight = height;
3990 if (height < 0) height = -height;
3991 if (!lines || (startscan >= height))
3992 return 0;
3994 if (!GetObjectW( hbitmap, sizeof(ds), &ds )) return 0;
3996 if (startscan + lines > height) lines = height - startscan;
3998 switch (descr.infoBpp)
4000 case 1:
4001 case 4:
4002 case 8:
4003 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
4004 descr.physDev, coloruse,
4005 physBitmap->pixmap_depth,
4006 info, &descr.nColorMap );
4007 if (!descr.colorMap) return 0;
4008 descr.rMask = descr.gMask = descr.bMask = 0;
4009 break;
4010 case 15:
4011 case 16:
4012 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4013 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4014 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4015 descr.colorMap = 0;
4016 break;
4018 case 24:
4019 case 32:
4020 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4021 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4022 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4023 descr.colorMap = 0;
4024 break;
4026 default: break;
4029 descr.bits = bits;
4030 descr.image = NULL;
4031 descr.palentry = NULL;
4032 descr.infoWidth = width;
4033 descr.lines = tmpheight >= 0 ? lines : -lines;
4034 descr.depth = physBitmap->pixmap_depth;
4035 descr.shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
4036 descr.drawable = physBitmap->pixmap;
4037 descr.gc = get_bitmap_gc(physBitmap->pixmap_depth);
4038 descr.xSrc = 0;
4039 descr.ySrc = 0;
4040 descr.xDest = 0;
4041 descr.yDest = height - startscan - lines;
4042 descr.width = ds.dsBm.bmWidth;
4043 descr.height = lines;
4044 descr.shm_mode = X11DRV_SHM_NONE;
4045 descr.dibpitch = ((descr.infoWidth * descr.infoBpp + 31) &~31) / 8;
4046 descr.physBitmap = NULL;
4047 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
4048 result = X11DRV_DIB_SetImageBits( &descr );
4050 /* optimisation for the case where the input bits are in exactly the same
4051 * format as the internal representation and copying to the app bits is
4052 * cheap - saves a round trip to the X server */
4053 if (descr.compression == BI_RGB &&
4054 coloruse == DIB_RGB_COLORS &&
4055 descr.infoBpp == ds.dsBm.bmBitsPixel &&
4056 physBitmap->base && physBitmap->size < 65536)
4058 unsigned int srcwidthb = X11DRV_DIB_GetDIBWidthBytes( width, descr.infoBpp );
4059 int dstwidthb = ds.dsBm.bmWidthBytes;
4060 LPBYTE dbits = physBitmap->base + startscan * dstwidthb;
4061 const BYTE *sbits = bits;
4062 int widthb;
4063 UINT y;
4065 TRACE("syncing compatible set bits to app bits\n");
4066 if ((tmpheight < 0) ^ physBitmap->topdown)
4068 dbits += dstwidthb * (lines-1);
4069 dstwidthb = -dstwidthb;
4071 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4072 widthb = min(srcwidthb, abs(dstwidthb));
4073 for (y = 0; y < lines; y++, dbits += dstwidthb, sbits += srcwidthb)
4074 memcpy(dbits, sbits, widthb);
4075 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4076 physBitmap->status = DIB_Status_InSync;
4078 X11DRV_DIB_Unlock( physBitmap, TRUE );
4080 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4082 return result;
4085 /***********************************************************************
4086 * GetDIBits (X11DRV.@)
4088 INT X11DRV_GetDIBits( PHYSDEV dev, HBITMAP hbitmap, UINT startscan, UINT lines,
4089 LPVOID bits, BITMAPINFO *info, UINT coloruse )
4091 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
4092 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
4093 DIBSECTION dib;
4094 X11DRV_DIB_IMAGEBITS_DESCR descr;
4095 PALETTEENTRY palette[256];
4096 size_t obj_size;
4097 int height;
4098 LONG width, tempHeight;
4099 int bitmap_type;
4100 BOOL core_header;
4101 void* colorPtr;
4102 static const PALETTEENTRY peBlack = {0,0,0,0};
4103 static const PALETTEENTRY peWhite = {255,255,255,0};
4105 if (!physBitmap) return 0;
4106 if (!(obj_size = GetObjectW( hbitmap, sizeof(dib), &dib ))) return 0;
4108 bitmap_type = DIB_GetBitmapInfo( (BITMAPINFOHEADER*)info, &width, &tempHeight, &descr.infoBpp, &descr.compression);
4109 if (bitmap_type == -1)
4111 ERR("Invalid bitmap\n");
4112 return 0;
4115 if (physBitmap->pixmap_depth > 1)
4117 GetPaletteEntries( GetCurrentObject( dev->hdc, OBJ_PAL ), 0, 256, palette );
4119 else
4121 palette[0] = peBlack;
4122 palette[1] = peWhite;
4125 descr.lines = tempHeight;
4126 core_header = (bitmap_type == 0);
4127 colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
4129 TRACE("%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
4130 lines, dib.dsBm.bmWidth, dib.dsBm.bmHeight, width, descr.lines, startscan);
4132 if( lines > dib.dsBm.bmHeight ) lines = dib.dsBm.bmHeight;
4134 height = descr.lines;
4135 if (height < 0) height = -height;
4136 if( lines > height ) lines = height;
4137 /* Top-down images have a negative biHeight, the scanlines of these images
4138 * were inverted in X11DRV_DIB_GetImageBits_xx
4139 * To prevent this we simply change the sign of lines
4140 * (the number of scan lines to copy).
4141 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4143 if( descr.lines < 0 && lines > 0) lines = -lines;
4145 if( startscan >= dib.dsBm.bmHeight ) return 0;
4147 descr.colorMap = NULL;
4149 switch (descr.infoBpp)
4151 case 1:
4152 case 4:
4153 case 8:
4154 descr.rMask= descr.gMask = descr.bMask = 0;
4155 if(coloruse == DIB_RGB_COLORS)
4156 descr.colorMap = colorPtr;
4157 else {
4158 int num_colors = 1 << descr.infoBpp, i;
4159 RGBQUAD *rgb;
4160 COLORREF colref;
4161 WORD *index = colorPtr;
4162 descr.colorMap = rgb = HeapAlloc(GetProcessHeap(), 0, num_colors * sizeof(RGBQUAD));
4163 for(i = 0; i < num_colors; i++, rgb++, index++) {
4164 colref = X11DRV_PALETTE_ToLogical(physDev, X11DRV_PALETTE_ToPhysical(physDev, PALETTEINDEX(*index)));
4165 rgb->rgbRed = GetRValue(colref);
4166 rgb->rgbGreen = GetGValue(colref);
4167 rgb->rgbBlue = GetBValue(colref);
4168 rgb->rgbReserved = 0;
4171 break;
4172 case 15:
4173 case 16:
4174 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4175 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4176 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4177 break;
4178 case 24:
4179 case 32:
4180 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4181 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4182 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4183 break;
4186 descr.physDev = physDev;
4187 descr.palentry = palette;
4188 descr.bits = bits;
4189 descr.image = physBitmap->image;
4190 descr.infoWidth = width;
4191 descr.lines = lines;
4192 descr.depth = physBitmap->pixmap_depth;
4193 descr.shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
4194 descr.drawable = physBitmap->pixmap;
4195 descr.gc = get_bitmap_gc(physBitmap->pixmap_depth);
4196 descr.width = dib.dsBm.bmWidth;
4197 descr.height = dib.dsBm.bmHeight;
4198 descr.xDest = 0;
4199 descr.yDest = 0;
4200 descr.xSrc = 0;
4201 descr.sizeImage = core_header ? 0 : info->bmiHeader.biSizeImage;
4202 descr.physBitmap = physBitmap;
4204 if (descr.lines > 0)
4206 descr.ySrc = (descr.height-1) - (startscan + (lines-1));
4208 else
4210 descr.ySrc = startscan;
4212 descr.shm_mode = physBitmap->shm_mode;
4213 descr.dibpitch = (((descr.infoWidth * descr.infoBpp + 31) &~31) / 8);
4215 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
4216 X11DRV_DIB_GetImageBits( &descr );
4217 X11DRV_DIB_Unlock( physBitmap, TRUE );
4219 if(!core_header && info->bmiHeader.biSizeImage == 0) /* Fill in biSizeImage */
4220 info->bmiHeader.biSizeImage = X11DRV_DIB_GetDIBImageBytes( descr.infoWidth,
4221 descr.lines,
4222 descr.infoBpp);
4224 if (descr.compression == BI_BITFIELDS)
4226 *(DWORD *)info->bmiColors = descr.rMask;
4227 *((DWORD *)info->bmiColors + 1) = descr.gMask;
4228 *((DWORD *)info->bmiColors + 2) = descr.bMask;
4230 else if (!core_header)
4232 /* if RLE or JPEG compression were supported,
4233 * this line would be invalid. */
4234 info->bmiHeader.biCompression = 0;
4237 if(descr.colorMap != colorPtr)
4238 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4239 return lines;
4242 /***********************************************************************
4243 * X11DRV_DIB_DoCopyDIBSection
4245 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB,
4246 void *colorMap, int nColorMap,
4247 Drawable dest, GC gc,
4248 DWORD xSrc, DWORD ySrc,
4249 DWORD xDest, DWORD yDest,
4250 DWORD width, DWORD height)
4252 DIBSECTION dibSection;
4253 X11DRV_DIB_IMAGEBITS_DESCR descr;
4254 int identity[2] = {0,1};
4256 if (!GetObjectW( physBitmap->hbitmap, sizeof(dibSection), &dibSection )) return;
4258 descr.physDev = NULL;
4259 descr.palentry = NULL;
4260 descr.infoWidth = dibSection.dsBmih.biWidth;
4261 descr.infoBpp = dibSection.dsBmih.biBitCount;
4262 descr.lines = physBitmap->topdown ? -dibSection.dsBmih.biHeight : dibSection.dsBmih.biHeight;
4263 descr.image = physBitmap->image;
4264 descr.colorMap = colorMap;
4265 descr.nColorMap = nColorMap;
4266 descr.bits = dibSection.dsBm.bmBits;
4267 descr.depth = physBitmap->pixmap_depth;
4268 descr.shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
4269 descr.compression = dibSection.dsBmih.biCompression;
4270 descr.physBitmap = physBitmap;
4272 if(descr.infoBpp == 1)
4273 descr.colorMap = (void*)identity;
4275 switch (descr.infoBpp)
4277 case 1:
4278 case 4:
4279 case 8:
4280 descr.rMask = descr.gMask = descr.bMask = 0;
4281 break;
4282 case 15:
4283 case 16:
4284 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0x7c00;
4285 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x03e0;
4286 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x001f;
4287 break;
4289 case 24:
4290 case 32:
4291 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0xff0000;
4292 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x00ff00;
4293 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x0000ff;
4294 break;
4297 /* Hack for now */
4298 descr.drawable = dest;
4299 descr.gc = gc;
4300 descr.xSrc = xSrc;
4301 descr.ySrc = ySrc;
4302 descr.xDest = xDest;
4303 descr.yDest = yDest;
4304 descr.width = width;
4305 descr.height = height;
4306 descr.sizeImage = 0;
4308 descr.shm_mode = physBitmap->shm_mode;
4309 #ifdef HAVE_LIBXXSHM
4310 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP && physBitmap->pixmap != dest)
4312 descr.shm_mode = X11DRV_SHM_NONE;
4314 #endif
4315 descr.dibpitch = dibSection.dsBm.bmWidthBytes;
4317 if (toDIB)
4319 TRACE("Copying from Pixmap to DIB bits\n");
4320 X11DRV_DIB_GetImageBits( &descr );
4322 else
4324 TRACE("Copying from DIB bits to Pixmap\n");
4325 X11DRV_DIB_SetImageBits( &descr );
4329 /***********************************************************************
4330 * X11DRV_DIB_CopyDIBSection
4332 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
4333 DWORD xSrc, DWORD ySrc, DWORD xDest, DWORD yDest,
4334 DWORD width, DWORD height)
4336 DIBSECTION dib;
4337 X_PHYSBITMAP *physBitmap;
4338 unsigned int nColorMap;
4339 int* x11ColorMap;
4340 int freeColorMap;
4342 TRACE("(%p,%p,%d,%d,%d,%d,%d,%d)\n", physDevSrc->dev.hdc, physDevDst->dev.hdc,
4343 xSrc, ySrc, xDest, yDest, width, height);
4344 /* this function is meant as an optimization for BitBlt,
4345 * not to be called otherwise */
4346 physBitmap = physDevSrc->bitmap;
4347 if (!physBitmap || GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib ) != sizeof(dib))
4349 ERR("called for non-DIBSection!?\n");
4350 return;
4352 /* while BitBlt should already have made sure we only get
4353 * positive values, we should check for oversize values */
4354 if ((xSrc < dib.dsBm.bmWidth) &&
4355 (ySrc < dib.dsBm.bmHeight)) {
4356 if (xSrc + width > dib.dsBm.bmWidth)
4357 width = dib.dsBm.bmWidth - xSrc;
4358 if (ySrc + height > dib.dsBm.bmHeight)
4359 height = dib.dsBm.bmHeight - ySrc;
4360 /* if the source bitmap is 8bpp or less, we're supposed to use the
4361 * DC's palette for color conversion (not the DIB color table) */
4362 if (dib.dsBm.bmBitsPixel <= 8) {
4363 HPALETTE hPalette = GetCurrentObject( physDevSrc->dev.hdc, OBJ_PAL );
4364 if (!hPalette || (hPalette == GetStockObject(DEFAULT_PALETTE))) {
4365 /* HACK: no palette has been set in the source DC,
4366 * use the DIB colormap instead - this is necessary in some
4367 * cases since we need to do depth conversion in some places
4368 * where real Windows can just copy data straight over */
4369 x11ColorMap = physBitmap->colorMap;
4370 nColorMap = physBitmap->nColorMap;
4371 freeColorMap = FALSE;
4372 } else {
4373 const BITMAPINFO* info = (BITMAPINFO*)&dib.dsBmih;
4374 unsigned int i;
4376 nColorMap = X11DRV_DIB_GetColorCount(info);
4377 x11ColorMap = HeapAlloc(GetProcessHeap(), 0, nColorMap * sizeof(int));
4378 for (i = 0; i < nColorMap; i++)
4379 x11ColorMap[i] = X11DRV_PALETTE_ToPhysical(physDevSrc, PALETTEINDEX(i));
4380 freeColorMap = TRUE;
4383 else
4385 nColorMap = 0;
4386 x11ColorMap = NULL;
4387 freeColorMap = FALSE;
4389 /* perform the copy */
4390 X11DRV_DIB_DoCopyDIBSection(physBitmap, FALSE, x11ColorMap, nColorMap,
4391 physDevDst->drawable, physDevDst->gc, xSrc, ySrc,
4392 physDevDst->dc_rect.left + xDest, physDevDst->dc_rect.top + yDest,
4393 width, height);
4394 /* free color mapping */
4395 if (freeColorMap)
4396 HeapFree(GetProcessHeap(), 0, x11ColorMap);
4400 /***********************************************************************
4401 * X11DRV_DIB_DoUpdateDIBSection
4403 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB)
4405 BITMAP bitmap;
4407 GetObjectW( physBitmap->hbitmap, sizeof(bitmap), &bitmap );
4408 X11DRV_DIB_DoCopyDIBSection(physBitmap, toDIB,
4409 physBitmap->colorMap, physBitmap->nColorMap,
4410 physBitmap->pixmap, get_bitmap_gc(physBitmap->pixmap_depth),
4411 0, 0, 0, 0, bitmap.bmWidth, bitmap.bmHeight);
4414 /***********************************************************************
4415 * X11DRV_DIB_FaultHandler
4417 static LONG CALLBACK X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep )
4419 X_PHYSBITMAP *physBitmap = NULL;
4420 BOOL found = FALSE;
4421 BYTE *addr;
4422 struct list *ptr;
4423 const size_t pagemask = getpagesize() - 1;
4425 if (ep->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
4426 return EXCEPTION_CONTINUE_SEARCH;
4428 addr = (BYTE *)ep->ExceptionRecord->ExceptionInformation[1];
4430 EnterCriticalSection(&dibs_cs);
4431 LIST_FOR_EACH( ptr, &dibs_list )
4433 physBitmap = LIST_ENTRY( ptr, X_PHYSBITMAP, entry );
4434 if ((physBitmap->base <= addr) &&
4435 (addr < physBitmap->base + ((physBitmap->size + pagemask) & ~pagemask)))
4437 found = TRUE;
4438 break;
4441 LeaveCriticalSection(&dibs_cs);
4443 if (!found) return EXCEPTION_CONTINUE_SEARCH;
4445 if (addr >= physBitmap->base + physBitmap->size)
4446 WARN( "%p: access to %p beyond the end of the DIB\n", physBitmap->hbitmap, addr );
4448 X11DRV_DIB_Lock( physBitmap, DIB_Status_None );
4449 if (ep->ExceptionRecord->ExceptionInformation[0] == EXCEPTION_WRITE_FAULT) {
4450 /* the app tried to write the DIB bits */
4451 X11DRV_DIB_Coerce( physBitmap, DIB_Status_AppMod);
4452 } else {
4453 /* the app tried to read the DIB bits */
4454 X11DRV_DIB_Coerce( physBitmap, DIB_Status_InSync);
4456 X11DRV_DIB_Unlock( physBitmap, TRUE );
4458 return EXCEPTION_CONTINUE_EXECUTION;
4461 /***********************************************************************
4462 * X11DRV_DIB_Coerce
4464 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *physBitmap, INT req)
4466 INT ret = DIB_Status_None;
4468 if (!physBitmap->image) return ret; /* not a DIB section */
4469 EnterCriticalSection(&physBitmap->lock);
4470 ret = physBitmap->status;
4471 switch (req) {
4472 case DIB_Status_GdiMod:
4473 /* GDI access - request to draw on pixmap */
4474 switch (physBitmap->status)
4476 default:
4477 case DIB_Status_None:
4478 physBitmap->p_status = DIB_Status_GdiMod;
4479 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4480 break;
4482 case DIB_Status_GdiMod:
4483 TRACE("GdiMod requested in status GdiMod\n" );
4484 physBitmap->p_status = DIB_Status_GdiMod;
4485 break;
4487 case DIB_Status_InSync:
4488 TRACE("GdiMod requested in status InSync\n" );
4489 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4490 physBitmap->status = DIB_Status_GdiMod;
4491 physBitmap->p_status = DIB_Status_InSync;
4492 break;
4494 case DIB_Status_AppMod:
4495 TRACE("GdiMod requested in status AppMod\n" );
4496 /* make it readonly to avoid app changing data while we copy */
4497 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4498 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4499 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4500 physBitmap->p_status = DIB_Status_AppMod;
4501 physBitmap->status = DIB_Status_GdiMod;
4502 break;
4504 break;
4506 case DIB_Status_InSync:
4507 /* App access - request access to read DIB surface */
4508 /* (typically called from signal handler) */
4509 switch (physBitmap->status)
4511 default:
4512 case DIB_Status_None:
4513 /* shouldn't happen from signal handler */
4514 break;
4516 case DIB_Status_GdiMod:
4517 TRACE("InSync requested in status GdiMod\n" );
4518 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4519 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4520 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4521 physBitmap->status = DIB_Status_InSync;
4522 break;
4524 case DIB_Status_InSync:
4525 TRACE("InSync requested in status InSync\n" );
4526 /* shouldn't happen from signal handler */
4527 break;
4529 case DIB_Status_AppMod:
4530 TRACE("InSync requested in status AppMod\n" );
4531 /* no reason to do anything here, and this
4532 * shouldn't happen from signal handler */
4533 break;
4535 break;
4537 case DIB_Status_AppMod:
4538 /* App access - request access to write DIB surface */
4539 /* (typically called from signal handler) */
4540 switch (physBitmap->status)
4542 default:
4543 case DIB_Status_None:
4544 /* shouldn't happen from signal handler */
4545 break;
4547 case DIB_Status_GdiMod:
4548 TRACE("AppMod requested in status GdiMod\n" );
4549 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4550 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4551 physBitmap->status = DIB_Status_AppMod;
4552 break;
4554 case DIB_Status_InSync:
4555 TRACE("AppMod requested in status InSync\n" );
4556 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4557 physBitmap->status = DIB_Status_AppMod;
4558 break;
4560 case DIB_Status_AppMod:
4561 TRACE("AppMod requested in status AppMod\n" );
4562 /* shouldn't happen from signal handler */
4563 break;
4565 break;
4567 /* it is up to the caller to do the copy/conversion, probably
4568 * using the return value to decide where to copy from */
4570 LeaveCriticalSection(&physBitmap->lock);
4571 return ret;
4574 /***********************************************************************
4575 * X11DRV_DIB_Lock
4577 INT X11DRV_DIB_Lock(X_PHYSBITMAP *physBitmap, INT req)
4579 INT ret = DIB_Status_None;
4581 if (!physBitmap->image) return ret; /* not a DIB section */
4582 TRACE("Locking %p from thread %04x\n", physBitmap->hbitmap, GetCurrentThreadId());
4583 EnterCriticalSection(&physBitmap->lock);
4584 ret = physBitmap->status;
4585 if (req != DIB_Status_None)
4586 X11DRV_DIB_Coerce(physBitmap, req);
4587 return ret;
4590 /***********************************************************************
4591 * X11DRV_DIB_Unlock
4593 void X11DRV_DIB_Unlock(X_PHYSBITMAP *physBitmap, BOOL commit)
4595 if (!physBitmap->image) return; /* not a DIB section */
4596 switch (physBitmap->status)
4598 default:
4599 case DIB_Status_None:
4600 /* in case anyone is wondering, this is the "signal handler doesn't
4601 * work" case, where we always have to be ready for app access */
4602 if (commit) {
4603 switch (physBitmap->p_status)
4605 case DIB_Status_GdiMod:
4606 TRACE("Unlocking and syncing from GdiMod\n" );
4607 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4608 break;
4610 default:
4611 TRACE("Unlocking without needing to sync\n" );
4612 break;
4615 else TRACE("Unlocking with no changes\n");
4616 physBitmap->p_status = DIB_Status_None;
4617 break;
4619 case DIB_Status_GdiMod:
4620 TRACE("Unlocking in status GdiMod\n" );
4621 /* DIB was protected in Coerce */
4622 if (!commit) {
4623 /* no commit, revert to InSync if applicable */
4624 if ((physBitmap->p_status == DIB_Status_InSync) ||
4625 (physBitmap->p_status == DIB_Status_AppMod)) {
4626 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4627 physBitmap->status = DIB_Status_InSync;
4630 break;
4632 case DIB_Status_InSync:
4633 TRACE("Unlocking in status InSync\n" );
4634 /* DIB was already protected in Coerce */
4635 break;
4637 case DIB_Status_AppMod:
4638 TRACE("Unlocking in status AppMod\n" );
4639 /* DIB was already protected in Coerce */
4640 /* this case is ordinary only called from the signal handler,
4641 * so we don't bother to check for !commit */
4642 break;
4644 LeaveCriticalSection(&physBitmap->lock);
4645 TRACE("Unlocked %p\n", physBitmap->hbitmap);
4648 /***********************************************************************
4649 * X11DRV_CoerceDIBSection
4651 INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req)
4653 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4654 return X11DRV_DIB_Coerce(physDev->bitmap, req);
4657 /***********************************************************************
4658 * X11DRV_LockDIBSection
4660 INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req)
4662 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4663 return X11DRV_DIB_Lock(physDev->bitmap, req);
4666 /***********************************************************************
4667 * X11DRV_UnlockDIBSection
4669 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
4671 if (!physDev || !physDev->bitmap) return;
4672 X11DRV_DIB_Unlock(physDev->bitmap, commit);
4676 #ifdef HAVE_LIBXXSHM
4677 /***********************************************************************
4678 * X11DRV_XShmErrorHandler
4681 static int XShmErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
4683 return 1; /* FIXME: should check event contents */
4686 /***********************************************************************
4687 * X11DRV_XShmCreateImage
4690 static XImage *X11DRV_XShmCreateImage( int width, int height, int bpp,
4691 XShmSegmentInfo* shminfo)
4693 XImage *image;
4695 image = XShmCreateImage(gdi_display, visual, bpp, ZPixmap, NULL, shminfo, width, height);
4696 if (image)
4698 shminfo->shmid = shmget(IPC_PRIVATE, image->bytes_per_line * height,
4699 IPC_CREAT|0700);
4700 if( shminfo->shmid != -1 )
4702 shminfo->shmaddr = shmat( shminfo->shmid, 0, 0 );
4703 if( shminfo->shmaddr != (char*)-1 )
4705 BOOL ok;
4707 shminfo->readOnly = FALSE;
4708 X11DRV_expect_error( gdi_display, XShmErrorHandler, NULL );
4709 ok = (XShmAttach( gdi_display, shminfo ) != 0);
4710 XSync( gdi_display, False );
4711 if (X11DRV_check_error()) ok = FALSE;
4712 if (ok)
4714 shmctl(shminfo->shmid, IPC_RMID, 0);
4715 return image; /* Success! */
4717 /* An error occurred */
4718 shmdt(shminfo->shmaddr);
4720 shmctl(shminfo->shmid, IPC_RMID, 0);
4721 shminfo->shmid = -1;
4723 XFlush(gdi_display);
4724 XDestroyImage(image);
4725 image = NULL;
4727 return image;
4729 #endif /* HAVE_LIBXXSHM */
4731 static Bool X11DRV_DIB_QueryXShm( Bool *pixmaps )
4733 static Bool have_xshm, have_xshm_pixmaps;
4734 static BOOL initialized;
4736 if (!initialized)
4738 #ifdef HAVE_LIBXXSHM
4739 int major, minor;
4741 have_xshm = XShmQueryVersion( gdi_display, &major, &minor, &have_xshm_pixmaps );
4742 #endif
4743 initialized = TRUE;
4746 *pixmaps = have_xshm_pixmaps;
4747 return have_xshm;
4750 /***********************************************************************
4751 * X11DRV_CreateDIBSection (X11DRV.@)
4753 HBITMAP X11DRV_CreateDIBSection( PHYSDEV dev, HBITMAP hbitmap, const BITMAPINFO *bmi, UINT usage )
4755 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
4756 X_PHYSBITMAP *physBitmap;
4757 DIBSECTION dib;
4758 WORD bpp, compr;
4759 LONG w, h;
4760 #ifdef HAVE_LIBXXSHM
4761 Bool pixmaps;
4762 #endif
4764 DIB_GetBitmapInfo( &bmi->bmiHeader, &w, &h, &bpp, &compr );
4766 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return 0;
4767 if (h < 0) physBitmap->topdown = TRUE;
4768 physBitmap->status = DIB_Status_None;
4770 GetObjectW( hbitmap, sizeof(dib), &dib );
4772 /* create color map */
4773 if (dib.dsBm.bmBitsPixel <= 8)
4775 physBitmap->colorMap = X11DRV_DIB_BuildColorMap( physDev,
4776 usage, dib.dsBm.bmBitsPixel, bmi,
4777 &physBitmap->nColorMap );
4780 if (!X11DRV_XRender_SetPhysBitmapDepth( physBitmap, dib.dsBm.bmBitsPixel, &dib ))
4782 if (dib.dsBm.bmBitsPixel == 1)
4784 physBitmap->pixmap_depth = 1;
4785 physBitmap->trueColor = FALSE;
4787 else
4789 physBitmap->pixmap_depth = screen_depth;
4790 physBitmap->pixmap_color_shifts = X11DRV_PALETTE_default_shifts;
4791 physBitmap->trueColor = (visual->class == TrueColor || visual->class == DirectColor);
4795 /* create pixmap and X image */
4796 wine_tsx11_lock();
4797 #ifdef HAVE_LIBXXSHM
4798 physBitmap->shminfo.shmid = -1;
4800 if (X11DRV_DIB_QueryXShm( &pixmaps )
4801 && (physBitmap->image = X11DRV_XShmCreateImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4802 physBitmap->pixmap_depth, &physBitmap->shminfo )))
4804 if (pixmaps)
4806 physBitmap->shm_mode = X11DRV_SHM_PIXMAP;
4807 physBitmap->image->data = HeapAlloc( GetProcessHeap(), 0,
4808 dib.dsBm.bmHeight * physBitmap->image->bytes_per_line );
4810 else
4812 physBitmap->shm_mode = X11DRV_SHM_IMAGE;
4813 physBitmap->image->data = physBitmap->shminfo.shmaddr;
4816 else
4817 #endif
4819 physBitmap->shm_mode = X11DRV_SHM_NONE;
4820 physBitmap->image = X11DRV_DIB_CreateXImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4821 physBitmap->pixmap_depth );
4824 #ifdef HAVE_LIBXXSHM
4825 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP)
4827 TRACE("Creating shared pixmap for bmp %p.\n", physBitmap->hbitmap);
4828 physBitmap->pixmap = XShmCreatePixmap( gdi_display, root_window,
4829 physBitmap->shminfo.shmaddr, &physBitmap->shminfo,
4830 dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4831 physBitmap->pixmap_depth );
4833 else
4834 #endif
4836 physBitmap->pixmap = XCreatePixmap( gdi_display, root_window, dib.dsBm.bmWidth,
4837 dib.dsBm.bmHeight, physBitmap->pixmap_depth );
4840 wine_tsx11_unlock();
4841 if (!physBitmap->pixmap || !physBitmap->image) return 0;
4843 if (physBitmap->trueColor)
4845 ColorShifts *shifts = &physBitmap->pixmap_color_shifts;
4847 /* When XRender is around and used, we also support dibsections in other formats like 16-bit. In these
4848 * cases we need to override the mask of XImages. The reason is that during XImage creation the masks are
4849 * derived from a 24-bit visual (no 16-bit ones are around when X runs at 24-bit). SetImageBits and other
4850 * functions rely on the color masks for proper color conversion, so we need to override the masks here. */
4851 physBitmap->image->red_mask = shifts->physicalRed.max << shifts->physicalRed.shift;
4852 physBitmap->image->green_mask = shifts->physicalGreen.max << shifts->physicalGreen.shift;
4853 physBitmap->image->blue_mask = shifts->physicalBlue.max << shifts->physicalBlue.shift;
4856 /* install fault handler */
4857 InitializeCriticalSection( &physBitmap->lock );
4858 physBitmap->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": X_PHYSBITMAP.lock");
4860 physBitmap->base = dib.dsBm.bmBits;
4861 physBitmap->size = dib.dsBmih.biSizeImage;
4862 physBitmap->status = DIB_Status_AppMod;
4864 if (!dibs_handler)
4865 dibs_handler = AddVectoredExceptionHandler( TRUE, X11DRV_DIB_FaultHandler );
4866 EnterCriticalSection( &dibs_cs );
4867 list_add_head( &dibs_list, &physBitmap->entry );
4868 LeaveCriticalSection( &dibs_cs );
4870 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4872 return hbitmap;
4875 /***********************************************************************
4876 * X11DRV_DIB_DeleteDIBSection
4878 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP *physBitmap, DIBSECTION *dib)
4880 BOOL last;
4882 EnterCriticalSection( &dibs_cs );
4883 list_remove( &physBitmap->entry );
4884 last = list_empty( &dibs_list );
4885 LeaveCriticalSection( &dibs_cs );
4887 if (last)
4889 RemoveVectoredExceptionHandler( dibs_handler );
4890 dibs_handler = NULL;
4893 if (dib->dshSection)
4894 X11DRV_DIB_Coerce(physBitmap, DIB_Status_InSync);
4896 if (physBitmap->image)
4898 wine_tsx11_lock();
4899 #ifdef HAVE_LIBXXSHM
4900 if (physBitmap->shminfo.shmid != -1)
4902 XShmDetach( gdi_display, &(physBitmap->shminfo) );
4903 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP) X11DRV_DIB_DestroyXImage( physBitmap->image );
4904 else XDestroyImage( physBitmap->image );
4905 shmdt( physBitmap->shminfo.shmaddr );
4906 physBitmap->shminfo.shmid = -1;
4907 physBitmap->shm_mode = X11DRV_SHM_NONE;
4909 else
4910 #endif
4911 X11DRV_DIB_DestroyXImage( physBitmap->image );
4912 wine_tsx11_unlock();
4915 HeapFree(GetProcessHeap(), 0, physBitmap->colorMap);
4916 physBitmap->lock.DebugInfo->Spare[0] = 0;
4917 DeleteCriticalSection(&physBitmap->lock);
4920 /***********************************************************************
4921 * SetDIBColorTable (X11DRV.@)
4923 UINT X11DRV_SetDIBColorTable( PHYSDEV dev, UINT start, UINT count, const RGBQUAD *colors )
4925 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
4926 DIBSECTION dib;
4927 UINT ret = 0;
4928 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4930 if (!physBitmap) return 0;
4931 GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib );
4933 if (physBitmap->colorMap && start < physBitmap->nColorMap) {
4934 UINT end = count + start;
4935 if (end > physBitmap->nColorMap) end = physBitmap->nColorMap;
4937 * Changing color table might change the mapping between
4938 * DIB colors and X11 colors and thus alter the visible state
4939 * of the bitmap object.
4942 * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4943 * at least for a 1 bpp dibsection
4945 X11DRV_DIB_Lock( physBitmap, DIB_Status_AppMod );
4946 X11DRV_DIB_GenColorMap( physDev, physBitmap->colorMap, DIB_RGB_COLORS,
4947 dib.dsBm.bmBitsPixel,
4948 TRUE, colors, start, end );
4949 X11DRV_DIB_Unlock( physBitmap, TRUE );
4950 ret = end - start;
4952 return ret;
4956 /***********************************************************************
4957 * X11DRV_DIB_CreateDIBFromBitmap
4959 * Allocates a packed DIB and copies the bitmap data into it.
4961 HGLOBAL X11DRV_DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
4963 BITMAP bmp;
4964 HGLOBAL hPackedDIB;
4965 LPBYTE pPackedDIB;
4966 LPBITMAPINFOHEADER pbmiHeader;
4967 unsigned int cDataSize, cPackedSize, OffsetBits;
4968 int nLinesCopied;
4970 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
4973 * A packed DIB contains a BITMAPINFO structure followed immediately by
4974 * an optional color palette and the pixel data.
4977 /* Calculate the size of the packed DIB */
4978 cDataSize = X11DRV_DIB_GetDIBWidthBytes( bmp.bmWidth, bmp.bmBitsPixel ) * abs( bmp.bmHeight );
4979 cPackedSize = sizeof(BITMAPINFOHEADER)
4980 + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
4981 + cDataSize;
4982 /* Get the offset to the bits */
4983 OffsetBits = cPackedSize - cDataSize;
4985 /* Allocate the packed DIB */
4986 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
4987 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
4988 cPackedSize );
4989 if ( !hPackedDIB )
4991 WARN("Could not allocate packed DIB!\n");
4992 return 0;
4995 /* A packed DIB starts with a BITMAPINFOHEADER */
4996 pPackedDIB = GlobalLock(hPackedDIB);
4997 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
4999 /* Init the BITMAPINFOHEADER */
5000 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
5001 pbmiHeader->biWidth = bmp.bmWidth;
5002 pbmiHeader->biHeight = bmp.bmHeight;
5003 pbmiHeader->biPlanes = 1;
5004 pbmiHeader->biBitCount = bmp.bmBitsPixel;
5005 pbmiHeader->biCompression = BI_RGB;
5006 pbmiHeader->biSizeImage = 0;
5007 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
5008 pbmiHeader->biClrUsed = 0;
5009 pbmiHeader->biClrImportant = 0;
5011 /* Retrieve the DIB bits from the bitmap and fill in the
5012 * DIB color table if present */
5014 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
5015 hBmp, /* Handle to bitmap */
5016 0, /* First scan line to set in dest bitmap */
5017 bmp.bmHeight, /* Number of scan lines to copy */
5018 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
5019 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
5020 0); /* RGB or palette index */
5021 GlobalUnlock(hPackedDIB);
5023 /* Cleanup if GetDIBits failed */
5024 if (nLinesCopied != bmp.bmHeight)
5026 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
5027 GlobalFree(hPackedDIB);
5028 hPackedDIB = 0;
5030 return hPackedDIB;
5034 /**************************************************************************
5035 * X11DRV_DIB_CreateDIBFromPixmap
5037 * Allocates a packed DIB and copies the Pixmap data into it.
5038 * The Pixmap passed in is deleted after the conversion.
5040 HGLOBAL X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap, HDC hdc)
5042 HDC hdcMem;
5043 X_PHYSBITMAP *physBitmap;
5044 Pixmap orig_pixmap;
5045 HBITMAP hBmp = 0;
5046 HGLOBAL hPackedDIB = 0;
5047 Window root;
5048 int x,y; /* Unused */
5049 unsigned border_width; /* Unused */
5050 unsigned int depth, width, height;
5052 /* Get the Pixmap dimensions and bit depth */
5053 wine_tsx11_lock();
5054 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
5055 &border_width, &depth)) depth = 0;
5056 wine_tsx11_unlock();
5057 if (!pixmap_formats[depth]) return 0;
5059 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
5060 width, height, depth);
5063 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
5064 * and make it a container for the pixmap passed.
5066 if (!(hBmp = CreateBitmap( width, height, 1, pixmap_formats[depth]->bits_per_pixel, NULL ))) return 0;
5068 /* force bitmap to be owned by a screen DC */
5069 hdcMem = CreateCompatibleDC( hdc );
5070 SelectObject( hdcMem, SelectObject( hdcMem, hBmp ));
5071 DeleteDC( hdcMem );
5073 physBitmap = X11DRV_get_phys_bitmap( hBmp );
5075 /* swap the new pixmap in */
5076 orig_pixmap = physBitmap->pixmap;
5077 physBitmap->pixmap = pixmap;
5080 * Create a packed DIB from the Pixmap wrapper bitmap created above.
5081 * A packed DIB contains a BITMAPINFO structure followed immediately by
5082 * an optional color palette and the pixel data.
5084 hPackedDIB = X11DRV_DIB_CreateDIBFromBitmap(hdc, hBmp);
5086 /* we can now get rid of the HBITMAP and its original pixmap */
5087 physBitmap->pixmap = orig_pixmap;
5088 DeleteObject(hBmp);
5090 TRACE("\tReturning packed DIB %p\n", hPackedDIB);
5091 return hPackedDIB;
5095 /**************************************************************************
5096 * X11DRV_DIB_CreatePixmapFromDIB
5098 * Creates a Pixmap from a packed DIB
5100 Pixmap X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB, HDC hdc )
5102 Pixmap pixmap;
5103 X_PHYSBITMAP *physBitmap;
5104 HBITMAP hBmp;
5105 LPBITMAPINFO pbmi;
5107 /* Create a DDB from the DIB */
5109 pbmi = GlobalLock(hPackedDIB);
5110 hBmp = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT,
5111 (LPBYTE)pbmi + bitmap_info_size( pbmi, DIB_RGB_COLORS ),
5112 pbmi, DIB_RGB_COLORS);
5113 GlobalUnlock(hPackedDIB);
5115 /* clear the physBitmap so that we can steal its pixmap */
5116 physBitmap = X11DRV_get_phys_bitmap( hBmp );
5117 pixmap = physBitmap->pixmap;
5118 physBitmap->pixmap = 0;
5120 /* Delete the DDB we created earlier now that we have stolen its pixmap */
5121 DeleteObject(hBmp);
5123 TRACE("Returning Pixmap %lx\n", pixmap);
5124 return pixmap;