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
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
), TAG_DIB
);
680 DPRINT1("Error, could not allocate another BITMAPINFO!\n");
683 RtlZeroMemory(Info
, sizeof(BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
));
684 Info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
685 Info
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
686 Info
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
687 Info
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
688 Info
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
689 rgbQuads
= Info
->bmiColors
;
692 pDC
= DC_LockDc(hDC
);
693 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
699 /* Get a pointer to the source bitmap object */
700 psurf
= SURFACE_LockSurface(hBitmap
);
706 /* Must not be selected */
707 if(psurf
->hdc
!= NULL
)
710 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
715 /* Fill in the structure */
718 case 0: /* Only info */
721 pbmci
->bmciHeader
.bcWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
722 pbmci
->bmciHeader
.bcHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
723 -psurf
->SurfObj
.sizlBitmap
.cy
:
724 psurf
->SurfObj
.sizlBitmap
.cy
;
725 pbmci
->bmciHeader
.bcPlanes
= 1;
726 pbmci
->bmciHeader
.bcBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
728 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
729 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
730 -psurf
->SurfObj
.sizlBitmap
.cy
:
731 psurf
->SurfObj
.sizlBitmap
.cy
;;
732 Info
->bmiHeader
.biPlanes
= 1;
733 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
734 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
735 Info
->bmiHeader
.biHeight
,
736 Info
->bmiHeader
.biBitCount
);
739 switch(Info
->bmiHeader
.biBitCount
)
743 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
746 Info
->bmiHeader
.biCompression
= BI_RGB
;
750 else if(Info
->bmiHeader
.biBitCount
> 8)
752 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
756 Info
->bmiHeader
.biCompression
= BI_RGB
;
758 Info
->bmiHeader
.biXPelsPerMeter
= 0;
759 Info
->bmiHeader
.biYPelsPerMeter
= 0;
760 Info
->bmiHeader
.biClrUsed
= 0;
761 Info
->bmiHeader
.biClrImportant
= 0;
762 ScanLines
= psurf
->SurfObj
.sizlBitmap
.cy
;
763 /* Get Complete info now */
764 bpp
= Info
->bmiHeader
.biBitCount
;
770 Info
->bmiHeader
.biClrUsed
= 0;
772 /* If the bitmap if a DIB section and has the same format than what
773 * we're asked, go ahead! */
774 if((psurf
->hSecure
) &&
775 (BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bpp
))
777 if(Usage
== DIB_RGB_COLORS
)
779 unsigned int colors
= min(psurf
->ppal
->NumColors
, 1 << bpp
);
783 for(i
=0; i
< colors
; i
++)
785 rgbTriples
[i
].rgbtRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
786 rgbTriples
[i
].rgbtGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
787 rgbTriples
[i
].rgbtBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
790 if(colors
!= 1 << bpp
) Info
->bmiHeader
.biClrUsed
= colors
;
791 RtlCopyMemory(rgbQuads
, psurf
->ppal
->IndexedColors
, colors
* sizeof(RGBQUAD
));
795 for(i
=0; i
< 1 << bpp
; i
++)
797 if(pbmci
) ((WORD
*)rgbTriples
)[i
] = i
;
798 ((WORD
*)rgbQuads
)[i
] = i
;
804 if(Usage
== DIB_PAL_COLORS
)
806 for(i
=0; i
< 1 << bpp
; i
++)
808 if(pbmci
) ((WORD
*)rgbTriples
)[i
] = i
;
809 ((WORD
*)rgbQuads
)[i
] = i
;
812 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
)) {
813 /* For color DDBs in native depth (mono DDBs always have
814 a black/white palette):
815 Generate the color map from the selected palette */
816 PPALETTE pDcPal
= PALETTE_LockPalette(pDC
->dclevel
.hpal
);
822 for (i
= 0; i
< pDcPal
->NumColors
; i
++) {
825 rgbTriples
[i
].rgbtRed
= pDcPal
->IndexedColors
[i
].peRed
;
826 rgbTriples
[i
].rgbtGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
827 rgbTriples
[i
].rgbtBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
830 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
831 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
832 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
833 rgbQuads
[i
].rgbReserved
= 0;
835 PALETTE_UnlockPalette(pDcPal
);
841 rgbTriples
[0].rgbtRed
= rgbTriples
[0].rgbtGreen
=
842 rgbTriples
[0].rgbtBlue
= 0;
843 rgbTriples
[1].rgbtRed
= rgbTriples
[1].rgbtGreen
=
844 rgbTriples
[1].rgbtBlue
= 0xff;
846 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
=
847 rgbQuads
[0].rgbBlue
= 0;
848 rgbQuads
[0].rgbReserved
= 0;
849 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
=
850 rgbQuads
[1].rgbBlue
= 0xff;
851 rgbQuads
[1].rgbReserved
= 0;
856 RtlCopyMemory(rgbTriples
, EGAColorsTriples
, sizeof(EGAColorsTriples
));
857 RtlCopyMemory(rgbQuads
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
867 RGBTRIPLE
*colorTriple
;
869 RtlCopyMemory(rgbTriples
, DefLogPaletteTriples
,
870 10 * sizeof(RGBTRIPLE
));
871 RtlCopyMemory(rgbTriples
+ 246, DefLogPaletteTriples
+ 10,
872 10 * sizeof(RGBTRIPLE
));
873 colorTriple
= rgbTriples
+ 10;
874 for(r
= 0; r
<= 5; r
++) /* FIXME */
876 for(g
= 0; g
<= 5; g
++)
878 for(b
= 0; b
<= 5; b
++)
880 colorTriple
->rgbtRed
= (r
* 0xff) / 5;
881 colorTriple
->rgbtGreen
= (g
* 0xff) / 5;
882 colorTriple
->rgbtBlue
= (b
* 0xff) / 5;
888 memcpy(rgbQuads
, DefLogPaletteQuads
,
889 10 * sizeof(RGBQUAD
));
890 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10,
891 10 * sizeof(RGBQUAD
));
892 color
= rgbQuads
+ 10;
893 for(r
= 0; r
<= 5; r
++) /* FIXME */
895 for(g
= 0; g
<= 5; g
++)
897 for(b
= 0; b
<= 5; b
++)
899 color
->rgbRed
= (r
* 0xff) / 5;
900 color
->rgbGreen
= (g
* 0xff) / 5;
901 color
->rgbBlue
= (b
* 0xff) / 5;
902 color
->rgbReserved
= 0;
914 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
916 ((PDWORD
)Info
->bmiColors
)[0] = 0x7c00;
917 ((PDWORD
)Info
->bmiColors
)[1] = 0x03e0;
918 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
923 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
925 if (psurf
->hSecure
) RtlCopyMemory( Info
->bmiColors
, psurf
->dsBitfields
, 3 * sizeof(DWORD
) );
928 ((PDWORD
)Info
->bmiColors
)[0] = 0xf800;
929 ((PDWORD
)Info
->bmiColors
)[1] = 0x07e0;
930 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
937 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
939 if (psurf
->hSecure
) RtlCopyMemory( Info
->bmiColors
, psurf
->dsBitfields
, 3 * sizeof(DWORD
) );
942 ((PDWORD
)Info
->bmiColors
)[0] = 0xff0000;
943 ((PDWORD
)Info
->bmiColors
)[1] = 0x00ff00;
944 ((PDWORD
)Info
->bmiColors
)[2] = 0x0000ff;
950 if(Bits
&& ScanLines
)
952 /* Create a DIBSECTION, blt it, profit */
954 HBITMAP hBmpDest
, hOldDest
= NULL
, hOldSrc
= NULL
;
958 if (StartScan
> psurf
->SurfObj
.sizlBitmap
.cy
)
965 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
968 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
972 DPRINT1("Unable to create a DIB Section!\n");
973 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
978 hdcDest
= NtGdiCreateCompatibleDC(0);
979 hdcSrc
= NtGdiCreateCompatibleDC(0);
981 if(!(hdcSrc
&& hdcDest
))
983 DPRINT1("Error: could not create HDCs!\n");
988 hOldDest
= NtGdiSelectBitmap(hdcDest
, hBmpDest
);
989 hOldSrc
= NtGdiSelectBitmap(hdcSrc
, hBitmap
);
991 if(!(hOldDest
&& hOldSrc
))
993 DPRINT1("Error : Could not Select bitmaps\n");
997 ret
= GreStretchBltMask(hdcDest
,
1005 psurf
->SurfObj
.sizlBitmap
.cx
,
1017 Status
= STATUS_SUCCESS
;
1020 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, height
, bpp
));
1022 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1024 Status
= _SEH2_GetExceptionCode();
1028 if(!NT_SUCCESS(Status
))
1030 DPRINT1("Unable to copy bits to the user provided pointer\n");
1038 if(hOldSrc
) NtGdiSelectBitmap(hdcSrc
, hOldSrc
);
1039 NtGdiDeleteObjectApp(hdcSrc
);
1043 if(hOldDest
) NtGdiSelectBitmap(hdcDest
, hOldDest
);
1044 NtGdiDeleteObjectApp(hdcDest
);
1046 GreDeleteObject(hBmpDest
);
1051 if(pDC
) DC_UnlockDc(pDC
);
1052 if(psurf
) SURFACE_UnlockSurface(psurf
);
1053 if(pbmci
) ExFreePoolWithTag(Info
, TAG_DIB
);
1061 NtGdiStretchDIBitsInternal(
1072 LPBITMAPINFO BitsInfo
,
1079 HBITMAP hBitmap
, hOldBitmap
= NULL
;
1081 HPALETTE hPal
= NULL
;
1084 BITMAPV5INFO bmiLocal
;
1086 if (!Bits
|| !BitsInfo
)
1088 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1094 Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, BitsInfo
, Usage
, cjMaxInfo
);
1095 ProbeForRead(Bits
, cjMaxBits
, 1);
1097 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1099 Status
= _SEH2_GetExceptionCode();
1103 if (!NT_SUCCESS(Status
))
1105 DPRINT1("NtGdiStretchDIBitsInternal fail to read BitMapInfo: %x or Bits: %x\n",BitsInfo
,Bits
);
1109 hdcMem
= NtGdiCreateCompatibleDC(hDC
);
1112 DPRINT1("NtGdiCreateCompatibleDC fail create hdc\n");
1116 hBitmap
= NtGdiCreateCompatibleBitmap(hDC
,
1117 abs(bmiLocal
.bmiHeader
.bV5Width
),
1118 abs(bmiLocal
.bmiHeader
.bV5Height
));
1119 if (hBitmap
== NULL
)
1121 DPRINT1("NtGdiCreateCompatibleBitmap fail create bitmap\n");
1122 DPRINT1("hDC : 0x%08x \n", hDC
);
1123 DPRINT1("BitsInfo->bmiHeader.biWidth : 0x%08x \n", BitsInfo
->bmiHeader
.biWidth
);
1124 DPRINT1("BitsInfo->bmiHeader.biHeight : 0x%08x \n", BitsInfo
->bmiHeader
.biHeight
);
1128 /* Select the bitmap into hdcMem, and save a handle to the old bitmap */
1129 hOldBitmap
= NtGdiSelectBitmap(hdcMem
, hBitmap
);
1131 if (Usage
== DIB_PAL_COLORS
)
1133 hPal
= NtGdiGetDCObject(hDC
, GDI_OBJECT_TYPE_PALETTE
);
1134 hPal
= GdiSelectPalette(hdcMem
, hPal
, FALSE
);
1137 if (bmiLocal
.bmiHeader
.bV5Compression
== BI_RLE4
||
1138 bmiLocal
.bmiHeader
.bV5Compression
== BI_RLE8
)
1140 /* copy existing bitmap from destination dc */
1141 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
1142 NtGdiBitBlt(hdcMem
, XSrc
, abs(bmiLocal
.bmiHeader
.bV5Height
) - SrcHeight
- YSrc
,
1143 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, ROP
, 0, 0);
1145 NtGdiStretchBlt(hdcMem
, XSrc
, abs(bmiLocal
.bmiHeader
.bV5Height
) - SrcHeight
- YSrc
,
1146 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
1150 pDC
= DC_LockDc(hdcMem
);
1153 /* Note BitsInfo->bmiHeader.biHeight is the number of scanline,
1154 * if it negitve we getting to many scanline for scanline is UINT not
1155 * a INT, so we need make the negtive value to positve and that make the
1156 * count correct for negtive bitmap, TODO : we need testcase for this api */
1157 IntSetDIBits(pDC
, hBitmap
, 0, abs(bmiLocal
.bmiHeader
.bV5Height
), Bits
,
1164 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
1165 left (negative biHeight) */
1166 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
1167 NtGdiBitBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
1168 hdcMem
, XSrc
, abs(bmiLocal
.bmiHeader
.bV5Height
) - SrcHeight
- YSrc
,
1171 NtGdiStretchBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
1172 hdcMem
, XSrc
, abs(bmiLocal
.bmiHeader
.bV5Height
) - SrcHeight
- YSrc
,
1173 SrcWidth
, SrcHeight
, ROP
, 0);
1177 GdiSelectPalette(hdcMem
, hPal
, FALSE
);
1180 NtGdiSelectBitmap(hdcMem
, hOldBitmap
);
1182 NtGdiDeleteObjectApp(hdcMem
);
1184 GreDeleteObject(hBitmap
);
1205 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1206 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1208 if (bpp
!= 1) fColor
= TRUE
;
1209 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1212 const RGBQUAD
*rgb
= data
->bmiColors
;
1213 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1215 // Check if the first color of the colormap is black
1216 if ((col
== RGB(0, 0, 0)))
1219 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1221 // If the second color is white, create a monochrome bitmap
1222 fColor
= (col
!= RGB(0xff,0xff,0xff));
1227 // Now create the bitmap
1230 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
1234 handle
= GreCreateBitmap(width
,
1244 if (NULL
!= handle
&& CBM_INIT
== init
)
1246 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
1252 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1253 // The DDB that is created will be whatever bit depth your reference DC is
1256 NtGdiCreateDIBitmapInternal(
1261 IN OPTIONAL LPBYTE pjInit
,
1262 IN OPTIONAL LPBITMAPINFO pbmi
,
1264 IN UINT cjMaxInitInfo
,
1269 BITMAPV5INFO bmiLocal
;
1270 NTSTATUS Status
= STATUS_SUCCESS
;
1274 if(pbmi
) Status
= ProbeAndConvertToBitmapV5Info(&bmiLocal
, pbmi
, iUsage
, cjMaxInitInfo
);
1275 if(pjInit
&& (fInit
== CBM_INIT
)) ProbeForRead(pjInit
, cjMaxBits
, 1);
1277 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1279 Status
= _SEH2_GetExceptionCode();
1283 if(!NT_SUCCESS(Status
))
1285 SetLastNtError(Status
);
1289 return GreCreateDIBitmapInternal(hDc
,
1294 pbmi
? &bmiLocal
: NULL
,
1302 GreCreateDIBitmapInternal(
1307 IN OPTIONAL LPBYTE pjInit
,
1308 IN OPTIONAL PBITMAPV5INFO pbmi
,
1318 if (!hDc
) /* 1bpp monochrome bitmap */
1319 { // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1320 hdcDest
= IntGdiCreateDC(NULL
, NULL
, NULL
, NULL
,FALSE
);
1331 Dc
= DC_LockDc(hdcDest
);
1334 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1337 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1338 * if bpp != 1 and ignore the real value that was passed */
1340 bpp
= pbmi
->bmiHeader
.bV5BitCount
;
1343 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
, iUsage
);
1348 NtGdiDeleteObjectApp(hdcDest
);
1356 NtGdiCreateDIBSection(
1358 IN OPTIONAL HANDLE hSection
,
1364 IN ULONG_PTR dwColorSpace
,
1367 HBITMAP hbitmap
= 0;
1369 BOOL bDesktopDC
= FALSE
;
1370 NTSTATUS Status
= STATUS_SUCCESS
;
1372 if (!bmi
) return hbitmap
; // Make sure.
1376 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1377 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1378 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, Usage
), 1);
1380 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1382 Status
= _SEH2_GetExceptionCode();
1386 if(!NT_SUCCESS(Status
))
1388 SetLastNtError(Status
);
1392 // If the reference hdc is null, take the desktop dc
1395 hDC
= NtGdiCreateCompatibleDC(0);
1399 if ((dc
= DC_LockDc(hDC
)))
1401 hbitmap
= DIB_CreateDIBSection(dc
,
1412 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1416 NtGdiDeleteObjectApp(hDC
);
1423 DIB_CreateDIBSection(
1425 CONST BITMAPINFO
*bmi
,
1433 SURFACE
*bmp
= NULL
;
1434 void *mapBits
= NULL
;
1436 ULONG palMode
= PAL_INDEXED
;
1438 // Fill BITMAP32 structure with DIB data
1439 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1444 CONST RGBQUAD
*lpRGB
= NULL
;
1446 DWORD dsBitfields
[3] = {0};
1449 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1450 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1451 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1453 /* CreateDIBSection should fail for compressed formats */
1454 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1456 return (HBITMAP
)NULL
;
1459 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1461 bm
.bmWidth
= bi
->biWidth
;
1462 bm
.bmHeight
= effHeight
;
1463 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
1465 bm
.bmPlanes
= bi
->biPlanes
;
1466 bm
.bmBitsPixel
= bi
->biBitCount
;
1469 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1470 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1471 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
1472 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1476 SYSTEM_BASIC_INFORMATION Sbi
;
1479 LARGE_INTEGER SectionOffset
;
1482 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1486 if (!NT_SUCCESS(Status
))
1491 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1492 mapSize
= bi
->biSizeImage
+ (offset
- mapOffset
);
1494 SectionOffset
.LowPart
= mapOffset
;
1495 SectionOffset
.HighPart
= 0;
1497 Status
= ZwMapViewOfSection(section
,
1507 if (!NT_SUCCESS(Status
))
1509 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1513 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1515 else if (ovr_pitch
&& offset
)
1516 bm
.bmBits
= (LPVOID
) offset
;
1520 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1521 if(!bm
.bmBits
) goto cleanup
;
1524 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1525 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1527 if (usage
== DIB_PAL_COLORS
)
1529 lpRGB
= DIB_MapPaletteColors(dc
, bmi
);
1530 ColorCount
= bi
->biClrUsed
;
1531 if (ColorCount
== 0)
1533 ColorCount
= max(1 << bi
->biBitCount
, 256);
1536 else if(bi
->biBitCount
<= 8)
1538 lpRGB
= bmi
->bmiColors
;
1539 ColorCount
= 1 << bi
->biBitCount
;
1547 /* Set dsBitfields values */
1548 if (usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
1550 dsBitfields
[0] = dsBitfields
[1] = dsBitfields
[2] = 0;
1551 palMode
= PAL_INDEXED
;
1553 else if (bi
->biCompression
== BI_RGB
)
1555 dsBitfields
[0] = dsBitfields
[1] = dsBitfields
[2] = 0;
1556 switch (bi
->biBitCount
)
1559 palMode
= PAL_RGB16_555
;
1563 palMode
= PAL_RGB16_565
;
1574 RtlCopyMemory(dsBitfields
, bmi
->bmiColors
, sizeof(dsBitfields
));
1575 palMode
= PAL_BITFIELDS
;
1578 // Create Device Dependent Bitmap and add DIB pointer
1579 Size
.cx
= bm
.bmWidth
;
1580 Size
.cy
= abs(bm
.bmHeight
);
1581 res
= GreCreateBitmapEx(bm
.bmWidth
,
1584 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1585 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1586 (bi
->biHeight
< 0 ? BMF_TOPDOWN
: 0),
1591 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
1594 bmp
= SURFACE_LockSurface(res
);
1597 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1601 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1602 table between the DIB and the X physical device. Obviously,
1603 this is left out of the ReactOS implementation. Instead,
1604 we call NtGdiSetDIBColorTable. */
1605 bmp
->hDIBSection
= section
;
1606 bmp
->hSecure
= hSecure
;
1607 bmp
->dwOffset
= offset
;
1608 bmp
->flags
= API_BITMAP
;
1609 bmp
->dsBitfields
[0] = dsBitfields
[0];
1610 bmp
->dsBitfields
[1] = dsBitfields
[1];
1611 bmp
->dsBitfields
[2] = dsBitfields
[2];
1612 bmp
->biClrUsed
= ColorCount
;
1613 bmp
->biClrImportant
= bi
->biClrImportant
;
1615 hpal
= PALETTE_AllocPalette(palMode
, ColorCount
, (ULONG
*)lpRGB
,
1620 bmp
->ppal
= PALETTE_ShareLockPalette(hpal
);
1621 /* Lazy delete hpal, it will be freed at surface release */
1622 GreDeleteObject(hpal
);
1624 // Clean up in case of errors
1626 if (!res
|| !bmp
|| !bm
.bmBits
)
1628 DPRINT("got an error res=%08x, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1631 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1634 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1639 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1647 SURFACE_FreeSurfaceByHandle(res
);
1652 if (lpRGB
!= bmi
->bmiColors
&& lpRGB
)
1654 ExFreePoolWithTag((PVOID
)lpRGB
, TAG_COLORMAP
);
1659 SURFACE_UnlockSurface(bmp
);
1662 // Return BITMAP handle and storage location
1663 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1671 /***********************************************************************
1674 * Get the info from a bitmap header.
1675 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
1679 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
1680 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
1682 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1684 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
1685 *width
= core
->bcWidth
;
1686 *height
= core
->bcHeight
;
1687 *planes
= core
->bcPlanes
;
1688 *bpp
= core
->bcBitCount
;
1693 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* assume BITMAPINFOHEADER */
1695 *width
= header
->biWidth
;
1696 *height
= header
->biHeight
;
1697 *planes
= header
->biPlanes
;
1698 *bpp
= header
->biBitCount
;
1699 *compr
= header
->biCompression
;
1700 *size
= header
->biSizeImage
;
1703 DPRINT1("(%d): unknown/wrong size for header\n", header
->biSize
);
1707 /***********************************************************************
1708 * DIB_GetDIBWidthBytes
1710 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1711 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
1712 * 11/16/1999 (RJJ) lifted from wine
1714 INT FASTCALL
DIB_GetDIBWidthBytes(INT width
, INT depth
)
1716 return ((width
* depth
+ 31) & ~31) >> 3;
1719 /***********************************************************************
1720 * DIB_GetDIBImageBytes
1722 * Return the number of bytes used to hold the image in a DIB bitmap.
1723 * 11/16/1999 (RJJ) lifted from wine
1726 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1728 return DIB_GetDIBWidthBytes(width
, depth
) * (height
< 0 ? -height
: height
);
1731 /***********************************************************************
1732 * DIB_BitmapInfoSize
1734 * Return the size of the bitmap info structure including color table.
1735 * 11/16/1999 (RJJ) lifted from wine
1738 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1740 unsigned int colors
, size
, masks
= 0;
1742 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1744 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
1745 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1746 return sizeof(BITMAPCOREHEADER
) + colors
*
1747 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1749 else /* assume BITMAPINFOHEADER */
1751 colors
= info
->bmiHeader
.biClrUsed
;
1752 if (colors
> 256) colors
= 256;
1753 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
1754 colors
= 1 << info
->bmiHeader
.biBitCount
;
1755 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
1756 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
1757 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1763 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1770 palGDI
= PALETTE_LockPalette(dc
->dclevel
.hpal
);
1777 if (palGDI
->Mode
!= PAL_INDEXED
)
1779 PALETTE_UnlockPalette(palGDI
);
1783 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1784 if (lpbmi
->bmiHeader
.biClrUsed
)
1786 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1789 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1792 PALETTE_UnlockPalette(palGDI
);
1796 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1798 for (i
= 0; i
< nNumColors
; i
++)
1800 if (*lpIndex
< palGDI
->NumColors
)
1802 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1803 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1804 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1808 lpRGB
[i
].rgbRed
= 0;
1809 lpRGB
[i
].rgbGreen
= 0;
1810 lpRGB
[i
].rgbBlue
= 0;
1812 lpRGB
[i
].rgbReserved
= 0;
1815 PALETTE_UnlockPalette(palGDI
);
1822 BuildDIBPalette(CONST BITMAPINFO
*bmi
, PINT paletteType
)
1827 ULONG RedMask
, GreenMask
, BlueMask
;
1828 PDWORD pdwColors
= (PDWORD
)((PBYTE
)bmi
+ bmi
->bmiHeader
.biSize
);
1830 // Determine Bits Per Pixel
1831 bits
= bmi
->bmiHeader
.biBitCount
;
1833 // Determine paletteType from Bits Per Pixel
1836 *paletteType
= PAL_INDEXED
;
1837 RedMask
= GreenMask
= BlueMask
= 0;
1839 else if (bmi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
1841 *paletteType
= PAL_BITFIELDS
;
1842 RedMask
= pdwColors
[0];
1843 GreenMask
= pdwColors
[1];
1844 BlueMask
= pdwColors
[2];
1846 else if (bits
== 15)
1848 *paletteType
= PAL_BITFIELDS
;
1853 else if (bits
== 16)
1855 *paletteType
= PAL_BITFIELDS
;
1862 *paletteType
= PAL_BGR
;
1864 GreenMask
= 0x00ff00;
1865 BlueMask
= 0x0000ff;
1868 if (bmi
->bmiHeader
.biClrUsed
== 0)
1870 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1874 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1877 if (PAL_INDEXED
== *paletteType
)
1879 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)pdwColors
);
1883 hPal
= PALETTE_AllocPalette(*paletteType
, ColorCount
,
1885 RedMask
, GreenMask
, BlueMask
);
1893 GetBMIColor(CONST BITMAPINFO
* pbmi
, INT i
)
1897 if(pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1899 /* BITMAPCOREINFO holds RGBTRIPLE values */
1900 size
= sizeof(RGBTRIPLE
);
1904 size
= sizeof(RGBQUAD
);
1906 memcpy(&dwRet
, (PBYTE
)pbmi
+ pbmi
->bmiHeader
.biSize
+ i
*size
, size
);
1912 SetBMIColor(CONST BITMAPINFO
* pbmi
, DWORD
* color
, INT i
)
1914 PVOID pvColors
= ((PBYTE
)pbmi
+ pbmi
->bmiHeader
.biSize
);
1915 if(pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1917 RGBTRIPLE
*pColor
= pvColors
;
1918 pColor
[i
] = *(RGBTRIPLE
*)color
;
1922 RGBQUAD
*pColor
= pvColors
;
1923 pColor
[i
] = *(RGBQUAD
*)color
;
1929 ProbeAndConvertToBitmapV5Info(
1930 OUT PBITMAPV5INFO pbmiDst
,
1931 IN CONST BITMAPINFO
* pbmiUnsafe
,
1932 IN DWORD dwColorUse
,
1937 PBITMAPV5HEADER pbmhDst
= &pbmiDst
->bmiHeader
;
1939 /* Get the size and probe */
1940 ProbeForRead(&pbmiUnsafe
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1941 dwSize
= pbmiUnsafe
->bmiHeader
.biSize
;
1942 /* At least dwSize bytes must be valids */
1943 ProbeForRead(pbmiUnsafe
, max(dwSize
, MaxSize
), 1);
1945 ProbeForRead(pbmiUnsafe
, DIB_BitmapInfoSize(pbmiUnsafe
, dwColorUse
), 1);
1947 /* Check the size */
1948 // FIXME: are intermediate sizes allowed? As what are they interpreted?
1949 // make sure we don't use a too big dwSize later
1950 if (dwSize
!= sizeof(BITMAPCOREHEADER
) &&
1951 dwSize
!= sizeof(BITMAPINFOHEADER
) &&
1952 dwSize
!= sizeof(BITMAPV4HEADER
) &&
1953 dwSize
!= sizeof(BITMAPV5HEADER
))
1955 return STATUS_INVALID_PARAMETER
;
1958 if (dwSize
== sizeof(BITMAPCOREHEADER
))
1960 PBITMAPCOREHEADER pbch
= (PBITMAPCOREHEADER
)pbmiUnsafe
;
1962 /* Manually copy the fields that are present */
1963 pbmhDst
->bV5Width
= pbch
->bcWidth
;
1964 pbmhDst
->bV5Height
= pbch
->bcHeight
;
1965 pbmhDst
->bV5Planes
= pbch
->bcPlanes
;
1966 pbmhDst
->bV5BitCount
= pbch
->bcBitCount
;
1968 /* Set some default values */
1969 pbmhDst
->bV5Compression
= BI_RGB
;
1970 pbmhDst
->bV5SizeImage
= DIB_GetDIBImageBytes(pbch
->bcWidth
,
1972 pbch
->bcPlanes
*pbch
->bcBitCount
) ;
1973 pbmhDst
->bV5XPelsPerMeter
= 72;
1974 pbmhDst
->bV5YPelsPerMeter
= 72;
1975 pbmhDst
->bV5ClrUsed
= 0;
1976 pbmhDst
->bV5ClrImportant
= 0;
1980 /* Copy valid fields */
1981 memcpy(pbmiDst
, pbmiUnsafe
, dwSize
);
1982 if(!pbmhDst
->bV5SizeImage
)
1983 pbmhDst
->bV5SizeImage
= DIB_GetDIBImageBytes(pbmhDst
->bV5Width
,
1985 pbmhDst
->bV5Planes
*pbmhDst
->bV5BitCount
) ;
1987 if(dwSize
< sizeof(BITMAPV5HEADER
))
1989 /* Zero out the rest of the V5 header */
1990 memset((char*)pbmiDst
+ dwSize
, 0, sizeof(BITMAPV5HEADER
) - dwSize
);
1993 pbmhDst
->bV5Size
= sizeof(BITMAPV5HEADER
);
1996 if (dwSize
< sizeof(BITMAPV4HEADER
))
1998 if (pbmhDst
->bV5Compression
== BI_BITFIELDS
)
2000 pbmhDst
->bV5RedMask
= GetBMIColor(pbmiUnsafe
, 0);
2001 pbmhDst
->bV5GreenMask
= GetBMIColor(pbmiUnsafe
, 1);
2002 pbmhDst
->bV5BlueMask
= GetBMIColor(pbmiUnsafe
, 2);
2003 pbmhDst
->bV5AlphaMask
= 0;
2004 pbmhDst
->bV5ClrUsed
= 0;
2007 // pbmhDst->bV5CSType;
2008 // pbmhDst->bV5Endpoints;
2009 // pbmhDst->bV5GammaRed;
2010 // pbmhDst->bV5GammaGreen;
2011 // pbmhDst->bV5GammaBlue;
2014 if (dwSize
< sizeof(BITMAPV5HEADER
))
2016 // pbmhDst->bV5Intent;
2017 // pbmhDst->bV5ProfileData;
2018 // pbmhDst->bV5ProfileSize;
2019 // pbmhDst->bV5Reserved;
2022 ulWidthBytes
= ((pbmhDst
->bV5Width
* pbmhDst
->bV5Planes
*
2023 pbmhDst
->bV5BitCount
+ 31) & ~31) / 8;
2025 if (pbmhDst
->bV5SizeImage
== 0)
2026 pbmhDst
->bV5SizeImage
= abs(ulWidthBytes
* pbmhDst
->bV5Height
);
2028 if (pbmhDst
->bV5ClrUsed
== 0)
2030 switch(pbmhDst
->bV5BitCount
)
2033 pbmhDst
->bV5ClrUsed
= 2;
2036 pbmhDst
->bV5ClrUsed
= 16;
2039 pbmhDst
->bV5ClrUsed
= 256;
2042 pbmhDst
->bV5ClrUsed
= 0;
2047 if (pbmhDst
->bV5Planes
!= 1)
2049 return STATUS_INVALID_PARAMETER
;
2052 if (pbmhDst
->bV5BitCount
!= 0 && pbmhDst
->bV5BitCount
!= 1 &&
2053 pbmhDst
->bV5BitCount
!= 4 && pbmhDst
->bV5BitCount
!= 8 &&
2054 pbmhDst
->bV5BitCount
!= 16 && pbmhDst
->bV5BitCount
!= 24 &&
2055 pbmhDst
->bV5BitCount
!= 32)
2057 DPRINT("Invalid bit count: %d\n", pbmhDst
->bV5BitCount
);
2058 return STATUS_INVALID_PARAMETER
;
2061 if ((pbmhDst
->bV5BitCount
== 0 &&
2062 pbmhDst
->bV5Compression
!= BI_JPEG
&& pbmhDst
->bV5Compression
!= BI_PNG
))
2064 DPRINT("Bit count 0 is invalid for compression %d.\n", pbmhDst
->bV5Compression
);
2065 return STATUS_INVALID_PARAMETER
;
2068 if (pbmhDst
->bV5Compression
== BI_BITFIELDS
&&
2069 pbmhDst
->bV5BitCount
!= 16 && pbmhDst
->bV5BitCount
!= 32)
2071 DPRINT("Bit count %d is invalid for compression BI_BITFIELDS.\n", pbmhDst
->bV5BitCount
);
2072 return STATUS_INVALID_PARAMETER
;
2076 if(pbmhDst
->bV5ClrUsed
)
2079 if(dwColorUse
== DIB_PAL_COLORS
)
2081 RtlCopyMemory(pbmiDst
->bmiColors
,
2082 pbmiUnsafe
->bmiColors
,
2083 pbmhDst
->bV5ClrUsed
* sizeof(WORD
));
2087 for(i
= 0; i
< pbmhDst
->bV5ClrUsed
; i
++)
2089 ((DWORD
*)pbmiDst
->bmiColors
)[i
] = GetBMIColor(pbmiUnsafe
, i
);
2094 return STATUS_SUCCESS
;
2099 GetBMIFromBitmapV5Info(IN PBITMAPV5INFO pbmiSrc
,
2100 OUT PBITMAPINFO pbmiDst
,
2101 IN DWORD dwColorUse
)
2103 if(pbmiDst
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
2105 /* Manually set value */
2106 BITMAPCOREHEADER
* pbmhCore
= (BITMAPCOREHEADER
*)&pbmiDst
->bmiHeader
;
2107 pbmhCore
->bcWidth
= pbmiSrc
->bmiHeader
.bV5Width
;
2108 pbmhCore
->bcHeight
= pbmiSrc
->bmiHeader
.bV5Height
;
2109 pbmhCore
->bcPlanes
= pbmiSrc
->bmiHeader
.bV5Planes
;
2110 pbmhCore
->bcBitCount
= pbmiSrc
->bmiHeader
.bV5BitCount
;
2114 /* Copy valid Fields, keep bmiHeader.biSize safe */
2115 RtlCopyMemory(&pbmiDst
->bmiHeader
.biWidth
,
2116 &pbmiSrc
->bmiHeader
.bV5Width
,
2117 pbmiDst
->bmiHeader
.biSize
- sizeof(DWORD
));
2119 if((pbmiDst
->bmiHeader
.biSize
< sizeof(BITMAPV4HEADER
)) &&
2120 (pbmiSrc
->bmiHeader
.bV5Compression
== BI_BITFIELDS
))
2122 /* Masks are already set in V4 and V5 headers */
2123 SetBMIColor(pbmiDst
, &pbmiSrc
->bmiHeader
.bV5RedMask
, 0);
2124 SetBMIColor(pbmiDst
, &pbmiSrc
->bmiHeader
.bV5GreenMask
, 1);
2125 SetBMIColor(pbmiDst
, &pbmiSrc
->bmiHeader
.bV5BlueMask
, 2);
2132 cColorsUsed
= pbmiSrc
->bmiHeader
.bV5ClrUsed
;
2133 if (cColorsUsed
== 0 && pbmiSrc
->bmiHeader
.bV5BitCount
<= 8)
2134 cColorsUsed
= (1 << pbmiSrc
->bmiHeader
.bV5BitCount
);
2136 if(dwColorUse
== DIB_PAL_COLORS
)
2138 RtlCopyMemory(pbmiDst
->bmiColors
,
2140 cColorsUsed
* sizeof(WORD
));
2144 for(i
= 0; i
< cColorsUsed
; i
++)
2146 SetBMIColor(pbmiDst
, (DWORD
*)pbmiSrc
->bmiColors
+ i
, i
);