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() */
42 } DIB_SETIMAGEBITS_DESCR
;
45 /***********************************************************************
53 for( i
= 0; bitmapDepthTable
[i
]; i
++ )
55 testimage
= TSXCreateImage(display
, DefaultVisualOfScreen(screen
),
56 bitmapDepthTable
[i
], ZPixmap
, 0, NULL
, 1, 1, 32, 20 );
57 if( testimage
) ximageDepthTable
[i
] = testimage
->bits_per_pixel
;
59 TSXDestroyImage(testimage
);
64 /***********************************************************************
65 * DIB_GetXImageWidthBytes
67 * Return the width of an X image in bytes
69 int DIB_GetXImageWidthBytes( int width
, int depth
)
73 if (!ximageDepthTable
[0]) {
76 for( i
= 0; bitmapDepthTable
[i
] ; i
++ )
77 if( bitmapDepthTable
[i
] == depth
)
78 return (4 * ((width
* ximageDepthTable
[i
] + 31)/32));
80 WARN(bitmap
, "(%d): Unsupported depth\n", depth
);
84 /***********************************************************************
85 * DIB_GetDIBWidthBytes
87 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
88 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
90 int DIB_GetDIBWidthBytes( int width
, int depth
)
96 case 1: words
= (width
+ 31) / 32; break;
97 case 4: words
= (width
+ 7) / 8; break;
98 case 8: words
= (width
+ 3) / 4; break;
100 case 16: words
= (width
+ 1) / 2; break;
101 case 24: words
= (width
* 3 + 3)/4; break;
104 WARN(bitmap
, "(%d): Unsupported depth\n", depth
);
113 /***********************************************************************
116 * Return the size of the bitmap info structure including color table.
118 int DIB_BitmapInfoSize( BITMAPINFO
* info
, WORD coloruse
)
122 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
124 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
125 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
126 return sizeof(BITMAPCOREHEADER
) + colors
*
127 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
129 else /* assume BITMAPINFOHEADER */
131 colors
= info
->bmiHeader
.biClrUsed
;
132 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
133 colors
= 1 << info
->bmiHeader
.biBitCount
;
134 return sizeof(BITMAPINFOHEADER
) + colors
*
135 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
140 /***********************************************************************
143 * Get the info from a bitmap header.
144 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
146 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
147 int *height
, WORD
*bpp
, WORD
*compr
)
149 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
151 *width
= header
->biWidth
;
152 *height
= header
->biHeight
;
153 *bpp
= header
->biBitCount
;
154 *compr
= header
->biCompression
;
157 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
159 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
160 *width
= core
->bcWidth
;
161 *height
= core
->bcHeight
;
162 *bpp
= core
->bcBitCount
;
166 WARN(bitmap
, "(%ld): wrong size for header\n", header
->biSize
);
171 /***********************************************************************
174 * Build the color map from the bitmap palette. Should not be called
175 * for a >8-bit deep bitmap.
177 static RGBQUAD
*DIB_BuildColorMap( const BITMAPINFO
*info
,
178 HPALETTE16 hPalette
, int *nColors
)
183 RGBQUAD
*colorMapping
;
185 if ((isInfo
= (info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))))
187 colors
= info
->bmiHeader
.biClrUsed
;
188 if (!colors
) colors
= 1 << info
->bmiHeader
.biBitCount
;
189 colorPtr
= (WORD
*)info
->bmiColors
;
191 else /* assume BITMAPCOREINFO */
193 colors
= 1 << ((BITMAPCOREHEADER
*)&info
->bmiHeader
)->bcBitCount
;
194 colorPtr
= (WORD
*)((BITMAPCOREINFO
*)info
)->bmciColors
;
199 ERR(bitmap
, "DIB_BuildColorMap called with >256 colors!\n");
203 if (!(colorMapping
= (RGBQUAD
*)HeapAlloc(GetProcessHeap(), 0,
204 colors
* sizeof(RGBQUAD
) )))
207 if (hPalette
== 0) /* DIB_RGB_COLORS */
211 RGBQUAD
* rgb
= (RGBQUAD
*)colorPtr
;
213 for (i
= 0; i
< colors
; i
++, rgb
++)
214 colorMapping
[i
] = *rgb
;
218 RGBTRIPLE
* rgb
= (RGBTRIPLE
*)colorPtr
;
220 for (i
= 0; i
< colors
; i
++, rgb
++)
221 colorMapping
[i
].rgbRed
= rgb
->rgbtRed
,
222 colorMapping
[i
].rgbGreen
= rgb
->rgbtGreen
,
223 colorMapping
[i
].rgbBlue
= rgb
->rgbtBlue
;
226 else /* DIB_PAL_COLORS */
228 PALETTEOBJ
*palPtr
= (PALETTEOBJ
*)GDI_GetObjPtr(hPalette
, PALETTE_MAGIC
);
231 HeapFree(GetProcessHeap(), 0, colorMapping
);
235 for (i
= 0; i
< colors
; i
++, colorPtr
++)
236 if (*colorPtr
>= palPtr
->logpalette
.palNumEntries
)
238 colorMapping
[i
].rgbRed
= 0;
239 colorMapping
[i
].rgbGreen
= 0;
240 colorMapping
[i
].rgbBlue
= 0;
244 PALETTEENTRY
*pe
= palPtr
->logpalette
.palPalEntry
+ *colorPtr
;
245 colorMapping
[i
].rgbRed
= pe
->peRed
;
246 colorMapping
[i
].rgbGreen
= pe
->peGreen
;
247 colorMapping
[i
].rgbBlue
= pe
->peBlue
;
250 GDI_HEAP_UNLOCK(hPalette
);
257 /***********************************************************************
258 * DIB_PhysicalColorMap
260 * Translate color map to physical colors
262 static int *DIB_PhysicalColorMap( DC
*dc
, int depth
,
263 RGBQUAD
*colorMap
, int nColorMap
)
265 int *colorMapping
, i
;
267 if (!nColorMap
) return NULL
;
268 if (!(colorMapping
= (int *)HeapAlloc(GetProcessHeap(), 0,
269 nColorMap
* sizeof(int) ))) return NULL
;
271 if (depth
== 1) /* Monochrome */
272 for (i
= 0; i
< nColorMap
; i
++, colorMap
++)
273 colorMapping
[i
] = (colorMap
->rgbRed
+ colorMap
->rgbGreen
+
274 colorMap
->rgbBlue
> 255*3/2);
276 for (i
= 0; i
< nColorMap
; i
++, colorMap
++)
277 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(colorMap
->rgbRed
,
283 /***********************************************************************
286 static int DIB_MapColor( int *physMap
, int nPhysMap
, int phys
)
290 for (color
= 0; color
< nPhysMap
; color
++)
291 if (physMap
[color
] == phys
)
294 WARN(bitmap
, "Strange color %08x\n", phys
);
299 /***********************************************************************
300 * DIB_SetImageBits_1_Line
302 * Handles a single line of 1 bit data.
304 static void DIB_SetImageBits_1_Line(DWORD dstwidth
, int left
, int *colors
,
305 XImage
*bmpImage
, int h
, const BYTE
*bits
)
310 dstwidth
+= left
; bits
+= left
>> 3;
312 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
313 for (i
= dstwidth
/8, x
= left
&~7; (i
> 0); i
--)
316 XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] );
317 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 6) & 1] );
318 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 5) & 1] );
319 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 4) & 1] );
320 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 3) & 1] );
321 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 2) & 1] );
322 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 1) & 1] );
323 XPutPixel( bmpImage
, x
++, h
, colors
[pix
& 1] );
328 case 7: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
329 case 6: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
330 case 5: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
331 case 4: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
332 case 3: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
333 case 2: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
334 case 1: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] );
338 /***********************************************************************
341 * SetDIBits for a 1-bit deep DIB.
343 static void DIB_SetImageBits_1( int lines
, const BYTE
*srcbits
,
344 DWORD srcwidth
, DWORD dstwidth
, int left
,
345 int *colors
, XImage
*bmpImage
)
350 DWORD linebytes
= ((srcwidth
+ 31) & ~31) / 8;
353 for (h
= lines
-1; h
>=0; h
--) {
354 DIB_SetImageBits_1_Line(dstwidth
, left
, colors
, bmpImage
, h
, srcbits
);
355 srcbits
+= linebytes
;
359 for (h
= 0; h
< lines
; h
++) {
360 DIB_SetImageBits_1_Line(dstwidth
, left
, colors
, bmpImage
, h
, srcbits
);
361 srcbits
+= linebytes
;
367 /***********************************************************************
370 * SetDIBits for a 4-bit deep DIB.
372 static void DIB_SetImageBits_4( int lines
, const BYTE
*srcbits
,
373 DWORD srcwidth
, DWORD dstwidth
, int left
,
374 int *colors
, XImage
*bmpImage
)
378 const BYTE
*bits
= srcbits
+ (left
>> 1);
381 DWORD linebytes
= ((srcwidth
+7)&~7)/2;
385 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
387 for (h
= lines
-1; h
>= 0; h
--) {
388 for (i
= dstwidth
/2, x
= left
&~1; i
> 0; i
--) {
390 XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 4] );
391 XPutPixel( bmpImage
, x
++, h
, colors
[pix
& 0x0f] );
393 if (dstwidth
& 1) XPutPixel( bmpImage
, x
, h
, colors
[*bits
>> 4] );
394 srcbits
+= linebytes
;
395 bits
= srcbits
+ (left
>> 1);
399 for (h
= 0; h
< lines
; h
++) {
400 for (i
= dstwidth
/2, x
= left
&~1; i
> 0; i
--) {
402 XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 4] );
403 XPutPixel( bmpImage
, x
++, h
, colors
[pix
& 0x0f] );
405 if (dstwidth
& 1) XPutPixel( bmpImage
, x
, h
, colors
[*bits
>> 4] );
406 srcbits
+= linebytes
;
407 bits
= srcbits
+ (left
>> 1);
412 /***********************************************************************
415 * GetDIBits for a 4-bit deep DIB.
417 static void DIB_GetImageBits_4( int lines
, BYTE
*srcbits
,
418 DWORD srcwidth
, DWORD dstwidth
, int left
,
419 int *colors
, int nColors
, XImage
*bmpImage
)
423 BYTE
*bits
= srcbits
+ (left
>> 1);
426 DWORD linebytes
= ((srcwidth
+7)&~7)/2;
430 /* FIXME: should avoid putting x<left pixels (minor speed issue) */
432 for (h
= lines
-1; h
>= 0; h
--) {
433 for (i
= dstwidth
/2, x
= left
&~1; i
> 0; i
--) {
434 *bits
++ = (DIB_MapColor( colors
, nColors
, XGetPixel( bmpImage
, x
++, h
)) << 4)
435 | (DIB_MapColor( colors
, nColors
, XGetPixel( bmpImage
, x
++, h
)) & 0x0f);
438 *bits
= (DIB_MapColor( colors
, nColors
, XGetPixel( bmpImage
, x
++, h
)) << 4);
439 srcbits
+= linebytes
;
440 bits
= srcbits
+ (left
>> 1);
444 for (h
= 0; h
< lines
; h
++) {
445 for (i
= dstwidth
/2, x
= left
&~1; i
> 0; i
--) {
446 *bits
++ = (DIB_MapColor( colors
, nColors
, XGetPixel( bmpImage
, x
++, h
)) << 4)
447 | (DIB_MapColor( colors
, nColors
, XGetPixel( bmpImage
, x
++, h
)) & 0x0f);
450 *bits
= (DIB_MapColor( colors
, nColors
, XGetPixel( bmpImage
, x
++, h
)) << 4);
451 srcbits
+= linebytes
;
452 bits
= srcbits
+ (left
>> 1);
457 #define check_xy(x,y) \
464 /***********************************************************************
465 * DIB_SetImageBits_RLE4
467 * SetDIBits for a 4-bit deep compressed DIB.
469 static void DIB_SetImageBits_RLE4( int lines
, const BYTE
*bits
, DWORD width
,
470 DWORD dstwidth
, int left
, int *colors
, XImage
*bmpImage
)
472 int x
= 0, c
, length
;
473 const BYTE
*begin
= bits
;
475 dstwidth
+= left
; /* FIXME: avoid putting x<left pixels */
478 while ((int)lines
>= 0)
481 if (length
) { /* encoded */
484 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
488 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
500 case 1: /* eopicture */
508 default: /* absolute */
511 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
515 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
519 if ((bits
- begin
) & 1)
526 /***********************************************************************
529 * SetDIBits for an 8-bit deep DIB.
531 static void DIB_SetImageBits_8( int lines
, const BYTE
*srcbits
,
532 DWORD srcwidth
, DWORD dstwidth
, int left
,
533 int *colors
, XImage
*bmpImage
)
537 const BYTE
*bits
= srcbits
+ left
;
539 /* align to 32 bit */
540 DWORD linebytes
= (srcwidth
+ 3) & ~3;
545 for (h
= lines
- 1; h
>= 0; h
--) {
546 for (x
= left
; x
< dstwidth
; x
++, bits
++) {
547 XPutPixel( bmpImage
, x
, h
, colors
[*bits
] );
549 bits
= (srcbits
+= linebytes
) + left
;
553 for (h
= 0; h
< lines
; h
++) {
554 for (x
= left
; x
< dstwidth
; x
++, bits
++) {
555 XPutPixel( bmpImage
, x
, h
, colors
[*bits
] );
557 bits
= (srcbits
+= linebytes
) + left
;
562 /***********************************************************************
565 * GetDIBits for an 8-bit deep DIB.
567 static void DIB_GetImageBits_8( int lines
, BYTE
*srcbits
,
568 DWORD srcwidth
, DWORD dstwidth
, int left
,
569 int *colors
, int nColors
, XImage
*bmpImage
)
573 BYTE
*bits
= srcbits
+ left
;
575 /* align to 32 bit */
576 DWORD linebytes
= (srcwidth
+ 3) & ~3;
581 for (h
= lines
- 1; h
>= 0; h
--) {
582 for (x
= left
; x
< dstwidth
; x
++, bits
++) {
583 if ( XGetPixel( bmpImage
, x
, h
) != colors
[*bits
] )
584 *bits
= DIB_MapColor( colors
, nColors
,
585 XGetPixel( bmpImage
, x
, h
) );
587 bits
= (srcbits
+= linebytes
) + left
;
591 for (h
= 0; h
< lines
; h
++) {
592 for (x
= left
; x
< dstwidth
; x
++, bits
++) {
593 if ( XGetPixel( bmpImage
, x
, h
) != colors
[*bits
] )
594 *bits
= DIB_MapColor( colors
, nColors
,
595 XGetPixel( bmpImage
, x
, h
) );
597 bits
= (srcbits
+= linebytes
) + left
;
602 /***********************************************************************
603 * DIB_SetImageBits_RLE8
605 * SetDIBits for an 8-bit deep compressed DIB.
607 * This function rewritten 941113 by James Youngman. WINE blew out when I
608 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
610 * This was because the algorithm assumed that all RLE8 bitmaps end with the
611 * 'End of bitmap' escape code. This code is very much laxer in what it
612 * allows to end the expansion. Possibly too lax. See the note by
613 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
614 * bitmap should end with RleEnd, but on the other hand, software exists
615 * that produces ones that don't and Windows 3.1 doesn't complain a bit
618 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
619 * James A. Youngman <mbcstjy@afs.man.ac.uk>
623 enum Rle8_EscapeCodes
626 * Apologies for polluting your file's namespace...
628 RleEol
= 0, /* End of line */
629 RleEnd
= 1, /* End of bitmap */
630 RleDelta
= 2 /* Delta */
633 static void DIB_SetImageBits_RLE8( int lines
, const BYTE
*bits
, DWORD width
,
634 DWORD dstwidth
, int left
, int *colors
, XImage
*bmpImage
)
636 int x
; /* X-positon on each line. Increases. */
637 int line
; /* Line #. Starts at lines-1, decreases */
638 const BYTE
*pIn
= bits
; /* Pointer to current position in bits */
639 BYTE length
; /* The length pf a run */
640 BYTE color_index
; /* index into colors[] as read from bits */
641 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
642 WORD color
; /* value of colour[color_index] */
644 if (lines
== 0) /* Let's hope this doesn't happen. */
647 dstwidth
+= left
; /* FIXME: avoid putting x<left pixels */
650 * Note that the bitmap data is stored by Windows starting at the
651 * bottom line of the bitmap and going upwards. Within each line,
652 * the data is stored left-to-right. That's the reason why line
653 * goes from lines-1 to 0. [JAY]
663 * If the length byte is not zero (which is the escape value),
664 * We have a run of length pixels all the same colour. The colour
665 * index is stored next.
667 * If the length byte is zero, we need to read the next byte to
668 * know what to do. [JAY]
673 * [Run-Length] Encoded mode
675 color_index
= (*pIn
++); /* Get the colour index. */
676 color
= colors
[color_index
];
679 XPutPixel(bmpImage
, x
++, line
, color
);
684 * Escape codes (may be an absolute sequence though)
686 escape_code
= (*pIn
++);
689 case RleEol
: /* =0, end of line */
696 case RleEnd
: /* =1, end of bitmap */
699 * Not all RLE8 bitmaps end with this
700 * code. For example, Paint Shop Pro
701 * produces some that don't. That's (I think)
702 * what caused the previous implementation to
705 line
=-1; /* Cause exit from do loop. */
709 case RleDelta
: /* =2, a delta */
712 * Note that deltaing to line 0
713 * will cause an exit from the loop,
714 * which may not be what is intended.
715 * The fact that there is a delta in the bits
716 * almost certainly implies that there is data
717 * to follow. You may feel that we should
718 * jump to the top of the loop to avoid exiting
721 * TODO: Decide what to do here in that case. [JAY]
727 TRACE(bitmap
, "Delta to last line of bitmap "
728 "(wrongly?) causes loop exit\n");
733 default: /* >2, switch to absolute mode */
738 length
= escape_code
;
741 color_index
= (*pIn
++);
742 XPutPixel(bmpImage
, x
++, line
,
743 colors
[color_index
]);
747 * If you think for a moment you'll realise that the
748 * only time we could ever possibly read an odd
749 * number of bytes is when there is a 0x00 (escape),
750 * a value >0x02 (absolute mode) and then an odd-
751 * length run. Therefore this is the only place we
752 * need to worry about it. Everywhere else the
753 * bytes are always read in pairs. [JAY]
756 pIn
++; /* Throw away the pad byte. */
759 } /* switch (escape_code) : Escape sequence */
760 } /* process either an encoded sequence or an escape sequence */
762 /* We expect to come here more than once per line. */
763 } while (line
>= 0); /* Do this until the bitmap is filled */
766 * Everybody comes here at the end.
767 * Check how we exited the loop and print a message if it's a bit odd.
770 if ( (*(pIn
-2) != 0/*escape*/) || (*(pIn
-1)!= RleEnd
) )
772 TRACE(bitmap
, "End-of-bitmap "
773 "without (strictly) proper escape code. Last two "
774 "bytes were: %02X %02X.\n",
781 /***********************************************************************
782 * DIB_SetImageBits_16
784 * SetDIBits for a 16-bit deep DIB.
786 static void DIB_SetImageBits_16( int lines
, const BYTE
*srcbits
,
787 DWORD srcwidth
, DWORD dstwidth
, int left
,
788 DC
*dc
, XImage
*bmpImage
)
796 /* align to 32 bit */
797 DWORD linebytes
= (srcwidth
* 2 + 3) & ~3;
801 ptr
= (LPWORD
) srcbits
+ left
;
803 for (h
= lines
- 1; h
>= 0; h
--) {
804 for (x
= left
; x
< dstwidth
; x
++, ptr
++) {
806 r
= (BYTE
) ((val
& 0x7c00) >> 7);
807 g
= (BYTE
) ((val
& 0x03e0) >> 2);
808 b
= (BYTE
) ((val
& 0x001f) << 3);
809 XPutPixel( bmpImage
, x
, h
,
810 COLOR_ToPhysical(dc
, RGB(r
,g
,b
)) );
812 ptr
= (LPWORD
) (srcbits
+= linebytes
) + left
;
816 for (h
= 0; h
< lines
; h
++) {
817 for (x
= left
; x
< dstwidth
; x
++, ptr
++) {
819 r
= (BYTE
) ((val
& 0x7c00) >> 7);
820 g
= (BYTE
) ((val
& 0x03e0) >> 2);
821 b
= (BYTE
) ((val
& 0x001f) << 3);
822 XPutPixel( bmpImage
, x
, h
,
823 COLOR_ToPhysical(dc
, RGB(r
,g
,b
)) );
825 ptr
= (LPWORD
) (srcbits
+= linebytes
) + left
;
831 /***********************************************************************
832 * DIB_GetImageBits_16
834 * GetDIBits for an 16-bit deep DIB.
836 static void DIB_GetImageBits_16( int lines
, BYTE
*srcbits
,
837 DWORD srcwidth
, DWORD dstwidth
, int left
,
838 int *colors
, int nColors
, XImage
*bmpImage
)
845 /* align to 32 bit */
846 DWORD linebytes
= (srcwidth
* 2 + 3) & ~3;
850 ptr
= (LPWORD
) srcbits
+ left
;
852 for (h
= lines
- 1; h
>= 0; h
--)
854 for (x
= left
; x
< dstwidth
; x
++, ptr
++)
856 COLORREF pixel
= COLOR_ToLogical( XGetPixel( bmpImage
, x
, h
) );
857 r
= (BYTE
) GetRValue(pixel
);
858 g
= (BYTE
) GetGValue(pixel
);
859 b
= (BYTE
) GetBValue(pixel
);
860 *ptr
= ( ((r
<< 7) & 0x7c00) | ((g
<< 2) & 0x03e0) | ((b
>> 3) & 0x001f) );
862 ptr
= (LPWORD
) (srcbits
+= linebytes
) + left
;
866 for (h
= 0; h
< lines
; h
++)
868 for (x
= left
; x
< dstwidth
; x
++, ptr
++)
870 COLORREF pixel
= COLOR_ToLogical( XGetPixel( bmpImage
, x
, h
) );
871 r
= (BYTE
) GetRValue(pixel
);
872 g
= (BYTE
) GetGValue(pixel
);
873 b
= (BYTE
) GetBValue(pixel
);
874 *ptr
= ( ((r
<< 7) & 0x7c00) | ((g
<< 2) & 0x03e0) | ((b
>> 3) & 0x001f) );
877 ptr
= (LPWORD
) (srcbits
+= linebytes
) + left
;
883 /***********************************************************************
884 * DIB_SetImageBits_24
886 * SetDIBits for a 24-bit deep DIB.
888 static void DIB_SetImageBits_24( int lines
, const BYTE
*srcbits
,
889 DWORD srcwidth
, DWORD dstwidth
, int left
,
890 DC
*dc
, XImage
*bmpImage
)
893 const BYTE
*bits
= srcbits
+ left
* 3;
896 /* align to 32 bit */
897 DWORD linebytes
= (srcwidth
* 3 + 3) & ~3;
901 /* "bits" order is reversed for some reason */
904 for (h
= lines
- 1; h
>= 0; h
--) {
905 for (x
= left
; x
< dstwidth
; x
++, bits
+= 3) {
906 XPutPixel( bmpImage
, x
, h
,
907 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])));
909 bits
= (srcbits
+= linebytes
) + left
* 3;
913 for (h
= 0; h
< lines
; h
++) {
914 for (x
= left
; x
< dstwidth
; x
++, bits
+= 3) {
915 XPutPixel( bmpImage
, x
, h
,
916 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])));
918 bits
= (srcbits
+= linebytes
) + left
* 3;
924 /***********************************************************************
925 * DIB_GetImageBits_24
927 * GetDIBits for an 24-bit deep DIB.
929 static void DIB_GetImageBits_24( int lines
, BYTE
*srcbits
,
930 DWORD srcwidth
, DWORD dstwidth
, int left
,
931 int *colors
, int nColors
, XImage
*bmpImage
)
935 BYTE
*bits
= srcbits
+ (left
* 3);
937 /* align to 32 bit */
938 DWORD linebytes
= (srcwidth
* 3 + 3) & ~3;
943 for (h
= lines
- 1; h
>= 0; h
--)
945 for (x
= left
; x
< dstwidth
; x
++, bits
+= 3)
947 COLORREF pixel
= COLOR_ToLogical( XGetPixel( bmpImage
, x
, h
) );
948 bits
[0] = GetRValue(pixel
);
949 bits
[1] = GetGValue(pixel
);
950 bits
[2] = GetBValue(pixel
);
952 bits
= (srcbits
+= linebytes
) + (left
* 3);
956 for (h
= 0; h
< lines
; h
++)
958 for (x
= left
; x
< dstwidth
; x
++, bits
+= 3)
960 COLORREF pixel
= COLOR_ToLogical( XGetPixel( bmpImage
, x
, h
) );
961 bits
[0] = GetRValue(pixel
);
962 bits
[1] = GetGValue(pixel
);
963 bits
[2] = GetBValue(pixel
);
966 bits
= (srcbits
+= linebytes
) + (left
* 3);
972 /***********************************************************************
973 * DIB_SetImageBits_32
975 * SetDIBits for a 32-bit deep DIB.
977 static void DIB_SetImageBits_32( int lines
, const BYTE
*srcbits
,
978 DWORD srcwidth
, DWORD dstwidth
, int left
,
979 DC
*dc
, XImage
*bmpImage
)
982 const BYTE
*bits
= srcbits
+ left
* 4;
985 DWORD linebytes
= (srcwidth
* 4);
990 for (h
= lines
- 1; h
>= 0; h
--) {
991 for (x
= left
; x
< dstwidth
; x
++, bits
+= 4) {
992 XPutPixel( bmpImage
, x
, h
,
993 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])));
995 bits
= (srcbits
+= linebytes
) + left
* 4;
999 for (h
= 0; h
< lines
; h
++) {
1000 for (x
= left
; x
< dstwidth
; x
++, bits
+= 4) {
1001 XPutPixel( bmpImage
, x
, h
,
1002 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])));
1004 bits
= (srcbits
+= linebytes
) + left
* 4;
1010 /***********************************************************************
1011 * DIB_GetImageBits_32
1013 * GetDIBits for an 32-bit deep DIB.
1015 static void DIB_GetImageBits_32( int lines
, BYTE
*srcbits
,
1016 DWORD srcwidth
, DWORD dstwidth
, int left
,
1017 int *colors
, int nColors
, XImage
*bmpImage
)
1021 BYTE
*bits
= srcbits
+ (left
* 4);
1023 /* align to 32 bit */
1024 DWORD linebytes
= (srcwidth
* 4);
1029 for (h
= lines
- 1; h
>= 0; h
--)
1031 for (x
= left
; x
< dstwidth
; x
++, bits
+= 4)
1033 COLORREF pixel
= COLOR_ToLogical( XGetPixel( bmpImage
, x
, h
) );
1034 bits
[0] = GetRValue(pixel
);
1035 bits
[1] = GetGValue(pixel
);
1036 bits
[2] = GetBValue(pixel
);
1038 bits
= (srcbits
+= linebytes
) + (left
* 4);
1042 for (h
= 0; h
< lines
; h
++)
1044 for (x
= left
; x
< dstwidth
; x
++, bits
+= 4)
1046 COLORREF pixel
= COLOR_ToLogical( XGetPixel( bmpImage
, x
, h
) );
1047 bits
[0] = GetRValue(pixel
);
1048 bits
[1] = GetGValue(pixel
);
1049 bits
[2] = GetBValue(pixel
);
1052 bits
= (srcbits
+= linebytes
) + (left
* 4);
1058 /***********************************************************************
1061 * Transfer the bits to an X image.
1062 * Helper function for SetDIBits() and SetDIBitsToDevice().
1063 * The Xlib critical section must be entered before calling this function.
1065 static int DIB_SetImageBits( const DIB_SETIMAGEBITS_DESCR
*descr
)
1071 /* Translate the color mapping table */
1073 if (descr
->infoBpp
> 8) colorMapping
= NULL
;
1074 else if (!(colorMapping
= DIB_PhysicalColorMap( descr
->dc
, descr
->depth
,
1075 descr
->colorMap
, descr
->nColorMap
)))
1078 if ( descr
->dc
&& descr
->dc
->w
.flags
& DC_DIRTY
)
1079 CLIPPING_UpdateGCRegion(descr
->dc
);
1081 /* Transfer the pixels */
1082 lines
= descr
->lines
;
1083 if (lines
< 0) lines
= -lines
;
1084 XCREATEIMAGE(bmpImage
, descr
->infoWidth
, lines
, descr
->depth
);
1086 switch(descr
->infoBpp
)
1089 DIB_SetImageBits_1( descr
->lines
, descr
->bits
, descr
->infoWidth
,
1090 descr
->width
, descr
->xSrc
, colorMapping
, bmpImage
);
1093 if (descr
->compression
) DIB_SetImageBits_RLE4( descr
->lines
, descr
->bits
,
1094 descr
->infoWidth
, descr
->width
, descr
->xSrc
,
1095 colorMapping
, bmpImage
);
1096 else DIB_SetImageBits_4( descr
->lines
, descr
->bits
, descr
->infoWidth
,
1097 descr
->width
, descr
->xSrc
, colorMapping
, bmpImage
);
1100 if (descr
->compression
) DIB_SetImageBits_RLE8( descr
->lines
, descr
->bits
,
1101 descr
->infoWidth
, descr
->width
, descr
->xSrc
,
1102 colorMapping
, bmpImage
);
1103 else DIB_SetImageBits_8( descr
->lines
, descr
->bits
, descr
->infoWidth
,
1104 descr
->width
, descr
->xSrc
, colorMapping
, bmpImage
);
1108 DIB_SetImageBits_16( descr
->lines
, descr
->bits
, descr
->infoWidth
,
1109 descr
->width
, descr
->xSrc
, descr
->dc
, bmpImage
);
1112 DIB_SetImageBits_24( descr
->lines
, descr
->bits
, descr
->infoWidth
,
1113 descr
->width
, descr
->xSrc
, descr
->dc
, bmpImage
);
1116 DIB_SetImageBits_32( descr
->lines
, descr
->bits
, descr
->infoWidth
,
1117 descr
->width
, descr
->xSrc
, descr
->dc
, bmpImage
);
1120 WARN(bitmap
, "(%d): Invalid depth\n", descr
->infoBpp
);
1123 if (colorMapping
) HeapFree( GetProcessHeap(), 0, colorMapping
);
1124 XPutImage( display
, descr
->drawable
, descr
->gc
, bmpImage
,
1125 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
1126 descr
->width
, descr
->height
);
1127 XDestroyImage( bmpImage
);
1131 /***********************************************************************
1134 * Transfer the bits from an X image.
1135 * The Xlib critical section must be entered before calling this function.
1137 static int DIB_GetImageBits( const DIB_SETIMAGEBITS_DESCR
*descr
)
1143 /* Translate the color mapping table */
1145 if (descr
->infoBpp
> 8) colorMapping
= NULL
;
1146 else if (!(colorMapping
= DIB_PhysicalColorMap( descr
->dc
, descr
->depth
,
1147 descr
->colorMap
, descr
->nColorMap
)))
1150 /* Transfer the pixels */
1152 lines
= descr
->lines
;
1153 if (lines
< 0) lines
= -lines
;
1154 bmpImage
= XGetImage( display
, descr
->drawable
, descr
->xDest
, descr
->yDest
,
1155 descr
->width
, descr
->height
, AllPlanes
, ZPixmap
);
1157 switch(descr
->infoBpp
)
1160 FIXME(bitmap
, "Depth 1 not yet supported!\n");
1164 if (descr
->compression
) FIXME(bitmap
, "Compression not yet supported!\n");
1165 else DIB_GetImageBits_4( descr
->lines
, (LPVOID
)descr
->bits
, descr
->infoWidth
,
1166 descr
->width
, descr
->xSrc
,
1167 colorMapping
, descr
->nColorMap
, bmpImage
);
1171 if (descr
->compression
) FIXME(bitmap
, "Compression not yet supported!\n");
1172 else DIB_GetImageBits_8( descr
->lines
, (LPVOID
)descr
->bits
, descr
->infoWidth
,
1173 descr
->width
, descr
->xSrc
,
1174 colorMapping
, descr
->nColorMap
, bmpImage
);
1179 DIB_GetImageBits_16( descr
->lines
, (LPVOID
)descr
->bits
, descr
->infoWidth
,
1180 descr
->width
, descr
->xSrc
,
1181 colorMapping
, descr
->nColorMap
, bmpImage
);
1185 DIB_GetImageBits_24( descr
->lines
, (LPVOID
)descr
->bits
, descr
->infoWidth
,
1186 descr
->width
, descr
->xSrc
,
1187 colorMapping
, descr
->nColorMap
, bmpImage
);
1191 DIB_GetImageBits_32( descr
->lines
, (LPVOID
)descr
->bits
, descr
->infoWidth
,
1192 descr
->width
, descr
->xSrc
,
1193 colorMapping
, descr
->nColorMap
, bmpImage
);
1197 WARN(bitmap
, "(%d): Invalid depth\n", descr
->infoBpp
);
1201 if (colorMapping
) HeapFree( GetProcessHeap(), 0, colorMapping
);
1203 XDestroyImage( bmpImage
);
1208 /***********************************************************************
1209 * StretchDIBits16 (GDI.439)
1211 INT16 WINAPI
StretchDIBits16(HDC16 hdc
, INT16 xDst
, INT16 yDst
, INT16 widthDst
,
1212 INT16 heightDst
, INT16 xSrc
, INT16 ySrc
, INT16 widthSrc
,
1213 INT16 heightSrc
, const VOID
*bits
,
1214 const BITMAPINFO
*info
, UINT16 wUsage
, DWORD dwRop
)
1216 return (INT16
)StretchDIBits32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
1217 xSrc
, ySrc
, widthSrc
, heightSrc
, bits
,
1218 info
, wUsage
, dwRop
);
1222 /***********************************************************************
1223 * StretchDIBits32 (GDI32.351)
1225 INT32 WINAPI
StretchDIBits32(HDC32 hdc
, INT32 xDst
, INT32 yDst
, INT32 widthDst
,
1226 INT32 heightDst
, INT32 xSrc
, INT32 ySrc
, INT32 widthSrc
,
1227 INT32 heightSrc
, const void *bits
,
1228 const BITMAPINFO
*info
, UINT32 wUsage
, DWORD dwRop
)
1230 HBITMAP32 hBitmap
, hOldBitmap
;
1233 hBitmap
= CreateDIBitmap32( hdc
, &info
->bmiHeader
, CBM_INIT
,
1234 bits
, info
, wUsage
);
1235 hdcMem
= CreateCompatibleDC32( hdc
);
1236 hOldBitmap
= SelectObject32( hdcMem
, hBitmap
);
1237 StretchBlt32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
1238 hdcMem
, xSrc
, ySrc
, widthSrc
, heightSrc
, dwRop
);
1239 SelectObject32( hdcMem
, hOldBitmap
);
1240 DeleteDC32( hdcMem
);
1241 DeleteObject32( hBitmap
);
1246 /***********************************************************************
1247 * SetDIBits16 (GDI.440)
1249 INT16 WINAPI
SetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
1250 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
1253 return SetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
1257 /******************************************************************************
1258 * SetDIBits32 [GDI32.312] Sets pixels in a bitmap using colors from DIB
1261 * hdc [I] Handle to device context
1262 * hbitmap [I] Handle to bitmap
1263 * startscan [I] Starting scan line
1264 * lines [I] Number of scan lines
1265 * bits [I] Array of bitmap bits
1266 * info [I] Address of structure with data
1267 * coloruse [I] Type of color indexes to use
1270 * Success: Number of scan lines copied
1273 INT32 WINAPI
SetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
1274 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
1277 DIB_SETIMAGEBITS_DESCR descr
;
1279 int height
, tmpheight
;
1282 /* Check parameters */
1284 descr
.dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1287 descr
.dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1288 if (!descr
.dc
) return 0;
1290 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
1292 GDI_HEAP_UNLOCK( hdc
);
1295 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &descr
.infoWidth
, &height
,
1296 &descr
.infoBpp
, &descr
.compression
) == -1)
1298 GDI_HEAP_UNLOCK( hbitmap
);
1299 GDI_HEAP_UNLOCK( hdc
);
1303 if (height
< 0) height
= -height
;
1304 if (!lines
|| (startscan
>= height
))
1306 GDI_HEAP_UNLOCK( hbitmap
);
1307 GDI_HEAP_UNLOCK( hdc
);
1310 if (startscan
+ lines
> height
) lines
= height
- startscan
;
1312 if (descr
.infoBpp
<= 8)
1314 descr
.colorMap
= DIB_BuildColorMap( info
, coloruse
== DIB_PAL_COLORS
?
1315 descr
.dc
->w
.hPalette
: 0,
1318 if (!descr
.colorMap
)
1320 GDI_HEAP_UNLOCK( hbitmap
);
1321 GDI_HEAP_UNLOCK( hdc
);
1329 descr
.lines
= tmpheight
>= 0 ? lines
: -lines
;
1330 descr
.depth
= bmp
->bitmap
.bmBitsPixel
;
1331 descr
.drawable
= bmp
->pixmap
;
1332 descr
.gc
= BITMAP_GC(bmp
);
1336 descr
.yDest
= height
- startscan
- lines
;
1337 descr
.width
= bmp
->bitmap
.bmWidth
;
1338 descr
.height
= lines
;
1340 EnterCriticalSection( &X11DRV_CritSection
);
1341 result
= CALL_LARGE_STACK( DIB_SetImageBits
, &descr
);
1342 LeaveCriticalSection( &X11DRV_CritSection
);
1344 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
1346 GDI_HEAP_UNLOCK( hdc
);
1347 GDI_HEAP_UNLOCK( hbitmap
);
1352 /***********************************************************************
1353 * SetDIBitsToDevice16 (GDI.443)
1355 INT16 WINAPI
SetDIBitsToDevice16(HDC16 hdc
, INT16 xDest
, INT16 yDest
, INT16 cx
,
1356 INT16 cy
, INT16 xSrc
, INT16 ySrc
, UINT16 startscan
,
1357 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
1360 return SetDIBitsToDevice32( hdc
, xDest
, yDest
, cx
, cy
, xSrc
, ySrc
,
1361 startscan
, lines
, bits
, info
, coloruse
);
1365 /***********************************************************************
1366 * SetDIBitsToDevice32 (GDI32.313)
1368 INT32 WINAPI
SetDIBitsToDevice32(HDC32 hdc
, INT32 xDest
, INT32 yDest
, DWORD cx
,
1369 DWORD cy
, INT32 xSrc
, INT32 ySrc
, UINT32 startscan
,
1370 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
1373 DIB_SETIMAGEBITS_DESCR descr
;
1375 DWORD width
, oldcy
= cy
;
1377 int height
, tmpheight
;
1379 /* Check parameters */
1381 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1384 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1387 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
,
1388 &descr
.infoBpp
, &descr
.compression
) == -1)
1391 if (height
< 0) height
= -height
;
1392 if (!lines
|| (startscan
>= height
)) return 0;
1393 if (startscan
+ lines
> height
) lines
= height
- startscan
;
1394 if (ySrc
< startscan
) ySrc
= startscan
;
1395 else if (ySrc
>= startscan
+ lines
) return 0;
1396 if (xSrc
>= width
) return 0;
1397 if (ySrc
+ cy
>= startscan
+ lines
) cy
= startscan
+ lines
- ySrc
;
1398 if (xSrc
+ cx
>= width
) cx
= width
- xSrc
;
1399 if (!cx
|| !cy
) return 0;
1401 DC_SetupGCForText( dc
); /* To have the correct colors */
1402 TSXSetFunction( display
, dc
->u
.x
.gc
, DC_XROPfunction
[dc
->w
.ROPmode
-1] );
1405 if (descr
.infoBpp
<= 8)
1407 descr
.colorMap
= DIB_BuildColorMap( info
, coloruse
== DIB_PAL_COLORS
?
1408 descr
.dc
->w
.hPalette
: 0,
1411 if (!descr
.colorMap
)
1415 descr
.lines
= tmpheight
>= 0 ? lines
: -lines
;
1416 descr
.infoWidth
= width
;
1417 descr
.depth
= dc
->w
.bitsPerPixel
;
1418 descr
.drawable
= dc
->u
.x
.drawable
;
1419 descr
.gc
= dc
->u
.x
.gc
;
1421 descr
.ySrc
= tmpheight
>= 0 ? lines
-(ySrc
-startscan
)-cy
+(oldcy
-cy
) : ySrc
- startscan
;
1422 descr
.xDest
= dc
->w
.DCOrgX
+ XLPTODP( dc
, xDest
);
1423 descr
.yDest
= dc
->w
.DCOrgY
+ YLPTODP( dc
, yDest
) + (tmpheight
>= 0 ? oldcy
-cy
: 0);
1427 EnterCriticalSection( &X11DRV_CritSection
);
1428 result
= CALL_LARGE_STACK( DIB_SetImageBits
, &descr
);
1429 LeaveCriticalSection( &X11DRV_CritSection
);
1431 HeapFree(GetProcessHeap(), 0, descr
.colorMap
);
1435 /***********************************************************************
1436 * SetDIBColorTable32 (GDI32.311)
1438 UINT32 WINAPI
SetDIBColorTable32( HDC32 hdc
, UINT32 startpos
, UINT32 entries
,
1442 PALETTEENTRY
* palEntry
;
1443 PALETTEOBJ
* palette
;
1446 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1449 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1453 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
1458 /* Transfer color info */
1460 if (dc
->w
.bitsPerPixel
<= 8) {
1461 palEntry
= palette
->logpalette
.palPalEntry
+ startpos
;
1462 if (startpos
+ entries
> (1 << dc
->w
.bitsPerPixel
)) {
1463 entries
= (1 << dc
->w
.bitsPerPixel
) - startpos
;
1465 for (end
= colors
+ entries
; colors
< end
; palEntry
++, colors
++)
1467 palEntry
->peRed
= colors
->rgbRed
;
1468 palEntry
->peGreen
= colors
->rgbGreen
;
1469 palEntry
->peBlue
= colors
->rgbBlue
;
1474 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1478 /***********************************************************************
1479 * GetDIBColorTable32 (GDI32.169)
1481 UINT32 WINAPI
GetDIBColorTable32( HDC32 hdc
, UINT32 startpos
, UINT32 entries
,
1485 PALETTEENTRY
* palEntry
;
1486 PALETTEOBJ
* palette
;
1489 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1492 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1496 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
1501 /* Transfer color info */
1503 if (dc
->w
.bitsPerPixel
<= 8) {
1504 palEntry
= palette
->logpalette
.palPalEntry
+ startpos
;
1505 if (startpos
+ entries
> (1 << dc
->w
.bitsPerPixel
)) {
1506 entries
= (1 << dc
->w
.bitsPerPixel
) - startpos
;
1508 for (end
= colors
+ entries
; colors
< end
; palEntry
++, colors
++)
1510 colors
->rgbRed
= palEntry
->peRed
;
1511 colors
->rgbGreen
= palEntry
->peGreen
;
1512 colors
->rgbBlue
= palEntry
->peBlue
;
1513 colors
->rgbReserved
= 0;
1518 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1522 /***********************************************************************
1523 * GetDIBits16 (GDI.441)
1525 INT16 WINAPI
GetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
1526 UINT16 lines
, LPSTR bits
, BITMAPINFO
* info
,
1529 return GetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
1533 /******************************************************************************
1534 * GetDIBits32 [GDI32.170] Retrieves bits of bitmap and copies to buffer
1537 * Success: Number of scan lines copied from bitmap
1540 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
1542 INT32 WINAPI
GetDIBits32(
1543 HDC32 hdc
, /* [in] Handle to device context */
1544 HBITMAP32 hbitmap
, /* [in] Handle to bitmap */
1545 UINT32 startscan
, /* [in] First scan line to set in dest bitmap */
1546 UINT32 lines
, /* [in] Number of scan lines to copy */
1547 LPSTR bits
, /* [out] Address of array for bitmap bits */
1548 BITMAPINFO
* info
, /* [out] Address of structure with bitmap data */
1549 UINT32 coloruse
) /* [in] RGB or palette index */
1553 PALETTEENTRY
* palEntry
;
1554 PALETTEOBJ
* palette
;
1558 if (!lines
) return 0;
1559 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1562 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1565 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
1567 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
1569 GDI_HEAP_UNLOCK( hbitmap
);
1573 /* Transfer color info */
1575 if (info
->bmiHeader
.biBitCount
<=8) {
1576 palEntry
= palette
->logpalette
.palPalEntry
;
1577 for (i
= 0; i
< info
->bmiHeader
.biClrUsed
; i
++, palEntry
++)
1579 if (coloruse
== DIB_RGB_COLORS
)
1581 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
1582 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
1583 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
1584 info
->bmiColors
[i
].rgbReserved
= 0;
1586 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
1593 int pad
, yend
, xend
= bmp
->bitmap
.bmWidth
;
1595 TRACE(bitmap
, "%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
1596 lines
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
1597 (int)info
->bmiHeader
.biWidth
, (int)info
->bmiHeader
.biHeight
, startscan
);
1599 /* adjust number of scanlines to copy */
1601 if( lines
> info
->bmiHeader
.biHeight
) lines
= info
->bmiHeader
.biHeight
;
1602 yend
= startscan
+ lines
;
1603 if( startscan
>= bmp
->bitmap
.bmHeight
)
1605 GDI_HEAP_UNLOCK( hbitmap
);
1606 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1609 if( yend
> bmp
->bitmap
.bmHeight
) yend
= bmp
->bitmap
.bmHeight
;
1611 /* adjust scanline width */
1613 pad
= info
->bmiHeader
.biWidth
- bmp
->bitmap
.bmWidth
;
1616 /* bitmap is wider than DIB, copy only a part */
1619 xend
= info
->bmiHeader
.biWidth
;
1622 EnterCriticalSection( &X11DRV_CritSection
);
1623 bmpImage
= (XImage
*)CALL_LARGE_STACK( BITMAP_GetXImage
, bmp
);
1625 switch( info
->bmiHeader
.biBitCount
)
1628 /* pad up to 32 bit */
1629 pad
+= (4 - (info
->bmiHeader
.biWidth
& 3)) & 3;
1630 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1632 for( x
= 0; x
< xend
; x
++ )
1633 *bbits
++ = XGetPixel( bmpImage
, x
, y
);
1638 pad
+= ((32 - (info
->bmiHeader
.biWidth
& 31)) / 8) & 3;
1639 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1642 for( x
= 0; x
< xend
; x
++ ) {
1644 *bbits
|= XGetPixel( bmpImage
, x
, y
)<<(7-(x
&7));
1654 pad
+= ((8 - (info
->bmiHeader
.biWidth
& 7)) / 2) & 3;
1655 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1658 for( x
= 0; x
< xend
; x
++ ) {
1660 *bbits
|= XGetPixel( bmpImage
, x
, y
)<<(4*(1-(x
&1)));
1671 pad
+= (4 - ((info
->bmiHeader
.biWidth
*2) & 3)) & 3;
1672 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1675 for( x
= 0; x
< xend
; x
++ ) {
1676 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
1677 *bbits
++ = pixel
& 0xff;
1678 *bbits
++ = (pixel
>> 8) & 0xff;
1684 pad
+= (4 - ((info
->bmiHeader
.biWidth
*3) & 3)) & 3;
1685 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1688 for( x
= 0; x
< xend
; x
++ ) {
1689 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
1690 *bbits
++ = (pixel
>>16) & 0xff;
1691 *bbits
++ = (pixel
>> 8) & 0xff;
1692 *bbits
++ = pixel
& 0xff;
1698 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1701 for( x
= 0; x
< xend
; x
++ ) {
1702 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
1703 *bbits
++ = (pixel
>>16) & 0xff;
1704 *bbits
++ = (pixel
>> 8) & 0xff;
1705 *bbits
++ = pixel
& 0xff;
1710 WARN(bitmap
,"Unsupported depth %d\n",
1711 info
->bmiHeader
.biBitCount
);
1715 XDestroyImage( bmpImage
);
1716 LeaveCriticalSection( &X11DRV_CritSection
);
1718 info
->bmiHeader
.biCompression
= 0;
1720 else if( info
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
) )
1722 /* fill in struct members */
1724 info
->bmiHeader
.biWidth
= bmp
->bitmap
.bmWidth
;
1725 info
->bmiHeader
.biHeight
= bmp
->bitmap
.bmHeight
;
1726 info
->bmiHeader
.biPlanes
= 1;
1727 info
->bmiHeader
.biBitCount
= bmp
->bitmap
.bmBitsPixel
;
1728 info
->bmiHeader
.biSizeImage
= bmp
->bitmap
.bmHeight
*
1729 DIB_GetDIBWidthBytes( bmp
->bitmap
.bmWidth
,
1730 bmp
->bitmap
.bmBitsPixel
);
1731 info
->bmiHeader
.biCompression
= 0;
1734 GDI_HEAP_UNLOCK( hbitmap
);
1735 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1740 /***********************************************************************
1741 * CreateDIBitmap16 (GDI.442)
1743 HBITMAP16 WINAPI
CreateDIBitmap16( HDC16 hdc
, const BITMAPINFOHEADER
* header
,
1744 DWORD init
, LPCVOID bits
, const BITMAPINFO
* data
,
1747 return CreateDIBitmap32( hdc
, header
, init
, bits
, data
, coloruse
);
1751 /***********************************************************************
1752 * CreateDIBitmap32 (GDI32.37)
1754 HBITMAP32 WINAPI
CreateDIBitmap32( HDC32 hdc
, const BITMAPINFOHEADER
*header
,
1755 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
1765 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
, &compr
) == -1) return 0;
1766 if (height
< 0) height
= -height
;
1768 /* Check if we should create a monochrome or color bitmap. */
1769 /* We create a monochrome bitmap only if it has exactly 2 */
1770 /* colors, which are either black or white, nothing else. */
1771 /* In all other cases, we create a color bitmap. */
1773 if (bpp
!= 1) fColor
= TRUE
;
1774 else if ((coloruse
!= DIB_RGB_COLORS
) ||
1775 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1778 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
1780 RGBQUAD
*rgb
= data
->bmiColors
;
1781 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1782 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
1785 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1786 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
1790 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1792 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
1793 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
1794 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
1797 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
1798 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
1804 WARN(bitmap
, "(%ld): wrong size for data\n",
1805 data
->bmiHeader
.biSize
);
1810 /* Now create the bitmap */
1812 handle
= fColor
? CreateBitmap32( width
, height
, 1, screenDepth
, NULL
) :
1813 CreateBitmap32( width
, height
, 1, 1, NULL
);
1814 if (!handle
) return 0;
1816 if (init
== CBM_INIT
)
1817 SetDIBits32( hdc
, handle
, 0, height
, bits
, data
, coloruse
);
1822 /***********************************************************************
1823 * DIB_DoProtectDIBSection
1825 static void DIB_DoProtectDIBSection( BITMAPOBJ
*bmp
, DWORD new_prot
)
1827 DIBSECTION
*dib
= bmp
->dibSection
;
1828 INT32 effHeight
= dib
->dsBm
.bmHeight
>= 0? dib
->dsBm
.bmHeight
1829 : -dib
->dsBm
.bmHeight
;
1830 INT32 totalSize
= dib
->dsBmih
.biSizeImage
? dib
->dsBmih
.biSizeImage
1831 : dib
->dsBm
.bmWidthBytes
* effHeight
;
1834 VirtualProtect(dib
->dsBm
.bmBits
, totalSize
, new_prot
, &old_prot
);
1835 TRACE(bitmap
, "Changed protection from %ld to %ld\n",
1836 old_prot
, new_prot
);
1839 /***********************************************************************
1840 * DIB_DoUpdateDIBSection
1842 static void DIB_DoUpdateDIBSection( BITMAPOBJ
*bmp
, BOOL32 toDIB
)
1844 DIBSECTION
*dib
= bmp
->dibSection
;
1845 DIB_SETIMAGEBITS_DESCR descr
;
1847 if (DIB_GetBitmapInfo( &dib
->dsBmih
, &descr
.infoWidth
, &descr
.lines
,
1848 &descr
.infoBpp
, &descr
.compression
) == -1)
1852 descr
.colorMap
= bmp
->colorMap
;
1853 descr
.nColorMap
= bmp
->nColorMap
;
1854 descr
.bits
= dib
->dsBm
.bmBits
;
1855 descr
.depth
= bmp
->bitmap
.bmBitsPixel
;
1856 descr
.drawable
= bmp
->pixmap
;
1857 descr
.gc
= BITMAP_GC(bmp
);
1862 descr
.width
= bmp
->bitmap
.bmWidth
;
1863 descr
.height
= bmp
->bitmap
.bmHeight
;
1867 TRACE(bitmap
, "Copying from Pixmap to DIB bits\n");
1868 EnterCriticalSection( &X11DRV_CritSection
);
1869 CALL_LARGE_STACK( DIB_GetImageBits
, &descr
);
1870 LeaveCriticalSection( &X11DRV_CritSection
);
1874 TRACE(bitmap
, "Copying from DIB bits to Pixmap\n");
1875 EnterCriticalSection( &X11DRV_CritSection
);
1876 CALL_LARGE_STACK( DIB_SetImageBits
, &descr
);
1877 LeaveCriticalSection( &X11DRV_CritSection
);
1881 /***********************************************************************
1884 static BOOL32
DIB_FaultHandler( LPVOID res
, LPVOID addr
)
1886 BOOL32 handled
= FALSE
;
1889 bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( (HBITMAP32
)res
, BITMAP_MAGIC
);
1890 if (!bmp
) return FALSE
;
1892 if (bmp
->dibSection
)
1893 switch (bmp
->status
)
1896 TRACE( bitmap
, "called in status DIB_GdiMod\n" );
1897 DIB_DoProtectDIBSection( bmp
, PAGE_READWRITE
);
1898 DIB_DoUpdateDIBSection( bmp
, TRUE
);
1899 DIB_DoProtectDIBSection( bmp
, PAGE_READONLY
);
1900 bmp
->status
= DIB_InSync
;
1905 TRACE( bitmap
, "called in status DIB_InSync\n" );
1906 DIB_DoProtectDIBSection( bmp
, PAGE_READWRITE
);
1907 bmp
->status
= DIB_AppMod
;
1912 FIXME( bitmap
, "called in status DIB_AppMod: "
1913 "this can't happen!\n" );
1917 FIXME( bitmap
, "called in status DIB_NoHandler: "
1918 "this can't happen!\n" );
1922 GDI_HEAP_UNLOCK( (HBITMAP32
)res
);
1926 /***********************************************************************
1927 * DIB_UpdateDIBSection
1929 void DIB_UpdateDIBSection( DC
*dc
, BOOL32 toDIB
)
1933 /* Ensure this is a Compatible DC that has a DIB section selected */
1936 if (!(dc
->w
.flags
& DC_MEMORY
)) return;
1938 bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( dc
->w
.hBitmap
, BITMAP_MAGIC
);
1941 if (!bmp
->dibSection
)
1943 GDI_HEAP_UNLOCK(dc
->w
.hBitmap
);
1950 /* Prepare for access to the DIB by GDI functions */
1952 switch (bmp
->status
)
1956 DIB_DoUpdateDIBSection( bmp
, FALSE
);
1960 TRACE( bitmap
, "fromDIB called in status DIB_GdiMod\n" );
1965 TRACE( bitmap
, "fromDIB called in status DIB_InSync\n" );
1970 TRACE( bitmap
, "fromDIB called in status DIB_AppMod\n" );
1971 DIB_DoUpdateDIBSection( bmp
, FALSE
);
1972 DIB_DoProtectDIBSection( bmp
, PAGE_READONLY
);
1973 bmp
->status
= DIB_InSync
;
1979 /* Acknowledge write access to the DIB by GDI functions */
1981 switch (bmp
->status
)
1985 DIB_DoUpdateDIBSection( bmp
, TRUE
);
1989 TRACE( bitmap
, " toDIB called in status DIB_GdiMod\n" );
1994 TRACE( bitmap
, " toDIB called in status DIB_InSync\n" );
1995 DIB_DoProtectDIBSection( bmp
, PAGE_NOACCESS
);
1996 bmp
->status
= DIB_GdiMod
;
2000 FIXME( bitmap
, " toDIB called in status DIB_AppMod: "
2001 "this can't happen!\n" );
2007 GDI_HEAP_UNLOCK(dc
->w
.hBitmap
);
2010 /***********************************************************************
2011 * CreateDIBSection16 (GDI.489)
2013 HBITMAP16 WINAPI
CreateDIBSection16 (HDC16 hdc
, BITMAPINFO
*bmi
, UINT16 usage
,
2014 LPVOID
**bits
, HANDLE16 section
,
2017 return CreateDIBSection32(hdc
, bmi
, usage
, bits
, section
, offset
);
2020 /***********************************************************************
2021 * CreateDIBSection32 (GDI32.36)
2023 HBITMAP32 WINAPI
CreateDIBSection32 (HDC32 hdc
, BITMAPINFO
*bmi
, UINT32 usage
,
2024 LPVOID
**bits
,HANDLE32 section
,
2028 BITMAPOBJ
*bmp
= NULL
;
2029 DIBSECTION
*dib
= NULL
;
2030 RGBQUAD
*colorMap
= NULL
;
2033 /* Fill BITMAP32 structure with DIB data */
2034 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
2035 INT32 effHeight
, totalSize
;
2038 TRACE(bitmap
, "format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
2039 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
2040 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
2042 if (bi
->biCompression
)
2044 FIXME(bitmap
, "Compression not implemented!\n");
2049 bm
.bmWidth
= bi
->biWidth
;
2050 bm
.bmHeight
= bi
->biHeight
;
2051 bm
.bmWidthBytes
= DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
2052 bm
.bmPlanes
= bi
->biPlanes
;
2053 bm
.bmBitsPixel
= bi
->biBitCount
;
2056 /* Get storage location for DIB bits */
2057 effHeight
= bm
.bmHeight
>= 0 ? bm
.bmHeight
: -bm
.bmHeight
;
2058 totalSize
= bi
->biSizeImage
? bi
->biSizeImage
: bm
.bmWidthBytes
* effHeight
;
2061 bm
.bmBits
= MapViewOfFile(section
, FILE_MAP_ALL_ACCESS
,
2062 0L, offset
, totalSize
);
2064 bm
.bmBits
= VirtualAlloc(NULL
, totalSize
,
2065 MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
2067 /* Allocate Memory for DIB and fill structure */
2069 dib
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(DIBSECTION
));
2074 /* FIXME: dib->dsBitfields ??? */
2075 dib
->dshSection
= section
;
2076 dib
->dsOffset
= offset
;
2079 /* Create Color Map */
2080 if (dib
&& bm
.bmBitsPixel
<= 8)
2082 HPALETTE16 hPalette
= 0;
2083 if (usage
== DIB_PAL_COLORS
)
2085 DC
*dc
= (DC
*)GDI_GetObjPtr(hdc
, DC_MAGIC
);
2087 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
2090 hPalette
= dc
->w
.hPalette
;
2091 GDI_HEAP_UNLOCK(hdc
);
2094 colorMap
= DIB_BuildColorMap(bmi
, hPalette
, &nColorMap
);
2097 /* Create Device Dependent Bitmap and add DIB/ColorMap info */
2100 res
= CreateDIBitmap32(hdc
, bi
, 0, NULL
, bmi
, usage
);
2101 bmp
= (BITMAPOBJ
*) GDI_GetObjPtr(res
, BITMAP_MAGIC
);
2104 bmp
->dibSection
= dib
;
2105 bmp
->colorMap
= colorMap
;
2106 bmp
->nColorMap
= nColorMap
;
2107 bmp
->status
= DIB_NoHandler
;
2111 /* Clean up in case of errors */
2112 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
|| (bm
.bmBitsPixel
<= 8 && !colorMap
))
2116 UnmapViewOfFile(bm
.bmBits
), bm
.bmBits
= NULL
;
2118 VirtualFree(bm
.bmBits
, MEM_RELEASE
, 0L), bm
.bmBits
= NULL
;
2120 if (colorMap
) HeapFree(GetProcessHeap(), 0, colorMap
), colorMap
= NULL
;
2121 if (dib
) HeapFree(GetProcessHeap(), 0, dib
), dib
= NULL
;
2122 if (res
) DeleteObject32(res
), res
= 0;
2125 /* Install fault handler, if possible */
2127 if (VIRTUAL_SetFaultHandler(bm
.bmBits
, DIB_FaultHandler
, (LPVOID
)res
))
2129 DIB_DoProtectDIBSection( bmp
, PAGE_READONLY
);
2130 bmp
->status
= DIB_InSync
;
2133 /* Return BITMAP handle and storage location */
2134 if (res
) GDI_HEAP_UNLOCK(res
);
2135 if (bm
.bmBits
&& bits
) *bits
= bm
.bmBits
;