Removed useless #ifdef WINELIB.
[wine/gsoc_dplay.git] / objects / dib.c
blob8c647136a4d38e6fcfa6fa28dcb27d601436a89a
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 <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "bitmap.h"
28 #include "gdi.h"
29 #include "wownt32.h"
30 #include "wine/debug.h"
31 #include "palette.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
35 /***********************************************************************
36 * DIB_GetDIBWidthBytes
38 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
39 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
41 int DIB_GetDIBWidthBytes( int width, int depth )
43 int words;
45 switch(depth)
47 case 1: words = (width + 31) / 32; break;
48 case 4: words = (width + 7) / 8; break;
49 case 8: words = (width + 3) / 4; break;
50 case 15:
51 case 16: words = (width + 1) / 2; break;
52 case 24: words = (width * 3 + 3)/4; break;
54 default:
55 WARN("(%d): Unsupported depth\n", depth );
56 /* fall through */
57 case 32:
58 words = width;
60 return 4 * words;
63 /***********************************************************************
64 * DIB_GetDIBImageBytes
66 * Return the number of bytes used to hold the image in a DIB bitmap.
68 int DIB_GetDIBImageBytes( int width, int height, int depth )
70 return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
74 /***********************************************************************
75 * DIB_BitmapInfoSize
77 * Return the size of the bitmap info structure including color table.
79 int DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
81 int colors;
83 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
85 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
86 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
87 return sizeof(BITMAPCOREHEADER) + colors *
88 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
90 else /* assume BITMAPINFOHEADER */
92 colors = info->bmiHeader.biClrUsed;
93 if (!colors && (info->bmiHeader.biBitCount <= 8))
94 colors = 1 << info->bmiHeader.biBitCount;
95 return sizeof(BITMAPINFOHEADER) + colors *
96 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
101 /***********************************************************************
102 * DIB_GetBitmapInfo
104 * Get the info from a bitmap header.
105 * Return 1 for INFOHEADER, 0 for COREHEADER,
106 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
108 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
109 int *height, WORD *bpp, WORD *compr )
111 if (header->biSize == sizeof(BITMAPINFOHEADER))
113 *width = header->biWidth;
114 *height = header->biHeight;
115 *bpp = header->biBitCount;
116 *compr = header->biCompression;
117 return 1;
119 if (header->biSize == sizeof(BITMAPCOREHEADER))
121 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
122 *width = core->bcWidth;
123 *height = core->bcHeight;
124 *bpp = core->bcBitCount;
125 *compr = 0;
126 return 0;
128 if (header->biSize == sizeof(BITMAPV4HEADER))
130 BITMAPV4HEADER *v4hdr = (BITMAPV4HEADER *)header;
131 *width = v4hdr->bV4Width;
132 *height = v4hdr->bV4Height;
133 *bpp = v4hdr->bV4BitCount;
134 *compr = v4hdr->bV4V4Compression;
135 return 4;
137 if (header->biSize == sizeof(BITMAPV5HEADER))
139 BITMAPV5HEADER *v5hdr = (BITMAPV5HEADER *)header;
140 *width = v5hdr->bV5Width;
141 *height = v5hdr->bV5Height;
142 *bpp = v5hdr->bV5BitCount;
143 *compr = v5hdr->bV5Compression;
144 return 5;
146 ERR("(%ld): unknown/wrong size for header\n", header->biSize );
147 return -1;
151 /***********************************************************************
152 * StretchDIBits (GDI32.@)
154 INT WINAPI StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
155 INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
156 INT heightSrc, const void *bits,
157 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
159 DC *dc;
161 if (!bits || !info)
162 return 0;
164 dc = DC_GetDCUpdate( hdc );
165 if(!dc) return FALSE;
167 if(dc->funcs->pStretchDIBits)
169 heightSrc = dc->funcs->pStretchDIBits(dc->physDev, xDst, yDst, widthDst,
170 heightDst, xSrc, ySrc, widthSrc,
171 heightSrc, bits, info, wUsage, dwRop);
172 GDI_ReleaseObj( hdc );
174 else /* use StretchBlt */
176 HBITMAP hBitmap, hOldBitmap;
177 HDC hdcMem;
179 GDI_ReleaseObj( hdc );
180 hdcMem = CreateCompatibleDC( hdc );
181 hBitmap = CreateCompatibleBitmap(hdc, info->bmiHeader.biWidth,
182 info->bmiHeader.biHeight);
183 hOldBitmap = SelectObject( hdcMem, hBitmap );
185 if (info->bmiHeader.biCompression == BI_RLE4 ||
186 info->bmiHeader.biCompression == BI_RLE8) {
188 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
189 * contain all the rectangle described in bmiHeader, but only part of it.
190 * This mean that those undescribed pixels must be left untouched.
191 * So, we first copy on a memory bitmap the current content of the
192 * destination rectangle, blit the DIB bits on top of it - hence leaving
193 * the gaps untouched -, and blitting the rectangle back.
194 * This insure that gaps are untouched on the destination rectangle
195 * Not doing so leads to trashed images (the gaps contain what was on the
196 * memory bitmap => generally black or garbage)
197 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
198 * another speed vs correctness issue. Anyway, if speed is needed, then the
199 * pStretchDIBits function shall be implemented.
200 * ericP (2000/09/09)
203 /* copy existing bitmap from destination dc */
204 StretchBlt( hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
205 widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
206 dwRop );
209 SetDIBits(hdcMem, hBitmap, 0, info->bmiHeader.biHeight, bits,
210 info, wUsage);
212 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
213 left (negative biHeight) */
214 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
215 hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
216 widthSrc, heightSrc, dwRop );
217 SelectObject( hdcMem, hOldBitmap );
218 DeleteDC( hdcMem );
219 DeleteObject( hBitmap );
221 return heightSrc;
225 /******************************************************************************
226 * SetDIBits [GDI32.@] Sets pixels in a bitmap using colors from DIB
228 * PARAMS
229 * hdc [I] Handle to device context
230 * hbitmap [I] Handle to bitmap
231 * startscan [I] Starting scan line
232 * lines [I] Number of scan lines
233 * bits [I] Array of bitmap bits
234 * info [I] Address of structure with data
235 * coloruse [I] Type of color indexes to use
237 * RETURNS
238 * Success: Number of scan lines copied
239 * Failure: 0
241 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
242 UINT lines, LPCVOID bits, const BITMAPINFO *info,
243 UINT coloruse )
245 DC *dc;
246 BITMAPOBJ *bitmap;
247 INT result = 0;
249 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
251 if (!(dc = DC_GetDCUpdate( hdc )))
253 if (coloruse == DIB_RGB_COLORS) FIXME( "shouldn't require a DC for DIB_RGB_COLORS\n" );
254 GDI_ReleaseObj( hbitmap );
255 return 0;
258 if (!bitmap->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) goto done;
260 if (bitmap->funcs && bitmap->funcs->pSetDIBits)
261 result = bitmap->funcs->pSetDIBits( dc->physDev, hbitmap, startscan, lines,
262 bits, info, coloruse );
263 else
264 result = lines;
266 done:
267 GDI_ReleaseObj( hdc );
268 GDI_ReleaseObj( hbitmap );
269 return result;
273 /***********************************************************************
274 * SetDIBitsToDevice (GDI32.@)
276 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
277 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
278 UINT lines, LPCVOID bits, const BITMAPINFO *info,
279 UINT coloruse )
281 INT ret;
282 DC *dc;
284 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
286 if(dc->funcs->pSetDIBitsToDevice)
287 ret = dc->funcs->pSetDIBitsToDevice( dc->physDev, xDest, yDest, cx, cy, xSrc,
288 ySrc, startscan, lines, bits,
289 info, coloruse );
290 else {
291 FIXME("unimplemented on hdc %p\n", hdc);
292 ret = 0;
295 GDI_ReleaseObj( hdc );
296 return ret;
299 /***********************************************************************
300 * SetDIBColorTable (GDI32.@)
302 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
304 DC * dc;
305 UINT result = 0;
307 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
309 if (dc->funcs->pSetDIBColorTable)
310 result = dc->funcs->pSetDIBColorTable(dc->physDev, startpos, entries, colors);
312 GDI_ReleaseObj( hdc );
313 return result;
317 /***********************************************************************
318 * GetDIBColorTable (GDI32.@)
320 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
322 DC * dc;
323 UINT result = 0;
325 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
327 if (dc->funcs->pGetDIBColorTable)
328 result = dc->funcs->pGetDIBColorTable(dc->physDev, startpos, entries, colors);
330 GDI_ReleaseObj( hdc );
331 return result;
334 /* FIXME the following two structs should be combined with __sysPalTemplate in
335 objects/color.c - this should happen after de-X11-ing both of these
336 files.
337 NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
338 and blue - sigh */
340 static RGBQUAD EGAColors[16] = {
341 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
342 { 0x00, 0x00, 0x00, 0x00 },
343 { 0x00, 0x00, 0x80, 0x00 },
344 { 0x00, 0x80, 0x00, 0x00 },
345 { 0x00, 0x80, 0x80, 0x00 },
346 { 0x80, 0x00, 0x00, 0x00 },
347 { 0x80, 0x00, 0x80, 0x00 },
348 { 0x80, 0x80, 0x00, 0x00 },
349 { 0x80, 0x80, 0x80, 0x00 },
350 { 0xc0, 0xc0, 0xc0, 0x00 },
351 { 0x00, 0x00, 0xff, 0x00 },
352 { 0x00, 0xff, 0x00, 0x00 },
353 { 0x00, 0xff, 0xff, 0x00 },
354 { 0xff, 0x00, 0x00, 0x00 },
355 { 0xff, 0x00, 0xff, 0x00 },
356 { 0xff, 0xff, 0x00, 0x00 },
357 { 0xff, 0xff, 0xff, 0x00 }
361 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
362 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
363 { 0x00, 0x00, 0x00, 0x00 },
364 { 0x00, 0x00, 0x80, 0x00 },
365 { 0x00, 0x80, 0x00, 0x00 },
366 { 0x00, 0x80, 0x80, 0x00 },
367 { 0x80, 0x00, 0x00, 0x00 },
368 { 0x80, 0x00, 0x80, 0x00 },
369 { 0x80, 0x80, 0x00, 0x00 },
370 { 0xc0, 0xc0, 0xc0, 0x00 },
371 { 0xc0, 0xdc, 0xc0, 0x00 },
372 { 0xf0, 0xca, 0xa6, 0x00 },
373 { 0xf0, 0xfb, 0xff, 0x00 },
374 { 0xa4, 0xa0, 0xa0, 0x00 },
375 { 0x80, 0x80, 0x80, 0x00 },
376 { 0x00, 0x00, 0xf0, 0x00 },
377 { 0x00, 0xff, 0x00, 0x00 },
378 { 0x00, 0xff, 0xff, 0x00 },
379 { 0xff, 0x00, 0x00, 0x00 },
380 { 0xff, 0x00, 0xff, 0x00 },
381 { 0xff, 0xff, 0x00, 0x00 },
382 { 0xff, 0xff, 0xff, 0x00 }
386 /******************************************************************************
387 * GetDIBits [GDI32.@] Retrieves bits of bitmap and copies to buffer
389 * RETURNS
390 * Success: Number of scan lines copied from bitmap
391 * Failure: 0
393 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
395 INT WINAPI GetDIBits(
396 HDC hdc, /* [in] Handle to device context */
397 HBITMAP hbitmap, /* [in] Handle to bitmap */
398 UINT startscan, /* [in] First scan line to set in dest bitmap */
399 UINT lines, /* [in] Number of scan lines to copy */
400 LPVOID bits, /* [out] Address of array for bitmap bits */
401 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
402 UINT coloruse) /* [in] RGB or palette index */
404 DC * dc;
405 BITMAPOBJ * bmp;
406 int i;
408 if (!info) return 0;
409 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
410 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
412 GDI_ReleaseObj( hdc );
413 return 0;
416 /* Transfer color info */
418 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
420 info->bmiHeader.biClrUsed = 0;
422 /* If the bitmap object already has a dib section at the
423 same color depth then get the color map from it */
424 if (bmp->dib && bmp->dib->dsBm.bmBitsPixel == info->bmiHeader.biBitCount) {
425 GetDIBColorTable(hdc, 0, 1 << info->bmiHeader.biBitCount, info->bmiColors);
427 else {
428 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
429 /* Generate the color map from the selected palette */
430 PALETTEENTRY * palEntry;
431 PALETTEOBJ * palette;
432 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC ))) {
433 GDI_ReleaseObj( hdc );
434 GDI_ReleaseObj( hbitmap );
435 return 0;
437 palEntry = palette->logpalette.palPalEntry;
438 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
439 if (coloruse == DIB_RGB_COLORS) {
440 info->bmiColors[i].rgbRed = palEntry->peRed;
441 info->bmiColors[i].rgbGreen = palEntry->peGreen;
442 info->bmiColors[i].rgbBlue = palEntry->peBlue;
443 info->bmiColors[i].rgbReserved = 0;
445 else ((WORD *)info->bmiColors)[i] = (WORD)i;
447 GDI_ReleaseObj( dc->hPalette );
448 } else {
449 switch (info->bmiHeader.biBitCount) {
450 case 1:
451 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
452 info->bmiColors[0].rgbBlue = 0;
453 info->bmiColors[0].rgbReserved = 0;
454 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
455 info->bmiColors[1].rgbBlue = 0xff;
456 info->bmiColors[1].rgbReserved = 0;
457 break;
459 case 4:
460 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
461 break;
463 case 8:
465 INT r, g, b;
466 RGBQUAD *color;
468 memcpy(info->bmiColors, DefLogPalette,
469 10 * sizeof(RGBQUAD));
470 memcpy(info->bmiColors + 246, DefLogPalette + 10,
471 10 * sizeof(RGBQUAD));
472 color = info->bmiColors + 10;
473 for(r = 0; r <= 5; r++) /* FIXME */
474 for(g = 0; g <= 5; g++)
475 for(b = 0; b <= 5; b++) {
476 color->rgbRed = (r * 0xff) / 5;
477 color->rgbGreen = (g * 0xff) / 5;
478 color->rgbBlue = (b * 0xff) / 5;
479 color->rgbReserved = 0;
480 color++;
488 if (bits && lines)
490 /* If the bitmap object already have a dib section that contains image data, get the bits from it */
491 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
493 /*FIXME: Only RGB dibs supported for now */
494 unsigned int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
495 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
496 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
497 unsigned int x, y;
499 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
501 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
502 dstwidthb = -dstwidthb;
505 switch( info->bmiHeader.biBitCount ) {
507 case 15:
508 case 16: /* 16 bpp dstDIB */
510 LPWORD dstbits = (LPWORD)dbits;
511 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
513 /* FIXME: BI_BITFIELDS not supported yet */
515 switch(bmp->dib->dsBm.bmBitsPixel) {
517 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
519 /* FIXME: BI_BITFIELDS not supported yet */
520 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
521 memcpy(dbits, sbits, srcwidthb);
523 break;
525 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
527 LPBYTE srcbits = sbits;
529 for( y = 0; y < lines; y++) {
530 for( x = 0; x < srcwidth; x++, srcbits += 3)
531 *dstbits++ = ((srcbits[0] >> 3) & bmask) |
532 (((WORD)srcbits[1] << 2) & gmask) |
533 (((WORD)srcbits[2] << 7) & rmask);
535 dstbits = (LPWORD)(dbits+=dstwidthb);
536 srcbits = (sbits += srcwidthb);
539 break;
541 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
543 LPDWORD srcbits = (LPDWORD)sbits;
544 DWORD val;
546 for( y = 0; y < lines; y++) {
547 for( x = 0; x < srcwidth; x++ ) {
548 val = *srcbits++;
549 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
550 ((val >> 9) & rmask));
552 dstbits = (LPWORD)(dbits+=dstwidthb);
553 srcbits = (LPDWORD)(sbits+=srcwidthb);
556 break;
558 default: /* ? bit bmp -> 16 bit DIB */
559 FIXME("15/16 bit DIB %d bit bitmap\n",
560 bmp->bitmap.bmBitsPixel);
561 break;
564 break;
566 case 24: /* 24 bpp dstDIB */
568 LPBYTE dstbits = dbits;
570 switch(bmp->dib->dsBm.bmBitsPixel) {
572 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
574 LPWORD srcbits = (LPWORD)sbits;
575 WORD val;
577 /* FIXME: BI_BITFIELDS not supported yet */
578 for( y = 0; y < lines; y++) {
579 for( x = 0; x < srcwidth; x++ ) {
580 val = *srcbits++;
581 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
582 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
583 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
585 dstbits = (LPBYTE)(dbits+=dstwidthb);
586 srcbits = (LPWORD)(sbits+=srcwidthb);
589 break;
591 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
593 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
594 memcpy(dbits, sbits, srcwidthb);
596 break;
598 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
600 LPBYTE srcbits = (LPBYTE)sbits;
602 for( y = 0; y < lines; y++) {
603 for( x = 0; x < srcwidth; x++, srcbits++ ) {
604 *dstbits++ = *srcbits++;
605 *dstbits++ = *srcbits++;
606 *dstbits++ = *srcbits++;
608 dstbits=(LPBYTE)(dbits+=dstwidthb);
609 srcbits = (LPBYTE)(sbits+=srcwidthb);
612 break;
614 default: /* ? bit bmp -> 24 bit DIB */
615 FIXME("24 bit DIB %d bit bitmap\n",
616 bmp->bitmap.bmBitsPixel);
617 break;
620 break;
622 case 32: /* 32 bpp dstDIB */
624 LPDWORD dstbits = (LPDWORD)dbits;
626 /* FIXME: BI_BITFIELDS not supported yet */
628 switch(bmp->dib->dsBm.bmBitsPixel) {
629 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
631 LPWORD srcbits = (LPWORD)sbits;
632 DWORD val;
634 /* FIXME: BI_BITFIELDS not supported yet */
635 for( y = 0; y < lines; y++) {
636 for( x = 0; x < srcwidth; x++ ) {
637 val = (DWORD)*srcbits++;
638 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
639 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
640 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
642 dstbits=(LPDWORD)(dbits+=dstwidthb);
643 srcbits=(LPWORD)(sbits+=srcwidthb);
646 break;
648 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
650 LPBYTE srcbits = sbits;
652 for( y = 0; y < lines; y++) {
653 for( x = 0; x < srcwidth; x++, srcbits+=3 )
654 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
655 dstbits=(LPDWORD)(dbits+=dstwidthb);
656 srcbits=(sbits+=srcwidthb);
659 break;
661 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
663 /* FIXME: BI_BITFIELDS not supported yet */
664 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
665 memcpy(dbits, sbits, srcwidthb);
667 break;
669 default: /* ? bit bmp -> 32 bit DIB */
670 FIXME("32 bit DIB %d bit bitmap\n",
671 bmp->bitmap.bmBitsPixel);
672 break;
675 break;
677 default: /* ? bit DIB */
678 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
679 break;
682 /* Otherwise, get bits from the XImage */
683 else
685 if (!bmp->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) lines = 0;
686 else
688 if (bmp->funcs && bmp->funcs->pGetDIBits)
689 lines = bmp->funcs->pGetDIBits( dc->physDev, hbitmap, startscan,
690 lines, bits, info, coloruse );
691 else
692 lines = 0; /* FIXME: should copy from bmp->bitmap.bmBits */
696 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
698 /* fill in struct members */
700 if( info->bmiHeader.biBitCount == 0)
702 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
703 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
704 info->bmiHeader.biPlanes = 1;
705 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
706 info->bmiHeader.biSizeImage =
707 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
708 bmp->bitmap.bmHeight,
709 bmp->bitmap.bmBitsPixel );
710 info->bmiHeader.biCompression = 0;
712 else
714 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
715 info->bmiHeader.biWidth,
716 info->bmiHeader.biHeight,
717 info->bmiHeader.biBitCount );
721 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
722 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
723 info->bmiHeader.biHeight);
725 GDI_ReleaseObj( hdc );
726 GDI_ReleaseObj( hbitmap );
728 return lines;
732 /***********************************************************************
733 * CreateDIBitmap (GDI32.@)
735 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
736 DWORD init, LPCVOID bits, const BITMAPINFO *data,
737 UINT coloruse )
739 HBITMAP handle;
740 BOOL fColor;
741 DWORD width;
742 int height;
743 WORD bpp;
744 WORD compr;
745 DC *dc;
747 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
748 if (height < 0) height = -height;
750 /* Check if we should create a monochrome or color bitmap. */
751 /* We create a monochrome bitmap only if it has exactly 2 */
752 /* colors, which are black followed by white, nothing else. */
753 /* In all other cases, we create a color bitmap. */
755 if (bpp != 1) fColor = TRUE;
756 else if ((coloruse != DIB_RGB_COLORS) || !data) fColor = FALSE;
757 else
759 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
761 RGBQUAD *rgb = data->bmiColors;
762 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
764 /* Check if the first color of the colormap is black */
765 if ((col == RGB(0,0,0)))
767 rgb++;
768 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
769 /* If the second color is white, create a monochrome bitmap */
770 fColor = (col != RGB(0xff,0xff,0xff));
772 /* Note : If the first color of the colormap is white
773 followed by black, we have to create a color bitmap.
774 If we don't the white will be displayed in black later on!*/
775 else fColor = TRUE;
777 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
779 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
780 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
781 if ((col == RGB(0,0,0)))
783 rgb++;
784 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
785 fColor = (col != RGB(0xff,0xff,0xff));
787 else fColor = TRUE;
789 else if (data->bmiHeader.biSize == sizeof(BITMAPV4HEADER))
790 { /* FIXME: correct ? */
791 RGBQUAD *rgb = data->bmiColors;
792 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
794 /* Check if the first color of the colormap is black */
795 if ((col == RGB(0,0,0)))
797 rgb++;
798 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
799 /* If the second color is white, create a monochrome bitmap */
800 fColor = (col != RGB(0xff,0xff,0xff));
802 /* Note : If the first color of the colormap is white
803 followed by black, we have to create a color bitmap.
804 If we don't the white will be displayed in black later on!*/
805 else fColor = TRUE;
807 else
809 ERR("(%ld): wrong/unknown size for data\n",
810 data->bmiHeader.biSize );
811 return 0;
815 /* Now create the bitmap */
817 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
819 if (fColor)
820 handle = CreateBitmap( width, height, GetDeviceCaps( hdc, PLANES ),
821 GetDeviceCaps( hdc, BITSPIXEL ), NULL );
822 else handle = CreateBitmap( width, height, 1, 1, NULL );
824 if (handle)
826 if (init == CBM_INIT) SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
827 else if (!BITMAP_SetOwnerDC( handle, dc ))
829 DeleteObject( handle );
830 handle = 0;
834 GDI_ReleaseObj( hdc );
835 return handle;
838 /***********************************************************************
839 * CreateDIBSection (GDI.489)
841 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
842 SEGPTR *bits16, HANDLE section, DWORD offset)
844 LPVOID bits32;
845 HBITMAP hbitmap;
847 hbitmap = CreateDIBSection( HDC_32(hdc), bmi, usage, &bits32, section, offset );
848 if (hbitmap)
850 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(hbitmap, BITMAP_MAGIC);
851 if (bmp && bmp->dib && bits32)
853 BITMAPINFOHEADER *bi = &bmi->bmiHeader;
854 INT height = bi->biHeight >= 0 ? bi->biHeight : -bi->biHeight;
855 INT width_bytes = DIB_GetDIBWidthBytes(bi->biWidth, bi->biBitCount);
856 INT size = (bi->biSizeImage && bi->biCompression != BI_RGB) ?
857 bi->biSizeImage : width_bytes * height;
859 /* calculate number of sel's needed for size with 64K steps */
860 WORD count = (size + 0xffff) / 0x10000;
861 WORD sel = AllocSelectorArray16(count);
862 int i;
864 for (i = 0; i < count; i++)
866 SetSelectorBase(sel + (i << __AHSHIFT), (DWORD)bits32 + i * 0x10000);
867 SetSelectorLimit16(sel + (i << __AHSHIFT), size - 1); /* yep, limit is correct */
868 size -= 0x10000;
870 bmp->segptr_bits = MAKESEGPTR( sel, 0 );
871 if (bits16) *bits16 = bmp->segptr_bits;
873 if (bmp) GDI_ReleaseObj( hbitmap );
875 return HBITMAP_16(hbitmap);
878 /***********************************************************************
879 * DIB_CreateDIBSection
881 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
882 LPVOID *bits, HANDLE section,
883 DWORD offset, DWORD ovr_pitch)
885 HBITMAP hbitmap = 0;
886 DC *dc;
887 BOOL bDesktopDC = FALSE;
889 /* If the reference hdc is null, take the desktop dc */
890 if (hdc == 0)
892 hdc = CreateCompatibleDC(0);
893 bDesktopDC = TRUE;
896 /* Windows ignores the supplied values of biClrUsed and biClrImportant thus: */
897 if (bmi->bmiHeader.biBitCount >= 1 && bmi->bmiHeader.biBitCount <= 8)
898 bmi->bmiHeader.biClrUsed = bmi->bmiHeader.biClrImportant = 1L << bmi->bmiHeader.biBitCount;
899 else
900 bmi->bmiHeader.biClrUsed = bmi->bmiHeader.biClrImportant = 0;
902 if ((dc = DC_GetDCPtr( hdc )))
904 if(dc->funcs->pCreateDIBSection)
905 hbitmap = dc->funcs->pCreateDIBSection(dc->physDev, bmi, usage, bits, section, offset, ovr_pitch);
906 GDI_ReleaseObj(hdc);
909 if (bDesktopDC)
910 DeleteDC(hdc);
912 return hbitmap;
915 /***********************************************************************
916 * CreateDIBSection (GDI32.@)
918 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
919 LPVOID *bits, HANDLE section,
920 DWORD offset)
922 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
925 /***********************************************************************
926 * DIB_CreateDIBFromBitmap
927 * Allocates a packed DIB and copies the bitmap data into it.
929 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
931 BITMAPOBJ *pBmp = NULL;
932 HGLOBAL hPackedDIB = 0;
933 LPBYTE pPackedDIB = NULL;
934 LPBITMAPINFOHEADER pbmiHeader = NULL;
935 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
936 OffsetBits = 0, nLinesCopied = 0;
938 /* Get a pointer to the BITMAPOBJ structure */
939 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
940 if (!pBmp) return hPackedDIB;
942 /* Get the bitmap dimensions */
943 width = pBmp->bitmap.bmWidth;
944 height = pBmp->bitmap.bmHeight;
945 depth = pBmp->bitmap.bmBitsPixel;
948 * A packed DIB contains a BITMAPINFO structure followed immediately by
949 * an optional color palette and the pixel data.
952 /* Calculate the size of the packed DIB */
953 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
954 cPackedSize = sizeof(BITMAPINFOHEADER)
955 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
956 + cDataSize;
957 /* Get the offset to the bits */
958 OffsetBits = cPackedSize - cDataSize;
960 /* Allocate the packed DIB */
961 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
962 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
963 cPackedSize );
964 if ( !hPackedDIB )
966 WARN("Could not allocate packed DIB!\n");
967 goto END;
970 /* A packed DIB starts with a BITMAPINFOHEADER */
971 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
972 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
974 /* Init the BITMAPINFOHEADER */
975 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
976 pbmiHeader->biWidth = width;
977 pbmiHeader->biHeight = height;
978 pbmiHeader->biPlanes = 1;
979 pbmiHeader->biBitCount = depth;
980 pbmiHeader->biCompression = BI_RGB;
981 pbmiHeader->biSizeImage = 0;
982 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
983 pbmiHeader->biClrUsed = 0;
984 pbmiHeader->biClrImportant = 0;
986 /* Retrieve the DIB bits from the bitmap and fill in the
987 * DIB color table if present */
989 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
990 hBmp, /* Handle to bitmap */
991 0, /* First scan line to set in dest bitmap */
992 height, /* Number of scan lines to copy */
993 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
994 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
995 0); /* RGB or palette index */
996 GlobalUnlock(hPackedDIB);
998 /* Cleanup if GetDIBits failed */
999 if (nLinesCopied != height)
1001 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
1002 GlobalFree(hPackedDIB);
1003 hPackedDIB = 0;
1006 END:
1007 GDI_ReleaseObj( hBmp );
1008 return hPackedDIB;