2 * PROJECT: ReactOS win32 kernel mode subsystem
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: win32ss/gdi/ntgdi/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
252 CONST BITMAPINFO
*bmi
,
255 HBITMAP SourceBitmap
;
256 PSURFACE psurfDst
, psurfSrc
;
261 PPALETTE ppalDIB
= 0;
265 if (!bmi
|| !Bits
) return 0;
267 /* Check for uncompressed formats */
268 if ((bmi
->bmiHeader
.biCompression
== BI_RGB
) ||
269 (bmi
->bmiHeader
.biCompression
== BI_BITFIELDS
))
271 /* Calculate the image size */
272 cjSizeImage
= DIB_GetDIBImageBytes(bmi
->bmiHeader
.biWidth
,
274 bmi
->bmiHeader
.biBitCount
);
277 /* Check if the header provided an image size */
278 else if (bmi
->bmiHeader
.biSizeImage
!= 0)
280 /* Use the given size */
281 cjSizeImage
= bmi
->bmiHeader
.biSizeImage
;
286 /* Compressed format without a size. This is invalid. */
287 DPRINT1("Compressed format without a size!");
291 /* Check if the size that we have is ok */
292 if (cjSizeImage
> cjMaxBits
)
294 DPRINT1("Size too large! cjSizeImage = %lu, cjMaxBits = %lu\n",
295 cjSizeImage
, cjMaxBits
);
299 /* We cannot use the provided buffer for uncompressed bits, since they
300 might not be aligned correctly! */
301 SourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
304 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
305 bmi
->bmiHeader
.biCompression
),
306 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
308 bCompressed
? (PVOID
)Bits
: NULL
,
312 DPRINT1("Error: Could not create a bitmap.\n");
313 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
317 psurfDst
= SURFACE_ShareLockSurface(hBitmap
);
318 psurfSrc
= SURFACE_ShareLockSurface(SourceBitmap
);
320 if(!(psurfSrc
&& psurfDst
))
322 DPRINT1("Error: Could not lock surfaces\n");
326 /* Check for uncompressed bits */
329 /* Copy the bitmap bits */
330 if (!UnsafeSetBitmapBits(psurfSrc
, cjMaxBits
, Bits
))
332 DPRINT1("Error: Could not set bitmap bits\n");
337 /* Create a palette for the DIB */
338 ppalDIB
= CreateDIBPalette(bmi
, DC
, ColorUse
);
341 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
345 /* Initialize EXLATEOBJ */
346 EXLATEOBJ_vInitialize(&exlo
,
349 RGB(0xff, 0xff, 0xff),
350 RGB(0xff, 0xff, 0xff), //DC->pdcattr->crBackgroundClr,
351 0); // DC->pdcattr->crForegroundClr);
353 rcDst
.top
= StartScan
;
355 rcDst
.bottom
= rcDst
.top
+ ScanLines
;
356 rcDst
.right
= psurfDst
->SurfObj
.sizlBitmap
.cx
;
360 result
= IntEngCopyBits(&psurfDst
->SurfObj
,
369 EXLATEOBJ_vCleanup(&exlo
);
372 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
373 if(psurfSrc
) SURFACE_ShareUnlockSurface(psurfSrc
);
374 if(psurfDst
) SURFACE_ShareUnlockSurface(psurfDst
);
375 GreDeleteObject(SourceBitmap
);
382 IntGdiCreateMaskFromRLE(
393 BYTE Data
, NumPixels
, ToSkip
;
395 ASSERT((Compression
== BI_RLE8
) || (Compression
== BI_RLE4
));
397 /* Create the bitmap */
398 Mask
= GreCreateBitmapEx(Width
, Height
, 0, BMF_1BPP
, 0, 0, NULL
, 0);
402 SurfObj
= EngLockSurface((HSURF
)Mask
);
405 GreDeleteObject(Mask
);
408 ASSERT(SurfObj
->pvBits
!= NULL
);
420 if ((x
+ NumPixels
) > Width
)
421 NumPixels
= Width
- x
;
426 DIB_1BPP_HLine(SurfObj
, x
, x
+ NumPixels
, y
, 1);
447 if (i
>= (BitsSize
- 1))
458 /* Done for this run */
462 /* Embedded data into the RLE */
464 if (Compression
== BI_RLE8
)
467 ToSkip
= (NumPixels
/ 2) + (NumPixels
& 1);
469 if ((i
+ ToSkip
) > BitsSize
)
471 ToSkip
= (ToSkip
+ 1) & ~1;
473 if ((x
+ NumPixels
) > Width
)
474 NumPixels
= Width
- x
;
478 DIB_1BPP_HLine(SurfObj
, x
, x
+ NumPixels
, y
, 1);
485 EngUnlockSurface(SurfObj
);
492 NtGdiSetDIBitsToDeviceInternal(
507 IN BOOL bTransformCoordinates
,
508 IN OPTIONAL HANDLE hcmXform
)
511 NTSTATUS Status
= STATUS_SUCCESS
;
513 HBITMAP hSourceBitmap
= NULL
, hMaskBitmap
= NULL
;
514 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
, *pMaskSurf
= NULL
;
521 PPALETTE ppalDIB
= NULL
;
522 LPBITMAPINFO pbmiSafe
;
526 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjMaxInfo
, 'pmTG');
527 if (!pbmiSafe
) return 0;
531 ProbeForRead(bmi
, cjMaxInfo
, 1);
532 ProbeForRead(Bits
, cjMaxBits
, 1);
533 RtlCopyMemory(pbmiSafe
, bmi
, cjMaxInfo
);
536 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
538 Status
= _SEH2_GetExceptionCode();
542 if (!NT_SUCCESS(Status
))
547 ScanLines
= min(ScanLines
, abs(bmi
->bmiHeader
.biHeight
) - StartScan
);
550 DPRINT1("ScanLines == 0\n");
554 pDC
= DC_LockDc(hDC
);
557 EngSetLastError(ERROR_INVALID_HANDLE
);
561 if (pDC
->dctype
== DC_TYPE_INFO
)
568 if (bTransformCoordinates
)
570 IntLPtoDP(pDC
, (LPPOINT
)&rcDest
, 2);
572 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
573 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
574 rcDest
.right
= rcDest
.left
+ Width
;
575 rcDest
.bottom
= rcDest
.top
+ Height
;
576 rcDest
.top
+= StartScan
;
581 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
582 SourceSize
.cy
= ScanLines
;
584 //DIBWidth = WIDTH_BYTES_ALIGN32(SourceSize.cx, bmi->bmiHeader.biBitCount);
586 hSourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
589 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
590 bmi
->bmiHeader
.biCompression
),
591 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
592 bmi
->bmiHeader
.biSizeImage
,
598 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
599 Status
= STATUS_NO_MEMORY
;
603 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
606 Status
= STATUS_UNSUCCESSFUL
;
610 /* HACK: If this is a RLE bitmap, only the relevant pixels must be set. */
611 if ((bmi
->bmiHeader
.biCompression
== BI_RLE8
) || (bmi
->bmiHeader
.biCompression
== BI_RLE4
))
613 hMaskBitmap
= IntGdiCreateMaskFromRLE(bmi
->bmiHeader
.biWidth
,
615 bmi
->bmiHeader
.biCompression
,
620 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
621 Status
= STATUS_NO_MEMORY
;
624 pMaskSurf
= EngLockSurface((HSURF
)hMaskBitmap
);
627 Status
= STATUS_UNSUCCESSFUL
;
632 /* Create a palette for the DIB */
633 ppalDIB
= CreateDIBPalette(bmi
, pDC
, ColorUse
);
636 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
637 Status
= STATUS_NO_MEMORY
;
641 /* This is actually a blit */
642 DC_vPrepareDCsForBlit(pDC
, &rcDest
, NULL
, NULL
);
643 pSurf
= pDC
->dclevel
.pSurface
;
646 DC_vFinishBlit(pDC
, NULL
);
653 /* Initialize EXLATEOBJ */
654 EXLATEOBJ_vInitialize(&exlo
,
657 RGB(0xff, 0xff, 0xff),
658 pDC
->pdcattr
->crBackgroundClr
,
659 pDC
->pdcattr
->crForegroundClr
);
661 pDestSurf
= &pSurf
->SurfObj
;
664 DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
665 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
666 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
667 Status
= IntEngBitBlt(pDestSurf
,
674 pMaskSurf
? &ptSource
: NULL
,
677 pMaskSurf
? ROP4_MASK
: ROP4_FROM_INDEX(R3_OPINDEX_SRCCOPY
));
679 /* Cleanup EXLATEOBJ */
680 EXLATEOBJ_vCleanup(&exlo
);
683 DC_vFinishBlit(pDC
, NULL
);
686 if (NT_SUCCESS(Status
))
691 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
692 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
693 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
694 if (pMaskSurf
) EngUnlockSurface(pMaskSurf
);
695 if (hMaskBitmap
) EngDeleteSurface((HSURF
)hMaskBitmap
);
696 if (pDC
) DC_UnlockDc(pDC
);
697 ExFreePoolWithTag(pbmiSafe
, 'pmTG');
703 /* Converts a device-dependent bitmap to a DIB */
706 GreGetDIBitsInternal(
717 BITMAPCOREINFO
* pbmci
= NULL
;
718 PSURFACE psurf
= NULL
;
728 DPRINT("Entered GreGetDIBitsInternal()\n");
730 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
733 colorPtr
= (LPBYTE
)Info
+ Info
->bmiHeader
.biSize
;
736 bitmap_type
= DIB_GetBitmapInfo(&Info
->bmiHeader
,
743 if(bitmap_type
== -1)
745 DPRINT("Wrong bitmap format\n");
746 EngSetLastError(ERROR_INVALID_PARAMETER
);
749 else if(bitmap_type
== 0)
751 /* We need a BITMAPINFO to create a DIB, but we have to fill
752 * the BITMAPCOREINFO we're provided */
753 pbmci
= (BITMAPCOREINFO
*)Info
;
754 Info
= DIB_ConvertBitmapInfo((BITMAPINFO
*)pbmci
, Usage
);
757 DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
760 rgbQuads
= Info
->bmiColors
;
763 pDC
= DC_LockDc(hDC
);
764 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
770 /* Get a pointer to the source bitmap object */
771 psurf
= SURFACE_ShareLockSurface(hBitmap
);
778 /* Fill in the structure */
781 case 0: /* Only info */
782 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
783 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
784 -psurf
->SurfObj
.sizlBitmap
.cy
:
785 psurf
->SurfObj
.sizlBitmap
.cy
;;
786 Info
->bmiHeader
.biPlanes
= 1;
787 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
788 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
789 Info
->bmiHeader
.biHeight
,
790 Info
->bmiHeader
.biBitCount
);
792 if ((Info
->bmiHeader
.biBitCount
== 16) ||
793 (Info
->bmiHeader
.biBitCount
== 32))
795 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
799 Info
->bmiHeader
.biCompression
= BI_RGB
;
801 Info
->bmiHeader
.biXPelsPerMeter
= 0;
802 Info
->bmiHeader
.biYPelsPerMeter
= 0;
803 Info
->bmiHeader
.biClrUsed
= 0;
804 Info
->bmiHeader
.biClrImportant
= 0;
805 ScanLines
= abs(Info
->bmiHeader
.biHeight
);
811 Info
->bmiHeader
.biClrUsed
= 0;
813 /* If the bitmap if a DIB section and has the same format than what
814 * we're asked, go ahead! */
815 if((psurf
->hSecure
) &&
816 (BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bpp
))
818 if(Usage
== DIB_RGB_COLORS
)
820 ULONG colors
= min(psurf
->ppal
->NumColors
, 256);
821 if(colors
!= 256) Info
->bmiHeader
.biClrUsed
= colors
;
822 for(i
= 0; i
< colors
; i
++)
824 rgbQuads
[i
].rgbRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
825 rgbQuads
[i
].rgbGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
826 rgbQuads
[i
].rgbBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
827 rgbQuads
[i
].rgbReserved
= 0;
832 for(i
= 0; i
< 256; i
++)
833 ((WORD
*)rgbQuads
)[i
] = i
;
838 if(Usage
== DIB_PAL_COLORS
)
840 for(i
= 0; i
< 256; i
++)
842 ((WORD
*)rgbQuads
)[i
] = i
;
845 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
))
847 /* For color DDBs in native depth (mono DDBs always have
848 a black/white palette):
849 Generate the color map from the selected palette */
850 PPALETTE pDcPal
= PALETTE_ShareLockPalette(pDC
->dclevel
.hpal
);
856 for (i
= 0; i
< pDcPal
->NumColors
; i
++)
858 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
859 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
860 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
861 rgbQuads
[i
].rgbReserved
= 0;
863 PALETTE_ShareUnlockPalette(pDcPal
);
870 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
= rgbQuads
[0].rgbBlue
= 0;
871 rgbQuads
[0].rgbReserved
= 0;
872 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
= rgbQuads
[1].rgbBlue
= 0xff;
873 rgbQuads
[1].rgbReserved
= 0;
877 RtlCopyMemory(rgbQuads
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
884 memcpy(rgbQuads
, DefLogPaletteQuads
,
885 10 * sizeof(RGBQUAD
));
886 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10,
887 10 * sizeof(RGBQUAD
));
888 color
= rgbQuads
+ 10;
889 for(r
= 0; r
<= 5; r
++) /* FIXME */
891 for(g
= 0; g
<= 5; g
++)
893 for(b
= 0; b
<= 5; b
++)
895 color
->rgbRed
= (r
* 0xff) / 5;
896 color
->rgbGreen
= (g
* 0xff) / 5;
897 color
->rgbBlue
= (b
* 0xff) / 5;
898 color
->rgbReserved
= 0;
910 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
912 ((PDWORD
)Info
->bmiColors
)[0] = 0x7c00;
913 ((PDWORD
)Info
->bmiColors
)[1] = 0x03e0;
914 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
919 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
923 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
924 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
925 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
929 ((PDWORD
)Info
->bmiColors
)[0] = 0xf800;
930 ((PDWORD
)Info
->bmiColors
)[1] = 0x07e0;
931 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
938 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
942 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
943 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
944 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
948 ((PDWORD
)Info
->bmiColors
)[0] = 0xff0000;
949 ((PDWORD
)Info
->bmiColors
)[1] = 0x00ff00;
950 ((PDWORD
)Info
->bmiColors
)[2] = 0x0000ff;
955 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(width
, height
, bpp
);
957 if(Bits
&& ScanLines
)
959 /* Create a DIBSECTION, blt it, profit */
968 if (StartScan
> (ULONG
)psurf
->SurfObj
.sizlBitmap
.cy
)
975 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
979 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
980 Info
->bmiHeader
.biHeight
= (height
< 0) ?
981 -(LONG
)ScanLines
: ScanLines
;
983 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
985 Info
->bmiHeader
.biWidth
= width
;
986 Info
->bmiHeader
.biHeight
= height
;
990 DPRINT1("Unable to create a DIB Section!\n");
991 EngSetLastError(ERROR_INVALID_PARAMETER
);
996 psurfDest
= SURFACE_ShareLockSurface(hBmpDest
);
998 RECTL_vSetRect(&rcDest
, 0, 0, psurf
->SurfObj
.sizlBitmap
.cx
, ScanLines
);
1006 if(ScanLines
<= StartScan
)
1009 SURFACE_ShareUnlockSurface(psurfDest
);
1010 GreDeleteObject(hBmpDest
);
1014 ScanLines
-= StartScan
;
1018 srcPoint
.y
= StartScan
;
1021 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xffffff, 0xffffff, 0);
1023 ret
= IntEngCopyBits(&psurfDest
->SurfObj
,
1030 SURFACE_ShareUnlockSurface(psurfDest
);
1036 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, ScanLines
, bpp
));
1039 GreDeleteObject(hBmpDest
);
1040 EXLATEOBJ_vCleanup(&exlo
);
1042 else ScanLines
= abs(height
);
1046 if(pDC
) DC_UnlockDc(pDC
);
1047 if(psurf
) SURFACE_ShareUnlockSurface(psurf
);
1048 if(pbmci
) DIB_FreeConvertedBitmapInfo(Info
, (BITMAPINFO
*)pbmci
, Usage
);
1053 _Success_(return!=0)
1057 NtGdiGetDIBitsInternal(
1060 _In_ UINT iStartScan
,
1062 _Out_writes_bytes_opt_(cjMaxBits
) LPBYTE pjBits
,
1063 _Inout_ LPBITMAPINFO pbmi
,
1065 _In_ UINT cjMaxBits
,
1066 _In_ UINT cjMaxInfo
)
1068 PBITMAPINFO pbmiSafe
;
1069 HANDLE hSecure
= NULL
;
1073 /* Check for bad iUsage */
1074 if (iUsage
> 2) return 0;
1076 /* Check if the size of the bitmap info is large enough */
1077 if (cjMaxInfo
< sizeof(BITMAPCOREHEADER
))
1082 /* Use maximum size */
1083 cjMaxInfo
= min(cjMaxInfo
, sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
));
1085 // HACK: the underlying code sucks and doesn't care for the size, so we
1086 // give it the maximum ever needed
1087 cjAlloc
= sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
);
1089 /* Allocate a buffer the bitmapinfo */
1090 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjAlloc
, 'imBG');
1100 /* Probe and copy the BITMAPINFO */
1101 ProbeForRead(pbmi
, cjMaxInfo
, 1);
1102 RtlCopyMemory(pbmiSafe
, pbmi
, cjMaxInfo
);
1104 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1106 _SEH2_YIELD(goto cleanup
;)
1110 /* Check if the header size is large enough */
1111 if ((pbmiSafe
->bmiHeader
.biSize
< sizeof(BITMAPCOREHEADER
)) ||
1112 (pbmiSafe
->bmiHeader
.biSize
> cjMaxInfo
))
1117 /* Check if the caller provided bitmap bits */
1120 /* Secure the user mode memory */
1121 hSecure
= EngSecureMem(pjBits
, cjMaxBits
);
1128 /* Now call the internal function */
1129 iResult
= GreGetDIBitsInternal(hdc
,
1139 /* Check for success */
1142 /* Use SEH to copy back to user mode */
1145 /* Copy the data back */
1146 cjMaxInfo
= min(cjMaxInfo
, (UINT
)DIB_BitmapInfoSize(pbmiSafe
, (WORD
)iUsage
));
1147 ProbeForWrite(pbmi
, cjMaxInfo
, 1);
1148 RtlCopyMemory(pbmi
, pbmiSafe
, cjMaxInfo
);
1150 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1159 if (hSecure
) EngUnsecureMem(hSecure
);
1160 ExFreePoolWithTag(pbmiSafe
, 'imBG');
1169 NtGdiStretchDIBitsInternal(
1179 IN OPTIONAL LPBYTE pjInit
,
1180 IN LPBITMAPINFO pbmi
,
1182 IN DWORD dwRop
, // MS ntgdi.h says dwRop4(?)
1187 BOOL bResult
= FALSE
;
1192 PSURFACE psurfTmp
= 0, psurfDst
= 0;
1193 PPALETTE ppalDIB
= 0;
1197 if (!(pdc
= DC_LockDc(hdc
)))
1199 EngSetLastError(ERROR_INVALID_HANDLE
);
1203 /* Check for info / mem DC without surface */
1204 if (!pdc
->dclevel
.pSurface
)
1211 /* Transform dest size */
1214 IntLPtoDP(pdc
, (POINTL
*)&sizel
, 1);
1217 /* Check if we can use NtGdiSetDIBitsToDeviceInternal */
1218 if ((sizel
.cx
== cxSrc
) && (sizel
.cy
== cySrc
) && (dwRop
== SRCCOPY
))
1221 return NtGdiSetDIBitsToDeviceInternal(hdc
,
1239 if (pjInit
&& (cjMaxBits
> 0))
1241 pvBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, 'pmeT');
1249 ProbeForRead(pjInit
, cjMaxBits
, 1);
1250 RtlCopyMemory(pvBits
, pjInit
, cjMaxBits
);
1252 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1254 ExFreePoolWithTag(pvBits
, 'pmeT');
1255 _SEH2_YIELD(return 0);
1264 /* FIXME: Locking twice is cheesy, coord tranlation in UM will fix it */
1265 if (!(pdc
= DC_LockDc(hdc
)))
1267 DPRINT1("Could not lock dc\n");
1268 EngSetLastError(ERROR_INVALID_HANDLE
);
1272 /* Calculate source and destination rect */
1275 rcSrc
.right
= xSrc
+ abs(cxSrc
);
1276 rcSrc
.bottom
= ySrc
+ abs(cySrc
);
1279 rcDst
.right
= rcDst
.left
+ cxDst
;
1280 rcDst
.bottom
= rcDst
.top
+ cyDst
;
1281 IntLPtoDP(pdc
, (POINTL
*)&rcDst
, 2);
1282 RECTL_vOffsetRect(&rcDst
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1284 hbmTmp
= GreCreateBitmapEx(pbmi
->bmiHeader
.biWidth
,
1285 abs(pbmi
->bmiHeader
.biHeight
),
1287 BitmapFormat(pbmi
->bmiHeader
.biBitCount
,
1288 pbmi
->bmiHeader
.biCompression
),
1289 pbmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
1290 pbmi
->bmiHeader
.biSizeImage
,
1300 psurfTmp
= SURFACE_ShareLockSurface(hbmTmp
);
1307 /* Create a palette for the DIB */
1308 ppalDIB
= CreateDIBPalette(pbmi
, pdc
, dwUsage
);
1315 /* Prepare DC for blit */
1316 DC_vPrepareDCsForBlit(pdc
, &rcDst
, NULL
, NULL
);
1318 psurfDst
= pdc
->dclevel
.pSurface
;
1320 /* Initialize XLATEOBJ */
1321 EXLATEOBJ_vInitialize(&exlo
,
1324 RGB(0xff, 0xff, 0xff),
1325 pdc
->pdcattr
->crBackgroundClr
,
1326 pdc
->pdcattr
->crForegroundClr
);
1328 /* Perform the stretch operation */
1329 bResult
= IntEngStretchBlt(&psurfDst
->SurfObj
,
1338 &pdc
->eboFill
.BrushObject
,
1340 WIN32_ROP3_TO_ENG_ROP4(dwRop
));
1343 DC_vFinishBlit(pdc
, NULL
);
1344 EXLATEOBJ_vCleanup(&exlo
);
1346 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
1347 if (psurfTmp
) SURFACE_ShareUnlockSurface(psurfTmp
);
1348 if (hbmTmp
) GreDeleteObject(hbmTmp
);
1349 if (pdc
) DC_UnlockDc(pdc
);
1350 if (pvBits
) ExFreePoolWithTag(pvBits
, 'pmeT');
1373 ULONG BmpFormat
= 0;
1376 BmpFormat
= BitmapFormat(planes
* bpp
, compression
);
1378 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1379 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1381 if (BmpFormat
!= BMF_1BPP
) fColor
= TRUE
;
1382 else if ((coloruse
> DIB_RGB_COLORS
) || ((init
& CBM_INIT
) == 0) || !data
) fColor
= FALSE
;
1385 const RGBQUAD
*rgb
= (RGBQUAD
*)((PBYTE
)data
+ data
->bmiHeader
.biSize
);
1386 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1388 // Check if the first color of the colormap is black
1389 if (col
== RGB(0, 0, 0))
1392 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1394 // If the second color is white, create a monochrome bitmap
1395 fColor
= (col
!= RGB(0xff,0xff,0xff));
1400 // Now create the bitmap
1403 if (init
& CBM_CREATDIB
)
1408 /* Undocumented flag which creates a DDB of the format specified by the bitmap info. */
1409 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
, planes
, bpp
);
1412 DPRINT1("IntCreateCompatibleBitmap() failed!\n");
1416 /* The palette must also match the given data */
1417 Surface
= SURFACE_ShareLockSurface(handle
);
1419 Palette
= CreateDIBPalette(data
, Dc
, coloruse
);
1421 SURFACE_vSetPalette(Surface
, Palette
);
1423 PALETTE_ShareUnlockPalette(Palette
);
1424 SURFACE_ShareUnlockSurface(Surface
);
1428 /* Create a regular compatible bitmap, in the same format as the device */
1429 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
, 0, 0);
1434 handle
= GreCreateBitmap(width
,
1444 if ((NULL
!= handle
) && (CBM_INIT
& init
))
1446 IntSetDIBits(Dc
, handle
, 0, height
, bits
, cjMaxBits
, data
, coloruse
);
1452 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1453 // The DDB that is created will be whatever bit depth your reference DC is
1456 NtGdiCreateDIBitmapInternal(
1461 IN OPTIONAL LPBYTE pjInit
,
1462 IN OPTIONAL LPBITMAPINFO pbmi
,
1464 IN UINT cjMaxInitInfo
,
1469 NTSTATUS Status
= STATUS_SUCCESS
;
1470 PBYTE safeBits
= NULL
;
1471 HBITMAP hbmResult
= NULL
;
1478 if(pjInit
&& (fInit
& CBM_INIT
))
1480 if (cjMaxBits
== 0) return NULL
;
1481 safeBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, TAG_DIB
);
1484 DPRINT1("Failed to allocate %lu bytes\n", cjMaxBits
);
1485 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1492 if(pbmi
) ProbeForRead(pbmi
, cjMaxInitInfo
, 1);
1493 if(pjInit
&& (fInit
& CBM_INIT
))
1495 ProbeForRead(pjInit
, cjMaxBits
, 1);
1496 RtlCopyMemory(safeBits
, pjInit
, cjMaxBits
);
1499 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1501 Status
= _SEH2_GetExceptionCode();
1505 if(!NT_SUCCESS(Status
))
1507 DPRINT1("Got an exception! pjInit = %p\n", pjInit
);
1508 SetLastNtError(Status
);
1512 hbmResult
= GreCreateDIBitmapInternal(hDc
,
1524 if (safeBits
) ExFreePoolWithTag(safeBits
, TAG_DIB
);
1530 GreCreateDIBitmapInternal(
1535 IN OPTIONAL LPBYTE pjInit
,
1536 IN OPTIONAL PBITMAPINFO pbmi
,
1548 if (!hDc
) /* 1bpp monochrome bitmap */
1550 // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1551 hdcDest
= NtGdiCreateCompatibleDC(0);
1554 DPRINT1("NtGdiCreateCompatibleDC failed\n");
1563 Dc
= DC_LockDc(hdcDest
);
1566 DPRINT1("Failed to lock hdcDest %p\n", hdcDest
);
1567 EngSetLastError(ERROR_INVALID_HANDLE
);
1570 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1571 * if bpp != 1 and ignore the real value that was passed */
1574 if (pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1576 BITMAPCOREHEADER
* CoreHeader
= (BITMAPCOREHEADER
*)&pbmi
->bmiHeader
;
1577 bpp
= CoreHeader
->bcBitCount
;
1578 planes
= CoreHeader
->bcPlanes
? CoreHeader
->bcPlanes
: 1;
1579 compression
= BI_RGB
;
1583 bpp
= pbmi
->bmiHeader
.biBitCount
;
1584 planes
= pbmi
->bmiHeader
.biPlanes
? pbmi
->bmiHeader
.biPlanes
: 1;
1585 compression
= pbmi
->bmiHeader
.biCompression
;
1594 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, planes
, bpp
, compression
, fInit
, pjInit
, cjMaxBits
, pbmi
, iUsage
);
1599 NtGdiDeleteObjectApp(hdcDest
);
1606 GreCreateDIBitmapFromPackedDIB(
1607 _In_reads_(cjPackedDIB
)PVOID pvPackedDIB
,
1608 _In_ UINT cjPackedDIB
,
1613 UINT cjInfo
, cjBits
;
1616 /* We only support BITMAPINFOHEADER, make sure the size is ok */
1617 if (cjPackedDIB
< sizeof(BITMAPINFOHEADER
))
1622 /* The packed DIB starts with the BITMAPINFOHEADER */
1625 if (cjPackedDIB
< pbmi
->bmiHeader
.biSize
)
1630 /* Calculate the info size and make sure the packed DIB is large enough */
1631 cjInfo
= DIB_BitmapInfoSize(pbmi
, uUsage
);
1632 if (cjPackedDIB
<= cjInfo
)
1637 /* The bitmap bits start after the header */
1638 pjBits
= (PBYTE
)pvPackedDIB
+ cjInfo
;
1639 cjBits
= cjPackedDIB
- cjInfo
;
1641 hbm
= GreCreateDIBitmapInternal(NULL
,
1642 pbmi
->bmiHeader
.biWidth
,
1643 abs(pbmi
->bmiHeader
.biHeight
),
1644 CBM_INIT
| CBM_CREATDIB
,
1657 NtGdiCreateDIBSection(
1659 IN OPTIONAL HANDLE hSection
,
1665 IN ULONG_PTR dwColorSpace
,
1668 HBITMAP hbitmap
= 0;
1670 BOOL bDesktopDC
= FALSE
;
1671 NTSTATUS Status
= STATUS_SUCCESS
;
1673 if (!bmi
) return hbitmap
; // Make sure.
1677 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1678 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1679 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, (WORD
)Usage
), 1);
1681 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1683 Status
= _SEH2_GetExceptionCode();
1687 if(!NT_SUCCESS(Status
))
1689 SetLastNtError(Status
);
1693 // If the reference hdc is null, take the desktop dc
1696 hDC
= NtGdiCreateCompatibleDC(0);
1700 if ((dc
= DC_LockDc(hDC
)))
1702 hbitmap
= DIB_CreateDIBSection(dc
,
1713 EngSetLastError(ERROR_INVALID_HANDLE
);
1717 NtGdiDeleteObjectApp(hDC
);
1724 DIB_CreateDIBSection(
1726 CONST BITMAPINFO
*bmi
,
1734 SURFACE
*bmp
= NULL
;
1735 void *mapBits
= NULL
;
1736 PPALETTE ppalDIB
= NULL
;
1738 // Fill BITMAP32 structure with DIB data
1739 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1746 DPRINT("format (%ld,%ld), planes %u, bpp %u, size %lu, colors %lu (%s)\n",
1747 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1748 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1750 /* CreateDIBSection should fail for compressed formats */
1751 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1753 DPRINT1("no compressed format allowed\n");
1754 return (HBITMAP
)NULL
;
1757 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1759 bm
.bmWidth
= bi
->biWidth
;
1760 bm
.bmHeight
= effHeight
;
1761 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: WIDTH_BYTES_ALIGN32(bm
.bmWidth
, bi
->biBitCount
);
1763 bm
.bmPlanes
= bi
->biPlanes
;
1764 bm
.bmBitsPixel
= bi
->biBitCount
;
1767 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1768 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1769 totalSize
= (bi
->biSizeImage
&& (bi
->biCompression
!= BI_RGB
) && (bi
->biCompression
!= BI_BITFIELDS
))
1770 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1774 SYSTEM_BASIC_INFORMATION Sbi
;
1777 LARGE_INTEGER SectionOffset
;
1780 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1784 if (!NT_SUCCESS(Status
))
1786 DPRINT1("ZwQuerySystemInformation failed (0x%lx)\n", Status
);
1790 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1791 mapSize
= totalSize
+ (offset
- mapOffset
);
1793 SectionOffset
.LowPart
= mapOffset
;
1794 SectionOffset
.HighPart
= 0;
1796 Status
= ZwMapViewOfSection(section
,
1806 if (!NT_SUCCESS(Status
))
1808 DPRINT1("ZwMapViewOfSection failed (0x%lx)\n", Status
);
1809 EngSetLastError(ERROR_INVALID_PARAMETER
);
1813 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1815 else if (ovr_pitch
&& offset
)
1816 bm
.bmBits
= (LPVOID
) offset
;
1820 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1823 DPRINT1("Failed to allocate memory\n");
1828 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1829 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1832 // Create Device Dependent Bitmap and add DIB pointer
1833 //Size.cx = bm.bmWidth;
1834 //Size.cy = abs(bm.bmHeight);
1835 res
= GreCreateBitmapEx(bm
.bmWidth
,
1838 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1839 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1840 ((bi
->biHeight
< 0) ? BMF_TOPDOWN
: 0),
1846 DPRINT1("GreCreateBitmapEx failed\n");
1847 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
1850 bmp
= SURFACE_ShareLockSurface(res
); // HACK
1853 DPRINT1("SURFACE_LockSurface failed\n");
1854 EngSetLastError(ERROR_INVALID_HANDLE
);
1858 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1859 table between the DIB and the X physical device. Obviously,
1860 this is left out of the ReactOS implementation. Instead,
1861 we call NtGdiSetDIBColorTable. */
1862 bmp
->hDIBSection
= section
;
1863 bmp
->hSecure
= hSecure
;
1864 bmp
->dwOffset
= offset
;
1865 bmp
->flags
= API_BITMAP
;
1866 bmp
->biClrImportant
= bi
->biClrImportant
;
1868 /* Create a palette for the DIB */
1869 ppalDIB
= CreateDIBPalette(bmi
, dc
, usage
);
1871 // Clean up in case of errors
1873 if (!res
|| !bmp
|| !bm
.bmBits
|| !ppalDIB
)
1875 DPRINT("Got an error res=%p, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1878 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1881 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1885 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1890 SURFACE_ShareUnlockSurface(bmp
);
1896 GreDeleteObject(res
);
1902 PALETTE_ShareUnlockPalette(ppalDIB
);
1908 /* If we're here, everything went fine */
1909 SURFACE_vSetPalette(bmp
, ppalDIB
);
1910 PALETTE_ShareUnlockPalette(ppalDIB
);
1911 SURFACE_ShareUnlockSurface(bmp
);
1914 // Return BITMAP handle and storage location
1915 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1923 /***********************************************************************
1926 * Get the info from a bitmap header.
1927 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
1931 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
1932 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
1934 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1936 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
1937 *width
= core
->bcWidth
;
1938 *height
= core
->bcHeight
;
1939 *planes
= core
->bcPlanes
;
1940 *bpp
= core
->bcBitCount
;
1945 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* Assume BITMAPINFOHEADER */
1947 *width
= header
->biWidth
;
1948 *height
= header
->biHeight
;
1949 *planes
= header
->biPlanes
;
1950 *bpp
= header
->biBitCount
;
1951 *compr
= header
->biCompression
;
1952 *size
= header
->biSizeImage
;
1955 DPRINT1("(%u): unknown/wrong size for header\n", header
->biSize
);
1959 /***********************************************************************
1960 * DIB_GetDIBImageBytes
1962 * Return the number of bytes used to hold the image in a DIB bitmap.
1963 * 11/16/1999 (RJJ) lifted from wine
1966 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1968 return WIDTH_BYTES_ALIGN32(width
, depth
) * (height
< 0 ? -height
: height
);
1971 /***********************************************************************
1972 * DIB_BitmapInfoSize
1974 * Return the size of the bitmap info structure including color table.
1975 * 11/16/1999 (RJJ) lifted from wine
1978 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1980 unsigned int colors
, size
, masks
= 0;
1981 unsigned int colorsize
;
1983 colorsize
= (coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) :
1984 (coloruse
== DIB_PAL_INDICES
) ? 0 :
1987 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1989 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
1990 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1991 return sizeof(BITMAPCOREHEADER
) + colors
* colorsize
;
1993 else /* Assume BITMAPINFOHEADER */
1995 colors
= info
->bmiHeader
.biClrUsed
;
1996 if (colors
> 256) colors
= 256;
1997 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
1998 colors
= 1 << info
->bmiHeader
.biBitCount
;
1999 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
2000 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
2001 return size
+ colors
* colorsize
;
2007 DIB_MapPaletteColors(PPALETTE ppalDc
, CONST BITMAPINFO
* lpbmi
)
2014 if (!(ppalDc
->flFlags
& PAL_INDEXED
))
2019 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
2020 if (lpbmi
->bmiHeader
.biClrUsed
)
2022 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
2025 ppalNew
= PALETTE_AllocPalWithHandle(PAL_INDEXED
, nNumColors
, NULL
, 0, 0, 0);
2026 if (ppalNew
== NULL
)
2028 DPRINT1("Could not allocate palette\n");
2032 lpIndex
= (USHORT
*)((PBYTE
)lpbmi
+ lpbmi
->bmiHeader
.biSize
);
2034 for (i
= 0; i
< nNumColors
; i
++)
2036 ULONG iColorIndex
= *lpIndex
% ppalDc
->NumColors
;
2037 ppalNew
->IndexedColors
[i
] = ppalDc
->IndexedColors
[iColorIndex
];
2041 hpal
= ppalNew
->BaseObject
.hHmgr
;
2042 PALETTE_UnlockPalette(ppalNew
);
2047 /* Converts a BITMAPCOREINFO to a BITMAPINFO structure,
2048 * or does nothing if it's already a BITMAPINFO (or V4 or V5) */
2051 DIB_ConvertBitmapInfo (CONST BITMAPINFO
* pbmi
, DWORD Usage
)
2053 CONST BITMAPCOREINFO
* pbmci
= (BITMAPCOREINFO
*)pbmi
;
2054 BITMAPINFO
* pNewBmi
;
2055 UINT numColors
= 0, ColorsSize
= 0;
2057 if(pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) return (BITMAPINFO
*)pbmi
;
2058 if(pbmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) return NULL
;
2060 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
2062 numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
2063 if(Usage
== DIB_PAL_COLORS
)
2065 ColorsSize
= numColors
* sizeof(WORD
);
2069 ColorsSize
= numColors
* sizeof(RGBQUAD
);
2072 else if (Usage
== DIB_PAL_COLORS
)
2074 /* Invalid at high-res */
2078 pNewBmi
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + ColorsSize
, TAG_DIB
);
2079 if(!pNewBmi
) return NULL
;
2081 RtlZeroMemory(pNewBmi
, sizeof(BITMAPINFOHEADER
) + ColorsSize
);
2083 pNewBmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
2084 pNewBmi
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
2085 pNewBmi
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
2086 pNewBmi
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
2087 pNewBmi
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
2088 pNewBmi
->bmiHeader
.biCompression
= BI_RGB
;
2089 pNewBmi
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(pNewBmi
->bmiHeader
.biWidth
,
2090 pNewBmi
->bmiHeader
.biHeight
,
2091 pNewBmi
->bmiHeader
.biBitCount
);
2093 if(Usage
== DIB_PAL_COLORS
)
2095 RtlCopyMemory(pNewBmi
->bmiColors
, pbmci
->bmciColors
, ColorsSize
);
2100 for(i
=0; i
<numColors
; i
++)
2102 pNewBmi
->bmiColors
[i
].rgbRed
= pbmci
->bmciColors
[i
].rgbtRed
;
2103 pNewBmi
->bmiColors
[i
].rgbGreen
= pbmci
->bmciColors
[i
].rgbtGreen
;
2104 pNewBmi
->bmiColors
[i
].rgbBlue
= pbmci
->bmciColors
[i
].rgbtBlue
;
2111 /* Frees a BITMAPINFO created with DIB_ConvertBitmapInfo */
2114 DIB_FreeConvertedBitmapInfo(BITMAPINFO
* converted
, BITMAPINFO
* orig
, DWORD usage
)
2116 BITMAPCOREINFO
* pbmci
;
2117 if(converted
== orig
)
2122 /* Caller don't want any conversion */
2123 ExFreePoolWithTag(converted
, TAG_DIB
);
2127 /* Perform inverse conversion */
2128 pbmci
= (BITMAPCOREINFO
*)orig
;
2130 ASSERT(pbmci
->bmciHeader
.bcSize
== sizeof(BITMAPCOREHEADER
));
2131 pbmci
->bmciHeader
.bcBitCount
= converted
->bmiHeader
.biBitCount
;
2132 pbmci
->bmciHeader
.bcWidth
= converted
->bmiHeader
.biWidth
;
2133 pbmci
->bmciHeader
.bcHeight
= converted
->bmiHeader
.biHeight
;
2134 pbmci
->bmciHeader
.bcPlanes
= converted
->bmiHeader
.biPlanes
;
2136 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
2138 UINT numColors
= converted
->bmiHeader
.biClrUsed
;
2139 if(!numColors
) numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
2140 if(usage
== DIB_PAL_COLORS
)
2142 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(WORD
));
2143 RtlCopyMemory(pbmci
->bmciColors
, converted
->bmiColors
, numColors
* sizeof(WORD
));
2148 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(RGBTRIPLE
));
2149 for(i
=0; i
<numColors
; i
++)
2151 pbmci
->bmciColors
[i
].rgbtRed
= converted
->bmiColors
[i
].rgbRed
;
2152 pbmci
->bmciColors
[i
].rgbtGreen
= converted
->bmiColors
[i
].rgbGreen
;
2153 pbmci
->bmciColors
[i
].rgbtBlue
= converted
->bmiColors
[i
].rgbBlue
;
2157 /* Now free it, it's not needed anymore */
2158 ExFreePoolWithTag(converted
, TAG_DIB
);