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 RGB value from the DC palette, indexed by the DIB
153 crColor
= PALETTE_ulGetRGBColorFromIndex(ppalDC
, pwColors
[i
]);
154 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
157 else if (iUsage
== DIB_PAL_BRUSHHACK
)
159 /* The colors are an array of WORD indices into the DC palette */
160 PWORD pwColors
= (PWORD
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
162 /* Loop all color indices in the DIB */
163 for (i
= 0; i
< cColors
; i
++)
165 /* Set the index directly as the RGB color, the real palette
166 containing RGB values will be calculated when the brush is
168 PALETTE_vSetRGBColorForIndex(ppal
, i
, pwColors
[i
]);
171 /* Mark the palette as a brush hack palette */
172 ppal
->flFlags
|= PAL_BRUSHHACK
;
174 else if (iUsage
== 2)
176 // FIXME: this one is undocumented
179 else // if (iUsage == DIB_RGB_COLORS)
181 /* The colors are an array of RGBQUAD values */
182 RGBQUAD
*prgb
= (RGBQUAD
*)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
184 // FIXME: do we need to handle PALETTEINDEX / PALETTERGB macro?
186 /* Loop all color indices in the DIB */
187 for (i
= 0; i
< cColors
; i
++)
189 /* Get the color value and translate it to a COLORREF */
190 RGBQUAD rgb
= prgb
[i
];
191 COLORREF crColor
= RGB(rgb
.rgbRed
, rgb
.rgbGreen
, rgb
.rgbBlue
);
193 /* Set the RGB value in the palette */
194 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
200 /* This is a bitfield / RGB palette */
201 ULONG flRedMask
, flGreenMask
, flBlueMask
;
203 /* Check if the DIB contains bitfield values */
204 if (pbmi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
206 /* Check if we have a v4/v5 header */
207 if (pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPV4HEADER
))
209 /* The masks are dedicated fields in the header */
210 PBITMAPV4HEADER pbmV4Header
= (PBITMAPV4HEADER
)&pbmi
->bmiHeader
;
211 flRedMask
= pbmV4Header
->bV4RedMask
;
212 flGreenMask
= pbmV4Header
->bV4GreenMask
;
213 flBlueMask
= pbmV4Header
->bV4BlueMask
;
217 /* The masks are the first 3 values in the DIB color table */
218 PDWORD pdwColors
= (PVOID
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
219 flRedMask
= pdwColors
[0];
220 flGreenMask
= pdwColors
[1];
221 flBlueMask
= pdwColors
[2];
226 /* Check what bit depth we have. Note: optimization flags are
227 calculated in PALETTE_AllocPalette() */
228 if (pbmi
->bmiHeader
.biBitCount
== 16)
230 /* This is an RGB 555 palette */
232 flGreenMask
= 0x03E0;
237 /* This is an RGB 888 palette */
238 flRedMask
= 0xFF0000;
239 flGreenMask
= 0x00FF00;
240 flBlueMask
= 0x0000FF;
244 /* Allocate the bitfield palette */
245 ppal
= PALETTE_AllocPalette(PAL_BITFIELDS
,
253 /* We're done, return the palette */
264 CONST RGBQUAD
*Colors
)
272 if (!(dc
= DC_LockDc(hDC
))) return 0;
273 if (dc
->dctype
== DC_TYPE_INFO
)
279 psurf
= dc
->dclevel
.pSurface
;
283 EngSetLastError(ERROR_INVALID_PARAMETER
);
287 if (psurf
->hSecure
== NULL
)
290 EngSetLastError(ERROR_INVALID_PARAMETER
);
294 biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
295 if ((biBitCount
<= 8) && (StartIndex
< (1UL << biBitCount
)))
297 if (StartIndex
+ Entries
> (1UL << biBitCount
))
298 Entries
= (1 << biBitCount
) - StartIndex
;
300 if (psurf
->ppal
== NULL
)
303 EngSetLastError(ERROR_INVALID_HANDLE
);
307 PalGDI
= psurf
->ppal
;
309 for (Index
= StartIndex
;
310 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
313 PalGDI
->IndexedColors
[Index
].peRed
= Colors
[Index
- StartIndex
].rgbRed
;
314 PalGDI
->IndexedColors
[Index
].peGreen
= Colors
[Index
- StartIndex
].rgbGreen
;
315 PalGDI
->IndexedColors
[Index
].peBlue
= Colors
[Index
- StartIndex
].rgbBlue
;
321 /* Mark the brushes invalid */
322 dc
->pdcattr
->ulDirty_
|= DIRTY_FILL
|DIRTY_LINE
|DIRTY_BACKGROUND
|DIRTY_TEXT
;
340 UINT Index
, Count
= 0;
342 if (!(dc
= DC_LockDc(hDC
))) return 0;
343 if (dc
->dctype
== DC_TYPE_INFO
)
349 psurf
= dc
->dclevel
.pSurface
;
353 EngSetLastError(ERROR_INVALID_PARAMETER
);
357 if (psurf
->hSecure
== NULL
)
360 EngSetLastError(ERROR_INVALID_PARAMETER
);
367 if (ppal
->flFlags
& PAL_INDEXED
)
370 for (Index
= StartIndex
;
371 Index
< StartIndex
+ Entries
&& Index
< ppal
->NumColors
;
374 Colors
[Index
- StartIndex
].rgbRed
= ppal
->IndexedColors
[Index
].peRed
;
375 Colors
[Index
- StartIndex
].rgbGreen
= ppal
->IndexedColors
[Index
].peGreen
;
376 Colors
[Index
- StartIndex
].rgbBlue
= ppal
->IndexedColors
[Index
].peBlue
;
377 Colors
[Index
- StartIndex
].rgbReserved
= 0;
387 // Converts a DIB to a device-dependent bitmap
396 CONST BITMAPINFO
*bmi
,
399 HBITMAP SourceBitmap
;
400 PSURFACE psurfDst
, psurfSrc
;
405 PPALETTE ppalDIB
= 0;
407 SourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
410 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
411 bmi
->bmiHeader
.biCompression
),
412 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
413 bmi
->bmiHeader
.biSizeImage
,
418 DPRINT1("Error: Could not create a bitmap.\n");
419 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
423 psurfDst
= SURFACE_ShareLockSurface(hBitmap
);
424 psurfSrc
= SURFACE_ShareLockSurface(SourceBitmap
);
426 if(!(psurfSrc
&& psurfDst
))
428 DPRINT1("Error: Could not lock surfaces\n");
432 /* Create a palette for the DIB */
433 ppalDIB
= CreateDIBPalette(bmi
, DC
, ColorUse
);
436 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
440 /* Initialize EXLATEOBJ */
441 EXLATEOBJ_vInitialize(&exlo
,
444 RGB(0xff, 0xff, 0xff),
445 RGB(0xff, 0xff, 0xff), //DC->pdcattr->crBackgroundClr,
446 0); // DC->pdcattr->crForegroundClr);
448 rcDst
.top
= StartScan
;
450 rcDst
.bottom
= rcDst
.top
+ ScanLines
;
451 rcDst
.right
= psurfDst
->SurfObj
.sizlBitmap
.cx
;
455 result
= IntEngCopyBits(&psurfDst
->SurfObj
,
464 EXLATEOBJ_vCleanup(&exlo
);
467 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
468 if(psurfSrc
) SURFACE_ShareUnlockSurface(psurfSrc
);
469 if(psurfDst
) SURFACE_ShareUnlockSurface(psurfDst
);
470 GreDeleteObject(SourceBitmap
);
478 NtGdiSetDIBitsToDeviceInternal(
493 IN BOOL bTransformCoordinates
,
494 IN OPTIONAL HANDLE hcmXform
)
497 NTSTATUS Status
= STATUS_SUCCESS
;
499 HBITMAP hSourceBitmap
= NULL
;
500 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
;
507 PPALETTE ppalDIB
= NULL
;
508 LPBITMAPINFO pbmiSafe
;
512 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjMaxInfo
, 'pmTG');
513 if (!pbmiSafe
) return 0;
517 ProbeForRead(bmi
, cjMaxInfo
, 1);
518 ProbeForRead(Bits
, cjMaxBits
, 1);
519 RtlCopyMemory(pbmiSafe
, bmi
, cjMaxInfo
);
522 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
524 Status
= _SEH2_GetExceptionCode();
528 if (!NT_SUCCESS(Status
))
533 pDC
= DC_LockDc(hDC
);
536 EngSetLastError(ERROR_INVALID_HANDLE
);
539 if (pDC
->dctype
== DC_TYPE_INFO
)
545 pSurf
= pDC
->dclevel
.pSurface
;
547 pDestSurf
= pSurf
? &pSurf
->SurfObj
: NULL
;
549 ScanLines
= min(ScanLines
, abs(bmi
->bmiHeader
.biHeight
) - StartScan
);
553 if (bTransformCoordinates
)
555 IntLPtoDP(pDC
, (LPPOINT
)&rcDest
, 2);
557 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
558 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
559 rcDest
.right
= rcDest
.left
+ Width
;
560 rcDest
.bottom
= rcDest
.top
+ Height
;
561 rcDest
.top
+= StartScan
;
566 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
567 SourceSize
.cy
= ScanLines
;
569 //DIBWidth = WIDTH_BYTES_ALIGN32(SourceSize.cx, bmi->bmiHeader.biBitCount);
571 hSourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
574 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
575 bmi
->bmiHeader
.biCompression
),
576 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
577 bmi
->bmiHeader
.biSizeImage
,
583 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
584 Status
= STATUS_NO_MEMORY
;
588 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
591 Status
= STATUS_UNSUCCESSFUL
;
597 /* Create a palette for the DIB */
598 ppalDIB
= CreateDIBPalette(bmi
, pDC
, ColorUse
);
601 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
602 Status
= STATUS_NO_MEMORY
;
606 /* Initialize EXLATEOBJ */
607 EXLATEOBJ_vInitialize(&exlo
,
610 RGB(0xff, 0xff, 0xff),
611 pDC
->pdcattr
->crBackgroundClr
,
612 pDC
->pdcattr
->crForegroundClr
);
615 DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
616 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
617 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
618 Status
= IntEngBitBlt(pDestSurf
,
621 pDC
->rosdc
.CombinedClip
,
628 ROP4_FROM_INDEX(R3_OPINDEX_SRCCOPY
));
630 /* Cleanup EXLATEOBJ */
631 EXLATEOBJ_vCleanup(&exlo
);
634 if (NT_SUCCESS(Status
))
639 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
641 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
642 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
645 ExFreePool(pbmiSafe
);
650 /* Converts a device-dependent bitmap to a DIB */
653 NtGdiGetDIBitsInternal(
664 BITMAPCOREINFO
* pbmci
= NULL
;
665 PSURFACE psurf
= NULL
;
672 RGBTRIPLE
* rgbTriples
;
675 NTSTATUS Status
= STATUS_SUCCESS
;
677 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
679 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
684 /* Probe for read and write */
685 ProbeForRead(Info
, MaxInfo
, 1);
686 ProbeForWrite(Info
, MaxInfo
, 1);
687 if (Bits
) ProbeForWrite(Bits
, MaxBits
, 1);
689 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
691 Status
= _SEH2_GetExceptionCode();
695 if (!NT_SUCCESS(Status
))
700 colorPtr
= (LPBYTE
)Info
+ Info
->bmiHeader
.biSize
;
701 rgbTriples
= colorPtr
;
704 bitmap_type
= DIB_GetBitmapInfo(&Info
->bmiHeader
,
711 if(bitmap_type
== -1)
713 DPRINT("Wrong bitmap format\n");
714 EngSetLastError(ERROR_INVALID_PARAMETER
);
717 else if(bitmap_type
== 0)
719 /* We need a BITMAPINFO to create a DIB, but we have to fill
720 * the BITMAPCOREINFO we're provided */
721 pbmci
= (BITMAPCOREINFO
*)Info
;
722 Info
= DIB_ConvertBitmapInfo((BITMAPINFO
*)pbmci
, Usage
);
725 DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
728 rgbQuads
= Info
->bmiColors
;
731 pDC
= DC_LockDc(hDC
);
732 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
738 /* Get a pointer to the source bitmap object */
739 psurf
= SURFACE_ShareLockSurface(hBitmap
);
746 /* Fill in the structure */
749 case 0: /* Only info */
752 pbmci
->bmciHeader
.bcWidth
= (WORD
)psurf
->SurfObj
.sizlBitmap
.cx
;
753 pbmci
->bmciHeader
.bcHeight
= (WORD
)((psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
754 -psurf
->SurfObj
.sizlBitmap
.cy
:
755 psurf
->SurfObj
.sizlBitmap
.cy
);
756 pbmci
->bmciHeader
.bcPlanes
= 1;
757 pbmci
->bmciHeader
.bcBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
759 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
760 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
761 -psurf
->SurfObj
.sizlBitmap
.cy
:
762 psurf
->SurfObj
.sizlBitmap
.cy
;;
763 Info
->bmiHeader
.biPlanes
= 1;
764 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
765 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
766 Info
->bmiHeader
.biHeight
,
767 Info
->bmiHeader
.biBitCount
);
770 switch(Info
->bmiHeader
.biBitCount
)
774 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
777 Info
->bmiHeader
.biCompression
= BI_RGB
;
781 else if(Info
->bmiHeader
.biBitCount
> 8)
783 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
787 Info
->bmiHeader
.biCompression
= BI_RGB
;
789 Info
->bmiHeader
.biXPelsPerMeter
= 0;
790 Info
->bmiHeader
.biYPelsPerMeter
= 0;
791 Info
->bmiHeader
.biClrUsed
= 0;
792 Info
->bmiHeader
.biClrImportant
= 0;
793 ScanLines
= abs(Info
->bmiHeader
.biHeight
);
799 Info
->bmiHeader
.biClrUsed
= 0;
801 /* If the bitmap if a DIB section and has the same format than what
802 * we're asked, go ahead! */
803 if((psurf
->hSecure
) &&
804 (BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bpp
))
806 if(Usage
== DIB_RGB_COLORS
)
808 ULONG colors
= min(psurf
->ppal
->NumColors
, 256);
812 for(i
= 0; i
< colors
; i
++)
814 rgbTriples
[i
].rgbtRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
815 rgbTriples
[i
].rgbtGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
816 rgbTriples
[i
].rgbtBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
819 if(colors
!= 256) Info
->bmiHeader
.biClrUsed
= colors
;
820 for(i
= 0; i
< colors
; i
++)
822 rgbQuads
[i
].rgbRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
823 rgbQuads
[i
].rgbGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
824 rgbQuads
[i
].rgbBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
829 for(i
= 0; i
< 256; i
++)
831 if(pbmci
) ((WORD
*)rgbTriples
)[i
] = i
;
832 ((WORD
*)rgbQuads
)[i
] = i
;
838 if(Usage
== DIB_PAL_COLORS
)
840 for(i
= 0; i
< 256; i
++)
842 if(pbmci
) ((WORD
*)rgbTriples
)[i
] = i
;
843 ((WORD
*)rgbQuads
)[i
] = i
;
846 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
))
848 /* For color DDBs in native depth (mono DDBs always have
849 a black/white palette):
850 Generate the color map from the selected palette */
851 PPALETTE pDcPal
= PALETTE_ShareLockPalette(pDC
->dclevel
.hpal
);
857 for (i
= 0; i
< pDcPal
->NumColors
; i
++)
861 rgbTriples
[i
].rgbtRed
= pDcPal
->IndexedColors
[i
].peRed
;
862 rgbTriples
[i
].rgbtGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
863 rgbTriples
[i
].rgbtBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
866 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
867 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
868 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
869 rgbQuads
[i
].rgbReserved
= 0;
871 PALETTE_ShareUnlockPalette(pDcPal
);
880 rgbTriples
[0].rgbtRed
= rgbTriples
[0].rgbtGreen
=
881 rgbTriples
[0].rgbtBlue
= 0;
882 rgbTriples
[1].rgbtRed
= rgbTriples
[1].rgbtGreen
=
883 rgbTriples
[1].rgbtBlue
= 0xff;
885 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
=
886 rgbQuads
[0].rgbBlue
= 0;
887 rgbQuads
[0].rgbReserved
= 0;
888 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
=
889 rgbQuads
[1].rgbBlue
= 0xff;
890 rgbQuads
[1].rgbReserved
= 0;
895 RtlCopyMemory(rgbTriples
, EGAColorsTriples
, sizeof(EGAColorsTriples
));
896 RtlCopyMemory(rgbQuads
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
906 RGBTRIPLE
*colorTriple
;
908 RtlCopyMemory(rgbTriples
, DefLogPaletteTriples
,
909 10 * sizeof(RGBTRIPLE
));
910 RtlCopyMemory(rgbTriples
+ 246, DefLogPaletteTriples
+ 10,
911 10 * sizeof(RGBTRIPLE
));
912 colorTriple
= rgbTriples
+ 10;
913 for(r
= 0; r
<= 5; r
++) /* FIXME */
915 for(g
= 0; g
<= 5; g
++)
917 for(b
= 0; b
<= 5; b
++)
919 colorTriple
->rgbtRed
= (r
* 0xff) / 5;
920 colorTriple
->rgbtGreen
= (g
* 0xff) / 5;
921 colorTriple
->rgbtBlue
= (b
* 0xff) / 5;
927 memcpy(rgbQuads
, DefLogPaletteQuads
,
928 10 * sizeof(RGBQUAD
));
929 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10,
930 10 * sizeof(RGBQUAD
));
931 color
= rgbQuads
+ 10;
932 for(r
= 0; r
<= 5; r
++) /* FIXME */
934 for(g
= 0; g
<= 5; g
++)
936 for(b
= 0; b
<= 5; b
++)
938 color
->rgbRed
= (r
* 0xff) / 5;
939 color
->rgbGreen
= (g
* 0xff) / 5;
940 color
->rgbBlue
= (b
* 0xff) / 5;
941 color
->rgbReserved
= 0;
953 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
955 ((PDWORD
)Info
->bmiColors
)[0] = 0x7c00;
956 ((PDWORD
)Info
->bmiColors
)[1] = 0x03e0;
957 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
962 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
966 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
967 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
968 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
972 ((PDWORD
)Info
->bmiColors
)[0] = 0xf800;
973 ((PDWORD
)Info
->bmiColors
)[1] = 0x07e0;
974 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
981 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
985 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
986 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
987 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
991 ((PDWORD
)Info
->bmiColors
)[0] = 0xff0000;
992 ((PDWORD
)Info
->bmiColors
)[1] = 0x00ff00;
993 ((PDWORD
)Info
->bmiColors
)[2] = 0x0000ff;
998 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(width
, height
, bpp
);
1000 if(Bits
&& ScanLines
)
1002 /* Create a DIBSECTION, blt it, profit */
1011 if (StartScan
> (ULONG
)psurf
->SurfObj
.sizlBitmap
.cy
)
1018 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
1022 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
1023 Info
->bmiHeader
.biHeight
= (height
< 0) ?
1024 -(LONG
)ScanLines
: ScanLines
;
1025 /* Create the DIB */
1026 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
1028 Info
->bmiHeader
.biWidth
= width
;
1029 Info
->bmiHeader
.biHeight
= height
;
1033 DPRINT1("Unable to create a DIB Section!\n");
1034 EngSetLastError(ERROR_INVALID_PARAMETER
);
1039 psurfDest
= SURFACE_ShareLockSurface(hBmpDest
);
1043 rcDest
.bottom
= ScanLines
;
1044 rcDest
.right
= psurf
->SurfObj
.sizlBitmap
.cx
;
1052 if(ScanLines
<= StartScan
)
1055 SURFACE_ShareUnlockSurface(psurfDest
);
1056 GreDeleteObject(hBmpDest
);
1060 ScanLines
-= StartScan
;
1064 srcPoint
.y
= StartScan
;
1067 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xffffff, 0xffffff, 0);
1069 ret
= IntEngCopyBits(&psurfDest
->SurfObj
,
1076 SURFACE_ShareUnlockSurface(psurfDest
);
1082 Status
= STATUS_SUCCESS
;
1085 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, ScanLines
, bpp
));
1087 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1089 Status
= _SEH2_GetExceptionCode();
1093 if(!NT_SUCCESS(Status
))
1095 DPRINT1("Unable to copy bits to the user provided pointer\n");
1100 GreDeleteObject(hBmpDest
);
1101 EXLATEOBJ_vCleanup(&exlo
);
1103 else ScanLines
= abs(height
);
1107 if(pDC
) DC_UnlockDc(pDC
);
1108 if(psurf
) SURFACE_ShareUnlockSurface(psurf
);
1109 if(pbmci
) DIB_FreeConvertedBitmapInfo(Info
, (BITMAPINFO
*)pbmci
);
1114 #define ROP_TO_ROP4(Rop) ((Rop) >> 16)
1119 NtGdiStretchDIBitsInternal(
1129 IN OPTIONAL LPBYTE pjInit
,
1130 IN LPBITMAPINFO pbmi
,
1132 IN DWORD dwRop
, // MS ntgdi.h says dwRop4(?)
1137 BOOL bResult
= FALSE
;
1142 PSURFACE psurfTmp
= 0, psurfDst
= 0;
1143 HPALETTE hpalDIB
= 0;
1144 PPALETTE ppalDIB
= 0;
1148 if (!(pdc
= DC_LockDc(hdc
)))
1150 EngSetLastError(ERROR_INVALID_HANDLE
);
1154 /* Transform dest size */
1157 IntLPtoDP(pdc
, (POINTL
*)&sizel
, 1);
1160 /* Check if we can use NtGdiSetDIBitsToDeviceInternal */
1161 if (sizel
.cx
== cxSrc
&& sizel
.cy
== cySrc
&& dwRop
== SRCCOPY
)
1164 return NtGdiSetDIBitsToDeviceInternal(hdc
,
1182 pvBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, 'pmeT');
1190 ProbeForRead(pjInit
, cjMaxBits
, 1);
1191 RtlCopyMemory(pvBits
, pjInit
, cjMaxBits
);
1193 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1195 _SEH2_YIELD(return 0);
1199 /* FIXME: Locking twice is cheesy, coord tranlation in UM will fix it */
1200 if (!(pdc
= DC_LockDc(hdc
)))
1202 DPRINT1("Could not lock dc\n");
1203 EngSetLastError(ERROR_INVALID_HANDLE
);
1207 psurfDst
= pdc
->dclevel
.pSurface
;
1215 /* Calculate source and destination rect */
1218 rcSrc
.right
= xSrc
+ abs(cxSrc
);
1219 rcSrc
.bottom
= ySrc
+ abs(cySrc
);
1222 rcDst
.right
= rcDst
.left
+ cxDst
;
1223 rcDst
.bottom
= rcDst
.top
+ cyDst
;
1224 IntLPtoDP(pdc
, (POINTL
*)&rcDst
, 2);
1225 RECTL_vOffsetRect(&rcDst
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1227 hbmTmp
= GreCreateBitmapEx(pbmi
->bmiHeader
.biWidth
,
1228 pbmi
->bmiHeader
.biHeight
,
1230 BitmapFormat(pbmi
->bmiHeader
.biBitCount
,
1231 pbmi
->bmiHeader
.biCompression
),
1232 pbmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
1233 pbmi
->bmiHeader
.biSizeImage
,
1243 psurfTmp
= SURFACE_ShareLockSurface(hbmTmp
);
1250 /* Create a palette for the DIB */
1251 ppalDIB
= CreateDIBPalette(pbmi
, pdc
, dwUsage
);
1258 /* Initialize XLATEOBJ */
1259 EXLATEOBJ_vInitialize(&exlo
,
1262 RGB(0xff, 0xff, 0xff),
1263 pdc
->pdcattr
->crBackgroundClr
,
1264 pdc
->pdcattr
->crForegroundClr
);
1266 /* Prepare DC for blit */
1267 DC_vPrepareDCsForBlit(pdc
, rcDst
, NULL
, rcSrc
);
1269 /* Perform the stretch operation */
1270 bResult
= IntEngStretchBlt(&psurfDst
->SurfObj
,
1273 pdc
->rosdc
.CombinedClip
,
1278 &pdc
->eboFill
.BrushObject
,
1280 ROP_TO_ROP4(dwRop
));
1283 DC_vFinishBlit(pdc
, NULL
);
1284 EXLATEOBJ_vCleanup(&exlo
);
1286 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
1287 if (psurfTmp
) SURFACE_ShareUnlockSurface(psurfTmp
);
1288 if (hbmTmp
) GreDeleteObject(hbmTmp
);
1289 if (pdc
) DC_UnlockDc(pdc
);
1290 ExFreePoolWithTag(pvBits
, 'pmeT');
1311 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1312 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1314 if (bpp
!= 1) fColor
= TRUE
;
1315 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1318 const RGBQUAD
*rgb
= (RGBQUAD
*)((PBYTE
)data
+ data
->bmiHeader
.biSize
);
1319 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1321 // Check if the first color of the colormap is black
1322 if ((col
== RGB(0, 0, 0)))
1325 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1327 // If the second color is white, create a monochrome bitmap
1328 fColor
= (col
!= RGB(0xff,0xff,0xff));
1333 // Now create the bitmap
1336 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
1340 handle
= GreCreateBitmap(width
,
1350 if (NULL
!= handle
&& CBM_INIT
== init
)
1352 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
1358 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1359 // The DDB that is created will be whatever bit depth your reference DC is
1362 NtGdiCreateDIBitmapInternal(
1367 IN OPTIONAL LPBYTE pjInit
,
1368 IN OPTIONAL LPBITMAPINFO pbmi
,
1370 IN UINT cjMaxInitInfo
,
1375 NTSTATUS Status
= STATUS_SUCCESS
;
1376 PBYTE safeBits
= NULL
;
1377 HBITMAP hbmResult
= NULL
;
1379 if(pjInit
&& (fInit
== CBM_INIT
))
1381 safeBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, TAG_DIB
);
1384 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1391 if(pbmi
) ProbeForRead(pbmi
, cjMaxInitInfo
, 1);
1392 if(pjInit
&& (fInit
== CBM_INIT
))
1394 ProbeForRead(pjInit
, cjMaxBits
, 1);
1395 RtlCopyMemory(safeBits
, pjInit
, cjMaxBits
);
1398 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1400 Status
= _SEH2_GetExceptionCode();
1404 if(!NT_SUCCESS(Status
))
1406 SetLastNtError(Status
);
1410 hbmResult
= GreCreateDIBitmapInternal(hDc
,
1422 if (safeBits
) ExFreePoolWithTag(safeBits
, TAG_DIB
);
1428 GreCreateDIBitmapInternal(
1433 IN OPTIONAL LPBYTE pjInit
,
1434 IN OPTIONAL PBITMAPINFO pbmi
,
1445 if (!hDc
) /* 1bpp monochrome bitmap */
1447 // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1448 hdcDest
= NtGdiCreateCompatibleDC(0);
1459 Dc
= DC_LockDc(hdcDest
);
1462 EngSetLastError(ERROR_INVALID_HANDLE
);
1465 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1466 * if bpp != 1 and ignore the real value that was passed */
1468 bpp
= pbmi
->bmiHeader
.biBitCount
;
1471 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
, iUsage
);
1476 NtGdiDeleteObjectApp(hdcDest
);
1484 NtGdiCreateDIBSection(
1486 IN OPTIONAL HANDLE hSection
,
1492 IN ULONG_PTR dwColorSpace
,
1495 HBITMAP hbitmap
= 0;
1497 BOOL bDesktopDC
= FALSE
;
1498 NTSTATUS Status
= STATUS_SUCCESS
;
1500 if (!bmi
) return hbitmap
; // Make sure.
1504 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1505 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1506 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, (WORD
)Usage
), 1);
1508 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1510 Status
= _SEH2_GetExceptionCode();
1514 if(!NT_SUCCESS(Status
))
1516 SetLastNtError(Status
);
1520 // If the reference hdc is null, take the desktop dc
1523 hDC
= NtGdiCreateCompatibleDC(0);
1527 if ((dc
= DC_LockDc(hDC
)))
1529 hbitmap
= DIB_CreateDIBSection(dc
,
1540 EngSetLastError(ERROR_INVALID_HANDLE
);
1544 NtGdiDeleteObjectApp(hDC
);
1551 DIB_CreateDIBSection(
1553 CONST BITMAPINFO
*bmi
,
1561 SURFACE
*bmp
= NULL
;
1562 void *mapBits
= NULL
;
1565 // Fill BITMAP32 structure with DIB data
1566 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1573 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1574 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1575 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1577 /* CreateDIBSection should fail for compressed formats */
1578 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1580 DPRINT1("no compressed format allowed\n");
1581 return (HBITMAP
)NULL
;
1584 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1586 bm
.bmWidth
= bi
->biWidth
;
1587 bm
.bmHeight
= effHeight
;
1588 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: WIDTH_BYTES_ALIGN32(bm
.bmWidth
, bi
->biBitCount
);
1590 bm
.bmPlanes
= bi
->biPlanes
;
1591 bm
.bmBitsPixel
= bi
->biBitCount
;
1594 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1595 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1596 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
&& bi
->biCompression
!= BI_BITFIELDS
1597 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1601 SYSTEM_BASIC_INFORMATION Sbi
;
1604 LARGE_INTEGER SectionOffset
;
1607 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1611 if (!NT_SUCCESS(Status
))
1613 DPRINT1("ZwQuerySystemInformation failed (0x%lx)\n", Status
);
1617 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1618 mapSize
= bi
->biSizeImage
+ (offset
- mapOffset
);
1620 SectionOffset
.LowPart
= mapOffset
;
1621 SectionOffset
.HighPart
= 0;
1623 Status
= ZwMapViewOfSection(section
,
1633 if (!NT_SUCCESS(Status
))
1635 DPRINT1("ZwMapViewOfSection failed (0x%lx)\n", Status
);
1636 EngSetLastError(ERROR_INVALID_PARAMETER
);
1640 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1642 else if (ovr_pitch
&& offset
)
1643 bm
.bmBits
= (LPVOID
) offset
;
1647 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1650 DPRINT1("Failed to allocate memory\n");
1655 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1656 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1659 // Create Device Dependent Bitmap and add DIB pointer
1660 //Size.cx = bm.bmWidth;
1661 //Size.cy = abs(bm.bmHeight);
1662 res
= GreCreateBitmapEx(bm
.bmWidth
,
1665 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1666 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1667 ((bi
->biHeight
< 0) ? BMF_TOPDOWN
: 0),
1673 DPRINT1("GreCreateBitmapEx failed\n");
1674 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
1677 bmp
= SURFACE_ShareLockSurface(res
); // HACK
1680 DPRINT1("SURFACE_LockSurface failed\n");
1681 EngSetLastError(ERROR_INVALID_HANDLE
);
1685 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1686 table between the DIB and the X physical device. Obviously,
1687 this is left out of the ReactOS implementation. Instead,
1688 we call NtGdiSetDIBColorTable. */
1689 bmp
->hDIBSection
= section
;
1690 bmp
->hSecure
= hSecure
;
1691 bmp
->dwOffset
= offset
;
1692 bmp
->flags
= API_BITMAP
;
1693 bmp
->biClrImportant
= bi
->biClrImportant
;
1695 /* Create a palette for the DIB */
1696 ppalDIB
= CreateDIBPalette(bmi
, dc
, usage
);
1699 if (bmp
->ppal
) PALETTE_ShareUnlockPalette(bmp
->ppal
);
1700 bmp
->ppal
= ppalDIB
;
1703 // Clean up in case of errors
1705 if (!res
|| !bmp
|| !bm
.bmBits
)
1707 DPRINT("Got an error res=%08x, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1710 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1713 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1717 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1725 GreDeleteObject(res
);
1732 SURFACE_ShareUnlockSurface(bmp
);
1735 // Return BITMAP handle and storage location
1736 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1744 /***********************************************************************
1747 * Get the info from a bitmap header.
1748 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
1752 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
1753 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
1755 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1757 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
1758 *width
= core
->bcWidth
;
1759 *height
= core
->bcHeight
;
1760 *planes
= core
->bcPlanes
;
1761 *bpp
= core
->bcBitCount
;
1766 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* Assume BITMAPINFOHEADER */
1768 *width
= header
->biWidth
;
1769 *height
= header
->biHeight
;
1770 *planes
= header
->biPlanes
;
1771 *bpp
= header
->biBitCount
;
1772 *compr
= header
->biCompression
;
1773 *size
= header
->biSizeImage
;
1776 DPRINT1("(%d): unknown/wrong size for header\n", header
->biSize
);
1780 /***********************************************************************
1781 * DIB_GetDIBImageBytes
1783 * Return the number of bytes used to hold the image in a DIB bitmap.
1784 * 11/16/1999 (RJJ) lifted from wine
1787 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1789 return WIDTH_BYTES_ALIGN32(width
, depth
) * (height
< 0 ? -height
: height
);
1792 /***********************************************************************
1793 * DIB_BitmapInfoSize
1795 * Return the size of the bitmap info structure including color table.
1796 * 11/16/1999 (RJJ) lifted from wine
1799 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1801 unsigned int colors
, size
, masks
= 0;
1803 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1805 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
1806 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1807 return sizeof(BITMAPCOREHEADER
) + colors
*
1808 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1810 else /* Assume BITMAPINFOHEADER */
1812 colors
= info
->bmiHeader
.biClrUsed
;
1813 if (colors
> 256) colors
= 256;
1814 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
1815 colors
= 1 << info
->bmiHeader
.biBitCount
;
1816 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
1817 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
1818 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1824 DIB_MapPaletteColors(PPALETTE ppalDc
, CONST BITMAPINFO
* lpbmi
)
1831 if (!(ppalDc
->flFlags
& PAL_INDEXED
))
1836 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1837 if (lpbmi
->bmiHeader
.biClrUsed
)
1839 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1842 ppalNew
= PALETTE_AllocPalWithHandle(PAL_INDEXED
, nNumColors
, NULL
, 0, 0, 0);
1843 if (ppalNew
== NULL
)
1845 DPRINT1("Could not allocate palette\n");
1849 lpIndex
= (USHORT
*)((PBYTE
)lpbmi
+ lpbmi
->bmiHeader
.biSize
);
1851 for (i
= 0; i
< nNumColors
; i
++)
1853 ULONG iColorIndex
= *lpIndex
% ppalDc
->NumColors
;
1854 ppalNew
->IndexedColors
[i
] = ppalDc
->IndexedColors
[iColorIndex
];
1858 hpal
= ppalNew
->BaseObject
.hHmgr
;
1859 PALETTE_UnlockPalette(ppalNew
);
1864 /* Converts a BITMAPCOREINFO to a BITMAPINFO structure,
1865 * or does nothing if it's already a BITMAPINFO (or V4 or V5) */
1868 DIB_ConvertBitmapInfo (CONST BITMAPINFO
* pbmi
, DWORD Usage
)
1870 CONST BITMAPCOREINFO
* pbmci
= (BITMAPCOREINFO
*)pbmi
;
1871 BITMAPINFO
* pNewBmi
;
1872 UINT numColors
= 0, ColorsSize
= 0;
1874 if(pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) return (BITMAPINFO
*)pbmi
;
1875 if(pbmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) return NULL
;
1877 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
1879 numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
1880 if(Usage
== DIB_PAL_COLORS
)
1882 ColorsSize
= numColors
* sizeof(WORD
);
1886 ColorsSize
= numColors
* sizeof(RGBQUAD
);
1889 else if (Usage
== DIB_PAL_COLORS
)
1891 /* Invalid at high-res */
1895 pNewBmi
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + ColorsSize
, TAG_DIB
);
1896 if(!pNewBmi
) return NULL
;
1898 RtlZeroMemory(pNewBmi
, sizeof(BITMAPINFOHEADER
) + ColorsSize
);
1900 pNewBmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1901 pNewBmi
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
1902 pNewBmi
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
1903 pNewBmi
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
1904 pNewBmi
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
1905 pNewBmi
->bmiHeader
.biCompression
= BI_RGB
;
1906 pNewBmi
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(pNewBmi
->bmiHeader
.biWidth
,
1907 pNewBmi
->bmiHeader
.biHeight
,
1908 pNewBmi
->bmiHeader
.biBitCount
);
1910 if(Usage
== DIB_PAL_COLORS
)
1912 RtlCopyMemory(pNewBmi
->bmiColors
, pbmci
->bmciColors
, ColorsSize
);
1917 for(i
=0; i
<numColors
; i
++)
1919 pNewBmi
->bmiColors
[i
].rgbRed
= pbmci
->bmciColors
[i
].rgbtRed
;
1920 pNewBmi
->bmiColors
[i
].rgbGreen
= pbmci
->bmciColors
[i
].rgbtGreen
;
1921 pNewBmi
->bmiColors
[i
].rgbBlue
= pbmci
->bmciColors
[i
].rgbtBlue
;
1928 /* Frees a BITMAPINFO created with DIB_ConvertBitmapInfo */
1931 DIB_FreeConvertedBitmapInfo(BITMAPINFO
* converted
, BITMAPINFO
* orig
)
1933 if(converted
!= orig
)
1934 ExFreePoolWithTag(converted
, TAG_DIB
);