Do not set EH_NONCONTINUABLE.
[wine/dcerpc.git] / objects / dib.c
blobd133f602b4c28a090b6bfdd64605e3cee1f9e5a6
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 "monitor.h"
14 #include "palette.h"
16 DEFAULT_DEBUG_CHANNEL(bitmap)
18 /***********************************************************************
19 * DIB_GetDIBWidthBytes
21 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
22 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
24 int DIB_GetDIBWidthBytes( int width, int depth )
26 int words;
28 switch(depth)
30 case 1: words = (width + 31) / 32; break;
31 case 4: words = (width + 7) / 8; break;
32 case 8: words = (width + 3) / 4; break;
33 case 15:
34 case 16: words = (width + 1) / 2; break;
35 case 24: words = (width * 3 + 3)/4; break;
37 default:
38 WARN("(%d): Unsupported depth\n", depth );
39 /* fall through */
40 case 32:
41 words = width;
43 return 4 * words;
46 /***********************************************************************
47 * DIB_GetDIBImageBytes
49 * Return the number of bytes used to hold the image in a DIB bitmap.
51 int DIB_GetDIBImageBytes( int width, int height, int depth )
53 return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
57 /***********************************************************************
58 * DIB_BitmapInfoSize
60 * Return the size of the bitmap info structure including color table.
62 int DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
64 int colors;
66 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
68 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
69 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
70 return sizeof(BITMAPCOREHEADER) + colors *
71 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
73 else /* assume BITMAPINFOHEADER */
75 colors = info->bmiHeader.biClrUsed;
76 if (!colors && (info->bmiHeader.biBitCount <= 8))
77 colors = 1 << info->bmiHeader.biBitCount;
78 return sizeof(BITMAPINFOHEADER) + colors *
79 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
84 /***********************************************************************
85 * DIB_GetBitmapInfo
87 * Get the info from a bitmap header.
88 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
90 int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
91 int *height, WORD *bpp, WORD *compr )
93 if (header->biSize == sizeof(BITMAPINFOHEADER))
95 *width = header->biWidth;
96 *height = header->biHeight;
97 *bpp = header->biBitCount;
98 *compr = header->biCompression;
99 return 1;
101 if (header->biSize == sizeof(BITMAPCOREHEADER))
103 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
104 *width = core->bcWidth;
105 *height = core->bcHeight;
106 *bpp = core->bcBitCount;
107 *compr = 0;
108 return 0;
110 WARN("(%ld): wrong size for header\n", header->biSize );
111 return -1;
115 /***********************************************************************
116 * StretchDIBits16 (GDI.439)
118 INT16 WINAPI StretchDIBits16(HDC16 hdc, INT16 xDst, INT16 yDst, INT16 widthDst,
119 INT16 heightDst, INT16 xSrc, INT16 ySrc, INT16 widthSrc,
120 INT16 heightSrc, const VOID *bits,
121 const BITMAPINFO *info, UINT16 wUsage, DWORD dwRop )
123 return (INT16)StretchDIBits( hdc, xDst, yDst, widthDst, heightDst,
124 xSrc, ySrc, widthSrc, heightSrc, bits,
125 info, wUsage, dwRop );
129 /***********************************************************************
130 * StretchDIBits32 (GDI32.351)
132 INT WINAPI StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
133 INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
134 INT heightSrc, const void *bits,
135 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
137 DC *dc = DC_GetDCPtr( hdc );
138 if(!dc) return FALSE;
140 if(dc->funcs->pStretchDIBits)
141 return dc->funcs->pStretchDIBits(dc, xDst, yDst, widthDst,
142 heightDst, xSrc, ySrc, widthSrc,
143 heightSrc, bits, info, wUsage,
144 dwRop);
145 else { /* use StretchBlt32 */
146 HBITMAP hBitmap, hOldBitmap;
147 HDC hdcMem;
149 hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
150 bits, info, wUsage );
151 hdcMem = CreateCompatibleDC( hdc );
152 hOldBitmap = SelectObject( hdcMem, hBitmap );
153 /* Origin for DIBitmap is bottom left ! */
154 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
155 hdcMem, xSrc, 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 * SetDIBits32 [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 * SetDIBitsToDevice32 (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 * SetDIBColorTable32 (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 * GetDIBColorTable32 (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 * GetDIBits32 [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 = 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 * CreateDIBitmap32 (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 either black or 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 );
815 if ((col == RGB(0,0,0)) || (col == RGB(0xff,0xff,0xff)))
817 rgb++;
818 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
819 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
821 else fColor = TRUE;
823 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
825 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
826 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
827 if ((col == RGB(0,0,0)) || (col == RGB(0xff,0xff,0xff)))
829 rgb++;
830 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
831 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
833 else fColor = TRUE;
835 else
837 WARN("(%ld): wrong size for data\n",
838 data->bmiHeader.biSize );
839 return 0;
843 /* Now create the bitmap */
845 handle = fColor ? CreateBitmap( width, height, 1, MONITOR_GetDepth(&MONITOR_PrimaryMonitor), NULL ) :
846 CreateBitmap( width, height, 1, 1, NULL );
847 if (!handle) return 0;
849 if (init == CBM_INIT)
850 SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
851 return handle;
854 /***********************************************************************
855 * CreateDIBSection16 (GDI.489)
857 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
858 SEGPTR *bits, HANDLE section,
859 DWORD offset)
861 HBITMAP16 hbitmap;
862 DC *dc = (DC *) GDI_GetObjPtr(hdc, DC_MAGIC);
863 if(!dc) dc = (DC *) GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
864 if(!dc) return (HBITMAP16) NULL;
866 hbitmap = dc->funcs->pCreateDIBSection16(dc, bmi, usage, bits, section, offset);
868 GDI_HEAP_UNLOCK(hdc);
870 return hbitmap;
873 /***********************************************************************
874 * CreateDIBSection32 (GDI32.36)
876 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
877 LPVOID *bits, HANDLE section,
878 DWORD offset)
880 HBITMAP hbitmap;
881 DC *dc = (DC *) GDI_GetObjPtr(hdc, DC_MAGIC);
882 if(!dc) dc = (DC *) GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
883 if(!dc) return (HBITMAP) NULL;
885 hbitmap = dc->funcs->pCreateDIBSection(dc, bmi, usage, bits, section, offset);
887 GDI_HEAP_UNLOCK(hdc);
889 return hbitmap;
892 /***********************************************************************
893 * DIB_DeleteDIBSection
895 void DIB_DeleteDIBSection( BITMAPOBJ *bmp )
897 if (bmp && bmp->dib)
899 DIBSECTION *dib = bmp->dib;
901 if (dib->dsBm.bmBits)
903 if (dib->dshSection)
904 UnmapViewOfFile(dib->dsBm.bmBits);
905 else
906 VirtualFree(dib->dsBm.bmBits, MEM_RELEASE, 0L);
909 BITMAP_Driver->pDeleteDIBSection(bmp);
911 HeapFree(GetProcessHeap(), 0, dib);
912 bmp->dib = NULL;
916 /***********************************************************************
917 * DIB_FixColorsToLoadflags
919 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
920 * are in loadflags
922 void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
924 int colors;
925 COLORREF c_W, c_S, c_F, c_L, c_C;
926 int incr,i;
927 RGBQUAD *ptr;
929 if (bmi->bmiHeader.biBitCount > 8) return;
930 if (bmi->bmiHeader.biSize == sizeof(BITMAPINFOHEADER)) incr = 4;
931 else if (bmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) incr = 3;
932 else {
933 WARN("Wrong bitmap header size!\n");
934 return;
936 colors = bmi->bmiHeader.biClrUsed;
937 if (!colors && (bmi->bmiHeader.biBitCount <= 8))
938 colors = 1 << bmi->bmiHeader.biBitCount;
939 c_W = GetSysColor(COLOR_WINDOW);
940 c_S = GetSysColor(COLOR_3DSHADOW);
941 c_F = GetSysColor(COLOR_3DFACE);
942 c_L = GetSysColor(COLOR_3DLIGHT);
943 if (loadflags & LR_LOADTRANSPARENT) {
944 switch (bmi->bmiHeader.biBitCount) {
945 case 1: pix = pix >> 7; break;
946 case 4: pix = pix >> 4; break;
947 case 8: break;
948 default:
949 WARN("(%d): Unsupported depth\n", bmi->bmiHeader.biBitCount);
950 return;
952 if (pix >= colors) {
953 WARN("pixel has color index greater than biClrUsed!\n");
954 return;
956 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
957 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
958 ptr->rgbBlue = GetBValue(c_W);
959 ptr->rgbGreen = GetGValue(c_W);
960 ptr->rgbRed = GetRValue(c_W);
962 if (loadflags & LR_LOADMAP3DCOLORS)
963 for (i=0; i<colors; i++) {
964 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
965 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
966 if (c_C == RGB(128, 128, 128)) {
967 ptr->rgbRed = GetRValue(c_S);
968 ptr->rgbGreen = GetGValue(c_S);
969 ptr->rgbBlue = GetBValue(c_S);
970 } else if (c_C == RGB(192, 192, 192)) {
971 ptr->rgbRed = GetRValue(c_F);
972 ptr->rgbGreen = GetGValue(c_F);
973 ptr->rgbBlue = GetBValue(c_F);
974 } else if (c_C == RGB(223, 223, 223)) {
975 ptr->rgbRed = GetRValue(c_L);
976 ptr->rgbGreen = GetGValue(c_L);
977 ptr->rgbBlue = GetBValue(c_L);
982 /***********************************************************************
983 * DIB_CreateDIBFromBitmap
984 * Allocates a packed DIB and copies the bitmap data into it.
986 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
988 BITMAPOBJ *pBmp = NULL;
989 HGLOBAL hPackedDIB = 0;
990 LPBYTE pPackedDIB = NULL;
991 LPBITMAPINFOHEADER pbmiHeader = NULL;
992 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
993 OffsetBits = 0, nLinesCopied = 0;
995 /* Get a pointer to the BITMAPOBJ structure */
996 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
998 /* Get the bitmap dimensions */
999 width = pBmp->bitmap.bmWidth;
1000 height = pBmp->bitmap.bmHeight;
1001 depth = pBmp->bitmap.bmBitsPixel;
1004 * A packed DIB contains a BITMAPINFO structure followed immediately by
1005 * an optional color palette and the pixel data.
1008 /* Calculate the size of the packed DIB */
1009 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
1010 cPackedSize = sizeof(BITMAPINFOHEADER)
1011 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
1012 + cDataSize;
1013 /* Get the offset to the bits */
1014 OffsetBits = cPackedSize - cDataSize;
1016 /* Allocate the packed DIB */
1017 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
1018 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
1019 cPackedSize );
1020 if ( !hPackedDIB )
1022 WARN("Could not allocate packed DIB!\n");
1023 goto END;
1026 /* A packed DIB starts with a BITMAPINFOHEADER */
1027 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
1028 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
1030 /* Init the BITMAPINFOHEADER */
1031 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
1032 pbmiHeader->biWidth = width;
1033 pbmiHeader->biHeight = height;
1034 pbmiHeader->biPlanes = 1;
1035 pbmiHeader->biBitCount = depth;
1036 pbmiHeader->biCompression = BI_RGB;
1037 pbmiHeader->biSizeImage = 0;
1038 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
1039 pbmiHeader->biClrUsed = 0;
1040 pbmiHeader->biClrImportant = 0;
1042 /* Retrieve the DIB bits from the bitmap and fill in the
1043 * DIB color table if present */
1045 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
1046 hBmp, /* Handle to bitmap */
1047 0, /* First scan line to set in dest bitmap */
1048 height, /* Number of scan lines to copy */
1049 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
1050 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
1051 0); /* RGB or palette index */
1052 GlobalUnlock(hPackedDIB);
1054 /* Cleanup if GetDIBits failed */
1055 if (nLinesCopied != height)
1057 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
1058 GlobalFree(hPackedDIB);
1059 hPackedDIB = 0;
1062 END:
1063 return hPackedDIB;