winex11.drv: Rename X11DRV_DIB_BitmapInfoSize to bitmap_info_size and fix to take...
[wine.git] / dlls / winex11.drv / dib.c
blob81345a9eed8ff62f414e4f4868f4e23e973b74c0
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"
22 #include "wine/port.h"
24 #include <X11/Xlib.h>
25 #ifdef HAVE_LIBXXSHM
26 #include <X11/extensions/XShm.h>
27 # ifdef HAVE_SYS_SHM_H
28 # include <sys/shm.h>
29 # endif
30 # ifdef HAVE_SYS_IPC_H
31 # include <sys/ipc.h>
32 # endif
33 #endif /* defined(HAVE_LIBXXSHM) */
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wingdi.h"
41 #include "x11drv.h"
42 #include "wine/exception.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
47 static struct list dibs_list = LIST_INIT(dibs_list);
49 static CRITICAL_SECTION dibs_cs;
50 static CRITICAL_SECTION_DEBUG dibs_cs_debug =
52 0, 0, &dibs_cs,
53 { &dibs_cs_debug.ProcessLocksList, &dibs_cs_debug.ProcessLocksList },
54 0, 0, { (DWORD_PTR)(__FILE__ ": dibs_cs") }
56 static CRITICAL_SECTION dibs_cs = { &dibs_cs_debug, -1, 0, 0, 0, 0 };
58 static PVOID dibs_handler;
60 static int ximageDepthTable[32];
62 /* This structure holds the arguments for DIB_SetImageBits() */
63 typedef struct
65 X11DRV_PDEVICE *physDev;
66 LPCVOID bits;
67 XImage *image;
68 PALETTEENTRY *palentry;
69 int lines;
70 DWORD infoWidth;
71 WORD depth;
72 WORD infoBpp;
73 WORD compression;
74 RGBQUAD *colorMap;
75 int nColorMap;
76 Drawable drawable;
77 GC gc;
78 int xSrc;
79 int ySrc;
80 int xDest;
81 int yDest;
82 int width;
83 int height;
84 DWORD rMask;
85 DWORD gMask;
86 DWORD bMask;
87 BOOL useShm;
88 int dibpitch;
89 DWORD sizeImage;
90 } X11DRV_DIB_IMAGEBITS_DESCR;
93 enum Rle_EscapeCodes
95 RLE_EOL = 0, /* End of line */
96 RLE_END = 1, /* End of bitmap */
97 RLE_DELTA = 2 /* Delta */
101 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *,INT);
102 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *,INT);
103 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *,BOOL);
106 Some of the following helper functions are duplicated in
107 dlls/gdi/dib.c
110 /***********************************************************************
111 * DIB_DoProtectDIBSection
113 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP *physBitmap, DWORD new_prot )
115 DWORD old_prot;
117 VirtualProtect(physBitmap->base, physBitmap->size, new_prot, &old_prot);
118 TRACE("Changed protection from %d to %d\n", old_prot, new_prot);
121 /***********************************************************************
122 * X11DRV_DIB_GetXImageWidthBytes
124 * Return the width of an X image in bytes
126 static inline int X11DRV_DIB_GetXImageWidthBytes( int width, int depth )
128 if (!depth || depth > 32) goto error;
130 if (!ximageDepthTable[depth-1])
132 XImage *testimage = XCreateImage( gdi_display, visual, depth,
133 ZPixmap, 0, NULL, 1, 1, 32, 20 );
134 if (testimage)
136 ximageDepthTable[depth-1] = testimage->bits_per_pixel;
137 XDestroyImage( testimage );
139 else ximageDepthTable[depth-1] = -1;
141 if (ximageDepthTable[depth-1] != -1)
142 return (4 * ((width * ximageDepthTable[depth-1] + 31) / 32));
144 error:
145 WARN( "(%d): Unsupported depth\n", depth );
146 return 4 * width;
150 /***********************************************************************
151 * X11DRV_DIB_GetDIBWidthBytes
153 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
155 static int X11DRV_DIB_GetDIBWidthBytes( int width, int depth )
157 int words;
159 switch(depth)
161 case 1: words = (width + 31) / 32; break;
162 case 4: words = (width + 7) / 8; break;
163 case 8: words = (width + 3) / 4; break;
164 case 15:
165 case 16: words = (width + 1) / 2; break;
166 case 24: words = (width * 3 + 3) / 4; break;
167 default:
168 WARN("(%d): Unsupported depth\n", depth );
169 /* fall through */
170 case 32:
171 words = width;
173 return 4 * words;
177 /***********************************************************************
178 * X11DRV_DIB_GetDIBImageBytes
180 * Return the number of bytes used to hold the image in a DIB bitmap.
182 static int X11DRV_DIB_GetDIBImageBytes( int width, int height, int depth )
184 return X11DRV_DIB_GetDIBWidthBytes( width, depth ) * abs( height );
188 /***********************************************************************
189 * bitmap_info_size
191 * Return the size of the bitmap info structure including color table.
193 int bitmap_info_size( const BITMAPINFO * info, WORD coloruse )
195 unsigned int colors, masks = 0;
197 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
199 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)info;
200 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
201 return sizeof(BITMAPCOREHEADER) + colors *
202 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
204 else /* assume BITMAPINFOHEADER */
206 colors = info->bmiHeader.biClrUsed;
207 if (!colors && (info->bmiHeader.biBitCount <= 8))
208 colors = 1 << info->bmiHeader.biBitCount;
209 if (info->bmiHeader.biCompression == BI_BITFIELDS) masks = 3;
210 return sizeof(BITMAPINFOHEADER) + masks * sizeof(DWORD) + colors *
211 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
216 /***********************************************************************
217 * X11DRV_DIB_CreateXImage
219 * Create an X image.
221 XImage *X11DRV_DIB_CreateXImage( int width, int height, int depth )
223 int width_bytes;
224 XImage *image;
226 wine_tsx11_lock();
227 width_bytes = X11DRV_DIB_GetXImageWidthBytes( width, depth );
228 image = XCreateImage( gdi_display, visual, depth, ZPixmap, 0,
229 calloc( height, width_bytes ),
230 width, height, 32, width_bytes );
231 wine_tsx11_unlock();
232 return image;
236 /***********************************************************************
237 * DIB_GetBitmapInfoEx
239 * Get the info from a bitmap header.
240 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
242 static int DIB_GetBitmapInfoEx( const BITMAPINFOHEADER *header, LONG *width,
243 LONG *height, WORD *planes, WORD *bpp,
244 WORD *compr, DWORD *size )
246 if (header->biSize == sizeof(BITMAPCOREHEADER))
248 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
249 *width = core->bcWidth;
250 *height = core->bcHeight;
251 *planes = core->bcPlanes;
252 *bpp = core->bcBitCount;
253 *compr = 0;
254 *size = 0;
255 return 0;
257 if (header->biSize >= sizeof(BITMAPINFOHEADER))
259 *width = header->biWidth;
260 *height = header->biHeight;
261 *planes = header->biPlanes;
262 *bpp = header->biBitCount;
263 *compr = header->biCompression;
264 *size = header->biSizeImage;
265 return 1;
267 ERR("(%d): unknown/wrong size for header\n", header->biSize );
268 return -1;
272 /***********************************************************************
273 * X11DRV_DIB_GetColorCount
275 * Computes the number of colors for the bitmap palette.
276 * Should not be called for a >8-bit deep bitmap.
278 static unsigned int X11DRV_DIB_GetColorCount(const BITMAPINFO *info)
280 unsigned int colors;
281 BOOL core_info = info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER);
283 if (core_info)
285 colors = 1 << ((const BITMAPCOREINFO*)info)->bmciHeader.bcBitCount;
287 else
289 colors = info->bmiHeader.biClrUsed;
290 if (!colors) colors = 1 << info->bmiHeader.biBitCount;
292 if (colors > 256)
294 ERR("called with >256 colors!\n");
295 colors = 0;
297 return colors;
300 /***********************************************************************
301 * DIB_GetBitmapInfo
303 * Get the info from a bitmap header.
304 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
306 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, LONG *width,
307 LONG *height, WORD *bpp, WORD *compr )
309 WORD planes;
310 DWORD size;
312 return DIB_GetBitmapInfoEx( header, width, height, &planes, bpp, compr, &size);
316 static inline BOOL colour_is_brighter(RGBQUAD c1, RGBQUAD c2)
318 return (c1.rgbRed * c1.rgbRed + c1.rgbGreen * c1.rgbGreen + c1.rgbBlue * c1.rgbBlue) >
319 (c2.rgbRed * c2.rgbRed + c2.rgbGreen * c2.rgbGreen + c2.rgbBlue * c2.rgbBlue);
322 /***********************************************************************
323 * X11DRV_DIB_GenColorMap
325 * Fills the color map of a bitmap palette. Should not be called
326 * for a >8-bit deep bitmap.
328 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE *physDev, int *colorMapping,
329 WORD coloruse, WORD depth, BOOL quads,
330 const void *colorPtr, int start, int end )
332 int i;
334 if (coloruse == DIB_RGB_COLORS)
336 if (quads)
338 const RGBQUAD * rgb = (const RGBQUAD *)colorPtr;
340 if (depth == 1) /* Monochrome */
342 BOOL invert = FALSE;
343 RGBQUAD table[2];
345 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
346 invert = !colour_is_brighter(table[1], table[0]);
348 for (i = start; i < end; i++, rgb++)
349 colorMapping[i] = ((rgb->rgbRed + rgb->rgbGreen +
350 rgb->rgbBlue > 255*3/2 && !invert) ||
351 (rgb->rgbRed + rgb->rgbGreen +
352 rgb->rgbBlue <= 255*3/2 && invert));
354 else
355 for (i = start; i < end; i++, rgb++)
356 colorMapping[i] = X11DRV_PALETTE_ToPhysical( NULL, RGB(rgb->rgbRed,
357 rgb->rgbGreen,
358 rgb->rgbBlue));
360 else
362 const RGBTRIPLE * rgb = (const RGBTRIPLE *)colorPtr;
364 if (depth == 1) /* Monochrome */
366 BOOL invert = FALSE;
367 RGBQUAD table[2];
369 if (GetDIBColorTable( physDev->hdc, 0, 2, table ) == 2)
370 invert = !colour_is_brighter(table[1], table[0]);
372 for (i = start; i < end; i++, rgb++)
373 colorMapping[i] = ((rgb->rgbtRed + rgb->rgbtGreen +
374 rgb->rgbtBlue > 255*3/2 && !invert) ||
375 (rgb->rgbtRed + rgb->rgbtGreen +
376 rgb->rgbtBlue <= 255*3/2 && invert));
378 else
379 for (i = start; i < end; i++, rgb++)
380 colorMapping[i] = X11DRV_PALETTE_ToPhysical( NULL, RGB(rgb->rgbtRed,
381 rgb->rgbtGreen,
382 rgb->rgbtBlue));
385 else /* DIB_PAL_COLORS */
387 const WORD * index = (const WORD *)colorPtr;
389 for (i = start; i < end; i++, index++)
390 colorMapping[i] = X11DRV_PALETTE_ToPhysical( physDev, PALETTEINDEX(*index) );
393 return colorMapping;
396 /***********************************************************************
397 * X11DRV_DIB_BuildColorMap
399 * Build the color map from the bitmap palette. Should not be called
400 * for a >8-bit deep bitmap.
402 static int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE *physDev, WORD coloruse, WORD depth,
403 const BITMAPINFO *info, int *nColors )
405 BOOL isInfo;
406 const void *colorPtr;
407 int *colorMapping;
410 *nColors = X11DRV_DIB_GetColorCount(info);
411 if (!*nColors) return NULL;
413 isInfo = info->bmiHeader.biSize != sizeof(BITMAPCOREHEADER);
414 colorPtr = (const BYTE*)info + (WORD)info->bmiHeader.biSize;
415 if (!(colorMapping = HeapAlloc(GetProcessHeap(), 0, *nColors * sizeof(int) )))
416 return NULL;
418 return X11DRV_DIB_GenColorMap( physDev, colorMapping, coloruse, depth,
419 isInfo, colorPtr, 0, *nColors);
422 /***********************************************************************
423 * X11DRV_DIB_MapColor
425 static int X11DRV_DIB_MapColor( int *physMap, int nPhysMap, int phys, int oldcol )
427 unsigned int color;
429 if ((oldcol < nPhysMap) && (physMap[oldcol] == phys))
430 return oldcol;
432 for (color = 0; color < nPhysMap; color++)
433 if (physMap[color] == phys)
434 return color;
436 WARN("Strange color %08x\n", phys);
437 return 0;
441 /*********************************************************************
442 * X11DRV_DIB_GetNearestIndex
444 * Helper for X11DRV_DIB_GetDIBits.
445 * Returns the nearest colour table index for a given RGB.
446 * Nearest is defined by minimizing the sum of the squares.
448 static INT X11DRV_DIB_GetNearestIndex(RGBQUAD *colormap, int numColors, BYTE r, BYTE g, BYTE b)
450 INT i, best = -1, diff, bestdiff = -1;
451 RGBQUAD *color;
453 for(color = colormap, i = 0; i < numColors; color++, i++) {
454 diff = (r - color->rgbRed) * (r - color->rgbRed) +
455 (g - color->rgbGreen) * (g - color->rgbGreen) +
456 (b - color->rgbBlue) * (b - color->rgbBlue);
457 if(diff == 0)
458 return i;
459 if(best == -1 || diff < bestdiff) {
460 best = i;
461 bestdiff = diff;
464 return best;
466 /*********************************************************************
467 * X11DRV_DIB_MaskToShift
469 * Helper for X11DRV_DIB_GetDIBits.
470 * Returns the by how many bits to shift a given color so that it is
471 * in the proper position.
473 INT X11DRV_DIB_MaskToShift(DWORD mask)
475 int shift;
477 if (mask==0)
478 return 0;
480 shift=0;
481 while ((mask&1)==0) {
482 mask>>=1;
483 shift++;
485 return shift;
488 /***********************************************************************
489 * X11DRV_DIB_CheckMask
491 * Check RGB mask if it is either 0 or matches visual's mask.
493 static inline int X11DRV_DIB_CheckMask(int red_mask, int green_mask, int blue_mask)
495 return ( red_mask == 0 && green_mask == 0 && blue_mask == 0 ) ||
496 ( red_mask == visual->red_mask && green_mask == visual->green_mask &&
497 blue_mask == visual->blue_mask );
500 /***********************************************************************
501 * X11DRV_DIB_SetImageBits_1
503 * SetDIBits for a 1-bit deep DIB.
505 static void X11DRV_DIB_SetImageBits_1( int lines, const BYTE *srcbits,
506 DWORD srcwidth, DWORD dstwidth, int left,
507 int *colors, XImage *bmpImage, DWORD linebytes)
509 int h, width;
510 const BYTE* srcbyte;
511 BYTE srcval, extra;
512 DWORD i, x;
514 if (lines < 0 ) {
515 lines = -lines;
516 srcbits = srcbits + linebytes * (lines - 1);
517 linebytes = -linebytes;
520 if ((extra = (left & 7)) != 0) {
521 left &= ~7;
522 dstwidth += extra;
524 srcbits += left >> 3;
525 width = min(srcwidth, dstwidth);
527 /* ==== pal 1 dib -> any bmp format ==== */
528 for (h = lines-1; h >=0; h--) {
529 srcbyte=srcbits;
530 for (i = width/8, x = left; i > 0; i--) {
531 srcval=*srcbyte++;
532 XPutPixel( bmpImage, x++, h, colors[ srcval >> 7] );
533 XPutPixel( bmpImage, x++, h, colors[(srcval >> 6) & 1] );
534 XPutPixel( bmpImage, x++, h, colors[(srcval >> 5) & 1] );
535 XPutPixel( bmpImage, x++, h, colors[(srcval >> 4) & 1] );
536 XPutPixel( bmpImage, x++, h, colors[(srcval >> 3) & 1] );
537 XPutPixel( bmpImage, x++, h, colors[(srcval >> 2) & 1] );
538 XPutPixel( bmpImage, x++, h, colors[(srcval >> 1) & 1] );
539 XPutPixel( bmpImage, x++, h, colors[ srcval & 1] );
541 if (width % 8){
542 srcval=*srcbyte;
543 switch (width & 7)
545 case 7: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
546 case 6: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
547 case 5: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
548 case 4: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
549 case 3: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
550 case 2: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]); srcval<<=1;
551 case 1: XPutPixel(bmpImage, x++, h, colors[srcval >> 7]);
554 srcbits += linebytes;
558 /***********************************************************************
559 * X11DRV_DIB_GetImageBits_1
561 * GetDIBits for a 1-bit deep DIB.
563 static void X11DRV_DIB_GetImageBits_1( int lines, BYTE *dstbits,
564 DWORD dstwidth, DWORD srcwidth,
565 RGBQUAD *colors, PALETTEENTRY *srccolors,
566 XImage *bmpImage, DWORD linebytes )
568 DWORD x;
569 int h, width = min(dstwidth, srcwidth);
571 if (lines < 0 ) {
572 lines = -lines;
573 dstbits = dstbits + linebytes * (lines - 1);
574 linebytes = -linebytes;
577 switch (bmpImage->depth)
579 case 1:
580 case 4:
581 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
582 && srccolors) {
583 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
584 BYTE* dstbyte;
586 for (h=lines-1; h>=0; h--) {
587 BYTE dstval;
588 dstbyte=dstbits;
589 dstval=0;
590 for (x=0; x<width; x++) {
591 PALETTEENTRY srcval;
592 srcval=srccolors[XGetPixel(bmpImage, x, h)];
593 dstval|=(X11DRV_DIB_GetNearestIndex
594 (colors, 2,
595 srcval.peRed,
596 srcval.peGreen,
597 srcval.peBlue) << (7 - (x & 7)));
598 if ((x&7)==7) {
599 *dstbyte++=dstval;
600 dstval=0;
603 if ((width&7)!=0) {
604 *dstbyte=dstval;
606 dstbits += linebytes;
608 } else {
609 goto notsupported;
611 break;
613 case 8:
614 if (X11DRV_DIB_CheckMask(bmpImage->red_mask, bmpImage->green_mask, bmpImage->blue_mask)
615 && srccolors) {
616 /* ==== pal 8 bmp -> pal 1 dib ==== */
617 const void* srcbits;
618 const BYTE* srcpixel;
619 BYTE* dstbyte;
621 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
623 for (h=0; h<lines; h++) {
624 BYTE dstval;
625 srcpixel=srcbits;
626 dstbyte=dstbits;
627 dstval=0;
628 for (x=0; x<width; x++) {
629 PALETTEENTRY srcval;
630 srcval=srccolors[*srcpixel++];
631 dstval|=(X11DRV_DIB_GetNearestIndex
632 (colors, 2,
633 srcval.peRed,
634 srcval.peGreen,
635 srcval.peBlue) << (7-(x&7)) );
636 if ((x&7)==7) {
637 *dstbyte++=dstval;
638 dstval=0;
641 if ((width&7)!=0) {
642 *dstbyte=dstval;
644 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
645 dstbits += linebytes;
647 } else {
648 goto notsupported;
650 break;
652 case 15:
653 case 16:
655 const void* srcbits;
656 const WORD* srcpixel;
657 BYTE* dstbyte;
659 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
661 if (bmpImage->green_mask==0x03e0) {
662 if (bmpImage->red_mask==0x7c00) {
663 /* ==== rgb 555 bmp -> pal 1 dib ==== */
664 for (h=0; h<lines; h++) {
665 BYTE dstval;
666 srcpixel=srcbits;
667 dstbyte=dstbits;
668 dstval=0;
669 for (x=0; x<width; x++) {
670 WORD srcval;
671 srcval=*srcpixel++;
672 dstval|=(X11DRV_DIB_GetNearestIndex
673 (colors, 2,
674 ((srcval >> 7) & 0xf8) | /* r */
675 ((srcval >> 12) & 0x07),
676 ((srcval >> 2) & 0xf8) | /* g */
677 ((srcval >> 7) & 0x07),
678 ((srcval << 3) & 0xf8) | /* b */
679 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
680 if ((x&7)==7) {
681 *dstbyte++=dstval;
682 dstval=0;
685 if ((width&7)!=0) {
686 *dstbyte=dstval;
688 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
689 dstbits += linebytes;
691 } else if (bmpImage->blue_mask==0x7c00) {
692 /* ==== bgr 555 bmp -> pal 1 dib ==== */
693 for (h=0; h<lines; h++) {
694 WORD dstval;
695 srcpixel=srcbits;
696 dstbyte=dstbits;
697 dstval=0;
698 for (x=0; x<width; x++) {
699 BYTE srcval;
700 srcval=*srcpixel++;
701 dstval|=(X11DRV_DIB_GetNearestIndex
702 (colors, 2,
703 ((srcval << 3) & 0xf8) | /* r */
704 ((srcval >> 2) & 0x07),
705 ((srcval >> 2) & 0xf8) | /* g */
706 ((srcval >> 7) & 0x07),
707 ((srcval >> 7) & 0xf8) | /* b */
708 ((srcval >> 12) & 0x07) ) << (7-(x&7)) );
709 if ((x&7)==7) {
710 *dstbyte++=dstval;
711 dstval=0;
714 if ((width&7)!=0) {
715 *dstbyte=dstval;
717 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
718 dstbits += linebytes;
720 } else {
721 goto notsupported;
723 } else if (bmpImage->green_mask==0x07e0) {
724 if (bmpImage->red_mask==0xf800) {
725 /* ==== rgb 565 bmp -> pal 1 dib ==== */
726 for (h=0; h<lines; h++) {
727 BYTE dstval;
728 srcpixel=srcbits;
729 dstbyte=dstbits;
730 dstval=0;
731 for (x=0; x<width; x++) {
732 WORD srcval;
733 srcval=*srcpixel++;
734 dstval|=(X11DRV_DIB_GetNearestIndex
735 (colors, 2,
736 ((srcval >> 8) & 0xf8) | /* r */
737 ((srcval >> 13) & 0x07),
738 ((srcval >> 3) & 0xfc) | /* g */
739 ((srcval >> 9) & 0x03),
740 ((srcval << 3) & 0xf8) | /* b */
741 ((srcval >> 2) & 0x07) ) << (7-(x&7)) );
742 if ((x&7)==7) {
743 *dstbyte++=dstval;
744 dstval=0;
747 if ((width&7)!=0) {
748 *dstbyte=dstval;
750 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
751 dstbits += linebytes;
753 } else if (bmpImage->blue_mask==0xf800) {
754 /* ==== bgr 565 bmp -> pal 1 dib ==== */
755 for (h=0; h<lines; h++) {
756 BYTE dstval;
757 srcpixel=srcbits;
758 dstbyte=dstbits;
759 dstval=0;
760 for (x=0; x<width; x++) {
761 WORD srcval;
762 srcval=*srcpixel++;
763 dstval|=(X11DRV_DIB_GetNearestIndex
764 (colors, 2,
765 ((srcval << 3) & 0xf8) | /* r */
766 ((srcval >> 2) & 0x07),
767 ((srcval >> 3) & 0xfc) | /* g */
768 ((srcval >> 9) & 0x03),
769 ((srcval >> 8) & 0xf8) | /* b */
770 ((srcval >> 13) & 0x07) ) << (7-(x&7)) );
771 if ((x&7)==7) {
772 *dstbyte++=dstval;
773 dstval=0;
776 if ((width&7)!=0) {
777 *dstbyte=dstval;
779 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
780 dstbits += linebytes;
782 } else {
783 goto notsupported;
785 } else {
786 goto notsupported;
789 break;
791 case 24:
792 case 32:
794 const void* srcbits;
795 const BYTE *srcbyte;
796 BYTE* dstbyte;
797 int bytes_per_pixel;
799 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
800 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
802 if (bmpImage->green_mask!=0x00ff00 ||
803 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
804 goto notsupported;
805 } else if (bmpImage->blue_mask==0xff) {
806 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
807 for (h=0; h<lines; h++) {
808 BYTE dstval;
809 srcbyte=srcbits;
810 dstbyte=dstbits;
811 dstval=0;
812 for (x=0; x<width; x++) {
813 dstval|=(X11DRV_DIB_GetNearestIndex
814 (colors, 2,
815 srcbyte[2],
816 srcbyte[1],
817 srcbyte[0]) << (7-(x&7)) );
818 srcbyte+=bytes_per_pixel;
819 if ((x&7)==7) {
820 *dstbyte++=dstval;
821 dstval=0;
824 if ((width&7)!=0) {
825 *dstbyte=dstval;
827 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
828 dstbits += linebytes;
830 } else {
831 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
832 for (h=0; h<lines; h++) {
833 BYTE dstval;
834 srcbyte=srcbits;
835 dstbyte=dstbits;
836 dstval=0;
837 for (x=0; x<width; x++) {
838 dstval|=(X11DRV_DIB_GetNearestIndex
839 (colors, 2,
840 srcbyte[0],
841 srcbyte[1],
842 srcbyte[2]) << (7-(x&7)) );
843 srcbyte+=bytes_per_pixel;
844 if ((x&7)==7) {
845 *dstbyte++=dstval;
846 dstval=0;
849 if ((width&7)!=0) {
850 *dstbyte=dstval;
852 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
853 dstbits += linebytes;
857 break;
859 default:
860 notsupported:
862 BYTE* dstbyte;
863 BYTE neg = 0;
864 unsigned long white = (1 << bmpImage->bits_per_pixel) - 1;
866 /* ==== any bmp format -> pal 1 dib ==== */
867 if ((unsigned)colors[0].rgbRed+colors[0].rgbGreen+colors[0].rgbBlue >=
868 (unsigned)colors[1].rgbRed+colors[1].rgbGreen+colors[1].rgbBlue )
869 neg = 1;
871 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB, "
872 "%s color mapping\n",
873 bmpImage->bits_per_pixel, bmpImage->red_mask,
874 bmpImage->green_mask, bmpImage->blue_mask,
875 neg?"negative":"direct" );
877 for (h=lines-1; h>=0; h--) {
878 BYTE dstval;
879 dstbyte=dstbits;
880 dstval=0;
881 for (x=0; x<width; x++) {
882 dstval|=((XGetPixel( bmpImage, x, h) >= white) ^ neg) << (7 - (x&7));
883 if ((x&7)==7) {
884 *dstbyte++=dstval;
885 dstval=0;
888 if ((width&7)!=0) {
889 *dstbyte=dstval;
891 dstbits += linebytes;
894 break;
898 /***********************************************************************
899 * X11DRV_DIB_SetImageBits_4
901 * SetDIBits for a 4-bit deep DIB.
903 static void X11DRV_DIB_SetImageBits_4( int lines, const BYTE *srcbits,
904 DWORD srcwidth, DWORD dstwidth, int left,
905 int *colors, XImage *bmpImage, DWORD linebytes)
907 int h, width;
908 const BYTE* srcbyte;
909 DWORD i, x;
911 if (lines < 0 ) {
912 lines = -lines;
913 srcbits = srcbits + linebytes * (lines - 1);
914 linebytes = -linebytes;
917 if (left & 1) {
918 left--;
919 dstwidth++;
921 srcbits += left >> 1;
922 width = min(srcwidth, dstwidth);
924 /* ==== pal 4 dib -> any bmp format ==== */
925 for (h = lines-1; h >= 0; h--) {
926 srcbyte=srcbits;
927 for (i = width/2, x = left; i > 0; i--) {
928 BYTE srcval=*srcbyte++;
929 XPutPixel( bmpImage, x++, h, colors[srcval >> 4] );
930 XPutPixel( bmpImage, x++, h, colors[srcval & 0x0f] );
932 if (width & 1)
933 XPutPixel( bmpImage, x, h, colors[*srcbyte >> 4] );
934 srcbits += linebytes;
940 /***********************************************************************
941 * X11DRV_DIB_GetImageBits_4
943 * GetDIBits for a 4-bit deep DIB.
945 static void X11DRV_DIB_GetImageBits_4( int lines, BYTE *dstbits,
946 DWORD srcwidth, DWORD dstwidth,
947 RGBQUAD *colors, PALETTEENTRY *srccolors,
948 XImage *bmpImage, DWORD linebytes )
950 DWORD x;
951 int h, width = min(srcwidth, dstwidth);
952 BYTE *bits;
954 if (lines < 0 )
956 lines = -lines;
957 dstbits = dstbits + ( linebytes * (lines-1) );
958 linebytes = -linebytes;
961 bits = dstbits;
963 switch (bmpImage->depth) {
964 case 1:
965 case 4:
966 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
967 && srccolors) {
968 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
969 BYTE* dstbyte;
971 for (h = lines-1; h >= 0; h--) {
972 BYTE dstval;
973 dstbyte=dstbits;
974 dstval=0;
975 for (x = 0; x < width; x++) {
976 PALETTEENTRY srcval;
977 srcval=srccolors[XGetPixel(bmpImage, x, h)];
978 dstval|=(X11DRV_DIB_GetNearestIndex
979 (colors, 16,
980 srcval.peRed,
981 srcval.peGreen,
982 srcval.peBlue) << (4-((x&1)<<2)));
983 if ((x&1)==1) {
984 *dstbyte++=dstval;
985 dstval=0;
988 if ((width&1)!=0) {
989 *dstbyte=dstval;
991 dstbits += linebytes;
993 } else {
994 goto notsupported;
996 break;
998 case 8:
999 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1000 && srccolors) {
1001 /* ==== pal 8 bmp -> pal 4 dib ==== */
1002 const void* srcbits;
1003 const BYTE *srcpixel;
1004 BYTE* dstbyte;
1006 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1007 for (h=0; h<lines; h++) {
1008 BYTE dstval;
1009 srcpixel=srcbits;
1010 dstbyte=dstbits;
1011 dstval=0;
1012 for (x=0; x<width; x++) {
1013 PALETTEENTRY srcval;
1014 srcval = srccolors[*srcpixel++];
1015 dstval|=(X11DRV_DIB_GetNearestIndex
1016 (colors, 16,
1017 srcval.peRed,
1018 srcval.peGreen,
1019 srcval.peBlue) << (4*(1-(x&1))) );
1020 if ((x&1)==1) {
1021 *dstbyte++=dstval;
1022 dstval=0;
1025 if ((width&1)!=0) {
1026 *dstbyte=dstval;
1028 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1029 dstbits += linebytes;
1031 } else {
1032 goto notsupported;
1034 break;
1036 case 15:
1037 case 16:
1039 const void* srcbits;
1040 const WORD* srcpixel;
1041 BYTE* dstbyte;
1043 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1045 if (bmpImage->green_mask==0x03e0) {
1046 if (bmpImage->red_mask==0x7c00) {
1047 /* ==== rgb 555 bmp -> pal 4 dib ==== */
1048 for (h=0; h<lines; h++) {
1049 BYTE dstval;
1050 srcpixel=srcbits;
1051 dstbyte=dstbits;
1052 dstval=0;
1053 for (x=0; x<width; x++) {
1054 WORD srcval;
1055 srcval=*srcpixel++;
1056 dstval|=(X11DRV_DIB_GetNearestIndex
1057 (colors, 16,
1058 ((srcval >> 7) & 0xf8) | /* r */
1059 ((srcval >> 12) & 0x07),
1060 ((srcval >> 2) & 0xf8) | /* g */
1061 ((srcval >> 7) & 0x07),
1062 ((srcval << 3) & 0xf8) | /* b */
1063 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1064 if ((x&1)==1) {
1065 *dstbyte++=dstval;
1066 dstval=0;
1069 if ((width&1)!=0) {
1070 *dstbyte=dstval;
1072 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1073 dstbits += linebytes;
1075 } else if (bmpImage->blue_mask==0x7c00) {
1076 /* ==== bgr 555 bmp -> pal 4 dib ==== */
1077 for (h=0; h<lines; h++) {
1078 WORD dstval;
1079 srcpixel=srcbits;
1080 dstbyte=dstbits;
1081 dstval=0;
1082 for (x=0; x<width; x++) {
1083 WORD srcval;
1084 srcval=*srcpixel++;
1085 dstval|=(X11DRV_DIB_GetNearestIndex
1086 (colors, 16,
1087 ((srcval << 3) & 0xf8) | /* r */
1088 ((srcval >> 2) & 0x07),
1089 ((srcval >> 2) & 0xf8) | /* g */
1090 ((srcval >> 7) & 0x07),
1091 ((srcval >> 7) & 0xf8) | /* b */
1092 ((srcval >> 12) & 0x07) ) << ((1-(x&1))<<2) );
1093 if ((x&1)==1) {
1094 *dstbyte++=dstval;
1095 dstval=0;
1098 if ((width&1)!=0) {
1099 *dstbyte=dstval;
1101 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1102 dstbits += linebytes;
1104 } else {
1105 goto notsupported;
1107 } else if (bmpImage->green_mask==0x07e0) {
1108 if (bmpImage->red_mask==0xf800) {
1109 /* ==== rgb 565 bmp -> pal 4 dib ==== */
1110 for (h=0; h<lines; h++) {
1111 BYTE dstval;
1112 srcpixel=srcbits;
1113 dstbyte=dstbits;
1114 dstval=0;
1115 for (x=0; x<width; x++) {
1116 WORD srcval;
1117 srcval=*srcpixel++;
1118 dstval|=(X11DRV_DIB_GetNearestIndex
1119 (colors, 16,
1120 ((srcval >> 8) & 0xf8) | /* r */
1121 ((srcval >> 13) & 0x07),
1122 ((srcval >> 3) & 0xfc) | /* g */
1123 ((srcval >> 9) & 0x03),
1124 ((srcval << 3) & 0xf8) | /* b */
1125 ((srcval >> 2) & 0x07) ) << ((1-(x&1))<<2) );
1126 if ((x&1)==1) {
1127 *dstbyte++=dstval;
1128 dstval=0;
1131 if ((width&1)!=0) {
1132 *dstbyte=dstval;
1134 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1135 dstbits += linebytes;
1137 } else if (bmpImage->blue_mask==0xf800) {
1138 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1139 for (h=0; h<lines; h++) {
1140 WORD dstval;
1141 srcpixel=srcbits;
1142 dstbyte=dstbits;
1143 dstval=0;
1144 for (x=0; x<width; x++) {
1145 WORD srcval;
1146 srcval=*srcpixel++;
1147 dstval|=(X11DRV_DIB_GetNearestIndex
1148 (colors, 16,
1149 ((srcval << 3) & 0xf8) | /* r */
1150 ((srcval >> 2) & 0x07),
1151 ((srcval >> 3) & 0xfc) | /* g */
1152 ((srcval >> 9) & 0x03),
1153 ((srcval >> 8) & 0xf8) | /* b */
1154 ((srcval >> 13) & 0x07) ) << ((1-(x&1))<<2) );
1155 if ((x&1)==1) {
1156 *dstbyte++=dstval;
1157 dstval=0;
1160 if ((width&1)!=0) {
1161 *dstbyte=dstval;
1163 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1164 dstbits += linebytes;
1166 } else {
1167 goto notsupported;
1169 } else {
1170 goto notsupported;
1173 break;
1175 case 24:
1176 if (bmpImage->bits_per_pixel==24) {
1177 const void* srcbits;
1178 const BYTE *srcbyte;
1179 BYTE* dstbyte;
1181 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1183 if (bmpImage->green_mask!=0x00ff00 ||
1184 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1185 goto notsupported;
1186 } else if (bmpImage->blue_mask==0xff) {
1187 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1188 for (h=0; h<lines; h++) {
1189 srcbyte=srcbits;
1190 dstbyte=dstbits;
1191 for (x=0; x<width/2; x++) {
1192 /* Do 2 pixels at a time */
1193 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1194 (colors, 16,
1195 srcbyte[2],
1196 srcbyte[1],
1197 srcbyte[0]) << 4) |
1198 X11DRV_DIB_GetNearestIndex
1199 (colors, 16,
1200 srcbyte[5],
1201 srcbyte[4],
1202 srcbyte[3]);
1203 srcbyte+=6;
1205 if (width&1) {
1206 /* And then the odd pixel */
1207 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1208 (colors, 16,
1209 srcbyte[2],
1210 srcbyte[1],
1211 srcbyte[0]) << 4);
1213 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1214 dstbits += linebytes;
1216 } else {
1217 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1218 for (h=0; h<lines; h++) {
1219 srcbyte=srcbits;
1220 dstbyte=dstbits;
1221 for (x=0; x<width/2; x++) {
1222 /* Do 2 pixels at a time */
1223 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1224 (colors, 16,
1225 srcbyte[0],
1226 srcbyte[1],
1227 srcbyte[2]) << 4) |
1228 X11DRV_DIB_GetNearestIndex
1229 (colors, 16,
1230 srcbyte[3],
1231 srcbyte[4],
1232 srcbyte[5]);
1233 srcbyte+=6;
1235 if (width&1) {
1236 /* And then the odd pixel */
1237 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1238 (colors, 16,
1239 srcbyte[0],
1240 srcbyte[1],
1241 srcbyte[2]) << 4);
1243 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1244 dstbits += linebytes;
1247 break;
1249 /* Fall through */
1251 case 32:
1253 const void* srcbits;
1254 const BYTE *srcbyte;
1255 BYTE* dstbyte;
1257 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1259 if (bmpImage->green_mask!=0x00ff00 ||
1260 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1261 goto notsupported;
1262 } else if (bmpImage->blue_mask==0xff) {
1263 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1264 for (h=0; h<lines; h++) {
1265 srcbyte=srcbits;
1266 dstbyte=dstbits;
1267 for (x=0; x<width/2; x++) {
1268 /* Do 2 pixels at a time */
1269 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1270 (colors, 16,
1271 srcbyte[2],
1272 srcbyte[1],
1273 srcbyte[0]) << 4) |
1274 X11DRV_DIB_GetNearestIndex
1275 (colors, 16,
1276 srcbyte[6],
1277 srcbyte[5],
1278 srcbyte[4]);
1279 srcbyte+=8;
1281 if (width&1) {
1282 /* And then the odd pixel */
1283 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1284 (colors, 16,
1285 srcbyte[2],
1286 srcbyte[1],
1287 srcbyte[0]) << 4);
1289 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1290 dstbits += linebytes;
1292 } else {
1293 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1294 for (h=0; h<lines; h++) {
1295 srcbyte=srcbits;
1296 dstbyte=dstbits;
1297 for (x=0; x<width/2; x++) {
1298 /* Do 2 pixels at a time */
1299 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1300 (colors, 16,
1301 srcbyte[0],
1302 srcbyte[1],
1303 srcbyte[2]) << 4) |
1304 X11DRV_DIB_GetNearestIndex
1305 (colors, 16,
1306 srcbyte[4],
1307 srcbyte[5],
1308 srcbyte[6]);
1309 srcbyte+=8;
1311 if (width&1) {
1312 /* And then the odd pixel */
1313 *dstbyte++=(X11DRV_DIB_GetNearestIndex
1314 (colors, 16,
1315 srcbyte[0],
1316 srcbyte[1],
1317 srcbyte[2]) << 4);
1319 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1320 dstbits += linebytes;
1324 break;
1326 default:
1327 notsupported:
1329 BYTE* dstbyte;
1331 /* ==== any bmp format -> pal 4 dib ==== */
1332 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1333 bmpImage->bits_per_pixel, bmpImage->red_mask,
1334 bmpImage->green_mask, bmpImage->blue_mask );
1335 for (h=lines-1; h>=0; h--) {
1336 dstbyte=dstbits;
1337 for (x=0; x<(width & ~1); x+=2) {
1338 *dstbyte++=(X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4) |
1339 X11DRV_DIB_MapColor((int*)colors, 16, XGetPixel(bmpImage, x+1, h), 0);
1341 if (width & 1) {
1342 *dstbyte=(X11DRV_DIB_MapColor((int *)colors, 16, XGetPixel(bmpImage, x, h), 0) << 4);
1344 dstbits += linebytes;
1347 break;
1351 /***********************************************************************
1352 * X11DRV_DIB_SetImageBits_RLE4
1354 * SetDIBits for a 4-bit deep compressed DIB.
1356 static void X11DRV_DIB_SetImageBits_RLE4( int lines, const BYTE *bits,
1357 DWORD srcwidth, DWORD dstwidth,
1358 int left, int *colors,
1359 XImage *bmpImage )
1361 unsigned int x = 0, width = min(srcwidth, dstwidth);
1362 int y = lines - 1, c, length;
1363 const BYTE *begin = bits;
1365 while (y >= 0)
1367 length = *bits++;
1368 if (length) { /* encoded */
1369 c = *bits++;
1370 while (length--) {
1371 if (x >= (left + width)) break;
1372 if( x >= left) XPutPixel(bmpImage, x, y, colors[c >> 4]);
1373 x++;
1374 if (!length--) break;
1375 if (x >= (left + width)) break;
1376 if( x >= left) XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1377 x++;
1379 } else {
1380 length = *bits++;
1381 switch (length)
1383 case RLE_EOL:
1384 x = 0;
1385 y--;
1386 break;
1388 case RLE_END:
1389 return;
1391 case RLE_DELTA:
1392 x += *bits++;
1393 y -= *bits++;
1394 break;
1396 default: /* absolute */
1397 while (length--) {
1398 c = *bits++;
1399 if (x >= left && x < (left + width))
1400 XPutPixel(bmpImage, x, y, colors[c >> 4]);
1401 x++;
1402 if (!length--) break;
1403 if (x >= left && x < (left + width))
1404 XPutPixel(bmpImage, x, y, colors[c & 0xf]);
1405 x++;
1407 if ((bits - begin) & 1)
1408 bits++;
1416 /***********************************************************************
1417 * X11DRV_DIB_SetImageBits_8
1419 * SetDIBits for an 8-bit deep DIB.
1421 static void X11DRV_DIB_SetImageBits_8( int lines, const BYTE *srcbits,
1422 DWORD srcwidth, DWORD dstwidth, int left,
1423 const int *colors, XImage *bmpImage,
1424 DWORD linebytes )
1426 DWORD x;
1427 int h, width = min(srcwidth, dstwidth);
1428 const BYTE* srcbyte;
1429 BYTE* dstbits;
1431 if (lines < 0 )
1433 lines = -lines;
1434 srcbits = srcbits + linebytes * (lines-1);
1435 linebytes = -linebytes;
1437 srcbits += left;
1438 srcbyte = srcbits;
1440 switch (bmpImage->depth) {
1441 case 15:
1442 case 16:
1443 /* Some X servers might have 32 bit/ 16bit deep pixel */
1444 if (lines && width && (bmpImage->bits_per_pixel == 16) &&
1445 (ImageByteOrder(gdi_display)==LSBFirst) )
1447 /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1448 dstbits=(BYTE*)bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1449 for (h = lines ; h--; ) {
1450 #if defined(__i386__) && defined(__GNUC__)
1451 int _cl1,_cl2; /* temp outputs for asm below */
1452 /* Borrowed from DirectDraw */
1453 __asm__ __volatile__(
1454 "xor %%eax,%%eax\n"
1455 "cld\n"
1456 "1:\n"
1457 " lodsb\n"
1458 " movw (%%edx,%%eax,4),%%ax\n"
1459 " stosw\n"
1460 " xor %%eax,%%eax\n"
1461 " loop 1b\n"
1462 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1463 :"S" (srcbyte),
1464 "D" (dstbits),
1465 "c" (width),
1466 "d" (colors)
1467 :"eax", "cc", "memory"
1469 #else
1470 DWORD* dstpixel=(DWORD*)dstbits;
1471 for (x=0; x<width/2; x++) {
1472 /* Do 2 pixels at a time */
1473 *dstpixel++=(colors[srcbyte[1]] << 16) | colors[srcbyte[0]];
1474 srcbyte+=2;
1476 if (width&1) {
1477 /* And then the odd pixel */
1478 *((WORD*)dstpixel)=colors[srcbyte[0]];
1480 #endif
1481 srcbyte = (srcbits += linebytes);
1482 dstbits -= bmpImage->bytes_per_line;
1484 return;
1486 break;
1487 case 24:
1488 case 32:
1489 if (lines && width && (bmpImage->bits_per_pixel == 32) &&
1490 (ImageByteOrder(gdi_display)==LSBFirst) )
1492 dstbits=(BYTE*)bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
1493 /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1494 for (h = lines ; h--; ) {
1495 #if defined(__i386__) && defined(__GNUC__)
1496 int _cl1,_cl2; /* temp outputs for asm below */
1497 /* Borrowed from DirectDraw */
1498 __asm__ __volatile__(
1499 "xor %%eax,%%eax\n"
1500 "cld\n"
1501 "1:\n"
1502 " lodsb\n"
1503 " movl (%%edx,%%eax,4),%%eax\n"
1504 " stosl\n"
1505 " xor %%eax,%%eax\n"
1506 " loop 1b\n"
1507 :"=S" (srcbyte), "=D" (_cl1), "=c" (_cl2)
1508 :"S" (srcbyte),
1509 "D" (dstbits),
1510 "c" (width),
1511 "d" (colors)
1512 :"eax", "cc", "memory"
1514 #else
1515 DWORD* dstpixel=(DWORD*)dstbits;
1516 for (x=0; x<width; x++) {
1517 *dstpixel++=colors[*srcbyte++];
1519 #endif
1520 srcbyte = (srcbits += linebytes);
1521 dstbits -= bmpImage->bytes_per_line;
1523 return;
1525 break;
1526 default:
1527 break; /* use slow generic case below */
1530 /* ==== pal 8 dib -> any bmp format ==== */
1531 for (h=lines-1; h>=0; h--) {
1532 for (x=left; x<width+left; x++) {
1533 XPutPixel(bmpImage, x, h, colors[*srcbyte++]);
1535 srcbyte = (srcbits += linebytes);
1539 /***********************************************************************
1540 * X11DRV_DIB_GetImageBits_8
1542 * GetDIBits for an 8-bit deep DIB.
1544 static void X11DRV_DIB_GetImageBits_8( int lines, BYTE *dstbits,
1545 DWORD srcwidth, DWORD dstwidth,
1546 RGBQUAD *colors, PALETTEENTRY *srccolors,
1547 XImage *bmpImage, DWORD linebytes )
1549 DWORD x;
1550 int h, width = min(srcwidth, dstwidth);
1551 BYTE* dstbyte;
1553 if (lines < 0 )
1555 lines = -lines;
1556 dstbits = dstbits + ( linebytes * (lines-1) );
1557 linebytes = -linebytes;
1561 * Hack for now
1562 * This condition is true when GetImageBits has been called by
1563 * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1564 * 256 colormaps, so we'll just use it for GetDIBits calls.
1565 * (In some cases, in an updateDIBSection, the returned colors are bad too)
1567 if (!srccolors) goto updatesection;
1569 switch (bmpImage->depth) {
1570 case 1:
1571 case 4:
1572 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1573 && srccolors) {
1575 /* ==== pal 1 bmp -> pal 8 dib ==== */
1576 /* ==== pal 4 bmp -> pal 8 dib ==== */
1577 for (h=lines-1; h>=0; h--) {
1578 dstbyte=dstbits;
1579 for (x=0; x<width; x++) {
1580 PALETTEENTRY srcval;
1581 srcval=srccolors[XGetPixel(bmpImage, x, h)];
1582 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1583 srcval.peRed,
1584 srcval.peGreen,
1585 srcval.peBlue);
1587 dstbits += linebytes;
1589 } else {
1590 goto notsupported;
1592 break;
1594 case 8:
1595 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
1596 && srccolors) {
1597 /* ==== pal 8 bmp -> pal 8 dib ==== */
1598 const void* srcbits;
1599 const BYTE* srcpixel;
1601 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1602 for (h=0; h<lines; h++) {
1603 srcpixel=srcbits;
1604 dstbyte=dstbits;
1605 for (x = 0; x < width; x++) {
1606 PALETTEENTRY srcval;
1607 srcval=srccolors[*srcpixel++];
1608 *dstbyte++=X11DRV_DIB_GetNearestIndex(colors, 256,
1609 srcval.peRed,
1610 srcval.peGreen,
1611 srcval.peBlue);
1613 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1614 dstbits += linebytes;
1616 } else {
1617 goto notsupported;
1619 break;
1621 case 15:
1622 case 16:
1624 const void* srcbits;
1625 const WORD* srcpixel;
1626 BYTE* dstbyte;
1628 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1630 if (bmpImage->green_mask==0x03e0) {
1631 if (bmpImage->red_mask==0x7c00) {
1632 /* ==== rgb 555 bmp -> pal 8 dib ==== */
1633 for (h=0; h<lines; h++) {
1634 srcpixel=srcbits;
1635 dstbyte=dstbits;
1636 for (x=0; x<width; x++) {
1637 WORD srcval;
1638 srcval=*srcpixel++;
1639 *dstbyte++=X11DRV_DIB_GetNearestIndex
1640 (colors, 256,
1641 ((srcval >> 7) & 0xf8) | /* r */
1642 ((srcval >> 12) & 0x07),
1643 ((srcval >> 2) & 0xf8) | /* g */
1644 ((srcval >> 7) & 0x07),
1645 ((srcval << 3) & 0xf8) | /* b */
1646 ((srcval >> 2) & 0x07) );
1648 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1649 dstbits += linebytes;
1651 } else if (bmpImage->blue_mask==0x7c00) {
1652 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1653 for (h=0; h<lines; h++) {
1654 srcpixel=srcbits;
1655 dstbyte=dstbits;
1656 for (x=0; x<width; x++) {
1657 WORD srcval;
1658 srcval=*srcpixel++;
1659 *dstbyte++=X11DRV_DIB_GetNearestIndex
1660 (colors, 256,
1661 ((srcval << 3) & 0xf8) | /* r */
1662 ((srcval >> 2) & 0x07),
1663 ((srcval >> 2) & 0xf8) | /* g */
1664 ((srcval >> 7) & 0x07),
1665 ((srcval >> 7) & 0xf8) | /* b */
1666 ((srcval >> 12) & 0x07) );
1668 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1669 dstbits += linebytes;
1671 } else {
1672 goto notsupported;
1674 } else if (bmpImage->green_mask==0x07e0) {
1675 if (bmpImage->red_mask==0xf800) {
1676 /* ==== rgb 565 bmp -> pal 8 dib ==== */
1677 for (h=0; h<lines; h++) {
1678 srcpixel=srcbits;
1679 dstbyte=dstbits;
1680 for (x=0; x<width; x++) {
1681 WORD srcval;
1682 srcval=*srcpixel++;
1683 *dstbyte++=X11DRV_DIB_GetNearestIndex
1684 (colors, 256,
1685 ((srcval >> 8) & 0xf8) | /* r */
1686 ((srcval >> 13) & 0x07),
1687 ((srcval >> 3) & 0xfc) | /* g */
1688 ((srcval >> 9) & 0x03),
1689 ((srcval << 3) & 0xf8) | /* b */
1690 ((srcval >> 2) & 0x07) );
1692 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1693 dstbits += linebytes;
1695 } else if (bmpImage->blue_mask==0xf800) {
1696 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1697 for (h=0; h<lines; h++) {
1698 srcpixel=srcbits;
1699 dstbyte=dstbits;
1700 for (x=0; x<width; x++) {
1701 WORD srcval;
1702 srcval=*srcpixel++;
1703 *dstbyte++=X11DRV_DIB_GetNearestIndex
1704 (colors, 256,
1705 ((srcval << 3) & 0xf8) | /* r */
1706 ((srcval >> 2) & 0x07),
1707 ((srcval >> 3) & 0xfc) | /* g */
1708 ((srcval >> 9) & 0x03),
1709 ((srcval >> 8) & 0xf8) | /* b */
1710 ((srcval >> 13) & 0x07) );
1712 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1713 dstbits += linebytes;
1715 } else {
1716 goto notsupported;
1718 } else {
1719 goto notsupported;
1722 break;
1724 case 24:
1725 case 32:
1727 const void* srcbits;
1728 const BYTE *srcbyte;
1729 BYTE* dstbyte;
1730 int bytes_per_pixel;
1732 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
1733 bytes_per_pixel=(bmpImage->bits_per_pixel==24?3:4);
1735 if (bmpImage->green_mask!=0x00ff00 ||
1736 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
1737 goto notsupported;
1738 } else if (bmpImage->blue_mask==0xff) {
1739 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1740 for (h=0; h<lines; h++) {
1741 srcbyte=srcbits;
1742 dstbyte=dstbits;
1743 for (x=0; x<width; x++) {
1744 *dstbyte++=X11DRV_DIB_GetNearestIndex
1745 (colors, 256,
1746 srcbyte[2],
1747 srcbyte[1],
1748 srcbyte[0]);
1749 srcbyte+=bytes_per_pixel;
1751 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1752 dstbits += linebytes;
1754 } else {
1755 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1756 for (h=0; h<lines; h++) {
1757 srcbyte=srcbits;
1758 dstbyte=dstbits;
1759 for (x=0; x<width; x++) {
1760 *dstbyte++=X11DRV_DIB_GetNearestIndex
1761 (colors, 256,
1762 srcbyte[0],
1763 srcbyte[1],
1764 srcbyte[2]);
1765 srcbyte+=bytes_per_pixel;
1767 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
1768 dstbits += linebytes;
1772 break;
1774 default:
1775 notsupported:
1776 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1777 bmpImage->depth, bmpImage->red_mask,
1778 bmpImage->green_mask, bmpImage->blue_mask );
1779 updatesection:
1780 /* ==== any bmp format -> pal 8 dib ==== */
1781 for (h=lines-1; h>=0; h--) {
1782 dstbyte=dstbits;
1783 for (x=0; x<width; x++) {
1784 *dstbyte=X11DRV_DIB_MapColor
1785 ((int*)colors, 256,
1786 XGetPixel(bmpImage, x, h), *dstbyte);
1787 dstbyte++;
1789 dstbits += linebytes;
1791 break;
1795 /***********************************************************************
1796 * X11DRV_DIB_SetImageBits_RLE8
1798 * SetDIBits for an 8-bit deep compressed DIB.
1800 * This function rewritten 941113 by James Youngman. WINE blew out when I
1801 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
1803 * This was because the algorithm assumed that all RLE8 bitmaps end with the
1804 * 'End of bitmap' escape code. This code is very much laxer in what it
1805 * allows to end the expansion. Possibly too lax. See the note by
1806 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
1807 * bitmap should end with RleEnd, but on the other hand, software exists
1808 * that produces ones that don't and Windows 3.1 doesn't complain a bit
1809 * about it.
1811 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
1812 * James A. Youngman <mbcstjy@afs.man.ac.uk>
1813 * [JAY]
1815 static void X11DRV_DIB_SetImageBits_RLE8( int lines, const BYTE *bits,
1816 DWORD srcwidth, DWORD dstwidth,
1817 int left, int *colors,
1818 XImage *bmpImage )
1820 unsigned int x; /* X-position on each line. Increases. */
1821 int y; /* Line #. Starts at lines-1, decreases */
1822 const BYTE *pIn = bits; /* Pointer to current position in bits */
1823 BYTE length; /* The length pf a run */
1824 BYTE escape_code; /* See enum Rle8_EscapeCodes.*/
1827 * Note that the bitmap data is stored by Windows starting at the
1828 * bottom line of the bitmap and going upwards. Within each line,
1829 * the data is stored left-to-right. That's the reason why line
1830 * goes from lines-1 to 0. [JAY]
1833 x = 0;
1834 y = lines - 1;
1835 while (y >= 0)
1837 length = *pIn++;
1840 * If the length byte is not zero (which is the escape value),
1841 * We have a run of length pixels all the same colour. The colour
1842 * index is stored next.
1844 * If the length byte is zero, we need to read the next byte to
1845 * know what to do. [JAY]
1847 if (length != 0)
1850 * [Run-Length] Encoded mode
1852 int color = colors[*pIn++];
1853 while (length-- && x < (left + dstwidth)) {
1854 if( x >= left) XPutPixel(bmpImage, x, y, color);
1855 x++;
1858 else
1861 * Escape codes (may be an absolute sequence though)
1863 escape_code = (*pIn++);
1864 switch(escape_code)
1866 case RLE_EOL:
1867 x = 0;
1868 y--;
1869 break;
1871 case RLE_END:
1872 /* Not all RLE8 bitmaps end with this code. For
1873 * example, Paint Shop Pro produces some that don't.
1874 * That's (I think) what caused the previous
1875 * implementation to fail. [JAY]
1877 return;
1879 case RLE_DELTA:
1880 x += (*pIn++);
1881 y -= (*pIn++);
1882 break;
1884 default: /* switch to absolute mode */
1885 length = escape_code;
1886 while (length--)
1888 int color = colors[*pIn++];
1889 if (x >= (left + dstwidth))
1891 pIn += length;
1892 break;
1894 if( x >= left) XPutPixel(bmpImage, x, y, color);
1895 x++;
1898 * If you think for a moment you'll realise that the
1899 * only time we could ever possibly read an odd
1900 * number of bytes is when there is a 0x00 (escape),
1901 * a value >0x02 (absolute mode) and then an odd-
1902 * length run. Therefore this is the only place we
1903 * need to worry about it. Everywhere else the
1904 * bytes are always read in pairs. [JAY]
1906 if (escape_code & 1) pIn++; /* Throw away the pad byte. */
1907 break;
1908 } /* switch (escape_code) : Escape sequence */
1914 /***********************************************************************
1915 * X11DRV_DIB_SetImageBits_16
1917 * SetDIBits for a 16-bit deep DIB.
1919 static void X11DRV_DIB_SetImageBits_16( int lines, const BYTE *srcbits,
1920 DWORD srcwidth, DWORD dstwidth, int left,
1921 X11DRV_PDEVICE *physDev, DWORD rSrc, DWORD gSrc, DWORD bSrc,
1922 XImage *bmpImage, DWORD linebytes )
1924 DWORD x;
1925 int h, width = min(srcwidth, dstwidth);
1926 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
1928 if (lines < 0 )
1930 lines = -lines;
1931 srcbits = srcbits + ( linebytes * (lines-1));
1932 linebytes = -linebytes;
1935 switch (bmpImage->depth)
1937 case 15:
1938 case 16:
1940 char* dstbits;
1942 srcbits=srcbits+left*2;
1943 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
1945 if (bmpImage->green_mask==0x03e0) {
1946 if (gSrc==bmpImage->green_mask) {
1947 if (rSrc==bmpImage->red_mask) {
1948 /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1949 /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1950 convs->Convert_5x5_asis
1951 (width,lines,
1952 srcbits,linebytes,
1953 dstbits,-bmpImage->bytes_per_line);
1954 } else if (rSrc==bmpImage->blue_mask) {
1955 /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1956 /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1957 convs->Convert_555_reverse
1958 (width,lines,
1959 srcbits,linebytes,
1960 dstbits,-bmpImage->bytes_per_line);
1962 } else {
1963 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1964 /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1965 /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1966 convs->Convert_565_to_555_asis
1967 (width,lines,
1968 srcbits,linebytes,
1969 dstbits,-bmpImage->bytes_per_line);
1970 } else {
1971 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1972 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1973 convs->Convert_565_to_555_reverse
1974 (width,lines,
1975 srcbits,linebytes,
1976 dstbits,-bmpImage->bytes_per_line);
1979 } else if (bmpImage->green_mask==0x07e0) {
1980 if (gSrc==bmpImage->green_mask) {
1981 if (rSrc==bmpImage->red_mask) {
1982 /* ==== rgb 565 dib -> rgb 565 bmp ==== */
1983 /* ==== bgr 565 dib -> bgr 565 bmp ==== */
1984 convs->Convert_5x5_asis
1985 (width,lines,
1986 srcbits,linebytes,
1987 dstbits,-bmpImage->bytes_per_line);
1988 } else {
1989 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
1990 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
1991 convs->Convert_565_reverse
1992 (width,lines,
1993 srcbits,linebytes,
1994 dstbits,-bmpImage->bytes_per_line);
1996 } else {
1997 if (rSrc==bmpImage->red_mask || bSrc==bmpImage->blue_mask) {
1998 /* ==== rgb 555 dib -> rgb 565 bmp ==== */
1999 /* ==== bgr 555 dib -> bgr 565 bmp ==== */
2000 convs->Convert_555_to_565_asis
2001 (width,lines,
2002 srcbits,linebytes,
2003 dstbits,-bmpImage->bytes_per_line);
2004 } else {
2005 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
2006 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
2007 convs->Convert_555_to_565_reverse
2008 (width,lines,
2009 srcbits,linebytes,
2010 dstbits,-bmpImage->bytes_per_line);
2013 } else {
2014 goto notsupported;
2017 break;
2019 case 24:
2020 if (bmpImage->bits_per_pixel==24) {
2021 char* dstbits;
2023 srcbits=srcbits+left*2;
2024 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2026 if (bmpImage->green_mask!=0x00ff00 ||
2027 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2028 goto notsupported;
2029 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2030 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2031 if (gSrc==0x03e0) {
2032 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
2033 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
2034 convs->Convert_555_to_888_asis
2035 (width,lines,
2036 srcbits,linebytes,
2037 dstbits,-bmpImage->bytes_per_line);
2038 } else {
2039 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
2040 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
2041 convs->Convert_565_to_888_asis
2042 (width,lines,
2043 srcbits,linebytes,
2044 dstbits,-bmpImage->bytes_per_line);
2046 } else {
2047 if (gSrc==0x03e0) {
2048 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
2049 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
2050 convs->Convert_555_to_888_reverse
2051 (width,lines,
2052 srcbits,linebytes,
2053 dstbits,-bmpImage->bytes_per_line);
2054 } else {
2055 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
2056 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
2057 convs->Convert_565_to_888_reverse
2058 (width,lines,
2059 srcbits,linebytes,
2060 dstbits,-bmpImage->bytes_per_line);
2063 break;
2065 /* Fall through */
2067 case 32:
2069 char* dstbits;
2071 srcbits=srcbits+left*2;
2072 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2074 if (bmpImage->green_mask!=0x00ff00 ||
2075 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2076 goto notsupported;
2077 } else if ((rSrc==0x1f && bmpImage->red_mask==0xff) ||
2078 (bSrc==0x1f && bmpImage->blue_mask==0xff)) {
2079 if (gSrc==0x03e0) {
2080 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
2081 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
2082 convs->Convert_555_to_0888_asis
2083 (width,lines,
2084 srcbits,linebytes,
2085 dstbits,-bmpImage->bytes_per_line);
2086 } else {
2087 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
2088 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
2089 convs->Convert_565_to_0888_asis
2090 (width,lines,
2091 srcbits,linebytes,
2092 dstbits,-bmpImage->bytes_per_line);
2094 } else {
2095 if (gSrc==0x03e0) {
2096 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
2097 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
2098 convs->Convert_555_to_0888_reverse
2099 (width,lines,
2100 srcbits,linebytes,
2101 dstbits,-bmpImage->bytes_per_line);
2102 } else {
2103 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
2104 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
2105 convs->Convert_565_to_0888_reverse
2106 (width,lines,
2107 srcbits,linebytes,
2108 dstbits,-bmpImage->bytes_per_line);
2112 break;
2114 default:
2115 notsupported:
2116 WARN("from 16 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2117 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2118 bmpImage->green_mask, bmpImage->blue_mask );
2119 /* fall through */
2120 case 1:
2121 case 4:
2122 case 8:
2124 /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
2125 const WORD* srcpixel;
2126 int rShift1,gShift1,bShift1;
2127 int rShift2,gShift2,bShift2;
2128 BYTE gMask1,gMask2;
2130 /* Set color scaling values */
2131 rShift1=16+X11DRV_DIB_MaskToShift(rSrc)-3;
2132 gShift1=16+X11DRV_DIB_MaskToShift(gSrc)-3;
2133 bShift1=16+X11DRV_DIB_MaskToShift(bSrc)-3;
2134 rShift2=rShift1+5;
2135 gShift2=gShift1+5;
2136 bShift2=bShift1+5;
2137 if (gSrc==0x03e0) {
2138 /* Green has 5 bits, like the others */
2139 gMask1=0xf8;
2140 gMask2=0x07;
2141 } else {
2142 /* Green has 6 bits, not 5. Compensate. */
2143 gShift1++;
2144 gShift2+=2;
2145 gMask1=0xfc;
2146 gMask2=0x03;
2149 srcbits+=2*left;
2151 /* We could split it into four separate cases to optimize
2152 * but it is probably not worth it.
2154 for (h=lines-1; h>=0; h--) {
2155 srcpixel=(const WORD*)srcbits;
2156 for (x=left; x<width+left; x++) {
2157 DWORD srcval;
2158 BYTE red,green,blue;
2159 srcval=*srcpixel++ << 16;
2160 red= ((srcval >> rShift1) & 0xf8) |
2161 ((srcval >> rShift2) & 0x07);
2162 green=((srcval >> gShift1) & gMask1) |
2163 ((srcval >> gShift2) & gMask2);
2164 blue= ((srcval >> bShift1) & 0xf8) |
2165 ((srcval >> bShift2) & 0x07);
2166 XPutPixel(bmpImage, x, h,
2167 X11DRV_PALETTE_ToPhysical
2168 (physDev, RGB(red,green,blue)));
2170 srcbits += linebytes;
2173 break;
2178 /***********************************************************************
2179 * X11DRV_DIB_GetImageBits_16
2181 * GetDIBits for an 16-bit deep DIB.
2183 static void X11DRV_DIB_GetImageBits_16( int lines, BYTE *dstbits,
2184 DWORD dstwidth, DWORD srcwidth,
2185 PALETTEENTRY *srccolors,
2186 DWORD rDst, DWORD gDst, DWORD bDst,
2187 XImage *bmpImage, DWORD dibpitch )
2189 DWORD x;
2190 int h, width = min(srcwidth, dstwidth);
2191 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2193 DWORD linebytes = dibpitch;
2195 if (lines < 0 )
2197 lines = -lines;
2198 dstbits = dstbits + ( linebytes * (lines-1));
2199 linebytes = -linebytes;
2202 switch (bmpImage->depth)
2204 case 15:
2205 case 16:
2207 const char* srcbits;
2209 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2211 if (bmpImage->green_mask==0x03e0) {
2212 if (gDst==bmpImage->green_mask) {
2213 if (rDst==bmpImage->red_mask) {
2214 /* ==== rgb 555 bmp -> rgb 555 dib ==== */
2215 /* ==== bgr 555 bmp -> bgr 555 dib ==== */
2216 convs->Convert_5x5_asis
2217 (width,lines,
2218 srcbits,-bmpImage->bytes_per_line,
2219 dstbits,linebytes);
2220 } else {
2221 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
2222 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
2223 convs->Convert_555_reverse
2224 (width,lines,
2225 srcbits,-bmpImage->bytes_per_line,
2226 dstbits,linebytes);
2228 } else {
2229 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2230 /* ==== rgb 555 bmp -> rgb 565 dib ==== */
2231 /* ==== bgr 555 bmp -> bgr 565 dib ==== */
2232 convs->Convert_555_to_565_asis
2233 (width,lines,
2234 srcbits,-bmpImage->bytes_per_line,
2235 dstbits,linebytes);
2236 } else {
2237 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
2238 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
2239 convs->Convert_555_to_565_reverse
2240 (width,lines,
2241 srcbits,-bmpImage->bytes_per_line,
2242 dstbits,linebytes);
2245 } else if (bmpImage->green_mask==0x07e0) {
2246 if (gDst==bmpImage->green_mask) {
2247 if (rDst == bmpImage->red_mask) {
2248 /* ==== rgb 565 bmp -> rgb 565 dib ==== */
2249 /* ==== bgr 565 bmp -> bgr 565 dib ==== */
2250 convs->Convert_5x5_asis
2251 (width,lines,
2252 srcbits,-bmpImage->bytes_per_line,
2253 dstbits,linebytes);
2254 } else {
2255 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
2256 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
2257 convs->Convert_565_reverse
2258 (width,lines,
2259 srcbits,-bmpImage->bytes_per_line,
2260 dstbits,linebytes);
2262 } else {
2263 if (rDst==bmpImage->red_mask || bDst==bmpImage->blue_mask) {
2264 /* ==== rgb 565 bmp -> rgb 555 dib ==== */
2265 /* ==== bgr 565 bmp -> bgr 555 dib ==== */
2266 convs->Convert_565_to_555_asis
2267 (width,lines,
2268 srcbits,-bmpImage->bytes_per_line,
2269 dstbits,linebytes);
2270 } else {
2271 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
2272 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
2273 convs->Convert_565_to_555_reverse
2274 (width,lines,
2275 srcbits,-bmpImage->bytes_per_line,
2276 dstbits,linebytes);
2279 } else {
2280 goto notsupported;
2283 break;
2285 case 24:
2286 if (bmpImage->bits_per_pixel == 24) {
2287 const char* srcbits;
2289 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2291 if (bmpImage->green_mask!=0x00ff00 ||
2292 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2293 goto notsupported;
2294 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2295 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2296 if (gDst==0x03e0) {
2297 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
2298 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
2299 convs->Convert_888_to_555_asis
2300 (width,lines,
2301 srcbits,-bmpImage->bytes_per_line,
2302 dstbits,linebytes);
2303 } else {
2304 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2305 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2306 convs->Convert_888_to_565_asis
2307 (width,lines,
2308 srcbits,-bmpImage->bytes_per_line,
2309 dstbits,linebytes);
2311 } else {
2312 if (gDst==0x03e0) {
2313 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
2314 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
2315 convs->Convert_888_to_555_reverse
2316 (width,lines,
2317 srcbits,-bmpImage->bytes_per_line,
2318 dstbits,linebytes);
2319 } else {
2320 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
2321 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
2322 convs->Convert_888_to_565_reverse
2323 (width,lines,
2324 srcbits,-bmpImage->bytes_per_line,
2325 dstbits,linebytes);
2328 break;
2330 /* Fall through */
2332 case 32:
2334 const char* srcbits;
2336 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2338 if (bmpImage->green_mask!=0x00ff00 ||
2339 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2340 goto notsupported;
2341 } else if ((rDst==0x1f && bmpImage->red_mask==0xff) ||
2342 (bDst==0x1f && bmpImage->blue_mask==0xff)) {
2343 if (gDst==0x03e0) {
2344 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2345 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2346 convs->Convert_0888_to_555_asis
2347 (width,lines,
2348 srcbits,-bmpImage->bytes_per_line,
2349 dstbits,linebytes);
2350 } else {
2351 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2352 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2353 convs->Convert_0888_to_565_asis
2354 (width,lines,
2355 srcbits,-bmpImage->bytes_per_line,
2356 dstbits,linebytes);
2358 } else {
2359 if (gDst==0x03e0) {
2360 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2361 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2362 convs->Convert_0888_to_555_reverse
2363 (width,lines,
2364 srcbits,-bmpImage->bytes_per_line,
2365 dstbits,linebytes);
2366 } else {
2367 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2368 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2369 convs->Convert_0888_to_565_reverse
2370 (width,lines,
2371 srcbits,-bmpImage->bytes_per_line,
2372 dstbits,linebytes);
2376 break;
2378 case 1:
2379 case 4:
2380 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2381 && srccolors) {
2382 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2383 int rShift,gShift,bShift;
2384 WORD* dstpixel;
2386 /* Shift everything 16 bits left so that all shifts are >0,
2387 * even for BGR DIBs. Then a single >> 16 will bring everything
2388 * back into place.
2390 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2391 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2392 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2393 if (gDst==0x07e0) {
2394 /* 6 bits for the green */
2395 gShift++;
2397 rDst=rDst << 16;
2398 gDst=gDst << 16;
2399 bDst=bDst << 16;
2400 for (h = lines - 1; h >= 0; h--) {
2401 dstpixel=(LPWORD)dstbits;
2402 for (x = 0; x < width; x++) {
2403 PALETTEENTRY srcval;
2404 DWORD dstval;
2405 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2406 dstval=((srcval.peRed << rShift) & rDst) |
2407 ((srcval.peGreen << gShift) & gDst) |
2408 ((srcval.peBlue << bShift) & bDst);
2409 *dstpixel++=dstval >> 16;
2411 dstbits += linebytes;
2413 } else {
2414 goto notsupported;
2416 break;
2418 case 8:
2419 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2420 && srccolors) {
2421 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2422 int rShift,gShift,bShift;
2423 const BYTE* srcbits;
2424 const BYTE* srcpixel;
2425 WORD* dstpixel;
2427 /* Shift everything 16 bits left so that all shifts are >0,
2428 * even for BGR DIBs. Then a single >> 16 will bring everything
2429 * back into place.
2431 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2432 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2433 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2434 if (gDst==0x07e0) {
2435 /* 6 bits for the green */
2436 gShift++;
2438 rDst=rDst << 16;
2439 gDst=gDst << 16;
2440 bDst=bDst << 16;
2441 srcbits=(BYTE*)bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2442 for (h=0; h<lines; h++) {
2443 srcpixel=srcbits;
2444 dstpixel=(LPWORD)dstbits;
2445 for (x = 0; x < width; x++) {
2446 PALETTEENTRY srcval;
2447 DWORD dstval;
2448 srcval=srccolors[*srcpixel++];
2449 dstval=((srcval.peRed << rShift) & rDst) |
2450 ((srcval.peGreen << gShift) & gDst) |
2451 ((srcval.peBlue << bShift) & bDst);
2452 *dstpixel++=dstval >> 16;
2454 srcbits -= bmpImage->bytes_per_line;
2455 dstbits += linebytes;
2457 } else {
2458 goto notsupported;
2460 break;
2462 default:
2463 notsupported:
2465 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2466 int rShift,gShift,bShift;
2467 WORD* dstpixel;
2469 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%x,%x,%x)\n",
2470 bmpImage->depth, bmpImage->red_mask,
2471 bmpImage->green_mask, bmpImage->blue_mask,
2472 rDst, gDst, bDst);
2474 /* Shift everything 16 bits left so that all shifts are >0,
2475 * even for BGR DIBs. Then a single >> 16 will bring everything
2476 * back into place.
2478 rShift=16+X11DRV_DIB_MaskToShift(rDst)-3;
2479 gShift=16+X11DRV_DIB_MaskToShift(gDst)-3;
2480 bShift=16+X11DRV_DIB_MaskToShift(bDst)-3;
2481 if (gDst==0x07e0) {
2482 /* 6 bits for the green */
2483 gShift++;
2485 rDst=rDst << 16;
2486 gDst=gDst << 16;
2487 bDst=bDst << 16;
2488 for (h = lines - 1; h >= 0; h--) {
2489 dstpixel=(LPWORD)dstbits;
2490 for (x = 0; x < width; x++) {
2491 COLORREF srcval;
2492 DWORD dstval;
2493 srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
2494 dstval=((GetRValue(srcval) << rShift) & rDst) |
2495 ((GetGValue(srcval) << gShift) & gDst) |
2496 ((GetBValue(srcval) << bShift) & bDst);
2497 *dstpixel++=dstval >> 16;
2499 dstbits += linebytes;
2502 break;
2507 /***********************************************************************
2508 * X11DRV_DIB_SetImageBits_24
2510 * SetDIBits for a 24-bit deep DIB.
2512 static void X11DRV_DIB_SetImageBits_24( int lines, const BYTE *srcbits,
2513 DWORD srcwidth, DWORD dstwidth, int left,
2514 X11DRV_PDEVICE *physDev,
2515 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2516 XImage *bmpImage, DWORD linebytes )
2518 DWORD x;
2519 int h, width = min(srcwidth, dstwidth);
2520 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2522 if (lines < 0 )
2524 lines = -lines;
2525 srcbits = srcbits + linebytes * (lines - 1);
2526 linebytes = -linebytes;
2529 switch (bmpImage->depth)
2531 case 24:
2532 if (bmpImage->bits_per_pixel==24) {
2533 char* dstbits;
2535 srcbits=srcbits+left*3;
2536 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2538 if (bmpImage->green_mask!=0x00ff00 ||
2539 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2540 goto notsupported;
2541 } else if (rSrc==bmpImage->red_mask) {
2542 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2543 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2544 convs->Convert_888_asis
2545 (width,lines,
2546 srcbits,linebytes,
2547 dstbits,-bmpImage->bytes_per_line);
2548 } else {
2549 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2550 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2551 convs->Convert_888_reverse
2552 (width,lines,
2553 srcbits,linebytes,
2554 dstbits,-bmpImage->bytes_per_line);
2556 break;
2558 /* fall through */
2560 case 32:
2562 char* dstbits;
2564 srcbits=srcbits+left*3;
2565 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2567 if (bmpImage->green_mask!=0x00ff00 ||
2568 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2569 goto notsupported;
2570 } else if (rSrc==bmpImage->red_mask) {
2571 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2572 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2573 convs->Convert_888_to_0888_asis
2574 (width,lines,
2575 srcbits,linebytes,
2576 dstbits,-bmpImage->bytes_per_line);
2577 } else {
2578 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2579 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2580 convs->Convert_888_to_0888_reverse
2581 (width,lines,
2582 srcbits,linebytes,
2583 dstbits,-bmpImage->bytes_per_line);
2585 break;
2588 case 15:
2589 case 16:
2591 char* dstbits;
2593 srcbits=srcbits+left*3;
2594 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
2596 if (bmpImage->green_mask==0x03e0) {
2597 if ((rSrc==0xff0000 && bmpImage->red_mask==0x7f00) ||
2598 (bSrc==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2599 /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2600 /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2601 convs->Convert_888_to_555_asis
2602 (width,lines,
2603 srcbits,linebytes,
2604 dstbits,-bmpImage->bytes_per_line);
2605 } else if ((rSrc==0xff && bmpImage->red_mask==0x7f00) ||
2606 (bSrc==0xff && bmpImage->blue_mask==0x7f00)) {
2607 /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2608 /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2609 convs->Convert_888_to_555_reverse
2610 (width,lines,
2611 srcbits,linebytes,
2612 dstbits,-bmpImage->bytes_per_line);
2613 } else {
2614 goto notsupported;
2616 } else if (bmpImage->green_mask==0x07e0) {
2617 if ((rSrc==0xff0000 && bmpImage->red_mask==0xf800) ||
2618 (bSrc==0xff0000 && bmpImage->blue_mask==0xf800)) {
2619 /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2620 /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2621 convs->Convert_888_to_565_asis
2622 (width,lines,
2623 srcbits,linebytes,
2624 dstbits,-bmpImage->bytes_per_line);
2625 } else if ((rSrc==0xff && bmpImage->red_mask==0xf800) ||
2626 (bSrc==0xff && bmpImage->blue_mask==0xf800)) {
2627 /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2628 /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2629 convs->Convert_888_to_565_reverse
2630 (width,lines,
2631 srcbits,linebytes,
2632 dstbits,-bmpImage->bytes_per_line);
2633 } else {
2634 goto notsupported;
2636 } else {
2637 goto notsupported;
2640 break;
2642 default:
2643 notsupported:
2644 WARN("from 24 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2645 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
2646 bmpImage->green_mask, bmpImage->blue_mask );
2647 /* fall through */
2648 case 1:
2649 case 4:
2650 case 8:
2652 /* ==== rgb 888 dib -> any bmp format ==== */
2653 const BYTE* srcbyte;
2655 /* Windows only supports one 24bpp DIB format: RGB888 */
2656 srcbits+=left*3;
2657 for (h = lines - 1; h >= 0; h--) {
2658 srcbyte = srcbits;
2659 for (x = left; x < width+left; x++) {
2660 XPutPixel(bmpImage, x, h,
2661 X11DRV_PALETTE_ToPhysical
2662 (physDev, RGB(srcbyte[2], srcbyte[1], srcbyte[0])));
2663 srcbyte+=3;
2665 srcbits += linebytes;
2668 break;
2673 /***********************************************************************
2674 * X11DRV_DIB_GetImageBits_24
2676 * GetDIBits for an 24-bit deep DIB.
2678 static void X11DRV_DIB_GetImageBits_24( int lines, BYTE *dstbits,
2679 DWORD dstwidth, DWORD srcwidth,
2680 PALETTEENTRY *srccolors,
2681 DWORD rDst, DWORD gDst, DWORD bDst,
2682 XImage *bmpImage, DWORD linebytes )
2684 DWORD x;
2685 int h, width = min(srcwidth, dstwidth);
2686 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
2688 if (lines < 0 )
2690 lines = -lines;
2691 dstbits = dstbits + ( linebytes * (lines-1) );
2692 linebytes = -linebytes;
2695 switch (bmpImage->depth)
2697 case 24:
2698 if (bmpImage->bits_per_pixel==24) {
2699 const char* srcbits;
2701 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2703 if (bmpImage->green_mask!=0x00ff00 ||
2704 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2705 goto notsupported;
2706 } else if (rDst==bmpImage->red_mask) {
2707 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2708 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2709 convs->Convert_888_asis
2710 (width,lines,
2711 srcbits,-bmpImage->bytes_per_line,
2712 dstbits,linebytes);
2713 } else {
2714 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2715 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2716 convs->Convert_888_reverse
2717 (width,lines,
2718 srcbits,-bmpImage->bytes_per_line,
2719 dstbits,linebytes);
2721 break;
2723 /* fall through */
2725 case 32:
2727 const char* srcbits;
2729 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2731 if (bmpImage->green_mask!=0x00ff00 ||
2732 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2733 goto notsupported;
2734 } else if (rDst==bmpImage->red_mask) {
2735 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2736 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2737 convs->Convert_0888_to_888_asis
2738 (width,lines,
2739 srcbits,-bmpImage->bytes_per_line,
2740 dstbits,linebytes);
2741 } else {
2742 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2743 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2744 convs->Convert_0888_to_888_reverse
2745 (width,lines,
2746 srcbits,-bmpImage->bytes_per_line,
2747 dstbits,linebytes);
2749 break;
2752 case 15:
2753 case 16:
2755 const char* srcbits;
2757 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2759 if (bmpImage->green_mask==0x03e0) {
2760 if ((rDst==0xff0000 && bmpImage->red_mask==0x7f00) ||
2761 (bDst==0xff0000 && bmpImage->blue_mask==0x7f00)) {
2762 /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2763 /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2764 convs->Convert_555_to_888_asis
2765 (width,lines,
2766 srcbits,-bmpImage->bytes_per_line,
2767 dstbits,linebytes);
2768 } else if ((rDst==0xff && bmpImage->red_mask==0x7f00) ||
2769 (bDst==0xff && bmpImage->blue_mask==0x7f00)) {
2770 /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2771 /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2772 convs->Convert_555_to_888_reverse
2773 (width,lines,
2774 srcbits,-bmpImage->bytes_per_line,
2775 dstbits,linebytes);
2776 } else {
2777 goto notsupported;
2779 } else if (bmpImage->green_mask==0x07e0) {
2780 if ((rDst==0xff0000 && bmpImage->red_mask==0xf800) ||
2781 (bDst==0xff0000 && bmpImage->blue_mask==0xf800)) {
2782 /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2783 /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2784 convs->Convert_565_to_888_asis
2785 (width,lines,
2786 srcbits,-bmpImage->bytes_per_line,
2787 dstbits,linebytes);
2788 } else if ((rDst==0xff && bmpImage->red_mask==0xf800) ||
2789 (bDst==0xff && bmpImage->blue_mask==0xf800)) {
2790 /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2791 /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2792 convs->Convert_565_to_888_reverse
2793 (width,lines,
2794 srcbits,-bmpImage->bytes_per_line,
2795 dstbits,linebytes);
2796 } else {
2797 goto notsupported;
2799 } else {
2800 goto notsupported;
2803 break;
2805 case 1:
2806 case 4:
2807 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2808 && srccolors) {
2809 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2810 BYTE* dstbyte;
2812 /* Windows only supports one 24bpp DIB format: rgb 888 */
2813 for (h = lines - 1; h >= 0; h--) {
2814 dstbyte=dstbits;
2815 for (x = 0; x < width; x++) {
2816 PALETTEENTRY srcval;
2817 srcval=srccolors[XGetPixel(bmpImage, x, h)];
2818 dstbyte[0]=srcval.peBlue;
2819 dstbyte[1]=srcval.peGreen;
2820 dstbyte[2]=srcval.peRed;
2821 dstbyte+=3;
2823 dstbits += linebytes;
2825 } else {
2826 goto notsupported;
2828 break;
2830 case 8:
2831 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
2832 && srccolors) {
2833 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2834 const void* srcbits;
2835 const BYTE* srcpixel;
2836 BYTE* dstbyte;
2838 /* Windows only supports one 24bpp DIB format: rgb 888 */
2839 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
2840 for (h = lines - 1; h >= 0; h--) {
2841 srcpixel=srcbits;
2842 dstbyte=dstbits;
2843 for (x = 0; x < width; x++ ) {
2844 PALETTEENTRY srcval;
2845 srcval=srccolors[*srcpixel++];
2846 dstbyte[0]=srcval.peBlue;
2847 dstbyte[1]=srcval.peGreen;
2848 dstbyte[2]=srcval.peRed;
2849 dstbyte+=3;
2851 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
2852 dstbits += linebytes;
2854 } else {
2855 goto notsupported;
2857 break;
2859 default:
2860 notsupported:
2862 /* ==== any bmp format -> 888 dib ==== */
2863 BYTE* dstbyte;
2865 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%x,%x,%x)\n",
2866 bmpImage->depth, bmpImage->red_mask,
2867 bmpImage->green_mask, bmpImage->blue_mask,
2868 rDst, gDst, bDst );
2870 /* Windows only supports one 24bpp DIB format: rgb 888 */
2871 for (h = lines - 1; h >= 0; h--) {
2872 dstbyte=dstbits;
2873 for (x = 0; x < width; x++) {
2874 COLORREF srcval=X11DRV_PALETTE_ToLogical
2875 (XGetPixel( bmpImage, x, h ));
2876 dstbyte[0]=GetBValue(srcval);
2877 dstbyte[1]=GetGValue(srcval);
2878 dstbyte[2]=GetRValue(srcval);
2879 dstbyte+=3;
2881 dstbits += linebytes;
2884 break;
2889 /***********************************************************************
2890 * X11DRV_DIB_SetImageBits_32
2892 * SetDIBits for a 32-bit deep DIB.
2894 static void X11DRV_DIB_SetImageBits_32(int lines, const BYTE *srcbits,
2895 DWORD srcwidth, DWORD dstwidth, int left,
2896 X11DRV_PDEVICE *physDev,
2897 DWORD rSrc, DWORD gSrc, DWORD bSrc,
2898 XImage *bmpImage,
2899 DWORD linebytes)
2901 DWORD x;
2902 const DWORD *ptr;
2903 int h, width = min(srcwidth, dstwidth);
2904 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_dst_byteswap;
2906 if (lines < 0 )
2908 lines = -lines;
2909 srcbits = srcbits + ( linebytes * (lines-1) );
2910 linebytes = -linebytes;
2913 ptr = (const DWORD *) srcbits + left;
2915 switch (bmpImage->depth)
2917 case 24:
2918 if (bmpImage->bits_per_pixel==24) {
2919 char* dstbits;
2921 srcbits=srcbits+left*4;
2922 dstbits=bmpImage->data+left*3+(lines-1)*bmpImage->bytes_per_line;
2924 if (rSrc==bmpImage->red_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->blue_mask) {
2925 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2926 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2927 convs->Convert_0888_to_888_asis
2928 (width,lines,
2929 srcbits,linebytes,
2930 dstbits,-bmpImage->bytes_per_line);
2931 } else if (bmpImage->green_mask!=0x00ff00 ||
2932 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2933 goto notsupported;
2934 /* the tests below assume sane bmpImage masks */
2935 } else if (rSrc==bmpImage->blue_mask && gSrc==bmpImage->green_mask && bSrc==bmpImage->red_mask) {
2936 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2937 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2938 convs->Convert_0888_to_888_reverse
2939 (width,lines,
2940 srcbits,linebytes,
2941 dstbits,-bmpImage->bytes_per_line);
2942 } else if (bmpImage->blue_mask==0xff) {
2943 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2944 convs->Convert_any0888_to_rgb888
2945 (width,lines,
2946 srcbits,linebytes,
2947 rSrc,gSrc,bSrc,
2948 dstbits,-bmpImage->bytes_per_line);
2949 } else {
2950 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2951 convs->Convert_any0888_to_bgr888
2952 (width,lines,
2953 srcbits,linebytes,
2954 rSrc,gSrc,bSrc,
2955 dstbits,-bmpImage->bytes_per_line);
2957 break;
2959 /* fall through */
2961 case 32:
2963 char* dstbits;
2965 srcbits=srcbits+left*4;
2966 dstbits=bmpImage->data+left*4+(lines-1)*bmpImage->bytes_per_line;
2968 if (gSrc==bmpImage->green_mask) {
2969 if (rSrc==bmpImage->red_mask && bSrc==bmpImage->blue_mask) {
2970 /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2971 /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2972 convs->Convert_0888_asis
2973 (width,lines,
2974 srcbits,linebytes,
2975 dstbits,-bmpImage->bytes_per_line);
2976 } else if (bmpImage->green_mask!=0x00ff00 ||
2977 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2978 goto notsupported;
2979 /* the tests below assume sane bmpImage masks */
2980 } else if (rSrc==bmpImage->blue_mask && bSrc==bmpImage->red_mask) {
2981 /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2982 /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2983 convs->Convert_0888_reverse
2984 (width,lines,
2985 srcbits,linebytes,
2986 dstbits,-bmpImage->bytes_per_line);
2987 } else {
2988 /* ==== any 0888 dib -> any 0888 bmp ==== */
2989 convs->Convert_0888_any
2990 (width,lines,
2991 srcbits,linebytes,
2992 rSrc,gSrc,bSrc,
2993 dstbits,-bmpImage->bytes_per_line,
2994 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
2996 } else if (bmpImage->green_mask!=0x00ff00 ||
2997 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
2998 goto notsupported;
2999 /* the tests below assume sane bmpImage masks */
3000 } else {
3001 /* ==== any 0888 dib -> any 0888 bmp ==== */
3002 convs->Convert_0888_any
3003 (width,lines,
3004 srcbits,linebytes,
3005 rSrc,gSrc,bSrc,
3006 dstbits,-bmpImage->bytes_per_line,
3007 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3010 break;
3012 case 15:
3013 case 16:
3015 char* dstbits;
3017 srcbits=srcbits+left*4;
3018 dstbits=bmpImage->data+left*2+(lines-1)*bmpImage->bytes_per_line;
3020 if (rSrc==0xff0000 && gSrc==0x00ff00 && bSrc==0x0000ff) {
3021 if (bmpImage->green_mask==0x03e0) {
3022 if (bmpImage->red_mask==0x7f00) {
3023 /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
3024 convs->Convert_0888_to_555_asis
3025 (width,lines,
3026 srcbits,linebytes,
3027 dstbits,-bmpImage->bytes_per_line);
3028 } else if (bmpImage->blue_mask==0x7f00) {
3029 /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
3030 convs->Convert_0888_to_555_reverse
3031 (width,lines,
3032 srcbits,linebytes,
3033 dstbits,-bmpImage->bytes_per_line);
3034 } else {
3035 goto notsupported;
3037 } else if (bmpImage->green_mask==0x07e0) {
3038 if (bmpImage->red_mask==0xf800) {
3039 /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
3040 convs->Convert_0888_to_565_asis
3041 (width,lines,
3042 srcbits,linebytes,
3043 dstbits,-bmpImage->bytes_per_line);
3044 } else if (bmpImage->blue_mask==0xf800) {
3045 /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
3046 convs->Convert_0888_to_565_reverse
3047 (width,lines,
3048 srcbits,linebytes,
3049 dstbits,-bmpImage->bytes_per_line);
3050 } else {
3051 goto notsupported;
3053 } else {
3054 goto notsupported;
3056 } else if (rSrc==0x0000ff && gSrc==0x00ff00 && bSrc==0xff0000) {
3057 if (bmpImage->green_mask==0x03e0) {
3058 if (bmpImage->blue_mask==0x7f00) {
3059 /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
3060 convs->Convert_0888_to_555_asis
3061 (width,lines,
3062 srcbits,linebytes,
3063 dstbits,-bmpImage->bytes_per_line);
3064 } else if (bmpImage->red_mask==0x7f00) {
3065 /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
3066 convs->Convert_0888_to_555_reverse
3067 (width,lines,
3068 srcbits,linebytes,
3069 dstbits,-bmpImage->bytes_per_line);
3070 } else {
3071 goto notsupported;
3073 } else if (bmpImage->green_mask==0x07e0) {
3074 if (bmpImage->blue_mask==0xf800) {
3075 /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
3076 convs->Convert_0888_to_565_asis
3077 (width,lines,
3078 srcbits,linebytes,
3079 dstbits,-bmpImage->bytes_per_line);
3080 } else if (bmpImage->red_mask==0xf800) {
3081 /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
3082 convs->Convert_0888_to_565_reverse
3083 (width,lines,
3084 srcbits,linebytes,
3085 dstbits,-bmpImage->bytes_per_line);
3086 } else {
3087 goto notsupported;
3089 } else {
3090 goto notsupported;
3092 } else {
3093 if (bmpImage->green_mask==0x03e0 &&
3094 (bmpImage->red_mask==0x7f00 ||
3095 bmpImage->blue_mask==0x7f00)) {
3096 /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
3097 convs->Convert_any0888_to_5x5
3098 (width,lines,
3099 srcbits,linebytes,
3100 rSrc,gSrc,bSrc,
3101 dstbits,-bmpImage->bytes_per_line,
3102 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3103 } else if (bmpImage->green_mask==0x07e0 &&
3104 (bmpImage->red_mask==0xf800 ||
3105 bmpImage->blue_mask==0xf800)) {
3106 /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
3107 convs->Convert_any0888_to_5x5
3108 (width,lines,
3109 srcbits,linebytes,
3110 rSrc,gSrc,bSrc,
3111 dstbits,-bmpImage->bytes_per_line,
3112 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3113 } else {
3114 goto notsupported;
3118 break;
3120 default:
3121 notsupported:
3122 WARN("from 32 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
3123 rSrc, gSrc, bSrc, bmpImage->bits_per_pixel, bmpImage->red_mask,
3124 bmpImage->green_mask, bmpImage->blue_mask );
3125 /* fall through */
3126 case 1:
3127 case 4:
3128 case 8:
3130 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
3131 const DWORD* srcpixel;
3132 int rShift,gShift,bShift;
3134 rShift=X11DRV_DIB_MaskToShift(rSrc);
3135 gShift=X11DRV_DIB_MaskToShift(gSrc);
3136 bShift=X11DRV_DIB_MaskToShift(bSrc);
3137 srcbits+=left*4;
3138 for (h = lines - 1; h >= 0; h--) {
3139 srcpixel=(const DWORD*)srcbits;
3140 for (x = left; x < width+left; x++) {
3141 DWORD srcvalue;
3142 BYTE red,green,blue;
3143 srcvalue=*srcpixel++;
3144 red= (srcvalue >> rShift) & 0xff;
3145 green=(srcvalue >> gShift) & 0xff;
3146 blue= (srcvalue >> bShift) & 0xff;
3147 XPutPixel(bmpImage, x, h, X11DRV_PALETTE_ToPhysical
3148 (physDev, RGB(red,green,blue)));
3150 srcbits += linebytes;
3153 break;
3158 /***********************************************************************
3159 * X11DRV_DIB_GetImageBits_32
3161 * GetDIBits for an 32-bit deep DIB.
3163 static void X11DRV_DIB_GetImageBits_32( int lines, BYTE *dstbits,
3164 DWORD dstwidth, DWORD srcwidth,
3165 PALETTEENTRY *srccolors,
3166 DWORD rDst, DWORD gDst, DWORD bDst,
3167 XImage *bmpImage, DWORD linebytes )
3169 DWORD x;
3170 int h, width = min(srcwidth, dstwidth);
3171 BYTE *bits;
3172 const dib_conversions *convs = (bmpImage->byte_order == LSBFirst) ? &dib_normal : &dib_src_byteswap;
3174 if (lines < 0 )
3176 lines = -lines;
3177 dstbits = dstbits + ( linebytes * (lines-1) );
3178 linebytes = -linebytes;
3181 bits = dstbits;
3183 switch (bmpImage->depth)
3185 case 24:
3186 if (bmpImage->bits_per_pixel==24) {
3187 const void* srcbits;
3189 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3191 if (rDst==bmpImage->red_mask && gDst==bmpImage->green_mask && bDst==bmpImage->blue_mask) {
3192 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
3193 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
3194 convs->Convert_888_to_0888_asis
3195 (width,lines,
3196 srcbits,-bmpImage->bytes_per_line,
3197 dstbits,linebytes);
3198 } else if (bmpImage->green_mask!=0x00ff00 ||
3199 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3200 goto notsupported;
3201 /* the tests below assume sane bmpImage masks */
3202 } else if (rDst==bmpImage->blue_mask && gDst==bmpImage->green_mask && bDst==bmpImage->red_mask) {
3203 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
3204 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
3205 convs->Convert_888_to_0888_reverse
3206 (width,lines,
3207 srcbits,-bmpImage->bytes_per_line,
3208 dstbits,linebytes);
3209 } else if (bmpImage->blue_mask==0xff) {
3210 /* ==== rgb 888 bmp -> any 0888 dib ==== */
3211 convs->Convert_rgb888_to_any0888
3212 (width,lines,
3213 srcbits,-bmpImage->bytes_per_line,
3214 dstbits,linebytes,
3215 rDst,gDst,bDst);
3216 } else {
3217 /* ==== bgr 888 bmp -> any 0888 dib ==== */
3218 convs->Convert_bgr888_to_any0888
3219 (width,lines,
3220 srcbits,-bmpImage->bytes_per_line,
3221 dstbits,linebytes,
3222 rDst,gDst,bDst);
3224 break;
3226 /* fall through */
3228 case 32:
3230 const char* srcbits;
3232 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3234 if (gDst==bmpImage->green_mask) {
3235 if (rDst==bmpImage->red_mask && bDst==bmpImage->blue_mask) {
3236 /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
3237 /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
3238 convs->Convert_0888_asis
3239 (width,lines,
3240 srcbits,-bmpImage->bytes_per_line,
3241 dstbits,linebytes);
3242 } else if (bmpImage->green_mask!=0x00ff00 ||
3243 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3244 goto notsupported;
3245 /* the tests below assume sane bmpImage masks */
3246 } else if (rDst==bmpImage->blue_mask && bDst==bmpImage->red_mask) {
3247 /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
3248 /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
3249 convs->Convert_0888_reverse
3250 (width,lines,
3251 srcbits,-bmpImage->bytes_per_line,
3252 dstbits,linebytes);
3253 } else {
3254 /* ==== any 0888 bmp -> any 0888 dib ==== */
3255 convs->Convert_0888_any
3256 (width,lines,
3257 srcbits,-bmpImage->bytes_per_line,
3258 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3259 dstbits,linebytes,
3260 rDst,gDst,bDst);
3262 } else if (bmpImage->green_mask!=0x00ff00 ||
3263 (bmpImage->red_mask|bmpImage->blue_mask)!=0xff00ff) {
3264 goto notsupported;
3265 /* the tests below assume sane bmpImage masks */
3266 } else {
3267 /* ==== any 0888 bmp -> any 0888 dib ==== */
3268 convs->Convert_0888_any
3269 (width,lines,
3270 srcbits,-bmpImage->bytes_per_line,
3271 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3272 dstbits,linebytes,
3273 rDst,gDst,bDst);
3276 break;
3278 case 15:
3279 case 16:
3281 const char* srcbits;
3283 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3285 if (rDst==0xff0000 && gDst==0x00ff00 && bDst==0x0000ff) {
3286 if (bmpImage->green_mask==0x03e0) {
3287 if (bmpImage->red_mask==0x7f00) {
3288 /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
3289 convs->Convert_555_to_0888_asis
3290 (width,lines,
3291 srcbits,-bmpImage->bytes_per_line,
3292 dstbits,linebytes);
3293 } else if (bmpImage->blue_mask==0x7f00) {
3294 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
3295 convs->Convert_555_to_0888_reverse
3296 (width,lines,
3297 srcbits,-bmpImage->bytes_per_line,
3298 dstbits,linebytes);
3299 } else {
3300 goto notsupported;
3302 } else if (bmpImage->green_mask==0x07e0) {
3303 if (bmpImage->red_mask==0xf800) {
3304 /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
3305 convs->Convert_565_to_0888_asis
3306 (width,lines,
3307 srcbits,-bmpImage->bytes_per_line,
3308 dstbits,linebytes);
3309 } else if (bmpImage->blue_mask==0xf800) {
3310 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
3311 convs->Convert_565_to_0888_reverse
3312 (width,lines,
3313 srcbits,-bmpImage->bytes_per_line,
3314 dstbits,linebytes);
3315 } else {
3316 goto notsupported;
3318 } else {
3319 goto notsupported;
3321 } else if (rDst==0x0000ff && gDst==0x00ff00 && bDst==0xff0000) {
3322 if (bmpImage->green_mask==0x03e0) {
3323 if (bmpImage->blue_mask==0x7f00) {
3324 /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
3325 convs->Convert_555_to_0888_asis
3326 (width,lines,
3327 srcbits,-bmpImage->bytes_per_line,
3328 dstbits,linebytes);
3329 } else if (bmpImage->red_mask==0x7f00) {
3330 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
3331 convs->Convert_555_to_0888_reverse
3332 (width,lines,
3333 srcbits,-bmpImage->bytes_per_line,
3334 dstbits,linebytes);
3335 } else {
3336 goto notsupported;
3338 } else if (bmpImage->green_mask==0x07e0) {
3339 if (bmpImage->blue_mask==0xf800) {
3340 /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3341 convs->Convert_565_to_0888_asis
3342 (width,lines,
3343 srcbits,-bmpImage->bytes_per_line,
3344 dstbits,linebytes);
3345 } else if (bmpImage->red_mask==0xf800) {
3346 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3347 convs->Convert_565_to_0888_reverse
3348 (width,lines,
3349 srcbits,-bmpImage->bytes_per_line,
3350 dstbits,linebytes);
3351 } else {
3352 goto notsupported;
3354 } else {
3355 goto notsupported;
3357 } else {
3358 if (bmpImage->green_mask==0x03e0 &&
3359 (bmpImage->red_mask==0x7f00 ||
3360 bmpImage->blue_mask==0x7f00)) {
3361 /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3362 convs->Convert_5x5_to_any0888
3363 (width,lines,
3364 srcbits,-bmpImage->bytes_per_line,
3365 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3366 dstbits,linebytes,
3367 rDst,gDst,bDst);
3368 } else if (bmpImage->green_mask==0x07e0 &&
3369 (bmpImage->red_mask==0xf800 ||
3370 bmpImage->blue_mask==0xf800)) {
3371 /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3372 convs->Convert_5x5_to_any0888
3373 (width,lines,
3374 srcbits,-bmpImage->bytes_per_line,
3375 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask,
3376 dstbits,linebytes,
3377 rDst,gDst,bDst);
3378 } else {
3379 goto notsupported;
3383 break;
3385 case 1:
3386 case 4:
3387 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3388 && srccolors) {
3389 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3390 int rShift,gShift,bShift;
3391 DWORD* dstpixel;
3393 rShift=X11DRV_DIB_MaskToShift(rDst);
3394 gShift=X11DRV_DIB_MaskToShift(gDst);
3395 bShift=X11DRV_DIB_MaskToShift(bDst);
3396 for (h = lines - 1; h >= 0; h--) {
3397 dstpixel=(DWORD*)dstbits;
3398 for (x = 0; x < width; x++) {
3399 PALETTEENTRY srcval;
3400 srcval = srccolors[XGetPixel(bmpImage, x, h)];
3401 *dstpixel++=(srcval.peRed << rShift) |
3402 (srcval.peGreen << gShift) |
3403 (srcval.peBlue << bShift);
3405 dstbits += linebytes;
3407 } else {
3408 goto notsupported;
3410 break;
3412 case 8:
3413 if (X11DRV_DIB_CheckMask(bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask)
3414 && srccolors) {
3415 /* ==== pal 8 bmp -> any 0888 dib ==== */
3416 int rShift,gShift,bShift;
3417 const void* srcbits;
3418 const BYTE* srcpixel;
3419 DWORD* dstpixel;
3421 rShift=X11DRV_DIB_MaskToShift(rDst);
3422 gShift=X11DRV_DIB_MaskToShift(gDst);
3423 bShift=X11DRV_DIB_MaskToShift(bDst);
3424 srcbits=bmpImage->data+(lines-1)*bmpImage->bytes_per_line;
3425 for (h = lines - 1; h >= 0; h--) {
3426 srcpixel=srcbits;
3427 dstpixel=(DWORD*)dstbits;
3428 for (x = 0; x < width; x++) {
3429 PALETTEENTRY srcval;
3430 srcval=srccolors[*srcpixel++];
3431 *dstpixel++=(srcval.peRed << rShift) |
3432 (srcval.peGreen << gShift) |
3433 (srcval.peBlue << bShift);
3435 srcbits = (const char*)srcbits - bmpImage->bytes_per_line;
3436 dstbits += linebytes;
3438 } else {
3439 goto notsupported;
3441 break;
3443 default:
3444 notsupported:
3446 /* ==== any bmp format -> any 0888 dib ==== */
3447 int rShift,gShift,bShift;
3448 DWORD* dstpixel;
3450 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%x,%x,%x)\n",
3451 bmpImage->depth, bmpImage->red_mask,
3452 bmpImage->green_mask, bmpImage->blue_mask,
3453 rDst,gDst,bDst);
3455 rShift=X11DRV_DIB_MaskToShift(rDst);
3456 gShift=X11DRV_DIB_MaskToShift(gDst);
3457 bShift=X11DRV_DIB_MaskToShift(bDst);
3458 for (h = lines - 1; h >= 0; h--) {
3459 dstpixel=(DWORD*)dstbits;
3460 for (x = 0; x < width; x++) {
3461 COLORREF srcval;
3462 srcval=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage, x, h));
3463 *dstpixel++=(GetRValue(srcval) << rShift) |
3464 (GetGValue(srcval) << gShift) |
3465 (GetBValue(srcval) << bShift);
3467 dstbits += linebytes;
3470 break;
3474 static int XGetSubImageErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
3476 return (event->request_code == X_GetImage && event->error_code == BadMatch);
3479 /***********************************************************************
3480 * X11DRV_DIB_SetImageBits_GetSubImage
3482 * Helper for X11DRV_DIB_SetImageBits
3484 static void X11DRV_DIB_SetImageBits_GetSubImage(
3485 const X11DRV_DIB_IMAGEBITS_DESCR *descr, XImage *bmpImage)
3487 /* compressed bitmaps may contain gaps in them. So make a copy
3488 * of the existing pixels first */
3489 RECT bmprc, rc;
3491 SetRect( &bmprc, descr->xDest, descr->yDest,
3492 descr->xDest + descr->width , descr->yDest + descr->height );
3493 GetRgnBox( descr->physDev->region, &rc );
3494 /* convert from dc to drawable origin */
3495 OffsetRect( &rc, descr->physDev->dc_rect.left, descr->physDev->dc_rect.top);
3496 /* clip visible rect with bitmap */
3497 if( IntersectRect( &rc, &rc, &bmprc))
3499 X11DRV_expect_error( gdi_display, XGetSubImageErrorHandler, NULL );
3500 XGetSubImage( gdi_display, descr->drawable, rc.left, rc.top,
3501 rc.right - rc.left, rc.bottom - rc.top, AllPlanes,
3502 ZPixmap, bmpImage,
3503 descr->xSrc + rc.left - bmprc.left,
3504 descr->ySrc + rc.top - bmprc.top);
3505 X11DRV_check_error();
3509 /***********************************************************************
3510 * X11DRV_DIB_SetImageBits
3512 * Transfer the bits to an X image.
3513 * Helper function for SetDIBits() and SetDIBitsToDevice().
3515 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3517 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3518 XImage *bmpImage;
3520 wine_tsx11_lock();
3521 if (descr->image)
3522 bmpImage = descr->image;
3523 else {
3524 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3525 descr->infoWidth, lines, 32, 0 );
3526 bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
3527 if(bmpImage->data == NULL) {
3528 ERR("Out of memory!\n");
3529 XDestroyImage( bmpImage );
3530 wine_tsx11_unlock();
3531 return lines;
3534 wine_tsx11_unlock();
3536 TRACE("Dib: depth=%d r=%x g=%x b=%x\n",
3537 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3538 TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3539 bmpImage->depth,bmpImage->bits_per_pixel,
3540 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3542 /* Transfer the pixels */
3543 __TRY
3545 switch(descr->infoBpp)
3547 case 1:
3548 X11DRV_DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
3549 descr->width, descr->xSrc, (int *)(descr->colorMap),
3550 bmpImage, descr->dibpitch );
3551 break;
3552 case 4:
3553 if (descr->compression) {
3554 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3555 X11DRV_DIB_SetImageBits_RLE4( descr->lines, descr->bits,
3556 descr->infoWidth, descr->width,
3557 descr->xSrc, (int *)(descr->colorMap),
3558 bmpImage );
3559 } else
3560 X11DRV_DIB_SetImageBits_4( descr->lines, descr->bits,
3561 descr->infoWidth, descr->width,
3562 descr->xSrc, (int*)(descr->colorMap),
3563 bmpImage, descr->dibpitch );
3564 break;
3565 case 8:
3566 if (descr->compression) {
3567 X11DRV_DIB_SetImageBits_GetSubImage( descr, bmpImage);
3568 X11DRV_DIB_SetImageBits_RLE8( descr->lines, descr->bits,
3569 descr->infoWidth, descr->width,
3570 descr->xSrc, (int *)(descr->colorMap),
3571 bmpImage );
3572 } else
3573 X11DRV_DIB_SetImageBits_8( descr->lines, descr->bits,
3574 descr->infoWidth, descr->width,
3575 descr->xSrc, (int *)(descr->colorMap),
3576 bmpImage, descr->dibpitch );
3577 break;
3578 case 15:
3579 case 16:
3580 X11DRV_DIB_SetImageBits_16( descr->lines, descr->bits,
3581 descr->infoWidth, descr->width,
3582 descr->xSrc, descr->physDev,
3583 descr->rMask, descr->gMask, descr->bMask,
3584 bmpImage, descr->dibpitch);
3585 break;
3586 case 24:
3587 X11DRV_DIB_SetImageBits_24( descr->lines, descr->bits,
3588 descr->infoWidth, descr->width,
3589 descr->xSrc, descr->physDev,
3590 descr->rMask, descr->gMask, descr->bMask,
3591 bmpImage, descr->dibpitch);
3592 break;
3593 case 32:
3594 X11DRV_DIB_SetImageBits_32( descr->lines, descr->bits,
3595 descr->infoWidth, descr->width,
3596 descr->xSrc, descr->physDev,
3597 descr->rMask, descr->gMask, descr->bMask,
3598 bmpImage, descr->dibpitch);
3599 break;
3600 default:
3601 WARN("(%d): Invalid depth\n", descr->infoBpp );
3602 break;
3605 __EXCEPT_PAGE_FAULT
3607 WARN( "invalid bits pointer %p\n", descr->bits );
3608 lines = 0;
3610 __ENDTRY
3612 TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3613 descr->drawable, descr->gc, bmpImage,
3614 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3615 descr->width, descr->height);
3617 wine_tsx11_lock();
3618 if (lines)
3620 #ifdef HAVE_LIBXXSHM
3621 if (descr->image && descr->useShm)
3623 XShmPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3624 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3625 descr->width, descr->height, FALSE );
3626 XSync( gdi_display, 0 );
3628 else
3629 #endif
3630 XPutImage( gdi_display, descr->drawable, descr->gc, bmpImage,
3631 descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
3632 descr->width, descr->height );
3634 if (!descr->image) XDestroyImage( bmpImage );
3635 wine_tsx11_unlock();
3636 return lines;
3639 /***********************************************************************
3640 * X11DRV_DIB_GetImageBits
3642 * Transfer the bits from an X image.
3644 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR *descr )
3646 int lines = descr->lines >= 0 ? descr->lines : -descr->lines;
3647 XImage *bmpImage;
3649 wine_tsx11_lock();
3650 if (descr->image)
3651 bmpImage = descr->image;
3652 else {
3653 bmpImage = XCreateImage( gdi_display, visual, descr->depth, ZPixmap, 0, NULL,
3654 descr->infoWidth, lines, 32, 0 );
3655 bmpImage->data = calloc( lines, bmpImage->bytes_per_line );
3656 if(bmpImage->data == NULL) {
3657 ERR("Out of memory!\n");
3658 XDestroyImage( bmpImage );
3659 wine_tsx11_unlock();
3660 return lines;
3664 #ifdef HAVE_LIBXXSHM
3666 /* We must not call XShmGetImage() with a bitmap which is bigger than the available area.
3667 If we do, XShmGetImage() will fail (X exception), as it checks for this internally. */
3668 if((descr->image && descr->useShm) && (bmpImage->width <= (descr->width - descr->xSrc))
3669 && (bmpImage->height <= (descr->height - descr->ySrc)))
3671 int saveRed, saveGreen, saveBlue;
3673 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3674 gdi_display, descr->drawable, bmpImage,
3675 descr->xSrc, descr->ySrc, AllPlanes);
3677 /* We must save and restore the bmpImage's masks in order
3678 * to preserve them across the call to XShmGetImage, which
3679 * decides to eliminate them since it doesn't happen to know
3680 * what the format of the image is supposed to be, even though
3681 * we do. */
3682 saveRed = bmpImage->red_mask;
3683 saveBlue= bmpImage->blue_mask;
3684 saveGreen = bmpImage->green_mask;
3686 XShmGetImage( gdi_display, descr->drawable, bmpImage,
3687 descr->xSrc, descr->ySrc, AllPlanes);
3689 bmpImage->red_mask = saveRed;
3690 bmpImage->blue_mask = saveBlue;
3691 bmpImage->green_mask = saveGreen;
3693 else
3694 #endif /* HAVE_LIBXXSHM */
3696 TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3697 gdi_display, descr->drawable, descr->xSrc, descr->ySrc, descr->width,
3698 lines, AllPlanes, ZPixmap, bmpImage, descr->xDest, descr->yDest);
3699 XGetSubImage( gdi_display, descr->drawable, descr->xSrc, descr->ySrc,
3700 descr->width, lines, AllPlanes, ZPixmap,
3701 bmpImage, descr->xDest, descr->yDest );
3703 wine_tsx11_unlock();
3705 TRACE("Dib: depth=%2d r=%x g=%x b=%x\n",
3706 descr->infoBpp,descr->rMask,descr->gMask,descr->bMask);
3707 TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3708 bmpImage->depth,bmpImage->bits_per_pixel,
3709 bmpImage->red_mask,bmpImage->green_mask,bmpImage->blue_mask);
3710 /* Transfer the pixels */
3711 switch(descr->infoBpp)
3713 case 1:
3714 X11DRV_DIB_GetImageBits_1( descr->lines,(LPVOID)descr->bits,
3715 descr->infoWidth, descr->width,
3716 descr->colorMap, descr->palentry,
3717 bmpImage, descr->dibpitch );
3718 break;
3720 case 4:
3721 if (descr->compression) {
3722 FIXME("Compression not yet supported!\n");
3723 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 4 ) * abs(descr->lines))
3724 break;
3726 X11DRV_DIB_GetImageBits_4( descr->lines,(LPVOID)descr->bits,
3727 descr->infoWidth, descr->width,
3728 descr->colorMap, descr->palentry,
3729 bmpImage, descr->dibpitch );
3730 break;
3731 case 8:
3732 if (descr->compression) {
3733 FIXME("Compression not yet supported!\n");
3734 if(descr->sizeImage < X11DRV_DIB_GetDIBWidthBytes( descr->infoWidth, 8 ) * abs(descr->lines))
3735 break;
3737 X11DRV_DIB_GetImageBits_8( descr->lines, (LPVOID)descr->bits,
3738 descr->infoWidth, descr->width,
3739 descr->colorMap, descr->palentry,
3740 bmpImage, descr->dibpitch );
3741 break;
3742 case 15:
3743 case 16:
3744 X11DRV_DIB_GetImageBits_16( descr->lines, (LPVOID)descr->bits,
3745 descr->infoWidth,descr->width,
3746 descr->palentry,
3747 descr->rMask, descr->gMask, descr->bMask,
3748 bmpImage, descr->dibpitch );
3749 break;
3751 case 24:
3752 X11DRV_DIB_GetImageBits_24( descr->lines, (LPVOID)descr->bits,
3753 descr->infoWidth,descr->width,
3754 descr->palentry,
3755 descr->rMask, descr->gMask, descr->bMask,
3756 bmpImage, descr->dibpitch);
3757 break;
3759 case 32:
3760 X11DRV_DIB_GetImageBits_32( descr->lines, (LPVOID)descr->bits,
3761 descr->infoWidth, descr->width,
3762 descr->palentry,
3763 descr->rMask, descr->gMask, descr->bMask,
3764 bmpImage, descr->dibpitch);
3765 break;
3767 default:
3768 WARN("(%d): Invalid depth\n", descr->infoBpp );
3769 break;
3772 if (!descr->image)
3774 wine_tsx11_lock();
3775 XDestroyImage( bmpImage );
3776 wine_tsx11_unlock();
3778 return lines;
3781 /*************************************************************************
3782 * X11DRV_SetDIBitsToDevice
3785 INT X11DRV_SetDIBitsToDevice( X11DRV_PDEVICE *physDev, INT xDest, INT yDest, DWORD cx,
3786 DWORD cy, INT xSrc, INT ySrc,
3787 UINT startscan, UINT lines, LPCVOID bits,
3788 const BITMAPINFO *info, UINT coloruse )
3790 X11DRV_DIB_IMAGEBITS_DESCR descr;
3791 INT result;
3792 LONG width, height;
3793 BOOL top_down;
3794 POINT pt;
3795 int rop = X11DRV_XROPfunction[GetROP2(physDev->hdc) - 1];
3797 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3798 &descr.infoBpp, &descr.compression ) == -1)
3799 return 0;
3801 top_down = (height < 0);
3802 if (top_down) height = -height;
3804 pt.x = xDest;
3805 pt.y = yDest;
3806 LPtoDP(physDev->hdc, &pt, 1);
3808 if (!lines || (startscan >= height)) return 0;
3809 if (!top_down && startscan + lines > height) lines = height - startscan;
3811 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3812 * and clamp all values to fit inside [startscan,startscan+lines]
3814 if (ySrc + cy <= startscan + lines)
3816 UINT y = startscan + lines - (ySrc + cy);
3817 if (ySrc < startscan) cy -= (startscan - ySrc);
3818 if (!top_down)
3820 /* avoid getting unnecessary lines */
3821 ySrc = 0;
3822 if (y >= lines) return 0;
3823 lines -= y;
3825 else
3827 if (y >= lines) return lines;
3828 ySrc = y; /* need to get all lines in top down mode */
3831 else
3833 if (ySrc >= startscan + lines) return lines;
3834 pt.y += ySrc + cy - (startscan + lines);
3835 cy = startscan + lines - ySrc;
3836 ySrc = 0;
3837 if (cy > lines) cy = lines;
3839 if (xSrc >= width) return lines;
3840 if (xSrc + cx >= width) cx = width - xSrc;
3841 if (!cx || !cy) return lines;
3843 /* Update the pixmap from the DIB section */
3844 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
3846 X11DRV_SetupGCForText( physDev ); /* To have the correct colors */
3847 wine_tsx11_lock();
3848 XSetFunction(gdi_display, physDev->gc, rop);
3849 wine_tsx11_unlock();
3851 switch (descr.infoBpp)
3853 case 1:
3854 case 4:
3855 case 8:
3856 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3857 physDev, coloruse,
3858 physDev->depth, info, &descr.nColorMap );
3859 if (!descr.colorMap) return 0;
3860 descr.rMask = descr.gMask = descr.bMask = 0;
3861 break;
3862 case 15:
3863 case 16:
3864 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3865 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3866 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3867 descr.colorMap = 0;
3868 break;
3870 case 24:
3871 case 32:
3872 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3873 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3874 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3875 descr.colorMap = 0;
3876 break;
3879 descr.physDev = physDev;
3880 descr.bits = bits;
3881 descr.image = NULL;
3882 descr.palentry = NULL;
3883 descr.lines = top_down ? -lines : lines;
3884 descr.infoWidth = width;
3885 descr.depth = physDev->depth;
3886 descr.drawable = physDev->drawable;
3887 descr.gc = physDev->gc;
3888 descr.xSrc = xSrc;
3889 descr.ySrc = ySrc;
3890 descr.xDest = physDev->dc_rect.left + pt.x;
3891 descr.yDest = physDev->dc_rect.top + pt.y;
3892 descr.width = cx;
3893 descr.height = cy;
3894 descr.useShm = FALSE;
3895 descr.dibpitch = ((width * descr.infoBpp + 31) &~31) / 8;
3897 result = X11DRV_DIB_SetImageBits( &descr );
3899 if (descr.infoBpp <= 8)
3900 HeapFree(GetProcessHeap(), 0, descr.colorMap);
3902 /* Update the DIBSection of the pixmap */
3903 X11DRV_UnlockDIBSection(physDev, TRUE);
3905 return result;
3908 /***********************************************************************
3909 * SetDIBits (X11DRV.@)
3911 INT X11DRV_SetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan,
3912 UINT lines, LPCVOID bits, const BITMAPINFO *info, UINT coloruse )
3914 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
3915 X11DRV_DIB_IMAGEBITS_DESCR descr;
3916 BITMAP bitmap;
3917 LONG width, height, tmpheight;
3918 INT result;
3920 descr.physDev = physDev;
3922 if (!physBitmap) return 0;
3924 if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height,
3925 &descr.infoBpp, &descr.compression ) == -1)
3926 return 0;
3928 tmpheight = height;
3929 if (height < 0) height = -height;
3930 if (!lines || (startscan >= height))
3931 return 0;
3933 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
3935 if (startscan + lines > height) lines = height - startscan;
3937 switch (descr.infoBpp)
3939 case 1:
3940 case 4:
3941 case 8:
3942 descr.colorMap = (RGBQUAD *)X11DRV_DIB_BuildColorMap(
3943 descr.physDev, coloruse,
3944 physBitmap->pixmap_depth,
3945 info, &descr.nColorMap );
3946 if (!descr.colorMap) return 0;
3947 descr.rMask = descr.gMask = descr.bMask = 0;
3948 break;
3949 case 15:
3950 case 16:
3951 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
3952 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
3953 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
3954 descr.colorMap = 0;
3955 break;
3957 case 24:
3958 case 32:
3959 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
3960 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
3961 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
3962 descr.colorMap = 0;
3963 break;
3965 default: break;
3968 descr.bits = bits;
3969 descr.image = NULL;
3970 descr.palentry = NULL;
3971 descr.infoWidth = width;
3972 descr.lines = tmpheight >= 0 ? lines : -lines;
3973 descr.depth = physBitmap->pixmap_depth;
3974 descr.drawable = physBitmap->pixmap;
3975 descr.gc = BITMAP_GC(physBitmap);
3976 descr.xSrc = 0;
3977 descr.ySrc = 0;
3978 descr.xDest = 0;
3979 descr.yDest = height - startscan - lines;
3980 descr.width = bitmap.bmWidth;
3981 descr.height = lines;
3982 descr.useShm = FALSE;
3983 descr.dibpitch = ((descr.infoWidth * descr.infoBpp + 31) &~31) / 8;
3984 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
3985 result = X11DRV_DIB_SetImageBits( &descr );
3987 /* optimisation for the case where the input bits are in exactly the same
3988 * format as the internal representation and copying to the app bits is
3989 * cheap - saves a round trip to the X server */
3990 if (descr.compression == BI_RGB &&
3991 coloruse == DIB_RGB_COLORS &&
3992 descr.infoBpp == bitmap.bmBitsPixel &&
3993 physBitmap->base && physBitmap->size < 65536)
3995 unsigned int srcwidthb = bitmap.bmWidthBytes;
3996 int dstwidthb = X11DRV_DIB_GetDIBWidthBytes( width, descr.infoBpp );
3997 LPBYTE dbits = physBitmap->base, sbits = (LPBYTE)bits + (startscan * srcwidthb);
3998 int widthb;
3999 UINT y;
4001 TRACE("syncing compatible set bits to app bits\n");
4002 if ((tmpheight < 0) ^ (bitmap.bmHeight < 0))
4004 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
4005 dstwidthb = -dstwidthb;
4007 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4008 widthb = min(srcwidthb, abs(dstwidthb));
4009 for (y = 0; y < lines; y++, dbits += dstwidthb, sbits += srcwidthb)
4010 memcpy(dbits, sbits, widthb);
4011 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4012 physBitmap->status = DIB_Status_InSync;
4014 X11DRV_DIB_Unlock( physBitmap, TRUE );
4016 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4018 return result;
4021 /***********************************************************************
4022 * GetDIBits (X11DRV.@)
4024 INT X11DRV_GetDIBits( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, UINT startscan, UINT lines,
4025 LPVOID bits, BITMAPINFO *info, UINT coloruse )
4027 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
4028 DIBSECTION dib;
4029 X11DRV_DIB_IMAGEBITS_DESCR descr;
4030 PALETTEENTRY palette[256];
4031 size_t obj_size;
4032 int height;
4033 LONG width, tempHeight;
4034 int bitmap_type;
4035 BOOL core_header;
4036 void* colorPtr;
4038 GetPaletteEntries( GetCurrentObject( physDev->hdc, OBJ_PAL ), 0, 256, palette );
4040 if (!physBitmap) return 0;
4041 if (!(obj_size = GetObjectW( hbitmap, sizeof(dib), &dib ))) return 0;
4043 bitmap_type = DIB_GetBitmapInfo( (BITMAPINFOHEADER*)info, &width, &tempHeight, &descr.infoBpp, &descr.compression);
4044 if (bitmap_type == -1)
4046 ERR("Invalid bitmap\n");
4047 return 0;
4049 descr.lines = tempHeight;
4050 core_header = (bitmap_type == 0);
4051 colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
4053 TRACE("%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
4054 lines, dib.dsBm.bmWidth, dib.dsBm.bmHeight, width, descr.lines, startscan);
4056 if( lines > dib.dsBm.bmHeight ) lines = dib.dsBm.bmHeight;
4058 height = descr.lines;
4059 if (height < 0) height = -height;
4060 if( lines > height ) lines = height;
4061 /* Top-down images have a negative biHeight, the scanlines of these images
4062 * were inverted in X11DRV_DIB_GetImageBits_xx
4063 * To prevent this we simply change the sign of lines
4064 * (the number of scan lines to copy).
4065 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4067 if( descr.lines < 0 && lines > 0) lines = -lines;
4069 if( startscan >= dib.dsBm.bmHeight ) return 0;
4071 descr.colorMap = NULL;
4073 switch (descr.infoBpp)
4075 case 1:
4076 case 4:
4077 case 8:
4078 descr.rMask= descr.gMask = descr.bMask = 0;
4079 if(coloruse == DIB_RGB_COLORS)
4080 descr.colorMap = colorPtr;
4081 else {
4082 int num_colors = 1 << descr.infoBpp, i;
4083 RGBQUAD *rgb;
4084 COLORREF colref;
4085 WORD *index = (WORD*)colorPtr;
4086 descr.colorMap = rgb = HeapAlloc(GetProcessHeap(), 0, num_colors * sizeof(RGBQUAD));
4087 for(i = 0; i < num_colors; i++, rgb++, index++) {
4088 colref = X11DRV_PALETTE_ToLogical(X11DRV_PALETTE_ToPhysical(physDev, PALETTEINDEX(*index)));
4089 rgb->rgbRed = GetRValue(colref);
4090 rgb->rgbGreen = GetGValue(colref);
4091 rgb->rgbBlue = GetBValue(colref);
4092 rgb->rgbReserved = 0;
4095 break;
4096 case 15:
4097 case 16:
4098 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0x7c00;
4099 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x03e0;
4100 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x001f;
4101 break;
4102 case 24:
4103 case 32:
4104 descr.rMask = (descr.compression == BI_BITFIELDS) ? *(const DWORD *)info->bmiColors : 0xff0000;
4105 descr.gMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 1) : 0x00ff00;
4106 descr.bMask = (descr.compression == BI_BITFIELDS) ? *((const DWORD *)info->bmiColors + 2) : 0x0000ff;
4107 break;
4110 descr.physDev = physDev;
4111 descr.palentry = palette;
4112 descr.bits = bits;
4113 descr.image = physBitmap->image;
4114 descr.infoWidth = width;
4115 descr.lines = lines;
4116 descr.depth = physBitmap->pixmap_depth;
4117 descr.drawable = physBitmap->pixmap;
4118 descr.gc = BITMAP_GC(physBitmap);
4119 descr.width = dib.dsBm.bmWidth;
4120 descr.height = dib.dsBm.bmHeight;
4121 descr.xDest = 0;
4122 descr.yDest = 0;
4123 descr.xSrc = 0;
4124 descr.sizeImage = core_header ? 0 : info->bmiHeader.biSizeImage;
4126 if (descr.lines > 0)
4128 descr.ySrc = (descr.height-1) - (startscan + (lines-1));
4130 else
4132 descr.ySrc = startscan;
4134 #ifdef HAVE_LIBXXSHM
4135 descr.useShm = (obj_size == sizeof(DIBSECTION)) && (physBitmap->shminfo.shmid != -1);
4136 #else
4137 descr.useShm = FALSE;
4138 #endif
4139 descr.dibpitch = (obj_size == sizeof(DIBSECTION)) ? dib.dsBm.bmWidthBytes
4140 : (((descr.infoWidth * descr.infoBpp + 31) &~31) / 8);
4142 X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
4143 X11DRV_DIB_GetImageBits( &descr );
4144 X11DRV_DIB_Unlock( physBitmap, TRUE );
4146 if(!core_header && info->bmiHeader.biSizeImage == 0) /* Fill in biSizeImage */
4147 info->bmiHeader.biSizeImage = X11DRV_DIB_GetDIBImageBytes( descr.infoWidth,
4148 descr.lines,
4149 descr.infoBpp);
4151 if (descr.compression == BI_BITFIELDS)
4153 *(DWORD *)info->bmiColors = descr.rMask;
4154 *((DWORD *)info->bmiColors + 1) = descr.gMask;
4155 *((DWORD *)info->bmiColors + 2) = descr.bMask;
4157 else if (!core_header)
4159 /* if RLE or JPEG compression were supported,
4160 * this line would be invalid. */
4161 info->bmiHeader.biCompression = 0;
4164 if(descr.colorMap != colorPtr)
4165 HeapFree(GetProcessHeap(), 0, descr.colorMap);
4166 return lines;
4169 /***********************************************************************
4170 * X11DRV_DIB_DoCopyDIBSection
4172 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB,
4173 void *colorMap, int nColorMap,
4174 Drawable dest, GC gc,
4175 DWORD xSrc, DWORD ySrc,
4176 DWORD xDest, DWORD yDest,
4177 DWORD width, DWORD height)
4179 DIBSECTION dibSection;
4180 X11DRV_DIB_IMAGEBITS_DESCR descr;
4181 int identity[2] = {0,1};
4183 if (!GetObjectW( physBitmap->hbitmap, sizeof(dibSection), &dibSection )) return;
4185 descr.physDev = NULL;
4186 descr.palentry = NULL;
4187 descr.infoWidth = dibSection.dsBmih.biWidth;
4188 descr.infoBpp = dibSection.dsBmih.biBitCount;
4189 descr.lines = dibSection.dsBmih.biHeight;
4190 descr.image = physBitmap->image;
4191 descr.colorMap = colorMap;
4192 descr.nColorMap = nColorMap;
4193 descr.bits = dibSection.dsBm.bmBits;
4194 descr.depth = physBitmap->pixmap_depth;
4195 descr.compression = dibSection.dsBmih.biCompression;
4197 if(descr.infoBpp == 1)
4198 descr.colorMap = (void*)identity;
4200 switch (descr.infoBpp)
4202 case 1:
4203 case 4:
4204 case 8:
4205 descr.rMask = descr.gMask = descr.bMask = 0;
4206 break;
4207 case 15:
4208 case 16:
4209 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0x7c00;
4210 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x03e0;
4211 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x001f;
4212 break;
4214 case 24:
4215 case 32:
4216 descr.rMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[0] : 0xff0000;
4217 descr.gMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[1] : 0x00ff00;
4218 descr.bMask = (descr.compression == BI_BITFIELDS) ? dibSection.dsBitfields[2] : 0x0000ff;
4219 break;
4222 /* Hack for now */
4223 descr.drawable = dest;
4224 descr.gc = gc;
4225 descr.xSrc = xSrc;
4226 descr.ySrc = ySrc;
4227 descr.xDest = xDest;
4228 descr.yDest = yDest;
4229 descr.width = width;
4230 descr.height = height;
4231 descr.sizeImage = 0;
4233 #ifdef HAVE_LIBXXSHM
4234 descr.useShm = (physBitmap->shminfo.shmid != -1);
4235 #else
4236 descr.useShm = FALSE;
4237 #endif
4238 descr.dibpitch = dibSection.dsBm.bmWidthBytes;
4240 if (toDIB)
4242 TRACE("Copying from Pixmap to DIB bits\n");
4243 X11DRV_DIB_GetImageBits( &descr );
4245 else
4247 TRACE("Copying from DIB bits to Pixmap\n");
4248 X11DRV_DIB_SetImageBits( &descr );
4252 /***********************************************************************
4253 * X11DRV_DIB_CopyDIBSection
4255 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
4256 DWORD xSrc, DWORD ySrc, DWORD xDest, DWORD yDest,
4257 DWORD width, DWORD height)
4259 DIBSECTION dib;
4260 X_PHYSBITMAP *physBitmap;
4261 unsigned int nColorMap;
4262 int* x11ColorMap;
4263 int freeColorMap;
4265 TRACE("(%p,%p,%d,%d,%d,%d,%d,%d)\n", physDevSrc->hdc, physDevDst->hdc,
4266 xSrc, ySrc, xDest, yDest, width, height);
4267 /* this function is meant as an optimization for BitBlt,
4268 * not to be called otherwise */
4269 physBitmap = physDevSrc->bitmap;
4270 if (!physBitmap || GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib ) != sizeof(dib))
4272 ERR("called for non-DIBSection!?\n");
4273 return;
4275 /* while BitBlt should already have made sure we only get
4276 * positive values, we should check for oversize values */
4277 if ((xSrc < dib.dsBm.bmWidth) &&
4278 (ySrc < dib.dsBm.bmHeight)) {
4279 if (xSrc + width > dib.dsBm.bmWidth)
4280 width = dib.dsBm.bmWidth - xSrc;
4281 if (ySrc + height > dib.dsBm.bmHeight)
4282 height = dib.dsBm.bmHeight - ySrc;
4283 /* if the source bitmap is 8bpp or less, we're supposed to use the
4284 * DC's palette for color conversion (not the DIB color table) */
4285 if (dib.dsBm.bmBitsPixel <= 8) {
4286 HPALETTE hPalette = GetCurrentObject( physDevSrc->hdc, OBJ_PAL );
4287 if (!hPalette || (hPalette == GetStockObject(DEFAULT_PALETTE))) {
4288 /* HACK: no palette has been set in the source DC,
4289 * use the DIB colormap instead - this is necessary in some
4290 * cases since we need to do depth conversion in some places
4291 * where real Windows can just copy data straight over */
4292 x11ColorMap = physBitmap->colorMap;
4293 nColorMap = physBitmap->nColorMap;
4294 freeColorMap = FALSE;
4295 } else {
4296 const BITMAPINFO* info = (BITMAPINFO*)&dib.dsBmih;
4297 int i;
4299 nColorMap = X11DRV_DIB_GetColorCount(info);
4300 x11ColorMap = HeapAlloc(GetProcessHeap(), 0, nColorMap * sizeof(int));
4301 for (i = 0; i < nColorMap; i++)
4302 x11ColorMap[i] = X11DRV_PALETTE_ToPhysical(physDevSrc, PALETTEINDEX(i));
4303 freeColorMap = TRUE;
4306 else
4308 nColorMap = 0;
4309 x11ColorMap = NULL;
4310 freeColorMap = FALSE;
4312 /* perform the copy */
4313 X11DRV_DIB_DoCopyDIBSection(physBitmap, FALSE, x11ColorMap, nColorMap,
4314 physDevDst->drawable, physDevDst->gc, xSrc, ySrc,
4315 physDevDst->dc_rect.left + xDest, physDevDst->dc_rect.top + yDest,
4316 width, height);
4317 /* free color mapping */
4318 if (freeColorMap)
4319 HeapFree(GetProcessHeap(), 0, x11ColorMap);
4323 /***********************************************************************
4324 * X11DRV_DIB_DoUpdateDIBSection
4326 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP *physBitmap, BOOL toDIB)
4328 BITMAP bitmap;
4330 GetObjectW( physBitmap->hbitmap, sizeof(bitmap), &bitmap );
4331 X11DRV_DIB_DoCopyDIBSection(physBitmap, toDIB,
4332 physBitmap->colorMap, physBitmap->nColorMap,
4333 physBitmap->pixmap, BITMAP_GC(physBitmap),
4334 0, 0, 0, 0, bitmap.bmWidth, bitmap.bmHeight);
4337 /***********************************************************************
4338 * X11DRV_DIB_FaultHandler
4340 static LONG CALLBACK X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep )
4342 X_PHYSBITMAP *physBitmap = NULL;
4343 BOOL found = FALSE;
4344 BYTE *addr;
4345 struct list *ptr;
4346 const size_t pagemask = getpagesize() - 1;
4348 if (ep->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
4349 return EXCEPTION_CONTINUE_SEARCH;
4351 addr = (BYTE *)ep->ExceptionRecord->ExceptionInformation[1];
4353 EnterCriticalSection(&dibs_cs);
4354 LIST_FOR_EACH( ptr, &dibs_list )
4356 physBitmap = LIST_ENTRY( ptr, X_PHYSBITMAP, entry );
4357 if ((physBitmap->base <= addr) &&
4358 (addr < physBitmap->base + ((physBitmap->size + pagemask) & ~pagemask)))
4360 found = TRUE;
4361 break;
4364 LeaveCriticalSection(&dibs_cs);
4366 if (!found) return EXCEPTION_CONTINUE_SEARCH;
4368 if (addr >= physBitmap->base + physBitmap->size)
4369 WARN( "%p: access to %p beyond the end of the DIB\n", physBitmap->hbitmap, addr );
4371 X11DRV_DIB_Lock( physBitmap, DIB_Status_None );
4372 if (ep->ExceptionRecord->ExceptionInformation[0] == EXCEPTION_WRITE_FAULT) {
4373 /* the app tried to write the DIB bits */
4374 X11DRV_DIB_Coerce( physBitmap, DIB_Status_AppMod);
4375 } else {
4376 /* the app tried to read the DIB bits */
4377 X11DRV_DIB_Coerce( physBitmap, DIB_Status_InSync);
4379 X11DRV_DIB_Unlock( physBitmap, TRUE );
4381 return EXCEPTION_CONTINUE_EXECUTION;
4384 /***********************************************************************
4385 * X11DRV_DIB_Coerce
4387 static INT X11DRV_DIB_Coerce(X_PHYSBITMAP *physBitmap, INT req)
4389 INT ret = DIB_Status_None;
4391 if (!physBitmap->image) return ret; /* not a DIB section */
4392 EnterCriticalSection(&physBitmap->lock);
4393 ret = physBitmap->status;
4394 switch (req) {
4395 case DIB_Status_GdiMod:
4396 /* GDI access - request to draw on pixmap */
4397 switch (physBitmap->status)
4399 default:
4400 case DIB_Status_None:
4401 physBitmap->p_status = DIB_Status_GdiMod;
4402 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4403 break;
4405 case DIB_Status_GdiMod:
4406 TRACE("GdiMod requested in status GdiMod\n" );
4407 physBitmap->p_status = DIB_Status_GdiMod;
4408 break;
4410 case DIB_Status_InSync:
4411 TRACE("GdiMod requested in status InSync\n" );
4412 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4413 physBitmap->status = DIB_Status_GdiMod;
4414 physBitmap->p_status = DIB_Status_InSync;
4415 break;
4417 case DIB_Status_AppMod:
4418 TRACE("GdiMod requested in status AppMod\n" );
4419 /* make it readonly to avoid app changing data while we copy */
4420 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4421 X11DRV_DIB_DoUpdateDIBSection( physBitmap, FALSE );
4422 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_NOACCESS );
4423 physBitmap->p_status = DIB_Status_AppMod;
4424 physBitmap->status = DIB_Status_GdiMod;
4425 break;
4427 break;
4429 case DIB_Status_InSync:
4430 /* App access - request access to read DIB surface */
4431 /* (typically called from signal handler) */
4432 switch (physBitmap->status)
4434 default:
4435 case DIB_Status_None:
4436 /* shouldn't happen from signal handler */
4437 break;
4439 case DIB_Status_GdiMod:
4440 TRACE("InSync requested in status GdiMod\n" );
4441 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4442 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4443 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4444 physBitmap->status = DIB_Status_InSync;
4445 break;
4447 case DIB_Status_InSync:
4448 TRACE("InSync requested in status InSync\n" );
4449 /* shouldn't happen from signal handler */
4450 break;
4452 case DIB_Status_AppMod:
4453 TRACE("InSync requested in status AppMod\n" );
4454 /* no reason to do anything here, and this
4455 * shouldn't happen from signal handler */
4456 break;
4458 break;
4460 case DIB_Status_AppMod:
4461 /* App access - request access to write DIB surface */
4462 /* (typically called from signal handler) */
4463 switch (physBitmap->status)
4465 default:
4466 case DIB_Status_None:
4467 /* shouldn't happen from signal handler */
4468 break;
4470 case DIB_Status_GdiMod:
4471 TRACE("AppMod requested in status GdiMod\n" );
4472 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4473 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4474 physBitmap->status = DIB_Status_AppMod;
4475 break;
4477 case DIB_Status_InSync:
4478 TRACE("AppMod requested in status InSync\n" );
4479 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4480 physBitmap->status = DIB_Status_AppMod;
4481 break;
4483 case DIB_Status_AppMod:
4484 TRACE("AppMod requested in status AppMod\n" );
4485 /* shouldn't happen from signal handler */
4486 break;
4488 break;
4490 /* it is up to the caller to do the copy/conversion, probably
4491 * using the return value to decide where to copy from */
4493 LeaveCriticalSection(&physBitmap->lock);
4494 return ret;
4497 /***********************************************************************
4498 * X11DRV_DIB_Lock
4500 static INT X11DRV_DIB_Lock(X_PHYSBITMAP *physBitmap, INT req)
4502 INT ret = DIB_Status_None;
4504 if (!physBitmap->image) return ret; /* not a DIB section */
4505 TRACE("Locking %p from thread %04x\n", physBitmap->hbitmap, GetCurrentThreadId());
4506 EnterCriticalSection(&physBitmap->lock);
4507 ret = physBitmap->status;
4508 if (req != DIB_Status_None)
4509 X11DRV_DIB_Coerce(physBitmap, req);
4510 return ret;
4513 /***********************************************************************
4514 * X11DRV_DIB_Unlock
4516 static void X11DRV_DIB_Unlock(X_PHYSBITMAP *physBitmap, BOOL commit)
4518 if (!physBitmap->image) return; /* not a DIB section */
4519 switch (physBitmap->status)
4521 default:
4522 case DIB_Status_None:
4523 /* in case anyone is wondering, this is the "signal handler doesn't
4524 * work" case, where we always have to be ready for app access */
4525 if (commit) {
4526 switch (physBitmap->p_status)
4528 case DIB_Status_GdiMod:
4529 TRACE("Unlocking and syncing from GdiMod\n" );
4530 X11DRV_DIB_DoUpdateDIBSection( physBitmap, TRUE );
4531 break;
4533 default:
4534 TRACE("Unlocking without needing to sync\n" );
4535 break;
4538 else TRACE("Unlocking with no changes\n");
4539 physBitmap->p_status = DIB_Status_None;
4540 break;
4542 case DIB_Status_GdiMod:
4543 TRACE("Unlocking in status GdiMod\n" );
4544 /* DIB was protected in Coerce */
4545 if (!commit) {
4546 /* no commit, revert to InSync if applicable */
4547 if ((physBitmap->p_status == DIB_Status_InSync) ||
4548 (physBitmap->p_status == DIB_Status_AppMod)) {
4549 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READONLY );
4550 physBitmap->status = DIB_Status_InSync;
4553 break;
4555 case DIB_Status_InSync:
4556 TRACE("Unlocking in status InSync\n" );
4557 /* DIB was already protected in Coerce */
4558 break;
4560 case DIB_Status_AppMod:
4561 TRACE("Unlocking in status AppMod\n" );
4562 /* DIB was already protected in Coerce */
4563 /* this case is ordinary only called from the signal handler,
4564 * so we don't bother to check for !commit */
4565 break;
4567 LeaveCriticalSection(&physBitmap->lock);
4568 TRACE("Unlocked %p\n", physBitmap->hbitmap);
4571 /***********************************************************************
4572 * X11DRV_CoerceDIBSection
4574 INT X11DRV_CoerceDIBSection(X11DRV_PDEVICE *physDev, INT req)
4576 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4577 return X11DRV_DIB_Coerce(physDev->bitmap, req);
4580 /***********************************************************************
4581 * X11DRV_LockDIBSection
4583 INT X11DRV_LockDIBSection(X11DRV_PDEVICE *physDev, INT req)
4585 if (!physDev || !physDev->bitmap) return DIB_Status_None;
4586 return X11DRV_DIB_Lock(physDev->bitmap, req);
4589 /***********************************************************************
4590 * X11DRV_UnlockDIBSection
4592 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE *physDev, BOOL commit)
4594 if (!physDev || !physDev->bitmap) return;
4595 X11DRV_DIB_Unlock(physDev->bitmap, commit);
4599 #ifdef HAVE_LIBXXSHM
4600 /***********************************************************************
4601 * X11DRV_XShmErrorHandler
4604 static int XShmErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
4606 return 1; /* FIXME: should check event contents */
4609 /***********************************************************************
4610 * X11DRV_XShmCreateImage
4613 static XImage *X11DRV_XShmCreateImage( int width, int height, int bpp,
4614 XShmSegmentInfo* shminfo)
4616 XImage *image;
4618 image = XShmCreateImage(gdi_display, visual, bpp, ZPixmap, NULL, shminfo, width, height);
4619 if (image)
4621 shminfo->shmid = shmget(IPC_PRIVATE, image->bytes_per_line * height,
4622 IPC_CREAT|0700);
4623 if( shminfo->shmid != -1 )
4625 shminfo->shmaddr = image->data = shmat(shminfo->shmid, 0, 0);
4626 if( shminfo->shmaddr != (char*)-1 )
4628 BOOL ok;
4630 shminfo->readOnly = FALSE;
4631 X11DRV_expect_error( gdi_display, XShmErrorHandler, NULL );
4632 ok = (XShmAttach( gdi_display, shminfo ) != 0);
4633 XSync( gdi_display, False );
4634 if (X11DRV_check_error()) ok = FALSE;
4635 if (ok)
4637 shmctl(shminfo->shmid, IPC_RMID, 0);
4638 return image; /* Success! */
4640 /* An error occurred */
4641 shmdt(shminfo->shmaddr);
4643 shmctl(shminfo->shmid, IPC_RMID, 0);
4644 shminfo->shmid = -1;
4646 XFlush(gdi_display);
4647 XDestroyImage(image);
4648 image = NULL;
4650 return image;
4652 #endif /* HAVE_LIBXXSHM */
4655 /***********************************************************************
4656 * X11DRV_CreateDIBSection (X11DRV.@)
4658 HBITMAP X11DRV_CreateDIBSection( X11DRV_PDEVICE *physDev, HBITMAP hbitmap,
4659 const BITMAPINFO *bmi, UINT usage )
4661 X_PHYSBITMAP *physBitmap;
4662 DIBSECTION dib;
4664 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return 0;
4665 physBitmap->status = DIB_Status_None;
4667 GetObjectW( hbitmap, sizeof(dib), &dib );
4669 /* create color map */
4670 if (dib.dsBm.bmBitsPixel <= 8)
4672 physBitmap->colorMap = X11DRV_DIB_BuildColorMap( physDev,
4673 usage, dib.dsBm.bmBitsPixel, bmi,
4674 &physBitmap->nColorMap );
4677 /* create pixmap and X image */
4678 wine_tsx11_lock();
4679 physBitmap->pixmap_depth = (dib.dsBm.bmBitsPixel == 1) ? 1 : screen_depth;
4680 physBitmap->pixmap = XCreatePixmap( gdi_display, root_window, dib.dsBm.bmWidth,
4681 dib.dsBm.bmHeight, physBitmap->pixmap_depth );
4682 #ifdef HAVE_LIBXXSHM
4683 physBitmap->shminfo.shmid = -1;
4684 if (!XShmQueryExtension(gdi_display) ||
4685 !(physBitmap->image = X11DRV_XShmCreateImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4686 physBitmap->pixmap_depth, &physBitmap->shminfo )) )
4687 #endif
4688 physBitmap->image = X11DRV_DIB_CreateXImage( dib.dsBm.bmWidth, dib.dsBm.bmHeight,
4689 physBitmap->pixmap_depth );
4690 wine_tsx11_unlock();
4691 if (!physBitmap->pixmap || !physBitmap->image) return 0;
4693 /* install fault handler */
4694 InitializeCriticalSection( &physBitmap->lock );
4695 physBitmap->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": X_PHYSBITMAP.lock");
4697 physBitmap->base = dib.dsBm.bmBits;
4698 physBitmap->size = dib.dsBmih.biSizeImage;
4699 physBitmap->status = DIB_Status_AppMod;
4701 if (!dibs_handler)
4702 dibs_handler = AddVectoredExceptionHandler( TRUE, X11DRV_DIB_FaultHandler );
4703 EnterCriticalSection( &dibs_cs );
4704 list_add_head( &dibs_list, &physBitmap->entry );
4705 LeaveCriticalSection( &dibs_cs );
4707 X11DRV_DIB_DoProtectDIBSection( physBitmap, PAGE_READWRITE );
4709 return hbitmap;
4712 /***********************************************************************
4713 * X11DRV_DIB_DeleteDIBSection
4715 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP *physBitmap, DIBSECTION *dib)
4717 BOOL last;
4719 EnterCriticalSection( &dibs_cs );
4720 list_remove( &physBitmap->entry );
4721 last = list_empty( &dibs_list );
4722 LeaveCriticalSection( &dibs_cs );
4724 if (last)
4726 RemoveVectoredExceptionHandler( dibs_handler );
4727 dibs_handler = NULL;
4730 if (dib->dshSection)
4731 X11DRV_DIB_Coerce(physBitmap, DIB_Status_InSync);
4733 if (physBitmap->image)
4735 wine_tsx11_lock();
4736 #ifdef HAVE_LIBXXSHM
4737 if (physBitmap->shminfo.shmid != -1)
4739 XShmDetach( gdi_display, &(physBitmap->shminfo) );
4740 XDestroyImage( physBitmap->image );
4741 shmdt( physBitmap->shminfo.shmaddr );
4742 physBitmap->shminfo.shmid = -1;
4744 else
4745 #endif
4746 XDestroyImage( physBitmap->image );
4747 wine_tsx11_unlock();
4750 HeapFree(GetProcessHeap(), 0, physBitmap->colorMap);
4751 physBitmap->lock.DebugInfo->Spare[0] = 0;
4752 DeleteCriticalSection(&physBitmap->lock);
4755 /***********************************************************************
4756 * SetDIBColorTable (X11DRV.@)
4758 UINT X11DRV_SetDIBColorTable( X11DRV_PDEVICE *physDev, UINT start, UINT count, const RGBQUAD *colors )
4760 DIBSECTION dib;
4761 UINT ret = 0;
4762 X_PHYSBITMAP *physBitmap = physDev->bitmap;
4764 if (!physBitmap) return 0;
4765 GetObjectW( physBitmap->hbitmap, sizeof(dib), &dib );
4767 if (physBitmap->colorMap && start < physBitmap->nColorMap) {
4768 UINT end = count + start;
4769 if (end > physBitmap->nColorMap) end = physBitmap->nColorMap;
4771 * Changing color table might change the mapping between
4772 * DIB colors and X11 colors and thus alter the visible state
4773 * of the bitmap object.
4776 * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4777 * at least for a 1 bpp dibsection
4779 X11DRV_DIB_Lock( physBitmap, DIB_Status_AppMod );
4780 X11DRV_DIB_GenColorMap( physDev, physBitmap->colorMap, DIB_RGB_COLORS,
4781 dib.dsBm.bmBitsPixel,
4782 TRUE, colors, start, end );
4783 X11DRV_DIB_Unlock( physBitmap, TRUE );
4784 ret = end - start;
4786 return ret;
4790 /***********************************************************************
4791 * X11DRV_DIB_CreateDIBFromBitmap
4793 * Allocates a packed DIB and copies the bitmap data into it.
4795 HGLOBAL X11DRV_DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
4797 BITMAP bmp;
4798 HGLOBAL hPackedDIB;
4799 LPBYTE pPackedDIB;
4800 LPBITMAPINFOHEADER pbmiHeader;
4801 unsigned int cDataSize, cPackedSize, OffsetBits;
4802 int nLinesCopied;
4804 if (!GetObjectW( hBmp, sizeof(bmp), &bmp )) return 0;
4807 * A packed DIB contains a BITMAPINFO structure followed immediately by
4808 * an optional color palette and the pixel data.
4811 /* Calculate the size of the packed DIB */
4812 cDataSize = X11DRV_DIB_GetDIBWidthBytes( bmp.bmWidth, bmp.bmBitsPixel ) * abs( bmp.bmHeight );
4813 cPackedSize = sizeof(BITMAPINFOHEADER)
4814 + ( (bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0 )
4815 + cDataSize;
4816 /* Get the offset to the bits */
4817 OffsetBits = cPackedSize - cDataSize;
4819 /* Allocate the packed DIB */
4820 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
4821 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
4822 cPackedSize );
4823 if ( !hPackedDIB )
4825 WARN("Could not allocate packed DIB!\n");
4826 return 0;
4829 /* A packed DIB starts with a BITMAPINFOHEADER */
4830 pPackedDIB = GlobalLock(hPackedDIB);
4831 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
4833 /* Init the BITMAPINFOHEADER */
4834 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
4835 pbmiHeader->biWidth = bmp.bmWidth;
4836 pbmiHeader->biHeight = bmp.bmHeight;
4837 pbmiHeader->biPlanes = 1;
4838 pbmiHeader->biBitCount = bmp.bmBitsPixel;
4839 pbmiHeader->biCompression = BI_RGB;
4840 pbmiHeader->biSizeImage = 0;
4841 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
4842 pbmiHeader->biClrUsed = 0;
4843 pbmiHeader->biClrImportant = 0;
4845 /* Retrieve the DIB bits from the bitmap and fill in the
4846 * DIB color table if present */
4848 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
4849 hBmp, /* Handle to bitmap */
4850 0, /* First scan line to set in dest bitmap */
4851 bmp.bmHeight, /* Number of scan lines to copy */
4852 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
4853 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
4854 0); /* RGB or palette index */
4855 GlobalUnlock(hPackedDIB);
4857 /* Cleanup if GetDIBits failed */
4858 if (nLinesCopied != bmp.bmHeight)
4860 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
4861 GlobalFree(hPackedDIB);
4862 hPackedDIB = 0;
4864 return hPackedDIB;
4868 /**************************************************************************
4869 * X11DRV_DIB_CreateDIBFromPixmap
4871 * Allocates a packed DIB and copies the Pixmap data into it.
4872 * The Pixmap passed in is deleted after the conversion.
4874 HGLOBAL X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap, HDC hdc)
4876 HDC hdcMem;
4877 X_PHYSBITMAP *physBitmap;
4878 Pixmap orig_pixmap;
4879 HBITMAP hBmp = 0;
4880 HGLOBAL hPackedDIB = 0;
4881 Window root;
4882 int x,y; /* Unused */
4883 unsigned border_width; /* Unused */
4884 unsigned int depth, width, height;
4886 /* Get the Pixmap dimensions and bit depth */
4887 wine_tsx11_lock();
4888 if (!XGetGeometry(gdi_display, pixmap, &root, &x, &y, &width, &height,
4889 &border_width, &depth)) depth = 0;
4890 wine_tsx11_unlock();
4891 if (!depth) return 0;
4893 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
4894 width, height, depth);
4897 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
4898 * and make it a container for the pixmap passed.
4900 if (!(hBmp = CreateBitmap( width, height, 1, depth_to_bpp(depth), NULL ))) return 0;
4902 /* force bitmap to be owned by a screen DC */
4903 hdcMem = CreateCompatibleDC( hdc );
4904 SelectObject( hdcMem, SelectObject( hdcMem, hBmp ));
4905 DeleteDC( hdcMem );
4907 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4909 /* swap the new pixmap in */
4910 orig_pixmap = physBitmap->pixmap;
4911 physBitmap->pixmap = pixmap;
4914 * Create a packed DIB from the Pixmap wrapper bitmap created above.
4915 * A packed DIB contains a BITMAPINFO structure followed immediately by
4916 * an optional color palette and the pixel data.
4918 hPackedDIB = X11DRV_DIB_CreateDIBFromBitmap(hdc, hBmp);
4920 /* we can now get rid of the HBITMAP and its original pixmap */
4921 physBitmap->pixmap = orig_pixmap;
4922 DeleteObject(hBmp);
4924 TRACE("\tReturning packed DIB %p\n", hPackedDIB);
4925 return hPackedDIB;
4929 /**************************************************************************
4930 * X11DRV_DIB_CreatePixmapFromDIB
4932 * Creates a Pixmap from a packed DIB
4934 Pixmap X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB, HDC hdc )
4936 Pixmap pixmap;
4937 X_PHYSBITMAP *physBitmap;
4938 HBITMAP hBmp;
4939 LPBITMAPINFO pbmi;
4941 /* Create a DDB from the DIB */
4943 pbmi = GlobalLock(hPackedDIB);
4944 hBmp = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT,
4945 (LPBYTE)pbmi + bitmap_info_size( pbmi, DIB_RGB_COLORS ),
4946 pbmi, DIB_RGB_COLORS);
4947 GlobalUnlock(hPackedDIB);
4949 /* clear the physBitmap so that we can steal its pixmap */
4950 physBitmap = X11DRV_get_phys_bitmap( hBmp );
4951 pixmap = physBitmap->pixmap;
4952 physBitmap->pixmap = 0;
4954 /* Delete the DDB we created earlier now that we have stolen its pixmap */
4955 DeleteObject(hBmp);
4957 TRACE("Returning Pixmap %lx\n", pixmap);
4958 return pixmap;