2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
13 #include "selectors.h"
15 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(bitmap);
20 /***********************************************************************
21 * DIB_GetDIBWidthBytes
23 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
24 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
26 int DIB_GetDIBWidthBytes( int width, int depth )
32 case 1: words = (width + 31) / 32; break;
33 case 4: words = (width + 7) / 8; break;
34 case 8: words = (width + 3) / 4; break;
36 case 16: words = (width + 1) / 2; break;
37 case 24: words = (width * 3 + 3)/4; break;
40 WARN("(%d): Unsupported depth\n", depth );
48 /***********************************************************************
49 * DIB_GetDIBImageBytes
51 * Return the number of bytes used to hold the image in a DIB bitmap.
53 int DIB_GetDIBImageBytes( int width, int height, int depth )
55 return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
59 /***********************************************************************
62 * Return the size of the bitmap info structure including color table.
64 int DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
68 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
70 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
71 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
72 return sizeof(BITMAPCOREHEADER) + colors *
73 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
75 else /* assume BITMAPINFOHEADER */
77 colors = info->bmiHeader.biClrUsed;
78 if (!colors && (info->bmiHeader.biBitCount <= 8))
79 colors = 1 << info->bmiHeader.biBitCount;
80 return sizeof(BITMAPINFOHEADER) + colors *
81 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
86 /***********************************************************************
89 * Get the info from a bitmap header.
90 * Return 1 for INFOHEADER, 0 for COREHEADER,
91 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
93 int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
94 int *height, WORD *bpp, WORD *compr )
96 if (header->biSize == sizeof(BITMAPINFOHEADER))
98 *width = header->biWidth;
99 *height = header->biHeight;
100 *bpp = header->biBitCount;
101 *compr = header->biCompression;
104 if (header->biSize == sizeof(BITMAPCOREHEADER))
106 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
107 *width = core->bcWidth;
108 *height = core->bcHeight;
109 *bpp = core->bcBitCount;
113 if (header->biSize == sizeof(BITMAPV4HEADER))
115 BITMAPV4HEADER *v4hdr = (BITMAPV4HEADER *)header;
116 *width = v4hdr->bV4Width;
117 *height = v4hdr->bV4Height;
118 *bpp = v4hdr->bV4BitCount;
119 *compr = v4hdr->bV4Compression;
122 if (header->biSize == sizeof(BITMAPV5HEADER))
124 BITMAPV5HEADER *v5hdr = (BITMAPV5HEADER *)header;
125 *width = v5hdr->bV5Width;
126 *height = v5hdr->bV5Height;
127 *bpp = v5hdr->bV5BitCount;
128 *compr = v5hdr->bV5Compression;
131 ERR("(%ld): unknown/wrong size for header\n", header->biSize );
136 /***********************************************************************
137 * StretchDIBits (GDI.439)
139 INT16 WINAPI StretchDIBits16(HDC16 hdc, INT16 xDst, INT16 yDst, INT16 widthDst,
140 INT16 heightDst, INT16 xSrc, INT16 ySrc, INT16 widthSrc,
141 INT16 heightSrc, const VOID *bits,
142 const BITMAPINFO *info, UINT16 wUsage, DWORD dwRop )
144 return (INT16)StretchDIBits( hdc, xDst, yDst, widthDst, heightDst,
145 xSrc, ySrc, widthSrc, heightSrc, bits,
146 info, wUsage, dwRop );
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 )
158 DC *dc = DC_GetDCUpdate( hdc );
159 if(!dc) return FALSE;
161 if(dc->funcs->pStretchDIBits)
163 heightSrc = dc->funcs->pStretchDIBits(dc, xDst, yDst, widthDst,
164 heightDst, xSrc, ySrc, widthSrc,
165 heightSrc, bits, info, wUsage, dwRop);
166 GDI_ReleaseObj( hdc );
168 else /* use StretchBlt */
170 HBITMAP hBitmap, hOldBitmap;
173 GDI_ReleaseObj( hdc );
174 hdcMem = CreateCompatibleDC( hdc );
175 if (info->bmiHeader.biCompression == BI_RLE4 ||
176 info->bmiHeader.biCompression == BI_RLE8) {
178 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
179 * contain all the rectangle described in bmiHeader, but only part of it.
180 * This mean that those undescribed pixels must be left untouched.
181 * So, we first copy on a memory bitmap the current content of the
182 * destination rectangle, blit the DIB bits on top of it - hence leaving
183 * the gaps untouched -, and blitting the rectangle back.
184 * This insure that gaps are untouched on the destination rectangle
185 * Not doing so leads to trashed images (the gaps contain what was on the
186 * memory bitmap => generally black or garbage)
187 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
188 * another speed vs correctness issue. Anyway, if speed is needed, then the
189 * pStretchDIBits function shall be implemented.
192 hBitmap = CreateCompatibleBitmap(hdc, info->bmiHeader.biWidth,
193 info->bmiHeader.biHeight);
194 hOldBitmap = SelectObject( hdcMem, hBitmap );
196 /* copy existing bitmap from destination dc */
197 StretchBlt( hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
198 widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
200 SetDIBits(hdcMem, hBitmap, 0, info->bmiHeader.biHeight, bits,
201 info, DIB_RGB_COLORS);
204 hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
205 bits, info, wUsage );
206 hOldBitmap = SelectObject( hdcMem, hBitmap );
209 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
210 left (negative biHeight) */
211 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
212 hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
213 widthSrc, heightSrc, dwRop );
214 SelectObject( hdcMem, hOldBitmap );
216 DeleteObject( hBitmap );
222 /***********************************************************************
223 * SetDIBits (GDI.440)
225 INT16 WINAPI SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
226 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
229 return SetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
233 /******************************************************************************
234 * SetDIBits [GDI32.@] Sets pixels in a bitmap using colors from DIB
237 * hdc [I] Handle to device context
238 * hbitmap [I] Handle to bitmap
239 * startscan [I] Starting scan line
240 * lines [I] Number of scan lines
241 * bits [I] Array of bitmap bits
242 * info [I] Address of structure with data
243 * coloruse [I] Type of color indexes to use
246 * Success: Number of scan lines copied
249 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
250 UINT lines, LPCVOID bits, const BITMAPINFO *info,
257 /* Check parameters */
258 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
260 if (!(bitmap = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
262 GDI_ReleaseObj( hdc );
266 result = BITMAP_Driver->pSetDIBits(bitmap, dc, startscan,
270 GDI_ReleaseObj( hbitmap );
271 GDI_ReleaseObj( hdc );
277 /***********************************************************************
278 * SetDIBitsToDevice (GDI.443)
280 INT16 WINAPI SetDIBitsToDevice16(HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
281 INT16 cy, INT16 xSrc, INT16 ySrc, UINT16 startscan,
282 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
285 return SetDIBitsToDevice( hdc, xDest, yDest, cx, cy, xSrc, ySrc,
286 startscan, lines, bits, info, coloruse );
290 /***********************************************************************
291 * SetDIBitsToDevice (GDI32.@)
293 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
294 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
295 UINT lines, LPCVOID bits, const BITMAPINFO *info,
301 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
303 if(dc->funcs->pSetDIBitsToDevice)
304 ret = dc->funcs->pSetDIBitsToDevice( dc, xDest, yDest, cx, cy, xSrc,
305 ySrc, startscan, lines, bits,
308 FIXME("unimplemented on hdc %08x\n", hdc);
312 GDI_ReleaseObj( hdc );
316 /***********************************************************************
317 * SetDIBColorTable (GDI.602)
319 UINT16 WINAPI SetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
322 return SetDIBColorTable( hdc, startpos, entries, colors );
325 /***********************************************************************
326 * SetDIBColorTable (GDI32.@)
328 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
335 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
337 if (!(bmp = (BITMAPOBJ*)GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC )))
339 GDI_ReleaseObj( hdc );
343 result = BITMAP_Driver->pSetDIBColorTable(bmp, dc, startpos, entries, colors);
345 GDI_ReleaseObj( dc->hBitmap );
346 GDI_ReleaseObj( hdc );
350 /***********************************************************************
351 * GetDIBColorTable (GDI.603)
353 UINT16 WINAPI GetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
356 return GetDIBColorTable( hdc, startpos, entries, colors );
359 /***********************************************************************
360 * GetDIBColorTable (GDI32.@)
362 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
369 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
371 if (!(bmp = (BITMAPOBJ*)GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC )))
373 GDI_ReleaseObj( hdc );
377 result = BITMAP_Driver->pGetDIBColorTable(bmp, dc, startpos, entries, colors);
379 GDI_ReleaseObj( dc->hBitmap );
380 GDI_ReleaseObj( hdc );
384 /* FIXME the following two structs should be combined with __sysPalTemplate in
385 objects/color.c - this should happen after de-X11-ing both of these
387 NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
390 static RGBQUAD EGAColors[16] = {
391 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
392 { 0x00, 0x00, 0x00, 0x00 },
393 { 0x00, 0x00, 0x80, 0x00 },
394 { 0x00, 0x80, 0x00, 0x00 },
395 { 0x00, 0x80, 0x80, 0x00 },
396 { 0x80, 0x00, 0x00, 0x00 },
397 { 0x80, 0x00, 0x80, 0x00 },
398 { 0x80, 0x80, 0x00, 0x00 },
399 { 0x80, 0x80, 0x80, 0x00 },
400 { 0xc0, 0xc0, 0xc0, 0x00 },
401 { 0x00, 0x00, 0xff, 0x00 },
402 { 0x00, 0xff, 0x00, 0x00 },
403 { 0x00, 0xff, 0xff, 0x00 },
404 { 0xff, 0x00, 0x00, 0x00 },
405 { 0xff, 0x00, 0xff, 0x00 },
406 { 0xff, 0xff, 0x00, 0x00 },
407 { 0xff, 0xff, 0xff, 0x00 }
411 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
412 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
413 { 0x00, 0x00, 0x00, 0x00 },
414 { 0x00, 0x00, 0x80, 0x00 },
415 { 0x00, 0x80, 0x00, 0x00 },
416 { 0x00, 0x80, 0x80, 0x00 },
417 { 0x80, 0x00, 0x00, 0x00 },
418 { 0x80, 0x00, 0x80, 0x00 },
419 { 0x80, 0x80, 0x00, 0x00 },
420 { 0xc0, 0xc0, 0xc0, 0x00 },
421 { 0xc0, 0xdc, 0xc0, 0x00 },
422 { 0xf0, 0xca, 0xa6, 0x00 },
423 { 0xf0, 0xfb, 0xff, 0x00 },
424 { 0xa4, 0xa0, 0xa0, 0x00 },
425 { 0x80, 0x80, 0x80, 0x00 },
426 { 0x00, 0x00, 0xf0, 0x00 },
427 { 0x00, 0xff, 0x00, 0x00 },
428 { 0x00, 0xff, 0xff, 0x00 },
429 { 0xff, 0x00, 0x00, 0x00 },
430 { 0xff, 0x00, 0xff, 0x00 },
431 { 0xff, 0xff, 0x00, 0x00 },
432 { 0xff, 0xff, 0xff, 0x00 }
435 /***********************************************************************
436 * GetDIBits (GDI.441)
438 INT16 WINAPI GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
439 UINT16 lines, LPVOID bits, BITMAPINFO * info,
442 return GetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
446 /******************************************************************************
447 * GetDIBits [GDI32.@] Retrieves bits of bitmap and copies to buffer
450 * Success: Number of scan lines copied from bitmap
453 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
455 INT WINAPI GetDIBits(
456 HDC hdc, /* [in] Handle to device context */
457 HBITMAP hbitmap, /* [in] Handle to bitmap */
458 UINT startscan, /* [in] First scan line to set in dest bitmap */
459 UINT lines, /* [in] Number of scan lines to copy */
460 LPVOID bits, /* [out] Address of array for bitmap bits */
461 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
462 UINT coloruse) /* [in] RGB or palette index */
466 PALETTEENTRY * palEntry;
467 PALETTEOBJ * palette;
471 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
472 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
474 GDI_ReleaseObj( hdc );
477 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC )))
479 GDI_ReleaseObj( hdc );
480 GDI_ReleaseObj( hbitmap );
484 /* Transfer color info */
486 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
488 info->bmiHeader.biClrUsed = 0;
490 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
491 palEntry = palette->logpalette.palPalEntry;
492 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
493 if (coloruse == DIB_RGB_COLORS) {
494 info->bmiColors[i].rgbRed = palEntry->peRed;
495 info->bmiColors[i].rgbGreen = palEntry->peGreen;
496 info->bmiColors[i].rgbBlue = palEntry->peBlue;
497 info->bmiColors[i].rgbReserved = 0;
499 else ((WORD *)info->bmiColors)[i] = (WORD)i;
502 switch (info->bmiHeader.biBitCount) {
504 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
505 info->bmiColors[0].rgbBlue = 0;
506 info->bmiColors[0].rgbReserved = 0;
507 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
508 info->bmiColors[1].rgbBlue = 0xff;
509 info->bmiColors[1].rgbReserved = 0;
513 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
521 memcpy(info->bmiColors, DefLogPalette,
522 10 * sizeof(RGBQUAD));
523 memcpy(info->bmiColors + 246, DefLogPalette + 10,
524 10 * sizeof(RGBQUAD));
525 color = info->bmiColors + 10;
526 for(r = 0; r <= 5; r++) /* FIXME */
527 for(g = 0; g <= 5; g++)
528 for(b = 0; b <= 5; b++) {
529 color->rgbRed = (r * 0xff) / 5;
530 color->rgbGreen = (g * 0xff) / 5;
531 color->rgbBlue = (b * 0xff) / 5;
532 color->rgbReserved = 0;
540 GDI_ReleaseObj( dc->hPalette );
544 /* If the bitmap object already have a dib section that contains image data, get the bits from it */
545 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
547 /*FIXME: Only RGB dibs supported for now */
548 unsigned int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
549 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
550 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
553 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
555 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
556 dstwidthb = -dstwidthb;
559 switch( info->bmiHeader.biBitCount ) {
562 case 16: /* 16 bpp dstDIB */
564 LPWORD dstbits = (LPWORD)dbits;
565 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
567 /* FIXME: BI_BITFIELDS not supported yet */
569 switch(bmp->dib->dsBm.bmBitsPixel) {
571 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
573 /* FIXME: BI_BITFIELDS not supported yet */
574 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
575 memcpy(dbits, sbits, srcwidthb);
579 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
581 LPBYTE srcbits = sbits;
583 for( y = 0; y < lines; y++) {
584 for( x = 0; x < srcwidth; x++, srcbits += 3)
585 *dstbits++ = ((srcbits[0] >> 3) & bmask) |
586 (((WORD)srcbits[1] << 2) & gmask) |
587 (((WORD)srcbits[2] << 7) & rmask);
589 dstbits = (LPWORD)(dbits+=dstwidthb);
590 srcbits = (sbits += srcwidthb);
595 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
597 LPDWORD srcbits = (LPDWORD)sbits;
600 for( y = 0; y < lines; y++) {
601 for( x = 0; x < srcwidth; x++ ) {
603 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
604 ((val >> 9) & rmask));
606 dstbits = (LPWORD)(dbits+=dstwidthb);
607 srcbits = (LPDWORD)(sbits+=srcwidthb);
612 default: /* ? bit bmp -> 16 bit DIB */
613 FIXME("15/16 bit DIB %d bit bitmap\n",
614 bmp->bitmap.bmBitsPixel);
620 case 24: /* 24 bpp dstDIB */
622 LPBYTE dstbits = dbits;
624 switch(bmp->dib->dsBm.bmBitsPixel) {
626 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
628 LPWORD srcbits = (LPWORD)sbits;
631 /* FIXME: BI_BITFIELDS not supported yet */
632 for( y = 0; y < lines; y++) {
633 for( x = 0; x < srcwidth; x++ ) {
635 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
636 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
637 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
639 dstbits = (LPBYTE)(dbits+=dstwidthb);
640 srcbits = (LPWORD)(sbits+=srcwidthb);
645 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
647 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
648 memcpy(dbits, sbits, srcwidthb);
652 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
654 LPBYTE srcbits = (LPBYTE)sbits;
656 for( y = 0; y < lines; y++) {
657 for( x = 0; x < srcwidth; x++, srcbits++ ) {
658 *dstbits++ = *srcbits++;
659 *dstbits++ = *srcbits++;
660 *dstbits++ = *srcbits++;
662 dstbits=(LPBYTE)(dbits+=dstwidthb);
663 srcbits = (LPBYTE)(sbits+=srcwidthb);
668 default: /* ? bit bmp -> 24 bit DIB */
669 FIXME("24 bit DIB %d bit bitmap\n",
670 bmp->bitmap.bmBitsPixel);
676 case 32: /* 32 bpp dstDIB */
678 LPDWORD dstbits = (LPDWORD)dbits;
680 /* FIXME: BI_BITFIELDS not supported yet */
682 switch(bmp->dib->dsBm.bmBitsPixel) {
683 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
685 LPWORD srcbits = (LPWORD)sbits;
688 /* FIXME: BI_BITFIELDS not supported yet */
689 for( y = 0; y < lines; y++) {
690 for( x = 0; x < srcwidth; x++ ) {
691 val = (DWORD)*srcbits++;
692 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
693 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
694 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
696 dstbits=(LPDWORD)(dbits+=dstwidthb);
697 srcbits=(LPWORD)(sbits+=srcwidthb);
702 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
704 LPBYTE srcbits = sbits;
706 for( y = 0; y < lines; y++) {
707 for( x = 0; x < srcwidth; x++, srcbits+=3 )
708 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
709 dstbits=(LPDWORD)(dbits+=dstwidthb);
710 srcbits=(sbits+=srcwidthb);
715 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
717 /* FIXME: BI_BITFIELDS not supported yet */
718 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
719 memcpy(dbits, sbits, srcwidthb);
723 default: /* ? bit bmp -> 16 bit DIB */
724 FIXME("15/16 bit DIB %d bit bitmap\n",
725 bmp->bitmap.bmBitsPixel);
731 default: /* ? bit DIB */
732 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
736 /* Otherwise, get bits from the XImage */
737 else if(!BITMAP_Driver->pGetDIBits(bmp, dc, startscan, lines, bits, info, coloruse, hbitmap))
739 GDI_ReleaseObj( hdc );
740 GDI_ReleaseObj( hbitmap );
745 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
747 /* fill in struct members */
749 if( info->bmiHeader.biBitCount == 0)
751 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
752 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
753 info->bmiHeader.biPlanes = 1;
754 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
755 info->bmiHeader.biSizeImage =
756 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
757 bmp->bitmap.bmHeight,
758 bmp->bitmap.bmBitsPixel );
759 info->bmiHeader.biCompression = 0;
763 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
764 info->bmiHeader.biWidth,
765 info->bmiHeader.biHeight,
766 info->bmiHeader.biBitCount );
770 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
771 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
772 info->bmiHeader.biHeight);
774 GDI_ReleaseObj( hdc );
775 GDI_ReleaseObj( hbitmap );
781 /***********************************************************************
782 * CreateDIBitmap (GDI.442)
784 HBITMAP16 WINAPI CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
785 DWORD init, LPCVOID bits, const BITMAPINFO * data,
788 return CreateDIBitmap( hdc, header, init, bits, data, coloruse );
792 /***********************************************************************
793 * CreateDIBitmap (GDI32.@)
795 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
796 DWORD init, LPCVOID bits, const BITMAPINFO *data,
806 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
807 if (height < 0) height = -height;
809 /* Check if we should create a monochrome or color bitmap. */
810 /* We create a monochrome bitmap only if it has exactly 2 */
811 /* colors, which are black followed by white, nothing else. */
812 /* In all other cases, we create a color bitmap. */
814 if (bpp != 1) fColor = TRUE;
815 else if ((coloruse != DIB_RGB_COLORS) ||
816 (init != CBM_INIT) || !data) fColor = FALSE;
819 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
821 RGBQUAD *rgb = data->bmiColors;
822 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
824 /* Check if the first color of the colormap is black */
825 if ((col == RGB(0,0,0)))
828 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
829 /* If the second color is white, create a monochrome bitmap */
830 fColor = (col != RGB(0xff,0xff,0xff));
832 /* Note : If the first color of the colormap is white
833 followed by black, we have to create a color bitmap.
834 If we don't the white will be displayed in black later on!*/
837 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
839 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
840 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
841 if ((col == RGB(0,0,0)))
844 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
845 fColor = (col != RGB(0xff,0xff,0xff));
849 else if (data->bmiHeader.biSize == sizeof(BITMAPV4HEADER))
850 { /* FIXME: correct ? */
851 RGBQUAD *rgb = data->bmiColors;
852 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
854 /* Check if the first color of the colormap is black */
855 if ((col == RGB(0,0,0)))
858 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
859 /* If the second color is white, create a monochrome bitmap */
860 fColor = (col != RGB(0xff,0xff,0xff));
862 /* Note : If the first color of the colormap is white
863 followed by black, we have to create a color bitmap.
864 If we don't the white will be displayed in black later on!*/
869 ERR("(%ld): wrong/unknown size for data\n",
870 data->bmiHeader.biSize );
875 /* Now create the bitmap */
878 handle = CreateBitmap( width, height, GetDeviceCaps( hdc, PLANES ),
879 GetDeviceCaps( hdc, BITSPIXEL ), NULL );
880 else handle = CreateBitmap( width, height, 1, 1, NULL );
882 if (!handle) return 0;
884 if (init == CBM_INIT)
885 SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
889 /***********************************************************************
890 * CreateDIBSection (GDI.489)
892 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
893 SEGPTR *bits16, HANDLE section, DWORD offset)
898 hbitmap = CreateDIBSection( hdc, bmi, usage, &bits32, section, offset );
901 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(hbitmap, BITMAP_MAGIC);
902 if (bmp && bmp->dib && bits32)
904 BITMAPINFOHEADER *bi = &bmi->bmiHeader;
905 INT height = bi->biHeight >= 0 ? bi->biHeight : -bi->biHeight;
906 INT width_bytes = DIB_GetDIBWidthBytes(bi->biWidth, bi->biBitCount);
907 INT size = (bi->biSizeImage && bi->biCompression != BI_RGB) ?
908 bi->biSizeImage : width_bytes * height;
910 WORD sel = SELECTOR_AllocBlock( bits32, size, WINE_LDT_FLAGS_DATA );
911 bmp->segptr_bits = MAKESEGPTR( sel, 0 );
912 if (bits16) *bits16 = bmp->segptr_bits;
914 if (bmp) GDI_ReleaseObj( hbitmap );
919 /***********************************************************************
920 * DIB_CreateDIBSection
922 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
923 LPVOID *bits, HANDLE section,
924 DWORD offset, DWORD ovr_pitch)
928 BOOL bDesktopDC = FALSE;
930 /* If the reference hdc is null, take the desktop dc */
933 hdc = CreateCompatibleDC(0);
937 if ((dc = DC_GetDCPtr( hdc )))
939 hbitmap = dc->funcs->pCreateDIBSection(dc, bmi, usage, bits, section, offset, ovr_pitch);
949 /***********************************************************************
950 * CreateDIBSection (GDI32.@)
952 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
953 LPVOID *bits, HANDLE section,
956 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
959 /***********************************************************************
960 * DIB_DeleteDIBSection
962 void DIB_DeleteDIBSection( BITMAPOBJ *bmp )
966 DIBSECTION *dib = bmp->dib;
968 if (dib->dsBm.bmBits)
972 SYSTEM_INFO SystemInfo;
973 GetSystemInfo( &SystemInfo );
974 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
975 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
977 else if (!dib->dsOffset)
978 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
981 BITMAP_Driver->pDeleteDIBSection(bmp);
983 HeapFree(GetProcessHeap(), 0, dib);
985 if (bmp->segptr_bits) SELECTOR_FreeBlock( SELECTOROF(bmp->segptr_bits) );
989 /***********************************************************************
990 * DIB_CreateDIBFromBitmap
991 * Allocates a packed DIB and copies the bitmap data into it.
993 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
995 BITMAPOBJ *pBmp = NULL;
996 HGLOBAL hPackedDIB = 0;
997 LPBYTE pPackedDIB = NULL;
998 LPBITMAPINFOHEADER pbmiHeader = NULL;
999 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
1000 OffsetBits = 0, nLinesCopied = 0;
1002 /* Get a pointer to the BITMAPOBJ structure */
1003 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
1004 if (!pBmp) return hPackedDIB;
1006 /* Get the bitmap dimensions */
1007 width = pBmp->bitmap.bmWidth;
1008 height = pBmp->bitmap.bmHeight;
1009 depth = pBmp->bitmap.bmBitsPixel;
1012 * A packed DIB contains a BITMAPINFO structure followed immediately by
1013 * an optional color palette and the pixel data.
1016 /* Calculate the size of the packed DIB */
1017 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
1018 cPackedSize = sizeof(BITMAPINFOHEADER)
1019 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
1021 /* Get the offset to the bits */
1022 OffsetBits = cPackedSize - cDataSize;
1024 /* Allocate the packed DIB */
1025 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
1026 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
1030 WARN("Could not allocate packed DIB!\n");
1034 /* A packed DIB starts with a BITMAPINFOHEADER */
1035 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
1036 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
1038 /* Init the BITMAPINFOHEADER */
1039 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
1040 pbmiHeader->biWidth = width;
1041 pbmiHeader->biHeight = height;
1042 pbmiHeader->biPlanes = 1;
1043 pbmiHeader->biBitCount = depth;
1044 pbmiHeader->biCompression = BI_RGB;
1045 pbmiHeader->biSizeImage = 0;
1046 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
1047 pbmiHeader->biClrUsed = 0;
1048 pbmiHeader->biClrImportant = 0;
1050 /* Retrieve the DIB bits from the bitmap and fill in the
1051 * DIB color table if present */
1053 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
1054 hBmp, /* Handle to bitmap */
1055 0, /* First scan line to set in dest bitmap */
1056 height, /* Number of scan lines to copy */
1057 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
1058 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
1059 0); /* RGB or palette index */
1060 GlobalUnlock(hPackedDIB);
1062 /* Cleanup if GetDIBits failed */
1063 if (nLinesCopied != height)
1065 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
1066 GlobalFree(hPackedDIB);
1071 GDI_ReleaseObj( hBmp );