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
22 Important information:
24 * Current Windows versions support two different DIB structures:
26 - BITMAPCOREINFO / BITMAPCOREHEADER (legacy structures; used in OS/2)
27 - BITMAPINFO / BITMAPINFOHEADER
29 Most Windows API functions taking a BITMAPINFO* / BITMAPINFOHEADER* also
30 accept the old "core" structures, and so must WINE.
31 You can distinguish them by looking at the first member (bcSize/biSize),
32 or use the internal function DIB_GetBitmapInfo.
35 * The palettes are stored in different formats:
37 - BITMAPCOREINFO: Array of RGBTRIPLE
38 - BITMAPINFO: Array of RGBQUAD
41 * There are even more DIB headers, but they all extend BITMAPINFOHEADER:
43 - BITMAPV4HEADER: Introduced in Windows 95 / NT 4.0
44 - BITMAPV5HEADER: Introduced in Windows 98 / 2000
46 If biCompression is BI_BITFIELDS, the color masks are at the same position
47 in all the headers (they start at bmiColors of BITMAPINFOHEADER), because
48 the new headers have structure members for the masks.
51 * You should never access the color table using the bmiColors member,
52 because the passed structure may have one of the extended headers
53 mentioned above. Use this to calculate the location:
56 void* colorPtr = (LPBYTE) info + (WORD) info->bmiHeader.biSize;
60 Search for "Bitmap Structures" in MSDN
71 #include "gdi_private.h"
72 #include "wine/debug.h"
74 WINE_DEFAULT_DEBUG_CHANNEL(bitmap
);
78 Some of the following helper functions are duplicated in
82 /***********************************************************************
83 * DIB_GetDIBWidthBytes
85 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
86 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_87eb.asp
88 int DIB_GetDIBWidthBytes( int width
, int depth
)
94 case 1: words
= (width
+ 31) / 32; break;
95 case 4: words
= (width
+ 7) / 8; break;
96 case 8: words
= (width
+ 3) / 4; break;
98 case 16: words
= (width
+ 1) / 2; break;
99 case 24: words
= (width
* 3 + 3)/4; break;
102 WARN("(%d): Unsupported depth\n", depth
);
110 /***********************************************************************
111 * DIB_GetDIBImageBytes
113 * Return the number of bytes used to hold the image in a DIB bitmap.
115 int DIB_GetDIBImageBytes( int width
, int height
, int depth
)
117 return DIB_GetDIBWidthBytes( width
, depth
) * abs( height
);
121 /***********************************************************************
124 * Return the size of the bitmap info structure including color table.
126 int DIB_BitmapInfoSize( const BITMAPINFO
* info
, WORD coloruse
)
130 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
132 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
133 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
134 return sizeof(BITMAPCOREHEADER
) + colors
*
135 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
137 else /* assume BITMAPINFOHEADER */
139 colors
= info
->bmiHeader
.biClrUsed
;
140 if (colors
> 256) colors
= 256;
141 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
142 colors
= 1 << info
->bmiHeader
.biBitCount
;
143 return sizeof(BITMAPINFOHEADER
) + colors
*
144 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
149 /***********************************************************************
152 * Get the info from a bitmap header.
153 * Return 1 for INFOHEADER, 0 for COREHEADER,
154 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
156 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
157 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
159 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
161 *width
= header
->biWidth
;
162 *height
= header
->biHeight
;
163 *planes
= header
->biPlanes
;
164 *bpp
= header
->biBitCount
;
165 *compr
= header
->biCompression
;
166 *size
= header
->biSizeImage
;
169 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
171 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
172 *width
= core
->bcWidth
;
173 *height
= core
->bcHeight
;
174 *planes
= core
->bcPlanes
;
175 *bpp
= core
->bcBitCount
;
180 if (header
->biSize
== sizeof(BITMAPV4HEADER
))
182 BITMAPV4HEADER
*v4hdr
= (BITMAPV4HEADER
*)header
;
183 *width
= v4hdr
->bV4Width
;
184 *height
= v4hdr
->bV4Height
;
185 *planes
= v4hdr
->bV4Planes
;
186 *bpp
= v4hdr
->bV4BitCount
;
187 *compr
= v4hdr
->bV4V4Compression
;
188 *size
= v4hdr
->bV4SizeImage
;
191 if (header
->biSize
== sizeof(BITMAPV5HEADER
))
193 BITMAPV5HEADER
*v5hdr
= (BITMAPV5HEADER
*)header
;
194 *width
= v5hdr
->bV5Width
;
195 *height
= v5hdr
->bV5Height
;
196 *planes
= v5hdr
->bV5Planes
;
197 *bpp
= v5hdr
->bV5BitCount
;
198 *compr
= v5hdr
->bV5Compression
;
199 *size
= v5hdr
->bV5SizeImage
;
202 ERR("(%ld): unknown/wrong size for header\n", header
->biSize
);
207 /***********************************************************************
208 * StretchDIBits (GDI32.@)
210 INT WINAPI
StretchDIBits(HDC hdc
, INT xDst
, INT yDst
, INT widthDst
,
211 INT heightDst
, INT xSrc
, INT ySrc
, INT widthSrc
,
212 INT heightSrc
, const void *bits
,
213 const BITMAPINFO
*info
, UINT wUsage
, DWORD dwRop
)
220 dc
= DC_GetDCUpdate( hdc
);
221 if(!dc
) return FALSE
;
223 if(dc
->funcs
->pStretchDIBits
)
225 heightSrc
= dc
->funcs
->pStretchDIBits(dc
->physDev
, xDst
, yDst
, widthDst
,
226 heightDst
, xSrc
, ySrc
, widthSrc
,
227 heightSrc
, bits
, info
, wUsage
, dwRop
);
228 GDI_ReleaseObj( hdc
);
230 else /* use StretchBlt */
232 HBITMAP hBitmap
, hOldBitmap
;
233 HPALETTE hpal
= NULL
;
240 GDI_ReleaseObj( hdc
);
242 if (DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
, &planes
, &bpp
, &compr
, &size
) == -1)
244 ERR("Invalid bitmap\n");
250 ERR("Bitmap has a negative width\n");
254 hdcMem
= CreateCompatibleDC( hdc
);
255 hBitmap
= CreateCompatibleBitmap(hdc
, width
, height
);
256 hOldBitmap
= SelectObject( hdcMem
, hBitmap
);
257 if(wUsage
== DIB_PAL_COLORS
)
259 hpal
= GetCurrentObject(hdc
, OBJ_PAL
);
260 hpal
= SelectPalette(hdcMem
, hpal
, FALSE
);
263 if (info
->bmiHeader
.biCompression
== BI_RLE4
||
264 info
->bmiHeader
.biCompression
== BI_RLE8
) {
266 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
267 * contain all the rectangle described in bmiHeader, but only part of it.
268 * This mean that those undescribed pixels must be left untouched.
269 * So, we first copy on a memory bitmap the current content of the
270 * destination rectangle, blit the DIB bits on top of it - hence leaving
271 * the gaps untouched -, and blitting the rectangle back.
272 * This insure that gaps are untouched on the destination rectangle
273 * Not doing so leads to trashed images (the gaps contain what was on the
274 * memory bitmap => generally black or garbage)
275 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
276 * another speed vs correctness issue. Anyway, if speed is needed, then the
277 * pStretchDIBits function shall be implemented.
281 /* copy existing bitmap from destination dc */
282 StretchBlt( hdcMem
, xSrc
, abs(height
) - heightSrc
- ySrc
,
283 widthSrc
, heightSrc
, hdc
, xDst
, yDst
, widthDst
, heightDst
,
287 SetDIBits(hdcMem
, hBitmap
, 0, height
, bits
, info
, wUsage
);
289 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
290 left (negative biHeight) */
291 StretchBlt( hdc
, xDst
, yDst
, widthDst
, heightDst
,
292 hdcMem
, xSrc
, abs(height
) - heightSrc
- ySrc
,
293 widthSrc
, heightSrc
, dwRop
);
295 SelectPalette(hdcMem
, hpal
, FALSE
);
296 SelectObject( hdcMem
, hOldBitmap
);
298 DeleteObject( hBitmap
);
304 /******************************************************************************
305 * SetDIBits [GDI32.@]
307 * Sets pixels in a bitmap using colors from DIB.
310 * hdc [I] Handle to device context
311 * hbitmap [I] Handle to bitmap
312 * startscan [I] Starting scan line
313 * lines [I] Number of scan lines
314 * bits [I] Array of bitmap bits
315 * info [I] Address of structure with data
316 * coloruse [I] Type of color indexes to use
319 * Success: Number of scan lines copied
322 INT WINAPI
SetDIBits( HDC hdc
, HBITMAP hbitmap
, UINT startscan
,
323 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
,
330 if (!(dc
= DC_GetDCUpdate( hdc
)))
332 if (coloruse
== DIB_RGB_COLORS
) FIXME( "shouldn't require a DC for DIB_RGB_COLORS\n" );
336 if (!(bitmap
= GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
338 GDI_ReleaseObj( hdc
);
342 if (!bitmap
->funcs
&& !BITMAP_SetOwnerDC( hbitmap
, dc
)) goto done
;
344 if (bitmap
->funcs
&& bitmap
->funcs
->pSetDIBits
)
345 result
= bitmap
->funcs
->pSetDIBits( dc
->physDev
, hbitmap
, startscan
, lines
,
346 bits
, info
, coloruse
);
351 GDI_ReleaseObj( hbitmap
);
352 GDI_ReleaseObj( hdc
);
357 /***********************************************************************
358 * SetDIBitsToDevice (GDI32.@)
360 INT WINAPI
SetDIBitsToDevice(HDC hdc
, INT xDest
, INT yDest
, DWORD cx
,
361 DWORD cy
, INT xSrc
, INT ySrc
, UINT startscan
,
362 UINT lines
, LPCVOID bits
, const BITMAPINFO
*info
,
368 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
370 if(dc
->funcs
->pSetDIBitsToDevice
)
371 ret
= dc
->funcs
->pSetDIBitsToDevice( dc
->physDev
, xDest
, yDest
, cx
, cy
, xSrc
,
372 ySrc
, startscan
, lines
, bits
,
375 FIXME("unimplemented on hdc %p\n", hdc
);
379 GDI_ReleaseObj( hdc
);
383 /***********************************************************************
384 * SetDIBColorTable (GDI32.@)
386 UINT WINAPI
SetDIBColorTable( HDC hdc
, UINT startpos
, UINT entries
, CONST RGBQUAD
*colors
)
391 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
393 if (dc
->funcs
->pSetDIBColorTable
)
394 result
= dc
->funcs
->pSetDIBColorTable(dc
->physDev
, startpos
, entries
, colors
);
396 GDI_ReleaseObj( hdc
);
401 /***********************************************************************
402 * GetDIBColorTable (GDI32.@)
404 UINT WINAPI
GetDIBColorTable( HDC hdc
, UINT startpos
, UINT entries
, RGBQUAD
*colors
)
409 if (!(dc
= DC_GetDCUpdate( hdc
))) return 0;
411 if (dc
->funcs
->pGetDIBColorTable
)
412 result
= dc
->funcs
->pGetDIBColorTable(dc
->physDev
, startpos
, entries
, colors
);
414 GDI_ReleaseObj( hdc
);
418 /* FIXME the following two structs should be combined with __sysPalTemplate in
419 objects/color.c - this should happen after de-X11-ing both of these
421 NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
424 static RGBQUAD EGAColorsQuads
[16] = {
425 /* rgbBlue, rgbGreen, rgbRed, rgbReserved */
426 { 0x00, 0x00, 0x00, 0x00 },
427 { 0x00, 0x00, 0x80, 0x00 },
428 { 0x00, 0x80, 0x00, 0x00 },
429 { 0x00, 0x80, 0x80, 0x00 },
430 { 0x80, 0x00, 0x00, 0x00 },
431 { 0x80, 0x00, 0x80, 0x00 },
432 { 0x80, 0x80, 0x00, 0x00 },
433 { 0x80, 0x80, 0x80, 0x00 },
434 { 0xc0, 0xc0, 0xc0, 0x00 },
435 { 0x00, 0x00, 0xff, 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 static RGBTRIPLE EGAColorsTriples
[16] = {
445 /* rgbBlue, rgbGreen, rgbRed */
446 { 0x00, 0x00, 0x00 },
447 { 0x00, 0x00, 0x80 },
448 { 0x00, 0x80, 0x00 },
449 { 0x00, 0x80, 0x80 },
450 { 0x80, 0x00, 0x00 },
451 { 0x80, 0x00, 0x80 },
452 { 0x80, 0x80, 0x00 },
453 { 0x80, 0x80, 0x80 },
454 { 0xc0, 0xc0, 0xc0 },
455 { 0x00, 0x00, 0xff },
456 { 0x00, 0xff, 0x00 },
457 { 0x00, 0xff, 0xff },
458 { 0xff, 0x00, 0x00 } ,
459 { 0xff, 0x00, 0xff },
460 { 0xff, 0xff, 0x00 },
464 static RGBQUAD DefLogPaletteQuads
[20] = { /* Copy of Default Logical Palette */
465 /* rgbBlue, rgbGreen, rgbRed, rgbReserved */
466 { 0x00, 0x00, 0x00, 0x00 },
467 { 0x00, 0x00, 0x80, 0x00 },
468 { 0x00, 0x80, 0x00, 0x00 },
469 { 0x00, 0x80, 0x80, 0x00 },
470 { 0x80, 0x00, 0x00, 0x00 },
471 { 0x80, 0x00, 0x80, 0x00 },
472 { 0x80, 0x80, 0x00, 0x00 },
473 { 0xc0, 0xc0, 0xc0, 0x00 },
474 { 0xc0, 0xdc, 0xc0, 0x00 },
475 { 0xf0, 0xca, 0xa6, 0x00 },
476 { 0xf0, 0xfb, 0xff, 0x00 },
477 { 0xa4, 0xa0, 0xa0, 0x00 },
478 { 0x80, 0x80, 0x80, 0x00 },
479 { 0x00, 0x00, 0xf0, 0x00 },
480 { 0x00, 0xff, 0x00, 0x00 },
481 { 0x00, 0xff, 0xff, 0x00 },
482 { 0xff, 0x00, 0x00, 0x00 },
483 { 0xff, 0x00, 0xff, 0x00 },
484 { 0xff, 0xff, 0x00, 0x00 },
485 { 0xff, 0xff, 0xff, 0x00 }
488 static RGBTRIPLE DefLogPaletteTriples
[20] = { /* Copy of Default Logical Palette */
489 /* rgbBlue, rgbGreen, rgbRed */
490 { 0x00, 0x00, 0x00 },
491 { 0x00, 0x00, 0x80 },
492 { 0x00, 0x80, 0x00 },
493 { 0x00, 0x80, 0x80 },
494 { 0x80, 0x00, 0x00 },
495 { 0x80, 0x00, 0x80 },
496 { 0x80, 0x80, 0x00 },
497 { 0xc0, 0xc0, 0xc0 },
498 { 0xc0, 0xdc, 0xc0 },
499 { 0xf0, 0xca, 0xa6 },
500 { 0xf0, 0xfb, 0xff },
501 { 0xa4, 0xa0, 0xa0 },
502 { 0x80, 0x80, 0x80 },
503 { 0x00, 0x00, 0xf0 },
504 { 0x00, 0xff, 0x00 },
505 { 0x00, 0xff, 0xff },
506 { 0xff, 0x00, 0x00 },
507 { 0xff, 0x00, 0xff },
508 { 0xff, 0xff, 0x00 },
513 /******************************************************************************
514 * GetDIBits [GDI32.@]
516 * Retrieves bits of bitmap and copies to buffer.
519 * Success: Number of scan lines copied from bitmap
522 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_87eb.asp
524 INT WINAPI
GetDIBits(
525 HDC hdc
, /* [in] Handle to device context */
526 HBITMAP hbitmap
, /* [in] Handle to bitmap */
527 UINT startscan
, /* [in] First scan line to set in dest bitmap */
528 UINT lines
, /* [in] Number of scan lines to copy */
529 LPVOID bits
, /* [out] Address of array for bitmap bits */
530 BITMAPINFO
* info
, /* [out] Address of structure with bitmap data */
531 UINT coloruse
) /* [in] RGB or palette index */
544 RGBTRIPLE
* rgbTriples
;
549 bitmap_type
= DIB_GetBitmapInfo( &info
->bmiHeader
, &width
, &height
, &planes
, &bpp
, &compr
, &size
);
550 if (bitmap_type
== -1)
552 ERR("Invalid bitmap format\n");
555 core_header
= (bitmap_type
== 0);
556 memdc
= CreateCompatibleDC(hdc
);
557 if (!(dc
= DC_GetDCUpdate( hdc
)))
562 if (!(bmp
= (BITMAPOBJ
*)GDI_GetObjPtr( hbitmap
, BITMAP_MAGIC
)))
564 GDI_ReleaseObj( hdc
);
569 colorPtr
= (LPBYTE
) info
+ (WORD
) info
->bmiHeader
.biSize
;
570 rgbTriples
= (RGBTRIPLE
*) colorPtr
;
571 rgbQuads
= (RGBQUAD
*) colorPtr
;
573 /* Transfer color info */
575 if (bpp
<= 8 && bpp
> 0)
577 if (!core_header
) info
->bmiHeader
.biClrUsed
= 0;
579 /* If the bitmap object already has a dib section at the
580 same color depth then get the color map from it */
581 if (bmp
->dib
&& bmp
->dib
->dsBm
.bmBitsPixel
== bpp
) {
582 if(coloruse
== DIB_RGB_COLORS
) {
583 HBITMAP oldbm
= SelectObject(memdc
, hbitmap
);
584 unsigned int colors
= 1 << bpp
;
588 /* Convert the color table (RGBQUAD to RGBTRIPLE) */
589 RGBQUAD
* buffer
= HeapAlloc(GetProcessHeap(), 0, colors
* sizeof(RGBQUAD
));
593 RGBTRIPLE
* index
= rgbTriples
;
594 GetDIBColorTable(memdc
, 0, colors
, buffer
);
596 for (i
=0; i
< colors
; i
++, index
++)
598 index
->rgbtRed
= buffer
[i
].rgbRed
;
599 index
->rgbtGreen
= buffer
[i
].rgbGreen
;
600 index
->rgbtBlue
= buffer
[i
].rgbBlue
;
603 HeapFree(GetProcessHeap(), 0, buffer
);
608 GetDIBColorTable(memdc
, 0, colors
, colorPtr
);
610 SelectObject(memdc
, oldbm
);
613 WORD
*index
= colorPtr
;
614 for(i
= 0; i
< 1 << info
->bmiHeader
.biBitCount
; i
++, index
++)
619 if(bpp
>= bmp
->bitmap
.bmBitsPixel
) {
620 /* Generate the color map from the selected palette */
621 PALETTEENTRY
* palEntry
;
622 PALETTEOBJ
* palette
;
623 if (!(palette
= (PALETTEOBJ
*)GDI_GetObjPtr( dc
->hPalette
, PALETTE_MAGIC
))) {
624 GDI_ReleaseObj( hdc
);
625 GDI_ReleaseObj( hbitmap
);
629 palEntry
= palette
->logpalette
.palPalEntry
;
630 for (i
= 0; i
< (1 << bmp
->bitmap
.bmBitsPixel
); i
++, palEntry
++) {
631 if (coloruse
== DIB_RGB_COLORS
) {
634 rgbTriples
[i
].rgbtRed
= palEntry
->peRed
;
635 rgbTriples
[i
].rgbtGreen
= palEntry
->peGreen
;
636 rgbTriples
[i
].rgbtBlue
= palEntry
->peBlue
;
640 rgbQuads
[i
].rgbRed
= palEntry
->peRed
;
641 rgbQuads
[i
].rgbGreen
= palEntry
->peGreen
;
642 rgbQuads
[i
].rgbBlue
= palEntry
->peBlue
;
643 rgbQuads
[i
].rgbReserved
= 0;
646 else ((WORD
*)colorPtr
)[i
] = (WORD
)i
;
648 GDI_ReleaseObj( dc
->hPalette
);
654 rgbTriples
[0].rgbtRed
= rgbTriples
[0].rgbtGreen
=
655 rgbTriples
[0].rgbtBlue
= 0;
656 rgbTriples
[1].rgbtRed
= rgbTriples
[1].rgbtGreen
=
657 rgbTriples
[1].rgbtBlue
= 0xff;
661 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
=
662 rgbQuads
[0].rgbBlue
= 0;
663 rgbQuads
[0].rgbReserved
= 0;
664 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
=
665 rgbQuads
[1].rgbBlue
= 0xff;
666 rgbQuads
[1].rgbReserved
= 0;
672 memcpy(colorPtr
, EGAColorsTriples
, sizeof(EGAColorsTriples
));
674 memcpy(colorPtr
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
685 memcpy(rgbTriples
, DefLogPaletteTriples
,
686 10 * sizeof(RGBTRIPLE
));
687 memcpy(rgbTriples
+ 246, DefLogPaletteTriples
+ 10,
688 10 * sizeof(RGBTRIPLE
));
689 color
= rgbTriples
+ 10;
690 for(r
= 0; r
<= 5; r
++) /* FIXME */
691 for(g
= 0; g
<= 5; g
++)
692 for(b
= 0; b
<= 5; b
++) {
693 color
->rgbtRed
= (r
* 0xff) / 5;
694 color
->rgbtGreen
= (g
* 0xff) / 5;
695 color
->rgbtBlue
= (b
* 0xff) / 5;
704 memcpy(rgbQuads
, DefLogPaletteQuads
,
705 10 * sizeof(RGBQUAD
));
706 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10,
707 10 * sizeof(RGBQUAD
));
708 color
= rgbQuads
+ 10;
709 for(r
= 0; r
<= 5; r
++) /* FIXME */
710 for(g
= 0; g
<= 5; g
++)
711 for(b
= 0; b
<= 5; b
++) {
712 color
->rgbRed
= (r
* 0xff) / 5;
713 color
->rgbGreen
= (g
* 0xff) / 5;
714 color
->rgbBlue
= (b
* 0xff) / 5;
715 color
->rgbReserved
= 0;
727 /* If the bitmap object already have a dib section that contains image data, get the bits from it */
728 if(bmp
->dib
&& bmp
->dib
->dsBm
.bmBitsPixel
>= 15 && bpp
>= 15)
730 /*FIXME: Only RGB dibs supported for now */
731 unsigned int srcwidth
= bmp
->dib
->dsBm
.bmWidth
, srcwidthb
= bmp
->dib
->dsBm
.bmWidthBytes
;
732 unsigned int dstwidth
= width
;
733 int dstwidthb
= DIB_GetDIBWidthBytes( width
, bpp
);
734 LPBYTE dbits
= bits
, sbits
= (LPBYTE
) bmp
->dib
->dsBm
.bmBits
+ (startscan
* srcwidthb
);
735 unsigned int x
, y
, width
, widthb
;
737 if ((height
< 0) ^ (bmp
->dib
->dsBmih
.biHeight
< 0))
739 dbits
= (LPBYTE
)bits
+ (dstwidthb
* (lines
-1));
740 dstwidthb
= -dstwidthb
;
746 case 16: /* 16 bpp dstDIB */
748 LPWORD dstbits
= (LPWORD
)dbits
;
749 WORD rmask
= 0x7c00, gmask
= 0x03e0, bmask
= 0x001f;
751 /* FIXME: BI_BITFIELDS not supported yet */
753 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
755 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
757 widthb
= min(srcwidthb
, abs(dstwidthb
));
758 /* FIXME: BI_BITFIELDS not supported yet */
759 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
760 memcpy(dbits
, sbits
, widthb
);
764 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
766 LPBYTE srcbits
= sbits
;
768 width
= min(srcwidth
, dstwidth
);
769 for( y
= 0; y
< lines
; y
++) {
770 for( x
= 0; x
< width
; x
++, srcbits
+= 3)
771 *dstbits
++ = ((srcbits
[0] >> 3) & bmask
) |
772 (((WORD
)srcbits
[1] << 2) & gmask
) |
773 (((WORD
)srcbits
[2] << 7) & rmask
);
775 dstbits
= (LPWORD
)(dbits
+=dstwidthb
);
776 srcbits
= (sbits
+= srcwidthb
);
781 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
783 LPDWORD srcbits
= (LPDWORD
)sbits
;
786 width
= min(srcwidth
, dstwidth
);
787 for( y
= 0; y
< lines
; y
++) {
788 for( x
= 0; x
< width
; x
++ ) {
790 *dstbits
++ = (WORD
)(((val
>> 3) & bmask
) | ((val
>> 6) & gmask
) |
791 ((val
>> 9) & rmask
));
793 dstbits
= (LPWORD
)(dbits
+=dstwidthb
);
794 srcbits
= (LPDWORD
)(sbits
+=srcwidthb
);
799 default: /* ? bit bmp -> 16 bit DIB */
800 FIXME("15/16 bit DIB %d bit bitmap\n",
801 bmp
->bitmap
.bmBitsPixel
);
807 case 24: /* 24 bpp dstDIB */
809 LPBYTE dstbits
= dbits
;
811 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
813 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
815 LPWORD srcbits
= (LPWORD
)sbits
;
818 width
= min(srcwidth
, dstwidth
);
819 /* FIXME: BI_BITFIELDS not supported yet */
820 for( y
= 0; y
< lines
; y
++) {
821 for( x
= 0; x
< width
; x
++ ) {
823 *dstbits
++ = (BYTE
)(((val
<< 3) & 0xf8) | ((val
>> 2) & 0x07));
824 *dstbits
++ = (BYTE
)(((val
>> 2) & 0xf8) | ((val
>> 7) & 0x07));
825 *dstbits
++ = (BYTE
)(((val
>> 7) & 0xf8) | ((val
>> 12) & 0x07));
827 dstbits
= (LPBYTE
)(dbits
+=dstwidthb
);
828 srcbits
= (LPWORD
)(sbits
+=srcwidthb
);
833 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
835 widthb
= min(srcwidthb
, abs(dstwidthb
));
836 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
)
837 memcpy(dbits
, sbits
, widthb
);
841 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
843 LPBYTE srcbits
= (LPBYTE
)sbits
;
845 width
= min(srcwidth
, dstwidth
);
846 for( y
= 0; y
< lines
; y
++) {
847 for( x
= 0; x
< width
; x
++, srcbits
++ ) {
848 *dstbits
++ = *srcbits
++;
849 *dstbits
++ = *srcbits
++;
850 *dstbits
++ = *srcbits
++;
852 dstbits
=(LPBYTE
)(dbits
+=dstwidthb
);
853 srcbits
= (LPBYTE
)(sbits
+=srcwidthb
);
858 default: /* ? bit bmp -> 24 bit DIB */
859 FIXME("24 bit DIB %d bit bitmap\n",
860 bmp
->bitmap
.bmBitsPixel
);
866 case 32: /* 32 bpp dstDIB */
868 LPDWORD dstbits
= (LPDWORD
)dbits
;
870 /* FIXME: BI_BITFIELDS not supported yet */
872 switch(bmp
->dib
->dsBm
.bmBitsPixel
) {
873 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
875 LPWORD srcbits
= (LPWORD
)sbits
;
878 width
= min(srcwidth
, dstwidth
);
879 /* FIXME: BI_BITFIELDS not supported yet */
880 for( y
= 0; y
< lines
; y
++) {
881 for( x
= 0; x
< width
; x
++ ) {
882 val
= (DWORD
)*srcbits
++;
883 *dstbits
++ = ((val
<< 3) & 0xf8) | ((val
>> 2) & 0x07) |
884 ((val
<< 6) & 0xf800) | ((val
<< 1) & 0x0700) |
885 ((val
<< 9) & 0xf80000) | ((val
<< 4) & 0x070000);
887 dstbits
=(LPDWORD
)(dbits
+=dstwidthb
);
888 srcbits
=(LPWORD
)(sbits
+=srcwidthb
);
893 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
895 LPBYTE srcbits
= sbits
;
897 width
= min(srcwidth
, dstwidth
);
898 for( y
= 0; y
< lines
; y
++) {
899 for( x
= 0; x
< width
; x
++, srcbits
+=3 )
900 *dstbits
++ = ((DWORD
)*srcbits
) & 0x00ffffff;
901 dstbits
=(LPDWORD
)(dbits
+=dstwidthb
);
902 srcbits
=(sbits
+=srcwidthb
);
907 case 32: /* 32 bpp srcDIB -> 32 bpp dstDIB */
909 widthb
= min(srcwidthb
, abs(dstwidthb
));
910 /* FIXME: BI_BITFIELDS not supported yet */
911 for (y
= 0; y
< lines
; y
++, dbits
+=dstwidthb
, sbits
+=srcwidthb
) {
912 memcpy(dbits
, sbits
, widthb
);
917 default: /* ? bit bmp -> 32 bit DIB */
918 FIXME("32 bit DIB %d bit bitmap\n",
919 bmp
->bitmap
.bmBitsPixel
);
925 default: /* ? bit DIB */
926 FIXME("Unsupported DIB depth %d\n", info
->bmiHeader
.biBitCount
);
930 /* Otherwise, get bits from the XImage */
933 if (!bmp
->funcs
&& !BITMAP_SetOwnerDC( hbitmap
, dc
)) lines
= 0;
936 if (bmp
->funcs
&& bmp
->funcs
->pGetDIBits
)
937 lines
= bmp
->funcs
->pGetDIBits( dc
->physDev
, hbitmap
, startscan
,
938 lines
, bits
, info
, coloruse
);
940 lines
= 0; /* FIXME: should copy from bmp->bitmap.bmBits */
946 /* fill in struct members */
952 BITMAPCOREHEADER
* coreheader
= (BITMAPCOREHEADER
*) info
;
953 coreheader
->bcWidth
= bmp
->bitmap
.bmWidth
;
954 coreheader
->bcHeight
= bmp
->bitmap
.bmHeight
;
955 coreheader
->bcPlanes
= 1;
956 coreheader
->bcBitCount
= bmp
->bitmap
.bmBitsPixel
;
960 info
->bmiHeader
.biWidth
= bmp
->bitmap
.bmWidth
;
961 info
->bmiHeader
.biHeight
= bmp
->bitmap
.bmHeight
;
962 info
->bmiHeader
.biPlanes
= 1;
963 info
->bmiHeader
.biSizeImage
=
964 DIB_GetDIBImageBytes( bmp
->bitmap
.bmWidth
,
965 bmp
->bitmap
.bmHeight
,
966 bmp
->bitmap
.bmBitsPixel
);
967 switch(bmp
->bitmap
.bmBitsPixel
)
970 info
->bmiHeader
.biBitCount
= 16;
971 info
->bmiHeader
.biCompression
= BI_RGB
;
975 info
->bmiHeader
.biBitCount
= 16;
976 info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
977 ((PDWORD
)info
->bmiColors
)[0] = 0xf800;
978 ((PDWORD
)info
->bmiColors
)[1] = 0x07e0;
979 ((PDWORD
)info
->bmiColors
)[2] = 0x001f;
983 info
->bmiHeader
.biBitCount
= bmp
->bitmap
.bmBitsPixel
;
984 info
->bmiHeader
.biCompression
= BI_RGB
;
987 info
->bmiHeader
.biXPelsPerMeter
= 0;
988 info
->bmiHeader
.biYPelsPerMeter
= 0;
989 info
->bmiHeader
.biClrUsed
= 0;
990 info
->bmiHeader
.biClrImportant
= 0;
992 /* Windows 2000 doesn't touch the additional struct members if
993 it's a BITMAPV4HEADER or a BITMAPV5HEADER */
995 lines
= abs(bmp
->bitmap
.bmHeight
);
999 /* The knowledge base article Q81498 ("DIBs and Their Uses") states that
1000 if bits == NULL and bpp != 0, only biSizeImage and the color table are
1004 /* FIXME: biSizeImage should be calculated according to the selected
1005 compression algorithm if biCompression != BI_RGB */
1006 info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( width
, height
, bpp
);
1008 lines
= abs(height
);
1014 TRACE("biSizeImage = %ld, ", info
->bmiHeader
.biSizeImage
);
1016 TRACE("biWidth = %ld, biHeight = %ld\n", width
, height
);
1018 GDI_ReleaseObj( hdc
);
1019 GDI_ReleaseObj( hbitmap
);
1025 /***********************************************************************
1026 * CreateDIBitmap (GDI32.@)
1028 * Creates a DDB (device dependent bitmap) from a DIB.
1029 * The DDB will have the same color depth as the reference DC.
1031 HBITMAP WINAPI
CreateDIBitmap( HDC hdc
, const BITMAPINFOHEADER
*header
,
1032 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
1042 if (DIB_GetBitmapInfo( header
, &width
, &height
, &planes
, &bpp
, &compr
, &size
) == -1) return 0;
1046 TRACE("Bitmap has a negative width\n");
1050 /* Top-down DIBs have a negative height */
1051 if (height
< 0) height
= -height
;
1053 TRACE("hdc=%p, header=%p, init=%lu, bits=%p, data=%p, coloruse=%u (bitmap: width=%ld, height=%ld, bpp=%u, compr=%lu)\n",
1054 hdc
, header
, init
, bits
, data
, coloruse
, width
, height
, bpp
, compr
);
1057 handle
= CreateBitmap( width
, height
, 1, 1, NULL
);
1059 handle
= CreateCompatibleBitmap( hdc
, width
, height
);
1063 if (init
== CBM_INIT
) SetDIBits( hdc
, handle
, 0, height
, bits
, data
, coloruse
);
1065 else if (hdc
&& ((dc
= DC_GetDCPtr( hdc
)) != NULL
) )
1067 if (!BITMAP_SetOwnerDC( handle
, dc
))
1069 DeleteObject( handle
);
1072 GDI_ReleaseObj( hdc
);
1079 /***********************************************************************
1080 * CreateDIBSection (GDI.489)
1082 HBITMAP16 WINAPI
CreateDIBSection16 (HDC16 hdc
, const BITMAPINFO
*bmi
, UINT16 usage
,
1083 SEGPTR
*bits16
, HANDLE section
, DWORD offset
)
1088 hbitmap
= CreateDIBSection( HDC_32(hdc
), bmi
, usage
, &bits32
, section
, offset
);
1091 BITMAPOBJ
*bmp
= (BITMAPOBJ
*) GDI_GetObjPtr(hbitmap
, BITMAP_MAGIC
);
1092 if (bmp
&& bmp
->dib
&& bits32
)
1094 const BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1102 DIB_GetBitmapInfo(bi
, &width
, &height
, &planes
, &bpp
, &compr
, &size
);
1104 height
= height
>= 0 ? height
: -height
;
1105 width_bytes
= DIB_GetDIBWidthBytes(width
, bpp
);
1107 if (!size
|| (compr
!= BI_RLE4
&& compr
!= BI_RLE8
)) size
= width_bytes
* height
;
1109 /* calculate number of sel's needed for size with 64K steps */
1110 count
= (size
+ 0xffff) / 0x10000;
1111 sel
= AllocSelectorArray16(count
);
1113 for (i
= 0; i
< count
; i
++)
1115 SetSelectorBase(sel
+ (i
<< __AHSHIFT
), (DWORD
)bits32
+ i
* 0x10000);
1116 SetSelectorLimit16(sel
+ (i
<< __AHSHIFT
), size
- 1); /* yep, limit is correct */
1119 bmp
->segptr_bits
= MAKESEGPTR( sel
, 0 );
1120 if (bits16
) *bits16
= bmp
->segptr_bits
;
1122 if (bmp
) GDI_ReleaseObj( hbitmap
);
1124 return HBITMAP_16(hbitmap
);
1127 /***********************************************************************
1128 * DIB_CreateDIBSection
1130 HBITMAP
DIB_CreateDIBSection(HDC hdc
, const BITMAPINFO
*bmi
, UINT usage
,
1131 VOID
**bits
, HANDLE section
,
1132 DWORD offset
, DWORD ovr_pitch
)
1136 BOOL bDesktopDC
= FALSE
;
1142 DWORD compression
, sizeImage
;
1143 void *mapBits
= NULL
;
1145 if (((bitmap_type
= DIB_GetBitmapInfo( &bmi
->bmiHeader
, &width
, &height
,
1146 &planes
, &bpp
, &compression
, &sizeImage
)) == -1))
1149 if (!(dib
= HeapAlloc( GetProcessHeap(), 0, sizeof(*dib
) ))) return 0;
1151 TRACE("format (%ld,%ld), planes %d, bpp %d, size %ld, %s\n",
1152 width
, height
, planes
, bpp
, sizeImage
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1154 dib
->dsBm
.bmType
= 0;
1155 dib
->dsBm
.bmWidth
= width
;
1156 dib
->dsBm
.bmHeight
= height
>= 0 ? height
: -height
;
1157 dib
->dsBm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: DIB_GetDIBWidthBytes(width
, bpp
);
1158 dib
->dsBm
.bmPlanes
= planes
;
1159 dib
->dsBm
.bmBitsPixel
= bpp
;
1160 dib
->dsBm
.bmBits
= NULL
;
1162 if (!bitmap_type
) /* core header */
1164 /* convert the BITMAPCOREHEADER to a BITMAPINFOHEADER */
1165 dib
->dsBmih
.biSize
= sizeof(BITMAPINFOHEADER
);
1166 dib
->dsBmih
.biWidth
= width
;
1167 dib
->dsBmih
.biHeight
= height
;
1168 dib
->dsBmih
.biPlanes
= planes
;
1169 dib
->dsBmih
.biBitCount
= bpp
;
1170 dib
->dsBmih
.biCompression
= compression
;
1171 dib
->dsBmih
.biXPelsPerMeter
= 0;
1172 dib
->dsBmih
.biYPelsPerMeter
= 0;
1173 dib
->dsBmih
.biClrUsed
= 0;
1174 dib
->dsBmih
.biClrImportant
= 0;
1178 /* truncate extended bitmap headers (BITMAPV4HEADER etc.) */
1179 dib
->dsBmih
= bmi
->bmiHeader
;
1180 dib
->dsBmih
.biSize
= sizeof(BITMAPINFOHEADER
);
1183 /* only use sizeImage if it's valid and we're dealing with a compressed bitmap */
1184 if (sizeImage
&& (compression
== BI_RLE4
|| compression
== BI_RLE8
))
1185 dib
->dsBmih
.biSizeImage
= sizeImage
;
1187 dib
->dsBmih
.biSizeImage
= dib
->dsBm
.bmWidthBytes
* dib
->dsBm
.bmHeight
;
1190 /* set dsBitfields values */
1191 if (usage
== DIB_PAL_COLORS
|| bpp
<= 8)
1193 dib
->dsBitfields
[0] = dib
->dsBitfields
[1] = dib
->dsBitfields
[2] = 0;
1199 dib
->dsBitfields
[0] = (compression
== BI_BITFIELDS
) ? *(const DWORD
*)bmi
->bmiColors
: 0x7c00;
1200 dib
->dsBitfields
[1] = (compression
== BI_BITFIELDS
) ? *((const DWORD
*)bmi
->bmiColors
+ 1) : 0x03e0;
1201 dib
->dsBitfields
[2] = (compression
== BI_BITFIELDS
) ? *((const DWORD
*)bmi
->bmiColors
+ 2) : 0x001f;
1205 dib
->dsBitfields
[0] = (compression
== BI_BITFIELDS
) ? *(const DWORD
*)bmi
->bmiColors
: 0xff0000;
1206 dib
->dsBitfields
[1] = (compression
== BI_BITFIELDS
) ? *((const DWORD
*)bmi
->bmiColors
+ 1) : 0x00ff00;
1207 dib
->dsBitfields
[2] = (compression
== BI_BITFIELDS
) ? *((const DWORD
*)bmi
->bmiColors
+ 2) : 0x0000ff;
1211 /* get storage location for DIB bits */
1215 SYSTEM_INFO SystemInfo
;
1219 GetSystemInfo( &SystemInfo
);
1220 mapOffset
= offset
- (offset
% SystemInfo
.dwAllocationGranularity
);
1221 mapSize
= dib
->dsBmih
.biSizeImage
+ (offset
- mapOffset
);
1222 mapBits
= MapViewOfFile( section
, FILE_MAP_ALL_ACCESS
, 0, mapOffset
, mapSize
);
1223 if (mapBits
) dib
->dsBm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1225 else if (ovr_pitch
&& offset
)
1226 dib
->dsBm
.bmBits
= (LPVOID
) offset
;
1230 dib
->dsBm
.bmBits
= VirtualAlloc( NULL
, dib
->dsBmih
.biSizeImage
,
1231 MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
1233 dib
->dshSection
= section
;
1234 dib
->dsOffset
= offset
;
1236 if (!dib
->dsBm
.bmBits
)
1238 HeapFree( GetProcessHeap(), 0, dib
);
1242 /* If the reference hdc is null, take the desktop dc */
1245 hdc
= CreateCompatibleDC(0);
1249 if (!(dc
= DC_GetDCPtr( hdc
))) goto error
;
1251 /* create Device Dependent Bitmap and add DIB pointer */
1252 ret
= CreateBitmap( dib
->dsBm
.bmWidth
, dib
->dsBm
.bmHeight
, 1,
1253 (bpp
== 1) ? 1 : GetDeviceCaps(hdc
, BITSPIXEL
), NULL
);
1255 if (ret
&& ((bmp
= GDI_GetObjPtr(ret
, BITMAP_MAGIC
))))
1258 bmp
->funcs
= dc
->funcs
;
1259 GDI_ReleaseObj( ret
);
1261 if (dc
->funcs
->pCreateDIBSection
)
1263 if (!dc
->funcs
->pCreateDIBSection(dc
->physDev
, ret
, bmi
, usage
))
1265 DeleteObject( ret
);
1271 GDI_ReleaseObj(hdc
);
1272 if (bDesktopDC
) DeleteDC( hdc
);
1273 if (ret
&& bits
) *bits
= dib
->dsBm
.bmBits
;
1277 if (bDesktopDC
) DeleteDC( hdc
);
1278 if (section
) UnmapViewOfFile( mapBits
);
1279 else if (!offset
) VirtualFree( dib
->dsBm
.bmBits
, 0, MEM_RELEASE
);
1280 HeapFree( GetProcessHeap(), 0, dib
);
1284 /***********************************************************************
1285 * CreateDIBSection (GDI32.@)
1287 HBITMAP WINAPI
CreateDIBSection(HDC hdc
, CONST BITMAPINFO
*bmi
, UINT usage
,
1288 VOID
**bits
, HANDLE section
,
1291 return DIB_CreateDIBSection(hdc
, bmi
, usage
, bits
, section
, offset
, 0);