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 * DIB_DoProtectDIBSection
113 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP
*physBitmap
, DWORD new_prot
)
117 VirtualProtect(physBitmap
->base
, physBitmap
->size
, new_prot
, &old_prot
);
118 TRACE("Changed protection from %d to %d\n", old_prot
, new_prot
);
121 /***********************************************************************
122 * X11DRV_DIB_GetXImageWidthBytes
124 * Return the width of an X image in bytes
126 static inline int X11DRV_DIB_GetXImageWidthBytes( int width
, int depth
)
128 if (!depth
|| depth
> 32) goto error
;
130 if (!ximageDepthTable
[depth
-1])
132 XImage
*testimage
= XCreateImage( gdi_display
, visual
, depth
,
133 ZPixmap
, 0, NULL
, 1, 1, 32, 20 );
136 ximageDepthTable
[depth
-1] = testimage
->bits_per_pixel
;
137 XDestroyImage( testimage
);
139 else ximageDepthTable
[depth
-1] = -1;
141 if (ximageDepthTable
[depth
-1] != -1)
142 return (4 * ((width
* ximageDepthTable
[depth
-1] + 31) / 32));
145 WARN( "(%d): Unsupported depth\n", depth
);
150 /***********************************************************************
151 * X11DRV_DIB_GetDIBWidthBytes
153 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
155 static int X11DRV_DIB_GetDIBWidthBytes( int width
, int depth
)
161 case 1: words
= (width
+ 31) / 32; break;
162 case 4: words
= (width
+ 7) / 8; break;
163 case 8: words
= (width
+ 3) / 4; break;
165 case 16: words
= (width
+ 1) / 2; break;
166 case 24: words
= (width
* 3 + 3) / 4; break;
168 WARN("(%d): Unsupported depth\n", depth
);
177 /***********************************************************************
178 * X11DRV_DIB_GetDIBImageBytes
180 * Return the number of bytes used to hold the image in a DIB bitmap.
182 static int X11DRV_DIB_GetDIBImageBytes( int width
, int height
, int depth
)
184 return X11DRV_DIB_GetDIBWidthBytes( width
, depth
) * abs( height
);
188 /***********************************************************************
189 * X11DRV_DIB_BitmapInfoSize
191 * Return the size of the bitmap info structure including color table.
193 int X11DRV_DIB_BitmapInfoSize( const BITMAPINFO
* info
, WORD coloruse
)
197 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
199 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
200 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
201 return sizeof(BITMAPCOREHEADER
) + colors
*
202 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
204 else /* assume BITMAPINFOHEADER */
206 colors
= info
->bmiHeader
.biClrUsed
;
207 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
208 colors
= 1 << info
->bmiHeader
.biBitCount
;
209 return sizeof(BITMAPINFOHEADER
) + colors
*
210 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
215 /***********************************************************************
216 * X11DRV_DIB_CreateXImage
220 XImage
*X11DRV_DIB_CreateXImage( int width
, int height
, int depth
)
226 width_bytes
= X11DRV_DIB_GetXImageWidthBytes( width
, depth
);
227 image
= XCreateImage( gdi_display
, visual
, depth
, ZPixmap
, 0,
228 calloc( height
, width_bytes
),
229 width
, height
, 32, width_bytes
);
235 /***********************************************************************
236 * DIB_GetBitmapInfoEx
238 * Get the info from a bitmap header.
239 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
241 static int DIB_GetBitmapInfoEx( const BITMAPINFOHEADER
*header
, LONG
*width
,
242 LONG
*height
, WORD
*planes
, WORD
*bpp
,
243 WORD
*compr
, DWORD
*size
)
245 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
247 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
248 *width
= core
->bcWidth
;
249 *height
= core
->bcHeight
;
250 *planes
= core
->bcPlanes
;
251 *bpp
= core
->bcBitCount
;
256 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
))
258 *width
= header
->biWidth
;
259 *height
= header
->biHeight
;
260 *planes
= header
->biPlanes
;
261 *bpp
= header
->biBitCount
;
262 *compr
= header
->biCompression
;
263 *size
= header
->biSizeImage
;
266 ERR("(%d): unknown/wrong size for header\n", header
->biSize
);
271 /***********************************************************************
272 * X11DRV_DIB_GetColorCount
274 * Computes the number of colors for the bitmap palette.
275 * Should not be called for a >8-bit deep bitmap.
277 static unsigned int X11DRV_DIB_GetColorCount(const BITMAPINFO
*info
)
280 BOOL core_info
= info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
);
284 colors
= 1 << ((const BITMAPCOREINFO
*)info
)->bmciHeader
.bcBitCount
;
288 colors
= info
->bmiHeader
.biClrUsed
;
289 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
293 ERR("called with >256 colors!\n");
299 /***********************************************************************
302 * Get the info from a bitmap header.
303 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
305 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
306 LONG
*height
, WORD
*bpp
, WORD
*compr
)
311 return DIB_GetBitmapInfoEx( header
, width
, height
, &planes
, bpp
, compr
, &size
);
315 static inline BOOL
colour_is_brighter(RGBQUAD c1
, RGBQUAD c2
)
317 return (c1
.rgbRed
* c1
.rgbRed
+ c1
.rgbGreen
* c1
.rgbGreen
+ c1
.rgbBlue
* c1
.rgbBlue
) >
318 (c2
.rgbRed
* c2
.rgbRed
+ c2
.rgbGreen
* c2
.rgbGreen
+ c2
.rgbBlue
* c2
.rgbBlue
);
321 /***********************************************************************
322 * X11DRV_DIB_GenColorMap
324 * Fills the color map of a bitmap palette. Should not be called
325 * for a >8-bit deep bitmap.
327 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE
*physDev
, int *colorMapping
,
328 WORD coloruse
, WORD depth
, BOOL quads
,
329 const void *colorPtr
, int start
, int end
)
333 if (coloruse
== DIB_RGB_COLORS
)
337 const RGBQUAD
* rgb
= (const RGBQUAD
*)colorPtr
;
339 if (depth
== 1) /* Monochrome */
344 if (GetDIBColorTable( physDev
->hdc
, 0, 2, table
) == 2)
345 invert
= !colour_is_brighter(table
[1], table
[0]);
347 for (i
= start
; i
< end
; i
++, rgb
++)
348 colorMapping
[i
] = ((rgb
->rgbRed
+ rgb
->rgbGreen
+
349 rgb
->rgbBlue
> 255*3/2 && !invert
) ||
350 (rgb
->rgbRed
+ rgb
->rgbGreen
+
351 rgb
->rgbBlue
<= 255*3/2 && invert
));
354 for (i
= start
; i
< end
; i
++, rgb
++)
355 colorMapping
[i
] = X11DRV_PALETTE_ToPhysical( NULL
, RGB(rgb
->rgbRed
,
361 const RGBTRIPLE
* rgb
= (const RGBTRIPLE
*)colorPtr
;
363 if (depth
== 1) /* Monochrome */
368 if (GetDIBColorTable( physDev
->hdc
, 0, 2, table
) == 2)
369 invert
= !colour_is_brighter(table
[1], table
[0]);
371 for (i
= start
; i
< end
; i
++, rgb
++)
372 colorMapping
[i
] = ((rgb
->rgbtRed
+ rgb
->rgbtGreen
+
373 rgb
->rgbtBlue
> 255*3/2 && !invert
) ||
374 (rgb
->rgbtRed
+ rgb
->rgbtGreen
+
375 rgb
->rgbtBlue
<= 255*3/2 && invert
));
378 for (i
= start
; i
< end
; i
++, rgb
++)
379 colorMapping
[i
] = X11DRV_PALETTE_ToPhysical( NULL
, RGB(rgb
->rgbtRed
,
384 else /* DIB_PAL_COLORS */
386 const WORD
* index
= (const WORD
*)colorPtr
;
388 for (i
= start
; i
< end
; i
++, index
++)
389 colorMapping
[i
] = X11DRV_PALETTE_ToPhysical( physDev
, PALETTEINDEX(*index
) );
395 /***********************************************************************
396 * X11DRV_DIB_BuildColorMap
398 * Build the color map from the bitmap palette. Should not be called
399 * for a >8-bit deep bitmap.
401 static int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE
*physDev
, WORD coloruse
, WORD depth
,
402 const BITMAPINFO
*info
, int *nColors
)
405 const void *colorPtr
;
409 *nColors
= X11DRV_DIB_GetColorCount(info
);
410 if (!*nColors
) return NULL
;
412 isInfo
= info
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
);
413 colorPtr
= (const BYTE
*)info
+ (WORD
)info
->bmiHeader
.biSize
;
414 if (!(colorMapping
= HeapAlloc(GetProcessHeap(), 0, *nColors
* sizeof(int) )))
417 return X11DRV_DIB_GenColorMap( physDev
, colorMapping
, coloruse
, depth
,
418 isInfo
, colorPtr
, 0, *nColors
);
421 /***********************************************************************
422 * X11DRV_DIB_MapColor
424 static int X11DRV_DIB_MapColor( int *physMap
, int nPhysMap
, int phys
, int oldcol
)
428 if ((oldcol
< nPhysMap
) && (physMap
[oldcol
] == phys
))
431 for (color
= 0; color
< nPhysMap
; color
++)
432 if (physMap
[color
] == phys
)
435 WARN("Strange color %08x\n", phys
);
440 /*********************************************************************
441 * X11DRV_DIB_GetNearestIndex
443 * Helper for X11DRV_DIB_GetDIBits.
444 * Returns the nearest colour table index for a given RGB.
445 * Nearest is defined by minimizing the sum of the squares.
447 static INT
X11DRV_DIB_GetNearestIndex(RGBQUAD
*colormap
, int numColors
, BYTE r
, BYTE g
, BYTE b
)
449 INT i
, best
= -1, diff
, bestdiff
= -1;
452 for(color
= colormap
, i
= 0; i
< numColors
; color
++, i
++) {
453 diff
= (r
- color
->rgbRed
) * (r
- color
->rgbRed
) +
454 (g
- color
->rgbGreen
) * (g
- color
->rgbGreen
) +
455 (b
- color
->rgbBlue
) * (b
- color
->rgbBlue
);
458 if(best
== -1 || diff
< bestdiff
) {
465 /*********************************************************************
466 * X11DRV_DIB_MaskToShift
468 * Helper for X11DRV_DIB_GetDIBits.
469 * Returns the by how many bits to shift a given color so that it is
470 * in the proper position.
472 INT
X11DRV_DIB_MaskToShift(DWORD mask
)
480 while ((mask
&1)==0) {
487 /***********************************************************************
488 * X11DRV_DIB_CheckMask
490 * Check RGB mask if it is either 0 or matches visual's mask.
492 static inline int X11DRV_DIB_CheckMask(int red_mask
, int green_mask
, int blue_mask
)
494 return ( red_mask
== 0 && green_mask
== 0 && blue_mask
== 0 ) ||
495 ( red_mask
== visual
->red_mask
&& green_mask
== visual
->green_mask
&&
496 blue_mask
== visual
->blue_mask
);
499 /***********************************************************************
500 * X11DRV_DIB_SetImageBits_1
502 * SetDIBits for a 1-bit deep DIB.
504 static void X11DRV_DIB_SetImageBits_1( int lines
, const BYTE
*srcbits
,
505 DWORD srcwidth
, DWORD dstwidth
, int left
,
506 int *colors
, XImage
*bmpImage
, DWORD linebytes
)
515 srcbits
= srcbits
+ linebytes
* (lines
- 1);
516 linebytes
= -linebytes
;
519 if ((extra
= (left
& 7)) != 0) {
523 srcbits
+= left
>> 3;
524 width
= min(srcwidth
, dstwidth
);
526 /* ==== pal 1 dib -> any bmp format ==== */
527 for (h
= lines
-1; h
>=0; h
--) {
529 for (i
= width
/8, x
= left
; i
> 0; i
--) {
531 XPutPixel( bmpImage
, x
++, h
, colors
[ srcval
>> 7] );
532 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 6) & 1] );
533 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 5) & 1] );
534 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 4) & 1] );
535 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 3) & 1] );
536 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 2) & 1] );
537 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 1) & 1] );
538 XPutPixel( bmpImage
, x
++, h
, colors
[ srcval
& 1] );
544 case 7: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
545 case 6: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
546 case 5: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
547 case 4: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
548 case 3: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
549 case 2: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
550 case 1: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]);
553 srcbits
+= linebytes
;
557 /***********************************************************************
558 * X11DRV_DIB_GetImageBits_1
560 * GetDIBits for a 1-bit deep DIB.
562 static void X11DRV_DIB_GetImageBits_1( int lines
, BYTE
*dstbits
,
563 DWORD dstwidth
, DWORD srcwidth
,
564 RGBQUAD
*colors
, PALETTEENTRY
*srccolors
,
565 XImage
*bmpImage
, DWORD linebytes
)
568 int h
, width
= min(dstwidth
, srcwidth
);
572 dstbits
= dstbits
+ linebytes
* (lines
- 1);
573 linebytes
= -linebytes
;
576 switch (bmpImage
->depth
)
580 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
582 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
585 for (h
=lines
-1; h
>=0; h
--) {
589 for (x
=0; x
<width
; x
++) {
591 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
592 dstval
|=(X11DRV_DIB_GetNearestIndex
596 srcval
.peBlue
) << (7 - (x
& 7)));
605 dstbits
+= linebytes
;
613 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
, bmpImage
->green_mask
, bmpImage
->blue_mask
)
615 /* ==== pal 8 bmp -> pal 1 dib ==== */
617 const BYTE
* srcpixel
;
620 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
622 for (h
=0; h
<lines
; h
++) {
627 for (x
=0; x
<width
; x
++) {
629 srcval
=srccolors
[*srcpixel
++];
630 dstval
|=(X11DRV_DIB_GetNearestIndex
634 srcval
.peBlue
) << (7-(x
&7)) );
643 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
644 dstbits
+= linebytes
;
655 const WORD
* srcpixel
;
658 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
660 if (bmpImage
->green_mask
==0x03e0) {
661 if (bmpImage
->red_mask
==0x7c00) {
662 /* ==== rgb 555 bmp -> pal 1 dib ==== */
663 for (h
=0; h
<lines
; h
++) {
668 for (x
=0; x
<width
; x
++) {
671 dstval
|=(X11DRV_DIB_GetNearestIndex
673 ((srcval
>> 7) & 0xf8) | /* r */
674 ((srcval
>> 12) & 0x07),
675 ((srcval
>> 2) & 0xf8) | /* g */
676 ((srcval
>> 7) & 0x07),
677 ((srcval
<< 3) & 0xf8) | /* b */
678 ((srcval
>> 2) & 0x07) ) << (7-(x
&7)) );
687 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
688 dstbits
+= linebytes
;
690 } else if (bmpImage
->blue_mask
==0x7c00) {
691 /* ==== bgr 555 bmp -> pal 1 dib ==== */
692 for (h
=0; h
<lines
; h
++) {
697 for (x
=0; x
<width
; x
++) {
700 dstval
|=(X11DRV_DIB_GetNearestIndex
702 ((srcval
<< 3) & 0xf8) | /* r */
703 ((srcval
>> 2) & 0x07),
704 ((srcval
>> 2) & 0xf8) | /* g */
705 ((srcval
>> 7) & 0x07),
706 ((srcval
>> 7) & 0xf8) | /* b */
707 ((srcval
>> 12) & 0x07) ) << (7-(x
&7)) );
716 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
717 dstbits
+= linebytes
;
722 } else if (bmpImage
->green_mask
==0x07e0) {
723 if (bmpImage
->red_mask
==0xf800) {
724 /* ==== rgb 565 bmp -> pal 1 dib ==== */
725 for (h
=0; h
<lines
; h
++) {
730 for (x
=0; x
<width
; x
++) {
733 dstval
|=(X11DRV_DIB_GetNearestIndex
735 ((srcval
>> 8) & 0xf8) | /* r */
736 ((srcval
>> 13) & 0x07),
737 ((srcval
>> 3) & 0xfc) | /* g */
738 ((srcval
>> 9) & 0x03),
739 ((srcval
<< 3) & 0xf8) | /* b */
740 ((srcval
>> 2) & 0x07) ) << (7-(x
&7)) );
749 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
750 dstbits
+= linebytes
;
752 } else if (bmpImage
->blue_mask
==0xf800) {
753 /* ==== bgr 565 bmp -> pal 1 dib ==== */
754 for (h
=0; h
<lines
; h
++) {
759 for (x
=0; x
<width
; x
++) {
762 dstval
|=(X11DRV_DIB_GetNearestIndex
764 ((srcval
<< 3) & 0xf8) | /* r */
765 ((srcval
>> 2) & 0x07),
766 ((srcval
>> 3) & 0xfc) | /* g */
767 ((srcval
>> 9) & 0x03),
768 ((srcval
>> 8) & 0xf8) | /* b */
769 ((srcval
>> 13) & 0x07) ) << (7-(x
&7)) );
778 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
779 dstbits
+= linebytes
;
798 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
799 bytes_per_pixel
=(bmpImage
->bits_per_pixel
==24?3:4);
801 if (bmpImage
->green_mask
!=0x00ff00 ||
802 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
804 } else if (bmpImage
->blue_mask
==0xff) {
805 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
806 for (h
=0; h
<lines
; h
++) {
811 for (x
=0; x
<width
; x
++) {
812 dstval
|=(X11DRV_DIB_GetNearestIndex
816 srcbyte
[0]) << (7-(x
&7)) );
817 srcbyte
+=bytes_per_pixel
;
826 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
827 dstbits
+= linebytes
;
830 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
831 for (h
=0; h
<lines
; h
++) {
836 for (x
=0; x
<width
; x
++) {
837 dstval
|=(X11DRV_DIB_GetNearestIndex
841 srcbyte
[2]) << (7-(x
&7)) );
842 srcbyte
+=bytes_per_pixel
;
851 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
852 dstbits
+= linebytes
;
863 unsigned long white
= (1 << bmpImage
->bits_per_pixel
) - 1;
865 /* ==== any bmp format -> pal 1 dib ==== */
866 if ((unsigned)colors
[0].rgbRed
+colors
[0].rgbGreen
+colors
[0].rgbBlue
>=
867 (unsigned)colors
[1].rgbRed
+colors
[1].rgbGreen
+colors
[1].rgbBlue
)
870 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB, "
871 "%s color mapping\n",
872 bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
873 bmpImage
->green_mask
, bmpImage
->blue_mask
,
874 neg
?"negative":"direct" );
876 for (h
=lines
-1; h
>=0; h
--) {
880 for (x
=0; x
<width
; x
++) {
881 dstval
|=((XGetPixel( bmpImage
, x
, h
) >= white
) ^ neg
) << (7 - (x
&7));
890 dstbits
+= linebytes
;
897 /***********************************************************************
898 * X11DRV_DIB_SetImageBits_4
900 * SetDIBits for a 4-bit deep DIB.
902 static void X11DRV_DIB_SetImageBits_4( int lines
, const BYTE
*srcbits
,
903 DWORD srcwidth
, DWORD dstwidth
, int left
,
904 int *colors
, XImage
*bmpImage
, DWORD linebytes
)
912 srcbits
= srcbits
+ linebytes
* (lines
- 1);
913 linebytes
= -linebytes
;
920 srcbits
+= left
>> 1;
921 width
= min(srcwidth
, dstwidth
);
923 /* ==== pal 4 dib -> any bmp format ==== */
924 for (h
= lines
-1; h
>= 0; h
--) {
926 for (i
= width
/2, x
= left
; i
> 0; i
--) {
927 BYTE srcval
=*srcbyte
++;
928 XPutPixel( bmpImage
, x
++, h
, colors
[srcval
>> 4] );
929 XPutPixel( bmpImage
, x
++, h
, colors
[srcval
& 0x0f] );
932 XPutPixel( bmpImage
, x
, h
, colors
[*srcbyte
>> 4] );
933 srcbits
+= linebytes
;
939 /***********************************************************************
940 * X11DRV_DIB_GetImageBits_4
942 * GetDIBits for a 4-bit deep DIB.
944 static void X11DRV_DIB_GetImageBits_4( int lines
, BYTE
*dstbits
,
945 DWORD srcwidth
, DWORD dstwidth
,
946 RGBQUAD
*colors
, PALETTEENTRY
*srccolors
,
947 XImage
*bmpImage
, DWORD linebytes
)
950 int h
, width
= min(srcwidth
, dstwidth
);
956 dstbits
= dstbits
+ ( linebytes
* (lines
-1) );
957 linebytes
= -linebytes
;
962 switch (bmpImage
->depth
) {
965 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
967 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
970 for (h
= lines
-1; h
>= 0; h
--) {
974 for (x
= 0; x
< width
; x
++) {
976 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
977 dstval
|=(X11DRV_DIB_GetNearestIndex
981 srcval
.peBlue
) << (4-((x
&1)<<2)));
990 dstbits
+= linebytes
;
998 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
1000 /* ==== pal 8 bmp -> pal 4 dib ==== */
1001 const void* srcbits
;
1002 const BYTE
*srcpixel
;
1005 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1006 for (h
=0; h
<lines
; h
++) {
1011 for (x
=0; x
<width
; x
++) {
1012 PALETTEENTRY srcval
;
1013 srcval
= srccolors
[*srcpixel
++];
1014 dstval
|=(X11DRV_DIB_GetNearestIndex
1018 srcval
.peBlue
) << (4*(1-(x
&1))) );
1027 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1028 dstbits
+= linebytes
;
1038 const void* srcbits
;
1039 const WORD
* srcpixel
;
1042 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1044 if (bmpImage
->green_mask
==0x03e0) {
1045 if (bmpImage
->red_mask
==0x7c00) {
1046 /* ==== rgb 555 bmp -> pal 4 dib ==== */
1047 for (h
=0; h
<lines
; h
++) {
1052 for (x
=0; x
<width
; x
++) {
1055 dstval
|=(X11DRV_DIB_GetNearestIndex
1057 ((srcval
>> 7) & 0xf8) | /* r */
1058 ((srcval
>> 12) & 0x07),
1059 ((srcval
>> 2) & 0xf8) | /* g */
1060 ((srcval
>> 7) & 0x07),
1061 ((srcval
<< 3) & 0xf8) | /* b */
1062 ((srcval
>> 2) & 0x07) ) << ((1-(x
&1))<<2) );
1071 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1072 dstbits
+= linebytes
;
1074 } else if (bmpImage
->blue_mask
==0x7c00) {
1075 /* ==== bgr 555 bmp -> pal 4 dib ==== */
1076 for (h
=0; h
<lines
; h
++) {
1081 for (x
=0; x
<width
; x
++) {
1084 dstval
|=(X11DRV_DIB_GetNearestIndex
1086 ((srcval
<< 3) & 0xf8) | /* r */
1087 ((srcval
>> 2) & 0x07),
1088 ((srcval
>> 2) & 0xf8) | /* g */
1089 ((srcval
>> 7) & 0x07),
1090 ((srcval
>> 7) & 0xf8) | /* b */
1091 ((srcval
>> 12) & 0x07) ) << ((1-(x
&1))<<2) );
1100 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1101 dstbits
+= linebytes
;
1106 } else if (bmpImage
->green_mask
==0x07e0) {
1107 if (bmpImage
->red_mask
==0xf800) {
1108 /* ==== rgb 565 bmp -> pal 4 dib ==== */
1109 for (h
=0; h
<lines
; h
++) {
1114 for (x
=0; x
<width
; x
++) {
1117 dstval
|=(X11DRV_DIB_GetNearestIndex
1119 ((srcval
>> 8) & 0xf8) | /* r */
1120 ((srcval
>> 13) & 0x07),
1121 ((srcval
>> 3) & 0xfc) | /* g */
1122 ((srcval
>> 9) & 0x03),
1123 ((srcval
<< 3) & 0xf8) | /* b */
1124 ((srcval
>> 2) & 0x07) ) << ((1-(x
&1))<<2) );
1133 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1134 dstbits
+= linebytes
;
1136 } else if (bmpImage
->blue_mask
==0xf800) {
1137 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1138 for (h
=0; h
<lines
; h
++) {
1143 for (x
=0; x
<width
; x
++) {
1146 dstval
|=(X11DRV_DIB_GetNearestIndex
1148 ((srcval
<< 3) & 0xf8) | /* r */
1149 ((srcval
>> 2) & 0x07),
1150 ((srcval
>> 3) & 0xfc) | /* g */
1151 ((srcval
>> 9) & 0x03),
1152 ((srcval
>> 8) & 0xf8) | /* b */
1153 ((srcval
>> 13) & 0x07) ) << ((1-(x
&1))<<2) );
1162 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1163 dstbits
+= linebytes
;
1175 if (bmpImage
->bits_per_pixel
==24) {
1176 const void* srcbits
;
1177 const BYTE
*srcbyte
;
1180 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1182 if (bmpImage
->green_mask
!=0x00ff00 ||
1183 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
1185 } else if (bmpImage
->blue_mask
==0xff) {
1186 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1187 for (h
=0; h
<lines
; h
++) {
1190 for (x
=0; x
<width
/2; x
++) {
1191 /* Do 2 pixels at a time */
1192 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1197 X11DRV_DIB_GetNearestIndex
1205 /* And then the odd pixel */
1206 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1212 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1213 dstbits
+= linebytes
;
1216 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1217 for (h
=0; h
<lines
; h
++) {
1220 for (x
=0; x
<width
/2; x
++) {
1221 /* Do 2 pixels at a time */
1222 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1227 X11DRV_DIB_GetNearestIndex
1235 /* And then the odd pixel */
1236 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1242 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1243 dstbits
+= linebytes
;
1252 const void* srcbits
;
1253 const BYTE
*srcbyte
;
1256 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1258 if (bmpImage
->green_mask
!=0x00ff00 ||
1259 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
1261 } else if (bmpImage
->blue_mask
==0xff) {
1262 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1263 for (h
=0; h
<lines
; h
++) {
1266 for (x
=0; x
<width
/2; x
++) {
1267 /* Do 2 pixels at a time */
1268 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1273 X11DRV_DIB_GetNearestIndex
1281 /* And then the odd pixel */
1282 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1288 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1289 dstbits
+= linebytes
;
1292 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1293 for (h
=0; h
<lines
; h
++) {
1296 for (x
=0; x
<width
/2; x
++) {
1297 /* Do 2 pixels at a time */
1298 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1303 X11DRV_DIB_GetNearestIndex
1311 /* And then the odd pixel */
1312 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1318 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1319 dstbits
+= linebytes
;
1330 /* ==== any bmp format -> pal 4 dib ==== */
1331 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1332 bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
1333 bmpImage
->green_mask
, bmpImage
->blue_mask
);
1334 for (h
=lines
-1; h
>=0; h
--) {
1336 for (x
=0; x
<(width
& ~1); x
+=2) {
1337 *dstbyte
++=(X11DRV_DIB_MapColor((int*)colors
, 16, XGetPixel(bmpImage
, x
, h
), 0) << 4) |
1338 X11DRV_DIB_MapColor((int*)colors
, 16, XGetPixel(bmpImage
, x
+1, h
), 0);
1341 *dstbyte
=(X11DRV_DIB_MapColor((int *)colors
, 16, XGetPixel(bmpImage
, x
, h
), 0) << 4);
1343 dstbits
+= linebytes
;
1350 /***********************************************************************
1351 * X11DRV_DIB_SetImageBits_RLE4
1353 * SetDIBits for a 4-bit deep compressed DIB.
1355 static void X11DRV_DIB_SetImageBits_RLE4( int lines
, const BYTE
*bits
,
1356 DWORD srcwidth
, DWORD dstwidth
,
1357 int left
, int *colors
,
1360 unsigned int x
= 0, width
= min(srcwidth
, dstwidth
);
1361 int y
= lines
- 1, c
, length
;
1362 const BYTE
*begin
= bits
;
1367 if (length
) { /* encoded */
1370 if (x
>= (left
+ width
)) break;
1371 if( x
>= left
) XPutPixel(bmpImage
, x
, y
, colors
[c
>> 4]);
1373 if (!length
--) break;
1374 if (x
>= (left
+ width
)) break;
1375 if( x
>= left
) XPutPixel(bmpImage
, x
, y
, colors
[c
& 0xf]);
1395 default: /* absolute */
1398 if (x
>= left
&& x
< (left
+ width
))
1399 XPutPixel(bmpImage
, x
, y
, colors
[c
>> 4]);
1401 if (!length
--) break;
1402 if (x
>= left
&& x
< (left
+ width
))
1403 XPutPixel(bmpImage
, x
, y
, colors
[c
& 0xf]);
1406 if ((bits
- begin
) & 1)
1415 /***********************************************************************
1416 * X11DRV_DIB_SetImageBits_8
1418 * SetDIBits for an 8-bit deep DIB.
1420 static void X11DRV_DIB_SetImageBits_8( int lines
, const BYTE
*srcbits
,
1421 DWORD srcwidth
, DWORD dstwidth
, int left
,
1422 const int *colors
, XImage
*bmpImage
,
1426 int h
, width
= min(srcwidth
, dstwidth
);
1427 const BYTE
* srcbyte
;
1433 srcbits
= srcbits
+ linebytes
* (lines
-1);
1434 linebytes
= -linebytes
;
1439 switch (bmpImage
->depth
) {
1442 /* Some X servers might have 32 bit/ 16bit deep pixel */
1443 if (lines
&& width
&& (bmpImage
->bits_per_pixel
== 16) &&
1444 (ImageByteOrder(gdi_display
)==LSBFirst
) )
1446 /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1447 dstbits
=(BYTE
*)bmpImage
->data
+left
*2+(lines
-1)*bmpImage
->bytes_per_line
;
1448 for (h
= lines
; h
--; ) {
1449 #if defined(__i386__) && defined(__GNUC__)
1450 int _cl1
,_cl2
; /* temp outputs for asm below */
1451 /* Borrowed from DirectDraw */
1452 __asm__
__volatile__(
1457 " movw (%%edx,%%eax,4),%%ax\n"
1459 " xor %%eax,%%eax\n"
1461 :"=S" (srcbyte
), "=D" (_cl1
), "=c" (_cl2
)
1466 :"eax", "cc", "memory"
1469 DWORD
* dstpixel
=(DWORD
*)dstbits
;
1470 for (x
=0; x
<width
/2; x
++) {
1471 /* Do 2 pixels at a time */
1472 *dstpixel
++=(colors
[srcbyte
[1]] << 16) | colors
[srcbyte
[0]];
1476 /* And then the odd pixel */
1477 *((WORD
*)dstpixel
)=colors
[srcbyte
[0]];
1480 srcbyte
= (srcbits
+= linebytes
);
1481 dstbits
-= bmpImage
->bytes_per_line
;
1488 if (lines
&& width
&& (bmpImage
->bits_per_pixel
== 32) &&
1489 (ImageByteOrder(gdi_display
)==LSBFirst
) )
1491 dstbits
=(BYTE
*)bmpImage
->data
+left
*4+(lines
-1)*bmpImage
->bytes_per_line
;
1492 /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1493 for (h
= lines
; h
--; ) {
1494 #if defined(__i386__) && defined(__GNUC__)
1495 int _cl1
,_cl2
; /* temp outputs for asm below */
1496 /* Borrowed from DirectDraw */
1497 __asm__
__volatile__(
1502 " movl (%%edx,%%eax,4),%%eax\n"
1504 " xor %%eax,%%eax\n"
1506 :"=S" (srcbyte
), "=D" (_cl1
), "=c" (_cl2
)
1511 :"eax", "cc", "memory"
1514 DWORD
* dstpixel
=(DWORD
*)dstbits
;
1515 for (x
=0; x
<width
; x
++) {
1516 *dstpixel
++=colors
[*srcbyte
++];
1519 srcbyte
= (srcbits
+= linebytes
);
1520 dstbits
-= bmpImage
->bytes_per_line
;
1526 break; /* use slow generic case below */
1529 /* ==== pal 8 dib -> any bmp format ==== */
1530 for (h
=lines
-1; h
>=0; h
--) {
1531 for (x
=left
; x
<width
+left
; x
++) {
1532 XPutPixel(bmpImage
, x
, h
, colors
[*srcbyte
++]);
1534 srcbyte
= (srcbits
+= linebytes
);
1538 /***********************************************************************
1539 * X11DRV_DIB_GetImageBits_8
1541 * GetDIBits for an 8-bit deep DIB.
1543 static void X11DRV_DIB_GetImageBits_8( int lines
, BYTE
*dstbits
,
1544 DWORD srcwidth
, DWORD dstwidth
,
1545 RGBQUAD
*colors
, PALETTEENTRY
*srccolors
,
1546 XImage
*bmpImage
, DWORD linebytes
)
1549 int h
, width
= min(srcwidth
, dstwidth
);
1555 dstbits
= dstbits
+ ( linebytes
* (lines
-1) );
1556 linebytes
= -linebytes
;
1561 * This condition is true when GetImageBits has been called by
1562 * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1563 * 256 colormaps, so we'll just use it for GetDIBits calls.
1564 * (In some cases, in an updateDIBSection, the returned colors are bad too)
1566 if (!srccolors
) goto updatesection
;
1568 switch (bmpImage
->depth
) {
1571 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
1574 /* ==== pal 1 bmp -> pal 8 dib ==== */
1575 /* ==== pal 4 bmp -> pal 8 dib ==== */
1576 for (h
=lines
-1; h
>=0; h
--) {
1578 for (x
=0; x
<width
; x
++) {
1579 PALETTEENTRY srcval
;
1580 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
1581 *dstbyte
++=X11DRV_DIB_GetNearestIndex(colors
, 256,
1586 dstbits
+= linebytes
;
1594 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
1596 /* ==== pal 8 bmp -> pal 8 dib ==== */
1597 const void* srcbits
;
1598 const BYTE
* srcpixel
;
1600 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1601 for (h
=0; h
<lines
; h
++) {
1604 for (x
= 0; x
< width
; x
++) {
1605 PALETTEENTRY srcval
;
1606 srcval
=srccolors
[*srcpixel
++];
1607 *dstbyte
++=X11DRV_DIB_GetNearestIndex(colors
, 256,
1612 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1613 dstbits
+= linebytes
;
1623 const void* srcbits
;
1624 const WORD
* srcpixel
;
1627 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1629 if (bmpImage
->green_mask
==0x03e0) {
1630 if (bmpImage
->red_mask
==0x7c00) {
1631 /* ==== rgb 555 bmp -> pal 8 dib ==== */
1632 for (h
=0; h
<lines
; h
++) {
1635 for (x
=0; x
<width
; x
++) {
1638 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1640 ((srcval
>> 7) & 0xf8) | /* r */
1641 ((srcval
>> 12) & 0x07),
1642 ((srcval
>> 2) & 0xf8) | /* g */
1643 ((srcval
>> 7) & 0x07),
1644 ((srcval
<< 3) & 0xf8) | /* b */
1645 ((srcval
>> 2) & 0x07) );
1647 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1648 dstbits
+= linebytes
;
1650 } else if (bmpImage
->blue_mask
==0x7c00) {
1651 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1652 for (h
=0; h
<lines
; h
++) {
1655 for (x
=0; x
<width
; x
++) {
1658 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1660 ((srcval
<< 3) & 0xf8) | /* r */
1661 ((srcval
>> 2) & 0x07),
1662 ((srcval
>> 2) & 0xf8) | /* g */
1663 ((srcval
>> 7) & 0x07),
1664 ((srcval
>> 7) & 0xf8) | /* b */
1665 ((srcval
>> 12) & 0x07) );
1667 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1668 dstbits
+= linebytes
;
1673 } else if (bmpImage
->green_mask
==0x07e0) {
1674 if (bmpImage
->red_mask
==0xf800) {
1675 /* ==== rgb 565 bmp -> pal 8 dib ==== */
1676 for (h
=0; h
<lines
; h
++) {
1679 for (x
=0; x
<width
; x
++) {
1682 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1684 ((srcval
>> 8) & 0xf8) | /* r */
1685 ((srcval
>> 13) & 0x07),
1686 ((srcval
>> 3) & 0xfc) | /* g */
1687 ((srcval
>> 9) & 0x03),
1688 ((srcval
<< 3) & 0xf8) | /* b */
1689 ((srcval
>> 2) & 0x07) );
1691 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1692 dstbits
+= linebytes
;
1694 } else if (bmpImage
->blue_mask
==0xf800) {
1695 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1696 for (h
=0; h
<lines
; h
++) {
1699 for (x
=0; x
<width
; x
++) {
1702 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1704 ((srcval
<< 3) & 0xf8) | /* r */
1705 ((srcval
>> 2) & 0x07),
1706 ((srcval
>> 3) & 0xfc) | /* g */
1707 ((srcval
>> 9) & 0x03),
1708 ((srcval
>> 8) & 0xf8) | /* b */
1709 ((srcval
>> 13) & 0x07) );
1711 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1712 dstbits
+= linebytes
;
1726 const void* srcbits
;
1727 const BYTE
*srcbyte
;
1729 int bytes_per_pixel
;
1731 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1732 bytes_per_pixel
=(bmpImage
->bits_per_pixel
==24?3:4);
1734 if (bmpImage
->green_mask
!=0x00ff00 ||
1735 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
1737 } else if (bmpImage
->blue_mask
==0xff) {
1738 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1739 for (h
=0; h
<lines
; h
++) {
1742 for (x
=0; x
<width
; x
++) {
1743 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1748 srcbyte
+=bytes_per_pixel
;
1750 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1751 dstbits
+= linebytes
;
1754 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1755 for (h
=0; h
<lines
; h
++) {
1758 for (x
=0; x
<width
; x
++) {
1759 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1764 srcbyte
+=bytes_per_pixel
;
1766 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1767 dstbits
+= linebytes
;
1775 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1776 bmpImage
->depth
, bmpImage
->red_mask
,
1777 bmpImage
->green_mask
, bmpImage
->blue_mask
);
1779 /* ==== any bmp format -> pal 8 dib ==== */
1780 for (h
=lines
-1; h
>=0; h
--) {
1782 for (x
=0; x
<width
; x
++) {
1783 *dstbyte
=X11DRV_DIB_MapColor
1785 XGetPixel(bmpImage
, x
, h
), *dstbyte
);
1788 dstbits
+= linebytes
;
1794 /***********************************************************************
1795 * X11DRV_DIB_SetImageBits_RLE8
1797 * SetDIBits for an 8-bit deep compressed DIB.
1799 * This function rewritten 941113 by James Youngman. WINE blew out when I
1800 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
1802 * This was because the algorithm assumed that all RLE8 bitmaps end with the
1803 * 'End of bitmap' escape code. This code is very much laxer in what it
1804 * allows to end the expansion. Possibly too lax. See the note by
1805 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
1806 * bitmap should end with RleEnd, but on the other hand, software exists
1807 * that produces ones that don't and Windows 3.1 doesn't complain a bit
1810 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
1811 * James A. Youngman <mbcstjy@afs.man.ac.uk>
1814 static void X11DRV_DIB_SetImageBits_RLE8( int lines
, const BYTE
*bits
,
1815 DWORD srcwidth
, DWORD dstwidth
,
1816 int left
, int *colors
,
1819 unsigned int x
; /* X-position on each line. Increases. */
1820 int y
; /* Line #. Starts at lines-1, decreases */
1821 const BYTE
*pIn
= bits
; /* Pointer to current position in bits */
1822 BYTE length
; /* The length pf a run */
1823 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
1826 * Note that the bitmap data is stored by Windows starting at the
1827 * bottom line of the bitmap and going upwards. Within each line,
1828 * the data is stored left-to-right. That's the reason why line
1829 * goes from lines-1 to 0. [JAY]
1839 * If the length byte is not zero (which is the escape value),
1840 * We have a run of length pixels all the same colour. The colour
1841 * index is stored next.
1843 * If the length byte is zero, we need to read the next byte to
1844 * know what to do. [JAY]
1849 * [Run-Length] Encoded mode
1851 int color
= colors
[*pIn
++];
1852 while (length
-- && x
< (left
+ dstwidth
)) {
1853 if( x
>= left
) XPutPixel(bmpImage
, x
, y
, color
);
1860 * Escape codes (may be an absolute sequence though)
1862 escape_code
= (*pIn
++);
1871 /* Not all RLE8 bitmaps end with this code. For
1872 * example, Paint Shop Pro produces some that don't.
1873 * That's (I think) what caused the previous
1874 * implementation to fail. [JAY]
1883 default: /* switch to absolute mode */
1884 length
= escape_code
;
1887 int color
= colors
[*pIn
++];
1888 if (x
>= (left
+ dstwidth
))
1893 if( x
>= left
) XPutPixel(bmpImage
, x
, y
, color
);
1897 * If you think for a moment you'll realise that the
1898 * only time we could ever possibly read an odd
1899 * number of bytes is when there is a 0x00 (escape),
1900 * a value >0x02 (absolute mode) and then an odd-
1901 * length run. Therefore this is the only place we
1902 * need to worry about it. Everywhere else the
1903 * bytes are always read in pairs. [JAY]
1905 if (escape_code
& 1) pIn
++; /* Throw away the pad byte. */
1907 } /* switch (escape_code) : Escape sequence */
1913 /***********************************************************************
1914 * X11DRV_DIB_SetImageBits_16
1916 * SetDIBits for a 16-bit deep DIB.
1918 static void X11DRV_DIB_SetImageBits_16( int lines
, const BYTE
*srcbits
,
1919 DWORD srcwidth
, DWORD dstwidth
, int left
,
1920 X11DRV_PDEVICE
*physDev
, DWORD rSrc
, DWORD gSrc
, DWORD bSrc
,
1921 XImage
*bmpImage
, DWORD linebytes
)
1924 int h
, width
= min(srcwidth
, dstwidth
);
1925 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_dst_byteswap
;
1930 srcbits
= srcbits
+ ( linebytes
* (lines
-1));
1931 linebytes
= -linebytes
;
1934 switch (bmpImage
->depth
)
1941 srcbits
=srcbits
+left
*2;
1942 dstbits
=bmpImage
->data
+left
*2+(lines
-1)*bmpImage
->bytes_per_line
;
1944 if (bmpImage
->green_mask
==0x03e0) {
1945 if (gSrc
==bmpImage
->green_mask
) {
1946 if (rSrc
==bmpImage
->red_mask
) {
1947 /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1948 /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1949 convs
->Convert_5x5_asis
1952 dstbits
,-bmpImage
->bytes_per_line
);
1953 } else if (rSrc
==bmpImage
->blue_mask
) {
1954 /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1955 /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1956 convs
->Convert_555_reverse
1959 dstbits
,-bmpImage
->bytes_per_line
);
1962 if (rSrc
==bmpImage
->red_mask
|| bSrc
==bmpImage
->blue_mask
) {
1963 /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1964 /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1965 convs
->Convert_565_to_555_asis
1968 dstbits
,-bmpImage
->bytes_per_line
);
1970 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1971 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1972 convs
->Convert_565_to_555_reverse
1975 dstbits
,-bmpImage
->bytes_per_line
);
1978 } else if (bmpImage
->green_mask
==0x07e0) {
1979 if (gSrc
==bmpImage
->green_mask
) {
1980 if (rSrc
==bmpImage
->red_mask
) {
1981 /* ==== rgb 565 dib -> rgb 565 bmp ==== */
1982 /* ==== bgr 565 dib -> bgr 565 bmp ==== */
1983 convs
->Convert_5x5_asis
1986 dstbits
,-bmpImage
->bytes_per_line
);
1988 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
1989 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
1990 convs
->Convert_565_reverse
1993 dstbits
,-bmpImage
->bytes_per_line
);
1996 if (rSrc
==bmpImage
->red_mask
|| bSrc
==bmpImage
->blue_mask
) {
1997 /* ==== rgb 555 dib -> rgb 565 bmp ==== */
1998 /* ==== bgr 555 dib -> bgr 565 bmp ==== */
1999 convs
->Convert_555_to_565_asis
2002 dstbits
,-bmpImage
->bytes_per_line
);
2004 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
2005 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
2006 convs
->Convert_555_to_565_reverse
2009 dstbits
,-bmpImage
->bytes_per_line
);
2019 if (bmpImage
->bits_per_pixel
==24) {
2022 srcbits
=srcbits
+left
*2;
2023 dstbits
=bmpImage
->data
+left
*3+(lines
-1)*bmpImage
->bytes_per_line
;
2025 if (bmpImage
->green_mask
!=0x00ff00 ||
2026 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2028 } else if ((rSrc
==0x1f && bmpImage
->red_mask
==0xff) ||
2029 (bSrc
==0x1f && bmpImage
->blue_mask
==0xff)) {
2031 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
2032 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
2033 convs
->Convert_555_to_888_asis
2036 dstbits
,-bmpImage
->bytes_per_line
);
2038 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
2039 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
2040 convs
->Convert_565_to_888_asis
2043 dstbits
,-bmpImage
->bytes_per_line
);
2047 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
2048 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
2049 convs
->Convert_555_to_888_reverse
2052 dstbits
,-bmpImage
->bytes_per_line
);
2054 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
2055 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
2056 convs
->Convert_565_to_888_reverse
2059 dstbits
,-bmpImage
->bytes_per_line
);
2070 srcbits
=srcbits
+left
*2;
2071 dstbits
=bmpImage
->data
+left
*4+(lines
-1)*bmpImage
->bytes_per_line
;
2073 if (bmpImage
->green_mask
!=0x00ff00 ||
2074 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2076 } else if ((rSrc
==0x1f && bmpImage
->red_mask
==0xff) ||
2077 (bSrc
==0x1f && bmpImage
->blue_mask
==0xff)) {
2079 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
2080 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
2081 convs
->Convert_555_to_0888_asis
2084 dstbits
,-bmpImage
->bytes_per_line
);
2086 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
2087 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
2088 convs
->Convert_565_to_0888_asis
2091 dstbits
,-bmpImage
->bytes_per_line
);
2095 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
2096 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
2097 convs
->Convert_555_to_0888_reverse
2100 dstbits
,-bmpImage
->bytes_per_line
);
2102 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
2103 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
2104 convs
->Convert_565_to_0888_reverse
2107 dstbits
,-bmpImage
->bytes_per_line
);
2115 WARN("from 16 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2116 rSrc
, gSrc
, bSrc
, bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
2117 bmpImage
->green_mask
, bmpImage
->blue_mask
);
2123 /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
2124 const WORD
* srcpixel
;
2125 int rShift1
,gShift1
,bShift1
;
2126 int rShift2
,gShift2
,bShift2
;
2129 /* Set color scaling values */
2130 rShift1
=16+X11DRV_DIB_MaskToShift(rSrc
)-3;
2131 gShift1
=16+X11DRV_DIB_MaskToShift(gSrc
)-3;
2132 bShift1
=16+X11DRV_DIB_MaskToShift(bSrc
)-3;
2137 /* Green has 5 bits, like the others */
2141 /* Green has 6 bits, not 5. Compensate. */
2150 /* We could split it into four separate cases to optimize
2151 * but it is probably not worth it.
2153 for (h
=lines
-1; h
>=0; h
--) {
2154 srcpixel
=(const WORD
*)srcbits
;
2155 for (x
=left
; x
<width
+left
; x
++) {
2157 BYTE red
,green
,blue
;
2158 srcval
=*srcpixel
++ << 16;
2159 red
= ((srcval
>> rShift1
) & 0xf8) |
2160 ((srcval
>> rShift2
) & 0x07);
2161 green
=((srcval
>> gShift1
) & gMask1
) |
2162 ((srcval
>> gShift2
) & gMask2
);
2163 blue
= ((srcval
>> bShift1
) & 0xf8) |
2164 ((srcval
>> bShift2
) & 0x07);
2165 XPutPixel(bmpImage
, x
, h
,
2166 X11DRV_PALETTE_ToPhysical
2167 (physDev
, RGB(red
,green
,blue
)));
2169 srcbits
+= linebytes
;
2177 /***********************************************************************
2178 * X11DRV_DIB_GetImageBits_16
2180 * GetDIBits for an 16-bit deep DIB.
2182 static void X11DRV_DIB_GetImageBits_16( int lines
, BYTE
*dstbits
,
2183 DWORD dstwidth
, DWORD srcwidth
,
2184 PALETTEENTRY
*srccolors
,
2185 DWORD rDst
, DWORD gDst
, DWORD bDst
,
2186 XImage
*bmpImage
, DWORD dibpitch
)
2189 int h
, width
= min(srcwidth
, dstwidth
);
2190 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_src_byteswap
;
2192 DWORD linebytes
= dibpitch
;
2197 dstbits
= dstbits
+ ( linebytes
* (lines
-1));
2198 linebytes
= -linebytes
;
2201 switch (bmpImage
->depth
)
2206 const char* srcbits
;
2208 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2210 if (bmpImage
->green_mask
==0x03e0) {
2211 if (gDst
==bmpImage
->green_mask
) {
2212 if (rDst
==bmpImage
->red_mask
) {
2213 /* ==== rgb 555 bmp -> rgb 555 dib ==== */
2214 /* ==== bgr 555 bmp -> bgr 555 dib ==== */
2215 convs
->Convert_5x5_asis
2217 srcbits
,-bmpImage
->bytes_per_line
,
2220 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
2221 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
2222 convs
->Convert_555_reverse
2224 srcbits
,-bmpImage
->bytes_per_line
,
2228 if (rDst
==bmpImage
->red_mask
|| bDst
==bmpImage
->blue_mask
) {
2229 /* ==== rgb 555 bmp -> rgb 565 dib ==== */
2230 /* ==== bgr 555 bmp -> bgr 565 dib ==== */
2231 convs
->Convert_555_to_565_asis
2233 srcbits
,-bmpImage
->bytes_per_line
,
2236 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
2237 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
2238 convs
->Convert_555_to_565_reverse
2240 srcbits
,-bmpImage
->bytes_per_line
,
2244 } else if (bmpImage
->green_mask
==0x07e0) {
2245 if (gDst
==bmpImage
->green_mask
) {
2246 if (rDst
== bmpImage
->red_mask
) {
2247 /* ==== rgb 565 bmp -> rgb 565 dib ==== */
2248 /* ==== bgr 565 bmp -> bgr 565 dib ==== */
2249 convs
->Convert_5x5_asis
2251 srcbits
,-bmpImage
->bytes_per_line
,
2254 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
2255 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
2256 convs
->Convert_565_reverse
2258 srcbits
,-bmpImage
->bytes_per_line
,
2262 if (rDst
==bmpImage
->red_mask
|| bDst
==bmpImage
->blue_mask
) {
2263 /* ==== rgb 565 bmp -> rgb 555 dib ==== */
2264 /* ==== bgr 565 bmp -> bgr 555 dib ==== */
2265 convs
->Convert_565_to_555_asis
2267 srcbits
,-bmpImage
->bytes_per_line
,
2270 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
2271 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
2272 convs
->Convert_565_to_555_reverse
2274 srcbits
,-bmpImage
->bytes_per_line
,
2285 if (bmpImage
->bits_per_pixel
== 24) {
2286 const char* srcbits
;
2288 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2290 if (bmpImage
->green_mask
!=0x00ff00 ||
2291 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2293 } else if ((rDst
==0x1f && bmpImage
->red_mask
==0xff) ||
2294 (bDst
==0x1f && bmpImage
->blue_mask
==0xff)) {
2296 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
2297 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
2298 convs
->Convert_888_to_555_asis
2300 srcbits
,-bmpImage
->bytes_per_line
,
2303 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2304 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2305 convs
->Convert_888_to_565_asis
2307 srcbits
,-bmpImage
->bytes_per_line
,
2312 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
2313 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
2314 convs
->Convert_888_to_555_reverse
2316 srcbits
,-bmpImage
->bytes_per_line
,
2319 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
2320 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
2321 convs
->Convert_888_to_565_reverse
2323 srcbits
,-bmpImage
->bytes_per_line
,
2333 const char* srcbits
;
2335 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2337 if (bmpImage
->green_mask
!=0x00ff00 ||
2338 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2340 } else if ((rDst
==0x1f && bmpImage
->red_mask
==0xff) ||
2341 (bDst
==0x1f && bmpImage
->blue_mask
==0xff)) {
2343 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2344 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2345 convs
->Convert_0888_to_555_asis
2347 srcbits
,-bmpImage
->bytes_per_line
,
2350 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2351 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2352 convs
->Convert_0888_to_565_asis
2354 srcbits
,-bmpImage
->bytes_per_line
,
2359 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2360 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2361 convs
->Convert_0888_to_555_reverse
2363 srcbits
,-bmpImage
->bytes_per_line
,
2366 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2367 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2368 convs
->Convert_0888_to_565_reverse
2370 srcbits
,-bmpImage
->bytes_per_line
,
2379 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
2381 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2382 int rShift
,gShift
,bShift
;
2385 /* Shift everything 16 bits left so that all shifts are >0,
2386 * even for BGR DIBs. Then a single >> 16 will bring everything
2389 rShift
=16+X11DRV_DIB_MaskToShift(rDst
)-3;
2390 gShift
=16+X11DRV_DIB_MaskToShift(gDst
)-3;
2391 bShift
=16+X11DRV_DIB_MaskToShift(bDst
)-3;
2393 /* 6 bits for the green */
2399 for (h
= lines
- 1; h
>= 0; h
--) {
2400 dstpixel
=(LPWORD
)dstbits
;
2401 for (x
= 0; x
< width
; x
++) {
2402 PALETTEENTRY srcval
;
2404 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
2405 dstval
=((srcval
.peRed
<< rShift
) & rDst
) |
2406 ((srcval
.peGreen
<< gShift
) & gDst
) |
2407 ((srcval
.peBlue
<< bShift
) & bDst
);
2408 *dstpixel
++=dstval
>> 16;
2410 dstbits
+= linebytes
;
2418 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
2420 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2421 int rShift
,gShift
,bShift
;
2422 const BYTE
* srcbits
;
2423 const BYTE
* srcpixel
;
2426 /* Shift everything 16 bits left so that all shifts are >0,
2427 * even for BGR DIBs. Then a single >> 16 will bring everything
2430 rShift
=16+X11DRV_DIB_MaskToShift(rDst
)-3;
2431 gShift
=16+X11DRV_DIB_MaskToShift(gDst
)-3;
2432 bShift
=16+X11DRV_DIB_MaskToShift(bDst
)-3;
2434 /* 6 bits for the green */
2440 srcbits
=(BYTE
*)bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2441 for (h
=0; h
<lines
; h
++) {
2443 dstpixel
=(LPWORD
)dstbits
;
2444 for (x
= 0; x
< width
; x
++) {
2445 PALETTEENTRY srcval
;
2447 srcval
=srccolors
[*srcpixel
++];
2448 dstval
=((srcval
.peRed
<< rShift
) & rDst
) |
2449 ((srcval
.peGreen
<< gShift
) & gDst
) |
2450 ((srcval
.peBlue
<< bShift
) & bDst
);
2451 *dstpixel
++=dstval
>> 16;
2453 srcbits
-= bmpImage
->bytes_per_line
;
2454 dstbits
+= linebytes
;
2464 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2465 int rShift
,gShift
,bShift
;
2468 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%x,%x,%x)\n",
2469 bmpImage
->depth
, bmpImage
->red_mask
,
2470 bmpImage
->green_mask
, bmpImage
->blue_mask
,
2473 /* Shift everything 16 bits left so that all shifts are >0,
2474 * even for BGR DIBs. Then a single >> 16 will bring everything
2477 rShift
=16+X11DRV_DIB_MaskToShift(rDst
)-3;
2478 gShift
=16+X11DRV_DIB_MaskToShift(gDst
)-3;
2479 bShift
=16+X11DRV_DIB_MaskToShift(bDst
)-3;
2481 /* 6 bits for the green */
2487 for (h
= lines
- 1; h
>= 0; h
--) {
2488 dstpixel
=(LPWORD
)dstbits
;
2489 for (x
= 0; x
< width
; x
++) {
2492 srcval
=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage
, x
, h
));
2493 dstval
=((GetRValue(srcval
) << rShift
) & rDst
) |
2494 ((GetGValue(srcval
) << gShift
) & gDst
) |
2495 ((GetBValue(srcval
) << bShift
) & bDst
);
2496 *dstpixel
++=dstval
>> 16;
2498 dstbits
+= linebytes
;
2506 /***********************************************************************
2507 * X11DRV_DIB_SetImageBits_24
2509 * SetDIBits for a 24-bit deep DIB.
2511 static void X11DRV_DIB_SetImageBits_24( int lines
, const BYTE
*srcbits
,
2512 DWORD srcwidth
, DWORD dstwidth
, int left
,
2513 X11DRV_PDEVICE
*physDev
,
2514 DWORD rSrc
, DWORD gSrc
, DWORD bSrc
,
2515 XImage
*bmpImage
, DWORD linebytes
)
2518 int h
, width
= min(srcwidth
, dstwidth
);
2519 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_dst_byteswap
;
2524 srcbits
= srcbits
+ linebytes
* (lines
- 1);
2525 linebytes
= -linebytes
;
2528 switch (bmpImage
->depth
)
2531 if (bmpImage
->bits_per_pixel
==24) {
2534 srcbits
=srcbits
+left
*3;
2535 dstbits
=bmpImage
->data
+left
*3+(lines
-1)*bmpImage
->bytes_per_line
;
2537 if (bmpImage
->green_mask
!=0x00ff00 ||
2538 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2540 } else if (rSrc
==bmpImage
->red_mask
) {
2541 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2542 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2543 convs
->Convert_888_asis
2546 dstbits
,-bmpImage
->bytes_per_line
);
2548 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2549 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2550 convs
->Convert_888_reverse
2553 dstbits
,-bmpImage
->bytes_per_line
);
2563 srcbits
=srcbits
+left
*3;
2564 dstbits
=bmpImage
->data
+left
*4+(lines
-1)*bmpImage
->bytes_per_line
;
2566 if (bmpImage
->green_mask
!=0x00ff00 ||
2567 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2569 } else if (rSrc
==bmpImage
->red_mask
) {
2570 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2571 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2572 convs
->Convert_888_to_0888_asis
2575 dstbits
,-bmpImage
->bytes_per_line
);
2577 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2578 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2579 convs
->Convert_888_to_0888_reverse
2582 dstbits
,-bmpImage
->bytes_per_line
);
2592 srcbits
=srcbits
+left
*3;
2593 dstbits
=bmpImage
->data
+left
*2+(lines
-1)*bmpImage
->bytes_per_line
;
2595 if (bmpImage
->green_mask
==0x03e0) {
2596 if ((rSrc
==0xff0000 && bmpImage
->red_mask
==0x7f00) ||
2597 (bSrc
==0xff0000 && bmpImage
->blue_mask
==0x7f00)) {
2598 /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2599 /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2600 convs
->Convert_888_to_555_asis
2603 dstbits
,-bmpImage
->bytes_per_line
);
2604 } else if ((rSrc
==0xff && bmpImage
->red_mask
==0x7f00) ||
2605 (bSrc
==0xff && bmpImage
->blue_mask
==0x7f00)) {
2606 /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2607 /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2608 convs
->Convert_888_to_555_reverse
2611 dstbits
,-bmpImage
->bytes_per_line
);
2615 } else if (bmpImage
->green_mask
==0x07e0) {
2616 if ((rSrc
==0xff0000 && bmpImage
->red_mask
==0xf800) ||
2617 (bSrc
==0xff0000 && bmpImage
->blue_mask
==0xf800)) {
2618 /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2619 /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2620 convs
->Convert_888_to_565_asis
2623 dstbits
,-bmpImage
->bytes_per_line
);
2624 } else if ((rSrc
==0xff && bmpImage
->red_mask
==0xf800) ||
2625 (bSrc
==0xff && bmpImage
->blue_mask
==0xf800)) {
2626 /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2627 /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2628 convs
->Convert_888_to_565_reverse
2631 dstbits
,-bmpImage
->bytes_per_line
);
2643 WARN("from 24 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2644 rSrc
, gSrc
, bSrc
, bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
2645 bmpImage
->green_mask
, bmpImage
->blue_mask
);
2651 /* ==== rgb 888 dib -> any bmp format ==== */
2652 const BYTE
* srcbyte
;
2654 /* Windows only supports one 24bpp DIB format: RGB888 */
2656 for (h
= lines
- 1; h
>= 0; h
--) {
2658 for (x
= left
; x
< width
+left
; x
++) {
2659 XPutPixel(bmpImage
, x
, h
,
2660 X11DRV_PALETTE_ToPhysical
2661 (physDev
, RGB(srcbyte
[2], srcbyte
[1], srcbyte
[0])));
2664 srcbits
+= linebytes
;
2672 /***********************************************************************
2673 * X11DRV_DIB_GetImageBits_24
2675 * GetDIBits for an 24-bit deep DIB.
2677 static void X11DRV_DIB_GetImageBits_24( int lines
, BYTE
*dstbits
,
2678 DWORD dstwidth
, DWORD srcwidth
,
2679 PALETTEENTRY
*srccolors
,
2680 DWORD rDst
, DWORD gDst
, DWORD bDst
,
2681 XImage
*bmpImage
, DWORD linebytes
)
2684 int h
, width
= min(srcwidth
, dstwidth
);
2685 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_src_byteswap
;
2690 dstbits
= dstbits
+ ( linebytes
* (lines
-1) );
2691 linebytes
= -linebytes
;
2694 switch (bmpImage
->depth
)
2697 if (bmpImage
->bits_per_pixel
==24) {
2698 const char* srcbits
;
2700 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2702 if (bmpImage
->green_mask
!=0x00ff00 ||
2703 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2705 } else if (rDst
==bmpImage
->red_mask
) {
2706 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2707 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2708 convs
->Convert_888_asis
2710 srcbits
,-bmpImage
->bytes_per_line
,
2713 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2714 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2715 convs
->Convert_888_reverse
2717 srcbits
,-bmpImage
->bytes_per_line
,
2726 const char* srcbits
;
2728 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2730 if (bmpImage
->green_mask
!=0x00ff00 ||
2731 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2733 } else if (rDst
==bmpImage
->red_mask
) {
2734 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2735 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2736 convs
->Convert_0888_to_888_asis
2738 srcbits
,-bmpImage
->bytes_per_line
,
2741 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2742 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2743 convs
->Convert_0888_to_888_reverse
2745 srcbits
,-bmpImage
->bytes_per_line
,
2754 const char* srcbits
;
2756 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2758 if (bmpImage
->green_mask
==0x03e0) {
2759 if ((rDst
==0xff0000 && bmpImage
->red_mask
==0x7f00) ||
2760 (bDst
==0xff0000 && bmpImage
->blue_mask
==0x7f00)) {
2761 /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2762 /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2763 convs
->Convert_555_to_888_asis
2765 srcbits
,-bmpImage
->bytes_per_line
,
2767 } else if ((rDst
==0xff && bmpImage
->red_mask
==0x7f00) ||
2768 (bDst
==0xff && bmpImage
->blue_mask
==0x7f00)) {
2769 /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2770 /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2771 convs
->Convert_555_to_888_reverse
2773 srcbits
,-bmpImage
->bytes_per_line
,
2778 } else if (bmpImage
->green_mask
==0x07e0) {
2779 if ((rDst
==0xff0000 && bmpImage
->red_mask
==0xf800) ||
2780 (bDst
==0xff0000 && bmpImage
->blue_mask
==0xf800)) {
2781 /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2782 /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2783 convs
->Convert_565_to_888_asis
2785 srcbits
,-bmpImage
->bytes_per_line
,
2787 } else if ((rDst
==0xff && bmpImage
->red_mask
==0xf800) ||
2788 (bDst
==0xff && bmpImage
->blue_mask
==0xf800)) {
2789 /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2790 /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2791 convs
->Convert_565_to_888_reverse
2793 srcbits
,-bmpImage
->bytes_per_line
,
2806 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
2808 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2811 /* Windows only supports one 24bpp DIB format: rgb 888 */
2812 for (h
= lines
- 1; h
>= 0; h
--) {
2814 for (x
= 0; x
< width
; x
++) {
2815 PALETTEENTRY srcval
;
2816 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
2817 dstbyte
[0]=srcval
.peBlue
;
2818 dstbyte
[1]=srcval
.peGreen
;
2819 dstbyte
[2]=srcval
.peRed
;
2822 dstbits
+= linebytes
;
2830 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
2832 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2833 const void* srcbits
;
2834 const BYTE
* srcpixel
;
2837 /* Windows only supports one 24bpp DIB format: rgb 888 */
2838 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2839 for (h
= lines
- 1; h
>= 0; h
--) {
2842 for (x
= 0; x
< width
; x
++ ) {
2843 PALETTEENTRY srcval
;
2844 srcval
=srccolors
[*srcpixel
++];
2845 dstbyte
[0]=srcval
.peBlue
;
2846 dstbyte
[1]=srcval
.peGreen
;
2847 dstbyte
[2]=srcval
.peRed
;
2850 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
2851 dstbits
+= linebytes
;
2861 /* ==== any bmp format -> 888 dib ==== */
2864 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%x,%x,%x)\n",
2865 bmpImage
->depth
, bmpImage
->red_mask
,
2866 bmpImage
->green_mask
, bmpImage
->blue_mask
,
2869 /* Windows only supports one 24bpp DIB format: rgb 888 */
2870 for (h
= lines
- 1; h
>= 0; h
--) {
2872 for (x
= 0; x
< width
; x
++) {
2873 COLORREF srcval
=X11DRV_PALETTE_ToLogical
2874 (XGetPixel( bmpImage
, x
, h
));
2875 dstbyte
[0]=GetBValue(srcval
);
2876 dstbyte
[1]=GetGValue(srcval
);
2877 dstbyte
[2]=GetRValue(srcval
);
2880 dstbits
+= linebytes
;
2888 /***********************************************************************
2889 * X11DRV_DIB_SetImageBits_32
2891 * SetDIBits for a 32-bit deep DIB.
2893 static void X11DRV_DIB_SetImageBits_32(int lines
, const BYTE
*srcbits
,
2894 DWORD srcwidth
, DWORD dstwidth
, int left
,
2895 X11DRV_PDEVICE
*physDev
,
2896 DWORD rSrc
, DWORD gSrc
, DWORD bSrc
,
2902 int h
, width
= min(srcwidth
, dstwidth
);
2903 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_dst_byteswap
;
2908 srcbits
= srcbits
+ ( linebytes
* (lines
-1) );
2909 linebytes
= -linebytes
;
2912 ptr
= (const DWORD
*) srcbits
+ left
;
2914 switch (bmpImage
->depth
)
2917 if (bmpImage
->bits_per_pixel
==24) {
2920 srcbits
=srcbits
+left
*4;
2921 dstbits
=bmpImage
->data
+left
*3+(lines
-1)*bmpImage
->bytes_per_line
;
2923 if (rSrc
==bmpImage
->red_mask
&& gSrc
==bmpImage
->green_mask
&& bSrc
==bmpImage
->blue_mask
) {
2924 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2925 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2926 convs
->Convert_0888_to_888_asis
2929 dstbits
,-bmpImage
->bytes_per_line
);
2930 } else if (bmpImage
->green_mask
!=0x00ff00 ||
2931 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2933 /* the tests below assume sane bmpImage masks */
2934 } else if (rSrc
==bmpImage
->blue_mask
&& gSrc
==bmpImage
->green_mask
&& bSrc
==bmpImage
->red_mask
) {
2935 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2936 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2937 convs
->Convert_0888_to_888_reverse
2940 dstbits
,-bmpImage
->bytes_per_line
);
2941 } else if (bmpImage
->blue_mask
==0xff) {
2942 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2943 convs
->Convert_any0888_to_rgb888
2947 dstbits
,-bmpImage
->bytes_per_line
);
2949 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2950 convs
->Convert_any0888_to_bgr888
2954 dstbits
,-bmpImage
->bytes_per_line
);
2964 srcbits
=srcbits
+left
*4;
2965 dstbits
=bmpImage
->data
+left
*4+(lines
-1)*bmpImage
->bytes_per_line
;
2967 if (gSrc
==bmpImage
->green_mask
) {
2968 if (rSrc
==bmpImage
->red_mask
&& bSrc
==bmpImage
->blue_mask
) {
2969 /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2970 /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2971 convs
->Convert_0888_asis
2974 dstbits
,-bmpImage
->bytes_per_line
);
2975 } else if (bmpImage
->green_mask
!=0x00ff00 ||
2976 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2978 /* the tests below assume sane bmpImage masks */
2979 } else if (rSrc
==bmpImage
->blue_mask
&& bSrc
==bmpImage
->red_mask
) {
2980 /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2981 /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2982 convs
->Convert_0888_reverse
2985 dstbits
,-bmpImage
->bytes_per_line
);
2987 /* ==== any 0888 dib -> any 0888 bmp ==== */
2988 convs
->Convert_0888_any
2992 dstbits
,-bmpImage
->bytes_per_line
,
2993 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
2995 } else if (bmpImage
->green_mask
!=0x00ff00 ||
2996 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2998 /* the tests below assume sane bmpImage masks */
3000 /* ==== any 0888 dib -> any 0888 bmp ==== */
3001 convs
->Convert_0888_any
3005 dstbits
,-bmpImage
->bytes_per_line
,
3006 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3016 srcbits
=srcbits
+left
*4;
3017 dstbits
=bmpImage
->data
+left
*2+(lines
-1)*bmpImage
->bytes_per_line
;
3019 if (rSrc
==0xff0000 && gSrc
==0x00ff00 && bSrc
==0x0000ff) {
3020 if (bmpImage
->green_mask
==0x03e0) {
3021 if (bmpImage
->red_mask
==0x7f00) {
3022 /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
3023 convs
->Convert_0888_to_555_asis
3026 dstbits
,-bmpImage
->bytes_per_line
);
3027 } else if (bmpImage
->blue_mask
==0x7f00) {
3028 /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
3029 convs
->Convert_0888_to_555_reverse
3032 dstbits
,-bmpImage
->bytes_per_line
);
3036 } else if (bmpImage
->green_mask
==0x07e0) {
3037 if (bmpImage
->red_mask
==0xf800) {
3038 /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
3039 convs
->Convert_0888_to_565_asis
3042 dstbits
,-bmpImage
->bytes_per_line
);
3043 } else if (bmpImage
->blue_mask
==0xf800) {
3044 /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
3045 convs
->Convert_0888_to_565_reverse
3048 dstbits
,-bmpImage
->bytes_per_line
);
3055 } else if (rSrc
==0x0000ff && gSrc
==0x00ff00 && bSrc
==0xff0000) {
3056 if (bmpImage
->green_mask
==0x03e0) {
3057 if (bmpImage
->blue_mask
==0x7f00) {
3058 /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
3059 convs
->Convert_0888_to_555_asis
3062 dstbits
,-bmpImage
->bytes_per_line
);
3063 } else if (bmpImage
->red_mask
==0x7f00) {
3064 /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
3065 convs
->Convert_0888_to_555_reverse
3068 dstbits
,-bmpImage
->bytes_per_line
);
3072 } else if (bmpImage
->green_mask
==0x07e0) {
3073 if (bmpImage
->blue_mask
==0xf800) {
3074 /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
3075 convs
->Convert_0888_to_565_asis
3078 dstbits
,-bmpImage
->bytes_per_line
);
3079 } else if (bmpImage
->red_mask
==0xf800) {
3080 /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
3081 convs
->Convert_0888_to_565_reverse
3084 dstbits
,-bmpImage
->bytes_per_line
);
3092 if (bmpImage
->green_mask
==0x03e0 &&
3093 (bmpImage
->red_mask
==0x7f00 ||
3094 bmpImage
->blue_mask
==0x7f00)) {
3095 /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
3096 convs
->Convert_any0888_to_5x5
3100 dstbits
,-bmpImage
->bytes_per_line
,
3101 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3102 } else if (bmpImage
->green_mask
==0x07e0 &&
3103 (bmpImage
->red_mask
==0xf800 ||
3104 bmpImage
->blue_mask
==0xf800)) {
3105 /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
3106 convs
->Convert_any0888_to_5x5
3110 dstbits
,-bmpImage
->bytes_per_line
,
3111 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3121 WARN("from 32 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
3122 rSrc
, gSrc
, bSrc
, bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
3123 bmpImage
->green_mask
, bmpImage
->blue_mask
);
3129 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
3130 const DWORD
* srcpixel
;
3131 int rShift
,gShift
,bShift
;
3133 rShift
=X11DRV_DIB_MaskToShift(rSrc
);
3134 gShift
=X11DRV_DIB_MaskToShift(gSrc
);
3135 bShift
=X11DRV_DIB_MaskToShift(bSrc
);
3137 for (h
= lines
- 1; h
>= 0; h
--) {
3138 srcpixel
=(const DWORD
*)srcbits
;
3139 for (x
= left
; x
< width
+left
; x
++) {
3141 BYTE red
,green
,blue
;
3142 srcvalue
=*srcpixel
++;
3143 red
= (srcvalue
>> rShift
) & 0xff;
3144 green
=(srcvalue
>> gShift
) & 0xff;
3145 blue
= (srcvalue
>> bShift
) & 0xff;
3146 XPutPixel(bmpImage
, x
, h
, X11DRV_PALETTE_ToPhysical
3147 (physDev
, RGB(red
,green
,blue
)));
3149 srcbits
+= linebytes
;
3157 /***********************************************************************
3158 * X11DRV_DIB_GetImageBits_32
3160 * GetDIBits for an 32-bit deep DIB.
3162 static void X11DRV_DIB_GetImageBits_32( int lines
, BYTE
*dstbits
,
3163 DWORD dstwidth
, DWORD srcwidth
,
3164 PALETTEENTRY
*srccolors
,
3165 DWORD rDst
, DWORD gDst
, DWORD bDst
,
3166 XImage
*bmpImage
, DWORD linebytes
)
3169 int h
, width
= min(srcwidth
, dstwidth
);
3171 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_src_byteswap
;
3176 dstbits
= dstbits
+ ( linebytes
* (lines
-1) );
3177 linebytes
= -linebytes
;
3182 switch (bmpImage
->depth
)
3185 if (bmpImage
->bits_per_pixel
==24) {
3186 const void* srcbits
;
3188 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
3190 if (rDst
==bmpImage
->red_mask
&& gDst
==bmpImage
->green_mask
&& bDst
==bmpImage
->blue_mask
) {
3191 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
3192 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
3193 convs
->Convert_888_to_0888_asis
3195 srcbits
,-bmpImage
->bytes_per_line
,
3197 } else if (bmpImage
->green_mask
!=0x00ff00 ||
3198 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
3200 /* the tests below assume sane bmpImage masks */
3201 } else if (rDst
==bmpImage
->blue_mask
&& gDst
==bmpImage
->green_mask
&& bDst
==bmpImage
->red_mask
) {
3202 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
3203 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
3204 convs
->Convert_888_to_0888_reverse
3206 srcbits
,-bmpImage
->bytes_per_line
,
3208 } else if (bmpImage
->blue_mask
==0xff) {
3209 /* ==== rgb 888 bmp -> any 0888 dib ==== */
3210 convs
->Convert_rgb888_to_any0888
3212 srcbits
,-bmpImage
->bytes_per_line
,
3216 /* ==== bgr 888 bmp -> any 0888 dib ==== */
3217 convs
->Convert_bgr888_to_any0888
3219 srcbits
,-bmpImage
->bytes_per_line
,
3229 const char* srcbits
;
3231 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
3233 if (gDst
==bmpImage
->green_mask
) {
3234 if (rDst
==bmpImage
->red_mask
&& bDst
==bmpImage
->blue_mask
) {
3235 /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
3236 /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
3237 convs
->Convert_0888_asis
3239 srcbits
,-bmpImage
->bytes_per_line
,
3241 } else if (bmpImage
->green_mask
!=0x00ff00 ||
3242 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
3244 /* the tests below assume sane bmpImage masks */
3245 } else if (rDst
==bmpImage
->blue_mask
&& bDst
==bmpImage
->red_mask
) {
3246 /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
3247 /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
3248 convs
->Convert_0888_reverse
3250 srcbits
,-bmpImage
->bytes_per_line
,
3253 /* ==== any 0888 bmp -> any 0888 dib ==== */
3254 convs
->Convert_0888_any
3256 srcbits
,-bmpImage
->bytes_per_line
,
3257 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
,
3261 } else if (bmpImage
->green_mask
!=0x00ff00 ||
3262 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
3264 /* the tests below assume sane bmpImage masks */
3266 /* ==== any 0888 bmp -> any 0888 dib ==== */
3267 convs
->Convert_0888_any
3269 srcbits
,-bmpImage
->bytes_per_line
,
3270 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
,
3280 const char* srcbits
;
3282 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
3284 if (rDst
==0xff0000 && gDst
==0x00ff00 && bDst
==0x0000ff) {
3285 if (bmpImage
->green_mask
==0x03e0) {
3286 if (bmpImage
->red_mask
==0x7f00) {
3287 /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
3288 convs
->Convert_555_to_0888_asis
3290 srcbits
,-bmpImage
->bytes_per_line
,
3292 } else if (bmpImage
->blue_mask
==0x7f00) {
3293 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
3294 convs
->Convert_555_to_0888_reverse
3296 srcbits
,-bmpImage
->bytes_per_line
,
3301 } else if (bmpImage
->green_mask
==0x07e0) {
3302 if (bmpImage
->red_mask
==0xf800) {
3303 /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
3304 convs
->Convert_565_to_0888_asis
3306 srcbits
,-bmpImage
->bytes_per_line
,
3308 } else if (bmpImage
->blue_mask
==0xf800) {
3309 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
3310 convs
->Convert_565_to_0888_reverse
3312 srcbits
,-bmpImage
->bytes_per_line
,
3320 } else if (rDst
==0x0000ff && gDst
==0x00ff00 && bDst
==0xff0000) {
3321 if (bmpImage
->green_mask
==0x03e0) {
3322 if (bmpImage
->blue_mask
==0x7f00) {
3323 /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
3324 convs
->Convert_555_to_0888_asis
3326 srcbits
,-bmpImage
->bytes_per_line
,
3328 } else if (bmpImage
->red_mask
==0x7f00) {
3329 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
3330 convs
->Convert_555_to_0888_reverse
3332 srcbits
,-bmpImage
->bytes_per_line
,
3337 } else if (bmpImage
->green_mask
==0x07e0) {
3338 if (bmpImage
->blue_mask
==0xf800) {
3339 /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3340 convs
->Convert_565_to_0888_asis
3342 srcbits
,-bmpImage
->bytes_per_line
,
3344 } else if (bmpImage
->red_mask
==0xf800) {
3345 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3346 convs
->Convert_565_to_0888_reverse
3348 srcbits
,-bmpImage
->bytes_per_line
,
3357 if (bmpImage
->green_mask
==0x03e0 &&
3358 (bmpImage
->red_mask
==0x7f00 ||
3359 bmpImage
->blue_mask
==0x7f00)) {
3360 /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3361 convs
->Convert_5x5_to_any0888
3363 srcbits
,-bmpImage
->bytes_per_line
,
3364 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
,
3367 } else if (bmpImage
->green_mask
==0x07e0 &&
3368 (bmpImage
->red_mask
==0xf800 ||
3369 bmpImage
->blue_mask
==0xf800)) {
3370 /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3371 convs
->Convert_5x5_to_any0888
3373 srcbits
,-bmpImage
->bytes_per_line
,
3374 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
,
3386 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
3388 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3389 int rShift
,gShift
,bShift
;
3392 rShift
=X11DRV_DIB_MaskToShift(rDst
);
3393 gShift
=X11DRV_DIB_MaskToShift(gDst
);
3394 bShift
=X11DRV_DIB_MaskToShift(bDst
);
3395 for (h
= lines
- 1; h
>= 0; h
--) {
3396 dstpixel
=(DWORD
*)dstbits
;
3397 for (x
= 0; x
< width
; x
++) {
3398 PALETTEENTRY srcval
;
3399 srcval
= srccolors
[XGetPixel(bmpImage
, x
, h
)];
3400 *dstpixel
++=(srcval
.peRed
<< rShift
) |
3401 (srcval
.peGreen
<< gShift
) |
3402 (srcval
.peBlue
<< bShift
);
3404 dstbits
+= linebytes
;
3412 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
3414 /* ==== pal 8 bmp -> any 0888 dib ==== */
3415 int rShift
,gShift
,bShift
;
3416 const void* srcbits
;
3417 const BYTE
* srcpixel
;
3420 rShift
=X11DRV_DIB_MaskToShift(rDst
);
3421 gShift
=X11DRV_DIB_MaskToShift(gDst
);
3422 bShift
=X11DRV_DIB_MaskToShift(bDst
);
3423 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
3424 for (h
= lines
- 1; h
>= 0; h
--) {
3426 dstpixel
=(DWORD
*)dstbits
;
3427 for (x
= 0; x
< width
; x
++) {
3428 PALETTEENTRY srcval
;
3429 srcval
=srccolors
[*srcpixel
++];
3430 *dstpixel
++=(srcval
.peRed
<< rShift
) |
3431 (srcval
.peGreen
<< gShift
) |
3432 (srcval
.peBlue
<< bShift
);
3434 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
3435 dstbits
+= linebytes
;
3445 /* ==== any bmp format -> any 0888 dib ==== */
3446 int rShift
,gShift
,bShift
;
3449 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%x,%x,%x)\n",
3450 bmpImage
->depth
, bmpImage
->red_mask
,
3451 bmpImage
->green_mask
, bmpImage
->blue_mask
,
3454 rShift
=X11DRV_DIB_MaskToShift(rDst
);
3455 gShift
=X11DRV_DIB_MaskToShift(gDst
);
3456 bShift
=X11DRV_DIB_MaskToShift(bDst
);
3457 for (h
= lines
- 1; h
>= 0; h
--) {
3458 dstpixel
=(DWORD
*)dstbits
;
3459 for (x
= 0; x
< width
; x
++) {
3461 srcval
=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage
, x
, h
));
3462 *dstpixel
++=(GetRValue(srcval
) << rShift
) |
3463 (GetGValue(srcval
) << gShift
) |
3464 (GetBValue(srcval
) << bShift
);
3466 dstbits
+= linebytes
;
3473 static int XGetSubImageErrorHandler(Display
*dpy
, XErrorEvent
*event
, void *arg
)
3475 return (event
->request_code
== X_GetImage
&& event
->error_code
== BadMatch
);
3478 /***********************************************************************
3479 * X11DRV_DIB_SetImageBits_GetSubImage
3481 * Helper for X11DRV_DIB_SetImageBits
3483 static void X11DRV_DIB_SetImageBits_GetSubImage(
3484 const X11DRV_DIB_IMAGEBITS_DESCR
*descr
, XImage
*bmpImage
)
3486 /* compressed bitmaps may contain gaps in them. So make a copy
3487 * of the existing pixels first */
3490 SetRect( &bmprc
, descr
->xDest
, descr
->yDest
,
3491 descr
->xDest
+ descr
->width
, descr
->yDest
+ descr
->height
);
3492 GetRgnBox( descr
->physDev
->region
, &rc
);
3493 /* convert from dc to drawable origin */
3494 OffsetRect( &rc
, descr
->physDev
->dc_rect
.left
, descr
->physDev
->dc_rect
.top
);
3495 /* clip visible rect with bitmap */
3496 if( IntersectRect( &rc
, &rc
, &bmprc
))
3498 X11DRV_expect_error( gdi_display
, XGetSubImageErrorHandler
, NULL
);
3499 XGetSubImage( gdi_display
, descr
->drawable
, rc
.left
, rc
.top
,
3500 rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, AllPlanes
,
3502 descr
->xSrc
+ rc
.left
- bmprc
.left
,
3503 descr
->ySrc
+ rc
.top
- bmprc
.top
);
3504 X11DRV_check_error();
3508 /***********************************************************************
3509 * X11DRV_DIB_SetImageBits
3511 * Transfer the bits to an X image.
3512 * Helper function for SetDIBits() and SetDIBitsToDevice().
3514 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR
*descr
)
3516 int lines
= descr
->lines
>= 0 ? descr
->lines
: -descr
->lines
;
3521 bmpImage
= descr
->image
;
3523 bmpImage
= XCreateImage( gdi_display
, visual
, descr
->depth
, ZPixmap
, 0, NULL
,
3524 descr
->infoWidth
, lines
, 32, 0 );
3525 bmpImage
->data
= calloc( lines
, bmpImage
->bytes_per_line
);
3526 if(bmpImage
->data
== NULL
) {
3527 ERR("Out of memory!\n");
3528 XDestroyImage( bmpImage
);
3529 wine_tsx11_unlock();
3533 wine_tsx11_unlock();
3535 TRACE("Dib: depth=%d r=%x g=%x b=%x\n",
3536 descr
->infoBpp
,descr
->rMask
,descr
->gMask
,descr
->bMask
);
3537 TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3538 bmpImage
->depth
,bmpImage
->bits_per_pixel
,
3539 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3541 /* Transfer the pixels */
3542 switch(descr
->infoBpp
)
3545 X11DRV_DIB_SetImageBits_1( descr
->lines
, descr
->bits
, descr
->infoWidth
,
3546 descr
->width
, descr
->xSrc
, (int *)(descr
->colorMap
),
3547 bmpImage
, descr
->dibpitch
);
3550 if (descr
->compression
) {
3551 X11DRV_DIB_SetImageBits_GetSubImage( descr
, bmpImage
);
3552 X11DRV_DIB_SetImageBits_RLE4( descr
->lines
, descr
->bits
,
3553 descr
->infoWidth
, descr
->width
,
3554 descr
->xSrc
, (int *)(descr
->colorMap
),
3557 X11DRV_DIB_SetImageBits_4( descr
->lines
, descr
->bits
,
3558 descr
->infoWidth
, descr
->width
,
3559 descr
->xSrc
, (int*)(descr
->colorMap
),
3560 bmpImage
, descr
->dibpitch
);
3563 if (descr
->compression
) {
3564 X11DRV_DIB_SetImageBits_GetSubImage( descr
, bmpImage
);
3565 X11DRV_DIB_SetImageBits_RLE8( descr
->lines
, descr
->bits
,
3566 descr
->infoWidth
, descr
->width
,
3567 descr
->xSrc
, (int *)(descr
->colorMap
),
3570 X11DRV_DIB_SetImageBits_8( descr
->lines
, descr
->bits
,
3571 descr
->infoWidth
, descr
->width
,
3572 descr
->xSrc
, (int *)(descr
->colorMap
),
3573 bmpImage
, descr
->dibpitch
);
3577 X11DRV_DIB_SetImageBits_16( descr
->lines
, descr
->bits
,
3578 descr
->infoWidth
, descr
->width
,
3579 descr
->xSrc
, descr
->physDev
,
3580 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3581 bmpImage
, descr
->dibpitch
);
3584 X11DRV_DIB_SetImageBits_24( descr
->lines
, descr
->bits
,
3585 descr
->infoWidth
, descr
->width
,
3586 descr
->xSrc
, descr
->physDev
,
3587 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3588 bmpImage
, descr
->dibpitch
);
3591 X11DRV_DIB_SetImageBits_32( descr
->lines
, descr
->bits
,
3592 descr
->infoWidth
, descr
->width
,
3593 descr
->xSrc
, descr
->physDev
,
3594 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3595 bmpImage
, descr
->dibpitch
);
3598 WARN("(%d): Invalid depth\n", descr
->infoBpp
);
3602 TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3603 descr
->drawable
, descr
->gc
, bmpImage
,
3604 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
3605 descr
->width
, descr
->height
);
3608 #ifdef HAVE_LIBXXSHM
3609 if (descr
->image
&& descr
->useShm
)
3611 XShmPutImage( gdi_display
, descr
->drawable
, descr
->gc
, bmpImage
,
3612 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
3613 descr
->width
, descr
->height
, FALSE
);
3614 XSync( gdi_display
, 0 );
3618 XPutImage( gdi_display
, descr
->drawable
, descr
->gc
, bmpImage
,
3619 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
3620 descr
->width
, descr
->height
);
3622 if (!descr
->image
) XDestroyImage( bmpImage
);
3623 wine_tsx11_unlock();
3627 /***********************************************************************
3628 * X11DRV_DIB_GetImageBits
3630 * Transfer the bits from an X image.
3632 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR
*descr
)
3634 int lines
= descr
->lines
>= 0 ? descr
->lines
: -descr
->lines
;
3639 bmpImage
= descr
->image
;
3641 bmpImage
= XCreateImage( gdi_display
, visual
, descr
->depth
, ZPixmap
, 0, NULL
,
3642 descr
->infoWidth
, lines
, 32, 0 );
3643 bmpImage
->data
= calloc( lines
, bmpImage
->bytes_per_line
);
3644 if(bmpImage
->data
== NULL
) {
3645 ERR("Out of memory!\n");
3646 XDestroyImage( bmpImage
);
3647 wine_tsx11_unlock();
3652 #ifdef HAVE_LIBXXSHM
3654 /* We must not call XShmGetImage() with a bitmap which is bigger than the available area.
3655 If we do, XShmGetImage() will fail (X exception), as it checks for this internally. */
3656 if((descr
->image
&& descr
->useShm
) && (bmpImage
->width
<= (descr
->width
- descr
->xSrc
))
3657 && (bmpImage
->height
<= (descr
->height
- descr
->ySrc
)))
3659 int saveRed
, saveGreen
, saveBlue
;
3661 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3662 gdi_display
, descr
->drawable
, bmpImage
,
3663 descr
->xSrc
, descr
->ySrc
, AllPlanes
);
3665 /* We must save and restore the bmpImage's masks in order
3666 * to preserve them across the call to XShmGetImage, which
3667 * decides to eliminate them since it doesn't happen to know
3668 * what the format of the image is supposed to be, even though
3670 saveRed
= bmpImage
->red_mask
;
3671 saveBlue
= bmpImage
->blue_mask
;
3672 saveGreen
= bmpImage
->green_mask
;
3674 XShmGetImage( gdi_display
, descr
->drawable
, bmpImage
,
3675 descr
->xSrc
, descr
->ySrc
, AllPlanes
);
3677 bmpImage
->red_mask
= saveRed
;
3678 bmpImage
->blue_mask
= saveBlue
;
3679 bmpImage
->green_mask
= saveGreen
;
3682 #endif /* HAVE_LIBXXSHM */
3684 TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3685 gdi_display
, descr
->drawable
, descr
->xSrc
, descr
->ySrc
, descr
->width
,
3686 lines
, AllPlanes
, ZPixmap
, bmpImage
, descr
->xDest
, descr
->yDest
);
3687 XGetSubImage( gdi_display
, descr
->drawable
, descr
->xSrc
, descr
->ySrc
,
3688 descr
->width
, lines
, AllPlanes
, ZPixmap
,
3689 bmpImage
, descr
->xDest
, descr
->yDest
);
3691 wine_tsx11_unlock();
3693 TRACE("Dib: depth=%2d r=%x g=%x b=%x\n",
3694 descr
->infoBpp
,descr
->rMask
,descr
->gMask
,descr
->bMask
);
3695 TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3696 bmpImage
->depth
,bmpImage
->bits_per_pixel
,
3697 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3698 /* Transfer the pixels */
3699 switch(descr
->infoBpp
)
3702 X11DRV_DIB_GetImageBits_1( descr
->lines
,(LPVOID
)descr
->bits
,
3703 descr
->infoWidth
, descr
->width
,
3704 descr
->colorMap
, descr
->palentry
,
3705 bmpImage
, descr
->dibpitch
);
3709 if (descr
->compression
) {
3710 FIXME("Compression not yet supported!\n");
3711 if(descr
->sizeImage
< X11DRV_DIB_GetDIBWidthBytes( descr
->infoWidth
, 4 ) * abs(descr
->lines
))
3714 X11DRV_DIB_GetImageBits_4( descr
->lines
,(LPVOID
)descr
->bits
,
3715 descr
->infoWidth
, descr
->width
,
3716 descr
->colorMap
, descr
->palentry
,
3717 bmpImage
, descr
->dibpitch
);
3720 if (descr
->compression
) {
3721 FIXME("Compression not yet supported!\n");
3722 if(descr
->sizeImage
< X11DRV_DIB_GetDIBWidthBytes( descr
->infoWidth
, 8 ) * abs(descr
->lines
))
3725 X11DRV_DIB_GetImageBits_8( descr
->lines
, (LPVOID
)descr
->bits
,
3726 descr
->infoWidth
, descr
->width
,
3727 descr
->colorMap
, descr
->palentry
,
3728 bmpImage
, descr
->dibpitch
);
3732 X11DRV_DIB_GetImageBits_16( descr
->lines
, (LPVOID
)descr
->bits
,
3733 descr
->infoWidth
,descr
->width
,
3735 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3736 bmpImage
, descr
->dibpitch
);
3740 X11DRV_DIB_GetImageBits_24( descr
->lines
, (LPVOID
)descr
->bits
,
3741 descr
->infoWidth
,descr
->width
,
3743 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3744 bmpImage
, descr
->dibpitch
);
3748 X11DRV_DIB_GetImageBits_32( descr
->lines
, (LPVOID
)descr
->bits
,
3749 descr
->infoWidth
, descr
->width
,
3751 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3752 bmpImage
, descr
->dibpitch
);
3756 WARN("(%d): Invalid depth\n", descr
->infoBpp
);
3763 XDestroyImage( bmpImage
);
3764 wine_tsx11_unlock();
3769 /*************************************************************************
3770 * X11DRV_SetDIBitsToDevice
3773 INT
X11DRV_SetDIBitsToDevice( X11DRV_PDEVICE
*physDev
, INT xDest
, INT yDest
, DWORD cx
,
3774 DWORD cy
, INT xSrc
, INT ySrc
,
3775 UINT startscan
, UINT lines
, LPCVOID bits
,
3776 const BITMAPINFO
*info
, UINT coloruse
)
3778 X11DRV_DIB_IMAGEBITS_DESCR descr
;
3783 int rop
= X11DRV_XROPfunction
[GetROP2(physDev
->hdc
) - 1];
3785 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
,
3786 &descr
.infoBpp
, &descr
.compression
) == -1)
3789 top_down
= (height
< 0);
3790 if (top_down
) height
= -height
;
3794 LPtoDP(physDev
->hdc
, &pt
, 1);
3796 if (!lines
|| (startscan
>= height
)) return 0;
3797 if (!top_down
&& startscan
+ lines
> height
) lines
= height
- startscan
;
3799 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3800 * and clamp all values to fit inside [startscan,startscan+lines]
3802 if (ySrc
+ cy
<= startscan
+ lines
)
3804 UINT y
= startscan
+ lines
- (ySrc
+ cy
);
3805 if (ySrc
< startscan
) cy
-= (startscan
- ySrc
);
3808 /* avoid getting unnecessary lines */
3810 if (y
>= lines
) return 0;
3815 if (y
>= lines
) return lines
;
3816 ySrc
= y
; /* need to get all lines in top down mode */
3821 if (ySrc
>= startscan
+ lines
) return lines
;
3822 pt
.y
+= ySrc
+ cy
- (startscan
+ lines
);
3823 cy
= startscan
+ lines
- ySrc
;
3825 if (cy
> lines
) cy
= lines
;
3827 if (xSrc
>= width
) return lines
;
3828 if (xSrc
+ cx
>= width
) cx
= width
- xSrc
;
3829 if (!cx
|| !cy
) return lines
;
3831 /* Update the pixmap from the DIB section */
3832 X11DRV_LockDIBSection(physDev
, DIB_Status_GdiMod
);
3834 X11DRV_SetupGCForText( physDev
); /* To have the correct colors */
3836 XSetFunction(gdi_display
, physDev
->gc
, rop
);
3837 wine_tsx11_unlock();
3839 switch (descr
.infoBpp
)
3844 descr
.colorMap
= (RGBQUAD
*)X11DRV_DIB_BuildColorMap(
3846 physDev
->depth
, info
, &descr
.nColorMap
);
3847 if (!descr
.colorMap
) return 0;
3848 descr
.rMask
= descr
.gMask
= descr
.bMask
= 0;
3852 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0x7c00;
3853 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x03e0;
3854 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x001f;
3860 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0xff0000;
3861 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x00ff00;
3862 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x0000ff;
3867 descr
.physDev
= physDev
;
3870 descr
.palentry
= NULL
;
3871 descr
.lines
= top_down
? -lines
: lines
;
3872 descr
.infoWidth
= width
;
3873 descr
.depth
= physDev
->depth
;
3874 descr
.drawable
= physDev
->drawable
;
3875 descr
.gc
= physDev
->gc
;
3878 descr
.xDest
= physDev
->dc_rect
.left
+ pt
.x
;
3879 descr
.yDest
= physDev
->dc_rect
.top
+ pt
.y
;
3882 descr
.useShm
= FALSE
;
3883 descr
.dibpitch
= ((width
* descr
.infoBpp
+ 31) &~31) / 8;
3885 result
= X11DRV_DIB_SetImageBits( &descr
);
3887 if (descr
.infoBpp
<= 8)
3888 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
3890 /* Update the DIBSection of the pixmap */
3891 X11DRV_UnlockDIBSection(physDev
, TRUE
);
3896 /***********************************************************************
3897 * SetDIBits (X11DRV.@)
3899 INT
X11DRV_SetDIBits( X11DRV_PDEVICE
*physDev
, HBITMAP hbitmap
, UINT startscan
,
3900 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
, UINT coloruse
)
3902 X_PHYSBITMAP
*physBitmap
= X11DRV_get_phys_bitmap( hbitmap
);
3903 X11DRV_DIB_IMAGEBITS_DESCR descr
;
3905 LONG width
, height
, tmpheight
;
3908 descr
.physDev
= physDev
;
3910 if (!physBitmap
) return 0;
3912 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
,
3913 &descr
.infoBpp
, &descr
.compression
) == -1)
3917 if (height
< 0) height
= -height
;
3918 if (!lines
|| (startscan
>= height
))
3921 if (!GetObjectW( hbitmap
, sizeof(bitmap
), &bitmap
)) return 0;
3923 if (startscan
+ lines
> height
) lines
= height
- startscan
;
3925 switch (descr
.infoBpp
)
3930 descr
.colorMap
= (RGBQUAD
*)X11DRV_DIB_BuildColorMap(
3931 descr
.physDev
, coloruse
,
3932 physBitmap
->pixmap_depth
,
3933 info
, &descr
.nColorMap
);
3934 if (!descr
.colorMap
) return 0;
3935 descr
.rMask
= descr
.gMask
= descr
.bMask
= 0;
3939 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0x7c00;
3940 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x03e0;
3941 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x001f;
3947 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0xff0000;
3948 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x00ff00;
3949 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x0000ff;
3958 descr
.palentry
= NULL
;
3959 descr
.infoWidth
= width
;
3960 descr
.lines
= tmpheight
>= 0 ? lines
: -lines
;
3961 descr
.depth
= physBitmap
->pixmap_depth
;
3962 descr
.drawable
= physBitmap
->pixmap
;
3963 descr
.gc
= BITMAP_GC(physBitmap
);
3967 descr
.yDest
= height
- startscan
- lines
;
3968 descr
.width
= bitmap
.bmWidth
;
3969 descr
.height
= lines
;
3970 descr
.useShm
= FALSE
;
3971 descr
.dibpitch
= ((descr
.infoWidth
* descr
.infoBpp
+ 31) &~31) / 8;
3972 X11DRV_DIB_Lock( physBitmap
, DIB_Status_GdiMod
);
3973 result
= X11DRV_DIB_SetImageBits( &descr
);
3975 /* optimisation for the case where the input bits are in exactly the same
3976 * format as the internal representation and copying to the app bits is
3977 * cheap - saves a round trip to the X server */
3978 if (descr
.compression
== BI_RGB
&&
3979 coloruse
== DIB_RGB_COLORS
&&
3980 descr
.infoBpp
== bitmap
.bmBitsPixel
&&
3981 physBitmap
->base
&& physBitmap
->size
< 65536)
3983 unsigned int srcwidthb
= bitmap
.bmWidthBytes
;
3984 int dstwidthb
= X11DRV_DIB_GetDIBWidthBytes( width
, descr
.infoBpp
);
3985 LPBYTE dbits
= physBitmap
->base
, sbits
= (LPBYTE
)bits
+ (startscan
* srcwidthb
);
3989 TRACE("syncing compatible set bits to app bits\n");
3990 if ((tmpheight
< 0) ^ (bitmap
.bmHeight
< 0))
3992 dbits
= (LPBYTE
)bits
+ (dstwidthb
* (lines
-1));
3993 dstwidthb
= -dstwidthb
;
3995 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
3996 widthb
= min(srcwidthb
, abs(dstwidthb
));
3997 for (y
= 0; y
< lines
; y
++, dbits
+= dstwidthb
, sbits
+= srcwidthb
)
3998 memcpy(dbits
, sbits
, widthb
);
3999 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READONLY
);
4000 physBitmap
->status
= DIB_Status_InSync
;
4002 X11DRV_DIB_Unlock( physBitmap
, TRUE
);
4004 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
4009 /***********************************************************************
4010 * GetDIBits (X11DRV.@)
4012 INT
X11DRV_GetDIBits( X11DRV_PDEVICE
*physDev
, HBITMAP hbitmap
, UINT startscan
, UINT lines
,
4013 LPVOID bits
, BITMAPINFO
*info
, UINT coloruse
)
4015 X_PHYSBITMAP
*physBitmap
= X11DRV_get_phys_bitmap( hbitmap
);
4017 X11DRV_DIB_IMAGEBITS_DESCR descr
;
4018 PALETTEENTRY palette
[256];
4021 LONG width
, tempHeight
;
4026 GetPaletteEntries( GetCurrentObject( physDev
->hdc
, OBJ_PAL
), 0, 256, palette
);
4028 if (!physBitmap
) return 0;
4029 if (!(obj_size
= GetObjectW( hbitmap
, sizeof(dib
), &dib
))) return 0;
4031 bitmap_type
= DIB_GetBitmapInfo( (BITMAPINFOHEADER
*)info
, &width
, &tempHeight
, &descr
.infoBpp
, &descr
.compression
);
4032 descr
.lines
= tempHeight
;
4033 if (bitmap_type
== -1)
4035 ERR("Invalid bitmap\n");
4038 core_header
= (bitmap_type
== 0);
4039 colorPtr
= (LPBYTE
) info
+ (WORD
) info
->bmiHeader
.biSize
;
4041 TRACE("%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
4042 lines
, dib
.dsBm
.bmWidth
, dib
.dsBm
.bmHeight
, width
, descr
.lines
, startscan
);
4044 if( lines
> dib
.dsBm
.bmHeight
) lines
= dib
.dsBm
.bmHeight
;
4046 height
= descr
.lines
;
4047 if (height
< 0) height
= -height
;
4048 if( lines
> height
) lines
= height
;
4049 /* Top-down images have a negative biHeight, the scanlines of these images
4050 * were inverted in X11DRV_DIB_GetImageBits_xx
4051 * To prevent this we simply change the sign of lines
4052 * (the number of scan lines to copy).
4053 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4055 if( descr
.lines
< 0 && lines
> 0) lines
= -lines
;
4057 if( startscan
>= dib
.dsBm
.bmHeight
) return 0;
4059 descr
.colorMap
= NULL
;
4061 switch (descr
.infoBpp
)
4066 descr
.rMask
= descr
.gMask
= descr
.bMask
= 0;
4067 if(coloruse
== DIB_RGB_COLORS
)
4068 descr
.colorMap
= colorPtr
;
4070 int num_colors
= 1 << descr
.infoBpp
, i
;
4073 WORD
*index
= (WORD
*)colorPtr
;
4074 descr
.colorMap
= rgb
= HeapAlloc(GetProcessHeap(), 0, num_colors
* sizeof(RGBQUAD
));
4075 for(i
= 0; i
< num_colors
; i
++, rgb
++, index
++) {
4076 colref
= X11DRV_PALETTE_ToLogical(X11DRV_PALETTE_ToPhysical(physDev
, PALETTEINDEX(*index
)));
4077 rgb
->rgbRed
= GetRValue(colref
);
4078 rgb
->rgbGreen
= GetGValue(colref
);
4079 rgb
->rgbBlue
= GetBValue(colref
);
4080 rgb
->rgbReserved
= 0;
4086 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0x7c00;
4087 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x03e0;
4088 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x001f;
4092 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0xff0000;
4093 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x00ff00;
4094 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x0000ff;
4098 descr
.physDev
= physDev
;
4099 descr
.palentry
= palette
;
4101 descr
.image
= physBitmap
->image
;
4102 descr
.infoWidth
= width
;
4103 descr
.lines
= lines
;
4104 descr
.depth
= physBitmap
->pixmap_depth
;
4105 descr
.drawable
= physBitmap
->pixmap
;
4106 descr
.gc
= BITMAP_GC(physBitmap
);
4107 descr
.width
= dib
.dsBm
.bmWidth
;
4108 descr
.height
= dib
.dsBm
.bmHeight
;
4112 descr
.sizeImage
= core_header
? 0 : info
->bmiHeader
.biSizeImage
;
4114 if (descr
.lines
> 0)
4116 descr
.ySrc
= (descr
.height
-1) - (startscan
+ (lines
-1));
4120 descr
.ySrc
= startscan
;
4122 #ifdef HAVE_LIBXXSHM
4123 descr
.useShm
= (obj_size
== sizeof(DIBSECTION
)) && (physBitmap
->shminfo
.shmid
!= -1);
4125 descr
.useShm
= FALSE
;
4127 descr
.dibpitch
= (obj_size
== sizeof(DIBSECTION
)) ? dib
.dsBm
.bmWidthBytes
4128 : (((descr
.infoWidth
* descr
.infoBpp
+ 31) &~31) / 8);
4130 X11DRV_DIB_Lock( physBitmap
, DIB_Status_GdiMod
);
4131 X11DRV_DIB_GetImageBits( &descr
);
4132 X11DRV_DIB_Unlock( physBitmap
, TRUE
);
4134 if(!core_header
&& info
->bmiHeader
.biSizeImage
== 0) /* Fill in biSizeImage */
4135 info
->bmiHeader
.biSizeImage
= X11DRV_DIB_GetDIBImageBytes( descr
.infoWidth
,
4139 if (descr
.compression
== BI_BITFIELDS
)
4141 *(DWORD
*)info
->bmiColors
= descr
.rMask
;
4142 *((DWORD
*)info
->bmiColors
+ 1) = descr
.gMask
;
4143 *((DWORD
*)info
->bmiColors
+ 2) = descr
.bMask
;
4145 else if (!core_header
)
4147 /* if RLE or JPEG compression were supported,
4148 * this line would be invalid. */
4149 info
->bmiHeader
.biCompression
= 0;
4152 if(descr
.colorMap
!= colorPtr
)
4153 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
4157 /***********************************************************************
4158 * X11DRV_DIB_DoCopyDIBSection
4160 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP
*physBitmap
, BOOL toDIB
,
4161 void *colorMap
, int nColorMap
,
4162 Drawable dest
, GC gc
,
4163 DWORD xSrc
, DWORD ySrc
,
4164 DWORD xDest
, DWORD yDest
,
4165 DWORD width
, DWORD height
)
4167 DIBSECTION dibSection
;
4168 X11DRV_DIB_IMAGEBITS_DESCR descr
;
4169 int identity
[2] = {0,1};
4171 if (!GetObjectW( physBitmap
->hbitmap
, sizeof(dibSection
), &dibSection
)) return;
4173 descr
.physDev
= NULL
;
4174 descr
.palentry
= NULL
;
4175 descr
.infoWidth
= dibSection
.dsBmih
.biWidth
;
4176 descr
.infoBpp
= dibSection
.dsBmih
.biBitCount
;
4177 descr
.lines
= dibSection
.dsBmih
.biHeight
;
4178 descr
.image
= physBitmap
->image
;
4179 descr
.colorMap
= colorMap
;
4180 descr
.nColorMap
= nColorMap
;
4181 descr
.bits
= dibSection
.dsBm
.bmBits
;
4182 descr
.depth
= physBitmap
->pixmap_depth
;
4183 descr
.compression
= dibSection
.dsBmih
.biCompression
;
4185 if(descr
.infoBpp
== 1)
4186 descr
.colorMap
= (void*)identity
;
4188 switch (descr
.infoBpp
)
4193 descr
.rMask
= descr
.gMask
= descr
.bMask
= 0;
4197 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[0] : 0x7c00;
4198 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[1] : 0x03e0;
4199 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[2] : 0x001f;
4204 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[0] : 0xff0000;
4205 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[1] : 0x00ff00;
4206 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[2] : 0x0000ff;
4211 descr
.drawable
= dest
;
4215 descr
.xDest
= xDest
;
4216 descr
.yDest
= yDest
;
4217 descr
.width
= width
;
4218 descr
.height
= height
;
4219 descr
.sizeImage
= 0;
4221 #ifdef HAVE_LIBXXSHM
4222 descr
.useShm
= (physBitmap
->shminfo
.shmid
!= -1);
4224 descr
.useShm
= FALSE
;
4226 descr
.dibpitch
= dibSection
.dsBm
.bmWidthBytes
;
4230 TRACE("Copying from Pixmap to DIB bits\n");
4231 X11DRV_DIB_GetImageBits( &descr
);
4235 TRACE("Copying from DIB bits to Pixmap\n");
4236 X11DRV_DIB_SetImageBits( &descr
);
4240 /***********************************************************************
4241 * X11DRV_DIB_CopyDIBSection
4243 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE
*physDevSrc
, X11DRV_PDEVICE
*physDevDst
,
4244 DWORD xSrc
, DWORD ySrc
, DWORD xDest
, DWORD yDest
,
4245 DWORD width
, DWORD height
)
4248 X_PHYSBITMAP
*physBitmap
;
4249 unsigned int nColorMap
;
4253 TRACE("(%p,%p,%d,%d,%d,%d,%d,%d)\n", physDevSrc
->hdc
, physDevDst
->hdc
,
4254 xSrc
, ySrc
, xDest
, yDest
, width
, height
);
4255 /* this function is meant as an optimization for BitBlt,
4256 * not to be called otherwise */
4257 physBitmap
= physDevSrc
->bitmap
;
4258 if (!physBitmap
|| GetObjectW( physBitmap
->hbitmap
, sizeof(dib
), &dib
) != sizeof(dib
))
4260 ERR("called for non-DIBSection!?\n");
4263 /* while BitBlt should already have made sure we only get
4264 * positive values, we should check for oversize values */
4265 if ((xSrc
< dib
.dsBm
.bmWidth
) &&
4266 (ySrc
< dib
.dsBm
.bmHeight
)) {
4267 if (xSrc
+ width
> dib
.dsBm
.bmWidth
)
4268 width
= dib
.dsBm
.bmWidth
- xSrc
;
4269 if (ySrc
+ height
> dib
.dsBm
.bmHeight
)
4270 height
= dib
.dsBm
.bmHeight
- ySrc
;
4271 /* if the source bitmap is 8bpp or less, we're supposed to use the
4272 * DC's palette for color conversion (not the DIB color table) */
4273 if (dib
.dsBm
.bmBitsPixel
<= 8) {
4274 HPALETTE hPalette
= GetCurrentObject( physDevSrc
->hdc
, OBJ_PAL
);
4275 if (!hPalette
|| (hPalette
== GetStockObject(DEFAULT_PALETTE
))) {
4276 /* HACK: no palette has been set in the source DC,
4277 * use the DIB colormap instead - this is necessary in some
4278 * cases since we need to do depth conversion in some places
4279 * where real Windows can just copy data straight over */
4280 x11ColorMap
= physBitmap
->colorMap
;
4281 nColorMap
= physBitmap
->nColorMap
;
4282 freeColorMap
= FALSE
;
4284 const BITMAPINFO
* info
= (BITMAPINFO
*)&dib
.dsBmih
;
4287 nColorMap
= X11DRV_DIB_GetColorCount(info
);
4288 x11ColorMap
= HeapAlloc(GetProcessHeap(), 0, nColorMap
* sizeof(int));
4289 for (i
= 0; i
< nColorMap
; i
++)
4290 x11ColorMap
[i
] = X11DRV_PALETTE_ToPhysical(physDevSrc
, PALETTEINDEX(i
));
4291 freeColorMap
= TRUE
;
4298 freeColorMap
= FALSE
;
4300 /* perform the copy */
4301 X11DRV_DIB_DoCopyDIBSection(physBitmap
, FALSE
, x11ColorMap
, nColorMap
,
4302 physDevDst
->drawable
, physDevDst
->gc
, xSrc
, ySrc
,
4303 physDevDst
->dc_rect
.left
+ xDest
, physDevDst
->dc_rect
.top
+ yDest
,
4305 /* free color mapping */
4307 HeapFree(GetProcessHeap(), 0, x11ColorMap
);
4311 /***********************************************************************
4312 * X11DRV_DIB_DoUpdateDIBSection
4314 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP
*physBitmap
, BOOL toDIB
)
4318 GetObjectW( physBitmap
->hbitmap
, sizeof(bitmap
), &bitmap
);
4319 X11DRV_DIB_DoCopyDIBSection(physBitmap
, toDIB
,
4320 physBitmap
->colorMap
, physBitmap
->nColorMap
,
4321 physBitmap
->pixmap
, BITMAP_GC(physBitmap
),
4322 0, 0, 0, 0, bitmap
.bmWidth
, bitmap
.bmHeight
);
4325 /***********************************************************************
4326 * X11DRV_DIB_FaultHandler
4328 static LONG CALLBACK
X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep
)
4330 X_PHYSBITMAP
*physBitmap
= NULL
;
4334 const size_t pagemask
= getpagesize() - 1;
4336 if (ep
->ExceptionRecord
->ExceptionCode
!= EXCEPTION_ACCESS_VIOLATION
)
4337 return EXCEPTION_CONTINUE_SEARCH
;
4339 addr
= (BYTE
*)ep
->ExceptionRecord
->ExceptionInformation
[1];
4341 EnterCriticalSection(&dibs_cs
);
4342 LIST_FOR_EACH( ptr
, &dibs_list
)
4344 physBitmap
= LIST_ENTRY( ptr
, X_PHYSBITMAP
, entry
);
4345 if ((physBitmap
->base
<= addr
) &&
4346 (addr
< physBitmap
->base
+ ((physBitmap
->size
+ pagemask
) & ~pagemask
)))
4352 LeaveCriticalSection(&dibs_cs
);
4354 if (!found
) return EXCEPTION_CONTINUE_SEARCH
;
4356 if (addr
>= physBitmap
->base
+ physBitmap
->size
)
4357 WARN( "%p: access to %p beyond the end of the DIB\n", physBitmap
->hbitmap
, addr
);
4359 X11DRV_DIB_Lock( physBitmap
, DIB_Status_None
);
4360 if (ep
->ExceptionRecord
->ExceptionInformation
[0] == EXCEPTION_WRITE_FAULT
) {
4361 /* the app tried to write the DIB bits */
4362 X11DRV_DIB_Coerce( physBitmap
, DIB_Status_AppMod
);
4364 /* the app tried to read the DIB bits */
4365 X11DRV_DIB_Coerce( physBitmap
, DIB_Status_InSync
);
4367 X11DRV_DIB_Unlock( physBitmap
, TRUE
);
4369 return EXCEPTION_CONTINUE_EXECUTION
;
4372 /***********************************************************************
4375 static INT
X11DRV_DIB_Coerce(X_PHYSBITMAP
*physBitmap
, INT req
)
4377 INT ret
= DIB_Status_None
;
4379 if (!physBitmap
->image
) return ret
; /* not a DIB section */
4380 EnterCriticalSection(&physBitmap
->lock
);
4381 ret
= physBitmap
->status
;
4383 case DIB_Status_GdiMod
:
4384 /* GDI access - request to draw on pixmap */
4385 switch (physBitmap
->status
)
4388 case DIB_Status_None
:
4389 physBitmap
->p_status
= DIB_Status_GdiMod
;
4390 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, FALSE
);
4393 case DIB_Status_GdiMod
:
4394 TRACE("GdiMod requested in status GdiMod\n" );
4395 physBitmap
->p_status
= DIB_Status_GdiMod
;
4398 case DIB_Status_InSync
:
4399 TRACE("GdiMod requested in status InSync\n" );
4400 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_NOACCESS
);
4401 physBitmap
->status
= DIB_Status_GdiMod
;
4402 physBitmap
->p_status
= DIB_Status_InSync
;
4405 case DIB_Status_AppMod
:
4406 TRACE("GdiMod requested in status AppMod\n" );
4407 /* make it readonly to avoid app changing data while we copy */
4408 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READONLY
);
4409 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, FALSE
);
4410 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_NOACCESS
);
4411 physBitmap
->p_status
= DIB_Status_AppMod
;
4412 physBitmap
->status
= DIB_Status_GdiMod
;
4417 case DIB_Status_InSync
:
4418 /* App access - request access to read DIB surface */
4419 /* (typically called from signal handler) */
4420 switch (physBitmap
->status
)
4423 case DIB_Status_None
:
4424 /* shouldn't happen from signal handler */
4427 case DIB_Status_GdiMod
:
4428 TRACE("InSync requested in status GdiMod\n" );
4429 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
4430 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, TRUE
);
4431 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READONLY
);
4432 physBitmap
->status
= DIB_Status_InSync
;
4435 case DIB_Status_InSync
:
4436 TRACE("InSync requested in status InSync\n" );
4437 /* shouldn't happen from signal handler */
4440 case DIB_Status_AppMod
:
4441 TRACE("InSync requested in status AppMod\n" );
4442 /* no reason to do anything here, and this
4443 * shouldn't happen from signal handler */
4448 case DIB_Status_AppMod
:
4449 /* App access - request access to write DIB surface */
4450 /* (typically called from signal handler) */
4451 switch (physBitmap
->status
)
4454 case DIB_Status_None
:
4455 /* shouldn't happen from signal handler */
4458 case DIB_Status_GdiMod
:
4459 TRACE("AppMod requested in status GdiMod\n" );
4460 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
4461 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, TRUE
);
4462 physBitmap
->status
= DIB_Status_AppMod
;
4465 case DIB_Status_InSync
:
4466 TRACE("AppMod requested in status InSync\n" );
4467 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
4468 physBitmap
->status
= DIB_Status_AppMod
;
4471 case DIB_Status_AppMod
:
4472 TRACE("AppMod requested in status AppMod\n" );
4473 /* shouldn't happen from signal handler */
4478 /* it is up to the caller to do the copy/conversion, probably
4479 * using the return value to decide where to copy from */
4481 LeaveCriticalSection(&physBitmap
->lock
);
4485 /***********************************************************************
4488 static INT
X11DRV_DIB_Lock(X_PHYSBITMAP
*physBitmap
, INT req
)
4490 INT ret
= DIB_Status_None
;
4492 if (!physBitmap
->image
) return ret
; /* not a DIB section */
4493 TRACE("Locking %p from thread %04x\n", physBitmap
->hbitmap
, GetCurrentThreadId());
4494 EnterCriticalSection(&physBitmap
->lock
);
4495 ret
= physBitmap
->status
;
4496 if (req
!= DIB_Status_None
)
4497 X11DRV_DIB_Coerce(physBitmap
, req
);
4501 /***********************************************************************
4504 static void X11DRV_DIB_Unlock(X_PHYSBITMAP
*physBitmap
, BOOL commit
)
4506 if (!physBitmap
->image
) return; /* not a DIB section */
4507 switch (physBitmap
->status
)
4510 case DIB_Status_None
:
4511 /* in case anyone is wondering, this is the "signal handler doesn't
4512 * work" case, where we always have to be ready for app access */
4514 switch (physBitmap
->p_status
)
4516 case DIB_Status_GdiMod
:
4517 TRACE("Unlocking and syncing from GdiMod\n" );
4518 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, TRUE
);
4522 TRACE("Unlocking without needing to sync\n" );
4526 else TRACE("Unlocking with no changes\n");
4527 physBitmap
->p_status
= DIB_Status_None
;
4530 case DIB_Status_GdiMod
:
4531 TRACE("Unlocking in status GdiMod\n" );
4532 /* DIB was protected in Coerce */
4534 /* no commit, revert to InSync if applicable */
4535 if ((physBitmap
->p_status
== DIB_Status_InSync
) ||
4536 (physBitmap
->p_status
== DIB_Status_AppMod
)) {
4537 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READONLY
);
4538 physBitmap
->status
= DIB_Status_InSync
;
4543 case DIB_Status_InSync
:
4544 TRACE("Unlocking in status InSync\n" );
4545 /* DIB was already protected in Coerce */
4548 case DIB_Status_AppMod
:
4549 TRACE("Unlocking in status AppMod\n" );
4550 /* DIB was already protected in Coerce */
4551 /* this case is ordinary only called from the signal handler,
4552 * so we don't bother to check for !commit */
4555 LeaveCriticalSection(&physBitmap
->lock
);
4556 TRACE("Unlocked %p\n", physBitmap
->hbitmap
);
4559 /***********************************************************************
4560 * X11DRV_CoerceDIBSection
4562 INT
X11DRV_CoerceDIBSection(X11DRV_PDEVICE
*physDev
, INT req
)
4564 if (!physDev
|| !physDev
->bitmap
) return DIB_Status_None
;
4565 return X11DRV_DIB_Coerce(physDev
->bitmap
, req
);
4568 /***********************************************************************
4569 * X11DRV_LockDIBSection
4571 INT
X11DRV_LockDIBSection(X11DRV_PDEVICE
*physDev
, INT req
)
4573 if (!physDev
|| !physDev
->bitmap
) return DIB_Status_None
;
4574 return X11DRV_DIB_Lock(physDev
->bitmap
, req
);
4577 /***********************************************************************
4578 * X11DRV_UnlockDIBSection
4580 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE
*physDev
, BOOL commit
)
4582 if (!physDev
|| !physDev
->bitmap
) return;
4583 X11DRV_DIB_Unlock(physDev
->bitmap
, commit
);
4587 #ifdef HAVE_LIBXXSHM
4588 /***********************************************************************
4589 * X11DRV_XShmErrorHandler
4592 static int XShmErrorHandler( Display
*dpy
, XErrorEvent
*event
, void *arg
)
4594 return 1; /* FIXME: should check event contents */
4597 /***********************************************************************
4598 * X11DRV_XShmCreateImage
4601 static XImage
*X11DRV_XShmCreateImage( int width
, int height
, int bpp
,
4602 XShmSegmentInfo
* shminfo
)
4606 image
= XShmCreateImage(gdi_display
, visual
, bpp
, ZPixmap
, NULL
, shminfo
, width
, height
);
4609 shminfo
->shmid
= shmget(IPC_PRIVATE
, image
->bytes_per_line
* height
,
4611 if( shminfo
->shmid
!= -1 )
4613 shminfo
->shmaddr
= image
->data
= shmat(shminfo
->shmid
, 0, 0);
4614 if( shminfo
->shmaddr
!= (char*)-1 )
4618 shminfo
->readOnly
= FALSE
;
4619 X11DRV_expect_error( gdi_display
, XShmErrorHandler
, NULL
);
4620 ok
= (XShmAttach( gdi_display
, shminfo
) != 0);
4621 XSync( gdi_display
, False
);
4622 if (X11DRV_check_error()) ok
= FALSE
;
4625 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
4626 return image
; /* Success! */
4628 /* An error occurred */
4629 shmdt(shminfo
->shmaddr
);
4631 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
4632 shminfo
->shmid
= -1;
4634 XFlush(gdi_display
);
4635 XDestroyImage(image
);
4640 #endif /* HAVE_LIBXXSHM */
4643 /***********************************************************************
4644 * X11DRV_CreateDIBSection (X11DRV.@)
4646 HBITMAP
X11DRV_CreateDIBSection( X11DRV_PDEVICE
*physDev
, HBITMAP hbitmap
,
4647 const BITMAPINFO
*bmi
, UINT usage
)
4649 X_PHYSBITMAP
*physBitmap
;
4652 if (!(physBitmap
= X11DRV_init_phys_bitmap( hbitmap
))) return 0;
4653 physBitmap
->status
= DIB_Status_None
;
4655 GetObjectW( hbitmap
, sizeof(dib
), &dib
);
4657 /* create color map */
4658 if (dib
.dsBm
.bmBitsPixel
<= 8)
4660 physBitmap
->colorMap
= X11DRV_DIB_BuildColorMap( physDev
,
4661 usage
, dib
.dsBm
.bmBitsPixel
, bmi
,
4662 &physBitmap
->nColorMap
);
4665 /* create pixmap and X image */
4667 physBitmap
->pixmap_depth
= (dib
.dsBm
.bmBitsPixel
== 1) ? 1 : screen_depth
;
4668 physBitmap
->pixmap
= XCreatePixmap( gdi_display
, root_window
, dib
.dsBm
.bmWidth
,
4669 dib
.dsBm
.bmHeight
, physBitmap
->pixmap_depth
);
4670 #ifdef HAVE_LIBXXSHM
4671 physBitmap
->shminfo
.shmid
= -1;
4672 if (!XShmQueryExtension(gdi_display
) ||
4673 !(physBitmap
->image
= X11DRV_XShmCreateImage( dib
.dsBm
.bmWidth
, dib
.dsBm
.bmHeight
,
4674 physBitmap
->pixmap_depth
, &physBitmap
->shminfo
)) )
4676 physBitmap
->image
= X11DRV_DIB_CreateXImage( dib
.dsBm
.bmWidth
, dib
.dsBm
.bmHeight
,
4677 physBitmap
->pixmap_depth
);
4678 wine_tsx11_unlock();
4679 if (!physBitmap
->pixmap
|| !physBitmap
->image
) return 0;
4681 /* install fault handler */
4682 InitializeCriticalSection( &physBitmap
->lock
);
4683 physBitmap
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": X_PHYSBITMAP.lock");
4685 physBitmap
->base
= dib
.dsBm
.bmBits
;
4686 physBitmap
->size
= dib
.dsBmih
.biSizeImage
;
4687 physBitmap
->status
= DIB_Status_AppMod
;
4690 dibs_handler
= AddVectoredExceptionHandler( TRUE
, X11DRV_DIB_FaultHandler
);
4691 EnterCriticalSection( &dibs_cs
);
4692 list_add_head( &dibs_list
, &physBitmap
->entry
);
4693 LeaveCriticalSection( &dibs_cs
);
4695 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
4700 /***********************************************************************
4701 * X11DRV_DIB_DeleteDIBSection
4703 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP
*physBitmap
, DIBSECTION
*dib
)
4707 EnterCriticalSection( &dibs_cs
);
4708 list_remove( &physBitmap
->entry
);
4709 last
= list_empty( &dibs_list
);
4710 LeaveCriticalSection( &dibs_cs
);
4714 RemoveVectoredExceptionHandler( dibs_handler
);
4715 dibs_handler
= NULL
;
4718 if (dib
->dshSection
)
4719 X11DRV_DIB_Coerce(physBitmap
, DIB_Status_InSync
);
4721 if (physBitmap
->image
)
4724 #ifdef HAVE_LIBXXSHM
4725 if (physBitmap
->shminfo
.shmid
!= -1)
4727 XShmDetach( gdi_display
, &(physBitmap
->shminfo
) );
4728 XDestroyImage( physBitmap
->image
);
4729 shmdt( physBitmap
->shminfo
.shmaddr
);
4730 physBitmap
->shminfo
.shmid
= -1;
4734 XDestroyImage( physBitmap
->image
);
4735 wine_tsx11_unlock();
4738 HeapFree(GetProcessHeap(), 0, physBitmap
->colorMap
);
4739 physBitmap
->lock
.DebugInfo
->Spare
[0] = 0;
4740 DeleteCriticalSection(&physBitmap
->lock
);
4743 /***********************************************************************
4744 * SetDIBColorTable (X11DRV.@)
4746 UINT
X11DRV_SetDIBColorTable( X11DRV_PDEVICE
*physDev
, UINT start
, UINT count
, const RGBQUAD
*colors
)
4750 X_PHYSBITMAP
*physBitmap
= physDev
->bitmap
;
4752 if (!physBitmap
) return 0;
4753 GetObjectW( physBitmap
->hbitmap
, sizeof(dib
), &dib
);
4755 if (physBitmap
->colorMap
&& start
< physBitmap
->nColorMap
) {
4756 UINT end
= count
+ start
;
4757 if (end
> physBitmap
->nColorMap
) end
= physBitmap
->nColorMap
;
4759 * Changing color table might change the mapping between
4760 * DIB colors and X11 colors and thus alter the visible state
4761 * of the bitmap object.
4764 * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4765 * at least for a 1 bpp dibsection
4767 X11DRV_DIB_Lock( physBitmap
, DIB_Status_AppMod
);
4768 X11DRV_DIB_GenColorMap( physDev
, physBitmap
->colorMap
, DIB_RGB_COLORS
,
4769 dib
.dsBm
.bmBitsPixel
,
4770 TRUE
, colors
, start
, end
);
4771 X11DRV_DIB_Unlock( physBitmap
, TRUE
);
4778 /***********************************************************************
4779 * X11DRV_DIB_CreateDIBFromBitmap
4781 * Allocates a packed DIB and copies the bitmap data into it.
4783 HGLOBAL
X11DRV_DIB_CreateDIBFromBitmap(HDC hdc
, HBITMAP hBmp
)
4788 LPBITMAPINFOHEADER pbmiHeader
;
4789 unsigned int cDataSize
, cPackedSize
, OffsetBits
;
4792 if (!GetObjectW( hBmp
, sizeof(bmp
), &bmp
)) return 0;
4795 * A packed DIB contains a BITMAPINFO structure followed immediately by
4796 * an optional color palette and the pixel data.
4799 /* Calculate the size of the packed DIB */
4800 cDataSize
= X11DRV_DIB_GetDIBWidthBytes( bmp
.bmWidth
, bmp
.bmBitsPixel
) * abs( bmp
.bmHeight
);
4801 cPackedSize
= sizeof(BITMAPINFOHEADER
)
4802 + ( (bmp
.bmBitsPixel
<= 8) ? (sizeof(RGBQUAD
) * (1 << bmp
.bmBitsPixel
)) : 0 )
4804 /* Get the offset to the bits */
4805 OffsetBits
= cPackedSize
- cDataSize
;
4807 /* Allocate the packed DIB */
4808 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize
);
4809 hPackedDIB
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
/*| GMEM_ZEROINIT*/,
4813 WARN("Could not allocate packed DIB!\n");
4817 /* A packed DIB starts with a BITMAPINFOHEADER */
4818 pPackedDIB
= GlobalLock(hPackedDIB
);
4819 pbmiHeader
= (LPBITMAPINFOHEADER
)pPackedDIB
;
4821 /* Init the BITMAPINFOHEADER */
4822 pbmiHeader
->biSize
= sizeof(BITMAPINFOHEADER
);
4823 pbmiHeader
->biWidth
= bmp
.bmWidth
;
4824 pbmiHeader
->biHeight
= bmp
.bmHeight
;
4825 pbmiHeader
->biPlanes
= 1;
4826 pbmiHeader
->biBitCount
= bmp
.bmBitsPixel
;
4827 pbmiHeader
->biCompression
= BI_RGB
;
4828 pbmiHeader
->biSizeImage
= 0;
4829 pbmiHeader
->biXPelsPerMeter
= pbmiHeader
->biYPelsPerMeter
= 0;
4830 pbmiHeader
->biClrUsed
= 0;
4831 pbmiHeader
->biClrImportant
= 0;
4833 /* Retrieve the DIB bits from the bitmap and fill in the
4834 * DIB color table if present */
4836 nLinesCopied
= GetDIBits(hdc
, /* Handle to device context */
4837 hBmp
, /* Handle to bitmap */
4838 0, /* First scan line to set in dest bitmap */
4839 bmp
.bmHeight
, /* Number of scan lines to copy */
4840 pPackedDIB
+ OffsetBits
, /* [out] Address of array for bitmap bits */
4841 (LPBITMAPINFO
) pbmiHeader
, /* [out] Address of BITMAPINFO structure */
4842 0); /* RGB or palette index */
4843 GlobalUnlock(hPackedDIB
);
4845 /* Cleanup if GetDIBits failed */
4846 if (nLinesCopied
!= bmp
.bmHeight
)
4848 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied
, bmp
.bmHeight
);
4849 GlobalFree(hPackedDIB
);
4856 /**************************************************************************
4857 * X11DRV_DIB_CreateDIBFromPixmap
4859 * Allocates a packed DIB and copies the Pixmap data into it.
4860 * The Pixmap passed in is deleted after the conversion.
4862 HGLOBAL
X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap
, HDC hdc
)
4865 X_PHYSBITMAP
*physBitmap
;
4866 HBITMAP hBmp
= 0, old
;
4867 HGLOBAL hPackedDIB
= 0;
4869 int x
,y
; /* Unused */
4870 unsigned border_width
; /* Unused */
4871 unsigned int depth
, width
, height
;
4873 /* Get the Pixmap dimensions and bit depth */
4875 if (!XGetGeometry(gdi_display
, pixmap
, &root
, &x
, &y
, &width
, &height
,
4876 &border_width
, &depth
)) depth
= 0;
4877 wine_tsx11_unlock();
4878 if (!depth
) return 0;
4880 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
4881 width
, height
, depth
);
4884 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
4885 * and make it a container for the pixmap passed.
4887 if (!(hBmp
= CreateBitmap( width
, height
, 1, depth_to_bpp(depth
), NULL
))) return 0;
4889 /* force bitmap to be owned by a screen DC */
4890 hdcMem
= CreateCompatibleDC( hdc
);
4891 old
= SelectObject( hdcMem
, hBmp
);
4893 physBitmap
= X11DRV_get_phys_bitmap( hBmp
);
4896 if (physBitmap
->pixmap
) XFreePixmap( gdi_display
, physBitmap
->pixmap
);
4897 physBitmap
->pixmap
= pixmap
;
4898 wine_tsx11_unlock();
4900 SelectObject( hdcMem
, old
);
4904 * Create a packed DIB from the Pixmap wrapper bitmap created above.
4905 * A packed DIB contains a BITMAPINFO structure followed immediately by
4906 * an optional color palette and the pixel data.
4908 hPackedDIB
= X11DRV_DIB_CreateDIBFromBitmap(hdc
, hBmp
);
4910 /* We can now get rid of the HBITMAP wrapper we created earlier.
4911 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
4915 TRACE("\tReturning packed DIB %p\n", hPackedDIB
);
4920 /**************************************************************************
4921 * X11DRV_DIB_CreatePixmapFromDIB
4923 * Creates a Pixmap from a packed DIB
4925 Pixmap
X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB
, HDC hdc
)
4928 X_PHYSBITMAP
*physBitmap
;
4932 /* Create a DDB from the DIB */
4934 pbmi
= GlobalLock(hPackedDIB
);
4935 hBmp
= CreateDIBitmap(hdc
, &pbmi
->bmiHeader
, CBM_INIT
,
4936 (LPBYTE
)pbmi
+ X11DRV_DIB_BitmapInfoSize( pbmi
, DIB_RGB_COLORS
),
4937 pbmi
, DIB_RGB_COLORS
);
4938 GlobalUnlock(hPackedDIB
);
4940 /* clear the physBitmap so that we can steal its pixmap */
4941 physBitmap
= X11DRV_get_phys_bitmap( hBmp
);
4942 pixmap
= physBitmap
->pixmap
;
4943 physBitmap
->pixmap
= 0;
4945 /* Delete the DDB we created earlier now that we have stolen its pixmap */
4948 TRACE("Returning Pixmap %ld\n", pixmap
);