X11DRV_DrawArc: Don't overwrite the ENDCAP style.
[wine/multimedia.git] / objects / dib.c
blob3119ac9f04e818d0cbd3ea864af8932129ea9f25
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;
160 if (!bits || !info)
161 return 0;
163 dc = DC_GetDCUpdate( hdc );
164 if(!dc) return FALSE;
166 if(dc->funcs->pStretchDIBits)
168 heightSrc = dc->funcs->pStretchDIBits(dc->physDev, xDst, yDst, widthDst,
169 heightDst, xSrc, ySrc, widthSrc,
170 heightSrc, bits, info, wUsage, dwRop);
171 GDI_ReleaseObj( hdc );
173 else /* use StretchBlt */
175 HBITMAP hBitmap, hOldBitmap;
176 HDC hdcMem;
178 GDI_ReleaseObj( hdc );
179 hdcMem = CreateCompatibleDC( hdc );
180 if (info->bmiHeader.biCompression == BI_RLE4 ||
181 info->bmiHeader.biCompression == BI_RLE8) {
183 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
184 * contain all the rectangle described in bmiHeader, but only part of it.
185 * This mean that those undescribed pixels must be left untouched.
186 * So, we first copy on a memory bitmap the current content of the
187 * destination rectangle, blit the DIB bits on top of it - hence leaving
188 * the gaps untouched -, and blitting the rectangle back.
189 * This insure that gaps are untouched on the destination rectangle
190 * Not doing so leads to trashed images (the gaps contain what was on the
191 * memory bitmap => generally black or garbage)
192 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
193 * another speed vs correctness issue. Anyway, if speed is needed, then the
194 * pStretchDIBits function shall be implemented.
195 * ericP (2000/09/09)
197 hBitmap = CreateCompatibleBitmap(hdc, info->bmiHeader.biWidth,
198 info->bmiHeader.biHeight);
199 hOldBitmap = SelectObject( hdcMem, hBitmap );
201 /* copy existing bitmap from destination dc */
202 StretchBlt( hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
203 widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
204 dwRop );
205 SetDIBits(hdcMem, hBitmap, 0, info->bmiHeader.biHeight, bits,
206 info, DIB_RGB_COLORS);
208 } else {
209 hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
210 bits, info, wUsage );
211 hOldBitmap = SelectObject( hdcMem, hBitmap );
214 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
215 left (negative biHeight) */
216 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
217 hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
218 widthSrc, heightSrc, dwRop );
219 SelectObject( hdcMem, hOldBitmap );
220 DeleteDC( hdcMem );
221 DeleteObject( hBitmap );
223 return heightSrc;
227 /******************************************************************************
228 * SetDIBits [GDI32.@] Sets pixels in a bitmap using colors from DIB
230 * PARAMS
231 * hdc [I] Handle to device context
232 * hbitmap [I] Handle to bitmap
233 * startscan [I] Starting scan line
234 * lines [I] Number of scan lines
235 * bits [I] Array of bitmap bits
236 * info [I] Address of structure with data
237 * coloruse [I] Type of color indexes to use
239 * RETURNS
240 * Success: Number of scan lines copied
241 * Failure: 0
243 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
244 UINT lines, LPCVOID bits, const BITMAPINFO *info,
245 UINT coloruse )
247 DC *dc;
248 BITMAPOBJ *bitmap;
249 INT result = 0;
251 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
253 if (!(dc = DC_GetDCUpdate( hdc )))
255 if (coloruse == DIB_RGB_COLORS) FIXME( "shouldn't require a DC for DIB_RGB_COLORS\n" );
256 GDI_ReleaseObj( hbitmap );
257 return 0;
260 if (!bitmap->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) goto done;
262 if (bitmap->funcs && bitmap->funcs->pSetDIBits)
263 result = bitmap->funcs->pSetDIBits( dc->physDev, hbitmap, startscan, lines,
264 bits, info, coloruse );
265 else
266 result = lines;
268 done:
269 GDI_ReleaseObj( hdc );
270 GDI_ReleaseObj( hbitmap );
271 return result;
275 /***********************************************************************
276 * SetDIBitsToDevice (GDI32.@)
278 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
279 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
280 UINT lines, LPCVOID bits, const BITMAPINFO *info,
281 UINT coloruse )
283 INT ret;
284 DC *dc;
286 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
288 if(dc->funcs->pSetDIBitsToDevice)
289 ret = dc->funcs->pSetDIBitsToDevice( dc->physDev, xDest, yDest, cx, cy, xSrc,
290 ySrc, startscan, lines, bits,
291 info, coloruse );
292 else {
293 FIXME("unimplemented on hdc %p\n", hdc);
294 ret = 0;
297 GDI_ReleaseObj( hdc );
298 return ret;
301 /***********************************************************************
302 * SetDIBColorTable (GDI32.@)
304 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
306 DC * dc;
307 UINT result = 0;
309 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
311 if (dc->funcs->pSetDIBColorTable)
312 result = dc->funcs->pSetDIBColorTable(dc->physDev, startpos, entries, colors);
314 GDI_ReleaseObj( hdc );
315 return result;
319 /***********************************************************************
320 * GetDIBColorTable (GDI32.@)
322 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
324 DC * dc;
325 UINT result = 0;
327 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
329 if (dc->funcs->pGetDIBColorTable)
330 result = dc->funcs->pGetDIBColorTable(dc->physDev, startpos, entries, colors);
332 GDI_ReleaseObj( hdc );
333 return result;
336 /* FIXME the following two structs should be combined with __sysPalTemplate in
337 objects/color.c - this should happen after de-X11-ing both of these
338 files.
339 NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
340 and blue - sigh */
342 static RGBQUAD EGAColors[16] = {
343 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
344 { 0x00, 0x00, 0x00, 0x00 },
345 { 0x00, 0x00, 0x80, 0x00 },
346 { 0x00, 0x80, 0x00, 0x00 },
347 { 0x00, 0x80, 0x80, 0x00 },
348 { 0x80, 0x00, 0x00, 0x00 },
349 { 0x80, 0x00, 0x80, 0x00 },
350 { 0x80, 0x80, 0x00, 0x00 },
351 { 0x80, 0x80, 0x80, 0x00 },
352 { 0xc0, 0xc0, 0xc0, 0x00 },
353 { 0x00, 0x00, 0xff, 0x00 },
354 { 0x00, 0xff, 0x00, 0x00 },
355 { 0x00, 0xff, 0xff, 0x00 },
356 { 0xff, 0x00, 0x00, 0x00 },
357 { 0xff, 0x00, 0xff, 0x00 },
358 { 0xff, 0xff, 0x00, 0x00 },
359 { 0xff, 0xff, 0xff, 0x00 }
363 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
364 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
365 { 0x00, 0x00, 0x00, 0x00 },
366 { 0x00, 0x00, 0x80, 0x00 },
367 { 0x00, 0x80, 0x00, 0x00 },
368 { 0x00, 0x80, 0x80, 0x00 },
369 { 0x80, 0x00, 0x00, 0x00 },
370 { 0x80, 0x00, 0x80, 0x00 },
371 { 0x80, 0x80, 0x00, 0x00 },
372 { 0xc0, 0xc0, 0xc0, 0x00 },
373 { 0xc0, 0xdc, 0xc0, 0x00 },
374 { 0xf0, 0xca, 0xa6, 0x00 },
375 { 0xf0, 0xfb, 0xff, 0x00 },
376 { 0xa4, 0xa0, 0xa0, 0x00 },
377 { 0x80, 0x80, 0x80, 0x00 },
378 { 0x00, 0x00, 0xf0, 0x00 },
379 { 0x00, 0xff, 0x00, 0x00 },
380 { 0x00, 0xff, 0xff, 0x00 },
381 { 0xff, 0x00, 0x00, 0x00 },
382 { 0xff, 0x00, 0xff, 0x00 },
383 { 0xff, 0xff, 0x00, 0x00 },
384 { 0xff, 0xff, 0xff, 0x00 }
388 /******************************************************************************
389 * GetDIBits [GDI32.@] Retrieves bits of bitmap and copies to buffer
391 * RETURNS
392 * Success: Number of scan lines copied from bitmap
393 * Failure: 0
395 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
397 INT WINAPI GetDIBits(
398 HDC hdc, /* [in] Handle to device context */
399 HBITMAP hbitmap, /* [in] Handle to bitmap */
400 UINT startscan, /* [in] First scan line to set in dest bitmap */
401 UINT lines, /* [in] Number of scan lines to copy */
402 LPVOID bits, /* [out] Address of array for bitmap bits */
403 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
404 UINT coloruse) /* [in] RGB or palette index */
406 DC * dc;
407 BITMAPOBJ * bmp;
408 PALETTEENTRY * palEntry;
409 PALETTEOBJ * palette;
410 int i;
412 if (!info) return 0;
413 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
414 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
416 GDI_ReleaseObj( hdc );
417 return 0;
419 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC )))
421 GDI_ReleaseObj( hdc );
422 GDI_ReleaseObj( hbitmap );
423 return 0;
426 /* Transfer color info */
428 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
430 info->bmiHeader.biClrUsed = 0;
432 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
433 palEntry = palette->logpalette.palPalEntry;
434 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
435 if (coloruse == DIB_RGB_COLORS) {
436 info->bmiColors[i].rgbRed = palEntry->peRed;
437 info->bmiColors[i].rgbGreen = palEntry->peGreen;
438 info->bmiColors[i].rgbBlue = palEntry->peBlue;
439 info->bmiColors[i].rgbReserved = 0;
441 else ((WORD *)info->bmiColors)[i] = (WORD)i;
443 } else {
444 switch (info->bmiHeader.biBitCount) {
445 case 1:
446 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
447 info->bmiColors[0].rgbBlue = 0;
448 info->bmiColors[0].rgbReserved = 0;
449 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
450 info->bmiColors[1].rgbBlue = 0xff;
451 info->bmiColors[1].rgbReserved = 0;
452 break;
454 case 4:
455 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
456 break;
458 case 8:
460 INT r, g, b;
461 RGBQUAD *color;
463 memcpy(info->bmiColors, DefLogPalette,
464 10 * sizeof(RGBQUAD));
465 memcpy(info->bmiColors + 246, DefLogPalette + 10,
466 10 * sizeof(RGBQUAD));
467 color = info->bmiColors + 10;
468 for(r = 0; r <= 5; r++) /* FIXME */
469 for(g = 0; g <= 5; g++)
470 for(b = 0; b <= 5; b++) {
471 color->rgbRed = (r * 0xff) / 5;
472 color->rgbGreen = (g * 0xff) / 5;
473 color->rgbBlue = (b * 0xff) / 5;
474 color->rgbReserved = 0;
475 color++;
482 GDI_ReleaseObj( dc->hPalette );
484 if (bits && lines)
486 /* If the bitmap object already have a dib section that contains image data, get the bits from it */
487 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
489 /*FIXME: Only RGB dibs supported for now */
490 unsigned int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
491 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
492 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
493 unsigned int x, y;
495 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
497 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
498 dstwidthb = -dstwidthb;
501 switch( info->bmiHeader.biBitCount ) {
503 case 15:
504 case 16: /* 16 bpp dstDIB */
506 LPWORD dstbits = (LPWORD)dbits;
507 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
509 /* FIXME: BI_BITFIELDS not supported yet */
511 switch(bmp->dib->dsBm.bmBitsPixel) {
513 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
515 /* FIXME: BI_BITFIELDS not supported yet */
516 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
517 memcpy(dbits, sbits, srcwidthb);
519 break;
521 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
523 LPBYTE srcbits = sbits;
525 for( y = 0; y < lines; y++) {
526 for( x = 0; x < srcwidth; x++, srcbits += 3)
527 *dstbits++ = ((srcbits[0] >> 3) & bmask) |
528 (((WORD)srcbits[1] << 2) & gmask) |
529 (((WORD)srcbits[2] << 7) & rmask);
531 dstbits = (LPWORD)(dbits+=dstwidthb);
532 srcbits = (sbits += srcwidthb);
535 break;
537 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
539 LPDWORD srcbits = (LPDWORD)sbits;
540 DWORD val;
542 for( y = 0; y < lines; y++) {
543 for( x = 0; x < srcwidth; x++ ) {
544 val = *srcbits++;
545 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
546 ((val >> 9) & rmask));
548 dstbits = (LPWORD)(dbits+=dstwidthb);
549 srcbits = (LPDWORD)(sbits+=srcwidthb);
552 break;
554 default: /* ? bit bmp -> 16 bit DIB */
555 FIXME("15/16 bit DIB %d bit bitmap\n",
556 bmp->bitmap.bmBitsPixel);
557 break;
560 break;
562 case 24: /* 24 bpp dstDIB */
564 LPBYTE dstbits = dbits;
566 switch(bmp->dib->dsBm.bmBitsPixel) {
568 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
570 LPWORD srcbits = (LPWORD)sbits;
571 WORD val;
573 /* FIXME: BI_BITFIELDS not supported yet */
574 for( y = 0; y < lines; y++) {
575 for( x = 0; x < srcwidth; x++ ) {
576 val = *srcbits++;
577 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
578 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
579 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
581 dstbits = (LPBYTE)(dbits+=dstwidthb);
582 srcbits = (LPWORD)(sbits+=srcwidthb);
585 break;
587 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
589 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
590 memcpy(dbits, sbits, srcwidthb);
592 break;
594 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
596 LPBYTE srcbits = (LPBYTE)sbits;
598 for( y = 0; y < lines; y++) {
599 for( x = 0; x < srcwidth; x++, srcbits++ ) {
600 *dstbits++ = *srcbits++;
601 *dstbits++ = *srcbits++;
602 *dstbits++ = *srcbits++;
604 dstbits=(LPBYTE)(dbits+=dstwidthb);
605 srcbits = (LPBYTE)(sbits+=srcwidthb);
608 break;
610 default: /* ? bit bmp -> 24 bit DIB */
611 FIXME("24 bit DIB %d bit bitmap\n",
612 bmp->bitmap.bmBitsPixel);
613 break;
616 break;
618 case 32: /* 32 bpp dstDIB */
620 LPDWORD dstbits = (LPDWORD)dbits;
622 /* FIXME: BI_BITFIELDS not supported yet */
624 switch(bmp->dib->dsBm.bmBitsPixel) {
625 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
627 LPWORD srcbits = (LPWORD)sbits;
628 DWORD val;
630 /* FIXME: BI_BITFIELDS not supported yet */
631 for( y = 0; y < lines; y++) {
632 for( x = 0; x < srcwidth; x++ ) {
633 val = (DWORD)*srcbits++;
634 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
635 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
636 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
638 dstbits=(LPDWORD)(dbits+=dstwidthb);
639 srcbits=(LPWORD)(sbits+=srcwidthb);
642 break;
644 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
646 LPBYTE srcbits = sbits;
648 for( y = 0; y < lines; y++) {
649 for( x = 0; x < srcwidth; x++, srcbits+=3 )
650 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
651 dstbits=(LPDWORD)(dbits+=dstwidthb);
652 srcbits=(sbits+=srcwidthb);
655 break;
657 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
659 /* FIXME: BI_BITFIELDS not supported yet */
660 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
661 memcpy(dbits, sbits, srcwidthb);
663 break;
665 default: /* ? bit bmp -> 16 bit DIB */
666 FIXME("15/16 bit DIB %d bit bitmap\n",
667 bmp->bitmap.bmBitsPixel);
668 break;
671 break;
673 default: /* ? bit DIB */
674 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
675 break;
678 /* Otherwise, get bits from the XImage */
679 else
681 if (!bmp->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) lines = 0;
682 else
684 if (bmp->funcs && bmp->funcs->pGetDIBits)
685 lines = bmp->funcs->pGetDIBits( dc->physDev, hbitmap, startscan,
686 lines, bits, info, coloruse );
687 else
688 lines = 0; /* FIXME: should copy from bmp->bitmap.bmBits */
692 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
694 /* fill in struct members */
696 if( info->bmiHeader.biBitCount == 0)
698 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
699 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
700 info->bmiHeader.biPlanes = 1;
701 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
702 info->bmiHeader.biSizeImage =
703 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
704 bmp->bitmap.bmHeight,
705 bmp->bitmap.bmBitsPixel );
706 info->bmiHeader.biCompression = 0;
708 else
710 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
711 info->bmiHeader.biWidth,
712 info->bmiHeader.biHeight,
713 info->bmiHeader.biBitCount );
717 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
718 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
719 info->bmiHeader.biHeight);
721 GDI_ReleaseObj( hdc );
722 GDI_ReleaseObj( hbitmap );
724 return lines;
728 /***********************************************************************
729 * CreateDIBitmap (GDI32.@)
731 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
732 DWORD init, LPCVOID bits, const BITMAPINFO *data,
733 UINT coloruse )
735 HBITMAP handle;
736 BOOL fColor;
737 DWORD width;
738 int height;
739 WORD bpp;
740 WORD compr;
741 DC *dc;
743 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
744 if (height < 0) height = -height;
746 /* Check if we should create a monochrome or color bitmap. */
747 /* We create a monochrome bitmap only if it has exactly 2 */
748 /* colors, which are black followed by white, nothing else. */
749 /* In all other cases, we create a color bitmap. */
751 if (bpp != 1) fColor = TRUE;
752 else if ((coloruse != DIB_RGB_COLORS) ||
753 (init != CBM_INIT) || !data) fColor = FALSE;
754 else
756 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
758 RGBQUAD *rgb = data->bmiColors;
759 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
761 /* Check if the first color of the colormap is black */
762 if ((col == RGB(0,0,0)))
764 rgb++;
765 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
766 /* If the second color is white, create a monochrome bitmap */
767 fColor = (col != RGB(0xff,0xff,0xff));
769 /* Note : If the first color of the colormap is white
770 followed by black, we have to create a color bitmap.
771 If we don't the white will be displayed in black later on!*/
772 else fColor = TRUE;
774 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
776 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
777 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
778 if ((col == RGB(0,0,0)))
780 rgb++;
781 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
782 fColor = (col != RGB(0xff,0xff,0xff));
784 else fColor = TRUE;
786 else if (data->bmiHeader.biSize == sizeof(BITMAPV4HEADER))
787 { /* FIXME: correct ? */
788 RGBQUAD *rgb = data->bmiColors;
789 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
791 /* Check if the first color of the colormap is black */
792 if ((col == RGB(0,0,0)))
794 rgb++;
795 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
796 /* If the second color is white, create a monochrome bitmap */
797 fColor = (col != RGB(0xff,0xff,0xff));
799 /* Note : If the first color of the colormap is white
800 followed by black, we have to create a color bitmap.
801 If we don't the white will be displayed in black later on!*/
802 else fColor = TRUE;
804 else
806 ERR("(%ld): wrong/unknown size for data\n",
807 data->bmiHeader.biSize );
808 return 0;
812 /* Now create the bitmap */
814 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
816 if (fColor)
817 handle = CreateBitmap( width, height, GetDeviceCaps( hdc, PLANES ),
818 GetDeviceCaps( hdc, BITSPIXEL ), NULL );
819 else handle = CreateBitmap( width, height, 1, 1, NULL );
821 if (handle)
823 if (init == CBM_INIT) SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
824 else if (!BITMAP_SetOwnerDC( handle, dc ))
826 DeleteObject( handle );
827 handle = 0;
831 GDI_ReleaseObj( hdc );
832 return handle;
835 /***********************************************************************
836 * CreateDIBSection (GDI.489)
838 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
839 SEGPTR *bits16, HANDLE section, DWORD offset)
841 LPVOID bits32;
842 HBITMAP hbitmap;
844 hbitmap = CreateDIBSection( HDC_32(hdc), bmi, usage, &bits32, section, offset );
845 if (hbitmap)
847 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(hbitmap, BITMAP_MAGIC);
848 if (bmp && bmp->dib && bits32)
850 BITMAPINFOHEADER *bi = &bmi->bmiHeader;
851 INT height = bi->biHeight >= 0 ? bi->biHeight : -bi->biHeight;
852 INT width_bytes = DIB_GetDIBWidthBytes(bi->biWidth, bi->biBitCount);
853 INT size = (bi->biSizeImage && bi->biCompression != BI_RGB) ?
854 bi->biSizeImage : width_bytes * height;
856 /* calculate number of sel's needed for size with 64K steps */
857 WORD count = (size + 0xffff) / 0x10000;
858 WORD sel = AllocSelectorArray16(count);
859 int i;
861 for (i = 0; i < count; i++)
863 SetSelectorBase(sel + (i << __AHSHIFT), (DWORD)bits32 + i * 0x10000);
864 SetSelectorLimit16(sel + (i << __AHSHIFT), size - 1); /* yep, limit is correct */
865 size -= 0x10000;
867 bmp->segptr_bits = MAKESEGPTR( sel, 0 );
868 if (bits16) *bits16 = bmp->segptr_bits;
870 if (bmp) GDI_ReleaseObj( hbitmap );
872 return HBITMAP_16(hbitmap);
875 /***********************************************************************
876 * DIB_CreateDIBSection
878 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
879 LPVOID *bits, HANDLE section,
880 DWORD offset, DWORD ovr_pitch)
882 HBITMAP hbitmap = 0;
883 DC *dc;
884 BOOL bDesktopDC = FALSE;
886 /* If the reference hdc is null, take the desktop dc */
887 if (hdc == 0)
889 hdc = CreateCompatibleDC(0);
890 bDesktopDC = TRUE;
893 if ((dc = DC_GetDCPtr( hdc )))
895 hbitmap = dc->funcs->pCreateDIBSection(dc->physDev, bmi, usage, bits, section, offset, ovr_pitch);
896 GDI_ReleaseObj(hdc);
899 if (bDesktopDC)
900 DeleteDC(hdc);
902 return hbitmap;
905 /***********************************************************************
906 * CreateDIBSection (GDI32.@)
908 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
909 LPVOID *bits, HANDLE section,
910 DWORD offset)
912 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
915 /***********************************************************************
916 * DIB_CreateDIBFromBitmap
917 * Allocates a packed DIB and copies the bitmap data into it.
919 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
921 BITMAPOBJ *pBmp = NULL;
922 HGLOBAL hPackedDIB = 0;
923 LPBYTE pPackedDIB = NULL;
924 LPBITMAPINFOHEADER pbmiHeader = NULL;
925 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
926 OffsetBits = 0, nLinesCopied = 0;
928 /* Get a pointer to the BITMAPOBJ structure */
929 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
930 if (!pBmp) return hPackedDIB;
932 /* Get the bitmap dimensions */
933 width = pBmp->bitmap.bmWidth;
934 height = pBmp->bitmap.bmHeight;
935 depth = pBmp->bitmap.bmBitsPixel;
938 * A packed DIB contains a BITMAPINFO structure followed immediately by
939 * an optional color palette and the pixel data.
942 /* Calculate the size of the packed DIB */
943 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
944 cPackedSize = sizeof(BITMAPINFOHEADER)
945 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
946 + cDataSize;
947 /* Get the offset to the bits */
948 OffsetBits = cPackedSize - cDataSize;
950 /* Allocate the packed DIB */
951 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
952 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
953 cPackedSize );
954 if ( !hPackedDIB )
956 WARN("Could not allocate packed DIB!\n");
957 goto END;
960 /* A packed DIB starts with a BITMAPINFOHEADER */
961 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
962 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
964 /* Init the BITMAPINFOHEADER */
965 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
966 pbmiHeader->biWidth = width;
967 pbmiHeader->biHeight = height;
968 pbmiHeader->biPlanes = 1;
969 pbmiHeader->biBitCount = depth;
970 pbmiHeader->biCompression = BI_RGB;
971 pbmiHeader->biSizeImage = 0;
972 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
973 pbmiHeader->biClrUsed = 0;
974 pbmiHeader->biClrImportant = 0;
976 /* Retrieve the DIB bits from the bitmap and fill in the
977 * DIB color table if present */
979 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
980 hBmp, /* Handle to bitmap */
981 0, /* First scan line to set in dest bitmap */
982 height, /* Number of scan lines to copy */
983 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
984 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
985 0); /* RGB or palette index */
986 GlobalUnlock(hPackedDIB);
988 /* Cleanup if GetDIBits failed */
989 if (nLinesCopied != height)
991 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
992 GlobalFree(hPackedDIB);
993 hPackedDIB = 0;
996 END:
997 GDI_ReleaseObj( hBmp );
998 return hPackedDIB;