2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
18 static int bitmapDepthTable
[] = { 8, 1, 32, 16, 24, 15, 4, 0 };
19 static int ximageDepthTable
[] = { 0, 0, 0, 0, 0, 0, 0 };
22 /* This structure holds the arguments for DIB_SetImageBits() */
43 } DIB_SETIMAGEBITS_DESCR
;
46 /***********************************************************************
54 for( i
= 0; bitmapDepthTable
[i
]; i
++ )
56 testimage
= TSXCreateImage(display
, DefaultVisualOfScreen(screen
),
57 bitmapDepthTable
[i
], ZPixmap
, 0, NULL
, 1, 1, 32, 20 );
58 if( testimage
) ximageDepthTable
[i
] = testimage
->bits_per_pixel
;
60 TSXDestroyImage(testimage
);
65 /***********************************************************************
66 * DIB_GetXImageWidthBytes
68 * Return the width of an X image in bytes
70 int DIB_GetXImageWidthBytes( int width
, int depth
)
74 if (!ximageDepthTable
[0]) {
77 for( i
= 0; bitmapDepthTable
[i
] ; i
++ )
78 if( bitmapDepthTable
[i
] == depth
)
79 return (4 * ((width
* ximageDepthTable
[i
] + 31)/32));
81 WARN(bitmap
, "(%d): Unsupported depth\n", depth
);
85 /***********************************************************************
86 * DIB_GetDIBWidthBytes
88 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
89 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
91 int DIB_GetDIBWidthBytes( int width
, int depth
)
97 case 1: words
= (width
+ 31) / 32; break;
98 case 4: words
= (width
+ 7) / 8; break;
99 case 8: words
= (width
+ 3) / 4; break;
101 case 16: words
= (width
+ 1) / 2; break;
102 case 24: words
= (width
* 3 + 3)/4; break;
105 WARN(bitmap
, "(%d): Unsupported depth\n", depth
);
114 /***********************************************************************
117 * Return the size of the bitmap info structure including color table.
119 int DIB_BitmapInfoSize( BITMAPINFO
* info
, WORD coloruse
)
123 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
125 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
126 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
127 return sizeof(BITMAPCOREHEADER
) + colors
*
128 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
130 else /* assume BITMAPINFOHEADER */
132 colors
= info
->bmiHeader
.biClrUsed
;
133 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
134 colors
= 1 << info
->bmiHeader
.biBitCount
;
135 return sizeof(BITMAPINFOHEADER
) + colors
*
136 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
141 /***********************************************************************
144 * Get the info from a bitmap header.
145 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
147 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
148 int *height
, WORD
*bpp
, WORD
*compr
)
150 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
152 *width
= header
->biWidth
;
153 *height
= header
->biHeight
;
154 *bpp
= header
->biBitCount
;
155 *compr
= header
->biCompression
;
158 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
160 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
161 *width
= core
->bcWidth
;
162 *height
= core
->bcHeight
;
163 *bpp
= core
->bcBitCount
;
167 WARN(bitmap
, "(%ld): wrong size for header\n", header
->biSize
);
172 /***********************************************************************
175 * Build the color map from the bitmap palette. Should not be called
176 * for a >8-bit deep bitmap.
178 static int *DIB_BuildColorMap( DC
*dc
, WORD coloruse
, WORD depth
,
179 const BITMAPINFO
*info
, int *nColors
)
186 if ((isInfo
= (info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))))
188 colors
= info
->bmiHeader
.biClrUsed
;
189 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
190 colorPtr
= (WORD
*)info
->bmiColors
;
192 else /* assume BITMAPCOREINFO */
194 colors
= 1 << ((BITMAPCOREHEADER
*)&info
->bmiHeader
)->bcBitCount
;
195 colorPtr
= (WORD
*)((BITMAPCOREINFO
*)info
)->bmciColors
;
200 ERR(bitmap
, "DIB_BuildColorMap called with >256 colors!\n");
204 if (!(colorMapping
= (int *)HeapAlloc(GetProcessHeap(), 0,
205 colors
* sizeof(int) )))
208 if (coloruse
== DIB_RGB_COLORS
)
212 RGBQUAD
* rgb
= (RGBQUAD
*)colorPtr
;
214 if (depth
== 1) /* Monochrome */
215 for (i
= 0; i
< colors
; i
++, rgb
++)
216 colorMapping
[i
] = (rgb
->rgbRed
+ rgb
->rgbGreen
+
217 rgb
->rgbBlue
> 255*3/2);
219 for (i
= 0; i
< colors
; i
++, rgb
++)
220 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbRed
,
226 RGBTRIPLE
* rgb
= (RGBTRIPLE
*)colorPtr
;
228 if (depth
== 1) /* Monochrome */
229 for (i
= 0; i
< colors
; i
++, rgb
++)
230 colorMapping
[i
] = (rgb
->rgbtRed
+ rgb
->rgbtGreen
+
231 rgb
->rgbtBlue
> 255*3/2);
233 for (i
= 0; i
< colors
; i
++, rgb
++)
234 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbtRed
,
239 else /* DIB_PAL_COLORS */
241 for (i
= 0; i
< colors
; i
++, colorPtr
++)
242 colorMapping
[i
] = COLOR_ToPhysical( dc
, PALETTEINDEX(*colorPtr
) );
250 /***********************************************************************
253 static int DIB_MapColor( int *physMap
, int nPhysMap
, int phys
)
257 for (color
= 0; color
< nPhysMap
; color
++)
258 if (physMap
[color
] == phys
)
261 WARN(bitmap
, "Strange color %08x\n", phys
);
266 /***********************************************************************
267 * DIB_SetImageBits_1_Line
269 * Handles a single line of 1 bit data.
271 static void DIB_SetImageBits_1_Line(DWORD dstwidth
, int left
, int *colors
,
272 XImage
*bmpImage
, int h
, const BYTE
*bits
)
277 dstwidth
+= left
; bits
+= left
>> 3;
279 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
280 for (i
= dstwidth
/8, x
= left
&~7; (i
> 0); i
--)
283 XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] );
284 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 6) & 1] );
285 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 5) & 1] );
286 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 4) & 1] );
287 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 3) & 1] );
288 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 2) & 1] );
289 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 1) & 1] );
290 XPutPixel( bmpImage
, x
++, h
, colors
[pix
& 1] );
295 case 7: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
296 case 6: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
297 case 5: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
298 case 4: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
299 case 3: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
300 case 2: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
301 case 1: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] );
305 /***********************************************************************
308 * SetDIBits for a 1-bit deep DIB.
310 static void DIB_SetImageBits_1( int lines
, const BYTE
*srcbits
,
311 DWORD srcwidth
, DWORD dstwidth
, int left
,
312 int *colors
, XImage
*bmpImage
)
317 DWORD linebytes
= ((srcwidth
+ 31) & ~31) / 8;
320 for (h
= lines
-1; h
>=0; h
--) {
321 DIB_SetImageBits_1_Line(dstwidth
, left
, colors
, bmpImage
, h
, srcbits
);
322 srcbits
+= linebytes
;
326 for (h
= 0; h
< lines
; h
++) {
327 DIB_SetImageBits_1_Line(dstwidth
, left
, colors
, bmpImage
, h
, srcbits
);
328 srcbits
+= linebytes
;
334 /***********************************************************************
337 * SetDIBits for a 4-bit deep DIB.
339 static void DIB_SetImageBits_4( int lines
, const BYTE
*srcbits
,
340 DWORD srcwidth
, DWORD dstwidth
, int left
,
341 int *colors
, XImage
*bmpImage
)
345 const BYTE
*bits
= srcbits
+ (left
>> 1);
348 DWORD linebytes
= ((srcwidth
+7)&~7)/2;
352 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
354 for (h
= lines
-1; h
>= 0; h
--) {
355 for (i
= dstwidth
/2, x
= left
&~1; i
> 0; i
--) {
357 XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 4] );
358 XPutPixel( bmpImage
, x
++, h
, colors
[pix
& 0x0f] );
360 if (dstwidth
& 1) XPutPixel( bmpImage
, x
, h
, colors
[*bits
>> 4] );
361 srcbits
+= linebytes
;
362 bits
= srcbits
+ (left
>> 1);
366 for (h
= 0; h
< lines
; h
++) {
367 for (i
= dstwidth
/2, x
= left
&~1; i
> 0; i
--) {
369 XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 4] );
370 XPutPixel( bmpImage
, x
++, h
, colors
[pix
& 0x0f] );
372 if (dstwidth
& 1) XPutPixel( bmpImage
, x
, h
, colors
[*bits
>> 4] );
373 srcbits
+= linebytes
;
374 bits
= srcbits
+ (left
>> 1);
379 /***********************************************************************
382 * GetDIBits for a 4-bit deep DIB.
384 static void DIB_GetImageBits_4( int lines
, BYTE
*srcbits
,
385 DWORD srcwidth
, DWORD dstwidth
, int left
,
386 int *colors
, int nColors
, XImage
*bmpImage
)
390 BYTE
*bits
= srcbits
+ (left
>> 1);
393 DWORD linebytes
= ((srcwidth
+7)&~7)/2;
397 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
399 for (h
= lines
-1; h
>= 0; h
--) {
400 for (i
= dstwidth
/2, x
= left
&~1; i
> 0; i
--) {
401 *bits
++ = (DIB_MapColor( colors
, nColors
, XGetPixel( bmpImage
, x
++, h
)) << 4)
402 | (DIB_MapColor( colors
, nColors
, XGetPixel( bmpImage
, x
++, h
)) & 0x0f);
405 *bits
= (DIB_MapColor( colors
, nColors
, XGetPixel( bmpImage
, x
++, h
)) << 4);
406 srcbits
+= linebytes
;
407 bits
= srcbits
+ (left
>> 1);
411 for (h
= 0; h
< lines
; h
++) {
412 for (i
= dstwidth
/2, x
= left
&~1; i
> 0; i
--) {
413 *bits
++ = (DIB_MapColor( colors
, nColors
, XGetPixel( bmpImage
, x
++, h
)) << 4)
414 | (DIB_MapColor( colors
, nColors
, XGetPixel( bmpImage
, x
++, h
)) & 0x0f);
417 *bits
= (DIB_MapColor( colors
, nColors
, XGetPixel( bmpImage
, x
++, h
)) << 4);
418 srcbits
+= linebytes
;
419 bits
= srcbits
+ (left
>> 1);
424 #define check_xy(x,y) \
431 /***********************************************************************
432 * DIB_SetImageBits_RLE4
434 * SetDIBits for a 4-bit deep compressed DIB.
436 static void DIB_SetImageBits_RLE4( int lines
, const BYTE
*bits
, DWORD width
,
437 DWORD dstwidth
, int left
, int *colors
, XImage
*bmpImage
)
439 int x
= 0, c
, length
;
440 const BYTE
*begin
= bits
;
442 dstwidth
+= left
; /* FIXME: avoid putting x<left pixels */
445 while ((int)lines
>= 0)
448 if (length
) { /* encoded */
451 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
455 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
467 case 1: /* eopicture */
475 default: /* absolute */
478 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
482 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
486 if ((bits
- begin
) & 1)
493 /***********************************************************************
496 * SetDIBits for an 8-bit deep DIB.
498 static void DIB_SetImageBits_8( int lines
, const BYTE
*srcbits
,
499 DWORD srcwidth
, DWORD dstwidth
, int left
,
500 int *colors
, XImage
*bmpImage
)
504 const BYTE
*bits
= srcbits
+ left
;
506 /* align to 32 bit */
507 DWORD linebytes
= (srcwidth
+ 3) & ~3;
512 for (h
= lines
- 1; h
>= 0; h
--) {
513 for (x
= left
; x
< dstwidth
; x
++, bits
++) {
514 XPutPixel( bmpImage
, x
, h
, colors
[*bits
] );
516 bits
= (srcbits
+= linebytes
) + left
;
520 for (h
= 0; h
< lines
; h
++) {
521 for (x
= left
; x
< dstwidth
; x
++, bits
++) {
522 XPutPixel( bmpImage
, x
, h
, colors
[*bits
] );
524 bits
= (srcbits
+= linebytes
) + left
;
529 /***********************************************************************
532 * GetDIBits for an 8-bit deep DIB.
534 static void DIB_GetImageBits_8( int lines
, BYTE
*srcbits
,
535 DWORD srcwidth
, DWORD dstwidth
, int left
,
536 int *colors
, int nColors
, XImage
*bmpImage
)
540 BYTE
*bits
= srcbits
+ left
;
542 /* align to 32 bit */
543 DWORD linebytes
= (srcwidth
+ 3) & ~3;
548 for (h
= lines
- 1; h
>= 0; h
--) {
549 for (x
= left
; x
< dstwidth
; x
++, bits
++) {
550 if ( XGetPixel( bmpImage
, x
, h
) != colors
[*bits
] )
551 *bits
= DIB_MapColor( colors
, nColors
,
552 XGetPixel( bmpImage
, x
, h
) );
554 bits
= (srcbits
+= linebytes
) + left
;
558 for (h
= 0; h
< lines
; h
++) {
559 for (x
= left
; x
< dstwidth
; x
++, bits
++) {
560 if ( XGetPixel( bmpImage
, x
, h
) != colors
[*bits
] )
561 *bits
= DIB_MapColor( colors
, nColors
,
562 XGetPixel( bmpImage
, x
, h
) );
564 bits
= (srcbits
+= linebytes
) + left
;
569 /***********************************************************************
570 * DIB_SetImageBits_RLE8
572 * SetDIBits for an 8-bit deep compressed DIB.
574 * This function rewritten 941113 by James Youngman. WINE blew out when I
575 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
577 * This was because the algorithm assumed that all RLE8 bitmaps end with the
578 * 'End of bitmap' escape code. This code is very much laxer in what it
579 * allows to end the expansion. Possibly too lax. See the note by
580 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
581 * bitmap should end with RleEnd, but on the other hand, software exists
582 * that produces ones that don't and Windows 3.1 doesn't complain a bit
585 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
586 * James A. Youngman <mbcstjy@afs.man.ac.uk>
590 enum Rle8_EscapeCodes
593 * Apologies for polluting your file's namespace...
595 RleEol
= 0, /* End of line */
596 RleEnd
= 1, /* End of bitmap */
597 RleDelta
= 2 /* Delta */
600 static void DIB_SetImageBits_RLE8( int lines
, const BYTE
*bits
, DWORD width
,
601 DWORD dstwidth
, int left
, int *colors
, XImage
*bmpImage
)
603 int x
; /* X-positon on each line. Increases. */
604 int line
; /* Line #. Starts at lines-1, decreases */
605 const BYTE
*pIn
= bits
; /* Pointer to current position in bits */
606 BYTE length
; /* The length pf a run */
607 BYTE color_index
; /* index into colors[] as read from bits */
608 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
609 WORD color
; /* value of colour[color_index] */
611 if (lines
== 0) /* Let's hope this doesn't happen. */
614 dstwidth
+= left
; /* FIXME: avoid putting x<left pixels */
617 * Note that the bitmap data is stored by Windows starting at the
618 * bottom line of the bitmap and going upwards. Within each line,
619 * the data is stored left-to-right. That's the reason why line
620 * goes from lines-1 to 0. [JAY]
630 * If the length byte is not zero (which is the escape value),
631 * We have a run of length pixels all the same colour. The colour
632 * index is stored next.
634 * If the length byte is zero, we need to read the next byte to
635 * know what to do. [JAY]
640 * [Run-Length] Encoded mode
642 color_index
= (*pIn
++); /* Get the colour index. */
643 color
= colors
[color_index
];
646 XPutPixel(bmpImage
, x
++, line
, color
);
651 * Escape codes (may be an absolute sequence though)
653 escape_code
= (*pIn
++);
656 case RleEol
: /* =0, end of line */
663 case RleEnd
: /* =1, end of bitmap */
666 * Not all RLE8 bitmaps end with this
667 * code. For example, Paint Shop Pro
668 * produces some that don't. That's (I think)
669 * what caused the previous implementation to
672 line
=-1; /* Cause exit from do loop. */
676 case RleDelta
: /* =2, a delta */
679 * Note that deltaing to line 0
680 * will cause an exit from the loop,
681 * which may not be what is intended.
682 * The fact that there is a delta in the bits
683 * almost certainly implies that there is data
684 * to follow. You may feel that we should
685 * jump to the top of the loop to avoid exiting
688 * TODO: Decide what to do here in that case. [JAY]
694 TRACE(bitmap
, "Delta to last line of bitmap "
695 "(wrongly?) causes loop exit\n");
700 default: /* >2, switch to absolute mode */
705 length
= escape_code
;
708 color_index
= (*pIn
++);
709 XPutPixel(bmpImage
, x
++, line
,
710 colors
[color_index
]);
714 * If you think for a moment you'll realise that the
715 * only time we could ever possibly read an odd
716 * number of bytes is when there is a 0x00 (escape),
717 * a value >0x02 (absolute mode) and then an odd-
718 * length run. Therefore this is the only place we
719 * need to worry about it. Everywhere else the
720 * bytes are always read in pairs. [JAY]
723 pIn
++; /* Throw away the pad byte. */
726 } /* switch (escape_code) : Escape sequence */
727 } /* process either an encoded sequence or an escape sequence */
729 /* We expect to come here more than once per line. */
730 } while (line
>= 0); /* Do this until the bitmap is filled */
733 * Everybody comes here at the end.
734 * Check how we exited the loop and print a message if it's a bit odd.
737 if ( (*(pIn
-2) != 0/*escape*/) || (*(pIn
-1)!= RleEnd
) )
739 TRACE(bitmap
, "End-of-bitmap "
740 "without (strictly) proper escape code. Last two "
741 "bytes were: %02X %02X.\n",
748 /***********************************************************************
749 * DIB_SetImageBits_16
751 * SetDIBits for a 16-bit deep DIB.
753 static void DIB_SetImageBits_16( int lines
, const BYTE
*srcbits
,
754 DWORD srcwidth
, DWORD dstwidth
, int left
,
755 DC
*dc
, XImage
*bmpImage
)
763 /* align to 32 bit */
764 DWORD linebytes
= (srcwidth
* 2 + 3) & ~3;
768 ptr
= (LPWORD
) srcbits
+ left
;
770 for (h
= lines
- 1; h
>= 0; h
--) {
771 for (x
= left
; x
< dstwidth
; x
++, ptr
++) {
773 r
= (BYTE
) ((val
& 0x7c00) >> 7);
774 g
= (BYTE
) ((val
& 0x03e0) >> 2);
775 b
= (BYTE
) ((val
& 0x001f) << 3);
776 XPutPixel( bmpImage
, x
, h
,
777 COLOR_ToPhysical(dc
, RGB(r
,g
,b
)) );
779 ptr
= (LPWORD
) (srcbits
+= linebytes
) + left
;
783 for (h
= 0; h
< lines
; h
++) {
784 for (x
= left
; x
< dstwidth
; x
++, ptr
++) {
786 r
= (BYTE
) ((val
& 0x7c00) >> 7);
787 g
= (BYTE
) ((val
& 0x03e0) >> 2);
788 b
= (BYTE
) ((val
& 0x001f) << 3);
789 XPutPixel( bmpImage
, x
, h
,
790 COLOR_ToPhysical(dc
, RGB(r
,g
,b
)) );
792 ptr
= (LPWORD
) (srcbits
+= linebytes
) + left
;
798 /***********************************************************************
799 * DIB_GetImageBits_16
801 * GetDIBits for an 16-bit deep DIB.
803 static void DIB_GetImageBits_16( int lines
, BYTE
*srcbits
,
804 DWORD srcwidth
, DWORD dstwidth
, int left
,
812 /* align to 32 bit */
813 DWORD linebytes
= (srcwidth
* 2 + 3) & ~3;
817 ptr
= (LPWORD
) srcbits
+ left
;
819 for (h
= lines
- 1; h
>= 0; h
--)
821 for (x
= left
; x
< dstwidth
; x
++, ptr
++)
823 COLORREF pixel
= COLOR_ToLogical( XGetPixel( bmpImage
, x
, h
) );
824 r
= (BYTE
) GetRValue(pixel
);
825 g
= (BYTE
) GetGValue(pixel
);
826 b
= (BYTE
) GetBValue(pixel
);
827 *ptr
= ( ((r
<< 7) & 0x7c00) | ((g
<< 2) & 0x03e0) | ((b
>> 3) & 0x001f) );
829 ptr
= (LPWORD
) (srcbits
+= linebytes
) + left
;
833 for (h
= 0; h
< lines
; h
++)
835 for (x
= left
; x
< dstwidth
; x
++, ptr
++)
837 COLORREF pixel
= COLOR_ToLogical( XGetPixel( bmpImage
, x
, h
) );
838 r
= (BYTE
) GetRValue(pixel
);
839 g
= (BYTE
) GetGValue(pixel
);
840 b
= (BYTE
) GetBValue(pixel
);
841 *ptr
= ( ((r
<< 7) & 0x7c00) | ((g
<< 2) & 0x03e0) | ((b
>> 3) & 0x001f) );
844 ptr
= (LPWORD
) (srcbits
+= linebytes
) + left
;
850 /***********************************************************************
851 * DIB_SetImageBits_24
853 * SetDIBits for a 24-bit deep DIB.
855 static void DIB_SetImageBits_24( int lines
, const BYTE
*srcbits
,
856 DWORD srcwidth
, DWORD dstwidth
, int left
,
857 DC
*dc
, XImage
*bmpImage
)
860 const BYTE
*bits
= srcbits
+ left
* 3;
863 /* align to 32 bit */
864 DWORD linebytes
= (srcwidth
* 3 + 3) & ~3;
868 /* "bits" order is reversed for some reason */
871 for (h
= lines
- 1; h
>= 0; h
--) {
872 for (x
= left
; x
< dstwidth
; x
++, bits
+= 3) {
873 XPutPixel( bmpImage
, x
, h
,
874 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])));
876 bits
= (srcbits
+= linebytes
) + left
* 3;
880 for (h
= 0; h
< lines
; h
++) {
881 for (x
= left
; x
< dstwidth
; x
++, bits
+= 3) {
882 XPutPixel( bmpImage
, x
, h
,
883 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])));
885 bits
= (srcbits
+= linebytes
) + left
* 3;
891 /***********************************************************************
892 * DIB_GetImageBits_24
894 * GetDIBits for an 24-bit deep DIB.
896 static void DIB_GetImageBits_24( int lines
, BYTE
*srcbits
,
897 DWORD srcwidth
, DWORD dstwidth
, int left
,
902 BYTE
*bits
= srcbits
+ (left
* 3);
904 /* align to 32 bit */
905 DWORD linebytes
= (srcwidth
* 3 + 3) & ~3;
910 for (h
= lines
- 1; h
>= 0; h
--)
912 for (x
= left
; x
< dstwidth
; x
++, bits
+= 3)
914 COLORREF pixel
= COLOR_ToLogical( XGetPixel( bmpImage
, x
, h
) );
915 bits
[0] = GetRValue(pixel
);
916 bits
[1] = GetGValue(pixel
);
917 bits
[2] = GetBValue(pixel
);
919 bits
= (srcbits
+= linebytes
) + (left
* 3);
923 for (h
= 0; h
< lines
; h
++)
925 for (x
= left
; x
< dstwidth
; x
++, bits
+= 3)
927 COLORREF pixel
= COLOR_ToLogical( XGetPixel( bmpImage
, x
, h
) );
928 bits
[0] = GetRValue(pixel
);
929 bits
[1] = GetGValue(pixel
);
930 bits
[2] = GetBValue(pixel
);
933 bits
= (srcbits
+= linebytes
) + (left
* 3);
939 /***********************************************************************
940 * DIB_SetImageBits_32
942 * SetDIBits for a 32-bit deep DIB.
944 static void DIB_SetImageBits_32( int lines
, const BYTE
*srcbits
,
945 DWORD srcwidth
, DWORD dstwidth
, int left
,
946 DC
*dc
, XImage
*bmpImage
)
949 const BYTE
*bits
= srcbits
+ left
* 4;
952 DWORD linebytes
= (srcwidth
* 4);
957 for (h
= lines
- 1; h
>= 0; h
--) {
958 for (x
= left
; x
< dstwidth
; x
++, bits
+= 4) {
959 XPutPixel( bmpImage
, x
, h
,
960 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])));
962 bits
= (srcbits
+= linebytes
) + left
* 4;
966 for (h
= 0; h
< lines
; h
++) {
967 for (x
= left
; x
< dstwidth
; x
++, bits
+= 4) {
968 XPutPixel( bmpImage
, x
, h
,
969 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])));
971 bits
= (srcbits
+= linebytes
) + left
* 4;
977 /***********************************************************************
978 * DIB_GetImageBits_32
980 * GetDIBits for an 32-bit deep DIB.
982 static void DIB_GetImageBits_32( int lines
, BYTE
*srcbits
,
983 DWORD srcwidth
, DWORD dstwidth
, int left
,
988 BYTE
*bits
= srcbits
+ (left
* 4);
990 /* align to 32 bit */
991 DWORD linebytes
= (srcwidth
* 4);
996 for (h
= lines
- 1; h
>= 0; h
--)
998 for (x
= left
; x
< dstwidth
; x
++, bits
+= 4)
1000 COLORREF pixel
= COLOR_ToLogical( XGetPixel( bmpImage
, x
, h
) );
1001 bits
[0] = GetRValue(pixel
);
1002 bits
[1] = GetGValue(pixel
);
1003 bits
[2] = GetBValue(pixel
);
1005 bits
= (srcbits
+= linebytes
) + (left
* 4);
1009 for (h
= 0; h
< lines
; h
++)
1011 for (x
= left
; x
< dstwidth
; x
++, bits
+= 4)
1013 COLORREF pixel
= COLOR_ToLogical( XGetPixel( bmpImage
, x
, h
) );
1014 bits
[0] = GetRValue(pixel
);
1015 bits
[1] = GetGValue(pixel
);
1016 bits
[2] = GetBValue(pixel
);
1019 bits
= (srcbits
+= linebytes
) + (left
* 4);
1025 /***********************************************************************
1028 * Transfer the bits to an X image.
1029 * Helper function for SetDIBits() and SetDIBitsToDevice().
1030 * The Xlib critical section must be entered before calling this function.
1032 static int DIB_SetImageBits( const DIB_SETIMAGEBITS_DESCR
*descr
)
1034 int lines
= descr
->lines
>= 0 ? descr
->lines
: -descr
->lines
;
1037 if ( descr
->dc
&& descr
->dc
->w
.flags
& DC_DIRTY
)
1038 CLIPPING_UpdateGCRegion( descr
->dc
);
1041 bmpImage
= descr
->image
;
1043 XCREATEIMAGE( bmpImage
, descr
->infoWidth
, lines
, descr
->depth
);
1045 /* Transfer the pixels */
1046 switch(descr
->infoBpp
)
1049 DIB_SetImageBits_1( descr
->lines
, descr
->bits
, descr
->infoWidth
,
1050 descr
->width
, descr
->xSrc
, descr
->colorMap
, bmpImage
);
1053 if (descr
->compression
) DIB_SetImageBits_RLE4( descr
->lines
, descr
->bits
,
1054 descr
->infoWidth
, descr
->width
, descr
->xSrc
,
1055 descr
->colorMap
, bmpImage
);
1056 else DIB_SetImageBits_4( descr
->lines
, descr
->bits
, descr
->infoWidth
,
1057 descr
->width
, descr
->xSrc
, descr
->colorMap
, bmpImage
);
1060 if (descr
->compression
) DIB_SetImageBits_RLE8( descr
->lines
, descr
->bits
,
1061 descr
->infoWidth
, descr
->width
, descr
->xSrc
,
1062 descr
->colorMap
, bmpImage
);
1063 else DIB_SetImageBits_8( descr
->lines
, descr
->bits
, descr
->infoWidth
,
1064 descr
->width
, descr
->xSrc
, descr
->colorMap
, bmpImage
);
1068 DIB_SetImageBits_16( descr
->lines
, descr
->bits
, descr
->infoWidth
,
1069 descr
->width
, descr
->xSrc
, descr
->dc
, bmpImage
);
1072 DIB_SetImageBits_24( descr
->lines
, descr
->bits
, descr
->infoWidth
,
1073 descr
->width
, descr
->xSrc
, descr
->dc
, bmpImage
);
1076 DIB_SetImageBits_32( descr
->lines
, descr
->bits
, descr
->infoWidth
,
1077 descr
->width
, descr
->xSrc
, descr
->dc
, bmpImage
);
1080 WARN(bitmap
, "(%d): Invalid depth\n", descr
->infoBpp
);
1084 XPutImage( display
, descr
->drawable
, descr
->gc
, bmpImage
,
1085 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
1086 descr
->width
, descr
->height
);
1088 if (!descr
->image
) XDestroyImage( bmpImage
);
1092 /***********************************************************************
1095 * Transfer the bits from an X image.
1096 * The Xlib critical section must be entered before calling this function.
1098 static int DIB_GetImageBits( const DIB_SETIMAGEBITS_DESCR
*descr
)
1100 int lines
= descr
->lines
>= 0 ? descr
->lines
: -descr
->lines
;
1104 bmpImage
= descr
->image
;
1106 XCREATEIMAGE( bmpImage
, descr
->infoWidth
, lines
, descr
->depth
);
1108 XGetSubImage( display
, descr
->drawable
, descr
->xDest
, descr
->yDest
,
1109 descr
->width
, descr
->height
, AllPlanes
, ZPixmap
,
1110 bmpImage
, descr
->xSrc
, descr
->ySrc
);
1112 /* Transfer the pixels */
1113 switch(descr
->infoBpp
)
1116 FIXME(bitmap
, "Depth 1 not yet supported!\n");
1120 if (descr
->compression
) FIXME(bitmap
, "Compression not yet supported!\n");
1121 else DIB_GetImageBits_4( descr
->lines
, (LPVOID
)descr
->bits
, descr
->infoWidth
,
1122 descr
->width
, descr
->xSrc
,
1123 descr
->colorMap
, descr
->nColorMap
, bmpImage
);
1127 if (descr
->compression
) FIXME(bitmap
, "Compression not yet supported!\n");
1128 else DIB_GetImageBits_8( descr
->lines
, (LPVOID
)descr
->bits
, descr
->infoWidth
,
1129 descr
->width
, descr
->xSrc
,
1130 descr
->colorMap
, descr
->nColorMap
, bmpImage
);
1135 DIB_GetImageBits_16( descr
->lines
, (LPVOID
)descr
->bits
, descr
->infoWidth
,
1136 descr
->width
, descr
->xSrc
, bmpImage
);
1140 DIB_GetImageBits_24( descr
->lines
, (LPVOID
)descr
->bits
, descr
->infoWidth
,
1141 descr
->width
, descr
->xSrc
, bmpImage
);
1145 DIB_GetImageBits_32( descr
->lines
, (LPVOID
)descr
->bits
, descr
->infoWidth
,
1146 descr
->width
, descr
->xSrc
, bmpImage
);
1150 WARN(bitmap
, "(%d): Invalid depth\n", descr
->infoBpp
);
1154 if (!descr
->image
) XDestroyImage( bmpImage
);
1159 /***********************************************************************
1160 * StretchDIBits16 (GDI.439)
1162 INT16 WINAPI
StretchDIBits16(HDC16 hdc
, INT16 xDst
, INT16 yDst
, INT16 widthDst
,
1163 INT16 heightDst
, INT16 xSrc
, INT16 ySrc
, INT16 widthSrc
,
1164 INT16 heightSrc
, const VOID
*bits
,
1165 const BITMAPINFO
*info
, UINT16 wUsage
, DWORD dwRop
)
1167 return (INT16
)StretchDIBits32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
1168 xSrc
, ySrc
, widthSrc
, heightSrc
, bits
,
1169 info
, wUsage
, dwRop
);
1173 /***********************************************************************
1174 * StretchDIBits32 (GDI32.351)
1176 INT32 WINAPI
StretchDIBits32(HDC32 hdc
, INT32 xDst
, INT32 yDst
, INT32 widthDst
,
1177 INT32 heightDst
, INT32 xSrc
, INT32 ySrc
, INT32 widthSrc
,
1178 INT32 heightSrc
, const void *bits
,
1179 const BITMAPINFO
*info
, UINT32 wUsage
, DWORD dwRop
)
1181 HBITMAP32 hBitmap
, hOldBitmap
;
1184 hBitmap
= CreateDIBitmap32( hdc
, &info
->bmiHeader
, CBM_INIT
,
1185 bits
, info
, wUsage
);
1186 hdcMem
= CreateCompatibleDC32( hdc
);
1187 hOldBitmap
= SelectObject32( hdcMem
, hBitmap
);
1188 StretchBlt32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
1189 hdcMem
, xSrc
, ySrc
, widthSrc
, heightSrc
, dwRop
);
1190 SelectObject32( hdcMem
, hOldBitmap
);
1191 DeleteDC32( hdcMem
);
1192 DeleteObject32( hBitmap
);
1197 /***********************************************************************
1198 * SetDIBits16 (GDI.440)
1200 INT16 WINAPI
SetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
1201 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
1204 return SetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
1208 /******************************************************************************
1209 * SetDIBits32 [GDI32.312] Sets pixels in a bitmap using colors from DIB
1212 * hdc [I] Handle to device context
1213 * hbitmap [I] Handle to bitmap
1214 * startscan [I] Starting scan line
1215 * lines [I] Number of scan lines
1216 * bits [I] Array of bitmap bits
1217 * info [I] Address of structure with data
1218 * coloruse [I] Type of color indexes to use
1221 * Success: Number of scan lines copied
1224 INT32 WINAPI
SetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
1225 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
1228 DIB_SETIMAGEBITS_DESCR descr
;
1230 int height
, tmpheight
;
1233 /* Check parameters */
1235 descr
.dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1238 descr
.dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1239 if (!descr
.dc
) return 0;
1241 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
1243 GDI_HEAP_UNLOCK( hdc
);
1246 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &descr
.infoWidth
, &height
,
1247 &descr
.infoBpp
, &descr
.compression
) == -1)
1249 GDI_HEAP_UNLOCK( hbitmap
);
1250 GDI_HEAP_UNLOCK( hdc
);
1254 if (height
< 0) height
= -height
;
1255 if (!lines
|| (startscan
>= height
))
1257 GDI_HEAP_UNLOCK( hbitmap
);
1258 GDI_HEAP_UNLOCK( hdc
);
1261 if (startscan
+ lines
> height
) lines
= height
- startscan
;
1263 if (descr
.infoBpp
<= 8)
1265 descr
.colorMap
= DIB_BuildColorMap( descr
.dc
, coloruse
, bmp
->bitmap
.bmBitsPixel
,
1266 info
, &descr
.nColorMap
);
1267 if (!descr
.colorMap
)
1269 GDI_HEAP_UNLOCK( hbitmap
);
1270 GDI_HEAP_UNLOCK( hdc
);
1277 descr
.lines
= tmpheight
>= 0 ? lines
: -lines
;
1278 descr
.depth
= bmp
->bitmap
.bmBitsPixel
;
1279 descr
.drawable
= bmp
->pixmap
;
1280 descr
.gc
= BITMAP_GC(bmp
);
1284 descr
.yDest
= height
- startscan
- lines
;
1285 descr
.width
= bmp
->bitmap
.bmWidth
;
1286 descr
.height
= lines
;
1288 EnterCriticalSection( &X11DRV_CritSection
);
1289 result
= CALL_LARGE_STACK( DIB_SetImageBits
, &descr
);
1290 LeaveCriticalSection( &X11DRV_CritSection
);
1292 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
1294 GDI_HEAP_UNLOCK( hdc
);
1295 GDI_HEAP_UNLOCK( hbitmap
);
1300 /***********************************************************************
1301 * SetDIBitsToDevice16 (GDI.443)
1303 INT16 WINAPI
SetDIBitsToDevice16(HDC16 hdc
, INT16 xDest
, INT16 yDest
, INT16 cx
,
1304 INT16 cy
, INT16 xSrc
, INT16 ySrc
, UINT16 startscan
,
1305 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
1308 return SetDIBitsToDevice32( hdc
, xDest
, yDest
, cx
, cy
, xSrc
, ySrc
,
1309 startscan
, lines
, bits
, info
, coloruse
);
1313 /***********************************************************************
1314 * SetDIBitsToDevice32 (GDI32.313)
1316 INT32 WINAPI
SetDIBitsToDevice32(HDC32 hdc
, INT32 xDest
, INT32 yDest
, DWORD cx
,
1317 DWORD cy
, INT32 xSrc
, INT32 ySrc
, UINT32 startscan
,
1318 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
1321 DIB_SETIMAGEBITS_DESCR descr
;
1323 DWORD width
, oldcy
= cy
;
1325 int height
, tmpheight
;
1327 /* Check parameters */
1329 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1332 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1335 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
,
1336 &descr
.infoBpp
, &descr
.compression
) == -1)
1339 if (height
< 0) height
= -height
;
1340 if (!lines
|| (startscan
>= height
)) return 0;
1341 if (startscan
+ lines
> height
) lines
= height
- startscan
;
1342 if (ySrc
< startscan
) ySrc
= startscan
;
1343 else if (ySrc
>= startscan
+ lines
) return 0;
1344 if (xSrc
>= width
) return 0;
1345 if (ySrc
+ cy
>= startscan
+ lines
) cy
= startscan
+ lines
- ySrc
;
1346 if (xSrc
+ cx
>= width
) cx
= width
- xSrc
;
1347 if (!cx
|| !cy
) return 0;
1349 DC_SetupGCForText( dc
); /* To have the correct colors */
1350 TSXSetFunction( display
, dc
->u
.x
.gc
, DC_XROPfunction
[dc
->w
.ROPmode
-1] );
1352 if (descr
.infoBpp
<= 8)
1354 descr
.colorMap
= DIB_BuildColorMap( dc
, coloruse
, dc
->w
.bitsPerPixel
,
1355 info
, &descr
.nColorMap
);
1356 if (!descr
.colorMap
)
1363 descr
.lines
= tmpheight
>= 0 ? lines
: -lines
;
1364 descr
.infoWidth
= width
;
1365 descr
.depth
= dc
->w
.bitsPerPixel
;
1366 descr
.drawable
= dc
->u
.x
.drawable
;
1367 descr
.gc
= dc
->u
.x
.gc
;
1369 descr
.ySrc
= tmpheight
>= 0 ? lines
-(ySrc
-startscan
)-cy
+(oldcy
-cy
) : ySrc
- startscan
;
1370 descr
.xDest
= dc
->w
.DCOrgX
+ XLPTODP( dc
, xDest
);
1371 descr
.yDest
= dc
->w
.DCOrgY
+ YLPTODP( dc
, yDest
) + (tmpheight
>= 0 ? oldcy
-cy
: 0);
1375 EnterCriticalSection( &X11DRV_CritSection
);
1376 result
= CALL_LARGE_STACK( DIB_SetImageBits
, &descr
);
1377 LeaveCriticalSection( &X11DRV_CritSection
);
1379 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
1383 /***********************************************************************
1384 * SetDIBColorTable16 (GDI.602)
1386 UINT16 WINAPI
SetDIBColorTable16( HDC16 hdc
, UINT16 startpos
, UINT16 entries
,
1389 return SetDIBColorTable32( hdc
, startpos
, entries
, colors
);
1392 /***********************************************************************
1393 * SetDIBColorTable32 (GDI32.311)
1395 UINT32 WINAPI
SetDIBColorTable32( HDC32 hdc
, UINT32 startpos
, UINT32 entries
,
1399 PALETTEENTRY
* palEntry
;
1400 PALETTEOBJ
* palette
;
1403 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1406 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1410 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
1415 /* Transfer color info */
1417 if (dc
->w
.bitsPerPixel
<= 8) {
1418 palEntry
= palette
->logpalette
.palPalEntry
+ startpos
;
1419 if (startpos
+ entries
> (1 << dc
->w
.bitsPerPixel
)) {
1420 entries
= (1 << dc
->w
.bitsPerPixel
) - startpos
;
1422 for (end
= colors
+ entries
; colors
< end
; palEntry
++, colors
++)
1424 palEntry
->peRed
= colors
->rgbRed
;
1425 palEntry
->peGreen
= colors
->rgbGreen
;
1426 palEntry
->peBlue
= colors
->rgbBlue
;
1431 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1435 /***********************************************************************
1436 * GetDIBColorTable16 (GDI.603)
1438 UINT16 WINAPI
GetDIBColorTable16( HDC16 hdc
, UINT16 startpos
, UINT16 entries
,
1441 return GetDIBColorTable32( hdc
, startpos
, entries
, colors
);
1444 /***********************************************************************
1445 * GetDIBColorTable32 (GDI32.169)
1447 UINT32 WINAPI
GetDIBColorTable32( HDC32 hdc
, UINT32 startpos
, UINT32 entries
,
1451 PALETTEENTRY
* palEntry
;
1452 PALETTEOBJ
* palette
;
1455 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1458 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1462 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
1467 /* Transfer color info */
1469 if (dc
->w
.bitsPerPixel
<= 8) {
1470 palEntry
= palette
->logpalette
.palPalEntry
+ startpos
;
1471 if (startpos
+ entries
> (1 << dc
->w
.bitsPerPixel
)) {
1472 entries
= (1 << dc
->w
.bitsPerPixel
) - startpos
;
1474 for (end
= colors
+ entries
; colors
< end
; palEntry
++, colors
++)
1476 colors
->rgbRed
= palEntry
->peRed
;
1477 colors
->rgbGreen
= palEntry
->peGreen
;
1478 colors
->rgbBlue
= palEntry
->peBlue
;
1479 colors
->rgbReserved
= 0;
1484 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1488 /***********************************************************************
1489 * GetDIBits16 (GDI.441)
1491 INT16 WINAPI
GetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
1492 UINT16 lines
, LPSTR bits
, BITMAPINFO
* info
,
1495 return GetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
1499 /******************************************************************************
1500 * GetDIBits32 [GDI32.170] Retrieves bits of bitmap and copies to buffer
1503 * Success: Number of scan lines copied from bitmap
1506 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
1508 INT32 WINAPI
GetDIBits32(
1509 HDC32 hdc
, /* [in] Handle to device context */
1510 HBITMAP32 hbitmap
, /* [in] Handle to bitmap */
1511 UINT32 startscan
, /* [in] First scan line to set in dest bitmap */
1512 UINT32 lines
, /* [in] Number of scan lines to copy */
1513 LPSTR bits
, /* [out] Address of array for bitmap bits */
1514 BITMAPINFO
* info
, /* [out] Address of structure with bitmap data */
1515 UINT32 coloruse
) /* [in] RGB or palette index */
1519 PALETTEENTRY
* palEntry
;
1520 PALETTEOBJ
* palette
;
1524 if (!lines
) return 0;
1525 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1528 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1531 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
1533 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
1535 GDI_HEAP_UNLOCK( hbitmap
);
1539 /* Transfer color info */
1541 if (info
->bmiHeader
.biBitCount
<=8) {
1542 palEntry
= palette
->logpalette
.palPalEntry
;
1543 for (i
= 0; i
< info
->bmiHeader
.biClrUsed
; i
++, palEntry
++)
1545 if (coloruse
== DIB_RGB_COLORS
)
1547 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
1548 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
1549 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
1550 info
->bmiColors
[i
].rgbReserved
= 0;
1552 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
1559 int pad
, yend
, xend
= bmp
->bitmap
.bmWidth
;
1561 TRACE(bitmap
, "%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
1562 lines
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
1563 (int)info
->bmiHeader
.biWidth
, (int)info
->bmiHeader
.biHeight
, startscan
);
1565 /* adjust number of scanlines to copy */
1567 if( lines
> info
->bmiHeader
.biHeight
) lines
= info
->bmiHeader
.biHeight
;
1568 yend
= startscan
+ lines
;
1569 if( startscan
>= bmp
->bitmap
.bmHeight
)
1571 GDI_HEAP_UNLOCK( hbitmap
);
1572 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1575 if( yend
> bmp
->bitmap
.bmHeight
) yend
= bmp
->bitmap
.bmHeight
;
1577 /* adjust scanline width */
1579 pad
= info
->bmiHeader
.biWidth
- bmp
->bitmap
.bmWidth
;
1582 /* bitmap is wider than DIB, copy only a part */
1585 xend
= info
->bmiHeader
.biWidth
;
1588 EnterCriticalSection( &X11DRV_CritSection
);
1589 bmpImage
= (XImage
*)CALL_LARGE_STACK( BITMAP_GetXImage
, bmp
);
1591 switch( info
->bmiHeader
.biBitCount
)
1594 /* pad up to 32 bit */
1595 pad
+= (4 - (info
->bmiHeader
.biWidth
& 3)) & 3;
1596 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1598 for( x
= 0; x
< xend
; x
++ )
1599 *bbits
++ = XGetPixel( bmpImage
, x
, y
);
1604 pad
+= ((32 - (info
->bmiHeader
.biWidth
& 31)) / 8) & 3;
1605 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1607 for( x
= 0; x
< xend
; x
++ ) {
1608 if (!(x
&7)) *bbits
= 0;
1609 *bbits
|= XGetPixel( bmpImage
, x
, y
)<<(7-(x
&7));
1610 if ((x
&7)==7) bbits
++;
1616 pad
+= ((8 - (info
->bmiHeader
.biWidth
& 7)) / 2) & 3;
1617 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1619 for( x
= 0; x
< xend
; x
++ ) {
1620 if (!(x
&1)) *bbits
= 0;
1621 *bbits
|= XGetPixel( bmpImage
, x
, y
)<<(4*(1-(x
&1)));
1622 if ((x
&1)==1) bbits
++;
1629 pad
+= (4 - ((info
->bmiHeader
.biWidth
*2) & 3)) & 3;
1630 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1632 for( x
= 0; x
< xend
; x
++ ) {
1633 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
1634 *bbits
++ = pixel
& 0xff;
1635 *bbits
++ = (pixel
>> 8) & 0xff;
1641 pad
+= (4 - ((info
->bmiHeader
.biWidth
*3) & 3)) & 3;
1642 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1644 for( x
= 0; x
< xend
; x
++ ) {
1645 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
1646 *bbits
++ = (pixel
>>16) & 0xff;
1647 *bbits
++ = (pixel
>> 8) & 0xff;
1648 *bbits
++ = pixel
& 0xff;
1654 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1656 for( x
= 0; x
< xend
; x
++ ) {
1657 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
1658 *bbits
++ = (pixel
>>16) & 0xff;
1659 *bbits
++ = (pixel
>> 8) & 0xff;
1660 *bbits
++ = pixel
& 0xff;
1665 WARN(bitmap
,"Unsupported depth %d\n",
1666 info
->bmiHeader
.biBitCount
);
1670 XDestroyImage( bmpImage
);
1671 LeaveCriticalSection( &X11DRV_CritSection
);
1673 info
->bmiHeader
.biCompression
= 0;
1675 else if( info
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
) )
1677 /* fill in struct members */
1679 info
->bmiHeader
.biWidth
= bmp
->bitmap
.bmWidth
;
1680 info
->bmiHeader
.biHeight
= bmp
->bitmap
.bmHeight
;
1681 info
->bmiHeader
.biPlanes
= 1;
1682 info
->bmiHeader
.biBitCount
= bmp
->bitmap
.bmBitsPixel
;
1683 info
->bmiHeader
.biSizeImage
= bmp
->bitmap
.bmHeight
*
1684 DIB_GetDIBWidthBytes( bmp
->bitmap
.bmWidth
,
1685 bmp
->bitmap
.bmBitsPixel
);
1686 info
->bmiHeader
.biCompression
= 0;
1689 GDI_HEAP_UNLOCK( hbitmap
);
1690 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1695 /***********************************************************************
1696 * CreateDIBitmap16 (GDI.442)
1698 HBITMAP16 WINAPI
CreateDIBitmap16( HDC16 hdc
, const BITMAPINFOHEADER
* header
,
1699 DWORD init
, LPCVOID bits
, const BITMAPINFO
* data
,
1702 return CreateDIBitmap32( hdc
, header
, init
, bits
, data
, coloruse
);
1706 /***********************************************************************
1707 * CreateDIBitmap32 (GDI32.37)
1709 HBITMAP32 WINAPI
CreateDIBitmap32( HDC32 hdc
, const BITMAPINFOHEADER
*header
,
1710 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
1720 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
, &compr
) == -1) return 0;
1721 if (height
< 0) height
= -height
;
1723 /* Check if we should create a monochrome or color bitmap. */
1724 /* We create a monochrome bitmap only if it has exactly 2 */
1725 /* colors, which are either black or white, nothing else. */
1726 /* In all other cases, we create a color bitmap. */
1728 if (bpp
!= 1) fColor
= TRUE
;
1729 else if ((coloruse
!= DIB_RGB_COLORS
) ||
1730 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1733 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
1735 RGBQUAD
*rgb
= data
->bmiColors
;
1736 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1737 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
1740 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1741 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
1745 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1747 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
1748 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
1749 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
1752 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
1753 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
1759 WARN(bitmap
, "(%ld): wrong size for data\n",
1760 data
->bmiHeader
.biSize
);
1765 /* Now create the bitmap */
1767 handle
= fColor
? CreateBitmap32( width
, height
, 1, screenDepth
, NULL
) :
1768 CreateBitmap32( width
, height
, 1, 1, NULL
);
1769 if (!handle
) return 0;
1771 if (init
== CBM_INIT
)
1772 SetDIBits32( hdc
, handle
, 0, height
, bits
, data
, coloruse
);
1777 /***********************************************************************
1778 * DIB_DoProtectDIBSection
1780 static void DIB_DoProtectDIBSection( BITMAPOBJ
*bmp
, DWORD new_prot
)
1782 DIBSECTION
*dib
= &bmp
->dib
->dibSection
;
1783 INT32 effHeight
= dib
->dsBm
.bmHeight
>= 0? dib
->dsBm
.bmHeight
1784 : -dib
->dsBm
.bmHeight
;
1785 INT32 totalSize
= dib
->dsBmih
.biSizeImage
? dib
->dsBmih
.biSizeImage
1786 : dib
->dsBm
.bmWidthBytes
* effHeight
;
1789 VirtualProtect(dib
->dsBm
.bmBits
, totalSize
, new_prot
, &old_prot
);
1790 TRACE(bitmap
, "Changed protection from %ld to %ld\n",
1791 old_prot
, new_prot
);
1794 /***********************************************************************
1795 * DIB_DoUpdateDIBSection
1797 static void DIB_DoUpdateDIBSection( BITMAPOBJ
*bmp
, BOOL32 toDIB
)
1799 DIBSECTIONOBJ
*dib
= bmp
->dib
;
1800 DIB_SETIMAGEBITS_DESCR descr
;
1802 if (DIB_GetBitmapInfo( &dib
->dibSection
.dsBmih
, &descr
.infoWidth
, &descr
.lines
,
1803 &descr
.infoBpp
, &descr
.compression
) == -1)
1807 descr
.image
= dib
->image
;
1808 descr
.colorMap
= dib
->colorMap
;
1809 descr
.nColorMap
= dib
->nColorMap
;
1810 descr
.bits
= dib
->dibSection
.dsBm
.bmBits
;
1811 descr
.depth
= bmp
->bitmap
.bmBitsPixel
;
1812 descr
.drawable
= bmp
->pixmap
;
1813 descr
.gc
= BITMAP_GC(bmp
);
1818 descr
.width
= bmp
->bitmap
.bmWidth
;
1819 descr
.height
= bmp
->bitmap
.bmHeight
;
1823 TRACE(bitmap
, "Copying from Pixmap to DIB bits\n");
1824 EnterCriticalSection( &X11DRV_CritSection
);
1825 CALL_LARGE_STACK( DIB_GetImageBits
, &descr
);
1826 LeaveCriticalSection( &X11DRV_CritSection
);
1830 TRACE(bitmap
, "Copying from DIB bits to Pixmap\n");
1831 EnterCriticalSection( &X11DRV_CritSection
);
1832 CALL_LARGE_STACK( DIB_SetImageBits
, &descr
);
1833 LeaveCriticalSection( &X11DRV_CritSection
);
1837 /***********************************************************************
1840 static BOOL32
DIB_FaultHandler( LPVOID res
, LPVOID addr
)
1842 BOOL32 handled
= FALSE
;
1845 bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( (HBITMAP32
)res
, BITMAP_MAGIC
);
1846 if (!bmp
) return FALSE
;
1849 switch (bmp
->dib
->status
)
1852 TRACE( bitmap
, "called in status DIB_GdiMod\n" );
1853 DIB_DoProtectDIBSection( bmp
, PAGE_READWRITE
);
1854 DIB_DoUpdateDIBSection( bmp
, TRUE
);
1855 DIB_DoProtectDIBSection( bmp
, PAGE_READONLY
);
1856 bmp
->dib
->status
= DIB_InSync
;
1861 TRACE( bitmap
, "called in status DIB_InSync\n" );
1862 DIB_DoProtectDIBSection( bmp
, PAGE_READWRITE
);
1863 bmp
->dib
->status
= DIB_AppMod
;
1868 FIXME( bitmap
, "called in status DIB_AppMod: "
1869 "this can't happen!\n" );
1873 FIXME( bitmap
, "called in status DIB_NoHandler: "
1874 "this can't happen!\n" );
1878 GDI_HEAP_UNLOCK( (HBITMAP32
)res
);
1882 /***********************************************************************
1883 * DIB_UpdateDIBSection
1885 void DIB_UpdateDIBSection( DC
*dc
, BOOL32 toDIB
)
1889 /* Ensure this is a Compatible DC that has a DIB section selected */
1892 if (!(dc
->w
.flags
& DC_MEMORY
)) return;
1894 bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( dc
->w
.hBitmap
, BITMAP_MAGIC
);
1899 GDI_HEAP_UNLOCK(dc
->w
.hBitmap
);
1906 /* Prepare for access to the DIB by GDI functions */
1908 switch (bmp
->dib
->status
)
1912 DIB_DoUpdateDIBSection( bmp
, FALSE
);
1916 TRACE( bitmap
, "fromDIB called in status DIB_GdiMod\n" );
1921 TRACE( bitmap
, "fromDIB called in status DIB_InSync\n" );
1926 TRACE( bitmap
, "fromDIB called in status DIB_AppMod\n" );
1927 DIB_DoUpdateDIBSection( bmp
, FALSE
);
1928 DIB_DoProtectDIBSection( bmp
, PAGE_READONLY
);
1929 bmp
->dib
->status
= DIB_InSync
;
1935 /* Acknowledge write access to the DIB by GDI functions */
1937 switch (bmp
->dib
->status
)
1941 DIB_DoUpdateDIBSection( bmp
, TRUE
);
1945 TRACE( bitmap
, " toDIB called in status DIB_GdiMod\n" );
1950 TRACE( bitmap
, " toDIB called in status DIB_InSync\n" );
1951 DIB_DoProtectDIBSection( bmp
, PAGE_NOACCESS
);
1952 bmp
->dib
->status
= DIB_GdiMod
;
1956 FIXME( bitmap
, " toDIB called in status DIB_AppMod: "
1957 "this can't happen!\n" );
1963 GDI_HEAP_UNLOCK(dc
->w
.hBitmap
);
1966 /***********************************************************************
1967 * CreateDIBSection16 (GDI.489)
1969 HBITMAP16 WINAPI
CreateDIBSection16 (HDC16 hdc
, BITMAPINFO
*bmi
, UINT16 usage
,
1970 LPVOID
**bits
, HANDLE32 section
,
1973 return CreateDIBSection32(hdc
, bmi
, usage
, bits
, section
, offset
);
1976 /***********************************************************************
1977 * CreateDIBSection32 (GDI32.36)
1979 HBITMAP32 WINAPI
CreateDIBSection32 (HDC32 hdc
, BITMAPINFO
*bmi
, UINT32 usage
,
1980 LPVOID
**bits
,HANDLE32 section
,
1984 BITMAPOBJ
*bmp
= NULL
;
1985 DIBSECTIONOBJ
*dib
= NULL
;
1986 int *colorMap
= NULL
;
1989 /* Fill BITMAP32 structure with DIB data */
1990 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1991 INT32 effHeight
, totalSize
;
1994 TRACE(bitmap
, "format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1995 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1996 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1999 bm
.bmWidth
= bi
->biWidth
;
2000 bm
.bmHeight
= bi
->biHeight
;
2001 bm
.bmWidthBytes
= DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
2002 bm
.bmPlanes
= bi
->biPlanes
;
2003 bm
.bmBitsPixel
= bi
->biBitCount
;
2006 /* Get storage location for DIB bits */
2007 effHeight
= bm
.bmHeight
>= 0 ? bm
.bmHeight
: -bm
.bmHeight
;
2008 totalSize
= bi
->biSizeImage
? bi
->biSizeImage
: bm
.bmWidthBytes
* effHeight
;
2011 bm
.bmBits
= MapViewOfFile(section
, FILE_MAP_ALL_ACCESS
,
2012 0L, offset
, totalSize
);
2014 bm
.bmBits
= VirtualAlloc(NULL
, totalSize
,
2015 MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
2017 /* Create Color Map */
2018 if (bm
.bmBits
&& bm
.bmBitsPixel
<= 8)
2020 DC
*dc
= hdc
? (DC
*)GDI_GetObjPtr(hdc
, DC_MAGIC
) : NULL
;
2021 if (hdc
&& !dc
) dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
2024 colorMap
= DIB_BuildColorMap( dc
, usage
, bm
.bmBitsPixel
,
2026 GDI_HEAP_UNLOCK(hdc
);
2029 /* Allocate Memory for DIB and fill structure */
2031 dib
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(DIBSECTIONOBJ
));
2034 dib
->dibSection
.dsBm
= bm
;
2035 dib
->dibSection
.dsBmih
= *bi
;
2036 /* FIXME: dib->dibSection.dsBitfields ??? */
2037 dib
->dibSection
.dshSection
= section
;
2038 dib
->dibSection
.dsOffset
= offset
;
2040 dib
->status
= DIB_NoHandler
;
2042 dib
->nColorMap
= nColorMap
;
2043 dib
->colorMap
= colorMap
;
2046 /* Create Device Dependent Bitmap and add DIB pointer */
2049 res
= CreateDIBitmap32(hdc
, bi
, 0, NULL
, bmi
, usage
);
2050 bmp
= (BITMAPOBJ
*) GDI_GetObjPtr(res
, BITMAP_MAGIC
);
2051 if (bmp
) bmp
->dib
= dib
;
2056 XCREATEIMAGE( dib
->image
, bm
.bmWidth
, effHeight
, bmp
->bitmap
.bmBitsPixel
);
2058 /* Clean up in case of errors */
2059 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
|| (bm
.bmBitsPixel
<= 8 && !colorMap
))
2063 UnmapViewOfFile(bm
.bmBits
), bm
.bmBits
= NULL
;
2065 VirtualFree(bm
.bmBits
, MEM_RELEASE
, 0L), bm
.bmBits
= NULL
;
2067 if (dib
->image
) XDestroyImage(dib
->image
), dib
->image
= NULL
;
2068 if (colorMap
) HeapFree(GetProcessHeap(), 0, colorMap
), colorMap
= NULL
;
2069 if (dib
) HeapFree(GetProcessHeap(), 0, dib
), dib
= NULL
;
2070 if (res
) DeleteObject32(res
), res
= 0;
2073 /* Install fault handler, if possible */
2075 if (VIRTUAL_SetFaultHandler(bm
.bmBits
, DIB_FaultHandler
, (LPVOID
)res
))
2077 DIB_DoProtectDIBSection( bmp
, PAGE_READONLY
);
2078 dib
->status
= DIB_InSync
;
2081 /* Return BITMAP handle and storage location */
2082 if (res
) GDI_HEAP_UNLOCK(res
);
2083 if (bm
.bmBits
&& bits
) *bits
= bm
.bmBits
;
2087 /***********************************************************************
2088 * DIB_DeleteDIBSection
2090 void DIB_DeleteDIBSection( BITMAPOBJ
*bmp
)
2092 if (bmp
&& bmp
->dib
)
2094 DIBSECTIONOBJ
*dib
= bmp
->dib
;
2096 if (dib
->dibSection
.dsBm
.bmBits
)
2097 if (dib
->dibSection
.dshSection
)
2098 UnmapViewOfFile(dib
->dibSection
.dsBm
.bmBits
);
2100 VirtualFree(dib
->dibSection
.dsBm
.bmBits
, MEM_RELEASE
, 0L);
2103 XDestroyImage( dib
->image
);
2106 HeapFree(GetProcessHeap(), 0, dib
->colorMap
);
2108 HeapFree(GetProcessHeap(), 0, dib
);