2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
14 #include "selectors.h"
16 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(bitmap
);
21 /***********************************************************************
22 * DIB_GetDIBWidthBytes
24 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
25 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
27 int DIB_GetDIBWidthBytes( int width
, int depth
)
33 case 1: words
= (width
+ 31) / 32; break;
34 case 4: words
= (width
+ 7) / 8; break;
35 case 8: words
= (width
+ 3) / 4; break;
37 case 16: words
= (width
+ 1) / 2; break;
38 case 24: words
= (width
* 3 + 3)/4; break;
41 WARN("(%d): Unsupported depth\n", depth
);
49 /***********************************************************************
50 * DIB_GetDIBImageBytes
52 * Return the number of bytes used to hold the image in a DIB bitmap.
54 int DIB_GetDIBImageBytes( int width
, int height
, int depth
)
56 return DIB_GetDIBWidthBytes( width
, depth
) * abs( height
);
60 /***********************************************************************
63 * Return the size of the bitmap info structure including color table.
65 int DIB_BitmapInfoSize( const BITMAPINFO
* info
, WORD coloruse
)
69 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
71 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
72 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
73 return sizeof(BITMAPCOREHEADER
) + colors
*
74 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
76 else /* assume BITMAPINFOHEADER */
78 colors
= info
->bmiHeader
.biClrUsed
;
79 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
80 colors
= 1 << info
->bmiHeader
.biBitCount
;
81 return sizeof(BITMAPINFOHEADER
) + colors
*
82 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
87 /***********************************************************************
90 * Get the info from a bitmap header.
91 * Return 1 for INFOHEADER, 0 for COREHEADER,
92 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
94 int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
95 int *height
, WORD
*bpp
, WORD
*compr
)
97 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
99 *width
= header
->biWidth
;
100 *height
= header
->biHeight
;
101 *bpp
= header
->biBitCount
;
102 *compr
= header
->biCompression
;
105 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
107 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
108 *width
= core
->bcWidth
;
109 *height
= core
->bcHeight
;
110 *bpp
= core
->bcBitCount
;
114 if (header
->biSize
== sizeof(BITMAPV4HEADER
))
116 BITMAPV4HEADER
*v4hdr
= (BITMAPV4HEADER
*)header
;
117 *width
= v4hdr
->bV4Width
;
118 *height
= v4hdr
->bV4Height
;
119 *bpp
= v4hdr
->bV4BitCount
;
120 *compr
= v4hdr
->bV4Compression
;
123 if (header
->biSize
== sizeof(BITMAPV5HEADER
))
125 BITMAPV5HEADER
*v5hdr
= (BITMAPV5HEADER
*)header
;
126 *width
= v5hdr
->bV5Width
;
127 *height
= v5hdr
->bV5Height
;
128 *bpp
= v5hdr
->bV5BitCount
;
129 *compr
= v5hdr
->bV5Compression
;
132 ERR("(%ld): unknown/wrong size for header\n", header
->biSize
);
137 /***********************************************************************
138 * StretchDIBits (GDI.439)
140 INT16 WINAPI
StretchDIBits16(HDC16 hdc
, INT16 xDst
, INT16 yDst
, INT16 widthDst
,
141 INT16 heightDst
, INT16 xSrc
, INT16 ySrc
, INT16 widthSrc
,
142 INT16 heightSrc
, const VOID
*bits
,
143 const BITMAPINFO
*info
, UINT16 wUsage
, DWORD dwRop
)
145 return (INT16
)StretchDIBits( hdc
, xDst
, yDst
, widthDst
, heightDst
,
146 xSrc
, ySrc
, widthSrc
, heightSrc
, bits
,
147 info
, wUsage
, dwRop
);
151 /***********************************************************************
152 * StretchDIBits (GDI32.@)
154 INT WINAPI
StretchDIBits(HDC hdc
, INT xDst
, INT yDst
, INT widthDst
,
155 INT heightDst
, INT xSrc
, INT ySrc
, INT widthSrc
,
156 INT heightSrc
, const void *bits
,
157 const BITMAPINFO
*info
, UINT wUsage
, DWORD dwRop
)
159 DC
*dc
= DC_GetDCUpdate( hdc
);
160 if(!dc
) return FALSE
;
162 if(dc
->funcs
->pStretchDIBits
)
164 heightSrc
= dc
->funcs
->pStretchDIBits(dc
, xDst
, yDst
, widthDst
,
165 heightDst
, xSrc
, ySrc
, widthSrc
,
166 heightSrc
, bits
, info
, wUsage
, dwRop
);
167 GDI_ReleaseObj( hdc
);
169 else /* use StretchBlt */
171 HBITMAP hBitmap
, hOldBitmap
;
174 GDI_ReleaseObj( hdc
);
175 hdcMem
= CreateCompatibleDC( hdc
);
176 if (info
->bmiHeader
.biCompression
== BI_RLE4
||
177 info
->bmiHeader
.biCompression
== BI_RLE8
) {
179 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
180 * contain all the rectangle described in bmiHeader, but only part of it.
181 * This mean that those undescribed pixels must be left untouched.
182 * So, we first copy on a memory bitmap the current content of the
183 * destination rectangle, blit the DIB bits on top of it - hence leaving
184 * the gaps untouched -, and blitting the rectangle back.
185 * This insure that gaps are untouched on the destination rectangle
186 * Not doing so leads to trashed images (the gaps contain what was on the
187 * memory bitmap => generally black or garbage)
188 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
189 * another speed vs correctness issue. Anyway, if speed is needed, then the
190 * pStretchDIBits function shall be implemented.
193 hBitmap
= CreateCompatibleBitmap(hdc
, info
->bmiHeader
.biWidth
,
194 info
->bmiHeader
.biHeight
);
195 hOldBitmap
= SelectObject( hdcMem
, hBitmap
);
197 /* copy existing bitmap from destination dc */
198 StretchBlt( hdcMem
, xSrc
, abs(info
->bmiHeader
.biHeight
) - heightSrc
- ySrc
,
199 widthSrc
, heightSrc
, hdc
, xDst
, yDst
, widthDst
, heightDst
,
201 SetDIBits(hdcMem
, hBitmap
, 0, info
->bmiHeader
.biHeight
, bits
,
202 info
, DIB_RGB_COLORS
);
205 hBitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
206 bits
, info
, wUsage
);
207 hOldBitmap
= SelectObject( hdcMem
, hBitmap
);
210 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
211 left (negative biHeight) */
212 StretchBlt( hdc
, xDst
, yDst
, widthDst
, heightDst
,
213 hdcMem
, xSrc
, abs(info
->bmiHeader
.biHeight
) - heightSrc
- ySrc
,
214 widthSrc
, heightSrc
, dwRop
);
215 SelectObject( hdcMem
, hOldBitmap
);
217 DeleteObject( hBitmap
);
223 /***********************************************************************
224 * SetDIBits (GDI.440)
226 INT16 WINAPI
SetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
227 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
230 return SetDIBits( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
234 /******************************************************************************
235 * SetDIBits [GDI32.@] Sets pixels in a bitmap using colors from DIB
238 * hdc [I] Handle to device context
239 * hbitmap [I] Handle to bitmap
240 * startscan [I] Starting scan line
241 * lines [I] Number of scan lines
242 * bits [I] Array of bitmap bits
243 * info [I] Address of structure with data
244 * coloruse [I] Type of color indexes to use
247 * Success: Number of scan lines copied
250 INT WINAPI
SetDIBits( HDC hdc
, HBITMAP hbitmap
, UINT startscan
,
251 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
,
258 /* Check parameters */
259 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
261 if (!(bitmap
= (BITMAPOBJ
*) GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
263 GDI_ReleaseObj( hdc
);
267 result
= BITMAP_Driver
->pSetDIBits(bitmap
, dc
, startscan
,
271 GDI_ReleaseObj( hbitmap
);
272 GDI_ReleaseObj( hdc
);
278 /***********************************************************************
279 * SetDIBitsToDevice (GDI.443)
281 INT16 WINAPI
SetDIBitsToDevice16(HDC16 hdc
, INT16 xDest
, INT16 yDest
, INT16 cx
,
282 INT16 cy
, INT16 xSrc
, INT16 ySrc
, UINT16 startscan
,
283 UINT16 lines
, LPCVOID bits
, const BITMAPINFO
*info
,
286 return SetDIBitsToDevice( hdc
, xDest
, yDest
, cx
, cy
, xSrc
, ySrc
,
287 startscan
, lines
, bits
, info
, coloruse
);
291 /***********************************************************************
292 * SetDIBitsToDevice (GDI32.@)
294 INT WINAPI
SetDIBitsToDevice(HDC hdc
, INT xDest
, INT yDest
, DWORD cx
,
295 DWORD cy
, INT xSrc
, INT ySrc
, UINT startscan
,
296 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
,
302 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
304 if(dc
->funcs
->pSetDIBitsToDevice
)
305 ret
= dc
->funcs
->pSetDIBitsToDevice( dc
, xDest
, yDest
, cx
, cy
, xSrc
,
306 ySrc
, startscan
, lines
, bits
,
309 FIXME("unimplemented on hdc %08x\n", hdc
);
313 GDI_ReleaseObj( hdc
);
317 /***********************************************************************
318 * SetDIBColorTable (GDI.602)
320 UINT16 WINAPI
SetDIBColorTable16( HDC16 hdc
, UINT16 startpos
, UINT16 entries
,
323 return SetDIBColorTable( hdc
, startpos
, entries
, colors
);
326 /***********************************************************************
327 * SetDIBColorTable (GDI32.@)
329 UINT WINAPI
SetDIBColorTable( HDC hdc
, UINT startpos
, UINT entries
,
336 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
338 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( dc
->hBitmap
, BITMAP_MAGIC
)))
340 GDI_ReleaseObj( hdc
);
344 result
= BITMAP_Driver
->pSetDIBColorTable(bmp
, dc
, startpos
, entries
, colors
);
346 GDI_ReleaseObj( dc
->hBitmap
);
347 GDI_ReleaseObj( hdc
);
351 /***********************************************************************
352 * GetDIBColorTable (GDI.603)
354 UINT16 WINAPI
GetDIBColorTable16( HDC16 hdc
, UINT16 startpos
, UINT16 entries
,
357 return GetDIBColorTable( hdc
, startpos
, entries
, colors
);
360 /***********************************************************************
361 * GetDIBColorTable (GDI32.@)
363 UINT WINAPI
GetDIBColorTable( HDC hdc
, UINT startpos
, UINT entries
,
370 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
372 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( dc
->hBitmap
, BITMAP_MAGIC
)))
374 GDI_ReleaseObj( hdc
);
378 result
= BITMAP_Driver
->pGetDIBColorTable(bmp
, dc
, startpos
, entries
, colors
);
380 GDI_ReleaseObj( dc
->hBitmap
);
381 GDI_ReleaseObj( hdc
);
385 /* FIXME the following two structs should be combined with __sysPalTemplate in
386 objects/color.c - this should happen after de-X11-ing both of these
388 NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
391 static RGBQUAD EGAColors
[16] = {
392 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
393 { 0x00, 0x00, 0x00, 0x00 },
394 { 0x00, 0x00, 0x80, 0x00 },
395 { 0x00, 0x80, 0x00, 0x00 },
396 { 0x00, 0x80, 0x80, 0x00 },
397 { 0x80, 0x00, 0x00, 0x00 },
398 { 0x80, 0x00, 0x80, 0x00 },
399 { 0x80, 0x80, 0x00, 0x00 },
400 { 0x80, 0x80, 0x80, 0x00 },
401 { 0xc0, 0xc0, 0xc0, 0x00 },
402 { 0x00, 0x00, 0xff, 0x00 },
403 { 0x00, 0xff, 0x00, 0x00 },
404 { 0x00, 0xff, 0xff, 0x00 },
405 { 0xff, 0x00, 0x00, 0x00 },
406 { 0xff, 0x00, 0xff, 0x00 },
407 { 0xff, 0xff, 0x00, 0x00 },
408 { 0xff, 0xff, 0xff, 0x00 }
412 static RGBQUAD DefLogPalette
[20] = { /* Copy of Default Logical Palette */
413 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
414 { 0x00, 0x00, 0x00, 0x00 },
415 { 0x00, 0x00, 0x80, 0x00 },
416 { 0x00, 0x80, 0x00, 0x00 },
417 { 0x00, 0x80, 0x80, 0x00 },
418 { 0x80, 0x00, 0x00, 0x00 },
419 { 0x80, 0x00, 0x80, 0x00 },
420 { 0x80, 0x80, 0x00, 0x00 },
421 { 0xc0, 0xc0, 0xc0, 0x00 },
422 { 0xc0, 0xdc, 0xc0, 0x00 },
423 { 0xf0, 0xca, 0xa6, 0x00 },
424 { 0xf0, 0xfb, 0xff, 0x00 },
425 { 0xa4, 0xa0, 0xa0, 0x00 },
426 { 0x80, 0x80, 0x80, 0x00 },
427 { 0x00, 0x00, 0xf0, 0x00 },
428 { 0x00, 0xff, 0x00, 0x00 },
429 { 0x00, 0xff, 0xff, 0x00 },
430 { 0xff, 0x00, 0x00, 0x00 },
431 { 0xff, 0x00, 0xff, 0x00 },
432 { 0xff, 0xff, 0x00, 0x00 },
433 { 0xff, 0xff, 0xff, 0x00 }
436 /***********************************************************************
437 * GetDIBits (GDI.441)
439 INT16 WINAPI
GetDIBits16( HDC16 hdc
, HBITMAP16 hbitmap
, UINT16 startscan
,
440 UINT16 lines
, LPVOID bits
, BITMAPINFO
* info
,
443 return GetDIBits( hdc
, hbitmap
, startscan
, lines
, bits
, info
, coloruse
);
447 /******************************************************************************
448 * GetDIBits [GDI32.@] Retrieves bits of bitmap and copies to buffer
451 * Success: Number of scan lines copied from bitmap
454 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
456 INT WINAPI
GetDIBits(
457 HDC hdc
, /* [in] Handle to device context */
458 HBITMAP hbitmap
, /* [in] Handle to bitmap */
459 UINT startscan
, /* [in] First scan line to set in dest bitmap */
460 UINT lines
, /* [in] Number of scan lines to copy */
461 LPVOID bits
, /* [out] Address of array for bitmap bits */
462 BITMAPINFO
* info
, /* [out] Address of structure with bitmap data */
463 UINT coloruse
) /* [in] RGB or palette index */
467 PALETTEENTRY
* palEntry
;
468 PALETTEOBJ
* palette
;
472 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
473 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
475 GDI_ReleaseObj( hdc
);
478 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->hPalette
, PALETTE_MAGIC
)))
480 GDI_ReleaseObj( hdc
);
481 GDI_ReleaseObj( hbitmap
);
485 /* Transfer color info */
487 if (info
->bmiHeader
.biBitCount
<= 8 && info
->bmiHeader
.biBitCount
> 0 ) {
489 info
->bmiHeader
.biClrUsed
= 0;
491 if(info
->bmiHeader
.biBitCount
>= bmp
->bitmap
.bmBitsPixel
) {
492 palEntry
= palette
->logpalette
.palPalEntry
;
493 for (i
= 0; i
< (1 << bmp
->bitmap
.bmBitsPixel
); i
++, palEntry
++) {
494 if (coloruse
== DIB_RGB_COLORS
) {
495 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
496 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
497 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
498 info
->bmiColors
[i
].rgbReserved
= 0;
500 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
503 switch (info
->bmiHeader
.biBitCount
) {
505 info
->bmiColors
[0].rgbRed
= info
->bmiColors
[0].rgbGreen
=
506 info
->bmiColors
[0].rgbBlue
= 0;
507 info
->bmiColors
[0].rgbReserved
= 0;
508 info
->bmiColors
[1].rgbRed
= info
->bmiColors
[1].rgbGreen
=
509 info
->bmiColors
[1].rgbBlue
= 0xff;
510 info
->bmiColors
[1].rgbReserved
= 0;
514 memcpy(info
->bmiColors
, EGAColors
, sizeof(EGAColors
));
522 memcpy(info
->bmiColors
, DefLogPalette
,
523 10 * sizeof(RGBQUAD
));
524 memcpy(info
->bmiColors
+ 246, DefLogPalette
+ 10,
525 10 * sizeof(RGBQUAD
));
526 color
= info
->bmiColors
+ 10;
527 for(r
= 0; r
<= 5; r
++) /* FIXME */
528 for(g
= 0; g
<= 5; g
++)
529 for(b
= 0; b
<= 5; b
++) {
530 color
->rgbRed
= (r
* 0xff) / 5;
531 color
->rgbGreen
= (g
* 0xff) / 5;
532 color
->rgbBlue
= (b
* 0xff) / 5;
533 color
->rgbReserved
= 0;
541 GDI_ReleaseObj( dc
->hPalette
);
545 /* If the bitmap object already have a dib section that contains image data, get the bits from it */
546 if(bmp
->dib
&& bmp
->dib
->dsBm
.bmBitsPixel
>= 15 && info
->bmiHeader
.biBitCount
>= 15)
548 /*FIXME: Only RGB dibs supported for now */
549 unsigned int srcwidth
= bmp
->dib
->dsBm
.bmWidth
, srcwidthb
= bmp
->dib
->dsBm
.bmWidthBytes
;
550 int dstwidthb
= DIB_GetDIBWidthBytes( info
->bmiHeader
.biWidth
, info
->bmiHeader
.biBitCount
);
551 LPBYTE dbits
= bits
, sbits
= (LPBYTE
) bmp
->dib
->dsBm
.bmBits
+ (startscan
* srcwidthb
);
554 if ((info
->bmiHeader
.biHeight
< 0) ^ (bmp
->dib
->dsBmih
.biHeight
< 0))
556 dbits
= (LPBYTE
)bits
+ (dstwidthb
* (lines
-1));
557 dstwidthb
= -dstwidthb
;
560 switch( info
->bmiHeader
.biBitCount
) {
563 case 16: /* 16 bpp dstDIB */
565 LPWORD dstbits
= (LPWORD
)dbits
;
566 WORD rmask
= 0x7c00, gmask
= 0x03e0, bmask
= 0x001f;
568 /* FIXME: BI_BITFIELDS not supported yet */
570 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
572 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
574 /* FIXME: BI_BITFIELDS not supported yet */
575 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
576 memcpy(dbits
, sbits
, srcwidthb
);
580 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
582 LPBYTE srcbits
= sbits
;
584 for( y
= 0; y
< lines
; y
++) {
585 for( x
= 0; x
< srcwidth
; x
++, srcbits
+= 3)
586 *dstbits
++ = ((srcbits
[0] >> 3) & bmask
) |
587 (((WORD
)srcbits
[1] << 2) & gmask
) |
588 (((WORD
)srcbits
[2] << 7) & rmask
);
590 dstbits
= (LPWORD
)(dbits
+=dstwidthb
);
591 srcbits
= (sbits
+= srcwidthb
);
596 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
598 LPDWORD srcbits
= (LPDWORD
)sbits
;
601 for( y
= 0; y
< lines
; y
++) {
602 for( x
= 0; x
< srcwidth
; x
++ ) {
604 *dstbits
++ = (WORD
)(((val
>> 3) & bmask
) | ((val
>> 6) & gmask
) |
605 ((val
>> 9) & rmask
));
607 dstbits
= (LPWORD
)(dbits
+=dstwidthb
);
608 srcbits
= (LPDWORD
)(sbits
+=srcwidthb
);
613 default: /* ? bit bmp -> 16 bit DIB */
614 FIXME("15/16 bit DIB %d bit bitmap\n",
615 bmp
->bitmap
.bmBitsPixel
);
621 case 24: /* 24 bpp dstDIB */
623 LPBYTE dstbits
= dbits
;
625 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
627 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
629 LPWORD srcbits
= (LPWORD
)sbits
;
632 /* FIXME: BI_BITFIELDS not supported yet */
633 for( y
= 0; y
< lines
; y
++) {
634 for( x
= 0; x
< srcwidth
; x
++ ) {
636 *dstbits
++ = (BYTE
)(((val
<< 3) & 0xf8) | ((val
>> 2) & 0x07));
637 *dstbits
++ = (BYTE
)(((val
>> 2) & 0xf8) | ((val
>> 7) & 0x07));
638 *dstbits
++ = (BYTE
)(((val
>> 7) & 0xf8) | ((val
>> 12) & 0x07));
640 dstbits
= (LPBYTE
)(dbits
+=dstwidthb
);
641 srcbits
= (LPWORD
)(sbits
+=srcwidthb
);
646 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
648 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
649 memcpy(dbits
, sbits
, srcwidthb
);
653 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
655 LPBYTE srcbits
= (LPBYTE
)sbits
;
657 for( y
= 0; y
< lines
; y
++) {
658 for( x
= 0; x
< srcwidth
; x
++, srcbits
++ ) {
659 *dstbits
++ = *srcbits
++;
660 *dstbits
++ = *srcbits
++;
661 *dstbits
++ = *srcbits
++;
663 dstbits
=(LPBYTE
)(dbits
+=dstwidthb
);
664 srcbits
= (LPBYTE
)(sbits
+=srcwidthb
);
669 default: /* ? bit bmp -> 24 bit DIB */
670 FIXME("24 bit DIB %d bit bitmap\n",
671 bmp
->bitmap
.bmBitsPixel
);
677 case 32: /* 32 bpp dstDIB */
679 LPDWORD dstbits
= (LPDWORD
)dbits
;
681 /* FIXME: BI_BITFIELDS not supported yet */
683 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
684 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
686 LPWORD srcbits
= (LPWORD
)sbits
;
689 /* FIXME: BI_BITFIELDS not supported yet */
690 for( y
= 0; y
< lines
; y
++) {
691 for( x
= 0; x
< srcwidth
; x
++ ) {
692 val
= (DWORD
)*srcbits
++;
693 *dstbits
++ = ((val
<< 3) & 0xf8) | ((val
>> 2) & 0x07) |
694 ((val
<< 6) & 0xf800) | ((val
<< 1) & 0x0700) |
695 ((val
<< 9) & 0xf80000) | ((val
<< 4) & 0x070000);
697 dstbits
=(LPDWORD
)(dbits
+=dstwidthb
);
698 srcbits
=(LPWORD
)(sbits
+=srcwidthb
);
703 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
705 LPBYTE srcbits
= sbits
;
707 for( y
= 0; y
< lines
; y
++) {
708 for( x
= 0; x
< srcwidth
; x
++, srcbits
+=3 )
709 *dstbits
++ = ((DWORD
)*srcbits
) & 0x00ffffff;
710 dstbits
=(LPDWORD
)(dbits
+=dstwidthb
);
711 srcbits
=(sbits
+=srcwidthb
);
716 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
718 /* FIXME: BI_BITFIELDS not supported yet */
719 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
720 memcpy(dbits
, sbits
, srcwidthb
);
724 default: /* ? bit bmp -> 16 bit DIB */
725 FIXME("15/16 bit DIB %d bit bitmap\n",
726 bmp
->bitmap
.bmBitsPixel
);
732 default: /* ? bit DIB */
733 FIXME("Unsupported DIB depth %d\n", info
->bmiHeader
.biBitCount
);
737 /* Otherwise, get bits from the XImage */
738 else if(!BITMAP_Driver
->pGetDIBits(bmp
, dc
, startscan
, lines
, bits
, info
, coloruse
, hbitmap
))
740 GDI_ReleaseObj( hdc
);
741 GDI_ReleaseObj( hbitmap
);
746 else if( info
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
) )
748 /* fill in struct members */
750 if( info
->bmiHeader
.biBitCount
== 0)
752 info
->bmiHeader
.biWidth
= bmp
->bitmap
.bmWidth
;
753 info
->bmiHeader
.biHeight
= bmp
->bitmap
.bmHeight
;
754 info
->bmiHeader
.biPlanes
= 1;
755 info
->bmiHeader
.biBitCount
= bmp
->bitmap
.bmBitsPixel
;
756 info
->bmiHeader
.biSizeImage
=
757 DIB_GetDIBImageBytes( bmp
->bitmap
.bmWidth
,
758 bmp
->bitmap
.bmHeight
,
759 bmp
->bitmap
.bmBitsPixel
);
760 info
->bmiHeader
.biCompression
= 0;
764 info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(
765 info
->bmiHeader
.biWidth
,
766 info
->bmiHeader
.biHeight
,
767 info
->bmiHeader
.biBitCount
);
771 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
772 info
->bmiHeader
.biSizeImage
, info
->bmiHeader
.biWidth
,
773 info
->bmiHeader
.biHeight
);
775 GDI_ReleaseObj( hdc
);
776 GDI_ReleaseObj( hbitmap
);
782 /***********************************************************************
783 * CreateDIBitmap (GDI.442)
785 HBITMAP16 WINAPI
CreateDIBitmap16( HDC16 hdc
, const BITMAPINFOHEADER
* header
,
786 DWORD init
, LPCVOID bits
, const BITMAPINFO
* data
,
789 return CreateDIBitmap( hdc
, header
, init
, bits
, data
, coloruse
);
793 /***********************************************************************
794 * CreateDIBitmap (GDI32.@)
796 HBITMAP WINAPI
CreateDIBitmap( HDC hdc
, const BITMAPINFOHEADER
*header
,
797 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
807 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
, &compr
) == -1) return 0;
808 if (height
< 0) height
= -height
;
810 /* Check if we should create a monochrome or color bitmap. */
811 /* We create a monochrome bitmap only if it has exactly 2 */
812 /* colors, which are black followed by white, nothing else. */
813 /* In all other cases, we create a color bitmap. */
815 if (bpp
!= 1) fColor
= TRUE
;
816 else if ((coloruse
!= DIB_RGB_COLORS
) ||
817 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
820 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
822 RGBQUAD
*rgb
= data
->bmiColors
;
823 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
825 /* Check if the first color of the colormap is black */
826 if ((col
== RGB(0,0,0)))
829 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
830 /* If the second color is white, create a monochrome bitmap */
831 fColor
= (col
!= RGB(0xff,0xff,0xff));
833 /* Note : If the first color of the colormap is white
834 followed by black, we have to create a color bitmap.
835 If we don't the white will be displayed in black later on!*/
838 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
840 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
841 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
842 if ((col
== RGB(0,0,0)))
845 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
846 fColor
= (col
!= RGB(0xff,0xff,0xff));
850 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPV4HEADER
))
851 { /* FIXME: correct ? */
852 RGBQUAD
*rgb
= data
->bmiColors
;
853 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
855 /* Check if the first color of the colormap is black */
856 if ((col
== RGB(0,0,0)))
859 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
860 /* If the second color is white, create a monochrome bitmap */
861 fColor
= (col
!= RGB(0xff,0xff,0xff));
863 /* Note : If the first color of the colormap is white
864 followed by black, we have to create a color bitmap.
865 If we don't the white will be displayed in black later on!*/
870 ERR("(%ld): wrong/unknown size for data\n",
871 data
->bmiHeader
.biSize
);
876 /* Now create the bitmap */
880 HDC tmpdc
= CreateDCA( "DISPLAY", NULL
, NULL
, NULL
);
881 handle
= CreateCompatibleBitmap( tmpdc
, width
, height
);
884 else handle
= CreateBitmap( width
, height
, 1, 1, NULL
);
886 if (!handle
) return 0;
888 if (init
== CBM_INIT
)
889 SetDIBits( hdc
, handle
, 0, height
, bits
, data
, coloruse
);
893 /***********************************************************************
894 * CreateDIBSection (GDI.489)
896 HBITMAP16 WINAPI
CreateDIBSection16 (HDC16 hdc
, BITMAPINFO
*bmi
, UINT16 usage
,
897 SEGPTR
*bits16
, HANDLE section
, DWORD offset
)
902 hbitmap
= CreateDIBSection( hdc
, bmi
, usage
, &bits32
, section
, offset
);
905 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr(hbitmap
, BITMAP_MAGIC
);
906 if (bmp
&& bmp
->dib
&& bits32
)
908 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
909 INT height
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
910 INT width_bytes
= DIB_GetDIBWidthBytes(bi
->biWidth
, bi
->biBitCount
);
911 INT size
= (bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
) ?
912 bi
->biSizeImage
: width_bytes
* height
;
914 WORD sel
= SELECTOR_AllocBlock( bits32
, size
, WINE_LDT_FLAGS_DATA
);
915 bmp
->segptr_bits
= MAKESEGPTR( sel
, 0 );
916 if (bits16
) *bits16
= bmp
->segptr_bits
;
918 if (bmp
) GDI_ReleaseObj( hbitmap
);
923 /***********************************************************************
924 * DIB_CreateDIBSection
926 HBITMAP
DIB_CreateDIBSection(HDC hdc
, BITMAPINFO
*bmi
, UINT usage
,
927 LPVOID
*bits
, HANDLE section
,
928 DWORD offset
, DWORD ovr_pitch
)
932 BOOL bDesktopDC
= FALSE
;
934 /* If the reference hdc is null, take the desktop dc */
937 hdc
= CreateCompatibleDC(0);
941 if ((dc
= DC_GetDCPtr( hdc
)))
943 hbitmap
= dc
->funcs
->pCreateDIBSection(dc
, bmi
, usage
, bits
, section
, offset
, ovr_pitch
);
953 /***********************************************************************
954 * CreateDIBSection (GDI32.@)
956 HBITMAP WINAPI
CreateDIBSection(HDC hdc
, BITMAPINFO
*bmi
, UINT usage
,
957 LPVOID
*bits
, HANDLE section
,
960 return DIB_CreateDIBSection(hdc
, bmi
, usage
, bits
, section
, offset
, 0);
963 /***********************************************************************
964 * DIB_DeleteDIBSection
966 void DIB_DeleteDIBSection( BITMAPOBJ
*bmp
)
970 DIBSECTION
*dib
= bmp
->dib
;
972 if (dib
->dsBm
.bmBits
)
976 SYSTEM_INFO SystemInfo
;
977 GetSystemInfo( &SystemInfo
);
978 UnmapViewOfFile( (char *)dib
->dsBm
.bmBits
-
979 (dib
->dsOffset
% SystemInfo
.dwAllocationGranularity
) );
981 else if (!dib
->dsOffset
)
982 VirtualFree(dib
->dsBm
.bmBits
, 0L, MEM_RELEASE
);
985 BITMAP_Driver
->pDeleteDIBSection(bmp
);
987 HeapFree(GetProcessHeap(), 0, dib
);
989 if (bmp
->segptr_bits
) SELECTOR_FreeBlock( SELECTOROF(bmp
->segptr_bits
) );
993 /***********************************************************************
994 * DIB_CreateDIBFromBitmap
995 * Allocates a packed DIB and copies the bitmap data into it.
997 HGLOBAL
DIB_CreateDIBFromBitmap(HDC hdc
, HBITMAP hBmp
)
999 BITMAPOBJ
*pBmp
= NULL
;
1000 HGLOBAL hPackedDIB
= 0;
1001 LPBYTE pPackedDIB
= NULL
;
1002 LPBITMAPINFOHEADER pbmiHeader
= NULL
;
1003 unsigned int width
, height
, depth
, cDataSize
= 0, cPackedSize
= 0,
1004 OffsetBits
= 0, nLinesCopied
= 0;
1006 /* Get a pointer to the BITMAPOBJ structure */
1007 pBmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hBmp
, BITMAP_MAGIC
);
1008 if (!pBmp
) return hPackedDIB
;
1010 /* Get the bitmap dimensions */
1011 width
= pBmp
->bitmap
.bmWidth
;
1012 height
= pBmp
->bitmap
.bmHeight
;
1013 depth
= pBmp
->bitmap
.bmBitsPixel
;
1016 * A packed DIB contains a BITMAPINFO structure followed immediately by
1017 * an optional color palette and the pixel data.
1020 /* Calculate the size of the packed DIB */
1021 cDataSize
= DIB_GetDIBImageBytes( width
, height
, depth
);
1022 cPackedSize
= sizeof(BITMAPINFOHEADER
)
1023 + ( (depth
<= 8) ? (sizeof(RGBQUAD
) * (1 << depth
)) : 0 )
1025 /* Get the offset to the bits */
1026 OffsetBits
= cPackedSize
- cDataSize
;
1028 /* Allocate the packed DIB */
1029 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize
);
1030 hPackedDIB
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
/*| GMEM_ZEROINIT*/,
1034 WARN("Could not allocate packed DIB!\n");
1038 /* A packed DIB starts with a BITMAPINFOHEADER */
1039 pPackedDIB
= (LPBYTE
)GlobalLock(hPackedDIB
);
1040 pbmiHeader
= (LPBITMAPINFOHEADER
)pPackedDIB
;
1042 /* Init the BITMAPINFOHEADER */
1043 pbmiHeader
->biSize
= sizeof(BITMAPINFOHEADER
);
1044 pbmiHeader
->biWidth
= width
;
1045 pbmiHeader
->biHeight
= height
;
1046 pbmiHeader
->biPlanes
= 1;
1047 pbmiHeader
->biBitCount
= depth
;
1048 pbmiHeader
->biCompression
= BI_RGB
;
1049 pbmiHeader
->biSizeImage
= 0;
1050 pbmiHeader
->biXPelsPerMeter
= pbmiHeader
->biYPelsPerMeter
= 0;
1051 pbmiHeader
->biClrUsed
= 0;
1052 pbmiHeader
->biClrImportant
= 0;
1054 /* Retrieve the DIB bits from the bitmap and fill in the
1055 * DIB color table if present */
1057 nLinesCopied
= GetDIBits(hdc
, /* Handle to device context */
1058 hBmp
, /* Handle to bitmap */
1059 0, /* First scan line to set in dest bitmap */
1060 height
, /* Number of scan lines to copy */
1061 pPackedDIB
+ OffsetBits
, /* [out] Address of array for bitmap bits */
1062 (LPBITMAPINFO
) pbmiHeader
, /* [out] Address of BITMAPINFO structure */
1063 0); /* RGB or palette index */
1064 GlobalUnlock(hPackedDIB
);
1066 /* Cleanup if GetDIBits failed */
1067 if (nLinesCopied
!= height
)
1069 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied
, height
);
1070 GlobalFree(hPackedDIB
);
1075 GDI_ReleaseObj( hBmp
);