Cleaned up major ugliness in __lfCheckSum.
[wine/gsoc_dplay.git] / objects / dib.c
blob8d5dcc86b5eb76f40fcb01b5a63e9fbc3b5d1549
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_GetDCUpdate( hdc );
137 if(!dc) return FALSE;
139 if(dc->funcs->pStretchDIBits)
140 heightSrc = 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 );
161 GDI_ReleaseObj( hdc );
162 return heightSrc;
166 /***********************************************************************
167 * SetDIBits16 (GDI.440)
169 INT16 WINAPI SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
170 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
171 UINT16 coloruse )
173 return SetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
177 /******************************************************************************
178 * SetDIBits [GDI32.312] Sets pixels in a bitmap using colors from DIB
180 * PARAMS
181 * hdc [I] Handle to device context
182 * hbitmap [I] Handle to bitmap
183 * startscan [I] Starting scan line
184 * lines [I] Number of scan lines
185 * bits [I] Array of bitmap bits
186 * info [I] Address of structure with data
187 * coloruse [I] Type of color indexes to use
189 * RETURNS
190 * Success: Number of scan lines copied
191 * Failure: 0
193 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
194 UINT lines, LPCVOID bits, const BITMAPINFO *info,
195 UINT coloruse )
197 DC *dc;
198 BITMAPOBJ *bitmap;
199 INT result;
201 /* Check parameters */
202 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
204 if (!(bitmap = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
206 GDI_ReleaseObj( hdc );
207 return 0;
210 result = BITMAP_Driver->pSetDIBits(bitmap, dc, startscan,
211 lines, bits, info,
212 coloruse, hbitmap);
214 GDI_ReleaseObj( hbitmap );
215 GDI_ReleaseObj( hdc );
217 return result;
221 /***********************************************************************
222 * SetDIBitsToDevice16 (GDI.443)
224 INT16 WINAPI SetDIBitsToDevice16(HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
225 INT16 cy, INT16 xSrc, INT16 ySrc, UINT16 startscan,
226 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
227 UINT16 coloruse )
229 return SetDIBitsToDevice( hdc, xDest, yDest, cx, cy, xSrc, ySrc,
230 startscan, lines, bits, info, coloruse );
234 /***********************************************************************
235 * SetDIBitsToDevice (GDI32.313)
237 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
238 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
239 UINT lines, LPCVOID bits, const BITMAPINFO *info,
240 UINT coloruse )
242 INT ret;
243 DC *dc;
245 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
247 if(dc->funcs->pSetDIBitsToDevice)
248 ret = dc->funcs->pSetDIBitsToDevice( dc, xDest, yDest, cx, cy, xSrc,
249 ySrc, startscan, lines, bits,
250 info, coloruse );
251 else {
252 FIXME("unimplemented on hdc %08x\n", hdc);
253 ret = 0;
256 GDI_ReleaseObj( hdc );
257 return ret;
260 /***********************************************************************
261 * SetDIBColorTable16 (GDI.602)
263 UINT16 WINAPI SetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
264 RGBQUAD *colors )
266 return SetDIBColorTable( hdc, startpos, entries, colors );
269 /***********************************************************************
270 * SetDIBColorTable (GDI32.311)
272 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
273 RGBQUAD *colors )
275 DC * dc;
276 PALETTEENTRY * palEntry;
277 PALETTEOBJ * palette;
278 RGBQUAD *end;
280 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
282 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
284 GDI_ReleaseObj( hdc );
285 return 0;
288 /* Transfer color info */
290 if (dc->w.bitsPerPixel <= 8) {
291 palEntry = palette->logpalette.palPalEntry + startpos;
292 if (startpos + entries > (1 << dc->w.bitsPerPixel))
293 entries = (1 << dc->w.bitsPerPixel) - startpos;
295 if (startpos + entries > palette->logpalette.palNumEntries)
296 entries = palette->logpalette.palNumEntries - startpos;
298 for (end = colors + entries; colors < end; palEntry++, colors++)
300 palEntry->peRed = colors->rgbRed;
301 palEntry->peGreen = colors->rgbGreen;
302 palEntry->peBlue = colors->rgbBlue;
304 } else {
305 entries = 0;
307 GDI_ReleaseObj( dc->w.hPalette );
308 GDI_ReleaseObj( hdc );
309 return entries;
312 /***********************************************************************
313 * GetDIBColorTable16 (GDI.603)
315 UINT16 WINAPI GetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
316 RGBQUAD *colors )
318 return GetDIBColorTable( hdc, startpos, entries, colors );
321 /***********************************************************************
322 * GetDIBColorTable (GDI32.169)
324 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
325 RGBQUAD *colors )
327 DC * dc;
328 PALETTEENTRY * palEntry;
329 PALETTEOBJ * palette;
330 RGBQUAD *end;
332 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
334 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
336 GDI_ReleaseObj( hdc );
337 return 0;
340 /* Transfer color info */
342 if (dc->w.bitsPerPixel <= 8) {
343 palEntry = palette->logpalette.palPalEntry + startpos;
344 if (startpos + entries > (1 << dc->w.bitsPerPixel)) {
345 entries = (1 << dc->w.bitsPerPixel) - startpos;
347 for (end = colors + entries; colors < end; palEntry++, colors++)
349 colors->rgbRed = palEntry->peRed;
350 colors->rgbGreen = palEntry->peGreen;
351 colors->rgbBlue = palEntry->peBlue;
352 colors->rgbReserved = 0;
354 } else {
355 entries = 0;
357 GDI_ReleaseObj( dc->w.hPalette );
358 GDI_ReleaseObj( hdc );
359 return entries;
362 /* FIXME the following two structs should be combined with __sysPalTemplate in
363 objects/color.c - this should happen after de-X11-ing both of these
364 files.
365 NB. RGBQUAD and PALETTENTRY have different orderings of red, green
366 and blue - sigh */
368 static RGBQUAD EGAColors[16] = {
369 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
370 { 0x00, 0x00, 0x00, 0x00 },
371 { 0x00, 0x00, 0x80, 0x00 },
372 { 0x00, 0x80, 0x00, 0x00 },
373 { 0x00, 0x80, 0x80, 0x00 },
374 { 0x80, 0x00, 0x00, 0x00 },
375 { 0x80, 0x00, 0x80, 0x00 },
376 { 0x80, 0x80, 0x00, 0x00 },
377 { 0x80, 0x80, 0x80, 0x00 },
378 { 0xc0, 0xc0, 0xc0, 0x00 },
379 { 0x00, 0x00, 0xff, 0x00 },
380 { 0x00, 0xff, 0x00, 0x00 },
381 { 0x00, 0xff, 0xff, 0x00 },
382 { 0xff, 0x00, 0x00, 0x00 },
383 { 0xff, 0x00, 0xff, 0x00 },
384 { 0xff, 0xff, 0x00, 0x00 },
385 { 0xff, 0xff, 0xff, 0x00 }
389 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
390 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
391 { 0x00, 0x00, 0x00, 0x00 },
392 { 0x00, 0x00, 0x80, 0x00 },
393 { 0x00, 0x80, 0x00, 0x00 },
394 { 0x00, 0x80, 0x80, 0x00 },
395 { 0x80, 0x00, 0x00, 0x00 },
396 { 0x80, 0x00, 0x80, 0x00 },
397 { 0x80, 0x80, 0x00, 0x00 },
398 { 0xc0, 0xc0, 0xc0, 0x00 },
399 { 0xc0, 0xdc, 0xc0, 0x00 },
400 { 0xf0, 0xca, 0xa6, 0x00 },
401 { 0xf0, 0xfb, 0xff, 0x00 },
402 { 0xa4, 0xa0, 0xa0, 0x00 },
403 { 0x80, 0x80, 0x80, 0x00 },
404 { 0x00, 0x00, 0xf0, 0x00 },
405 { 0x00, 0xff, 0x00, 0x00 },
406 { 0x00, 0xff, 0xff, 0x00 },
407 { 0xff, 0x00, 0x00, 0x00 },
408 { 0xff, 0x00, 0xff, 0x00 },
409 { 0xff, 0xff, 0x00, 0x00 },
410 { 0xff, 0xff, 0xff, 0x00 }
413 /***********************************************************************
414 * GetDIBits16 (GDI.441)
416 INT16 WINAPI GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
417 UINT16 lines, LPVOID bits, BITMAPINFO * info,
418 UINT16 coloruse )
420 return GetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
424 /******************************************************************************
425 * GetDIBits [GDI32.170] Retrieves bits of bitmap and copies to buffer
427 * RETURNS
428 * Success: Number of scan lines copied from bitmap
429 * Failure: 0
431 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
433 INT WINAPI GetDIBits(
434 HDC hdc, /* [in] Handle to device context */
435 HBITMAP hbitmap, /* [in] Handle to bitmap */
436 UINT startscan, /* [in] First scan line to set in dest bitmap */
437 UINT lines, /* [in] Number of scan lines to copy */
438 LPVOID bits, /* [out] Address of array for bitmap bits */
439 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
440 UINT coloruse) /* [in] RGB or palette index */
442 DC * dc;
443 BITMAPOBJ * bmp;
444 PALETTEENTRY * palEntry;
445 PALETTEOBJ * palette;
446 int i;
448 if (!info) return 0;
449 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
450 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
452 GDI_ReleaseObj( hdc );
453 return 0;
455 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
457 GDI_ReleaseObj( hdc );
458 GDI_ReleaseObj( hbitmap );
459 return 0;
462 /* Transfer color info */
464 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
466 info->bmiHeader.biClrUsed = 0;
468 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
469 palEntry = palette->logpalette.palPalEntry;
470 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
471 if (coloruse == DIB_RGB_COLORS) {
472 info->bmiColors[i].rgbRed = palEntry->peRed;
473 info->bmiColors[i].rgbGreen = palEntry->peGreen;
474 info->bmiColors[i].rgbBlue = palEntry->peBlue;
475 info->bmiColors[i].rgbReserved = 0;
477 else ((WORD *)info->bmiColors)[i] = (WORD)i;
479 } else {
480 switch (info->bmiHeader.biBitCount) {
481 case 1:
482 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
483 info->bmiColors[0].rgbBlue = 0;
484 info->bmiColors[0].rgbReserved = 0;
485 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
486 info->bmiColors[1].rgbBlue = 0xff;
487 info->bmiColors[1].rgbReserved = 0;
488 break;
490 case 4:
491 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
492 break;
494 case 8:
496 INT r, g, b;
497 RGBQUAD *color;
499 memcpy(info->bmiColors, DefLogPalette,
500 10 * sizeof(RGBQUAD));
501 memcpy(info->bmiColors + 246, DefLogPalette + 10,
502 10 * sizeof(RGBQUAD));
503 color = info->bmiColors + 10;
504 for(r = 0; r <= 5; r++) /* FIXME */
505 for(g = 0; g <= 5; g++)
506 for(b = 0; b <= 5; b++) {
507 color->rgbRed = (r * 0xff) / 5;
508 color->rgbGreen = (g * 0xff) / 5;
509 color->rgbBlue = (b * 0xff) / 5;
510 color->rgbReserved = 0;
511 color++;
518 GDI_ReleaseObj( dc->w.hPalette );
520 if (bits && lines)
522 /* If the bitmap object already have a dib section that contains image data, get the bits from it*/
523 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
525 /*FIXME: Only RGB dibs supported for now */
526 int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
527 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
528 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
529 int x, y;
531 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
533 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
534 dstwidthb = -dstwidthb;
537 switch( info->bmiHeader.biBitCount ) {
539 case 15:
540 case 16: /* 16 bpp dstDIB */
542 LPWORD dstbits = (LPWORD)dbits;
543 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
545 /* FIXME: BI_BITFIELDS not supported yet */
547 switch(bmp->dib->dsBm.bmBitsPixel) {
549 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
551 /* FIXME: BI_BITFIELDS not supported yet */
552 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
553 memcpy(dbits, sbits, srcwidthb);
555 break;
557 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
559 LPBYTE srcbits = sbits;
561 for( y = 0; y < lines; y++) {
562 for( x = 0; x < srcwidth; x++ )
563 *dstbits++ = ((*srcbits++ >> 3) & bmask) |
564 (((WORD)*srcbits++ << 2) & gmask) |
565 (((WORD)*srcbits++ << 7) & rmask);
566 dstbits = (LPWORD)(dbits+=dstwidthb);
567 srcbits = (sbits += srcwidthb);
570 break;
572 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
574 LPDWORD srcbits = (LPDWORD)sbits;
575 DWORD val;
577 for( y = 0; y < lines; y++) {
578 for( x = 0; x < srcwidth; x++ ) {
579 val = *srcbits++;
580 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
581 ((val >> 9) & rmask));
583 dstbits = (LPWORD)(dbits+=dstwidthb);
584 srcbits = (LPDWORD)(sbits+=srcwidthb);
587 break;
589 default: /* ? bit bmp -> 16 bit DIB */
590 FIXME("15/16 bit DIB %d bit bitmap\n",
591 bmp->bitmap.bmBitsPixel);
592 break;
595 break;
597 case 24: /* 24 bpp dstDIB */
599 LPBYTE dstbits = dbits;
601 switch(bmp->dib->dsBm.bmBitsPixel) {
603 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
605 LPWORD srcbits = (LPWORD)sbits;
606 WORD val;
608 /* FIXME: BI_BITFIELDS not supported yet */
609 for( y = 0; y < lines; y++) {
610 for( x = 0; x < srcwidth; x++ ) {
611 val = *srcbits++;
612 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
613 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
614 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
616 dstbits = (LPBYTE)(dbits+=dstwidthb);
617 srcbits = (LPWORD)(sbits+=srcwidthb);
620 break;
622 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
624 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
625 memcpy(dbits, sbits, srcwidthb);
627 break;
629 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
631 LPBYTE srcbits = (LPBYTE)sbits;
633 for( y = 0; y < lines; y++) {
634 for( x = 0; x < srcwidth; x++, srcbits++ ) {
635 *dstbits++ = *srcbits++;
636 *dstbits++ = *srcbits++;
637 *dstbits++ = *srcbits++;
639 dstbits=(LPBYTE)(dbits+=dstwidthb);
640 srcbits = (LPBYTE)(sbits+=srcwidthb);
643 break;
645 default: /* ? bit bmp -> 24 bit DIB */
646 FIXME("24 bit DIB %d bit bitmap\n",
647 bmp->bitmap.bmBitsPixel);
648 break;
651 break;
653 case 32: /* 32 bpp dstDIB */
655 LPDWORD dstbits = (LPDWORD)dbits;
657 /* FIXME: BI_BITFIELDS not supported yet */
659 switch(bmp->dib->dsBm.bmBitsPixel) {
660 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
662 LPWORD srcbits = (LPWORD)sbits;
663 DWORD val;
665 /* FIXME: BI_BITFIELDS not supported yet */
666 for( y = 0; y < lines; y++) {
667 for( x = 0; x < srcwidth; x++ ) {
668 val = (DWORD)*srcbits++;
669 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
670 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
671 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
673 dstbits=(LPDWORD)(dbits+=dstwidthb);
674 srcbits=(LPWORD)(sbits+=srcwidthb);
677 break;
679 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
681 LPBYTE srcbits = sbits;
683 for( y = 0; y < lines; y++) {
684 for( x = 0; x < srcwidth; x++, srcbits+=3 )
685 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
686 dstbits=(LPDWORD)(dbits+=dstwidthb);
687 srcbits=(sbits+=srcwidthb);
690 break;
692 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
694 /* FIXME: BI_BITFIELDS not supported yet */
695 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
696 memcpy(dbits, sbits, srcwidthb);
698 break;
700 default: /* ? bit bmp -> 16 bit DIB */
701 FIXME("15/16 bit DIB %d bit bitmap\n",
702 bmp->bitmap.bmBitsPixel);
703 break;
706 break;
708 default: /* ? bit DIB */
709 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
710 break;
713 /* Otherwise, get bits from the XImage */
714 else if(!BITMAP_Driver->pGetDIBits(bmp, dc, startscan, lines, bits, info, coloruse, hbitmap))
716 GDI_ReleaseObj( hdc );
717 GDI_ReleaseObj( hbitmap );
719 return 0;
722 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
724 /* fill in struct members */
726 if( info->bmiHeader.biBitCount == 0)
728 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
729 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
730 info->bmiHeader.biPlanes = 1;
731 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
732 info->bmiHeader.biSizeImage =
733 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
734 bmp->bitmap.bmHeight,
735 bmp->bitmap.bmBitsPixel );
736 info->bmiHeader.biCompression = 0;
738 else
740 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
741 info->bmiHeader.biWidth,
742 info->bmiHeader.biHeight,
743 info->bmiHeader.biBitCount );
747 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
748 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
749 info->bmiHeader.biHeight);
751 GDI_ReleaseObj( hdc );
752 GDI_ReleaseObj( hbitmap );
754 return lines;
758 /***********************************************************************
759 * CreateDIBitmap16 (GDI.442)
761 HBITMAP16 WINAPI CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
762 DWORD init, LPCVOID bits, const BITMAPINFO * data,
763 UINT16 coloruse )
765 return CreateDIBitmap( hdc, header, init, bits, data, coloruse );
769 /***********************************************************************
770 * CreateDIBitmap (GDI32.37)
772 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
773 DWORD init, LPCVOID bits, const BITMAPINFO *data,
774 UINT coloruse )
776 HBITMAP handle;
777 BOOL fColor;
778 DWORD width;
779 int height;
780 WORD bpp;
781 WORD compr;
783 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
784 if (height < 0) height = -height;
786 /* Check if we should create a monochrome or color bitmap. */
787 /* We create a monochrome bitmap only if it has exactly 2 */
788 /* colors, which are black followed by white, nothing else. */
789 /* In all other cases, we create a color bitmap. */
791 if (bpp != 1) fColor = TRUE;
792 else if ((coloruse != DIB_RGB_COLORS) ||
793 (init != CBM_INIT) || !data) fColor = FALSE;
794 else
796 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
798 RGBQUAD *rgb = data->bmiColors;
799 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
801 /* Check if the first color of the colormap is black */
802 if ((col == RGB(0,0,0)))
804 rgb++;
805 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
806 /* If the second color is white, create a monochrome bitmap */
807 fColor = (col != RGB(0xff,0xff,0xff));
809 /* Note : If the first color of the colormap is white
810 followed by black, we have to create a color bitmap.
811 If we don't the white will be displayed in black later on!*/
812 else fColor = TRUE;
814 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
816 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
817 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
818 if ((col == RGB(0,0,0)))
820 rgb++;
821 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
822 fColor = (col != RGB(0xff,0xff,0xff));
824 else fColor = TRUE;
826 else
828 WARN("(%ld): wrong size for data\n",
829 data->bmiHeader.biSize );
830 return 0;
834 /* Now create the bitmap */
836 if (fColor)
838 HDC tmpdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
839 handle = CreateCompatibleBitmap( tmpdc, width, height );
840 DeleteDC( tmpdc );
842 else handle = CreateBitmap( width, height, 1, 1, NULL );
844 if (!handle) return 0;
846 if (init == CBM_INIT)
847 SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
848 return handle;
851 /***********************************************************************
852 * CreateDIBSection16 (GDI.489)
854 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
855 SEGPTR *bits, HANDLE section,
856 DWORD offset)
858 HBITMAP16 hbitmap = 0;
859 DC *dc;
860 BOOL bDesktopDC = FALSE;
862 /* If the reference hdc is null, take the desktop dc */
863 if (hdc == 0)
865 hdc = CreateCompatibleDC(0);
866 bDesktopDC = TRUE;
869 if ((dc = DC_GetDCPtr( hdc )))
871 hbitmap = dc->funcs->pCreateDIBSection16(dc, bmi, usage, bits, section, offset, 0);
872 GDI_ReleaseObj(hdc);
875 if (bDesktopDC)
876 DeleteDC(hdc);
878 return hbitmap;
881 /***********************************************************************
882 * DIB_CreateDIBSection
884 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
885 LPVOID *bits, HANDLE section,
886 DWORD offset, DWORD ovr_pitch)
888 HBITMAP hbitmap = 0;
889 DC *dc;
890 BOOL bDesktopDC = FALSE;
892 /* If the reference hdc is null, take the desktop dc */
893 if (hdc == 0)
895 hdc = CreateCompatibleDC(0);
896 bDesktopDC = TRUE;
899 if ((dc = DC_GetDCPtr( hdc )))
901 hbitmap = dc->funcs->pCreateDIBSection(dc, bmi, usage, bits, section, offset, ovr_pitch);
902 GDI_ReleaseObj(hdc);
905 if (bDesktopDC)
906 DeleteDC(hdc);
908 return hbitmap;
911 /***********************************************************************
912 * CreateDIBSection (GDI32.36)
914 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
915 LPVOID *bits, HANDLE section,
916 DWORD offset)
918 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
921 /***********************************************************************
922 * DIB_DeleteDIBSection
924 void DIB_DeleteDIBSection( BITMAPOBJ *bmp )
926 if (bmp && bmp->dib)
928 DIBSECTION *dib = bmp->dib;
930 if (dib->dsBm.bmBits)
932 if (dib->dshSection)
933 UnmapViewOfFile(dib->dsBm.bmBits);
934 else if (!dib->dsOffset)
935 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
938 BITMAP_Driver->pDeleteDIBSection(bmp);
940 HeapFree(GetProcessHeap(), 0, dib);
941 bmp->dib = NULL;
945 /***********************************************************************
946 * DIB_CreateDIBFromBitmap
947 * Allocates a packed DIB and copies the bitmap data into it.
949 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
951 BITMAPOBJ *pBmp = NULL;
952 HGLOBAL hPackedDIB = 0;
953 LPBYTE pPackedDIB = NULL;
954 LPBITMAPINFOHEADER pbmiHeader = NULL;
955 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
956 OffsetBits = 0, nLinesCopied = 0;
958 /* Get a pointer to the BITMAPOBJ structure */
959 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
960 if (!pBmp) return hPackedDIB;
962 /* Get the bitmap dimensions */
963 width = pBmp->bitmap.bmWidth;
964 height = pBmp->bitmap.bmHeight;
965 depth = pBmp->bitmap.bmBitsPixel;
968 * A packed DIB contains a BITMAPINFO structure followed immediately by
969 * an optional color palette and the pixel data.
972 /* Calculate the size of the packed DIB */
973 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
974 cPackedSize = sizeof(BITMAPINFOHEADER)
975 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
976 + cDataSize;
977 /* Get the offset to the bits */
978 OffsetBits = cPackedSize - cDataSize;
980 /* Allocate the packed DIB */
981 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
982 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
983 cPackedSize );
984 if ( !hPackedDIB )
986 WARN("Could not allocate packed DIB!\n");
987 goto END;
990 /* A packed DIB starts with a BITMAPINFOHEADER */
991 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
992 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
994 /* Init the BITMAPINFOHEADER */
995 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
996 pbmiHeader->biWidth = width;
997 pbmiHeader->biHeight = height;
998 pbmiHeader->biPlanes = 1;
999 pbmiHeader->biBitCount = depth;
1000 pbmiHeader->biCompression = BI_RGB;
1001 pbmiHeader->biSizeImage = 0;
1002 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
1003 pbmiHeader->biClrUsed = 0;
1004 pbmiHeader->biClrImportant = 0;
1006 /* Retrieve the DIB bits from the bitmap and fill in the
1007 * DIB color table if present */
1009 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
1010 hBmp, /* Handle to bitmap */
1011 0, /* First scan line to set in dest bitmap */
1012 height, /* Number of scan lines to copy */
1013 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
1014 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
1015 0); /* RGB or palette index */
1016 GlobalUnlock(hPackedDIB);
1018 /* Cleanup if GetDIBits failed */
1019 if (nLinesCopied != height)
1021 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
1022 GlobalFree(hPackedDIB);
1023 hPackedDIB = 0;
1026 END:
1027 GDI_ReleaseObj( hBmp );
1028 return hPackedDIB;