1 #undef WIN32_LEAN_AND_MEAN
4 #include <win32k/bitmaps.h>
5 #include <win32k/debug.h>
7 #include "../eng/handle.h"
8 #include <ntos/minmax.h>
10 VOID
BitmapToSurf(HDC hdc
, PSURFGDI SurfGDI
, PSURFOBJ SurfObj
, PBITMAPOBJ Bitmap
);
12 UINT STDCALL
W32kSetDIBColorTable(HDC hDC
,
15 CONST RGBQUAD
*Colors
)
18 PALETTEENTRY
* palEntry
;
22 if (!(dc
= (PDC
)AccessUserObject(hDC
))) return 0;
24 if (!(palette
= (PPALOBJ
)AccessUserObject(dc
->DevInfo
.hpalDefault
)))
26 // GDI_ReleaseObj( hdc );
30 // Transfer color info
32 if (dc
->w
.bitsPerPixel
<= 8) {
33 palEntry
= palette
->logpalette
->palPalEntry
+ StartIndex
;
34 if (StartIndex
+ Entries
> (1 << dc
->w
.bitsPerPixel
))
35 Entries
= (1 << dc
->w
.bitsPerPixel
) - StartIndex
;
37 if (StartIndex
+ Entries
> palette
->logpalette
->palNumEntries
)
38 Entries
= palette
->logpalette
->palNumEntries
- StartIndex
;
40 for (end
= Colors
+ Entries
; Colors
< end
; palEntry
++, Colors
++)
42 palEntry
->peRed
= Colors
->rgbRed
;
43 palEntry
->peGreen
= Colors
->rgbGreen
;
44 palEntry
->peBlue
= Colors
->rgbBlue
;
50 // GDI_ReleaseObj(dc->DevInfo.hpalDefault);
51 // GDI_ReleaseObj(hdc);
56 // Converts a DIB to a device-dependent bitmap
57 INT STDCALL
W32kSetDIBits(HDC hDC
,
62 CONST BITMAPINFO
*bmi
,
67 HBITMAP SourceBitmap
, DestBitmap
;
70 PSURFOBJ DestSurf
, SourceSurf
;
78 HPALETTE DDB_Palette
, DIB_Palette
;
79 ULONG DDB_Palette_Type
, DIB_Palette_Type
;
83 if (!(dc
= DC_HandleToPtr(hDC
)))
86 if (!(bitmap
= (BITMAPOBJ
*)GDIOBJ_LockObj(hBitmap
, GO_BITMAP_MAGIC
)))
93 if (ColorUse
== DIB_PAL_COLORS
)
94 lpRGB
= DIB_MapPaletteColors(hDC
, bmi
);
96 lpRGB
= &bmi
->bmiColors
[0];
98 // Create a temporary surface for the destination bitmap
99 DestBitmap
= (HBITMAP
)CreateGDIHandle(sizeof(SURFGDI
), sizeof(SURFOBJ
));
101 DestSurf
= (PSURFOBJ
) AccessUserObject( DestBitmap
);
102 DestGDI
= (PSURFGDI
) AccessInternalObject( DestBitmap
);
104 BitmapToSurf(hDC
, DestGDI
, DestSurf
, bitmap
);
106 // Create source surface
107 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
108 SourceSize
.cy
= bmi
->bmiHeader
.biHeight
;
109 SourceBitmap
= EngCreateBitmap(SourceSize
, DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
),
110 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
112 SourceSurf
= (PSURFOBJ
)AccessUserObject(SourceBitmap
);
114 // Destination palette obtained from the hDC
115 hDCPalette
= (PPALGDI
)AccessInternalObject(dc
->DevInfo
.hpalDefault
);
116 DDB_Palette_Type
= hDCPalette
->Mode
;
117 DDB_Palette
= dc
->DevInfo
.hpalDefault
;
119 // Source palette obtained from the BITMAPINFO
120 DIB_Palette
= BuildDIBPalette(bmi
, &DIB_Palette_Type
);
122 // Determine XLATEOBJ for color translation
123 XlateObj
= EngCreateXlate(DDB_Palette_Type
, DIB_Palette_Type
, DDB_Palette
, DIB_Palette
);
125 // Determine destination rectangle and source point
130 DestRect
.right
= SourceSize
.cx
;
131 DestRect
.bottom
= SourceSize
.cy
;
133 copyBitsResult
= EngCopyBits(DestSurf
, SourceSurf
, NULL
, XlateObj
, &DestRect
, &ZeroPoint
);
136 EngDeleteSurface(SourceBitmap
);
137 EngDeleteSurface(DestBitmap
);
139 // if (ColorUse == DIB_PAL_COLORS)
140 // WinFree((LPSTR)lpRGB);
142 // GDI_ReleaseObj(hBitmap); unlock?
143 GDIOBJ_UnlockObj(hBitmap
, GO_BITMAP_MAGIC
);
149 INT STDCALL
W32kSetDIBitsToDevice(HDC hDC
,
159 CONST BITMAPINFO
*bmi
,
165 UINT STDCALL
W32kGetDIBColorTable(HDC hDC
,
173 // Converts a device-dependent bitmap to a DIB
174 INT STDCALL
W32kGetDIBits(HDC hDC
,
185 INT STDCALL
W32kStretchDIBits(HDC hDC
,
195 CONST BITMAPINFO
*BitsInfo
,
202 LONG STDCALL
W32kGetBitmapBits(HBITMAP hBitmap
,
209 bmp
= BITMAPOBJ_HandleToPtr (hBitmap
);
215 /* If the bits vector is null, the function should return the read size */
218 return bmp
->bitmap
.bmWidthBytes
* bmp
->bitmap
.bmHeight
;
223 DPRINT ("(%ld): Negative number of bytes passed???\n", Count
);
227 /* Only get entire lines */
228 height
= Count
/ bmp
->bitmap
.bmWidthBytes
;
229 if (height
> bmp
->bitmap
.bmHeight
)
231 height
= bmp
->bitmap
.bmHeight
;
233 Count
= height
* bmp
->bitmap
.bmWidthBytes
;
236 DPRINT("Less then one entire line requested\n");
240 DPRINT("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
241 hBitmap
, Count
, Bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
242 1 << bmp
->bitmap
.bmBitsPixel
, height
);
244 /* FIXME: Call DDI CopyBits here if available */
247 DPRINT("Calling device specific BitmapBits\n");
248 if(bmp
->DDBitmap
->funcs
->pBitmapBits
)
250 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hbitmap
, bits
, count
,
255 ERR_(bitmap
)("BitmapBits == NULL??\n");
262 if(!bmp
->bitmap
.bmBits
)
264 DPRINT ("Bitmap is empty\n");
269 memcpy(Bits
, bmp
->bitmap
.bmBits
, Count
);
277 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
278 // The DDB that is created will be whatever bit depth your reference DC is
279 HBITMAP STDCALL
W32kCreateDIBitmap(HDC hdc
, const BITMAPINFOHEADER
*header
,
280 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
290 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
, &compr
) == -1) return 0;
291 if (height
< 0) height
= -height
;
293 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
294 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
296 if (bpp
!= 1) fColor
= TRUE
;
297 else if ((coloruse
!= DIB_RGB_COLORS
) ||
298 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
301 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
303 RGBQUAD
*rgb
= data
->bmiColors
;
304 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
306 // Check if the first color of the colormap is black
307 if ((col
== RGB(0, 0, 0)))
310 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
312 // If the second color is white, create a monochrome bitmap
313 fColor
= (col
!= RGB(0xff,0xff,0xff));
317 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
319 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
320 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
322 if ((col
== RGB(0,0,0)))
325 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
326 fColor
= (col
!= RGB(0xff,0xff,0xff));
332 DbgPrint("(%ld): wrong size for data\n", data
->bmiHeader
.biSize
);
337 // Now create the bitmap
341 // If we are using indexed colors, then we need to create a bitmap that is compatible with the palette
342 if(coloruse
== DIB_PAL_COLORS
)
344 handle
= W32kCreateCompatibleBitmap(hdc
, width
, height
);
346 else if(coloruse
== DIB_RGB_COLORS
) {
347 handle
= W32kCreateBitmap(width
, height
, 1, 24, NULL
);
350 else handle
= W32kCreateBitmap(width
, height
, 1, 1, NULL
);
352 if (!handle
) return 0;
354 if (init
== CBM_INIT
)
356 W32kSetDIBits(hdc
, handle
, 0, height
, bits
, data
, coloruse
);
362 HBITMAP STDCALL
W32kCreateDIBSection(HDC hDC
,
363 CONST BITMAPINFO
*bmi
,
371 BOOL bDesktopDC
= FALSE
;
373 // If the reference hdc is null, take the desktop dc
376 hDC
= W32kCreateCompatableDC(0);
380 if ((dc
= DC_HandleToPtr(hDC
)))
382 hbitmap
= DIB_CreateDIBSection(dc
, bmi
, Usage
, Bits
, hSection
, dwOffset
, 0);
383 DC_ReleasePtr( hDC
);
392 HBITMAP
DIB_CreateDIBSection(
393 PDC dc
, BITMAPINFO
*bmi
, UINT usage
,
394 LPVOID
*bits
, HANDLE section
,
395 DWORD offset
, DWORD ovr_pitch
)
398 BITMAPOBJ
*bmp
= NULL
;
399 DIBSECTION
*dib
= NULL
;
400 int *colorMap
= NULL
;
403 // Fill BITMAP32 structure with DIB data
404 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
405 INT effHeight
, totalSize
;
408 DbgPrint("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
409 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
410 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
412 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
414 bm
.bmWidth
= bi
->biWidth
;
415 bm
.bmHeight
= effHeight
;
416 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
418 bm
.bmPlanes
= bi
->biPlanes
;
419 bm
.bmBitsPixel
= bi
->biBitCount
;
422 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
423 // we're dealing with a compressed bitmap. Otherwise, use width * height.
424 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
425 ? bi
->biSizeImage
: bm
.bmWidthBytes
* effHeight
;
429 bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
430 0L, offset, totalSize);
431 else if (ovr_pitch && offset)
432 bm.bmBits = (LPVOID) offset;
435 /* bm.bmBits = VirtualAlloc(NULL, totalSize,
436 MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); */
438 bm
.bmBits
= ExAllocatePool(NonPagedPool
, totalSize
);
442 if (usage
== DIB_PAL_COLORS
) memcpy(bmi
->bmiColors
, (UINT
*)DIB_MapPaletteColors(dc
, bmi
), sizeof(UINT
*));
444 // Allocate Memory for DIB and fill structure
446 dib
= ExAllocatePool(PagedPool
, sizeof(DIBSECTION
));
447 RtlZeroMemory(dib
, sizeof(DIBSECTION
));
453 dib
->dsBmih
.biSizeImage
= totalSize
;
455 /* Set dsBitfields values */
456 if ( usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
458 dib
->dsBitfields
[0] = dib
->dsBitfields
[1] = dib
->dsBitfields
[2] = 0;
460 else switch(bi
->biBitCount
)
463 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)bmi
->bmiColors
: 0x7c00;
464 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 1) : 0x03e0;
465 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 2) : 0x001f;
469 dib
->dsBitfields
[0] = 0xff;
470 dib
->dsBitfields
[1] = 0xff00;
471 dib
->dsBitfields
[2] = 0xff0000;
475 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)bmi
->bmiColors
: 0xff;
476 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 1) : 0xff00;
477 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 2) : 0xff0000;
480 dib
->dshSection
= section
;
481 dib
->dsOffset
= offset
;
484 // Create Device Dependent Bitmap and add DIB pointer
487 res
= W32kCreateDIBitmap(dc
->hSelf
, bi
, 0, NULL
, bmi
, usage
);
490 bmp
= BITMAPOBJ_HandleToPtr (res
);
493 bmp
->dib
= (DIBSECTION
*) dib
;
498 // Clean up in case of errors
499 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
|| (bm
.bmBitsPixel
<= 8 && !colorMap
))
501 DbgPrint("got an error res=%08x, bmp=%p, dib=%p, bm.bmBits=%p\n", res
, bmp
, dib
, bm
.bmBits
);
505 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
507 VirtualFree(bm.bmBits, 0L, MEM_RELEASE), bm.bmBits = NULL;
510 if (colorMap
) { ExFreePool(colorMap
); colorMap
= NULL
; }
511 if (dib
) { ExFreePool(dib
); dib
= NULL
; }
512 if (bmp
) { bmp
= NULL
; }
513 if (res
) { GDIOBJ_FreeObj(res
, GO_BITMAP_MAGIC
); res
= 0; }
516 // Install fault handler, if possible
519 if (VIRTUAL_SetFaultHandler(bm.bmBits, DIB_FaultHandler, (LPVOID)res))
521 if (section || offset)
523 DIB_DoProtectDIBSection( bmp, PAGE_READWRITE );
524 if (dib) dib->status = DIB_AppMod;
528 DIB_DoProtectDIBSection( bmp, PAGE_READONLY );
529 if (dib) dib->status = DIB_InSync;
535 BITMAPOBJ_ReleasePtr(res
);
537 // Return BITMAP handle and storage location
538 if (bm
.bmBits
&& bits
) *bits
= bm
.bmBits
;
542 /***********************************************************************
543 * DIB_GetDIBWidthBytes
545 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
546 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
547 * 11/16/1999 (RJJ) lifted from wine
549 int DIB_GetDIBWidthBytes(int width
, int depth
)
555 case 1: words
= (width
+ 31) / 32; break;
556 case 4: words
= (width
+ 7) / 8; break;
557 case 8: words
= (width
+ 3) / 4; break;
559 case 16: words
= (width
+ 1) / 2; break;
560 case 24: words
= (width
* 3 + 3)/4; break;
563 DPRINT("(%d): Unsupported depth\n", depth
);
571 /***********************************************************************
572 * DIB_GetDIBImageBytes
574 * Return the number of bytes used to hold the image in a DIB bitmap.
575 * 11/16/1999 (RJJ) lifted from wine
578 int DIB_GetDIBImageBytes (int width
, int height
, int depth
)
580 return DIB_GetDIBWidthBytes( width
, depth
) * (height
< 0 ? -height
: height
);
583 /***********************************************************************
586 * Return the size of the bitmap info structure including color table.
587 * 11/16/1999 (RJJ) lifted from wine
590 int DIB_BitmapInfoSize (const BITMAPINFO
* info
, WORD coloruse
)
594 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
596 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
597 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
598 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
600 else /* assume BITMAPINFOHEADER */
602 colors
= info
->bmiHeader
.biClrUsed
;
603 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
604 return sizeof(BITMAPINFOHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
608 int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
609 int *height
, WORD
*bpp
, WORD
*compr
)
611 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
613 *width
= header
->biWidth
;
614 *height
= header
->biHeight
;
615 *bpp
= header
->biBitCount
;
616 *compr
= header
->biCompression
;
619 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
621 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
622 *width
= core
->bcWidth
;
623 *height
= core
->bcHeight
;
624 *bpp
= core
->bcBitCount
;
628 DbgPrint("(%ld): wrong size for header\n", header
->biSize
);
632 // Converts a Device Independent Bitmap (DIB) to a Device Dependant Bitmap (DDB)
633 // The specified Device Context (DC) defines what the DIB should be converted to
634 PBITMAPOBJ
DIBtoDDB(HGLOBAL hPackedDIB
, HDC hdc
) // FIXME: This should be removed. All references to this function should
635 // change to W32kSetDIBits
638 PBITMAPOBJ pBmp
= NULL
;
642 // Get a pointer to the packed DIB's data
643 // pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
646 pbits
= (dib
+ DIB_BitmapInfoSize(&dib
->dsBmih
, DIB_RGB_COLORS
));
648 // Create a DDB from the DIB
649 hBmp
= W32kCreateDIBitmap(hdc
, &dib
->dsBmih
, CBM_INIT
, (LPVOID
)pbits
, &dib
->dsBmih
, DIB_RGB_COLORS
);
651 // GlobalUnlock(hPackedDIB);
653 // Retrieve the internal Pixmap from the DDB
654 pBmp
= (BITMAPOBJ
*)GDIOBJ_LockObj(hBmp
, GO_BITMAP_MAGIC
);
659 RGBQUAD
*DIB_MapPaletteColors(PDC dc
, LPBITMAPINFO lpbmi
)
666 palObj
= AccessUserObject(dc
->DevInfo
.hpalDefault
);
668 if (palObj
== NULL
) {
669 // RELEASEDCINFO(hDC);
673 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
674 if (lpbmi
->bmiHeader
.biClrUsed
)
675 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
677 lpRGB
= (RGBQUAD
*)ExAllocatePool(NonPagedPool
, sizeof(RGBQUAD
) * nNumColors
);
678 lpIndex
= (DWORD
*)&lpbmi
->bmiColors
[0];
680 for (i
=0; i
<nNumColors
; i
++) {
681 lpRGB
[i
].rgbRed
= palObj
->logpalette
->palPalEntry
[*lpIndex
].peRed
;
682 lpRGB
[i
].rgbGreen
= palObj
->logpalette
->palPalEntry
[*lpIndex
].peGreen
;
683 lpRGB
[i
].rgbBlue
= palObj
->logpalette
->palPalEntry
[*lpIndex
].peBlue
;
686 // RELEASEDCINFO(hDC);
687 // RELEASEPALETTEINFO(hPalette);
691 HPALETTE
BuildDIBPalette(BITMAPINFO
*bmi
, PINT paletteType
)
696 // Determine Bits Per Pixel
697 bits
= bmi
->bmiHeader
.biBitCount
;
699 // Determine paletteType from Bits Per Pixel
702 *paletteType
= PAL_INDEXED
;
706 *paletteType
= PAL_BITFIELDS
;
708 *paletteType
= PAL_RGB
; // FIXME: This could be BGR, must still check
711 if (bmi
->bmiHeader
.biClrUsed
== 0 &&
712 bmi
->bmiHeader
.biBitCount
<= 8)
714 ColourCount
= 1 << bmi
->bmiHeader
.biBitCount
;
718 ColourCount
= bmi
->bmiHeader
.biClrUsed
;
721 return EngCreatePalette(*paletteType
, ColourCount
, bmi
->bmiColors
, 0, 0, 0);