Winspool DocumentProperties and DeviceCapabilities should now work on
[wine.git] / objects / dib.c
blob4a8da7653466f0acfbecebebf328c243bf1f524c
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 (!lines) return 0;
459 if (!info) return 0;
460 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
461 if (!dc)
463 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
464 if (!dc) return 0;
466 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
468 GDI_HEAP_UNLOCK( hdc );
469 return 0;
471 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
473 GDI_HEAP_UNLOCK( hdc );
474 GDI_HEAP_UNLOCK( hbitmap );
475 return 0;
478 /* Transfer color info */
480 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
482 info->bmiHeader.biClrUsed = 0;
484 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
485 palEntry = palette->logpalette.palPalEntry;
486 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
487 if (coloruse == DIB_RGB_COLORS) {
488 info->bmiColors[i].rgbRed = palEntry->peRed;
489 info->bmiColors[i].rgbGreen = palEntry->peGreen;
490 info->bmiColors[i].rgbBlue = palEntry->peBlue;
491 info->bmiColors[i].rgbReserved = 0;
493 else ((WORD *)info->bmiColors)[i] = (WORD)i;
495 } else {
496 switch (info->bmiHeader.biBitCount) {
497 case 1:
498 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
499 info->bmiColors[0].rgbBlue = 0;
500 info->bmiColors[0].rgbReserved = 0;
501 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
502 info->bmiColors[1].rgbBlue = 0xff;
503 info->bmiColors[1].rgbReserved = 0;
504 break;
506 case 4:
507 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
508 break;
510 case 8:
512 INT r, g, b;
513 RGBQUAD *color;
515 memcpy(info->bmiColors, DefLogPalette,
516 10 * sizeof(RGBQUAD));
517 memcpy(info->bmiColors + 246, DefLogPalette + 10,
518 10 * sizeof(RGBQUAD));
519 color = info->bmiColors + 10;
520 for(r = 0; r <= 5; r++) /* FIXME */
521 for(g = 0; g <= 5; g++)
522 for(b = 0; b <= 5; b++) {
523 color->rgbRed = (r * 0xff) / 5;
524 color->rgbGreen = (g * 0xff) / 5;
525 color->rgbBlue = (b * 0xff) / 5;
526 color->rgbReserved = 0;
527 color++;
534 GDI_HEAP_UNLOCK( dc->w.hPalette );
536 if (bits)
538 /* If the bitmap object already have a dib section that contains image data, get the bits from it*/
539 if(bmp->dib->dsBm.bmBits && bmp->dib->dsBm.bmBitsPixel >= 16 && info->bmiHeader.biBitCount >= 16)
541 /*FIXME: Only RGB dibs supported for now */
542 int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
543 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
544 LPVOID dbits = bits, sbits = bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
545 int x, y;
547 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
549 dbits = bits + (dstwidthb * lines);
550 dstwidthb = -dstwidthb;
553 switch( info->bmiHeader.biBitCount ) {
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 = (LPBYTE)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 = (LPBYTE)(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 >> 19) & bmask) | ((val >> 6) & gmask) |
596 ((val << 7) & 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 = (LPBYTE)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 >> 7) & 0xf8) | ((val >> 12) & 0x07));
628 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
629 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 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;
671 DWORD rmask = 0xff, gmask = 0xff00, bmask = 0xff0000;
673 /* FIXME: BI_BITFIELDS not supported yet */
675 switch(bmp->dib->dsBm.bmBitsPixel) {
676 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
678 LPWORD srcbits = (LPWORD)sbits;
679 DWORD val;
681 /* FIXME: BI_BITFIELDS not supported yet */
682 for( y = 0; y < lines; y++) {
683 for( x = 0; x < srcwidth; x++ ) {
684 val = (DWORD)*srcbits++;
685 *dstbits++ = ((val >> 7) & 0xf8) | ((val >> 12) & 0x07) |
686 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
687 ((val << 19) & 0xf800) | ((val << 14) & 0x070000);
689 dstbits=(dbits+=dstwidthb);
690 srcbits=(sbits+=srcwidthb);
693 break;
695 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
697 LPBYTE srcbits = (LPBYTE)sbits;
699 for( y = 0; y < lines; y++) {
700 for( x = 0; x < srcwidth; x++ )
701 *dstbits++ = ((DWORD)*srcbits++ & rmask) |
702 (((DWORD)*srcbits++ << 8) & gmask) |
703 (((DWORD)*srcbits++ << 7) & bmask);
704 dstbits=(dbits+=dstwidthb);
705 srcbits=(sbits+=srcwidthb);
708 break;
710 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
712 /* FIXME: BI_BITFIELDS not supported yet */
713 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
714 memcpy(dbits, sbits, srcwidthb);
716 break;
718 default: /* ? bit bmp -> 16 bit DIB */
719 FIXME("15/16 bit DIB %d bit bitmap\n",
720 bmp->bitmap.bmBitsPixel);
721 break;
724 break;
726 default: /* ? bit DIB */
727 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
728 break;
731 /* Otherwise, get bits from the XImage */
732 else if(!BITMAP_Driver->pGetDIBits(bmp, dc, startscan, lines, bits, info, coloruse, hbitmap))
734 GDI_HEAP_UNLOCK( hdc );
735 GDI_HEAP_UNLOCK( hbitmap );
737 return FALSE;
740 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
742 /* fill in struct members */
744 if( info->bmiHeader.biBitCount == 0)
746 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
747 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
748 info->bmiHeader.biPlanes = 1;
749 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
750 info->bmiHeader.biSizeImage =
751 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
752 bmp->bitmap.bmHeight,
753 bmp->bitmap.bmBitsPixel );
754 info->bmiHeader.biCompression = 0;
756 else
758 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
759 info->bmiHeader.biWidth,
760 info->bmiHeader.biHeight,
761 info->bmiHeader.biBitCount );
765 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
766 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
767 info->bmiHeader.biHeight);
769 GDI_HEAP_UNLOCK( hdc );
770 GDI_HEAP_UNLOCK( hbitmap );
772 return lines;
776 /***********************************************************************
777 * CreateDIBitmap16 (GDI.442)
779 HBITMAP16 WINAPI CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
780 DWORD init, LPCVOID bits, const BITMAPINFO * data,
781 UINT16 coloruse )
783 return CreateDIBitmap( hdc, header, init, bits, data, coloruse );
787 /***********************************************************************
788 * CreateDIBitmap32 (GDI32.37)
790 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
791 DWORD init, LPCVOID bits, const BITMAPINFO *data,
792 UINT coloruse )
794 HBITMAP handle;
795 BOOL fColor;
796 DWORD width;
797 int height;
798 WORD bpp;
799 WORD compr;
801 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
802 if (height < 0) height = -height;
804 /* Check if we should create a monochrome or color bitmap. */
805 /* We create a monochrome bitmap only if it has exactly 2 */
806 /* colors, which are either black or white, nothing else. */
807 /* In all other cases, we create a color bitmap. */
809 if (bpp != 1) fColor = TRUE;
810 else if ((coloruse != DIB_RGB_COLORS) ||
811 (init != CBM_INIT) || !data) fColor = FALSE;
812 else
814 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
816 RGBQUAD *rgb = data->bmiColors;
817 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
818 if ((col == RGB(0,0,0)) || (col == RGB(0xff,0xff,0xff)))
820 rgb++;
821 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
822 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
824 else fColor = TRUE;
826 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
828 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
829 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
830 if ((col == RGB(0,0,0)) || (col == RGB(0xff,0xff,0xff)))
832 rgb++;
833 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
834 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
836 else fColor = TRUE;
838 else
840 WARN("(%ld): wrong size for data\n",
841 data->bmiHeader.biSize );
842 return 0;
846 /* Now create the bitmap */
848 handle = fColor ? CreateBitmap( width, height, 1, MONITOR_GetDepth(&MONITOR_PrimaryMonitor), NULL ) :
849 CreateBitmap( width, height, 1, 1, NULL );
850 if (!handle) return 0;
852 if (init == CBM_INIT)
853 SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
854 return handle;
857 /***********************************************************************
858 * CreateDIBSection16 (GDI.489)
860 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
861 SEGPTR *bits, HANDLE section,
862 DWORD offset)
864 HBITMAP16 hbitmap;
865 DC *dc = (DC *) GDI_GetObjPtr(hdc, DC_MAGIC);
866 if(!dc) dc = (DC *) GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
867 if(!dc) return (HBITMAP16) NULL;
869 hbitmap = dc->funcs->pCreateDIBSection16(dc, bmi, usage, bits, section, offset);
871 GDI_HEAP_UNLOCK(hdc);
873 return hbitmap;
876 /***********************************************************************
877 * CreateDIBSection32 (GDI32.36)
879 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
880 LPVOID *bits, HANDLE section,
881 DWORD offset)
883 HBITMAP hbitmap;
884 DC *dc = (DC *) GDI_GetObjPtr(hdc, DC_MAGIC);
885 if(!dc) dc = (DC *) GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
886 if(!dc) return (HBITMAP) NULL;
888 hbitmap = dc->funcs->pCreateDIBSection(dc, bmi, usage, bits, section, offset);
890 GDI_HEAP_UNLOCK(hdc);
892 return hbitmap;
895 /***********************************************************************
896 * DIB_DeleteDIBSection
898 void DIB_DeleteDIBSection( BITMAPOBJ *bmp )
900 if (bmp && bmp->dib)
902 DIBSECTION *dib = bmp->dib;
904 if (dib->dsBm.bmBits)
906 if (dib->dshSection)
907 UnmapViewOfFile(dib->dsBm.bmBits);
908 else
909 VirtualFree(dib->dsBm.bmBits, MEM_RELEASE, 0L);
912 BITMAP_Driver->pDeleteDIBSection(bmp);
914 HeapFree(GetProcessHeap(), 0, dib);
915 bmp->dib = NULL;
919 /***********************************************************************
920 * DIB_FixColorsToLoadflags
922 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
923 * are in loadflags
925 void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
927 int colors;
928 COLORREF c_W, c_S, c_F, c_L, c_C;
929 int incr,i;
930 RGBQUAD *ptr;
932 if (bmi->bmiHeader.biBitCount > 8) return;
933 if (bmi->bmiHeader.biSize == sizeof(BITMAPINFOHEADER)) incr = 4;
934 else if (bmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) incr = 3;
935 else {
936 WARN("Wrong bitmap header size!\n");
937 return;
939 colors = bmi->bmiHeader.biClrUsed;
940 if (!colors && (bmi->bmiHeader.biBitCount <= 8))
941 colors = 1 << bmi->bmiHeader.biBitCount;
942 c_W = GetSysColor(COLOR_WINDOW);
943 c_S = GetSysColor(COLOR_3DSHADOW);
944 c_F = GetSysColor(COLOR_3DFACE);
945 c_L = GetSysColor(COLOR_3DLIGHT);
946 if (loadflags & LR_LOADTRANSPARENT) {
947 switch (bmi->bmiHeader.biBitCount) {
948 case 1: pix = pix >> 7; break;
949 case 4: pix = pix >> 4; break;
950 case 8: break;
951 default:
952 WARN("(%d): Unsupported depth\n", bmi->bmiHeader.biBitCount);
953 return;
955 if (pix >= colors) {
956 WARN("pixel has color index greater than biClrUsed!\n");
957 return;
959 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
960 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
961 ptr->rgbBlue = GetBValue(c_W);
962 ptr->rgbGreen = GetGValue(c_W);
963 ptr->rgbRed = GetRValue(c_W);
965 if (loadflags & LR_LOADMAP3DCOLORS)
966 for (i=0; i<colors; i++) {
967 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
968 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
969 if (c_C == RGB(128, 128, 128)) {
970 ptr->rgbRed = GetRValue(c_S);
971 ptr->rgbGreen = GetGValue(c_S);
972 ptr->rgbBlue = GetBValue(c_S);
973 } else if (c_C == RGB(192, 192, 192)) {
974 ptr->rgbRed = GetRValue(c_F);
975 ptr->rgbGreen = GetGValue(c_F);
976 ptr->rgbBlue = GetBValue(c_F);
977 } else if (c_C == RGB(223, 223, 223)) {
978 ptr->rgbRed = GetRValue(c_L);
979 ptr->rgbGreen = GetGValue(c_L);
980 ptr->rgbBlue = GetBValue(c_L);