Also #include <sys/uio.h>.
[wine/multimedia.git] / objects / dib.c
blob764fbf8d0499a85655b4acf2ccd01cf4b856ac84
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 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 (GDI.439)
152 INT16 WINAPI StretchDIBits16(HDC16 hdc, INT16 xDst, INT16 yDst, INT16 widthDst,
153 INT16 heightDst, INT16 xSrc, INT16 ySrc, INT16 widthSrc,
154 INT16 heightSrc, const VOID *bits,
155 const BITMAPINFO *info, UINT16 wUsage, DWORD dwRop )
157 return (INT16)StretchDIBits( hdc, xDst, yDst, widthDst, heightDst,
158 xSrc, ySrc, widthSrc, heightSrc, bits,
159 info, wUsage, dwRop );
163 /***********************************************************************
164 * StretchDIBits (GDI32.@)
166 INT WINAPI StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
167 INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
168 INT heightSrc, const void *bits,
169 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
171 DC *dc = DC_GetDCUpdate( hdc );
172 if(!dc) return FALSE;
174 if(dc->funcs->pStretchDIBits)
176 heightSrc = dc->funcs->pStretchDIBits(dc->physDev, xDst, yDst, widthDst,
177 heightDst, xSrc, ySrc, widthSrc,
178 heightSrc, bits, info, wUsage, dwRop);
179 GDI_ReleaseObj( hdc );
181 else /* use StretchBlt */
183 HBITMAP hBitmap, hOldBitmap;
184 HDC hdcMem;
186 GDI_ReleaseObj( hdc );
187 hdcMem = CreateCompatibleDC( hdc );
188 if (info->bmiHeader.biCompression == BI_RLE4 ||
189 info->bmiHeader.biCompression == BI_RLE8) {
191 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
192 * contain all the rectangle described in bmiHeader, but only part of it.
193 * This mean that those undescribed pixels must be left untouched.
194 * So, we first copy on a memory bitmap the current content of the
195 * destination rectangle, blit the DIB bits on top of it - hence leaving
196 * the gaps untouched -, and blitting the rectangle back.
197 * This insure that gaps are untouched on the destination rectangle
198 * Not doing so leads to trashed images (the gaps contain what was on the
199 * memory bitmap => generally black or garbage)
200 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
201 * another speed vs correctness issue. Anyway, if speed is needed, then the
202 * pStretchDIBits function shall be implemented.
203 * ericP (2000/09/09)
205 hBitmap = CreateCompatibleBitmap(hdc, info->bmiHeader.biWidth,
206 info->bmiHeader.biHeight);
207 hOldBitmap = SelectObject( hdcMem, hBitmap );
209 /* copy existing bitmap from destination dc */
210 StretchBlt( hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
211 widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
212 dwRop );
213 SetDIBits(hdcMem, hBitmap, 0, info->bmiHeader.biHeight, bits,
214 info, DIB_RGB_COLORS);
216 } else {
217 hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
218 bits, info, wUsage );
219 hOldBitmap = SelectObject( hdcMem, hBitmap );
222 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
223 left (negative biHeight) */
224 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
225 hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
226 widthSrc, heightSrc, dwRop );
227 SelectObject( hdcMem, hOldBitmap );
228 DeleteDC( hdcMem );
229 DeleteObject( hBitmap );
231 return heightSrc;
235 /***********************************************************************
236 * SetDIBits (GDI.440)
238 INT16 WINAPI SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
239 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
240 UINT16 coloruse )
242 return SetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
246 /******************************************************************************
247 * SetDIBits [GDI32.@] Sets pixels in a bitmap using colors from DIB
249 * PARAMS
250 * hdc [I] Handle to device context
251 * hbitmap [I] Handle to bitmap
252 * startscan [I] Starting scan line
253 * lines [I] Number of scan lines
254 * bits [I] Array of bitmap bits
255 * info [I] Address of structure with data
256 * coloruse [I] Type of color indexes to use
258 * RETURNS
259 * Success: Number of scan lines copied
260 * Failure: 0
262 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
263 UINT lines, LPCVOID bits, const BITMAPINFO *info,
264 UINT coloruse )
266 DC *dc;
267 INT result = 0;
269 /* Check parameters */
270 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
272 if (dc->funcs->pSetDIBits)
273 result = dc->funcs->pSetDIBits(dc->physDev, hbitmap, startscan, lines, bits, info, coloruse);
275 GDI_ReleaseObj( hdc );
276 return result;
280 /***********************************************************************
281 * SetDIBitsToDevice (GDI.443)
283 INT16 WINAPI SetDIBitsToDevice16(HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
284 INT16 cy, INT16 xSrc, INT16 ySrc, UINT16 startscan,
285 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
286 UINT16 coloruse )
288 return SetDIBitsToDevice( hdc, xDest, yDest, cx, cy, xSrc, ySrc,
289 startscan, lines, bits, info, coloruse );
293 /***********************************************************************
294 * SetDIBitsToDevice (GDI32.@)
296 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
297 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
298 UINT lines, LPCVOID bits, const BITMAPINFO *info,
299 UINT coloruse )
301 INT ret;
302 DC *dc;
304 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
306 if(dc->funcs->pSetDIBitsToDevice)
307 ret = dc->funcs->pSetDIBitsToDevice( dc->physDev, xDest, yDest, cx, cy, xSrc,
308 ySrc, startscan, lines, bits,
309 info, coloruse );
310 else {
311 FIXME("unimplemented on hdc %08x\n", hdc);
312 ret = 0;
315 GDI_ReleaseObj( hdc );
316 return ret;
319 /***********************************************************************
320 * SetDIBColorTable (GDI.602)
322 UINT16 WINAPI SetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
323 RGBQUAD *colors )
325 return SetDIBColorTable( hdc, startpos, entries, colors );
328 /***********************************************************************
329 * SetDIBColorTable (GDI32.@)
331 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
333 DC * dc;
334 UINT result = 0;
336 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
338 if (dc->funcs->pSetDIBColorTable)
339 result = dc->funcs->pSetDIBColorTable(dc->physDev, startpos, entries, colors);
341 GDI_ReleaseObj( hdc );
342 return result;
345 /***********************************************************************
346 * GetDIBColorTable (GDI.603)
348 UINT16 WINAPI GetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
349 RGBQUAD *colors )
351 return GetDIBColorTable( hdc, startpos, entries, colors );
354 /***********************************************************************
355 * GetDIBColorTable (GDI32.@)
357 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
359 DC * dc;
360 UINT result = 0;
362 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
364 if (dc->funcs->pGetDIBColorTable)
365 result = dc->funcs->pGetDIBColorTable(dc->physDev, startpos, entries, colors);
367 GDI_ReleaseObj( hdc );
368 return result;
371 /* FIXME the following two structs should be combined with __sysPalTemplate in
372 objects/color.c - this should happen after de-X11-ing both of these
373 files.
374 NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
375 and blue - sigh */
377 static RGBQUAD EGAColors[16] = {
378 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
379 { 0x00, 0x00, 0x00, 0x00 },
380 { 0x00, 0x00, 0x80, 0x00 },
381 { 0x00, 0x80, 0x00, 0x00 },
382 { 0x00, 0x80, 0x80, 0x00 },
383 { 0x80, 0x00, 0x00, 0x00 },
384 { 0x80, 0x00, 0x80, 0x00 },
385 { 0x80, 0x80, 0x00, 0x00 },
386 { 0x80, 0x80, 0x80, 0x00 },
387 { 0xc0, 0xc0, 0xc0, 0x00 },
388 { 0x00, 0x00, 0xff, 0x00 },
389 { 0x00, 0xff, 0x00, 0x00 },
390 { 0x00, 0xff, 0xff, 0x00 },
391 { 0xff, 0x00, 0x00, 0x00 },
392 { 0xff, 0x00, 0xff, 0x00 },
393 { 0xff, 0xff, 0x00, 0x00 },
394 { 0xff, 0xff, 0xff, 0x00 }
398 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
399 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
400 { 0x00, 0x00, 0x00, 0x00 },
401 { 0x00, 0x00, 0x80, 0x00 },
402 { 0x00, 0x80, 0x00, 0x00 },
403 { 0x00, 0x80, 0x80, 0x00 },
404 { 0x80, 0x00, 0x00, 0x00 },
405 { 0x80, 0x00, 0x80, 0x00 },
406 { 0x80, 0x80, 0x00, 0x00 },
407 { 0xc0, 0xc0, 0xc0, 0x00 },
408 { 0xc0, 0xdc, 0xc0, 0x00 },
409 { 0xf0, 0xca, 0xa6, 0x00 },
410 { 0xf0, 0xfb, 0xff, 0x00 },
411 { 0xa4, 0xa0, 0xa0, 0x00 },
412 { 0x80, 0x80, 0x80, 0x00 },
413 { 0x00, 0x00, 0xf0, 0x00 },
414 { 0x00, 0xff, 0x00, 0x00 },
415 { 0x00, 0xff, 0xff, 0x00 },
416 { 0xff, 0x00, 0x00, 0x00 },
417 { 0xff, 0x00, 0xff, 0x00 },
418 { 0xff, 0xff, 0x00, 0x00 },
419 { 0xff, 0xff, 0xff, 0x00 }
422 /***********************************************************************
423 * GetDIBits (GDI.441)
425 INT16 WINAPI GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
426 UINT16 lines, LPVOID bits, BITMAPINFO * info,
427 UINT16 coloruse )
429 return GetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
433 /******************************************************************************
434 * GetDIBits [GDI32.@] Retrieves bits of bitmap and copies to buffer
436 * RETURNS
437 * Success: Number of scan lines copied from bitmap
438 * Failure: 0
440 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
442 INT WINAPI GetDIBits(
443 HDC hdc, /* [in] Handle to device context */
444 HBITMAP hbitmap, /* [in] Handle to bitmap */
445 UINT startscan, /* [in] First scan line to set in dest bitmap */
446 UINT lines, /* [in] Number of scan lines to copy */
447 LPVOID bits, /* [out] Address of array for bitmap bits */
448 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
449 UINT coloruse) /* [in] RGB or palette index */
451 DC * dc;
452 BITMAPOBJ * bmp;
453 PALETTEENTRY * palEntry;
454 PALETTEOBJ * palette;
455 int i;
457 if (!info) return 0;
458 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
459 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
461 GDI_ReleaseObj( hdc );
462 return 0;
464 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC )))
466 GDI_ReleaseObj( hdc );
467 GDI_ReleaseObj( hbitmap );
468 return 0;
471 /* Transfer color info */
473 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
475 info->bmiHeader.biClrUsed = 0;
477 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
478 palEntry = palette->logpalette.palPalEntry;
479 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
480 if (coloruse == DIB_RGB_COLORS) {
481 info->bmiColors[i].rgbRed = palEntry->peRed;
482 info->bmiColors[i].rgbGreen = palEntry->peGreen;
483 info->bmiColors[i].rgbBlue = palEntry->peBlue;
484 info->bmiColors[i].rgbReserved = 0;
486 else ((WORD *)info->bmiColors)[i] = (WORD)i;
488 } else {
489 switch (info->bmiHeader.biBitCount) {
490 case 1:
491 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
492 info->bmiColors[0].rgbBlue = 0;
493 info->bmiColors[0].rgbReserved = 0;
494 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
495 info->bmiColors[1].rgbBlue = 0xff;
496 info->bmiColors[1].rgbReserved = 0;
497 break;
499 case 4:
500 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
501 break;
503 case 8:
505 INT r, g, b;
506 RGBQUAD *color;
508 memcpy(info->bmiColors, DefLogPalette,
509 10 * sizeof(RGBQUAD));
510 memcpy(info->bmiColors + 246, DefLogPalette + 10,
511 10 * sizeof(RGBQUAD));
512 color = info->bmiColors + 10;
513 for(r = 0; r <= 5; r++) /* FIXME */
514 for(g = 0; g <= 5; g++)
515 for(b = 0; b <= 5; b++) {
516 color->rgbRed = (r * 0xff) / 5;
517 color->rgbGreen = (g * 0xff) / 5;
518 color->rgbBlue = (b * 0xff) / 5;
519 color->rgbReserved = 0;
520 color++;
527 GDI_ReleaseObj( dc->hPalette );
529 if (bits && lines)
531 /* If the bitmap object already have a dib section that contains image data, get the bits from it */
532 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
534 /*FIXME: Only RGB dibs supported for now */
535 unsigned int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
536 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
537 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
538 unsigned int x, y;
540 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
542 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
543 dstwidthb = -dstwidthb;
546 switch( info->bmiHeader.biBitCount ) {
548 case 15:
549 case 16: /* 16 bpp dstDIB */
551 LPWORD dstbits = (LPWORD)dbits;
552 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
554 /* FIXME: BI_BITFIELDS not supported yet */
556 switch(bmp->dib->dsBm.bmBitsPixel) {
558 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
560 /* FIXME: BI_BITFIELDS not supported yet */
561 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
562 memcpy(dbits, sbits, srcwidthb);
564 break;
566 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
568 LPBYTE srcbits = sbits;
570 for( y = 0; y < lines; y++) {
571 for( x = 0; x < srcwidth; x++, srcbits += 3)
572 *dstbits++ = ((srcbits[0] >> 3) & bmask) |
573 (((WORD)srcbits[1] << 2) & gmask) |
574 (((WORD)srcbits[2] << 7) & rmask);
576 dstbits = (LPWORD)(dbits+=dstwidthb);
577 srcbits = (sbits += srcwidthb);
580 break;
582 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
584 LPDWORD srcbits = (LPDWORD)sbits;
585 DWORD val;
587 for( y = 0; y < lines; y++) {
588 for( x = 0; x < srcwidth; x++ ) {
589 val = *srcbits++;
590 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
591 ((val >> 9) & rmask));
593 dstbits = (LPWORD)(dbits+=dstwidthb);
594 srcbits = (LPDWORD)(sbits+=srcwidthb);
597 break;
599 default: /* ? bit bmp -> 16 bit DIB */
600 FIXME("15/16 bit DIB %d bit bitmap\n",
601 bmp->bitmap.bmBitsPixel);
602 break;
605 break;
607 case 24: /* 24 bpp dstDIB */
609 LPBYTE dstbits = dbits;
611 switch(bmp->dib->dsBm.bmBitsPixel) {
613 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
615 LPWORD srcbits = (LPWORD)sbits;
616 WORD val;
618 /* FIXME: BI_BITFIELDS not supported yet */
619 for( y = 0; y < lines; y++) {
620 for( x = 0; x < srcwidth; x++ ) {
621 val = *srcbits++;
622 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
623 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
624 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
626 dstbits = (LPBYTE)(dbits+=dstwidthb);
627 srcbits = (LPWORD)(sbits+=srcwidthb);
630 break;
632 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
634 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
635 memcpy(dbits, sbits, srcwidthb);
637 break;
639 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
641 LPBYTE srcbits = (LPBYTE)sbits;
643 for( y = 0; y < lines; y++) {
644 for( x = 0; x < srcwidth; x++, srcbits++ ) {
645 *dstbits++ = *srcbits++;
646 *dstbits++ = *srcbits++;
647 *dstbits++ = *srcbits++;
649 dstbits=(LPBYTE)(dbits+=dstwidthb);
650 srcbits = (LPBYTE)(sbits+=srcwidthb);
653 break;
655 default: /* ? bit bmp -> 24 bit DIB */
656 FIXME("24 bit DIB %d bit bitmap\n",
657 bmp->bitmap.bmBitsPixel);
658 break;
661 break;
663 case 32: /* 32 bpp dstDIB */
665 LPDWORD dstbits = (LPDWORD)dbits;
667 /* FIXME: BI_BITFIELDS not supported yet */
669 switch(bmp->dib->dsBm.bmBitsPixel) {
670 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
672 LPWORD srcbits = (LPWORD)sbits;
673 DWORD val;
675 /* FIXME: BI_BITFIELDS not supported yet */
676 for( y = 0; y < lines; y++) {
677 for( x = 0; x < srcwidth; x++ ) {
678 val = (DWORD)*srcbits++;
679 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
680 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
681 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
683 dstbits=(LPDWORD)(dbits+=dstwidthb);
684 srcbits=(LPWORD)(sbits+=srcwidthb);
687 break;
689 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
691 LPBYTE srcbits = sbits;
693 for( y = 0; y < lines; y++) {
694 for( x = 0; x < srcwidth; x++, srcbits+=3 )
695 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
696 dstbits=(LPDWORD)(dbits+=dstwidthb);
697 srcbits=(sbits+=srcwidthb);
700 break;
702 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
704 /* FIXME: BI_BITFIELDS not supported yet */
705 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
706 memcpy(dbits, sbits, srcwidthb);
708 break;
710 default: /* ? bit bmp -> 16 bit DIB */
711 FIXME("15/16 bit DIB %d bit bitmap\n",
712 bmp->bitmap.bmBitsPixel);
713 break;
716 break;
718 default: /* ? bit DIB */
719 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
720 break;
723 /* Otherwise, get bits from the XImage */
724 else if (!dc->funcs->pGetDIBits ||
725 !dc->funcs->pGetDIBits(dc->physDev, hbitmap, startscan, lines, bits, info, coloruse))
727 GDI_ReleaseObj( hdc );
728 GDI_ReleaseObj( hbitmap );
730 return 0;
733 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
735 /* fill in struct members */
737 if( info->bmiHeader.biBitCount == 0)
739 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
740 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
741 info->bmiHeader.biPlanes = 1;
742 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
743 info->bmiHeader.biSizeImage =
744 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
745 bmp->bitmap.bmHeight,
746 bmp->bitmap.bmBitsPixel );
747 info->bmiHeader.biCompression = 0;
749 else
751 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
752 info->bmiHeader.biWidth,
753 info->bmiHeader.biHeight,
754 info->bmiHeader.biBitCount );
758 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
759 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
760 info->bmiHeader.biHeight);
762 GDI_ReleaseObj( hdc );
763 GDI_ReleaseObj( hbitmap );
765 return lines;
769 /***********************************************************************
770 * CreateDIBitmap (GDI.442)
772 HBITMAP16 WINAPI CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
773 DWORD init, LPCVOID bits, const BITMAPINFO * data,
774 UINT16 coloruse )
776 return CreateDIBitmap( hdc, header, init, bits, data, coloruse );
780 /***********************************************************************
781 * CreateDIBitmap (GDI32.@)
783 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
784 DWORD init, LPCVOID bits, const BITMAPINFO *data,
785 UINT coloruse )
787 HBITMAP handle;
788 BOOL fColor;
789 DWORD width;
790 int height;
791 WORD bpp;
792 WORD compr;
794 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
795 if (height < 0) height = -height;
797 /* Check if we should create a monochrome or color bitmap. */
798 /* We create a monochrome bitmap only if it has exactly 2 */
799 /* colors, which are black followed by white, nothing else. */
800 /* In all other cases, we create a color bitmap. */
802 if (bpp != 1) fColor = TRUE;
803 else if ((coloruse != DIB_RGB_COLORS) ||
804 (init != CBM_INIT) || !data) fColor = FALSE;
805 else
807 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
809 RGBQUAD *rgb = data->bmiColors;
810 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
812 /* Check if the first color of the colormap is black */
813 if ((col == RGB(0,0,0)))
815 rgb++;
816 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
817 /* If the second color is white, create a monochrome bitmap */
818 fColor = (col != RGB(0xff,0xff,0xff));
820 /* Note : If the first color of the colormap is white
821 followed by black, we have to create a color bitmap.
822 If we don't the white will be displayed in black later on!*/
823 else fColor = TRUE;
825 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
827 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
828 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
829 if ((col == RGB(0,0,0)))
831 rgb++;
832 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
833 fColor = (col != RGB(0xff,0xff,0xff));
835 else fColor = TRUE;
837 else if (data->bmiHeader.biSize == sizeof(BITMAPV4HEADER))
838 { /* FIXME: correct ? */
839 RGBQUAD *rgb = data->bmiColors;
840 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
842 /* Check if the first color of the colormap is black */
843 if ((col == RGB(0,0,0)))
845 rgb++;
846 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
847 /* If the second color is white, create a monochrome bitmap */
848 fColor = (col != RGB(0xff,0xff,0xff));
850 /* Note : If the first color of the colormap is white
851 followed by black, we have to create a color bitmap.
852 If we don't the white will be displayed in black later on!*/
853 else fColor = TRUE;
855 else
857 ERR("(%ld): wrong/unknown size for data\n",
858 data->bmiHeader.biSize );
859 return 0;
863 /* Now create the bitmap */
865 if (fColor)
866 handle = CreateBitmap( width, height, GetDeviceCaps( hdc, PLANES ),
867 GetDeviceCaps( hdc, BITSPIXEL ), NULL );
868 else handle = CreateBitmap( width, height, 1, 1, NULL );
870 if (!handle) return 0;
872 if (init == CBM_INIT)
873 SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
874 return handle;
877 /***********************************************************************
878 * CreateDIBSection (GDI.489)
880 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
881 SEGPTR *bits16, HANDLE section, DWORD offset)
883 LPVOID bits32;
884 HBITMAP hbitmap;
886 hbitmap = CreateDIBSection( hdc, bmi, usage, &bits32, section, offset );
887 if (hbitmap)
889 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(hbitmap, BITMAP_MAGIC);
890 if (bmp && bmp->dib && bits32)
892 BITMAPINFOHEADER *bi = &bmi->bmiHeader;
893 INT height = bi->biHeight >= 0 ? bi->biHeight : -bi->biHeight;
894 INT width_bytes = DIB_GetDIBWidthBytes(bi->biWidth, bi->biBitCount);
895 INT size = (bi->biSizeImage && bi->biCompression != BI_RGB) ?
896 bi->biSizeImage : width_bytes * height;
898 WORD sel = SELECTOR_AllocBlock( bits32, size, WINE_LDT_FLAGS_DATA );
899 bmp->segptr_bits = MAKESEGPTR( sel, 0 );
900 if (bits16) *bits16 = bmp->segptr_bits;
902 if (bmp) GDI_ReleaseObj( hbitmap );
904 return hbitmap;
907 /***********************************************************************
908 * DIB_CreateDIBSection
910 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
911 LPVOID *bits, HANDLE section,
912 DWORD offset, DWORD ovr_pitch)
914 HBITMAP hbitmap = 0;
915 DC *dc;
916 BOOL bDesktopDC = FALSE;
918 /* If the reference hdc is null, take the desktop dc */
919 if (hdc == 0)
921 hdc = CreateCompatibleDC(0);
922 bDesktopDC = TRUE;
925 if ((dc = DC_GetDCPtr( hdc )))
927 hbitmap = dc->funcs->pCreateDIBSection(dc->physDev, bmi, usage, bits, section, offset, ovr_pitch);
928 GDI_ReleaseObj(hdc);
931 if (bDesktopDC)
932 DeleteDC(hdc);
934 return hbitmap;
937 /***********************************************************************
938 * CreateDIBSection (GDI32.@)
940 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
941 LPVOID *bits, HANDLE section,
942 DWORD offset)
944 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
947 /***********************************************************************
948 * DIB_CreateDIBFromBitmap
949 * Allocates a packed DIB and copies the bitmap data into it.
951 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
953 BITMAPOBJ *pBmp = NULL;
954 HGLOBAL hPackedDIB = 0;
955 LPBYTE pPackedDIB = NULL;
956 LPBITMAPINFOHEADER pbmiHeader = NULL;
957 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
958 OffsetBits = 0, nLinesCopied = 0;
960 /* Get a pointer to the BITMAPOBJ structure */
961 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
962 if (!pBmp) return hPackedDIB;
964 /* Get the bitmap dimensions */
965 width = pBmp->bitmap.bmWidth;
966 height = pBmp->bitmap.bmHeight;
967 depth = pBmp->bitmap.bmBitsPixel;
970 * A packed DIB contains a BITMAPINFO structure followed immediately by
971 * an optional color palette and the pixel data.
974 /* Calculate the size of the packed DIB */
975 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
976 cPackedSize = sizeof(BITMAPINFOHEADER)
977 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
978 + cDataSize;
979 /* Get the offset to the bits */
980 OffsetBits = cPackedSize - cDataSize;
982 /* Allocate the packed DIB */
983 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
984 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
985 cPackedSize );
986 if ( !hPackedDIB )
988 WARN("Could not allocate packed DIB!\n");
989 goto END;
992 /* A packed DIB starts with a BITMAPINFOHEADER */
993 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
994 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
996 /* Init the BITMAPINFOHEADER */
997 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
998 pbmiHeader->biWidth = width;
999 pbmiHeader->biHeight = height;
1000 pbmiHeader->biPlanes = 1;
1001 pbmiHeader->biBitCount = depth;
1002 pbmiHeader->biCompression = BI_RGB;
1003 pbmiHeader->biSizeImage = 0;
1004 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
1005 pbmiHeader->biClrUsed = 0;
1006 pbmiHeader->biClrImportant = 0;
1008 /* Retrieve the DIB bits from the bitmap and fill in the
1009 * DIB color table if present */
1011 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
1012 hBmp, /* Handle to bitmap */
1013 0, /* First scan line to set in dest bitmap */
1014 height, /* Number of scan lines to copy */
1015 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
1016 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
1017 0); /* RGB or palette index */
1018 GlobalUnlock(hPackedDIB);
1020 /* Cleanup if GetDIBits failed */
1021 if (nLinesCopied != height)
1023 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
1024 GlobalFree(hPackedDIB);
1025 hPackedDIB = 0;
1028 END:
1029 GDI_ReleaseObj( hBmp );
1030 return hPackedDIB;