Ignore invalid iSubItem in LISTVIEW_GetItemT().
[wine.git] / objects / dib.c
blob604eb54e3c1fb958bacc09366aac959105d58c0e
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 "selectors.h"
29 #include "gdi.h"
30 #include "wownt32.h"
31 #include "wine/debug.h"
32 #include "palette.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
36 /***********************************************************************
37 * DIB_GetDIBWidthBytes
39 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
40 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
42 int DIB_GetDIBWidthBytes( int width, int depth )
44 int words;
46 switch(depth)
48 case 1: words = (width + 31) / 32; break;
49 case 4: words = (width + 7) / 8; break;
50 case 8: words = (width + 3) / 4; break;
51 case 15:
52 case 16: words = (width + 1) / 2; break;
53 case 24: words = (width * 3 + 3)/4; break;
55 default:
56 WARN("(%d): Unsupported depth\n", depth );
57 /* fall through */
58 case 32:
59 words = width;
61 return 4 * words;
64 /***********************************************************************
65 * DIB_GetDIBImageBytes
67 * Return the number of bytes used to hold the image in a DIB bitmap.
69 int DIB_GetDIBImageBytes( int width, int height, int depth )
71 return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
75 /***********************************************************************
76 * DIB_BitmapInfoSize
78 * Return the size of the bitmap info structure including color table.
80 int DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
82 int colors;
84 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
86 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
87 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
88 return sizeof(BITMAPCOREHEADER) + colors *
89 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
91 else /* assume BITMAPINFOHEADER */
93 colors = info->bmiHeader.biClrUsed;
94 if (!colors && (info->bmiHeader.biBitCount <= 8))
95 colors = 1 << info->bmiHeader.biBitCount;
96 return sizeof(BITMAPINFOHEADER) + colors *
97 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
102 /***********************************************************************
103 * DIB_GetBitmapInfo
105 * Get the info from a bitmap header.
106 * Return 1 for INFOHEADER, 0 for COREHEADER,
107 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
109 static int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
110 int *height, WORD *bpp, WORD *compr )
112 if (header->biSize == sizeof(BITMAPINFOHEADER))
114 *width = header->biWidth;
115 *height = header->biHeight;
116 *bpp = header->biBitCount;
117 *compr = header->biCompression;
118 return 1;
120 if (header->biSize == sizeof(BITMAPCOREHEADER))
122 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
123 *width = core->bcWidth;
124 *height = core->bcHeight;
125 *bpp = core->bcBitCount;
126 *compr = 0;
127 return 0;
129 if (header->biSize == sizeof(BITMAPV4HEADER))
131 BITMAPV4HEADER *v4hdr = (BITMAPV4HEADER *)header;
132 *width = v4hdr->bV4Width;
133 *height = v4hdr->bV4Height;
134 *bpp = v4hdr->bV4BitCount;
135 *compr = v4hdr->bV4V4Compression;
136 return 4;
138 if (header->biSize == sizeof(BITMAPV5HEADER))
140 BITMAPV5HEADER *v5hdr = (BITMAPV5HEADER *)header;
141 *width = v5hdr->bV5Width;
142 *height = v5hdr->bV5Height;
143 *bpp = v5hdr->bV5BitCount;
144 *compr = v5hdr->bV5Compression;
145 return 5;
147 ERR("(%ld): unknown/wrong size for header\n", header->biSize );
148 return -1;
152 /***********************************************************************
153 * StretchDIBits (GDI32.@)
155 INT WINAPI StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
156 INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
157 INT heightSrc, const void *bits,
158 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
160 DC *dc;
162 if (!bits || !info)
163 return 0;
165 dc = DC_GetDCUpdate( hdc );
166 if(!dc) return FALSE;
168 if(dc->funcs->pStretchDIBits)
170 heightSrc = dc->funcs->pStretchDIBits(dc->physDev, xDst, yDst, widthDst,
171 heightDst, xSrc, ySrc, widthSrc,
172 heightSrc, bits, info, wUsage, dwRop);
173 GDI_ReleaseObj( hdc );
175 else /* use StretchBlt */
177 HBITMAP hBitmap, hOldBitmap;
178 HDC hdcMem;
180 GDI_ReleaseObj( hdc );
181 hdcMem = CreateCompatibleDC( hdc );
182 hBitmap = CreateCompatibleBitmap(hdc, info->bmiHeader.biWidth,
183 info->bmiHeader.biHeight);
184 hOldBitmap = SelectObject( hdcMem, hBitmap );
186 if (info->bmiHeader.biCompression == BI_RLE4 ||
187 info->bmiHeader.biCompression == BI_RLE8) {
189 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
190 * contain all the rectangle described in bmiHeader, but only part of it.
191 * This mean that those undescribed pixels must be left untouched.
192 * So, we first copy on a memory bitmap the current content of the
193 * destination rectangle, blit the DIB bits on top of it - hence leaving
194 * the gaps untouched -, and blitting the rectangle back.
195 * This insure that gaps are untouched on the destination rectangle
196 * Not doing so leads to trashed images (the gaps contain what was on the
197 * memory bitmap => generally black or garbage)
198 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
199 * another speed vs correctness issue. Anyway, if speed is needed, then the
200 * pStretchDIBits function shall be implemented.
201 * ericP (2000/09/09)
204 /* copy existing bitmap from destination dc */
205 StretchBlt( hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
206 widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
207 dwRop );
210 SetDIBits(hdcMem, hBitmap, 0, info->bmiHeader.biHeight, bits,
211 info, wUsage);
213 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
214 left (negative biHeight) */
215 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
216 hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
217 widthSrc, heightSrc, dwRop );
218 SelectObject( hdcMem, hOldBitmap );
219 DeleteDC( hdcMem );
220 DeleteObject( hBitmap );
222 return heightSrc;
226 /******************************************************************************
227 * SetDIBits [GDI32.@] Sets pixels in a bitmap using colors from DIB
229 * PARAMS
230 * hdc [I] Handle to device context
231 * hbitmap [I] Handle to bitmap
232 * startscan [I] Starting scan line
233 * lines [I] Number of scan lines
234 * bits [I] Array of bitmap bits
235 * info [I] Address of structure with data
236 * coloruse [I] Type of color indexes to use
238 * RETURNS
239 * Success: Number of scan lines copied
240 * Failure: 0
242 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
243 UINT lines, LPCVOID bits, const BITMAPINFO *info,
244 UINT coloruse )
246 DC *dc;
247 BITMAPOBJ *bitmap;
248 INT result = 0;
250 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
252 if (!(dc = DC_GetDCUpdate( hdc )))
254 if (coloruse == DIB_RGB_COLORS) FIXME( "shouldn't require a DC for DIB_RGB_COLORS\n" );
255 GDI_ReleaseObj( hbitmap );
256 return 0;
259 if (!bitmap->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) goto done;
261 if (bitmap->funcs && bitmap->funcs->pSetDIBits)
262 result = bitmap->funcs->pSetDIBits( dc->physDev, hbitmap, startscan, lines,
263 bits, info, coloruse );
264 else
265 result = lines;
267 done:
268 GDI_ReleaseObj( hdc );
269 GDI_ReleaseObj( hbitmap );
270 return result;
274 /***********************************************************************
275 * SetDIBitsToDevice (GDI32.@)
277 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
278 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
279 UINT lines, LPCVOID bits, const BITMAPINFO *info,
280 UINT coloruse )
282 INT ret;
283 DC *dc;
285 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
287 if(dc->funcs->pSetDIBitsToDevice)
288 ret = dc->funcs->pSetDIBitsToDevice( dc->physDev, xDest, yDest, cx, cy, xSrc,
289 ySrc, startscan, lines, bits,
290 info, coloruse );
291 else {
292 FIXME("unimplemented on hdc %p\n", hdc);
293 ret = 0;
296 GDI_ReleaseObj( hdc );
297 return ret;
300 /***********************************************************************
301 * SetDIBColorTable (GDI32.@)
303 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
305 DC * dc;
306 UINT result = 0;
308 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
310 if (dc->funcs->pSetDIBColorTable)
311 result = dc->funcs->pSetDIBColorTable(dc->physDev, startpos, entries, colors);
313 GDI_ReleaseObj( hdc );
314 return result;
318 /***********************************************************************
319 * GetDIBColorTable (GDI32.@)
321 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
323 DC * dc;
324 UINT result = 0;
326 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
328 if (dc->funcs->pGetDIBColorTable)
329 result = dc->funcs->pGetDIBColorTable(dc->physDev, startpos, entries, colors);
331 GDI_ReleaseObj( hdc );
332 return result;
335 /* FIXME the following two structs should be combined with __sysPalTemplate in
336 objects/color.c - this should happen after de-X11-ing both of these
337 files.
338 NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
339 and blue - sigh */
341 static RGBQUAD EGAColors[16] = {
342 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
343 { 0x00, 0x00, 0x00, 0x00 },
344 { 0x00, 0x00, 0x80, 0x00 },
345 { 0x00, 0x80, 0x00, 0x00 },
346 { 0x00, 0x80, 0x80, 0x00 },
347 { 0x80, 0x00, 0x00, 0x00 },
348 { 0x80, 0x00, 0x80, 0x00 },
349 { 0x80, 0x80, 0x00, 0x00 },
350 { 0x80, 0x80, 0x80, 0x00 },
351 { 0xc0, 0xc0, 0xc0, 0x00 },
352 { 0x00, 0x00, 0xff, 0x00 },
353 { 0x00, 0xff, 0x00, 0x00 },
354 { 0x00, 0xff, 0xff, 0x00 },
355 { 0xff, 0x00, 0x00, 0x00 },
356 { 0xff, 0x00, 0xff, 0x00 },
357 { 0xff, 0xff, 0x00, 0x00 },
358 { 0xff, 0xff, 0xff, 0x00 }
362 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
363 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
364 { 0x00, 0x00, 0x00, 0x00 },
365 { 0x00, 0x00, 0x80, 0x00 },
366 { 0x00, 0x80, 0x00, 0x00 },
367 { 0x00, 0x80, 0x80, 0x00 },
368 { 0x80, 0x00, 0x00, 0x00 },
369 { 0x80, 0x00, 0x80, 0x00 },
370 { 0x80, 0x80, 0x00, 0x00 },
371 { 0xc0, 0xc0, 0xc0, 0x00 },
372 { 0xc0, 0xdc, 0xc0, 0x00 },
373 { 0xf0, 0xca, 0xa6, 0x00 },
374 { 0xf0, 0xfb, 0xff, 0x00 },
375 { 0xa4, 0xa0, 0xa0, 0x00 },
376 { 0x80, 0x80, 0x80, 0x00 },
377 { 0x00, 0x00, 0xf0, 0x00 },
378 { 0x00, 0xff, 0x00, 0x00 },
379 { 0x00, 0xff, 0xff, 0x00 },
380 { 0xff, 0x00, 0x00, 0x00 },
381 { 0xff, 0x00, 0xff, 0x00 },
382 { 0xff, 0xff, 0x00, 0x00 },
383 { 0xff, 0xff, 0xff, 0x00 }
387 /******************************************************************************
388 * GetDIBits [GDI32.@] Retrieves bits of bitmap and copies to buffer
390 * RETURNS
391 * Success: Number of scan lines copied from bitmap
392 * Failure: 0
394 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
396 INT WINAPI GetDIBits(
397 HDC hdc, /* [in] Handle to device context */
398 HBITMAP hbitmap, /* [in] Handle to bitmap */
399 UINT startscan, /* [in] First scan line to set in dest bitmap */
400 UINT lines, /* [in] Number of scan lines to copy */
401 LPVOID bits, /* [out] Address of array for bitmap bits */
402 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
403 UINT coloruse) /* [in] RGB or palette index */
405 DC * dc;
406 BITMAPOBJ * bmp;
407 int i;
409 if (!info) return 0;
410 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
411 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
413 GDI_ReleaseObj( hdc );
414 return 0;
417 /* Transfer color info */
419 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
421 info->bmiHeader.biClrUsed = 0;
423 /* If the bitmap object already has a dib section at the
424 same color depth then get the color map from it */
425 if (bmp->dib && bmp->dib->dsBm.bmBitsPixel == info->bmiHeader.biBitCount) {
426 GetDIBColorTable(hdc, 0, 1 << info->bmiHeader.biBitCount, info->bmiColors);
428 else {
429 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
430 /* Generate the color map from the selected palette */
431 PALETTEENTRY * palEntry;
432 PALETTEOBJ * palette;
433 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC ))) {
434 GDI_ReleaseObj( hdc );
435 GDI_ReleaseObj( hbitmap );
436 return 0;
438 palEntry = palette->logpalette.palPalEntry;
439 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
440 if (coloruse == DIB_RGB_COLORS) {
441 info->bmiColors[i].rgbRed = palEntry->peRed;
442 info->bmiColors[i].rgbGreen = palEntry->peGreen;
443 info->bmiColors[i].rgbBlue = palEntry->peBlue;
444 info->bmiColors[i].rgbReserved = 0;
446 else ((WORD *)info->bmiColors)[i] = (WORD)i;
448 GDI_ReleaseObj( dc->hPalette );
449 } else {
450 switch (info->bmiHeader.biBitCount) {
451 case 1:
452 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
453 info->bmiColors[0].rgbBlue = 0;
454 info->bmiColors[0].rgbReserved = 0;
455 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
456 info->bmiColors[1].rgbBlue = 0xff;
457 info->bmiColors[1].rgbReserved = 0;
458 break;
460 case 4:
461 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
462 break;
464 case 8:
466 INT r, g, b;
467 RGBQUAD *color;
469 memcpy(info->bmiColors, DefLogPalette,
470 10 * sizeof(RGBQUAD));
471 memcpy(info->bmiColors + 246, DefLogPalette + 10,
472 10 * sizeof(RGBQUAD));
473 color = info->bmiColors + 10;
474 for(r = 0; r <= 5; r++) /* FIXME */
475 for(g = 0; g <= 5; g++)
476 for(b = 0; b <= 5; b++) {
477 color->rgbRed = (r * 0xff) / 5;
478 color->rgbGreen = (g * 0xff) / 5;
479 color->rgbBlue = (b * 0xff) / 5;
480 color->rgbReserved = 0;
481 color++;
489 if (bits && lines)
491 /* If the bitmap object already have a dib section that contains image data, get the bits from it */
492 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
494 /*FIXME: Only RGB dibs supported for now */
495 unsigned int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
496 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
497 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
498 unsigned int x, y;
500 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
502 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
503 dstwidthb = -dstwidthb;
506 switch( info->bmiHeader.biBitCount ) {
508 case 15:
509 case 16: /* 16 bpp dstDIB */
511 LPWORD dstbits = (LPWORD)dbits;
512 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
514 /* FIXME: BI_BITFIELDS not supported yet */
516 switch(bmp->dib->dsBm.bmBitsPixel) {
518 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
520 /* FIXME: BI_BITFIELDS not supported yet */
521 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
522 memcpy(dbits, sbits, srcwidthb);
524 break;
526 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
528 LPBYTE srcbits = sbits;
530 for( y = 0; y < lines; y++) {
531 for( x = 0; x < srcwidth; x++, srcbits += 3)
532 *dstbits++ = ((srcbits[0] >> 3) & bmask) |
533 (((WORD)srcbits[1] << 2) & gmask) |
534 (((WORD)srcbits[2] << 7) & rmask);
536 dstbits = (LPWORD)(dbits+=dstwidthb);
537 srcbits = (sbits += srcwidthb);
540 break;
542 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
544 LPDWORD srcbits = (LPDWORD)sbits;
545 DWORD val;
547 for( y = 0; y < lines; y++) {
548 for( x = 0; x < srcwidth; x++ ) {
549 val = *srcbits++;
550 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
551 ((val >> 9) & rmask));
553 dstbits = (LPWORD)(dbits+=dstwidthb);
554 srcbits = (LPDWORD)(sbits+=srcwidthb);
557 break;
559 default: /* ? bit bmp -> 16 bit DIB */
560 FIXME("15/16 bit DIB %d bit bitmap\n",
561 bmp->bitmap.bmBitsPixel);
562 break;
565 break;
567 case 24: /* 24 bpp dstDIB */
569 LPBYTE dstbits = dbits;
571 switch(bmp->dib->dsBm.bmBitsPixel) {
573 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
575 LPWORD srcbits = (LPWORD)sbits;
576 WORD val;
578 /* FIXME: BI_BITFIELDS not supported yet */
579 for( y = 0; y < lines; y++) {
580 for( x = 0; x < srcwidth; x++ ) {
581 val = *srcbits++;
582 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
583 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
584 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
586 dstbits = (LPBYTE)(dbits+=dstwidthb);
587 srcbits = (LPWORD)(sbits+=srcwidthb);
590 break;
592 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
594 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
595 memcpy(dbits, sbits, srcwidthb);
597 break;
599 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
601 LPBYTE srcbits = (LPBYTE)sbits;
603 for( y = 0; y < lines; y++) {
604 for( x = 0; x < srcwidth; x++, srcbits++ ) {
605 *dstbits++ = *srcbits++;
606 *dstbits++ = *srcbits++;
607 *dstbits++ = *srcbits++;
609 dstbits=(LPBYTE)(dbits+=dstwidthb);
610 srcbits = (LPBYTE)(sbits+=srcwidthb);
613 break;
615 default: /* ? bit bmp -> 24 bit DIB */
616 FIXME("24 bit DIB %d bit bitmap\n",
617 bmp->bitmap.bmBitsPixel);
618 break;
621 break;
623 case 32: /* 32 bpp dstDIB */
625 LPDWORD dstbits = (LPDWORD)dbits;
627 /* FIXME: BI_BITFIELDS not supported yet */
629 switch(bmp->dib->dsBm.bmBitsPixel) {
630 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
632 LPWORD srcbits = (LPWORD)sbits;
633 DWORD val;
635 /* FIXME: BI_BITFIELDS not supported yet */
636 for( y = 0; y < lines; y++) {
637 for( x = 0; x < srcwidth; x++ ) {
638 val = (DWORD)*srcbits++;
639 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
640 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
641 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
643 dstbits=(LPDWORD)(dbits+=dstwidthb);
644 srcbits=(LPWORD)(sbits+=srcwidthb);
647 break;
649 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
651 LPBYTE srcbits = sbits;
653 for( y = 0; y < lines; y++) {
654 for( x = 0; x < srcwidth; x++, srcbits+=3 )
655 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
656 dstbits=(LPDWORD)(dbits+=dstwidthb);
657 srcbits=(sbits+=srcwidthb);
660 break;
662 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
664 /* FIXME: BI_BITFIELDS not supported yet */
665 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
666 memcpy(dbits, sbits, srcwidthb);
668 break;
670 default: /* ? bit bmp -> 32 bit DIB */
671 FIXME("32 bit DIB %d bit bitmap\n",
672 bmp->bitmap.bmBitsPixel);
673 break;
676 break;
678 default: /* ? bit DIB */
679 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
680 break;
683 /* Otherwise, get bits from the XImage */
684 else
686 if (!bmp->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) lines = 0;
687 else
689 if (bmp->funcs && bmp->funcs->pGetDIBits)
690 lines = bmp->funcs->pGetDIBits( dc->physDev, hbitmap, startscan,
691 lines, bits, info, coloruse );
692 else
693 lines = 0; /* FIXME: should copy from bmp->bitmap.bmBits */
697 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
699 /* fill in struct members */
701 if( info->bmiHeader.biBitCount == 0)
703 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
704 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
705 info->bmiHeader.biPlanes = 1;
706 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
707 info->bmiHeader.biSizeImage =
708 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
709 bmp->bitmap.bmHeight,
710 bmp->bitmap.bmBitsPixel );
711 info->bmiHeader.biCompression = 0;
713 else
715 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
716 info->bmiHeader.biWidth,
717 info->bmiHeader.biHeight,
718 info->bmiHeader.biBitCount );
722 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
723 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
724 info->bmiHeader.biHeight);
726 GDI_ReleaseObj( hdc );
727 GDI_ReleaseObj( hbitmap );
729 return lines;
733 /***********************************************************************
734 * CreateDIBitmap (GDI32.@)
736 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
737 DWORD init, LPCVOID bits, const BITMAPINFO *data,
738 UINT coloruse )
740 HBITMAP handle;
741 BOOL fColor;
742 DWORD width;
743 int height;
744 WORD bpp;
745 WORD compr;
746 DC *dc;
748 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
749 if (height < 0) height = -height;
751 /* Check if we should create a monochrome or color bitmap. */
752 /* We create a monochrome bitmap only if it has exactly 2 */
753 /* colors, which are black followed by white, nothing else. */
754 /* In all other cases, we create a color bitmap. */
756 if (bpp != 1) fColor = TRUE;
757 else if ((coloruse != DIB_RGB_COLORS) || !data) fColor = FALSE;
758 else
760 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
762 RGBQUAD *rgb = data->bmiColors;
763 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
765 /* Check if the first color of the colormap is black */
766 if ((col == RGB(0,0,0)))
768 rgb++;
769 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
770 /* If the second color is white, create a monochrome bitmap */
771 fColor = (col != RGB(0xff,0xff,0xff));
773 /* Note : If the first color of the colormap is white
774 followed by black, we have to create a color bitmap.
775 If we don't the white will be displayed in black later on!*/
776 else fColor = TRUE;
778 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
780 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
781 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
782 if ((col == RGB(0,0,0)))
784 rgb++;
785 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
786 fColor = (col != RGB(0xff,0xff,0xff));
788 else fColor = TRUE;
790 else if (data->bmiHeader.biSize == sizeof(BITMAPV4HEADER))
791 { /* FIXME: correct ? */
792 RGBQUAD *rgb = data->bmiColors;
793 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
795 /* Check if the first color of the colormap is black */
796 if ((col == RGB(0,0,0)))
798 rgb++;
799 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
800 /* If the second color is white, create a monochrome bitmap */
801 fColor = (col != RGB(0xff,0xff,0xff));
803 /* Note : If the first color of the colormap is white
804 followed by black, we have to create a color bitmap.
805 If we don't the white will be displayed in black later on!*/
806 else fColor = TRUE;
808 else
810 ERR("(%ld): wrong/unknown size for data\n",
811 data->bmiHeader.biSize );
812 return 0;
816 /* Now create the bitmap */
818 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
820 if (fColor)
821 handle = CreateBitmap( width, height, GetDeviceCaps( hdc, PLANES ),
822 GetDeviceCaps( hdc, BITSPIXEL ), NULL );
823 else handle = CreateBitmap( width, height, 1, 1, NULL );
825 if (handle)
827 if (init == CBM_INIT) SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
828 else if (!BITMAP_SetOwnerDC( handle, dc ))
830 DeleteObject( handle );
831 handle = 0;
835 GDI_ReleaseObj( hdc );
836 return handle;
839 /***********************************************************************
840 * CreateDIBSection (GDI.489)
842 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
843 SEGPTR *bits16, HANDLE section, DWORD offset)
845 LPVOID bits32;
846 HBITMAP hbitmap;
848 hbitmap = CreateDIBSection( HDC_32(hdc), bmi, usage, &bits32, section, offset );
849 if (hbitmap)
851 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(hbitmap, BITMAP_MAGIC);
852 if (bmp && bmp->dib && bits32)
854 BITMAPINFOHEADER *bi = &bmi->bmiHeader;
855 INT height = bi->biHeight >= 0 ? bi->biHeight : -bi->biHeight;
856 INT width_bytes = DIB_GetDIBWidthBytes(bi->biWidth, bi->biBitCount);
857 INT size = (bi->biSizeImage && bi->biCompression != BI_RGB) ?
858 bi->biSizeImage : width_bytes * height;
860 /* calculate number of sel's needed for size with 64K steps */
861 WORD count = (size + 0xffff) / 0x10000;
862 WORD sel = AllocSelectorArray16(count);
863 int i;
865 for (i = 0; i < count; i++)
867 SetSelectorBase(sel + (i << __AHSHIFT), (DWORD)bits32 + i * 0x10000);
868 SetSelectorLimit16(sel + (i << __AHSHIFT), size - 1); /* yep, limit is correct */
869 size -= 0x10000;
871 bmp->segptr_bits = MAKESEGPTR( sel, 0 );
872 if (bits16) *bits16 = bmp->segptr_bits;
874 if (bmp) GDI_ReleaseObj( hbitmap );
876 return HBITMAP_16(hbitmap);
879 /***********************************************************************
880 * DIB_CreateDIBSection
882 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
883 LPVOID *bits, HANDLE section,
884 DWORD offset, DWORD ovr_pitch)
886 HBITMAP hbitmap = 0;
887 DC *dc;
888 BOOL bDesktopDC = FALSE;
890 /* If the reference hdc is null, take the desktop dc */
891 if (hdc == 0)
893 hdc = CreateCompatibleDC(0);
894 bDesktopDC = TRUE;
897 /* Windows ignores the supplied values of biClrUsed and biClrImportant thus: */
898 if (bmi->bmiHeader.biBitCount >= 1 && bmi->bmiHeader.biBitCount <= 8)
899 bmi->bmiHeader.biClrUsed = bmi->bmiHeader.biClrImportant = 1L << bmi->bmiHeader.biBitCount;
900 else
901 bmi->bmiHeader.biClrUsed = bmi->bmiHeader.biClrImportant = 0;
903 if ((dc = DC_GetDCPtr( hdc )))
905 if(dc->funcs->pCreateDIBSection)
906 hbitmap = dc->funcs->pCreateDIBSection(dc->physDev, bmi, usage, bits, section, offset, ovr_pitch);
907 GDI_ReleaseObj(hdc);
910 if (bDesktopDC)
911 DeleteDC(hdc);
913 return hbitmap;
916 /***********************************************************************
917 * CreateDIBSection (GDI32.@)
919 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
920 LPVOID *bits, HANDLE section,
921 DWORD offset)
923 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
926 /***********************************************************************
927 * DIB_CreateDIBFromBitmap
928 * Allocates a packed DIB and copies the bitmap data into it.
930 HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
932 BITMAPOBJ *pBmp = NULL;
933 HGLOBAL hPackedDIB = 0;
934 LPBYTE pPackedDIB = NULL;
935 LPBITMAPINFOHEADER pbmiHeader = NULL;
936 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
937 OffsetBits = 0, nLinesCopied = 0;
939 /* Get a pointer to the BITMAPOBJ structure */
940 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
941 if (!pBmp) return hPackedDIB;
943 /* Get the bitmap dimensions */
944 width = pBmp->bitmap.bmWidth;
945 height = pBmp->bitmap.bmHeight;
946 depth = pBmp->bitmap.bmBitsPixel;
949 * A packed DIB contains a BITMAPINFO structure followed immediately by
950 * an optional color palette and the pixel data.
953 /* Calculate the size of the packed DIB */
954 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
955 cPackedSize = sizeof(BITMAPINFOHEADER)
956 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
957 + cDataSize;
958 /* Get the offset to the bits */
959 OffsetBits = cPackedSize - cDataSize;
961 /* Allocate the packed DIB */
962 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
963 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
964 cPackedSize );
965 if ( !hPackedDIB )
967 WARN("Could not allocate packed DIB!\n");
968 goto END;
971 /* A packed DIB starts with a BITMAPINFOHEADER */
972 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
973 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
975 /* Init the BITMAPINFOHEADER */
976 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
977 pbmiHeader->biWidth = width;
978 pbmiHeader->biHeight = height;
979 pbmiHeader->biPlanes = 1;
980 pbmiHeader->biBitCount = depth;
981 pbmiHeader->biCompression = BI_RGB;
982 pbmiHeader->biSizeImage = 0;
983 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
984 pbmiHeader->biClrUsed = 0;
985 pbmiHeader->biClrImportant = 0;
987 /* Retrieve the DIB bits from the bitmap and fill in the
988 * DIB color table if present */
990 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
991 hBmp, /* Handle to bitmap */
992 0, /* First scan line to set in dest bitmap */
993 height, /* Number of scan lines to copy */
994 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
995 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
996 0); /* RGB or palette index */
997 GlobalUnlock(hPackedDIB);
999 /* Cleanup if GetDIBits failed */
1000 if (nLinesCopied != height)
1002 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
1003 GlobalFree(hPackedDIB);
1004 hPackedDIB = 0;
1007 END:
1008 GDI_ReleaseObj( hBmp );
1009 return hPackedDIB;