2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "selectors.h"
29 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(bitmap
);
34 /***********************************************************************
35 * DIB_GetDIBWidthBytes
37 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
38 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
40 int DIB_GetDIBWidthBytes( int width
, int depth
)
46 case 1: words
= (width
+ 31) / 32; break;
47 case 4: words
= (width
+ 7) / 8; break;
48 case 8: words
= (width
+ 3) / 4; break;
50 case 16: words
= (width
+ 1) / 2; break;
51 case 24: words
= (width
* 3 + 3)/4; break;
54 WARN("(%d): Unsupported depth\n", depth
);
62 /***********************************************************************
63 * DIB_GetDIBImageBytes
65 * Return the number of bytes used to hold the image in a DIB bitmap.
67 int DIB_GetDIBImageBytes( int width
, int height
, int depth
)
69 return DIB_GetDIBWidthBytes( width
, depth
) * abs( height
);
73 /***********************************************************************
76 * Return the size of the bitmap info structure including color table.
78 int DIB_BitmapInfoSize( const BITMAPINFO
* info
, WORD coloruse
)
82 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
84 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
85 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
86 return sizeof(BITMAPCOREHEADER
) + colors
*
87 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
89 else /* assume BITMAPINFOHEADER */
91 colors
= info
->bmiHeader
.biClrUsed
;
92 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
93 colors
= 1 << info
->bmiHeader
.biBitCount
;
94 return sizeof(BITMAPINFOHEADER
) + colors
*
95 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
100 /***********************************************************************
103 * Get the info from a bitmap header.
104 * Return 1 for INFOHEADER, 0 for COREHEADER,
105 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
107 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
108 int *height
, WORD
*bpp
, WORD
*compr
)
110 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
112 *width
= header
->biWidth
;
113 *height
= header
->biHeight
;
114 *bpp
= header
->biBitCount
;
115 *compr
= header
->biCompression
;
118 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
120 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
121 *width
= core
->bcWidth
;
122 *height
= core
->bcHeight
;
123 *bpp
= core
->bcBitCount
;
127 if (header
->biSize
== sizeof(BITMAPV4HEADER
))
129 BITMAPV4HEADER
*v4hdr
= (BITMAPV4HEADER
*)header
;
130 *width
= v4hdr
->bV4Width
;
131 *height
= v4hdr
->bV4Height
;
132 *bpp
= v4hdr
->bV4BitCount
;
133 *compr
= v4hdr
->bV4V4Compression
;
136 if (header
->biSize
== sizeof(BITMAPV5HEADER
))
138 BITMAPV5HEADER
*v5hdr
= (BITMAPV5HEADER
*)header
;
139 *width
= v5hdr
->bV5Width
;
140 *height
= v5hdr
->bV5Height
;
141 *bpp
= v5hdr
->bV5BitCount
;
142 *compr
= v5hdr
->bV5Compression
;
145 ERR("(%ld): unknown/wrong size for header\n", header
->biSize
);
150 /***********************************************************************
151 * StretchDIBits (GDI32.@)
153 INT WINAPI
StretchDIBits(HDC hdc
, INT xDst
, INT yDst
, INT widthDst
,
154 INT heightDst
, INT xSrc
, INT ySrc
, INT widthSrc
,
155 INT heightSrc
, const void *bits
,
156 const BITMAPINFO
*info
, UINT wUsage
, DWORD dwRop
)
163 dc
= DC_GetDCUpdate( hdc
);
164 if(!dc
) return FALSE
;
166 if(dc
->funcs
->pStretchDIBits
)
168 heightSrc
= dc
->funcs
->pStretchDIBits(dc
->physDev
, xDst
, yDst
, widthDst
,
169 heightDst
, xSrc
, ySrc
, widthSrc
,
170 heightSrc
, bits
, info
, wUsage
, dwRop
);
171 GDI_ReleaseObj( hdc
);
173 else /* use StretchBlt */
175 HBITMAP hBitmap
, hOldBitmap
;
178 GDI_ReleaseObj( hdc
);
179 hdcMem
= CreateCompatibleDC( hdc
);
180 if (info
->bmiHeader
.biCompression
== BI_RLE4
||
181 info
->bmiHeader
.biCompression
== BI_RLE8
) {
183 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
184 * contain all the rectangle described in bmiHeader, but only part of it.
185 * This mean that those undescribed pixels must be left untouched.
186 * So, we first copy on a memory bitmap the current content of the
187 * destination rectangle, blit the DIB bits on top of it - hence leaving
188 * the gaps untouched -, and blitting the rectangle back.
189 * This insure that gaps are untouched on the destination rectangle
190 * Not doing so leads to trashed images (the gaps contain what was on the
191 * memory bitmap => generally black or garbage)
192 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
193 * another speed vs correctness issue. Anyway, if speed is needed, then the
194 * pStretchDIBits function shall be implemented.
197 hBitmap
= CreateCompatibleBitmap(hdc
, info
->bmiHeader
.biWidth
,
198 info
->bmiHeader
.biHeight
);
199 hOldBitmap
= SelectObject( hdcMem
, hBitmap
);
201 /* copy existing bitmap from destination dc */
202 StretchBlt( hdcMem
, xSrc
, abs(info
->bmiHeader
.biHeight
) - heightSrc
- ySrc
,
203 widthSrc
, heightSrc
, hdc
, xDst
, yDst
, widthDst
, heightDst
,
205 SetDIBits(hdcMem
, hBitmap
, 0, info
->bmiHeader
.biHeight
, bits
,
206 info
, DIB_RGB_COLORS
);
209 hBitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
210 bits
, info
, wUsage
);
211 hOldBitmap
= SelectObject( hdcMem
, hBitmap
);
214 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
215 left (negative biHeight) */
216 StretchBlt( hdc
, xDst
, yDst
, widthDst
, heightDst
,
217 hdcMem
, xSrc
, abs(info
->bmiHeader
.biHeight
) - heightSrc
- ySrc
,
218 widthSrc
, heightSrc
, dwRop
);
219 SelectObject( hdcMem
, hOldBitmap
);
221 DeleteObject( hBitmap
);
227 /******************************************************************************
228 * SetDIBits [GDI32.@] Sets pixels in a bitmap using colors from DIB
231 * hdc [I] Handle to device context
232 * hbitmap [I] Handle to bitmap
233 * startscan [I] Starting scan line
234 * lines [I] Number of scan lines
235 * bits [I] Array of bitmap bits
236 * info [I] Address of structure with data
237 * coloruse [I] Type of color indexes to use
240 * Success: Number of scan lines copied
243 INT WINAPI
SetDIBits( HDC hdc
, HBITMAP hbitmap
, UINT startscan
,
244 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
,
251 if (!(bitmap
= GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
))) return 0;
253 if (!(dc
= DC_GetDCUpdate( hdc
)))
255 if (coloruse
== DIB_RGB_COLORS
) FIXME( "shouldn't require a DC for DIB_RGB_COLORS\n" );
256 GDI_ReleaseObj( hbitmap
);
260 if (!bitmap
->funcs
&& !BITMAP_SetOwnerDC( hbitmap
, dc
)) goto done
;
262 if (bitmap
->funcs
&& bitmap
->funcs
->pSetDIBits
)
263 result
= bitmap
->funcs
->pSetDIBits( dc
->physDev
, hbitmap
, startscan
, lines
,
264 bits
, info
, coloruse
);
269 GDI_ReleaseObj( hdc
);
270 GDI_ReleaseObj( hbitmap
);
275 /***********************************************************************
276 * SetDIBitsToDevice (GDI32.@)
278 INT WINAPI
SetDIBitsToDevice(HDC hdc
, INT xDest
, INT yDest
, DWORD cx
,
279 DWORD cy
, INT xSrc
, INT ySrc
, UINT startscan
,
280 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
,
286 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
288 if(dc
->funcs
->pSetDIBitsToDevice
)
289 ret
= dc
->funcs
->pSetDIBitsToDevice( dc
->physDev
, xDest
, yDest
, cx
, cy
, xSrc
,
290 ySrc
, startscan
, lines
, bits
,
293 FIXME("unimplemented on hdc %p\n", hdc
);
297 GDI_ReleaseObj( hdc
);
301 /***********************************************************************
302 * SetDIBColorTable (GDI32.@)
304 UINT WINAPI
SetDIBColorTable( HDC hdc
, UINT startpos
, UINT entries
, RGBQUAD
*colors
)
309 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
311 if (dc
->funcs
->pSetDIBColorTable
)
312 result
= dc
->funcs
->pSetDIBColorTable(dc
->physDev
, startpos
, entries
, colors
);
314 GDI_ReleaseObj( hdc
);
319 /***********************************************************************
320 * GetDIBColorTable (GDI32.@)
322 UINT WINAPI
GetDIBColorTable( HDC hdc
, UINT startpos
, UINT entries
, RGBQUAD
*colors
)
327 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
329 if (dc
->funcs
->pGetDIBColorTable
)
330 result
= dc
->funcs
->pGetDIBColorTable(dc
->physDev
, startpos
, entries
, colors
);
332 GDI_ReleaseObj( hdc
);
336 /* FIXME the following two structs should be combined with __sysPalTemplate in
337 objects/color.c - this should happen after de-X11-ing both of these
339 NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
342 static RGBQUAD EGAColors
[16] = {
343 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
344 { 0x00, 0x00, 0x00, 0x00 },
345 { 0x00, 0x00, 0x80, 0x00 },
346 { 0x00, 0x80, 0x00, 0x00 },
347 { 0x00, 0x80, 0x80, 0x00 },
348 { 0x80, 0x00, 0x00, 0x00 },
349 { 0x80, 0x00, 0x80, 0x00 },
350 { 0x80, 0x80, 0x00, 0x00 },
351 { 0x80, 0x80, 0x80, 0x00 },
352 { 0xc0, 0xc0, 0xc0, 0x00 },
353 { 0x00, 0x00, 0xff, 0x00 },
354 { 0x00, 0xff, 0x00, 0x00 },
355 { 0x00, 0xff, 0xff, 0x00 },
356 { 0xff, 0x00, 0x00, 0x00 },
357 { 0xff, 0x00, 0xff, 0x00 },
358 { 0xff, 0xff, 0x00, 0x00 },
359 { 0xff, 0xff, 0xff, 0x00 }
363 static RGBQUAD DefLogPalette
[20] = { /* Copy of Default Logical Palette */
364 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
365 { 0x00, 0x00, 0x00, 0x00 },
366 { 0x00, 0x00, 0x80, 0x00 },
367 { 0x00, 0x80, 0x00, 0x00 },
368 { 0x00, 0x80, 0x80, 0x00 },
369 { 0x80, 0x00, 0x00, 0x00 },
370 { 0x80, 0x00, 0x80, 0x00 },
371 { 0x80, 0x80, 0x00, 0x00 },
372 { 0xc0, 0xc0, 0xc0, 0x00 },
373 { 0xc0, 0xdc, 0xc0, 0x00 },
374 { 0xf0, 0xca, 0xa6, 0x00 },
375 { 0xf0, 0xfb, 0xff, 0x00 },
376 { 0xa4, 0xa0, 0xa0, 0x00 },
377 { 0x80, 0x80, 0x80, 0x00 },
378 { 0x00, 0x00, 0xf0, 0x00 },
379 { 0x00, 0xff, 0x00, 0x00 },
380 { 0x00, 0xff, 0xff, 0x00 },
381 { 0xff, 0x00, 0x00, 0x00 },
382 { 0xff, 0x00, 0xff, 0x00 },
383 { 0xff, 0xff, 0x00, 0x00 },
384 { 0xff, 0xff, 0xff, 0x00 }
388 /******************************************************************************
389 * GetDIBits [GDI32.@] Retrieves bits of bitmap and copies to buffer
392 * Success: Number of scan lines copied from bitmap
395 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
397 INT WINAPI
GetDIBits(
398 HDC hdc
, /* [in] Handle to device context */
399 HBITMAP hbitmap
, /* [in] Handle to bitmap */
400 UINT startscan
, /* [in] First scan line to set in dest bitmap */
401 UINT lines
, /* [in] Number of scan lines to copy */
402 LPVOID bits
, /* [out] Address of array for bitmap bits */
403 BITMAPINFO
* info
, /* [out] Address of structure with bitmap data */
404 UINT coloruse
) /* [in] RGB or palette index */
408 PALETTEENTRY
* palEntry
;
409 PALETTEOBJ
* palette
;
413 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
414 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
416 GDI_ReleaseObj( hdc
);
419 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->hPalette
, PALETTE_MAGIC
)))
421 GDI_ReleaseObj( hdc
);
422 GDI_ReleaseObj( hbitmap
);
426 /* Transfer color info */
428 if (info
->bmiHeader
.biBitCount
<= 8 && info
->bmiHeader
.biBitCount
> 0 ) {
430 info
->bmiHeader
.biClrUsed
= 0;
432 if(info
->bmiHeader
.biBitCount
>= bmp
->bitmap
.bmBitsPixel
) {
433 palEntry
= palette
->logpalette
.palPalEntry
;
434 for (i
= 0; i
< (1 << bmp
->bitmap
.bmBitsPixel
); i
++, palEntry
++) {
435 if (coloruse
== DIB_RGB_COLORS
) {
436 info
->bmiColors
[i
].rgbRed
= palEntry
->peRed
;
437 info
->bmiColors
[i
].rgbGreen
= palEntry
->peGreen
;
438 info
->bmiColors
[i
].rgbBlue
= palEntry
->peBlue
;
439 info
->bmiColors
[i
].rgbReserved
= 0;
441 else ((WORD
*)info
->bmiColors
)[i
] = (WORD
)i
;
444 switch (info
->bmiHeader
.biBitCount
) {
446 info
->bmiColors
[0].rgbRed
= info
->bmiColors
[0].rgbGreen
=
447 info
->bmiColors
[0].rgbBlue
= 0;
448 info
->bmiColors
[0].rgbReserved
= 0;
449 info
->bmiColors
[1].rgbRed
= info
->bmiColors
[1].rgbGreen
=
450 info
->bmiColors
[1].rgbBlue
= 0xff;
451 info
->bmiColors
[1].rgbReserved
= 0;
455 memcpy(info
->bmiColors
, EGAColors
, sizeof(EGAColors
));
463 memcpy(info
->bmiColors
, DefLogPalette
,
464 10 * sizeof(RGBQUAD
));
465 memcpy(info
->bmiColors
+ 246, DefLogPalette
+ 10,
466 10 * sizeof(RGBQUAD
));
467 color
= info
->bmiColors
+ 10;
468 for(r
= 0; r
<= 5; r
++) /* FIXME */
469 for(g
= 0; g
<= 5; g
++)
470 for(b
= 0; b
<= 5; b
++) {
471 color
->rgbRed
= (r
* 0xff) / 5;
472 color
->rgbGreen
= (g
* 0xff) / 5;
473 color
->rgbBlue
= (b
* 0xff) / 5;
474 color
->rgbReserved
= 0;
482 GDI_ReleaseObj( dc
->hPalette
);
486 /* If the bitmap object already have a dib section that contains image data, get the bits from it */
487 if(bmp
->dib
&& bmp
->dib
->dsBm
.bmBitsPixel
>= 15 && info
->bmiHeader
.biBitCount
>= 15)
489 /*FIXME: Only RGB dibs supported for now */
490 unsigned int srcwidth
= bmp
->dib
->dsBm
.bmWidth
, srcwidthb
= bmp
->dib
->dsBm
.bmWidthBytes
;
491 int dstwidthb
= DIB_GetDIBWidthBytes( info
->bmiHeader
.biWidth
, info
->bmiHeader
.biBitCount
);
492 LPBYTE dbits
= bits
, sbits
= (LPBYTE
) bmp
->dib
->dsBm
.bmBits
+ (startscan
* srcwidthb
);
495 if ((info
->bmiHeader
.biHeight
< 0) ^ (bmp
->dib
->dsBmih
.biHeight
< 0))
497 dbits
= (LPBYTE
)bits
+ (dstwidthb
* (lines
-1));
498 dstwidthb
= -dstwidthb
;
501 switch( info
->bmiHeader
.biBitCount
) {
504 case 16: /* 16 bpp dstDIB */
506 LPWORD dstbits
= (LPWORD
)dbits
;
507 WORD rmask
= 0x7c00, gmask
= 0x03e0, bmask
= 0x001f;
509 /* FIXME: BI_BITFIELDS not supported yet */
511 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
513 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
515 /* FIXME: BI_BITFIELDS not supported yet */
516 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
517 memcpy(dbits
, sbits
, srcwidthb
);
521 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
523 LPBYTE srcbits
= sbits
;
525 for( y
= 0; y
< lines
; y
++) {
526 for( x
= 0; x
< srcwidth
; x
++, srcbits
+= 3)
527 *dstbits
++ = ((srcbits
[0] >> 3) & bmask
) |
528 (((WORD
)srcbits
[1] << 2) & gmask
) |
529 (((WORD
)srcbits
[2] << 7) & rmask
);
531 dstbits
= (LPWORD
)(dbits
+=dstwidthb
);
532 srcbits
= (sbits
+= srcwidthb
);
537 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
539 LPDWORD srcbits
= (LPDWORD
)sbits
;
542 for( y
= 0; y
< lines
; y
++) {
543 for( x
= 0; x
< srcwidth
; x
++ ) {
545 *dstbits
++ = (WORD
)(((val
>> 3) & bmask
) | ((val
>> 6) & gmask
) |
546 ((val
>> 9) & rmask
));
548 dstbits
= (LPWORD
)(dbits
+=dstwidthb
);
549 srcbits
= (LPDWORD
)(sbits
+=srcwidthb
);
554 default: /* ? bit bmp -> 16 bit DIB */
555 FIXME("15/16 bit DIB %d bit bitmap\n",
556 bmp
->bitmap
.bmBitsPixel
);
562 case 24: /* 24 bpp dstDIB */
564 LPBYTE dstbits
= dbits
;
566 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
568 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
570 LPWORD srcbits
= (LPWORD
)sbits
;
573 /* FIXME: BI_BITFIELDS not supported yet */
574 for( y
= 0; y
< lines
; y
++) {
575 for( x
= 0; x
< srcwidth
; x
++ ) {
577 *dstbits
++ = (BYTE
)(((val
<< 3) & 0xf8) | ((val
>> 2) & 0x07));
578 *dstbits
++ = (BYTE
)(((val
>> 2) & 0xf8) | ((val
>> 7) & 0x07));
579 *dstbits
++ = (BYTE
)(((val
>> 7) & 0xf8) | ((val
>> 12) & 0x07));
581 dstbits
= (LPBYTE
)(dbits
+=dstwidthb
);
582 srcbits
= (LPWORD
)(sbits
+=srcwidthb
);
587 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
589 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
590 memcpy(dbits
, sbits
, srcwidthb
);
594 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
596 LPBYTE srcbits
= (LPBYTE
)sbits
;
598 for( y
= 0; y
< lines
; y
++) {
599 for( x
= 0; x
< srcwidth
; x
++, srcbits
++ ) {
600 *dstbits
++ = *srcbits
++;
601 *dstbits
++ = *srcbits
++;
602 *dstbits
++ = *srcbits
++;
604 dstbits
=(LPBYTE
)(dbits
+=dstwidthb
);
605 srcbits
= (LPBYTE
)(sbits
+=srcwidthb
);
610 default: /* ? bit bmp -> 24 bit DIB */
611 FIXME("24 bit DIB %d bit bitmap\n",
612 bmp
->bitmap
.bmBitsPixel
);
618 case 32: /* 32 bpp dstDIB */
620 LPDWORD dstbits
= (LPDWORD
)dbits
;
622 /* FIXME: BI_BITFIELDS not supported yet */
624 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
625 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
627 LPWORD srcbits
= (LPWORD
)sbits
;
630 /* FIXME: BI_BITFIELDS not supported yet */
631 for( y
= 0; y
< lines
; y
++) {
632 for( x
= 0; x
< srcwidth
; x
++ ) {
633 val
= (DWORD
)*srcbits
++;
634 *dstbits
++ = ((val
<< 3) & 0xf8) | ((val
>> 2) & 0x07) |
635 ((val
<< 6) & 0xf800) | ((val
<< 1) & 0x0700) |
636 ((val
<< 9) & 0xf80000) | ((val
<< 4) & 0x070000);
638 dstbits
=(LPDWORD
)(dbits
+=dstwidthb
);
639 srcbits
=(LPWORD
)(sbits
+=srcwidthb
);
644 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
646 LPBYTE srcbits
= sbits
;
648 for( y
= 0; y
< lines
; y
++) {
649 for( x
= 0; x
< srcwidth
; x
++, srcbits
+=3 )
650 *dstbits
++ = ((DWORD
)*srcbits
) & 0x00ffffff;
651 dstbits
=(LPDWORD
)(dbits
+=dstwidthb
);
652 srcbits
=(sbits
+=srcwidthb
);
657 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
659 /* FIXME: BI_BITFIELDS not supported yet */
660 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
661 memcpy(dbits
, sbits
, srcwidthb
);
665 default: /* ? bit bmp -> 16 bit DIB */
666 FIXME("15/16 bit DIB %d bit bitmap\n",
667 bmp
->bitmap
.bmBitsPixel
);
673 default: /* ? bit DIB */
674 FIXME("Unsupported DIB depth %d\n", info
->bmiHeader
.biBitCount
);
678 /* Otherwise, get bits from the XImage */
681 if (!bmp
->funcs
&& !BITMAP_SetOwnerDC( hbitmap
, dc
)) lines
= 0;
684 if (bmp
->funcs
&& bmp
->funcs
->pGetDIBits
)
685 lines
= bmp
->funcs
->pGetDIBits( dc
->physDev
, hbitmap
, startscan
,
686 lines
, bits
, info
, coloruse
);
688 lines
= 0; /* FIXME: should copy from bmp->bitmap.bmBits */
692 else if( info
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
) )
694 /* fill in struct members */
696 if( info
->bmiHeader
.biBitCount
== 0)
698 info
->bmiHeader
.biWidth
= bmp
->bitmap
.bmWidth
;
699 info
->bmiHeader
.biHeight
= bmp
->bitmap
.bmHeight
;
700 info
->bmiHeader
.biPlanes
= 1;
701 info
->bmiHeader
.biBitCount
= bmp
->bitmap
.bmBitsPixel
;
702 info
->bmiHeader
.biSizeImage
=
703 DIB_GetDIBImageBytes( bmp
->bitmap
.bmWidth
,
704 bmp
->bitmap
.bmHeight
,
705 bmp
->bitmap
.bmBitsPixel
);
706 info
->bmiHeader
.biCompression
= 0;
710 info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(
711 info
->bmiHeader
.biWidth
,
712 info
->bmiHeader
.biHeight
,
713 info
->bmiHeader
.biBitCount
);
717 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
718 info
->bmiHeader
.biSizeImage
, info
->bmiHeader
.biWidth
,
719 info
->bmiHeader
.biHeight
);
721 GDI_ReleaseObj( hdc
);
722 GDI_ReleaseObj( hbitmap
);
728 /***********************************************************************
729 * CreateDIBitmap (GDI32.@)
731 HBITMAP WINAPI
CreateDIBitmap( HDC hdc
, const BITMAPINFOHEADER
*header
,
732 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
743 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
, &compr
) == -1) return 0;
744 if (height
< 0) height
= -height
;
746 /* Check if we should create a monochrome or color bitmap. */
747 /* We create a monochrome bitmap only if it has exactly 2 */
748 /* colors, which are black followed by white, nothing else. */
749 /* In all other cases, we create a color bitmap. */
751 if (bpp
!= 1) fColor
= TRUE
;
752 else if ((coloruse
!= DIB_RGB_COLORS
) ||
753 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
756 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
758 RGBQUAD
*rgb
= data
->bmiColors
;
759 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
761 /* Check if the first color of the colormap is black */
762 if ((col
== RGB(0,0,0)))
765 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
766 /* If the second color is white, create a monochrome bitmap */
767 fColor
= (col
!= RGB(0xff,0xff,0xff));
769 /* Note : If the first color of the colormap is white
770 followed by black, we have to create a color bitmap.
771 If we don't the white will be displayed in black later on!*/
774 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
776 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
777 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
778 if ((col
== RGB(0,0,0)))
781 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
782 fColor
= (col
!= RGB(0xff,0xff,0xff));
786 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPV4HEADER
))
787 { /* FIXME: correct ? */
788 RGBQUAD
*rgb
= data
->bmiColors
;
789 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
791 /* Check if the first color of the colormap is black */
792 if ((col
== RGB(0,0,0)))
795 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
796 /* If the second color is white, create a monochrome bitmap */
797 fColor
= (col
!= RGB(0xff,0xff,0xff));
799 /* Note : If the first color of the colormap is white
800 followed by black, we have to create a color bitmap.
801 If we don't the white will be displayed in black later on!*/
806 ERR("(%ld): wrong/unknown size for data\n",
807 data
->bmiHeader
.biSize
);
812 /* Now create the bitmap */
814 if (!(dc
= DC_GetDCPtr( hdc
))) return 0;
817 handle
= CreateBitmap( width
, height
, GetDeviceCaps( hdc
, PLANES
),
818 GetDeviceCaps( hdc
, BITSPIXEL
), NULL
);
819 else handle
= CreateBitmap( width
, height
, 1, 1, NULL
);
823 if (init
== CBM_INIT
) SetDIBits( hdc
, handle
, 0, height
, bits
, data
, coloruse
);
824 else if (!BITMAP_SetOwnerDC( handle
, dc
))
826 DeleteObject( handle
);
831 GDI_ReleaseObj( hdc
);
835 /***********************************************************************
836 * CreateDIBSection (GDI.489)
838 HBITMAP16 WINAPI
CreateDIBSection16 (HDC16 hdc
, BITMAPINFO
*bmi
, UINT16 usage
,
839 SEGPTR
*bits16
, HANDLE section
, DWORD offset
)
844 hbitmap
= CreateDIBSection( HDC_32(hdc
), bmi
, usage
, &bits32
, section
, offset
);
847 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr(hbitmap
, BITMAP_MAGIC
);
848 if (bmp
&& bmp
->dib
&& bits32
)
850 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
851 INT height
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
852 INT width_bytes
= DIB_GetDIBWidthBytes(bi
->biWidth
, bi
->biBitCount
);
853 INT size
= (bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
) ?
854 bi
->biSizeImage
: width_bytes
* height
;
856 /* calculate number of sel's needed for size with 64K steps */
857 WORD count
= (size
+ 0xffff) / 0x10000;
858 WORD sel
= AllocSelectorArray16(count
);
861 for (i
= 0; i
< count
; i
++)
863 SetSelectorBase(sel
+ (i
<< __AHSHIFT
), (DWORD
)bits32
+ i
* 0x10000);
864 SetSelectorLimit16(sel
+ (i
<< __AHSHIFT
), size
- 1); /* yep, limit is correct */
867 bmp
->segptr_bits
= MAKESEGPTR( sel
, 0 );
868 if (bits16
) *bits16
= bmp
->segptr_bits
;
870 if (bmp
) GDI_ReleaseObj( hbitmap
);
872 return HBITMAP_16(hbitmap
);
875 /***********************************************************************
876 * DIB_CreateDIBSection
878 HBITMAP
DIB_CreateDIBSection(HDC hdc
, BITMAPINFO
*bmi
, UINT usage
,
879 LPVOID
*bits
, HANDLE section
,
880 DWORD offset
, DWORD ovr_pitch
)
884 BOOL bDesktopDC
= FALSE
;
886 /* If the reference hdc is null, take the desktop dc */
889 hdc
= CreateCompatibleDC(0);
893 if ((dc
= DC_GetDCPtr( hdc
)))
895 hbitmap
= dc
->funcs
->pCreateDIBSection(dc
->physDev
, bmi
, usage
, bits
, section
, offset
, ovr_pitch
);
905 /***********************************************************************
906 * CreateDIBSection (GDI32.@)
908 HBITMAP WINAPI
CreateDIBSection(HDC hdc
, BITMAPINFO
*bmi
, UINT usage
,
909 LPVOID
*bits
, HANDLE section
,
912 return DIB_CreateDIBSection(hdc
, bmi
, usage
, bits
, section
, offset
, 0);
915 /***********************************************************************
916 * DIB_CreateDIBFromBitmap
917 * Allocates a packed DIB and copies the bitmap data into it.
919 HGLOBAL
DIB_CreateDIBFromBitmap(HDC hdc
, HBITMAP hBmp
)
921 BITMAPOBJ
*pBmp
= NULL
;
922 HGLOBAL hPackedDIB
= 0;
923 LPBYTE pPackedDIB
= NULL
;
924 LPBITMAPINFOHEADER pbmiHeader
= NULL
;
925 unsigned int width
, height
, depth
, cDataSize
= 0, cPackedSize
= 0,
926 OffsetBits
= 0, nLinesCopied
= 0;
928 /* Get a pointer to the BITMAPOBJ structure */
929 pBmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hBmp
, BITMAP_MAGIC
);
930 if (!pBmp
) return hPackedDIB
;
932 /* Get the bitmap dimensions */
933 width
= pBmp
->bitmap
.bmWidth
;
934 height
= pBmp
->bitmap
.bmHeight
;
935 depth
= pBmp
->bitmap
.bmBitsPixel
;
938 * A packed DIB contains a BITMAPINFO structure followed immediately by
939 * an optional color palette and the pixel data.
942 /* Calculate the size of the packed DIB */
943 cDataSize
= DIB_GetDIBImageBytes( width
, height
, depth
);
944 cPackedSize
= sizeof(BITMAPINFOHEADER
)
945 + ( (depth
<= 8) ? (sizeof(RGBQUAD
) * (1 << depth
)) : 0 )
947 /* Get the offset to the bits */
948 OffsetBits
= cPackedSize
- cDataSize
;
950 /* Allocate the packed DIB */
951 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize
);
952 hPackedDIB
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_DDESHARE
/*| GMEM_ZEROINIT*/,
956 WARN("Could not allocate packed DIB!\n");
960 /* A packed DIB starts with a BITMAPINFOHEADER */
961 pPackedDIB
= (LPBYTE
)GlobalLock(hPackedDIB
);
962 pbmiHeader
= (LPBITMAPINFOHEADER
)pPackedDIB
;
964 /* Init the BITMAPINFOHEADER */
965 pbmiHeader
->biSize
= sizeof(BITMAPINFOHEADER
);
966 pbmiHeader
->biWidth
= width
;
967 pbmiHeader
->biHeight
= height
;
968 pbmiHeader
->biPlanes
= 1;
969 pbmiHeader
->biBitCount
= depth
;
970 pbmiHeader
->biCompression
= BI_RGB
;
971 pbmiHeader
->biSizeImage
= 0;
972 pbmiHeader
->biXPelsPerMeter
= pbmiHeader
->biYPelsPerMeter
= 0;
973 pbmiHeader
->biClrUsed
= 0;
974 pbmiHeader
->biClrImportant
= 0;
976 /* Retrieve the DIB bits from the bitmap and fill in the
977 * DIB color table if present */
979 nLinesCopied
= GetDIBits(hdc
, /* Handle to device context */
980 hBmp
, /* Handle to bitmap */
981 0, /* First scan line to set in dest bitmap */
982 height
, /* Number of scan lines to copy */
983 pPackedDIB
+ OffsetBits
, /* [out] Address of array for bitmap bits */
984 (LPBITMAPINFO
) pbmiHeader
, /* [out] Address of BITMAPINFO structure */
985 0); /* RGB or palette index */
986 GlobalUnlock(hPackedDIB
);
988 /* Cleanup if GetDIBits failed */
989 if (nLinesCopied
!= height
)
991 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied
, height
);
992 GlobalFree(hPackedDIB
);
997 GDI_ReleaseObj( hBmp
);