2 * PROJECT: ReactOS win32 kernel mode subsystem
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: subsystems/win32/win32k/objects/dibobj.c
5 * PURPOSE: Dib object functions
14 static const RGBQUAD EGAColorsQuads
[16] =
16 /* rgbBlue, rgbGreen, rgbRed, rgbReserved */
17 { 0x00, 0x00, 0x00, 0x00 },
18 { 0x00, 0x00, 0x80, 0x00 },
19 { 0x00, 0x80, 0x00, 0x00 },
20 { 0x00, 0x80, 0x80, 0x00 },
21 { 0x80, 0x00, 0x00, 0x00 },
22 { 0x80, 0x00, 0x80, 0x00 },
23 { 0x80, 0x80, 0x00, 0x00 },
24 { 0x80, 0x80, 0x80, 0x00 },
25 { 0xc0, 0xc0, 0xc0, 0x00 },
26 { 0x00, 0x00, 0xff, 0x00 },
27 { 0x00, 0xff, 0x00, 0x00 },
28 { 0x00, 0xff, 0xff, 0x00 },
29 { 0xff, 0x00, 0x00, 0x00 },
30 { 0xff, 0x00, 0xff, 0x00 },
31 { 0xff, 0xff, 0x00, 0x00 },
32 { 0xff, 0xff, 0xff, 0x00 }
35 static const RGBTRIPLE EGAColorsTriples
[16] =
37 /* rgbBlue, rgbGreen, rgbRed */
56 static const RGBQUAD DefLogPaletteQuads
[20] = /* Copy of Default Logical Palette */
58 /* rgbBlue, rgbGreen, rgbRed, rgbReserved */
59 { 0x00, 0x00, 0x00, 0x00 },
60 { 0x00, 0x00, 0x80, 0x00 },
61 { 0x00, 0x80, 0x00, 0x00 },
62 { 0x00, 0x80, 0x80, 0x00 },
63 { 0x80, 0x00, 0x00, 0x00 },
64 { 0x80, 0x00, 0x80, 0x00 },
65 { 0x80, 0x80, 0x00, 0x00 },
66 { 0xc0, 0xc0, 0xc0, 0x00 },
67 { 0xc0, 0xdc, 0xc0, 0x00 },
68 { 0xf0, 0xca, 0xa6, 0x00 },
69 { 0xf0, 0xfb, 0xff, 0x00 },
70 { 0xa4, 0xa0, 0xa0, 0x00 },
71 { 0x80, 0x80, 0x80, 0x00 },
72 { 0x00, 0x00, 0xf0, 0x00 },
73 { 0x00, 0xff, 0x00, 0x00 },
74 { 0x00, 0xff, 0xff, 0x00 },
75 { 0xff, 0x00, 0x00, 0x00 },
76 { 0xff, 0x00, 0xff, 0x00 },
77 { 0xff, 0xff, 0x00, 0x00 },
78 { 0xff, 0xff, 0xff, 0x00 }
81 static const RGBQUAD DefLogPaletteTriples
[20] = /* Copy of Default Logical Palette */
83 /* rgbBlue, rgbGreen, rgbRed, rgbReserved */
100 { 0xff, 0x00, 0x00 },
101 { 0xff, 0x00, 0xff },
102 { 0xff, 0xff, 0x00 },
109 _In_
const BITMAPINFO
*pbmi
,
116 /* Check if the colors are indexed */
117 if (pbmi
->bmiHeader
.biBitCount
<= 8)
119 /* We create a "full" palette */
120 cColors
= 1 << pbmi
->bmiHeader
.biBitCount
;
122 /* Allocate the palette */
123 ppal
= PALETTE_AllocPalette(PAL_INDEXED
,
130 /* Check if the BITMAPINFO specifies how many colors to use */
131 if (pbmi
->bmiHeader
.biClrUsed
!= 0)
133 /* This is how many colors we can actually process */
134 cColors
= min(cColors
, pbmi
->bmiHeader
.biClrUsed
);
137 /* Check how to use the colors */
138 if (iUsage
== DIB_PAL_COLORS
)
142 /* The colors are an array of WORD indices into the DC palette */
143 PWORD pwColors
= (PWORD
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
145 /* Use the DCs palette or, if no DC is given, the default one */
146 PPALETTE ppalDC
= pdc
? pdc
->dclevel
.ppal
: gppalDefault
;
148 /* Loop all color indices in the DIB */
149 for (i
= 0; i
< cColors
; i
++)
151 /* Get the palette index and handle wraparound when exceeding
152 the number of colors in the DC palette */
153 WORD wIndex
= pwColors
[i
] % ppalDC
->NumColors
;
155 /* USe the RGB value from the DC palette */
156 crColor
= PALETTE_ulGetRGBColorFromIndex(ppalDC
, wIndex
);
157 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
160 else if (iUsage
== DIB_PAL_BRUSHHACK
)
162 /* The colors are an array of WORD indices into the DC palette */
163 PWORD pwColors
= (PWORD
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
165 /* Loop all color indices in the DIB */
166 for (i
= 0; i
< cColors
; i
++)
168 /* Set the index directly as the RGB color, the real palette
169 containing RGB values will be calculated when the brush is
171 PALETTE_vSetRGBColorForIndex(ppal
, i
, pwColors
[i
]);
174 /* Mark the palette as a brush hack palette */
175 ppal
->flFlags
|= PAL_BRUSHHACK
;
177 // else if (iUsage == 2)
179 // FIXME: this one is undocumented
182 else // if (iUsage == DIB_RGB_COLORS)
184 /* The colors are an array of RGBQUAD values */
185 RGBQUAD
*prgb
= (RGBQUAD
*)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
187 // FIXME: do we need to handle PALETTEINDEX / PALETTERGB macro?
189 /* Loop all color indices in the DIB */
190 for (i
= 0; i
< cColors
; i
++)
192 /* Get the color value and translate it to a COLORREF */
193 RGBQUAD rgb
= prgb
[i
];
194 COLORREF crColor
= RGB(rgb
.rgbRed
, rgb
.rgbGreen
, rgb
.rgbBlue
);
196 /* Set the RGB value in the palette */
197 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
203 /* This is a bitfield / RGB palette */
204 ULONG flRedMask
, flGreenMask
, flBlueMask
;
206 /* Check if the DIB contains bitfield values */
207 if (pbmi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
209 /* Check if we have a v4/v5 header */
210 if (pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPV4HEADER
))
212 /* The masks are dedicated fields in the header */
213 PBITMAPV4HEADER pbmV4Header
= (PBITMAPV4HEADER
)&pbmi
->bmiHeader
;
214 flRedMask
= pbmV4Header
->bV4RedMask
;
215 flGreenMask
= pbmV4Header
->bV4GreenMask
;
216 flBlueMask
= pbmV4Header
->bV4BlueMask
;
220 /* The masks are the first 3 values in the DIB color table */
221 PDWORD pdwColors
= (PVOID
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
222 flRedMask
= pdwColors
[0];
223 flGreenMask
= pdwColors
[1];
224 flBlueMask
= pdwColors
[2];
229 /* Check what bit depth we have. Note: optimization flags are
230 calculated in PALETTE_AllocPalette() */
231 if (pbmi
->bmiHeader
.biBitCount
== 16)
233 /* This is an RGB 555 palette */
235 flGreenMask
= 0x03E0;
240 /* This is an RGB 888 palette */
241 flRedMask
= 0xFF0000;
242 flGreenMask
= 0x00FF00;
243 flBlueMask
= 0x0000FF;
247 /* Allocate the bitfield palette */
248 ppal
= PALETTE_AllocPalette(PAL_BITFIELDS
,
256 /* We're done, return the palette */
267 CONST RGBQUAD
*Colors
)
275 if (!(dc
= DC_LockDc(hDC
))) return 0;
276 if (dc
->dctype
== DC_TYPE_INFO
)
282 psurf
= dc
->dclevel
.pSurface
;
286 EngSetLastError(ERROR_INVALID_PARAMETER
);
290 if (psurf
->hSecure
== NULL
)
293 EngSetLastError(ERROR_INVALID_PARAMETER
);
297 biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
298 if ((biBitCount
<= 8) && (StartIndex
< (1UL << biBitCount
)))
300 if (StartIndex
+ Entries
> (1UL << biBitCount
))
301 Entries
= (1 << biBitCount
) - StartIndex
;
303 if (psurf
->ppal
== NULL
)
306 EngSetLastError(ERROR_INVALID_HANDLE
);
310 PalGDI
= psurf
->ppal
;
312 for (Index
= StartIndex
;
313 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
316 PalGDI
->IndexedColors
[Index
].peRed
= Colors
[Index
- StartIndex
].rgbRed
;
317 PalGDI
->IndexedColors
[Index
].peGreen
= Colors
[Index
- StartIndex
].rgbGreen
;
318 PalGDI
->IndexedColors
[Index
].peBlue
= Colors
[Index
- StartIndex
].rgbBlue
;
324 /* Mark the brushes invalid */
325 dc
->pdcattr
->ulDirty_
|= DIRTY_FILL
|DIRTY_LINE
|DIRTY_BACKGROUND
|DIRTY_TEXT
;
343 UINT Index
, Count
= 0;
345 if (!(dc
= DC_LockDc(hDC
))) return 0;
346 if (dc
->dctype
== DC_TYPE_INFO
)
352 psurf
= dc
->dclevel
.pSurface
;
356 EngSetLastError(ERROR_INVALID_PARAMETER
);
360 if (psurf
->hSecure
== NULL
)
363 EngSetLastError(ERROR_INVALID_PARAMETER
);
370 if (ppal
->flFlags
& PAL_INDEXED
)
373 for (Index
= StartIndex
;
374 Index
< StartIndex
+ Entries
&& Index
< ppal
->NumColors
;
377 Colors
[Index
- StartIndex
].rgbRed
= ppal
->IndexedColors
[Index
].peRed
;
378 Colors
[Index
- StartIndex
].rgbGreen
= ppal
->IndexedColors
[Index
].peGreen
;
379 Colors
[Index
- StartIndex
].rgbBlue
= ppal
->IndexedColors
[Index
].peBlue
;
380 Colors
[Index
- StartIndex
].rgbReserved
= 0;
390 // Converts a DIB to a device-dependent bitmap
399 CONST BITMAPINFO
*bmi
,
402 HBITMAP SourceBitmap
;
403 PSURFACE psurfDst
, psurfSrc
;
408 PPALETTE ppalDIB
= 0;
412 SourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
415 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
416 bmi
->bmiHeader
.biCompression
),
417 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
418 bmi
->bmiHeader
.biSizeImage
,
423 DPRINT1("Error: Could not create a bitmap.\n");
424 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
428 psurfDst
= SURFACE_ShareLockSurface(hBitmap
);
429 psurfSrc
= SURFACE_ShareLockSurface(SourceBitmap
);
431 if(!(psurfSrc
&& psurfDst
))
433 DPRINT1("Error: Could not lock surfaces\n");
437 /* Create a palette for the DIB */
438 ppalDIB
= CreateDIBPalette(bmi
, DC
, ColorUse
);
441 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
445 /* Initialize EXLATEOBJ */
446 EXLATEOBJ_vInitialize(&exlo
,
449 RGB(0xff, 0xff, 0xff),
450 RGB(0xff, 0xff, 0xff), //DC->pdcattr->crBackgroundClr,
451 0); // DC->pdcattr->crForegroundClr);
453 rcDst
.top
= StartScan
;
455 rcDst
.bottom
= rcDst
.top
+ ScanLines
;
456 rcDst
.right
= psurfDst
->SurfObj
.sizlBitmap
.cx
;
460 result
= IntEngCopyBits(&psurfDst
->SurfObj
,
469 EXLATEOBJ_vCleanup(&exlo
);
472 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
473 if(psurfSrc
) SURFACE_ShareUnlockSurface(psurfSrc
);
474 if(psurfDst
) SURFACE_ShareUnlockSurface(psurfDst
);
475 GreDeleteObject(SourceBitmap
);
483 NtGdiSetDIBitsToDeviceInternal(
498 IN BOOL bTransformCoordinates
,
499 IN OPTIONAL HANDLE hcmXform
)
502 NTSTATUS Status
= STATUS_SUCCESS
;
504 HBITMAP hSourceBitmap
= NULL
;
505 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
;
512 PPALETTE ppalDIB
= NULL
;
513 LPBITMAPINFO pbmiSafe
;
517 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjMaxInfo
, 'pmTG');
518 if (!pbmiSafe
) return 0;
522 ProbeForRead(bmi
, cjMaxInfo
, 1);
523 ProbeForRead(Bits
, cjMaxBits
, 1);
524 RtlCopyMemory(pbmiSafe
, bmi
, cjMaxInfo
);
527 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
529 Status
= _SEH2_GetExceptionCode();
533 if (!NT_SUCCESS(Status
))
538 pDC
= DC_LockDc(hDC
);
541 EngSetLastError(ERROR_INVALID_HANDLE
);
544 if (pDC
->dctype
== DC_TYPE_INFO
)
550 pSurf
= pDC
->dclevel
.pSurface
;
552 pDestSurf
= pSurf
? &pSurf
->SurfObj
: NULL
;
554 ScanLines
= min(ScanLines
, abs(bmi
->bmiHeader
.biHeight
) - StartScan
);
558 if (bTransformCoordinates
)
560 IntLPtoDP(pDC
, (LPPOINT
)&rcDest
, 2);
562 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
563 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
564 rcDest
.right
= rcDest
.left
+ Width
;
565 rcDest
.bottom
= rcDest
.top
+ Height
;
566 rcDest
.top
+= StartScan
;
571 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
572 SourceSize
.cy
= ScanLines
;
574 //DIBWidth = WIDTH_BYTES_ALIGN32(SourceSize.cx, bmi->bmiHeader.biBitCount);
576 hSourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
579 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
580 bmi
->bmiHeader
.biCompression
),
581 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
582 bmi
->bmiHeader
.biSizeImage
,
588 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
589 Status
= STATUS_NO_MEMORY
;
593 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
596 Status
= STATUS_UNSUCCESSFUL
;
602 /* Create a palette for the DIB */
603 ppalDIB
= CreateDIBPalette(bmi
, pDC
, ColorUse
);
606 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
607 Status
= STATUS_NO_MEMORY
;
611 /* Initialize EXLATEOBJ */
612 EXLATEOBJ_vInitialize(&exlo
,
615 RGB(0xff, 0xff, 0xff),
616 pDC
->pdcattr
->crBackgroundClr
,
617 pDC
->pdcattr
->crForegroundClr
);
620 DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
621 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
622 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
623 Status
= IntEngBitBlt(pDestSurf
,
626 pDC
->rosdc
.CombinedClip
,
633 ROP4_FROM_INDEX(R3_OPINDEX_SRCCOPY
));
635 /* Cleanup EXLATEOBJ */
636 EXLATEOBJ_vCleanup(&exlo
);
639 if (NT_SUCCESS(Status
))
644 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
646 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
647 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
650 ExFreePool(pbmiSafe
);
655 /* Converts a device-dependent bitmap to a DIB */
658 NtGdiGetDIBitsInternal(
669 BITMAPCOREINFO
* pbmci
= NULL
;
670 PSURFACE psurf
= NULL
;
677 RGBTRIPLE
* rgbTriples
;
680 NTSTATUS Status
= STATUS_SUCCESS
;
682 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
684 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
689 /* Probe for read and write */
690 ProbeForRead(Info
, MaxInfo
, 1);
691 ProbeForWrite(Info
, MaxInfo
, 1);
692 if (Bits
) ProbeForWrite(Bits
, MaxBits
, 1);
694 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
696 Status
= _SEH2_GetExceptionCode();
700 if (!NT_SUCCESS(Status
))
705 colorPtr
= (LPBYTE
)Info
+ Info
->bmiHeader
.biSize
;
706 rgbTriples
= colorPtr
;
709 bitmap_type
= DIB_GetBitmapInfo(&Info
->bmiHeader
,
716 if(bitmap_type
== -1)
718 DPRINT("Wrong bitmap format\n");
719 EngSetLastError(ERROR_INVALID_PARAMETER
);
722 else if(bitmap_type
== 0)
724 /* We need a BITMAPINFO to create a DIB, but we have to fill
725 * the BITMAPCOREINFO we're provided */
726 pbmci
= (BITMAPCOREINFO
*)Info
;
727 Info
= DIB_ConvertBitmapInfo((BITMAPINFO
*)pbmci
, Usage
);
730 DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
733 rgbQuads
= Info
->bmiColors
;
736 pDC
= DC_LockDc(hDC
);
737 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
743 /* Get a pointer to the source bitmap object */
744 psurf
= SURFACE_ShareLockSurface(hBitmap
);
751 /* Fill in the structure */
754 case 0: /* Only info */
757 pbmci
->bmciHeader
.bcWidth
= (WORD
)psurf
->SurfObj
.sizlBitmap
.cx
;
758 pbmci
->bmciHeader
.bcHeight
= (WORD
)((psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
759 -psurf
->SurfObj
.sizlBitmap
.cy
:
760 psurf
->SurfObj
.sizlBitmap
.cy
);
761 pbmci
->bmciHeader
.bcPlanes
= 1;
762 pbmci
->bmciHeader
.bcBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
764 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
765 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
766 -psurf
->SurfObj
.sizlBitmap
.cy
:
767 psurf
->SurfObj
.sizlBitmap
.cy
;;
768 Info
->bmiHeader
.biPlanes
= 1;
769 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
770 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
771 Info
->bmiHeader
.biHeight
,
772 Info
->bmiHeader
.biBitCount
);
775 switch(Info
->bmiHeader
.biBitCount
)
779 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
782 Info
->bmiHeader
.biCompression
= BI_RGB
;
786 else if(Info
->bmiHeader
.biBitCount
> 8)
788 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
792 Info
->bmiHeader
.biCompression
= BI_RGB
;
794 Info
->bmiHeader
.biXPelsPerMeter
= 0;
795 Info
->bmiHeader
.biYPelsPerMeter
= 0;
796 Info
->bmiHeader
.biClrUsed
= 0;
797 Info
->bmiHeader
.biClrImportant
= 0;
798 ScanLines
= abs(Info
->bmiHeader
.biHeight
);
804 Info
->bmiHeader
.biClrUsed
= 0;
806 /* If the bitmap if a DIB section and has the same format than what
807 * we're asked, go ahead! */
808 if((psurf
->hSecure
) &&
809 (BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bpp
))
811 if(Usage
== DIB_RGB_COLORS
)
813 ULONG colors
= min(psurf
->ppal
->NumColors
, 256);
817 for(i
= 0; i
< colors
; i
++)
819 rgbTriples
[i
].rgbtRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
820 rgbTriples
[i
].rgbtGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
821 rgbTriples
[i
].rgbtBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
824 if(colors
!= 256) Info
->bmiHeader
.biClrUsed
= colors
;
825 for(i
= 0; i
< colors
; i
++)
827 rgbQuads
[i
].rgbRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
828 rgbQuads
[i
].rgbGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
829 rgbQuads
[i
].rgbBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
834 for(i
= 0; i
< 256; i
++)
836 if(pbmci
) ((WORD
*)rgbTriples
)[i
] = i
;
837 ((WORD
*)rgbQuads
)[i
] = i
;
843 if(Usage
== DIB_PAL_COLORS
)
845 for(i
= 0; i
< 256; i
++)
847 if(pbmci
) ((WORD
*)rgbTriples
)[i
] = i
;
848 ((WORD
*)rgbQuads
)[i
] = i
;
851 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
))
853 /* For color DDBs in native depth (mono DDBs always have
854 a black/white palette):
855 Generate the color map from the selected palette */
856 PPALETTE pDcPal
= PALETTE_ShareLockPalette(pDC
->dclevel
.hpal
);
862 for (i
= 0; i
< pDcPal
->NumColors
; i
++)
866 rgbTriples
[i
].rgbtRed
= pDcPal
->IndexedColors
[i
].peRed
;
867 rgbTriples
[i
].rgbtGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
868 rgbTriples
[i
].rgbtBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
871 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
872 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
873 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
874 rgbQuads
[i
].rgbReserved
= 0;
876 PALETTE_ShareUnlockPalette(pDcPal
);
885 rgbTriples
[0].rgbtRed
= rgbTriples
[0].rgbtGreen
=
886 rgbTriples
[0].rgbtBlue
= 0;
887 rgbTriples
[1].rgbtRed
= rgbTriples
[1].rgbtGreen
=
888 rgbTriples
[1].rgbtBlue
= 0xff;
890 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
=
891 rgbQuads
[0].rgbBlue
= 0;
892 rgbQuads
[0].rgbReserved
= 0;
893 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
=
894 rgbQuads
[1].rgbBlue
= 0xff;
895 rgbQuads
[1].rgbReserved
= 0;
900 RtlCopyMemory(rgbTriples
, EGAColorsTriples
, sizeof(EGAColorsTriples
));
901 RtlCopyMemory(rgbQuads
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
911 RGBTRIPLE
*colorTriple
;
913 RtlCopyMemory(rgbTriples
, DefLogPaletteTriples
,
914 10 * sizeof(RGBTRIPLE
));
915 RtlCopyMemory(rgbTriples
+ 246, DefLogPaletteTriples
+ 10,
916 10 * sizeof(RGBTRIPLE
));
917 colorTriple
= rgbTriples
+ 10;
918 for(r
= 0; r
<= 5; r
++) /* FIXME */
920 for(g
= 0; g
<= 5; g
++)
922 for(b
= 0; b
<= 5; b
++)
924 colorTriple
->rgbtRed
= (r
* 0xff) / 5;
925 colorTriple
->rgbtGreen
= (g
* 0xff) / 5;
926 colorTriple
->rgbtBlue
= (b
* 0xff) / 5;
932 memcpy(rgbQuads
, DefLogPaletteQuads
,
933 10 * sizeof(RGBQUAD
));
934 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10,
935 10 * sizeof(RGBQUAD
));
936 color
= rgbQuads
+ 10;
937 for(r
= 0; r
<= 5; r
++) /* FIXME */
939 for(g
= 0; g
<= 5; g
++)
941 for(b
= 0; b
<= 5; b
++)
943 color
->rgbRed
= (r
* 0xff) / 5;
944 color
->rgbGreen
= (g
* 0xff) / 5;
945 color
->rgbBlue
= (b
* 0xff) / 5;
946 color
->rgbReserved
= 0;
958 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
960 ((PDWORD
)Info
->bmiColors
)[0] = 0x7c00;
961 ((PDWORD
)Info
->bmiColors
)[1] = 0x03e0;
962 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
967 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
971 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
972 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
973 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
977 ((PDWORD
)Info
->bmiColors
)[0] = 0xf800;
978 ((PDWORD
)Info
->bmiColors
)[1] = 0x07e0;
979 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
986 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
990 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
991 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
992 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
996 ((PDWORD
)Info
->bmiColors
)[0] = 0xff0000;
997 ((PDWORD
)Info
->bmiColors
)[1] = 0x00ff00;
998 ((PDWORD
)Info
->bmiColors
)[2] = 0x0000ff;
1003 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(width
, height
, bpp
);
1005 if(Bits
&& ScanLines
)
1007 /* Create a DIBSECTION, blt it, profit */
1016 if (StartScan
> (ULONG
)psurf
->SurfObj
.sizlBitmap
.cy
)
1023 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
1027 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
1028 Info
->bmiHeader
.biHeight
= (height
< 0) ?
1029 -(LONG
)ScanLines
: ScanLines
;
1030 /* Create the DIB */
1031 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
1033 Info
->bmiHeader
.biWidth
= width
;
1034 Info
->bmiHeader
.biHeight
= height
;
1038 DPRINT1("Unable to create a DIB Section!\n");
1039 EngSetLastError(ERROR_INVALID_PARAMETER
);
1044 psurfDest
= SURFACE_ShareLockSurface(hBmpDest
);
1048 rcDest
.bottom
= ScanLines
;
1049 rcDest
.right
= psurf
->SurfObj
.sizlBitmap
.cx
;
1057 if(ScanLines
<= StartScan
)
1060 SURFACE_ShareUnlockSurface(psurfDest
);
1061 GreDeleteObject(hBmpDest
);
1065 ScanLines
-= StartScan
;
1069 srcPoint
.y
= StartScan
;
1072 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xffffff, 0xffffff, 0);
1074 ret
= IntEngCopyBits(&psurfDest
->SurfObj
,
1081 SURFACE_ShareUnlockSurface(psurfDest
);
1087 Status
= STATUS_SUCCESS
;
1090 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, ScanLines
, bpp
));
1092 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1094 Status
= _SEH2_GetExceptionCode();
1098 if(!NT_SUCCESS(Status
))
1100 DPRINT1("Unable to copy bits to the user provided pointer\n");
1105 GreDeleteObject(hBmpDest
);
1106 EXLATEOBJ_vCleanup(&exlo
);
1108 else ScanLines
= abs(height
);
1112 if(pDC
) DC_UnlockDc(pDC
);
1113 if(psurf
) SURFACE_ShareUnlockSurface(psurf
);
1114 if(pbmci
) DIB_FreeConvertedBitmapInfo(Info
, (BITMAPINFO
*)pbmci
);
1119 #define ROP_TO_ROP4(Rop) ((Rop) >> 16)
1124 NtGdiStretchDIBitsInternal(
1134 IN OPTIONAL LPBYTE pjInit
,
1135 IN LPBITMAPINFO pbmi
,
1137 IN DWORD dwRop
, // MS ntgdi.h says dwRop4(?)
1142 BOOL bResult
= FALSE
;
1147 PSURFACE psurfTmp
= 0, psurfDst
= 0;
1148 PPALETTE ppalDIB
= 0;
1152 if (!(pdc
= DC_LockDc(hdc
)))
1154 EngSetLastError(ERROR_INVALID_HANDLE
);
1158 /* Transform dest size */
1161 IntLPtoDP(pdc
, (POINTL
*)&sizel
, 1);
1164 /* Check if we can use NtGdiSetDIBitsToDeviceInternal */
1165 if (sizel
.cx
== cxSrc
&& sizel
.cy
== cySrc
&& dwRop
== SRCCOPY
)
1168 return NtGdiSetDIBitsToDeviceInternal(hdc
,
1186 pvBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, 'pmeT');
1194 ProbeForRead(pjInit
, cjMaxBits
, 1);
1195 RtlCopyMemory(pvBits
, pjInit
, cjMaxBits
);
1197 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1199 _SEH2_YIELD(return 0);
1203 /* FIXME: Locking twice is cheesy, coord tranlation in UM will fix it */
1204 if (!(pdc
= DC_LockDc(hdc
)))
1206 DPRINT1("Could not lock dc\n");
1207 EngSetLastError(ERROR_INVALID_HANDLE
);
1211 psurfDst
= pdc
->dclevel
.pSurface
;
1219 /* Calculate source and destination rect */
1222 rcSrc
.right
= xSrc
+ abs(cxSrc
);
1223 rcSrc
.bottom
= ySrc
+ abs(cySrc
);
1226 rcDst
.right
= rcDst
.left
+ cxDst
;
1227 rcDst
.bottom
= rcDst
.top
+ cyDst
;
1228 IntLPtoDP(pdc
, (POINTL
*)&rcDst
, 2);
1229 RECTL_vOffsetRect(&rcDst
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1231 hbmTmp
= GreCreateBitmapEx(pbmi
->bmiHeader
.biWidth
,
1232 pbmi
->bmiHeader
.biHeight
,
1234 BitmapFormat(pbmi
->bmiHeader
.biBitCount
,
1235 pbmi
->bmiHeader
.biCompression
),
1236 pbmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
1237 pbmi
->bmiHeader
.biSizeImage
,
1247 psurfTmp
= SURFACE_ShareLockSurface(hbmTmp
);
1254 /* Create a palette for the DIB */
1255 ppalDIB
= CreateDIBPalette(pbmi
, pdc
, dwUsage
);
1262 /* Initialize XLATEOBJ */
1263 EXLATEOBJ_vInitialize(&exlo
,
1266 RGB(0xff, 0xff, 0xff),
1267 pdc
->pdcattr
->crBackgroundClr
,
1268 pdc
->pdcattr
->crForegroundClr
);
1270 /* Prepare DC for blit */
1271 DC_vPrepareDCsForBlit(pdc
, rcDst
, NULL
, rcSrc
);
1273 /* Perform the stretch operation */
1274 bResult
= IntEngStretchBlt(&psurfDst
->SurfObj
,
1277 pdc
->rosdc
.CombinedClip
,
1282 &pdc
->eboFill
.BrushObject
,
1284 ROP_TO_ROP4(dwRop
));
1287 DC_vFinishBlit(pdc
, NULL
);
1288 EXLATEOBJ_vCleanup(&exlo
);
1290 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
1291 if (psurfTmp
) SURFACE_ShareUnlockSurface(psurfTmp
);
1292 if (hbmTmp
) GreDeleteObject(hbmTmp
);
1293 if (pdc
) DC_UnlockDc(pdc
);
1294 ExFreePoolWithTag(pvBits
, 'pmeT');
1315 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1316 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1318 if (bpp
!= 1) fColor
= TRUE
;
1319 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1322 const RGBQUAD
*rgb
= (RGBQUAD
*)((PBYTE
)data
+ data
->bmiHeader
.biSize
);
1323 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1325 // Check if the first color of the colormap is black
1326 if ((col
== RGB(0, 0, 0)))
1329 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1331 // If the second color is white, create a monochrome bitmap
1332 fColor
= (col
!= RGB(0xff,0xff,0xff));
1337 // Now create the bitmap
1340 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
1344 handle
= GreCreateBitmap(width
,
1354 if (NULL
!= handle
&& CBM_INIT
== init
)
1356 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
1362 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1363 // The DDB that is created will be whatever bit depth your reference DC is
1366 NtGdiCreateDIBitmapInternal(
1371 IN OPTIONAL LPBYTE pjInit
,
1372 IN OPTIONAL LPBITMAPINFO pbmi
,
1374 IN UINT cjMaxInitInfo
,
1379 NTSTATUS Status
= STATUS_SUCCESS
;
1380 PBYTE safeBits
= NULL
;
1381 HBITMAP hbmResult
= NULL
;
1383 if(pjInit
&& (fInit
== CBM_INIT
))
1385 if (cjMaxBits
== 0) return NULL
;
1386 safeBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, TAG_DIB
);
1389 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1396 if(pbmi
) ProbeForRead(pbmi
, cjMaxInitInfo
, 1);
1397 if(pjInit
&& (fInit
== CBM_INIT
))
1399 ProbeForRead(pjInit
, cjMaxBits
, 1);
1400 RtlCopyMemory(safeBits
, pjInit
, cjMaxBits
);
1403 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1405 Status
= _SEH2_GetExceptionCode();
1409 if(!NT_SUCCESS(Status
))
1411 SetLastNtError(Status
);
1415 hbmResult
= GreCreateDIBitmapInternal(hDc
,
1427 if (safeBits
) ExFreePoolWithTag(safeBits
, TAG_DIB
);
1433 GreCreateDIBitmapInternal(
1438 IN OPTIONAL LPBYTE pjInit
,
1439 IN OPTIONAL PBITMAPINFO pbmi
,
1450 if (!hDc
) /* 1bpp monochrome bitmap */
1452 // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1453 hdcDest
= NtGdiCreateCompatibleDC(0);
1464 Dc
= DC_LockDc(hdcDest
);
1467 EngSetLastError(ERROR_INVALID_HANDLE
);
1470 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1471 * if bpp != 1 and ignore the real value that was passed */
1473 bpp
= pbmi
->bmiHeader
.biBitCount
;
1476 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
, iUsage
);
1481 NtGdiDeleteObjectApp(hdcDest
);
1489 NtGdiCreateDIBSection(
1491 IN OPTIONAL HANDLE hSection
,
1497 IN ULONG_PTR dwColorSpace
,
1500 HBITMAP hbitmap
= 0;
1502 BOOL bDesktopDC
= FALSE
;
1503 NTSTATUS Status
= STATUS_SUCCESS
;
1505 if (!bmi
) return hbitmap
; // Make sure.
1509 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1510 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1511 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, (WORD
)Usage
), 1);
1513 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1515 Status
= _SEH2_GetExceptionCode();
1519 if(!NT_SUCCESS(Status
))
1521 SetLastNtError(Status
);
1525 // If the reference hdc is null, take the desktop dc
1528 hDC
= NtGdiCreateCompatibleDC(0);
1532 if ((dc
= DC_LockDc(hDC
)))
1534 hbitmap
= DIB_CreateDIBSection(dc
,
1545 EngSetLastError(ERROR_INVALID_HANDLE
);
1549 NtGdiDeleteObjectApp(hDC
);
1556 DIB_CreateDIBSection(
1558 CONST BITMAPINFO
*bmi
,
1566 SURFACE
*bmp
= NULL
;
1567 void *mapBits
= NULL
;
1570 // Fill BITMAP32 structure with DIB data
1571 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1578 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1579 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1580 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1582 /* CreateDIBSection should fail for compressed formats */
1583 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1585 DPRINT1("no compressed format allowed\n");
1586 return (HBITMAP
)NULL
;
1589 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1591 bm
.bmWidth
= bi
->biWidth
;
1592 bm
.bmHeight
= effHeight
;
1593 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: WIDTH_BYTES_ALIGN32(bm
.bmWidth
, bi
->biBitCount
);
1595 bm
.bmPlanes
= bi
->biPlanes
;
1596 bm
.bmBitsPixel
= bi
->biBitCount
;
1599 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1600 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1601 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
&& bi
->biCompression
!= BI_BITFIELDS
1602 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1606 SYSTEM_BASIC_INFORMATION Sbi
;
1609 LARGE_INTEGER SectionOffset
;
1612 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1616 if (!NT_SUCCESS(Status
))
1618 DPRINT1("ZwQuerySystemInformation failed (0x%lx)\n", Status
);
1622 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1623 mapSize
= bi
->biSizeImage
+ (offset
- mapOffset
);
1625 SectionOffset
.LowPart
= mapOffset
;
1626 SectionOffset
.HighPart
= 0;
1628 Status
= ZwMapViewOfSection(section
,
1638 if (!NT_SUCCESS(Status
))
1640 DPRINT1("ZwMapViewOfSection failed (0x%lx)\n", Status
);
1641 EngSetLastError(ERROR_INVALID_PARAMETER
);
1645 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1647 else if (ovr_pitch
&& offset
)
1648 bm
.bmBits
= (LPVOID
) offset
;
1652 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1655 DPRINT1("Failed to allocate memory\n");
1660 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1661 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1664 // Create Device Dependent Bitmap and add DIB pointer
1665 //Size.cx = bm.bmWidth;
1666 //Size.cy = abs(bm.bmHeight);
1667 res
= GreCreateBitmapEx(bm
.bmWidth
,
1670 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1671 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1672 ((bi
->biHeight
< 0) ? BMF_TOPDOWN
: 0),
1678 DPRINT1("GreCreateBitmapEx failed\n");
1679 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
1682 bmp
= SURFACE_ShareLockSurface(res
); // HACK
1685 DPRINT1("SURFACE_LockSurface failed\n");
1686 EngSetLastError(ERROR_INVALID_HANDLE
);
1690 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1691 table between the DIB and the X physical device. Obviously,
1692 this is left out of the ReactOS implementation. Instead,
1693 we call NtGdiSetDIBColorTable. */
1694 bmp
->hDIBSection
= section
;
1695 bmp
->hSecure
= hSecure
;
1696 bmp
->dwOffset
= offset
;
1697 bmp
->flags
= API_BITMAP
;
1698 bmp
->biClrImportant
= bi
->biClrImportant
;
1700 /* Create a palette for the DIB */
1701 ppalDIB
= CreateDIBPalette(bmi
, dc
, usage
);
1704 if (bmp
->ppal
) PALETTE_ShareUnlockPalette(bmp
->ppal
);
1705 bmp
->ppal
= ppalDIB
;
1708 // Clean up in case of errors
1710 if (!res
|| !bmp
|| !bm
.bmBits
)
1712 DPRINT("Got an error res=%08x, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1715 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1718 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1722 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1730 GreDeleteObject(res
);
1737 SURFACE_ShareUnlockSurface(bmp
);
1740 // Return BITMAP handle and storage location
1741 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1749 /***********************************************************************
1752 * Get the info from a bitmap header.
1753 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
1757 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
1758 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
1760 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1762 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
1763 *width
= core
->bcWidth
;
1764 *height
= core
->bcHeight
;
1765 *planes
= core
->bcPlanes
;
1766 *bpp
= core
->bcBitCount
;
1771 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* Assume BITMAPINFOHEADER */
1773 *width
= header
->biWidth
;
1774 *height
= header
->biHeight
;
1775 *planes
= header
->biPlanes
;
1776 *bpp
= header
->biBitCount
;
1777 *compr
= header
->biCompression
;
1778 *size
= header
->biSizeImage
;
1781 DPRINT1("(%d): unknown/wrong size for header\n", header
->biSize
);
1785 /***********************************************************************
1786 * DIB_GetDIBImageBytes
1788 * Return the number of bytes used to hold the image in a DIB bitmap.
1789 * 11/16/1999 (RJJ) lifted from wine
1792 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1794 return WIDTH_BYTES_ALIGN32(width
, depth
) * (height
< 0 ? -height
: height
);
1797 /***********************************************************************
1798 * DIB_BitmapInfoSize
1800 * Return the size of the bitmap info structure including color table.
1801 * 11/16/1999 (RJJ) lifted from wine
1804 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1806 unsigned int colors
, size
, masks
= 0;
1808 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1810 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
1811 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1812 return sizeof(BITMAPCOREHEADER
) + colors
*
1813 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1815 else /* Assume BITMAPINFOHEADER */
1817 colors
= info
->bmiHeader
.biClrUsed
;
1818 if (colors
> 256) colors
= 256;
1819 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
1820 colors
= 1 << info
->bmiHeader
.biBitCount
;
1821 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
1822 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
1823 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1829 DIB_MapPaletteColors(PPALETTE ppalDc
, CONST BITMAPINFO
* lpbmi
)
1836 if (!(ppalDc
->flFlags
& PAL_INDEXED
))
1841 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1842 if (lpbmi
->bmiHeader
.biClrUsed
)
1844 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1847 ppalNew
= PALETTE_AllocPalWithHandle(PAL_INDEXED
, nNumColors
, NULL
, 0, 0, 0);
1848 if (ppalNew
== NULL
)
1850 DPRINT1("Could not allocate palette\n");
1854 lpIndex
= (USHORT
*)((PBYTE
)lpbmi
+ lpbmi
->bmiHeader
.biSize
);
1856 for (i
= 0; i
< nNumColors
; i
++)
1858 ULONG iColorIndex
= *lpIndex
% ppalDc
->NumColors
;
1859 ppalNew
->IndexedColors
[i
] = ppalDc
->IndexedColors
[iColorIndex
];
1863 hpal
= ppalNew
->BaseObject
.hHmgr
;
1864 PALETTE_UnlockPalette(ppalNew
);
1869 /* Converts a BITMAPCOREINFO to a BITMAPINFO structure,
1870 * or does nothing if it's already a BITMAPINFO (or V4 or V5) */
1873 DIB_ConvertBitmapInfo (CONST BITMAPINFO
* pbmi
, DWORD Usage
)
1875 CONST BITMAPCOREINFO
* pbmci
= (BITMAPCOREINFO
*)pbmi
;
1876 BITMAPINFO
* pNewBmi
;
1877 UINT numColors
= 0, ColorsSize
= 0;
1879 if(pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) return (BITMAPINFO
*)pbmi
;
1880 if(pbmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) return NULL
;
1882 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
1884 numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
1885 if(Usage
== DIB_PAL_COLORS
)
1887 ColorsSize
= numColors
* sizeof(WORD
);
1891 ColorsSize
= numColors
* sizeof(RGBQUAD
);
1894 else if (Usage
== DIB_PAL_COLORS
)
1896 /* Invalid at high-res */
1900 pNewBmi
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + ColorsSize
, TAG_DIB
);
1901 if(!pNewBmi
) return NULL
;
1903 RtlZeroMemory(pNewBmi
, sizeof(BITMAPINFOHEADER
) + ColorsSize
);
1905 pNewBmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1906 pNewBmi
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
1907 pNewBmi
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
1908 pNewBmi
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
1909 pNewBmi
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
1910 pNewBmi
->bmiHeader
.biCompression
= BI_RGB
;
1911 pNewBmi
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(pNewBmi
->bmiHeader
.biWidth
,
1912 pNewBmi
->bmiHeader
.biHeight
,
1913 pNewBmi
->bmiHeader
.biBitCount
);
1915 if(Usage
== DIB_PAL_COLORS
)
1917 RtlCopyMemory(pNewBmi
->bmiColors
, pbmci
->bmciColors
, ColorsSize
);
1922 for(i
=0; i
<numColors
; i
++)
1924 pNewBmi
->bmiColors
[i
].rgbRed
= pbmci
->bmciColors
[i
].rgbtRed
;
1925 pNewBmi
->bmiColors
[i
].rgbGreen
= pbmci
->bmciColors
[i
].rgbtGreen
;
1926 pNewBmi
->bmiColors
[i
].rgbBlue
= pbmci
->bmciColors
[i
].rgbtBlue
;
1933 /* Frees a BITMAPINFO created with DIB_ConvertBitmapInfo */
1936 DIB_FreeConvertedBitmapInfo(BITMAPINFO
* converted
, BITMAPINFO
* orig
)
1938 if(converted
!= orig
)
1939 ExFreePoolWithTag(converted
, TAG_DIB
);