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( X11DRV_PDEVICE *physDev, 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_DIB_IMAGEBITS_DESCR descr;
3855 int rop = X11DRV_XROPfunction[GetROP2(physDev->hdc) - 1];
3857 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3858 &descr.infoBpp, &descr.compression ) == -1)
3861 top_down = (height < 0);
3862 if (top_down) height = -height;
3866 LPtoDP(physDev->hdc, &pt, 1);
3868 if (!lines || (startscan >= height)) return 0;
3869 if (!top_down && startscan + lines > height) lines = height - startscan;
3871 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3872 * and clamp all values to fit inside [startscan,startscan+lines]
3874 if (ySrc + cy <= startscan + lines)
3876 UINT y = startscan + lines - (ySrc + cy);
3877 if (ySrc < startscan) cy -= (startscan - ySrc);
3880 /* avoid getting unnecessary lines */
3882 if (y >= lines) return 0;
3887 if (y >= lines) return lines;
3888 ySrc = y; /* need to get all lines in top down mode */
3893 if (ySrc >= startscan + lines) return lines;
3894 pt.y += ySrc + cy - (startscan + lines);
3895 cy = startscan + lines - ySrc;
3897 if (cy > lines) cy = lines;
3899 if (xSrc >= width) return lines;
3900 if (xSrc + cx >= width) cx = width - xSrc;
3901 if (!cx || !cy) return lines;
3903 /* Update the pixmap from the DIB section */
3904 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
3906 X11DRV_SetupGCForText( physDev ); /* To have the correct colors */
3908 XSetFunction(gdi_display, physDev->gc, rop);
3909 wine_tsx11_unlock();
3911 switch (descr.infoBpp)
3916 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3918 physDev->depth, info, &descr.nColorMap );
3919 if (!descr.colorMap) return 0;
3920 descr.rMask = descr.gMask = descr.bMask = 0;
3924 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3925 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3926 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3932 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3933 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3934 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3939 descr.physDev = physDev;
3942 descr.palentry = NULL;
3943 descr.lines = top_down ? -lines : lines;
3944 descr.infoWidth = width;
3945 descr.depth = physDev->depth;
3946 descr.shifts = physDev->color_shifts;
3947 descr.drawable = physDev->drawable;
3948 descr.gc = physDev->gc;
3951 descr.xDest = physDev->dc_rect.left + pt.x;
3952 descr.yDest = physDev->dc_rect.top + pt.y;
3955 descr.shm_mode = X11DRV_SHM_NONE;
3956 descr.dibpitch = ((width * descr.infoBpp + 31) &~31) / 8;
3957 descr.physBitmap = NULL;
3959 result = X11DRV_DIB_SetImageBits( &descr );
3961 if (descr.infoBpp <= 8)
3962 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3964 /* Update the DIBSection of the pixmap */
3965 X11DRV_UnlockDIBSection(physDev, TRUE);
3970 /***********************************************************************
3971 * SetDIBits (X11DRV.@)
3973 INT CDECL X11DRV_SetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan,
3974 UINT lines, LPCVOID bits, const BITMAPINFO *info, UINT coloruse )
3976 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3977 X11DRV_DIB_IMAGEBITS_DESCR descr;
3979 LONG width, height, tmpheight;
3982 descr.physDev = physDev;
3984 if (!physBitmap) return 0;
3986 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3987 &descr.infoBpp, &descr.compression ) == -1)
3991 if (height < 0) height = -height;
3992 if (!lines || (startscan >= height))
3995 if (!GetObjectW( hbitmap, sizeof(ds), &ds )) return 0;
3997 if (startscan + lines > height) lines = height - startscan;
3999 switch (descr.infoBpp)
4004 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
4005 descr.physDev, coloruse,
4006 physBitmap->pixmap_depth,
4007 info, &descr.nColorMap );
4008 if (!descr.colorMap) return 0;
4009 descr.rMask = descr.gMask = descr.bMask = 0;
4013 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4014 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4015 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4021 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4022 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4023 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4032 descr.palentry = NULL;
4033 descr.infoWidth = width;
4034 descr.lines = tmpheight >= 0 ? lines : -lines;
4035 descr.depth = physBitmap->pixmap_depth;
4036 descr.shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
4037 descr.drawable = physBitmap->pixmap;
4038 descr.gc = get_bitmap_gc(physBitmap->pixmap_depth);
4042 descr.yDest = height - startscan - lines;
4043 descr.width = ds.dsBm.bmWidth;
4044 descr.height = lines;
4045 descr.shm_mode = X11DRV_SHM_NONE;
4046 descr.dibpitch = ((descr.infoWidth * descr.infoBpp + 31) &~31) / 8;
4047 descr.physBitmap = NULL;
4048 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
4049 result = X11DRV_DIB_SetImageBits( &descr );
4051 /* optimisation for the case where the input bits are in exactly the same
4052 * format as the internal representation and copying to the app bits is
4053 * cheap - saves a round trip to the X server */
4054 if (descr.compression == BI_RGB &&
4055 coloruse == DIB_RGB_COLORS &&
4056 descr.infoBpp == ds.dsBm.bmBitsPixel &&
4057 physBitmap->base && physBitmap->size < 65536)
4059 unsigned int srcwidthb = X11DRV_DIB_GetDIBWidthBytes( width, descr.infoBpp );
4060 int dstwidthb = ds.dsBm.bmWidthBytes;
4061 LPBYTE dbits = physBitmap->base + startscan * dstwidthb;
4062 const BYTE *sbits = bits;
4066 TRACE("syncing compatible set bits to app bits\n");
4067 if ((tmpheight < 0) ^ physBitmap->topdown)
4069 dbits += dstwidthb * (lines-1);
4070 dstwidthb = -dstwidthb;
4072 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4073 widthb = min(srcwidthb, abs(dstwidthb));
4074 for (y = 0; y < lines; y++, dbits += dstwidthb, sbits += srcwidthb)
4075 memcpy(dbits, sbits, widthb);
4076 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4077 physBitmap->status = DIB_Status_InSync;
4079 X11DRV_DIB_Unlock( physBitmap, TRUE );
4081 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4086 /***********************************************************************
4087 * GetDIBits (X11DRV.@)
4089 INT CDECL X11DRV_GetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan, UINT lines,
4090 LPVOID bits, BITMAPINFO *info, UINT coloruse )
4092 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
4094 X11DRV_DIB_IMAGEBITS_DESCR descr;
4095 PALETTEENTRY palette[256];
4098 LONG width, tempHeight;
4102 static const PALETTEENTRY peBlack = {0,0,0,0};
4103 static const PALETTEENTRY peWhite = {255,255,255,0};
4105 if (!physBitmap) return 0;
4106 if (!(obj_size = GetObjectW( hbitmap, sizeof(dib), &dib ))) return 0;
4108 bitmap_type = DIB_GetBitmapInfo( (BITMAPINFOHEADER*)info, &width, &tempHeight, &descr.infoBpp, &descr.compression);
4109 if (bitmap_type == -1)
4111 ERR("Invalid bitmap\n");
4115 if (physBitmap->pixmap_depth > 1)
4117 GetPaletteEntries( GetCurrentObject( physDev->hdc, OBJ_PAL ), 0, 256, palette );
4121 palette[0] = peBlack;
4122 palette[1] = peWhite;
4125 descr.lines = tempHeight;
4126 core_header = (bitmap_type == 0);
4127 colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
4129 TRACE("%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
4130 lines, dib.dsBm.bmWidth, dib.dsBm.bmHeight, width, descr.lines, startscan);
4132 if( lines > dib.dsBm.bmHeight ) lines = dib.dsBm.bmHeight;
4134 height = descr.lines;
4135 if (height < 0) height = -height;
4136 if( lines > height ) lines = height;
4137 /* Top-down images have a negative biHeight, the scanlines of these images
4138 * were inverted in X11DRV_DIB_GetImageBits_xx
4139 * To prevent this we simply change the sign of lines
4140 * (the number of scan lines to copy).
4141 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4143 if( descr.lines < 0 && lines > 0) lines = -lines;
4145 if( startscan >= dib.dsBm.bmHeight ) return 0;
4147 descr.colorMap = NULL;
4149 switch (descr.infoBpp)
4154 descr.rMask= descr.gMask = descr.bMask = 0;
4155 if(coloruse == DIB_RGB_COLORS)
4156 descr.colorMap = colorPtr;
4158 int num_colors = 1 << descr.infoBpp, i;
4161 WORD *index = colorPtr;
4162 descr.colorMap = rgb = HeapAlloc(GetProcessHeap(), 0, num_colors * sizeof(RGBQUAD));
4163 for(i = 0; i < num_colors; i++, rgb++, index++) {
4164 colref = X11DRV_PALETTE_ToLogical(physDev, X11DRV_PALETTE_ToPhysical(physDev, PALETTEINDEX(*index)));
4165 rgb->rgbRed = GetRValue(colref);
4166 rgb->rgbGreen = GetGValue(colref);
4167 rgb->rgbBlue = GetBValue(colref);
4168 rgb->rgbReserved = 0;
4174 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4175 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4176 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4180 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4181 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4182 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4186 descr.physDev = physDev;
4187 descr.palentry = palette;
4189 descr.image = physBitmap->image;
4190 descr.infoWidth = width;
4191 descr.lines = lines;
4192 descr.depth = physBitmap->pixmap_depth;
4193 descr.shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
4194 descr.drawable = physBitmap->pixmap;
4195 descr.gc = get_bitmap_gc(physBitmap->pixmap_depth);
4196 descr.width = dib.dsBm.bmWidth;
4197 descr.height = dib.dsBm.bmHeight;
4201 descr.sizeImage = core_header ? 0 : info->bmiHeader.biSizeImage;
4202 descr.physBitmap = physBitmap;
4204 if (descr.lines > 0)
4206 descr.ySrc = (descr.height-1) - (startscan + (lines-1));
4210 descr.ySrc = startscan;
4212 descr.shm_mode = physBitmap->shm_mode;
4213 descr.dibpitch = (obj_size == sizeof(DIBSECTION)) ? dib.dsBm.bmWidthBytes
4214 : (((descr.infoWidth * descr.infoBpp + 31) &~31) / 8);
4216 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
4217 X11DRV_DIB_GetImageBits( &descr );
4218 X11DRV_DIB_Unlock( physBitmap, TRUE );
4220 if(!core_header && info->bmiHeader.biSizeImage == 0) /* Fill in biSizeImage */
4221 info->bmiHeader.biSizeImage = X11DRV_DIB_GetDIBImageBytes( descr.infoWidth,
4225 if (descr.compression == BI_BITFIELDS)
4227 *(DWORD *)info->bmiColors = descr.rMask;
4228 *((DWORD *)info->bmiColors + 1) = descr.gMask;
4229 *((DWORD *)info->bmiColors + 2) = descr.bMask;
4231 else if (!core_header)
4233 /* if RLE or JPEG compression were supported,
4234 * this line would be invalid. */
4235 info->bmiHeader.biCompression = 0;
4238 if(descr.colorMap != colorPtr)
4239 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4243 /***********************************************************************
4244 * X11DRV_DIB_DoCopyDIBSection
4246 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB,
4247 void *colorMap, int nColorMap,
4248 Drawable dest, GC gc,
4249 DWORD xSrc, DWORD ySrc,
4250 DWORD xDest, DWORD yDest,
4251 DWORD width, DWORD height)
4253 DIBSECTION dibSection;
4254 X11DRV_DIB_IMAGEBITS_DESCR descr;
4255 int identity[2] = {0,1};
4257 if (!GetObjectW( physBitmap->hbitmap, sizeof(dibSection), &dibSection )) return;
4259 descr.physDev = NULL;
4260 descr.palentry = NULL;
4261 descr.infoWidth = dibSection.dsBmih.biWidth;
4262 descr.infoBpp = dibSection.dsBmih.biBitCount;
4263 descr.lines = physBitmap->topdown ? -dibSection.dsBmih.biHeight : dibSection.dsBmih.biHeight;
4264 descr.image = physBitmap->image;
4265 descr.colorMap = colorMap;
4266 descr.nColorMap = nColorMap;
4267 descr.bits = dibSection.dsBm.bmBits;
4268 descr.depth = physBitmap->pixmap_depth;
4269 descr.shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
4270 descr.compression = dibSection.dsBmih.biCompression;
4271 descr.physBitmap = physBitmap;
4273 if(descr.infoBpp == 1)
4274 descr.colorMap = (void*)identity;
4276 switch (descr.infoBpp)
4281 descr.rMask = descr.gMask = descr.bMask = 0;
4285 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0x7c00;
4286 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x03e0;
4287 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x001f;
4292 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0xff0000;
4293 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x00ff00;
4294 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x0000ff;
4299 descr.drawable = dest;
4303 descr.xDest = xDest;
4304 descr.yDest = yDest;
4305 descr.width = width;
4306 descr.height = height;
4307 descr.sizeImage = 0;
4309 descr.shm_mode = physBitmap->shm_mode;
4310 #ifdef HAVE_LIBXXSHM
4311 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP && physBitmap->pixmap != dest)
4313 descr.shm_mode = X11DRV_SHM_NONE;
4316 descr.dibpitch = dibSection.dsBm.bmWidthBytes;
4320 TRACE("Copying from Pixmap to DIB bits\n");
4321 X11DRV_DIB_GetImageBits( &descr );
4325 TRACE("Copying from DIB bits to Pixmap\n");
4326 X11DRV_DIB_SetImageBits( &descr );
4330 /***********************************************************************
4331 * X11DRV_DIB_CopyDIBSection
4333 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
4334 DWORD xSrc, DWORD ySrc, DWORD xDest, DWORD yDest,
4335 DWORD width, DWORD height)
4338 X_PHYSBITMAP *physBitmap;
4339 unsigned int nColorMap;
4343 TRACE("(%p,%p,%d,%d,%d,%d,%d,%d)\n", physDevSrc->hdc, physDevDst->hdc,
4344 xSrc, ySrc, xDest, yDest, width, height);
4345 /* this function is meant as an optimization for BitBlt,
4346 * not to be called otherwise */
4347 physBitmap = physDevSrc->bitmap;
4348 if (!physBitmap || GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib ) != sizeof(dib))
4350 ERR("called for non-DIBSection!?\n");
4353 /* while BitBlt should already have made sure we only get
4354 * positive values, we should check for oversize values */
4355 if ((xSrc < dib.dsBm.bmWidth) &&
4356 (ySrc < dib.dsBm.bmHeight)) {
4357 if (xSrc + width > dib.dsBm.bmWidth)
4358 width = dib.dsBm.bmWidth - xSrc;
4359 if (ySrc + height > dib.dsBm.bmHeight)
4360 height = dib.dsBm.bmHeight - ySrc;
4361 /* if the source bitmap is 8bpp or less, we're supposed to use the
4362 * DC's palette for color conversion (not the DIB color table) */
4363 if (dib.dsBm.bmBitsPixel <= 8) {
4364 HPALETTE hPalette = GetCurrentObject( physDevSrc->hdc, OBJ_PAL );
4365 if (!hPalette || (hPalette == GetStockObject(DEFAULT_PALETTE))) {
4366 /* HACK: no palette has been set in the source DC,
4367 * use the DIB colormap instead - this is necessary in some
4368 * cases since we need to do depth conversion in some places
4369 * where real Windows can just copy data straight over */
4370 x11ColorMap = physBitmap->colorMap;
4371 nColorMap = physBitmap->nColorMap;
4372 freeColorMap = FALSE;
4374 const BITMAPINFO* info = (BITMAPINFO*)&dib.dsBmih;
4377 nColorMap = X11DRV_DIB_GetColorCount(info);
4378 x11ColorMap = HeapAlloc(GetProcessHeap(), 0, nColorMap * sizeof(int));
4379 for (i = 0; i < nColorMap; i++)
4380 x11ColorMap[i] = X11DRV_PALETTE_ToPhysical(physDevSrc, PALETTEINDEX(i));
4381 freeColorMap = TRUE;
4388 freeColorMap = FALSE;
4390 /* perform the copy */
4391 X11DRV_DIB_DoCopyDIBSection(physBitmap, FALSE, x11ColorMap, nColorMap,
4392 physDevDst->drawable, physDevDst->gc, xSrc, ySrc,
4393 physDevDst->dc_rect.left + xDest, physDevDst->dc_rect.top + yDest,
4395 /* free color mapping */
4397 HeapFree(GetProcessHeap(), 0, x11ColorMap);
4401 /***********************************************************************
4402 * X11DRV_DIB_DoUpdateDIBSection
4404 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB)
4408 GetObjectW( physBitmap->hbitmap, sizeof(bitmap), &bitmap );
4409 X11DRV_DIB_DoCopyDIBSection(physBitmap, toDIB,
4410 physBitmap->colorMap, physBitmap->nColorMap,
4411 physBitmap->pixmap, get_bitmap_gc(physBitmap->pixmap_depth),
4412 0, 0, 0, 0, bitmap.bmWidth, bitmap.bmHeight);
4415 /***********************************************************************
4416 * X11DRV_DIB_FaultHandler
4418 static LONG CALLBACK X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep )
4420 X_PHYSBITMAP *physBitmap = NULL;
4424 const size_t pagemask = getpagesize() - 1;
4426 if (ep->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
4427 return EXCEPTION_CONTINUE_SEARCH;
4429 addr = (BYTE *)ep->ExceptionRecord->ExceptionInformation[1];
4431 EnterCriticalSection(&dibs_cs);
4432 LIST_FOR_EACH( ptr, &dibs_list )
4434 physBitmap = LIST_ENTRY( ptr, X_PHYSBITMAP, entry );
4435 if ((physBitmap->base <= addr) &&
4436 (addr < physBitmap->base + ((physBitmap->size + pagemask) & ~pagemask)))
4442 LeaveCriticalSection(&dibs_cs);
4444 if (!found) return EXCEPTION_CONTINUE_SEARCH;
4446 if (addr >= physBitmap->base + physBitmap->size)
4447 WARN( "%p: access to %p beyond the end of the DIB\n", physBitmap->hbitmap, addr );
4449 X11DRV_DIB_Lock( physBitmap, DIB_Status_None );
4450 if (ep->ExceptionRecord->ExceptionInformation[0] == EXCEPTION_WRITE_FAULT) {
4451 /* the app tried to write the DIB bits */
4452 X11DRV_DIB_Coerce( physBitmap, DIB_Status_AppMod);
4454 /* the app tried to read the DIB bits */
4455 X11DRV_DIB_Coerce( physBitmap, DIB_Status_InSync);
4457 X11DRV_DIB_Unlock( physBitmap, TRUE );
4459 return EXCEPTION_CONTINUE_EXECUTION;
4462 /***********************************************************************
4465 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *physBitmap, INT req)
4467 INT ret = DIB_Status_None;
4469 if (!physBitmap->image) return ret; /* not a DIB section */
4470 EnterCriticalSection(&physBitmap->lock);
4471 ret = physBitmap->status;
4473 case DIB_Status_GdiMod:
4474 /* GDI access - request to draw on pixmap */
4475 switch (physBitmap->status)
4478 case DIB_Status_None:
4479 physBitmap->p_status = DIB_Status_GdiMod;
4480 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4483 case DIB_Status_GdiMod:
4484 TRACE("GdiMod requested in status GdiMod\n" );
4485 physBitmap->p_status = DIB_Status_GdiMod;
4488 case DIB_Status_InSync:
4489 TRACE("GdiMod requested in status InSync\n" );
4490 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4491 physBitmap->status = DIB_Status_GdiMod;
4492 physBitmap->p_status = DIB_Status_InSync;
4495 case DIB_Status_AppMod:
4496 TRACE("GdiMod requested in status AppMod\n" );
4497 /* make it readonly to avoid app changing data while we copy */
4498 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4499 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4500 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4501 physBitmap->p_status = DIB_Status_AppMod;
4502 physBitmap->status = DIB_Status_GdiMod;
4507 case DIB_Status_InSync:
4508 /* App access - request access to read DIB surface */
4509 /* (typically called from signal handler) */
4510 switch (physBitmap->status)
4513 case DIB_Status_None:
4514 /* shouldn't happen from signal handler */
4517 case DIB_Status_GdiMod:
4518 TRACE("InSync requested in status GdiMod\n" );
4519 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4520 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4521 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4522 physBitmap->status = DIB_Status_InSync;
4525 case DIB_Status_InSync:
4526 TRACE("InSync requested in status InSync\n" );
4527 /* shouldn't happen from signal handler */
4530 case DIB_Status_AppMod:
4531 TRACE("InSync requested in status AppMod\n" );
4532 /* no reason to do anything here, and this
4533 * shouldn't happen from signal handler */
4538 case DIB_Status_AppMod:
4539 /* App access - request access to write DIB surface */
4540 /* (typically called from signal handler) */
4541 switch (physBitmap->status)
4544 case DIB_Status_None:
4545 /* shouldn't happen from signal handler */
4548 case DIB_Status_GdiMod:
4549 TRACE("AppMod requested in status GdiMod\n" );
4550 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4551 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4552 physBitmap->status = DIB_Status_AppMod;
4555 case DIB_Status_InSync:
4556 TRACE("AppMod requested in status InSync\n" );
4557 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4558 physBitmap->status = DIB_Status_AppMod;
4561 case DIB_Status_AppMod:
4562 TRACE("AppMod requested in status AppMod\n" );
4563 /* shouldn't happen from signal handler */
4568 /* it is up to the caller to do the copy/conversion, probably
4569 * using the return value to decide where to copy from */
4571 LeaveCriticalSection(&physBitmap->lock);
4575 /***********************************************************************
4578 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *physBitmap, INT req)
4580 INT ret = DIB_Status_None;
4582 if (!physBitmap->image) return ret; /* not a DIB section */
4583 TRACE("Locking %p from thread %04x\n", physBitmap->hbitmap, GetCurrentThreadId());
4584 EnterCriticalSection(&physBitmap->lock);
4585 ret = physBitmap->status;
4586 if (req != DIB_Status_None)
4587 X11DRV_DIB_Coerce(physBitmap, req);
4591 /***********************************************************************
4594 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *physBitmap, BOOL commit)
4596 if (!physBitmap->image) return; /* not a DIB section */
4597 switch (physBitmap->status)
4600 case DIB_Status_None:
4601 /* in case anyone is wondering, this is the "signal handler doesn't
4602 * work" case, where we always have to be ready for app access */
4604 switch (physBitmap->p_status)
4606 case DIB_Status_GdiMod:
4607 TRACE("Unlocking and syncing from GdiMod\n" );
4608 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4612 TRACE("Unlocking without needing to sync\n" );
4616 else TRACE("Unlocking with no changes\n");
4617 physBitmap->p_status = DIB_Status_None;
4620 case DIB_Status_GdiMod:
4621 TRACE("Unlocking in status GdiMod\n" );
4622 /* DIB was protected in Coerce */
4624 /* no commit, revert to InSync if applicable */
4625 if ((physBitmap->p_status == DIB_Status_InSync) ||
4626 (physBitmap->p_status == DIB_Status_AppMod)) {
4627 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4628 physBitmap->status = DIB_Status_InSync;
4633 case DIB_Status_InSync:
4634 TRACE("Unlocking in status InSync\n" );
4635 /* DIB was already protected in Coerce */
4638 case DIB_Status_AppMod:
4639 TRACE("Unlocking in status AppMod\n" );
4640 /* DIB was already protected in Coerce */
4641 /* this case is ordinary only called from the signal handler,
4642 * so we don't bother to check for !commit */
4645 LeaveCriticalSection(&physBitmap->lock);
4646 TRACE("Unlocked %p\n", physBitmap->hbitmap);
4649 /***********************************************************************
4650 * X11DRV_CoerceDIBSection
4652 INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req)
4654 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4655 return X11DRV_DIB_Coerce(physDev->bitmap, req);
4658 /***********************************************************************
4659 * X11DRV_LockDIBSection
4661 INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req)
4663 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4664 return X11DRV_DIB_Lock(physDev->bitmap, req);
4667 /***********************************************************************
4668 * X11DRV_UnlockDIBSection
4670 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
4672 if (!physDev || !physDev->bitmap) return;
4673 X11DRV_DIB_Unlock(physDev->bitmap, commit);
4677 #ifdef HAVE_LIBXXSHM
4678 /***********************************************************************
4679 * X11DRV_XShmErrorHandler
4682 static int XShmErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
4684 return 1; /* FIXME: should check event contents */
4687 /***********************************************************************
4688 * X11DRV_XShmCreateImage
4691 static XImage *X11DRV_XShmCreateImage( int width, int height, int bpp,
4692 XShmSegmentInfo* shminfo)
4696 image = XShmCreateImage(gdi_display, visual, bpp, ZPixmap, NULL, shminfo, width, height);
4699 shminfo->shmid = shmget(IPC_PRIVATE, image->bytes_per_line * height,
4701 if( shminfo->shmid != -1 )
4703 shminfo->shmaddr = shmat( shminfo->shmid, 0, 0 );
4704 if( shminfo->shmaddr != (char*)-1 )
4708 shminfo->readOnly = FALSE;
4709 X11DRV_expect_error( gdi_display, XShmErrorHandler, NULL );
4710 ok = (XShmAttach( gdi_display, shminfo ) != 0);
4711 XSync( gdi_display, False );
4712 if (X11DRV_check_error()) ok = FALSE;
4715 shmctl(shminfo->shmid, IPC_RMID, 0);
4716 return image; /* Success! */
4718 /* An error occurred */
4719 shmdt(shminfo->shmaddr);
4721 shmctl(shminfo->shmid, IPC_RMID, 0);
4722 shminfo->shmid = -1;
4724 XFlush(gdi_display);
4725 XDestroyImage(image);
4730 #endif /* HAVE_LIBXXSHM */
4733 /***********************************************************************
4734 * X11DRV_CreateDIBSection (X11DRV.@)
4736 HBITMAP CDECL X11DRV_CreateDIBSection( X11DRV_PDEVICE *physDev, HBITMAP hbitmap,
4737 const BITMAPINFO *bmi, UINT usage )
4739 X_PHYSBITMAP *physBitmap;
4743 #ifdef HAVE_LIBXXSHM
4748 DIB_GetBitmapInfo( &bmi->bmiHeader, &w, &h, &bpp, &compr );
4750 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return 0;
4751 if (h < 0) physBitmap->topdown = TRUE;
4752 physBitmap->status = DIB_Status_None;
4754 GetObjectW( hbitmap, sizeof(dib), &dib );
4756 /* create color map */
4757 if (dib.dsBm.bmBitsPixel <= 8)
4759 physBitmap->colorMap = X11DRV_DIB_BuildColorMap( physDev,
4760 usage, dib.dsBm.bmBitsPixel, bmi,
4761 &physBitmap->nColorMap );
4764 if (!X11DRV_XRender_SetPhysBitmapDepth( physBitmap, dib.dsBm.bmBitsPixel, &dib ))
4766 if (dib.dsBm.bmBitsPixel == 1)
4768 physBitmap->pixmap_depth = 1;
4769 physBitmap->trueColor = FALSE;
4773 physBitmap->pixmap_depth = screen_depth;
4774 physBitmap->pixmap_color_shifts = X11DRV_PALETTE_default_shifts;
4775 physBitmap->trueColor = (visual->class == TrueColor || visual->class == DirectColor);
4779 /* create pixmap and X image */
4781 #ifdef HAVE_LIBXXSHM
4782 physBitmap->shminfo.shmid = -1;
4784 if (XShmQueryVersion( gdi_display, &major, &minor, &pixmaps )
4785 && (physBitmap->image = X11DRV_XShmCreateImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4786 physBitmap->pixmap_depth, &physBitmap->shminfo )))
4790 physBitmap->shm_mode = X11DRV_SHM_PIXMAP;
4791 physBitmap->image->data = HeapAlloc( GetProcessHeap(), 0,
4792 dib.dsBm.bmHeight * physBitmap->image->bytes_per_line );
4796 physBitmap->shm_mode = X11DRV_SHM_IMAGE;
4797 physBitmap->image->data = physBitmap->shminfo.shmaddr;
4803 physBitmap->shm_mode = X11DRV_SHM_NONE;
4804 physBitmap->image = X11DRV_DIB_CreateXImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4805 physBitmap->pixmap_depth );
4808 #ifdef HAVE_LIBXXSHM
4809 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP)
4811 TRACE("Creating shared pixmap for bmp %p.\n", physBitmap->hbitmap);
4812 physBitmap->pixmap = XShmCreatePixmap( gdi_display, root_window,
4813 physBitmap->shminfo.shmaddr, &physBitmap->shminfo,
4814 dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4815 physBitmap->pixmap_depth );
4820 physBitmap->pixmap = XCreatePixmap( gdi_display, root_window, dib.dsBm.bmWidth,
4821 dib.dsBm.bmHeight, physBitmap->pixmap_depth );
4824 wine_tsx11_unlock();
4825 if (!physBitmap->pixmap || !physBitmap->image) return 0;
4827 if (physBitmap->trueColor)
4829 ColorShifts *shifts = &physBitmap->pixmap_color_shifts;
4831 /* When XRender is around and used, we also support dibsections in other formats like 16-bit. In these
4832 * cases we need to override the mask of XImages. The reason is that during XImage creation the masks are
4833 * derived from a 24-bit visual (no 16-bit ones are around when X runs at 24-bit). SetImageBits and other
4834 * functions rely on the color masks for proper color conversion, so we need to override the masks here. */
4835 physBitmap->image->red_mask = shifts->physicalRed.max << shifts->physicalRed.shift;
4836 physBitmap->image->green_mask = shifts->physicalGreen.max << shifts->physicalGreen.shift;
4837 physBitmap->image->blue_mask = shifts->physicalBlue.max << shifts->physicalBlue.shift;
4840 /* install fault handler */
4841 InitializeCriticalSection( &physBitmap->lock );
4842 physBitmap->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": X_PHYSBITMAP.lock");
4844 physBitmap->base = dib.dsBm.bmBits;
4845 physBitmap->size = dib.dsBmih.biSizeImage;
4846 physBitmap->status = DIB_Status_AppMod;
4849 dibs_handler = AddVectoredExceptionHandler( TRUE, X11DRV_DIB_FaultHandler );
4850 EnterCriticalSection( &dibs_cs );
4851 list_add_head( &dibs_list, &physBitmap->entry );
4852 LeaveCriticalSection( &dibs_cs );
4854 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4859 /***********************************************************************
4860 * X11DRV_DIB_DeleteDIBSection
4862 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP *physBitmap, DIBSECTION *dib)
4866 EnterCriticalSection( &dibs_cs );
4867 list_remove( &physBitmap->entry );
4868 last = list_empty( &dibs_list );
4869 LeaveCriticalSection( &dibs_cs );
4873 RemoveVectoredExceptionHandler( dibs_handler );
4874 dibs_handler = NULL;
4877 if (dib->dshSection)
4878 X11DRV_DIB_Coerce(physBitmap, DIB_Status_InSync);
4880 if (physBitmap->image)
4883 #ifdef HAVE_LIBXXSHM
4884 if (physBitmap->shminfo.shmid != -1)
4886 XShmDetach( gdi_display, &(physBitmap->shminfo) );
4887 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP) X11DRV_DIB_DestroyXImage( physBitmap->image );
4888 else XDestroyImage( physBitmap->image );
4889 shmdt( physBitmap->shminfo.shmaddr );
4890 physBitmap->shminfo.shmid = -1;
4891 physBitmap->shm_mode = X11DRV_SHM_NONE;
4895 X11DRV_DIB_DestroyXImage( physBitmap->image );
4896 wine_tsx11_unlock();
4899 HeapFree(GetProcessHeap(), 0, physBitmap->colorMap);
4900 physBitmap->lock.DebugInfo->Spare[0] = 0;
4901 DeleteCriticalSection(&physBitmap->lock);
4904 /***********************************************************************
4905 * SetDIBColorTable (X11DRV.@)
4907 UINT CDECL X11DRV_SetDIBColorTable( X11DRV_PDEVICE *physDev, UINT start, UINT count, const RGBQUAD *colors )
4911 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4913 if (!physBitmap) return 0;
4914 GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib );
4916 if (physBitmap->colorMap && start < physBitmap->nColorMap) {
4917 UINT end = count + start;
4918 if (end > physBitmap->nColorMap) end = physBitmap->nColorMap;
4920 * Changing color table might change the mapping between
4921 * DIB colors and X11 colors and thus alter the visible state
4922 * of the bitmap object.
4925 * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4926 * at least for a 1 bpp dibsection
4928 X11DRV_DIB_Lock( physBitmap, DIB_Status_AppMod );
4929 X11DRV_DIB_GenColorMap( physDev, physBitmap->colorMap, DIB_RGB_COLORS,
4930 dib.dsBm.bmBitsPixel,
4931 TRUE, colors, start, end );
4932 X11DRV_DIB_Unlock( physBitmap, TRUE );
4939 /***********************************************************************
4940 * X11DRV_DIB_CreateDIBFromBitmap
4942 * Allocates a packed DIB and copies the bitmap data into it.
4944 HGLOBAL X11DRV_DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
4949 LPBITMAPINFOHEADER pbmiHeader;
4950 unsigned int cDataSize, cPackedSize, OffsetBits;
4953 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
4956 * A packed DIB contains a BITMAPINFO structure followed immediately by
4957 * an optional color palette and the pixel data.
4960 /* Calculate the size of the packed DIB */
4961 cDataSize = X11DRV_DIB_GetDIBWidthBytes( bmp.bmWidth, bmp.bmBitsPixel ) * abs( bmp.bmHeight );
4962 cPackedSize = sizeof(BITMAPINFOHEADER)
4963 + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
4965 /* Get the offset to the bits */
4966 OffsetBits = cPackedSize - cDataSize;
4968 /* Allocate the packed DIB */
4969 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
4970 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
4974 WARN("Could not allocate packed DIB!\n");
4978 /* A packed DIB starts with a BITMAPINFOHEADER */
4979 pPackedDIB = GlobalLock(hPackedDIB);
4980 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
4982 /* Init the BITMAPINFOHEADER */
4983 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
4984 pbmiHeader->biWidth = bmp.bmWidth;
4985 pbmiHeader->biHeight = bmp.bmHeight;
4986 pbmiHeader->biPlanes = 1;
4987 pbmiHeader->biBitCount = bmp.bmBitsPixel;
4988 pbmiHeader->biCompression = BI_RGB;
4989 pbmiHeader->biSizeImage = 0;
4990 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
4991 pbmiHeader->biClrUsed = 0;
4992 pbmiHeader->biClrImportant = 0;
4994 /* Retrieve the DIB bits from the bitmap and fill in the
4995 * DIB color table if present */
4997 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
4998 hBmp, /* Handle to bitmap */
4999 0, /* First scan line to set in dest bitmap */
5000 bmp.bmHeight, /* Number of scan lines to copy */
5001 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
5002 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
5003 0); /* RGB or palette index */
5004 GlobalUnlock(hPackedDIB);
5006 /* Cleanup if GetDIBits failed */
5007 if (nLinesCopied != bmp.bmHeight)
5009 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
5010 GlobalFree(hPackedDIB);
5017 /**************************************************************************
5018 * X11DRV_DIB_CreateDIBFromPixmap
5020 * Allocates a packed DIB and copies the Pixmap data into it.
5021 * The Pixmap passed in is deleted after the conversion.
5023 HGLOBAL X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap, HDC hdc)
5026 X_PHYSBITMAP *physBitmap;
5029 HGLOBAL hPackedDIB = 0;
5031 int x,y; /* Unused */
5032 unsigned border_width; /* Unused */
5033 unsigned int depth, width, height;
5035 /* Get the Pixmap dimensions and bit depth */
5037 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
5038 &border_width, &depth)) depth = 0;
5039 wine_tsx11_unlock();
5040 if (!depth) return 0;
5042 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
5043 width, height, depth);
5046 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
5047 * and make it a container for the pixmap passed.
5049 if (!(hBmp = CreateBitmap( width, height, 1, depth_to_bpp(depth), NULL ))) return 0;
5051 /* force bitmap to be owned by a screen DC */
5052 hdcMem = CreateCompatibleDC( hdc );
5053 SelectObject( hdcMem, SelectObject( hdcMem, hBmp ));
5056 physBitmap = X11DRV_get_phys_bitmap( hBmp );
5058 /* swap the new pixmap in */
5059 orig_pixmap = physBitmap->pixmap;
5060 physBitmap->pixmap = pixmap;
5063 * Create a packed DIB from the Pixmap wrapper bitmap created above.
5064 * A packed DIB contains a BITMAPINFO structure followed immediately by
5065 * an optional color palette and the pixel data.
5067 hPackedDIB = X11DRV_DIB_CreateDIBFromBitmap(hdc, hBmp);
5069 /* we can now get rid of the HBITMAP and its original pixmap */
5070 physBitmap->pixmap = orig_pixmap;
5073 TRACE("\tReturning packed DIB %p\n", hPackedDIB);
5078 /**************************************************************************
5079 * X11DRV_DIB_CreatePixmapFromDIB
5081 * Creates a Pixmap from a packed DIB
5083 Pixmap X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB, HDC hdc )
5086 X_PHYSBITMAP *physBitmap;
5090 /* Create a DDB from the DIB */
5092 pbmi = GlobalLock(hPackedDIB);
5093 hBmp = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT,
5094 (LPBYTE)pbmi + bitmap_info_size( pbmi, DIB_RGB_COLORS ),
5095 pbmi, DIB_RGB_COLORS);
5096 GlobalUnlock(hPackedDIB);
5098 /* clear the physBitmap so that we can steal its pixmap */
5099 physBitmap = X11DRV_get_phys_bitmap( hBmp );
5100 pixmap = physBitmap->pixmap;
5101 physBitmap->pixmap = 0;
5103 /* Delete the DDB we created earlier now that we have stolen its pixmap */
5106 TRACE("Returning Pixmap %lx\n", pixmap);