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(DC
->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
= DC
->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 ProbeForWrite(Info
, sizeof(BITMAPINFO
), 1);
494 BITMAPCOREHEADER
* coreheader
= (BITMAPCOREHEADER
*) Info
;
495 coreheader
->bcWidth
=BitmapObj
->SurfObj
.sizlBitmap
.cx
;
496 coreheader
->bcPlanes
= 1;
497 coreheader
->bcBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
498 coreheader
->bcHeight
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
500 if (BitmapObj
->SurfObj
.lDelta
> 0)
501 coreheader
->bcHeight
= -coreheader
->bcHeight
;
504 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
506 ProbeForWrite(Info
, sizeof(BITMAPINFO
), 1);
508 Info
->bmiHeader
.biWidth
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
509 Info
->bmiHeader
.biHeight
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
510 /* Report negtive height for top-down bitmaps. */
511 if (BitmapObj
->SurfObj
.lDelta
> 0)
512 Info
->bmiHeader
.biHeight
= -Info
->bmiHeader
.biHeight
;
513 Info
->bmiHeader
.biPlanes
= 1;
514 Info
->bmiHeader
.biBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
515 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
517 switch (BitmapObj
->SurfObj
.iBitmapFormat
)
525 Info
->bmiHeader
.biCompression
= BI_RGB
;
528 Info
->bmiHeader
.biCompression
= BI_RLE4
;
531 Info
->bmiHeader
.biCompression
= BI_RLE8
;
534 Info
->bmiHeader
.biCompression
= BI_JPEG
;
537 Info
->bmiHeader
.biCompression
= BI_PNG
;
541 Info
->bmiHeader
.biSizeImage
= BitmapObj
->SurfObj
.cjBits
;
542 Info
->bmiHeader
.biXPelsPerMeter
= 0; /* FIXME */
543 Info
->bmiHeader
.biYPelsPerMeter
= 0; /* FIXME */
544 Info
->bmiHeader
.biClrUsed
= 0;
545 Info
->bmiHeader
.biClrImportant
= 1 << Info
->bmiHeader
.biBitCount
; /* FIXME */
551 Status
= _SEH_GetExceptionCode();
555 if (NT_SUCCESS(Status
))
557 Result
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
563 ULONG SourcePaletteType
= 0;
564 ULONG DestPaletteType
;
570 ProbeForRead(Info
, sizeof(BITMAPINFO
), 1);
572 if (Info
->bmiHeader
.biBitCount
== BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
))
574 hDestPalette
= hSourcePalette
;
575 bPaletteMatch
= TRUE
;
578 hDestPalette
= BuildDIBPalette(Info
, (PINT
)&DestPaletteType
); //hDestPalette = Dc->DevInfo->hpalDefault;
580 SourcePalette
= PALETTE_LockPalette(hSourcePalette
);
581 /* FIXME - SourcePalette can be NULL!!! Don't assert here! */
582 ASSERT(SourcePalette
);
583 SourcePaletteType
= SourcePalette
->Mode
;
584 PALETTE_UnlockPalette(SourcePalette
);
588 DestPalette
= PALETTE_LockPalette(hDestPalette
);
589 /* FIXME - DestPalette can be NULL!!!! Don't assert here!!! */
590 DPRINT("DestPalette : %p\n", DestPalette
);
592 DestPaletteType
= DestPalette
->Mode
;
596 DestPalette
= SourcePalette
;
600 /* FIXME: This is largely incomplete. */
601 if (Info
->bmiHeader
.biBitCount
<= 8)
603 if (Usage
== DIB_RGB_COLORS
)
606 Index
< (1 << Info
->bmiHeader
.biBitCount
) && Index
< DestPalette
->NumColors
;
609 Info
->bmiColors
[Index
].rgbRed
= DestPalette
->IndexedColors
[Index
].peRed
;
610 Info
->bmiColors
[Index
].rgbGreen
= DestPalette
->IndexedColors
[Index
].peGreen
;
611 Info
->bmiColors
[Index
].rgbBlue
= DestPalette
->IndexedColors
[Index
].peBlue
;
615 if (Usage
== DIB_PAL_COLORS
)
617 DPRINT1("GetDIBits with DIB_PAL_COLORS isn't implemented yet\n");
622 PALETTE_UnlockPalette(DestPalette
);
624 /* Create the destination bitmap to for the copy operation */
625 if (StartScan
> BitmapObj
->SurfObj
.sizlBitmap
.cy
)
627 _SEH_YIELD(goto cleanup
);
631 ScanLines
= min(ScanLines
, BitmapObj
->SurfObj
.sizlBitmap
.cy
- StartScan
);
632 DestSize
.cx
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
633 DestSize
.cy
= ScanLines
;
637 ProbeForWrite(Bits
, sizeof(BitmapObj
->SurfObj
.cjBits
), 1);
639 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
641 BITMAPCOREHEADER
* coreheader
= (BITMAPCOREHEADER
*) Info
;
643 hDestBitmap
= EngCreateBitmap(DestSize
,
644 DIB_GetDIBWidthBytes(DestSize
.cx
, coreheader
->bcBitCount
),
645 BitmapFormat(coreheader
->bcBitCount
, BI_RGB
),
646 0 < coreheader
->bcHeight
? 0 : BMF_TOPDOWN
,
650 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
652 hDestBitmap
= EngCreateBitmap(DestSize
,
653 /* DIB_GetDIBWidthBytes(DestSize.cx, Info->bmiHeader.biBitCount), */
654 DestSize
.cx
* (Info
->bmiHeader
.biBitCount
>> 3), /* HACK */
655 BitmapFormat(Info
->bmiHeader
.biBitCount
, Info
->bmiHeader
.biCompression
),
656 0 < Info
->bmiHeader
.biHeight
? 0 : BMF_TOPDOWN
,
660 if (hDestBitmap
== NULL
)
661 _SEH_YIELD(goto cleanup
);
666 Status
= _SEH_GetExceptionCode();
670 if (NT_SUCCESS(Status
))
673 SURFOBJ
*DestSurfObj
;
676 XlateObj
= IntEngCreateXlate(DestPaletteType
,
682 SourcePoint
.y
= BitmapObj
->SurfObj
.sizlBitmap
.cy
- (StartScan
+ ScanLines
);
684 /* Determine destination rectangle */
687 DestRect
.right
= DestSize
.cx
;
688 DestRect
.bottom
= DestSize
.cy
;
690 DestSurfObj
= EngLockSurface((HSURF
)hDestBitmap
);
692 if (EngCopyBits(DestSurfObj
,
702 EngDeleteXlate(XlateObj
);
703 EngUnlockSurface(DestSurfObj
);
708 if (hDestBitmap
!= NULL
)
709 EngDeleteSurface((HSURF
)hDestBitmap
);
711 if (hDestPalette
!= NULL
&& bPaletteMatch
== FALSE
)
712 PALETTE_FreePalette(hDestPalette
);
714 BITMAPOBJ_UnlockBitmap(BitmapObj
);
716 DPRINT("leaving NtGdiGetDIBitsInternal\n");
721 INT STDCALL
NtGdiStretchDIBits(HDC hDC
,
731 CONST BITMAPINFO
*BitsInfo
,
735 HBITMAP hBitmap
, hOldBitmap
;
737 HPALETTE hPal
= NULL
;
739 if (!Bits
|| !BitsInfo
)
741 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
745 hdcMem
= NtGdiCreateCompatibleDC(hDC
);
746 hBitmap
= NtGdiCreateCompatibleBitmap(hDC
, BitsInfo
->bmiHeader
.biWidth
,
747 BitsInfo
->bmiHeader
.biHeight
);
748 hOldBitmap
= NtGdiSelectObject(hdcMem
, hBitmap
);
750 if(Usage
== DIB_PAL_COLORS
)
752 hPal
= NtGdiGetDCObject(hDC
, GDI_OBJECT_TYPE_PALETTE
);
753 hPal
= NtUserSelectPalette(hdcMem
, hPal
, FALSE
);
756 if (BitsInfo
->bmiHeader
.biCompression
== BI_RLE4
||
757 BitsInfo
->bmiHeader
.biCompression
== BI_RLE8
)
759 /* copy existing bitmap from destination dc */
760 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
761 NtGdiBitBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
762 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, ROP
, 0, 0);
764 NtGdiStretchBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
765 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
769 NtGdiSetDIBits(hdcMem
, hBitmap
, 0, BitsInfo
->bmiHeader
.biHeight
, Bits
,
772 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
773 left (negative biHeight) */
774 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
775 NtGdiBitBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
776 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
779 NtGdiStretchBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
780 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
781 SrcWidth
, SrcHeight
, ROP
, 0);
784 NtUserSelectPalette(hdcMem
, hPal
, FALSE
);
786 NtGdiSelectObject(hdcMem
, hOldBitmap
);
787 NtGdiDeleteObjectApp(hdcMem
);
788 NtGdiDeleteObject(hBitmap
);
795 IntCreateDIBitmap(PDC Dc
, const BITMAPINFOHEADER
*header
,
796 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
811 if (DIB_GetBitmapInfo( header
, &width
, &height
, &planes
, &bpp
, &compr
, &dibsize
) == -1) return 0;
814 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
815 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
817 if (bpp
!= 1) fColor
= TRUE
;
818 else if ((coloruse
!= DIB_RGB_COLORS
) ||
819 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
822 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
824 const RGBQUAD
*rgb
= data
->bmiColors
;
825 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
827 // Check if the first color of the colormap is black
828 if ((col
== RGB(0, 0, 0)))
831 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
833 // If the second color is white, create a monochrome bitmap
834 fColor
= (col
!= RGB(0xff,0xff,0xff));
838 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
840 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
841 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
843 if ((col
== RGB(0,0,0)))
846 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
847 fColor
= (col
!= RGB(0xff,0xff,0xff));
853 DPRINT("(%ld): wrong size for data\n", data
->bmiHeader
.biSize
);
858 // Now create the bitmap
861 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
866 size
.cy
= abs(height
);
868 handle
= IntCreateBitmap(size
, DIB_GetDIBWidthBytes(width
, 1), BMF_1BPP
,
869 (height
< 0 ? BMF_TOPDOWN
: 0) | BMF_NOZEROINIT
,
876 if (NULL
!= handle
&& CBM_INIT
== init
)
878 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
884 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
885 // The DDB that is created will be whatever bit depth your reference DC is
886 HBITMAP STDCALL
NtGdiCreateDIBitmap(HDC hDc
, const BITMAPINFOHEADER
*Header
,
887 DWORD Init
, LPCVOID Bits
, const BITMAPINFO
*Data
,
898 if (Header
->biSize
== 0)
905 BITMAPINFOHEADER
*change_Header
= (BITMAPINFOHEADER
*)Header
;
906 hDc
= IntGdiCreateDC(NULL
, NULL
, NULL
, NULL
,FALSE
);
909 SetLastWin32Error(ERROR_INVALID_HANDLE
);
915 NtGdiDeleteObjectApp(hDc
);
916 SetLastWin32Error(ERROR_INVALID_HANDLE
);
920 change_Header
->biBitCount
= 1;
921 change_Header
->biPlanes
= 1;
923 Bmp
= IntCreateDIBitmap(Dc
, Header
, Init
, Bits
, Data
, ColorUse
);
925 NtGdiDeleteObjectApp(hDc
);
932 SetLastWin32Error(ERROR_INVALID_HANDLE
);
935 Bmp
= IntCreateDIBitmap(Dc
, Header
, Init
, Bits
, Data
, ColorUse
);
944 HBITMAP STDCALL
NtGdiCreateDIBSection(HDC hDC
,
945 IN OPTIONAL HANDLE hSection
,
951 IN ULONG_PTR dwColorSpace
,
956 BOOL bDesktopDC
= FALSE
;
958 // If the reference hdc is null, take the desktop dc
961 hDC
= NtGdiCreateCompatibleDC(0);
965 if ((dc
= DC_LockDc(hDC
)))
967 hbitmap
= DIB_CreateDIBSection ( dc
, (BITMAPINFO
*)bmi
, Usage
, Bits
,
968 hSection
, dwOffset
, 0);
973 SetLastWin32Error(ERROR_INVALID_HANDLE
);
977 NtGdiDeleteObjectApp(hDC
);
983 DIB_CreateDIBSection(
984 PDC dc
, BITMAPINFO
*bmi
, UINT usage
,
985 LPVOID
*bits
, HANDLE section
,
986 DWORD offset
, DWORD ovr_pitch
)
989 BITMAPOBJ
*bmp
= NULL
;
990 DIBSECTION
*dib
= NULL
;
992 // Fill BITMAP32 structure with DIB data
993 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1001 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1002 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1003 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1005 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1007 bm
.bmWidth
= bi
->biWidth
;
1008 bm
.bmHeight
= effHeight
;
1009 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
1011 bm
.bmPlanes
= bi
->biPlanes
;
1012 bm
.bmBitsPixel
= bi
->biBitCount
;
1015 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1016 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1017 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
1018 ? bi
->biSizeImage
: (ULONG
) (bm
.bmWidthBytes
* effHeight
);
1022 /* bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
1023 0L, offset, totalSize); */
1024 DbgPrint("DIB_CreateDIBSection: Cannot yet handle section DIBs\n");
1025 SetLastWin32Error(ERROR_CALL_NOT_IMPLEMENTED
);
1028 else if (ovr_pitch
&& offset
)
1029 bm
.bmBits
= (LPVOID
) offset
;
1032 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1035 if(usage
== DIB_PAL_COLORS
)
1036 lpRGB
= DIB_MapPaletteColors(dc
, bmi
);
1038 lpRGB
= bmi
->bmiColors
;
1040 // Allocate Memory for DIB and fill structure
1043 dib
= ExAllocatePoolWithTag(PagedPool
, sizeof(DIBSECTION
), TAG_DIB
);
1044 if (dib
!= NULL
) RtlZeroMemory(dib
, sizeof(DIBSECTION
));
1051 dib
->dsBmih
.biSizeImage
= totalSize
;
1053 /* Set dsBitfields values */
1054 if ( usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
1056 dib
->dsBitfields
[0] = dib
->dsBitfields
[1] = dib
->dsBitfields
[2] = 0;
1058 else switch(bi
->biBitCount
)
1061 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)lpRGB
: 0x7c00;
1062 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 1) : 0x03e0;
1063 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 2) : 0x001f; break;
1066 dib
->dsBitfields
[0] = 0xff0000;
1067 dib
->dsBitfields
[1] = 0x00ff00;
1068 dib
->dsBitfields
[2] = 0x0000ff;
1072 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)lpRGB
: 0xff0000;
1073 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 1) : 0x00ff00;
1074 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 2) : 0x0000ff;
1077 dib
->dshSection
= section
;
1078 dib
->dsOffset
= offset
;
1081 // Create Device Dependent Bitmap and add DIB pointer
1084 Size
.cx
= bm
.bmWidth
;
1085 Size
.cy
= abs(bm
.bmHeight
);
1086 res
= IntCreateBitmap(Size
, bm
.bmWidthBytes
,
1087 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1088 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1089 (bi
->biHeight
< 0 ? BMF_TOPDOWN
: 0),
1093 if (lpRGB
!= bmi
->bmiColors
)
1097 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
1100 bmp
= BITMAPOBJ_LockBitmap(res
);
1103 if (lpRGB
!= bmi
->bmiColors
)
1107 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1108 NtGdiDeleteObject(bmp
);
1111 bmp
->dib
= (DIBSECTION
*) dib
;
1112 bmp
->flFlags
= BITMAPOBJ_IS_APIBITMAP
;
1114 /* WINE NOTE: WINE makes use of a colormap, which is a color translation table between the DIB and the X physical
1115 device. Obviously, this is left out of the ReactOS implementation. Instead, we call
1116 NtGdiSetDIBColorTable. */
1117 if(bi
->biBitCount
== 1) { Entries
= 2; } else
1118 if(bi
->biBitCount
== 4) { Entries
= 16; } else
1119 if(bi
->biBitCount
== 8) { Entries
= 256; }
1122 bmp
->hDIBPalette
= PALETTE_AllocPaletteIndexedRGB(Entries
, lpRGB
);
1124 bmp
->hDIBPalette
= PALETTE_AllocPalette(PAL_BITFIELDS
, 0, NULL
,
1125 dib
->dsBitfields
[0],
1126 dib
->dsBitfields
[1],
1127 dib
->dsBitfields
[2]);
1130 // Clean up in case of errors
1131 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
)
1133 DPRINT("got an error res=%08x, bmp=%p, dib=%p, bm.bmBits=%p\n", res
, bmp
, dib
, bm
.bmBits
);
1137 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
1139 VirtualFree(bm.bmBits, 0L, MEM_RELEASE), bm.bmBits = NULL;
1142 if (dib
) { ExFreePool(dib
); dib
= NULL
; }
1143 if (bmp
) { bmp
= NULL
; }
1144 if (res
) { BITMAPOBJ_FreeBitmap(res
); res
= 0; }
1147 if (lpRGB
!= bmi
->bmiColors
)
1154 BITMAPOBJ_UnlockBitmap(bmp
);
1157 // Return BITMAP handle and storage location
1158 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1166 /***********************************************************************
1167 * DIB_GetDIBWidthBytes
1169 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1170 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
1171 * 11/16/1999 (RJJ) lifted from wine
1173 INT FASTCALL
DIB_GetDIBWidthBytes (INT width
, INT depth
)
1175 return ((width
* depth
+ 31) & ~31) >> 3;
1178 /***********************************************************************
1179 * DIB_GetDIBImageBytes
1181 * Return the number of bytes used to hold the image in a DIB bitmap.
1182 * 11/16/1999 (RJJ) lifted from wine
1185 INT STDCALL
DIB_GetDIBImageBytes (INT width
, INT height
, INT depth
)
1187 return DIB_GetDIBWidthBytes( width
, depth
) * (height
< 0 ? -height
: height
);
1190 /***********************************************************************
1191 * DIB_BitmapInfoSize
1193 * Return the size of the bitmap info structure including color table.
1194 * 11/16/1999 (RJJ) lifted from wine
1197 INT FASTCALL
DIB_BitmapInfoSize (const BITMAPINFO
* info
, WORD coloruse
)
1201 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1203 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
1204 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1205 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1207 else /* assume BITMAPINFOHEADER */
1209 colors
= info
->bmiHeader
.biClrUsed
;
1210 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
1211 return sizeof(BITMAPINFOHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1216 * DIB_GetBitmapInfo is complete copy of wine cvs 2/9-2006
1217 * from file dib.c from gdi32.dll or orginal version
1218 * did not calc the info right for some headers.
1222 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
,
1231 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1233 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
1234 *width
= core
->bcWidth
;
1235 *height
= core
->bcHeight
;
1236 *planes
= core
->bcPlanes
;
1237 *bpp
= core
->bcBitCount
;
1243 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
1245 *width
= header
->biWidth
;
1246 *height
= header
->biHeight
;
1247 *planes
= header
->biPlanes
;
1248 *bpp
= header
->biBitCount
;
1249 *compr
= header
->biCompression
;
1250 *size
= header
->biSizeImage
;
1254 if (header
->biSize
== sizeof(BITMAPV4HEADER
))
1256 BITMAPV4HEADER
*v4hdr
= (BITMAPV4HEADER
*)header
;
1257 *width
= v4hdr
->bV4Width
;
1258 *height
= v4hdr
->bV4Height
;
1259 *planes
= v4hdr
->bV4Planes
;
1260 *bpp
= v4hdr
->bV4BitCount
;
1261 *compr
= v4hdr
->bV4V4Compression
;
1262 *size
= v4hdr
->bV4SizeImage
;
1266 if (header
->biSize
== sizeof(BITMAPV5HEADER
))
1268 BITMAPV5HEADER
*v5hdr
= (BITMAPV5HEADER
*)header
;
1269 *width
= v5hdr
->bV5Width
;
1270 *height
= v5hdr
->bV5Height
;
1271 *planes
= v5hdr
->bV5Planes
;
1272 *bpp
= v5hdr
->bV5BitCount
;
1273 *compr
= v5hdr
->bV5Compression
;
1274 *size
= v5hdr
->bV5SizeImage
;
1277 DPRINT("(%ld): wrong size for header\n", header
->biSize
);
1281 // Converts a Device Independent Bitmap (DIB) to a Device Dependant Bitmap (DDB)
1282 // The specified Device Context (DC) defines what the DIB should be converted to
1283 PBITMAPOBJ FASTCALL
DIBtoDDB(HGLOBAL hPackedDIB
, HDC hdc
) // FIXME: This should be removed. All references to this function should
1284 // change to NtGdiSetDIBits
1287 PBITMAPOBJ pBmp
= NULL
;
1289 LPBYTE pbits
= NULL
;
1291 // Get a pointer to the packed DIB's data
1292 // pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
1295 pbits
= (LPBYTE
)(dib
+ DIB_BitmapInfoSize((BITMAPINFO
*)&dib
->dsBmih
, DIB_RGB_COLORS
));
1297 // Create a DDB from the DIB
1298 hBmp
= NtGdiCreateDIBitmap ( hdc
, &dib
->dsBmih
, CBM_INIT
,
1299 (LPVOID
)pbits
, (BITMAPINFO
*)&dib
->dsBmih
, DIB_RGB_COLORS
);
1301 // GlobalUnlock(hPackedDIB);
1303 // Retrieve the internal Pixmap from the DDB
1304 pBmp
= BITMAPOBJ_LockBitmap(hBmp
);
1310 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1317 palGDI
= PALETTE_LockPalette(dc
->w
.hPalette
);
1321 // RELEASEDCINFO(hDC);
1325 if (palGDI
->Mode
!= PAL_INDEXED
)
1327 PALETTE_UnlockPalette(palGDI
);
1328 palGDI
= PALETTE_LockPalette(dc
->PalIndexed
);
1329 if (palGDI
->Mode
!= PAL_INDEXED
)
1335 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1336 if (lpbmi
->bmiHeader
.biClrUsed
)
1338 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1341 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1344 PALETTE_UnlockPalette(palGDI
);
1348 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1350 for (i
= 0; i
< nNumColors
; i
++)
1352 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1353 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1354 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1357 // RELEASEDCINFO(hDC);
1358 PALETTE_UnlockPalette(palGDI
);
1364 BuildDIBPalette (CONST BITMAPINFO
*bmi
, PINT paletteType
)
1368 PALETTEENTRY
*palEntries
= NULL
;
1370 ULONG RedMask
, GreenMask
, BlueMask
;
1372 // Determine Bits Per Pixel
1373 bits
= bmi
->bmiHeader
.biBitCount
;
1375 // Determine paletteType from Bits Per Pixel
1378 *paletteType
= PAL_INDEXED
;
1379 RedMask
= GreenMask
= BlueMask
= 0;
1383 *paletteType
= PAL_BITFIELDS
;
1390 *paletteType
= PAL_BGR
;
1392 GreenMask
= 0x00ff00;
1393 BlueMask
= 0x0000ff;
1396 if (bmi
->bmiHeader
.biClrUsed
== 0)
1398 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1402 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1405 if (PAL_INDEXED
== *paletteType
)
1407 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)bmi
->bmiColors
);
1411 hPal
= PALETTE_AllocPalette(*paletteType
, ColorCount
,
1412 (ULONG
*) palEntries
,
1413 RedMask
, GreenMask
, BlueMask
);