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.
26 #define IN_RECT(r,x,y) \
40 IN OPTIONAL LPBYTE pBits
)
47 /* NOTE: Windows also doesn't store nr. of planes separately! */
48 BitsPixel
= BITMAPOBJ_GetRealBitsPixel(BitsPixel
* Planes
);
50 /* Check parameters */
51 if (BitsPixel
== 0 || Width
< 1 || Height
< 1)
53 DPRINT1("Width = %d, Height = %d BitsPixel = %d\n", Width
, Height
, BitsPixel
);
54 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
58 WidthBytes
= BITMAPOBJ_GetWidthBytes(Width
, BitsPixel
);
63 /* Create the bitmap object. */
64 hBitmap
= IntCreateBitmap(Size
, WidthBytes
,
65 BitmapFormat(BitsPixel
, BI_RGB
),
66 (Height
< 0 ? BMF_TOPDOWN
: 0) |
67 (NULL
== pBits
? 0 : BMF_NOZEROINIT
), NULL
);
70 DPRINT("IntGdiCreateBitmap: returned 0\n");
74 DPRINT("IntGdiCreateBitmap:%dx%d, %d BPP colors returning %08x\n",
75 Size
.cx
, Size
.cy
, BitsPixel
, hBitmap
);
77 bmp
= BITMAPOBJ_LockBitmap( hBitmap
);
80 /* FIXME should we free the hBitmap or return it ?? */
84 bmp
->flFlags
= BITMAPOBJ_IS_APIBITMAP
;
88 IntSetBitmapBits(bmp
, bmp
->SurfObj
.cjBits
, pBits
);
91 BITMAPOBJ_UnlockBitmap( bmp
);
103 IN OPTIONAL LPBYTE pUnsafeBits
)
111 UINT cjBits
= BITMAPOBJ_GetWidthBytes(Width
, BitsPixel
) * Height
;
112 ProbeForRead(pUnsafeBits
, cjBits
, 1);
114 hBitmap
= IntGdiCreateBitmap(Width
, Height
, Planes
, BitsPixel
, pUnsafeBits
);
126 BITMAP_Cleanup(PVOID ObjectBody
)
128 PBITMAPOBJ pBmp
= (PBITMAPOBJ
)ObjectBody
;
129 if (pBmp
->SurfObj
.pvBits
!= NULL
&&
130 (pBmp
->flFlags
& BITMAPOBJ_IS_APIBITMAP
))
132 if (pBmp
->dib
== NULL
)
134 if (pBmp
->SurfObj
.pvBits
!= NULL
)
135 ExFreePool(pBmp
->SurfObj
.pvBits
);
139 if (pBmp
->SurfObj
.pvBits
!= NULL
)
140 EngFreeUserMem(pBmp
->SurfObj
.pvBits
);
142 if (pBmp
->hDIBPalette
!= NULL
)
144 NtGdiDeleteObject(pBmp
->hDIBPalette
);
148 if (NULL
!= pBmp
->BitsLock
)
150 ExFreePoolWithTag(pBmp
->BitsLock
, TAG_BITMAPOBJ
);
151 pBmp
->BitsLock
= NULL
;
159 IntCreateCompatibleBitmap(
168 if ((Width
>= 0x10000) || (Height
>= 0x10000))
170 DPRINT1("got bad width %d or height %d, please look for reason\n", Width
, Height
);
174 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
175 if (0 == Width
|| 0 == Height
)
177 Bmp
= IntGdiCreateBitmap (1, 1, 1, 1, NULL
);
181 Bmp
= IntGdiCreateBitmap(Width
, Height
, 1, Dc
->w
.bitsPerPixel
, NULL
);
188 NtGdiCreateCompatibleBitmap(
198 DPRINT("NtGdiCreateCompatibleBitmap(%04x,%d,%d, bpp:%d) = \n", hDC
, Width
, Height
, Dc
->w
.bitsPerPixel
);
202 SetLastWin32Error(ERROR_INVALID_HANDLE
);
206 Bmp
= IntCreateCompatibleBitmap(Dc
, Width
, Height
);
208 DPRINT ("\t\t%04x\n", Bmp
);
214 NtGdiGetBitmapDimension(
220 bmp
= BITMAPOBJ_LockBitmap(hBitmap
);
226 *Dimension
= bmp
->dimension
;
228 BITMAPOBJ_UnlockBitmap(bmp
);
234 NtGdiGetPixel(HDC hDC
, INT XPos
, INT YPos
)
237 COLORREF Result
= (COLORREF
)CLR_INVALID
; // default to failure
238 BOOL bInRect
= FALSE
;
239 BITMAPOBJ
*BitmapObject
;
240 SURFOBJ
*SurfaceObject
;
245 dc
= DC_LockDc (hDC
);
249 SetLastWin32Error(ERROR_INVALID_HANDLE
);
257 XPos
+= dc
->w
.DCOrgX
;
258 YPos
+= dc
->w
.DCOrgY
;
259 if ( IN_RECT(dc
->CombinedClip
->rclBounds
,XPos
,YPos
) )
262 BitmapObject
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
263 SurfaceObject
= &BitmapObject
->SurfObj
;
266 if ( dc
->w
.hPalette
!= 0 )
267 Pal
= dc
->w
.hPalette
;
268 /* FIXME: Verify if it shouldn't be PAL_BGR! */
269 XlateObj
= (XLATEOBJ
*)IntEngCreateXlate ( PAL_RGB
, 0, NULL
, Pal
);
272 // check if this DC has a DIB behind it...
273 if ( SurfaceObject
->pvScan0
) // STYPE_BITMAP == SurfaceObject->iType
275 ASSERT ( SurfaceObject
->lDelta
);
276 Result
= XLATEOBJ_iXlate(XlateObj
,
277 DibFunctionsForBitmapFormat
[SurfaceObject
->iBitmapFormat
].DIB_GetPixel ( SurfaceObject
, XPos
, YPos
) );
279 EngDeleteXlate(XlateObj
);
281 BITMAPOBJ_UnlockBitmap(BitmapObject
);
286 // if Result is still CLR_INVALID, then the "quick" method above didn't work
287 if ( bInRect
&& Result
== CLR_INVALID
)
289 // FIXME: create a 1x1 32BPP DIB, and blit to it
290 HDC hDCTmp
= NtGdiCreateCompatibleDC(hDC
);
293 static const BITMAPINFOHEADER bih
= { sizeof(BITMAPINFOHEADER
), 1, 1, 1, 32, BI_RGB
, 0, 0, 0, 0, 0 };
295 RtlMoveMemory ( &(bi
.bmiHeader
), &bih
, sizeof(bih
) );
296 hBmpTmp
= NtGdiCreateDIBitmap ( hDC
, &bi
.bmiHeader
, 0, NULL
, &bi
, DIB_RGB_COLORS
);
297 //HBITMAP hBmpTmp = IntGdiCreateBitmap ( 1, 1, 1, 32, NULL);
300 HBITMAP hBmpOld
= (HBITMAP
)NtGdiSelectObject ( hDCTmp
, hBmpTmp
);
305 NtGdiBitBlt ( hDCTmp
, 0, 0, 1, 1, hDC
, XPos
, YPos
, SRCCOPY
, 0, 0 );
306 NtGdiSelectObject ( hDCTmp
, hBmpOld
);
308 // our bitmap is no longer selected, so we can access it's stuff...
309 bmpobj
= BITMAPOBJ_LockBitmap ( hBmpTmp
);
312 Result
= *(COLORREF
*)bmpobj
->SurfObj
.pvScan0
;
313 BITMAPOBJ_UnlockBitmap ( bmpobj
);
316 NtGdiDeleteObject ( hBmpTmp
);
318 NtGdiDeleteObjectApp ( hDCTmp
);
336 /* Don't copy more bytes than the buffer has */
337 Bytes
= min(Bytes
, bmp
->SurfObj
.cjBits
);
340 /* FIXME: Call DDI CopyBits here if available */
343 DPRINT("Calling device specific BitmapBits\n");
344 if(bmp
->DDBitmap
->funcs
->pBitmapBits
)
346 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hbitmap
, bits
, count
, DDB_GET
);
350 ERR_(bitmap
)("BitmapBits == NULL??\n");
357 RtlCopyMemory(Bits
, bmp
->SurfObj
.pvBits
, Bytes
);
364 NtGdiGetBitmapBits(HBITMAP hBitmap
,
366 OUT OPTIONAL PBYTE pUnsafeBits
)
371 if (pUnsafeBits
!= NULL
&& Bytes
== 0)
376 bmp
= BITMAPOBJ_LockBitmap (hBitmap
);
379 SetLastWin32Error(ERROR_INVALID_HANDLE
);
383 /* If the bits vector is null, the function should return the read size */
384 if (pUnsafeBits
== NULL
)
386 ret
= bmp
->SurfObj
.cjBits
;
387 BITMAPOBJ_UnlockBitmap (bmp
);
391 /* Don't copy more bytes than the buffer has */
392 Bytes
= min(Bytes
, bmp
->SurfObj
.cjBits
);
396 ProbeForWrite(pUnsafeBits
, Bytes
, 1);
397 ret
= IntGetBitmapBits(bmp
, Bytes
, pUnsafeBits
);
405 BITMAPOBJ_UnlockBitmap (bmp
);
419 /* Don't copy more bytes than the buffer has */
420 Bytes
= min(Bytes
, bmp
->SurfObj
.cjBits
);
423 /* FIXME: call DDI specific function here if available */
426 DPRINT ("Calling device specific BitmapBits\n");
427 if (bmp
->DDBitmap
->funcs
->pBitmapBits
)
429 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hBitmap
, (void *) Bits
, Bytes
, DDB_SET
);
433 DPRINT ("BitmapBits == NULL??\n");
440 RtlCopyMemory(bmp
->SurfObj
.pvBits
, Bits
, Bytes
);
452 IN PBYTE pUnsafeBits
)
457 if (pUnsafeBits
== NULL
|| Bytes
== 0)
462 bmp
= BITMAPOBJ_LockBitmap(hBitmap
);
465 SetLastWin32Error(ERROR_INVALID_HANDLE
);
471 ProbeForRead(pUnsafeBits
, Bytes
, 1);
472 ret
= IntSetBitmapBits(bmp
, Bytes
, pUnsafeBits
);
480 BITMAPOBJ_UnlockBitmap(bmp
);
486 NtGdiSetBitmapDimension(
494 bmp
= BITMAPOBJ_LockBitmap(hBitmap
);
502 *Size
= bmp
->dimension
;
504 bmp
->dimension
.cx
= Width
;
505 bmp
->dimension
.cy
= Height
;
507 BITMAPOBJ_UnlockBitmap (bmp
);
520 DPRINT("0 NtGdiSetPixel X %ld Y %ld C %ld\n",X
,Y
,Color
);
523 DPRINT("0 NtGdiSetPixel X %ld Y %ld C %ld\n",X
,Y
,Color
);
525 if (NtGdiSetPixelV(hDC
,X
,Y
,Color
))
527 Color
= NtGdiGetPixel(hDC
,X
,Y
);
528 DPRINT("1 NtGdiSetPixel X %ld Y %ld C %ld\n",X
,Y
,Color
);
532 Color
= ((COLORREF
) -1);
533 DPRINT("2 NtGdiSetPixel X %ld Y %ld C %ld\n",X
,Y
,Color
);
544 HBRUSH NewBrush
= NtGdiCreateSolidBrush(Color
, NULL
);
547 if (NewBrush
== NULL
)
549 OldBrush
= NtGdiSelectObject(hDC
, NewBrush
);
550 if (OldBrush
== NULL
)
552 NtGdiDeleteObject(NewBrush
);
555 NtGdiPatBlt(hDC
, X
, Y
, 1, 1, PATCOPY
);
556 NtGdiSelectObject(hDC
, OldBrush
);
557 NtGdiDeleteObject(NewBrush
);
561 /* Internal Functions */
564 BITMAPOBJ_GetRealBitsPixel(INT nBitsPixel
)
576 if (nBitsPixel
<= 16)
578 if (nBitsPixel
<= 24)
580 if (nBitsPixel
<= 32)
587 BITMAPOBJ_GetWidthBytes (INT bmWidth
, INT bpp
)
593 return 2 * ((bmWidth
+15) >> 4);
596 bmWidth
*= 3; /* fall through */
598 return bmWidth
+ (bmWidth
& 1);
608 return 2 * ((bmWidth
+3) >> 2);
617 return ((bmWidth
* bpp
+ 15) & ~15) >> 3;
621 BITMAPOBJ_CopyBitmap(HBITMAP hBitmap
)
625 BITMAPOBJ
*Bitmap
, *resBitmap
;
633 Bitmap
= GDIOBJ_LockObj(GdiHandleTable
, hBitmap
, GDI_OBJECT_TYPE_BITMAP
);
639 BITMAP_GetObject(Bitmap
, sizeof(BITMAP
), &bm
);
641 if (Bitmap
->SurfObj
.lDelta
>= 0)
642 bm
.bmHeight
= -bm
.bmHeight
;
644 Size
.cx
= abs(bm
.bmWidth
);
645 Size
.cy
= abs(bm
.bmHeight
);
646 res
= IntCreateBitmap(Size
,
648 BitmapFormat(bm
.bmBitsPixel
* bm
.bmPlanes
, BI_RGB
),
649 (bm
.bmHeight
< 0 ? BMF_TOPDOWN
: 0) | BMF_NOZEROINIT
,
656 resBitmap
= GDIOBJ_LockObj(GdiHandleTable
, res
, GDI_OBJECT_TYPE_BITMAP
);
659 buf
= ExAllocatePoolWithTag (PagedPool
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), TAG_BITMAP
);
660 IntGetBitmapBits (Bitmap
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), buf
);
661 IntSetBitmapBits (resBitmap
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), buf
);
663 GDIOBJ_UnlockObjByPtr(GdiHandleTable
, resBitmap
);
667 GDIOBJ_UnlockObjByPtr(GdiHandleTable
, Bitmap
);
673 BITMAP_GetObject(BITMAPOBJ
* bmp
, INT Count
, LPVOID buffer
)
675 if ((UINT
)Count
< sizeof(BITMAP
)) return 0;
679 if((UINT
)Count
< sizeof(DIBSECTION
))
681 Count
= sizeof(BITMAP
);
685 Count
= sizeof(DIBSECTION
);
689 memcpy(buffer
, bmp
->dib
, Count
);
695 Count
= sizeof(BITMAP
);
700 Count
= sizeof(BITMAP
);
702 Bitmap
.bmWidth
= bmp
->SurfObj
.sizlBitmap
.cx
;
703 Bitmap
.bmHeight
= bmp
->SurfObj
.sizlBitmap
.cy
;
704 Bitmap
.bmWidthBytes
= abs(bmp
->SurfObj
.lDelta
);
706 Bitmap
.bmBitsPixel
= BitsPerFormat(bmp
->SurfObj
.iBitmapFormat
);
707 //Bitmap.bmBits = bmp->SurfObj.pvBits;
708 Bitmap
.bmBits
= NULL
; /* not set accoring wine test confirm in win2k */
709 memcpy(buffer
, &Bitmap
, Count
);