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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 IN OPTIONAL LPBYTE pBits
)
38 /* NOTE: Windows also doesn't store nr. of planes separately! */
39 BitsPixel
= BITMAP_GetRealBitsPixel(BitsPixel
* Planes
);
41 /* Check parameters */
42 if (BitsPixel
== 0 || Width
<= 0 || Width
>= 0x8000000 || Height
== 0)
44 DPRINT1("Width = %d, Height = %d BitsPixel = %d\n",
45 Width
, Height
, BitsPixel
);
46 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
50 WidthBytes
= BITMAP_GetWidthBytes(Width
, BitsPixel
);
53 Size
.cy
= abs(Height
);
55 /* Make sure that cjBits will not overflow */
56 if ((ULONGLONG
)WidthBytes
* Size
.cy
>= 0x100000000ULL
)
58 DPRINT1("Width = %d, Height = %d BitsPixel = %d\n",
59 Width
, Height
, BitsPixel
);
60 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
64 /* Create the bitmap object. */
65 hBitmap
= IntCreateBitmap(Size
, WidthBytes
,
66 BitmapFormat(BitsPixel
, BI_RGB
),
67 (Height
< 0 ? BMF_TOPDOWN
: 0) |
68 (NULL
== pBits
? 0 : BMF_NOZEROINIT
), NULL
);
71 DPRINT("IntGdiCreateBitmap: returned 0\n");
75 psurfBmp
= SURFACE_LockSurface(hBitmap
);
78 GreDeleteObject(hBitmap
);
82 psurfBmp
->flFlags
= BITMAPOBJ_IS_APIBITMAP
;
83 psurfBmp
->hDC
= NULL
; // Fixme
87 IntSetBitmapBits(psurfBmp
, psurfBmp
->SurfObj
.cjBits
, pBits
);
90 SURFACE_UnlockSurface(psurfBmp
);
92 DPRINT("IntGdiCreateBitmap : %dx%d, %d BPP colors, topdown %d, returning %08x\n",
93 Size
.cx
, Size
.cy
, BitsPixel
, (Height
< 0 ? 1 : 0), hBitmap
);
105 IN OPTIONAL LPBYTE pUnsafeBits
)
110 UINT cjBits
= BITMAP_GetWidthBytes(Width
, BitsPixel
) * abs(Height
);
112 // FIXME: Use MmSecureVirtualMemory
115 ProbeForRead(pUnsafeBits
, cjBits
, 1);
117 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
126 return IntGdiCreateBitmap(Width
, Height
, Planes
, BitsPixel
, pUnsafeBits
);
130 IntCreateCompatibleBitmap(
137 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
138 if (0 == Width
|| 0 == Height
)
140 Bmp
= NtGdiGetStockObject(DEFAULT_BITMAP
);
144 if (Dc
->dctype
!= DC_TYPE_MEMORY
)
146 Bmp
= IntGdiCreateBitmap(abs(Width
),
148 IntGdiGetDeviceCaps(Dc
,PLANES
),
149 IntGdiGetDeviceCaps(Dc
,BITSPIXEL
),
156 PSURFACE psurf
= Dc
->dclevel
.pSurface
;
157 Count
= BITMAP_GetObject(psurf
, sizeof(dibs
), &dibs
);
161 if (Count
== sizeof(BITMAP
))
163 /* We have a bitmap bug!!! W/O the HACK, we have white icons.
165 MSDN Note: When a memory device context is created, it initially
166 has a 1-by-1 monochrome bitmap selected into it. If this memory
167 device context is used in CreateCompatibleBitmap, the bitmap that
168 is created is a monochrome bitmap. To create a color bitmap, use
169 the hDC that was used to create the memory device context, as
170 shown in the following code:
172 HDC memDC = CreateCompatibleDC(hDC);
173 HBITMAP memBM = CreateCompatibleBitmap(hDC, nWidth, nHeight);
174 SelectObject(memDC, memBM);
176 Bmp
= IntGdiCreateBitmap(abs(Width
),
179 IntGdiGetDeviceCaps(Dc
,BITSPIXEL
),//<-- HACK! dibs.dsBm.bmBitsPixel, // <-- Correct!
184 /* A DIB section is selected in the DC */
188 /* Allocate memory for a BITMAPINFOHEADER structure and a
189 color table. The maximum number of colors in a color table
190 is 256 which corresponds to a bitmap with depth 8.
191 Bitmaps with higher depths don't have color tables. */
192 bi
= ExAllocatePoolWithTag(PagedPool
,
193 sizeof(BITMAPINFOHEADER
) +
194 256 * sizeof(RGBQUAD
),
199 bi
->bmiHeader
.biSize
= sizeof(bi
->bmiHeader
);
200 bi
->bmiHeader
.biWidth
= Width
;
201 bi
->bmiHeader
.biHeight
= Height
;
202 bi
->bmiHeader
.biPlanes
= dibs
.dsBmih
.biPlanes
;
203 bi
->bmiHeader
.biBitCount
= dibs
.dsBmih
.biBitCount
;
204 bi
->bmiHeader
.biCompression
= dibs
.dsBmih
.biCompression
;
205 bi
->bmiHeader
.biSizeImage
= 0;
206 bi
->bmiHeader
.biXPelsPerMeter
= dibs
.dsBmih
.biXPelsPerMeter
;
207 bi
->bmiHeader
.biYPelsPerMeter
= dibs
.dsBmih
.biYPelsPerMeter
;
208 bi
->bmiHeader
.biClrUsed
= dibs
.dsBmih
.biClrUsed
;
209 bi
->bmiHeader
.biClrImportant
= dibs
.dsBmih
.biClrImportant
;
211 if (bi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
213 /* Copy the color masks */
214 RtlCopyMemory(bi
->bmiColors
, dibs
.dsBitfields
, 3 * sizeof(DWORD
));
216 else if (bi
->bmiHeader
.biBitCount
<= 8)
218 /* Copy the color table */
220 PPALETTE PalGDI
= PALETTE_LockPalette(psurf
->hDIBPalette
);
224 ExFreePoolWithTag(bi
, TAG_TEMP
);
225 SetLastWin32Error(ERROR_INVALID_HANDLE
);
230 Index
< 256 && Index
< PalGDI
->NumColors
;
233 bi
->bmiColors
[Index
].rgbRed
= PalGDI
->IndexedColors
[Index
].peRed
;
234 bi
->bmiColors
[Index
].rgbGreen
= PalGDI
->IndexedColors
[Index
].peGreen
;
235 bi
->bmiColors
[Index
].rgbBlue
= PalGDI
->IndexedColors
[Index
].peBlue
;
236 bi
->bmiColors
[Index
].rgbReserved
= 0;
238 PALETTE_UnlockPalette(PalGDI
);
241 Bmp
= DIB_CreateDIBSection(Dc
,
249 ExFreePoolWithTag(bi
, TAG_TEMP
);
260 NtGdiCreateCompatibleBitmap(
268 if (Width
<= 0 || Height
<= 0 || (Width
* Height
) > 0x3FFFFFFF)
270 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
275 return IntGdiCreateBitmap(Width
, Height
, 1, 1, 0);
279 DPRINT("NtGdiCreateCompatibleBitmap(%04x,%d,%d, bpp:%d) = \n",
280 hDC
, Width
, Height
, Dc
->ppdev
->gdiinfo
.cBitsPixel
);
284 SetLastWin32Error(ERROR_INVALID_HANDLE
);
288 Bmp
= IntCreateCompatibleBitmap(Dc
, Width
, Height
);
290 DPRINT("\t\t%04x\n", Bmp
);
296 NtGdiGetBitmapDimension(
306 psurfBmp
= SURFACE_LockSurface(hBitmap
);
307 if (psurfBmp
== NULL
)
309 SetLastWin32Error(ERROR_INVALID_HANDLE
);
315 ProbeForWrite(Dimension
, sizeof(SIZE
), 1);
316 *Dimension
= psurfBmp
->dimension
;
318 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
324 SURFACE_UnlockSurface(psurfBmp
);
330 NtGdiGetPixel(HDC hDC
, INT XPos
, INT YPos
)
333 COLORREF Result
= (COLORREF
)CLR_INVALID
; // default to failure
334 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
;
364 pso
= &psurf
->SurfObj
;
365 hpal
= psurf
->hDIBPalette
;
366 if (!hpal
) hpal
= pPrimarySurface
->devinfo
.hpalDefault
;
367 ppal
= PALETTE_ShareLockPalette(hpal
);
369 if (psurf
->SurfObj
.iBitmapFormat
== BMF_1BPP
&& !psurf
->hSecure
)
371 /* FIXME: palette should be gpalMono already ! */
372 EXLATEOBJ_vInitialize(&exlo
, &gpalMono
, &gpalRGB
, 0, 0xffffff, 0);
376 EXLATEOBJ_vInitialize(&exlo
, ppal
, &gpalRGB
, 0, 0xffffff, 0);
379 // check if this DC has a DIB behind it...
380 if (pso
->pvScan0
) // STYPE_BITMAP == pso->iType
383 Result
= XLATEOBJ_iXlate(&exlo
.xlo
,
384 DibFunctionsForBitmapFormat
[pso
->iBitmapFormat
].DIB_GetPixel(pso
, XPos
, YPos
));
387 EXLATEOBJ_vCleanup(&exlo
);
388 PALETTE_ShareUnlockPalette(ppal
);
393 // if Result is still CLR_INVALID, then the "quick" method above didn't work
394 if (bInRect
&& Result
== CLR_INVALID
)
396 // FIXME: create a 1x1 32BPP DIB, and blit to it
397 HDC hDCTmp
= NtGdiCreateCompatibleDC(hDC
);
400 static const BITMAPINFOHEADER bih
= { sizeof(BITMAPINFOHEADER
), 1, 1, 1, 32, BI_RGB
, 0, 0, 0, 0, 0 };
402 RtlMoveMemory(&(bi
.bmiHeader
), &bih
, sizeof(bih
));
403 hBmpTmp
= NtGdiCreateDIBitmapInternal(hDC
,
404 bi
.bmiHeader
.biWidth
,
405 bi
.bmiHeader
.biHeight
,
410 bi
.bmiHeader
.biBitCount
,
411 bi
.bmiHeader
.biSizeImage
,
415 //HBITMAP hBmpTmp = IntGdiCreateBitmap(1, 1, 1, 32, NULL);
418 HBITMAP hBmpOld
= (HBITMAP
)NtGdiSelectBitmap(hDCTmp
, hBmpTmp
);
423 NtGdiBitBlt(hDCTmp
, 0, 0, 1, 1, hDC
, XPos
, YPos
, SRCCOPY
, 0, 0);
424 NtGdiSelectBitmap(hDCTmp
, hBmpOld
);
426 // our bitmap is no longer selected, so we can access it's stuff...
427 psurf
= SURFACE_LockSurface(hBmpTmp
);
430 // Dont you need to convert something here?
431 Result
= *(COLORREF
*)psurf
->SurfObj
.pvScan0
;
432 SURFACE_UnlockSurface(psurf
);
435 GreDeleteObject(hBmpTmp
);
437 NtGdiDeleteObjectApp(hDCTmp
);
455 /* Don't copy more bytes than the buffer has */
456 Bytes
= min(Bytes
, psurf
->SurfObj
.cjBits
);
459 /* FIXME: Call DDI CopyBits here if available */
462 DPRINT("Calling device specific BitmapBits\n");
463 if (psurf
->DDBitmap
->funcs
->pBitmapBits
)
465 ret
= psurf
->DDBitmap
->funcs
->pBitmapBits(hbitmap
,
472 ERR_(bitmap
)("BitmapBits == NULL??\n");
479 RtlCopyMemory(Bits
, psurf
->SurfObj
.pvBits
, Bytes
);
489 OUT OPTIONAL PBYTE pUnsafeBits
)
494 if (pUnsafeBits
!= NULL
&& Bytes
== 0)
499 psurf
= SURFACE_LockSurface(hBitmap
);
502 SetLastWin32Error(ERROR_INVALID_HANDLE
);
506 bmSize
= BITMAP_GetWidthBytes(psurf
->SurfObj
.sizlBitmap
.cx
,
507 BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
)) *
508 abs(psurf
->SurfObj
.sizlBitmap
.cy
);
510 /* If the bits vector is null, the function should return the read size */
511 if (pUnsafeBits
== NULL
)
513 SURFACE_UnlockSurface(psurf
);
517 /* Don't copy more bytes than the buffer has */
518 Bytes
= min(Bytes
, bmSize
);
520 // FIXME: use MmSecureVirtualMemory
523 ProbeForWrite(pUnsafeBits
, Bytes
, 1);
524 ret
= IntGetBitmapBits(psurf
, Bytes
, pUnsafeBits
);
526 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
532 SURFACE_UnlockSurface(psurf
);
546 /* Don't copy more bytes than the buffer has */
547 Bytes
= min(Bytes
, psurf
->SurfObj
.cjBits
);
550 /* FIXME: call DDI specific function here if available */
553 DPRINT("Calling device specific BitmapBits\n");
554 if (psurf
->DDBitmap
->funcs
->pBitmapBits
)
556 ret
= psurf
->DDBitmap
->funcs
->pBitmapBits(hBitmap
,
563 DPRINT("BitmapBits == NULL??\n");
570 RtlCopyMemory(psurf
->SurfObj
.pvBits
, Bits
, Bytes
);
582 IN PBYTE pUnsafeBits
)
587 if (pUnsafeBits
== NULL
|| Bytes
== 0)
592 psurf
= SURFACE_LockSurface(hBitmap
);
595 SetLastWin32Error(ERROR_INVALID_HANDLE
);
601 ProbeForRead(pUnsafeBits
, Bytes
, 1);
602 ret
= IntSetBitmapBits(psurf
, Bytes
, pUnsafeBits
);
604 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
610 SURFACE_UnlockSurface(psurf
);
616 NtGdiSetBitmapDimension(
628 psurf
= SURFACE_LockSurface(hBitmap
);
631 SetLastWin32Error(ERROR_INVALID_HANDLE
);
639 ProbeForWrite(Size
, sizeof(SIZE
), 1);
640 *Size
= psurf
->dimension
;
642 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
649 /* The dimension is changed even if writing the old value failed */
650 psurf
->dimension
.cx
= Width
;
651 psurf
->dimension
.cy
= Height
;
653 SURFACE_UnlockSurface(psurf
);
658 VOID
IntHandleSpecialColorType(HDC hDC
, COLORREF
* Color
)
662 PALETTEENTRY palEntry
;
665 switch (*Color
>> 24)
667 case 0x10: /* DIBINDEX */
668 if (IntGetDIBColorTable(hDC
, LOWORD(*Color
), 1, &quad
) == 1)
670 *Color
= RGB(quad
.rgbRed
, quad
.rgbGreen
, quad
.rgbBlue
);
674 /* Out of color table bounds - use black */
675 *Color
= RGB(0, 0, 0);
678 case 0x02: /* PALETTERGB */
679 pdc
= DC_LockDc(hDC
);
680 index
= NtGdiGetNearestPaletteIndex(pdc
->dclevel
.hpal
, *Color
);
681 if (IntGetPaletteEntries(pdc
->dclevel
.hpal
, index
, 1, &palEntry
) == 1)
683 *Color
= RGB(palEntry
.peRed
, palEntry
.peGreen
, palEntry
.peBlue
);
687 case 0x01: /* PALETTEINDEX */
688 pdc
= DC_LockDc(hDC
);
689 if (IntGetPaletteEntries(pdc
->dclevel
.hpal
, LOWORD(*Color
), 1, &palEntry
) == 1)
691 *Color
= RGB(palEntry
.peRed
, palEntry
.peGreen
, palEntry
.peBlue
);
696 DPRINT("Unsupported color type %d passed\n", *Color
>> 24);
711 if ((Color
& 0xFF000000) != 0)
713 IntHandleSpecialColorType(hDC
, &Color
);
716 hBrush
= NtGdiCreateSolidBrush(Color
, NULL
);
720 OldBrush
= NtGdiSelectBrush(hDC
, hBrush
);
721 if (OldBrush
== NULL
)
723 GreDeleteObject(hBrush
);
727 NtGdiPatBlt(hDC
, X
, Y
, 1, 1, PATCOPY
);
728 NtGdiSelectBrush(hDC
, OldBrush
);
729 GreDeleteObject(hBrush
);
741 DPRINT("0 NtGdiSetPixel X %ld Y %ld C %ld\n", X
, Y
, Color
);
743 if (GdiSetPixelV(hDC
,X
,Y
,Color
))
745 Color
= NtGdiGetPixel(hDC
,X
,Y
);
746 DPRINT("1 NtGdiSetPixel X %ld Y %ld C %ld\n", X
, Y
, Color
);
750 Color
= (COLORREF
)CLR_INVALID
;
751 DPRINT("2 NtGdiSetPixel X %ld Y %ld C %ld\n", X
, Y
, Color
);
756 /* Internal Functions */
759 BITMAP_GetRealBitsPixel(UINT nBitsPixel
)
767 if (nBitsPixel
<= 16)
769 if (nBitsPixel
<= 24)
771 if (nBitsPixel
<= 32)
778 BITMAP_GetWidthBytes(INT bmWidth
, INT bpp
)
784 return 2 * ((bmWidth
+15) >> 4);
787 bmWidth
*= 3; /* fall through */
789 return bmWidth
+ (bmWidth
& 1);
799 return 2 * ((bmWidth
+3) >> 2);
808 return ((bmWidth
* bpp
+ 15) & ~15) >> 3;
812 BITMAP_CopyBitmap(HBITMAP hBitmap
)
816 SURFACE
*Bitmap
, *resBitmap
;
824 Bitmap
= GDIOBJ_LockObj(hBitmap
, GDI_OBJECT_TYPE_BITMAP
);
830 BITMAP_GetObject(Bitmap
, sizeof(BITMAP
), (PVOID
)&bm
);
832 if (Bitmap
->SurfObj
.lDelta
>= 0)
833 bm
.bmHeight
= -bm
.bmHeight
;
835 Size
.cx
= abs(bm
.bmWidth
);
836 Size
.cy
= abs(bm
.bmHeight
);
837 res
= IntCreateBitmap(Size
,
839 BitmapFormat(bm
.bmBitsPixel
* bm
.bmPlanes
, BI_RGB
),
840 (bm
.bmHeight
< 0 ? BMF_TOPDOWN
: 0) | BMF_NOZEROINIT
,
847 resBitmap
= GDIOBJ_LockObj(res
, GDI_OBJECT_TYPE_BITMAP
);
850 buf
= ExAllocatePoolWithTag(PagedPool
,
851 bm
.bmWidthBytes
* abs(bm
.bmHeight
),
855 GDIOBJ_UnlockObjByPtr((POBJ
)resBitmap
);
856 GDIOBJ_UnlockObjByPtr((POBJ
)Bitmap
);
857 GreDeleteObject(res
);
860 IntGetBitmapBits(Bitmap
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), buf
);
861 IntSetBitmapBits(resBitmap
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), buf
);
862 ExFreePoolWithTag(buf
,TAG_BITMAP
);
863 resBitmap
->flFlags
= Bitmap
->flFlags
;
864 GDIOBJ_UnlockObjByPtr((POBJ
)resBitmap
);
868 GreDeleteObject(res
);
873 GDIOBJ_UnlockObjByPtr((POBJ
)Bitmap
);
879 BITMAP_GetObject(SURFACE
*psurf
, INT Count
, LPVOID buffer
)
883 if (!buffer
) return sizeof(BITMAP
);
884 if ((UINT
)Count
< sizeof(BITMAP
)) return 0;
886 /* always fill a basic BITMAP structure */
889 pBitmap
->bmWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
890 pBitmap
->bmHeight
= psurf
->SurfObj
.sizlBitmap
.cy
;
891 pBitmap
->bmWidthBytes
= abs(psurf
->SurfObj
.lDelta
);
892 pBitmap
->bmPlanes
= 1;
893 pBitmap
->bmBitsPixel
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
895 /* Check for DIB section */
898 /* Set bmBits in this case */
899 pBitmap
->bmBits
= psurf
->SurfObj
.pvBits
;
901 if (Count
>= sizeof(DIBSECTION
))
903 /* Fill rest of DIBSECTION */
904 PDIBSECTION pds
= buffer
;
906 pds
->dsBmih
.biSize
= sizeof(BITMAPINFOHEADER
);
907 pds
->dsBmih
.biWidth
= pds
->dsBm
.bmWidth
;
908 pds
->dsBmih
.biHeight
= pds
->dsBm
.bmHeight
;
909 pds
->dsBmih
.biPlanes
= pds
->dsBm
.bmPlanes
;
910 pds
->dsBmih
.biBitCount
= pds
->dsBm
.bmBitsPixel
;
911 switch (psurf
->SurfObj
.iBitmapFormat
)
913 /* FIXME: What about BI_BITFIELDS? */
920 pds
->dsBmih
.biCompression
= BI_RGB
;
923 pds
->dsBmih
.biCompression
= BI_RLE4
;
926 pds
->dsBmih
.biCompression
= BI_RLE8
;
929 pds
->dsBmih
.biCompression
= BI_JPEG
;
932 pds
->dsBmih
.biCompression
= BI_PNG
;
935 pds
->dsBmih
.biSizeImage
= psurf
->SurfObj
.cjBits
;
936 pds
->dsBmih
.biXPelsPerMeter
= 0;
937 pds
->dsBmih
.biYPelsPerMeter
= 0;
938 pds
->dsBmih
.biClrUsed
= psurf
->biClrUsed
;
939 pds
->dsBmih
.biClrImportant
= psurf
->biClrImportant
;
940 pds
->dsBitfields
[0] = psurf
->dsBitfields
[0];
941 pds
->dsBitfields
[1] = psurf
->dsBitfields
[1];
942 pds
->dsBitfields
[2] = psurf
->dsBitfields
[2];
943 pds
->dshSection
= psurf
->hDIBSection
;
944 pds
->dsOffset
= psurf
->dwOffset
;
946 return sizeof(DIBSECTION
);
951 /* not set according to wine test, confirmed in win2k */
952 pBitmap
->bmBits
= NULL
;
955 return sizeof(BITMAP
);
967 PSURFACE psurf
= SURFACE_LockSurface(hsurf
);
971 SURFACE_UnlockSurface(psurf
);