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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <X11/extensions/XShm.h>
26 # ifdef HAVE_SYS_SHM_H
29 # ifdef HAVE_SYS_IPC_H
32 #endif /* defined(HAVE_LIBXXSHM) */
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
46 static struct list dibs_list = LIST_INIT(dibs_list);
48 static CRITICAL_SECTION dibs_cs;
49 static CRITICAL_SECTION_DEBUG dibs_cs_debug =
52 { &dibs_cs_debug.ProcessLocksList, &dibs_cs_debug.ProcessLocksList },
53 0, 0, { (DWORD_PTR)(__FILE__ ": dibs_cs") }
55 static CRITICAL_SECTION dibs_cs = { &dibs_cs_debug, -1, 0, 0, 0, 0 };
57 static PVOID dibs_handler;
59 static int ximageDepthTable[32];
61 /* This structure holds the arguments for DIB_SetImageBits() */
64 X11DRV_PDEVICE *physDev;
67 PALETTEENTRY *palentry;
89 } X11DRV_DIB_IMAGEBITS_DESCR;
94 RLE_EOL = 0, /* End of line */
95 RLE_END = 1, /* End of bitmap */
96 RLE_DELTA = 2 /* Delta */
100 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *,INT,BOOL);
101 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *,INT,BOOL);
102 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *,BOOL);
105 Some of the following helper functions are duplicated in
109 /***********************************************************************
110 * X11DRV_DIB_GetXImageWidthBytes
112 * Return the width of an X image in bytes
114 inline static int X11DRV_DIB_GetXImageWidthBytes( int width, int depth )
116 if (!depth || depth > 32) goto error;
118 if (!ximageDepthTable[depth-1])
120 XImage *testimage = XCreateImage( gdi_display, visual, depth,
121 ZPixmap, 0, NULL, 1, 1, 32, 20 );
124 ximageDepthTable[depth-1] = testimage->bits_per_pixel;
125 XDestroyImage( testimage );
127 else ximageDepthTable[depth-1] = -1;
129 if (ximageDepthTable[depth-1] != -1)
130 return (4 * ((width * ximageDepthTable[depth-1] + 31) / 32));
133 WARN( "(%d): Unsupported depth\n", depth );
138 /***********************************************************************
139 * X11DRV_DIB_GetDIBWidthBytes
141 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
143 static int X11DRV_DIB_GetDIBWidthBytes( int width, int depth )
149 case 1: words = (width + 31) / 32; break;
150 case 4: words = (width + 7) / 8; break;
151 case 8: words = (width + 3) / 4; break;
153 case 16: words = (width + 1) / 2; break;
154 case 24: words = (width * 3 + 3) / 4; break;
156 WARN("(%d): Unsupported depth\n", depth );
165 /***********************************************************************
166 * X11DRV_DIB_GetDIBImageBytes
168 * Return the number of bytes used to hold the image in a DIB bitmap.
170 static int X11DRV_DIB_GetDIBImageBytes( int width, int height, int depth )
172 return X11DRV_DIB_GetDIBWidthBytes( width, depth ) * abs( height );
176 /***********************************************************************
177 * X11DRV_DIB_BitmapInfoSize
179 * Return the size of the bitmap info structure including color table.
181 int X11DRV_DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
185 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
187 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
188 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
189 return sizeof(BITMAPCOREHEADER) + colors *
190 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
192 else /* assume BITMAPINFOHEADER */
194 colors = info->bmiHeader.biClrUsed;
195 if (!colors && (info->bmiHeader.biBitCount <= 8))
196 colors = 1 << info->bmiHeader.biBitCount;
197 return sizeof(BITMAPINFOHEADER) + colors *
198 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
203 /***********************************************************************
204 * X11DRV_DIB_CreateXImage
208 XImage *X11DRV_DIB_CreateXImage( int width, int height, int depth )
214 width_bytes = X11DRV_DIB_GetXImageWidthBytes( width, depth );
215 image = XCreateImage( gdi_display, visual, depth, ZPixmap, 0,
216 calloc( height, width_bytes ),
217 width, height, 32, width_bytes );
223 /***********************************************************************
224 * DIB_GetBitmapInfoEx
226 * Get the info from a bitmap header.
227 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
229 static int DIB_GetBitmapInfoEx( const BITMAPINFOHEADER *header, LONG *width,
230 LONG *height, WORD *planes, WORD *bpp,
231 WORD *compr, DWORD *size )
233 if (header->biSize == sizeof(BITMAPCOREHEADER))
235 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
236 *width = core->bcWidth;
237 *height = core->bcHeight;
238 *planes = core->bcPlanes;
239 *bpp = core->bcBitCount;
244 if (header->biSize >= sizeof(BITMAPINFOHEADER))
246 *width = header->biWidth;
247 *height = header->biHeight;
248 *planes = header->biPlanes;
249 *bpp = header->biBitCount;
250 *compr = header->biCompression;
251 *size = header->biSizeImage;
254 ERR("(%ld): unknown/wrong size for header\n", header->biSize );
259 /***********************************************************************
262 * Get the info from a bitmap header.
263 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
265 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
266 LONG *height, WORD *bpp, WORD *compr )
271 return DIB_GetBitmapInfoEx( header, width, height, &planes, bpp, compr, &size);
275 /***********************************************************************
276 * X11DRV_DIB_GenColorMap
278 * Fills the color map of a bitmap palette. Should not be called
279 * for a >8-bit deep bitmap.
281 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
282 WORD coloruse, WORD depth, BOOL quads,
283 const void *colorPtr, int start, int end )
287 if (coloruse == DIB_RGB_COLORS)
291 const RGBQUAD * rgb = (const RGBQUAD *)colorPtr;
293 if (depth == 1) /* Monochrome */
294 for (i = start; i < end; i++, rgb++)
295 colorMapping[i] = (rgb->rgbRed + rgb->rgbGreen +
296 rgb->rgbBlue > 255*3/2);
298 for (i = start; i < end; i++, rgb++)
299 colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, RGB(rgb->rgbRed,
305 const RGBTRIPLE * rgb = (const RGBTRIPLE *)colorPtr;
307 if (depth == 1) /* Monochrome */
308 for (i = start; i < end; i++, rgb++)
309 colorMapping[i] = (rgb->rgbtRed + rgb->rgbtGreen +
310 rgb->rgbtBlue > 255*3/2);
312 for (i = start; i < end; i++, rgb++)
313 colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, RGB(rgb->rgbtRed,
318 else /* DIB_PAL_COLORS */
321 const WORD * index = (const WORD *)colorPtr;
323 for (i = start; i < end; i++, index++)
324 colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(*index) );
326 for (i = start; i < end; i++)
327 colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(i) );
334 /***********************************************************************
335 * X11DRV_DIB_BuildColorMap
337 * Build the color map from the bitmap palette. Should not be called
338 * for a >8-bit deep bitmap.
340 int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE *physDev, WORD coloruse, WORD depth,
341 const BITMAPINFO *info, int *nColors )
345 const void *colorPtr;
348 isInfo = info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER);
352 colors = info->bmiHeader.biClrUsed;
353 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
357 colors = 1 << ((const BITMAPCOREHEADER *)info)->bcBitCount;
360 colorPtr = (const BYTE*) info + (WORD) info->bmiHeader.biSize;
364 ERR("called with >256 colors!\n");
368 /* just so CopyDIBSection doesn't have to create an identity palette */
369 if (coloruse == (WORD)-1) colorPtr = NULL;
371 if (!(colorMapping = HeapAlloc(GetProcessHeap(), 0, colors * sizeof(int) )))
375 return X11DRV_DIB_GenColorMap( physDev, colorMapping, coloruse, depth,
376 isInfo, colorPtr, 0, colors);
379 /***********************************************************************
380 * X11DRV_DIB_BuildColorTable
382 * Build the dib color table. This either keeps a copy of the bmiColors array if
383 * usage is DIB_RGB_COLORS, or looks up the palette indicies if usage is
385 * Should not be called for a >8-bit deep bitmap.
387 static RGBQUAD *X11DRV_DIB_BuildColorTable( X11DRV_PDEVICE *physDev, WORD coloruse, WORD depth,
388 const BITMAPINFO *info )
393 BOOL core_info = info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER);
397 colors = 1 << ((const BITMAPCOREINFO*) info)->bmciHeader.bcBitCount;
401 colors = info->bmiHeader.biClrUsed;
402 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
406 ERR("called with >256 colors!\n");
410 if (!(colorTable = HeapAlloc(GetProcessHeap(), 0, colors * sizeof(RGBQUAD) )))
413 if(coloruse == DIB_RGB_COLORS)
417 /* Convert RGBTRIPLEs to RGBQUADs */
418 for (i=0; i < colors; i++)
420 colorTable[i].rgbRed = ((const BITMAPCOREINFO*) info)->bmciColors[i].rgbtRed;
421 colorTable[i].rgbGreen = ((const BITMAPCOREINFO*) info)->bmciColors[i].rgbtGreen;
422 colorTable[i].rgbBlue = ((const BITMAPCOREINFO*) info)->bmciColors[i].rgbtBlue;
423 colorTable[i].rgbReserved = 0;
428 memcpy(colorTable, (const BYTE*) info + (WORD) info->bmiHeader.biSize, colors * sizeof(RGBQUAD));
433 HPALETTE hpal = GetCurrentObject(physDev->hdc, OBJ_PAL);
434 PALETTEENTRY * pal_ents;
435 const WORD *index = (const WORD*) ((const BYTE*) info + (WORD) info->bmiHeader.biSize);
436 int logcolors, entry;
438 logcolors = GetPaletteEntries( hpal, 0, 0, NULL );
439 pal_ents = HeapAlloc(GetProcessHeap(), 0, logcolors * sizeof(*pal_ents));
440 logcolors = GetPaletteEntries( hpal, 0, logcolors, pal_ents );
442 for(i = 0; i < colors; i++, index++)
444 entry = *index % logcolors;
445 colorTable[i].rgbRed = pal_ents[entry].peRed;
446 colorTable[i].rgbGreen = pal_ents[entry].peGreen;
447 colorTable[i].rgbBlue = pal_ents[entry].peBlue;
448 colorTable[i].rgbReserved = 0;
451 HeapFree(GetProcessHeap(), 0, pal_ents);
457 /***********************************************************************
458 * X11DRV_DIB_MapColor
460 static int X11DRV_DIB_MapColor( int *physMap, int nPhysMap, int phys, int oldcol )
464 if ((oldcol < nPhysMap) && (physMap[oldcol] == phys))
467 for (color = 0; color < nPhysMap; color++)
468 if (physMap[color] == phys)
471 WARN("Strange color %08x\n", phys);
476 /*********************************************************************
477 * X11DRV_DIB_GetNearestIndex
479 * Helper for X11DRV_DIB_GetDIBits.
480 * Returns the nearest colour table index for a given RGB.
481 * Nearest is defined by minimizing the sum of the squares.
483 static INT X11DRV_DIB_GetNearestIndex(RGBQUAD *colormap, int numColors, BYTE r, BYTE g, BYTE b)
485 INT i, best = -1, diff, bestdiff = -1;
488 for(color = colormap, i = 0; i < numColors; color++, i++) {
489 diff = (r - color->rgbRed) * (r - color->rgbRed) +
490 (g - color->rgbGreen) * (g - color->rgbGreen) +
491 (b - color->rgbBlue) * (b - color->rgbBlue);
494 if(best == -1 || diff < bestdiff) {
501 /*********************************************************************
502 * X11DRV_DIB_MaskToShift
504 * Helper for X11DRV_DIB_GetDIBits.
505 * Returns the by how many bits to shift a given color so that it is
506 * in the proper position.
508 INT X11DRV_DIB_MaskToShift(DWORD mask)
516 while ((mask&1)==0) {
523 /***********************************************************************
524 * X11DRV_DIB_CheckMask
526 * Check RGB mask if it is either 0 or matches visual's mask.
528 static inline int X11DRV_DIB_CheckMask(int red_mask, int green_mask, int blue_mask)
530 return ( red_mask == 0 && green_mask == 0 && blue_mask == 0 ) ||
531 ( red_mask == visual->red_mask && green_mask == visual->green_mask &&
532 blue_mask == visual->blue_mask );
535 /***********************************************************************
536 * X11DRV_DIB_SetImageBits_1
538 * SetDIBits for a 1-bit deep DIB.
540 static void X11DRV_DIB_SetImageBits_1( int lines, const BYTE *srcbits,
541 DWORD srcwidth, DWORD dstwidth, int left,
542 int *colors, XImage *bmpImage, DWORD linebytes)
551 srcbits = srcbits + linebytes * (lines - 1);
552 linebytes = -linebytes;
555 if ((extra = (left & 7)) != 0) {
559 srcbits += left >> 3;
560 width = min(srcwidth, dstwidth);
562 /* ==== pal 1 dib -> any bmp format ==== */
563 for (h = lines-1; h >=0; h--) {
565 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
566 for (i = width/8, x = left; i > 0; i--) {
568 XPutPixel( bmpImage, x++, h, colors[ srcval >> 7] );
569 XPutPixel( bmpImage, x++, h, colors[(srcval >> 6) & 1] );
570 XPutPixel( bmpImage, x++, h, colors[(srcval >> 5) & 1] );
571 XPutPixel( bmpImage, x++, h, colors[(srcval >> 4) & 1] );
572 XPutPixel( bmpImage, x++, h, colors[(srcval >> 3) & 1] );
573 XPutPixel( bmpImage, x++, h, colors[(srcval >> 2) & 1] );
574 XPutPixel( bmpImage, x++, h, colors[(srcval >> 1) & 1] );
575 XPutPixel( bmpImage, x++, h, colors[ srcval & 1] );
581 case 7: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
582 case 6: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
583 case 5: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
584 case 4: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
585 case 3: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
586 case 2: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
587 case 1: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]);
590 srcbits += linebytes;
594 /***********************************************************************
595 * X11DRV_DIB_GetImageBits_1
597 * GetDIBits for a 1-bit deep DIB.
599 static void X11DRV_DIB_GetImageBits_1( int lines, BYTE *dstbits,
600 DWORD dstwidth, DWORD srcwidth,
601 RGBQUAD *colors, PALETTEENTRY *srccolors,
602 XImage *bmpImage, DWORD linebytes )
605 int h, width = min(dstwidth, srcwidth);
609 dstbits = dstbits + linebytes * (lines - 1);
610 linebytes = -linebytes;
613 switch (bmpImage->depth)
617 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
619 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
622 for (h=lines-1; h>=0; h--) {
626 for (x=0; x<width; x++) {
628 srcval=srccolors[XGetPixel(bmpImage, x, h)];
629 dstval|=(X11DRV_DIB_GetNearestIndex
633 srcval.peBlue) << (7 - (x & 7)));
642 dstbits += linebytes;
650 if (X11DRV_DIB_CheckMask(bmpImage->red_mask, bmpImage->green_mask, bmpImage->blue_mask)
652 /* ==== pal 8 bmp -> pal 1 dib ==== */
654 const BYTE* srcpixel;
657 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
659 for (h=0; h<lines; h++) {
664 for (x=0; x<width; x++) {
666 srcval=srccolors[(int)*srcpixel++];
667 dstval|=(X11DRV_DIB_GetNearestIndex
671 srcval.peBlue) << (7-(x&7)) );
680 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
681 dstbits += linebytes;
692 const WORD* srcpixel;
695 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
697 if (bmpImage->green_mask==0x03e0) {
698 if (bmpImage->red_mask==0x7c00) {
699 /* ==== rgb 555 bmp -> pal 1 dib ==== */
700 for (h=0; h<lines; h++) {
705 for (x=0; x<width; x++) {
708 dstval|=(X11DRV_DIB_GetNearestIndex
710 ((srcval >> 7) & 0xf8) | /* r */
711 ((srcval >> 12) & 0x07),
712 ((srcval >> 2) & 0xf8) | /* g */
713 ((srcval >> 7) & 0x07),
714 ((srcval << 3) & 0xf8) | /* b */
715 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
724 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
725 dstbits += linebytes;
727 } else if (bmpImage->blue_mask==0x7c00) {
728 /* ==== bgr 555 bmp -> pal 1 dib ==== */
729 for (h=0; h<lines; h++) {
734 for (x=0; x<width; x++) {
737 dstval|=(X11DRV_DIB_GetNearestIndex
739 ((srcval << 3) & 0xf8) | /* r */
740 ((srcval >> 2) & 0x07),
741 ((srcval >> 2) & 0xf8) | /* g */
742 ((srcval >> 7) & 0x07),
743 ((srcval >> 7) & 0xf8) | /* b */
744 ((srcval >> 12) & 0x07) ) << (7-(x&7)) );
753 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
754 dstbits += linebytes;
759 } else if (bmpImage->green_mask==0x07e0) {
760 if (bmpImage->red_mask==0xf800) {
761 /* ==== rgb 565 bmp -> pal 1 dib ==== */
762 for (h=0; h<lines; h++) {
767 for (x=0; x<width; x++) {
770 dstval|=(X11DRV_DIB_GetNearestIndex
772 ((srcval >> 8) & 0xf8) | /* r */
773 ((srcval >> 13) & 0x07),
774 ((srcval >> 3) & 0xfc) | /* g */
775 ((srcval >> 9) & 0x03),
776 ((srcval << 3) & 0xf8) | /* b */
777 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
786 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
787 dstbits += linebytes;
789 } else if (bmpImage->blue_mask==0xf800) {
790 /* ==== bgr 565 bmp -> pal 1 dib ==== */
791 for (h=0; h<lines; h++) {
796 for (x=0; x<width; x++) {
799 dstval|=(X11DRV_DIB_GetNearestIndex
801 ((srcval << 3) & 0xf8) | /* r */
802 ((srcval >> 2) & 0x07),
803 ((srcval >> 3) & 0xfc) | /* g */
804 ((srcval >> 9) & 0x03),
805 ((srcval >> 8) & 0xf8) | /* b */
806 ((srcval >> 13) & 0x07) ) << (7-(x&7)) );
815 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
816 dstbits += linebytes;
835 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
836 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
838 if (bmpImage->green_mask!=0x00ff00 ||
839 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
841 } else if (bmpImage->blue_mask==0xff) {
842 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
843 for (h=0; h<lines; h++) {
848 for (x=0; x<width; x++) {
849 dstval|=(X11DRV_DIB_GetNearestIndex
853 srcbyte[0]) << (7-(x&7)) );
854 srcbyte+=bytes_per_pixel;
863 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
864 dstbits += linebytes;
867 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
868 for (h=0; h<lines; h++) {
873 for (x=0; x<width; x++) {
874 dstval|=(X11DRV_DIB_GetNearestIndex
878 srcbyte[2]) << (7-(x&7)) );
879 srcbyte+=bytes_per_pixel;
888 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
889 dstbits += linebytes;
899 unsigned long white = (1 << bmpImage->bits_per_pixel) - 1;
901 /* ==== any bmp format -> pal 1 dib ==== */
902 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB\n",
903 bmpImage->bits_per_pixel, bmpImage->red_mask,
904 bmpImage->green_mask, bmpImage->blue_mask );
906 for (h=lines-1; h>=0; h--) {
910 for (x=0; x<width; x++) {
911 dstval|=(XGetPixel( bmpImage, x, h) >= white) << (7 - (x&7));
920 dstbits += linebytes;
927 /***********************************************************************
928 * X11DRV_DIB_SetImageBits_4
930 * SetDIBits for a 4-bit deep DIB.
932 static void X11DRV_DIB_SetImageBits_4( int lines, const BYTE *srcbits,
933 DWORD srcwidth, DWORD dstwidth, int left,
934 int *colors, XImage *bmpImage, DWORD linebytes)
942 srcbits = srcbits + linebytes * (lines - 1);
943 linebytes = -linebytes;
950 srcbits += left >> 1;
951 width = min(srcwidth, dstwidth);
953 /* ==== pal 4 dib -> any bmp format ==== */
954 for (h = lines-1; h >= 0; h--) {
956 for (i = width/2, x = left; i > 0; i--) {
957 BYTE srcval=*srcbyte++;
958 XPutPixel( bmpImage, x++, h, colors[srcval >> 4] );
959 XPutPixel( bmpImage, x++, h, colors[srcval & 0x0f] );
962 XPutPixel( bmpImage, x, h, colors[*srcbyte >> 4] );
963 srcbits += linebytes;
969 /***********************************************************************
970 * X11DRV_DIB_GetImageBits_4
972 * GetDIBits for a 4-bit deep DIB.
974 static void X11DRV_DIB_GetImageBits_4( int lines, BYTE *dstbits,
975 DWORD srcwidth, DWORD dstwidth,
976 RGBQUAD *colors, PALETTEENTRY *srccolors,
977 XImage *bmpImage, DWORD linebytes )
980 int h, width = min(srcwidth, dstwidth);
986 dstbits = dstbits + ( linebytes * (lines-1) );
987 linebytes = -linebytes;
992 switch (bmpImage->depth) {
995 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
997 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
1000 for (h = lines-1; h >= 0; h--) {
1004 for (x = 0; x < width; x++) {
1005 PALETTEENTRY srcval;
1006 srcval=srccolors[XGetPixel(bmpImage, x, h)];
1007 dstval|=(X11DRV_DIB_GetNearestIndex
1011 srcval.peBlue) << (4-((x&1)<<2)));
1020 dstbits += linebytes;
1028 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1030 /* ==== pal 8 bmp -> pal 4 dib ==== */
1031 const void* srcbits;
1032 const BYTE *srcpixel;
1035 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1036 for (h=0; h<lines; h++) {
1041 for (x=0; x<width; x++) {
1042 PALETTEENTRY srcval;
1043 srcval = srccolors[(int)*srcpixel++];
1044 dstval|=(X11DRV_DIB_GetNearestIndex
1048 srcval.peBlue) << (4*(1-(x&1))) );
1057 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1058 dstbits += linebytes;
1068 const void* srcbits;
1069 const WORD* srcpixel;
1072 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1074 if (bmpImage->green_mask==0x03e0) {
1075 if (bmpImage->red_mask==0x7c00) {
1076 /* ==== rgb 555 bmp -> pal 4 dib ==== */
1077 for (h=0; h<lines; h++) {
1082 for (x=0; x<width; x++) {
1085 dstval|=(X11DRV_DIB_GetNearestIndex
1087 ((srcval >> 7) & 0xf8) | /* r */
1088 ((srcval >> 12) & 0x07),
1089 ((srcval >> 2) & 0xf8) | /* g */
1090 ((srcval >> 7) & 0x07),
1091 ((srcval << 3) & 0xf8) | /* b */
1092 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1101 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1102 dstbits += linebytes;
1104 } else if (bmpImage->blue_mask==0x7c00) {
1105 /* ==== bgr 555 bmp -> pal 4 dib ==== */
1106 for (h=0; h<lines; h++) {
1111 for (x=0; x<width; x++) {
1114 dstval|=(X11DRV_DIB_GetNearestIndex
1116 ((srcval << 3) & 0xf8) | /* r */
1117 ((srcval >> 2) & 0x07),
1118 ((srcval >> 2) & 0xf8) | /* g */
1119 ((srcval >> 7) & 0x07),
1120 ((srcval >> 7) & 0xf8) | /* b */
1121 ((srcval >> 12) & 0x07) ) << ((1-(x&1))<<2) );
1130 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1131 dstbits += linebytes;
1136 } else if (bmpImage->green_mask==0x07e0) {
1137 if (bmpImage->red_mask==0xf800) {
1138 /* ==== rgb 565 bmp -> pal 4 dib ==== */
1139 for (h=0; h<lines; h++) {
1144 for (x=0; x<width; x++) {
1147 dstval|=(X11DRV_DIB_GetNearestIndex
1149 ((srcval >> 8) & 0xf8) | /* r */
1150 ((srcval >> 13) & 0x07),
1151 ((srcval >> 3) & 0xfc) | /* g */
1152 ((srcval >> 9) & 0x03),
1153 ((srcval << 3) & 0xf8) | /* b */
1154 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1163 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1164 dstbits += linebytes;
1166 } else if (bmpImage->blue_mask==0xf800) {
1167 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1168 for (h=0; h<lines; h++) {
1173 for (x=0; x<width; x++) {
1176 dstval|=(X11DRV_DIB_GetNearestIndex
1178 ((srcval << 3) & 0xf8) | /* r */
1179 ((srcval >> 2) & 0x07),
1180 ((srcval >> 3) & 0xfc) | /* g */
1181 ((srcval >> 9) & 0x03),
1182 ((srcval >> 8) & 0xf8) | /* b */
1183 ((srcval >> 13) & 0x07) ) << ((1-(x&1))<<2) );
1192 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1193 dstbits += linebytes;
1205 if (bmpImage->bits_per_pixel==24) {
1206 const void* srcbits;
1207 const BYTE *srcbyte;
1210 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1212 if (bmpImage->green_mask!=0x00ff00 ||
1213 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1215 } else if (bmpImage->blue_mask==0xff) {
1216 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1217 for (h=0; h<lines; h++) {
1220 for (x=0; x<width/2; x++) {
1221 /* Do 2 pixels at a time */
1222 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1227 X11DRV_DIB_GetNearestIndex
1235 /* And the the odd pixel */
1236 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1242 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1243 dstbits += linebytes;
1246 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1247 for (h=0; h<lines; h++) {
1250 for (x=0; x<width/2; x++) {
1251 /* Do 2 pixels at a time */
1252 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1257 X11DRV_DIB_GetNearestIndex
1265 /* And the the odd pixel */
1266 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1272 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1273 dstbits += linebytes;
1282 const void* srcbits;
1283 const BYTE *srcbyte;
1286 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1288 if (bmpImage->green_mask!=0x00ff00 ||
1289 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1291 } else if (bmpImage->blue_mask==0xff) {
1292 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1293 for (h=0; h<lines; h++) {
1296 for (x=0; x<width/2; x++) {
1297 /* Do 2 pixels at a time */
1298 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1303 X11DRV_DIB_GetNearestIndex
1311 /* And the the odd pixel */
1312 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1318 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1319 dstbits += linebytes;
1322 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1323 for (h=0; h<lines; h++) {
1326 for (x=0; x<width/2; x++) {
1327 /* Do 2 pixels at a time */
1328 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1333 X11DRV_DIB_GetNearestIndex
1341 /* And the the odd pixel */
1342 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1348 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1349 dstbits += linebytes;
1360 /* ==== any bmp format -> pal 4 dib ==== */
1361 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1362 bmpImage->bits_per_pixel, bmpImage->red_mask,
1363 bmpImage->green_mask, bmpImage->blue_mask );
1364 for (h=lines-1; h>=0; h--) {
1366 for (x=0; x<(width & ~1); x+=2) {
1367 *dstbyte++=(X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4) |
1368 X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x+1, h), 0);
1371 *dstbyte=(X11DRV_DIB_MapColor((int *)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4);
1373 dstbits += linebytes;
1380 /***********************************************************************
1381 * X11DRV_DIB_SetImageBits_RLE4
1383 * SetDIBits for a 4-bit deep compressed DIB.
1385 static void X11DRV_DIB_SetImageBits_RLE4( int lines, const BYTE *bits,
1386 DWORD srcwidth, DWORD dstwidth,
1387 int left, int *colors,
1390 unsigned int x = 0, width = min(srcwidth, dstwidth);
1391 int y = lines - 1, c, length;
1392 const BYTE *begin = bits;
1397 if (length) { /* encoded */
1400 if (x >= (left + width)) break;
1401 if( x >= left) XPutPixel(bmpImage, x, y, colors[c >> 4]);
1403 if (!length--) break;
1404 if (x >= (left + width)) break;
1405 if( x >= left) XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1425 default: /* absolute */
1428 if (x >= left && x < (left + width))
1429 XPutPixel(bmpImage, x, y, colors[c >> 4]);
1431 if (!length--) break;
1432 if (x >= left && x < (left + width))
1433 XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1436 if ((bits - begin) & 1)
1445 /***********************************************************************
1446 * X11DRV_DIB_SetImageBits_8
1448 * SetDIBits for an 8-bit deep DIB.
1450 static void X11DRV_DIB_SetImageBits_8( int lines, const BYTE *srcbits,
1451 DWORD srcwidth, DWORD dstwidth, int left,
1452 const int *colors, XImage *bmpImage,
1456 int h, width = min(srcwidth, dstwidth);
1457 const BYTE* srcbyte;
1463 srcbits = srcbits + linebytes * (lines-1);
1464 linebytes = -linebytes;
1469 switch (bmpImage->depth) {
1472 #if defined(__i386__) && defined(__GNUC__)
1473 /* Some X servers might have 32 bit/ 16bit deep pixel */
1474 if (lines && width && (bmpImage->bits_per_pixel == 16) &&
1475 (ImageByteOrder(gdi_display)==LSBFirst) )
1477 dstbits=(BYTE*)bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1478 /* FIXME: Does this really handle all these cases correctly? */
1479 /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1480 for (h = lines ; h--; ) {
1481 int _cl1,_cl2; /* temp outputs for asm below */
1482 /* Borrowed from DirectDraw */
1483 __asm__ __volatile__(
1488 " movw (%%edx,%%eax,4),%%ax\n"
1490 " xor %%eax,%%eax\n"
1492 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1497 :"eax", "cc", "memory"
1499 srcbyte = (srcbits += linebytes);
1500 dstbits -= bmpImage->bytes_per_line;
1508 #if defined(__i386__) && defined(__GNUC__)
1509 if (lines && width && (bmpImage->bits_per_pixel == 32) &&
1510 (ImageByteOrder(gdi_display)==LSBFirst) )
1512 dstbits=(BYTE*)bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
1513 /* FIXME: Does this really handle both cases correctly? */
1514 /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1515 for (h = lines ; h--; ) {
1516 int _cl1,_cl2; /* temp outputs for asm below */
1517 /* Borrowed from DirectDraw */
1518 __asm__ __volatile__(
1523 " movl (%%edx,%%eax,4),%%eax\n"
1525 " xor %%eax,%%eax\n"
1527 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1532 :"eax", "cc", "memory"
1534 srcbyte = (srcbits += linebytes);
1535 dstbits -= bmpImage->bytes_per_line;
1542 break; /* use slow generic case below */
1545 /* ==== pal 8 dib -> any bmp format ==== */
1546 for (h=lines-1; h>=0; h--) {
1547 for (x=left; x<width+left; x++) {
1548 XPutPixel(bmpImage, x, h, colors[*srcbyte++]);
1550 srcbyte = (srcbits += linebytes);
1554 /***********************************************************************
1555 * X11DRV_DIB_GetImageBits_8
1557 * GetDIBits for an 8-bit deep DIB.
1559 static void X11DRV_DIB_GetImageBits_8( int lines, BYTE *dstbits,
1560 DWORD srcwidth, DWORD dstwidth,
1561 RGBQUAD *colors, PALETTEENTRY *srccolors,
1562 XImage *bmpImage, DWORD linebytes )
1565 int h, width = min(srcwidth, dstwidth);
1571 dstbits = dstbits + ( linebytes * (lines-1) );
1572 linebytes = -linebytes;
1577 * This condition is true when GetImageBits has been called by
1578 * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1579 * 256 colormaps, so we'll just use for for GetDIBits calls.
1580 * (In somes cases, in a updateDIBSection, the returned colors are bad too)
1582 if (!srccolors) goto updatesection;
1584 switch (bmpImage->depth) {
1587 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1590 /* ==== pal 1 bmp -> pal 8 dib ==== */
1591 /* ==== pal 4 bmp -> pal 8 dib ==== */
1592 for (h=lines-1; h>=0; h--) {
1594 for (x=0; x<width; x++) {
1595 PALETTEENTRY srcval;
1596 srcval=srccolors[XGetPixel(bmpImage, x, h)];
1597 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1602 dstbits += linebytes;
1610 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1612 /* ==== pal 8 bmp -> pal 8 dib ==== */
1613 const void* srcbits;
1614 const BYTE* srcpixel;
1616 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1617 for (h=0; h<lines; h++) {
1620 for (x = 0; x < width; x++) {
1621 PALETTEENTRY srcval;
1622 srcval=srccolors[(int)*srcpixel++];
1623 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1628 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1629 dstbits += linebytes;
1639 const void* srcbits;
1640 const WORD* srcpixel;
1643 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1645 if (bmpImage->green_mask==0x03e0) {
1646 if (bmpImage->red_mask==0x7c00) {
1647 /* ==== rgb 555 bmp -> pal 8 dib ==== */
1648 for (h=0; h<lines; h++) {
1651 for (x=0; x<width; x++) {
1654 *dstbyte++=X11DRV_DIB_GetNearestIndex
1656 ((srcval >> 7) & 0xf8) | /* r */
1657 ((srcval >> 12) & 0x07),
1658 ((srcval >> 2) & 0xf8) | /* g */
1659 ((srcval >> 7) & 0x07),
1660 ((srcval << 3) & 0xf8) | /* b */
1661 ((srcval >> 2) & 0x07) );
1663 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1664 dstbits += linebytes;
1666 } else if (bmpImage->blue_mask==0x7c00) {
1667 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1668 for (h=0; h<lines; h++) {
1671 for (x=0; x<width; x++) {
1674 *dstbyte++=X11DRV_DIB_GetNearestIndex
1676 ((srcval << 3) & 0xf8) | /* r */
1677 ((srcval >> 2) & 0x07),
1678 ((srcval >> 2) & 0xf8) | /* g */
1679 ((srcval >> 7) & 0x07),
1680 ((srcval >> 7) & 0xf8) | /* b */
1681 ((srcval >> 12) & 0x07) );
1683 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1684 dstbits += linebytes;
1689 } else if (bmpImage->green_mask==0x07e0) {
1690 if (bmpImage->red_mask==0xf800) {
1691 /* ==== rgb 565 bmp -> pal 8 dib ==== */
1692 for (h=0; h<lines; h++) {
1695 for (x=0; x<width; x++) {
1698 *dstbyte++=X11DRV_DIB_GetNearestIndex
1700 ((srcval >> 8) & 0xf8) | /* r */
1701 ((srcval >> 13) & 0x07),
1702 ((srcval >> 3) & 0xfc) | /* g */
1703 ((srcval >> 9) & 0x03),
1704 ((srcval << 3) & 0xf8) | /* b */
1705 ((srcval >> 2) & 0x07) );
1707 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1708 dstbits += linebytes;
1710 } else if (bmpImage->blue_mask==0xf800) {
1711 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1712 for (h=0; h<lines; h++) {
1715 for (x=0; x<width; x++) {
1718 *dstbyte++=X11DRV_DIB_GetNearestIndex
1720 ((srcval << 3) & 0xf8) | /* r */
1721 ((srcval >> 2) & 0x07),
1722 ((srcval >> 3) & 0xfc) | /* g */
1723 ((srcval >> 9) & 0x03),
1724 ((srcval >> 8) & 0xf8) | /* b */
1725 ((srcval >> 13) & 0x07) );
1727 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1728 dstbits += linebytes;
1742 const void* srcbits;
1743 const BYTE *srcbyte;
1745 int bytes_per_pixel;
1747 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1748 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
1750 if (bmpImage->green_mask!=0x00ff00 ||
1751 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1753 } else if (bmpImage->blue_mask==0xff) {
1754 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1755 for (h=0; h<lines; h++) {
1758 for (x=0; x<width; x++) {
1759 *dstbyte++=X11DRV_DIB_GetNearestIndex
1764 srcbyte+=bytes_per_pixel;
1766 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1767 dstbits += linebytes;
1770 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1771 for (h=0; h<lines; h++) {
1774 for (x=0; x<width; x++) {
1775 *dstbyte++=X11DRV_DIB_GetNearestIndex
1780 srcbyte+=bytes_per_pixel;
1782 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1783 dstbits += linebytes;
1791 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1792 bmpImage->depth, bmpImage->red_mask,
1793 bmpImage->green_mask, bmpImage->blue_mask );
1795 /* ==== any bmp format -> pal 8 dib ==== */
1796 for (h=lines-1; h>=0; h--) {
1798 for (x=0; x<width; x++) {
1799 *dstbyte=X11DRV_DIB_MapColor
1801 XGetPixel(bmpImage, x, h), *dstbyte);
1804 dstbits += linebytes;
1810 /***********************************************************************
1811 * X11DRV_DIB_SetImageBits_RLE8
1813 * SetDIBits for an 8-bit deep compressed DIB.
1815 * This function rewritten 941113 by James Youngman. WINE blew out when I
1816 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
1818 * This was because the algorithm assumed that all RLE8 bitmaps end with the
1819 * 'End of bitmap' escape code. This code is very much laxer in what it
1820 * allows to end the expansion. Possibly too lax. See the note by
1821 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
1822 * bitmap should end with RleEnd, but on the other hand, software exists
1823 * that produces ones that don't and Windows 3.1 doesn't complain a bit
1826 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
1827 * James A. Youngman <mbcstjy@afs.man.ac.uk>
1830 static void X11DRV_DIB_SetImageBits_RLE8( int lines, const BYTE *bits,
1831 DWORD srcwidth, DWORD dstwidth,
1832 int left, int *colors,
1835 unsigned int x; /* X-position on each line. Increases. */
1836 int y; /* Line #. Starts at lines-1, decreases */
1837 const BYTE *pIn = bits; /* Pointer to current position in bits */
1838 BYTE length; /* The length pf a run */
1839 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
1842 * Note that the bitmap data is stored by Windows starting at the
1843 * bottom line of the bitmap and going upwards. Within each line,
1844 * the data is stored left-to-right. That's the reason why line
1845 * goes from lines-1 to 0. [JAY]
1855 * If the length byte is not zero (which is the escape value),
1856 * We have a run of length pixels all the same colour. The colour
1857 * index is stored next.
1859 * If the length byte is zero, we need to read the next byte to
1860 * know what to do. [JAY]
1865 * [Run-Length] Encoded mode
1867 int color = colors[*pIn++];
1868 while (length-- && x < (left + dstwidth)) {
1869 if( x >= left) XPutPixel(bmpImage, x, y, color);
1876 * Escape codes (may be an absolute sequence though)
1878 escape_code = (*pIn++);
1887 /* Not all RLE8 bitmaps end with this code. For
1888 * example, Paint Shop Pro produces some that don't.
1889 * That's (I think) what caused the previous
1890 * implementation to fail. [JAY]
1899 default: /* switch to absolute mode */
1900 length = escape_code;
1903 int color = colors[*pIn++];
1904 if (x >= (left + dstwidth))
1909 if( x >= left) XPutPixel(bmpImage, x, y, color);
1913 * If you think for a moment you'll realise that the
1914 * only time we could ever possibly read an odd
1915 * number of bytes is when there is a 0x00 (escape),
1916 * a value >0x02 (absolute mode) and then an odd-
1917 * length run. Therefore this is the only place we
1918 * need to worry about it. Everywhere else the
1919 * bytes are always read in pairs. [JAY]
1921 if (escape_code & 1) pIn++; /* Throw away the pad byte. */
1923 } /* switch (escape_code) : Escape sequence */
1929 /***********************************************************************
1930 * X11DRV_DIB_SetImageBits_16
1932 * SetDIBits for a 16-bit deep DIB.
1934 static void X11DRV_DIB_SetImageBits_16( int lines, const BYTE *srcbits,
1935 DWORD srcwidth, DWORD dstwidth, int left,
1936 X11DRV_PDEVICE *physDev, DWORD rSrc, DWORD gSrc, DWORD bSrc,
1937 XImage *bmpImage, DWORD linebytes )
1940 int h, width = min(srcwidth, dstwidth);
1941 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
1946 srcbits = srcbits + ( linebytes * (lines-1));
1947 linebytes = -linebytes;
1950 switch (bmpImage->depth)
1957 srcbits=srcbits+left*2;
1958 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1960 if (bmpImage->green_mask==0x03e0) {
1961 if (gSrc==bmpImage->green_mask) {
1962 if (rSrc==bmpImage->red_mask) {
1963 /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1964 /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1965 convs->Convert_5x5_asis
1968 dstbits,-bmpImage->bytes_per_line);
1969 } else if (rSrc==bmpImage->blue_mask) {
1970 /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1971 /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1972 convs->Convert_555_reverse
1975 dstbits,-bmpImage->bytes_per_line);
1978 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1979 /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1980 /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1981 convs->Convert_565_to_555_asis
1984 dstbits,-bmpImage->bytes_per_line);
1986 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1987 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1988 convs->Convert_565_to_555_reverse
1991 dstbits,-bmpImage->bytes_per_line);
1994 } else if (bmpImage->green_mask==0x07e0) {
1995 if (gSrc==bmpImage->green_mask) {
1996 if (rSrc==bmpImage->red_mask) {
1997 /* ==== rgb 565 dib -> rgb 565 bmp ==== */
1998 /* ==== bgr 565 dib -> bgr 565 bmp ==== */
1999 convs->Convert_5x5_asis
2002 dstbits,-bmpImage->bytes_per_line);
2004 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
2005 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
2006 convs->Convert_565_reverse
2009 dstbits,-bmpImage->bytes_per_line);
2012 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
2013 /* ==== rgb 555 dib -> rgb 565 bmp ==== */
2014 /* ==== bgr 555 dib -> bgr 565 bmp ==== */
2015 convs->Convert_555_to_565_asis
2018 dstbits,-bmpImage->bytes_per_line);
2020 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
2021 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
2022 convs->Convert_555_to_565_reverse
2025 dstbits,-bmpImage->bytes_per_line);
2035 if (bmpImage->bits_per_pixel==24) {
2038 srcbits=srcbits+left*2;
2039 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2041 if (bmpImage->green_mask!=0x00ff00 ||
2042 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2044 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2045 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2047 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
2048 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
2049 convs->Convert_555_to_888_asis
2052 dstbits,-bmpImage->bytes_per_line);
2054 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
2055 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
2056 convs->Convert_565_to_888_asis
2059 dstbits,-bmpImage->bytes_per_line);
2063 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
2064 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
2065 convs->Convert_555_to_888_reverse
2068 dstbits,-bmpImage->bytes_per_line);
2070 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
2071 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
2072 convs->Convert_565_to_888_reverse
2075 dstbits,-bmpImage->bytes_per_line);
2086 srcbits=srcbits+left*2;
2087 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2089 if (bmpImage->green_mask!=0x00ff00 ||
2090 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2092 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2093 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2095 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
2096 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
2097 convs->Convert_555_to_0888_asis
2100 dstbits,-bmpImage->bytes_per_line);
2102 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
2103 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
2104 convs->Convert_565_to_0888_asis
2107 dstbits,-bmpImage->bytes_per_line);
2111 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
2112 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
2113 convs->Convert_555_to_0888_reverse
2116 dstbits,-bmpImage->bytes_per_line);
2118 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
2119 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
2120 convs->Convert_565_to_0888_reverse
2123 dstbits,-bmpImage->bytes_per_line);
2131 WARN("from 16 bit DIB (%lx,%lx,%lx) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2132 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2133 bmpImage->green_mask, bmpImage->blue_mask );
2139 /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
2140 const WORD* srcpixel;
2141 int rShift1,gShift1,bShift1;
2142 int rShift2,gShift2,bShift2;
2145 /* Set color scaling values */
2146 rShift1=16+X11DRV_DIB_MaskToShift(rSrc)-3;
2147 gShift1=16+X11DRV_DIB_MaskToShift(gSrc)-3;
2148 bShift1=16+X11DRV_DIB_MaskToShift(bSrc)-3;
2153 /* Green has 5 bits, like the others */
2157 /* Green has 6 bits, not 5. Compensate. */
2166 /* We could split it into four separate cases to optimize
2167 * but it is probably not worth it.
2169 for (h=lines-1; h>=0; h--) {
2170 srcpixel=(const WORD*)srcbits;
2171 for (x=left; x<width+left; x++) {
2173 BYTE red,green,blue;
2174 srcval=*srcpixel++ << 16;
2175 red= ((srcval >> rShift1) & 0xf8) |
2176 ((srcval >> rShift2) & 0x07);
2177 green=((srcval >> gShift1) & gMask1) |
2178 ((srcval >> gShift2) & gMask2);
2179 blue= ((srcval >> bShift1) & 0xf8) |
2180 ((srcval >> bShift2) & 0x07);
2181 XPutPixel(bmpImage, x, h,
2182 X11DRV_PALETTE_ToPhysical
2183 (physDev, RGB(red,green,blue)));
2185 srcbits += linebytes;
2193 /***********************************************************************
2194 * X11DRV_DIB_GetImageBits_16
2196 * GetDIBits for an 16-bit deep DIB.
2198 static void X11DRV_DIB_GetImageBits_16( int lines, BYTE *dstbits,
2199 DWORD dstwidth, DWORD srcwidth,
2200 PALETTEENTRY *srccolors,
2201 DWORD rDst, DWORD gDst, DWORD bDst,
2202 XImage *bmpImage, DWORD dibpitch )
2205 int h, width = min(srcwidth, dstwidth);
2206 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2208 DWORD linebytes = dibpitch;
2213 dstbits = dstbits + ( linebytes * (lines-1));
2214 linebytes = -linebytes;
2217 switch (bmpImage->depth)
2222 const char* srcbits;
2224 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2226 if (bmpImage->green_mask==0x03e0) {
2227 if (gDst==bmpImage->green_mask) {
2228 if (rDst==bmpImage->red_mask) {
2229 /* ==== rgb 555 bmp -> rgb 555 dib ==== */
2230 /* ==== bgr 555 bmp -> bgr 555 dib ==== */
2231 convs->Convert_5x5_asis
2233 srcbits,-bmpImage->bytes_per_line,
2236 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
2237 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
2238 convs->Convert_555_reverse
2240 srcbits,-bmpImage->bytes_per_line,
2244 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2245 /* ==== rgb 555 bmp -> rgb 565 dib ==== */
2246 /* ==== bgr 555 bmp -> bgr 565 dib ==== */
2247 convs->Convert_555_to_565_asis
2249 srcbits,-bmpImage->bytes_per_line,
2252 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
2253 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
2254 convs->Convert_555_to_565_reverse
2256 srcbits,-bmpImage->bytes_per_line,
2260 } else if (bmpImage->green_mask==0x07e0) {
2261 if (gDst==bmpImage->green_mask) {
2262 if (rDst == bmpImage->red_mask) {
2263 /* ==== rgb 565 bmp -> rgb 565 dib ==== */
2264 /* ==== bgr 565 bmp -> bgr 565 dib ==== */
2265 convs->Convert_5x5_asis
2267 srcbits,-bmpImage->bytes_per_line,
2270 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
2271 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
2272 convs->Convert_565_reverse
2274 srcbits,-bmpImage->bytes_per_line,
2278 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2279 /* ==== rgb 565 bmp -> rgb 555 dib ==== */
2280 /* ==== bgr 565 bmp -> bgr 555 dib ==== */
2281 convs->Convert_565_to_555_asis
2283 srcbits,-bmpImage->bytes_per_line,
2286 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
2287 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
2288 convs->Convert_565_to_555_reverse
2290 srcbits,-bmpImage->bytes_per_line,
2301 if (bmpImage->bits_per_pixel == 24) {
2302 const char* srcbits;
2304 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2306 if (bmpImage->green_mask!=0x00ff00 ||
2307 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2309 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2310 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2312 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
2313 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
2314 convs->Convert_888_to_555_asis
2316 srcbits,-bmpImage->bytes_per_line,
2319 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2320 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2321 convs->Convert_888_to_565_asis
2323 srcbits,-bmpImage->bytes_per_line,
2328 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
2329 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
2330 convs->Convert_888_to_555_reverse
2332 srcbits,-bmpImage->bytes_per_line,
2335 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
2336 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
2337 convs->Convert_888_to_565_reverse
2339 srcbits,-bmpImage->bytes_per_line,
2349 const char* srcbits;
2351 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2353 if (bmpImage->green_mask!=0x00ff00 ||
2354 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2356 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2357 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2359 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2360 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2361 convs->Convert_0888_to_555_asis
2363 srcbits,-bmpImage->bytes_per_line,
2366 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2367 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2368 convs->Convert_0888_to_565_asis
2370 srcbits,-bmpImage->bytes_per_line,
2375 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2376 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2377 convs->Convert_0888_to_555_reverse
2379 srcbits,-bmpImage->bytes_per_line,
2382 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2383 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2384 convs->Convert_0888_to_565_reverse
2386 srcbits,-bmpImage->bytes_per_line,
2395 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2397 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2398 int rShift,gShift,bShift;
2401 /* Shift everything 16 bits left so that all shifts are >0,
2402 * even for BGR DIBs. Then a single >> 16 will bring everything
2405 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2406 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2407 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2409 /* 6 bits for the green */
2415 for (h = lines - 1; h >= 0; h--) {
2416 dstpixel=(LPWORD)dstbits;
2417 for (x = 0; x < width; x++) {
2418 PALETTEENTRY srcval;
2420 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2421 dstval=((srcval.peRed << rShift) & rDst) |
2422 ((srcval.peGreen << gShift) & gDst) |
2423 ((srcval.peBlue << bShift) & bDst);
2424 *dstpixel++=dstval >> 16;
2426 dstbits += linebytes;
2434 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2436 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2437 int rShift,gShift,bShift;
2438 const BYTE* srcbits;
2439 const BYTE* srcpixel;
2442 /* Shift everything 16 bits left so that all shifts are >0,
2443 * even for BGR DIBs. Then a single >> 16 will bring everything
2446 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2447 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2448 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2450 /* 6 bits for the green */
2456 srcbits=(BYTE*)bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2457 for (h=0; h<lines; h++) {
2459 dstpixel=(LPWORD)dstbits;
2460 for (x = 0; x < width; x++) {
2461 PALETTEENTRY srcval;
2463 srcval=srccolors[(int)*srcpixel++];
2464 dstval=((srcval.peRed << rShift) & rDst) |
2465 ((srcval.peGreen << gShift) & gDst) |
2466 ((srcval.peBlue << bShift) & bDst);
2467 *dstpixel++=dstval >> 16;
2469 srcbits -= bmpImage->bytes_per_line;
2470 dstbits += linebytes;
2480 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2481 int rShift,gShift,bShift;
2484 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%lx,%lx,%lx)\n",
2485 bmpImage->depth, bmpImage->red_mask,
2486 bmpImage->green_mask, bmpImage->blue_mask,
2489 /* Shift everything 16 bits left so that all shifts are >0,
2490 * even for BGR DIBs. Then a single >> 16 will bring everything
2493 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2494 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2495 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2497 /* 6 bits for the green */
2503 for (h = lines - 1; h >= 0; h--) {
2504 dstpixel=(LPWORD)dstbits;
2505 for (x = 0; x < width; x++) {
2508 srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
2509 dstval=((GetRValue(srcval) << rShift) & rDst) |
2510 ((GetGValue(srcval) << gShift) & gDst) |
2511 ((GetBValue(srcval) << bShift) & bDst);
2512 *dstpixel++=dstval >> 16;
2514 dstbits += linebytes;
2522 /***********************************************************************
2523 * X11DRV_DIB_SetImageBits_24
2525 * SetDIBits for a 24-bit deep DIB.
2527 static void X11DRV_DIB_SetImageBits_24( int lines, const BYTE *srcbits,
2528 DWORD srcwidth, DWORD dstwidth, int left,
2529 X11DRV_PDEVICE *physDev,
2530 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2531 XImage *bmpImage, DWORD linebytes )
2534 int h, width = min(srcwidth, dstwidth);
2535 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2540 srcbits = srcbits + linebytes * (lines - 1);
2541 linebytes = -linebytes;
2544 switch (bmpImage->depth)
2547 if (bmpImage->bits_per_pixel==24) {
2550 srcbits=srcbits+left*3;
2551 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2553 if (bmpImage->green_mask!=0x00ff00 ||
2554 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2556 } else if (rSrc==bmpImage->red_mask) {
2557 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2558 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2559 convs->Convert_888_asis
2562 dstbits,-bmpImage->bytes_per_line);
2564 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2565 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2566 convs->Convert_888_reverse
2569 dstbits,-bmpImage->bytes_per_line);
2579 srcbits=srcbits+left*3;
2580 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2582 if (bmpImage->green_mask!=0x00ff00 ||
2583 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2585 } else if (rSrc==bmpImage->red_mask) {
2586 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2587 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2588 convs->Convert_888_to_0888_asis
2591 dstbits,-bmpImage->bytes_per_line);
2593 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2594 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2595 convs->Convert_888_to_0888_reverse
2598 dstbits,-bmpImage->bytes_per_line);
2608 srcbits=srcbits+left*3;
2609 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
2611 if (bmpImage->green_mask==0x03e0) {
2612 if ((rSrc==0xff0000 && bmpImage->red_mask==0x7f00) ||
2613 (bSrc==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2614 /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2615 /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2616 convs->Convert_888_to_555_asis
2619 dstbits,-bmpImage->bytes_per_line);
2620 } else if ((rSrc==0xff && bmpImage->red_mask==0x7f00) ||
2621 (bSrc==0xff && bmpImage->blue_mask==0x7f00)) {
2622 /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2623 /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2624 convs->Convert_888_to_555_reverse
2627 dstbits,-bmpImage->bytes_per_line);
2631 } else if (bmpImage->green_mask==0x07e0) {
2632 if ((rSrc==0xff0000 && bmpImage->red_mask==0xf800) ||
2633 (bSrc==0xff0000 && bmpImage->blue_mask==0xf800)) {
2634 /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2635 /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2636 convs->Convert_888_to_565_asis
2639 dstbits,-bmpImage->bytes_per_line);
2640 } else if ((rSrc==0xff && bmpImage->red_mask==0xf800) ||
2641 (bSrc==0xff && bmpImage->blue_mask==0xf800)) {
2642 /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2643 /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2644 convs->Convert_888_to_565_reverse
2647 dstbits,-bmpImage->bytes_per_line);
2659 WARN("from 24 bit DIB (%lx,%lx,%lx) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2660 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2661 bmpImage->green_mask, bmpImage->blue_mask );
2667 /* ==== rgb 888 dib -> any bmp bormat ==== */
2668 const BYTE* srcbyte;
2670 /* Windows only supports one 24bpp DIB format: RGB888 */
2672 for (h = lines - 1; h >= 0; h--) {
2673 srcbyte=(const BYTE*)srcbits;
2674 for (x = left; x < width+left; x++) {
2675 XPutPixel(bmpImage, x, h,
2676 X11DRV_PALETTE_ToPhysical
2677 (physDev, RGB(srcbyte[2], srcbyte[1], srcbyte[0])));
2680 srcbits += linebytes;
2688 /***********************************************************************
2689 * X11DRV_DIB_GetImageBits_24
2691 * GetDIBits for an 24-bit deep DIB.
2693 static void X11DRV_DIB_GetImageBits_24( int lines, BYTE *dstbits,
2694 DWORD dstwidth, DWORD srcwidth,
2695 PALETTEENTRY *srccolors,
2696 DWORD rDst, DWORD gDst, DWORD bDst,
2697 XImage *bmpImage, DWORD linebytes )
2700 int h, width = min(srcwidth, dstwidth);
2701 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2706 dstbits = dstbits + ( linebytes * (lines-1) );
2707 linebytes = -linebytes;
2710 switch (bmpImage->depth)
2713 if (bmpImage->bits_per_pixel==24) {
2714 const char* srcbits;
2716 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2718 if (bmpImage->green_mask!=0x00ff00 ||
2719 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2721 } else if (rDst==bmpImage->red_mask) {
2722 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2723 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2724 convs->Convert_888_asis
2726 srcbits,-bmpImage->bytes_per_line,
2729 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2730 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2731 convs->Convert_888_reverse
2733 srcbits,-bmpImage->bytes_per_line,
2742 const char* srcbits;
2744 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2746 if (bmpImage->green_mask!=0x00ff00 ||
2747 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2749 } else if (rDst==bmpImage->red_mask) {
2750 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2751 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2752 convs->Convert_0888_to_888_asis
2754 srcbits,-bmpImage->bytes_per_line,
2757 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2758 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2759 convs->Convert_0888_to_888_reverse
2761 srcbits,-bmpImage->bytes_per_line,
2770 const char* srcbits;
2772 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2774 if (bmpImage->green_mask==0x03e0) {
2775 if ((rDst==0xff0000 && bmpImage->red_mask==0x7f00) ||
2776 (bDst==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2777 /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2778 /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2779 convs->Convert_555_to_888_asis
2781 srcbits,-bmpImage->bytes_per_line,
2783 } else if ((rDst==0xff && bmpImage->red_mask==0x7f00) ||
2784 (bDst==0xff && bmpImage->blue_mask==0x7f00)) {
2785 /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2786 /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2787 convs->Convert_555_to_888_reverse
2789 srcbits,-bmpImage->bytes_per_line,
2794 } else if (bmpImage->green_mask==0x07e0) {
2795 if ((rDst==0xff0000 && bmpImage->red_mask==0xf800) ||
2796 (bDst==0xff0000 && bmpImage->blue_mask==0xf800)) {
2797 /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2798 /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2799 convs->Convert_565_to_888_asis
2801 srcbits,-bmpImage->bytes_per_line,
2803 } else if ((rDst==0xff && bmpImage->red_mask==0xf800) ||
2804 (bDst==0xff && bmpImage->blue_mask==0xf800)) {
2805 /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2806 /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2807 convs->Convert_565_to_888_reverse
2809 srcbits,-bmpImage->bytes_per_line,
2822 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2824 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2827 /* Windows only supports one 24bpp DIB format: rgb 888 */
2828 for (h = lines - 1; h >= 0; h--) {
2830 for (x = 0; x < width; x++) {
2831 PALETTEENTRY srcval;
2832 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2833 dstbyte[0]=srcval.peBlue;
2834 dstbyte[1]=srcval.peGreen;
2835 dstbyte[2]=srcval.peRed;
2838 dstbits += linebytes;
2846 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2848 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2849 const void* srcbits;
2850 const BYTE* srcpixel;
2853 /* Windows only supports one 24bpp DIB format: rgb 888 */
2854 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2855 for (h = lines - 1; h >= 0; h--) {
2858 for (x = 0; x < width; x++ ) {
2859 PALETTEENTRY srcval;
2860 srcval=srccolors[(int)*srcpixel++];
2861 dstbyte[0]=srcval.peBlue;
2862 dstbyte[1]=srcval.peGreen;
2863 dstbyte[2]=srcval.peRed;
2866 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
2867 dstbits += linebytes;
2877 /* ==== any bmp format -> 888 dib ==== */
2880 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%lx,%lx,%lx)\n",
2881 bmpImage->depth, bmpImage->red_mask,
2882 bmpImage->green_mask, bmpImage->blue_mask,
2885 /* Windows only supports one 24bpp DIB format: rgb 888 */
2886 for (h = lines - 1; h >= 0; h--) {
2888 for (x = 0; x < width; x++) {
2889 COLORREF srcval=X11DRV_PALETTE_ToLogical
2890 (XGetPixel( bmpImage, x, h ));
2891 dstbyte[0]=GetBValue(srcval);
2892 dstbyte[1]=GetGValue(srcval);
2893 dstbyte[2]=GetRValue(srcval);
2896 dstbits += linebytes;
2904 /***********************************************************************
2905 * X11DRV_DIB_SetImageBits_32
2907 * SetDIBits for a 32-bit deep DIB.
2909 static void X11DRV_DIB_SetImageBits_32(int lines, const BYTE *srcbits,
2910 DWORD srcwidth, DWORD dstwidth, int left,
2911 X11DRV_PDEVICE *physDev,
2912 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2918 int h, width = min(srcwidth, dstwidth);
2919 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2924 srcbits = srcbits + ( linebytes * (lines-1) );
2925 linebytes = -linebytes;
2928 ptr = (const DWORD *) srcbits + left;
2930 switch (bmpImage->depth)
2933 if (bmpImage->bits_per_pixel==24) {
2936 srcbits=srcbits+left*4;
2937 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2939 if (rSrc==bmpImage->red_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->blue_mask) {
2940 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2941 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2942 convs->Convert_0888_to_888_asis
2945 dstbits,-bmpImage->bytes_per_line);
2946 } else if (bmpImage->green_mask!=0x00ff00 ||
2947 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2949 /* the tests below assume sane bmpImage masks */
2950 } else if (rSrc==bmpImage->blue_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->red_mask) {
2951 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2952 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2953 convs->Convert_0888_to_888_reverse
2956 dstbits,-bmpImage->bytes_per_line);
2957 } else if (bmpImage->blue_mask==0xff) {
2958 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2959 convs->Convert_any0888_to_rgb888
2963 dstbits,-bmpImage->bytes_per_line);
2965 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2966 convs->Convert_any0888_to_bgr888
2970 dstbits,-bmpImage->bytes_per_line);
2980 srcbits=srcbits+left*4;
2981 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2983 if (gSrc==bmpImage->green_mask) {
2984 if (rSrc==bmpImage->red_mask && bSrc==bmpImage->blue_mask) {
2985 /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2986 /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2987 convs->Convert_0888_asis
2990 dstbits,-bmpImage->bytes_per_line);
2991 } else if (bmpImage->green_mask!=0x00ff00 ||
2992 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2994 /* the tests below assume sane bmpImage masks */
2995 } else if (rSrc==bmpImage->blue_mask && bSrc==bmpImage->red_mask) {
2996 /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2997 /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2998 convs->Convert_0888_reverse
3001 dstbits,-bmpImage->bytes_per_line);
3003 /* ==== any 0888 dib -> any 0888 bmp ==== */
3004 convs->Convert_0888_any
3008 dstbits,-bmpImage->bytes_per_line,
3009 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3011 } else if (bmpImage->green_mask!=0x00ff00 ||
3012 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3014 /* the tests below assume sane bmpImage masks */
3016 /* ==== any 0888 dib -> any 0888 bmp ==== */
3017 convs->Convert_0888_any
3021 dstbits,-bmpImage->bytes_per_line,
3022 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3032 srcbits=srcbits+left*4;
3033 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
3035 if (rSrc==0xff0000 && gSrc==0x00ff00 && bSrc==0x0000ff) {
3036 if (bmpImage->green_mask==0x03e0) {
3037 if (bmpImage->red_mask==0x7f00) {
3038 /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
3039 convs->Convert_0888_to_555_asis
3042 dstbits,-bmpImage->bytes_per_line);
3043 } else if (bmpImage->blue_mask==0x7f00) {
3044 /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
3045 convs->Convert_0888_to_555_reverse
3048 dstbits,-bmpImage->bytes_per_line);
3052 } else if (bmpImage->green_mask==0x07e0) {
3053 if (bmpImage->red_mask==0xf800) {
3054 /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
3055 convs->Convert_0888_to_565_asis
3058 dstbits,-bmpImage->bytes_per_line);
3059 } else if (bmpImage->blue_mask==0xf800) {
3060 /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
3061 convs->Convert_0888_to_565_reverse
3064 dstbits,-bmpImage->bytes_per_line);
3071 } else if (rSrc==0x0000ff && gSrc==0x00ff00 && bSrc==0xff0000) {
3072 if (bmpImage->green_mask==0x03e0) {
3073 if (bmpImage->blue_mask==0x7f00) {
3074 /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
3075 convs->Convert_0888_to_555_asis
3078 dstbits,-bmpImage->bytes_per_line);
3079 } else if (bmpImage->red_mask==0x7f00) {
3080 /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
3081 convs->Convert_0888_to_555_reverse
3084 dstbits,-bmpImage->bytes_per_line);
3088 } else if (bmpImage->green_mask==0x07e0) {
3089 if (bmpImage->blue_mask==0xf800) {
3090 /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
3091 convs->Convert_0888_to_565_asis
3094 dstbits,-bmpImage->bytes_per_line);
3095 } else if (bmpImage->red_mask==0xf800) {
3096 /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
3097 convs->Convert_0888_to_565_reverse
3100 dstbits,-bmpImage->bytes_per_line);
3108 if (bmpImage->green_mask==0x03e0 &&
3109 (bmpImage->red_mask==0x7f00 ||
3110 bmpImage->blue_mask==0x7f00)) {
3111 /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
3112 convs->Convert_any0888_to_5x5
3116 dstbits,-bmpImage->bytes_per_line,
3117 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3118 } else if (bmpImage->green_mask==0x07e0 &&
3119 (bmpImage->red_mask==0xf800 ||
3120 bmpImage->blue_mask==0xf800)) {
3121 /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
3122 convs->Convert_any0888_to_5x5
3126 dstbits,-bmpImage->bytes_per_line,
3127 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3137 WARN("from 32 bit DIB (%lx,%lx,%lx) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
3138 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
3139 bmpImage->green_mask, bmpImage->blue_mask );
3145 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
3146 const DWORD* srcpixel;
3147 int rShift,gShift,bShift;
3149 rShift=X11DRV_DIB_MaskToShift(rSrc);
3150 gShift=X11DRV_DIB_MaskToShift(gSrc);
3151 bShift=X11DRV_DIB_MaskToShift(bSrc);
3153 for (h = lines - 1; h >= 0; h--) {
3154 srcpixel=(const DWORD*)srcbits;
3155 for (x = left; x < width+left; x++) {
3157 BYTE red,green,blue;
3158 srcvalue=*srcpixel++;
3159 red= (srcvalue >> rShift) & 0xff;
3160 green=(srcvalue >> gShift) & 0xff;
3161 blue= (srcvalue >> bShift) & 0xff;
3162 XPutPixel(bmpImage, x, h, X11DRV_PALETTE_ToPhysical
3163 (physDev, RGB(red,green,blue)));
3165 srcbits += linebytes;
3173 /***********************************************************************
3174 * X11DRV_DIB_GetImageBits_32
3176 * GetDIBits for an 32-bit deep DIB.
3178 static void X11DRV_DIB_GetImageBits_32( int lines, BYTE *dstbits,
3179 DWORD dstwidth, DWORD srcwidth,
3180 PALETTEENTRY *srccolors,
3181 DWORD rDst, DWORD gDst, DWORD bDst,
3182 XImage *bmpImage, DWORD linebytes )
3185 int h, width = min(srcwidth, dstwidth);
3187 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
3192 dstbits = dstbits + ( linebytes * (lines-1) );
3193 linebytes = -linebytes;
3198 switch (bmpImage->depth)
3201 if (bmpImage->bits_per_pixel==24) {
3202 const void* srcbits;
3204 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3206 if (rDst==bmpImage->red_mask && gDst==bmpImage->green_mask && bDst==bmpImage->blue_mask) {
3207 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
3208 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
3209 convs->Convert_888_to_0888_asis
3211 srcbits,-bmpImage->bytes_per_line,
3213 } else if (bmpImage->green_mask!=0x00ff00 ||
3214 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3216 /* the tests below assume sane bmpImage masks */
3217 } else if (rDst==bmpImage->blue_mask && gDst==bmpImage->green_mask && bDst==bmpImage->red_mask) {
3218 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
3219 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
3220 convs->Convert_888_to_0888_reverse
3222 srcbits,-bmpImage->bytes_per_line,
3224 } else if (bmpImage->blue_mask==0xff) {
3225 /* ==== rgb 888 bmp -> any 0888 dib ==== */
3226 convs->Convert_rgb888_to_any0888
3228 srcbits,-bmpImage->bytes_per_line,
3232 /* ==== bgr 888 bmp -> any 0888 dib ==== */
3233 convs->Convert_bgr888_to_any0888
3235 srcbits,-bmpImage->bytes_per_line,
3245 const char* srcbits;
3247 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3249 if (gDst==bmpImage->green_mask) {
3250 if (rDst==bmpImage->red_mask && bDst==bmpImage->blue_mask) {
3251 /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
3252 /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
3253 convs->Convert_0888_asis
3255 srcbits,-bmpImage->bytes_per_line,
3257 } else if (bmpImage->green_mask!=0x00ff00 ||
3258 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3260 /* the tests below assume sane bmpImage masks */
3261 } else if (rDst==bmpImage->blue_mask && bDst==bmpImage->red_mask) {
3262 /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
3263 /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
3264 convs->Convert_0888_reverse
3266 srcbits,-bmpImage->bytes_per_line,
3269 /* ==== any 0888 bmp -> any 0888 dib ==== */
3270 convs->Convert_0888_any
3272 srcbits,-bmpImage->bytes_per_line,
3273 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3277 } else if (bmpImage->green_mask!=0x00ff00 ||
3278 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3280 /* the tests below assume sane bmpImage masks */
3282 /* ==== any 0888 bmp -> any 0888 dib ==== */
3283 convs->Convert_0888_any
3285 srcbits,-bmpImage->bytes_per_line,
3286 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3296 const char* srcbits;
3298 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3300 if (rDst==0xff0000 && gDst==0x00ff00 && bDst==0x0000ff) {
3301 if (bmpImage->green_mask==0x03e0) {
3302 if (bmpImage->red_mask==0x7f00) {
3303 /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
3304 convs->Convert_555_to_0888_asis
3306 srcbits,-bmpImage->bytes_per_line,
3308 } else if (bmpImage->blue_mask==0x7f00) {
3309 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
3310 convs->Convert_555_to_0888_reverse
3312 srcbits,-bmpImage->bytes_per_line,
3317 } else if (bmpImage->green_mask==0x07e0) {
3318 if (bmpImage->red_mask==0xf800) {
3319 /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
3320 convs->Convert_565_to_0888_asis
3322 srcbits,-bmpImage->bytes_per_line,
3324 } else if (bmpImage->blue_mask==0xf800) {
3325 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
3326 convs->Convert_565_to_0888_reverse
3328 srcbits,-bmpImage->bytes_per_line,
3336 } else if (rDst==0x0000ff && gDst==0x00ff00 && bDst==0xff0000) {
3337 if (bmpImage->green_mask==0x03e0) {
3338 if (bmpImage->blue_mask==0x7f00) {
3339 /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
3340 convs->Convert_555_to_0888_asis
3342 srcbits,-bmpImage->bytes_per_line,
3344 } else if (bmpImage->red_mask==0x7f00) {
3345 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
3346 convs->Convert_555_to_0888_reverse
3348 srcbits,-bmpImage->bytes_per_line,
3353 } else if (bmpImage->green_mask==0x07e0) {
3354 if (bmpImage->blue_mask==0xf800) {
3355 /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3356 convs->Convert_565_to_0888_asis
3358 srcbits,-bmpImage->bytes_per_line,
3360 } else if (bmpImage->red_mask==0xf800) {
3361 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3362 convs->Convert_565_to_0888_reverse
3364 srcbits,-bmpImage->bytes_per_line,
3373 if (bmpImage->green_mask==0x03e0 &&
3374 (bmpImage->red_mask==0x7f00 ||
3375 bmpImage->blue_mask==0x7f00)) {
3376 /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3377 convs->Convert_5x5_to_any0888
3379 srcbits,-bmpImage->bytes_per_line,
3380 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3383 } else if (bmpImage->green_mask==0x07e0 &&
3384 (bmpImage->red_mask==0xf800 ||
3385 bmpImage->blue_mask==0xf800)) {
3386 /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3387 convs->Convert_5x5_to_any0888
3389 srcbits,-bmpImage->bytes_per_line,
3390 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3402 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3404 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3405 int rShift,gShift,bShift;
3408 rShift=X11DRV_DIB_MaskToShift(rDst);
3409 gShift=X11DRV_DIB_MaskToShift(gDst);
3410 bShift=X11DRV_DIB_MaskToShift(bDst);
3411 for (h = lines - 1; h >= 0; h--) {
3412 dstpixel=(DWORD*)dstbits;
3413 for (x = 0; x < width; x++) {
3414 PALETTEENTRY srcval;
3415 srcval = srccolors[XGetPixel(bmpImage, x, h)];
3416 *dstpixel++=(srcval.peRed << rShift) |
3417 (srcval.peGreen << gShift) |
3418 (srcval.peBlue << bShift);
3420 dstbits += linebytes;
3428 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3430 /* ==== pal 8 bmp -> any 0888 dib ==== */
3431 int rShift,gShift,bShift;
3432 const void* srcbits;
3433 const BYTE* srcpixel;
3436 rShift=X11DRV_DIB_MaskToShift(rDst);
3437 gShift=X11DRV_DIB_MaskToShift(gDst);
3438 bShift=X11DRV_DIB_MaskToShift(bDst);
3439 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3440 for (h = lines - 1; h >= 0; h--) {
3442 dstpixel=(DWORD*)dstbits;
3443 for (x = 0; x < width; x++) {
3444 PALETTEENTRY srcval;
3445 srcval=srccolors[(int)*srcpixel++];
3446 *dstpixel++=(srcval.peRed << rShift) |
3447 (srcval.peGreen << gShift) |
3448 (srcval.peBlue << bShift);
3450 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
3451 dstbits += linebytes;
3461 /* ==== any bmp format -> any 0888 dib ==== */
3462 int rShift,gShift,bShift;
3465 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%lx,%lx,%lx)\n",
3466 bmpImage->depth, bmpImage->red_mask,
3467 bmpImage->green_mask, bmpImage->blue_mask,
3470 rShift=X11DRV_DIB_MaskToShift(rDst);
3471 gShift=X11DRV_DIB_MaskToShift(gDst);
3472 bShift=X11DRV_DIB_MaskToShift(bDst);
3473 for (h = lines - 1; h >= 0; h--) {
3474 dstpixel=(DWORD*)dstbits;
3475 for (x = 0; x < width; x++) {
3477 srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
3478 *dstpixel++=(GetRValue(srcval) << rShift) |
3479 (GetGValue(srcval) << gShift) |
3480 (GetBValue(srcval) << bShift);
3482 dstbits += linebytes;
3489 static int XGetSubImageErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
3491 return (event->request_code == X_GetImage && event->error_code == BadMatch);
3494 /***********************************************************************
3495 * X11DRV_DIB_SetImageBits_GetSubImage
3497 * Helper for X11DRV_DIB_SetImageBits
3499 static void X11DRV_DIB_SetImageBits_GetSubImage(
3500 const X11DRV_DIB_IMAGEBITS_DESCR *descr, XImage *bmpImage)
3502 /* compressed bitmaps may contain gaps in them. So make a copy
3503 * of the existing pixels first */
3506 SetRect( &bmprc, descr->xDest, descr->yDest,
3507 descr->xDest + descr->width , descr->yDest + descr->height );
3508 GetRgnBox( descr->physDev->region, &rc );
3509 /* convert from dc to drawable origin */
3510 OffsetRect( &rc, descr->physDev->org.x, descr->physDev->org.y);
3511 /* clip visible rect with bitmap */
3512 if( IntersectRect( &rc, &rc, &bmprc))
3514 X11DRV_expect_error( gdi_display, XGetSubImageErrorHandler, NULL );
3515 XGetSubImage( gdi_display, descr->drawable, rc.left, rc.top,
3516 rc.right - rc.left, rc.bottom - rc.top, AllPlanes,
3518 descr->xSrc + rc.left - bmprc.left,
3519 descr->ySrc + rc.top - bmprc.top);
3520 X11DRV_check_error();
3524 /***********************************************************************
3525 * X11DRV_DIB_SetImageBits
3527 * Transfer the bits to an X image.
3528 * Helper function for SetDIBits() and SetDIBitsToDevice().
3530 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3532 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3537 bmpImage = descr->image;
3539 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3540 descr->infoWidth, lines, 32, 0 );
3541 bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
3542 if(bmpImage->data == NULL) {
3543 ERR("Out of memory!\n");
3544 XDestroyImage( bmpImage );
3545 wine_tsx11_unlock();
3550 TRACE("Dib: depth=%d r=%lx g=%lx b=%lx\n",
3551 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3552 TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3553 bmpImage->depth,bmpImage->bits_per_pixel,
3554 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3556 /* Transfer the pixels */
3557 switch(descr->infoBpp)
3560 X11DRV_DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
3561 descr->width, descr->xSrc, (int *)(descr->colorMap),
3562 bmpImage, descr->dibpitch );
3565 if (descr->compression) {
3566 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3567 X11DRV_DIB_SetImageBits_RLE4( descr->lines, descr->bits,
3568 descr->infoWidth, descr->width,
3569 descr->xSrc, (int *)(descr->colorMap),
3572 X11DRV_DIB_SetImageBits_4( descr->lines, descr->bits,
3573 descr->infoWidth, descr->width,
3574 descr->xSrc, (int*)(descr->colorMap),
3575 bmpImage, descr->dibpitch );
3578 if (descr->compression) {
3579 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3580 X11DRV_DIB_SetImageBits_RLE8( descr->lines, descr->bits,
3581 descr->infoWidth, descr->width,
3582 descr->xSrc, (int *)(descr->colorMap),
3585 X11DRV_DIB_SetImageBits_8( descr->lines, descr->bits,
3586 descr->infoWidth, descr->width,
3587 descr->xSrc, (int *)(descr->colorMap),
3588 bmpImage, descr->dibpitch );
3592 X11DRV_DIB_SetImageBits_16( descr->lines, descr->bits,
3593 descr->infoWidth, descr->width,
3594 descr->xSrc, descr->physDev,
3595 descr->rMask, descr->gMask, descr->bMask,
3596 bmpImage, descr->dibpitch);
3599 X11DRV_DIB_SetImageBits_24( descr->lines, descr->bits,
3600 descr->infoWidth, descr->width,
3601 descr->xSrc, descr->physDev,
3602 descr->rMask, descr->gMask, descr->bMask,
3603 bmpImage, descr->dibpitch);
3606 X11DRV_DIB_SetImageBits_32( descr->lines, descr->bits,
3607 descr->infoWidth, descr->width,
3608 descr->xSrc, descr->physDev,
3609 descr->rMask, descr->gMask, descr->bMask,
3610 bmpImage, descr->dibpitch);
3613 WARN("(%d): Invalid depth\n", descr->infoBpp );
3617 TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3618 descr->drawable, descr->gc, bmpImage,
3619 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3620 descr->width, descr->height);
3621 #ifdef HAVE_LIBXXSHM
3624 XShmPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3625 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3626 descr->width, descr->height, FALSE );
3627 XSync( gdi_display, 0 );
3631 XPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3632 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3633 descr->width, descr->height );
3635 if (!descr->image) XDestroyImage( bmpImage );
3636 wine_tsx11_unlock();
3640 /***********************************************************************
3641 * X11DRV_DIB_GetImageBits
3643 * Transfer the bits from an X image.
3645 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3647 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3652 bmpImage = descr->image;
3654 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3655 descr->infoWidth, lines, 32, 0 );
3656 bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
3657 if(bmpImage->data == NULL) {
3658 ERR("Out of memory!\n");
3659 XDestroyImage( bmpImage );
3660 wine_tsx11_unlock();
3665 #ifdef HAVE_LIBXXSHM
3668 int saveRed, saveGreen, saveBlue;
3670 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3671 gdi_display, descr->drawable, bmpImage,
3672 descr->xSrc, descr->ySrc, AllPlanes);
3674 /* We must save and restore the bmpImage's masks in order
3675 * to preserve them across the call to XShmGetImage, which
3676 * decides to eleminate them since it doesn't happen to know
3677 * what the format of the image is supposed to be, even though
3679 saveRed = bmpImage->red_mask;
3680 saveBlue= bmpImage->blue_mask;
3681 saveGreen = bmpImage->green_mask;
3683 XShmGetImage( gdi_display, descr->drawable, bmpImage,
3684 descr->xSrc, descr->ySrc, AllPlanes);
3686 bmpImage->red_mask = saveRed;
3687 bmpImage->blue_mask = saveBlue;
3688 bmpImage->green_mask = saveGreen;
3691 #endif /* HAVE_LIBXXSHM */
3693 TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3694 gdi_display, descr->drawable, descr->xSrc, descr->ySrc, descr->width,
3695 lines, AllPlanes, ZPixmap, bmpImage, descr->xDest, descr->yDest);
3696 XGetSubImage( gdi_display, descr->drawable, descr->xSrc, descr->ySrc,
3697 descr->width, lines, AllPlanes, ZPixmap,
3698 bmpImage, descr->xDest, descr->yDest );
3701 TRACE("Dib: depth=%2d r=%lx g=%lx b=%lx\n",
3702 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3703 TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3704 bmpImage->depth,bmpImage->bits_per_pixel,
3705 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3706 /* Transfer the pixels */
3707 switch(descr->infoBpp)
3710 X11DRV_DIB_GetImageBits_1( descr->lines,(LPVOID)descr->bits,
3711 descr->infoWidth, descr->width,
3712 descr->colorMap, descr->palentry,
3713 bmpImage, descr->dibpitch );
3717 if (descr->compression) {
3718 FIXME("Compression not yet supported!\n");
3719 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 4 ) * abs(descr->lines))
3722 X11DRV_DIB_GetImageBits_4( descr->lines,(LPVOID)descr->bits,
3723 descr->infoWidth, descr->width,
3724 descr->colorMap, descr->palentry,
3725 bmpImage, descr->dibpitch );
3728 if (descr->compression) {
3729 FIXME("Compression not yet supported!\n");
3730 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 8 ) * abs(descr->lines))
3733 X11DRV_DIB_GetImageBits_8( descr->lines, (LPVOID)descr->bits,
3734 descr->infoWidth, descr->width,
3735 descr->colorMap, descr->palentry,
3736 bmpImage, descr->dibpitch );
3740 X11DRV_DIB_GetImageBits_16( descr->lines, (LPVOID)descr->bits,
3741 descr->infoWidth,descr->width,
3743 descr->rMask, descr->gMask, descr->bMask,
3744 bmpImage, descr->dibpitch );
3748 X11DRV_DIB_GetImageBits_24( descr->lines, (LPVOID)descr->bits,
3749 descr->infoWidth,descr->width,
3751 descr->rMask, descr->gMask, descr->bMask,
3752 bmpImage, descr->dibpitch);
3756 X11DRV_DIB_GetImageBits_32( descr->lines, (LPVOID)descr->bits,
3757 descr->infoWidth, descr->width,
3759 descr->rMask, descr->gMask, descr->bMask,
3760 bmpImage, descr->dibpitch);
3764 WARN("(%d): Invalid depth\n", descr->infoBpp );
3768 if (!descr->image) XDestroyImage( bmpImage );
3769 wine_tsx11_unlock();
3773 /*************************************************************************
3774 * X11DRV_SetDIBitsToDevice
3777 INT X11DRV_SetDIBitsToDevice( X11DRV_PDEVICE *physDev, INT xDest, INT yDest, DWORD cx,
3778 DWORD cy, INT xSrc, INT ySrc,
3779 UINT startscan, UINT lines, LPCVOID bits,
3780 const BITMAPINFO *info, UINT coloruse )
3782 X11DRV_DIB_IMAGEBITS_DESCR descr;
3788 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3789 &descr.infoBpp, &descr.compression ) == -1)
3792 top_down = (height < 0);
3793 if (top_down) height = -height;
3797 LPtoDP(physDev->hdc, &pt, 1);
3799 if (!lines || (startscan >= height)) return 0;
3800 if (!top_down && startscan + lines > height) lines = height - startscan;
3802 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3803 * and clamp all values to fit inside [startscan,startscan+lines]
3805 if (ySrc + cy <= startscan + lines)
3807 UINT y = startscan + lines - (ySrc + cy);
3808 if (ySrc < startscan) cy -= (startscan - ySrc);
3811 /* avoid getting unnecessary lines */
3813 if (y >= lines) return 0;
3818 if (y >= lines) return lines;
3819 ySrc = y; /* need to get all lines in top down mode */
3824 if (ySrc >= startscan + lines) return lines;
3825 pt.y += ySrc + cy - (startscan + lines);
3826 cy = startscan + lines - ySrc;
3828 if (cy > lines) cy = lines;
3830 if (xSrc >= width) return lines;
3831 if (xSrc + cx >= width) cx = width - xSrc;
3832 if (!cx || !cy) return lines;
3834 /* Update the pixmap from the DIB section */
3835 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
3837 X11DRV_SetupGCForText( physDev ); /* To have the correct colors */
3839 XSetFunction(gdi_display, physDev->gc, X11DRV_XROPfunction[GetROP2(physDev->hdc) - 1]);
3840 wine_tsx11_unlock();
3842 switch (descr.infoBpp)
3847 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3848 coloruse == DIB_PAL_COLORS ? physDev : NULL, coloruse,
3849 physDev->depth, info, &descr.nColorMap );
3850 if (!descr.colorMap) return 0;
3851 descr.rMask = descr.gMask = descr.bMask = 0;
3855 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3856 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3857 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3863 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3864 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3865 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3870 descr.physDev = physDev;
3873 descr.palentry = NULL;
3874 descr.lines = top_down ? -lines : lines;
3875 descr.infoWidth = width;
3876 descr.depth = physDev->depth;
3877 descr.drawable = physDev->drawable;
3878 descr.gc = physDev->gc;
3881 descr.xDest = physDev->org.x + pt.x;
3882 descr.yDest = physDev->org.y + pt.y;
3885 descr.useShm = FALSE;
3886 descr.dibpitch = ((width * descr.infoBpp + 31) &~31) / 8;
3888 result = X11DRV_DIB_SetImageBits( &descr );
3890 if (descr.infoBpp <= 8)
3891 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3893 /* Update the DIBSection of the pixmap */
3894 X11DRV_UnlockDIBSection(physDev, TRUE);
3899 /***********************************************************************
3900 * SetDIBits (X11DRV.@)
3902 INT X11DRV_SetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan,
3903 UINT lines, LPCVOID bits, const BITMAPINFO *info, UINT coloruse )
3905 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3906 X11DRV_DIB_IMAGEBITS_DESCR descr;
3908 LONG width, height, tmpheight;
3911 descr.physDev = physDev;
3913 if (!physBitmap) return 0;
3915 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3916 &descr.infoBpp, &descr.compression ) == -1)
3920 if (height < 0) height = -height;
3921 if (!lines || (startscan >= height))
3924 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
3926 if (startscan + lines > height) lines = height - startscan;
3928 switch (descr.infoBpp)
3933 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3934 coloruse == DIB_PAL_COLORS ? descr.physDev : NULL, coloruse,
3935 physBitmap->pixmap_depth,
3936 info, &descr.nColorMap );
3937 if (!descr.colorMap) return 0;
3938 descr.rMask = descr.gMask = descr.bMask = 0;
3942 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3943 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3944 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3950 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3951 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3952 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3961 descr.palentry = NULL;
3962 descr.infoWidth = width;
3963 descr.lines = tmpheight >= 0 ? lines : -lines;
3964 descr.depth = physBitmap->pixmap_depth;
3965 descr.drawable = physBitmap->pixmap;
3966 descr.gc = BITMAP_GC(physBitmap);
3970 descr.yDest = height - startscan - lines;
3971 descr.width = bitmap.bmWidth;
3972 descr.height = lines;
3973 descr.useShm = FALSE;
3974 descr.dibpitch = ((descr.infoWidth * descr.infoBpp + 31) &~31) / 8;
3975 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod, FALSE );
3976 result = X11DRV_DIB_SetImageBits( &descr );
3977 X11DRV_DIB_Unlock( physBitmap, TRUE );
3979 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3984 /***********************************************************************
3985 * GetDIBits (X11DRV.@)
3987 INT X11DRV_GetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan, UINT lines,
3988 LPVOID bits, BITMAPINFO *info, UINT coloruse )
3990 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3992 X11DRV_DIB_IMAGEBITS_DESCR descr;
3993 PALETTEENTRY palette[256];
3996 LONG width, tempHeight;
4001 GetPaletteEntries( GetCurrentObject( physDev->hdc, OBJ_PAL ), 0, 256, palette );
4003 if (!physBitmap) return 0;
4004 if (!(obj_size = GetObjectW( hbitmap, sizeof(dib), &dib ))) return 0;
4006 bitmap_type = DIB_GetBitmapInfo( (BITMAPINFOHEADER*)info, &width, &tempHeight, &descr.infoBpp, &descr.compression);
4007 descr.lines = tempHeight;
4008 if (bitmap_type == -1)
4010 ERR("Invalid bitmap\n");
4013 core_header = (bitmap_type == 0);
4014 colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
4016 TRACE("%u scanlines of (%i,%i) -> (%li,%i) starting from %u\n",
4017 lines, dib.dsBm.bmWidth, dib.dsBm.bmHeight, width, descr.lines, startscan);
4019 if( lines > dib.dsBm.bmHeight ) lines = dib.dsBm.bmHeight;
4021 height = descr.lines;
4022 if (height < 0) height = -height;
4023 if( lines > height ) lines = height;
4024 /* Top-down images have a negative biHeight, the scanlines of theses images
4025 * were inverted in X11DRV_DIB_GetImageBits_xx
4026 * To prevent this we simply change the sign of lines
4027 * (the number of scan lines to copy).
4028 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4030 if( descr.lines < 0 && lines > 0) lines = -lines;
4032 if( startscan >= dib.dsBm.bmHeight ) return 0;
4034 descr.colorMap = NULL;
4036 switch (descr.infoBpp)
4041 descr.rMask= descr.gMask = descr.bMask = 0;
4042 if(coloruse == DIB_RGB_COLORS)
4043 descr.colorMap = colorPtr;
4045 int num_colors = 1 << descr.infoBpp, i;
4048 WORD *index = (WORD*)colorPtr;
4049 descr.colorMap = rgb = HeapAlloc(GetProcessHeap(), 0, num_colors * sizeof(RGBQUAD));
4050 for(i = 0; i < num_colors; i++, rgb++, index++) {
4051 colref = X11DRV_PALETTE_ToLogical(X11DRV_PALETTE_ToPhysical(physDev, PALETTEINDEX(*index)));
4052 rgb->rgbRed = GetRValue(colref);
4053 rgb->rgbGreen = GetGValue(colref);
4054 rgb->rgbBlue = GetBValue(colref);
4055 rgb->rgbReserved = 0;
4061 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4062 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4063 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4067 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4068 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4069 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4073 descr.physDev = physDev;
4074 descr.palentry = palette;
4077 descr.infoWidth = width;
4078 descr.lines = lines;
4079 descr.depth = physBitmap->pixmap_depth;
4080 descr.drawable = physBitmap->pixmap;
4081 descr.gc = BITMAP_GC(physBitmap);
4082 descr.width = dib.dsBm.bmWidth;
4083 descr.height = dib.dsBm.bmHeight;
4087 descr.sizeImage = core_header ? 0 : info->bmiHeader.biSizeImage;
4089 if (descr.lines > 0)
4091 descr.ySrc = (descr.height-1) - (startscan + (lines-1));
4095 descr.ySrc = startscan;
4097 #ifdef HAVE_LIBXXSHM
4098 descr.useShm = (obj_size == sizeof(DIBSECTION)) && (physBitmap->shminfo.shmid != -1);
4100 descr.useShm = FALSE;
4102 descr.dibpitch = (obj_size == sizeof(DIBSECTION)) ? dib.dsBm.bmWidthBytes
4103 : (((descr.infoWidth * descr.infoBpp + 31) &~31) / 8);
4105 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod, FALSE );
4106 X11DRV_DIB_GetImageBits( &descr );
4107 X11DRV_DIB_Unlock( physBitmap, TRUE );
4109 if(!core_header && info->bmiHeader.biSizeImage == 0) /* Fill in biSizeImage */
4110 info->bmiHeader.biSizeImage = X11DRV_DIB_GetDIBImageBytes( descr.infoWidth,
4114 if (descr.compression == BI_BITFIELDS)
4116 *(DWORD *)info->bmiColors = descr.rMask;
4117 *((DWORD *)info->bmiColors + 1) = descr.gMask;
4118 *((DWORD *)info->bmiColors + 2) = descr.bMask;
4120 else if (!core_header)
4122 /* if RLE or JPEG compression were supported,
4123 * this line would be invalid. */
4124 info->bmiHeader.biCompression = 0;
4127 if(descr.colorMap && descr.colorMap != colorPtr)
4128 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4132 /***********************************************************************
4133 * DIB_DoProtectDIBSection
4135 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP *physBitmap, DWORD new_prot )
4139 VirtualProtect(physBitmap->base, physBitmap->size, new_prot, &old_prot);
4140 TRACE("Changed protection from %ld to %ld\n", old_prot, new_prot);
4143 /***********************************************************************
4144 * X11DRV_DIB_DoCopyDIBSection
4146 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB,
4147 void *colorMap, int nColorMap,
4149 DWORD xSrc, DWORD ySrc,
4150 DWORD xDest, DWORD yDest,
4151 DWORD width, DWORD height)
4153 DIBSECTION dibSection;
4154 X11DRV_DIB_IMAGEBITS_DESCR descr;
4155 int identity[2] = {0,1};
4157 if (!GetObjectW( physBitmap->hbitmap, sizeof(dibSection), &dibSection )) return;
4159 descr.physDev = NULL;
4160 descr.palentry = NULL;
4161 descr.infoWidth = dibSection.dsBmih.biWidth;
4162 descr.infoBpp = dibSection.dsBmih.biBitCount;
4163 descr.lines = dibSection.dsBmih.biHeight;
4164 descr.image = physBitmap->image;
4165 descr.colorMap = colorMap;
4166 descr.nColorMap = nColorMap;
4167 descr.bits = dibSection.dsBm.bmBits;
4168 descr.depth = physBitmap->pixmap_depth;
4169 descr.compression = dibSection.dsBmih.biCompression;
4171 if(descr.infoBpp == 1)
4172 descr.colorMap = (void*)identity;
4174 switch (descr.infoBpp)
4179 descr.rMask = descr.gMask = descr.bMask = 0;
4183 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0x7c00;
4184 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x03e0;
4185 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x001f;
4190 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0xff0000;
4191 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x00ff00;
4192 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x0000ff;
4197 descr.drawable = dest;
4198 descr.gc = BITMAP_GC(physBitmap);
4201 descr.xDest = xDest;
4202 descr.yDest = yDest;
4203 descr.width = width;
4204 descr.height = height;
4205 descr.sizeImage = 0;
4207 #ifdef HAVE_LIBXXSHM
4208 descr.useShm = (physBitmap->shminfo.shmid != -1);
4210 descr.useShm = FALSE;
4212 descr.dibpitch = dibSection.dsBm.bmWidthBytes;
4216 TRACE("Copying from Pixmap to DIB bits\n");
4217 X11DRV_DIB_GetImageBits( &descr );
4221 TRACE("Copying from DIB bits to Pixmap\n");
4222 X11DRV_DIB_SetImageBits( &descr );
4226 /***********************************************************************
4227 * X11DRV_DIB_CopyDIBSection
4229 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
4230 DWORD xSrc, DWORD ySrc, DWORD xDest, DWORD yDest,
4231 DWORD width, DWORD height)
4234 X_PHYSBITMAP *physBitmap;
4235 int nColorMap = 0, *colorMap = NULL, aColorMap = FALSE;
4237 TRACE("(%p,%p,%ld,%ld,%ld,%ld,%ld,%ld)\n", physDevSrc->hdc, physDevDst->hdc,
4238 xSrc, ySrc, xDest, yDest, width, height);
4239 /* this function is meant as an optimization for BitBlt,
4240 * not to be called otherwise */
4241 physBitmap = physDevSrc->bitmap;
4242 if (!physBitmap || GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib ) != sizeof(dib))
4244 ERR("called for non-DIBSection!?\n");
4247 /* while BitBlt should already have made sure we only get
4248 * positive values, we should check for oversize values */
4249 if ((xSrc < dib.dsBm.bmWidth) &&
4250 (ySrc < dib.dsBm.bmHeight)) {
4251 if (xSrc + width > dib.dsBm.bmWidth)
4252 width = dib.dsBm.bmWidth - xSrc;
4253 if (ySrc + height > dib.dsBm.bmHeight)
4254 height = dib.dsBm.bmHeight - ySrc;
4255 /* if the source bitmap is 8bpp or less, we're supposed to use the
4256 * DC's palette for color conversion (not the DIB color table) */
4257 if (dib.dsBm.bmBitsPixel <= 8) {
4258 HPALETTE hPalette = GetCurrentObject( physDevSrc->hdc, OBJ_PAL );
4259 if (!hPalette || (hPalette == GetStockObject(DEFAULT_PALETTE))) {
4260 /* HACK: no palette has been set in the source DC,
4261 * use the DIB colormap instead - this is necessary in some
4262 * cases since we need to do depth conversion in some places
4263 * where real Windows can just copy data straight over */
4264 colorMap = physBitmap->colorMap;
4265 nColorMap = physBitmap->nColorMap;
4267 colorMap = X11DRV_DIB_BuildColorMap( physDevSrc, (WORD)-1,
4268 dib.dsBm.bmBitsPixel,
4269 (BITMAPINFO*)&dib.dsBmih,
4271 if (colorMap) aColorMap = TRUE;
4274 /* perform the copy */
4275 X11DRV_DIB_DoCopyDIBSection(physBitmap, FALSE, colorMap, nColorMap,
4276 physDevDst->drawable, xSrc, ySrc,
4277 physDevDst->org.x + xDest, physDevDst->org.y + yDest,
4279 /* free color mapping */
4281 HeapFree(GetProcessHeap(), 0, colorMap);
4285 /***********************************************************************
4286 * X11DRV_DIB_DoUpdateDIBSection
4288 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB)
4292 GetObjectW( physBitmap->hbitmap, sizeof(bitmap), &bitmap );
4293 X11DRV_DIB_DoCopyDIBSection(physBitmap, toDIB,
4294 physBitmap->colorMap, physBitmap->nColorMap,
4295 physBitmap->pixmap, 0, 0, 0, 0,
4296 bitmap.bmWidth, bitmap.bmHeight);
4299 /***********************************************************************
4300 * X11DRV_DIB_FaultHandler
4302 static LONG CALLBACK X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep )
4304 X_PHYSBITMAP *physBitmap = NULL;
4309 if (ep->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
4310 return EXCEPTION_CONTINUE_SEARCH;
4312 addr = (BYTE *)ep->ExceptionRecord->ExceptionInformation[1];
4314 EnterCriticalSection(&dibs_cs);
4315 LIST_FOR_EACH( ptr, &dibs_list )
4317 physBitmap = LIST_ENTRY( ptr, X_PHYSBITMAP, entry );
4318 if ((physBitmap->base <= addr) && (addr < physBitmap->base + physBitmap->size))
4324 LeaveCriticalSection(&dibs_cs);
4326 if (!found) return EXCEPTION_CONTINUE_SEARCH;
4328 X11DRV_DIB_Lock( physBitmap, DIB_Status_None, FALSE );
4329 if (ep->ExceptionRecord->ExceptionInformation[0]) {
4330 /* the app tried to write the DIB bits */
4331 X11DRV_DIB_Coerce( physBitmap, DIB_Status_AppMod, FALSE );
4333 /* the app tried to read the DIB bits */
4334 X11DRV_DIB_Coerce( physBitmap, DIB_Status_InSync, FALSE );
4336 X11DRV_DIB_Unlock( physBitmap, TRUE );
4338 return EXCEPTION_CONTINUE_EXECUTION;
4341 /***********************************************************************
4344 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *physBitmap, INT req, BOOL lossy)
4346 INT ret = DIB_Status_None;
4348 if (!physBitmap->image) return ret; /* not a DIB section */
4349 EnterCriticalSection(&physBitmap->lock);
4350 ret = physBitmap->status;
4352 case DIB_Status_GdiMod:
4353 /* GDI access - request to draw on pixmap */
4354 switch (physBitmap->status)
4357 case DIB_Status_None:
4358 physBitmap->p_status = DIB_Status_GdiMod;
4359 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4362 case DIB_Status_GdiMod:
4363 TRACE("GdiMod requested in status GdiMod\n" );
4364 physBitmap->p_status = DIB_Status_GdiMod;
4367 case DIB_Status_InSync:
4368 TRACE("GdiMod requested in status InSync\n" );
4369 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4370 physBitmap->status = DIB_Status_GdiMod;
4371 physBitmap->p_status = DIB_Status_InSync;
4374 case DIB_Status_AppMod:
4375 TRACE("GdiMod requested in status AppMod\n" );
4377 /* make it readonly to avoid app changing data while we copy */
4378 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4379 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4381 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4382 physBitmap->p_status = DIB_Status_AppMod;
4383 physBitmap->status = DIB_Status_GdiMod;
4388 case DIB_Status_InSync:
4389 /* App access - request access to read DIB surface */
4390 /* (typically called from signal handler) */
4391 switch (physBitmap->status)
4394 case DIB_Status_None:
4395 /* shouldn't happen from signal handler */
4398 case DIB_Status_GdiMod:
4399 TRACE("InSync requested in status GdiMod\n" );
4401 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4402 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4404 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4405 physBitmap->status = DIB_Status_InSync;
4408 case DIB_Status_InSync:
4409 TRACE("InSync requested in status InSync\n" );
4410 /* shouldn't happen from signal handler */
4413 case DIB_Status_AppMod:
4414 TRACE("InSync requested in status AppMod\n" );
4415 /* no reason to do anything here, and this
4416 * shouldn't happen from signal handler */
4421 case DIB_Status_AppMod:
4422 /* App access - request access to write DIB surface */
4423 /* (typically called from signal handler) */
4424 switch (physBitmap->status)
4427 case DIB_Status_None:
4428 /* shouldn't happen from signal handler */
4431 case DIB_Status_GdiMod:
4432 TRACE("AppMod requested in status GdiMod\n" );
4433 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4434 if (!lossy) X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4435 physBitmap->status = DIB_Status_AppMod;
4438 case DIB_Status_InSync:
4439 TRACE("AppMod requested in status InSync\n" );
4440 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4441 physBitmap->status = DIB_Status_AppMod;
4444 case DIB_Status_AppMod:
4445 TRACE("AppMod requested in status AppMod\n" );
4446 /* shouldn't happen from signal handler */
4451 /* it is up to the caller to do the copy/conversion, probably
4452 * using the return value to decide where to copy from */
4454 LeaveCriticalSection(&physBitmap->lock);
4458 /***********************************************************************
4461 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *physBitmap, INT req, BOOL lossy)
4463 INT ret = DIB_Status_None;
4465 if (!physBitmap->image) return ret; /* not a DIB section */
4466 TRACE("Locking %p from thread %04lx\n", physBitmap->hbitmap, GetCurrentThreadId());
4467 EnterCriticalSection(&physBitmap->lock);
4468 ret = physBitmap->status;
4469 if (req != DIB_Status_None)
4470 X11DRV_DIB_Coerce(physBitmap, req, lossy);
4474 /***********************************************************************
4477 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *physBitmap, BOOL commit)
4479 if (!physBitmap->image) return; /* not a DIB section */
4480 switch (physBitmap->status)
4483 case DIB_Status_None:
4484 /* in case anyone is wondering, this is the "signal handler doesn't
4485 * work" case, where we always have to be ready for app access */
4487 switch (physBitmap->p_status)
4489 case DIB_Status_GdiMod:
4490 TRACE("Unlocking and syncing from GdiMod\n" );
4491 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4495 TRACE("Unlocking without needing to sync\n" );
4499 else TRACE("Unlocking with no changes\n");
4500 physBitmap->p_status = DIB_Status_None;
4503 case DIB_Status_GdiMod:
4504 TRACE("Unlocking in status GdiMod\n" );
4505 /* DIB was protected in Coerce */
4507 /* no commit, revert to InSync if applicable */
4508 if ((physBitmap->p_status == DIB_Status_InSync) ||
4509 (physBitmap->p_status == DIB_Status_AppMod)) {
4510 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4511 physBitmap->status = DIB_Status_InSync;
4516 case DIB_Status_InSync:
4517 TRACE("Unlocking in status InSync\n" );
4518 /* DIB was already protected in Coerce */
4521 case DIB_Status_AppMod:
4522 TRACE("Unlocking in status AppMod\n" );
4523 /* DIB was already protected in Coerce */
4524 /* this case is ordinary only called from the signal handler,
4525 * so we don't bother to check for !commit */
4528 LeaveCriticalSection(&physBitmap->lock);
4529 TRACE("Unlocked %p\n", physBitmap->hbitmap);
4532 /***********************************************************************
4533 * X11DRV_CoerceDIBSection
4535 INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req, BOOL lossy)
4537 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4538 return X11DRV_DIB_Coerce(physDev->bitmap, req, lossy);
4541 /***********************************************************************
4542 * X11DRV_LockDIBSection
4544 INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req, BOOL lossy)
4546 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4547 return X11DRV_DIB_Lock(physDev->bitmap, req, lossy);
4550 /***********************************************************************
4551 * X11DRV_UnlockDIBSection
4553 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
4555 if (!physDev || !physDev->bitmap) return;
4556 X11DRV_DIB_Unlock(physDev->bitmap, commit);
4560 #ifdef HAVE_LIBXXSHM
4561 /***********************************************************************
4562 * X11DRV_XShmErrorHandler
4565 static int XShmErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
4567 return 1; /* FIXME: should check event contents */
4570 /***********************************************************************
4571 * X11DRV_XShmCreateImage
4574 static XImage *X11DRV_XShmCreateImage( int width, int height, int bpp,
4575 XShmSegmentInfo* shminfo)
4579 image = XShmCreateImage(gdi_display, visual, bpp, ZPixmap, NULL, shminfo, width, height);
4582 shminfo->shmid = shmget(IPC_PRIVATE, image->bytes_per_line * height,
4584 if( shminfo->shmid != -1 )
4586 shminfo->shmaddr = image->data = shmat(shminfo->shmid, 0, 0);
4587 if( shminfo->shmaddr != (char*)-1 )
4591 shminfo->readOnly = FALSE;
4592 X11DRV_expect_error( gdi_display, XShmErrorHandler, NULL );
4593 ok = (XShmAttach( gdi_display, shminfo ) != 0);
4594 XSync( gdi_display, False );
4595 if (X11DRV_check_error()) ok = FALSE;
4598 shmctl(shminfo->shmid, IPC_RMID, 0);
4599 return image; /* Success! */
4601 /* An error occurred */
4602 shmdt(shminfo->shmaddr);
4604 shmctl(shminfo->shmid, IPC_RMID, 0);
4605 shminfo->shmid = -1;
4607 XFlush(gdi_display);
4608 XDestroyImage(image);
4613 #endif /* HAVE_LIBXXSHM */
4616 /***********************************************************************
4617 * X11DRV_CreateDIBSection (X11DRV.@)
4619 HBITMAP X11DRV_CreateDIBSection( X11DRV_PDEVICE *physDev, HBITMAP hbitmap,
4620 const BITMAPINFO *bmi, UINT usage )
4622 X_PHYSBITMAP *physBitmap;
4625 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return 0;
4626 physBitmap->status = DIB_Status_None;
4628 GetObjectW( hbitmap, sizeof(dib), &dib );
4630 /* create color map */
4631 if (dib.dsBm.bmBitsPixel <= 8)
4633 physBitmap->colorMap = X11DRV_DIB_BuildColorMap( usage == DIB_PAL_COLORS ? physDev : NULL,
4634 usage, dib.dsBm.bmBitsPixel, bmi,
4635 &physBitmap->nColorMap );
4636 physBitmap->colorTable = X11DRV_DIB_BuildColorTable( physDev, usage, dib.dsBm.bmBitsPixel, bmi );
4639 /* create pixmap and X image */
4641 physBitmap->pixmap_depth = (dib.dsBm.bmBitsPixel == 1) ? 1 : screen_depth;
4642 physBitmap->pixmap = XCreatePixmap( gdi_display, root_window, dib.dsBm.bmWidth,
4643 dib.dsBm.bmHeight, physBitmap->pixmap_depth );
4644 #ifdef HAVE_LIBXXSHM
4645 physBitmap->shminfo.shmid = -1;
4646 if (!XShmQueryExtension(gdi_display) ||
4647 !(physBitmap->image = X11DRV_XShmCreateImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4648 physBitmap->pixmap_depth, &physBitmap->shminfo )) )
4650 physBitmap->image = X11DRV_DIB_CreateXImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4651 physBitmap->pixmap_depth );
4652 wine_tsx11_unlock();
4653 if (!physBitmap->pixmap || !physBitmap->image) return 0;
4655 /* install fault handler */
4656 InitializeCriticalSection( &physBitmap->lock );
4658 physBitmap->base = dib.dsBm.bmBits;
4659 physBitmap->size = dib.dsBmih.biSizeImage;
4660 physBitmap->status = DIB_Status_AppMod;
4663 dibs_handler = AddVectoredExceptionHandler( TRUE, X11DRV_DIB_FaultHandler );
4664 EnterCriticalSection( &dibs_cs );
4665 list_add_head( &dibs_list, &physBitmap->entry );
4666 LeaveCriticalSection( &dibs_cs );
4668 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4673 /***********************************************************************
4674 * X11DRV_DIB_DeleteDIBSection
4676 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP *physBitmap, DIBSECTION *dib)
4680 EnterCriticalSection( &dibs_cs );
4681 list_remove( &physBitmap->entry );
4682 last = list_empty( &dibs_list );
4683 LeaveCriticalSection( &dibs_cs );
4687 RemoveVectoredExceptionHandler( dibs_handler );
4688 dibs_handler = NULL;
4691 if (dib->dshSection)
4692 X11DRV_DIB_Coerce(physBitmap, DIB_Status_InSync, FALSE);
4694 if (physBitmap->image)
4697 #ifdef HAVE_LIBXXSHM
4698 if (physBitmap->shminfo.shmid != -1)
4700 XShmDetach( gdi_display, &(physBitmap->shminfo) );
4701 XDestroyImage( physBitmap->image );
4702 shmdt( physBitmap->shminfo.shmaddr );
4703 physBitmap->shminfo.shmid = -1;
4707 XDestroyImage( physBitmap->image );
4708 wine_tsx11_unlock();
4711 HeapFree(GetProcessHeap(), 0, physBitmap->colorMap);
4712 HeapFree(GetProcessHeap(), 0, physBitmap->colorTable);
4713 DeleteCriticalSection(&physBitmap->lock);
4716 /***********************************************************************
4717 * SetDIBColorTable (X11DRV.@)
4719 UINT X11DRV_SetDIBColorTable( X11DRV_PDEVICE *physDev, UINT start, UINT count, const RGBQUAD *colors )
4723 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4725 if (!physBitmap) return 0;
4726 GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib );
4728 if (physBitmap->colorMap && start < physBitmap->nColorMap) {
4729 UINT end = count + start;
4730 if (end > physBitmap->nColorMap) end = physBitmap->nColorMap;
4732 * Changing color table might change the mapping between
4733 * DIB colors and X11 colors and thus alter the visible state
4734 * of the bitmap object.
4736 X11DRV_DIB_Lock( physBitmap, DIB_Status_AppMod, FALSE );
4737 memcpy(physBitmap->colorTable + start, colors, (end - start) * sizeof(RGBQUAD));
4738 X11DRV_DIB_GenColorMap( physDev, physBitmap->colorMap, DIB_RGB_COLORS,
4739 dib.dsBm.bmBitsPixel,
4740 TRUE, colors, start, end );
4741 X11DRV_DIB_Unlock( physBitmap, TRUE );
4747 /***********************************************************************
4748 * GetDIBColorTable (X11DRV.@)
4750 UINT X11DRV_GetDIBColorTable( X11DRV_PDEVICE *physDev, UINT start, UINT count, RGBQUAD *colors )
4753 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4755 if (physBitmap && physBitmap->colorTable && start < physBitmap->nColorMap) {
4756 if (start + count > physBitmap->nColorMap) count = physBitmap->nColorMap - start;
4757 memcpy(colors, physBitmap->colorTable + start, count * sizeof(RGBQUAD));
4765 /***********************************************************************
4766 * X11DRV_DIB_CreateDIBFromBitmap
4768 * Allocates a packed DIB and copies the bitmap data into it.
4770 HGLOBAL X11DRV_DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
4775 LPBITMAPINFOHEADER pbmiHeader;
4776 unsigned int cDataSize, cPackedSize, OffsetBits;
4779 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
4782 * A packed DIB contains a BITMAPINFO structure followed immediately by
4783 * an optional color palette and the pixel data.
4786 /* Calculate the size of the packed DIB */
4787 cDataSize = X11DRV_DIB_GetDIBWidthBytes( bmp.bmWidth, bmp.bmBitsPixel ) * abs( bmp.bmHeight );
4788 cPackedSize = sizeof(BITMAPINFOHEADER)
4789 + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
4791 /* Get the offset to the bits */
4792 OffsetBits = cPackedSize - cDataSize;
4794 /* Allocate the packed DIB */
4795 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
4796 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
4800 WARN("Could not allocate packed DIB!\n");
4804 /* A packed DIB starts with a BITMAPINFOHEADER */
4805 pPackedDIB = GlobalLock(hPackedDIB);
4806 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
4808 /* Init the BITMAPINFOHEADER */
4809 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
4810 pbmiHeader->biWidth = bmp.bmWidth;
4811 pbmiHeader->biHeight = bmp.bmHeight;
4812 pbmiHeader->biPlanes = 1;
4813 pbmiHeader->biBitCount = bmp.bmBitsPixel;
4814 pbmiHeader->biCompression = BI_RGB;
4815 pbmiHeader->biSizeImage = 0;
4816 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
4817 pbmiHeader->biClrUsed = 0;
4818 pbmiHeader->biClrImportant = 0;
4820 /* Retrieve the DIB bits from the bitmap and fill in the
4821 * DIB color table if present */
4823 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
4824 hBmp, /* Handle to bitmap */
4825 0, /* First scan line to set in dest bitmap */
4826 bmp.bmHeight, /* Number of scan lines to copy */
4827 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
4828 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
4829 0); /* RGB or palette index */
4830 GlobalUnlock(hPackedDIB);
4832 /* Cleanup if GetDIBits failed */
4833 if (nLinesCopied != bmp.bmHeight)
4835 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
4836 GlobalFree(hPackedDIB);
4843 /**************************************************************************
4844 * X11DRV_DIB_CreateDIBFromPixmap
4846 * Allocates a packed DIB and copies the Pixmap data into it.
4847 * The Pixmap passed in is deleted after the conversion.
4849 HGLOBAL X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap, HDC hdc)
4852 X_PHYSBITMAP *physBitmap;
4853 HBITMAP hBmp = 0, old;
4854 HGLOBAL hPackedDIB = 0;
4856 int x,y; /* Unused */
4857 unsigned border_width; /* Unused */
4858 unsigned int depth, width, height;
4860 /* Get the Pixmap dimensions and bit depth */
4862 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
4863 &border_width, &depth)) depth = 0;
4864 wine_tsx11_unlock();
4865 if (!depth) return 0;
4867 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
4868 width, height, depth);
4871 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
4872 * and make it a container for the pixmap passed.
4874 hBmp = CreateBitmap( width, height, 1, depth, NULL );
4876 /* force bitmap to be owned by a screen DC */
4877 hdcMem = CreateCompatibleDC( hdc );
4878 old = SelectObject( hdcMem, hBmp );
4880 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4883 if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
4884 physBitmap->pixmap = pixmap;
4885 wine_tsx11_unlock();
4887 SelectObject( hdcMem, old );
4891 * Create a packed DIB from the Pixmap wrapper bitmap created above.
4892 * A packed DIB contains a BITMAPINFO structure followed immediately by
4893 * an optional color palette and the pixel data.
4895 hPackedDIB = X11DRV_DIB_CreateDIBFromBitmap(hdc, hBmp);
4897 /* We can now get rid of the HBITMAP wrapper we created earlier.
4898 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
4902 TRACE("\tReturning packed DIB %p\n", hPackedDIB);
4907 /**************************************************************************
4908 * X11DRV_DIB_CreatePixmapFromDIB
4910 * Creates a Pixmap from a packed DIB
4912 Pixmap X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB, HDC hdc )
4915 X_PHYSBITMAP *physBitmap;
4919 /* Create a DDB from the DIB */
4921 pbmi = GlobalLock(hPackedDIB);
4922 hBmp = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT,
4923 (LPBYTE)pbmi + X11DRV_DIB_BitmapInfoSize( pbmi, DIB_RGB_COLORS ),
4924 pbmi, DIB_RGB_COLORS);
4925 GlobalUnlock(hPackedDIB);
4927 /* clear the physBitmap so that we can steal its pixmap */
4928 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4929 pixmap = physBitmap->pixmap;
4930 physBitmap->pixmap = 0;
4932 /* Delete the DDB we created earlier now that we have stolen its pixmap */
4935 TRACE("Returning Pixmap %ld\n", pixmap);