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
);
359 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
361 Status
= _SEH2_GetExceptionCode();
365 if (!NT_SUCCESS(Status
))
373 SetLastWin32Error(ERROR_INVALID_HANDLE
);
376 if (Dc
->dctype
== DC_TYPE_INFO
)
382 Ret
= IntSetDIBits(Dc
, hBitmap
, StartScan
, ScanLines
, Bits
, (PBITMAPINFO
)&bmiLocal
, ColorUse
);
392 NtGdiSetDIBitsToDeviceInternal(
407 IN BOOL bTransformCoordinates
,
408 IN OPTIONAL HANDLE hcmXform
)
411 NTSTATUS Status
= STATUS_SUCCESS
;
413 HBITMAP hSourceBitmap
= NULL
;
414 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
;
421 PPALETTE ppalDIB
= NULL
;
422 HPALETTE hpalDIB
= NULL
;
423 ULONG DIBPaletteType
;
424 BITMAPV5INFO bmiLocal
;
430 Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, bmi
, ColorUse
);
432 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
434 Status
= _SEH2_GetExceptionCode();
438 if (!NT_SUCCESS(Status
))
443 pDC
= DC_LockDc(hDC
);
446 SetLastWin32Error(ERROR_INVALID_HANDLE
);
449 if (pDC
->dctype
== DC_TYPE_INFO
)
455 pSurf
= pDC
->dclevel
.pSurface
;
457 pDestSurf
= pSurf
? &pSurf
->SurfObj
: NULL
;
459 ScanLines
= min(ScanLines
, abs(bmiLocal
.bmiHeader
.bV5Height
) - StartScan
);
463 if (bTransformCoordinates
)
465 CoordLPtoDP(pDC
, (LPPOINT
)&rcDest
);
467 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
468 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
469 rcDest
.right
= rcDest
.left
+ Width
;
470 rcDest
.bottom
= rcDest
.top
+ Height
;
471 rcDest
.top
+= StartScan
;
476 SourceSize
.cx
= bmiLocal
.bmiHeader
.bV5Width
;
477 SourceSize
.cy
= ScanLines
;
479 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmiLocal
.bmiHeader
.bV5BitCount
);
481 hSourceBitmap
= EngCreateBitmap(SourceSize
,
483 BitmapFormat(bmiLocal
.bmiHeader
.bV5BitCount
,
484 bmiLocal
.bmiHeader
.bV5Compression
),
485 bmiLocal
.bmiHeader
.bV5Height
< 0 ? BMF_TOPDOWN
: 0,
489 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
490 Status
= STATUS_NO_MEMORY
;
494 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
497 Status
= STATUS_UNSUCCESSFUL
;
503 /* Create a palette for the DIB */
504 hpalDIB
= BuildDIBPalette((PBITMAPINFO
)&bmiLocal
, (PINT
)&DIBPaletteType
);
507 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
508 Status
= STATUS_NO_MEMORY
;
512 /* Lock the DIB palette */
513 ppalDIB
= PALETTE_LockPalette(hpalDIB
);
516 SetLastWin32Error(ERROR_INVALID_HANDLE
);
517 Status
= STATUS_UNSUCCESSFUL
;
521 /* Initialize EXLATEOBJ */
522 EXLATEOBJ_vInitialize(&exlo
, ppalDIB
, pSurf
->ppal
, 0, 0, 0);
525 DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
526 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
527 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
528 Status
= IntEngBitBlt(pDestSurf
,
531 pDC
->rosdc
.CombinedClip
,
538 ROP3_TO_ROP4(SRCCOPY
));
540 /* Cleanup EXLATEOBJ */
541 EXLATEOBJ_vCleanup(&exlo
);
544 if (NT_SUCCESS(Status
))
549 if (ppalDIB
) PALETTE_UnlockPalette(ppalDIB
);
551 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
552 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
553 if (hpalDIB
) PALETTE_FreePaletteByHandle(hpalDIB
);
560 /* Converts a device-dependent bitmap to a DIB */
563 NtGdiGetDIBitsInternal(
575 SURFACE
*psurf
= NULL
;
576 HBITMAP hDestBitmap
= NULL
;
577 HPALETTE hDestPalette
= NULL
;
578 BITMAPV5INFO bmiLocal
;
579 PPALETTE ppalDst
= NULL
;
580 NTSTATUS Status
= STATUS_SUCCESS
;
582 BOOL bPaletteMatch
= FALSE
;
583 PBYTE ChkBits
= Bits
;
584 ULONG DestPaletteType
;
587 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
589 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
592 // if ScanLines == 0, no need to copy Bits.
598 Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, Info
, Usage
);
599 if (ChkBits
) ProbeForWrite(ChkBits
, MaxBits
, 1);
601 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
603 Status
= _SEH2_GetExceptionCode();
607 if (!NT_SUCCESS(Status
))
613 if (Dc
== NULL
) return 0;
614 if (Dc
->dctype
== DC_TYPE_INFO
)
620 /* Get a pointer to the source bitmap object */
621 psurf
= SURFACE_LockSurface(hBitmap
);
632 /* Copy palette information
633 * Always create a palette for 15 & 16 bit. */
634 if ((bmiLocal
.bmiHeader
.bV5BitCount
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) &&
635 bmiLocal
.bmiHeader
.bV5BitCount
!= 15 && bmiLocal
.bmiHeader
.bV5BitCount
!= 16) ||
638 ppalDst
= psurf
->ppal
;
639 bPaletteMatch
= TRUE
;
643 hDestPalette
= BuildDIBPalette((PBITMAPINFO
)&bmiLocal
, (PINT
)&DestPaletteType
);
644 ppalDst
= PALETTE_LockPalette(hDestPalette
);
647 DPRINT("ppalDst : %p\n", ppalDst
);
651 switch (bmiLocal
.bmiHeader
.bV5BitCount
)
656 bmiLocal
.bmiHeader
.bV5ClrUsed
= 0;
657 if (psurf
->hSecure
&&
658 BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bmiLocal
.bmiHeader
.bV5BitCount
)
660 if (Usage
== DIB_RGB_COLORS
)
662 if (ppalDst
->NumColors
!= 1 << bmiLocal
.bmiHeader
.bV5BitCount
)
663 bmiLocal
.bmiHeader
.bV5ClrUsed
= ppalDst
->NumColors
;
665 Index
< (1 << bmiLocal
.bmiHeader
.bV5BitCount
) && Index
< ppalDst
->NumColors
;
668 bmiLocal
.bmiColors
[Index
].rgbRed
= ppalDst
->IndexedColors
[Index
].peRed
;
669 bmiLocal
.bmiColors
[Index
].rgbGreen
= ppalDst
->IndexedColors
[Index
].peGreen
;
670 bmiLocal
.bmiColors
[Index
].rgbBlue
= ppalDst
->IndexedColors
[Index
].peBlue
;
671 bmiLocal
.bmiColors
[Index
].rgbReserved
= 0;
677 Index
< (1 << bmiLocal
.bmiHeader
.bV5BitCount
);
680 ((WORD
*)bmiLocal
.bmiColors
)[Index
] = Index
;
686 if (Usage
== DIB_PAL_COLORS
)
689 Index
< (1 << bmiLocal
.bmiHeader
.bV5BitCount
);
692 ((WORD
*)bmiLocal
.bmiColors
)[Index
] = (WORD
)Index
;
695 else if (bmiLocal
.bmiHeader
.bV5BitCount
> 1 && bPaletteMatch
)
698 Index
< (1 << bmiLocal
.bmiHeader
.bV5BitCount
) && Index
< ppalDst
->NumColors
;
701 bmiLocal
.bmiColors
[Index
].rgbRed
= ppalDst
->IndexedColors
[Index
].peRed
;
702 bmiLocal
.bmiColors
[Index
].rgbGreen
= ppalDst
->IndexedColors
[Index
].peGreen
;
703 bmiLocal
.bmiColors
[Index
].rgbBlue
= ppalDst
->IndexedColors
[Index
].peBlue
;
704 bmiLocal
.bmiColors
[Index
].rgbReserved
= 0;
709 switch (bmiLocal
.bmiHeader
.bV5BitCount
)
712 bmiLocal
.bmiColors
[0].rgbRed
=0 ;
713 bmiLocal
.bmiColors
[0].rgbGreen
= 0;
714 bmiLocal
.bmiColors
[0].rgbBlue
= 0;
715 bmiLocal
.bmiColors
[0].rgbReserved
= 0;
716 bmiLocal
.bmiColors
[1].rgbRed
=0xFF ;
717 bmiLocal
.bmiColors
[1].rgbGreen
= 0xFF;
718 bmiLocal
.bmiColors
[1].rgbBlue
= 0xFF;
719 bmiLocal
.bmiColors
[1].rgbReserved
= 0;
722 RtlCopyMemory(bmiLocal
.bmiColors
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
729 RtlCopyMemory(bmiLocal
.bmiColors
, DefLogPaletteQuads
, 10 * sizeof(RGBQUAD
));
730 RtlCopyMemory(bmiLocal
.bmiColors
+ 246, DefLogPaletteQuads
+ 10, 10 * sizeof(RGBQUAD
));
731 color
= bmiLocal
.bmiColors
+ 10;
732 for (r
= 0; r
<= 5; r
++) /* FIXME */
733 for (g
= 0; g
<= 5; g
++)
734 for (b
= 0; b
<= 5; b
++)
736 color
->rgbRed
= (r
* 0xff) / 5;
737 color
->rgbGreen
= (g
* 0xff) / 5;
738 color
->rgbBlue
= (b
* 0xff) / 5;
739 color
->rgbReserved
= 0;
749 if (bmiLocal
.bmiHeader
.bV5Compression
== BI_BITFIELDS
)
751 bmiLocal
.bmiHeader
.bV5RedMask
= 0x7c00;
752 bmiLocal
.bmiHeader
.bV5GreenMask
= 0x03e0;
753 bmiLocal
.bmiHeader
.bV5BlueMask
= 0x001f;
758 if (bmiLocal
.bmiHeader
.bV5Compression
== BI_BITFIELDS
)
760 bmiLocal
.bmiHeader
.bV5RedMask
= 0xf800;
761 bmiLocal
.bmiHeader
.bV5GreenMask
= 0x07e0;
762 bmiLocal
.bmiHeader
.bV5BlueMask
= 0x001f;
768 if (bmiLocal
.bmiHeader
.bV5Compression
== BI_BITFIELDS
)
770 bmiLocal
.bmiHeader
.bV5RedMask
= 0xff0000;
771 bmiLocal
.bmiHeader
.bV5GreenMask
= 0x00ff00;
772 bmiLocal
.bmiHeader
.bV5BlueMask
= 0x0000ff;
777 /* fill out the BITMAPINFO struct */
780 bmiLocal
.bmiHeader
.bV5Width
= psurf
->SurfObj
.sizlBitmap
.cx
;
781 /* Report negative height for top-down bitmaps. */
782 if (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
)
783 bmiLocal
.bmiHeader
.bV5Height
= - psurf
->SurfObj
.sizlBitmap
.cy
;
785 bmiLocal
.bmiHeader
.bV5Height
= psurf
->SurfObj
.sizlBitmap
.cy
;
786 bmiLocal
.bmiHeader
.bV5Planes
= 1;
787 bmiLocal
.bmiHeader
.bV5BitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
788 switch (psurf
->SurfObj
.iBitmapFormat
)
790 /* FIXME: What about BI_BITFIELDS? */
797 bmiLocal
.bmiHeader
.bV5Compression
= BI_RGB
;
800 bmiLocal
.bmiHeader
.bV5Compression
= BI_RLE4
;
803 bmiLocal
.bmiHeader
.bV5Compression
= BI_RLE8
;
806 bmiLocal
.bmiHeader
.bV5Compression
= BI_JPEG
;
809 bmiLocal
.bmiHeader
.bV5Compression
= BI_PNG
;
813 bmiLocal
.bmiHeader
.bV5SizeImage
= psurf
->SurfObj
.cjBits
;
814 bmiLocal
.bmiHeader
.bV5XPelsPerMeter
= psurf
->sizlDim
.cx
; /* FIXME */
815 bmiLocal
.bmiHeader
.bV5YPelsPerMeter
= psurf
->sizlDim
.cy
; /* FIXME */
816 bmiLocal
.bmiHeader
.bV5ClrUsed
= 0;
817 bmiLocal
.bmiHeader
.bV5ClrImportant
= 1 << bmiLocal
.bmiHeader
.bV5BitCount
; /* FIXME */
818 Result
= psurf
->SurfObj
.sizlBitmap
.cy
;
826 // If we have a good dib pointer, why not just copy bits from there w/o XLATE'ing them.
828 /* Create the destination bitmap too for the copy operation */
829 if (StartScan
> psurf
->SurfObj
.sizlBitmap
.cy
)
835 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
836 DestSize
.cx
= psurf
->SurfObj
.sizlBitmap
.cx
;
837 DestSize
.cy
= ScanLines
;
841 bmiLocal
.bmiHeader
.bV5SizeImage
= DIB_GetDIBWidthBytes(DestSize
.cx
,
842 bmiLocal
.bmiHeader
.bV5BitCount
) * DestSize
.cy
;
844 hDestBitmap
= EngCreateBitmap(DestSize
,
845 DIB_GetDIBWidthBytes(DestSize
.cx
,
846 bmiLocal
.bmiHeader
.bV5BitCount
),
847 BitmapFormat(bmiLocal
.bmiHeader
.bV5BitCount
,
848 bmiLocal
.bmiHeader
.bV5Compression
),
849 bmiLocal
.bmiHeader
.bV5Height
> 0 ? 0 : BMF_TOPDOWN
,
852 if (hDestBitmap
== NULL
)
856 if (NT_SUCCESS(Status
))
859 SURFOBJ
*DestSurfObj
;
862 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, ppalDst
, 0, 0, 0);
865 SourcePoint
.y
= psurf
->SurfObj
.sizlBitmap
.cy
- (StartScan
+ ScanLines
);
867 /* Determine destination rectangle */
870 DestRect
.right
= DestSize
.cx
;
871 DestRect
.bottom
= DestSize
.cy
;
873 DestSurfObj
= EngLockSurface((HSURF
)hDestBitmap
);
875 if (IntEngCopyBits(DestSurfObj
,
882 DPRINT("GetDIBits %d \n",abs(bmiLocal
.bmiHeader
.bV5Height
) - StartScan
);
886 EXLATEOBJ_vCleanup(&exlo
);
887 EngUnlockSurface(DestSurfObj
);
891 /* Now that everything is over, get back the information to caller */
894 /* Note : Info has already been probed */
895 GetBMIFromBitmapV5Info(&bmiLocal
, Info
, Usage
);
897 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
899 /* FIXME: fail or something */
905 if (hDestBitmap
!= NULL
)
906 EngDeleteSurface((HSURF
)hDestBitmap
);
908 if (hDestPalette
!= NULL
&& !bPaletteMatch
)
910 PALETTE_UnlockPalette(ppalDst
);
911 PALETTE_FreePaletteByHandle(hDestPalette
);
914 SURFACE_UnlockSurface(psurf
);
916 DPRINT("leaving NtGdiGetDIBitsInternal\n");
923 NtGdiStretchDIBitsInternal(
934 LPBITMAPINFO BitsInfo
,
941 HBITMAP hBitmap
, hOldBitmap
= NULL
;
943 HPALETTE hPal
= NULL
;
947 if (!Bits
|| !BitsInfo
)
949 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
955 ProbeForRead(BitsInfo
, cjMaxInfo
, 1);
956 ProbeForRead(Bits
, cjMaxBits
, 1);
958 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
966 DPRINT1("NtGdiStretchDIBitsInternal fail to read BitMapInfo: %x or Bits: %x\n",BitsInfo
,Bits
);
970 hdcMem
= NtGdiCreateCompatibleDC(hDC
);
973 DPRINT1("NtGdiCreateCompatibleDC fail create hdc\n");
977 hBitmap
= NtGdiCreateCompatibleBitmap(hDC
,
978 abs(BitsInfo
->bmiHeader
.biWidth
),
979 abs(BitsInfo
->bmiHeader
.biHeight
));
982 DPRINT1("NtGdiCreateCompatibleBitmap fail create bitmap\n");
983 DPRINT1("hDC : 0x%08x \n", hDC
);
984 DPRINT1("BitsInfo->bmiHeader.biWidth : 0x%08x \n", BitsInfo
->bmiHeader
.biWidth
);
985 DPRINT1("BitsInfo->bmiHeader.biHeight : 0x%08x \n", BitsInfo
->bmiHeader
.biHeight
);
989 /* Select the bitmap into hdcMem, and save a handle to the old bitmap */
990 hOldBitmap
= NtGdiSelectBitmap(hdcMem
, hBitmap
);
992 if (Usage
== DIB_PAL_COLORS
)
994 hPal
= NtGdiGetDCObject(hDC
, GDI_OBJECT_TYPE_PALETTE
);
995 hPal
= GdiSelectPalette(hdcMem
, hPal
, FALSE
);
998 if (BitsInfo
->bmiHeader
.biCompression
== BI_RLE4
||
999 BitsInfo
->bmiHeader
.biCompression
== BI_RLE8
)
1001 /* copy existing bitmap from destination dc */
1002 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
1003 NtGdiBitBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
1004 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, ROP
, 0, 0);
1006 NtGdiStretchBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
1007 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
1011 pDC
= DC_LockDc(hdcMem
);
1014 /* Note BitsInfo->bmiHeader.biHeight is the number of scanline,
1015 * if it negitve we getting to many scanline for scanline is UINT not
1016 * a INT, so we need make the negtive value to positve and that make the
1017 * count correct for negtive bitmap, TODO : we need testcase for this api */
1018 IntSetDIBits(pDC
, hBitmap
, 0, abs(BitsInfo
->bmiHeader
.biHeight
), Bits
,
1025 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
1026 left (negative biHeight) */
1027 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
1028 NtGdiBitBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
1029 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
1032 NtGdiStretchBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
1033 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
1034 SrcWidth
, SrcHeight
, ROP
, 0);
1038 GdiSelectPalette(hdcMem
, hPal
, FALSE
);
1041 NtGdiSelectBitmap(hdcMem
, hOldBitmap
);
1043 NtGdiDeleteObjectApp(hdcMem
);
1045 GreDeleteObject(hBitmap
);
1066 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1067 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1069 if (bpp
!= 1) fColor
= TRUE
;
1070 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1073 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
1075 const RGBQUAD
*rgb
= data
->bmiColors
;
1076 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1078 // Check if the first color of the colormap is black
1079 if ((col
== RGB(0, 0, 0)))
1082 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1084 // If the second color is white, create a monochrome bitmap
1085 fColor
= (col
!= RGB(0xff,0xff,0xff));
1089 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1091 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
1092 DWORD col
= RGB(rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
1094 if ((col
== RGB(0,0,0)))
1097 col
= RGB(rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
1098 fColor
= (col
!= RGB(0xff,0xff,0xff));
1104 DPRINT("(%ld): wrong size for data\n", data
->bmiHeader
.biSize
);
1109 // Now create the bitmap
1112 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
1116 handle
= GreCreateBitmap(width
,
1126 if (NULL
!= handle
&& CBM_INIT
== init
)
1128 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
1134 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1135 // The DDB that is created will be whatever bit depth your reference DC is
1138 NtGdiCreateDIBitmapInternal(
1143 IN OPTIONAL LPBYTE pjInit
,
1144 IN OPTIONAL LPBITMAPINFO pbmi
,
1146 IN UINT cjMaxInitInfo
,
1155 if (!hDc
) // CreateBitmap
1156 { // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1157 hDc
= IntGdiCreateDC(NULL
, NULL
, NULL
, NULL
,FALSE
);
1160 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1164 Dc
= DC_LockDc(hDc
);
1167 NtGdiDeleteObjectApp(hDc
);
1168 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1172 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
, iUsage
);
1175 NtGdiDeleteObjectApp(hDc
);
1177 else // CreateCompatibleBitmap
1179 Dc
= DC_LockDc(hDc
);
1182 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1186 First create an un-initialised bitmap. The depth of the bitmap
1187 should match that of the hdc and not that supplied in bmih.
1190 bpp
= pbmi
->bmiHeader
.biBitCount
;
1193 if (Dc
->dctype
!= DC_TYPE_MEMORY
)
1194 bpp
= Dc
->ppdev
->gdiinfo
.cBitsPixel
;
1199 SURFACE
*psurf
= Dc
->dclevel
.pSurface
;
1200 Count
= BITMAP_GetObject(psurf
, sizeof(dibs
), &dibs
);
1205 if (Count
== sizeof(BITMAP
))
1206 /* A device-dependent bitmap is selected in the DC */
1207 bpp
= dibs
.dsBm
.bmBitsPixel
;
1209 /* A DIB section is selected in the DC */
1210 bpp
= dibs
.dsBmih
.biBitCount
;
1214 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
, iUsage
);
1223 NtGdiCreateDIBSection(
1225 IN OPTIONAL HANDLE hSection
,
1227 IN LPBITMAPINFO bmi
,
1231 IN ULONG_PTR dwColorSpace
,
1234 HBITMAP hbitmap
= 0;
1236 BOOL bDesktopDC
= FALSE
;
1238 if (!bmi
) return hbitmap
; // Make sure.
1240 // If the reference hdc is null, take the desktop dc
1243 hDC
= NtGdiCreateCompatibleDC(0);
1247 if ((dc
= DC_LockDc(hDC
)))
1249 hbitmap
= DIB_CreateDIBSection(dc
,
1260 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1264 NtGdiDeleteObjectApp(hDC
);
1271 DIB_CreateDIBSection(
1281 SURFACE
*bmp
= NULL
;
1282 void *mapBits
= NULL
;
1286 // Fill BITMAP32 structure with DIB data
1287 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1294 DWORD dsBitfields
[3] = {0};
1297 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1298 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1299 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1301 /* CreateDIBSection should fail for compressed formats */
1302 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1304 return (HBITMAP
)NULL
;
1307 pdcattr
= dc
->pdcattr
;
1309 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1311 bm
.bmWidth
= bi
->biWidth
;
1312 bm
.bmHeight
= effHeight
;
1313 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
1315 bm
.bmPlanes
= bi
->biPlanes
;
1316 bm
.bmBitsPixel
= bi
->biBitCount
;
1319 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1320 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1321 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
1322 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1326 SYSTEM_BASIC_INFORMATION Sbi
;
1329 LARGE_INTEGER SectionOffset
;
1332 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1336 if (!NT_SUCCESS(Status
))
1341 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1342 mapSize
= bi
->biSizeImage
+ (offset
- mapOffset
);
1344 SectionOffset
.LowPart
= mapOffset
;
1345 SectionOffset
.HighPart
= 0;
1347 Status
= ZwMapViewOfSection(section
,
1357 if (!NT_SUCCESS(Status
))
1359 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1363 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1365 else if (ovr_pitch
&& offset
)
1366 bm
.bmBits
= (LPVOID
) offset
;
1370 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1373 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1374 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1376 if (usage
== DIB_PAL_COLORS
)
1378 lpRGB
= DIB_MapPaletteColors(dc
, bmi
);
1379 ColorCount
= bi
->biClrUsed
;
1380 if (ColorCount
== 0)
1382 ColorCount
= 1 << bi
->biBitCount
;
1387 lpRGB
= bmi
->bmiColors
;
1388 ColorCount
= 1 << bi
->biBitCount
;
1391 /* Set dsBitfields values */
1392 if (usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
1394 dsBitfields
[0] = dsBitfields
[1] = dsBitfields
[2] = 0;
1396 else if (bi
->biCompression
== BI_RGB
)
1398 switch (bi
->biBitCount
)
1401 dsBitfields
[0] = 0x7c00;
1402 dsBitfields
[1] = 0x03e0;
1403 dsBitfields
[2] = 0x001f;
1407 dsBitfields
[0] = 0xF800;
1408 dsBitfields
[1] = 0x07e0;
1409 dsBitfields
[2] = 0x001f;
1414 dsBitfields
[0] = 0xff0000;
1415 dsBitfields
[1] = 0x00ff00;
1416 dsBitfields
[2] = 0x0000ff;
1422 dsBitfields
[0] = ((DWORD
*)bmi
->bmiColors
)[0];
1423 dsBitfields
[1] = ((DWORD
*)bmi
->bmiColors
)[1];
1424 dsBitfields
[2] = ((DWORD
*)bmi
->bmiColors
)[2];
1427 // Create Device Dependent Bitmap and add DIB pointer
1428 Size
.cx
= bm
.bmWidth
;
1429 Size
.cy
= abs(bm
.bmHeight
);
1430 res
= GreCreateBitmapEx(bm
.bmWidth
,
1433 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1434 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1435 (bi
->biHeight
< 0 ? BMF_TOPDOWN
: 0),
1440 if (lpRGB
!= bmi
->bmiColors
)
1442 ExFreePoolWithTag(lpRGB
, TAG_COLORMAP
);
1444 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
1447 bmp
= SURFACE_LockSurface(res
);
1450 if (lpRGB
!= bmi
->bmiColors
)
1452 ExFreePoolWithTag(lpRGB
, TAG_COLORMAP
);
1454 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1455 GreDeleteObject(res
);
1459 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1460 table between the DIB and the X physical device. Obviously,
1461 this is left out of the ReactOS implementation. Instead,
1462 we call NtGdiSetDIBColorTable. */
1463 if (bi
->biBitCount
<= 8)
1465 bi
->biClrUsed
= 1 << bi
->biBitCount
;
1472 bmp
->hDIBSection
= section
;
1473 bmp
->hSecure
= hSecure
;
1474 bmp
->dwOffset
= offset
;
1475 bmp
->flags
= API_BITMAP
;
1476 bmp
->dsBitfields
[0] = dsBitfields
[0];
1477 bmp
->dsBitfields
[1] = dsBitfields
[1];
1478 bmp
->dsBitfields
[2] = dsBitfields
[2];
1479 bmp
->biClrUsed
= bi
->biClrUsed
;
1480 bmp
->biClrImportant
= bi
->biClrImportant
;
1482 if (bi
->biClrUsed
!= 0)
1484 hpal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, lpRGB
);
1488 hpal
= PALETTE_AllocPalette(PAL_BITFIELDS
, 0, NULL
,
1494 bmp
->ppal
= PALETTE_ShareLockPalette(hpal
);
1495 /* Lazy delete hpal, it will be freed at surface release */
1496 GreDeleteObject(hpal
);
1498 // Clean up in case of errors
1499 if (!res
|| !bmp
|| !bm
.bmBits
)
1501 DPRINT("got an error res=%08x, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1504 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1507 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1512 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1520 SURFACE_FreeSurfaceByHandle(res
);
1525 if (lpRGB
!= bmi
->bmiColors
)
1527 ExFreePoolWithTag(lpRGB
, TAG_COLORMAP
);
1532 SURFACE_UnlockSurface(bmp
);
1535 // Return BITMAP handle and storage location
1536 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1541 if (res
) pdcattr
->ulDirty_
|= DC_DIBSECTION
;
1546 /***********************************************************************
1547 * DIB_GetDIBWidthBytes
1549 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1550 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
1551 * 11/16/1999 (RJJ) lifted from wine
1553 INT FASTCALL
DIB_GetDIBWidthBytes(INT width
, INT depth
)
1555 return ((width
* depth
+ 31) & ~31) >> 3;
1558 /***********************************************************************
1559 * DIB_GetDIBImageBytes
1561 * Return the number of bytes used to hold the image in a DIB bitmap.
1562 * 11/16/1999 (RJJ) lifted from wine
1565 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1567 return DIB_GetDIBWidthBytes(width
, depth
) * (height
< 0 ? -height
: height
);
1570 /***********************************************************************
1571 * DIB_BitmapInfoSize
1573 * Return the size of the bitmap info structure including color table.
1574 * 11/16/1999 (RJJ) lifted from wine
1577 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1581 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1583 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
1584 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1585 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1587 else /* assume BITMAPINFOHEADER */
1589 colors
= info
->bmiHeader
.biClrUsed
;
1590 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
1591 return info
->bmiHeader
.biSize
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1597 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1604 palGDI
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
1611 if (palGDI
->Mode
!= PAL_INDEXED
)
1613 PALETTE_UnlockPalette(palGDI
);
1617 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1618 if (lpbmi
->bmiHeader
.biClrUsed
)
1620 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1623 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1626 PALETTE_UnlockPalette(palGDI
);
1630 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1632 for (i
= 0; i
< nNumColors
; i
++)
1634 if (*lpIndex
< palGDI
->NumColors
)
1636 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1637 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1638 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1642 lpRGB
[i
].rgbRed
= 0;
1643 lpRGB
[i
].rgbGreen
= 0;
1644 lpRGB
[i
].rgbBlue
= 0;
1646 lpRGB
[i
].rgbReserved
= 0;
1649 PALETTE_UnlockPalette(palGDI
);
1656 BuildDIBPalette(CONST BITMAPINFO
*bmi
, PINT paletteType
)
1661 ULONG RedMask
, GreenMask
, BlueMask
;
1662 PDWORD pdwColors
= (PDWORD
)((PBYTE
)bmi
+ bmi
->bmiHeader
.biSize
);
1664 // Determine Bits Per Pixel
1665 bits
= bmi
->bmiHeader
.biBitCount
;
1667 // Determine paletteType from Bits Per Pixel
1670 *paletteType
= PAL_INDEXED
;
1671 RedMask
= GreenMask
= BlueMask
= 0;
1673 else if (bmi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
1675 *paletteType
= PAL_BITFIELDS
;
1676 RedMask
= pdwColors
[0];
1677 GreenMask
= pdwColors
[1];
1678 BlueMask
= pdwColors
[2];
1680 else if (bits
== 15)
1682 *paletteType
= PAL_BITFIELDS
;
1687 else if (bits
== 16)
1689 *paletteType
= PAL_BITFIELDS
;
1696 *paletteType
= PAL_BGR
;
1698 GreenMask
= 0x00ff00;
1699 BlueMask
= 0x0000ff;
1702 if (bmi
->bmiHeader
.biClrUsed
== 0)
1704 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1708 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1711 if (PAL_INDEXED
== *paletteType
)
1713 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)pdwColors
);
1717 hPal
= PALETTE_AllocPalette(*paletteType
, ColorCount
,
1719 RedMask
, GreenMask
, BlueMask
);
1727 GetBMIColor(CONST BITMAPINFO
* pbmi
, INT i
)
1731 if(pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1733 /* BITMAPCOREINFO holds RGBTRIPLE values */
1734 size
= sizeof(RGBTRIPLE
);
1738 size
= sizeof(RGBQUAD
);
1740 memcpy(&dwRet
, (PBYTE
)pbmi
+ pbmi
->bmiHeader
.biSize
+ i
*size
, size
);
1746 SetBMIColor(CONST BITMAPINFO
* pbmi
, DWORD
* color
, INT i
)
1748 if(pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1750 *(RGBTRIPLE
*)((PBYTE
)pbmi
+ pbmi
->bmiHeader
.biSize
+ i
*sizeof(RGBTRIPLE
)) = *(RGBTRIPLE
*)color
;
1754 *(RGBQUAD
*)((PBYTE
)pbmi
+ pbmi
->bmiHeader
.biSize
+ i
*sizeof(RGBQUAD
)) = *(RGBQUAD
*)color
;
1760 ProbeAndConvertToBitmapV5Info(
1761 OUT PBITMAPV5INFO pbmiDst
,
1762 IN CONST BITMAPINFO
* pbmiUnsafe
,
1763 IN DWORD dwColorUse
)
1767 PBITMAPV5HEADER pbmhDst
= &pbmiDst
->bmiHeader
;
1769 /* Get the size and probe */
1770 ProbeForRead(&pbmiUnsafe
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1771 dwSize
= pbmiUnsafe
->bmiHeader
.biSize
;
1772 ProbeForRead(pbmiUnsafe
, dwSize
, 1);
1774 /* Check the size */
1775 // FIXME: are intermediate sizes allowed? As what are they interpreted?
1776 // make sure we don't use a too big dwSize later
1777 if (dwSize
!= sizeof(BITMAPCOREHEADER
) &&
1778 dwSize
!= sizeof(BITMAPINFOHEADER
) &&
1779 dwSize
!= sizeof(BITMAPV4HEADER
) &&
1780 dwSize
!= sizeof(BITMAPV5HEADER
))
1782 return STATUS_INVALID_PARAMETER
;
1785 if (dwSize
== sizeof(BITMAPCOREHEADER
))
1787 PBITMAPCOREHEADER pbch
= (PBITMAPCOREHEADER
)pbmiUnsafe
;
1789 /* Manually copy the fields that are present */
1790 pbmhDst
->bV5Width
= pbch
->bcWidth
;
1791 pbmhDst
->bV5Height
= pbch
->bcHeight
;
1792 pbmhDst
->bV5Planes
= pbch
->bcPlanes
;
1793 pbmhDst
->bV5BitCount
= pbch
->bcBitCount
;
1795 /* Set some default values */
1796 pbmhDst
->bV5Compression
= BI_RGB
;
1797 pbmhDst
->bV5SizeImage
= 0;
1798 pbmhDst
->bV5XPelsPerMeter
= 72;
1799 pbmhDst
->bV5YPelsPerMeter
= 72;
1800 pbmhDst
->bV5ClrUsed
= 0;
1801 pbmhDst
->bV5ClrImportant
= 0;
1805 /* Copy valid fields */
1806 memcpy(pbmiDst
, pbmiUnsafe
, dwSize
);
1808 if(dwSize
< sizeof(BITMAPV5HEADER
))
1810 /* Zero out the rest of the V5 header */
1811 memset((char*)pbmiDst
+ dwSize
, 0, sizeof(BITMAPV5HEADER
) - dwSize
);
1814 pbmhDst
->bV5Size
= sizeof(BITMAPV5HEADER
);
1817 if (dwSize
< sizeof(BITMAPV4HEADER
))
1819 if (pbmhDst
->bV5Compression
== BI_BITFIELDS
)
1821 pbmhDst
->bV5RedMask
= GetBMIColor(pbmiUnsafe
, 0);
1822 pbmhDst
->bV5GreenMask
= GetBMIColor(pbmiUnsafe
, 1);
1823 pbmhDst
->bV5BlueMask
= GetBMIColor(pbmiUnsafe
, 2);
1824 pbmhDst
->bV5AlphaMask
= 0;
1825 pbmhDst
->bV5ClrUsed
= 0;
1828 // pbmhDst->bV5CSType;
1829 // pbmhDst->bV5Endpoints;
1830 // pbmhDst->bV5GammaRed;
1831 // pbmhDst->bV5GammaGreen;
1832 // pbmhDst->bV5GammaBlue;
1835 if (dwSize
< sizeof(BITMAPV5HEADER
))
1837 // pbmhDst->bV5Intent;
1838 // pbmhDst->bV5ProfileData;
1839 // pbmhDst->bV5ProfileSize;
1840 // pbmhDst->bV5Reserved;
1843 ulWidthBytes
= ((pbmhDst
->bV5Width
* pbmhDst
->bV5Planes
*
1844 pbmhDst
->bV5BitCount
+ 31) & ~31) / 8;
1846 if (pbmhDst
->bV5SizeImage
== 0)
1847 pbmhDst
->bV5SizeImage
= abs(ulWidthBytes
* pbmhDst
->bV5Height
);
1849 if (pbmhDst
->bV5ClrUsed
== 0)
1851 switch(pbmhDst
->bV5BitCount
)
1854 pbmhDst
->bV5ClrUsed
= 2;
1857 pbmhDst
->bV5ClrUsed
= 16;
1860 pbmhDst
->bV5ClrUsed
= 256;
1863 pbmhDst
->bV5ClrUsed
= 0;
1868 if (pbmhDst
->bV5Planes
!= 1)
1870 return STATUS_INVALID_PARAMETER
;
1873 if (pbmhDst
->bV5BitCount
!= 0 && pbmhDst
->bV5BitCount
!= 1 &&
1874 pbmhDst
->bV5BitCount
!= 4 && pbmhDst
->bV5BitCount
!= 8 &&
1875 pbmhDst
->bV5BitCount
!= 16 && pbmhDst
->bV5BitCount
!= 24 &&
1876 pbmhDst
->bV5BitCount
!= 32)
1878 DPRINT("Invalid bit count: %d\n", pbmhDst
->bV5BitCount
);
1879 return STATUS_INVALID_PARAMETER
;
1882 if ((pbmhDst
->bV5BitCount
== 0 &&
1883 pbmhDst
->bV5Compression
!= BI_JPEG
&& pbmhDst
->bV5Compression
!= BI_PNG
))
1885 DPRINT("Bit count 0 is invalid for compression %d.\n", pbmhDst
->bV5Compression
);
1886 return STATUS_INVALID_PARAMETER
;
1889 if (pbmhDst
->bV5Compression
== BI_BITFIELDS
&&
1890 pbmhDst
->bV5BitCount
!= 16 && pbmhDst
->bV5BitCount
!= 32)
1892 DPRINT("Bit count %d is invalid for compression BI_BITFIELDS.\n", pbmhDst
->bV5BitCount
);
1893 return STATUS_INVALID_PARAMETER
;
1897 if(pbmhDst
->bV5ClrUsed
)
1900 if(dwColorUse
== DIB_PAL_COLORS
)
1902 RtlCopyMemory(pbmiDst
->bmiColors
,
1903 pbmiUnsafe
->bmiColors
,
1904 pbmhDst
->bV5ClrUsed
* sizeof(WORD
));
1908 for(i
= 0; i
< pbmhDst
->bV5ClrUsed
; i
++)
1910 ((DWORD
*)pbmiDst
->bmiColors
)[i
] = GetBMIColor(pbmiUnsafe
, i
);
1915 return STATUS_SUCCESS
;
1920 GetBMIFromBitmapV5Info(IN PBITMAPV5INFO pbmiSrc
,
1921 OUT PBITMAPINFO pbmiDst
,
1922 IN DWORD dwColorUse
)
1924 if(pbmiDst
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1926 /* Manually set value */
1927 BITMAPCOREHEADER
* pbmhCore
= (BITMAPCOREHEADER
*)&pbmiDst
->bmiHeader
;
1928 pbmhCore
->bcWidth
= pbmiSrc
->bmiHeader
.bV5Width
;
1929 pbmhCore
->bcHeight
= pbmiSrc
->bmiHeader
.bV5Height
;
1930 pbmhCore
->bcPlanes
= pbmiSrc
->bmiHeader
.bV5Planes
;
1931 pbmhCore
->bcBitCount
= pbmiSrc
->bmiHeader
.bV5BitCount
;
1935 /* Copy valid Fields, keep bmiHeader.biSize safe */
1936 RtlCopyMemory((PBYTE
)pbmiDst
+ sizeof(DWORD
), pbmiSrc
, pbmiDst
->bmiHeader
.biSize
- sizeof(DWORD
));
1938 if((pbmiDst
->bmiHeader
.biSize
< sizeof(BITMAPV4HEADER
)) &&
1939 (pbmiSrc
->bmiHeader
.bV5Compression
== BI_BITFIELDS
))
1941 /* Masks are already set in V4 and V5 headers */
1942 SetBMIColor(pbmiDst
, &pbmiSrc
->bmiHeader
.bV5RedMask
, 0);
1943 SetBMIColor(pbmiDst
, &pbmiSrc
->bmiHeader
.bV5GreenMask
, 1);
1944 SetBMIColor(pbmiDst
, &pbmiSrc
->bmiHeader
.bV5BlueMask
, 2);
1949 if(dwColorUse
== DIB_PAL_COLORS
)
1951 RtlCopyMemory(pbmiDst
->bmiColors
,
1953 pbmiSrc
->bmiHeader
.bV5ClrUsed
* sizeof(WORD
));
1957 for(i
= 0; i
< pbmiSrc
->bmiHeader
.bV5ClrUsed
; i
++)
1959 SetBMIColor(pbmiDst
, (DWORD
*)pbmiSrc
->bmiColors
+ i
, i
);