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 DefLogPaletteQuads
[20] = /* Copy of Default Logical Palette */
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 { 0xc0, 0xc0, 0xc0, 0x00 },
25 { 0xc0, 0xdc, 0xc0, 0x00 },
26 { 0xf0, 0xca, 0xa6, 0x00 },
27 { 0xf0, 0xfb, 0xff, 0x00 },
28 { 0xa4, 0xa0, 0xa0, 0x00 },
29 { 0x80, 0x80, 0x80, 0x00 },
30 { 0x00, 0x00, 0xff, 0x00 },
31 { 0x00, 0xff, 0x00, 0x00 },
32 { 0x00, 0xff, 0xff, 0x00 },
33 { 0xff, 0x00, 0x00, 0x00 },
34 { 0xff, 0x00, 0xff, 0x00 },
35 { 0xff, 0xff, 0x00, 0x00 },
36 { 0xff, 0xff, 0xff, 0x00 }
42 _In_
const BITMAPINFO
*pbmi
,
47 ULONG i
, cBitsPixel
, cColors
;
49 if (pbmi
->bmiHeader
.biSize
< sizeof(BITMAPINFOHEADER
))
51 PBITMAPCOREINFO pbci
= (PBITMAPCOREINFO
)pbmi
;
52 cBitsPixel
= pbci
->bmciHeader
.bcBitCount
;
56 cBitsPixel
= pbmi
->bmiHeader
.biBitCount
;
59 /* Check if the colors are indexed */
62 /* We create a "full" palette */
63 cColors
= 1 << cBitsPixel
;
65 /* Allocate the palette */
66 ppal
= PALETTE_AllocPalette(PAL_INDEXED
,
73 /* Check if the BITMAPINFO specifies how many colors to use */
74 if ((pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) &&
75 (pbmi
->bmiHeader
.biClrUsed
!= 0))
77 /* This is how many colors we can actually process */
78 cColors
= min(cColors
, pbmi
->bmiHeader
.biClrUsed
);
81 /* Check how to use the colors */
82 if (iUsage
== DIB_PAL_COLORS
)
86 /* The colors are an array of WORD indices into the DC palette */
87 PWORD pwColors
= (PWORD
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
89 /* Use the DCs palette or, if no DC is given, the default one */
90 PPALETTE ppalDC
= pdc
? pdc
->dclevel
.ppal
: gppalDefault
;
92 /* Loop all color indices in the DIB */
93 for (i
= 0; i
< cColors
; i
++)
95 /* Get the palette index and handle wraparound when exceeding
96 the number of colors in the DC palette */
97 WORD wIndex
= pwColors
[i
] % ppalDC
->NumColors
;
99 /* USe the RGB value from the DC palette */
100 crColor
= PALETTE_ulGetRGBColorFromIndex(ppalDC
, wIndex
);
101 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
104 else if (iUsage
== DIB_PAL_BRUSHHACK
)
106 /* The colors are an array of WORD indices into the DC palette */
107 PWORD pwColors
= (PWORD
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
109 /* Loop all color indices in the DIB */
110 for (i
= 0; i
< cColors
; i
++)
112 /* Set the index directly as the RGB color, the real palette
113 containing RGB values will be calculated when the brush is
115 PALETTE_vSetRGBColorForIndex(ppal
, i
, pwColors
[i
]);
118 /* Mark the palette as a brush hack palette */
119 ppal
->flFlags
|= PAL_BRUSHHACK
;
121 // else if (iUsage == 2)
123 // FIXME: this one is undocumented
126 else if (pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
))
128 /* The colors are an array of RGBQUAD values */
129 RGBQUAD
*prgb
= (RGBQUAD
*)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
130 RGBQUAD colors
[256] = {{0}};
132 // FIXME: do we need to handle PALETTEINDEX / PALETTERGB macro?
134 /* Use SEH to verify we can READ prgb[] succesfully */
137 RtlCopyMemory(colors
, prgb
, cColors
* sizeof(colors
[0]));
139 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
145 for (i
= 0; i
< cColors
; ++i
)
147 /* Get the color value and translate it to a COLORREF */
148 COLORREF crColor
= RGB(colors
[i
].rgbRed
, colors
[i
].rgbGreen
, colors
[i
].rgbBlue
);
150 /* Set the RGB value in the palette */
151 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
157 /* The colors are an array of RGBTRIPLE values */
158 RGBTRIPLE
*prgb
= (RGBTRIPLE
*)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
160 /* Loop all color indices in the DIB */
161 for (i
= 0; i
< cColors
; i
++)
163 /* Get the color value and translate it to a COLORREF */
164 RGBTRIPLE rgb
= prgb
[i
];
165 COLORREF crColor
= RGB(rgb
.rgbtRed
, rgb
.rgbtGreen
, rgb
.rgbtBlue
);
167 /* Set the RGB value in the palette */
168 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
174 /* This is a bitfield / RGB palette */
175 ULONG flRedMask
, flGreenMask
, flBlueMask
;
177 /* Check if the DIB contains bitfield values */
178 if ((pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) &&
179 (pbmi
->bmiHeader
.biCompression
== BI_BITFIELDS
))
181 /* Check if we have a v4/v5 header */
182 if (pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPV4HEADER
))
184 /* The masks are dedicated fields in the header */
185 PBITMAPV4HEADER pbmV4Header
= (PBITMAPV4HEADER
)&pbmi
->bmiHeader
;
186 flRedMask
= pbmV4Header
->bV4RedMask
;
187 flGreenMask
= pbmV4Header
->bV4GreenMask
;
188 flBlueMask
= pbmV4Header
->bV4BlueMask
;
192 /* The masks are the first 3 values in the DIB color table */
193 PDWORD pdwColors
= (PVOID
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
194 flRedMask
= pdwColors
[0];
195 flGreenMask
= pdwColors
[1];
196 flBlueMask
= pdwColors
[2];
201 /* Check what bit depth we have. Note: optimization flags are
202 calculated in PALETTE_AllocPalette() */
203 if (cBitsPixel
== 16)
205 /* This is an RGB 555 palette */
207 flGreenMask
= 0x03E0;
212 /* This is an RGB 888 palette */
213 flRedMask
= 0xFF0000;
214 flGreenMask
= 0x00FF00;
215 flBlueMask
= 0x0000FF;
219 /* Allocate the bitfield palette */
220 ppal
= PALETTE_AllocPalette(PAL_BITFIELDS
,
228 /* We're done, return the palette */
232 // Converts a DIB to a device-dependent bitmap
242 CONST BITMAPINFO
*bmi
,
245 HBITMAP SourceBitmap
;
246 PSURFACE psurfDst
, psurfSrc
;
251 PPALETTE ppalDIB
= 0;
254 if (!bmi
|| !Bits
) return 0;
256 /* Check for uncompressed formats */
257 if ((bmi
->bmiHeader
.biCompression
== BI_RGB
) ||
258 (bmi
->bmiHeader
.biCompression
== BI_BITFIELDS
))
260 /* Calculate the image size */
261 cjSizeImage
= DIB_GetDIBImageBytes(bmi
->bmiHeader
.biWidth
,
263 bmi
->bmiHeader
.biBitCount
);
265 /* Check if the header provided an image size */
266 else if (bmi
->bmiHeader
.biSizeImage
!= 0)
268 /* Use the given size */
269 cjSizeImage
= bmi
->bmiHeader
.biSizeImage
;
273 /* Compressed format without a size. This is invalid. */
274 DPRINT1("Compressed format without a size!");
278 /* Check if the size that we have is ok */
279 if ((cjSizeImage
> cjMaxBits
) || (cjSizeImage
== 0))
281 DPRINT1("Invalid bitmap size! cjSizeImage = %lu, cjMaxBits = %lu\n",
282 cjSizeImage
, cjMaxBits
);
286 SourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
289 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
290 bmi
->bmiHeader
.biCompression
),
291 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
297 DPRINT1("Error: Could not create a bitmap.\n");
298 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
302 psurfDst
= SURFACE_ShareLockSurface(hBitmap
);
303 psurfSrc
= SURFACE_ShareLockSurface(SourceBitmap
);
305 if(!(psurfSrc
&& psurfDst
))
307 DPRINT1("Error: Could not lock surfaces\n");
311 /* Create a palette for the DIB */
312 ppalDIB
= CreateDIBPalette(bmi
, DC
, ColorUse
);
315 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
319 /* Initialize EXLATEOBJ */
320 EXLATEOBJ_vInitialize(&exlo
,
323 RGB(0xff, 0xff, 0xff),
324 RGB(0xff, 0xff, 0xff), //DC->pdcattr->crBackgroundClr,
325 0); // DC->pdcattr->crForegroundClr);
327 rcDst
.top
= StartScan
;
329 rcDst
.bottom
= rcDst
.top
+ ScanLines
;
330 rcDst
.right
= psurfDst
->SurfObj
.sizlBitmap
.cx
;
334 result
= IntEngCopyBits(&psurfDst
->SurfObj
,
343 EXLATEOBJ_vCleanup(&exlo
);
346 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
347 if(psurfSrc
) SURFACE_ShareUnlockSurface(psurfSrc
);
348 if(psurfDst
) SURFACE_ShareUnlockSurface(psurfDst
);
349 GreDeleteObject(SourceBitmap
);
356 IntGdiCreateMaskFromRLE(
367 BYTE Data
, NumPixels
, ToSkip
;
369 ASSERT((Compression
== BI_RLE8
) || (Compression
== BI_RLE4
));
371 /* Create the bitmap */
372 Mask
= GreCreateBitmapEx(Width
, Height
, 0, BMF_1BPP
, 0, 0, NULL
, 0);
376 SurfObj
= EngLockSurface((HSURF
)Mask
);
379 GreDeleteObject(Mask
);
382 ASSERT(SurfObj
->pvBits
!= NULL
);
394 if ((x
+ NumPixels
) > Width
)
395 NumPixels
= Width
- x
;
400 DIB_1BPP_HLine(SurfObj
, x
, x
+ NumPixels
, y
, 1);
421 if (i
>= (BitsSize
- 1))
432 /* Done for this run */
436 /* Embedded data into the RLE */
438 if (Compression
== BI_RLE8
)
441 ToSkip
= (NumPixels
/ 2) + (NumPixels
& 1);
443 if ((i
+ ToSkip
) > BitsSize
)
445 ToSkip
= (ToSkip
+ 1) & ~1;
447 if ((x
+ NumPixels
) > Width
)
448 NumPixels
= Width
- x
;
452 DIB_1BPP_HLine(SurfObj
, x
, x
+ NumPixels
, y
, 1);
459 EngUnlockSurface(SurfObj
);
466 NtGdiSetDIBitsToDeviceInternal(
481 IN BOOL bTransformCoordinates
,
482 IN OPTIONAL HANDLE hcmXform
)
486 HBITMAP hSourceBitmap
= NULL
, hMaskBitmap
= NULL
;
487 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
, *pMaskSurf
= NULL
;
494 PPALETTE ppalDIB
= NULL
;
495 LPBITMAPINFO pbmiSafe
;
500 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjMaxInfo
, 'pmTG');
501 if (!pbmiSafe
) return 0;
505 ProbeForRead(bmi
, cjMaxInfo
, 1);
506 ProbeForRead(Bits
, cjMaxBits
, 1);
507 RtlCopyMemory(pbmiSafe
, bmi
, cjMaxInfo
);
510 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
517 ScanLines
= min(ScanLines
, abs(bmi
->bmiHeader
.biHeight
) - StartScan
);
520 DPRINT1("ScanLines == 0\n");
525 pDC
= DC_LockDc(hDC
);
528 EngSetLastError(ERROR_INVALID_HANDLE
);
533 if (pDC
->dctype
== DC_TYPE_INFO
)
541 if (bTransformCoordinates
)
543 IntLPtoDP(pDC
, (LPPOINT
)&rcDest
, 2);
545 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
546 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
547 rcDest
.right
= rcDest
.left
+ Width
;
548 rcDest
.bottom
= rcDest
.top
+ Height
;
549 rcDest
.top
+= StartScan
;
551 if (pDC
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
553 IntUpdateBoundsRect(pDC
, &rcDest
);
559 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
560 SourceSize
.cy
= ScanLines
;
562 //DIBWidth = WIDTH_BYTES_ALIGN32(SourceSize.cx, bmi->bmiHeader.biBitCount);
564 hSourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
567 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
568 bmi
->bmiHeader
.biCompression
),
569 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
570 bmi
->bmiHeader
.biSizeImage
,
576 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
581 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
588 /* HACK: If this is a RLE bitmap, only the relevant pixels must be set. */
589 if ((bmi
->bmiHeader
.biCompression
== BI_RLE8
) || (bmi
->bmiHeader
.biCompression
== BI_RLE4
))
591 hMaskBitmap
= IntGdiCreateMaskFromRLE(bmi
->bmiHeader
.biWidth
,
593 bmi
->bmiHeader
.biCompression
,
598 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
602 pMaskSurf
= EngLockSurface((HSURF
)hMaskBitmap
);
610 /* Create a palette for the DIB */
611 ppalDIB
= CreateDIBPalette(bmi
, pDC
, ColorUse
);
614 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
619 /* This is actually a blit */
620 DC_vPrepareDCsForBlit(pDC
, &rcDest
, NULL
, NULL
);
621 pSurf
= pDC
->dclevel
.pSurface
;
624 DC_vFinishBlit(pDC
, NULL
);
631 /* Initialize EXLATEOBJ */
632 EXLATEOBJ_vInitialize(&exlo
,
635 RGB(0xff, 0xff, 0xff),
636 pDC
->pdcattr
->crBackgroundClr
,
637 pDC
->pdcattr
->crForegroundClr
);
639 pDestSurf
= &pSurf
->SurfObj
;
642 DPRINT("BitsToDev with rcDest=(%d|%d) (%d|%d), ptSource=(%d|%d) w=%d h=%d\n",
643 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
644 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
646 /* This fixes the large Google text on Google.com from being upside down */
647 if (rcDest
.top
> rcDest
.bottom
)
649 RECTL_vMakeWellOrdered(&rcDest
);
650 ptSource
.y
-= SourceSize
.cy
;
653 bResult
= IntEngBitBlt(pDestSurf
,
660 pMaskSurf
? &ptSource
: NULL
,
663 pMaskSurf
? ROP4_MASK
: ROP4_FROM_INDEX(R3_OPINDEX_SRCCOPY
));
665 /* Cleanup EXLATEOBJ */
666 EXLATEOBJ_vCleanup(&exlo
);
669 DC_vFinishBlit(pDC
, NULL
);
671 ret
= bResult
? ScanLines
: 0;
675 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
676 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
677 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
678 if (pMaskSurf
) EngUnlockSurface(pMaskSurf
);
679 if (hMaskBitmap
) EngDeleteSurface((HSURF
)hMaskBitmap
);
680 if (pDC
) DC_UnlockDc(pDC
);
681 ExFreePoolWithTag(pbmiSafe
, 'pmTG');
687 /* Converts a device-dependent bitmap to a DIB */
690 GreGetDIBitsInternal(
701 BITMAPCOREINFO
* pbmci
= NULL
;
702 PSURFACE psurf
= NULL
;
712 DPRINT("Entered GreGetDIBitsInternal()\n");
714 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
717 pDC
= DC_LockDc(hDC
);
718 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
724 /* Get a pointer to the source bitmap object */
725 psurf
= SURFACE_ShareLockSurface(hBitmap
);
732 colorPtr
= (LPBYTE
)Info
+ Info
->bmiHeader
.biSize
;
735 bitmap_type
= DIB_GetBitmapInfo(&Info
->bmiHeader
,
742 if(bitmap_type
== -1)
744 DPRINT1("Wrong bitmap format\n");
745 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 /* fill in the the bit count, so we can calculate the right ColorsSize during the conversion */
755 pbmci
->bmciHeader
.bcBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
756 Info
= DIB_ConvertBitmapInfo((BITMAPINFO
*)pbmci
, Usage
);
759 DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
763 rgbQuads
= Info
->bmiColors
;
767 - negative width is always an invalid value
768 - non-null Bits and zero bpp is an invalid combination
769 - only check the rest of the input params if either bpp is non-zero or Bits are set */
770 if (width
< 0 || (bpp
== 0 && Bits
))
778 if ((height
== 0 || width
== 0) || (compr
&& compr
!= BI_BITFIELDS
&& compr
!= BI_RGB
))
785 Info
->bmiHeader
.biClrUsed
= 0;
786 Info
->bmiHeader
.biClrImportant
= 0;
788 /* Fill in the structure */
791 case 0: /* Only info */
792 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
793 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
794 -psurf
->SurfObj
.sizlBitmap
.cy
:
795 psurf
->SurfObj
.sizlBitmap
.cy
;
796 Info
->bmiHeader
.biPlanes
= 1;
797 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
798 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
799 Info
->bmiHeader
.biHeight
,
800 Info
->bmiHeader
.biBitCount
);
801 Info
->bmiHeader
.biCompression
= (Info
->bmiHeader
.biBitCount
== 16 || Info
->bmiHeader
.biBitCount
== 32) ?
802 BI_BITFIELDS
: BI_RGB
;
803 Info
->bmiHeader
.biXPelsPerMeter
= 0;
804 Info
->bmiHeader
.biYPelsPerMeter
= 0;
806 if (Info
->bmiHeader
.biBitCount
<= 8 && Info
->bmiHeader
.biClrUsed
== 0)
807 Info
->bmiHeader
.biClrUsed
= 1 << Info
->bmiHeader
.biBitCount
;
815 Info
->bmiHeader
.biClrUsed
= 1 << bpp
;
817 /* If the bitmap is a DIB section and has the same format as what
818 * is requested, go ahead! */
819 if((psurf
->hSecure
) &&
820 (BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bpp
))
822 if(Usage
== DIB_RGB_COLORS
)
824 ULONG colors
= min(psurf
->ppal
->NumColors
, 256);
825 if(colors
!= 256) Info
->bmiHeader
.biClrUsed
= colors
;
826 for(i
= 0; i
< colors
; i
++)
828 rgbQuads
[i
].rgbRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
829 rgbQuads
[i
].rgbGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
830 rgbQuads
[i
].rgbBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
831 rgbQuads
[i
].rgbReserved
= 0;
836 for(i
= 0; i
< 256; i
++)
837 ((WORD
*)rgbQuads
)[i
] = i
;
842 if(Usage
== DIB_PAL_COLORS
)
844 for(i
= 0; i
< 256; i
++)
846 ((WORD
*)rgbQuads
)[i
] = i
;
849 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
))
851 /* For color DDBs in native depth (mono DDBs always have
852 a black/white palette):
853 Generate the color map from the selected palette */
854 PPALETTE pDcPal
= PALETTE_ShareLockPalette(pDC
->dclevel
.hpal
);
860 for (i
= 0; i
< pDcPal
->NumColors
; i
++)
862 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
863 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
864 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
865 rgbQuads
[i
].rgbReserved
= 0;
867 PALETTE_ShareUnlockPalette(pDcPal
);
874 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
= rgbQuads
[0].rgbBlue
= 0;
875 rgbQuads
[0].rgbReserved
= 0;
876 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
= rgbQuads
[1].rgbBlue
= 0xff;
877 rgbQuads
[1].rgbReserved
= 0;
881 /* The EGA palette is the first and last 8 colours of the default palette
882 with the innermost pair swapped */
883 RtlCopyMemory(rgbQuads
, DefLogPaletteQuads
, 7 * sizeof(RGBQUAD
));
884 RtlCopyMemory(rgbQuads
+ 7, DefLogPaletteQuads
+ 12, 1 * sizeof(RGBQUAD
));
885 RtlCopyMemory(rgbQuads
+ 8, DefLogPaletteQuads
+ 7, 1 * sizeof(RGBQUAD
));
886 RtlCopyMemory(rgbQuads
+ 9, DefLogPaletteQuads
+ 13, 7 * sizeof(RGBQUAD
));
893 memcpy(rgbQuads
, DefLogPaletteQuads
, 10 * sizeof(RGBQUAD
));
894 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10, 10 * sizeof(RGBQUAD
));
896 for (i
= 10; i
< 246; i
++)
898 rgbQuads
[i
].rgbRed
= (i
& 0x07) << 5;
899 rgbQuads
[i
].rgbGreen
= (i
& 0x38) << 2;
900 rgbQuads
[i
].rgbBlue
= i
& 0xc0;
901 rgbQuads
[i
].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;
960 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(width
, height
, bpp
);
961 Info
->bmiHeader
.biPlanes
= 1;
963 if(Bits
&& ScanLines
)
965 /* Create a DIBSECTION, blt it, profit */
975 if (StartScan
>= abs(Info
->bmiHeader
.biHeight
))
982 ScanLines
= min(ScanLines
, abs(Info
->bmiHeader
.biHeight
) - StartScan
);
985 if (abs(Info
->bmiHeader
.biHeight
) < psurf
->SurfObj
.sizlBitmap
.cy
)
987 StartScan
+= psurf
->SurfObj
.sizlBitmap
.cy
- abs(Info
->bmiHeader
.biHeight
);
990 Info
->bmiHeader
.biHeight
= (height
< 0) ?
991 -(LONG
)ScanLines
: ScanLines
;
993 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
995 Info
->bmiHeader
.biHeight
= height
;
999 DPRINT1("Unable to create a DIB Section!\n");
1000 EngSetLastError(ERROR_INVALID_PARAMETER
);
1005 psurfDest
= SURFACE_ShareLockSurface(hBmpDest
);
1007 RECTL_vSetRect(&rcDest
, 0, 0, Info
->bmiHeader
.biWidth
, ScanLines
);
1008 Info
->bmiHeader
.biWidth
= width
;
1011 if (abs(Info
->bmiHeader
.biHeight
) <= psurf
->SurfObj
.sizlBitmap
.cy
)
1013 srcPoint
.y
= psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
- ScanLines
;
1017 /* Determine the actual number of lines copied from the */
1018 /* original bitmap. It might be different from ScanLines. */
1019 newLines
= abs(Info
->bmiHeader
.biHeight
) - psurf
->SurfObj
.sizlBitmap
.cy
;
1020 newLines
= min((int)(StartScan
+ ScanLines
- newLines
), psurf
->SurfObj
.sizlBitmap
.cy
);
1023 srcPoint
.y
= psurf
->SurfObj
.sizlBitmap
.cy
- newLines
;
1024 if (StartScan
> psurf
->SurfObj
.sizlBitmap
.cy
)
1026 newLines
-= (StartScan
- psurf
->SurfObj
.sizlBitmap
.cy
);
1032 srcPoint
.y
= psurf
->SurfObj
.sizlBitmap
.cy
;
1036 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xffffff, 0xffffff, 0);
1038 ret
= IntEngCopyBits(&psurfDest
->SurfObj
,
1045 SURFACE_ShareUnlockSurface(psurfDest
);
1051 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, ScanLines
, bpp
));
1053 /* Update if line count has changed */
1056 ScanLines
= (UINT
)newLines
;
1058 GreDeleteObject(hBmpDest
);
1059 EXLATEOBJ_vCleanup(&exlo
);
1063 /* Signals success and not the actual number of scan lines*/
1070 DIB_FreeConvertedBitmapInfo(Info
, (BITMAPINFO
*)pbmci
, Usage
);
1073 SURFACE_ShareUnlockSurface(psurf
);
1081 _Success_(return!=0)
1085 NtGdiGetDIBitsInternal(
1088 _In_ UINT iStartScan
,
1090 _Out_writes_bytes_opt_(cjMaxBits
) LPBYTE pjBits
,
1091 _Inout_ LPBITMAPINFO pbmi
,
1093 _In_ UINT cjMaxBits
,
1094 _In_ UINT cjMaxInfo
)
1096 PBITMAPINFO pbmiSafe
;
1097 HANDLE hSecure
= NULL
;
1101 /* Check for bad iUsage */
1102 if (iUsage
> 2) return 0;
1104 /* Check if the size of the bitmap info is large enough */
1105 if (cjMaxInfo
< sizeof(BITMAPCOREHEADER
))
1110 /* Use maximum size */
1111 cjMaxInfo
= min(cjMaxInfo
, sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
));
1113 // HACK: the underlying code sucks and doesn't care for the size, so we
1114 // give it the maximum ever needed
1115 cjAlloc
= sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
);
1117 /* Allocate a buffer the bitmapinfo */
1118 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjAlloc
, 'imBG');
1128 /* Probe and copy the BITMAPINFO */
1129 ProbeForRead(pbmi
, cjMaxInfo
, 1);
1130 RtlCopyMemory(pbmiSafe
, pbmi
, cjMaxInfo
);
1132 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1138 /* Check if the header size is large enough */
1139 if ((pbmiSafe
->bmiHeader
.biSize
< sizeof(BITMAPCOREHEADER
)) ||
1140 (pbmiSafe
->bmiHeader
.biSize
> cjMaxInfo
))
1145 /* Check if the caller provided bitmap bits */
1148 /* Secure the user mode memory */
1149 hSecure
= EngSecureMem(pjBits
, cjMaxBits
);
1156 /* Now call the internal function */
1157 iResult
= GreGetDIBitsInternal(hdc
,
1167 /* Check for success */
1170 /* Use SEH to copy back to user mode */
1173 /* Copy the data back */
1174 cjMaxInfo
= min(cjMaxInfo
, (UINT
)DIB_BitmapInfoSize(pbmiSafe
, (WORD
)iUsage
));
1175 ProbeForWrite(pbmi
, cjMaxInfo
, 1);
1176 RtlCopyMemory(pbmi
, pbmiSafe
, cjMaxInfo
);
1178 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1187 if (hSecure
) EngUnsecureMem(hSecure
);
1188 ExFreePoolWithTag(pbmiSafe
, 'imBG');
1197 NtGdiStretchDIBitsInternal(
1207 IN OPTIONAL LPBYTE pjInit
,
1208 IN LPBITMAPINFO pbmi
,
1210 IN DWORD dwRop
, // MS ntgdi.h says dwRop4(?)
1219 PSURFACE psurfTmp
= 0, psurfDst
= 0;
1220 PPALETTE ppalDIB
= 0;
1224 LPBITMAPINFO pbmiSafe
;
1226 HBITMAP hBitmap
, hOldBitmap
= NULL
;
1228 HPALETTE hPal
= NULL
;
1229 ULONG BmpFormat
= 0;
1230 INT LinesCopied
= 0;
1232 /* Check for bad iUsage */
1233 if (dwUsage
> 2) return 0;
1235 /* We must have LPBITMAPINFO */
1238 DPRINT1("Error, Invalid Parameter.\n");
1239 EngSetLastError(ERROR_INVALID_PARAMETER
);
1243 /* Check if the size of the bitmap info is large enough */
1244 if (cjMaxInfo
< sizeof(BITMAPCOREHEADER
))
1249 /* Use maximum size */
1250 cjMaxInfo
= min(cjMaxInfo
, sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
));
1252 // HACK: the underlying code sucks and doesn't care for the size, so we
1253 // give it the maximum ever needed
1254 cjAlloc
= sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
);
1256 /* Allocate a buffer the bitmapinfo */
1257 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjAlloc
, 'imBG');
1267 /* Probe and copy the BITMAPINFO */
1268 ProbeForRead(pbmi
, cjMaxInfo
, 1);
1269 RtlCopyMemory(pbmiSafe
, pbmi
, cjMaxInfo
);
1271 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1273 ExFreePoolWithTag(pbmiSafe
, 'imBG');
1278 /* Check if the header size is large enough */
1279 if ((pbmiSafe
->bmiHeader
.biSize
< sizeof(BITMAPCOREHEADER
)) ||
1280 (pbmiSafe
->bmiHeader
.biSize
> cjMaxInfo
))
1282 ExFreePoolWithTag(pbmiSafe
, 'imBG');
1286 if (!(pdc
= DC_LockDc(hdc
)))
1288 EngSetLastError(ERROR_INVALID_HANDLE
);
1292 /* Check for info / mem DC without surface */
1293 if (!pdc
->dclevel
.pSurface
)
1300 /* Transform dest size */
1303 IntLPtoDP(pdc
, (POINTL
*)&sizel
, 1);
1306 if (pjInit
&& (cjMaxBits
> 0))
1308 pvBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, TAG_DIB
);
1316 ProbeForRead(pjInit
, cjMaxBits
, 1);
1317 RtlCopyMemory(pvBits
, pjInit
, cjMaxBits
);
1319 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1321 ExFreePoolWithTag(pvBits
, TAG_DIB
);
1331 /* Here we select between the dwRop with SRCCOPY or not. */
1332 if (dwRop
== SRCCOPY
)
1334 hdcMem
= NtGdiCreateCompatibleDC(hdc
);
1337 DPRINT1("NtGdiCreateCompatibleDC failed to create hdc.\n");
1338 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
1342 hBitmap
= NtGdiCreateCompatibleBitmap(hdc
,
1343 abs(pbmiSafe
->bmiHeader
.biWidth
),
1344 abs(pbmiSafe
->bmiHeader
.biHeight
));
1345 if (hBitmap
== NULL
)
1347 DPRINT1("NtGdiCreateCompatibleBitmap failed to create bitmap.\n");
1348 DPRINT1("hdc : 0x%08x \n", hdc
);
1349 DPRINT1("width : 0x%08x \n", pbmiSafe
->bmiHeader
.biWidth
);
1350 DPRINT1("height : 0x%08x \n", pbmiSafe
->bmiHeader
.biHeight
);
1351 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
1355 /* Select the bitmap into hdcMem, and save a handle to the old bitmap */
1356 hOldBitmap
= NtGdiSelectBitmap(hdcMem
, hBitmap
);
1358 if (dwUsage
== DIB_PAL_COLORS
)
1360 hPal
= NtGdiGetDCObject(hdc
, GDI_OBJECT_TYPE_PALETTE
);
1361 hPal
= GdiSelectPalette(hdcMem
, hPal
, FALSE
);
1364 pdc
= DC_LockDc(hdcMem
);
1367 IntSetDIBits(pdc
, hBitmap
, 0, abs(pbmiSafe
->bmiHeader
.biHeight
), pvBits
,
1368 cjMaxBits
, pbmiSafe
, dwUsage
);
1372 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
1373 left (negative biHeight) */
1374 if (cxSrc
== cxDst
&& cySrc
== cyDst
)
1376 NtGdiBitBlt(hdc
, xDst
, yDst
, cxDst
, cyDst
,
1377 hdcMem
, xSrc
, abs(pbmiSafe
->bmiHeader
.biHeight
) - cySrc
- ySrc
,
1382 NtGdiStretchBlt(hdc
, xDst
, yDst
, cxDst
, cyDst
,
1383 hdcMem
, xSrc
, abs(pbmiSafe
->bmiHeader
.biHeight
) - cySrc
- ySrc
,
1384 cxSrc
, cySrc
, dwRop
, 0);
1389 GdiSelectPalette(hdcMem
, hPal
, FALSE
);
1392 NtGdiSelectBitmap(hdcMem
, hOldBitmap
);
1394 NtGdiDeleteObjectApp(hdcMem
);
1395 GreDeleteObject(hBitmap
);
1397 } /* End of dwRop == SRCCOPY */
1399 { /* Start of dwRop != SRCCOPY */
1400 /* FIXME: Locking twice is cheesy, coord tranlation in UM will fix it */
1401 if (!(pdc
= DC_LockDc(hdc
)))
1403 DPRINT1("Could not lock dc\n");
1404 EngSetLastError(ERROR_INVALID_HANDLE
);
1408 /* Calculate source and destination rect */
1411 rcSrc
.right
= xSrc
+ abs(cxSrc
);
1412 rcSrc
.bottom
= ySrc
+ abs(cySrc
);
1415 rcDst
.right
= rcDst
.left
+ cxDst
;
1416 rcDst
.bottom
= rcDst
.top
+ cyDst
;
1417 IntLPtoDP(pdc
, (POINTL
*)&rcDst
, 2);
1418 RECTL_vOffsetRect(&rcDst
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1420 if (pdc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
1422 IntUpdateBoundsRect(pdc
, &rcDst
);
1425 BmpFormat
= BitmapFormat(pbmiSafe
->bmiHeader
.biBitCount
,
1426 pbmiSafe
->bmiHeader
.biCompression
);
1428 hbmTmp
= GreCreateBitmapEx(pbmiSafe
->bmiHeader
.biWidth
,
1429 abs(pbmiSafe
->bmiHeader
.biHeight
),
1432 pbmiSafe
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
1442 psurfTmp
= SURFACE_ShareLockSurface(hbmTmp
);
1448 /* Create a palette for the DIB */
1449 ppalDIB
= CreateDIBPalette(pbmiSafe
, pdc
, dwUsage
);
1455 /* Prepare DC for blit */
1456 DC_vPrepareDCsForBlit(pdc
, &rcDst
, NULL
, NULL
);
1458 psurfDst
= pdc
->dclevel
.pSurface
;
1460 /* Initialize XLATEOBJ */
1461 EXLATEOBJ_vInitialize(&exlo
,
1464 RGB(0xff, 0xff, 0xff),
1465 pdc
->pdcattr
->crBackgroundClr
,
1466 pdc
->pdcattr
->crForegroundClr
);
1468 /* Perform the stretch operation */
1469 IntEngStretchBlt(&psurfDst
->SurfObj
,
1472 (CLIPOBJ
*)&pdc
->co
,
1478 &pdc
->eboFill
.BrushObject
,
1480 WIN32_ROP3_TO_ENG_ROP4(dwRop
));
1483 DC_vFinishBlit(pdc
, NULL
);
1484 EXLATEOBJ_vCleanup(&exlo
);
1487 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
1488 if (psurfTmp
) SURFACE_ShareUnlockSurface(psurfTmp
);
1489 if (hbmTmp
) GreDeleteObject(hbmTmp
);
1490 if (pdc
) DC_UnlockDc(pdc
);
1493 if (pbmiSafe
) ExFreePoolWithTag(pbmiSafe
, 'imBG');
1494 if (pvBits
) ExFreePoolWithTag(pvBits
, TAG_DIB
);
1496 /* This is not what MSDN says is returned from this function, but it
1497 * follows Wine's dlls/gdi32/dib.c function nulldrv_StretchDIBits
1498 * and it fixes over 100 gdi32:dib regression tests. */
1499 if (dwRop
== SRCCOPY
)
1501 LinesCopied
= abs(pbmiSafe
->bmiHeader
.biHeight
);
1505 LinesCopied
= pbmiSafe
->bmiHeader
.biHeight
;
1529 ULONG BmpFormat
= 0;
1532 BmpFormat
= BitmapFormat(planes
* bpp
, compression
);
1534 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1535 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1537 if (BmpFormat
!= BMF_1BPP
) fColor
= TRUE
;
1538 else if ((coloruse
> DIB_RGB_COLORS
) || ((init
& CBM_INIT
) == 0) || !data
) fColor
= FALSE
;
1541 const RGBQUAD
*rgb
= (RGBQUAD
*)((PBYTE
)data
+ data
->bmiHeader
.biSize
);
1542 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1544 // Check if the first color of the colormap is black
1545 if (col
== RGB(0, 0, 0))
1548 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1550 // If the second color is white, create a monochrome bitmap
1551 fColor
= (col
!= RGB(0xff,0xff,0xff));
1556 // Now create the bitmap
1559 if (init
& CBM_CREATDIB
)
1564 /* Undocumented flag which creates a DDB of the format specified by the bitmap info. */
1565 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
, planes
, bpp
);
1568 DPRINT1("IntCreateCompatibleBitmap() failed!\n");
1572 /* The palette must also match the given data */
1573 Surface
= SURFACE_ShareLockSurface(handle
);
1575 Palette
= CreateDIBPalette(data
, Dc
, coloruse
);
1577 SURFACE_vSetPalette(Surface
, Palette
);
1579 PALETTE_ShareUnlockPalette(Palette
);
1580 SURFACE_ShareUnlockSurface(Surface
);
1584 /* Create a regular compatible bitmap, in the same format as the device */
1585 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
, 0, 0);
1590 handle
= GreCreateBitmap(width
,
1600 if ((NULL
!= handle
) && (CBM_INIT
& init
))
1602 IntSetDIBits(Dc
, handle
, 0, height
, bits
, cjMaxBits
, data
, coloruse
);
1608 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1609 // The DDB that is created will be whatever bit depth your reference DC is
1612 NtGdiCreateDIBitmapInternal(
1617 IN OPTIONAL LPBYTE pjInit
,
1618 IN OPTIONAL LPBITMAPINFO pbmi
,
1620 IN UINT cjMaxInitInfo
,
1625 NTSTATUS Status
= STATUS_SUCCESS
;
1626 PBYTE safeBits
= NULL
;
1627 HBITMAP hbmResult
= NULL
;
1634 if(pjInit
&& (fInit
& CBM_INIT
))
1636 if (cjMaxBits
== 0) return NULL
;
1637 safeBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, TAG_DIB
);
1640 DPRINT1("Failed to allocate %lu bytes\n", cjMaxBits
);
1641 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1648 if(pbmi
) ProbeForRead(pbmi
, cjMaxInitInfo
, 1);
1649 if(pjInit
&& (fInit
& CBM_INIT
))
1651 ProbeForRead(pjInit
, cjMaxBits
, 1);
1652 RtlCopyMemory(safeBits
, pjInit
, cjMaxBits
);
1655 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1657 Status
= _SEH2_GetExceptionCode();
1661 if(!NT_SUCCESS(Status
))
1663 DPRINT1("Got an exception! pjInit = %p\n", pjInit
);
1664 SetLastNtError(Status
);
1668 hbmResult
= GreCreateDIBitmapInternal(hDc
,
1680 if (safeBits
) ExFreePoolWithTag(safeBits
, TAG_DIB
);
1686 GreCreateDIBitmapInternal(
1691 IN OPTIONAL LPBYTE pjInit
,
1692 IN OPTIONAL PBITMAPINFO pbmi
,
1704 if (!hDc
) /* 1bpp monochrome bitmap */
1706 // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1707 hdcDest
= NtGdiCreateCompatibleDC(0);
1710 DPRINT1("NtGdiCreateCompatibleDC failed\n");
1719 Dc
= DC_LockDc(hdcDest
);
1722 DPRINT1("Failed to lock hdcDest %p\n", hdcDest
);
1723 EngSetLastError(ERROR_INVALID_HANDLE
);
1726 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1727 * if bpp != 1 and ignore the real value that was passed */
1730 if (pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1732 BITMAPCOREHEADER
* CoreHeader
= (BITMAPCOREHEADER
*)&pbmi
->bmiHeader
;
1733 bpp
= CoreHeader
->bcBitCount
;
1734 planes
= CoreHeader
->bcPlanes
? CoreHeader
->bcPlanes
: 1;
1735 compression
= BI_RGB
;
1739 bpp
= pbmi
->bmiHeader
.biBitCount
;
1740 planes
= pbmi
->bmiHeader
.biPlanes
? pbmi
->bmiHeader
.biPlanes
: 1;
1741 compression
= pbmi
->bmiHeader
.biCompression
;
1750 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, planes
, bpp
, compression
, fInit
, pjInit
, cjMaxBits
, pbmi
, iUsage
);
1755 NtGdiDeleteObjectApp(hdcDest
);
1762 GreCreateDIBitmapFromPackedDIB(
1763 _In_reads_(cjPackedDIB
)PVOID pvPackedDIB
,
1764 _In_ UINT cjPackedDIB
,
1769 UINT cjInfo
, cjBits
;
1772 /* We only support BITMAPINFOHEADER, make sure the size is ok */
1773 if (cjPackedDIB
< sizeof(BITMAPINFOHEADER
))
1778 /* The packed DIB starts with the BITMAPINFOHEADER */
1781 if (cjPackedDIB
< pbmi
->bmiHeader
.biSize
)
1786 /* Calculate the info size and make sure the packed DIB is large enough */
1787 cjInfo
= DIB_BitmapInfoSize(pbmi
, uUsage
);
1788 if (cjPackedDIB
<= cjInfo
)
1793 /* The bitmap bits start after the header */
1794 pjBits
= (PBYTE
)pvPackedDIB
+ cjInfo
;
1795 cjBits
= cjPackedDIB
- cjInfo
;
1797 hbm
= GreCreateDIBitmapInternal(NULL
,
1798 pbmi
->bmiHeader
.biWidth
,
1799 abs(pbmi
->bmiHeader
.biHeight
),
1800 CBM_INIT
| CBM_CREATDIB
,
1813 NtGdiCreateDIBSection(
1815 IN OPTIONAL HANDLE hSection
,
1821 IN ULONG_PTR dwColorSpace
,
1824 HBITMAP hbitmap
= 0;
1826 BOOL bDesktopDC
= FALSE
;
1827 NTSTATUS Status
= STATUS_SUCCESS
;
1829 if (!bmi
) return hbitmap
; // Make sure.
1833 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1834 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1835 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, (WORD
)Usage
), 1);
1837 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1839 Status
= _SEH2_GetExceptionCode();
1843 if(!NT_SUCCESS(Status
))
1845 SetLastNtError(Status
);
1849 // If the reference hdc is null, take the desktop dc
1852 hDC
= NtGdiCreateCompatibleDC(0);
1856 if ((dc
= DC_LockDc(hDC
)))
1858 hbitmap
= DIB_CreateDIBSection(dc
,
1869 EngSetLastError(ERROR_INVALID_HANDLE
);
1873 NtGdiDeleteObjectApp(hDC
);
1880 DIB_CreateDIBSection(
1882 CONST BITMAPINFO
*bmi
,
1890 SURFACE
*bmp
= NULL
;
1891 void *mapBits
= NULL
;
1892 PPALETTE ppalDIB
= NULL
;
1894 // Fill BITMAP32 structure with DIB data
1895 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1902 DPRINT("format (%ld,%ld), planes %u, bpp %u, size %lu, colors %lu (%s)\n",
1903 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1904 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1906 /* CreateDIBSection should fail for compressed formats */
1907 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1909 DPRINT1("no compressed format allowed\n");
1910 return (HBITMAP
)NULL
;
1913 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1915 bm
.bmWidth
= bi
->biWidth
;
1916 bm
.bmHeight
= effHeight
;
1917 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: WIDTH_BYTES_ALIGN32(bm
.bmWidth
, bi
->biBitCount
);
1919 bm
.bmPlanes
= bi
->biPlanes
;
1920 bm
.bmBitsPixel
= bi
->biBitCount
;
1923 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1924 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1925 totalSize
= (bi
->biSizeImage
&& (bi
->biCompression
!= BI_RGB
) && (bi
->biCompression
!= BI_BITFIELDS
))
1926 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1930 SYSTEM_BASIC_INFORMATION Sbi
;
1933 LARGE_INTEGER SectionOffset
;
1936 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1940 if (!NT_SUCCESS(Status
))
1942 DPRINT1("ZwQuerySystemInformation failed (0x%lx)\n", Status
);
1946 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1947 mapSize
= totalSize
+ (offset
- mapOffset
);
1949 SectionOffset
.LowPart
= mapOffset
;
1950 SectionOffset
.HighPart
= 0;
1952 Status
= ZwMapViewOfSection(section
,
1962 if (!NT_SUCCESS(Status
))
1964 DPRINT1("ZwMapViewOfSection failed (0x%lx)\n", Status
);
1965 EngSetLastError(ERROR_INVALID_PARAMETER
);
1969 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1971 else if (ovr_pitch
&& offset
)
1972 bm
.bmBits
= UlongToPtr(offset
);
1976 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1979 DPRINT1("Failed to allocate memory\n");
1984 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1985 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1988 // Create Device Dependent Bitmap and add DIB pointer
1989 //Size.cx = bm.bmWidth;
1990 //Size.cy = abs(bm.bmHeight);
1991 res
= GreCreateBitmapEx(bm
.bmWidth
,
1994 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1995 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1996 ((bi
->biHeight
< 0) ? BMF_TOPDOWN
: 0),
2002 DPRINT1("GreCreateBitmapEx failed\n");
2003 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
2006 bmp
= SURFACE_ShareLockSurface(res
); // HACK
2009 DPRINT1("SURFACE_LockSurface failed\n");
2010 EngSetLastError(ERROR_INVALID_HANDLE
);
2014 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
2015 table between the DIB and the X physical device. Obviously,
2016 this is left out of the ReactOS implementation. Instead,
2017 we call NtGdiSetDIBColorTable. */
2018 bmp
->hDIBSection
= section
;
2019 bmp
->hSecure
= hSecure
;
2020 bmp
->dwOffset
= offset
;
2021 bmp
->flags
= API_BITMAP
;
2022 bmp
->biClrImportant
= bi
->biClrImportant
;
2024 /* Create a palette for the DIB */
2025 ppalDIB
= CreateDIBPalette(bmi
, dc
, usage
);
2027 // Clean up in case of errors
2029 if (!res
|| !bmp
|| !bm
.bmBits
|| !ppalDIB
)
2031 DPRINT("Got an error res=%p, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
2034 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
2037 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
2041 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
2046 SURFACE_ShareUnlockSurface(bmp
);
2052 GreDeleteObject(res
);
2058 PALETTE_ShareUnlockPalette(ppalDIB
);
2064 /* If we're here, everything went fine */
2065 SURFACE_vSetPalette(bmp
, ppalDIB
);
2066 PALETTE_ShareUnlockPalette(ppalDIB
);
2067 SURFACE_ShareUnlockSurface(bmp
);
2070 // Return BITMAP handle and storage location
2071 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
2079 /***********************************************************************
2082 * Get the info from a bitmap header.
2083 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
2087 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
2088 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
2090 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
2092 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
2093 *width
= core
->bcWidth
;
2094 *height
= core
->bcHeight
;
2095 *planes
= core
->bcPlanes
;
2096 *bpp
= core
->bcBitCount
;
2101 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* Assume BITMAPINFOHEADER */
2103 *width
= header
->biWidth
;
2104 *height
= header
->biHeight
;
2105 *planes
= header
->biPlanes
;
2106 *bpp
= header
->biBitCount
;
2107 *compr
= header
->biCompression
;
2108 *size
= header
->biSizeImage
;
2111 DPRINT1("(%u): unknown/wrong size for header\n", header
->biSize
);
2115 /***********************************************************************
2116 * DIB_GetDIBImageBytes
2118 * Return the number of bytes used to hold the image in a DIB bitmap.
2119 * 11/16/1999 (RJJ) lifted from wine
2122 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
2124 return WIDTH_BYTES_ALIGN32(width
, depth
) * (height
< 0 ? -height
: height
);
2127 /***********************************************************************
2128 * DIB_BitmapInfoSize
2130 * Return the size of the bitmap info structure including color table.
2131 * 11/16/1999 (RJJ) lifted from wine
2134 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
2136 unsigned int colors
, size
, masks
= 0;
2137 unsigned int colorsize
;
2139 colorsize
= (coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) :
2140 (coloruse
== DIB_PAL_INDICES
) ? 0 :
2143 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
2145 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
2146 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
2147 return sizeof(BITMAPCOREHEADER
) + colors
* colorsize
;
2149 else /* Assume BITMAPINFOHEADER */
2151 colors
= info
->bmiHeader
.biClrUsed
;
2152 if (colors
> 256) colors
= 256;
2153 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
2154 colors
= 1 << info
->bmiHeader
.biBitCount
;
2155 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
2156 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
2157 return size
+ colors
* colorsize
;
2163 DIB_MapPaletteColors(PPALETTE ppalDc
, CONST BITMAPINFO
* lpbmi
)
2170 if (!(ppalDc
->flFlags
& PAL_INDEXED
))
2175 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
2176 if (lpbmi
->bmiHeader
.biClrUsed
)
2178 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
2181 ppalNew
= PALETTE_AllocPalWithHandle(PAL_INDEXED
, nNumColors
, NULL
, 0, 0, 0);
2182 if (ppalNew
== NULL
)
2184 DPRINT1("Could not allocate palette\n");
2188 lpIndex
= (USHORT
*)((PBYTE
)lpbmi
+ lpbmi
->bmiHeader
.biSize
);
2190 for (i
= 0; i
< nNumColors
; i
++)
2192 ULONG iColorIndex
= *lpIndex
% ppalDc
->NumColors
;
2193 ppalNew
->IndexedColors
[i
] = ppalDc
->IndexedColors
[iColorIndex
];
2197 hpal
= ppalNew
->BaseObject
.hHmgr
;
2198 PALETTE_UnlockPalette(ppalNew
);
2203 /* Converts a BITMAPCOREINFO to a BITMAPINFO structure,
2204 * or does nothing if it's already a BITMAPINFO (or V4 or V5) */
2207 DIB_ConvertBitmapInfo (CONST BITMAPINFO
* pbmi
, DWORD Usage
)
2209 CONST BITMAPCOREINFO
* pbmci
= (BITMAPCOREINFO
*)pbmi
;
2210 BITMAPINFO
* pNewBmi
;
2211 UINT numColors
= 0, ColorsSize
= 0;
2213 if(pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) return (BITMAPINFO
*)pbmi
;
2214 if(pbmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) return NULL
;
2216 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
2218 numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
2219 if(Usage
== DIB_PAL_COLORS
)
2221 ColorsSize
= numColors
* sizeof(WORD
);
2225 ColorsSize
= numColors
* sizeof(RGBQUAD
);
2228 else if (Usage
== DIB_PAL_COLORS
)
2230 /* Invalid at high-res */
2234 pNewBmi
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + ColorsSize
, TAG_DIB
);
2235 if(!pNewBmi
) return NULL
;
2237 RtlZeroMemory(pNewBmi
, sizeof(BITMAPINFOHEADER
) + ColorsSize
);
2239 pNewBmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
2240 pNewBmi
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
2241 pNewBmi
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
2242 pNewBmi
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
2243 pNewBmi
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
2244 pNewBmi
->bmiHeader
.biCompression
= BI_RGB
;
2245 pNewBmi
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(pNewBmi
->bmiHeader
.biWidth
,
2246 pNewBmi
->bmiHeader
.biHeight
,
2247 pNewBmi
->bmiHeader
.biBitCount
);
2248 pNewBmi
->bmiHeader
.biClrUsed
= numColors
;
2250 if(Usage
== DIB_PAL_COLORS
)
2252 RtlCopyMemory(pNewBmi
->bmiColors
, pbmci
->bmciColors
, ColorsSize
);
2257 for(i
=0; i
<numColors
; i
++)
2259 pNewBmi
->bmiColors
[i
].rgbRed
= pbmci
->bmciColors
[i
].rgbtRed
;
2260 pNewBmi
->bmiColors
[i
].rgbGreen
= pbmci
->bmciColors
[i
].rgbtGreen
;
2261 pNewBmi
->bmiColors
[i
].rgbBlue
= pbmci
->bmciColors
[i
].rgbtBlue
;
2268 /* Frees a BITMAPINFO created with DIB_ConvertBitmapInfo */
2271 DIB_FreeConvertedBitmapInfo(BITMAPINFO
* converted
, BITMAPINFO
* orig
, DWORD usage
)
2273 BITMAPCOREINFO
* pbmci
;
2274 if(converted
== orig
)
2279 /* Caller don't want any conversion */
2280 ExFreePoolWithTag(converted
, TAG_DIB
);
2284 /* Perform inverse conversion */
2285 pbmci
= (BITMAPCOREINFO
*)orig
;
2287 ASSERT(pbmci
->bmciHeader
.bcSize
== sizeof(BITMAPCOREHEADER
));
2288 pbmci
->bmciHeader
.bcBitCount
= converted
->bmiHeader
.biBitCount
;
2289 pbmci
->bmciHeader
.bcWidth
= converted
->bmiHeader
.biWidth
;
2290 pbmci
->bmciHeader
.bcHeight
= converted
->bmiHeader
.biHeight
;
2291 pbmci
->bmciHeader
.bcPlanes
= converted
->bmiHeader
.biPlanes
;
2293 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
2295 UINT numColors
= converted
->bmiHeader
.biClrUsed
;
2296 if(!numColors
) numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
2297 if(usage
== DIB_PAL_COLORS
)
2299 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(WORD
));
2300 RtlCopyMemory(pbmci
->bmciColors
, converted
->bmiColors
, numColors
* sizeof(WORD
));
2305 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(RGBTRIPLE
));
2306 for(i
=0; i
<numColors
; i
++)
2308 pbmci
->bmciColors
[i
].rgbtRed
= converted
->bmiColors
[i
].rgbRed
;
2309 pbmci
->bmciColors
[i
].rgbtGreen
= converted
->bmiColors
[i
].rgbGreen
;
2310 pbmci
->bmciColors
[i
].rgbtBlue
= converted
->bmiColors
[i
].rgbBlue
;
2314 /* Now free it, it's not needed anymore */
2315 ExFreePoolWithTag(converted
, TAG_DIB
);