2 * X11DRV device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
26 #include <X11/extensions/XShm.h>
27 # ifdef HAVE_SYS_SHM_H
30 # ifdef HAVE_SYS_IPC_H
33 #endif /* defined(HAVE_LIBXXSHM) */
42 #include "wine/exception.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
47 static struct list dibs_list = LIST_INIT(dibs_list);
49 static CRITICAL_SECTION dibs_cs;
50 static CRITICAL_SECTION_DEBUG dibs_cs_debug =
53 { &dibs_cs_debug.ProcessLocksList, &dibs_cs_debug.ProcessLocksList },
54 0, 0, { (DWORD_PTR)(__FILE__ ": dibs_cs") }
56 static CRITICAL_SECTION dibs_cs = { &dibs_cs_debug, -1, 0, 0, 0, 0 };
58 static PVOID dibs_handler;
60 static int ximageDepthTable[32];
62 /* This structure holds the arguments for DIB_SetImageBits() */
65 X11DRV_PDEVICE *physDev;
68 PALETTEENTRY *palentry;
88 enum x11drv_shm_mode shm_mode;
91 X_PHYSBITMAP *physBitmap;
92 } X11DRV_DIB_IMAGEBITS_DESCR;
97 RLE_EOL = 0, /* End of line */
98 RLE_END = 1, /* End of bitmap */
99 RLE_DELTA = 2 /* Delta */
103 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *,INT);
104 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *,INT);
105 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *,BOOL);
108 Some of the following helper functions are duplicated in
112 /***********************************************************************
113 * DIB_DoProtectDIBSection
115 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP *physBitmap, DWORD new_prot )
119 VirtualProtect(physBitmap->base, physBitmap->size, new_prot, &old_prot);
120 TRACE("Changed protection from %d to %d\n", old_prot, new_prot);
123 /***********************************************************************
124 * X11DRV_DIB_GetXImageWidthBytes
126 * Return the width of an X image in bytes
128 static inline int X11DRV_DIB_GetXImageWidthBytes( int width, int depth )
130 if (!depth || depth > 32) goto error;
132 if (!ximageDepthTable[depth-1])
134 XImage *testimage = XCreateImage( gdi_display, visual, depth,
135 ZPixmap, 0, NULL, 1, 1, 32, 20 );
138 ximageDepthTable[depth-1] = testimage->bits_per_pixel;
139 XDestroyImage( testimage );
141 else ximageDepthTable[depth-1] = -1;
143 if (ximageDepthTable[depth-1] != -1)
144 return (4 * ((width * ximageDepthTable[depth-1] + 31) / 32));
147 WARN( "(%d): Unsupported depth\n", depth );
152 /***********************************************************************
153 * X11DRV_DIB_GetDIBWidthBytes
155 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
157 static int X11DRV_DIB_GetDIBWidthBytes( int width, int depth )
163 case 1: words = (width + 31) / 32; break;
164 case 4: words = (width + 7) / 8; break;
165 case 8: words = (width + 3) / 4; break;
167 case 16: words = (width + 1) / 2; break;
168 case 24: words = (width * 3 + 3) / 4; break;
170 WARN("(%d): Unsupported depth\n", depth );
179 /***********************************************************************
180 * X11DRV_DIB_GetDIBImageBytes
182 * Return the number of bytes used to hold the image in a DIB bitmap.
184 static int X11DRV_DIB_GetDIBImageBytes( int width, int height, int depth )
186 return X11DRV_DIB_GetDIBWidthBytes( width, depth ) * abs( height );
190 /***********************************************************************
193 * Return the size of the bitmap info structure including color table.
195 int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
197 unsigned int colors, size, masks = 0;
199 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
201 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
202 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
203 return sizeof(BITMAPCOREHEADER) + colors *
204 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
206 else /* assume BITMAPINFOHEADER */
208 colors = info->bmiHeader.biClrUsed;
209 if (!colors && (info->bmiHeader.biBitCount <= 8))
210 colors = 1 << info->bmiHeader.biBitCount;
211 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
212 size = max( info->bmiHeader.biSize, sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) );
213 return size + colors * ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
218 /***********************************************************************
219 * X11DRV_DIB_CreateXImage
223 XImage *X11DRV_DIB_CreateXImage( int width, int height, int depth )
226 XImage *image = NULL;
230 width_bytes = X11DRV_DIB_GetXImageWidthBytes( width, depth );
231 data = HeapAlloc( GetProcessHeap(), 0, height * width_bytes );
232 if (data) image = XCreateImage( gdi_display, visual, depth, ZPixmap, 0,
233 data, width, height, 32, width_bytes );
234 if (!image) HeapFree( GetProcessHeap(), 0, data );
240 /***********************************************************************
241 * X11DRV_DIB_DestroyXImage
243 * Destroy an X image created with X11DRV_DIB_CreateXImage.
245 void X11DRV_DIB_DestroyXImage( XImage *image )
247 HeapFree( GetProcessHeap(), 0, image->data );
250 XDestroyImage( image );
255 /***********************************************************************
256 * DIB_GetBitmapInfoEx
258 * Get the info from a bitmap header.
259 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
261 static int DIB_GetBitmapInfoEx( const BITMAPINFOHEADER *header, LONG *width,
262 LONG *height, WORD *planes, WORD *bpp,
263 WORD *compr, DWORD *size )
265 if (header->biSize == sizeof(BITMAPCOREHEADER))
267 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
268 *width = core->bcWidth;
269 *height = core->bcHeight;
270 *planes = core->bcPlanes;
271 *bpp = core->bcBitCount;
276 if (header->biSize >= sizeof(BITMAPINFOHEADER))
278 *width = header->biWidth;
279 *height = header->biHeight;
280 *planes = header->biPlanes;
281 *bpp = header->biBitCount;
282 *compr = header->biCompression;
283 *size = header->biSizeImage;
286 ERR("(%d): unknown/wrong size for header\n", header->biSize );
291 /***********************************************************************
292 * X11DRV_DIB_GetColorCount
294 * Computes the number of colors for the bitmap palette.
295 * Should not be called for a >8-bit deep bitmap.
297 static unsigned int X11DRV_DIB_GetColorCount(const BITMAPINFO *info)
300 BOOL core_info = info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER);
304 colors = 1 << ((const BITMAPCOREINFO*)info)->bmciHeader.bcBitCount;
308 colors = info->bmiHeader.biClrUsed;
309 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
313 ERR("called with >256 colors!\n");
319 /***********************************************************************
322 * Get the info from a bitmap header.
323 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
325 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
326 LONG *height, WORD *bpp, WORD *compr )
331 return DIB_GetBitmapInfoEx( header, width, height, &planes, bpp, compr, &size);
335 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
337 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
338 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
341 /***********************************************************************
342 * X11DRV_DIB_GenColorMap
344 * Fills the color map of a bitmap palette. Should not be called
345 * for a >8-bit deep bitmap.
347 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
348 WORD coloruse, WORD depth, BOOL quads,
349 const void *colorPtr, int start, int end )
353 if (coloruse == DIB_RGB_COLORS)
357 const RGBQUAD * rgb = colorPtr;
359 if (depth == 1) /* Monochrome */
364 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
365 invert = !colour_is_brighter(table[1], table[0]);
367 for (i = start; i < end; i++, rgb++)
368 colorMapping[i] = ((rgb->rgbRed + rgb->rgbGreen +
369 rgb->rgbBlue > 255*3/2 && !invert) ||
370 (rgb->rgbRed + rgb->rgbGreen +
371 rgb->rgbBlue <= 255*3/2 && invert));
374 for (i = start; i < end; i++, rgb++)
375 colorMapping[i] = X11DRV_PALETTE_LookupPixel(physDev->color_shifts, RGB(rgb->rgbRed,
381 const RGBTRIPLE * rgb = colorPtr;
383 if (depth == 1) /* Monochrome */
388 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
389 invert = !colour_is_brighter(table[1], table[0]);
391 for (i = start; i < end; i++, rgb++)
392 colorMapping[i] = ((rgb->rgbtRed + rgb->rgbtGreen +
393 rgb->rgbtBlue > 255*3/2 && !invert) ||
394 (rgb->rgbtRed + rgb->rgbtGreen +
395 rgb->rgbtBlue <= 255*3/2 && invert));
398 for (i = start; i < end; i++, rgb++)
399 colorMapping[i] = X11DRV_PALETTE_LookupPixel(physDev->color_shifts, RGB(rgb->rgbtRed,
404 else /* DIB_PAL_COLORS */
406 const WORD * index = colorPtr;
408 for (i = start; i < end; i++, index++)
409 colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(*index) );
415 /***********************************************************************
416 * X11DRV_DIB_BuildColorMap
418 * Build the color map from the bitmap palette. Should not be called
419 * for a >8-bit deep bitmap.
421 static int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE *physDev, WORD coloruse, WORD depth,
422 const BITMAPINFO *info, int *nColors )
425 const void *colorPtr;
429 *nColors = X11DRV_DIB_GetColorCount(info);
430 if (!*nColors) return NULL;
432 isInfo = info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER);
433 colorPtr = (const BYTE*)info + (WORD)info->bmiHeader.biSize;
434 if (!(colorMapping = HeapAlloc(GetProcessHeap(), 0, *nColors * sizeof(int) )))
437 return X11DRV_DIB_GenColorMap( physDev, colorMapping, coloruse, depth,
438 isInfo, colorPtr, 0, *nColors);
441 /***********************************************************************
442 * X11DRV_DIB_MapColor
444 static int X11DRV_DIB_MapColor( int *physMap, int nPhysMap, int phys, int oldcol )
448 if ((oldcol < nPhysMap) && (physMap[oldcol] == phys))
451 for (color = 0; color < nPhysMap; color++)
452 if (physMap[color] == phys)
455 WARN("Strange color %08x\n", phys);
460 /*********************************************************************
461 * X11DRV_DIB_GetNearestIndex
463 * Helper for X11DRV_DIB_GetDIBits.
464 * Returns the nearest colour table index for a given RGB.
465 * Nearest is defined by minimizing the sum of the squares.
467 static INT X11DRV_DIB_GetNearestIndex(RGBQUAD *colormap, int numColors, BYTE r, BYTE g, BYTE b)
469 INT i, best = -1, diff, bestdiff = -1;
472 for(color = colormap, i = 0; i < numColors; color++, i++) {
473 diff = (r - color->rgbRed) * (r - color->rgbRed) +
474 (g - color->rgbGreen) * (g - color->rgbGreen) +
475 (b - color->rgbBlue) * (b - color->rgbBlue);
478 if(best == -1 || diff < bestdiff) {
485 /*********************************************************************
486 * X11DRV_DIB_MaskToShift
488 * Helper for X11DRV_DIB_GetDIBits.
489 * Returns the by how many bits to shift a given color so that it is
490 * in the proper position.
492 INT X11DRV_DIB_MaskToShift(DWORD mask)
500 while ((mask&1)==0) {
507 /***********************************************************************
508 * X11DRV_DIB_CheckMask
510 * Check RGB mask if it is either 0 or matches visual's mask.
512 static inline int X11DRV_DIB_CheckMask(int red_mask, int green_mask, int blue_mask)
514 return ( red_mask == 0 && green_mask == 0 && blue_mask == 0 ) ||
515 ( red_mask == visual->red_mask && green_mask == visual->green_mask &&
516 blue_mask == visual->blue_mask );
519 /***********************************************************************
520 * X11DRV_DIB_SetImageBits_1
522 * SetDIBits for a 1-bit deep DIB.
524 static void X11DRV_DIB_SetImageBits_1( int lines, const BYTE *srcbits,
525 DWORD srcwidth, DWORD dstwidth, int left,
526 int *colors, XImage *bmpImage, int linebytes)
535 srcbits = srcbits + linebytes * (lines - 1);
536 linebytes = -linebytes;
539 if ((extra = (left & 7)) != 0) {
543 srcbits += left >> 3;
544 width = min(srcwidth, dstwidth);
546 /* ==== pal 1 dib -> any bmp format ==== */
547 for (h = lines-1; h >=0; h--) {
549 for (i = width/8, x = left; i > 0; i--) {
551 XPutPixel( bmpImage, x++, h, colors[ srcval >> 7] );
552 XPutPixel( bmpImage, x++, h, colors[(srcval >> 6) & 1] );
553 XPutPixel( bmpImage, x++, h, colors[(srcval >> 5) & 1] );
554 XPutPixel( bmpImage, x++, h, colors[(srcval >> 4) & 1] );
555 XPutPixel( bmpImage, x++, h, colors[(srcval >> 3) & 1] );
556 XPutPixel( bmpImage, x++, h, colors[(srcval >> 2) & 1] );
557 XPutPixel( bmpImage, x++, h, colors[(srcval >> 1) & 1] );
558 XPutPixel( bmpImage, x++, h, colors[ srcval & 1] );
564 case 7: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
565 case 6: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
566 case 5: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
567 case 4: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
568 case 3: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
569 case 2: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
570 case 1: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]);
573 srcbits += linebytes;
577 /***********************************************************************
578 * X11DRV_DIB_GetImageBits_1
580 * GetDIBits for a 1-bit deep DIB.
582 static void X11DRV_DIB_GetImageBits_1( int lines, BYTE *dstbits,
583 DWORD dstwidth, DWORD srcwidth,
584 RGBQUAD *colors, PALETTEENTRY *srccolors,
585 XImage *bmpImage, int linebytes )
588 int h, width = min(dstwidth, srcwidth);
592 dstbits = dstbits + linebytes * (lines - 1);
593 linebytes = -linebytes;
596 switch (bmpImage->depth)
600 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
602 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
605 for (h=lines-1; h>=0; h--) {
609 for (x=0; x<width; x++) {
611 srcval=srccolors[XGetPixel(bmpImage, x, h)];
612 dstval|=(X11DRV_DIB_GetNearestIndex
616 srcval.peBlue) << (7 - (x & 7)));
625 /* pad with 0 to DWORD alignment */
626 for (x = (x+7)&~7; x < ((width + 31) & ~31); x+=8)
628 dstbits += linebytes;
636 if (X11DRV_DIB_CheckMask(bmpImage->red_mask, bmpImage->green_mask, bmpImage->blue_mask)
638 /* ==== pal 8 bmp -> pal 1 dib ==== */
640 const BYTE* srcpixel;
643 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
645 for (h=0; h<lines; h++) {
650 for (x=0; x<width; x++) {
652 srcval=srccolors[*srcpixel++];
653 dstval|=(X11DRV_DIB_GetNearestIndex
657 srcval.peBlue) << (7-(x&7)) );
666 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
667 dstbits += linebytes;
678 const WORD* srcpixel;
681 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
683 if (bmpImage->green_mask==0x03e0) {
684 if (bmpImage->red_mask==0x7c00) {
685 /* ==== rgb 555 bmp -> pal 1 dib ==== */
686 for (h=0; h<lines; h++) {
691 for (x=0; x<width; x++) {
694 dstval|=(X11DRV_DIB_GetNearestIndex
696 ((srcval >> 7) & 0xf8) | /* r */
697 ((srcval >> 12) & 0x07),
698 ((srcval >> 2) & 0xf8) | /* g */
699 ((srcval >> 7) & 0x07),
700 ((srcval << 3) & 0xf8) | /* b */
701 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
710 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
711 dstbits += linebytes;
713 } else if (bmpImage->blue_mask==0x7c00) {
714 /* ==== bgr 555 bmp -> pal 1 dib ==== */
715 for (h=0; h<lines; h++) {
720 for (x=0; x<width; x++) {
723 dstval|=(X11DRV_DIB_GetNearestIndex
725 ((srcval << 3) & 0xf8) | /* r */
726 ((srcval >> 2) & 0x07),
727 ((srcval >> 2) & 0xf8) | /* g */
728 ((srcval >> 7) & 0x07),
729 ((srcval >> 7) & 0xf8) | /* b */
730 ((srcval >> 12) & 0x07) ) << (7-(x&7)) );
739 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
740 dstbits += linebytes;
745 } else if (bmpImage->green_mask==0x07e0) {
746 if (bmpImage->red_mask==0xf800) {
747 /* ==== rgb 565 bmp -> pal 1 dib ==== */
748 for (h=0; h<lines; h++) {
753 for (x=0; x<width; x++) {
756 dstval|=(X11DRV_DIB_GetNearestIndex
758 ((srcval >> 8) & 0xf8) | /* r */
759 ((srcval >> 13) & 0x07),
760 ((srcval >> 3) & 0xfc) | /* g */
761 ((srcval >> 9) & 0x03),
762 ((srcval << 3) & 0xf8) | /* b */
763 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
772 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
773 dstbits += linebytes;
775 } else if (bmpImage->blue_mask==0xf800) {
776 /* ==== bgr 565 bmp -> pal 1 dib ==== */
777 for (h=0; h<lines; h++) {
782 for (x=0; x<width; x++) {
785 dstval|=(X11DRV_DIB_GetNearestIndex
787 ((srcval << 3) & 0xf8) | /* r */
788 ((srcval >> 2) & 0x07),
789 ((srcval >> 3) & 0xfc) | /* g */
790 ((srcval >> 9) & 0x03),
791 ((srcval >> 8) & 0xf8) | /* b */
792 ((srcval >> 13) & 0x07) ) << (7-(x&7)) );
801 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
802 dstbits += linebytes;
821 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
822 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
824 if (bmpImage->green_mask!=0x00ff00 ||
825 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
827 } else if (bmpImage->blue_mask==0xff) {
828 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
829 for (h=0; h<lines; h++) {
834 for (x=0; x<width; x++) {
835 dstval|=(X11DRV_DIB_GetNearestIndex
839 srcbyte[0]) << (7-(x&7)) );
840 srcbyte+=bytes_per_pixel;
849 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
850 dstbits += linebytes;
853 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
854 for (h=0; h<lines; h++) {
859 for (x=0; x<width; x++) {
860 dstval|=(X11DRV_DIB_GetNearestIndex
864 srcbyte[2]) << (7-(x&7)) );
865 srcbyte+=bytes_per_pixel;
874 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
875 dstbits += linebytes;
886 unsigned long white = (1 << bmpImage->bits_per_pixel) - 1;
888 /* ==== any bmp format -> pal 1 dib ==== */
889 if ((unsigned)colors[0].rgbRed+colors[0].rgbGreen+colors[0].rgbBlue >=
890 (unsigned)colors[1].rgbRed+colors[1].rgbGreen+colors[1].rgbBlue )
893 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB, "
894 "%s color mapping\n",
895 bmpImage->bits_per_pixel, bmpImage->red_mask,
896 bmpImage->green_mask, bmpImage->blue_mask,
897 neg?"negative":"direct" );
899 for (h=lines-1; h>=0; h--) {
903 for (x=0; x<width; x++) {
904 dstval|=((XGetPixel( bmpImage, x, h) >= white) ^ neg) << (7 - (x&7));
913 dstbits += linebytes;
920 /***********************************************************************
921 * X11DRV_DIB_SetImageBits_4
923 * SetDIBits for a 4-bit deep DIB.
925 static void X11DRV_DIB_SetImageBits_4( int lines, const BYTE *srcbits,
926 DWORD srcwidth, DWORD dstwidth, int left,
927 int *colors, XImage *bmpImage, int linebytes)
935 srcbits = srcbits + linebytes * (lines - 1);
936 linebytes = -linebytes;
943 srcbits += left >> 1;
944 width = min(srcwidth, dstwidth);
946 /* ==== pal 4 dib -> any bmp format ==== */
947 for (h = lines-1; h >= 0; h--) {
949 for (i = width/2, x = left; i > 0; i--) {
950 BYTE srcval=*srcbyte++;
951 XPutPixel( bmpImage, x++, h, colors[srcval >> 4] );
952 XPutPixel( bmpImage, x++, h, colors[srcval & 0x0f] );
955 XPutPixel( bmpImage, x, h, colors[*srcbyte >> 4] );
956 srcbits += linebytes;
962 /***********************************************************************
963 * X11DRV_DIB_GetImageBits_4
965 * GetDIBits for a 4-bit deep DIB.
967 static void X11DRV_DIB_GetImageBits_4( int lines, BYTE *dstbits,
968 DWORD srcwidth, DWORD dstwidth,
969 RGBQUAD *colors, PALETTEENTRY *srccolors,
970 XImage *bmpImage, int linebytes )
973 int h, width = min(srcwidth, dstwidth);
978 dstbits = dstbits + ( linebytes * (lines-1) );
979 linebytes = -linebytes;
982 switch (bmpImage->depth) {
985 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
987 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
990 for (h = lines-1; h >= 0; h--) {
994 for (x = 0; x < width; x++) {
996 srcval=srccolors[XGetPixel(bmpImage, x, h)];
997 dstval|=(X11DRV_DIB_GetNearestIndex
1001 srcval.peBlue) << (4-((x&1)<<2)));
1010 dstbits += linebytes;
1018 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1020 /* ==== pal 8 bmp -> pal 4 dib ==== */
1021 const void* srcbits;
1022 const BYTE *srcpixel;
1025 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1026 for (h=0; h<lines; h++) {
1031 for (x=0; x<width; x++) {
1032 PALETTEENTRY srcval;
1033 srcval = srccolors[*srcpixel++];
1034 dstval|=(X11DRV_DIB_GetNearestIndex
1038 srcval.peBlue) << (4*(1-(x&1))) );
1047 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1048 dstbits += linebytes;
1058 const void* srcbits;
1059 const WORD* srcpixel;
1062 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1064 if (bmpImage->green_mask==0x03e0) {
1065 if (bmpImage->red_mask==0x7c00) {
1066 /* ==== rgb 555 bmp -> pal 4 dib ==== */
1067 for (h=0; h<lines; h++) {
1072 for (x=0; x<width; x++) {
1075 dstval|=(X11DRV_DIB_GetNearestIndex
1077 ((srcval >> 7) & 0xf8) | /* r */
1078 ((srcval >> 12) & 0x07),
1079 ((srcval >> 2) & 0xf8) | /* g */
1080 ((srcval >> 7) & 0x07),
1081 ((srcval << 3) & 0xf8) | /* b */
1082 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1091 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1092 dstbits += linebytes;
1094 } else if (bmpImage->blue_mask==0x7c00) {
1095 /* ==== bgr 555 bmp -> pal 4 dib ==== */
1096 for (h=0; h<lines; h++) {
1101 for (x=0; x<width; x++) {
1104 dstval|=(X11DRV_DIB_GetNearestIndex
1106 ((srcval << 3) & 0xf8) | /* r */
1107 ((srcval >> 2) & 0x07),
1108 ((srcval >> 2) & 0xf8) | /* g */
1109 ((srcval >> 7) & 0x07),
1110 ((srcval >> 7) & 0xf8) | /* b */
1111 ((srcval >> 12) & 0x07) ) << ((1-(x&1))<<2) );
1120 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1121 dstbits += linebytes;
1126 } else if (bmpImage->green_mask==0x07e0) {
1127 if (bmpImage->red_mask==0xf800) {
1128 /* ==== rgb 565 bmp -> pal 4 dib ==== */
1129 for (h=0; h<lines; h++) {
1134 for (x=0; x<width; x++) {
1137 dstval|=(X11DRV_DIB_GetNearestIndex
1139 ((srcval >> 8) & 0xf8) | /* r */
1140 ((srcval >> 13) & 0x07),
1141 ((srcval >> 3) & 0xfc) | /* g */
1142 ((srcval >> 9) & 0x03),
1143 ((srcval << 3) & 0xf8) | /* b */
1144 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1153 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1154 dstbits += linebytes;
1156 } else if (bmpImage->blue_mask==0xf800) {
1157 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1158 for (h=0; h<lines; h++) {
1163 for (x=0; x<width; x++) {
1166 dstval|=(X11DRV_DIB_GetNearestIndex
1168 ((srcval << 3) & 0xf8) | /* r */
1169 ((srcval >> 2) & 0x07),
1170 ((srcval >> 3) & 0xfc) | /* g */
1171 ((srcval >> 9) & 0x03),
1172 ((srcval >> 8) & 0xf8) | /* b */
1173 ((srcval >> 13) & 0x07) ) << ((1-(x&1))<<2) );
1182 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1183 dstbits += linebytes;
1195 if (bmpImage->bits_per_pixel==24) {
1196 const void* srcbits;
1197 const BYTE *srcbyte;
1200 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1202 if (bmpImage->green_mask!=0x00ff00 ||
1203 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1205 } else if (bmpImage->blue_mask==0xff) {
1206 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1207 for (h=0; h<lines; h++) {
1210 for (x=0; x<width/2; x++) {
1211 /* Do 2 pixels at a time */
1212 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1217 X11DRV_DIB_GetNearestIndex
1225 /* And then the odd pixel */
1226 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1232 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1233 dstbits += linebytes;
1236 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1237 for (h=0; h<lines; h++) {
1240 for (x=0; x<width/2; x++) {
1241 /* Do 2 pixels at a time */
1242 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1247 X11DRV_DIB_GetNearestIndex
1255 /* And then the odd pixel */
1256 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1262 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1263 dstbits += linebytes;
1272 const void* srcbits;
1273 const BYTE *srcbyte;
1276 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1278 if (bmpImage->green_mask!=0x00ff00 ||
1279 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1281 } else if (bmpImage->blue_mask==0xff) {
1282 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1283 for (h=0; h<lines; h++) {
1286 for (x=0; x<width/2; x++) {
1287 /* Do 2 pixels at a time */
1288 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1293 X11DRV_DIB_GetNearestIndex
1301 /* And then the odd pixel */
1302 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1308 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1309 dstbits += linebytes;
1312 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1313 for (h=0; h<lines; h++) {
1316 for (x=0; x<width/2; x++) {
1317 /* Do 2 pixels at a time */
1318 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1323 X11DRV_DIB_GetNearestIndex
1331 /* And then the odd pixel */
1332 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1338 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1339 dstbits += linebytes;
1350 /* ==== any bmp format -> pal 4 dib ==== */
1351 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1352 bmpImage->bits_per_pixel, bmpImage->red_mask,
1353 bmpImage->green_mask, bmpImage->blue_mask );
1354 for (h=lines-1; h>=0; h--) {
1356 for (x=0; x<(width & ~1); x+=2) {
1357 *dstbyte++=(X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4) |
1358 X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x+1, h), 0);
1361 *dstbyte=(X11DRV_DIB_MapColor((int *)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4);
1363 dstbits += linebytes;
1370 /***********************************************************************
1371 * X11DRV_DIB_SetImageBits_RLE4
1373 * SetDIBits for a 4-bit deep compressed DIB.
1375 static void X11DRV_DIB_SetImageBits_RLE4( int lines, const BYTE *bits,
1376 DWORD srcwidth, DWORD dstwidth,
1377 int left, int *colors,
1380 unsigned int x = 0, width = min(srcwidth, dstwidth);
1381 int y = lines - 1, c, length;
1382 const BYTE *begin = bits;
1387 if (length) { /* encoded */
1390 if (x >= (left + width)) break;
1391 if( x >= left) XPutPixel(bmpImage, x, y, colors[c >> 4]);
1393 if (!length--) break;
1394 if (x >= (left + width)) break;
1395 if( x >= left) XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1415 default: /* absolute */
1418 if (x >= left && x < (left + width))
1419 XPutPixel(bmpImage, x, y, colors[c >> 4]);
1421 if (!length--) break;
1422 if (x >= left && x < (left + width))
1423 XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1426 if ((bits - begin) & 1)
1435 /***********************************************************************
1436 * X11DRV_DIB_SetImageBits_8
1438 * SetDIBits for an 8-bit deep DIB.
1440 static void X11DRV_DIB_SetImageBits_8( int lines, const BYTE *srcbits,
1441 DWORD srcwidth, DWORD dstwidth, int left,
1442 const int *colors, XImage *bmpImage,
1446 int h, width = min(srcwidth, dstwidth);
1447 const BYTE* srcbyte;
1453 srcbits = srcbits + linebytes * (lines-1);
1454 linebytes = -linebytes;
1459 switch (bmpImage->depth) {
1462 /* Some X servers might have 32 bit/ 16bit deep pixel */
1463 if (lines && width && (bmpImage->bits_per_pixel == 16) &&
1464 (ImageByteOrder(gdi_display)==LSBFirst) )
1466 /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1467 dstbits=(BYTE*)bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1468 for (h = lines ; h--; ) {
1469 #if defined(__i386__) && defined(__GNUC__)
1470 int _cl1,_cl2; /* temp outputs for asm below */
1471 /* Borrowed from DirectDraw */
1472 __asm__ __volatile__(
1477 " movw (%%edx,%%eax,4),%%ax\n"
1479 " xor %%eax,%%eax\n"
1481 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1486 :"eax", "cc", "memory"
1489 DWORD* dstpixel=(DWORD*)dstbits;
1490 for (x=0; x<width/2; x++) {
1491 /* Do 2 pixels at a time */
1492 *dstpixel++=(colors[srcbyte[1]] << 16) | colors[srcbyte[0]];
1496 /* And then the odd pixel */
1497 *((WORD*)dstpixel)=colors[srcbyte[0]];
1500 srcbyte = (srcbits += linebytes);
1501 dstbits -= bmpImage->bytes_per_line;
1508 if (lines && width && (bmpImage->bits_per_pixel == 32) &&
1509 (ImageByteOrder(gdi_display)==LSBFirst) )
1511 dstbits=(BYTE*)bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
1512 /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1513 for (h = lines ; h--; ) {
1514 #if defined(__i386__) && defined(__GNUC__)
1515 int _cl1,_cl2; /* temp outputs for asm below */
1516 /* Borrowed from DirectDraw */
1517 __asm__ __volatile__(
1522 " movl (%%edx,%%eax,4),%%eax\n"
1524 " xor %%eax,%%eax\n"
1526 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1531 :"eax", "cc", "memory"
1534 DWORD* dstpixel=(DWORD*)dstbits;
1535 for (x=0; x<width; x++) {
1536 *dstpixel++=colors[*srcbyte++];
1539 srcbyte = (srcbits += linebytes);
1540 dstbits -= bmpImage->bytes_per_line;
1546 break; /* use slow generic case below */
1549 /* ==== pal 8 dib -> any bmp format ==== */
1550 for (h=lines-1; h>=0; h--) {
1551 for (x=left; x<width+left; x++) {
1552 XPutPixel(bmpImage, x, h, colors[*srcbyte++]);
1554 srcbyte = (srcbits += linebytes);
1558 /***********************************************************************
1559 * X11DRV_DIB_GetImageBits_8
1561 * GetDIBits for an 8-bit deep DIB.
1563 static void X11DRV_DIB_GetImageBits_8( int lines, BYTE *dstbits,
1564 DWORD srcwidth, DWORD dstwidth,
1565 RGBQUAD *colors, PALETTEENTRY *srccolors,
1566 XImage *bmpImage, int linebytes )
1569 int h, width = min(srcwidth, dstwidth);
1575 dstbits = dstbits + ( linebytes * (lines-1) );
1576 linebytes = -linebytes;
1581 * This condition is true when GetImageBits has been called by
1582 * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1583 * 256 colormaps, so we'll just use it for GetDIBits calls.
1584 * (In some cases, in an updateDIBSection, the returned colors are bad too)
1586 if (!srccolors) goto updatesection;
1588 switch (bmpImage->depth) {
1591 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1594 /* ==== pal 1 bmp -> pal 8 dib ==== */
1595 /* ==== pal 4 bmp -> pal 8 dib ==== */
1596 for (h=lines-1; h>=0; h--) {
1598 for (x=0; x<width; x++) {
1599 PALETTEENTRY srcval;
1600 srcval=srccolors[XGetPixel(bmpImage, x, h)];
1601 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1606 dstbits += linebytes;
1614 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1616 /* ==== pal 8 bmp -> pal 8 dib ==== */
1617 const void* srcbits;
1618 const BYTE* srcpixel;
1620 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1621 for (h=0; h<lines; h++) {
1624 for (x = 0; x < width; x++) {
1625 PALETTEENTRY srcval;
1626 srcval=srccolors[*srcpixel++];
1627 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1632 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1633 dstbits += linebytes;
1643 const void* srcbits;
1644 const WORD* srcpixel;
1647 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1649 if (bmpImage->green_mask==0x03e0) {
1650 if (bmpImage->red_mask==0x7c00) {
1651 /* ==== rgb 555 bmp -> pal 8 dib ==== */
1652 for (h=0; h<lines; h++) {
1655 for (x=0; x<width; x++) {
1658 *dstbyte++=X11DRV_DIB_GetNearestIndex
1660 ((srcval >> 7) & 0xf8) | /* r */
1661 ((srcval >> 12) & 0x07),
1662 ((srcval >> 2) & 0xf8) | /* g */
1663 ((srcval >> 7) & 0x07),
1664 ((srcval << 3) & 0xf8) | /* b */
1665 ((srcval >> 2) & 0x07) );
1667 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1668 dstbits += linebytes;
1670 } else if (bmpImage->blue_mask==0x7c00) {
1671 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1672 for (h=0; h<lines; h++) {
1675 for (x=0; x<width; x++) {
1678 *dstbyte++=X11DRV_DIB_GetNearestIndex
1680 ((srcval << 3) & 0xf8) | /* r */
1681 ((srcval >> 2) & 0x07),
1682 ((srcval >> 2) & 0xf8) | /* g */
1683 ((srcval >> 7) & 0x07),
1684 ((srcval >> 7) & 0xf8) | /* b */
1685 ((srcval >> 12) & 0x07) );
1687 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1688 dstbits += linebytes;
1693 } else if (bmpImage->green_mask==0x07e0) {
1694 if (bmpImage->red_mask==0xf800) {
1695 /* ==== rgb 565 bmp -> pal 8 dib ==== */
1696 for (h=0; h<lines; h++) {
1699 for (x=0; x<width; x++) {
1702 *dstbyte++=X11DRV_DIB_GetNearestIndex
1704 ((srcval >> 8) & 0xf8) | /* r */
1705 ((srcval >> 13) & 0x07),
1706 ((srcval >> 3) & 0xfc) | /* g */
1707 ((srcval >> 9) & 0x03),
1708 ((srcval << 3) & 0xf8) | /* b */
1709 ((srcval >> 2) & 0x07) );
1711 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1712 dstbits += linebytes;
1714 } else if (bmpImage->blue_mask==0xf800) {
1715 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1716 for (h=0; h<lines; h++) {
1719 for (x=0; x<width; x++) {
1722 *dstbyte++=X11DRV_DIB_GetNearestIndex
1724 ((srcval << 3) & 0xf8) | /* r */
1725 ((srcval >> 2) & 0x07),
1726 ((srcval >> 3) & 0xfc) | /* g */
1727 ((srcval >> 9) & 0x03),
1728 ((srcval >> 8) & 0xf8) | /* b */
1729 ((srcval >> 13) & 0x07) );
1731 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1732 dstbits += linebytes;
1746 const void* srcbits;
1747 const BYTE *srcbyte;
1749 int bytes_per_pixel;
1751 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1752 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
1754 if (bmpImage->green_mask!=0x00ff00 ||
1755 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1757 } else if (bmpImage->blue_mask==0xff) {
1758 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1759 for (h=0; h<lines; h++) {
1762 for (x=0; x<width; x++) {
1763 *dstbyte++=X11DRV_DIB_GetNearestIndex
1768 srcbyte+=bytes_per_pixel;
1770 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1771 dstbits += linebytes;
1774 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1775 for (h=0; h<lines; h++) {
1778 for (x=0; x<width; x++) {
1779 *dstbyte++=X11DRV_DIB_GetNearestIndex
1784 srcbyte+=bytes_per_pixel;
1786 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1787 dstbits += linebytes;
1795 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1796 bmpImage->depth, bmpImage->red_mask,
1797 bmpImage->green_mask, bmpImage->blue_mask );
1799 /* ==== any bmp format -> pal 8 dib ==== */
1800 for (h=lines-1; h>=0; h--) {
1802 for (x=0; x<width; x++) {
1803 *dstbyte=X11DRV_DIB_MapColor
1805 XGetPixel(bmpImage, x, h), *dstbyte);
1808 dstbits += linebytes;
1814 /***********************************************************************
1815 * X11DRV_DIB_SetImageBits_RLE8
1817 * SetDIBits for an 8-bit deep compressed DIB.
1819 * This function rewritten 941113 by James Youngman. WINE blew out when I
1820 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
1822 * This was because the algorithm assumed that all RLE8 bitmaps end with the
1823 * 'End of bitmap' escape code. This code is very much laxer in what it
1824 * allows to end the expansion. Possibly too lax. See the note by
1825 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
1826 * bitmap should end with RleEnd, but on the other hand, software exists
1827 * that produces ones that don't and Windows 3.1 doesn't complain a bit
1830 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
1831 * James A. Youngman <mbcstjy@afs.man.ac.uk>
1834 static void X11DRV_DIB_SetImageBits_RLE8( int lines, const BYTE *bits,
1835 DWORD srcwidth, DWORD dstwidth,
1836 int left, int *colors,
1839 unsigned int x; /* X-position on each line. Increases. */
1840 int y; /* Line #. Starts at lines-1, decreases */
1841 const BYTE *pIn = bits; /* Pointer to current position in bits */
1842 BYTE length; /* The length pf a run */
1843 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
1846 * Note that the bitmap data is stored by Windows starting at the
1847 * bottom line of the bitmap and going upwards. Within each line,
1848 * the data is stored left-to-right. That's the reason why line
1849 * goes from lines-1 to 0. [JAY]
1859 * If the length byte is not zero (which is the escape value),
1860 * We have a run of length pixels all the same colour. The colour
1861 * index is stored next.
1863 * If the length byte is zero, we need to read the next byte to
1864 * know what to do. [JAY]
1869 * [Run-Length] Encoded mode
1871 int color = colors[*pIn++];
1872 while (length-- && x < (left + dstwidth)) {
1873 if( x >= left) XPutPixel(bmpImage, x, y, color);
1880 * Escape codes (may be an absolute sequence though)
1882 escape_code = (*pIn++);
1891 /* Not all RLE8 bitmaps end with this code. For
1892 * example, Paint Shop Pro produces some that don't.
1893 * That's (I think) what caused the previous
1894 * implementation to fail. [JAY]
1903 default: /* switch to absolute mode */
1904 length = escape_code;
1907 int color = colors[*pIn++];
1908 if (x >= (left + dstwidth))
1913 if( x >= left) XPutPixel(bmpImage, x, y, color);
1917 * If you think for a moment you'll realise that the
1918 * only time we could ever possibly read an odd
1919 * number of bytes is when there is a 0x00 (escape),
1920 * a value >0x02 (absolute mode) and then an odd-
1921 * length run. Therefore this is the only place we
1922 * need to worry about it. Everywhere else the
1923 * bytes are always read in pairs. [JAY]
1925 if (escape_code & 1) pIn++; /* Throw away the pad byte. */
1927 } /* switch (escape_code) : Escape sequence */
1933 /***********************************************************************
1934 * X11DRV_DIB_SetImageBits_16
1936 * SetDIBits for a 16-bit deep DIB.
1938 static void X11DRV_DIB_SetImageBits_16( int lines, const BYTE *srcbits,
1939 DWORD srcwidth, DWORD dstwidth, int left,
1940 X11DRV_PDEVICE *physDev, DWORD rSrc, DWORD gSrc, DWORD bSrc,
1941 XImage *bmpImage, int linebytes )
1944 int h, width = min(srcwidth, dstwidth);
1945 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
1950 srcbits = srcbits + ( linebytes * (lines-1));
1951 linebytes = -linebytes;
1954 switch (bmpImage->depth)
1961 srcbits=srcbits+left*2;
1962 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1964 if (bmpImage->green_mask==0x03e0) {
1965 if (gSrc==bmpImage->green_mask) {
1966 if (rSrc==bmpImage->red_mask) {
1967 /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1968 /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1969 convs->Convert_5x5_asis
1972 dstbits,-bmpImage->bytes_per_line);
1973 } else if (rSrc==bmpImage->blue_mask) {
1974 /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1975 /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1976 convs->Convert_555_reverse
1979 dstbits,-bmpImage->bytes_per_line);
1982 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1983 /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1984 /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1985 convs->Convert_565_to_555_asis
1988 dstbits,-bmpImage->bytes_per_line);
1990 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1991 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1992 convs->Convert_565_to_555_reverse
1995 dstbits,-bmpImage->bytes_per_line);
1998 } else if (bmpImage->green_mask==0x07e0) {
1999 if (gSrc==bmpImage->green_mask) {
2000 if (rSrc==bmpImage->red_mask) {
2001 /* ==== rgb 565 dib -> rgb 565 bmp ==== */
2002 /* ==== bgr 565 dib -> bgr 565 bmp ==== */
2003 convs->Convert_5x5_asis
2006 dstbits,-bmpImage->bytes_per_line);
2008 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
2009 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
2010 convs->Convert_565_reverse
2013 dstbits,-bmpImage->bytes_per_line);
2016 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
2017 /* ==== rgb 555 dib -> rgb 565 bmp ==== */
2018 /* ==== bgr 555 dib -> bgr 565 bmp ==== */
2019 convs->Convert_555_to_565_asis
2022 dstbits,-bmpImage->bytes_per_line);
2024 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
2025 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
2026 convs->Convert_555_to_565_reverse
2029 dstbits,-bmpImage->bytes_per_line);
2039 if (bmpImage->bits_per_pixel==24) {
2042 srcbits=srcbits+left*2;
2043 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2045 if (bmpImage->green_mask!=0x00ff00 ||
2046 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2048 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2049 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2051 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
2052 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
2053 convs->Convert_555_to_888_asis
2056 dstbits,-bmpImage->bytes_per_line);
2058 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
2059 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
2060 convs->Convert_565_to_888_asis
2063 dstbits,-bmpImage->bytes_per_line);
2067 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
2068 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
2069 convs->Convert_555_to_888_reverse
2072 dstbits,-bmpImage->bytes_per_line);
2074 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
2075 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
2076 convs->Convert_565_to_888_reverse
2079 dstbits,-bmpImage->bytes_per_line);
2090 srcbits=srcbits+left*2;
2091 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2093 if (bmpImage->green_mask!=0x00ff00 ||
2094 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2096 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2097 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2099 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
2100 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
2101 convs->Convert_555_to_0888_asis
2104 dstbits,-bmpImage->bytes_per_line);
2106 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
2107 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
2108 convs->Convert_565_to_0888_asis
2111 dstbits,-bmpImage->bytes_per_line);
2115 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
2116 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
2117 convs->Convert_555_to_0888_reverse
2120 dstbits,-bmpImage->bytes_per_line);
2122 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
2123 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
2124 convs->Convert_565_to_0888_reverse
2127 dstbits,-bmpImage->bytes_per_line);
2135 WARN("from 16 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2136 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2137 bmpImage->green_mask, bmpImage->blue_mask );
2143 /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
2144 const WORD* srcpixel;
2145 int rShift1,gShift1,bShift1;
2146 int rShift2,gShift2,bShift2;
2149 /* Set color scaling values */
2150 rShift1=16+X11DRV_DIB_MaskToShift(rSrc)-3;
2151 gShift1=16+X11DRV_DIB_MaskToShift(gSrc)-3;
2152 bShift1=16+X11DRV_DIB_MaskToShift(bSrc)-3;
2157 /* Green has 5 bits, like the others */
2161 /* Green has 6 bits, not 5. Compensate. */
2170 /* We could split it into four separate cases to optimize
2171 * but it is probably not worth it.
2173 for (h=lines-1; h>=0; h--) {
2174 srcpixel=(const WORD*)srcbits;
2175 for (x=left; x<width+left; x++) {
2177 BYTE red,green,blue;
2178 srcval=*srcpixel++ << 16;
2179 red= ((srcval >> rShift1) & 0xf8) |
2180 ((srcval >> rShift2) & 0x07);
2181 green=((srcval >> gShift1) & gMask1) |
2182 ((srcval >> gShift2) & gMask2);
2183 blue= ((srcval >> bShift1) & 0xf8) |
2184 ((srcval >> bShift2) & 0x07);
2185 XPutPixel(bmpImage, x, h,
2186 X11DRV_PALETTE_ToPhysical
2187 (physDev, RGB(red,green,blue)));
2189 srcbits += linebytes;
2197 /***********************************************************************
2198 * X11DRV_DIB_GetImageBits_16
2200 * GetDIBits for an 16-bit deep DIB.
2202 static void X11DRV_DIB_GetImageBits_16( X11DRV_PDEVICE *physDev, int lines, BYTE *dstbits,
2203 DWORD dstwidth, DWORD srcwidth,
2204 PALETTEENTRY *srccolors,
2205 DWORD rDst, DWORD gDst, DWORD bDst,
2206 XImage *bmpImage, int linebytes )
2209 int h, width = min(srcwidth, dstwidth);
2210 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2215 dstbits = dstbits + ( linebytes * (lines-1));
2216 linebytes = -linebytes;
2219 switch (bmpImage->depth)
2224 const char* srcbits;
2226 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2228 if (bmpImage->green_mask==0x03e0) {
2229 if (gDst==bmpImage->green_mask) {
2230 if (rDst==bmpImage->red_mask) {
2231 /* ==== rgb 555 bmp -> rgb 555 dib ==== */
2232 /* ==== bgr 555 bmp -> bgr 555 dib ==== */
2233 convs->Convert_5x5_asis
2235 srcbits,-bmpImage->bytes_per_line,
2238 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
2239 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
2240 convs->Convert_555_reverse
2242 srcbits,-bmpImage->bytes_per_line,
2246 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2247 /* ==== rgb 555 bmp -> rgb 565 dib ==== */
2248 /* ==== bgr 555 bmp -> bgr 565 dib ==== */
2249 convs->Convert_555_to_565_asis
2251 srcbits,-bmpImage->bytes_per_line,
2254 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
2255 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
2256 convs->Convert_555_to_565_reverse
2258 srcbits,-bmpImage->bytes_per_line,
2262 } else if (bmpImage->green_mask==0x07e0) {
2263 if (gDst==bmpImage->green_mask) {
2264 if (rDst == bmpImage->red_mask) {
2265 /* ==== rgb 565 bmp -> rgb 565 dib ==== */
2266 /* ==== bgr 565 bmp -> bgr 565 dib ==== */
2267 convs->Convert_5x5_asis
2269 srcbits,-bmpImage->bytes_per_line,
2272 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
2273 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
2274 convs->Convert_565_reverse
2276 srcbits,-bmpImage->bytes_per_line,
2280 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2281 /* ==== rgb 565 bmp -> rgb 555 dib ==== */
2282 /* ==== bgr 565 bmp -> bgr 555 dib ==== */
2283 convs->Convert_565_to_555_asis
2285 srcbits,-bmpImage->bytes_per_line,
2288 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
2289 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
2290 convs->Convert_565_to_555_reverse
2292 srcbits,-bmpImage->bytes_per_line,
2303 if (bmpImage->bits_per_pixel == 24) {
2304 const char* srcbits;
2306 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2308 if (bmpImage->green_mask!=0x00ff00 ||
2309 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2311 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2312 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2314 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
2315 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
2316 convs->Convert_888_to_555_asis
2318 srcbits,-bmpImage->bytes_per_line,
2321 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2322 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2323 convs->Convert_888_to_565_asis
2325 srcbits,-bmpImage->bytes_per_line,
2330 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
2331 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
2332 convs->Convert_888_to_555_reverse
2334 srcbits,-bmpImage->bytes_per_line,
2337 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
2338 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
2339 convs->Convert_888_to_565_reverse
2341 srcbits,-bmpImage->bytes_per_line,
2351 const char* srcbits;
2353 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2355 if (bmpImage->green_mask!=0x00ff00 ||
2356 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2358 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2359 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2361 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2362 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2363 convs->Convert_0888_to_555_asis
2365 srcbits,-bmpImage->bytes_per_line,
2368 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2369 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2370 convs->Convert_0888_to_565_asis
2372 srcbits,-bmpImage->bytes_per_line,
2377 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2378 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2379 convs->Convert_0888_to_555_reverse
2381 srcbits,-bmpImage->bytes_per_line,
2384 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2385 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2386 convs->Convert_0888_to_565_reverse
2388 srcbits,-bmpImage->bytes_per_line,
2397 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2399 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2400 int rShift,gShift,bShift;
2403 /* Shift everything 16 bits left so that all shifts are >0,
2404 * even for BGR DIBs. Then a single >> 16 will bring everything
2407 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2408 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2409 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2411 /* 6 bits for the green */
2417 for (h = lines - 1; h >= 0; h--) {
2418 dstpixel=(LPWORD)dstbits;
2419 for (x = 0; x < width; x++) {
2420 PALETTEENTRY srcval;
2422 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2423 dstval=((srcval.peRed << rShift) & rDst) |
2424 ((srcval.peGreen << gShift) & gDst) |
2425 ((srcval.peBlue << bShift) & bDst);
2426 *dstpixel++=dstval >> 16;
2428 dstbits += linebytes;
2436 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2438 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2439 int rShift,gShift,bShift;
2440 const BYTE* srcbits;
2441 const BYTE* srcpixel;
2444 /* Shift everything 16 bits left so that all shifts are >0,
2445 * even for BGR DIBs. Then a single >> 16 will bring everything
2448 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2449 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2450 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2452 /* 6 bits for the green */
2458 srcbits=(BYTE*)bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2459 for (h=0; h<lines; h++) {
2461 dstpixel=(LPWORD)dstbits;
2462 for (x = 0; x < width; x++) {
2463 PALETTEENTRY srcval;
2465 srcval=srccolors[*srcpixel++];
2466 dstval=((srcval.peRed << rShift) & rDst) |
2467 ((srcval.peGreen << gShift) & gDst) |
2468 ((srcval.peBlue << bShift) & bDst);
2469 *dstpixel++=dstval >> 16;
2471 srcbits -= bmpImage->bytes_per_line;
2472 dstbits += linebytes;
2482 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2483 int rShift,gShift,bShift;
2486 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%x,%x,%x)\n",
2487 bmpImage->depth, bmpImage->red_mask,
2488 bmpImage->green_mask, bmpImage->blue_mask,
2491 /* Shift everything 16 bits left so that all shifts are >0,
2492 * even for BGR DIBs. Then a single >> 16 will bring everything
2495 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2496 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2497 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2499 /* 6 bits for the green */
2505 for (h = lines - 1; h >= 0; h--) {
2506 dstpixel=(LPWORD)dstbits;
2507 for (x = 0; x < width; x++) {
2510 srcval=X11DRV_PALETTE_ToLogical(physDev, XGetPixel(bmpImage, x, h));
2511 dstval=((GetRValue(srcval) << rShift) & rDst) |
2512 ((GetGValue(srcval) << gShift) & gDst) |
2513 ((GetBValue(srcval) << bShift) & bDst);
2514 *dstpixel++=dstval >> 16;
2516 dstbits += linebytes;
2524 /***********************************************************************
2525 * X11DRV_DIB_SetImageBits_24
2527 * SetDIBits for a 24-bit deep DIB.
2529 static void X11DRV_DIB_SetImageBits_24( int lines, const BYTE *srcbits,
2530 DWORD srcwidth, DWORD dstwidth, int left,
2531 X11DRV_PDEVICE *physDev,
2532 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2533 XImage *bmpImage, DWORD linebytes )
2536 int h, width = min(srcwidth, dstwidth);
2537 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2542 srcbits = srcbits + linebytes * (lines - 1);
2543 linebytes = -linebytes;
2546 switch (bmpImage->depth)
2549 if (bmpImage->bits_per_pixel==24) {
2552 srcbits=srcbits+left*3;
2553 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2555 if (bmpImage->green_mask!=0x00ff00 ||
2556 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2558 } else if (rSrc==bmpImage->red_mask) {
2559 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2560 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2561 convs->Convert_888_asis
2564 dstbits,-bmpImage->bytes_per_line);
2566 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2567 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2568 convs->Convert_888_reverse
2571 dstbits,-bmpImage->bytes_per_line);
2581 srcbits=srcbits+left*3;
2582 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2584 if (bmpImage->green_mask!=0x00ff00 ||
2585 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2587 } else if (rSrc==bmpImage->red_mask) {
2588 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2589 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2590 convs->Convert_888_to_0888_asis
2593 dstbits,-bmpImage->bytes_per_line);
2595 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2596 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2597 convs->Convert_888_to_0888_reverse
2600 dstbits,-bmpImage->bytes_per_line);
2610 srcbits=srcbits+left*3;
2611 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
2613 if (bmpImage->green_mask==0x03e0) {
2614 if ((rSrc==0xff0000 && bmpImage->red_mask==0x7f00) ||
2615 (bSrc==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2616 /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2617 /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2618 convs->Convert_888_to_555_asis
2621 dstbits,-bmpImage->bytes_per_line);
2622 } else if ((rSrc==0xff && bmpImage->red_mask==0x7f00) ||
2623 (bSrc==0xff && bmpImage->blue_mask==0x7f00)) {
2624 /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2625 /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2626 convs->Convert_888_to_555_reverse
2629 dstbits,-bmpImage->bytes_per_line);
2633 } else if (bmpImage->green_mask==0x07e0) {
2634 if ((rSrc==0xff0000 && bmpImage->red_mask==0xf800) ||
2635 (bSrc==0xff0000 && bmpImage->blue_mask==0xf800)) {
2636 /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2637 /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2638 convs->Convert_888_to_565_asis
2641 dstbits,-bmpImage->bytes_per_line);
2642 } else if ((rSrc==0xff && bmpImage->red_mask==0xf800) ||
2643 (bSrc==0xff && bmpImage->blue_mask==0xf800)) {
2644 /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2645 /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2646 convs->Convert_888_to_565_reverse
2649 dstbits,-bmpImage->bytes_per_line);
2661 WARN("from 24 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2662 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2663 bmpImage->green_mask, bmpImage->blue_mask );
2669 /* ==== rgb 888 dib -> any bmp format ==== */
2670 const BYTE* srcbyte;
2672 /* Windows only supports one 24bpp DIB format: RGB888 */
2674 for (h = lines - 1; h >= 0; h--) {
2676 for (x = left; x < width+left; x++) {
2677 XPutPixel(bmpImage, x, h,
2678 X11DRV_PALETTE_ToPhysical
2679 (physDev, RGB(srcbyte[2], srcbyte[1], srcbyte[0])));
2682 srcbits += linebytes;
2690 /***********************************************************************
2691 * X11DRV_DIB_GetImageBits_24
2693 * GetDIBits for an 24-bit deep DIB.
2695 static void X11DRV_DIB_GetImageBits_24( X11DRV_PDEVICE *physDev, int lines, BYTE *dstbits,
2696 DWORD dstwidth, DWORD srcwidth,
2697 PALETTEENTRY *srccolors,
2698 DWORD rDst, DWORD gDst, DWORD bDst,
2699 XImage *bmpImage, DWORD linebytes )
2702 int h, width = min(srcwidth, dstwidth);
2703 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2708 dstbits = dstbits + ( linebytes * (lines-1) );
2709 linebytes = -linebytes;
2712 switch (bmpImage->depth)
2715 if (bmpImage->bits_per_pixel==24) {
2716 const char* srcbits;
2718 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2720 if (bmpImage->green_mask!=0x00ff00 ||
2721 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2723 } else if (rDst==bmpImage->red_mask) {
2724 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2725 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2726 convs->Convert_888_asis
2728 srcbits,-bmpImage->bytes_per_line,
2731 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2732 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2733 convs->Convert_888_reverse
2735 srcbits,-bmpImage->bytes_per_line,
2744 const char* srcbits;
2746 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2748 if (bmpImage->green_mask!=0x00ff00 ||
2749 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2751 } else if (rDst==bmpImage->red_mask) {
2752 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2753 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2754 convs->Convert_0888_to_888_asis
2756 srcbits,-bmpImage->bytes_per_line,
2759 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2760 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2761 convs->Convert_0888_to_888_reverse
2763 srcbits,-bmpImage->bytes_per_line,
2772 const char* srcbits;
2774 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2776 if (bmpImage->green_mask==0x03e0) {
2777 if ((rDst==0xff0000 && bmpImage->red_mask==0x7f00) ||
2778 (bDst==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2779 /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2780 /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2781 convs->Convert_555_to_888_asis
2783 srcbits,-bmpImage->bytes_per_line,
2785 } else if ((rDst==0xff && bmpImage->red_mask==0x7f00) ||
2786 (bDst==0xff && bmpImage->blue_mask==0x7f00)) {
2787 /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2788 /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2789 convs->Convert_555_to_888_reverse
2791 srcbits,-bmpImage->bytes_per_line,
2796 } else if (bmpImage->green_mask==0x07e0) {
2797 if ((rDst==0xff0000 && bmpImage->red_mask==0xf800) ||
2798 (bDst==0xff0000 && bmpImage->blue_mask==0xf800)) {
2799 /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2800 /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2801 convs->Convert_565_to_888_asis
2803 srcbits,-bmpImage->bytes_per_line,
2805 } else if ((rDst==0xff && bmpImage->red_mask==0xf800) ||
2806 (bDst==0xff && bmpImage->blue_mask==0xf800)) {
2807 /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2808 /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2809 convs->Convert_565_to_888_reverse
2811 srcbits,-bmpImage->bytes_per_line,
2824 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2826 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2829 /* Windows only supports one 24bpp DIB format: rgb 888 */
2830 for (h = lines - 1; h >= 0; h--) {
2832 for (x = 0; x < width; x++) {
2833 PALETTEENTRY srcval;
2834 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2835 dstbyte[0]=srcval.peBlue;
2836 dstbyte[1]=srcval.peGreen;
2837 dstbyte[2]=srcval.peRed;
2840 dstbits += linebytes;
2848 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2850 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2851 const void* srcbits;
2852 const BYTE* srcpixel;
2855 /* Windows only supports one 24bpp DIB format: rgb 888 */
2856 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2857 for (h = lines - 1; h >= 0; h--) {
2860 for (x = 0; x < width; x++ ) {
2861 PALETTEENTRY srcval;
2862 srcval=srccolors[*srcpixel++];
2863 dstbyte[0]=srcval.peBlue;
2864 dstbyte[1]=srcval.peGreen;
2865 dstbyte[2]=srcval.peRed;
2868 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
2869 dstbits += linebytes;
2879 /* ==== any bmp format -> 888 dib ==== */
2882 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%x,%x,%x)\n",
2883 bmpImage->depth, bmpImage->red_mask,
2884 bmpImage->green_mask, bmpImage->blue_mask,
2887 /* Windows only supports one 24bpp DIB format: rgb 888 */
2888 for (h = lines - 1; h >= 0; h--) {
2890 for (x = 0; x < width; x++) {
2891 COLORREF srcval=X11DRV_PALETTE_ToLogical
2892 (physDev, XGetPixel( bmpImage, x, h ));
2893 dstbyte[0]=GetBValue(srcval);
2894 dstbyte[1]=GetGValue(srcval);
2895 dstbyte[2]=GetRValue(srcval);
2898 dstbits += linebytes;
2906 /***********************************************************************
2907 * X11DRV_DIB_SetImageBits_32
2909 * SetDIBits for a 32-bit deep DIB.
2911 static void X11DRV_DIB_SetImageBits_32(int lines, const BYTE *srcbits,
2912 DWORD srcwidth, DWORD dstwidth, int left,
2913 X11DRV_PDEVICE *physDev,
2914 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2919 int h, width = min(srcwidth, dstwidth);
2920 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2925 srcbits = srcbits + ( linebytes * (lines-1) );
2926 linebytes = -linebytes;
2929 switch (bmpImage->depth)
2932 if (bmpImage->bits_per_pixel==24) {
2935 srcbits=srcbits+left*4;
2936 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2938 if (rSrc==bmpImage->red_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->blue_mask) {
2939 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2940 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2941 convs->Convert_0888_to_888_asis
2944 dstbits,-bmpImage->bytes_per_line);
2945 } else if (bmpImage->green_mask!=0x00ff00 ||
2946 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2948 /* the tests below assume sane bmpImage masks */
2949 } else if (rSrc==bmpImage->blue_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->red_mask) {
2950 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2951 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2952 convs->Convert_0888_to_888_reverse
2955 dstbits,-bmpImage->bytes_per_line);
2956 } else if (bmpImage->blue_mask==0xff) {
2957 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2958 convs->Convert_any0888_to_rgb888
2962 dstbits,-bmpImage->bytes_per_line);
2964 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2965 convs->Convert_any0888_to_bgr888
2969 dstbits,-bmpImage->bytes_per_line);
2979 srcbits=srcbits+left*4;
2980 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2982 if (gSrc==bmpImage->green_mask) {
2983 if (rSrc==bmpImage->red_mask && bSrc==bmpImage->blue_mask) {
2984 /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2985 /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2986 convs->Convert_0888_asis
2989 dstbits,-bmpImage->bytes_per_line);
2990 } else if (bmpImage->green_mask!=0x00ff00 ||
2991 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2993 /* the tests below assume sane bmpImage masks */
2994 } else if (rSrc==bmpImage->blue_mask && bSrc==bmpImage->red_mask) {
2995 /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2996 /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2997 convs->Convert_0888_reverse
3000 dstbits,-bmpImage->bytes_per_line);
3002 /* ==== any 0888 dib -> any 0888 bmp ==== */
3003 convs->Convert_0888_any
3007 dstbits,-bmpImage->bytes_per_line,
3008 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3010 } else if (bmpImage->green_mask!=0x00ff00 ||
3011 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3013 /* the tests below assume sane bmpImage masks */
3015 /* ==== any 0888 dib -> any 0888 bmp ==== */
3016 convs->Convert_0888_any
3020 dstbits,-bmpImage->bytes_per_line,
3021 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3031 srcbits=srcbits+left*4;
3032 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
3034 if (rSrc==0xff0000 && gSrc==0x00ff00 && bSrc==0x0000ff) {
3035 if (bmpImage->green_mask==0x03e0) {
3036 if (bmpImage->red_mask==0x7f00) {
3037 /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
3038 convs->Convert_0888_to_555_asis
3041 dstbits,-bmpImage->bytes_per_line);
3042 } else if (bmpImage->blue_mask==0x7f00) {
3043 /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
3044 convs->Convert_0888_to_555_reverse
3047 dstbits,-bmpImage->bytes_per_line);
3051 } else if (bmpImage->green_mask==0x07e0) {
3052 if (bmpImage->red_mask==0xf800) {
3053 /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
3054 convs->Convert_0888_to_565_asis
3057 dstbits,-bmpImage->bytes_per_line);
3058 } else if (bmpImage->blue_mask==0xf800) {
3059 /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
3060 convs->Convert_0888_to_565_reverse
3063 dstbits,-bmpImage->bytes_per_line);
3070 } else if (rSrc==0x0000ff && gSrc==0x00ff00 && bSrc==0xff0000) {
3071 if (bmpImage->green_mask==0x03e0) {
3072 if (bmpImage->blue_mask==0x7f00) {
3073 /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
3074 convs->Convert_0888_to_555_asis
3077 dstbits,-bmpImage->bytes_per_line);
3078 } else if (bmpImage->red_mask==0x7f00) {
3079 /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
3080 convs->Convert_0888_to_555_reverse
3083 dstbits,-bmpImage->bytes_per_line);
3087 } else if (bmpImage->green_mask==0x07e0) {
3088 if (bmpImage->blue_mask==0xf800) {
3089 /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
3090 convs->Convert_0888_to_565_asis
3093 dstbits,-bmpImage->bytes_per_line);
3094 } else if (bmpImage->red_mask==0xf800) {
3095 /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
3096 convs->Convert_0888_to_565_reverse
3099 dstbits,-bmpImage->bytes_per_line);
3107 if (bmpImage->green_mask==0x03e0 &&
3108 (bmpImage->red_mask==0x7f00 ||
3109 bmpImage->blue_mask==0x7f00)) {
3110 /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
3111 convs->Convert_any0888_to_5x5
3115 dstbits,-bmpImage->bytes_per_line,
3116 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3117 } else if (bmpImage->green_mask==0x07e0 &&
3118 (bmpImage->red_mask==0xf800 ||
3119 bmpImage->blue_mask==0xf800)) {
3120 /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
3121 convs->Convert_any0888_to_5x5
3125 dstbits,-bmpImage->bytes_per_line,
3126 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3136 WARN("from 32 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
3137 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
3138 bmpImage->green_mask, bmpImage->blue_mask );
3144 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
3145 const DWORD* srcpixel;
3146 int rShift,gShift,bShift;
3148 rShift=X11DRV_DIB_MaskToShift(rSrc);
3149 gShift=X11DRV_DIB_MaskToShift(gSrc);
3150 bShift=X11DRV_DIB_MaskToShift(bSrc);
3152 for (h = lines - 1; h >= 0; h--) {
3153 srcpixel=(const DWORD*)srcbits;
3154 for (x = left; x < width+left; x++) {
3156 BYTE red,green,blue;
3157 srcvalue=*srcpixel++;
3158 red= (srcvalue >> rShift) & 0xff;
3159 green=(srcvalue >> gShift) & 0xff;
3160 blue= (srcvalue >> bShift) & 0xff;
3161 XPutPixel(bmpImage, x, h, X11DRV_PALETTE_ToPhysical
3162 (physDev, RGB(red,green,blue)));
3164 srcbits += linebytes;
3172 /***********************************************************************
3173 * X11DRV_DIB_GetImageBits_32
3175 * GetDIBits for an 32-bit deep DIB.
3177 static void X11DRV_DIB_GetImageBits_32( X11DRV_PDEVICE *physDev, int lines, BYTE *dstbits,
3178 DWORD dstwidth, DWORD srcwidth,
3179 PALETTEENTRY *srccolors,
3180 DWORD rDst, DWORD gDst, DWORD bDst,
3181 XImage *bmpImage, int linebytes )
3184 int h, width = min(srcwidth, dstwidth);
3185 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
3190 dstbits = dstbits + ( linebytes * (lines-1) );
3191 linebytes = -linebytes;
3194 switch (bmpImage->depth)
3197 if (bmpImage->bits_per_pixel==24) {
3198 const void* srcbits;
3200 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3202 if (rDst==bmpImage->red_mask && gDst==bmpImage->green_mask && bDst==bmpImage->blue_mask) {
3203 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
3204 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
3205 convs->Convert_888_to_0888_asis
3207 srcbits,-bmpImage->bytes_per_line,
3209 } else if (bmpImage->green_mask!=0x00ff00 ||
3210 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3212 /* the tests below assume sane bmpImage masks */
3213 } else if (rDst==bmpImage->blue_mask && gDst==bmpImage->green_mask && bDst==bmpImage->red_mask) {
3214 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
3215 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
3216 convs->Convert_888_to_0888_reverse
3218 srcbits,-bmpImage->bytes_per_line,
3220 } else if (bmpImage->blue_mask==0xff) {
3221 /* ==== rgb 888 bmp -> any 0888 dib ==== */
3222 convs->Convert_rgb888_to_any0888
3224 srcbits,-bmpImage->bytes_per_line,
3228 /* ==== bgr 888 bmp -> any 0888 dib ==== */
3229 convs->Convert_bgr888_to_any0888
3231 srcbits,-bmpImage->bytes_per_line,
3241 const char* srcbits;
3243 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3245 if (gDst==bmpImage->green_mask) {
3246 if (rDst==bmpImage->red_mask && bDst==bmpImage->blue_mask) {
3247 /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
3248 /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
3249 convs->Convert_0888_asis
3251 srcbits,-bmpImage->bytes_per_line,
3253 } else if (bmpImage->green_mask!=0x00ff00 ||
3254 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3256 /* the tests below assume sane bmpImage masks */
3257 } else if (rDst==bmpImage->blue_mask && bDst==bmpImage->red_mask) {
3258 /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
3259 /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
3260 convs->Convert_0888_reverse
3262 srcbits,-bmpImage->bytes_per_line,
3265 /* ==== any 0888 bmp -> any 0888 dib ==== */
3266 convs->Convert_0888_any
3268 srcbits,-bmpImage->bytes_per_line,
3269 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3273 } else if (bmpImage->green_mask!=0x00ff00 ||
3274 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3276 /* the tests below assume sane bmpImage masks */
3278 /* ==== any 0888 bmp -> any 0888 dib ==== */
3279 convs->Convert_0888_any
3281 srcbits,-bmpImage->bytes_per_line,
3282 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3292 const char* srcbits;
3294 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3296 if (rDst==0xff0000 && gDst==0x00ff00 && bDst==0x0000ff) {
3297 if (bmpImage->green_mask==0x03e0) {
3298 if (bmpImage->red_mask==0x7f00) {
3299 /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
3300 convs->Convert_555_to_0888_asis
3302 srcbits,-bmpImage->bytes_per_line,
3304 } else if (bmpImage->blue_mask==0x7f00) {
3305 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
3306 convs->Convert_555_to_0888_reverse
3308 srcbits,-bmpImage->bytes_per_line,
3313 } else if (bmpImage->green_mask==0x07e0) {
3314 if (bmpImage->red_mask==0xf800) {
3315 /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
3316 convs->Convert_565_to_0888_asis
3318 srcbits,-bmpImage->bytes_per_line,
3320 } else if (bmpImage->blue_mask==0xf800) {
3321 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
3322 convs->Convert_565_to_0888_reverse
3324 srcbits,-bmpImage->bytes_per_line,
3332 } else if (rDst==0x0000ff && gDst==0x00ff00 && bDst==0xff0000) {
3333 if (bmpImage->green_mask==0x03e0) {
3334 if (bmpImage->blue_mask==0x7f00) {
3335 /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
3336 convs->Convert_555_to_0888_asis
3338 srcbits,-bmpImage->bytes_per_line,
3340 } else if (bmpImage->red_mask==0x7f00) {
3341 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
3342 convs->Convert_555_to_0888_reverse
3344 srcbits,-bmpImage->bytes_per_line,
3349 } else if (bmpImage->green_mask==0x07e0) {
3350 if (bmpImage->blue_mask==0xf800) {
3351 /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3352 convs->Convert_565_to_0888_asis
3354 srcbits,-bmpImage->bytes_per_line,
3356 } else if (bmpImage->red_mask==0xf800) {
3357 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3358 convs->Convert_565_to_0888_reverse
3360 srcbits,-bmpImage->bytes_per_line,
3369 if (bmpImage->green_mask==0x03e0 &&
3370 (bmpImage->red_mask==0x7f00 ||
3371 bmpImage->blue_mask==0x7f00)) {
3372 /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3373 convs->Convert_5x5_to_any0888
3375 srcbits,-bmpImage->bytes_per_line,
3376 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3379 } else if (bmpImage->green_mask==0x07e0 &&
3380 (bmpImage->red_mask==0xf800 ||
3381 bmpImage->blue_mask==0xf800)) {
3382 /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3383 convs->Convert_5x5_to_any0888
3385 srcbits,-bmpImage->bytes_per_line,
3386 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3398 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3400 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3401 int rShift,gShift,bShift;
3404 rShift=X11DRV_DIB_MaskToShift(rDst);
3405 gShift=X11DRV_DIB_MaskToShift(gDst);
3406 bShift=X11DRV_DIB_MaskToShift(bDst);
3407 for (h = lines - 1; h >= 0; h--) {
3408 dstpixel=(DWORD*)dstbits;
3409 for (x = 0; x < width; x++) {
3410 PALETTEENTRY srcval;
3411 srcval = srccolors[XGetPixel(bmpImage, x, h)];
3412 *dstpixel++=(srcval.peRed << rShift) |
3413 (srcval.peGreen << gShift) |
3414 (srcval.peBlue << bShift);
3416 dstbits += linebytes;
3424 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3426 /* ==== pal 8 bmp -> any 0888 dib ==== */
3427 int rShift,gShift,bShift;
3428 const void* srcbits;
3429 const BYTE* srcpixel;
3432 rShift=X11DRV_DIB_MaskToShift(rDst);
3433 gShift=X11DRV_DIB_MaskToShift(gDst);
3434 bShift=X11DRV_DIB_MaskToShift(bDst);
3435 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3436 for (h = lines - 1; h >= 0; h--) {
3438 dstpixel=(DWORD*)dstbits;
3439 for (x = 0; x < width; x++) {
3440 PALETTEENTRY srcval;
3441 srcval=srccolors[*srcpixel++];
3442 *dstpixel++=(srcval.peRed << rShift) |
3443 (srcval.peGreen << gShift) |
3444 (srcval.peBlue << bShift);
3446 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
3447 dstbits += linebytes;
3457 /* ==== any bmp format -> any 0888 dib ==== */
3458 int rShift,gShift,bShift;
3461 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%x,%x,%x)\n",
3462 bmpImage->depth, bmpImage->red_mask,
3463 bmpImage->green_mask, bmpImage->blue_mask,
3466 rShift=X11DRV_DIB_MaskToShift(rDst);
3467 gShift=X11DRV_DIB_MaskToShift(gDst);
3468 bShift=X11DRV_DIB_MaskToShift(bDst);
3469 for (h = lines - 1; h >= 0; h--) {
3470 dstpixel=(DWORD*)dstbits;
3471 for (x = 0; x < width; x++) {
3473 srcval=X11DRV_PALETTE_ToLogical(physDev, XGetPixel(bmpImage, x, h));
3474 *dstpixel++=(GetRValue(srcval) << rShift) |
3475 (GetGValue(srcval) << gShift) |
3476 (GetBValue(srcval) << bShift);
3478 dstbits += linebytes;
3485 static int XGetSubImageErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
3487 return (event->request_code == X_GetImage && event->error_code == BadMatch);
3490 /***********************************************************************
3491 * X11DRV_DIB_SetImageBits_GetSubImage
3493 * Helper for X11DRV_DIB_SetImageBits
3495 static void X11DRV_DIB_SetImageBits_GetSubImage(
3496 const X11DRV_DIB_IMAGEBITS_DESCR *descr, XImage *bmpImage)
3498 /* compressed bitmaps may contain gaps in them. So make a copy
3499 * of the existing pixels first */
3502 SetRect( &bmprc, descr->xDest, descr->yDest,
3503 descr->xDest + descr->width , descr->yDest + descr->height );
3504 GetRgnBox( descr->physDev->region, &rc );
3505 /* convert from dc to drawable origin */
3506 OffsetRect( &rc, descr->physDev->dc_rect.left, descr->physDev->dc_rect.top);
3507 /* clip visible rect with bitmap */
3508 if( IntersectRect( &rc, &rc, &bmprc))
3510 X11DRV_expect_error( gdi_display, XGetSubImageErrorHandler, NULL );
3511 XGetSubImage( gdi_display, descr->drawable, rc.left, rc.top,
3512 rc.right - rc.left, rc.bottom - rc.top, AllPlanes,
3514 descr->xSrc + rc.left - bmprc.left,
3515 descr->ySrc + rc.top - bmprc.top);
3516 X11DRV_check_error();
3520 /***********************************************************************
3521 * X11DRV_DIB_SetImageBits
3523 * Transfer the bits to an X image.
3524 * Helper function for SetDIBits() and SetDIBitsToDevice().
3526 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3528 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3529 void *old_data = NULL;
3534 bmpImage = descr->image;
3536 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3537 descr->infoWidth, lines, 32, 0 );
3538 bmpImage->data = HeapAlloc( GetProcessHeap(), 0, lines * bmpImage->bytes_per_line );
3539 if(bmpImage->data == NULL) {
3540 ERR("Out of memory!\n");
3541 XDestroyImage( bmpImage );
3542 wine_tsx11_unlock();
3547 bmpImage->red_mask = descr->shifts->physicalRed.max << descr->shifts->physicalRed.shift;
3548 bmpImage->green_mask = descr->shifts->physicalGreen.max << descr->shifts->physicalGreen.shift;
3549 bmpImage->blue_mask = descr->shifts->physicalBlue.max << descr->shifts->physicalBlue.shift;
3552 wine_tsx11_unlock();
3554 TRACE("Dib: depth=%d r=%x g=%x b=%x\n",
3555 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3556 TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3557 bmpImage->depth,bmpImage->bits_per_pixel,
3558 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3560 #ifdef HAVE_LIBXXSHM
3561 if (descr->shm_mode == X11DRV_SHM_PIXMAP
3562 && descr->xSrc == 0 && descr->ySrc == 0
3563 && descr->xDest == 0 && descr->yDest == 0)
3565 TRACE("Using the shared pixmap data.\n");
3568 XSync( gdi_display, False );
3569 wine_tsx11_unlock();
3571 old_data = descr->image->data;
3572 descr->image->data = descr->physBitmap->shminfo.shmaddr;
3576 /* Transfer the pixels */
3579 switch(descr->infoBpp)
3582 X11DRV_DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
3583 descr->width, descr->xSrc, (int *)(descr->colorMap),
3584 bmpImage, descr->dibpitch );
3587 if (descr->compression) {
3588 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3589 X11DRV_DIB_SetImageBits_RLE4( descr->lines, descr->bits,
3590 descr->infoWidth, descr->width,
3591 descr->xSrc, (int *)(descr->colorMap),
3594 X11DRV_DIB_SetImageBits_4( descr->lines, descr->bits,
3595 descr->infoWidth, descr->width,
3596 descr->xSrc, (int*)(descr->colorMap),
3597 bmpImage, descr->dibpitch );
3600 if (descr->compression) {
3601 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3602 X11DRV_DIB_SetImageBits_RLE8( descr->lines, descr->bits,
3603 descr->infoWidth, descr->width,
3604 descr->xSrc, (int *)(descr->colorMap),
3607 X11DRV_DIB_SetImageBits_8( descr->lines, descr->bits,
3608 descr->infoWidth, descr->width,
3609 descr->xSrc, (int *)(descr->colorMap),
3610 bmpImage, descr->dibpitch );
3614 X11DRV_DIB_SetImageBits_16( descr->lines, descr->bits,
3615 descr->infoWidth, descr->width,
3616 descr->xSrc, descr->physDev,
3617 descr->rMask, descr->gMask, descr->bMask,
3618 bmpImage, descr->dibpitch);
3621 X11DRV_DIB_SetImageBits_24( descr->lines, descr->bits,
3622 descr->infoWidth, descr->width,
3623 descr->xSrc, descr->physDev,
3624 descr->rMask, descr->gMask, descr->bMask,
3625 bmpImage, descr->dibpitch);
3628 X11DRV_DIB_SetImageBits_32( descr->lines, descr->bits,
3629 descr->infoWidth, descr->width,
3630 descr->xSrc, descr->physDev,
3631 descr->rMask, descr->gMask, descr->bMask,
3632 bmpImage, descr->dibpitch);
3635 WARN("(%d): Invalid depth\n", descr->infoBpp );
3641 WARN( "invalid bits pointer %p\n", descr->bits );
3646 TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3647 descr->drawable, descr->gc, bmpImage,
3648 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3649 descr->width, descr->height);
3654 #ifdef HAVE_LIBXXSHM
3655 if (descr->shm_mode == X11DRV_SHM_PIXMAP
3656 && descr->xSrc == 0 && descr->ySrc == 0
3657 && descr->xDest == 0 && descr->yDest == 0)
3659 XSync( gdi_display, False );
3661 else if (descr->shm_mode == X11DRV_SHM_IMAGE && descr->image)
3663 XShmPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3664 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3665 descr->width, descr->height, FALSE );
3666 XSync( gdi_display, 0 );
3671 XPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3672 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3673 descr->width, descr->height );
3677 if (old_data) descr->image->data = old_data;
3679 if (!descr->image) X11DRV_DIB_DestroyXImage( bmpImage );
3680 wine_tsx11_unlock();
3684 /***********************************************************************
3685 * X11DRV_DIB_GetImageBits
3687 * Transfer the bits from an X image.
3689 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3691 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3692 void *old_data = NULL;
3697 bmpImage = descr->image;
3699 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3700 descr->infoWidth, lines, 32, 0 );
3701 bmpImage->data = HeapAlloc( GetProcessHeap(), 0, lines * bmpImage->bytes_per_line );
3702 if(bmpImage->data == NULL) {
3703 ERR("Out of memory!\n");
3704 XDestroyImage( bmpImage );
3705 wine_tsx11_unlock();
3710 bmpImage->red_mask = descr->shifts->physicalRed.max << descr->shifts->physicalRed.shift;
3711 bmpImage->green_mask = descr->shifts->physicalGreen.max << descr->shifts->physicalGreen.shift;
3712 bmpImage->blue_mask = descr->shifts->physicalBlue.max << descr->shifts->physicalBlue.shift;
3716 #ifdef HAVE_LIBXXSHM
3718 /* We must not call XShmGetImage() with a bitmap which is bigger than the available area.
3719 If we do, XShmGetImage() will fail (X exception), as it checks for this internally. */
3720 if (descr->shm_mode == X11DRV_SHM_PIXMAP && descr->image
3721 && descr->xSrc == 0 && descr->ySrc == 0
3722 && descr->xDest == 0 && descr->yDest == 0
3723 && bmpImage->width <= (descr->width - descr->xSrc)
3724 && bmpImage->height <= (descr->height - descr->ySrc))
3726 XSync( gdi_display, False );
3727 old_data = bmpImage->data;
3728 bmpImage->data = descr->physBitmap->shminfo.shmaddr;
3729 TRACE("Using shared pixmap data.\n");
3731 else if (descr->shm_mode == X11DRV_SHM_IMAGE && descr->image
3732 && bmpImage->width <= (descr->width - descr->xSrc)
3733 && bmpImage->height <= (descr->height - descr->ySrc))
3735 int saveRed, saveGreen, saveBlue;
3737 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3738 gdi_display, descr->drawable, bmpImage,
3739 descr->xSrc, descr->ySrc, AllPlanes);
3741 /* We must save and restore the bmpImage's masks in order
3742 * to preserve them across the call to XShmGetImage, which
3743 * decides to eliminate them since it doesn't happen to know
3744 * what the format of the image is supposed to be, even though
3746 saveRed = bmpImage->red_mask;
3747 saveBlue= bmpImage->blue_mask;
3748 saveGreen = bmpImage->green_mask;
3750 XShmGetImage( gdi_display, descr->drawable, bmpImage,
3751 descr->xSrc, descr->ySrc, AllPlanes);
3753 bmpImage->red_mask = saveRed;
3754 bmpImage->blue_mask = saveBlue;
3755 bmpImage->green_mask = saveGreen;
3758 #endif /* HAVE_LIBXXSHM */
3760 TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3761 gdi_display, descr->drawable, descr->xSrc, descr->ySrc, descr->width,
3762 lines, AllPlanes, ZPixmap, bmpImage, descr->xDest, descr->yDest);
3763 XGetSubImage( gdi_display, descr->drawable, descr->xSrc, descr->ySrc,
3764 descr->width, lines, AllPlanes, ZPixmap,
3765 bmpImage, descr->xDest, descr->yDest );
3767 wine_tsx11_unlock();
3769 TRACE("Dib: depth=%2d r=%x g=%x b=%x\n",
3770 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3771 TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3772 bmpImage->depth,bmpImage->bits_per_pixel,
3773 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3774 /* Transfer the pixels */
3775 switch(descr->infoBpp)
3778 X11DRV_DIB_GetImageBits_1( descr->lines,(LPVOID)descr->bits,
3779 descr->infoWidth, descr->width,
3780 descr->colorMap, descr->palentry,
3781 bmpImage, descr->dibpitch );
3785 if (descr->compression) {
3786 FIXME("Compression not yet supported!\n");
3787 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 4 ) * abs(descr->lines))
3790 X11DRV_DIB_GetImageBits_4( descr->lines,(LPVOID)descr->bits,
3791 descr->infoWidth, descr->width,
3792 descr->colorMap, descr->palentry,
3793 bmpImage, descr->dibpitch );
3796 if (descr->compression) {
3797 FIXME("Compression not yet supported!\n");
3798 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 8 ) * abs(descr->lines))
3801 X11DRV_DIB_GetImageBits_8( descr->lines, (LPVOID)descr->bits,
3802 descr->infoWidth, descr->width,
3803 descr->colorMap, descr->palentry,
3804 bmpImage, descr->dibpitch );
3808 X11DRV_DIB_GetImageBits_16( descr->physDev, descr->lines, (LPVOID)descr->bits,
3809 descr->infoWidth,descr->width,
3811 descr->rMask, descr->gMask, descr->bMask,
3812 bmpImage, descr->dibpitch );
3816 X11DRV_DIB_GetImageBits_24( descr->physDev, descr->lines, (LPVOID)descr->bits,
3817 descr->infoWidth,descr->width,
3819 descr->rMask, descr->gMask, descr->bMask,
3820 bmpImage, descr->dibpitch);
3824 X11DRV_DIB_GetImageBits_32( descr->physDev, descr->lines, (LPVOID)descr->bits,
3825 descr->infoWidth, descr->width,
3827 descr->rMask, descr->gMask, descr->bMask,
3828 bmpImage, descr->dibpitch);
3832 WARN("(%d): Invalid depth\n", descr->infoBpp );
3836 if (old_data) bmpImage->data = old_data;
3837 if (!descr->image) X11DRV_DIB_DestroyXImage( bmpImage );
3841 /*************************************************************************
3842 * X11DRV_SetDIBitsToDevice
3845 INT CDECL X11DRV_SetDIBitsToDevice( PHYSDEV dev, INT xDest, INT yDest, DWORD cx,
3846 DWORD cy, INT xSrc, INT ySrc,
3847 UINT startscan, UINT lines, LPCVOID bits,
3848 const BITMAPINFO *info, UINT coloruse )
3850 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
3851 X11DRV_DIB_IMAGEBITS_DESCR descr;
3856 int rop = X11DRV_XROPfunction[GetROP2(physDev->hdc) - 1];
3858 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3859 &descr.infoBpp, &descr.compression ) == -1)
3862 top_down = (height < 0);
3863 if (top_down) height = -height;
3867 LPtoDP(physDev->hdc, &pt, 1);
3869 if (!lines || (startscan >= height)) return 0;
3870 if (!top_down && startscan + lines > height) lines = height - startscan;
3872 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3873 * and clamp all values to fit inside [startscan,startscan+lines]
3875 if (ySrc + cy <= startscan + lines)
3877 UINT y = startscan + lines - (ySrc + cy);
3878 if (ySrc < startscan) cy -= (startscan - ySrc);
3881 /* avoid getting unnecessary lines */
3883 if (y >= lines) return 0;
3888 if (y >= lines) return lines;
3889 ySrc = y; /* need to get all lines in top down mode */
3894 if (ySrc >= startscan + lines) return lines;
3895 pt.y += ySrc + cy - (startscan + lines);
3896 cy = startscan + lines - ySrc;
3898 if (cy > lines) cy = lines;
3900 if (xSrc >= width) return lines;
3901 if (xSrc + cx >= width) cx = width - xSrc;
3902 if (!cx || !cy) return lines;
3904 /* Update the pixmap from the DIB section */
3905 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
3907 X11DRV_SetupGCForText( physDev ); /* To have the correct colors */
3909 XSetFunction(gdi_display, physDev->gc, rop);
3910 wine_tsx11_unlock();
3912 switch (descr.infoBpp)
3917 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3919 physDev->depth, info, &descr.nColorMap );
3920 if (!descr.colorMap) return 0;
3921 descr.rMask = descr.gMask = descr.bMask = 0;
3925 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3926 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3927 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3933 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3934 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3935 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3940 descr.physDev = physDev;
3943 descr.palentry = NULL;
3944 descr.lines = top_down ? -lines : lines;
3945 descr.infoWidth = width;
3946 descr.depth = physDev->depth;
3947 descr.shifts = physDev->color_shifts;
3948 descr.drawable = physDev->drawable;
3949 descr.gc = physDev->gc;
3952 descr.xDest = physDev->dc_rect.left + pt.x;
3953 descr.yDest = physDev->dc_rect.top + pt.y;
3956 descr.shm_mode = X11DRV_SHM_NONE;
3957 descr.dibpitch = ((width * descr.infoBpp + 31) &~31) / 8;
3958 descr.physBitmap = NULL;
3960 result = X11DRV_DIB_SetImageBits( &descr );
3962 if (descr.infoBpp <= 8)
3963 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3965 /* Update the DIBSection of the pixmap */
3966 X11DRV_UnlockDIBSection(physDev, TRUE);
3971 /***********************************************************************
3972 * SetDIBits (X11DRV.@)
3974 INT CDECL X11DRV_SetDIBits( PHYSDEV dev, HBITMAP hbitmap, UINT startscan,
3975 UINT lines, LPCVOID bits, const BITMAPINFO *info, UINT coloruse )
3977 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
3978 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3979 X11DRV_DIB_IMAGEBITS_DESCR descr;
3981 LONG width, height, tmpheight;
3984 descr.physDev = physDev;
3986 if (!physBitmap) return 0;
3988 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3989 &descr.infoBpp, &descr.compression ) == -1)
3993 if (height < 0) height = -height;
3994 if (!lines || (startscan >= height))
3997 if (!GetObjectW( hbitmap, sizeof(ds), &ds )) return 0;
3999 if (startscan + lines > height) lines = height - startscan;
4001 switch (descr.infoBpp)
4006 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
4007 descr.physDev, coloruse,
4008 physBitmap->pixmap_depth,
4009 info, &descr.nColorMap );
4010 if (!descr.colorMap) return 0;
4011 descr.rMask = descr.gMask = descr.bMask = 0;
4015 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4016 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4017 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4023 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4024 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4025 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4034 descr.palentry = NULL;
4035 descr.infoWidth = width;
4036 descr.lines = tmpheight >= 0 ? lines : -lines;
4037 descr.depth = physBitmap->pixmap_depth;
4038 descr.shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
4039 descr.drawable = physBitmap->pixmap;
4040 descr.gc = get_bitmap_gc(physBitmap->pixmap_depth);
4044 descr.yDest = height - startscan - lines;
4045 descr.width = ds.dsBm.bmWidth;
4046 descr.height = lines;
4047 descr.shm_mode = X11DRV_SHM_NONE;
4048 descr.dibpitch = ((descr.infoWidth * descr.infoBpp + 31) &~31) / 8;
4049 descr.physBitmap = NULL;
4050 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
4051 result = X11DRV_DIB_SetImageBits( &descr );
4053 /* optimisation for the case where the input bits are in exactly the same
4054 * format as the internal representation and copying to the app bits is
4055 * cheap - saves a round trip to the X server */
4056 if (descr.compression == BI_RGB &&
4057 coloruse == DIB_RGB_COLORS &&
4058 descr.infoBpp == ds.dsBm.bmBitsPixel &&
4059 physBitmap->base && physBitmap->size < 65536)
4061 unsigned int srcwidthb = X11DRV_DIB_GetDIBWidthBytes( width, descr.infoBpp );
4062 int dstwidthb = ds.dsBm.bmWidthBytes;
4063 LPBYTE dbits = physBitmap->base + startscan * dstwidthb;
4064 const BYTE *sbits = bits;
4068 TRACE("syncing compatible set bits to app bits\n");
4069 if ((tmpheight < 0) ^ physBitmap->topdown)
4071 dbits += dstwidthb * (lines-1);
4072 dstwidthb = -dstwidthb;
4074 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4075 widthb = min(srcwidthb, abs(dstwidthb));
4076 for (y = 0; y < lines; y++, dbits += dstwidthb, sbits += srcwidthb)
4077 memcpy(dbits, sbits, widthb);
4078 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4079 physBitmap->status = DIB_Status_InSync;
4081 X11DRV_DIB_Unlock( physBitmap, TRUE );
4083 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4088 /***********************************************************************
4089 * GetDIBits (X11DRV.@)
4091 INT CDECL X11DRV_GetDIBits( PHYSDEV dev, HBITMAP hbitmap, UINT startscan, UINT lines,
4092 LPVOID bits, BITMAPINFO *info, UINT coloruse )
4094 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
4095 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
4097 X11DRV_DIB_IMAGEBITS_DESCR descr;
4098 PALETTEENTRY palette[256];
4101 LONG width, tempHeight;
4105 static const PALETTEENTRY peBlack = {0,0,0,0};
4106 static const PALETTEENTRY peWhite = {255,255,255,0};
4108 if (!physBitmap) return 0;
4109 if (!(obj_size = GetObjectW( hbitmap, sizeof(dib), &dib ))) return 0;
4111 bitmap_type = DIB_GetBitmapInfo( (BITMAPINFOHEADER*)info, &width, &tempHeight, &descr.infoBpp, &descr.compression);
4112 if (bitmap_type == -1)
4114 ERR("Invalid bitmap\n");
4118 if (physBitmap->pixmap_depth > 1)
4120 GetPaletteEntries( GetCurrentObject( physDev->hdc, OBJ_PAL ), 0, 256, palette );
4124 palette[0] = peBlack;
4125 palette[1] = peWhite;
4128 descr.lines = tempHeight;
4129 core_header = (bitmap_type == 0);
4130 colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
4132 TRACE("%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
4133 lines, dib.dsBm.bmWidth, dib.dsBm.bmHeight, width, descr.lines, startscan);
4135 if( lines > dib.dsBm.bmHeight ) lines = dib.dsBm.bmHeight;
4137 height = descr.lines;
4138 if (height < 0) height = -height;
4139 if( lines > height ) lines = height;
4140 /* Top-down images have a negative biHeight, the scanlines of these images
4141 * were inverted in X11DRV_DIB_GetImageBits_xx
4142 * To prevent this we simply change the sign of lines
4143 * (the number of scan lines to copy).
4144 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4146 if( descr.lines < 0 && lines > 0) lines = -lines;
4148 if( startscan >= dib.dsBm.bmHeight ) return 0;
4150 descr.colorMap = NULL;
4152 switch (descr.infoBpp)
4157 descr.rMask= descr.gMask = descr.bMask = 0;
4158 if(coloruse == DIB_RGB_COLORS)
4159 descr.colorMap = colorPtr;
4161 int num_colors = 1 << descr.infoBpp, i;
4164 WORD *index = colorPtr;
4165 descr.colorMap = rgb = HeapAlloc(GetProcessHeap(), 0, num_colors * sizeof(RGBQUAD));
4166 for(i = 0; i < num_colors; i++, rgb++, index++) {
4167 colref = X11DRV_PALETTE_ToLogical(physDev, X11DRV_PALETTE_ToPhysical(physDev, PALETTEINDEX(*index)));
4168 rgb->rgbRed = GetRValue(colref);
4169 rgb->rgbGreen = GetGValue(colref);
4170 rgb->rgbBlue = GetBValue(colref);
4171 rgb->rgbReserved = 0;
4177 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4178 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4179 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4183 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4184 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4185 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4189 descr.physDev = physDev;
4190 descr.palentry = palette;
4192 descr.image = physBitmap->image;
4193 descr.infoWidth = width;
4194 descr.lines = lines;
4195 descr.depth = physBitmap->pixmap_depth;
4196 descr.shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
4197 descr.drawable = physBitmap->pixmap;
4198 descr.gc = get_bitmap_gc(physBitmap->pixmap_depth);
4199 descr.width = dib.dsBm.bmWidth;
4200 descr.height = dib.dsBm.bmHeight;
4204 descr.sizeImage = core_header ? 0 : info->bmiHeader.biSizeImage;
4205 descr.physBitmap = physBitmap;
4207 if (descr.lines > 0)
4209 descr.ySrc = (descr.height-1) - (startscan + (lines-1));
4213 descr.ySrc = startscan;
4215 descr.shm_mode = physBitmap->shm_mode;
4216 descr.dibpitch = (((descr.infoWidth * descr.infoBpp + 31) &~31) / 8);
4218 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
4219 X11DRV_DIB_GetImageBits( &descr );
4220 X11DRV_DIB_Unlock( physBitmap, TRUE );
4222 if(!core_header && info->bmiHeader.biSizeImage == 0) /* Fill in biSizeImage */
4223 info->bmiHeader.biSizeImage = X11DRV_DIB_GetDIBImageBytes( descr.infoWidth,
4227 if (descr.compression == BI_BITFIELDS)
4229 *(DWORD *)info->bmiColors = descr.rMask;
4230 *((DWORD *)info->bmiColors + 1) = descr.gMask;
4231 *((DWORD *)info->bmiColors + 2) = descr.bMask;
4233 else if (!core_header)
4235 /* if RLE or JPEG compression were supported,
4236 * this line would be invalid. */
4237 info->bmiHeader.biCompression = 0;
4240 if(descr.colorMap != colorPtr)
4241 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4245 /***********************************************************************
4246 * X11DRV_DIB_DoCopyDIBSection
4248 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB,
4249 void *colorMap, int nColorMap,
4250 Drawable dest, GC gc,
4251 DWORD xSrc, DWORD ySrc,
4252 DWORD xDest, DWORD yDest,
4253 DWORD width, DWORD height)
4255 DIBSECTION dibSection;
4256 X11DRV_DIB_IMAGEBITS_DESCR descr;
4257 int identity[2] = {0,1};
4259 if (!GetObjectW( physBitmap->hbitmap, sizeof(dibSection), &dibSection )) return;
4261 descr.physDev = NULL;
4262 descr.palentry = NULL;
4263 descr.infoWidth = dibSection.dsBmih.biWidth;
4264 descr.infoBpp = dibSection.dsBmih.biBitCount;
4265 descr.lines = physBitmap->topdown ? -dibSection.dsBmih.biHeight : dibSection.dsBmih.biHeight;
4266 descr.image = physBitmap->image;
4267 descr.colorMap = colorMap;
4268 descr.nColorMap = nColorMap;
4269 descr.bits = dibSection.dsBm.bmBits;
4270 descr.depth = physBitmap->pixmap_depth;
4271 descr.shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
4272 descr.compression = dibSection.dsBmih.biCompression;
4273 descr.physBitmap = physBitmap;
4275 if(descr.infoBpp == 1)
4276 descr.colorMap = (void*)identity;
4278 switch (descr.infoBpp)
4283 descr.rMask = descr.gMask = descr.bMask = 0;
4287 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0x7c00;
4288 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x03e0;
4289 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x001f;
4294 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0xff0000;
4295 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x00ff00;
4296 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x0000ff;
4301 descr.drawable = dest;
4305 descr.xDest = xDest;
4306 descr.yDest = yDest;
4307 descr.width = width;
4308 descr.height = height;
4309 descr.sizeImage = 0;
4311 descr.shm_mode = physBitmap->shm_mode;
4312 #ifdef HAVE_LIBXXSHM
4313 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP && physBitmap->pixmap != dest)
4315 descr.shm_mode = X11DRV_SHM_NONE;
4318 descr.dibpitch = dibSection.dsBm.bmWidthBytes;
4322 TRACE("Copying from Pixmap to DIB bits\n");
4323 X11DRV_DIB_GetImageBits( &descr );
4327 TRACE("Copying from DIB bits to Pixmap\n");
4328 X11DRV_DIB_SetImageBits( &descr );
4332 /***********************************************************************
4333 * X11DRV_DIB_CopyDIBSection
4335 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
4336 DWORD xSrc, DWORD ySrc, DWORD xDest, DWORD yDest,
4337 DWORD width, DWORD height)
4340 X_PHYSBITMAP *physBitmap;
4341 unsigned int nColorMap;
4345 TRACE("(%p,%p,%d,%d,%d,%d,%d,%d)\n", physDevSrc->hdc, physDevDst->hdc,
4346 xSrc, ySrc, xDest, yDest, width, height);
4347 /* this function is meant as an optimization for BitBlt,
4348 * not to be called otherwise */
4349 physBitmap = physDevSrc->bitmap;
4350 if (!physBitmap || GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib ) != sizeof(dib))
4352 ERR("called for non-DIBSection!?\n");
4355 /* while BitBlt should already have made sure we only get
4356 * positive values, we should check for oversize values */
4357 if ((xSrc < dib.dsBm.bmWidth) &&
4358 (ySrc < dib.dsBm.bmHeight)) {
4359 if (xSrc + width > dib.dsBm.bmWidth)
4360 width = dib.dsBm.bmWidth - xSrc;
4361 if (ySrc + height > dib.dsBm.bmHeight)
4362 height = dib.dsBm.bmHeight - ySrc;
4363 /* if the source bitmap is 8bpp or less, we're supposed to use the
4364 * DC's palette for color conversion (not the DIB color table) */
4365 if (dib.dsBm.bmBitsPixel <= 8) {
4366 HPALETTE hPalette = GetCurrentObject( physDevSrc->hdc, OBJ_PAL );
4367 if (!hPalette || (hPalette == GetStockObject(DEFAULT_PALETTE))) {
4368 /* HACK: no palette has been set in the source DC,
4369 * use the DIB colormap instead - this is necessary in some
4370 * cases since we need to do depth conversion in some places
4371 * where real Windows can just copy data straight over */
4372 x11ColorMap = physBitmap->colorMap;
4373 nColorMap = physBitmap->nColorMap;
4374 freeColorMap = FALSE;
4376 const BITMAPINFO* info = (BITMAPINFO*)&dib.dsBmih;
4379 nColorMap = X11DRV_DIB_GetColorCount(info);
4380 x11ColorMap = HeapAlloc(GetProcessHeap(), 0, nColorMap * sizeof(int));
4381 for (i = 0; i < nColorMap; i++)
4382 x11ColorMap[i] = X11DRV_PALETTE_ToPhysical(physDevSrc, PALETTEINDEX(i));
4383 freeColorMap = TRUE;
4390 freeColorMap = FALSE;
4392 /* perform the copy */
4393 X11DRV_DIB_DoCopyDIBSection(physBitmap, FALSE, x11ColorMap, nColorMap,
4394 physDevDst->drawable, physDevDst->gc, xSrc, ySrc,
4395 physDevDst->dc_rect.left + xDest, physDevDst->dc_rect.top + yDest,
4397 /* free color mapping */
4399 HeapFree(GetProcessHeap(), 0, x11ColorMap);
4403 /***********************************************************************
4404 * X11DRV_DIB_DoUpdateDIBSection
4406 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB)
4410 GetObjectW( physBitmap->hbitmap, sizeof(bitmap), &bitmap );
4411 X11DRV_DIB_DoCopyDIBSection(physBitmap, toDIB,
4412 physBitmap->colorMap, physBitmap->nColorMap,
4413 physBitmap->pixmap, get_bitmap_gc(physBitmap->pixmap_depth),
4414 0, 0, 0, 0, bitmap.bmWidth, bitmap.bmHeight);
4417 /***********************************************************************
4418 * X11DRV_DIB_FaultHandler
4420 static LONG CALLBACK X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep )
4422 X_PHYSBITMAP *physBitmap = NULL;
4426 const size_t pagemask = getpagesize() - 1;
4428 if (ep->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
4429 return EXCEPTION_CONTINUE_SEARCH;
4431 addr = (BYTE *)ep->ExceptionRecord->ExceptionInformation[1];
4433 EnterCriticalSection(&dibs_cs);
4434 LIST_FOR_EACH( ptr, &dibs_list )
4436 physBitmap = LIST_ENTRY( ptr, X_PHYSBITMAP, entry );
4437 if ((physBitmap->base <= addr) &&
4438 (addr < physBitmap->base + ((physBitmap->size + pagemask) & ~pagemask)))
4444 LeaveCriticalSection(&dibs_cs);
4446 if (!found) return EXCEPTION_CONTINUE_SEARCH;
4448 if (addr >= physBitmap->base + physBitmap->size)
4449 WARN( "%p: access to %p beyond the end of the DIB\n", physBitmap->hbitmap, addr );
4451 X11DRV_DIB_Lock( physBitmap, DIB_Status_None );
4452 if (ep->ExceptionRecord->ExceptionInformation[0] == EXCEPTION_WRITE_FAULT) {
4453 /* the app tried to write the DIB bits */
4454 X11DRV_DIB_Coerce( physBitmap, DIB_Status_AppMod);
4456 /* the app tried to read the DIB bits */
4457 X11DRV_DIB_Coerce( physBitmap, DIB_Status_InSync);
4459 X11DRV_DIB_Unlock( physBitmap, TRUE );
4461 return EXCEPTION_CONTINUE_EXECUTION;
4464 /***********************************************************************
4467 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *physBitmap, INT req)
4469 INT ret = DIB_Status_None;
4471 if (!physBitmap->image) return ret; /* not a DIB section */
4472 EnterCriticalSection(&physBitmap->lock);
4473 ret = physBitmap->status;
4475 case DIB_Status_GdiMod:
4476 /* GDI access - request to draw on pixmap */
4477 switch (physBitmap->status)
4480 case DIB_Status_None:
4481 physBitmap->p_status = DIB_Status_GdiMod;
4482 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4485 case DIB_Status_GdiMod:
4486 TRACE("GdiMod requested in status GdiMod\n" );
4487 physBitmap->p_status = DIB_Status_GdiMod;
4490 case DIB_Status_InSync:
4491 TRACE("GdiMod requested in status InSync\n" );
4492 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4493 physBitmap->status = DIB_Status_GdiMod;
4494 physBitmap->p_status = DIB_Status_InSync;
4497 case DIB_Status_AppMod:
4498 TRACE("GdiMod requested in status AppMod\n" );
4499 /* make it readonly to avoid app changing data while we copy */
4500 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4501 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4502 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4503 physBitmap->p_status = DIB_Status_AppMod;
4504 physBitmap->status = DIB_Status_GdiMod;
4509 case DIB_Status_InSync:
4510 /* App access - request access to read DIB surface */
4511 /* (typically called from signal handler) */
4512 switch (physBitmap->status)
4515 case DIB_Status_None:
4516 /* shouldn't happen from signal handler */
4519 case DIB_Status_GdiMod:
4520 TRACE("InSync requested in status GdiMod\n" );
4521 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4522 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4523 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4524 physBitmap->status = DIB_Status_InSync;
4527 case DIB_Status_InSync:
4528 TRACE("InSync requested in status InSync\n" );
4529 /* shouldn't happen from signal handler */
4532 case DIB_Status_AppMod:
4533 TRACE("InSync requested in status AppMod\n" );
4534 /* no reason to do anything here, and this
4535 * shouldn't happen from signal handler */
4540 case DIB_Status_AppMod:
4541 /* App access - request access to write DIB surface */
4542 /* (typically called from signal handler) */
4543 switch (physBitmap->status)
4546 case DIB_Status_None:
4547 /* shouldn't happen from signal handler */
4550 case DIB_Status_GdiMod:
4551 TRACE("AppMod requested in status GdiMod\n" );
4552 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4553 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4554 physBitmap->status = DIB_Status_AppMod;
4557 case DIB_Status_InSync:
4558 TRACE("AppMod requested in status InSync\n" );
4559 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4560 physBitmap->status = DIB_Status_AppMod;
4563 case DIB_Status_AppMod:
4564 TRACE("AppMod requested in status AppMod\n" );
4565 /* shouldn't happen from signal handler */
4570 /* it is up to the caller to do the copy/conversion, probably
4571 * using the return value to decide where to copy from */
4573 LeaveCriticalSection(&physBitmap->lock);
4577 /***********************************************************************
4580 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *physBitmap, INT req)
4582 INT ret = DIB_Status_None;
4584 if (!physBitmap->image) return ret; /* not a DIB section */
4585 TRACE("Locking %p from thread %04x\n", physBitmap->hbitmap, GetCurrentThreadId());
4586 EnterCriticalSection(&physBitmap->lock);
4587 ret = physBitmap->status;
4588 if (req != DIB_Status_None)
4589 X11DRV_DIB_Coerce(physBitmap, req);
4593 /***********************************************************************
4596 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *physBitmap, BOOL commit)
4598 if (!physBitmap->image) return; /* not a DIB section */
4599 switch (physBitmap->status)
4602 case DIB_Status_None:
4603 /* in case anyone is wondering, this is the "signal handler doesn't
4604 * work" case, where we always have to be ready for app access */
4606 switch (physBitmap->p_status)
4608 case DIB_Status_GdiMod:
4609 TRACE("Unlocking and syncing from GdiMod\n" );
4610 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4614 TRACE("Unlocking without needing to sync\n" );
4618 else TRACE("Unlocking with no changes\n");
4619 physBitmap->p_status = DIB_Status_None;
4622 case DIB_Status_GdiMod:
4623 TRACE("Unlocking in status GdiMod\n" );
4624 /* DIB was protected in Coerce */
4626 /* no commit, revert to InSync if applicable */
4627 if ((physBitmap->p_status == DIB_Status_InSync) ||
4628 (physBitmap->p_status == DIB_Status_AppMod)) {
4629 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4630 physBitmap->status = DIB_Status_InSync;
4635 case DIB_Status_InSync:
4636 TRACE("Unlocking in status InSync\n" );
4637 /* DIB was already protected in Coerce */
4640 case DIB_Status_AppMod:
4641 TRACE("Unlocking in status AppMod\n" );
4642 /* DIB was already protected in Coerce */
4643 /* this case is ordinary only called from the signal handler,
4644 * so we don't bother to check for !commit */
4647 LeaveCriticalSection(&physBitmap->lock);
4648 TRACE("Unlocked %p\n", physBitmap->hbitmap);
4651 /***********************************************************************
4652 * X11DRV_CoerceDIBSection
4654 INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req)
4656 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4657 return X11DRV_DIB_Coerce(physDev->bitmap, req);
4660 /***********************************************************************
4661 * X11DRV_LockDIBSection
4663 INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req)
4665 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4666 return X11DRV_DIB_Lock(physDev->bitmap, req);
4669 /***********************************************************************
4670 * X11DRV_UnlockDIBSection
4672 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
4674 if (!physDev || !physDev->bitmap) return;
4675 X11DRV_DIB_Unlock(physDev->bitmap, commit);
4679 #ifdef HAVE_LIBXXSHM
4680 /***********************************************************************
4681 * X11DRV_XShmErrorHandler
4684 static int XShmErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
4686 return 1; /* FIXME: should check event contents */
4689 /***********************************************************************
4690 * X11DRV_XShmCreateImage
4693 static XImage *X11DRV_XShmCreateImage( int width, int height, int bpp,
4694 XShmSegmentInfo* shminfo)
4698 image = XShmCreateImage(gdi_display, visual, bpp, ZPixmap, NULL, shminfo, width, height);
4701 shminfo->shmid = shmget(IPC_PRIVATE, image->bytes_per_line * height,
4703 if( shminfo->shmid != -1 )
4705 shminfo->shmaddr = shmat( shminfo->shmid, 0, 0 );
4706 if( shminfo->shmaddr != (char*)-1 )
4710 shminfo->readOnly = FALSE;
4711 X11DRV_expect_error( gdi_display, XShmErrorHandler, NULL );
4712 ok = (XShmAttach( gdi_display, shminfo ) != 0);
4713 XSync( gdi_display, False );
4714 if (X11DRV_check_error()) ok = FALSE;
4717 shmctl(shminfo->shmid, IPC_RMID, 0);
4718 return image; /* Success! */
4720 /* An error occurred */
4721 shmdt(shminfo->shmaddr);
4723 shmctl(shminfo->shmid, IPC_RMID, 0);
4724 shminfo->shmid = -1;
4726 XFlush(gdi_display);
4727 XDestroyImage(image);
4732 #endif /* HAVE_LIBXXSHM */
4734 static Bool X11DRV_DIB_QueryXShm( Bool *pixmaps )
4736 static Bool have_xshm, have_xshm_pixmaps;
4737 static BOOL initialized;
4741 #ifdef HAVE_LIBXXSHM
4744 have_xshm = XShmQueryVersion( gdi_display, &major, &minor, &have_xshm_pixmaps );
4749 *pixmaps = have_xshm_pixmaps;
4753 /***********************************************************************
4754 * X11DRV_CreateDIBSection (X11DRV.@)
4756 HBITMAP CDECL X11DRV_CreateDIBSection( PHYSDEV dev, HBITMAP hbitmap,
4757 const BITMAPINFO *bmi, UINT usage )
4759 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
4760 X_PHYSBITMAP *physBitmap;
4764 #ifdef HAVE_LIBXXSHM
4768 DIB_GetBitmapInfo( &bmi->bmiHeader, &w, &h, &bpp, &compr );
4770 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return 0;
4771 if (h < 0) physBitmap->topdown = TRUE;
4772 physBitmap->status = DIB_Status_None;
4774 GetObjectW( hbitmap, sizeof(dib), &dib );
4776 /* create color map */
4777 if (dib.dsBm.bmBitsPixel <= 8)
4779 physBitmap->colorMap = X11DRV_DIB_BuildColorMap( physDev,
4780 usage, dib.dsBm.bmBitsPixel, bmi,
4781 &physBitmap->nColorMap );
4784 if (!X11DRV_XRender_SetPhysBitmapDepth( physBitmap, dib.dsBm.bmBitsPixel, &dib ))
4786 if (dib.dsBm.bmBitsPixel == 1)
4788 physBitmap->pixmap_depth = 1;
4789 physBitmap->trueColor = FALSE;
4793 physBitmap->pixmap_depth = screen_depth;
4794 physBitmap->pixmap_color_shifts = X11DRV_PALETTE_default_shifts;
4795 physBitmap->trueColor = (visual->class == TrueColor || visual->class == DirectColor);
4799 /* create pixmap and X image */
4801 #ifdef HAVE_LIBXXSHM
4802 physBitmap->shminfo.shmid = -1;
4804 if (X11DRV_DIB_QueryXShm( &pixmaps )
4805 && (physBitmap->image = X11DRV_XShmCreateImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4806 physBitmap->pixmap_depth, &physBitmap->shminfo )))
4810 physBitmap->shm_mode = X11DRV_SHM_PIXMAP;
4811 physBitmap->image->data = HeapAlloc( GetProcessHeap(), 0,
4812 dib.dsBm.bmHeight * physBitmap->image->bytes_per_line );
4816 physBitmap->shm_mode = X11DRV_SHM_IMAGE;
4817 physBitmap->image->data = physBitmap->shminfo.shmaddr;
4823 physBitmap->shm_mode = X11DRV_SHM_NONE;
4824 physBitmap->image = X11DRV_DIB_CreateXImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4825 physBitmap->pixmap_depth );
4828 #ifdef HAVE_LIBXXSHM
4829 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP)
4831 TRACE("Creating shared pixmap for bmp %p.\n", physBitmap->hbitmap);
4832 physBitmap->pixmap = XShmCreatePixmap( gdi_display, root_window,
4833 physBitmap->shminfo.shmaddr, &physBitmap->shminfo,
4834 dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4835 physBitmap->pixmap_depth );
4840 physBitmap->pixmap = XCreatePixmap( gdi_display, root_window, dib.dsBm.bmWidth,
4841 dib.dsBm.bmHeight, physBitmap->pixmap_depth );
4844 wine_tsx11_unlock();
4845 if (!physBitmap->pixmap || !physBitmap->image) return 0;
4847 if (physBitmap->trueColor)
4849 ColorShifts *shifts = &physBitmap->pixmap_color_shifts;
4851 /* When XRender is around and used, we also support dibsections in other formats like 16-bit. In these
4852 * cases we need to override the mask of XImages. The reason is that during XImage creation the masks are
4853 * derived from a 24-bit visual (no 16-bit ones are around when X runs at 24-bit). SetImageBits and other
4854 * functions rely on the color masks for proper color conversion, so we need to override the masks here. */
4855 physBitmap->image->red_mask = shifts->physicalRed.max << shifts->physicalRed.shift;
4856 physBitmap->image->green_mask = shifts->physicalGreen.max << shifts->physicalGreen.shift;
4857 physBitmap->image->blue_mask = shifts->physicalBlue.max << shifts->physicalBlue.shift;
4860 /* install fault handler */
4861 InitializeCriticalSection( &physBitmap->lock );
4862 physBitmap->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": X_PHYSBITMAP.lock");
4864 physBitmap->base = dib.dsBm.bmBits;
4865 physBitmap->size = dib.dsBmih.biSizeImage;
4866 physBitmap->status = DIB_Status_AppMod;
4869 dibs_handler = AddVectoredExceptionHandler( TRUE, X11DRV_DIB_FaultHandler );
4870 EnterCriticalSection( &dibs_cs );
4871 list_add_head( &dibs_list, &physBitmap->entry );
4872 LeaveCriticalSection( &dibs_cs );
4874 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4879 /***********************************************************************
4880 * X11DRV_DIB_DeleteDIBSection
4882 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP *physBitmap, DIBSECTION *dib)
4886 EnterCriticalSection( &dibs_cs );
4887 list_remove( &physBitmap->entry );
4888 last = list_empty( &dibs_list );
4889 LeaveCriticalSection( &dibs_cs );
4893 RemoveVectoredExceptionHandler( dibs_handler );
4894 dibs_handler = NULL;
4897 if (dib->dshSection)
4898 X11DRV_DIB_Coerce(physBitmap, DIB_Status_InSync);
4900 if (physBitmap->image)
4903 #ifdef HAVE_LIBXXSHM
4904 if (physBitmap->shminfo.shmid != -1)
4906 XShmDetach( gdi_display, &(physBitmap->shminfo) );
4907 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP) X11DRV_DIB_DestroyXImage( physBitmap->image );
4908 else XDestroyImage( physBitmap->image );
4909 shmdt( physBitmap->shminfo.shmaddr );
4910 physBitmap->shminfo.shmid = -1;
4911 physBitmap->shm_mode = X11DRV_SHM_NONE;
4915 X11DRV_DIB_DestroyXImage( physBitmap->image );
4916 wine_tsx11_unlock();
4919 HeapFree(GetProcessHeap(), 0, physBitmap->colorMap);
4920 physBitmap->lock.DebugInfo->Spare[0] = 0;
4921 DeleteCriticalSection(&physBitmap->lock);
4924 /***********************************************************************
4925 * SetDIBColorTable (X11DRV.@)
4927 UINT CDECL X11DRV_SetDIBColorTable( PHYSDEV dev, UINT start, UINT count, const RGBQUAD *colors )
4929 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
4932 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4934 if (!physBitmap) return 0;
4935 GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib );
4937 if (physBitmap->colorMap && start < physBitmap->nColorMap) {
4938 UINT end = count + start;
4939 if (end > physBitmap->nColorMap) end = physBitmap->nColorMap;
4941 * Changing color table might change the mapping between
4942 * DIB colors and X11 colors and thus alter the visible state
4943 * of the bitmap object.
4946 * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4947 * at least for a 1 bpp dibsection
4949 X11DRV_DIB_Lock( physBitmap, DIB_Status_AppMod );
4950 X11DRV_DIB_GenColorMap( physDev, physBitmap->colorMap, DIB_RGB_COLORS,
4951 dib.dsBm.bmBitsPixel,
4952 TRUE, colors, start, end );
4953 X11DRV_DIB_Unlock( physBitmap, TRUE );
4960 /***********************************************************************
4961 * X11DRV_DIB_CreateDIBFromBitmap
4963 * Allocates a packed DIB and copies the bitmap data into it.
4965 HGLOBAL X11DRV_DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
4970 LPBITMAPINFOHEADER pbmiHeader;
4971 unsigned int cDataSize, cPackedSize, OffsetBits;
4974 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
4977 * A packed DIB contains a BITMAPINFO structure followed immediately by
4978 * an optional color palette and the pixel data.
4981 /* Calculate the size of the packed DIB */
4982 cDataSize = X11DRV_DIB_GetDIBWidthBytes( bmp.bmWidth, bmp.bmBitsPixel ) * abs( bmp.bmHeight );
4983 cPackedSize = sizeof(BITMAPINFOHEADER)
4984 + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
4986 /* Get the offset to the bits */
4987 OffsetBits = cPackedSize - cDataSize;
4989 /* Allocate the packed DIB */
4990 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
4991 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
4995 WARN("Could not allocate packed DIB!\n");
4999 /* A packed DIB starts with a BITMAPINFOHEADER */
5000 pPackedDIB = GlobalLock(hPackedDIB);
5001 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
5003 /* Init the BITMAPINFOHEADER */
5004 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
5005 pbmiHeader->biWidth = bmp.bmWidth;
5006 pbmiHeader->biHeight = bmp.bmHeight;
5007 pbmiHeader->biPlanes = 1;
5008 pbmiHeader->biBitCount = bmp.bmBitsPixel;
5009 pbmiHeader->biCompression = BI_RGB;
5010 pbmiHeader->biSizeImage = 0;
5011 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
5012 pbmiHeader->biClrUsed = 0;
5013 pbmiHeader->biClrImportant = 0;
5015 /* Retrieve the DIB bits from the bitmap and fill in the
5016 * DIB color table if present */
5018 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
5019 hBmp, /* Handle to bitmap */
5020 0, /* First scan line to set in dest bitmap */
5021 bmp.bmHeight, /* Number of scan lines to copy */
5022 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
5023 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
5024 0); /* RGB or palette index */
5025 GlobalUnlock(hPackedDIB);
5027 /* Cleanup if GetDIBits failed */
5028 if (nLinesCopied != bmp.bmHeight)
5030 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
5031 GlobalFree(hPackedDIB);
5038 /**************************************************************************
5039 * X11DRV_DIB_CreateDIBFromPixmap
5041 * Allocates a packed DIB and copies the Pixmap data into it.
5042 * The Pixmap passed in is deleted after the conversion.
5044 HGLOBAL X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap, HDC hdc)
5047 X_PHYSBITMAP *physBitmap;
5050 HGLOBAL hPackedDIB = 0;
5052 int x,y; /* Unused */
5053 unsigned border_width; /* Unused */
5054 unsigned int depth, width, height;
5056 /* Get the Pixmap dimensions and bit depth */
5058 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
5059 &border_width, &depth)) depth = 0;
5060 wine_tsx11_unlock();
5061 if (!pixmap_formats[depth]) return 0;
5063 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
5064 width, height, depth);
5067 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
5068 * and make it a container for the pixmap passed.
5070 if (!(hBmp = CreateBitmap( width, height, 1, pixmap_formats[depth]->bits_per_pixel, NULL ))) return 0;
5072 /* force bitmap to be owned by a screen DC */
5073 hdcMem = CreateCompatibleDC( hdc );
5074 SelectObject( hdcMem, SelectObject( hdcMem, hBmp ));
5077 physBitmap = X11DRV_get_phys_bitmap( hBmp );
5079 /* swap the new pixmap in */
5080 orig_pixmap = physBitmap->pixmap;
5081 physBitmap->pixmap = pixmap;
5084 * Create a packed DIB from the Pixmap wrapper bitmap created above.
5085 * A packed DIB contains a BITMAPINFO structure followed immediately by
5086 * an optional color palette and the pixel data.
5088 hPackedDIB = X11DRV_DIB_CreateDIBFromBitmap(hdc, hBmp);
5090 /* we can now get rid of the HBITMAP and its original pixmap */
5091 physBitmap->pixmap = orig_pixmap;
5094 TRACE("\tReturning packed DIB %p\n", hPackedDIB);
5099 /**************************************************************************
5100 * X11DRV_DIB_CreatePixmapFromDIB
5102 * Creates a Pixmap from a packed DIB
5104 Pixmap X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB, HDC hdc )
5107 X_PHYSBITMAP *physBitmap;
5111 /* Create a DDB from the DIB */
5113 pbmi = GlobalLock(hPackedDIB);
5114 hBmp = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT,
5115 (LPBYTE)pbmi + bitmap_info_size( pbmi, DIB_RGB_COLORS ),
5116 pbmi, DIB_RGB_COLORS);
5117 GlobalUnlock(hPackedDIB);
5119 /* clear the physBitmap so that we can steal its pixmap */
5120 physBitmap = X11DRV_get_phys_bitmap( hBmp );
5121 pixmap = physBitmap->pixmap;
5122 physBitmap->pixmap = 0;
5124 /* Delete the DDB we created earlier now that we have stolen its pixmap */
5127 TRACE("Returning Pixmap %lx\n", pixmap);