2 * X11DRV device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
26 #include <X11/extensions/XShm.h>
27 # ifdef HAVE_SYS_SHM_H
30 # ifdef HAVE_SYS_IPC_H
33 #endif /* defined(HAVE_LIBXXSHM) */
42 #include "wine/exception.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
47 static struct list dibs_list = LIST_INIT(dibs_list);
49 static CRITICAL_SECTION dibs_cs;
50 static CRITICAL_SECTION_DEBUG dibs_cs_debug =
53 { &dibs_cs_debug.ProcessLocksList, &dibs_cs_debug.ProcessLocksList },
54 0, 0, { (DWORD_PTR)(__FILE__ ": dibs_cs") }
56 static CRITICAL_SECTION dibs_cs = { &dibs_cs_debug, -1, 0, 0, 0, 0 };
58 static PVOID dibs_handler;
60 static int ximageDepthTable[32];
62 /* This structure holds the arguments for DIB_SetImageBits() */
65 X11DRV_PDEVICE *physDev;
68 PALETTEENTRY *palentry;
90 } X11DRV_DIB_IMAGEBITS_DESCR;
95 RLE_EOL = 0, /* End of line */
96 RLE_END = 1, /* End of bitmap */
97 RLE_DELTA = 2 /* Delta */
101 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *,INT);
102 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *,INT);
103 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *,BOOL);
106 Some of the following helper functions are duplicated in
110 /***********************************************************************
111 * X11DRV_DIB_GetXImageWidthBytes
113 * Return the width of an X image in bytes
115 static inline int X11DRV_DIB_GetXImageWidthBytes( int width, int depth )
117 if (!depth || depth > 32) goto error;
119 if (!ximageDepthTable[depth-1])
121 XImage *testimage = XCreateImage( gdi_display, visual, depth,
122 ZPixmap, 0, NULL, 1, 1, 32, 20 );
125 ximageDepthTable[depth-1] = testimage->bits_per_pixel;
126 XDestroyImage( testimage );
128 else ximageDepthTable[depth-1] = -1;
130 if (ximageDepthTable[depth-1] != -1)
131 return (4 * ((width * ximageDepthTable[depth-1] + 31) / 32));
134 WARN( "(%d): Unsupported depth\n", depth );
139 /***********************************************************************
140 * X11DRV_DIB_GetDIBWidthBytes
142 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
144 static int X11DRV_DIB_GetDIBWidthBytes( int width, int depth )
150 case 1: words = (width + 31) / 32; break;
151 case 4: words = (width + 7) / 8; break;
152 case 8: words = (width + 3) / 4; break;
154 case 16: words = (width + 1) / 2; break;
155 case 24: words = (width * 3 + 3) / 4; break;
157 WARN("(%d): Unsupported depth\n", depth );
166 /***********************************************************************
167 * X11DRV_DIB_GetDIBImageBytes
169 * Return the number of bytes used to hold the image in a DIB bitmap.
171 static int X11DRV_DIB_GetDIBImageBytes( int width, int height, int depth )
173 return X11DRV_DIB_GetDIBWidthBytes( width, depth ) * abs( height );
177 /***********************************************************************
180 * Return the size of the bitmap info structure including color table.
182 int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
184 unsigned int colors, masks = 0;
186 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
188 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
189 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
190 return sizeof(BITMAPCOREHEADER) + colors *
191 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
193 else /* assume BITMAPINFOHEADER */
195 colors = info->bmiHeader.biClrUsed;
196 if (!colors && (info->bmiHeader.biBitCount <= 8))
197 colors = 1 << info->bmiHeader.biBitCount;
198 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
199 return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
200 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
205 /***********************************************************************
206 * X11DRV_DIB_CreateXImage
210 XImage *X11DRV_DIB_CreateXImage( int width, int height, int depth )
216 width_bytes = X11DRV_DIB_GetXImageWidthBytes( width, depth );
217 image = XCreateImage( gdi_display, visual, depth, ZPixmap, 0,
218 calloc( height, width_bytes ),
219 width, height, 32, width_bytes );
225 /***********************************************************************
226 * DIB_GetBitmapInfoEx
228 * Get the info from a bitmap header.
229 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
231 static int DIB_GetBitmapInfoEx( const BITMAPINFOHEADER *header, LONG *width,
232 LONG *height, WORD *planes, WORD *bpp,
233 WORD *compr, DWORD *size )
235 if (header->biSize == sizeof(BITMAPCOREHEADER))
237 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
238 *width = core->bcWidth;
239 *height = core->bcHeight;
240 *planes = core->bcPlanes;
241 *bpp = core->bcBitCount;
246 if (header->biSize >= sizeof(BITMAPINFOHEADER))
248 *width = header->biWidth;
249 *height = header->biHeight;
250 *planes = header->biPlanes;
251 *bpp = header->biBitCount;
252 *compr = header->biCompression;
253 *size = header->biSizeImage;
256 ERR("(%d): unknown/wrong size for header\n", header->biSize );
261 /***********************************************************************
262 * X11DRV_DIB_GetColorCount
264 * Computes the number of colors for the bitmap palette.
265 * Should not be called for a >8-bit deep bitmap.
267 static unsigned int X11DRV_DIB_GetColorCount(const BITMAPINFO *info)
270 BOOL core_info = info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER);
274 colors = 1 << ((const BITMAPCOREINFO*)info)->bmciHeader.bcBitCount;
278 colors = info->bmiHeader.biClrUsed;
279 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
283 ERR("called with >256 colors!\n");
289 /***********************************************************************
292 * Get the info from a bitmap header.
293 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
295 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
296 LONG *height, WORD *bpp, WORD *compr )
301 return DIB_GetBitmapInfoEx( header, width, height, &planes, bpp, compr, &size);
305 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
307 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
308 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
311 /***********************************************************************
312 * X11DRV_DIB_GenColorMap
314 * Fills the color map of a bitmap palette. Should not be called
315 * for a >8-bit deep bitmap.
317 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
318 WORD coloruse, WORD depth, BOOL quads,
319 const void *colorPtr, int start, int end )
323 if (coloruse == DIB_RGB_COLORS)
327 const RGBQUAD * rgb = (const RGBQUAD *)colorPtr;
329 if (depth == 1) /* Monochrome */
334 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
335 invert = !colour_is_brighter(table[1], table[0]);
337 for (i = start; i < end; i++, rgb++)
338 colorMapping[i] = ((rgb->rgbRed + rgb->rgbGreen +
339 rgb->rgbBlue > 255*3/2 && !invert) ||
340 (rgb->rgbRed + rgb->rgbGreen +
341 rgb->rgbBlue <= 255*3/2 && invert));
344 for (i = start; i < end; i++, rgb++)
345 colorMapping[i] = X11DRV_PALETTE_ToPhysical( NULL, RGB(rgb->rgbRed,
351 const RGBTRIPLE * rgb = (const RGBTRIPLE *)colorPtr;
353 if (depth == 1) /* Monochrome */
358 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
359 invert = !colour_is_brighter(table[1], table[0]);
361 for (i = start; i < end; i++, rgb++)
362 colorMapping[i] = ((rgb->rgbtRed + rgb->rgbtGreen +
363 rgb->rgbtBlue > 255*3/2 && !invert) ||
364 (rgb->rgbtRed + rgb->rgbtGreen +
365 rgb->rgbtBlue <= 255*3/2 && invert));
368 for (i = start; i < end; i++, rgb++)
369 colorMapping[i] = X11DRV_PALETTE_ToPhysical( NULL, RGB(rgb->rgbtRed,
374 else /* DIB_PAL_COLORS */
376 const WORD * index = (const WORD *)colorPtr;
378 for (i = start; i < end; i++, index++)
379 colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(*index) );
385 /***********************************************************************
386 * X11DRV_DIB_BuildColorMap
388 * Build the color map from the bitmap palette. Should not be called
389 * for a >8-bit deep bitmap.
391 static int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE *physDev, WORD coloruse, WORD depth,
392 const BITMAPINFO *info, int *nColors )
395 const void *colorPtr;
399 *nColors = X11DRV_DIB_GetColorCount(info);
400 if (!*nColors) return NULL;
402 isInfo = info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER);
403 colorPtr = (const BYTE*)info + (WORD)info->bmiHeader.biSize;
404 if (!(colorMapping = HeapAlloc(GetProcessHeap(), 0, *nColors * sizeof(int) )))
407 return X11DRV_DIB_GenColorMap( physDev, colorMapping, coloruse, depth,
408 isInfo, colorPtr, 0, *nColors);
411 /***********************************************************************
412 * X11DRV_DIB_MapColor
414 static int X11DRV_DIB_MapColor( int *physMap, int nPhysMap, int phys, int oldcol )
418 if ((oldcol < nPhysMap) && (physMap[oldcol] == phys))
421 for (color = 0; color < nPhysMap; color++)
422 if (physMap[color] == phys)
425 WARN("Strange color %08x\n", phys);
430 /*********************************************************************
431 * X11DRV_DIB_GetNearestIndex
433 * Helper for X11DRV_DIB_GetDIBits.
434 * Returns the nearest colour table index for a given RGB.
435 * Nearest is defined by minimizing the sum of the squares.
437 static INT X11DRV_DIB_GetNearestIndex(RGBQUAD *colormap, int numColors, BYTE r, BYTE g, BYTE b)
439 INT i, best = -1, diff, bestdiff = -1;
442 for(color = colormap, i = 0; i < numColors; color++, i++) {
443 diff = (r - color->rgbRed) * (r - color->rgbRed) +
444 (g - color->rgbGreen) * (g - color->rgbGreen) +
445 (b - color->rgbBlue) * (b - color->rgbBlue);
448 if(best == -1 || diff < bestdiff) {
455 /*********************************************************************
456 * X11DRV_DIB_MaskToShift
458 * Helper for X11DRV_DIB_GetDIBits.
459 * Returns the by how many bits to shift a given color so that it is
460 * in the proper position.
462 INT X11DRV_DIB_MaskToShift(DWORD mask)
470 while ((mask&1)==0) {
477 /***********************************************************************
478 * X11DRV_DIB_CheckMask
480 * Check RGB mask if it is either 0 or matches visual's mask.
482 static inline int X11DRV_DIB_CheckMask(int red_mask, int green_mask, int blue_mask)
484 return ( red_mask == 0 && green_mask == 0 && blue_mask == 0 ) ||
485 ( red_mask == visual->red_mask && green_mask == visual->green_mask &&
486 blue_mask == visual->blue_mask );
489 /***********************************************************************
490 * X11DRV_DIB_SetImageBits_1
492 * SetDIBits for a 1-bit deep DIB.
494 static void X11DRV_DIB_SetImageBits_1( int lines, const BYTE *srcbits,
495 DWORD srcwidth, DWORD dstwidth, int left,
496 int *colors, XImage *bmpImage, DWORD linebytes)
505 srcbits = srcbits + linebytes * (lines - 1);
506 linebytes = -linebytes;
509 if ((extra = (left & 7)) != 0) {
513 srcbits += left >> 3;
514 width = min(srcwidth, dstwidth);
516 /* ==== pal 1 dib -> any bmp format ==== */
517 for (h = lines-1; h >=0; h--) {
519 for (i = width/8, x = left; i > 0; i--) {
521 XPutPixel( bmpImage, x++, h, colors[ srcval >> 7] );
522 XPutPixel( bmpImage, x++, h, colors[(srcval >> 6) & 1] );
523 XPutPixel( bmpImage, x++, h, colors[(srcval >> 5) & 1] );
524 XPutPixel( bmpImage, x++, h, colors[(srcval >> 4) & 1] );
525 XPutPixel( bmpImage, x++, h, colors[(srcval >> 3) & 1] );
526 XPutPixel( bmpImage, x++, h, colors[(srcval >> 2) & 1] );
527 XPutPixel( bmpImage, x++, h, colors[(srcval >> 1) & 1] );
528 XPutPixel( bmpImage, x++, h, colors[ srcval & 1] );
534 case 7: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
535 case 6: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
536 case 5: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
537 case 4: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
538 case 3: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
539 case 2: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
540 case 1: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]);
543 srcbits += linebytes;
547 /***********************************************************************
548 * X11DRV_DIB_GetImageBits_1
550 * GetDIBits for a 1-bit deep DIB.
552 static void X11DRV_DIB_GetImageBits_1( int lines, BYTE *dstbits,
553 DWORD dstwidth, DWORD srcwidth,
554 RGBQUAD *colors, PALETTEENTRY *srccolors,
555 XImage *bmpImage, DWORD linebytes )
558 int h, width = min(dstwidth, srcwidth);
562 dstbits = dstbits + linebytes * (lines - 1);
563 linebytes = -linebytes;
566 switch (bmpImage->depth)
570 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
572 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
575 for (h=lines-1; h>=0; h--) {
579 for (x=0; x<width; x++) {
581 srcval=srccolors[XGetPixel(bmpImage, x, h)];
582 dstval|=(X11DRV_DIB_GetNearestIndex
586 srcval.peBlue) << (7 - (x & 7)));
595 dstbits += linebytes;
603 if (X11DRV_DIB_CheckMask(bmpImage->red_mask, bmpImage->green_mask, bmpImage->blue_mask)
605 /* ==== pal 8 bmp -> pal 1 dib ==== */
607 const BYTE* srcpixel;
610 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
612 for (h=0; h<lines; h++) {
617 for (x=0; x<width; x++) {
619 srcval=srccolors[*srcpixel++];
620 dstval|=(X11DRV_DIB_GetNearestIndex
624 srcval.peBlue) << (7-(x&7)) );
633 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
634 dstbits += linebytes;
645 const WORD* srcpixel;
648 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
650 if (bmpImage->green_mask==0x03e0) {
651 if (bmpImage->red_mask==0x7c00) {
652 /* ==== rgb 555 bmp -> pal 1 dib ==== */
653 for (h=0; h<lines; h++) {
658 for (x=0; x<width; x++) {
661 dstval|=(X11DRV_DIB_GetNearestIndex
663 ((srcval >> 7) & 0xf8) | /* r */
664 ((srcval >> 12) & 0x07),
665 ((srcval >> 2) & 0xf8) | /* g */
666 ((srcval >> 7) & 0x07),
667 ((srcval << 3) & 0xf8) | /* b */
668 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
677 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
678 dstbits += linebytes;
680 } else if (bmpImage->blue_mask==0x7c00) {
681 /* ==== bgr 555 bmp -> pal 1 dib ==== */
682 for (h=0; h<lines; h++) {
687 for (x=0; x<width; x++) {
690 dstval|=(X11DRV_DIB_GetNearestIndex
692 ((srcval << 3) & 0xf8) | /* r */
693 ((srcval >> 2) & 0x07),
694 ((srcval >> 2) & 0xf8) | /* g */
695 ((srcval >> 7) & 0x07),
696 ((srcval >> 7) & 0xf8) | /* b */
697 ((srcval >> 12) & 0x07) ) << (7-(x&7)) );
706 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
707 dstbits += linebytes;
712 } else if (bmpImage->green_mask==0x07e0) {
713 if (bmpImage->red_mask==0xf800) {
714 /* ==== rgb 565 bmp -> pal 1 dib ==== */
715 for (h=0; h<lines; h++) {
720 for (x=0; x<width; x++) {
723 dstval|=(X11DRV_DIB_GetNearestIndex
725 ((srcval >> 8) & 0xf8) | /* r */
726 ((srcval >> 13) & 0x07),
727 ((srcval >> 3) & 0xfc) | /* g */
728 ((srcval >> 9) & 0x03),
729 ((srcval << 3) & 0xf8) | /* b */
730 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
739 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
740 dstbits += linebytes;
742 } else if (bmpImage->blue_mask==0xf800) {
743 /* ==== bgr 565 bmp -> pal 1 dib ==== */
744 for (h=0; h<lines; h++) {
749 for (x=0; x<width; x++) {
752 dstval|=(X11DRV_DIB_GetNearestIndex
754 ((srcval << 3) & 0xf8) | /* r */
755 ((srcval >> 2) & 0x07),
756 ((srcval >> 3) & 0xfc) | /* g */
757 ((srcval >> 9) & 0x03),
758 ((srcval >> 8) & 0xf8) | /* b */
759 ((srcval >> 13) & 0x07) ) << (7-(x&7)) );
768 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
769 dstbits += linebytes;
788 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
789 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
791 if (bmpImage->green_mask!=0x00ff00 ||
792 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
794 } else if (bmpImage->blue_mask==0xff) {
795 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
796 for (h=0; h<lines; h++) {
801 for (x=0; x<width; x++) {
802 dstval|=(X11DRV_DIB_GetNearestIndex
806 srcbyte[0]) << (7-(x&7)) );
807 srcbyte+=bytes_per_pixel;
816 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
817 dstbits += linebytes;
820 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
821 for (h=0; h<lines; h++) {
826 for (x=0; x<width; x++) {
827 dstval|=(X11DRV_DIB_GetNearestIndex
831 srcbyte[2]) << (7-(x&7)) );
832 srcbyte+=bytes_per_pixel;
841 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
842 dstbits += linebytes;
853 unsigned long white = (1 << bmpImage->bits_per_pixel) - 1;
855 /* ==== any bmp format -> pal 1 dib ==== */
856 if ((unsigned)colors[0].rgbRed+colors[0].rgbGreen+colors[0].rgbBlue >=
857 (unsigned)colors[1].rgbRed+colors[1].rgbGreen+colors[1].rgbBlue )
860 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB, "
861 "%s color mapping\n",
862 bmpImage->bits_per_pixel, bmpImage->red_mask,
863 bmpImage->green_mask, bmpImage->blue_mask,
864 neg?"negative":"direct" );
866 for (h=lines-1; h>=0; h--) {
870 for (x=0; x<width; x++) {
871 dstval|=((XGetPixel( bmpImage, x, h) >= white) ^ neg) << (7 - (x&7));
880 dstbits += linebytes;
887 /***********************************************************************
888 * X11DRV_DIB_SetImageBits_4
890 * SetDIBits for a 4-bit deep DIB.
892 static void X11DRV_DIB_SetImageBits_4( int lines, const BYTE *srcbits,
893 DWORD srcwidth, DWORD dstwidth, int left,
894 int *colors, XImage *bmpImage, DWORD linebytes)
902 srcbits = srcbits + linebytes * (lines - 1);
903 linebytes = -linebytes;
910 srcbits += left >> 1;
911 width = min(srcwidth, dstwidth);
913 /* ==== pal 4 dib -> any bmp format ==== */
914 for (h = lines-1; h >= 0; h--) {
916 for (i = width/2, x = left; i > 0; i--) {
917 BYTE srcval=*srcbyte++;
918 XPutPixel( bmpImage, x++, h, colors[srcval >> 4] );
919 XPutPixel( bmpImage, x++, h, colors[srcval & 0x0f] );
922 XPutPixel( bmpImage, x, h, colors[*srcbyte >> 4] );
923 srcbits += linebytes;
929 /***********************************************************************
930 * X11DRV_DIB_GetImageBits_4
932 * GetDIBits for a 4-bit deep DIB.
934 static void X11DRV_DIB_GetImageBits_4( int lines, BYTE *dstbits,
935 DWORD srcwidth, DWORD dstwidth,
936 RGBQUAD *colors, PALETTEENTRY *srccolors,
937 XImage *bmpImage, DWORD linebytes )
940 int h, width = min(srcwidth, dstwidth);
946 dstbits = dstbits + ( linebytes * (lines-1) );
947 linebytes = -linebytes;
952 switch (bmpImage->depth) {
955 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
957 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
960 for (h = lines-1; h >= 0; h--) {
964 for (x = 0; x < width; x++) {
966 srcval=srccolors[XGetPixel(bmpImage, x, h)];
967 dstval|=(X11DRV_DIB_GetNearestIndex
971 srcval.peBlue) << (4-((x&1)<<2)));
980 dstbits += linebytes;
988 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
990 /* ==== pal 8 bmp -> pal 4 dib ==== */
992 const BYTE *srcpixel;
995 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
996 for (h=0; h<lines; h++) {
1001 for (x=0; x<width; x++) {
1002 PALETTEENTRY srcval;
1003 srcval = srccolors[*srcpixel++];
1004 dstval|=(X11DRV_DIB_GetNearestIndex
1008 srcval.peBlue) << (4*(1-(x&1))) );
1017 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1018 dstbits += linebytes;
1028 const void* srcbits;
1029 const WORD* srcpixel;
1032 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1034 if (bmpImage->green_mask==0x03e0) {
1035 if (bmpImage->red_mask==0x7c00) {
1036 /* ==== rgb 555 bmp -> pal 4 dib ==== */
1037 for (h=0; h<lines; h++) {
1042 for (x=0; x<width; x++) {
1045 dstval|=(X11DRV_DIB_GetNearestIndex
1047 ((srcval >> 7) & 0xf8) | /* r */
1048 ((srcval >> 12) & 0x07),
1049 ((srcval >> 2) & 0xf8) | /* g */
1050 ((srcval >> 7) & 0x07),
1051 ((srcval << 3) & 0xf8) | /* b */
1052 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1061 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1062 dstbits += linebytes;
1064 } else if (bmpImage->blue_mask==0x7c00) {
1065 /* ==== bgr 555 bmp -> pal 4 dib ==== */
1066 for (h=0; h<lines; h++) {
1071 for (x=0; x<width; x++) {
1074 dstval|=(X11DRV_DIB_GetNearestIndex
1076 ((srcval << 3) & 0xf8) | /* r */
1077 ((srcval >> 2) & 0x07),
1078 ((srcval >> 2) & 0xf8) | /* g */
1079 ((srcval >> 7) & 0x07),
1080 ((srcval >> 7) & 0xf8) | /* b */
1081 ((srcval >> 12) & 0x07) ) << ((1-(x&1))<<2) );
1090 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1091 dstbits += linebytes;
1096 } else if (bmpImage->green_mask==0x07e0) {
1097 if (bmpImage->red_mask==0xf800) {
1098 /* ==== rgb 565 bmp -> pal 4 dib ==== */
1099 for (h=0; h<lines; h++) {
1104 for (x=0; x<width; x++) {
1107 dstval|=(X11DRV_DIB_GetNearestIndex
1109 ((srcval >> 8) & 0xf8) | /* r */
1110 ((srcval >> 13) & 0x07),
1111 ((srcval >> 3) & 0xfc) | /* g */
1112 ((srcval >> 9) & 0x03),
1113 ((srcval << 3) & 0xf8) | /* b */
1114 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1123 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1124 dstbits += linebytes;
1126 } else if (bmpImage->blue_mask==0xf800) {
1127 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1128 for (h=0; h<lines; h++) {
1133 for (x=0; x<width; x++) {
1136 dstval|=(X11DRV_DIB_GetNearestIndex
1138 ((srcval << 3) & 0xf8) | /* r */
1139 ((srcval >> 2) & 0x07),
1140 ((srcval >> 3) & 0xfc) | /* g */
1141 ((srcval >> 9) & 0x03),
1142 ((srcval >> 8) & 0xf8) | /* b */
1143 ((srcval >> 13) & 0x07) ) << ((1-(x&1))<<2) );
1152 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1153 dstbits += linebytes;
1165 if (bmpImage->bits_per_pixel==24) {
1166 const void* srcbits;
1167 const BYTE *srcbyte;
1170 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1172 if (bmpImage->green_mask!=0x00ff00 ||
1173 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1175 } else if (bmpImage->blue_mask==0xff) {
1176 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1177 for (h=0; h<lines; h++) {
1180 for (x=0; x<width/2; x++) {
1181 /* Do 2 pixels at a time */
1182 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1187 X11DRV_DIB_GetNearestIndex
1195 /* And then the odd pixel */
1196 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1202 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1203 dstbits += linebytes;
1206 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1207 for (h=0; h<lines; h++) {
1210 for (x=0; x<width/2; x++) {
1211 /* Do 2 pixels at a time */
1212 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1217 X11DRV_DIB_GetNearestIndex
1225 /* And then the odd pixel */
1226 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1232 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1233 dstbits += linebytes;
1242 const void* srcbits;
1243 const BYTE *srcbyte;
1246 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1248 if (bmpImage->green_mask!=0x00ff00 ||
1249 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1251 } else if (bmpImage->blue_mask==0xff) {
1252 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1253 for (h=0; h<lines; h++) {
1256 for (x=0; x<width/2; x++) {
1257 /* Do 2 pixels at a time */
1258 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1263 X11DRV_DIB_GetNearestIndex
1271 /* And then the odd pixel */
1272 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1278 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1279 dstbits += linebytes;
1282 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1283 for (h=0; h<lines; h++) {
1286 for (x=0; x<width/2; x++) {
1287 /* Do 2 pixels at a time */
1288 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1293 X11DRV_DIB_GetNearestIndex
1301 /* And then the odd pixel */
1302 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1308 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1309 dstbits += linebytes;
1320 /* ==== any bmp format -> pal 4 dib ==== */
1321 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1322 bmpImage->bits_per_pixel, bmpImage->red_mask,
1323 bmpImage->green_mask, bmpImage->blue_mask );
1324 for (h=lines-1; h>=0; h--) {
1326 for (x=0; x<(width & ~1); x+=2) {
1327 *dstbyte++=(X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4) |
1328 X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x+1, h), 0);
1331 *dstbyte=(X11DRV_DIB_MapColor((int *)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4);
1333 dstbits += linebytes;
1340 /***********************************************************************
1341 * X11DRV_DIB_SetImageBits_RLE4
1343 * SetDIBits for a 4-bit deep compressed DIB.
1345 static void X11DRV_DIB_SetImageBits_RLE4( int lines, const BYTE *bits,
1346 DWORD srcwidth, DWORD dstwidth,
1347 int left, int *colors,
1350 unsigned int x = 0, width = min(srcwidth, dstwidth);
1351 int y = lines - 1, c, length;
1352 const BYTE *begin = bits;
1357 if (length) { /* encoded */
1360 if (x >= (left + width)) break;
1361 if( x >= left) XPutPixel(bmpImage, x, y, colors[c >> 4]);
1363 if (!length--) break;
1364 if (x >= (left + width)) break;
1365 if( x >= left) XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1385 default: /* absolute */
1388 if (x >= left && x < (left + width))
1389 XPutPixel(bmpImage, x, y, colors[c >> 4]);
1391 if (!length--) break;
1392 if (x >= left && x < (left + width))
1393 XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1396 if ((bits - begin) & 1)
1405 /***********************************************************************
1406 * X11DRV_DIB_SetImageBits_8
1408 * SetDIBits for an 8-bit deep DIB.
1410 static void X11DRV_DIB_SetImageBits_8( int lines, const BYTE *srcbits,
1411 DWORD srcwidth, DWORD dstwidth, int left,
1412 const int *colors, XImage *bmpImage,
1416 int h, width = min(srcwidth, dstwidth);
1417 const BYTE* srcbyte;
1423 srcbits = srcbits + linebytes * (lines-1);
1424 linebytes = -linebytes;
1429 switch (bmpImage->depth) {
1432 /* Some X servers might have 32 bit/ 16bit deep pixel */
1433 if (lines && width && (bmpImage->bits_per_pixel == 16) &&
1434 (ImageByteOrder(gdi_display)==LSBFirst) )
1436 /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1437 dstbits=(BYTE*)bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1438 for (h = lines ; h--; ) {
1439 #if defined(__i386__) && defined(__GNUC__)
1440 int _cl1,_cl2; /* temp outputs for asm below */
1441 /* Borrowed from DirectDraw */
1442 __asm__ __volatile__(
1447 " movw (%%edx,%%eax,4),%%ax\n"
1449 " xor %%eax,%%eax\n"
1451 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1456 :"eax", "cc", "memory"
1459 DWORD* dstpixel=(DWORD*)dstbits;
1460 for (x=0; x<width/2; x++) {
1461 /* Do 2 pixels at a time */
1462 *dstpixel++=(colors[srcbyte[1]] << 16) | colors[srcbyte[0]];
1466 /* And then the odd pixel */
1467 *((WORD*)dstpixel)=colors[srcbyte[0]];
1470 srcbyte = (srcbits += linebytes);
1471 dstbits -= bmpImage->bytes_per_line;
1478 if (lines && width && (bmpImage->bits_per_pixel == 32) &&
1479 (ImageByteOrder(gdi_display)==LSBFirst) )
1481 dstbits=(BYTE*)bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
1482 /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1483 for (h = lines ; h--; ) {
1484 #if defined(__i386__) && defined(__GNUC__)
1485 int _cl1,_cl2; /* temp outputs for asm below */
1486 /* Borrowed from DirectDraw */
1487 __asm__ __volatile__(
1492 " movl (%%edx,%%eax,4),%%eax\n"
1494 " xor %%eax,%%eax\n"
1496 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1501 :"eax", "cc", "memory"
1504 DWORD* dstpixel=(DWORD*)dstbits;
1505 for (x=0; x<width; x++) {
1506 *dstpixel++=colors[*srcbyte++];
1509 srcbyte = (srcbits += linebytes);
1510 dstbits -= bmpImage->bytes_per_line;
1516 break; /* use slow generic case below */
1519 /* ==== pal 8 dib -> any bmp format ==== */
1520 for (h=lines-1; h>=0; h--) {
1521 for (x=left; x<width+left; x++) {
1522 XPutPixel(bmpImage, x, h, colors[*srcbyte++]);
1524 srcbyte = (srcbits += linebytes);
1528 /***********************************************************************
1529 * X11DRV_DIB_GetImageBits_8
1531 * GetDIBits for an 8-bit deep DIB.
1533 static void X11DRV_DIB_GetImageBits_8( int lines, BYTE *dstbits,
1534 DWORD srcwidth, DWORD dstwidth,
1535 RGBQUAD *colors, PALETTEENTRY *srccolors,
1536 XImage *bmpImage, DWORD linebytes )
1539 int h, width = min(srcwidth, dstwidth);
1545 dstbits = dstbits + ( linebytes * (lines-1) );
1546 linebytes = -linebytes;
1551 * This condition is true when GetImageBits has been called by
1552 * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1553 * 256 colormaps, so we'll just use it for GetDIBits calls.
1554 * (In some cases, in an updateDIBSection, the returned colors are bad too)
1556 if (!srccolors) goto updatesection;
1558 switch (bmpImage->depth) {
1561 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1564 /* ==== pal 1 bmp -> pal 8 dib ==== */
1565 /* ==== pal 4 bmp -> pal 8 dib ==== */
1566 for (h=lines-1; h>=0; h--) {
1568 for (x=0; x<width; x++) {
1569 PALETTEENTRY srcval;
1570 srcval=srccolors[XGetPixel(bmpImage, x, h)];
1571 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1576 dstbits += linebytes;
1584 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1586 /* ==== pal 8 bmp -> pal 8 dib ==== */
1587 const void* srcbits;
1588 const BYTE* srcpixel;
1590 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1591 for (h=0; h<lines; h++) {
1594 for (x = 0; x < width; x++) {
1595 PALETTEENTRY srcval;
1596 srcval=srccolors[*srcpixel++];
1597 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1602 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1603 dstbits += linebytes;
1613 const void* srcbits;
1614 const WORD* srcpixel;
1617 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1619 if (bmpImage->green_mask==0x03e0) {
1620 if (bmpImage->red_mask==0x7c00) {
1621 /* ==== rgb 555 bmp -> pal 8 dib ==== */
1622 for (h=0; h<lines; h++) {
1625 for (x=0; x<width; x++) {
1628 *dstbyte++=X11DRV_DIB_GetNearestIndex
1630 ((srcval >> 7) & 0xf8) | /* r */
1631 ((srcval >> 12) & 0x07),
1632 ((srcval >> 2) & 0xf8) | /* g */
1633 ((srcval >> 7) & 0x07),
1634 ((srcval << 3) & 0xf8) | /* b */
1635 ((srcval >> 2) & 0x07) );
1637 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1638 dstbits += linebytes;
1640 } else if (bmpImage->blue_mask==0x7c00) {
1641 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1642 for (h=0; h<lines; h++) {
1645 for (x=0; x<width; x++) {
1648 *dstbyte++=X11DRV_DIB_GetNearestIndex
1650 ((srcval << 3) & 0xf8) | /* r */
1651 ((srcval >> 2) & 0x07),
1652 ((srcval >> 2) & 0xf8) | /* g */
1653 ((srcval >> 7) & 0x07),
1654 ((srcval >> 7) & 0xf8) | /* b */
1655 ((srcval >> 12) & 0x07) );
1657 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1658 dstbits += linebytes;
1663 } else if (bmpImage->green_mask==0x07e0) {
1664 if (bmpImage->red_mask==0xf800) {
1665 /* ==== rgb 565 bmp -> pal 8 dib ==== */
1666 for (h=0; h<lines; h++) {
1669 for (x=0; x<width; x++) {
1672 *dstbyte++=X11DRV_DIB_GetNearestIndex
1674 ((srcval >> 8) & 0xf8) | /* r */
1675 ((srcval >> 13) & 0x07),
1676 ((srcval >> 3) & 0xfc) | /* g */
1677 ((srcval >> 9) & 0x03),
1678 ((srcval << 3) & 0xf8) | /* b */
1679 ((srcval >> 2) & 0x07) );
1681 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1682 dstbits += linebytes;
1684 } else if (bmpImage->blue_mask==0xf800) {
1685 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1686 for (h=0; h<lines; h++) {
1689 for (x=0; x<width; x++) {
1692 *dstbyte++=X11DRV_DIB_GetNearestIndex
1694 ((srcval << 3) & 0xf8) | /* r */
1695 ((srcval >> 2) & 0x07),
1696 ((srcval >> 3) & 0xfc) | /* g */
1697 ((srcval >> 9) & 0x03),
1698 ((srcval >> 8) & 0xf8) | /* b */
1699 ((srcval >> 13) & 0x07) );
1701 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1702 dstbits += linebytes;
1716 const void* srcbits;
1717 const BYTE *srcbyte;
1719 int bytes_per_pixel;
1721 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1722 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
1724 if (bmpImage->green_mask!=0x00ff00 ||
1725 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1727 } else if (bmpImage->blue_mask==0xff) {
1728 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1729 for (h=0; h<lines; h++) {
1732 for (x=0; x<width; x++) {
1733 *dstbyte++=X11DRV_DIB_GetNearestIndex
1738 srcbyte+=bytes_per_pixel;
1740 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1741 dstbits += linebytes;
1744 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1745 for (h=0; h<lines; h++) {
1748 for (x=0; x<width; x++) {
1749 *dstbyte++=X11DRV_DIB_GetNearestIndex
1754 srcbyte+=bytes_per_pixel;
1756 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1757 dstbits += linebytes;
1765 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1766 bmpImage->depth, bmpImage->red_mask,
1767 bmpImage->green_mask, bmpImage->blue_mask );
1769 /* ==== any bmp format -> pal 8 dib ==== */
1770 for (h=lines-1; h>=0; h--) {
1772 for (x=0; x<width; x++) {
1773 *dstbyte=X11DRV_DIB_MapColor
1775 XGetPixel(bmpImage, x, h), *dstbyte);
1778 dstbits += linebytes;
1784 /***********************************************************************
1785 * X11DRV_DIB_SetImageBits_RLE8
1787 * SetDIBits for an 8-bit deep compressed DIB.
1789 * This function rewritten 941113 by James Youngman. WINE blew out when I
1790 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
1792 * This was because the algorithm assumed that all RLE8 bitmaps end with the
1793 * 'End of bitmap' escape code. This code is very much laxer in what it
1794 * allows to end the expansion. Possibly too lax. See the note by
1795 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
1796 * bitmap should end with RleEnd, but on the other hand, software exists
1797 * that produces ones that don't and Windows 3.1 doesn't complain a bit
1800 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
1801 * James A. Youngman <mbcstjy@afs.man.ac.uk>
1804 static void X11DRV_DIB_SetImageBits_RLE8( int lines, const BYTE *bits,
1805 DWORD srcwidth, DWORD dstwidth,
1806 int left, int *colors,
1809 unsigned int x; /* X-position on each line. Increases. */
1810 int y; /* Line #. Starts at lines-1, decreases */
1811 const BYTE *pIn = bits; /* Pointer to current position in bits */
1812 BYTE length; /* The length pf a run */
1813 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
1816 * Note that the bitmap data is stored by Windows starting at the
1817 * bottom line of the bitmap and going upwards. Within each line,
1818 * the data is stored left-to-right. That's the reason why line
1819 * goes from lines-1 to 0. [JAY]
1829 * If the length byte is not zero (which is the escape value),
1830 * We have a run of length pixels all the same colour. The colour
1831 * index is stored next.
1833 * If the length byte is zero, we need to read the next byte to
1834 * know what to do. [JAY]
1839 * [Run-Length] Encoded mode
1841 int color = colors[*pIn++];
1842 while (length-- && x < (left + dstwidth)) {
1843 if( x >= left) XPutPixel(bmpImage, x, y, color);
1850 * Escape codes (may be an absolute sequence though)
1852 escape_code = (*pIn++);
1861 /* Not all RLE8 bitmaps end with this code. For
1862 * example, Paint Shop Pro produces some that don't.
1863 * That's (I think) what caused the previous
1864 * implementation to fail. [JAY]
1873 default: /* switch to absolute mode */
1874 length = escape_code;
1877 int color = colors[*pIn++];
1878 if (x >= (left + dstwidth))
1883 if( x >= left) XPutPixel(bmpImage, x, y, color);
1887 * If you think for a moment you'll realise that the
1888 * only time we could ever possibly read an odd
1889 * number of bytes is when there is a 0x00 (escape),
1890 * a value >0x02 (absolute mode) and then an odd-
1891 * length run. Therefore this is the only place we
1892 * need to worry about it. Everywhere else the
1893 * bytes are always read in pairs. [JAY]
1895 if (escape_code & 1) pIn++; /* Throw away the pad byte. */
1897 } /* switch (escape_code) : Escape sequence */
1903 /***********************************************************************
1904 * X11DRV_DIB_SetImageBits_16
1906 * SetDIBits for a 16-bit deep DIB.
1908 static void X11DRV_DIB_SetImageBits_16( int lines, const BYTE *srcbits,
1909 DWORD srcwidth, DWORD dstwidth, int left,
1910 X11DRV_PDEVICE *physDev, DWORD rSrc, DWORD gSrc, DWORD bSrc,
1911 XImage *bmpImage, DWORD linebytes )
1914 int h, width = min(srcwidth, dstwidth);
1915 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
1920 srcbits = srcbits + ( linebytes * (lines-1));
1921 linebytes = -linebytes;
1924 switch (bmpImage->depth)
1931 srcbits=srcbits+left*2;
1932 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1934 if (bmpImage->green_mask==0x03e0) {
1935 if (gSrc==bmpImage->green_mask) {
1936 if (rSrc==bmpImage->red_mask) {
1937 /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1938 /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1939 convs->Convert_5x5_asis
1942 dstbits,-bmpImage->bytes_per_line);
1943 } else if (rSrc==bmpImage->blue_mask) {
1944 /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1945 /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1946 convs->Convert_555_reverse
1949 dstbits,-bmpImage->bytes_per_line);
1952 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1953 /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1954 /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1955 convs->Convert_565_to_555_asis
1958 dstbits,-bmpImage->bytes_per_line);
1960 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1961 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1962 convs->Convert_565_to_555_reverse
1965 dstbits,-bmpImage->bytes_per_line);
1968 } else if (bmpImage->green_mask==0x07e0) {
1969 if (gSrc==bmpImage->green_mask) {
1970 if (rSrc==bmpImage->red_mask) {
1971 /* ==== rgb 565 dib -> rgb 565 bmp ==== */
1972 /* ==== bgr 565 dib -> bgr 565 bmp ==== */
1973 convs->Convert_5x5_asis
1976 dstbits,-bmpImage->bytes_per_line);
1978 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
1979 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
1980 convs->Convert_565_reverse
1983 dstbits,-bmpImage->bytes_per_line);
1986 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1987 /* ==== rgb 555 dib -> rgb 565 bmp ==== */
1988 /* ==== bgr 555 dib -> bgr 565 bmp ==== */
1989 convs->Convert_555_to_565_asis
1992 dstbits,-bmpImage->bytes_per_line);
1994 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
1995 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
1996 convs->Convert_555_to_565_reverse
1999 dstbits,-bmpImage->bytes_per_line);
2009 if (bmpImage->bits_per_pixel==24) {
2012 srcbits=srcbits+left*2;
2013 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2015 if (bmpImage->green_mask!=0x00ff00 ||
2016 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2018 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2019 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2021 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
2022 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
2023 convs->Convert_555_to_888_asis
2026 dstbits,-bmpImage->bytes_per_line);
2028 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
2029 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
2030 convs->Convert_565_to_888_asis
2033 dstbits,-bmpImage->bytes_per_line);
2037 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
2038 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
2039 convs->Convert_555_to_888_reverse
2042 dstbits,-bmpImage->bytes_per_line);
2044 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
2045 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
2046 convs->Convert_565_to_888_reverse
2049 dstbits,-bmpImage->bytes_per_line);
2060 srcbits=srcbits+left*2;
2061 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2063 if (bmpImage->green_mask!=0x00ff00 ||
2064 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2066 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2067 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2069 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
2070 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
2071 convs->Convert_555_to_0888_asis
2074 dstbits,-bmpImage->bytes_per_line);
2076 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
2077 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
2078 convs->Convert_565_to_0888_asis
2081 dstbits,-bmpImage->bytes_per_line);
2085 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
2086 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
2087 convs->Convert_555_to_0888_reverse
2090 dstbits,-bmpImage->bytes_per_line);
2092 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
2093 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
2094 convs->Convert_565_to_0888_reverse
2097 dstbits,-bmpImage->bytes_per_line);
2105 WARN("from 16 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2106 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2107 bmpImage->green_mask, bmpImage->blue_mask );
2113 /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
2114 const WORD* srcpixel;
2115 int rShift1,gShift1,bShift1;
2116 int rShift2,gShift2,bShift2;
2119 /* Set color scaling values */
2120 rShift1=16+X11DRV_DIB_MaskToShift(rSrc)-3;
2121 gShift1=16+X11DRV_DIB_MaskToShift(gSrc)-3;
2122 bShift1=16+X11DRV_DIB_MaskToShift(bSrc)-3;
2127 /* Green has 5 bits, like the others */
2131 /* Green has 6 bits, not 5. Compensate. */
2140 /* We could split it into four separate cases to optimize
2141 * but it is probably not worth it.
2143 for (h=lines-1; h>=0; h--) {
2144 srcpixel=(const WORD*)srcbits;
2145 for (x=left; x<width+left; x++) {
2147 BYTE red,green,blue;
2148 srcval=*srcpixel++ << 16;
2149 red= ((srcval >> rShift1) & 0xf8) |
2150 ((srcval >> rShift2) & 0x07);
2151 green=((srcval >> gShift1) & gMask1) |
2152 ((srcval >> gShift2) & gMask2);
2153 blue= ((srcval >> bShift1) & 0xf8) |
2154 ((srcval >> bShift2) & 0x07);
2155 XPutPixel(bmpImage, x, h,
2156 X11DRV_PALETTE_ToPhysical
2157 (physDev, RGB(red,green,blue)));
2159 srcbits += linebytes;
2167 /***********************************************************************
2168 * X11DRV_DIB_GetImageBits_16
2170 * GetDIBits for an 16-bit deep DIB.
2172 static void X11DRV_DIB_GetImageBits_16( int lines, BYTE *dstbits,
2173 DWORD dstwidth, DWORD srcwidth,
2174 PALETTEENTRY *srccolors,
2175 DWORD rDst, DWORD gDst, DWORD bDst,
2176 XImage *bmpImage, DWORD dibpitch )
2179 int h, width = min(srcwidth, dstwidth);
2180 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2182 DWORD linebytes = dibpitch;
2187 dstbits = dstbits + ( linebytes * (lines-1));
2188 linebytes = -linebytes;
2191 switch (bmpImage->depth)
2196 const char* srcbits;
2198 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2200 if (bmpImage->green_mask==0x03e0) {
2201 if (gDst==bmpImage->green_mask) {
2202 if (rDst==bmpImage->red_mask) {
2203 /* ==== rgb 555 bmp -> rgb 555 dib ==== */
2204 /* ==== bgr 555 bmp -> bgr 555 dib ==== */
2205 convs->Convert_5x5_asis
2207 srcbits,-bmpImage->bytes_per_line,
2210 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
2211 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
2212 convs->Convert_555_reverse
2214 srcbits,-bmpImage->bytes_per_line,
2218 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2219 /* ==== rgb 555 bmp -> rgb 565 dib ==== */
2220 /* ==== bgr 555 bmp -> bgr 565 dib ==== */
2221 convs->Convert_555_to_565_asis
2223 srcbits,-bmpImage->bytes_per_line,
2226 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
2227 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
2228 convs->Convert_555_to_565_reverse
2230 srcbits,-bmpImage->bytes_per_line,
2234 } else if (bmpImage->green_mask==0x07e0) {
2235 if (gDst==bmpImage->green_mask) {
2236 if (rDst == bmpImage->red_mask) {
2237 /* ==== rgb 565 bmp -> rgb 565 dib ==== */
2238 /* ==== bgr 565 bmp -> bgr 565 dib ==== */
2239 convs->Convert_5x5_asis
2241 srcbits,-bmpImage->bytes_per_line,
2244 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
2245 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
2246 convs->Convert_565_reverse
2248 srcbits,-bmpImage->bytes_per_line,
2252 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2253 /* ==== rgb 565 bmp -> rgb 555 dib ==== */
2254 /* ==== bgr 565 bmp -> bgr 555 dib ==== */
2255 convs->Convert_565_to_555_asis
2257 srcbits,-bmpImage->bytes_per_line,
2260 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
2261 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
2262 convs->Convert_565_to_555_reverse
2264 srcbits,-bmpImage->bytes_per_line,
2275 if (bmpImage->bits_per_pixel == 24) {
2276 const char* srcbits;
2278 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2280 if (bmpImage->green_mask!=0x00ff00 ||
2281 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2283 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2284 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2286 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
2287 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
2288 convs->Convert_888_to_555_asis
2290 srcbits,-bmpImage->bytes_per_line,
2293 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2294 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2295 convs->Convert_888_to_565_asis
2297 srcbits,-bmpImage->bytes_per_line,
2302 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
2303 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
2304 convs->Convert_888_to_555_reverse
2306 srcbits,-bmpImage->bytes_per_line,
2309 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
2310 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
2311 convs->Convert_888_to_565_reverse
2313 srcbits,-bmpImage->bytes_per_line,
2323 const char* srcbits;
2325 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2327 if (bmpImage->green_mask!=0x00ff00 ||
2328 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2330 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2331 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2333 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2334 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2335 convs->Convert_0888_to_555_asis
2337 srcbits,-bmpImage->bytes_per_line,
2340 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2341 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2342 convs->Convert_0888_to_565_asis
2344 srcbits,-bmpImage->bytes_per_line,
2349 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2350 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2351 convs->Convert_0888_to_555_reverse
2353 srcbits,-bmpImage->bytes_per_line,
2356 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2357 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2358 convs->Convert_0888_to_565_reverse
2360 srcbits,-bmpImage->bytes_per_line,
2369 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2371 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2372 int rShift,gShift,bShift;
2375 /* Shift everything 16 bits left so that all shifts are >0,
2376 * even for BGR DIBs. Then a single >> 16 will bring everything
2379 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2380 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2381 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2383 /* 6 bits for the green */
2389 for (h = lines - 1; h >= 0; h--) {
2390 dstpixel=(LPWORD)dstbits;
2391 for (x = 0; x < width; x++) {
2392 PALETTEENTRY srcval;
2394 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2395 dstval=((srcval.peRed << rShift) & rDst) |
2396 ((srcval.peGreen << gShift) & gDst) |
2397 ((srcval.peBlue << bShift) & bDst);
2398 *dstpixel++=dstval >> 16;
2400 dstbits += linebytes;
2408 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2410 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2411 int rShift,gShift,bShift;
2412 const BYTE* srcbits;
2413 const BYTE* srcpixel;
2416 /* Shift everything 16 bits left so that all shifts are >0,
2417 * even for BGR DIBs. Then a single >> 16 will bring everything
2420 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2421 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2422 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2424 /* 6 bits for the green */
2430 srcbits=(BYTE*)bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2431 for (h=0; h<lines; h++) {
2433 dstpixel=(LPWORD)dstbits;
2434 for (x = 0; x < width; x++) {
2435 PALETTEENTRY srcval;
2437 srcval=srccolors[*srcpixel++];
2438 dstval=((srcval.peRed << rShift) & rDst) |
2439 ((srcval.peGreen << gShift) & gDst) |
2440 ((srcval.peBlue << bShift) & bDst);
2441 *dstpixel++=dstval >> 16;
2443 srcbits -= bmpImage->bytes_per_line;
2444 dstbits += linebytes;
2454 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2455 int rShift,gShift,bShift;
2458 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%x,%x,%x)\n",
2459 bmpImage->depth, bmpImage->red_mask,
2460 bmpImage->green_mask, bmpImage->blue_mask,
2463 /* Shift everything 16 bits left so that all shifts are >0,
2464 * even for BGR DIBs. Then a single >> 16 will bring everything
2467 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2468 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2469 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2471 /* 6 bits for the green */
2477 for (h = lines - 1; h >= 0; h--) {
2478 dstpixel=(LPWORD)dstbits;
2479 for (x = 0; x < width; x++) {
2482 srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
2483 dstval=((GetRValue(srcval) << rShift) & rDst) |
2484 ((GetGValue(srcval) << gShift) & gDst) |
2485 ((GetBValue(srcval) << bShift) & bDst);
2486 *dstpixel++=dstval >> 16;
2488 dstbits += linebytes;
2496 /***********************************************************************
2497 * X11DRV_DIB_SetImageBits_24
2499 * SetDIBits for a 24-bit deep DIB.
2501 static void X11DRV_DIB_SetImageBits_24( int lines, const BYTE *srcbits,
2502 DWORD srcwidth, DWORD dstwidth, int left,
2503 X11DRV_PDEVICE *physDev,
2504 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2505 XImage *bmpImage, DWORD linebytes )
2508 int h, width = min(srcwidth, dstwidth);
2509 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2514 srcbits = srcbits + linebytes * (lines - 1);
2515 linebytes = -linebytes;
2518 switch (bmpImage->depth)
2521 if (bmpImage->bits_per_pixel==24) {
2524 srcbits=srcbits+left*3;
2525 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2527 if (bmpImage->green_mask!=0x00ff00 ||
2528 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2530 } else if (rSrc==bmpImage->red_mask) {
2531 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2532 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2533 convs->Convert_888_asis
2536 dstbits,-bmpImage->bytes_per_line);
2538 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2539 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2540 convs->Convert_888_reverse
2543 dstbits,-bmpImage->bytes_per_line);
2553 srcbits=srcbits+left*3;
2554 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2556 if (bmpImage->green_mask!=0x00ff00 ||
2557 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2559 } else if (rSrc==bmpImage->red_mask) {
2560 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2561 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2562 convs->Convert_888_to_0888_asis
2565 dstbits,-bmpImage->bytes_per_line);
2567 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2568 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2569 convs->Convert_888_to_0888_reverse
2572 dstbits,-bmpImage->bytes_per_line);
2582 srcbits=srcbits+left*3;
2583 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
2585 if (bmpImage->green_mask==0x03e0) {
2586 if ((rSrc==0xff0000 && bmpImage->red_mask==0x7f00) ||
2587 (bSrc==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2588 /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2589 /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2590 convs->Convert_888_to_555_asis
2593 dstbits,-bmpImage->bytes_per_line);
2594 } else if ((rSrc==0xff && bmpImage->red_mask==0x7f00) ||
2595 (bSrc==0xff && bmpImage->blue_mask==0x7f00)) {
2596 /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2597 /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2598 convs->Convert_888_to_555_reverse
2601 dstbits,-bmpImage->bytes_per_line);
2605 } else if (bmpImage->green_mask==0x07e0) {
2606 if ((rSrc==0xff0000 && bmpImage->red_mask==0xf800) ||
2607 (bSrc==0xff0000 && bmpImage->blue_mask==0xf800)) {
2608 /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2609 /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2610 convs->Convert_888_to_565_asis
2613 dstbits,-bmpImage->bytes_per_line);
2614 } else if ((rSrc==0xff && bmpImage->red_mask==0xf800) ||
2615 (bSrc==0xff && bmpImage->blue_mask==0xf800)) {
2616 /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2617 /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2618 convs->Convert_888_to_565_reverse
2621 dstbits,-bmpImage->bytes_per_line);
2633 WARN("from 24 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2634 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2635 bmpImage->green_mask, bmpImage->blue_mask );
2641 /* ==== rgb 888 dib -> any bmp format ==== */
2642 const BYTE* srcbyte;
2644 /* Windows only supports one 24bpp DIB format: RGB888 */
2646 for (h = lines - 1; h >= 0; h--) {
2648 for (x = left; x < width+left; x++) {
2649 XPutPixel(bmpImage, x, h,
2650 X11DRV_PALETTE_ToPhysical
2651 (physDev, RGB(srcbyte[2], srcbyte[1], srcbyte[0])));
2654 srcbits += linebytes;
2662 /***********************************************************************
2663 * X11DRV_DIB_GetImageBits_24
2665 * GetDIBits for an 24-bit deep DIB.
2667 static void X11DRV_DIB_GetImageBits_24( int lines, BYTE *dstbits,
2668 DWORD dstwidth, DWORD srcwidth,
2669 PALETTEENTRY *srccolors,
2670 DWORD rDst, DWORD gDst, DWORD bDst,
2671 XImage *bmpImage, DWORD linebytes )
2674 int h, width = min(srcwidth, dstwidth);
2675 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2680 dstbits = dstbits + ( linebytes * (lines-1) );
2681 linebytes = -linebytes;
2684 switch (bmpImage->depth)
2687 if (bmpImage->bits_per_pixel==24) {
2688 const char* srcbits;
2690 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2692 if (bmpImage->green_mask!=0x00ff00 ||
2693 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2695 } else if (rDst==bmpImage->red_mask) {
2696 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2697 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2698 convs->Convert_888_asis
2700 srcbits,-bmpImage->bytes_per_line,
2703 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2704 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2705 convs->Convert_888_reverse
2707 srcbits,-bmpImage->bytes_per_line,
2716 const char* srcbits;
2718 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2720 if (bmpImage->green_mask!=0x00ff00 ||
2721 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2723 } else if (rDst==bmpImage->red_mask) {
2724 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2725 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2726 convs->Convert_0888_to_888_asis
2728 srcbits,-bmpImage->bytes_per_line,
2731 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2732 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2733 convs->Convert_0888_to_888_reverse
2735 srcbits,-bmpImage->bytes_per_line,
2744 const char* srcbits;
2746 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2748 if (bmpImage->green_mask==0x03e0) {
2749 if ((rDst==0xff0000 && bmpImage->red_mask==0x7f00) ||
2750 (bDst==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2751 /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2752 /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2753 convs->Convert_555_to_888_asis
2755 srcbits,-bmpImage->bytes_per_line,
2757 } else if ((rDst==0xff && bmpImage->red_mask==0x7f00) ||
2758 (bDst==0xff && bmpImage->blue_mask==0x7f00)) {
2759 /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2760 /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2761 convs->Convert_555_to_888_reverse
2763 srcbits,-bmpImage->bytes_per_line,
2768 } else if (bmpImage->green_mask==0x07e0) {
2769 if ((rDst==0xff0000 && bmpImage->red_mask==0xf800) ||
2770 (bDst==0xff0000 && bmpImage->blue_mask==0xf800)) {
2771 /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2772 /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2773 convs->Convert_565_to_888_asis
2775 srcbits,-bmpImage->bytes_per_line,
2777 } else if ((rDst==0xff && bmpImage->red_mask==0xf800) ||
2778 (bDst==0xff && bmpImage->blue_mask==0xf800)) {
2779 /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2780 /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2781 convs->Convert_565_to_888_reverse
2783 srcbits,-bmpImage->bytes_per_line,
2796 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2798 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2801 /* Windows only supports one 24bpp DIB format: rgb 888 */
2802 for (h = lines - 1; h >= 0; h--) {
2804 for (x = 0; x < width; x++) {
2805 PALETTEENTRY srcval;
2806 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2807 dstbyte[0]=srcval.peBlue;
2808 dstbyte[1]=srcval.peGreen;
2809 dstbyte[2]=srcval.peRed;
2812 dstbits += linebytes;
2820 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2822 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2823 const void* srcbits;
2824 const BYTE* srcpixel;
2827 /* Windows only supports one 24bpp DIB format: rgb 888 */
2828 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2829 for (h = lines - 1; h >= 0; h--) {
2832 for (x = 0; x < width; x++ ) {
2833 PALETTEENTRY srcval;
2834 srcval=srccolors[*srcpixel++];
2835 dstbyte[0]=srcval.peBlue;
2836 dstbyte[1]=srcval.peGreen;
2837 dstbyte[2]=srcval.peRed;
2840 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
2841 dstbits += linebytes;
2851 /* ==== any bmp format -> 888 dib ==== */
2854 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%x,%x,%x)\n",
2855 bmpImage->depth, bmpImage->red_mask,
2856 bmpImage->green_mask, bmpImage->blue_mask,
2859 /* Windows only supports one 24bpp DIB format: rgb 888 */
2860 for (h = lines - 1; h >= 0; h--) {
2862 for (x = 0; x < width; x++) {
2863 COLORREF srcval=X11DRV_PALETTE_ToLogical
2864 (XGetPixel( bmpImage, x, h ));
2865 dstbyte[0]=GetBValue(srcval);
2866 dstbyte[1]=GetGValue(srcval);
2867 dstbyte[2]=GetRValue(srcval);
2870 dstbits += linebytes;
2878 /***********************************************************************
2879 * X11DRV_DIB_SetImageBits_32
2881 * SetDIBits for a 32-bit deep DIB.
2883 static void X11DRV_DIB_SetImageBits_32(int lines, const BYTE *srcbits,
2884 DWORD srcwidth, DWORD dstwidth, int left,
2885 X11DRV_PDEVICE *physDev,
2886 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2891 int h, width = min(srcwidth, dstwidth);
2892 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2897 srcbits = srcbits + ( linebytes * (lines-1) );
2898 linebytes = -linebytes;
2901 switch (bmpImage->depth)
2904 if (bmpImage->bits_per_pixel==24) {
2907 srcbits=srcbits+left*4;
2908 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2910 if (rSrc==bmpImage->red_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->blue_mask) {
2911 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2912 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2913 convs->Convert_0888_to_888_asis
2916 dstbits,-bmpImage->bytes_per_line);
2917 } else if (bmpImage->green_mask!=0x00ff00 ||
2918 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2920 /* the tests below assume sane bmpImage masks */
2921 } else if (rSrc==bmpImage->blue_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->red_mask) {
2922 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2923 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2924 convs->Convert_0888_to_888_reverse
2927 dstbits,-bmpImage->bytes_per_line);
2928 } else if (bmpImage->blue_mask==0xff) {
2929 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2930 convs->Convert_any0888_to_rgb888
2934 dstbits,-bmpImage->bytes_per_line);
2936 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2937 convs->Convert_any0888_to_bgr888
2941 dstbits,-bmpImage->bytes_per_line);
2951 srcbits=srcbits+left*4;
2952 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2954 if (gSrc==bmpImage->green_mask) {
2955 if (rSrc==bmpImage->red_mask && bSrc==bmpImage->blue_mask) {
2956 /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2957 /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2958 convs->Convert_0888_asis
2961 dstbits,-bmpImage->bytes_per_line);
2962 } else if (bmpImage->green_mask!=0x00ff00 ||
2963 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2965 /* the tests below assume sane bmpImage masks */
2966 } else if (rSrc==bmpImage->blue_mask && bSrc==bmpImage->red_mask) {
2967 /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2968 /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2969 convs->Convert_0888_reverse
2972 dstbits,-bmpImage->bytes_per_line);
2974 /* ==== any 0888 dib -> any 0888 bmp ==== */
2975 convs->Convert_0888_any
2979 dstbits,-bmpImage->bytes_per_line,
2980 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
2982 } else if (bmpImage->green_mask!=0x00ff00 ||
2983 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2985 /* the tests below assume sane bmpImage masks */
2987 /* ==== any 0888 dib -> any 0888 bmp ==== */
2988 convs->Convert_0888_any
2992 dstbits,-bmpImage->bytes_per_line,
2993 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3003 srcbits=srcbits+left*4;
3004 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
3006 if (rSrc==0xff0000 && gSrc==0x00ff00 && bSrc==0x0000ff) {
3007 if (bmpImage->green_mask==0x03e0) {
3008 if (bmpImage->red_mask==0x7f00) {
3009 /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
3010 convs->Convert_0888_to_555_asis
3013 dstbits,-bmpImage->bytes_per_line);
3014 } else if (bmpImage->blue_mask==0x7f00) {
3015 /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
3016 convs->Convert_0888_to_555_reverse
3019 dstbits,-bmpImage->bytes_per_line);
3023 } else if (bmpImage->green_mask==0x07e0) {
3024 if (bmpImage->red_mask==0xf800) {
3025 /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
3026 convs->Convert_0888_to_565_asis
3029 dstbits,-bmpImage->bytes_per_line);
3030 } else if (bmpImage->blue_mask==0xf800) {
3031 /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
3032 convs->Convert_0888_to_565_reverse
3035 dstbits,-bmpImage->bytes_per_line);
3042 } else if (rSrc==0x0000ff && gSrc==0x00ff00 && bSrc==0xff0000) {
3043 if (bmpImage->green_mask==0x03e0) {
3044 if (bmpImage->blue_mask==0x7f00) {
3045 /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
3046 convs->Convert_0888_to_555_asis
3049 dstbits,-bmpImage->bytes_per_line);
3050 } else if (bmpImage->red_mask==0x7f00) {
3051 /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
3052 convs->Convert_0888_to_555_reverse
3055 dstbits,-bmpImage->bytes_per_line);
3059 } else if (bmpImage->green_mask==0x07e0) {
3060 if (bmpImage->blue_mask==0xf800) {
3061 /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
3062 convs->Convert_0888_to_565_asis
3065 dstbits,-bmpImage->bytes_per_line);
3066 } else if (bmpImage->red_mask==0xf800) {
3067 /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
3068 convs->Convert_0888_to_565_reverse
3071 dstbits,-bmpImage->bytes_per_line);
3079 if (bmpImage->green_mask==0x03e0 &&
3080 (bmpImage->red_mask==0x7f00 ||
3081 bmpImage->blue_mask==0x7f00)) {
3082 /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
3083 convs->Convert_any0888_to_5x5
3087 dstbits,-bmpImage->bytes_per_line,
3088 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3089 } else if (bmpImage->green_mask==0x07e0 &&
3090 (bmpImage->red_mask==0xf800 ||
3091 bmpImage->blue_mask==0xf800)) {
3092 /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
3093 convs->Convert_any0888_to_5x5
3097 dstbits,-bmpImage->bytes_per_line,
3098 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3108 WARN("from 32 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
3109 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
3110 bmpImage->green_mask, bmpImage->blue_mask );
3116 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
3117 const DWORD* srcpixel;
3118 int rShift,gShift,bShift;
3120 rShift=X11DRV_DIB_MaskToShift(rSrc);
3121 gShift=X11DRV_DIB_MaskToShift(gSrc);
3122 bShift=X11DRV_DIB_MaskToShift(bSrc);
3124 for (h = lines - 1; h >= 0; h--) {
3125 srcpixel=(const DWORD*)srcbits;
3126 for (x = left; x < width+left; x++) {
3128 BYTE red,green,blue;
3129 srcvalue=*srcpixel++;
3130 red= (srcvalue >> rShift) & 0xff;
3131 green=(srcvalue >> gShift) & 0xff;
3132 blue= (srcvalue >> bShift) & 0xff;
3133 XPutPixel(bmpImage, x, h, X11DRV_PALETTE_ToPhysical
3134 (physDev, RGB(red,green,blue)));
3136 srcbits += linebytes;
3144 /***********************************************************************
3145 * X11DRV_DIB_GetImageBits_32
3147 * GetDIBits for an 32-bit deep DIB.
3149 static void X11DRV_DIB_GetImageBits_32( int lines, BYTE *dstbits,
3150 DWORD dstwidth, DWORD srcwidth,
3151 PALETTEENTRY *srccolors,
3152 DWORD rDst, DWORD gDst, DWORD bDst,
3153 XImage *bmpImage, DWORD linebytes )
3156 int h, width = min(srcwidth, dstwidth);
3158 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
3163 dstbits = dstbits + ( linebytes * (lines-1) );
3164 linebytes = -linebytes;
3169 switch (bmpImage->depth)
3172 if (bmpImage->bits_per_pixel==24) {
3173 const void* srcbits;
3175 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3177 if (rDst==bmpImage->red_mask && gDst==bmpImage->green_mask && bDst==bmpImage->blue_mask) {
3178 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
3179 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
3180 convs->Convert_888_to_0888_asis
3182 srcbits,-bmpImage->bytes_per_line,
3184 } else if (bmpImage->green_mask!=0x00ff00 ||
3185 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3187 /* the tests below assume sane bmpImage masks */
3188 } else if (rDst==bmpImage->blue_mask && gDst==bmpImage->green_mask && bDst==bmpImage->red_mask) {
3189 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
3190 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
3191 convs->Convert_888_to_0888_reverse
3193 srcbits,-bmpImage->bytes_per_line,
3195 } else if (bmpImage->blue_mask==0xff) {
3196 /* ==== rgb 888 bmp -> any 0888 dib ==== */
3197 convs->Convert_rgb888_to_any0888
3199 srcbits,-bmpImage->bytes_per_line,
3203 /* ==== bgr 888 bmp -> any 0888 dib ==== */
3204 convs->Convert_bgr888_to_any0888
3206 srcbits,-bmpImage->bytes_per_line,
3216 const char* srcbits;
3218 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3220 if (gDst==bmpImage->green_mask) {
3221 if (rDst==bmpImage->red_mask && bDst==bmpImage->blue_mask) {
3222 /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
3223 /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
3224 convs->Convert_0888_asis
3226 srcbits,-bmpImage->bytes_per_line,
3228 } else if (bmpImage->green_mask!=0x00ff00 ||
3229 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3231 /* the tests below assume sane bmpImage masks */
3232 } else if (rDst==bmpImage->blue_mask && bDst==bmpImage->red_mask) {
3233 /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
3234 /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
3235 convs->Convert_0888_reverse
3237 srcbits,-bmpImage->bytes_per_line,
3240 /* ==== any 0888 bmp -> any 0888 dib ==== */
3241 convs->Convert_0888_any
3243 srcbits,-bmpImage->bytes_per_line,
3244 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3248 } else if (bmpImage->green_mask!=0x00ff00 ||
3249 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3251 /* the tests below assume sane bmpImage masks */
3253 /* ==== any 0888 bmp -> any 0888 dib ==== */
3254 convs->Convert_0888_any
3256 srcbits,-bmpImage->bytes_per_line,
3257 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3267 const char* srcbits;
3269 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3271 if (rDst==0xff0000 && gDst==0x00ff00 && bDst==0x0000ff) {
3272 if (bmpImage->green_mask==0x03e0) {
3273 if (bmpImage->red_mask==0x7f00) {
3274 /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
3275 convs->Convert_555_to_0888_asis
3277 srcbits,-bmpImage->bytes_per_line,
3279 } else if (bmpImage->blue_mask==0x7f00) {
3280 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
3281 convs->Convert_555_to_0888_reverse
3283 srcbits,-bmpImage->bytes_per_line,
3288 } else if (bmpImage->green_mask==0x07e0) {
3289 if (bmpImage->red_mask==0xf800) {
3290 /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
3291 convs->Convert_565_to_0888_asis
3293 srcbits,-bmpImage->bytes_per_line,
3295 } else if (bmpImage->blue_mask==0xf800) {
3296 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
3297 convs->Convert_565_to_0888_reverse
3299 srcbits,-bmpImage->bytes_per_line,
3307 } else if (rDst==0x0000ff && gDst==0x00ff00 && bDst==0xff0000) {
3308 if (bmpImage->green_mask==0x03e0) {
3309 if (bmpImage->blue_mask==0x7f00) {
3310 /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
3311 convs->Convert_555_to_0888_asis
3313 srcbits,-bmpImage->bytes_per_line,
3315 } else if (bmpImage->red_mask==0x7f00) {
3316 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
3317 convs->Convert_555_to_0888_reverse
3319 srcbits,-bmpImage->bytes_per_line,
3324 } else if (bmpImage->green_mask==0x07e0) {
3325 if (bmpImage->blue_mask==0xf800) {
3326 /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3327 convs->Convert_565_to_0888_asis
3329 srcbits,-bmpImage->bytes_per_line,
3331 } else if (bmpImage->red_mask==0xf800) {
3332 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3333 convs->Convert_565_to_0888_reverse
3335 srcbits,-bmpImage->bytes_per_line,
3344 if (bmpImage->green_mask==0x03e0 &&
3345 (bmpImage->red_mask==0x7f00 ||
3346 bmpImage->blue_mask==0x7f00)) {
3347 /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3348 convs->Convert_5x5_to_any0888
3350 srcbits,-bmpImage->bytes_per_line,
3351 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3354 } else if (bmpImage->green_mask==0x07e0 &&
3355 (bmpImage->red_mask==0xf800 ||
3356 bmpImage->blue_mask==0xf800)) {
3357 /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3358 convs->Convert_5x5_to_any0888
3360 srcbits,-bmpImage->bytes_per_line,
3361 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3373 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3375 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3376 int rShift,gShift,bShift;
3379 rShift=X11DRV_DIB_MaskToShift(rDst);
3380 gShift=X11DRV_DIB_MaskToShift(gDst);
3381 bShift=X11DRV_DIB_MaskToShift(bDst);
3382 for (h = lines - 1; h >= 0; h--) {
3383 dstpixel=(DWORD*)dstbits;
3384 for (x = 0; x < width; x++) {
3385 PALETTEENTRY srcval;
3386 srcval = srccolors[XGetPixel(bmpImage, x, h)];
3387 *dstpixel++=(srcval.peRed << rShift) |
3388 (srcval.peGreen << gShift) |
3389 (srcval.peBlue << bShift);
3391 dstbits += linebytes;
3399 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3401 /* ==== pal 8 bmp -> any 0888 dib ==== */
3402 int rShift,gShift,bShift;
3403 const void* srcbits;
3404 const BYTE* srcpixel;
3407 rShift=X11DRV_DIB_MaskToShift(rDst);
3408 gShift=X11DRV_DIB_MaskToShift(gDst);
3409 bShift=X11DRV_DIB_MaskToShift(bDst);
3410 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3411 for (h = lines - 1; h >= 0; h--) {
3413 dstpixel=(DWORD*)dstbits;
3414 for (x = 0; x < width; x++) {
3415 PALETTEENTRY srcval;
3416 srcval=srccolors[*srcpixel++];
3417 *dstpixel++=(srcval.peRed << rShift) |
3418 (srcval.peGreen << gShift) |
3419 (srcval.peBlue << bShift);
3421 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
3422 dstbits += linebytes;
3432 /* ==== any bmp format -> any 0888 dib ==== */
3433 int rShift,gShift,bShift;
3436 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%x,%x,%x)\n",
3437 bmpImage->depth, bmpImage->red_mask,
3438 bmpImage->green_mask, bmpImage->blue_mask,
3441 rShift=X11DRV_DIB_MaskToShift(rDst);
3442 gShift=X11DRV_DIB_MaskToShift(gDst);
3443 bShift=X11DRV_DIB_MaskToShift(bDst);
3444 for (h = lines - 1; h >= 0; h--) {
3445 dstpixel=(DWORD*)dstbits;
3446 for (x = 0; x < width; x++) {
3448 srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
3449 *dstpixel++=(GetRValue(srcval) << rShift) |
3450 (GetGValue(srcval) << gShift) |
3451 (GetBValue(srcval) << bShift);
3453 dstbits += linebytes;
3460 static int XGetSubImageErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
3462 return (event->request_code == X_GetImage && event->error_code == BadMatch);
3465 /***********************************************************************
3466 * X11DRV_DIB_SetImageBits_GetSubImage
3468 * Helper for X11DRV_DIB_SetImageBits
3470 static void X11DRV_DIB_SetImageBits_GetSubImage(
3471 const X11DRV_DIB_IMAGEBITS_DESCR *descr, XImage *bmpImage)
3473 /* compressed bitmaps may contain gaps in them. So make a copy
3474 * of the existing pixels first */
3477 SetRect( &bmprc, descr->xDest, descr->yDest,
3478 descr->xDest + descr->width , descr->yDest + descr->height );
3479 GetRgnBox( descr->physDev->region, &rc );
3480 /* convert from dc to drawable origin */
3481 OffsetRect( &rc, descr->physDev->dc_rect.left, descr->physDev->dc_rect.top);
3482 /* clip visible rect with bitmap */
3483 if( IntersectRect( &rc, &rc, &bmprc))
3485 X11DRV_expect_error( gdi_display, XGetSubImageErrorHandler, NULL );
3486 XGetSubImage( gdi_display, descr->drawable, rc.left, rc.top,
3487 rc.right - rc.left, rc.bottom - rc.top, AllPlanes,
3489 descr->xSrc + rc.left - bmprc.left,
3490 descr->ySrc + rc.top - bmprc.top);
3491 X11DRV_check_error();
3495 /***********************************************************************
3496 * X11DRV_DIB_SetImageBits
3498 * Transfer the bits to an X image.
3499 * Helper function for SetDIBits() and SetDIBitsToDevice().
3501 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3503 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3508 bmpImage = descr->image;
3510 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3511 descr->infoWidth, lines, 32, 0 );
3512 bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
3513 if(bmpImage->data == NULL) {
3514 ERR("Out of memory!\n");
3515 XDestroyImage( bmpImage );
3516 wine_tsx11_unlock();
3520 wine_tsx11_unlock();
3522 TRACE("Dib: depth=%d r=%x g=%x b=%x\n",
3523 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3524 TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3525 bmpImage->depth,bmpImage->bits_per_pixel,
3526 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3528 /* Transfer the pixels */
3531 switch(descr->infoBpp)
3534 X11DRV_DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
3535 descr->width, descr->xSrc, (int *)(descr->colorMap),
3536 bmpImage, descr->dibpitch );
3539 if (descr->compression) {
3540 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3541 X11DRV_DIB_SetImageBits_RLE4( descr->lines, descr->bits,
3542 descr->infoWidth, descr->width,
3543 descr->xSrc, (int *)(descr->colorMap),
3546 X11DRV_DIB_SetImageBits_4( descr->lines, descr->bits,
3547 descr->infoWidth, descr->width,
3548 descr->xSrc, (int*)(descr->colorMap),
3549 bmpImage, descr->dibpitch );
3552 if (descr->compression) {
3553 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3554 X11DRV_DIB_SetImageBits_RLE8( descr->lines, descr->bits,
3555 descr->infoWidth, descr->width,
3556 descr->xSrc, (int *)(descr->colorMap),
3559 X11DRV_DIB_SetImageBits_8( descr->lines, descr->bits,
3560 descr->infoWidth, descr->width,
3561 descr->xSrc, (int *)(descr->colorMap),
3562 bmpImage, descr->dibpitch );
3566 X11DRV_DIB_SetImageBits_16( descr->lines, descr->bits,
3567 descr->infoWidth, descr->width,
3568 descr->xSrc, descr->physDev,
3569 descr->rMask, descr->gMask, descr->bMask,
3570 bmpImage, descr->dibpitch);
3573 X11DRV_DIB_SetImageBits_24( descr->lines, descr->bits,
3574 descr->infoWidth, descr->width,
3575 descr->xSrc, descr->physDev,
3576 descr->rMask, descr->gMask, descr->bMask,
3577 bmpImage, descr->dibpitch);
3580 X11DRV_DIB_SetImageBits_32( descr->lines, descr->bits,
3581 descr->infoWidth, descr->width,
3582 descr->xSrc, descr->physDev,
3583 descr->rMask, descr->gMask, descr->bMask,
3584 bmpImage, descr->dibpitch);
3587 WARN("(%d): Invalid depth\n", descr->infoBpp );
3593 WARN( "invalid bits pointer %p\n", descr->bits );
3598 TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3599 descr->drawable, descr->gc, bmpImage,
3600 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3601 descr->width, descr->height);
3606 #ifdef HAVE_LIBXXSHM
3607 if (descr->image && descr->useShm)
3609 XShmPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3610 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3611 descr->width, descr->height, FALSE );
3612 XSync( gdi_display, 0 );
3616 XPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3617 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3618 descr->width, descr->height );
3620 if (!descr->image) XDestroyImage( bmpImage );
3621 wine_tsx11_unlock();
3625 /***********************************************************************
3626 * X11DRV_DIB_GetImageBits
3628 * Transfer the bits from an X image.
3630 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3632 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3637 bmpImage = descr->image;
3639 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3640 descr->infoWidth, lines, 32, 0 );
3641 bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
3642 if(bmpImage->data == NULL) {
3643 ERR("Out of memory!\n");
3644 XDestroyImage( bmpImage );
3645 wine_tsx11_unlock();
3650 #ifdef HAVE_LIBXXSHM
3652 /* We must not call XShmGetImage() with a bitmap which is bigger than the available area.
3653 If we do, XShmGetImage() will fail (X exception), as it checks for this internally. */
3654 if((descr->image && descr->useShm) && (bmpImage->width <= (descr->width - descr->xSrc))
3655 && (bmpImage->height <= (descr->height - descr->ySrc)))
3657 int saveRed, saveGreen, saveBlue;
3659 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3660 gdi_display, descr->drawable, bmpImage,
3661 descr->xSrc, descr->ySrc, AllPlanes);
3663 /* We must save and restore the bmpImage's masks in order
3664 * to preserve them across the call to XShmGetImage, which
3665 * decides to eliminate them since it doesn't happen to know
3666 * what the format of the image is supposed to be, even though
3668 saveRed = bmpImage->red_mask;
3669 saveBlue= bmpImage->blue_mask;
3670 saveGreen = bmpImage->green_mask;
3672 XShmGetImage( gdi_display, descr->drawable, bmpImage,
3673 descr->xSrc, descr->ySrc, AllPlanes);
3675 bmpImage->red_mask = saveRed;
3676 bmpImage->blue_mask = saveBlue;
3677 bmpImage->green_mask = saveGreen;
3680 #endif /* HAVE_LIBXXSHM */
3682 TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3683 gdi_display, descr->drawable, descr->xSrc, descr->ySrc, descr->width,
3684 lines, AllPlanes, ZPixmap, bmpImage, descr->xDest, descr->yDest);
3685 XGetSubImage( gdi_display, descr->drawable, descr->xSrc, descr->ySrc,
3686 descr->width, lines, AllPlanes, ZPixmap,
3687 bmpImage, descr->xDest, descr->yDest );
3689 wine_tsx11_unlock();
3691 TRACE("Dib: depth=%2d r=%x g=%x b=%x\n",
3692 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3693 TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3694 bmpImage->depth,bmpImage->bits_per_pixel,
3695 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3696 /* Transfer the pixels */
3697 switch(descr->infoBpp)
3700 X11DRV_DIB_GetImageBits_1( descr->lines,(LPVOID)descr->bits,
3701 descr->infoWidth, descr->width,
3702 descr->colorMap, descr->palentry,
3703 bmpImage, descr->dibpitch );
3707 if (descr->compression) {
3708 FIXME("Compression not yet supported!\n");
3709 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 4 ) * abs(descr->lines))
3712 X11DRV_DIB_GetImageBits_4( descr->lines,(LPVOID)descr->bits,
3713 descr->infoWidth, descr->width,
3714 descr->colorMap, descr->palentry,
3715 bmpImage, descr->dibpitch );
3718 if (descr->compression) {
3719 FIXME("Compression not yet supported!\n");
3720 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 8 ) * abs(descr->lines))
3723 X11DRV_DIB_GetImageBits_8( descr->lines, (LPVOID)descr->bits,
3724 descr->infoWidth, descr->width,
3725 descr->colorMap, descr->palentry,
3726 bmpImage, descr->dibpitch );
3730 X11DRV_DIB_GetImageBits_16( descr->lines, (LPVOID)descr->bits,
3731 descr->infoWidth,descr->width,
3733 descr->rMask, descr->gMask, descr->bMask,
3734 bmpImage, descr->dibpitch );
3738 X11DRV_DIB_GetImageBits_24( descr->lines, (LPVOID)descr->bits,
3739 descr->infoWidth,descr->width,
3741 descr->rMask, descr->gMask, descr->bMask,
3742 bmpImage, descr->dibpitch);
3746 X11DRV_DIB_GetImageBits_32( descr->lines, (LPVOID)descr->bits,
3747 descr->infoWidth, descr->width,
3749 descr->rMask, descr->gMask, descr->bMask,
3750 bmpImage, descr->dibpitch);
3754 WARN("(%d): Invalid depth\n", descr->infoBpp );
3761 XDestroyImage( bmpImage );
3762 wine_tsx11_unlock();
3767 /*************************************************************************
3768 * X11DRV_SetDIBitsToDevice
3771 INT X11DRV_SetDIBitsToDevice( X11DRV_PDEVICE *physDev, INT xDest, INT yDest, DWORD cx,
3772 DWORD cy, INT xSrc, INT ySrc,
3773 UINT startscan, UINT lines, LPCVOID bits,
3774 const BITMAPINFO *info, UINT coloruse )
3776 X11DRV_DIB_IMAGEBITS_DESCR descr;
3781 int rop = X11DRV_XROPfunction[GetROP2(physDev->hdc) - 1];
3783 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3784 &descr.infoBpp, &descr.compression ) == -1)
3787 top_down = (height < 0);
3788 if (top_down) height = -height;
3792 LPtoDP(physDev->hdc, &pt, 1);
3794 if (!lines || (startscan >= height)) return 0;
3795 if (!top_down && startscan + lines > height) lines = height - startscan;
3797 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3798 * and clamp all values to fit inside [startscan,startscan+lines]
3800 if (ySrc + cy <= startscan + lines)
3802 UINT y = startscan + lines - (ySrc + cy);
3803 if (ySrc < startscan) cy -= (startscan - ySrc);
3806 /* avoid getting unnecessary lines */
3808 if (y >= lines) return 0;
3813 if (y >= lines) return lines;
3814 ySrc = y; /* need to get all lines in top down mode */
3819 if (ySrc >= startscan + lines) return lines;
3820 pt.y += ySrc + cy - (startscan + lines);
3821 cy = startscan + lines - ySrc;
3823 if (cy > lines) cy = lines;
3825 if (xSrc >= width) return lines;
3826 if (xSrc + cx >= width) cx = width - xSrc;
3827 if (!cx || !cy) return lines;
3829 /* Update the pixmap from the DIB section */
3830 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
3832 X11DRV_SetupGCForText( physDev ); /* To have the correct colors */
3834 XSetFunction(gdi_display, physDev->gc, rop);
3835 wine_tsx11_unlock();
3837 switch (descr.infoBpp)
3842 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3844 physDev->depth, info, &descr.nColorMap );
3845 if (!descr.colorMap) return 0;
3846 descr.rMask = descr.gMask = descr.bMask = 0;
3850 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3851 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3852 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3858 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3859 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3860 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3865 descr.physDev = physDev;
3868 descr.palentry = NULL;
3869 descr.lines = top_down ? -lines : lines;
3870 descr.infoWidth = width;
3871 descr.depth = physDev->depth;
3872 descr.drawable = physDev->drawable;
3873 descr.gc = physDev->gc;
3876 descr.xDest = physDev->dc_rect.left + pt.x;
3877 descr.yDest = physDev->dc_rect.top + pt.y;
3880 descr.useShm = FALSE;
3881 descr.dibpitch = ((width * descr.infoBpp + 31) &~31) / 8;
3883 result = X11DRV_DIB_SetImageBits( &descr );
3885 if (descr.infoBpp <= 8)
3886 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3888 /* Update the DIBSection of the pixmap */
3889 X11DRV_UnlockDIBSection(physDev, TRUE);
3894 /***********************************************************************
3895 * SetDIBits (X11DRV.@)
3897 INT X11DRV_SetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan,
3898 UINT lines, LPCVOID bits, const BITMAPINFO *info, UINT coloruse )
3900 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3901 X11DRV_DIB_IMAGEBITS_DESCR descr;
3903 LONG width, height, tmpheight;
3906 descr.physDev = physDev;
3908 if (!physBitmap) return 0;
3910 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3911 &descr.infoBpp, &descr.compression ) == -1)
3915 if (height < 0) height = -height;
3916 if (!lines || (startscan >= height))
3919 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
3921 if (startscan + lines > height) lines = height - startscan;
3923 switch (descr.infoBpp)
3928 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3929 descr.physDev, coloruse,
3930 physBitmap->pixmap_depth,
3931 info, &descr.nColorMap );
3932 if (!descr.colorMap) return 0;
3933 descr.rMask = descr.gMask = descr.bMask = 0;
3937 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3938 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3939 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3945 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3946 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3947 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3956 descr.palentry = NULL;
3957 descr.infoWidth = width;
3958 descr.lines = tmpheight >= 0 ? lines : -lines;
3959 descr.depth = physBitmap->pixmap_depth;
3960 descr.drawable = physBitmap->pixmap;
3961 descr.gc = BITMAP_GC(physBitmap);
3965 descr.yDest = height - startscan - lines;
3966 descr.width = bitmap.bmWidth;
3967 descr.height = lines;
3968 descr.useShm = FALSE;
3969 descr.dibpitch = ((descr.infoWidth * descr.infoBpp + 31) &~31) / 8;
3970 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
3971 result = X11DRV_DIB_SetImageBits( &descr );
3972 X11DRV_DIB_Unlock( physBitmap, TRUE );
3974 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3979 /***********************************************************************
3980 * GetDIBits (X11DRV.@)
3982 INT X11DRV_GetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan, UINT lines,
3983 LPVOID bits, BITMAPINFO *info, UINT coloruse )
3985 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3987 X11DRV_DIB_IMAGEBITS_DESCR descr;
3988 PALETTEENTRY palette[256];
3991 LONG width, tempHeight;
3995 static const PALETTEENTRY peBlack = {0,0,0,0};
3996 static const PALETTEENTRY peWhite = {255,255,255,0};
3998 if (!physBitmap) return 0;
3999 if (!(obj_size = GetObjectW( hbitmap, sizeof(dib), &dib ))) return 0;
4001 bitmap_type = DIB_GetBitmapInfo( (BITMAPINFOHEADER*)info, &width, &tempHeight, &descr.infoBpp, &descr.compression);
4003 if (physDev->depth > 1)
4005 GetPaletteEntries( GetCurrentObject( physDev->hdc, OBJ_PAL ), 0, 256, palette );
4009 palette[0] = peBlack;
4010 palette[1] = peWhite;
4013 if (bitmap_type == -1)
4015 ERR("Invalid bitmap\n");
4018 descr.lines = tempHeight;
4019 core_header = (bitmap_type == 0);
4020 colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
4022 TRACE("%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
4023 lines, dib.dsBm.bmWidth, dib.dsBm.bmHeight, width, descr.lines, startscan);
4025 if( lines > dib.dsBm.bmHeight ) lines = dib.dsBm.bmHeight;
4027 height = descr.lines;
4028 if (height < 0) height = -height;
4029 if( lines > height ) lines = height;
4030 /* Top-down images have a negative biHeight, the scanlines of these images
4031 * were inverted in X11DRV_DIB_GetImageBits_xx
4032 * To prevent this we simply change the sign of lines
4033 * (the number of scan lines to copy).
4034 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4036 if( descr.lines < 0 && lines > 0) lines = -lines;
4038 if( startscan >= dib.dsBm.bmHeight ) return 0;
4040 descr.colorMap = NULL;
4042 switch (descr.infoBpp)
4047 descr.rMask= descr.gMask = descr.bMask = 0;
4048 if(coloruse == DIB_RGB_COLORS)
4049 descr.colorMap = colorPtr;
4051 int num_colors = 1 << descr.infoBpp, i;
4054 WORD *index = (WORD*)colorPtr;
4055 descr.colorMap = rgb = HeapAlloc(GetProcessHeap(), 0, num_colors * sizeof(RGBQUAD));
4056 for(i = 0; i < num_colors; i++, rgb++, index++) {
4057 colref = X11DRV_PALETTE_ToLogical(X11DRV_PALETTE_ToPhysical(physDev, PALETTEINDEX(*index)));
4058 rgb->rgbRed = GetRValue(colref);
4059 rgb->rgbGreen = GetGValue(colref);
4060 rgb->rgbBlue = GetBValue(colref);
4061 rgb->rgbReserved = 0;
4067 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4068 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4069 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4073 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4074 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4075 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4079 descr.physDev = physDev;
4080 descr.palentry = palette;
4082 descr.image = physBitmap->image;
4083 descr.infoWidth = width;
4084 descr.lines = lines;
4085 descr.depth = physBitmap->pixmap_depth;
4086 descr.drawable = physBitmap->pixmap;
4087 descr.gc = BITMAP_GC(physBitmap);
4088 descr.width = dib.dsBm.bmWidth;
4089 descr.height = dib.dsBm.bmHeight;
4093 descr.sizeImage = core_header ? 0 : info->bmiHeader.biSizeImage;
4095 if (descr.lines > 0)
4097 descr.ySrc = (descr.height-1) - (startscan + (lines-1));
4101 descr.ySrc = startscan;
4103 #ifdef HAVE_LIBXXSHM
4104 descr.useShm = (obj_size == sizeof(DIBSECTION)) && (physBitmap->shminfo.shmid != -1);
4106 descr.useShm = FALSE;
4108 descr.dibpitch = (obj_size == sizeof(DIBSECTION)) ? dib.dsBm.bmWidthBytes
4109 : (((descr.infoWidth * descr.infoBpp + 31) &~31) / 8);
4111 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
4112 X11DRV_DIB_GetImageBits( &descr );
4113 X11DRV_DIB_Unlock( physBitmap, TRUE );
4115 if(!core_header && info->bmiHeader.biSizeImage == 0) /* Fill in biSizeImage */
4116 info->bmiHeader.biSizeImage = X11DRV_DIB_GetDIBImageBytes( descr.infoWidth,
4120 if (descr.compression == BI_BITFIELDS)
4122 *(DWORD *)info->bmiColors = descr.rMask;
4123 *((DWORD *)info->bmiColors + 1) = descr.gMask;
4124 *((DWORD *)info->bmiColors + 2) = descr.bMask;
4126 else if (!core_header)
4128 /* if RLE or JPEG compression were supported,
4129 * this line would be invalid. */
4130 info->bmiHeader.biCompression = 0;
4133 if(descr.colorMap != colorPtr)
4134 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4138 /***********************************************************************
4139 * DIB_DoProtectDIBSection
4141 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP *physBitmap, DWORD new_prot )
4145 VirtualProtect(physBitmap->base, physBitmap->size, new_prot, &old_prot);
4146 TRACE("Changed protection from %d to %d\n", old_prot, new_prot);
4149 /***********************************************************************
4150 * X11DRV_DIB_DoCopyDIBSection
4152 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB,
4153 void *colorMap, int nColorMap,
4154 Drawable dest, GC gc,
4155 DWORD xSrc, DWORD ySrc,
4156 DWORD xDest, DWORD yDest,
4157 DWORD width, DWORD height)
4159 DIBSECTION dibSection;
4160 X11DRV_DIB_IMAGEBITS_DESCR descr;
4161 int identity[2] = {0,1};
4163 if (!GetObjectW( physBitmap->hbitmap, sizeof(dibSection), &dibSection )) return;
4165 descr.physDev = NULL;
4166 descr.palentry = NULL;
4167 descr.infoWidth = dibSection.dsBmih.biWidth;
4168 descr.infoBpp = dibSection.dsBmih.biBitCount;
4169 descr.lines = dibSection.dsBmih.biHeight;
4170 descr.image = physBitmap->image;
4171 descr.colorMap = colorMap;
4172 descr.nColorMap = nColorMap;
4173 descr.bits = dibSection.dsBm.bmBits;
4174 descr.depth = physBitmap->pixmap_depth;
4175 descr.compression = dibSection.dsBmih.biCompression;
4177 if(descr.infoBpp == 1)
4178 descr.colorMap = (void*)identity;
4180 switch (descr.infoBpp)
4185 descr.rMask = descr.gMask = descr.bMask = 0;
4189 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0x7c00;
4190 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x03e0;
4191 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x001f;
4196 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0xff0000;
4197 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x00ff00;
4198 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x0000ff;
4203 descr.drawable = dest;
4207 descr.xDest = xDest;
4208 descr.yDest = yDest;
4209 descr.width = width;
4210 descr.height = height;
4211 descr.sizeImage = 0;
4213 #ifdef HAVE_LIBXXSHM
4214 descr.useShm = (physBitmap->shminfo.shmid != -1);
4216 descr.useShm = FALSE;
4218 descr.dibpitch = dibSection.dsBm.bmWidthBytes;
4222 TRACE("Copying from Pixmap to DIB bits\n");
4223 X11DRV_DIB_GetImageBits( &descr );
4227 TRACE("Copying from DIB bits to Pixmap\n");
4228 X11DRV_DIB_SetImageBits( &descr );
4232 /***********************************************************************
4233 * X11DRV_DIB_CopyDIBSection
4235 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
4236 DWORD xSrc, DWORD ySrc, DWORD xDest, DWORD yDest,
4237 DWORD width, DWORD height)
4240 X_PHYSBITMAP *physBitmap;
4241 unsigned int nColorMap;
4245 TRACE("(%p,%p,%d,%d,%d,%d,%d,%d)\n", physDevSrc->hdc, physDevDst->hdc,
4246 xSrc, ySrc, xDest, yDest, width, height);
4247 /* this function is meant as an optimization for BitBlt,
4248 * not to be called otherwise */
4249 physBitmap = physDevSrc->bitmap;
4250 if (!physBitmap || GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib ) != sizeof(dib))
4252 ERR("called for non-DIBSection!?\n");
4255 /* while BitBlt should already have made sure we only get
4256 * positive values, we should check for oversize values */
4257 if ((xSrc < dib.dsBm.bmWidth) &&
4258 (ySrc < dib.dsBm.bmHeight)) {
4259 if (xSrc + width > dib.dsBm.bmWidth)
4260 width = dib.dsBm.bmWidth - xSrc;
4261 if (ySrc + height > dib.dsBm.bmHeight)
4262 height = dib.dsBm.bmHeight - ySrc;
4263 /* if the source bitmap is 8bpp or less, we're supposed to use the
4264 * DC's palette for color conversion (not the DIB color table) */
4265 if (dib.dsBm.bmBitsPixel <= 8) {
4266 HPALETTE hPalette = GetCurrentObject( physDevSrc->hdc, OBJ_PAL );
4267 if (!hPalette || (hPalette == GetStockObject(DEFAULT_PALETTE))) {
4268 /* HACK: no palette has been set in the source DC,
4269 * use the DIB colormap instead - this is necessary in some
4270 * cases since we need to do depth conversion in some places
4271 * where real Windows can just copy data straight over */
4272 x11ColorMap = physBitmap->colorMap;
4273 nColorMap = physBitmap->nColorMap;
4274 freeColorMap = FALSE;
4276 const BITMAPINFO* info = (BITMAPINFO*)&dib.dsBmih;
4279 nColorMap = X11DRV_DIB_GetColorCount(info);
4280 x11ColorMap = HeapAlloc(GetProcessHeap(), 0, nColorMap * sizeof(int));
4281 for (i = 0; i < nColorMap; i++)
4282 x11ColorMap[i] = X11DRV_PALETTE_ToPhysical(physDevSrc, PALETTEINDEX(i));
4283 freeColorMap = TRUE;
4290 freeColorMap = FALSE;
4292 /* perform the copy */
4293 X11DRV_DIB_DoCopyDIBSection(physBitmap, FALSE, x11ColorMap, nColorMap,
4294 physDevDst->drawable, physDevDst->gc, xSrc, ySrc,
4295 physDevDst->dc_rect.left + xDest, physDevDst->dc_rect.top + yDest,
4297 /* free color mapping */
4299 HeapFree(GetProcessHeap(), 0, x11ColorMap);
4303 /***********************************************************************
4304 * X11DRV_DIB_DoUpdateDIBSection
4306 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB)
4310 GetObjectW( physBitmap->hbitmap, sizeof(bitmap), &bitmap );
4311 X11DRV_DIB_DoCopyDIBSection(physBitmap, toDIB,
4312 physBitmap->colorMap, physBitmap->nColorMap,
4313 physBitmap->pixmap, BITMAP_GC(physBitmap),
4314 0, 0, 0, 0, bitmap.bmWidth, bitmap.bmHeight);
4317 /***********************************************************************
4318 * X11DRV_DIB_FaultHandler
4320 static LONG CALLBACK X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep )
4322 X_PHYSBITMAP *physBitmap = NULL;
4326 const size_t pagemask = getpagesize() - 1;
4328 if (ep->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
4329 return EXCEPTION_CONTINUE_SEARCH;
4331 addr = (BYTE *)ep->ExceptionRecord->ExceptionInformation[1];
4333 EnterCriticalSection(&dibs_cs);
4334 LIST_FOR_EACH( ptr, &dibs_list )
4336 physBitmap = LIST_ENTRY( ptr, X_PHYSBITMAP, entry );
4337 if ((physBitmap->base <= addr) &&
4338 (addr < physBitmap->base + ((physBitmap->size + pagemask) & ~pagemask)))
4344 LeaveCriticalSection(&dibs_cs);
4346 if (!found) return EXCEPTION_CONTINUE_SEARCH;
4348 if (addr >= physBitmap->base + physBitmap->size)
4349 WARN( "%p: access to %p beyond the end of the DIB\n", physBitmap->hbitmap, addr );
4351 X11DRV_DIB_Lock( physBitmap, DIB_Status_None );
4352 if (ep->ExceptionRecord->ExceptionInformation[0] == EXCEPTION_WRITE_FAULT) {
4353 /* the app tried to write the DIB bits */
4354 X11DRV_DIB_Coerce( physBitmap, DIB_Status_AppMod);
4356 /* the app tried to read the DIB bits */
4357 X11DRV_DIB_Coerce( physBitmap, DIB_Status_InSync);
4359 X11DRV_DIB_Unlock( physBitmap, TRUE );
4361 return EXCEPTION_CONTINUE_EXECUTION;
4364 /***********************************************************************
4367 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *physBitmap, INT req)
4369 INT ret = DIB_Status_None;
4371 if (!physBitmap->image) return ret; /* not a DIB section */
4372 EnterCriticalSection(&physBitmap->lock);
4373 ret = physBitmap->status;
4375 case DIB_Status_GdiMod:
4376 /* GDI access - request to draw on pixmap */
4377 switch (physBitmap->status)
4380 case DIB_Status_None:
4381 physBitmap->p_status = DIB_Status_GdiMod;
4382 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4385 case DIB_Status_GdiMod:
4386 TRACE("GdiMod requested in status GdiMod\n" );
4387 physBitmap->p_status = DIB_Status_GdiMod;
4390 case DIB_Status_InSync:
4391 TRACE("GdiMod requested in status InSync\n" );
4392 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4393 physBitmap->status = DIB_Status_GdiMod;
4394 physBitmap->p_status = DIB_Status_InSync;
4397 case DIB_Status_AppMod:
4398 TRACE("GdiMod requested in status AppMod\n" );
4399 /* make it readonly to avoid app changing data while we copy */
4400 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4401 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4402 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4403 physBitmap->p_status = DIB_Status_AppMod;
4404 physBitmap->status = DIB_Status_GdiMod;
4409 case DIB_Status_InSync:
4410 /* App access - request access to read DIB surface */
4411 /* (typically called from signal handler) */
4412 switch (physBitmap->status)
4415 case DIB_Status_None:
4416 /* shouldn't happen from signal handler */
4419 case DIB_Status_GdiMod:
4420 TRACE("InSync requested in status GdiMod\n" );
4421 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4422 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4423 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4424 physBitmap->status = DIB_Status_InSync;
4427 case DIB_Status_InSync:
4428 TRACE("InSync requested in status InSync\n" );
4429 /* shouldn't happen from signal handler */
4432 case DIB_Status_AppMod:
4433 TRACE("InSync requested in status AppMod\n" );
4434 /* no reason to do anything here, and this
4435 * shouldn't happen from signal handler */
4440 case DIB_Status_AppMod:
4441 /* App access - request access to write DIB surface */
4442 /* (typically called from signal handler) */
4443 switch (physBitmap->status)
4446 case DIB_Status_None:
4447 /* shouldn't happen from signal handler */
4450 case DIB_Status_GdiMod:
4451 TRACE("AppMod requested in status GdiMod\n" );
4452 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4453 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4454 physBitmap->status = DIB_Status_AppMod;
4457 case DIB_Status_InSync:
4458 TRACE("AppMod requested in status InSync\n" );
4459 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4460 physBitmap->status = DIB_Status_AppMod;
4463 case DIB_Status_AppMod:
4464 TRACE("AppMod requested in status AppMod\n" );
4465 /* shouldn't happen from signal handler */
4470 /* it is up to the caller to do the copy/conversion, probably
4471 * using the return value to decide where to copy from */
4473 LeaveCriticalSection(&physBitmap->lock);
4477 /***********************************************************************
4480 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *physBitmap, INT req)
4482 INT ret = DIB_Status_None;
4484 if (!physBitmap->image) return ret; /* not a DIB section */
4485 TRACE("Locking %p from thread %04x\n", physBitmap->hbitmap, GetCurrentThreadId());
4486 EnterCriticalSection(&physBitmap->lock);
4487 ret = physBitmap->status;
4488 if (req != DIB_Status_None)
4489 X11DRV_DIB_Coerce(physBitmap, req);
4493 /***********************************************************************
4496 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *physBitmap, BOOL commit)
4498 if (!physBitmap->image) return; /* not a DIB section */
4499 switch (physBitmap->status)
4502 case DIB_Status_None:
4503 /* in case anyone is wondering, this is the "signal handler doesn't
4504 * work" case, where we always have to be ready for app access */
4506 switch (physBitmap->p_status)
4508 case DIB_Status_GdiMod:
4509 TRACE("Unlocking and syncing from GdiMod\n" );
4510 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4514 TRACE("Unlocking without needing to sync\n" );
4518 else TRACE("Unlocking with no changes\n");
4519 physBitmap->p_status = DIB_Status_None;
4522 case DIB_Status_GdiMod:
4523 TRACE("Unlocking in status GdiMod\n" );
4524 /* DIB was protected in Coerce */
4526 /* no commit, revert to InSync if applicable */
4527 if ((physBitmap->p_status == DIB_Status_InSync) ||
4528 (physBitmap->p_status == DIB_Status_AppMod)) {
4529 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4530 physBitmap->status = DIB_Status_InSync;
4535 case DIB_Status_InSync:
4536 TRACE("Unlocking in status InSync\n" );
4537 /* DIB was already protected in Coerce */
4540 case DIB_Status_AppMod:
4541 TRACE("Unlocking in status AppMod\n" );
4542 /* DIB was already protected in Coerce */
4543 /* this case is ordinary only called from the signal handler,
4544 * so we don't bother to check for !commit */
4547 LeaveCriticalSection(&physBitmap->lock);
4548 TRACE("Unlocked %p\n", physBitmap->hbitmap);
4551 /***********************************************************************
4552 * X11DRV_CoerceDIBSection
4554 INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req)
4556 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4557 return X11DRV_DIB_Coerce(physDev->bitmap, req);
4560 /***********************************************************************
4561 * X11DRV_LockDIBSection
4563 INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req)
4565 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4566 return X11DRV_DIB_Lock(physDev->bitmap, req);
4569 /***********************************************************************
4570 * X11DRV_UnlockDIBSection
4572 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
4574 if (!physDev || !physDev->bitmap) return;
4575 X11DRV_DIB_Unlock(physDev->bitmap, commit);
4579 #ifdef HAVE_LIBXXSHM
4580 /***********************************************************************
4581 * X11DRV_XShmErrorHandler
4584 static int XShmErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
4586 return 1; /* FIXME: should check event contents */
4589 /***********************************************************************
4590 * X11DRV_XShmCreateImage
4593 static XImage *X11DRV_XShmCreateImage( int width, int height, int bpp,
4594 XShmSegmentInfo* shminfo)
4598 image = XShmCreateImage(gdi_display, visual, bpp, ZPixmap, NULL, shminfo, width, height);
4601 shminfo->shmid = shmget(IPC_PRIVATE, image->bytes_per_line * height,
4603 if( shminfo->shmid != -1 )
4605 shminfo->shmaddr = image->data = shmat(shminfo->shmid, 0, 0);
4606 if( shminfo->shmaddr != (char*)-1 )
4610 shminfo->readOnly = FALSE;
4611 X11DRV_expect_error( gdi_display, XShmErrorHandler, NULL );
4612 ok = (XShmAttach( gdi_display, shminfo ) != 0);
4613 XSync( gdi_display, False );
4614 if (X11DRV_check_error()) ok = FALSE;
4617 shmctl(shminfo->shmid, IPC_RMID, 0);
4618 return image; /* Success! */
4620 /* An error occurred */
4621 shmdt(shminfo->shmaddr);
4623 shmctl(shminfo->shmid, IPC_RMID, 0);
4624 shminfo->shmid = -1;
4626 XFlush(gdi_display);
4627 XDestroyImage(image);
4632 #endif /* HAVE_LIBXXSHM */
4635 /***********************************************************************
4636 * X11DRV_CreateDIBSection (X11DRV.@)
4638 HBITMAP X11DRV_CreateDIBSection( X11DRV_PDEVICE *physDev, HBITMAP hbitmap,
4639 const BITMAPINFO *bmi, UINT usage )
4641 X_PHYSBITMAP *physBitmap;
4644 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return 0;
4645 physBitmap->status = DIB_Status_None;
4647 GetObjectW( hbitmap, sizeof(dib), &dib );
4649 /* create color map */
4650 if (dib.dsBm.bmBitsPixel <= 8)
4652 physBitmap->colorMap = X11DRV_DIB_BuildColorMap( physDev,
4653 usage, dib.dsBm.bmBitsPixel, bmi,
4654 &physBitmap->nColorMap );
4657 /* create pixmap and X image */
4659 physBitmap->pixmap_depth = (dib.dsBm.bmBitsPixel == 1) ? 1 : screen_depth;
4660 physBitmap->pixmap = XCreatePixmap( gdi_display, root_window, dib.dsBm.bmWidth,
4661 dib.dsBm.bmHeight, physBitmap->pixmap_depth );
4662 #ifdef HAVE_LIBXXSHM
4663 physBitmap->shminfo.shmid = -1;
4664 if (!XShmQueryExtension(gdi_display) ||
4665 !(physBitmap->image = X11DRV_XShmCreateImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4666 physBitmap->pixmap_depth, &physBitmap->shminfo )) )
4668 physBitmap->image = X11DRV_DIB_CreateXImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4669 physBitmap->pixmap_depth );
4670 wine_tsx11_unlock();
4671 if (!physBitmap->pixmap || !physBitmap->image) return 0;
4673 /* install fault handler */
4674 InitializeCriticalSection( &physBitmap->lock );
4675 physBitmap->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": X_PHYSBITMAP.lock");
4677 physBitmap->base = dib.dsBm.bmBits;
4678 physBitmap->size = dib.dsBmih.biSizeImage;
4679 physBitmap->status = DIB_Status_AppMod;
4682 dibs_handler = AddVectoredExceptionHandler( TRUE, X11DRV_DIB_FaultHandler );
4683 EnterCriticalSection( &dibs_cs );
4684 list_add_head( &dibs_list, &physBitmap->entry );
4685 LeaveCriticalSection( &dibs_cs );
4687 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4692 /***********************************************************************
4693 * X11DRV_DIB_DeleteDIBSection
4695 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP *physBitmap, DIBSECTION *dib)
4699 EnterCriticalSection( &dibs_cs );
4700 list_remove( &physBitmap->entry );
4701 last = list_empty( &dibs_list );
4702 LeaveCriticalSection( &dibs_cs );
4706 RemoveVectoredExceptionHandler( dibs_handler );
4707 dibs_handler = NULL;
4710 if (dib->dshSection)
4711 X11DRV_DIB_Coerce(physBitmap, DIB_Status_InSync);
4713 if (physBitmap->image)
4716 #ifdef HAVE_LIBXXSHM
4717 if (physBitmap->shminfo.shmid != -1)
4719 XShmDetach( gdi_display, &(physBitmap->shminfo) );
4720 XDestroyImage( physBitmap->image );
4721 shmdt( physBitmap->shminfo.shmaddr );
4722 physBitmap->shminfo.shmid = -1;
4726 XDestroyImage( physBitmap->image );
4727 wine_tsx11_unlock();
4730 HeapFree(GetProcessHeap(), 0, physBitmap->colorMap);
4731 physBitmap->lock.DebugInfo->Spare[0] = 0;
4732 DeleteCriticalSection(&physBitmap->lock);
4735 /***********************************************************************
4736 * SetDIBColorTable (X11DRV.@)
4738 UINT X11DRV_SetDIBColorTable( X11DRV_PDEVICE *physDev, UINT start, UINT count, const RGBQUAD *colors )
4742 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4744 if (!physBitmap) return 0;
4745 GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib );
4747 if (physBitmap->colorMap && start < physBitmap->nColorMap) {
4748 UINT end = count + start;
4749 if (end > physBitmap->nColorMap) end = physBitmap->nColorMap;
4751 * Changing color table might change the mapping between
4752 * DIB colors and X11 colors and thus alter the visible state
4753 * of the bitmap object.
4756 * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4757 * at least for a 1 bpp dibsection
4759 X11DRV_DIB_Lock( physBitmap, DIB_Status_AppMod );
4760 X11DRV_DIB_GenColorMap( physDev, physBitmap->colorMap, DIB_RGB_COLORS,
4761 dib.dsBm.bmBitsPixel,
4762 TRUE, colors, start, end );
4763 X11DRV_DIB_Unlock( physBitmap, TRUE );
4770 /***********************************************************************
4771 * X11DRV_DIB_CreateDIBFromBitmap
4773 * Allocates a packed DIB and copies the bitmap data into it.
4775 HGLOBAL X11DRV_DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
4780 LPBITMAPINFOHEADER pbmiHeader;
4781 unsigned int cDataSize, cPackedSize, OffsetBits;
4784 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
4787 * A packed DIB contains a BITMAPINFO structure followed immediately by
4788 * an optional color palette and the pixel data.
4791 /* Calculate the size of the packed DIB */
4792 cDataSize = X11DRV_DIB_GetDIBWidthBytes( bmp.bmWidth, bmp.bmBitsPixel ) * abs( bmp.bmHeight );
4793 cPackedSize = sizeof(BITMAPINFOHEADER)
4794 + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
4796 /* Get the offset to the bits */
4797 OffsetBits = cPackedSize - cDataSize;
4799 /* Allocate the packed DIB */
4800 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
4801 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
4805 WARN("Could not allocate packed DIB!\n");
4809 /* A packed DIB starts with a BITMAPINFOHEADER */
4810 pPackedDIB = GlobalLock(hPackedDIB);
4811 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
4813 /* Init the BITMAPINFOHEADER */
4814 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
4815 pbmiHeader->biWidth = bmp.bmWidth;
4816 pbmiHeader->biHeight = bmp.bmHeight;
4817 pbmiHeader->biPlanes = 1;
4818 pbmiHeader->biBitCount = bmp.bmBitsPixel;
4819 pbmiHeader->biCompression = BI_RGB;
4820 pbmiHeader->biSizeImage = 0;
4821 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
4822 pbmiHeader->biClrUsed = 0;
4823 pbmiHeader->biClrImportant = 0;
4825 /* Retrieve the DIB bits from the bitmap and fill in the
4826 * DIB color table if present */
4828 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
4829 hBmp, /* Handle to bitmap */
4830 0, /* First scan line to set in dest bitmap */
4831 bmp.bmHeight, /* Number of scan lines to copy */
4832 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
4833 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
4834 0); /* RGB or palette index */
4835 GlobalUnlock(hPackedDIB);
4837 /* Cleanup if GetDIBits failed */
4838 if (nLinesCopied != bmp.bmHeight)
4840 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
4841 GlobalFree(hPackedDIB);
4848 /**************************************************************************
4849 * X11DRV_DIB_CreateDIBFromPixmap
4851 * Allocates a packed DIB and copies the Pixmap data into it.
4852 * The Pixmap passed in is deleted after the conversion.
4854 HGLOBAL X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap, HDC hdc)
4857 X_PHYSBITMAP *physBitmap;
4860 HGLOBAL hPackedDIB = 0;
4862 int x,y; /* Unused */
4863 unsigned border_width; /* Unused */
4864 unsigned int depth, width, height;
4866 /* Get the Pixmap dimensions and bit depth */
4868 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
4869 &border_width, &depth)) depth = 0;
4870 wine_tsx11_unlock();
4871 if (!depth) return 0;
4873 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
4874 width, height, depth);
4877 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
4878 * and make it a container for the pixmap passed.
4880 if (!(hBmp = CreateBitmap( width, height, 1, depth_to_bpp(depth), NULL ))) return 0;
4882 /* force bitmap to be owned by a screen DC */
4883 hdcMem = CreateCompatibleDC( hdc );
4884 SelectObject( hdcMem, SelectObject( hdcMem, hBmp ));
4887 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4889 /* swap the new pixmap in */
4890 orig_pixmap = physBitmap->pixmap;
4891 physBitmap->pixmap = pixmap;
4894 * Create a packed DIB from the Pixmap wrapper bitmap created above.
4895 * A packed DIB contains a BITMAPINFO structure followed immediately by
4896 * an optional color palette and the pixel data.
4898 hPackedDIB = X11DRV_DIB_CreateDIBFromBitmap(hdc, hBmp);
4900 /* we can now get rid of the HBITMAP and its original pixmap */
4901 physBitmap->pixmap = orig_pixmap;
4904 TRACE("\tReturning packed DIB %p\n", hPackedDIB);
4909 /**************************************************************************
4910 * X11DRV_DIB_CreatePixmapFromDIB
4912 * Creates a Pixmap from a packed DIB
4914 Pixmap X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB, HDC hdc )
4917 X_PHYSBITMAP *physBitmap;
4921 /* Create a DDB from the DIB */
4923 pbmi = GlobalLock(hPackedDIB);
4924 hBmp = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT,
4925 (LPBYTE)pbmi + bitmap_info_size( pbmi, DIB_RGB_COLORS ),
4926 pbmi, DIB_RGB_COLORS);
4927 GlobalUnlock(hPackedDIB);
4929 /* clear the physBitmap so that we can steal its pixmap */
4930 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4931 pixmap = physBitmap->pixmap;
4932 physBitmap->pixmap = 0;
4934 /* Delete the DDB we created earlier now that we have stolen its pixmap */
4937 TRACE("Returning Pixmap %lx\n", pixmap);