Avoid direct accesses to the xformWorld2Vport DC field.
[wine/multimedia.git] / objects / dib.c
blobeb68bedc0af6537903d71e651995f4914eade864
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 "gdi_private.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.@]
229 * Sets pixels in a bitmap using colors from DIB.
231 * PARAMS
232 * hdc [I] Handle to device context
233 * hbitmap [I] Handle to bitmap
234 * startscan [I] Starting scan line
235 * lines [I] Number of scan lines
236 * bits [I] Array of bitmap bits
237 * info [I] Address of structure with data
238 * coloruse [I] Type of color indexes to use
240 * RETURNS
241 * Success: Number of scan lines copied
242 * Failure: 0
244 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
245 UINT lines, LPCVOID bits, const BITMAPINFO *info,
246 UINT coloruse )
248 DC *dc;
249 BITMAPOBJ *bitmap;
250 INT result = 0;
252 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return 0;
254 if (!(dc = DC_GetDCUpdate( hdc )))
256 if (coloruse == DIB_RGB_COLORS) FIXME( "shouldn't require a DC for DIB_RGB_COLORS\n" );
257 GDI_ReleaseObj( hbitmap );
258 return 0;
261 if (!bitmap->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) goto done;
263 if (bitmap->funcs && bitmap->funcs->pSetDIBits)
264 result = bitmap->funcs->pSetDIBits( dc->physDev, hbitmap, startscan, lines,
265 bits, info, coloruse );
266 else
267 result = lines;
269 done:
270 GDI_ReleaseObj( hdc );
271 GDI_ReleaseObj( hbitmap );
272 return result;
276 /***********************************************************************
277 * SetDIBitsToDevice (GDI32.@)
279 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
280 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
281 UINT lines, LPCVOID bits, const BITMAPINFO *info,
282 UINT coloruse )
284 INT ret;
285 DC *dc;
287 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
289 if(dc->funcs->pSetDIBitsToDevice)
290 ret = dc->funcs->pSetDIBitsToDevice( dc->physDev, xDest, yDest, cx, cy, xSrc,
291 ySrc, startscan, lines, bits,
292 info, coloruse );
293 else {
294 FIXME("unimplemented on hdc %p\n", hdc);
295 ret = 0;
298 GDI_ReleaseObj( hdc );
299 return ret;
302 /***********************************************************************
303 * SetDIBColorTable (GDI32.@)
305 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
307 DC * dc;
308 UINT result = 0;
310 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
312 if (dc->funcs->pSetDIBColorTable)
313 result = dc->funcs->pSetDIBColorTable(dc->physDev, startpos, entries, colors);
315 GDI_ReleaseObj( hdc );
316 return result;
320 /***********************************************************************
321 * GetDIBColorTable (GDI32.@)
323 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
325 DC * dc;
326 UINT result = 0;
328 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
330 if (dc->funcs->pGetDIBColorTable)
331 result = dc->funcs->pGetDIBColorTable(dc->physDev, startpos, entries, colors);
333 GDI_ReleaseObj( hdc );
334 return result;
337 /* FIXME the following two structs should be combined with __sysPalTemplate in
338 objects/color.c - this should happen after de-X11-ing both of these
339 files.
340 NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
341 and blue - sigh */
343 static RGBQUAD EGAColors[16] = {
344 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
345 { 0x00, 0x00, 0x00, 0x00 },
346 { 0x00, 0x00, 0x80, 0x00 },
347 { 0x00, 0x80, 0x00, 0x00 },
348 { 0x00, 0x80, 0x80, 0x00 },
349 { 0x80, 0x00, 0x00, 0x00 },
350 { 0x80, 0x00, 0x80, 0x00 },
351 { 0x80, 0x80, 0x00, 0x00 },
352 { 0x80, 0x80, 0x80, 0x00 },
353 { 0xc0, 0xc0, 0xc0, 0x00 },
354 { 0x00, 0x00, 0xff, 0x00 },
355 { 0x00, 0xff, 0x00, 0x00 },
356 { 0x00, 0xff, 0xff, 0x00 },
357 { 0xff, 0x00, 0x00, 0x00 },
358 { 0xff, 0x00, 0xff, 0x00 },
359 { 0xff, 0xff, 0x00, 0x00 },
360 { 0xff, 0xff, 0xff, 0x00 }
364 static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
365 /* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
366 { 0x00, 0x00, 0x00, 0x00 },
367 { 0x00, 0x00, 0x80, 0x00 },
368 { 0x00, 0x80, 0x00, 0x00 },
369 { 0x00, 0x80, 0x80, 0x00 },
370 { 0x80, 0x00, 0x00, 0x00 },
371 { 0x80, 0x00, 0x80, 0x00 },
372 { 0x80, 0x80, 0x00, 0x00 },
373 { 0xc0, 0xc0, 0xc0, 0x00 },
374 { 0xc0, 0xdc, 0xc0, 0x00 },
375 { 0xf0, 0xca, 0xa6, 0x00 },
376 { 0xf0, 0xfb, 0xff, 0x00 },
377 { 0xa4, 0xa0, 0xa0, 0x00 },
378 { 0x80, 0x80, 0x80, 0x00 },
379 { 0x00, 0x00, 0xf0, 0x00 },
380 { 0x00, 0xff, 0x00, 0x00 },
381 { 0x00, 0xff, 0xff, 0x00 },
382 { 0xff, 0x00, 0x00, 0x00 },
383 { 0xff, 0x00, 0xff, 0x00 },
384 { 0xff, 0xff, 0x00, 0x00 },
385 { 0xff, 0xff, 0xff, 0x00 }
389 /******************************************************************************
390 * GetDIBits [GDI32.@]
392 * Retrieves bits of bitmap and copies to buffer.
394 * RETURNS
395 * Success: Number of scan lines copied from bitmap
396 * Failure: 0
398 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
400 INT WINAPI GetDIBits(
401 HDC hdc, /* [in] Handle to device context */
402 HBITMAP hbitmap, /* [in] Handle to bitmap */
403 UINT startscan, /* [in] First scan line to set in dest bitmap */
404 UINT lines, /* [in] Number of scan lines to copy */
405 LPVOID bits, /* [out] Address of array for bitmap bits */
406 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
407 UINT coloruse) /* [in] RGB or palette index */
409 DC * dc;
410 BITMAPOBJ * bmp;
411 int i;
413 if (!info) return 0;
414 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
415 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
417 GDI_ReleaseObj( hdc );
418 return 0;
421 /* Transfer color info */
423 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
425 info->bmiHeader.biClrUsed = 0;
427 /* If the bitmap object already has a dib section at the
428 same color depth then get the color map from it */
429 if (bmp->dib && bmp->dib->dsBm.bmBitsPixel == info->bmiHeader.biBitCount) {
430 GetDIBColorTable(hdc, 0, 1 << info->bmiHeader.biBitCount, info->bmiColors);
432 else {
433 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
434 /* Generate the color map from the selected palette */
435 PALETTEENTRY * palEntry;
436 PALETTEOBJ * palette;
437 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC ))) {
438 GDI_ReleaseObj( hdc );
439 GDI_ReleaseObj( hbitmap );
440 return 0;
442 palEntry = palette->logpalette.palPalEntry;
443 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
444 if (coloruse == DIB_RGB_COLORS) {
445 info->bmiColors[i].rgbRed = palEntry->peRed;
446 info->bmiColors[i].rgbGreen = palEntry->peGreen;
447 info->bmiColors[i].rgbBlue = palEntry->peBlue;
448 info->bmiColors[i].rgbReserved = 0;
450 else ((WORD *)info->bmiColors)[i] = (WORD)i;
452 GDI_ReleaseObj( dc->hPalette );
453 } else {
454 switch (info->bmiHeader.biBitCount) {
455 case 1:
456 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
457 info->bmiColors[0].rgbBlue = 0;
458 info->bmiColors[0].rgbReserved = 0;
459 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
460 info->bmiColors[1].rgbBlue = 0xff;
461 info->bmiColors[1].rgbReserved = 0;
462 break;
464 case 4:
465 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
466 break;
468 case 8:
470 INT r, g, b;
471 RGBQUAD *color;
473 memcpy(info->bmiColors, DefLogPalette,
474 10 * sizeof(RGBQUAD));
475 memcpy(info->bmiColors + 246, DefLogPalette + 10,
476 10 * sizeof(RGBQUAD));
477 color = info->bmiColors + 10;
478 for(r = 0; r <= 5; r++) /* FIXME */
479 for(g = 0; g <= 5; g++)
480 for(b = 0; b <= 5; b++) {
481 color->rgbRed = (r * 0xff) / 5;
482 color->rgbGreen = (g * 0xff) / 5;
483 color->rgbBlue = (b * 0xff) / 5;
484 color->rgbReserved = 0;
485 color++;
493 if (bits && lines)
495 /* If the bitmap object already have a dib section that contains image data, get the bits from it */
496 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
498 /*FIXME: Only RGB dibs supported for now */
499 unsigned int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
500 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
501 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
502 unsigned int x, y;
504 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
506 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
507 dstwidthb = -dstwidthb;
510 switch( info->bmiHeader.biBitCount ) {
512 case 15:
513 case 16: /* 16 bpp dstDIB */
515 LPWORD dstbits = (LPWORD)dbits;
516 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
518 /* FIXME: BI_BITFIELDS not supported yet */
520 switch(bmp->dib->dsBm.bmBitsPixel) {
522 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
524 /* FIXME: BI_BITFIELDS not supported yet */
525 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
526 memcpy(dbits, sbits, srcwidthb);
528 break;
530 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
532 LPBYTE srcbits = sbits;
534 for( y = 0; y < lines; y++) {
535 for( x = 0; x < srcwidth; x++, srcbits += 3)
536 *dstbits++ = ((srcbits[0] >> 3) & bmask) |
537 (((WORD)srcbits[1] << 2) & gmask) |
538 (((WORD)srcbits[2] << 7) & rmask);
540 dstbits = (LPWORD)(dbits+=dstwidthb);
541 srcbits = (sbits += srcwidthb);
544 break;
546 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
548 LPDWORD srcbits = (LPDWORD)sbits;
549 DWORD val;
551 for( y = 0; y < lines; y++) {
552 for( x = 0; x < srcwidth; x++ ) {
553 val = *srcbits++;
554 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
555 ((val >> 9) & rmask));
557 dstbits = (LPWORD)(dbits+=dstwidthb);
558 srcbits = (LPDWORD)(sbits+=srcwidthb);
561 break;
563 default: /* ? bit bmp -> 16 bit DIB */
564 FIXME("15/16 bit DIB %d bit bitmap\n",
565 bmp->bitmap.bmBitsPixel);
566 break;
569 break;
571 case 24: /* 24 bpp dstDIB */
573 LPBYTE dstbits = dbits;
575 switch(bmp->dib->dsBm.bmBitsPixel) {
577 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
579 LPWORD srcbits = (LPWORD)sbits;
580 WORD val;
582 /* FIXME: BI_BITFIELDS not supported yet */
583 for( y = 0; y < lines; y++) {
584 for( x = 0; x < srcwidth; x++ ) {
585 val = *srcbits++;
586 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
587 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
588 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
590 dstbits = (LPBYTE)(dbits+=dstwidthb);
591 srcbits = (LPWORD)(sbits+=srcwidthb);
594 break;
596 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
598 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
599 memcpy(dbits, sbits, srcwidthb);
601 break;
603 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
605 LPBYTE srcbits = (LPBYTE)sbits;
607 for( y = 0; y < lines; y++) {
608 for( x = 0; x < srcwidth; x++, srcbits++ ) {
609 *dstbits++ = *srcbits++;
610 *dstbits++ = *srcbits++;
611 *dstbits++ = *srcbits++;
613 dstbits=(LPBYTE)(dbits+=dstwidthb);
614 srcbits = (LPBYTE)(sbits+=srcwidthb);
617 break;
619 default: /* ? bit bmp -> 24 bit DIB */
620 FIXME("24 bit DIB %d bit bitmap\n",
621 bmp->bitmap.bmBitsPixel);
622 break;
625 break;
627 case 32: /* 32 bpp dstDIB */
629 LPDWORD dstbits = (LPDWORD)dbits;
631 /* FIXME: BI_BITFIELDS not supported yet */
633 switch(bmp->dib->dsBm.bmBitsPixel) {
634 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
636 LPWORD srcbits = (LPWORD)sbits;
637 DWORD val;
639 /* FIXME: BI_BITFIELDS not supported yet */
640 for( y = 0; y < lines; y++) {
641 for( x = 0; x < srcwidth; x++ ) {
642 val = (DWORD)*srcbits++;
643 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
644 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
645 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
647 dstbits=(LPDWORD)(dbits+=dstwidthb);
648 srcbits=(LPWORD)(sbits+=srcwidthb);
651 break;
653 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
655 LPBYTE srcbits = sbits;
657 for( y = 0; y < lines; y++) {
658 for( x = 0; x < srcwidth; x++, srcbits+=3 )
659 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
660 dstbits=(LPDWORD)(dbits+=dstwidthb);
661 srcbits=(sbits+=srcwidthb);
664 break;
666 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
668 /* FIXME: BI_BITFIELDS not supported yet */
669 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
670 memcpy(dbits, sbits, srcwidthb);
672 break;
674 default: /* ? bit bmp -> 32 bit DIB */
675 FIXME("32 bit DIB %d bit bitmap\n",
676 bmp->bitmap.bmBitsPixel);
677 break;
680 break;
682 default: /* ? bit DIB */
683 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
684 break;
687 /* Otherwise, get bits from the XImage */
688 else
690 if (!bmp->funcs && !BITMAP_SetOwnerDC( hbitmap, dc )) lines = 0;
691 else
693 if (bmp->funcs && bmp->funcs->pGetDIBits)
694 lines = bmp->funcs->pGetDIBits( dc->physDev, hbitmap, startscan,
695 lines, bits, info, coloruse );
696 else
697 lines = 0; /* FIXME: should copy from bmp->bitmap.bmBits */
701 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
703 /* fill in struct members */
705 if( info->bmiHeader.biBitCount == 0)
707 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
708 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
709 info->bmiHeader.biPlanes = 1;
710 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
711 info->bmiHeader.biSizeImage =
712 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
713 bmp->bitmap.bmHeight,
714 bmp->bitmap.bmBitsPixel );
715 info->bmiHeader.biCompression = 0;
717 else
719 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
720 info->bmiHeader.biWidth,
721 info->bmiHeader.biHeight,
722 info->bmiHeader.biBitCount );
726 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
727 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
728 info->bmiHeader.biHeight);
730 GDI_ReleaseObj( hdc );
731 GDI_ReleaseObj( hbitmap );
733 return lines;
737 /***********************************************************************
738 * CreateDIBitmap (GDI32.@)
740 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
741 DWORD init, LPCVOID bits, const BITMAPINFO *data,
742 UINT coloruse )
744 HBITMAP handle;
745 BOOL fColor;
746 DWORD width;
747 int height;
748 WORD bpp;
749 WORD compr;
750 DC *dc;
752 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
753 if (height < 0) height = -height;
755 /* Check if we should create a monochrome or color bitmap. */
756 /* We create a monochrome bitmap only if it has exactly 2 */
757 /* colors, which are black followed by white, nothing else. */
758 /* In all other cases, we create a color bitmap. */
760 if (bpp != 1) fColor = TRUE;
761 else if ((coloruse != DIB_RGB_COLORS) || !data) fColor = FALSE;
762 else
764 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
766 RGBQUAD *rgb = data->bmiColors;
767 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
769 /* Check if the first color of the colormap is black */
770 if ((col == RGB(0,0,0)))
772 rgb++;
773 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
774 /* If the second color is white, create a monochrome bitmap */
775 fColor = (col != RGB(0xff,0xff,0xff));
777 /* Note : If the first color of the colormap is white
778 followed by black, we have to create a color bitmap.
779 If we don't the white will be displayed in black later on!*/
780 else fColor = TRUE;
782 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
784 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
785 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
786 if ((col == RGB(0,0,0)))
788 rgb++;
789 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
790 fColor = (col != RGB(0xff,0xff,0xff));
792 else fColor = TRUE;
794 else if (data->bmiHeader.biSize == sizeof(BITMAPV4HEADER))
795 { /* FIXME: correct ? */
796 RGBQUAD *rgb = data->bmiColors;
797 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
799 /* Check if the first color of the colormap is black */
800 if ((col == RGB(0,0,0)))
802 rgb++;
803 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
804 /* If the second color is white, create a monochrome bitmap */
805 fColor = (col != RGB(0xff,0xff,0xff));
807 /* Note : If the first color of the colormap is white
808 followed by black, we have to create a color bitmap.
809 If we don't the white will be displayed in black later on!*/
810 else fColor = TRUE;
812 else
814 ERR("(%ld): wrong/unknown size for data\n",
815 data->bmiHeader.biSize );
816 return 0;
820 /* Now create the bitmap */
822 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
824 if (fColor)
825 handle = CreateBitmap( width, height, GetDeviceCaps( hdc, PLANES ),
826 GetDeviceCaps( hdc, BITSPIXEL ), NULL );
827 else handle = CreateBitmap( width, height, 1, 1, NULL );
829 if (handle)
831 if (init == CBM_INIT) SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
832 else if (!BITMAP_SetOwnerDC( handle, dc ))
834 DeleteObject( handle );
835 handle = 0;
839 GDI_ReleaseObj( hdc );
840 return handle;
843 /***********************************************************************
844 * CreateDIBSection (GDI.489)
846 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
847 SEGPTR *bits16, HANDLE section, DWORD offset)
849 LPVOID bits32;
850 HBITMAP hbitmap;
852 hbitmap = CreateDIBSection( HDC_32(hdc), bmi, usage, &bits32, section, offset );
853 if (hbitmap)
855 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(hbitmap, BITMAP_MAGIC);
856 if (bmp && bmp->dib && bits32)
858 BITMAPINFOHEADER *bi = &bmi->bmiHeader;
859 INT height = bi->biHeight >= 0 ? bi->biHeight : -bi->biHeight;
860 INT width_bytes = DIB_GetDIBWidthBytes(bi->biWidth, bi->biBitCount);
861 INT size = (bi->biSizeImage && bi->biCompression != BI_RGB) ?
862 bi->biSizeImage : width_bytes * height;
864 /* calculate number of sel's needed for size with 64K steps */
865 WORD count = (size + 0xffff) / 0x10000;
866 WORD sel = AllocSelectorArray16(count);
867 int i;
869 for (i = 0; i < count; i++)
871 SetSelectorBase(sel + (i << __AHSHIFT), (DWORD)bits32 + i * 0x10000);
872 SetSelectorLimit16(sel + (i << __AHSHIFT), size - 1); /* yep, limit is correct */
873 size -= 0x10000;
875 bmp->segptr_bits = MAKESEGPTR( sel, 0 );
876 if (bits16) *bits16 = bmp->segptr_bits;
878 if (bmp) GDI_ReleaseObj( hbitmap );
880 return HBITMAP_16(hbitmap);
883 /***********************************************************************
884 * DIB_CreateDIBSection
886 HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
887 LPVOID *bits, HANDLE section,
888 DWORD offset, DWORD ovr_pitch)
890 HBITMAP hbitmap = 0;
891 DC *dc;
892 BOOL bDesktopDC = FALSE;
894 /* If the reference hdc is null, take the desktop dc */
895 if (hdc == 0)
897 hdc = CreateCompatibleDC(0);
898 bDesktopDC = TRUE;
901 /* Windows ignores the supplied values of biClrUsed and biClrImportant thus: */
902 if (bmi->bmiHeader.biBitCount >= 1 && bmi->bmiHeader.biBitCount <= 8)
903 bmi->bmiHeader.biClrUsed = bmi->bmiHeader.biClrImportant = 1L << bmi->bmiHeader.biBitCount;
904 else
905 bmi->bmiHeader.biClrUsed = bmi->bmiHeader.biClrImportant = 0;
907 if ((dc = DC_GetDCPtr( hdc )))
909 if(dc->funcs->pCreateDIBSection)
910 hbitmap = dc->funcs->pCreateDIBSection(dc->physDev, bmi, usage, bits, section, offset, ovr_pitch);
911 GDI_ReleaseObj(hdc);
914 if (bDesktopDC)
915 DeleteDC(hdc);
917 return hbitmap;
920 /***********************************************************************
921 * CreateDIBSection (GDI32.@)
923 HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
924 LPVOID *bits, HANDLE section,
925 DWORD offset)
927 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);