2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
12 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(bitmap
);
17 /***********************************************************************
18 * DIB_GetDIBWidthBytes
20 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
21 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
23 int DIB_GetDIBWidthBytes( int width
, int depth
)
29 case 1: words
= (width
+ 31) / 32; break;
30 case 4: words
= (width
+ 7) / 8; break;
31 case 8: words
= (width
+ 3) / 4; break;
33 case 16: words
= (width
+ 1) / 2; break;
34 case 24: words
= (width
* 3 + 3)/4; break;
37 WARN("(%d): Unsupported depth\n", depth
);
45 /***********************************************************************
46 * DIB_GetDIBImageBytes
48 * Return the number of bytes used to hold the image in a DIB bitmap.
50 int DIB_GetDIBImageBytes( int width
, int height
, int depth
)
52 return DIB_GetDIBWidthBytes( width
, depth
) * abs( height
);
56 /***********************************************************************
59 * Return the size of the bitmap info structure including color table.
61 int DIB_BitmapInfoSize( const BITMAPINFO
* info
, WORD coloruse
)
65 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
67 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
68 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
69 return sizeof(BITMAPCOREHEADER
) + colors
*
70 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
72 else /* assume BITMAPINFOHEADER */
74 colors
= info
->bmiHeader
.biClrUsed
;
75 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
76 colors
= 1 << info
->bmiHeader
.biBitCount
;
77 return sizeof(BITMAPINFOHEADER
) + colors
*
78 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
83 /***********************************************************************
86 * Get the info from a bitmap header.
87 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
89 int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
90 int *height
, WORD
*bpp
, WORD
*compr
)
92 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
94 *width
= header
->biWidth
;
95 *height
= header
->biHeight
;
96 *bpp
= header
->biBitCount
;
97 *compr
= header
->biCompression
;
100 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
102 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
103 *width
= core
->bcWidth
;
104 *height
= core
->bcHeight
;
105 *bpp
= core
->bcBitCount
;
109 WARN("(%ld): wrong size for header\n", header
->biSize
);
114 /***********************************************************************
115 * StretchDIBits16 (GDI.439)
117 INT16 WINAPI
StretchDIBits16(HDC16 hdc
, INT16 xDst
, INT16 yDst
, INT16 widthDst
,
118 INT16 heightDst
, INT16 xSrc
, INT16 ySrc
, INT16 widthSrc
,
119 INT16 heightSrc
, const VOID
*bits
,
120 const BITMAPINFO
*info
, UINT16 wUsage
, DWORD dwRop
)
122 return (INT16
)StretchDIBits( hdc
, xDst
, yDst
, widthDst
, heightDst
,
123 xSrc
, ySrc
, widthSrc
, heightSrc
, bits
,
124 info
, wUsage
, dwRop
);
128 /***********************************************************************
129 * StretchDIBits (GDI32.351)
131 INT WINAPI
StretchDIBits(HDC hdc
, INT xDst
, INT yDst
, INT widthDst
,
132 INT heightDst
, INT xSrc
, INT ySrc
, INT widthSrc
,
133 INT heightSrc
, const void *bits
,
134 const BITMAPINFO
*info
, UINT wUsage
, DWORD dwRop
)
136 DC
*dc
= DC_GetDCPtr( hdc
);
137 if(!dc
) return FALSE
;
139 if(dc
->funcs
->pStretchDIBits
)
140 return dc
->funcs
->pStretchDIBits(dc
, xDst
, yDst
, widthDst
,
141 heightDst
, xSrc
, ySrc
, widthSrc
,
142 heightSrc
, bits
, info
, wUsage
,
144 else { /* use StretchBlt */
145 HBITMAP hBitmap
, hOldBitmap
;
148 hBitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
149 bits
, info
, wUsage
);
150 hdcMem
= CreateCompatibleDC( hdc
);
151 hOldBitmap
= SelectObject( hdcMem
, hBitmap
);
152 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
153 left (negative biHeight) */
154 StretchBlt( hdc
, xDst
, yDst
, widthDst
, heightDst
,
155 hdcMem
, xSrc
, abs(info
->bmiHeader
.biHeight
) - heightSrc
- ySrc
,
156 widthSrc
, heightSrc
, dwRop
);
157 SelectObject( hdcMem
, hOldBitmap
);
159 DeleteObject( hBitmap
);
165 /***********************************************************************
166 * SetDIBits16 (GDI.440)
168 INT16 WINAPI
SetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
169 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
172 return SetDIBits( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
176 /******************************************************************************
177 * SetDIBits [GDI32.312] Sets pixels in a bitmap using colors from DIB
180 * hdc [I] Handle to device context
181 * hbitmap [I] Handle to bitmap
182 * startscan [I] Starting scan line
183 * lines [I] Number of scan lines
184 * bits [I] Array of bitmap bits
185 * info [I] Address of structure with data
186 * coloruse [I] Type of color indexes to use
189 * Success: Number of scan lines copied
192 INT WINAPI
SetDIBits( HDC hdc
, HBITMAP hbitmap
, UINT startscan
,
193 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
,
200 /* Check parameters */
201 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
204 dc
= (DC
*) GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
208 if (!(bitmap
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
210 GDI_HEAP_UNLOCK( hdc
);
214 result
= BITMAP_Driver
->pSetDIBits(bitmap
, dc
, startscan
,
218 GDI_HEAP_UNLOCK( hdc
);
219 GDI_HEAP_UNLOCK( hbitmap
);
225 /***********************************************************************
226 * SetDIBitsToDevice16 (GDI.443)
228 INT16 WINAPI
SetDIBitsToDevice16(HDC16 hdc
, INT16 xDest
, INT16 yDest
, INT16 cx
,
229 INT16 cy
, INT16 xSrc
, INT16 ySrc
, UINT16 startscan
,
230 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
233 return SetDIBitsToDevice( hdc
, xDest
, yDest
, cx
, cy
, xSrc
, ySrc
,
234 startscan
, lines
, bits
, info
, coloruse
);
238 /***********************************************************************
239 * SetDIBitsToDevice (GDI32.313)
241 INT WINAPI
SetDIBitsToDevice(HDC hdc
, INT xDest
, INT yDest
, DWORD cx
,
242 DWORD cy
, INT xSrc
, INT ySrc
, UINT startscan
,
243 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
,
249 if (!(dc
= DC_GetDCPtr( hdc
))) return 0;
251 if(dc
->funcs
->pSetDIBitsToDevice
)
252 ret
= dc
->funcs
->pSetDIBitsToDevice( dc
, xDest
, yDest
, cx
, cy
, xSrc
,
253 ySrc
, startscan
, lines
, bits
,
256 FIXME("unimplemented on hdc %08x\n", hdc
);
260 GDI_HEAP_UNLOCK( hdc
);
264 /***********************************************************************
265 * SetDIBColorTable16 (GDI.602)
267 UINT16 WINAPI
SetDIBColorTable16( HDC16 hdc
, UINT16 startpos
, UINT16 entries
,
270 return SetDIBColorTable( hdc
, startpos
, entries
, colors
);
273 /***********************************************************************
274 * SetDIBColorTable (GDI32.311)
276 UINT WINAPI
SetDIBColorTable( HDC hdc
, UINT startpos
, UINT entries
,
280 PALETTEENTRY
* palEntry
;
281 PALETTEOBJ
* palette
;
284 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
287 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
291 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
296 /* Transfer color info */
298 if (dc
->w
.bitsPerPixel
<= 8) {
299 palEntry
= palette
->logpalette
.palPalEntry
+ startpos
;
300 if (startpos
+ entries
> (1 << dc
->w
.bitsPerPixel
))
301 entries
= (1 << dc
->w
.bitsPerPixel
) - startpos
;
303 if (startpos
+ entries
> palette
->logpalette
.palNumEntries
)
304 entries
= palette
->logpalette
.palNumEntries
- startpos
;
306 for (end
= colors
+ entries
; colors
< end
; palEntry
++, colors
++)
308 palEntry
->peRed
= colors
->rgbRed
;
309 palEntry
->peGreen
= colors
->rgbGreen
;
310 palEntry
->peBlue
= colors
->rgbBlue
;
315 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
319 /***********************************************************************
320 * GetDIBColorTable16 (GDI.603)
322 UINT16 WINAPI
GetDIBColorTable16( HDC16 hdc
, UINT16 startpos
, UINT16 entries
,
325 return GetDIBColorTable( hdc
, startpos
, entries
, colors
);
328 /***********************************************************************
329 * GetDIBColorTable (GDI32.169)
331 UINT WINAPI
GetDIBColorTable( HDC hdc
, UINT startpos
, UINT entries
,
335 PALETTEENTRY
* palEntry
;
336 PALETTEOBJ
* palette
;
339 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
342 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
346 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
351 /* Transfer color info */
353 if (dc
->w
.bitsPerPixel
<= 8) {
354 palEntry
= palette
->logpalette
.palPalEntry
+ startpos
;
355 if (startpos
+ entries
> (1 << dc
->w
.bitsPerPixel
)) {
356 entries
= (1 << dc
->w
.bitsPerPixel
) - startpos
;
358 for (end
= colors
+ entries
; colors
< end
; palEntry
++, colors
++)
360 colors
->rgbRed
= palEntry
->peRed
;
361 colors
->rgbGreen
= palEntry
->peGreen
;
362 colors
->rgbBlue
= palEntry
->peBlue
;
363 colors
->rgbReserved
= 0;
368 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
372 /* FIXME the following two structs should be combined with __sysPalTemplate in
373 objects/color.c - this should happen after de-X11-ing both of these
375 NB. RGBQUAD and PALETTENTRY have different orderings of red, green
378 static RGBQUAD EGAColors
[16] = {
379 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
380 { 0x00, 0x00, 0x00, 0x00 },
381 { 0x00, 0x00, 0x80, 0x00 },
382 { 0x00, 0x80, 0x00, 0x00 },
383 { 0x00, 0x80, 0x80, 0x00 },
384 { 0x80, 0x00, 0x00, 0x00 },
385 { 0x80, 0x00, 0x80, 0x00 },
386 { 0x80, 0x80, 0x00, 0x00 },
387 { 0x80, 0x80, 0x80, 0x00 },
388 { 0xc0, 0xc0, 0xc0, 0x00 },
389 { 0x00, 0x00, 0xff, 0x00 },
390 { 0x00, 0xff, 0x00, 0x00 },
391 { 0x00, 0xff, 0xff, 0x00 },
392 { 0xff, 0x00, 0x00, 0x00 },
393 { 0xff, 0x00, 0xff, 0x00 },
394 { 0xff, 0xff, 0x00, 0x00 },
395 { 0xff, 0xff, 0xff, 0x00 }
399 static RGBQUAD DefLogPalette
[20] = { /* Copy of Default Logical Palette */
400 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
401 { 0x00, 0x00, 0x00, 0x00 },
402 { 0x00, 0x00, 0x80, 0x00 },
403 { 0x00, 0x80, 0x00, 0x00 },
404 { 0x00, 0x80, 0x80, 0x00 },
405 { 0x80, 0x00, 0x00, 0x00 },
406 { 0x80, 0x00, 0x80, 0x00 },
407 { 0x80, 0x80, 0x00, 0x00 },
408 { 0xc0, 0xc0, 0xc0, 0x00 },
409 { 0xc0, 0xdc, 0xc0, 0x00 },
410 { 0xf0, 0xca, 0xa6, 0x00 },
411 { 0xf0, 0xfb, 0xff, 0x00 },
412 { 0xa4, 0xa0, 0xa0, 0x00 },
413 { 0x80, 0x80, 0x80, 0x00 },
414 { 0x00, 0x00, 0xf0, 0x00 },
415 { 0x00, 0xff, 0x00, 0x00 },
416 { 0x00, 0xff, 0xff, 0x00 },
417 { 0xff, 0x00, 0x00, 0x00 },
418 { 0xff, 0x00, 0xff, 0x00 },
419 { 0xff, 0xff, 0x00, 0x00 },
420 { 0xff, 0xff, 0xff, 0x00 }
423 /***********************************************************************
424 * GetDIBits16 (GDI.441)
426 INT16 WINAPI
GetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
427 UINT16 lines
, LPVOID bits
, BITMAPINFO
* info
,
430 return GetDIBits( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
434 /******************************************************************************
435 * GetDIBits [GDI32.170] Retrieves bits of bitmap and copies to buffer
438 * Success: Number of scan lines copied from bitmap
441 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
443 INT WINAPI
GetDIBits(
444 HDC hdc
, /* [in] Handle to device context */
445 HBITMAP hbitmap
, /* [in] Handle to bitmap */
446 UINT startscan
, /* [in] First scan line to set in dest bitmap */
447 UINT lines
, /* [in] Number of scan lines to copy */
448 LPVOID bits
, /* [out] Address of array for bitmap bits */
449 BITMAPINFO
* info
, /* [out] Address of structure with bitmap data */
450 UINT coloruse
) /* [in] RGB or palette index */
454 PALETTEENTRY
* palEntry
;
455 PALETTEOBJ
* palette
;
459 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
462 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
465 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
467 GDI_HEAP_UNLOCK( hdc
);
470 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
472 GDI_HEAP_UNLOCK( hdc
);
473 GDI_HEAP_UNLOCK( hbitmap
);
477 /* Transfer color info */
479 if (info
->bmiHeader
.biBitCount
<= 8 && info
->bmiHeader
.biBitCount
> 0 ) {
481 info
->bmiHeader
.biClrUsed
= 0;
483 if(info
->bmiHeader
.biBitCount
>= bmp
->bitmap
.bmBitsPixel
) {
484 palEntry
= palette
->logpalette
.palPalEntry
;
485 for (i
= 0; i
< (1 << bmp
->bitmap
.bmBitsPixel
); i
++, palEntry
++) {
486 if (coloruse
== DIB_RGB_COLORS
) {
487 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
488 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
489 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
490 info
->bmiColors
[i
].rgbReserved
= 0;
492 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
495 switch (info
->bmiHeader
.biBitCount
) {
497 info
->bmiColors
[0].rgbRed
= info
->bmiColors
[0].rgbGreen
=
498 info
->bmiColors
[0].rgbBlue
= 0;
499 info
->bmiColors
[0].rgbReserved
= 0;
500 info
->bmiColors
[1].rgbRed
= info
->bmiColors
[1].rgbGreen
=
501 info
->bmiColors
[1].rgbBlue
= 0xff;
502 info
->bmiColors
[1].rgbReserved
= 0;
506 memcpy(info
->bmiColors
, EGAColors
, sizeof(EGAColors
));
514 memcpy(info
->bmiColors
, DefLogPalette
,
515 10 * sizeof(RGBQUAD
));
516 memcpy(info
->bmiColors
+ 246, DefLogPalette
+ 10,
517 10 * sizeof(RGBQUAD
));
518 color
= info
->bmiColors
+ 10;
519 for(r
= 0; r
<= 5; r
++) /* FIXME */
520 for(g
= 0; g
<= 5; g
++)
521 for(b
= 0; b
<= 5; b
++) {
522 color
->rgbRed
= (r
* 0xff) / 5;
523 color
->rgbGreen
= (g
* 0xff) / 5;
524 color
->rgbBlue
= (b
* 0xff) / 5;
525 color
->rgbReserved
= 0;
533 GDI_HEAP_UNLOCK( dc
->w
.hPalette
);
537 /* If the bitmap object already have a dib section that contains image data, get the bits from it*/
538 if(bmp
->dib
&& bmp
->dib
->dsBm
.bmBitsPixel
>= 15 && info
->bmiHeader
.biBitCount
>= 15)
540 /*FIXME: Only RGB dibs supported for now */
541 int srcwidth
= bmp
->dib
->dsBm
.bmWidth
, srcwidthb
= bmp
->dib
->dsBm
.bmWidthBytes
;
542 int dstwidthb
= DIB_GetDIBWidthBytes( info
->bmiHeader
.biWidth
, info
->bmiHeader
.biBitCount
);
543 LPBYTE dbits
= bits
, sbits
= (LPBYTE
) bmp
->dib
->dsBm
.bmBits
+ (startscan
* srcwidthb
);
546 if ((info
->bmiHeader
.biHeight
< 0) ^ (bmp
->dib
->dsBmih
.biHeight
< 0))
548 dbits
= (LPBYTE
)bits
+ (dstwidthb
* (lines
-1));
549 dstwidthb
= -dstwidthb
;
552 switch( info
->bmiHeader
.biBitCount
) {
555 case 16: /* 16 bpp dstDIB */
557 LPWORD dstbits
= (LPWORD
)dbits
;
558 WORD rmask
= 0x7c00, gmask
= 0x03e0, bmask
= 0x001f;
560 /* FIXME: BI_BITFIELDS not supported yet */
562 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
564 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
566 /* FIXME: BI_BITFIELDS not supported yet */
567 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
568 memcpy(dbits
, sbits
, srcwidthb
);
572 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
574 LPBYTE srcbits
= sbits
;
576 for( y
= 0; y
< lines
; y
++) {
577 for( x
= 0; x
< srcwidth
; x
++ )
578 *dstbits
++ = ((*srcbits
++ >> 3) & bmask
) |
579 (((WORD
)*srcbits
++ << 2) & gmask
) |
580 (((WORD
)*srcbits
++ << 7) & rmask
);
581 dstbits
= (LPWORD
)(dbits
+=dstwidthb
);
582 srcbits
= (sbits
+= srcwidthb
);
587 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
589 LPDWORD srcbits
= (LPDWORD
)sbits
;
592 for( y
= 0; y
< lines
; y
++) {
593 for( x
= 0; x
< srcwidth
; x
++ ) {
595 *dstbits
++ = (WORD
)(((val
>> 3) & bmask
) | ((val
>> 6) & gmask
) |
596 ((val
>> 9) & rmask
));
598 dstbits
= (LPWORD
)(dbits
+=dstwidthb
);
599 srcbits
= (LPDWORD
)(sbits
+=srcwidthb
);
604 default: /* ? bit bmp -> 16 bit DIB */
605 FIXME("15/16 bit DIB %d bit bitmap\n",
606 bmp
->bitmap
.bmBitsPixel
);
612 case 24: /* 24 bpp dstDIB */
614 LPBYTE dstbits
= dbits
;
616 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
618 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
620 LPWORD srcbits
= (LPWORD
)sbits
;
623 /* FIXME: BI_BITFIELDS not supported yet */
624 for( y
= 0; y
< lines
; y
++) {
625 for( x
= 0; x
< srcwidth
; x
++ ) {
627 *dstbits
++ = (BYTE
)(((val
<< 3) & 0xf8) | ((val
>> 2) & 0x07));
628 *dstbits
++ = (BYTE
)(((val
>> 2) & 0xf8) | ((val
>> 7) & 0x07));
629 *dstbits
++ = (BYTE
)(((val
>> 7) & 0xf8) | ((val
>> 12) & 0x07));
631 dstbits
= (LPBYTE
)(dbits
+=dstwidthb
);
632 srcbits
= (LPWORD
)(sbits
+=srcwidthb
);
637 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
639 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
640 memcpy(dbits
, sbits
, srcwidthb
);
644 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
646 LPBYTE srcbits
= (LPBYTE
)sbits
;
648 for( y
= 0; y
< lines
; y
++) {
649 for( x
= 0; x
< srcwidth
; x
++, srcbits
++ ) {
650 *dstbits
++ = *srcbits
++;
651 *dstbits
++ = *srcbits
++;
652 *dstbits
++ = *srcbits
++;
654 dstbits
=(LPBYTE
)(dbits
+=dstwidthb
);
655 srcbits
= (LPBYTE
)(sbits
+=srcwidthb
);
660 default: /* ? bit bmp -> 24 bit DIB */
661 FIXME("24 bit DIB %d bit bitmap\n",
662 bmp
->bitmap
.bmBitsPixel
);
668 case 32: /* 32 bpp dstDIB */
670 LPDWORD dstbits
= (LPDWORD
)dbits
;
672 /* FIXME: BI_BITFIELDS not supported yet */
674 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
675 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
677 LPWORD srcbits
= (LPWORD
)sbits
;
680 /* FIXME: BI_BITFIELDS not supported yet */
681 for( y
= 0; y
< lines
; y
++) {
682 for( x
= 0; x
< srcwidth
; x
++ ) {
683 val
= (DWORD
)*srcbits
++;
684 *dstbits
++ = ((val
<< 3) & 0xf8) | ((val
>> 2) & 0x07) |
685 ((val
<< 6) & 0xf800) | ((val
<< 1) & 0x0700) |
686 ((val
<< 9) & 0xf80000) | ((val
<< 4) & 0x070000);
688 dstbits
=(LPDWORD
)(dbits
+=dstwidthb
);
689 srcbits
=(LPWORD
)(sbits
+=srcwidthb
);
694 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
696 LPBYTE srcbits
= sbits
;
698 for( y
= 0; y
< lines
; y
++) {
699 for( x
= 0; x
< srcwidth
; x
++, srcbits
+=3 )
700 *dstbits
++ = ((DWORD
)*srcbits
) & 0x00ffffff;
701 dstbits
=(LPDWORD
)(dbits
+=dstwidthb
);
702 srcbits
=(sbits
+=srcwidthb
);
707 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
709 /* FIXME: BI_BITFIELDS not supported yet */
710 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
711 memcpy(dbits
, sbits
, srcwidthb
);
715 default: /* ? bit bmp -> 16 bit DIB */
716 FIXME("15/16 bit DIB %d bit bitmap\n",
717 bmp
->bitmap
.bmBitsPixel
);
723 default: /* ? bit DIB */
724 FIXME("Unsupported DIB depth %d\n", info
->bmiHeader
.biBitCount
);
728 /* Otherwise, get bits from the XImage */
729 else if(!BITMAP_Driver
->pGetDIBits(bmp
, dc
, startscan
, lines
, bits
, info
, coloruse
, hbitmap
))
731 GDI_HEAP_UNLOCK( hdc
);
732 GDI_HEAP_UNLOCK( hbitmap
);
737 else if( info
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
) )
739 /* fill in struct members */
741 if( info
->bmiHeader
.biBitCount
== 0)
743 info
->bmiHeader
.biWidth
= bmp
->bitmap
.bmWidth
;
744 info
->bmiHeader
.biHeight
= bmp
->bitmap
.bmHeight
;
745 info
->bmiHeader
.biPlanes
= 1;
746 info
->bmiHeader
.biBitCount
= bmp
->bitmap
.bmBitsPixel
;
747 info
->bmiHeader
.biSizeImage
=
748 DIB_GetDIBImageBytes( bmp
->bitmap
.bmWidth
,
749 bmp
->bitmap
.bmHeight
,
750 bmp
->bitmap
.bmBitsPixel
);
751 info
->bmiHeader
.biCompression
= 0;
755 info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(
756 info
->bmiHeader
.biWidth
,
757 info
->bmiHeader
.biHeight
,
758 info
->bmiHeader
.biBitCount
);
762 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
763 info
->bmiHeader
.biSizeImage
, info
->bmiHeader
.biWidth
,
764 info
->bmiHeader
.biHeight
);
766 GDI_HEAP_UNLOCK( hdc
);
767 GDI_HEAP_UNLOCK( hbitmap
);
773 /***********************************************************************
774 * CreateDIBitmap16 (GDI.442)
776 HBITMAP16 WINAPI
CreateDIBitmap16( HDC16 hdc
, const BITMAPINFOHEADER
* header
,
777 DWORD init
, LPCVOID bits
, const BITMAPINFO
* data
,
780 return CreateDIBitmap( hdc
, header
, init
, bits
, data
, coloruse
);
784 /***********************************************************************
785 * CreateDIBitmap (GDI32.37)
787 HBITMAP WINAPI
CreateDIBitmap( HDC hdc
, const BITMAPINFOHEADER
*header
,
788 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
798 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
, &compr
) == -1) return 0;
799 if (height
< 0) height
= -height
;
801 /* Check if we should create a monochrome or color bitmap. */
802 /* We create a monochrome bitmap only if it has exactly 2 */
803 /* colors, which are black followed by white, nothing else. */
804 /* In all other cases, we create a color bitmap. */
806 if (bpp
!= 1) fColor
= TRUE
;
807 else if ((coloruse
!= DIB_RGB_COLORS
) ||
808 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
811 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
813 RGBQUAD
*rgb
= data
->bmiColors
;
814 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
816 /* Check if the first color of the colormap is black */
817 if ((col
== RGB(0,0,0)))
820 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
821 /* If the second color is white, create a monochrome bitmap */
822 fColor
= (col
!= RGB(0xff,0xff,0xff));
824 /* Note : If the first color of the colormap is white
825 followed by black, we have to create a color bitmap.
826 If we don't the white will be displayed in black later on!*/
829 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
831 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
832 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
833 if ((col
== RGB(0,0,0)))
836 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
837 fColor
= (col
!= RGB(0xff,0xff,0xff));
843 WARN("(%ld): wrong size for data\n",
844 data
->bmiHeader
.biSize
);
849 /* Now create the bitmap */
853 HDC tmpdc
= CreateDCA( "DISPLAY", NULL
, NULL
, NULL
);
854 handle
= CreateCompatibleBitmap( tmpdc
, width
, height
);
857 else handle
= CreateBitmap( width
, height
, 1, 1, NULL
);
859 if (!handle
) return 0;
861 if (init
== CBM_INIT
)
862 SetDIBits( hdc
, handle
, 0, height
, bits
, data
, coloruse
);
866 /***********************************************************************
867 * CreateDIBSection16 (GDI.489)
869 HBITMAP16 WINAPI
CreateDIBSection16 (HDC16 hdc
, BITMAPINFO
*bmi
, UINT16 usage
,
870 SEGPTR
*bits
, HANDLE section
,
875 BOOL bDesktopDC
= FALSE
;
877 /* If the reference hdc is null, take the desktop dc */
880 hdc
= CreateCompatibleDC(0);
884 dc
= (DC
*) GDI_GetObjPtr(hdc
, DC_MAGIC
);
885 if(!dc
) dc
= (DC
*) GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
886 if(!dc
) return (HBITMAP16
) NULL
;
888 hbitmap
= dc
->funcs
->pCreateDIBSection16(dc
, bmi
, usage
, bits
, section
, offset
, 0);
890 GDI_HEAP_UNLOCK(hdc
);
898 /***********************************************************************
899 * DIB_CreateDIBSection
901 HBITMAP
DIB_CreateDIBSection(HDC hdc
, BITMAPINFO
*bmi
, UINT usage
,
902 LPVOID
*bits
, HANDLE section
,
903 DWORD offset
, DWORD ovr_pitch
)
907 BOOL bDesktopDC
= FALSE
;
909 /* If the reference hdc is null, take the desktop dc */
912 hdc
= CreateCompatibleDC(0);
916 dc
= (DC
*) GDI_GetObjPtr(hdc
, DC_MAGIC
);
917 if(!dc
) dc
= (DC
*) GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
918 if(!dc
) return (HBITMAP
) NULL
;
920 hbitmap
= dc
->funcs
->pCreateDIBSection(dc
, bmi
, usage
, bits
, section
, offset
, ovr_pitch
);
922 GDI_HEAP_UNLOCK(hdc
);
930 /***********************************************************************
931 * CreateDIBSection (GDI32.36)
933 HBITMAP WINAPI
CreateDIBSection(HDC hdc
, BITMAPINFO
*bmi
, UINT usage
,
934 LPVOID
*bits
, HANDLE section
,
937 return DIB_CreateDIBSection(hdc
, bmi
, usage
, bits
, section
, offset
, 0);
940 /***********************************************************************
941 * DIB_DeleteDIBSection
943 void DIB_DeleteDIBSection( BITMAPOBJ
*bmp
)
947 DIBSECTION
*dib
= bmp
->dib
;
949 if (dib
->dsBm
.bmBits
)
952 UnmapViewOfFile(dib
->dsBm
.bmBits
);
953 else if (!dib
->dsOffset
)
954 VirtualFree(dib
->dsBm
.bmBits
, 0L, MEM_RELEASE
);
957 BITMAP_Driver
->pDeleteDIBSection(bmp
);
959 HeapFree(GetProcessHeap(), 0, dib
);
964 /***********************************************************************
965 * DIB_CreateDIBFromBitmap
966 * Allocates a packed DIB and copies the bitmap data into it.
968 HGLOBAL
DIB_CreateDIBFromBitmap(HDC hdc
, HBITMAP hBmp
)
970 BITMAPOBJ
*pBmp
= NULL
;
971 HGLOBAL hPackedDIB
= 0;
972 LPBYTE pPackedDIB
= NULL
;
973 LPBITMAPINFOHEADER pbmiHeader
= NULL
;
974 unsigned int width
, height
, depth
, cDataSize
= 0, cPackedSize
= 0,
975 OffsetBits
= 0, nLinesCopied
= 0;
977 /* Get a pointer to the BITMAPOBJ structure */
978 pBmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hBmp
, BITMAP_MAGIC
);
980 /* Get the bitmap dimensions */
981 width
= pBmp
->bitmap
.bmWidth
;
982 height
= pBmp
->bitmap
.bmHeight
;
983 depth
= pBmp
->bitmap
.bmBitsPixel
;
986 * A packed DIB contains a BITMAPINFO structure followed immediately by
987 * an optional color palette and the pixel data.
990 /* Calculate the size of the packed DIB */
991 cDataSize
= DIB_GetDIBImageBytes( width
, height
, depth
);
992 cPackedSize
= sizeof(BITMAPINFOHEADER
)
993 + ( (depth
<= 8) ? (sizeof(RGBQUAD
) * (1 << depth
)) : 0 )
995 /* Get the offset to the bits */
996 OffsetBits
= cPackedSize
- cDataSize
;
998 /* Allocate the packed DIB */
999 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize
);
1000 hPackedDIB
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
/*| GMEM_ZEROINIT*/,
1004 WARN("Could not allocate packed DIB!\n");
1008 /* A packed DIB starts with a BITMAPINFOHEADER */
1009 pPackedDIB
= (LPBYTE
)GlobalLock(hPackedDIB
);
1010 pbmiHeader
= (LPBITMAPINFOHEADER
)pPackedDIB
;
1012 /* Init the BITMAPINFOHEADER */
1013 pbmiHeader
->biSize
= sizeof(BITMAPINFOHEADER
);
1014 pbmiHeader
->biWidth
= width
;
1015 pbmiHeader
->biHeight
= height
;
1016 pbmiHeader
->biPlanes
= 1;
1017 pbmiHeader
->biBitCount
= depth
;
1018 pbmiHeader
->biCompression
= BI_RGB
;
1019 pbmiHeader
->biSizeImage
= 0;
1020 pbmiHeader
->biXPelsPerMeter
= pbmiHeader
->biYPelsPerMeter
= 0;
1021 pbmiHeader
->biClrUsed
= 0;
1022 pbmiHeader
->biClrImportant
= 0;
1024 /* Retrieve the DIB bits from the bitmap and fill in the
1025 * DIB color table if present */
1027 nLinesCopied
= GetDIBits(hdc
, /* Handle to device context */
1028 hBmp
, /* Handle to bitmap */
1029 0, /* First scan line to set in dest bitmap */
1030 height
, /* Number of scan lines to copy */
1031 pPackedDIB
+ OffsetBits
, /* [out] Address of array for bitmap bits */
1032 (LPBITMAPINFO
) pbmiHeader
, /* [out] Address of BITMAPINFO structure */
1033 0); /* RGB or palette index */
1034 GlobalUnlock(hPackedDIB
);
1036 /* Cleanup if GetDIBits failed */
1037 if (nLinesCopied
!= height
)
1039 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied
, height
);
1040 GlobalFree(hPackedDIB
);