Prepare switching to unicode of builtin widgets.
[wine.git] / objects / dib.c
blob2c08dac01e0d5eb1fe86bc7363825bb9975cb3e3
1 /*
2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
6 */
8 #include <stdlib.h>
10 #include "winbase.h"
11 #include "bitmap.h"
12 #include "callback.h"
13 #include "gdi.h"
14 #include "debugtools.h"
15 #include "palette.h"
17 DEFAULT_DEBUG_CHANNEL(bitmap);
19 /***********************************************************************
20 * DIB_GetDIBWidthBytes
22 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
23 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
25 int DIB_GetDIBWidthBytes( int width, int depth )
27 int words;
29 switch(depth)
31 case 1: words = (width + 31) / 32; break;
32 case 4: words = (width + 7) / 8; break;
33 case 8: words = (width + 3) / 4; break;
34 case 15:
35 case 16: words = (width + 1) / 2; break;
36 case 24: words = (width * 3 + 3)/4; break;
38 default:
39 WARN("(%d): Unsupported depth\n", depth );
40 /* fall through */
41 case 32:
42 words = width;
44 return 4 * words;
47 /***********************************************************************
48 * DIB_GetDIBImageBytes
50 * Return the number of bytes used to hold the image in a DIB bitmap.
52 int DIB_GetDIBImageBytes( int width, int height, int depth )
54 return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
58 /***********************************************************************
59 * DIB_BitmapInfoSize
61 * Return the size of the bitmap info structure including color table.
63 int DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
65 int colors;
67 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
69 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
70 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
71 return sizeof(BITMAPCOREHEADER) + colors *
72 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
74 else /* assume BITMAPINFOHEADER */
76 colors = info->bmiHeader.biClrUsed;
77 if (!colors && (info->bmiHeader.biBitCount <= 8))
78 colors = 1 << info->bmiHeader.biBitCount;
79 return sizeof(BITMAPINFOHEADER) + colors *
80 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
85 /***********************************************************************
86 * DIB_GetBitmapInfo
88 * Get the info from a bitmap header.
89 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
91 int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
92 int *height, WORD *bpp, WORD *compr )
94 if (header->biSize == sizeof(BITMAPINFOHEADER))
96 *width = header->biWidth;
97 *height = header->biHeight;
98 *bpp = header->biBitCount;
99 *compr = header->biCompression;
100 return 1;
102 if (header->biSize == sizeof(BITMAPCOREHEADER))
104 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
105 *width = core->bcWidth;
106 *height = core->bcHeight;
107 *bpp = core->bcBitCount;
108 *compr = 0;
109 return 0;
111 WARN("(%ld): wrong size for header\n", header->biSize );
112 return -1;
116 /***********************************************************************
117 * StretchDIBits16 (GDI.439)
119 INT16 WINAPI StretchDIBits16(HDC16 hdc, INT16 xDst, INT16 yDst, INT16 widthDst,
120 INT16 heightDst, INT16 xSrc, INT16 ySrc, INT16 widthSrc,
121 INT16 heightSrc, const VOID *bits,
122 const BITMAPINFO *info, UINT16 wUsage, DWORD dwRop )
124 return (INT16)StretchDIBits( hdc, xDst, yDst, widthDst, heightDst,
125 xSrc, ySrc, widthSrc, heightSrc, bits,
126 info, wUsage, dwRop );
130 /***********************************************************************
131 * StretchDIBits (GDI32.351)
133 INT WINAPI StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
134 INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
135 INT heightSrc, const void *bits,
136 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
138 DC *dc = DC_GetDCUpdate( hdc );
139 if(!dc) return FALSE;
141 if(dc->funcs->pStretchDIBits)
142 heightSrc = dc->funcs->pStretchDIBits(dc, xDst, yDst, widthDst,
143 heightDst, xSrc, ySrc, widthSrc,
144 heightSrc, bits, info, wUsage,
145 dwRop);
146 else { /* use StretchBlt */
147 HBITMAP hBitmap, hOldBitmap;
148 HDC hdcMem;
150 hdcMem = CreateCompatibleDC( hdc );
151 if (info->bmiHeader.biCompression == BI_RLE4 ||
152 info->bmiHeader.biCompression == BI_RLE8) {
154 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
155 * contain all the rectangle described in bmiHeader, but only part of it.
156 * This mean that those undescribed pixels must be left untouched.
157 * So, we first copy on a memory bitmap the current content of the
158 * destination rectangle, blit the DIB bits on top of it - hence leaving
159 * the gaps untouched -, and blitting the rectangle back.
160 * This insure that gaps are untouched on the destination rectangle
161 * Not doing so leads to trashed images (the gaps contain what was on the
162 * memory bitmap => generally black or garbage)
163 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
164 * another speed vs correctness issue. Anyway, if speed is needed, then the
165 * pStretchDIBits function shall be implemented.
166 * ericP (2000/09/09)
168 hBitmap = CreateCompatibleBitmap(hdc, info->bmiHeader.biWidth,
169 info->bmiHeader.biHeight);
170 hOldBitmap = SelectObject( hdcMem, hBitmap );
172 /* copy existing bitmap from destination dc */
173 StretchBlt( hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
174 widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
175 dwRop );
176 SetDIBits(hdcMem, hBitmap, 0, info->bmiHeader.biHeight, bits,
177 info, DIB_RGB_COLORS);
179 } else {
180 hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
181 bits, info, wUsage );
182 hOldBitmap = SelectObject( hdcMem, hBitmap );
185 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
186 left (negative biHeight) */
187 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
188 hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
189 widthSrc, heightSrc, dwRop );
190 SelectObject( hdcMem, hOldBitmap );
191 DeleteDC( hdcMem );
192 DeleteObject( hBitmap );
194 GDI_ReleaseObj( hdc );
195 return heightSrc;
199 /***********************************************************************
200 * SetDIBits16 (GDI.440)
202 INT16 WINAPI SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
203 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
204 UINT16 coloruse )
206 return SetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
210 /******************************************************************************
211 * SetDIBits [GDI32.312] Sets pixels in a bitmap using colors from DIB
213 * PARAMS
214 * hdc [I] Handle to device context
215 * hbitmap [I] Handle to bitmap
216 * startscan [I] Starting scan line
217 * lines [I] Number of scan lines
218 * bits [I] Array of bitmap bits
219 * info [I] Address of structure with data
220 * coloruse [I] Type of color indexes to use
222 * RETURNS
223 * Success: Number of scan lines copied
224 * Failure: 0
226 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
227 UINT lines, LPCVOID bits, const BITMAPINFO *info,
228 UINT coloruse )
230 DC *dc;
231 BITMAPOBJ *bitmap;
232 INT result;
234 /* Check parameters */
235 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
237 if (!(bitmap = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
239 GDI_ReleaseObj( hdc );
240 return 0;
243 result = BITMAP_Driver->pSetDIBits(bitmap, dc, startscan,
244 lines, bits, info,
245 coloruse, hbitmap);
247 GDI_ReleaseObj( hbitmap );
248 GDI_ReleaseObj( hdc );
250 return result;
254 /***********************************************************************
255 * SetDIBitsToDevice16 (GDI.443)
257 INT16 WINAPI SetDIBitsToDevice16(HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
258 INT16 cy, INT16 xSrc, INT16 ySrc, UINT16 startscan,
259 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
260 UINT16 coloruse )
262 return SetDIBitsToDevice( hdc, xDest, yDest, cx, cy, xSrc, ySrc,
263 startscan, lines, bits, info, coloruse );
267 /***********************************************************************
268 * SetDIBitsToDevice (GDI32.313)
270 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
271 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
272 UINT lines, LPCVOID bits, const BITMAPINFO *info,
273 UINT coloruse )
275 INT ret;
276 DC *dc;
278 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
280 if(dc->funcs->pSetDIBitsToDevice)
281 ret = dc->funcs->pSetDIBitsToDevice( dc, xDest, yDest, cx, cy, xSrc,
282 ySrc, startscan, lines, bits,
283 info, coloruse );
284 else {
285 FIXME("unimplemented on hdc %08x\n", hdc);
286 ret = 0;
289 GDI_ReleaseObj( hdc );
290 return ret;
293 /***********************************************************************
294 * SetDIBColorTable16 (GDI.602)
296 UINT16 WINAPI SetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
297 RGBQUAD *colors )
299 return SetDIBColorTable( hdc, startpos, entries, colors );
302 /***********************************************************************
303 * SetDIBColorTable (GDI32.311)
305 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
306 RGBQUAD *colors )
308 DC * dc;
309 PALETTEENTRY * palEntry;
310 PALETTEOBJ * palette;
311 RGBQUAD *end;
313 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
315 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC )))
317 GDI_ReleaseObj( hdc );
318 return 0;
321 /* Transfer color info */
323 if (dc->bitsPerPixel <= 8) {
324 palEntry = palette->logpalette.palPalEntry + startpos;
325 if (startpos + entries > (1 << dc->bitsPerPixel))
326 entries = (1 << dc->bitsPerPixel) - startpos;
328 if (startpos + entries > palette->logpalette.palNumEntries)
329 entries = palette->logpalette.palNumEntries - startpos;
331 for (end = colors + entries; colors < end; palEntry++, colors++)
333 palEntry->peRed = colors->rgbRed;
334 palEntry->peGreen = colors->rgbGreen;
335 palEntry->peBlue = colors->rgbBlue;
337 } else {
338 entries = 0;
340 GDI_ReleaseObj( dc->hPalette );
341 GDI_ReleaseObj( hdc );
342 return entries;
345 /***********************************************************************
346 * GetDIBColorTable16 (GDI.603)
348 UINT16 WINAPI GetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
349 RGBQUAD *colors )
351 return GetDIBColorTable( hdc, startpos, entries, colors );
354 /***********************************************************************
355 * GetDIBColorTable (GDI32.169)
357 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
358 RGBQUAD *colors )
360 DC * dc;
361 PALETTEENTRY * palEntry;
362 PALETTEOBJ * palette;
363 RGBQUAD *end;
365 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
367 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC )))
369 GDI_ReleaseObj( hdc );
370 return 0;
373 /* Transfer color info */
375 if (dc->bitsPerPixel <= 8) {
376 palEntry = palette->logpalette.palPalEntry + startpos;
377 if (startpos + entries > (1 << dc->bitsPerPixel)) {
378 entries = (1 << dc->bitsPerPixel) - startpos;
380 for (end = colors + entries; colors < end; palEntry++, colors++)
382 colors->rgbRed = palEntry->peRed;
383 colors->rgbGreen = palEntry->peGreen;
384 colors->rgbBlue = palEntry->peBlue;
385 colors->rgbReserved = 0;
387 } else {
388 entries = 0;
390 GDI_ReleaseObj( dc->hPalette );
391 GDI_ReleaseObj( hdc );
392 return entries;
395 /* FIXME the following two structs should be combined with __sysPalTemplate in
396 objects/color.c - this should happen after de-X11-ing both of these
397 files.
398 NB. RGBQUAD and PALETTENTRY have different orderings of red, green
399 and blue - sigh */
401 static RGBQUAD EGAColors[16] = {
402 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
403 { 0x00, 0x00, 0x00, 0x00 },
404 { 0x00, 0x00, 0x80, 0x00 },
405 { 0x00, 0x80, 0x00, 0x00 },
406 { 0x00, 0x80, 0x80, 0x00 },
407 { 0x80, 0x00, 0x00, 0x00 },
408 { 0x80, 0x00, 0x80, 0x00 },
409 { 0x80, 0x80, 0x00, 0x00 },
410 { 0x80, 0x80, 0x80, 0x00 },
411 { 0xc0, 0xc0, 0xc0, 0x00 },
412 { 0x00, 0x00, 0xff, 0x00 },
413 { 0x00, 0xff, 0x00, 0x00 },
414 { 0x00, 0xff, 0xff, 0x00 },
415 { 0xff, 0x00, 0x00, 0x00 },
416 { 0xff, 0x00, 0xff, 0x00 },
417 { 0xff, 0xff, 0x00, 0x00 },
418 { 0xff, 0xff, 0xff, 0x00 }
422 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
423 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
424 { 0x00, 0x00, 0x00, 0x00 },
425 { 0x00, 0x00, 0x80, 0x00 },
426 { 0x00, 0x80, 0x00, 0x00 },
427 { 0x00, 0x80, 0x80, 0x00 },
428 { 0x80, 0x00, 0x00, 0x00 },
429 { 0x80, 0x00, 0x80, 0x00 },
430 { 0x80, 0x80, 0x00, 0x00 },
431 { 0xc0, 0xc0, 0xc0, 0x00 },
432 { 0xc0, 0xdc, 0xc0, 0x00 },
433 { 0xf0, 0xca, 0xa6, 0x00 },
434 { 0xf0, 0xfb, 0xff, 0x00 },
435 { 0xa4, 0xa0, 0xa0, 0x00 },
436 { 0x80, 0x80, 0x80, 0x00 },
437 { 0x00, 0x00, 0xf0, 0x00 },
438 { 0x00, 0xff, 0x00, 0x00 },
439 { 0x00, 0xff, 0xff, 0x00 },
440 { 0xff, 0x00, 0x00, 0x00 },
441 { 0xff, 0x00, 0xff, 0x00 },
442 { 0xff, 0xff, 0x00, 0x00 },
443 { 0xff, 0xff, 0xff, 0x00 }
446 /***********************************************************************
447 * GetDIBits16 (GDI.441)
449 INT16 WINAPI GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
450 UINT16 lines, LPVOID bits, BITMAPINFO * info,
451 UINT16 coloruse )
453 return GetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
457 /******************************************************************************
458 * GetDIBits [GDI32.170] Retrieves bits of bitmap and copies to buffer
460 * RETURNS
461 * Success: Number of scan lines copied from bitmap
462 * Failure: 0
464 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
466 INT WINAPI GetDIBits(
467 HDC hdc, /* [in] Handle to device context */
468 HBITMAP hbitmap, /* [in] Handle to bitmap */
469 UINT startscan, /* [in] First scan line to set in dest bitmap */
470 UINT lines, /* [in] Number of scan lines to copy */
471 LPVOID bits, /* [out] Address of array for bitmap bits */
472 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
473 UINT coloruse) /* [in] RGB or palette index */
475 DC * dc;
476 BITMAPOBJ * bmp;
477 PALETTEENTRY * palEntry;
478 PALETTEOBJ * palette;
479 int i;
481 if (!info) return 0;
482 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
483 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
485 GDI_ReleaseObj( hdc );
486 return 0;
488 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC )))
490 GDI_ReleaseObj( hdc );
491 GDI_ReleaseObj( hbitmap );
492 return 0;
495 /* Transfer color info */
497 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
499 info->bmiHeader.biClrUsed = 0;
501 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
502 palEntry = palette->logpalette.palPalEntry;
503 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
504 if (coloruse == DIB_RGB_COLORS) {
505 info->bmiColors[i].rgbRed = palEntry->peRed;
506 info->bmiColors[i].rgbGreen = palEntry->peGreen;
507 info->bmiColors[i].rgbBlue = palEntry->peBlue;
508 info->bmiColors[i].rgbReserved = 0;
510 else ((WORD *)info->bmiColors)[i] = (WORD)i;
512 } else {
513 switch (info->bmiHeader.biBitCount) {
514 case 1:
515 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
516 info->bmiColors[0].rgbBlue = 0;
517 info->bmiColors[0].rgbReserved = 0;
518 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
519 info->bmiColors[1].rgbBlue = 0xff;
520 info->bmiColors[1].rgbReserved = 0;
521 break;
523 case 4:
524 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
525 break;
527 case 8:
529 INT r, g, b;
530 RGBQUAD *color;
532 memcpy(info->bmiColors, DefLogPalette,
533 10 * sizeof(RGBQUAD));
534 memcpy(info->bmiColors + 246, DefLogPalette + 10,
535 10 * sizeof(RGBQUAD));
536 color = info->bmiColors + 10;
537 for(r = 0; r <= 5; r++) /* FIXME */
538 for(g = 0; g <= 5; g++)
539 for(b = 0; b <= 5; b++) {
540 color->rgbRed = (r * 0xff) / 5;
541 color->rgbGreen = (g * 0xff) / 5;
542 color->rgbBlue = (b * 0xff) / 5;
543 color->rgbReserved = 0;
544 color++;
551 GDI_ReleaseObj( dc->hPalette );
553 if (bits && lines)
555 /* If the bitmap object already have a dib section that contains image data, get the bits from it*/
556 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
558 /*FIXME: Only RGB dibs supported for now */
559 int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
560 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
561 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
562 int x, y;
564 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
566 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
567 dstwidthb = -dstwidthb;
570 switch( info->bmiHeader.biBitCount ) {
572 case 15:
573 case 16: /* 16 bpp dstDIB */
575 LPWORD dstbits = (LPWORD)dbits;
576 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
578 /* FIXME: BI_BITFIELDS not supported yet */
580 switch(bmp->dib->dsBm.bmBitsPixel) {
582 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
584 /* FIXME: BI_BITFIELDS not supported yet */
585 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
586 memcpy(dbits, sbits, srcwidthb);
588 break;
590 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
592 LPBYTE srcbits = sbits;
594 for( y = 0; y < lines; y++) {
595 for( x = 0; x < srcwidth; x++ )
596 *dstbits++ = ((*srcbits++ >> 3) & bmask) |
597 (((WORD)*srcbits++ << 2) & gmask) |
598 (((WORD)*srcbits++ << 7) & rmask);
599 dstbits = (LPWORD)(dbits+=dstwidthb);
600 srcbits = (sbits += srcwidthb);
603 break;
605 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
607 LPDWORD srcbits = (LPDWORD)sbits;
608 DWORD val;
610 for( y = 0; y < lines; y++) {
611 for( x = 0; x < srcwidth; x++ ) {
612 val = *srcbits++;
613 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
614 ((val >> 9) & rmask));
616 dstbits = (LPWORD)(dbits+=dstwidthb);
617 srcbits = (LPDWORD)(sbits+=srcwidthb);
620 break;
622 default: /* ? bit bmp -> 16 bit DIB */
623 FIXME("15/16 bit DIB %d bit bitmap\n",
624 bmp->bitmap.bmBitsPixel);
625 break;
628 break;
630 case 24: /* 24 bpp dstDIB */
632 LPBYTE dstbits = dbits;
634 switch(bmp->dib->dsBm.bmBitsPixel) {
636 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
638 LPWORD srcbits = (LPWORD)sbits;
639 WORD val;
641 /* FIXME: BI_BITFIELDS not supported yet */
642 for( y = 0; y < lines; y++) {
643 for( x = 0; x < srcwidth; x++ ) {
644 val = *srcbits++;
645 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
646 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
647 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
649 dstbits = (LPBYTE)(dbits+=dstwidthb);
650 srcbits = (LPWORD)(sbits+=srcwidthb);
653 break;
655 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
657 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
658 memcpy(dbits, sbits, srcwidthb);
660 break;
662 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
664 LPBYTE srcbits = (LPBYTE)sbits;
666 for( y = 0; y < lines; y++) {
667 for( x = 0; x < srcwidth; x++, srcbits++ ) {
668 *dstbits++ = *srcbits++;
669 *dstbits++ = *srcbits++;
670 *dstbits++ = *srcbits++;
672 dstbits=(LPBYTE)(dbits+=dstwidthb);
673 srcbits = (LPBYTE)(sbits+=srcwidthb);
676 break;
678 default: /* ? bit bmp -> 24 bit DIB */
679 FIXME("24 bit DIB %d bit bitmap\n",
680 bmp->bitmap.bmBitsPixel);
681 break;
684 break;
686 case 32: /* 32 bpp dstDIB */
688 LPDWORD dstbits = (LPDWORD)dbits;
690 /* FIXME: BI_BITFIELDS not supported yet */
692 switch(bmp->dib->dsBm.bmBitsPixel) {
693 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
695 LPWORD srcbits = (LPWORD)sbits;
696 DWORD val;
698 /* FIXME: BI_BITFIELDS not supported yet */
699 for( y = 0; y < lines; y++) {
700 for( x = 0; x < srcwidth; x++ ) {
701 val = (DWORD)*srcbits++;
702 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
703 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
704 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
706 dstbits=(LPDWORD)(dbits+=dstwidthb);
707 srcbits=(LPWORD)(sbits+=srcwidthb);
710 break;
712 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
714 LPBYTE srcbits = sbits;
716 for( y = 0; y < lines; y++) {
717 for( x = 0; x < srcwidth; x++, srcbits+=3 )
718 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
719 dstbits=(LPDWORD)(dbits+=dstwidthb);
720 srcbits=(sbits+=srcwidthb);
723 break;
725 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
727 /* FIXME: BI_BITFIELDS not supported yet */
728 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
729 memcpy(dbits, sbits, srcwidthb);
731 break;
733 default: /* ? bit bmp -> 16 bit DIB */
734 FIXME("15/16 bit DIB %d bit bitmap\n",
735 bmp->bitmap.bmBitsPixel);
736 break;
739 break;
741 default: /* ? bit DIB */
742 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
743 break;
746 /* Otherwise, get bits from the XImage */
747 else if(!BITMAP_Driver->pGetDIBits(bmp, dc, startscan, lines, bits, info, coloruse, hbitmap))
749 GDI_ReleaseObj( hdc );
750 GDI_ReleaseObj( hbitmap );
752 return 0;
755 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
757 /* fill in struct members */
759 if( info->bmiHeader.biBitCount == 0)
761 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
762 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
763 info->bmiHeader.biPlanes = 1;
764 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
765 info->bmiHeader.biSizeImage =
766 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
767 bmp->bitmap.bmHeight,
768 bmp->bitmap.bmBitsPixel );
769 info->bmiHeader.biCompression = 0;
771 else
773 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
774 info->bmiHeader.biWidth,
775 info->bmiHeader.biHeight,
776 info->bmiHeader.biBitCount );
780 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
781 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
782 info->bmiHeader.biHeight);
784 GDI_ReleaseObj( hdc );
785 GDI_ReleaseObj( hbitmap );
787 return lines;
791 /***********************************************************************
792 * CreateDIBitmap16 (GDI.442)
794 HBITMAP16 WINAPI CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
795 DWORD init, LPCVOID bits, const BITMAPINFO * data,
796 UINT16 coloruse )
798 return CreateDIBitmap( hdc, header, init, bits, data, coloruse );
802 /***********************************************************************
803 * CreateDIBitmap (GDI32.37)
805 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
806 DWORD init, LPCVOID bits, const BITMAPINFO *data,
807 UINT coloruse )
809 HBITMAP handle;
810 BOOL fColor;
811 DWORD width;
812 int height;
813 WORD bpp;
814 WORD compr;
816 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
817 if (height < 0) height = -height;
819 /* Check if we should create a monochrome or color bitmap. */
820 /* We create a monochrome bitmap only if it has exactly 2 */
821 /* colors, which are black followed by white, nothing else. */
822 /* In all other cases, we create a color bitmap. */
824 if (bpp != 1) fColor = TRUE;
825 else if ((coloruse != DIB_RGB_COLORS) ||
826 (init != CBM_INIT) || !data) fColor = FALSE;
827 else
829 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
831 RGBQUAD *rgb = data->bmiColors;
832 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
834 /* Check if the first color of the colormap is black */
835 if ((col == RGB(0,0,0)))
837 rgb++;
838 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
839 /* If the second color is white, create a monochrome bitmap */
840 fColor = (col != RGB(0xff,0xff,0xff));
842 /* Note : If the first color of the colormap is white
843 followed by black, we have to create a color bitmap.
844 If we don't the white will be displayed in black later on!*/
845 else fColor = TRUE;
847 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
849 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
850 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
851 if ((col == RGB(0,0,0)))
853 rgb++;
854 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
855 fColor = (col != RGB(0xff,0xff,0xff));
857 else fColor = TRUE;
859 else
861 WARN("(%ld): wrong size for data\n",
862 data->bmiHeader.biSize );
863 return 0;
867 /* Now create the bitmap */
869 if (fColor)
871 HDC tmpdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
872 handle = CreateCompatibleBitmap( tmpdc, width, height );
873 DeleteDC( tmpdc );
875 else handle = CreateBitmap( width, height, 1, 1, NULL );
877 if (!handle) return 0;
879 if (init == CBM_INIT)
880 SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
881 return handle;
884 /***********************************************************************
885 * CreateDIBSection16 (GDI.489)
887 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
888 SEGPTR *bits, HANDLE section,
889 DWORD offset)
891 HBITMAP16 hbitmap = 0;
892 DC *dc;
893 BOOL bDesktopDC = FALSE;
895 /* If the reference hdc is null, take the desktop dc */
896 if (hdc == 0)
898 hdc = CreateCompatibleDC(0);
899 bDesktopDC = TRUE;
902 if ((dc = DC_GetDCPtr( hdc )))
904 hbitmap = dc->funcs->pCreateDIBSection16(dc, bmi, usage, bits, section, offset, 0);
905 GDI_ReleaseObj(hdc);
908 if (bDesktopDC)
909 DeleteDC(hdc);
911 return hbitmap;
914 /***********************************************************************
915 * DIB_CreateDIBSection
917 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
918 LPVOID *bits, HANDLE section,
919 DWORD offset, DWORD ovr_pitch)
921 HBITMAP hbitmap = 0;
922 DC *dc;
923 BOOL bDesktopDC = FALSE;
925 /* If the reference hdc is null, take the desktop dc */
926 if (hdc == 0)
928 hdc = CreateCompatibleDC(0);
929 bDesktopDC = TRUE;
932 if ((dc = DC_GetDCPtr( hdc )))
934 hbitmap = dc->funcs->pCreateDIBSection(dc, bmi, usage, bits, section, offset, ovr_pitch);
935 GDI_ReleaseObj(hdc);
938 if (bDesktopDC)
939 DeleteDC(hdc);
941 return hbitmap;
944 /***********************************************************************
945 * CreateDIBSection (GDI32.36)
947 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
948 LPVOID *bits, HANDLE section,
949 DWORD offset)
951 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
954 /***********************************************************************
955 * DIB_DeleteDIBSection
957 void DIB_DeleteDIBSection( BITMAPOBJ *bmp )
959 if (bmp && bmp->dib)
961 DIBSECTION *dib = bmp->dib;
963 if (dib->dsBm.bmBits)
965 if (dib->dshSection)
966 UnmapViewOfFile(dib->dsBm.bmBits);
967 else if (!dib->dsOffset)
968 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
971 BITMAP_Driver->pDeleteDIBSection(bmp);
973 HeapFree(GetProcessHeap(), 0, dib);
974 bmp->dib = NULL;
978 /***********************************************************************
979 * DIB_CreateDIBFromBitmap
980 * Allocates a packed DIB and copies the bitmap data into it.
982 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
984 BITMAPOBJ *pBmp = NULL;
985 HGLOBAL hPackedDIB = 0;
986 LPBYTE pPackedDIB = NULL;
987 LPBITMAPINFOHEADER pbmiHeader = NULL;
988 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
989 OffsetBits = 0, nLinesCopied = 0;
991 /* Get a pointer to the BITMAPOBJ structure */
992 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
993 if (!pBmp) return hPackedDIB;
995 /* Get the bitmap dimensions */
996 width = pBmp->bitmap.bmWidth;
997 height = pBmp->bitmap.bmHeight;
998 depth = pBmp->bitmap.bmBitsPixel;
1001 * A packed DIB contains a BITMAPINFO structure followed immediately by
1002 * an optional color palette and the pixel data.
1005 /* Calculate the size of the packed DIB */
1006 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
1007 cPackedSize = sizeof(BITMAPINFOHEADER)
1008 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
1009 + cDataSize;
1010 /* Get the offset to the bits */
1011 OffsetBits = cPackedSize - cDataSize;
1013 /* Allocate the packed DIB */
1014 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
1015 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
1016 cPackedSize );
1017 if ( !hPackedDIB )
1019 WARN("Could not allocate packed DIB!\n");
1020 goto END;
1023 /* A packed DIB starts with a BITMAPINFOHEADER */
1024 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
1025 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
1027 /* Init the BITMAPINFOHEADER */
1028 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
1029 pbmiHeader->biWidth = width;
1030 pbmiHeader->biHeight = height;
1031 pbmiHeader->biPlanes = 1;
1032 pbmiHeader->biBitCount = depth;
1033 pbmiHeader->biCompression = BI_RGB;
1034 pbmiHeader->biSizeImage = 0;
1035 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
1036 pbmiHeader->biClrUsed = 0;
1037 pbmiHeader->biClrImportant = 0;
1039 /* Retrieve the DIB bits from the bitmap and fill in the
1040 * DIB color table if present */
1042 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
1043 hBmp, /* Handle to bitmap */
1044 0, /* First scan line to set in dest bitmap */
1045 height, /* Number of scan lines to copy */
1046 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
1047 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
1048 0); /* RGB or palette index */
1049 GlobalUnlock(hPackedDIB);
1051 /* Cleanup if GetDIBits failed */
1052 if (nLinesCopied != height)
1054 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
1055 GlobalFree(hPackedDIB);
1056 hPackedDIB = 0;
1059 END:
1060 GDI_ReleaseObj( hBmp );
1061 return hPackedDIB;