Removed unnecessary X11 includes from ddraw.h.
[wine/multimedia.git] / objects / dib.c
blob4947f122d97bc5f8139cee44d17f6ba1f0776ea1
1 /*
2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
6 */
8 #include "winbase.h"
9 #include "bitmap.h"
10 #include "callback.h"
11 #include "dc.h"
12 #include "debugtools.h"
13 #include "palette.h"
15 DEFAULT_DEBUG_CHANNEL(bitmap);
17 /***********************************************************************
18 * DIB_GetDIBWidthBytes
20 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
21 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
23 int DIB_GetDIBWidthBytes( int width, int depth )
25 int words;
27 switch(depth)
29 case 1: words = (width + 31) / 32; break;
30 case 4: words = (width + 7) / 8; break;
31 case 8: words = (width + 3) / 4; break;
32 case 15:
33 case 16: words = (width + 1) / 2; break;
34 case 24: words = (width * 3 + 3)/4; break;
36 default:
37 WARN("(%d): Unsupported depth\n", depth );
38 /* fall through */
39 case 32:
40 words = width;
42 return 4 * words;
45 /***********************************************************************
46 * DIB_GetDIBImageBytes
48 * Return the number of bytes used to hold the image in a DIB bitmap.
50 int DIB_GetDIBImageBytes( int width, int height, int depth )
52 return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
56 /***********************************************************************
57 * DIB_BitmapInfoSize
59 * Return the size of the bitmap info structure including color table.
61 int DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
63 int colors;
65 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
67 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
68 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
69 return sizeof(BITMAPCOREHEADER) + colors *
70 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
72 else /* assume BITMAPINFOHEADER */
74 colors = info->bmiHeader.biClrUsed;
75 if (!colors && (info->bmiHeader.biBitCount <= 8))
76 colors = 1 << info->bmiHeader.biBitCount;
77 return sizeof(BITMAPINFOHEADER) + colors *
78 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
83 /***********************************************************************
84 * DIB_GetBitmapInfo
86 * Get the info from a bitmap header.
87 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
89 int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
90 int *height, WORD *bpp, WORD *compr )
92 if (header->biSize == sizeof(BITMAPINFOHEADER))
94 *width = header->biWidth;
95 *height = header->biHeight;
96 *bpp = header->biBitCount;
97 *compr = header->biCompression;
98 return 1;
100 if (header->biSize == sizeof(BITMAPCOREHEADER))
102 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
103 *width = core->bcWidth;
104 *height = core->bcHeight;
105 *bpp = core->bcBitCount;
106 *compr = 0;
107 return 0;
109 WARN("(%ld): wrong size for header\n", header->biSize );
110 return -1;
114 /***********************************************************************
115 * StretchDIBits16 (GDI.439)
117 INT16 WINAPI StretchDIBits16(HDC16 hdc, INT16 xDst, INT16 yDst, INT16 widthDst,
118 INT16 heightDst, INT16 xSrc, INT16 ySrc, INT16 widthSrc,
119 INT16 heightSrc, const VOID *bits,
120 const BITMAPINFO *info, UINT16 wUsage, DWORD dwRop )
122 return (INT16)StretchDIBits( hdc, xDst, yDst, widthDst, heightDst,
123 xSrc, ySrc, widthSrc, heightSrc, bits,
124 info, wUsage, dwRop );
128 /***********************************************************************
129 * StretchDIBits (GDI32.351)
131 INT WINAPI StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
132 INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
133 INT heightSrc, const void *bits,
134 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
136 DC *dc = DC_GetDCPtr( hdc );
137 if(!dc) return FALSE;
139 if(dc->funcs->pStretchDIBits)
140 return dc->funcs->pStretchDIBits(dc, xDst, yDst, widthDst,
141 heightDst, xSrc, ySrc, widthSrc,
142 heightSrc, bits, info, wUsage,
143 dwRop);
144 else { /* use StretchBlt */
145 HBITMAP hBitmap, hOldBitmap;
146 HDC hdcMem;
148 hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
149 bits, info, wUsage );
150 hdcMem = CreateCompatibleDC( hdc );
151 hOldBitmap = SelectObject( hdcMem, hBitmap );
152 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
153 left (negative biHeight) */
154 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
155 hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
156 widthSrc, heightSrc, dwRop );
157 SelectObject( hdcMem, hOldBitmap );
158 DeleteDC( hdcMem );
159 DeleteObject( hBitmap );
160 return heightSrc;
165 /***********************************************************************
166 * SetDIBits16 (GDI.440)
168 INT16 WINAPI SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
169 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
170 UINT16 coloruse )
172 return SetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
176 /******************************************************************************
177 * SetDIBits [GDI32.312] Sets pixels in a bitmap using colors from DIB
179 * PARAMS
180 * hdc [I] Handle to device context
181 * hbitmap [I] Handle to bitmap
182 * startscan [I] Starting scan line
183 * lines [I] Number of scan lines
184 * bits [I] Array of bitmap bits
185 * info [I] Address of structure with data
186 * coloruse [I] Type of color indexes to use
188 * RETURNS
189 * Success: Number of scan lines copied
190 * Failure: 0
192 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
193 UINT lines, LPCVOID bits, const BITMAPINFO *info,
194 UINT coloruse )
196 DC *dc;
197 BITMAPOBJ *bitmap;
198 INT result;
200 /* Check parameters */
201 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
202 if (!dc)
204 dc = (DC *) GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
205 if (!dc) return 0;
208 if (!(bitmap = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
210 GDI_HEAP_UNLOCK( hdc );
211 return 0;
214 result = BITMAP_Driver->pSetDIBits(bitmap, dc, startscan,
215 lines, bits, info,
216 coloruse, hbitmap);
218 GDI_HEAP_UNLOCK( hdc );
219 GDI_HEAP_UNLOCK( hbitmap );
221 return result;
225 /***********************************************************************
226 * SetDIBitsToDevice16 (GDI.443)
228 INT16 WINAPI SetDIBitsToDevice16(HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
229 INT16 cy, INT16 xSrc, INT16 ySrc, UINT16 startscan,
230 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
231 UINT16 coloruse )
233 return SetDIBitsToDevice( hdc, xDest, yDest, cx, cy, xSrc, ySrc,
234 startscan, lines, bits, info, coloruse );
238 /***********************************************************************
239 * SetDIBitsToDevice (GDI32.313)
241 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
242 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
243 UINT lines, LPCVOID bits, const BITMAPINFO *info,
244 UINT coloruse )
246 INT ret;
247 DC *dc;
249 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
251 if(dc->funcs->pSetDIBitsToDevice)
252 ret = dc->funcs->pSetDIBitsToDevice( dc, xDest, yDest, cx, cy, xSrc,
253 ySrc, startscan, lines, bits,
254 info, coloruse );
255 else {
256 FIXME("unimplemented on hdc %08x\n", hdc);
257 ret = 0;
260 GDI_HEAP_UNLOCK( hdc );
261 return ret;
264 /***********************************************************************
265 * SetDIBColorTable16 (GDI.602)
267 UINT16 WINAPI SetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
268 RGBQUAD *colors )
270 return SetDIBColorTable( hdc, startpos, entries, colors );
273 /***********************************************************************
274 * SetDIBColorTable (GDI32.311)
276 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
277 RGBQUAD *colors )
279 DC * dc;
280 PALETTEENTRY * palEntry;
281 PALETTEOBJ * palette;
282 RGBQUAD *end;
284 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
285 if (!dc)
287 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
288 if (!dc) return 0;
291 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
293 return 0;
296 /* Transfer color info */
298 if (dc->w.bitsPerPixel <= 8) {
299 palEntry = palette->logpalette.palPalEntry + startpos;
300 if (startpos + entries > (1 << dc->w.bitsPerPixel))
301 entries = (1 << dc->w.bitsPerPixel) - startpos;
303 if (startpos + entries > palette->logpalette.palNumEntries)
304 entries = palette->logpalette.palNumEntries - startpos;
306 for (end = colors + entries; colors < end; palEntry++, colors++)
308 palEntry->peRed = colors->rgbRed;
309 palEntry->peGreen = colors->rgbGreen;
310 palEntry->peBlue = colors->rgbBlue;
312 } else {
313 entries = 0;
315 GDI_HEAP_UNLOCK( dc->w.hPalette );
316 return entries;
319 /***********************************************************************
320 * GetDIBColorTable16 (GDI.603)
322 UINT16 WINAPI GetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
323 RGBQUAD *colors )
325 return GetDIBColorTable( hdc, startpos, entries, colors );
328 /***********************************************************************
329 * GetDIBColorTable (GDI32.169)
331 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
332 RGBQUAD *colors )
334 DC * dc;
335 PALETTEENTRY * palEntry;
336 PALETTEOBJ * palette;
337 RGBQUAD *end;
339 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
340 if (!dc)
342 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
343 if (!dc) return 0;
346 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
348 return 0;
351 /* Transfer color info */
353 if (dc->w.bitsPerPixel <= 8) {
354 palEntry = palette->logpalette.palPalEntry + startpos;
355 if (startpos + entries > (1 << dc->w.bitsPerPixel)) {
356 entries = (1 << dc->w.bitsPerPixel) - startpos;
358 for (end = colors + entries; colors < end; palEntry++, colors++)
360 colors->rgbRed = palEntry->peRed;
361 colors->rgbGreen = palEntry->peGreen;
362 colors->rgbBlue = palEntry->peBlue;
363 colors->rgbReserved = 0;
365 } else {
366 entries = 0;
368 GDI_HEAP_UNLOCK( dc->w.hPalette );
369 return entries;
372 /* FIXME the following two structs should be combined with __sysPalTemplate in
373 objects/color.c - this should happen after de-X11-ing both of these
374 files.
375 NB. RGBQUAD and PALETTENTRY have different orderings of red, green
376 and blue - sigh */
378 static RGBQUAD EGAColors[16] = {
379 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
380 { 0x00, 0x00, 0x00, 0x00 },
381 { 0x00, 0x00, 0x80, 0x00 },
382 { 0x00, 0x80, 0x00, 0x00 },
383 { 0x00, 0x80, 0x80, 0x00 },
384 { 0x80, 0x00, 0x00, 0x00 },
385 { 0x80, 0x00, 0x80, 0x00 },
386 { 0x80, 0x80, 0x00, 0x00 },
387 { 0x80, 0x80, 0x80, 0x00 },
388 { 0xc0, 0xc0, 0xc0, 0x00 },
389 { 0x00, 0x00, 0xff, 0x00 },
390 { 0x00, 0xff, 0x00, 0x00 },
391 { 0x00, 0xff, 0xff, 0x00 },
392 { 0xff, 0x00, 0x00, 0x00 },
393 { 0xff, 0x00, 0xff, 0x00 },
394 { 0xff, 0xff, 0x00, 0x00 },
395 { 0xff, 0xff, 0xff, 0x00 }
399 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
400 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
401 { 0x00, 0x00, 0x00, 0x00 },
402 { 0x00, 0x00, 0x80, 0x00 },
403 { 0x00, 0x80, 0x00, 0x00 },
404 { 0x00, 0x80, 0x80, 0x00 },
405 { 0x80, 0x00, 0x00, 0x00 },
406 { 0x80, 0x00, 0x80, 0x00 },
407 { 0x80, 0x80, 0x00, 0x00 },
408 { 0xc0, 0xc0, 0xc0, 0x00 },
409 { 0xc0, 0xdc, 0xc0, 0x00 },
410 { 0xf0, 0xca, 0xa6, 0x00 },
411 { 0xf0, 0xfb, 0xff, 0x00 },
412 { 0xa4, 0xa0, 0xa0, 0x00 },
413 { 0x80, 0x80, 0x80, 0x00 },
414 { 0x00, 0x00, 0xf0, 0x00 },
415 { 0x00, 0xff, 0x00, 0x00 },
416 { 0x00, 0xff, 0xff, 0x00 },
417 { 0xff, 0x00, 0x00, 0x00 },
418 { 0xff, 0x00, 0xff, 0x00 },
419 { 0xff, 0xff, 0x00, 0x00 },
420 { 0xff, 0xff, 0xff, 0x00 }
423 /***********************************************************************
424 * GetDIBits16 (GDI.441)
426 INT16 WINAPI GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
427 UINT16 lines, LPVOID bits, BITMAPINFO * info,
428 UINT16 coloruse )
430 return GetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
434 /******************************************************************************
435 * GetDIBits [GDI32.170] Retrieves bits of bitmap and copies to buffer
437 * RETURNS
438 * Success: Number of scan lines copied from bitmap
439 * Failure: 0
441 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
443 INT WINAPI GetDIBits(
444 HDC hdc, /* [in] Handle to device context */
445 HBITMAP hbitmap, /* [in] Handle to bitmap */
446 UINT startscan, /* [in] First scan line to set in dest bitmap */
447 UINT lines, /* [in] Number of scan lines to copy */
448 LPVOID bits, /* [out] Address of array for bitmap bits */
449 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
450 UINT coloruse) /* [in] RGB or palette index */
452 DC * dc;
453 BITMAPOBJ * bmp;
454 PALETTEENTRY * palEntry;
455 PALETTEOBJ * palette;
456 int i;
458 if (!info) return 0;
459 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
460 if (!dc)
462 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
463 if (!dc) return 0;
465 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
467 GDI_HEAP_UNLOCK( hdc );
468 return 0;
470 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
472 GDI_HEAP_UNLOCK( hdc );
473 GDI_HEAP_UNLOCK( hbitmap );
474 return 0;
477 /* Transfer color info */
479 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
481 info->bmiHeader.biClrUsed = 0;
483 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
484 palEntry = palette->logpalette.palPalEntry;
485 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
486 if (coloruse == DIB_RGB_COLORS) {
487 info->bmiColors[i].rgbRed = palEntry->peRed;
488 info->bmiColors[i].rgbGreen = palEntry->peGreen;
489 info->bmiColors[i].rgbBlue = palEntry->peBlue;
490 info->bmiColors[i].rgbReserved = 0;
492 else ((WORD *)info->bmiColors)[i] = (WORD)i;
494 } else {
495 switch (info->bmiHeader.biBitCount) {
496 case 1:
497 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
498 info->bmiColors[0].rgbBlue = 0;
499 info->bmiColors[0].rgbReserved = 0;
500 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
501 info->bmiColors[1].rgbBlue = 0xff;
502 info->bmiColors[1].rgbReserved = 0;
503 break;
505 case 4:
506 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
507 break;
509 case 8:
511 INT r, g, b;
512 RGBQUAD *color;
514 memcpy(info->bmiColors, DefLogPalette,
515 10 * sizeof(RGBQUAD));
516 memcpy(info->bmiColors + 246, DefLogPalette + 10,
517 10 * sizeof(RGBQUAD));
518 color = info->bmiColors + 10;
519 for(r = 0; r <= 5; r++) /* FIXME */
520 for(g = 0; g <= 5; g++)
521 for(b = 0; b <= 5; b++) {
522 color->rgbRed = (r * 0xff) / 5;
523 color->rgbGreen = (g * 0xff) / 5;
524 color->rgbBlue = (b * 0xff) / 5;
525 color->rgbReserved = 0;
526 color++;
533 GDI_HEAP_UNLOCK( dc->w.hPalette );
535 if (bits && lines)
537 /* If the bitmap object already have a dib section that contains image data, get the bits from it*/
538 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
540 /*FIXME: Only RGB dibs supported for now */
541 int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
542 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
543 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
544 int x, y;
546 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
548 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
549 dstwidthb = -dstwidthb;
552 switch( info->bmiHeader.biBitCount ) {
554 case 15:
555 case 16: /* 16 bpp dstDIB */
557 LPWORD dstbits = (LPWORD)dbits;
558 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
560 /* FIXME: BI_BITFIELDS not supported yet */
562 switch(bmp->dib->dsBm.bmBitsPixel) {
564 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
566 /* FIXME: BI_BITFIELDS not supported yet */
567 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
568 memcpy(dbits, sbits, srcwidthb);
570 break;
572 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
574 LPBYTE srcbits = sbits;
576 for( y = 0; y < lines; y++) {
577 for( x = 0; x < srcwidth; x++ )
578 *dstbits++ = ((*srcbits++ >> 3) & bmask) |
579 (((WORD)*srcbits++ << 2) & gmask) |
580 (((WORD)*srcbits++ << 7) & rmask);
581 dstbits = (LPWORD)(dbits+=dstwidthb);
582 srcbits = (sbits += srcwidthb);
585 break;
587 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
589 LPDWORD srcbits = (LPDWORD)sbits;
590 DWORD val;
592 for( y = 0; y < lines; y++) {
593 for( x = 0; x < srcwidth; x++ ) {
594 val = *srcbits++;
595 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
596 ((val >> 9) & rmask));
598 dstbits = (LPWORD)(dbits+=dstwidthb);
599 srcbits = (LPDWORD)(sbits+=srcwidthb);
602 break;
604 default: /* ? bit bmp -> 16 bit DIB */
605 FIXME("15/16 bit DIB %d bit bitmap\n",
606 bmp->bitmap.bmBitsPixel);
607 break;
610 break;
612 case 24: /* 24 bpp dstDIB */
614 LPBYTE dstbits = dbits;
616 switch(bmp->dib->dsBm.bmBitsPixel) {
618 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
620 LPWORD srcbits = (LPWORD)sbits;
621 WORD val;
623 /* FIXME: BI_BITFIELDS not supported yet */
624 for( y = 0; y < lines; y++) {
625 for( x = 0; x < srcwidth; x++ ) {
626 val = *srcbits++;
627 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
628 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
629 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
631 dstbits = (LPBYTE)(dbits+=dstwidthb);
632 srcbits = (LPWORD)(sbits+=srcwidthb);
635 break;
637 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
639 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
640 memcpy(dbits, sbits, srcwidthb);
642 break;
644 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
646 LPBYTE srcbits = (LPBYTE)sbits;
648 for( y = 0; y < lines; y++) {
649 for( x = 0; x < srcwidth; x++, srcbits++ ) {
650 *dstbits++ = *srcbits++;
651 *dstbits++ = *srcbits++;
652 *dstbits++ = *srcbits++;
654 dstbits=(LPBYTE)(dbits+=dstwidthb);
655 srcbits = (LPBYTE)(sbits+=srcwidthb);
658 break;
660 default: /* ? bit bmp -> 24 bit DIB */
661 FIXME("24 bit DIB %d bit bitmap\n",
662 bmp->bitmap.bmBitsPixel);
663 break;
666 break;
668 case 32: /* 32 bpp dstDIB */
670 LPDWORD dstbits = (LPDWORD)dbits;
672 /* FIXME: BI_BITFIELDS not supported yet */
674 switch(bmp->dib->dsBm.bmBitsPixel) {
675 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
677 LPWORD srcbits = (LPWORD)sbits;
678 DWORD val;
680 /* FIXME: BI_BITFIELDS not supported yet */
681 for( y = 0; y < lines; y++) {
682 for( x = 0; x < srcwidth; x++ ) {
683 val = (DWORD)*srcbits++;
684 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
685 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
686 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
688 dstbits=(LPDWORD)(dbits+=dstwidthb);
689 srcbits=(LPWORD)(sbits+=srcwidthb);
692 break;
694 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
696 LPBYTE srcbits = sbits;
698 for( y = 0; y < lines; y++) {
699 for( x = 0; x < srcwidth; x++, srcbits+=3 )
700 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
701 dstbits=(LPDWORD)(dbits+=dstwidthb);
702 srcbits=(sbits+=srcwidthb);
705 break;
707 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
709 /* FIXME: BI_BITFIELDS not supported yet */
710 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
711 memcpy(dbits, sbits, srcwidthb);
713 break;
715 default: /* ? bit bmp -> 16 bit DIB */
716 FIXME("15/16 bit DIB %d bit bitmap\n",
717 bmp->bitmap.bmBitsPixel);
718 break;
721 break;
723 default: /* ? bit DIB */
724 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
725 break;
728 /* Otherwise, get bits from the XImage */
729 else if(!BITMAP_Driver->pGetDIBits(bmp, dc, startscan, lines, bits, info, coloruse, hbitmap))
731 GDI_HEAP_UNLOCK( hdc );
732 GDI_HEAP_UNLOCK( hbitmap );
734 return FALSE;
737 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
739 /* fill in struct members */
741 if( info->bmiHeader.biBitCount == 0)
743 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
744 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
745 info->bmiHeader.biPlanes = 1;
746 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
747 info->bmiHeader.biSizeImage =
748 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
749 bmp->bitmap.bmHeight,
750 bmp->bitmap.bmBitsPixel );
751 info->bmiHeader.biCompression = 0;
753 else
755 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
756 info->bmiHeader.biWidth,
757 info->bmiHeader.biHeight,
758 info->bmiHeader.biBitCount );
762 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
763 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
764 info->bmiHeader.biHeight);
766 GDI_HEAP_UNLOCK( hdc );
767 GDI_HEAP_UNLOCK( hbitmap );
769 return lines;
773 /***********************************************************************
774 * CreateDIBitmap16 (GDI.442)
776 HBITMAP16 WINAPI CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
777 DWORD init, LPCVOID bits, const BITMAPINFO * data,
778 UINT16 coloruse )
780 return CreateDIBitmap( hdc, header, init, bits, data, coloruse );
784 /***********************************************************************
785 * CreateDIBitmap (GDI32.37)
787 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
788 DWORD init, LPCVOID bits, const BITMAPINFO *data,
789 UINT coloruse )
791 HBITMAP handle;
792 BOOL fColor;
793 DWORD width;
794 int height;
795 WORD bpp;
796 WORD compr;
798 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
799 if (height < 0) height = -height;
801 /* Check if we should create a monochrome or color bitmap. */
802 /* We create a monochrome bitmap only if it has exactly 2 */
803 /* colors, which are black followed by white, nothing else. */
804 /* In all other cases, we create a color bitmap. */
806 if (bpp != 1) fColor = TRUE;
807 else if ((coloruse != DIB_RGB_COLORS) ||
808 (init != CBM_INIT) || !data) fColor = FALSE;
809 else
811 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
813 RGBQUAD *rgb = data->bmiColors;
814 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
816 /* Check if the first color of the colormap is black */
817 if ((col == RGB(0,0,0)))
819 rgb++;
820 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
821 /* If the second color is white, create a monochrome bitmap */
822 fColor = (col != RGB(0xff,0xff,0xff));
824 /* Note : If the first color of the colormap is white
825 followed by black, we have to create a color bitmap.
826 If we don't the white will be displayed in black later on!*/
827 else fColor = TRUE;
829 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
831 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
832 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
833 if ((col == RGB(0,0,0)))
835 rgb++;
836 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
837 fColor = (col != RGB(0xff,0xff,0xff));
839 else fColor = TRUE;
841 else
843 WARN("(%ld): wrong size for data\n",
844 data->bmiHeader.biSize );
845 return 0;
849 /* Now create the bitmap */
851 if (fColor)
853 HDC tmpdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
854 handle = CreateCompatibleBitmap( tmpdc, width, height );
855 DeleteDC( tmpdc );
857 else handle = CreateBitmap( width, height, 1, 1, NULL );
859 if (!handle) return 0;
861 if (init == CBM_INIT)
862 SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
863 return handle;
866 /***********************************************************************
867 * CreateDIBSection16 (GDI.489)
869 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
870 SEGPTR *bits, HANDLE section,
871 DWORD offset)
873 HBITMAP16 hbitmap;
874 DC *dc;
875 BOOL bDesktopDC = FALSE;
877 /* If the reference hdc is null, take the desktop dc */
878 if (hdc == 0)
880 hdc = CreateCompatibleDC(0);
881 bDesktopDC = TRUE;
884 dc = (DC *) GDI_GetObjPtr(hdc, DC_MAGIC);
885 if(!dc) dc = (DC *) GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
886 if(!dc) return (HBITMAP16) NULL;
888 hbitmap = dc->funcs->pCreateDIBSection16(dc, bmi, usage, bits, section, offset, 0);
890 GDI_HEAP_UNLOCK(hdc);
892 if (bDesktopDC)
893 DeleteDC(hdc);
895 return hbitmap;
898 /***********************************************************************
899 * DIB_CreateDIBSection
901 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
902 LPVOID *bits, HANDLE section,
903 DWORD offset, DWORD ovr_pitch)
905 HBITMAP hbitmap;
906 DC *dc;
907 BOOL bDesktopDC = FALSE;
909 /* If the reference hdc is null, take the desktop dc */
910 if (hdc == 0)
912 hdc = CreateCompatibleDC(0);
913 bDesktopDC = TRUE;
916 dc = (DC *) GDI_GetObjPtr(hdc, DC_MAGIC);
917 if(!dc) dc = (DC *) GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
918 if(!dc) return (HBITMAP) NULL;
920 hbitmap = dc->funcs->pCreateDIBSection(dc, bmi, usage, bits, section, offset, ovr_pitch);
922 GDI_HEAP_UNLOCK(hdc);
924 if (bDesktopDC)
925 DeleteDC(hdc);
927 return hbitmap;
930 /***********************************************************************
931 * CreateDIBSection (GDI32.36)
933 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
934 LPVOID *bits, HANDLE section,
935 DWORD offset)
937 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
940 /***********************************************************************
941 * DIB_DeleteDIBSection
943 void DIB_DeleteDIBSection( BITMAPOBJ *bmp )
945 if (bmp && bmp->dib)
947 DIBSECTION *dib = bmp->dib;
949 if (dib->dsBm.bmBits)
951 if (dib->dshSection)
952 UnmapViewOfFile(dib->dsBm.bmBits);
953 else if (!dib->dsOffset)
954 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
957 BITMAP_Driver->pDeleteDIBSection(bmp);
959 HeapFree(GetProcessHeap(), 0, dib);
960 bmp->dib = NULL;
964 /***********************************************************************
965 * DIB_CreateDIBFromBitmap
966 * Allocates a packed DIB and copies the bitmap data into it.
968 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
970 BITMAPOBJ *pBmp = NULL;
971 HGLOBAL hPackedDIB = 0;
972 LPBYTE pPackedDIB = NULL;
973 LPBITMAPINFOHEADER pbmiHeader = NULL;
974 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
975 OffsetBits = 0, nLinesCopied = 0;
977 /* Get a pointer to the BITMAPOBJ structure */
978 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
980 /* Get the bitmap dimensions */
981 width = pBmp->bitmap.bmWidth;
982 height = pBmp->bitmap.bmHeight;
983 depth = pBmp->bitmap.bmBitsPixel;
986 * A packed DIB contains a BITMAPINFO structure followed immediately by
987 * an optional color palette and the pixel data.
990 /* Calculate the size of the packed DIB */
991 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
992 cPackedSize = sizeof(BITMAPINFOHEADER)
993 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
994 + cDataSize;
995 /* Get the offset to the bits */
996 OffsetBits = cPackedSize - cDataSize;
998 /* Allocate the packed DIB */
999 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
1000 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
1001 cPackedSize );
1002 if ( !hPackedDIB )
1004 WARN("Could not allocate packed DIB!\n");
1005 goto END;
1008 /* A packed DIB starts with a BITMAPINFOHEADER */
1009 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
1010 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
1012 /* Init the BITMAPINFOHEADER */
1013 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
1014 pbmiHeader->biWidth = width;
1015 pbmiHeader->biHeight = height;
1016 pbmiHeader->biPlanes = 1;
1017 pbmiHeader->biBitCount = depth;
1018 pbmiHeader->biCompression = BI_RGB;
1019 pbmiHeader->biSizeImage = 0;
1020 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
1021 pbmiHeader->biClrUsed = 0;
1022 pbmiHeader->biClrImportant = 0;
1024 /* Retrieve the DIB bits from the bitmap and fill in the
1025 * DIB color table if present */
1027 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
1028 hBmp, /* Handle to bitmap */
1029 0, /* First scan line to set in dest bitmap */
1030 height, /* Number of scan lines to copy */
1031 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
1032 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
1033 0); /* RGB or palette index */
1034 GlobalUnlock(hPackedDIB);
1036 /* Cleanup if GetDIBits failed */
1037 if (nLinesCopied != height)
1039 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
1040 GlobalFree(hPackedDIB);
1041 hPackedDIB = 0;
1044 END:
1045 return hPackedDIB;