Add and use the header files mssip.h and sipbase.h.
[wine/multimedia.git] / objects / dib.c
blobdb32287ac3226300f75bebe73f1471a1c83d0bdb
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 "wine/debug.h"
29 #include "palette.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
33 /***********************************************************************
34 * DIB_GetDIBWidthBytes
36 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
37 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
39 int DIB_GetDIBWidthBytes( int width, int depth )
41 int words;
43 switch(depth)
45 case 1: words = (width + 31) / 32; break;
46 case 4: words = (width + 7) / 8; break;
47 case 8: words = (width + 3) / 4; break;
48 case 15:
49 case 16: words = (width + 1) / 2; break;
50 case 24: words = (width * 3 + 3)/4; break;
52 default:
53 WARN("(%d): Unsupported depth\n", depth );
54 /* fall through */
55 case 32:
56 words = width;
58 return 4 * words;
61 /***********************************************************************
62 * DIB_GetDIBImageBytes
64 * Return the number of bytes used to hold the image in a DIB bitmap.
66 int DIB_GetDIBImageBytes( int width, int height, int depth )
68 return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
72 /***********************************************************************
73 * DIB_BitmapInfoSize
75 * Return the size of the bitmap info structure including color table.
77 int DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
79 int colors;
81 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
83 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
84 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
85 return sizeof(BITMAPCOREHEADER) + colors *
86 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
88 else /* assume BITMAPINFOHEADER */
90 colors = info->bmiHeader.biClrUsed;
91 if (!colors && (info->bmiHeader.biBitCount <= 8))
92 colors = 1 << info->bmiHeader.biBitCount;
93 return sizeof(BITMAPINFOHEADER) + colors *
94 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
99 /***********************************************************************
100 * DIB_GetBitmapInfo
102 * Get the info from a bitmap header.
103 * Return 1 for INFOHEADER, 0 for COREHEADER,
104 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
106 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
107 int *height, WORD *bpp, WORD *compr )
109 if (header->biSize == sizeof(BITMAPINFOHEADER))
111 *width = header->biWidth;
112 *height = header->biHeight;
113 *bpp = header->biBitCount;
114 *compr = header->biCompression;
115 return 1;
117 if (header->biSize == sizeof(BITMAPCOREHEADER))
119 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
120 *width = core->bcWidth;
121 *height = core->bcHeight;
122 *bpp = core->bcBitCount;
123 *compr = 0;
124 return 0;
126 if (header->biSize == sizeof(BITMAPV4HEADER))
128 BITMAPV4HEADER *v4hdr = (BITMAPV4HEADER *)header;
129 *width = v4hdr->bV4Width;
130 *height = v4hdr->bV4Height;
131 *bpp = v4hdr->bV4BitCount;
132 *compr = v4hdr->bV4Compression;
133 return 4;
135 if (header->biSize == sizeof(BITMAPV5HEADER))
137 BITMAPV5HEADER *v5hdr = (BITMAPV5HEADER *)header;
138 *width = v5hdr->bV5Width;
139 *height = v5hdr->bV5Height;
140 *bpp = v5hdr->bV5BitCount;
141 *compr = v5hdr->bV5Compression;
142 return 5;
144 ERR("(%ld): unknown/wrong size for header\n", header->biSize );
145 return -1;
149 /***********************************************************************
150 * StretchDIBits (GDI32.@)
152 INT WINAPI StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
153 INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
154 INT heightSrc, const void *bits,
155 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
157 DC *dc = DC_GetDCUpdate( hdc );
158 if(!dc) return FALSE;
160 if(dc->funcs->pStretchDIBits)
162 heightSrc = dc->funcs->pStretchDIBits(dc->physDev, xDst, yDst, widthDst,
163 heightDst, xSrc, ySrc, widthSrc,
164 heightSrc, bits, info, wUsage, dwRop);
165 GDI_ReleaseObj( hdc );
167 else /* use StretchBlt */
169 HBITMAP hBitmap, hOldBitmap;
170 HDC hdcMem;
172 GDI_ReleaseObj( hdc );
173 hdcMem = CreateCompatibleDC( hdc );
174 if (info->bmiHeader.biCompression == BI_RLE4 ||
175 info->bmiHeader.biCompression == BI_RLE8) {
177 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
178 * contain all the rectangle described in bmiHeader, but only part of it.
179 * This mean that those undescribed pixels must be left untouched.
180 * So, we first copy on a memory bitmap the current content of the
181 * destination rectangle, blit the DIB bits on top of it - hence leaving
182 * the gaps untouched -, and blitting the rectangle back.
183 * This insure that gaps are untouched on the destination rectangle
184 * Not doing so leads to trashed images (the gaps contain what was on the
185 * memory bitmap => generally black or garbage)
186 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
187 * another speed vs correctness issue. Anyway, if speed is needed, then the
188 * pStretchDIBits function shall be implemented.
189 * ericP (2000/09/09)
191 hBitmap = CreateCompatibleBitmap(hdc, info->bmiHeader.biWidth,
192 info->bmiHeader.biHeight);
193 hOldBitmap = SelectObject( hdcMem, hBitmap );
195 /* copy existing bitmap from destination dc */
196 StretchBlt( hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
197 widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
198 dwRop );
199 SetDIBits(hdcMem, hBitmap, 0, info->bmiHeader.biHeight, bits,
200 info, DIB_RGB_COLORS);
202 } else {
203 hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
204 bits, info, wUsage );
205 hOldBitmap = SelectObject( hdcMem, hBitmap );
208 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
209 left (negative biHeight) */
210 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
211 hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
212 widthSrc, heightSrc, dwRop );
213 SelectObject( hdcMem, hOldBitmap );
214 DeleteDC( hdcMem );
215 DeleteObject( hBitmap );
217 return heightSrc;
221 /******************************************************************************
222 * SetDIBits [GDI32.@] Sets pixels in a bitmap using colors from DIB
224 * PARAMS
225 * hdc [I] Handle to device context
226 * hbitmap [I] Handle to bitmap
227 * startscan [I] Starting scan line
228 * lines [I] Number of scan lines
229 * bits [I] Array of bitmap bits
230 * info [I] Address of structure with data
231 * coloruse [I] Type of color indexes to use
233 * RETURNS
234 * Success: Number of scan lines copied
235 * Failure: 0
237 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
238 UINT lines, LPCVOID bits, const BITMAPINFO *info,
239 UINT coloruse )
241 DC *dc;
242 BITMAPOBJ *bitmap;
243 INT result = 0;
245 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
247 if (!(dc = DC_GetDCUpdate( hdc )))
249 if (coloruse == DIB_RGB_COLORS) FIXME( "shouldn't require a DC for DIB_RGB_COLORS\n" );
250 GDI_ReleaseObj( hbitmap );
251 return 0;
254 if (!bitmap->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) goto done;
256 if (bitmap->funcs && bitmap->funcs->pSetDIBits)
257 result = bitmap->funcs->pSetDIBits( dc->physDev, hbitmap, startscan, lines,
258 bits, info, coloruse );
259 else
260 result = lines;
262 done:
263 GDI_ReleaseObj( hdc );
264 GDI_ReleaseObj( hbitmap );
265 return result;
269 /***********************************************************************
270 * SetDIBitsToDevice (GDI32.@)
272 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
273 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
274 UINT lines, LPCVOID bits, const BITMAPINFO *info,
275 UINT coloruse )
277 INT ret;
278 DC *dc;
280 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
282 if(dc->funcs->pSetDIBitsToDevice)
283 ret = dc->funcs->pSetDIBitsToDevice( dc->physDev, xDest, yDest, cx, cy, xSrc,
284 ySrc, startscan, lines, bits,
285 info, coloruse );
286 else {
287 FIXME("unimplemented on hdc %08x\n", hdc);
288 ret = 0;
291 GDI_ReleaseObj( hdc );
292 return ret;
295 /***********************************************************************
296 * SetDIBColorTable (GDI32.@)
298 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
300 DC * dc;
301 UINT result = 0;
303 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
305 if (dc->funcs->pSetDIBColorTable)
306 result = dc->funcs->pSetDIBColorTable(dc->physDev, startpos, entries, colors);
308 GDI_ReleaseObj( hdc );
309 return result;
313 /***********************************************************************
314 * GetDIBColorTable (GDI32.@)
316 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
318 DC * dc;
319 UINT result = 0;
321 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
323 if (dc->funcs->pGetDIBColorTable)
324 result = dc->funcs->pGetDIBColorTable(dc->physDev, startpos, entries, colors);
326 GDI_ReleaseObj( hdc );
327 return result;
330 /* FIXME the following two structs should be combined with __sysPalTemplate in
331 objects/color.c - this should happen after de-X11-ing both of these
332 files.
333 NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
334 and blue - sigh */
336 static RGBQUAD EGAColors[16] = {
337 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
338 { 0x00, 0x00, 0x00, 0x00 },
339 { 0x00, 0x00, 0x80, 0x00 },
340 { 0x00, 0x80, 0x00, 0x00 },
341 { 0x00, 0x80, 0x80, 0x00 },
342 { 0x80, 0x00, 0x00, 0x00 },
343 { 0x80, 0x00, 0x80, 0x00 },
344 { 0x80, 0x80, 0x00, 0x00 },
345 { 0x80, 0x80, 0x80, 0x00 },
346 { 0xc0, 0xc0, 0xc0, 0x00 },
347 { 0x00, 0x00, 0xff, 0x00 },
348 { 0x00, 0xff, 0x00, 0x00 },
349 { 0x00, 0xff, 0xff, 0x00 },
350 { 0xff, 0x00, 0x00, 0x00 },
351 { 0xff, 0x00, 0xff, 0x00 },
352 { 0xff, 0xff, 0x00, 0x00 },
353 { 0xff, 0xff, 0xff, 0x00 }
357 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
358 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
359 { 0x00, 0x00, 0x00, 0x00 },
360 { 0x00, 0x00, 0x80, 0x00 },
361 { 0x00, 0x80, 0x00, 0x00 },
362 { 0x00, 0x80, 0x80, 0x00 },
363 { 0x80, 0x00, 0x00, 0x00 },
364 { 0x80, 0x00, 0x80, 0x00 },
365 { 0x80, 0x80, 0x00, 0x00 },
366 { 0xc0, 0xc0, 0xc0, 0x00 },
367 { 0xc0, 0xdc, 0xc0, 0x00 },
368 { 0xf0, 0xca, 0xa6, 0x00 },
369 { 0xf0, 0xfb, 0xff, 0x00 },
370 { 0xa4, 0xa0, 0xa0, 0x00 },
371 { 0x80, 0x80, 0x80, 0x00 },
372 { 0x00, 0x00, 0xf0, 0x00 },
373 { 0x00, 0xff, 0x00, 0x00 },
374 { 0x00, 0xff, 0xff, 0x00 },
375 { 0xff, 0x00, 0x00, 0x00 },
376 { 0xff, 0x00, 0xff, 0x00 },
377 { 0xff, 0xff, 0x00, 0x00 },
378 { 0xff, 0xff, 0xff, 0x00 }
382 /******************************************************************************
383 * GetDIBits [GDI32.@] Retrieves bits of bitmap and copies to buffer
385 * RETURNS
386 * Success: Number of scan lines copied from bitmap
387 * Failure: 0
389 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
391 INT WINAPI GetDIBits(
392 HDC hdc, /* [in] Handle to device context */
393 HBITMAP hbitmap, /* [in] Handle to bitmap */
394 UINT startscan, /* [in] First scan line to set in dest bitmap */
395 UINT lines, /* [in] Number of scan lines to copy */
396 LPVOID bits, /* [out] Address of array for bitmap bits */
397 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
398 UINT coloruse) /* [in] RGB or palette index */
400 DC * dc;
401 BITMAPOBJ * bmp;
402 PALETTEENTRY * palEntry;
403 PALETTEOBJ * palette;
404 int i;
406 if (!info) return 0;
407 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
408 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
410 GDI_ReleaseObj( hdc );
411 return 0;
413 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC )))
415 GDI_ReleaseObj( hdc );
416 GDI_ReleaseObj( hbitmap );
417 return 0;
420 /* Transfer color info */
422 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
424 info->bmiHeader.biClrUsed = 0;
426 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
427 palEntry = palette->logpalette.palPalEntry;
428 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
429 if (coloruse == DIB_RGB_COLORS) {
430 info->bmiColors[i].rgbRed = palEntry->peRed;
431 info->bmiColors[i].rgbGreen = palEntry->peGreen;
432 info->bmiColors[i].rgbBlue = palEntry->peBlue;
433 info->bmiColors[i].rgbReserved = 0;
435 else ((WORD *)info->bmiColors)[i] = (WORD)i;
437 } else {
438 switch (info->bmiHeader.biBitCount) {
439 case 1:
440 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
441 info->bmiColors[0].rgbBlue = 0;
442 info->bmiColors[0].rgbReserved = 0;
443 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
444 info->bmiColors[1].rgbBlue = 0xff;
445 info->bmiColors[1].rgbReserved = 0;
446 break;
448 case 4:
449 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
450 break;
452 case 8:
454 INT r, g, b;
455 RGBQUAD *color;
457 memcpy(info->bmiColors, DefLogPalette,
458 10 * sizeof(RGBQUAD));
459 memcpy(info->bmiColors + 246, DefLogPalette + 10,
460 10 * sizeof(RGBQUAD));
461 color = info->bmiColors + 10;
462 for(r = 0; r <= 5; r++) /* FIXME */
463 for(g = 0; g <= 5; g++)
464 for(b = 0; b <= 5; b++) {
465 color->rgbRed = (r * 0xff) / 5;
466 color->rgbGreen = (g * 0xff) / 5;
467 color->rgbBlue = (b * 0xff) / 5;
468 color->rgbReserved = 0;
469 color++;
476 GDI_ReleaseObj( dc->hPalette );
478 if (bits && lines)
480 /* If the bitmap object already have a dib section that contains image data, get the bits from it */
481 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
483 /*FIXME: Only RGB dibs supported for now */
484 unsigned int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
485 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
486 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
487 unsigned int x, y;
489 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
491 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
492 dstwidthb = -dstwidthb;
495 switch( info->bmiHeader.biBitCount ) {
497 case 15:
498 case 16: /* 16 bpp dstDIB */
500 LPWORD dstbits = (LPWORD)dbits;
501 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
503 /* FIXME: BI_BITFIELDS not supported yet */
505 switch(bmp->dib->dsBm.bmBitsPixel) {
507 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
509 /* FIXME: BI_BITFIELDS not supported yet */
510 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
511 memcpy(dbits, sbits, srcwidthb);
513 break;
515 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
517 LPBYTE srcbits = sbits;
519 for( y = 0; y < lines; y++) {
520 for( x = 0; x < srcwidth; x++, srcbits += 3)
521 *dstbits++ = ((srcbits[0] >> 3) & bmask) |
522 (((WORD)srcbits[1] << 2) & gmask) |
523 (((WORD)srcbits[2] << 7) & rmask);
525 dstbits = (LPWORD)(dbits+=dstwidthb);
526 srcbits = (sbits += srcwidthb);
529 break;
531 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
533 LPDWORD srcbits = (LPDWORD)sbits;
534 DWORD val;
536 for( y = 0; y < lines; y++) {
537 for( x = 0; x < srcwidth; x++ ) {
538 val = *srcbits++;
539 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
540 ((val >> 9) & rmask));
542 dstbits = (LPWORD)(dbits+=dstwidthb);
543 srcbits = (LPDWORD)(sbits+=srcwidthb);
546 break;
548 default: /* ? bit bmp -> 16 bit DIB */
549 FIXME("15/16 bit DIB %d bit bitmap\n",
550 bmp->bitmap.bmBitsPixel);
551 break;
554 break;
556 case 24: /* 24 bpp dstDIB */
558 LPBYTE dstbits = dbits;
560 switch(bmp->dib->dsBm.bmBitsPixel) {
562 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
564 LPWORD srcbits = (LPWORD)sbits;
565 WORD val;
567 /* FIXME: BI_BITFIELDS not supported yet */
568 for( y = 0; y < lines; y++) {
569 for( x = 0; x < srcwidth; x++ ) {
570 val = *srcbits++;
571 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
572 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
573 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
575 dstbits = (LPBYTE)(dbits+=dstwidthb);
576 srcbits = (LPWORD)(sbits+=srcwidthb);
579 break;
581 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
583 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
584 memcpy(dbits, sbits, srcwidthb);
586 break;
588 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
590 LPBYTE srcbits = (LPBYTE)sbits;
592 for( y = 0; y < lines; y++) {
593 for( x = 0; x < srcwidth; x++, srcbits++ ) {
594 *dstbits++ = *srcbits++;
595 *dstbits++ = *srcbits++;
596 *dstbits++ = *srcbits++;
598 dstbits=(LPBYTE)(dbits+=dstwidthb);
599 srcbits = (LPBYTE)(sbits+=srcwidthb);
602 break;
604 default: /* ? bit bmp -> 24 bit DIB */
605 FIXME("24 bit DIB %d bit bitmap\n",
606 bmp->bitmap.bmBitsPixel);
607 break;
610 break;
612 case 32: /* 32 bpp dstDIB */
614 LPDWORD dstbits = (LPDWORD)dbits;
616 /* FIXME: BI_BITFIELDS not supported yet */
618 switch(bmp->dib->dsBm.bmBitsPixel) {
619 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
621 LPWORD srcbits = (LPWORD)sbits;
622 DWORD val;
624 /* FIXME: BI_BITFIELDS not supported yet */
625 for( y = 0; y < lines; y++) {
626 for( x = 0; x < srcwidth; x++ ) {
627 val = (DWORD)*srcbits++;
628 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
629 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
630 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
632 dstbits=(LPDWORD)(dbits+=dstwidthb);
633 srcbits=(LPWORD)(sbits+=srcwidthb);
636 break;
638 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
640 LPBYTE srcbits = sbits;
642 for( y = 0; y < lines; y++) {
643 for( x = 0; x < srcwidth; x++, srcbits+=3 )
644 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
645 dstbits=(LPDWORD)(dbits+=dstwidthb);
646 srcbits=(sbits+=srcwidthb);
649 break;
651 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
653 /* FIXME: BI_BITFIELDS not supported yet */
654 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
655 memcpy(dbits, sbits, srcwidthb);
657 break;
659 default: /* ? bit bmp -> 16 bit DIB */
660 FIXME("15/16 bit DIB %d bit bitmap\n",
661 bmp->bitmap.bmBitsPixel);
662 break;
665 break;
667 default: /* ? bit DIB */
668 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
669 break;
672 /* Otherwise, get bits from the XImage */
673 else
675 if (!bmp->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) lines = 0;
676 else
678 if (bmp->funcs && bmp->funcs->pGetDIBits)
679 lines = bmp->funcs->pGetDIBits( dc->physDev, hbitmap, startscan,
680 lines, bits, info, coloruse );
681 else
682 lines = 0; /* FIXME: should copy from bmp->bitmap.bmBits */
686 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
688 /* fill in struct members */
690 if( info->bmiHeader.biBitCount == 0)
692 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
693 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
694 info->bmiHeader.biPlanes = 1;
695 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
696 info->bmiHeader.biSizeImage =
697 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
698 bmp->bitmap.bmHeight,
699 bmp->bitmap.bmBitsPixel );
700 info->bmiHeader.biCompression = 0;
702 else
704 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
705 info->bmiHeader.biWidth,
706 info->bmiHeader.biHeight,
707 info->bmiHeader.biBitCount );
711 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
712 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
713 info->bmiHeader.biHeight);
715 GDI_ReleaseObj( hdc );
716 GDI_ReleaseObj( hbitmap );
718 return lines;
722 /***********************************************************************
723 * CreateDIBitmap (GDI32.@)
725 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
726 DWORD init, LPCVOID bits, const BITMAPINFO *data,
727 UINT coloruse )
729 HBITMAP handle;
730 BOOL fColor;
731 DWORD width;
732 int height;
733 WORD bpp;
734 WORD compr;
735 DC *dc;
737 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
738 if (height < 0) height = -height;
740 /* Check if we should create a monochrome or color bitmap. */
741 /* We create a monochrome bitmap only if it has exactly 2 */
742 /* colors, which are black followed by white, nothing else. */
743 /* In all other cases, we create a color bitmap. */
745 if (bpp != 1) fColor = TRUE;
746 else if ((coloruse != DIB_RGB_COLORS) ||
747 (init != CBM_INIT) || !data) fColor = FALSE;
748 else
750 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
752 RGBQUAD *rgb = data->bmiColors;
753 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
755 /* Check if the first color of the colormap is black */
756 if ((col == RGB(0,0,0)))
758 rgb++;
759 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
760 /* If the second color is white, create a monochrome bitmap */
761 fColor = (col != RGB(0xff,0xff,0xff));
763 /* Note : If the first color of the colormap is white
764 followed by black, we have to create a color bitmap.
765 If we don't the white will be displayed in black later on!*/
766 else fColor = TRUE;
768 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
770 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
771 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
772 if ((col == RGB(0,0,0)))
774 rgb++;
775 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
776 fColor = (col != RGB(0xff,0xff,0xff));
778 else fColor = TRUE;
780 else if (data->bmiHeader.biSize == sizeof(BITMAPV4HEADER))
781 { /* FIXME: correct ? */
782 RGBQUAD *rgb = data->bmiColors;
783 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
785 /* Check if the first color of the colormap is black */
786 if ((col == RGB(0,0,0)))
788 rgb++;
789 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
790 /* If the second color is white, create a monochrome bitmap */
791 fColor = (col != RGB(0xff,0xff,0xff));
793 /* Note : If the first color of the colormap is white
794 followed by black, we have to create a color bitmap.
795 If we don't the white will be displayed in black later on!*/
796 else fColor = TRUE;
798 else
800 ERR("(%ld): wrong/unknown size for data\n",
801 data->bmiHeader.biSize );
802 return 0;
806 /* Now create the bitmap */
808 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
810 if (fColor)
811 handle = CreateBitmap( width, height, GetDeviceCaps( hdc, PLANES ),
812 GetDeviceCaps( hdc, BITSPIXEL ), NULL );
813 else handle = CreateBitmap( width, height, 1, 1, NULL );
815 if (handle)
817 if (init == CBM_INIT) SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
818 else if (!BITMAP_SetOwnerDC( handle, dc ))
820 DeleteObject( handle );
821 handle = 0;
825 GDI_ReleaseObj( hdc );
826 return handle;
829 /***********************************************************************
830 * CreateDIBSection (GDI.489)
832 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
833 SEGPTR *bits16, HANDLE section, DWORD offset)
835 LPVOID bits32;
836 HBITMAP hbitmap;
838 hbitmap = CreateDIBSection( hdc, bmi, usage, &bits32, section, offset );
839 if (hbitmap)
841 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(hbitmap, BITMAP_MAGIC);
842 if (bmp && bmp->dib && bits32)
844 BITMAPINFOHEADER *bi = &bmi->bmiHeader;
845 INT height = bi->biHeight >= 0 ? bi->biHeight : -bi->biHeight;
846 INT width_bytes = DIB_GetDIBWidthBytes(bi->biWidth, bi->biBitCount);
847 INT size = (bi->biSizeImage && bi->biCompression != BI_RGB) ?
848 bi->biSizeImage : width_bytes * height;
850 /* calculate number of sel's needed for size with 64K steps */
851 WORD count = (size + 0xffff) / 0x10000;
852 WORD sel = AllocSelectorArray16(count);
853 int i;
855 for (i = 0; i < count; i++)
857 SetSelectorBase(sel + (i << __AHSHIFT), (DWORD)bits32 + i * 0x10000);
858 SetSelectorLimit16(sel + (i << __AHSHIFT), size - 1); /* yep, limit is correct */
859 size -= 0x10000;
861 bmp->segptr_bits = MAKESEGPTR( sel, 0 );
862 if (bits16) *bits16 = bmp->segptr_bits;
864 if (bmp) GDI_ReleaseObj( hbitmap );
866 return hbitmap;
869 /***********************************************************************
870 * DIB_CreateDIBSection
872 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
873 LPVOID *bits, HANDLE section,
874 DWORD offset, DWORD ovr_pitch)
876 HBITMAP hbitmap = 0;
877 DC *dc;
878 BOOL bDesktopDC = FALSE;
880 /* If the reference hdc is null, take the desktop dc */
881 if (hdc == 0)
883 hdc = CreateCompatibleDC(0);
884 bDesktopDC = TRUE;
887 if ((dc = DC_GetDCPtr( hdc )))
889 hbitmap = dc->funcs->pCreateDIBSection(dc->physDev, bmi, usage, bits, section, offset, ovr_pitch);
890 GDI_ReleaseObj(hdc);
893 if (bDesktopDC)
894 DeleteDC(hdc);
896 return hbitmap;
899 /***********************************************************************
900 * CreateDIBSection (GDI32.@)
902 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
903 LPVOID *bits, HANDLE section,
904 DWORD offset)
906 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
909 /***********************************************************************
910 * DIB_CreateDIBFromBitmap
911 * Allocates a packed DIB and copies the bitmap data into it.
913 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
915 BITMAPOBJ *pBmp = NULL;
916 HGLOBAL hPackedDIB = 0;
917 LPBYTE pPackedDIB = NULL;
918 LPBITMAPINFOHEADER pbmiHeader = NULL;
919 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
920 OffsetBits = 0, nLinesCopied = 0;
922 /* Get a pointer to the BITMAPOBJ structure */
923 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
924 if (!pBmp) return hPackedDIB;
926 /* Get the bitmap dimensions */
927 width = pBmp->bitmap.bmWidth;
928 height = pBmp->bitmap.bmHeight;
929 depth = pBmp->bitmap.bmBitsPixel;
932 * A packed DIB contains a BITMAPINFO structure followed immediately by
933 * an optional color palette and the pixel data.
936 /* Calculate the size of the packed DIB */
937 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
938 cPackedSize = sizeof(BITMAPINFOHEADER)
939 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
940 + cDataSize;
941 /* Get the offset to the bits */
942 OffsetBits = cPackedSize - cDataSize;
944 /* Allocate the packed DIB */
945 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
946 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
947 cPackedSize );
948 if ( !hPackedDIB )
950 WARN("Could not allocate packed DIB!\n");
951 goto END;
954 /* A packed DIB starts with a BITMAPINFOHEADER */
955 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
956 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
958 /* Init the BITMAPINFOHEADER */
959 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
960 pbmiHeader->biWidth = width;
961 pbmiHeader->biHeight = height;
962 pbmiHeader->biPlanes = 1;
963 pbmiHeader->biBitCount = depth;
964 pbmiHeader->biCompression = BI_RGB;
965 pbmiHeader->biSizeImage = 0;
966 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
967 pbmiHeader->biClrUsed = 0;
968 pbmiHeader->biClrImportant = 0;
970 /* Retrieve the DIB bits from the bitmap and fill in the
971 * DIB color table if present */
973 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
974 hBmp, /* Handle to bitmap */
975 0, /* First scan line to set in dest bitmap */
976 height, /* Number of scan lines to copy */
977 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
978 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
979 0); /* RGB or palette index */
980 GlobalUnlock(hPackedDIB);
982 /* Cleanup if GetDIBits failed */
983 if (nLinesCopied != height)
985 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
986 GlobalFree(hPackedDIB);
987 hPackedDIB = 0;
990 END:
991 GDI_ReleaseObj( hBmp );
992 return hPackedDIB;