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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 static const RGBQUAD EGAColorsQuads
[16] = {
26 /* rgbBlue, rgbGreen, rgbRed, rgbReserved */
27 { 0x00, 0x00, 0x00, 0x00 },
28 { 0x00, 0x00, 0x80, 0x00 },
29 { 0x00, 0x80, 0x00, 0x00 },
30 { 0x00, 0x80, 0x80, 0x00 },
31 { 0x80, 0x00, 0x00, 0x00 },
32 { 0x80, 0x00, 0x80, 0x00 },
33 { 0x80, 0x80, 0x00, 0x00 },
34 { 0x80, 0x80, 0x80, 0x00 },
35 { 0xc0, 0xc0, 0xc0, 0x00 },
36 { 0x00, 0x00, 0xff, 0x00 },
37 { 0x00, 0xff, 0x00, 0x00 },
38 { 0x00, 0xff, 0xff, 0x00 },
39 { 0xff, 0x00, 0x00, 0x00 },
40 { 0xff, 0x00, 0xff, 0x00 },
41 { 0xff, 0xff, 0x00, 0x00 },
42 { 0xff, 0xff, 0xff, 0x00 }
45 static const RGBQUAD DefLogPaletteQuads
[20] = { /* Copy of Default Logical Palette */
46 /* rgbBlue, rgbGreen, rgbRed, rgbReserved */
47 { 0x00, 0x00, 0x00, 0x00 },
48 { 0x00, 0x00, 0x80, 0x00 },
49 { 0x00, 0x80, 0x00, 0x00 },
50 { 0x00, 0x80, 0x80, 0x00 },
51 { 0x80, 0x00, 0x00, 0x00 },
52 { 0x80, 0x00, 0x80, 0x00 },
53 { 0x80, 0x80, 0x00, 0x00 },
54 { 0xc0, 0xc0, 0xc0, 0x00 },
55 { 0xc0, 0xdc, 0xc0, 0x00 },
56 { 0xf0, 0xca, 0xa6, 0x00 },
57 { 0xf0, 0xfb, 0xff, 0x00 },
58 { 0xa4, 0xa0, 0xa0, 0x00 },
59 { 0x80, 0x80, 0x80, 0x00 },
60 { 0x00, 0x00, 0xf0, 0x00 },
61 { 0x00, 0xff, 0x00, 0x00 },
62 { 0x00, 0xff, 0xff, 0x00 },
63 { 0xff, 0x00, 0x00, 0x00 },
64 { 0xff, 0x00, 0xff, 0x00 },
65 { 0xff, 0xff, 0x00, 0x00 },
66 { 0xff, 0xff, 0xff, 0x00 }
76 CONST RGBQUAD
*Colors
)
84 if (!(dc
= DC_LockDc(hDC
))) return 0;
85 if (dc
->dctype
== DC_TYPE_INFO
)
91 psurf
= dc
->dclevel
.pSurface
;
95 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
99 if (psurf
->hSecure
== NULL
)
102 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
106 biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
107 if (biBitCount
<= 8 && StartIndex
< (1 << biBitCount
))
109 if (StartIndex
+ Entries
> (1 << biBitCount
))
110 Entries
= (1 << biBitCount
) - StartIndex
;
112 if (psurf
->ppal
== NULL
)
115 SetLastWin32Error(ERROR_INVALID_HANDLE
);
119 PalGDI
= PALETTE_LockPalette(psurf
->ppal
->BaseObject
.hHmgr
);
121 for (Index
= StartIndex
;
122 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
125 PalGDI
->IndexedColors
[Index
].peRed
= Colors
[Index
- StartIndex
].rgbRed
;
126 PalGDI
->IndexedColors
[Index
].peGreen
= Colors
[Index
- StartIndex
].rgbGreen
;
127 PalGDI
->IndexedColors
[Index
].peBlue
= Colors
[Index
- StartIndex
].rgbBlue
;
129 PALETTE_UnlockPalette(PalGDI
);
134 /* Mark the brushes invalid */
135 dc
->pdcattr
->ulDirty_
|= DIRTY_FILL
|DIRTY_LINE
|DIRTY_BACKGROUND
|DIRTY_TEXT
;
153 UINT Index
, Count
= 0;
156 if (!(dc
= DC_LockDc(hDC
))) return 0;
157 if (dc
->dctype
== DC_TYPE_INFO
)
163 psurf
= dc
->dclevel
.pSurface
;
167 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
171 if (psurf
->hSecure
== NULL
)
174 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
178 biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
179 if (biBitCount
<= 8 &&
180 StartIndex
< (1 << biBitCount
))
182 if (StartIndex
+ Entries
> (1 << biBitCount
))
183 Entries
= (1 << biBitCount
) - StartIndex
;
185 if (psurf
->ppal
== NULL
)
188 SetLastWin32Error(ERROR_INVALID_HANDLE
);
192 PalGDI
= PALETTE_LockPalette(psurf
->ppal
->BaseObject
.hHmgr
);
194 for (Index
= StartIndex
;
195 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
198 Colors
[Index
- StartIndex
].rgbRed
= PalGDI
->IndexedColors
[Index
].peRed
;
199 Colors
[Index
- StartIndex
].rgbGreen
= PalGDI
->IndexedColors
[Index
].peGreen
;
200 Colors
[Index
- StartIndex
].rgbBlue
= PalGDI
->IndexedColors
[Index
].peBlue
;
201 Colors
[Index
- StartIndex
].rgbReserved
= 0;
204 PALETTE_UnlockPalette(PalGDI
);
212 // Converts a DIB to a device-dependent bitmap
221 CONST BITMAPINFO
*bmi
,
225 HBITMAP SourceBitmap
;
228 SURFOBJ
*DestSurf
, *SourceSurf
;
235 HPALETTE DIB_Palette
;
236 ULONG DIB_Palette_Type
;
240 if (!(bitmap
= SURFACE_LockSurface(hBitmap
)))
246 //if (ColorUse == DIB_PAL_COLORS)
247 // lpRGB = DIB_MapPaletteColors(hDC, bmi);
249 // lpRGB = &bmi->bmiColors;
251 DestSurf
= &bitmap
->SurfObj
;
253 // Create source surface
254 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
255 SourceSize
.cy
= ScanLines
;
257 // Determine width of DIB
258 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
);
260 SourceBitmap
= EngCreateBitmap(SourceSize
,
262 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
263 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
265 if (0 == SourceBitmap
)
267 SURFACE_UnlockSurface(bitmap
);
268 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
272 SourceSurf
= EngLockSurface((HSURF
)SourceBitmap
);
273 if (NULL
== SourceSurf
)
275 EngDeleteSurface((HSURF
)SourceBitmap
);
276 SURFACE_UnlockSurface(bitmap
);
277 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
281 ASSERT(bitmap
->ppal
);
283 // Source palette obtained from the BITMAPINFO
284 DIB_Palette
= BuildDIBPalette(bmi
, (PINT
)&DIB_Palette_Type
);
285 if (NULL
== DIB_Palette
)
287 EngUnlockSurface(SourceSurf
);
288 EngDeleteSurface((HSURF
)SourceBitmap
);
289 SURFACE_UnlockSurface(bitmap
);
290 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
294 ppalDIB
= PALETTE_LockPalette(DIB_Palette
);
296 /* Initialize XLATEOBJ for color translation */
297 EXLATEOBJ_vInitialize(&exlo
, ppalDIB
, bitmap
->ppal
, 0, 0, 0);
303 // Determine destination rectangle
305 DestRect
.top
= abs(bmi
->bmiHeader
.biHeight
) - StartScan
- ScanLines
;
306 DestRect
.right
= SourceSize
.cx
;
307 DestRect
.bottom
= DestRect
.top
+ ScanLines
;
309 copyBitsResult
= IntEngCopyBits(DestSurf
, SourceSurf
, NULL
, &exlo
.xlo
, &DestRect
, &ZeroPoint
);
311 // If it succeeded, return number of scanlines copies
312 if (copyBitsResult
== TRUE
)
314 result
= SourceSize
.cy
;
316 // result = abs(bmi->bmiHeader.biHeight) - StartScan;
320 EXLATEOBJ_vCleanup(&exlo
);
321 PALETTE_UnlockPalette(ppalDIB
);
322 PALETTE_FreePaletteByHandle(DIB_Palette
);
323 EngUnlockSurface(SourceSurf
);
324 EngDeleteSurface((HSURF
)SourceBitmap
);
326 // if (ColorUse == DIB_PAL_COLORS)
327 // WinFree((LPSTR)lpRGB);
329 SURFACE_UnlockSurface(bitmap
);
334 // FIXME by Removing NtGdiSetDIBits!!!
335 // This is a victim of the Win32k Initialization BUG!!!!!
336 // Converts a DIB to a device-dependent bitmap
345 CONST BITMAPINFO
*bmi
,
350 NTSTATUS Status
= STATUS_SUCCESS
;
351 BITMAPV5INFO bmiLocal
;
357 Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, bmi
, ColorUse
, 0);
358 /* Don't check Bits and hope we won't die */
360 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
362 Status
= _SEH2_GetExceptionCode();
366 if (!NT_SUCCESS(Status
))
374 SetLastWin32Error(ERROR_INVALID_HANDLE
);
377 if (Dc
->dctype
== DC_TYPE_INFO
)
383 Ret
= IntSetDIBits(Dc
, hBitmap
, StartScan
, ScanLines
, Bits
, (PBITMAPINFO
)&bmiLocal
, ColorUse
);
393 NtGdiSetDIBitsToDeviceInternal(
408 IN BOOL bTransformCoordinates
,
409 IN OPTIONAL HANDLE hcmXform
)
412 NTSTATUS Status
= STATUS_SUCCESS
;
414 HBITMAP hSourceBitmap
= NULL
;
415 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
;
422 PPALETTE ppalDIB
= NULL
;
423 HPALETTE hpalDIB
= NULL
;
424 ULONG DIBPaletteType
;
425 BITMAPV5INFO bmiLocal
;
431 Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, bmi
, ColorUse
, cjMaxInfo
);
432 ProbeForRead(Bits
, cjMaxBits
, 1);
434 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
436 Status
= _SEH2_GetExceptionCode();
440 if (!NT_SUCCESS(Status
))
445 pDC
= DC_LockDc(hDC
);
448 SetLastWin32Error(ERROR_INVALID_HANDLE
);
451 if (pDC
->dctype
== DC_TYPE_INFO
)
457 pSurf
= pDC
->dclevel
.pSurface
;
459 pDestSurf
= pSurf
? &pSurf
->SurfObj
: NULL
;
461 ScanLines
= min(ScanLines
, abs(bmiLocal
.bmiHeader
.bV5Height
) - StartScan
);
465 if (bTransformCoordinates
)
467 CoordLPtoDP(pDC
, (LPPOINT
)&rcDest
);
469 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
470 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
471 rcDest
.right
= rcDest
.left
+ Width
;
472 rcDest
.bottom
= rcDest
.top
+ Height
;
473 rcDest
.top
+= StartScan
;
478 SourceSize
.cx
= bmiLocal
.bmiHeader
.bV5Width
;
479 SourceSize
.cy
= ScanLines
;
481 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmiLocal
.bmiHeader
.bV5BitCount
);
483 hSourceBitmap
= EngCreateBitmap(SourceSize
,
485 BitmapFormat(bmiLocal
.bmiHeader
.bV5BitCount
,
486 bmiLocal
.bmiHeader
.bV5Compression
),
487 bmiLocal
.bmiHeader
.bV5Height
< 0 ? BMF_TOPDOWN
: 0,
491 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
492 Status
= STATUS_NO_MEMORY
;
496 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
499 Status
= STATUS_UNSUCCESSFUL
;
505 /* Create a palette for the DIB */
506 hpalDIB
= BuildDIBPalette((PBITMAPINFO
)&bmiLocal
, (PINT
)&DIBPaletteType
);
509 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
510 Status
= STATUS_NO_MEMORY
;
514 /* Lock the DIB palette */
515 ppalDIB
= PALETTE_LockPalette(hpalDIB
);
518 SetLastWin32Error(ERROR_INVALID_HANDLE
);
519 Status
= STATUS_UNSUCCESSFUL
;
523 /* Initialize EXLATEOBJ */
524 EXLATEOBJ_vInitialize(&exlo
, ppalDIB
, pSurf
->ppal
, 0, 0, 0);
527 DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
528 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
529 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
530 Status
= IntEngBitBlt(pDestSurf
,
533 pDC
->rosdc
.CombinedClip
,
540 ROP3_TO_ROP4(SRCCOPY
));
542 /* Cleanup EXLATEOBJ */
543 EXLATEOBJ_vCleanup(&exlo
);
546 if (NT_SUCCESS(Status
))
551 if (ppalDIB
) PALETTE_UnlockPalette(ppalDIB
);
553 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
554 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
555 if (hpalDIB
) PALETTE_FreePaletteByHandle(hpalDIB
);
562 /* Converts a device-dependent bitmap to a DIB */
565 NtGdiGetDIBitsInternal(
577 SURFACE
*psurf
= NULL
;
578 HBITMAP hDestBitmap
= NULL
;
579 HPALETTE hDestPalette
= NULL
;
580 BITMAPV5INFO bmiLocal
;
581 PPALETTE ppalDst
= NULL
;
582 NTSTATUS Status
= STATUS_SUCCESS
;
584 BOOL bPaletteMatch
= FALSE
;
585 PBYTE ChkBits
= Bits
;
586 ULONG DestPaletteType
;
589 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
591 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
594 // if ScanLines == 0, no need to copy Bits.
600 Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, Info
, Usage
, MaxInfo
);
601 if (ChkBits
) ProbeForWrite(ChkBits
, MaxBits
, 1);
603 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
605 Status
= _SEH2_GetExceptionCode();
609 if (!NT_SUCCESS(Status
))
615 if (Dc
== NULL
) return 0;
616 if (Dc
->dctype
== DC_TYPE_INFO
)
622 /* Get a pointer to the source bitmap object */
623 psurf
= SURFACE_LockSurface(hBitmap
);
634 /* Copy palette information
635 * Always create a palette for 15 & 16 bit. */
636 if ((bmiLocal
.bmiHeader
.bV5BitCount
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) &&
637 bmiLocal
.bmiHeader
.bV5BitCount
!= 15 && bmiLocal
.bmiHeader
.bV5BitCount
!= 16) ||
640 ppalDst
= psurf
->ppal
;
641 bPaletteMatch
= TRUE
;
645 hDestPalette
= BuildDIBPalette((PBITMAPINFO
)&bmiLocal
, (PINT
)&DestPaletteType
);
646 ppalDst
= PALETTE_LockPalette(hDestPalette
);
649 DPRINT("ppalDst : %p\n", ppalDst
);
653 switch (bmiLocal
.bmiHeader
.bV5BitCount
)
658 bmiLocal
.bmiHeader
.bV5ClrUsed
= 0;
659 if (psurf
->hSecure
&&
660 BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bmiLocal
.bmiHeader
.bV5BitCount
)
662 if (Usage
== DIB_RGB_COLORS
)
664 if (ppalDst
->NumColors
!= 1 << bmiLocal
.bmiHeader
.bV5BitCount
)
665 bmiLocal
.bmiHeader
.bV5ClrUsed
= ppalDst
->NumColors
;
667 Index
< (1 << bmiLocal
.bmiHeader
.bV5BitCount
) && Index
< ppalDst
->NumColors
;
670 bmiLocal
.bmiColors
[Index
].rgbRed
= ppalDst
->IndexedColors
[Index
].peRed
;
671 bmiLocal
.bmiColors
[Index
].rgbGreen
= ppalDst
->IndexedColors
[Index
].peGreen
;
672 bmiLocal
.bmiColors
[Index
].rgbBlue
= ppalDst
->IndexedColors
[Index
].peBlue
;
673 bmiLocal
.bmiColors
[Index
].rgbReserved
= 0;
679 Index
< (1 << bmiLocal
.bmiHeader
.bV5BitCount
);
682 ((WORD
*)bmiLocal
.bmiColors
)[Index
] = Index
;
688 if (Usage
== DIB_PAL_COLORS
)
691 Index
< (1 << bmiLocal
.bmiHeader
.bV5BitCount
);
694 ((WORD
*)bmiLocal
.bmiColors
)[Index
] = (WORD
)Index
;
697 else if (bmiLocal
.bmiHeader
.bV5BitCount
> 1 && bPaletteMatch
)
700 Index
< (1 << bmiLocal
.bmiHeader
.bV5BitCount
) && Index
< ppalDst
->NumColors
;
703 bmiLocal
.bmiColors
[Index
].rgbRed
= ppalDst
->IndexedColors
[Index
].peRed
;
704 bmiLocal
.bmiColors
[Index
].rgbGreen
= ppalDst
->IndexedColors
[Index
].peGreen
;
705 bmiLocal
.bmiColors
[Index
].rgbBlue
= ppalDst
->IndexedColors
[Index
].peBlue
;
706 bmiLocal
.bmiColors
[Index
].rgbReserved
= 0;
711 switch (bmiLocal
.bmiHeader
.bV5BitCount
)
714 bmiLocal
.bmiColors
[0].rgbRed
=0 ;
715 bmiLocal
.bmiColors
[0].rgbGreen
= 0;
716 bmiLocal
.bmiColors
[0].rgbBlue
= 0;
717 bmiLocal
.bmiColors
[0].rgbReserved
= 0;
718 bmiLocal
.bmiColors
[1].rgbRed
=0xFF ;
719 bmiLocal
.bmiColors
[1].rgbGreen
= 0xFF;
720 bmiLocal
.bmiColors
[1].rgbBlue
= 0xFF;
721 bmiLocal
.bmiColors
[1].rgbReserved
= 0;
724 RtlCopyMemory(bmiLocal
.bmiColors
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
731 RtlCopyMemory(bmiLocal
.bmiColors
, DefLogPaletteQuads
, 10 * sizeof(RGBQUAD
));
732 RtlCopyMemory(bmiLocal
.bmiColors
+ 246, DefLogPaletteQuads
+ 10, 10 * sizeof(RGBQUAD
));
733 color
= bmiLocal
.bmiColors
+ 10;
734 for (r
= 0; r
<= 5; r
++) /* FIXME */
735 for (g
= 0; g
<= 5; g
++)
736 for (b
= 0; b
<= 5; b
++)
738 color
->rgbRed
= (r
* 0xff) / 5;
739 color
->rgbGreen
= (g
* 0xff) / 5;
740 color
->rgbBlue
= (b
* 0xff) / 5;
741 color
->rgbReserved
= 0;
751 if (bmiLocal
.bmiHeader
.bV5Compression
== BI_BITFIELDS
)
753 bmiLocal
.bmiHeader
.bV5RedMask
= 0x7c00;
754 bmiLocal
.bmiHeader
.bV5GreenMask
= 0x03e0;
755 bmiLocal
.bmiHeader
.bV5BlueMask
= 0x001f;
760 if (bmiLocal
.bmiHeader
.bV5Compression
== BI_BITFIELDS
)
762 bmiLocal
.bmiHeader
.bV5RedMask
= 0xf800;
763 bmiLocal
.bmiHeader
.bV5GreenMask
= 0x07e0;
764 bmiLocal
.bmiHeader
.bV5BlueMask
= 0x001f;
770 if (bmiLocal
.bmiHeader
.bV5Compression
== BI_BITFIELDS
)
772 bmiLocal
.bmiHeader
.bV5RedMask
= 0xff0000;
773 bmiLocal
.bmiHeader
.bV5GreenMask
= 0x00ff00;
774 bmiLocal
.bmiHeader
.bV5BlueMask
= 0x0000ff;
779 /* fill out the BITMAPINFO struct */
782 bmiLocal
.bmiHeader
.bV5Width
= psurf
->SurfObj
.sizlBitmap
.cx
;
783 /* Report negative height for top-down bitmaps. */
784 if (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
)
785 bmiLocal
.bmiHeader
.bV5Height
= - psurf
->SurfObj
.sizlBitmap
.cy
;
787 bmiLocal
.bmiHeader
.bV5Height
= psurf
->SurfObj
.sizlBitmap
.cy
;
788 bmiLocal
.bmiHeader
.bV5Planes
= 1;
789 bmiLocal
.bmiHeader
.bV5BitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
790 switch (psurf
->SurfObj
.iBitmapFormat
)
792 /* FIXME: What about BI_BITFIELDS? */
799 bmiLocal
.bmiHeader
.bV5Compression
= BI_RGB
;
802 bmiLocal
.bmiHeader
.bV5Compression
= BI_RLE4
;
805 bmiLocal
.bmiHeader
.bV5Compression
= BI_RLE8
;
808 bmiLocal
.bmiHeader
.bV5Compression
= BI_JPEG
;
811 bmiLocal
.bmiHeader
.bV5Compression
= BI_PNG
;
815 bmiLocal
.bmiHeader
.bV5SizeImage
= psurf
->SurfObj
.cjBits
;
816 bmiLocal
.bmiHeader
.bV5XPelsPerMeter
= psurf
->sizlDim
.cx
; /* FIXME */
817 bmiLocal
.bmiHeader
.bV5YPelsPerMeter
= psurf
->sizlDim
.cy
; /* FIXME */
818 bmiLocal
.bmiHeader
.bV5ClrUsed
= 0;
819 bmiLocal
.bmiHeader
.bV5ClrImportant
= 1 << bmiLocal
.bmiHeader
.bV5BitCount
; /* FIXME */
820 Result
= psurf
->SurfObj
.sizlBitmap
.cy
;
828 // If we have a good dib pointer, why not just copy bits from there w/o XLATE'ing them.
830 /* Create the destination bitmap too for the copy operation */
831 if (StartScan
> psurf
->SurfObj
.sizlBitmap
.cy
)
837 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
838 DestSize
.cx
= psurf
->SurfObj
.sizlBitmap
.cx
;
839 DestSize
.cy
= ScanLines
;
843 bmiLocal
.bmiHeader
.bV5SizeImage
= DIB_GetDIBWidthBytes(DestSize
.cx
,
844 bmiLocal
.bmiHeader
.bV5BitCount
) * DestSize
.cy
;
846 hDestBitmap
= EngCreateBitmap(DestSize
,
847 DIB_GetDIBWidthBytes(DestSize
.cx
,
848 bmiLocal
.bmiHeader
.bV5BitCount
),
849 BitmapFormat(bmiLocal
.bmiHeader
.bV5BitCount
,
850 bmiLocal
.bmiHeader
.bV5Compression
),
851 bmiLocal
.bmiHeader
.bV5Height
> 0 ? 0 : BMF_TOPDOWN
,
854 if (hDestBitmap
== NULL
)
858 if (NT_SUCCESS(Status
))
861 SURFOBJ
*DestSurfObj
;
864 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, ppalDst
, 0, 0, 0);
867 SourcePoint
.y
= psurf
->SurfObj
.sizlBitmap
.cy
- (StartScan
+ ScanLines
);
869 /* Determine destination rectangle */
872 DestRect
.right
= DestSize
.cx
;
873 DestRect
.bottom
= DestSize
.cy
;
875 DestSurfObj
= EngLockSurface((HSURF
)hDestBitmap
);
877 if (IntEngCopyBits(DestSurfObj
,
884 DPRINT("GetDIBits %d \n",abs(bmiLocal
.bmiHeader
.bV5Height
) - StartScan
);
888 EXLATEOBJ_vCleanup(&exlo
);
889 EngUnlockSurface(DestSurfObj
);
893 /* Now that everything is over, get back the information to caller */
896 /* Note : Info has already been probed */
897 GetBMIFromBitmapV5Info(&bmiLocal
, Info
, Usage
);
899 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
901 /* FIXME: fail or something */
907 if (hDestBitmap
!= NULL
)
908 EngDeleteSurface((HSURF
)hDestBitmap
);
910 if (hDestPalette
!= NULL
&& !bPaletteMatch
)
912 PALETTE_UnlockPalette(ppalDst
);
913 PALETTE_FreePaletteByHandle(hDestPalette
);
916 SURFACE_UnlockSurface(psurf
);
918 DPRINT("leaving NtGdiGetDIBitsInternal\n");
925 NtGdiStretchDIBitsInternal(
936 LPBITMAPINFO BitsInfo
,
943 HBITMAP hBitmap
, hOldBitmap
= NULL
;
945 HPALETTE hPal
= NULL
;
948 BITMAPV5INFO bmiLocal
;
950 if (!Bits
|| !BitsInfo
)
952 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
958 Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, BitsInfo
, Usage
, cjMaxInfo
);
959 ProbeForRead(Bits
, cjMaxBits
, 1);
961 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
963 Status
= _SEH2_GetExceptionCode();
967 if (!NT_SUCCESS(Status
))
969 DPRINT1("NtGdiStretchDIBitsInternal fail to read BitMapInfo: %x or Bits: %x\n",BitsInfo
,Bits
);
973 hdcMem
= NtGdiCreateCompatibleDC(hDC
);
976 DPRINT1("NtGdiCreateCompatibleDC fail create hdc\n");
980 hBitmap
= NtGdiCreateCompatibleBitmap(hDC
,
981 abs(bmiLocal
.bmiHeader
.bV5Width
),
982 abs(bmiLocal
.bmiHeader
.bV5Height
));
985 DPRINT1("NtGdiCreateCompatibleBitmap fail create bitmap\n");
986 DPRINT1("hDC : 0x%08x \n", hDC
);
987 DPRINT1("BitsInfo->bmiHeader.biWidth : 0x%08x \n", BitsInfo
->bmiHeader
.biWidth
);
988 DPRINT1("BitsInfo->bmiHeader.biHeight : 0x%08x \n", BitsInfo
->bmiHeader
.biHeight
);
992 /* Select the bitmap into hdcMem, and save a handle to the old bitmap */
993 hOldBitmap
= NtGdiSelectBitmap(hdcMem
, hBitmap
);
995 if (Usage
== DIB_PAL_COLORS
)
997 hPal
= NtGdiGetDCObject(hDC
, GDI_OBJECT_TYPE_PALETTE
);
998 hPal
= GdiSelectPalette(hdcMem
, hPal
, FALSE
);
1001 if (bmiLocal
.bmiHeader
.bV5Compression
== BI_RLE4
||
1002 bmiLocal
.bmiHeader
.bV5Compression
== BI_RLE8
)
1004 /* copy existing bitmap from destination dc */
1005 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
1006 NtGdiBitBlt(hdcMem
, XSrc
, abs(bmiLocal
.bmiHeader
.bV5Height
) - SrcHeight
- YSrc
,
1007 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, ROP
, 0, 0);
1009 NtGdiStretchBlt(hdcMem
, XSrc
, abs(bmiLocal
.bmiHeader
.bV5Height
) - SrcHeight
- YSrc
,
1010 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
1014 pDC
= DC_LockDc(hdcMem
);
1017 /* Note BitsInfo->bmiHeader.biHeight is the number of scanline,
1018 * if it negitve we getting to many scanline for scanline is UINT not
1019 * a INT, so we need make the negtive value to positve and that make the
1020 * count correct for negtive bitmap, TODO : we need testcase for this api */
1021 IntSetDIBits(pDC
, hBitmap
, 0, abs(bmiLocal
.bmiHeader
.bV5Height
), Bits
,
1022 (PBITMAPINFO
)&bmiLocal
, Usage
);
1028 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
1029 left (negative biHeight) */
1030 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
1031 NtGdiBitBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
1032 hdcMem
, XSrc
, abs(bmiLocal
.bmiHeader
.bV5Height
) - SrcHeight
- YSrc
,
1035 NtGdiStretchBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
1036 hdcMem
, XSrc
, abs(bmiLocal
.bmiHeader
.bV5Height
) - SrcHeight
- YSrc
,
1037 SrcWidth
, SrcHeight
, ROP
, 0);
1041 GdiSelectPalette(hdcMem
, hPal
, FALSE
);
1044 NtGdiSelectBitmap(hdcMem
, hOldBitmap
);
1046 NtGdiDeleteObjectApp(hdcMem
);
1048 GreDeleteObject(hBitmap
);
1069 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1070 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1072 if (bpp
!= 1) fColor
= TRUE
;
1073 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1076 const RGBQUAD
*rgb
= data
->bmiColors
;
1077 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1079 // Check if the first color of the colormap is black
1080 if ((col
== RGB(0, 0, 0)))
1083 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1085 // If the second color is white, create a monochrome bitmap
1086 fColor
= (col
!= RGB(0xff,0xff,0xff));
1091 // Now create the bitmap
1094 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
1098 handle
= GreCreateBitmap(width
,
1108 if (NULL
!= handle
&& CBM_INIT
== init
)
1110 IntSetDIBits(Dc
, handle
, 0, height
, bits
, (BITMAPINFO
*)data
, coloruse
);
1116 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1117 // The DDB that is created will be whatever bit depth your reference DC is
1120 NtGdiCreateDIBitmapInternal(
1125 IN OPTIONAL LPBYTE pjInit
,
1126 IN OPTIONAL LPBITMAPINFO pbmi
,
1128 IN UINT cjMaxInitInfo
,
1136 BITMAPV5INFO bmiLocal
;
1137 NTSTATUS Status
= STATUS_SUCCESS
;
1141 if(pbmi
) Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, pbmi
, iUsage
, cjMaxInitInfo
);
1142 if(pjInit
&& (fInit
== CBM_INIT
)) ProbeForRead(pjInit
, cjMaxBits
, 1);
1144 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1146 Status
= _SEH2_GetExceptionCode();
1150 if(!NT_SUCCESS(Status
))
1152 SetLastNtError(Status
);
1156 if (!hDc
) // CreateBitmap
1157 { // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1158 hDc
= IntGdiCreateDC(NULL
, NULL
, NULL
, NULL
,FALSE
);
1161 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1165 Dc
= DC_LockDc(hDc
);
1168 NtGdiDeleteObjectApp(hDc
);
1169 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1173 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
? &bmiLocal
: NULL
, iUsage
);
1176 NtGdiDeleteObjectApp(hDc
);
1178 else // CreateCompatibleBitmap
1180 Dc
= DC_LockDc(hDc
);
1183 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1187 First create an un-initialised bitmap. The depth of the bitmap
1188 should match that of the hdc and not that supplied in bmih.
1191 bpp
= bmiLocal
.bmiHeader
.bV5BitCount
;
1194 if (Dc
->dctype
!= DC_TYPE_MEMORY
)
1195 bpp
= Dc
->ppdev
->gdiinfo
.cBitsPixel
;
1200 SURFACE
*psurf
= Dc
->dclevel
.pSurface
;
1201 Count
= BITMAP_GetObject(psurf
, sizeof(dibs
), &dibs
);
1206 if (Count
== sizeof(BITMAP
))
1207 /* A device-dependent bitmap is selected in the DC */
1208 bpp
= dibs
.dsBm
.bmBitsPixel
;
1210 /* A DIB section is selected in the DC */
1211 bpp
= dibs
.dsBmih
.biBitCount
;
1215 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
? &bmiLocal
: NULL
, iUsage
);
1224 NtGdiCreateDIBSection(
1226 IN OPTIONAL HANDLE hSection
,
1228 IN LPBITMAPINFO bmi
,
1232 IN ULONG_PTR dwColorSpace
,
1235 HBITMAP hbitmap
= 0;
1237 BOOL bDesktopDC
= FALSE
;
1239 if (!bmi
) return hbitmap
; // Make sure.
1241 // If the reference hdc is null, take the desktop dc
1244 hDC
= NtGdiCreateCompatibleDC(0);
1248 if ((dc
= DC_LockDc(hDC
)))
1250 hbitmap
= DIB_CreateDIBSection(dc
,
1261 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1265 NtGdiDeleteObjectApp(hDC
);
1272 DIB_CreateDIBSection(
1282 SURFACE
*bmp
= NULL
;
1283 void *mapBits
= NULL
;
1287 // Fill BITMAP32 structure with DIB data
1288 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1295 DWORD dsBitfields
[3] = {0};
1298 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1299 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1300 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1302 /* CreateDIBSection should fail for compressed formats */
1303 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1305 return (HBITMAP
)NULL
;
1308 pdcattr
= dc
->pdcattr
;
1310 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1312 bm
.bmWidth
= bi
->biWidth
;
1313 bm
.bmHeight
= effHeight
;
1314 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
1316 bm
.bmPlanes
= bi
->biPlanes
;
1317 bm
.bmBitsPixel
= bi
->biBitCount
;
1320 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1321 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1322 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
1323 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1327 SYSTEM_BASIC_INFORMATION Sbi
;
1330 LARGE_INTEGER SectionOffset
;
1333 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1337 if (!NT_SUCCESS(Status
))
1342 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1343 mapSize
= bi
->biSizeImage
+ (offset
- mapOffset
);
1345 SectionOffset
.LowPart
= mapOffset
;
1346 SectionOffset
.HighPart
= 0;
1348 Status
= ZwMapViewOfSection(section
,
1358 if (!NT_SUCCESS(Status
))
1360 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1364 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1366 else if (ovr_pitch
&& offset
)
1367 bm
.bmBits
= (LPVOID
) offset
;
1371 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1374 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1375 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1377 if (usage
== DIB_PAL_COLORS
)
1379 lpRGB
= DIB_MapPaletteColors(dc
, bmi
);
1380 ColorCount
= bi
->biClrUsed
;
1381 if (ColorCount
== 0)
1383 ColorCount
= 1 << bi
->biBitCount
;
1388 lpRGB
= bmi
->bmiColors
;
1389 ColorCount
= 1 << bi
->biBitCount
;
1392 /* Set dsBitfields values */
1393 if (usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
1395 dsBitfields
[0] = dsBitfields
[1] = dsBitfields
[2] = 0;
1397 else if (bi
->biCompression
== BI_RGB
)
1399 switch (bi
->biBitCount
)
1402 dsBitfields
[0] = 0x7c00;
1403 dsBitfields
[1] = 0x03e0;
1404 dsBitfields
[2] = 0x001f;
1408 dsBitfields
[0] = 0xF800;
1409 dsBitfields
[1] = 0x07e0;
1410 dsBitfields
[2] = 0x001f;
1415 dsBitfields
[0] = 0xff0000;
1416 dsBitfields
[1] = 0x00ff00;
1417 dsBitfields
[2] = 0x0000ff;
1423 dsBitfields
[0] = ((DWORD
*)bmi
->bmiColors
)[0];
1424 dsBitfields
[1] = ((DWORD
*)bmi
->bmiColors
)[1];
1425 dsBitfields
[2] = ((DWORD
*)bmi
->bmiColors
)[2];
1428 // Create Device Dependent Bitmap and add DIB pointer
1429 Size
.cx
= bm
.bmWidth
;
1430 Size
.cy
= abs(bm
.bmHeight
);
1431 res
= GreCreateBitmapEx(bm
.bmWidth
,
1434 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1435 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1436 (bi
->biHeight
< 0 ? BMF_TOPDOWN
: 0),
1441 if (lpRGB
!= bmi
->bmiColors
)
1443 ExFreePoolWithTag(lpRGB
, TAG_COLORMAP
);
1445 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
1448 bmp
= SURFACE_LockSurface(res
);
1451 if (lpRGB
!= bmi
->bmiColors
)
1453 ExFreePoolWithTag(lpRGB
, TAG_COLORMAP
);
1455 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1456 GreDeleteObject(res
);
1460 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1461 table between the DIB and the X physical device. Obviously,
1462 this is left out of the ReactOS implementation. Instead,
1463 we call NtGdiSetDIBColorTable. */
1464 if (bi
->biBitCount
<= 8)
1466 bi
->biClrUsed
= 1 << bi
->biBitCount
;
1473 bmp
->hDIBSection
= section
;
1474 bmp
->hSecure
= hSecure
;
1475 bmp
->dwOffset
= offset
;
1476 bmp
->flags
= API_BITMAP
;
1477 bmp
->dsBitfields
[0] = dsBitfields
[0];
1478 bmp
->dsBitfields
[1] = dsBitfields
[1];
1479 bmp
->dsBitfields
[2] = dsBitfields
[2];
1480 bmp
->biClrUsed
= bi
->biClrUsed
;
1481 bmp
->biClrImportant
= bi
->biClrImportant
;
1483 if (bi
->biClrUsed
!= 0)
1485 hpal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, lpRGB
);
1489 hpal
= PALETTE_AllocPalette(PAL_BITFIELDS
, 0, NULL
,
1495 bmp
->ppal
= PALETTE_ShareLockPalette(hpal
);
1496 /* Lazy delete hpal, it will be freed at surface release */
1497 GreDeleteObject(hpal
);
1499 // Clean up in case of errors
1500 if (!res
|| !bmp
|| !bm
.bmBits
)
1502 DPRINT("got an error res=%08x, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1505 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1508 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1513 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1521 SURFACE_FreeSurfaceByHandle(res
);
1526 if (lpRGB
!= bmi
->bmiColors
)
1528 ExFreePoolWithTag(lpRGB
, TAG_COLORMAP
);
1533 SURFACE_UnlockSurface(bmp
);
1536 // Return BITMAP handle and storage location
1537 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1542 if (res
) pdcattr
->ulDirty_
|= DC_DIBSECTION
;
1547 /***********************************************************************
1548 * DIB_GetDIBWidthBytes
1550 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1551 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
1552 * 11/16/1999 (RJJ) lifted from wine
1554 INT FASTCALL
DIB_GetDIBWidthBytes(INT width
, INT depth
)
1556 return ((width
* depth
+ 31) & ~31) >> 3;
1559 /***********************************************************************
1560 * DIB_GetDIBImageBytes
1562 * Return the number of bytes used to hold the image in a DIB bitmap.
1563 * 11/16/1999 (RJJ) lifted from wine
1566 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1568 return DIB_GetDIBWidthBytes(width
, depth
) * (height
< 0 ? -height
: height
);
1571 /***********************************************************************
1572 * DIB_BitmapInfoSize
1574 * Return the size of the bitmap info structure including color table.
1575 * 11/16/1999 (RJJ) lifted from wine
1578 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1582 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1584 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
1585 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1586 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1588 else /* assume BITMAPINFOHEADER */
1590 colors
= info
->bmiHeader
.biClrUsed
;
1591 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
1592 return info
->bmiHeader
.biSize
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1598 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1605 palGDI
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
1612 if (palGDI
->Mode
!= PAL_INDEXED
)
1614 PALETTE_UnlockPalette(palGDI
);
1618 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1619 if (lpbmi
->bmiHeader
.biClrUsed
)
1621 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1624 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1627 PALETTE_UnlockPalette(palGDI
);
1631 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1633 for (i
= 0; i
< nNumColors
; i
++)
1635 if (*lpIndex
< palGDI
->NumColors
)
1637 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1638 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1639 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1643 lpRGB
[i
].rgbRed
= 0;
1644 lpRGB
[i
].rgbGreen
= 0;
1645 lpRGB
[i
].rgbBlue
= 0;
1647 lpRGB
[i
].rgbReserved
= 0;
1650 PALETTE_UnlockPalette(palGDI
);
1657 BuildDIBPalette(CONST BITMAPINFO
*bmi
, PINT paletteType
)
1662 ULONG RedMask
, GreenMask
, BlueMask
;
1663 PDWORD pdwColors
= (PDWORD
)((PBYTE
)bmi
+ bmi
->bmiHeader
.biSize
);
1665 // Determine Bits Per Pixel
1666 bits
= bmi
->bmiHeader
.biBitCount
;
1668 // Determine paletteType from Bits Per Pixel
1671 *paletteType
= PAL_INDEXED
;
1672 RedMask
= GreenMask
= BlueMask
= 0;
1674 else if (bmi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
1676 *paletteType
= PAL_BITFIELDS
;
1677 RedMask
= pdwColors
[0];
1678 GreenMask
= pdwColors
[1];
1679 BlueMask
= pdwColors
[2];
1681 else if (bits
== 15)
1683 *paletteType
= PAL_BITFIELDS
;
1688 else if (bits
== 16)
1690 *paletteType
= PAL_BITFIELDS
;
1697 *paletteType
= PAL_BGR
;
1699 GreenMask
= 0x00ff00;
1700 BlueMask
= 0x0000ff;
1703 if (bmi
->bmiHeader
.biClrUsed
== 0)
1705 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1709 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1712 if (PAL_INDEXED
== *paletteType
)
1714 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)pdwColors
);
1718 hPal
= PALETTE_AllocPalette(*paletteType
, ColorCount
,
1720 RedMask
, GreenMask
, BlueMask
);
1728 GetBMIColor(CONST BITMAPINFO
* pbmi
, INT i
)
1732 if(pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1734 /* BITMAPCOREINFO holds RGBTRIPLE values */
1735 size
= sizeof(RGBTRIPLE
);
1739 size
= sizeof(RGBQUAD
);
1741 memcpy(&dwRet
, (PBYTE
)pbmi
+ pbmi
->bmiHeader
.biSize
+ i
*size
, size
);
1747 SetBMIColor(CONST BITMAPINFO
* pbmi
, DWORD
* color
, INT i
)
1749 if(pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1751 *(RGBTRIPLE
*)((PBYTE
)pbmi
+ pbmi
->bmiHeader
.biSize
+ i
*sizeof(RGBTRIPLE
)) = *(RGBTRIPLE
*)color
;
1755 *(RGBQUAD
*)((PBYTE
)pbmi
+ pbmi
->bmiHeader
.biSize
+ i
*sizeof(RGBQUAD
)) = *(RGBQUAD
*)color
;
1761 ProbeAndConvertToBitmapV5Info(
1762 OUT PBITMAPV5INFO pbmiDst
,
1763 IN CONST BITMAPINFO
* pbmiUnsafe
,
1764 IN DWORD dwColorUse
,
1769 PBITMAPV5HEADER pbmhDst
= &pbmiDst
->bmiHeader
;
1771 /* Get the size and probe */
1772 ProbeForRead(&pbmiUnsafe
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1773 dwSize
= pbmiUnsafe
->bmiHeader
.biSize
;
1774 /* At least dwSize bytes must be valids */
1775 ProbeForRead(pbmiUnsafe
, max(dwSize
, MaxSize
), 1);
1777 /* Check the size */
1778 // FIXME: are intermediate sizes allowed? As what are they interpreted?
1779 // make sure we don't use a too big dwSize later
1780 if (dwSize
!= sizeof(BITMAPCOREHEADER
) &&
1781 dwSize
!= sizeof(BITMAPINFOHEADER
) &&
1782 dwSize
!= sizeof(BITMAPV4HEADER
) &&
1783 dwSize
!= sizeof(BITMAPV5HEADER
))
1785 return STATUS_INVALID_PARAMETER
;
1788 if (dwSize
== sizeof(BITMAPCOREHEADER
))
1790 PBITMAPCOREHEADER pbch
= (PBITMAPCOREHEADER
)pbmiUnsafe
;
1792 /* Manually copy the fields that are present */
1793 pbmhDst
->bV5Width
= pbch
->bcWidth
;
1794 pbmhDst
->bV5Height
= pbch
->bcHeight
;
1795 pbmhDst
->bV5Planes
= pbch
->bcPlanes
;
1796 pbmhDst
->bV5BitCount
= pbch
->bcBitCount
;
1798 /* Set some default values */
1799 pbmhDst
->bV5Compression
= BI_RGB
;
1800 pbmhDst
->bV5SizeImage
= 0;
1801 pbmhDst
->bV5XPelsPerMeter
= 72;
1802 pbmhDst
->bV5YPelsPerMeter
= 72;
1803 pbmhDst
->bV5ClrUsed
= 0;
1804 pbmhDst
->bV5ClrImportant
= 0;
1808 /* Copy valid fields */
1809 memcpy(pbmiDst
, pbmiUnsafe
, dwSize
);
1811 if(dwSize
< sizeof(BITMAPV5HEADER
))
1813 /* Zero out the rest of the V5 header */
1814 memset((char*)pbmiDst
+ dwSize
, 0, sizeof(BITMAPV5HEADER
) - dwSize
);
1817 pbmhDst
->bV5Size
= sizeof(BITMAPV5HEADER
);
1820 if (dwSize
< sizeof(BITMAPV4HEADER
))
1822 if (pbmhDst
->bV5Compression
== BI_BITFIELDS
)
1824 pbmhDst
->bV5RedMask
= GetBMIColor(pbmiUnsafe
, 0);
1825 pbmhDst
->bV5GreenMask
= GetBMIColor(pbmiUnsafe
, 1);
1826 pbmhDst
->bV5BlueMask
= GetBMIColor(pbmiUnsafe
, 2);
1827 pbmhDst
->bV5AlphaMask
= 0;
1828 pbmhDst
->bV5ClrUsed
= 0;
1831 // pbmhDst->bV5CSType;
1832 // pbmhDst->bV5Endpoints;
1833 // pbmhDst->bV5GammaRed;
1834 // pbmhDst->bV5GammaGreen;
1835 // pbmhDst->bV5GammaBlue;
1838 if (dwSize
< sizeof(BITMAPV5HEADER
))
1840 // pbmhDst->bV5Intent;
1841 // pbmhDst->bV5ProfileData;
1842 // pbmhDst->bV5ProfileSize;
1843 // pbmhDst->bV5Reserved;
1846 ulWidthBytes
= ((pbmhDst
->bV5Width
* pbmhDst
->bV5Planes
*
1847 pbmhDst
->bV5BitCount
+ 31) & ~31) / 8;
1849 if (pbmhDst
->bV5SizeImage
== 0)
1850 pbmhDst
->bV5SizeImage
= abs(ulWidthBytes
* pbmhDst
->bV5Height
);
1852 if (pbmhDst
->bV5ClrUsed
== 0)
1854 switch(pbmhDst
->bV5BitCount
)
1857 pbmhDst
->bV5ClrUsed
= 2;
1860 pbmhDst
->bV5ClrUsed
= 16;
1863 pbmhDst
->bV5ClrUsed
= 256;
1866 pbmhDst
->bV5ClrUsed
= 0;
1871 if (pbmhDst
->bV5Planes
!= 1)
1873 return STATUS_INVALID_PARAMETER
;
1876 if (pbmhDst
->bV5BitCount
!= 0 && pbmhDst
->bV5BitCount
!= 1 &&
1877 pbmhDst
->bV5BitCount
!= 4 && pbmhDst
->bV5BitCount
!= 8 &&
1878 pbmhDst
->bV5BitCount
!= 16 && pbmhDst
->bV5BitCount
!= 24 &&
1879 pbmhDst
->bV5BitCount
!= 32)
1881 DPRINT("Invalid bit count: %d\n", pbmhDst
->bV5BitCount
);
1882 return STATUS_INVALID_PARAMETER
;
1885 if ((pbmhDst
->bV5BitCount
== 0 &&
1886 pbmhDst
->bV5Compression
!= BI_JPEG
&& pbmhDst
->bV5Compression
!= BI_PNG
))
1888 DPRINT("Bit count 0 is invalid for compression %d.\n", pbmhDst
->bV5Compression
);
1889 return STATUS_INVALID_PARAMETER
;
1892 if (pbmhDst
->bV5Compression
== BI_BITFIELDS
&&
1893 pbmhDst
->bV5BitCount
!= 16 && pbmhDst
->bV5BitCount
!= 32)
1895 DPRINT("Bit count %d is invalid for compression BI_BITFIELDS.\n", pbmhDst
->bV5BitCount
);
1896 return STATUS_INVALID_PARAMETER
;
1900 if(pbmhDst
->bV5ClrUsed
)
1903 if(dwColorUse
== DIB_PAL_COLORS
)
1905 RtlCopyMemory(pbmiDst
->bmiColors
,
1906 pbmiUnsafe
->bmiColors
,
1907 pbmhDst
->bV5ClrUsed
* sizeof(WORD
));
1911 for(i
= 0; i
< pbmhDst
->bV5ClrUsed
; i
++)
1913 ((DWORD
*)pbmiDst
->bmiColors
)[i
] = GetBMIColor(pbmiUnsafe
, i
);
1918 return STATUS_SUCCESS
;
1923 GetBMIFromBitmapV5Info(IN PBITMAPV5INFO pbmiSrc
,
1924 OUT PBITMAPINFO pbmiDst
,
1925 IN DWORD dwColorUse
)
1927 if(pbmiDst
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1929 /* Manually set value */
1930 BITMAPCOREHEADER
* pbmhCore
= (BITMAPCOREHEADER
*)&pbmiDst
->bmiHeader
;
1931 pbmhCore
->bcWidth
= pbmiSrc
->bmiHeader
.bV5Width
;
1932 pbmhCore
->bcHeight
= pbmiSrc
->bmiHeader
.bV5Height
;
1933 pbmhCore
->bcPlanes
= pbmiSrc
->bmiHeader
.bV5Planes
;
1934 pbmhCore
->bcBitCount
= pbmiSrc
->bmiHeader
.bV5BitCount
;
1938 /* Copy valid Fields, keep bmiHeader.biSize safe */
1939 RtlCopyMemory(&pbmiDst
->bmiHeader
.biWidth
,
1940 &pbmiSrc
->bmiHeader
.bV5Width
,
1941 pbmiDst
->bmiHeader
.biSize
- sizeof(DWORD
));
1943 if((pbmiDst
->bmiHeader
.biSize
< sizeof(BITMAPV4HEADER
)) &&
1944 (pbmiSrc
->bmiHeader
.bV5Compression
== BI_BITFIELDS
))
1946 /* Masks are already set in V4 and V5 headers */
1947 SetBMIColor(pbmiDst
, &pbmiSrc
->bmiHeader
.bV5RedMask
, 0);
1948 SetBMIColor(pbmiDst
, &pbmiSrc
->bmiHeader
.bV5GreenMask
, 1);
1949 SetBMIColor(pbmiDst
, &pbmiSrc
->bmiHeader
.bV5BlueMask
, 2);
1954 if(dwColorUse
== DIB_PAL_COLORS
)
1956 RtlCopyMemory(pbmiDst
->bmiColors
,
1958 pbmiSrc
->bmiHeader
.bV5ClrUsed
* sizeof(WORD
));
1962 for(i
= 0; i
< pbmiSrc
->bmiHeader
.bV5ClrUsed
; i
++)
1964 SetBMIColor(pbmiDst
, (DWORD
*)pbmiSrc
->bmiColors
+ i
, i
);