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_GetDCUpdate( hdc
);
137 if(!dc
) return FALSE
;
139 if(dc
->funcs
->pStretchDIBits
)
140 heightSrc
= 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 hdcMem
= CreateCompatibleDC( hdc
);
149 if (info
->bmiHeader
.biCompression
== BI_RLE4
||
150 info
->bmiHeader
.biCompression
== BI_RLE8
) {
152 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
153 * contain all the rectangle described in bmiHeader, but only part of it.
154 * This mean that those undescribed pixels must be left untouched.
155 * So, we first copy on a memory bitmap the current content of the
156 * destination rectangle, blit the DIB bits on top of it - hence leaving
157 * the gaps untouched -, and blitting the rectangle back.
158 * This insure that gaps are untouched on the destination rectangle
159 * Not doing so leads to trashed images (the gaps contain what was on the
160 * memory bitmap => generally black or garbage)
161 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
162 * another speed vs correctness issue. Anyway, if speed is needed, then the
163 * pStretchDIBits function shall be implemented.
166 hBitmap
= CreateCompatibleBitmap(hdc
, info
->bmiHeader
.biWidth
,
167 info
->bmiHeader
.biHeight
);
168 hOldBitmap
= SelectObject( hdcMem
, hBitmap
);
170 /* copy existing bitmap from destination dc */
171 StretchBlt( hdcMem
, xSrc
, abs(info
->bmiHeader
.biHeight
) - heightSrc
- ySrc
,
172 widthSrc
, heightSrc
, hdc
, xDst
, yDst
, widthDst
, heightDst
,
174 SetDIBits(hdcMem
, hBitmap
, 0, info
->bmiHeader
.biHeight
, bits
,
175 info
, DIB_RGB_COLORS
);
178 hBitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
179 bits
, info
, wUsage
);
180 hOldBitmap
= SelectObject( hdcMem
, hBitmap
);
183 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
184 left (negative biHeight) */
185 StretchBlt( hdc
, xDst
, yDst
, widthDst
, heightDst
,
186 hdcMem
, xSrc
, abs(info
->bmiHeader
.biHeight
) - heightSrc
- ySrc
,
187 widthSrc
, heightSrc
, dwRop
);
188 SelectObject( hdcMem
, hOldBitmap
);
190 DeleteObject( hBitmap
);
192 GDI_ReleaseObj( hdc
);
197 /***********************************************************************
198 * SetDIBits16 (GDI.440)
200 INT16 WINAPI
SetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
201 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
204 return SetDIBits( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
208 /******************************************************************************
209 * SetDIBits [GDI32.312] Sets pixels in a bitmap using colors from DIB
212 * hdc [I] Handle to device context
213 * hbitmap [I] Handle to bitmap
214 * startscan [I] Starting scan line
215 * lines [I] Number of scan lines
216 * bits [I] Array of bitmap bits
217 * info [I] Address of structure with data
218 * coloruse [I] Type of color indexes to use
221 * Success: Number of scan lines copied
224 INT WINAPI
SetDIBits( HDC hdc
, HBITMAP hbitmap
, UINT startscan
,
225 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
,
232 /* Check parameters */
233 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
235 if (!(bitmap
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
237 GDI_ReleaseObj( hdc
);
241 result
= BITMAP_Driver
->pSetDIBits(bitmap
, dc
, startscan
,
245 GDI_ReleaseObj( hbitmap
);
246 GDI_ReleaseObj( hdc
);
252 /***********************************************************************
253 * SetDIBitsToDevice16 (GDI.443)
255 INT16 WINAPI
SetDIBitsToDevice16(HDC16 hdc
, INT16 xDest
, INT16 yDest
, INT16 cx
,
256 INT16 cy
, INT16 xSrc
, INT16 ySrc
, UINT16 startscan
,
257 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
260 return SetDIBitsToDevice( hdc
, xDest
, yDest
, cx
, cy
, xSrc
, ySrc
,
261 startscan
, lines
, bits
, info
, coloruse
);
265 /***********************************************************************
266 * SetDIBitsToDevice (GDI32.313)
268 INT WINAPI
SetDIBitsToDevice(HDC hdc
, INT xDest
, INT yDest
, DWORD cx
,
269 DWORD cy
, INT xSrc
, INT ySrc
, UINT startscan
,
270 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
,
276 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
278 if(dc
->funcs
->pSetDIBitsToDevice
)
279 ret
= dc
->funcs
->pSetDIBitsToDevice( dc
, xDest
, yDest
, cx
, cy
, xSrc
,
280 ySrc
, startscan
, lines
, bits
,
283 FIXME("unimplemented on hdc %08x\n", hdc
);
287 GDI_ReleaseObj( hdc
);
291 /***********************************************************************
292 * SetDIBColorTable16 (GDI.602)
294 UINT16 WINAPI
SetDIBColorTable16( HDC16 hdc
, UINT16 startpos
, UINT16 entries
,
297 return SetDIBColorTable( hdc
, startpos
, entries
, colors
);
300 /***********************************************************************
301 * SetDIBColorTable (GDI32.311)
303 UINT WINAPI
SetDIBColorTable( HDC hdc
, UINT startpos
, UINT entries
,
307 PALETTEENTRY
* palEntry
;
308 PALETTEOBJ
* palette
;
311 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
313 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
315 GDI_ReleaseObj( hdc
);
319 /* Transfer color info */
321 if (dc
->w
.bitsPerPixel
<= 8) {
322 palEntry
= palette
->logpalette
.palPalEntry
+ startpos
;
323 if (startpos
+ entries
> (1 << dc
->w
.bitsPerPixel
))
324 entries
= (1 << dc
->w
.bitsPerPixel
) - startpos
;
326 if (startpos
+ entries
> palette
->logpalette
.palNumEntries
)
327 entries
= palette
->logpalette
.palNumEntries
- startpos
;
329 for (end
= colors
+ entries
; colors
< end
; palEntry
++, colors
++)
331 palEntry
->peRed
= colors
->rgbRed
;
332 palEntry
->peGreen
= colors
->rgbGreen
;
333 palEntry
->peBlue
= colors
->rgbBlue
;
338 GDI_ReleaseObj( dc
->w
.hPalette
);
339 GDI_ReleaseObj( hdc
);
343 /***********************************************************************
344 * GetDIBColorTable16 (GDI.603)
346 UINT16 WINAPI
GetDIBColorTable16( HDC16 hdc
, UINT16 startpos
, UINT16 entries
,
349 return GetDIBColorTable( hdc
, startpos
, entries
, colors
);
352 /***********************************************************************
353 * GetDIBColorTable (GDI32.169)
355 UINT WINAPI
GetDIBColorTable( HDC hdc
, UINT startpos
, UINT entries
,
359 PALETTEENTRY
* palEntry
;
360 PALETTEOBJ
* palette
;
363 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
365 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
367 GDI_ReleaseObj( hdc
);
371 /* Transfer color info */
373 if (dc
->w
.bitsPerPixel
<= 8) {
374 palEntry
= palette
->logpalette
.palPalEntry
+ startpos
;
375 if (startpos
+ entries
> (1 << dc
->w
.bitsPerPixel
)) {
376 entries
= (1 << dc
->w
.bitsPerPixel
) - startpos
;
378 for (end
= colors
+ entries
; colors
< end
; palEntry
++, colors
++)
380 colors
->rgbRed
= palEntry
->peRed
;
381 colors
->rgbGreen
= palEntry
->peGreen
;
382 colors
->rgbBlue
= palEntry
->peBlue
;
383 colors
->rgbReserved
= 0;
388 GDI_ReleaseObj( dc
->w
.hPalette
);
389 GDI_ReleaseObj( hdc
);
393 /* FIXME the following two structs should be combined with __sysPalTemplate in
394 objects/color.c - this should happen after de-X11-ing both of these
396 NB. RGBQUAD and PALETTENTRY have different orderings of red, green
399 static RGBQUAD EGAColors
[16] = {
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 { 0x80, 0x80, 0x80, 0x00 },
409 { 0xc0, 0xc0, 0xc0, 0x00 },
410 { 0x00, 0x00, 0xff, 0x00 },
411 { 0x00, 0xff, 0x00, 0x00 },
412 { 0x00, 0xff, 0xff, 0x00 },
413 { 0xff, 0x00, 0x00, 0x00 },
414 { 0xff, 0x00, 0xff, 0x00 },
415 { 0xff, 0xff, 0x00, 0x00 },
416 { 0xff, 0xff, 0xff, 0x00 }
420 static RGBQUAD DefLogPalette
[20] = { /* Copy of Default Logical Palette */
421 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
422 { 0x00, 0x00, 0x00, 0x00 },
423 { 0x00, 0x00, 0x80, 0x00 },
424 { 0x00, 0x80, 0x00, 0x00 },
425 { 0x00, 0x80, 0x80, 0x00 },
426 { 0x80, 0x00, 0x00, 0x00 },
427 { 0x80, 0x00, 0x80, 0x00 },
428 { 0x80, 0x80, 0x00, 0x00 },
429 { 0xc0, 0xc0, 0xc0, 0x00 },
430 { 0xc0, 0xdc, 0xc0, 0x00 },
431 { 0xf0, 0xca, 0xa6, 0x00 },
432 { 0xf0, 0xfb, 0xff, 0x00 },
433 { 0xa4, 0xa0, 0xa0, 0x00 },
434 { 0x80, 0x80, 0x80, 0x00 },
435 { 0x00, 0x00, 0xf0, 0x00 },
436 { 0x00, 0xff, 0x00, 0x00 },
437 { 0x00, 0xff, 0xff, 0x00 },
438 { 0xff, 0x00, 0x00, 0x00 },
439 { 0xff, 0x00, 0xff, 0x00 },
440 { 0xff, 0xff, 0x00, 0x00 },
441 { 0xff, 0xff, 0xff, 0x00 }
444 /***********************************************************************
445 * GetDIBits16 (GDI.441)
447 INT16 WINAPI
GetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
448 UINT16 lines
, LPVOID bits
, BITMAPINFO
* info
,
451 return GetDIBits( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
455 /******************************************************************************
456 * GetDIBits [GDI32.170] Retrieves bits of bitmap and copies to buffer
459 * Success: Number of scan lines copied from bitmap
462 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
464 INT WINAPI
GetDIBits(
465 HDC hdc
, /* [in] Handle to device context */
466 HBITMAP hbitmap
, /* [in] Handle to bitmap */
467 UINT startscan
, /* [in] First scan line to set in dest bitmap */
468 UINT lines
, /* [in] Number of scan lines to copy */
469 LPVOID bits
, /* [out] Address of array for bitmap bits */
470 BITMAPINFO
* info
, /* [out] Address of structure with bitmap data */
471 UINT coloruse
) /* [in] RGB or palette index */
475 PALETTEENTRY
* palEntry
;
476 PALETTEOBJ
* palette
;
480 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
481 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
483 GDI_ReleaseObj( hdc
);
486 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->w
.hPalette
, PALETTE_MAGIC
)))
488 GDI_ReleaseObj( hdc
);
489 GDI_ReleaseObj( hbitmap
);
493 /* Transfer color info */
495 if (info
->bmiHeader
.biBitCount
<= 8 && info
->bmiHeader
.biBitCount
> 0 ) {
497 info
->bmiHeader
.biClrUsed
= 0;
499 if(info
->bmiHeader
.biBitCount
>= bmp
->bitmap
.bmBitsPixel
) {
500 palEntry
= palette
->logpalette
.palPalEntry
;
501 for (i
= 0; i
< (1 << bmp
->bitmap
.bmBitsPixel
); i
++, palEntry
++) {
502 if (coloruse
== DIB_RGB_COLORS
) {
503 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
504 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
505 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
506 info
->bmiColors
[i
].rgbReserved
= 0;
508 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
511 switch (info
->bmiHeader
.biBitCount
) {
513 info
->bmiColors
[0].rgbRed
= info
->bmiColors
[0].rgbGreen
=
514 info
->bmiColors
[0].rgbBlue
= 0;
515 info
->bmiColors
[0].rgbReserved
= 0;
516 info
->bmiColors
[1].rgbRed
= info
->bmiColors
[1].rgbGreen
=
517 info
->bmiColors
[1].rgbBlue
= 0xff;
518 info
->bmiColors
[1].rgbReserved
= 0;
522 memcpy(info
->bmiColors
, EGAColors
, sizeof(EGAColors
));
530 memcpy(info
->bmiColors
, DefLogPalette
,
531 10 * sizeof(RGBQUAD
));
532 memcpy(info
->bmiColors
+ 246, DefLogPalette
+ 10,
533 10 * sizeof(RGBQUAD
));
534 color
= info
->bmiColors
+ 10;
535 for(r
= 0; r
<= 5; r
++) /* FIXME */
536 for(g
= 0; g
<= 5; g
++)
537 for(b
= 0; b
<= 5; b
++) {
538 color
->rgbRed
= (r
* 0xff) / 5;
539 color
->rgbGreen
= (g
* 0xff) / 5;
540 color
->rgbBlue
= (b
* 0xff) / 5;
541 color
->rgbReserved
= 0;
549 GDI_ReleaseObj( dc
->w
.hPalette
);
553 /* If the bitmap object already have a dib section that contains image data, get the bits from it*/
554 if(bmp
->dib
&& bmp
->dib
->dsBm
.bmBitsPixel
>= 15 && info
->bmiHeader
.biBitCount
>= 15)
556 /*FIXME: Only RGB dibs supported for now */
557 int srcwidth
= bmp
->dib
->dsBm
.bmWidth
, srcwidthb
= bmp
->dib
->dsBm
.bmWidthBytes
;
558 int dstwidthb
= DIB_GetDIBWidthBytes( info
->bmiHeader
.biWidth
, info
->bmiHeader
.biBitCount
);
559 LPBYTE dbits
= bits
, sbits
= (LPBYTE
) bmp
->dib
->dsBm
.bmBits
+ (startscan
* srcwidthb
);
562 if ((info
->bmiHeader
.biHeight
< 0) ^ (bmp
->dib
->dsBmih
.biHeight
< 0))
564 dbits
= (LPBYTE
)bits
+ (dstwidthb
* (lines
-1));
565 dstwidthb
= -dstwidthb
;
568 switch( info
->bmiHeader
.biBitCount
) {
571 case 16: /* 16 bpp dstDIB */
573 LPWORD dstbits
= (LPWORD
)dbits
;
574 WORD rmask
= 0x7c00, gmask
= 0x03e0, bmask
= 0x001f;
576 /* FIXME: BI_BITFIELDS not supported yet */
578 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
580 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
582 /* FIXME: BI_BITFIELDS not supported yet */
583 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
584 memcpy(dbits
, sbits
, srcwidthb
);
588 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
590 LPBYTE srcbits
= sbits
;
592 for( y
= 0; y
< lines
; y
++) {
593 for( x
= 0; x
< srcwidth
; x
++ )
594 *dstbits
++ = ((*srcbits
++ >> 3) & bmask
) |
595 (((WORD
)*srcbits
++ << 2) & gmask
) |
596 (((WORD
)*srcbits
++ << 7) & rmask
);
597 dstbits
= (LPWORD
)(dbits
+=dstwidthb
);
598 srcbits
= (sbits
+= srcwidthb
);
603 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
605 LPDWORD srcbits
= (LPDWORD
)sbits
;
608 for( y
= 0; y
< lines
; y
++) {
609 for( x
= 0; x
< srcwidth
; x
++ ) {
611 *dstbits
++ = (WORD
)(((val
>> 3) & bmask
) | ((val
>> 6) & gmask
) |
612 ((val
>> 9) & rmask
));
614 dstbits
= (LPWORD
)(dbits
+=dstwidthb
);
615 srcbits
= (LPDWORD
)(sbits
+=srcwidthb
);
620 default: /* ? bit bmp -> 16 bit DIB */
621 FIXME("15/16 bit DIB %d bit bitmap\n",
622 bmp
->bitmap
.bmBitsPixel
);
628 case 24: /* 24 bpp dstDIB */
630 LPBYTE dstbits
= dbits
;
632 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
634 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
636 LPWORD srcbits
= (LPWORD
)sbits
;
639 /* FIXME: BI_BITFIELDS not supported yet */
640 for( y
= 0; y
< lines
; y
++) {
641 for( x
= 0; x
< srcwidth
; x
++ ) {
643 *dstbits
++ = (BYTE
)(((val
<< 3) & 0xf8) | ((val
>> 2) & 0x07));
644 *dstbits
++ = (BYTE
)(((val
>> 2) & 0xf8) | ((val
>> 7) & 0x07));
645 *dstbits
++ = (BYTE
)(((val
>> 7) & 0xf8) | ((val
>> 12) & 0x07));
647 dstbits
= (LPBYTE
)(dbits
+=dstwidthb
);
648 srcbits
= (LPWORD
)(sbits
+=srcwidthb
);
653 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
655 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
656 memcpy(dbits
, sbits
, srcwidthb
);
660 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
662 LPBYTE srcbits
= (LPBYTE
)sbits
;
664 for( y
= 0; y
< lines
; y
++) {
665 for( x
= 0; x
< srcwidth
; x
++, srcbits
++ ) {
666 *dstbits
++ = *srcbits
++;
667 *dstbits
++ = *srcbits
++;
668 *dstbits
++ = *srcbits
++;
670 dstbits
=(LPBYTE
)(dbits
+=dstwidthb
);
671 srcbits
= (LPBYTE
)(sbits
+=srcwidthb
);
676 default: /* ? bit bmp -> 24 bit DIB */
677 FIXME("24 bit DIB %d bit bitmap\n",
678 bmp
->bitmap
.bmBitsPixel
);
684 case 32: /* 32 bpp dstDIB */
686 LPDWORD dstbits
= (LPDWORD
)dbits
;
688 /* FIXME: BI_BITFIELDS not supported yet */
690 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
691 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
693 LPWORD srcbits
= (LPWORD
)sbits
;
696 /* FIXME: BI_BITFIELDS not supported yet */
697 for( y
= 0; y
< lines
; y
++) {
698 for( x
= 0; x
< srcwidth
; x
++ ) {
699 val
= (DWORD
)*srcbits
++;
700 *dstbits
++ = ((val
<< 3) & 0xf8) | ((val
>> 2) & 0x07) |
701 ((val
<< 6) & 0xf800) | ((val
<< 1) & 0x0700) |
702 ((val
<< 9) & 0xf80000) | ((val
<< 4) & 0x070000);
704 dstbits
=(LPDWORD
)(dbits
+=dstwidthb
);
705 srcbits
=(LPWORD
)(sbits
+=srcwidthb
);
710 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
712 LPBYTE srcbits
= sbits
;
714 for( y
= 0; y
< lines
; y
++) {
715 for( x
= 0; x
< srcwidth
; x
++, srcbits
+=3 )
716 *dstbits
++ = ((DWORD
)*srcbits
) & 0x00ffffff;
717 dstbits
=(LPDWORD
)(dbits
+=dstwidthb
);
718 srcbits
=(sbits
+=srcwidthb
);
723 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
725 /* FIXME: BI_BITFIELDS not supported yet */
726 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
727 memcpy(dbits
, sbits
, srcwidthb
);
731 default: /* ? bit bmp -> 16 bit DIB */
732 FIXME("15/16 bit DIB %d bit bitmap\n",
733 bmp
->bitmap
.bmBitsPixel
);
739 default: /* ? bit DIB */
740 FIXME("Unsupported DIB depth %d\n", info
->bmiHeader
.biBitCount
);
744 /* Otherwise, get bits from the XImage */
745 else if(!BITMAP_Driver
->pGetDIBits(bmp
, dc
, startscan
, lines
, bits
, info
, coloruse
, hbitmap
))
747 GDI_ReleaseObj( hdc
);
748 GDI_ReleaseObj( hbitmap
);
753 else if( info
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
) )
755 /* fill in struct members */
757 if( info
->bmiHeader
.biBitCount
== 0)
759 info
->bmiHeader
.biWidth
= bmp
->bitmap
.bmWidth
;
760 info
->bmiHeader
.biHeight
= bmp
->bitmap
.bmHeight
;
761 info
->bmiHeader
.biPlanes
= 1;
762 info
->bmiHeader
.biBitCount
= bmp
->bitmap
.bmBitsPixel
;
763 info
->bmiHeader
.biSizeImage
=
764 DIB_GetDIBImageBytes( bmp
->bitmap
.bmWidth
,
765 bmp
->bitmap
.bmHeight
,
766 bmp
->bitmap
.bmBitsPixel
);
767 info
->bmiHeader
.biCompression
= 0;
771 info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(
772 info
->bmiHeader
.biWidth
,
773 info
->bmiHeader
.biHeight
,
774 info
->bmiHeader
.biBitCount
);
778 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
779 info
->bmiHeader
.biSizeImage
, info
->bmiHeader
.biWidth
,
780 info
->bmiHeader
.biHeight
);
782 GDI_ReleaseObj( hdc
);
783 GDI_ReleaseObj( hbitmap
);
789 /***********************************************************************
790 * CreateDIBitmap16 (GDI.442)
792 HBITMAP16 WINAPI
CreateDIBitmap16( HDC16 hdc
, const BITMAPINFOHEADER
* header
,
793 DWORD init
, LPCVOID bits
, const BITMAPINFO
* data
,
796 return CreateDIBitmap( hdc
, header
, init
, bits
, data
, coloruse
);
800 /***********************************************************************
801 * CreateDIBitmap (GDI32.37)
803 HBITMAP WINAPI
CreateDIBitmap( HDC hdc
, const BITMAPINFOHEADER
*header
,
804 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
814 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
, &compr
) == -1) return 0;
815 if (height
< 0) height
= -height
;
817 /* Check if we should create a monochrome or color bitmap. */
818 /* We create a monochrome bitmap only if it has exactly 2 */
819 /* colors, which are black followed by white, nothing else. */
820 /* In all other cases, we create a color bitmap. */
822 if (bpp
!= 1) fColor
= TRUE
;
823 else if ((coloruse
!= DIB_RGB_COLORS
) ||
824 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
827 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
829 RGBQUAD
*rgb
= data
->bmiColors
;
830 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
832 /* Check if the first color of the colormap is black */
833 if ((col
== RGB(0,0,0)))
836 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
837 /* If the second color is white, create a monochrome bitmap */
838 fColor
= (col
!= RGB(0xff,0xff,0xff));
840 /* Note : If the first color of the colormap is white
841 followed by black, we have to create a color bitmap.
842 If we don't the white will be displayed in black later on!*/
845 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
847 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
848 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
849 if ((col
== RGB(0,0,0)))
852 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
853 fColor
= (col
!= RGB(0xff,0xff,0xff));
859 WARN("(%ld): wrong size for data\n",
860 data
->bmiHeader
.biSize
);
865 /* Now create the bitmap */
869 HDC tmpdc
= CreateDCA( "DISPLAY", NULL
, NULL
, NULL
);
870 handle
= CreateCompatibleBitmap( tmpdc
, width
, height
);
873 else handle
= CreateBitmap( width
, height
, 1, 1, NULL
);
875 if (!handle
) return 0;
877 if (init
== CBM_INIT
)
878 SetDIBits( hdc
, handle
, 0, height
, bits
, data
, coloruse
);
882 /***********************************************************************
883 * CreateDIBSection16 (GDI.489)
885 HBITMAP16 WINAPI
CreateDIBSection16 (HDC16 hdc
, BITMAPINFO
*bmi
, UINT16 usage
,
886 SEGPTR
*bits
, HANDLE section
,
889 HBITMAP16 hbitmap
= 0;
891 BOOL bDesktopDC
= FALSE
;
893 /* If the reference hdc is null, take the desktop dc */
896 hdc
= CreateCompatibleDC(0);
900 if ((dc
= DC_GetDCPtr( hdc
)))
902 hbitmap
= dc
->funcs
->pCreateDIBSection16(dc
, bmi
, usage
, bits
, section
, offset
, 0);
912 /***********************************************************************
913 * DIB_CreateDIBSection
915 HBITMAP
DIB_CreateDIBSection(HDC hdc
, BITMAPINFO
*bmi
, UINT usage
,
916 LPVOID
*bits
, HANDLE section
,
917 DWORD offset
, DWORD ovr_pitch
)
921 BOOL bDesktopDC
= FALSE
;
923 /* If the reference hdc is null, take the desktop dc */
926 hdc
= CreateCompatibleDC(0);
930 if ((dc
= DC_GetDCPtr( hdc
)))
932 hbitmap
= dc
->funcs
->pCreateDIBSection(dc
, bmi
, usage
, bits
, section
, offset
, ovr_pitch
);
942 /***********************************************************************
943 * CreateDIBSection (GDI32.36)
945 HBITMAP WINAPI
CreateDIBSection(HDC hdc
, BITMAPINFO
*bmi
, UINT usage
,
946 LPVOID
*bits
, HANDLE section
,
949 return DIB_CreateDIBSection(hdc
, bmi
, usage
, bits
, section
, offset
, 0);
952 /***********************************************************************
953 * DIB_DeleteDIBSection
955 void DIB_DeleteDIBSection( BITMAPOBJ
*bmp
)
959 DIBSECTION
*dib
= bmp
->dib
;
961 if (dib
->dsBm
.bmBits
)
964 UnmapViewOfFile(dib
->dsBm
.bmBits
);
965 else if (!dib
->dsOffset
)
966 VirtualFree(dib
->dsBm
.bmBits
, 0L, MEM_RELEASE
);
969 BITMAP_Driver
->pDeleteDIBSection(bmp
);
971 HeapFree(GetProcessHeap(), 0, dib
);
976 /***********************************************************************
977 * DIB_CreateDIBFromBitmap
978 * Allocates a packed DIB and copies the bitmap data into it.
980 HGLOBAL
DIB_CreateDIBFromBitmap(HDC hdc
, HBITMAP hBmp
)
982 BITMAPOBJ
*pBmp
= NULL
;
983 HGLOBAL hPackedDIB
= 0;
984 LPBYTE pPackedDIB
= NULL
;
985 LPBITMAPINFOHEADER pbmiHeader
= NULL
;
986 unsigned int width
, height
, depth
, cDataSize
= 0, cPackedSize
= 0,
987 OffsetBits
= 0, nLinesCopied
= 0;
989 /* Get a pointer to the BITMAPOBJ structure */
990 pBmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hBmp
, BITMAP_MAGIC
);
991 if (!pBmp
) return hPackedDIB
;
993 /* Get the bitmap dimensions */
994 width
= pBmp
->bitmap
.bmWidth
;
995 height
= pBmp
->bitmap
.bmHeight
;
996 depth
= pBmp
->bitmap
.bmBitsPixel
;
999 * A packed DIB contains a BITMAPINFO structure followed immediately by
1000 * an optional color palette and the pixel data.
1003 /* Calculate the size of the packed DIB */
1004 cDataSize
= DIB_GetDIBImageBytes( width
, height
, depth
);
1005 cPackedSize
= sizeof(BITMAPINFOHEADER
)
1006 + ( (depth
<= 8) ? (sizeof(RGBQUAD
) * (1 << depth
)) : 0 )
1008 /* Get the offset to the bits */
1009 OffsetBits
= cPackedSize
- cDataSize
;
1011 /* Allocate the packed DIB */
1012 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize
);
1013 hPackedDIB
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
/*| GMEM_ZEROINIT*/,
1017 WARN("Could not allocate packed DIB!\n");
1021 /* A packed DIB starts with a BITMAPINFOHEADER */
1022 pPackedDIB
= (LPBYTE
)GlobalLock(hPackedDIB
);
1023 pbmiHeader
= (LPBITMAPINFOHEADER
)pPackedDIB
;
1025 /* Init the BITMAPINFOHEADER */
1026 pbmiHeader
->biSize
= sizeof(BITMAPINFOHEADER
);
1027 pbmiHeader
->biWidth
= width
;
1028 pbmiHeader
->biHeight
= height
;
1029 pbmiHeader
->biPlanes
= 1;
1030 pbmiHeader
->biBitCount
= depth
;
1031 pbmiHeader
->biCompression
= BI_RGB
;
1032 pbmiHeader
->biSizeImage
= 0;
1033 pbmiHeader
->biXPelsPerMeter
= pbmiHeader
->biYPelsPerMeter
= 0;
1034 pbmiHeader
->biClrUsed
= 0;
1035 pbmiHeader
->biClrImportant
= 0;
1037 /* Retrieve the DIB bits from the bitmap and fill in the
1038 * DIB color table if present */
1040 nLinesCopied
= GetDIBits(hdc
, /* Handle to device context */
1041 hBmp
, /* Handle to bitmap */
1042 0, /* First scan line to set in dest bitmap */
1043 height
, /* Number of scan lines to copy */
1044 pPackedDIB
+ OffsetBits
, /* [out] Address of array for bitmap bits */
1045 (LPBITMAPINFO
) pbmiHeader
, /* [out] Address of BITMAPINFO structure */
1046 0); /* RGB or palette index */
1047 GlobalUnlock(hPackedDIB
);
1049 /* Cleanup if GetDIBits failed */
1050 if (nLinesCopied
!= height
)
1052 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied
, height
);
1053 GlobalFree(hPackedDIB
);
1058 GDI_ReleaseObj( hBmp
);