Added support for non-deletable system brushes and pens created by
[wine/hacks.git] / objects / dib.c
blob4650d89f47d78257a18b9c2c4ea05cb7e7242fc3
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 hdcMem = CreateCompatibleDC( hdc );
149 if (info->bmiHeader.biCompression == BI_RLE4 ||
150 info->bmiHeader.biCompression == BI_RLE8) {
152 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
153 * contain all the rectangle described in bmiHeader, but only part of it.
154 * This mean that those undescribed pixels must be left untouched.
155 * So, we first copy on a memory bitmap the current content of the
156 * destination rectangle, blit the DIB bits on top of it - hence leaving
157 * the gaps untouched -, and blitting the rectangle back.
158 * This insure that gaps are untouched on the destination rectangle
159 * Not doing so leads to trashed images (the gaps contain what was on the
160 * memory bitmap => generally black or garbage)
161 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
162 * another speed vs correctness issue. Anyway, if speed is needed, then the
163 * pStretchDIBits function shall be implemented.
164 * ericP (2000/09/09)
166 hBitmap = CreateCompatibleBitmap(hdc, info->bmiHeader.biWidth,
167 info->bmiHeader.biHeight);
168 hOldBitmap = SelectObject( hdcMem, hBitmap );
170 /* copy existing bitmap from destination dc */
171 StretchBlt( hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
172 widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
173 dwRop );
174 SetDIBits(hdcMem, hBitmap, 0, info->bmiHeader.biHeight, bits,
175 info, DIB_RGB_COLORS);
177 } else {
178 hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
179 bits, info, wUsage );
180 hOldBitmap = SelectObject( hdcMem, hBitmap );
183 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
184 left (negative biHeight) */
185 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
186 hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
187 widthSrc, heightSrc, dwRop );
188 SelectObject( hdcMem, hOldBitmap );
189 DeleteDC( hdcMem );
190 DeleteObject( hBitmap );
192 GDI_ReleaseObj( hdc );
193 return heightSrc;
197 /***********************************************************************
198 * SetDIBits16 (GDI.440)
200 INT16 WINAPI SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
201 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
202 UINT16 coloruse )
204 return SetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
208 /******************************************************************************
209 * SetDIBits [GDI32.312] Sets pixels in a bitmap using colors from DIB
211 * PARAMS
212 * hdc [I] Handle to device context
213 * hbitmap [I] Handle to bitmap
214 * startscan [I] Starting scan line
215 * lines [I] Number of scan lines
216 * bits [I] Array of bitmap bits
217 * info [I] Address of structure with data
218 * coloruse [I] Type of color indexes to use
220 * RETURNS
221 * Success: Number of scan lines copied
222 * Failure: 0
224 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
225 UINT lines, LPCVOID bits, const BITMAPINFO *info,
226 UINT coloruse )
228 DC *dc;
229 BITMAPOBJ *bitmap;
230 INT result;
232 /* Check parameters */
233 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
235 if (!(bitmap = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
237 GDI_ReleaseObj( hdc );
238 return 0;
241 result = BITMAP_Driver->pSetDIBits(bitmap, dc, startscan,
242 lines, bits, info,
243 coloruse, hbitmap);
245 GDI_ReleaseObj( hbitmap );
246 GDI_ReleaseObj( hdc );
248 return result;
252 /***********************************************************************
253 * SetDIBitsToDevice16 (GDI.443)
255 INT16 WINAPI SetDIBitsToDevice16(HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
256 INT16 cy, INT16 xSrc, INT16 ySrc, UINT16 startscan,
257 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
258 UINT16 coloruse )
260 return SetDIBitsToDevice( hdc, xDest, yDest, cx, cy, xSrc, ySrc,
261 startscan, lines, bits, info, coloruse );
265 /***********************************************************************
266 * SetDIBitsToDevice (GDI32.313)
268 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
269 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
270 UINT lines, LPCVOID bits, const BITMAPINFO *info,
271 UINT coloruse )
273 INT ret;
274 DC *dc;
276 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
278 if(dc->funcs->pSetDIBitsToDevice)
279 ret = dc->funcs->pSetDIBitsToDevice( dc, xDest, yDest, cx, cy, xSrc,
280 ySrc, startscan, lines, bits,
281 info, coloruse );
282 else {
283 FIXME("unimplemented on hdc %08x\n", hdc);
284 ret = 0;
287 GDI_ReleaseObj( hdc );
288 return ret;
291 /***********************************************************************
292 * SetDIBColorTable16 (GDI.602)
294 UINT16 WINAPI SetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
295 RGBQUAD *colors )
297 return SetDIBColorTable( hdc, startpos, entries, colors );
300 /***********************************************************************
301 * SetDIBColorTable (GDI32.311)
303 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
304 RGBQUAD *colors )
306 DC * dc;
307 PALETTEENTRY * palEntry;
308 PALETTEOBJ * palette;
309 RGBQUAD *end;
311 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
313 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
315 GDI_ReleaseObj( hdc );
316 return 0;
319 /* Transfer color info */
321 if (dc->w.bitsPerPixel <= 8) {
322 palEntry = palette->logpalette.palPalEntry + startpos;
323 if (startpos + entries > (1 << dc->w.bitsPerPixel))
324 entries = (1 << dc->w.bitsPerPixel) - startpos;
326 if (startpos + entries > palette->logpalette.palNumEntries)
327 entries = palette->logpalette.palNumEntries - startpos;
329 for (end = colors + entries; colors < end; palEntry++, colors++)
331 palEntry->peRed = colors->rgbRed;
332 palEntry->peGreen = colors->rgbGreen;
333 palEntry->peBlue = colors->rgbBlue;
335 } else {
336 entries = 0;
338 GDI_ReleaseObj( dc->w.hPalette );
339 GDI_ReleaseObj( hdc );
340 return entries;
343 /***********************************************************************
344 * GetDIBColorTable16 (GDI.603)
346 UINT16 WINAPI GetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
347 RGBQUAD *colors )
349 return GetDIBColorTable( hdc, startpos, entries, colors );
352 /***********************************************************************
353 * GetDIBColorTable (GDI32.169)
355 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
356 RGBQUAD *colors )
358 DC * dc;
359 PALETTEENTRY * palEntry;
360 PALETTEOBJ * palette;
361 RGBQUAD *end;
363 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
365 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
367 GDI_ReleaseObj( hdc );
368 return 0;
371 /* Transfer color info */
373 if (dc->w.bitsPerPixel <= 8) {
374 palEntry = palette->logpalette.palPalEntry + startpos;
375 if (startpos + entries > (1 << dc->w.bitsPerPixel)) {
376 entries = (1 << dc->w.bitsPerPixel) - startpos;
378 for (end = colors + entries; colors < end; palEntry++, colors++)
380 colors->rgbRed = palEntry->peRed;
381 colors->rgbGreen = palEntry->peGreen;
382 colors->rgbBlue = palEntry->peBlue;
383 colors->rgbReserved = 0;
385 } else {
386 entries = 0;
388 GDI_ReleaseObj( dc->w.hPalette );
389 GDI_ReleaseObj( hdc );
390 return entries;
393 /* FIXME the following two structs should be combined with __sysPalTemplate in
394 objects/color.c - this should happen after de-X11-ing both of these
395 files.
396 NB. RGBQUAD and PALETTENTRY have different orderings of red, green
397 and blue - sigh */
399 static RGBQUAD EGAColors[16] = {
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 { 0x80, 0x80, 0x80, 0x00 },
409 { 0xc0, 0xc0, 0xc0, 0x00 },
410 { 0x00, 0x00, 0xff, 0x00 },
411 { 0x00, 0xff, 0x00, 0x00 },
412 { 0x00, 0xff, 0xff, 0x00 },
413 { 0xff, 0x00, 0x00, 0x00 },
414 { 0xff, 0x00, 0xff, 0x00 },
415 { 0xff, 0xff, 0x00, 0x00 },
416 { 0xff, 0xff, 0xff, 0x00 }
420 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
421 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
422 { 0x00, 0x00, 0x00, 0x00 },
423 { 0x00, 0x00, 0x80, 0x00 },
424 { 0x00, 0x80, 0x00, 0x00 },
425 { 0x00, 0x80, 0x80, 0x00 },
426 { 0x80, 0x00, 0x00, 0x00 },
427 { 0x80, 0x00, 0x80, 0x00 },
428 { 0x80, 0x80, 0x00, 0x00 },
429 { 0xc0, 0xc0, 0xc0, 0x00 },
430 { 0xc0, 0xdc, 0xc0, 0x00 },
431 { 0xf0, 0xca, 0xa6, 0x00 },
432 { 0xf0, 0xfb, 0xff, 0x00 },
433 { 0xa4, 0xa0, 0xa0, 0x00 },
434 { 0x80, 0x80, 0x80, 0x00 },
435 { 0x00, 0x00, 0xf0, 0x00 },
436 { 0x00, 0xff, 0x00, 0x00 },
437 { 0x00, 0xff, 0xff, 0x00 },
438 { 0xff, 0x00, 0x00, 0x00 },
439 { 0xff, 0x00, 0xff, 0x00 },
440 { 0xff, 0xff, 0x00, 0x00 },
441 { 0xff, 0xff, 0xff, 0x00 }
444 /***********************************************************************
445 * GetDIBits16 (GDI.441)
447 INT16 WINAPI GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
448 UINT16 lines, LPVOID bits, BITMAPINFO * info,
449 UINT16 coloruse )
451 return GetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
455 /******************************************************************************
456 * GetDIBits [GDI32.170] Retrieves bits of bitmap and copies to buffer
458 * RETURNS
459 * Success: Number of scan lines copied from bitmap
460 * Failure: 0
462 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
464 INT WINAPI GetDIBits(
465 HDC hdc, /* [in] Handle to device context */
466 HBITMAP hbitmap, /* [in] Handle to bitmap */
467 UINT startscan, /* [in] First scan line to set in dest bitmap */
468 UINT lines, /* [in] Number of scan lines to copy */
469 LPVOID bits, /* [out] Address of array for bitmap bits */
470 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
471 UINT coloruse) /* [in] RGB or palette index */
473 DC * dc;
474 BITMAPOBJ * bmp;
475 PALETTEENTRY * palEntry;
476 PALETTEOBJ * palette;
477 int i;
479 if (!info) return 0;
480 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
481 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
483 GDI_ReleaseObj( hdc );
484 return 0;
486 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
488 GDI_ReleaseObj( hdc );
489 GDI_ReleaseObj( hbitmap );
490 return 0;
493 /* Transfer color info */
495 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
497 info->bmiHeader.biClrUsed = 0;
499 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
500 palEntry = palette->logpalette.palPalEntry;
501 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
502 if (coloruse == DIB_RGB_COLORS) {
503 info->bmiColors[i].rgbRed = palEntry->peRed;
504 info->bmiColors[i].rgbGreen = palEntry->peGreen;
505 info->bmiColors[i].rgbBlue = palEntry->peBlue;
506 info->bmiColors[i].rgbReserved = 0;
508 else ((WORD *)info->bmiColors)[i] = (WORD)i;
510 } else {
511 switch (info->bmiHeader.biBitCount) {
512 case 1:
513 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
514 info->bmiColors[0].rgbBlue = 0;
515 info->bmiColors[0].rgbReserved = 0;
516 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
517 info->bmiColors[1].rgbBlue = 0xff;
518 info->bmiColors[1].rgbReserved = 0;
519 break;
521 case 4:
522 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
523 break;
525 case 8:
527 INT r, g, b;
528 RGBQUAD *color;
530 memcpy(info->bmiColors, DefLogPalette,
531 10 * sizeof(RGBQUAD));
532 memcpy(info->bmiColors + 246, DefLogPalette + 10,
533 10 * sizeof(RGBQUAD));
534 color = info->bmiColors + 10;
535 for(r = 0; r <= 5; r++) /* FIXME */
536 for(g = 0; g <= 5; g++)
537 for(b = 0; b <= 5; b++) {
538 color->rgbRed = (r * 0xff) / 5;
539 color->rgbGreen = (g * 0xff) / 5;
540 color->rgbBlue = (b * 0xff) / 5;
541 color->rgbReserved = 0;
542 color++;
549 GDI_ReleaseObj( dc->w.hPalette );
551 if (bits && lines)
553 /* If the bitmap object already have a dib section that contains image data, get the bits from it*/
554 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
556 /*FIXME: Only RGB dibs supported for now */
557 int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
558 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
559 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
560 int x, y;
562 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
564 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
565 dstwidthb = -dstwidthb;
568 switch( info->bmiHeader.biBitCount ) {
570 case 15:
571 case 16: /* 16 bpp dstDIB */
573 LPWORD dstbits = (LPWORD)dbits;
574 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
576 /* FIXME: BI_BITFIELDS not supported yet */
578 switch(bmp->dib->dsBm.bmBitsPixel) {
580 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
582 /* FIXME: BI_BITFIELDS not supported yet */
583 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
584 memcpy(dbits, sbits, srcwidthb);
586 break;
588 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
590 LPBYTE srcbits = sbits;
592 for( y = 0; y < lines; y++) {
593 for( x = 0; x < srcwidth; x++ )
594 *dstbits++ = ((*srcbits++ >> 3) & bmask) |
595 (((WORD)*srcbits++ << 2) & gmask) |
596 (((WORD)*srcbits++ << 7) & rmask);
597 dstbits = (LPWORD)(dbits+=dstwidthb);
598 srcbits = (sbits += srcwidthb);
601 break;
603 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
605 LPDWORD srcbits = (LPDWORD)sbits;
606 DWORD val;
608 for( y = 0; y < lines; y++) {
609 for( x = 0; x < srcwidth; x++ ) {
610 val = *srcbits++;
611 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
612 ((val >> 9) & rmask));
614 dstbits = (LPWORD)(dbits+=dstwidthb);
615 srcbits = (LPDWORD)(sbits+=srcwidthb);
618 break;
620 default: /* ? bit bmp -> 16 bit DIB */
621 FIXME("15/16 bit DIB %d bit bitmap\n",
622 bmp->bitmap.bmBitsPixel);
623 break;
626 break;
628 case 24: /* 24 bpp dstDIB */
630 LPBYTE dstbits = dbits;
632 switch(bmp->dib->dsBm.bmBitsPixel) {
634 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
636 LPWORD srcbits = (LPWORD)sbits;
637 WORD val;
639 /* FIXME: BI_BITFIELDS not supported yet */
640 for( y = 0; y < lines; y++) {
641 for( x = 0; x < srcwidth; x++ ) {
642 val = *srcbits++;
643 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
644 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
645 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
647 dstbits = (LPBYTE)(dbits+=dstwidthb);
648 srcbits = (LPWORD)(sbits+=srcwidthb);
651 break;
653 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
655 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
656 memcpy(dbits, sbits, srcwidthb);
658 break;
660 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
662 LPBYTE srcbits = (LPBYTE)sbits;
664 for( y = 0; y < lines; y++) {
665 for( x = 0; x < srcwidth; x++, srcbits++ ) {
666 *dstbits++ = *srcbits++;
667 *dstbits++ = *srcbits++;
668 *dstbits++ = *srcbits++;
670 dstbits=(LPBYTE)(dbits+=dstwidthb);
671 srcbits = (LPBYTE)(sbits+=srcwidthb);
674 break;
676 default: /* ? bit bmp -> 24 bit DIB */
677 FIXME("24 bit DIB %d bit bitmap\n",
678 bmp->bitmap.bmBitsPixel);
679 break;
682 break;
684 case 32: /* 32 bpp dstDIB */
686 LPDWORD dstbits = (LPDWORD)dbits;
688 /* FIXME: BI_BITFIELDS not supported yet */
690 switch(bmp->dib->dsBm.bmBitsPixel) {
691 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
693 LPWORD srcbits = (LPWORD)sbits;
694 DWORD val;
696 /* FIXME: BI_BITFIELDS not supported yet */
697 for( y = 0; y < lines; y++) {
698 for( x = 0; x < srcwidth; x++ ) {
699 val = (DWORD)*srcbits++;
700 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
701 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
702 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
704 dstbits=(LPDWORD)(dbits+=dstwidthb);
705 srcbits=(LPWORD)(sbits+=srcwidthb);
708 break;
710 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
712 LPBYTE srcbits = sbits;
714 for( y = 0; y < lines; y++) {
715 for( x = 0; x < srcwidth; x++, srcbits+=3 )
716 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
717 dstbits=(LPDWORD)(dbits+=dstwidthb);
718 srcbits=(sbits+=srcwidthb);
721 break;
723 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
725 /* FIXME: BI_BITFIELDS not supported yet */
726 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
727 memcpy(dbits, sbits, srcwidthb);
729 break;
731 default: /* ? bit bmp -> 16 bit DIB */
732 FIXME("15/16 bit DIB %d bit bitmap\n",
733 bmp->bitmap.bmBitsPixel);
734 break;
737 break;
739 default: /* ? bit DIB */
740 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
741 break;
744 /* Otherwise, get bits from the XImage */
745 else if(!BITMAP_Driver->pGetDIBits(bmp, dc, startscan, lines, bits, info, coloruse, hbitmap))
747 GDI_ReleaseObj( hdc );
748 GDI_ReleaseObj( hbitmap );
750 return 0;
753 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
755 /* fill in struct members */
757 if( info->bmiHeader.biBitCount == 0)
759 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
760 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
761 info->bmiHeader.biPlanes = 1;
762 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
763 info->bmiHeader.biSizeImage =
764 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
765 bmp->bitmap.bmHeight,
766 bmp->bitmap.bmBitsPixel );
767 info->bmiHeader.biCompression = 0;
769 else
771 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
772 info->bmiHeader.biWidth,
773 info->bmiHeader.biHeight,
774 info->bmiHeader.biBitCount );
778 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
779 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
780 info->bmiHeader.biHeight);
782 GDI_ReleaseObj( hdc );
783 GDI_ReleaseObj( hbitmap );
785 return lines;
789 /***********************************************************************
790 * CreateDIBitmap16 (GDI.442)
792 HBITMAP16 WINAPI CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
793 DWORD init, LPCVOID bits, const BITMAPINFO * data,
794 UINT16 coloruse )
796 return CreateDIBitmap( hdc, header, init, bits, data, coloruse );
800 /***********************************************************************
801 * CreateDIBitmap (GDI32.37)
803 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
804 DWORD init, LPCVOID bits, const BITMAPINFO *data,
805 UINT coloruse )
807 HBITMAP handle;
808 BOOL fColor;
809 DWORD width;
810 int height;
811 WORD bpp;
812 WORD compr;
814 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
815 if (height < 0) height = -height;
817 /* Check if we should create a monochrome or color bitmap. */
818 /* We create a monochrome bitmap only if it has exactly 2 */
819 /* colors, which are black followed by white, nothing else. */
820 /* In all other cases, we create a color bitmap. */
822 if (bpp != 1) fColor = TRUE;
823 else if ((coloruse != DIB_RGB_COLORS) ||
824 (init != CBM_INIT) || !data) fColor = FALSE;
825 else
827 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
829 RGBQUAD *rgb = data->bmiColors;
830 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
832 /* Check if the first color of the colormap is black */
833 if ((col == RGB(0,0,0)))
835 rgb++;
836 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
837 /* If the second color is white, create a monochrome bitmap */
838 fColor = (col != RGB(0xff,0xff,0xff));
840 /* Note : If the first color of the colormap is white
841 followed by black, we have to create a color bitmap.
842 If we don't the white will be displayed in black later on!*/
843 else fColor = TRUE;
845 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
847 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
848 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
849 if ((col == RGB(0,0,0)))
851 rgb++;
852 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
853 fColor = (col != RGB(0xff,0xff,0xff));
855 else fColor = TRUE;
857 else
859 WARN("(%ld): wrong size for data\n",
860 data->bmiHeader.biSize );
861 return 0;
865 /* Now create the bitmap */
867 if (fColor)
869 HDC tmpdc = CreateDCA( "DISPLAY", NULL, NULL, NULL );
870 handle = CreateCompatibleBitmap( tmpdc, width, height );
871 DeleteDC( tmpdc );
873 else handle = CreateBitmap( width, height, 1, 1, NULL );
875 if (!handle) return 0;
877 if (init == CBM_INIT)
878 SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
879 return handle;
882 /***********************************************************************
883 * CreateDIBSection16 (GDI.489)
885 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
886 SEGPTR *bits, HANDLE section,
887 DWORD offset)
889 HBITMAP16 hbitmap = 0;
890 DC *dc;
891 BOOL bDesktopDC = FALSE;
893 /* If the reference hdc is null, take the desktop dc */
894 if (hdc == 0)
896 hdc = CreateCompatibleDC(0);
897 bDesktopDC = TRUE;
900 if ((dc = DC_GetDCPtr( hdc )))
902 hbitmap = dc->funcs->pCreateDIBSection16(dc, bmi, usage, bits, section, offset, 0);
903 GDI_ReleaseObj(hdc);
906 if (bDesktopDC)
907 DeleteDC(hdc);
909 return hbitmap;
912 /***********************************************************************
913 * DIB_CreateDIBSection
915 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
916 LPVOID *bits, HANDLE section,
917 DWORD offset, DWORD ovr_pitch)
919 HBITMAP hbitmap = 0;
920 DC *dc;
921 BOOL bDesktopDC = FALSE;
923 /* If the reference hdc is null, take the desktop dc */
924 if (hdc == 0)
926 hdc = CreateCompatibleDC(0);
927 bDesktopDC = TRUE;
930 if ((dc = DC_GetDCPtr( hdc )))
932 hbitmap = dc->funcs->pCreateDIBSection(dc, bmi, usage, bits, section, offset, ovr_pitch);
933 GDI_ReleaseObj(hdc);
936 if (bDesktopDC)
937 DeleteDC(hdc);
939 return hbitmap;
942 /***********************************************************************
943 * CreateDIBSection (GDI32.36)
945 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
946 LPVOID *bits, HANDLE section,
947 DWORD offset)
949 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
952 /***********************************************************************
953 * DIB_DeleteDIBSection
955 void DIB_DeleteDIBSection( BITMAPOBJ *bmp )
957 if (bmp && bmp->dib)
959 DIBSECTION *dib = bmp->dib;
961 if (dib->dsBm.bmBits)
963 if (dib->dshSection)
964 UnmapViewOfFile(dib->dsBm.bmBits);
965 else if (!dib->dsOffset)
966 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
969 BITMAP_Driver->pDeleteDIBSection(bmp);
971 HeapFree(GetProcessHeap(), 0, dib);
972 bmp->dib = NULL;
976 /***********************************************************************
977 * DIB_CreateDIBFromBitmap
978 * Allocates a packed DIB and copies the bitmap data into it.
980 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
982 BITMAPOBJ *pBmp = NULL;
983 HGLOBAL hPackedDIB = 0;
984 LPBYTE pPackedDIB = NULL;
985 LPBITMAPINFOHEADER pbmiHeader = NULL;
986 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
987 OffsetBits = 0, nLinesCopied = 0;
989 /* Get a pointer to the BITMAPOBJ structure */
990 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
991 if (!pBmp) return hPackedDIB;
993 /* Get the bitmap dimensions */
994 width = pBmp->bitmap.bmWidth;
995 height = pBmp->bitmap.bmHeight;
996 depth = pBmp->bitmap.bmBitsPixel;
999 * A packed DIB contains a BITMAPINFO structure followed immediately by
1000 * an optional color palette and the pixel data.
1003 /* Calculate the size of the packed DIB */
1004 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
1005 cPackedSize = sizeof(BITMAPINFOHEADER)
1006 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
1007 + cDataSize;
1008 /* Get the offset to the bits */
1009 OffsetBits = cPackedSize - cDataSize;
1011 /* Allocate the packed DIB */
1012 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
1013 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
1014 cPackedSize );
1015 if ( !hPackedDIB )
1017 WARN("Could not allocate packed DIB!\n");
1018 goto END;
1021 /* A packed DIB starts with a BITMAPINFOHEADER */
1022 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
1023 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
1025 /* Init the BITMAPINFOHEADER */
1026 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
1027 pbmiHeader->biWidth = width;
1028 pbmiHeader->biHeight = height;
1029 pbmiHeader->biPlanes = 1;
1030 pbmiHeader->biBitCount = depth;
1031 pbmiHeader->biCompression = BI_RGB;
1032 pbmiHeader->biSizeImage = 0;
1033 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
1034 pbmiHeader->biClrUsed = 0;
1035 pbmiHeader->biClrImportant = 0;
1037 /* Retrieve the DIB bits from the bitmap and fill in the
1038 * DIB color table if present */
1040 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
1041 hBmp, /* Handle to bitmap */
1042 0, /* First scan line to set in dest bitmap */
1043 height, /* Number of scan lines to copy */
1044 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
1045 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
1046 0); /* RGB or palette index */
1047 GlobalUnlock(hPackedDIB);
1049 /* Cleanup if GetDIBits failed */
1050 if (nLinesCopied != height)
1052 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
1053 GlobalFree(hPackedDIB);
1054 hPackedDIB = 0;
1057 END:
1058 GDI_ReleaseObj( hBmp );
1059 return hPackedDIB;