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 UINT STDCALL
W32kSetDIBColorTable(HDC hDC
,
13 CONST RGBQUAD
*Colors
)
16 PALETTEENTRY
* palEntry
;
20 if (!(dc
= (PDC
)AccessUserObject(hDC
))) return 0;
22 if (!(palette
= (PPALOBJ
)AccessUserObject(dc
->DevInfo
.hpalDefault
)))
24 // GDI_ReleaseObj( hdc );
28 // Transfer color info
30 if (dc
->w
.bitsPerPixel
<= 8) {
31 palEntry
= palette
->logpalette
->palPalEntry
+ StartIndex
;
32 if (StartIndex
+ Entries
> (1 << dc
->w
.bitsPerPixel
))
33 Entries
= (1 << dc
->w
.bitsPerPixel
) - StartIndex
;
35 if (StartIndex
+ Entries
> palette
->logpalette
->palNumEntries
)
36 Entries
= palette
->logpalette
->palNumEntries
- StartIndex
;
38 for (end
= Colors
+ Entries
; Colors
< end
; palEntry
++, Colors
++)
40 palEntry
->peRed
= Colors
->rgbRed
;
41 palEntry
->peGreen
= Colors
->rgbGreen
;
42 palEntry
->peBlue
= Colors
->rgbBlue
;
48 // GDI_ReleaseObj(dc->DevInfo.hpalDefault);
49 // GDI_ReleaseObj(hdc);
54 // Converts a DIB to a device-dependent bitmap
55 INT STDCALL
W32kSetDIBits(HDC hDC
,
60 CONST BITMAPINFO
*bmi
,
65 HBITMAP SourceBitmap
, DestBitmap
;
68 PSURFOBJ DestSurf
, SourceSurf
;
76 HPALETTE DDB_Palette
, DIB_Palette
;
77 USHORT DDB_Palette_Type
, DIB_Palette_Type
;
81 if (!(dc
= DC_HandleToPtr(hDC
))) return 0;
83 if (!(bitmap
= (BITMAPOBJ
*)GDIOBJ_HandleToPtr(hBitmap
, GO_BITMAP_MAGIC
)))
85 // GDI_ReleaseObj(hDC);
90 if (ColorUse
== DIB_PAL_COLORS
)
91 lpRGB
= DIB_MapPaletteColors(hDC
, bmi
);
93 lpRGB
= &bmi
->bmiColors
[0];
95 // Create a temporary surface for the destination bitmap
96 DestSurf
= ExAllocatePool(PagedPool
, sizeof(SURFOBJ
));
97 DestGDI
= ExAllocatePool(PagedPool
, sizeof(SURFGDI
));
98 DestBitmap
= (HBITMAP
)CreateGDIHandle(DestGDI
, DestSurf
);
100 BitmapToSurf(hDC
, DestGDI
, DestSurf
, bitmap
);
102 // Create source surface
103 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
104 SourceSize
.cy
= bmi
->bmiHeader
.biHeight
;
105 SourceBitmap
= EngCreateBitmap(SourceSize
, DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
),
106 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
108 SourceSurf
= (PSURFOBJ
)AccessUserObject(SourceBitmap
);
110 // Destination palette obtained from the hDC
111 hDCPalette
= (PPALGDI
)AccessInternalObject(dc
->DevInfo
.hpalDefault
);
112 DDB_Palette_Type
= hDCPalette
->Mode
;
113 DDB_Palette
= dc
->DevInfo
.hpalDefault
;
115 // Source palette obtained from the BITMAPINFO
116 DIB_Palette
= BuildDIBPalette(bmi
, &DIB_Palette_Type
);
118 // Determine XLATEOBJ for color translation
119 XlateObj
= EngCreateXlate(DDB_Palette_Type
, DIB_Palette_Type
, DDB_Palette
, DIB_Palette
);
121 // Determine destination rectangle and source point
126 DestRect
.right
= SourceSize
.cx
;
127 DestRect
.bottom
= SourceSize
.cy
;
129 copyBitsResult
= EngCopyBits(DestSurf
, SourceSurf
, NULL
, XlateObj
, &DestRect
, &ZeroPoint
);
132 EngDeleteSurface(SourceBitmap
);
133 EngDeleteSurface(DestBitmap
);
135 // if (ColorUse == DIB_PAL_COLORS)
136 // WinFree((LPSTR)lpRGB);
138 // GDI_ReleaseObj(hBitmap); unlock?
139 // GDI_ReleaseObj(hDC);
144 INT STDCALL
W32kSetDIBitsToDevice(HDC hDC
,
154 CONST BITMAPINFO
*bmi
,
160 UINT STDCALL
W32kGetDIBColorTable(HDC hDC
,
168 // Converts a device-dependent bitmap to a DIB
169 INT STDCALL
W32kGetDIBits(HDC hDC
,
180 INT STDCALL
W32kStretchDIBits(HDC hDC
,
190 CONST BITMAPINFO
*BitsInfo
,
197 LONG STDCALL
W32kGetBitmapBits(HBITMAP hBitmap
,
204 bmp
= BITMAPOBJ_HandleToPtr (hBitmap
);
210 /* If the bits vector is null, the function should return the read size */
213 return bmp
->bitmap
.bmWidthBytes
* bmp
->bitmap
.bmHeight
;
218 DPRINT ("(%ld): Negative number of bytes passed???\n", Count
);
222 /* Only get entire lines */
223 height
= Count
/ bmp
->bitmap
.bmWidthBytes
;
224 if (height
> bmp
->bitmap
.bmHeight
)
226 height
= bmp
->bitmap
.bmHeight
;
228 Count
= height
* bmp
->bitmap
.bmWidthBytes
;
231 DPRINT("Less then one entire line requested\n");
232 BITMAPOBJ_UnlockBitmap (hBitmap
);
236 DPRINT("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
237 hBitmap
, Count
, Bits
, bmp
->bitmap
.bmWidth
, bmp
->bitmap
.bmHeight
,
238 1 << bmp
->bitmap
.bmBitsPixel
, height
);
240 /* FIXME: Call DDI CopyBits here if available */
243 DPRINT("Calling device specific BitmapBits\n");
244 if(bmp
->DDBitmap
->funcs
->pBitmapBits
)
246 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hbitmap
, bits
, count
,
251 ERR_(bitmap
)("BitmapBits == NULL??\n");
258 if(!bmp
->bitmap
.bmBits
)
260 DPRINT ("Bitmap is empty\n");
265 memcpy(Bits
, bmp
->bitmap
.bmBits
, Count
);
269 BITMAPOBJ_UnlockBitmap (hBitmap
);
274 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
275 // The DDB that is created will be whatever bit depth your reference DC is
276 HBITMAP STDCALL
W32kCreateDIBitmap(HDC hdc
, const BITMAPINFOHEADER
*header
,
277 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
287 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
, &compr
) == -1) return 0;
288 if (height
< 0) height
= -height
;
290 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
291 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
293 if (bpp
!= 1) fColor
= TRUE
;
294 else if ((coloruse
!= DIB_RGB_COLORS
) ||
295 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
298 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
300 RGBQUAD
*rgb
= data
->bmiColors
;
301 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
303 // Check if the first color of the colormap is black
304 if ((col
== RGB(0, 0, 0)))
307 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
309 // If the second color is white, create a monochrome bitmap
310 fColor
= (col
!= RGB(0xff,0xff,0xff));
314 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
316 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
317 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
319 if ((col
== RGB(0,0,0)))
322 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
323 fColor
= (col
!= RGB(0xff,0xff,0xff));
329 DbgPrint("(%ld): wrong size for data\n", data
->bmiHeader
.biSize
);
334 // Now create the bitmap
338 // If we are using indexed colors, then we need to create a bitmap that is compatible with the palette
339 if(coloruse
== DIB_PAL_COLORS
)
341 handle
= W32kCreateCompatibleBitmap(hdc
, width
, height
);
343 else if(coloruse
== DIB_RGB_COLORS
) {
344 handle
= W32kCreateBitmap(width
, height
, 1, 24, NULL
);
347 else handle
= W32kCreateBitmap(width
, height
, 1, 1, NULL
);
349 if (!handle
) return 0;
351 if (init
== CBM_INIT
)
353 W32kSetDIBits(hdc
, handle
, 0, height
, bits
, data
, coloruse
);
359 HBITMAP STDCALL
W32kCreateDIBSection(HDC hDC
,
360 CONST BITMAPINFO
*bmi
,
368 BOOL bDesktopDC
= FALSE
;
370 // If the reference hdc is null, take the desktop dc
373 hDC
= W32kCreateCompatableDC(0);
377 if ((dc
= DC_HandleToPtr(hDC
)))
379 hbitmap
= DIB_CreateDIBSection(dc
, bmi
, Usage
, Bits
, hSection
, dwOffset
, 0);
389 HBITMAP
DIB_CreateDIBSection(
390 PDC dc
, BITMAPINFO
*bmi
, UINT usage
,
391 LPVOID
*bits
, HANDLE section
,
392 DWORD offset
, DWORD ovr_pitch
)
395 BITMAPOBJ
*bmp
= NULL
;
396 DIBSECTION
*dib
= NULL
;
397 int *colorMap
= NULL
;
400 // Fill BITMAP32 structure with DIB data
401 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
402 INT effHeight
, totalSize
;
405 DbgPrint("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
406 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
407 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
409 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
411 bm
.bmWidth
= bi
->biWidth
;
412 bm
.bmHeight
= effHeight
;
413 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
415 bm
.bmPlanes
= bi
->biPlanes
;
416 bm
.bmBitsPixel
= bi
->biBitCount
;
419 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
420 // we're dealing with a compressed bitmap. Otherwise, use width * height.
421 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
422 ? bi
->biSizeImage
: bm
.bmWidthBytes
* effHeight
;
426 bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
427 0L, offset, totalSize);
428 else if (ovr_pitch && offset)
429 bm.bmBits = (LPVOID) offset;
432 /* bm.bmBits = VirtualAlloc(NULL, totalSize,
433 MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); */
435 bm
.bmBits
= ExAllocatePool(NonPagedPool
, totalSize
);
439 if (usage
== DIB_PAL_COLORS
) memcpy(bmi
->bmiColors
, (UINT
*)DIB_MapPaletteColors(dc
, bmi
), sizeof(UINT
*));
441 // Allocate Memory for DIB and fill structure
443 dib
= ExAllocatePool(PagedPool
, sizeof(DIBSECTION
));
444 RtlZeroMemory(dib
, sizeof(DIBSECTION
));
450 dib
->dsBmih
.biSizeImage
= totalSize
;
452 /* Set dsBitfields values */
453 if ( usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
455 dib
->dsBitfields
[0] = dib
->dsBitfields
[1] = dib
->dsBitfields
[2] = 0;
457 else switch(bi
->biBitCount
)
460 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)bmi
->bmiColors
: 0x7c00;
461 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 1) : 0x03e0;
462 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 2) : 0x001f;
466 dib
->dsBitfields
[0] = 0xff;
467 dib
->dsBitfields
[1] = 0xff00;
468 dib
->dsBitfields
[2] = 0xff0000;
472 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)bmi
->bmiColors
: 0xff;
473 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 1) : 0xff00;
474 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 2) : 0xff0000;
477 dib
->dshSection
= section
;
478 dib
->dsOffset
= offset
;
481 // Create Device Dependent Bitmap and add DIB pointer
484 res
= W32kCreateDIBitmap(dc
->hSelf
, bi
, 0, NULL
, bmi
, usage
);
487 bmp
= BITMAPOBJ_HandleToPtr (res
);
490 bmp
->dib
= (DIBSECTION
*) dib
;
495 // Clean up in case of errors
496 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
|| (bm
.bmBitsPixel
<= 8 && !colorMap
))
498 DbgPrint("got an error res=%08x, bmp=%p, dib=%p, bm.bmBits=%p\n", res
, bmp
, dib
, bm
.bmBits
);
502 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
504 VirtualFree(bm.bmBits, 0L, MEM_RELEASE), bm.bmBits = NULL;
507 if (colorMap
) { ExFreePool(colorMap
); colorMap
= NULL
; }
508 if (dib
) { ExFreePool(dib
); dib
= NULL
; }
509 if (bmp
) { BITMAPOBJ_UnlockBitmap(res
); bmp
= NULL
; }
510 if (res
) { GDIOBJ_FreeObject(res
, GO_BITMAP_MAGIC
); res
= 0; }
513 // Install fault handler, if possible
516 if (VIRTUAL_SetFaultHandler(bm.bmBits, DIB_FaultHandler, (LPVOID)res))
518 if (section || offset)
520 DIB_DoProtectDIBSection( bmp, PAGE_READWRITE );
521 if (dib) dib->status = DIB_AppMod;
525 DIB_DoProtectDIBSection( bmp, PAGE_READONLY );
526 if (dib) dib->status = DIB_InSync;
531 // Return BITMAP handle and storage location
532 if (bmp
) BITMAPOBJ_UnlockBitmap(res
);
533 if (bm
.bmBits
&& bits
) *bits
= bm
.bmBits
;
537 /***********************************************************************
538 * DIB_GetDIBWidthBytes
540 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
541 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
542 * 11/16/1999 (RJJ) lifted from wine
544 int DIB_GetDIBWidthBytes(int width
, int depth
)
550 case 1: words
= (width
+ 31) / 32; break;
551 case 4: words
= (width
+ 7) / 8; break;
552 case 8: words
= (width
+ 3) / 4; break;
554 case 16: words
= (width
+ 1) / 2; break;
555 case 24: words
= (width
* 3 + 3)/4; break;
558 DPRINT("(%d): Unsupported depth\n", depth
);
566 /***********************************************************************
567 * DIB_GetDIBImageBytes
569 * Return the number of bytes used to hold the image in a DIB bitmap.
570 * 11/16/1999 (RJJ) lifted from wine
573 int DIB_GetDIBImageBytes (int width
, int height
, int depth
)
575 return DIB_GetDIBWidthBytes( width
, depth
) * (height
< 0 ? -height
: height
);
578 /***********************************************************************
581 * Return the size of the bitmap info structure including color table.
582 * 11/16/1999 (RJJ) lifted from wine
585 int DIB_BitmapInfoSize (const BITMAPINFO
* info
, WORD coloruse
)
589 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
591 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
592 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
593 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
595 else /* assume BITMAPINFOHEADER */
597 colors
= info
->bmiHeader
.biClrUsed
;
598 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
599 return sizeof(BITMAPINFOHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
603 int DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, DWORD
*width
,
604 int *height
, WORD
*bpp
, WORD
*compr
)
606 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
608 *width
= header
->biWidth
;
609 *height
= header
->biHeight
;
610 *bpp
= header
->biBitCount
;
611 *compr
= header
->biCompression
;
614 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
616 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
617 *width
= core
->bcWidth
;
618 *height
= core
->bcHeight
;
619 *bpp
= core
->bcBitCount
;
623 DbgPrint("(%ld): wrong size for header\n", header
->biSize
);
627 // Converts a Device Independent Bitmap (DIB) to a Device Dependant Bitmap (DDB)
628 // The specified Device Context (DC) defines what the DIB should be converted to
629 PBITMAPOBJ
DIBtoDDB(HGLOBAL hPackedDIB
, HDC hdc
) // FIXME: This should be removed. All references to this function should
630 // change to W32kSetDIBits
633 PBITMAPOBJ pBmp
= NULL
;
637 // Get a pointer to the packed DIB's data
638 // pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
641 pbits
= (dib
+ DIB_BitmapInfoSize(&dib
->dsBmih
, DIB_RGB_COLORS
));
643 // Create a DDB from the DIB
644 hBmp
= W32kCreateDIBitmap(hdc
, &dib
->dsBmih
, CBM_INIT
, (LPVOID
)pbits
, &dib
->dsBmih
, DIB_RGB_COLORS
);
646 // GlobalUnlock(hPackedDIB);
648 // Retrieve the internal Pixmap from the DDB
649 pBmp
= (BITMAPOBJ
*)GDIOBJ_HandleToPtr(hBmp
, GO_BITMAP_MAGIC
);
654 RGBQUAD
*DIB_MapPaletteColors(PDC dc
, LPBITMAPINFO lpbmi
)
661 palObj
= AccessUserObject(dc
->DevInfo
.hpalDefault
);
663 if (palObj
== NULL
) {
664 // RELEASEDCINFO(hDC);
668 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
669 if (lpbmi
->bmiHeader
.biClrUsed
)
670 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
672 lpRGB
= (RGBQUAD
*)ExAllocatePool(NonPagedPool
, sizeof(RGBQUAD
) * nNumColors
);
673 lpIndex
= (DWORD
*)&lpbmi
->bmiColors
[0];
675 for (i
=0; i
<nNumColors
; i
++) {
676 lpRGB
[i
].rgbRed
= palObj
->logpalette
->palPalEntry
[*lpIndex
].peRed
;
677 lpRGB
[i
].rgbGreen
= palObj
->logpalette
->palPalEntry
[*lpIndex
].peGreen
;
678 lpRGB
[i
].rgbBlue
= palObj
->logpalette
->palPalEntry
[*lpIndex
].peBlue
;
681 // RELEASEDCINFO(hDC);
682 // RELEASEPALETTEINFO(hPalette);
686 HPALETTE
BuildDIBPalette(BITMAPINFO
*bmi
, PINT paletteType
)
690 // Determine Bits Per Pixel
691 bits
= bmi
->bmiHeader
.biBitCount
;
693 // Determine paletteType from Bits Per Pixel
696 *paletteType
= PAL_INDEXED
;
700 *paletteType
= PAL_BITFIELDS
;
702 *paletteType
= PAL_RGB
; // FIXME: This could be BGR, must still check
705 return EngCreatePalette(*paletteType
, bmi
->bmiHeader
.biClrUsed
, bmi
->bmiColors
, 0, 0, 0);