2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
10 #include <X11/Xutil.h>
19 extern void CLIPPING_UpdateGCRegion(DC
* );
21 static int bitmapDepthTable
[] = { 8, 1, 32, 16, 24, 15, 4, 0 };
22 static int ximageDepthTable
[] = { 0, 0, 0, 0, 0, 0, 0 };
25 /* This structure holds the arguments for DIB_SetImageBits() */
34 const BITMAPINFO
*info
;
44 } DIB_SETIMAGEBITS_DESCR
;
47 /***********************************************************************
55 for( i
= 0; bitmapDepthTable
[i
]; i
++ )
57 testimage
= XCreateImage(display
, DefaultVisualOfScreen(screen
),
58 bitmapDepthTable
[i
], ZPixmap
, 0, NULL
, 1, 1, 32, 20 );
59 if( testimage
) ximageDepthTable
[i
] = testimage
->bits_per_pixel
;
61 XDestroyImage(testimage
);
66 /***********************************************************************
67 * DIB_GetXImageWidthBytes
69 * Return the width of an X image in bytes
71 int DIB_GetXImageWidthBytes( int width
, int depth
)
75 if (!ximageDepthTable
[0]) {
78 for( i
= 0; bitmapDepthTable
[i
] ; i
++ )
79 if( bitmapDepthTable
[i
] == depth
)
80 return (4 * ((width
* ximageDepthTable
[i
] + 31)/32));
81 fprintf(stderr
, "DIB: unsupported depth %d.\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 fprintf(stderr
, "DIB: unsupported depth %d.\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
)
150 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
152 *width
= header
->biWidth
;
153 *height
= header
->biHeight
;
154 *bpp
= header
->biBitCount
;
157 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
159 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
160 *width
= core
->bcWidth
;
161 *height
= core
->bcHeight
;
162 *bpp
= core
->bcBitCount
;
165 fprintf( stderr
, "DIB_GetBitmapInfo: wrong size (%ld) for header\n",
171 /***********************************************************************
174 * Build the color map from the bitmap palette. Should not be called
175 * for a 24-bit deep bitmap.
177 static int *DIB_BuildColorMap( DC
*dc
, WORD coloruse
, WORD depth
,
178 const BITMAPINFO
*info
)
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
;
196 if (!(colorMapping
= (int *)HeapAlloc(GetProcessHeap(), 0,
197 colors
* sizeof(int) ))) return NULL
;
199 if (coloruse
== DIB_RGB_COLORS
)
203 RGBQUAD
* rgb
= (RGBQUAD
*)colorPtr
;
205 if (depth
== 1) /* Monochrome */
206 for (i
= 0; i
< colors
; i
++, rgb
++)
207 colorMapping
[i
] = (rgb
->rgbRed
+ rgb
->rgbGreen
+
208 rgb
->rgbBlue
> 255*3/2);
210 for (i
= 0; i
< colors
; i
++, rgb
++)
211 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbRed
,
217 RGBTRIPLE
* rgb
= (RGBTRIPLE
*)colorPtr
;
219 if (depth
== 1) /* Monochrome */
220 for (i
= 0; i
< colors
; i
++, rgb
++)
221 colorMapping
[i
] = (rgb
->rgbtRed
+ rgb
->rgbtGreen
+
222 rgb
->rgbtBlue
> 255*3/2);
224 for (i
= 0; i
< colors
; i
++, rgb
++)
225 colorMapping
[i
] = COLOR_ToPhysical( dc
, RGB(rgb
->rgbtRed
,
230 else /* DIB_PAL_COLORS */
232 for (i
= 0; i
< colors
; i
++, colorPtr
++)
233 colorMapping
[i
] = COLOR_ToPhysical( dc
, PALETTEINDEX(*colorPtr
) );
238 /***********************************************************************
239 * DIB_SetImageBits_1_Line
241 * Handles a single line of 1 bit data.
243 static void DIB_SetImageBits_1_Line(DWORD dstwidth
, int *colors
,
244 XImage
*bmpImage
, int h
, const BYTE
*bits
)
249 for (i
= dstwidth
/8, x
= 0; (i
> 0); i
--)
252 XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] );
253 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 6) & 1] );
254 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 5) & 1] );
255 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 4) & 1] );
256 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 3) & 1] );
257 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 2) & 1] );
258 XPutPixel( bmpImage
, x
++, h
, colors
[(pix
>> 1) & 1] );
259 XPutPixel( bmpImage
, x
++, h
, colors
[pix
& 1] );
264 case 7: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
265 case 6: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
266 case 5: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
267 case 4: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
268 case 3: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
269 case 2: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] ); pix
<<= 1;
270 case 1: XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 7] );
274 /***********************************************************************
277 * SetDIBits for a 1-bit deep DIB.
279 static void DIB_SetImageBits_1( int lines
, const BYTE
*srcbits
,
280 DWORD srcwidth
, DWORD dstwidth
,
281 int *colors
, XImage
*bmpImage
)
286 DWORD linebytes
= ((srcwidth
+ 31) & ~31) / 8;
289 for (h
= lines
-1; h
>=0; h
--) {
290 DIB_SetImageBits_1_Line(dstwidth
, colors
, bmpImage
, h
, srcbits
);
291 srcbits
+= linebytes
;
295 for (h
= 0; h
< lines
; h
++) {
296 DIB_SetImageBits_1_Line(dstwidth
, colors
, bmpImage
, h
, srcbits
);
297 srcbits
+= linebytes
;
303 /***********************************************************************
306 * SetDIBits for a 4-bit deep DIB.
308 static void DIB_SetImageBits_4( int lines
, const BYTE
*srcbits
,
309 DWORD srcwidth
, DWORD dstwidth
,
310 int *colors
, XImage
*bmpImage
)
314 const BYTE
*bits
= srcbits
;
317 DWORD linebytes
= ((srcwidth
+7)&~7)/2;
320 for (h
= lines
-1; h
>= 0; h
--) {
321 for (i
= dstwidth
/2, x
= 0; i
> 0; i
--) {
323 XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 4] );
324 XPutPixel( bmpImage
, x
++, h
, colors
[pix
& 0x0f] );
326 if (dstwidth
& 1) XPutPixel( bmpImage
, x
, h
, colors
[*bits
>> 4] );
327 srcbits
+= linebytes
;
332 for (h
= 0; h
< lines
; h
++) {
333 for (i
= dstwidth
/2, x
= 0; i
> 0; i
--) {
335 XPutPixel( bmpImage
, x
++, h
, colors
[pix
>> 4] );
336 XPutPixel( bmpImage
, x
++, h
, colors
[pix
& 0x0f] );
338 if (dstwidth
& 1) XPutPixel( bmpImage
, x
, h
, colors
[*bits
>> 4] );
339 srcbits
+= linebytes
;
345 #define check_xy(x,y) \
352 /***********************************************************************
353 * DIB_SetImageBits_RLE4
355 * SetDIBits for a 4-bit deep compressed DIB.
357 static void DIB_SetImageBits_RLE4( int lines
, const BYTE
*bits
, DWORD width
,
358 DWORD dstwidth
, int *colors
, XImage
*bmpImage
)
360 int x
= 0, c
, length
;
361 const BYTE
*begin
= bits
;
364 while ((int)lines
>= 0)
367 if (length
) { /* encoded */
370 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
374 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
386 case 1: /* eopicture */
394 default: /* absolute */
397 XPutPixel(bmpImage
, x
++, lines
, colors
[c
>> 4]);
401 XPutPixel(bmpImage
, x
++, lines
, colors
[c
& 0xf]);
405 if ((bits
- begin
) & 1)
412 /***********************************************************************
415 * SetDIBits for an 8-bit deep DIB.
417 static void DIB_SetImageBits_8( int lines
, const BYTE
*srcbits
,
418 DWORD srcwidth
, DWORD dstwidth
,
419 int *colors
, XImage
*bmpImage
)
423 const BYTE
*bits
= srcbits
;
425 /* align to 32 bit */
426 DWORD linebytes
= (srcwidth
+ 3) & ~3;
429 for (h
= lines
- 1; h
>= 0; h
--) {
430 for (x
= 0; x
< dstwidth
; x
++, bits
++) {
431 XPutPixel( bmpImage
, x
, h
, colors
[*bits
] );
433 bits
= (srcbits
+= linebytes
);
437 for (h
= 0; h
< lines
; h
++) {
438 for (x
= 0; x
< dstwidth
; x
++, bits
++) {
439 XPutPixel( bmpImage
, x
, h
, colors
[*bits
] );
441 bits
= (srcbits
+= linebytes
);
446 /***********************************************************************
447 * DIB_SetImageBits_RLE8
449 * SetDIBits for an 8-bit deep compressed DIB.
451 * This function rewritten 941113 by James Youngman. WINE blew out when I
452 * first ran it because my desktop wallpaper is a (large) RLE8 bitmap.
454 * This was because the algorithm assumed that all RLE8 bitmaps end with the
455 * 'End of bitmap' escape code. This code is very much laxer in what it
456 * allows to end the expansion. Possibly too lax. See the note by
457 * case RleDelta. BTW, MS's documentation implies that a correct RLE8
458 * bitmap should end with RleEnd, but on the other hand, software exists
459 * that produces ones that don't and Windows 3.1 doesn't complain a bit
462 * (No) apologies for my English spelling. [Emacs users: c-indent-level=4].
463 * James A. Youngman <mbcstjy@afs.man.ac.uk>
467 enum Rle8_EscapeCodes
470 * Apologies for polluting your file's namespace...
472 RleEol
= 0, /* End of line */
473 RleEnd
= 1, /* End of bitmap */
474 RleDelta
= 2 /* Delta */
477 static void DIB_SetImageBits_RLE8( int lines
, const BYTE
*bits
, DWORD width
,
478 DWORD dstwidth
, int *colors
, XImage
*bmpImage
)
480 int x
; /* X-positon on each line. Increases. */
481 int line
; /* Line #. Starts at lines-1, decreases */
482 const BYTE
*pIn
= bits
; /* Pointer to current position in bits */
483 BYTE length
; /* The length pf a run */
484 BYTE color_index
; /* index into colors[] as read from bits */
485 BYTE escape_code
; /* See enum Rle8_EscapeCodes.*/
486 WORD color
; /* value of colour[color_index] */
488 if (lines
== 0) /* Let's hope this doesn't happen. */
492 * Note that the bitmap data is stored by Windows starting at the
493 * bottom line of the bitmap and going upwards. Within each line,
494 * the data is stored left-to-right. That's the reason why line
495 * goes from lines-1 to 0. [JAY]
505 * If the length byte is not zero (which is the escape value),
506 * We have a run of length pixels all the same colour. The colour
507 * index is stored next.
509 * If the length byte is zero, we need to read the next byte to
510 * know what to do. [JAY]
515 * [Run-Length] Encoded mode
517 color_index
= (*pIn
++); /* Get the colour index. */
518 color
= colors
[color_index
];
521 XPutPixel(bmpImage
, x
++, line
, color
);
526 * Escape codes (may be an absolute sequence though)
528 escape_code
= (*pIn
++);
531 case RleEol
: /* =0, end of line */
538 case RleEnd
: /* =1, end of bitmap */
541 * Not all RLE8 bitmaps end with this
542 * code. For example, Paint Shop Pro
543 * produces some that don't. That's (I think)
544 * what caused the previous implementation to
547 line
=-1; /* Cause exit from do loop. */
551 case RleDelta
: /* =2, a delta */
554 * Note that deltaing to line 0
555 * will cause an exit from the loop,
556 * which may not be what is intended.
557 * The fact that there is a delta in the bits
558 * almost certainly implies that there is data
559 * to follow. You may feel that we should
560 * jump to the top of the loop to avoid exiting
563 * TODO: Decide what to do here in that case. [JAY]
569 dprintf_bitmap(stddeb
,
570 "DIB_SetImageBits_RLE8(): "
571 "Delta to last line of bitmap "
572 "(wrongly?) causes loop exit\n");
577 default: /* >2, switch to absolute mode */
582 length
= escape_code
;
585 color_index
= (*pIn
++);
586 XPutPixel(bmpImage
, x
++, line
,
587 colors
[color_index
]);
591 * If you think for a moment you'll realise that the
592 * only time we could ever possibly read an odd
593 * number of bytes is when there is a 0x00 (escape),
594 * a value >0x02 (absolute mode) and then an odd-
595 * length run. Therefore this is the only place we
596 * need to worry about it. Everywhere else the
597 * bytes are always read in pairs. [JAY]
600 pIn
++; /* Throw away the pad byte. */
603 } /* switch (escape_code) : Escape sequence */
604 } /* process either an encoded sequence or an escape sequence */
606 /* We expect to come here more than once per line. */
607 } while (line
>= 0); /* Do this until the bitmap is filled */
610 * Everybody comes here at the end.
611 * Check how we exited the loop and print a message if it's a bit odd.
614 if ( (*(pIn
-2) != 0/*escape*/) || (*(pIn
-1)!= RleEnd
) )
616 dprintf_bitmap(stddeb
, "DIB_SetImageBits_RLE8(): End-of-bitmap "
617 "without (strictly) proper escape code. Last two "
618 "bytes were: %02X %02X.\n",
625 /***********************************************************************
626 * DIB_SetImageBits_16
628 * SetDIBits for a 16-bit deep DIB.
630 static void DIB_SetImageBits_16( int lines
, const BYTE
*srcbits
,
631 DWORD srcwidth
, DWORD dstwidth
,
632 DC
*dc
, XImage
*bmpImage
)
640 /* align to 32 bit */
641 DWORD linebytes
= (srcwidth
* 2 + 3) & ~3;
643 ptr
= (LPWORD
) srcbits
;
645 for (h
= lines
- 1; h
>= 0; h
--) {
646 for (x
= 0; x
< dstwidth
; x
++, ptr
++) {
648 r
= (BYTE
) ((val
& 0x7c00) >> 7);
649 g
= (BYTE
) ((val
& 0x03e0) >> 2);
650 b
= (BYTE
) ((val
& 0x001f) << 3);
651 XPutPixel( bmpImage
, x
, h
,
652 COLOR_ToPhysical(dc
, RGB(r
,g
,b
)) );
654 ptr
= (LPWORD
) (srcbits
+= linebytes
);
658 for (h
= 0; h
< lines
; h
++) {
659 for (x
= 0; x
< dstwidth
; x
++, ptr
++) {
661 r
= (BYTE
) ((val
& 0x7c00) >> 7);
662 g
= (BYTE
) ((val
& 0x03e0) >> 2);
663 b
= (BYTE
) ((val
& 0x001f) << 3);
664 XPutPixel( bmpImage
, x
, h
,
665 COLOR_ToPhysical(dc
, RGB(r
,g
,b
)) );
667 ptr
= (LPWORD
) (srcbits
+= linebytes
);
673 /***********************************************************************
674 * DIB_SetImageBits_24
676 * SetDIBits for a 24-bit deep DIB.
678 static void DIB_SetImageBits_24( int lines
, const BYTE
*srcbits
,
679 DWORD srcwidth
, DWORD dstwidth
,
680 DC
*dc
, XImage
*bmpImage
)
683 const BYTE
*bits
= srcbits
;
686 /* align to 32 bit */
687 DWORD linebytes
= (srcwidth
* 3 + 3) & ~3;
689 /* "bits" order is reversed for some reason */
692 for (h
= lines
- 1; h
>= 0; h
--) {
693 for (x
= 0; x
< dstwidth
; x
++, bits
+= 3) {
694 XPutPixel( bmpImage
, x
, h
,
695 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])));
697 bits
= (srcbits
+= linebytes
);
701 for (h
= 0; h
< lines
; h
++) {
702 for (x
= 0; x
< dstwidth
; x
++, bits
+= 3) {
703 XPutPixel( bmpImage
, x
, h
,
704 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])));
706 bits
= (srcbits
+= linebytes
);
712 /***********************************************************************
713 * DIB_SetImageBits_32
715 * SetDIBits for a 32-bit deep DIB.
717 static void DIB_SetImageBits_32( int lines
, const BYTE
*srcbits
,
718 DWORD srcwidth
, DWORD dstwidth
,
719 DC
*dc
, XImage
*bmpImage
)
722 const BYTE
*bits
= srcbits
;
725 DWORD linebytes
= (srcwidth
* 4);
728 for (h
= lines
- 1; h
>= 0; h
--) {
729 for (x
= 0; x
< dstwidth
; x
++, bits
+= 4) {
730 XPutPixel( bmpImage
, x
, h
,
731 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])));
733 bits
= (srcbits
+= linebytes
);
737 for (h
= 0; h
< lines
; h
++) {
738 for (x
= 0; x
< dstwidth
; x
++, bits
+= 4) {
739 XPutPixel( bmpImage
, x
, h
,
740 COLOR_ToPhysical(dc
, RGB(bits
[2],bits
[1],bits
[0])));
742 bits
= (srcbits
+= linebytes
);
748 /***********************************************************************
751 * Transfer the bits to an X image.
752 * Helper function for SetDIBits() and SetDIBitsToDevice().
754 static int DIB_SetImageBits( const DIB_SETIMAGEBITS_DESCR
*descr
)
758 DWORD compression
= 0;
761 if (descr
->info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
762 compression
= descr
->info
->bmiHeader
.biCompression
;
764 /* Build the color mapping table */
766 if (descr
->infoBpp
> 8) colorMapping
= NULL
;
767 else if (!(colorMapping
= DIB_BuildColorMap( descr
->dc
, descr
->coloruse
,
768 descr
->depth
, descr
->info
)))
771 if( descr
->dc
->w
.flags
& DC_DIRTY
) CLIPPING_UpdateGCRegion(descr
->dc
);
773 /* Transfer the pixels */
774 lines
= descr
->lines
;
775 if (lines
< 0) lines
= -lines
;
776 XCREATEIMAGE(bmpImage
, descr
->infoWidth
, lines
, descr
->depth
);
778 switch(descr
->infoBpp
)
781 DIB_SetImageBits_1( descr
->lines
, descr
->bits
, descr
->infoWidth
,
782 descr
->width
, colorMapping
, bmpImage
);
785 if (compression
) DIB_SetImageBits_RLE4( descr
->lines
, descr
->bits
,
786 descr
->infoWidth
, descr
->width
,
787 colorMapping
, bmpImage
);
788 else DIB_SetImageBits_4( descr
->lines
, descr
->bits
, descr
->infoWidth
,
789 descr
->width
, colorMapping
, bmpImage
);
792 if (compression
) DIB_SetImageBits_RLE8( descr
->lines
, descr
->bits
,
793 descr
->infoWidth
, descr
->width
,
794 colorMapping
, bmpImage
);
795 else DIB_SetImageBits_8( descr
->lines
, descr
->bits
, descr
->infoWidth
,
796 descr
->width
, colorMapping
, bmpImage
);
800 DIB_SetImageBits_16( descr
->lines
, descr
->bits
, descr
->infoWidth
,
801 descr
->width
, descr
->dc
, bmpImage
);
804 DIB_SetImageBits_24( descr
->lines
, descr
->bits
, descr
->infoWidth
,
805 descr
->width
, descr
->dc
, bmpImage
);
808 DIB_SetImageBits_32( descr
->lines
, descr
->bits
, descr
->infoWidth
,
809 descr
->width
, descr
->dc
, bmpImage
);
812 fprintf( stderr
, "Invalid depth %d for SetDIBits!\n", descr
->infoBpp
);
815 if (colorMapping
) HeapFree( GetProcessHeap(), 0, colorMapping
);
816 XPutImage( display
, descr
->drawable
, descr
->gc
, bmpImage
,
817 descr
->xSrc
, descr
->ySrc
, descr
->xDest
, descr
->yDest
,
818 descr
->width
, descr
->height
);
819 XDestroyImage( bmpImage
);
824 /***********************************************************************
825 * StretchDIBits16 (GDI.439)
827 INT16 WINAPI
StretchDIBits16(HDC16 hdc
, INT16 xDst
, INT16 yDst
, INT16 widthDst
,
828 INT16 heightDst
, INT16 xSrc
, INT16 ySrc
, INT16 widthSrc
,
829 INT16 heightSrc
, const VOID
*bits
,
830 const BITMAPINFO
*info
, UINT16 wUsage
, DWORD dwRop
)
832 return (INT16
)StretchDIBits32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
833 xSrc
, ySrc
, widthSrc
, heightSrc
, bits
,
834 info
, wUsage
, dwRop
);
838 /***********************************************************************
839 * StretchDIBits32 (GDI32.351)
841 INT32 WINAPI
StretchDIBits32(HDC32 hdc
, INT32 xDst
, INT32 yDst
, INT32 widthDst
,
842 INT32 heightDst
, INT32 xSrc
, INT32 ySrc
, INT32 widthSrc
,
843 INT32 heightSrc
, const void *bits
,
844 const BITMAPINFO
*info
, UINT32 wUsage
, DWORD dwRop
)
846 HBITMAP32 hBitmap
, hOldBitmap
;
849 hBitmap
= CreateDIBitmap32( hdc
, &info
->bmiHeader
, CBM_INIT
,
850 bits
, info
, wUsage
);
851 hdcMem
= CreateCompatibleDC32( hdc
);
852 hOldBitmap
= SelectObject32( hdcMem
, hBitmap
);
853 StretchBlt32( hdc
, xDst
, yDst
, widthDst
, heightDst
,
854 hdcMem
, xSrc
, ySrc
, widthSrc
, heightSrc
, dwRop
);
855 SelectObject32( hdcMem
, hOldBitmap
);
856 DeleteDC32( hdcMem
);
857 DeleteObject32( hBitmap
);
862 /***********************************************************************
863 * SetDIBits16 (GDI.440)
865 INT16 WINAPI
SetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
866 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
869 return SetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
873 /***********************************************************************
874 * SetDIBits32 (GDI32.312)
876 INT32 WINAPI
SetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
877 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
880 DIB_SETIMAGEBITS_DESCR descr
;
882 int height
, tmpheight
;
885 /* Check parameters */
887 descr
.dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
890 descr
.dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
891 if (!descr
.dc
) return 0;
893 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
895 GDI_HEAP_UNLOCK( hdc
);
898 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &descr
.infoWidth
, &height
,
899 &descr
.infoBpp
) == -1)
901 GDI_HEAP_UNLOCK( hbitmap
);
902 GDI_HEAP_UNLOCK( hdc
);
906 if (height
< 0) height
= -height
;
907 if (!lines
|| (startscan
>= height
))
909 GDI_HEAP_UNLOCK( hbitmap
);
910 GDI_HEAP_UNLOCK( hdc
);
913 if (startscan
+ lines
> height
) lines
= height
- startscan
;
916 descr
.lines
= tmpheight
>= 0 ? lines
: -lines
;
917 descr
.depth
= bmp
->bitmap
.bmBitsPixel
;
919 descr
.coloruse
= coloruse
;
920 descr
.drawable
= bmp
->pixmap
;
921 descr
.gc
= BITMAP_GC(bmp
);
925 descr
.yDest
= height
- startscan
- lines
;
926 descr
.width
= bmp
->bitmap
.bmWidth
;
927 descr
.height
= lines
;
929 result
= CALL_LARGE_STACK( DIB_SetImageBits
, &descr
);
930 GDI_HEAP_UNLOCK( hdc
);
931 GDI_HEAP_UNLOCK( hbitmap
);
936 /***********************************************************************
937 * SetDIBitsToDevice16 (GDI.443)
939 INT16 WINAPI
SetDIBitsToDevice16(HDC16 hdc
, INT16 xDest
, INT16 yDest
, INT16 cx
,
940 INT16 cy
, INT16 xSrc
, INT16 ySrc
, UINT16 startscan
,
941 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
944 return SetDIBitsToDevice32( hdc
, xDest
, yDest
, cx
, cy
, xSrc
, ySrc
,
945 startscan
, lines
, bits
, info
, coloruse
);
949 /***********************************************************************
950 * SetDIBitsToDevice32 (GDI32.313)
952 INT32 WINAPI
SetDIBitsToDevice32(HDC32 hdc
, INT32 xDest
, INT32 yDest
, DWORD cx
,
953 DWORD cy
, INT32 xSrc
, INT32 ySrc
, UINT32 startscan
,
954 UINT32 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
957 DIB_SETIMAGEBITS_DESCR descr
;
960 int height
, tmpheight
;
962 /* Check parameters */
964 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
967 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
970 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
,
971 &height
, &descr
.infoBpp
) == -1)
974 if (height
< 0) height
= -height
;
975 if (!lines
|| (startscan
>= height
)) return 0;
976 if (startscan
+ lines
> height
) lines
= height
- startscan
;
977 if (ySrc
< startscan
) ySrc
= startscan
;
978 else if (ySrc
>= startscan
+ lines
) return 0;
979 if (xSrc
>= width
) return 0;
980 if (ySrc
+ cy
>= startscan
+ lines
) cy
= startscan
+ lines
- ySrc
;
981 if (xSrc
+ cx
>= width
) cx
= width
- xSrc
;
982 if (!cx
|| !cy
) return 0;
984 DC_SetupGCForText( dc
); /* To have the correct colors */
985 XSetFunction( display
, dc
->u
.x
.gc
, DC_XROPfunction
[dc
->w
.ROPmode
-1] );
989 descr
.lines
= tmpheight
>= 0 ? lines
: -lines
;
990 descr
.infoWidth
= width
;
991 descr
.depth
= dc
->w
.bitsPerPixel
;
993 descr
.coloruse
= coloruse
;
994 descr
.drawable
= dc
->u
.x
.drawable
;
995 descr
.gc
= dc
->u
.x
.gc
;
997 descr
.ySrc
= ySrc
- startscan
;
998 descr
.xDest
= dc
->w
.DCOrgX
+ XLPTODP( dc
, xDest
);
999 descr
.yDest
= dc
->w
.DCOrgY
+ YLPTODP( dc
, yDest
);
1003 return CALL_LARGE_STACK( DIB_SetImageBits
, &descr
);
1006 /***********************************************************************
1007 * SetDIBColorTable32 (GDI32.311)
1009 UINT32 WINAPI
SetDIBColorTable32( HDC32 hdc
, UINT32 startpos
, UINT32 entries
,
1013 PALETTEENTRY
* palEntry
;
1014 PALETTEOBJ
* palette
;
1017 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1020 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1024 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
1029 /* Transfer color info */
1031 if (dc
->w
.bitsPerPixel
<= 8) {
1032 palEntry
= palette
->logpalette
.palPalEntry
+ startpos
;
1033 if (startpos
+ entries
> (1 << dc
->w
.bitsPerPixel
)) {
1034 entries
= (1 << dc
->w
.bitsPerPixel
) - startpos
;
1036 for (end
= colors
+ entries
; colors
< end
; palEntry
++, colors
++)
1038 palEntry
->peRed
= colors
->rgbRed
;
1039 palEntry
->peGreen
= colors
->rgbGreen
;
1040 palEntry
->peBlue
= colors
->rgbBlue
;
1045 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1049 /***********************************************************************
1050 * GetDIBColorTable32 (GDI32.169)
1052 UINT32 WINAPI
GetDIBColorTable32( HDC32 hdc
, UINT32 startpos
, UINT32 entries
,
1056 PALETTEENTRY
* palEntry
;
1057 PALETTEOBJ
* palette
;
1060 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1063 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1067 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
1072 /* Transfer color info */
1074 if (dc
->w
.bitsPerPixel
<= 8) {
1075 palEntry
= palette
->logpalette
.palPalEntry
+ startpos
;
1076 if (startpos
+ entries
> (1 << dc
->w
.bitsPerPixel
)) {
1077 entries
= (1 << dc
->w
.bitsPerPixel
) - startpos
;
1079 for (end
= colors
+ entries
; colors
< end
; palEntry
++, colors
++)
1081 colors
->rgbRed
= palEntry
->peRed
;
1082 colors
->rgbGreen
= palEntry
->peGreen
;
1083 colors
->rgbBlue
= palEntry
->peBlue
;
1084 colors
->rgbReserved
= 0;
1089 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1093 /***********************************************************************
1094 * GetDIBits16 (GDI.441)
1096 INT16 WINAPI
GetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
1097 UINT16 lines
, LPSTR bits
, BITMAPINFO
* info
,
1100 return GetDIBits32( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
1104 /***********************************************************************
1105 * GetDIBits32 (GDI32.170)
1107 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
1109 INT32 WINAPI
GetDIBits32( HDC32 hdc
, HBITMAP32 hbitmap
, UINT32 startscan
,
1110 UINT32 lines
, LPSTR bits
, BITMAPINFO
* info
,
1115 PALETTEENTRY
* palEntry
;
1116 PALETTEOBJ
* palette
;
1120 if (!lines
) return 0;
1121 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
1124 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
1127 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
1129 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
1131 GDI_HEAP_UNLOCK( hbitmap
);
1135 /* Transfer color info */
1137 if (info
->bmiHeader
.biBitCount
<=8) {
1138 palEntry
= palette
->logpalette
.palPalEntry
;
1139 for (i
= 0; i
< info
->bmiHeader
.biClrUsed
; i
++, palEntry
++)
1141 if (coloruse
== DIB_RGB_COLORS
)
1143 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
1144 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
1145 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
1146 info
->bmiColors
[i
].rgbReserved
= 0;
1148 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
1155 int pad
, yend
, xend
= bmp
->bitmap
.bmWidth
;
1157 dprintf_bitmap(stddeb
, "GetDIBits: %u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
1158 lines
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
1159 (int)info
->bmiHeader
.biWidth
, (int)info
->bmiHeader
.biHeight
, startscan
);
1161 /* adjust number of scanlines to copy */
1163 if( lines
> info
->bmiHeader
.biHeight
) lines
= info
->bmiHeader
.biHeight
;
1164 yend
= startscan
+ lines
;
1165 if( startscan
>= bmp
->bitmap
.bmHeight
)
1167 GDI_HEAP_UNLOCK( hbitmap
);
1168 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1171 if( yend
> bmp
->bitmap
.bmHeight
) yend
= bmp
->bitmap
.bmHeight
;
1173 /* adjust scanline width */
1175 pad
= info
->bmiHeader
.biWidth
- bmp
->bitmap
.bmWidth
;
1178 /* bitmap is wider than DIB, copy only a part */
1181 xend
= info
->bmiHeader
.biWidth
;
1184 bmpImage
= (XImage
*)CALL_LARGE_STACK( BITMAP_GetXImage
, bmp
);
1186 switch( info
->bmiHeader
.biBitCount
)
1189 /* pad up to 32 bit */
1190 pad
+= (4 - (info
->bmiHeader
.biWidth
& 3)) & 3;
1191 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1193 for( x
= 0; x
< xend
; x
++ )
1194 *bbits
++ = XGetPixel( bmpImage
, x
, y
);
1199 pad
+= ((32 - (info
->bmiHeader
.biWidth
& 31)) / 8) & 3;
1200 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1203 for( x
= 0; x
< xend
; x
++ ) {
1205 *bbits
|= XGetPixel( bmpImage
, x
, y
)<<(7-(x
&7));
1215 pad
+= ((8 - (info
->bmiHeader
.biWidth
& 7)) / 2) & 3;
1216 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1219 for( x
= 0; x
< xend
; x
++ ) {
1221 *bbits
|= XGetPixel( bmpImage
, x
, y
)<<(4*(1-(x
&1)));
1232 pad
+= (4 - ((info
->bmiHeader
.biWidth
*2) & 3)) & 3;
1233 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1236 for( x
= 0; x
< xend
; x
++ ) {
1237 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
1238 *bbits
++ = pixel
& 0xff;
1239 *bbits
++ = (pixel
>> 8) & 0xff;
1245 pad
+= (4 - ((info
->bmiHeader
.biWidth
*3) & 3)) & 3;
1246 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1249 for( x
= 0; x
< xend
; x
++ ) {
1250 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
1251 *bbits
++ = (pixel
>>16) & 0xff;
1252 *bbits
++ = (pixel
>> 8) & 0xff;
1253 *bbits
++ = pixel
& 0xff;
1259 for( y
= yend
- 1; (int)y
>= (int)startscan
; y
-- )
1262 for( x
= 0; x
< xend
; x
++ ) {
1263 unsigned long pixel
=XGetPixel( bmpImage
, x
, y
);
1264 *bbits
++ = (pixel
>>16) & 0xff;
1265 *bbits
++ = (pixel
>> 8) & 0xff;
1266 *bbits
++ = pixel
& 0xff;
1271 fprintf(stderr
,"GetDIBits*: unsupported depth %d\n",
1272 info
->bmiHeader
.biBitCount
1277 XDestroyImage( bmpImage
);
1279 info
->bmiHeader
.biCompression
= 0;
1281 else if( info
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
) )
1283 /* fill in struct members */
1285 info
->bmiHeader
.biWidth
= bmp
->bitmap
.bmWidth
;
1286 info
->bmiHeader
.biHeight
= bmp
->bitmap
.bmHeight
;
1287 info
->bmiHeader
.biPlanes
= 1;
1288 info
->bmiHeader
.biBitCount
= bmp
->bitmap
.bmBitsPixel
;
1289 info
->bmiHeader
.biSizeImage
= bmp
->bitmap
.bmHeight
*
1290 DIB_GetDIBWidthBytes( bmp
->bitmap
.bmWidth
,
1291 bmp
->bitmap
.bmBitsPixel
);
1292 info
->bmiHeader
.biCompression
= 0;
1295 GDI_HEAP_UNLOCK( hbitmap
);
1296 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
1301 /***********************************************************************
1302 * CreateDIBitmap16 (GDI.442)
1304 HBITMAP16 WINAPI
CreateDIBitmap16( HDC16 hdc
, const BITMAPINFOHEADER
* header
,
1305 DWORD init
, LPCVOID bits
, const BITMAPINFO
* data
,
1308 return CreateDIBitmap32( hdc
, header
, init
, bits
, data
, coloruse
);
1312 /***********************************************************************
1313 * CreateDIBitmap32 (GDI32.37)
1315 HBITMAP32 WINAPI
CreateDIBitmap32( HDC32 hdc
, const BITMAPINFOHEADER
*header
,
1316 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
1325 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
) == -1) return 0;
1326 if (height
< 0) height
= -height
;
1328 /* Check if we should create a monochrome or color bitmap. */
1329 /* We create a monochrome bitmap only if it has exactly 2 */
1330 /* colors, which are either black or white, nothing else. */
1331 /* In all other cases, we create a color bitmap. */
1333 if (bpp
!= 1) fColor
= TRUE
;
1334 else if ((coloruse
!= DIB_RGB_COLORS
) ||
1335 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1338 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
1340 RGBQUAD
*rgb
= data
->bmiColors
;
1341 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1342 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
1345 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1346 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
1350 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1352 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
1353 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
1354 if ((col
== RGB(0,0,0)) || (col
== RGB(0xff,0xff,0xff)))
1357 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
1358 fColor
= ((col
!= RGB(0,0,0)) && (col
!= RGB(0xff,0xff,0xff)));
1364 fprintf( stderr
, "CreateDIBitmap: wrong size (%ld) for data\n",
1365 data
->bmiHeader
.biSize
);
1370 /* Now create the bitmap */
1372 handle
= fColor
? CreateCompatibleBitmap32( hdc
, width
, height
) :
1373 CreateBitmap32( width
, height
, 1, 1, NULL
);
1374 if (!handle
) return 0;
1376 if (init
== CBM_INIT
)
1377 SetDIBits32( hdc
, handle
, 0, height
, bits
, data
, coloruse
);
1381 /***********************************************************************
1382 * CreateDIBSection16 (GDI.489)
1384 HBITMAP16 WINAPI
CreateDIBSection16 (HDC16 hdc
, BITMAPINFO
*bmi
, UINT16 usage
,
1385 LPVOID
**bits
, HANDLE16 section
,
1388 return CreateDIBSection32 (hdc
, bmi
, usage
, bits
, section
, offset
);
1391 /***********************************************************************
1392 * CreateDIBSection32 (GDI32.36)
1394 HBITMAP32 WINAPI
CreateDIBSection32 (HDC32 hdc
, BITMAPINFO
*bmi
, UINT32 usage
,
1395 LPVOID
**bits
,HANDLE32 section
,
1401 "CreateDIBSection(%d,[w=%ld,h=%ld],%d,%p,0x%08x,%ld),semistub\n",
1402 hdc
,bmi
->bmiHeader
.biWidth
,bmi
->bmiHeader
.biHeight
,
1403 usage
,bits
,section
,offset
1405 /* FIXME. The following line isn't quite right. */
1406 res
= CreateDIBitmap32 (hdc
, &bmi
->bmiHeader
, 0, NULL
, bmi
, 0);
1410 if (GetObject32A (res
, sizeof (bmp
), &bmp
))
1418 if (res
) DeleteObject32 (res
);