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
);
131 // FIXME: do we need to handle PALETTEINDEX / PALETTERGB macro?
133 /* Loop all color indices in the DIB */
134 for (i
= 0; i
< cColors
; i
++)
136 /* Get the color value and translate it to a COLORREF */
137 RGBQUAD rgb
= prgb
[i
];
138 COLORREF crColor
= RGB(rgb
.rgbRed
, rgb
.rgbGreen
, rgb
.rgbBlue
);
140 /* Set the RGB value in the palette */
141 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
146 /* The colors are an array of RGBTRIPLE values */
147 RGBTRIPLE
*prgb
= (RGBTRIPLE
*)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
149 /* Loop all color indices in the DIB */
150 for (i
= 0; i
< cColors
; i
++)
152 /* Get the color value and translate it to a COLORREF */
153 RGBTRIPLE rgb
= prgb
[i
];
154 COLORREF crColor
= RGB(rgb
.rgbtRed
, rgb
.rgbtGreen
, rgb
.rgbtBlue
);
156 /* Set the RGB value in the palette */
157 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
163 /* This is a bitfield / RGB palette */
164 ULONG flRedMask
, flGreenMask
, flBlueMask
;
166 /* Check if the DIB contains bitfield values */
167 if ((pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) &&
168 (pbmi
->bmiHeader
.biCompression
== BI_BITFIELDS
))
170 /* Check if we have a v4/v5 header */
171 if (pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPV4HEADER
))
173 /* The masks are dedicated fields in the header */
174 PBITMAPV4HEADER pbmV4Header
= (PBITMAPV4HEADER
)&pbmi
->bmiHeader
;
175 flRedMask
= pbmV4Header
->bV4RedMask
;
176 flGreenMask
= pbmV4Header
->bV4GreenMask
;
177 flBlueMask
= pbmV4Header
->bV4BlueMask
;
181 /* The masks are the first 3 values in the DIB color table */
182 PDWORD pdwColors
= (PVOID
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
183 flRedMask
= pdwColors
[0];
184 flGreenMask
= pdwColors
[1];
185 flBlueMask
= pdwColors
[2];
190 /* Check what bit depth we have. Note: optimization flags are
191 calculated in PALETTE_AllocPalette() */
192 if (cBitsPixel
== 16)
194 /* This is an RGB 555 palette */
196 flGreenMask
= 0x03E0;
201 /* This is an RGB 888 palette */
202 flRedMask
= 0xFF0000;
203 flGreenMask
= 0x00FF00;
204 flBlueMask
= 0x0000FF;
208 /* Allocate the bitfield palette */
209 ppal
= PALETTE_AllocPalette(PAL_BITFIELDS
,
217 /* We're done, return the palette */
221 // Converts a DIB to a device-dependent bitmap
231 CONST BITMAPINFO
*bmi
,
234 HBITMAP SourceBitmap
;
235 PSURFACE psurfDst
, psurfSrc
;
240 PPALETTE ppalDIB
= 0;
243 if (!bmi
|| !Bits
) return 0;
245 /* Check for uncompressed formats */
246 if ((bmi
->bmiHeader
.biCompression
== BI_RGB
) ||
247 (bmi
->bmiHeader
.biCompression
== BI_BITFIELDS
))
249 /* Calculate the image size */
250 cjSizeImage
= DIB_GetDIBImageBytes(bmi
->bmiHeader
.biWidth
,
252 bmi
->bmiHeader
.biBitCount
);
254 /* Check if the header provided an image size */
255 else if (bmi
->bmiHeader
.biSizeImage
!= 0)
257 /* Use the given size */
258 cjSizeImage
= bmi
->bmiHeader
.biSizeImage
;
262 /* Compressed format without a size. This is invalid. */
263 DPRINT1("Compressed format without a size!");
267 /* Check if the size that we have is ok */
268 if ((cjSizeImage
> cjMaxBits
) || (cjSizeImage
== 0))
270 DPRINT1("Invalid bitmap size! cjSizeImage = %lu, cjMaxBits = %lu\n",
271 cjSizeImage
, cjMaxBits
);
275 SourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
278 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
279 bmi
->bmiHeader
.biCompression
),
280 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
286 DPRINT1("Error: Could not create a bitmap.\n");
287 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
291 psurfDst
= SURFACE_ShareLockSurface(hBitmap
);
292 psurfSrc
= SURFACE_ShareLockSurface(SourceBitmap
);
294 if(!(psurfSrc
&& psurfDst
))
296 DPRINT1("Error: Could not lock surfaces\n");
300 /* Create a palette for the DIB */
301 ppalDIB
= CreateDIBPalette(bmi
, DC
, ColorUse
);
304 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
308 /* Initialize EXLATEOBJ */
309 EXLATEOBJ_vInitialize(&exlo
,
312 RGB(0xff, 0xff, 0xff),
313 RGB(0xff, 0xff, 0xff), //DC->pdcattr->crBackgroundClr,
314 0); // DC->pdcattr->crForegroundClr);
316 rcDst
.top
= StartScan
;
318 rcDst
.bottom
= rcDst
.top
+ ScanLines
;
319 rcDst
.right
= psurfDst
->SurfObj
.sizlBitmap
.cx
;
323 result
= IntEngCopyBits(&psurfDst
->SurfObj
,
332 EXLATEOBJ_vCleanup(&exlo
);
335 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
336 if(psurfSrc
) SURFACE_ShareUnlockSurface(psurfSrc
);
337 if(psurfDst
) SURFACE_ShareUnlockSurface(psurfDst
);
338 GreDeleteObject(SourceBitmap
);
345 IntGdiCreateMaskFromRLE(
356 BYTE Data
, NumPixels
, ToSkip
;
358 ASSERT((Compression
== BI_RLE8
) || (Compression
== BI_RLE4
));
360 /* Create the bitmap */
361 Mask
= GreCreateBitmapEx(Width
, Height
, 0, BMF_1BPP
, 0, 0, NULL
, 0);
365 SurfObj
= EngLockSurface((HSURF
)Mask
);
368 GreDeleteObject(Mask
);
371 ASSERT(SurfObj
->pvBits
!= NULL
);
383 if ((x
+ NumPixels
) > Width
)
384 NumPixels
= Width
- x
;
389 DIB_1BPP_HLine(SurfObj
, x
, x
+ NumPixels
, y
, 1);
410 if (i
>= (BitsSize
- 1))
421 /* Done for this run */
425 /* Embedded data into the RLE */
427 if (Compression
== BI_RLE8
)
430 ToSkip
= (NumPixels
/ 2) + (NumPixels
& 1);
432 if ((i
+ ToSkip
) > BitsSize
)
434 ToSkip
= (ToSkip
+ 1) & ~1;
436 if ((x
+ NumPixels
) > Width
)
437 NumPixels
= Width
- x
;
441 DIB_1BPP_HLine(SurfObj
, x
, x
+ NumPixels
, y
, 1);
448 EngUnlockSurface(SurfObj
);
455 NtGdiSetDIBitsToDeviceInternal(
470 IN BOOL bTransformCoordinates
,
471 IN OPTIONAL HANDLE hcmXform
)
474 NTSTATUS Status
= STATUS_SUCCESS
;
476 HBITMAP hSourceBitmap
= NULL
, hMaskBitmap
= NULL
;
477 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
, *pMaskSurf
= NULL
;
484 PPALETTE ppalDIB
= NULL
;
485 LPBITMAPINFO pbmiSafe
;
489 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjMaxInfo
, 'pmTG');
490 if (!pbmiSafe
) return 0;
494 ProbeForRead(bmi
, cjMaxInfo
, 1);
495 ProbeForRead(Bits
, cjMaxBits
, 1);
496 RtlCopyMemory(pbmiSafe
, bmi
, cjMaxInfo
);
499 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
501 Status
= _SEH2_GetExceptionCode();
505 if (!NT_SUCCESS(Status
))
510 ScanLines
= min(ScanLines
, abs(bmi
->bmiHeader
.biHeight
) - StartScan
);
513 DPRINT1("ScanLines == 0\n");
517 pDC
= DC_LockDc(hDC
);
520 EngSetLastError(ERROR_INVALID_HANDLE
);
524 if (pDC
->dctype
== DC_TYPE_INFO
)
531 if (bTransformCoordinates
)
533 IntLPtoDP(pDC
, (LPPOINT
)&rcDest
, 2);
535 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
536 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
537 rcDest
.right
= rcDest
.left
+ Width
;
538 rcDest
.bottom
= rcDest
.top
+ Height
;
539 rcDest
.top
+= StartScan
;
541 if (pDC
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
543 IntUpdateBoundsRect(pDC
, &rcDest
);
549 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
550 SourceSize
.cy
= ScanLines
;
552 //DIBWidth = WIDTH_BYTES_ALIGN32(SourceSize.cx, bmi->bmiHeader.biBitCount);
554 hSourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
557 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
558 bmi
->bmiHeader
.biCompression
),
559 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
560 bmi
->bmiHeader
.biSizeImage
,
566 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
567 Status
= STATUS_NO_MEMORY
;
571 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
574 Status
= STATUS_UNSUCCESSFUL
;
578 /* HACK: If this is a RLE bitmap, only the relevant pixels must be set. */
579 if ((bmi
->bmiHeader
.biCompression
== BI_RLE8
) || (bmi
->bmiHeader
.biCompression
== BI_RLE4
))
581 hMaskBitmap
= IntGdiCreateMaskFromRLE(bmi
->bmiHeader
.biWidth
,
583 bmi
->bmiHeader
.biCompression
,
588 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
589 Status
= STATUS_NO_MEMORY
;
592 pMaskSurf
= EngLockSurface((HSURF
)hMaskBitmap
);
595 Status
= STATUS_UNSUCCESSFUL
;
600 /* Create a palette for the DIB */
601 ppalDIB
= CreateDIBPalette(bmi
, pDC
, ColorUse
);
604 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
605 Status
= STATUS_NO_MEMORY
;
609 /* This is actually a blit */
610 DC_vPrepareDCsForBlit(pDC
, &rcDest
, NULL
, NULL
);
611 pSurf
= pDC
->dclevel
.pSurface
;
614 DC_vFinishBlit(pDC
, NULL
);
621 /* Initialize EXLATEOBJ */
622 EXLATEOBJ_vInitialize(&exlo
,
625 RGB(0xff, 0xff, 0xff),
626 pDC
->pdcattr
->crBackgroundClr
,
627 pDC
->pdcattr
->crForegroundClr
);
629 pDestSurf
= &pSurf
->SurfObj
;
632 DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
633 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
634 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
635 Status
= IntEngBitBlt(pDestSurf
,
642 pMaskSurf
? &ptSource
: NULL
,
645 pMaskSurf
? ROP4_MASK
: ROP4_FROM_INDEX(R3_OPINDEX_SRCCOPY
));
647 /* Cleanup EXLATEOBJ */
648 EXLATEOBJ_vCleanup(&exlo
);
651 DC_vFinishBlit(pDC
, NULL
);
654 if (NT_SUCCESS(Status
))
659 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
660 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
661 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
662 if (pMaskSurf
) EngUnlockSurface(pMaskSurf
);
663 if (hMaskBitmap
) EngDeleteSurface((HSURF
)hMaskBitmap
);
664 if (pDC
) DC_UnlockDc(pDC
);
665 ExFreePoolWithTag(pbmiSafe
, 'pmTG');
671 /* Converts a device-dependent bitmap to a DIB */
674 GreGetDIBitsInternal(
685 BITMAPCOREINFO
* pbmci
= NULL
;
686 PSURFACE psurf
= NULL
;
696 DPRINT("Entered GreGetDIBitsInternal()\n");
698 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
701 pDC
= DC_LockDc(hDC
);
702 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
708 /* Get a pointer to the source bitmap object */
709 psurf
= SURFACE_ShareLockSurface(hBitmap
);
716 colorPtr
= (LPBYTE
)Info
+ Info
->bmiHeader
.biSize
;
719 bitmap_type
= DIB_GetBitmapInfo(&Info
->bmiHeader
,
726 if(bitmap_type
== -1)
728 DPRINT("Wrong bitmap format\n");
729 EngSetLastError(ERROR_INVALID_PARAMETER
);
733 else if(bitmap_type
== 0)
735 /* We need a BITMAPINFO to create a DIB, but we have to fill
736 * the BITMAPCOREINFO we're provided */
737 pbmci
= (BITMAPCOREINFO
*)Info
;
738 /* fill in the the bit count, so we can calculate the right ColorsSize during the conversion */
739 pbmci
->bmciHeader
.bcBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
740 Info
= DIB_ConvertBitmapInfo((BITMAPINFO
*)pbmci
, Usage
);
743 DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
747 rgbQuads
= Info
->bmiColors
;
751 - negative width is always an invalid value
752 - non-null Bits and zero bpp is an invalid combination
753 - only check the rest of the input params if either bpp is non-zero or Bits are set */
754 if (width
< 0 || (bpp
== 0 && Bits
))
762 if ((height
== 0 || planes
< 0 || width
== 0) || (compr
&& compr
!= BI_BITFIELDS
&& compr
!= BI_RGB
))
769 Info
->bmiHeader
.biClrUsed
= 0;
770 Info
->bmiHeader
.biClrImportant
= 0;
772 /* Fill in the structure */
775 case 0: /* Only info */
776 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
777 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
778 -psurf
->SurfObj
.sizlBitmap
.cy
:
779 psurf
->SurfObj
.sizlBitmap
.cy
;
780 Info
->bmiHeader
.biPlanes
= 1;
781 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
782 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
783 Info
->bmiHeader
.biHeight
,
784 Info
->bmiHeader
.biBitCount
);
785 Info
->bmiHeader
.biCompression
= (Info
->bmiHeader
.biBitCount
== 16 || Info
->bmiHeader
.biBitCount
== 32) ?
786 BI_BITFIELDS
: BI_RGB
;
787 Info
->bmiHeader
.biXPelsPerMeter
= 0;
788 Info
->bmiHeader
.biYPelsPerMeter
= 0;
790 if (Info
->bmiHeader
.biBitCount
<= 8 && Info
->bmiHeader
.biClrUsed
== 0)
791 Info
->bmiHeader
.biClrUsed
= 1 << Info
->bmiHeader
.biBitCount
;
799 Info
->bmiHeader
.biClrUsed
= 1 << bpp
;
801 /* If the bitmap is a DIB section and has the same format as what
802 * is requested, 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);
809 if(colors
!= 256) Info
->bmiHeader
.biClrUsed
= colors
;
810 for(i
= 0; i
< colors
; i
++)
812 rgbQuads
[i
].rgbRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
813 rgbQuads
[i
].rgbGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
814 rgbQuads
[i
].rgbBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
815 rgbQuads
[i
].rgbReserved
= 0;
820 for(i
= 0; i
< 256; i
++)
821 ((WORD
*)rgbQuads
)[i
] = i
;
826 if(Usage
== DIB_PAL_COLORS
)
828 for(i
= 0; i
< 256; i
++)
830 ((WORD
*)rgbQuads
)[i
] = i
;
833 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
))
835 /* For color DDBs in native depth (mono DDBs always have
836 a black/white palette):
837 Generate the color map from the selected palette */
838 PPALETTE pDcPal
= PALETTE_ShareLockPalette(pDC
->dclevel
.hpal
);
844 for (i
= 0; i
< pDcPal
->NumColors
; i
++)
846 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
847 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
848 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
849 rgbQuads
[i
].rgbReserved
= 0;
851 PALETTE_ShareUnlockPalette(pDcPal
);
858 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
= rgbQuads
[0].rgbBlue
= 0;
859 rgbQuads
[0].rgbReserved
= 0;
860 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
= rgbQuads
[1].rgbBlue
= 0xff;
861 rgbQuads
[1].rgbReserved
= 0;
865 /* The EGA palette is the first and last 8 colours of the default palette
866 with the innermost pair swapped */
867 RtlCopyMemory(rgbQuads
, DefLogPaletteQuads
, 7 * sizeof(RGBQUAD
));
868 RtlCopyMemory(rgbQuads
+ 7, DefLogPaletteQuads
+ 12, 1 * sizeof(RGBQUAD
));
869 RtlCopyMemory(rgbQuads
+ 8, DefLogPaletteQuads
+ 7, 1 * sizeof(RGBQUAD
));
870 RtlCopyMemory(rgbQuads
+ 9, DefLogPaletteQuads
+ 13, 7 * sizeof(RGBQUAD
));
877 memcpy(rgbQuads
, DefLogPaletteQuads
, 10 * sizeof(RGBQUAD
));
878 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10, 10 * sizeof(RGBQUAD
));
880 for (i
= 10; i
< 246; i
++)
882 rgbQuads
[i
].rgbRed
= (i
& 0x07) << 5;
883 rgbQuads
[i
].rgbGreen
= (i
& 0x38) << 2;
884 rgbQuads
[i
].rgbBlue
= i
& 0xc0;
885 rgbQuads
[i
].rgbReserved
= 0;
894 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
896 ((PDWORD
)Info
->bmiColors
)[0] = 0x7c00;
897 ((PDWORD
)Info
->bmiColors
)[1] = 0x03e0;
898 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
903 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
907 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
908 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
909 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
913 ((PDWORD
)Info
->bmiColors
)[0] = 0xf800;
914 ((PDWORD
)Info
->bmiColors
)[1] = 0x07e0;
915 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
922 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
926 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
927 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
928 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
932 ((PDWORD
)Info
->bmiColors
)[0] = 0xff0000;
933 ((PDWORD
)Info
->bmiColors
)[1] = 0x00ff00;
934 ((PDWORD
)Info
->bmiColors
)[2] = 0x0000ff;
944 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(width
, height
, bpp
);
945 Info
->bmiHeader
.biPlanes
= 1;
947 if(Bits
&& ScanLines
)
949 /* Create a DIBSECTION, blt it, profit */
958 if (StartScan
> (ULONG
)psurf
->SurfObj
.sizlBitmap
.cy
)
965 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
969 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
970 Info
->bmiHeader
.biHeight
= (height
< 0) ?
971 -(LONG
)ScanLines
: ScanLines
;
973 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
975 Info
->bmiHeader
.biWidth
= width
;
976 Info
->bmiHeader
.biHeight
= height
;
980 DPRINT1("Unable to create a DIB Section!\n");
981 EngSetLastError(ERROR_INVALID_PARAMETER
);
986 psurfDest
= SURFACE_ShareLockSurface(hBmpDest
);
988 RECTL_vSetRect(&rcDest
, 0, 0, psurf
->SurfObj
.sizlBitmap
.cx
, ScanLines
);
996 if(ScanLines
<= StartScan
)
999 SURFACE_ShareUnlockSurface(psurfDest
);
1000 GreDeleteObject(hBmpDest
);
1004 ScanLines
-= StartScan
;
1008 srcPoint
.y
= StartScan
;
1011 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xffffff, 0xffffff, 0);
1013 ret
= IntEngCopyBits(&psurfDest
->SurfObj
,
1020 SURFACE_ShareUnlockSurface(psurfDest
);
1026 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, ScanLines
, bpp
));
1029 GreDeleteObject(hBmpDest
);
1030 EXLATEOBJ_vCleanup(&exlo
);
1034 /* Signals success and not the actual number of scan lines*/
1041 DIB_FreeConvertedBitmapInfo(Info
, (BITMAPINFO
*)pbmci
, Usage
);
1044 SURFACE_ShareUnlockSurface(psurf
);
1052 _Success_(return!=0)
1056 NtGdiGetDIBitsInternal(
1059 _In_ UINT iStartScan
,
1061 _Out_writes_bytes_opt_(cjMaxBits
) LPBYTE pjBits
,
1062 _Inout_ LPBITMAPINFO pbmi
,
1064 _In_ UINT cjMaxBits
,
1065 _In_ UINT cjMaxInfo
)
1067 PBITMAPINFO pbmiSafe
;
1068 HANDLE hSecure
= NULL
;
1072 /* Check for bad iUsage */
1073 if (iUsage
> 2) return 0;
1075 /* Check if the size of the bitmap info is large enough */
1076 if (cjMaxInfo
< sizeof(BITMAPCOREHEADER
))
1081 /* Use maximum size */
1082 cjMaxInfo
= min(cjMaxInfo
, sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
));
1084 // HACK: the underlying code sucks and doesn't care for the size, so we
1085 // give it the maximum ever needed
1086 cjAlloc
= sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
);
1088 /* Allocate a buffer the bitmapinfo */
1089 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjAlloc
, 'imBG');
1099 /* Probe and copy the BITMAPINFO */
1100 ProbeForRead(pbmi
, cjMaxInfo
, 1);
1101 RtlCopyMemory(pbmiSafe
, pbmi
, cjMaxInfo
);
1103 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1105 _SEH2_YIELD(goto cleanup
;)
1109 /* Check if the header size is large enough */
1110 if ((pbmiSafe
->bmiHeader
.biSize
< sizeof(BITMAPCOREHEADER
)) ||
1111 (pbmiSafe
->bmiHeader
.biSize
> cjMaxInfo
))
1116 /* Check if the caller provided bitmap bits */
1119 /* Secure the user mode memory */
1120 hSecure
= EngSecureMem(pjBits
, cjMaxBits
);
1127 /* Now call the internal function */
1128 iResult
= GreGetDIBitsInternal(hdc
,
1138 /* Check for success */
1141 /* Use SEH to copy back to user mode */
1144 /* Copy the data back */
1145 cjMaxInfo
= min(cjMaxInfo
, (UINT
)DIB_BitmapInfoSize(pbmiSafe
, (WORD
)iUsage
));
1146 ProbeForWrite(pbmi
, cjMaxInfo
, 1);
1147 RtlCopyMemory(pbmi
, pbmiSafe
, cjMaxInfo
);
1149 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1158 if (hSecure
) EngUnsecureMem(hSecure
);
1159 ExFreePoolWithTag(pbmiSafe
, 'imBG');
1168 NtGdiStretchDIBitsInternal(
1178 IN OPTIONAL LPBYTE pjInit
,
1179 IN LPBITMAPINFO pbmi
,
1181 IN DWORD dwRop
, // MS ntgdi.h says dwRop4(?)
1186 BOOL bResult
= FALSE
;
1191 PSURFACE psurfTmp
= 0, psurfDst
= 0;
1192 PPALETTE ppalDIB
= 0;
1196 if (!(pdc
= DC_LockDc(hdc
)))
1198 EngSetLastError(ERROR_INVALID_HANDLE
);
1202 /* Check for info / mem DC without surface */
1203 if (!pdc
->dclevel
.pSurface
)
1210 /* Transform dest size */
1213 IntLPtoDP(pdc
, (POINTL
*)&sizel
, 1);
1216 /* Check if we can use NtGdiSetDIBitsToDeviceInternal */
1217 if ((sizel
.cx
== cxSrc
) && (sizel
.cy
== cySrc
) && (dwRop
== SRCCOPY
))
1220 return NtGdiSetDIBitsToDeviceInternal(hdc
,
1238 if (pjInit
&& (cjMaxBits
> 0))
1240 pvBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, 'pmeT');
1248 ProbeForRead(pjInit
, cjMaxBits
, 1);
1249 RtlCopyMemory(pvBits
, pjInit
, cjMaxBits
);
1251 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1253 ExFreePoolWithTag(pvBits
, 'pmeT');
1254 _SEH2_YIELD(return 0);
1263 /* FIXME: Locking twice is cheesy, coord tranlation in UM will fix it */
1264 if (!(pdc
= DC_LockDc(hdc
)))
1266 DPRINT1("Could not lock dc\n");
1267 EngSetLastError(ERROR_INVALID_HANDLE
);
1271 /* Calculate source and destination rect */
1274 rcSrc
.right
= xSrc
+ abs(cxSrc
);
1275 rcSrc
.bottom
= ySrc
+ abs(cySrc
);
1278 rcDst
.right
= rcDst
.left
+ cxDst
;
1279 rcDst
.bottom
= rcDst
.top
+ cyDst
;
1280 IntLPtoDP(pdc
, (POINTL
*)&rcDst
, 2);
1281 RECTL_vOffsetRect(&rcDst
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1283 if (pdc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
1285 IntUpdateBoundsRect(pdc
, &rcDst
);
1288 hbmTmp
= GreCreateBitmapEx(pbmi
->bmiHeader
.biWidth
,
1289 abs(pbmi
->bmiHeader
.biHeight
),
1291 BitmapFormat(pbmi
->bmiHeader
.biBitCount
,
1292 pbmi
->bmiHeader
.biCompression
),
1293 pbmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
1294 pbmi
->bmiHeader
.biSizeImage
,
1304 psurfTmp
= SURFACE_ShareLockSurface(hbmTmp
);
1311 /* Create a palette for the DIB */
1312 ppalDIB
= CreateDIBPalette(pbmi
, pdc
, dwUsage
);
1319 /* Prepare DC for blit */
1320 DC_vPrepareDCsForBlit(pdc
, &rcDst
, NULL
, NULL
);
1322 psurfDst
= pdc
->dclevel
.pSurface
;
1324 /* Initialize XLATEOBJ */
1325 EXLATEOBJ_vInitialize(&exlo
,
1328 RGB(0xff, 0xff, 0xff),
1329 pdc
->pdcattr
->crBackgroundClr
,
1330 pdc
->pdcattr
->crForegroundClr
);
1332 /* Perform the stretch operation */
1333 bResult
= IntEngStretchBlt(&psurfDst
->SurfObj
,
1342 &pdc
->eboFill
.BrushObject
,
1344 WIN32_ROP3_TO_ENG_ROP4(dwRop
));
1347 DC_vFinishBlit(pdc
, NULL
);
1348 EXLATEOBJ_vCleanup(&exlo
);
1350 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
1351 if (psurfTmp
) SURFACE_ShareUnlockSurface(psurfTmp
);
1352 if (hbmTmp
) GreDeleteObject(hbmTmp
);
1353 if (pdc
) DC_UnlockDc(pdc
);
1354 if (pvBits
) ExFreePoolWithTag(pvBits
, 'pmeT');
1377 ULONG BmpFormat
= 0;
1380 BmpFormat
= BitmapFormat(planes
* bpp
, compression
);
1382 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1383 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1385 if (BmpFormat
!= BMF_1BPP
) fColor
= TRUE
;
1386 else if ((coloruse
> DIB_RGB_COLORS
) || ((init
& CBM_INIT
) == 0) || !data
) fColor
= FALSE
;
1389 const RGBQUAD
*rgb
= (RGBQUAD
*)((PBYTE
)data
+ data
->bmiHeader
.biSize
);
1390 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1392 // Check if the first color of the colormap is black
1393 if (col
== RGB(0, 0, 0))
1396 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1398 // If the second color is white, create a monochrome bitmap
1399 fColor
= (col
!= RGB(0xff,0xff,0xff));
1404 // Now create the bitmap
1407 if (init
& CBM_CREATDIB
)
1412 /* Undocumented flag which creates a DDB of the format specified by the bitmap info. */
1413 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
, planes
, bpp
);
1416 DPRINT1("IntCreateCompatibleBitmap() failed!\n");
1420 /* The palette must also match the given data */
1421 Surface
= SURFACE_ShareLockSurface(handle
);
1423 Palette
= CreateDIBPalette(data
, Dc
, coloruse
);
1425 SURFACE_vSetPalette(Surface
, Palette
);
1427 PALETTE_ShareUnlockPalette(Palette
);
1428 SURFACE_ShareUnlockSurface(Surface
);
1432 /* Create a regular compatible bitmap, in the same format as the device */
1433 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
, 0, 0);
1438 handle
= GreCreateBitmap(width
,
1448 if ((NULL
!= handle
) && (CBM_INIT
& init
))
1450 IntSetDIBits(Dc
, handle
, 0, height
, bits
, cjMaxBits
, data
, coloruse
);
1456 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1457 // The DDB that is created will be whatever bit depth your reference DC is
1460 NtGdiCreateDIBitmapInternal(
1465 IN OPTIONAL LPBYTE pjInit
,
1466 IN OPTIONAL LPBITMAPINFO pbmi
,
1468 IN UINT cjMaxInitInfo
,
1473 NTSTATUS Status
= STATUS_SUCCESS
;
1474 PBYTE safeBits
= NULL
;
1475 HBITMAP hbmResult
= NULL
;
1482 if(pjInit
&& (fInit
& CBM_INIT
))
1484 if (cjMaxBits
== 0) return NULL
;
1485 safeBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, TAG_DIB
);
1488 DPRINT1("Failed to allocate %lu bytes\n", cjMaxBits
);
1489 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1496 if(pbmi
) ProbeForRead(pbmi
, cjMaxInitInfo
, 1);
1497 if(pjInit
&& (fInit
& CBM_INIT
))
1499 ProbeForRead(pjInit
, cjMaxBits
, 1);
1500 RtlCopyMemory(safeBits
, pjInit
, cjMaxBits
);
1503 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1505 Status
= _SEH2_GetExceptionCode();
1509 if(!NT_SUCCESS(Status
))
1511 DPRINT1("Got an exception! pjInit = %p\n", pjInit
);
1512 SetLastNtError(Status
);
1516 hbmResult
= GreCreateDIBitmapInternal(hDc
,
1528 if (safeBits
) ExFreePoolWithTag(safeBits
, TAG_DIB
);
1534 GreCreateDIBitmapInternal(
1539 IN OPTIONAL LPBYTE pjInit
,
1540 IN OPTIONAL PBITMAPINFO pbmi
,
1552 if (!hDc
) /* 1bpp monochrome bitmap */
1554 // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1555 hdcDest
= NtGdiCreateCompatibleDC(0);
1558 DPRINT1("NtGdiCreateCompatibleDC failed\n");
1567 Dc
= DC_LockDc(hdcDest
);
1570 DPRINT1("Failed to lock hdcDest %p\n", hdcDest
);
1571 EngSetLastError(ERROR_INVALID_HANDLE
);
1574 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1575 * if bpp != 1 and ignore the real value that was passed */
1578 if (pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1580 BITMAPCOREHEADER
* CoreHeader
= (BITMAPCOREHEADER
*)&pbmi
->bmiHeader
;
1581 bpp
= CoreHeader
->bcBitCount
;
1582 planes
= CoreHeader
->bcPlanes
? CoreHeader
->bcPlanes
: 1;
1583 compression
= BI_RGB
;
1587 bpp
= pbmi
->bmiHeader
.biBitCount
;
1588 planes
= pbmi
->bmiHeader
.biPlanes
? pbmi
->bmiHeader
.biPlanes
: 1;
1589 compression
= pbmi
->bmiHeader
.biCompression
;
1598 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, planes
, bpp
, compression
, fInit
, pjInit
, cjMaxBits
, pbmi
, iUsage
);
1603 NtGdiDeleteObjectApp(hdcDest
);
1610 GreCreateDIBitmapFromPackedDIB(
1611 _In_reads_(cjPackedDIB
)PVOID pvPackedDIB
,
1612 _In_ UINT cjPackedDIB
,
1617 UINT cjInfo
, cjBits
;
1620 /* We only support BITMAPINFOHEADER, make sure the size is ok */
1621 if (cjPackedDIB
< sizeof(BITMAPINFOHEADER
))
1626 /* The packed DIB starts with the BITMAPINFOHEADER */
1629 if (cjPackedDIB
< pbmi
->bmiHeader
.biSize
)
1634 /* Calculate the info size and make sure the packed DIB is large enough */
1635 cjInfo
= DIB_BitmapInfoSize(pbmi
, uUsage
);
1636 if (cjPackedDIB
<= cjInfo
)
1641 /* The bitmap bits start after the header */
1642 pjBits
= (PBYTE
)pvPackedDIB
+ cjInfo
;
1643 cjBits
= cjPackedDIB
- cjInfo
;
1645 hbm
= GreCreateDIBitmapInternal(NULL
,
1646 pbmi
->bmiHeader
.biWidth
,
1647 abs(pbmi
->bmiHeader
.biHeight
),
1648 CBM_INIT
| CBM_CREATDIB
,
1661 NtGdiCreateDIBSection(
1663 IN OPTIONAL HANDLE hSection
,
1669 IN ULONG_PTR dwColorSpace
,
1672 HBITMAP hbitmap
= 0;
1674 BOOL bDesktopDC
= FALSE
;
1675 NTSTATUS Status
= STATUS_SUCCESS
;
1677 if (!bmi
) return hbitmap
; // Make sure.
1681 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1682 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1683 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, (WORD
)Usage
), 1);
1685 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1687 Status
= _SEH2_GetExceptionCode();
1691 if(!NT_SUCCESS(Status
))
1693 SetLastNtError(Status
);
1697 // If the reference hdc is null, take the desktop dc
1700 hDC
= NtGdiCreateCompatibleDC(0);
1704 if ((dc
= DC_LockDc(hDC
)))
1706 hbitmap
= DIB_CreateDIBSection(dc
,
1717 EngSetLastError(ERROR_INVALID_HANDLE
);
1721 NtGdiDeleteObjectApp(hDC
);
1728 DIB_CreateDIBSection(
1730 CONST BITMAPINFO
*bmi
,
1738 SURFACE
*bmp
= NULL
;
1739 void *mapBits
= NULL
;
1740 PPALETTE ppalDIB
= NULL
;
1742 // Fill BITMAP32 structure with DIB data
1743 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1750 DPRINT("format (%ld,%ld), planes %u, bpp %u, size %lu, colors %lu (%s)\n",
1751 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1752 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1754 /* CreateDIBSection should fail for compressed formats */
1755 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1757 DPRINT1("no compressed format allowed\n");
1758 return (HBITMAP
)NULL
;
1761 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1763 bm
.bmWidth
= bi
->biWidth
;
1764 bm
.bmHeight
= effHeight
;
1765 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: WIDTH_BYTES_ALIGN32(bm
.bmWidth
, bi
->biBitCount
);
1767 bm
.bmPlanes
= bi
->biPlanes
;
1768 bm
.bmBitsPixel
= bi
->biBitCount
;
1771 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1772 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1773 totalSize
= (bi
->biSizeImage
&& (bi
->biCompression
!= BI_RGB
) && (bi
->biCompression
!= BI_BITFIELDS
))
1774 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1778 SYSTEM_BASIC_INFORMATION Sbi
;
1781 LARGE_INTEGER SectionOffset
;
1784 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1788 if (!NT_SUCCESS(Status
))
1790 DPRINT1("ZwQuerySystemInformation failed (0x%lx)\n", Status
);
1794 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1795 mapSize
= totalSize
+ (offset
- mapOffset
);
1797 SectionOffset
.LowPart
= mapOffset
;
1798 SectionOffset
.HighPart
= 0;
1800 Status
= ZwMapViewOfSection(section
,
1810 if (!NT_SUCCESS(Status
))
1812 DPRINT1("ZwMapViewOfSection failed (0x%lx)\n", Status
);
1813 EngSetLastError(ERROR_INVALID_PARAMETER
);
1817 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1819 else if (ovr_pitch
&& offset
)
1820 bm
.bmBits
= (LPVOID
) offset
;
1824 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1827 DPRINT1("Failed to allocate memory\n");
1832 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1833 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1836 // Create Device Dependent Bitmap and add DIB pointer
1837 //Size.cx = bm.bmWidth;
1838 //Size.cy = abs(bm.bmHeight);
1839 res
= GreCreateBitmapEx(bm
.bmWidth
,
1842 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1843 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1844 ((bi
->biHeight
< 0) ? BMF_TOPDOWN
: 0),
1850 DPRINT1("GreCreateBitmapEx failed\n");
1851 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
1854 bmp
= SURFACE_ShareLockSurface(res
); // HACK
1857 DPRINT1("SURFACE_LockSurface failed\n");
1858 EngSetLastError(ERROR_INVALID_HANDLE
);
1862 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1863 table between the DIB and the X physical device. Obviously,
1864 this is left out of the ReactOS implementation. Instead,
1865 we call NtGdiSetDIBColorTable. */
1866 bmp
->hDIBSection
= section
;
1867 bmp
->hSecure
= hSecure
;
1868 bmp
->dwOffset
= offset
;
1869 bmp
->flags
= API_BITMAP
;
1870 bmp
->biClrImportant
= bi
->biClrImportant
;
1872 /* Create a palette for the DIB */
1873 ppalDIB
= CreateDIBPalette(bmi
, dc
, usage
);
1875 // Clean up in case of errors
1877 if (!res
|| !bmp
|| !bm
.bmBits
|| !ppalDIB
)
1879 DPRINT("Got an error res=%p, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1882 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1885 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1889 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1894 SURFACE_ShareUnlockSurface(bmp
);
1900 GreDeleteObject(res
);
1906 PALETTE_ShareUnlockPalette(ppalDIB
);
1912 /* If we're here, everything went fine */
1913 SURFACE_vSetPalette(bmp
, ppalDIB
);
1914 PALETTE_ShareUnlockPalette(ppalDIB
);
1915 SURFACE_ShareUnlockSurface(bmp
);
1918 // Return BITMAP handle and storage location
1919 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1927 /***********************************************************************
1930 * Get the info from a bitmap header.
1931 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
1935 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
1936 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
1938 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1940 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
1941 *width
= core
->bcWidth
;
1942 *height
= core
->bcHeight
;
1943 *planes
= core
->bcPlanes
;
1944 *bpp
= core
->bcBitCount
;
1949 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* Assume BITMAPINFOHEADER */
1951 *width
= header
->biWidth
;
1952 *height
= header
->biHeight
;
1953 *planes
= header
->biPlanes
;
1954 *bpp
= header
->biBitCount
;
1955 *compr
= header
->biCompression
;
1956 *size
= header
->biSizeImage
;
1959 DPRINT1("(%u): unknown/wrong size for header\n", header
->biSize
);
1963 /***********************************************************************
1964 * DIB_GetDIBImageBytes
1966 * Return the number of bytes used to hold the image in a DIB bitmap.
1967 * 11/16/1999 (RJJ) lifted from wine
1970 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1972 return WIDTH_BYTES_ALIGN32(width
, depth
) * (height
< 0 ? -height
: height
);
1975 /***********************************************************************
1976 * DIB_BitmapInfoSize
1978 * Return the size of the bitmap info structure including color table.
1979 * 11/16/1999 (RJJ) lifted from wine
1982 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1984 unsigned int colors
, size
, masks
= 0;
1985 unsigned int colorsize
;
1987 colorsize
= (coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) :
1988 (coloruse
== DIB_PAL_INDICES
) ? 0 :
1991 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1993 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
1994 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1995 return sizeof(BITMAPCOREHEADER
) + colors
* colorsize
;
1997 else /* Assume BITMAPINFOHEADER */
1999 colors
= info
->bmiHeader
.biClrUsed
;
2000 if (colors
> 256) colors
= 256;
2001 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
2002 colors
= 1 << info
->bmiHeader
.biBitCount
;
2003 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
2004 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
2005 return size
+ colors
* colorsize
;
2011 DIB_MapPaletteColors(PPALETTE ppalDc
, CONST BITMAPINFO
* lpbmi
)
2018 if (!(ppalDc
->flFlags
& PAL_INDEXED
))
2023 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
2024 if (lpbmi
->bmiHeader
.biClrUsed
)
2026 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
2029 ppalNew
= PALETTE_AllocPalWithHandle(PAL_INDEXED
, nNumColors
, NULL
, 0, 0, 0);
2030 if (ppalNew
== NULL
)
2032 DPRINT1("Could not allocate palette\n");
2036 lpIndex
= (USHORT
*)((PBYTE
)lpbmi
+ lpbmi
->bmiHeader
.biSize
);
2038 for (i
= 0; i
< nNumColors
; i
++)
2040 ULONG iColorIndex
= *lpIndex
% ppalDc
->NumColors
;
2041 ppalNew
->IndexedColors
[i
] = ppalDc
->IndexedColors
[iColorIndex
];
2045 hpal
= ppalNew
->BaseObject
.hHmgr
;
2046 PALETTE_UnlockPalette(ppalNew
);
2051 /* Converts a BITMAPCOREINFO to a BITMAPINFO structure,
2052 * or does nothing if it's already a BITMAPINFO (or V4 or V5) */
2055 DIB_ConvertBitmapInfo (CONST BITMAPINFO
* pbmi
, DWORD Usage
)
2057 CONST BITMAPCOREINFO
* pbmci
= (BITMAPCOREINFO
*)pbmi
;
2058 BITMAPINFO
* pNewBmi
;
2059 UINT numColors
= 0, ColorsSize
= 0;
2061 if(pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) return (BITMAPINFO
*)pbmi
;
2062 if(pbmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) return NULL
;
2064 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
2066 numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
2067 if(Usage
== DIB_PAL_COLORS
)
2069 ColorsSize
= numColors
* sizeof(WORD
);
2073 ColorsSize
= numColors
* sizeof(RGBQUAD
);
2076 else if (Usage
== DIB_PAL_COLORS
)
2078 /* Invalid at high-res */
2082 pNewBmi
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + ColorsSize
, TAG_DIB
);
2083 if(!pNewBmi
) return NULL
;
2085 RtlZeroMemory(pNewBmi
, sizeof(BITMAPINFOHEADER
) + ColorsSize
);
2087 pNewBmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
2088 pNewBmi
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
2089 pNewBmi
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
2090 pNewBmi
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
2091 pNewBmi
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
2092 pNewBmi
->bmiHeader
.biCompression
= BI_RGB
;
2093 pNewBmi
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(pNewBmi
->bmiHeader
.biWidth
,
2094 pNewBmi
->bmiHeader
.biHeight
,
2095 pNewBmi
->bmiHeader
.biBitCount
);
2096 pNewBmi
->bmiHeader
.biClrUsed
= numColors
;
2098 if(Usage
== DIB_PAL_COLORS
)
2100 RtlCopyMemory(pNewBmi
->bmiColors
, pbmci
->bmciColors
, ColorsSize
);
2105 for(i
=0; i
<numColors
; i
++)
2107 pNewBmi
->bmiColors
[i
].rgbRed
= pbmci
->bmciColors
[i
].rgbtRed
;
2108 pNewBmi
->bmiColors
[i
].rgbGreen
= pbmci
->bmciColors
[i
].rgbtGreen
;
2109 pNewBmi
->bmiColors
[i
].rgbBlue
= pbmci
->bmciColors
[i
].rgbtBlue
;
2116 /* Frees a BITMAPINFO created with DIB_ConvertBitmapInfo */
2119 DIB_FreeConvertedBitmapInfo(BITMAPINFO
* converted
, BITMAPINFO
* orig
, DWORD usage
)
2121 BITMAPCOREINFO
* pbmci
;
2122 if(converted
== orig
)
2127 /* Caller don't want any conversion */
2128 ExFreePoolWithTag(converted
, TAG_DIB
);
2132 /* Perform inverse conversion */
2133 pbmci
= (BITMAPCOREINFO
*)orig
;
2135 ASSERT(pbmci
->bmciHeader
.bcSize
== sizeof(BITMAPCOREHEADER
));
2136 pbmci
->bmciHeader
.bcBitCount
= converted
->bmiHeader
.biBitCount
;
2137 pbmci
->bmciHeader
.bcWidth
= converted
->bmiHeader
.biWidth
;
2138 pbmci
->bmciHeader
.bcHeight
= converted
->bmiHeader
.biHeight
;
2139 pbmci
->bmciHeader
.bcPlanes
= converted
->bmiHeader
.biPlanes
;
2141 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
2143 UINT numColors
= converted
->bmiHeader
.biClrUsed
;
2144 if(!numColors
) numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
2145 if(usage
== DIB_PAL_COLORS
)
2147 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(WORD
));
2148 RtlCopyMemory(pbmci
->bmciColors
, converted
->bmiColors
, numColors
* sizeof(WORD
));
2153 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(RGBTRIPLE
));
2154 for(i
=0; i
<numColors
; i
++)
2156 pbmci
->bmciColors
[i
].rgbtRed
= converted
->bmiColors
[i
].rgbRed
;
2157 pbmci
->bmciColors
[i
].rgbtGreen
= converted
->bmiColors
[i
].rgbGreen
;
2158 pbmci
->bmciColors
[i
].rgbtBlue
= converted
->bmiColors
[i
].rgbBlue
;
2162 /* Now free it, it's not needed anymore */
2163 ExFreePoolWithTag(converted
, TAG_DIB
);