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 RGBTRIPLE EGAColorsTriples
[16] = {
46 /* rgbBlue, rgbGreen, rgbRed */
65 static const RGBQUAD DefLogPaletteQuads
[20] = { /* Copy of Default Logical Palette */
66 /* rgbBlue, rgbGreen, rgbRed, rgbReserved */
67 { 0x00, 0x00, 0x00, 0x00 },
68 { 0x00, 0x00, 0x80, 0x00 },
69 { 0x00, 0x80, 0x00, 0x00 },
70 { 0x00, 0x80, 0x80, 0x00 },
71 { 0x80, 0x00, 0x00, 0x00 },
72 { 0x80, 0x00, 0x80, 0x00 },
73 { 0x80, 0x80, 0x00, 0x00 },
74 { 0xc0, 0xc0, 0xc0, 0x00 },
75 { 0xc0, 0xdc, 0xc0, 0x00 },
76 { 0xf0, 0xca, 0xa6, 0x00 },
77 { 0xf0, 0xfb, 0xff, 0x00 },
78 { 0xa4, 0xa0, 0xa0, 0x00 },
79 { 0x80, 0x80, 0x80, 0x00 },
80 { 0x00, 0x00, 0xf0, 0x00 },
81 { 0x00, 0xff, 0x00, 0x00 },
82 { 0x00, 0xff, 0xff, 0x00 },
83 { 0xff, 0x00, 0x00, 0x00 },
84 { 0xff, 0x00, 0xff, 0x00 },
85 { 0xff, 0xff, 0x00, 0x00 },
86 { 0xff, 0xff, 0xff, 0x00 }
89 static const RGBQUAD DefLogPaletteTriples
[20] = { /* Copy of Default Logical Palette */
90 /* rgbBlue, rgbGreen, rgbRed, rgbReserved */
100 { 0xf0, 0xca, 0xa6 },
101 { 0xf0, 0xfb, 0xff },
102 { 0xa4, 0xa0, 0xa0 },
103 { 0x80, 0x80, 0x80 },
104 { 0x00, 0x00, 0xf0 },
105 { 0x00, 0xff, 0x00 },
106 { 0x00, 0xff, 0xff },
107 { 0xff, 0x00, 0x00 },
108 { 0xff, 0x00, 0xff },
109 { 0xff, 0xff, 0x00 },
120 CONST RGBQUAD
*Colors
)
128 if (!(dc
= DC_LockDc(hDC
))) return 0;
129 if (dc
->dctype
== DC_TYPE_INFO
)
135 psurf
= dc
->dclevel
.pSurface
;
139 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
143 if (psurf
->hSecure
== NULL
)
146 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
150 biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
151 if (biBitCount
<= 8 && StartIndex
< (1 << biBitCount
))
153 if (StartIndex
+ Entries
> (1 << biBitCount
))
154 Entries
= (1 << biBitCount
) - StartIndex
;
156 if (psurf
->ppal
== NULL
)
159 SetLastWin32Error(ERROR_INVALID_HANDLE
);
163 PalGDI
= PALETTE_LockPalette(psurf
->ppal
->BaseObject
.hHmgr
);
165 for (Index
= StartIndex
;
166 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
169 PalGDI
->IndexedColors
[Index
].peRed
= Colors
[Index
- StartIndex
].rgbRed
;
170 PalGDI
->IndexedColors
[Index
].peGreen
= Colors
[Index
- StartIndex
].rgbGreen
;
171 PalGDI
->IndexedColors
[Index
].peBlue
= Colors
[Index
- StartIndex
].rgbBlue
;
173 PALETTE_UnlockPalette(PalGDI
);
178 /* Mark the brushes invalid */
179 dc
->pdcattr
->ulDirty_
|= DIRTY_FILL
|DIRTY_LINE
|DIRTY_BACKGROUND
|DIRTY_TEXT
;
197 UINT Index
, Count
= 0;
200 if (!(dc
= DC_LockDc(hDC
))) return 0;
201 if (dc
->dctype
== DC_TYPE_INFO
)
207 psurf
= dc
->dclevel
.pSurface
;
211 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
215 if (psurf
->hSecure
== NULL
)
218 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
222 biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
223 if (biBitCount
<= 8 &&
224 StartIndex
< (1 << biBitCount
))
226 if (StartIndex
+ Entries
> (1 << biBitCount
))
227 Entries
= (1 << biBitCount
) - StartIndex
;
229 if (psurf
->ppal
== NULL
)
232 SetLastWin32Error(ERROR_INVALID_HANDLE
);
236 PalGDI
= PALETTE_LockPalette(psurf
->ppal
->BaseObject
.hHmgr
);
238 for (Index
= StartIndex
;
239 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
242 Colors
[Index
- StartIndex
].rgbRed
= PalGDI
->IndexedColors
[Index
].peRed
;
243 Colors
[Index
- StartIndex
].rgbGreen
= PalGDI
->IndexedColors
[Index
].peGreen
;
244 Colors
[Index
- StartIndex
].rgbBlue
= PalGDI
->IndexedColors
[Index
].peBlue
;
245 Colors
[Index
- StartIndex
].rgbReserved
= 0;
248 PALETTE_UnlockPalette(PalGDI
);
256 // Converts a DIB to a device-dependent bitmap
265 CONST BITMAPV5INFO
*bmi
,
269 HBITMAP SourceBitmap
;
272 SURFOBJ
*DestSurf
, *SourceSurf
;
279 HPALETTE DIB_Palette
;
280 ULONG DIB_Palette_Type
;
284 if (!(bitmap
= SURFACE_LockSurface(hBitmap
)))
290 //if (ColorUse == DIB_PAL_COLORS)
291 // lpRGB = DIB_MapPaletteColors(hDC, bmi);
293 // lpRGB = &bmi->bmiColors;
295 DestSurf
= &bitmap
->SurfObj
;
297 // Create source surface
298 SourceSize
.cx
= bmi
->bmiHeader
.bV5Width
;
299 SourceSize
.cy
= ScanLines
;
301 // Determine width of DIB
302 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.bV5BitCount
);
304 SourceBitmap
= EngCreateBitmap(SourceSize
,
306 BitmapFormat(bmi
->bmiHeader
.bV5BitCount
, bmi
->bmiHeader
.bV5Compression
),
307 bmi
->bmiHeader
.bV5Height
< 0 ? BMF_TOPDOWN
: 0,
309 if (0 == SourceBitmap
)
311 SURFACE_UnlockSurface(bitmap
);
312 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
316 SourceSurf
= EngLockSurface((HSURF
)SourceBitmap
);
317 if (NULL
== SourceSurf
)
319 EngDeleteSurface((HSURF
)SourceBitmap
);
320 SURFACE_UnlockSurface(bitmap
);
321 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
325 ASSERT(bitmap
->ppal
);
327 // Source palette obtained from the BITMAPINFO
328 DIB_Palette
= BuildDIBPalette((BITMAPINFO
*)bmi
, (PINT
)&DIB_Palette_Type
);
329 if (NULL
== DIB_Palette
)
331 EngUnlockSurface(SourceSurf
);
332 EngDeleteSurface((HSURF
)SourceBitmap
);
333 SURFACE_UnlockSurface(bitmap
);
334 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
338 ppalDIB
= PALETTE_LockPalette(DIB_Palette
);
340 /* Initialize XLATEOBJ for color translation */
341 EXLATEOBJ_vInitialize(&exlo
, ppalDIB
, bitmap
->ppal
, 0, 0, 0);
347 // Determine destination rectangle
349 DestRect
.top
= abs(bmi
->bmiHeader
.bV5Height
) - StartScan
- ScanLines
;
350 DestRect
.right
= SourceSize
.cx
;
351 DestRect
.bottom
= DestRect
.top
+ ScanLines
;
353 copyBitsResult
= IntEngCopyBits(DestSurf
, SourceSurf
, NULL
, &exlo
.xlo
, &DestRect
, &ZeroPoint
);
355 // If it succeeded, return number of scanlines copies
356 if (copyBitsResult
== TRUE
)
358 result
= SourceSize
.cy
;
360 // result = abs(bmi->bmiHeader.biHeight) - StartScan;
364 EXLATEOBJ_vCleanup(&exlo
);
365 PALETTE_UnlockPalette(ppalDIB
);
366 PALETTE_FreePaletteByHandle(DIB_Palette
);
367 EngUnlockSurface(SourceSurf
);
368 EngDeleteSurface((HSURF
)SourceBitmap
);
370 // if (ColorUse == DIB_PAL_COLORS)
371 // WinFree((LPSTR)lpRGB);
373 SURFACE_UnlockSurface(bitmap
);
378 // FIXME by Removing NtGdiSetDIBits!!!
379 // This is a victim of the Win32k Initialization BUG!!!!!
380 // Converts a DIB to a device-dependent bitmap
389 CONST BITMAPINFO
*bmi
,
394 NTSTATUS Status
= STATUS_SUCCESS
;
395 BITMAPV5INFO bmiLocal
;
401 Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, bmi
, ColorUse
, 0);
402 ProbeForRead(Bits
, bmiLocal
.bmiHeader
.bV5SizeImage
, 1);
404 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
406 Status
= _SEH2_GetExceptionCode();
410 if (!NT_SUCCESS(Status
))
418 SetLastWin32Error(ERROR_INVALID_HANDLE
);
421 if (Dc
->dctype
== DC_TYPE_INFO
)
427 Ret
= IntSetDIBits(Dc
, hBitmap
, StartScan
, ScanLines
, Bits
, &bmiLocal
, ColorUse
);
437 NtGdiSetDIBitsToDeviceInternal(
452 IN BOOL bTransformCoordinates
,
453 IN OPTIONAL HANDLE hcmXform
)
456 NTSTATUS Status
= STATUS_SUCCESS
;
458 HBITMAP hSourceBitmap
= NULL
;
459 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
;
466 PPALETTE ppalDIB
= NULL
;
467 HPALETTE hpalDIB
= NULL
;
468 ULONG DIBPaletteType
;
469 BITMAPV5INFO bmiLocal
;
475 Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, bmi
, ColorUse
, cjMaxInfo
);
476 ProbeForRead(Bits
, cjMaxBits
, 1);
478 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
480 Status
= _SEH2_GetExceptionCode();
484 if (!NT_SUCCESS(Status
))
489 pDC
= DC_LockDc(hDC
);
492 SetLastWin32Error(ERROR_INVALID_HANDLE
);
495 if (pDC
->dctype
== DC_TYPE_INFO
)
501 pSurf
= pDC
->dclevel
.pSurface
;
503 pDestSurf
= pSurf
? &pSurf
->SurfObj
: NULL
;
505 ScanLines
= min(ScanLines
, abs(bmiLocal
.bmiHeader
.bV5Height
) - StartScan
);
509 if (bTransformCoordinates
)
511 CoordLPtoDP(pDC
, (LPPOINT
)&rcDest
);
513 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
514 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
515 rcDest
.right
= rcDest
.left
+ Width
;
516 rcDest
.bottom
= rcDest
.top
+ Height
;
517 rcDest
.top
+= StartScan
;
522 SourceSize
.cx
= bmiLocal
.bmiHeader
.bV5Width
;
523 SourceSize
.cy
= ScanLines
;
525 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmiLocal
.bmiHeader
.bV5BitCount
);
527 hSourceBitmap
= EngCreateBitmap(SourceSize
,
529 BitmapFormat(bmiLocal
.bmiHeader
.bV5BitCount
,
530 bmiLocal
.bmiHeader
.bV5Compression
),
531 bmiLocal
.bmiHeader
.bV5Height
< 0 ? BMF_TOPDOWN
: 0,
535 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
536 Status
= STATUS_NO_MEMORY
;
540 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
543 Status
= STATUS_UNSUCCESSFUL
;
549 /* Create a palette for the DIB */
550 hpalDIB
= BuildDIBPalette((PBITMAPINFO
)&bmiLocal
, (PINT
)&DIBPaletteType
);
553 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
554 Status
= STATUS_NO_MEMORY
;
558 /* Lock the DIB palette */
559 ppalDIB
= PALETTE_LockPalette(hpalDIB
);
562 SetLastWin32Error(ERROR_INVALID_HANDLE
);
563 Status
= STATUS_UNSUCCESSFUL
;
567 /* Initialize EXLATEOBJ */
568 EXLATEOBJ_vInitialize(&exlo
, ppalDIB
, pSurf
->ppal
, 0, 0, 0);
571 DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
572 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
573 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
574 Status
= IntEngBitBlt(pDestSurf
,
577 pDC
->rosdc
.CombinedClip
,
584 ROP3_TO_ROP4(SRCCOPY
));
586 /* Cleanup EXLATEOBJ */
587 EXLATEOBJ_vCleanup(&exlo
);
590 if (NT_SUCCESS(Status
))
595 if (ppalDIB
) PALETTE_UnlockPalette(ppalDIB
);
597 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
598 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
599 if (hpalDIB
) PALETTE_FreePaletteByHandle(hpalDIB
);
606 /* Converts a device-dependent bitmap to a DIB */
609 NtGdiGetDIBitsInternal(
620 BITMAPCOREINFO
* pbmci
= NULL
;
621 PSURFACE psurf
= NULL
;
627 RGBTRIPLE
* rgbTriples
;
630 NTSTATUS Status
= STATUS_SUCCESS
;
632 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
634 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
639 /* Probe for read and write */
640 ProbeForRead(Info
, MaxInfo
, 1);
641 ProbeForWrite(Info
, MaxInfo
, 1);
642 if (Bits
) ProbeForWrite(Bits
, MaxBits
, 1);
644 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
646 Status
= _SEH2_GetExceptionCode();
650 if (!NT_SUCCESS(Status
))
655 colorPtr
= (LPBYTE
)Info
+ Info
->bmiHeader
.biSize
;
656 rgbTriples
= colorPtr
;
659 bitmap_type
= DIB_GetBitmapInfo(&Info
->bmiHeader
,
666 if(bitmap_type
== -1)
668 DPRINT("Wrong bitmap format\n");
669 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
672 else if(bitmap_type
== 0)
674 /* We need a BITMAPINFO to create a DIB, but we have to fill
675 * the BITMAPCOREINFO we're provided */
676 pbmci
= (BITMAPCOREINFO
*)Info
;
677 Info
= DIB_ConvertBitmapInfo((BITMAPINFO
*)pbmci
, Usage
);
680 DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
683 rgbQuads
= Info
->bmiColors
;
686 pDC
= DC_LockDc(hDC
);
687 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
693 /* Get a pointer to the source bitmap object */
694 psurf
= SURFACE_LockSurface(hBitmap
);
700 /* Must not be selected */
701 if(psurf
->hdc
!= NULL
)
704 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
708 /* Fill in the structure */
711 case 0: /* Only info */
714 pbmci
->bmciHeader
.bcWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
715 pbmci
->bmciHeader
.bcHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
716 -psurf
->SurfObj
.sizlBitmap
.cy
:
717 psurf
->SurfObj
.sizlBitmap
.cy
;
718 pbmci
->bmciHeader
.bcPlanes
= 1;
719 pbmci
->bmciHeader
.bcBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
721 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
722 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
723 -psurf
->SurfObj
.sizlBitmap
.cy
:
724 psurf
->SurfObj
.sizlBitmap
.cy
;;
725 Info
->bmiHeader
.biPlanes
= 1;
726 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
727 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
728 Info
->bmiHeader
.biHeight
,
729 Info
->bmiHeader
.biBitCount
);
732 switch(Info
->bmiHeader
.biBitCount
)
736 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
739 Info
->bmiHeader
.biCompression
= BI_RGB
;
743 else if(Info
->bmiHeader
.biBitCount
> 8)
745 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
749 Info
->bmiHeader
.biCompression
= BI_RGB
;
751 Info
->bmiHeader
.biXPelsPerMeter
= 0;
752 Info
->bmiHeader
.biYPelsPerMeter
= 0;
753 Info
->bmiHeader
.biClrUsed
= 0;
754 Info
->bmiHeader
.biClrImportant
= 0;
755 ScanLines
= psurf
->SurfObj
.sizlBitmap
.cy
;
756 /* Get Complete info now */
762 Info
->bmiHeader
.biClrUsed
= 0;
764 /* If the bitmap if a DIB section and has the same format than what
765 * we're asked, go ahead! */
766 if((psurf
->hSecure
) &&
767 (BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bpp
))
769 if(Usage
== DIB_RGB_COLORS
)
771 unsigned int colors
= min(psurf
->ppal
->NumColors
, 1 << bpp
);
775 for(i
=0; i
< colors
; i
++)
777 rgbTriples
[i
].rgbtRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
778 rgbTriples
[i
].rgbtGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
779 rgbTriples
[i
].rgbtBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
782 if(colors
!= 1 << bpp
) Info
->bmiHeader
.biClrUsed
= colors
;
783 RtlCopyMemory(rgbQuads
, psurf
->ppal
->IndexedColors
, colors
* sizeof(RGBQUAD
));
787 for(i
=0; i
< 1 << bpp
; i
++)
789 if(pbmci
) ((WORD
*)rgbTriples
)[i
] = i
;
790 ((WORD
*)rgbQuads
)[i
] = i
;
796 if(Usage
== DIB_PAL_COLORS
)
798 for(i
=0; i
< 1 << bpp
; i
++)
800 if(pbmci
) ((WORD
*)rgbTriples
)[i
] = i
;
801 ((WORD
*)rgbQuads
)[i
] = i
;
804 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
)) {
805 /* For color DDBs in native depth (mono DDBs always have
806 a black/white palette):
807 Generate the color map from the selected palette */
808 PPALETTE pDcPal
= PALETTE_LockPalette(pDC
->dclevel
.hpal
);
814 for (i
= 0; i
< pDcPal
->NumColors
; i
++) {
817 rgbTriples
[i
].rgbtRed
= pDcPal
->IndexedColors
[i
].peRed
;
818 rgbTriples
[i
].rgbtGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
819 rgbTriples
[i
].rgbtBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
822 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
823 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
824 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
825 rgbQuads
[i
].rgbReserved
= 0;
827 PALETTE_UnlockPalette(pDcPal
);
833 rgbTriples
[0].rgbtRed
= rgbTriples
[0].rgbtGreen
=
834 rgbTriples
[0].rgbtBlue
= 0;
835 rgbTriples
[1].rgbtRed
= rgbTriples
[1].rgbtGreen
=
836 rgbTriples
[1].rgbtBlue
= 0xff;
838 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
=
839 rgbQuads
[0].rgbBlue
= 0;
840 rgbQuads
[0].rgbReserved
= 0;
841 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
=
842 rgbQuads
[1].rgbBlue
= 0xff;
843 rgbQuads
[1].rgbReserved
= 0;
848 RtlCopyMemory(rgbTriples
, EGAColorsTriples
, sizeof(EGAColorsTriples
));
849 RtlCopyMemory(rgbQuads
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
859 RGBTRIPLE
*colorTriple
;
861 RtlCopyMemory(rgbTriples
, DefLogPaletteTriples
,
862 10 * sizeof(RGBTRIPLE
));
863 RtlCopyMemory(rgbTriples
+ 246, DefLogPaletteTriples
+ 10,
864 10 * sizeof(RGBTRIPLE
));
865 colorTriple
= rgbTriples
+ 10;
866 for(r
= 0; r
<= 5; r
++) /* FIXME */
868 for(g
= 0; g
<= 5; g
++)
870 for(b
= 0; b
<= 5; b
++)
872 colorTriple
->rgbtRed
= (r
* 0xff) / 5;
873 colorTriple
->rgbtGreen
= (g
* 0xff) / 5;
874 colorTriple
->rgbtBlue
= (b
* 0xff) / 5;
880 memcpy(rgbQuads
, DefLogPaletteQuads
,
881 10 * sizeof(RGBQUAD
));
882 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10,
883 10 * sizeof(RGBQUAD
));
884 color
= rgbQuads
+ 10;
885 for(r
= 0; r
<= 5; r
++) /* FIXME */
887 for(g
= 0; g
<= 5; g
++)
889 for(b
= 0; b
<= 5; b
++)
891 color
->rgbRed
= (r
* 0xff) / 5;
892 color
->rgbGreen
= (g
* 0xff) / 5;
893 color
->rgbBlue
= (b
* 0xff) / 5;
894 color
->rgbReserved
= 0;
906 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
908 ((PDWORD
)Info
->bmiColors
)[0] = 0x7c00;
909 ((PDWORD
)Info
->bmiColors
)[1] = 0x03e0;
910 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
915 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
919 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
920 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
921 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
925 ((PDWORD
)Info
->bmiColors
)[0] = 0xf800;
926 ((PDWORD
)Info
->bmiColors
)[1] = 0x07e0;
927 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
934 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
938 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
939 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
940 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
944 ((PDWORD
)Info
->bmiColors
)[0] = 0xff0000;
945 ((PDWORD
)Info
->bmiColors
)[1] = 0x00ff00;
946 ((PDWORD
)Info
->bmiColors
)[2] = 0x0000ff;
952 if(Bits
&& ScanLines
)
954 /* Create a DIBSECTION, blt it, profit */
956 HBITMAP hBmpDest
, hOldDest
= NULL
, hOldSrc
= NULL
;
960 if (StartScan
> psurf
->SurfObj
.sizlBitmap
.cy
)
967 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
970 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
974 DPRINT1("Unable to create a DIB Section!\n");
975 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
980 hdcDest
= NtGdiCreateCompatibleDC(0);
981 hdcSrc
= NtGdiCreateCompatibleDC(0);
983 if(!(hdcSrc
&& hdcDest
))
985 DPRINT1("Error: could not create HDCs!\n");
990 hOldDest
= NtGdiSelectBitmap(hdcDest
, hBmpDest
);
991 hOldSrc
= NtGdiSelectBitmap(hdcSrc
, hBitmap
);
993 if(!(hOldDest
&& hOldSrc
))
995 DPRINT1("Error : Could not Select bitmaps\n");
999 ret
= GreStretchBltMask(hdcDest
,
1007 psurf
->SurfObj
.sizlBitmap
.cx
,
1019 Status
= STATUS_SUCCESS
;
1022 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, height
, bpp
));
1024 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1026 Status
= _SEH2_GetExceptionCode();
1030 if(!NT_SUCCESS(Status
))
1032 DPRINT1("Unable to copy bits to the user provided pointer\n");
1040 if(hOldSrc
) NtGdiSelectBitmap(hdcSrc
, hOldSrc
);
1041 NtGdiDeleteObjectApp(hdcSrc
);
1045 if(hOldDest
) NtGdiSelectBitmap(hdcDest
, hOldDest
);
1046 NtGdiDeleteObjectApp(hdcDest
);
1048 GreDeleteObject(hBmpDest
);
1053 if(pDC
) DC_UnlockDc(pDC
);
1054 if(psurf
) SURFACE_UnlockSurface(psurf
);
1055 if(pbmci
) DIB_FreeConvertedBitmapInfo(Info
, (BITMAPINFO
*)pbmci
);
1063 NtGdiStretchDIBitsInternal(
1074 LPBITMAPINFO BitsInfo
,
1081 HBITMAP hBitmap
, hOldBitmap
= NULL
;
1083 HPALETTE hPal
= NULL
;
1086 BITMAPV5INFO bmiLocal
;
1088 if (!Bits
|| !BitsInfo
)
1090 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1096 Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, BitsInfo
, Usage
, cjMaxInfo
);
1097 ProbeForRead(Bits
, cjMaxBits
, 1);
1099 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1101 Status
= _SEH2_GetExceptionCode();
1105 if (!NT_SUCCESS(Status
))
1107 DPRINT1("NtGdiStretchDIBitsInternal fail to read BitMapInfo: %x or Bits: %x\n",BitsInfo
,Bits
);
1111 hdcMem
= NtGdiCreateCompatibleDC(hDC
);
1114 DPRINT1("NtGdiCreateCompatibleDC fail create hdc\n");
1118 hBitmap
= NtGdiCreateCompatibleBitmap(hDC
,
1119 abs(bmiLocal
.bmiHeader
.bV5Width
),
1120 abs(bmiLocal
.bmiHeader
.bV5Height
));
1121 if (hBitmap
== NULL
)
1123 DPRINT1("NtGdiCreateCompatibleBitmap fail create bitmap\n");
1124 DPRINT1("hDC : 0x%08x \n", hDC
);
1125 DPRINT1("BitsInfo->bmiHeader.biWidth : 0x%08x \n", BitsInfo
->bmiHeader
.biWidth
);
1126 DPRINT1("BitsInfo->bmiHeader.biHeight : 0x%08x \n", BitsInfo
->bmiHeader
.biHeight
);
1130 /* Select the bitmap into hdcMem, and save a handle to the old bitmap */
1131 hOldBitmap
= NtGdiSelectBitmap(hdcMem
, hBitmap
);
1133 if (Usage
== DIB_PAL_COLORS
)
1135 hPal
= NtGdiGetDCObject(hDC
, GDI_OBJECT_TYPE_PALETTE
);
1136 hPal
= GdiSelectPalette(hdcMem
, hPal
, FALSE
);
1139 if (bmiLocal
.bmiHeader
.bV5Compression
== BI_RLE4
||
1140 bmiLocal
.bmiHeader
.bV5Compression
== BI_RLE8
)
1142 /* copy existing bitmap from destination dc */
1143 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
1144 NtGdiBitBlt(hdcMem
, XSrc
, abs(bmiLocal
.bmiHeader
.bV5Height
) - SrcHeight
- YSrc
,
1145 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, ROP
, 0, 0);
1147 NtGdiStretchBlt(hdcMem
, XSrc
, abs(bmiLocal
.bmiHeader
.bV5Height
) - SrcHeight
- YSrc
,
1148 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
1152 pDC
= DC_LockDc(hdcMem
);
1155 /* Note BitsInfo->bmiHeader.biHeight is the number of scanline,
1156 * if it negitve we getting to many scanline for scanline is UINT not
1157 * a INT, so we need make the negtive value to positve and that make the
1158 * count correct for negtive bitmap, TODO : we need testcase for this api */
1159 IntSetDIBits(pDC
, hBitmap
, 0, abs(bmiLocal
.bmiHeader
.bV5Height
), Bits
,
1166 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
1167 left (negative biHeight) */
1168 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
1169 NtGdiBitBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
1170 hdcMem
, XSrc
, abs(bmiLocal
.bmiHeader
.bV5Height
) - SrcHeight
- YSrc
,
1173 NtGdiStretchBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
1174 hdcMem
, XSrc
, abs(bmiLocal
.bmiHeader
.bV5Height
) - SrcHeight
- YSrc
,
1175 SrcWidth
, SrcHeight
, ROP
, 0);
1179 GdiSelectPalette(hdcMem
, hPal
, FALSE
);
1182 NtGdiSelectBitmap(hdcMem
, hOldBitmap
);
1184 NtGdiDeleteObjectApp(hdcMem
);
1186 GreDeleteObject(hBitmap
);
1207 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1208 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1210 if (bpp
!= 1) fColor
= TRUE
;
1211 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1214 const RGBQUAD
*rgb
= data
->bmiColors
;
1215 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1217 // Check if the first color of the colormap is black
1218 if ((col
== RGB(0, 0, 0)))
1221 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1223 // If the second color is white, create a monochrome bitmap
1224 fColor
= (col
!= RGB(0xff,0xff,0xff));
1229 // Now create the bitmap
1232 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
1236 handle
= GreCreateBitmap(width
,
1246 if (NULL
!= handle
&& CBM_INIT
== init
)
1248 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
1254 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1255 // The DDB that is created will be whatever bit depth your reference DC is
1258 NtGdiCreateDIBitmapInternal(
1263 IN OPTIONAL LPBYTE pjInit
,
1264 IN OPTIONAL LPBITMAPINFO pbmi
,
1266 IN UINT cjMaxInitInfo
,
1271 BITMAPV5INFO bmiLocal
;
1272 NTSTATUS Status
= STATUS_SUCCESS
;
1276 if(pbmi
) Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, pbmi
, iUsage
, cjMaxInitInfo
);
1277 if(pjInit
&& (fInit
== CBM_INIT
)) ProbeForRead(pjInit
, cjMaxBits
, 1);
1279 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1281 Status
= _SEH2_GetExceptionCode();
1285 if(!NT_SUCCESS(Status
))
1287 SetLastNtError(Status
);
1291 return GreCreateDIBitmapInternal(hDc
,
1296 pbmi
? &bmiLocal
: NULL
,
1304 GreCreateDIBitmapInternal(
1309 IN OPTIONAL LPBYTE pjInit
,
1310 IN OPTIONAL PBITMAPV5INFO pbmi
,
1320 if (!hDc
) /* 1bpp monochrome bitmap */
1321 { // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1322 hdcDest
= IntGdiCreateDC(NULL
, NULL
, NULL
, NULL
,FALSE
);
1333 Dc
= DC_LockDc(hdcDest
);
1336 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1339 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1340 * if bpp != 1 and ignore the real value that was passed */
1342 bpp
= pbmi
->bmiHeader
.bV5BitCount
;
1345 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
, iUsage
);
1350 NtGdiDeleteObjectApp(hdcDest
);
1358 NtGdiCreateDIBSection(
1360 IN OPTIONAL HANDLE hSection
,
1366 IN ULONG_PTR dwColorSpace
,
1369 HBITMAP hbitmap
= 0;
1371 BOOL bDesktopDC
= FALSE
;
1372 NTSTATUS Status
= STATUS_SUCCESS
;
1374 if (!bmi
) return hbitmap
; // Make sure.
1378 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1379 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1380 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, Usage
), 1);
1382 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1384 Status
= _SEH2_GetExceptionCode();
1388 if(!NT_SUCCESS(Status
))
1390 SetLastNtError(Status
);
1394 // If the reference hdc is null, take the desktop dc
1397 hDC
= NtGdiCreateCompatibleDC(0);
1401 if ((dc
= DC_LockDc(hDC
)))
1403 hbitmap
= DIB_CreateDIBSection(dc
,
1414 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1418 NtGdiDeleteObjectApp(hDC
);
1425 DIB_CreateDIBSection(
1427 CONST BITMAPINFO
*bmi
,
1435 SURFACE
*bmp
= NULL
;
1436 void *mapBits
= NULL
;
1438 INT palMode
= PAL_INDEXED
;
1440 // Fill BITMAP32 structure with DIB data
1441 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1448 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1449 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1450 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1452 /* CreateDIBSection should fail for compressed formats */
1453 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1455 return (HBITMAP
)NULL
;
1458 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1460 bm
.bmWidth
= bi
->biWidth
;
1461 bm
.bmHeight
= effHeight
;
1462 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
1464 bm
.bmPlanes
= bi
->biPlanes
;
1465 bm
.bmBitsPixel
= bi
->biBitCount
;
1468 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1469 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1470 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
1471 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1475 SYSTEM_BASIC_INFORMATION Sbi
;
1478 LARGE_INTEGER SectionOffset
;
1481 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1485 if (!NT_SUCCESS(Status
))
1490 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1491 mapSize
= bi
->biSizeImage
+ (offset
- mapOffset
);
1493 SectionOffset
.LowPart
= mapOffset
;
1494 SectionOffset
.HighPart
= 0;
1496 Status
= ZwMapViewOfSection(section
,
1506 if (!NT_SUCCESS(Status
))
1508 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1512 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1514 else if (ovr_pitch
&& offset
)
1515 bm
.bmBits
= (LPVOID
) offset
;
1519 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1520 if(!bm
.bmBits
) goto cleanup
;
1523 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1524 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1526 if (usage
== DIB_PAL_COLORS
)
1529 pdcPal
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
1532 DPRINT1("Unable to lock DC palette?!\n");
1535 if(pdcPal
->Mode
!= PAL_INDEXED
)
1537 DPRINT1("Not indexed palette selected in the DC?!\n");
1538 PALETTE_UnlockPalette(pdcPal
);
1540 hpal
= PALETTE_AllocPalette(PAL_INDEXED
,
1542 (ULONG
*)pdcPal
->IndexedColors
, 0, 0, 0);
1543 PALETTE_UnlockPalette(pdcPal
);
1547 hpal
= BuildDIBPalette(bmi
, &palMode
);
1550 // Create Device Dependent Bitmap and add DIB pointer
1551 Size
.cx
= bm
.bmWidth
;
1552 Size
.cy
= abs(bm
.bmHeight
);
1553 res
= GreCreateBitmapEx(bm
.bmWidth
,
1556 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1557 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1558 (bi
->biHeight
< 0 ? BMF_TOPDOWN
: 0),
1563 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
1566 bmp
= SURFACE_LockSurface(res
);
1569 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1573 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1574 table between the DIB and the X physical device. Obviously,
1575 this is left out of the ReactOS implementation. Instead,
1576 we call NtGdiSetDIBColorTable. */
1577 bmp
->hDIBSection
= section
;
1578 bmp
->hSecure
= hSecure
;
1579 bmp
->dwOffset
= offset
;
1580 bmp
->flags
= API_BITMAP
;
1581 bmp
->biClrImportant
= bi
->biClrImportant
;
1583 bmp
->ppal
= PALETTE_ShareLockPalette(hpal
);
1584 /* Lazy delete hpal, it will be freed at surface release */
1585 GreDeleteObject(hpal
);
1587 // Clean up in case of errors
1589 if (!res
|| !bmp
|| !bm
.bmBits
)
1591 DPRINT("got an error res=%08x, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1594 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1597 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1602 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1610 SURFACE_FreeSurfaceByHandle(res
);
1617 SURFACE_UnlockSurface(bmp
);
1620 // Return BITMAP handle and storage location
1621 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1629 /***********************************************************************
1632 * Get the info from a bitmap header.
1633 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
1637 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
1638 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
1640 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1642 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
1643 *width
= core
->bcWidth
;
1644 *height
= core
->bcHeight
;
1645 *planes
= core
->bcPlanes
;
1646 *bpp
= core
->bcBitCount
;
1651 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* assume BITMAPINFOHEADER */
1653 *width
= header
->biWidth
;
1654 *height
= header
->biHeight
;
1655 *planes
= header
->biPlanes
;
1656 *bpp
= header
->biBitCount
;
1657 *compr
= header
->biCompression
;
1658 *size
= header
->biSizeImage
;
1661 DPRINT1("(%d): unknown/wrong size for header\n", header
->biSize
);
1665 /***********************************************************************
1666 * DIB_GetDIBWidthBytes
1668 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1669 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
1670 * 11/16/1999 (RJJ) lifted from wine
1672 INT FASTCALL
DIB_GetDIBWidthBytes(INT width
, INT depth
)
1674 return ((width
* depth
+ 31) & ~31) >> 3;
1677 /***********************************************************************
1678 * DIB_GetDIBImageBytes
1680 * Return the number of bytes used to hold the image in a DIB bitmap.
1681 * 11/16/1999 (RJJ) lifted from wine
1684 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1686 return DIB_GetDIBWidthBytes(width
, depth
) * (height
< 0 ? -height
: height
);
1689 /***********************************************************************
1690 * DIB_BitmapInfoSize
1692 * Return the size of the bitmap info structure including color table.
1693 * 11/16/1999 (RJJ) lifted from wine
1696 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1698 unsigned int colors
, size
, masks
= 0;
1700 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1702 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
1703 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1704 return sizeof(BITMAPCOREHEADER
) + colors
*
1705 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1707 else /* assume BITMAPINFOHEADER */
1709 colors
= info
->bmiHeader
.biClrUsed
;
1710 if (colors
> 256) colors
= 256;
1711 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
1712 colors
= 1 << info
->bmiHeader
.biBitCount
;
1713 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
1714 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
1715 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1721 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1728 palGDI
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
1735 if (palGDI
->Mode
!= PAL_INDEXED
)
1737 PALETTE_UnlockPalette(palGDI
);
1741 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1742 if (lpbmi
->bmiHeader
.biClrUsed
)
1744 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1747 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1750 PALETTE_UnlockPalette(palGDI
);
1754 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1756 for (i
= 0; i
< nNumColors
; i
++)
1758 if (*lpIndex
< palGDI
->NumColors
)
1760 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1761 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1762 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1766 lpRGB
[i
].rgbRed
= 0;
1767 lpRGB
[i
].rgbGreen
= 0;
1768 lpRGB
[i
].rgbBlue
= 0;
1770 lpRGB
[i
].rgbReserved
= 0;
1773 PALETTE_UnlockPalette(palGDI
);
1780 BuildDIBPalette(CONST BITMAPINFO
*bmi
, PINT paletteType
)
1785 ULONG RedMask
, GreenMask
, BlueMask
;
1786 PDWORD pdwColors
= (PDWORD
)((PBYTE
)bmi
+ bmi
->bmiHeader
.biSize
);
1788 // Determine Bits Per Pixel
1789 bits
= bmi
->bmiHeader
.biBitCount
;
1791 // Determine paletteType from Bits Per Pixel
1794 *paletteType
= PAL_INDEXED
;
1795 RedMask
= GreenMask
= BlueMask
= 0;
1797 else if (bmi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
1799 *paletteType
= PAL_BITFIELDS
;
1800 RedMask
= pdwColors
[0];
1801 GreenMask
= pdwColors
[1];
1802 BlueMask
= pdwColors
[2];
1804 else if (bits
== 15)
1806 *paletteType
= PAL_BITFIELDS
;
1811 else if (bits
== 16)
1813 *paletteType
= PAL_BITFIELDS
;
1820 *paletteType
= PAL_RGB
;
1822 GreenMask
= 0x00ff00;
1823 BlueMask
= 0x0000ff;
1826 if (bmi
->bmiHeader
.biClrUsed
== 0)
1828 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1832 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1835 if (PAL_INDEXED
== *paletteType
)
1837 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)pdwColors
);
1841 hPal
= PALETTE_AllocPalette(*paletteType
, 0,
1843 RedMask
, GreenMask
, BlueMask
);
1851 GetBMIColor(CONST BITMAPINFO
* pbmi
, INT i
)
1855 if(pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1857 /* BITMAPCOREINFO holds RGBTRIPLE values */
1858 size
= sizeof(RGBTRIPLE
);
1862 size
= sizeof(RGBQUAD
);
1864 memcpy(&dwRet
, (PBYTE
)pbmi
+ pbmi
->bmiHeader
.biSize
+ i
*size
, size
);
1870 ProbeAndConvertToBitmapV5Info(
1871 OUT PBITMAPV5INFO pbmiDst
,
1872 IN CONST BITMAPINFO
* pbmiUnsafe
,
1873 IN DWORD dwColorUse
,
1878 PBITMAPV5HEADER pbmhDst
= &pbmiDst
->bmiHeader
;
1880 /* Get the size and probe */
1881 ProbeForRead(&pbmiUnsafe
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1882 dwSize
= pbmiUnsafe
->bmiHeader
.biSize
;
1883 /* At least dwSize bytes must be valids */
1884 ProbeForRead(pbmiUnsafe
, max(dwSize
, MaxSize
), 1);
1886 ProbeForRead(pbmiUnsafe
, DIB_BitmapInfoSize(pbmiUnsafe
, dwColorUse
), 1);
1888 /* Check the size */
1889 // FIXME: are intermediate sizes allowed? As what are they interpreted?
1890 // make sure we don't use a too big dwSize later
1891 if (dwSize
!= sizeof(BITMAPCOREHEADER
) &&
1892 dwSize
!= sizeof(BITMAPINFOHEADER
) &&
1893 dwSize
!= sizeof(BITMAPV4HEADER
) &&
1894 dwSize
!= sizeof(BITMAPV5HEADER
))
1896 return STATUS_INVALID_PARAMETER
;
1899 if (dwSize
== sizeof(BITMAPCOREHEADER
))
1901 PBITMAPCOREHEADER pbch
= (PBITMAPCOREHEADER
)pbmiUnsafe
;
1903 /* Manually copy the fields that are present */
1904 pbmhDst
->bV5Width
= pbch
->bcWidth
;
1905 pbmhDst
->bV5Height
= pbch
->bcHeight
;
1906 pbmhDst
->bV5Planes
= pbch
->bcPlanes
;
1907 pbmhDst
->bV5BitCount
= pbch
->bcBitCount
;
1909 /* Set some default values */
1910 pbmhDst
->bV5Compression
= BI_RGB
;
1911 pbmhDst
->bV5SizeImage
= DIB_GetDIBImageBytes(pbch
->bcWidth
,
1913 pbch
->bcPlanes
*pbch
->bcBitCount
) ;
1914 pbmhDst
->bV5XPelsPerMeter
= 72;
1915 pbmhDst
->bV5YPelsPerMeter
= 72;
1916 pbmhDst
->bV5ClrUsed
= 0;
1917 pbmhDst
->bV5ClrImportant
= 0;
1921 /* Copy valid fields */
1922 memcpy(pbmiDst
, pbmiUnsafe
, dwSize
);
1923 if(!pbmhDst
->bV5SizeImage
)
1924 pbmhDst
->bV5SizeImage
= DIB_GetDIBImageBytes(pbmhDst
->bV5Width
,
1926 pbmhDst
->bV5Planes
*pbmhDst
->bV5BitCount
) ;
1928 if(dwSize
< sizeof(BITMAPV5HEADER
))
1930 /* Zero out the rest of the V5 header */
1931 memset((char*)pbmiDst
+ dwSize
, 0, sizeof(BITMAPV5HEADER
) - dwSize
);
1934 pbmhDst
->bV5Size
= sizeof(BITMAPV5HEADER
);
1937 if (dwSize
< sizeof(BITMAPV4HEADER
))
1939 if (pbmhDst
->bV5Compression
== BI_BITFIELDS
)
1941 pbmhDst
->bV5RedMask
= GetBMIColor(pbmiUnsafe
, 0);
1942 pbmhDst
->bV5GreenMask
= GetBMIColor(pbmiUnsafe
, 1);
1943 pbmhDst
->bV5BlueMask
= GetBMIColor(pbmiUnsafe
, 2);
1944 pbmhDst
->bV5AlphaMask
= 0;
1945 pbmhDst
->bV5ClrUsed
= 0;
1948 // pbmhDst->bV5CSType;
1949 // pbmhDst->bV5Endpoints;
1950 // pbmhDst->bV5GammaRed;
1951 // pbmhDst->bV5GammaGreen;
1952 // pbmhDst->bV5GammaBlue;
1955 if (dwSize
< sizeof(BITMAPV5HEADER
))
1957 // pbmhDst->bV5Intent;
1958 // pbmhDst->bV5ProfileData;
1959 // pbmhDst->bV5ProfileSize;
1960 // pbmhDst->bV5Reserved;
1963 ulWidthBytes
= ((pbmhDst
->bV5Width
* pbmhDst
->bV5Planes
*
1964 pbmhDst
->bV5BitCount
+ 31) & ~31) / 8;
1966 if (pbmhDst
->bV5SizeImage
== 0)
1967 pbmhDst
->bV5SizeImage
= abs(ulWidthBytes
* pbmhDst
->bV5Height
);
1969 if (pbmhDst
->bV5ClrUsed
== 0)
1971 switch(pbmhDst
->bV5BitCount
)
1974 pbmhDst
->bV5ClrUsed
= 2;
1977 pbmhDst
->bV5ClrUsed
= 16;
1980 pbmhDst
->bV5ClrUsed
= 256;
1983 pbmhDst
->bV5ClrUsed
= 0;
1988 if (pbmhDst
->bV5Planes
!= 1)
1990 return STATUS_INVALID_PARAMETER
;
1993 if (pbmhDst
->bV5BitCount
!= 0 && pbmhDst
->bV5BitCount
!= 1 &&
1994 pbmhDst
->bV5BitCount
!= 4 && pbmhDst
->bV5BitCount
!= 8 &&
1995 pbmhDst
->bV5BitCount
!= 16 && pbmhDst
->bV5BitCount
!= 24 &&
1996 pbmhDst
->bV5BitCount
!= 32)
1998 DPRINT("Invalid bit count: %d\n", pbmhDst
->bV5BitCount
);
1999 return STATUS_INVALID_PARAMETER
;
2002 if ((pbmhDst
->bV5BitCount
== 0 &&
2003 pbmhDst
->bV5Compression
!= BI_JPEG
&& pbmhDst
->bV5Compression
!= BI_PNG
))
2005 DPRINT("Bit count 0 is invalid for compression %d.\n", pbmhDst
->bV5Compression
);
2006 return STATUS_INVALID_PARAMETER
;
2009 if (pbmhDst
->bV5Compression
== BI_BITFIELDS
&&
2010 pbmhDst
->bV5BitCount
!= 16 && pbmhDst
->bV5BitCount
!= 32)
2012 DPRINT("Bit count %d is invalid for compression BI_BITFIELDS.\n", pbmhDst
->bV5BitCount
);
2013 return STATUS_INVALID_PARAMETER
;
2017 if(pbmhDst
->bV5ClrUsed
)
2020 if(dwColorUse
== DIB_PAL_COLORS
)
2022 RtlCopyMemory(pbmiDst
->bmiColors
,
2023 pbmiUnsafe
->bmiColors
,
2024 pbmhDst
->bV5ClrUsed
* sizeof(WORD
));
2028 for(i
= 0; i
< pbmhDst
->bV5ClrUsed
; i
++)
2030 ((DWORD
*)pbmiDst
->bmiColors
)[i
] = GetBMIColor(pbmiUnsafe
, i
);
2035 return STATUS_SUCCESS
;
2038 /* Converts a BITMAPCOREINFO to a BITMAPINFO structure,
2039 * or does nothing if it's already a BITMAPINFO (or V4 or V5) */
2042 DIB_ConvertBitmapInfo (CONST BITMAPINFO
* pbmi
, DWORD Usage
)
2044 CONST BITMAPCOREINFO
* pbmci
= (BITMAPCOREINFO
*)pbmi
;
2045 BITMAPINFO
* pNewBmi
;
2046 UINT numColors
= 0, ColorsSize
= 0;
2048 if(pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) return (BITMAPINFO
*)pbmi
;
2049 if(pbmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) return NULL
;
2051 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
2053 numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
2054 if(Usage
== DIB_PAL_COLORS
)
2056 ColorsSize
= numColors
* sizeof(WORD
);
2060 ColorsSize
= numColors
* sizeof(RGBQUAD
);
2063 else if (Usage
== DIB_PAL_COLORS
)
2065 /* Invalid at high Res */
2069 pNewBmi
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + ColorsSize
, TAG_DIB
);
2070 if(!pNewBmi
) return NULL
;
2072 RtlZeroMemory(pNewBmi
, sizeof(BITMAPINFOHEADER
) + ColorsSize
);
2074 pNewBmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
2075 pNewBmi
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
2076 pNewBmi
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
2077 pNewBmi
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
2078 pNewBmi
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
2079 pNewBmi
->bmiHeader
.biCompression
= BI_RGB
;
2080 pNewBmi
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(pNewBmi
->bmiHeader
.biWidth
,
2081 pNewBmi
->bmiHeader
.biHeight
,
2082 pNewBmi
->bmiHeader
.biBitCount
);
2084 if(Usage
== DIB_PAL_COLORS
)
2086 RtlCopyMemory(pNewBmi
->bmiColors
, pbmci
->bmciColors
, ColorsSize
);
2091 for(i
=0; i
<numColors
; i
++)
2093 pNewBmi
->bmiColors
[i
].rgbRed
= pbmci
->bmciColors
[i
].rgbtRed
;
2094 pNewBmi
->bmiColors
[i
].rgbGreen
= pbmci
->bmciColors
[i
].rgbtGreen
;
2095 pNewBmi
->bmiColors
[i
].rgbBlue
= pbmci
->bmciColors
[i
].rgbtBlue
;
2102 /* Frees a BITMAPINFO created with DIB_ConvertBitmapInfo */
2105 DIB_FreeConvertedBitmapInfo(BITMAPINFO
* converted
, BITMAPINFO
* orig
)
2107 if(converted
!= orig
)
2108 ExFreePoolWithTag(converted
, TAG_DIB
);