4 * ReactOS W32 Subsystem
5 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 IntSetDIBColorTable(HDC hDC
, UINT StartIndex
, UINT Entries
, CONST RGBQUAD
*Colors
)
35 if (!(dc
= DC_LockDc(hDC
))) return 0;
36 if (dc
->DC_Type
== DC_TYPE_INFO
)
42 BitmapObj
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
43 if (BitmapObj
== NULL
)
46 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
50 if (BitmapObj
->dib
== NULL
)
52 BITMAPOBJ_UnlockBitmap(BitmapObj
);
54 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
58 if (BitmapObj
->dib
->dsBmih
.biBitCount
<= 8 &&
59 StartIndex
< (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
61 if (StartIndex
+ Entries
> (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
62 Entries
= (1 << BitmapObj
->dib
->dsBmih
.biBitCount
) - StartIndex
;
64 PalGDI
= PALETTE_LockPalette(BitmapObj
->hDIBPalette
);
67 BITMAPOBJ_UnlockBitmap(BitmapObj
);
69 SetLastWin32Error(ERROR_INVALID_HANDLE
);
73 for (Index
= StartIndex
;
74 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
77 PalGDI
->IndexedColors
[Index
].peRed
= Colors
[Index
- StartIndex
].rgbRed
;
78 PalGDI
->IndexedColors
[Index
].peGreen
= Colors
[Index
- StartIndex
].rgbGreen
;
79 PalGDI
->IndexedColors
[Index
].peBlue
= Colors
[Index
- StartIndex
].rgbBlue
;
81 PALETTE_UnlockPalette(PalGDI
);
86 BITMAPOBJ_UnlockBitmap(BitmapObj
);
93 IntGetDIBColorTable(HDC hDC
, UINT StartIndex
, UINT Entries
, RGBQUAD
*Colors
)
100 if (!(dc
= DC_LockDc(hDC
))) return 0;
101 if (dc
->DC_Type
== DC_TYPE_INFO
)
107 BitmapObj
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
108 if (BitmapObj
== NULL
)
111 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
115 if (BitmapObj
->dib
== NULL
)
117 BITMAPOBJ_UnlockBitmap(BitmapObj
);
119 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
123 if (BitmapObj
->dib
->dsBmih
.biBitCount
<= 8 &&
124 StartIndex
< (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
126 if (StartIndex
+ Entries
> (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
127 Entries
= (1 << BitmapObj
->dib
->dsBmih
.biBitCount
) - StartIndex
;
129 PalGDI
= PALETTE_LockPalette(BitmapObj
->hDIBPalette
);
132 BITMAPOBJ_UnlockBitmap(BitmapObj
);
134 SetLastWin32Error(ERROR_INVALID_HANDLE
);
138 for (Index
= StartIndex
;
139 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
142 Colors
[Index
- StartIndex
].rgbRed
= PalGDI
->IndexedColors
[Index
].peRed
;
143 Colors
[Index
- StartIndex
].rgbGreen
= PalGDI
->IndexedColors
[Index
].peGreen
;
144 Colors
[Index
- StartIndex
].rgbBlue
= PalGDI
->IndexedColors
[Index
].peBlue
;
146 PALETTE_UnlockPalette(PalGDI
);
151 BITMAPOBJ_UnlockBitmap(BitmapObj
);
157 // Converts a DIB to a device-dependent bitmap
165 CONST BITMAPINFO
*bmi
,
169 HBITMAP SourceBitmap
;
172 SURFOBJ
*DestSurf
, *SourceSurf
;
179 HPALETTE DDB_Palette
, DIB_Palette
;
180 ULONG DDB_Palette_Type
, DIB_Palette_Type
;
184 if (!(bitmap
= BITMAPOBJ_LockBitmap(hBitmap
)))
190 //if (ColorUse == DIB_PAL_COLORS)
191 // lpRGB = DIB_MapPaletteColors(hDC, bmi);
193 // lpRGB = &bmi->bmiColors;
195 DestSurf
= &bitmap
->SurfObj
;
197 // Create source surface
198 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
199 SourceSize
.cy
= ScanLines
;
201 // Determine width of DIB
202 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
);
204 SourceBitmap
= EngCreateBitmap(SourceSize
,
206 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
207 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
209 if (0 == SourceBitmap
)
211 BITMAPOBJ_UnlockBitmap(bitmap
);
212 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
216 SourceSurf
= EngLockSurface((HSURF
)SourceBitmap
);
217 if (NULL
== SourceSurf
)
219 EngDeleteSurface((HSURF
)SourceBitmap
);
220 BITMAPOBJ_UnlockBitmap(bitmap
);
221 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
225 // Destination palette obtained from the hDC
226 hDCPalette
= PALETTE_LockPalette(((GDIDEVICE
*)DC
->pPDev
)->DevInfo
.hpalDefault
);
227 if (NULL
== hDCPalette
)
229 EngUnlockSurface(SourceSurf
);
230 EngDeleteSurface((HSURF
)SourceBitmap
);
231 BITMAPOBJ_UnlockBitmap(bitmap
);
232 SetLastWin32Error(ERROR_INVALID_HANDLE
);
235 DDB_Palette_Type
= hDCPalette
->Mode
;
236 DDB_Palette
= ((GDIDEVICE
*)DC
->pPDev
)->DevInfo
.hpalDefault
;
237 PALETTE_UnlockPalette(hDCPalette
);
239 // Source palette obtained from the BITMAPINFO
240 DIB_Palette
= BuildDIBPalette ( (PBITMAPINFO
)bmi
, (PINT
)&DIB_Palette_Type
);
241 if (NULL
== DIB_Palette
)
243 EngUnlockSurface(SourceSurf
);
244 EngDeleteSurface((HSURF
)SourceBitmap
);
245 BITMAPOBJ_UnlockBitmap(bitmap
);
246 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
250 // Determine XLATEOBJ for color translation
251 XlateObj
= IntEngCreateXlate(DDB_Palette_Type
, DIB_Palette_Type
, DDB_Palette
, DIB_Palette
);
252 if (NULL
== XlateObj
)
254 PALETTE_FreePaletteByHandle(DIB_Palette
);
255 EngUnlockSurface(SourceSurf
);
256 EngDeleteSurface((HSURF
)SourceBitmap
);
257 BITMAPOBJ_UnlockBitmap(bitmap
);
258 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
266 // Determine destination rectangle
268 DestRect
.top
= abs(bmi
->bmiHeader
.biHeight
) - StartScan
- ScanLines
;
269 DestRect
.right
= SourceSize
.cx
;
270 DestRect
.bottom
= DestRect
.top
+ ScanLines
;
272 copyBitsResult
= EngCopyBits(DestSurf
, SourceSurf
, NULL
, XlateObj
, &DestRect
, &ZeroPoint
);
274 // If it succeeded, return number of scanlines copies
275 if(copyBitsResult
== TRUE
)
277 result
= SourceSize
.cy
- 1;
281 EngDeleteXlate(XlateObj
);
282 PALETTE_FreePaletteByHandle(DIB_Palette
);
283 EngUnlockSurface(SourceSurf
);
284 EngDeleteSurface((HSURF
)SourceBitmap
);
286 // if (ColorUse == DIB_PAL_COLORS)
287 // WinFree((LPSTR)lpRGB);
289 BITMAPOBJ_UnlockBitmap(bitmap
);
294 // Converts a DIB to a device-dependent bitmap
302 CONST BITMAPINFO
*bmi
,
311 SetLastWin32Error(ERROR_INVALID_HANDLE
);
314 if (Dc
->DC_Type
== DC_TYPE_INFO
)
320 Ret
= IntSetDIBits(Dc
, hBitmap
, StartScan
, ScanLines
, Bits
, bmi
, ColorUse
);
332 NtGdiSetDIBitsToDeviceInternal(
347 IN BOOL bTransformCoordinates
,
348 IN OPTIONAL HANDLE hcmXform
352 NTSTATUS Status
= STATUS_SUCCESS
;
354 HBITMAP hSourceBitmap
= NULL
;
355 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
;
360 XLATEOBJ
*XlateObj
= NULL
;
362 HPALETTE DDBPalette
, DIBPalette
= NULL
;
363 ULONG DDBPaletteType
, DIBPaletteType
;
367 pDC
= DC_LockDc(hDC
);
370 SetLastWin32Error(ERROR_INVALID_HANDLE
);
373 if (pDC
->DC_Type
== DC_TYPE_INFO
)
379 pDestSurf
= EngLockSurface((HSURF
)pDC
->w
.hBitmap
);
382 /* FIXME: SetLastError ? */
389 if (bTransformCoordinates
)
391 CoordLPtoDP(pDC
, (LPPOINT
)&rcDest
);
393 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
394 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
395 rcDest
.right
= rcDest
.left
+ Width
;
396 rcDest
.bottom
= rcDest
.top
+ Height
;
400 /* Enter SEH, as the bits are user mode */
403 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
404 SourceSize
.cy
= ScanLines
;
405 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
);
407 ProbeForRead(Bits
, DIBWidth
* abs(bmi
->bmiHeader
.biHeight
), 1);
408 hSourceBitmap
= EngCreateBitmap(SourceSize
,
410 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
411 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
415 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
416 Status
= STATUS_NO_MEMORY
;
420 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
423 Status
= STATUS_UNSUCCESSFUL
;
428 /* Obtain destination palette from the DC */
429 pDCPalette
= PALETTE_LockPalette(((GDIDEVICE
*)pDC
->pPDev
)->DevInfo
.hpalDefault
);
432 SetLastWin32Error(ERROR_INVALID_HANDLE
);
433 Status
= STATUS_UNSUCCESSFUL
;
436 DDBPaletteType
= pDCPalette
->Mode
;
437 DDBPalette
= ((GDIDEVICE
*)pDC
->pPDev
)->DevInfo
.hpalDefault
;
438 PALETTE_UnlockPalette(pDCPalette
);
440 DIBPalette
= BuildDIBPalette(bmi
, (PINT
)&DIBPaletteType
);
443 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
444 Status
= STATUS_NO_MEMORY
;
448 /* Determine XlateObj */
449 XlateObj
= IntEngCreateXlate(DDBPaletteType
, DIBPaletteType
, DDBPalette
, DIBPalette
);
452 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
453 Status
= STATUS_NO_MEMORY
;
458 Status
= IntEngBitBlt(pDestSurf
,
468 ROP3_TO_ROP4(SRCCOPY
));
473 Status
= _SEH_GetExceptionCode();
477 if (NT_SUCCESS(Status
))
479 /* FIXME: Should probably be only the number of lines actually copied */
483 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
484 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
485 if (XlateObj
) EngDeleteXlate(XlateObj
);
486 if (DIBPalette
) PALETTE_FreePaletteByHandle(DIBPalette
);
487 EngUnlockSurface(pDestSurf
);
494 /* Converts a device-dependent bitmap to a DIB */
496 NtGdiGetDIBitsInternal(HDC hDC
,
507 BITMAPOBJ
*BitmapObj
= NULL
;
508 HBITMAP hDestBitmap
= NULL
;
509 HPALETTE hSourcePalette
= NULL
;
510 HPALETTE hDestPalette
= NULL
;
511 PPALGDI SourcePalette
= NULL
;
512 PPALGDI DestPalette
= NULL
;
513 NTSTATUS Status
= STATUS_SUCCESS
;
515 BOOL bPaletteMatch
= FALSE
;
517 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
519 /* Get handle for the palette in DC. */
521 if (Dc
== NULL
) return 0;
522 if (Dc
->DC_Type
== DC_TYPE_INFO
)
527 /* Source palette obtained from the windows hdc */
528 hSourcePalette
= Dc
->DcLevel
.hpal
;
531 /* don't do anything if we fail this */
532 if (Usage
!= DIB_RGB_COLORS
&& Usage
!= DIB_PAL_COLORS
)
535 /* Get a pointer to the source bitmap object */
536 BitmapObj
= BITMAPOBJ_LockBitmap(hBitmap
);
537 if (BitmapObj
== NULL
)
540 /* fill out the BITMAPINFO struct */
545 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
547 BITMAPCOREHEADER
* coreheader
;
549 ProbeForWrite(Info
, sizeof(BITMAPINFO
), 1);
551 coreheader
= (BITMAPCOREHEADER
*) Info
;
552 coreheader
->bcWidth
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
553 coreheader
->bcPlanes
= 1;
554 coreheader
->bcBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
555 /* Resulting height may be smaller than original height */
556 coreheader
->bcHeight
= min(ScanLines
, BitmapObj
->SurfObj
.sizlBitmap
.cy
- StartScan
);
558 if (BitmapObj
->SurfObj
.lDelta
> 0)
559 coreheader
->bcHeight
= -coreheader
->bcHeight
;
562 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
564 ProbeForWrite(Info
, sizeof(BITMAPINFO
), 1);
566 Info
->bmiHeader
.biWidth
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
567 /* Resulting height may be smaller than original height */
568 Info
->bmiHeader
.biHeight
= min(ScanLines
, BitmapObj
->SurfObj
.sizlBitmap
.cy
- StartScan
);
569 /* Report negtive height for top-down bitmaps. */
570 if (BitmapObj
->SurfObj
.lDelta
> 0)
571 Info
->bmiHeader
.biHeight
= -Info
->bmiHeader
.biHeight
;
572 Info
->bmiHeader
.biPlanes
= 1;
573 Info
->bmiHeader
.biBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
574 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
576 switch (BitmapObj
->SurfObj
.iBitmapFormat
)
584 Info
->bmiHeader
.biCompression
= BI_RGB
;
587 Info
->bmiHeader
.biCompression
= BI_RLE4
;
590 Info
->bmiHeader
.biCompression
= BI_RLE8
;
593 Info
->bmiHeader
.biCompression
= BI_JPEG
;
596 Info
->bmiHeader
.biCompression
= BI_PNG
;
599 /* Image size has to be calculated */
600 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBWidthBytes(Info
->bmiHeader
.biWidth
,
601 Info
->bmiHeader
.biBitCount
) * Info
->bmiHeader
.biHeight
;
602 Info
->bmiHeader
.biXPelsPerMeter
= 0; /* FIXME */
603 Info
->bmiHeader
.biYPelsPerMeter
= 0; /* FIXME */
604 Info
->bmiHeader
.biClrUsed
= 0;
605 Info
->bmiHeader
.biClrImportant
= 1 << Info
->bmiHeader
.biBitCount
; /* FIXME */
611 Status
= _SEH_GetExceptionCode();
615 if (NT_SUCCESS(Status
))
617 Result
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
623 ULONG SourcePaletteType
= 0;
624 ULONG DestPaletteType
;
630 ProbeForRead(Info
, sizeof(BITMAPINFO
), 1);
632 if (Info
->bmiHeader
.biBitCount
== BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
))
634 hDestPalette
= hSourcePalette
;
635 bPaletteMatch
= TRUE
;
638 hDestPalette
= BuildDIBPalette(Info
, (PINT
)&DestPaletteType
); //hDestPalette = Dc->DevInfo->hpalDefault;
640 SourcePalette
= PALETTE_LockPalette(hSourcePalette
);
641 /* FIXME - SourcePalette can be NULL!!! Don't assert here! */
642 ASSERT(SourcePalette
);
643 SourcePaletteType
= SourcePalette
->Mode
;
644 PALETTE_UnlockPalette(SourcePalette
);
648 DestPalette
= PALETTE_LockPalette(hDestPalette
);
649 /* FIXME - DestPalette can be NULL!!!! Don't assert here!!! */
650 DPRINT("DestPalette : %p\n", DestPalette
);
652 DestPaletteType
= DestPalette
->Mode
;
656 DestPalette
= SourcePalette
;
660 /* FIXME: This is largely incomplete. */
661 if (Info
->bmiHeader
.biBitCount
<= 8)
663 if (Usage
== DIB_RGB_COLORS
)
666 Index
< (1 << Info
->bmiHeader
.biBitCount
) && Index
< DestPalette
->NumColors
;
669 Info
->bmiColors
[Index
].rgbRed
= DestPalette
->IndexedColors
[Index
].peRed
;
670 Info
->bmiColors
[Index
].rgbGreen
= DestPalette
->IndexedColors
[Index
].peGreen
;
671 Info
->bmiColors
[Index
].rgbBlue
= DestPalette
->IndexedColors
[Index
].peBlue
;
675 if (Usage
== DIB_PAL_COLORS
)
677 DPRINT1("GetDIBits with DIB_PAL_COLORS isn't implemented yet\n");
682 PALETTE_UnlockPalette(DestPalette
);
684 /* Create the destination bitmap to for the copy operation */
685 if (StartScan
> BitmapObj
->SurfObj
.sizlBitmap
.cy
)
687 _SEH_YIELD(goto cleanup
);
691 ScanLines
= min(ScanLines
, BitmapObj
->SurfObj
.sizlBitmap
.cy
- StartScan
);
692 DestSize
.cx
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
693 DestSize
.cy
= ScanLines
;
697 ProbeForWrite(Bits
, sizeof(BitmapObj
->SurfObj
.cjBits
), 1);
699 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
701 BITMAPCOREHEADER
* coreheader
= (BITMAPCOREHEADER
*) Info
;
703 hDestBitmap
= EngCreateBitmap(DestSize
,
704 DIB_GetDIBWidthBytes(DestSize
.cx
, coreheader
->bcBitCount
),
705 BitmapFormat(coreheader
->bcBitCount
, BI_RGB
),
706 0 < coreheader
->bcHeight
? 0 : BMF_TOPDOWN
,
710 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
712 hDestBitmap
= EngCreateBitmap(DestSize
,
713 DIB_GetDIBWidthBytes(DestSize
.cx
, Info
->bmiHeader
.biBitCount
),
714 BitmapFormat(Info
->bmiHeader
.biBitCount
, Info
->bmiHeader
.biCompression
),
715 0 < Info
->bmiHeader
.biHeight
? 0 : BMF_TOPDOWN
,
719 if (hDestBitmap
== NULL
)
720 _SEH_YIELD(goto cleanup
);
725 Status
= _SEH_GetExceptionCode();
729 if (NT_SUCCESS(Status
))
732 SURFOBJ
*DestSurfObj
;
735 XlateObj
= IntEngCreateXlate(DestPaletteType
,
741 SourcePoint
.y
= BitmapObj
->SurfObj
.sizlBitmap
.cy
- (StartScan
+ ScanLines
);
743 /* Determine destination rectangle */
746 DestRect
.right
= DestSize
.cx
;
747 DestRect
.bottom
= DestSize
.cy
;
749 DestSurfObj
= EngLockSurface((HSURF
)hDestBitmap
);
751 if (EngCopyBits(DestSurfObj
,
761 EngDeleteXlate(XlateObj
);
762 EngUnlockSurface(DestSurfObj
);
767 if (hDestBitmap
!= NULL
)
768 EngDeleteSurface((HSURF
)hDestBitmap
);
770 if (hDestPalette
!= NULL
&& bPaletteMatch
== FALSE
)
771 PALETTE_FreePaletteByHandle(hDestPalette
);
773 BITMAPOBJ_UnlockBitmap(BitmapObj
);
775 DPRINT("leaving NtGdiGetDIBitsInternal\n");
782 NtGdiStretchDIBitsInternal(
793 LPBITMAPINFO BitsInfo
,
800 HBITMAP hBitmap
, hOldBitmap
= NULL
;
802 HPALETTE hPal
= NULL
;
805 if (!Bits
|| !BitsInfo
)
807 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
811 hdcMem
= NtGdiCreateCompatibleDC(hDC
);
814 DPRINT1("NtGdiCreateCompatibleDC fail create hdc\n");
818 hBitmap
= NtGdiCreateCompatibleBitmap(hDC
, BitsInfo
->bmiHeader
.biWidth
,
819 BitsInfo
->bmiHeader
.biHeight
);
822 DPRINT1("NtGdiCreateCompatibleBitmap fail create bitmap\n");
823 DPRINT1("hDC : 0x%08x \n", hDC
);
824 DPRINT1("BitsInfo->bmiHeader.biWidth : 0x%08x \n", BitsInfo
->bmiHeader
.biWidth
);
825 DPRINT1("BitsInfo->bmiHeader.biHeight : 0x%08x \n", BitsInfo
->bmiHeader
.biHeight
);
829 /* Select the bitmap into hdcMem, and save a handle to the old bitmap */
830 hOldBitmap
= NtGdiSelectBitmap(hdcMem
, hBitmap
);
832 if(Usage
== DIB_PAL_COLORS
)
834 hPal
= NtGdiGetDCObject(hDC
, GDI_OBJECT_TYPE_PALETTE
);
835 hPal
= GdiSelectPalette(hdcMem
, hPal
, FALSE
);
838 if (BitsInfo
->bmiHeader
.biCompression
== BI_RLE4
||
839 BitsInfo
->bmiHeader
.biCompression
== BI_RLE8
)
841 /* copy existing bitmap from destination dc */
842 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
843 NtGdiBitBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
844 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, ROP
, 0, 0);
846 NtGdiStretchBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
847 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
851 pDC
= DC_LockDc(hdcMem
);
854 /* Note BitsInfo->bmiHeader.biHeight is the number of scanline,
855 * if it negitve we getting to many scanline for scanline is UINT not
856 * a INT, so we need make the negtive value to positve and that make the
857 * count correct for negtive bitmap, TODO : we need testcase for this api */
859 IntSetDIBits(pDC
, hBitmap
, 0, abs(BitsInfo
->bmiHeader
.biHeight
), Bits
,
866 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
867 left (negative biHeight) */
868 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
869 NtGdiBitBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
870 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
873 NtGdiStretchBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
874 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
875 SrcWidth
, SrcHeight
, ROP
, 0);
879 GdiSelectPalette(hdcMem
, hPal
, FALSE
);
882 NtGdiSelectBitmap(hdcMem
, hOldBitmap
);
884 NtGdiDeleteObjectApp(hdcMem
);
886 NtGdiDeleteObject(hBitmap
);
894 IntCreateDIBitmap(PDC Dc
,
906 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
907 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
909 if (bpp
!= 1) fColor
= TRUE
;
910 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
913 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
915 const RGBQUAD
*rgb
= data
->bmiColors
;
916 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
918 // Check if the first color of the colormap is black
919 if ((col
== RGB(0, 0, 0)))
922 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
924 // If the second color is white, create a monochrome bitmap
925 fColor
= (col
!= RGB(0xff,0xff,0xff));
929 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
931 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
932 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
934 if ((col
== RGB(0,0,0)))
937 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
938 fColor
= (col
!= RGB(0xff,0xff,0xff));
944 DPRINT("(%ld): wrong size for data\n", data
->bmiHeader
.biSize
);
949 // Now create the bitmap
952 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
956 handle
= IntGdiCreateBitmap(width
,
966 if (NULL
!= handle
&& CBM_INIT
== init
)
968 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
974 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
975 // The DDB that is created will be whatever bit depth your reference DC is
978 NtGdiCreateDIBitmapInternal(IN HDC hDc
,
982 IN OPTIONAL LPBYTE pjInit
,
983 IN OPTIONAL LPBITMAPINFO pbmi
,
985 IN UINT cjMaxInitInfo
,
995 hDc
= IntGdiCreateDC(NULL
, NULL
, NULL
, NULL
,FALSE
);
998 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1002 Dc
= DC_LockDc(hDc
);
1005 NtGdiDeleteObjectApp(hDc
);
1006 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1011 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, cjMaxInitInfo
, fInit
, pjInit
, pbmi
, iUsage
);
1014 NtGdiDeleteObjectApp(hDc
);
1018 Dc
= DC_LockDc(hDc
);
1021 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1025 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, cjMaxInitInfo
, fInit
, pjInit
, pbmi
, iUsage
);
1032 HBITMAP STDCALL
NtGdiCreateDIBSection(HDC hDC
,
1033 IN OPTIONAL HANDLE hSection
,
1035 IN LPBITMAPINFO bmi
,
1039 IN ULONG_PTR dwColorSpace
,
1042 HBITMAP hbitmap
= 0;
1044 BOOL bDesktopDC
= FALSE
;
1046 if (!bmi
) return hbitmap
; // Make sure.
1048 // If the reference hdc is null, take the desktop dc
1051 hDC
= NtGdiCreateCompatibleDC(0);
1055 if ((dc
= DC_LockDc(hDC
)))
1057 hbitmap
= DIB_CreateDIBSection ( dc
, (BITMAPINFO
*)bmi
, Usage
, Bits
,
1058 hSection
, dwOffset
, 0);
1063 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1067 NtGdiDeleteObjectApp(hDC
);
1073 DIB_CreateDIBSection(
1074 PDC dc
, BITMAPINFO
*bmi
, UINT usage
,
1075 LPVOID
*bits
, HANDLE section
,
1076 DWORD offset
, DWORD ovr_pitch
)
1079 BITMAPOBJ
*bmp
= NULL
;
1080 DIBSECTION
*dib
= NULL
;
1082 // Fill BITMAP32 structure with DIB data
1083 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1091 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1092 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1093 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1095 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1097 bm
.bmWidth
= bi
->biWidth
;
1098 bm
.bmHeight
= effHeight
;
1099 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
1101 bm
.bmPlanes
= bi
->biPlanes
;
1102 bm
.bmBitsPixel
= bi
->biBitCount
;
1105 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1106 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1107 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
1108 ? bi
->biSizeImage
: (ULONG
) (bm
.bmWidthBytes
* effHeight
);
1112 /* bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
1113 0L, offset, totalSize); */
1114 DbgPrint("DIB_CreateDIBSection: Cannot yet handle section DIBs\n");
1115 SetLastWin32Error(ERROR_CALL_NOT_IMPLEMENTED
);
1118 else if (ovr_pitch
&& offset
)
1119 bm
.bmBits
= (LPVOID
) offset
;
1122 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1125 if(usage
== DIB_PAL_COLORS
)
1126 lpRGB
= DIB_MapPaletteColors(dc
, bmi
);
1128 lpRGB
= bmi
->bmiColors
;
1130 // Allocate Memory for DIB and fill structure
1133 dib
= ExAllocatePoolWithTag(PagedPool
, sizeof(DIBSECTION
), TAG_DIB
);
1134 if (dib
!= NULL
) RtlZeroMemory(dib
, sizeof(DIBSECTION
));
1141 dib
->dsBmih
.biSizeImage
= totalSize
;
1143 /* Set dsBitfields values */
1144 if ( usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
1146 dib
->dsBitfields
[0] = dib
->dsBitfields
[1] = dib
->dsBitfields
[2] = 0;
1148 else switch(bi
->biBitCount
)
1151 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)lpRGB
: 0x7c00;
1152 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 1) : 0x03e0;
1153 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 2) : 0x001f;
1157 dib
->dsBitfields
[0] = 0xff0000;
1158 dib
->dsBitfields
[1] = 0x00ff00;
1159 dib
->dsBitfields
[2] = 0x0000ff;
1163 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)lpRGB
: 0xff0000;
1164 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 1) : 0x00ff00;
1165 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 2) : 0x0000ff;
1168 dib
->dshSection
= section
;
1169 dib
->dsOffset
= offset
;
1171 // Create Device Dependent Bitmap and add DIB pointer
1172 Size
.cx
= bm
.bmWidth
;
1173 Size
.cy
= abs(bm
.bmHeight
);
1174 res
= IntCreateBitmap(Size
, bm
.bmWidthBytes
,
1175 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1176 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1177 (bi
->biHeight
< 0 ? BMF_TOPDOWN
: 0),
1181 if (lpRGB
!= bmi
->bmiColors
)
1185 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
1188 bmp
= BITMAPOBJ_LockBitmap(res
);
1191 if (lpRGB
!= bmi
->bmiColors
)
1195 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1196 NtGdiDeleteObject(bmp
);
1199 bmp
->dib
= (DIBSECTION
*) dib
;
1200 bmp
->flFlags
= BITMAPOBJ_IS_APIBITMAP
;
1202 /* WINE NOTE: WINE makes use of a colormap, which is a color translation table between the DIB and the X physical
1203 device. Obviously, this is left out of the ReactOS implementation. Instead, we call
1204 NtGdiSetDIBColorTable. */
1205 if(bi
->biBitCount
== 1) { Entries
= 2; } else
1206 if(bi
->biBitCount
== 4) { Entries
= 16; } else
1207 if(bi
->biBitCount
== 8) { Entries
= 256; }
1210 bmp
->hDIBPalette
= PALETTE_AllocPaletteIndexedRGB(Entries
, lpRGB
);
1212 bmp
->hDIBPalette
= PALETTE_AllocPalette(PAL_BITFIELDS
, 0, NULL
,
1213 dib
->dsBitfields
[0],
1214 dib
->dsBitfields
[1],
1215 dib
->dsBitfields
[2]);
1218 // Clean up in case of errors
1219 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
)
1221 DPRINT("got an error res=%08x, bmp=%p, dib=%p, bm.bmBits=%p\n", res
, bmp
, dib
, bm
.bmBits
);
1225 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
1227 VirtualFree(bm.bmBits, 0L, MEM_RELEASE), bm.bmBits = NULL;
1230 if (dib
) { ExFreePool(dib
); dib
= NULL
; }
1231 if (bmp
) { bmp
= NULL
; }
1232 if (res
) { BITMAPOBJ_FreeBitmapByHandle(res
); res
= 0; }
1235 if (lpRGB
!= bmi
->bmiColors
)
1242 BITMAPOBJ_UnlockBitmap(bmp
);
1245 // Return BITMAP handle and storage location
1246 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1254 /***********************************************************************
1255 * DIB_GetDIBWidthBytes
1257 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1258 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
1259 * 11/16/1999 (RJJ) lifted from wine
1261 INT FASTCALL
DIB_GetDIBWidthBytes (INT width
, INT depth
)
1263 return ((width
* depth
+ 31) & ~31) >> 3;
1266 /***********************************************************************
1267 * DIB_GetDIBImageBytes
1269 * Return the number of bytes used to hold the image in a DIB bitmap.
1270 * 11/16/1999 (RJJ) lifted from wine
1273 INT STDCALL
DIB_GetDIBImageBytes (INT width
, INT height
, INT depth
)
1275 return DIB_GetDIBWidthBytes( width
, depth
) * (height
< 0 ? -height
: height
);
1278 /***********************************************************************
1279 * DIB_BitmapInfoSize
1281 * Return the size of the bitmap info structure including color table.
1282 * 11/16/1999 (RJJ) lifted from wine
1285 INT FASTCALL
DIB_BitmapInfoSize (const BITMAPINFO
* info
, WORD coloruse
)
1289 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1291 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
1292 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1293 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1295 else /* assume BITMAPINFOHEADER */
1297 colors
= info
->bmiHeader
.biClrUsed
;
1298 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
1299 return sizeof(BITMAPINFOHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1304 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1311 palGDI
= PALETTE_LockPalette(dc
->DcLevel
.hpal
);
1318 if (palGDI
->Mode
!= PAL_INDEXED
)
1320 PALETTE_UnlockPalette(palGDI
);
1324 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1325 if (lpbmi
->bmiHeader
.biClrUsed
)
1327 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1330 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1333 PALETTE_UnlockPalette(palGDI
);
1337 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1339 for (i
= 0; i
< nNumColors
; i
++)
1341 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1342 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1343 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1346 PALETTE_UnlockPalette(palGDI
);
1352 BuildDIBPalette (CONST BITMAPINFO
*bmi
, PINT paletteType
)
1356 PALETTEENTRY
*palEntries
= NULL
;
1358 ULONG RedMask
, GreenMask
, BlueMask
;
1360 // Determine Bits Per Pixel
1361 bits
= bmi
->bmiHeader
.biBitCount
;
1363 // Determine paletteType from Bits Per Pixel
1366 *paletteType
= PAL_INDEXED
;
1367 RedMask
= GreenMask
= BlueMask
= 0;
1369 else if(bmi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
1371 *paletteType
= PAL_BITFIELDS
;
1372 RedMask
= ((ULONG
*)bmi
->bmiColors
)[0];
1373 GreenMask
= ((ULONG
*)bmi
->bmiColors
)[1];
1374 BlueMask
= ((ULONG
*)bmi
->bmiColors
)[2];
1378 *paletteType
= PAL_BITFIELDS
;
1385 *paletteType
= PAL_BGR
;
1387 GreenMask
= 0x00ff00;
1388 BlueMask
= 0x0000ff;
1391 if (bmi
->bmiHeader
.biClrUsed
== 0)
1393 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1397 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1400 if (PAL_INDEXED
== *paletteType
)
1402 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)bmi
->bmiColors
);
1406 hPal
= PALETTE_AllocPalette(*paletteType
, ColorCount
,
1407 (ULONG
*) palEntries
,
1408 RedMask
, GreenMask
, BlueMask
);