push 50b650a476c6f0df362ce67f063848f086de44d9
[wine/hacks.git] / dlls / winex11.drv / dib.c
blob5d4ac459a943434cf4263d9cd701652b85a93e93
1 /*
2 * X11DRV device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <X11/Xlib.h>
24 #ifdef HAVE_LIBXXSHM
25 #include <X11/extensions/XShm.h>
26 # ifdef HAVE_SYS_SHM_H
27 # include <sys/shm.h>
28 # endif
29 # ifdef HAVE_SYS_IPC_H
30 # include <sys/ipc.h>
31 # endif
32 #endif /* defined(HAVE_LIBXXSHM) */
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "x11drv.h"
41 #include "excpt.h"
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 =
51 0, 0, &dibs_cs,
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() */
62 typedef struct
64 X11DRV_PDEVICE *physDev;
65 LPCVOID bits;
66 XImage *image;
67 PALETTEENTRY *palentry;
68 int lines;
69 DWORD infoWidth;
70 WORD depth;
71 WORD infoBpp;
72 WORD compression;
73 RGBQUAD *colorMap;
74 int nColorMap;
75 Drawable drawable;
76 GC gc;
77 int xSrc;
78 int ySrc;
79 int xDest;
80 int yDest;
81 int width;
82 int height;
83 DWORD rMask;
84 DWORD gMask;
85 DWORD bMask;
86 BOOL useShm;
87 int dibpitch;
88 DWORD sizeImage;
89 } X11DRV_DIB_IMAGEBITS_DESCR;
92 enum Rle_EscapeCodes
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);
101 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *,INT);
102 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *,BOOL);
105 Some of the following helper functions are duplicated in
106 dlls/gdi/dib.c
109 /***********************************************************************
110 * X11DRV_DIB_GetXImageWidthBytes
112 * Return the width of an X image in bytes
114 static inline 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 );
122 if (testimage)
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));
132 error:
133 WARN( "(%d): Unsupported depth\n", depth );
134 return 4 * width;
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 )
145 int words;
147 switch(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;
152 case 15:
153 case 16: words = (width + 1) / 2; break;
154 case 24: words = (width * 3 + 3) / 4; break;
155 default:
156 WARN("(%d): Unsupported depth\n", depth );
157 /* fall through */
158 case 32:
159 words = width;
161 return 4 * words;
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 )
183 unsigned int colors;
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
206 * Create an X image.
208 XImage *X11DRV_DIB_CreateXImage( int width, int height, int depth )
210 int width_bytes;
211 XImage *image;
213 wine_tsx11_lock();
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 );
218 wine_tsx11_unlock();
219 return image;
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;
240 *compr = 0;
241 *size = 0;
242 return 0;
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;
252 return 1;
254 ERR("(%d): unknown/wrong size for header\n", header->biSize );
255 return -1;
259 /***********************************************************************
260 * X11DRV_DIB_GetColorCount
262 * Computes the number of colors for the bitmap palette.
263 * Should not be called for a >8-bit deep bitmap.
265 static unsigned int X11DRV_DIB_GetColorCount(const BITMAPINFO *info)
267 unsigned int colors;
268 BOOL core_info = info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER);
270 if (core_info)
272 colors = 1 << ((const BITMAPCOREINFO*)info)->bmciHeader.bcBitCount;
274 else
276 colors = info->bmiHeader.biClrUsed;
277 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
279 if (colors > 256)
281 ERR("called with >256 colors!\n");
282 colors = 0;
284 return colors;
287 /***********************************************************************
288 * DIB_GetBitmapInfo
290 * Get the info from a bitmap header.
291 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
293 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
294 LONG *height, WORD *bpp, WORD *compr )
296 WORD planes;
297 DWORD size;
299 return DIB_GetBitmapInfoEx( header, width, height, &planes, bpp, compr, &size);
303 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
305 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
306 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
309 /***********************************************************************
310 * X11DRV_DIB_GenColorMap
312 * Fills the color map of a bitmap palette. Should not be called
313 * for a >8-bit deep bitmap.
315 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
316 WORD coloruse, WORD depth, BOOL quads,
317 const void *colorPtr, int start, int end )
319 int i;
321 if (coloruse == DIB_RGB_COLORS)
323 if (quads)
325 const RGBQUAD * rgb = (const RGBQUAD *)colorPtr;
327 if (depth == 1) /* Monochrome */
329 BOOL invert = FALSE;
330 RGBQUAD table[2];
332 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
333 invert = !colour_is_brighter(table[1], table[0]);
335 for (i = start; i < end; i++, rgb++)
336 colorMapping[i] = ((rgb->rgbRed + rgb->rgbGreen +
337 rgb->rgbBlue > 255*3/2 && !invert) ||
338 (rgb->rgbRed + rgb->rgbGreen +
339 rgb->rgbBlue <= 255*3/2 && invert));
341 else
342 for (i = start; i < end; i++, rgb++)
343 colorMapping[i] = X11DRV_PALETTE_ToPhysical( NULL, RGB(rgb->rgbRed,
344 rgb->rgbGreen,
345 rgb->rgbBlue));
347 else
349 const RGBTRIPLE * rgb = (const RGBTRIPLE *)colorPtr;
351 if (depth == 1) /* Monochrome */
353 BOOL invert = FALSE;
354 RGBQUAD table[2];
356 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
357 invert = !colour_is_brighter(table[1], table[0]);
359 for (i = start; i < end; i++, rgb++)
360 colorMapping[i] = ((rgb->rgbtRed + rgb->rgbtGreen +
361 rgb->rgbtBlue > 255*3/2 && !invert) ||
362 (rgb->rgbtRed + rgb->rgbtGreen +
363 rgb->rgbtBlue <= 255*3/2 && invert));
365 else
366 for (i = start; i < end; i++, rgb++)
367 colorMapping[i] = X11DRV_PALETTE_ToPhysical( NULL, RGB(rgb->rgbtRed,
368 rgb->rgbtGreen,
369 rgb->rgbtBlue));
372 else /* DIB_PAL_COLORS */
374 const WORD * index = (const WORD *)colorPtr;
376 for (i = start; i < end; i++, index++)
377 colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(*index) );
380 return colorMapping;
383 /***********************************************************************
384 * X11DRV_DIB_BuildColorMap
386 * Build the color map from the bitmap palette. Should not be called
387 * for a >8-bit deep bitmap.
389 static int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE *physDev, WORD coloruse, WORD depth,
390 const BITMAPINFO *info, int *nColors )
392 BOOL isInfo;
393 const void *colorPtr;
394 int *colorMapping;
397 *nColors = X11DRV_DIB_GetColorCount(info);
398 if (!*nColors) return NULL;
400 isInfo = info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER);
401 colorPtr = (const BYTE*)info + (WORD)info->bmiHeader.biSize;
402 if (!(colorMapping = HeapAlloc(GetProcessHeap(), 0, *nColors * sizeof(int) )))
403 return NULL;
405 return X11DRV_DIB_GenColorMap( physDev, colorMapping, coloruse, depth,
406 isInfo, colorPtr, 0, *nColors);
409 /***********************************************************************
410 * X11DRV_DIB_MapColor
412 static int X11DRV_DIB_MapColor( int *physMap, int nPhysMap, int phys, int oldcol )
414 unsigned int color;
416 if ((oldcol < nPhysMap) && (physMap[oldcol] == phys))
417 return oldcol;
419 for (color = 0; color < nPhysMap; color++)
420 if (physMap[color] == phys)
421 return color;
423 WARN("Strange color %08x\n", phys);
424 return 0;
428 /*********************************************************************
429 * X11DRV_DIB_GetNearestIndex
431 * Helper for X11DRV_DIB_GetDIBits.
432 * Returns the nearest colour table index for a given RGB.
433 * Nearest is defined by minimizing the sum of the squares.
435 static INT X11DRV_DIB_GetNearestIndex(RGBQUAD *colormap, int numColors, BYTE r, BYTE g, BYTE b)
437 INT i, best = -1, diff, bestdiff = -1;
438 RGBQUAD *color;
440 for(color = colormap, i = 0; i < numColors; color++, i++) {
441 diff = (r - color->rgbRed) * (r - color->rgbRed) +
442 (g - color->rgbGreen) * (g - color->rgbGreen) +
443 (b - color->rgbBlue) * (b - color->rgbBlue);
444 if(diff == 0)
445 return i;
446 if(best == -1 || diff < bestdiff) {
447 best = i;
448 bestdiff = diff;
451 return best;
453 /*********************************************************************
454 * X11DRV_DIB_MaskToShift
456 * Helper for X11DRV_DIB_GetDIBits.
457 * Returns the by how many bits to shift a given color so that it is
458 * in the proper position.
460 INT X11DRV_DIB_MaskToShift(DWORD mask)
462 int shift;
464 if (mask==0)
465 return 0;
467 shift=0;
468 while ((mask&1)==0) {
469 mask>>=1;
470 shift++;
472 return shift;
475 /***********************************************************************
476 * X11DRV_DIB_CheckMask
478 * Check RGB mask if it is either 0 or matches visual's mask.
480 static inline int X11DRV_DIB_CheckMask(int red_mask, int green_mask, int blue_mask)
482 return ( red_mask == 0 && green_mask == 0 && blue_mask == 0 ) ||
483 ( red_mask == visual->red_mask && green_mask == visual->green_mask &&
484 blue_mask == visual->blue_mask );
487 /***********************************************************************
488 * X11DRV_DIB_SetImageBits_1
490 * SetDIBits for a 1-bit deep DIB.
492 static void X11DRV_DIB_SetImageBits_1( int lines, const BYTE *srcbits,
493 DWORD srcwidth, DWORD dstwidth, int left,
494 int *colors, XImage *bmpImage, DWORD linebytes)
496 int h, width;
497 const BYTE* srcbyte;
498 BYTE srcval, extra;
499 DWORD i, x;
501 if (lines < 0 ) {
502 lines = -lines;
503 srcbits = srcbits + linebytes * (lines - 1);
504 linebytes = -linebytes;
507 if ((extra = (left & 7)) != 0) {
508 left &= ~7;
509 dstwidth += extra;
511 srcbits += left >> 3;
512 width = min(srcwidth, dstwidth);
514 /* ==== pal 1 dib -> any bmp format ==== */
515 for (h = lines-1; h >=0; h--) {
516 srcbyte=srcbits;
517 for (i = width/8, x = left; i > 0; i--) {
518 srcval=*srcbyte++;
519 XPutPixel( bmpImage, x++, h, colors[ srcval >> 7] );
520 XPutPixel( bmpImage, x++, h, colors[(srcval >> 6) & 1] );
521 XPutPixel( bmpImage, x++, h, colors[(srcval >> 5) & 1] );
522 XPutPixel( bmpImage, x++, h, colors[(srcval >> 4) & 1] );
523 XPutPixel( bmpImage, x++, h, colors[(srcval >> 3) & 1] );
524 XPutPixel( bmpImage, x++, h, colors[(srcval >> 2) & 1] );
525 XPutPixel( bmpImage, x++, h, colors[(srcval >> 1) & 1] );
526 XPutPixel( bmpImage, x++, h, colors[ srcval & 1] );
528 if (width % 8){
529 srcval=*srcbyte;
530 switch (width & 7)
532 case 7: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
533 case 6: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
534 case 5: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
535 case 4: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
536 case 3: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
537 case 2: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
538 case 1: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]);
541 srcbits += linebytes;
545 /***********************************************************************
546 * X11DRV_DIB_GetImageBits_1
548 * GetDIBits for a 1-bit deep DIB.
550 static void X11DRV_DIB_GetImageBits_1( int lines, BYTE *dstbits,
551 DWORD dstwidth, DWORD srcwidth,
552 RGBQUAD *colors, PALETTEENTRY *srccolors,
553 XImage *bmpImage, DWORD linebytes )
555 DWORD x;
556 int h, width = min(dstwidth, srcwidth);
558 if (lines < 0 ) {
559 lines = -lines;
560 dstbits = dstbits + linebytes * (lines - 1);
561 linebytes = -linebytes;
564 switch (bmpImage->depth)
566 case 1:
567 case 4:
568 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
569 && srccolors) {
570 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
571 BYTE* dstbyte;
573 for (h=lines-1; h>=0; h--) {
574 BYTE dstval;
575 dstbyte=dstbits;
576 dstval=0;
577 for (x=0; x<width; x++) {
578 PALETTEENTRY srcval;
579 srcval=srccolors[XGetPixel(bmpImage, x, h)];
580 dstval|=(X11DRV_DIB_GetNearestIndex
581 (colors, 2,
582 srcval.peRed,
583 srcval.peGreen,
584 srcval.peBlue) << (7 - (x & 7)));
585 if ((x&7)==7) {
586 *dstbyte++=dstval;
587 dstval=0;
590 if ((width&7)!=0) {
591 *dstbyte=dstval;
593 dstbits += linebytes;
595 } else {
596 goto notsupported;
598 break;
600 case 8:
601 if (X11DRV_DIB_CheckMask(bmpImage->red_mask, bmpImage->green_mask, bmpImage->blue_mask)
602 && srccolors) {
603 /* ==== pal 8 bmp -> pal 1 dib ==== */
604 const void* srcbits;
605 const BYTE* srcpixel;
606 BYTE* dstbyte;
608 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
610 for (h=0; h<lines; h++) {
611 BYTE dstval;
612 srcpixel=srcbits;
613 dstbyte=dstbits;
614 dstval=0;
615 for (x=0; x<width; x++) {
616 PALETTEENTRY srcval;
617 srcval=srccolors[*srcpixel++];
618 dstval|=(X11DRV_DIB_GetNearestIndex
619 (colors, 2,
620 srcval.peRed,
621 srcval.peGreen,
622 srcval.peBlue) << (7-(x&7)) );
623 if ((x&7)==7) {
624 *dstbyte++=dstval;
625 dstval=0;
628 if ((width&7)!=0) {
629 *dstbyte=dstval;
631 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
632 dstbits += linebytes;
634 } else {
635 goto notsupported;
637 break;
639 case 15:
640 case 16:
642 const void* srcbits;
643 const WORD* srcpixel;
644 BYTE* dstbyte;
646 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
648 if (bmpImage->green_mask==0x03e0) {
649 if (bmpImage->red_mask==0x7c00) {
650 /* ==== rgb 555 bmp -> pal 1 dib ==== */
651 for (h=0; h<lines; h++) {
652 BYTE dstval;
653 srcpixel=srcbits;
654 dstbyte=dstbits;
655 dstval=0;
656 for (x=0; x<width; x++) {
657 WORD srcval;
658 srcval=*srcpixel++;
659 dstval|=(X11DRV_DIB_GetNearestIndex
660 (colors, 2,
661 ((srcval >> 7) & 0xf8) | /* r */
662 ((srcval >> 12) & 0x07),
663 ((srcval >> 2) & 0xf8) | /* g */
664 ((srcval >> 7) & 0x07),
665 ((srcval << 3) & 0xf8) | /* b */
666 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
667 if ((x&7)==7) {
668 *dstbyte++=dstval;
669 dstval=0;
672 if ((width&7)!=0) {
673 *dstbyte=dstval;
675 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
676 dstbits += linebytes;
678 } else if (bmpImage->blue_mask==0x7c00) {
679 /* ==== bgr 555 bmp -> pal 1 dib ==== */
680 for (h=0; h<lines; h++) {
681 WORD dstval;
682 srcpixel=srcbits;
683 dstbyte=dstbits;
684 dstval=0;
685 for (x=0; x<width; x++) {
686 BYTE srcval;
687 srcval=*srcpixel++;
688 dstval|=(X11DRV_DIB_GetNearestIndex
689 (colors, 2,
690 ((srcval << 3) & 0xf8) | /* r */
691 ((srcval >> 2) & 0x07),
692 ((srcval >> 2) & 0xf8) | /* g */
693 ((srcval >> 7) & 0x07),
694 ((srcval >> 7) & 0xf8) | /* b */
695 ((srcval >> 12) & 0x07) ) << (7-(x&7)) );
696 if ((x&7)==7) {
697 *dstbyte++=dstval;
698 dstval=0;
701 if ((width&7)!=0) {
702 *dstbyte=dstval;
704 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
705 dstbits += linebytes;
707 } else {
708 goto notsupported;
710 } else if (bmpImage->green_mask==0x07e0) {
711 if (bmpImage->red_mask==0xf800) {
712 /* ==== rgb 565 bmp -> pal 1 dib ==== */
713 for (h=0; h<lines; h++) {
714 BYTE dstval;
715 srcpixel=srcbits;
716 dstbyte=dstbits;
717 dstval=0;
718 for (x=0; x<width; x++) {
719 WORD srcval;
720 srcval=*srcpixel++;
721 dstval|=(X11DRV_DIB_GetNearestIndex
722 (colors, 2,
723 ((srcval >> 8) & 0xf8) | /* r */
724 ((srcval >> 13) & 0x07),
725 ((srcval >> 3) & 0xfc) | /* g */
726 ((srcval >> 9) & 0x03),
727 ((srcval << 3) & 0xf8) | /* b */
728 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
729 if ((x&7)==7) {
730 *dstbyte++=dstval;
731 dstval=0;
734 if ((width&7)!=0) {
735 *dstbyte=dstval;
737 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
738 dstbits += linebytes;
740 } else if (bmpImage->blue_mask==0xf800) {
741 /* ==== bgr 565 bmp -> pal 1 dib ==== */
742 for (h=0; h<lines; h++) {
743 BYTE dstval;
744 srcpixel=srcbits;
745 dstbyte=dstbits;
746 dstval=0;
747 for (x=0; x<width; x++) {
748 WORD srcval;
749 srcval=*srcpixel++;
750 dstval|=(X11DRV_DIB_GetNearestIndex
751 (colors, 2,
752 ((srcval << 3) & 0xf8) | /* r */
753 ((srcval >> 2) & 0x07),
754 ((srcval >> 3) & 0xfc) | /* g */
755 ((srcval >> 9) & 0x03),
756 ((srcval >> 8) & 0xf8) | /* b */
757 ((srcval >> 13) & 0x07) ) << (7-(x&7)) );
758 if ((x&7)==7) {
759 *dstbyte++=dstval;
760 dstval=0;
763 if ((width&7)!=0) {
764 *dstbyte=dstval;
766 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
767 dstbits += linebytes;
769 } else {
770 goto notsupported;
772 } else {
773 goto notsupported;
776 break;
778 case 24:
779 case 32:
781 const void* srcbits;
782 const BYTE *srcbyte;
783 BYTE* dstbyte;
784 int bytes_per_pixel;
786 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
787 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
789 if (bmpImage->green_mask!=0x00ff00 ||
790 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
791 goto notsupported;
792 } else if (bmpImage->blue_mask==0xff) {
793 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
794 for (h=0; h<lines; h++) {
795 BYTE dstval;
796 srcbyte=srcbits;
797 dstbyte=dstbits;
798 dstval=0;
799 for (x=0; x<width; x++) {
800 dstval|=(X11DRV_DIB_GetNearestIndex
801 (colors, 2,
802 srcbyte[2],
803 srcbyte[1],
804 srcbyte[0]) << (7-(x&7)) );
805 srcbyte+=bytes_per_pixel;
806 if ((x&7)==7) {
807 *dstbyte++=dstval;
808 dstval=0;
811 if ((width&7)!=0) {
812 *dstbyte=dstval;
814 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
815 dstbits += linebytes;
817 } else {
818 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
819 for (h=0; h<lines; h++) {
820 BYTE dstval;
821 srcbyte=srcbits;
822 dstbyte=dstbits;
823 dstval=0;
824 for (x=0; x<width; x++) {
825 dstval|=(X11DRV_DIB_GetNearestIndex
826 (colors, 2,
827 srcbyte[0],
828 srcbyte[1],
829 srcbyte[2]) << (7-(x&7)) );
830 srcbyte+=bytes_per_pixel;
831 if ((x&7)==7) {
832 *dstbyte++=dstval;
833 dstval=0;
836 if ((width&7)!=0) {
837 *dstbyte=dstval;
839 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
840 dstbits += linebytes;
844 break;
846 default:
847 notsupported:
849 BYTE* dstbyte;
850 BYTE neg = 0;
851 unsigned long white = (1 << bmpImage->bits_per_pixel) - 1;
853 /* ==== any bmp format -> pal 1 dib ==== */
854 if ((unsigned)colors[0].rgbRed+colors[0].rgbGreen+colors[0].rgbBlue >=
855 (unsigned)colors[1].rgbRed+colors[1].rgbGreen+colors[1].rgbBlue )
856 neg = 1;
858 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB, "
859 "%s color mapping\n",
860 bmpImage->bits_per_pixel, bmpImage->red_mask,
861 bmpImage->green_mask, bmpImage->blue_mask,
862 neg?"negative":"direct" );
864 for (h=lines-1; h>=0; h--) {
865 BYTE dstval;
866 dstbyte=dstbits;
867 dstval=0;
868 for (x=0; x<width; x++) {
869 dstval|=((XGetPixel( bmpImage, x, h) >= white) ^ neg) << (7 - (x&7));
870 if ((x&7)==7) {
871 *dstbyte++=dstval;
872 dstval=0;
875 if ((width&7)!=0) {
876 *dstbyte=dstval;
878 dstbits += linebytes;
881 break;
885 /***********************************************************************
886 * X11DRV_DIB_SetImageBits_4
888 * SetDIBits for a 4-bit deep DIB.
890 static void X11DRV_DIB_SetImageBits_4( int lines, const BYTE *srcbits,
891 DWORD srcwidth, DWORD dstwidth, int left,
892 int *colors, XImage *bmpImage, DWORD linebytes)
894 int h, width;
895 const BYTE* srcbyte;
896 DWORD i, x;
898 if (lines < 0 ) {
899 lines = -lines;
900 srcbits = srcbits + linebytes * (lines - 1);
901 linebytes = -linebytes;
904 if (left & 1) {
905 left--;
906 dstwidth++;
908 srcbits += left >> 1;
909 width = min(srcwidth, dstwidth);
911 /* ==== pal 4 dib -> any bmp format ==== */
912 for (h = lines-1; h >= 0; h--) {
913 srcbyte=srcbits;
914 for (i = width/2, x = left; i > 0; i--) {
915 BYTE srcval=*srcbyte++;
916 XPutPixel( bmpImage, x++, h, colors[srcval >> 4] );
917 XPutPixel( bmpImage, x++, h, colors[srcval & 0x0f] );
919 if (width & 1)
920 XPutPixel( bmpImage, x, h, colors[*srcbyte >> 4] );
921 srcbits += linebytes;
927 /***********************************************************************
928 * X11DRV_DIB_GetImageBits_4
930 * GetDIBits for a 4-bit deep DIB.
932 static void X11DRV_DIB_GetImageBits_4( int lines, BYTE *dstbits,
933 DWORD srcwidth, DWORD dstwidth,
934 RGBQUAD *colors, PALETTEENTRY *srccolors,
935 XImage *bmpImage, DWORD linebytes )
937 DWORD x;
938 int h, width = min(srcwidth, dstwidth);
939 BYTE *bits;
941 if (lines < 0 )
943 lines = -lines;
944 dstbits = dstbits + ( linebytes * (lines-1) );
945 linebytes = -linebytes;
948 bits = dstbits;
950 switch (bmpImage->depth) {
951 case 1:
952 case 4:
953 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
954 && srccolors) {
955 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
956 BYTE* dstbyte;
958 for (h = lines-1; h >= 0; h--) {
959 BYTE dstval;
960 dstbyte=dstbits;
961 dstval=0;
962 for (x = 0; x < width; x++) {
963 PALETTEENTRY srcval;
964 srcval=srccolors[XGetPixel(bmpImage, x, h)];
965 dstval|=(X11DRV_DIB_GetNearestIndex
966 (colors, 16,
967 srcval.peRed,
968 srcval.peGreen,
969 srcval.peBlue) << (4-((x&1)<<2)));
970 if ((x&1)==1) {
971 *dstbyte++=dstval;
972 dstval=0;
975 if ((width&1)!=0) {
976 *dstbyte=dstval;
978 dstbits += linebytes;
980 } else {
981 goto notsupported;
983 break;
985 case 8:
986 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
987 && srccolors) {
988 /* ==== pal 8 bmp -> pal 4 dib ==== */
989 const void* srcbits;
990 const BYTE *srcpixel;
991 BYTE* dstbyte;
993 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
994 for (h=0; h<lines; h++) {
995 BYTE dstval;
996 srcpixel=srcbits;
997 dstbyte=dstbits;
998 dstval=0;
999 for (x=0; x<width; x++) {
1000 PALETTEENTRY srcval;
1001 srcval = srccolors[*srcpixel++];
1002 dstval|=(X11DRV_DIB_GetNearestIndex
1003 (colors, 16,
1004 srcval.peRed,
1005 srcval.peGreen,
1006 srcval.peBlue) << (4*(1-(x&1))) );
1007 if ((x&1)==1) {
1008 *dstbyte++=dstval;
1009 dstval=0;
1012 if ((width&1)!=0) {
1013 *dstbyte=dstval;
1015 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1016 dstbits += linebytes;
1018 } else {
1019 goto notsupported;
1021 break;
1023 case 15:
1024 case 16:
1026 const void* srcbits;
1027 const WORD* srcpixel;
1028 BYTE* dstbyte;
1030 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1032 if (bmpImage->green_mask==0x03e0) {
1033 if (bmpImage->red_mask==0x7c00) {
1034 /* ==== rgb 555 bmp -> pal 4 dib ==== */
1035 for (h=0; h<lines; h++) {
1036 BYTE dstval;
1037 srcpixel=srcbits;
1038 dstbyte=dstbits;
1039 dstval=0;
1040 for (x=0; x<width; x++) {
1041 WORD srcval;
1042 srcval=*srcpixel++;
1043 dstval|=(X11DRV_DIB_GetNearestIndex
1044 (colors, 16,
1045 ((srcval >> 7) & 0xf8) | /* r */
1046 ((srcval >> 12) & 0x07),
1047 ((srcval >> 2) & 0xf8) | /* g */
1048 ((srcval >> 7) & 0x07),
1049 ((srcval << 3) & 0xf8) | /* b */
1050 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1051 if ((x&1)==1) {
1052 *dstbyte++=dstval;
1053 dstval=0;
1056 if ((width&1)!=0) {
1057 *dstbyte=dstval;
1059 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1060 dstbits += linebytes;
1062 } else if (bmpImage->blue_mask==0x7c00) {
1063 /* ==== bgr 555 bmp -> pal 4 dib ==== */
1064 for (h=0; h<lines; h++) {
1065 WORD dstval;
1066 srcpixel=srcbits;
1067 dstbyte=dstbits;
1068 dstval=0;
1069 for (x=0; x<width; x++) {
1070 WORD srcval;
1071 srcval=*srcpixel++;
1072 dstval|=(X11DRV_DIB_GetNearestIndex
1073 (colors, 16,
1074 ((srcval << 3) & 0xf8) | /* r */
1075 ((srcval >> 2) & 0x07),
1076 ((srcval >> 2) & 0xf8) | /* g */
1077 ((srcval >> 7) & 0x07),
1078 ((srcval >> 7) & 0xf8) | /* b */
1079 ((srcval >> 12) & 0x07) ) << ((1-(x&1))<<2) );
1080 if ((x&1)==1) {
1081 *dstbyte++=dstval;
1082 dstval=0;
1085 if ((width&1)!=0) {
1086 *dstbyte=dstval;
1088 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1089 dstbits += linebytes;
1091 } else {
1092 goto notsupported;
1094 } else if (bmpImage->green_mask==0x07e0) {
1095 if (bmpImage->red_mask==0xf800) {
1096 /* ==== rgb 565 bmp -> pal 4 dib ==== */
1097 for (h=0; h<lines; h++) {
1098 BYTE dstval;
1099 srcpixel=srcbits;
1100 dstbyte=dstbits;
1101 dstval=0;
1102 for (x=0; x<width; x++) {
1103 WORD srcval;
1104 srcval=*srcpixel++;
1105 dstval|=(X11DRV_DIB_GetNearestIndex
1106 (colors, 16,
1107 ((srcval >> 8) & 0xf8) | /* r */
1108 ((srcval >> 13) & 0x07),
1109 ((srcval >> 3) & 0xfc) | /* g */
1110 ((srcval >> 9) & 0x03),
1111 ((srcval << 3) & 0xf8) | /* b */
1112 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1113 if ((x&1)==1) {
1114 *dstbyte++=dstval;
1115 dstval=0;
1118 if ((width&1)!=0) {
1119 *dstbyte=dstval;
1121 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1122 dstbits += linebytes;
1124 } else if (bmpImage->blue_mask==0xf800) {
1125 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1126 for (h=0; h<lines; h++) {
1127 WORD dstval;
1128 srcpixel=srcbits;
1129 dstbyte=dstbits;
1130 dstval=0;
1131 for (x=0; x<width; x++) {
1132 WORD srcval;
1133 srcval=*srcpixel++;
1134 dstval|=(X11DRV_DIB_GetNearestIndex
1135 (colors, 16,
1136 ((srcval << 3) & 0xf8) | /* r */
1137 ((srcval >> 2) & 0x07),
1138 ((srcval >> 3) & 0xfc) | /* g */
1139 ((srcval >> 9) & 0x03),
1140 ((srcval >> 8) & 0xf8) | /* b */
1141 ((srcval >> 13) & 0x07) ) << ((1-(x&1))<<2) );
1142 if ((x&1)==1) {
1143 *dstbyte++=dstval;
1144 dstval=0;
1147 if ((width&1)!=0) {
1148 *dstbyte=dstval;
1150 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1151 dstbits += linebytes;
1153 } else {
1154 goto notsupported;
1156 } else {
1157 goto notsupported;
1160 break;
1162 case 24:
1163 if (bmpImage->bits_per_pixel==24) {
1164 const void* srcbits;
1165 const BYTE *srcbyte;
1166 BYTE* dstbyte;
1168 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1170 if (bmpImage->green_mask!=0x00ff00 ||
1171 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1172 goto notsupported;
1173 } else if (bmpImage->blue_mask==0xff) {
1174 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1175 for (h=0; h<lines; h++) {
1176 srcbyte=srcbits;
1177 dstbyte=dstbits;
1178 for (x=0; x<width/2; x++) {
1179 /* Do 2 pixels at a time */
1180 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1181 (colors, 16,
1182 srcbyte[2],
1183 srcbyte[1],
1184 srcbyte[0]) << 4) |
1185 X11DRV_DIB_GetNearestIndex
1186 (colors, 16,
1187 srcbyte[5],
1188 srcbyte[4],
1189 srcbyte[3]);
1190 srcbyte+=6;
1192 if (width&1) {
1193 /* And then the odd pixel */
1194 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1195 (colors, 16,
1196 srcbyte[2],
1197 srcbyte[1],
1198 srcbyte[0]) << 4);
1200 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1201 dstbits += linebytes;
1203 } else {
1204 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1205 for (h=0; h<lines; h++) {
1206 srcbyte=srcbits;
1207 dstbyte=dstbits;
1208 for (x=0; x<width/2; x++) {
1209 /* Do 2 pixels at a time */
1210 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1211 (colors, 16,
1212 srcbyte[0],
1213 srcbyte[1],
1214 srcbyte[2]) << 4) |
1215 X11DRV_DIB_GetNearestIndex
1216 (colors, 16,
1217 srcbyte[3],
1218 srcbyte[4],
1219 srcbyte[5]);
1220 srcbyte+=6;
1222 if (width&1) {
1223 /* And then the odd pixel */
1224 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1225 (colors, 16,
1226 srcbyte[0],
1227 srcbyte[1],
1228 srcbyte[2]) << 4);
1230 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1231 dstbits += linebytes;
1234 break;
1236 /* Fall through */
1238 case 32:
1240 const void* srcbits;
1241 const BYTE *srcbyte;
1242 BYTE* dstbyte;
1244 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1246 if (bmpImage->green_mask!=0x00ff00 ||
1247 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1248 goto notsupported;
1249 } else if (bmpImage->blue_mask==0xff) {
1250 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1251 for (h=0; h<lines; h++) {
1252 srcbyte=srcbits;
1253 dstbyte=dstbits;
1254 for (x=0; x<width/2; x++) {
1255 /* Do 2 pixels at a time */
1256 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1257 (colors, 16,
1258 srcbyte[2],
1259 srcbyte[1],
1260 srcbyte[0]) << 4) |
1261 X11DRV_DIB_GetNearestIndex
1262 (colors, 16,
1263 srcbyte[6],
1264 srcbyte[5],
1265 srcbyte[4]);
1266 srcbyte+=8;
1268 if (width&1) {
1269 /* And then the odd pixel */
1270 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1271 (colors, 16,
1272 srcbyte[2],
1273 srcbyte[1],
1274 srcbyte[0]) << 4);
1276 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1277 dstbits += linebytes;
1279 } else {
1280 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1281 for (h=0; h<lines; h++) {
1282 srcbyte=srcbits;
1283 dstbyte=dstbits;
1284 for (x=0; x<width/2; x++) {
1285 /* Do 2 pixels at a time */
1286 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1287 (colors, 16,
1288 srcbyte[0],
1289 srcbyte[1],
1290 srcbyte[2]) << 4) |
1291 X11DRV_DIB_GetNearestIndex
1292 (colors, 16,
1293 srcbyte[4],
1294 srcbyte[5],
1295 srcbyte[6]);
1296 srcbyte+=8;
1298 if (width&1) {
1299 /* And then the odd pixel */
1300 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1301 (colors, 16,
1302 srcbyte[0],
1303 srcbyte[1],
1304 srcbyte[2]) << 4);
1306 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1307 dstbits += linebytes;
1311 break;
1313 default:
1314 notsupported:
1316 BYTE* dstbyte;
1318 /* ==== any bmp format -> pal 4 dib ==== */
1319 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1320 bmpImage->bits_per_pixel, bmpImage->red_mask,
1321 bmpImage->green_mask, bmpImage->blue_mask );
1322 for (h=lines-1; h>=0; h--) {
1323 dstbyte=dstbits;
1324 for (x=0; x<(width & ~1); x+=2) {
1325 *dstbyte++=(X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4) |
1326 X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x+1, h), 0);
1328 if (width & 1) {
1329 *dstbyte=(X11DRV_DIB_MapColor((int *)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4);
1331 dstbits += linebytes;
1334 break;
1338 /***********************************************************************
1339 * X11DRV_DIB_SetImageBits_RLE4
1341 * SetDIBits for a 4-bit deep compressed DIB.
1343 static void X11DRV_DIB_SetImageBits_RLE4( int lines, const BYTE *bits,
1344 DWORD srcwidth, DWORD dstwidth,
1345 int left, int *colors,
1346 XImage *bmpImage )
1348 unsigned int x = 0, width = min(srcwidth, dstwidth);
1349 int y = lines - 1, c, length;
1350 const BYTE *begin = bits;
1352 while (y >= 0)
1354 length = *bits++;
1355 if (length) { /* encoded */
1356 c = *bits++;
1357 while (length--) {
1358 if (x >= (left + width)) break;
1359 if( x >= left) XPutPixel(bmpImage, x, y, colors[c >> 4]);
1360 x++;
1361 if (!length--) break;
1362 if (x >= (left + width)) break;
1363 if( x >= left) XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1364 x++;
1366 } else {
1367 length = *bits++;
1368 switch (length)
1370 case RLE_EOL:
1371 x = 0;
1372 y--;
1373 break;
1375 case RLE_END:
1376 return;
1378 case RLE_DELTA:
1379 x += *bits++;
1380 y -= *bits++;
1381 break;
1383 default: /* absolute */
1384 while (length--) {
1385 c = *bits++;
1386 if (x >= left && x < (left + width))
1387 XPutPixel(bmpImage, x, y, colors[c >> 4]);
1388 x++;
1389 if (!length--) break;
1390 if (x >= left && x < (left + width))
1391 XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1392 x++;
1394 if ((bits - begin) & 1)
1395 bits++;
1403 /***********************************************************************
1404 * X11DRV_DIB_SetImageBits_8
1406 * SetDIBits for an 8-bit deep DIB.
1408 static void X11DRV_DIB_SetImageBits_8( int lines, const BYTE *srcbits,
1409 DWORD srcwidth, DWORD dstwidth, int left,
1410 const int *colors, XImage *bmpImage,
1411 DWORD linebytes )
1413 DWORD x;
1414 int h, width = min(srcwidth, dstwidth);
1415 const BYTE* srcbyte;
1416 BYTE* dstbits;
1418 if (lines < 0 )
1420 lines = -lines;
1421 srcbits = srcbits + linebytes * (lines-1);
1422 linebytes = -linebytes;
1424 srcbits += left;
1425 srcbyte = srcbits;
1427 switch (bmpImage->depth) {
1428 case 15:
1429 case 16:
1430 #if defined(__i386__) && defined(__GNUC__)
1431 /* Some X servers might have 32 bit/ 16bit deep pixel */
1432 if (lines && width && (bmpImage->bits_per_pixel == 16) &&
1433 (ImageByteOrder(gdi_display)==LSBFirst) )
1435 dstbits=(BYTE*)bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1436 /* FIXME: Does this really handle all these cases correctly? */
1437 /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1438 for (h = lines ; h--; ) {
1439 int _cl1,_cl2; /* temp outputs for asm below */
1440 /* Borrowed from DirectDraw */
1441 __asm__ __volatile__(
1442 "xor %%eax,%%eax\n"
1443 "cld\n"
1444 "1:\n"
1445 " lodsb\n"
1446 " movw (%%edx,%%eax,4),%%ax\n"
1447 " stosw\n"
1448 " xor %%eax,%%eax\n"
1449 " loop 1b\n"
1450 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1451 :"S" (srcbyte),
1452 "D" (dstbits),
1453 "c" (width),
1454 "d" (colors)
1455 :"eax", "cc", "memory"
1457 srcbyte = (srcbits += linebytes);
1458 dstbits -= bmpImage->bytes_per_line;
1460 return;
1462 break;
1463 #endif
1464 case 24:
1465 case 32:
1466 #if defined(__i386__) && defined(__GNUC__)
1467 if (lines && width && (bmpImage->bits_per_pixel == 32) &&
1468 (ImageByteOrder(gdi_display)==LSBFirst) )
1470 dstbits=(BYTE*)bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
1471 /* FIXME: Does this really handle both cases correctly? */
1472 /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1473 for (h = lines ; h--; ) {
1474 int _cl1,_cl2; /* temp outputs for asm below */
1475 /* Borrowed from DirectDraw */
1476 __asm__ __volatile__(
1477 "xor %%eax,%%eax\n"
1478 "cld\n"
1479 "1:\n"
1480 " lodsb\n"
1481 " movl (%%edx,%%eax,4),%%eax\n"
1482 " stosl\n"
1483 " xor %%eax,%%eax\n"
1484 " loop 1b\n"
1485 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1486 :"S" (srcbyte),
1487 "D" (dstbits),
1488 "c" (width),
1489 "d" (colors)
1490 :"eax", "cc", "memory"
1492 srcbyte = (srcbits += linebytes);
1493 dstbits -= bmpImage->bytes_per_line;
1495 return;
1497 break;
1498 #endif
1499 default:
1500 break; /* use slow generic case below */
1503 /* ==== pal 8 dib -> any bmp format ==== */
1504 for (h=lines-1; h>=0; h--) {
1505 for (x=left; x<width+left; x++) {
1506 XPutPixel(bmpImage, x, h, colors[*srcbyte++]);
1508 srcbyte = (srcbits += linebytes);
1512 /***********************************************************************
1513 * X11DRV_DIB_GetImageBits_8
1515 * GetDIBits for an 8-bit deep DIB.
1517 static void X11DRV_DIB_GetImageBits_8( int lines, BYTE *dstbits,
1518 DWORD srcwidth, DWORD dstwidth,
1519 RGBQUAD *colors, PALETTEENTRY *srccolors,
1520 XImage *bmpImage, DWORD linebytes )
1522 DWORD x;
1523 int h, width = min(srcwidth, dstwidth);
1524 BYTE* dstbyte;
1526 if (lines < 0 )
1528 lines = -lines;
1529 dstbits = dstbits + ( linebytes * (lines-1) );
1530 linebytes = -linebytes;
1534 * Hack for now
1535 * This condition is true when GetImageBits has been called by
1536 * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1537 * 256 colormaps, so we'll just use it for GetDIBits calls.
1538 * (In some cases, in an updateDIBSection, the returned colors are bad too)
1540 if (!srccolors) goto updatesection;
1542 switch (bmpImage->depth) {
1543 case 1:
1544 case 4:
1545 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1546 && srccolors) {
1548 /* ==== pal 1 bmp -> pal 8 dib ==== */
1549 /* ==== pal 4 bmp -> pal 8 dib ==== */
1550 for (h=lines-1; h>=0; h--) {
1551 dstbyte=dstbits;
1552 for (x=0; x<width; x++) {
1553 PALETTEENTRY srcval;
1554 srcval=srccolors[XGetPixel(bmpImage, x, h)];
1555 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1556 srcval.peRed,
1557 srcval.peGreen,
1558 srcval.peBlue);
1560 dstbits += linebytes;
1562 } else {
1563 goto notsupported;
1565 break;
1567 case 8:
1568 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1569 && srccolors) {
1570 /* ==== pal 8 bmp -> pal 8 dib ==== */
1571 const void* srcbits;
1572 const BYTE* srcpixel;
1574 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1575 for (h=0; h<lines; h++) {
1576 srcpixel=srcbits;
1577 dstbyte=dstbits;
1578 for (x = 0; x < width; x++) {
1579 PALETTEENTRY srcval;
1580 srcval=srccolors[*srcpixel++];
1581 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1582 srcval.peRed,
1583 srcval.peGreen,
1584 srcval.peBlue);
1586 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1587 dstbits += linebytes;
1589 } else {
1590 goto notsupported;
1592 break;
1594 case 15:
1595 case 16:
1597 const void* srcbits;
1598 const WORD* srcpixel;
1599 BYTE* dstbyte;
1601 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1603 if (bmpImage->green_mask==0x03e0) {
1604 if (bmpImage->red_mask==0x7c00) {
1605 /* ==== rgb 555 bmp -> pal 8 dib ==== */
1606 for (h=0; h<lines; h++) {
1607 srcpixel=srcbits;
1608 dstbyte=dstbits;
1609 for (x=0; x<width; x++) {
1610 WORD srcval;
1611 srcval=*srcpixel++;
1612 *dstbyte++=X11DRV_DIB_GetNearestIndex
1613 (colors, 256,
1614 ((srcval >> 7) & 0xf8) | /* r */
1615 ((srcval >> 12) & 0x07),
1616 ((srcval >> 2) & 0xf8) | /* g */
1617 ((srcval >> 7) & 0x07),
1618 ((srcval << 3) & 0xf8) | /* b */
1619 ((srcval >> 2) & 0x07) );
1621 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1622 dstbits += linebytes;
1624 } else if (bmpImage->blue_mask==0x7c00) {
1625 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1626 for (h=0; h<lines; h++) {
1627 srcpixel=srcbits;
1628 dstbyte=dstbits;
1629 for (x=0; x<width; x++) {
1630 WORD srcval;
1631 srcval=*srcpixel++;
1632 *dstbyte++=X11DRV_DIB_GetNearestIndex
1633 (colors, 256,
1634 ((srcval << 3) & 0xf8) | /* r */
1635 ((srcval >> 2) & 0x07),
1636 ((srcval >> 2) & 0xf8) | /* g */
1637 ((srcval >> 7) & 0x07),
1638 ((srcval >> 7) & 0xf8) | /* b */
1639 ((srcval >> 12) & 0x07) );
1641 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1642 dstbits += linebytes;
1644 } else {
1645 goto notsupported;
1647 } else if (bmpImage->green_mask==0x07e0) {
1648 if (bmpImage->red_mask==0xf800) {
1649 /* ==== rgb 565 bmp -> pal 8 dib ==== */
1650 for (h=0; h<lines; h++) {
1651 srcpixel=srcbits;
1652 dstbyte=dstbits;
1653 for (x=0; x<width; x++) {
1654 WORD srcval;
1655 srcval=*srcpixel++;
1656 *dstbyte++=X11DRV_DIB_GetNearestIndex
1657 (colors, 256,
1658 ((srcval >> 8) & 0xf8) | /* r */
1659 ((srcval >> 13) & 0x07),
1660 ((srcval >> 3) & 0xfc) | /* g */
1661 ((srcval >> 9) & 0x03),
1662 ((srcval << 3) & 0xf8) | /* b */
1663 ((srcval >> 2) & 0x07) );
1665 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1666 dstbits += linebytes;
1668 } else if (bmpImage->blue_mask==0xf800) {
1669 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1670 for (h=0; h<lines; h++) {
1671 srcpixel=srcbits;
1672 dstbyte=dstbits;
1673 for (x=0; x<width; x++) {
1674 WORD srcval;
1675 srcval=*srcpixel++;
1676 *dstbyte++=X11DRV_DIB_GetNearestIndex
1677 (colors, 256,
1678 ((srcval << 3) & 0xf8) | /* r */
1679 ((srcval >> 2) & 0x07),
1680 ((srcval >> 3) & 0xfc) | /* g */
1681 ((srcval >> 9) & 0x03),
1682 ((srcval >> 8) & 0xf8) | /* b */
1683 ((srcval >> 13) & 0x07) );
1685 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1686 dstbits += linebytes;
1688 } else {
1689 goto notsupported;
1691 } else {
1692 goto notsupported;
1695 break;
1697 case 24:
1698 case 32:
1700 const void* srcbits;
1701 const BYTE *srcbyte;
1702 BYTE* dstbyte;
1703 int bytes_per_pixel;
1705 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1706 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
1708 if (bmpImage->green_mask!=0x00ff00 ||
1709 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1710 goto notsupported;
1711 } else if (bmpImage->blue_mask==0xff) {
1712 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1713 for (h=0; h<lines; h++) {
1714 srcbyte=srcbits;
1715 dstbyte=dstbits;
1716 for (x=0; x<width; x++) {
1717 *dstbyte++=X11DRV_DIB_GetNearestIndex
1718 (colors, 256,
1719 srcbyte[2],
1720 srcbyte[1],
1721 srcbyte[0]);
1722 srcbyte+=bytes_per_pixel;
1724 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1725 dstbits += linebytes;
1727 } else {
1728 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1729 for (h=0; h<lines; h++) {
1730 srcbyte=srcbits;
1731 dstbyte=dstbits;
1732 for (x=0; x<width; x++) {
1733 *dstbyte++=X11DRV_DIB_GetNearestIndex
1734 (colors, 256,
1735 srcbyte[0],
1736 srcbyte[1],
1737 srcbyte[2]);
1738 srcbyte+=bytes_per_pixel;
1740 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1741 dstbits += linebytes;
1745 break;
1747 default:
1748 notsupported:
1749 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1750 bmpImage->depth, bmpImage->red_mask,
1751 bmpImage->green_mask, bmpImage->blue_mask );
1752 updatesection:
1753 /* ==== any bmp format -> pal 8 dib ==== */
1754 for (h=lines-1; h>=0; h--) {
1755 dstbyte=dstbits;
1756 for (x=0; x<width; x++) {
1757 *dstbyte=X11DRV_DIB_MapColor
1758 ((int*)colors, 256,
1759 XGetPixel(bmpImage, x, h), *dstbyte);
1760 dstbyte++;
1762 dstbits += linebytes;
1764 break;
1768 /***********************************************************************
1769 * X11DRV_DIB_SetImageBits_RLE8
1771 * SetDIBits for an 8-bit deep compressed DIB.
1773 * This function rewritten 941113 by James Youngman. WINE blew out when I
1774 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
1776 * This was because the algorithm assumed that all RLE8 bitmaps end with the
1777 * 'End of bitmap' escape code. This code is very much laxer in what it
1778 * allows to end the expansion. Possibly too lax. See the note by
1779 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
1780 * bitmap should end with RleEnd, but on the other hand, software exists
1781 * that produces ones that don't and Windows 3.1 doesn't complain a bit
1782 * about it.
1784 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
1785 * James A. Youngman <mbcstjy@afs.man.ac.uk>
1786 * [JAY]
1788 static void X11DRV_DIB_SetImageBits_RLE8( int lines, const BYTE *bits,
1789 DWORD srcwidth, DWORD dstwidth,
1790 int left, int *colors,
1791 XImage *bmpImage )
1793 unsigned int x; /* X-position on each line. Increases. */
1794 int y; /* Line #. Starts at lines-1, decreases */
1795 const BYTE *pIn = bits; /* Pointer to current position in bits */
1796 BYTE length; /* The length pf a run */
1797 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
1800 * Note that the bitmap data is stored by Windows starting at the
1801 * bottom line of the bitmap and going upwards. Within each line,
1802 * the data is stored left-to-right. That's the reason why line
1803 * goes from lines-1 to 0. [JAY]
1806 x = 0;
1807 y = lines - 1;
1808 while (y >= 0)
1810 length = *pIn++;
1813 * If the length byte is not zero (which is the escape value),
1814 * We have a run of length pixels all the same colour. The colour
1815 * index is stored next.
1817 * If the length byte is zero, we need to read the next byte to
1818 * know what to do. [JAY]
1820 if (length != 0)
1823 * [Run-Length] Encoded mode
1825 int color = colors[*pIn++];
1826 while (length-- && x < (left + dstwidth)) {
1827 if( x >= left) XPutPixel(bmpImage, x, y, color);
1828 x++;
1831 else
1834 * Escape codes (may be an absolute sequence though)
1836 escape_code = (*pIn++);
1837 switch(escape_code)
1839 case RLE_EOL:
1840 x = 0;
1841 y--;
1842 break;
1844 case RLE_END:
1845 /* Not all RLE8 bitmaps end with this code. For
1846 * example, Paint Shop Pro produces some that don't.
1847 * That's (I think) what caused the previous
1848 * implementation to fail. [JAY]
1850 return;
1852 case RLE_DELTA:
1853 x += (*pIn++);
1854 y -= (*pIn++);
1855 break;
1857 default: /* switch to absolute mode */
1858 length = escape_code;
1859 while (length--)
1861 int color = colors[*pIn++];
1862 if (x >= (left + dstwidth))
1864 pIn += length;
1865 break;
1867 if( x >= left) XPutPixel(bmpImage, x, y, color);
1868 x++;
1871 * If you think for a moment you'll realise that the
1872 * only time we could ever possibly read an odd
1873 * number of bytes is when there is a 0x00 (escape),
1874 * a value >0x02 (absolute mode) and then an odd-
1875 * length run. Therefore this is the only place we
1876 * need to worry about it. Everywhere else the
1877 * bytes are always read in pairs. [JAY]
1879 if (escape_code & 1) pIn++; /* Throw away the pad byte. */
1880 break;
1881 } /* switch (escape_code) : Escape sequence */
1887 /***********************************************************************
1888 * X11DRV_DIB_SetImageBits_16
1890 * SetDIBits for a 16-bit deep DIB.
1892 static void X11DRV_DIB_SetImageBits_16( int lines, const BYTE *srcbits,
1893 DWORD srcwidth, DWORD dstwidth, int left,
1894 X11DRV_PDEVICE *physDev, DWORD rSrc, DWORD gSrc, DWORD bSrc,
1895 XImage *bmpImage, DWORD linebytes )
1897 DWORD x;
1898 int h, width = min(srcwidth, dstwidth);
1899 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
1901 if (lines < 0 )
1903 lines = -lines;
1904 srcbits = srcbits + ( linebytes * (lines-1));
1905 linebytes = -linebytes;
1908 switch (bmpImage->depth)
1910 case 15:
1911 case 16:
1913 char* dstbits;
1915 srcbits=srcbits+left*2;
1916 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1918 if (bmpImage->green_mask==0x03e0) {
1919 if (gSrc==bmpImage->green_mask) {
1920 if (rSrc==bmpImage->red_mask) {
1921 /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1922 /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1923 convs->Convert_5x5_asis
1924 (width,lines,
1925 srcbits,linebytes,
1926 dstbits,-bmpImage->bytes_per_line);
1927 } else if (rSrc==bmpImage->blue_mask) {
1928 /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1929 /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1930 convs->Convert_555_reverse
1931 (width,lines,
1932 srcbits,linebytes,
1933 dstbits,-bmpImage->bytes_per_line);
1935 } else {
1936 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1937 /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1938 /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1939 convs->Convert_565_to_555_asis
1940 (width,lines,
1941 srcbits,linebytes,
1942 dstbits,-bmpImage->bytes_per_line);
1943 } else {
1944 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1945 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1946 convs->Convert_565_to_555_reverse
1947 (width,lines,
1948 srcbits,linebytes,
1949 dstbits,-bmpImage->bytes_per_line);
1952 } else if (bmpImage->green_mask==0x07e0) {
1953 if (gSrc==bmpImage->green_mask) {
1954 if (rSrc==bmpImage->red_mask) {
1955 /* ==== rgb 565 dib -> rgb 565 bmp ==== */
1956 /* ==== bgr 565 dib -> bgr 565 bmp ==== */
1957 convs->Convert_5x5_asis
1958 (width,lines,
1959 srcbits,linebytes,
1960 dstbits,-bmpImage->bytes_per_line);
1961 } else {
1962 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
1963 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
1964 convs->Convert_565_reverse
1965 (width,lines,
1966 srcbits,linebytes,
1967 dstbits,-bmpImage->bytes_per_line);
1969 } else {
1970 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1971 /* ==== rgb 555 dib -> rgb 565 bmp ==== */
1972 /* ==== bgr 555 dib -> bgr 565 bmp ==== */
1973 convs->Convert_555_to_565_asis
1974 (width,lines,
1975 srcbits,linebytes,
1976 dstbits,-bmpImage->bytes_per_line);
1977 } else {
1978 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
1979 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
1980 convs->Convert_555_to_565_reverse
1981 (width,lines,
1982 srcbits,linebytes,
1983 dstbits,-bmpImage->bytes_per_line);
1986 } else {
1987 goto notsupported;
1990 break;
1992 case 24:
1993 if (bmpImage->bits_per_pixel==24) {
1994 char* dstbits;
1996 srcbits=srcbits+left*2;
1997 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
1999 if (bmpImage->green_mask!=0x00ff00 ||
2000 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2001 goto notsupported;
2002 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2003 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2004 if (gSrc==0x03e0) {
2005 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
2006 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
2007 convs->Convert_555_to_888_asis
2008 (width,lines,
2009 srcbits,linebytes,
2010 dstbits,-bmpImage->bytes_per_line);
2011 } else {
2012 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
2013 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
2014 convs->Convert_565_to_888_asis
2015 (width,lines,
2016 srcbits,linebytes,
2017 dstbits,-bmpImage->bytes_per_line);
2019 } else {
2020 if (gSrc==0x03e0) {
2021 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
2022 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
2023 convs->Convert_555_to_888_reverse
2024 (width,lines,
2025 srcbits,linebytes,
2026 dstbits,-bmpImage->bytes_per_line);
2027 } else {
2028 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
2029 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
2030 convs->Convert_565_to_888_reverse
2031 (width,lines,
2032 srcbits,linebytes,
2033 dstbits,-bmpImage->bytes_per_line);
2036 break;
2038 /* Fall through */
2040 case 32:
2042 char* dstbits;
2044 srcbits=srcbits+left*2;
2045 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2047 if (bmpImage->green_mask!=0x00ff00 ||
2048 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2049 goto notsupported;
2050 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2051 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2052 if (gSrc==0x03e0) {
2053 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
2054 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
2055 convs->Convert_555_to_0888_asis
2056 (width,lines,
2057 srcbits,linebytes,
2058 dstbits,-bmpImage->bytes_per_line);
2059 } else {
2060 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
2061 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
2062 convs->Convert_565_to_0888_asis
2063 (width,lines,
2064 srcbits,linebytes,
2065 dstbits,-bmpImage->bytes_per_line);
2067 } else {
2068 if (gSrc==0x03e0) {
2069 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
2070 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
2071 convs->Convert_555_to_0888_reverse
2072 (width,lines,
2073 srcbits,linebytes,
2074 dstbits,-bmpImage->bytes_per_line);
2075 } else {
2076 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
2077 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
2078 convs->Convert_565_to_0888_reverse
2079 (width,lines,
2080 srcbits,linebytes,
2081 dstbits,-bmpImage->bytes_per_line);
2085 break;
2087 default:
2088 notsupported:
2089 WARN("from 16 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2090 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2091 bmpImage->green_mask, bmpImage->blue_mask );
2092 /* fall through */
2093 case 1:
2094 case 4:
2095 case 8:
2097 /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
2098 const WORD* srcpixel;
2099 int rShift1,gShift1,bShift1;
2100 int rShift2,gShift2,bShift2;
2101 BYTE gMask1,gMask2;
2103 /* Set color scaling values */
2104 rShift1=16+X11DRV_DIB_MaskToShift(rSrc)-3;
2105 gShift1=16+X11DRV_DIB_MaskToShift(gSrc)-3;
2106 bShift1=16+X11DRV_DIB_MaskToShift(bSrc)-3;
2107 rShift2=rShift1+5;
2108 gShift2=gShift1+5;
2109 bShift2=bShift1+5;
2110 if (gSrc==0x03e0) {
2111 /* Green has 5 bits, like the others */
2112 gMask1=0xf8;
2113 gMask2=0x07;
2114 } else {
2115 /* Green has 6 bits, not 5. Compensate. */
2116 gShift1++;
2117 gShift2+=2;
2118 gMask1=0xfc;
2119 gMask2=0x03;
2122 srcbits+=2*left;
2124 /* We could split it into four separate cases to optimize
2125 * but it is probably not worth it.
2127 for (h=lines-1; h>=0; h--) {
2128 srcpixel=(const WORD*)srcbits;
2129 for (x=left; x<width+left; x++) {
2130 DWORD srcval;
2131 BYTE red,green,blue;
2132 srcval=*srcpixel++ << 16;
2133 red= ((srcval >> rShift1) & 0xf8) |
2134 ((srcval >> rShift2) & 0x07);
2135 green=((srcval >> gShift1) & gMask1) |
2136 ((srcval >> gShift2) & gMask2);
2137 blue= ((srcval >> bShift1) & 0xf8) |
2138 ((srcval >> bShift2) & 0x07);
2139 XPutPixel(bmpImage, x, h,
2140 X11DRV_PALETTE_ToPhysical
2141 (physDev, RGB(red,green,blue)));
2143 srcbits += linebytes;
2146 break;
2151 /***********************************************************************
2152 * X11DRV_DIB_GetImageBits_16
2154 * GetDIBits for an 16-bit deep DIB.
2156 static void X11DRV_DIB_GetImageBits_16( int lines, BYTE *dstbits,
2157 DWORD dstwidth, DWORD srcwidth,
2158 PALETTEENTRY *srccolors,
2159 DWORD rDst, DWORD gDst, DWORD bDst,
2160 XImage *bmpImage, DWORD dibpitch )
2162 DWORD x;
2163 int h, width = min(srcwidth, dstwidth);
2164 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2166 DWORD linebytes = dibpitch;
2168 if (lines < 0 )
2170 lines = -lines;
2171 dstbits = dstbits + ( linebytes * (lines-1));
2172 linebytes = -linebytes;
2175 switch (bmpImage->depth)
2177 case 15:
2178 case 16:
2180 const char* srcbits;
2182 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2184 if (bmpImage->green_mask==0x03e0) {
2185 if (gDst==bmpImage->green_mask) {
2186 if (rDst==bmpImage->red_mask) {
2187 /* ==== rgb 555 bmp -> rgb 555 dib ==== */
2188 /* ==== bgr 555 bmp -> bgr 555 dib ==== */
2189 convs->Convert_5x5_asis
2190 (width,lines,
2191 srcbits,-bmpImage->bytes_per_line,
2192 dstbits,linebytes);
2193 } else {
2194 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
2195 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
2196 convs->Convert_555_reverse
2197 (width,lines,
2198 srcbits,-bmpImage->bytes_per_line,
2199 dstbits,linebytes);
2201 } else {
2202 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2203 /* ==== rgb 555 bmp -> rgb 565 dib ==== */
2204 /* ==== bgr 555 bmp -> bgr 565 dib ==== */
2205 convs->Convert_555_to_565_asis
2206 (width,lines,
2207 srcbits,-bmpImage->bytes_per_line,
2208 dstbits,linebytes);
2209 } else {
2210 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
2211 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
2212 convs->Convert_555_to_565_reverse
2213 (width,lines,
2214 srcbits,-bmpImage->bytes_per_line,
2215 dstbits,linebytes);
2218 } else if (bmpImage->green_mask==0x07e0) {
2219 if (gDst==bmpImage->green_mask) {
2220 if (rDst == bmpImage->red_mask) {
2221 /* ==== rgb 565 bmp -> rgb 565 dib ==== */
2222 /* ==== bgr 565 bmp -> bgr 565 dib ==== */
2223 convs->Convert_5x5_asis
2224 (width,lines,
2225 srcbits,-bmpImage->bytes_per_line,
2226 dstbits,linebytes);
2227 } else {
2228 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
2229 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
2230 convs->Convert_565_reverse
2231 (width,lines,
2232 srcbits,-bmpImage->bytes_per_line,
2233 dstbits,linebytes);
2235 } else {
2236 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2237 /* ==== rgb 565 bmp -> rgb 555 dib ==== */
2238 /* ==== bgr 565 bmp -> bgr 555 dib ==== */
2239 convs->Convert_565_to_555_asis
2240 (width,lines,
2241 srcbits,-bmpImage->bytes_per_line,
2242 dstbits,linebytes);
2243 } else {
2244 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
2245 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
2246 convs->Convert_565_to_555_reverse
2247 (width,lines,
2248 srcbits,-bmpImage->bytes_per_line,
2249 dstbits,linebytes);
2252 } else {
2253 goto notsupported;
2256 break;
2258 case 24:
2259 if (bmpImage->bits_per_pixel == 24) {
2260 const char* srcbits;
2262 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2264 if (bmpImage->green_mask!=0x00ff00 ||
2265 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2266 goto notsupported;
2267 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2268 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2269 if (gDst==0x03e0) {
2270 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
2271 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
2272 convs->Convert_888_to_555_asis
2273 (width,lines,
2274 srcbits,-bmpImage->bytes_per_line,
2275 dstbits,linebytes);
2276 } else {
2277 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2278 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2279 convs->Convert_888_to_565_asis
2280 (width,lines,
2281 srcbits,-bmpImage->bytes_per_line,
2282 dstbits,linebytes);
2284 } else {
2285 if (gDst==0x03e0) {
2286 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
2287 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
2288 convs->Convert_888_to_555_reverse
2289 (width,lines,
2290 srcbits,-bmpImage->bytes_per_line,
2291 dstbits,linebytes);
2292 } else {
2293 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
2294 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
2295 convs->Convert_888_to_565_reverse
2296 (width,lines,
2297 srcbits,-bmpImage->bytes_per_line,
2298 dstbits,linebytes);
2301 break;
2303 /* Fall through */
2305 case 32:
2307 const char* srcbits;
2309 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2311 if (bmpImage->green_mask!=0x00ff00 ||
2312 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2313 goto notsupported;
2314 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2315 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2316 if (gDst==0x03e0) {
2317 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2318 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2319 convs->Convert_0888_to_555_asis
2320 (width,lines,
2321 srcbits,-bmpImage->bytes_per_line,
2322 dstbits,linebytes);
2323 } else {
2324 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2325 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2326 convs->Convert_0888_to_565_asis
2327 (width,lines,
2328 srcbits,-bmpImage->bytes_per_line,
2329 dstbits,linebytes);
2331 } else {
2332 if (gDst==0x03e0) {
2333 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2334 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2335 convs->Convert_0888_to_555_reverse
2336 (width,lines,
2337 srcbits,-bmpImage->bytes_per_line,
2338 dstbits,linebytes);
2339 } else {
2340 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2341 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2342 convs->Convert_0888_to_565_reverse
2343 (width,lines,
2344 srcbits,-bmpImage->bytes_per_line,
2345 dstbits,linebytes);
2349 break;
2351 case 1:
2352 case 4:
2353 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2354 && srccolors) {
2355 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2356 int rShift,gShift,bShift;
2357 WORD* dstpixel;
2359 /* Shift everything 16 bits left so that all shifts are >0,
2360 * even for BGR DIBs. Then a single >> 16 will bring everything
2361 * back into place.
2363 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2364 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2365 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2366 if (gDst==0x07e0) {
2367 /* 6 bits for the green */
2368 gShift++;
2370 rDst=rDst << 16;
2371 gDst=gDst << 16;
2372 bDst=bDst << 16;
2373 for (h = lines - 1; h >= 0; h--) {
2374 dstpixel=(LPWORD)dstbits;
2375 for (x = 0; x < width; x++) {
2376 PALETTEENTRY srcval;
2377 DWORD dstval;
2378 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2379 dstval=((srcval.peRed << rShift) & rDst) |
2380 ((srcval.peGreen << gShift) & gDst) |
2381 ((srcval.peBlue << bShift) & bDst);
2382 *dstpixel++=dstval >> 16;
2384 dstbits += linebytes;
2386 } else {
2387 goto notsupported;
2389 break;
2391 case 8:
2392 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2393 && srccolors) {
2394 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2395 int rShift,gShift,bShift;
2396 const BYTE* srcbits;
2397 const BYTE* srcpixel;
2398 WORD* dstpixel;
2400 /* Shift everything 16 bits left so that all shifts are >0,
2401 * even for BGR DIBs. Then a single >> 16 will bring everything
2402 * back into place.
2404 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2405 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2406 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2407 if (gDst==0x07e0) {
2408 /* 6 bits for the green */
2409 gShift++;
2411 rDst=rDst << 16;
2412 gDst=gDst << 16;
2413 bDst=bDst << 16;
2414 srcbits=(BYTE*)bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2415 for (h=0; h<lines; h++) {
2416 srcpixel=srcbits;
2417 dstpixel=(LPWORD)dstbits;
2418 for (x = 0; x < width; x++) {
2419 PALETTEENTRY srcval;
2420 DWORD dstval;
2421 srcval=srccolors[*srcpixel++];
2422 dstval=((srcval.peRed << rShift) & rDst) |
2423 ((srcval.peGreen << gShift) & gDst) |
2424 ((srcval.peBlue << bShift) & bDst);
2425 *dstpixel++=dstval >> 16;
2427 srcbits -= bmpImage->bytes_per_line;
2428 dstbits += linebytes;
2430 } else {
2431 goto notsupported;
2433 break;
2435 default:
2436 notsupported:
2438 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2439 int rShift,gShift,bShift;
2440 WORD* dstpixel;
2442 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%x,%x,%x)\n",
2443 bmpImage->depth, bmpImage->red_mask,
2444 bmpImage->green_mask, bmpImage->blue_mask,
2445 rDst, gDst, bDst);
2447 /* Shift everything 16 bits left so that all shifts are >0,
2448 * even for BGR DIBs. Then a single >> 16 will bring everything
2449 * back into place.
2451 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2452 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2453 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2454 if (gDst==0x07e0) {
2455 /* 6 bits for the green */
2456 gShift++;
2458 rDst=rDst << 16;
2459 gDst=gDst << 16;
2460 bDst=bDst << 16;
2461 for (h = lines - 1; h >= 0; h--) {
2462 dstpixel=(LPWORD)dstbits;
2463 for (x = 0; x < width; x++) {
2464 COLORREF srcval;
2465 DWORD dstval;
2466 srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
2467 dstval=((GetRValue(srcval) << rShift) & rDst) |
2468 ((GetGValue(srcval) << gShift) & gDst) |
2469 ((GetBValue(srcval) << bShift) & bDst);
2470 *dstpixel++=dstval >> 16;
2472 dstbits += linebytes;
2475 break;
2480 /***********************************************************************
2481 * X11DRV_DIB_SetImageBits_24
2483 * SetDIBits for a 24-bit deep DIB.
2485 static void X11DRV_DIB_SetImageBits_24( int lines, const BYTE *srcbits,
2486 DWORD srcwidth, DWORD dstwidth, int left,
2487 X11DRV_PDEVICE *physDev,
2488 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2489 XImage *bmpImage, DWORD linebytes )
2491 DWORD x;
2492 int h, width = min(srcwidth, dstwidth);
2493 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2495 if (lines < 0 )
2497 lines = -lines;
2498 srcbits = srcbits + linebytes * (lines - 1);
2499 linebytes = -linebytes;
2502 switch (bmpImage->depth)
2504 case 24:
2505 if (bmpImage->bits_per_pixel==24) {
2506 char* dstbits;
2508 srcbits=srcbits+left*3;
2509 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2511 if (bmpImage->green_mask!=0x00ff00 ||
2512 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2513 goto notsupported;
2514 } else if (rSrc==bmpImage->red_mask) {
2515 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2516 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2517 convs->Convert_888_asis
2518 (width,lines,
2519 srcbits,linebytes,
2520 dstbits,-bmpImage->bytes_per_line);
2521 } else {
2522 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2523 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2524 convs->Convert_888_reverse
2525 (width,lines,
2526 srcbits,linebytes,
2527 dstbits,-bmpImage->bytes_per_line);
2529 break;
2531 /* fall through */
2533 case 32:
2535 char* dstbits;
2537 srcbits=srcbits+left*3;
2538 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2540 if (bmpImage->green_mask!=0x00ff00 ||
2541 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2542 goto notsupported;
2543 } else if (rSrc==bmpImage->red_mask) {
2544 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2545 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2546 convs->Convert_888_to_0888_asis
2547 (width,lines,
2548 srcbits,linebytes,
2549 dstbits,-bmpImage->bytes_per_line);
2550 } else {
2551 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2552 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2553 convs->Convert_888_to_0888_reverse
2554 (width,lines,
2555 srcbits,linebytes,
2556 dstbits,-bmpImage->bytes_per_line);
2558 break;
2561 case 15:
2562 case 16:
2564 char* dstbits;
2566 srcbits=srcbits+left*3;
2567 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
2569 if (bmpImage->green_mask==0x03e0) {
2570 if ((rSrc==0xff0000 && bmpImage->red_mask==0x7f00) ||
2571 (bSrc==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2572 /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2573 /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2574 convs->Convert_888_to_555_asis
2575 (width,lines,
2576 srcbits,linebytes,
2577 dstbits,-bmpImage->bytes_per_line);
2578 } else if ((rSrc==0xff && bmpImage->red_mask==0x7f00) ||
2579 (bSrc==0xff && bmpImage->blue_mask==0x7f00)) {
2580 /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2581 /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2582 convs->Convert_888_to_555_reverse
2583 (width,lines,
2584 srcbits,linebytes,
2585 dstbits,-bmpImage->bytes_per_line);
2586 } else {
2587 goto notsupported;
2589 } else if (bmpImage->green_mask==0x07e0) {
2590 if ((rSrc==0xff0000 && bmpImage->red_mask==0xf800) ||
2591 (bSrc==0xff0000 && bmpImage->blue_mask==0xf800)) {
2592 /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2593 /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2594 convs->Convert_888_to_565_asis
2595 (width,lines,
2596 srcbits,linebytes,
2597 dstbits,-bmpImage->bytes_per_line);
2598 } else if ((rSrc==0xff && bmpImage->red_mask==0xf800) ||
2599 (bSrc==0xff && bmpImage->blue_mask==0xf800)) {
2600 /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2601 /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2602 convs->Convert_888_to_565_reverse
2603 (width,lines,
2604 srcbits,linebytes,
2605 dstbits,-bmpImage->bytes_per_line);
2606 } else {
2607 goto notsupported;
2609 } else {
2610 goto notsupported;
2613 break;
2615 default:
2616 notsupported:
2617 WARN("from 24 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2618 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2619 bmpImage->green_mask, bmpImage->blue_mask );
2620 /* fall through */
2621 case 1:
2622 case 4:
2623 case 8:
2625 /* ==== rgb 888 dib -> any bmp bormat ==== */
2626 const BYTE* srcbyte;
2628 /* Windows only supports one 24bpp DIB format: RGB888 */
2629 srcbits+=left*3;
2630 for (h = lines - 1; h >= 0; h--) {
2631 srcbyte=(const BYTE*)srcbits;
2632 for (x = left; x < width+left; x++) {
2633 XPutPixel(bmpImage, x, h,
2634 X11DRV_PALETTE_ToPhysical
2635 (physDev, RGB(srcbyte[2], srcbyte[1], srcbyte[0])));
2636 srcbyte+=3;
2638 srcbits += linebytes;
2641 break;
2646 /***********************************************************************
2647 * X11DRV_DIB_GetImageBits_24
2649 * GetDIBits for an 24-bit deep DIB.
2651 static void X11DRV_DIB_GetImageBits_24( int lines, BYTE *dstbits,
2652 DWORD dstwidth, DWORD srcwidth,
2653 PALETTEENTRY *srccolors,
2654 DWORD rDst, DWORD gDst, DWORD bDst,
2655 XImage *bmpImage, DWORD linebytes )
2657 DWORD x;
2658 int h, width = min(srcwidth, dstwidth);
2659 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2661 if (lines < 0 )
2663 lines = -lines;
2664 dstbits = dstbits + ( linebytes * (lines-1) );
2665 linebytes = -linebytes;
2668 switch (bmpImage->depth)
2670 case 24:
2671 if (bmpImage->bits_per_pixel==24) {
2672 const char* srcbits;
2674 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2676 if (bmpImage->green_mask!=0x00ff00 ||
2677 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2678 goto notsupported;
2679 } else if (rDst==bmpImage->red_mask) {
2680 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2681 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2682 convs->Convert_888_asis
2683 (width,lines,
2684 srcbits,-bmpImage->bytes_per_line,
2685 dstbits,linebytes);
2686 } else {
2687 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2688 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2689 convs->Convert_888_reverse
2690 (width,lines,
2691 srcbits,-bmpImage->bytes_per_line,
2692 dstbits,linebytes);
2694 break;
2696 /* fall through */
2698 case 32:
2700 const char* srcbits;
2702 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2704 if (bmpImage->green_mask!=0x00ff00 ||
2705 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2706 goto notsupported;
2707 } else if (rDst==bmpImage->red_mask) {
2708 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2709 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2710 convs->Convert_0888_to_888_asis
2711 (width,lines,
2712 srcbits,-bmpImage->bytes_per_line,
2713 dstbits,linebytes);
2714 } else {
2715 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2716 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2717 convs->Convert_0888_to_888_reverse
2718 (width,lines,
2719 srcbits,-bmpImage->bytes_per_line,
2720 dstbits,linebytes);
2722 break;
2725 case 15:
2726 case 16:
2728 const char* srcbits;
2730 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2732 if (bmpImage->green_mask==0x03e0) {
2733 if ((rDst==0xff0000 && bmpImage->red_mask==0x7f00) ||
2734 (bDst==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2735 /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2736 /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2737 convs->Convert_555_to_888_asis
2738 (width,lines,
2739 srcbits,-bmpImage->bytes_per_line,
2740 dstbits,linebytes);
2741 } else if ((rDst==0xff && bmpImage->red_mask==0x7f00) ||
2742 (bDst==0xff && bmpImage->blue_mask==0x7f00)) {
2743 /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2744 /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2745 convs->Convert_555_to_888_reverse
2746 (width,lines,
2747 srcbits,-bmpImage->bytes_per_line,
2748 dstbits,linebytes);
2749 } else {
2750 goto notsupported;
2752 } else if (bmpImage->green_mask==0x07e0) {
2753 if ((rDst==0xff0000 && bmpImage->red_mask==0xf800) ||
2754 (bDst==0xff0000 && bmpImage->blue_mask==0xf800)) {
2755 /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2756 /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2757 convs->Convert_565_to_888_asis
2758 (width,lines,
2759 srcbits,-bmpImage->bytes_per_line,
2760 dstbits,linebytes);
2761 } else if ((rDst==0xff && bmpImage->red_mask==0xf800) ||
2762 (bDst==0xff && bmpImage->blue_mask==0xf800)) {
2763 /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2764 /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2765 convs->Convert_565_to_888_reverse
2766 (width,lines,
2767 srcbits,-bmpImage->bytes_per_line,
2768 dstbits,linebytes);
2769 } else {
2770 goto notsupported;
2772 } else {
2773 goto notsupported;
2776 break;
2778 case 1:
2779 case 4:
2780 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2781 && srccolors) {
2782 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2783 BYTE* dstbyte;
2785 /* Windows only supports one 24bpp DIB format: rgb 888 */
2786 for (h = lines - 1; h >= 0; h--) {
2787 dstbyte=dstbits;
2788 for (x = 0; x < width; x++) {
2789 PALETTEENTRY srcval;
2790 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2791 dstbyte[0]=srcval.peBlue;
2792 dstbyte[1]=srcval.peGreen;
2793 dstbyte[2]=srcval.peRed;
2794 dstbyte+=3;
2796 dstbits += linebytes;
2798 } else {
2799 goto notsupported;
2801 break;
2803 case 8:
2804 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2805 && srccolors) {
2806 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2807 const void* srcbits;
2808 const BYTE* srcpixel;
2809 BYTE* dstbyte;
2811 /* Windows only supports one 24bpp DIB format: rgb 888 */
2812 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2813 for (h = lines - 1; h >= 0; h--) {
2814 srcpixel=srcbits;
2815 dstbyte=dstbits;
2816 for (x = 0; x < width; x++ ) {
2817 PALETTEENTRY srcval;
2818 srcval=srccolors[*srcpixel++];
2819 dstbyte[0]=srcval.peBlue;
2820 dstbyte[1]=srcval.peGreen;
2821 dstbyte[2]=srcval.peRed;
2822 dstbyte+=3;
2824 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
2825 dstbits += linebytes;
2827 } else {
2828 goto notsupported;
2830 break;
2832 default:
2833 notsupported:
2835 /* ==== any bmp format -> 888 dib ==== */
2836 BYTE* dstbyte;
2838 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%x,%x,%x)\n",
2839 bmpImage->depth, bmpImage->red_mask,
2840 bmpImage->green_mask, bmpImage->blue_mask,
2841 rDst, gDst, bDst );
2843 /* Windows only supports one 24bpp DIB format: rgb 888 */
2844 for (h = lines - 1; h >= 0; h--) {
2845 dstbyte=dstbits;
2846 for (x = 0; x < width; x++) {
2847 COLORREF srcval=X11DRV_PALETTE_ToLogical
2848 (XGetPixel( bmpImage, x, h ));
2849 dstbyte[0]=GetBValue(srcval);
2850 dstbyte[1]=GetGValue(srcval);
2851 dstbyte[2]=GetRValue(srcval);
2852 dstbyte+=3;
2854 dstbits += linebytes;
2857 break;
2862 /***********************************************************************
2863 * X11DRV_DIB_SetImageBits_32
2865 * SetDIBits for a 32-bit deep DIB.
2867 static void X11DRV_DIB_SetImageBits_32(int lines, const BYTE *srcbits,
2868 DWORD srcwidth, DWORD dstwidth, int left,
2869 X11DRV_PDEVICE *physDev,
2870 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2871 XImage *bmpImage,
2872 DWORD linebytes)
2874 DWORD x;
2875 const DWORD *ptr;
2876 int h, width = min(srcwidth, dstwidth);
2877 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2879 if (lines < 0 )
2881 lines = -lines;
2882 srcbits = srcbits + ( linebytes * (lines-1) );
2883 linebytes = -linebytes;
2886 ptr = (const DWORD *) srcbits + left;
2888 switch (bmpImage->depth)
2890 case 24:
2891 if (bmpImage->bits_per_pixel==24) {
2892 char* dstbits;
2894 srcbits=srcbits+left*4;
2895 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2897 if (rSrc==bmpImage->red_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->blue_mask) {
2898 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2899 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2900 convs->Convert_0888_to_888_asis
2901 (width,lines,
2902 srcbits,linebytes,
2903 dstbits,-bmpImage->bytes_per_line);
2904 } else if (bmpImage->green_mask!=0x00ff00 ||
2905 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2906 goto notsupported;
2907 /* the tests below assume sane bmpImage masks */
2908 } else if (rSrc==bmpImage->blue_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->red_mask) {
2909 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2910 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2911 convs->Convert_0888_to_888_reverse
2912 (width,lines,
2913 srcbits,linebytes,
2914 dstbits,-bmpImage->bytes_per_line);
2915 } else if (bmpImage->blue_mask==0xff) {
2916 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2917 convs->Convert_any0888_to_rgb888
2918 (width,lines,
2919 srcbits,linebytes,
2920 rSrc,gSrc,bSrc,
2921 dstbits,-bmpImage->bytes_per_line);
2922 } else {
2923 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2924 convs->Convert_any0888_to_bgr888
2925 (width,lines,
2926 srcbits,linebytes,
2927 rSrc,gSrc,bSrc,
2928 dstbits,-bmpImage->bytes_per_line);
2930 break;
2932 /* fall through */
2934 case 32:
2936 char* dstbits;
2938 srcbits=srcbits+left*4;
2939 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2941 if (gSrc==bmpImage->green_mask) {
2942 if (rSrc==bmpImage->red_mask && bSrc==bmpImage->blue_mask) {
2943 /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2944 /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2945 convs->Convert_0888_asis
2946 (width,lines,
2947 srcbits,linebytes,
2948 dstbits,-bmpImage->bytes_per_line);
2949 } else if (bmpImage->green_mask!=0x00ff00 ||
2950 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2951 goto notsupported;
2952 /* the tests below assume sane bmpImage masks */
2953 } else if (rSrc==bmpImage->blue_mask && bSrc==bmpImage->red_mask) {
2954 /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2955 /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2956 convs->Convert_0888_reverse
2957 (width,lines,
2958 srcbits,linebytes,
2959 dstbits,-bmpImage->bytes_per_line);
2960 } else {
2961 /* ==== any 0888 dib -> any 0888 bmp ==== */
2962 convs->Convert_0888_any
2963 (width,lines,
2964 srcbits,linebytes,
2965 rSrc,gSrc,bSrc,
2966 dstbits,-bmpImage->bytes_per_line,
2967 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
2969 } else if (bmpImage->green_mask!=0x00ff00 ||
2970 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2971 goto notsupported;
2972 /* the tests below assume sane bmpImage masks */
2973 } else {
2974 /* ==== any 0888 dib -> any 0888 bmp ==== */
2975 convs->Convert_0888_any
2976 (width,lines,
2977 srcbits,linebytes,
2978 rSrc,gSrc,bSrc,
2979 dstbits,-bmpImage->bytes_per_line,
2980 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
2983 break;
2985 case 15:
2986 case 16:
2988 char* dstbits;
2990 srcbits=srcbits+left*4;
2991 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
2993 if (rSrc==0xff0000 && gSrc==0x00ff00 && bSrc==0x0000ff) {
2994 if (bmpImage->green_mask==0x03e0) {
2995 if (bmpImage->red_mask==0x7f00) {
2996 /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
2997 convs->Convert_0888_to_555_asis
2998 (width,lines,
2999 srcbits,linebytes,
3000 dstbits,-bmpImage->bytes_per_line);
3001 } else if (bmpImage->blue_mask==0x7f00) {
3002 /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
3003 convs->Convert_0888_to_555_reverse
3004 (width,lines,
3005 srcbits,linebytes,
3006 dstbits,-bmpImage->bytes_per_line);
3007 } else {
3008 goto notsupported;
3010 } else if (bmpImage->green_mask==0x07e0) {
3011 if (bmpImage->red_mask==0xf800) {
3012 /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
3013 convs->Convert_0888_to_565_asis
3014 (width,lines,
3015 srcbits,linebytes,
3016 dstbits,-bmpImage->bytes_per_line);
3017 } else if (bmpImage->blue_mask==0xf800) {
3018 /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
3019 convs->Convert_0888_to_565_reverse
3020 (width,lines,
3021 srcbits,linebytes,
3022 dstbits,-bmpImage->bytes_per_line);
3023 } else {
3024 goto notsupported;
3026 } else {
3027 goto notsupported;
3029 } else if (rSrc==0x0000ff && gSrc==0x00ff00 && bSrc==0xff0000) {
3030 if (bmpImage->green_mask==0x03e0) {
3031 if (bmpImage->blue_mask==0x7f00) {
3032 /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
3033 convs->Convert_0888_to_555_asis
3034 (width,lines,
3035 srcbits,linebytes,
3036 dstbits,-bmpImage->bytes_per_line);
3037 } else if (bmpImage->red_mask==0x7f00) {
3038 /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
3039 convs->Convert_0888_to_555_reverse
3040 (width,lines,
3041 srcbits,linebytes,
3042 dstbits,-bmpImage->bytes_per_line);
3043 } else {
3044 goto notsupported;
3046 } else if (bmpImage->green_mask==0x07e0) {
3047 if (bmpImage->blue_mask==0xf800) {
3048 /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
3049 convs->Convert_0888_to_565_asis
3050 (width,lines,
3051 srcbits,linebytes,
3052 dstbits,-bmpImage->bytes_per_line);
3053 } else if (bmpImage->red_mask==0xf800) {
3054 /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
3055 convs->Convert_0888_to_565_reverse
3056 (width,lines,
3057 srcbits,linebytes,
3058 dstbits,-bmpImage->bytes_per_line);
3059 } else {
3060 goto notsupported;
3062 } else {
3063 goto notsupported;
3065 } else {
3066 if (bmpImage->green_mask==0x03e0 &&
3067 (bmpImage->red_mask==0x7f00 ||
3068 bmpImage->blue_mask==0x7f00)) {
3069 /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
3070 convs->Convert_any0888_to_5x5
3071 (width,lines,
3072 srcbits,linebytes,
3073 rSrc,gSrc,bSrc,
3074 dstbits,-bmpImage->bytes_per_line,
3075 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3076 } else if (bmpImage->green_mask==0x07e0 &&
3077 (bmpImage->red_mask==0xf800 ||
3078 bmpImage->blue_mask==0xf800)) {
3079 /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
3080 convs->Convert_any0888_to_5x5
3081 (width,lines,
3082 srcbits,linebytes,
3083 rSrc,gSrc,bSrc,
3084 dstbits,-bmpImage->bytes_per_line,
3085 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3086 } else {
3087 goto notsupported;
3091 break;
3093 default:
3094 notsupported:
3095 WARN("from 32 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
3096 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
3097 bmpImage->green_mask, bmpImage->blue_mask );
3098 /* fall through */
3099 case 1:
3100 case 4:
3101 case 8:
3103 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
3104 const DWORD* srcpixel;
3105 int rShift,gShift,bShift;
3107 rShift=X11DRV_DIB_MaskToShift(rSrc);
3108 gShift=X11DRV_DIB_MaskToShift(gSrc);
3109 bShift=X11DRV_DIB_MaskToShift(bSrc);
3110 srcbits+=left*4;
3111 for (h = lines - 1; h >= 0; h--) {
3112 srcpixel=(const DWORD*)srcbits;
3113 for (x = left; x < width+left; x++) {
3114 DWORD srcvalue;
3115 BYTE red,green,blue;
3116 srcvalue=*srcpixel++;
3117 red= (srcvalue >> rShift) & 0xff;
3118 green=(srcvalue >> gShift) & 0xff;
3119 blue= (srcvalue >> bShift) & 0xff;
3120 XPutPixel(bmpImage, x, h, X11DRV_PALETTE_ToPhysical
3121 (physDev, RGB(red,green,blue)));
3123 srcbits += linebytes;
3126 break;
3131 /***********************************************************************
3132 * X11DRV_DIB_GetImageBits_32
3134 * GetDIBits for an 32-bit deep DIB.
3136 static void X11DRV_DIB_GetImageBits_32( int lines, BYTE *dstbits,
3137 DWORD dstwidth, DWORD srcwidth,
3138 PALETTEENTRY *srccolors,
3139 DWORD rDst, DWORD gDst, DWORD bDst,
3140 XImage *bmpImage, DWORD linebytes )
3142 DWORD x;
3143 int h, width = min(srcwidth, dstwidth);
3144 BYTE *bits;
3145 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
3147 if (lines < 0 )
3149 lines = -lines;
3150 dstbits = dstbits + ( linebytes * (lines-1) );
3151 linebytes = -linebytes;
3154 bits = dstbits;
3156 switch (bmpImage->depth)
3158 case 24:
3159 if (bmpImage->bits_per_pixel==24) {
3160 const void* srcbits;
3162 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3164 if (rDst==bmpImage->red_mask && gDst==bmpImage->green_mask && bDst==bmpImage->blue_mask) {
3165 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
3166 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
3167 convs->Convert_888_to_0888_asis
3168 (width,lines,
3169 srcbits,-bmpImage->bytes_per_line,
3170 dstbits,linebytes);
3171 } else if (bmpImage->green_mask!=0x00ff00 ||
3172 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3173 goto notsupported;
3174 /* the tests below assume sane bmpImage masks */
3175 } else if (rDst==bmpImage->blue_mask && gDst==bmpImage->green_mask && bDst==bmpImage->red_mask) {
3176 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
3177 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
3178 convs->Convert_888_to_0888_reverse
3179 (width,lines,
3180 srcbits,-bmpImage->bytes_per_line,
3181 dstbits,linebytes);
3182 } else if (bmpImage->blue_mask==0xff) {
3183 /* ==== rgb 888 bmp -> any 0888 dib ==== */
3184 convs->Convert_rgb888_to_any0888
3185 (width,lines,
3186 srcbits,-bmpImage->bytes_per_line,
3187 dstbits,linebytes,
3188 rDst,gDst,bDst);
3189 } else {
3190 /* ==== bgr 888 bmp -> any 0888 dib ==== */
3191 convs->Convert_bgr888_to_any0888
3192 (width,lines,
3193 srcbits,-bmpImage->bytes_per_line,
3194 dstbits,linebytes,
3195 rDst,gDst,bDst);
3197 break;
3199 /* fall through */
3201 case 32:
3203 const char* srcbits;
3205 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3207 if (gDst==bmpImage->green_mask) {
3208 if (rDst==bmpImage->red_mask && bDst==bmpImage->blue_mask) {
3209 /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
3210 /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
3211 convs->Convert_0888_asis
3212 (width,lines,
3213 srcbits,-bmpImage->bytes_per_line,
3214 dstbits,linebytes);
3215 } else if (bmpImage->green_mask!=0x00ff00 ||
3216 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3217 goto notsupported;
3218 /* the tests below assume sane bmpImage masks */
3219 } else if (rDst==bmpImage->blue_mask && bDst==bmpImage->red_mask) {
3220 /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
3221 /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
3222 convs->Convert_0888_reverse
3223 (width,lines,
3224 srcbits,-bmpImage->bytes_per_line,
3225 dstbits,linebytes);
3226 } else {
3227 /* ==== any 0888 bmp -> any 0888 dib ==== */
3228 convs->Convert_0888_any
3229 (width,lines,
3230 srcbits,-bmpImage->bytes_per_line,
3231 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3232 dstbits,linebytes,
3233 rDst,gDst,bDst);
3235 } else if (bmpImage->green_mask!=0x00ff00 ||
3236 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3237 goto notsupported;
3238 /* the tests below assume sane bmpImage masks */
3239 } else {
3240 /* ==== any 0888 bmp -> any 0888 dib ==== */
3241 convs->Convert_0888_any
3242 (width,lines,
3243 srcbits,-bmpImage->bytes_per_line,
3244 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3245 dstbits,linebytes,
3246 rDst,gDst,bDst);
3249 break;
3251 case 15:
3252 case 16:
3254 const char* srcbits;
3256 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3258 if (rDst==0xff0000 && gDst==0x00ff00 && bDst==0x0000ff) {
3259 if (bmpImage->green_mask==0x03e0) {
3260 if (bmpImage->red_mask==0x7f00) {
3261 /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
3262 convs->Convert_555_to_0888_asis
3263 (width,lines,
3264 srcbits,-bmpImage->bytes_per_line,
3265 dstbits,linebytes);
3266 } else if (bmpImage->blue_mask==0x7f00) {
3267 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
3268 convs->Convert_555_to_0888_reverse
3269 (width,lines,
3270 srcbits,-bmpImage->bytes_per_line,
3271 dstbits,linebytes);
3272 } else {
3273 goto notsupported;
3275 } else if (bmpImage->green_mask==0x07e0) {
3276 if (bmpImage->red_mask==0xf800) {
3277 /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
3278 convs->Convert_565_to_0888_asis
3279 (width,lines,
3280 srcbits,-bmpImage->bytes_per_line,
3281 dstbits,linebytes);
3282 } else if (bmpImage->blue_mask==0xf800) {
3283 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
3284 convs->Convert_565_to_0888_reverse
3285 (width,lines,
3286 srcbits,-bmpImage->bytes_per_line,
3287 dstbits,linebytes);
3288 } else {
3289 goto notsupported;
3291 } else {
3292 goto notsupported;
3294 } else if (rDst==0x0000ff && gDst==0x00ff00 && bDst==0xff0000) {
3295 if (bmpImage->green_mask==0x03e0) {
3296 if (bmpImage->blue_mask==0x7f00) {
3297 /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
3298 convs->Convert_555_to_0888_asis
3299 (width,lines,
3300 srcbits,-bmpImage->bytes_per_line,
3301 dstbits,linebytes);
3302 } else if (bmpImage->red_mask==0x7f00) {
3303 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
3304 convs->Convert_555_to_0888_reverse
3305 (width,lines,
3306 srcbits,-bmpImage->bytes_per_line,
3307 dstbits,linebytes);
3308 } else {
3309 goto notsupported;
3311 } else if (bmpImage->green_mask==0x07e0) {
3312 if (bmpImage->blue_mask==0xf800) {
3313 /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3314 convs->Convert_565_to_0888_asis
3315 (width,lines,
3316 srcbits,-bmpImage->bytes_per_line,
3317 dstbits,linebytes);
3318 } else if (bmpImage->red_mask==0xf800) {
3319 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3320 convs->Convert_565_to_0888_reverse
3321 (width,lines,
3322 srcbits,-bmpImage->bytes_per_line,
3323 dstbits,linebytes);
3324 } else {
3325 goto notsupported;
3327 } else {
3328 goto notsupported;
3330 } else {
3331 if (bmpImage->green_mask==0x03e0 &&
3332 (bmpImage->red_mask==0x7f00 ||
3333 bmpImage->blue_mask==0x7f00)) {
3334 /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3335 convs->Convert_5x5_to_any0888
3336 (width,lines,
3337 srcbits,-bmpImage->bytes_per_line,
3338 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3339 dstbits,linebytes,
3340 rDst,gDst,bDst);
3341 } else if (bmpImage->green_mask==0x07e0 &&
3342 (bmpImage->red_mask==0xf800 ||
3343 bmpImage->blue_mask==0xf800)) {
3344 /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3345 convs->Convert_5x5_to_any0888
3346 (width,lines,
3347 srcbits,-bmpImage->bytes_per_line,
3348 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3349 dstbits,linebytes,
3350 rDst,gDst,bDst);
3351 } else {
3352 goto notsupported;
3356 break;
3358 case 1:
3359 case 4:
3360 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3361 && srccolors) {
3362 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3363 int rShift,gShift,bShift;
3364 DWORD* dstpixel;
3366 rShift=X11DRV_DIB_MaskToShift(rDst);
3367 gShift=X11DRV_DIB_MaskToShift(gDst);
3368 bShift=X11DRV_DIB_MaskToShift(bDst);
3369 for (h = lines - 1; h >= 0; h--) {
3370 dstpixel=(DWORD*)dstbits;
3371 for (x = 0; x < width; x++) {
3372 PALETTEENTRY srcval;
3373 srcval = srccolors[XGetPixel(bmpImage, x, h)];
3374 *dstpixel++=(srcval.peRed << rShift) |
3375 (srcval.peGreen << gShift) |
3376 (srcval.peBlue << bShift);
3378 dstbits += linebytes;
3380 } else {
3381 goto notsupported;
3383 break;
3385 case 8:
3386 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3387 && srccolors) {
3388 /* ==== pal 8 bmp -> any 0888 dib ==== */
3389 int rShift,gShift,bShift;
3390 const void* srcbits;
3391 const BYTE* srcpixel;
3392 DWORD* dstpixel;
3394 rShift=X11DRV_DIB_MaskToShift(rDst);
3395 gShift=X11DRV_DIB_MaskToShift(gDst);
3396 bShift=X11DRV_DIB_MaskToShift(bDst);
3397 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3398 for (h = lines - 1; h >= 0; h--) {
3399 srcpixel=srcbits;
3400 dstpixel=(DWORD*)dstbits;
3401 for (x = 0; x < width; x++) {
3402 PALETTEENTRY srcval;
3403 srcval=srccolors[*srcpixel++];
3404 *dstpixel++=(srcval.peRed << rShift) |
3405 (srcval.peGreen << gShift) |
3406 (srcval.peBlue << bShift);
3408 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
3409 dstbits += linebytes;
3411 } else {
3412 goto notsupported;
3414 break;
3416 default:
3417 notsupported:
3419 /* ==== any bmp format -> any 0888 dib ==== */
3420 int rShift,gShift,bShift;
3421 DWORD* dstpixel;
3423 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%x,%x,%x)\n",
3424 bmpImage->depth, bmpImage->red_mask,
3425 bmpImage->green_mask, bmpImage->blue_mask,
3426 rDst,gDst,bDst);
3428 rShift=X11DRV_DIB_MaskToShift(rDst);
3429 gShift=X11DRV_DIB_MaskToShift(gDst);
3430 bShift=X11DRV_DIB_MaskToShift(bDst);
3431 for (h = lines - 1; h >= 0; h--) {
3432 dstpixel=(DWORD*)dstbits;
3433 for (x = 0; x < width; x++) {
3434 COLORREF srcval;
3435 srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
3436 *dstpixel++=(GetRValue(srcval) << rShift) |
3437 (GetGValue(srcval) << gShift) |
3438 (GetBValue(srcval) << bShift);
3440 dstbits += linebytes;
3443 break;
3447 static int XGetSubImageErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
3449 return (event->request_code == X_GetImage && event->error_code == BadMatch);
3452 /***********************************************************************
3453 * X11DRV_DIB_SetImageBits_GetSubImage
3455 * Helper for X11DRV_DIB_SetImageBits
3457 static void X11DRV_DIB_SetImageBits_GetSubImage(
3458 const X11DRV_DIB_IMAGEBITS_DESCR *descr, XImage *bmpImage)
3460 /* compressed bitmaps may contain gaps in them. So make a copy
3461 * of the existing pixels first */
3462 RECT bmprc, rc;
3464 SetRect( &bmprc, descr->xDest, descr->yDest,
3465 descr->xDest + descr->width , descr->yDest + descr->height );
3466 GetRgnBox( descr->physDev->region, &rc );
3467 /* convert from dc to drawable origin */
3468 OffsetRect( &rc, descr->physDev->dc_rect.left, descr->physDev->dc_rect.top);
3469 /* clip visible rect with bitmap */
3470 if( IntersectRect( &rc, &rc, &bmprc))
3472 X11DRV_expect_error( gdi_display, XGetSubImageErrorHandler, NULL );
3473 XGetSubImage( gdi_display, descr->drawable, rc.left, rc.top,
3474 rc.right - rc.left, rc.bottom - rc.top, AllPlanes,
3475 ZPixmap, bmpImage,
3476 descr->xSrc + rc.left - bmprc.left,
3477 descr->ySrc + rc.top - bmprc.top);
3478 X11DRV_check_error();
3482 /***********************************************************************
3483 * X11DRV_DIB_SetImageBits
3485 * Transfer the bits to an X image.
3486 * Helper function for SetDIBits() and SetDIBitsToDevice().
3488 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3490 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3491 XImage *bmpImage;
3493 wine_tsx11_lock();
3494 if (descr->image)
3495 bmpImage = descr->image;
3496 else {
3497 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3498 descr->infoWidth, lines, 32, 0 );
3499 bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
3500 if(bmpImage->data == NULL) {
3501 ERR("Out of memory!\n");
3502 XDestroyImage( bmpImage );
3503 wine_tsx11_unlock();
3504 return lines;
3507 wine_tsx11_unlock();
3509 TRACE("Dib: depth=%d r=%x g=%x b=%x\n",
3510 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3511 TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3512 bmpImage->depth,bmpImage->bits_per_pixel,
3513 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3515 /* Transfer the pixels */
3516 switch(descr->infoBpp)
3518 case 1:
3519 X11DRV_DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
3520 descr->width, descr->xSrc, (int *)(descr->colorMap),
3521 bmpImage, descr->dibpitch );
3522 break;
3523 case 4:
3524 if (descr->compression) {
3525 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3526 X11DRV_DIB_SetImageBits_RLE4( descr->lines, descr->bits,
3527 descr->infoWidth, descr->width,
3528 descr->xSrc, (int *)(descr->colorMap),
3529 bmpImage );
3530 } else
3531 X11DRV_DIB_SetImageBits_4( descr->lines, descr->bits,
3532 descr->infoWidth, descr->width,
3533 descr->xSrc, (int*)(descr->colorMap),
3534 bmpImage, descr->dibpitch );
3535 break;
3536 case 8:
3537 if (descr->compression) {
3538 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3539 X11DRV_DIB_SetImageBits_RLE8( descr->lines, descr->bits,
3540 descr->infoWidth, descr->width,
3541 descr->xSrc, (int *)(descr->colorMap),
3542 bmpImage );
3543 } else
3544 X11DRV_DIB_SetImageBits_8( descr->lines, descr->bits,
3545 descr->infoWidth, descr->width,
3546 descr->xSrc, (int *)(descr->colorMap),
3547 bmpImage, descr->dibpitch );
3548 break;
3549 case 15:
3550 case 16:
3551 X11DRV_DIB_SetImageBits_16( descr->lines, descr->bits,
3552 descr->infoWidth, descr->width,
3553 descr->xSrc, descr->physDev,
3554 descr->rMask, descr->gMask, descr->bMask,
3555 bmpImage, descr->dibpitch);
3556 break;
3557 case 24:
3558 X11DRV_DIB_SetImageBits_24( descr->lines, descr->bits,
3559 descr->infoWidth, descr->width,
3560 descr->xSrc, descr->physDev,
3561 descr->rMask, descr->gMask, descr->bMask,
3562 bmpImage, descr->dibpitch);
3563 break;
3564 case 32:
3565 X11DRV_DIB_SetImageBits_32( descr->lines, descr->bits,
3566 descr->infoWidth, descr->width,
3567 descr->xSrc, descr->physDev,
3568 descr->rMask, descr->gMask, descr->bMask,
3569 bmpImage, descr->dibpitch);
3570 break;
3571 default:
3572 WARN("(%d): Invalid depth\n", descr->infoBpp );
3573 break;
3576 TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3577 descr->drawable, descr->gc, bmpImage,
3578 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3579 descr->width, descr->height);
3581 wine_tsx11_lock();
3582 #ifdef HAVE_LIBXXSHM
3583 if (descr->image && descr->useShm)
3585 XShmPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3586 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3587 descr->width, descr->height, FALSE );
3588 XSync( gdi_display, 0 );
3590 else
3591 #endif
3592 XPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3593 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3594 descr->width, descr->height );
3596 if (!descr->image) XDestroyImage( bmpImage );
3597 wine_tsx11_unlock();
3598 return lines;
3601 /***********************************************************************
3602 * X11DRV_DIB_GetImageBits
3604 * Transfer the bits from an X image.
3606 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3608 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3609 XImage *bmpImage;
3611 wine_tsx11_lock();
3612 if (descr->image)
3613 bmpImage = descr->image;
3614 else {
3615 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3616 descr->infoWidth, lines, 32, 0 );
3617 bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
3618 if(bmpImage->data == NULL) {
3619 ERR("Out of memory!\n");
3620 XDestroyImage( bmpImage );
3621 wine_tsx11_unlock();
3622 return lines;
3626 #ifdef HAVE_LIBXXSHM
3628 /* We must not call XShmGetImage() with a bitmap which is bigger than the available area.
3629 If we do, XShmGetImage() will fail (X exception), as it checks for this internally. */
3630 if((descr->image && descr->useShm) && (bmpImage->width <= (descr->width - descr->xSrc))
3631 && (bmpImage->height <= (descr->height - descr->ySrc)))
3633 int saveRed, saveGreen, saveBlue;
3635 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3636 gdi_display, descr->drawable, bmpImage,
3637 descr->xSrc, descr->ySrc, AllPlanes);
3639 /* We must save and restore the bmpImage's masks in order
3640 * to preserve them across the call to XShmGetImage, which
3641 * decides to eleminate them since it doesn't happen to know
3642 * what the format of the image is supposed to be, even though
3643 * we do. */
3644 saveRed = bmpImage->red_mask;
3645 saveBlue= bmpImage->blue_mask;
3646 saveGreen = bmpImage->green_mask;
3648 XShmGetImage( gdi_display, descr->drawable, bmpImage,
3649 descr->xSrc, descr->ySrc, AllPlanes);
3651 bmpImage->red_mask = saveRed;
3652 bmpImage->blue_mask = saveBlue;
3653 bmpImage->green_mask = saveGreen;
3655 else
3656 #endif /* HAVE_LIBXXSHM */
3658 TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3659 gdi_display, descr->drawable, descr->xSrc, descr->ySrc, descr->width,
3660 lines, AllPlanes, ZPixmap, bmpImage, descr->xDest, descr->yDest);
3661 XGetSubImage( gdi_display, descr->drawable, descr->xSrc, descr->ySrc,
3662 descr->width, lines, AllPlanes, ZPixmap,
3663 bmpImage, descr->xDest, descr->yDest );
3665 wine_tsx11_unlock();
3667 TRACE("Dib: depth=%2d r=%x g=%x b=%x\n",
3668 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3669 TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3670 bmpImage->depth,bmpImage->bits_per_pixel,
3671 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3672 /* Transfer the pixels */
3673 switch(descr->infoBpp)
3675 case 1:
3676 X11DRV_DIB_GetImageBits_1( descr->lines,(LPVOID)descr->bits,
3677 descr->infoWidth, descr->width,
3678 descr->colorMap, descr->palentry,
3679 bmpImage, descr->dibpitch );
3680 break;
3682 case 4:
3683 if (descr->compression) {
3684 FIXME("Compression not yet supported!\n");
3685 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 4 ) * abs(descr->lines))
3686 break;
3688 X11DRV_DIB_GetImageBits_4( descr->lines,(LPVOID)descr->bits,
3689 descr->infoWidth, descr->width,
3690 descr->colorMap, descr->palentry,
3691 bmpImage, descr->dibpitch );
3692 break;
3693 case 8:
3694 if (descr->compression) {
3695 FIXME("Compression not yet supported!\n");
3696 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 8 ) * abs(descr->lines))
3697 break;
3699 X11DRV_DIB_GetImageBits_8( descr->lines, (LPVOID)descr->bits,
3700 descr->infoWidth, descr->width,
3701 descr->colorMap, descr->palentry,
3702 bmpImage, descr->dibpitch );
3703 break;
3704 case 15:
3705 case 16:
3706 X11DRV_DIB_GetImageBits_16( descr->lines, (LPVOID)descr->bits,
3707 descr->infoWidth,descr->width,
3708 descr->palentry,
3709 descr->rMask, descr->gMask, descr->bMask,
3710 bmpImage, descr->dibpitch );
3711 break;
3713 case 24:
3714 X11DRV_DIB_GetImageBits_24( descr->lines, (LPVOID)descr->bits,
3715 descr->infoWidth,descr->width,
3716 descr->palentry,
3717 descr->rMask, descr->gMask, descr->bMask,
3718 bmpImage, descr->dibpitch);
3719 break;
3721 case 32:
3722 X11DRV_DIB_GetImageBits_32( descr->lines, (LPVOID)descr->bits,
3723 descr->infoWidth, descr->width,
3724 descr->palentry,
3725 descr->rMask, descr->gMask, descr->bMask,
3726 bmpImage, descr->dibpitch);
3727 break;
3729 default:
3730 WARN("(%d): Invalid depth\n", descr->infoBpp );
3731 break;
3734 if (!descr->image)
3736 wine_tsx11_lock();
3737 XDestroyImage( bmpImage );
3738 wine_tsx11_unlock();
3740 return lines;
3743 /*************************************************************************
3744 * X11DRV_SetDIBitsToDevice
3747 INT X11DRV_SetDIBitsToDevice( X11DRV_PDEVICE *physDev, INT xDest, INT yDest, DWORD cx,
3748 DWORD cy, INT xSrc, INT ySrc,
3749 UINT startscan, UINT lines, LPCVOID bits,
3750 const BITMAPINFO *info, UINT coloruse )
3752 X11DRV_DIB_IMAGEBITS_DESCR descr;
3753 INT result;
3754 LONG width, height;
3755 BOOL top_down;
3756 POINT pt;
3757 int rop = X11DRV_XROPfunction[GetROP2(physDev->hdc) - 1];
3759 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3760 &descr.infoBpp, &descr.compression ) == -1)
3761 return 0;
3763 top_down = (height < 0);
3764 if (top_down) height = -height;
3766 pt.x = xDest;
3767 pt.y = yDest;
3768 LPtoDP(physDev->hdc, &pt, 1);
3770 if (!lines || (startscan >= height)) return 0;
3771 if (!top_down && startscan + lines > height) lines = height - startscan;
3773 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3774 * and clamp all values to fit inside [startscan,startscan+lines]
3776 if (ySrc + cy <= startscan + lines)
3778 UINT y = startscan + lines - (ySrc + cy);
3779 if (ySrc < startscan) cy -= (startscan - ySrc);
3780 if (!top_down)
3782 /* avoid getting unnecessary lines */
3783 ySrc = 0;
3784 if (y >= lines) return 0;
3785 lines -= y;
3787 else
3789 if (y >= lines) return lines;
3790 ySrc = y; /* need to get all lines in top down mode */
3793 else
3795 if (ySrc >= startscan + lines) return lines;
3796 pt.y += ySrc + cy - (startscan + lines);
3797 cy = startscan + lines - ySrc;
3798 ySrc = 0;
3799 if (cy > lines) cy = lines;
3801 if (xSrc >= width) return lines;
3802 if (xSrc + cx >= width) cx = width - xSrc;
3803 if (!cx || !cy) return lines;
3805 /* Update the pixmap from the DIB section */
3806 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
3808 X11DRV_SetupGCForText( physDev ); /* To have the correct colors */
3809 wine_tsx11_lock();
3810 XSetFunction(gdi_display, physDev->gc, rop);
3811 wine_tsx11_unlock();
3813 switch (descr.infoBpp)
3815 case 1:
3816 case 4:
3817 case 8:
3818 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3819 physDev, coloruse,
3820 physDev->depth, info, &descr.nColorMap );
3821 if (!descr.colorMap) return 0;
3822 descr.rMask = descr.gMask = descr.bMask = 0;
3823 break;
3824 case 15:
3825 case 16:
3826 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3827 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3828 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3829 descr.colorMap = 0;
3830 break;
3832 case 24:
3833 case 32:
3834 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3835 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3836 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3837 descr.colorMap = 0;
3838 break;
3841 descr.physDev = physDev;
3842 descr.bits = bits;
3843 descr.image = NULL;
3844 descr.palentry = NULL;
3845 descr.lines = top_down ? -lines : lines;
3846 descr.infoWidth = width;
3847 descr.depth = physDev->depth;
3848 descr.drawable = physDev->drawable;
3849 descr.gc = physDev->gc;
3850 descr.xSrc = xSrc;
3851 descr.ySrc = ySrc;
3852 descr.xDest = physDev->dc_rect.left + pt.x;
3853 descr.yDest = physDev->dc_rect.top + pt.y;
3854 descr.width = cx;
3855 descr.height = cy;
3856 descr.useShm = FALSE;
3857 descr.dibpitch = ((width * descr.infoBpp + 31) &~31) / 8;
3859 result = X11DRV_DIB_SetImageBits( &descr );
3861 if (descr.infoBpp <= 8)
3862 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3864 /* Update the DIBSection of the pixmap */
3865 X11DRV_UnlockDIBSection(physDev, TRUE);
3867 return result;
3870 /***********************************************************************
3871 * SetDIBits (X11DRV.@)
3873 INT X11DRV_SetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan,
3874 UINT lines, LPCVOID bits, const BITMAPINFO *info, UINT coloruse )
3876 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3877 X11DRV_DIB_IMAGEBITS_DESCR descr;
3878 BITMAP bitmap;
3879 LONG width, height, tmpheight;
3880 INT result;
3882 descr.physDev = physDev;
3884 if (!physBitmap) return 0;
3886 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3887 &descr.infoBpp, &descr.compression ) == -1)
3888 return 0;
3890 tmpheight = height;
3891 if (height < 0) height = -height;
3892 if (!lines || (startscan >= height))
3893 return 0;
3895 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
3897 if (startscan + lines > height) lines = height - startscan;
3899 switch (descr.infoBpp)
3901 case 1:
3902 case 4:
3903 case 8:
3904 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3905 descr.physDev, coloruse,
3906 physBitmap->pixmap_depth,
3907 info, &descr.nColorMap );
3908 if (!descr.colorMap) return 0;
3909 descr.rMask = descr.gMask = descr.bMask = 0;
3910 break;
3911 case 15:
3912 case 16:
3913 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3914 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3915 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3916 descr.colorMap = 0;
3917 break;
3919 case 24:
3920 case 32:
3921 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3922 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3923 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3924 descr.colorMap = 0;
3925 break;
3927 default: break;
3930 descr.bits = bits;
3931 descr.image = NULL;
3932 descr.palentry = NULL;
3933 descr.infoWidth = width;
3934 descr.lines = tmpheight >= 0 ? lines : -lines;
3935 descr.depth = physBitmap->pixmap_depth;
3936 descr.drawable = physBitmap->pixmap;
3937 descr.gc = BITMAP_GC(physBitmap);
3938 descr.xSrc = 0;
3939 descr.ySrc = 0;
3940 descr.xDest = 0;
3941 descr.yDest = height - startscan - lines;
3942 descr.width = bitmap.bmWidth;
3943 descr.height = lines;
3944 descr.useShm = FALSE;
3945 descr.dibpitch = ((descr.infoWidth * descr.infoBpp + 31) &~31) / 8;
3946 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
3947 result = X11DRV_DIB_SetImageBits( &descr );
3948 X11DRV_DIB_Unlock( physBitmap, TRUE );
3950 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3952 return result;
3955 /***********************************************************************
3956 * GetDIBits (X11DRV.@)
3958 INT X11DRV_GetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan, UINT lines,
3959 LPVOID bits, BITMAPINFO *info, UINT coloruse )
3961 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3962 DIBSECTION dib;
3963 X11DRV_DIB_IMAGEBITS_DESCR descr;
3964 PALETTEENTRY palette[256];
3965 size_t obj_size;
3966 int height;
3967 LONG width, tempHeight;
3968 int bitmap_type;
3969 BOOL core_header;
3970 void* colorPtr;
3972 GetPaletteEntries( GetCurrentObject( physDev->hdc, OBJ_PAL ), 0, 256, palette );
3974 if (!physBitmap) return 0;
3975 if (!(obj_size = GetObjectW( hbitmap, sizeof(dib), &dib ))) return 0;
3977 bitmap_type = DIB_GetBitmapInfo( (BITMAPINFOHEADER*)info, &width, &tempHeight, &descr.infoBpp, &descr.compression);
3978 descr.lines = tempHeight;
3979 if (bitmap_type == -1)
3981 ERR("Invalid bitmap\n");
3982 return 0;
3984 core_header = (bitmap_type == 0);
3985 colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
3987 TRACE("%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
3988 lines, dib.dsBm.bmWidth, dib.dsBm.bmHeight, width, descr.lines, startscan);
3990 if( lines > dib.dsBm.bmHeight ) lines = dib.dsBm.bmHeight;
3992 height = descr.lines;
3993 if (height < 0) height = -height;
3994 if( lines > height ) lines = height;
3995 /* Top-down images have a negative biHeight, the scanlines of these images
3996 * were inverted in X11DRV_DIB_GetImageBits_xx
3997 * To prevent this we simply change the sign of lines
3998 * (the number of scan lines to copy).
3999 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4001 if( descr.lines < 0 && lines > 0) lines = -lines;
4003 if( startscan >= dib.dsBm.bmHeight ) return 0;
4005 descr.colorMap = NULL;
4007 switch (descr.infoBpp)
4009 case 1:
4010 case 4:
4011 case 8:
4012 descr.rMask= descr.gMask = descr.bMask = 0;
4013 if(coloruse == DIB_RGB_COLORS)
4014 descr.colorMap = colorPtr;
4015 else {
4016 int num_colors = 1 << descr.infoBpp, i;
4017 RGBQUAD *rgb;
4018 COLORREF colref;
4019 WORD *index = (WORD*)colorPtr;
4020 descr.colorMap = rgb = HeapAlloc(GetProcessHeap(), 0, num_colors * sizeof(RGBQUAD));
4021 for(i = 0; i < num_colors; i++, rgb++, index++) {
4022 colref = X11DRV_PALETTE_ToLogical(X11DRV_PALETTE_ToPhysical(physDev, PALETTEINDEX(*index)));
4023 rgb->rgbRed = GetRValue(colref);
4024 rgb->rgbGreen = GetGValue(colref);
4025 rgb->rgbBlue = GetBValue(colref);
4026 rgb->rgbReserved = 0;
4029 break;
4030 case 15:
4031 case 16:
4032 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4033 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4034 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4035 break;
4036 case 24:
4037 case 32:
4038 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4039 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4040 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4041 break;
4044 descr.physDev = physDev;
4045 descr.palentry = palette;
4046 descr.bits = bits;
4047 descr.image = physBitmap->image;
4048 descr.infoWidth = width;
4049 descr.lines = lines;
4050 descr.depth = physBitmap->pixmap_depth;
4051 descr.drawable = physBitmap->pixmap;
4052 descr.gc = BITMAP_GC(physBitmap);
4053 descr.width = dib.dsBm.bmWidth;
4054 descr.height = dib.dsBm.bmHeight;
4055 descr.xDest = 0;
4056 descr.yDest = 0;
4057 descr.xSrc = 0;
4058 descr.sizeImage = core_header ? 0 : info->bmiHeader.biSizeImage;
4060 if (descr.lines > 0)
4062 descr.ySrc = (descr.height-1) - (startscan + (lines-1));
4064 else
4066 descr.ySrc = startscan;
4068 #ifdef HAVE_LIBXXSHM
4069 descr.useShm = (obj_size == sizeof(DIBSECTION)) && (physBitmap->shminfo.shmid != -1);
4070 #else
4071 descr.useShm = FALSE;
4072 #endif
4073 descr.dibpitch = (obj_size == sizeof(DIBSECTION)) ? dib.dsBm.bmWidthBytes
4074 : (((descr.infoWidth * descr.infoBpp + 31) &~31) / 8);
4076 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
4077 X11DRV_DIB_GetImageBits( &descr );
4078 X11DRV_DIB_Unlock( physBitmap, TRUE );
4080 if(!core_header && info->bmiHeader.biSizeImage == 0) /* Fill in biSizeImage */
4081 info->bmiHeader.biSizeImage = X11DRV_DIB_GetDIBImageBytes( descr.infoWidth,
4082 descr.lines,
4083 descr.infoBpp);
4085 if (descr.compression == BI_BITFIELDS)
4087 *(DWORD *)info->bmiColors = descr.rMask;
4088 *((DWORD *)info->bmiColors + 1) = descr.gMask;
4089 *((DWORD *)info->bmiColors + 2) = descr.bMask;
4091 else if (!core_header)
4093 /* if RLE or JPEG compression were supported,
4094 * this line would be invalid. */
4095 info->bmiHeader.biCompression = 0;
4098 if(descr.colorMap != colorPtr)
4099 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4100 return lines;
4103 /***********************************************************************
4104 * DIB_DoProtectDIBSection
4106 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP *physBitmap, DWORD new_prot )
4108 DWORD old_prot;
4110 VirtualProtect(physBitmap->base, physBitmap->size, new_prot, &old_prot);
4111 TRACE("Changed protection from %d to %d\n", old_prot, new_prot);
4114 /***********************************************************************
4115 * X11DRV_DIB_DoCopyDIBSection
4117 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB,
4118 void *colorMap, int nColorMap,
4119 Drawable dest, GC gc,
4120 DWORD xSrc, DWORD ySrc,
4121 DWORD xDest, DWORD yDest,
4122 DWORD width, DWORD height)
4124 DIBSECTION dibSection;
4125 X11DRV_DIB_IMAGEBITS_DESCR descr;
4126 int identity[2] = {0,1};
4128 if (!GetObjectW( physBitmap->hbitmap, sizeof(dibSection), &dibSection )) return;
4130 descr.physDev = NULL;
4131 descr.palentry = NULL;
4132 descr.infoWidth = dibSection.dsBmih.biWidth;
4133 descr.infoBpp = dibSection.dsBmih.biBitCount;
4134 descr.lines = dibSection.dsBmih.biHeight;
4135 descr.image = physBitmap->image;
4136 descr.colorMap = colorMap;
4137 descr.nColorMap = nColorMap;
4138 descr.bits = dibSection.dsBm.bmBits;
4139 descr.depth = physBitmap->pixmap_depth;
4140 descr.compression = dibSection.dsBmih.biCompression;
4142 if(descr.infoBpp == 1)
4143 descr.colorMap = (void*)identity;
4145 switch (descr.infoBpp)
4147 case 1:
4148 case 4:
4149 case 8:
4150 descr.rMask = descr.gMask = descr.bMask = 0;
4151 break;
4152 case 15:
4153 case 16:
4154 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0x7c00;
4155 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x03e0;
4156 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x001f;
4157 break;
4159 case 24:
4160 case 32:
4161 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0xff0000;
4162 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x00ff00;
4163 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x0000ff;
4164 break;
4167 /* Hack for now */
4168 descr.drawable = dest;
4169 descr.gc = gc;
4170 descr.xSrc = xSrc;
4171 descr.ySrc = ySrc;
4172 descr.xDest = xDest;
4173 descr.yDest = yDest;
4174 descr.width = width;
4175 descr.height = height;
4176 descr.sizeImage = 0;
4178 #ifdef HAVE_LIBXXSHM
4179 descr.useShm = (physBitmap->shminfo.shmid != -1);
4180 #else
4181 descr.useShm = FALSE;
4182 #endif
4183 descr.dibpitch = dibSection.dsBm.bmWidthBytes;
4185 if (toDIB)
4187 TRACE("Copying from Pixmap to DIB bits\n");
4188 X11DRV_DIB_GetImageBits( &descr );
4190 else
4192 TRACE("Copying from DIB bits to Pixmap\n");
4193 X11DRV_DIB_SetImageBits( &descr );
4197 /***********************************************************************
4198 * X11DRV_DIB_CopyDIBSection
4200 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
4201 DWORD xSrc, DWORD ySrc, DWORD xDest, DWORD yDest,
4202 DWORD width, DWORD height)
4204 DIBSECTION dib;
4205 X_PHYSBITMAP *physBitmap;
4206 unsigned int nColorMap;
4207 int* x11ColorMap;
4208 int freeColorMap;
4210 TRACE("(%p,%p,%d,%d,%d,%d,%d,%d)\n", physDevSrc->hdc, physDevDst->hdc,
4211 xSrc, ySrc, xDest, yDest, width, height);
4212 /* this function is meant as an optimization for BitBlt,
4213 * not to be called otherwise */
4214 physBitmap = physDevSrc->bitmap;
4215 if (!physBitmap || GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib ) != sizeof(dib))
4217 ERR("called for non-DIBSection!?\n");
4218 return;
4220 /* while BitBlt should already have made sure we only get
4221 * positive values, we should check for oversize values */
4222 if ((xSrc < dib.dsBm.bmWidth) &&
4223 (ySrc < dib.dsBm.bmHeight)) {
4224 if (xSrc + width > dib.dsBm.bmWidth)
4225 width = dib.dsBm.bmWidth - xSrc;
4226 if (ySrc + height > dib.dsBm.bmHeight)
4227 height = dib.dsBm.bmHeight - ySrc;
4228 /* if the source bitmap is 8bpp or less, we're supposed to use the
4229 * DC's palette for color conversion (not the DIB color table) */
4230 if (dib.dsBm.bmBitsPixel <= 8) {
4231 HPALETTE hPalette = GetCurrentObject( physDevSrc->hdc, OBJ_PAL );
4232 if (!hPalette || (hPalette == GetStockObject(DEFAULT_PALETTE))) {
4233 /* HACK: no palette has been set in the source DC,
4234 * use the DIB colormap instead - this is necessary in some
4235 * cases since we need to do depth conversion in some places
4236 * where real Windows can just copy data straight over */
4237 x11ColorMap = physBitmap->colorMap;
4238 nColorMap = physBitmap->nColorMap;
4239 freeColorMap = FALSE;
4240 } else {
4241 const BITMAPINFO* info = (BITMAPINFO*)&dib.dsBmih;
4242 int i;
4244 nColorMap = X11DRV_DIB_GetColorCount(info);
4245 x11ColorMap = HeapAlloc(GetProcessHeap(), 0, nColorMap * sizeof(int));
4246 for (i = 0; i < nColorMap; i++)
4247 x11ColorMap[i] = X11DRV_PALETTE_ToPhysical(physDevSrc, PALETTEINDEX(i));
4248 freeColorMap = TRUE;
4251 else
4253 nColorMap = 0;
4254 x11ColorMap = NULL;
4255 freeColorMap = FALSE;
4257 /* perform the copy */
4258 X11DRV_DIB_DoCopyDIBSection(physBitmap, FALSE, x11ColorMap, nColorMap,
4259 physDevDst->drawable, physDevDst->gc, xSrc, ySrc,
4260 physDevDst->dc_rect.left + xDest, physDevDst->dc_rect.top + yDest,
4261 width, height);
4262 /* free color mapping */
4263 if (freeColorMap)
4264 HeapFree(GetProcessHeap(), 0, x11ColorMap);
4268 /***********************************************************************
4269 * X11DRV_DIB_DoUpdateDIBSection
4271 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB)
4273 BITMAP bitmap;
4275 GetObjectW( physBitmap->hbitmap, sizeof(bitmap), &bitmap );
4276 X11DRV_DIB_DoCopyDIBSection(physBitmap, toDIB,
4277 physBitmap->colorMap, physBitmap->nColorMap,
4278 physBitmap->pixmap, BITMAP_GC(physBitmap),
4279 0, 0, 0, 0, bitmap.bmWidth, bitmap.bmHeight);
4282 /***********************************************************************
4283 * X11DRV_DIB_FaultHandler
4285 static LONG CALLBACK X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep )
4287 X_PHYSBITMAP *physBitmap = NULL;
4288 BOOL found = FALSE;
4289 BYTE *addr;
4290 struct list *ptr;
4292 if (ep->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
4293 return EXCEPTION_CONTINUE_SEARCH;
4295 addr = (BYTE *)ep->ExceptionRecord->ExceptionInformation[1];
4297 EnterCriticalSection(&dibs_cs);
4298 LIST_FOR_EACH( ptr, &dibs_list )
4300 physBitmap = LIST_ENTRY( ptr, X_PHYSBITMAP, entry );
4301 if ((physBitmap->base <= addr) && (addr < physBitmap->base + physBitmap->size))
4303 found = TRUE;
4304 break;
4307 LeaveCriticalSection(&dibs_cs);
4309 if (!found) return EXCEPTION_CONTINUE_SEARCH;
4311 X11DRV_DIB_Lock( physBitmap, DIB_Status_None );
4312 if (ep->ExceptionRecord->ExceptionInformation[0] == EXCEPTION_WRITE_FAULT) {
4313 /* the app tried to write the DIB bits */
4314 X11DRV_DIB_Coerce( physBitmap, DIB_Status_AppMod);
4315 } else {
4316 /* the app tried to read the DIB bits */
4317 X11DRV_DIB_Coerce( physBitmap, DIB_Status_InSync);
4319 X11DRV_DIB_Unlock( physBitmap, TRUE );
4321 return EXCEPTION_CONTINUE_EXECUTION;
4324 /***********************************************************************
4325 * X11DRV_DIB_Coerce
4327 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *physBitmap, INT req)
4329 INT ret = DIB_Status_None;
4331 if (!physBitmap->image) return ret; /* not a DIB section */
4332 EnterCriticalSection(&physBitmap->lock);
4333 ret = physBitmap->status;
4334 switch (req) {
4335 case DIB_Status_GdiMod:
4336 /* GDI access - request to draw on pixmap */
4337 switch (physBitmap->status)
4339 default:
4340 case DIB_Status_None:
4341 physBitmap->p_status = DIB_Status_GdiMod;
4342 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4343 break;
4345 case DIB_Status_GdiMod:
4346 TRACE("GdiMod requested in status GdiMod\n" );
4347 physBitmap->p_status = DIB_Status_GdiMod;
4348 break;
4350 case DIB_Status_InSync:
4351 TRACE("GdiMod requested in status InSync\n" );
4352 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4353 physBitmap->status = DIB_Status_GdiMod;
4354 physBitmap->p_status = DIB_Status_InSync;
4355 break;
4357 case DIB_Status_AppMod:
4358 TRACE("GdiMod requested in status AppMod\n" );
4359 /* make it readonly to avoid app changing data while we copy */
4360 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4361 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4362 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4363 physBitmap->p_status = DIB_Status_AppMod;
4364 physBitmap->status = DIB_Status_GdiMod;
4365 break;
4367 break;
4369 case DIB_Status_InSync:
4370 /* App access - request access to read DIB surface */
4371 /* (typically called from signal handler) */
4372 switch (physBitmap->status)
4374 default:
4375 case DIB_Status_None:
4376 /* shouldn't happen from signal handler */
4377 break;
4379 case DIB_Status_GdiMod:
4380 TRACE("InSync requested in status GdiMod\n" );
4381 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4382 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4383 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4384 physBitmap->status = DIB_Status_InSync;
4385 break;
4387 case DIB_Status_InSync:
4388 TRACE("InSync requested in status InSync\n" );
4389 /* shouldn't happen from signal handler */
4390 break;
4392 case DIB_Status_AppMod:
4393 TRACE("InSync requested in status AppMod\n" );
4394 /* no reason to do anything here, and this
4395 * shouldn't happen from signal handler */
4396 break;
4398 break;
4400 case DIB_Status_AppMod:
4401 /* App access - request access to write DIB surface */
4402 /* (typically called from signal handler) */
4403 switch (physBitmap->status)
4405 default:
4406 case DIB_Status_None:
4407 /* shouldn't happen from signal handler */
4408 break;
4410 case DIB_Status_GdiMod:
4411 TRACE("AppMod requested in status GdiMod\n" );
4412 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4413 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4414 physBitmap->status = DIB_Status_AppMod;
4415 break;
4417 case DIB_Status_InSync:
4418 TRACE("AppMod requested in status InSync\n" );
4419 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4420 physBitmap->status = DIB_Status_AppMod;
4421 break;
4423 case DIB_Status_AppMod:
4424 TRACE("AppMod requested in status AppMod\n" );
4425 /* shouldn't happen from signal handler */
4426 break;
4428 break;
4430 /* it is up to the caller to do the copy/conversion, probably
4431 * using the return value to decide where to copy from */
4433 LeaveCriticalSection(&physBitmap->lock);
4434 return ret;
4437 /***********************************************************************
4438 * X11DRV_DIB_Lock
4440 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *physBitmap, INT req)
4442 INT ret = DIB_Status_None;
4444 if (!physBitmap->image) return ret; /* not a DIB section */
4445 TRACE("Locking %p from thread %04x\n", physBitmap->hbitmap, GetCurrentThreadId());
4446 EnterCriticalSection(&physBitmap->lock);
4447 ret = physBitmap->status;
4448 if (req != DIB_Status_None)
4449 X11DRV_DIB_Coerce(physBitmap, req);
4450 return ret;
4453 /***********************************************************************
4454 * X11DRV_DIB_Unlock
4456 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *physBitmap, BOOL commit)
4458 if (!physBitmap->image) return; /* not a DIB section */
4459 switch (physBitmap->status)
4461 default:
4462 case DIB_Status_None:
4463 /* in case anyone is wondering, this is the "signal handler doesn't
4464 * work" case, where we always have to be ready for app access */
4465 if (commit) {
4466 switch (physBitmap->p_status)
4468 case DIB_Status_GdiMod:
4469 TRACE("Unlocking and syncing from GdiMod\n" );
4470 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4471 break;
4473 default:
4474 TRACE("Unlocking without needing to sync\n" );
4475 break;
4478 else TRACE("Unlocking with no changes\n");
4479 physBitmap->p_status = DIB_Status_None;
4480 break;
4482 case DIB_Status_GdiMod:
4483 TRACE("Unlocking in status GdiMod\n" );
4484 /* DIB was protected in Coerce */
4485 if (!commit) {
4486 /* no commit, revert to InSync if applicable */
4487 if ((physBitmap->p_status == DIB_Status_InSync) ||
4488 (physBitmap->p_status == DIB_Status_AppMod)) {
4489 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4490 physBitmap->status = DIB_Status_InSync;
4493 break;
4495 case DIB_Status_InSync:
4496 TRACE("Unlocking in status InSync\n" );
4497 /* DIB was already protected in Coerce */
4498 break;
4500 case DIB_Status_AppMod:
4501 TRACE("Unlocking in status AppMod\n" );
4502 /* DIB was already protected in Coerce */
4503 /* this case is ordinary only called from the signal handler,
4504 * so we don't bother to check for !commit */
4505 break;
4507 LeaveCriticalSection(&physBitmap->lock);
4508 TRACE("Unlocked %p\n", physBitmap->hbitmap);
4511 /***********************************************************************
4512 * X11DRV_CoerceDIBSection
4514 INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req)
4516 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4517 return X11DRV_DIB_Coerce(physDev->bitmap, req);
4520 /***********************************************************************
4521 * X11DRV_LockDIBSection
4523 INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req)
4525 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4526 return X11DRV_DIB_Lock(physDev->bitmap, req);
4529 /***********************************************************************
4530 * X11DRV_UnlockDIBSection
4532 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
4534 if (!physDev || !physDev->bitmap) return;
4535 X11DRV_DIB_Unlock(physDev->bitmap, commit);
4539 #ifdef HAVE_LIBXXSHM
4540 /***********************************************************************
4541 * X11DRV_XShmErrorHandler
4544 static int XShmErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
4546 return 1; /* FIXME: should check event contents */
4549 /***********************************************************************
4550 * X11DRV_XShmCreateImage
4553 static XImage *X11DRV_XShmCreateImage( int width, int height, int bpp,
4554 XShmSegmentInfo* shminfo)
4556 XImage *image;
4558 image = XShmCreateImage(gdi_display, visual, bpp, ZPixmap, NULL, shminfo, width, height);
4559 if (image)
4561 shminfo->shmid = shmget(IPC_PRIVATE, image->bytes_per_line * height,
4562 IPC_CREAT|0700);
4563 if( shminfo->shmid != -1 )
4565 shminfo->shmaddr = image->data = shmat(shminfo->shmid, 0, 0);
4566 if( shminfo->shmaddr != (char*)-1 )
4568 BOOL ok;
4570 shminfo->readOnly = FALSE;
4571 X11DRV_expect_error( gdi_display, XShmErrorHandler, NULL );
4572 ok = (XShmAttach( gdi_display, shminfo ) != 0);
4573 XSync( gdi_display, False );
4574 if (X11DRV_check_error()) ok = FALSE;
4575 if (ok)
4577 shmctl(shminfo->shmid, IPC_RMID, 0);
4578 return image; /* Success! */
4580 /* An error occurred */
4581 shmdt(shminfo->shmaddr);
4583 shmctl(shminfo->shmid, IPC_RMID, 0);
4584 shminfo->shmid = -1;
4586 XFlush(gdi_display);
4587 XDestroyImage(image);
4588 image = NULL;
4590 return image;
4592 #endif /* HAVE_LIBXXSHM */
4595 /***********************************************************************
4596 * X11DRV_CreateDIBSection (X11DRV.@)
4598 HBITMAP X11DRV_CreateDIBSection( X11DRV_PDEVICE *physDev, HBITMAP hbitmap,
4599 const BITMAPINFO *bmi, UINT usage )
4601 X_PHYSBITMAP *physBitmap;
4602 DIBSECTION dib;
4604 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return 0;
4605 physBitmap->status = DIB_Status_None;
4607 GetObjectW( hbitmap, sizeof(dib), &dib );
4609 /* create color map */
4610 if (dib.dsBm.bmBitsPixel <= 8)
4612 physBitmap->colorMap = X11DRV_DIB_BuildColorMap( physDev,
4613 usage, dib.dsBm.bmBitsPixel, bmi,
4614 &physBitmap->nColorMap );
4617 /* create pixmap and X image */
4618 wine_tsx11_lock();
4619 physBitmap->pixmap_depth = (dib.dsBm.bmBitsPixel == 1) ? 1 : screen_depth;
4620 physBitmap->pixmap = XCreatePixmap( gdi_display, root_window, dib.dsBm.bmWidth,
4621 dib.dsBm.bmHeight, physBitmap->pixmap_depth );
4622 #ifdef HAVE_LIBXXSHM
4623 physBitmap->shminfo.shmid = -1;
4624 if (!XShmQueryExtension(gdi_display) ||
4625 !(physBitmap->image = X11DRV_XShmCreateImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4626 physBitmap->pixmap_depth, &physBitmap->shminfo )) )
4627 #endif
4628 physBitmap->image = X11DRV_DIB_CreateXImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4629 physBitmap->pixmap_depth );
4630 wine_tsx11_unlock();
4631 if (!physBitmap->pixmap || !physBitmap->image) return 0;
4633 /* install fault handler */
4634 InitializeCriticalSection( &physBitmap->lock );
4635 physBitmap->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": X_PHYSBITMAP.lock");
4637 physBitmap->base = dib.dsBm.bmBits;
4638 physBitmap->size = dib.dsBmih.biSizeImage;
4639 physBitmap->status = DIB_Status_AppMod;
4641 if (!dibs_handler)
4642 dibs_handler = AddVectoredExceptionHandler( TRUE, X11DRV_DIB_FaultHandler );
4643 EnterCriticalSection( &dibs_cs );
4644 list_add_head( &dibs_list, &physBitmap->entry );
4645 LeaveCriticalSection( &dibs_cs );
4647 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4649 return hbitmap;
4652 /***********************************************************************
4653 * X11DRV_DIB_DeleteDIBSection
4655 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP *physBitmap, DIBSECTION *dib)
4657 BOOL last;
4659 EnterCriticalSection( &dibs_cs );
4660 list_remove( &physBitmap->entry );
4661 last = list_empty( &dibs_list );
4662 LeaveCriticalSection( &dibs_cs );
4664 if (last)
4666 RemoveVectoredExceptionHandler( dibs_handler );
4667 dibs_handler = NULL;
4670 if (dib->dshSection)
4671 X11DRV_DIB_Coerce(physBitmap, DIB_Status_InSync);
4673 if (physBitmap->image)
4675 wine_tsx11_lock();
4676 #ifdef HAVE_LIBXXSHM
4677 if (physBitmap->shminfo.shmid != -1)
4679 XShmDetach( gdi_display, &(physBitmap->shminfo) );
4680 XDestroyImage( physBitmap->image );
4681 shmdt( physBitmap->shminfo.shmaddr );
4682 physBitmap->shminfo.shmid = -1;
4684 else
4685 #endif
4686 XDestroyImage( physBitmap->image );
4687 wine_tsx11_unlock();
4690 HeapFree(GetProcessHeap(), 0, physBitmap->colorMap);
4691 physBitmap->lock.DebugInfo->Spare[0] = 0;
4692 DeleteCriticalSection(&physBitmap->lock);
4695 /***********************************************************************
4696 * SetDIBColorTable (X11DRV.@)
4698 UINT X11DRV_SetDIBColorTable( X11DRV_PDEVICE *physDev, UINT start, UINT count, const RGBQUAD *colors )
4700 DIBSECTION dib;
4701 UINT ret = 0;
4702 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4704 if (!physBitmap) return 0;
4705 GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib );
4707 if (physBitmap->colorMap && start < physBitmap->nColorMap) {
4708 UINT end = count + start;
4709 if (end > physBitmap->nColorMap) end = physBitmap->nColorMap;
4711 * Changing color table might change the mapping between
4712 * DIB colors and X11 colors and thus alter the visible state
4713 * of the bitmap object.
4716 * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4717 * at least for a 1 bpp dibsection
4719 X11DRV_DIB_Lock( physBitmap, DIB_Status_AppMod );
4720 X11DRV_DIB_GenColorMap( physDev, physBitmap->colorMap, DIB_RGB_COLORS,
4721 dib.dsBm.bmBitsPixel,
4722 TRUE, colors, start, end );
4723 X11DRV_DIB_Unlock( physBitmap, TRUE );
4724 ret = end - start;
4726 return ret;
4730 /***********************************************************************
4731 * X11DRV_DIB_CreateDIBFromBitmap
4733 * Allocates a packed DIB and copies the bitmap data into it.
4735 HGLOBAL X11DRV_DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
4737 BITMAP bmp;
4738 HGLOBAL hPackedDIB;
4739 LPBYTE pPackedDIB;
4740 LPBITMAPINFOHEADER pbmiHeader;
4741 unsigned int cDataSize, cPackedSize, OffsetBits;
4742 int nLinesCopied;
4744 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
4747 * A packed DIB contains a BITMAPINFO structure followed immediately by
4748 * an optional color palette and the pixel data.
4751 /* Calculate the size of the packed DIB */
4752 cDataSize = X11DRV_DIB_GetDIBWidthBytes( bmp.bmWidth, bmp.bmBitsPixel ) * abs( bmp.bmHeight );
4753 cPackedSize = sizeof(BITMAPINFOHEADER)
4754 + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
4755 + cDataSize;
4756 /* Get the offset to the bits */
4757 OffsetBits = cPackedSize - cDataSize;
4759 /* Allocate the packed DIB */
4760 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
4761 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
4762 cPackedSize );
4763 if ( !hPackedDIB )
4765 WARN("Could not allocate packed DIB!\n");
4766 return 0;
4769 /* A packed DIB starts with a BITMAPINFOHEADER */
4770 pPackedDIB = GlobalLock(hPackedDIB);
4771 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
4773 /* Init the BITMAPINFOHEADER */
4774 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
4775 pbmiHeader->biWidth = bmp.bmWidth;
4776 pbmiHeader->biHeight = bmp.bmHeight;
4777 pbmiHeader->biPlanes = 1;
4778 pbmiHeader->biBitCount = bmp.bmBitsPixel;
4779 pbmiHeader->biCompression = BI_RGB;
4780 pbmiHeader->biSizeImage = 0;
4781 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
4782 pbmiHeader->biClrUsed = 0;
4783 pbmiHeader->biClrImportant = 0;
4785 /* Retrieve the DIB bits from the bitmap and fill in the
4786 * DIB color table if present */
4788 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
4789 hBmp, /* Handle to bitmap */
4790 0, /* First scan line to set in dest bitmap */
4791 bmp.bmHeight, /* Number of scan lines to copy */
4792 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
4793 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
4794 0); /* RGB or palette index */
4795 GlobalUnlock(hPackedDIB);
4797 /* Cleanup if GetDIBits failed */
4798 if (nLinesCopied != bmp.bmHeight)
4800 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
4801 GlobalFree(hPackedDIB);
4802 hPackedDIB = 0;
4804 return hPackedDIB;
4808 /**************************************************************************
4809 * X11DRV_DIB_CreateDIBFromPixmap
4811 * Allocates a packed DIB and copies the Pixmap data into it.
4812 * The Pixmap passed in is deleted after the conversion.
4814 HGLOBAL X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap, HDC hdc)
4816 HDC hdcMem;
4817 X_PHYSBITMAP *physBitmap;
4818 HBITMAP hBmp = 0, old;
4819 HGLOBAL hPackedDIB = 0;
4820 Window root;
4821 int x,y; /* Unused */
4822 unsigned border_width; /* Unused */
4823 unsigned int depth, width, height;
4825 /* Get the Pixmap dimensions and bit depth */
4826 wine_tsx11_lock();
4827 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
4828 &border_width, &depth)) depth = 0;
4829 wine_tsx11_unlock();
4830 if (!depth) return 0;
4832 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
4833 width, height, depth);
4836 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
4837 * and make it a container for the pixmap passed.
4839 hBmp = CreateBitmap( width, height, 1, depth, NULL );
4841 /* force bitmap to be owned by a screen DC */
4842 hdcMem = CreateCompatibleDC( hdc );
4843 old = SelectObject( hdcMem, hBmp );
4845 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4847 wine_tsx11_lock();
4848 if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
4849 physBitmap->pixmap = pixmap;
4850 wine_tsx11_unlock();
4852 SelectObject( hdcMem, old );
4853 DeleteDC( hdcMem );
4856 * Create a packed DIB from the Pixmap wrapper bitmap created above.
4857 * A packed DIB contains a BITMAPINFO structure followed immediately by
4858 * an optional color palette and the pixel data.
4860 hPackedDIB = X11DRV_DIB_CreateDIBFromBitmap(hdc, hBmp);
4862 /* We can now get rid of the HBITMAP wrapper we created earlier.
4863 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
4865 DeleteObject(hBmp);
4867 TRACE("\tReturning packed DIB %p\n", hPackedDIB);
4868 return hPackedDIB;
4872 /**************************************************************************
4873 * X11DRV_DIB_CreatePixmapFromDIB
4875 * Creates a Pixmap from a packed DIB
4877 Pixmap X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB, HDC hdc )
4879 Pixmap pixmap;
4880 X_PHYSBITMAP *physBitmap;
4881 HBITMAP hBmp;
4882 LPBITMAPINFO pbmi;
4884 /* Create a DDB from the DIB */
4886 pbmi = GlobalLock(hPackedDIB);
4887 hBmp = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT,
4888 (LPBYTE)pbmi + X11DRV_DIB_BitmapInfoSize( pbmi, DIB_RGB_COLORS ),
4889 pbmi, DIB_RGB_COLORS);
4890 GlobalUnlock(hPackedDIB);
4892 /* clear the physBitmap so that we can steal its pixmap */
4893 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4894 pixmap = physBitmap->pixmap;
4895 physBitmap->pixmap = 0;
4897 /* Delete the DDB we created earlier now that we have stolen its pixmap */
4898 DeleteObject(hBmp);
4900 TRACE("Returning Pixmap %ld\n", pixmap);
4901 return pixmap;