Altered WM_MOUSEHOVER so the lParam and wParam fields are set
[wine/multimedia.git] / objects / dib.c
blob1a734e40239d1e86a521ef7daba178e3e2abc500
1 /*
2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdlib.h>
22 #include <string.h>
24 #include "winbase.h"
25 #include "bitmap.h"
26 #include "selectors.h"
27 #include "gdi.h"
28 #include "wownt32.h"
29 #include "wine/debug.h"
30 #include "palette.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
34 /***********************************************************************
35 * DIB_GetDIBWidthBytes
37 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
38 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
40 int DIB_GetDIBWidthBytes( int width, int depth )
42 int words;
44 switch(depth)
46 case 1: words = (width + 31) / 32; break;
47 case 4: words = (width + 7) / 8; break;
48 case 8: words = (width + 3) / 4; break;
49 case 15:
50 case 16: words = (width + 1) / 2; break;
51 case 24: words = (width * 3 + 3)/4; break;
53 default:
54 WARN("(%d): Unsupported depth\n", depth );
55 /* fall through */
56 case 32:
57 words = width;
59 return 4 * words;
62 /***********************************************************************
63 * DIB_GetDIBImageBytes
65 * Return the number of bytes used to hold the image in a DIB bitmap.
67 int DIB_GetDIBImageBytes( int width, int height, int depth )
69 return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
73 /***********************************************************************
74 * DIB_BitmapInfoSize
76 * Return the size of the bitmap info structure including color table.
78 int DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
80 int colors;
82 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
84 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
85 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
86 return sizeof(BITMAPCOREHEADER) + colors *
87 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
89 else /* assume BITMAPINFOHEADER */
91 colors = info->bmiHeader.biClrUsed;
92 if (!colors && (info->bmiHeader.biBitCount <= 8))
93 colors = 1 << info->bmiHeader.biBitCount;
94 return sizeof(BITMAPINFOHEADER) + colors *
95 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
100 /***********************************************************************
101 * DIB_GetBitmapInfo
103 * Get the info from a bitmap header.
104 * Return 1 for INFOHEADER, 0 for COREHEADER,
105 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
107 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
108 int *height, WORD *bpp, WORD *compr )
110 if (header->biSize == sizeof(BITMAPINFOHEADER))
112 *width = header->biWidth;
113 *height = header->biHeight;
114 *bpp = header->biBitCount;
115 *compr = header->biCompression;
116 return 1;
118 if (header->biSize == sizeof(BITMAPCOREHEADER))
120 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
121 *width = core->bcWidth;
122 *height = core->bcHeight;
123 *bpp = core->bcBitCount;
124 *compr = 0;
125 return 0;
127 if (header->biSize == sizeof(BITMAPV4HEADER))
129 BITMAPV4HEADER *v4hdr = (BITMAPV4HEADER *)header;
130 *width = v4hdr->bV4Width;
131 *height = v4hdr->bV4Height;
132 *bpp = v4hdr->bV4BitCount;
133 *compr = v4hdr->bV4V4Compression;
134 return 4;
136 if (header->biSize == sizeof(BITMAPV5HEADER))
138 BITMAPV5HEADER *v5hdr = (BITMAPV5HEADER *)header;
139 *width = v5hdr->bV5Width;
140 *height = v5hdr->bV5Height;
141 *bpp = v5hdr->bV5BitCount;
142 *compr = v5hdr->bV5Compression;
143 return 5;
145 ERR("(%ld): unknown/wrong size for header\n", header->biSize );
146 return -1;
150 /***********************************************************************
151 * StretchDIBits (GDI32.@)
153 INT WINAPI StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
154 INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
155 INT heightSrc, const void *bits,
156 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
158 DC *dc = DC_GetDCUpdate( hdc );
159 if(!dc) return FALSE;
161 if(dc->funcs->pStretchDIBits)
163 heightSrc = dc->funcs->pStretchDIBits(dc->physDev, xDst, yDst, widthDst,
164 heightDst, xSrc, ySrc, widthSrc,
165 heightSrc, bits, info, wUsage, dwRop);
166 GDI_ReleaseObj( hdc );
168 else /* use StretchBlt */
170 HBITMAP hBitmap, hOldBitmap;
171 HDC hdcMem;
173 GDI_ReleaseObj( hdc );
174 hdcMem = CreateCompatibleDC( hdc );
175 if (info->bmiHeader.biCompression == BI_RLE4 ||
176 info->bmiHeader.biCompression == BI_RLE8) {
178 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
179 * contain all the rectangle described in bmiHeader, but only part of it.
180 * This mean that those undescribed pixels must be left untouched.
181 * So, we first copy on a memory bitmap the current content of the
182 * destination rectangle, blit the DIB bits on top of it - hence leaving
183 * the gaps untouched -, and blitting the rectangle back.
184 * This insure that gaps are untouched on the destination rectangle
185 * Not doing so leads to trashed images (the gaps contain what was on the
186 * memory bitmap => generally black or garbage)
187 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
188 * another speed vs correctness issue. Anyway, if speed is needed, then the
189 * pStretchDIBits function shall be implemented.
190 * ericP (2000/09/09)
192 hBitmap = CreateCompatibleBitmap(hdc, info->bmiHeader.biWidth,
193 info->bmiHeader.biHeight);
194 hOldBitmap = SelectObject( hdcMem, hBitmap );
196 /* copy existing bitmap from destination dc */
197 StretchBlt( hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
198 widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
199 dwRop );
200 SetDIBits(hdcMem, hBitmap, 0, info->bmiHeader.biHeight, bits,
201 info, DIB_RGB_COLORS);
203 } else {
204 hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
205 bits, info, wUsage );
206 hOldBitmap = SelectObject( hdcMem, hBitmap );
209 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
210 left (negative biHeight) */
211 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
212 hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
213 widthSrc, heightSrc, dwRop );
214 SelectObject( hdcMem, hOldBitmap );
215 DeleteDC( hdcMem );
216 DeleteObject( hBitmap );
218 return heightSrc;
222 /******************************************************************************
223 * SetDIBits [GDI32.@] Sets pixels in a bitmap using colors from DIB
225 * PARAMS
226 * hdc [I] Handle to device context
227 * hbitmap [I] Handle to bitmap
228 * startscan [I] Starting scan line
229 * lines [I] Number of scan lines
230 * bits [I] Array of bitmap bits
231 * info [I] Address of structure with data
232 * coloruse [I] Type of color indexes to use
234 * RETURNS
235 * Success: Number of scan lines copied
236 * Failure: 0
238 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
239 UINT lines, LPCVOID bits, const BITMAPINFO *info,
240 UINT coloruse )
242 DC *dc;
243 BITMAPOBJ *bitmap;
244 INT result = 0;
246 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
248 if (!(dc = DC_GetDCUpdate( hdc )))
250 if (coloruse == DIB_RGB_COLORS) FIXME( "shouldn't require a DC for DIB_RGB_COLORS\n" );
251 GDI_ReleaseObj( hbitmap );
252 return 0;
255 if (!bitmap->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) goto done;
257 if (bitmap->funcs && bitmap->funcs->pSetDIBits)
258 result = bitmap->funcs->pSetDIBits( dc->physDev, hbitmap, startscan, lines,
259 bits, info, coloruse );
260 else
261 result = lines;
263 done:
264 GDI_ReleaseObj( hdc );
265 GDI_ReleaseObj( hbitmap );
266 return result;
270 /***********************************************************************
271 * SetDIBitsToDevice (GDI32.@)
273 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
274 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
275 UINT lines, LPCVOID bits, const BITMAPINFO *info,
276 UINT coloruse )
278 INT ret;
279 DC *dc;
281 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
283 if(dc->funcs->pSetDIBitsToDevice)
284 ret = dc->funcs->pSetDIBitsToDevice( dc->physDev, xDest, yDest, cx, cy, xSrc,
285 ySrc, startscan, lines, bits,
286 info, coloruse );
287 else {
288 FIXME("unimplemented on hdc %p\n", hdc);
289 ret = 0;
292 GDI_ReleaseObj( hdc );
293 return ret;
296 /***********************************************************************
297 * SetDIBColorTable (GDI32.@)
299 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
301 DC * dc;
302 UINT result = 0;
304 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
306 if (dc->funcs->pSetDIBColorTable)
307 result = dc->funcs->pSetDIBColorTable(dc->physDev, startpos, entries, colors);
309 GDI_ReleaseObj( hdc );
310 return result;
314 /***********************************************************************
315 * GetDIBColorTable (GDI32.@)
317 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
319 DC * dc;
320 UINT result = 0;
322 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
324 if (dc->funcs->pGetDIBColorTable)
325 result = dc->funcs->pGetDIBColorTable(dc->physDev, startpos, entries, colors);
327 GDI_ReleaseObj( hdc );
328 return result;
331 /* FIXME the following two structs should be combined with __sysPalTemplate in
332 objects/color.c - this should happen after de-X11-ing both of these
333 files.
334 NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
335 and blue - sigh */
337 static RGBQUAD EGAColors[16] = {
338 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
339 { 0x00, 0x00, 0x00, 0x00 },
340 { 0x00, 0x00, 0x80, 0x00 },
341 { 0x00, 0x80, 0x00, 0x00 },
342 { 0x00, 0x80, 0x80, 0x00 },
343 { 0x80, 0x00, 0x00, 0x00 },
344 { 0x80, 0x00, 0x80, 0x00 },
345 { 0x80, 0x80, 0x00, 0x00 },
346 { 0x80, 0x80, 0x80, 0x00 },
347 { 0xc0, 0xc0, 0xc0, 0x00 },
348 { 0x00, 0x00, 0xff, 0x00 },
349 { 0x00, 0xff, 0x00, 0x00 },
350 { 0x00, 0xff, 0xff, 0x00 },
351 { 0xff, 0x00, 0x00, 0x00 },
352 { 0xff, 0x00, 0xff, 0x00 },
353 { 0xff, 0xff, 0x00, 0x00 },
354 { 0xff, 0xff, 0xff, 0x00 }
358 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
359 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
360 { 0x00, 0x00, 0x00, 0x00 },
361 { 0x00, 0x00, 0x80, 0x00 },
362 { 0x00, 0x80, 0x00, 0x00 },
363 { 0x00, 0x80, 0x80, 0x00 },
364 { 0x80, 0x00, 0x00, 0x00 },
365 { 0x80, 0x00, 0x80, 0x00 },
366 { 0x80, 0x80, 0x00, 0x00 },
367 { 0xc0, 0xc0, 0xc0, 0x00 },
368 { 0xc0, 0xdc, 0xc0, 0x00 },
369 { 0xf0, 0xca, 0xa6, 0x00 },
370 { 0xf0, 0xfb, 0xff, 0x00 },
371 { 0xa4, 0xa0, 0xa0, 0x00 },
372 { 0x80, 0x80, 0x80, 0x00 },
373 { 0x00, 0x00, 0xf0, 0x00 },
374 { 0x00, 0xff, 0x00, 0x00 },
375 { 0x00, 0xff, 0xff, 0x00 },
376 { 0xff, 0x00, 0x00, 0x00 },
377 { 0xff, 0x00, 0xff, 0x00 },
378 { 0xff, 0xff, 0x00, 0x00 },
379 { 0xff, 0xff, 0xff, 0x00 }
383 /******************************************************************************
384 * GetDIBits [GDI32.@] Retrieves bits of bitmap and copies to buffer
386 * RETURNS
387 * Success: Number of scan lines copied from bitmap
388 * Failure: 0
390 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
392 INT WINAPI GetDIBits(
393 HDC hdc, /* [in] Handle to device context */
394 HBITMAP hbitmap, /* [in] Handle to bitmap */
395 UINT startscan, /* [in] First scan line to set in dest bitmap */
396 UINT lines, /* [in] Number of scan lines to copy */
397 LPVOID bits, /* [out] Address of array for bitmap bits */
398 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
399 UINT coloruse) /* [in] RGB or palette index */
401 DC * dc;
402 BITMAPOBJ * bmp;
403 PALETTEENTRY * palEntry;
404 PALETTEOBJ * palette;
405 int i;
407 if (!info) return 0;
408 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
409 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
411 GDI_ReleaseObj( hdc );
412 return 0;
414 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC )))
416 GDI_ReleaseObj( hdc );
417 GDI_ReleaseObj( hbitmap );
418 return 0;
421 /* Transfer color info */
423 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
425 info->bmiHeader.biClrUsed = 0;
427 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
428 palEntry = palette->logpalette.palPalEntry;
429 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
430 if (coloruse == DIB_RGB_COLORS) {
431 info->bmiColors[i].rgbRed = palEntry->peRed;
432 info->bmiColors[i].rgbGreen = palEntry->peGreen;
433 info->bmiColors[i].rgbBlue = palEntry->peBlue;
434 info->bmiColors[i].rgbReserved = 0;
436 else ((WORD *)info->bmiColors)[i] = (WORD)i;
438 } else {
439 switch (info->bmiHeader.biBitCount) {
440 case 1:
441 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
442 info->bmiColors[0].rgbBlue = 0;
443 info->bmiColors[0].rgbReserved = 0;
444 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
445 info->bmiColors[1].rgbBlue = 0xff;
446 info->bmiColors[1].rgbReserved = 0;
447 break;
449 case 4:
450 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
451 break;
453 case 8:
455 INT r, g, b;
456 RGBQUAD *color;
458 memcpy(info->bmiColors, DefLogPalette,
459 10 * sizeof(RGBQUAD));
460 memcpy(info->bmiColors + 246, DefLogPalette + 10,
461 10 * sizeof(RGBQUAD));
462 color = info->bmiColors + 10;
463 for(r = 0; r <= 5; r++) /* FIXME */
464 for(g = 0; g <= 5; g++)
465 for(b = 0; b <= 5; b++) {
466 color->rgbRed = (r * 0xff) / 5;
467 color->rgbGreen = (g * 0xff) / 5;
468 color->rgbBlue = (b * 0xff) / 5;
469 color->rgbReserved = 0;
470 color++;
477 GDI_ReleaseObj( dc->hPalette );
479 if (bits && lines)
481 /* If the bitmap object already have a dib section that contains image data, get the bits from it */
482 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
484 /*FIXME: Only RGB dibs supported for now */
485 unsigned int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
486 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
487 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
488 unsigned int x, y;
490 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
492 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
493 dstwidthb = -dstwidthb;
496 switch( info->bmiHeader.biBitCount ) {
498 case 15:
499 case 16: /* 16 bpp dstDIB */
501 LPWORD dstbits = (LPWORD)dbits;
502 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
504 /* FIXME: BI_BITFIELDS not supported yet */
506 switch(bmp->dib->dsBm.bmBitsPixel) {
508 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
510 /* FIXME: BI_BITFIELDS not supported yet */
511 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
512 memcpy(dbits, sbits, srcwidthb);
514 break;
516 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
518 LPBYTE srcbits = sbits;
520 for( y = 0; y < lines; y++) {
521 for( x = 0; x < srcwidth; x++, srcbits += 3)
522 *dstbits++ = ((srcbits[0] >> 3) & bmask) |
523 (((WORD)srcbits[1] << 2) & gmask) |
524 (((WORD)srcbits[2] << 7) & rmask);
526 dstbits = (LPWORD)(dbits+=dstwidthb);
527 srcbits = (sbits += srcwidthb);
530 break;
532 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
534 LPDWORD srcbits = (LPDWORD)sbits;
535 DWORD val;
537 for( y = 0; y < lines; y++) {
538 for( x = 0; x < srcwidth; x++ ) {
539 val = *srcbits++;
540 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
541 ((val >> 9) & rmask));
543 dstbits = (LPWORD)(dbits+=dstwidthb);
544 srcbits = (LPDWORD)(sbits+=srcwidthb);
547 break;
549 default: /* ? bit bmp -> 16 bit DIB */
550 FIXME("15/16 bit DIB %d bit bitmap\n",
551 bmp->bitmap.bmBitsPixel);
552 break;
555 break;
557 case 24: /* 24 bpp dstDIB */
559 LPBYTE dstbits = dbits;
561 switch(bmp->dib->dsBm.bmBitsPixel) {
563 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
565 LPWORD srcbits = (LPWORD)sbits;
566 WORD val;
568 /* FIXME: BI_BITFIELDS not supported yet */
569 for( y = 0; y < lines; y++) {
570 for( x = 0; x < srcwidth; x++ ) {
571 val = *srcbits++;
572 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
573 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
574 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
576 dstbits = (LPBYTE)(dbits+=dstwidthb);
577 srcbits = (LPWORD)(sbits+=srcwidthb);
580 break;
582 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
584 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
585 memcpy(dbits, sbits, srcwidthb);
587 break;
589 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
591 LPBYTE srcbits = (LPBYTE)sbits;
593 for( y = 0; y < lines; y++) {
594 for( x = 0; x < srcwidth; x++, srcbits++ ) {
595 *dstbits++ = *srcbits++;
596 *dstbits++ = *srcbits++;
597 *dstbits++ = *srcbits++;
599 dstbits=(LPBYTE)(dbits+=dstwidthb);
600 srcbits = (LPBYTE)(sbits+=srcwidthb);
603 break;
605 default: /* ? bit bmp -> 24 bit DIB */
606 FIXME("24 bit DIB %d bit bitmap\n",
607 bmp->bitmap.bmBitsPixel);
608 break;
611 break;
613 case 32: /* 32 bpp dstDIB */
615 LPDWORD dstbits = (LPDWORD)dbits;
617 /* FIXME: BI_BITFIELDS not supported yet */
619 switch(bmp->dib->dsBm.bmBitsPixel) {
620 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
622 LPWORD srcbits = (LPWORD)sbits;
623 DWORD val;
625 /* FIXME: BI_BITFIELDS not supported yet */
626 for( y = 0; y < lines; y++) {
627 for( x = 0; x < srcwidth; x++ ) {
628 val = (DWORD)*srcbits++;
629 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
630 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
631 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
633 dstbits=(LPDWORD)(dbits+=dstwidthb);
634 srcbits=(LPWORD)(sbits+=srcwidthb);
637 break;
639 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
641 LPBYTE srcbits = sbits;
643 for( y = 0; y < lines; y++) {
644 for( x = 0; x < srcwidth; x++, srcbits+=3 )
645 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
646 dstbits=(LPDWORD)(dbits+=dstwidthb);
647 srcbits=(sbits+=srcwidthb);
650 break;
652 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
654 /* FIXME: BI_BITFIELDS not supported yet */
655 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
656 memcpy(dbits, sbits, srcwidthb);
658 break;
660 default: /* ? bit bmp -> 16 bit DIB */
661 FIXME("15/16 bit DIB %d bit bitmap\n",
662 bmp->bitmap.bmBitsPixel);
663 break;
666 break;
668 default: /* ? bit DIB */
669 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
670 break;
673 /* Otherwise, get bits from the XImage */
674 else
676 if (!bmp->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) lines = 0;
677 else
679 if (bmp->funcs && bmp->funcs->pGetDIBits)
680 lines = bmp->funcs->pGetDIBits( dc->physDev, hbitmap, startscan,
681 lines, bits, info, coloruse );
682 else
683 lines = 0; /* FIXME: should copy from bmp->bitmap.bmBits */
687 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
689 /* fill in struct members */
691 if( info->bmiHeader.biBitCount == 0)
693 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
694 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
695 info->bmiHeader.biPlanes = 1;
696 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
697 info->bmiHeader.biSizeImage =
698 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
699 bmp->bitmap.bmHeight,
700 bmp->bitmap.bmBitsPixel );
701 info->bmiHeader.biCompression = 0;
703 else
705 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
706 info->bmiHeader.biWidth,
707 info->bmiHeader.biHeight,
708 info->bmiHeader.biBitCount );
712 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
713 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
714 info->bmiHeader.biHeight);
716 GDI_ReleaseObj( hdc );
717 GDI_ReleaseObj( hbitmap );
719 return lines;
723 /***********************************************************************
724 * CreateDIBitmap (GDI32.@)
726 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
727 DWORD init, LPCVOID bits, const BITMAPINFO *data,
728 UINT coloruse )
730 HBITMAP handle;
731 BOOL fColor;
732 DWORD width;
733 int height;
734 WORD bpp;
735 WORD compr;
736 DC *dc;
738 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
739 if (height < 0) height = -height;
741 /* Check if we should create a monochrome or color bitmap. */
742 /* We create a monochrome bitmap only if it has exactly 2 */
743 /* colors, which are black followed by white, nothing else. */
744 /* In all other cases, we create a color bitmap. */
746 if (bpp != 1) fColor = TRUE;
747 else if ((coloruse != DIB_RGB_COLORS) ||
748 (init != CBM_INIT) || !data) fColor = FALSE;
749 else
751 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
753 RGBQUAD *rgb = data->bmiColors;
754 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
756 /* Check if the first color of the colormap is black */
757 if ((col == RGB(0,0,0)))
759 rgb++;
760 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
761 /* If the second color is white, create a monochrome bitmap */
762 fColor = (col != RGB(0xff,0xff,0xff));
764 /* Note : If the first color of the colormap is white
765 followed by black, we have to create a color bitmap.
766 If we don't the white will be displayed in black later on!*/
767 else fColor = TRUE;
769 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
771 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
772 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
773 if ((col == RGB(0,0,0)))
775 rgb++;
776 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
777 fColor = (col != RGB(0xff,0xff,0xff));
779 else fColor = TRUE;
781 else if (data->bmiHeader.biSize == sizeof(BITMAPV4HEADER))
782 { /* FIXME: correct ? */
783 RGBQUAD *rgb = data->bmiColors;
784 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
786 /* Check if the first color of the colormap is black */
787 if ((col == RGB(0,0,0)))
789 rgb++;
790 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
791 /* If the second color is white, create a monochrome bitmap */
792 fColor = (col != RGB(0xff,0xff,0xff));
794 /* Note : If the first color of the colormap is white
795 followed by black, we have to create a color bitmap.
796 If we don't the white will be displayed in black later on!*/
797 else fColor = TRUE;
799 else
801 ERR("(%ld): wrong/unknown size for data\n",
802 data->bmiHeader.biSize );
803 return 0;
807 /* Now create the bitmap */
809 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
811 if (fColor)
812 handle = CreateBitmap( width, height, GetDeviceCaps( hdc, PLANES ),
813 GetDeviceCaps( hdc, BITSPIXEL ), NULL );
814 else handle = CreateBitmap( width, height, 1, 1, NULL );
816 if (handle)
818 if (init == CBM_INIT) SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
819 else if (!BITMAP_SetOwnerDC( handle, dc ))
821 DeleteObject( handle );
822 handle = 0;
826 GDI_ReleaseObj( hdc );
827 return handle;
830 /***********************************************************************
831 * CreateDIBSection (GDI.489)
833 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
834 SEGPTR *bits16, HANDLE section, DWORD offset)
836 LPVOID bits32;
837 HBITMAP hbitmap;
839 hbitmap = CreateDIBSection( HDC_32(hdc), bmi, usage, &bits32, section, offset );
840 if (hbitmap)
842 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(hbitmap, BITMAP_MAGIC);
843 if (bmp && bmp->dib && bits32)
845 BITMAPINFOHEADER *bi = &bmi->bmiHeader;
846 INT height = bi->biHeight >= 0 ? bi->biHeight : -bi->biHeight;
847 INT width_bytes = DIB_GetDIBWidthBytes(bi->biWidth, bi->biBitCount);
848 INT size = (bi->biSizeImage && bi->biCompression != BI_RGB) ?
849 bi->biSizeImage : width_bytes * height;
851 /* calculate number of sel's needed for size with 64K steps */
852 WORD count = (size + 0xffff) / 0x10000;
853 WORD sel = AllocSelectorArray16(count);
854 int i;
856 for (i = 0; i < count; i++)
858 SetSelectorBase(sel + (i << __AHSHIFT), (DWORD)bits32 + i * 0x10000);
859 SetSelectorLimit16(sel + (i << __AHSHIFT), size - 1); /* yep, limit is correct */
860 size -= 0x10000;
862 bmp->segptr_bits = MAKESEGPTR( sel, 0 );
863 if (bits16) *bits16 = bmp->segptr_bits;
865 if (bmp) GDI_ReleaseObj( hbitmap );
867 return HBITMAP_16(hbitmap);
870 /***********************************************************************
871 * DIB_CreateDIBSection
873 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
874 LPVOID *bits, HANDLE section,
875 DWORD offset, DWORD ovr_pitch)
877 HBITMAP hbitmap = 0;
878 DC *dc;
879 BOOL bDesktopDC = FALSE;
881 /* If the reference hdc is null, take the desktop dc */
882 if (hdc == 0)
884 hdc = CreateCompatibleDC(0);
885 bDesktopDC = TRUE;
888 if ((dc = DC_GetDCPtr( hdc )))
890 hbitmap = dc->funcs->pCreateDIBSection(dc->physDev, bmi, usage, bits, section, offset, ovr_pitch);
891 GDI_ReleaseObj(hdc);
894 if (bDesktopDC)
895 DeleteDC(hdc);
897 return hbitmap;
900 /***********************************************************************
901 * CreateDIBSection (GDI32.@)
903 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
904 LPVOID *bits, HANDLE section,
905 DWORD offset)
907 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
910 /***********************************************************************
911 * DIB_CreateDIBFromBitmap
912 * Allocates a packed DIB and copies the bitmap data into it.
914 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
916 BITMAPOBJ *pBmp = NULL;
917 HGLOBAL hPackedDIB = 0;
918 LPBYTE pPackedDIB = NULL;
919 LPBITMAPINFOHEADER pbmiHeader = NULL;
920 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
921 OffsetBits = 0, nLinesCopied = 0;
923 /* Get a pointer to the BITMAPOBJ structure */
924 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
925 if (!pBmp) return hPackedDIB;
927 /* Get the bitmap dimensions */
928 width = pBmp->bitmap.bmWidth;
929 height = pBmp->bitmap.bmHeight;
930 depth = pBmp->bitmap.bmBitsPixel;
933 * A packed DIB contains a BITMAPINFO structure followed immediately by
934 * an optional color palette and the pixel data.
937 /* Calculate the size of the packed DIB */
938 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
939 cPackedSize = sizeof(BITMAPINFOHEADER)
940 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
941 + cDataSize;
942 /* Get the offset to the bits */
943 OffsetBits = cPackedSize - cDataSize;
945 /* Allocate the packed DIB */
946 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
947 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
948 cPackedSize );
949 if ( !hPackedDIB )
951 WARN("Could not allocate packed DIB!\n");
952 goto END;
955 /* A packed DIB starts with a BITMAPINFOHEADER */
956 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
957 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
959 /* Init the BITMAPINFOHEADER */
960 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
961 pbmiHeader->biWidth = width;
962 pbmiHeader->biHeight = height;
963 pbmiHeader->biPlanes = 1;
964 pbmiHeader->biBitCount = depth;
965 pbmiHeader->biCompression = BI_RGB;
966 pbmiHeader->biSizeImage = 0;
967 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
968 pbmiHeader->biClrUsed = 0;
969 pbmiHeader->biClrImportant = 0;
971 /* Retrieve the DIB bits from the bitmap and fill in the
972 * DIB color table if present */
974 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
975 hBmp, /* Handle to bitmap */
976 0, /* First scan line to set in dest bitmap */
977 height, /* Number of scan lines to copy */
978 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
979 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
980 0); /* RGB or palette index */
981 GlobalUnlock(hPackedDIB);
983 /* Cleanup if GetDIBits failed */
984 if (nLinesCopied != height)
986 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
987 GlobalFree(hPackedDIB);
988 hPackedDIB = 0;
991 END:
992 GDI_ReleaseObj( hBmp );
993 return hPackedDIB;