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;
87 enum x11drv_shm_mode shm_mode;
90 X_PHYSBITMAP *physBitmap;
91 } X11DRV_DIB_IMAGEBITS_DESCR;
96 RLE_EOL = 0, /* End of line */
97 RLE_END = 1, /* End of bitmap */
98 RLE_DELTA = 2 /* Delta */
102 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *,INT);
103 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *,INT);
104 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *,BOOL);
107 Some of the following helper functions are duplicated in
111 /***********************************************************************
112 * DIB_DoProtectDIBSection
114 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP *physBitmap, DWORD new_prot )
118 VirtualProtect(physBitmap->base, physBitmap->size, new_prot, &old_prot);
119 TRACE("Changed protection from %d to %d\n", old_prot, new_prot);
122 /***********************************************************************
123 * X11DRV_DIB_GetXImageWidthBytes
125 * Return the width of an X image in bytes
127 static inline int X11DRV_DIB_GetXImageWidthBytes( int width, int depth )
129 if (!depth || depth > 32) goto error;
131 if (!ximageDepthTable[depth-1])
133 XImage *testimage = XCreateImage( gdi_display, visual, depth,
134 ZPixmap, 0, NULL, 1, 1, 32, 20 );
137 ximageDepthTable[depth-1] = testimage->bits_per_pixel;
138 XDestroyImage( testimage );
140 else ximageDepthTable[depth-1] = -1;
142 if (ximageDepthTable[depth-1] != -1)
143 return (4 * ((width * ximageDepthTable[depth-1] + 31) / 32));
146 WARN( "(%d): Unsupported depth\n", depth );
151 /***********************************************************************
152 * X11DRV_DIB_GetDIBWidthBytes
154 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
156 static int X11DRV_DIB_GetDIBWidthBytes( int width, int depth )
162 case 1: words = (width + 31) / 32; break;
163 case 4: words = (width + 7) / 8; break;
164 case 8: words = (width + 3) / 4; break;
166 case 16: words = (width + 1) / 2; break;
167 case 24: words = (width * 3 + 3) / 4; break;
169 WARN("(%d): Unsupported depth\n", depth );
178 /***********************************************************************
179 * X11DRV_DIB_GetDIBImageBytes
181 * Return the number of bytes used to hold the image in a DIB bitmap.
183 static int X11DRV_DIB_GetDIBImageBytes( int width, int height, int depth )
185 return X11DRV_DIB_GetDIBWidthBytes( width, depth ) * abs( height );
189 /***********************************************************************
192 * Return the size of the bitmap info structure including color table.
194 int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
196 unsigned int colors, masks = 0;
198 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
200 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
201 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
202 return sizeof(BITMAPCOREHEADER) + colors *
203 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
205 else /* assume BITMAPINFOHEADER */
207 colors = info->bmiHeader.biClrUsed;
208 if (!colors && (info->bmiHeader.biBitCount <= 8))
209 colors = 1 << info->bmiHeader.biBitCount;
210 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
211 return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
212 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
217 /***********************************************************************
218 * X11DRV_DIB_CreateXImage
222 XImage *X11DRV_DIB_CreateXImage( int width, int height, int depth )
225 XImage *image = NULL;
229 width_bytes = X11DRV_DIB_GetXImageWidthBytes( width, depth );
230 data = HeapAlloc( GetProcessHeap(), 0, height * width_bytes );
231 if (data) image = XCreateImage( gdi_display, visual, depth, ZPixmap, 0,
232 data, width, height, 32, width_bytes );
233 if (!image) HeapFree( GetProcessHeap(), 0, data );
239 /***********************************************************************
240 * X11DRV_DIB_DestroyXImage
242 * Destroy an X image created with X11DRV_DIB_CreateXImage.
244 void X11DRV_DIB_DestroyXImage( XImage *image )
246 HeapFree( GetProcessHeap(), 0, image->data );
249 XDestroyImage( image );
254 /***********************************************************************
255 * DIB_GetBitmapInfoEx
257 * Get the info from a bitmap header.
258 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
260 static int DIB_GetBitmapInfoEx( const BITMAPINFOHEADER *header, LONG *width,
261 LONG *height, WORD *planes, WORD *bpp,
262 WORD *compr, DWORD *size )
264 if (header->biSize == sizeof(BITMAPCOREHEADER))
266 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
267 *width = core->bcWidth;
268 *height = core->bcHeight;
269 *planes = core->bcPlanes;
270 *bpp = core->bcBitCount;
275 if (header->biSize >= sizeof(BITMAPINFOHEADER))
277 *width = header->biWidth;
278 *height = header->biHeight;
279 *planes = header->biPlanes;
280 *bpp = header->biBitCount;
281 *compr = header->biCompression;
282 *size = header->biSizeImage;
285 ERR("(%d): unknown/wrong size for header\n", header->biSize );
290 /***********************************************************************
291 * X11DRV_DIB_GetColorCount
293 * Computes the number of colors for the bitmap palette.
294 * Should not be called for a >8-bit deep bitmap.
296 static unsigned int X11DRV_DIB_GetColorCount(const BITMAPINFO *info)
299 BOOL core_info = info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER);
303 colors = 1 << ((const BITMAPCOREINFO*)info)->bmciHeader.bcBitCount;
307 colors = info->bmiHeader.biClrUsed;
308 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
312 ERR("called with >256 colors!\n");
318 /***********************************************************************
321 * Get the info from a bitmap header.
322 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
324 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
325 LONG *height, WORD *bpp, WORD *compr )
330 return DIB_GetBitmapInfoEx( header, width, height, &planes, bpp, compr, &size);
334 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
336 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
337 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
340 /***********************************************************************
341 * X11DRV_DIB_GenColorMap
343 * Fills the color map of a bitmap palette. Should not be called
344 * for a >8-bit deep bitmap.
346 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
347 WORD coloruse, WORD depth, BOOL quads,
348 const void *colorPtr, int start, int end )
352 if (coloruse == DIB_RGB_COLORS)
356 const RGBQUAD * rgb = colorPtr;
358 if (depth == 1) /* Monochrome */
363 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
364 invert = !colour_is_brighter(table[1], table[0]);
366 for (i = start; i < end; i++, rgb++)
367 colorMapping[i] = ((rgb->rgbRed + rgb->rgbGreen +
368 rgb->rgbBlue > 255*3/2 && !invert) ||
369 (rgb->rgbRed + rgb->rgbGreen +
370 rgb->rgbBlue <= 255*3/2 && invert));
373 for (i = start; i < end; i++, rgb++)
374 colorMapping[i] = X11DRV_PALETTE_LookupPixel(RGB(rgb->rgbRed,
380 const RGBTRIPLE * rgb = colorPtr;
382 if (depth == 1) /* Monochrome */
387 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
388 invert = !colour_is_brighter(table[1], table[0]);
390 for (i = start; i < end; i++, rgb++)
391 colorMapping[i] = ((rgb->rgbtRed + rgb->rgbtGreen +
392 rgb->rgbtBlue > 255*3/2 && !invert) ||
393 (rgb->rgbtRed + rgb->rgbtGreen +
394 rgb->rgbtBlue <= 255*3/2 && invert));
397 for (i = start; i < end; i++, rgb++)
398 colorMapping[i] = X11DRV_PALETTE_LookupPixel(RGB(rgb->rgbtRed,
403 else /* DIB_PAL_COLORS */
405 const WORD * index = colorPtr;
407 for (i = start; i < end; i++, index++)
408 colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(*index) );
414 /***********************************************************************
415 * X11DRV_DIB_BuildColorMap
417 * Build the color map from the bitmap palette. Should not be called
418 * for a >8-bit deep bitmap.
420 static int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE *physDev, WORD coloruse, WORD depth,
421 const BITMAPINFO *info, int *nColors )
424 const void *colorPtr;
428 *nColors = X11DRV_DIB_GetColorCount(info);
429 if (!*nColors) return NULL;
431 isInfo = info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER);
432 colorPtr = (const BYTE*)info + (WORD)info->bmiHeader.biSize;
433 if (!(colorMapping = HeapAlloc(GetProcessHeap(), 0, *nColors * sizeof(int) )))
436 return X11DRV_DIB_GenColorMap( physDev, colorMapping, coloruse, depth,
437 isInfo, colorPtr, 0, *nColors);
440 /***********************************************************************
441 * X11DRV_DIB_MapColor
443 static int X11DRV_DIB_MapColor( int *physMap, int nPhysMap, int phys, int oldcol )
447 if ((oldcol < nPhysMap) && (physMap[oldcol] == phys))
450 for (color = 0; color < nPhysMap; color++)
451 if (physMap[color] == phys)
454 WARN("Strange color %08x\n", phys);
459 /*********************************************************************
460 * X11DRV_DIB_GetNearestIndex
462 * Helper for X11DRV_DIB_GetDIBits.
463 * Returns the nearest colour table index for a given RGB.
464 * Nearest is defined by minimizing the sum of the squares.
466 static INT X11DRV_DIB_GetNearestIndex(RGBQUAD *colormap, int numColors, BYTE r, BYTE g, BYTE b)
468 INT i, best = -1, diff, bestdiff = -1;
471 for(color = colormap, i = 0; i < numColors; color++, i++) {
472 diff = (r - color->rgbRed) * (r - color->rgbRed) +
473 (g - color->rgbGreen) * (g - color->rgbGreen) +
474 (b - color->rgbBlue) * (b - color->rgbBlue);
477 if(best == -1 || diff < bestdiff) {
484 /*********************************************************************
485 * X11DRV_DIB_MaskToShift
487 * Helper for X11DRV_DIB_GetDIBits.
488 * Returns the by how many bits to shift a given color so that it is
489 * in the proper position.
491 INT X11DRV_DIB_MaskToShift(DWORD mask)
499 while ((mask&1)==0) {
506 /***********************************************************************
507 * X11DRV_DIB_CheckMask
509 * Check RGB mask if it is either 0 or matches visual's mask.
511 static inline int X11DRV_DIB_CheckMask(int red_mask, int green_mask, int blue_mask)
513 return ( red_mask == 0 && green_mask == 0 && blue_mask == 0 ) ||
514 ( red_mask == visual->red_mask && green_mask == visual->green_mask &&
515 blue_mask == visual->blue_mask );
518 /***********************************************************************
519 * X11DRV_DIB_SetImageBits_1
521 * SetDIBits for a 1-bit deep DIB.
523 static void X11DRV_DIB_SetImageBits_1( int lines, const BYTE *srcbits,
524 DWORD srcwidth, DWORD dstwidth, int left,
525 int *colors, XImage *bmpImage, int linebytes)
534 srcbits = srcbits + linebytes * (lines - 1);
535 linebytes = -linebytes;
538 if ((extra = (left & 7)) != 0) {
542 srcbits += left >> 3;
543 width = min(srcwidth, dstwidth);
545 /* ==== pal 1 dib -> any bmp format ==== */
546 for (h = lines-1; h >=0; h--) {
548 for (i = width/8, x = left; i > 0; i--) {
550 XPutPixel( bmpImage, x++, h, colors[ srcval >> 7] );
551 XPutPixel( bmpImage, x++, h, colors[(srcval >> 6) & 1] );
552 XPutPixel( bmpImage, x++, h, colors[(srcval >> 5) & 1] );
553 XPutPixel( bmpImage, x++, h, colors[(srcval >> 4) & 1] );
554 XPutPixel( bmpImage, x++, h, colors[(srcval >> 3) & 1] );
555 XPutPixel( bmpImage, x++, h, colors[(srcval >> 2) & 1] );
556 XPutPixel( bmpImage, x++, h, colors[(srcval >> 1) & 1] );
557 XPutPixel( bmpImage, x++, h, colors[ srcval & 1] );
563 case 7: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
564 case 6: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
565 case 5: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
566 case 4: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
567 case 3: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
568 case 2: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
569 case 1: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]);
572 srcbits += linebytes;
576 /***********************************************************************
577 * X11DRV_DIB_GetImageBits_1
579 * GetDIBits for a 1-bit deep DIB.
581 static void X11DRV_DIB_GetImageBits_1( int lines, BYTE *dstbits,
582 DWORD dstwidth, DWORD srcwidth,
583 RGBQUAD *colors, PALETTEENTRY *srccolors,
584 XImage *bmpImage, int linebytes )
587 int h, width = min(dstwidth, srcwidth);
591 dstbits = dstbits + linebytes * (lines - 1);
592 linebytes = -linebytes;
595 switch (bmpImage->depth)
599 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
601 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
604 for (h=lines-1; h>=0; h--) {
608 for (x=0; x<width; x++) {
610 srcval=srccolors[XGetPixel(bmpImage, x, h)];
611 dstval|=(X11DRV_DIB_GetNearestIndex
615 srcval.peBlue) << (7 - (x & 7)));
624 /* pad with 0 to DWORD alignment */
625 for (x = (x+7)&~7; x < ((width + 31) & ~31); x+=8)
627 dstbits += linebytes;
635 if (X11DRV_DIB_CheckMask(bmpImage->red_mask, bmpImage->green_mask, bmpImage->blue_mask)
637 /* ==== pal 8 bmp -> pal 1 dib ==== */
639 const BYTE* srcpixel;
642 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
644 for (h=0; h<lines; h++) {
649 for (x=0; x<width; x++) {
651 srcval=srccolors[*srcpixel++];
652 dstval|=(X11DRV_DIB_GetNearestIndex
656 srcval.peBlue) << (7-(x&7)) );
665 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
666 dstbits += linebytes;
677 const WORD* srcpixel;
680 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
682 if (bmpImage->green_mask==0x03e0) {
683 if (bmpImage->red_mask==0x7c00) {
684 /* ==== rgb 555 bmp -> pal 1 dib ==== */
685 for (h=0; h<lines; h++) {
690 for (x=0; x<width; x++) {
693 dstval|=(X11DRV_DIB_GetNearestIndex
695 ((srcval >> 7) & 0xf8) | /* r */
696 ((srcval >> 12) & 0x07),
697 ((srcval >> 2) & 0xf8) | /* g */
698 ((srcval >> 7) & 0x07),
699 ((srcval << 3) & 0xf8) | /* b */
700 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
709 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
710 dstbits += linebytes;
712 } else if (bmpImage->blue_mask==0x7c00) {
713 /* ==== bgr 555 bmp -> pal 1 dib ==== */
714 for (h=0; h<lines; h++) {
719 for (x=0; x<width; x++) {
722 dstval|=(X11DRV_DIB_GetNearestIndex
724 ((srcval << 3) & 0xf8) | /* r */
725 ((srcval >> 2) & 0x07),
726 ((srcval >> 2) & 0xf8) | /* g */
727 ((srcval >> 7) & 0x07),
728 ((srcval >> 7) & 0xf8) | /* b */
729 ((srcval >> 12) & 0x07) ) << (7-(x&7)) );
738 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
739 dstbits += linebytes;
744 } else if (bmpImage->green_mask==0x07e0) {
745 if (bmpImage->red_mask==0xf800) {
746 /* ==== rgb 565 bmp -> pal 1 dib ==== */
747 for (h=0; h<lines; h++) {
752 for (x=0; x<width; x++) {
755 dstval|=(X11DRV_DIB_GetNearestIndex
757 ((srcval >> 8) & 0xf8) | /* r */
758 ((srcval >> 13) & 0x07),
759 ((srcval >> 3) & 0xfc) | /* g */
760 ((srcval >> 9) & 0x03),
761 ((srcval << 3) & 0xf8) | /* b */
762 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
771 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
772 dstbits += linebytes;
774 } else if (bmpImage->blue_mask==0xf800) {
775 /* ==== bgr 565 bmp -> pal 1 dib ==== */
776 for (h=0; h<lines; h++) {
781 for (x=0; x<width; x++) {
784 dstval|=(X11DRV_DIB_GetNearestIndex
786 ((srcval << 3) & 0xf8) | /* r */
787 ((srcval >> 2) & 0x07),
788 ((srcval >> 3) & 0xfc) | /* g */
789 ((srcval >> 9) & 0x03),
790 ((srcval >> 8) & 0xf8) | /* b */
791 ((srcval >> 13) & 0x07) ) << (7-(x&7)) );
800 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
801 dstbits += linebytes;
820 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
821 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
823 if (bmpImage->green_mask!=0x00ff00 ||
824 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
826 } else if (bmpImage->blue_mask==0xff) {
827 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
828 for (h=0; h<lines; h++) {
833 for (x=0; x<width; x++) {
834 dstval|=(X11DRV_DIB_GetNearestIndex
838 srcbyte[0]) << (7-(x&7)) );
839 srcbyte+=bytes_per_pixel;
848 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
849 dstbits += linebytes;
852 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
853 for (h=0; h<lines; h++) {
858 for (x=0; x<width; x++) {
859 dstval|=(X11DRV_DIB_GetNearestIndex
863 srcbyte[2]) << (7-(x&7)) );
864 srcbyte+=bytes_per_pixel;
873 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
874 dstbits += linebytes;
885 unsigned long white = (1 << bmpImage->bits_per_pixel) - 1;
887 /* ==== any bmp format -> pal 1 dib ==== */
888 if ((unsigned)colors[0].rgbRed+colors[0].rgbGreen+colors[0].rgbBlue >=
889 (unsigned)colors[1].rgbRed+colors[1].rgbGreen+colors[1].rgbBlue )
892 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB, "
893 "%s color mapping\n",
894 bmpImage->bits_per_pixel, bmpImage->red_mask,
895 bmpImage->green_mask, bmpImage->blue_mask,
896 neg?"negative":"direct" );
898 for (h=lines-1; h>=0; h--) {
902 for (x=0; x<width; x++) {
903 dstval|=((XGetPixel( bmpImage, x, h) >= white) ^ neg) << (7 - (x&7));
912 dstbits += linebytes;
919 /***********************************************************************
920 * X11DRV_DIB_SetImageBits_4
922 * SetDIBits for a 4-bit deep DIB.
924 static void X11DRV_DIB_SetImageBits_4( int lines, const BYTE *srcbits,
925 DWORD srcwidth, DWORD dstwidth, int left,
926 int *colors, XImage *bmpImage, int linebytes)
934 srcbits = srcbits + linebytes * (lines - 1);
935 linebytes = -linebytes;
942 srcbits += left >> 1;
943 width = min(srcwidth, dstwidth);
945 /* ==== pal 4 dib -> any bmp format ==== */
946 for (h = lines-1; h >= 0; h--) {
948 for (i = width/2, x = left; i > 0; i--) {
949 BYTE srcval=*srcbyte++;
950 XPutPixel( bmpImage, x++, h, colors[srcval >> 4] );
951 XPutPixel( bmpImage, x++, h, colors[srcval & 0x0f] );
954 XPutPixel( bmpImage, x, h, colors[*srcbyte >> 4] );
955 srcbits += linebytes;
961 /***********************************************************************
962 * X11DRV_DIB_GetImageBits_4
964 * GetDIBits for a 4-bit deep DIB.
966 static void X11DRV_DIB_GetImageBits_4( int lines, BYTE *dstbits,
967 DWORD srcwidth, DWORD dstwidth,
968 RGBQUAD *colors, PALETTEENTRY *srccolors,
969 XImage *bmpImage, int linebytes )
972 int h, width = min(srcwidth, dstwidth);
978 dstbits = dstbits + ( linebytes * (lines-1) );
979 linebytes = -linebytes;
984 switch (bmpImage->depth) {
987 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
989 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
992 for (h = lines-1; h >= 0; h--) {
996 for (x = 0; x < width; x++) {
998 srcval=srccolors[XGetPixel(bmpImage, x, h)];
999 dstval|=(X11DRV_DIB_GetNearestIndex
1003 srcval.peBlue) << (4-((x&1)<<2)));
1012 dstbits += linebytes;
1020 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1022 /* ==== pal 8 bmp -> pal 4 dib ==== */
1023 const void* srcbits;
1024 const BYTE *srcpixel;
1027 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1028 for (h=0; h<lines; h++) {
1033 for (x=0; x<width; x++) {
1034 PALETTEENTRY srcval;
1035 srcval = srccolors[*srcpixel++];
1036 dstval|=(X11DRV_DIB_GetNearestIndex
1040 srcval.peBlue) << (4*(1-(x&1))) );
1049 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1050 dstbits += linebytes;
1060 const void* srcbits;
1061 const WORD* srcpixel;
1064 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1066 if (bmpImage->green_mask==0x03e0) {
1067 if (bmpImage->red_mask==0x7c00) {
1068 /* ==== rgb 555 bmp -> pal 4 dib ==== */
1069 for (h=0; h<lines; h++) {
1074 for (x=0; x<width; x++) {
1077 dstval|=(X11DRV_DIB_GetNearestIndex
1079 ((srcval >> 7) & 0xf8) | /* r */
1080 ((srcval >> 12) & 0x07),
1081 ((srcval >> 2) & 0xf8) | /* g */
1082 ((srcval >> 7) & 0x07),
1083 ((srcval << 3) & 0xf8) | /* b */
1084 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1093 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1094 dstbits += linebytes;
1096 } else if (bmpImage->blue_mask==0x7c00) {
1097 /* ==== bgr 555 bmp -> pal 4 dib ==== */
1098 for (h=0; h<lines; h++) {
1103 for (x=0; x<width; x++) {
1106 dstval|=(X11DRV_DIB_GetNearestIndex
1108 ((srcval << 3) & 0xf8) | /* r */
1109 ((srcval >> 2) & 0x07),
1110 ((srcval >> 2) & 0xf8) | /* g */
1111 ((srcval >> 7) & 0x07),
1112 ((srcval >> 7) & 0xf8) | /* b */
1113 ((srcval >> 12) & 0x07) ) << ((1-(x&1))<<2) );
1122 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1123 dstbits += linebytes;
1128 } else if (bmpImage->green_mask==0x07e0) {
1129 if (bmpImage->red_mask==0xf800) {
1130 /* ==== rgb 565 bmp -> pal 4 dib ==== */
1131 for (h=0; h<lines; h++) {
1136 for (x=0; x<width; x++) {
1139 dstval|=(X11DRV_DIB_GetNearestIndex
1141 ((srcval >> 8) & 0xf8) | /* r */
1142 ((srcval >> 13) & 0x07),
1143 ((srcval >> 3) & 0xfc) | /* g */
1144 ((srcval >> 9) & 0x03),
1145 ((srcval << 3) & 0xf8) | /* b */
1146 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1155 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1156 dstbits += linebytes;
1158 } else if (bmpImage->blue_mask==0xf800) {
1159 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1160 for (h=0; h<lines; h++) {
1165 for (x=0; x<width; x++) {
1168 dstval|=(X11DRV_DIB_GetNearestIndex
1170 ((srcval << 3) & 0xf8) | /* r */
1171 ((srcval >> 2) & 0x07),
1172 ((srcval >> 3) & 0xfc) | /* g */
1173 ((srcval >> 9) & 0x03),
1174 ((srcval >> 8) & 0xf8) | /* b */
1175 ((srcval >> 13) & 0x07) ) << ((1-(x&1))<<2) );
1184 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1185 dstbits += linebytes;
1197 if (bmpImage->bits_per_pixel==24) {
1198 const void* srcbits;
1199 const BYTE *srcbyte;
1202 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1204 if (bmpImage->green_mask!=0x00ff00 ||
1205 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1207 } else if (bmpImage->blue_mask==0xff) {
1208 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1209 for (h=0; h<lines; h++) {
1212 for (x=0; x<width/2; x++) {
1213 /* Do 2 pixels at a time */
1214 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1219 X11DRV_DIB_GetNearestIndex
1227 /* And then the odd pixel */
1228 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1234 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1235 dstbits += linebytes;
1238 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1239 for (h=0; h<lines; h++) {
1242 for (x=0; x<width/2; x++) {
1243 /* Do 2 pixels at a time */
1244 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1249 X11DRV_DIB_GetNearestIndex
1257 /* And then the odd pixel */
1258 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1264 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1265 dstbits += linebytes;
1274 const void* srcbits;
1275 const BYTE *srcbyte;
1278 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1280 if (bmpImage->green_mask!=0x00ff00 ||
1281 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1283 } else if (bmpImage->blue_mask==0xff) {
1284 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1285 for (h=0; h<lines; h++) {
1288 for (x=0; x<width/2; x++) {
1289 /* Do 2 pixels at a time */
1290 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1295 X11DRV_DIB_GetNearestIndex
1303 /* And then the odd pixel */
1304 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1310 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1311 dstbits += linebytes;
1314 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1315 for (h=0; h<lines; h++) {
1318 for (x=0; x<width/2; x++) {
1319 /* Do 2 pixels at a time */
1320 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1325 X11DRV_DIB_GetNearestIndex
1333 /* And then the odd pixel */
1334 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1340 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1341 dstbits += linebytes;
1352 /* ==== any bmp format -> pal 4 dib ==== */
1353 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1354 bmpImage->bits_per_pixel, bmpImage->red_mask,
1355 bmpImage->green_mask, bmpImage->blue_mask );
1356 for (h=lines-1; h>=0; h--) {
1358 for (x=0; x<(width & ~1); x+=2) {
1359 *dstbyte++=(X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4) |
1360 X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x+1, h), 0);
1363 *dstbyte=(X11DRV_DIB_MapColor((int *)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4);
1365 dstbits += linebytes;
1372 /***********************************************************************
1373 * X11DRV_DIB_SetImageBits_RLE4
1375 * SetDIBits for a 4-bit deep compressed DIB.
1377 static void X11DRV_DIB_SetImageBits_RLE4( int lines, const BYTE *bits,
1378 DWORD srcwidth, DWORD dstwidth,
1379 int left, int *colors,
1382 unsigned int x = 0, width = min(srcwidth, dstwidth);
1383 int y = lines - 1, c, length;
1384 const BYTE *begin = bits;
1389 if (length) { /* encoded */
1392 if (x >= (left + width)) break;
1393 if( x >= left) XPutPixel(bmpImage, x, y, colors[c >> 4]);
1395 if (!length--) break;
1396 if (x >= (left + width)) break;
1397 if( x >= left) XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1417 default: /* absolute */
1420 if (x >= left && x < (left + width))
1421 XPutPixel(bmpImage, x, y, colors[c >> 4]);
1423 if (!length--) break;
1424 if (x >= left && x < (left + width))
1425 XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1428 if ((bits - begin) & 1)
1437 /***********************************************************************
1438 * X11DRV_DIB_SetImageBits_8
1440 * SetDIBits for an 8-bit deep DIB.
1442 static void X11DRV_DIB_SetImageBits_8( int lines, const BYTE *srcbits,
1443 DWORD srcwidth, DWORD dstwidth, int left,
1444 const int *colors, XImage *bmpImage,
1448 int h, width = min(srcwidth, dstwidth);
1449 const BYTE* srcbyte;
1455 srcbits = srcbits + linebytes * (lines-1);
1456 linebytes = -linebytes;
1461 switch (bmpImage->depth) {
1464 /* Some X servers might have 32 bit/ 16bit deep pixel */
1465 if (lines && width && (bmpImage->bits_per_pixel == 16) &&
1466 (ImageByteOrder(gdi_display)==LSBFirst) )
1468 /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1469 dstbits=(BYTE*)bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1470 for (h = lines ; h--; ) {
1471 #if defined(__i386__) && defined(__GNUC__)
1472 int _cl1,_cl2; /* temp outputs for asm below */
1473 /* Borrowed from DirectDraw */
1474 __asm__ __volatile__(
1479 " movw (%%edx,%%eax,4),%%ax\n"
1481 " xor %%eax,%%eax\n"
1483 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1488 :"eax", "cc", "memory"
1491 DWORD* dstpixel=(DWORD*)dstbits;
1492 for (x=0; x<width/2; x++) {
1493 /* Do 2 pixels at a time */
1494 *dstpixel++=(colors[srcbyte[1]] << 16) | colors[srcbyte[0]];
1498 /* And then the odd pixel */
1499 *((WORD*)dstpixel)=colors[srcbyte[0]];
1502 srcbyte = (srcbits += linebytes);
1503 dstbits -= bmpImage->bytes_per_line;
1510 if (lines && width && (bmpImage->bits_per_pixel == 32) &&
1511 (ImageByteOrder(gdi_display)==LSBFirst) )
1513 dstbits=(BYTE*)bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
1514 /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1515 for (h = lines ; h--; ) {
1516 #if defined(__i386__) && defined(__GNUC__)
1517 int _cl1,_cl2; /* temp outputs for asm below */
1518 /* Borrowed from DirectDraw */
1519 __asm__ __volatile__(
1524 " movl (%%edx,%%eax,4),%%eax\n"
1526 " xor %%eax,%%eax\n"
1528 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1533 :"eax", "cc", "memory"
1536 DWORD* dstpixel=(DWORD*)dstbits;
1537 for (x=0; x<width; x++) {
1538 *dstpixel++=colors[*srcbyte++];
1541 srcbyte = (srcbits += linebytes);
1542 dstbits -= bmpImage->bytes_per_line;
1548 break; /* use slow generic case below */
1551 /* ==== pal 8 dib -> any bmp format ==== */
1552 for (h=lines-1; h>=0; h--) {
1553 for (x=left; x<width+left; x++) {
1554 XPutPixel(bmpImage, x, h, colors[*srcbyte++]);
1556 srcbyte = (srcbits += linebytes);
1560 /***********************************************************************
1561 * X11DRV_DIB_GetImageBits_8
1563 * GetDIBits for an 8-bit deep DIB.
1565 static void X11DRV_DIB_GetImageBits_8( int lines, BYTE *dstbits,
1566 DWORD srcwidth, DWORD dstwidth,
1567 RGBQUAD *colors, PALETTEENTRY *srccolors,
1568 XImage *bmpImage, int linebytes )
1571 int h, width = min(srcwidth, dstwidth);
1577 dstbits = dstbits + ( linebytes * (lines-1) );
1578 linebytes = -linebytes;
1583 * This condition is true when GetImageBits has been called by
1584 * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1585 * 256 colormaps, so we'll just use it for GetDIBits calls.
1586 * (In some cases, in an updateDIBSection, the returned colors are bad too)
1588 if (!srccolors) goto updatesection;
1590 switch (bmpImage->depth) {
1593 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1596 /* ==== pal 1 bmp -> pal 8 dib ==== */
1597 /* ==== pal 4 bmp -> pal 8 dib ==== */
1598 for (h=lines-1; h>=0; h--) {
1600 for (x=0; x<width; x++) {
1601 PALETTEENTRY srcval;
1602 srcval=srccolors[XGetPixel(bmpImage, x, h)];
1603 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1608 dstbits += linebytes;
1616 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1618 /* ==== pal 8 bmp -> pal 8 dib ==== */
1619 const void* srcbits;
1620 const BYTE* srcpixel;
1622 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1623 for (h=0; h<lines; h++) {
1626 for (x = 0; x < width; x++) {
1627 PALETTEENTRY srcval;
1628 srcval=srccolors[*srcpixel++];
1629 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1634 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1635 dstbits += linebytes;
1645 const void* srcbits;
1646 const WORD* srcpixel;
1649 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1651 if (bmpImage->green_mask==0x03e0) {
1652 if (bmpImage->red_mask==0x7c00) {
1653 /* ==== rgb 555 bmp -> pal 8 dib ==== */
1654 for (h=0; h<lines; h++) {
1657 for (x=0; x<width; x++) {
1660 *dstbyte++=X11DRV_DIB_GetNearestIndex
1662 ((srcval >> 7) & 0xf8) | /* r */
1663 ((srcval >> 12) & 0x07),
1664 ((srcval >> 2) & 0xf8) | /* g */
1665 ((srcval >> 7) & 0x07),
1666 ((srcval << 3) & 0xf8) | /* b */
1667 ((srcval >> 2) & 0x07) );
1669 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1670 dstbits += linebytes;
1672 } else if (bmpImage->blue_mask==0x7c00) {
1673 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1674 for (h=0; h<lines; h++) {
1677 for (x=0; x<width; x++) {
1680 *dstbyte++=X11DRV_DIB_GetNearestIndex
1682 ((srcval << 3) & 0xf8) | /* r */
1683 ((srcval >> 2) & 0x07),
1684 ((srcval >> 2) & 0xf8) | /* g */
1685 ((srcval >> 7) & 0x07),
1686 ((srcval >> 7) & 0xf8) | /* b */
1687 ((srcval >> 12) & 0x07) );
1689 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1690 dstbits += linebytes;
1695 } else if (bmpImage->green_mask==0x07e0) {
1696 if (bmpImage->red_mask==0xf800) {
1697 /* ==== rgb 565 bmp -> pal 8 dib ==== */
1698 for (h=0; h<lines; h++) {
1701 for (x=0; x<width; x++) {
1704 *dstbyte++=X11DRV_DIB_GetNearestIndex
1706 ((srcval >> 8) & 0xf8) | /* r */
1707 ((srcval >> 13) & 0x07),
1708 ((srcval >> 3) & 0xfc) | /* g */
1709 ((srcval >> 9) & 0x03),
1710 ((srcval << 3) & 0xf8) | /* b */
1711 ((srcval >> 2) & 0x07) );
1713 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1714 dstbits += linebytes;
1716 } else if (bmpImage->blue_mask==0xf800) {
1717 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1718 for (h=0; h<lines; h++) {
1721 for (x=0; x<width; x++) {
1724 *dstbyte++=X11DRV_DIB_GetNearestIndex
1726 ((srcval << 3) & 0xf8) | /* r */
1727 ((srcval >> 2) & 0x07),
1728 ((srcval >> 3) & 0xfc) | /* g */
1729 ((srcval >> 9) & 0x03),
1730 ((srcval >> 8) & 0xf8) | /* b */
1731 ((srcval >> 13) & 0x07) );
1733 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1734 dstbits += linebytes;
1748 const void* srcbits;
1749 const BYTE *srcbyte;
1751 int bytes_per_pixel;
1753 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1754 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
1756 if (bmpImage->green_mask!=0x00ff00 ||
1757 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1759 } else if (bmpImage->blue_mask==0xff) {
1760 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1761 for (h=0; h<lines; h++) {
1764 for (x=0; x<width; x++) {
1765 *dstbyte++=X11DRV_DIB_GetNearestIndex
1770 srcbyte+=bytes_per_pixel;
1772 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1773 dstbits += linebytes;
1776 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1777 for (h=0; h<lines; h++) {
1780 for (x=0; x<width; x++) {
1781 *dstbyte++=X11DRV_DIB_GetNearestIndex
1786 srcbyte+=bytes_per_pixel;
1788 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1789 dstbits += linebytes;
1797 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1798 bmpImage->depth, bmpImage->red_mask,
1799 bmpImage->green_mask, bmpImage->blue_mask );
1801 /* ==== any bmp format -> pal 8 dib ==== */
1802 for (h=lines-1; h>=0; h--) {
1804 for (x=0; x<width; x++) {
1805 *dstbyte=X11DRV_DIB_MapColor
1807 XGetPixel(bmpImage, x, h), *dstbyte);
1810 dstbits += linebytes;
1816 /***********************************************************************
1817 * X11DRV_DIB_SetImageBits_RLE8
1819 * SetDIBits for an 8-bit deep compressed DIB.
1821 * This function rewritten 941113 by James Youngman. WINE blew out when I
1822 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
1824 * This was because the algorithm assumed that all RLE8 bitmaps end with the
1825 * 'End of bitmap' escape code. This code is very much laxer in what it
1826 * allows to end the expansion. Possibly too lax. See the note by
1827 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
1828 * bitmap should end with RleEnd, but on the other hand, software exists
1829 * that produces ones that don't and Windows 3.1 doesn't complain a bit
1832 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
1833 * James A. Youngman <mbcstjy@afs.man.ac.uk>
1836 static void X11DRV_DIB_SetImageBits_RLE8( int lines, const BYTE *bits,
1837 DWORD srcwidth, DWORD dstwidth,
1838 int left, int *colors,
1841 unsigned int x; /* X-position on each line. Increases. */
1842 int y; /* Line #. Starts at lines-1, decreases */
1843 const BYTE *pIn = bits; /* Pointer to current position in bits */
1844 BYTE length; /* The length pf a run */
1845 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
1848 * Note that the bitmap data is stored by Windows starting at the
1849 * bottom line of the bitmap and going upwards. Within each line,
1850 * the data is stored left-to-right. That's the reason why line
1851 * goes from lines-1 to 0. [JAY]
1861 * If the length byte is not zero (which is the escape value),
1862 * We have a run of length pixels all the same colour. The colour
1863 * index is stored next.
1865 * If the length byte is zero, we need to read the next byte to
1866 * know what to do. [JAY]
1871 * [Run-Length] Encoded mode
1873 int color = colors[*pIn++];
1874 while (length-- && x < (left + dstwidth)) {
1875 if( x >= left) XPutPixel(bmpImage, x, y, color);
1882 * Escape codes (may be an absolute sequence though)
1884 escape_code = (*pIn++);
1893 /* Not all RLE8 bitmaps end with this code. For
1894 * example, Paint Shop Pro produces some that don't.
1895 * That's (I think) what caused the previous
1896 * implementation to fail. [JAY]
1905 default: /* switch to absolute mode */
1906 length = escape_code;
1909 int color = colors[*pIn++];
1910 if (x >= (left + dstwidth))
1915 if( x >= left) XPutPixel(bmpImage, x, y, color);
1919 * If you think for a moment you'll realise that the
1920 * only time we could ever possibly read an odd
1921 * number of bytes is when there is a 0x00 (escape),
1922 * a value >0x02 (absolute mode) and then an odd-
1923 * length run. Therefore this is the only place we
1924 * need to worry about it. Everywhere else the
1925 * bytes are always read in pairs. [JAY]
1927 if (escape_code & 1) pIn++; /* Throw away the pad byte. */
1929 } /* switch (escape_code) : Escape sequence */
1935 /***********************************************************************
1936 * X11DRV_DIB_SetImageBits_16
1938 * SetDIBits for a 16-bit deep DIB.
1940 static void X11DRV_DIB_SetImageBits_16( int lines, const BYTE *srcbits,
1941 DWORD srcwidth, DWORD dstwidth, int left,
1942 X11DRV_PDEVICE *physDev, DWORD rSrc, DWORD gSrc, DWORD bSrc,
1943 XImage *bmpImage, int linebytes )
1946 int h, width = min(srcwidth, dstwidth);
1947 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
1952 srcbits = srcbits + ( linebytes * (lines-1));
1953 linebytes = -linebytes;
1956 switch (bmpImage->depth)
1963 srcbits=srcbits+left*2;
1964 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1966 if (bmpImage->green_mask==0x03e0) {
1967 if (gSrc==bmpImage->green_mask) {
1968 if (rSrc==bmpImage->red_mask) {
1969 /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1970 /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1971 convs->Convert_5x5_asis
1974 dstbits,-bmpImage->bytes_per_line);
1975 } else if (rSrc==bmpImage->blue_mask) {
1976 /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1977 /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1978 convs->Convert_555_reverse
1981 dstbits,-bmpImage->bytes_per_line);
1984 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1985 /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1986 /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1987 convs->Convert_565_to_555_asis
1990 dstbits,-bmpImage->bytes_per_line);
1992 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1993 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1994 convs->Convert_565_to_555_reverse
1997 dstbits,-bmpImage->bytes_per_line);
2000 } else if (bmpImage->green_mask==0x07e0) {
2001 if (gSrc==bmpImage->green_mask) {
2002 if (rSrc==bmpImage->red_mask) {
2003 /* ==== rgb 565 dib -> rgb 565 bmp ==== */
2004 /* ==== bgr 565 dib -> bgr 565 bmp ==== */
2005 convs->Convert_5x5_asis
2008 dstbits,-bmpImage->bytes_per_line);
2010 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
2011 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
2012 convs->Convert_565_reverse
2015 dstbits,-bmpImage->bytes_per_line);
2018 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
2019 /* ==== rgb 555 dib -> rgb 565 bmp ==== */
2020 /* ==== bgr 555 dib -> bgr 565 bmp ==== */
2021 convs->Convert_555_to_565_asis
2024 dstbits,-bmpImage->bytes_per_line);
2026 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
2027 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
2028 convs->Convert_555_to_565_reverse
2031 dstbits,-bmpImage->bytes_per_line);
2041 if (bmpImage->bits_per_pixel==24) {
2044 srcbits=srcbits+left*2;
2045 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2047 if (bmpImage->green_mask!=0x00ff00 ||
2048 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2050 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2051 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2053 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
2054 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
2055 convs->Convert_555_to_888_asis
2058 dstbits,-bmpImage->bytes_per_line);
2060 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
2061 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
2062 convs->Convert_565_to_888_asis
2065 dstbits,-bmpImage->bytes_per_line);
2069 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
2070 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
2071 convs->Convert_555_to_888_reverse
2074 dstbits,-bmpImage->bytes_per_line);
2076 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
2077 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
2078 convs->Convert_565_to_888_reverse
2081 dstbits,-bmpImage->bytes_per_line);
2092 srcbits=srcbits+left*2;
2093 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2095 if (bmpImage->green_mask!=0x00ff00 ||
2096 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2098 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2099 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2101 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
2102 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
2103 convs->Convert_555_to_0888_asis
2106 dstbits,-bmpImage->bytes_per_line);
2108 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
2109 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
2110 convs->Convert_565_to_0888_asis
2113 dstbits,-bmpImage->bytes_per_line);
2117 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
2118 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
2119 convs->Convert_555_to_0888_reverse
2122 dstbits,-bmpImage->bytes_per_line);
2124 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
2125 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
2126 convs->Convert_565_to_0888_reverse
2129 dstbits,-bmpImage->bytes_per_line);
2137 WARN("from 16 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2138 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2139 bmpImage->green_mask, bmpImage->blue_mask );
2145 /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
2146 const WORD* srcpixel;
2147 int rShift1,gShift1,bShift1;
2148 int rShift2,gShift2,bShift2;
2151 /* Set color scaling values */
2152 rShift1=16+X11DRV_DIB_MaskToShift(rSrc)-3;
2153 gShift1=16+X11DRV_DIB_MaskToShift(gSrc)-3;
2154 bShift1=16+X11DRV_DIB_MaskToShift(bSrc)-3;
2159 /* Green has 5 bits, like the others */
2163 /* Green has 6 bits, not 5. Compensate. */
2172 /* We could split it into four separate cases to optimize
2173 * but it is probably not worth it.
2175 for (h=lines-1; h>=0; h--) {
2176 srcpixel=(const WORD*)srcbits;
2177 for (x=left; x<width+left; x++) {
2179 BYTE red,green,blue;
2180 srcval=*srcpixel++ << 16;
2181 red= ((srcval >> rShift1) & 0xf8) |
2182 ((srcval >> rShift2) & 0x07);
2183 green=((srcval >> gShift1) & gMask1) |
2184 ((srcval >> gShift2) & gMask2);
2185 blue= ((srcval >> bShift1) & 0xf8) |
2186 ((srcval >> bShift2) & 0x07);
2187 XPutPixel(bmpImage, x, h,
2188 X11DRV_PALETTE_ToPhysical
2189 (physDev, RGB(red,green,blue)));
2191 srcbits += linebytes;
2199 /***********************************************************************
2200 * X11DRV_DIB_GetImageBits_16
2202 * GetDIBits for an 16-bit deep DIB.
2204 static void X11DRV_DIB_GetImageBits_16( X11DRV_PDEVICE *physDev, int lines, BYTE *dstbits,
2205 DWORD dstwidth, DWORD srcwidth,
2206 PALETTEENTRY *srccolors,
2207 DWORD rDst, DWORD gDst, DWORD bDst,
2208 XImage *bmpImage, int linebytes )
2211 int h, width = min(srcwidth, dstwidth);
2212 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2217 dstbits = dstbits + ( linebytes * (lines-1));
2218 linebytes = -linebytes;
2221 switch (bmpImage->depth)
2226 const char* srcbits;
2228 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2230 if (bmpImage->green_mask==0x03e0) {
2231 if (gDst==bmpImage->green_mask) {
2232 if (rDst==bmpImage->red_mask) {
2233 /* ==== rgb 555 bmp -> rgb 555 dib ==== */
2234 /* ==== bgr 555 bmp -> bgr 555 dib ==== */
2235 convs->Convert_5x5_asis
2237 srcbits,-bmpImage->bytes_per_line,
2240 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
2241 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
2242 convs->Convert_555_reverse
2244 srcbits,-bmpImage->bytes_per_line,
2248 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2249 /* ==== rgb 555 bmp -> rgb 565 dib ==== */
2250 /* ==== bgr 555 bmp -> bgr 565 dib ==== */
2251 convs->Convert_555_to_565_asis
2253 srcbits,-bmpImage->bytes_per_line,
2256 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
2257 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
2258 convs->Convert_555_to_565_reverse
2260 srcbits,-bmpImage->bytes_per_line,
2264 } else if (bmpImage->green_mask==0x07e0) {
2265 if (gDst==bmpImage->green_mask) {
2266 if (rDst == bmpImage->red_mask) {
2267 /* ==== rgb 565 bmp -> rgb 565 dib ==== */
2268 /* ==== bgr 565 bmp -> bgr 565 dib ==== */
2269 convs->Convert_5x5_asis
2271 srcbits,-bmpImage->bytes_per_line,
2274 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
2275 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
2276 convs->Convert_565_reverse
2278 srcbits,-bmpImage->bytes_per_line,
2282 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2283 /* ==== rgb 565 bmp -> rgb 555 dib ==== */
2284 /* ==== bgr 565 bmp -> bgr 555 dib ==== */
2285 convs->Convert_565_to_555_asis
2287 srcbits,-bmpImage->bytes_per_line,
2290 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
2291 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
2292 convs->Convert_565_to_555_reverse
2294 srcbits,-bmpImage->bytes_per_line,
2305 if (bmpImage->bits_per_pixel == 24) {
2306 const char* srcbits;
2308 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2310 if (bmpImage->green_mask!=0x00ff00 ||
2311 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2313 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2314 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2316 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
2317 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
2318 convs->Convert_888_to_555_asis
2320 srcbits,-bmpImage->bytes_per_line,
2323 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2324 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2325 convs->Convert_888_to_565_asis
2327 srcbits,-bmpImage->bytes_per_line,
2332 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
2333 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
2334 convs->Convert_888_to_555_reverse
2336 srcbits,-bmpImage->bytes_per_line,
2339 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
2340 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
2341 convs->Convert_888_to_565_reverse
2343 srcbits,-bmpImage->bytes_per_line,
2353 const char* srcbits;
2355 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2357 if (bmpImage->green_mask!=0x00ff00 ||
2358 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2360 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2361 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2363 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2364 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2365 convs->Convert_0888_to_555_asis
2367 srcbits,-bmpImage->bytes_per_line,
2370 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2371 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2372 convs->Convert_0888_to_565_asis
2374 srcbits,-bmpImage->bytes_per_line,
2379 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2380 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2381 convs->Convert_0888_to_555_reverse
2383 srcbits,-bmpImage->bytes_per_line,
2386 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2387 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2388 convs->Convert_0888_to_565_reverse
2390 srcbits,-bmpImage->bytes_per_line,
2399 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2401 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2402 int rShift,gShift,bShift;
2405 /* Shift everything 16 bits left so that all shifts are >0,
2406 * even for BGR DIBs. Then a single >> 16 will bring everything
2409 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2410 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2411 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2413 /* 6 bits for the green */
2419 for (h = lines - 1; h >= 0; h--) {
2420 dstpixel=(LPWORD)dstbits;
2421 for (x = 0; x < width; x++) {
2422 PALETTEENTRY srcval;
2424 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2425 dstval=((srcval.peRed << rShift) & rDst) |
2426 ((srcval.peGreen << gShift) & gDst) |
2427 ((srcval.peBlue << bShift) & bDst);
2428 *dstpixel++=dstval >> 16;
2430 dstbits += linebytes;
2438 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2440 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2441 int rShift,gShift,bShift;
2442 const BYTE* srcbits;
2443 const BYTE* srcpixel;
2446 /* Shift everything 16 bits left so that all shifts are >0,
2447 * even for BGR DIBs. Then a single >> 16 will bring everything
2450 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2451 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2452 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2454 /* 6 bits for the green */
2460 srcbits=(BYTE*)bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2461 for (h=0; h<lines; h++) {
2463 dstpixel=(LPWORD)dstbits;
2464 for (x = 0; x < width; x++) {
2465 PALETTEENTRY srcval;
2467 srcval=srccolors[*srcpixel++];
2468 dstval=((srcval.peRed << rShift) & rDst) |
2469 ((srcval.peGreen << gShift) & gDst) |
2470 ((srcval.peBlue << bShift) & bDst);
2471 *dstpixel++=dstval >> 16;
2473 srcbits -= bmpImage->bytes_per_line;
2474 dstbits += linebytes;
2484 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2485 int rShift,gShift,bShift;
2488 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%x,%x,%x)\n",
2489 bmpImage->depth, bmpImage->red_mask,
2490 bmpImage->green_mask, bmpImage->blue_mask,
2493 /* Shift everything 16 bits left so that all shifts are >0,
2494 * even for BGR DIBs. Then a single >> 16 will bring everything
2497 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2498 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2499 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2501 /* 6 bits for the green */
2507 for (h = lines - 1; h >= 0; h--) {
2508 dstpixel=(LPWORD)dstbits;
2509 for (x = 0; x < width; x++) {
2512 srcval=X11DRV_PALETTE_ToLogical(physDev, XGetPixel(bmpImage, x, h));
2513 dstval=((GetRValue(srcval) << rShift) & rDst) |
2514 ((GetGValue(srcval) << gShift) & gDst) |
2515 ((GetBValue(srcval) << bShift) & bDst);
2516 *dstpixel++=dstval >> 16;
2518 dstbits += linebytes;
2526 /***********************************************************************
2527 * X11DRV_DIB_SetImageBits_24
2529 * SetDIBits for a 24-bit deep DIB.
2531 static void X11DRV_DIB_SetImageBits_24( int lines, const BYTE *srcbits,
2532 DWORD srcwidth, DWORD dstwidth, int left,
2533 X11DRV_PDEVICE *physDev,
2534 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2535 XImage *bmpImage, DWORD linebytes )
2538 int h, width = min(srcwidth, dstwidth);
2539 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2544 srcbits = srcbits + linebytes * (lines - 1);
2545 linebytes = -linebytes;
2548 switch (bmpImage->depth)
2551 if (bmpImage->bits_per_pixel==24) {
2554 srcbits=srcbits+left*3;
2555 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2557 if (bmpImage->green_mask!=0x00ff00 ||
2558 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2560 } else if (rSrc==bmpImage->red_mask) {
2561 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2562 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2563 convs->Convert_888_asis
2566 dstbits,-bmpImage->bytes_per_line);
2568 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2569 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2570 convs->Convert_888_reverse
2573 dstbits,-bmpImage->bytes_per_line);
2583 srcbits=srcbits+left*3;
2584 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2586 if (bmpImage->green_mask!=0x00ff00 ||
2587 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2589 } else if (rSrc==bmpImage->red_mask) {
2590 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2591 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2592 convs->Convert_888_to_0888_asis
2595 dstbits,-bmpImage->bytes_per_line);
2597 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2598 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2599 convs->Convert_888_to_0888_reverse
2602 dstbits,-bmpImage->bytes_per_line);
2612 srcbits=srcbits+left*3;
2613 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
2615 if (bmpImage->green_mask==0x03e0) {
2616 if ((rSrc==0xff0000 && bmpImage->red_mask==0x7f00) ||
2617 (bSrc==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2618 /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2619 /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2620 convs->Convert_888_to_555_asis
2623 dstbits,-bmpImage->bytes_per_line);
2624 } else if ((rSrc==0xff && bmpImage->red_mask==0x7f00) ||
2625 (bSrc==0xff && bmpImage->blue_mask==0x7f00)) {
2626 /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2627 /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2628 convs->Convert_888_to_555_reverse
2631 dstbits,-bmpImage->bytes_per_line);
2635 } else if (bmpImage->green_mask==0x07e0) {
2636 if ((rSrc==0xff0000 && bmpImage->red_mask==0xf800) ||
2637 (bSrc==0xff0000 && bmpImage->blue_mask==0xf800)) {
2638 /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2639 /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2640 convs->Convert_888_to_565_asis
2643 dstbits,-bmpImage->bytes_per_line);
2644 } else if ((rSrc==0xff && bmpImage->red_mask==0xf800) ||
2645 (bSrc==0xff && bmpImage->blue_mask==0xf800)) {
2646 /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2647 /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2648 convs->Convert_888_to_565_reverse
2651 dstbits,-bmpImage->bytes_per_line);
2663 WARN("from 24 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2664 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2665 bmpImage->green_mask, bmpImage->blue_mask );
2671 /* ==== rgb 888 dib -> any bmp format ==== */
2672 const BYTE* srcbyte;
2674 /* Windows only supports one 24bpp DIB format: RGB888 */
2676 for (h = lines - 1; h >= 0; h--) {
2678 for (x = left; x < width+left; x++) {
2679 XPutPixel(bmpImage, x, h,
2680 X11DRV_PALETTE_ToPhysical
2681 (physDev, RGB(srcbyte[2], srcbyte[1], srcbyte[0])));
2684 srcbits += linebytes;
2692 /***********************************************************************
2693 * X11DRV_DIB_GetImageBits_24
2695 * GetDIBits for an 24-bit deep DIB.
2697 static void X11DRV_DIB_GetImageBits_24( X11DRV_PDEVICE *physDev, int lines, BYTE *dstbits,
2698 DWORD dstwidth, DWORD srcwidth,
2699 PALETTEENTRY *srccolors,
2700 DWORD rDst, DWORD gDst, DWORD bDst,
2701 XImage *bmpImage, DWORD linebytes )
2704 int h, width = min(srcwidth, dstwidth);
2705 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2710 dstbits = dstbits + ( linebytes * (lines-1) );
2711 linebytes = -linebytes;
2714 switch (bmpImage->depth)
2717 if (bmpImage->bits_per_pixel==24) {
2718 const char* srcbits;
2720 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2722 if (bmpImage->green_mask!=0x00ff00 ||
2723 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2725 } else if (rDst==bmpImage->red_mask) {
2726 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2727 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2728 convs->Convert_888_asis
2730 srcbits,-bmpImage->bytes_per_line,
2733 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2734 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2735 convs->Convert_888_reverse
2737 srcbits,-bmpImage->bytes_per_line,
2746 const char* srcbits;
2748 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2750 if (bmpImage->green_mask!=0x00ff00 ||
2751 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2753 } else if (rDst==bmpImage->red_mask) {
2754 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2755 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2756 convs->Convert_0888_to_888_asis
2758 srcbits,-bmpImage->bytes_per_line,
2761 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2762 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2763 convs->Convert_0888_to_888_reverse
2765 srcbits,-bmpImage->bytes_per_line,
2774 const char* srcbits;
2776 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2778 if (bmpImage->green_mask==0x03e0) {
2779 if ((rDst==0xff0000 && bmpImage->red_mask==0x7f00) ||
2780 (bDst==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2781 /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2782 /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2783 convs->Convert_555_to_888_asis
2785 srcbits,-bmpImage->bytes_per_line,
2787 } else if ((rDst==0xff && bmpImage->red_mask==0x7f00) ||
2788 (bDst==0xff && bmpImage->blue_mask==0x7f00)) {
2789 /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2790 /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2791 convs->Convert_555_to_888_reverse
2793 srcbits,-bmpImage->bytes_per_line,
2798 } else if (bmpImage->green_mask==0x07e0) {
2799 if ((rDst==0xff0000 && bmpImage->red_mask==0xf800) ||
2800 (bDst==0xff0000 && bmpImage->blue_mask==0xf800)) {
2801 /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2802 /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2803 convs->Convert_565_to_888_asis
2805 srcbits,-bmpImage->bytes_per_line,
2807 } else if ((rDst==0xff && bmpImage->red_mask==0xf800) ||
2808 (bDst==0xff && bmpImage->blue_mask==0xf800)) {
2809 /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2810 /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2811 convs->Convert_565_to_888_reverse
2813 srcbits,-bmpImage->bytes_per_line,
2826 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2828 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2831 /* Windows only supports one 24bpp DIB format: rgb 888 */
2832 for (h = lines - 1; h >= 0; h--) {
2834 for (x = 0; x < width; x++) {
2835 PALETTEENTRY srcval;
2836 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2837 dstbyte[0]=srcval.peBlue;
2838 dstbyte[1]=srcval.peGreen;
2839 dstbyte[2]=srcval.peRed;
2842 dstbits += linebytes;
2850 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2852 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2853 const void* srcbits;
2854 const BYTE* srcpixel;
2857 /* Windows only supports one 24bpp DIB format: rgb 888 */
2858 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2859 for (h = lines - 1; h >= 0; h--) {
2862 for (x = 0; x < width; x++ ) {
2863 PALETTEENTRY srcval;
2864 srcval=srccolors[*srcpixel++];
2865 dstbyte[0]=srcval.peBlue;
2866 dstbyte[1]=srcval.peGreen;
2867 dstbyte[2]=srcval.peRed;
2870 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
2871 dstbits += linebytes;
2881 /* ==== any bmp format -> 888 dib ==== */
2884 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%x,%x,%x)\n",
2885 bmpImage->depth, bmpImage->red_mask,
2886 bmpImage->green_mask, bmpImage->blue_mask,
2889 /* Windows only supports one 24bpp DIB format: rgb 888 */
2890 for (h = lines - 1; h >= 0; h--) {
2892 for (x = 0; x < width; x++) {
2893 COLORREF srcval=X11DRV_PALETTE_ToLogical
2894 (physDev, XGetPixel( bmpImage, x, h ));
2895 dstbyte[0]=GetBValue(srcval);
2896 dstbyte[1]=GetGValue(srcval);
2897 dstbyte[2]=GetRValue(srcval);
2900 dstbits += linebytes;
2908 /***********************************************************************
2909 * X11DRV_DIB_SetImageBits_32
2911 * SetDIBits for a 32-bit deep DIB.
2913 static void X11DRV_DIB_SetImageBits_32(int lines, const BYTE *srcbits,
2914 DWORD srcwidth, DWORD dstwidth, int left,
2915 X11DRV_PDEVICE *physDev,
2916 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2921 int h, width = min(srcwidth, dstwidth);
2922 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2927 srcbits = srcbits + ( linebytes * (lines-1) );
2928 linebytes = -linebytes;
2931 switch (bmpImage->depth)
2934 if (bmpImage->bits_per_pixel==24) {
2937 srcbits=srcbits+left*4;
2938 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2940 if (rSrc==bmpImage->red_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->blue_mask) {
2941 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2942 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2943 convs->Convert_0888_to_888_asis
2946 dstbits,-bmpImage->bytes_per_line);
2947 } else if (bmpImage->green_mask!=0x00ff00 ||
2948 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2950 /* the tests below assume sane bmpImage masks */
2951 } else if (rSrc==bmpImage->blue_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->red_mask) {
2952 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2953 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2954 convs->Convert_0888_to_888_reverse
2957 dstbits,-bmpImage->bytes_per_line);
2958 } else if (bmpImage->blue_mask==0xff) {
2959 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2960 convs->Convert_any0888_to_rgb888
2964 dstbits,-bmpImage->bytes_per_line);
2966 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2967 convs->Convert_any0888_to_bgr888
2971 dstbits,-bmpImage->bytes_per_line);
2981 srcbits=srcbits+left*4;
2982 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2984 if (gSrc==bmpImage->green_mask) {
2985 if (rSrc==bmpImage->red_mask && bSrc==bmpImage->blue_mask) {
2986 /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2987 /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2988 convs->Convert_0888_asis
2991 dstbits,-bmpImage->bytes_per_line);
2992 } else if (bmpImage->green_mask!=0x00ff00 ||
2993 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2995 /* the tests below assume sane bmpImage masks */
2996 } else if (rSrc==bmpImage->blue_mask && bSrc==bmpImage->red_mask) {
2997 /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2998 /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2999 convs->Convert_0888_reverse
3002 dstbits,-bmpImage->bytes_per_line);
3004 /* ==== any 0888 dib -> any 0888 bmp ==== */
3005 convs->Convert_0888_any
3009 dstbits,-bmpImage->bytes_per_line,
3010 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3012 } else if (bmpImage->green_mask!=0x00ff00 ||
3013 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3015 /* the tests below assume sane bmpImage masks */
3017 /* ==== any 0888 dib -> any 0888 bmp ==== */
3018 convs->Convert_0888_any
3022 dstbits,-bmpImage->bytes_per_line,
3023 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3033 srcbits=srcbits+left*4;
3034 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
3036 if (rSrc==0xff0000 && gSrc==0x00ff00 && bSrc==0x0000ff) {
3037 if (bmpImage->green_mask==0x03e0) {
3038 if (bmpImage->red_mask==0x7f00) {
3039 /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
3040 convs->Convert_0888_to_555_asis
3043 dstbits,-bmpImage->bytes_per_line);
3044 } else if (bmpImage->blue_mask==0x7f00) {
3045 /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
3046 convs->Convert_0888_to_555_reverse
3049 dstbits,-bmpImage->bytes_per_line);
3053 } else if (bmpImage->green_mask==0x07e0) {
3054 if (bmpImage->red_mask==0xf800) {
3055 /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
3056 convs->Convert_0888_to_565_asis
3059 dstbits,-bmpImage->bytes_per_line);
3060 } else if (bmpImage->blue_mask==0xf800) {
3061 /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
3062 convs->Convert_0888_to_565_reverse
3065 dstbits,-bmpImage->bytes_per_line);
3072 } else if (rSrc==0x0000ff && gSrc==0x00ff00 && bSrc==0xff0000) {
3073 if (bmpImage->green_mask==0x03e0) {
3074 if (bmpImage->blue_mask==0x7f00) {
3075 /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
3076 convs->Convert_0888_to_555_asis
3079 dstbits,-bmpImage->bytes_per_line);
3080 } else if (bmpImage->red_mask==0x7f00) {
3081 /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
3082 convs->Convert_0888_to_555_reverse
3085 dstbits,-bmpImage->bytes_per_line);
3089 } else if (bmpImage->green_mask==0x07e0) {
3090 if (bmpImage->blue_mask==0xf800) {
3091 /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
3092 convs->Convert_0888_to_565_asis
3095 dstbits,-bmpImage->bytes_per_line);
3096 } else if (bmpImage->red_mask==0xf800) {
3097 /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
3098 convs->Convert_0888_to_565_reverse
3101 dstbits,-bmpImage->bytes_per_line);
3109 if (bmpImage->green_mask==0x03e0 &&
3110 (bmpImage->red_mask==0x7f00 ||
3111 bmpImage->blue_mask==0x7f00)) {
3112 /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
3113 convs->Convert_any0888_to_5x5
3117 dstbits,-bmpImage->bytes_per_line,
3118 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3119 } else if (bmpImage->green_mask==0x07e0 &&
3120 (bmpImage->red_mask==0xf800 ||
3121 bmpImage->blue_mask==0xf800)) {
3122 /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
3123 convs->Convert_any0888_to_5x5
3127 dstbits,-bmpImage->bytes_per_line,
3128 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3138 WARN("from 32 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
3139 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
3140 bmpImage->green_mask, bmpImage->blue_mask );
3146 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
3147 const DWORD* srcpixel;
3148 int rShift,gShift,bShift;
3150 rShift=X11DRV_DIB_MaskToShift(rSrc);
3151 gShift=X11DRV_DIB_MaskToShift(gSrc);
3152 bShift=X11DRV_DIB_MaskToShift(bSrc);
3154 for (h = lines - 1; h >= 0; h--) {
3155 srcpixel=(const DWORD*)srcbits;
3156 for (x = left; x < width+left; x++) {
3158 BYTE red,green,blue;
3159 srcvalue=*srcpixel++;
3160 red= (srcvalue >> rShift) & 0xff;
3161 green=(srcvalue >> gShift) & 0xff;
3162 blue= (srcvalue >> bShift) & 0xff;
3163 XPutPixel(bmpImage, x, h, X11DRV_PALETTE_ToPhysical
3164 (physDev, RGB(red,green,blue)));
3166 srcbits += linebytes;
3174 /***********************************************************************
3175 * X11DRV_DIB_GetImageBits_32
3177 * GetDIBits for an 32-bit deep DIB.
3179 static void X11DRV_DIB_GetImageBits_32( X11DRV_PDEVICE *physDev, int lines, BYTE *dstbits,
3180 DWORD dstwidth, DWORD srcwidth,
3181 PALETTEENTRY *srccolors,
3182 DWORD rDst, DWORD gDst, DWORD bDst,
3183 XImage *bmpImage, int linebytes )
3186 int h, width = min(srcwidth, dstwidth);
3188 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
3193 dstbits = dstbits + ( linebytes * (lines-1) );
3194 linebytes = -linebytes;
3199 switch (bmpImage->depth)
3202 if (bmpImage->bits_per_pixel==24) {
3203 const void* srcbits;
3205 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3207 if (rDst==bmpImage->red_mask && gDst==bmpImage->green_mask && bDst==bmpImage->blue_mask) {
3208 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
3209 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
3210 convs->Convert_888_to_0888_asis
3212 srcbits,-bmpImage->bytes_per_line,
3214 } else if (bmpImage->green_mask!=0x00ff00 ||
3215 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3217 /* the tests below assume sane bmpImage masks */
3218 } else if (rDst==bmpImage->blue_mask && gDst==bmpImage->green_mask && bDst==bmpImage->red_mask) {
3219 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
3220 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
3221 convs->Convert_888_to_0888_reverse
3223 srcbits,-bmpImage->bytes_per_line,
3225 } else if (bmpImage->blue_mask==0xff) {
3226 /* ==== rgb 888 bmp -> any 0888 dib ==== */
3227 convs->Convert_rgb888_to_any0888
3229 srcbits,-bmpImage->bytes_per_line,
3233 /* ==== bgr 888 bmp -> any 0888 dib ==== */
3234 convs->Convert_bgr888_to_any0888
3236 srcbits,-bmpImage->bytes_per_line,
3246 const char* srcbits;
3248 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3250 if (gDst==bmpImage->green_mask) {
3251 if (rDst==bmpImage->red_mask && bDst==bmpImage->blue_mask) {
3252 /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
3253 /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
3254 convs->Convert_0888_asis
3256 srcbits,-bmpImage->bytes_per_line,
3258 } else if (bmpImage->green_mask!=0x00ff00 ||
3259 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3261 /* the tests below assume sane bmpImage masks */
3262 } else if (rDst==bmpImage->blue_mask && bDst==bmpImage->red_mask) {
3263 /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
3264 /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
3265 convs->Convert_0888_reverse
3267 srcbits,-bmpImage->bytes_per_line,
3270 /* ==== any 0888 bmp -> any 0888 dib ==== */
3271 convs->Convert_0888_any
3273 srcbits,-bmpImage->bytes_per_line,
3274 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3278 } else if (bmpImage->green_mask!=0x00ff00 ||
3279 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3281 /* the tests below assume sane bmpImage masks */
3283 /* ==== any 0888 bmp -> any 0888 dib ==== */
3284 convs->Convert_0888_any
3286 srcbits,-bmpImage->bytes_per_line,
3287 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3297 const char* srcbits;
3299 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3301 if (rDst==0xff0000 && gDst==0x00ff00 && bDst==0x0000ff) {
3302 if (bmpImage->green_mask==0x03e0) {
3303 if (bmpImage->red_mask==0x7f00) {
3304 /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
3305 convs->Convert_555_to_0888_asis
3307 srcbits,-bmpImage->bytes_per_line,
3309 } else if (bmpImage->blue_mask==0x7f00) {
3310 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
3311 convs->Convert_555_to_0888_reverse
3313 srcbits,-bmpImage->bytes_per_line,
3318 } else if (bmpImage->green_mask==0x07e0) {
3319 if (bmpImage->red_mask==0xf800) {
3320 /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
3321 convs->Convert_565_to_0888_asis
3323 srcbits,-bmpImage->bytes_per_line,
3325 } else if (bmpImage->blue_mask==0xf800) {
3326 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
3327 convs->Convert_565_to_0888_reverse
3329 srcbits,-bmpImage->bytes_per_line,
3337 } else if (rDst==0x0000ff && gDst==0x00ff00 && bDst==0xff0000) {
3338 if (bmpImage->green_mask==0x03e0) {
3339 if (bmpImage->blue_mask==0x7f00) {
3340 /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
3341 convs->Convert_555_to_0888_asis
3343 srcbits,-bmpImage->bytes_per_line,
3345 } else if (bmpImage->red_mask==0x7f00) {
3346 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
3347 convs->Convert_555_to_0888_reverse
3349 srcbits,-bmpImage->bytes_per_line,
3354 } else if (bmpImage->green_mask==0x07e0) {
3355 if (bmpImage->blue_mask==0xf800) {
3356 /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3357 convs->Convert_565_to_0888_asis
3359 srcbits,-bmpImage->bytes_per_line,
3361 } else if (bmpImage->red_mask==0xf800) {
3362 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3363 convs->Convert_565_to_0888_reverse
3365 srcbits,-bmpImage->bytes_per_line,
3374 if (bmpImage->green_mask==0x03e0 &&
3375 (bmpImage->red_mask==0x7f00 ||
3376 bmpImage->blue_mask==0x7f00)) {
3377 /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3378 convs->Convert_5x5_to_any0888
3380 srcbits,-bmpImage->bytes_per_line,
3381 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3384 } else if (bmpImage->green_mask==0x07e0 &&
3385 (bmpImage->red_mask==0xf800 ||
3386 bmpImage->blue_mask==0xf800)) {
3387 /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3388 convs->Convert_5x5_to_any0888
3390 srcbits,-bmpImage->bytes_per_line,
3391 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3403 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3405 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3406 int rShift,gShift,bShift;
3409 rShift=X11DRV_DIB_MaskToShift(rDst);
3410 gShift=X11DRV_DIB_MaskToShift(gDst);
3411 bShift=X11DRV_DIB_MaskToShift(bDst);
3412 for (h = lines - 1; h >= 0; h--) {
3413 dstpixel=(DWORD*)dstbits;
3414 for (x = 0; x < width; x++) {
3415 PALETTEENTRY srcval;
3416 srcval = srccolors[XGetPixel(bmpImage, x, h)];
3417 *dstpixel++=(srcval.peRed << rShift) |
3418 (srcval.peGreen << gShift) |
3419 (srcval.peBlue << bShift);
3421 dstbits += linebytes;
3429 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3431 /* ==== pal 8 bmp -> any 0888 dib ==== */
3432 int rShift,gShift,bShift;
3433 const void* srcbits;
3434 const BYTE* srcpixel;
3437 rShift=X11DRV_DIB_MaskToShift(rDst);
3438 gShift=X11DRV_DIB_MaskToShift(gDst);
3439 bShift=X11DRV_DIB_MaskToShift(bDst);
3440 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3441 for (h = lines - 1; h >= 0; h--) {
3443 dstpixel=(DWORD*)dstbits;
3444 for (x = 0; x < width; x++) {
3445 PALETTEENTRY srcval;
3446 srcval=srccolors[*srcpixel++];
3447 *dstpixel++=(srcval.peRed << rShift) |
3448 (srcval.peGreen << gShift) |
3449 (srcval.peBlue << bShift);
3451 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
3452 dstbits += linebytes;
3462 /* ==== any bmp format -> any 0888 dib ==== */
3463 int rShift,gShift,bShift;
3466 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%x,%x,%x)\n",
3467 bmpImage->depth, bmpImage->red_mask,
3468 bmpImage->green_mask, bmpImage->blue_mask,
3471 rShift=X11DRV_DIB_MaskToShift(rDst);
3472 gShift=X11DRV_DIB_MaskToShift(gDst);
3473 bShift=X11DRV_DIB_MaskToShift(bDst);
3474 for (h = lines - 1; h >= 0; h--) {
3475 dstpixel=(DWORD*)dstbits;
3476 for (x = 0; x < width; x++) {
3478 srcval=X11DRV_PALETTE_ToLogical(physDev, XGetPixel(bmpImage, x, h));
3479 *dstpixel++=(GetRValue(srcval) << rShift) |
3480 (GetGValue(srcval) << gShift) |
3481 (GetBValue(srcval) << bShift);
3483 dstbits += linebytes;
3490 static int XGetSubImageErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
3492 return (event->request_code == X_GetImage && event->error_code == BadMatch);
3495 /***********************************************************************
3496 * X11DRV_DIB_SetImageBits_GetSubImage
3498 * Helper for X11DRV_DIB_SetImageBits
3500 static void X11DRV_DIB_SetImageBits_GetSubImage(
3501 const X11DRV_DIB_IMAGEBITS_DESCR *descr, XImage *bmpImage)
3503 /* compressed bitmaps may contain gaps in them. So make a copy
3504 * of the existing pixels first */
3507 SetRect( &bmprc, descr->xDest, descr->yDest,
3508 descr->xDest + descr->width , descr->yDest + descr->height );
3509 GetRgnBox( descr->physDev->region, &rc );
3510 /* convert from dc to drawable origin */
3511 OffsetRect( &rc, descr->physDev->dc_rect.left, descr->physDev->dc_rect.top);
3512 /* clip visible rect with bitmap */
3513 if( IntersectRect( &rc, &rc, &bmprc))
3515 X11DRV_expect_error( gdi_display, XGetSubImageErrorHandler, NULL );
3516 XGetSubImage( gdi_display, descr->drawable, rc.left, rc.top,
3517 rc.right - rc.left, rc.bottom - rc.top, AllPlanes,
3519 descr->xSrc + rc.left - bmprc.left,
3520 descr->ySrc + rc.top - bmprc.top);
3521 X11DRV_check_error();
3525 /***********************************************************************
3526 * X11DRV_DIB_SetImageBits
3528 * Transfer the bits to an X image.
3529 * Helper function for SetDIBits() and SetDIBitsToDevice().
3531 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3533 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3534 void *old_data = NULL;
3539 bmpImage = descr->image;
3541 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3542 descr->infoWidth, lines, 32, 0 );
3543 bmpImage->data = HeapAlloc( GetProcessHeap(), 0, lines * bmpImage->bytes_per_line );
3544 if(bmpImage->data == NULL) {
3545 ERR("Out of memory!\n");
3546 XDestroyImage( bmpImage );
3547 wine_tsx11_unlock();
3551 wine_tsx11_unlock();
3553 TRACE("Dib: depth=%d r=%x g=%x b=%x\n",
3554 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3555 TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3556 bmpImage->depth,bmpImage->bits_per_pixel,
3557 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3559 #ifdef HAVE_LIBXXSHM
3560 if (descr->shm_mode == X11DRV_SHM_PIXMAP
3561 && descr->xSrc == 0 && descr->ySrc == 0
3562 && descr->xDest == 0 && descr->yDest == 0)
3564 TRACE("Using the shared pixmap data.\n");
3567 XSync( gdi_display, False );
3568 wine_tsx11_unlock();
3570 old_data = descr->image->data;
3571 descr->image->data = descr->physBitmap->shminfo.shmaddr;
3575 /* Transfer the pixels */
3578 switch(descr->infoBpp)
3581 X11DRV_DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
3582 descr->width, descr->xSrc, (int *)(descr->colorMap),
3583 bmpImage, descr->dibpitch );
3586 if (descr->compression) {
3587 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3588 X11DRV_DIB_SetImageBits_RLE4( descr->lines, descr->bits,
3589 descr->infoWidth, descr->width,
3590 descr->xSrc, (int *)(descr->colorMap),
3593 X11DRV_DIB_SetImageBits_4( descr->lines, descr->bits,
3594 descr->infoWidth, descr->width,
3595 descr->xSrc, (int*)(descr->colorMap),
3596 bmpImage, descr->dibpitch );
3599 if (descr->compression) {
3600 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3601 X11DRV_DIB_SetImageBits_RLE8( descr->lines, descr->bits,
3602 descr->infoWidth, descr->width,
3603 descr->xSrc, (int *)(descr->colorMap),
3606 X11DRV_DIB_SetImageBits_8( descr->lines, descr->bits,
3607 descr->infoWidth, descr->width,
3608 descr->xSrc, (int *)(descr->colorMap),
3609 bmpImage, descr->dibpitch );
3613 X11DRV_DIB_SetImageBits_16( descr->lines, descr->bits,
3614 descr->infoWidth, descr->width,
3615 descr->xSrc, descr->physDev,
3616 descr->rMask, descr->gMask, descr->bMask,
3617 bmpImage, descr->dibpitch);
3620 X11DRV_DIB_SetImageBits_24( descr->lines, descr->bits,
3621 descr->infoWidth, descr->width,
3622 descr->xSrc, descr->physDev,
3623 descr->rMask, descr->gMask, descr->bMask,
3624 bmpImage, descr->dibpitch);
3627 X11DRV_DIB_SetImageBits_32( descr->lines, descr->bits,
3628 descr->infoWidth, descr->width,
3629 descr->xSrc, descr->physDev,
3630 descr->rMask, descr->gMask, descr->bMask,
3631 bmpImage, descr->dibpitch);
3634 WARN("(%d): Invalid depth\n", descr->infoBpp );
3640 WARN( "invalid bits pointer %p\n", descr->bits );
3645 TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3646 descr->drawable, descr->gc, bmpImage,
3647 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3648 descr->width, descr->height);
3653 #ifdef HAVE_LIBXXSHM
3654 if (descr->shm_mode == X11DRV_SHM_PIXMAP
3655 && descr->xSrc == 0 && descr->ySrc == 0
3656 && descr->xDest == 0 && descr->yDest == 0)
3658 XSync( gdi_display, False );
3660 else if (descr->shm_mode == X11DRV_SHM_IMAGE && descr->image)
3662 XShmPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3663 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3664 descr->width, descr->height, FALSE );
3665 XSync( gdi_display, 0 );
3670 XPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3671 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3672 descr->width, descr->height );
3676 if (old_data) descr->image->data = old_data;
3678 if (!descr->image) X11DRV_DIB_DestroyXImage( bmpImage );
3679 wine_tsx11_unlock();
3683 /***********************************************************************
3684 * X11DRV_DIB_GetImageBits
3686 * Transfer the bits from an X image.
3688 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3690 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3691 void *old_data = NULL;
3696 bmpImage = descr->image;
3698 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3699 descr->infoWidth, lines, 32, 0 );
3700 bmpImage->data = HeapAlloc( GetProcessHeap(), 0, lines * bmpImage->bytes_per_line );
3701 if(bmpImage->data == NULL) {
3702 ERR("Out of memory!\n");
3703 XDestroyImage( bmpImage );
3704 wine_tsx11_unlock();
3709 #ifdef HAVE_LIBXXSHM
3711 /* We must not call XShmGetImage() with a bitmap which is bigger than the available area.
3712 If we do, XShmGetImage() will fail (X exception), as it checks for this internally. */
3713 if (descr->shm_mode == X11DRV_SHM_PIXMAP && descr->image
3714 && descr->xSrc == 0 && descr->ySrc == 0
3715 && descr->xDest == 0 && descr->yDest == 0
3716 && bmpImage->width <= (descr->width - descr->xSrc)
3717 && bmpImage->height <= (descr->height - descr->ySrc))
3719 XSync( gdi_display, False );
3720 old_data = bmpImage->data;
3721 bmpImage->data = descr->physBitmap->shminfo.shmaddr;
3722 TRACE("Using shared pixmap data.\n");
3724 else if (descr->shm_mode == X11DRV_SHM_IMAGE && descr->image
3725 && bmpImage->width <= (descr->width - descr->xSrc)
3726 && bmpImage->height <= (descr->height - descr->ySrc))
3728 int saveRed, saveGreen, saveBlue;
3730 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3731 gdi_display, descr->drawable, bmpImage,
3732 descr->xSrc, descr->ySrc, AllPlanes);
3734 /* We must save and restore the bmpImage's masks in order
3735 * to preserve them across the call to XShmGetImage, which
3736 * decides to eliminate them since it doesn't happen to know
3737 * what the format of the image is supposed to be, even though
3739 saveRed = bmpImage->red_mask;
3740 saveBlue= bmpImage->blue_mask;
3741 saveGreen = bmpImage->green_mask;
3743 XShmGetImage( gdi_display, descr->drawable, bmpImage,
3744 descr->xSrc, descr->ySrc, AllPlanes);
3746 bmpImage->red_mask = saveRed;
3747 bmpImage->blue_mask = saveBlue;
3748 bmpImage->green_mask = saveGreen;
3751 #endif /* HAVE_LIBXXSHM */
3753 TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3754 gdi_display, descr->drawable, descr->xSrc, descr->ySrc, descr->width,
3755 lines, AllPlanes, ZPixmap, bmpImage, descr->xDest, descr->yDest);
3756 XGetSubImage( gdi_display, descr->drawable, descr->xSrc, descr->ySrc,
3757 descr->width, lines, AllPlanes, ZPixmap,
3758 bmpImage, descr->xDest, descr->yDest );
3760 wine_tsx11_unlock();
3762 TRACE("Dib: depth=%2d r=%x g=%x b=%x\n",
3763 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3764 TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3765 bmpImage->depth,bmpImage->bits_per_pixel,
3766 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3767 /* Transfer the pixels */
3768 switch(descr->infoBpp)
3771 X11DRV_DIB_GetImageBits_1( descr->lines,(LPVOID)descr->bits,
3772 descr->infoWidth, descr->width,
3773 descr->colorMap, descr->palentry,
3774 bmpImage, descr->dibpitch );
3778 if (descr->compression) {
3779 FIXME("Compression not yet supported!\n");
3780 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 4 ) * abs(descr->lines))
3783 X11DRV_DIB_GetImageBits_4( descr->lines,(LPVOID)descr->bits,
3784 descr->infoWidth, descr->width,
3785 descr->colorMap, descr->palentry,
3786 bmpImage, descr->dibpitch );
3789 if (descr->compression) {
3790 FIXME("Compression not yet supported!\n");
3791 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 8 ) * abs(descr->lines))
3794 X11DRV_DIB_GetImageBits_8( descr->lines, (LPVOID)descr->bits,
3795 descr->infoWidth, descr->width,
3796 descr->colorMap, descr->palentry,
3797 bmpImage, descr->dibpitch );
3801 X11DRV_DIB_GetImageBits_16( descr->physDev, descr->lines, (LPVOID)descr->bits,
3802 descr->infoWidth,descr->width,
3804 descr->rMask, descr->gMask, descr->bMask,
3805 bmpImage, descr->dibpitch );
3809 X11DRV_DIB_GetImageBits_24( descr->physDev, descr->lines, (LPVOID)descr->bits,
3810 descr->infoWidth,descr->width,
3812 descr->rMask, descr->gMask, descr->bMask,
3813 bmpImage, descr->dibpitch);
3817 X11DRV_DIB_GetImageBits_32( descr->physDev, descr->lines, (LPVOID)descr->bits,
3818 descr->infoWidth, descr->width,
3820 descr->rMask, descr->gMask, descr->bMask,
3821 bmpImage, descr->dibpitch);
3825 WARN("(%d): Invalid depth\n", descr->infoBpp );
3829 if (old_data) bmpImage->data = old_data;
3830 if (!descr->image) X11DRV_DIB_DestroyXImage( bmpImage );
3834 /*************************************************************************
3835 * X11DRV_SetDIBitsToDevice
3838 INT CDECL X11DRV_SetDIBitsToDevice( X11DRV_PDEVICE *physDev, INT xDest, INT yDest, DWORD cx,
3839 DWORD cy, INT xSrc, INT ySrc,
3840 UINT startscan, UINT lines, LPCVOID bits,
3841 const BITMAPINFO *info, UINT coloruse )
3843 X11DRV_DIB_IMAGEBITS_DESCR descr;
3848 int rop = X11DRV_XROPfunction[GetROP2(physDev->hdc) - 1];
3850 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3851 &descr.infoBpp, &descr.compression ) == -1)
3854 top_down = (height < 0);
3855 if (top_down) height = -height;
3859 LPtoDP(physDev->hdc, &pt, 1);
3861 if (!lines || (startscan >= height)) return 0;
3862 if (!top_down && startscan + lines > height) lines = height - startscan;
3864 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3865 * and clamp all values to fit inside [startscan,startscan+lines]
3867 if (ySrc + cy <= startscan + lines)
3869 UINT y = startscan + lines - (ySrc + cy);
3870 if (ySrc < startscan) cy -= (startscan - ySrc);
3873 /* avoid getting unnecessary lines */
3875 if (y >= lines) return 0;
3880 if (y >= lines) return lines;
3881 ySrc = y; /* need to get all lines in top down mode */
3886 if (ySrc >= startscan + lines) return lines;
3887 pt.y += ySrc + cy - (startscan + lines);
3888 cy = startscan + lines - ySrc;
3890 if (cy > lines) cy = lines;
3892 if (xSrc >= width) return lines;
3893 if (xSrc + cx >= width) cx = width - xSrc;
3894 if (!cx || !cy) return lines;
3896 /* Update the pixmap from the DIB section */
3897 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
3899 X11DRV_SetupGCForText( physDev ); /* To have the correct colors */
3901 XSetFunction(gdi_display, physDev->gc, rop);
3902 wine_tsx11_unlock();
3904 switch (descr.infoBpp)
3909 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3911 physDev->depth, info, &descr.nColorMap );
3912 if (!descr.colorMap) return 0;
3913 descr.rMask = descr.gMask = descr.bMask = 0;
3917 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3918 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3919 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3925 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3926 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3927 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3932 descr.physDev = physDev;
3935 descr.palentry = NULL;
3936 descr.lines = top_down ? -lines : lines;
3937 descr.infoWidth = width;
3938 descr.depth = physDev->depth;
3939 descr.drawable = physDev->drawable;
3940 descr.gc = physDev->gc;
3943 descr.xDest = physDev->dc_rect.left + pt.x;
3944 descr.yDest = physDev->dc_rect.top + pt.y;
3947 descr.shm_mode = X11DRV_SHM_NONE;
3948 descr.dibpitch = ((width * descr.infoBpp + 31) &~31) / 8;
3949 descr.physBitmap = NULL;
3951 result = X11DRV_DIB_SetImageBits( &descr );
3953 if (descr.infoBpp <= 8)
3954 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3956 /* Update the DIBSection of the pixmap */
3957 X11DRV_UnlockDIBSection(physDev, TRUE);
3962 /***********************************************************************
3963 * SetDIBits (X11DRV.@)
3965 INT CDECL X11DRV_SetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan,
3966 UINT lines, LPCVOID bits, const BITMAPINFO *info, UINT coloruse )
3968 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3969 X11DRV_DIB_IMAGEBITS_DESCR descr;
3971 LONG width, height, tmpheight;
3974 descr.physDev = physDev;
3976 if (!physBitmap) return 0;
3978 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3979 &descr.infoBpp, &descr.compression ) == -1)
3983 if (height < 0) height = -height;
3984 if (!lines || (startscan >= height))
3987 if (!GetObjectW( hbitmap, sizeof(ds), &ds )) return 0;
3989 if (startscan + lines > height) lines = height - startscan;
3991 switch (descr.infoBpp)
3996 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3997 descr.physDev, coloruse,
3998 physBitmap->pixmap_depth,
3999 info, &descr.nColorMap );
4000 if (!descr.colorMap) return 0;
4001 descr.rMask = descr.gMask = descr.bMask = 0;
4005 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4006 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4007 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4013 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4014 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4015 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4024 descr.palentry = NULL;
4025 descr.infoWidth = width;
4026 descr.lines = tmpheight >= 0 ? lines : -lines;
4027 descr.depth = physBitmap->pixmap_depth;
4028 descr.drawable = physBitmap->pixmap;
4029 descr.gc = get_bitmap_gc(physBitmap->pixmap_depth);
4033 descr.yDest = height - startscan - lines;
4034 descr.width = ds.dsBm.bmWidth;
4035 descr.height = lines;
4036 descr.shm_mode = X11DRV_SHM_NONE;
4037 descr.dibpitch = ((descr.infoWidth * descr.infoBpp + 31) &~31) / 8;
4038 descr.physBitmap = NULL;
4039 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
4040 result = X11DRV_DIB_SetImageBits( &descr );
4042 /* optimisation for the case where the input bits are in exactly the same
4043 * format as the internal representation and copying to the app bits is
4044 * cheap - saves a round trip to the X server */
4045 if (descr.compression == BI_RGB &&
4046 coloruse == DIB_RGB_COLORS &&
4047 descr.infoBpp == ds.dsBm.bmBitsPixel &&
4048 physBitmap->base && physBitmap->size < 65536)
4050 unsigned int srcwidthb = ds.dsBm.bmWidthBytes;
4051 int dstwidthb = X11DRV_DIB_GetDIBWidthBytes( width, descr.infoBpp );
4052 LPBYTE dbits = physBitmap->base, sbits = (LPBYTE)bits + (startscan * srcwidthb);
4056 TRACE("syncing compatible set bits to app bits\n");
4057 if ((tmpheight < 0) ^ (ds.dsBmih.biHeight < 0))
4059 dbits += dstwidthb * (lines-1);
4060 dstwidthb = -dstwidthb;
4062 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4063 widthb = min(srcwidthb, abs(dstwidthb));
4064 for (y = 0; y < lines; y++, dbits += dstwidthb, sbits += srcwidthb)
4065 memcpy(dbits, sbits, widthb);
4066 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4067 physBitmap->status = DIB_Status_InSync;
4069 X11DRV_DIB_Unlock( physBitmap, TRUE );
4071 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4076 /***********************************************************************
4077 * GetDIBits (X11DRV.@)
4079 INT CDECL X11DRV_GetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan, UINT lines,
4080 LPVOID bits, BITMAPINFO *info, UINT coloruse )
4082 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
4084 X11DRV_DIB_IMAGEBITS_DESCR descr;
4085 PALETTEENTRY palette[256];
4088 LONG width, tempHeight;
4092 static const PALETTEENTRY peBlack = {0,0,0,0};
4093 static const PALETTEENTRY peWhite = {255,255,255,0};
4095 if (!physBitmap) return 0;
4096 if (!(obj_size = GetObjectW( hbitmap, sizeof(dib), &dib ))) return 0;
4098 bitmap_type = DIB_GetBitmapInfo( (BITMAPINFOHEADER*)info, &width, &tempHeight, &descr.infoBpp, &descr.compression);
4099 if (bitmap_type == -1)
4101 ERR("Invalid bitmap\n");
4105 if (physBitmap->pixmap_depth > 1)
4107 GetPaletteEntries( GetCurrentObject( physDev->hdc, OBJ_PAL ), 0, 256, palette );
4111 palette[0] = peBlack;
4112 palette[1] = peWhite;
4115 descr.lines = tempHeight;
4116 core_header = (bitmap_type == 0);
4117 colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
4119 TRACE("%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
4120 lines, dib.dsBm.bmWidth, dib.dsBm.bmHeight, width, descr.lines, startscan);
4122 if( lines > dib.dsBm.bmHeight ) lines = dib.dsBm.bmHeight;
4124 height = descr.lines;
4125 if (height < 0) height = -height;
4126 if( lines > height ) lines = height;
4127 /* Top-down images have a negative biHeight, the scanlines of these images
4128 * were inverted in X11DRV_DIB_GetImageBits_xx
4129 * To prevent this we simply change the sign of lines
4130 * (the number of scan lines to copy).
4131 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4133 if( descr.lines < 0 && lines > 0) lines = -lines;
4135 if( startscan >= dib.dsBm.bmHeight ) return 0;
4137 descr.colorMap = NULL;
4139 switch (descr.infoBpp)
4144 descr.rMask= descr.gMask = descr.bMask = 0;
4145 if(coloruse == DIB_RGB_COLORS)
4146 descr.colorMap = colorPtr;
4148 int num_colors = 1 << descr.infoBpp, i;
4151 WORD *index = colorPtr;
4152 descr.colorMap = rgb = HeapAlloc(GetProcessHeap(), 0, num_colors * sizeof(RGBQUAD));
4153 for(i = 0; i < num_colors; i++, rgb++, index++) {
4154 colref = X11DRV_PALETTE_ToLogical(physDev, X11DRV_PALETTE_ToPhysical(physDev, PALETTEINDEX(*index)));
4155 rgb->rgbRed = GetRValue(colref);
4156 rgb->rgbGreen = GetGValue(colref);
4157 rgb->rgbBlue = GetBValue(colref);
4158 rgb->rgbReserved = 0;
4164 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4165 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4166 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4170 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4171 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4172 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4176 descr.physDev = physDev;
4177 descr.palentry = palette;
4179 descr.image = physBitmap->image;
4180 descr.infoWidth = width;
4181 descr.lines = lines;
4182 descr.depth = physBitmap->pixmap_depth;
4183 descr.drawable = physBitmap->pixmap;
4184 descr.gc = get_bitmap_gc(physBitmap->pixmap_depth);
4185 descr.width = dib.dsBm.bmWidth;
4186 descr.height = dib.dsBm.bmHeight;
4190 descr.sizeImage = core_header ? 0 : info->bmiHeader.biSizeImage;
4191 descr.physBitmap = physBitmap;
4193 if (descr.lines > 0)
4195 descr.ySrc = (descr.height-1) - (startscan + (lines-1));
4199 descr.ySrc = startscan;
4201 descr.shm_mode = physBitmap->shm_mode;
4202 descr.dibpitch = (obj_size == sizeof(DIBSECTION)) ? dib.dsBm.bmWidthBytes
4203 : (((descr.infoWidth * descr.infoBpp + 31) &~31) / 8);
4205 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
4206 X11DRV_DIB_GetImageBits( &descr );
4207 X11DRV_DIB_Unlock( physBitmap, TRUE );
4209 if(!core_header && info->bmiHeader.biSizeImage == 0) /* Fill in biSizeImage */
4210 info->bmiHeader.biSizeImage = X11DRV_DIB_GetDIBImageBytes( descr.infoWidth,
4214 if (descr.compression == BI_BITFIELDS)
4216 *(DWORD *)info->bmiColors = descr.rMask;
4217 *((DWORD *)info->bmiColors + 1) = descr.gMask;
4218 *((DWORD *)info->bmiColors + 2) = descr.bMask;
4220 else if (!core_header)
4222 /* if RLE or JPEG compression were supported,
4223 * this line would be invalid. */
4224 info->bmiHeader.biCompression = 0;
4227 if(descr.colorMap != colorPtr)
4228 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4232 /***********************************************************************
4233 * X11DRV_DIB_DoCopyDIBSection
4235 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB,
4236 void *colorMap, int nColorMap,
4237 Drawable dest, GC gc,
4238 DWORD xSrc, DWORD ySrc,
4239 DWORD xDest, DWORD yDest,
4240 DWORD width, DWORD height)
4242 DIBSECTION dibSection;
4243 X11DRV_DIB_IMAGEBITS_DESCR descr;
4244 int identity[2] = {0,1};
4246 if (!GetObjectW( physBitmap->hbitmap, sizeof(dibSection), &dibSection )) return;
4248 descr.physDev = NULL;
4249 descr.palentry = NULL;
4250 descr.infoWidth = dibSection.dsBmih.biWidth;
4251 descr.infoBpp = dibSection.dsBmih.biBitCount;
4252 descr.lines = dibSection.dsBmih.biHeight;
4253 descr.image = physBitmap->image;
4254 descr.colorMap = colorMap;
4255 descr.nColorMap = nColorMap;
4256 descr.bits = dibSection.dsBm.bmBits;
4257 descr.depth = physBitmap->pixmap_depth;
4258 descr.compression = dibSection.dsBmih.biCompression;
4259 descr.physBitmap = physBitmap;
4261 if(descr.infoBpp == 1)
4262 descr.colorMap = (void*)identity;
4264 switch (descr.infoBpp)
4269 descr.rMask = descr.gMask = descr.bMask = 0;
4273 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0x7c00;
4274 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x03e0;
4275 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x001f;
4280 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0xff0000;
4281 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x00ff00;
4282 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x0000ff;
4287 descr.drawable = dest;
4291 descr.xDest = xDest;
4292 descr.yDest = yDest;
4293 descr.width = width;
4294 descr.height = height;
4295 descr.sizeImage = 0;
4297 descr.shm_mode = physBitmap->shm_mode;
4298 #ifdef HAVE_LIBXXSHM
4299 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP && physBitmap->pixmap != dest)
4301 descr.shm_mode = X11DRV_SHM_NONE;
4304 descr.dibpitch = dibSection.dsBm.bmWidthBytes;
4308 TRACE("Copying from Pixmap to DIB bits\n");
4309 X11DRV_DIB_GetImageBits( &descr );
4313 TRACE("Copying from DIB bits to Pixmap\n");
4314 X11DRV_DIB_SetImageBits( &descr );
4318 /***********************************************************************
4319 * X11DRV_DIB_CopyDIBSection
4321 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
4322 DWORD xSrc, DWORD ySrc, DWORD xDest, DWORD yDest,
4323 DWORD width, DWORD height)
4326 X_PHYSBITMAP *physBitmap;
4327 unsigned int nColorMap;
4331 TRACE("(%p,%p,%d,%d,%d,%d,%d,%d)\n", physDevSrc->hdc, physDevDst->hdc,
4332 xSrc, ySrc, xDest, yDest, width, height);
4333 /* this function is meant as an optimization for BitBlt,
4334 * not to be called otherwise */
4335 physBitmap = physDevSrc->bitmap;
4336 if (!physBitmap || GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib ) != sizeof(dib))
4338 ERR("called for non-DIBSection!?\n");
4341 /* while BitBlt should already have made sure we only get
4342 * positive values, we should check for oversize values */
4343 if ((xSrc < dib.dsBm.bmWidth) &&
4344 (ySrc < dib.dsBm.bmHeight)) {
4345 if (xSrc + width > dib.dsBm.bmWidth)
4346 width = dib.dsBm.bmWidth - xSrc;
4347 if (ySrc + height > dib.dsBm.bmHeight)
4348 height = dib.dsBm.bmHeight - ySrc;
4349 /* if the source bitmap is 8bpp or less, we're supposed to use the
4350 * DC's palette for color conversion (not the DIB color table) */
4351 if (dib.dsBm.bmBitsPixel <= 8) {
4352 HPALETTE hPalette = GetCurrentObject( physDevSrc->hdc, OBJ_PAL );
4353 if (!hPalette || (hPalette == GetStockObject(DEFAULT_PALETTE))) {
4354 /* HACK: no palette has been set in the source DC,
4355 * use the DIB colormap instead - this is necessary in some
4356 * cases since we need to do depth conversion in some places
4357 * where real Windows can just copy data straight over */
4358 x11ColorMap = physBitmap->colorMap;
4359 nColorMap = physBitmap->nColorMap;
4360 freeColorMap = FALSE;
4362 const BITMAPINFO* info = (BITMAPINFO*)&dib.dsBmih;
4365 nColorMap = X11DRV_DIB_GetColorCount(info);
4366 x11ColorMap = HeapAlloc(GetProcessHeap(), 0, nColorMap * sizeof(int));
4367 for (i = 0; i < nColorMap; i++)
4368 x11ColorMap[i] = X11DRV_PALETTE_ToPhysical(physDevSrc, PALETTEINDEX(i));
4369 freeColorMap = TRUE;
4376 freeColorMap = FALSE;
4378 /* perform the copy */
4379 X11DRV_DIB_DoCopyDIBSection(physBitmap, FALSE, x11ColorMap, nColorMap,
4380 physDevDst->drawable, physDevDst->gc, xSrc, ySrc,
4381 physDevDst->dc_rect.left + xDest, physDevDst->dc_rect.top + yDest,
4383 /* free color mapping */
4385 HeapFree(GetProcessHeap(), 0, x11ColorMap);
4389 /***********************************************************************
4390 * X11DRV_DIB_DoUpdateDIBSection
4392 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB)
4396 GetObjectW( physBitmap->hbitmap, sizeof(bitmap), &bitmap );
4397 X11DRV_DIB_DoCopyDIBSection(physBitmap, toDIB,
4398 physBitmap->colorMap, physBitmap->nColorMap,
4399 physBitmap->pixmap, get_bitmap_gc(physBitmap->pixmap_depth),
4400 0, 0, 0, 0, bitmap.bmWidth, bitmap.bmHeight);
4403 /***********************************************************************
4404 * X11DRV_DIB_FaultHandler
4406 static LONG CALLBACK X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep )
4408 X_PHYSBITMAP *physBitmap = NULL;
4412 const size_t pagemask = getpagesize() - 1;
4414 if (ep->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
4415 return EXCEPTION_CONTINUE_SEARCH;
4417 addr = (BYTE *)ep->ExceptionRecord->ExceptionInformation[1];
4419 EnterCriticalSection(&dibs_cs);
4420 LIST_FOR_EACH( ptr, &dibs_list )
4422 physBitmap = LIST_ENTRY( ptr, X_PHYSBITMAP, entry );
4423 if ((physBitmap->base <= addr) &&
4424 (addr < physBitmap->base + ((physBitmap->size + pagemask) & ~pagemask)))
4430 LeaveCriticalSection(&dibs_cs);
4432 if (!found) return EXCEPTION_CONTINUE_SEARCH;
4434 if (addr >= physBitmap->base + physBitmap->size)
4435 WARN( "%p: access to %p beyond the end of the DIB\n", physBitmap->hbitmap, addr );
4437 X11DRV_DIB_Lock( physBitmap, DIB_Status_None );
4438 if (ep->ExceptionRecord->ExceptionInformation[0] == EXCEPTION_WRITE_FAULT) {
4439 /* the app tried to write the DIB bits */
4440 X11DRV_DIB_Coerce( physBitmap, DIB_Status_AppMod);
4442 /* the app tried to read the DIB bits */
4443 X11DRV_DIB_Coerce( physBitmap, DIB_Status_InSync);
4445 X11DRV_DIB_Unlock( physBitmap, TRUE );
4447 return EXCEPTION_CONTINUE_EXECUTION;
4450 /***********************************************************************
4453 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *physBitmap, INT req)
4455 INT ret = DIB_Status_None;
4457 if (!physBitmap->image) return ret; /* not a DIB section */
4458 EnterCriticalSection(&physBitmap->lock);
4459 ret = physBitmap->status;
4461 case DIB_Status_GdiMod:
4462 /* GDI access - request to draw on pixmap */
4463 switch (physBitmap->status)
4466 case DIB_Status_None:
4467 physBitmap->p_status = DIB_Status_GdiMod;
4468 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4471 case DIB_Status_GdiMod:
4472 TRACE("GdiMod requested in status GdiMod\n" );
4473 physBitmap->p_status = DIB_Status_GdiMod;
4476 case DIB_Status_InSync:
4477 TRACE("GdiMod requested in status InSync\n" );
4478 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4479 physBitmap->status = DIB_Status_GdiMod;
4480 physBitmap->p_status = DIB_Status_InSync;
4483 case DIB_Status_AppMod:
4484 TRACE("GdiMod requested in status AppMod\n" );
4485 /* make it readonly to avoid app changing data while we copy */
4486 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4487 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4488 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4489 physBitmap->p_status = DIB_Status_AppMod;
4490 physBitmap->status = DIB_Status_GdiMod;
4495 case DIB_Status_InSync:
4496 /* App access - request access to read DIB surface */
4497 /* (typically called from signal handler) */
4498 switch (physBitmap->status)
4501 case DIB_Status_None:
4502 /* shouldn't happen from signal handler */
4505 case DIB_Status_GdiMod:
4506 TRACE("InSync requested in status GdiMod\n" );
4507 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4508 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4509 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4510 physBitmap->status = DIB_Status_InSync;
4513 case DIB_Status_InSync:
4514 TRACE("InSync requested in status InSync\n" );
4515 /* shouldn't happen from signal handler */
4518 case DIB_Status_AppMod:
4519 TRACE("InSync requested in status AppMod\n" );
4520 /* no reason to do anything here, and this
4521 * shouldn't happen from signal handler */
4526 case DIB_Status_AppMod:
4527 /* App access - request access to write DIB surface */
4528 /* (typically called from signal handler) */
4529 switch (physBitmap->status)
4532 case DIB_Status_None:
4533 /* shouldn't happen from signal handler */
4536 case DIB_Status_GdiMod:
4537 TRACE("AppMod requested in status GdiMod\n" );
4538 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4539 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4540 physBitmap->status = DIB_Status_AppMod;
4543 case DIB_Status_InSync:
4544 TRACE("AppMod requested in status InSync\n" );
4545 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4546 physBitmap->status = DIB_Status_AppMod;
4549 case DIB_Status_AppMod:
4550 TRACE("AppMod requested in status AppMod\n" );
4551 /* shouldn't happen from signal handler */
4556 /* it is up to the caller to do the copy/conversion, probably
4557 * using the return value to decide where to copy from */
4559 LeaveCriticalSection(&physBitmap->lock);
4563 /***********************************************************************
4566 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *physBitmap, INT req)
4568 INT ret = DIB_Status_None;
4570 if (!physBitmap->image) return ret; /* not a DIB section */
4571 TRACE("Locking %p from thread %04x\n", physBitmap->hbitmap, GetCurrentThreadId());
4572 EnterCriticalSection(&physBitmap->lock);
4573 ret = physBitmap->status;
4574 if (req != DIB_Status_None)
4575 X11DRV_DIB_Coerce(physBitmap, req);
4579 /***********************************************************************
4582 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *physBitmap, BOOL commit)
4584 if (!physBitmap->image) return; /* not a DIB section */
4585 switch (physBitmap->status)
4588 case DIB_Status_None:
4589 /* in case anyone is wondering, this is the "signal handler doesn't
4590 * work" case, where we always have to be ready for app access */
4592 switch (physBitmap->p_status)
4594 case DIB_Status_GdiMod:
4595 TRACE("Unlocking and syncing from GdiMod\n" );
4596 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4600 TRACE("Unlocking without needing to sync\n" );
4604 else TRACE("Unlocking with no changes\n");
4605 physBitmap->p_status = DIB_Status_None;
4608 case DIB_Status_GdiMod:
4609 TRACE("Unlocking in status GdiMod\n" );
4610 /* DIB was protected in Coerce */
4612 /* no commit, revert to InSync if applicable */
4613 if ((physBitmap->p_status == DIB_Status_InSync) ||
4614 (physBitmap->p_status == DIB_Status_AppMod)) {
4615 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4616 physBitmap->status = DIB_Status_InSync;
4621 case DIB_Status_InSync:
4622 TRACE("Unlocking in status InSync\n" );
4623 /* DIB was already protected in Coerce */
4626 case DIB_Status_AppMod:
4627 TRACE("Unlocking in status AppMod\n" );
4628 /* DIB was already protected in Coerce */
4629 /* this case is ordinary only called from the signal handler,
4630 * so we don't bother to check for !commit */
4633 LeaveCriticalSection(&physBitmap->lock);
4634 TRACE("Unlocked %p\n", physBitmap->hbitmap);
4637 /***********************************************************************
4638 * X11DRV_CoerceDIBSection
4640 INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req)
4642 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4643 return X11DRV_DIB_Coerce(physDev->bitmap, req);
4646 /***********************************************************************
4647 * X11DRV_LockDIBSection
4649 INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req)
4651 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4652 return X11DRV_DIB_Lock(physDev->bitmap, req);
4655 /***********************************************************************
4656 * X11DRV_UnlockDIBSection
4658 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
4660 if (!physDev || !physDev->bitmap) return;
4661 X11DRV_DIB_Unlock(physDev->bitmap, commit);
4665 #ifdef HAVE_LIBXXSHM
4666 /***********************************************************************
4667 * X11DRV_XShmErrorHandler
4670 static int XShmErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
4672 return 1; /* FIXME: should check event contents */
4675 /***********************************************************************
4676 * X11DRV_XShmCreateImage
4679 static XImage *X11DRV_XShmCreateImage( int width, int height, int bpp,
4680 XShmSegmentInfo* shminfo)
4684 image = XShmCreateImage(gdi_display, visual, bpp, ZPixmap, NULL, shminfo, width, height);
4687 shminfo->shmid = shmget(IPC_PRIVATE, image->bytes_per_line * height,
4689 if( shminfo->shmid != -1 )
4691 shminfo->shmaddr = shmat( shminfo->shmid, 0, 0 );
4692 if( shminfo->shmaddr != (char*)-1 )
4696 shminfo->readOnly = FALSE;
4697 X11DRV_expect_error( gdi_display, XShmErrorHandler, NULL );
4698 ok = (XShmAttach( gdi_display, shminfo ) != 0);
4699 XSync( gdi_display, False );
4700 if (X11DRV_check_error()) ok = FALSE;
4703 shmctl(shminfo->shmid, IPC_RMID, 0);
4704 return image; /* Success! */
4706 /* An error occurred */
4707 shmdt(shminfo->shmaddr);
4709 shmctl(shminfo->shmid, IPC_RMID, 0);
4710 shminfo->shmid = -1;
4712 XFlush(gdi_display);
4713 XDestroyImage(image);
4718 #endif /* HAVE_LIBXXSHM */
4721 /***********************************************************************
4722 * X11DRV_CreateDIBSection (X11DRV.@)
4724 HBITMAP CDECL X11DRV_CreateDIBSection( X11DRV_PDEVICE *physDev, HBITMAP hbitmap,
4725 const BITMAPINFO *bmi, UINT usage )
4727 X_PHYSBITMAP *physBitmap;
4729 #ifdef HAVE_LIBXXSHM
4734 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return 0;
4735 physBitmap->status = DIB_Status_None;
4737 GetObjectW( hbitmap, sizeof(dib), &dib );
4739 /* create color map */
4740 if (dib.dsBm.bmBitsPixel <= 8)
4742 physBitmap->colorMap = X11DRV_DIB_BuildColorMap( physDev,
4743 usage, dib.dsBm.bmBitsPixel, bmi,
4744 &physBitmap->nColorMap );
4747 if (!X11DRV_XRender_SetPhysBitmapDepth( physBitmap, &dib ))
4749 if (dib.dsBm.bmBitsPixel == 1)
4751 physBitmap->pixmap_depth = 1;
4752 physBitmap->trueColor = FALSE;
4756 physBitmap->pixmap_depth = screen_depth;
4757 physBitmap->pixmap_color_shifts = X11DRV_PALETTE_default_shifts;
4758 physBitmap->trueColor = (visual->class == TrueColor || visual->class == DirectColor);
4762 /* create pixmap and X image */
4764 #ifdef HAVE_LIBXXSHM
4765 physBitmap->shminfo.shmid = -1;
4767 if (XShmQueryVersion( gdi_display, &major, &minor, &pixmaps )
4768 && (physBitmap->image = X11DRV_XShmCreateImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4769 physBitmap->pixmap_depth, &physBitmap->shminfo )))
4773 physBitmap->shm_mode = X11DRV_SHM_PIXMAP;
4774 physBitmap->image->data = HeapAlloc( GetProcessHeap(), 0,
4775 dib.dsBm.bmHeight * physBitmap->image->bytes_per_line );
4779 physBitmap->shm_mode = X11DRV_SHM_IMAGE;
4780 physBitmap->image->data = physBitmap->shminfo.shmaddr;
4786 physBitmap->shm_mode = X11DRV_SHM_NONE;
4787 physBitmap->image = X11DRV_DIB_CreateXImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4788 physBitmap->pixmap_depth );
4791 #ifdef HAVE_LIBXXSHM
4792 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP)
4794 TRACE("Creating shared pixmap for bmp %p.\n", physBitmap->hbitmap);
4795 physBitmap->pixmap = XShmCreatePixmap( gdi_display, root_window,
4796 physBitmap->shminfo.shmaddr, &physBitmap->shminfo,
4797 dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4798 physBitmap->pixmap_depth );
4803 physBitmap->pixmap = XCreatePixmap( gdi_display, root_window, dib.dsBm.bmWidth,
4804 dib.dsBm.bmHeight, physBitmap->pixmap_depth );
4807 wine_tsx11_unlock();
4808 if (!physBitmap->pixmap || !physBitmap->image) return 0;
4810 if (physBitmap->trueColor)
4812 ColorShifts *shifts = &physBitmap->pixmap_color_shifts;
4814 /* When XRender is around and used, we also support dibsections in other formats like 16-bit. In these
4815 * cases we need to override the mask of XImages. The reason is that during XImage creation the masks are
4816 * derived from a 24-bit visual (no 16-bit ones are around when X runs at 24-bit). SetImageBits and other
4817 * functions rely on the color masks for proper color conversion, so we need to override the masks here. */
4818 physBitmap->image->red_mask = shifts->physicalRed.max << shifts->physicalRed.shift;
4819 physBitmap->image->green_mask = shifts->physicalGreen.max << shifts->physicalGreen.shift;
4820 physBitmap->image->blue_mask = shifts->physicalBlue.max << shifts->physicalBlue.shift;
4823 /* install fault handler */
4824 InitializeCriticalSection( &physBitmap->lock );
4825 physBitmap->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": X_PHYSBITMAP.lock");
4827 physBitmap->base = dib.dsBm.bmBits;
4828 physBitmap->size = dib.dsBmih.biSizeImage;
4829 physBitmap->status = DIB_Status_AppMod;
4832 dibs_handler = AddVectoredExceptionHandler( TRUE, X11DRV_DIB_FaultHandler );
4833 EnterCriticalSection( &dibs_cs );
4834 list_add_head( &dibs_list, &physBitmap->entry );
4835 LeaveCriticalSection( &dibs_cs );
4837 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4842 /***********************************************************************
4843 * X11DRV_DIB_DeleteDIBSection
4845 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP *physBitmap, DIBSECTION *dib)
4849 EnterCriticalSection( &dibs_cs );
4850 list_remove( &physBitmap->entry );
4851 last = list_empty( &dibs_list );
4852 LeaveCriticalSection( &dibs_cs );
4856 RemoveVectoredExceptionHandler( dibs_handler );
4857 dibs_handler = NULL;
4860 if (dib->dshSection)
4861 X11DRV_DIB_Coerce(physBitmap, DIB_Status_InSync);
4863 if (physBitmap->image)
4866 #ifdef HAVE_LIBXXSHM
4867 if (physBitmap->shminfo.shmid != -1)
4869 XShmDetach( gdi_display, &(physBitmap->shminfo) );
4870 if (physBitmap->shm_mode == X11DRV_SHM_PIXMAP) X11DRV_DIB_DestroyXImage( physBitmap->image );
4871 else XDestroyImage( physBitmap->image );
4872 shmdt( physBitmap->shminfo.shmaddr );
4873 physBitmap->shminfo.shmid = -1;
4874 physBitmap->shm_mode = X11DRV_SHM_NONE;
4878 X11DRV_DIB_DestroyXImage( physBitmap->image );
4879 wine_tsx11_unlock();
4882 HeapFree(GetProcessHeap(), 0, physBitmap->colorMap);
4883 physBitmap->lock.DebugInfo->Spare[0] = 0;
4884 DeleteCriticalSection(&physBitmap->lock);
4887 /***********************************************************************
4888 * SetDIBColorTable (X11DRV.@)
4890 UINT CDECL X11DRV_SetDIBColorTable( X11DRV_PDEVICE *physDev, UINT start, UINT count, const RGBQUAD *colors )
4894 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4896 if (!physBitmap) return 0;
4897 GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib );
4899 if (physBitmap->colorMap && start < physBitmap->nColorMap) {
4900 UINT end = count + start;
4901 if (end > physBitmap->nColorMap) end = physBitmap->nColorMap;
4903 * Changing color table might change the mapping between
4904 * DIB colors and X11 colors and thus alter the visible state
4905 * of the bitmap object.
4908 * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4909 * at least for a 1 bpp dibsection
4911 X11DRV_DIB_Lock( physBitmap, DIB_Status_AppMod );
4912 X11DRV_DIB_GenColorMap( physDev, physBitmap->colorMap, DIB_RGB_COLORS,
4913 dib.dsBm.bmBitsPixel,
4914 TRUE, colors, start, end );
4915 X11DRV_DIB_Unlock( physBitmap, TRUE );
4922 /***********************************************************************
4923 * X11DRV_DIB_CreateDIBFromBitmap
4925 * Allocates a packed DIB and copies the bitmap data into it.
4927 HGLOBAL X11DRV_DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
4932 LPBITMAPINFOHEADER pbmiHeader;
4933 unsigned int cDataSize, cPackedSize, OffsetBits;
4936 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
4939 * A packed DIB contains a BITMAPINFO structure followed immediately by
4940 * an optional color palette and the pixel data.
4943 /* Calculate the size of the packed DIB */
4944 cDataSize = X11DRV_DIB_GetDIBWidthBytes( bmp.bmWidth, bmp.bmBitsPixel ) * abs( bmp.bmHeight );
4945 cPackedSize = sizeof(BITMAPINFOHEADER)
4946 + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
4948 /* Get the offset to the bits */
4949 OffsetBits = cPackedSize - cDataSize;
4951 /* Allocate the packed DIB */
4952 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
4953 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
4957 WARN("Could not allocate packed DIB!\n");
4961 /* A packed DIB starts with a BITMAPINFOHEADER */
4962 pPackedDIB = GlobalLock(hPackedDIB);
4963 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
4965 /* Init the BITMAPINFOHEADER */
4966 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
4967 pbmiHeader->biWidth = bmp.bmWidth;
4968 pbmiHeader->biHeight = bmp.bmHeight;
4969 pbmiHeader->biPlanes = 1;
4970 pbmiHeader->biBitCount = bmp.bmBitsPixel;
4971 pbmiHeader->biCompression = BI_RGB;
4972 pbmiHeader->biSizeImage = 0;
4973 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
4974 pbmiHeader->biClrUsed = 0;
4975 pbmiHeader->biClrImportant = 0;
4977 /* Retrieve the DIB bits from the bitmap and fill in the
4978 * DIB color table if present */
4980 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
4981 hBmp, /* Handle to bitmap */
4982 0, /* First scan line to set in dest bitmap */
4983 bmp.bmHeight, /* Number of scan lines to copy */
4984 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
4985 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
4986 0); /* RGB or palette index */
4987 GlobalUnlock(hPackedDIB);
4989 /* Cleanup if GetDIBits failed */
4990 if (nLinesCopied != bmp.bmHeight)
4992 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
4993 GlobalFree(hPackedDIB);
5000 /**************************************************************************
5001 * X11DRV_DIB_CreateDIBFromPixmap
5003 * Allocates a packed DIB and copies the Pixmap data into it.
5004 * The Pixmap passed in is deleted after the conversion.
5006 HGLOBAL X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap, HDC hdc)
5009 X_PHYSBITMAP *physBitmap;
5012 HGLOBAL hPackedDIB = 0;
5014 int x,y; /* Unused */
5015 unsigned border_width; /* Unused */
5016 unsigned int depth, width, height;
5018 /* Get the Pixmap dimensions and bit depth */
5020 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
5021 &border_width, &depth)) depth = 0;
5022 wine_tsx11_unlock();
5023 if (!depth) return 0;
5025 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
5026 width, height, depth);
5029 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
5030 * and make it a container for the pixmap passed.
5032 if (!(hBmp = CreateBitmap( width, height, 1, depth_to_bpp(depth), NULL ))) return 0;
5034 /* force bitmap to be owned by a screen DC */
5035 hdcMem = CreateCompatibleDC( hdc );
5036 SelectObject( hdcMem, SelectObject( hdcMem, hBmp ));
5039 physBitmap = X11DRV_get_phys_bitmap( hBmp );
5041 /* swap the new pixmap in */
5042 orig_pixmap = physBitmap->pixmap;
5043 physBitmap->pixmap = pixmap;
5046 * Create a packed DIB from the Pixmap wrapper bitmap created above.
5047 * A packed DIB contains a BITMAPINFO structure followed immediately by
5048 * an optional color palette and the pixel data.
5050 hPackedDIB = X11DRV_DIB_CreateDIBFromBitmap(hdc, hBmp);
5052 /* we can now get rid of the HBITMAP and its original pixmap */
5053 physBitmap->pixmap = orig_pixmap;
5056 TRACE("\tReturning packed DIB %p\n", hPackedDIB);
5061 /**************************************************************************
5062 * X11DRV_DIB_CreatePixmapFromDIB
5064 * Creates a Pixmap from a packed DIB
5066 Pixmap X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB, HDC hdc )
5069 X_PHYSBITMAP *physBitmap;
5073 /* Create a DDB from the DIB */
5075 pbmi = GlobalLock(hPackedDIB);
5076 hBmp = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT,
5077 (LPBYTE)pbmi + bitmap_info_size( pbmi, DIB_RGB_COLORS ),
5078 pbmi, DIB_RGB_COLORS);
5079 GlobalUnlock(hPackedDIB);
5081 /* clear the physBitmap so that we can steal its pixmap */
5082 physBitmap = X11DRV_get_phys_bitmap( hBmp );
5083 pixmap = physBitmap->pixmap;
5084 physBitmap->pixmap = 0;
5086 /* Delete the DDB we created earlier now that we have stolen its pixmap */
5089 TRACE("Returning Pixmap %lx\n", pixmap);