1 #undef WIN32_LEAN_AND_MEAN
4 #include <win32k/bitmaps.h>
5 #include <win32k/debug.h>
7 #include "../eng/objects.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 USHORT 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 DestSurf
= ExAllocatePool(PagedPool
, sizeof(SURFOBJ
));
100 DestGDI
= ExAllocatePool(PagedPool
, sizeof(SURFGDI
));
101 DestBitmap
= (HBITMAP
)CreateGDIHandle(DestGDI
, DestSurf
);
103 BitmapToSurf(hDC
, DestGDI
, DestSurf
, bitmap
);
105 // Create source surface
106 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
107 SourceSize
.cy
= bmi
->bmiHeader
.biHeight
;
108 SourceBitmap
= EngCreateBitmap(SourceSize
, DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
),
109 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
111 SourceSurf
= (PSURFOBJ
)AccessUserObject(SourceBitmap
);
113 // Destination palette obtained from the hDC
114 hDCPalette
= (PPALGDI
)AccessInternalObject(dc
->DevInfo
.hpalDefault
);
115 DDB_Palette_Type
= hDCPalette
->Mode
;
116 DDB_Palette
= dc
->DevInfo
.hpalDefault
;
118 // Source palette obtained from the BITMAPINFO
119 DIB_Palette
= BuildDIBPalette(bmi
, &DIB_Palette_Type
);
121 // Determine XLATEOBJ for color translation
122 XlateObj
= EngCreateXlate(DDB_Palette_Type
, DIB_Palette_Type
, DDB_Palette
, DIB_Palette
);
124 // Determine destination rectangle and source point
129 DestRect
.right
= SourceSize
.cx
;
130 DestRect
.bottom
= SourceSize
.cy
;
132 copyBitsResult
= EngCopyBits(DestSurf
, SourceSurf
, NULL
, XlateObj
, &DestRect
, &ZeroPoint
);
135 EngDeleteSurface(SourceBitmap
);
136 EngDeleteSurface(DestBitmap
);
138 // if (ColorUse == DIB_PAL_COLORS)
139 // WinFree((LPSTR)lpRGB);
141 // GDI_ReleaseObj(hBitmap); unlock?
142 GDIOBJ_UnlockObj(hBitmap
, GO_BITMAP_MAGIC
);
148 INT STDCALL
W32kSetDIBitsToDevice(HDC hDC
,
158 CONST BITMAPINFO
*bmi
,
164 UINT STDCALL
W32kGetDIBColorTable(HDC hDC
,
172 // Converts a device-dependent bitmap to a DIB
173 INT STDCALL
W32kGetDIBits(HDC hDC
,
184 INT STDCALL
W32kStretchDIBits(HDC hDC
,
194 CONST BITMAPINFO
*BitsInfo
,
201 LONG STDCALL
W32kGetBitmapBits(HBITMAP hBitmap
,
208 bmp
= BITMAPOBJ_HandleToPtr (hBitmap
);
214 /* If the bits vector is null, the function should return the read size */
217 return bmp
->bitmap
.bmWidthBytes
* bmp
->bitmap
.bmHeight
;
222 DPRINT ("(%ld): Negative number of bytes passed???\n", Count
);
226 /* Only get entire lines */
227 height
= Count
/ bmp
->bitmap
.bmWidthBytes
;
228 if (height
> bmp
->bitmap
.bmHeight
)
230 height
= bmp
->bitmap
.bmHeight
;
232 Count
= height
* bmp
->bitmap
.bmWidthBytes
;
235 DPRINT("Less then one entire line requested\n");
239 DPRINT("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
240 hBitmap
, Count
, Bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
241 1 << bmp
->bitmap
.bmBitsPixel
, height
);
243 /* FIXME: Call DDI CopyBits here if available */
246 DPRINT("Calling device specific BitmapBits\n");
247 if(bmp
->DDBitmap
->funcs
->pBitmapBits
)
249 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hbitmap
, bits
, count
,
254 ERR_(bitmap
)("BitmapBits == NULL??\n");
261 if(!bmp
->bitmap
.bmBits
)
263 DPRINT ("Bitmap is empty\n");
268 memcpy(Bits
, bmp
->bitmap
.bmBits
, Count
);
276 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
277 // The DDB that is created will be whatever bit depth your reference DC is
278 HBITMAP STDCALL
W32kCreateDIBitmap(HDC hdc
, const BITMAPINFOHEADER
*header
,
279 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
289 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
, &compr
) == -1) return 0;
290 if (height
< 0) height
= -height
;
292 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
293 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
295 if (bpp
!= 1) fColor
= TRUE
;
296 else if ((coloruse
!= DIB_RGB_COLORS
) ||
297 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
300 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
302 RGBQUAD
*rgb
= data
->bmiColors
;
303 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
305 // Check if the first color of the colormap is black
306 if ((col
== RGB(0, 0, 0)))
309 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
311 // If the second color is white, create a monochrome bitmap
312 fColor
= (col
!= RGB(0xff,0xff,0xff));
316 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
318 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
319 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
321 if ((col
== RGB(0,0,0)))
324 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
325 fColor
= (col
!= RGB(0xff,0xff,0xff));
331 DbgPrint("(%ld): wrong size for data\n", data
->bmiHeader
.biSize
);
336 // Now create the bitmap
340 // If we are using indexed colors, then we need to create a bitmap that is compatible with the palette
341 if(coloruse
== DIB_PAL_COLORS
)
343 handle
= W32kCreateCompatibleBitmap(hdc
, width
, height
);
345 else if(coloruse
== DIB_RGB_COLORS
) {
346 handle
= W32kCreateBitmap(width
, height
, 1, 24, NULL
);
349 else handle
= W32kCreateBitmap(width
, height
, 1, 1, NULL
);
351 if (!handle
) return 0;
353 if (init
== CBM_INIT
)
355 W32kSetDIBits(hdc
, handle
, 0, height
, bits
, data
, coloruse
);
361 HBITMAP STDCALL
W32kCreateDIBSection(HDC hDC
,
362 CONST BITMAPINFO
*bmi
,
370 BOOL bDesktopDC
= FALSE
;
372 // If the reference hdc is null, take the desktop dc
375 hDC
= W32kCreateCompatableDC(0);
379 if ((dc
= DC_HandleToPtr(hDC
)))
381 hbitmap
= DIB_CreateDIBSection(dc
, bmi
, Usage
, Bits
, hSection
, dwOffset
, 0);
382 DC_ReleasePtr( hDC
);
391 HBITMAP
DIB_CreateDIBSection(
392 PDC dc
, BITMAPINFO
*bmi
, UINT usage
,
393 LPVOID
*bits
, HANDLE section
,
394 DWORD offset
, DWORD ovr_pitch
)
397 BITMAPOBJ
*bmp
= NULL
;
398 DIBSECTION
*dib
= NULL
;
399 int *colorMap
= NULL
;
402 // Fill BITMAP32 structure with DIB data
403 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
404 INT effHeight
, totalSize
;
407 DbgPrint("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
408 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
409 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
411 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
413 bm
.bmWidth
= bi
->biWidth
;
414 bm
.bmHeight
= effHeight
;
415 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
417 bm
.bmPlanes
= bi
->biPlanes
;
418 bm
.bmBitsPixel
= bi
->biBitCount
;
421 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
422 // we're dealing with a compressed bitmap. Otherwise, use width * height.
423 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
424 ? bi
->biSizeImage
: bm
.bmWidthBytes
* effHeight
;
428 bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
429 0L, offset, totalSize);
430 else if (ovr_pitch && offset)
431 bm.bmBits = (LPVOID) offset;
434 /* bm.bmBits = VirtualAlloc(NULL, totalSize,
435 MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); */
437 bm
.bmBits
= ExAllocatePool(NonPagedPool
, totalSize
);
441 if (usage
== DIB_PAL_COLORS
) memcpy(bmi
->bmiColors
, (UINT
*)DIB_MapPaletteColors(dc
, bmi
), sizeof(UINT
*));
443 // Allocate Memory for DIB and fill structure
445 dib
= ExAllocatePool(PagedPool
, sizeof(DIBSECTION
));
446 RtlZeroMemory(dib
, sizeof(DIBSECTION
));
452 dib
->dsBmih
.biSizeImage
= totalSize
;
454 /* Set dsBitfields values */
455 if ( usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
457 dib
->dsBitfields
[0] = dib
->dsBitfields
[1] = dib
->dsBitfields
[2] = 0;
459 else switch(bi
->biBitCount
)
462 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)bmi
->bmiColors
: 0x7c00;
463 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 1) : 0x03e0;
464 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 2) : 0x001f;
468 dib
->dsBitfields
[0] = 0xff;
469 dib
->dsBitfields
[1] = 0xff00;
470 dib
->dsBitfields
[2] = 0xff0000;
474 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)bmi
->bmiColors
: 0xff;
475 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 1) : 0xff00;
476 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 2) : 0xff0000;
479 dib
->dshSection
= section
;
480 dib
->dsOffset
= offset
;
483 // Create Device Dependent Bitmap and add DIB pointer
486 res
= W32kCreateDIBitmap(dc
->hSelf
, bi
, 0, NULL
, bmi
, usage
);
489 bmp
= BITMAPOBJ_HandleToPtr (res
);
492 bmp
->dib
= (DIBSECTION
*) dib
;
497 // Clean up in case of errors
498 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
|| (bm
.bmBitsPixel
<= 8 && !colorMap
))
500 DbgPrint("got an error res=%08x, bmp=%p, dib=%p, bm.bmBits=%p\n", res
, bmp
, dib
, bm
.bmBits
);
504 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
506 VirtualFree(bm.bmBits, 0L, MEM_RELEASE), bm.bmBits = NULL;
509 if (colorMap
) { ExFreePool(colorMap
); colorMap
= NULL
; }
510 if (dib
) { ExFreePool(dib
); dib
= NULL
; }
511 if (bmp
) { bmp
= NULL
; }
512 if (res
) { GDIOBJ_FreeObj(res
, GO_BITMAP_MAGIC
); res
= 0; }
515 // Install fault handler, if possible
518 if (VIRTUAL_SetFaultHandler(bm.bmBits, DIB_FaultHandler, (LPVOID)res))
520 if (section || offset)
522 DIB_DoProtectDIBSection( bmp, PAGE_READWRITE );
523 if (dib) dib->status = DIB_AppMod;
527 DIB_DoProtectDIBSection( bmp, PAGE_READONLY );
528 if (dib) dib->status = DIB_InSync;
534 BITMAPOBJ_ReleasePtr(res
);
536 // Return BITMAP handle and storage location
537 if (bm
.bmBits
&& bits
) *bits
= bm
.bmBits
;
541 /***********************************************************************
542 * DIB_GetDIBWidthBytes
544 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
545 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
546 * 11/16/1999 (RJJ) lifted from wine
548 int DIB_GetDIBWidthBytes(int width
, int depth
)
554 case 1: words
= (width
+ 31) / 32; break;
555 case 4: words
= (width
+ 7) / 8; break;
556 case 8: words
= (width
+ 3) / 4; break;
558 case 16: words
= (width
+ 1) / 2; break;
559 case 24: words
= (width
* 3 + 3)/4; break;
562 DPRINT("(%d): Unsupported depth\n", depth
);
570 /***********************************************************************
571 * DIB_GetDIBImageBytes
573 * Return the number of bytes used to hold the image in a DIB bitmap.
574 * 11/16/1999 (RJJ) lifted from wine
577 int DIB_GetDIBImageBytes (int width
, int height
, int depth
)
579 return DIB_GetDIBWidthBytes( width
, depth
) * (height
< 0 ? -height
: height
);
582 /***********************************************************************
585 * Return the size of the bitmap info structure including color table.
586 * 11/16/1999 (RJJ) lifted from wine
589 int DIB_BitmapInfoSize (const BITMAPINFO
* info
, WORD coloruse
)
593 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
595 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
596 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
597 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
599 else /* assume BITMAPINFOHEADER */
601 colors
= info
->bmiHeader
.biClrUsed
;
602 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
603 return sizeof(BITMAPINFOHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
607 int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
608 int *height
, WORD
*bpp
, WORD
*compr
)
610 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
612 *width
= header
->biWidth
;
613 *height
= header
->biHeight
;
614 *bpp
= header
->biBitCount
;
615 *compr
= header
->biCompression
;
618 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
620 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
621 *width
= core
->bcWidth
;
622 *height
= core
->bcHeight
;
623 *bpp
= core
->bcBitCount
;
627 DbgPrint("(%ld): wrong size for header\n", header
->biSize
);
631 // Converts a Device Independent Bitmap (DIB) to a Device Dependant Bitmap (DDB)
632 // The specified Device Context (DC) defines what the DIB should be converted to
633 PBITMAPOBJ
DIBtoDDB(HGLOBAL hPackedDIB
, HDC hdc
) // FIXME: This should be removed. All references to this function should
634 // change to W32kSetDIBits
637 PBITMAPOBJ pBmp
= NULL
;
641 // Get a pointer to the packed DIB's data
642 // pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
645 pbits
= (dib
+ DIB_BitmapInfoSize(&dib
->dsBmih
, DIB_RGB_COLORS
));
647 // Create a DDB from the DIB
648 hBmp
= W32kCreateDIBitmap(hdc
, &dib
->dsBmih
, CBM_INIT
, (LPVOID
)pbits
, &dib
->dsBmih
, DIB_RGB_COLORS
);
650 // GlobalUnlock(hPackedDIB);
652 // Retrieve the internal Pixmap from the DDB
653 pBmp
= (BITMAPOBJ
*)GDIOBJ_LockObj(hBmp
, GO_BITMAP_MAGIC
);
658 RGBQUAD
*DIB_MapPaletteColors(PDC dc
, LPBITMAPINFO lpbmi
)
665 palObj
= AccessUserObject(dc
->DevInfo
.hpalDefault
);
667 if (palObj
== NULL
) {
668 // RELEASEDCINFO(hDC);
672 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
673 if (lpbmi
->bmiHeader
.biClrUsed
)
674 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
676 lpRGB
= (RGBQUAD
*)ExAllocatePool(NonPagedPool
, sizeof(RGBQUAD
) * nNumColors
);
677 lpIndex
= (DWORD
*)&lpbmi
->bmiColors
[0];
679 for (i
=0; i
<nNumColors
; i
++) {
680 lpRGB
[i
].rgbRed
= palObj
->logpalette
->palPalEntry
[*lpIndex
].peRed
;
681 lpRGB
[i
].rgbGreen
= palObj
->logpalette
->palPalEntry
[*lpIndex
].peGreen
;
682 lpRGB
[i
].rgbBlue
= palObj
->logpalette
->palPalEntry
[*lpIndex
].peBlue
;
685 // RELEASEDCINFO(hDC);
686 // RELEASEPALETTEINFO(hPalette);
690 HPALETTE
BuildDIBPalette(BITMAPINFO
*bmi
, PINT paletteType
)
694 // Determine Bits Per Pixel
695 bits
= bmi
->bmiHeader
.biBitCount
;
697 // Determine paletteType from Bits Per Pixel
700 *paletteType
= PAL_INDEXED
;
704 *paletteType
= PAL_BITFIELDS
;
706 *paletteType
= PAL_RGB
; // FIXME: This could be BGR, must still check
709 return EngCreatePalette(*paletteType
, bmi
->bmiHeader
.biClrUsed
, bmi
->bmiColors
, 0, 0, 0);