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
25 #include <X11/extensions/XShm.h>
26 # ifdef HAVE_SYS_SHM_H
29 # ifdef HAVE_SYS_IPC_H
32 #endif /* defined(HAVE_LIBXXSHM) */
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
46 static struct list dibs_list = LIST_INIT(dibs_list);
48 static CRITICAL_SECTION dibs_cs;
49 static CRITICAL_SECTION_DEBUG dibs_cs_debug =
52 { &dibs_cs_debug.ProcessLocksList, &dibs_cs_debug.ProcessLocksList },
53 0, 0, { (DWORD_PTR)(__FILE__ ": dibs_cs") }
55 static CRITICAL_SECTION dibs_cs = { &dibs_cs_debug, -1, 0, 0, 0, 0 };
57 static PVOID dibs_handler;
59 static int ximageDepthTable[32];
61 /* This structure holds the arguments for DIB_SetImageBits() */
64 X11DRV_PDEVICE *physDev;
67 PALETTEENTRY *palentry;
89 } X11DRV_DIB_IMAGEBITS_DESCR;
94 RLE_EOL = 0, /* End of line */
95 RLE_END = 1, /* End of bitmap */
96 RLE_DELTA = 2 /* Delta */
100 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *,INT);
101 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *,INT);
102 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *,BOOL);
105 Some of the following helper functions are duplicated in
109 /***********************************************************************
110 * X11DRV_DIB_GetXImageWidthBytes
112 * Return the width of an X image in bytes
114 static inline int X11DRV_DIB_GetXImageWidthBytes( int width, int depth )
116 if (!depth || depth > 32) goto error;
118 if (!ximageDepthTable[depth-1])
120 XImage *testimage = XCreateImage( gdi_display, visual, depth,
121 ZPixmap, 0, NULL, 1, 1, 32, 20 );
124 ximageDepthTable[depth-1] = testimage->bits_per_pixel;
125 XDestroyImage( testimage );
127 else ximageDepthTable[depth-1] = -1;
129 if (ximageDepthTable[depth-1] != -1)
130 return (4 * ((width * ximageDepthTable[depth-1] + 31) / 32));
133 WARN( "(%d): Unsupported depth\n", depth );
138 /***********************************************************************
139 * X11DRV_DIB_GetDIBWidthBytes
141 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
143 static int X11DRV_DIB_GetDIBWidthBytes( int width, int depth )
149 case 1: words = (width + 31) / 32; break;
150 case 4: words = (width + 7) / 8; break;
151 case 8: words = (width + 3) / 4; break;
153 case 16: words = (width + 1) / 2; break;
154 case 24: words = (width * 3 + 3) / 4; break;
156 WARN("(%d): Unsupported depth\n", depth );
165 /***********************************************************************
166 * X11DRV_DIB_GetDIBImageBytes
168 * Return the number of bytes used to hold the image in a DIB bitmap.
170 static int X11DRV_DIB_GetDIBImageBytes( int width, int height, int depth )
172 return X11DRV_DIB_GetDIBWidthBytes( width, depth ) * abs( height );
176 /***********************************************************************
177 * X11DRV_DIB_BitmapInfoSize
179 * Return the size of the bitmap info structure including color table.
181 int X11DRV_DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
185 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
187 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
188 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
189 return sizeof(BITMAPCOREHEADER) + colors *
190 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
192 else /* assume BITMAPINFOHEADER */
194 colors = info->bmiHeader.biClrUsed;
195 if (!colors && (info->bmiHeader.biBitCount <= 8))
196 colors = 1 << info->bmiHeader.biBitCount;
197 return sizeof(BITMAPINFOHEADER) + colors *
198 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
203 /***********************************************************************
204 * X11DRV_DIB_CreateXImage
208 XImage *X11DRV_DIB_CreateXImage( int width, int height, int depth )
214 width_bytes = X11DRV_DIB_GetXImageWidthBytes( width, depth );
215 image = XCreateImage( gdi_display, visual, depth, ZPixmap, 0,
216 calloc( height, width_bytes ),
217 width, height, 32, width_bytes );
223 /***********************************************************************
224 * DIB_GetBitmapInfoEx
226 * Get the info from a bitmap header.
227 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
229 static int DIB_GetBitmapInfoEx( const BITMAPINFOHEADER *header, LONG *width,
230 LONG *height, WORD *planes, WORD *bpp,
231 WORD *compr, DWORD *size )
233 if (header->biSize == sizeof(BITMAPCOREHEADER))
235 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
236 *width = core->bcWidth;
237 *height = core->bcHeight;
238 *planes = core->bcPlanes;
239 *bpp = core->bcBitCount;
244 if (header->biSize >= sizeof(BITMAPINFOHEADER))
246 *width = header->biWidth;
247 *height = header->biHeight;
248 *planes = header->biPlanes;
249 *bpp = header->biBitCount;
250 *compr = header->biCompression;
251 *size = header->biSizeImage;
254 ERR("(%d): unknown/wrong size for header\n", header->biSize );
259 /***********************************************************************
260 * X11DRV_DIB_GetColorCount
262 * Computes the number of colors for the bitmap palette.
263 * Should not be called for a >8-bit deep bitmap.
265 static unsigned int X11DRV_DIB_GetColorCount(const BITMAPINFO *info)
268 BOOL core_info = info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER);
272 colors = 1 << ((const BITMAPCOREINFO*)info)->bmciHeader.bcBitCount;
276 colors = info->bmiHeader.biClrUsed;
277 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
281 ERR("called with >256 colors!\n");
287 /***********************************************************************
290 * Get the info from a bitmap header.
291 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
293 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
294 LONG *height, WORD *bpp, WORD *compr )
299 return DIB_GetBitmapInfoEx( header, width, height, &planes, bpp, compr, &size);
303 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
305 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
306 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
309 /***********************************************************************
310 * X11DRV_DIB_GenColorMap
312 * Fills the color map of a bitmap palette. Should not be called
313 * for a >8-bit deep bitmap.
315 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
316 WORD coloruse, WORD depth, BOOL quads,
317 const void *colorPtr, int start, int end )
321 if (coloruse == DIB_RGB_COLORS)
325 const RGBQUAD * rgb = (const RGBQUAD *)colorPtr;
327 if (depth == 1) /* Monochrome */
332 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
333 invert = !colour_is_brighter(table[1], table[0]);
335 for (i = start; i < end; i++, rgb++)
336 colorMapping[i] = ((rgb->rgbRed + rgb->rgbGreen +
337 rgb->rgbBlue > 255*3/2 && !invert) ||
338 (rgb->rgbRed + rgb->rgbGreen +
339 rgb->rgbBlue <= 255*3/2 && invert));
342 for (i = start; i < end; i++, rgb++)
343 colorMapping[i] = X11DRV_PALETTE_ToPhysical( NULL, RGB(rgb->rgbRed,
349 const RGBTRIPLE * rgb = (const RGBTRIPLE *)colorPtr;
351 if (depth == 1) /* Monochrome */
356 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
357 invert = !colour_is_brighter(table[1], table[0]);
359 for (i = start; i < end; i++, rgb++)
360 colorMapping[i] = ((rgb->rgbtRed + rgb->rgbtGreen +
361 rgb->rgbtBlue > 255*3/2 && !invert) ||
362 (rgb->rgbtRed + rgb->rgbtGreen +
363 rgb->rgbtBlue <= 255*3/2 && invert));
366 for (i = start; i < end; i++, rgb++)
367 colorMapping[i] = X11DRV_PALETTE_ToPhysical( NULL, RGB(rgb->rgbtRed,
372 else /* DIB_PAL_COLORS */
374 const WORD * index = (const WORD *)colorPtr;
376 for (i = start; i < end; i++, index++)
377 colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(*index) );
383 /***********************************************************************
384 * X11DRV_DIB_BuildColorMap
386 * Build the color map from the bitmap palette. Should not be called
387 * for a >8-bit deep bitmap.
389 static int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE *physDev, WORD coloruse, WORD depth,
390 const BITMAPINFO *info, int *nColors )
393 const void *colorPtr;
397 *nColors = X11DRV_DIB_GetColorCount(info);
398 if (!*nColors) return NULL;
400 isInfo = info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER);
401 colorPtr = (const BYTE*)info + (WORD)info->bmiHeader.biSize;
402 if (!(colorMapping = HeapAlloc(GetProcessHeap(), 0, *nColors * sizeof(int) )))
405 return X11DRV_DIB_GenColorMap( physDev, colorMapping, coloruse, depth,
406 isInfo, colorPtr, 0, *nColors);
409 /***********************************************************************
410 * X11DRV_DIB_MapColor
412 static int X11DRV_DIB_MapColor( int *physMap, int nPhysMap, int phys, int oldcol )
416 if ((oldcol < nPhysMap) && (physMap[oldcol] == phys))
419 for (color = 0; color < nPhysMap; color++)
420 if (physMap[color] == phys)
423 WARN("Strange color %08x\n", phys);
428 /*********************************************************************
429 * X11DRV_DIB_GetNearestIndex
431 * Helper for X11DRV_DIB_GetDIBits.
432 * Returns the nearest colour table index for a given RGB.
433 * Nearest is defined by minimizing the sum of the squares.
435 static INT X11DRV_DIB_GetNearestIndex(RGBQUAD *colormap, int numColors, BYTE r, BYTE g, BYTE b)
437 INT i, best = -1, diff, bestdiff = -1;
440 for(color = colormap, i = 0; i < numColors; color++, i++) {
441 diff = (r - color->rgbRed) * (r - color->rgbRed) +
442 (g - color->rgbGreen) * (g - color->rgbGreen) +
443 (b - color->rgbBlue) * (b - color->rgbBlue);
446 if(best == -1 || diff < bestdiff) {
453 /*********************************************************************
454 * X11DRV_DIB_MaskToShift
456 * Helper for X11DRV_DIB_GetDIBits.
457 * Returns the by how many bits to shift a given color so that it is
458 * in the proper position.
460 INT X11DRV_DIB_MaskToShift(DWORD mask)
468 while ((mask&1)==0) {
475 /***********************************************************************
476 * X11DRV_DIB_CheckMask
478 * Check RGB mask if it is either 0 or matches visual's mask.
480 static inline int X11DRV_DIB_CheckMask(int red_mask, int green_mask, int blue_mask)
482 return ( red_mask == 0 && green_mask == 0 && blue_mask == 0 ) ||
483 ( red_mask == visual->red_mask && green_mask == visual->green_mask &&
484 blue_mask == visual->blue_mask );
487 /***********************************************************************
488 * X11DRV_DIB_SetImageBits_1
490 * SetDIBits for a 1-bit deep DIB.
492 static void X11DRV_DIB_SetImageBits_1( int lines, const BYTE *srcbits,
493 DWORD srcwidth, DWORD dstwidth, int left,
494 int *colors, XImage *bmpImage, DWORD linebytes)
503 srcbits = srcbits + linebytes * (lines - 1);
504 linebytes = -linebytes;
507 if ((extra = (left & 7)) != 0) {
511 srcbits += left >> 3;
512 width = min(srcwidth, dstwidth);
514 /* ==== pal 1 dib -> any bmp format ==== */
515 for (h = lines-1; h >=0; h--) {
517 for (i = width/8, x = left; i > 0; i--) {
519 XPutPixel( bmpImage, x++, h, colors[ srcval >> 7] );
520 XPutPixel( bmpImage, x++, h, colors[(srcval >> 6) & 1] );
521 XPutPixel( bmpImage, x++, h, colors[(srcval >> 5) & 1] );
522 XPutPixel( bmpImage, x++, h, colors[(srcval >> 4) & 1] );
523 XPutPixel( bmpImage, x++, h, colors[(srcval >> 3) & 1] );
524 XPutPixel( bmpImage, x++, h, colors[(srcval >> 2) & 1] );
525 XPutPixel( bmpImage, x++, h, colors[(srcval >> 1) & 1] );
526 XPutPixel( bmpImage, x++, h, colors[ srcval & 1] );
532 case 7: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
533 case 6: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
534 case 5: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
535 case 4: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
536 case 3: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
537 case 2: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
538 case 1: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]);
541 srcbits += linebytes;
545 /***********************************************************************
546 * X11DRV_DIB_GetImageBits_1
548 * GetDIBits for a 1-bit deep DIB.
550 static void X11DRV_DIB_GetImageBits_1( int lines, BYTE *dstbits,
551 DWORD dstwidth, DWORD srcwidth,
552 RGBQUAD *colors, PALETTEENTRY *srccolors,
553 XImage *bmpImage, DWORD linebytes )
556 int h, width = min(dstwidth, srcwidth);
560 dstbits = dstbits + linebytes * (lines - 1);
561 linebytes = -linebytes;
564 switch (bmpImage->depth)
568 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
570 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
573 for (h=lines-1; h>=0; h--) {
577 for (x=0; x<width; x++) {
579 srcval=srccolors[XGetPixel(bmpImage, x, h)];
580 dstval|=(X11DRV_DIB_GetNearestIndex
584 srcval.peBlue) << (7 - (x & 7)));
593 dstbits += linebytes;
601 if (X11DRV_DIB_CheckMask(bmpImage->red_mask, bmpImage->green_mask, bmpImage->blue_mask)
603 /* ==== pal 8 bmp -> pal 1 dib ==== */
605 const BYTE* srcpixel;
608 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
610 for (h=0; h<lines; h++) {
615 for (x=0; x<width; x++) {
617 srcval=srccolors[*srcpixel++];
618 dstval|=(X11DRV_DIB_GetNearestIndex
622 srcval.peBlue) << (7-(x&7)) );
631 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
632 dstbits += linebytes;
643 const WORD* srcpixel;
646 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
648 if (bmpImage->green_mask==0x03e0) {
649 if (bmpImage->red_mask==0x7c00) {
650 /* ==== rgb 555 bmp -> pal 1 dib ==== */
651 for (h=0; h<lines; h++) {
656 for (x=0; x<width; x++) {
659 dstval|=(X11DRV_DIB_GetNearestIndex
661 ((srcval >> 7) & 0xf8) | /* r */
662 ((srcval >> 12) & 0x07),
663 ((srcval >> 2) & 0xf8) | /* g */
664 ((srcval >> 7) & 0x07),
665 ((srcval << 3) & 0xf8) | /* b */
666 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
675 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
676 dstbits += linebytes;
678 } else if (bmpImage->blue_mask==0x7c00) {
679 /* ==== bgr 555 bmp -> pal 1 dib ==== */
680 for (h=0; h<lines; h++) {
685 for (x=0; x<width; x++) {
688 dstval|=(X11DRV_DIB_GetNearestIndex
690 ((srcval << 3) & 0xf8) | /* r */
691 ((srcval >> 2) & 0x07),
692 ((srcval >> 2) & 0xf8) | /* g */
693 ((srcval >> 7) & 0x07),
694 ((srcval >> 7) & 0xf8) | /* b */
695 ((srcval >> 12) & 0x07) ) << (7-(x&7)) );
704 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
705 dstbits += linebytes;
710 } else if (bmpImage->green_mask==0x07e0) {
711 if (bmpImage->red_mask==0xf800) {
712 /* ==== rgb 565 bmp -> pal 1 dib ==== */
713 for (h=0; h<lines; h++) {
718 for (x=0; x<width; x++) {
721 dstval|=(X11DRV_DIB_GetNearestIndex
723 ((srcval >> 8) & 0xf8) | /* r */
724 ((srcval >> 13) & 0x07),
725 ((srcval >> 3) & 0xfc) | /* g */
726 ((srcval >> 9) & 0x03),
727 ((srcval << 3) & 0xf8) | /* b */
728 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
737 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
738 dstbits += linebytes;
740 } else if (bmpImage->blue_mask==0xf800) {
741 /* ==== bgr 565 bmp -> pal 1 dib ==== */
742 for (h=0; h<lines; h++) {
747 for (x=0; x<width; x++) {
750 dstval|=(X11DRV_DIB_GetNearestIndex
752 ((srcval << 3) & 0xf8) | /* r */
753 ((srcval >> 2) & 0x07),
754 ((srcval >> 3) & 0xfc) | /* g */
755 ((srcval >> 9) & 0x03),
756 ((srcval >> 8) & 0xf8) | /* b */
757 ((srcval >> 13) & 0x07) ) << (7-(x&7)) );
766 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
767 dstbits += linebytes;
786 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
787 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
789 if (bmpImage->green_mask!=0x00ff00 ||
790 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
792 } else if (bmpImage->blue_mask==0xff) {
793 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
794 for (h=0; h<lines; h++) {
799 for (x=0; x<width; x++) {
800 dstval|=(X11DRV_DIB_GetNearestIndex
804 srcbyte[0]) << (7-(x&7)) );
805 srcbyte+=bytes_per_pixel;
814 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
815 dstbits += linebytes;
818 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
819 for (h=0; h<lines; h++) {
824 for (x=0; x<width; x++) {
825 dstval|=(X11DRV_DIB_GetNearestIndex
829 srcbyte[2]) << (7-(x&7)) );
830 srcbyte+=bytes_per_pixel;
839 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
840 dstbits += linebytes;
851 unsigned long white = (1 << bmpImage->bits_per_pixel) - 1;
853 /* ==== any bmp format -> pal 1 dib ==== */
854 if ((unsigned)colors[0].rgbRed+colors[0].rgbGreen+colors[0].rgbBlue >=
855 (unsigned)colors[1].rgbRed+colors[1].rgbGreen+colors[1].rgbBlue )
858 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB, "
859 "%s color mapping\n",
860 bmpImage->bits_per_pixel, bmpImage->red_mask,
861 bmpImage->green_mask, bmpImage->blue_mask,
862 neg?"negative":"direct" );
864 for (h=lines-1; h>=0; h--) {
868 for (x=0; x<width; x++) {
869 dstval|=((XGetPixel( bmpImage, x, h) >= white) ^ neg) << (7 - (x&7));
878 dstbits += linebytes;
885 /***********************************************************************
886 * X11DRV_DIB_SetImageBits_4
888 * SetDIBits for a 4-bit deep DIB.
890 static void X11DRV_DIB_SetImageBits_4( int lines, const BYTE *srcbits,
891 DWORD srcwidth, DWORD dstwidth, int left,
892 int *colors, XImage *bmpImage, DWORD linebytes)
900 srcbits = srcbits + linebytes * (lines - 1);
901 linebytes = -linebytes;
908 srcbits += left >> 1;
909 width = min(srcwidth, dstwidth);
911 /* ==== pal 4 dib -> any bmp format ==== */
912 for (h = lines-1; h >= 0; h--) {
914 for (i = width/2, x = left; i > 0; i--) {
915 BYTE srcval=*srcbyte++;
916 XPutPixel( bmpImage, x++, h, colors[srcval >> 4] );
917 XPutPixel( bmpImage, x++, h, colors[srcval & 0x0f] );
920 XPutPixel( bmpImage, x, h, colors[*srcbyte >> 4] );
921 srcbits += linebytes;
927 /***********************************************************************
928 * X11DRV_DIB_GetImageBits_4
930 * GetDIBits for a 4-bit deep DIB.
932 static void X11DRV_DIB_GetImageBits_4( int lines, BYTE *dstbits,
933 DWORD srcwidth, DWORD dstwidth,
934 RGBQUAD *colors, PALETTEENTRY *srccolors,
935 XImage *bmpImage, DWORD linebytes )
938 int h, width = min(srcwidth, dstwidth);
944 dstbits = dstbits + ( linebytes * (lines-1) );
945 linebytes = -linebytes;
950 switch (bmpImage->depth) {
953 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
955 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
958 for (h = lines-1; h >= 0; h--) {
962 for (x = 0; x < width; x++) {
964 srcval=srccolors[XGetPixel(bmpImage, x, h)];
965 dstval|=(X11DRV_DIB_GetNearestIndex
969 srcval.peBlue) << (4-((x&1)<<2)));
978 dstbits += linebytes;
986 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
988 /* ==== pal 8 bmp -> pal 4 dib ==== */
990 const BYTE *srcpixel;
993 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
994 for (h=0; h<lines; h++) {
999 for (x=0; x<width; x++) {
1000 PALETTEENTRY srcval;
1001 srcval = srccolors[*srcpixel++];
1002 dstval|=(X11DRV_DIB_GetNearestIndex
1006 srcval.peBlue) << (4*(1-(x&1))) );
1015 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1016 dstbits += linebytes;
1026 const void* srcbits;
1027 const WORD* srcpixel;
1030 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1032 if (bmpImage->green_mask==0x03e0) {
1033 if (bmpImage->red_mask==0x7c00) {
1034 /* ==== rgb 555 bmp -> pal 4 dib ==== */
1035 for (h=0; h<lines; h++) {
1040 for (x=0; x<width; x++) {
1043 dstval|=(X11DRV_DIB_GetNearestIndex
1045 ((srcval >> 7) & 0xf8) | /* r */
1046 ((srcval >> 12) & 0x07),
1047 ((srcval >> 2) & 0xf8) | /* g */
1048 ((srcval >> 7) & 0x07),
1049 ((srcval << 3) & 0xf8) | /* b */
1050 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1059 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1060 dstbits += linebytes;
1062 } else if (bmpImage->blue_mask==0x7c00) {
1063 /* ==== bgr 555 bmp -> pal 4 dib ==== */
1064 for (h=0; h<lines; h++) {
1069 for (x=0; x<width; x++) {
1072 dstval|=(X11DRV_DIB_GetNearestIndex
1074 ((srcval << 3) & 0xf8) | /* r */
1075 ((srcval >> 2) & 0x07),
1076 ((srcval >> 2) & 0xf8) | /* g */
1077 ((srcval >> 7) & 0x07),
1078 ((srcval >> 7) & 0xf8) | /* b */
1079 ((srcval >> 12) & 0x07) ) << ((1-(x&1))<<2) );
1088 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1089 dstbits += linebytes;
1094 } else if (bmpImage->green_mask==0x07e0) {
1095 if (bmpImage->red_mask==0xf800) {
1096 /* ==== rgb 565 bmp -> pal 4 dib ==== */
1097 for (h=0; h<lines; h++) {
1102 for (x=0; x<width; x++) {
1105 dstval|=(X11DRV_DIB_GetNearestIndex
1107 ((srcval >> 8) & 0xf8) | /* r */
1108 ((srcval >> 13) & 0x07),
1109 ((srcval >> 3) & 0xfc) | /* g */
1110 ((srcval >> 9) & 0x03),
1111 ((srcval << 3) & 0xf8) | /* b */
1112 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1121 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1122 dstbits += linebytes;
1124 } else if (bmpImage->blue_mask==0xf800) {
1125 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1126 for (h=0; h<lines; h++) {
1131 for (x=0; x<width; x++) {
1134 dstval|=(X11DRV_DIB_GetNearestIndex
1136 ((srcval << 3) & 0xf8) | /* r */
1137 ((srcval >> 2) & 0x07),
1138 ((srcval >> 3) & 0xfc) | /* g */
1139 ((srcval >> 9) & 0x03),
1140 ((srcval >> 8) & 0xf8) | /* b */
1141 ((srcval >> 13) & 0x07) ) << ((1-(x&1))<<2) );
1150 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1151 dstbits += linebytes;
1163 if (bmpImage->bits_per_pixel==24) {
1164 const void* srcbits;
1165 const BYTE *srcbyte;
1168 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1170 if (bmpImage->green_mask!=0x00ff00 ||
1171 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1173 } else if (bmpImage->blue_mask==0xff) {
1174 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1175 for (h=0; h<lines; h++) {
1178 for (x=0; x<width/2; x++) {
1179 /* Do 2 pixels at a time */
1180 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1185 X11DRV_DIB_GetNearestIndex
1193 /* And then the odd pixel */
1194 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1200 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1201 dstbits += linebytes;
1204 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1205 for (h=0; h<lines; h++) {
1208 for (x=0; x<width/2; x++) {
1209 /* Do 2 pixels at a time */
1210 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1215 X11DRV_DIB_GetNearestIndex
1223 /* And then the odd pixel */
1224 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1230 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1231 dstbits += linebytes;
1240 const void* srcbits;
1241 const BYTE *srcbyte;
1244 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1246 if (bmpImage->green_mask!=0x00ff00 ||
1247 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1249 } else if (bmpImage->blue_mask==0xff) {
1250 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1251 for (h=0; h<lines; h++) {
1254 for (x=0; x<width/2; x++) {
1255 /* Do 2 pixels at a time */
1256 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1261 X11DRV_DIB_GetNearestIndex
1269 /* And then the odd pixel */
1270 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1276 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1277 dstbits += linebytes;
1280 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1281 for (h=0; h<lines; h++) {
1284 for (x=0; x<width/2; x++) {
1285 /* Do 2 pixels at a time */
1286 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1291 X11DRV_DIB_GetNearestIndex
1299 /* And then the odd pixel */
1300 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1306 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1307 dstbits += linebytes;
1318 /* ==== any bmp format -> pal 4 dib ==== */
1319 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1320 bmpImage->bits_per_pixel, bmpImage->red_mask,
1321 bmpImage->green_mask, bmpImage->blue_mask );
1322 for (h=lines-1; h>=0; h--) {
1324 for (x=0; x<(width & ~1); x+=2) {
1325 *dstbyte++=(X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4) |
1326 X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x+1, h), 0);
1329 *dstbyte=(X11DRV_DIB_MapColor((int *)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4);
1331 dstbits += linebytes;
1338 /***********************************************************************
1339 * X11DRV_DIB_SetImageBits_RLE4
1341 * SetDIBits for a 4-bit deep compressed DIB.
1343 static void X11DRV_DIB_SetImageBits_RLE4( int lines, const BYTE *bits,
1344 DWORD srcwidth, DWORD dstwidth,
1345 int left, int *colors,
1348 unsigned int x = 0, width = min(srcwidth, dstwidth);
1349 int y = lines - 1, c, length;
1350 const BYTE *begin = bits;
1355 if (length) { /* encoded */
1358 if (x >= (left + width)) break;
1359 if( x >= left) XPutPixel(bmpImage, x, y, colors[c >> 4]);
1361 if (!length--) break;
1362 if (x >= (left + width)) break;
1363 if( x >= left) XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1383 default: /* absolute */
1386 if (x >= left && x < (left + width))
1387 XPutPixel(bmpImage, x, y, colors[c >> 4]);
1389 if (!length--) break;
1390 if (x >= left && x < (left + width))
1391 XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1394 if ((bits - begin) & 1)
1403 /***********************************************************************
1404 * X11DRV_DIB_SetImageBits_8
1406 * SetDIBits for an 8-bit deep DIB.
1408 static void X11DRV_DIB_SetImageBits_8( int lines, const BYTE *srcbits,
1409 DWORD srcwidth, DWORD dstwidth, int left,
1410 const int *colors, XImage *bmpImage,
1414 int h, width = min(srcwidth, dstwidth);
1415 const BYTE* srcbyte;
1421 srcbits = srcbits + linebytes * (lines-1);
1422 linebytes = -linebytes;
1427 switch (bmpImage->depth) {
1430 /* Some X servers might have 32 bit/ 16bit deep pixel */
1431 if (lines && width && (bmpImage->bits_per_pixel == 16) &&
1432 (ImageByteOrder(gdi_display)==LSBFirst) )
1434 /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1435 dstbits=(BYTE*)bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1436 for (h = lines ; h--; ) {
1437 #if defined(__i386__) && defined(__GNUC__)
1438 int _cl1,_cl2; /* temp outputs for asm below */
1439 /* Borrowed from DirectDraw */
1440 __asm__ __volatile__(
1445 " movw (%%edx,%%eax,4),%%ax\n"
1447 " xor %%eax,%%eax\n"
1449 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1454 :"eax", "cc", "memory"
1457 DWORD* dstpixel=(DWORD*)dstbits;
1458 for (x=0; x<width/2; x++) {
1459 /* Do 2 pixels at a time */
1460 *dstpixel++=(colors[srcbyte[1]] << 16) | colors[srcbyte[0]];
1464 /* And then the odd pixel */
1465 *((WORD*)dstpixel)=colors[srcbyte[0]];
1468 srcbyte = (srcbits += linebytes);
1469 dstbits -= bmpImage->bytes_per_line;
1476 if (lines && width && (bmpImage->bits_per_pixel == 32) &&
1477 (ImageByteOrder(gdi_display)==LSBFirst) )
1479 dstbits=(BYTE*)bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
1480 /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1481 for (h = lines ; h--; ) {
1482 #if defined(__i386__) && defined(__GNUC__)
1483 int _cl1,_cl2; /* temp outputs for asm below */
1484 /* Borrowed from DirectDraw */
1485 __asm__ __volatile__(
1490 " movl (%%edx,%%eax,4),%%eax\n"
1492 " xor %%eax,%%eax\n"
1494 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1499 :"eax", "cc", "memory"
1502 DWORD* dstpixel=(DWORD*)dstbits;
1503 for (x=0; x<width; x++) {
1504 *dstpixel++=colors[*srcbyte++];
1507 srcbyte = (srcbits += linebytes);
1508 dstbits -= bmpImage->bytes_per_line;
1514 break; /* use slow generic case below */
1517 /* ==== pal 8 dib -> any bmp format ==== */
1518 for (h=lines-1; h>=0; h--) {
1519 for (x=left; x<width+left; x++) {
1520 XPutPixel(bmpImage, x, h, colors[*srcbyte++]);
1522 srcbyte = (srcbits += linebytes);
1526 /***********************************************************************
1527 * X11DRV_DIB_GetImageBits_8
1529 * GetDIBits for an 8-bit deep DIB.
1531 static void X11DRV_DIB_GetImageBits_8( int lines, BYTE *dstbits,
1532 DWORD srcwidth, DWORD dstwidth,
1533 RGBQUAD *colors, PALETTEENTRY *srccolors,
1534 XImage *bmpImage, DWORD linebytes )
1537 int h, width = min(srcwidth, dstwidth);
1543 dstbits = dstbits + ( linebytes * (lines-1) );
1544 linebytes = -linebytes;
1549 * This condition is true when GetImageBits has been called by
1550 * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1551 * 256 colormaps, so we'll just use it for GetDIBits calls.
1552 * (In some cases, in an updateDIBSection, the returned colors are bad too)
1554 if (!srccolors) goto updatesection;
1556 switch (bmpImage->depth) {
1559 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1562 /* ==== pal 1 bmp -> pal 8 dib ==== */
1563 /* ==== pal 4 bmp -> pal 8 dib ==== */
1564 for (h=lines-1; h>=0; h--) {
1566 for (x=0; x<width; x++) {
1567 PALETTEENTRY srcval;
1568 srcval=srccolors[XGetPixel(bmpImage, x, h)];
1569 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1574 dstbits += linebytes;
1582 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1584 /* ==== pal 8 bmp -> pal 8 dib ==== */
1585 const void* srcbits;
1586 const BYTE* srcpixel;
1588 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1589 for (h=0; h<lines; h++) {
1592 for (x = 0; x < width; x++) {
1593 PALETTEENTRY srcval;
1594 srcval=srccolors[*srcpixel++];
1595 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1600 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1601 dstbits += linebytes;
1611 const void* srcbits;
1612 const WORD* srcpixel;
1615 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1617 if (bmpImage->green_mask==0x03e0) {
1618 if (bmpImage->red_mask==0x7c00) {
1619 /* ==== rgb 555 bmp -> pal 8 dib ==== */
1620 for (h=0; h<lines; h++) {
1623 for (x=0; x<width; x++) {
1626 *dstbyte++=X11DRV_DIB_GetNearestIndex
1628 ((srcval >> 7) & 0xf8) | /* r */
1629 ((srcval >> 12) & 0x07),
1630 ((srcval >> 2) & 0xf8) | /* g */
1631 ((srcval >> 7) & 0x07),
1632 ((srcval << 3) & 0xf8) | /* b */
1633 ((srcval >> 2) & 0x07) );
1635 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1636 dstbits += linebytes;
1638 } else if (bmpImage->blue_mask==0x7c00) {
1639 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1640 for (h=0; h<lines; h++) {
1643 for (x=0; x<width; x++) {
1646 *dstbyte++=X11DRV_DIB_GetNearestIndex
1648 ((srcval << 3) & 0xf8) | /* r */
1649 ((srcval >> 2) & 0x07),
1650 ((srcval >> 2) & 0xf8) | /* g */
1651 ((srcval >> 7) & 0x07),
1652 ((srcval >> 7) & 0xf8) | /* b */
1653 ((srcval >> 12) & 0x07) );
1655 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1656 dstbits += linebytes;
1661 } else if (bmpImage->green_mask==0x07e0) {
1662 if (bmpImage->red_mask==0xf800) {
1663 /* ==== rgb 565 bmp -> pal 8 dib ==== */
1664 for (h=0; h<lines; h++) {
1667 for (x=0; x<width; x++) {
1670 *dstbyte++=X11DRV_DIB_GetNearestIndex
1672 ((srcval >> 8) & 0xf8) | /* r */
1673 ((srcval >> 13) & 0x07),
1674 ((srcval >> 3) & 0xfc) | /* g */
1675 ((srcval >> 9) & 0x03),
1676 ((srcval << 3) & 0xf8) | /* b */
1677 ((srcval >> 2) & 0x07) );
1679 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1680 dstbits += linebytes;
1682 } else if (bmpImage->blue_mask==0xf800) {
1683 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1684 for (h=0; h<lines; h++) {
1687 for (x=0; x<width; x++) {
1690 *dstbyte++=X11DRV_DIB_GetNearestIndex
1692 ((srcval << 3) & 0xf8) | /* r */
1693 ((srcval >> 2) & 0x07),
1694 ((srcval >> 3) & 0xfc) | /* g */
1695 ((srcval >> 9) & 0x03),
1696 ((srcval >> 8) & 0xf8) | /* b */
1697 ((srcval >> 13) & 0x07) );
1699 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1700 dstbits += linebytes;
1714 const void* srcbits;
1715 const BYTE *srcbyte;
1717 int bytes_per_pixel;
1719 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1720 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
1722 if (bmpImage->green_mask!=0x00ff00 ||
1723 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1725 } else if (bmpImage->blue_mask==0xff) {
1726 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1727 for (h=0; h<lines; h++) {
1730 for (x=0; x<width; x++) {
1731 *dstbyte++=X11DRV_DIB_GetNearestIndex
1736 srcbyte+=bytes_per_pixel;
1738 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1739 dstbits += linebytes;
1742 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1743 for (h=0; h<lines; h++) {
1746 for (x=0; x<width; x++) {
1747 *dstbyte++=X11DRV_DIB_GetNearestIndex
1752 srcbyte+=bytes_per_pixel;
1754 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1755 dstbits += linebytes;
1763 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1764 bmpImage->depth, bmpImage->red_mask,
1765 bmpImage->green_mask, bmpImage->blue_mask );
1767 /* ==== any bmp format -> pal 8 dib ==== */
1768 for (h=lines-1; h>=0; h--) {
1770 for (x=0; x<width; x++) {
1771 *dstbyte=X11DRV_DIB_MapColor
1773 XGetPixel(bmpImage, x, h), *dstbyte);
1776 dstbits += linebytes;
1782 /***********************************************************************
1783 * X11DRV_DIB_SetImageBits_RLE8
1785 * SetDIBits for an 8-bit deep compressed DIB.
1787 * This function rewritten 941113 by James Youngman. WINE blew out when I
1788 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
1790 * This was because the algorithm assumed that all RLE8 bitmaps end with the
1791 * 'End of bitmap' escape code. This code is very much laxer in what it
1792 * allows to end the expansion. Possibly too lax. See the note by
1793 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
1794 * bitmap should end with RleEnd, but on the other hand, software exists
1795 * that produces ones that don't and Windows 3.1 doesn't complain a bit
1798 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
1799 * James A. Youngman <mbcstjy@afs.man.ac.uk>
1802 static void X11DRV_DIB_SetImageBits_RLE8( int lines, const BYTE *bits,
1803 DWORD srcwidth, DWORD dstwidth,
1804 int left, int *colors,
1807 unsigned int x; /* X-position on each line. Increases. */
1808 int y; /* Line #. Starts at lines-1, decreases */
1809 const BYTE *pIn = bits; /* Pointer to current position in bits */
1810 BYTE length; /* The length pf a run */
1811 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
1814 * Note that the bitmap data is stored by Windows starting at the
1815 * bottom line of the bitmap and going upwards. Within each line,
1816 * the data is stored left-to-right. That's the reason why line
1817 * goes from lines-1 to 0. [JAY]
1827 * If the length byte is not zero (which is the escape value),
1828 * We have a run of length pixels all the same colour. The colour
1829 * index is stored next.
1831 * If the length byte is zero, we need to read the next byte to
1832 * know what to do. [JAY]
1837 * [Run-Length] Encoded mode
1839 int color = colors[*pIn++];
1840 while (length-- && x < (left + dstwidth)) {
1841 if( x >= left) XPutPixel(bmpImage, x, y, color);
1848 * Escape codes (may be an absolute sequence though)
1850 escape_code = (*pIn++);
1859 /* Not all RLE8 bitmaps end with this code. For
1860 * example, Paint Shop Pro produces some that don't.
1861 * That's (I think) what caused the previous
1862 * implementation to fail. [JAY]
1871 default: /* switch to absolute mode */
1872 length = escape_code;
1875 int color = colors[*pIn++];
1876 if (x >= (left + dstwidth))
1881 if( x >= left) XPutPixel(bmpImage, x, y, color);
1885 * If you think for a moment you'll realise that the
1886 * only time we could ever possibly read an odd
1887 * number of bytes is when there is a 0x00 (escape),
1888 * a value >0x02 (absolute mode) and then an odd-
1889 * length run. Therefore this is the only place we
1890 * need to worry about it. Everywhere else the
1891 * bytes are always read in pairs. [JAY]
1893 if (escape_code & 1) pIn++; /* Throw away the pad byte. */
1895 } /* switch (escape_code) : Escape sequence */
1901 /***********************************************************************
1902 * X11DRV_DIB_SetImageBits_16
1904 * SetDIBits for a 16-bit deep DIB.
1906 static void X11DRV_DIB_SetImageBits_16( int lines, const BYTE *srcbits,
1907 DWORD srcwidth, DWORD dstwidth, int left,
1908 X11DRV_PDEVICE *physDev, DWORD rSrc, DWORD gSrc, DWORD bSrc,
1909 XImage *bmpImage, DWORD linebytes )
1912 int h, width = min(srcwidth, dstwidth);
1913 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
1918 srcbits = srcbits + ( linebytes * (lines-1));
1919 linebytes = -linebytes;
1922 switch (bmpImage->depth)
1929 srcbits=srcbits+left*2;
1930 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1932 if (bmpImage->green_mask==0x03e0) {
1933 if (gSrc==bmpImage->green_mask) {
1934 if (rSrc==bmpImage->red_mask) {
1935 /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1936 /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1937 convs->Convert_5x5_asis
1940 dstbits,-bmpImage->bytes_per_line);
1941 } else if (rSrc==bmpImage->blue_mask) {
1942 /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1943 /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1944 convs->Convert_555_reverse
1947 dstbits,-bmpImage->bytes_per_line);
1950 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1951 /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1952 /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1953 convs->Convert_565_to_555_asis
1956 dstbits,-bmpImage->bytes_per_line);
1958 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1959 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1960 convs->Convert_565_to_555_reverse
1963 dstbits,-bmpImage->bytes_per_line);
1966 } else if (bmpImage->green_mask==0x07e0) {
1967 if (gSrc==bmpImage->green_mask) {
1968 if (rSrc==bmpImage->red_mask) {
1969 /* ==== rgb 565 dib -> rgb 565 bmp ==== */
1970 /* ==== bgr 565 dib -> bgr 565 bmp ==== */
1971 convs->Convert_5x5_asis
1974 dstbits,-bmpImage->bytes_per_line);
1976 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
1977 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
1978 convs->Convert_565_reverse
1981 dstbits,-bmpImage->bytes_per_line);
1984 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1985 /* ==== rgb 555 dib -> rgb 565 bmp ==== */
1986 /* ==== bgr 555 dib -> bgr 565 bmp ==== */
1987 convs->Convert_555_to_565_asis
1990 dstbits,-bmpImage->bytes_per_line);
1992 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
1993 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
1994 convs->Convert_555_to_565_reverse
1997 dstbits,-bmpImage->bytes_per_line);
2007 if (bmpImage->bits_per_pixel==24) {
2010 srcbits=srcbits+left*2;
2011 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2013 if (bmpImage->green_mask!=0x00ff00 ||
2014 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2016 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2017 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2019 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
2020 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
2021 convs->Convert_555_to_888_asis
2024 dstbits,-bmpImage->bytes_per_line);
2026 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
2027 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
2028 convs->Convert_565_to_888_asis
2031 dstbits,-bmpImage->bytes_per_line);
2035 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
2036 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
2037 convs->Convert_555_to_888_reverse
2040 dstbits,-bmpImage->bytes_per_line);
2042 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
2043 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
2044 convs->Convert_565_to_888_reverse
2047 dstbits,-bmpImage->bytes_per_line);
2058 srcbits=srcbits+left*2;
2059 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2061 if (bmpImage->green_mask!=0x00ff00 ||
2062 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2064 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2065 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2067 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
2068 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
2069 convs->Convert_555_to_0888_asis
2072 dstbits,-bmpImage->bytes_per_line);
2074 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
2075 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
2076 convs->Convert_565_to_0888_asis
2079 dstbits,-bmpImage->bytes_per_line);
2083 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
2084 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
2085 convs->Convert_555_to_0888_reverse
2088 dstbits,-bmpImage->bytes_per_line);
2090 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
2091 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
2092 convs->Convert_565_to_0888_reverse
2095 dstbits,-bmpImage->bytes_per_line);
2103 WARN("from 16 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2104 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2105 bmpImage->green_mask, bmpImage->blue_mask );
2111 /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
2112 const WORD* srcpixel;
2113 int rShift1,gShift1,bShift1;
2114 int rShift2,gShift2,bShift2;
2117 /* Set color scaling values */
2118 rShift1=16+X11DRV_DIB_MaskToShift(rSrc)-3;
2119 gShift1=16+X11DRV_DIB_MaskToShift(gSrc)-3;
2120 bShift1=16+X11DRV_DIB_MaskToShift(bSrc)-3;
2125 /* Green has 5 bits, like the others */
2129 /* Green has 6 bits, not 5. Compensate. */
2138 /* We could split it into four separate cases to optimize
2139 * but it is probably not worth it.
2141 for (h=lines-1; h>=0; h--) {
2142 srcpixel=(const WORD*)srcbits;
2143 for (x=left; x<width+left; x++) {
2145 BYTE red,green,blue;
2146 srcval=*srcpixel++ << 16;
2147 red= ((srcval >> rShift1) & 0xf8) |
2148 ((srcval >> rShift2) & 0x07);
2149 green=((srcval >> gShift1) & gMask1) |
2150 ((srcval >> gShift2) & gMask2);
2151 blue= ((srcval >> bShift1) & 0xf8) |
2152 ((srcval >> bShift2) & 0x07);
2153 XPutPixel(bmpImage, x, h,
2154 X11DRV_PALETTE_ToPhysical
2155 (physDev, RGB(red,green,blue)));
2157 srcbits += linebytes;
2165 /***********************************************************************
2166 * X11DRV_DIB_GetImageBits_16
2168 * GetDIBits for an 16-bit deep DIB.
2170 static void X11DRV_DIB_GetImageBits_16( int lines, BYTE *dstbits,
2171 DWORD dstwidth, DWORD srcwidth,
2172 PALETTEENTRY *srccolors,
2173 DWORD rDst, DWORD gDst, DWORD bDst,
2174 XImage *bmpImage, DWORD dibpitch )
2177 int h, width = min(srcwidth, dstwidth);
2178 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2180 DWORD linebytes = dibpitch;
2185 dstbits = dstbits + ( linebytes * (lines-1));
2186 linebytes = -linebytes;
2189 switch (bmpImage->depth)
2194 const char* srcbits;
2196 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2198 if (bmpImage->green_mask==0x03e0) {
2199 if (gDst==bmpImage->green_mask) {
2200 if (rDst==bmpImage->red_mask) {
2201 /* ==== rgb 555 bmp -> rgb 555 dib ==== */
2202 /* ==== bgr 555 bmp -> bgr 555 dib ==== */
2203 convs->Convert_5x5_asis
2205 srcbits,-bmpImage->bytes_per_line,
2208 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
2209 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
2210 convs->Convert_555_reverse
2212 srcbits,-bmpImage->bytes_per_line,
2216 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2217 /* ==== rgb 555 bmp -> rgb 565 dib ==== */
2218 /* ==== bgr 555 bmp -> bgr 565 dib ==== */
2219 convs->Convert_555_to_565_asis
2221 srcbits,-bmpImage->bytes_per_line,
2224 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
2225 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
2226 convs->Convert_555_to_565_reverse
2228 srcbits,-bmpImage->bytes_per_line,
2232 } else if (bmpImage->green_mask==0x07e0) {
2233 if (gDst==bmpImage->green_mask) {
2234 if (rDst == bmpImage->red_mask) {
2235 /* ==== rgb 565 bmp -> rgb 565 dib ==== */
2236 /* ==== bgr 565 bmp -> bgr 565 dib ==== */
2237 convs->Convert_5x5_asis
2239 srcbits,-bmpImage->bytes_per_line,
2242 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
2243 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
2244 convs->Convert_565_reverse
2246 srcbits,-bmpImage->bytes_per_line,
2250 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2251 /* ==== rgb 565 bmp -> rgb 555 dib ==== */
2252 /* ==== bgr 565 bmp -> bgr 555 dib ==== */
2253 convs->Convert_565_to_555_asis
2255 srcbits,-bmpImage->bytes_per_line,
2258 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
2259 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
2260 convs->Convert_565_to_555_reverse
2262 srcbits,-bmpImage->bytes_per_line,
2273 if (bmpImage->bits_per_pixel == 24) {
2274 const char* srcbits;
2276 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2278 if (bmpImage->green_mask!=0x00ff00 ||
2279 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2281 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2282 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2284 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
2285 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
2286 convs->Convert_888_to_555_asis
2288 srcbits,-bmpImage->bytes_per_line,
2291 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2292 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2293 convs->Convert_888_to_565_asis
2295 srcbits,-bmpImage->bytes_per_line,
2300 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
2301 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
2302 convs->Convert_888_to_555_reverse
2304 srcbits,-bmpImage->bytes_per_line,
2307 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
2308 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
2309 convs->Convert_888_to_565_reverse
2311 srcbits,-bmpImage->bytes_per_line,
2321 const char* srcbits;
2323 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2325 if (bmpImage->green_mask!=0x00ff00 ||
2326 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2328 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2329 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2331 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2332 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2333 convs->Convert_0888_to_555_asis
2335 srcbits,-bmpImage->bytes_per_line,
2338 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2339 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2340 convs->Convert_0888_to_565_asis
2342 srcbits,-bmpImage->bytes_per_line,
2347 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2348 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2349 convs->Convert_0888_to_555_reverse
2351 srcbits,-bmpImage->bytes_per_line,
2354 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2355 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2356 convs->Convert_0888_to_565_reverse
2358 srcbits,-bmpImage->bytes_per_line,
2367 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2369 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2370 int rShift,gShift,bShift;
2373 /* Shift everything 16 bits left so that all shifts are >0,
2374 * even for BGR DIBs. Then a single >> 16 will bring everything
2377 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2378 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2379 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2381 /* 6 bits for the green */
2387 for (h = lines - 1; h >= 0; h--) {
2388 dstpixel=(LPWORD)dstbits;
2389 for (x = 0; x < width; x++) {
2390 PALETTEENTRY srcval;
2392 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2393 dstval=((srcval.peRed << rShift) & rDst) |
2394 ((srcval.peGreen << gShift) & gDst) |
2395 ((srcval.peBlue << bShift) & bDst);
2396 *dstpixel++=dstval >> 16;
2398 dstbits += linebytes;
2406 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2408 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2409 int rShift,gShift,bShift;
2410 const BYTE* srcbits;
2411 const BYTE* srcpixel;
2414 /* Shift everything 16 bits left so that all shifts are >0,
2415 * even for BGR DIBs. Then a single >> 16 will bring everything
2418 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2419 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2420 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2422 /* 6 bits for the green */
2428 srcbits=(BYTE*)bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2429 for (h=0; h<lines; h++) {
2431 dstpixel=(LPWORD)dstbits;
2432 for (x = 0; x < width; x++) {
2433 PALETTEENTRY srcval;
2435 srcval=srccolors[*srcpixel++];
2436 dstval=((srcval.peRed << rShift) & rDst) |
2437 ((srcval.peGreen << gShift) & gDst) |
2438 ((srcval.peBlue << bShift) & bDst);
2439 *dstpixel++=dstval >> 16;
2441 srcbits -= bmpImage->bytes_per_line;
2442 dstbits += linebytes;
2452 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2453 int rShift,gShift,bShift;
2456 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%x,%x,%x)\n",
2457 bmpImage->depth, bmpImage->red_mask,
2458 bmpImage->green_mask, bmpImage->blue_mask,
2461 /* Shift everything 16 bits left so that all shifts are >0,
2462 * even for BGR DIBs. Then a single >> 16 will bring everything
2465 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2466 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2467 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2469 /* 6 bits for the green */
2475 for (h = lines - 1; h >= 0; h--) {
2476 dstpixel=(LPWORD)dstbits;
2477 for (x = 0; x < width; x++) {
2480 srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
2481 dstval=((GetRValue(srcval) << rShift) & rDst) |
2482 ((GetGValue(srcval) << gShift) & gDst) |
2483 ((GetBValue(srcval) << bShift) & bDst);
2484 *dstpixel++=dstval >> 16;
2486 dstbits += linebytes;
2494 /***********************************************************************
2495 * X11DRV_DIB_SetImageBits_24
2497 * SetDIBits for a 24-bit deep DIB.
2499 static void X11DRV_DIB_SetImageBits_24( int lines, const BYTE *srcbits,
2500 DWORD srcwidth, DWORD dstwidth, int left,
2501 X11DRV_PDEVICE *physDev,
2502 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2503 XImage *bmpImage, DWORD linebytes )
2506 int h, width = min(srcwidth, dstwidth);
2507 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2512 srcbits = srcbits + linebytes * (lines - 1);
2513 linebytes = -linebytes;
2516 switch (bmpImage->depth)
2519 if (bmpImage->bits_per_pixel==24) {
2522 srcbits=srcbits+left*3;
2523 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2525 if (bmpImage->green_mask!=0x00ff00 ||
2526 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2528 } else if (rSrc==bmpImage->red_mask) {
2529 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2530 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2531 convs->Convert_888_asis
2534 dstbits,-bmpImage->bytes_per_line);
2536 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2537 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2538 convs->Convert_888_reverse
2541 dstbits,-bmpImage->bytes_per_line);
2551 srcbits=srcbits+left*3;
2552 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2554 if (bmpImage->green_mask!=0x00ff00 ||
2555 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2557 } else if (rSrc==bmpImage->red_mask) {
2558 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2559 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2560 convs->Convert_888_to_0888_asis
2563 dstbits,-bmpImage->bytes_per_line);
2565 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2566 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2567 convs->Convert_888_to_0888_reverse
2570 dstbits,-bmpImage->bytes_per_line);
2580 srcbits=srcbits+left*3;
2581 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
2583 if (bmpImage->green_mask==0x03e0) {
2584 if ((rSrc==0xff0000 && bmpImage->red_mask==0x7f00) ||
2585 (bSrc==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2586 /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2587 /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2588 convs->Convert_888_to_555_asis
2591 dstbits,-bmpImage->bytes_per_line);
2592 } else if ((rSrc==0xff && bmpImage->red_mask==0x7f00) ||
2593 (bSrc==0xff && bmpImage->blue_mask==0x7f00)) {
2594 /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2595 /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2596 convs->Convert_888_to_555_reverse
2599 dstbits,-bmpImage->bytes_per_line);
2603 } else if (bmpImage->green_mask==0x07e0) {
2604 if ((rSrc==0xff0000 && bmpImage->red_mask==0xf800) ||
2605 (bSrc==0xff0000 && bmpImage->blue_mask==0xf800)) {
2606 /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2607 /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2608 convs->Convert_888_to_565_asis
2611 dstbits,-bmpImage->bytes_per_line);
2612 } else if ((rSrc==0xff && bmpImage->red_mask==0xf800) ||
2613 (bSrc==0xff && bmpImage->blue_mask==0xf800)) {
2614 /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2615 /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2616 convs->Convert_888_to_565_reverse
2619 dstbits,-bmpImage->bytes_per_line);
2631 WARN("from 24 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2632 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2633 bmpImage->green_mask, bmpImage->blue_mask );
2639 /* ==== rgb 888 dib -> any bmp bormat ==== */
2640 const BYTE* srcbyte;
2642 /* Windows only supports one 24bpp DIB format: RGB888 */
2644 for (h = lines - 1; h >= 0; h--) {
2645 srcbyte=(const BYTE*)srcbits;
2646 for (x = left; x < width+left; x++) {
2647 XPutPixel(bmpImage, x, h,
2648 X11DRV_PALETTE_ToPhysical
2649 (physDev, RGB(srcbyte[2], srcbyte[1], srcbyte[0])));
2652 srcbits += linebytes;
2660 /***********************************************************************
2661 * X11DRV_DIB_GetImageBits_24
2663 * GetDIBits for an 24-bit deep DIB.
2665 static void X11DRV_DIB_GetImageBits_24( int lines, BYTE *dstbits,
2666 DWORD dstwidth, DWORD srcwidth,
2667 PALETTEENTRY *srccolors,
2668 DWORD rDst, DWORD gDst, DWORD bDst,
2669 XImage *bmpImage, DWORD linebytes )
2672 int h, width = min(srcwidth, dstwidth);
2673 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2678 dstbits = dstbits + ( linebytes * (lines-1) );
2679 linebytes = -linebytes;
2682 switch (bmpImage->depth)
2685 if (bmpImage->bits_per_pixel==24) {
2686 const char* srcbits;
2688 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2690 if (bmpImage->green_mask!=0x00ff00 ||
2691 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2693 } else if (rDst==bmpImage->red_mask) {
2694 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2695 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2696 convs->Convert_888_asis
2698 srcbits,-bmpImage->bytes_per_line,
2701 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2702 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2703 convs->Convert_888_reverse
2705 srcbits,-bmpImage->bytes_per_line,
2714 const char* srcbits;
2716 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2718 if (bmpImage->green_mask!=0x00ff00 ||
2719 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2721 } else if (rDst==bmpImage->red_mask) {
2722 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2723 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2724 convs->Convert_0888_to_888_asis
2726 srcbits,-bmpImage->bytes_per_line,
2729 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2730 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2731 convs->Convert_0888_to_888_reverse
2733 srcbits,-bmpImage->bytes_per_line,
2742 const char* srcbits;
2744 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2746 if (bmpImage->green_mask==0x03e0) {
2747 if ((rDst==0xff0000 && bmpImage->red_mask==0x7f00) ||
2748 (bDst==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2749 /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2750 /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2751 convs->Convert_555_to_888_asis
2753 srcbits,-bmpImage->bytes_per_line,
2755 } else if ((rDst==0xff && bmpImage->red_mask==0x7f00) ||
2756 (bDst==0xff && bmpImage->blue_mask==0x7f00)) {
2757 /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2758 /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2759 convs->Convert_555_to_888_reverse
2761 srcbits,-bmpImage->bytes_per_line,
2766 } else if (bmpImage->green_mask==0x07e0) {
2767 if ((rDst==0xff0000 && bmpImage->red_mask==0xf800) ||
2768 (bDst==0xff0000 && bmpImage->blue_mask==0xf800)) {
2769 /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2770 /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2771 convs->Convert_565_to_888_asis
2773 srcbits,-bmpImage->bytes_per_line,
2775 } else if ((rDst==0xff && bmpImage->red_mask==0xf800) ||
2776 (bDst==0xff && bmpImage->blue_mask==0xf800)) {
2777 /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2778 /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2779 convs->Convert_565_to_888_reverse
2781 srcbits,-bmpImage->bytes_per_line,
2794 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2796 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2799 /* Windows only supports one 24bpp DIB format: rgb 888 */
2800 for (h = lines - 1; h >= 0; h--) {
2802 for (x = 0; x < width; x++) {
2803 PALETTEENTRY srcval;
2804 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2805 dstbyte[0]=srcval.peBlue;
2806 dstbyte[1]=srcval.peGreen;
2807 dstbyte[2]=srcval.peRed;
2810 dstbits += linebytes;
2818 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2820 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2821 const void* srcbits;
2822 const BYTE* srcpixel;
2825 /* Windows only supports one 24bpp DIB format: rgb 888 */
2826 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2827 for (h = lines - 1; h >= 0; h--) {
2830 for (x = 0; x < width; x++ ) {
2831 PALETTEENTRY srcval;
2832 srcval=srccolors[*srcpixel++];
2833 dstbyte[0]=srcval.peBlue;
2834 dstbyte[1]=srcval.peGreen;
2835 dstbyte[2]=srcval.peRed;
2838 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
2839 dstbits += linebytes;
2849 /* ==== any bmp format -> 888 dib ==== */
2852 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%x,%x,%x)\n",
2853 bmpImage->depth, bmpImage->red_mask,
2854 bmpImage->green_mask, bmpImage->blue_mask,
2857 /* Windows only supports one 24bpp DIB format: rgb 888 */
2858 for (h = lines - 1; h >= 0; h--) {
2860 for (x = 0; x < width; x++) {
2861 COLORREF srcval=X11DRV_PALETTE_ToLogical
2862 (XGetPixel( bmpImage, x, h ));
2863 dstbyte[0]=GetBValue(srcval);
2864 dstbyte[1]=GetGValue(srcval);
2865 dstbyte[2]=GetRValue(srcval);
2868 dstbits += linebytes;
2876 /***********************************************************************
2877 * X11DRV_DIB_SetImageBits_32
2879 * SetDIBits for a 32-bit deep DIB.
2881 static void X11DRV_DIB_SetImageBits_32(int lines, const BYTE *srcbits,
2882 DWORD srcwidth, DWORD dstwidth, int left,
2883 X11DRV_PDEVICE *physDev,
2884 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2890 int h, width = min(srcwidth, dstwidth);
2891 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2896 srcbits = srcbits + ( linebytes * (lines-1) );
2897 linebytes = -linebytes;
2900 ptr = (const DWORD *) srcbits + left;
2902 switch (bmpImage->depth)
2905 if (bmpImage->bits_per_pixel==24) {
2908 srcbits=srcbits+left*4;
2909 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2911 if (rSrc==bmpImage->red_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->blue_mask) {
2912 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2913 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2914 convs->Convert_0888_to_888_asis
2917 dstbits,-bmpImage->bytes_per_line);
2918 } else if (bmpImage->green_mask!=0x00ff00 ||
2919 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2921 /* the tests below assume sane bmpImage masks */
2922 } else if (rSrc==bmpImage->blue_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->red_mask) {
2923 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2924 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2925 convs->Convert_0888_to_888_reverse
2928 dstbits,-bmpImage->bytes_per_line);
2929 } else if (bmpImage->blue_mask==0xff) {
2930 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2931 convs->Convert_any0888_to_rgb888
2935 dstbits,-bmpImage->bytes_per_line);
2937 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2938 convs->Convert_any0888_to_bgr888
2942 dstbits,-bmpImage->bytes_per_line);
2952 srcbits=srcbits+left*4;
2953 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2955 if (gSrc==bmpImage->green_mask) {
2956 if (rSrc==bmpImage->red_mask && bSrc==bmpImage->blue_mask) {
2957 /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2958 /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2959 convs->Convert_0888_asis
2962 dstbits,-bmpImage->bytes_per_line);
2963 } else if (bmpImage->green_mask!=0x00ff00 ||
2964 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2966 /* the tests below assume sane bmpImage masks */
2967 } else if (rSrc==bmpImage->blue_mask && bSrc==bmpImage->red_mask) {
2968 /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2969 /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2970 convs->Convert_0888_reverse
2973 dstbits,-bmpImage->bytes_per_line);
2975 /* ==== any 0888 dib -> any 0888 bmp ==== */
2976 convs->Convert_0888_any
2980 dstbits,-bmpImage->bytes_per_line,
2981 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
2983 } else if (bmpImage->green_mask!=0x00ff00 ||
2984 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2986 /* the tests below assume sane bmpImage masks */
2988 /* ==== any 0888 dib -> any 0888 bmp ==== */
2989 convs->Convert_0888_any
2993 dstbits,-bmpImage->bytes_per_line,
2994 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3004 srcbits=srcbits+left*4;
3005 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
3007 if (rSrc==0xff0000 && gSrc==0x00ff00 && bSrc==0x0000ff) {
3008 if (bmpImage->green_mask==0x03e0) {
3009 if (bmpImage->red_mask==0x7f00) {
3010 /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
3011 convs->Convert_0888_to_555_asis
3014 dstbits,-bmpImage->bytes_per_line);
3015 } else if (bmpImage->blue_mask==0x7f00) {
3016 /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
3017 convs->Convert_0888_to_555_reverse
3020 dstbits,-bmpImage->bytes_per_line);
3024 } else if (bmpImage->green_mask==0x07e0) {
3025 if (bmpImage->red_mask==0xf800) {
3026 /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
3027 convs->Convert_0888_to_565_asis
3030 dstbits,-bmpImage->bytes_per_line);
3031 } else if (bmpImage->blue_mask==0xf800) {
3032 /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
3033 convs->Convert_0888_to_565_reverse
3036 dstbits,-bmpImage->bytes_per_line);
3043 } else if (rSrc==0x0000ff && gSrc==0x00ff00 && bSrc==0xff0000) {
3044 if (bmpImage->green_mask==0x03e0) {
3045 if (bmpImage->blue_mask==0x7f00) {
3046 /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
3047 convs->Convert_0888_to_555_asis
3050 dstbits,-bmpImage->bytes_per_line);
3051 } else if (bmpImage->red_mask==0x7f00) {
3052 /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
3053 convs->Convert_0888_to_555_reverse
3056 dstbits,-bmpImage->bytes_per_line);
3060 } else if (bmpImage->green_mask==0x07e0) {
3061 if (bmpImage->blue_mask==0xf800) {
3062 /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
3063 convs->Convert_0888_to_565_asis
3066 dstbits,-bmpImage->bytes_per_line);
3067 } else if (bmpImage->red_mask==0xf800) {
3068 /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
3069 convs->Convert_0888_to_565_reverse
3072 dstbits,-bmpImage->bytes_per_line);
3080 if (bmpImage->green_mask==0x03e0 &&
3081 (bmpImage->red_mask==0x7f00 ||
3082 bmpImage->blue_mask==0x7f00)) {
3083 /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
3084 convs->Convert_any0888_to_5x5
3088 dstbits,-bmpImage->bytes_per_line,
3089 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3090 } else if (bmpImage->green_mask==0x07e0 &&
3091 (bmpImage->red_mask==0xf800 ||
3092 bmpImage->blue_mask==0xf800)) {
3093 /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
3094 convs->Convert_any0888_to_5x5
3098 dstbits,-bmpImage->bytes_per_line,
3099 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3109 WARN("from 32 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
3110 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
3111 bmpImage->green_mask, bmpImage->blue_mask );
3117 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
3118 const DWORD* srcpixel;
3119 int rShift,gShift,bShift;
3121 rShift=X11DRV_DIB_MaskToShift(rSrc);
3122 gShift=X11DRV_DIB_MaskToShift(gSrc);
3123 bShift=X11DRV_DIB_MaskToShift(bSrc);
3125 for (h = lines - 1; h >= 0; h--) {
3126 srcpixel=(const DWORD*)srcbits;
3127 for (x = left; x < width+left; x++) {
3129 BYTE red,green,blue;
3130 srcvalue=*srcpixel++;
3131 red= (srcvalue >> rShift) & 0xff;
3132 green=(srcvalue >> gShift) & 0xff;
3133 blue= (srcvalue >> bShift) & 0xff;
3134 XPutPixel(bmpImage, x, h, X11DRV_PALETTE_ToPhysical
3135 (physDev, RGB(red,green,blue)));
3137 srcbits += linebytes;
3145 /***********************************************************************
3146 * X11DRV_DIB_GetImageBits_32
3148 * GetDIBits for an 32-bit deep DIB.
3150 static void X11DRV_DIB_GetImageBits_32( int lines, BYTE *dstbits,
3151 DWORD dstwidth, DWORD srcwidth,
3152 PALETTEENTRY *srccolors,
3153 DWORD rDst, DWORD gDst, DWORD bDst,
3154 XImage *bmpImage, DWORD linebytes )
3157 int h, width = min(srcwidth, dstwidth);
3159 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
3164 dstbits = dstbits + ( linebytes * (lines-1) );
3165 linebytes = -linebytes;
3170 switch (bmpImage->depth)
3173 if (bmpImage->bits_per_pixel==24) {
3174 const void* srcbits;
3176 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3178 if (rDst==bmpImage->red_mask && gDst==bmpImage->green_mask && bDst==bmpImage->blue_mask) {
3179 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
3180 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
3181 convs->Convert_888_to_0888_asis
3183 srcbits,-bmpImage->bytes_per_line,
3185 } else if (bmpImage->green_mask!=0x00ff00 ||
3186 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3188 /* the tests below assume sane bmpImage masks */
3189 } else if (rDst==bmpImage->blue_mask && gDst==bmpImage->green_mask && bDst==bmpImage->red_mask) {
3190 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
3191 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
3192 convs->Convert_888_to_0888_reverse
3194 srcbits,-bmpImage->bytes_per_line,
3196 } else if (bmpImage->blue_mask==0xff) {
3197 /* ==== rgb 888 bmp -> any 0888 dib ==== */
3198 convs->Convert_rgb888_to_any0888
3200 srcbits,-bmpImage->bytes_per_line,
3204 /* ==== bgr 888 bmp -> any 0888 dib ==== */
3205 convs->Convert_bgr888_to_any0888
3207 srcbits,-bmpImage->bytes_per_line,
3217 const char* srcbits;
3219 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3221 if (gDst==bmpImage->green_mask) {
3222 if (rDst==bmpImage->red_mask && bDst==bmpImage->blue_mask) {
3223 /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
3224 /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
3225 convs->Convert_0888_asis
3227 srcbits,-bmpImage->bytes_per_line,
3229 } else if (bmpImage->green_mask!=0x00ff00 ||
3230 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3232 /* the tests below assume sane bmpImage masks */
3233 } else if (rDst==bmpImage->blue_mask && bDst==bmpImage->red_mask) {
3234 /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
3235 /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
3236 convs->Convert_0888_reverse
3238 srcbits,-bmpImage->bytes_per_line,
3241 /* ==== any 0888 bmp -> any 0888 dib ==== */
3242 convs->Convert_0888_any
3244 srcbits,-bmpImage->bytes_per_line,
3245 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3249 } else if (bmpImage->green_mask!=0x00ff00 ||
3250 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3252 /* the tests below assume sane bmpImage masks */
3254 /* ==== any 0888 bmp -> any 0888 dib ==== */
3255 convs->Convert_0888_any
3257 srcbits,-bmpImage->bytes_per_line,
3258 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3268 const char* srcbits;
3270 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3272 if (rDst==0xff0000 && gDst==0x00ff00 && bDst==0x0000ff) {
3273 if (bmpImage->green_mask==0x03e0) {
3274 if (bmpImage->red_mask==0x7f00) {
3275 /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
3276 convs->Convert_555_to_0888_asis
3278 srcbits,-bmpImage->bytes_per_line,
3280 } else if (bmpImage->blue_mask==0x7f00) {
3281 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
3282 convs->Convert_555_to_0888_reverse
3284 srcbits,-bmpImage->bytes_per_line,
3289 } else if (bmpImage->green_mask==0x07e0) {
3290 if (bmpImage->red_mask==0xf800) {
3291 /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
3292 convs->Convert_565_to_0888_asis
3294 srcbits,-bmpImage->bytes_per_line,
3296 } else if (bmpImage->blue_mask==0xf800) {
3297 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
3298 convs->Convert_565_to_0888_reverse
3300 srcbits,-bmpImage->bytes_per_line,
3308 } else if (rDst==0x0000ff && gDst==0x00ff00 && bDst==0xff0000) {
3309 if (bmpImage->green_mask==0x03e0) {
3310 if (bmpImage->blue_mask==0x7f00) {
3311 /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
3312 convs->Convert_555_to_0888_asis
3314 srcbits,-bmpImage->bytes_per_line,
3316 } else if (bmpImage->red_mask==0x7f00) {
3317 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
3318 convs->Convert_555_to_0888_reverse
3320 srcbits,-bmpImage->bytes_per_line,
3325 } else if (bmpImage->green_mask==0x07e0) {
3326 if (bmpImage->blue_mask==0xf800) {
3327 /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3328 convs->Convert_565_to_0888_asis
3330 srcbits,-bmpImage->bytes_per_line,
3332 } else if (bmpImage->red_mask==0xf800) {
3333 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3334 convs->Convert_565_to_0888_reverse
3336 srcbits,-bmpImage->bytes_per_line,
3345 if (bmpImage->green_mask==0x03e0 &&
3346 (bmpImage->red_mask==0x7f00 ||
3347 bmpImage->blue_mask==0x7f00)) {
3348 /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3349 convs->Convert_5x5_to_any0888
3351 srcbits,-bmpImage->bytes_per_line,
3352 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3355 } else if (bmpImage->green_mask==0x07e0 &&
3356 (bmpImage->red_mask==0xf800 ||
3357 bmpImage->blue_mask==0xf800)) {
3358 /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3359 convs->Convert_5x5_to_any0888
3361 srcbits,-bmpImage->bytes_per_line,
3362 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3374 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3376 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3377 int rShift,gShift,bShift;
3380 rShift=X11DRV_DIB_MaskToShift(rDst);
3381 gShift=X11DRV_DIB_MaskToShift(gDst);
3382 bShift=X11DRV_DIB_MaskToShift(bDst);
3383 for (h = lines - 1; h >= 0; h--) {
3384 dstpixel=(DWORD*)dstbits;
3385 for (x = 0; x < width; x++) {
3386 PALETTEENTRY srcval;
3387 srcval = srccolors[XGetPixel(bmpImage, x, h)];
3388 *dstpixel++=(srcval.peRed << rShift) |
3389 (srcval.peGreen << gShift) |
3390 (srcval.peBlue << bShift);
3392 dstbits += linebytes;
3400 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3402 /* ==== pal 8 bmp -> any 0888 dib ==== */
3403 int rShift,gShift,bShift;
3404 const void* srcbits;
3405 const BYTE* srcpixel;
3408 rShift=X11DRV_DIB_MaskToShift(rDst);
3409 gShift=X11DRV_DIB_MaskToShift(gDst);
3410 bShift=X11DRV_DIB_MaskToShift(bDst);
3411 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3412 for (h = lines - 1; h >= 0; h--) {
3414 dstpixel=(DWORD*)dstbits;
3415 for (x = 0; x < width; x++) {
3416 PALETTEENTRY srcval;
3417 srcval=srccolors[*srcpixel++];
3418 *dstpixel++=(srcval.peRed << rShift) |
3419 (srcval.peGreen << gShift) |
3420 (srcval.peBlue << bShift);
3422 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
3423 dstbits += linebytes;
3433 /* ==== any bmp format -> any 0888 dib ==== */
3434 int rShift,gShift,bShift;
3437 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%x,%x,%x)\n",
3438 bmpImage->depth, bmpImage->red_mask,
3439 bmpImage->green_mask, bmpImage->blue_mask,
3442 rShift=X11DRV_DIB_MaskToShift(rDst);
3443 gShift=X11DRV_DIB_MaskToShift(gDst);
3444 bShift=X11DRV_DIB_MaskToShift(bDst);
3445 for (h = lines - 1; h >= 0; h--) {
3446 dstpixel=(DWORD*)dstbits;
3447 for (x = 0; x < width; x++) {
3449 srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
3450 *dstpixel++=(GetRValue(srcval) << rShift) |
3451 (GetGValue(srcval) << gShift) |
3452 (GetBValue(srcval) << bShift);
3454 dstbits += linebytes;
3461 static int XGetSubImageErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
3463 return (event->request_code == X_GetImage && event->error_code == BadMatch);
3466 /***********************************************************************
3467 * X11DRV_DIB_SetImageBits_GetSubImage
3469 * Helper for X11DRV_DIB_SetImageBits
3471 static void X11DRV_DIB_SetImageBits_GetSubImage(
3472 const X11DRV_DIB_IMAGEBITS_DESCR *descr, XImage *bmpImage)
3474 /* compressed bitmaps may contain gaps in them. So make a copy
3475 * of the existing pixels first */
3478 SetRect( &bmprc, descr->xDest, descr->yDest,
3479 descr->xDest + descr->width , descr->yDest + descr->height );
3480 GetRgnBox( descr->physDev->region, &rc );
3481 /* convert from dc to drawable origin */
3482 OffsetRect( &rc, descr->physDev->dc_rect.left, descr->physDev->dc_rect.top);
3483 /* clip visible rect with bitmap */
3484 if( IntersectRect( &rc, &rc, &bmprc))
3486 X11DRV_expect_error( gdi_display, XGetSubImageErrorHandler, NULL );
3487 XGetSubImage( gdi_display, descr->drawable, rc.left, rc.top,
3488 rc.right - rc.left, rc.bottom - rc.top, AllPlanes,
3490 descr->xSrc + rc.left - bmprc.left,
3491 descr->ySrc + rc.top - bmprc.top);
3492 X11DRV_check_error();
3496 /***********************************************************************
3497 * X11DRV_DIB_SetImageBits
3499 * Transfer the bits to an X image.
3500 * Helper function for SetDIBits() and SetDIBitsToDevice().
3502 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3504 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3509 bmpImage = descr->image;
3511 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3512 descr->infoWidth, lines, 32, 0 );
3513 bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
3514 if(bmpImage->data == NULL) {
3515 ERR("Out of memory!\n");
3516 XDestroyImage( bmpImage );
3517 wine_tsx11_unlock();
3521 wine_tsx11_unlock();
3523 TRACE("Dib: depth=%d r=%x g=%x b=%x\n",
3524 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3525 TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3526 bmpImage->depth,bmpImage->bits_per_pixel,
3527 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3529 /* Transfer the pixels */
3530 switch(descr->infoBpp)
3533 X11DRV_DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
3534 descr->width, descr->xSrc, (int *)(descr->colorMap),
3535 bmpImage, descr->dibpitch );
3538 if (descr->compression) {
3539 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3540 X11DRV_DIB_SetImageBits_RLE4( descr->lines, descr->bits,
3541 descr->infoWidth, descr->width,
3542 descr->xSrc, (int *)(descr->colorMap),
3545 X11DRV_DIB_SetImageBits_4( descr->lines, descr->bits,
3546 descr->infoWidth, descr->width,
3547 descr->xSrc, (int*)(descr->colorMap),
3548 bmpImage, descr->dibpitch );
3551 if (descr->compression) {
3552 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3553 X11DRV_DIB_SetImageBits_RLE8( descr->lines, descr->bits,
3554 descr->infoWidth, descr->width,
3555 descr->xSrc, (int *)(descr->colorMap),
3558 X11DRV_DIB_SetImageBits_8( descr->lines, descr->bits,
3559 descr->infoWidth, descr->width,
3560 descr->xSrc, (int *)(descr->colorMap),
3561 bmpImage, descr->dibpitch );
3565 X11DRV_DIB_SetImageBits_16( descr->lines, descr->bits,
3566 descr->infoWidth, descr->width,
3567 descr->xSrc, descr->physDev,
3568 descr->rMask, descr->gMask, descr->bMask,
3569 bmpImage, descr->dibpitch);
3572 X11DRV_DIB_SetImageBits_24( descr->lines, descr->bits,
3573 descr->infoWidth, descr->width,
3574 descr->xSrc, descr->physDev,
3575 descr->rMask, descr->gMask, descr->bMask,
3576 bmpImage, descr->dibpitch);
3579 X11DRV_DIB_SetImageBits_32( descr->lines, descr->bits,
3580 descr->infoWidth, descr->width,
3581 descr->xSrc, descr->physDev,
3582 descr->rMask, descr->gMask, descr->bMask,
3583 bmpImage, descr->dibpitch);
3586 WARN("(%d): Invalid depth\n", descr->infoBpp );
3590 TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3591 descr->drawable, descr->gc, bmpImage,
3592 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3593 descr->width, descr->height);
3596 #ifdef HAVE_LIBXXSHM
3597 if (descr->image && descr->useShm)
3599 XShmPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3600 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3601 descr->width, descr->height, FALSE );
3602 XSync( gdi_display, 0 );
3606 XPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3607 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3608 descr->width, descr->height );
3610 if (!descr->image) XDestroyImage( bmpImage );
3611 wine_tsx11_unlock();
3615 /***********************************************************************
3616 * X11DRV_DIB_GetImageBits
3618 * Transfer the bits from an X image.
3620 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3622 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3627 bmpImage = descr->image;
3629 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3630 descr->infoWidth, lines, 32, 0 );
3631 bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
3632 if(bmpImage->data == NULL) {
3633 ERR("Out of memory!\n");
3634 XDestroyImage( bmpImage );
3635 wine_tsx11_unlock();
3640 #ifdef HAVE_LIBXXSHM
3642 /* We must not call XShmGetImage() with a bitmap which is bigger than the available area.
3643 If we do, XShmGetImage() will fail (X exception), as it checks for this internally. */
3644 if((descr->image && descr->useShm) && (bmpImage->width <= (descr->width - descr->xSrc))
3645 && (bmpImage->height <= (descr->height - descr->ySrc)))
3647 int saveRed, saveGreen, saveBlue;
3649 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3650 gdi_display, descr->drawable, bmpImage,
3651 descr->xSrc, descr->ySrc, AllPlanes);
3653 /* We must save and restore the bmpImage's masks in order
3654 * to preserve them across the call to XShmGetImage, which
3655 * decides to eleminate them since it doesn't happen to know
3656 * what the format of the image is supposed to be, even though
3658 saveRed = bmpImage->red_mask;
3659 saveBlue= bmpImage->blue_mask;
3660 saveGreen = bmpImage->green_mask;
3662 XShmGetImage( gdi_display, descr->drawable, bmpImage,
3663 descr->xSrc, descr->ySrc, AllPlanes);
3665 bmpImage->red_mask = saveRed;
3666 bmpImage->blue_mask = saveBlue;
3667 bmpImage->green_mask = saveGreen;
3670 #endif /* HAVE_LIBXXSHM */
3672 TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3673 gdi_display, descr->drawable, descr->xSrc, descr->ySrc, descr->width,
3674 lines, AllPlanes, ZPixmap, bmpImage, descr->xDest, descr->yDest);
3675 XGetSubImage( gdi_display, descr->drawable, descr->xSrc, descr->ySrc,
3676 descr->width, lines, AllPlanes, ZPixmap,
3677 bmpImage, descr->xDest, descr->yDest );
3679 wine_tsx11_unlock();
3681 TRACE("Dib: depth=%2d r=%x g=%x b=%x\n",
3682 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3683 TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3684 bmpImage->depth,bmpImage->bits_per_pixel,
3685 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3686 /* Transfer the pixels */
3687 switch(descr->infoBpp)
3690 X11DRV_DIB_GetImageBits_1( descr->lines,(LPVOID)descr->bits,
3691 descr->infoWidth, descr->width,
3692 descr->colorMap, descr->palentry,
3693 bmpImage, descr->dibpitch );
3697 if (descr->compression) {
3698 FIXME("Compression not yet supported!\n");
3699 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 4 ) * abs(descr->lines))
3702 X11DRV_DIB_GetImageBits_4( descr->lines,(LPVOID)descr->bits,
3703 descr->infoWidth, descr->width,
3704 descr->colorMap, descr->palentry,
3705 bmpImage, descr->dibpitch );
3708 if (descr->compression) {
3709 FIXME("Compression not yet supported!\n");
3710 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 8 ) * abs(descr->lines))
3713 X11DRV_DIB_GetImageBits_8( descr->lines, (LPVOID)descr->bits,
3714 descr->infoWidth, descr->width,
3715 descr->colorMap, descr->palentry,
3716 bmpImage, descr->dibpitch );
3720 X11DRV_DIB_GetImageBits_16( descr->lines, (LPVOID)descr->bits,
3721 descr->infoWidth,descr->width,
3723 descr->rMask, descr->gMask, descr->bMask,
3724 bmpImage, descr->dibpitch );
3728 X11DRV_DIB_GetImageBits_24( descr->lines, (LPVOID)descr->bits,
3729 descr->infoWidth,descr->width,
3731 descr->rMask, descr->gMask, descr->bMask,
3732 bmpImage, descr->dibpitch);
3736 X11DRV_DIB_GetImageBits_32( descr->lines, (LPVOID)descr->bits,
3737 descr->infoWidth, descr->width,
3739 descr->rMask, descr->gMask, descr->bMask,
3740 bmpImage, descr->dibpitch);
3744 WARN("(%d): Invalid depth\n", descr->infoBpp );
3751 XDestroyImage( bmpImage );
3752 wine_tsx11_unlock();
3757 /*************************************************************************
3758 * X11DRV_SetDIBitsToDevice
3761 INT X11DRV_SetDIBitsToDevice( X11DRV_PDEVICE *physDev, INT xDest, INT yDest, DWORD cx,
3762 DWORD cy, INT xSrc, INT ySrc,
3763 UINT startscan, UINT lines, LPCVOID bits,
3764 const BITMAPINFO *info, UINT coloruse )
3766 X11DRV_DIB_IMAGEBITS_DESCR descr;
3771 int rop = X11DRV_XROPfunction[GetROP2(physDev->hdc) - 1];
3773 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3774 &descr.infoBpp, &descr.compression ) == -1)
3777 top_down = (height < 0);
3778 if (top_down) height = -height;
3782 LPtoDP(physDev->hdc, &pt, 1);
3784 if (!lines || (startscan >= height)) return 0;
3785 if (!top_down && startscan + lines > height) lines = height - startscan;
3787 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3788 * and clamp all values to fit inside [startscan,startscan+lines]
3790 if (ySrc + cy <= startscan + lines)
3792 UINT y = startscan + lines - (ySrc + cy);
3793 if (ySrc < startscan) cy -= (startscan - ySrc);
3796 /* avoid getting unnecessary lines */
3798 if (y >= lines) return 0;
3803 if (y >= lines) return lines;
3804 ySrc = y; /* need to get all lines in top down mode */
3809 if (ySrc >= startscan + lines) return lines;
3810 pt.y += ySrc + cy - (startscan + lines);
3811 cy = startscan + lines - ySrc;
3813 if (cy > lines) cy = lines;
3815 if (xSrc >= width) return lines;
3816 if (xSrc + cx >= width) cx = width - xSrc;
3817 if (!cx || !cy) return lines;
3819 /* Update the pixmap from the DIB section */
3820 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
3822 X11DRV_SetupGCForText( physDev ); /* To have the correct colors */
3824 XSetFunction(gdi_display, physDev->gc, rop);
3825 wine_tsx11_unlock();
3827 switch (descr.infoBpp)
3832 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3834 physDev->depth, info, &descr.nColorMap );
3835 if (!descr.colorMap) return 0;
3836 descr.rMask = descr.gMask = descr.bMask = 0;
3840 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3841 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3842 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3848 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3849 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3850 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3855 descr.physDev = physDev;
3858 descr.palentry = NULL;
3859 descr.lines = top_down ? -lines : lines;
3860 descr.infoWidth = width;
3861 descr.depth = physDev->depth;
3862 descr.drawable = physDev->drawable;
3863 descr.gc = physDev->gc;
3866 descr.xDest = physDev->dc_rect.left + pt.x;
3867 descr.yDest = physDev->dc_rect.top + pt.y;
3870 descr.useShm = FALSE;
3871 descr.dibpitch = ((width * descr.infoBpp + 31) &~31) / 8;
3873 result = X11DRV_DIB_SetImageBits( &descr );
3875 if (descr.infoBpp <= 8)
3876 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3878 /* Update the DIBSection of the pixmap */
3879 X11DRV_UnlockDIBSection(physDev, TRUE);
3884 /***********************************************************************
3885 * SetDIBits (X11DRV.@)
3887 INT X11DRV_SetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan,
3888 UINT lines, LPCVOID bits, const BITMAPINFO *info, UINT coloruse )
3890 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3891 X11DRV_DIB_IMAGEBITS_DESCR descr;
3893 LONG width, height, tmpheight;
3896 descr.physDev = physDev;
3898 if (!physBitmap) return 0;
3900 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3901 &descr.infoBpp, &descr.compression ) == -1)
3905 if (height < 0) height = -height;
3906 if (!lines || (startscan >= height))
3909 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
3911 if (startscan + lines > height) lines = height - startscan;
3913 switch (descr.infoBpp)
3918 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3919 descr.physDev, coloruse,
3920 physBitmap->pixmap_depth,
3921 info, &descr.nColorMap );
3922 if (!descr.colorMap) return 0;
3923 descr.rMask = descr.gMask = descr.bMask = 0;
3927 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3928 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3929 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3935 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3936 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3937 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3946 descr.palentry = NULL;
3947 descr.infoWidth = width;
3948 descr.lines = tmpheight >= 0 ? lines : -lines;
3949 descr.depth = physBitmap->pixmap_depth;
3950 descr.drawable = physBitmap->pixmap;
3951 descr.gc = BITMAP_GC(physBitmap);
3955 descr.yDest = height - startscan - lines;
3956 descr.width = bitmap.bmWidth;
3957 descr.height = lines;
3958 descr.useShm = FALSE;
3959 descr.dibpitch = ((descr.infoWidth * descr.infoBpp + 31) &~31) / 8;
3960 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
3961 result = X11DRV_DIB_SetImageBits( &descr );
3962 X11DRV_DIB_Unlock( physBitmap, TRUE );
3964 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3969 /***********************************************************************
3970 * GetDIBits (X11DRV.@)
3972 INT X11DRV_GetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan, UINT lines,
3973 LPVOID bits, BITMAPINFO *info, UINT coloruse )
3975 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3977 X11DRV_DIB_IMAGEBITS_DESCR descr;
3978 PALETTEENTRY palette[256];
3981 LONG width, tempHeight;
3986 GetPaletteEntries( GetCurrentObject( physDev->hdc, OBJ_PAL ), 0, 256, palette );
3988 if (!physBitmap) return 0;
3989 if (!(obj_size = GetObjectW( hbitmap, sizeof(dib), &dib ))) return 0;
3991 bitmap_type = DIB_GetBitmapInfo( (BITMAPINFOHEADER*)info, &width, &tempHeight, &descr.infoBpp, &descr.compression);
3992 descr.lines = tempHeight;
3993 if (bitmap_type == -1)
3995 ERR("Invalid bitmap\n");
3998 core_header = (bitmap_type == 0);
3999 colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
4001 TRACE("%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
4002 lines, dib.dsBm.bmWidth, dib.dsBm.bmHeight, width, descr.lines, startscan);
4004 if( lines > dib.dsBm.bmHeight ) lines = dib.dsBm.bmHeight;
4006 height = descr.lines;
4007 if (height < 0) height = -height;
4008 if( lines > height ) lines = height;
4009 /* Top-down images have a negative biHeight, the scanlines of these images
4010 * were inverted in X11DRV_DIB_GetImageBits_xx
4011 * To prevent this we simply change the sign of lines
4012 * (the number of scan lines to copy).
4013 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4015 if( descr.lines < 0 && lines > 0) lines = -lines;
4017 if( startscan >= dib.dsBm.bmHeight ) return 0;
4019 descr.colorMap = NULL;
4021 switch (descr.infoBpp)
4026 descr.rMask= descr.gMask = descr.bMask = 0;
4027 if(coloruse == DIB_RGB_COLORS)
4028 descr.colorMap = colorPtr;
4030 int num_colors = 1 << descr.infoBpp, i;
4033 WORD *index = (WORD*)colorPtr;
4034 descr.colorMap = rgb = HeapAlloc(GetProcessHeap(), 0, num_colors * sizeof(RGBQUAD));
4035 for(i = 0; i < num_colors; i++, rgb++, index++) {
4036 colref = X11DRV_PALETTE_ToLogical(X11DRV_PALETTE_ToPhysical(physDev, PALETTEINDEX(*index)));
4037 rgb->rgbRed = GetRValue(colref);
4038 rgb->rgbGreen = GetGValue(colref);
4039 rgb->rgbBlue = GetBValue(colref);
4040 rgb->rgbReserved = 0;
4046 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4047 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4048 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4052 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4053 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4054 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4058 descr.physDev = physDev;
4059 descr.palentry = palette;
4061 descr.image = physBitmap->image;
4062 descr.infoWidth = width;
4063 descr.lines = lines;
4064 descr.depth = physBitmap->pixmap_depth;
4065 descr.drawable = physBitmap->pixmap;
4066 descr.gc = BITMAP_GC(physBitmap);
4067 descr.width = dib.dsBm.bmWidth;
4068 descr.height = dib.dsBm.bmHeight;
4072 descr.sizeImage = core_header ? 0 : info->bmiHeader.biSizeImage;
4074 if (descr.lines > 0)
4076 descr.ySrc = (descr.height-1) - (startscan + (lines-1));
4080 descr.ySrc = startscan;
4082 #ifdef HAVE_LIBXXSHM
4083 descr.useShm = (obj_size == sizeof(DIBSECTION)) && (physBitmap->shminfo.shmid != -1);
4085 descr.useShm = FALSE;
4087 descr.dibpitch = (obj_size == sizeof(DIBSECTION)) ? dib.dsBm.bmWidthBytes
4088 : (((descr.infoWidth * descr.infoBpp + 31) &~31) / 8);
4090 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
4091 X11DRV_DIB_GetImageBits( &descr );
4092 X11DRV_DIB_Unlock( physBitmap, TRUE );
4094 if(!core_header && info->bmiHeader.biSizeImage == 0) /* Fill in biSizeImage */
4095 info->bmiHeader.biSizeImage = X11DRV_DIB_GetDIBImageBytes( descr.infoWidth,
4099 if (descr.compression == BI_BITFIELDS)
4101 *(DWORD *)info->bmiColors = descr.rMask;
4102 *((DWORD *)info->bmiColors + 1) = descr.gMask;
4103 *((DWORD *)info->bmiColors + 2) = descr.bMask;
4105 else if (!core_header)
4107 /* if RLE or JPEG compression were supported,
4108 * this line would be invalid. */
4109 info->bmiHeader.biCompression = 0;
4112 if(descr.colorMap != colorPtr)
4113 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4117 /***********************************************************************
4118 * DIB_DoProtectDIBSection
4120 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP *physBitmap, DWORD new_prot )
4124 VirtualProtect(physBitmap->base, physBitmap->size, new_prot, &old_prot);
4125 TRACE("Changed protection from %d to %d\n", old_prot, new_prot);
4128 /***********************************************************************
4129 * X11DRV_DIB_DoCopyDIBSection
4131 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB,
4132 void *colorMap, int nColorMap,
4133 Drawable dest, GC gc,
4134 DWORD xSrc, DWORD ySrc,
4135 DWORD xDest, DWORD yDest,
4136 DWORD width, DWORD height)
4138 DIBSECTION dibSection;
4139 X11DRV_DIB_IMAGEBITS_DESCR descr;
4140 int identity[2] = {0,1};
4142 if (!GetObjectW( physBitmap->hbitmap, sizeof(dibSection), &dibSection )) return;
4144 descr.physDev = NULL;
4145 descr.palentry = NULL;
4146 descr.infoWidth = dibSection.dsBmih.biWidth;
4147 descr.infoBpp = dibSection.dsBmih.biBitCount;
4148 descr.lines = dibSection.dsBmih.biHeight;
4149 descr.image = physBitmap->image;
4150 descr.colorMap = colorMap;
4151 descr.nColorMap = nColorMap;
4152 descr.bits = dibSection.dsBm.bmBits;
4153 descr.depth = physBitmap->pixmap_depth;
4154 descr.compression = dibSection.dsBmih.biCompression;
4156 if(descr.infoBpp == 1)
4157 descr.colorMap = (void*)identity;
4159 switch (descr.infoBpp)
4164 descr.rMask = descr.gMask = descr.bMask = 0;
4168 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0x7c00;
4169 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x03e0;
4170 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x001f;
4175 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0xff0000;
4176 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x00ff00;
4177 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x0000ff;
4182 descr.drawable = dest;
4186 descr.xDest = xDest;
4187 descr.yDest = yDest;
4188 descr.width = width;
4189 descr.height = height;
4190 descr.sizeImage = 0;
4192 #ifdef HAVE_LIBXXSHM
4193 descr.useShm = (physBitmap->shminfo.shmid != -1);
4195 descr.useShm = FALSE;
4197 descr.dibpitch = dibSection.dsBm.bmWidthBytes;
4201 TRACE("Copying from Pixmap to DIB bits\n");
4202 X11DRV_DIB_GetImageBits( &descr );
4206 TRACE("Copying from DIB bits to Pixmap\n");
4207 X11DRV_DIB_SetImageBits( &descr );
4211 /***********************************************************************
4212 * X11DRV_DIB_CopyDIBSection
4214 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
4215 DWORD xSrc, DWORD ySrc, DWORD xDest, DWORD yDest,
4216 DWORD width, DWORD height)
4219 X_PHYSBITMAP *physBitmap;
4220 unsigned int nColorMap;
4224 TRACE("(%p,%p,%d,%d,%d,%d,%d,%d)\n", physDevSrc->hdc, physDevDst->hdc,
4225 xSrc, ySrc, xDest, yDest, width, height);
4226 /* this function is meant as an optimization for BitBlt,
4227 * not to be called otherwise */
4228 physBitmap = physDevSrc->bitmap;
4229 if (!physBitmap || GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib ) != sizeof(dib))
4231 ERR("called for non-DIBSection!?\n");
4234 /* while BitBlt should already have made sure we only get
4235 * positive values, we should check for oversize values */
4236 if ((xSrc < dib.dsBm.bmWidth) &&
4237 (ySrc < dib.dsBm.bmHeight)) {
4238 if (xSrc + width > dib.dsBm.bmWidth)
4239 width = dib.dsBm.bmWidth - xSrc;
4240 if (ySrc + height > dib.dsBm.bmHeight)
4241 height = dib.dsBm.bmHeight - ySrc;
4242 /* if the source bitmap is 8bpp or less, we're supposed to use the
4243 * DC's palette for color conversion (not the DIB color table) */
4244 if (dib.dsBm.bmBitsPixel <= 8) {
4245 HPALETTE hPalette = GetCurrentObject( physDevSrc->hdc, OBJ_PAL );
4246 if (!hPalette || (hPalette == GetStockObject(DEFAULT_PALETTE))) {
4247 /* HACK: no palette has been set in the source DC,
4248 * use the DIB colormap instead - this is necessary in some
4249 * cases since we need to do depth conversion in some places
4250 * where real Windows can just copy data straight over */
4251 x11ColorMap = physBitmap->colorMap;
4252 nColorMap = physBitmap->nColorMap;
4253 freeColorMap = FALSE;
4255 const BITMAPINFO* info = (BITMAPINFO*)&dib.dsBmih;
4258 nColorMap = X11DRV_DIB_GetColorCount(info);
4259 x11ColorMap = HeapAlloc(GetProcessHeap(), 0, nColorMap * sizeof(int));
4260 for (i = 0; i < nColorMap; i++)
4261 x11ColorMap[i] = X11DRV_PALETTE_ToPhysical(physDevSrc, PALETTEINDEX(i));
4262 freeColorMap = TRUE;
4269 freeColorMap = FALSE;
4271 /* perform the copy */
4272 X11DRV_DIB_DoCopyDIBSection(physBitmap, FALSE, x11ColorMap, nColorMap,
4273 physDevDst->drawable, physDevDst->gc, xSrc, ySrc,
4274 physDevDst->dc_rect.left + xDest, physDevDst->dc_rect.top + yDest,
4276 /* free color mapping */
4278 HeapFree(GetProcessHeap(), 0, x11ColorMap);
4282 /***********************************************************************
4283 * X11DRV_DIB_DoUpdateDIBSection
4285 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB)
4289 GetObjectW( physBitmap->hbitmap, sizeof(bitmap), &bitmap );
4290 X11DRV_DIB_DoCopyDIBSection(physBitmap, toDIB,
4291 physBitmap->colorMap, physBitmap->nColorMap,
4292 physBitmap->pixmap, BITMAP_GC(physBitmap),
4293 0, 0, 0, 0, bitmap.bmWidth, bitmap.bmHeight);
4296 /***********************************************************************
4297 * X11DRV_DIB_FaultHandler
4299 static LONG CALLBACK X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep )
4301 X_PHYSBITMAP *physBitmap = NULL;
4306 if (ep->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
4307 return EXCEPTION_CONTINUE_SEARCH;
4309 addr = (BYTE *)ep->ExceptionRecord->ExceptionInformation[1];
4311 EnterCriticalSection(&dibs_cs);
4312 LIST_FOR_EACH( ptr, &dibs_list )
4314 physBitmap = LIST_ENTRY( ptr, X_PHYSBITMAP, entry );
4315 if ((physBitmap->base <= addr) && (addr < physBitmap->base + physBitmap->size))
4321 LeaveCriticalSection(&dibs_cs);
4323 if (!found) return EXCEPTION_CONTINUE_SEARCH;
4325 X11DRV_DIB_Lock( physBitmap, DIB_Status_None );
4326 if (ep->ExceptionRecord->ExceptionInformation[0] == EXCEPTION_WRITE_FAULT) {
4327 /* the app tried to write the DIB bits */
4328 X11DRV_DIB_Coerce( physBitmap, DIB_Status_AppMod);
4330 /* the app tried to read the DIB bits */
4331 X11DRV_DIB_Coerce( physBitmap, DIB_Status_InSync);
4333 X11DRV_DIB_Unlock( physBitmap, TRUE );
4335 return EXCEPTION_CONTINUE_EXECUTION;
4338 /***********************************************************************
4341 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *physBitmap, INT req)
4343 INT ret = DIB_Status_None;
4345 if (!physBitmap->image) return ret; /* not a DIB section */
4346 EnterCriticalSection(&physBitmap->lock);
4347 ret = physBitmap->status;
4349 case DIB_Status_GdiMod:
4350 /* GDI access - request to draw on pixmap */
4351 switch (physBitmap->status)
4354 case DIB_Status_None:
4355 physBitmap->p_status = DIB_Status_GdiMod;
4356 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4359 case DIB_Status_GdiMod:
4360 TRACE("GdiMod requested in status GdiMod\n" );
4361 physBitmap->p_status = DIB_Status_GdiMod;
4364 case DIB_Status_InSync:
4365 TRACE("GdiMod requested in status InSync\n" );
4366 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4367 physBitmap->status = DIB_Status_GdiMod;
4368 physBitmap->p_status = DIB_Status_InSync;
4371 case DIB_Status_AppMod:
4372 TRACE("GdiMod requested in status AppMod\n" );
4373 /* make it readonly to avoid app changing data while we copy */
4374 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4375 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4376 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4377 physBitmap->p_status = DIB_Status_AppMod;
4378 physBitmap->status = DIB_Status_GdiMod;
4383 case DIB_Status_InSync:
4384 /* App access - request access to read DIB surface */
4385 /* (typically called from signal handler) */
4386 switch (physBitmap->status)
4389 case DIB_Status_None:
4390 /* shouldn't happen from signal handler */
4393 case DIB_Status_GdiMod:
4394 TRACE("InSync requested in status GdiMod\n" );
4395 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4396 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4397 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4398 physBitmap->status = DIB_Status_InSync;
4401 case DIB_Status_InSync:
4402 TRACE("InSync requested in status InSync\n" );
4403 /* shouldn't happen from signal handler */
4406 case DIB_Status_AppMod:
4407 TRACE("InSync requested in status AppMod\n" );
4408 /* no reason to do anything here, and this
4409 * shouldn't happen from signal handler */
4414 case DIB_Status_AppMod:
4415 /* App access - request access to write DIB surface */
4416 /* (typically called from signal handler) */
4417 switch (physBitmap->status)
4420 case DIB_Status_None:
4421 /* shouldn't happen from signal handler */
4424 case DIB_Status_GdiMod:
4425 TRACE("AppMod requested in status GdiMod\n" );
4426 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4427 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4428 physBitmap->status = DIB_Status_AppMod;
4431 case DIB_Status_InSync:
4432 TRACE("AppMod requested in status InSync\n" );
4433 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4434 physBitmap->status = DIB_Status_AppMod;
4437 case DIB_Status_AppMod:
4438 TRACE("AppMod requested in status AppMod\n" );
4439 /* shouldn't happen from signal handler */
4444 /* it is up to the caller to do the copy/conversion, probably
4445 * using the return value to decide where to copy from */
4447 LeaveCriticalSection(&physBitmap->lock);
4451 /***********************************************************************
4454 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *physBitmap, INT req)
4456 INT ret = DIB_Status_None;
4458 if (!physBitmap->image) return ret; /* not a DIB section */
4459 TRACE("Locking %p from thread %04x\n", physBitmap->hbitmap, GetCurrentThreadId());
4460 EnterCriticalSection(&physBitmap->lock);
4461 ret = physBitmap->status;
4462 if (req != DIB_Status_None)
4463 X11DRV_DIB_Coerce(physBitmap, req);
4467 /***********************************************************************
4470 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *physBitmap, BOOL commit)
4472 if (!physBitmap->image) return; /* not a DIB section */
4473 switch (physBitmap->status)
4476 case DIB_Status_None:
4477 /* in case anyone is wondering, this is the "signal handler doesn't
4478 * work" case, where we always have to be ready for app access */
4480 switch (physBitmap->p_status)
4482 case DIB_Status_GdiMod:
4483 TRACE("Unlocking and syncing from GdiMod\n" );
4484 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4488 TRACE("Unlocking without needing to sync\n" );
4492 else TRACE("Unlocking with no changes\n");
4493 physBitmap->p_status = DIB_Status_None;
4496 case DIB_Status_GdiMod:
4497 TRACE("Unlocking in status GdiMod\n" );
4498 /* DIB was protected in Coerce */
4500 /* no commit, revert to InSync if applicable */
4501 if ((physBitmap->p_status == DIB_Status_InSync) ||
4502 (physBitmap->p_status == DIB_Status_AppMod)) {
4503 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4504 physBitmap->status = DIB_Status_InSync;
4509 case DIB_Status_InSync:
4510 TRACE("Unlocking in status InSync\n" );
4511 /* DIB was already protected in Coerce */
4514 case DIB_Status_AppMod:
4515 TRACE("Unlocking in status AppMod\n" );
4516 /* DIB was already protected in Coerce */
4517 /* this case is ordinary only called from the signal handler,
4518 * so we don't bother to check for !commit */
4521 LeaveCriticalSection(&physBitmap->lock);
4522 TRACE("Unlocked %p\n", physBitmap->hbitmap);
4525 /***********************************************************************
4526 * X11DRV_CoerceDIBSection
4528 INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req)
4530 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4531 return X11DRV_DIB_Coerce(physDev->bitmap, req);
4534 /***********************************************************************
4535 * X11DRV_LockDIBSection
4537 INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req)
4539 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4540 return X11DRV_DIB_Lock(physDev->bitmap, req);
4543 /***********************************************************************
4544 * X11DRV_UnlockDIBSection
4546 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
4548 if (!physDev || !physDev->bitmap) return;
4549 X11DRV_DIB_Unlock(physDev->bitmap, commit);
4553 #ifdef HAVE_LIBXXSHM
4554 /***********************************************************************
4555 * X11DRV_XShmErrorHandler
4558 static int XShmErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
4560 return 1; /* FIXME: should check event contents */
4563 /***********************************************************************
4564 * X11DRV_XShmCreateImage
4567 static XImage *X11DRV_XShmCreateImage( int width, int height, int bpp,
4568 XShmSegmentInfo* shminfo)
4572 image = XShmCreateImage(gdi_display, visual, bpp, ZPixmap, NULL, shminfo, width, height);
4575 shminfo->shmid = shmget(IPC_PRIVATE, image->bytes_per_line * height,
4577 if( shminfo->shmid != -1 )
4579 shminfo->shmaddr = image->data = shmat(shminfo->shmid, 0, 0);
4580 if( shminfo->shmaddr != (char*)-1 )
4584 shminfo->readOnly = FALSE;
4585 X11DRV_expect_error( gdi_display, XShmErrorHandler, NULL );
4586 ok = (XShmAttach( gdi_display, shminfo ) != 0);
4587 XSync( gdi_display, False );
4588 if (X11DRV_check_error()) ok = FALSE;
4591 shmctl(shminfo->shmid, IPC_RMID, 0);
4592 return image; /* Success! */
4594 /* An error occurred */
4595 shmdt(shminfo->shmaddr);
4597 shmctl(shminfo->shmid, IPC_RMID, 0);
4598 shminfo->shmid = -1;
4600 XFlush(gdi_display);
4601 XDestroyImage(image);
4606 #endif /* HAVE_LIBXXSHM */
4609 /***********************************************************************
4610 * X11DRV_CreateDIBSection (X11DRV.@)
4612 HBITMAP X11DRV_CreateDIBSection( X11DRV_PDEVICE *physDev, HBITMAP hbitmap,
4613 const BITMAPINFO *bmi, UINT usage )
4615 X_PHYSBITMAP *physBitmap;
4618 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return 0;
4619 physBitmap->status = DIB_Status_None;
4621 GetObjectW( hbitmap, sizeof(dib), &dib );
4623 /* create color map */
4624 if (dib.dsBm.bmBitsPixel <= 8)
4626 physBitmap->colorMap = X11DRV_DIB_BuildColorMap( physDev,
4627 usage, dib.dsBm.bmBitsPixel, bmi,
4628 &physBitmap->nColorMap );
4631 /* create pixmap and X image */
4633 physBitmap->pixmap_depth = (dib.dsBm.bmBitsPixel == 1) ? 1 : screen_depth;
4634 physBitmap->pixmap = XCreatePixmap( gdi_display, root_window, dib.dsBm.bmWidth,
4635 dib.dsBm.bmHeight, physBitmap->pixmap_depth );
4636 #ifdef HAVE_LIBXXSHM
4637 physBitmap->shminfo.shmid = -1;
4638 if (!XShmQueryExtension(gdi_display) ||
4639 !(physBitmap->image = X11DRV_XShmCreateImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4640 physBitmap->pixmap_depth, &physBitmap->shminfo )) )
4642 physBitmap->image = X11DRV_DIB_CreateXImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4643 physBitmap->pixmap_depth );
4644 wine_tsx11_unlock();
4645 if (!physBitmap->pixmap || !physBitmap->image) return 0;
4647 /* install fault handler */
4648 InitializeCriticalSection( &physBitmap->lock );
4649 physBitmap->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": X_PHYSBITMAP.lock");
4651 physBitmap->base = dib.dsBm.bmBits;
4652 physBitmap->size = dib.dsBmih.biSizeImage;
4653 physBitmap->status = DIB_Status_AppMod;
4656 dibs_handler = AddVectoredExceptionHandler( TRUE, X11DRV_DIB_FaultHandler );
4657 EnterCriticalSection( &dibs_cs );
4658 list_add_head( &dibs_list, &physBitmap->entry );
4659 LeaveCriticalSection( &dibs_cs );
4661 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4666 /***********************************************************************
4667 * X11DRV_DIB_DeleteDIBSection
4669 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP *physBitmap, DIBSECTION *dib)
4673 EnterCriticalSection( &dibs_cs );
4674 list_remove( &physBitmap->entry );
4675 last = list_empty( &dibs_list );
4676 LeaveCriticalSection( &dibs_cs );
4680 RemoveVectoredExceptionHandler( dibs_handler );
4681 dibs_handler = NULL;
4684 if (dib->dshSection)
4685 X11DRV_DIB_Coerce(physBitmap, DIB_Status_InSync);
4687 if (physBitmap->image)
4690 #ifdef HAVE_LIBXXSHM
4691 if (physBitmap->shminfo.shmid != -1)
4693 XShmDetach( gdi_display, &(physBitmap->shminfo) );
4694 XDestroyImage( physBitmap->image );
4695 shmdt( physBitmap->shminfo.shmaddr );
4696 physBitmap->shminfo.shmid = -1;
4700 XDestroyImage( physBitmap->image );
4701 wine_tsx11_unlock();
4704 HeapFree(GetProcessHeap(), 0, physBitmap->colorMap);
4705 physBitmap->lock.DebugInfo->Spare[0] = 0;
4706 DeleteCriticalSection(&physBitmap->lock);
4709 /***********************************************************************
4710 * SetDIBColorTable (X11DRV.@)
4712 UINT X11DRV_SetDIBColorTable( X11DRV_PDEVICE *physDev, UINT start, UINT count, const RGBQUAD *colors )
4716 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4718 if (!physBitmap) return 0;
4719 GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib );
4721 if (physBitmap->colorMap && start < physBitmap->nColorMap) {
4722 UINT end = count + start;
4723 if (end > physBitmap->nColorMap) end = physBitmap->nColorMap;
4725 * Changing color table might change the mapping between
4726 * DIB colors and X11 colors and thus alter the visible state
4727 * of the bitmap object.
4730 * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4731 * at least for a 1 bpp dibsection
4733 X11DRV_DIB_Lock( physBitmap, DIB_Status_AppMod );
4734 X11DRV_DIB_GenColorMap( physDev, physBitmap->colorMap, DIB_RGB_COLORS,
4735 dib.dsBm.bmBitsPixel,
4736 TRUE, colors, start, end );
4737 X11DRV_DIB_Unlock( physBitmap, TRUE );
4744 /***********************************************************************
4745 * X11DRV_DIB_CreateDIBFromBitmap
4747 * Allocates a packed DIB and copies the bitmap data into it.
4749 HGLOBAL X11DRV_DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
4754 LPBITMAPINFOHEADER pbmiHeader;
4755 unsigned int cDataSize, cPackedSize, OffsetBits;
4758 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
4761 * A packed DIB contains a BITMAPINFO structure followed immediately by
4762 * an optional color palette and the pixel data.
4765 /* Calculate the size of the packed DIB */
4766 cDataSize = X11DRV_DIB_GetDIBWidthBytes( bmp.bmWidth, bmp.bmBitsPixel ) * abs( bmp.bmHeight );
4767 cPackedSize = sizeof(BITMAPINFOHEADER)
4768 + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
4770 /* Get the offset to the bits */
4771 OffsetBits = cPackedSize - cDataSize;
4773 /* Allocate the packed DIB */
4774 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
4775 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
4779 WARN("Could not allocate packed DIB!\n");
4783 /* A packed DIB starts with a BITMAPINFOHEADER */
4784 pPackedDIB = GlobalLock(hPackedDIB);
4785 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
4787 /* Init the BITMAPINFOHEADER */
4788 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
4789 pbmiHeader->biWidth = bmp.bmWidth;
4790 pbmiHeader->biHeight = bmp.bmHeight;
4791 pbmiHeader->biPlanes = 1;
4792 pbmiHeader->biBitCount = bmp.bmBitsPixel;
4793 pbmiHeader->biCompression = BI_RGB;
4794 pbmiHeader->biSizeImage = 0;
4795 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
4796 pbmiHeader->biClrUsed = 0;
4797 pbmiHeader->biClrImportant = 0;
4799 /* Retrieve the DIB bits from the bitmap and fill in the
4800 * DIB color table if present */
4802 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
4803 hBmp, /* Handle to bitmap */
4804 0, /* First scan line to set in dest bitmap */
4805 bmp.bmHeight, /* Number of scan lines to copy */
4806 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
4807 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
4808 0); /* RGB or palette index */
4809 GlobalUnlock(hPackedDIB);
4811 /* Cleanup if GetDIBits failed */
4812 if (nLinesCopied != bmp.bmHeight)
4814 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
4815 GlobalFree(hPackedDIB);
4822 /**************************************************************************
4823 * X11DRV_DIB_CreateDIBFromPixmap
4825 * Allocates a packed DIB and copies the Pixmap data into it.
4826 * The Pixmap passed in is deleted after the conversion.
4828 HGLOBAL X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap, HDC hdc)
4831 X_PHYSBITMAP *physBitmap;
4832 HBITMAP hBmp = 0, old;
4833 HGLOBAL hPackedDIB = 0;
4835 int x,y; /* Unused */
4836 unsigned border_width; /* Unused */
4837 unsigned int depth, width, height;
4839 /* Get the Pixmap dimensions and bit depth */
4841 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
4842 &border_width, &depth)) depth = 0;
4843 wine_tsx11_unlock();
4844 if (!depth) return 0;
4846 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
4847 width, height, depth);
4850 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
4851 * and make it a container for the pixmap passed.
4853 hBmp = CreateBitmap( width, height, 1, depth, NULL );
4855 /* force bitmap to be owned by a screen DC */
4856 hdcMem = CreateCompatibleDC( hdc );
4857 old = SelectObject( hdcMem, hBmp );
4859 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4862 if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
4863 physBitmap->pixmap = pixmap;
4864 wine_tsx11_unlock();
4866 SelectObject( hdcMem, old );
4870 * Create a packed DIB from the Pixmap wrapper bitmap created above.
4871 * A packed DIB contains a BITMAPINFO structure followed immediately by
4872 * an optional color palette and the pixel data.
4874 hPackedDIB = X11DRV_DIB_CreateDIBFromBitmap(hdc, hBmp);
4876 /* We can now get rid of the HBITMAP wrapper we created earlier.
4877 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
4881 TRACE("\tReturning packed DIB %p\n", hPackedDIB);
4886 /**************************************************************************
4887 * X11DRV_DIB_CreatePixmapFromDIB
4889 * Creates a Pixmap from a packed DIB
4891 Pixmap X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB, HDC hdc )
4894 X_PHYSBITMAP *physBitmap;
4898 /* Create a DDB from the DIB */
4900 pbmi = GlobalLock(hPackedDIB);
4901 hBmp = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT,
4902 (LPBYTE)pbmi + X11DRV_DIB_BitmapInfoSize( pbmi, DIB_RGB_COLORS ),
4903 pbmi, DIB_RGB_COLORS);
4904 GlobalUnlock(hPackedDIB);
4906 /* clear the physBitmap so that we can steal its pixmap */
4907 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4908 pixmap = physBitmap->pixmap;
4909 physBitmap->pixmap = 0;
4911 /* Delete the DDB we created earlier now that we have stolen its pixmap */
4914 TRACE("Returning Pixmap %ld\n", pixmap);