2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 IN OPTIONAL LPBYTE pBits
)
39 /* NOTE: Windows also doesn't store nr. of planes separately! */
40 BitsPixel
= BITMAP_GetRealBitsPixel(BitsPixel
* Planes
);
42 /* Check parameters */
43 if (BitsPixel
== 0 || Width
<= 0 || Width
>= 0x8000000 || Height
== 0)
45 DPRINT1("Width = %d, Height = %d BitsPixel = %d\n",
46 Width
, Height
, BitsPixel
);
47 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
51 WidthBytes
= BITMAP_GetWidthBytes(Width
, BitsPixel
);
54 Size
.cy
= abs(Height
);
56 /* Make sure that cjBits will not overflow */
57 if ((ULONGLONG
)WidthBytes
* Size
.cy
>= 0x100000000ULL
)
59 DPRINT1("Width = %d, Height = %d BitsPixel = %d\n",
60 Width
, Height
, BitsPixel
);
61 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
65 /* Create the bitmap object. */
66 hBitmap
= IntCreateBitmap(Size
, WidthBytes
,
67 BitmapFormat(BitsPixel
, BI_RGB
),
68 (Height
< 0 ? BMF_TOPDOWN
: 0) |
69 (NULL
== pBits
? 0 : BMF_NOZEROINIT
), NULL
);
72 DPRINT("IntGdiCreateBitmap: returned 0\n");
76 psurfBmp
= SURFACE_LockSurface(hBitmap
);
79 GreDeleteObject(hBitmap
);
83 psurfBmp
->flFlags
= BITMAPOBJ_IS_APIBITMAP
;
84 psurfBmp
->hDC
= NULL
; // Fixme
88 IntSetBitmapBits(psurfBmp
, psurfBmp
->SurfObj
.cjBits
, pBits
);
91 SURFACE_UnlockSurface(psurfBmp
);
93 DPRINT("IntGdiCreateBitmap : %dx%d, %d BPP colors, topdown %d, returning %08x\n",
94 Size
.cx
, Size
.cy
, BitsPixel
, (Height
< 0 ? 1 : 0), hBitmap
);
106 IN OPTIONAL LPBYTE pUnsafeBits
)
111 UINT cjBits
= BITMAP_GetWidthBytes(Width
, BitsPixel
) * abs(Height
);
113 // FIXME: Use MmSecureVirtualMemory
116 ProbeForRead(pUnsafeBits
, cjBits
, 1);
118 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
127 return IntGdiCreateBitmap(Width
, Height
, Planes
, BitsPixel
, pUnsafeBits
);
131 IntCreateCompatibleBitmap(
138 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
139 if (0 == Width
|| 0 == Height
)
141 Bmp
= NtGdiGetStockObject(DEFAULT_BITMAP
);
145 if (Dc
->dctype
!= DC_TYPE_MEMORY
)
147 Bmp
= IntGdiCreateBitmap(abs(Width
),
149 IntGdiGetDeviceCaps(Dc
,PLANES
),
150 IntGdiGetDeviceCaps(Dc
,BITSPIXEL
),
157 PSURFACE psurf
= Dc
->dclevel
.pSurface
;
158 Count
= BITMAP_GetObject(psurf
, sizeof(dibs
), &dibs
);
162 if (Count
== sizeof(BITMAP
))
164 /* We have a bitmap bug!!! W/O the HACK, we have white icons.
166 MSDN Note: When a memory device context is created, it initially
167 has a 1-by-1 monochrome bitmap selected into it. If this memory
168 device context is used in CreateCompatibleBitmap, the bitmap that
169 is created is a monochrome bitmap. To create a color bitmap, use
170 the hDC that was used to create the memory device context, as
171 shown in the following code:
173 HDC memDC = CreateCompatibleDC(hDC);
174 HBITMAP memBM = CreateCompatibleBitmap(hDC, nWidth, nHeight);
175 SelectObject(memDC, memBM);
177 Bmp
= IntGdiCreateBitmap(abs(Width
),
180 IntGdiGetDeviceCaps(Dc
,BITSPIXEL
),//<-- HACK! dibs.dsBm.bmBitsPixel, // <-- Correct!
185 /* A DIB section is selected in the DC */
189 /* Allocate memory for a BITMAPINFOHEADER structure and a
190 color table. The maximum number of colors in a color table
191 is 256 which corresponds to a bitmap with depth 8.
192 Bitmaps with higher depths don't have color tables. */
193 bi
= ExAllocatePoolWithTag(PagedPool
,
194 sizeof(BITMAPINFOHEADER
) +
195 256 * sizeof(RGBQUAD
),
200 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
201 bi
->bmiHeader
.biWidth
= Width
;
202 bi
->bmiHeader
.biHeight
= Height
;
203 bi
->bmiHeader
.biPlanes
= dibs
.dsBmih
.biPlanes
;
204 bi
->bmiHeader
.biBitCount
= dibs
.dsBmih
.biBitCount
;
205 bi
->bmiHeader
.biCompression
= dibs
.dsBmih
.biCompression
;
206 bi
->bmiHeader
.biSizeImage
= 0;
207 bi
->bmiHeader
.biXPelsPerMeter
= dibs
.dsBmih
.biXPelsPerMeter
;
208 bi
->bmiHeader
.biYPelsPerMeter
= dibs
.dsBmih
.biYPelsPerMeter
;
209 bi
->bmiHeader
.biClrUsed
= dibs
.dsBmih
.biClrUsed
;
210 bi
->bmiHeader
.biClrImportant
= dibs
.dsBmih
.biClrImportant
;
212 if (bi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
214 /* Copy the color masks */
215 RtlCopyMemory(bi
->bmiColors
, dibs
.dsBitfields
, 3 * sizeof(DWORD
));
217 else if (bi
->bmiHeader
.biBitCount
<= 8)
219 /* Copy the color table */
221 PPALETTE PalGDI
= PALETTE_LockPalette(psurf
->hDIBPalette
);
225 ExFreePoolWithTag(bi
, TAG_TEMP
);
226 SetLastWin32Error(ERROR_INVALID_HANDLE
);
231 Index
< 256 && Index
< PalGDI
->NumColors
;
234 bi
->bmiColors
[Index
].rgbRed
= PalGDI
->IndexedColors
[Index
].peRed
;
235 bi
->bmiColors
[Index
].rgbGreen
= PalGDI
->IndexedColors
[Index
].peGreen
;
236 bi
->bmiColors
[Index
].rgbBlue
= PalGDI
->IndexedColors
[Index
].peBlue
;
237 bi
->bmiColors
[Index
].rgbReserved
= 0;
239 PALETTE_UnlockPalette(PalGDI
);
242 Bmp
= DIB_CreateDIBSection(Dc
,
250 ExFreePoolWithTag(bi
, TAG_TEMP
);
261 NtGdiCreateCompatibleBitmap(
269 if (Width
<= 0 || Height
<= 0 || (Width
* Height
) > 0x3FFFFFFF)
271 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
276 return IntGdiCreateBitmap(Width
, Height
, 1, 1, 0);
280 DPRINT("NtGdiCreateCompatibleBitmap(%04x,%d,%d, bpp:%d) = \n",
281 hDC
, Width
, Height
, Dc
->ppdev
->GDIInfo
.cBitsPixel
);
285 SetLastWin32Error(ERROR_INVALID_HANDLE
);
289 Bmp
= IntCreateCompatibleBitmap(Dc
, Width
, Height
);
291 DPRINT("\t\t%04x\n", Bmp
);
297 NtGdiGetBitmapDimension(
307 psurfBmp
= SURFACE_LockSurface(hBitmap
);
308 if (psurfBmp
== NULL
)
310 SetLastWin32Error(ERROR_INVALID_HANDLE
);
316 ProbeForWrite(Dimension
, sizeof(SIZE
), 1);
317 *Dimension
= psurfBmp
->dimension
;
319 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
325 SURFACE_UnlockSurface(psurfBmp
);
331 NtGdiGetPixel(HDC hDC
, INT XPos
, INT YPos
)
334 COLORREF Result
= (COLORREF
)CLR_INVALID
; // default to failure
335 BOOL bInRect
= FALSE
;
346 SetLastWin32Error(ERROR_INVALID_HANDLE
);
350 if (dc
->dctype
== DC_TYPE_INFO
)
356 XPos
+= dc
->ptlDCOrig
.x
;
357 YPos
+= dc
->ptlDCOrig
.y
;
358 if (RECTL_bPointInRect(&dc
->rosdc
.CombinedClip
->rclBounds
, XPos
, YPos
))
361 psurf
= dc
->dclevel
.pSurface
;
362 pso
= &psurf
->SurfObj
;
365 Pal
= psurf
->hDIBPalette
;
366 if (!Pal
) Pal
= pPrimarySurface
->DevInfo
.hpalDefault
;
368 /* FIXME: Verify if it shouldn't be PAL_BGR! */
369 XlateObj
= (XLATEOBJ
*)IntEngCreateXlate(PAL_RGB
, 0, NULL
, Pal
);
372 // check if this DC has a DIB behind it...
373 if (pso
->pvScan0
) // STYPE_BITMAP == pso->iType
376 Result
= XLATEOBJ_iXlate(XlateObj
,
377 DibFunctionsForBitmapFormat
[pso
->iBitmapFormat
].DIB_GetPixel(pso
, XPos
, YPos
));
379 EngDeleteXlate(XlateObj
);
385 // if Result is still CLR_INVALID, then the "quick" method above didn't work
386 if (bInRect
&& Result
== CLR_INVALID
)
388 // FIXME: create a 1x1 32BPP DIB, and blit to it
389 HDC hDCTmp
= NtGdiCreateCompatibleDC(hDC
);
392 static const BITMAPINFOHEADER bih
= { sizeof(BITMAPINFOHEADER
), 1, 1, 1, 32, BI_RGB
, 0, 0, 0, 0, 0 };
394 RtlMoveMemory(&(bi
.bmiHeader
), &bih
, sizeof(bih
));
395 hBmpTmp
= NtGdiCreateDIBitmapInternal(hDC
,
396 bi
.bmiHeader
.biWidth
,
397 bi
.bmiHeader
.biHeight
,
402 bi
.bmiHeader
.biBitCount
,
403 bi
.bmiHeader
.biSizeImage
,
407 //HBITMAP hBmpTmp = IntGdiCreateBitmap(1, 1, 1, 32, NULL);
410 HBITMAP hBmpOld
= (HBITMAP
)NtGdiSelectBitmap(hDCTmp
, hBmpTmp
);
415 NtGdiBitBlt(hDCTmp
, 0, 0, 1, 1, hDC
, XPos
, YPos
, SRCCOPY
, 0, 0);
416 NtGdiSelectBitmap(hDCTmp
, hBmpOld
);
418 // our bitmap is no longer selected, so we can access it's stuff...
419 psurf
= SURFACE_LockSurface(hBmpTmp
);
422 // Dont you need to convert something here?
423 Result
= *(COLORREF
*)psurf
->SurfObj
.pvScan0
;
424 SURFACE_UnlockSurface(psurf
);
427 GreDeleteObject(hBmpTmp
);
429 NtGdiDeleteObjectApp(hDCTmp
);
447 /* Don't copy more bytes than the buffer has */
448 Bytes
= min(Bytes
, psurf
->SurfObj
.cjBits
);
451 /* FIXME: Call DDI CopyBits here if available */
454 DPRINT("Calling device specific BitmapBits\n");
455 if (psurf
->DDBitmap
->funcs
->pBitmapBits
)
457 ret
= psurf
->DDBitmap
->funcs
->pBitmapBits(hbitmap
,
464 ERR_(bitmap
)("BitmapBits == NULL??\n");
471 RtlCopyMemory(Bits
, psurf
->SurfObj
.pvBits
, Bytes
);
481 OUT OPTIONAL PBYTE pUnsafeBits
)
486 if (pUnsafeBits
!= NULL
&& Bytes
== 0)
491 psurf
= SURFACE_LockSurface(hBitmap
);
494 SetLastWin32Error(ERROR_INVALID_HANDLE
);
498 bmSize
= BITMAP_GetWidthBytes(psurf
->SurfObj
.sizlBitmap
.cx
,
499 BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
)) *
500 abs(psurf
->SurfObj
.sizlBitmap
.cy
);
502 /* If the bits vector is null, the function should return the read size */
503 if (pUnsafeBits
== NULL
)
505 SURFACE_UnlockSurface(psurf
);
509 /* Don't copy more bytes than the buffer has */
510 Bytes
= min(Bytes
, bmSize
);
512 // FIXME: use MmSecureVirtualMemory
515 ProbeForWrite(pUnsafeBits
, Bytes
, 1);
516 ret
= IntGetBitmapBits(psurf
, Bytes
, pUnsafeBits
);
518 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
524 SURFACE_UnlockSurface(psurf
);
538 /* Don't copy more bytes than the buffer has */
539 Bytes
= min(Bytes
, psurf
->SurfObj
.cjBits
);
542 /* FIXME: call DDI specific function here if available */
545 DPRINT("Calling device specific BitmapBits\n");
546 if (psurf
->DDBitmap
->funcs
->pBitmapBits
)
548 ret
= psurf
->DDBitmap
->funcs
->pBitmapBits(hBitmap
,
555 DPRINT("BitmapBits == NULL??\n");
562 RtlCopyMemory(psurf
->SurfObj
.pvBits
, Bits
, Bytes
);
574 IN PBYTE pUnsafeBits
)
579 if (pUnsafeBits
== NULL
|| Bytes
== 0)
584 psurf
= SURFACE_LockSurface(hBitmap
);
587 SetLastWin32Error(ERROR_INVALID_HANDLE
);
593 ProbeForRead(pUnsafeBits
, Bytes
, 1);
594 ret
= IntSetBitmapBits(psurf
, Bytes
, pUnsafeBits
);
596 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
602 SURFACE_UnlockSurface(psurf
);
608 NtGdiSetBitmapDimension(
620 psurf
= SURFACE_LockSurface(hBitmap
);
623 SetLastWin32Error(ERROR_INVALID_HANDLE
);
631 ProbeForWrite(Size
, sizeof(SIZE
), 1);
632 *Size
= psurf
->dimension
;
634 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
641 /* The dimension is changed even if writing the old value failed */
642 psurf
->dimension
.cx
= Width
;
643 psurf
->dimension
.cy
= Height
;
645 SURFACE_UnlockSurface(psurf
);
657 HBRUSH hbrush
= NtGdiCreateSolidBrush(Color
, NULL
);
663 OldBrush
= NtGdiSelectBrush(hDC
, hbrush
);
664 if (OldBrush
== NULL
)
666 GreDeleteObject(hbrush
);
670 NtGdiPatBlt(hDC
, X
, Y
, 1, 1, PATCOPY
);
671 NtGdiSelectBrush(hDC
, OldBrush
);
672 GreDeleteObject(hbrush
);
684 DPRINT("0 NtGdiSetPixel X %ld Y %ld C %ld\n", X
, Y
, Color
);
686 if (GdiSetPixelV(hDC
,X
,Y
,Color
))
688 Color
= NtGdiGetPixel(hDC
,X
,Y
);
689 DPRINT("1 NtGdiSetPixel X %ld Y %ld C %ld\n", X
, Y
, Color
);
693 Color
= (COLORREF
)CLR_INVALID
;
694 DPRINT("2 NtGdiSetPixel X %ld Y %ld C %ld\n", X
, Y
, Color
);
699 /* Internal Functions */
702 BITMAP_GetRealBitsPixel(UINT nBitsPixel
)
710 if (nBitsPixel
<= 16)
712 if (nBitsPixel
<= 24)
714 if (nBitsPixel
<= 32)
721 BITMAP_GetWidthBytes(INT bmWidth
, INT bpp
)
727 return 2 * ((bmWidth
+15) >> 4);
730 bmWidth
*= 3; /* fall through */
732 return bmWidth
+ (bmWidth
& 1);
742 return 2 * ((bmWidth
+3) >> 2);
751 return ((bmWidth
* bpp
+ 15) & ~15) >> 3;
755 BITMAP_CopyBitmap(HBITMAP hBitmap
)
759 SURFACE
*Bitmap
, *resBitmap
;
767 Bitmap
= GDIOBJ_LockObj(hBitmap
, GDI_OBJECT_TYPE_BITMAP
);
773 BITMAP_GetObject(Bitmap
, sizeof(BITMAP
), (PVOID
)&bm
);
775 if (Bitmap
->SurfObj
.lDelta
>= 0)
776 bm
.bmHeight
= -bm
.bmHeight
;
778 Size
.cx
= abs(bm
.bmWidth
);
779 Size
.cy
= abs(bm
.bmHeight
);
780 res
= IntCreateBitmap(Size
,
782 BitmapFormat(bm
.bmBitsPixel
* bm
.bmPlanes
, BI_RGB
),
783 (bm
.bmHeight
< 0 ? BMF_TOPDOWN
: 0) | BMF_NOZEROINIT
,
790 resBitmap
= GDIOBJ_LockObj(res
, GDI_OBJECT_TYPE_BITMAP
);
793 buf
= ExAllocatePoolWithTag(PagedPool
,
794 bm
.bmWidthBytes
* abs(bm
.bmHeight
),
798 GDIOBJ_UnlockObjByPtr((POBJ
)resBitmap
);
799 GDIOBJ_UnlockObjByPtr((POBJ
)Bitmap
);
800 GreDeleteObject(res
);
803 IntGetBitmapBits(Bitmap
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), buf
);
804 IntSetBitmapBits(resBitmap
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), buf
);
805 ExFreePoolWithTag(buf
,TAG_BITMAP
);
806 resBitmap
->flFlags
= Bitmap
->flFlags
;
807 GDIOBJ_UnlockObjByPtr((POBJ
)resBitmap
);
811 GreDeleteObject(res
);
816 GDIOBJ_UnlockObjByPtr((POBJ
)Bitmap
);
822 BITMAP_GetObject(SURFACE
*psurf
, INT Count
, LPVOID buffer
)
826 if (!buffer
) return sizeof(BITMAP
);
827 if ((UINT
)Count
< sizeof(BITMAP
)) return 0;
829 /* always fill a basic BITMAP structure */
832 pBitmap
->bmWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
833 pBitmap
->bmHeight
= psurf
->SurfObj
.sizlBitmap
.cy
;
834 pBitmap
->bmWidthBytes
= abs(psurf
->SurfObj
.lDelta
);
835 pBitmap
->bmPlanes
= 1;
836 pBitmap
->bmBitsPixel
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
838 /* Check for DIB section */
841 /* Set bmBits in this case */
842 pBitmap
->bmBits
= psurf
->SurfObj
.pvBits
;
844 if (Count
>= sizeof(DIBSECTION
))
846 /* Fill rest of DIBSECTION */
847 PDIBSECTION pds
= buffer
;
849 pds
->dsBmih
.biSize
= sizeof(BITMAPINFOHEADER
);
850 pds
->dsBmih
.biWidth
= pds
->dsBm
.bmWidth
;
851 pds
->dsBmih
.biHeight
= pds
->dsBm
.bmHeight
;
852 pds
->dsBmih
.biPlanes
= pds
->dsBm
.bmPlanes
;
853 pds
->dsBmih
.biBitCount
= pds
->dsBm
.bmBitsPixel
;
854 switch (psurf
->SurfObj
.iBitmapFormat
)
856 /* FIXME: What about BI_BITFIELDS? */
863 pds
->dsBmih
.biCompression
= BI_RGB
;
866 pds
->dsBmih
.biCompression
= BI_RLE4
;
869 pds
->dsBmih
.biCompression
= BI_RLE8
;
872 pds
->dsBmih
.biCompression
= BI_JPEG
;
875 pds
->dsBmih
.biCompression
= BI_PNG
;
878 pds
->dsBmih
.biSizeImage
= psurf
->SurfObj
.cjBits
;
879 pds
->dsBmih
.biXPelsPerMeter
= 0;
880 pds
->dsBmih
.biYPelsPerMeter
= 0;
881 pds
->dsBmih
.biClrUsed
= psurf
->biClrUsed
;
882 pds
->dsBmih
.biClrImportant
= psurf
->biClrImportant
;
883 pds
->dsBitfields
[0] = psurf
->dsBitfields
[0];
884 pds
->dsBitfields
[1] = psurf
->dsBitfields
[1];
885 pds
->dsBitfields
[2] = psurf
->dsBitfields
[2];
886 pds
->dshSection
= psurf
->hDIBSection
;
887 pds
->dsOffset
= psurf
->dwOffset
;
889 return sizeof(DIBSECTION
);
894 /* not set according to wine test, confirmed in win2k */
895 pBitmap
->bmBits
= NULL
;
898 return sizeof(BITMAP
);
910 PSURFACE psurf
= SURFACE_LockSurface(hsurf
);
914 SURFACE_UnlockSurface(psurf
);