2 * GDI device-independent bitmaps
4 * Copyright 1993,1994 Alexandre Julliard
6 * TODO: Still contains some references to X11DRV.
18 #include "selectors.h"
21 #include "xmalloc.h" /* for XCREATEIMAGE macro */
25 /***********************************************************************
26 * DIB_GetDIBWidthBytes
28 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
29 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
31 int DIB_GetDIBWidthBytes( int width, int depth )
37 case 1: words = (width + 31) / 32; break;
38 case 4: words = (width + 7) / 8; break;
39 case 8: words = (width + 3) / 4; break;
41 case 16: words = (width + 1) / 2; break;
42 case 24: words = (width * 3 + 3)/4; break;
45 WARN(bitmap, "(%d): Unsupported depth\n", depth );
54 /***********************************************************************
57 * Return the size of the bitmap info structure including color table.
59 int DIB_BitmapInfoSize( BITMAPINFO * info, WORD coloruse )
63 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
65 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
66 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
67 return sizeof(BITMAPCOREHEADER) + colors *
68 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
70 else /* assume BITMAPINFOHEADER */
72 colors = info->bmiHeader.biClrUsed;
73 if (!colors && (info->bmiHeader.biBitCount <= 8))
74 colors = 1 << info->bmiHeader.biBitCount;
75 return sizeof(BITMAPINFOHEADER) + colors *
76 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
81 /***********************************************************************
84 * Get the info from a bitmap header.
85 * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
87 int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
88 int *height, WORD *bpp, WORD *compr )
90 if (header->biSize == sizeof(BITMAPINFOHEADER))
92 *width = header->biWidth;
93 *height = header->biHeight;
94 *bpp = header->biBitCount;
95 *compr = header->biCompression;
98 if (header->biSize == sizeof(BITMAPCOREHEADER))
100 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
101 *width = core->bcWidth;
102 *height = core->bcHeight;
103 *bpp = core->bcBitCount;
107 WARN(bitmap, "(%ld): wrong size for header\n", header->biSize );
112 /***********************************************************************
113 * StretchDIBits16 (GDI.439)
115 INT16 WINAPI StretchDIBits16(HDC16 hdc, INT16 xDst, INT16 yDst, INT16 widthDst,
116 INT16 heightDst, INT16 xSrc, INT16 ySrc, INT16 widthSrc,
117 INT16 heightSrc, const VOID *bits,
118 const BITMAPINFO *info, UINT16 wUsage, DWORD dwRop )
120 return (INT16)StretchDIBits( hdc, xDst, yDst, widthDst, heightDst,
121 xSrc, ySrc, widthSrc, heightSrc, bits,
122 info, wUsage, dwRop );
126 /***********************************************************************
127 * StretchDIBits32 (GDI32.351)
129 INT WINAPI StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
130 INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
131 INT heightSrc, const void *bits,
132 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
134 DC *dc = DC_GetDCPtr( hdc );
135 if(!dc) return FALSE;
137 if(dc->funcs->pStretchDIBits)
138 return dc->funcs->pStretchDIBits(dc, xDst, yDst, widthDst,
139 heightDst, xSrc, ySrc, widthSrc,
140 heightSrc, bits, info, wUsage,
142 else { /* use StretchBlt32 */
143 HBITMAP hBitmap, hOldBitmap;
146 hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
147 bits, info, wUsage );
148 hdcMem = CreateCompatibleDC( hdc );
149 hOldBitmap = SelectObject( hdcMem, hBitmap );
150 /* Origin for DIBitmap is bottom left ! */
151 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
152 hdcMem, xSrc, info->bmiHeader.biHeight - heightSrc - ySrc,
153 widthSrc, heightSrc, dwRop );
154 SelectObject( hdcMem, hOldBitmap );
156 DeleteObject( hBitmap );
162 /***********************************************************************
163 * SetDIBits16 (GDI.440)
165 INT16 WINAPI SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
166 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
169 return SetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
173 /******************************************************************************
174 * SetDIBits32 [GDI32.312] Sets pixels in a bitmap using colors from DIB
177 * hdc [I] Handle to device context
178 * hbitmap [I] Handle to bitmap
179 * startscan [I] Starting scan line
180 * lines [I] Number of scan lines
181 * bits [I] Array of bitmap bits
182 * info [I] Address of structure with data
183 * coloruse [I] Type of color indexes to use
186 * Success: Number of scan lines copied
189 INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
190 UINT lines, LPCVOID bits, const BITMAPINFO *info,
193 DIB_SETIMAGEBITS_DESCR descr;
195 int height, tmpheight;
198 /* Check parameters */
200 descr.dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
203 descr.dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
204 if (!descr.dc) return 0;
206 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
208 GDI_HEAP_UNLOCK( hdc );
211 if (DIB_GetBitmapInfo( &info->bmiHeader, &descr.infoWidth, &height,
212 &descr.infoBpp, &descr.compression ) == -1)
214 GDI_HEAP_UNLOCK( hbitmap );
215 GDI_HEAP_UNLOCK( hdc );
219 if (height < 0) height = -height;
220 if (!lines || (startscan >= height))
222 GDI_HEAP_UNLOCK( hbitmap );
223 GDI_HEAP_UNLOCK( hdc );
226 if (startscan + lines > height) lines = height - startscan;
228 if (descr.infoBpp <= 8)
230 descr.colorMap = X11DRV_DIB_BuildColorMap( descr.dc, coloruse,
231 bmp->bitmap.bmBitsPixel,
232 info, &descr.nColorMap );
235 GDI_HEAP_UNLOCK( hbitmap );
236 GDI_HEAP_UNLOCK( hdc );
244 X11DRV_CreateBitmap(hbitmap);
246 X11DRV_PHYSBITMAP *pbitmap = bmp->DDBitmap->physBitmap;
251 descr.lines = tmpheight >= 0 ? lines : -lines;
252 descr.depth = bmp->bitmap.bmBitsPixel;
253 descr.drawable = pbitmap->pixmap;
254 descr.gc = BITMAP_GC(bmp);
258 descr.yDest = height - startscan - lines;
259 descr.width = bmp->bitmap.bmWidth;
260 descr.height = lines;
262 EnterCriticalSection( &X11DRV_CritSection );
263 result = CALL_LARGE_STACK( X11DRV_DIB_SetImageBits, &descr );
264 LeaveCriticalSection( &X11DRV_CritSection );
266 if (descr.colorMap) HeapFree(GetProcessHeap(), 0, descr.colorMap);
268 GDI_HEAP_UNLOCK( hdc );
269 GDI_HEAP_UNLOCK( hbitmap );
274 /***********************************************************************
275 * SetDIBitsToDevice16 (GDI.443)
277 INT16 WINAPI SetDIBitsToDevice16(HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
278 INT16 cy, INT16 xSrc, INT16 ySrc, UINT16 startscan,
279 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
282 return SetDIBitsToDevice( hdc, xDest, yDest, cx, cy, xSrc, ySrc,
283 startscan, lines, bits, info, coloruse );
287 /***********************************************************************
288 * SetDIBitsToDevice32 (GDI32.313)
290 INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
291 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
292 UINT lines, LPCVOID bits, const BITMAPINFO *info,
298 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
300 if(dc->funcs->pSetDIBitsToDevice)
301 ret = dc->funcs->pSetDIBitsToDevice( dc, xDest, yDest, cx, cy, xSrc,
302 ySrc, startscan, lines, bits,
305 FIXME(bitmap, "unimplemented on hdc %08x\n", hdc);
309 GDI_HEAP_UNLOCK( hdc );
313 /***********************************************************************
314 * SetDIBColorTable16 (GDI.602)
316 UINT16 WINAPI SetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
319 return SetDIBColorTable( hdc, startpos, entries, colors );
322 /***********************************************************************
323 * SetDIBColorTable32 (GDI32.311)
325 UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
329 PALETTEENTRY * palEntry;
330 PALETTEOBJ * palette;
333 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
336 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
340 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
345 /* Transfer color info */
347 if (dc->w.bitsPerPixel <= 8) {
348 palEntry = palette->logpalette.palPalEntry + startpos;
349 if (startpos + entries > (1 << dc->w.bitsPerPixel)) {
350 entries = (1 << dc->w.bitsPerPixel) - startpos;
352 for (end = colors + entries; colors < end; palEntry++, colors++)
354 palEntry->peRed = colors->rgbRed;
355 palEntry->peGreen = colors->rgbGreen;
356 palEntry->peBlue = colors->rgbBlue;
361 GDI_HEAP_UNLOCK( dc->w.hPalette );
365 /***********************************************************************
366 * GetDIBColorTable16 (GDI.603)
368 UINT16 WINAPI GetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
371 return GetDIBColorTable( hdc, startpos, entries, colors );
374 /***********************************************************************
375 * GetDIBColorTable32 (GDI32.169)
377 UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries,
381 PALETTEENTRY * palEntry;
382 PALETTEOBJ * palette;
385 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
388 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
392 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
397 /* Transfer color info */
399 if (dc->w.bitsPerPixel <= 8) {
400 palEntry = palette->logpalette.palPalEntry + startpos;
401 if (startpos + entries > (1 << dc->w.bitsPerPixel)) {
402 entries = (1 << dc->w.bitsPerPixel) - startpos;
404 for (end = colors + entries; colors < end; palEntry++, colors++)
406 colors->rgbRed = palEntry->peRed;
407 colors->rgbGreen = palEntry->peGreen;
408 colors->rgbBlue = palEntry->peBlue;
409 colors->rgbReserved = 0;
414 GDI_HEAP_UNLOCK( dc->w.hPalette );
418 /***********************************************************************
419 * GetDIBits16 (GDI.441)
421 INT16 WINAPI GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
422 UINT16 lines, LPVOID bits, BITMAPINFO * info,
425 return GetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
429 /******************************************************************************
430 * GetDIBits32 [GDI32.170] Retrieves bits of bitmap and copies to buffer
433 * Success: Number of scan lines copied from bitmap
436 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
438 INT WINAPI GetDIBits(
439 HDC hdc, /* [in] Handle to device context */
440 HBITMAP hbitmap, /* [in] Handle to bitmap */
441 UINT startscan, /* [in] First scan line to set in dest bitmap */
442 UINT lines, /* [in] Number of scan lines to copy */
443 LPVOID bits, /* [out] Address of array for bitmap bits */
444 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
445 UINT coloruse) /* [in] RGB or palette index */
449 PALETTEENTRY * palEntry;
450 PALETTEOBJ * palette;
454 if (!lines) return 0;
455 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
458 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
461 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
463 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
465 GDI_HEAP_UNLOCK( hbitmap );
469 /* Transfer color info (FIXME) */
471 if (info && (info->bmiHeader.biBitCount <= 8) &&
472 (bmp->bitmap.bmBitsPixel <= 8))
474 int colors = 1 << info->bmiHeader.biBitCount;
475 info->bmiHeader.biClrUsed = 0;
476 palEntry = palette->logpalette.palPalEntry;
477 for (i = 0; i < colors; i++, palEntry++)
479 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;
492 BYTE *bbits = (BYTE*)bits, *linestart;
493 int dstwidth, yend, xend = bmp->bitmap.bmWidth;
495 TRACE(bitmap, "%u scanlines of (%i,%i) -> (%i,%i) starting from %u\n",
496 lines, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
497 (int)info->bmiHeader.biWidth, (int)info->bmiHeader.biHeight,
500 /* adjust number of scanlines to copy */
502 if( lines > info->bmiHeader.biHeight ) lines = info->bmiHeader.biHeight;
503 yend = startscan + lines;
504 if( startscan >= bmp->bitmap.bmHeight )
506 GDI_HEAP_UNLOCK( hbitmap );
507 GDI_HEAP_UNLOCK( dc->w.hPalette );
510 if( yend > bmp->bitmap.bmHeight ) yend = bmp->bitmap.bmHeight;
512 /* adjust scanline width */
514 if(bmp->bitmap.bmWidth > info->bmiHeader.biWidth)
515 xend = info->bmiHeader.biWidth;
519 X11DRV_CreateBitmap(hbitmap);
521 dstwidth = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth,
522 info->bmiHeader.biBitCount );
524 EnterCriticalSection( &X11DRV_CritSection );
525 bmpImage = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
528 switch( info->bmiHeader.biBitCount )
531 for( y = yend - 1; (int)y >= (int)startscan; y-- )
533 for( x = 0; x < xend; x++ )
534 *bbits++ = XGetPixel( bmpImage, x, y );
535 bbits = (linestart += dstwidth);
539 for( y = yend - 1; (int)y >= (int)startscan; y-- )
541 for( x = 0; x < xend; x++ ) {
542 if (!(x&7)) *bbits = 0;
543 *bbits |= XGetPixel( bmpImage, x, y)<<(7-(x&7));
544 if ((x&7)==7) bbits++;
546 bbits = (linestart += dstwidth);
550 for( y = yend - 1; (int)y >= (int)startscan; y-- )
552 for( x = 0; x < xend; x++ ) {
553 if (!(x&1)) *bbits = 0;
554 *bbits |= XGetPixel( bmpImage, x, y)<<(4*(1-(x&1)));
555 if ((x&1)==1) bbits++;
557 bbits = (linestart += dstwidth);
562 for( y = yend - 1; (int)y >= (int)startscan; y-- )
564 for( x = 0; x < xend; x++ ) {
565 unsigned long pixel=XGetPixel( bmpImage, x, y);
566 *bbits++ = pixel & 0xff;
567 *bbits++ = (pixel >> 8) & 0xff;
569 bbits = (linestart += dstwidth);
573 for( y = yend - 1; (int)y >= (int)startscan; y-- )
575 for( x = 0; x < xend; x++ ) {
576 unsigned long pixel=XGetPixel( bmpImage, x, y);
577 *bbits++ = (pixel >>16) & 0xff;
578 *bbits++ = (pixel >> 8) & 0xff;
579 *bbits++ = pixel & 0xff;
581 bbits = (linestart += dstwidth);
585 for( y = yend - 1; (int)y >= (int)startscan; y-- )
587 for( x = 0; x < xend; x++ ) {
588 unsigned long pixel=XGetPixel( bmpImage, x, y);
589 *bbits++ = (pixel >>16) & 0xff;
590 *bbits++ = (pixel >> 8) & 0xff;
591 *bbits++ = pixel & 0xff;
593 bbits = (linestart += dstwidth);
597 WARN(bitmap,"Unsupported depth %d\n",
598 info->bmiHeader.biBitCount);
602 XDestroyImage( bmpImage );
603 LeaveCriticalSection( &X11DRV_CritSection );
605 if(bbits - (BYTE *)bits > info->bmiHeader.biSizeImage)
606 ERR(bitmap, "Buffer overrun. Please investigate.\n");
608 info->bmiHeader.biCompression = 0;
610 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
612 /* fill in struct members */
614 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
615 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
616 info->bmiHeader.biPlanes = 1;
617 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
618 info->bmiHeader.biSizeImage = bmp->bitmap.bmHeight *
619 DIB_GetDIBWidthBytes( bmp->bitmap.bmWidth,
620 bmp->bitmap.bmBitsPixel );
621 info->bmiHeader.biCompression = 0;
623 TRACE(bitmap, "biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
624 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
625 info->bmiHeader.biHeight);
626 GDI_HEAP_UNLOCK( hbitmap );
627 GDI_HEAP_UNLOCK( dc->w.hPalette );
632 /***********************************************************************
633 * CreateDIBitmap16 (GDI.442)
635 HBITMAP16 WINAPI CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
636 DWORD init, LPCVOID bits, const BITMAPINFO * data,
639 return CreateDIBitmap( hdc, header, init, bits, data, coloruse );
643 /***********************************************************************
644 * CreateDIBitmap32 (GDI32.37)
646 HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
647 DWORD init, LPCVOID bits, const BITMAPINFO *data,
657 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
658 if (height < 0) height = -height;
660 /* Check if we should create a monochrome or color bitmap. */
661 /* We create a monochrome bitmap only if it has exactly 2 */
662 /* colors, which are either black or white, nothing else. */
663 /* In all other cases, we create a color bitmap. */
665 if (bpp != 1) fColor = TRUE;
666 else if ((coloruse != DIB_RGB_COLORS) ||
667 (init != CBM_INIT) || !data) fColor = FALSE;
670 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
672 RGBQUAD *rgb = data->bmiColors;
673 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
674 if ((col == RGB(0,0,0)) || (col == RGB(0xff,0xff,0xff)))
677 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
678 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
682 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
684 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
685 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
686 if ((col == RGB(0,0,0)) || (col == RGB(0xff,0xff,0xff)))
689 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
690 fColor = ((col != RGB(0,0,0)) && (col != RGB(0xff,0xff,0xff)));
696 WARN(bitmap, "(%ld): wrong size for data\n",
697 data->bmiHeader.biSize );
702 /* Now create the bitmap */
704 handle = fColor ? CreateBitmap( width, height, 1, MONITOR_GetDepth(&MONITOR_PrimaryMonitor), NULL ) :
705 CreateBitmap( width, height, 1, 1, NULL );
706 if (!handle) return 0;
708 if (init == CBM_INIT)
709 SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
714 /***********************************************************************
715 * DIB_DoProtectDIBSection
717 static void DIB_DoProtectDIBSection( BITMAPOBJ *bmp, DWORD new_prot )
719 DIBSECTION *dib = &bmp->dib->dibSection;
720 INT effHeight = dib->dsBm.bmHeight >= 0? dib->dsBm.bmHeight
721 : -dib->dsBm.bmHeight;
722 INT totalSize = dib->dsBmih.biSizeImage? dib->dsBmih.biSizeImage
723 : dib->dsBm.bmWidthBytes * effHeight;
726 VirtualProtect(dib->dsBm.bmBits, totalSize, new_prot, &old_prot);
727 TRACE(bitmap, "Changed protection from %ld to %ld\n",
731 /***********************************************************************
732 * DIB_DoUpdateDIBSection
734 static void DIB_DoUpdateDIBSection( BITMAPOBJ *bmp, BOOL toDIB )
736 DIBSECTIONOBJ *dib = bmp->dib;
737 DIB_SETIMAGEBITS_DESCR descr;
739 if (DIB_GetBitmapInfo( &dib->dibSection.dsBmih, &descr.infoWidth, &descr.lines,
740 &descr.infoBpp, &descr.compression ) == -1)
744 descr.image = dib->image;
745 descr.colorMap = dib->colorMap;
746 descr.nColorMap = dib->nColorMap;
747 descr.bits = dib->dibSection.dsBm.bmBits;
748 descr.depth = bmp->bitmap.bmBitsPixel;
751 descr.drawable = ((X11DRV_PHYSBITMAP *)bmp->DDBitmap->physBitmap)->pixmap;
752 descr.gc = BITMAP_GC(bmp);
757 descr.width = bmp->bitmap.bmWidth;
758 descr.height = bmp->bitmap.bmHeight;
762 TRACE(bitmap, "Copying from Pixmap to DIB bits\n");
763 EnterCriticalSection( &X11DRV_CritSection );
764 CALL_LARGE_STACK( X11DRV_DIB_GetImageBits, &descr );
765 LeaveCriticalSection( &X11DRV_CritSection );
769 TRACE(bitmap, "Copying from DIB bits to Pixmap\n");
770 EnterCriticalSection( &X11DRV_CritSection );
771 CALL_LARGE_STACK( X11DRV_DIB_SetImageBits, &descr );
772 LeaveCriticalSection( &X11DRV_CritSection );
776 /***********************************************************************
779 static BOOL DIB_FaultHandler( LPVOID res, LPCVOID addr )
781 BOOL handled = FALSE;
784 bmp = (BITMAPOBJ *)GDI_GetObjPtr( (HBITMAP)res, BITMAP_MAGIC );
785 if (!bmp) return FALSE;
788 switch (bmp->dib->status)
791 TRACE( bitmap, "called in status DIB_GdiMod\n" );
792 DIB_DoProtectDIBSection( bmp, PAGE_READWRITE );
793 DIB_DoUpdateDIBSection( bmp, TRUE );
794 DIB_DoProtectDIBSection( bmp, PAGE_READONLY );
795 bmp->dib->status = DIB_InSync;
800 TRACE( bitmap, "called in status DIB_InSync\n" );
801 DIB_DoProtectDIBSection( bmp, PAGE_READWRITE );
802 bmp->dib->status = DIB_AppMod;
807 FIXME( bitmap, "called in status DIB_AppMod: "
808 "this can't happen!\n" );
812 FIXME( bitmap, "called in status DIB_NoHandler: "
813 "this can't happen!\n" );
817 GDI_HEAP_UNLOCK( (HBITMAP)res );
821 /***********************************************************************
822 * DIB_UpdateDIBSection
824 void DIB_UpdateDIBSection( DC *dc, BOOL toDIB )
828 /* Ensure this is a Compatible DC that has a DIB section selected */
831 if (!(dc->w.flags & DC_MEMORY)) return;
833 bmp = (BITMAPOBJ *)GDI_GetObjPtr( dc->w.hBitmap, BITMAP_MAGIC );
838 GDI_HEAP_UNLOCK(dc->w.hBitmap);
845 /* Prepare for access to the DIB by GDI functions */
847 switch (bmp->dib->status)
851 DIB_DoUpdateDIBSection( bmp, FALSE );
855 TRACE( bitmap, "fromDIB called in status DIB_GdiMod\n" );
860 TRACE( bitmap, "fromDIB called in status DIB_InSync\n" );
865 TRACE( bitmap, "fromDIB called in status DIB_AppMod\n" );
866 DIB_DoUpdateDIBSection( bmp, FALSE );
867 DIB_DoProtectDIBSection( bmp, PAGE_READONLY );
868 bmp->dib->status = DIB_InSync;
874 /* Acknowledge write access to the DIB by GDI functions */
876 switch (bmp->dib->status)
880 DIB_DoUpdateDIBSection( bmp, TRUE );
884 TRACE( bitmap, " toDIB called in status DIB_GdiMod\n" );
889 TRACE( bitmap, " toDIB called in status DIB_InSync\n" );
890 DIB_DoProtectDIBSection( bmp, PAGE_NOACCESS );
891 bmp->dib->status = DIB_GdiMod;
895 FIXME( bitmap, " toDIB called in status DIB_AppMod: "
896 "this can't happen!\n" );
902 GDI_HEAP_UNLOCK(dc->w.hBitmap);
905 /***********************************************************************
906 * CreateDIBSection16 (GDI.489)
908 HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
909 SEGPTR *bits, HANDLE section,
912 HBITMAP res = CreateDIBSection(hdc, bmi, usage, NULL, section,
917 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(res, BITMAP_MAGIC);
918 if ( bmp && bmp->dib )
920 DIBSECTION *dib = &bmp->dib->dibSection;
921 INT height = dib->dsBm.bmHeight >= 0 ?
922 dib->dsBm.bmHeight : -dib->dsBm.bmHeight;
923 INT size = dib->dsBmih.biSizeImage ?
924 dib->dsBmih.biSizeImage : dib->dsBm.bmWidthBytes * height;
925 if ( dib->dsBm.bmBits )
928 SELECTOR_AllocBlock( dib->dsBm.bmBits, size,
929 SEGMENT_DATA, FALSE, FALSE );
931 printf("ptr = %p, size =%d, selector = %04x, segptr = %ld\n",
932 dib->dsBm.bmBits, size, bmp->dib->selector,
933 PTR_SEG_OFF_TO_SEGPTR(bmp->dib->selector, 0));
935 GDI_HEAP_UNLOCK( res );
938 *bits = PTR_SEG_OFF_TO_SEGPTR( bmp->dib->selector, 0 );
944 /***********************************************************************
945 * CreateDIBSection32 (GDI32.36)
947 HBITMAP WINAPI CreateDIBSection (HDC hdc, BITMAPINFO *bmi, UINT usage,
948 LPVOID *bits,HANDLE section,
952 BITMAPOBJ *bmp = NULL;
953 DIBSECTIONOBJ *dib = NULL;
954 int *colorMap = NULL;
957 /* Fill BITMAP32 structure with DIB data */
958 BITMAPINFOHEADER *bi = &bmi->bmiHeader;
959 INT effHeight, totalSize;
962 TRACE(bitmap, "format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
963 bi->biWidth, bi->biHeight, bi->biPlanes, bi->biBitCount,
964 bi->biSizeImage, bi->biClrUsed, usage == DIB_PAL_COLORS? "PAL" : "RGB");
967 bm.bmWidth = bi->biWidth;
968 bm.bmHeight = bi->biHeight;
969 bm.bmWidthBytes = DIB_GetDIBWidthBytes(bm.bmWidth, bi->biBitCount);
970 bm.bmPlanes = bi->biPlanes;
971 bm.bmBitsPixel = bi->biBitCount;
974 /* Get storage location for DIB bits */
975 effHeight = bm.bmHeight >= 0 ? bm.bmHeight : -bm.bmHeight;
976 totalSize = bi->biSizeImage? bi->biSizeImage : bm.bmWidthBytes * effHeight;
979 bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
980 0L, offset, totalSize);
982 bm.bmBits = VirtualAlloc(NULL, totalSize,
983 MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
985 /* Create Color Map */
986 if (bm.bmBits && bm.bmBitsPixel <= 8)
988 DC *dc = hdc? (DC *)GDI_GetObjPtr(hdc, DC_MAGIC) : NULL;
989 if (hdc && !dc) dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
992 colorMap = X11DRV_DIB_BuildColorMap( dc, usage, bm.bmBitsPixel,
994 GDI_HEAP_UNLOCK(hdc);
997 /* Allocate Memory for DIB and fill structure */
999 dib = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DIBSECTIONOBJ));
1002 dib->dibSection.dsBm = bm;
1003 dib->dibSection.dsBmih = *bi;
1004 /* FIXME: dib->dibSection.dsBitfields ??? */
1005 dib->dibSection.dshSection = section;
1006 dib->dibSection.dsOffset = offset;
1008 dib->status = DIB_NoHandler;
1011 dib->nColorMap = nColorMap;
1012 dib->colorMap = colorMap;
1015 /* Create Device Dependent Bitmap and add DIB pointer */
1018 res = CreateDIBitmap(hdc, bi, 0, NULL, bmi, usage);
1021 bmp = (BITMAPOBJ *) GDI_GetObjPtr(res, BITMAP_MAGIC);
1027 X11DRV_CreateBitmap(res);
1034 XCREATEIMAGE( dib->image, bm.bmWidth, effHeight, bmp->bitmap.bmBitsPixel );
1036 /* Clean up in case of errors */
1037 if (!res || !bmp || !dib || !bm.bmBits || (bm.bmBitsPixel <= 8 && !colorMap))
1039 TRACE(bitmap, "got an error res=%08x, bmp=%p, dib=%p, bm.bmBits=%p\n",
1040 res, bmp, dib, bm.bmBits);
1044 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
1046 VirtualFree(bm.bmBits, MEM_RELEASE, 0L), bm.bmBits = NULL;
1049 if (dib && dib->image) { XDestroyImage(dib->image); dib->image = NULL; }
1050 if (colorMap) { HeapFree(GetProcessHeap(), 0, colorMap); colorMap = NULL; }
1051 if (dib) { HeapFree(GetProcessHeap(), 0, dib); dib = NULL; }
1052 if (res) { DeleteObject(res); res = 0; }
1055 /* Install fault handler, if possible */
1058 if (VIRTUAL_SetFaultHandler(bm.bmBits, DIB_FaultHandler, (LPVOID)res))
1060 DIB_DoProtectDIBSection( bmp, PAGE_READONLY );
1061 if (dib) dib->status = DIB_InSync;
1065 /* Return BITMAP handle and storage location */
1066 if (res) GDI_HEAP_UNLOCK(res);
1067 if (bm.bmBits && bits) *bits = bm.bmBits;
1071 /***********************************************************************
1072 * DIB_DeleteDIBSection
1074 void DIB_DeleteDIBSection( BITMAPOBJ *bmp )
1076 if (bmp && bmp->dib)
1078 DIBSECTIONOBJ *dib = bmp->dib;
1080 if (dib->dibSection.dsBm.bmBits)
1082 if (dib->dibSection.dshSection)
1083 UnmapViewOfFile(dib->dibSection.dsBm.bmBits);
1085 VirtualFree(dib->dibSection.dsBm.bmBits, MEM_RELEASE, 0L);
1089 XDestroyImage( dib->image );
1092 HeapFree(GetProcessHeap(), 0, dib->colorMap);
1096 WORD count = (GET_SEL_LIMIT( dib->selector ) >> 16) + 1;
1097 SELECTOR_FreeBlock( dib->selector, count );
1100 HeapFree(GetProcessHeap(), 0, dib);
1105 /***********************************************************************
1106 * DIB_FixColorsToLoadflags
1108 * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
1111 void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
1114 COLORREF c_W, c_S, c_F, c_L, c_C;
1118 if (bmi->bmiHeader.biBitCount > 8) return;
1119 if (bmi->bmiHeader.biSize == sizeof(BITMAPINFOHEADER)) incr = 4;
1120 else if (bmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) incr = 3;
1122 WARN(bitmap, "Wrong bitmap header size!\n");
1125 colors = bmi->bmiHeader.biClrUsed;
1126 if (!colors && (bmi->bmiHeader.biBitCount <= 8))
1127 colors = 1 << bmi->bmiHeader.biBitCount;
1128 c_W = GetSysColor(COLOR_WINDOW);
1129 c_S = GetSysColor(COLOR_3DSHADOW);
1130 c_F = GetSysColor(COLOR_3DFACE);
1131 c_L = GetSysColor(COLOR_3DLIGHT);
1132 if (loadflags & LR_LOADTRANSPARENT) {
1133 switch (bmi->bmiHeader.biBitCount) {
1134 case 1: pix = pix >> 7; break;
1135 case 4: pix = pix >> 4; break;
1138 WARN(bitmap, "(%d): Unsupported depth\n", bmi->bmiHeader.biBitCount);
1141 if (pix >= colors) {
1142 WARN(bitmap, "pixel has color index greater than biClrUsed!\n");
1145 if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
1146 ptr = (RGBQUAD*)((char*)bmi->bmiColors+pix*incr);
1147 ptr->rgbBlue = GetBValue(c_W);
1148 ptr->rgbGreen = GetGValue(c_W);
1149 ptr->rgbRed = GetRValue(c_W);
1151 if (loadflags & LR_LOADMAP3DCOLORS)
1152 for (i=0; i<colors; i++) {
1153 ptr = (RGBQUAD*)((char*)bmi->bmiColors+i*incr);
1154 c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
1155 if (c_C == RGB(128, 128, 128)) {
1156 ptr->rgbRed = GetRValue(c_S);
1157 ptr->rgbGreen = GetGValue(c_S);
1158 ptr->rgbBlue = GetBValue(c_S);
1159 } else if (c_C == RGB(192, 192, 192)) {
1160 ptr->rgbRed = GetRValue(c_F);
1161 ptr->rgbGreen = GetGValue(c_F);
1162 ptr->rgbBlue = GetBValue(c_F);
1163 } else if (c_C == RGB(223, 223, 223)) {
1164 ptr->rgbRed = GetRValue(c_L);
1165 ptr->rgbGreen = GetGValue(c_L);
1166 ptr->rgbBlue = GetBValue(c_L);