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 ProbeForRead(Bits
, bmiLocal
.bmiHeader
.bV5SizeImage
, 1);
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
,
1133 BITMAPV5INFO bmiLocal
;
1134 NTSTATUS Status
= STATUS_SUCCESS
;
1138 if(pbmi
) Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, pbmi
, iUsage
, cjMaxInitInfo
);
1139 if(pjInit
&& (fInit
== CBM_INIT
)) ProbeForRead(pjInit
, cjMaxBits
, 1);
1141 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1143 Status
= _SEH2_GetExceptionCode();
1147 if(!NT_SUCCESS(Status
))
1149 SetLastNtError(Status
);
1153 return GreCreateDIBitmapInternal(hDc
,
1158 pbmi
? &bmiLocal
: NULL
,
1166 GreCreateDIBitmapInternal(
1171 IN OPTIONAL LPBYTE pjInit
,
1172 IN OPTIONAL PBITMAPV5INFO pbmi
,
1182 if (!hDc
) /* 1bpp monochrome bitmap */
1183 { // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1184 hdcDest
= IntGdiCreateDC(NULL
, NULL
, NULL
, NULL
,FALSE
);
1195 Dc
= DC_LockDc(hdcDest
);
1198 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1201 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1202 * if bpp != 1 and ignore the real value that was passed */
1204 bpp
= pbmi
->bmiHeader
.bV5BitCount
;
1207 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
, iUsage
);
1212 NtGdiDeleteObjectApp(hdcDest
);
1220 NtGdiCreateDIBSection(
1222 IN OPTIONAL HANDLE hSection
,
1224 IN LPBITMAPINFO bmi
,
1228 IN ULONG_PTR dwColorSpace
,
1231 HBITMAP hbitmap
= 0;
1233 BOOL bDesktopDC
= FALSE
;
1235 if (!bmi
) return hbitmap
; // Make sure.
1237 // If the reference hdc is null, take the desktop dc
1240 hDC
= NtGdiCreateCompatibleDC(0);
1244 if ((dc
= DC_LockDc(hDC
)))
1246 hbitmap
= DIB_CreateDIBSection(dc
,
1257 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1261 NtGdiDeleteObjectApp(hDC
);
1268 DIB_CreateDIBSection(
1278 SURFACE
*bmp
= NULL
;
1279 void *mapBits
= NULL
;
1283 // Fill BITMAP32 structure with DIB data
1284 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1291 DWORD dsBitfields
[3] = {0};
1294 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1295 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1296 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1298 /* CreateDIBSection should fail for compressed formats */
1299 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1301 return (HBITMAP
)NULL
;
1304 pdcattr
= dc
->pdcattr
;
1306 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1308 bm
.bmWidth
= bi
->biWidth
;
1309 bm
.bmHeight
= effHeight
;
1310 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
1312 bm
.bmPlanes
= bi
->biPlanes
;
1313 bm
.bmBitsPixel
= bi
->biBitCount
;
1316 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1317 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1318 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
1319 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1323 SYSTEM_BASIC_INFORMATION Sbi
;
1326 LARGE_INTEGER SectionOffset
;
1329 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1333 if (!NT_SUCCESS(Status
))
1338 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1339 mapSize
= bi
->biSizeImage
+ (offset
- mapOffset
);
1341 SectionOffset
.LowPart
= mapOffset
;
1342 SectionOffset
.HighPart
= 0;
1344 Status
= ZwMapViewOfSection(section
,
1354 if (!NT_SUCCESS(Status
))
1356 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1360 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1362 else if (ovr_pitch
&& offset
)
1363 bm
.bmBits
= (LPVOID
) offset
;
1367 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1370 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1371 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1373 if (usage
== DIB_PAL_COLORS
)
1375 lpRGB
= DIB_MapPaletteColors(dc
, bmi
);
1376 ColorCount
= bi
->biClrUsed
;
1377 if (ColorCount
== 0)
1379 ColorCount
= 1 << bi
->biBitCount
;
1384 lpRGB
= bmi
->bmiColors
;
1385 ColorCount
= 1 << bi
->biBitCount
;
1388 /* Set dsBitfields values */
1389 if (usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
1391 dsBitfields
[0] = dsBitfields
[1] = dsBitfields
[2] = 0;
1393 else if (bi
->biCompression
== BI_RGB
)
1395 switch (bi
->biBitCount
)
1398 dsBitfields
[0] = 0x7c00;
1399 dsBitfields
[1] = 0x03e0;
1400 dsBitfields
[2] = 0x001f;
1404 dsBitfields
[0] = 0xF800;
1405 dsBitfields
[1] = 0x07e0;
1406 dsBitfields
[2] = 0x001f;
1411 dsBitfields
[0] = 0xff0000;
1412 dsBitfields
[1] = 0x00ff00;
1413 dsBitfields
[2] = 0x0000ff;
1419 dsBitfields
[0] = ((DWORD
*)bmi
->bmiColors
)[0];
1420 dsBitfields
[1] = ((DWORD
*)bmi
->bmiColors
)[1];
1421 dsBitfields
[2] = ((DWORD
*)bmi
->bmiColors
)[2];
1424 // Create Device Dependent Bitmap and add DIB pointer
1425 Size
.cx
= bm
.bmWidth
;
1426 Size
.cy
= abs(bm
.bmHeight
);
1427 res
= GreCreateBitmapEx(bm
.bmWidth
,
1430 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1431 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1432 (bi
->biHeight
< 0 ? BMF_TOPDOWN
: 0),
1437 if (lpRGB
!= bmi
->bmiColors
)
1439 ExFreePoolWithTag(lpRGB
, TAG_COLORMAP
);
1441 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
1444 bmp
= SURFACE_LockSurface(res
);
1447 if (lpRGB
!= bmi
->bmiColors
)
1449 ExFreePoolWithTag(lpRGB
, TAG_COLORMAP
);
1451 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1452 GreDeleteObject(res
);
1456 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1457 table between the DIB and the X physical device. Obviously,
1458 this is left out of the ReactOS implementation. Instead,
1459 we call NtGdiSetDIBColorTable. */
1460 if (bi
->biBitCount
<= 8)
1462 bi
->biClrUsed
= 1 << bi
->biBitCount
;
1469 bmp
->hDIBSection
= section
;
1470 bmp
->hSecure
= hSecure
;
1471 bmp
->dwOffset
= offset
;
1472 bmp
->flags
= API_BITMAP
;
1473 bmp
->dsBitfields
[0] = dsBitfields
[0];
1474 bmp
->dsBitfields
[1] = dsBitfields
[1];
1475 bmp
->dsBitfields
[2] = dsBitfields
[2];
1476 bmp
->biClrUsed
= bi
->biClrUsed
;
1477 bmp
->biClrImportant
= bi
->biClrImportant
;
1479 if (bi
->biClrUsed
!= 0)
1481 hpal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, lpRGB
);
1485 hpal
= PALETTE_AllocPalette(PAL_BITFIELDS
, 0, NULL
,
1491 bmp
->ppal
= PALETTE_ShareLockPalette(hpal
);
1492 /* Lazy delete hpal, it will be freed at surface release */
1493 GreDeleteObject(hpal
);
1495 // Clean up in case of errors
1496 if (!res
|| !bmp
|| !bm
.bmBits
)
1498 DPRINT("got an error res=%08x, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1501 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1504 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1509 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1517 SURFACE_FreeSurfaceByHandle(res
);
1522 if (lpRGB
!= bmi
->bmiColors
)
1524 ExFreePoolWithTag(lpRGB
, TAG_COLORMAP
);
1529 SURFACE_UnlockSurface(bmp
);
1532 // Return BITMAP handle and storage location
1533 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1538 if (res
) pdcattr
->ulDirty_
|= DC_DIBSECTION
;
1543 /***********************************************************************
1544 * DIB_GetDIBWidthBytes
1546 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1547 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
1548 * 11/16/1999 (RJJ) lifted from wine
1550 INT FASTCALL
DIB_GetDIBWidthBytes(INT width
, INT depth
)
1552 return ((width
* depth
+ 31) & ~31) >> 3;
1555 /***********************************************************************
1556 * DIB_GetDIBImageBytes
1558 * Return the number of bytes used to hold the image in a DIB bitmap.
1559 * 11/16/1999 (RJJ) lifted from wine
1562 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1564 return DIB_GetDIBWidthBytes(width
, depth
) * (height
< 0 ? -height
: height
);
1567 /***********************************************************************
1568 * DIB_BitmapInfoSize
1570 * Return the size of the bitmap info structure including color table.
1571 * 11/16/1999 (RJJ) lifted from wine
1574 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1578 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1580 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
1581 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1582 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1584 else /* assume BITMAPINFOHEADER */
1586 colors
= info
->bmiHeader
.biClrUsed
;
1587 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
1588 return info
->bmiHeader
.biSize
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1594 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1601 palGDI
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
1608 if (palGDI
->Mode
!= PAL_INDEXED
)
1610 PALETTE_UnlockPalette(palGDI
);
1614 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1615 if (lpbmi
->bmiHeader
.biClrUsed
)
1617 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1620 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1623 PALETTE_UnlockPalette(palGDI
);
1627 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1629 for (i
= 0; i
< nNumColors
; i
++)
1631 if (*lpIndex
< palGDI
->NumColors
)
1633 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1634 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1635 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1639 lpRGB
[i
].rgbRed
= 0;
1640 lpRGB
[i
].rgbGreen
= 0;
1641 lpRGB
[i
].rgbBlue
= 0;
1643 lpRGB
[i
].rgbReserved
= 0;
1646 PALETTE_UnlockPalette(palGDI
);
1653 BuildDIBPalette(CONST BITMAPINFO
*bmi
, PINT paletteType
)
1658 ULONG RedMask
, GreenMask
, BlueMask
;
1659 PDWORD pdwColors
= (PDWORD
)((PBYTE
)bmi
+ bmi
->bmiHeader
.biSize
);
1661 // Determine Bits Per Pixel
1662 bits
= bmi
->bmiHeader
.biBitCount
;
1664 // Determine paletteType from Bits Per Pixel
1667 *paletteType
= PAL_INDEXED
;
1668 RedMask
= GreenMask
= BlueMask
= 0;
1670 else if (bmi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
1672 *paletteType
= PAL_BITFIELDS
;
1673 RedMask
= pdwColors
[0];
1674 GreenMask
= pdwColors
[1];
1675 BlueMask
= pdwColors
[2];
1677 else if (bits
== 15)
1679 *paletteType
= PAL_BITFIELDS
;
1684 else if (bits
== 16)
1686 *paletteType
= PAL_BITFIELDS
;
1693 *paletteType
= PAL_BGR
;
1695 GreenMask
= 0x00ff00;
1696 BlueMask
= 0x0000ff;
1699 if (bmi
->bmiHeader
.biClrUsed
== 0)
1701 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1705 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1708 if (PAL_INDEXED
== *paletteType
)
1710 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)pdwColors
);
1714 hPal
= PALETTE_AllocPalette(*paletteType
, ColorCount
,
1716 RedMask
, GreenMask
, BlueMask
);
1724 GetBMIColor(CONST BITMAPINFO
* pbmi
, INT i
)
1728 if(pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1730 /* BITMAPCOREINFO holds RGBTRIPLE values */
1731 size
= sizeof(RGBTRIPLE
);
1735 size
= sizeof(RGBQUAD
);
1737 memcpy(&dwRet
, (PBYTE
)pbmi
+ pbmi
->bmiHeader
.biSize
+ i
*size
, size
);
1743 SetBMIColor(CONST BITMAPINFO
* pbmi
, DWORD
* color
, INT i
)
1745 PVOID pvColors
= ((PBYTE
)pbmi
+ pbmi
->bmiHeader
.biSize
);
1746 if(pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1748 RGBTRIPLE
*pColor
= pvColors
;
1749 pColor
[i
] = *(RGBTRIPLE
*)color
;
1753 RGBQUAD
*pColor
= pvColors
;
1754 pColor
[i
] = *(RGBQUAD
*)color
;
1760 ProbeAndConvertToBitmapV5Info(
1761 OUT PBITMAPV5INFO pbmiDst
,
1762 IN CONST BITMAPINFO
* pbmiUnsafe
,
1763 IN DWORD dwColorUse
,
1768 PBITMAPV5HEADER pbmhDst
= &pbmiDst
->bmiHeader
;
1770 /* Get the size and probe */
1771 ProbeForRead(&pbmiUnsafe
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1772 dwSize
= pbmiUnsafe
->bmiHeader
.biSize
;
1773 /* At least dwSize bytes must be valids */
1774 ProbeForRead(pbmiUnsafe
, max(dwSize
, MaxSize
), 1);
1776 /* Check the size */
1777 // FIXME: are intermediate sizes allowed? As what are they interpreted?
1778 // make sure we don't use a too big dwSize later
1779 if (dwSize
!= sizeof(BITMAPCOREHEADER
) &&
1780 dwSize
!= sizeof(BITMAPINFOHEADER
) &&
1781 dwSize
!= sizeof(BITMAPV4HEADER
) &&
1782 dwSize
!= sizeof(BITMAPV5HEADER
))
1784 return STATUS_INVALID_PARAMETER
;
1787 if (dwSize
== sizeof(BITMAPCOREHEADER
))
1789 PBITMAPCOREHEADER pbch
= (PBITMAPCOREHEADER
)pbmiUnsafe
;
1791 /* Manually copy the fields that are present */
1792 pbmhDst
->bV5Width
= pbch
->bcWidth
;
1793 pbmhDst
->bV5Height
= pbch
->bcHeight
;
1794 pbmhDst
->bV5Planes
= pbch
->bcPlanes
;
1795 pbmhDst
->bV5BitCount
= pbch
->bcBitCount
;
1797 /* Set some default values */
1798 pbmhDst
->bV5Compression
= BI_RGB
;
1799 pbmhDst
->bV5SizeImage
= DIB_GetDIBImageBytes(pbch
->bcWidth
,
1801 pbch
->bcPlanes
*pbch
->bcBitCount
) ;
1802 pbmhDst
->bV5XPelsPerMeter
= 72;
1803 pbmhDst
->bV5YPelsPerMeter
= 72;
1804 pbmhDst
->bV5ClrUsed
= 0;
1805 pbmhDst
->bV5ClrImportant
= 0;
1809 /* Copy valid fields */
1810 memcpy(pbmiDst
, pbmiUnsafe
, dwSize
);
1811 if(!pbmhDst
->bV5SizeImage
)
1812 pbmhDst
->bV5SizeImage
= DIB_GetDIBImageBytes(pbmhDst
->bV5Width
,
1814 pbmhDst
->bV5Planes
*pbmhDst
->bV5BitCount
) ;
1816 if(dwSize
< sizeof(BITMAPV5HEADER
))
1818 /* Zero out the rest of the V5 header */
1819 memset((char*)pbmiDst
+ dwSize
, 0, sizeof(BITMAPV5HEADER
) - dwSize
);
1822 pbmhDst
->bV5Size
= sizeof(BITMAPV5HEADER
);
1825 if (dwSize
< sizeof(BITMAPV4HEADER
))
1827 if (pbmhDst
->bV5Compression
== BI_BITFIELDS
)
1829 pbmhDst
->bV5RedMask
= GetBMIColor(pbmiUnsafe
, 0);
1830 pbmhDst
->bV5GreenMask
= GetBMIColor(pbmiUnsafe
, 1);
1831 pbmhDst
->bV5BlueMask
= GetBMIColor(pbmiUnsafe
, 2);
1832 pbmhDst
->bV5AlphaMask
= 0;
1833 pbmhDst
->bV5ClrUsed
= 0;
1836 // pbmhDst->bV5CSType;
1837 // pbmhDst->bV5Endpoints;
1838 // pbmhDst->bV5GammaRed;
1839 // pbmhDst->bV5GammaGreen;
1840 // pbmhDst->bV5GammaBlue;
1843 if (dwSize
< sizeof(BITMAPV5HEADER
))
1845 // pbmhDst->bV5Intent;
1846 // pbmhDst->bV5ProfileData;
1847 // pbmhDst->bV5ProfileSize;
1848 // pbmhDst->bV5Reserved;
1851 ulWidthBytes
= ((pbmhDst
->bV5Width
* pbmhDst
->bV5Planes
*
1852 pbmhDst
->bV5BitCount
+ 31) & ~31) / 8;
1854 if (pbmhDst
->bV5SizeImage
== 0)
1855 pbmhDst
->bV5SizeImage
= abs(ulWidthBytes
* pbmhDst
->bV5Height
);
1857 if (pbmhDst
->bV5ClrUsed
== 0)
1859 switch(pbmhDst
->bV5BitCount
)
1862 pbmhDst
->bV5ClrUsed
= 2;
1865 pbmhDst
->bV5ClrUsed
= 16;
1868 pbmhDst
->bV5ClrUsed
= 256;
1871 pbmhDst
->bV5ClrUsed
= 0;
1876 if (pbmhDst
->bV5Planes
!= 1)
1878 return STATUS_INVALID_PARAMETER
;
1881 if (pbmhDst
->bV5BitCount
!= 0 && pbmhDst
->bV5BitCount
!= 1 &&
1882 pbmhDst
->bV5BitCount
!= 4 && pbmhDst
->bV5BitCount
!= 8 &&
1883 pbmhDst
->bV5BitCount
!= 16 && pbmhDst
->bV5BitCount
!= 24 &&
1884 pbmhDst
->bV5BitCount
!= 32)
1886 DPRINT("Invalid bit count: %d\n", pbmhDst
->bV5BitCount
);
1887 return STATUS_INVALID_PARAMETER
;
1890 if ((pbmhDst
->bV5BitCount
== 0 &&
1891 pbmhDst
->bV5Compression
!= BI_JPEG
&& pbmhDst
->bV5Compression
!= BI_PNG
))
1893 DPRINT("Bit count 0 is invalid for compression %d.\n", pbmhDst
->bV5Compression
);
1894 return STATUS_INVALID_PARAMETER
;
1897 if (pbmhDst
->bV5Compression
== BI_BITFIELDS
&&
1898 pbmhDst
->bV5BitCount
!= 16 && pbmhDst
->bV5BitCount
!= 32)
1900 DPRINT("Bit count %d is invalid for compression BI_BITFIELDS.\n", pbmhDst
->bV5BitCount
);
1901 return STATUS_INVALID_PARAMETER
;
1905 if(pbmhDst
->bV5ClrUsed
)
1908 if(dwColorUse
== DIB_PAL_COLORS
)
1910 RtlCopyMemory(pbmiDst
->bmiColors
,
1911 pbmiUnsafe
->bmiColors
,
1912 pbmhDst
->bV5ClrUsed
* sizeof(WORD
));
1916 for(i
= 0; i
< pbmhDst
->bV5ClrUsed
; i
++)
1918 ((DWORD
*)pbmiDst
->bmiColors
)[i
] = GetBMIColor(pbmiUnsafe
, i
);
1923 return STATUS_SUCCESS
;
1928 GetBMIFromBitmapV5Info(IN PBITMAPV5INFO pbmiSrc
,
1929 OUT PBITMAPINFO pbmiDst
,
1930 IN DWORD dwColorUse
)
1932 if(pbmiDst
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1934 /* Manually set value */
1935 BITMAPCOREHEADER
* pbmhCore
= (BITMAPCOREHEADER
*)&pbmiDst
->bmiHeader
;
1936 pbmhCore
->bcWidth
= pbmiSrc
->bmiHeader
.bV5Width
;
1937 pbmhCore
->bcHeight
= pbmiSrc
->bmiHeader
.bV5Height
;
1938 pbmhCore
->bcPlanes
= pbmiSrc
->bmiHeader
.bV5Planes
;
1939 pbmhCore
->bcBitCount
= pbmiSrc
->bmiHeader
.bV5BitCount
;
1943 /* Copy valid Fields, keep bmiHeader.biSize safe */
1944 RtlCopyMemory(&pbmiDst
->bmiHeader
.biWidth
,
1945 &pbmiSrc
->bmiHeader
.bV5Width
,
1946 pbmiDst
->bmiHeader
.biSize
- sizeof(DWORD
));
1948 if((pbmiDst
->bmiHeader
.biSize
< sizeof(BITMAPV4HEADER
)) &&
1949 (pbmiSrc
->bmiHeader
.bV5Compression
== BI_BITFIELDS
))
1951 /* Masks are already set in V4 and V5 headers */
1952 SetBMIColor(pbmiDst
, &pbmiSrc
->bmiHeader
.bV5RedMask
, 0);
1953 SetBMIColor(pbmiDst
, &pbmiSrc
->bmiHeader
.bV5GreenMask
, 1);
1954 SetBMIColor(pbmiDst
, &pbmiSrc
->bmiHeader
.bV5BlueMask
, 2);
1961 cColorsUsed
= pbmiSrc
->bmiHeader
.bV5ClrUsed
;
1962 if (cColorsUsed
== 0 && pbmiSrc
->bmiHeader
.bV5BitCount
<= 8)
1963 cColorsUsed
= (1 << pbmiSrc
->bmiHeader
.bV5BitCount
);
1965 if(dwColorUse
== DIB_PAL_COLORS
)
1967 RtlCopyMemory(pbmiDst
->bmiColors
,
1969 cColorsUsed
* sizeof(WORD
));
1973 for(i
= 0; i
< cColorsUsed
; i
++)
1975 SetBMIColor(pbmiDst
, (DWORD
*)pbmiSrc
->bmiColors
+ i
, i
);