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 RGBQUAD DefLogPaletteQuads
[20] = /* Copy of Default Logical Palette */
37 /* rgbBlue, rgbGreen, rgbRed, rgbReserved */
38 { 0x00, 0x00, 0x00, 0x00 },
39 { 0x00, 0x00, 0x80, 0x00 },
40 { 0x00, 0x80, 0x00, 0x00 },
41 { 0x00, 0x80, 0x80, 0x00 },
42 { 0x80, 0x00, 0x00, 0x00 },
43 { 0x80, 0x00, 0x80, 0x00 },
44 { 0x80, 0x80, 0x00, 0x00 },
45 { 0xc0, 0xc0, 0xc0, 0x00 },
46 { 0xc0, 0xdc, 0xc0, 0x00 },
47 { 0xf0, 0xca, 0xa6, 0x00 },
48 { 0xf0, 0xfb, 0xff, 0x00 },
49 { 0xa4, 0xa0, 0xa0, 0x00 },
50 { 0x80, 0x80, 0x80, 0x00 },
51 { 0x00, 0x00, 0xf0, 0x00 },
52 { 0x00, 0xff, 0x00, 0x00 },
53 { 0x00, 0xff, 0xff, 0x00 },
54 { 0xff, 0x00, 0x00, 0x00 },
55 { 0xff, 0x00, 0xff, 0x00 },
56 { 0xff, 0xff, 0x00, 0x00 },
57 { 0xff, 0xff, 0xff, 0x00 }
63 _In_
const BITMAPINFO
*pbmi
,
68 ULONG i
, cBitsPixel
, cColors
;
70 if (pbmi
->bmiHeader
.biSize
< sizeof(BITMAPINFOHEADER
))
72 PBITMAPCOREINFO pbci
= (PBITMAPCOREINFO
)pbmi
;
73 cBitsPixel
= pbci
->bmciHeader
.bcBitCount
;
77 cBitsPixel
= pbmi
->bmiHeader
.biBitCount
;
80 /* Check if the colors are indexed */
83 /* We create a "full" palette */
84 cColors
= 1 << cBitsPixel
;
86 /* Allocate the palette */
87 ppal
= PALETTE_AllocPalette(PAL_INDEXED
,
94 /* Check if the BITMAPINFO specifies how many colors to use */
95 if ((pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) &&
96 (pbmi
->bmiHeader
.biClrUsed
!= 0))
98 /* This is how many colors we can actually process */
99 cColors
= min(cColors
, pbmi
->bmiHeader
.biClrUsed
);
102 /* Check how to use the colors */
103 if (iUsage
== DIB_PAL_COLORS
)
107 /* The colors are an array of WORD indices into the DC palette */
108 PWORD pwColors
= (PWORD
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
110 /* Use the DCs palette or, if no DC is given, the default one */
111 PPALETTE ppalDC
= pdc
? pdc
->dclevel
.ppal
: gppalDefault
;
113 /* Loop all color indices in the DIB */
114 for (i
= 0; i
< cColors
; i
++)
116 /* Get the palette index and handle wraparound when exceeding
117 the number of colors in the DC palette */
118 WORD wIndex
= pwColors
[i
] % ppalDC
->NumColors
;
120 /* USe the RGB value from the DC palette */
121 crColor
= PALETTE_ulGetRGBColorFromIndex(ppalDC
, wIndex
);
122 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
125 else if (iUsage
== DIB_PAL_BRUSHHACK
)
127 /* The colors are an array of WORD indices into the DC palette */
128 PWORD pwColors
= (PWORD
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
130 /* Loop all color indices in the DIB */
131 for (i
= 0; i
< cColors
; i
++)
133 /* Set the index directly as the RGB color, the real palette
134 containing RGB values will be calculated when the brush is
136 PALETTE_vSetRGBColorForIndex(ppal
, i
, pwColors
[i
]);
139 /* Mark the palette as a brush hack palette */
140 ppal
->flFlags
|= PAL_BRUSHHACK
;
142 // else if (iUsage == 2)
144 // FIXME: this one is undocumented
147 else if (pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
))
149 /* The colors are an array of RGBQUAD values */
150 RGBQUAD
*prgb
= (RGBQUAD
*)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
152 // FIXME: do we need to handle PALETTEINDEX / PALETTERGB macro?
154 /* Loop all color indices in the DIB */
155 for (i
= 0; i
< cColors
; i
++)
157 /* Get the color value and translate it to a COLORREF */
158 RGBQUAD rgb
= prgb
[i
];
159 COLORREF crColor
= RGB(rgb
.rgbRed
, rgb
.rgbGreen
, rgb
.rgbBlue
);
161 /* Set the RGB value in the palette */
162 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
167 /* The colors are an array of RGBTRIPLE values */
168 RGBTRIPLE
*prgb
= (RGBTRIPLE
*)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
170 /* Loop all color indices in the DIB */
171 for (i
= 0; i
< cColors
; i
++)
173 /* Get the color value and translate it to a COLORREF */
174 RGBTRIPLE rgb
= prgb
[i
];
175 COLORREF crColor
= RGB(rgb
.rgbtRed
, rgb
.rgbtGreen
, rgb
.rgbtBlue
);
177 /* Set the RGB value in the palette */
178 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
184 /* This is a bitfield / RGB palette */
185 ULONG flRedMask
, flGreenMask
, flBlueMask
;
187 /* Check if the DIB contains bitfield values */
188 if ((pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) &&
189 (pbmi
->bmiHeader
.biCompression
== BI_BITFIELDS
))
191 /* Check if we have a v4/v5 header */
192 if (pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPV4HEADER
))
194 /* The masks are dedicated fields in the header */
195 PBITMAPV4HEADER pbmV4Header
= (PBITMAPV4HEADER
)&pbmi
->bmiHeader
;
196 flRedMask
= pbmV4Header
->bV4RedMask
;
197 flGreenMask
= pbmV4Header
->bV4GreenMask
;
198 flBlueMask
= pbmV4Header
->bV4BlueMask
;
202 /* The masks are the first 3 values in the DIB color table */
203 PDWORD pdwColors
= (PVOID
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
204 flRedMask
= pdwColors
[0];
205 flGreenMask
= pdwColors
[1];
206 flBlueMask
= pdwColors
[2];
211 /* Check what bit depth we have. Note: optimization flags are
212 calculated in PALETTE_AllocPalette() */
213 if (cBitsPixel
== 16)
215 /* This is an RGB 555 palette */
217 flGreenMask
= 0x03E0;
222 /* This is an RGB 888 palette */
223 flRedMask
= 0xFF0000;
224 flGreenMask
= 0x00FF00;
225 flBlueMask
= 0x0000FF;
229 /* Allocate the bitfield palette */
230 ppal
= PALETTE_AllocPalette(PAL_BITFIELDS
,
238 /* We're done, return the palette */
242 // Converts a DIB to a device-dependent bitmap
251 CONST BITMAPINFO
*bmi
,
254 HBITMAP SourceBitmap
;
255 PSURFACE psurfDst
, psurfSrc
;
260 PPALETTE ppalDIB
= 0;
264 SourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
267 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
268 bmi
->bmiHeader
.biCompression
),
269 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
270 bmi
->bmiHeader
.biSizeImage
,
275 DPRINT1("Error: Could not create a bitmap.\n");
276 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
280 psurfDst
= SURFACE_ShareLockSurface(hBitmap
);
281 psurfSrc
= SURFACE_ShareLockSurface(SourceBitmap
);
283 if(!(psurfSrc
&& psurfDst
))
285 DPRINT1("Error: Could not lock surfaces\n");
289 /* Create a palette for the DIB */
290 ppalDIB
= CreateDIBPalette(bmi
, DC
, ColorUse
);
293 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
297 /* Initialize EXLATEOBJ */
298 EXLATEOBJ_vInitialize(&exlo
,
301 RGB(0xff, 0xff, 0xff),
302 RGB(0xff, 0xff, 0xff), //DC->pdcattr->crBackgroundClr,
303 0); // DC->pdcattr->crForegroundClr);
305 rcDst
.top
= StartScan
;
307 rcDst
.bottom
= rcDst
.top
+ ScanLines
;
308 rcDst
.right
= psurfDst
->SurfObj
.sizlBitmap
.cx
;
312 result
= IntEngCopyBits(&psurfDst
->SurfObj
,
321 EXLATEOBJ_vCleanup(&exlo
);
324 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
325 if(psurfSrc
) SURFACE_ShareUnlockSurface(psurfSrc
);
326 if(psurfDst
) SURFACE_ShareUnlockSurface(psurfDst
);
327 GreDeleteObject(SourceBitmap
);
334 IntGdiCreateMaskFromRLE(
345 BYTE Data
, NumPixels
, ToSkip
;
347 ASSERT((Compression
== BI_RLE8
) || (Compression
== BI_RLE4
));
349 /* Create the bitmap */
350 Mask
= GreCreateBitmapEx(Width
, Height
, 0, BMF_1BPP
, 0, 0, NULL
, 0);
354 SurfObj
= EngLockSurface((HSURF
)Mask
);
357 GreDeleteObject(Mask
);
360 ASSERT(SurfObj
->pvBits
!= NULL
);
372 if ((x
+ NumPixels
) > Width
)
373 NumPixels
= Width
- x
;
378 DIB_1BPP_HLine(SurfObj
, x
, x
+ NumPixels
, y
, 1);
399 if (i
>= (BitsSize
- 1))
410 /* Done for this run */
414 /* Embedded data into the RLE */
416 if (Compression
== BI_RLE8
)
419 ToSkip
= (NumPixels
/ 2) + (NumPixels
& 1);
421 if ((i
+ ToSkip
) > BitsSize
)
423 ToSkip
= (ToSkip
+ 1) & ~1;
425 if ((x
+ NumPixels
) > Width
)
426 NumPixels
= Width
- x
;
430 DIB_1BPP_HLine(SurfObj
, x
, x
+ NumPixels
, y
, 1);
437 EngUnlockSurface(SurfObj
);
444 NtGdiSetDIBitsToDeviceInternal(
459 IN BOOL bTransformCoordinates
,
460 IN OPTIONAL HANDLE hcmXform
)
463 NTSTATUS Status
= STATUS_SUCCESS
;
465 HBITMAP hSourceBitmap
= NULL
, hMaskBitmap
= NULL
;
466 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
, *pMaskSurf
= NULL
;
473 PPALETTE ppalDIB
= NULL
;
474 LPBITMAPINFO pbmiSafe
;
478 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjMaxInfo
, 'pmTG');
479 if (!pbmiSafe
) return 0;
483 ProbeForRead(bmi
, cjMaxInfo
, 1);
484 ProbeForRead(Bits
, cjMaxBits
, 1);
485 RtlCopyMemory(pbmiSafe
, bmi
, cjMaxInfo
);
488 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
490 Status
= _SEH2_GetExceptionCode();
494 if (!NT_SUCCESS(Status
))
499 ScanLines
= min(ScanLines
, abs(bmi
->bmiHeader
.biHeight
) - StartScan
);
501 pDC
= DC_LockDc(hDC
);
504 EngSetLastError(ERROR_INVALID_HANDLE
);
508 if (pDC
->dctype
== DC_TYPE_INFO
)
516 if (bTransformCoordinates
)
518 IntLPtoDP(pDC
, (LPPOINT
)&rcDest
, 2);
520 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
521 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
522 rcDest
.right
= rcDest
.left
+ Width
;
523 rcDest
.bottom
= rcDest
.top
+ Height
;
524 rcDest
.top
+= StartScan
;
529 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
530 SourceSize
.cy
= ScanLines
;
532 //DIBWidth = WIDTH_BYTES_ALIGN32(SourceSize.cx, bmi->bmiHeader.biBitCount);
534 hSourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
537 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
538 bmi
->bmiHeader
.biCompression
),
539 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
540 bmi
->bmiHeader
.biSizeImage
,
546 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
547 Status
= STATUS_NO_MEMORY
;
551 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
554 Status
= STATUS_UNSUCCESSFUL
;
558 /* HACK: If this is a RLE bitmap, only the relevant pixels must be set. */
559 if ((bmi
->bmiHeader
.biCompression
== BI_RLE8
) || (bmi
->bmiHeader
.biCompression
== BI_RLE4
))
561 hMaskBitmap
= IntGdiCreateMaskFromRLE(bmi
->bmiHeader
.biWidth
,
563 bmi
->bmiHeader
.biCompression
,
568 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
569 Status
= STATUS_NO_MEMORY
;
572 pMaskSurf
= EngLockSurface((HSURF
)hMaskBitmap
);
575 Status
= STATUS_UNSUCCESSFUL
;
580 /* Create a palette for the DIB */
581 ppalDIB
= CreateDIBPalette(bmi
, pDC
, ColorUse
);
584 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
585 Status
= STATUS_NO_MEMORY
;
589 /* This is actually a blit */
590 DC_vPrepareDCsForBlit(pDC
, &rcDest
, NULL
, NULL
);
591 pSurf
= pDC
->dclevel
.pSurface
;
594 DC_vFinishBlit(pDC
, NULL
);
601 /* Initialize EXLATEOBJ */
602 EXLATEOBJ_vInitialize(&exlo
,
605 RGB(0xff, 0xff, 0xff),
606 pDC
->pdcattr
->crBackgroundClr
,
607 pDC
->pdcattr
->crForegroundClr
);
609 pDestSurf
= &pSurf
->SurfObj
;
612 DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
613 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
614 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
615 Status
= IntEngBitBlt(pDestSurf
,
622 pMaskSurf
? &ptSource
: NULL
,
625 pMaskSurf
? ROP4_MASK
: ROP4_FROM_INDEX(R3_OPINDEX_SRCCOPY
));
627 /* Cleanup EXLATEOBJ */
628 EXLATEOBJ_vCleanup(&exlo
);
631 DC_vFinishBlit(pDC
, NULL
);
634 if (NT_SUCCESS(Status
))
639 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
641 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
642 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
643 if (pMaskSurf
) EngUnlockSurface(pMaskSurf
);
644 if (hMaskBitmap
) EngDeleteSurface((HSURF
)hMaskBitmap
);
647 ExFreePoolWithTag(pbmiSafe
, 'pmTG');
652 /* Converts a device-dependent bitmap to a DIB */
655 GreGetDIBitsInternal(
666 BITMAPCOREINFO
* pbmci
= NULL
;
667 PSURFACE psurf
= NULL
;
677 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
679 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
682 colorPtr
= (LPBYTE
)Info
+ Info
->bmiHeader
.biSize
;
685 bitmap_type
= DIB_GetBitmapInfo(&Info
->bmiHeader
,
692 if(bitmap_type
== -1)
694 DPRINT("Wrong bitmap format\n");
695 EngSetLastError(ERROR_INVALID_PARAMETER
);
698 else if(bitmap_type
== 0)
700 /* We need a BITMAPINFO to create a DIB, but we have to fill
701 * the BITMAPCOREINFO we're provided */
702 pbmci
= (BITMAPCOREINFO
*)Info
;
703 Info
= DIB_ConvertBitmapInfo((BITMAPINFO
*)pbmci
, Usage
);
706 DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
709 rgbQuads
= Info
->bmiColors
;
712 pDC
= DC_LockDc(hDC
);
713 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
719 /* Get a pointer to the source bitmap object */
720 psurf
= SURFACE_ShareLockSurface(hBitmap
);
727 /* Fill in the structure */
730 case 0: /* Only info */
731 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
732 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
733 -psurf
->SurfObj
.sizlBitmap
.cy
:
734 psurf
->SurfObj
.sizlBitmap
.cy
;;
735 Info
->bmiHeader
.biPlanes
= 1;
736 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
737 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
738 Info
->bmiHeader
.biHeight
,
739 Info
->bmiHeader
.biBitCount
);
742 switch(Info
->bmiHeader
.biBitCount
)
746 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
749 Info
->bmiHeader
.biCompression
= BI_RGB
;
753 else if(Info
->bmiHeader
.biBitCount
> 8)
755 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
759 Info
->bmiHeader
.biCompression
= BI_RGB
;
761 Info
->bmiHeader
.biXPelsPerMeter
= 0;
762 Info
->bmiHeader
.biYPelsPerMeter
= 0;
763 Info
->bmiHeader
.biClrUsed
= 0;
764 Info
->bmiHeader
.biClrImportant
= 0;
765 ScanLines
= abs(Info
->bmiHeader
.biHeight
);
771 Info
->bmiHeader
.biClrUsed
= 0;
773 /* If the bitmap if a DIB section and has the same format than what
774 * we're asked, go ahead! */
775 if((psurf
->hSecure
) &&
776 (BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bpp
))
778 if(Usage
== DIB_RGB_COLORS
)
780 ULONG colors
= min(psurf
->ppal
->NumColors
, 256);
781 if(colors
!= 256) Info
->bmiHeader
.biClrUsed
= colors
;
782 for(i
= 0; i
< colors
; i
++)
784 rgbQuads
[i
].rgbRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
785 rgbQuads
[i
].rgbGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
786 rgbQuads
[i
].rgbBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
787 rgbQuads
[i
].rgbReserved
= 0;
792 for(i
= 0; i
< 256; i
++)
793 ((WORD
*)rgbQuads
)[i
] = i
;
798 if(Usage
== DIB_PAL_COLORS
)
800 for(i
= 0; i
< 256; i
++)
802 ((WORD
*)rgbQuads
)[i
] = i
;
805 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
))
807 /* For color DDBs in native depth (mono DDBs always have
808 a black/white palette):
809 Generate the color map from the selected palette */
810 PPALETTE pDcPal
= PALETTE_ShareLockPalette(pDC
->dclevel
.hpal
);
816 for (i
= 0; i
< pDcPal
->NumColors
; i
++)
818 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
819 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
820 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
821 rgbQuads
[i
].rgbReserved
= 0;
823 PALETTE_ShareUnlockPalette(pDcPal
);
830 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
= rgbQuads
[0].rgbBlue
= 0;
831 rgbQuads
[0].rgbReserved
= 0;
832 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
= rgbQuads
[1].rgbBlue
= 0xff;
833 rgbQuads
[1].rgbReserved
= 0;
837 RtlCopyMemory(rgbQuads
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
844 memcpy(rgbQuads
, DefLogPaletteQuads
,
845 10 * sizeof(RGBQUAD
));
846 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10,
847 10 * sizeof(RGBQUAD
));
848 color
= rgbQuads
+ 10;
849 for(r
= 0; r
<= 5; r
++) /* FIXME */
851 for(g
= 0; g
<= 5; g
++)
853 for(b
= 0; b
<= 5; b
++)
855 color
->rgbRed
= (r
* 0xff) / 5;
856 color
->rgbGreen
= (g
* 0xff) / 5;
857 color
->rgbBlue
= (b
* 0xff) / 5;
858 color
->rgbReserved
= 0;
870 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
872 ((PDWORD
)Info
->bmiColors
)[0] = 0x7c00;
873 ((PDWORD
)Info
->bmiColors
)[1] = 0x03e0;
874 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
879 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
883 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
884 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
885 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
889 ((PDWORD
)Info
->bmiColors
)[0] = 0xf800;
890 ((PDWORD
)Info
->bmiColors
)[1] = 0x07e0;
891 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
898 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
902 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
903 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
904 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
908 ((PDWORD
)Info
->bmiColors
)[0] = 0xff0000;
909 ((PDWORD
)Info
->bmiColors
)[1] = 0x00ff00;
910 ((PDWORD
)Info
->bmiColors
)[2] = 0x0000ff;
915 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(width
, height
, bpp
);
917 if(Bits
&& ScanLines
)
919 /* Create a DIBSECTION, blt it, profit */
928 if (StartScan
> (ULONG
)psurf
->SurfObj
.sizlBitmap
.cy
)
935 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
939 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
940 Info
->bmiHeader
.biHeight
= (height
< 0) ?
941 -(LONG
)ScanLines
: ScanLines
;
943 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
945 Info
->bmiHeader
.biWidth
= width
;
946 Info
->bmiHeader
.biHeight
= height
;
950 DPRINT1("Unable to create a DIB Section!\n");
951 EngSetLastError(ERROR_INVALID_PARAMETER
);
956 psurfDest
= SURFACE_ShareLockSurface(hBmpDest
);
958 RECTL_vSetRect(&rcDest
, 0, 0, psurf
->SurfObj
.sizlBitmap
.cx
, ScanLines
);
966 if(ScanLines
<= StartScan
)
969 SURFACE_ShareUnlockSurface(psurfDest
);
970 GreDeleteObject(hBmpDest
);
974 ScanLines
-= StartScan
;
978 srcPoint
.y
= StartScan
;
981 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xffffff, 0xffffff, 0);
983 ret
= IntEngCopyBits(&psurfDest
->SurfObj
,
990 SURFACE_ShareUnlockSurface(psurfDest
);
996 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, ScanLines
, bpp
));
999 GreDeleteObject(hBmpDest
);
1000 EXLATEOBJ_vCleanup(&exlo
);
1002 else ScanLines
= abs(height
);
1006 if(pDC
) DC_UnlockDc(pDC
);
1007 if(psurf
) SURFACE_ShareUnlockSurface(psurf
);
1008 if(pbmci
) DIB_FreeConvertedBitmapInfo(Info
, (BITMAPINFO
*)pbmci
, Usage
);
1015 NtGdiGetDIBitsInternal(
1018 _In_ UINT iStartScan
,
1020 _Out_opt_ LPBYTE pjBits
,
1021 _Inout_ LPBITMAPINFO pbmiUser
,
1023 _In_ UINT cjMaxBits
,
1024 _In_ UINT cjMaxInfo
)
1027 HANDLE hSecure
= NULL
;
1031 /* Check for bad iUsage */
1032 if (iUsage
> 2) return 0;
1034 /* Check if the size of the bitmap info is large enough */
1035 if (cjMaxInfo
< sizeof(BITMAPCOREHEADER
))
1040 /* Use maximum size */
1041 cjMaxInfo
= min(cjMaxInfo
, sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
));
1043 // HACK: the underlying code sucks and doesn't care for the size, so we
1044 // give it the maximum ever needed
1045 cjAlloc
= sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
);
1047 /* Allocate a buffer the bitmapinfo */
1048 pbmi
= ExAllocatePoolWithTag(PagedPool
, cjAlloc
, 'imBG');
1058 /* Probe and copy the BITMAPINFO */
1059 ProbeForRead(pbmiUser
, cjMaxInfo
, 1);
1060 RtlCopyMemory(pbmi
, pbmiUser
, cjMaxInfo
);
1062 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1064 _SEH2_YIELD(goto cleanup
;)
1068 /* Check if the header size is large enough */
1069 if ((pbmi
->bmiHeader
.biSize
< sizeof(BITMAPCOREHEADER
)) ||
1070 (pbmi
->bmiHeader
.biSize
> cjMaxInfo
))
1075 /* Check if the caller provided bitmap bits */
1078 /* Secure the user mode memory */
1079 hSecure
= EngSecureMem(pjBits
, cjMaxBits
);
1086 /* Now call the internal function */
1087 iResult
= GreGetDIBitsInternal(hdc
,
1097 /* Check for success */
1100 /* Use SEH to copy back to user mode */
1103 /* Copy the data back */
1104 ProbeForWrite(pbmiUser
, cjMaxInfo
, 1);
1105 RtlCopyMemory(pbmiUser
, pbmi
, cjMaxInfo
);
1107 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1115 if (hSecure
) EngUnsecureMem(hSecure
);
1116 ExFreePoolWithTag(pbmi
, 'imBG');
1122 #define ROP_TO_ROP4(Rop) ((Rop) >> 16)
1127 NtGdiStretchDIBitsInternal(
1137 IN OPTIONAL LPBYTE pjInit
,
1138 IN LPBITMAPINFO pbmi
,
1140 IN DWORD dwRop
, // MS ntgdi.h says dwRop4(?)
1145 BOOL bResult
= FALSE
;
1150 PSURFACE psurfTmp
= 0, psurfDst
= 0;
1151 PPALETTE ppalDIB
= 0;
1155 if (!(pdc
= DC_LockDc(hdc
)))
1157 EngSetLastError(ERROR_INVALID_HANDLE
);
1161 /* Check for info / mem DC without surface */
1162 if (!pdc
->dclevel
.pSurface
)
1169 /* Transform dest size */
1172 IntLPtoDP(pdc
, (POINTL
*)&sizel
, 1);
1175 /* Check if we can use NtGdiSetDIBitsToDeviceInternal */
1176 if (sizel
.cx
== cxSrc
&& sizel
.cy
== cySrc
&& dwRop
== SRCCOPY
)
1179 return NtGdiSetDIBitsToDeviceInternal(hdc
,
1197 if (pjInit
&& (cjMaxBits
> 0))
1199 pvBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, 'pmeT');
1207 ProbeForRead(pjInit
, cjMaxBits
, 1);
1208 RtlCopyMemory(pvBits
, pjInit
, cjMaxBits
);
1210 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1212 ExFreePoolWithTag(pvBits
, 'pmeT');
1213 _SEH2_YIELD(return 0);
1222 /* FIXME: Locking twice is cheesy, coord tranlation in UM will fix it */
1223 if (!(pdc
= DC_LockDc(hdc
)))
1225 DPRINT1("Could not lock dc\n");
1226 EngSetLastError(ERROR_INVALID_HANDLE
);
1230 /* Calculate source and destination rect */
1233 rcSrc
.right
= xSrc
+ abs(cxSrc
);
1234 rcSrc
.bottom
= ySrc
+ abs(cySrc
);
1237 rcDst
.right
= rcDst
.left
+ cxDst
;
1238 rcDst
.bottom
= rcDst
.top
+ cyDst
;
1239 IntLPtoDP(pdc
, (POINTL
*)&rcDst
, 2);
1240 RECTL_vOffsetRect(&rcDst
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1242 hbmTmp
= GreCreateBitmapEx(pbmi
->bmiHeader
.biWidth
,
1243 pbmi
->bmiHeader
.biHeight
,
1245 BitmapFormat(pbmi
->bmiHeader
.biBitCount
,
1246 pbmi
->bmiHeader
.biCompression
),
1247 pbmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
1248 pbmi
->bmiHeader
.biSizeImage
,
1258 psurfTmp
= SURFACE_ShareLockSurface(hbmTmp
);
1265 /* Create a palette for the DIB */
1266 ppalDIB
= CreateDIBPalette(pbmi
, pdc
, dwUsage
);
1273 /* Prepare DC for blit */
1274 DC_vPrepareDCsForBlit(pdc
, &rcDst
, NULL
, NULL
);
1276 psurfDst
= pdc
->dclevel
.pSurface
;
1278 /* Initialize XLATEOBJ */
1279 EXLATEOBJ_vInitialize(&exlo
,
1282 RGB(0xff, 0xff, 0xff),
1283 pdc
->pdcattr
->crBackgroundClr
,
1284 pdc
->pdcattr
->crForegroundClr
);
1286 /* Perform the stretch operation */
1287 bResult
= IntEngStretchBlt(&psurfDst
->SurfObj
,
1296 &pdc
->eboFill
.BrushObject
,
1298 ROP_TO_ROP4(dwRop
));
1301 DC_vFinishBlit(pdc
, NULL
);
1302 EXLATEOBJ_vCleanup(&exlo
);
1304 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
1305 if (psurfTmp
) SURFACE_ShareUnlockSurface(psurfTmp
);
1306 if (hbmTmp
) GreDeleteObject(hbmTmp
);
1307 if (pdc
) DC_UnlockDc(pdc
);
1308 if (pvBits
) ExFreePoolWithTag(pvBits
, 'pmeT');
1329 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1330 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1332 if (bpp
!= 1) fColor
= TRUE
;
1333 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1336 const RGBQUAD
*rgb
= (RGBQUAD
*)((PBYTE
)data
+ data
->bmiHeader
.biSize
);
1337 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1339 // Check if the first color of the colormap is black
1340 if (col
== RGB(0, 0, 0))
1343 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1345 // If the second color is white, create a monochrome bitmap
1346 fColor
= (col
!= RGB(0xff,0xff,0xff));
1351 // Now create the bitmap
1354 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
1358 handle
= GreCreateBitmap(width
,
1368 if (NULL
!= handle
&& CBM_INIT
== init
)
1370 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
1376 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1377 // The DDB that is created will be whatever bit depth your reference DC is
1380 NtGdiCreateDIBitmapInternal(
1385 IN OPTIONAL LPBYTE pjInit
,
1386 IN OPTIONAL LPBITMAPINFO pbmi
,
1388 IN UINT cjMaxInitInfo
,
1393 NTSTATUS Status
= STATUS_SUCCESS
;
1394 PBYTE safeBits
= NULL
;
1395 HBITMAP hbmResult
= NULL
;
1397 if(pjInit
&& (fInit
== CBM_INIT
))
1399 if (cjMaxBits
== 0) return NULL
;
1400 safeBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, TAG_DIB
);
1403 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1410 if(pbmi
) ProbeForRead(pbmi
, cjMaxInitInfo
, 1);
1411 if(pjInit
&& (fInit
== CBM_INIT
))
1413 ProbeForRead(pjInit
, cjMaxBits
, 1);
1414 RtlCopyMemory(safeBits
, pjInit
, cjMaxBits
);
1417 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1419 Status
= _SEH2_GetExceptionCode();
1423 if(!NT_SUCCESS(Status
))
1425 SetLastNtError(Status
);
1429 hbmResult
= GreCreateDIBitmapInternal(hDc
,
1441 if (safeBits
) ExFreePoolWithTag(safeBits
, TAG_DIB
);
1447 GreCreateDIBitmapInternal(
1452 IN OPTIONAL LPBYTE pjInit
,
1453 IN OPTIONAL PBITMAPINFO pbmi
,
1464 if (!hDc
) /* 1bpp monochrome bitmap */
1466 // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1467 hdcDest
= NtGdiCreateCompatibleDC(0);
1478 Dc
= DC_LockDc(hdcDest
);
1481 EngSetLastError(ERROR_INVALID_HANDLE
);
1484 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1485 * if bpp != 1 and ignore the real value that was passed */
1487 bpp
= pbmi
->bmiHeader
.biBitCount
;
1490 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
, iUsage
);
1495 NtGdiDeleteObjectApp(hdcDest
);
1503 NtGdiCreateDIBSection(
1505 IN OPTIONAL HANDLE hSection
,
1511 IN ULONG_PTR dwColorSpace
,
1514 HBITMAP hbitmap
= 0;
1516 BOOL bDesktopDC
= FALSE
;
1517 NTSTATUS Status
= STATUS_SUCCESS
;
1519 if (!bmi
) return hbitmap
; // Make sure.
1523 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1524 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1525 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, (WORD
)Usage
), 1);
1527 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1529 Status
= _SEH2_GetExceptionCode();
1533 if(!NT_SUCCESS(Status
))
1535 SetLastNtError(Status
);
1539 // If the reference hdc is null, take the desktop dc
1542 hDC
= NtGdiCreateCompatibleDC(0);
1546 if ((dc
= DC_LockDc(hDC
)))
1548 hbitmap
= DIB_CreateDIBSection(dc
,
1559 EngSetLastError(ERROR_INVALID_HANDLE
);
1563 NtGdiDeleteObjectApp(hDC
);
1570 DIB_CreateDIBSection(
1572 CONST BITMAPINFO
*bmi
,
1580 SURFACE
*bmp
= NULL
;
1581 void *mapBits
= NULL
;
1582 PPALETTE ppalDIB
= NULL
;
1584 // Fill BITMAP32 structure with DIB data
1585 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1592 DPRINT("format (%ld,%ld), planes %u, bpp %u, size %lu, colors %lu (%s)\n",
1593 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1594 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1596 /* CreateDIBSection should fail for compressed formats */
1597 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1599 DPRINT1("no compressed format allowed\n");
1600 return (HBITMAP
)NULL
;
1603 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1605 bm
.bmWidth
= bi
->biWidth
;
1606 bm
.bmHeight
= effHeight
;
1607 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: WIDTH_BYTES_ALIGN32(bm
.bmWidth
, bi
->biBitCount
);
1609 bm
.bmPlanes
= bi
->biPlanes
;
1610 bm
.bmBitsPixel
= bi
->biBitCount
;
1613 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1614 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1615 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
&& bi
->biCompression
!= BI_BITFIELDS
1616 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1620 SYSTEM_BASIC_INFORMATION Sbi
;
1623 LARGE_INTEGER SectionOffset
;
1626 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1630 if (!NT_SUCCESS(Status
))
1632 DPRINT1("ZwQuerySystemInformation failed (0x%lx)\n", Status
);
1636 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1637 mapSize
= bi
->biSizeImage
+ (offset
- mapOffset
);
1639 SectionOffset
.LowPart
= mapOffset
;
1640 SectionOffset
.HighPart
= 0;
1642 Status
= ZwMapViewOfSection(section
,
1652 if (!NT_SUCCESS(Status
))
1654 DPRINT1("ZwMapViewOfSection failed (0x%lx)\n", Status
);
1655 EngSetLastError(ERROR_INVALID_PARAMETER
);
1659 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1661 else if (ovr_pitch
&& offset
)
1662 bm
.bmBits
= (LPVOID
) offset
;
1666 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1669 DPRINT1("Failed to allocate memory\n");
1674 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1675 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1678 // Create Device Dependent Bitmap and add DIB pointer
1679 //Size.cx = bm.bmWidth;
1680 //Size.cy = abs(bm.bmHeight);
1681 res
= GreCreateBitmapEx(bm
.bmWidth
,
1684 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1685 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1686 ((bi
->biHeight
< 0) ? BMF_TOPDOWN
: 0),
1692 DPRINT1("GreCreateBitmapEx failed\n");
1693 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
1696 bmp
= SURFACE_ShareLockSurface(res
); // HACK
1699 DPRINT1("SURFACE_LockSurface failed\n");
1700 EngSetLastError(ERROR_INVALID_HANDLE
);
1704 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1705 table between the DIB and the X physical device. Obviously,
1706 this is left out of the ReactOS implementation. Instead,
1707 we call NtGdiSetDIBColorTable. */
1708 bmp
->hDIBSection
= section
;
1709 bmp
->hSecure
= hSecure
;
1710 bmp
->dwOffset
= offset
;
1711 bmp
->flags
= API_BITMAP
;
1712 bmp
->biClrImportant
= bi
->biClrImportant
;
1714 /* Create a palette for the DIB */
1715 ppalDIB
= CreateDIBPalette(bmi
, dc
, usage
);
1717 // Clean up in case of errors
1719 if (!res
|| !bmp
|| !bm
.bmBits
|| !ppalDIB
)
1721 DPRINT("Got an error res=%p, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1724 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1727 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1731 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1736 SURFACE_ShareUnlockSurface(bmp
);
1742 GreDeleteObject(res
);
1748 PALETTE_ShareUnlockPalette(ppalDIB
);
1754 /* If we're here, everything went fine */
1755 SURFACE_vSetPalette(bmp
, ppalDIB
);
1756 PALETTE_ShareUnlockPalette(ppalDIB
);
1757 SURFACE_ShareUnlockSurface(bmp
);
1760 // Return BITMAP handle and storage location
1761 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1769 /***********************************************************************
1772 * Get the info from a bitmap header.
1773 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
1777 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
1778 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
1780 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1782 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
1783 *width
= core
->bcWidth
;
1784 *height
= core
->bcHeight
;
1785 *planes
= core
->bcPlanes
;
1786 *bpp
= core
->bcBitCount
;
1791 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* Assume BITMAPINFOHEADER */
1793 *width
= header
->biWidth
;
1794 *height
= header
->biHeight
;
1795 *planes
= header
->biPlanes
;
1796 *bpp
= header
->biBitCount
;
1797 *compr
= header
->biCompression
;
1798 *size
= header
->biSizeImage
;
1801 DPRINT1("(%u): unknown/wrong size for header\n", header
->biSize
);
1805 /***********************************************************************
1806 * DIB_GetDIBImageBytes
1808 * Return the number of bytes used to hold the image in a DIB bitmap.
1809 * 11/16/1999 (RJJ) lifted from wine
1812 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1814 return WIDTH_BYTES_ALIGN32(width
, depth
) * (height
< 0 ? -height
: height
);
1817 /***********************************************************************
1818 * DIB_BitmapInfoSize
1820 * Return the size of the bitmap info structure including color table.
1821 * 11/16/1999 (RJJ) lifted from wine
1824 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1826 unsigned int colors
, size
, masks
= 0;
1828 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1830 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
1831 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1832 return sizeof(BITMAPCOREHEADER
) + colors
*
1833 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1835 else /* Assume BITMAPINFOHEADER */
1837 colors
= info
->bmiHeader
.biClrUsed
;
1838 if (colors
> 256) colors
= 256;
1839 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
1840 colors
= 1 << info
->bmiHeader
.biBitCount
;
1841 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
1842 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
1843 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1849 DIB_MapPaletteColors(PPALETTE ppalDc
, CONST BITMAPINFO
* lpbmi
)
1856 if (!(ppalDc
->flFlags
& PAL_INDEXED
))
1861 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1862 if (lpbmi
->bmiHeader
.biClrUsed
)
1864 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1867 ppalNew
= PALETTE_AllocPalWithHandle(PAL_INDEXED
, nNumColors
, NULL
, 0, 0, 0);
1868 if (ppalNew
== NULL
)
1870 DPRINT1("Could not allocate palette\n");
1874 lpIndex
= (USHORT
*)((PBYTE
)lpbmi
+ lpbmi
->bmiHeader
.biSize
);
1876 for (i
= 0; i
< nNumColors
; i
++)
1878 ULONG iColorIndex
= *lpIndex
% ppalDc
->NumColors
;
1879 ppalNew
->IndexedColors
[i
] = ppalDc
->IndexedColors
[iColorIndex
];
1883 hpal
= ppalNew
->BaseObject
.hHmgr
;
1884 PALETTE_UnlockPalette(ppalNew
);
1889 /* Converts a BITMAPCOREINFO to a BITMAPINFO structure,
1890 * or does nothing if it's already a BITMAPINFO (or V4 or V5) */
1893 DIB_ConvertBitmapInfo (CONST BITMAPINFO
* pbmi
, DWORD Usage
)
1895 CONST BITMAPCOREINFO
* pbmci
= (BITMAPCOREINFO
*)pbmi
;
1896 BITMAPINFO
* pNewBmi
;
1897 UINT numColors
= 0, ColorsSize
= 0;
1899 if(pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) return (BITMAPINFO
*)pbmi
;
1900 if(pbmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) return NULL
;
1902 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
1904 numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
1905 if(Usage
== DIB_PAL_COLORS
)
1907 ColorsSize
= numColors
* sizeof(WORD
);
1911 ColorsSize
= numColors
* sizeof(RGBQUAD
);
1914 else if (Usage
== DIB_PAL_COLORS
)
1916 /* Invalid at high-res */
1920 pNewBmi
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + ColorsSize
, TAG_DIB
);
1921 if(!pNewBmi
) return NULL
;
1923 RtlZeroMemory(pNewBmi
, sizeof(BITMAPINFOHEADER
) + ColorsSize
);
1925 pNewBmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1926 pNewBmi
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
1927 pNewBmi
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
1928 pNewBmi
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
1929 pNewBmi
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
1930 pNewBmi
->bmiHeader
.biCompression
= BI_RGB
;
1931 pNewBmi
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(pNewBmi
->bmiHeader
.biWidth
,
1932 pNewBmi
->bmiHeader
.biHeight
,
1933 pNewBmi
->bmiHeader
.biBitCount
);
1935 if(Usage
== DIB_PAL_COLORS
)
1937 RtlCopyMemory(pNewBmi
->bmiColors
, pbmci
->bmciColors
, ColorsSize
);
1942 for(i
=0; i
<numColors
; i
++)
1944 pNewBmi
->bmiColors
[i
].rgbRed
= pbmci
->bmciColors
[i
].rgbtRed
;
1945 pNewBmi
->bmiColors
[i
].rgbGreen
= pbmci
->bmciColors
[i
].rgbtGreen
;
1946 pNewBmi
->bmiColors
[i
].rgbBlue
= pbmci
->bmciColors
[i
].rgbtBlue
;
1953 /* Frees a BITMAPINFO created with DIB_ConvertBitmapInfo */
1956 DIB_FreeConvertedBitmapInfo(BITMAPINFO
* converted
, BITMAPINFO
* orig
, DWORD usage
)
1958 BITMAPCOREINFO
* pbmci
;
1959 if(converted
== orig
)
1964 /* Caller don't want any conversion */
1965 ExFreePoolWithTag(converted
, TAG_DIB
);
1969 /* Perform inverse conversion */
1970 pbmci
= (BITMAPCOREINFO
*)orig
;
1972 ASSERT(pbmci
->bmciHeader
.bcSize
== sizeof(BITMAPCOREHEADER
));
1973 pbmci
->bmciHeader
.bcBitCount
= converted
->bmiHeader
.biBitCount
;
1974 pbmci
->bmciHeader
.bcWidth
= converted
->bmiHeader
.biWidth
;
1975 pbmci
->bmciHeader
.bcHeight
= converted
->bmiHeader
.biHeight
;
1976 pbmci
->bmciHeader
.bcPlanes
= converted
->bmiHeader
.biPlanes
;
1978 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
1980 UINT numColors
= converted
->bmiHeader
.biClrUsed
;
1981 if(!numColors
) numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
1982 if(usage
== DIB_PAL_COLORS
)
1984 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(WORD
));
1985 RtlCopyMemory(pbmci
->bmciColors
, converted
->bmiColors
, numColors
* sizeof(WORD
));
1990 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(RGBTRIPLE
));
1991 for(i
=0; i
<numColors
; i
++)
1993 pbmci
->bmciColors
[i
].rgbtRed
= converted
->bmiColors
[i
].rgbRed
;
1994 pbmci
->bmciColors
[i
].rgbtGreen
= converted
->bmiColors
[i
].rgbGreen
;
1995 pbmci
->bmciColors
[i
].rgbtBlue
= converted
->bmiColors
[i
].rgbBlue
;
1999 /* Now free it, it's not needed anymore */
2000 ExFreePoolWithTag(converted
, TAG_DIB
);