2 * X11DRV device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
26 #include <X11/extensions/XShm.h>
27 # ifdef HAVE_SYS_SHM_H
30 # ifdef HAVE_SYS_IPC_H
33 #endif /* defined(HAVE_LIBXXSHM) */
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(bitmap
);
47 static struct list dibs_list
= LIST_INIT(dibs_list
);
49 static CRITICAL_SECTION dibs_cs
;
50 static CRITICAL_SECTION_DEBUG dibs_cs_debug
=
53 { &dibs_cs_debug
.ProcessLocksList
, &dibs_cs_debug
.ProcessLocksList
},
54 0, 0, { (DWORD_PTR
)(__FILE__
": dibs_cs") }
56 static CRITICAL_SECTION dibs_cs
= { &dibs_cs_debug
, -1, 0, 0, 0, 0 };
58 static PVOID dibs_handler
;
60 static int ximageDepthTable
[32];
62 /* This structure holds the arguments for DIB_SetImageBits() */
65 X11DRV_PDEVICE
*physDev
;
68 PALETTEENTRY
*palentry
;
90 } X11DRV_DIB_IMAGEBITS_DESCR
;
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
110 /***********************************************************************
111 * X11DRV_DIB_GetXImageWidthBytes
113 * Return the width of an X image in bytes
115 static inline int X11DRV_DIB_GetXImageWidthBytes( int width
, int depth
)
117 if (!depth
|| depth
> 32) goto error
;
119 if (!ximageDepthTable
[depth
-1])
121 XImage
*testimage
= XCreateImage( gdi_display
, visual
, depth
,
122 ZPixmap
, 0, NULL
, 1, 1, 32, 20 );
125 ximageDepthTable
[depth
-1] = testimage
->bits_per_pixel
;
126 XDestroyImage( testimage
);
128 else ximageDepthTable
[depth
-1] = -1;
130 if (ximageDepthTable
[depth
-1] != -1)
131 return (4 * ((width
* ximageDepthTable
[depth
-1] + 31) / 32));
134 WARN( "(%d): Unsupported depth\n", depth
);
139 /***********************************************************************
140 * X11DRV_DIB_GetDIBWidthBytes
142 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
144 static int X11DRV_DIB_GetDIBWidthBytes( int width
, int depth
)
150 case 1: words
= (width
+ 31) / 32; break;
151 case 4: words
= (width
+ 7) / 8; break;
152 case 8: words
= (width
+ 3) / 4; break;
154 case 16: words
= (width
+ 1) / 2; break;
155 case 24: words
= (width
* 3 + 3) / 4; break;
157 WARN("(%d): Unsupported depth\n", depth
);
166 /***********************************************************************
167 * X11DRV_DIB_GetDIBImageBytes
169 * Return the number of bytes used to hold the image in a DIB bitmap.
171 static int X11DRV_DIB_GetDIBImageBytes( int width
, int height
, int depth
)
173 return X11DRV_DIB_GetDIBWidthBytes( width
, depth
) * abs( height
);
177 /***********************************************************************
178 * X11DRV_DIB_BitmapInfoSize
180 * Return the size of the bitmap info structure including color table.
182 int X11DRV_DIB_BitmapInfoSize( const BITMAPINFO
* info
, WORD coloruse
)
186 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
188 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
189 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
190 return sizeof(BITMAPCOREHEADER
) + colors
*
191 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
193 else /* assume BITMAPINFOHEADER */
195 colors
= info
->bmiHeader
.biClrUsed
;
196 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
197 colors
= 1 << info
->bmiHeader
.biBitCount
;
198 return sizeof(BITMAPINFOHEADER
) + colors
*
199 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
204 /***********************************************************************
205 * X11DRV_DIB_CreateXImage
209 XImage
*X11DRV_DIB_CreateXImage( int width
, int height
, int depth
)
215 width_bytes
= X11DRV_DIB_GetXImageWidthBytes( width
, depth
);
216 image
= XCreateImage( gdi_display
, visual
, depth
, ZPixmap
, 0,
217 calloc( height
, width_bytes
),
218 width
, height
, 32, width_bytes
);
224 /***********************************************************************
225 * DIB_GetBitmapInfoEx
227 * Get the info from a bitmap header.
228 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
230 static int DIB_GetBitmapInfoEx( const BITMAPINFOHEADER
*header
, LONG
*width
,
231 LONG
*height
, WORD
*planes
, WORD
*bpp
,
232 WORD
*compr
, DWORD
*size
)
234 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
236 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
237 *width
= core
->bcWidth
;
238 *height
= core
->bcHeight
;
239 *planes
= core
->bcPlanes
;
240 *bpp
= core
->bcBitCount
;
245 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
))
247 *width
= header
->biWidth
;
248 *height
= header
->biHeight
;
249 *planes
= header
->biPlanes
;
250 *bpp
= header
->biBitCount
;
251 *compr
= header
->biCompression
;
252 *size
= header
->biSizeImage
;
255 ERR("(%d): unknown/wrong size for header\n", header
->biSize
);
260 /***********************************************************************
261 * X11DRV_DIB_GetColorCount
263 * Computes the number of colors for the bitmap palette.
264 * Should not be called for a >8-bit deep bitmap.
266 static unsigned int X11DRV_DIB_GetColorCount(const BITMAPINFO
*info
)
269 BOOL core_info
= info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
);
273 colors
= 1 << ((const BITMAPCOREINFO
*)info
)->bmciHeader
.bcBitCount
;
277 colors
= info
->bmiHeader
.biClrUsed
;
278 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
282 ERR("called with >256 colors!\n");
288 /***********************************************************************
291 * Get the info from a bitmap header.
292 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
294 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
295 LONG
*height
, WORD
*bpp
, WORD
*compr
)
300 return DIB_GetBitmapInfoEx( header
, width
, height
, &planes
, bpp
, compr
, &size
);
304 static inline BOOL
colour_is_brighter(RGBQUAD c1
, RGBQUAD c2
)
306 return (c1
.rgbRed
* c1
.rgbRed
+ c1
.rgbGreen
* c1
.rgbGreen
+ c1
.rgbBlue
* c1
.rgbBlue
) >
307 (c2
.rgbRed
* c2
.rgbRed
+ c2
.rgbGreen
* c2
.rgbGreen
+ c2
.rgbBlue
* c2
.rgbBlue
);
310 /***********************************************************************
311 * X11DRV_DIB_GenColorMap
313 * Fills the color map of a bitmap palette. Should not be called
314 * for a >8-bit deep bitmap.
316 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE
*physDev
, int *colorMapping
,
317 WORD coloruse
, WORD depth
, BOOL quads
,
318 const void *colorPtr
, int start
, int end
)
322 if (coloruse
== DIB_RGB_COLORS
)
326 const RGBQUAD
* rgb
= (const RGBQUAD
*)colorPtr
;
328 if (depth
== 1) /* Monochrome */
333 if (GetDIBColorTable( physDev
->hdc
, 0, 2, table
) == 2)
334 invert
= !colour_is_brighter(table
[1], table
[0]);
336 for (i
= start
; i
< end
; i
++, rgb
++)
337 colorMapping
[i
] = ((rgb
->rgbRed
+ rgb
->rgbGreen
+
338 rgb
->rgbBlue
> 255*3/2 && !invert
) ||
339 (rgb
->rgbRed
+ rgb
->rgbGreen
+
340 rgb
->rgbBlue
<= 255*3/2 && invert
));
343 for (i
= start
; i
< end
; i
++, rgb
++)
344 colorMapping
[i
] = X11DRV_PALETTE_ToPhysical( NULL
, RGB(rgb
->rgbRed
,
350 const RGBTRIPLE
* rgb
= (const RGBTRIPLE
*)colorPtr
;
352 if (depth
== 1) /* Monochrome */
357 if (GetDIBColorTable( physDev
->hdc
, 0, 2, table
) == 2)
358 invert
= !colour_is_brighter(table
[1], table
[0]);
360 for (i
= start
; i
< end
; i
++, rgb
++)
361 colorMapping
[i
] = ((rgb
->rgbtRed
+ rgb
->rgbtGreen
+
362 rgb
->rgbtBlue
> 255*3/2 && !invert
) ||
363 (rgb
->rgbtRed
+ rgb
->rgbtGreen
+
364 rgb
->rgbtBlue
<= 255*3/2 && invert
));
367 for (i
= start
; i
< end
; i
++, rgb
++)
368 colorMapping
[i
] = X11DRV_PALETTE_ToPhysical( NULL
, RGB(rgb
->rgbtRed
,
373 else /* DIB_PAL_COLORS */
375 const WORD
* index
= (const WORD
*)colorPtr
;
377 for (i
= start
; i
< end
; i
++, index
++)
378 colorMapping
[i
] = X11DRV_PALETTE_ToPhysical( physDev
, PALETTEINDEX(*index
) );
384 /***********************************************************************
385 * X11DRV_DIB_BuildColorMap
387 * Build the color map from the bitmap palette. Should not be called
388 * for a >8-bit deep bitmap.
390 static int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE
*physDev
, WORD coloruse
, WORD depth
,
391 const BITMAPINFO
*info
, int *nColors
)
394 const void *colorPtr
;
398 *nColors
= X11DRV_DIB_GetColorCount(info
);
399 if (!*nColors
) return NULL
;
401 isInfo
= info
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
);
402 colorPtr
= (const BYTE
*)info
+ (WORD
)info
->bmiHeader
.biSize
;
403 if (!(colorMapping
= HeapAlloc(GetProcessHeap(), 0, *nColors
* sizeof(int) )))
406 return X11DRV_DIB_GenColorMap( physDev
, colorMapping
, coloruse
, depth
,
407 isInfo
, colorPtr
, 0, *nColors
);
410 /***********************************************************************
411 * X11DRV_DIB_MapColor
413 static int X11DRV_DIB_MapColor( int *physMap
, int nPhysMap
, int phys
, int oldcol
)
417 if ((oldcol
< nPhysMap
) && (physMap
[oldcol
] == phys
))
420 for (color
= 0; color
< nPhysMap
; color
++)
421 if (physMap
[color
] == phys
)
424 WARN("Strange color %08x\n", phys
);
429 /*********************************************************************
430 * X11DRV_DIB_GetNearestIndex
432 * Helper for X11DRV_DIB_GetDIBits.
433 * Returns the nearest colour table index for a given RGB.
434 * Nearest is defined by minimizing the sum of the squares.
436 static INT
X11DRV_DIB_GetNearestIndex(RGBQUAD
*colormap
, int numColors
, BYTE r
, BYTE g
, BYTE b
)
438 INT i
, best
= -1, diff
, bestdiff
= -1;
441 for(color
= colormap
, i
= 0; i
< numColors
; color
++, i
++) {
442 diff
= (r
- color
->rgbRed
) * (r
- color
->rgbRed
) +
443 (g
- color
->rgbGreen
) * (g
- color
->rgbGreen
) +
444 (b
- color
->rgbBlue
) * (b
- color
->rgbBlue
);
447 if(best
== -1 || diff
< bestdiff
) {
454 /*********************************************************************
455 * X11DRV_DIB_MaskToShift
457 * Helper for X11DRV_DIB_GetDIBits.
458 * Returns the by how many bits to shift a given color so that it is
459 * in the proper position.
461 INT
X11DRV_DIB_MaskToShift(DWORD mask
)
469 while ((mask
&1)==0) {
476 /***********************************************************************
477 * X11DRV_DIB_CheckMask
479 * Check RGB mask if it is either 0 or matches visual's mask.
481 static inline int X11DRV_DIB_CheckMask(int red_mask
, int green_mask
, int blue_mask
)
483 return ( red_mask
== 0 && green_mask
== 0 && blue_mask
== 0 ) ||
484 ( red_mask
== visual
->red_mask
&& green_mask
== visual
->green_mask
&&
485 blue_mask
== visual
->blue_mask
);
488 /***********************************************************************
489 * X11DRV_DIB_SetImageBits_1
491 * SetDIBits for a 1-bit deep DIB.
493 static void X11DRV_DIB_SetImageBits_1( int lines
, const BYTE
*srcbits
,
494 DWORD srcwidth
, DWORD dstwidth
, int left
,
495 int *colors
, XImage
*bmpImage
, DWORD linebytes
)
504 srcbits
= srcbits
+ linebytes
* (lines
- 1);
505 linebytes
= -linebytes
;
508 if ((extra
= (left
& 7)) != 0) {
512 srcbits
+= left
>> 3;
513 width
= min(srcwidth
, dstwidth
);
515 /* ==== pal 1 dib -> any bmp format ==== */
516 for (h
= lines
-1; h
>=0; h
--) {
518 for (i
= width
/8, x
= left
; i
> 0; i
--) {
520 XPutPixel( bmpImage
, x
++, h
, colors
[ srcval
>> 7] );
521 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 6) & 1] );
522 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 5) & 1] );
523 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 4) & 1] );
524 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 3) & 1] );
525 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 2) & 1] );
526 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 1) & 1] );
527 XPutPixel( bmpImage
, x
++, h
, colors
[ srcval
& 1] );
533 case 7: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
534 case 6: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
535 case 5: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
536 case 4: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
537 case 3: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
538 case 2: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
539 case 1: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]);
542 srcbits
+= linebytes
;
546 /***********************************************************************
547 * X11DRV_DIB_GetImageBits_1
549 * GetDIBits for a 1-bit deep DIB.
551 static void X11DRV_DIB_GetImageBits_1( int lines
, BYTE
*dstbits
,
552 DWORD dstwidth
, DWORD srcwidth
,
553 RGBQUAD
*colors
, PALETTEENTRY
*srccolors
,
554 XImage
*bmpImage
, DWORD linebytes
)
557 int h
, width
= min(dstwidth
, srcwidth
);
561 dstbits
= dstbits
+ linebytes
* (lines
- 1);
562 linebytes
= -linebytes
;
565 switch (bmpImage
->depth
)
569 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
571 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
574 for (h
=lines
-1; h
>=0; h
--) {
578 for (x
=0; x
<width
; x
++) {
580 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
581 dstval
|=(X11DRV_DIB_GetNearestIndex
585 srcval
.peBlue
) << (7 - (x
& 7)));
594 dstbits
+= linebytes
;
602 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
, bmpImage
->green_mask
, bmpImage
->blue_mask
)
604 /* ==== pal 8 bmp -> pal 1 dib ==== */
606 const BYTE
* srcpixel
;
609 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
611 for (h
=0; h
<lines
; h
++) {
616 for (x
=0; x
<width
; x
++) {
618 srcval
=srccolors
[*srcpixel
++];
619 dstval
|=(X11DRV_DIB_GetNearestIndex
623 srcval
.peBlue
) << (7-(x
&7)) );
632 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
633 dstbits
+= linebytes
;
644 const WORD
* srcpixel
;
647 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
649 if (bmpImage
->green_mask
==0x03e0) {
650 if (bmpImage
->red_mask
==0x7c00) {
651 /* ==== rgb 555 bmp -> pal 1 dib ==== */
652 for (h
=0; h
<lines
; h
++) {
657 for (x
=0; x
<width
; x
++) {
660 dstval
|=(X11DRV_DIB_GetNearestIndex
662 ((srcval
>> 7) & 0xf8) | /* r */
663 ((srcval
>> 12) & 0x07),
664 ((srcval
>> 2) & 0xf8) | /* g */
665 ((srcval
>> 7) & 0x07),
666 ((srcval
<< 3) & 0xf8) | /* b */
667 ((srcval
>> 2) & 0x07) ) << (7-(x
&7)) );
676 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
677 dstbits
+= linebytes
;
679 } else if (bmpImage
->blue_mask
==0x7c00) {
680 /* ==== bgr 555 bmp -> pal 1 dib ==== */
681 for (h
=0; h
<lines
; h
++) {
686 for (x
=0; x
<width
; x
++) {
689 dstval
|=(X11DRV_DIB_GetNearestIndex
691 ((srcval
<< 3) & 0xf8) | /* r */
692 ((srcval
>> 2) & 0x07),
693 ((srcval
>> 2) & 0xf8) | /* g */
694 ((srcval
>> 7) & 0x07),
695 ((srcval
>> 7) & 0xf8) | /* b */
696 ((srcval
>> 12) & 0x07) ) << (7-(x
&7)) );
705 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
706 dstbits
+= linebytes
;
711 } else if (bmpImage
->green_mask
==0x07e0) {
712 if (bmpImage
->red_mask
==0xf800) {
713 /* ==== rgb 565 bmp -> pal 1 dib ==== */
714 for (h
=0; h
<lines
; h
++) {
719 for (x
=0; x
<width
; x
++) {
722 dstval
|=(X11DRV_DIB_GetNearestIndex
724 ((srcval
>> 8) & 0xf8) | /* r */
725 ((srcval
>> 13) & 0x07),
726 ((srcval
>> 3) & 0xfc) | /* g */
727 ((srcval
>> 9) & 0x03),
728 ((srcval
<< 3) & 0xf8) | /* b */
729 ((srcval
>> 2) & 0x07) ) << (7-(x
&7)) );
738 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
739 dstbits
+= linebytes
;
741 } else if (bmpImage
->blue_mask
==0xf800) {
742 /* ==== bgr 565 bmp -> pal 1 dib ==== */
743 for (h
=0; h
<lines
; h
++) {
748 for (x
=0; x
<width
; x
++) {
751 dstval
|=(X11DRV_DIB_GetNearestIndex
753 ((srcval
<< 3) & 0xf8) | /* r */
754 ((srcval
>> 2) & 0x07),
755 ((srcval
>> 3) & 0xfc) | /* g */
756 ((srcval
>> 9) & 0x03),
757 ((srcval
>> 8) & 0xf8) | /* b */
758 ((srcval
>> 13) & 0x07) ) << (7-(x
&7)) );
767 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
768 dstbits
+= linebytes
;
787 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
788 bytes_per_pixel
=(bmpImage
->bits_per_pixel
==24?3:4);
790 if (bmpImage
->green_mask
!=0x00ff00 ||
791 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
793 } else if (bmpImage
->blue_mask
==0xff) {
794 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
795 for (h
=0; h
<lines
; h
++) {
800 for (x
=0; x
<width
; x
++) {
801 dstval
|=(X11DRV_DIB_GetNearestIndex
805 srcbyte
[0]) << (7-(x
&7)) );
806 srcbyte
+=bytes_per_pixel
;
815 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
816 dstbits
+= linebytes
;
819 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
820 for (h
=0; h
<lines
; h
++) {
825 for (x
=0; x
<width
; x
++) {
826 dstval
|=(X11DRV_DIB_GetNearestIndex
830 srcbyte
[2]) << (7-(x
&7)) );
831 srcbyte
+=bytes_per_pixel
;
840 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
841 dstbits
+= linebytes
;
852 unsigned long white
= (1 << bmpImage
->bits_per_pixel
) - 1;
854 /* ==== any bmp format -> pal 1 dib ==== */
855 if ((unsigned)colors
[0].rgbRed
+colors
[0].rgbGreen
+colors
[0].rgbBlue
>=
856 (unsigned)colors
[1].rgbRed
+colors
[1].rgbGreen
+colors
[1].rgbBlue
)
859 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB, "
860 "%s color mapping\n",
861 bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
862 bmpImage
->green_mask
, bmpImage
->blue_mask
,
863 neg
?"negative":"direct" );
865 for (h
=lines
-1; h
>=0; h
--) {
869 for (x
=0; x
<width
; x
++) {
870 dstval
|=((XGetPixel( bmpImage
, x
, h
) >= white
) ^ neg
) << (7 - (x
&7));
879 dstbits
+= linebytes
;
886 /***********************************************************************
887 * X11DRV_DIB_SetImageBits_4
889 * SetDIBits for a 4-bit deep DIB.
891 static void X11DRV_DIB_SetImageBits_4( int lines
, const BYTE
*srcbits
,
892 DWORD srcwidth
, DWORD dstwidth
, int left
,
893 int *colors
, XImage
*bmpImage
, DWORD linebytes
)
901 srcbits
= srcbits
+ linebytes
* (lines
- 1);
902 linebytes
= -linebytes
;
909 srcbits
+= left
>> 1;
910 width
= min(srcwidth
, dstwidth
);
912 /* ==== pal 4 dib -> any bmp format ==== */
913 for (h
= lines
-1; h
>= 0; h
--) {
915 for (i
= width
/2, x
= left
; i
> 0; i
--) {
916 BYTE srcval
=*srcbyte
++;
917 XPutPixel( bmpImage
, x
++, h
, colors
[srcval
>> 4] );
918 XPutPixel( bmpImage
, x
++, h
, colors
[srcval
& 0x0f] );
921 XPutPixel( bmpImage
, x
, h
, colors
[*srcbyte
>> 4] );
922 srcbits
+= linebytes
;
928 /***********************************************************************
929 * X11DRV_DIB_GetImageBits_4
931 * GetDIBits for a 4-bit deep DIB.
933 static void X11DRV_DIB_GetImageBits_4( int lines
, BYTE
*dstbits
,
934 DWORD srcwidth
, DWORD dstwidth
,
935 RGBQUAD
*colors
, PALETTEENTRY
*srccolors
,
936 XImage
*bmpImage
, DWORD linebytes
)
939 int h
, width
= min(srcwidth
, dstwidth
);
945 dstbits
= dstbits
+ ( linebytes
* (lines
-1) );
946 linebytes
= -linebytes
;
951 switch (bmpImage
->depth
) {
954 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
956 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
959 for (h
= lines
-1; h
>= 0; h
--) {
963 for (x
= 0; x
< width
; x
++) {
965 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
966 dstval
|=(X11DRV_DIB_GetNearestIndex
970 srcval
.peBlue
) << (4-((x
&1)<<2)));
979 dstbits
+= linebytes
;
987 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
989 /* ==== pal 8 bmp -> pal 4 dib ==== */
991 const BYTE
*srcpixel
;
994 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
995 for (h
=0; h
<lines
; h
++) {
1000 for (x
=0; x
<width
; x
++) {
1001 PALETTEENTRY srcval
;
1002 srcval
= srccolors
[*srcpixel
++];
1003 dstval
|=(X11DRV_DIB_GetNearestIndex
1007 srcval
.peBlue
) << (4*(1-(x
&1))) );
1016 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1017 dstbits
+= linebytes
;
1027 const void* srcbits
;
1028 const WORD
* srcpixel
;
1031 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1033 if (bmpImage
->green_mask
==0x03e0) {
1034 if (bmpImage
->red_mask
==0x7c00) {
1035 /* ==== rgb 555 bmp -> pal 4 dib ==== */
1036 for (h
=0; h
<lines
; h
++) {
1041 for (x
=0; x
<width
; x
++) {
1044 dstval
|=(X11DRV_DIB_GetNearestIndex
1046 ((srcval
>> 7) & 0xf8) | /* r */
1047 ((srcval
>> 12) & 0x07),
1048 ((srcval
>> 2) & 0xf8) | /* g */
1049 ((srcval
>> 7) & 0x07),
1050 ((srcval
<< 3) & 0xf8) | /* b */
1051 ((srcval
>> 2) & 0x07) ) << ((1-(x
&1))<<2) );
1060 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1061 dstbits
+= linebytes
;
1063 } else if (bmpImage
->blue_mask
==0x7c00) {
1064 /* ==== bgr 555 bmp -> pal 4 dib ==== */
1065 for (h
=0; h
<lines
; h
++) {
1070 for (x
=0; x
<width
; x
++) {
1073 dstval
|=(X11DRV_DIB_GetNearestIndex
1075 ((srcval
<< 3) & 0xf8) | /* r */
1076 ((srcval
>> 2) & 0x07),
1077 ((srcval
>> 2) & 0xf8) | /* g */
1078 ((srcval
>> 7) & 0x07),
1079 ((srcval
>> 7) & 0xf8) | /* b */
1080 ((srcval
>> 12) & 0x07) ) << ((1-(x
&1))<<2) );
1089 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1090 dstbits
+= linebytes
;
1095 } else if (bmpImage
->green_mask
==0x07e0) {
1096 if (bmpImage
->red_mask
==0xf800) {
1097 /* ==== rgb 565 bmp -> pal 4 dib ==== */
1098 for (h
=0; h
<lines
; h
++) {
1103 for (x
=0; x
<width
; x
++) {
1106 dstval
|=(X11DRV_DIB_GetNearestIndex
1108 ((srcval
>> 8) & 0xf8) | /* r */
1109 ((srcval
>> 13) & 0x07),
1110 ((srcval
>> 3) & 0xfc) | /* g */
1111 ((srcval
>> 9) & 0x03),
1112 ((srcval
<< 3) & 0xf8) | /* b */
1113 ((srcval
>> 2) & 0x07) ) << ((1-(x
&1))<<2) );
1122 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1123 dstbits
+= linebytes
;
1125 } else if (bmpImage
->blue_mask
==0xf800) {
1126 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1127 for (h
=0; h
<lines
; h
++) {
1132 for (x
=0; x
<width
; x
++) {
1135 dstval
|=(X11DRV_DIB_GetNearestIndex
1137 ((srcval
<< 3) & 0xf8) | /* r */
1138 ((srcval
>> 2) & 0x07),
1139 ((srcval
>> 3) & 0xfc) | /* g */
1140 ((srcval
>> 9) & 0x03),
1141 ((srcval
>> 8) & 0xf8) | /* b */
1142 ((srcval
>> 13) & 0x07) ) << ((1-(x
&1))<<2) );
1151 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1152 dstbits
+= linebytes
;
1164 if (bmpImage
->bits_per_pixel
==24) {
1165 const void* srcbits
;
1166 const BYTE
*srcbyte
;
1169 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1171 if (bmpImage
->green_mask
!=0x00ff00 ||
1172 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
1174 } else if (bmpImage
->blue_mask
==0xff) {
1175 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1176 for (h
=0; h
<lines
; h
++) {
1179 for (x
=0; x
<width
/2; x
++) {
1180 /* Do 2 pixels at a time */
1181 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1186 X11DRV_DIB_GetNearestIndex
1194 /* And then the odd pixel */
1195 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1201 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1202 dstbits
+= linebytes
;
1205 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1206 for (h
=0; h
<lines
; h
++) {
1209 for (x
=0; x
<width
/2; x
++) {
1210 /* Do 2 pixels at a time */
1211 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1216 X11DRV_DIB_GetNearestIndex
1224 /* And then the odd pixel */
1225 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1231 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1232 dstbits
+= linebytes
;
1241 const void* srcbits
;
1242 const BYTE
*srcbyte
;
1245 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1247 if (bmpImage
->green_mask
!=0x00ff00 ||
1248 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
1250 } else if (bmpImage
->blue_mask
==0xff) {
1251 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1252 for (h
=0; h
<lines
; h
++) {
1255 for (x
=0; x
<width
/2; x
++) {
1256 /* Do 2 pixels at a time */
1257 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1262 X11DRV_DIB_GetNearestIndex
1270 /* And then the odd pixel */
1271 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1277 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1278 dstbits
+= linebytes
;
1281 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1282 for (h
=0; h
<lines
; h
++) {
1285 for (x
=0; x
<width
/2; x
++) {
1286 /* Do 2 pixels at a time */
1287 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1292 X11DRV_DIB_GetNearestIndex
1300 /* And then the odd pixel */
1301 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1307 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1308 dstbits
+= linebytes
;
1319 /* ==== any bmp format -> pal 4 dib ==== */
1320 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1321 bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
1322 bmpImage
->green_mask
, bmpImage
->blue_mask
);
1323 for (h
=lines
-1; h
>=0; h
--) {
1325 for (x
=0; x
<(width
& ~1); x
+=2) {
1326 *dstbyte
++=(X11DRV_DIB_MapColor((int*)colors
, 16, XGetPixel(bmpImage
, x
, h
), 0) << 4) |
1327 X11DRV_DIB_MapColor((int*)colors
, 16, XGetPixel(bmpImage
, x
+1, h
), 0);
1330 *dstbyte
=(X11DRV_DIB_MapColor((int *)colors
, 16, XGetPixel(bmpImage
, x
, h
), 0) << 4);
1332 dstbits
+= linebytes
;
1339 /***********************************************************************
1340 * X11DRV_DIB_SetImageBits_RLE4
1342 * SetDIBits for a 4-bit deep compressed DIB.
1344 static void X11DRV_DIB_SetImageBits_RLE4( int lines
, const BYTE
*bits
,
1345 DWORD srcwidth
, DWORD dstwidth
,
1346 int left
, int *colors
,
1349 unsigned int x
= 0, width
= min(srcwidth
, dstwidth
);
1350 int y
= lines
- 1, c
, length
;
1351 const BYTE
*begin
= bits
;
1356 if (length
) { /* encoded */
1359 if (x
>= (left
+ width
)) break;
1360 if( x
>= left
) XPutPixel(bmpImage
, x
, y
, colors
[c
>> 4]);
1362 if (!length
--) break;
1363 if (x
>= (left
+ width
)) break;
1364 if( x
>= left
) XPutPixel(bmpImage
, x
, y
, colors
[c
& 0xf]);
1384 default: /* absolute */
1387 if (x
>= left
&& x
< (left
+ width
))
1388 XPutPixel(bmpImage
, x
, y
, colors
[c
>> 4]);
1390 if (!length
--) break;
1391 if (x
>= left
&& x
< (left
+ width
))
1392 XPutPixel(bmpImage
, x
, y
, colors
[c
& 0xf]);
1395 if ((bits
- begin
) & 1)
1404 /***********************************************************************
1405 * X11DRV_DIB_SetImageBits_8
1407 * SetDIBits for an 8-bit deep DIB.
1409 static void X11DRV_DIB_SetImageBits_8( int lines
, const BYTE
*srcbits
,
1410 DWORD srcwidth
, DWORD dstwidth
, int left
,
1411 const int *colors
, XImage
*bmpImage
,
1415 int h
, width
= min(srcwidth
, dstwidth
);
1416 const BYTE
* srcbyte
;
1422 srcbits
= srcbits
+ linebytes
* (lines
-1);
1423 linebytes
= -linebytes
;
1428 switch (bmpImage
->depth
) {
1431 /* Some X servers might have 32 bit/ 16bit deep pixel */
1432 if (lines
&& width
&& (bmpImage
->bits_per_pixel
== 16) &&
1433 (ImageByteOrder(gdi_display
)==LSBFirst
) )
1435 /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1436 dstbits
=(BYTE
*)bmpImage
->data
+left
*2+(lines
-1)*bmpImage
->bytes_per_line
;
1437 for (h
= lines
; h
--; ) {
1438 #if defined(__i386__) && defined(__GNUC__)
1439 int _cl1
,_cl2
; /* temp outputs for asm below */
1440 /* Borrowed from DirectDraw */
1441 __asm__
__volatile__(
1446 " movw (%%edx,%%eax,4),%%ax\n"
1448 " xor %%eax,%%eax\n"
1450 :"=S" (srcbyte
), "=D" (_cl1
), "=c" (_cl2
)
1455 :"eax", "cc", "memory"
1458 DWORD
* dstpixel
=(DWORD
*)dstbits
;
1459 for (x
=0; x
<width
/2; x
++) {
1460 /* Do 2 pixels at a time */
1461 *dstpixel
++=(colors
[srcbyte
[1]] << 16) | colors
[srcbyte
[0]];
1465 /* And then the odd pixel */
1466 *((WORD
*)dstpixel
)=colors
[srcbyte
[0]];
1469 srcbyte
= (srcbits
+= linebytes
);
1470 dstbits
-= bmpImage
->bytes_per_line
;
1477 if (lines
&& width
&& (bmpImage
->bits_per_pixel
== 32) &&
1478 (ImageByteOrder(gdi_display
)==LSBFirst
) )
1480 dstbits
=(BYTE
*)bmpImage
->data
+left
*4+(lines
-1)*bmpImage
->bytes_per_line
;
1481 /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1482 for (h
= lines
; h
--; ) {
1483 #if defined(__i386__) && defined(__GNUC__)
1484 int _cl1
,_cl2
; /* temp outputs for asm below */
1485 /* Borrowed from DirectDraw */
1486 __asm__
__volatile__(
1491 " movl (%%edx,%%eax,4),%%eax\n"
1493 " xor %%eax,%%eax\n"
1495 :"=S" (srcbyte
), "=D" (_cl1
), "=c" (_cl2
)
1500 :"eax", "cc", "memory"
1503 DWORD
* dstpixel
=(DWORD
*)dstbits
;
1504 for (x
=0; x
<width
; x
++) {
1505 *dstpixel
++=colors
[*srcbyte
++];
1508 srcbyte
= (srcbits
+= linebytes
);
1509 dstbits
-= bmpImage
->bytes_per_line
;
1515 break; /* use slow generic case below */
1518 /* ==== pal 8 dib -> any bmp format ==== */
1519 for (h
=lines
-1; h
>=0; h
--) {
1520 for (x
=left
; x
<width
+left
; x
++) {
1521 XPutPixel(bmpImage
, x
, h
, colors
[*srcbyte
++]);
1523 srcbyte
= (srcbits
+= linebytes
);
1527 /***********************************************************************
1528 * X11DRV_DIB_GetImageBits_8
1530 * GetDIBits for an 8-bit deep DIB.
1532 static void X11DRV_DIB_GetImageBits_8( int lines
, BYTE
*dstbits
,
1533 DWORD srcwidth
, DWORD dstwidth
,
1534 RGBQUAD
*colors
, PALETTEENTRY
*srccolors
,
1535 XImage
*bmpImage
, DWORD linebytes
)
1538 int h
, width
= min(srcwidth
, dstwidth
);
1544 dstbits
= dstbits
+ ( linebytes
* (lines
-1) );
1545 linebytes
= -linebytes
;
1550 * This condition is true when GetImageBits has been called by
1551 * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1552 * 256 colormaps, so we'll just use it for GetDIBits calls.
1553 * (In some cases, in an updateDIBSection, the returned colors are bad too)
1555 if (!srccolors
) goto updatesection
;
1557 switch (bmpImage
->depth
) {
1560 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
1563 /* ==== pal 1 bmp -> pal 8 dib ==== */
1564 /* ==== pal 4 bmp -> pal 8 dib ==== */
1565 for (h
=lines
-1; h
>=0; h
--) {
1567 for (x
=0; x
<width
; x
++) {
1568 PALETTEENTRY srcval
;
1569 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
1570 *dstbyte
++=X11DRV_DIB_GetNearestIndex(colors
, 256,
1575 dstbits
+= linebytes
;
1583 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
1585 /* ==== pal 8 bmp -> pal 8 dib ==== */
1586 const void* srcbits
;
1587 const BYTE
* srcpixel
;
1589 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1590 for (h
=0; h
<lines
; h
++) {
1593 for (x
= 0; x
< width
; x
++) {
1594 PALETTEENTRY srcval
;
1595 srcval
=srccolors
[*srcpixel
++];
1596 *dstbyte
++=X11DRV_DIB_GetNearestIndex(colors
, 256,
1601 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1602 dstbits
+= linebytes
;
1612 const void* srcbits
;
1613 const WORD
* srcpixel
;
1616 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1618 if (bmpImage
->green_mask
==0x03e0) {
1619 if (bmpImage
->red_mask
==0x7c00) {
1620 /* ==== rgb 555 bmp -> pal 8 dib ==== */
1621 for (h
=0; h
<lines
; h
++) {
1624 for (x
=0; x
<width
; x
++) {
1627 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1629 ((srcval
>> 7) & 0xf8) | /* r */
1630 ((srcval
>> 12) & 0x07),
1631 ((srcval
>> 2) & 0xf8) | /* g */
1632 ((srcval
>> 7) & 0x07),
1633 ((srcval
<< 3) & 0xf8) | /* b */
1634 ((srcval
>> 2) & 0x07) );
1636 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1637 dstbits
+= linebytes
;
1639 } else if (bmpImage
->blue_mask
==0x7c00) {
1640 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1641 for (h
=0; h
<lines
; h
++) {
1644 for (x
=0; x
<width
; x
++) {
1647 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1649 ((srcval
<< 3) & 0xf8) | /* r */
1650 ((srcval
>> 2) & 0x07),
1651 ((srcval
>> 2) & 0xf8) | /* g */
1652 ((srcval
>> 7) & 0x07),
1653 ((srcval
>> 7) & 0xf8) | /* b */
1654 ((srcval
>> 12) & 0x07) );
1656 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1657 dstbits
+= linebytes
;
1662 } else if (bmpImage
->green_mask
==0x07e0) {
1663 if (bmpImage
->red_mask
==0xf800) {
1664 /* ==== rgb 565 bmp -> pal 8 dib ==== */
1665 for (h
=0; h
<lines
; h
++) {
1668 for (x
=0; x
<width
; x
++) {
1671 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1673 ((srcval
>> 8) & 0xf8) | /* r */
1674 ((srcval
>> 13) & 0x07),
1675 ((srcval
>> 3) & 0xfc) | /* g */
1676 ((srcval
>> 9) & 0x03),
1677 ((srcval
<< 3) & 0xf8) | /* b */
1678 ((srcval
>> 2) & 0x07) );
1680 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1681 dstbits
+= linebytes
;
1683 } else if (bmpImage
->blue_mask
==0xf800) {
1684 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1685 for (h
=0; h
<lines
; h
++) {
1688 for (x
=0; x
<width
; x
++) {
1691 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1693 ((srcval
<< 3) & 0xf8) | /* r */
1694 ((srcval
>> 2) & 0x07),
1695 ((srcval
>> 3) & 0xfc) | /* g */
1696 ((srcval
>> 9) & 0x03),
1697 ((srcval
>> 8) & 0xf8) | /* b */
1698 ((srcval
>> 13) & 0x07) );
1700 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1701 dstbits
+= linebytes
;
1715 const void* srcbits
;
1716 const BYTE
*srcbyte
;
1718 int bytes_per_pixel
;
1720 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1721 bytes_per_pixel
=(bmpImage
->bits_per_pixel
==24?3:4);
1723 if (bmpImage
->green_mask
!=0x00ff00 ||
1724 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
1726 } else if (bmpImage
->blue_mask
==0xff) {
1727 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1728 for (h
=0; h
<lines
; h
++) {
1731 for (x
=0; x
<width
; x
++) {
1732 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1737 srcbyte
+=bytes_per_pixel
;
1739 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1740 dstbits
+= linebytes
;
1743 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1744 for (h
=0; h
<lines
; h
++) {
1747 for (x
=0; x
<width
; x
++) {
1748 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1753 srcbyte
+=bytes_per_pixel
;
1755 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1756 dstbits
+= linebytes
;
1764 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1765 bmpImage
->depth
, bmpImage
->red_mask
,
1766 bmpImage
->green_mask
, bmpImage
->blue_mask
);
1768 /* ==== any bmp format -> pal 8 dib ==== */
1769 for (h
=lines
-1; h
>=0; h
--) {
1771 for (x
=0; x
<width
; x
++) {
1772 *dstbyte
=X11DRV_DIB_MapColor
1774 XGetPixel(bmpImage
, x
, h
), *dstbyte
);
1777 dstbits
+= linebytes
;
1783 /***********************************************************************
1784 * X11DRV_DIB_SetImageBits_RLE8
1786 * SetDIBits for an 8-bit deep compressed DIB.
1788 * This function rewritten 941113 by James Youngman. WINE blew out when I
1789 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
1791 * This was because the algorithm assumed that all RLE8 bitmaps end with the
1792 * 'End of bitmap' escape code. This code is very much laxer in what it
1793 * allows to end the expansion. Possibly too lax. See the note by
1794 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
1795 * bitmap should end with RleEnd, but on the other hand, software exists
1796 * that produces ones that don't and Windows 3.1 doesn't complain a bit
1799 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
1800 * James A. Youngman <mbcstjy@afs.man.ac.uk>
1803 static void X11DRV_DIB_SetImageBits_RLE8( int lines
, const BYTE
*bits
,
1804 DWORD srcwidth
, DWORD dstwidth
,
1805 int left
, int *colors
,
1808 unsigned int x
; /* X-position on each line. Increases. */
1809 int y
; /* Line #. Starts at lines-1, decreases */
1810 const BYTE
*pIn
= bits
; /* Pointer to current position in bits */
1811 BYTE length
; /* The length pf a run */
1812 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
1815 * Note that the bitmap data is stored by Windows starting at the
1816 * bottom line of the bitmap and going upwards. Within each line,
1817 * the data is stored left-to-right. That's the reason why line
1818 * goes from lines-1 to 0. [JAY]
1828 * If the length byte is not zero (which is the escape value),
1829 * We have a run of length pixels all the same colour. The colour
1830 * index is stored next.
1832 * If the length byte is zero, we need to read the next byte to
1833 * know what to do. [JAY]
1838 * [Run-Length] Encoded mode
1840 int color
= colors
[*pIn
++];
1841 while (length
-- && x
< (left
+ dstwidth
)) {
1842 if( x
>= left
) XPutPixel(bmpImage
, x
, y
, color
);
1849 * Escape codes (may be an absolute sequence though)
1851 escape_code
= (*pIn
++);
1860 /* Not all RLE8 bitmaps end with this code. For
1861 * example, Paint Shop Pro produces some that don't.
1862 * That's (I think) what caused the previous
1863 * implementation to fail. [JAY]
1872 default: /* switch to absolute mode */
1873 length
= escape_code
;
1876 int color
= colors
[*pIn
++];
1877 if (x
>= (left
+ dstwidth
))
1882 if( x
>= left
) XPutPixel(bmpImage
, x
, y
, color
);
1886 * If you think for a moment you'll realise that the
1887 * only time we could ever possibly read an odd
1888 * number of bytes is when there is a 0x00 (escape),
1889 * a value >0x02 (absolute mode) and then an odd-
1890 * length run. Therefore this is the only place we
1891 * need to worry about it. Everywhere else the
1892 * bytes are always read in pairs. [JAY]
1894 if (escape_code
& 1) pIn
++; /* Throw away the pad byte. */
1896 } /* switch (escape_code) : Escape sequence */
1902 /***********************************************************************
1903 * X11DRV_DIB_SetImageBits_16
1905 * SetDIBits for a 16-bit deep DIB.
1907 static void X11DRV_DIB_SetImageBits_16( int lines
, const BYTE
*srcbits
,
1908 DWORD srcwidth
, DWORD dstwidth
, int left
,
1909 X11DRV_PDEVICE
*physDev
, DWORD rSrc
, DWORD gSrc
, DWORD bSrc
,
1910 XImage
*bmpImage
, DWORD linebytes
)
1913 int h
, width
= min(srcwidth
, dstwidth
);
1914 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_dst_byteswap
;
1919 srcbits
= srcbits
+ ( linebytes
* (lines
-1));
1920 linebytes
= -linebytes
;
1923 switch (bmpImage
->depth
)
1930 srcbits
=srcbits
+left
*2;
1931 dstbits
=bmpImage
->data
+left
*2+(lines
-1)*bmpImage
->bytes_per_line
;
1933 if (bmpImage
->green_mask
==0x03e0) {
1934 if (gSrc
==bmpImage
->green_mask
) {
1935 if (rSrc
==bmpImage
->red_mask
) {
1936 /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1937 /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1938 convs
->Convert_5x5_asis
1941 dstbits
,-bmpImage
->bytes_per_line
);
1942 } else if (rSrc
==bmpImage
->blue_mask
) {
1943 /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1944 /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1945 convs
->Convert_555_reverse
1948 dstbits
,-bmpImage
->bytes_per_line
);
1951 if (rSrc
==bmpImage
->red_mask
|| bSrc
==bmpImage
->blue_mask
) {
1952 /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1953 /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1954 convs
->Convert_565_to_555_asis
1957 dstbits
,-bmpImage
->bytes_per_line
);
1959 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1960 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1961 convs
->Convert_565_to_555_reverse
1964 dstbits
,-bmpImage
->bytes_per_line
);
1967 } else if (bmpImage
->green_mask
==0x07e0) {
1968 if (gSrc
==bmpImage
->green_mask
) {
1969 if (rSrc
==bmpImage
->red_mask
) {
1970 /* ==== rgb 565 dib -> rgb 565 bmp ==== */
1971 /* ==== bgr 565 dib -> bgr 565 bmp ==== */
1972 convs
->Convert_5x5_asis
1975 dstbits
,-bmpImage
->bytes_per_line
);
1977 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
1978 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
1979 convs
->Convert_565_reverse
1982 dstbits
,-bmpImage
->bytes_per_line
);
1985 if (rSrc
==bmpImage
->red_mask
|| bSrc
==bmpImage
->blue_mask
) {
1986 /* ==== rgb 555 dib -> rgb 565 bmp ==== */
1987 /* ==== bgr 555 dib -> bgr 565 bmp ==== */
1988 convs
->Convert_555_to_565_asis
1991 dstbits
,-bmpImage
->bytes_per_line
);
1993 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
1994 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
1995 convs
->Convert_555_to_565_reverse
1998 dstbits
,-bmpImage
->bytes_per_line
);
2008 if (bmpImage
->bits_per_pixel
==24) {
2011 srcbits
=srcbits
+left
*2;
2012 dstbits
=bmpImage
->data
+left
*3+(lines
-1)*bmpImage
->bytes_per_line
;
2014 if (bmpImage
->green_mask
!=0x00ff00 ||
2015 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2017 } else if ((rSrc
==0x1f && bmpImage
->red_mask
==0xff) ||
2018 (bSrc
==0x1f && bmpImage
->blue_mask
==0xff)) {
2020 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
2021 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
2022 convs
->Convert_555_to_888_asis
2025 dstbits
,-bmpImage
->bytes_per_line
);
2027 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
2028 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
2029 convs
->Convert_565_to_888_asis
2032 dstbits
,-bmpImage
->bytes_per_line
);
2036 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
2037 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
2038 convs
->Convert_555_to_888_reverse
2041 dstbits
,-bmpImage
->bytes_per_line
);
2043 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
2044 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
2045 convs
->Convert_565_to_888_reverse
2048 dstbits
,-bmpImage
->bytes_per_line
);
2059 srcbits
=srcbits
+left
*2;
2060 dstbits
=bmpImage
->data
+left
*4+(lines
-1)*bmpImage
->bytes_per_line
;
2062 if (bmpImage
->green_mask
!=0x00ff00 ||
2063 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2065 } else if ((rSrc
==0x1f && bmpImage
->red_mask
==0xff) ||
2066 (bSrc
==0x1f && bmpImage
->blue_mask
==0xff)) {
2068 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
2069 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
2070 convs
->Convert_555_to_0888_asis
2073 dstbits
,-bmpImage
->bytes_per_line
);
2075 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
2076 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
2077 convs
->Convert_565_to_0888_asis
2080 dstbits
,-bmpImage
->bytes_per_line
);
2084 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
2085 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
2086 convs
->Convert_555_to_0888_reverse
2089 dstbits
,-bmpImage
->bytes_per_line
);
2091 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
2092 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
2093 convs
->Convert_565_to_0888_reverse
2096 dstbits
,-bmpImage
->bytes_per_line
);
2104 WARN("from 16 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2105 rSrc
, gSrc
, bSrc
, bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
2106 bmpImage
->green_mask
, bmpImage
->blue_mask
);
2112 /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
2113 const WORD
* srcpixel
;
2114 int rShift1
,gShift1
,bShift1
;
2115 int rShift2
,gShift2
,bShift2
;
2118 /* Set color scaling values */
2119 rShift1
=16+X11DRV_DIB_MaskToShift(rSrc
)-3;
2120 gShift1
=16+X11DRV_DIB_MaskToShift(gSrc
)-3;
2121 bShift1
=16+X11DRV_DIB_MaskToShift(bSrc
)-3;
2126 /* Green has 5 bits, like the others */
2130 /* Green has 6 bits, not 5. Compensate. */
2139 /* We could split it into four separate cases to optimize
2140 * but it is probably not worth it.
2142 for (h
=lines
-1; h
>=0; h
--) {
2143 srcpixel
=(const WORD
*)srcbits
;
2144 for (x
=left
; x
<width
+left
; x
++) {
2146 BYTE red
,green
,blue
;
2147 srcval
=*srcpixel
++ << 16;
2148 red
= ((srcval
>> rShift1
) & 0xf8) |
2149 ((srcval
>> rShift2
) & 0x07);
2150 green
=((srcval
>> gShift1
) & gMask1
) |
2151 ((srcval
>> gShift2
) & gMask2
);
2152 blue
= ((srcval
>> bShift1
) & 0xf8) |
2153 ((srcval
>> bShift2
) & 0x07);
2154 XPutPixel(bmpImage
, x
, h
,
2155 X11DRV_PALETTE_ToPhysical
2156 (physDev
, RGB(red
,green
,blue
)));
2158 srcbits
+= linebytes
;
2166 /***********************************************************************
2167 * X11DRV_DIB_GetImageBits_16
2169 * GetDIBits for an 16-bit deep DIB.
2171 static void X11DRV_DIB_GetImageBits_16( int lines
, BYTE
*dstbits
,
2172 DWORD dstwidth
, DWORD srcwidth
,
2173 PALETTEENTRY
*srccolors
,
2174 DWORD rDst
, DWORD gDst
, DWORD bDst
,
2175 XImage
*bmpImage
, DWORD dibpitch
)
2178 int h
, width
= min(srcwidth
, dstwidth
);
2179 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_src_byteswap
;
2181 DWORD linebytes
= dibpitch
;
2186 dstbits
= dstbits
+ ( linebytes
* (lines
-1));
2187 linebytes
= -linebytes
;
2190 switch (bmpImage
->depth
)
2195 const char* srcbits
;
2197 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2199 if (bmpImage
->green_mask
==0x03e0) {
2200 if (gDst
==bmpImage
->green_mask
) {
2201 if (rDst
==bmpImage
->red_mask
) {
2202 /* ==== rgb 555 bmp -> rgb 555 dib ==== */
2203 /* ==== bgr 555 bmp -> bgr 555 dib ==== */
2204 convs
->Convert_5x5_asis
2206 srcbits
,-bmpImage
->bytes_per_line
,
2209 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
2210 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
2211 convs
->Convert_555_reverse
2213 srcbits
,-bmpImage
->bytes_per_line
,
2217 if (rDst
==bmpImage
->red_mask
|| bDst
==bmpImage
->blue_mask
) {
2218 /* ==== rgb 555 bmp -> rgb 565 dib ==== */
2219 /* ==== bgr 555 bmp -> bgr 565 dib ==== */
2220 convs
->Convert_555_to_565_asis
2222 srcbits
,-bmpImage
->bytes_per_line
,
2225 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
2226 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
2227 convs
->Convert_555_to_565_reverse
2229 srcbits
,-bmpImage
->bytes_per_line
,
2233 } else if (bmpImage
->green_mask
==0x07e0) {
2234 if (gDst
==bmpImage
->green_mask
) {
2235 if (rDst
== bmpImage
->red_mask
) {
2236 /* ==== rgb 565 bmp -> rgb 565 dib ==== */
2237 /* ==== bgr 565 bmp -> bgr 565 dib ==== */
2238 convs
->Convert_5x5_asis
2240 srcbits
,-bmpImage
->bytes_per_line
,
2243 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
2244 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
2245 convs
->Convert_565_reverse
2247 srcbits
,-bmpImage
->bytes_per_line
,
2251 if (rDst
==bmpImage
->red_mask
|| bDst
==bmpImage
->blue_mask
) {
2252 /* ==== rgb 565 bmp -> rgb 555 dib ==== */
2253 /* ==== bgr 565 bmp -> bgr 555 dib ==== */
2254 convs
->Convert_565_to_555_asis
2256 srcbits
,-bmpImage
->bytes_per_line
,
2259 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
2260 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
2261 convs
->Convert_565_to_555_reverse
2263 srcbits
,-bmpImage
->bytes_per_line
,
2274 if (bmpImage
->bits_per_pixel
== 24) {
2275 const char* srcbits
;
2277 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2279 if (bmpImage
->green_mask
!=0x00ff00 ||
2280 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2282 } else if ((rDst
==0x1f && bmpImage
->red_mask
==0xff) ||
2283 (bDst
==0x1f && bmpImage
->blue_mask
==0xff)) {
2285 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
2286 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
2287 convs
->Convert_888_to_555_asis
2289 srcbits
,-bmpImage
->bytes_per_line
,
2292 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2293 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2294 convs
->Convert_888_to_565_asis
2296 srcbits
,-bmpImage
->bytes_per_line
,
2301 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
2302 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
2303 convs
->Convert_888_to_555_reverse
2305 srcbits
,-bmpImage
->bytes_per_line
,
2308 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
2309 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
2310 convs
->Convert_888_to_565_reverse
2312 srcbits
,-bmpImage
->bytes_per_line
,
2322 const char* srcbits
;
2324 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2326 if (bmpImage
->green_mask
!=0x00ff00 ||
2327 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2329 } else if ((rDst
==0x1f && bmpImage
->red_mask
==0xff) ||
2330 (bDst
==0x1f && bmpImage
->blue_mask
==0xff)) {
2332 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2333 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2334 convs
->Convert_0888_to_555_asis
2336 srcbits
,-bmpImage
->bytes_per_line
,
2339 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2340 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2341 convs
->Convert_0888_to_565_asis
2343 srcbits
,-bmpImage
->bytes_per_line
,
2348 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2349 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2350 convs
->Convert_0888_to_555_reverse
2352 srcbits
,-bmpImage
->bytes_per_line
,
2355 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2356 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2357 convs
->Convert_0888_to_565_reverse
2359 srcbits
,-bmpImage
->bytes_per_line
,
2368 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
2370 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2371 int rShift
,gShift
,bShift
;
2374 /* Shift everything 16 bits left so that all shifts are >0,
2375 * even for BGR DIBs. Then a single >> 16 will bring everything
2378 rShift
=16+X11DRV_DIB_MaskToShift(rDst
)-3;
2379 gShift
=16+X11DRV_DIB_MaskToShift(gDst
)-3;
2380 bShift
=16+X11DRV_DIB_MaskToShift(bDst
)-3;
2382 /* 6 bits for the green */
2388 for (h
= lines
- 1; h
>= 0; h
--) {
2389 dstpixel
=(LPWORD
)dstbits
;
2390 for (x
= 0; x
< width
; x
++) {
2391 PALETTEENTRY srcval
;
2393 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
2394 dstval
=((srcval
.peRed
<< rShift
) & rDst
) |
2395 ((srcval
.peGreen
<< gShift
) & gDst
) |
2396 ((srcval
.peBlue
<< bShift
) & bDst
);
2397 *dstpixel
++=dstval
>> 16;
2399 dstbits
+= linebytes
;
2407 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
2409 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2410 int rShift
,gShift
,bShift
;
2411 const BYTE
* srcbits
;
2412 const BYTE
* srcpixel
;
2415 /* Shift everything 16 bits left so that all shifts are >0,
2416 * even for BGR DIBs. Then a single >> 16 will bring everything
2419 rShift
=16+X11DRV_DIB_MaskToShift(rDst
)-3;
2420 gShift
=16+X11DRV_DIB_MaskToShift(gDst
)-3;
2421 bShift
=16+X11DRV_DIB_MaskToShift(bDst
)-3;
2423 /* 6 bits for the green */
2429 srcbits
=(BYTE
*)bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2430 for (h
=0; h
<lines
; h
++) {
2432 dstpixel
=(LPWORD
)dstbits
;
2433 for (x
= 0; x
< width
; x
++) {
2434 PALETTEENTRY srcval
;
2436 srcval
=srccolors
[*srcpixel
++];
2437 dstval
=((srcval
.peRed
<< rShift
) & rDst
) |
2438 ((srcval
.peGreen
<< gShift
) & gDst
) |
2439 ((srcval
.peBlue
<< bShift
) & bDst
);
2440 *dstpixel
++=dstval
>> 16;
2442 srcbits
-= bmpImage
->bytes_per_line
;
2443 dstbits
+= linebytes
;
2453 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2454 int rShift
,gShift
,bShift
;
2457 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%x,%x,%x)\n",
2458 bmpImage
->depth
, bmpImage
->red_mask
,
2459 bmpImage
->green_mask
, bmpImage
->blue_mask
,
2462 /* Shift everything 16 bits left so that all shifts are >0,
2463 * even for BGR DIBs. Then a single >> 16 will bring everything
2466 rShift
=16+X11DRV_DIB_MaskToShift(rDst
)-3;
2467 gShift
=16+X11DRV_DIB_MaskToShift(gDst
)-3;
2468 bShift
=16+X11DRV_DIB_MaskToShift(bDst
)-3;
2470 /* 6 bits for the green */
2476 for (h
= lines
- 1; h
>= 0; h
--) {
2477 dstpixel
=(LPWORD
)dstbits
;
2478 for (x
= 0; x
< width
; x
++) {
2481 srcval
=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage
, x
, h
));
2482 dstval
=((GetRValue(srcval
) << rShift
) & rDst
) |
2483 ((GetGValue(srcval
) << gShift
) & gDst
) |
2484 ((GetBValue(srcval
) << bShift
) & bDst
);
2485 *dstpixel
++=dstval
>> 16;
2487 dstbits
+= linebytes
;
2495 /***********************************************************************
2496 * X11DRV_DIB_SetImageBits_24
2498 * SetDIBits for a 24-bit deep DIB.
2500 static void X11DRV_DIB_SetImageBits_24( int lines
, const BYTE
*srcbits
,
2501 DWORD srcwidth
, DWORD dstwidth
, int left
,
2502 X11DRV_PDEVICE
*physDev
,
2503 DWORD rSrc
, DWORD gSrc
, DWORD bSrc
,
2504 XImage
*bmpImage
, DWORD linebytes
)
2507 int h
, width
= min(srcwidth
, dstwidth
);
2508 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_dst_byteswap
;
2513 srcbits
= srcbits
+ linebytes
* (lines
- 1);
2514 linebytes
= -linebytes
;
2517 switch (bmpImage
->depth
)
2520 if (bmpImage
->bits_per_pixel
==24) {
2523 srcbits
=srcbits
+left
*3;
2524 dstbits
=bmpImage
->data
+left
*3+(lines
-1)*bmpImage
->bytes_per_line
;
2526 if (bmpImage
->green_mask
!=0x00ff00 ||
2527 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2529 } else if (rSrc
==bmpImage
->red_mask
) {
2530 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2531 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2532 convs
->Convert_888_asis
2535 dstbits
,-bmpImage
->bytes_per_line
);
2537 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2538 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2539 convs
->Convert_888_reverse
2542 dstbits
,-bmpImage
->bytes_per_line
);
2552 srcbits
=srcbits
+left
*3;
2553 dstbits
=bmpImage
->data
+left
*4+(lines
-1)*bmpImage
->bytes_per_line
;
2555 if (bmpImage
->green_mask
!=0x00ff00 ||
2556 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2558 } else if (rSrc
==bmpImage
->red_mask
) {
2559 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2560 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2561 convs
->Convert_888_to_0888_asis
2564 dstbits
,-bmpImage
->bytes_per_line
);
2566 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2567 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2568 convs
->Convert_888_to_0888_reverse
2571 dstbits
,-bmpImage
->bytes_per_line
);
2581 srcbits
=srcbits
+left
*3;
2582 dstbits
=bmpImage
->data
+left
*2+(lines
-1)*bmpImage
->bytes_per_line
;
2584 if (bmpImage
->green_mask
==0x03e0) {
2585 if ((rSrc
==0xff0000 && bmpImage
->red_mask
==0x7f00) ||
2586 (bSrc
==0xff0000 && bmpImage
->blue_mask
==0x7f00)) {
2587 /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2588 /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2589 convs
->Convert_888_to_555_asis
2592 dstbits
,-bmpImage
->bytes_per_line
);
2593 } else if ((rSrc
==0xff && bmpImage
->red_mask
==0x7f00) ||
2594 (bSrc
==0xff && bmpImage
->blue_mask
==0x7f00)) {
2595 /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2596 /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2597 convs
->Convert_888_to_555_reverse
2600 dstbits
,-bmpImage
->bytes_per_line
);
2604 } else if (bmpImage
->green_mask
==0x07e0) {
2605 if ((rSrc
==0xff0000 && bmpImage
->red_mask
==0xf800) ||
2606 (bSrc
==0xff0000 && bmpImage
->blue_mask
==0xf800)) {
2607 /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2608 /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2609 convs
->Convert_888_to_565_asis
2612 dstbits
,-bmpImage
->bytes_per_line
);
2613 } else if ((rSrc
==0xff && bmpImage
->red_mask
==0xf800) ||
2614 (bSrc
==0xff && bmpImage
->blue_mask
==0xf800)) {
2615 /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2616 /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2617 convs
->Convert_888_to_565_reverse
2620 dstbits
,-bmpImage
->bytes_per_line
);
2632 WARN("from 24 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2633 rSrc
, gSrc
, bSrc
, bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
2634 bmpImage
->green_mask
, bmpImage
->blue_mask
);
2640 /* ==== rgb 888 dib -> any bmp format ==== */
2641 const BYTE
* srcbyte
;
2643 /* Windows only supports one 24bpp DIB format: RGB888 */
2645 for (h
= lines
- 1; h
>= 0; h
--) {
2647 for (x
= left
; x
< width
+left
; x
++) {
2648 XPutPixel(bmpImage
, x
, h
,
2649 X11DRV_PALETTE_ToPhysical
2650 (physDev
, RGB(srcbyte
[2], srcbyte
[1], srcbyte
[0])));
2653 srcbits
+= linebytes
;
2661 /***********************************************************************
2662 * X11DRV_DIB_GetImageBits_24
2664 * GetDIBits for an 24-bit deep DIB.
2666 static void X11DRV_DIB_GetImageBits_24( int lines
, BYTE
*dstbits
,
2667 DWORD dstwidth
, DWORD srcwidth
,
2668 PALETTEENTRY
*srccolors
,
2669 DWORD rDst
, DWORD gDst
, DWORD bDst
,
2670 XImage
*bmpImage
, DWORD linebytes
)
2673 int h
, width
= min(srcwidth
, dstwidth
);
2674 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_src_byteswap
;
2679 dstbits
= dstbits
+ ( linebytes
* (lines
-1) );
2680 linebytes
= -linebytes
;
2683 switch (bmpImage
->depth
)
2686 if (bmpImage
->bits_per_pixel
==24) {
2687 const char* srcbits
;
2689 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2691 if (bmpImage
->green_mask
!=0x00ff00 ||
2692 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2694 } else if (rDst
==bmpImage
->red_mask
) {
2695 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2696 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2697 convs
->Convert_888_asis
2699 srcbits
,-bmpImage
->bytes_per_line
,
2702 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2703 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2704 convs
->Convert_888_reverse
2706 srcbits
,-bmpImage
->bytes_per_line
,
2715 const char* srcbits
;
2717 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2719 if (bmpImage
->green_mask
!=0x00ff00 ||
2720 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2722 } else if (rDst
==bmpImage
->red_mask
) {
2723 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2724 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2725 convs
->Convert_0888_to_888_asis
2727 srcbits
,-bmpImage
->bytes_per_line
,
2730 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2731 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2732 convs
->Convert_0888_to_888_reverse
2734 srcbits
,-bmpImage
->bytes_per_line
,
2743 const char* srcbits
;
2745 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2747 if (bmpImage
->green_mask
==0x03e0) {
2748 if ((rDst
==0xff0000 && bmpImage
->red_mask
==0x7f00) ||
2749 (bDst
==0xff0000 && bmpImage
->blue_mask
==0x7f00)) {
2750 /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2751 /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2752 convs
->Convert_555_to_888_asis
2754 srcbits
,-bmpImage
->bytes_per_line
,
2756 } else if ((rDst
==0xff && bmpImage
->red_mask
==0x7f00) ||
2757 (bDst
==0xff && bmpImage
->blue_mask
==0x7f00)) {
2758 /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2759 /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2760 convs
->Convert_555_to_888_reverse
2762 srcbits
,-bmpImage
->bytes_per_line
,
2767 } else if (bmpImage
->green_mask
==0x07e0) {
2768 if ((rDst
==0xff0000 && bmpImage
->red_mask
==0xf800) ||
2769 (bDst
==0xff0000 && bmpImage
->blue_mask
==0xf800)) {
2770 /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2771 /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2772 convs
->Convert_565_to_888_asis
2774 srcbits
,-bmpImage
->bytes_per_line
,
2776 } else if ((rDst
==0xff && bmpImage
->red_mask
==0xf800) ||
2777 (bDst
==0xff && bmpImage
->blue_mask
==0xf800)) {
2778 /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2779 /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2780 convs
->Convert_565_to_888_reverse
2782 srcbits
,-bmpImage
->bytes_per_line
,
2795 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
2797 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2800 /* Windows only supports one 24bpp DIB format: rgb 888 */
2801 for (h
= lines
- 1; h
>= 0; h
--) {
2803 for (x
= 0; x
< width
; x
++) {
2804 PALETTEENTRY srcval
;
2805 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
2806 dstbyte
[0]=srcval
.peBlue
;
2807 dstbyte
[1]=srcval
.peGreen
;
2808 dstbyte
[2]=srcval
.peRed
;
2811 dstbits
+= linebytes
;
2819 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
2821 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2822 const void* srcbits
;
2823 const BYTE
* srcpixel
;
2826 /* Windows only supports one 24bpp DIB format: rgb 888 */
2827 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2828 for (h
= lines
- 1; h
>= 0; h
--) {
2831 for (x
= 0; x
< width
; x
++ ) {
2832 PALETTEENTRY srcval
;
2833 srcval
=srccolors
[*srcpixel
++];
2834 dstbyte
[0]=srcval
.peBlue
;
2835 dstbyte
[1]=srcval
.peGreen
;
2836 dstbyte
[2]=srcval
.peRed
;
2839 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
2840 dstbits
+= linebytes
;
2850 /* ==== any bmp format -> 888 dib ==== */
2853 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%x,%x,%x)\n",
2854 bmpImage
->depth
, bmpImage
->red_mask
,
2855 bmpImage
->green_mask
, bmpImage
->blue_mask
,
2858 /* Windows only supports one 24bpp DIB format: rgb 888 */
2859 for (h
= lines
- 1; h
>= 0; h
--) {
2861 for (x
= 0; x
< width
; x
++) {
2862 COLORREF srcval
=X11DRV_PALETTE_ToLogical
2863 (XGetPixel( bmpImage
, x
, h
));
2864 dstbyte
[0]=GetBValue(srcval
);
2865 dstbyte
[1]=GetGValue(srcval
);
2866 dstbyte
[2]=GetRValue(srcval
);
2869 dstbits
+= linebytes
;
2877 /***********************************************************************
2878 * X11DRV_DIB_SetImageBits_32
2880 * SetDIBits for a 32-bit deep DIB.
2882 static void X11DRV_DIB_SetImageBits_32(int lines
, const BYTE
*srcbits
,
2883 DWORD srcwidth
, DWORD dstwidth
, int left
,
2884 X11DRV_PDEVICE
*physDev
,
2885 DWORD rSrc
, DWORD gSrc
, DWORD bSrc
,
2891 int h
, width
= min(srcwidth
, dstwidth
);
2892 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_dst_byteswap
;
2897 srcbits
= srcbits
+ ( linebytes
* (lines
-1) );
2898 linebytes
= -linebytes
;
2901 ptr
= (const DWORD
*) srcbits
+ left
;
2903 switch (bmpImage
->depth
)
2906 if (bmpImage
->bits_per_pixel
==24) {
2909 srcbits
=srcbits
+left
*4;
2910 dstbits
=bmpImage
->data
+left
*3+(lines
-1)*bmpImage
->bytes_per_line
;
2912 if (rSrc
==bmpImage
->red_mask
&& gSrc
==bmpImage
->green_mask
&& bSrc
==bmpImage
->blue_mask
) {
2913 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2914 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2915 convs
->Convert_0888_to_888_asis
2918 dstbits
,-bmpImage
->bytes_per_line
);
2919 } else if (bmpImage
->green_mask
!=0x00ff00 ||
2920 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2922 /* the tests below assume sane bmpImage masks */
2923 } else if (rSrc
==bmpImage
->blue_mask
&& gSrc
==bmpImage
->green_mask
&& bSrc
==bmpImage
->red_mask
) {
2924 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2925 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2926 convs
->Convert_0888_to_888_reverse
2929 dstbits
,-bmpImage
->bytes_per_line
);
2930 } else if (bmpImage
->blue_mask
==0xff) {
2931 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2932 convs
->Convert_any0888_to_rgb888
2936 dstbits
,-bmpImage
->bytes_per_line
);
2938 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2939 convs
->Convert_any0888_to_bgr888
2943 dstbits
,-bmpImage
->bytes_per_line
);
2953 srcbits
=srcbits
+left
*4;
2954 dstbits
=bmpImage
->data
+left
*4+(lines
-1)*bmpImage
->bytes_per_line
;
2956 if (gSrc
==bmpImage
->green_mask
) {
2957 if (rSrc
==bmpImage
->red_mask
&& bSrc
==bmpImage
->blue_mask
) {
2958 /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2959 /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2960 convs
->Convert_0888_asis
2963 dstbits
,-bmpImage
->bytes_per_line
);
2964 } else if (bmpImage
->green_mask
!=0x00ff00 ||
2965 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2967 /* the tests below assume sane bmpImage masks */
2968 } else if (rSrc
==bmpImage
->blue_mask
&& bSrc
==bmpImage
->red_mask
) {
2969 /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2970 /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2971 convs
->Convert_0888_reverse
2974 dstbits
,-bmpImage
->bytes_per_line
);
2976 /* ==== any 0888 dib -> any 0888 bmp ==== */
2977 convs
->Convert_0888_any
2981 dstbits
,-bmpImage
->bytes_per_line
,
2982 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
2984 } else if (bmpImage
->green_mask
!=0x00ff00 ||
2985 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2987 /* the tests below assume sane bmpImage masks */
2989 /* ==== any 0888 dib -> any 0888 bmp ==== */
2990 convs
->Convert_0888_any
2994 dstbits
,-bmpImage
->bytes_per_line
,
2995 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3005 srcbits
=srcbits
+left
*4;
3006 dstbits
=bmpImage
->data
+left
*2+(lines
-1)*bmpImage
->bytes_per_line
;
3008 if (rSrc
==0xff0000 && gSrc
==0x00ff00 && bSrc
==0x0000ff) {
3009 if (bmpImage
->green_mask
==0x03e0) {
3010 if (bmpImage
->red_mask
==0x7f00) {
3011 /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
3012 convs
->Convert_0888_to_555_asis
3015 dstbits
,-bmpImage
->bytes_per_line
);
3016 } else if (bmpImage
->blue_mask
==0x7f00) {
3017 /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
3018 convs
->Convert_0888_to_555_reverse
3021 dstbits
,-bmpImage
->bytes_per_line
);
3025 } else if (bmpImage
->green_mask
==0x07e0) {
3026 if (bmpImage
->red_mask
==0xf800) {
3027 /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
3028 convs
->Convert_0888_to_565_asis
3031 dstbits
,-bmpImage
->bytes_per_line
);
3032 } else if (bmpImage
->blue_mask
==0xf800) {
3033 /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
3034 convs
->Convert_0888_to_565_reverse
3037 dstbits
,-bmpImage
->bytes_per_line
);
3044 } else if (rSrc
==0x0000ff && gSrc
==0x00ff00 && bSrc
==0xff0000) {
3045 if (bmpImage
->green_mask
==0x03e0) {
3046 if (bmpImage
->blue_mask
==0x7f00) {
3047 /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
3048 convs
->Convert_0888_to_555_asis
3051 dstbits
,-bmpImage
->bytes_per_line
);
3052 } else if (bmpImage
->red_mask
==0x7f00) {
3053 /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
3054 convs
->Convert_0888_to_555_reverse
3057 dstbits
,-bmpImage
->bytes_per_line
);
3061 } else if (bmpImage
->green_mask
==0x07e0) {
3062 if (bmpImage
->blue_mask
==0xf800) {
3063 /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
3064 convs
->Convert_0888_to_565_asis
3067 dstbits
,-bmpImage
->bytes_per_line
);
3068 } else if (bmpImage
->red_mask
==0xf800) {
3069 /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
3070 convs
->Convert_0888_to_565_reverse
3073 dstbits
,-bmpImage
->bytes_per_line
);
3081 if (bmpImage
->green_mask
==0x03e0 &&
3082 (bmpImage
->red_mask
==0x7f00 ||
3083 bmpImage
->blue_mask
==0x7f00)) {
3084 /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
3085 convs
->Convert_any0888_to_5x5
3089 dstbits
,-bmpImage
->bytes_per_line
,
3090 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3091 } else if (bmpImage
->green_mask
==0x07e0 &&
3092 (bmpImage
->red_mask
==0xf800 ||
3093 bmpImage
->blue_mask
==0xf800)) {
3094 /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
3095 convs
->Convert_any0888_to_5x5
3099 dstbits
,-bmpImage
->bytes_per_line
,
3100 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3110 WARN("from 32 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
3111 rSrc
, gSrc
, bSrc
, bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
3112 bmpImage
->green_mask
, bmpImage
->blue_mask
);
3118 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
3119 const DWORD
* srcpixel
;
3120 int rShift
,gShift
,bShift
;
3122 rShift
=X11DRV_DIB_MaskToShift(rSrc
);
3123 gShift
=X11DRV_DIB_MaskToShift(gSrc
);
3124 bShift
=X11DRV_DIB_MaskToShift(bSrc
);
3126 for (h
= lines
- 1; h
>= 0; h
--) {
3127 srcpixel
=(const DWORD
*)srcbits
;
3128 for (x
= left
; x
< width
+left
; x
++) {
3130 BYTE red
,green
,blue
;
3131 srcvalue
=*srcpixel
++;
3132 red
= (srcvalue
>> rShift
) & 0xff;
3133 green
=(srcvalue
>> gShift
) & 0xff;
3134 blue
= (srcvalue
>> bShift
) & 0xff;
3135 XPutPixel(bmpImage
, x
, h
, X11DRV_PALETTE_ToPhysical
3136 (physDev
, RGB(red
,green
,blue
)));
3138 srcbits
+= linebytes
;
3146 /***********************************************************************
3147 * X11DRV_DIB_GetImageBits_32
3149 * GetDIBits for an 32-bit deep DIB.
3151 static void X11DRV_DIB_GetImageBits_32( int lines
, BYTE
*dstbits
,
3152 DWORD dstwidth
, DWORD srcwidth
,
3153 PALETTEENTRY
*srccolors
,
3154 DWORD rDst
, DWORD gDst
, DWORD bDst
,
3155 XImage
*bmpImage
, DWORD linebytes
)
3158 int h
, width
= min(srcwidth
, dstwidth
);
3160 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_src_byteswap
;
3165 dstbits
= dstbits
+ ( linebytes
* (lines
-1) );
3166 linebytes
= -linebytes
;
3171 switch (bmpImage
->depth
)
3174 if (bmpImage
->bits_per_pixel
==24) {
3175 const void* srcbits
;
3177 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
3179 if (rDst
==bmpImage
->red_mask
&& gDst
==bmpImage
->green_mask
&& bDst
==bmpImage
->blue_mask
) {
3180 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
3181 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
3182 convs
->Convert_888_to_0888_asis
3184 srcbits
,-bmpImage
->bytes_per_line
,
3186 } else if (bmpImage
->green_mask
!=0x00ff00 ||
3187 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
3189 /* the tests below assume sane bmpImage masks */
3190 } else if (rDst
==bmpImage
->blue_mask
&& gDst
==bmpImage
->green_mask
&& bDst
==bmpImage
->red_mask
) {
3191 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
3192 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
3193 convs
->Convert_888_to_0888_reverse
3195 srcbits
,-bmpImage
->bytes_per_line
,
3197 } else if (bmpImage
->blue_mask
==0xff) {
3198 /* ==== rgb 888 bmp -> any 0888 dib ==== */
3199 convs
->Convert_rgb888_to_any0888
3201 srcbits
,-bmpImage
->bytes_per_line
,
3205 /* ==== bgr 888 bmp -> any 0888 dib ==== */
3206 convs
->Convert_bgr888_to_any0888
3208 srcbits
,-bmpImage
->bytes_per_line
,
3218 const char* srcbits
;
3220 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
3222 if (gDst
==bmpImage
->green_mask
) {
3223 if (rDst
==bmpImage
->red_mask
&& bDst
==bmpImage
->blue_mask
) {
3224 /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
3225 /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
3226 convs
->Convert_0888_asis
3228 srcbits
,-bmpImage
->bytes_per_line
,
3230 } else if (bmpImage
->green_mask
!=0x00ff00 ||
3231 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
3233 /* the tests below assume sane bmpImage masks */
3234 } else if (rDst
==bmpImage
->blue_mask
&& bDst
==bmpImage
->red_mask
) {
3235 /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
3236 /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
3237 convs
->Convert_0888_reverse
3239 srcbits
,-bmpImage
->bytes_per_line
,
3242 /* ==== any 0888 bmp -> any 0888 dib ==== */
3243 convs
->Convert_0888_any
3245 srcbits
,-bmpImage
->bytes_per_line
,
3246 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
,
3250 } else if (bmpImage
->green_mask
!=0x00ff00 ||
3251 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
3253 /* the tests below assume sane bmpImage masks */
3255 /* ==== any 0888 bmp -> any 0888 dib ==== */
3256 convs
->Convert_0888_any
3258 srcbits
,-bmpImage
->bytes_per_line
,
3259 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
,
3269 const char* srcbits
;
3271 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
3273 if (rDst
==0xff0000 && gDst
==0x00ff00 && bDst
==0x0000ff) {
3274 if (bmpImage
->green_mask
==0x03e0) {
3275 if (bmpImage
->red_mask
==0x7f00) {
3276 /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
3277 convs
->Convert_555_to_0888_asis
3279 srcbits
,-bmpImage
->bytes_per_line
,
3281 } else if (bmpImage
->blue_mask
==0x7f00) {
3282 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
3283 convs
->Convert_555_to_0888_reverse
3285 srcbits
,-bmpImage
->bytes_per_line
,
3290 } else if (bmpImage
->green_mask
==0x07e0) {
3291 if (bmpImage
->red_mask
==0xf800) {
3292 /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
3293 convs
->Convert_565_to_0888_asis
3295 srcbits
,-bmpImage
->bytes_per_line
,
3297 } else if (bmpImage
->blue_mask
==0xf800) {
3298 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
3299 convs
->Convert_565_to_0888_reverse
3301 srcbits
,-bmpImage
->bytes_per_line
,
3309 } else if (rDst
==0x0000ff && gDst
==0x00ff00 && bDst
==0xff0000) {
3310 if (bmpImage
->green_mask
==0x03e0) {
3311 if (bmpImage
->blue_mask
==0x7f00) {
3312 /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
3313 convs
->Convert_555_to_0888_asis
3315 srcbits
,-bmpImage
->bytes_per_line
,
3317 } else if (bmpImage
->red_mask
==0x7f00) {
3318 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
3319 convs
->Convert_555_to_0888_reverse
3321 srcbits
,-bmpImage
->bytes_per_line
,
3326 } else if (bmpImage
->green_mask
==0x07e0) {
3327 if (bmpImage
->blue_mask
==0xf800) {
3328 /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3329 convs
->Convert_565_to_0888_asis
3331 srcbits
,-bmpImage
->bytes_per_line
,
3333 } else if (bmpImage
->red_mask
==0xf800) {
3334 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3335 convs
->Convert_565_to_0888_reverse
3337 srcbits
,-bmpImage
->bytes_per_line
,
3346 if (bmpImage
->green_mask
==0x03e0 &&
3347 (bmpImage
->red_mask
==0x7f00 ||
3348 bmpImage
->blue_mask
==0x7f00)) {
3349 /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3350 convs
->Convert_5x5_to_any0888
3352 srcbits
,-bmpImage
->bytes_per_line
,
3353 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
,
3356 } else if (bmpImage
->green_mask
==0x07e0 &&
3357 (bmpImage
->red_mask
==0xf800 ||
3358 bmpImage
->blue_mask
==0xf800)) {
3359 /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3360 convs
->Convert_5x5_to_any0888
3362 srcbits
,-bmpImage
->bytes_per_line
,
3363 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
,
3375 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
3377 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3378 int rShift
,gShift
,bShift
;
3381 rShift
=X11DRV_DIB_MaskToShift(rDst
);
3382 gShift
=X11DRV_DIB_MaskToShift(gDst
);
3383 bShift
=X11DRV_DIB_MaskToShift(bDst
);
3384 for (h
= lines
- 1; h
>= 0; h
--) {
3385 dstpixel
=(DWORD
*)dstbits
;
3386 for (x
= 0; x
< width
; x
++) {
3387 PALETTEENTRY srcval
;
3388 srcval
= srccolors
[XGetPixel(bmpImage
, x
, h
)];
3389 *dstpixel
++=(srcval
.peRed
<< rShift
) |
3390 (srcval
.peGreen
<< gShift
) |
3391 (srcval
.peBlue
<< bShift
);
3393 dstbits
+= linebytes
;
3401 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
3403 /* ==== pal 8 bmp -> any 0888 dib ==== */
3404 int rShift
,gShift
,bShift
;
3405 const void* srcbits
;
3406 const BYTE
* srcpixel
;
3409 rShift
=X11DRV_DIB_MaskToShift(rDst
);
3410 gShift
=X11DRV_DIB_MaskToShift(gDst
);
3411 bShift
=X11DRV_DIB_MaskToShift(bDst
);
3412 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
3413 for (h
= lines
- 1; h
>= 0; h
--) {
3415 dstpixel
=(DWORD
*)dstbits
;
3416 for (x
= 0; x
< width
; x
++) {
3417 PALETTEENTRY srcval
;
3418 srcval
=srccolors
[*srcpixel
++];
3419 *dstpixel
++=(srcval
.peRed
<< rShift
) |
3420 (srcval
.peGreen
<< gShift
) |
3421 (srcval
.peBlue
<< bShift
);
3423 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
3424 dstbits
+= linebytes
;
3434 /* ==== any bmp format -> any 0888 dib ==== */
3435 int rShift
,gShift
,bShift
;
3438 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%x,%x,%x)\n",
3439 bmpImage
->depth
, bmpImage
->red_mask
,
3440 bmpImage
->green_mask
, bmpImage
->blue_mask
,
3443 rShift
=X11DRV_DIB_MaskToShift(rDst
);
3444 gShift
=X11DRV_DIB_MaskToShift(gDst
);
3445 bShift
=X11DRV_DIB_MaskToShift(bDst
);
3446 for (h
= lines
- 1; h
>= 0; h
--) {
3447 dstpixel
=(DWORD
*)dstbits
;
3448 for (x
= 0; x
< width
; x
++) {
3450 srcval
=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage
, x
, h
));
3451 *dstpixel
++=(GetRValue(srcval
) << rShift
) |
3452 (GetGValue(srcval
) << gShift
) |
3453 (GetBValue(srcval
) << bShift
);
3455 dstbits
+= linebytes
;
3462 static int XGetSubImageErrorHandler(Display
*dpy
, XErrorEvent
*event
, void *arg
)
3464 return (event
->request_code
== X_GetImage
&& event
->error_code
== BadMatch
);
3467 /***********************************************************************
3468 * X11DRV_DIB_SetImageBits_GetSubImage
3470 * Helper for X11DRV_DIB_SetImageBits
3472 static void X11DRV_DIB_SetImageBits_GetSubImage(
3473 const X11DRV_DIB_IMAGEBITS_DESCR
*descr
, XImage
*bmpImage
)
3475 /* compressed bitmaps may contain gaps in them. So make a copy
3476 * of the existing pixels first */
3479 SetRect( &bmprc
, descr
->xDest
, descr
->yDest
,
3480 descr
->xDest
+ descr
->width
, descr
->yDest
+ descr
->height
);
3481 GetRgnBox( descr
->physDev
->region
, &rc
);
3482 /* convert from dc to drawable origin */
3483 OffsetRect( &rc
, descr
->physDev
->dc_rect
.left
, descr
->physDev
->dc_rect
.top
);
3484 /* clip visible rect with bitmap */
3485 if( IntersectRect( &rc
, &rc
, &bmprc
))
3487 X11DRV_expect_error( gdi_display
, XGetSubImageErrorHandler
, NULL
);
3488 XGetSubImage( gdi_display
, descr
->drawable
, rc
.left
, rc
.top
,
3489 rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, AllPlanes
,
3491 descr
->xSrc
+ rc
.left
- bmprc
.left
,
3492 descr
->ySrc
+ rc
.top
- bmprc
.top
);
3493 X11DRV_check_error();
3497 /***********************************************************************
3498 * X11DRV_DIB_SetImageBits
3500 * Transfer the bits to an X image.
3501 * Helper function for SetDIBits() and SetDIBitsToDevice().
3503 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR
*descr
)
3505 int lines
= descr
->lines
>= 0 ? descr
->lines
: -descr
->lines
;
3510 bmpImage
= descr
->image
;
3512 bmpImage
= XCreateImage( gdi_display
, visual
, descr
->depth
, ZPixmap
, 0, NULL
,
3513 descr
->infoWidth
, lines
, 32, 0 );
3514 bmpImage
->data
= calloc( lines
, bmpImage
->bytes_per_line
);
3515 if(bmpImage
->data
== NULL
) {
3516 ERR("Out of memory!\n");
3517 XDestroyImage( bmpImage
);
3518 wine_tsx11_unlock();
3522 wine_tsx11_unlock();
3524 TRACE("Dib: depth=%d r=%x g=%x b=%x\n",
3525 descr
->infoBpp
,descr
->rMask
,descr
->gMask
,descr
->bMask
);
3526 TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3527 bmpImage
->depth
,bmpImage
->bits_per_pixel
,
3528 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3530 /* Transfer the pixels */
3531 switch(descr
->infoBpp
)
3534 X11DRV_DIB_SetImageBits_1( descr
->lines
, descr
->bits
, descr
->infoWidth
,
3535 descr
->width
, descr
->xSrc
, (int *)(descr
->colorMap
),
3536 bmpImage
, descr
->dibpitch
);
3539 if (descr
->compression
) {
3540 X11DRV_DIB_SetImageBits_GetSubImage( descr
, bmpImage
);
3541 X11DRV_DIB_SetImageBits_RLE4( descr
->lines
, descr
->bits
,
3542 descr
->infoWidth
, descr
->width
,
3543 descr
->xSrc
, (int *)(descr
->colorMap
),
3546 X11DRV_DIB_SetImageBits_4( descr
->lines
, descr
->bits
,
3547 descr
->infoWidth
, descr
->width
,
3548 descr
->xSrc
, (int*)(descr
->colorMap
),
3549 bmpImage
, descr
->dibpitch
);
3552 if (descr
->compression
) {
3553 X11DRV_DIB_SetImageBits_GetSubImage( descr
, bmpImage
);
3554 X11DRV_DIB_SetImageBits_RLE8( descr
->lines
, descr
->bits
,
3555 descr
->infoWidth
, descr
->width
,
3556 descr
->xSrc
, (int *)(descr
->colorMap
),
3559 X11DRV_DIB_SetImageBits_8( descr
->lines
, descr
->bits
,
3560 descr
->infoWidth
, descr
->width
,
3561 descr
->xSrc
, (int *)(descr
->colorMap
),
3562 bmpImage
, descr
->dibpitch
);
3566 X11DRV_DIB_SetImageBits_16( descr
->lines
, descr
->bits
,
3567 descr
->infoWidth
, descr
->width
,
3568 descr
->xSrc
, descr
->physDev
,
3569 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3570 bmpImage
, descr
->dibpitch
);
3573 X11DRV_DIB_SetImageBits_24( descr
->lines
, descr
->bits
,
3574 descr
->infoWidth
, descr
->width
,
3575 descr
->xSrc
, descr
->physDev
,
3576 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3577 bmpImage
, descr
->dibpitch
);
3580 X11DRV_DIB_SetImageBits_32( 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
);
3587 WARN("(%d): Invalid depth\n", descr
->infoBpp
);
3591 TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3592 descr
->drawable
, descr
->gc
, bmpImage
,
3593 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
3594 descr
->width
, descr
->height
);
3597 #ifdef HAVE_LIBXXSHM
3598 if (descr
->image
&& descr
->useShm
)
3600 XShmPutImage( gdi_display
, descr
->drawable
, descr
->gc
, bmpImage
,
3601 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
3602 descr
->width
, descr
->height
, FALSE
);
3603 XSync( gdi_display
, 0 );
3607 XPutImage( gdi_display
, descr
->drawable
, descr
->gc
, bmpImage
,
3608 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
3609 descr
->width
, descr
->height
);
3611 if (!descr
->image
) XDestroyImage( bmpImage
);
3612 wine_tsx11_unlock();
3616 /***********************************************************************
3617 * X11DRV_DIB_GetImageBits
3619 * Transfer the bits from an X image.
3621 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR
*descr
)
3623 int lines
= descr
->lines
>= 0 ? descr
->lines
: -descr
->lines
;
3628 bmpImage
= descr
->image
;
3630 bmpImage
= XCreateImage( gdi_display
, visual
, descr
->depth
, ZPixmap
, 0, NULL
,
3631 descr
->infoWidth
, lines
, 32, 0 );
3632 bmpImage
->data
= calloc( lines
, bmpImage
->bytes_per_line
);
3633 if(bmpImage
->data
== NULL
) {
3634 ERR("Out of memory!\n");
3635 XDestroyImage( bmpImage
);
3636 wine_tsx11_unlock();
3641 #ifdef HAVE_LIBXXSHM
3643 /* We must not call XShmGetImage() with a bitmap which is bigger than the available area.
3644 If we do, XShmGetImage() will fail (X exception), as it checks for this internally. */
3645 if((descr
->image
&& descr
->useShm
) && (bmpImage
->width
<= (descr
->width
- descr
->xSrc
))
3646 && (bmpImage
->height
<= (descr
->height
- descr
->ySrc
)))
3648 int saveRed
, saveGreen
, saveBlue
;
3650 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3651 gdi_display
, descr
->drawable
, bmpImage
,
3652 descr
->xSrc
, descr
->ySrc
, AllPlanes
);
3654 /* We must save and restore the bmpImage's masks in order
3655 * to preserve them across the call to XShmGetImage, which
3656 * decides to eliminate them since it doesn't happen to know
3657 * what the format of the image is supposed to be, even though
3659 saveRed
= bmpImage
->red_mask
;
3660 saveBlue
= bmpImage
->blue_mask
;
3661 saveGreen
= bmpImage
->green_mask
;
3663 XShmGetImage( gdi_display
, descr
->drawable
, bmpImage
,
3664 descr
->xSrc
, descr
->ySrc
, AllPlanes
);
3666 bmpImage
->red_mask
= saveRed
;
3667 bmpImage
->blue_mask
= saveBlue
;
3668 bmpImage
->green_mask
= saveGreen
;
3671 #endif /* HAVE_LIBXXSHM */
3673 TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3674 gdi_display
, descr
->drawable
, descr
->xSrc
, descr
->ySrc
, descr
->width
,
3675 lines
, AllPlanes
, ZPixmap
, bmpImage
, descr
->xDest
, descr
->yDest
);
3676 XGetSubImage( gdi_display
, descr
->drawable
, descr
->xSrc
, descr
->ySrc
,
3677 descr
->width
, lines
, AllPlanes
, ZPixmap
,
3678 bmpImage
, descr
->xDest
, descr
->yDest
);
3680 wine_tsx11_unlock();
3682 TRACE("Dib: depth=%2d r=%x g=%x b=%x\n",
3683 descr
->infoBpp
,descr
->rMask
,descr
->gMask
,descr
->bMask
);
3684 TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3685 bmpImage
->depth
,bmpImage
->bits_per_pixel
,
3686 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3687 /* Transfer the pixels */
3688 switch(descr
->infoBpp
)
3691 X11DRV_DIB_GetImageBits_1( descr
->lines
,(LPVOID
)descr
->bits
,
3692 descr
->infoWidth
, descr
->width
,
3693 descr
->colorMap
, descr
->palentry
,
3694 bmpImage
, descr
->dibpitch
);
3698 if (descr
->compression
) {
3699 FIXME("Compression not yet supported!\n");
3700 if(descr
->sizeImage
< X11DRV_DIB_GetDIBWidthBytes( descr
->infoWidth
, 4 ) * abs(descr
->lines
))
3703 X11DRV_DIB_GetImageBits_4( descr
->lines
,(LPVOID
)descr
->bits
,
3704 descr
->infoWidth
, descr
->width
,
3705 descr
->colorMap
, descr
->palentry
,
3706 bmpImage
, descr
->dibpitch
);
3709 if (descr
->compression
) {
3710 FIXME("Compression not yet supported!\n");
3711 if(descr
->sizeImage
< X11DRV_DIB_GetDIBWidthBytes( descr
->infoWidth
, 8 ) * abs(descr
->lines
))
3714 X11DRV_DIB_GetImageBits_8( descr
->lines
, (LPVOID
)descr
->bits
,
3715 descr
->infoWidth
, descr
->width
,
3716 descr
->colorMap
, descr
->palentry
,
3717 bmpImage
, descr
->dibpitch
);
3721 X11DRV_DIB_GetImageBits_16( descr
->lines
, (LPVOID
)descr
->bits
,
3722 descr
->infoWidth
,descr
->width
,
3724 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3725 bmpImage
, descr
->dibpitch
);
3729 X11DRV_DIB_GetImageBits_24( descr
->lines
, (LPVOID
)descr
->bits
,
3730 descr
->infoWidth
,descr
->width
,
3732 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3733 bmpImage
, descr
->dibpitch
);
3737 X11DRV_DIB_GetImageBits_32( descr
->lines
, (LPVOID
)descr
->bits
,
3738 descr
->infoWidth
, descr
->width
,
3740 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3741 bmpImage
, descr
->dibpitch
);
3745 WARN("(%d): Invalid depth\n", descr
->infoBpp
);
3752 XDestroyImage( bmpImage
);
3753 wine_tsx11_unlock();
3758 /*************************************************************************
3759 * X11DRV_SetDIBitsToDevice
3762 INT
X11DRV_SetDIBitsToDevice( X11DRV_PDEVICE
*physDev
, INT xDest
, INT yDest
, DWORD cx
,
3763 DWORD cy
, INT xSrc
, INT ySrc
,
3764 UINT startscan
, UINT lines
, LPCVOID bits
,
3765 const BITMAPINFO
*info
, UINT coloruse
)
3767 X11DRV_DIB_IMAGEBITS_DESCR descr
;
3772 int rop
= X11DRV_XROPfunction
[GetROP2(physDev
->hdc
) - 1];
3774 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
,
3775 &descr
.infoBpp
, &descr
.compression
) == -1)
3778 top_down
= (height
< 0);
3779 if (top_down
) height
= -height
;
3783 LPtoDP(physDev
->hdc
, &pt
, 1);
3785 if (!lines
|| (startscan
>= height
)) return 0;
3786 if (!top_down
&& startscan
+ lines
> height
) lines
= height
- startscan
;
3788 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3789 * and clamp all values to fit inside [startscan,startscan+lines]
3791 if (ySrc
+ cy
<= startscan
+ lines
)
3793 UINT y
= startscan
+ lines
- (ySrc
+ cy
);
3794 if (ySrc
< startscan
) cy
-= (startscan
- ySrc
);
3797 /* avoid getting unnecessary lines */
3799 if (y
>= lines
) return 0;
3804 if (y
>= lines
) return lines
;
3805 ySrc
= y
; /* need to get all lines in top down mode */
3810 if (ySrc
>= startscan
+ lines
) return lines
;
3811 pt
.y
+= ySrc
+ cy
- (startscan
+ lines
);
3812 cy
= startscan
+ lines
- ySrc
;
3814 if (cy
> lines
) cy
= lines
;
3816 if (xSrc
>= width
) return lines
;
3817 if (xSrc
+ cx
>= width
) cx
= width
- xSrc
;
3818 if (!cx
|| !cy
) return lines
;
3820 /* Update the pixmap from the DIB section */
3821 X11DRV_LockDIBSection(physDev
, DIB_Status_GdiMod
);
3823 X11DRV_SetupGCForText( physDev
); /* To have the correct colors */
3825 XSetFunction(gdi_display
, physDev
->gc
, rop
);
3826 wine_tsx11_unlock();
3828 switch (descr
.infoBpp
)
3833 descr
.colorMap
= (RGBQUAD
*)X11DRV_DIB_BuildColorMap(
3835 physDev
->depth
, info
, &descr
.nColorMap
);
3836 if (!descr
.colorMap
) return 0;
3837 descr
.rMask
= descr
.gMask
= descr
.bMask
= 0;
3841 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0x7c00;
3842 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x03e0;
3843 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x001f;
3849 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0xff0000;
3850 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x00ff00;
3851 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x0000ff;
3856 descr
.physDev
= physDev
;
3859 descr
.palentry
= NULL
;
3860 descr
.lines
= top_down
? -lines
: lines
;
3861 descr
.infoWidth
= width
;
3862 descr
.depth
= physDev
->depth
;
3863 descr
.drawable
= physDev
->drawable
;
3864 descr
.gc
= physDev
->gc
;
3867 descr
.xDest
= physDev
->dc_rect
.left
+ pt
.x
;
3868 descr
.yDest
= physDev
->dc_rect
.top
+ pt
.y
;
3871 descr
.useShm
= FALSE
;
3872 descr
.dibpitch
= ((width
* descr
.infoBpp
+ 31) &~31) / 8;
3874 result
= X11DRV_DIB_SetImageBits( &descr
);
3876 if (descr
.infoBpp
<= 8)
3877 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
3879 /* Update the DIBSection of the pixmap */
3880 X11DRV_UnlockDIBSection(physDev
, TRUE
);
3885 /***********************************************************************
3886 * SetDIBits (X11DRV.@)
3888 INT
X11DRV_SetDIBits( X11DRV_PDEVICE
*physDev
, HBITMAP hbitmap
, UINT startscan
,
3889 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
, UINT coloruse
)
3891 X_PHYSBITMAP
*physBitmap
= X11DRV_get_phys_bitmap( hbitmap
);
3892 X11DRV_DIB_IMAGEBITS_DESCR descr
;
3894 LONG width
, height
, tmpheight
;
3897 descr
.physDev
= physDev
;
3899 if (!physBitmap
) return 0;
3901 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
,
3902 &descr
.infoBpp
, &descr
.compression
) == -1)
3906 if (height
< 0) height
= -height
;
3907 if (!lines
|| (startscan
>= height
))
3910 if (!GetObjectW( hbitmap
, sizeof(bitmap
), &bitmap
)) return 0;
3912 if (startscan
+ lines
> height
) lines
= height
- startscan
;
3914 switch (descr
.infoBpp
)
3919 descr
.colorMap
= (RGBQUAD
*)X11DRV_DIB_BuildColorMap(
3920 descr
.physDev
, coloruse
,
3921 physBitmap
->pixmap_depth
,
3922 info
, &descr
.nColorMap
);
3923 if (!descr
.colorMap
) return 0;
3924 descr
.rMask
= descr
.gMask
= descr
.bMask
= 0;
3928 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0x7c00;
3929 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x03e0;
3930 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x001f;
3936 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0xff0000;
3937 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x00ff00;
3938 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x0000ff;
3947 descr
.palentry
= NULL
;
3948 descr
.infoWidth
= width
;
3949 descr
.lines
= tmpheight
>= 0 ? lines
: -lines
;
3950 descr
.depth
= physBitmap
->pixmap_depth
;
3951 descr
.drawable
= physBitmap
->pixmap
;
3952 descr
.gc
= BITMAP_GC(physBitmap
);
3956 descr
.yDest
= height
- startscan
- lines
;
3957 descr
.width
= bitmap
.bmWidth
;
3958 descr
.height
= lines
;
3959 descr
.useShm
= FALSE
;
3960 descr
.dibpitch
= ((descr
.infoWidth
* descr
.infoBpp
+ 31) &~31) / 8;
3961 X11DRV_DIB_Lock( physBitmap
, DIB_Status_GdiMod
);
3962 result
= X11DRV_DIB_SetImageBits( &descr
);
3963 X11DRV_DIB_Unlock( physBitmap
, TRUE
);
3965 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
3970 /***********************************************************************
3971 * GetDIBits (X11DRV.@)
3973 INT
X11DRV_GetDIBits( X11DRV_PDEVICE
*physDev
, HBITMAP hbitmap
, UINT startscan
, UINT lines
,
3974 LPVOID bits
, BITMAPINFO
*info
, UINT coloruse
)
3976 X_PHYSBITMAP
*physBitmap
= X11DRV_get_phys_bitmap( hbitmap
);
3978 X11DRV_DIB_IMAGEBITS_DESCR descr
;
3979 PALETTEENTRY palette
[256];
3982 LONG width
, tempHeight
;
3987 GetPaletteEntries( GetCurrentObject( physDev
->hdc
, OBJ_PAL
), 0, 256, palette
);
3989 if (!physBitmap
) return 0;
3990 if (!(obj_size
= GetObjectW( hbitmap
, sizeof(dib
), &dib
))) return 0;
3992 bitmap_type
= DIB_GetBitmapInfo( (BITMAPINFOHEADER
*)info
, &width
, &tempHeight
, &descr
.infoBpp
, &descr
.compression
);
3993 descr
.lines
= tempHeight
;
3994 if (bitmap_type
== -1)
3996 ERR("Invalid bitmap\n");
3999 core_header
= (bitmap_type
== 0);
4000 colorPtr
= (LPBYTE
) info
+ (WORD
) info
->bmiHeader
.biSize
;
4002 TRACE("%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
4003 lines
, dib
.dsBm
.bmWidth
, dib
.dsBm
.bmHeight
, width
, descr
.lines
, startscan
);
4005 if( lines
> dib
.dsBm
.bmHeight
) lines
= dib
.dsBm
.bmHeight
;
4007 height
= descr
.lines
;
4008 if (height
< 0) height
= -height
;
4009 if( lines
> height
) lines
= height
;
4010 /* Top-down images have a negative biHeight, the scanlines of these images
4011 * were inverted in X11DRV_DIB_GetImageBits_xx
4012 * To prevent this we simply change the sign of lines
4013 * (the number of scan lines to copy).
4014 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4016 if( descr
.lines
< 0 && lines
> 0) lines
= -lines
;
4018 if( startscan
>= dib
.dsBm
.bmHeight
) return 0;
4020 descr
.colorMap
= NULL
;
4022 switch (descr
.infoBpp
)
4027 descr
.rMask
= descr
.gMask
= descr
.bMask
= 0;
4028 if(coloruse
== DIB_RGB_COLORS
)
4029 descr
.colorMap
= colorPtr
;
4031 int num_colors
= 1 << descr
.infoBpp
, i
;
4034 WORD
*index
= (WORD
*)colorPtr
;
4035 descr
.colorMap
= rgb
= HeapAlloc(GetProcessHeap(), 0, num_colors
* sizeof(RGBQUAD
));
4036 for(i
= 0; i
< num_colors
; i
++, rgb
++, index
++) {
4037 colref
= X11DRV_PALETTE_ToLogical(X11DRV_PALETTE_ToPhysical(physDev
, PALETTEINDEX(*index
)));
4038 rgb
->rgbRed
= GetRValue(colref
);
4039 rgb
->rgbGreen
= GetGValue(colref
);
4040 rgb
->rgbBlue
= GetBValue(colref
);
4041 rgb
->rgbReserved
= 0;
4047 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0x7c00;
4048 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x03e0;
4049 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x001f;
4053 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0xff0000;
4054 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x00ff00;
4055 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x0000ff;
4059 descr
.physDev
= physDev
;
4060 descr
.palentry
= palette
;
4062 descr
.image
= physBitmap
->image
;
4063 descr
.infoWidth
= width
;
4064 descr
.lines
= lines
;
4065 descr
.depth
= physBitmap
->pixmap_depth
;
4066 descr
.drawable
= physBitmap
->pixmap
;
4067 descr
.gc
= BITMAP_GC(physBitmap
);
4068 descr
.width
= dib
.dsBm
.bmWidth
;
4069 descr
.height
= dib
.dsBm
.bmHeight
;
4073 descr
.sizeImage
= core_header
? 0 : info
->bmiHeader
.biSizeImage
;
4075 if (descr
.lines
> 0)
4077 descr
.ySrc
= (descr
.height
-1) - (startscan
+ (lines
-1));
4081 descr
.ySrc
= startscan
;
4083 #ifdef HAVE_LIBXXSHM
4084 descr
.useShm
= (obj_size
== sizeof(DIBSECTION
)) && (physBitmap
->shminfo
.shmid
!= -1);
4086 descr
.useShm
= FALSE
;
4088 descr
.dibpitch
= (obj_size
== sizeof(DIBSECTION
)) ? dib
.dsBm
.bmWidthBytes
4089 : (((descr
.infoWidth
* descr
.infoBpp
+ 31) &~31) / 8);
4091 X11DRV_DIB_Lock( physBitmap
, DIB_Status_GdiMod
);
4092 X11DRV_DIB_GetImageBits( &descr
);
4093 X11DRV_DIB_Unlock( physBitmap
, TRUE
);
4095 if(!core_header
&& info
->bmiHeader
.biSizeImage
== 0) /* Fill in biSizeImage */
4096 info
->bmiHeader
.biSizeImage
= X11DRV_DIB_GetDIBImageBytes( descr
.infoWidth
,
4100 if (descr
.compression
== BI_BITFIELDS
)
4102 *(DWORD
*)info
->bmiColors
= descr
.rMask
;
4103 *((DWORD
*)info
->bmiColors
+ 1) = descr
.gMask
;
4104 *((DWORD
*)info
->bmiColors
+ 2) = descr
.bMask
;
4106 else if (!core_header
)
4108 /* if RLE or JPEG compression were supported,
4109 * this line would be invalid. */
4110 info
->bmiHeader
.biCompression
= 0;
4113 if(descr
.colorMap
!= colorPtr
)
4114 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
4118 /***********************************************************************
4119 * DIB_DoProtectDIBSection
4121 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP
*physBitmap
, DWORD new_prot
)
4125 VirtualProtect(physBitmap
->base
, physBitmap
->size
, new_prot
, &old_prot
);
4126 TRACE("Changed protection from %d to %d\n", old_prot
, new_prot
);
4129 /***********************************************************************
4130 * X11DRV_DIB_DoCopyDIBSection
4132 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP
*physBitmap
, BOOL toDIB
,
4133 void *colorMap
, int nColorMap
,
4134 Drawable dest
, GC gc
,
4135 DWORD xSrc
, DWORD ySrc
,
4136 DWORD xDest
, DWORD yDest
,
4137 DWORD width
, DWORD height
)
4139 DIBSECTION dibSection
;
4140 X11DRV_DIB_IMAGEBITS_DESCR descr
;
4141 int identity
[2] = {0,1};
4143 if (!GetObjectW( physBitmap
->hbitmap
, sizeof(dibSection
), &dibSection
)) return;
4145 descr
.physDev
= NULL
;
4146 descr
.palentry
= NULL
;
4147 descr
.infoWidth
= dibSection
.dsBmih
.biWidth
;
4148 descr
.infoBpp
= dibSection
.dsBmih
.biBitCount
;
4149 descr
.lines
= dibSection
.dsBmih
.biHeight
;
4150 descr
.image
= physBitmap
->image
;
4151 descr
.colorMap
= colorMap
;
4152 descr
.nColorMap
= nColorMap
;
4153 descr
.bits
= dibSection
.dsBm
.bmBits
;
4154 descr
.depth
= physBitmap
->pixmap_depth
;
4155 descr
.compression
= dibSection
.dsBmih
.biCompression
;
4157 if(descr
.infoBpp
== 1)
4158 descr
.colorMap
= (void*)identity
;
4160 switch (descr
.infoBpp
)
4165 descr
.rMask
= descr
.gMask
= descr
.bMask
= 0;
4169 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[0] : 0x7c00;
4170 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[1] : 0x03e0;
4171 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[2] : 0x001f;
4176 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[0] : 0xff0000;
4177 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[1] : 0x00ff00;
4178 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[2] : 0x0000ff;
4183 descr
.drawable
= dest
;
4187 descr
.xDest
= xDest
;
4188 descr
.yDest
= yDest
;
4189 descr
.width
= width
;
4190 descr
.height
= height
;
4191 descr
.sizeImage
= 0;
4193 #ifdef HAVE_LIBXXSHM
4194 descr
.useShm
= (physBitmap
->shminfo
.shmid
!= -1);
4196 descr
.useShm
= FALSE
;
4198 descr
.dibpitch
= dibSection
.dsBm
.bmWidthBytes
;
4202 TRACE("Copying from Pixmap to DIB bits\n");
4203 X11DRV_DIB_GetImageBits( &descr
);
4207 TRACE("Copying from DIB bits to Pixmap\n");
4208 X11DRV_DIB_SetImageBits( &descr
);
4212 /***********************************************************************
4213 * X11DRV_DIB_CopyDIBSection
4215 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE
*physDevSrc
, X11DRV_PDEVICE
*physDevDst
,
4216 DWORD xSrc
, DWORD ySrc
, DWORD xDest
, DWORD yDest
,
4217 DWORD width
, DWORD height
)
4220 X_PHYSBITMAP
*physBitmap
;
4221 unsigned int nColorMap
;
4225 TRACE("(%p,%p,%d,%d,%d,%d,%d,%d)\n", physDevSrc
->hdc
, physDevDst
->hdc
,
4226 xSrc
, ySrc
, xDest
, yDest
, width
, height
);
4227 /* this function is meant as an optimization for BitBlt,
4228 * not to be called otherwise */
4229 physBitmap
= physDevSrc
->bitmap
;
4230 if (!physBitmap
|| GetObjectW( physBitmap
->hbitmap
, sizeof(dib
), &dib
) != sizeof(dib
))
4232 ERR("called for non-DIBSection!?\n");
4235 /* while BitBlt should already have made sure we only get
4236 * positive values, we should check for oversize values */
4237 if ((xSrc
< dib
.dsBm
.bmWidth
) &&
4238 (ySrc
< dib
.dsBm
.bmHeight
)) {
4239 if (xSrc
+ width
> dib
.dsBm
.bmWidth
)
4240 width
= dib
.dsBm
.bmWidth
- xSrc
;
4241 if (ySrc
+ height
> dib
.dsBm
.bmHeight
)
4242 height
= dib
.dsBm
.bmHeight
- ySrc
;
4243 /* if the source bitmap is 8bpp or less, we're supposed to use the
4244 * DC's palette for color conversion (not the DIB color table) */
4245 if (dib
.dsBm
.bmBitsPixel
<= 8) {
4246 HPALETTE hPalette
= GetCurrentObject( physDevSrc
->hdc
, OBJ_PAL
);
4247 if (!hPalette
|| (hPalette
== GetStockObject(DEFAULT_PALETTE
))) {
4248 /* HACK: no palette has been set in the source DC,
4249 * use the DIB colormap instead - this is necessary in some
4250 * cases since we need to do depth conversion in some places
4251 * where real Windows can just copy data straight over */
4252 x11ColorMap
= physBitmap
->colorMap
;
4253 nColorMap
= physBitmap
->nColorMap
;
4254 freeColorMap
= FALSE
;
4256 const BITMAPINFO
* info
= (BITMAPINFO
*)&dib
.dsBmih
;
4259 nColorMap
= X11DRV_DIB_GetColorCount(info
);
4260 x11ColorMap
= HeapAlloc(GetProcessHeap(), 0, nColorMap
* sizeof(int));
4261 for (i
= 0; i
< nColorMap
; i
++)
4262 x11ColorMap
[i
] = X11DRV_PALETTE_ToPhysical(physDevSrc
, PALETTEINDEX(i
));
4263 freeColorMap
= TRUE
;
4270 freeColorMap
= FALSE
;
4272 /* perform the copy */
4273 X11DRV_DIB_DoCopyDIBSection(physBitmap
, FALSE
, x11ColorMap
, nColorMap
,
4274 physDevDst
->drawable
, physDevDst
->gc
, xSrc
, ySrc
,
4275 physDevDst
->dc_rect
.left
+ xDest
, physDevDst
->dc_rect
.top
+ yDest
,
4277 /* free color mapping */
4279 HeapFree(GetProcessHeap(), 0, x11ColorMap
);
4283 /***********************************************************************
4284 * X11DRV_DIB_DoUpdateDIBSection
4286 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP
*physBitmap
, BOOL toDIB
)
4290 GetObjectW( physBitmap
->hbitmap
, sizeof(bitmap
), &bitmap
);
4291 X11DRV_DIB_DoCopyDIBSection(physBitmap
, toDIB
,
4292 physBitmap
->colorMap
, physBitmap
->nColorMap
,
4293 physBitmap
->pixmap
, BITMAP_GC(physBitmap
),
4294 0, 0, 0, 0, bitmap
.bmWidth
, bitmap
.bmHeight
);
4297 /***********************************************************************
4298 * X11DRV_DIB_FaultHandler
4300 static LONG CALLBACK
X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep
)
4302 X_PHYSBITMAP
*physBitmap
= NULL
;
4306 const size_t pagemask
= getpagesize() - 1;
4308 if (ep
->ExceptionRecord
->ExceptionCode
!= EXCEPTION_ACCESS_VIOLATION
)
4309 return EXCEPTION_CONTINUE_SEARCH
;
4311 addr
= (BYTE
*)ep
->ExceptionRecord
->ExceptionInformation
[1];
4313 EnterCriticalSection(&dibs_cs
);
4314 LIST_FOR_EACH( ptr
, &dibs_list
)
4316 physBitmap
= LIST_ENTRY( ptr
, X_PHYSBITMAP
, entry
);
4317 if ((physBitmap
->base
<= addr
) &&
4318 (addr
< physBitmap
->base
+ ((physBitmap
->size
+ pagemask
) & ~pagemask
)))
4324 LeaveCriticalSection(&dibs_cs
);
4326 if (!found
) return EXCEPTION_CONTINUE_SEARCH
;
4328 if (addr
>= physBitmap
->base
+ physBitmap
->size
)
4329 WARN( "%p: access to %p beyond the end of the DIB\n", physBitmap
->hbitmap
, addr
);
4331 X11DRV_DIB_Lock( physBitmap
, DIB_Status_None
);
4332 if (ep
->ExceptionRecord
->ExceptionInformation
[0] == EXCEPTION_WRITE_FAULT
) {
4333 /* the app tried to write the DIB bits */
4334 X11DRV_DIB_Coerce( physBitmap
, DIB_Status_AppMod
);
4336 /* the app tried to read the DIB bits */
4337 X11DRV_DIB_Coerce( physBitmap
, DIB_Status_InSync
);
4339 X11DRV_DIB_Unlock( physBitmap
, TRUE
);
4341 return EXCEPTION_CONTINUE_EXECUTION
;
4344 /***********************************************************************
4347 static INT
X11DRV_DIB_Coerce(X_PHYSBITMAP
*physBitmap
, INT req
)
4349 INT ret
= DIB_Status_None
;
4351 if (!physBitmap
->image
) return ret
; /* not a DIB section */
4352 EnterCriticalSection(&physBitmap
->lock
);
4353 ret
= physBitmap
->status
;
4355 case DIB_Status_GdiMod
:
4356 /* GDI access - request to draw on pixmap */
4357 switch (physBitmap
->status
)
4360 case DIB_Status_None
:
4361 physBitmap
->p_status
= DIB_Status_GdiMod
;
4362 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, FALSE
);
4365 case DIB_Status_GdiMod
:
4366 TRACE("GdiMod requested in status GdiMod\n" );
4367 physBitmap
->p_status
= DIB_Status_GdiMod
;
4370 case DIB_Status_InSync
:
4371 TRACE("GdiMod requested in status InSync\n" );
4372 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_NOACCESS
);
4373 physBitmap
->status
= DIB_Status_GdiMod
;
4374 physBitmap
->p_status
= DIB_Status_InSync
;
4377 case DIB_Status_AppMod
:
4378 TRACE("GdiMod requested in status AppMod\n" );
4379 /* make it readonly to avoid app changing data while we copy */
4380 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READONLY
);
4381 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, FALSE
);
4382 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_NOACCESS
);
4383 physBitmap
->p_status
= DIB_Status_AppMod
;
4384 physBitmap
->status
= DIB_Status_GdiMod
;
4389 case DIB_Status_InSync
:
4390 /* App access - request access to read DIB surface */
4391 /* (typically called from signal handler) */
4392 switch (physBitmap
->status
)
4395 case DIB_Status_None
:
4396 /* shouldn't happen from signal handler */
4399 case DIB_Status_GdiMod
:
4400 TRACE("InSync requested in status GdiMod\n" );
4401 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
4402 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, TRUE
);
4403 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READONLY
);
4404 physBitmap
->status
= DIB_Status_InSync
;
4407 case DIB_Status_InSync
:
4408 TRACE("InSync requested in status InSync\n" );
4409 /* shouldn't happen from signal handler */
4412 case DIB_Status_AppMod
:
4413 TRACE("InSync requested in status AppMod\n" );
4414 /* no reason to do anything here, and this
4415 * shouldn't happen from signal handler */
4420 case DIB_Status_AppMod
:
4421 /* App access - request access to write DIB surface */
4422 /* (typically called from signal handler) */
4423 switch (physBitmap
->status
)
4426 case DIB_Status_None
:
4427 /* shouldn't happen from signal handler */
4430 case DIB_Status_GdiMod
:
4431 TRACE("AppMod requested in status GdiMod\n" );
4432 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
4433 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, TRUE
);
4434 physBitmap
->status
= DIB_Status_AppMod
;
4437 case DIB_Status_InSync
:
4438 TRACE("AppMod requested in status InSync\n" );
4439 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
4440 physBitmap
->status
= DIB_Status_AppMod
;
4443 case DIB_Status_AppMod
:
4444 TRACE("AppMod requested in status AppMod\n" );
4445 /* shouldn't happen from signal handler */
4450 /* it is up to the caller to do the copy/conversion, probably
4451 * using the return value to decide where to copy from */
4453 LeaveCriticalSection(&physBitmap
->lock
);
4457 /***********************************************************************
4460 static INT
X11DRV_DIB_Lock(X_PHYSBITMAP
*physBitmap
, INT req
)
4462 INT ret
= DIB_Status_None
;
4464 if (!physBitmap
->image
) return ret
; /* not a DIB section */
4465 TRACE("Locking %p from thread %04x\n", physBitmap
->hbitmap
, GetCurrentThreadId());
4466 EnterCriticalSection(&physBitmap
->lock
);
4467 ret
= physBitmap
->status
;
4468 if (req
!= DIB_Status_None
)
4469 X11DRV_DIB_Coerce(physBitmap
, req
);
4473 /***********************************************************************
4476 static void X11DRV_DIB_Unlock(X_PHYSBITMAP
*physBitmap
, BOOL commit
)
4478 if (!physBitmap
->image
) return; /* not a DIB section */
4479 switch (physBitmap
->status
)
4482 case DIB_Status_None
:
4483 /* in case anyone is wondering, this is the "signal handler doesn't
4484 * work" case, where we always have to be ready for app access */
4486 switch (physBitmap
->p_status
)
4488 case DIB_Status_GdiMod
:
4489 TRACE("Unlocking and syncing from GdiMod\n" );
4490 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, TRUE
);
4494 TRACE("Unlocking without needing to sync\n" );
4498 else TRACE("Unlocking with no changes\n");
4499 physBitmap
->p_status
= DIB_Status_None
;
4502 case DIB_Status_GdiMod
:
4503 TRACE("Unlocking in status GdiMod\n" );
4504 /* DIB was protected in Coerce */
4506 /* no commit, revert to InSync if applicable */
4507 if ((physBitmap
->p_status
== DIB_Status_InSync
) ||
4508 (physBitmap
->p_status
== DIB_Status_AppMod
)) {
4509 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READONLY
);
4510 physBitmap
->status
= DIB_Status_InSync
;
4515 case DIB_Status_InSync
:
4516 TRACE("Unlocking in status InSync\n" );
4517 /* DIB was already protected in Coerce */
4520 case DIB_Status_AppMod
:
4521 TRACE("Unlocking in status AppMod\n" );
4522 /* DIB was already protected in Coerce */
4523 /* this case is ordinary only called from the signal handler,
4524 * so we don't bother to check for !commit */
4527 LeaveCriticalSection(&physBitmap
->lock
);
4528 TRACE("Unlocked %p\n", physBitmap
->hbitmap
);
4531 /***********************************************************************
4532 * X11DRV_CoerceDIBSection
4534 INT
X11DRV_CoerceDIBSection(X11DRV_PDEVICE
*physDev
, INT req
)
4536 if (!physDev
|| !physDev
->bitmap
) return DIB_Status_None
;
4537 return X11DRV_DIB_Coerce(physDev
->bitmap
, req
);
4540 /***********************************************************************
4541 * X11DRV_LockDIBSection
4543 INT
X11DRV_LockDIBSection(X11DRV_PDEVICE
*physDev
, INT req
)
4545 if (!physDev
|| !physDev
->bitmap
) return DIB_Status_None
;
4546 return X11DRV_DIB_Lock(physDev
->bitmap
, req
);
4549 /***********************************************************************
4550 * X11DRV_UnlockDIBSection
4552 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE
*physDev
, BOOL commit
)
4554 if (!physDev
|| !physDev
->bitmap
) return;
4555 X11DRV_DIB_Unlock(physDev
->bitmap
, commit
);
4559 #ifdef HAVE_LIBXXSHM
4560 /***********************************************************************
4561 * X11DRV_XShmErrorHandler
4564 static int XShmErrorHandler( Display
*dpy
, XErrorEvent
*event
, void *arg
)
4566 return 1; /* FIXME: should check event contents */
4569 /***********************************************************************
4570 * X11DRV_XShmCreateImage
4573 static XImage
*X11DRV_XShmCreateImage( int width
, int height
, int bpp
,
4574 XShmSegmentInfo
* shminfo
)
4578 image
= XShmCreateImage(gdi_display
, visual
, bpp
, ZPixmap
, NULL
, shminfo
, width
, height
);
4581 shminfo
->shmid
= shmget(IPC_PRIVATE
, image
->bytes_per_line
* height
,
4583 if( shminfo
->shmid
!= -1 )
4585 shminfo
->shmaddr
= image
->data
= shmat(shminfo
->shmid
, 0, 0);
4586 if( shminfo
->shmaddr
!= (char*)-1 )
4590 shminfo
->readOnly
= FALSE
;
4591 X11DRV_expect_error( gdi_display
, XShmErrorHandler
, NULL
);
4592 ok
= (XShmAttach( gdi_display
, shminfo
) != 0);
4593 XSync( gdi_display
, False
);
4594 if (X11DRV_check_error()) ok
= FALSE
;
4597 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
4598 return image
; /* Success! */
4600 /* An error occurred */
4601 shmdt(shminfo
->shmaddr
);
4603 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
4604 shminfo
->shmid
= -1;
4606 XFlush(gdi_display
);
4607 XDestroyImage(image
);
4612 #endif /* HAVE_LIBXXSHM */
4615 /***********************************************************************
4616 * X11DRV_CreateDIBSection (X11DRV.@)
4618 HBITMAP
X11DRV_CreateDIBSection( X11DRV_PDEVICE
*physDev
, HBITMAP hbitmap
,
4619 const BITMAPINFO
*bmi
, UINT usage
)
4621 X_PHYSBITMAP
*physBitmap
;
4624 if (!(physBitmap
= X11DRV_init_phys_bitmap( hbitmap
))) return 0;
4625 physBitmap
->status
= DIB_Status_None
;
4627 GetObjectW( hbitmap
, sizeof(dib
), &dib
);
4629 /* create color map */
4630 if (dib
.dsBm
.bmBitsPixel
<= 8)
4632 physBitmap
->colorMap
= X11DRV_DIB_BuildColorMap( physDev
,
4633 usage
, dib
.dsBm
.bmBitsPixel
, bmi
,
4634 &physBitmap
->nColorMap
);
4637 /* create pixmap and X image */
4639 physBitmap
->pixmap_depth
= (dib
.dsBm
.bmBitsPixel
== 1) ? 1 : screen_depth
;
4640 physBitmap
->pixmap
= XCreatePixmap( gdi_display
, root_window
, dib
.dsBm
.bmWidth
,
4641 dib
.dsBm
.bmHeight
, physBitmap
->pixmap_depth
);
4642 #ifdef HAVE_LIBXXSHM
4643 physBitmap
->shminfo
.shmid
= -1;
4644 if (!XShmQueryExtension(gdi_display
) ||
4645 !(physBitmap
->image
= X11DRV_XShmCreateImage( dib
.dsBm
.bmWidth
, dib
.dsBm
.bmHeight
,
4646 physBitmap
->pixmap_depth
, &physBitmap
->shminfo
)) )
4648 physBitmap
->image
= X11DRV_DIB_CreateXImage( dib
.dsBm
.bmWidth
, dib
.dsBm
.bmHeight
,
4649 physBitmap
->pixmap_depth
);
4650 wine_tsx11_unlock();
4651 if (!physBitmap
->pixmap
|| !physBitmap
->image
) return 0;
4653 /* install fault handler */
4654 InitializeCriticalSection( &physBitmap
->lock
);
4655 physBitmap
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": X_PHYSBITMAP.lock");
4657 physBitmap
->base
= dib
.dsBm
.bmBits
;
4658 physBitmap
->size
= dib
.dsBmih
.biSizeImage
;
4659 physBitmap
->status
= DIB_Status_AppMod
;
4662 dibs_handler
= AddVectoredExceptionHandler( TRUE
, X11DRV_DIB_FaultHandler
);
4663 EnterCriticalSection( &dibs_cs
);
4664 list_add_head( &dibs_list
, &physBitmap
->entry
);
4665 LeaveCriticalSection( &dibs_cs
);
4667 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
4672 /***********************************************************************
4673 * X11DRV_DIB_DeleteDIBSection
4675 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP
*physBitmap
, DIBSECTION
*dib
)
4679 EnterCriticalSection( &dibs_cs
);
4680 list_remove( &physBitmap
->entry
);
4681 last
= list_empty( &dibs_list
);
4682 LeaveCriticalSection( &dibs_cs
);
4686 RemoveVectoredExceptionHandler( dibs_handler
);
4687 dibs_handler
= NULL
;
4690 if (dib
->dshSection
)
4691 X11DRV_DIB_Coerce(physBitmap
, DIB_Status_InSync
);
4693 if (physBitmap
->image
)
4696 #ifdef HAVE_LIBXXSHM
4697 if (physBitmap
->shminfo
.shmid
!= -1)
4699 XShmDetach( gdi_display
, &(physBitmap
->shminfo
) );
4700 XDestroyImage( physBitmap
->image
);
4701 shmdt( physBitmap
->shminfo
.shmaddr
);
4702 physBitmap
->shminfo
.shmid
= -1;
4706 XDestroyImage( physBitmap
->image
);
4707 wine_tsx11_unlock();
4710 HeapFree(GetProcessHeap(), 0, physBitmap
->colorMap
);
4711 physBitmap
->lock
.DebugInfo
->Spare
[0] = 0;
4712 DeleteCriticalSection(&physBitmap
->lock
);
4715 /***********************************************************************
4716 * SetDIBColorTable (X11DRV.@)
4718 UINT
X11DRV_SetDIBColorTable( X11DRV_PDEVICE
*physDev
, UINT start
, UINT count
, const RGBQUAD
*colors
)
4722 X_PHYSBITMAP
*physBitmap
= physDev
->bitmap
;
4724 if (!physBitmap
) return 0;
4725 GetObjectW( physBitmap
->hbitmap
, sizeof(dib
), &dib
);
4727 if (physBitmap
->colorMap
&& start
< physBitmap
->nColorMap
) {
4728 UINT end
= count
+ start
;
4729 if (end
> physBitmap
->nColorMap
) end
= physBitmap
->nColorMap
;
4731 * Changing color table might change the mapping between
4732 * DIB colors and X11 colors and thus alter the visible state
4733 * of the bitmap object.
4736 * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4737 * at least for a 1 bpp dibsection
4739 X11DRV_DIB_Lock( physBitmap
, DIB_Status_AppMod
);
4740 X11DRV_DIB_GenColorMap( physDev
, physBitmap
->colorMap
, DIB_RGB_COLORS
,
4741 dib
.dsBm
.bmBitsPixel
,
4742 TRUE
, colors
, start
, end
);
4743 X11DRV_DIB_Unlock( physBitmap
, TRUE
);
4750 /***********************************************************************
4751 * X11DRV_DIB_CreateDIBFromBitmap
4753 * Allocates a packed DIB and copies the bitmap data into it.
4755 HGLOBAL
X11DRV_DIB_CreateDIBFromBitmap(HDC hdc
, HBITMAP hBmp
)
4760 LPBITMAPINFOHEADER pbmiHeader
;
4761 unsigned int cDataSize
, cPackedSize
, OffsetBits
;
4764 if (!GetObjectW( hBmp
, sizeof(bmp
), &bmp
)) return 0;
4767 * A packed DIB contains a BITMAPINFO structure followed immediately by
4768 * an optional color palette and the pixel data.
4771 /* Calculate the size of the packed DIB */
4772 cDataSize
= X11DRV_DIB_GetDIBWidthBytes( bmp
.bmWidth
, bmp
.bmBitsPixel
) * abs( bmp
.bmHeight
);
4773 cPackedSize
= sizeof(BITMAPINFOHEADER
)
4774 + ( (bmp
.bmBitsPixel
<= 8) ? (sizeof(RGBQUAD
) * (1 << bmp
.bmBitsPixel
)) : 0 )
4776 /* Get the offset to the bits */
4777 OffsetBits
= cPackedSize
- cDataSize
;
4779 /* Allocate the packed DIB */
4780 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize
);
4781 hPackedDIB
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
/*| GMEM_ZEROINIT*/,
4785 WARN("Could not allocate packed DIB!\n");
4789 /* A packed DIB starts with a BITMAPINFOHEADER */
4790 pPackedDIB
= GlobalLock(hPackedDIB
);
4791 pbmiHeader
= (LPBITMAPINFOHEADER
)pPackedDIB
;
4793 /* Init the BITMAPINFOHEADER */
4794 pbmiHeader
->biSize
= sizeof(BITMAPINFOHEADER
);
4795 pbmiHeader
->biWidth
= bmp
.bmWidth
;
4796 pbmiHeader
->biHeight
= bmp
.bmHeight
;
4797 pbmiHeader
->biPlanes
= 1;
4798 pbmiHeader
->biBitCount
= bmp
.bmBitsPixel
;
4799 pbmiHeader
->biCompression
= BI_RGB
;
4800 pbmiHeader
->biSizeImage
= 0;
4801 pbmiHeader
->biXPelsPerMeter
= pbmiHeader
->biYPelsPerMeter
= 0;
4802 pbmiHeader
->biClrUsed
= 0;
4803 pbmiHeader
->biClrImportant
= 0;
4805 /* Retrieve the DIB bits from the bitmap and fill in the
4806 * DIB color table if present */
4808 nLinesCopied
= GetDIBits(hdc
, /* Handle to device context */
4809 hBmp
, /* Handle to bitmap */
4810 0, /* First scan line to set in dest bitmap */
4811 bmp
.bmHeight
, /* Number of scan lines to copy */
4812 pPackedDIB
+ OffsetBits
, /* [out] Address of array for bitmap bits */
4813 (LPBITMAPINFO
) pbmiHeader
, /* [out] Address of BITMAPINFO structure */
4814 0); /* RGB or palette index */
4815 GlobalUnlock(hPackedDIB
);
4817 /* Cleanup if GetDIBits failed */
4818 if (nLinesCopied
!= bmp
.bmHeight
)
4820 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied
, bmp
.bmHeight
);
4821 GlobalFree(hPackedDIB
);
4828 /**************************************************************************
4829 * X11DRV_DIB_CreateDIBFromPixmap
4831 * Allocates a packed DIB and copies the Pixmap data into it.
4832 * The Pixmap passed in is deleted after the conversion.
4834 HGLOBAL
X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap
, HDC hdc
)
4837 X_PHYSBITMAP
*physBitmap
;
4838 HBITMAP hBmp
= 0, old
;
4839 HGLOBAL hPackedDIB
= 0;
4841 int x
,y
; /* Unused */
4842 unsigned border_width
; /* Unused */
4843 unsigned int depth
, width
, height
;
4845 /* Get the Pixmap dimensions and bit depth */
4847 if (!XGetGeometry(gdi_display
, pixmap
, &root
, &x
, &y
, &width
, &height
,
4848 &border_width
, &depth
)) depth
= 0;
4849 wine_tsx11_unlock();
4850 if (!depth
) return 0;
4852 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
4853 width
, height
, depth
);
4856 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
4857 * and make it a container for the pixmap passed.
4859 if (!(hBmp
= CreateBitmap( width
, height
, 1, depth_to_bpp(depth
), NULL
))) return 0;
4861 /* force bitmap to be owned by a screen DC */
4862 hdcMem
= CreateCompatibleDC( hdc
);
4863 old
= SelectObject( hdcMem
, hBmp
);
4865 physBitmap
= X11DRV_get_phys_bitmap( hBmp
);
4868 if (physBitmap
->pixmap
) XFreePixmap( gdi_display
, physBitmap
->pixmap
);
4869 physBitmap
->pixmap
= pixmap
;
4870 wine_tsx11_unlock();
4872 SelectObject( hdcMem
, old
);
4876 * Create a packed DIB from the Pixmap wrapper bitmap created above.
4877 * A packed DIB contains a BITMAPINFO structure followed immediately by
4878 * an optional color palette and the pixel data.
4880 hPackedDIB
= X11DRV_DIB_CreateDIBFromBitmap(hdc
, hBmp
);
4882 /* We can now get rid of the HBITMAP wrapper we created earlier.
4883 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
4887 TRACE("\tReturning packed DIB %p\n", hPackedDIB
);
4892 /**************************************************************************
4893 * X11DRV_DIB_CreatePixmapFromDIB
4895 * Creates a Pixmap from a packed DIB
4897 Pixmap
X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB
, HDC hdc
)
4900 X_PHYSBITMAP
*physBitmap
;
4904 /* Create a DDB from the DIB */
4906 pbmi
= GlobalLock(hPackedDIB
);
4907 hBmp
= CreateDIBitmap(hdc
, &pbmi
->bmiHeader
, CBM_INIT
,
4908 (LPBYTE
)pbmi
+ X11DRV_DIB_BitmapInfoSize( pbmi
, DIB_RGB_COLORS
),
4909 pbmi
, DIB_RGB_COLORS
);
4910 GlobalUnlock(hPackedDIB
);
4912 /* clear the physBitmap so that we can steal its pixmap */
4913 physBitmap
= X11DRV_get_phys_bitmap( hBmp
);
4914 pixmap
= physBitmap
->pixmap
;
4915 physBitmap
->pixmap
= 0;
4917 /* Delete the DDB we created earlier now that we have stolen its pixmap */
4920 TRACE("Returning Pixmap %ld\n", pixmap
);