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
)
46 /* NOTE: Windows also doesn't store nr. of planes separately! */
47 BitsPixel
= BITMAPOBJ_GetRealBitsPixel(BitsPixel
* Planes
);
49 /* Check parameters */
50 if (BitsPixel
== 0 || Width
< 0)
52 DPRINT1("Width = %d, Height = %d BitsPixel = %d\n", Width
, Height
, BitsPixel
);
53 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
57 WidthBytes
= BITMAPOBJ_GetWidthBytes(Width
, BitsPixel
);
60 Size
.cy
= abs(Height
);
62 /* Create the bitmap object. */
63 hBitmap
= IntCreateBitmap(Size
, WidthBytes
,
64 BitmapFormat(BitsPixel
, BI_RGB
),
65 (Height
< 0 ? BMF_TOPDOWN
: 0) |
66 (NULL
== pBits
? 0 : BMF_NOZEROINIT
), NULL
);
69 DPRINT("IntGdiCreateBitmap: returned 0\n");
75 PBITMAPOBJ bmp
= BITMAPOBJ_LockBitmap( hBitmap
);
78 NtGdiDeleteObject(hBitmap
);
82 bmp
->flFlags
= BITMAPOBJ_IS_APIBITMAP
;
84 IntSetBitmapBits(bmp
, bmp
->SurfObj
.cjBits
, pBits
);
87 BITMAPOBJ_UnlockBitmap( bmp
);
90 DPRINT("IntGdiCreateBitmap : %dx%d, %d BPP colors, topdown %d, returning %08x\n",
91 Size
.cx
, Size
.cy
, BitsPixel
, (Height
< 0 ? 1 : 0), hBitmap
);
103 IN OPTIONAL LPBYTE pUnsafeBits
)
111 UINT cjBits
= BITMAPOBJ_GetWidthBytes(Width
, BitsPixel
) * Height
;
112 ProbeForRead(pUnsafeBits
, cjBits
, 1);
115 if (0 == Width
|| 0 == Height
)
117 hBitmap
= IntGdiCreateBitmap (1, 1, 1, 1, NULL
);
121 hBitmap
= IntGdiCreateBitmap(Width
, Height
, Planes
, BitsPixel
, pUnsafeBits
);
134 BITMAP_Cleanup(PVOID ObjectBody
)
136 PBITMAPOBJ pBmp
= (PBITMAPOBJ
)ObjectBody
;
137 if (pBmp
->SurfObj
.pvBits
!= NULL
&&
138 (pBmp
->flFlags
& BITMAPOBJ_IS_APIBITMAP
))
140 if (pBmp
->dib
== NULL
)
142 if (pBmp
->SurfObj
.pvBits
!= NULL
)
143 ExFreePool(pBmp
->SurfObj
.pvBits
);
147 if (pBmp
->SurfObj
.pvBits
!= NULL
)
148 EngFreeUserMem(pBmp
->SurfObj
.pvBits
);
150 if (pBmp
->hDIBPalette
!= NULL
)
152 NtGdiDeleteObject(pBmp
->hDIBPalette
);
156 if (NULL
!= pBmp
->BitsLock
)
158 ExFreePoolWithTag(pBmp
->BitsLock
, TAG_BITMAPOBJ
);
159 pBmp
->BitsLock
= NULL
;
167 IntCreateCompatibleBitmap(
176 if ((Width
>= 0x10000) || (Height
>= 0x10000))
178 DPRINT1("got bad width %d or height %d, please look for reason\n", Width
, Height
);
182 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
183 if (0 == Width
|| 0 == Height
)
185 Bmp
= IntGdiCreateBitmap (1, 1, 1, 1, NULL
);
189 Bmp
= IntGdiCreateBitmap(Width
, Height
, 1, Dc
->w
.bitsPerPixel
, NULL
);
196 NtGdiCreateCompatibleBitmap(
206 DPRINT("NtGdiCreateCompatibleBitmap(%04x,%d,%d, bpp:%d) = \n", hDC
, Width
, Height
, Dc
->w
.bitsPerPixel
);
210 SetLastWin32Error(ERROR_INVALID_HANDLE
);
214 Bmp
= IntCreateCompatibleBitmap(Dc
, Width
, Height
);
216 DPRINT ("\t\t%04x\n", Bmp
);
222 NtGdiGetBitmapDimension(
228 bmp
= BITMAPOBJ_LockBitmap(hBitmap
);
234 *Dimension
= bmp
->dimension
;
236 BITMAPOBJ_UnlockBitmap(bmp
);
242 NtGdiGetPixel(HDC hDC
, INT XPos
, INT YPos
)
245 COLORREF Result
= (COLORREF
)CLR_INVALID
; // default to failure
246 BOOL bInRect
= FALSE
;
247 BITMAPOBJ
*BitmapObject
;
248 SURFOBJ
*SurfaceObject
;
253 dc
= DC_LockDc (hDC
);
257 SetLastWin32Error(ERROR_INVALID_HANDLE
);
265 XPos
+= dc
->w
.DCOrgX
;
266 YPos
+= dc
->w
.DCOrgY
;
267 if ( IN_RECT(dc
->CombinedClip
->rclBounds
,XPos
,YPos
) )
270 BitmapObject
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
271 SurfaceObject
= &BitmapObject
->SurfObj
;
274 if ( dc
->w
.hPalette
!= 0 )
275 Pal
= dc
->w
.hPalette
;
276 /* FIXME: Verify if it shouldn't be PAL_BGR! */
277 XlateObj
= (XLATEOBJ
*)IntEngCreateXlate ( PAL_RGB
, 0, NULL
, Pal
);
280 // check if this DC has a DIB behind it...
281 if ( SurfaceObject
->pvScan0
) // STYPE_BITMAP == SurfaceObject->iType
283 ASSERT ( SurfaceObject
->lDelta
);
284 Result
= XLATEOBJ_iXlate(XlateObj
,
285 DibFunctionsForBitmapFormat
[SurfaceObject
->iBitmapFormat
].DIB_GetPixel ( SurfaceObject
, XPos
, YPos
) );
287 EngDeleteXlate(XlateObj
);
289 BITMAPOBJ_UnlockBitmap(BitmapObject
);
294 // if Result is still CLR_INVALID, then the "quick" method above didn't work
295 if ( bInRect
&& Result
== CLR_INVALID
)
297 // FIXME: create a 1x1 32BPP DIB, and blit to it
298 HDC hDCTmp
= NtGdiCreateCompatibleDC(hDC
);
301 static const BITMAPINFOHEADER bih
= { sizeof(BITMAPINFOHEADER
), 1, 1, 1, 32, BI_RGB
, 0, 0, 0, 0, 0 };
303 RtlMoveMemory ( &(bi
.bmiHeader
), &bih
, sizeof(bih
) );
304 hBmpTmp
= NtGdiCreateDIBitmap ( hDC
, &bi
.bmiHeader
, 0, NULL
, &bi
, DIB_RGB_COLORS
);
305 //HBITMAP hBmpTmp = IntGdiCreateBitmap ( 1, 1, 1, 32, NULL);
308 HBITMAP hBmpOld
= (HBITMAP
)NtGdiSelectObject ( hDCTmp
, hBmpTmp
);
313 NtGdiBitBlt ( hDCTmp
, 0, 0, 1, 1, hDC
, XPos
, YPos
, SRCCOPY
, 0, 0 );
314 NtGdiSelectObject ( hDCTmp
, hBmpOld
);
316 // our bitmap is no longer selected, so we can access it's stuff...
317 bmpobj
= BITMAPOBJ_LockBitmap ( hBmpTmp
);
320 Result
= *(COLORREF
*)bmpobj
->SurfObj
.pvScan0
;
321 BITMAPOBJ_UnlockBitmap ( bmpobj
);
324 NtGdiDeleteObject ( hBmpTmp
);
326 NtGdiDeleteObjectApp ( hDCTmp
);
344 /* Don't copy more bytes than the buffer has */
345 Bytes
= min(Bytes
, bmp
->SurfObj
.cjBits
);
348 /* FIXME: Call DDI CopyBits here if available */
351 DPRINT("Calling device specific BitmapBits\n");
352 if(bmp
->DDBitmap
->funcs
->pBitmapBits
)
354 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hbitmap
, bits
, count
, DDB_GET
);
358 ERR_(bitmap
)("BitmapBits == NULL??\n");
365 RtlCopyMemory(Bits
, bmp
->SurfObj
.pvBits
, Bytes
);
372 NtGdiGetBitmapBits(HBITMAP hBitmap
,
374 OUT OPTIONAL PBYTE pUnsafeBits
)
379 if (pUnsafeBits
!= NULL
&& Bytes
== 0)
384 bmp
= BITMAPOBJ_LockBitmap (hBitmap
);
387 SetLastWin32Error(ERROR_INVALID_HANDLE
);
391 /* If the bits vector is null, the function should return the read size */
392 if (pUnsafeBits
== NULL
)
394 ret
= bmp
->SurfObj
.cjBits
;
395 BITMAPOBJ_UnlockBitmap (bmp
);
399 /* Don't copy more bytes than the buffer has */
400 Bytes
= min(Bytes
, bmp
->SurfObj
.cjBits
);
404 ProbeForWrite(pUnsafeBits
, Bytes
, 1);
405 ret
= IntGetBitmapBits(bmp
, Bytes
, pUnsafeBits
);
413 BITMAPOBJ_UnlockBitmap (bmp
);
427 /* Don't copy more bytes than the buffer has */
428 Bytes
= min(Bytes
, bmp
->SurfObj
.cjBits
);
431 /* FIXME: call DDI specific function here if available */
434 DPRINT ("Calling device specific BitmapBits\n");
435 if (bmp
->DDBitmap
->funcs
->pBitmapBits
)
437 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hBitmap
, (void *) Bits
, Bytes
, DDB_SET
);
441 DPRINT ("BitmapBits == NULL??\n");
448 RtlCopyMemory(bmp
->SurfObj
.pvBits
, Bits
, Bytes
);
460 IN PBYTE pUnsafeBits
)
465 if (pUnsafeBits
== NULL
|| Bytes
== 0)
470 bmp
= BITMAPOBJ_LockBitmap(hBitmap
);
473 SetLastWin32Error(ERROR_INVALID_HANDLE
);
479 ProbeForRead(pUnsafeBits
, Bytes
, 1);
480 ret
= IntSetBitmapBits(bmp
, Bytes
, pUnsafeBits
);
488 BITMAPOBJ_UnlockBitmap(bmp
);
494 NtGdiSetBitmapDimension(
502 bmp
= BITMAPOBJ_LockBitmap(hBitmap
);
510 *Size
= bmp
->dimension
;
512 bmp
->dimension
.cx
= Width
;
513 bmp
->dimension
.cy
= Height
;
515 BITMAPOBJ_UnlockBitmap (bmp
);
527 HBRUSH NewBrush
= NtGdiCreateSolidBrush(Color
, NULL
);
530 if (NewBrush
== NULL
)
532 OldBrush
= NtGdiSelectObject(hDC
, NewBrush
);
533 if (OldBrush
== NULL
)
535 NtGdiDeleteObject(NewBrush
);
538 NtGdiPatBlt(hDC
, X
, Y
, 1, 1, PATCOPY
);
539 NtGdiSelectObject(hDC
, OldBrush
);
540 NtGdiDeleteObject(NewBrush
);
551 DPRINT("0 NtGdiSetPixel X %ld Y %ld C %ld\n",X
,Y
,Color
);
553 if (GdiSetPixelV(hDC
,X
,Y
,Color
))
555 Color
= NtGdiGetPixel(hDC
,X
,Y
);
556 DPRINT("1 NtGdiSetPixel X %ld Y %ld C %ld\n",X
,Y
,Color
);
560 Color
= ((COLORREF
) CLR_INVALID
);
561 DPRINT("2 NtGdiSetPixel X %ld Y %ld C %ld\n",X
,Y
,Color
);
566 /* Internal Functions */
569 BITMAPOBJ_GetRealBitsPixel(INT nBitsPixel
)
581 if (nBitsPixel
<= 16)
583 if (nBitsPixel
<= 24)
585 if (nBitsPixel
<= 32)
592 BITMAPOBJ_GetWidthBytes (INT bmWidth
, INT bpp
)
598 return 2 * ((bmWidth
+15) >> 4);
601 bmWidth
*= 3; /* fall through */
603 return bmWidth
+ (bmWidth
& 1);
613 return 2 * ((bmWidth
+3) >> 2);
622 return ((bmWidth
* bpp
+ 15) & ~15) >> 3;
626 BITMAPOBJ_CopyBitmap(HBITMAP hBitmap
)
630 BITMAPOBJ
*Bitmap
, *resBitmap
;
638 Bitmap
= GDIOBJ_LockObj(GdiHandleTable
, hBitmap
, GDI_OBJECT_TYPE_BITMAP
);
644 BITMAP_GetObject(Bitmap
, sizeof(BITMAP
), &bm
);
646 if (Bitmap
->SurfObj
.lDelta
>= 0)
647 bm
.bmHeight
= -bm
.bmHeight
;
649 Size
.cx
= abs(bm
.bmWidth
);
650 Size
.cy
= abs(bm
.bmHeight
);
651 res
= IntCreateBitmap(Size
,
653 BitmapFormat(bm
.bmBitsPixel
* bm
.bmPlanes
, BI_RGB
),
654 (bm
.bmHeight
< 0 ? BMF_TOPDOWN
: 0) | BMF_NOZEROINIT
,
661 resBitmap
= GDIOBJ_LockObj(GdiHandleTable
, res
, GDI_OBJECT_TYPE_BITMAP
);
664 buf
= ExAllocatePoolWithTag (PagedPool
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), TAG_BITMAP
);
665 IntGetBitmapBits (Bitmap
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), buf
);
666 IntSetBitmapBits (resBitmap
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), buf
);
668 GDIOBJ_UnlockObjByPtr(GdiHandleTable
, resBitmap
);
672 GDIOBJ_UnlockObjByPtr(GdiHandleTable
, Bitmap
);
678 BITMAP_GetObject(BITMAPOBJ
* bmp
, INT Count
, LPVOID buffer
)
680 if ((UINT
)Count
< sizeof(BITMAP
)) return 0;
684 if((UINT
)Count
< sizeof(DIBSECTION
))
686 Count
= sizeof(BITMAP
);
690 Count
= sizeof(DIBSECTION
);
694 memcpy(buffer
, bmp
->dib
, Count
);
700 Count
= sizeof(BITMAP
);
705 Count
= sizeof(BITMAP
);
707 Bitmap
.bmWidth
= bmp
->SurfObj
.sizlBitmap
.cx
;
708 Bitmap
.bmHeight
= bmp
->SurfObj
.sizlBitmap
.cy
;
709 Bitmap
.bmWidthBytes
= abs(bmp
->SurfObj
.lDelta
);
711 Bitmap
.bmBitsPixel
= BitsPerFormat(bmp
->SurfObj
.iBitmapFormat
);
712 //Bitmap.bmBits = bmp->SurfObj.pvBits;
713 Bitmap
.bmBits
= NULL
; /* not set accoring wine test confirm in win2k */
714 memcpy(buffer
, &Bitmap
, Count
);