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
);
66 for (Index
= StartIndex
;
67 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
70 PalGDI
->IndexedColors
[Index
].peRed
= Colors
[Index
- StartIndex
].rgbRed
;
71 PalGDI
->IndexedColors
[Index
].peGreen
= Colors
[Index
- StartIndex
].rgbGreen
;
72 PalGDI
->IndexedColors
[Index
].peBlue
= Colors
[Index
- StartIndex
].rgbBlue
;
74 PALETTE_UnlockPalette(PalGDI
);
79 BITMAPOBJ_UnlockBitmap(BitmapObj
);
86 IntGetDIBColorTable(HDC hDC
, UINT StartIndex
, UINT Entries
, RGBQUAD
*Colors
)
93 if (!(dc
= DC_LockDc(hDC
))) return 0;
94 if (dc
->DC_Type
== DC_TYPE_INFO
)
100 BitmapObj
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
101 if (BitmapObj
== NULL
)
104 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
108 if (BitmapObj
->dib
== NULL
)
110 BITMAPOBJ_UnlockBitmap(BitmapObj
);
112 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
116 if (BitmapObj
->dib
->dsBmih
.biBitCount
<= 8 &&
117 StartIndex
< (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
119 if (StartIndex
+ Entries
> (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
120 Entries
= (1 << BitmapObj
->dib
->dsBmih
.biBitCount
) - StartIndex
;
122 PalGDI
= PALETTE_LockPalette(BitmapObj
->hDIBPalette
);
124 for (Index
= StartIndex
;
125 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
128 Colors
[Index
- StartIndex
].rgbRed
= PalGDI
->IndexedColors
[Index
].peRed
;
129 Colors
[Index
- StartIndex
].rgbGreen
= PalGDI
->IndexedColors
[Index
].peGreen
;
130 Colors
[Index
- StartIndex
].rgbBlue
= PalGDI
->IndexedColors
[Index
].peBlue
;
132 PALETTE_UnlockPalette(PalGDI
);
137 BITMAPOBJ_UnlockBitmap(BitmapObj
);
143 // Converts a DIB to a device-dependent bitmap
151 CONST BITMAPINFO
*bmi
,
155 HBITMAP SourceBitmap
;
158 SURFOBJ
*DestSurf
, *SourceSurf
;
165 HPALETTE DDB_Palette
, DIB_Palette
;
166 ULONG DDB_Palette_Type
, DIB_Palette_Type
;
170 if (!(bitmap
= BITMAPOBJ_LockBitmap(hBitmap
)))
176 //if (ColorUse == DIB_PAL_COLORS)
177 // lpRGB = DIB_MapPaletteColors(hDC, bmi);
179 // lpRGB = &bmi->bmiColors;
181 DestSurf
= &bitmap
->SurfObj
;
183 // Create source surface
184 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
185 SourceSize
.cy
= ScanLines
;
187 // Determine width of DIB
188 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
);
190 SourceBitmap
= EngCreateBitmap(SourceSize
,
192 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
193 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
195 if (0 == SourceBitmap
)
197 BITMAPOBJ_UnlockBitmap(bitmap
);
198 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
202 SourceSurf
= EngLockSurface((HSURF
)SourceBitmap
);
203 if (NULL
== SourceSurf
)
205 EngDeleteSurface((HSURF
)SourceBitmap
);
206 BITMAPOBJ_UnlockBitmap(bitmap
);
207 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
211 // Destination palette obtained from the hDC
212 hDCPalette
= PALETTE_LockPalette(((GDIDEVICE
*)DC
->pPDev
)->DevInfo
.hpalDefault
);
213 if (NULL
== hDCPalette
)
215 EngUnlockSurface(SourceSurf
);
216 EngDeleteSurface((HSURF
)SourceBitmap
);
217 BITMAPOBJ_UnlockBitmap(bitmap
);
218 SetLastWin32Error(ERROR_INVALID_HANDLE
);
221 DDB_Palette_Type
= hDCPalette
->Mode
;
222 DDB_Palette
= ((GDIDEVICE
*)DC
->pPDev
)->DevInfo
.hpalDefault
;
223 PALETTE_UnlockPalette(hDCPalette
);
225 // Source palette obtained from the BITMAPINFO
226 DIB_Palette
= BuildDIBPalette ( (PBITMAPINFO
)bmi
, (PINT
)&DIB_Palette_Type
);
227 if (NULL
== DIB_Palette
)
229 EngUnlockSurface(SourceSurf
);
230 EngDeleteSurface((HSURF
)SourceBitmap
);
231 BITMAPOBJ_UnlockBitmap(bitmap
);
232 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
236 // Determine XLATEOBJ for color translation
237 XlateObj
= IntEngCreateXlate(DDB_Palette_Type
, DIB_Palette_Type
, DDB_Palette
, DIB_Palette
);
238 if (NULL
== XlateObj
)
240 PALETTE_FreePaletteByHandle(DIB_Palette
);
241 EngUnlockSurface(SourceSurf
);
242 EngDeleteSurface((HSURF
)SourceBitmap
);
243 BITMAPOBJ_UnlockBitmap(bitmap
);
244 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
252 // Determine destination rectangle
254 DestRect
.top
= abs(bmi
->bmiHeader
.biHeight
) - StartScan
- ScanLines
;
255 DestRect
.right
= SourceSize
.cx
;
256 DestRect
.bottom
= DestRect
.top
+ ScanLines
;
258 copyBitsResult
= EngCopyBits(DestSurf
, SourceSurf
, NULL
, XlateObj
, &DestRect
, &ZeroPoint
);
260 // If it succeeded, return number of scanlines copies
261 if(copyBitsResult
== TRUE
)
263 result
= SourceSize
.cy
- 1;
267 EngDeleteXlate(XlateObj
);
268 PALETTE_FreePaletteByHandle(DIB_Palette
);
269 EngUnlockSurface(SourceSurf
);
270 EngDeleteSurface((HSURF
)SourceBitmap
);
272 // if (ColorUse == DIB_PAL_COLORS)
273 // WinFree((LPSTR)lpRGB);
275 BITMAPOBJ_UnlockBitmap(bitmap
);
280 // Converts a DIB to a device-dependent bitmap
288 CONST BITMAPINFO
*bmi
,
297 SetLastWin32Error(ERROR_INVALID_HANDLE
);
300 if (Dc
->DC_Type
== DC_TYPE_INFO
)
306 Ret
= IntSetDIBits(Dc
, hBitmap
, StartScan
, ScanLines
, Bits
, bmi
, ColorUse
);
318 NtGdiSetDIBitsToDeviceInternal(
333 IN BOOL bTransformCoordinates
,
334 IN OPTIONAL HANDLE hcmXform
338 NTSTATUS Status
= STATUS_SUCCESS
;
340 HBITMAP hSourceBitmap
= NULL
;
341 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
;
346 XLATEOBJ
*XlateObj
= NULL
;
348 HPALETTE DDBPalette
, DIBPalette
= NULL
;
349 ULONG DDBPaletteType
, DIBPaletteType
;
353 pDC
= DC_LockDc(hDC
);
356 SetLastWin32Error(ERROR_INVALID_HANDLE
);
359 if (pDC
->DC_Type
== DC_TYPE_INFO
)
365 pDestSurf
= EngLockSurface((HSURF
)pDC
->w
.hBitmap
);
368 /* FIXME: SetLastError ? */
375 rcDest
.right
= XDest
+ Width
;
376 rcDest
.bottom
= YDest
+ Height
;
380 /* Enter SEH, as the bits are user mode */
383 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
384 SourceSize
.cy
= ScanLines
;
385 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
);
387 ProbeForRead(Bits
, DIBWidth
* abs(bmi
->bmiHeader
.biHeight
), 1);
388 hSourceBitmap
= EngCreateBitmap(SourceSize
,
390 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
391 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
395 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
396 Status
= STATUS_NO_MEMORY
;
400 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
403 Status
= STATUS_UNSUCCESSFUL
;
408 /* Obtain destination palette from the DC */
409 pDCPalette
= PALETTE_LockPalette(((GDIDEVICE
*)pDC
->pPDev
)->DevInfo
.hpalDefault
);
412 SetLastWin32Error(ERROR_INVALID_HANDLE
);
413 Status
= STATUS_UNSUCCESSFUL
;
416 DDBPaletteType
= pDCPalette
->Mode
;
417 DDBPalette
= ((GDIDEVICE
*)pDC
->pPDev
)->DevInfo
.hpalDefault
;
418 PALETTE_UnlockPalette(pDCPalette
);
420 DIBPalette
= BuildDIBPalette(bmi
, (PINT
)&DIBPaletteType
);
423 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
424 Status
= STATUS_NO_MEMORY
;
428 /* Determine XlateObj */
429 XlateObj
= IntEngCreateXlate(DDBPaletteType
, DIBPaletteType
, DDBPalette
, DIBPalette
);
432 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
433 Status
= STATUS_NO_MEMORY
;
438 Status
= IntEngBitBlt(pDestSurf
,
448 ROP3_TO_ROP4(SRCCOPY
));
453 Status
= _SEH_GetExceptionCode();
457 if (NT_SUCCESS(Status
))
459 /* FIXME: Should probably be only the number of lines actually copied */
463 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
464 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
465 if (XlateObj
) EngDeleteXlate(XlateObj
);
466 if (DIBPalette
) PALETTE_FreePaletteByHandle(DIBPalette
);
467 EngUnlockSurface(pDestSurf
);
474 /* Converts a device-dependent bitmap to a DIB */
476 NtGdiGetDIBitsInternal(HDC hDC
,
487 BITMAPOBJ
*BitmapObj
= NULL
;
488 HBITMAP hDestBitmap
= NULL
;
489 HPALETTE hSourcePalette
= NULL
;
490 HPALETTE hDestPalette
= NULL
;
491 PPALGDI SourcePalette
= NULL
;
492 PPALGDI DestPalette
= NULL
;
493 NTSTATUS Status
= STATUS_SUCCESS
;
495 BOOL bPaletteMatch
= FALSE
;
497 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
499 /* Get handle for the palette in DC. */
501 if (Dc
== NULL
) return 0;
502 if (Dc
->DC_Type
== DC_TYPE_INFO
)
507 /* Source palette obtained from the windows hdc */
508 hSourcePalette
= Dc
->DcLevel
.hpal
;
511 /* don't do anything if we fail this */
512 if (Usage
!= DIB_RGB_COLORS
&& Usage
!= DIB_PAL_COLORS
)
515 /* Get a pointer to the source bitmap object */
516 BitmapObj
= BITMAPOBJ_LockBitmap(hBitmap
);
517 if (BitmapObj
== NULL
)
520 /* fill out the BITMAPINFO struct */
525 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
527 BITMAPCOREHEADER
* coreheader
;
529 ProbeForWrite(Info
, sizeof(BITMAPINFO
), 1);
531 coreheader
= (BITMAPCOREHEADER
*) Info
;
532 coreheader
->bcWidth
=BitmapObj
->SurfObj
.sizlBitmap
.cx
;
533 coreheader
->bcPlanes
= 1;
534 coreheader
->bcBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
535 coreheader
->bcHeight
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
537 if (BitmapObj
->SurfObj
.lDelta
> 0)
538 coreheader
->bcHeight
= -coreheader
->bcHeight
;
541 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
543 ProbeForWrite(Info
, sizeof(BITMAPINFO
), 1);
545 Info
->bmiHeader
.biWidth
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
546 Info
->bmiHeader
.biHeight
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
547 /* Report negtive height for top-down bitmaps. */
548 if (BitmapObj
->SurfObj
.lDelta
> 0)
549 Info
->bmiHeader
.biHeight
= -Info
->bmiHeader
.biHeight
;
550 Info
->bmiHeader
.biPlanes
= 1;
551 Info
->bmiHeader
.biBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
552 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
554 switch (BitmapObj
->SurfObj
.iBitmapFormat
)
562 Info
->bmiHeader
.biCompression
= BI_RGB
;
565 Info
->bmiHeader
.biCompression
= BI_RLE4
;
568 Info
->bmiHeader
.biCompression
= BI_RLE8
;
571 Info
->bmiHeader
.biCompression
= BI_JPEG
;
574 Info
->bmiHeader
.biCompression
= BI_PNG
;
578 Info
->bmiHeader
.biSizeImage
= BitmapObj
->SurfObj
.cjBits
;
579 Info
->bmiHeader
.biXPelsPerMeter
= 0; /* FIXME */
580 Info
->bmiHeader
.biYPelsPerMeter
= 0; /* FIXME */
581 Info
->bmiHeader
.biClrUsed
= 0;
582 Info
->bmiHeader
.biClrImportant
= 1 << Info
->bmiHeader
.biBitCount
; /* FIXME */
588 Status
= _SEH_GetExceptionCode();
592 if (NT_SUCCESS(Status
))
594 Result
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
600 ULONG SourcePaletteType
= 0;
601 ULONG DestPaletteType
;
607 ProbeForRead(Info
, sizeof(BITMAPINFO
), 1);
609 if (Info
->bmiHeader
.biBitCount
== BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
))
611 hDestPalette
= hSourcePalette
;
612 bPaletteMatch
= TRUE
;
615 hDestPalette
= BuildDIBPalette(Info
, (PINT
)&DestPaletteType
); //hDestPalette = Dc->DevInfo->hpalDefault;
617 SourcePalette
= PALETTE_LockPalette(hSourcePalette
);
618 /* FIXME - SourcePalette can be NULL!!! Don't assert here! */
619 ASSERT(SourcePalette
);
620 SourcePaletteType
= SourcePalette
->Mode
;
621 PALETTE_UnlockPalette(SourcePalette
);
625 DestPalette
= PALETTE_LockPalette(hDestPalette
);
626 /* FIXME - DestPalette can be NULL!!!! Don't assert here!!! */
627 DPRINT("DestPalette : %p\n", DestPalette
);
629 DestPaletteType
= DestPalette
->Mode
;
633 DestPalette
= SourcePalette
;
637 /* FIXME: This is largely incomplete. */
638 if (Info
->bmiHeader
.biBitCount
<= 8)
640 if (Usage
== DIB_RGB_COLORS
)
643 Index
< (1 << Info
->bmiHeader
.biBitCount
) && Index
< DestPalette
->NumColors
;
646 Info
->bmiColors
[Index
].rgbRed
= DestPalette
->IndexedColors
[Index
].peRed
;
647 Info
->bmiColors
[Index
].rgbGreen
= DestPalette
->IndexedColors
[Index
].peGreen
;
648 Info
->bmiColors
[Index
].rgbBlue
= DestPalette
->IndexedColors
[Index
].peBlue
;
652 if (Usage
== DIB_PAL_COLORS
)
654 DPRINT1("GetDIBits with DIB_PAL_COLORS isn't implemented yet\n");
659 PALETTE_UnlockPalette(DestPalette
);
661 /* Create the destination bitmap to for the copy operation */
662 if (StartScan
> BitmapObj
->SurfObj
.sizlBitmap
.cy
)
664 _SEH_YIELD(goto cleanup
);
669 ScanLines
= min(ScanLines
, BitmapObj
->SurfObj
.sizlBitmap
.cy
- StartScan
);
671 DestSize
.cx
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
672 DestSize
.cy
= ScanLines
;
676 ProbeForWrite(Bits
, sizeof(BitmapObj
->SurfObj
.cjBits
), 1);
678 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
680 BITMAPCOREHEADER
* coreheader
= (BITMAPCOREHEADER
*) Info
;
682 hDestBitmap
= EngCreateBitmap(DestSize
,
683 DIB_GetDIBWidthBytes(DestSize
.cx
, coreheader
->bcBitCount
),
684 BitmapFormat(coreheader
->bcBitCount
, BI_RGB
),
685 0 < coreheader
->bcHeight
? 0 : BMF_TOPDOWN
,
689 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
692 hDestBitmap
= EngCreateBitmap(DestSize
,
693 DIB_GetDIBWidthBytes(DestSize
.cx
, Info
->bmiHeader
.biBitCount
),
694 BitmapFormat(Info
->bmiHeader
.biBitCount
, Info
->bmiHeader
.biCompression
),
695 0 < Info
->bmiHeader
.biHeight
? 0 : BMF_TOPDOWN
,
699 if (hDestBitmap
== NULL
)
700 _SEH_YIELD(goto cleanup
);
705 Status
= _SEH_GetExceptionCode();
709 if (NT_SUCCESS(Status
))
712 SURFOBJ
*DestSurfObj
;
715 XlateObj
= IntEngCreateXlate(DestPaletteType
,
721 SourcePoint
.y
= BitmapObj
->SurfObj
.sizlBitmap
.cy
- (StartScan
+ ScanLines
);
723 /* Determine destination rectangle */
726 DestRect
.right
= DestSize
.cx
;
727 DestRect
.bottom
= DestSize
.cy
;
729 DestSurfObj
= EngLockSurface((HSURF
)hDestBitmap
);
731 if (EngCopyBits(DestSurfObj
,
741 EngDeleteXlate(XlateObj
);
742 EngUnlockSurface(DestSurfObj
);
747 if (hDestBitmap
!= NULL
)
748 EngDeleteSurface((HSURF
)hDestBitmap
);
750 if (hDestPalette
!= NULL
&& bPaletteMatch
== FALSE
)
751 PALETTE_FreePaletteByHandle(hDestPalette
);
753 BITMAPOBJ_UnlockBitmap(BitmapObj
);
755 DPRINT("leaving NtGdiGetDIBitsInternal\n");
762 NtGdiStretchDIBitsInternal(
773 LPBITMAPINFO BitsInfo
,
780 HBITMAP hBitmap
, hOldBitmap
;
782 HPALETTE hPal
= NULL
;
785 if (!Bits
|| !BitsInfo
)
787 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
791 hdcMem
= NtGdiCreateCompatibleDC(hDC
);
792 hBitmap
= NtGdiCreateCompatibleBitmap(hDC
, BitsInfo
->bmiHeader
.biWidth
,
793 BitsInfo
->bmiHeader
.biHeight
);
794 hOldBitmap
= NtGdiSelectBitmap(hdcMem
, hBitmap
);
796 if(Usage
== DIB_PAL_COLORS
)
798 hPal
= NtGdiGetDCObject(hDC
, GDI_OBJECT_TYPE_PALETTE
);
799 hPal
= NtUserSelectPalette(hdcMem
, hPal
, FALSE
);
802 if (BitsInfo
->bmiHeader
.biCompression
== BI_RLE4
||
803 BitsInfo
->bmiHeader
.biCompression
== BI_RLE8
)
805 /* copy existing bitmap from destination dc */
806 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
807 NtGdiBitBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
808 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, ROP
, 0, 0);
810 NtGdiStretchBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
811 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
815 pDC
= DC_LockDc(hdcMem
);
817 /* Note BitsInfo->bmiHeader.biHeight is the number of scanline,
818 * if it negitve we getting to many scanline for scanline is UINT not
819 * a INT, so we need make the negtive value to positve and that make the
820 * count correct for negtive bitmap, TODO : we need testcase for this api */
822 IntSetDIBits(pDC
, hBitmap
, 0, abs(BitsInfo
->bmiHeader
.biHeight
), Bits
,
828 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
829 left (negative biHeight) */
830 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
831 NtGdiBitBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
832 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
835 NtGdiStretchBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
836 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
837 SrcWidth
, SrcHeight
, ROP
, 0);
840 NtUserSelectPalette(hdcMem
, hPal
, FALSE
);
842 NtGdiSelectBitmap(hdcMem
, hOldBitmap
);
843 NtGdiDeleteObjectApp(hdcMem
);
844 NtGdiDeleteObject(hBitmap
);
852 IntCreateDIBitmap(PDC Dc
,
864 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
865 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
867 if (bpp
!= 1) fColor
= TRUE
;
868 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
871 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
873 const RGBQUAD
*rgb
= data
->bmiColors
;
874 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
876 // Check if the first color of the colormap is black
877 if ((col
== RGB(0, 0, 0)))
880 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
882 // If the second color is white, create a monochrome bitmap
883 fColor
= (col
!= RGB(0xff,0xff,0xff));
887 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
889 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
890 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
892 if ((col
== RGB(0,0,0)))
895 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
896 fColor
= (col
!= RGB(0xff,0xff,0xff));
902 DPRINT("(%ld): wrong size for data\n", data
->bmiHeader
.biSize
);
907 // Now create the bitmap
910 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
914 handle
= IntGdiCreateBitmap(width
,
924 if (NULL
!= handle
&& CBM_INIT
== init
)
926 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
932 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
933 // The DDB that is created will be whatever bit depth your reference DC is
936 NtGdiCreateDIBitmapInternal(IN HDC hDc
,
940 IN OPTIONAL LPBYTE pjInit
,
941 IN OPTIONAL LPBITMAPINFO pbmi
,
943 IN UINT cjMaxInitInfo
,
953 hDc
= IntGdiCreateDC(NULL
, NULL
, NULL
, NULL
,FALSE
);
956 SetLastWin32Error(ERROR_INVALID_HANDLE
);
963 NtGdiDeleteObjectApp(hDc
);
964 SetLastWin32Error(ERROR_INVALID_HANDLE
);
969 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, cjMaxInitInfo
, fInit
, pjInit
, pbmi
, iUsage
);
972 NtGdiDeleteObjectApp(hDc
);
979 SetLastWin32Error(ERROR_INVALID_HANDLE
);
983 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, cjMaxInitInfo
, fInit
, pjInit
, pbmi
, iUsage
);
990 HBITMAP STDCALL
NtGdiCreateDIBSection(HDC hDC
,
991 IN OPTIONAL HANDLE hSection
,
997 IN ULONG_PTR dwColorSpace
,
1000 HBITMAP hbitmap
= 0;
1002 BOOL bDesktopDC
= FALSE
;
1004 if (!bmi
) return hbitmap
; // Make sure.
1006 // If the reference hdc is null, take the desktop dc
1009 hDC
= NtGdiCreateCompatibleDC(0);
1013 if ((dc
= DC_LockDc(hDC
)))
1015 hbitmap
= DIB_CreateDIBSection ( dc
, (BITMAPINFO
*)bmi
, Usage
, Bits
,
1016 hSection
, dwOffset
, 0);
1021 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1025 NtGdiDeleteObjectApp(hDC
);
1031 DIB_CreateDIBSection(
1032 PDC dc
, BITMAPINFO
*bmi
, UINT usage
,
1033 LPVOID
*bits
, HANDLE section
,
1034 DWORD offset
, DWORD ovr_pitch
)
1037 BITMAPOBJ
*bmp
= NULL
;
1038 DIBSECTION
*dib
= NULL
;
1040 // Fill BITMAP32 structure with DIB data
1041 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1049 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1050 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1051 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1053 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1055 bm
.bmWidth
= bi
->biWidth
;
1056 bm
.bmHeight
= effHeight
;
1057 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
1059 bm
.bmPlanes
= bi
->biPlanes
;
1060 bm
.bmBitsPixel
= bi
->biBitCount
;
1063 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1064 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1065 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
1066 ? bi
->biSizeImage
: (ULONG
) (bm
.bmWidthBytes
* effHeight
);
1070 /* bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
1071 0L, offset, totalSize); */
1072 DbgPrint("DIB_CreateDIBSection: Cannot yet handle section DIBs\n");
1073 SetLastWin32Error(ERROR_CALL_NOT_IMPLEMENTED
);
1076 else if (ovr_pitch
&& offset
)
1077 bm
.bmBits
= (LPVOID
) offset
;
1080 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1083 if(usage
== DIB_PAL_COLORS
)
1084 lpRGB
= DIB_MapPaletteColors(dc
, bmi
);
1086 lpRGB
= bmi
->bmiColors
;
1088 // Allocate Memory for DIB and fill structure
1091 dib
= ExAllocatePoolWithTag(PagedPool
, sizeof(DIBSECTION
), TAG_DIB
);
1092 if (dib
!= NULL
) RtlZeroMemory(dib
, sizeof(DIBSECTION
));
1099 dib
->dsBmih
.biSizeImage
= totalSize
;
1101 /* Set dsBitfields values */
1102 if ( usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
1104 dib
->dsBitfields
[0] = dib
->dsBitfields
[1] = dib
->dsBitfields
[2] = 0;
1106 else switch(bi
->biBitCount
)
1109 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)lpRGB
: 0x7c00;
1110 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 1) : 0x03e0;
1111 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 2) : 0x001f; break;
1114 dib
->dsBitfields
[0] = 0xff0000;
1115 dib
->dsBitfields
[1] = 0x00ff00;
1116 dib
->dsBitfields
[2] = 0x0000ff;
1120 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)lpRGB
: 0xff0000;
1121 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 1) : 0x00ff00;
1122 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 2) : 0x0000ff;
1125 dib
->dshSection
= section
;
1126 dib
->dsOffset
= offset
;
1129 // Create Device Dependent Bitmap and add DIB pointer
1132 Size
.cx
= bm
.bmWidth
;
1133 Size
.cy
= abs(bm
.bmHeight
);
1134 res
= IntCreateBitmap(Size
, bm
.bmWidthBytes
,
1135 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1136 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1137 (bi
->biHeight
< 0 ? BMF_TOPDOWN
: 0),
1141 if (lpRGB
!= bmi
->bmiColors
)
1145 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
1148 bmp
= BITMAPOBJ_LockBitmap(res
);
1151 if (lpRGB
!= bmi
->bmiColors
)
1155 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1156 NtGdiDeleteObject(bmp
);
1159 bmp
->dib
= (DIBSECTION
*) dib
;
1160 bmp
->flFlags
= BITMAPOBJ_IS_APIBITMAP
;
1162 /* WINE NOTE: WINE makes use of a colormap, which is a color translation table between the DIB and the X physical
1163 device. Obviously, this is left out of the ReactOS implementation. Instead, we call
1164 NtGdiSetDIBColorTable. */
1165 if(bi
->biBitCount
== 1) { Entries
= 2; } else
1166 if(bi
->biBitCount
== 4) { Entries
= 16; } else
1167 if(bi
->biBitCount
== 8) { Entries
= 256; }
1170 bmp
->hDIBPalette
= PALETTE_AllocPaletteIndexedRGB(Entries
, lpRGB
);
1172 bmp
->hDIBPalette
= PALETTE_AllocPalette(PAL_BITFIELDS
, 0, NULL
,
1173 dib
->dsBitfields
[0],
1174 dib
->dsBitfields
[1],
1175 dib
->dsBitfields
[2]);
1178 // Clean up in case of errors
1179 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
)
1181 DPRINT("got an error res=%08x, bmp=%p, dib=%p, bm.bmBits=%p\n", res
, bmp
, dib
, bm
.bmBits
);
1185 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
1187 VirtualFree(bm.bmBits, 0L, MEM_RELEASE), bm.bmBits = NULL;
1190 if (dib
) { ExFreePool(dib
); dib
= NULL
; }
1191 if (bmp
) { bmp
= NULL
; }
1192 if (res
) { BITMAPOBJ_FreeBitmapByHandle(res
); res
= 0; }
1195 if (lpRGB
!= bmi
->bmiColors
)
1202 BITMAPOBJ_UnlockBitmap(bmp
);
1205 // Return BITMAP handle and storage location
1206 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1214 /***********************************************************************
1215 * DIB_GetDIBWidthBytes
1217 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1218 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
1219 * 11/16/1999 (RJJ) lifted from wine
1221 INT FASTCALL
DIB_GetDIBWidthBytes (INT width
, INT depth
)
1223 /* http://www.osronline.com/DDKx/graphics/gdifncs_9pgn.htm say it must be exacly
1224 * number of byte to next scanline in the bitmap
1226 UINT bytes
= ((UINT
)(abs(width
)) * (UINT
)depth
) >> 3;
1227 // FIXME : this is wrong return (width * depth + 31) & ~31) >> 3;
1231 /***********************************************************************
1232 * DIB_GetDIBImageBytes
1234 * Return the number of bytes used to hold the image in a DIB bitmap.
1235 * 11/16/1999 (RJJ) lifted from wine
1238 INT STDCALL
DIB_GetDIBImageBytes (INT width
, INT height
, INT depth
)
1240 return DIB_GetDIBWidthBytes( width
, depth
) * (height
< 0 ? -height
: height
);
1243 /***********************************************************************
1244 * DIB_BitmapInfoSize
1246 * Return the size of the bitmap info structure including color table.
1247 * 11/16/1999 (RJJ) lifted from wine
1250 INT FASTCALL
DIB_BitmapInfoSize (const BITMAPINFO
* info
, WORD coloruse
)
1254 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1256 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
1257 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1258 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1260 else /* assume BITMAPINFOHEADER */
1262 colors
= info
->bmiHeader
.biClrUsed
;
1263 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
1264 return sizeof(BITMAPINFOHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1269 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1276 palGDI
= PALETTE_LockPalette(dc
->DcLevel
.hpal
);
1280 // RELEASEDCINFO(hDC);
1284 if (palGDI
->Mode
!= PAL_INDEXED
)
1286 PALETTE_UnlockPalette(palGDI
);
1290 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1291 if (lpbmi
->bmiHeader
.biClrUsed
)
1293 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1296 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1299 PALETTE_UnlockPalette(palGDI
);
1303 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1305 for (i
= 0; i
< nNumColors
; i
++)
1307 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1308 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1309 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1312 // RELEASEDCINFO(hDC);
1313 PALETTE_UnlockPalette(palGDI
);
1319 BuildDIBPalette (CONST BITMAPINFO
*bmi
, PINT paletteType
)
1323 PALETTEENTRY
*palEntries
= NULL
;
1325 ULONG RedMask
, GreenMask
, BlueMask
;
1327 // Determine Bits Per Pixel
1328 bits
= bmi
->bmiHeader
.biBitCount
;
1330 // Determine paletteType from Bits Per Pixel
1333 *paletteType
= PAL_INDEXED
;
1334 RedMask
= GreenMask
= BlueMask
= 0;
1338 *paletteType
= PAL_BITFIELDS
;
1345 *paletteType
= PAL_BGR
;
1347 GreenMask
= 0x00ff00;
1348 BlueMask
= 0x0000ff;
1351 if (bmi
->bmiHeader
.biClrUsed
== 0)
1353 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1357 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1360 if (PAL_INDEXED
== *paletteType
)
1362 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)bmi
->bmiColors
);
1366 hPal
= PALETTE_AllocPalette(*paletteType
, ColorCount
,
1367 (ULONG
*) palEntries
,
1368 RedMask
, GreenMask
, BlueMask
);