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
25 #include <X11/extensions/XShm.h>
26 # ifdef HAVE_SYS_SHM_H
29 # ifdef HAVE_SYS_IPC_H
32 #endif /* defined(HAVE_LIBXXSHM) */
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(bitmap
);
46 static struct list dibs_list
= LIST_INIT(dibs_list
);
48 static CRITICAL_SECTION dibs_cs
;
49 static CRITICAL_SECTION_DEBUG dibs_cs_debug
=
52 { &dibs_cs_debug
.ProcessLocksList
, &dibs_cs_debug
.ProcessLocksList
},
53 0, 0, { (DWORD_PTR
)(__FILE__
": dibs_cs") }
55 static CRITICAL_SECTION dibs_cs
= { &dibs_cs_debug
, -1, 0, 0, 0, 0 };
57 static PVOID dibs_handler
;
59 static int ximageDepthTable
[32];
61 /* This structure holds the arguments for DIB_SetImageBits() */
64 X11DRV_PDEVICE
*physDev
;
67 PALETTEENTRY
*palentry
;
89 } X11DRV_DIB_IMAGEBITS_DESCR
;
94 RLE_EOL
= 0, /* End of line */
95 RLE_END
= 1, /* End of bitmap */
96 RLE_DELTA
= 2 /* Delta */
100 static INT
X11DRV_DIB_Coerce(X_PHYSBITMAP
*,INT
);
101 static INT
X11DRV_DIB_Lock(X_PHYSBITMAP
*,INT
);
102 static void X11DRV_DIB_Unlock(X_PHYSBITMAP
*,BOOL
);
105 Some of the following helper functions are duplicated in
109 /***********************************************************************
110 * X11DRV_DIB_GetXImageWidthBytes
112 * Return the width of an X image in bytes
114 static inline int X11DRV_DIB_GetXImageWidthBytes( int width
, int depth
)
116 if (!depth
|| depth
> 32) goto error
;
118 if (!ximageDepthTable
[depth
-1])
120 XImage
*testimage
= XCreateImage( gdi_display
, visual
, depth
,
121 ZPixmap
, 0, NULL
, 1, 1, 32, 20 );
124 ximageDepthTable
[depth
-1] = testimage
->bits_per_pixel
;
125 XDestroyImage( testimage
);
127 else ximageDepthTable
[depth
-1] = -1;
129 if (ximageDepthTable
[depth
-1] != -1)
130 return (4 * ((width
* ximageDepthTable
[depth
-1] + 31) / 32));
133 WARN( "(%d): Unsupported depth\n", depth
);
138 /***********************************************************************
139 * X11DRV_DIB_GetDIBWidthBytes
141 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
143 static int X11DRV_DIB_GetDIBWidthBytes( int width
, int depth
)
149 case 1: words
= (width
+ 31) / 32; break;
150 case 4: words
= (width
+ 7) / 8; break;
151 case 8: words
= (width
+ 3) / 4; break;
153 case 16: words
= (width
+ 1) / 2; break;
154 case 24: words
= (width
* 3 + 3) / 4; break;
156 WARN("(%d): Unsupported depth\n", depth
);
165 /***********************************************************************
166 * X11DRV_DIB_GetDIBImageBytes
168 * Return the number of bytes used to hold the image in a DIB bitmap.
170 static int X11DRV_DIB_GetDIBImageBytes( int width
, int height
, int depth
)
172 return X11DRV_DIB_GetDIBWidthBytes( width
, depth
) * abs( height
);
176 /***********************************************************************
177 * X11DRV_DIB_BitmapInfoSize
179 * Return the size of the bitmap info structure including color table.
181 int X11DRV_DIB_BitmapInfoSize( const BITMAPINFO
* info
, WORD coloruse
)
185 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
187 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
188 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
189 return sizeof(BITMAPCOREHEADER
) + colors
*
190 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
192 else /* assume BITMAPINFOHEADER */
194 colors
= info
->bmiHeader
.biClrUsed
;
195 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
196 colors
= 1 << info
->bmiHeader
.biBitCount
;
197 return sizeof(BITMAPINFOHEADER
) + colors
*
198 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
203 /***********************************************************************
204 * X11DRV_DIB_CreateXImage
208 XImage
*X11DRV_DIB_CreateXImage( int width
, int height
, int depth
)
214 width_bytes
= X11DRV_DIB_GetXImageWidthBytes( width
, depth
);
215 image
= XCreateImage( gdi_display
, visual
, depth
, ZPixmap
, 0,
216 calloc( height
, width_bytes
),
217 width
, height
, 32, width_bytes
);
223 /***********************************************************************
224 * DIB_GetBitmapInfoEx
226 * Get the info from a bitmap header.
227 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
229 static int DIB_GetBitmapInfoEx( const BITMAPINFOHEADER
*header
, LONG
*width
,
230 LONG
*height
, WORD
*planes
, WORD
*bpp
,
231 WORD
*compr
, DWORD
*size
)
233 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
235 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
236 *width
= core
->bcWidth
;
237 *height
= core
->bcHeight
;
238 *planes
= core
->bcPlanes
;
239 *bpp
= core
->bcBitCount
;
244 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
))
246 *width
= header
->biWidth
;
247 *height
= header
->biHeight
;
248 *planes
= header
->biPlanes
;
249 *bpp
= header
->biBitCount
;
250 *compr
= header
->biCompression
;
251 *size
= header
->biSizeImage
;
254 ERR("(%d): unknown/wrong size for header\n", header
->biSize
);
259 /***********************************************************************
260 * X11DRV_DIB_GetColorCount
262 * Computes the number of colors for the bitmap palette.
263 * Should not be called for a >8-bit deep bitmap.
265 static unsigned int X11DRV_DIB_GetColorCount(const BITMAPINFO
*info
)
268 BOOL core_info
= info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
);
272 colors
= 1 << ((const BITMAPCOREINFO
*)info
)->bmciHeader
.bcBitCount
;
276 colors
= info
->bmiHeader
.biClrUsed
;
277 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
281 ERR("called with >256 colors!\n");
287 /***********************************************************************
290 * Get the info from a bitmap header.
291 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
293 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
294 LONG
*height
, WORD
*bpp
, WORD
*compr
)
299 return DIB_GetBitmapInfoEx( header
, width
, height
, &planes
, bpp
, compr
, &size
);
303 static inline BOOL
colour_is_brighter(RGBQUAD c1
, RGBQUAD c2
)
305 return (c1
.rgbRed
* c1
.rgbRed
+ c1
.rgbGreen
* c1
.rgbGreen
+ c1
.rgbBlue
* c1
.rgbBlue
) >
306 (c2
.rgbRed
* c2
.rgbRed
+ c2
.rgbGreen
* c2
.rgbGreen
+ c2
.rgbBlue
* c2
.rgbBlue
);
309 /***********************************************************************
310 * X11DRV_DIB_GenColorMap
312 * Fills the color map of a bitmap palette. Should not be called
313 * for a >8-bit deep bitmap.
315 static int *X11DRV_DIB_GenColorMap( X11DRV_PDEVICE
*physDev
, int *colorMapping
,
316 WORD coloruse
, WORD depth
, BOOL quads
,
317 const void *colorPtr
, int start
, int end
)
321 if (coloruse
== DIB_RGB_COLORS
)
325 const RGBQUAD
* rgb
= (const RGBQUAD
*)colorPtr
;
327 if (depth
== 1) /* Monochrome */
332 if (GetDIBColorTable( physDev
->hdc
, 0, 2, table
) == 2)
333 invert
= !colour_is_brighter(table
[1], table
[0]);
335 for (i
= start
; i
< end
; i
++, rgb
++)
336 colorMapping
[i
] = ((rgb
->rgbRed
+ rgb
->rgbGreen
+
337 rgb
->rgbBlue
> 255*3/2 && !invert
) ||
338 (rgb
->rgbRed
+ rgb
->rgbGreen
+
339 rgb
->rgbBlue
<= 255*3/2 && invert
));
342 for (i
= start
; i
< end
; i
++, rgb
++)
343 colorMapping
[i
] = X11DRV_PALETTE_ToPhysical( NULL
, RGB(rgb
->rgbRed
,
349 const RGBTRIPLE
* rgb
= (const RGBTRIPLE
*)colorPtr
;
351 if (depth
== 1) /* Monochrome */
356 if (GetDIBColorTable( physDev
->hdc
, 0, 2, table
) == 2)
357 invert
= !colour_is_brighter(table
[1], table
[0]);
359 for (i
= start
; i
< end
; i
++, rgb
++)
360 colorMapping
[i
] = ((rgb
->rgbtRed
+ rgb
->rgbtGreen
+
361 rgb
->rgbtBlue
> 255*3/2 && !invert
) ||
362 (rgb
->rgbtRed
+ rgb
->rgbtGreen
+
363 rgb
->rgbtBlue
<= 255*3/2 && invert
));
366 for (i
= start
; i
< end
; i
++, rgb
++)
367 colorMapping
[i
] = X11DRV_PALETTE_ToPhysical( NULL
, RGB(rgb
->rgbtRed
,
372 else /* DIB_PAL_COLORS */
374 const WORD
* index
= (const WORD
*)colorPtr
;
376 for (i
= start
; i
< end
; i
++, index
++)
377 colorMapping
[i
] = X11DRV_PALETTE_ToPhysical( physDev
, PALETTEINDEX(*index
) );
383 /***********************************************************************
384 * X11DRV_DIB_BuildColorMap
386 * Build the color map from the bitmap palette. Should not be called
387 * for a >8-bit deep bitmap.
389 static int *X11DRV_DIB_BuildColorMap( X11DRV_PDEVICE
*physDev
, WORD coloruse
, WORD depth
,
390 const BITMAPINFO
*info
, int *nColors
)
393 const void *colorPtr
;
397 *nColors
= X11DRV_DIB_GetColorCount(info
);
398 if (!*nColors
) return NULL
;
400 isInfo
= info
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
);
401 colorPtr
= (const BYTE
*)info
+ (WORD
)info
->bmiHeader
.biSize
;
402 if (!(colorMapping
= HeapAlloc(GetProcessHeap(), 0, *nColors
* sizeof(int) )))
405 return X11DRV_DIB_GenColorMap( physDev
, colorMapping
, coloruse
, depth
,
406 isInfo
, colorPtr
, 0, *nColors
);
409 /***********************************************************************
410 * X11DRV_DIB_MapColor
412 static int X11DRV_DIB_MapColor( int *physMap
, int nPhysMap
, int phys
, int oldcol
)
416 if ((oldcol
< nPhysMap
) && (physMap
[oldcol
] == phys
))
419 for (color
= 0; color
< nPhysMap
; color
++)
420 if (physMap
[color
] == phys
)
423 WARN("Strange color %08x\n", phys
);
428 /*********************************************************************
429 * X11DRV_DIB_GetNearestIndex
431 * Helper for X11DRV_DIB_GetDIBits.
432 * Returns the nearest colour table index for a given RGB.
433 * Nearest is defined by minimizing the sum of the squares.
435 static INT
X11DRV_DIB_GetNearestIndex(RGBQUAD
*colormap
, int numColors
, BYTE r
, BYTE g
, BYTE b
)
437 INT i
, best
= -1, diff
, bestdiff
= -1;
440 for(color
= colormap
, i
= 0; i
< numColors
; color
++, i
++) {
441 diff
= (r
- color
->rgbRed
) * (r
- color
->rgbRed
) +
442 (g
- color
->rgbGreen
) * (g
- color
->rgbGreen
) +
443 (b
- color
->rgbBlue
) * (b
- color
->rgbBlue
);
446 if(best
== -1 || diff
< bestdiff
) {
453 /*********************************************************************
454 * X11DRV_DIB_MaskToShift
456 * Helper for X11DRV_DIB_GetDIBits.
457 * Returns the by how many bits to shift a given color so that it is
458 * in the proper position.
460 INT
X11DRV_DIB_MaskToShift(DWORD mask
)
468 while ((mask
&1)==0) {
475 /***********************************************************************
476 * X11DRV_DIB_CheckMask
478 * Check RGB mask if it is either 0 or matches visual's mask.
480 static inline int X11DRV_DIB_CheckMask(int red_mask
, int green_mask
, int blue_mask
)
482 return ( red_mask
== 0 && green_mask
== 0 && blue_mask
== 0 ) ||
483 ( red_mask
== visual
->red_mask
&& green_mask
== visual
->green_mask
&&
484 blue_mask
== visual
->blue_mask
);
487 /***********************************************************************
488 * X11DRV_DIB_SetImageBits_1
490 * SetDIBits for a 1-bit deep DIB.
492 static void X11DRV_DIB_SetImageBits_1( int lines
, const BYTE
*srcbits
,
493 DWORD srcwidth
, DWORD dstwidth
, int left
,
494 int *colors
, XImage
*bmpImage
, DWORD linebytes
)
503 srcbits
= srcbits
+ linebytes
* (lines
- 1);
504 linebytes
= -linebytes
;
507 if ((extra
= (left
& 7)) != 0) {
511 srcbits
+= left
>> 3;
512 width
= min(srcwidth
, dstwidth
);
514 /* ==== pal 1 dib -> any bmp format ==== */
515 for (h
= lines
-1; h
>=0; h
--) {
517 for (i
= width
/8, x
= left
; i
> 0; i
--) {
519 XPutPixel( bmpImage
, x
++, h
, colors
[ srcval
>> 7] );
520 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 6) & 1] );
521 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 5) & 1] );
522 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 4) & 1] );
523 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 3) & 1] );
524 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 2) & 1] );
525 XPutPixel( bmpImage
, x
++, h
, colors
[(srcval
>> 1) & 1] );
526 XPutPixel( bmpImage
, x
++, h
, colors
[ srcval
& 1] );
532 case 7: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
533 case 6: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
534 case 5: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
535 case 4: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
536 case 3: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
537 case 2: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]); srcval
<<=1;
538 case 1: XPutPixel(bmpImage
, x
++, h
, colors
[srcval
>> 7]);
541 srcbits
+= linebytes
;
545 /***********************************************************************
546 * X11DRV_DIB_GetImageBits_1
548 * GetDIBits for a 1-bit deep DIB.
550 static void X11DRV_DIB_GetImageBits_1( int lines
, BYTE
*dstbits
,
551 DWORD dstwidth
, DWORD srcwidth
,
552 RGBQUAD
*colors
, PALETTEENTRY
*srccolors
,
553 XImage
*bmpImage
, DWORD linebytes
)
556 int h
, width
= min(dstwidth
, srcwidth
);
560 dstbits
= dstbits
+ linebytes
* (lines
- 1);
561 linebytes
= -linebytes
;
564 switch (bmpImage
->depth
)
568 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
570 /* ==== pal 1 or 4 bmp -> pal 1 dib ==== */
573 for (h
=lines
-1; h
>=0; h
--) {
577 for (x
=0; x
<width
; x
++) {
579 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
580 dstval
|=(X11DRV_DIB_GetNearestIndex
584 srcval
.peBlue
) << (7 - (x
& 7)));
593 dstbits
+= linebytes
;
601 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
, bmpImage
->green_mask
, bmpImage
->blue_mask
)
603 /* ==== pal 8 bmp -> pal 1 dib ==== */
605 const BYTE
* srcpixel
;
608 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
610 for (h
=0; h
<lines
; h
++) {
615 for (x
=0; x
<width
; x
++) {
617 srcval
=srccolors
[*srcpixel
++];
618 dstval
|=(X11DRV_DIB_GetNearestIndex
622 srcval
.peBlue
) << (7-(x
&7)) );
631 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
632 dstbits
+= linebytes
;
643 const WORD
* srcpixel
;
646 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
648 if (bmpImage
->green_mask
==0x03e0) {
649 if (bmpImage
->red_mask
==0x7c00) {
650 /* ==== rgb 555 bmp -> pal 1 dib ==== */
651 for (h
=0; h
<lines
; h
++) {
656 for (x
=0; x
<width
; x
++) {
659 dstval
|=(X11DRV_DIB_GetNearestIndex
661 ((srcval
>> 7) & 0xf8) | /* r */
662 ((srcval
>> 12) & 0x07),
663 ((srcval
>> 2) & 0xf8) | /* g */
664 ((srcval
>> 7) & 0x07),
665 ((srcval
<< 3) & 0xf8) | /* b */
666 ((srcval
>> 2) & 0x07) ) << (7-(x
&7)) );
675 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
676 dstbits
+= linebytes
;
678 } else if (bmpImage
->blue_mask
==0x7c00) {
679 /* ==== bgr 555 bmp -> pal 1 dib ==== */
680 for (h
=0; h
<lines
; h
++) {
685 for (x
=0; x
<width
; x
++) {
688 dstval
|=(X11DRV_DIB_GetNearestIndex
690 ((srcval
<< 3) & 0xf8) | /* r */
691 ((srcval
>> 2) & 0x07),
692 ((srcval
>> 2) & 0xf8) | /* g */
693 ((srcval
>> 7) & 0x07),
694 ((srcval
>> 7) & 0xf8) | /* b */
695 ((srcval
>> 12) & 0x07) ) << (7-(x
&7)) );
704 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
705 dstbits
+= linebytes
;
710 } else if (bmpImage
->green_mask
==0x07e0) {
711 if (bmpImage
->red_mask
==0xf800) {
712 /* ==== rgb 565 bmp -> pal 1 dib ==== */
713 for (h
=0; h
<lines
; h
++) {
718 for (x
=0; x
<width
; x
++) {
721 dstval
|=(X11DRV_DIB_GetNearestIndex
723 ((srcval
>> 8) & 0xf8) | /* r */
724 ((srcval
>> 13) & 0x07),
725 ((srcval
>> 3) & 0xfc) | /* g */
726 ((srcval
>> 9) & 0x03),
727 ((srcval
<< 3) & 0xf8) | /* b */
728 ((srcval
>> 2) & 0x07) ) << (7-(x
&7)) );
737 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
738 dstbits
+= linebytes
;
740 } else if (bmpImage
->blue_mask
==0xf800) {
741 /* ==== bgr 565 bmp -> pal 1 dib ==== */
742 for (h
=0; h
<lines
; h
++) {
747 for (x
=0; x
<width
; x
++) {
750 dstval
|=(X11DRV_DIB_GetNearestIndex
752 ((srcval
<< 3) & 0xf8) | /* r */
753 ((srcval
>> 2) & 0x07),
754 ((srcval
>> 3) & 0xfc) | /* g */
755 ((srcval
>> 9) & 0x03),
756 ((srcval
>> 8) & 0xf8) | /* b */
757 ((srcval
>> 13) & 0x07) ) << (7-(x
&7)) );
766 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
767 dstbits
+= linebytes
;
786 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
787 bytes_per_pixel
=(bmpImage
->bits_per_pixel
==24?3:4);
789 if (bmpImage
->green_mask
!=0x00ff00 ||
790 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
792 } else if (bmpImage
->blue_mask
==0xff) {
793 /* ==== rgb 888 or 0888 bmp -> pal 1 dib ==== */
794 for (h
=0; h
<lines
; h
++) {
799 for (x
=0; x
<width
; x
++) {
800 dstval
|=(X11DRV_DIB_GetNearestIndex
804 srcbyte
[0]) << (7-(x
&7)) );
805 srcbyte
+=bytes_per_pixel
;
814 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
815 dstbits
+= linebytes
;
818 /* ==== bgr 888 or 0888 bmp -> pal 1 dib ==== */
819 for (h
=0; h
<lines
; h
++) {
824 for (x
=0; x
<width
; x
++) {
825 dstval
|=(X11DRV_DIB_GetNearestIndex
829 srcbyte
[2]) << (7-(x
&7)) );
830 srcbyte
+=bytes_per_pixel
;
839 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
840 dstbits
+= linebytes
;
851 unsigned long white
= (1 << bmpImage
->bits_per_pixel
) - 1;
853 /* ==== any bmp format -> pal 1 dib ==== */
854 if ((unsigned)colors
[0].rgbRed
+colors
[0].rgbGreen
+colors
[0].rgbBlue
>=
855 (unsigned)colors
[1].rgbRed
+colors
[1].rgbGreen
+colors
[1].rgbBlue
)
858 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 1 bit DIB, "
859 "%s color mapping\n",
860 bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
861 bmpImage
->green_mask
, bmpImage
->blue_mask
,
862 neg
?"negative":"direct" );
864 for (h
=lines
-1; h
>=0; h
--) {
868 for (x
=0; x
<width
; x
++) {
869 dstval
|=((XGetPixel( bmpImage
, x
, h
) >= white
) ^ neg
) << (7 - (x
&7));
878 dstbits
+= linebytes
;
885 /***********************************************************************
886 * X11DRV_DIB_SetImageBits_4
888 * SetDIBits for a 4-bit deep DIB.
890 static void X11DRV_DIB_SetImageBits_4( int lines
, const BYTE
*srcbits
,
891 DWORD srcwidth
, DWORD dstwidth
, int left
,
892 int *colors
, XImage
*bmpImage
, DWORD linebytes
)
900 srcbits
= srcbits
+ linebytes
* (lines
- 1);
901 linebytes
= -linebytes
;
908 srcbits
+= left
>> 1;
909 width
= min(srcwidth
, dstwidth
);
911 /* ==== pal 4 dib -> any bmp format ==== */
912 for (h
= lines
-1; h
>= 0; h
--) {
914 for (i
= width
/2, x
= left
; i
> 0; i
--) {
915 BYTE srcval
=*srcbyte
++;
916 XPutPixel( bmpImage
, x
++, h
, colors
[srcval
>> 4] );
917 XPutPixel( bmpImage
, x
++, h
, colors
[srcval
& 0x0f] );
920 XPutPixel( bmpImage
, x
, h
, colors
[*srcbyte
>> 4] );
921 srcbits
+= linebytes
;
927 /***********************************************************************
928 * X11DRV_DIB_GetImageBits_4
930 * GetDIBits for a 4-bit deep DIB.
932 static void X11DRV_DIB_GetImageBits_4( int lines
, BYTE
*dstbits
,
933 DWORD srcwidth
, DWORD dstwidth
,
934 RGBQUAD
*colors
, PALETTEENTRY
*srccolors
,
935 XImage
*bmpImage
, DWORD linebytes
)
938 int h
, width
= min(srcwidth
, dstwidth
);
944 dstbits
= dstbits
+ ( linebytes
* (lines
-1) );
945 linebytes
= -linebytes
;
950 switch (bmpImage
->depth
) {
953 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
955 /* ==== pal 1 or 4 bmp -> pal 4 dib ==== */
958 for (h
= lines
-1; h
>= 0; h
--) {
962 for (x
= 0; x
< width
; x
++) {
964 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
965 dstval
|=(X11DRV_DIB_GetNearestIndex
969 srcval
.peBlue
) << (4-((x
&1)<<2)));
978 dstbits
+= linebytes
;
986 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
988 /* ==== pal 8 bmp -> pal 4 dib ==== */
990 const BYTE
*srcpixel
;
993 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
994 for (h
=0; h
<lines
; h
++) {
999 for (x
=0; x
<width
; x
++) {
1000 PALETTEENTRY srcval
;
1001 srcval
= srccolors
[*srcpixel
++];
1002 dstval
|=(X11DRV_DIB_GetNearestIndex
1006 srcval
.peBlue
) << (4*(1-(x
&1))) );
1015 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1016 dstbits
+= linebytes
;
1026 const void* srcbits
;
1027 const WORD
* srcpixel
;
1030 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1032 if (bmpImage
->green_mask
==0x03e0) {
1033 if (bmpImage
->red_mask
==0x7c00) {
1034 /* ==== rgb 555 bmp -> pal 4 dib ==== */
1035 for (h
=0; h
<lines
; h
++) {
1040 for (x
=0; x
<width
; x
++) {
1043 dstval
|=(X11DRV_DIB_GetNearestIndex
1045 ((srcval
>> 7) & 0xf8) | /* r */
1046 ((srcval
>> 12) & 0x07),
1047 ((srcval
>> 2) & 0xf8) | /* g */
1048 ((srcval
>> 7) & 0x07),
1049 ((srcval
<< 3) & 0xf8) | /* b */
1050 ((srcval
>> 2) & 0x07) ) << ((1-(x
&1))<<2) );
1059 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1060 dstbits
+= linebytes
;
1062 } else if (bmpImage
->blue_mask
==0x7c00) {
1063 /* ==== bgr 555 bmp -> pal 4 dib ==== */
1064 for (h
=0; h
<lines
; h
++) {
1069 for (x
=0; x
<width
; x
++) {
1072 dstval
|=(X11DRV_DIB_GetNearestIndex
1074 ((srcval
<< 3) & 0xf8) | /* r */
1075 ((srcval
>> 2) & 0x07),
1076 ((srcval
>> 2) & 0xf8) | /* g */
1077 ((srcval
>> 7) & 0x07),
1078 ((srcval
>> 7) & 0xf8) | /* b */
1079 ((srcval
>> 12) & 0x07) ) << ((1-(x
&1))<<2) );
1088 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1089 dstbits
+= linebytes
;
1094 } else if (bmpImage
->green_mask
==0x07e0) {
1095 if (bmpImage
->red_mask
==0xf800) {
1096 /* ==== rgb 565 bmp -> pal 4 dib ==== */
1097 for (h
=0; h
<lines
; h
++) {
1102 for (x
=0; x
<width
; x
++) {
1105 dstval
|=(X11DRV_DIB_GetNearestIndex
1107 ((srcval
>> 8) & 0xf8) | /* r */
1108 ((srcval
>> 13) & 0x07),
1109 ((srcval
>> 3) & 0xfc) | /* g */
1110 ((srcval
>> 9) & 0x03),
1111 ((srcval
<< 3) & 0xf8) | /* b */
1112 ((srcval
>> 2) & 0x07) ) << ((1-(x
&1))<<2) );
1121 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1122 dstbits
+= linebytes
;
1124 } else if (bmpImage
->blue_mask
==0xf800) {
1125 /* ==== bgr 565 bmp -> pal 4 dib ==== */
1126 for (h
=0; h
<lines
; h
++) {
1131 for (x
=0; x
<width
; x
++) {
1134 dstval
|=(X11DRV_DIB_GetNearestIndex
1136 ((srcval
<< 3) & 0xf8) | /* r */
1137 ((srcval
>> 2) & 0x07),
1138 ((srcval
>> 3) & 0xfc) | /* g */
1139 ((srcval
>> 9) & 0x03),
1140 ((srcval
>> 8) & 0xf8) | /* b */
1141 ((srcval
>> 13) & 0x07) ) << ((1-(x
&1))<<2) );
1150 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1151 dstbits
+= linebytes
;
1163 if (bmpImage
->bits_per_pixel
==24) {
1164 const void* srcbits
;
1165 const BYTE
*srcbyte
;
1168 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1170 if (bmpImage
->green_mask
!=0x00ff00 ||
1171 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
1173 } else if (bmpImage
->blue_mask
==0xff) {
1174 /* ==== rgb 888 bmp -> pal 4 dib ==== */
1175 for (h
=0; h
<lines
; h
++) {
1178 for (x
=0; x
<width
/2; x
++) {
1179 /* Do 2 pixels at a time */
1180 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1185 X11DRV_DIB_GetNearestIndex
1193 /* And then the odd pixel */
1194 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1200 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1201 dstbits
+= linebytes
;
1204 /* ==== bgr 888 bmp -> pal 4 dib ==== */
1205 for (h
=0; h
<lines
; h
++) {
1208 for (x
=0; x
<width
/2; x
++) {
1209 /* Do 2 pixels at a time */
1210 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1215 X11DRV_DIB_GetNearestIndex
1223 /* And then the odd pixel */
1224 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1230 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1231 dstbits
+= linebytes
;
1240 const void* srcbits
;
1241 const BYTE
*srcbyte
;
1244 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1246 if (bmpImage
->green_mask
!=0x00ff00 ||
1247 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
1249 } else if (bmpImage
->blue_mask
==0xff) {
1250 /* ==== rgb 0888 bmp -> pal 4 dib ==== */
1251 for (h
=0; h
<lines
; h
++) {
1254 for (x
=0; x
<width
/2; x
++) {
1255 /* Do 2 pixels at a time */
1256 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1261 X11DRV_DIB_GetNearestIndex
1269 /* And then the odd pixel */
1270 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1276 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1277 dstbits
+= linebytes
;
1280 /* ==== bgr 0888 bmp -> pal 4 dib ==== */
1281 for (h
=0; h
<lines
; h
++) {
1284 for (x
=0; x
<width
/2; x
++) {
1285 /* Do 2 pixels at a time */
1286 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1291 X11DRV_DIB_GetNearestIndex
1299 /* And then the odd pixel */
1300 *dstbyte
++=(X11DRV_DIB_GetNearestIndex
1306 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1307 dstbits
+= linebytes
;
1318 /* ==== any bmp format -> pal 4 dib ==== */
1319 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 4 bit DIB\n",
1320 bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
1321 bmpImage
->green_mask
, bmpImage
->blue_mask
);
1322 for (h
=lines
-1; h
>=0; h
--) {
1324 for (x
=0; x
<(width
& ~1); x
+=2) {
1325 *dstbyte
++=(X11DRV_DIB_MapColor((int*)colors
, 16, XGetPixel(bmpImage
, x
, h
), 0) << 4) |
1326 X11DRV_DIB_MapColor((int*)colors
, 16, XGetPixel(bmpImage
, x
+1, h
), 0);
1329 *dstbyte
=(X11DRV_DIB_MapColor((int *)colors
, 16, XGetPixel(bmpImage
, x
, h
), 0) << 4);
1331 dstbits
+= linebytes
;
1338 /***********************************************************************
1339 * X11DRV_DIB_SetImageBits_RLE4
1341 * SetDIBits for a 4-bit deep compressed DIB.
1343 static void X11DRV_DIB_SetImageBits_RLE4( int lines
, const BYTE
*bits
,
1344 DWORD srcwidth
, DWORD dstwidth
,
1345 int left
, int *colors
,
1348 unsigned int x
= 0, width
= min(srcwidth
, dstwidth
);
1349 int y
= lines
- 1, c
, length
;
1350 const BYTE
*begin
= bits
;
1355 if (length
) { /* encoded */
1358 if (x
>= (left
+ width
)) break;
1359 if( x
>= left
) XPutPixel(bmpImage
, x
, y
, colors
[c
>> 4]);
1361 if (!length
--) break;
1362 if (x
>= (left
+ width
)) break;
1363 if( x
>= left
) XPutPixel(bmpImage
, x
, y
, colors
[c
& 0xf]);
1383 default: /* absolute */
1386 if (x
>= left
&& x
< (left
+ width
))
1387 XPutPixel(bmpImage
, x
, y
, colors
[c
>> 4]);
1389 if (!length
--) break;
1390 if (x
>= left
&& x
< (left
+ width
))
1391 XPutPixel(bmpImage
, x
, y
, colors
[c
& 0xf]);
1394 if ((bits
- begin
) & 1)
1403 /***********************************************************************
1404 * X11DRV_DIB_SetImageBits_8
1406 * SetDIBits for an 8-bit deep DIB.
1408 static void X11DRV_DIB_SetImageBits_8( int lines
, const BYTE
*srcbits
,
1409 DWORD srcwidth
, DWORD dstwidth
, int left
,
1410 const int *colors
, XImage
*bmpImage
,
1414 int h
, width
= min(srcwidth
, dstwidth
);
1415 const BYTE
* srcbyte
;
1421 srcbits
= srcbits
+ linebytes
* (lines
-1);
1422 linebytes
= -linebytes
;
1427 switch (bmpImage
->depth
) {
1430 #if defined(__i386__) && defined(__GNUC__)
1431 /* Some X servers might have 32 bit/ 16bit deep pixel */
1432 if (lines
&& width
&& (bmpImage
->bits_per_pixel
== 16) &&
1433 (ImageByteOrder(gdi_display
)==LSBFirst
) )
1435 dstbits
=(BYTE
*)bmpImage
->data
+left
*2+(lines
-1)*bmpImage
->bytes_per_line
;
1436 /* FIXME: Does this really handle all these cases correctly? */
1437 /* ==== pal 8 dib -> rgb or bgr 555 or 565 bmp ==== */
1438 for (h
= lines
; h
--; ) {
1439 int _cl1
,_cl2
; /* temp outputs for asm below */
1440 /* Borrowed from DirectDraw */
1441 __asm__
__volatile__(
1446 " movw (%%edx,%%eax,4),%%ax\n"
1448 " xor %%eax,%%eax\n"
1450 :"=S" (srcbyte
), "=D" (_cl1
), "=c" (_cl2
)
1455 :"eax", "cc", "memory"
1457 srcbyte
= (srcbits
+= linebytes
);
1458 dstbits
-= bmpImage
->bytes_per_line
;
1466 #if defined(__i386__) && defined(__GNUC__)
1467 if (lines
&& width
&& (bmpImage
->bits_per_pixel
== 32) &&
1468 (ImageByteOrder(gdi_display
)==LSBFirst
) )
1470 dstbits
=(BYTE
*)bmpImage
->data
+left
*4+(lines
-1)*bmpImage
->bytes_per_line
;
1471 /* FIXME: Does this really handle both cases correctly? */
1472 /* ==== pal 8 dib -> rgb or bgr 0888 bmp ==== */
1473 for (h
= lines
; h
--; ) {
1474 int _cl1
,_cl2
; /* temp outputs for asm below */
1475 /* Borrowed from DirectDraw */
1476 __asm__
__volatile__(
1481 " movl (%%edx,%%eax,4),%%eax\n"
1483 " xor %%eax,%%eax\n"
1485 :"=S" (srcbyte
), "=D" (_cl1
), "=c" (_cl2
)
1490 :"eax", "cc", "memory"
1492 srcbyte
= (srcbits
+= linebytes
);
1493 dstbits
-= bmpImage
->bytes_per_line
;
1500 break; /* use slow generic case below */
1503 /* ==== pal 8 dib -> any bmp format ==== */
1504 for (h
=lines
-1; h
>=0; h
--) {
1505 for (x
=left
; x
<width
+left
; x
++) {
1506 XPutPixel(bmpImage
, x
, h
, colors
[*srcbyte
++]);
1508 srcbyte
= (srcbits
+= linebytes
);
1512 /***********************************************************************
1513 * X11DRV_DIB_GetImageBits_8
1515 * GetDIBits for an 8-bit deep DIB.
1517 static void X11DRV_DIB_GetImageBits_8( int lines
, BYTE
*dstbits
,
1518 DWORD srcwidth
, DWORD dstwidth
,
1519 RGBQUAD
*colors
, PALETTEENTRY
*srccolors
,
1520 XImage
*bmpImage
, DWORD linebytes
)
1523 int h
, width
= min(srcwidth
, dstwidth
);
1529 dstbits
= dstbits
+ ( linebytes
* (lines
-1) );
1530 linebytes
= -linebytes
;
1535 * This condition is true when GetImageBits has been called by
1536 * UpdateDIBSection. For now, GetNearestIndex is too slow to support
1537 * 256 colormaps, so we'll just use it for GetDIBits calls.
1538 * (In some cases, in an updateDIBSection, the returned colors are bad too)
1540 if (!srccolors
) goto updatesection
;
1542 switch (bmpImage
->depth
) {
1545 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
1548 /* ==== pal 1 bmp -> pal 8 dib ==== */
1549 /* ==== pal 4 bmp -> pal 8 dib ==== */
1550 for (h
=lines
-1; h
>=0; h
--) {
1552 for (x
=0; x
<width
; x
++) {
1553 PALETTEENTRY srcval
;
1554 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
1555 *dstbyte
++=X11DRV_DIB_GetNearestIndex(colors
, 256,
1560 dstbits
+= linebytes
;
1568 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
1570 /* ==== pal 8 bmp -> pal 8 dib ==== */
1571 const void* srcbits
;
1572 const BYTE
* srcpixel
;
1574 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1575 for (h
=0; h
<lines
; h
++) {
1578 for (x
= 0; x
< width
; x
++) {
1579 PALETTEENTRY srcval
;
1580 srcval
=srccolors
[*srcpixel
++];
1581 *dstbyte
++=X11DRV_DIB_GetNearestIndex(colors
, 256,
1586 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1587 dstbits
+= linebytes
;
1597 const void* srcbits
;
1598 const WORD
* srcpixel
;
1601 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1603 if (bmpImage
->green_mask
==0x03e0) {
1604 if (bmpImage
->red_mask
==0x7c00) {
1605 /* ==== rgb 555 bmp -> pal 8 dib ==== */
1606 for (h
=0; h
<lines
; h
++) {
1609 for (x
=0; x
<width
; x
++) {
1612 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1614 ((srcval
>> 7) & 0xf8) | /* r */
1615 ((srcval
>> 12) & 0x07),
1616 ((srcval
>> 2) & 0xf8) | /* g */
1617 ((srcval
>> 7) & 0x07),
1618 ((srcval
<< 3) & 0xf8) | /* b */
1619 ((srcval
>> 2) & 0x07) );
1621 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1622 dstbits
+= linebytes
;
1624 } else if (bmpImage
->blue_mask
==0x7c00) {
1625 /* ==== bgr 555 bmp -> pal 8 dib ==== */
1626 for (h
=0; h
<lines
; h
++) {
1629 for (x
=0; x
<width
; x
++) {
1632 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1634 ((srcval
<< 3) & 0xf8) | /* r */
1635 ((srcval
>> 2) & 0x07),
1636 ((srcval
>> 2) & 0xf8) | /* g */
1637 ((srcval
>> 7) & 0x07),
1638 ((srcval
>> 7) & 0xf8) | /* b */
1639 ((srcval
>> 12) & 0x07) );
1641 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1642 dstbits
+= linebytes
;
1647 } else if (bmpImage
->green_mask
==0x07e0) {
1648 if (bmpImage
->red_mask
==0xf800) {
1649 /* ==== rgb 565 bmp -> pal 8 dib ==== */
1650 for (h
=0; h
<lines
; h
++) {
1653 for (x
=0; x
<width
; x
++) {
1656 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1658 ((srcval
>> 8) & 0xf8) | /* r */
1659 ((srcval
>> 13) & 0x07),
1660 ((srcval
>> 3) & 0xfc) | /* g */
1661 ((srcval
>> 9) & 0x03),
1662 ((srcval
<< 3) & 0xf8) | /* b */
1663 ((srcval
>> 2) & 0x07) );
1665 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1666 dstbits
+= linebytes
;
1668 } else if (bmpImage
->blue_mask
==0xf800) {
1669 /* ==== bgr 565 bmp -> pal 8 dib ==== */
1670 for (h
=0; h
<lines
; h
++) {
1673 for (x
=0; x
<width
; x
++) {
1676 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1678 ((srcval
<< 3) & 0xf8) | /* r */
1679 ((srcval
>> 2) & 0x07),
1680 ((srcval
>> 3) & 0xfc) | /* g */
1681 ((srcval
>> 9) & 0x03),
1682 ((srcval
>> 8) & 0xf8) | /* b */
1683 ((srcval
>> 13) & 0x07) );
1685 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1686 dstbits
+= linebytes
;
1700 const void* srcbits
;
1701 const BYTE
*srcbyte
;
1703 int bytes_per_pixel
;
1705 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
1706 bytes_per_pixel
=(bmpImage
->bits_per_pixel
==24?3:4);
1708 if (bmpImage
->green_mask
!=0x00ff00 ||
1709 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
1711 } else if (bmpImage
->blue_mask
==0xff) {
1712 /* ==== rgb 888 or 0888 bmp -> pal 8 dib ==== */
1713 for (h
=0; h
<lines
; h
++) {
1716 for (x
=0; x
<width
; x
++) {
1717 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1722 srcbyte
+=bytes_per_pixel
;
1724 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1725 dstbits
+= linebytes
;
1728 /* ==== bgr 888 or 0888 bmp -> pal 8 dib ==== */
1729 for (h
=0; h
<lines
; h
++) {
1732 for (x
=0; x
<width
; x
++) {
1733 *dstbyte
++=X11DRV_DIB_GetNearestIndex
1738 srcbyte
+=bytes_per_pixel
;
1740 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
1741 dstbits
+= linebytes
;
1749 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 8 bit DIB\n",
1750 bmpImage
->depth
, bmpImage
->red_mask
,
1751 bmpImage
->green_mask
, bmpImage
->blue_mask
);
1753 /* ==== any bmp format -> pal 8 dib ==== */
1754 for (h
=lines
-1; h
>=0; h
--) {
1756 for (x
=0; x
<width
; x
++) {
1757 *dstbyte
=X11DRV_DIB_MapColor
1759 XGetPixel(bmpImage
, x
, h
), *dstbyte
);
1762 dstbits
+= linebytes
;
1768 /***********************************************************************
1769 * X11DRV_DIB_SetImageBits_RLE8
1771 * SetDIBits for an 8-bit deep compressed DIB.
1773 * This function rewritten 941113 by James Youngman. WINE blew out when I
1774 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
1776 * This was because the algorithm assumed that all RLE8 bitmaps end with the
1777 * 'End of bitmap' escape code. This code is very much laxer in what it
1778 * allows to end the expansion. Possibly too lax. See the note by
1779 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
1780 * bitmap should end with RleEnd, but on the other hand, software exists
1781 * that produces ones that don't and Windows 3.1 doesn't complain a bit
1784 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
1785 * James A. Youngman <mbcstjy@afs.man.ac.uk>
1788 static void X11DRV_DIB_SetImageBits_RLE8( int lines
, const BYTE
*bits
,
1789 DWORD srcwidth
, DWORD dstwidth
,
1790 int left
, int *colors
,
1793 unsigned int x
; /* X-position on each line. Increases. */
1794 int y
; /* Line #. Starts at lines-1, decreases */
1795 const BYTE
*pIn
= bits
; /* Pointer to current position in bits */
1796 BYTE length
; /* The length pf a run */
1797 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
1800 * Note that the bitmap data is stored by Windows starting at the
1801 * bottom line of the bitmap and going upwards. Within each line,
1802 * the data is stored left-to-right. That's the reason why line
1803 * goes from lines-1 to 0. [JAY]
1813 * If the length byte is not zero (which is the escape value),
1814 * We have a run of length pixels all the same colour. The colour
1815 * index is stored next.
1817 * If the length byte is zero, we need to read the next byte to
1818 * know what to do. [JAY]
1823 * [Run-Length] Encoded mode
1825 int color
= colors
[*pIn
++];
1826 while (length
-- && x
< (left
+ dstwidth
)) {
1827 if( x
>= left
) XPutPixel(bmpImage
, x
, y
, color
);
1834 * Escape codes (may be an absolute sequence though)
1836 escape_code
= (*pIn
++);
1845 /* Not all RLE8 bitmaps end with this code. For
1846 * example, Paint Shop Pro produces some that don't.
1847 * That's (I think) what caused the previous
1848 * implementation to fail. [JAY]
1857 default: /* switch to absolute mode */
1858 length
= escape_code
;
1861 int color
= colors
[*pIn
++];
1862 if (x
>= (left
+ dstwidth
))
1867 if( x
>= left
) XPutPixel(bmpImage
, x
, y
, color
);
1871 * If you think for a moment you'll realise that the
1872 * only time we could ever possibly read an odd
1873 * number of bytes is when there is a 0x00 (escape),
1874 * a value >0x02 (absolute mode) and then an odd-
1875 * length run. Therefore this is the only place we
1876 * need to worry about it. Everywhere else the
1877 * bytes are always read in pairs. [JAY]
1879 if (escape_code
& 1) pIn
++; /* Throw away the pad byte. */
1881 } /* switch (escape_code) : Escape sequence */
1887 /***********************************************************************
1888 * X11DRV_DIB_SetImageBits_16
1890 * SetDIBits for a 16-bit deep DIB.
1892 static void X11DRV_DIB_SetImageBits_16( int lines
, const BYTE
*srcbits
,
1893 DWORD srcwidth
, DWORD dstwidth
, int left
,
1894 X11DRV_PDEVICE
*physDev
, DWORD rSrc
, DWORD gSrc
, DWORD bSrc
,
1895 XImage
*bmpImage
, DWORD linebytes
)
1898 int h
, width
= min(srcwidth
, dstwidth
);
1899 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_dst_byteswap
;
1904 srcbits
= srcbits
+ ( linebytes
* (lines
-1));
1905 linebytes
= -linebytes
;
1908 switch (bmpImage
->depth
)
1915 srcbits
=srcbits
+left
*2;
1916 dstbits
=bmpImage
->data
+left
*2+(lines
-1)*bmpImage
->bytes_per_line
;
1918 if (bmpImage
->green_mask
==0x03e0) {
1919 if (gSrc
==bmpImage
->green_mask
) {
1920 if (rSrc
==bmpImage
->red_mask
) {
1921 /* ==== rgb 555 dib -> rgb 555 bmp ==== */
1922 /* ==== bgr 555 dib -> bgr 555 bmp ==== */
1923 convs
->Convert_5x5_asis
1926 dstbits
,-bmpImage
->bytes_per_line
);
1927 } else if (rSrc
==bmpImage
->blue_mask
) {
1928 /* ==== rgb 555 dib -> bgr 555 bmp ==== */
1929 /* ==== bgr 555 dib -> rgb 555 bmp ==== */
1930 convs
->Convert_555_reverse
1933 dstbits
,-bmpImage
->bytes_per_line
);
1936 if (rSrc
==bmpImage
->red_mask
|| bSrc
==bmpImage
->blue_mask
) {
1937 /* ==== rgb 565 dib -> rgb 555 bmp ==== */
1938 /* ==== bgr 565 dib -> bgr 555 bmp ==== */
1939 convs
->Convert_565_to_555_asis
1942 dstbits
,-bmpImage
->bytes_per_line
);
1944 /* ==== rgb 565 dib -> bgr 555 bmp ==== */
1945 /* ==== bgr 565 dib -> rgb 555 bmp ==== */
1946 convs
->Convert_565_to_555_reverse
1949 dstbits
,-bmpImage
->bytes_per_line
);
1952 } else if (bmpImage
->green_mask
==0x07e0) {
1953 if (gSrc
==bmpImage
->green_mask
) {
1954 if (rSrc
==bmpImage
->red_mask
) {
1955 /* ==== rgb 565 dib -> rgb 565 bmp ==== */
1956 /* ==== bgr 565 dib -> bgr 565 bmp ==== */
1957 convs
->Convert_5x5_asis
1960 dstbits
,-bmpImage
->bytes_per_line
);
1962 /* ==== rgb 565 dib -> bgr 565 bmp ==== */
1963 /* ==== bgr 565 dib -> rgb 565 bmp ==== */
1964 convs
->Convert_565_reverse
1967 dstbits
,-bmpImage
->bytes_per_line
);
1970 if (rSrc
==bmpImage
->red_mask
|| bSrc
==bmpImage
->blue_mask
) {
1971 /* ==== rgb 555 dib -> rgb 565 bmp ==== */
1972 /* ==== bgr 555 dib -> bgr 565 bmp ==== */
1973 convs
->Convert_555_to_565_asis
1976 dstbits
,-bmpImage
->bytes_per_line
);
1978 /* ==== rgb 555 dib -> bgr 565 bmp ==== */
1979 /* ==== bgr 555 dib -> rgb 565 bmp ==== */
1980 convs
->Convert_555_to_565_reverse
1983 dstbits
,-bmpImage
->bytes_per_line
);
1993 if (bmpImage
->bits_per_pixel
==24) {
1996 srcbits
=srcbits
+left
*2;
1997 dstbits
=bmpImage
->data
+left
*3+(lines
-1)*bmpImage
->bytes_per_line
;
1999 if (bmpImage
->green_mask
!=0x00ff00 ||
2000 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2002 } else if ((rSrc
==0x1f && bmpImage
->red_mask
==0xff) ||
2003 (bSrc
==0x1f && bmpImage
->blue_mask
==0xff)) {
2005 /* ==== rgb 555 dib -> rgb 888 bmp ==== */
2006 /* ==== bgr 555 dib -> bgr 888 bmp ==== */
2007 convs
->Convert_555_to_888_asis
2010 dstbits
,-bmpImage
->bytes_per_line
);
2012 /* ==== rgb 565 dib -> rgb 888 bmp ==== */
2013 /* ==== bgr 565 dib -> bgr 888 bmp ==== */
2014 convs
->Convert_565_to_888_asis
2017 dstbits
,-bmpImage
->bytes_per_line
);
2021 /* ==== rgb 555 dib -> bgr 888 bmp ==== */
2022 /* ==== bgr 555 dib -> rgb 888 bmp ==== */
2023 convs
->Convert_555_to_888_reverse
2026 dstbits
,-bmpImage
->bytes_per_line
);
2028 /* ==== rgb 565 dib -> bgr 888 bmp ==== */
2029 /* ==== bgr 565 dib -> rgb 888 bmp ==== */
2030 convs
->Convert_565_to_888_reverse
2033 dstbits
,-bmpImage
->bytes_per_line
);
2044 srcbits
=srcbits
+left
*2;
2045 dstbits
=bmpImage
->data
+left
*4+(lines
-1)*bmpImage
->bytes_per_line
;
2047 if (bmpImage
->green_mask
!=0x00ff00 ||
2048 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2050 } else if ((rSrc
==0x1f && bmpImage
->red_mask
==0xff) ||
2051 (bSrc
==0x1f && bmpImage
->blue_mask
==0xff)) {
2053 /* ==== rgb 555 dib -> rgb 0888 bmp ==== */
2054 /* ==== bgr 555 dib -> bgr 0888 bmp ==== */
2055 convs
->Convert_555_to_0888_asis
2058 dstbits
,-bmpImage
->bytes_per_line
);
2060 /* ==== rgb 565 dib -> rgb 0888 bmp ==== */
2061 /* ==== bgr 565 dib -> bgr 0888 bmp ==== */
2062 convs
->Convert_565_to_0888_asis
2065 dstbits
,-bmpImage
->bytes_per_line
);
2069 /* ==== rgb 555 dib -> bgr 0888 bmp ==== */
2070 /* ==== bgr 555 dib -> rgb 0888 bmp ==== */
2071 convs
->Convert_555_to_0888_reverse
2074 dstbits
,-bmpImage
->bytes_per_line
);
2076 /* ==== rgb 565 dib -> bgr 0888 bmp ==== */
2077 /* ==== bgr 565 dib -> rgb 0888 bmp ==== */
2078 convs
->Convert_565_to_0888_reverse
2081 dstbits
,-bmpImage
->bytes_per_line
);
2089 WARN("from 16 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2090 rSrc
, gSrc
, bSrc
, bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
2091 bmpImage
->green_mask
, bmpImage
->blue_mask
);
2097 /* ==== rgb or bgr 555 or 565 dib -> pal 1, 4 or 8 ==== */
2098 const WORD
* srcpixel
;
2099 int rShift1
,gShift1
,bShift1
;
2100 int rShift2
,gShift2
,bShift2
;
2103 /* Set color scaling values */
2104 rShift1
=16+X11DRV_DIB_MaskToShift(rSrc
)-3;
2105 gShift1
=16+X11DRV_DIB_MaskToShift(gSrc
)-3;
2106 bShift1
=16+X11DRV_DIB_MaskToShift(bSrc
)-3;
2111 /* Green has 5 bits, like the others */
2115 /* Green has 6 bits, not 5. Compensate. */
2124 /* We could split it into four separate cases to optimize
2125 * but it is probably not worth it.
2127 for (h
=lines
-1; h
>=0; h
--) {
2128 srcpixel
=(const WORD
*)srcbits
;
2129 for (x
=left
; x
<width
+left
; x
++) {
2131 BYTE red
,green
,blue
;
2132 srcval
=*srcpixel
++ << 16;
2133 red
= ((srcval
>> rShift1
) & 0xf8) |
2134 ((srcval
>> rShift2
) & 0x07);
2135 green
=((srcval
>> gShift1
) & gMask1
) |
2136 ((srcval
>> gShift2
) & gMask2
);
2137 blue
= ((srcval
>> bShift1
) & 0xf8) |
2138 ((srcval
>> bShift2
) & 0x07);
2139 XPutPixel(bmpImage
, x
, h
,
2140 X11DRV_PALETTE_ToPhysical
2141 (physDev
, RGB(red
,green
,blue
)));
2143 srcbits
+= linebytes
;
2151 /***********************************************************************
2152 * X11DRV_DIB_GetImageBits_16
2154 * GetDIBits for an 16-bit deep DIB.
2156 static void X11DRV_DIB_GetImageBits_16( int lines
, BYTE
*dstbits
,
2157 DWORD dstwidth
, DWORD srcwidth
,
2158 PALETTEENTRY
*srccolors
,
2159 DWORD rDst
, DWORD gDst
, DWORD bDst
,
2160 XImage
*bmpImage
, DWORD dibpitch
)
2163 int h
, width
= min(srcwidth
, dstwidth
);
2164 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_src_byteswap
;
2166 DWORD linebytes
= dibpitch
;
2171 dstbits
= dstbits
+ ( linebytes
* (lines
-1));
2172 linebytes
= -linebytes
;
2175 switch (bmpImage
->depth
)
2180 const char* srcbits
;
2182 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2184 if (bmpImage
->green_mask
==0x03e0) {
2185 if (gDst
==bmpImage
->green_mask
) {
2186 if (rDst
==bmpImage
->red_mask
) {
2187 /* ==== rgb 555 bmp -> rgb 555 dib ==== */
2188 /* ==== bgr 555 bmp -> bgr 555 dib ==== */
2189 convs
->Convert_5x5_asis
2191 srcbits
,-bmpImage
->bytes_per_line
,
2194 /* ==== rgb 555 bmp -> bgr 555 dib ==== */
2195 /* ==== bgr 555 bmp -> rgb 555 dib ==== */
2196 convs
->Convert_555_reverse
2198 srcbits
,-bmpImage
->bytes_per_line
,
2202 if (rDst
==bmpImage
->red_mask
|| bDst
==bmpImage
->blue_mask
) {
2203 /* ==== rgb 555 bmp -> rgb 565 dib ==== */
2204 /* ==== bgr 555 bmp -> bgr 565 dib ==== */
2205 convs
->Convert_555_to_565_asis
2207 srcbits
,-bmpImage
->bytes_per_line
,
2210 /* ==== rgb 555 bmp -> bgr 565 dib ==== */
2211 /* ==== bgr 555 bmp -> rgb 565 dib ==== */
2212 convs
->Convert_555_to_565_reverse
2214 srcbits
,-bmpImage
->bytes_per_line
,
2218 } else if (bmpImage
->green_mask
==0x07e0) {
2219 if (gDst
==bmpImage
->green_mask
) {
2220 if (rDst
== bmpImage
->red_mask
) {
2221 /* ==== rgb 565 bmp -> rgb 565 dib ==== */
2222 /* ==== bgr 565 bmp -> bgr 565 dib ==== */
2223 convs
->Convert_5x5_asis
2225 srcbits
,-bmpImage
->bytes_per_line
,
2228 /* ==== rgb 565 bmp -> bgr 565 dib ==== */
2229 /* ==== bgr 565 bmp -> rgb 565 dib ==== */
2230 convs
->Convert_565_reverse
2232 srcbits
,-bmpImage
->bytes_per_line
,
2236 if (rDst
==bmpImage
->red_mask
|| bDst
==bmpImage
->blue_mask
) {
2237 /* ==== rgb 565 bmp -> rgb 555 dib ==== */
2238 /* ==== bgr 565 bmp -> bgr 555 dib ==== */
2239 convs
->Convert_565_to_555_asis
2241 srcbits
,-bmpImage
->bytes_per_line
,
2244 /* ==== rgb 565 bmp -> bgr 555 dib ==== */
2245 /* ==== bgr 565 bmp -> rgb 555 dib ==== */
2246 convs
->Convert_565_to_555_reverse
2248 srcbits
,-bmpImage
->bytes_per_line
,
2259 if (bmpImage
->bits_per_pixel
== 24) {
2260 const char* srcbits
;
2262 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2264 if (bmpImage
->green_mask
!=0x00ff00 ||
2265 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2267 } else if ((rDst
==0x1f && bmpImage
->red_mask
==0xff) ||
2268 (bDst
==0x1f && bmpImage
->blue_mask
==0xff)) {
2270 /* ==== rgb 888 bmp -> rgb 555 dib ==== */
2271 /* ==== bgr 888 bmp -> bgr 555 dib ==== */
2272 convs
->Convert_888_to_555_asis
2274 srcbits
,-bmpImage
->bytes_per_line
,
2277 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2278 /* ==== rgb 888 bmp -> rgb 565 dib ==== */
2279 convs
->Convert_888_to_565_asis
2281 srcbits
,-bmpImage
->bytes_per_line
,
2286 /* ==== rgb 888 bmp -> bgr 555 dib ==== */
2287 /* ==== bgr 888 bmp -> rgb 555 dib ==== */
2288 convs
->Convert_888_to_555_reverse
2290 srcbits
,-bmpImage
->bytes_per_line
,
2293 /* ==== rgb 888 bmp -> bgr 565 dib ==== */
2294 /* ==== bgr 888 bmp -> rgb 565 dib ==== */
2295 convs
->Convert_888_to_565_reverse
2297 srcbits
,-bmpImage
->bytes_per_line
,
2307 const char* srcbits
;
2309 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2311 if (bmpImage
->green_mask
!=0x00ff00 ||
2312 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2314 } else if ((rDst
==0x1f && bmpImage
->red_mask
==0xff) ||
2315 (bDst
==0x1f && bmpImage
->blue_mask
==0xff)) {
2317 /* ==== rgb 0888 bmp -> rgb 555 dib ==== */
2318 /* ==== bgr 0888 bmp -> bgr 555 dib ==== */
2319 convs
->Convert_0888_to_555_asis
2321 srcbits
,-bmpImage
->bytes_per_line
,
2324 /* ==== rgb 0888 bmp -> rgb 565 dib ==== */
2325 /* ==== bgr 0888 bmp -> bgr 565 dib ==== */
2326 convs
->Convert_0888_to_565_asis
2328 srcbits
,-bmpImage
->bytes_per_line
,
2333 /* ==== rgb 0888 bmp -> bgr 555 dib ==== */
2334 /* ==== bgr 0888 bmp -> rgb 555 dib ==== */
2335 convs
->Convert_0888_to_555_reverse
2337 srcbits
,-bmpImage
->bytes_per_line
,
2340 /* ==== rgb 0888 bmp -> bgr 565 dib ==== */
2341 /* ==== bgr 0888 bmp -> rgb 565 dib ==== */
2342 convs
->Convert_0888_to_565_reverse
2344 srcbits
,-bmpImage
->bytes_per_line
,
2353 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
2355 /* ==== pal 1 or 4 bmp -> rgb or bgr 555 or 565 dib ==== */
2356 int rShift
,gShift
,bShift
;
2359 /* Shift everything 16 bits left so that all shifts are >0,
2360 * even for BGR DIBs. Then a single >> 16 will bring everything
2363 rShift
=16+X11DRV_DIB_MaskToShift(rDst
)-3;
2364 gShift
=16+X11DRV_DIB_MaskToShift(gDst
)-3;
2365 bShift
=16+X11DRV_DIB_MaskToShift(bDst
)-3;
2367 /* 6 bits for the green */
2373 for (h
= lines
- 1; h
>= 0; h
--) {
2374 dstpixel
=(LPWORD
)dstbits
;
2375 for (x
= 0; x
< width
; x
++) {
2376 PALETTEENTRY srcval
;
2378 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
2379 dstval
=((srcval
.peRed
<< rShift
) & rDst
) |
2380 ((srcval
.peGreen
<< gShift
) & gDst
) |
2381 ((srcval
.peBlue
<< bShift
) & bDst
);
2382 *dstpixel
++=dstval
>> 16;
2384 dstbits
+= linebytes
;
2392 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
2394 /* ==== pal 8 bmp -> rgb or bgr 555 or 565 dib ==== */
2395 int rShift
,gShift
,bShift
;
2396 const BYTE
* srcbits
;
2397 const BYTE
* srcpixel
;
2400 /* Shift everything 16 bits left so that all shifts are >0,
2401 * even for BGR DIBs. Then a single >> 16 will bring everything
2404 rShift
=16+X11DRV_DIB_MaskToShift(rDst
)-3;
2405 gShift
=16+X11DRV_DIB_MaskToShift(gDst
)-3;
2406 bShift
=16+X11DRV_DIB_MaskToShift(bDst
)-3;
2408 /* 6 bits for the green */
2414 srcbits
=(BYTE
*)bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2415 for (h
=0; h
<lines
; h
++) {
2417 dstpixel
=(LPWORD
)dstbits
;
2418 for (x
= 0; x
< width
; x
++) {
2419 PALETTEENTRY srcval
;
2421 srcval
=srccolors
[*srcpixel
++];
2422 dstval
=((srcval
.peRed
<< rShift
) & rDst
) |
2423 ((srcval
.peGreen
<< gShift
) & gDst
) |
2424 ((srcval
.peBlue
<< bShift
) & bDst
);
2425 *dstpixel
++=dstval
>> 16;
2427 srcbits
-= bmpImage
->bytes_per_line
;
2428 dstbits
+= linebytes
;
2438 /* ==== any bmp format -> rgb or bgr 555 or 565 dib ==== */
2439 int rShift
,gShift
,bShift
;
2442 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 16 bit DIB (%x,%x,%x)\n",
2443 bmpImage
->depth
, bmpImage
->red_mask
,
2444 bmpImage
->green_mask
, bmpImage
->blue_mask
,
2447 /* Shift everything 16 bits left so that all shifts are >0,
2448 * even for BGR DIBs. Then a single >> 16 will bring everything
2451 rShift
=16+X11DRV_DIB_MaskToShift(rDst
)-3;
2452 gShift
=16+X11DRV_DIB_MaskToShift(gDst
)-3;
2453 bShift
=16+X11DRV_DIB_MaskToShift(bDst
)-3;
2455 /* 6 bits for the green */
2461 for (h
= lines
- 1; h
>= 0; h
--) {
2462 dstpixel
=(LPWORD
)dstbits
;
2463 for (x
= 0; x
< width
; x
++) {
2466 srcval
=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage
, x
, h
));
2467 dstval
=((GetRValue(srcval
) << rShift
) & rDst
) |
2468 ((GetGValue(srcval
) << gShift
) & gDst
) |
2469 ((GetBValue(srcval
) << bShift
) & bDst
);
2470 *dstpixel
++=dstval
>> 16;
2472 dstbits
+= linebytes
;
2480 /***********************************************************************
2481 * X11DRV_DIB_SetImageBits_24
2483 * SetDIBits for a 24-bit deep DIB.
2485 static void X11DRV_DIB_SetImageBits_24( int lines
, const BYTE
*srcbits
,
2486 DWORD srcwidth
, DWORD dstwidth
, int left
,
2487 X11DRV_PDEVICE
*physDev
,
2488 DWORD rSrc
, DWORD gSrc
, DWORD bSrc
,
2489 XImage
*bmpImage
, DWORD linebytes
)
2492 int h
, width
= min(srcwidth
, dstwidth
);
2493 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_dst_byteswap
;
2498 srcbits
= srcbits
+ linebytes
* (lines
- 1);
2499 linebytes
= -linebytes
;
2502 switch (bmpImage
->depth
)
2505 if (bmpImage
->bits_per_pixel
==24) {
2508 srcbits
=srcbits
+left
*3;
2509 dstbits
=bmpImage
->data
+left
*3+(lines
-1)*bmpImage
->bytes_per_line
;
2511 if (bmpImage
->green_mask
!=0x00ff00 ||
2512 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2514 } else if (rSrc
==bmpImage
->red_mask
) {
2515 /* ==== rgb 888 dib -> rgb 888 bmp ==== */
2516 /* ==== bgr 888 dib -> bgr 888 bmp ==== */
2517 convs
->Convert_888_asis
2520 dstbits
,-bmpImage
->bytes_per_line
);
2522 /* ==== rgb 888 dib -> bgr 888 bmp ==== */
2523 /* ==== bgr 888 dib -> rgb 888 bmp ==== */
2524 convs
->Convert_888_reverse
2527 dstbits
,-bmpImage
->bytes_per_line
);
2537 srcbits
=srcbits
+left
*3;
2538 dstbits
=bmpImage
->data
+left
*4+(lines
-1)*bmpImage
->bytes_per_line
;
2540 if (bmpImage
->green_mask
!=0x00ff00 ||
2541 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2543 } else if (rSrc
==bmpImage
->red_mask
) {
2544 /* ==== rgb 888 dib -> rgb 0888 bmp ==== */
2545 /* ==== bgr 888 dib -> bgr 0888 bmp ==== */
2546 convs
->Convert_888_to_0888_asis
2549 dstbits
,-bmpImage
->bytes_per_line
);
2551 /* ==== rgb 888 dib -> bgr 0888 bmp ==== */
2552 /* ==== bgr 888 dib -> rgb 0888 bmp ==== */
2553 convs
->Convert_888_to_0888_reverse
2556 dstbits
,-bmpImage
->bytes_per_line
);
2566 srcbits
=srcbits
+left
*3;
2567 dstbits
=bmpImage
->data
+left
*2+(lines
-1)*bmpImage
->bytes_per_line
;
2569 if (bmpImage
->green_mask
==0x03e0) {
2570 if ((rSrc
==0xff0000 && bmpImage
->red_mask
==0x7f00) ||
2571 (bSrc
==0xff0000 && bmpImage
->blue_mask
==0x7f00)) {
2572 /* ==== rgb 888 dib -> rgb 555 bmp ==== */
2573 /* ==== bgr 888 dib -> bgr 555 bmp ==== */
2574 convs
->Convert_888_to_555_asis
2577 dstbits
,-bmpImage
->bytes_per_line
);
2578 } else if ((rSrc
==0xff && bmpImage
->red_mask
==0x7f00) ||
2579 (bSrc
==0xff && bmpImage
->blue_mask
==0x7f00)) {
2580 /* ==== rgb 888 dib -> bgr 555 bmp ==== */
2581 /* ==== bgr 888 dib -> rgb 555 bmp ==== */
2582 convs
->Convert_888_to_555_reverse
2585 dstbits
,-bmpImage
->bytes_per_line
);
2589 } else if (bmpImage
->green_mask
==0x07e0) {
2590 if ((rSrc
==0xff0000 && bmpImage
->red_mask
==0xf800) ||
2591 (bSrc
==0xff0000 && bmpImage
->blue_mask
==0xf800)) {
2592 /* ==== rgb 888 dib -> rgb 565 bmp ==== */
2593 /* ==== bgr 888 dib -> bgr 565 bmp ==== */
2594 convs
->Convert_888_to_565_asis
2597 dstbits
,-bmpImage
->bytes_per_line
);
2598 } else if ((rSrc
==0xff && bmpImage
->red_mask
==0xf800) ||
2599 (bSrc
==0xff && bmpImage
->blue_mask
==0xf800)) {
2600 /* ==== rgb 888 dib -> bgr 565 bmp ==== */
2601 /* ==== bgr 888 dib -> rgb 565 bmp ==== */
2602 convs
->Convert_888_to_565_reverse
2605 dstbits
,-bmpImage
->bytes_per_line
);
2617 WARN("from 24 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
2618 rSrc
, gSrc
, bSrc
, bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
2619 bmpImage
->green_mask
, bmpImage
->blue_mask
);
2625 /* ==== rgb 888 dib -> any bmp bormat ==== */
2626 const BYTE
* srcbyte
;
2628 /* Windows only supports one 24bpp DIB format: RGB888 */
2630 for (h
= lines
- 1; h
>= 0; h
--) {
2631 srcbyte
=(const BYTE
*)srcbits
;
2632 for (x
= left
; x
< width
+left
; x
++) {
2633 XPutPixel(bmpImage
, x
, h
,
2634 X11DRV_PALETTE_ToPhysical
2635 (physDev
, RGB(srcbyte
[2], srcbyte
[1], srcbyte
[0])));
2638 srcbits
+= linebytes
;
2646 /***********************************************************************
2647 * X11DRV_DIB_GetImageBits_24
2649 * GetDIBits for an 24-bit deep DIB.
2651 static void X11DRV_DIB_GetImageBits_24( int lines
, BYTE
*dstbits
,
2652 DWORD dstwidth
, DWORD srcwidth
,
2653 PALETTEENTRY
*srccolors
,
2654 DWORD rDst
, DWORD gDst
, DWORD bDst
,
2655 XImage
*bmpImage
, DWORD linebytes
)
2658 int h
, width
= min(srcwidth
, dstwidth
);
2659 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_src_byteswap
;
2664 dstbits
= dstbits
+ ( linebytes
* (lines
-1) );
2665 linebytes
= -linebytes
;
2668 switch (bmpImage
->depth
)
2671 if (bmpImage
->bits_per_pixel
==24) {
2672 const char* srcbits
;
2674 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2676 if (bmpImage
->green_mask
!=0x00ff00 ||
2677 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2679 } else if (rDst
==bmpImage
->red_mask
) {
2680 /* ==== rgb 888 bmp -> rgb 888 dib ==== */
2681 /* ==== bgr 888 bmp -> bgr 888 dib ==== */
2682 convs
->Convert_888_asis
2684 srcbits
,-bmpImage
->bytes_per_line
,
2687 /* ==== rgb 888 bmp -> bgr 888 dib ==== */
2688 /* ==== bgr 888 bmp -> rgb 888 dib ==== */
2689 convs
->Convert_888_reverse
2691 srcbits
,-bmpImage
->bytes_per_line
,
2700 const char* srcbits
;
2702 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2704 if (bmpImage
->green_mask
!=0x00ff00 ||
2705 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2707 } else if (rDst
==bmpImage
->red_mask
) {
2708 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
2709 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
2710 convs
->Convert_0888_to_888_asis
2712 srcbits
,-bmpImage
->bytes_per_line
,
2715 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
2716 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
2717 convs
->Convert_0888_to_888_reverse
2719 srcbits
,-bmpImage
->bytes_per_line
,
2728 const char* srcbits
;
2730 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2732 if (bmpImage
->green_mask
==0x03e0) {
2733 if ((rDst
==0xff0000 && bmpImage
->red_mask
==0x7f00) ||
2734 (bDst
==0xff0000 && bmpImage
->blue_mask
==0x7f00)) {
2735 /* ==== rgb 555 bmp -> rgb 888 dib ==== */
2736 /* ==== bgr 555 bmp -> bgr 888 dib ==== */
2737 convs
->Convert_555_to_888_asis
2739 srcbits
,-bmpImage
->bytes_per_line
,
2741 } else if ((rDst
==0xff && bmpImage
->red_mask
==0x7f00) ||
2742 (bDst
==0xff && bmpImage
->blue_mask
==0x7f00)) {
2743 /* ==== rgb 555 bmp -> bgr 888 dib ==== */
2744 /* ==== bgr 555 bmp -> rgb 888 dib ==== */
2745 convs
->Convert_555_to_888_reverse
2747 srcbits
,-bmpImage
->bytes_per_line
,
2752 } else if (bmpImage
->green_mask
==0x07e0) {
2753 if ((rDst
==0xff0000 && bmpImage
->red_mask
==0xf800) ||
2754 (bDst
==0xff0000 && bmpImage
->blue_mask
==0xf800)) {
2755 /* ==== rgb 565 bmp -> rgb 888 dib ==== */
2756 /* ==== bgr 565 bmp -> bgr 888 dib ==== */
2757 convs
->Convert_565_to_888_asis
2759 srcbits
,-bmpImage
->bytes_per_line
,
2761 } else if ((rDst
==0xff && bmpImage
->red_mask
==0xf800) ||
2762 (bDst
==0xff && bmpImage
->blue_mask
==0xf800)) {
2763 /* ==== rgb 565 bmp -> bgr 888 dib ==== */
2764 /* ==== bgr 565 bmp -> rgb 888 dib ==== */
2765 convs
->Convert_565_to_888_reverse
2767 srcbits
,-bmpImage
->bytes_per_line
,
2780 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
2782 /* ==== pal 1 or 4 bmp -> rgb 888 dib ==== */
2785 /* Windows only supports one 24bpp DIB format: rgb 888 */
2786 for (h
= lines
- 1; h
>= 0; h
--) {
2788 for (x
= 0; x
< width
; x
++) {
2789 PALETTEENTRY srcval
;
2790 srcval
=srccolors
[XGetPixel(bmpImage
, x
, h
)];
2791 dstbyte
[0]=srcval
.peBlue
;
2792 dstbyte
[1]=srcval
.peGreen
;
2793 dstbyte
[2]=srcval
.peRed
;
2796 dstbits
+= linebytes
;
2804 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
2806 /* ==== pal 8 bmp -> rgb 888 dib ==== */
2807 const void* srcbits
;
2808 const BYTE
* srcpixel
;
2811 /* Windows only supports one 24bpp DIB format: rgb 888 */
2812 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
2813 for (h
= lines
- 1; h
>= 0; h
--) {
2816 for (x
= 0; x
< width
; x
++ ) {
2817 PALETTEENTRY srcval
;
2818 srcval
=srccolors
[*srcpixel
++];
2819 dstbyte
[0]=srcval
.peBlue
;
2820 dstbyte
[1]=srcval
.peGreen
;
2821 dstbyte
[2]=srcval
.peRed
;
2824 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
2825 dstbits
+= linebytes
;
2835 /* ==== any bmp format -> 888 dib ==== */
2838 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 24 bit DIB (%x,%x,%x)\n",
2839 bmpImage
->depth
, bmpImage
->red_mask
,
2840 bmpImage
->green_mask
, bmpImage
->blue_mask
,
2843 /* Windows only supports one 24bpp DIB format: rgb 888 */
2844 for (h
= lines
- 1; h
>= 0; h
--) {
2846 for (x
= 0; x
< width
; x
++) {
2847 COLORREF srcval
=X11DRV_PALETTE_ToLogical
2848 (XGetPixel( bmpImage
, x
, h
));
2849 dstbyte
[0]=GetBValue(srcval
);
2850 dstbyte
[1]=GetGValue(srcval
);
2851 dstbyte
[2]=GetRValue(srcval
);
2854 dstbits
+= linebytes
;
2862 /***********************************************************************
2863 * X11DRV_DIB_SetImageBits_32
2865 * SetDIBits for a 32-bit deep DIB.
2867 static void X11DRV_DIB_SetImageBits_32(int lines
, const BYTE
*srcbits
,
2868 DWORD srcwidth
, DWORD dstwidth
, int left
,
2869 X11DRV_PDEVICE
*physDev
,
2870 DWORD rSrc
, DWORD gSrc
, DWORD bSrc
,
2876 int h
, width
= min(srcwidth
, dstwidth
);
2877 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_dst_byteswap
;
2882 srcbits
= srcbits
+ ( linebytes
* (lines
-1) );
2883 linebytes
= -linebytes
;
2886 ptr
= (const DWORD
*) srcbits
+ left
;
2888 switch (bmpImage
->depth
)
2891 if (bmpImage
->bits_per_pixel
==24) {
2894 srcbits
=srcbits
+left
*4;
2895 dstbits
=bmpImage
->data
+left
*3+(lines
-1)*bmpImage
->bytes_per_line
;
2897 if (rSrc
==bmpImage
->red_mask
&& gSrc
==bmpImage
->green_mask
&& bSrc
==bmpImage
->blue_mask
) {
2898 /* ==== rgb 0888 dib -> rgb 888 bmp ==== */
2899 /* ==== bgr 0888 dib -> bgr 888 bmp ==== */
2900 convs
->Convert_0888_to_888_asis
2903 dstbits
,-bmpImage
->bytes_per_line
);
2904 } else if (bmpImage
->green_mask
!=0x00ff00 ||
2905 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2907 /* the tests below assume sane bmpImage masks */
2908 } else if (rSrc
==bmpImage
->blue_mask
&& gSrc
==bmpImage
->green_mask
&& bSrc
==bmpImage
->red_mask
) {
2909 /* ==== rgb 0888 dib -> bgr 888 bmp ==== */
2910 /* ==== bgr 0888 dib -> rgb 888 bmp ==== */
2911 convs
->Convert_0888_to_888_reverse
2914 dstbits
,-bmpImage
->bytes_per_line
);
2915 } else if (bmpImage
->blue_mask
==0xff) {
2916 /* ==== any 0888 dib -> rgb 888 bmp ==== */
2917 convs
->Convert_any0888_to_rgb888
2921 dstbits
,-bmpImage
->bytes_per_line
);
2923 /* ==== any 0888 dib -> bgr 888 bmp ==== */
2924 convs
->Convert_any0888_to_bgr888
2928 dstbits
,-bmpImage
->bytes_per_line
);
2938 srcbits
=srcbits
+left
*4;
2939 dstbits
=bmpImage
->data
+left
*4+(lines
-1)*bmpImage
->bytes_per_line
;
2941 if (gSrc
==bmpImage
->green_mask
) {
2942 if (rSrc
==bmpImage
->red_mask
&& bSrc
==bmpImage
->blue_mask
) {
2943 /* ==== rgb 0888 dib -> rgb 0888 bmp ==== */
2944 /* ==== bgr 0888 dib -> bgr 0888 bmp ==== */
2945 convs
->Convert_0888_asis
2948 dstbits
,-bmpImage
->bytes_per_line
);
2949 } else if (bmpImage
->green_mask
!=0x00ff00 ||
2950 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2952 /* the tests below assume sane bmpImage masks */
2953 } else if (rSrc
==bmpImage
->blue_mask
&& bSrc
==bmpImage
->red_mask
) {
2954 /* ==== rgb 0888 dib -> bgr 0888 bmp ==== */
2955 /* ==== bgr 0888 dib -> rgb 0888 bmp ==== */
2956 convs
->Convert_0888_reverse
2959 dstbits
,-bmpImage
->bytes_per_line
);
2961 /* ==== any 0888 dib -> any 0888 bmp ==== */
2962 convs
->Convert_0888_any
2966 dstbits
,-bmpImage
->bytes_per_line
,
2967 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
2969 } else if (bmpImage
->green_mask
!=0x00ff00 ||
2970 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
2972 /* the tests below assume sane bmpImage masks */
2974 /* ==== any 0888 dib -> any 0888 bmp ==== */
2975 convs
->Convert_0888_any
2979 dstbits
,-bmpImage
->bytes_per_line
,
2980 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
2990 srcbits
=srcbits
+left
*4;
2991 dstbits
=bmpImage
->data
+left
*2+(lines
-1)*bmpImage
->bytes_per_line
;
2993 if (rSrc
==0xff0000 && gSrc
==0x00ff00 && bSrc
==0x0000ff) {
2994 if (bmpImage
->green_mask
==0x03e0) {
2995 if (bmpImage
->red_mask
==0x7f00) {
2996 /* ==== rgb 0888 dib -> rgb 555 bmp ==== */
2997 convs
->Convert_0888_to_555_asis
3000 dstbits
,-bmpImage
->bytes_per_line
);
3001 } else if (bmpImage
->blue_mask
==0x7f00) {
3002 /* ==== rgb 0888 dib -> bgr 555 bmp ==== */
3003 convs
->Convert_0888_to_555_reverse
3006 dstbits
,-bmpImage
->bytes_per_line
);
3010 } else if (bmpImage
->green_mask
==0x07e0) {
3011 if (bmpImage
->red_mask
==0xf800) {
3012 /* ==== rgb 0888 dib -> rgb 565 bmp ==== */
3013 convs
->Convert_0888_to_565_asis
3016 dstbits
,-bmpImage
->bytes_per_line
);
3017 } else if (bmpImage
->blue_mask
==0xf800) {
3018 /* ==== rgb 0888 dib -> bgr 565 bmp ==== */
3019 convs
->Convert_0888_to_565_reverse
3022 dstbits
,-bmpImage
->bytes_per_line
);
3029 } else if (rSrc
==0x0000ff && gSrc
==0x00ff00 && bSrc
==0xff0000) {
3030 if (bmpImage
->green_mask
==0x03e0) {
3031 if (bmpImage
->blue_mask
==0x7f00) {
3032 /* ==== bgr 0888 dib -> bgr 555 bmp ==== */
3033 convs
->Convert_0888_to_555_asis
3036 dstbits
,-bmpImage
->bytes_per_line
);
3037 } else if (bmpImage
->red_mask
==0x7f00) {
3038 /* ==== bgr 0888 dib -> rgb 555 bmp ==== */
3039 convs
->Convert_0888_to_555_reverse
3042 dstbits
,-bmpImage
->bytes_per_line
);
3046 } else if (bmpImage
->green_mask
==0x07e0) {
3047 if (bmpImage
->blue_mask
==0xf800) {
3048 /* ==== bgr 0888 dib -> bgr 565 bmp ==== */
3049 convs
->Convert_0888_to_565_asis
3052 dstbits
,-bmpImage
->bytes_per_line
);
3053 } else if (bmpImage
->red_mask
==0xf800) {
3054 /* ==== bgr 0888 dib -> rgb 565 bmp ==== */
3055 convs
->Convert_0888_to_565_reverse
3058 dstbits
,-bmpImage
->bytes_per_line
);
3066 if (bmpImage
->green_mask
==0x03e0 &&
3067 (bmpImage
->red_mask
==0x7f00 ||
3068 bmpImage
->blue_mask
==0x7f00)) {
3069 /* ==== any 0888 dib -> rgb or bgr 555 bmp ==== */
3070 convs
->Convert_any0888_to_5x5
3074 dstbits
,-bmpImage
->bytes_per_line
,
3075 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3076 } else if (bmpImage
->green_mask
==0x07e0 &&
3077 (bmpImage
->red_mask
==0xf800 ||
3078 bmpImage
->blue_mask
==0xf800)) {
3079 /* ==== any 0888 dib -> rgb or bgr 565 bmp ==== */
3080 convs
->Convert_any0888_to_5x5
3084 dstbits
,-bmpImage
->bytes_per_line
,
3085 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3095 WARN("from 32 bit DIB (%x,%x,%x) to unknown %d bit bitmap (%lx,%lx,%lx)\n",
3096 rSrc
, gSrc
, bSrc
, bmpImage
->bits_per_pixel
, bmpImage
->red_mask
,
3097 bmpImage
->green_mask
, bmpImage
->blue_mask
);
3103 /* ==== any 0888 dib -> pal 1, 4 or 8 bmp ==== */
3104 const DWORD
* srcpixel
;
3105 int rShift
,gShift
,bShift
;
3107 rShift
=X11DRV_DIB_MaskToShift(rSrc
);
3108 gShift
=X11DRV_DIB_MaskToShift(gSrc
);
3109 bShift
=X11DRV_DIB_MaskToShift(bSrc
);
3111 for (h
= lines
- 1; h
>= 0; h
--) {
3112 srcpixel
=(const DWORD
*)srcbits
;
3113 for (x
= left
; x
< width
+left
; x
++) {
3115 BYTE red
,green
,blue
;
3116 srcvalue
=*srcpixel
++;
3117 red
= (srcvalue
>> rShift
) & 0xff;
3118 green
=(srcvalue
>> gShift
) & 0xff;
3119 blue
= (srcvalue
>> bShift
) & 0xff;
3120 XPutPixel(bmpImage
, x
, h
, X11DRV_PALETTE_ToPhysical
3121 (physDev
, RGB(red
,green
,blue
)));
3123 srcbits
+= linebytes
;
3131 /***********************************************************************
3132 * X11DRV_DIB_GetImageBits_32
3134 * GetDIBits for an 32-bit deep DIB.
3136 static void X11DRV_DIB_GetImageBits_32( int lines
, BYTE
*dstbits
,
3137 DWORD dstwidth
, DWORD srcwidth
,
3138 PALETTEENTRY
*srccolors
,
3139 DWORD rDst
, DWORD gDst
, DWORD bDst
,
3140 XImage
*bmpImage
, DWORD linebytes
)
3143 int h
, width
= min(srcwidth
, dstwidth
);
3145 const dib_conversions
*convs
= (bmpImage
->byte_order
== LSBFirst
) ? &dib_normal
: &dib_src_byteswap
;
3150 dstbits
= dstbits
+ ( linebytes
* (lines
-1) );
3151 linebytes
= -linebytes
;
3156 switch (bmpImage
->depth
)
3159 if (bmpImage
->bits_per_pixel
==24) {
3160 const void* srcbits
;
3162 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
3164 if (rDst
==bmpImage
->red_mask
&& gDst
==bmpImage
->green_mask
&& bDst
==bmpImage
->blue_mask
) {
3165 /* ==== rgb 888 bmp -> rgb 0888 dib ==== */
3166 /* ==== bgr 888 bmp -> bgr 0888 dib ==== */
3167 convs
->Convert_888_to_0888_asis
3169 srcbits
,-bmpImage
->bytes_per_line
,
3171 } else if (bmpImage
->green_mask
!=0x00ff00 ||
3172 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
3174 /* the tests below assume sane bmpImage masks */
3175 } else if (rDst
==bmpImage
->blue_mask
&& gDst
==bmpImage
->green_mask
&& bDst
==bmpImage
->red_mask
) {
3176 /* ==== rgb 888 bmp -> bgr 0888 dib ==== */
3177 /* ==== bgr 888 bmp -> rgb 0888 dib ==== */
3178 convs
->Convert_888_to_0888_reverse
3180 srcbits
,-bmpImage
->bytes_per_line
,
3182 } else if (bmpImage
->blue_mask
==0xff) {
3183 /* ==== rgb 888 bmp -> any 0888 dib ==== */
3184 convs
->Convert_rgb888_to_any0888
3186 srcbits
,-bmpImage
->bytes_per_line
,
3190 /* ==== bgr 888 bmp -> any 0888 dib ==== */
3191 convs
->Convert_bgr888_to_any0888
3193 srcbits
,-bmpImage
->bytes_per_line
,
3203 const char* srcbits
;
3205 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
3207 if (gDst
==bmpImage
->green_mask
) {
3208 if (rDst
==bmpImage
->red_mask
&& bDst
==bmpImage
->blue_mask
) {
3209 /* ==== rgb 0888 bmp -> rgb 0888 dib ==== */
3210 /* ==== bgr 0888 bmp -> bgr 0888 dib ==== */
3211 convs
->Convert_0888_asis
3213 srcbits
,-bmpImage
->bytes_per_line
,
3215 } else if (bmpImage
->green_mask
!=0x00ff00 ||
3216 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
3218 /* the tests below assume sane bmpImage masks */
3219 } else if (rDst
==bmpImage
->blue_mask
&& bDst
==bmpImage
->red_mask
) {
3220 /* ==== rgb 0888 bmp -> bgr 0888 dib ==== */
3221 /* ==== bgr 0888 bmp -> rgb 0888 dib ==== */
3222 convs
->Convert_0888_reverse
3224 srcbits
,-bmpImage
->bytes_per_line
,
3227 /* ==== any 0888 bmp -> any 0888 dib ==== */
3228 convs
->Convert_0888_any
3230 srcbits
,-bmpImage
->bytes_per_line
,
3231 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
,
3235 } else if (bmpImage
->green_mask
!=0x00ff00 ||
3236 (bmpImage
->red_mask
|bmpImage
->blue_mask
)!=0xff00ff) {
3238 /* the tests below assume sane bmpImage masks */
3240 /* ==== any 0888 bmp -> any 0888 dib ==== */
3241 convs
->Convert_0888_any
3243 srcbits
,-bmpImage
->bytes_per_line
,
3244 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
,
3254 const char* srcbits
;
3256 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
3258 if (rDst
==0xff0000 && gDst
==0x00ff00 && bDst
==0x0000ff) {
3259 if (bmpImage
->green_mask
==0x03e0) {
3260 if (bmpImage
->red_mask
==0x7f00) {
3261 /* ==== rgb 555 bmp -> rgb 0888 dib ==== */
3262 convs
->Convert_555_to_0888_asis
3264 srcbits
,-bmpImage
->bytes_per_line
,
3266 } else if (bmpImage
->blue_mask
==0x7f00) {
3267 /* ==== bgr 555 bmp -> rgb 0888 dib ==== */
3268 convs
->Convert_555_to_0888_reverse
3270 srcbits
,-bmpImage
->bytes_per_line
,
3275 } else if (bmpImage
->green_mask
==0x07e0) {
3276 if (bmpImage
->red_mask
==0xf800) {
3277 /* ==== rgb 565 bmp -> rgb 0888 dib ==== */
3278 convs
->Convert_565_to_0888_asis
3280 srcbits
,-bmpImage
->bytes_per_line
,
3282 } else if (bmpImage
->blue_mask
==0xf800) {
3283 /* ==== bgr 565 bmp -> rgb 0888 dib ==== */
3284 convs
->Convert_565_to_0888_reverse
3286 srcbits
,-bmpImage
->bytes_per_line
,
3294 } else if (rDst
==0x0000ff && gDst
==0x00ff00 && bDst
==0xff0000) {
3295 if (bmpImage
->green_mask
==0x03e0) {
3296 if (bmpImage
->blue_mask
==0x7f00) {
3297 /* ==== bgr 555 bmp -> bgr 0888 dib ==== */
3298 convs
->Convert_555_to_0888_asis
3300 srcbits
,-bmpImage
->bytes_per_line
,
3302 } else if (bmpImage
->red_mask
==0x7f00) {
3303 /* ==== rgb 555 bmp -> bgr 0888 dib ==== */
3304 convs
->Convert_555_to_0888_reverse
3306 srcbits
,-bmpImage
->bytes_per_line
,
3311 } else if (bmpImage
->green_mask
==0x07e0) {
3312 if (bmpImage
->blue_mask
==0xf800) {
3313 /* ==== bgr 565 bmp -> bgr 0888 dib ==== */
3314 convs
->Convert_565_to_0888_asis
3316 srcbits
,-bmpImage
->bytes_per_line
,
3318 } else if (bmpImage
->red_mask
==0xf800) {
3319 /* ==== rgb 565 bmp -> bgr 0888 dib ==== */
3320 convs
->Convert_565_to_0888_reverse
3322 srcbits
,-bmpImage
->bytes_per_line
,
3331 if (bmpImage
->green_mask
==0x03e0 &&
3332 (bmpImage
->red_mask
==0x7f00 ||
3333 bmpImage
->blue_mask
==0x7f00)) {
3334 /* ==== rgb or bgr 555 bmp -> any 0888 dib ==== */
3335 convs
->Convert_5x5_to_any0888
3337 srcbits
,-bmpImage
->bytes_per_line
,
3338 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
,
3341 } else if (bmpImage
->green_mask
==0x07e0 &&
3342 (bmpImage
->red_mask
==0xf800 ||
3343 bmpImage
->blue_mask
==0xf800)) {
3344 /* ==== rgb or bgr 565 bmp -> any 0888 dib ==== */
3345 convs
->Convert_5x5_to_any0888
3347 srcbits
,-bmpImage
->bytes_per_line
,
3348 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
,
3360 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
3362 /* ==== pal 1 or 4 bmp -> any 0888 dib ==== */
3363 int rShift
,gShift
,bShift
;
3366 rShift
=X11DRV_DIB_MaskToShift(rDst
);
3367 gShift
=X11DRV_DIB_MaskToShift(gDst
);
3368 bShift
=X11DRV_DIB_MaskToShift(bDst
);
3369 for (h
= lines
- 1; h
>= 0; h
--) {
3370 dstpixel
=(DWORD
*)dstbits
;
3371 for (x
= 0; x
< width
; x
++) {
3372 PALETTEENTRY srcval
;
3373 srcval
= srccolors
[XGetPixel(bmpImage
, x
, h
)];
3374 *dstpixel
++=(srcval
.peRed
<< rShift
) |
3375 (srcval
.peGreen
<< gShift
) |
3376 (srcval
.peBlue
<< bShift
);
3378 dstbits
+= linebytes
;
3386 if (X11DRV_DIB_CheckMask(bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
)
3388 /* ==== pal 8 bmp -> any 0888 dib ==== */
3389 int rShift
,gShift
,bShift
;
3390 const void* srcbits
;
3391 const BYTE
* srcpixel
;
3394 rShift
=X11DRV_DIB_MaskToShift(rDst
);
3395 gShift
=X11DRV_DIB_MaskToShift(gDst
);
3396 bShift
=X11DRV_DIB_MaskToShift(bDst
);
3397 srcbits
=bmpImage
->data
+(lines
-1)*bmpImage
->bytes_per_line
;
3398 for (h
= lines
- 1; h
>= 0; h
--) {
3400 dstpixel
=(DWORD
*)dstbits
;
3401 for (x
= 0; x
< width
; x
++) {
3402 PALETTEENTRY srcval
;
3403 srcval
=srccolors
[*srcpixel
++];
3404 *dstpixel
++=(srcval
.peRed
<< rShift
) |
3405 (srcval
.peGreen
<< gShift
) |
3406 (srcval
.peBlue
<< bShift
);
3408 srcbits
= (const char*)srcbits
- bmpImage
->bytes_per_line
;
3409 dstbits
+= linebytes
;
3419 /* ==== any bmp format -> any 0888 dib ==== */
3420 int rShift
,gShift
,bShift
;
3423 WARN("from unknown %d bit bitmap (%lx,%lx,%lx) to 32 bit DIB (%x,%x,%x)\n",
3424 bmpImage
->depth
, bmpImage
->red_mask
,
3425 bmpImage
->green_mask
, bmpImage
->blue_mask
,
3428 rShift
=X11DRV_DIB_MaskToShift(rDst
);
3429 gShift
=X11DRV_DIB_MaskToShift(gDst
);
3430 bShift
=X11DRV_DIB_MaskToShift(bDst
);
3431 for (h
= lines
- 1; h
>= 0; h
--) {
3432 dstpixel
=(DWORD
*)dstbits
;
3433 for (x
= 0; x
< width
; x
++) {
3435 srcval
=X11DRV_PALETTE_ToLogical(XGetPixel(bmpImage
, x
, h
));
3436 *dstpixel
++=(GetRValue(srcval
) << rShift
) |
3437 (GetGValue(srcval
) << gShift
) |
3438 (GetBValue(srcval
) << bShift
);
3440 dstbits
+= linebytes
;
3447 static int XGetSubImageErrorHandler(Display
*dpy
, XErrorEvent
*event
, void *arg
)
3449 return (event
->request_code
== X_GetImage
&& event
->error_code
== BadMatch
);
3452 /***********************************************************************
3453 * X11DRV_DIB_SetImageBits_GetSubImage
3455 * Helper for X11DRV_DIB_SetImageBits
3457 static void X11DRV_DIB_SetImageBits_GetSubImage(
3458 const X11DRV_DIB_IMAGEBITS_DESCR
*descr
, XImage
*bmpImage
)
3460 /* compressed bitmaps may contain gaps in them. So make a copy
3461 * of the existing pixels first */
3464 SetRect( &bmprc
, descr
->xDest
, descr
->yDest
,
3465 descr
->xDest
+ descr
->width
, descr
->yDest
+ descr
->height
);
3466 GetRgnBox( descr
->physDev
->region
, &rc
);
3467 /* convert from dc to drawable origin */
3468 OffsetRect( &rc
, descr
->physDev
->dc_rect
.left
, descr
->physDev
->dc_rect
.top
);
3469 /* clip visible rect with bitmap */
3470 if( IntersectRect( &rc
, &rc
, &bmprc
))
3472 X11DRV_expect_error( gdi_display
, XGetSubImageErrorHandler
, NULL
);
3473 XGetSubImage( gdi_display
, descr
->drawable
, rc
.left
, rc
.top
,
3474 rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, AllPlanes
,
3476 descr
->xSrc
+ rc
.left
- bmprc
.left
,
3477 descr
->ySrc
+ rc
.top
- bmprc
.top
);
3478 X11DRV_check_error();
3482 /***********************************************************************
3483 * X11DRV_DIB_SetImageBits
3485 * Transfer the bits to an X image.
3486 * Helper function for SetDIBits() and SetDIBitsToDevice().
3488 static int X11DRV_DIB_SetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR
*descr
)
3490 int lines
= descr
->lines
>= 0 ? descr
->lines
: -descr
->lines
;
3495 bmpImage
= descr
->image
;
3497 bmpImage
= XCreateImage( gdi_display
, visual
, descr
->depth
, ZPixmap
, 0, NULL
,
3498 descr
->infoWidth
, lines
, 32, 0 );
3499 bmpImage
->data
= calloc( lines
, bmpImage
->bytes_per_line
);
3500 if(bmpImage
->data
== NULL
) {
3501 ERR("Out of memory!\n");
3502 XDestroyImage( bmpImage
);
3503 wine_tsx11_unlock();
3507 wine_tsx11_unlock();
3509 TRACE("Dib: depth=%d r=%x g=%x b=%x\n",
3510 descr
->infoBpp
,descr
->rMask
,descr
->gMask
,descr
->bMask
);
3511 TRACE("Bmp: depth=%d/%d r=%lx g=%lx b=%lx\n",
3512 bmpImage
->depth
,bmpImage
->bits_per_pixel
,
3513 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3515 /* Transfer the pixels */
3516 switch(descr
->infoBpp
)
3519 X11DRV_DIB_SetImageBits_1( descr
->lines
, descr
->bits
, descr
->infoWidth
,
3520 descr
->width
, descr
->xSrc
, (int *)(descr
->colorMap
),
3521 bmpImage
, descr
->dibpitch
);
3524 if (descr
->compression
) {
3525 X11DRV_DIB_SetImageBits_GetSubImage( descr
, bmpImage
);
3526 X11DRV_DIB_SetImageBits_RLE4( descr
->lines
, descr
->bits
,
3527 descr
->infoWidth
, descr
->width
,
3528 descr
->xSrc
, (int *)(descr
->colorMap
),
3531 X11DRV_DIB_SetImageBits_4( descr
->lines
, descr
->bits
,
3532 descr
->infoWidth
, descr
->width
,
3533 descr
->xSrc
, (int*)(descr
->colorMap
),
3534 bmpImage
, descr
->dibpitch
);
3537 if (descr
->compression
) {
3538 X11DRV_DIB_SetImageBits_GetSubImage( descr
, bmpImage
);
3539 X11DRV_DIB_SetImageBits_RLE8( descr
->lines
, descr
->bits
,
3540 descr
->infoWidth
, descr
->width
,
3541 descr
->xSrc
, (int *)(descr
->colorMap
),
3544 X11DRV_DIB_SetImageBits_8( descr
->lines
, descr
->bits
,
3545 descr
->infoWidth
, descr
->width
,
3546 descr
->xSrc
, (int *)(descr
->colorMap
),
3547 bmpImage
, descr
->dibpitch
);
3551 X11DRV_DIB_SetImageBits_16( descr
->lines
, descr
->bits
,
3552 descr
->infoWidth
, descr
->width
,
3553 descr
->xSrc
, descr
->physDev
,
3554 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3555 bmpImage
, descr
->dibpitch
);
3558 X11DRV_DIB_SetImageBits_24( descr
->lines
, descr
->bits
,
3559 descr
->infoWidth
, descr
->width
,
3560 descr
->xSrc
, descr
->physDev
,
3561 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3562 bmpImage
, descr
->dibpitch
);
3565 X11DRV_DIB_SetImageBits_32( descr
->lines
, descr
->bits
,
3566 descr
->infoWidth
, descr
->width
,
3567 descr
->xSrc
, descr
->physDev
,
3568 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3569 bmpImage
, descr
->dibpitch
);
3572 WARN("(%d): Invalid depth\n", descr
->infoBpp
);
3576 TRACE("XPutImage(%ld,%p,%p,%d,%d,%d,%d,%d,%d)\n",
3577 descr
->drawable
, descr
->gc
, bmpImage
,
3578 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
3579 descr
->width
, descr
->height
);
3582 #ifdef HAVE_LIBXXSHM
3583 if (descr
->image
&& descr
->useShm
)
3585 XShmPutImage( gdi_display
, descr
->drawable
, descr
->gc
, bmpImage
,
3586 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
3587 descr
->width
, descr
->height
, FALSE
);
3588 XSync( gdi_display
, 0 );
3592 XPutImage( gdi_display
, descr
->drawable
, descr
->gc
, bmpImage
,
3593 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
3594 descr
->width
, descr
->height
);
3596 if (!descr
->image
) XDestroyImage( bmpImage
);
3597 wine_tsx11_unlock();
3601 /***********************************************************************
3602 * X11DRV_DIB_GetImageBits
3604 * Transfer the bits from an X image.
3606 static int X11DRV_DIB_GetImageBits( const X11DRV_DIB_IMAGEBITS_DESCR
*descr
)
3608 int lines
= descr
->lines
>= 0 ? descr
->lines
: -descr
->lines
;
3613 bmpImage
= descr
->image
;
3615 bmpImage
= XCreateImage( gdi_display
, visual
, descr
->depth
, ZPixmap
, 0, NULL
,
3616 descr
->infoWidth
, lines
, 32, 0 );
3617 bmpImage
->data
= calloc( lines
, bmpImage
->bytes_per_line
);
3618 if(bmpImage
->data
== NULL
) {
3619 ERR("Out of memory!\n");
3620 XDestroyImage( bmpImage
);
3621 wine_tsx11_unlock();
3626 #ifdef HAVE_LIBXXSHM
3628 /* We must not call XShmGetImage() with a bitmap which is bigger than the available area.
3629 If we do, XShmGetImage() will fail (X exception), as it checks for this internally. */
3630 if((descr
->image
&& descr
->useShm
) && (bmpImage
->width
<= (descr
->width
- descr
->xSrc
))
3631 && (bmpImage
->height
<= (descr
->height
- descr
->ySrc
)))
3633 int saveRed
, saveGreen
, saveBlue
;
3635 TRACE("XShmGetImage(%p, %ld, %p, %d, %d, %ld)\n",
3636 gdi_display
, descr
->drawable
, bmpImage
,
3637 descr
->xSrc
, descr
->ySrc
, AllPlanes
);
3639 /* We must save and restore the bmpImage's masks in order
3640 * to preserve them across the call to XShmGetImage, which
3641 * decides to eleminate them since it doesn't happen to know
3642 * what the format of the image is supposed to be, even though
3644 saveRed
= bmpImage
->red_mask
;
3645 saveBlue
= bmpImage
->blue_mask
;
3646 saveGreen
= bmpImage
->green_mask
;
3648 XShmGetImage( gdi_display
, descr
->drawable
, bmpImage
,
3649 descr
->xSrc
, descr
->ySrc
, AllPlanes
);
3651 bmpImage
->red_mask
= saveRed
;
3652 bmpImage
->blue_mask
= saveBlue
;
3653 bmpImage
->green_mask
= saveGreen
;
3656 #endif /* HAVE_LIBXXSHM */
3658 TRACE("XGetSubImage(%p,%ld,%d,%d,%d,%d,%ld,%d,%p,%d,%d)\n",
3659 gdi_display
, descr
->drawable
, descr
->xSrc
, descr
->ySrc
, descr
->width
,
3660 lines
, AllPlanes
, ZPixmap
, bmpImage
, descr
->xDest
, descr
->yDest
);
3661 XGetSubImage( gdi_display
, descr
->drawable
, descr
->xSrc
, descr
->ySrc
,
3662 descr
->width
, lines
, AllPlanes
, ZPixmap
,
3663 bmpImage
, descr
->xDest
, descr
->yDest
);
3665 wine_tsx11_unlock();
3667 TRACE("Dib: depth=%2d r=%x g=%x b=%x\n",
3668 descr
->infoBpp
,descr
->rMask
,descr
->gMask
,descr
->bMask
);
3669 TRACE("Bmp: depth=%2d/%2d r=%lx g=%lx b=%lx\n",
3670 bmpImage
->depth
,bmpImage
->bits_per_pixel
,
3671 bmpImage
->red_mask
,bmpImage
->green_mask
,bmpImage
->blue_mask
);
3672 /* Transfer the pixels */
3673 switch(descr
->infoBpp
)
3676 X11DRV_DIB_GetImageBits_1( descr
->lines
,(LPVOID
)descr
->bits
,
3677 descr
->infoWidth
, descr
->width
,
3678 descr
->colorMap
, descr
->palentry
,
3679 bmpImage
, descr
->dibpitch
);
3683 if (descr
->compression
) {
3684 FIXME("Compression not yet supported!\n");
3685 if(descr
->sizeImage
< X11DRV_DIB_GetDIBWidthBytes( descr
->infoWidth
, 4 ) * abs(descr
->lines
))
3688 X11DRV_DIB_GetImageBits_4( descr
->lines
,(LPVOID
)descr
->bits
,
3689 descr
->infoWidth
, descr
->width
,
3690 descr
->colorMap
, descr
->palentry
,
3691 bmpImage
, descr
->dibpitch
);
3694 if (descr
->compression
) {
3695 FIXME("Compression not yet supported!\n");
3696 if(descr
->sizeImage
< X11DRV_DIB_GetDIBWidthBytes( descr
->infoWidth
, 8 ) * abs(descr
->lines
))
3699 X11DRV_DIB_GetImageBits_8( descr
->lines
, (LPVOID
)descr
->bits
,
3700 descr
->infoWidth
, descr
->width
,
3701 descr
->colorMap
, descr
->palentry
,
3702 bmpImage
, descr
->dibpitch
);
3706 X11DRV_DIB_GetImageBits_16( descr
->lines
, (LPVOID
)descr
->bits
,
3707 descr
->infoWidth
,descr
->width
,
3709 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3710 bmpImage
, descr
->dibpitch
);
3714 X11DRV_DIB_GetImageBits_24( descr
->lines
, (LPVOID
)descr
->bits
,
3715 descr
->infoWidth
,descr
->width
,
3717 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3718 bmpImage
, descr
->dibpitch
);
3722 X11DRV_DIB_GetImageBits_32( descr
->lines
, (LPVOID
)descr
->bits
,
3723 descr
->infoWidth
, descr
->width
,
3725 descr
->rMask
, descr
->gMask
, descr
->bMask
,
3726 bmpImage
, descr
->dibpitch
);
3730 WARN("(%d): Invalid depth\n", descr
->infoBpp
);
3737 XDestroyImage( bmpImage
);
3738 wine_tsx11_unlock();
3743 /*************************************************************************
3744 * X11DRV_SetDIBitsToDevice
3747 INT
X11DRV_SetDIBitsToDevice( X11DRV_PDEVICE
*physDev
, INT xDest
, INT yDest
, DWORD cx
,
3748 DWORD cy
, INT xSrc
, INT ySrc
,
3749 UINT startscan
, UINT lines
, LPCVOID bits
,
3750 const BITMAPINFO
*info
, UINT coloruse
)
3752 X11DRV_DIB_IMAGEBITS_DESCR descr
;
3757 int rop
= X11DRV_XROPfunction
[GetROP2(physDev
->hdc
) - 1];
3759 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
,
3760 &descr
.infoBpp
, &descr
.compression
) == -1)
3763 top_down
= (height
< 0);
3764 if (top_down
) height
= -height
;
3768 LPtoDP(physDev
->hdc
, &pt
, 1);
3770 if (!lines
|| (startscan
>= height
)) return 0;
3771 if (!top_down
&& startscan
+ lines
> height
) lines
= height
- startscan
;
3773 /* make xSrc,ySrc point to the upper-left corner, not the lower-left one,
3774 * and clamp all values to fit inside [startscan,startscan+lines]
3776 if (ySrc
+ cy
<= startscan
+ lines
)
3778 UINT y
= startscan
+ lines
- (ySrc
+ cy
);
3779 if (ySrc
< startscan
) cy
-= (startscan
- ySrc
);
3782 /* avoid getting unnecessary lines */
3784 if (y
>= lines
) return 0;
3789 if (y
>= lines
) return lines
;
3790 ySrc
= y
; /* need to get all lines in top down mode */
3795 if (ySrc
>= startscan
+ lines
) return lines
;
3796 pt
.y
+= ySrc
+ cy
- (startscan
+ lines
);
3797 cy
= startscan
+ lines
- ySrc
;
3799 if (cy
> lines
) cy
= lines
;
3801 if (xSrc
>= width
) return lines
;
3802 if (xSrc
+ cx
>= width
) cx
= width
- xSrc
;
3803 if (!cx
|| !cy
) return lines
;
3805 /* Update the pixmap from the DIB section */
3806 X11DRV_LockDIBSection(physDev
, DIB_Status_GdiMod
);
3808 X11DRV_SetupGCForText( physDev
); /* To have the correct colors */
3810 XSetFunction(gdi_display
, physDev
->gc
, rop
);
3811 wine_tsx11_unlock();
3813 switch (descr
.infoBpp
)
3818 descr
.colorMap
= (RGBQUAD
*)X11DRV_DIB_BuildColorMap(
3820 physDev
->depth
, info
, &descr
.nColorMap
);
3821 if (!descr
.colorMap
) return 0;
3822 descr
.rMask
= descr
.gMask
= descr
.bMask
= 0;
3826 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0x7c00;
3827 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x03e0;
3828 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x001f;
3834 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0xff0000;
3835 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x00ff00;
3836 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x0000ff;
3841 descr
.physDev
= physDev
;
3844 descr
.palentry
= NULL
;
3845 descr
.lines
= top_down
? -lines
: lines
;
3846 descr
.infoWidth
= width
;
3847 descr
.depth
= physDev
->depth
;
3848 descr
.drawable
= physDev
->drawable
;
3849 descr
.gc
= physDev
->gc
;
3852 descr
.xDest
= physDev
->dc_rect
.left
+ pt
.x
;
3853 descr
.yDest
= physDev
->dc_rect
.top
+ pt
.y
;
3856 descr
.useShm
= FALSE
;
3857 descr
.dibpitch
= ((width
* descr
.infoBpp
+ 31) &~31) / 8;
3859 result
= X11DRV_DIB_SetImageBits( &descr
);
3861 if (descr
.infoBpp
<= 8)
3862 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
3864 /* Update the DIBSection of the pixmap */
3865 X11DRV_UnlockDIBSection(physDev
, TRUE
);
3870 /***********************************************************************
3871 * SetDIBits (X11DRV.@)
3873 INT
X11DRV_SetDIBits( X11DRV_PDEVICE
*physDev
, HBITMAP hbitmap
, UINT startscan
,
3874 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
, UINT coloruse
)
3876 X_PHYSBITMAP
*physBitmap
= X11DRV_get_phys_bitmap( hbitmap
);
3877 X11DRV_DIB_IMAGEBITS_DESCR descr
;
3879 LONG width
, height
, tmpheight
;
3882 descr
.physDev
= physDev
;
3884 if (!physBitmap
) return 0;
3886 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
,
3887 &descr
.infoBpp
, &descr
.compression
) == -1)
3891 if (height
< 0) height
= -height
;
3892 if (!lines
|| (startscan
>= height
))
3895 if (!GetObjectW( hbitmap
, sizeof(bitmap
), &bitmap
)) return 0;
3897 if (startscan
+ lines
> height
) lines
= height
- startscan
;
3899 switch (descr
.infoBpp
)
3904 descr
.colorMap
= (RGBQUAD
*)X11DRV_DIB_BuildColorMap(
3905 descr
.physDev
, coloruse
,
3906 physBitmap
->pixmap_depth
,
3907 info
, &descr
.nColorMap
);
3908 if (!descr
.colorMap
) return 0;
3909 descr
.rMask
= descr
.gMask
= descr
.bMask
= 0;
3913 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0x7c00;
3914 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x03e0;
3915 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x001f;
3921 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0xff0000;
3922 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x00ff00;
3923 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x0000ff;
3932 descr
.palentry
= NULL
;
3933 descr
.infoWidth
= width
;
3934 descr
.lines
= tmpheight
>= 0 ? lines
: -lines
;
3935 descr
.depth
= physBitmap
->pixmap_depth
;
3936 descr
.drawable
= physBitmap
->pixmap
;
3937 descr
.gc
= BITMAP_GC(physBitmap
);
3941 descr
.yDest
= height
- startscan
- lines
;
3942 descr
.width
= bitmap
.bmWidth
;
3943 descr
.height
= lines
;
3944 descr
.useShm
= FALSE
;
3945 descr
.dibpitch
= ((descr
.infoWidth
* descr
.infoBpp
+ 31) &~31) / 8;
3946 X11DRV_DIB_Lock( physBitmap
, DIB_Status_GdiMod
);
3947 result
= X11DRV_DIB_SetImageBits( &descr
);
3948 X11DRV_DIB_Unlock( physBitmap
, TRUE
);
3950 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
3955 /***********************************************************************
3956 * GetDIBits (X11DRV.@)
3958 INT
X11DRV_GetDIBits( X11DRV_PDEVICE
*physDev
, HBITMAP hbitmap
, UINT startscan
, UINT lines
,
3959 LPVOID bits
, BITMAPINFO
*info
, UINT coloruse
)
3961 X_PHYSBITMAP
*physBitmap
= X11DRV_get_phys_bitmap( hbitmap
);
3963 X11DRV_DIB_IMAGEBITS_DESCR descr
;
3964 PALETTEENTRY palette
[256];
3967 LONG width
, tempHeight
;
3972 GetPaletteEntries( GetCurrentObject( physDev
->hdc
, OBJ_PAL
), 0, 256, palette
);
3974 if (!physBitmap
) return 0;
3975 if (!(obj_size
= GetObjectW( hbitmap
, sizeof(dib
), &dib
))) return 0;
3977 bitmap_type
= DIB_GetBitmapInfo( (BITMAPINFOHEADER
*)info
, &width
, &tempHeight
, &descr
.infoBpp
, &descr
.compression
);
3978 descr
.lines
= tempHeight
;
3979 if (bitmap_type
== -1)
3981 ERR("Invalid bitmap\n");
3984 core_header
= (bitmap_type
== 0);
3985 colorPtr
= (LPBYTE
) info
+ (WORD
) info
->bmiHeader
.biSize
;
3987 TRACE("%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
3988 lines
, dib
.dsBm
.bmWidth
, dib
.dsBm
.bmHeight
, width
, descr
.lines
, startscan
);
3990 if( lines
> dib
.dsBm
.bmHeight
) lines
= dib
.dsBm
.bmHeight
;
3992 height
= descr
.lines
;
3993 if (height
< 0) height
= -height
;
3994 if( lines
> height
) lines
= height
;
3995 /* Top-down images have a negative biHeight, the scanlines of these images
3996 * were inverted in X11DRV_DIB_GetImageBits_xx
3997 * To prevent this we simply change the sign of lines
3998 * (the number of scan lines to copy).
3999 * Negative lines are correctly handled by X11DRV_DIB_GetImageBits_xx.
4001 if( descr
.lines
< 0 && lines
> 0) lines
= -lines
;
4003 if( startscan
>= dib
.dsBm
.bmHeight
) return 0;
4005 descr
.colorMap
= NULL
;
4007 switch (descr
.infoBpp
)
4012 descr
.rMask
= descr
.gMask
= descr
.bMask
= 0;
4013 if(coloruse
== DIB_RGB_COLORS
)
4014 descr
.colorMap
= colorPtr
;
4016 int num_colors
= 1 << descr
.infoBpp
, i
;
4019 WORD
*index
= (WORD
*)colorPtr
;
4020 descr
.colorMap
= rgb
= HeapAlloc(GetProcessHeap(), 0, num_colors
* sizeof(RGBQUAD
));
4021 for(i
= 0; i
< num_colors
; i
++, rgb
++, index
++) {
4022 colref
= X11DRV_PALETTE_ToLogical(X11DRV_PALETTE_ToPhysical(physDev
, PALETTEINDEX(*index
)));
4023 rgb
->rgbRed
= GetRValue(colref
);
4024 rgb
->rgbGreen
= GetGValue(colref
);
4025 rgb
->rgbBlue
= GetBValue(colref
);
4026 rgb
->rgbReserved
= 0;
4032 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0x7c00;
4033 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x03e0;
4034 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x001f;
4038 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? *(const DWORD
*)info
->bmiColors
: 0xff0000;
4039 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 1) : 0x00ff00;
4040 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? *((const DWORD
*)info
->bmiColors
+ 2) : 0x0000ff;
4044 descr
.physDev
= physDev
;
4045 descr
.palentry
= palette
;
4047 descr
.image
= physBitmap
->image
;
4048 descr
.infoWidth
= width
;
4049 descr
.lines
= lines
;
4050 descr
.depth
= physBitmap
->pixmap_depth
;
4051 descr
.drawable
= physBitmap
->pixmap
;
4052 descr
.gc
= BITMAP_GC(physBitmap
);
4053 descr
.width
= dib
.dsBm
.bmWidth
;
4054 descr
.height
= dib
.dsBm
.bmHeight
;
4058 descr
.sizeImage
= core_header
? 0 : info
->bmiHeader
.biSizeImage
;
4060 if (descr
.lines
> 0)
4062 descr
.ySrc
= (descr
.height
-1) - (startscan
+ (lines
-1));
4066 descr
.ySrc
= startscan
;
4068 #ifdef HAVE_LIBXXSHM
4069 descr
.useShm
= (obj_size
== sizeof(DIBSECTION
)) && (physBitmap
->shminfo
.shmid
!= -1);
4071 descr
.useShm
= FALSE
;
4073 descr
.dibpitch
= (obj_size
== sizeof(DIBSECTION
)) ? dib
.dsBm
.bmWidthBytes
4074 : (((descr
.infoWidth
* descr
.infoBpp
+ 31) &~31) / 8);
4076 X11DRV_DIB_Lock( physBitmap
, DIB_Status_GdiMod
);
4077 X11DRV_DIB_GetImageBits( &descr
);
4078 X11DRV_DIB_Unlock( physBitmap
, TRUE
);
4080 if(!core_header
&& info
->bmiHeader
.biSizeImage
== 0) /* Fill in biSizeImage */
4081 info
->bmiHeader
.biSizeImage
= X11DRV_DIB_GetDIBImageBytes( descr
.infoWidth
,
4085 if (descr
.compression
== BI_BITFIELDS
)
4087 *(DWORD
*)info
->bmiColors
= descr
.rMask
;
4088 *((DWORD
*)info
->bmiColors
+ 1) = descr
.gMask
;
4089 *((DWORD
*)info
->bmiColors
+ 2) = descr
.bMask
;
4091 else if (!core_header
)
4093 /* if RLE or JPEG compression were supported,
4094 * this line would be invalid. */
4095 info
->bmiHeader
.biCompression
= 0;
4098 if(descr
.colorMap
!= colorPtr
)
4099 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
4103 /***********************************************************************
4104 * DIB_DoProtectDIBSection
4106 static void X11DRV_DIB_DoProtectDIBSection( X_PHYSBITMAP
*physBitmap
, DWORD new_prot
)
4110 VirtualProtect(physBitmap
->base
, physBitmap
->size
, new_prot
, &old_prot
);
4111 TRACE("Changed protection from %d to %d\n", old_prot
, new_prot
);
4114 /***********************************************************************
4115 * X11DRV_DIB_DoCopyDIBSection
4117 static void X11DRV_DIB_DoCopyDIBSection(X_PHYSBITMAP
*physBitmap
, BOOL toDIB
,
4118 void *colorMap
, int nColorMap
,
4119 Drawable dest
, GC gc
,
4120 DWORD xSrc
, DWORD ySrc
,
4121 DWORD xDest
, DWORD yDest
,
4122 DWORD width
, DWORD height
)
4124 DIBSECTION dibSection
;
4125 X11DRV_DIB_IMAGEBITS_DESCR descr
;
4126 int identity
[2] = {0,1};
4128 if (!GetObjectW( physBitmap
->hbitmap
, sizeof(dibSection
), &dibSection
)) return;
4130 descr
.physDev
= NULL
;
4131 descr
.palentry
= NULL
;
4132 descr
.infoWidth
= dibSection
.dsBmih
.biWidth
;
4133 descr
.infoBpp
= dibSection
.dsBmih
.biBitCount
;
4134 descr
.lines
= dibSection
.dsBmih
.biHeight
;
4135 descr
.image
= physBitmap
->image
;
4136 descr
.colorMap
= colorMap
;
4137 descr
.nColorMap
= nColorMap
;
4138 descr
.bits
= dibSection
.dsBm
.bmBits
;
4139 descr
.depth
= physBitmap
->pixmap_depth
;
4140 descr
.compression
= dibSection
.dsBmih
.biCompression
;
4142 if(descr
.infoBpp
== 1)
4143 descr
.colorMap
= (void*)identity
;
4145 switch (descr
.infoBpp
)
4150 descr
.rMask
= descr
.gMask
= descr
.bMask
= 0;
4154 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[0] : 0x7c00;
4155 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[1] : 0x03e0;
4156 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[2] : 0x001f;
4161 descr
.rMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[0] : 0xff0000;
4162 descr
.gMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[1] : 0x00ff00;
4163 descr
.bMask
= (descr
.compression
== BI_BITFIELDS
) ? dibSection
.dsBitfields
[2] : 0x0000ff;
4168 descr
.drawable
= dest
;
4172 descr
.xDest
= xDest
;
4173 descr
.yDest
= yDest
;
4174 descr
.width
= width
;
4175 descr
.height
= height
;
4176 descr
.sizeImage
= 0;
4178 #ifdef HAVE_LIBXXSHM
4179 descr
.useShm
= (physBitmap
->shminfo
.shmid
!= -1);
4181 descr
.useShm
= FALSE
;
4183 descr
.dibpitch
= dibSection
.dsBm
.bmWidthBytes
;
4187 TRACE("Copying from Pixmap to DIB bits\n");
4188 X11DRV_DIB_GetImageBits( &descr
);
4192 TRACE("Copying from DIB bits to Pixmap\n");
4193 X11DRV_DIB_SetImageBits( &descr
);
4197 /***********************************************************************
4198 * X11DRV_DIB_CopyDIBSection
4200 void X11DRV_DIB_CopyDIBSection(X11DRV_PDEVICE
*physDevSrc
, X11DRV_PDEVICE
*physDevDst
,
4201 DWORD xSrc
, DWORD ySrc
, DWORD xDest
, DWORD yDest
,
4202 DWORD width
, DWORD height
)
4205 X_PHYSBITMAP
*physBitmap
;
4206 unsigned int nColorMap
;
4210 TRACE("(%p,%p,%d,%d,%d,%d,%d,%d)\n", physDevSrc
->hdc
, physDevDst
->hdc
,
4211 xSrc
, ySrc
, xDest
, yDest
, width
, height
);
4212 /* this function is meant as an optimization for BitBlt,
4213 * not to be called otherwise */
4214 physBitmap
= physDevSrc
->bitmap
;
4215 if (!physBitmap
|| GetObjectW( physBitmap
->hbitmap
, sizeof(dib
), &dib
) != sizeof(dib
))
4217 ERR("called for non-DIBSection!?\n");
4220 /* while BitBlt should already have made sure we only get
4221 * positive values, we should check for oversize values */
4222 if ((xSrc
< dib
.dsBm
.bmWidth
) &&
4223 (ySrc
< dib
.dsBm
.bmHeight
)) {
4224 if (xSrc
+ width
> dib
.dsBm
.bmWidth
)
4225 width
= dib
.dsBm
.bmWidth
- xSrc
;
4226 if (ySrc
+ height
> dib
.dsBm
.bmHeight
)
4227 height
= dib
.dsBm
.bmHeight
- ySrc
;
4228 /* if the source bitmap is 8bpp or less, we're supposed to use the
4229 * DC's palette for color conversion (not the DIB color table) */
4230 if (dib
.dsBm
.bmBitsPixel
<= 8) {
4231 HPALETTE hPalette
= GetCurrentObject( physDevSrc
->hdc
, OBJ_PAL
);
4232 if (!hPalette
|| (hPalette
== GetStockObject(DEFAULT_PALETTE
))) {
4233 /* HACK: no palette has been set in the source DC,
4234 * use the DIB colormap instead - this is necessary in some
4235 * cases since we need to do depth conversion in some places
4236 * where real Windows can just copy data straight over */
4237 x11ColorMap
= physBitmap
->colorMap
;
4238 nColorMap
= physBitmap
->nColorMap
;
4239 freeColorMap
= FALSE
;
4241 const BITMAPINFO
* info
= (BITMAPINFO
*)&dib
.dsBmih
;
4244 nColorMap
= X11DRV_DIB_GetColorCount(info
);
4245 x11ColorMap
= HeapAlloc(GetProcessHeap(), 0, nColorMap
* sizeof(int));
4246 for (i
= 0; i
< nColorMap
; i
++)
4247 x11ColorMap
[i
] = X11DRV_PALETTE_ToPhysical(physDevSrc
, PALETTEINDEX(i
));
4248 freeColorMap
= TRUE
;
4255 freeColorMap
= FALSE
;
4257 /* perform the copy */
4258 X11DRV_DIB_DoCopyDIBSection(physBitmap
, FALSE
, x11ColorMap
, nColorMap
,
4259 physDevDst
->drawable
, physDevDst
->gc
, xSrc
, ySrc
,
4260 physDevDst
->dc_rect
.left
+ xDest
, physDevDst
->dc_rect
.top
+ yDest
,
4262 /* free color mapping */
4264 HeapFree(GetProcessHeap(), 0, x11ColorMap
);
4268 /***********************************************************************
4269 * X11DRV_DIB_DoUpdateDIBSection
4271 static void X11DRV_DIB_DoUpdateDIBSection(X_PHYSBITMAP
*physBitmap
, BOOL toDIB
)
4275 GetObjectW( physBitmap
->hbitmap
, sizeof(bitmap
), &bitmap
);
4276 X11DRV_DIB_DoCopyDIBSection(physBitmap
, toDIB
,
4277 physBitmap
->colorMap
, physBitmap
->nColorMap
,
4278 physBitmap
->pixmap
, BITMAP_GC(physBitmap
),
4279 0, 0, 0, 0, bitmap
.bmWidth
, bitmap
.bmHeight
);
4282 /***********************************************************************
4283 * X11DRV_DIB_FaultHandler
4285 static LONG CALLBACK
X11DRV_DIB_FaultHandler( PEXCEPTION_POINTERS ep
)
4287 X_PHYSBITMAP
*physBitmap
= NULL
;
4292 if (ep
->ExceptionRecord
->ExceptionCode
!= EXCEPTION_ACCESS_VIOLATION
)
4293 return EXCEPTION_CONTINUE_SEARCH
;
4295 addr
= (BYTE
*)ep
->ExceptionRecord
->ExceptionInformation
[1];
4297 EnterCriticalSection(&dibs_cs
);
4298 LIST_FOR_EACH( ptr
, &dibs_list
)
4300 physBitmap
= LIST_ENTRY( ptr
, X_PHYSBITMAP
, entry
);
4301 if ((physBitmap
->base
<= addr
) && (addr
< physBitmap
->base
+ physBitmap
->size
))
4307 LeaveCriticalSection(&dibs_cs
);
4309 if (!found
) return EXCEPTION_CONTINUE_SEARCH
;
4311 X11DRV_DIB_Lock( physBitmap
, DIB_Status_None
);
4312 if (ep
->ExceptionRecord
->ExceptionInformation
[0] == EXCEPTION_WRITE_FAULT
) {
4313 /* the app tried to write the DIB bits */
4314 X11DRV_DIB_Coerce( physBitmap
, DIB_Status_AppMod
);
4316 /* the app tried to read the DIB bits */
4317 X11DRV_DIB_Coerce( physBitmap
, DIB_Status_InSync
);
4319 X11DRV_DIB_Unlock( physBitmap
, TRUE
);
4321 return EXCEPTION_CONTINUE_EXECUTION
;
4324 /***********************************************************************
4327 static INT
X11DRV_DIB_Coerce(X_PHYSBITMAP
*physBitmap
, INT req
)
4329 INT ret
= DIB_Status_None
;
4331 if (!physBitmap
->image
) return ret
; /* not a DIB section */
4332 EnterCriticalSection(&physBitmap
->lock
);
4333 ret
= physBitmap
->status
;
4335 case DIB_Status_GdiMod
:
4336 /* GDI access - request to draw on pixmap */
4337 switch (physBitmap
->status
)
4340 case DIB_Status_None
:
4341 physBitmap
->p_status
= DIB_Status_GdiMod
;
4342 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, FALSE
);
4345 case DIB_Status_GdiMod
:
4346 TRACE("GdiMod requested in status GdiMod\n" );
4347 physBitmap
->p_status
= DIB_Status_GdiMod
;
4350 case DIB_Status_InSync
:
4351 TRACE("GdiMod requested in status InSync\n" );
4352 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_NOACCESS
);
4353 physBitmap
->status
= DIB_Status_GdiMod
;
4354 physBitmap
->p_status
= DIB_Status_InSync
;
4357 case DIB_Status_AppMod
:
4358 TRACE("GdiMod requested in status AppMod\n" );
4359 /* make it readonly to avoid app changing data while we copy */
4360 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READONLY
);
4361 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, FALSE
);
4362 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_NOACCESS
);
4363 physBitmap
->p_status
= DIB_Status_AppMod
;
4364 physBitmap
->status
= DIB_Status_GdiMod
;
4369 case DIB_Status_InSync
:
4370 /* App access - request access to read DIB surface */
4371 /* (typically called from signal handler) */
4372 switch (physBitmap
->status
)
4375 case DIB_Status_None
:
4376 /* shouldn't happen from signal handler */
4379 case DIB_Status_GdiMod
:
4380 TRACE("InSync requested in status GdiMod\n" );
4381 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
4382 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, TRUE
);
4383 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READONLY
);
4384 physBitmap
->status
= DIB_Status_InSync
;
4387 case DIB_Status_InSync
:
4388 TRACE("InSync requested in status InSync\n" );
4389 /* shouldn't happen from signal handler */
4392 case DIB_Status_AppMod
:
4393 TRACE("InSync requested in status AppMod\n" );
4394 /* no reason to do anything here, and this
4395 * shouldn't happen from signal handler */
4400 case DIB_Status_AppMod
:
4401 /* App access - request access to write DIB surface */
4402 /* (typically called from signal handler) */
4403 switch (physBitmap
->status
)
4406 case DIB_Status_None
:
4407 /* shouldn't happen from signal handler */
4410 case DIB_Status_GdiMod
:
4411 TRACE("AppMod requested in status GdiMod\n" );
4412 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
4413 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, TRUE
);
4414 physBitmap
->status
= DIB_Status_AppMod
;
4417 case DIB_Status_InSync
:
4418 TRACE("AppMod requested in status InSync\n" );
4419 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
4420 physBitmap
->status
= DIB_Status_AppMod
;
4423 case DIB_Status_AppMod
:
4424 TRACE("AppMod requested in status AppMod\n" );
4425 /* shouldn't happen from signal handler */
4430 /* it is up to the caller to do the copy/conversion, probably
4431 * using the return value to decide where to copy from */
4433 LeaveCriticalSection(&physBitmap
->lock
);
4437 /***********************************************************************
4440 static INT
X11DRV_DIB_Lock(X_PHYSBITMAP
*physBitmap
, INT req
)
4442 INT ret
= DIB_Status_None
;
4444 if (!physBitmap
->image
) return ret
; /* not a DIB section */
4445 TRACE("Locking %p from thread %04x\n", physBitmap
->hbitmap
, GetCurrentThreadId());
4446 EnterCriticalSection(&physBitmap
->lock
);
4447 ret
= physBitmap
->status
;
4448 if (req
!= DIB_Status_None
)
4449 X11DRV_DIB_Coerce(physBitmap
, req
);
4453 /***********************************************************************
4456 static void X11DRV_DIB_Unlock(X_PHYSBITMAP
*physBitmap
, BOOL commit
)
4458 if (!physBitmap
->image
) return; /* not a DIB section */
4459 switch (physBitmap
->status
)
4462 case DIB_Status_None
:
4463 /* in case anyone is wondering, this is the "signal handler doesn't
4464 * work" case, where we always have to be ready for app access */
4466 switch (physBitmap
->p_status
)
4468 case DIB_Status_GdiMod
:
4469 TRACE("Unlocking and syncing from GdiMod\n" );
4470 X11DRV_DIB_DoUpdateDIBSection( physBitmap
, TRUE
);
4474 TRACE("Unlocking without needing to sync\n" );
4478 else TRACE("Unlocking with no changes\n");
4479 physBitmap
->p_status
= DIB_Status_None
;
4482 case DIB_Status_GdiMod
:
4483 TRACE("Unlocking in status GdiMod\n" );
4484 /* DIB was protected in Coerce */
4486 /* no commit, revert to InSync if applicable */
4487 if ((physBitmap
->p_status
== DIB_Status_InSync
) ||
4488 (physBitmap
->p_status
== DIB_Status_AppMod
)) {
4489 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READONLY
);
4490 physBitmap
->status
= DIB_Status_InSync
;
4495 case DIB_Status_InSync
:
4496 TRACE("Unlocking in status InSync\n" );
4497 /* DIB was already protected in Coerce */
4500 case DIB_Status_AppMod
:
4501 TRACE("Unlocking in status AppMod\n" );
4502 /* DIB was already protected in Coerce */
4503 /* this case is ordinary only called from the signal handler,
4504 * so we don't bother to check for !commit */
4507 LeaveCriticalSection(&physBitmap
->lock
);
4508 TRACE("Unlocked %p\n", physBitmap
->hbitmap
);
4511 /***********************************************************************
4512 * X11DRV_CoerceDIBSection
4514 INT
X11DRV_CoerceDIBSection(X11DRV_PDEVICE
*physDev
, INT req
)
4516 if (!physDev
|| !physDev
->bitmap
) return DIB_Status_None
;
4517 return X11DRV_DIB_Coerce(physDev
->bitmap
, req
);
4520 /***********************************************************************
4521 * X11DRV_LockDIBSection
4523 INT
X11DRV_LockDIBSection(X11DRV_PDEVICE
*physDev
, INT req
)
4525 if (!physDev
|| !physDev
->bitmap
) return DIB_Status_None
;
4526 return X11DRV_DIB_Lock(physDev
->bitmap
, req
);
4529 /***********************************************************************
4530 * X11DRV_UnlockDIBSection
4532 void X11DRV_UnlockDIBSection(X11DRV_PDEVICE
*physDev
, BOOL commit
)
4534 if (!physDev
|| !physDev
->bitmap
) return;
4535 X11DRV_DIB_Unlock(physDev
->bitmap
, commit
);
4539 #ifdef HAVE_LIBXXSHM
4540 /***********************************************************************
4541 * X11DRV_XShmErrorHandler
4544 static int XShmErrorHandler( Display
*dpy
, XErrorEvent
*event
, void *arg
)
4546 return 1; /* FIXME: should check event contents */
4549 /***********************************************************************
4550 * X11DRV_XShmCreateImage
4553 static XImage
*X11DRV_XShmCreateImage( int width
, int height
, int bpp
,
4554 XShmSegmentInfo
* shminfo
)
4558 image
= XShmCreateImage(gdi_display
, visual
, bpp
, ZPixmap
, NULL
, shminfo
, width
, height
);
4561 shminfo
->shmid
= shmget(IPC_PRIVATE
, image
->bytes_per_line
* height
,
4563 if( shminfo
->shmid
!= -1 )
4565 shminfo
->shmaddr
= image
->data
= shmat(shminfo
->shmid
, 0, 0);
4566 if( shminfo
->shmaddr
!= (char*)-1 )
4570 shminfo
->readOnly
= FALSE
;
4571 X11DRV_expect_error( gdi_display
, XShmErrorHandler
, NULL
);
4572 ok
= (XShmAttach( gdi_display
, shminfo
) != 0);
4573 XSync( gdi_display
, False
);
4574 if (X11DRV_check_error()) ok
= FALSE
;
4577 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
4578 return image
; /* Success! */
4580 /* An error occurred */
4581 shmdt(shminfo
->shmaddr
);
4583 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
4584 shminfo
->shmid
= -1;
4586 XFlush(gdi_display
);
4587 XDestroyImage(image
);
4592 #endif /* HAVE_LIBXXSHM */
4595 /***********************************************************************
4596 * X11DRV_CreateDIBSection (X11DRV.@)
4598 HBITMAP
X11DRV_CreateDIBSection( X11DRV_PDEVICE
*physDev
, HBITMAP hbitmap
,
4599 const BITMAPINFO
*bmi
, UINT usage
)
4601 X_PHYSBITMAP
*physBitmap
;
4604 if (!(physBitmap
= X11DRV_init_phys_bitmap( hbitmap
))) return 0;
4605 physBitmap
->status
= DIB_Status_None
;
4607 GetObjectW( hbitmap
, sizeof(dib
), &dib
);
4609 /* create color map */
4610 if (dib
.dsBm
.bmBitsPixel
<= 8)
4612 physBitmap
->colorMap
= X11DRV_DIB_BuildColorMap( physDev
,
4613 usage
, dib
.dsBm
.bmBitsPixel
, bmi
,
4614 &physBitmap
->nColorMap
);
4617 /* create pixmap and X image */
4619 physBitmap
->pixmap_depth
= (dib
.dsBm
.bmBitsPixel
== 1) ? 1 : screen_depth
;
4620 physBitmap
->pixmap
= XCreatePixmap( gdi_display
, root_window
, dib
.dsBm
.bmWidth
,
4621 dib
.dsBm
.bmHeight
, physBitmap
->pixmap_depth
);
4622 #ifdef HAVE_LIBXXSHM
4623 physBitmap
->shminfo
.shmid
= -1;
4624 if (!XShmQueryExtension(gdi_display
) ||
4625 !(physBitmap
->image
= X11DRV_XShmCreateImage( dib
.dsBm
.bmWidth
, dib
.dsBm
.bmHeight
,
4626 physBitmap
->pixmap_depth
, &physBitmap
->shminfo
)) )
4628 physBitmap
->image
= X11DRV_DIB_CreateXImage( dib
.dsBm
.bmWidth
, dib
.dsBm
.bmHeight
,
4629 physBitmap
->pixmap_depth
);
4630 wine_tsx11_unlock();
4631 if (!physBitmap
->pixmap
|| !physBitmap
->image
) return 0;
4633 /* install fault handler */
4634 InitializeCriticalSection( &physBitmap
->lock
);
4635 physBitmap
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": X_PHYSBITMAP.lock");
4637 physBitmap
->base
= dib
.dsBm
.bmBits
;
4638 physBitmap
->size
= dib
.dsBmih
.biSizeImage
;
4639 physBitmap
->status
= DIB_Status_AppMod
;
4642 dibs_handler
= AddVectoredExceptionHandler( TRUE
, X11DRV_DIB_FaultHandler
);
4643 EnterCriticalSection( &dibs_cs
);
4644 list_add_head( &dibs_list
, &physBitmap
->entry
);
4645 LeaveCriticalSection( &dibs_cs
);
4647 X11DRV_DIB_DoProtectDIBSection( physBitmap
, PAGE_READWRITE
);
4652 /***********************************************************************
4653 * X11DRV_DIB_DeleteDIBSection
4655 void X11DRV_DIB_DeleteDIBSection(X_PHYSBITMAP
*physBitmap
, DIBSECTION
*dib
)
4659 EnterCriticalSection( &dibs_cs
);
4660 list_remove( &physBitmap
->entry
);
4661 last
= list_empty( &dibs_list
);
4662 LeaveCriticalSection( &dibs_cs
);
4666 RemoveVectoredExceptionHandler( dibs_handler
);
4667 dibs_handler
= NULL
;
4670 if (dib
->dshSection
)
4671 X11DRV_DIB_Coerce(physBitmap
, DIB_Status_InSync
);
4673 if (physBitmap
->image
)
4676 #ifdef HAVE_LIBXXSHM
4677 if (physBitmap
->shminfo
.shmid
!= -1)
4679 XShmDetach( gdi_display
, &(physBitmap
->shminfo
) );
4680 XDestroyImage( physBitmap
->image
);
4681 shmdt( physBitmap
->shminfo
.shmaddr
);
4682 physBitmap
->shminfo
.shmid
= -1;
4686 XDestroyImage( physBitmap
->image
);
4687 wine_tsx11_unlock();
4690 HeapFree(GetProcessHeap(), 0, physBitmap
->colorMap
);
4691 physBitmap
->lock
.DebugInfo
->Spare
[0] = 0;
4692 DeleteCriticalSection(&physBitmap
->lock
);
4695 /***********************************************************************
4696 * SetDIBColorTable (X11DRV.@)
4698 UINT
X11DRV_SetDIBColorTable( X11DRV_PDEVICE
*physDev
, UINT start
, UINT count
, const RGBQUAD
*colors
)
4702 X_PHYSBITMAP
*physBitmap
= physDev
->bitmap
;
4704 if (!physBitmap
) return 0;
4705 GetObjectW( physBitmap
->hbitmap
, sizeof(dib
), &dib
);
4707 if (physBitmap
->colorMap
&& start
< physBitmap
->nColorMap
) {
4708 UINT end
= count
+ start
;
4709 if (end
> physBitmap
->nColorMap
) end
= physBitmap
->nColorMap
;
4711 * Changing color table might change the mapping between
4712 * DIB colors and X11 colors and thus alter the visible state
4713 * of the bitmap object.
4716 * FIXME we need to recalculate the pen, brush, text and bkgnd pixels here,
4717 * at least for a 1 bpp dibsection
4719 X11DRV_DIB_Lock( physBitmap
, DIB_Status_AppMod
);
4720 X11DRV_DIB_GenColorMap( physDev
, physBitmap
->colorMap
, DIB_RGB_COLORS
,
4721 dib
.dsBm
.bmBitsPixel
,
4722 TRUE
, colors
, start
, end
);
4723 X11DRV_DIB_Unlock( physBitmap
, TRUE
);
4730 /***********************************************************************
4731 * X11DRV_DIB_CreateDIBFromBitmap
4733 * Allocates a packed DIB and copies the bitmap data into it.
4735 HGLOBAL
X11DRV_DIB_CreateDIBFromBitmap(HDC hdc
, HBITMAP hBmp
)
4740 LPBITMAPINFOHEADER pbmiHeader
;
4741 unsigned int cDataSize
, cPackedSize
, OffsetBits
;
4744 if (!GetObjectW( hBmp
, sizeof(bmp
), &bmp
)) return 0;
4747 * A packed DIB contains a BITMAPINFO structure followed immediately by
4748 * an optional color palette and the pixel data.
4751 /* Calculate the size of the packed DIB */
4752 cDataSize
= X11DRV_DIB_GetDIBWidthBytes( bmp
.bmWidth
, bmp
.bmBitsPixel
) * abs( bmp
.bmHeight
);
4753 cPackedSize
= sizeof(BITMAPINFOHEADER
)
4754 + ( (bmp
.bmBitsPixel
<= 8) ? (sizeof(RGBQUAD
) * (1 << bmp
.bmBitsPixel
)) : 0 )
4756 /* Get the offset to the bits */
4757 OffsetBits
= cPackedSize
- cDataSize
;
4759 /* Allocate the packed DIB */
4760 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize
);
4761 hPackedDIB
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
/*| GMEM_ZEROINIT*/,
4765 WARN("Could not allocate packed DIB!\n");
4769 /* A packed DIB starts with a BITMAPINFOHEADER */
4770 pPackedDIB
= GlobalLock(hPackedDIB
);
4771 pbmiHeader
= (LPBITMAPINFOHEADER
)pPackedDIB
;
4773 /* Init the BITMAPINFOHEADER */
4774 pbmiHeader
->biSize
= sizeof(BITMAPINFOHEADER
);
4775 pbmiHeader
->biWidth
= bmp
.bmWidth
;
4776 pbmiHeader
->biHeight
= bmp
.bmHeight
;
4777 pbmiHeader
->biPlanes
= 1;
4778 pbmiHeader
->biBitCount
= bmp
.bmBitsPixel
;
4779 pbmiHeader
->biCompression
= BI_RGB
;
4780 pbmiHeader
->biSizeImage
= 0;
4781 pbmiHeader
->biXPelsPerMeter
= pbmiHeader
->biYPelsPerMeter
= 0;
4782 pbmiHeader
->biClrUsed
= 0;
4783 pbmiHeader
->biClrImportant
= 0;
4785 /* Retrieve the DIB bits from the bitmap and fill in the
4786 * DIB color table if present */
4788 nLinesCopied
= GetDIBits(hdc
, /* Handle to device context */
4789 hBmp
, /* Handle to bitmap */
4790 0, /* First scan line to set in dest bitmap */
4791 bmp
.bmHeight
, /* Number of scan lines to copy */
4792 pPackedDIB
+ OffsetBits
, /* [out] Address of array for bitmap bits */
4793 (LPBITMAPINFO
) pbmiHeader
, /* [out] Address of BITMAPINFO structure */
4794 0); /* RGB or palette index */
4795 GlobalUnlock(hPackedDIB
);
4797 /* Cleanup if GetDIBits failed */
4798 if (nLinesCopied
!= bmp
.bmHeight
)
4800 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied
, bmp
.bmHeight
);
4801 GlobalFree(hPackedDIB
);
4808 /**************************************************************************
4809 * X11DRV_DIB_CreateDIBFromPixmap
4811 * Allocates a packed DIB and copies the Pixmap data into it.
4812 * The Pixmap passed in is deleted after the conversion.
4814 HGLOBAL
X11DRV_DIB_CreateDIBFromPixmap(Pixmap pixmap
, HDC hdc
)
4817 X_PHYSBITMAP
*physBitmap
;
4818 HBITMAP hBmp
= 0, old
;
4819 HGLOBAL hPackedDIB
= 0;
4821 int x
,y
; /* Unused */
4822 unsigned border_width
; /* Unused */
4823 unsigned int depth
, width
, height
;
4825 /* Get the Pixmap dimensions and bit depth */
4827 if (!XGetGeometry(gdi_display
, pixmap
, &root
, &x
, &y
, &width
, &height
,
4828 &border_width
, &depth
)) depth
= 0;
4829 wine_tsx11_unlock();
4830 if (!depth
) return 0;
4832 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
4833 width
, height
, depth
);
4836 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
4837 * and make it a container for the pixmap passed.
4839 hBmp
= CreateBitmap( width
, height
, 1, depth
, NULL
);
4841 /* force bitmap to be owned by a screen DC */
4842 hdcMem
= CreateCompatibleDC( hdc
);
4843 old
= SelectObject( hdcMem
, hBmp
);
4845 physBitmap
= X11DRV_get_phys_bitmap( hBmp
);
4848 if (physBitmap
->pixmap
) XFreePixmap( gdi_display
, physBitmap
->pixmap
);
4849 physBitmap
->pixmap
= pixmap
;
4850 wine_tsx11_unlock();
4852 SelectObject( hdcMem
, old
);
4856 * Create a packed DIB from the Pixmap wrapper bitmap created above.
4857 * A packed DIB contains a BITMAPINFO structure followed immediately by
4858 * an optional color palette and the pixel data.
4860 hPackedDIB
= X11DRV_DIB_CreateDIBFromBitmap(hdc
, hBmp
);
4862 /* We can now get rid of the HBITMAP wrapper we created earlier.
4863 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
4867 TRACE("\tReturning packed DIB %p\n", hPackedDIB
);
4872 /**************************************************************************
4873 * X11DRV_DIB_CreatePixmapFromDIB
4875 * Creates a Pixmap from a packed DIB
4877 Pixmap
X11DRV_DIB_CreatePixmapFromDIB( HGLOBAL hPackedDIB
, HDC hdc
)
4880 X_PHYSBITMAP
*physBitmap
;
4884 /* Create a DDB from the DIB */
4886 pbmi
= GlobalLock(hPackedDIB
);
4887 hBmp
= CreateDIBitmap(hdc
, &pbmi
->bmiHeader
, CBM_INIT
,
4888 (LPBYTE
)pbmi
+ X11DRV_DIB_BitmapInfoSize( pbmi
, DIB_RGB_COLORS
),
4889 pbmi
, DIB_RGB_COLORS
);
4890 GlobalUnlock(hPackedDIB
);
4892 /* clear the physBitmap so that we can steal its pixmap */
4893 physBitmap
= X11DRV_get_phys_bitmap( hBmp
);
4894 pixmap
= physBitmap
->pixmap
;
4895 physBitmap
->pixmap
= 0;
4897 /* Delete the DDB we created earlier now that we have stolen its pixmap */
4900 TRACE("Returning Pixmap %ld\n", pixmap
);