4 * ReactOS W32 Subsystem
5 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 IntSetDIBColorTable(HDC hDC
, UINT StartIndex
, UINT Entries
, CONST RGBQUAD
*Colors
)
35 if (!(dc
= DC_LockDc(hDC
))) return 0;
42 BitmapObj
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
43 if (BitmapObj
== NULL
)
46 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
50 if (BitmapObj
->dib
== NULL
)
52 BITMAPOBJ_UnlockBitmap(BitmapObj
);
54 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
58 if (BitmapObj
->dib
->dsBmih
.biBitCount
<= 8 &&
59 StartIndex
< (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
61 if (StartIndex
+ Entries
> (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
62 Entries
= (1 << BitmapObj
->dib
->dsBmih
.biBitCount
) - StartIndex
;
64 PalGDI
= PALETTE_LockPalette(BitmapObj
->hDIBPalette
);
66 for (Index
= StartIndex
;
67 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
70 PalGDI
->IndexedColors
[Index
].peRed
= Colors
[Index
- StartIndex
].rgbRed
;
71 PalGDI
->IndexedColors
[Index
].peGreen
= Colors
[Index
- StartIndex
].rgbGreen
;
72 PalGDI
->IndexedColors
[Index
].peBlue
= Colors
[Index
- StartIndex
].rgbBlue
;
74 PALETTE_UnlockPalette(PalGDI
);
79 BITMAPOBJ_UnlockBitmap(BitmapObj
);
86 IntGetDIBColorTable(HDC hDC
, UINT StartIndex
, UINT Entries
, RGBQUAD
*Colors
)
93 if (!(dc
= DC_LockDc(hDC
))) return 0;
100 BitmapObj
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
101 if (BitmapObj
== NULL
)
104 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
108 if (BitmapObj
->dib
== NULL
)
110 BITMAPOBJ_UnlockBitmap(BitmapObj
);
112 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
116 if (BitmapObj
->dib
->dsBmih
.biBitCount
<= 8 &&
117 StartIndex
< (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
119 if (StartIndex
+ Entries
> (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
120 Entries
= (1 << BitmapObj
->dib
->dsBmih
.biBitCount
) - StartIndex
;
122 PalGDI
= PALETTE_LockPalette(BitmapObj
->hDIBPalette
);
124 for (Index
= StartIndex
;
125 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
128 Colors
[Index
- StartIndex
].rgbRed
= PalGDI
->IndexedColors
[Index
].peRed
;
129 Colors
[Index
- StartIndex
].rgbGreen
= PalGDI
->IndexedColors
[Index
].peGreen
;
130 Colors
[Index
- StartIndex
].rgbBlue
= PalGDI
->IndexedColors
[Index
].peBlue
;
132 PALETTE_UnlockPalette(PalGDI
);
137 BITMAPOBJ_UnlockBitmap(BitmapObj
);
143 // Converts a DIB to a device-dependent bitmap
151 CONST BITMAPINFO
*bmi
,
155 HBITMAP SourceBitmap
;
158 SURFOBJ
*DestSurf
, *SourceSurf
;
165 HPALETTE DDB_Palette
, DIB_Palette
;
166 ULONG DDB_Palette_Type
, DIB_Palette_Type
;
170 if (!(bitmap
= BITMAPOBJ_LockBitmap(hBitmap
)))
176 //if (ColorUse == DIB_PAL_COLORS)
177 // lpRGB = DIB_MapPaletteColors(hDC, bmi);
179 // lpRGB = &bmi->bmiColors;
181 DestSurf
= &bitmap
->SurfObj
;
183 // Create source surface
184 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
185 SourceSize
.cy
= ScanLines
;
187 // Determine width of DIB
188 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
);
190 SourceBitmap
= EngCreateBitmap(SourceSize
,
192 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
193 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
195 if (0 == SourceBitmap
)
197 BITMAPOBJ_UnlockBitmap(bitmap
);
198 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
202 SourceSurf
= EngLockSurface((HSURF
)SourceBitmap
);
203 if (NULL
== SourceSurf
)
205 EngDeleteSurface((HSURF
)SourceBitmap
);
206 BITMAPOBJ_UnlockBitmap(bitmap
);
207 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
211 // Destination palette obtained from the hDC
212 hDCPalette
= PALETTE_LockPalette(((GDIDEVICE
*)DC
->pPDev
)->DevInfo
.hpalDefault
);
213 if (NULL
== hDCPalette
)
215 EngUnlockSurface(SourceSurf
);
216 EngDeleteSurface((HSURF
)SourceBitmap
);
217 BITMAPOBJ_UnlockBitmap(bitmap
);
218 SetLastWin32Error(ERROR_INVALID_HANDLE
);
221 DDB_Palette_Type
= hDCPalette
->Mode
;
222 DDB_Palette
= ((GDIDEVICE
*)DC
->pPDev
)->DevInfo
.hpalDefault
;
223 PALETTE_UnlockPalette(hDCPalette
);
225 // Source palette obtained from the BITMAPINFO
226 DIB_Palette
= BuildDIBPalette ( (PBITMAPINFO
)bmi
, (PINT
)&DIB_Palette_Type
);
227 if (NULL
== DIB_Palette
)
229 EngUnlockSurface(SourceSurf
);
230 EngDeleteSurface((HSURF
)SourceBitmap
);
231 BITMAPOBJ_UnlockBitmap(bitmap
);
232 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
236 // Determine XLATEOBJ for color translation
237 XlateObj
= IntEngCreateXlate(DDB_Palette_Type
, DIB_Palette_Type
, DDB_Palette
, DIB_Palette
);
238 if (NULL
== XlateObj
)
240 PALETTE_FreePalette(DIB_Palette
);
241 EngUnlockSurface(SourceSurf
);
242 EngDeleteSurface((HSURF
)SourceBitmap
);
243 BITMAPOBJ_UnlockBitmap(bitmap
);
244 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
252 // Determine destination rectangle
254 DestRect
.top
= abs(bmi
->bmiHeader
.biHeight
) - StartScan
- ScanLines
;
255 DestRect
.right
= SourceSize
.cx
;
256 DestRect
.bottom
= DestRect
.top
+ ScanLines
;
258 copyBitsResult
= EngCopyBits(DestSurf
, SourceSurf
, NULL
, XlateObj
, &DestRect
, &ZeroPoint
);
260 // If it succeeded, return number of scanlines copies
261 if(copyBitsResult
== TRUE
)
263 result
= SourceSize
.cy
- 1;
267 EngDeleteXlate(XlateObj
);
268 PALETTE_FreePalette(DIB_Palette
);
269 EngUnlockSurface(SourceSurf
);
270 EngDeleteSurface((HSURF
)SourceBitmap
);
272 // if (ColorUse == DIB_PAL_COLORS)
273 // WinFree((LPSTR)lpRGB);
275 BITMAPOBJ_UnlockBitmap(bitmap
);
280 // Converts a DIB to a device-dependent bitmap
288 CONST BITMAPINFO
*bmi
,
297 SetLastWin32Error(ERROR_INVALID_HANDLE
);
306 Ret
= IntSetDIBits(Dc
, hBitmap
, StartScan
, ScanLines
, Bits
, bmi
, ColorUse
);
316 NtGdiSetDIBitsToDeviceInternal(
331 IN BOOL bTransformCoordinates
,
332 IN OPTIONAL HANDLE hcmXform
336 NTSTATUS Status
= STATUS_SUCCESS
;
338 HBITMAP hSourceBitmap
;
339 SURFOBJ
*pDestSurf
, *pSourceSurf
;
344 XLATEOBJ
*XlateObj
= NULL
;
346 pDC
= DC_LockDc(hDC
);
349 SetLastWin32Error(ERROR_INVALID_HANDLE
);
358 pDestSurf
= EngLockSurface((HSURF
)pDC
->w
.hBitmap
);
361 /* FIXME: SetLastError ? */
366 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
367 SourceSize
.cy
= ScanLines
;
368 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
);
372 rcDest
.right
= XDest
+ Width
;
373 rcDest
.bottom
= YDest
+ Height
;
377 /* Enter SEH, as the bits are user mode */
380 ProbeForRead(Bits
, DIBWidth
* abs(bmi
->bmiHeader
.biHeight
), 1);
381 hSourceBitmap
= EngCreateBitmap(SourceSize
,
383 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
384 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
388 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
389 Status
= STATUS_NO_MEMORY
;
393 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
396 EngDeleteSurface((HSURF
)hSourceBitmap
);
397 Status
= STATUS_UNSUCCESSFUL
;
401 /* FIXME: handle XlateObj */
405 Status
= IntEngBitBlt(pDestSurf
,
415 ROP3_TO_ROP4(SRCCOPY
));
417 EngUnlockSurface(pSourceSurf
);
418 EngDeleteSurface((HSURF
)hSourceBitmap
);
422 Status
= _SEH_GetExceptionCode();
426 if (NT_SUCCESS(Status
))
428 /* FIXME: Should probably be only the number of lines actually copied */
432 EngUnlockSurface(pDestSurf
);
439 /* Converts a device-dependent bitmap to a DIB */
441 NtGdiGetDIBitsInternal(HDC hDC
,
452 BITMAPOBJ
*BitmapObj
= NULL
;
453 HBITMAP hDestBitmap
= NULL
;
454 HPALETTE hSourcePalette
= NULL
;
455 HPALETTE hDestPalette
= NULL
;
456 PPALGDI SourcePalette
= NULL
;
457 PPALGDI DestPalette
= NULL
;
458 NTSTATUS Status
= STATUS_SUCCESS
;
460 BOOL bPaletteMatch
= FALSE
;
462 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
464 /* Get handle for the palette in DC. */
466 if (Dc
== NULL
) return 0;
472 /* Source palette obtained from the windows hdc */
473 hSourcePalette
= Dc
->w
.hPalette
;
476 /* don't do anything if we fail this */
477 if (Usage
!= DIB_RGB_COLORS
&& Usage
!= DIB_PAL_COLORS
)
480 /* Get a pointer to the source bitmap object */
481 BitmapObj
= BITMAPOBJ_LockBitmap(hBitmap
);
482 if (BitmapObj
== NULL
)
485 /* fill out the BITMAPINFO struct */
490 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
492 BITMAPCOREHEADER
* coreheader
;
494 ProbeForWrite(Info
, sizeof(BITMAPINFO
), 1);
496 coreheader
= (BITMAPCOREHEADER
*) Info
;
497 coreheader
->bcWidth
=BitmapObj
->SurfObj
.sizlBitmap
.cx
;
498 coreheader
->bcPlanes
= 1;
499 coreheader
->bcBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
500 coreheader
->bcHeight
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
502 if (BitmapObj
->SurfObj
.lDelta
> 0)
503 coreheader
->bcHeight
= -coreheader
->bcHeight
;
506 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
508 ProbeForWrite(Info
, sizeof(BITMAPINFO
), 1);
510 Info
->bmiHeader
.biWidth
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
511 Info
->bmiHeader
.biHeight
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
512 /* Report negtive height for top-down bitmaps. */
513 if (BitmapObj
->SurfObj
.lDelta
> 0)
514 Info
->bmiHeader
.biHeight
= -Info
->bmiHeader
.biHeight
;
515 Info
->bmiHeader
.biPlanes
= 1;
516 Info
->bmiHeader
.biBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
517 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
519 switch (BitmapObj
->SurfObj
.iBitmapFormat
)
527 Info
->bmiHeader
.biCompression
= BI_RGB
;
530 Info
->bmiHeader
.biCompression
= BI_RLE4
;
533 Info
->bmiHeader
.biCompression
= BI_RLE8
;
536 Info
->bmiHeader
.biCompression
= BI_JPEG
;
539 Info
->bmiHeader
.biCompression
= BI_PNG
;
543 Info
->bmiHeader
.biSizeImage
= BitmapObj
->SurfObj
.cjBits
;
544 Info
->bmiHeader
.biXPelsPerMeter
= 0; /* FIXME */
545 Info
->bmiHeader
.biYPelsPerMeter
= 0; /* FIXME */
546 Info
->bmiHeader
.biClrUsed
= 0;
547 Info
->bmiHeader
.biClrImportant
= 1 << Info
->bmiHeader
.biBitCount
; /* FIXME */
553 Status
= _SEH_GetExceptionCode();
557 if (NT_SUCCESS(Status
))
559 Result
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
565 ULONG SourcePaletteType
= 0;
566 ULONG DestPaletteType
;
572 ProbeForRead(Info
, sizeof(BITMAPINFO
), 1);
574 if (Info
->bmiHeader
.biBitCount
== BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
))
576 hDestPalette
= hSourcePalette
;
577 bPaletteMatch
= TRUE
;
580 hDestPalette
= BuildDIBPalette(Info
, (PINT
)&DestPaletteType
); //hDestPalette = Dc->DevInfo->hpalDefault;
582 SourcePalette
= PALETTE_LockPalette(hSourcePalette
);
583 /* FIXME - SourcePalette can be NULL!!! Don't assert here! */
584 ASSERT(SourcePalette
);
585 SourcePaletteType
= SourcePalette
->Mode
;
586 PALETTE_UnlockPalette(SourcePalette
);
590 DestPalette
= PALETTE_LockPalette(hDestPalette
);
591 /* FIXME - DestPalette can be NULL!!!! Don't assert here!!! */
592 DPRINT("DestPalette : %p\n", DestPalette
);
594 DestPaletteType
= DestPalette
->Mode
;
598 DestPalette
= SourcePalette
;
602 /* FIXME: This is largely incomplete. */
603 if (Info
->bmiHeader
.biBitCount
<= 8)
605 if (Usage
== DIB_RGB_COLORS
)
608 Index
< (1 << Info
->bmiHeader
.biBitCount
) && Index
< DestPalette
->NumColors
;
611 Info
->bmiColors
[Index
].rgbRed
= DestPalette
->IndexedColors
[Index
].peRed
;
612 Info
->bmiColors
[Index
].rgbGreen
= DestPalette
->IndexedColors
[Index
].peGreen
;
613 Info
->bmiColors
[Index
].rgbBlue
= DestPalette
->IndexedColors
[Index
].peBlue
;
617 if (Usage
== DIB_PAL_COLORS
)
619 DPRINT1("GetDIBits with DIB_PAL_COLORS isn't implemented yet\n");
624 PALETTE_UnlockPalette(DestPalette
);
626 /* Create the destination bitmap to for the copy operation */
627 if (StartScan
> BitmapObj
->SurfObj
.sizlBitmap
.cy
)
629 _SEH_YIELD(goto cleanup
);
633 ScanLines
= min(ScanLines
, BitmapObj
->SurfObj
.sizlBitmap
.cy
- StartScan
);
634 DestSize
.cx
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
635 DestSize
.cy
= ScanLines
;
639 ProbeForWrite(Bits
, sizeof(BitmapObj
->SurfObj
.cjBits
), 1);
641 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
643 BITMAPCOREHEADER
* coreheader
= (BITMAPCOREHEADER
*) Info
;
645 hDestBitmap
= EngCreateBitmap(DestSize
,
646 DIB_GetDIBWidthBytes(DestSize
.cx
, coreheader
->bcBitCount
),
647 BitmapFormat(coreheader
->bcBitCount
, BI_RGB
),
648 0 < coreheader
->bcHeight
? 0 : BMF_TOPDOWN
,
652 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
654 hDestBitmap
= EngCreateBitmap(DestSize
,
655 /* DIB_GetDIBWidthBytes(DestSize.cx, Info->bmiHeader.biBitCount), */
656 DestSize
.cx
* (Info
->bmiHeader
.biBitCount
>> 3), /* HACK */
657 BitmapFormat(Info
->bmiHeader
.biBitCount
, Info
->bmiHeader
.biCompression
),
658 0 < Info
->bmiHeader
.biHeight
? 0 : BMF_TOPDOWN
,
662 if (hDestBitmap
== NULL
)
663 _SEH_YIELD(goto cleanup
);
668 Status
= _SEH_GetExceptionCode();
672 if (NT_SUCCESS(Status
))
675 SURFOBJ
*DestSurfObj
;
678 XlateObj
= IntEngCreateXlate(DestPaletteType
,
684 SourcePoint
.y
= BitmapObj
->SurfObj
.sizlBitmap
.cy
- (StartScan
+ ScanLines
);
686 /* Determine destination rectangle */
689 DestRect
.right
= DestSize
.cx
;
690 DestRect
.bottom
= DestSize
.cy
;
692 DestSurfObj
= EngLockSurface((HSURF
)hDestBitmap
);
694 if (EngCopyBits(DestSurfObj
,
704 EngDeleteXlate(XlateObj
);
705 EngUnlockSurface(DestSurfObj
);
710 if (hDestBitmap
!= NULL
)
711 EngDeleteSurface((HSURF
)hDestBitmap
);
713 if (hDestPalette
!= NULL
&& bPaletteMatch
== FALSE
)
714 PALETTE_FreePalette(hDestPalette
);
716 BITMAPOBJ_UnlockBitmap(BitmapObj
);
718 DPRINT("leaving NtGdiGetDIBitsInternal\n");
723 INT STDCALL
NtGdiStretchDIBits(HDC hDC
,
733 CONST BITMAPINFO
*BitsInfo
,
737 HBITMAP hBitmap
, hOldBitmap
;
739 HPALETTE hPal
= NULL
;
741 if (!Bits
|| !BitsInfo
)
743 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
747 hdcMem
= NtGdiCreateCompatibleDC(hDC
);
748 hBitmap
= NtGdiCreateCompatibleBitmap(hDC
, BitsInfo
->bmiHeader
.biWidth
,
749 BitsInfo
->bmiHeader
.biHeight
);
750 hOldBitmap
= NtGdiSelectObject(hdcMem
, hBitmap
);
752 if(Usage
== DIB_PAL_COLORS
)
754 hPal
= NtGdiGetDCObject(hDC
, GDI_OBJECT_TYPE_PALETTE
);
755 hPal
= NtUserSelectPalette(hdcMem
, hPal
, FALSE
);
758 if (BitsInfo
->bmiHeader
.biCompression
== BI_RLE4
||
759 BitsInfo
->bmiHeader
.biCompression
== BI_RLE8
)
761 /* copy existing bitmap from destination dc */
762 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
763 NtGdiBitBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
764 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, ROP
, 0, 0);
766 NtGdiStretchBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
767 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
771 NtGdiSetDIBits(hdcMem
, hBitmap
, 0, BitsInfo
->bmiHeader
.biHeight
, Bits
,
774 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
775 left (negative biHeight) */
776 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
777 NtGdiBitBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
778 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
781 NtGdiStretchBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
782 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
783 SrcWidth
, SrcHeight
, ROP
, 0);
786 NtUserSelectPalette(hdcMem
, hPal
, FALSE
);
788 NtGdiSelectObject(hdcMem
, hOldBitmap
);
789 NtGdiDeleteObjectApp(hdcMem
);
790 NtGdiDeleteObject(hBitmap
);
797 IntCreateDIBitmap(PDC Dc
, const BITMAPINFOHEADER
*header
,
798 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
813 if (DIB_GetBitmapInfo( header
, &width
, &height
, &planes
, &bpp
, &compr
, &dibsize
) == -1) return 0;
816 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
817 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
819 if (bpp
!= 1) fColor
= TRUE
;
820 else if ((coloruse
!= DIB_RGB_COLORS
) ||
821 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
824 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
826 const RGBQUAD
*rgb
= data
->bmiColors
;
827 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
829 // Check if the first color of the colormap is black
830 if ((col
== RGB(0, 0, 0)))
833 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
835 // If the second color is white, create a monochrome bitmap
836 fColor
= (col
!= RGB(0xff,0xff,0xff));
840 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
842 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
843 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
845 if ((col
== RGB(0,0,0)))
848 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
849 fColor
= (col
!= RGB(0xff,0xff,0xff));
855 DPRINT("(%ld): wrong size for data\n", data
->bmiHeader
.biSize
);
860 // Now create the bitmap
863 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
868 size
.cy
= abs(height
);
870 handle
= IntCreateBitmap(size
, DIB_GetDIBWidthBytes(width
, 1), BMF_1BPP
,
871 (height
< 0 ? BMF_TOPDOWN
: 0) | BMF_NOZEROINIT
,
878 if (NULL
!= handle
&& CBM_INIT
== init
)
880 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
886 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
887 // The DDB that is created will be whatever bit depth your reference DC is
888 HBITMAP STDCALL
NtGdiCreateDIBitmap(HDC hDc
, const BITMAPINFOHEADER
*Header
,
889 DWORD Init
, LPCVOID Bits
, const BITMAPINFO
*Data
,
900 if (Header
->biSize
== 0)
907 BITMAPINFOHEADER
*change_Header
= (BITMAPINFOHEADER
*)Header
;
908 hDc
= IntGdiCreateDC(NULL
, NULL
, NULL
, NULL
,FALSE
);
911 SetLastWin32Error(ERROR_INVALID_HANDLE
);
917 NtGdiDeleteObjectApp(hDc
);
918 SetLastWin32Error(ERROR_INVALID_HANDLE
);
922 change_Header
->biBitCount
= 1;
923 change_Header
->biPlanes
= 1;
925 Bmp
= IntCreateDIBitmap(Dc
, Header
, Init
, Bits
, Data
, ColorUse
);
927 NtGdiDeleteObjectApp(hDc
);
934 SetLastWin32Error(ERROR_INVALID_HANDLE
);
937 Bmp
= IntCreateDIBitmap(Dc
, Header
, Init
, Bits
, Data
, ColorUse
);
946 HBITMAP STDCALL
NtGdiCreateDIBSection(HDC hDC
,
947 IN OPTIONAL HANDLE hSection
,
953 IN ULONG_PTR dwColorSpace
,
958 BOOL bDesktopDC
= FALSE
;
960 // If the reference hdc is null, take the desktop dc
963 hDC
= NtGdiCreateCompatibleDC(0);
967 if ((dc
= DC_LockDc(hDC
)))
969 hbitmap
= DIB_CreateDIBSection ( dc
, (BITMAPINFO
*)bmi
, Usage
, Bits
,
970 hSection
, dwOffset
, 0);
975 SetLastWin32Error(ERROR_INVALID_HANDLE
);
979 NtGdiDeleteObjectApp(hDC
);
985 DIB_CreateDIBSection(
986 PDC dc
, BITMAPINFO
*bmi
, UINT usage
,
987 LPVOID
*bits
, HANDLE section
,
988 DWORD offset
, DWORD ovr_pitch
)
991 BITMAPOBJ
*bmp
= NULL
;
992 DIBSECTION
*dib
= NULL
;
994 // Fill BITMAP32 structure with DIB data
995 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1003 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1004 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1005 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1007 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1009 bm
.bmWidth
= bi
->biWidth
;
1010 bm
.bmHeight
= effHeight
;
1011 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
1013 bm
.bmPlanes
= bi
->biPlanes
;
1014 bm
.bmBitsPixel
= bi
->biBitCount
;
1017 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1018 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1019 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
1020 ? bi
->biSizeImage
: (ULONG
) (bm
.bmWidthBytes
* effHeight
);
1024 /* bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
1025 0L, offset, totalSize); */
1026 DbgPrint("DIB_CreateDIBSection: Cannot yet handle section DIBs\n");
1027 SetLastWin32Error(ERROR_CALL_NOT_IMPLEMENTED
);
1030 else if (ovr_pitch
&& offset
)
1031 bm
.bmBits
= (LPVOID
) offset
;
1034 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1037 if(usage
== DIB_PAL_COLORS
)
1038 lpRGB
= DIB_MapPaletteColors(dc
, bmi
);
1040 lpRGB
= bmi
->bmiColors
;
1042 // Allocate Memory for DIB and fill structure
1045 dib
= ExAllocatePoolWithTag(PagedPool
, sizeof(DIBSECTION
), TAG_DIB
);
1046 if (dib
!= NULL
) RtlZeroMemory(dib
, sizeof(DIBSECTION
));
1053 dib
->dsBmih
.biSizeImage
= totalSize
;
1055 /* Set dsBitfields values */
1056 if ( usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
1058 dib
->dsBitfields
[0] = dib
->dsBitfields
[1] = dib
->dsBitfields
[2] = 0;
1060 else switch(bi
->biBitCount
)
1063 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)lpRGB
: 0x7c00;
1064 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 1) : 0x03e0;
1065 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 2) : 0x001f; break;
1068 dib
->dsBitfields
[0] = 0xff0000;
1069 dib
->dsBitfields
[1] = 0x00ff00;
1070 dib
->dsBitfields
[2] = 0x0000ff;
1074 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)lpRGB
: 0xff0000;
1075 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 1) : 0x00ff00;
1076 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 2) : 0x0000ff;
1079 dib
->dshSection
= section
;
1080 dib
->dsOffset
= offset
;
1083 // Create Device Dependent Bitmap and add DIB pointer
1086 Size
.cx
= bm
.bmWidth
;
1087 Size
.cy
= abs(bm
.bmHeight
);
1088 res
= IntCreateBitmap(Size
, bm
.bmWidthBytes
,
1089 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1090 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1091 (bi
->biHeight
< 0 ? BMF_TOPDOWN
: 0),
1095 if (lpRGB
!= bmi
->bmiColors
)
1099 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
1102 bmp
= BITMAPOBJ_LockBitmap(res
);
1105 if (lpRGB
!= bmi
->bmiColors
)
1109 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1110 NtGdiDeleteObject(bmp
);
1113 bmp
->dib
= (DIBSECTION
*) dib
;
1114 bmp
->flFlags
= BITMAPOBJ_IS_APIBITMAP
;
1116 /* WINE NOTE: WINE makes use of a colormap, which is a color translation table between the DIB and the X physical
1117 device. Obviously, this is left out of the ReactOS implementation. Instead, we call
1118 NtGdiSetDIBColorTable. */
1119 if(bi
->biBitCount
== 1) { Entries
= 2; } else
1120 if(bi
->biBitCount
== 4) { Entries
= 16; } else
1121 if(bi
->biBitCount
== 8) { Entries
= 256; }
1124 bmp
->hDIBPalette
= PALETTE_AllocPaletteIndexedRGB(Entries
, lpRGB
);
1126 bmp
->hDIBPalette
= PALETTE_AllocPalette(PAL_BITFIELDS
, 0, NULL
,
1127 dib
->dsBitfields
[0],
1128 dib
->dsBitfields
[1],
1129 dib
->dsBitfields
[2]);
1132 // Clean up in case of errors
1133 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
)
1135 DPRINT("got an error res=%08x, bmp=%p, dib=%p, bm.bmBits=%p\n", res
, bmp
, dib
, bm
.bmBits
);
1139 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
1141 VirtualFree(bm.bmBits, 0L, MEM_RELEASE), bm.bmBits = NULL;
1144 if (dib
) { ExFreePool(dib
); dib
= NULL
; }
1145 if (bmp
) { bmp
= NULL
; }
1146 if (res
) { BITMAPOBJ_FreeBitmap(res
); res
= 0; }
1149 if (lpRGB
!= bmi
->bmiColors
)
1156 BITMAPOBJ_UnlockBitmap(bmp
);
1159 // Return BITMAP handle and storage location
1160 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1168 /***********************************************************************
1169 * DIB_GetDIBWidthBytes
1171 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1172 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
1173 * 11/16/1999 (RJJ) lifted from wine
1175 INT FASTCALL
DIB_GetDIBWidthBytes (INT width
, INT depth
)
1177 return ((width
* depth
+ 31) & ~31) >> 3;
1180 /***********************************************************************
1181 * DIB_GetDIBImageBytes
1183 * Return the number of bytes used to hold the image in a DIB bitmap.
1184 * 11/16/1999 (RJJ) lifted from wine
1187 INT STDCALL
DIB_GetDIBImageBytes (INT width
, INT height
, INT depth
)
1189 return DIB_GetDIBWidthBytes( width
, depth
) * (height
< 0 ? -height
: height
);
1192 /***********************************************************************
1193 * DIB_BitmapInfoSize
1195 * Return the size of the bitmap info structure including color table.
1196 * 11/16/1999 (RJJ) lifted from wine
1199 INT FASTCALL
DIB_BitmapInfoSize (const BITMAPINFO
* info
, WORD coloruse
)
1203 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1205 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
1206 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1207 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1209 else /* assume BITMAPINFOHEADER */
1211 colors
= info
->bmiHeader
.biClrUsed
;
1212 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
1213 return sizeof(BITMAPINFOHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1218 * DIB_GetBitmapInfo is complete copy of wine cvs 2/9-2006
1219 * from file dib.c from gdi32.dll or orginal version
1220 * did not calc the info right for some headers.
1224 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
,
1233 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1235 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
1236 *width
= core
->bcWidth
;
1237 *height
= core
->bcHeight
;
1238 *planes
= core
->bcPlanes
;
1239 *bpp
= core
->bcBitCount
;
1245 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
1247 *width
= header
->biWidth
;
1248 *height
= header
->biHeight
;
1249 *planes
= header
->biPlanes
;
1250 *bpp
= header
->biBitCount
;
1251 *compr
= header
->biCompression
;
1252 *size
= header
->biSizeImage
;
1256 if (header
->biSize
== sizeof(BITMAPV4HEADER
))
1258 BITMAPV4HEADER
*v4hdr
= (BITMAPV4HEADER
*)header
;
1259 *width
= v4hdr
->bV4Width
;
1260 *height
= v4hdr
->bV4Height
;
1261 *planes
= v4hdr
->bV4Planes
;
1262 *bpp
= v4hdr
->bV4BitCount
;
1263 *compr
= v4hdr
->bV4V4Compression
;
1264 *size
= v4hdr
->bV4SizeImage
;
1268 if (header
->biSize
== sizeof(BITMAPV5HEADER
))
1270 BITMAPV5HEADER
*v5hdr
= (BITMAPV5HEADER
*)header
;
1271 *width
= v5hdr
->bV5Width
;
1272 *height
= v5hdr
->bV5Height
;
1273 *planes
= v5hdr
->bV5Planes
;
1274 *bpp
= v5hdr
->bV5BitCount
;
1275 *compr
= v5hdr
->bV5Compression
;
1276 *size
= v5hdr
->bV5SizeImage
;
1279 DPRINT("(%ld): wrong size for header\n", header
->biSize
);
1283 // Converts a Device Independent Bitmap (DIB) to a Device Dependant Bitmap (DDB)
1284 // The specified Device Context (DC) defines what the DIB should be converted to
1285 PBITMAPOBJ FASTCALL
DIBtoDDB(HGLOBAL hPackedDIB
, HDC hdc
) // FIXME: This should be removed. All references to this function should
1286 // change to NtGdiSetDIBits
1289 PBITMAPOBJ pBmp
= NULL
;
1291 LPBYTE pbits
= NULL
;
1293 // Get a pointer to the packed DIB's data
1294 // pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
1297 pbits
= (LPBYTE
)(dib
+ DIB_BitmapInfoSize((BITMAPINFO
*)&dib
->dsBmih
, DIB_RGB_COLORS
));
1299 // Create a DDB from the DIB
1300 hBmp
= NtGdiCreateDIBitmap ( hdc
, &dib
->dsBmih
, CBM_INIT
,
1301 (LPVOID
)pbits
, (BITMAPINFO
*)&dib
->dsBmih
, DIB_RGB_COLORS
);
1303 // GlobalUnlock(hPackedDIB);
1305 // Retrieve the internal Pixmap from the DDB
1306 pBmp
= BITMAPOBJ_LockBitmap(hBmp
);
1312 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1319 palGDI
= PALETTE_LockPalette(dc
->w
.hPalette
);
1323 // RELEASEDCINFO(hDC);
1327 if (palGDI
->Mode
!= PAL_INDEXED
)
1329 PALETTE_UnlockPalette(palGDI
);
1330 palGDI
= PALETTE_LockPalette(dc
->PalIndexed
);
1331 if (palGDI
->Mode
!= PAL_INDEXED
)
1337 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1338 if (lpbmi
->bmiHeader
.biClrUsed
)
1340 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1343 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1346 PALETTE_UnlockPalette(palGDI
);
1350 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1352 for (i
= 0; i
< nNumColors
; i
++)
1354 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1355 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1356 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1359 // RELEASEDCINFO(hDC);
1360 PALETTE_UnlockPalette(palGDI
);
1366 BuildDIBPalette (CONST BITMAPINFO
*bmi
, PINT paletteType
)
1370 PALETTEENTRY
*palEntries
= NULL
;
1372 ULONG RedMask
, GreenMask
, BlueMask
;
1374 // Determine Bits Per Pixel
1375 bits
= bmi
->bmiHeader
.biBitCount
;
1377 // Determine paletteType from Bits Per Pixel
1380 *paletteType
= PAL_INDEXED
;
1381 RedMask
= GreenMask
= BlueMask
= 0;
1385 *paletteType
= PAL_BITFIELDS
;
1392 *paletteType
= PAL_BGR
;
1394 GreenMask
= 0x00ff00;
1395 BlueMask
= 0x0000ff;
1398 if (bmi
->bmiHeader
.biClrUsed
== 0)
1400 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1404 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1407 if (PAL_INDEXED
== *paletteType
)
1409 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)bmi
->bmiColors
);
1413 hPal
= PALETTE_AllocPalette(*paletteType
, ColorCount
,
1414 (ULONG
*) palEntries
,
1415 RedMask
, GreenMask
, BlueMask
);