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_SetImageBits_1
526 * SetDIBits for a 1-bit deep DIB.
528 static void X11DRV_DIB_SetImageBits_1( int lines, const BYTE *srcbits,
529 DWORD srcwidth, DWORD dstwidth, int left,
530 int *colors, XImage *bmpImage, DWORD linebytes)
539 srcbits = srcbits + linebytes * (lines - 1);
540 linebytes = -linebytes;
543 if ((extra = (left & 7)) != 0) {
547 srcbits += left >> 3;
548 width = min(srcwidth, dstwidth);
550 /* ==== pal 1 dib -> any bmp format ==== */
551 for (h = lines-1; h >=0; h--) {
553 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
554 for (i = width/8, x = left; i > 0; i--) {
556 XPutPixel( bmpImage, x++, h, colors[ srcval >> 7] );
557 XPutPixel( bmpImage, x++, h, colors[(srcval >> 6) & 1] );
558 XPutPixel( bmpImage, x++, h, colors[(srcval >> 5) & 1] );
559 XPutPixel( bmpImage, x++, h, colors[(srcval >> 4) & 1] );
560 XPutPixel( bmpImage, x++, h, colors[(srcval >> 3) & 1] );
561 XPutPixel( bmpImage, x++, h, colors[(srcval >> 2) & 1] );
562 XPutPixel( bmpImage, x++, h, colors[(srcval >> 1) & 1] );
563 XPutPixel( bmpImage, x++, h, colors[ srcval & 1] );
569 case 7: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
570 case 6: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
571 case 5: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
572 case 4: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
573 case 3: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
574 case 2: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
575 case 1: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]);
578 srcbits += linebytes;
582 /***********************************************************************
583 * X11DRV_DIB_GetImageBits_1
585 * GetDIBits for a 1-bit deep DIB.
587 static void X11DRV_DIB_GetImageBits_1( int lines, BYTE *dstbits,
588 DWORD dstwidth, DWORD srcwidth,
589 RGBQUAD *colors, PALETTEENTRY *srccolors,
590 XImage *bmpImage, DWORD linebytes )
593 int h, width = min(dstwidth, srcwidth);
597 dstbits = dstbits + linebytes * (lines - 1);
598 linebytes = -linebytes;
601 switch (bmpImage->depth)
605 if (bmpImage->red_mask==0 && bmpImage->green_mask==0 && bmpImage->blue_mask==0 && srccolors) {
606 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
609 for (h=lines-1; h>=0; h--) {
613 for (x=0; x<width; x++) {
615 srcval=srccolors[XGetPixel(bmpImage, x, h)];
616 dstval|=(X11DRV_DIB_GetNearestIndex
620 srcval.peBlue) << (7 - (x & 7)));
629 dstbits += linebytes;
637 if (bmpImage->red_mask==0 && bmpImage->green_mask==0 && bmpImage->blue_mask==0 && srccolors) {
638 /* ==== pal 8 bmp -> pal 1 dib ==== */
640 const BYTE* srcpixel;
643 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
645 for (h=0; h<lines; h++) {
650 for (x=0; x<width; x++) {
652 srcval=srccolors[(int)*srcpixel++];
653 dstval|=(X11DRV_DIB_GetNearestIndex
657 srcval.peBlue) << (7-(x&7)) );
666 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
667 dstbits += linebytes;
678 const WORD* srcpixel;
681 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
683 if (bmpImage->green_mask==0x03e0) {
684 if (bmpImage->red_mask==0x7c00) {
685 /* ==== rgb 555 bmp -> pal 1 dib ==== */
686 for (h=0; h<lines; h++) {
691 for (x=0; x<width; x++) {
694 dstval|=(X11DRV_DIB_GetNearestIndex
696 ((srcval >> 7) & 0xf8) | /* r */
697 ((srcval >> 12) & 0x07),
698 ((srcval >> 2) & 0xf8) | /* g */
699 ((srcval >> 7) & 0x07),
700 ((srcval << 3) & 0xf8) | /* b */
701 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
710 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
711 dstbits += linebytes;
713 } else if (bmpImage->blue_mask==0x7c00) {
714 /* ==== bgr 555 bmp -> pal 1 dib ==== */
715 for (h=0; h<lines; h++) {
720 for (x=0; x<width; x++) {
723 dstval|=(X11DRV_DIB_GetNearestIndex
725 ((srcval << 3) & 0xf8) | /* r */
726 ((srcval >> 2) & 0x07),
727 ((srcval >> 2) & 0xf8) | /* g */
728 ((srcval >> 7) & 0x07),
729 ((srcval >> 7) & 0xf8) | /* b */
730 ((srcval >> 12) & 0x07) ) << (7-(x&7)) );
739 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
740 dstbits += linebytes;
745 } else if (bmpImage->green_mask==0x07e0) {
746 if (bmpImage->red_mask==0xf800) {
747 /* ==== rgb 565 bmp -> pal 1 dib ==== */
748 for (h=0; h<lines; h++) {
753 for (x=0; x<width; x++) {
756 dstval|=(X11DRV_DIB_GetNearestIndex
758 ((srcval >> 8) & 0xf8) | /* r */
759 ((srcval >> 13) & 0x07),
760 ((srcval >> 3) & 0xfc) | /* g */
761 ((srcval >> 9) & 0x03),
762 ((srcval << 3) & 0xf8) | /* b */
763 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
772 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
773 dstbits += linebytes;
775 } else if (bmpImage->blue_mask==0xf800) {
776 /* ==== bgr 565 bmp -> pal 1 dib ==== */
777 for (h=0; h<lines; h++) {
782 for (x=0; x<width; x++) {
785 dstval|=(X11DRV_DIB_GetNearestIndex
787 ((srcval << 3) & 0xf8) | /* r */
788 ((srcval >> 2) & 0x07),
789 ((srcval >> 3) & 0xfc) | /* g */
790 ((srcval >> 9) & 0x03),
791 ((srcval >> 8) & 0xf8) | /* b */
792 ((srcval >> 13) & 0x07) ) << (7-(x&7)) );
801 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
802 dstbits += linebytes;
821 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
822 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
824 if (bmpImage->green_mask!=0x00ff00 ||
825 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
827 } else if (bmpImage->blue_mask==0xff) {
828 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
829 for (h=0; h<lines; h++) {
834 for (x=0; x<width; x++) {
835 dstval|=(X11DRV_DIB_GetNearestIndex
839 srcbyte[0]) << (7-(x&7)) );
840 srcbyte+=bytes_per_pixel;
849 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
850 dstbits += linebytes;
853 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
854 for (h=0; h<lines; h++) {
859 for (x=0; x<width; x++) {
860 dstval|=(X11DRV_DIB_GetNearestIndex
864 srcbyte[2]) << (7-(x&7)) );
865 srcbyte+=bytes_per_pixel;
874 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
875 dstbits += linebytes;
885 unsigned long white = (1 << bmpImage->bits_per_pixel) - 1;
887 /* ==== any bmp format -> pal 1 dib ==== */
888 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB\n",
889 bmpImage->bits_per_pixel, bmpImage->red_mask,
890 bmpImage->green_mask, bmpImage->blue_mask );
892 for (h=lines-1; h>=0; h--) {
896 for (x=0; x<width; x++) {
897 dstval|=(XGetPixel( bmpImage, x, h) >= white) << (7 - (x&7));
906 dstbits += linebytes;
913 /***********************************************************************
914 * X11DRV_DIB_SetImageBits_4
916 * SetDIBits for a 4-bit deep DIB.
918 static void X11DRV_DIB_SetImageBits_4( int lines, const BYTE *srcbits,
919 DWORD srcwidth, DWORD dstwidth, int left,
920 int *colors, XImage *bmpImage, DWORD linebytes)
928 srcbits = srcbits + linebytes * (lines - 1);
929 linebytes = -linebytes;
936 srcbits += left >> 1;
937 width = min(srcwidth, dstwidth);
939 /* ==== pal 4 dib -> any bmp format ==== */
940 for (h = lines-1; h >= 0; h--) {
942 for (i = width/2, x = left; i > 0; i--) {
943 BYTE srcval=*srcbyte++;
944 XPutPixel( bmpImage, x++, h, colors[srcval >> 4] );
945 XPutPixel( bmpImage, x++, h, colors[srcval & 0x0f] );
948 XPutPixel( bmpImage, x, h, colors[*srcbyte >> 4] );
949 srcbits += linebytes;
955 /***********************************************************************
956 * X11DRV_DIB_GetImageBits_4
958 * GetDIBits for a 4-bit deep DIB.
960 static void X11DRV_DIB_GetImageBits_4( int lines, BYTE *dstbits,
961 DWORD srcwidth, DWORD dstwidth,
962 RGBQUAD *colors, PALETTEENTRY *srccolors,
963 XImage *bmpImage, DWORD linebytes )
966 int h, width = min(srcwidth, dstwidth);
972 dstbits = dstbits + ( linebytes * (lines-1) );
973 linebytes = -linebytes;
978 switch (bmpImage->depth) {
981 if (bmpImage->red_mask==0 && bmpImage->green_mask==0 && bmpImage->blue_mask==0 && srccolors) {
982 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
985 for (h = lines-1; h >= 0; h--) {
989 for (x = 0; x < width; x++) {
991 srcval=srccolors[XGetPixel(bmpImage, x, h)];
992 dstval|=(X11DRV_DIB_GetNearestIndex
996 srcval.peBlue) << (4-((x&1)<<2)));
1005 dstbits += linebytes;
1013 if (bmpImage->red_mask==0 && bmpImage->green_mask==0 && bmpImage->blue_mask==0 && srccolors) {
1014 /* ==== pal 8 bmp -> pal 4 dib ==== */
1015 const void* srcbits;
1016 const BYTE *srcpixel;
1019 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1020 for (h=0; h<lines; h++) {
1025 for (x=0; x<width; x++) {
1026 PALETTEENTRY srcval;
1027 srcval = srccolors[(int)*srcpixel++];
1028 dstval|=(X11DRV_DIB_GetNearestIndex
1032 srcval.peBlue) << (4*(1-(x&1))) );
1041 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1042 dstbits += linebytes;
1052 const void* srcbits;
1053 const WORD* srcpixel;
1056 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1058 if (bmpImage->green_mask==0x03e0) {
1059 if (bmpImage->red_mask==0x7c00) {
1060 /* ==== rgb 555 bmp -> pal 4 dib ==== */
1061 for (h=0; h<lines; h++) {
1066 for (x=0; x<width; x++) {
1069 dstval|=(X11DRV_DIB_GetNearestIndex
1071 ((srcval >> 7) & 0xf8) | /* r */
1072 ((srcval >> 12) & 0x07),
1073 ((srcval >> 2) & 0xf8) | /* g */
1074 ((srcval >> 7) & 0x07),
1075 ((srcval << 3) & 0xf8) | /* b */
1076 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1085 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1086 dstbits += linebytes;
1088 } else if (bmpImage->blue_mask==0x7c00) {
1089 /* ==== bgr 555 bmp -> pal 4 dib ==== */
1090 for (h=0; h<lines; h++) {
1095 for (x=0; x<width; x++) {
1098 dstval|=(X11DRV_DIB_GetNearestIndex
1100 ((srcval << 3) & 0xf8) | /* r */
1101 ((srcval >> 2) & 0x07),
1102 ((srcval >> 2) & 0xf8) | /* g */
1103 ((srcval >> 7) & 0x07),
1104 ((srcval >> 7) & 0xf8) | /* b */
1105 ((srcval >> 12) & 0x07) ) << ((1-(x&1))<<2) );
1114 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1115 dstbits += linebytes;
1120 } else if (bmpImage->green_mask==0x07e0) {
1121 if (bmpImage->red_mask==0xf800) {
1122 /* ==== rgb 565 bmp -> pal 4 dib ==== */
1123 for (h=0; h<lines; h++) {
1128 for (x=0; x<width; x++) {
1131 dstval|=(X11DRV_DIB_GetNearestIndex
1133 ((srcval >> 8) & 0xf8) | /* r */
1134 ((srcval >> 13) & 0x07),
1135 ((srcval >> 3) & 0xfc) | /* g */
1136 ((srcval >> 9) & 0x03),
1137 ((srcval << 3) & 0xf8) | /* b */
1138 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1147 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1148 dstbits += linebytes;
1150 } else if (bmpImage->blue_mask==0xf800) {
1151 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1152 for (h=0; h<lines; h++) {
1157 for (x=0; x<width; x++) {
1160 dstval|=(X11DRV_DIB_GetNearestIndex
1162 ((srcval << 3) & 0xf8) | /* r */
1163 ((srcval >> 2) & 0x07),
1164 ((srcval >> 3) & 0xfc) | /* g */
1165 ((srcval >> 9) & 0x03),
1166 ((srcval >> 8) & 0xf8) | /* b */
1167 ((srcval >> 13) & 0x07) ) << ((1-(x&1))<<2) );
1176 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1177 dstbits += linebytes;
1189 if (bmpImage->bits_per_pixel==24) {
1190 const void* srcbits;
1191 const BYTE *srcbyte;
1194 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1196 if (bmpImage->green_mask!=0x00ff00 ||
1197 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1199 } else if (bmpImage->blue_mask==0xff) {
1200 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1201 for (h=0; h<lines; h++) {
1204 for (x=0; x<width/2; x++) {
1205 /* Do 2 pixels at a time */
1206 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1211 X11DRV_DIB_GetNearestIndex
1219 /* And the the odd pixel */
1220 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1226 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1227 dstbits += linebytes;
1230 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1231 for (h=0; h<lines; h++) {
1234 for (x=0; x<width/2; x++) {
1235 /* Do 2 pixels at a time */
1236 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1241 X11DRV_DIB_GetNearestIndex
1249 /* And the the odd pixel */
1250 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1256 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1257 dstbits += linebytes;
1266 const void* srcbits;
1267 const BYTE *srcbyte;
1270 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1272 if (bmpImage->green_mask!=0x00ff00 ||
1273 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1275 } else if (bmpImage->blue_mask==0xff) {
1276 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1277 for (h=0; h<lines; h++) {
1280 for (x=0; x<width/2; x++) {
1281 /* Do 2 pixels at a time */
1282 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1287 X11DRV_DIB_GetNearestIndex
1295 /* And the the odd pixel */
1296 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1302 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1303 dstbits += linebytes;
1306 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1307 for (h=0; h<lines; h++) {
1310 for (x=0; x<width/2; x++) {
1311 /* Do 2 pixels at a time */
1312 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1317 X11DRV_DIB_GetNearestIndex
1325 /* And the the odd pixel */
1326 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1332 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1333 dstbits += linebytes;
1344 /* ==== any bmp format -> pal 4 dib ==== */
1345 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1346 bmpImage->bits_per_pixel, bmpImage->red_mask,
1347 bmpImage->green_mask, bmpImage->blue_mask );
1348 for (h=lines-1; h>=0; h--) {
1350 for (x=0; x<(width & ~1); x+=2) {
1351 *dstbyte++=(X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4) |
1352 X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x+1, h), 0);
1355 *dstbyte=(X11DRV_DIB_MapColor((int *)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4);
1357 dstbits += linebytes;
1364 /***********************************************************************
1365 * X11DRV_DIB_SetImageBits_RLE4
1367 * SetDIBits for a 4-bit deep compressed DIB.
1369 static void X11DRV_DIB_SetImageBits_RLE4( int lines, const BYTE *bits,
1370 DWORD srcwidth, DWORD dstwidth,
1371 int left, int *colors,
1374 unsigned int x = 0, width = min(srcwidth, dstwidth);
1375 int y = lines - 1, c, length;
1376 const BYTE *begin = bits;
1381 if (length) { /* encoded */
1384 if (x >= (left + width)) break;
1385 if( x >= left) XPutPixel(bmpImage, x, y, colors[c >> 4]);
1387 if (!length--) break;
1388 if (x >= (left + width)) break;
1389 if( x >= left) XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1409 default: /* absolute */
1412 if (x >= left && x < (left + width))
1413 XPutPixel(bmpImage, x, y, colors[c >> 4]);
1415 if (!length--) break;
1416 if (x >= left && x < (left + width))
1417 XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1420 if ((bits - begin) & 1)
1429 /***********************************************************************
1430 * X11DRV_DIB_SetImageBits_8
1432 * SetDIBits for an 8-bit deep DIB.
1434 static void X11DRV_DIB_SetImageBits_8( int lines, const BYTE *srcbits,
1435 DWORD srcwidth, DWORD dstwidth, int left,
1436 const int *colors, XImage *bmpImage,
1440 int h, width = min(srcwidth, dstwidth);
1441 const BYTE* srcbyte;
1447 srcbits = srcbits + linebytes * (lines-1);
1448 linebytes = -linebytes;
1453 switch (bmpImage->depth) {
1456 #if defined(__i386__) && defined(__GNUC__)
1457 /* Some X servers might have 32 bit/ 16bit deep pixel */
1458 if (lines && width && (bmpImage->bits_per_pixel == 16) &&
1459 (ImageByteOrder(gdi_display)==LSBFirst) )
1461 dstbits=(BYTE*)bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1462 /* FIXME: Does this really handle all these cases correctly? */
1463 /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1464 for (h = lines ; h--; ) {
1465 int _cl1,_cl2; /* temp outputs for asm below */
1466 /* Borrowed from DirectDraw */
1467 __asm__ __volatile__(
1472 " movw (%%edx,%%eax,4),%%ax\n"
1474 " xor %%eax,%%eax\n"
1476 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1481 :"eax", "cc", "memory"
1483 srcbyte = (srcbits += linebytes);
1484 dstbits -= bmpImage->bytes_per_line;
1492 #if defined(__i386__) && defined(__GNUC__)
1493 if (lines && width && (bmpImage->bits_per_pixel == 32) &&
1494 (ImageByteOrder(gdi_display)==LSBFirst) )
1496 dstbits=(BYTE*)bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
1497 /* FIXME: Does this really handle both cases correctly? */
1498 /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1499 for (h = lines ; h--; ) {
1500 int _cl1,_cl2; /* temp outputs for asm below */
1501 /* Borrowed from DirectDraw */
1502 __asm__ __volatile__(
1507 " movl (%%edx,%%eax,4),%%eax\n"
1509 " xor %%eax,%%eax\n"
1511 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1516 :"eax", "cc", "memory"
1518 srcbyte = (srcbits += linebytes);
1519 dstbits -= bmpImage->bytes_per_line;
1526 break; /* use slow generic case below */
1529 /* ==== pal 8 dib -> any bmp format ==== */
1530 for (h=lines-1; h>=0; h--) {
1531 for (x=left; x<width+left; x++) {
1532 XPutPixel(bmpImage, x, h, colors[*srcbyte++]);
1534 srcbyte = (srcbits += linebytes);
1538 /***********************************************************************
1539 * X11DRV_DIB_GetImageBits_8
1541 * GetDIBits for an 8-bit deep DIB.
1543 static void X11DRV_DIB_GetImageBits_8( int lines, BYTE *dstbits,
1544 DWORD srcwidth, DWORD dstwidth,
1545 RGBQUAD *colors, PALETTEENTRY *srccolors,
1546 XImage *bmpImage, DWORD linebytes )
1549 int h, width = min(srcwidth, dstwidth);
1555 dstbits = dstbits + ( linebytes * (lines-1) );
1556 linebytes = -linebytes;
1561 * This condition is true when GetImageBits has been called by
1562 * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1563 * 256 colormaps, so we'll just use for for GetDIBits calls.
1564 * (In somes cases, in a updateDIBSection, the returned colors are bad too)
1566 if (!srccolors) goto updatesection;
1568 switch (bmpImage->depth) {
1571 if (bmpImage->red_mask==0 && bmpImage->green_mask==0 && bmpImage->blue_mask==0 && srccolors) {
1573 /* ==== pal 1 bmp -> pal 8 dib ==== */
1574 /* ==== pal 4 bmp -> pal 8 dib ==== */
1575 for (h=lines-1; h>=0; h--) {
1577 for (x=0; x<width; x++) {
1578 PALETTEENTRY srcval;
1579 srcval=srccolors[XGetPixel(bmpImage, x, h)];
1580 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1585 dstbits += linebytes;
1593 if (bmpImage->red_mask==0 && bmpImage->green_mask==0 && bmpImage->blue_mask==0 && srccolors) {
1594 /* ==== pal 8 bmp -> pal 8 dib ==== */
1595 const void* srcbits;
1596 const BYTE* srcpixel;
1598 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1599 for (h=0; h<lines; h++) {
1602 for (x = 0; x < width; x++) {
1603 PALETTEENTRY srcval;
1604 srcval=srccolors[(int)*srcpixel++];
1605 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1610 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1611 dstbits += linebytes;
1621 const void* srcbits;
1622 const WORD* srcpixel;
1625 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1627 if (bmpImage->green_mask==0x03e0) {
1628 if (bmpImage->red_mask==0x7c00) {
1629 /* ==== rgb 555 bmp -> pal 8 dib ==== */
1630 for (h=0; h<lines; h++) {
1633 for (x=0; x<width; x++) {
1636 *dstbyte++=X11DRV_DIB_GetNearestIndex
1638 ((srcval >> 7) & 0xf8) | /* r */
1639 ((srcval >> 12) & 0x07),
1640 ((srcval >> 2) & 0xf8) | /* g */
1641 ((srcval >> 7) & 0x07),
1642 ((srcval << 3) & 0xf8) | /* b */
1643 ((srcval >> 2) & 0x07) );
1645 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1646 dstbits += linebytes;
1648 } else if (bmpImage->blue_mask==0x7c00) {
1649 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1650 for (h=0; h<lines; h++) {
1653 for (x=0; x<width; x++) {
1656 *dstbyte++=X11DRV_DIB_GetNearestIndex
1658 ((srcval << 3) & 0xf8) | /* r */
1659 ((srcval >> 2) & 0x07),
1660 ((srcval >> 2) & 0xf8) | /* g */
1661 ((srcval >> 7) & 0x07),
1662 ((srcval >> 7) & 0xf8) | /* b */
1663 ((srcval >> 12) & 0x07) );
1665 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1666 dstbits += linebytes;
1671 } else if (bmpImage->green_mask==0x07e0) {
1672 if (bmpImage->red_mask==0xf800) {
1673 /* ==== rgb 565 bmp -> pal 8 dib ==== */
1674 for (h=0; h<lines; h++) {
1677 for (x=0; x<width; x++) {
1680 *dstbyte++=X11DRV_DIB_GetNearestIndex
1682 ((srcval >> 8) & 0xf8) | /* r */
1683 ((srcval >> 13) & 0x07),
1684 ((srcval >> 3) & 0xfc) | /* g */
1685 ((srcval >> 9) & 0x03),
1686 ((srcval << 3) & 0xf8) | /* b */
1687 ((srcval >> 2) & 0x07) );
1689 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1690 dstbits += linebytes;
1692 } else if (bmpImage->blue_mask==0xf800) {
1693 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1694 for (h=0; h<lines; h++) {
1697 for (x=0; x<width; x++) {
1700 *dstbyte++=X11DRV_DIB_GetNearestIndex
1702 ((srcval << 3) & 0xf8) | /* r */
1703 ((srcval >> 2) & 0x07),
1704 ((srcval >> 3) & 0xfc) | /* g */
1705 ((srcval >> 9) & 0x03),
1706 ((srcval >> 8) & 0xf8) | /* b */
1707 ((srcval >> 13) & 0x07) );
1709 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1710 dstbits += linebytes;
1724 const void* srcbits;
1725 const BYTE *srcbyte;
1727 int bytes_per_pixel;
1729 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1730 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
1732 if (bmpImage->green_mask!=0x00ff00 ||
1733 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1735 } else if (bmpImage->blue_mask==0xff) {
1736 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1737 for (h=0; h<lines; h++) {
1740 for (x=0; x<width; x++) {
1741 *dstbyte++=X11DRV_DIB_GetNearestIndex
1746 srcbyte+=bytes_per_pixel;
1748 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1749 dstbits += linebytes;
1752 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1753 for (h=0; h<lines; h++) {
1756 for (x=0; x<width; x++) {
1757 *dstbyte++=X11DRV_DIB_GetNearestIndex
1762 srcbyte+=bytes_per_pixel;
1764 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1765 dstbits += linebytes;
1773 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1774 bmpImage->depth, bmpImage->red_mask,
1775 bmpImage->green_mask, bmpImage->blue_mask );
1777 /* ==== any bmp format -> pal 8 dib ==== */
1778 for (h=lines-1; h>=0; h--) {
1780 for (x=0; x<width; x++) {
1781 *dstbyte=X11DRV_DIB_MapColor
1783 XGetPixel(bmpImage, x, h), *dstbyte);
1786 dstbits += linebytes;
1792 /***********************************************************************
1793 * X11DRV_DIB_SetImageBits_RLE8
1795 * SetDIBits for an 8-bit deep compressed DIB.
1797 * This function rewritten 941113 by James Youngman. WINE blew out when I
1798 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
1800 * This was because the algorithm assumed that all RLE8 bitmaps end with the
1801 * 'End of bitmap' escape code. This code is very much laxer in what it
1802 * allows to end the expansion. Possibly too lax. See the note by
1803 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
1804 * bitmap should end with RleEnd, but on the other hand, software exists
1805 * that produces ones that don't and Windows 3.1 doesn't complain a bit
1808 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
1809 * James A. Youngman <mbcstjy@afs.man.ac.uk>
1812 static void X11DRV_DIB_SetImageBits_RLE8( int lines, const BYTE *bits,
1813 DWORD srcwidth, DWORD dstwidth,
1814 int left, int *colors,
1817 unsigned int x; /* X-position on each line. Increases. */
1818 int y; /* Line #. Starts at lines-1, decreases */
1819 const BYTE *pIn = bits; /* Pointer to current position in bits */
1820 BYTE length; /* The length pf a run */
1821 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
1824 * Note that the bitmap data is stored by Windows starting at the
1825 * bottom line of the bitmap and going upwards. Within each line,
1826 * the data is stored left-to-right. That's the reason why line
1827 * goes from lines-1 to 0. [JAY]
1837 * If the length byte is not zero (which is the escape value),
1838 * We have a run of length pixels all the same colour. The colour
1839 * index is stored next.
1841 * If the length byte is zero, we need to read the next byte to
1842 * know what to do. [JAY]
1847 * [Run-Length] Encoded mode
1849 int color = colors[*pIn++];
1850 while (length-- && x < (left + dstwidth)) {
1851 if( x >= left) XPutPixel(bmpImage, x, y, color);
1858 * Escape codes (may be an absolute sequence though)
1860 escape_code = (*pIn++);
1869 /* Not all RLE8 bitmaps end with this code. For
1870 * example, Paint Shop Pro produces some that don't.
1871 * That's (I think) what caused the previous
1872 * implementation to fail. [JAY]
1881 default: /* switch to absolute mode */
1882 length = escape_code;
1885 int color = colors[*pIn++];
1886 if (x >= (left + dstwidth))
1891 if( x >= left) XPutPixel(bmpImage, x, y, color);
1895 * If you think for a moment you'll realise that the
1896 * only time we could ever possibly read an odd
1897 * number of bytes is when there is a 0x00 (escape),
1898 * a value >0x02 (absolute mode) and then an odd-
1899 * length run. Therefore this is the only place we
1900 * need to worry about it. Everywhere else the
1901 * bytes are always read in pairs. [JAY]
1903 if (escape_code & 1) pIn++; /* Throw away the pad byte. */
1905 } /* switch (escape_code) : Escape sequence */
1911 /***********************************************************************
1912 * X11DRV_DIB_SetImageBits_16
1914 * SetDIBits for a 16-bit deep DIB.
1916 static void X11DRV_DIB_SetImageBits_16( int lines, const BYTE *srcbits,
1917 DWORD srcwidth, DWORD dstwidth, int left,
1918 X11DRV_PDEVICE *physDev, DWORD rSrc, DWORD gSrc, DWORD bSrc,
1919 XImage *bmpImage, DWORD linebytes )
1922 int h, width = min(srcwidth, dstwidth);
1923 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
1928 srcbits = srcbits + ( linebytes * (lines-1));
1929 linebytes = -linebytes;
1932 switch (bmpImage->depth)
1939 srcbits=srcbits+left*2;
1940 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1942 if (bmpImage->green_mask==0x03e0) {
1943 if (gSrc==bmpImage->green_mask) {
1944 if (rSrc==bmpImage->red_mask) {
1945 /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1946 /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1947 convs->Convert_5x5_asis
1950 dstbits,-bmpImage->bytes_per_line);
1951 } else if (rSrc==bmpImage->blue_mask) {
1952 /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1953 /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1954 convs->Convert_555_reverse
1957 dstbits,-bmpImage->bytes_per_line);
1960 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1961 /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1962 /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1963 convs->Convert_565_to_555_asis
1966 dstbits,-bmpImage->bytes_per_line);
1968 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1969 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1970 convs->Convert_565_to_555_reverse
1973 dstbits,-bmpImage->bytes_per_line);
1976 } else if (bmpImage->green_mask==0x07e0) {
1977 if (gSrc==bmpImage->green_mask) {
1978 if (rSrc==bmpImage->red_mask) {
1979 /* ==== rgb 565 dib -> rgb 565 bmp ==== */
1980 /* ==== bgr 565 dib -> bgr 565 bmp ==== */
1981 convs->Convert_5x5_asis
1984 dstbits,-bmpImage->bytes_per_line);
1986 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
1987 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
1988 convs->Convert_565_reverse
1991 dstbits,-bmpImage->bytes_per_line);
1994 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1995 /* ==== rgb 555 dib -> rgb 565 bmp ==== */
1996 /* ==== bgr 555 dib -> bgr 565 bmp ==== */
1997 convs->Convert_555_to_565_asis
2000 dstbits,-bmpImage->bytes_per_line);
2002 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
2003 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
2004 convs->Convert_555_to_565_reverse
2007 dstbits,-bmpImage->bytes_per_line);
2017 if (bmpImage->bits_per_pixel==24) {
2020 srcbits=srcbits+left*2;
2021 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2023 if (bmpImage->green_mask!=0x00ff00 ||
2024 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2026 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2027 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2029 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
2030 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
2031 convs->Convert_555_to_888_asis
2034 dstbits,-bmpImage->bytes_per_line);
2036 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
2037 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
2038 convs->Convert_565_to_888_asis
2041 dstbits,-bmpImage->bytes_per_line);
2045 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
2046 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
2047 convs->Convert_555_to_888_reverse
2050 dstbits,-bmpImage->bytes_per_line);
2052 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
2053 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
2054 convs->Convert_565_to_888_reverse
2057 dstbits,-bmpImage->bytes_per_line);
2068 srcbits=srcbits+left*2;
2069 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2071 if (bmpImage->green_mask!=0x00ff00 ||
2072 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2074 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2075 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2077 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
2078 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
2079 convs->Convert_555_to_0888_asis
2082 dstbits,-bmpImage->bytes_per_line);
2084 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
2085 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
2086 convs->Convert_565_to_0888_asis
2089 dstbits,-bmpImage->bytes_per_line);
2093 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
2094 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
2095 convs->Convert_555_to_0888_reverse
2098 dstbits,-bmpImage->bytes_per_line);
2100 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
2101 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
2102 convs->Convert_565_to_0888_reverse
2105 dstbits,-bmpImage->bytes_per_line);
2113 WARN("from 16 bit DIB (%lx,%lx,%lx) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2114 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2115 bmpImage->green_mask, bmpImage->blue_mask );
2121 /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
2122 const WORD* srcpixel;
2123 int rShift1,gShift1,bShift1;
2124 int rShift2,gShift2,bShift2;
2127 /* Set color scaling values */
2128 rShift1=16+X11DRV_DIB_MaskToShift(rSrc)-3;
2129 gShift1=16+X11DRV_DIB_MaskToShift(gSrc)-3;
2130 bShift1=16+X11DRV_DIB_MaskToShift(bSrc)-3;
2135 /* Green has 5 bits, like the others */
2139 /* Green has 6 bits, not 5. Compensate. */
2148 /* We could split it into four separate cases to optimize
2149 * but it is probably not worth it.
2151 for (h=lines-1; h>=0; h--) {
2152 srcpixel=(const WORD*)srcbits;
2153 for (x=left; x<width+left; x++) {
2155 BYTE red,green,blue;
2156 srcval=*srcpixel++ << 16;
2157 red= ((srcval >> rShift1) & 0xf8) |
2158 ((srcval >> rShift2) & 0x07);
2159 green=((srcval >> gShift1) & gMask1) |
2160 ((srcval >> gShift2) & gMask2);
2161 blue= ((srcval >> bShift1) & 0xf8) |
2162 ((srcval >> bShift2) & 0x07);
2163 XPutPixel(bmpImage, x, h,
2164 X11DRV_PALETTE_ToPhysical
2165 (physDev, RGB(red,green,blue)));
2167 srcbits += linebytes;
2175 /***********************************************************************
2176 * X11DRV_DIB_GetImageBits_16
2178 * GetDIBits for an 16-bit deep DIB.
2180 static void X11DRV_DIB_GetImageBits_16( int lines, BYTE *dstbits,
2181 DWORD dstwidth, DWORD srcwidth,
2182 PALETTEENTRY *srccolors,
2183 DWORD rDst, DWORD gDst, DWORD bDst,
2184 XImage *bmpImage, DWORD dibpitch )
2187 int h, width = min(srcwidth, dstwidth);
2188 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2190 DWORD linebytes = dibpitch;
2195 dstbits = dstbits + ( linebytes * (lines-1));
2196 linebytes = -linebytes;
2199 switch (bmpImage->depth)
2204 const char* srcbits;
2206 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2208 if (bmpImage->green_mask==0x03e0) {
2209 if (gDst==bmpImage->green_mask) {
2210 if (rDst==bmpImage->red_mask) {
2211 /* ==== rgb 555 bmp -> rgb 555 dib ==== */
2212 /* ==== bgr 555 bmp -> bgr 555 dib ==== */
2213 convs->Convert_5x5_asis
2215 srcbits,-bmpImage->bytes_per_line,
2218 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
2219 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
2220 convs->Convert_555_reverse
2222 srcbits,-bmpImage->bytes_per_line,
2226 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2227 /* ==== rgb 555 bmp -> rgb 565 dib ==== */
2228 /* ==== bgr 555 bmp -> bgr 565 dib ==== */
2229 convs->Convert_555_to_565_asis
2231 srcbits,-bmpImage->bytes_per_line,
2234 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
2235 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
2236 convs->Convert_555_to_565_reverse
2238 srcbits,-bmpImage->bytes_per_line,
2242 } else if (bmpImage->green_mask==0x07e0) {
2243 if (gDst==bmpImage->green_mask) {
2244 if (rDst == bmpImage->red_mask) {
2245 /* ==== rgb 565 bmp -> rgb 565 dib ==== */
2246 /* ==== bgr 565 bmp -> bgr 565 dib ==== */
2247 convs->Convert_5x5_asis
2249 srcbits,-bmpImage->bytes_per_line,
2252 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
2253 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
2254 convs->Convert_565_reverse
2256 srcbits,-bmpImage->bytes_per_line,
2260 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2261 /* ==== rgb 565 bmp -> rgb 555 dib ==== */
2262 /* ==== bgr 565 bmp -> bgr 555 dib ==== */
2263 convs->Convert_565_to_555_asis
2265 srcbits,-bmpImage->bytes_per_line,
2268 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
2269 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
2270 convs->Convert_565_to_555_reverse
2272 srcbits,-bmpImage->bytes_per_line,
2283 if (bmpImage->bits_per_pixel == 24) {
2284 const char* srcbits;
2286 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2288 if (bmpImage->green_mask!=0x00ff00 ||
2289 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2291 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2292 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2294 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
2295 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
2296 convs->Convert_888_to_555_asis
2298 srcbits,-bmpImage->bytes_per_line,
2301 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2302 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2303 convs->Convert_888_to_565_asis
2305 srcbits,-bmpImage->bytes_per_line,
2310 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
2311 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
2312 convs->Convert_888_to_555_reverse
2314 srcbits,-bmpImage->bytes_per_line,
2317 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
2318 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
2319 convs->Convert_888_to_565_reverse
2321 srcbits,-bmpImage->bytes_per_line,
2331 const char* srcbits;
2333 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2335 if (bmpImage->green_mask!=0x00ff00 ||
2336 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2338 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2339 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2341 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2342 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2343 convs->Convert_0888_to_555_asis
2345 srcbits,-bmpImage->bytes_per_line,
2348 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2349 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2350 convs->Convert_0888_to_565_asis
2352 srcbits,-bmpImage->bytes_per_line,
2357 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2358 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2359 convs->Convert_0888_to_555_reverse
2361 srcbits,-bmpImage->bytes_per_line,
2364 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2365 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2366 convs->Convert_0888_to_565_reverse
2368 srcbits,-bmpImage->bytes_per_line,
2377 if (bmpImage->red_mask==0 && bmpImage->green_mask==0 && bmpImage->blue_mask==0 && srccolors) {
2378 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2379 int rShift,gShift,bShift;
2382 /* Shift everything 16 bits left so that all shifts are >0,
2383 * even for BGR DIBs. Then a single >> 16 will bring everything
2386 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2387 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2388 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2390 /* 6 bits for the green */
2396 for (h = lines - 1; h >= 0; h--) {
2397 dstpixel=(LPWORD)dstbits;
2398 for (x = 0; x < width; x++) {
2399 PALETTEENTRY srcval;
2401 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2402 dstval=((srcval.peRed << rShift) & rDst) |
2403 ((srcval.peGreen << gShift) & gDst) |
2404 ((srcval.peBlue << bShift) & bDst);
2405 *dstpixel++=dstval >> 16;
2407 dstbits += linebytes;
2415 if (bmpImage->red_mask==0 && bmpImage->green_mask==0 && bmpImage->blue_mask==0 && srccolors) {
2416 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2417 int rShift,gShift,bShift;
2418 const BYTE* srcbits;
2419 const BYTE* srcpixel;
2422 /* Shift everything 16 bits left so that all shifts are >0,
2423 * even for BGR DIBs. Then a single >> 16 will bring everything
2426 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2427 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2428 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2430 /* 6 bits for the green */
2436 srcbits=(BYTE*)bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2437 for (h=0; h<lines; h++) {
2439 dstpixel=(LPWORD)dstbits;
2440 for (x = 0; x < width; x++) {
2441 PALETTEENTRY srcval;
2443 srcval=srccolors[(int)*srcpixel++];
2444 dstval=((srcval.peRed << rShift) & rDst) |
2445 ((srcval.peGreen << gShift) & gDst) |
2446 ((srcval.peBlue << bShift) & bDst);
2447 *dstpixel++=dstval >> 16;
2449 srcbits -= bmpImage->bytes_per_line;
2450 dstbits += linebytes;
2460 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2461 int rShift,gShift,bShift;
2464 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%lx,%lx,%lx)\n",
2465 bmpImage->depth, bmpImage->red_mask,
2466 bmpImage->green_mask, bmpImage->blue_mask,
2469 /* Shift everything 16 bits left so that all shifts are >0,
2470 * even for BGR DIBs. Then a single >> 16 will bring everything
2473 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2474 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2475 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2477 /* 6 bits for the green */
2483 for (h = lines - 1; h >= 0; h--) {
2484 dstpixel=(LPWORD)dstbits;
2485 for (x = 0; x < width; x++) {
2488 srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
2489 dstval=((GetRValue(srcval) << rShift) & rDst) |
2490 ((GetGValue(srcval) << gShift) & gDst) |
2491 ((GetBValue(srcval) << bShift) & bDst);
2492 *dstpixel++=dstval >> 16;
2494 dstbits += linebytes;
2502 /***********************************************************************
2503 * X11DRV_DIB_SetImageBits_24
2505 * SetDIBits for a 24-bit deep DIB.
2507 static void X11DRV_DIB_SetImageBits_24( int lines, const BYTE *srcbits,
2508 DWORD srcwidth, DWORD dstwidth, int left,
2509 X11DRV_PDEVICE *physDev,
2510 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2511 XImage *bmpImage, DWORD linebytes )
2514 int h, width = min(srcwidth, dstwidth);
2515 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2520 srcbits = srcbits + linebytes * (lines - 1);
2521 linebytes = -linebytes;
2524 switch (bmpImage->depth)
2527 if (bmpImage->bits_per_pixel==24) {
2530 srcbits=srcbits+left*3;
2531 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2533 if (bmpImage->green_mask!=0x00ff00 ||
2534 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2536 } else if (rSrc==bmpImage->red_mask) {
2537 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2538 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2539 convs->Convert_888_asis
2542 dstbits,-bmpImage->bytes_per_line);
2544 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2545 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2546 convs->Convert_888_reverse
2549 dstbits,-bmpImage->bytes_per_line);
2559 srcbits=srcbits+left*3;
2560 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2562 if (bmpImage->green_mask!=0x00ff00 ||
2563 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2565 } else if (rSrc==bmpImage->red_mask) {
2566 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2567 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2568 convs->Convert_888_to_0888_asis
2571 dstbits,-bmpImage->bytes_per_line);
2573 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2574 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2575 convs->Convert_888_to_0888_reverse
2578 dstbits,-bmpImage->bytes_per_line);
2588 srcbits=srcbits+left*3;
2589 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
2591 if (bmpImage->green_mask==0x03e0) {
2592 if ((rSrc==0xff0000 && bmpImage->red_mask==0x7f00) ||
2593 (bSrc==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2594 /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2595 /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2596 convs->Convert_888_to_555_asis
2599 dstbits,-bmpImage->bytes_per_line);
2600 } else if ((rSrc==0xff && bmpImage->red_mask==0x7f00) ||
2601 (bSrc==0xff && bmpImage->blue_mask==0x7f00)) {
2602 /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2603 /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2604 convs->Convert_888_to_555_reverse
2607 dstbits,-bmpImage->bytes_per_line);
2611 } else if (bmpImage->green_mask==0x07e0) {
2612 if ((rSrc==0xff0000 && bmpImage->red_mask==0xf800) ||
2613 (bSrc==0xff0000 && bmpImage->blue_mask==0xf800)) {
2614 /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2615 /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2616 convs->Convert_888_to_565_asis
2619 dstbits,-bmpImage->bytes_per_line);
2620 } else if ((rSrc==0xff && bmpImage->red_mask==0xf800) ||
2621 (bSrc==0xff && bmpImage->blue_mask==0xf800)) {
2622 /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2623 /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2624 convs->Convert_888_to_565_reverse
2627 dstbits,-bmpImage->bytes_per_line);
2639 WARN("from 24 bit DIB (%lx,%lx,%lx) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2640 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2641 bmpImage->green_mask, bmpImage->blue_mask );
2647 /* ==== rgb 888 dib -> any bmp bormat ==== */
2648 const BYTE* srcbyte;
2650 /* Windows only supports one 24bpp DIB format: RGB888 */
2652 for (h = lines - 1; h >= 0; h--) {
2653 srcbyte=(const BYTE*)srcbits;
2654 for (x = left; x < width+left; x++) {
2655 XPutPixel(bmpImage, x, h,
2656 X11DRV_PALETTE_ToPhysical
2657 (physDev, RGB(srcbyte[2], srcbyte[1], srcbyte[0])));
2660 srcbits += linebytes;
2668 /***********************************************************************
2669 * X11DRV_DIB_GetImageBits_24
2671 * GetDIBits for an 24-bit deep DIB.
2673 static void X11DRV_DIB_GetImageBits_24( int lines, BYTE *dstbits,
2674 DWORD dstwidth, DWORD srcwidth,
2675 PALETTEENTRY *srccolors,
2676 DWORD rDst, DWORD gDst, DWORD bDst,
2677 XImage *bmpImage, DWORD linebytes )
2680 int h, width = min(srcwidth, dstwidth);
2681 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2686 dstbits = dstbits + ( linebytes * (lines-1) );
2687 linebytes = -linebytes;
2690 switch (bmpImage->depth)
2693 if (bmpImage->bits_per_pixel==24) {
2694 const char* srcbits;
2696 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2698 if (bmpImage->green_mask!=0x00ff00 ||
2699 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2701 } else if (rDst==bmpImage->red_mask) {
2702 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2703 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2704 convs->Convert_888_asis
2706 srcbits,-bmpImage->bytes_per_line,
2709 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2710 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2711 convs->Convert_888_reverse
2713 srcbits,-bmpImage->bytes_per_line,
2722 const char* srcbits;
2724 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2726 if (bmpImage->green_mask!=0x00ff00 ||
2727 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2729 } else if (rDst==bmpImage->red_mask) {
2730 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2731 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2732 convs->Convert_0888_to_888_asis
2734 srcbits,-bmpImage->bytes_per_line,
2737 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2738 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2739 convs->Convert_0888_to_888_reverse
2741 srcbits,-bmpImage->bytes_per_line,
2750 const char* srcbits;
2752 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2754 if (bmpImage->green_mask==0x03e0) {
2755 if ((rDst==0xff0000 && bmpImage->red_mask==0x7f00) ||
2756 (bDst==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2757 /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2758 /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2759 convs->Convert_555_to_888_asis
2761 srcbits,-bmpImage->bytes_per_line,
2763 } else if ((rDst==0xff && bmpImage->red_mask==0x7f00) ||
2764 (bDst==0xff && bmpImage->blue_mask==0x7f00)) {
2765 /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2766 /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2767 convs->Convert_555_to_888_reverse
2769 srcbits,-bmpImage->bytes_per_line,
2774 } else if (bmpImage->green_mask==0x07e0) {
2775 if ((rDst==0xff0000 && bmpImage->red_mask==0xf800) ||
2776 (bDst==0xff0000 && bmpImage->blue_mask==0xf800)) {
2777 /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2778 /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2779 convs->Convert_565_to_888_asis
2781 srcbits,-bmpImage->bytes_per_line,
2783 } else if ((rDst==0xff && bmpImage->red_mask==0xf800) ||
2784 (bDst==0xff && bmpImage->blue_mask==0xf800)) {
2785 /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2786 /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2787 convs->Convert_565_to_888_reverse
2789 srcbits,-bmpImage->bytes_per_line,
2802 if (bmpImage->red_mask==0 && bmpImage->green_mask==0 && bmpImage->blue_mask==0 && srccolors) {
2803 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2806 /* Windows only supports one 24bpp DIB format: rgb 888 */
2807 for (h = lines - 1; h >= 0; h--) {
2809 for (x = 0; x < width; x++) {
2810 PALETTEENTRY srcval;
2811 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2812 dstbyte[0]=srcval.peBlue;
2813 dstbyte[1]=srcval.peGreen;
2814 dstbyte[2]=srcval.peRed;
2817 dstbits += linebytes;
2825 if (bmpImage->red_mask==0 && bmpImage->green_mask==0 && bmpImage->blue_mask == 0 && srccolors) {
2826 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2827 const void* srcbits;
2828 const BYTE* srcpixel;
2831 /* Windows only supports one 24bpp DIB format: rgb 888 */
2832 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2833 for (h = lines - 1; h >= 0; h--) {
2836 for (x = 0; x < width; x++ ) {
2837 PALETTEENTRY srcval;
2838 srcval=srccolors[(int)*srcpixel++];
2839 dstbyte[0]=srcval.peBlue;
2840 dstbyte[1]=srcval.peGreen;
2841 dstbyte[2]=srcval.peRed;
2844 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
2845 dstbits += linebytes;
2855 /* ==== any bmp format -> 888 dib ==== */
2858 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%lx,%lx,%lx)\n",
2859 bmpImage->depth, bmpImage->red_mask,
2860 bmpImage->green_mask, bmpImage->blue_mask,
2863 /* Windows only supports one 24bpp DIB format: rgb 888 */
2864 for (h = lines - 1; h >= 0; h--) {
2866 for (x = 0; x < width; x++) {
2867 COLORREF srcval=X11DRV_PALETTE_ToLogical
2868 (XGetPixel( bmpImage, x, h ));
2869 dstbyte[0]=GetBValue(srcval);
2870 dstbyte[1]=GetGValue(srcval);
2871 dstbyte[2]=GetRValue(srcval);
2874 dstbits += linebytes;
2882 /***********************************************************************
2883 * X11DRV_DIB_SetImageBits_32
2885 * SetDIBits for a 32-bit deep DIB.
2887 static void X11DRV_DIB_SetImageBits_32(int lines, const BYTE *srcbits,
2888 DWORD srcwidth, DWORD dstwidth, int left,
2889 X11DRV_PDEVICE *physDev,
2890 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2896 int h, width = min(srcwidth, dstwidth);
2897 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2902 srcbits = srcbits + ( linebytes * (lines-1) );
2903 linebytes = -linebytes;
2906 ptr = (const DWORD *) srcbits + left;
2908 switch (bmpImage->depth)
2911 if (bmpImage->bits_per_pixel==24) {
2914 srcbits=srcbits+left*4;
2915 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2917 if (rSrc==bmpImage->red_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->blue_mask) {
2918 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2919 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2920 convs->Convert_0888_to_888_asis
2923 dstbits,-bmpImage->bytes_per_line);
2924 } else if (bmpImage->green_mask!=0x00ff00 ||
2925 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2927 /* the tests below assume sane bmpImage masks */
2928 } else if (rSrc==bmpImage->blue_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->red_mask) {
2929 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2930 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2931 convs->Convert_0888_to_888_reverse
2934 dstbits,-bmpImage->bytes_per_line);
2935 } else if (bmpImage->blue_mask==0xff) {
2936 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2937 convs->Convert_any0888_to_rgb888
2941 dstbits,-bmpImage->bytes_per_line);
2943 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2944 convs->Convert_any0888_to_bgr888
2948 dstbits,-bmpImage->bytes_per_line);
2958 srcbits=srcbits+left*4;
2959 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2961 if (gSrc==bmpImage->green_mask) {
2962 if (rSrc==bmpImage->red_mask && bSrc==bmpImage->blue_mask) {
2963 /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2964 /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2965 convs->Convert_0888_asis
2968 dstbits,-bmpImage->bytes_per_line);
2969 } else if (bmpImage->green_mask!=0x00ff00 ||
2970 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2972 /* the tests below assume sane bmpImage masks */
2973 } else if (rSrc==bmpImage->blue_mask && bSrc==bmpImage->red_mask) {
2974 /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2975 /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2976 convs->Convert_0888_reverse
2979 dstbits,-bmpImage->bytes_per_line);
2981 /* ==== any 0888 dib -> any 0888 bmp ==== */
2982 convs->Convert_0888_any
2986 dstbits,-bmpImage->bytes_per_line,
2987 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
2989 } else if (bmpImage->green_mask!=0x00ff00 ||
2990 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2992 /* the tests below assume sane bmpImage masks */
2994 /* ==== any 0888 dib -> any 0888 bmp ==== */
2995 convs->Convert_0888_any
2999 dstbits,-bmpImage->bytes_per_line,
3000 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3010 srcbits=srcbits+left*4;
3011 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
3013 if (rSrc==0xff0000 && gSrc==0x00ff00 && bSrc==0x0000ff) {
3014 if (bmpImage->green_mask==0x03e0) {
3015 if (bmpImage->red_mask==0x7f00) {
3016 /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
3017 convs->Convert_0888_to_555_asis
3020 dstbits,-bmpImage->bytes_per_line);
3021 } else if (bmpImage->blue_mask==0x7f00) {
3022 /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
3023 convs->Convert_0888_to_555_reverse
3026 dstbits,-bmpImage->bytes_per_line);
3030 } else if (bmpImage->green_mask==0x07e0) {
3031 if (bmpImage->red_mask==0xf800) {
3032 /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
3033 convs->Convert_0888_to_565_asis
3036 dstbits,-bmpImage->bytes_per_line);
3037 } else if (bmpImage->blue_mask==0xf800) {
3038 /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
3039 convs->Convert_0888_to_565_reverse
3042 dstbits,-bmpImage->bytes_per_line);
3049 } else if (rSrc==0x0000ff && gSrc==0x00ff00 && bSrc==0xff0000) {
3050 if (bmpImage->green_mask==0x03e0) {
3051 if (bmpImage->blue_mask==0x7f00) {
3052 /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
3053 convs->Convert_0888_to_555_asis
3056 dstbits,-bmpImage->bytes_per_line);
3057 } else if (bmpImage->red_mask==0x7f00) {
3058 /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
3059 convs->Convert_0888_to_555_reverse
3062 dstbits,-bmpImage->bytes_per_line);
3066 } else if (bmpImage->green_mask==0x07e0) {
3067 if (bmpImage->blue_mask==0xf800) {
3068 /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
3069 convs->Convert_0888_to_565_asis
3072 dstbits,-bmpImage->bytes_per_line);
3073 } else if (bmpImage->red_mask==0xf800) {
3074 /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
3075 convs->Convert_0888_to_565_reverse
3078 dstbits,-bmpImage->bytes_per_line);
3086 if (bmpImage->green_mask==0x03e0 &&
3087 (bmpImage->red_mask==0x7f00 ||
3088 bmpImage->blue_mask==0x7f00)) {
3089 /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
3090 convs->Convert_any0888_to_5x5
3094 dstbits,-bmpImage->bytes_per_line,
3095 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3096 } else if (bmpImage->green_mask==0x07e0 &&
3097 (bmpImage->red_mask==0xf800 ||
3098 bmpImage->blue_mask==0xf800)) {
3099 /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
3100 convs->Convert_any0888_to_5x5
3104 dstbits,-bmpImage->bytes_per_line,
3105 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3115 WARN("from 32 bit DIB (%lx,%lx,%lx) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
3116 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
3117 bmpImage->green_mask, bmpImage->blue_mask );
3123 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
3124 const DWORD* srcpixel;
3125 int rShift,gShift,bShift;
3127 rShift=X11DRV_DIB_MaskToShift(rSrc);
3128 gShift=X11DRV_DIB_MaskToShift(gSrc);
3129 bShift=X11DRV_DIB_MaskToShift(bSrc);
3131 for (h = lines - 1; h >= 0; h--) {
3132 srcpixel=(const DWORD*)srcbits;
3133 for (x = left; x < width+left; x++) {
3135 BYTE red,green,blue;
3136 srcvalue=*srcpixel++;
3137 red= (srcvalue >> rShift) & 0xff;
3138 green=(srcvalue >> gShift) & 0xff;
3139 blue= (srcvalue >> bShift) & 0xff;
3140 XPutPixel(bmpImage, x, h, X11DRV_PALETTE_ToPhysical
3141 (physDev, RGB(red,green,blue)));
3143 srcbits += linebytes;
3151 /***********************************************************************
3152 * X11DRV_DIB_GetImageBits_32
3154 * GetDIBits for an 32-bit deep DIB.
3156 static void X11DRV_DIB_GetImageBits_32( int lines, BYTE *dstbits,
3157 DWORD dstwidth, DWORD srcwidth,
3158 PALETTEENTRY *srccolors,
3159 DWORD rDst, DWORD gDst, DWORD bDst,
3160 XImage *bmpImage, DWORD linebytes )
3163 int h, width = min(srcwidth, dstwidth);
3165 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
3170 dstbits = dstbits + ( linebytes * (lines-1) );
3171 linebytes = -linebytes;
3176 switch (bmpImage->depth)
3179 if (bmpImage->bits_per_pixel==24) {
3180 const void* srcbits;
3182 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3184 if (rDst==bmpImage->red_mask && gDst==bmpImage->green_mask && bDst==bmpImage->blue_mask) {
3185 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
3186 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
3187 convs->Convert_888_to_0888_asis
3189 srcbits,-bmpImage->bytes_per_line,
3191 } else if (bmpImage->green_mask!=0x00ff00 ||
3192 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3194 /* the tests below assume sane bmpImage masks */
3195 } else if (rDst==bmpImage->blue_mask && gDst==bmpImage->green_mask && bDst==bmpImage->red_mask) {
3196 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
3197 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
3198 convs->Convert_888_to_0888_reverse
3200 srcbits,-bmpImage->bytes_per_line,
3202 } else if (bmpImage->blue_mask==0xff) {
3203 /* ==== rgb 888 bmp -> any 0888 dib ==== */
3204 convs->Convert_rgb888_to_any0888
3206 srcbits,-bmpImage->bytes_per_line,
3210 /* ==== bgr 888 bmp -> any 0888 dib ==== */
3211 convs->Convert_bgr888_to_any0888
3213 srcbits,-bmpImage->bytes_per_line,
3223 const char* srcbits;
3225 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3227 if (gDst==bmpImage->green_mask) {
3228 if (rDst==bmpImage->red_mask && bDst==bmpImage->blue_mask) {
3229 /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
3230 /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
3231 convs->Convert_0888_asis
3233 srcbits,-bmpImage->bytes_per_line,
3235 } else if (bmpImage->green_mask!=0x00ff00 ||
3236 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3238 /* the tests below assume sane bmpImage masks */
3239 } else if (rDst==bmpImage->blue_mask && bDst==bmpImage->red_mask) {
3240 /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
3241 /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
3242 convs->Convert_0888_reverse
3244 srcbits,-bmpImage->bytes_per_line,
3247 /* ==== any 0888 bmp -> any 0888 dib ==== */
3248 convs->Convert_0888_any
3250 srcbits,-bmpImage->bytes_per_line,
3251 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3255 } else if (bmpImage->green_mask!=0x00ff00 ||
3256 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3258 /* the tests below assume sane bmpImage masks */
3260 /* ==== any 0888 bmp -> any 0888 dib ==== */
3261 convs->Convert_0888_any
3263 srcbits,-bmpImage->bytes_per_line,
3264 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3274 const char* srcbits;
3276 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3278 if (rDst==0xff0000 && gDst==0x00ff00 && bDst==0x0000ff) {
3279 if (bmpImage->green_mask==0x03e0) {
3280 if (bmpImage->red_mask==0x7f00) {
3281 /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
3282 convs->Convert_555_to_0888_asis
3284 srcbits,-bmpImage->bytes_per_line,
3286 } else if (bmpImage->blue_mask==0x7f00) {
3287 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
3288 convs->Convert_555_to_0888_reverse
3290 srcbits,-bmpImage->bytes_per_line,
3295 } else if (bmpImage->green_mask==0x07e0) {
3296 if (bmpImage->red_mask==0xf800) {
3297 /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
3298 convs->Convert_565_to_0888_asis
3300 srcbits,-bmpImage->bytes_per_line,
3302 } else if (bmpImage->blue_mask==0xf800) {
3303 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
3304 convs->Convert_565_to_0888_reverse
3306 srcbits,-bmpImage->bytes_per_line,
3314 } else if (rDst==0x0000ff && gDst==0x00ff00 && bDst==0xff0000) {
3315 if (bmpImage->green_mask==0x03e0) {
3316 if (bmpImage->blue_mask==0x7f00) {
3317 /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
3318 convs->Convert_555_to_0888_asis
3320 srcbits,-bmpImage->bytes_per_line,
3322 } else if (bmpImage->red_mask==0x7f00) {
3323 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
3324 convs->Convert_555_to_0888_reverse
3326 srcbits,-bmpImage->bytes_per_line,
3331 } else if (bmpImage->green_mask==0x07e0) {
3332 if (bmpImage->blue_mask==0xf800) {
3333 /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3334 convs->Convert_565_to_0888_asis
3336 srcbits,-bmpImage->bytes_per_line,
3338 } else if (bmpImage->red_mask==0xf800) {
3339 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3340 convs->Convert_565_to_0888_reverse
3342 srcbits,-bmpImage->bytes_per_line,
3351 if (bmpImage->green_mask==0x03e0 &&
3352 (bmpImage->red_mask==0x7f00 ||
3353 bmpImage->blue_mask==0x7f00)) {
3354 /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3355 convs->Convert_5x5_to_any0888
3357 srcbits,-bmpImage->bytes_per_line,
3358 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3361 } else if (bmpImage->green_mask==0x07e0 &&
3362 (bmpImage->red_mask==0xf800 ||
3363 bmpImage->blue_mask==0xf800)) {
3364 /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3365 convs->Convert_5x5_to_any0888
3367 srcbits,-bmpImage->bytes_per_line,
3368 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3380 if (bmpImage->red_mask==0 && bmpImage->green_mask==0 && bmpImage->blue_mask==0 && srccolors) {
3381 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3382 int rShift,gShift,bShift;
3385 rShift=X11DRV_DIB_MaskToShift(rDst);
3386 gShift=X11DRV_DIB_MaskToShift(gDst);
3387 bShift=X11DRV_DIB_MaskToShift(bDst);
3388 for (h = lines - 1; h >= 0; h--) {
3389 dstpixel=(DWORD*)dstbits;
3390 for (x = 0; x < width; x++) {
3391 PALETTEENTRY srcval;
3392 srcval = srccolors[XGetPixel(bmpImage, x, h)];
3393 *dstpixel++=(srcval.peRed << rShift) |
3394 (srcval.peGreen << gShift) |
3395 (srcval.peBlue << bShift);
3397 dstbits += linebytes;
3405 if (bmpImage->red_mask==0 && bmpImage->green_mask==0 && bmpImage->blue_mask==0 && srccolors) {
3406 /* ==== pal 8 bmp -> any 0888 dib ==== */
3407 int rShift,gShift,bShift;
3408 const void* srcbits;
3409 const BYTE* srcpixel;
3412 rShift=X11DRV_DIB_MaskToShift(rDst);
3413 gShift=X11DRV_DIB_MaskToShift(gDst);
3414 bShift=X11DRV_DIB_MaskToShift(bDst);
3415 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3416 for (h = lines - 1; h >= 0; h--) {
3418 dstpixel=(DWORD*)dstbits;
3419 for (x = 0; x < width; x++) {
3420 PALETTEENTRY srcval;
3421 srcval=srccolors[(int)*srcpixel++];
3422 *dstpixel++=(srcval.peRed << rShift) |
3423 (srcval.peGreen << gShift) |
3424 (srcval.peBlue << bShift);
3426 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
3427 dstbits += linebytes;
3437 /* ==== any bmp format -> any 0888 dib ==== */
3438 int rShift,gShift,bShift;
3441 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%lx,%lx,%lx)\n",
3442 bmpImage->depth, bmpImage->red_mask,
3443 bmpImage->green_mask, bmpImage->blue_mask,
3446 rShift=X11DRV_DIB_MaskToShift(rDst);
3447 gShift=X11DRV_DIB_MaskToShift(gDst);
3448 bShift=X11DRV_DIB_MaskToShift(bDst);
3449 for (h = lines - 1; h >= 0; h--) {
3450 dstpixel=(DWORD*)dstbits;
3451 for (x = 0; x < width; x++) {
3453 srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
3454 *dstpixel++=(GetRValue(srcval) << rShift) |
3455 (GetGValue(srcval) << gShift) |
3456 (GetBValue(srcval) << bShift);
3458 dstbits += linebytes;
3465 static int XGetSubImageErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
3467 return (event->request_code == X_GetImage && event->error_code == BadMatch);
3470 /***********************************************************************
3471 * X11DRV_DIB_SetImageBits_GetSubImage
3473 * Helper for X11DRV_DIB_SetImageBits
3475 static void X11DRV_DIB_SetImageBits_GetSubImage(
3476 const X11DRV_DIB_IMAGEBITS_DESCR *descr, XImage *bmpImage)
3478 /* compressed bitmaps may contain gaps in them. So make a copy
3479 * of the existing pixels first */
3482 SetRect( &bmprc, descr->xDest, descr->yDest,
3483 descr->xDest + descr->width , descr->yDest + descr->height );
3484 GetRgnBox( descr->physDev->region, &rc );
3485 /* convert from dc to drawable origin */
3486 OffsetRect( &rc, descr->physDev->org.x, descr->physDev->org.y);
3487 /* clip visible rect with bitmap */
3488 if( IntersectRect( &rc, &rc, &bmprc))
3490 X11DRV_expect_error( gdi_display, XGetSubImageErrorHandler, NULL );
3491 XGetSubImage( gdi_display, descr->drawable, rc.left, rc.top,
3492 rc.right - rc.left, rc.bottom - rc.top, AllPlanes,
3494 descr->xSrc + rc.left - bmprc.left,
3495 descr->ySrc + rc.top - bmprc.top);
3496 X11DRV_check_error();
3500 /***********************************************************************
3501 * X11DRV_DIB_SetImageBits
3503 * Transfer the bits to an X image.
3504 * Helper function for SetDIBits() and SetDIBitsToDevice().
3506 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3508 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3513 bmpImage = descr->image;
3515 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3516 descr->infoWidth, lines, 32, 0 );
3517 bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
3518 if(bmpImage->data == NULL) {
3519 ERR("Out of memory!\n");
3520 XDestroyImage( bmpImage );
3521 wine_tsx11_unlock();
3526 TRACE("Dib: depth=%d r=%lx g=%lx b=%lx\n",
3527 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3528 TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3529 bmpImage->depth,bmpImage->bits_per_pixel,
3530 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3532 /* Transfer the pixels */
3533 switch(descr->infoBpp)
3536 X11DRV_DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
3537 descr->width, descr->xSrc, (int *)(descr->colorMap),
3538 bmpImage, descr->dibpitch );
3541 if (descr->compression) {
3542 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3543 X11DRV_DIB_SetImageBits_RLE4( descr->lines, descr->bits,
3544 descr->infoWidth, descr->width,
3545 descr->xSrc, (int *)(descr->colorMap),
3548 X11DRV_DIB_SetImageBits_4( descr->lines, descr->bits,
3549 descr->infoWidth, descr->width,
3550 descr->xSrc, (int*)(descr->colorMap),
3551 bmpImage, descr->dibpitch );
3554 if (descr->compression) {
3555 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3556 X11DRV_DIB_SetImageBits_RLE8( descr->lines, descr->bits,
3557 descr->infoWidth, descr->width,
3558 descr->xSrc, (int *)(descr->colorMap),
3561 X11DRV_DIB_SetImageBits_8( descr->lines, descr->bits,
3562 descr->infoWidth, descr->width,
3563 descr->xSrc, (int *)(descr->colorMap),
3564 bmpImage, descr->dibpitch );
3568 X11DRV_DIB_SetImageBits_16( descr->lines, descr->bits,
3569 descr->infoWidth, descr->width,
3570 descr->xSrc, descr->physDev,
3571 descr->rMask, descr->gMask, descr->bMask,
3572 bmpImage, descr->dibpitch);
3575 X11DRV_DIB_SetImageBits_24( descr->lines, descr->bits,
3576 descr->infoWidth, descr->width,
3577 descr->xSrc, descr->physDev,
3578 descr->rMask, descr->gMask, descr->bMask,
3579 bmpImage, descr->dibpitch);
3582 X11DRV_DIB_SetImageBits_32( descr->lines, descr->bits,
3583 descr->infoWidth, descr->width,
3584 descr->xSrc, descr->physDev,
3585 descr->rMask, descr->gMask, descr->bMask,
3586 bmpImage, descr->dibpitch);
3589 WARN("(%d): Invalid depth\n", descr->infoBpp );
3593 TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3594 descr->drawable, descr->gc, bmpImage,
3595 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3596 descr->width, descr->height);
3597 #ifdef HAVE_LIBXXSHM
3600 XShmPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3601 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3602 descr->width, descr->height, FALSE );
3603 XSync( gdi_display, 0 );
3607 XPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3608 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3609 descr->width, descr->height );
3611 if (!descr->image) XDestroyImage( bmpImage );
3612 wine_tsx11_unlock();
3616 /***********************************************************************
3617 * X11DRV_DIB_GetImageBits
3619 * Transfer the bits from an X image.
3621 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3623 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3628 bmpImage = descr->image;
3630 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3631 descr->infoWidth, lines, 32, 0 );
3632 bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
3633 if(bmpImage->data == NULL) {
3634 ERR("Out of memory!\n");
3635 XDestroyImage( bmpImage );
3636 wine_tsx11_unlock();
3641 #ifdef HAVE_LIBXXSHM
3644 int saveRed, saveGreen, saveBlue;
3646 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3647 gdi_display, descr->drawable, bmpImage,
3648 descr->xSrc, descr->ySrc, AllPlanes);
3650 /* We must save and restore the bmpImage's masks in order
3651 * to preserve them across the call to XShmGetImage, which
3652 * decides to eleminate them since it doesn't happen to know
3653 * what the format of the image is supposed to be, even though
3655 saveRed = bmpImage->red_mask;
3656 saveBlue= bmpImage->blue_mask;
3657 saveGreen = bmpImage->green_mask;
3659 XShmGetImage( gdi_display, descr->drawable, bmpImage,
3660 descr->xSrc, descr->ySrc, AllPlanes);
3662 bmpImage->red_mask = saveRed;
3663 bmpImage->blue_mask = saveBlue;
3664 bmpImage->green_mask = saveGreen;
3667 #endif /* HAVE_LIBXXSHM */
3669 TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3670 gdi_display, descr->drawable, descr->xSrc, descr->ySrc, descr->width,
3671 lines, AllPlanes, ZPixmap, bmpImage, descr->xDest, descr->yDest);
3672 XGetSubImage( gdi_display, descr->drawable, descr->xSrc, descr->ySrc,
3673 descr->width, lines, AllPlanes, ZPixmap,
3674 bmpImage, descr->xDest, descr->yDest );
3677 TRACE("Dib: depth=%2d r=%lx g=%lx b=%lx\n",
3678 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3679 TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3680 bmpImage->depth,bmpImage->bits_per_pixel,
3681 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3682 /* Transfer the pixels */
3683 switch(descr->infoBpp)
3686 X11DRV_DIB_GetImageBits_1( descr->lines,(LPVOID)descr->bits,
3687 descr->infoWidth, descr->width,
3688 descr->colorMap, descr->palentry,
3689 bmpImage, descr->dibpitch );
3693 if (descr->compression) {
3694 FIXME("Compression not yet supported!\n");
3695 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 4 ) * abs(descr->lines))
3698 X11DRV_DIB_GetImageBits_4( descr->lines,(LPVOID)descr->bits,
3699 descr->infoWidth, descr->width,
3700 descr->colorMap, descr->palentry,
3701 bmpImage, descr->dibpitch );
3704 if (descr->compression) {
3705 FIXME("Compression not yet supported!\n");
3706 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 8 ) * abs(descr->lines))
3709 X11DRV_DIB_GetImageBits_8( descr->lines, (LPVOID)descr->bits,
3710 descr->infoWidth, descr->width,
3711 descr->colorMap, descr->palentry,
3712 bmpImage, descr->dibpitch );
3716 X11DRV_DIB_GetImageBits_16( descr->lines, (LPVOID)descr->bits,
3717 descr->infoWidth,descr->width,
3719 descr->rMask, descr->gMask, descr->bMask,
3720 bmpImage, descr->dibpitch );
3724 X11DRV_DIB_GetImageBits_24( descr->lines, (LPVOID)descr->bits,
3725 descr->infoWidth,descr->width,
3727 descr->rMask, descr->gMask, descr->bMask,
3728 bmpImage, descr->dibpitch);
3732 X11DRV_DIB_GetImageBits_32( descr->lines, (LPVOID)descr->bits,
3733 descr->infoWidth, descr->width,
3735 descr->rMask, descr->gMask, descr->bMask,
3736 bmpImage, descr->dibpitch);
3740 WARN("(%d): Invalid depth\n", descr->infoBpp );
3744 if (!descr->image) XDestroyImage( bmpImage );
3745 wine_tsx11_unlock();
3749 /*************************************************************************
3750 * X11DRV_SetDIBitsToDevice
3753 INT X11DRV_SetDIBitsToDevice( X11DRV_PDEVICE *physDev, INT xDest, INT yDest, DWORD cx,
3754 DWORD cy, INT xSrc, INT ySrc,
3755 UINT startscan, UINT lines, LPCVOID bits,
3756 const BITMAPINFO *info, UINT coloruse )
3758 X11DRV_DIB_IMAGEBITS_DESCR descr;
3764 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3765 &descr.infoBpp, &descr.compression ) == -1)
3768 top_down = (height < 0);
3769 if (top_down) height = -height;
3773 LPtoDP(physDev->hdc, &pt, 1);
3775 if (!lines || (startscan >= height)) return 0;
3776 if (!top_down && startscan + lines > height) lines = height - startscan;
3778 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3779 * and clamp all values to fit inside [startscan,startscan+lines]
3781 if (ySrc + cy <= startscan + lines)
3783 UINT y = startscan + lines - (ySrc + cy);
3784 if (ySrc < startscan) cy -= (startscan - ySrc);
3787 /* avoid getting unnecessary lines */
3789 if (y >= lines) return 0;
3794 if (y >= lines) return lines;
3795 ySrc = y; /* need to get all lines in top down mode */
3800 if (ySrc >= startscan + lines) return lines;
3801 pt.y += ySrc + cy - (startscan + lines);
3802 cy = startscan + lines - ySrc;
3804 if (cy > lines) cy = lines;
3806 if (xSrc >= width) return lines;
3807 if (xSrc + cx >= width) cx = width - xSrc;
3808 if (!cx || !cy) return lines;
3810 /* Update the pixmap from the DIB section */
3811 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod, FALSE);
3813 X11DRV_SetupGCForText( physDev ); /* To have the correct colors */
3815 XSetFunction(gdi_display, physDev->gc, X11DRV_XROPfunction[GetROP2(physDev->hdc) - 1]);
3816 wine_tsx11_unlock();
3818 switch (descr.infoBpp)
3823 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3824 coloruse == DIB_PAL_COLORS ? physDev : NULL, coloruse,
3825 physDev->depth, info, &descr.nColorMap );
3826 if (!descr.colorMap) return 0;
3827 descr.rMask = descr.gMask = descr.bMask = 0;
3831 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3832 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3833 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3839 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3840 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3841 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3846 descr.physDev = physDev;
3849 descr.palentry = NULL;
3850 descr.lines = top_down ? -lines : lines;
3851 descr.infoWidth = width;
3852 descr.depth = physDev->depth;
3853 descr.drawable = physDev->drawable;
3854 descr.gc = physDev->gc;
3857 descr.xDest = physDev->org.x + pt.x;
3858 descr.yDest = physDev->org.y + pt.y;
3861 descr.useShm = FALSE;
3862 descr.dibpitch = ((width * descr.infoBpp + 31) &~31) / 8;
3864 result = X11DRV_DIB_SetImageBits( &descr );
3866 if (descr.infoBpp <= 8)
3867 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3869 /* Update the DIBSection of the pixmap */
3870 X11DRV_UnlockDIBSection(physDev, TRUE);
3875 /***********************************************************************
3876 * SetDIBits (X11DRV.@)
3878 INT X11DRV_SetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan,
3879 UINT lines, LPCVOID bits, const BITMAPINFO *info, UINT coloruse )
3881 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3882 X11DRV_DIB_IMAGEBITS_DESCR descr;
3884 LONG width, height, tmpheight;
3887 descr.physDev = physDev;
3889 if (!physBitmap) return 0;
3891 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3892 &descr.infoBpp, &descr.compression ) == -1)
3896 if (height < 0) height = -height;
3897 if (!lines || (startscan >= height))
3900 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
3902 if (startscan + lines > height) lines = height - startscan;
3904 switch (descr.infoBpp)
3909 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3910 coloruse == DIB_PAL_COLORS ? descr.physDev : NULL, coloruse,
3911 physBitmap->pixmap_depth,
3912 info, &descr.nColorMap );
3913 if (!descr.colorMap) return 0;
3914 descr.rMask = descr.gMask = descr.bMask = 0;
3918 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3919 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3920 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3926 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3927 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3928 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3937 descr.palentry = NULL;
3938 descr.infoWidth = width;
3939 descr.lines = tmpheight >= 0 ? lines : -lines;
3940 descr.depth = physBitmap->pixmap_depth;
3941 descr.drawable = physBitmap->pixmap;
3942 descr.gc = BITMAP_GC(physBitmap);
3946 descr.yDest = height - startscan - lines;
3947 descr.width = bitmap.bmWidth;
3948 descr.height = lines;
3949 descr.useShm = FALSE;
3950 descr.dibpitch = ((descr.infoWidth * descr.infoBpp + 31) &~31) / 8;
3951 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod, FALSE );
3952 result = X11DRV_DIB_SetImageBits( &descr );
3953 X11DRV_DIB_Unlock( physBitmap, TRUE );
3955 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3960 /***********************************************************************
3961 * GetDIBits (X11DRV.@)
3963 INT X11DRV_GetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan, UINT lines,
3964 LPVOID bits, BITMAPINFO *info, UINT coloruse )
3966 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3968 X11DRV_DIB_IMAGEBITS_DESCR descr;
3969 PALETTEENTRY palette[256];
3972 LONG width, tempHeight;
3977 GetPaletteEntries( GetCurrentObject( physDev->hdc, OBJ_PAL ), 0, 256, palette );
3979 if (!physBitmap) return 0;
3980 if (!(obj_size = GetObjectW( hbitmap, sizeof(dib), &dib ))) return 0;
3982 bitmap_type = DIB_GetBitmapInfo( (BITMAPINFOHEADER*)info, &width, &tempHeight, &descr.infoBpp, &descr.compression);
3983 descr.lines = tempHeight;
3984 if (bitmap_type == -1)
3986 ERR("Invalid bitmap\n");
3989 core_header = (bitmap_type == 0);
3990 colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
3992 TRACE("%u scanlines of (%i,%i) -> (%li,%i) starting from %u\n",
3993 lines, dib.dsBm.bmWidth, dib.dsBm.bmHeight, width, descr.lines, startscan);
3995 if( lines > dib.dsBm.bmHeight ) lines = dib.dsBm.bmHeight;
3997 height = descr.lines;
3998 if (height < 0) height = -height;
3999 if( lines > height ) lines = height;
4000 /* Top-down images have a negative biHeight, the scanlines of theses images
4001 * were inverted in X11DRV_DIB_GetImageBits_xx
4002 * To prevent this we simply change the sign of lines
4003 * (the number of scan lines to copy).
4004 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4006 if( descr.lines < 0 && lines > 0) lines = -lines;
4008 if( startscan >= dib.dsBm.bmHeight ) return 0;
4010 descr.colorMap = NULL;
4012 switch (descr.infoBpp)
4017 descr.rMask= descr.gMask = descr.bMask = 0;
4018 if(coloruse == DIB_RGB_COLORS)
4019 descr.colorMap = colorPtr;
4021 int num_colors = 1 << descr.infoBpp, i;
4024 WORD *index = (WORD*)colorPtr;
4025 descr.colorMap = rgb = HeapAlloc(GetProcessHeap(), 0, num_colors * sizeof(RGBQUAD));
4026 for(i = 0; i < num_colors; i++, rgb++, index++) {
4027 colref = X11DRV_PALETTE_ToLogical(X11DRV_PALETTE_ToPhysical(physDev, PALETTEINDEX(*index)));
4028 rgb->rgbRed = GetRValue(colref);
4029 rgb->rgbGreen = GetGValue(colref);
4030 rgb->rgbBlue = GetBValue(colref);
4031 rgb->rgbReserved = 0;
4037 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4038 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4039 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4043 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4044 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4045 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4049 descr.physDev = physDev;
4050 descr.palentry = palette;
4053 descr.infoWidth = width;
4054 descr.lines = lines;
4055 descr.depth = physBitmap->pixmap_depth;
4056 descr.drawable = physBitmap->pixmap;
4057 descr.gc = BITMAP_GC(physBitmap);
4058 descr.width = dib.dsBm.bmWidth;
4059 descr.height = dib.dsBm.bmHeight;
4063 descr.sizeImage = core_header ? 0 : info->bmiHeader.biSizeImage;
4065 if (descr.lines > 0)
4067 descr.ySrc = (descr.height-1) - (startscan + (lines-1));
4071 descr.ySrc = startscan;
4073 #ifdef HAVE_LIBXXSHM
4074 descr.useShm = (obj_size == sizeof(DIBSECTION)) && (physBitmap->shminfo.shmid != -1);
4076 descr.useShm = FALSE;
4078 descr.dibpitch = (obj_size == sizeof(DIBSECTION)) ? dib.dsBm.bmWidthBytes
4079 : (((descr.infoWidth * descr.infoBpp + 31) &~31) / 8);
4081 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod, FALSE );
4082 X11DRV_DIB_GetImageBits( &descr );
4083 X11DRV_DIB_Unlock( physBitmap, TRUE );
4085 if(!core_header && info->bmiHeader.biSizeImage == 0) /* Fill in biSizeImage */
4086 info->bmiHeader.biSizeImage = X11DRV_DIB_GetDIBImageBytes( descr.infoWidth,
4090 if (descr.compression == BI_BITFIELDS)
4092 *(DWORD *)info->bmiColors = descr.rMask;
4093 *((DWORD *)info->bmiColors + 1) = descr.gMask;
4094 *((DWORD *)info->bmiColors + 2) = descr.bMask;
4096 else if (!core_header)
4098 /* if RLE or JPEG compression were supported,
4099 * this line would be invalid. */
4100 info->bmiHeader.biCompression = 0;
4103 if(descr.colorMap && descr.colorMap != colorPtr)
4104 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4108 /***********************************************************************
4109 * DIB_DoProtectDIBSection
4111 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP *physBitmap, DWORD new_prot )
4115 VirtualProtect(physBitmap->base, physBitmap->size, new_prot, &old_prot);
4116 TRACE("Changed protection from %ld to %ld\n", old_prot, new_prot);
4119 /***********************************************************************
4120 * X11DRV_DIB_DoCopyDIBSection
4122 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB,
4123 void *colorMap, int nColorMap,
4125 DWORD xSrc, DWORD ySrc,
4126 DWORD xDest, DWORD yDest,
4127 DWORD width, DWORD height)
4129 DIBSECTION dibSection;
4130 X11DRV_DIB_IMAGEBITS_DESCR descr;
4131 int identity[2] = {0,1};
4133 if (!GetObjectW( physBitmap->hbitmap, sizeof(dibSection), &dibSection )) return;
4135 descr.physDev = NULL;
4136 descr.palentry = NULL;
4137 descr.infoWidth = dibSection.dsBmih.biWidth;
4138 descr.infoBpp = dibSection.dsBmih.biBitCount;
4139 descr.lines = dibSection.dsBmih.biHeight;
4140 descr.image = physBitmap->image;
4141 descr.colorMap = colorMap;
4142 descr.nColorMap = nColorMap;
4143 descr.bits = dibSection.dsBm.bmBits;
4144 descr.depth = physBitmap->pixmap_depth;
4145 descr.compression = dibSection.dsBmih.biCompression;
4147 if(descr.infoBpp == 1)
4148 descr.colorMap = (void*)identity;
4150 switch (descr.infoBpp)
4155 descr.rMask = descr.gMask = descr.bMask = 0;
4159 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0x7c00;
4160 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x03e0;
4161 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x001f;
4166 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0xff0000;
4167 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x00ff00;
4168 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x0000ff;
4173 descr.drawable = dest;
4174 descr.gc = BITMAP_GC(physBitmap);
4177 descr.xDest = xDest;
4178 descr.yDest = yDest;
4179 descr.width = width;
4180 descr.height = height;
4181 descr.sizeImage = 0;
4183 #ifdef HAVE_LIBXXSHM
4184 descr.useShm = (physBitmap->shminfo.shmid != -1);
4186 descr.useShm = FALSE;
4188 descr.dibpitch = dibSection.dsBm.bmWidthBytes;
4192 TRACE("Copying from Pixmap to DIB bits\n");
4193 X11DRV_DIB_GetImageBits( &descr );
4197 TRACE("Copying from DIB bits to Pixmap\n");
4198 X11DRV_DIB_SetImageBits( &descr );
4202 /***********************************************************************
4203 * X11DRV_DIB_CopyDIBSection
4205 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
4206 DWORD xSrc, DWORD ySrc, DWORD xDest, DWORD yDest,
4207 DWORD width, DWORD height)
4210 X_PHYSBITMAP *physBitmap;
4211 int nColorMap = 0, *colorMap = NULL, aColorMap = FALSE;
4213 TRACE("(%p,%p,%ld,%ld,%ld,%ld,%ld,%ld)\n", physDevSrc->hdc, physDevDst->hdc,
4214 xSrc, ySrc, xDest, yDest, width, height);
4215 /* this function is meant as an optimization for BitBlt,
4216 * not to be called otherwise */
4217 physBitmap = physDevSrc->bitmap;
4218 if (!physBitmap || GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib ) != sizeof(dib))
4220 ERR("called for non-DIBSection!?\n");
4223 /* while BitBlt should already have made sure we only get
4224 * positive values, we should check for oversize values */
4225 if ((xSrc < dib.dsBm.bmWidth) &&
4226 (ySrc < dib.dsBm.bmHeight)) {
4227 if (xSrc + width > dib.dsBm.bmWidth)
4228 width = dib.dsBm.bmWidth - xSrc;
4229 if (ySrc + height > dib.dsBm.bmHeight)
4230 height = dib.dsBm.bmHeight - ySrc;
4231 /* if the source bitmap is 8bpp or less, we're supposed to use the
4232 * DC's palette for color conversion (not the DIB color table) */
4233 if (dib.dsBm.bmBitsPixel <= 8) {
4234 HPALETTE hPalette = GetCurrentObject( physDevSrc->hdc, OBJ_PAL );
4235 if (!hPalette || (hPalette == GetStockObject(DEFAULT_PALETTE))) {
4236 /* HACK: no palette has been set in the source DC,
4237 * use the DIB colormap instead - this is necessary in some
4238 * cases since we need to do depth conversion in some places
4239 * where real Windows can just copy data straight over */
4240 colorMap = physBitmap->colorMap;
4241 nColorMap = physBitmap->nColorMap;
4243 colorMap = X11DRV_DIB_BuildColorMap( physDevSrc, (WORD)-1,
4244 dib.dsBm.bmBitsPixel,
4245 (BITMAPINFO*)&dib.dsBmih,
4247 if (colorMap) aColorMap = TRUE;
4250 /* perform the copy */
4251 X11DRV_DIB_DoCopyDIBSection(physBitmap, FALSE, colorMap, nColorMap,
4252 physDevDst->drawable, xSrc, ySrc,
4253 physDevDst->org.x + xDest, physDevDst->org.y + yDest,
4255 /* free color mapping */
4257 HeapFree(GetProcessHeap(), 0, colorMap);
4261 /***********************************************************************
4262 * X11DRV_DIB_DoUpdateDIBSection
4264 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB)
4268 GetObjectW( physBitmap->hbitmap, sizeof(bitmap), &bitmap );
4269 X11DRV_DIB_DoCopyDIBSection(physBitmap, toDIB,
4270 physBitmap->colorMap, physBitmap->nColorMap,
4271 physBitmap->pixmap, 0, 0, 0, 0,
4272 bitmap.bmWidth, bitmap.bmHeight);
4275 /***********************************************************************
4276 * X11DRV_DIB_FaultHandler
4278 static LONG CALLBACK X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep )
4280 X_PHYSBITMAP *physBitmap = NULL;
4285 if (ep->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
4286 return EXCEPTION_CONTINUE_SEARCH;
4288 addr = (BYTE *)ep->ExceptionRecord->ExceptionInformation[1];
4290 EnterCriticalSection(&dibs_cs);
4291 LIST_FOR_EACH( ptr, &dibs_list )
4293 physBitmap = LIST_ENTRY( ptr, X_PHYSBITMAP, entry );
4294 if ((physBitmap->base <= addr) && (addr < physBitmap->base + physBitmap->size))
4300 LeaveCriticalSection(&dibs_cs);
4302 if (!found) return EXCEPTION_CONTINUE_SEARCH;
4304 X11DRV_DIB_Lock( physBitmap, DIB_Status_None, FALSE );
4305 if (ep->ExceptionRecord->ExceptionInformation[0]) {
4306 /* the app tried to write the DIB bits */
4307 X11DRV_DIB_Coerce( physBitmap, DIB_Status_AppMod, FALSE );
4309 /* the app tried to read the DIB bits */
4310 X11DRV_DIB_Coerce( physBitmap, DIB_Status_InSync, FALSE );
4312 X11DRV_DIB_Unlock( physBitmap, TRUE );
4314 return EXCEPTION_CONTINUE_EXECUTION;
4317 /***********************************************************************
4320 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *physBitmap, INT req, BOOL lossy)
4322 INT ret = DIB_Status_None;
4324 if (!physBitmap->image) return ret; /* not a DIB section */
4325 EnterCriticalSection(&physBitmap->lock);
4326 ret = physBitmap->status;
4328 case DIB_Status_GdiMod:
4329 /* GDI access - request to draw on pixmap */
4330 switch (physBitmap->status)
4333 case DIB_Status_None:
4334 physBitmap->p_status = DIB_Status_GdiMod;
4335 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4338 case DIB_Status_GdiMod:
4339 TRACE("GdiMod requested in status GdiMod\n" );
4340 physBitmap->p_status = DIB_Status_GdiMod;
4343 case DIB_Status_InSync:
4344 TRACE("GdiMod requested in status InSync\n" );
4345 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4346 physBitmap->status = DIB_Status_GdiMod;
4347 physBitmap->p_status = DIB_Status_InSync;
4350 case DIB_Status_AppMod:
4351 TRACE("GdiMod requested in status AppMod\n" );
4353 /* make it readonly to avoid app changing data while we copy */
4354 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4355 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4357 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4358 physBitmap->p_status = DIB_Status_AppMod;
4359 physBitmap->status = DIB_Status_GdiMod;
4364 case DIB_Status_InSync:
4365 /* App access - request access to read DIB surface */
4366 /* (typically called from signal handler) */
4367 switch (physBitmap->status)
4370 case DIB_Status_None:
4371 /* shouldn't happen from signal handler */
4374 case DIB_Status_GdiMod:
4375 TRACE("InSync requested in status GdiMod\n" );
4377 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4378 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4380 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4381 physBitmap->status = DIB_Status_InSync;
4384 case DIB_Status_InSync:
4385 TRACE("InSync requested in status InSync\n" );
4386 /* shouldn't happen from signal handler */
4389 case DIB_Status_AppMod:
4390 TRACE("InSync requested in status AppMod\n" );
4391 /* no reason to do anything here, and this
4392 * shouldn't happen from signal handler */
4397 case DIB_Status_AppMod:
4398 /* App access - request access to write DIB surface */
4399 /* (typically called from signal handler) */
4400 switch (physBitmap->status)
4403 case DIB_Status_None:
4404 /* shouldn't happen from signal handler */
4407 case DIB_Status_GdiMod:
4408 TRACE("AppMod requested in status GdiMod\n" );
4409 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4410 if (!lossy) X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4411 physBitmap->status = DIB_Status_AppMod;
4414 case DIB_Status_InSync:
4415 TRACE("AppMod requested in status InSync\n" );
4416 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4417 physBitmap->status = DIB_Status_AppMod;
4420 case DIB_Status_AppMod:
4421 TRACE("AppMod requested in status AppMod\n" );
4422 /* shouldn't happen from signal handler */
4427 /* it is up to the caller to do the copy/conversion, probably
4428 * using the return value to decide where to copy from */
4430 LeaveCriticalSection(&physBitmap->lock);
4434 /***********************************************************************
4437 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *physBitmap, INT req, BOOL lossy)
4439 INT ret = DIB_Status_None;
4441 if (!physBitmap->image) return ret; /* not a DIB section */
4442 TRACE("Locking %p from thread %04lx\n", physBitmap->hbitmap, GetCurrentThreadId());
4443 EnterCriticalSection(&physBitmap->lock);
4444 ret = physBitmap->status;
4445 if (req != DIB_Status_None)
4446 X11DRV_DIB_Coerce(physBitmap, req, lossy);
4450 /***********************************************************************
4453 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *physBitmap, BOOL commit)
4455 if (!physBitmap->image) return; /* not a DIB section */
4456 switch (physBitmap->status)
4459 case DIB_Status_None:
4460 /* in case anyone is wondering, this is the "signal handler doesn't
4461 * work" case, where we always have to be ready for app access */
4463 switch (physBitmap->p_status)
4465 case DIB_Status_GdiMod:
4466 TRACE("Unlocking and syncing from GdiMod\n" );
4467 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4471 TRACE("Unlocking without needing to sync\n" );
4475 else TRACE("Unlocking with no changes\n");
4476 physBitmap->p_status = DIB_Status_None;
4479 case DIB_Status_GdiMod:
4480 TRACE("Unlocking in status GdiMod\n" );
4481 /* DIB was protected in Coerce */
4483 /* no commit, revert to InSync if applicable */
4484 if ((physBitmap->p_status == DIB_Status_InSync) ||
4485 (physBitmap->p_status == DIB_Status_AppMod)) {
4486 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4487 physBitmap->status = DIB_Status_InSync;
4492 case DIB_Status_InSync:
4493 TRACE("Unlocking in status InSync\n" );
4494 /* DIB was already protected in Coerce */
4497 case DIB_Status_AppMod:
4498 TRACE("Unlocking in status AppMod\n" );
4499 /* DIB was already protected in Coerce */
4500 /* this case is ordinary only called from the signal handler,
4501 * so we don't bother to check for !commit */
4504 LeaveCriticalSection(&physBitmap->lock);
4505 TRACE("Unlocked %p\n", physBitmap->hbitmap);
4508 /***********************************************************************
4509 * X11DRV_CoerceDIBSection
4511 INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req, BOOL lossy)
4513 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4514 return X11DRV_DIB_Coerce(physDev->bitmap, req, lossy);
4517 /***********************************************************************
4518 * X11DRV_LockDIBSection
4520 INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req, BOOL lossy)
4522 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4523 return X11DRV_DIB_Lock(physDev->bitmap, req, lossy);
4526 /***********************************************************************
4527 * X11DRV_UnlockDIBSection
4529 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
4531 if (!physDev || !physDev->bitmap) return;
4532 X11DRV_DIB_Unlock(physDev->bitmap, commit);
4536 #ifdef HAVE_LIBXXSHM
4537 /***********************************************************************
4538 * X11DRV_XShmErrorHandler
4541 static int XShmErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
4543 return 1; /* FIXME: should check event contents */
4546 /***********************************************************************
4547 * X11DRV_XShmCreateImage
4550 static XImage *X11DRV_XShmCreateImage( int width, int height, int bpp,
4551 XShmSegmentInfo* shminfo)
4555 image = XShmCreateImage(gdi_display, visual, bpp, ZPixmap, NULL, shminfo, width, height);
4558 shminfo->shmid = shmget(IPC_PRIVATE, image->bytes_per_line * height,
4560 if( shminfo->shmid != -1 )
4562 shminfo->shmaddr = image->data = shmat(shminfo->shmid, 0, 0);
4563 if( shminfo->shmaddr != (char*)-1 )
4567 shminfo->readOnly = FALSE;
4568 X11DRV_expect_error( gdi_display, XShmErrorHandler, NULL );
4569 ok = (XShmAttach( gdi_display, shminfo ) != 0);
4570 XSync( gdi_display, False );
4571 if (X11DRV_check_error()) ok = FALSE;
4574 shmctl(shminfo->shmid, IPC_RMID, 0);
4575 return image; /* Success! */
4577 /* An error occurred */
4578 shmdt(shminfo->shmaddr);
4580 shmctl(shminfo->shmid, IPC_RMID, 0);
4581 shminfo->shmid = -1;
4583 XFlush(gdi_display);
4584 XDestroyImage(image);
4589 #endif /* HAVE_LIBXXSHM */
4592 /***********************************************************************
4593 * X11DRV_CreateDIBSection (X11DRV.@)
4595 HBITMAP X11DRV_CreateDIBSection( X11DRV_PDEVICE *physDev, HBITMAP hbitmap,
4596 const BITMAPINFO *bmi, UINT usage )
4598 X_PHYSBITMAP *physBitmap;
4601 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return 0;
4602 physBitmap->status = DIB_Status_None;
4604 GetObjectW( hbitmap, sizeof(dib), &dib );
4606 /* create color map */
4607 if (dib.dsBm.bmBitsPixel <= 8)
4609 physBitmap->colorMap = X11DRV_DIB_BuildColorMap( usage == DIB_PAL_COLORS ? physDev : NULL,
4610 usage, dib.dsBm.bmBitsPixel, bmi,
4611 &physBitmap->nColorMap );
4612 physBitmap->colorTable = X11DRV_DIB_BuildColorTable( physDev, usage, dib.dsBm.bmBitsPixel, bmi );
4615 /* create pixmap and X image */
4617 physBitmap->pixmap_depth = (dib.dsBm.bmBitsPixel == 1) ? 1 : screen_depth;
4618 physBitmap->pixmap = XCreatePixmap( gdi_display, root_window, dib.dsBm.bmWidth,
4619 dib.dsBm.bmHeight, physBitmap->pixmap_depth );
4620 #ifdef HAVE_LIBXXSHM
4621 physBitmap->shminfo.shmid = -1;
4622 if (!XShmQueryExtension(gdi_display) ||
4623 !(physBitmap->image = X11DRV_XShmCreateImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4624 physBitmap->pixmap_depth, &physBitmap->shminfo )) )
4626 physBitmap->image = X11DRV_DIB_CreateXImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4627 physBitmap->pixmap_depth );
4628 wine_tsx11_unlock();
4629 if (!physBitmap->pixmap || !physBitmap->image) return 0;
4631 /* install fault handler */
4632 InitializeCriticalSection( &physBitmap->lock );
4634 physBitmap->base = dib.dsBm.bmBits;
4635 physBitmap->size = dib.dsBmih.biSizeImage;
4636 physBitmap->status = DIB_Status_AppMod;
4639 dibs_handler = AddVectoredExceptionHandler( TRUE, X11DRV_DIB_FaultHandler );
4640 EnterCriticalSection( &dibs_cs );
4641 list_add_head( &dibs_list, &physBitmap->entry );
4642 LeaveCriticalSection( &dibs_cs );
4644 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4649 /***********************************************************************
4650 * X11DRV_DIB_DeleteDIBSection
4652 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP *physBitmap, DIBSECTION *dib)
4656 EnterCriticalSection( &dibs_cs );
4657 list_remove( &physBitmap->entry );
4658 last = list_empty( &dibs_list );
4659 LeaveCriticalSection( &dibs_cs );
4663 RemoveVectoredExceptionHandler( dibs_handler );
4664 dibs_handler = NULL;
4667 if (dib->dshSection)
4668 X11DRV_DIB_Coerce(physBitmap, DIB_Status_InSync, FALSE);
4670 if (physBitmap->image)
4673 #ifdef HAVE_LIBXXSHM
4674 if (physBitmap->shminfo.shmid != -1)
4676 XShmDetach( gdi_display, &(physBitmap->shminfo) );
4677 XDestroyImage( physBitmap->image );
4678 shmdt( physBitmap->shminfo.shmaddr );
4679 physBitmap->shminfo.shmid = -1;
4683 XDestroyImage( physBitmap->image );
4684 wine_tsx11_unlock();
4687 HeapFree(GetProcessHeap(), 0, physBitmap->colorMap);
4688 HeapFree(GetProcessHeap(), 0, physBitmap->colorTable);
4689 DeleteCriticalSection(&physBitmap->lock);
4692 /***********************************************************************
4693 * SetDIBColorTable (X11DRV.@)
4695 UINT X11DRV_SetDIBColorTable( X11DRV_PDEVICE *physDev, UINT start, UINT count, const RGBQUAD *colors )
4699 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4701 if (!physBitmap) return 0;
4702 GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib );
4704 if (physBitmap->colorMap && start < physBitmap->nColorMap) {
4705 UINT end = count + start;
4706 if (end > physBitmap->nColorMap) end = physBitmap->nColorMap;
4708 * Changing color table might change the mapping between
4709 * DIB colors and X11 colors and thus alter the visible state
4710 * of the bitmap object.
4712 X11DRV_DIB_Lock( physBitmap, DIB_Status_AppMod, FALSE );
4713 memcpy(physBitmap->colorTable + start, colors, (end - start) * sizeof(RGBQUAD));
4714 X11DRV_DIB_GenColorMap( physDev, physBitmap->colorMap, DIB_RGB_COLORS,
4715 dib.dsBm.bmBitsPixel,
4716 TRUE, colors, start, end );
4717 X11DRV_DIB_Unlock( physBitmap, TRUE );
4723 /***********************************************************************
4724 * GetDIBColorTable (X11DRV.@)
4726 UINT X11DRV_GetDIBColorTable( X11DRV_PDEVICE *physDev, UINT start, UINT count, RGBQUAD *colors )
4729 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4731 if (physBitmap && physBitmap->colorTable && start < physBitmap->nColorMap) {
4732 if (start + count > physBitmap->nColorMap) count = physBitmap->nColorMap - start;
4733 memcpy(colors, physBitmap->colorTable + start, count * sizeof(RGBQUAD));
4741 /***********************************************************************
4742 * X11DRV_DIB_CreateDIBFromBitmap
4744 * Allocates a packed DIB and copies the bitmap data into it.
4746 HGLOBAL X11DRV_DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
4751 LPBITMAPINFOHEADER pbmiHeader;
4752 unsigned int cDataSize, cPackedSize, OffsetBits;
4755 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
4758 * A packed DIB contains a BITMAPINFO structure followed immediately by
4759 * an optional color palette and the pixel data.
4762 /* Calculate the size of the packed DIB */
4763 cDataSize = X11DRV_DIB_GetDIBWidthBytes( bmp.bmWidth, bmp.bmBitsPixel ) * abs( bmp.bmHeight );
4764 cPackedSize = sizeof(BITMAPINFOHEADER)
4765 + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
4767 /* Get the offset to the bits */
4768 OffsetBits = cPackedSize - cDataSize;
4770 /* Allocate the packed DIB */
4771 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
4772 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
4776 WARN("Could not allocate packed DIB!\n");
4780 /* A packed DIB starts with a BITMAPINFOHEADER */
4781 pPackedDIB = GlobalLock(hPackedDIB);
4782 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
4784 /* Init the BITMAPINFOHEADER */
4785 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
4786 pbmiHeader->biWidth = bmp.bmWidth;
4787 pbmiHeader->biHeight = bmp.bmHeight;
4788 pbmiHeader->biPlanes = 1;
4789 pbmiHeader->biBitCount = bmp.bmBitsPixel;
4790 pbmiHeader->biCompression = BI_RGB;
4791 pbmiHeader->biSizeImage = 0;
4792 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
4793 pbmiHeader->biClrUsed = 0;
4794 pbmiHeader->biClrImportant = 0;
4796 /* Retrieve the DIB bits from the bitmap and fill in the
4797 * DIB color table if present */
4799 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
4800 hBmp, /* Handle to bitmap */
4801 0, /* First scan line to set in dest bitmap */
4802 bmp.bmHeight, /* Number of scan lines to copy */
4803 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
4804 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
4805 0); /* RGB or palette index */
4806 GlobalUnlock(hPackedDIB);
4808 /* Cleanup if GetDIBits failed */
4809 if (nLinesCopied != bmp.bmHeight)
4811 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
4812 GlobalFree(hPackedDIB);
4819 /**************************************************************************
4820 * X11DRV_DIB_CreateDIBFromPixmap
4822 * Allocates a packed DIB and copies the Pixmap data into it.
4823 * The Pixmap passed in is deleted after the conversion.
4825 HGLOBAL X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap, HDC hdc)
4828 X_PHYSBITMAP *physBitmap;
4829 HBITMAP hBmp = 0, old;
4830 HGLOBAL hPackedDIB = 0;
4832 int x,y; /* Unused */
4833 unsigned border_width; /* Unused */
4834 unsigned int depth, width, height;
4836 /* Get the Pixmap dimensions and bit depth */
4838 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
4839 &border_width, &depth)) depth = 0;
4840 wine_tsx11_unlock();
4841 if (!depth) return 0;
4843 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
4844 width, height, depth);
4847 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
4848 * and make it a container for the pixmap passed.
4850 hBmp = CreateBitmap( width, height, 1, depth, NULL );
4852 /* force bitmap to be owned by a screen DC */
4853 hdcMem = CreateCompatibleDC( hdc );
4854 old = SelectObject( hdcMem, hBmp );
4856 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4859 if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
4860 physBitmap->pixmap = pixmap;
4861 wine_tsx11_unlock();
4863 SelectObject( hdcMem, old );
4867 * Create a packed DIB from the Pixmap wrapper bitmap created above.
4868 * A packed DIB contains a BITMAPINFO structure followed immediately by
4869 * an optional color palette and the pixel data.
4871 hPackedDIB = X11DRV_DIB_CreateDIBFromBitmap(hdc, hBmp);
4873 /* We can now get rid of the HBITMAP wrapper we created earlier.
4874 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
4878 TRACE("\tReturning packed DIB %p\n", hPackedDIB);
4883 /**************************************************************************
4884 * X11DRV_DIB_CreatePixmapFromDIB
4886 * Creates a Pixmap from a packed DIB
4888 Pixmap X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB, HDC hdc )
4891 X_PHYSBITMAP *physBitmap;
4895 /* Create a DDB from the DIB */
4897 pbmi = GlobalLock(hPackedDIB);
4898 hBmp = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT,
4899 (LPBYTE)pbmi + X11DRV_DIB_BitmapInfoSize( pbmi, DIB_RGB_COLORS ),
4900 pbmi, DIB_RGB_COLORS);
4901 GlobalUnlock(hPackedDIB);
4903 /* clear the physBitmap so that we can steal its pixmap */
4904 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4905 pixmap = physBitmap->pixmap;
4906 physBitmap->pixmap = 0;
4908 /* Delete the DDB we created earlier now that we have stolen its pixmap */
4911 TRACE("Returning Pixmap %ld\n", pixmap);