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
, Planes
* 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");
73 PBITMAPOBJ bmp
= BITMAPOBJ_LockBitmap( hBitmap
);
76 NtGdiDeleteObject(hBitmap
);
80 bmp
->flFlags
= BITMAPOBJ_IS_APIBITMAP
;
84 IntSetBitmapBits(bmp
, bmp
->SurfObj
.cjBits
, pBits
);
87 BITMAPOBJ_UnlockBitmap( bmp
);
89 DPRINT("IntGdiCreateBitmap : %dx%d, %d BPP colors, topdown %d, returning %08x\n",
90 Size
.cx
, Size
.cy
, BitsPixel
, (Height
< 0 ? 1 : 0), hBitmap
);
102 IN OPTIONAL LPBYTE pUnsafeBits
)
110 UINT cjBits
= BITMAPOBJ_GetWidthBytes(Width
, BitsPixel
) * abs(Height
);
111 ProbeForRead(pUnsafeBits
, cjBits
, 1);
114 if (0 == Width
|| 0 == Height
)
116 hBitmap
= IntGdiCreateBitmap (1, 1, 1, 1, NULL
);
120 hBitmap
= IntGdiCreateBitmap(Width
, Height
, Planes
, BitsPixel
, pUnsafeBits
);
133 BITMAP_Cleanup(PVOID ObjectBody
)
135 PBITMAPOBJ pBmp
= (PBITMAPOBJ
)ObjectBody
;
136 if (pBmp
->SurfObj
.pvBits
!= NULL
&&
137 (pBmp
->flFlags
& BITMAPOBJ_IS_APIBITMAP
))
139 if (pBmp
->dib
== NULL
)
141 if (pBmp
->SurfObj
.pvBits
!= NULL
)
142 ExFreePool(pBmp
->SurfObj
.pvBits
);
146 if (pBmp
->SurfObj
.pvBits
!= NULL
)
147 EngFreeUserMem(pBmp
->SurfObj
.pvBits
);
149 if (pBmp
->hDIBPalette
!= NULL
)
151 NtGdiDeleteObject(pBmp
->hDIBPalette
);
155 if (NULL
!= pBmp
->BitsLock
)
157 ExFreePoolWithTag(pBmp
->BitsLock
, TAG_BITMAPOBJ
);
158 pBmp
->BitsLock
= NULL
;
166 IntCreateCompatibleBitmap(
175 if ((Width
>= 0x10000) || (Height
>= 0x10000))
177 DPRINT1("got bad width %d or height %d, please look for reason\n", Width
, Height
);
181 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
182 if (0 == Width
|| 0 == Height
)
184 Bmp
= IntGdiCreateBitmap (1, 1, 1, 1, NULL
);
188 Bmp
= IntGdiCreateBitmap(abs(Width
), abs(Height
), 1, Dc
->w
.bitsPerPixel
, NULL
);
195 NtGdiCreateCompatibleBitmap(
205 DPRINT("NtGdiCreateCompatibleBitmap(%04x,%d,%d, bpp:%d) = \n", hDC
, Width
, Height
, Dc
->w
.bitsPerPixel
);
209 SetLastWin32Error(ERROR_INVALID_HANDLE
);
213 Bmp
= IntCreateCompatibleBitmap(Dc
, Width
, Height
);
215 DPRINT ("\t\t%04x\n", Bmp
);
221 NtGdiGetBitmapDimension(
227 bmp
= BITMAPOBJ_LockBitmap(hBitmap
);
233 *Dimension
= bmp
->dimension
;
235 BITMAPOBJ_UnlockBitmap(bmp
);
241 NtGdiGetPixel(HDC hDC
, INT XPos
, INT YPos
)
244 COLORREF Result
= (COLORREF
)CLR_INVALID
; // default to failure
245 BOOL bInRect
= FALSE
;
246 BITMAPOBJ
*BitmapObject
;
247 SURFOBJ
*SurfaceObject
;
252 dc
= DC_LockDc (hDC
);
256 SetLastWin32Error(ERROR_INVALID_HANDLE
);
259 if (dc
->DC_Type
== DC_TYPE_INFO
)
264 XPos
+= dc
->w
.DCOrgX
;
265 YPos
+= dc
->w
.DCOrgY
;
266 if ( IN_RECT(dc
->CombinedClip
->rclBounds
,XPos
,YPos
) )
269 BitmapObject
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
270 SurfaceObject
= &BitmapObject
->SurfObj
;
273 if ( dc
->w
.hPalette
!= 0 )
274 Pal
= dc
->w
.hPalette
;
275 /* FIXME: Verify if it shouldn't be PAL_BGR! */
276 XlateObj
= (XLATEOBJ
*)IntEngCreateXlate ( PAL_RGB
, 0, NULL
, Pal
);
279 // check if this DC has a DIB behind it...
280 if ( SurfaceObject
->pvScan0
) // STYPE_BITMAP == SurfaceObject->iType
282 ASSERT ( SurfaceObject
->lDelta
);
283 Result
= XLATEOBJ_iXlate(XlateObj
,
284 DibFunctionsForBitmapFormat
[SurfaceObject
->iBitmapFormat
].DIB_GetPixel ( SurfaceObject
, XPos
, YPos
) );
286 EngDeleteXlate(XlateObj
);
288 BITMAPOBJ_UnlockBitmap(BitmapObject
);
293 // if Result is still CLR_INVALID, then the "quick" method above didn't work
294 if ( bInRect
&& Result
== CLR_INVALID
)
296 // FIXME: create a 1x1 32BPP DIB, and blit to it
297 HDC hDCTmp
= NtGdiCreateCompatibleDC(hDC
);
300 static const BITMAPINFOHEADER bih
= { sizeof(BITMAPINFOHEADER
), 1, 1, 1, 32, BI_RGB
, 0, 0, 0, 0, 0 };
302 RtlMoveMemory ( &(bi
.bmiHeader
), &bih
, sizeof(bih
) );
303 hBmpTmp
= NtGdiCreateDIBitmap ( hDC
, &bi
.bmiHeader
, 0, NULL
, &bi
, DIB_RGB_COLORS
);
304 //HBITMAP hBmpTmp = IntGdiCreateBitmap ( 1, 1, 1, 32, NULL);
307 HBITMAP hBmpOld
= (HBITMAP
)NtGdiSelectBitmap ( hDCTmp
, hBmpTmp
);
312 NtGdiBitBlt ( hDCTmp
, 0, 0, 1, 1, hDC
, XPos
, YPos
, SRCCOPY
, 0, 0 );
313 NtGdiSelectBitmap ( hDCTmp
, hBmpOld
);
315 // our bitmap is no longer selected, so we can access it's stuff...
316 bmpobj
= BITMAPOBJ_LockBitmap ( hBmpTmp
);
319 Result
= *(COLORREF
*)bmpobj
->SurfObj
.pvScan0
;
320 BITMAPOBJ_UnlockBitmap ( bmpobj
);
323 NtGdiDeleteObject ( hBmpTmp
);
325 NtGdiDeleteObjectApp ( hDCTmp
);
343 /* Don't copy more bytes than the buffer has */
344 Bytes
= min(Bytes
, bmp
->SurfObj
.cjBits
);
347 /* FIXME: Call DDI CopyBits here if available */
350 DPRINT("Calling device specific BitmapBits\n");
351 if(bmp
->DDBitmap
->funcs
->pBitmapBits
)
353 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hbitmap
, bits
, count
, DDB_GET
);
357 ERR_(bitmap
)("BitmapBits == NULL??\n");
364 RtlCopyMemory(Bits
, bmp
->SurfObj
.pvBits
, Bytes
);
371 NtGdiGetBitmapBits(HBITMAP hBitmap
,
373 OUT OPTIONAL PBYTE pUnsafeBits
)
378 if (pUnsafeBits
!= NULL
&& Bytes
== 0)
383 bmp
= BITMAPOBJ_LockBitmap (hBitmap
);
386 SetLastWin32Error(ERROR_INVALID_HANDLE
);
390 /* If the bits vector is null, the function should return the read size */
391 if (pUnsafeBits
== NULL
)
393 ret
= bmp
->SurfObj
.cjBits
;
394 BITMAPOBJ_UnlockBitmap (bmp
);
398 /* Don't copy more bytes than the buffer has */
399 Bytes
= min(Bytes
, bmp
->SurfObj
.cjBits
);
403 ProbeForWrite(pUnsafeBits
, Bytes
, 1);
404 ret
= IntGetBitmapBits(bmp
, Bytes
, pUnsafeBits
);
412 BITMAPOBJ_UnlockBitmap (bmp
);
426 /* Don't copy more bytes than the buffer has */
427 Bytes
= min(Bytes
, bmp
->SurfObj
.cjBits
);
430 /* FIXME: call DDI specific function here if available */
433 DPRINT ("Calling device specific BitmapBits\n");
434 if (bmp
->DDBitmap
->funcs
->pBitmapBits
)
436 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hBitmap
, (void *) Bits
, Bytes
, DDB_SET
);
440 DPRINT ("BitmapBits == NULL??\n");
447 RtlCopyMemory(bmp
->SurfObj
.pvBits
, Bits
, Bytes
);
459 IN PBYTE pUnsafeBits
)
464 if (pUnsafeBits
== NULL
|| Bytes
== 0)
469 bmp
= BITMAPOBJ_LockBitmap(hBitmap
);
472 SetLastWin32Error(ERROR_INVALID_HANDLE
);
478 ProbeForRead(pUnsafeBits
, Bytes
, 1);
479 ret
= IntSetBitmapBits(bmp
, Bytes
, pUnsafeBits
);
487 BITMAPOBJ_UnlockBitmap(bmp
);
493 NtGdiSetBitmapDimension(
501 bmp
= BITMAPOBJ_LockBitmap(hBitmap
);
509 *Size
= bmp
->dimension
;
511 bmp
->dimension
.cx
= Width
;
512 bmp
->dimension
.cy
= Height
;
514 BITMAPOBJ_UnlockBitmap (bmp
);
526 HBRUSH NewBrush
= NtGdiCreateSolidBrush(Color
, NULL
);
529 if (NewBrush
== NULL
)
531 OldBrush
= NtGdiSelectBrush(hDC
, NewBrush
);
532 if (OldBrush
== NULL
)
534 NtGdiDeleteObject(NewBrush
);
537 NtGdiPatBlt(hDC
, X
, Y
, 1, 1, PATCOPY
);
538 NtGdiSelectBrush(hDC
, OldBrush
);
539 NtGdiDeleteObject(NewBrush
);
550 DPRINT("0 NtGdiSetPixel X %ld Y %ld C %ld\n",X
,Y
,Color
);
552 if (GdiSetPixelV(hDC
,X
,Y
,Color
))
554 Color
= NtGdiGetPixel(hDC
,X
,Y
);
555 DPRINT("1 NtGdiSetPixel X %ld Y %ld C %ld\n",X
,Y
,Color
);
559 Color
= ((COLORREF
) CLR_INVALID
);
560 DPRINT("2 NtGdiSetPixel X %ld Y %ld C %ld\n",X
,Y
,Color
);
565 /* Internal Functions */
568 BITMAPOBJ_GetRealBitsPixel(INT nBitsPixel
)
580 if (nBitsPixel
<= 16)
582 if (nBitsPixel
<= 24)
584 if (nBitsPixel
<= 32)
591 BITMAPOBJ_GetWidthBytes (INT bmWidth
, INT bpp
)
597 return 2 * ((bmWidth
+15) >> 4);
600 bmWidth
*= 3; /* fall through */
602 return bmWidth
+ (bmWidth
& 1);
612 return 2 * ((bmWidth
+3) >> 2);
621 return ((bmWidth
* bpp
+ 15) & ~15) >> 3;
625 BITMAPOBJ_CopyBitmap(HBITMAP hBitmap
)
629 BITMAPOBJ
*Bitmap
, *resBitmap
;
637 Bitmap
= GDIOBJ_LockObj(hBitmap
, GDI_OBJECT_TYPE_BITMAP
);
643 BITMAP_GetObject(Bitmap
, sizeof(BITMAP
), &bm
);
645 if (Bitmap
->SurfObj
.lDelta
>= 0)
646 bm
.bmHeight
= -bm
.bmHeight
;
648 Size
.cx
= abs(bm
.bmWidth
);
649 Size
.cy
= abs(bm
.bmHeight
);
650 res
= IntCreateBitmap(Size
,
652 BitmapFormat(bm
.bmBitsPixel
* bm
.bmPlanes
, BI_RGB
),
653 (bm
.bmHeight
< 0 ? BMF_TOPDOWN
: 0) | BMF_NOZEROINIT
,
660 resBitmap
= GDIOBJ_LockObj(res
, GDI_OBJECT_TYPE_BITMAP
);
663 buf
= ExAllocatePoolWithTag (PagedPool
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), TAG_BITMAP
);
666 GDIOBJ_UnlockObjByPtr((POBJ
)resBitmap
);
667 GDIOBJ_UnlockObjByPtr((POBJ
)Bitmap
);
668 NtGdiDeleteObject(res
);
671 IntGetBitmapBits (Bitmap
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), buf
);
672 IntSetBitmapBits (resBitmap
, bm
.bmWidthBytes
* abs(bm
.bmHeight
), buf
);
674 resBitmap
->flFlags
= Bitmap
->flFlags
;
675 GDIOBJ_UnlockObjByPtr((POBJ
)resBitmap
);
679 NtGdiDeleteObject(res
);
684 GDIOBJ_UnlockObjByPtr((POBJ
)Bitmap
);
690 BITMAP_GetObject(BITMAPOBJ
* bmp
, INT Count
, LPVOID buffer
)
692 if ((UINT
)Count
< sizeof(BITMAP
)) return 0;
696 if((UINT
)Count
< sizeof(DIBSECTION
))
698 Count
= sizeof(BITMAP
);
702 Count
= sizeof(DIBSECTION
);
706 memcpy(buffer
, bmp
->dib
, Count
);
712 Count
= sizeof(BITMAP
);
717 Count
= sizeof(BITMAP
);
719 Bitmap
.bmWidth
= bmp
->SurfObj
.sizlBitmap
.cx
;
720 Bitmap
.bmHeight
= bmp
->SurfObj
.sizlBitmap
.cy
;
721 Bitmap
.bmWidthBytes
= abs(bmp
->SurfObj
.lDelta
);
723 Bitmap
.bmBitsPixel
= BitsPerFormat(bmp
->SurfObj
.iBitmapFormat
);
724 //Bitmap.bmBits = bmp->SurfObj.pvBits;
725 Bitmap
.bmBits
= NULL
; /* not set accoring wine test confirm in win2k */
726 memcpy(buffer
, &Bitmap
, Count
);