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, 0xff, 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;
264 if (!bmi
|| !Bits
) return 0;
266 /* Check for uncompressed formats */
267 if ((bmi
->bmiHeader
.biCompression
== BI_RGB
) ||
268 (bmi
->bmiHeader
.biCompression
== BI_BITFIELDS
))
270 /* Calculate the image size */
271 cjSizeImage
= DIB_GetDIBImageBytes(bmi
->bmiHeader
.biWidth
,
273 bmi
->bmiHeader
.biBitCount
);
275 /* Check if the header provided an image size */
276 else if (bmi
->bmiHeader
.biSizeImage
!= 0)
278 /* Use the given size */
279 cjSizeImage
= bmi
->bmiHeader
.biSizeImage
;
283 /* Compressed format without a size. This is invalid. */
284 DPRINT1("Compressed format without a size!");
288 /* Check if the size that we have is ok */
289 if ((cjSizeImage
> cjMaxBits
) || (cjSizeImage
== 0))
291 DPRINT1("Invalid bitmap size! cjSizeImage = %lu, cjMaxBits = %lu\n",
292 cjSizeImage
, cjMaxBits
);
296 SourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
299 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
300 bmi
->bmiHeader
.biCompression
),
301 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
307 DPRINT1("Error: Could not create a bitmap.\n");
308 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
312 psurfDst
= SURFACE_ShareLockSurface(hBitmap
);
313 psurfSrc
= SURFACE_ShareLockSurface(SourceBitmap
);
315 if(!(psurfSrc
&& psurfDst
))
317 DPRINT1("Error: Could not lock surfaces\n");
321 /* Create a palette for the DIB */
322 ppalDIB
= CreateDIBPalette(bmi
, DC
, ColorUse
);
325 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
329 /* Initialize EXLATEOBJ */
330 EXLATEOBJ_vInitialize(&exlo
,
333 RGB(0xff, 0xff, 0xff),
334 RGB(0xff, 0xff, 0xff), //DC->pdcattr->crBackgroundClr,
335 0); // DC->pdcattr->crForegroundClr);
337 rcDst
.top
= StartScan
;
339 rcDst
.bottom
= rcDst
.top
+ ScanLines
;
340 rcDst
.right
= psurfDst
->SurfObj
.sizlBitmap
.cx
;
344 result
= IntEngCopyBits(&psurfDst
->SurfObj
,
353 EXLATEOBJ_vCleanup(&exlo
);
356 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
357 if(psurfSrc
) SURFACE_ShareUnlockSurface(psurfSrc
);
358 if(psurfDst
) SURFACE_ShareUnlockSurface(psurfDst
);
359 GreDeleteObject(SourceBitmap
);
366 IntGdiCreateMaskFromRLE(
377 BYTE Data
, NumPixels
, ToSkip
;
379 ASSERT((Compression
== BI_RLE8
) || (Compression
== BI_RLE4
));
381 /* Create the bitmap */
382 Mask
= GreCreateBitmapEx(Width
, Height
, 0, BMF_1BPP
, 0, 0, NULL
, 0);
386 SurfObj
= EngLockSurface((HSURF
)Mask
);
389 GreDeleteObject(Mask
);
392 ASSERT(SurfObj
->pvBits
!= NULL
);
404 if ((x
+ NumPixels
) > Width
)
405 NumPixels
= Width
- x
;
410 DIB_1BPP_HLine(SurfObj
, x
, x
+ NumPixels
, y
, 1);
431 if (i
>= (BitsSize
- 1))
442 /* Done for this run */
446 /* Embedded data into the RLE */
448 if (Compression
== BI_RLE8
)
451 ToSkip
= (NumPixels
/ 2) + (NumPixels
& 1);
453 if ((i
+ ToSkip
) > BitsSize
)
455 ToSkip
= (ToSkip
+ 1) & ~1;
457 if ((x
+ NumPixels
) > Width
)
458 NumPixels
= Width
- x
;
462 DIB_1BPP_HLine(SurfObj
, x
, x
+ NumPixels
, y
, 1);
469 EngUnlockSurface(SurfObj
);
476 NtGdiSetDIBitsToDeviceInternal(
491 IN BOOL bTransformCoordinates
,
492 IN OPTIONAL HANDLE hcmXform
)
495 NTSTATUS Status
= STATUS_SUCCESS
;
497 HBITMAP hSourceBitmap
= NULL
, hMaskBitmap
= NULL
;
498 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
, *pMaskSurf
= NULL
;
505 PPALETTE ppalDIB
= NULL
;
506 LPBITMAPINFO pbmiSafe
;
510 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjMaxInfo
, 'pmTG');
511 if (!pbmiSafe
) return 0;
515 ProbeForRead(bmi
, cjMaxInfo
, 1);
516 ProbeForRead(Bits
, cjMaxBits
, 1);
517 RtlCopyMemory(pbmiSafe
, bmi
, cjMaxInfo
);
520 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
522 Status
= _SEH2_GetExceptionCode();
526 if (!NT_SUCCESS(Status
))
531 ScanLines
= min(ScanLines
, abs(bmi
->bmiHeader
.biHeight
) - StartScan
);
534 DPRINT1("ScanLines == 0\n");
538 pDC
= DC_LockDc(hDC
);
541 EngSetLastError(ERROR_INVALID_HANDLE
);
545 if (pDC
->dctype
== DC_TYPE_INFO
)
552 if (bTransformCoordinates
)
554 IntLPtoDP(pDC
, (LPPOINT
)&rcDest
, 2);
556 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
557 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
558 rcDest
.right
= rcDest
.left
+ Width
;
559 rcDest
.bottom
= rcDest
.top
+ Height
;
560 rcDest
.top
+= StartScan
;
562 if (pDC
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
564 IntUpdateBoundsRect(pDC
, &rcDest
);
570 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
571 SourceSize
.cy
= ScanLines
;
573 //DIBWidth = WIDTH_BYTES_ALIGN32(SourceSize.cx, bmi->bmiHeader.biBitCount);
575 hSourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
578 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
579 bmi
->bmiHeader
.biCompression
),
580 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
581 bmi
->bmiHeader
.biSizeImage
,
587 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
588 Status
= STATUS_NO_MEMORY
;
592 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
595 Status
= STATUS_UNSUCCESSFUL
;
599 /* HACK: If this is a RLE bitmap, only the relevant pixels must be set. */
600 if ((bmi
->bmiHeader
.biCompression
== BI_RLE8
) || (bmi
->bmiHeader
.biCompression
== BI_RLE4
))
602 hMaskBitmap
= IntGdiCreateMaskFromRLE(bmi
->bmiHeader
.biWidth
,
604 bmi
->bmiHeader
.biCompression
,
609 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
610 Status
= STATUS_NO_MEMORY
;
613 pMaskSurf
= EngLockSurface((HSURF
)hMaskBitmap
);
616 Status
= STATUS_UNSUCCESSFUL
;
621 /* Create a palette for the DIB */
622 ppalDIB
= CreateDIBPalette(bmi
, pDC
, ColorUse
);
625 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
626 Status
= STATUS_NO_MEMORY
;
630 /* This is actually a blit */
631 DC_vPrepareDCsForBlit(pDC
, &rcDest
, NULL
, NULL
);
632 pSurf
= pDC
->dclevel
.pSurface
;
635 DC_vFinishBlit(pDC
, NULL
);
642 /* Initialize EXLATEOBJ */
643 EXLATEOBJ_vInitialize(&exlo
,
646 RGB(0xff, 0xff, 0xff),
647 pDC
->pdcattr
->crBackgroundClr
,
648 pDC
->pdcattr
->crForegroundClr
);
650 pDestSurf
= &pSurf
->SurfObj
;
653 DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
654 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
655 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
656 Status
= IntEngBitBlt(pDestSurf
,
663 pMaskSurf
? &ptSource
: NULL
,
666 pMaskSurf
? ROP4_MASK
: ROP4_FROM_INDEX(R3_OPINDEX_SRCCOPY
));
668 /* Cleanup EXLATEOBJ */
669 EXLATEOBJ_vCleanup(&exlo
);
672 DC_vFinishBlit(pDC
, NULL
);
675 if (NT_SUCCESS(Status
))
680 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
681 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
682 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
683 if (pMaskSurf
) EngUnlockSurface(pMaskSurf
);
684 if (hMaskBitmap
) EngDeleteSurface((HSURF
)hMaskBitmap
);
685 if (pDC
) DC_UnlockDc(pDC
);
686 ExFreePoolWithTag(pbmiSafe
, 'pmTG');
692 /* Converts a device-dependent bitmap to a DIB */
695 GreGetDIBitsInternal(
706 BITMAPCOREINFO
* pbmci
= NULL
;
707 PSURFACE psurf
= NULL
;
717 DPRINT("Entered GreGetDIBitsInternal()\n");
719 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
722 colorPtr
= (LPBYTE
)Info
+ Info
->bmiHeader
.biSize
;
725 bitmap_type
= DIB_GetBitmapInfo(&Info
->bmiHeader
,
732 if(bitmap_type
== -1)
734 DPRINT("Wrong bitmap format\n");
735 EngSetLastError(ERROR_INVALID_PARAMETER
);
738 else if(bitmap_type
== 0)
740 /* We need a BITMAPINFO to create a DIB, but we have to fill
741 * the BITMAPCOREINFO we're provided */
742 pbmci
= (BITMAPCOREINFO
*)Info
;
743 Info
= DIB_ConvertBitmapInfo((BITMAPINFO
*)pbmci
, Usage
);
746 DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
749 rgbQuads
= Info
->bmiColors
;
752 pDC
= DC_LockDc(hDC
);
753 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
759 /* Get a pointer to the source bitmap object */
760 psurf
= SURFACE_ShareLockSurface(hBitmap
);
767 /* Fill in the structure */
770 case 0: /* Only info */
771 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
772 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
773 -psurf
->SurfObj
.sizlBitmap
.cy
:
774 psurf
->SurfObj
.sizlBitmap
.cy
;
775 Info
->bmiHeader
.biPlanes
= 1;
776 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
777 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
778 Info
->bmiHeader
.biHeight
,
779 Info
->bmiHeader
.biBitCount
);
781 if ((Info
->bmiHeader
.biBitCount
== 16) ||
782 (Info
->bmiHeader
.biBitCount
== 32))
784 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
788 Info
->bmiHeader
.biCompression
= BI_RGB
;
790 Info
->bmiHeader
.biXPelsPerMeter
= 0;
791 Info
->bmiHeader
.biYPelsPerMeter
= 0;
792 Info
->bmiHeader
.biClrUsed
= 0;
793 Info
->bmiHeader
.biClrImportant
= 0;
794 ScanLines
= abs(Info
->bmiHeader
.biHeight
);
800 Info
->bmiHeader
.biClrUsed
= 0;
802 /* If the bitmap is a DIB section and has the same format as what
803 * is requested, go ahead! */
804 if((psurf
->hSecure
) &&
805 (BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bpp
))
807 if(Usage
== DIB_RGB_COLORS
)
809 ULONG colors
= min(psurf
->ppal
->NumColors
, 256);
810 if(colors
!= 256) Info
->bmiHeader
.biClrUsed
= colors
;
811 for(i
= 0; i
< colors
; i
++)
813 rgbQuads
[i
].rgbRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
814 rgbQuads
[i
].rgbGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
815 rgbQuads
[i
].rgbBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
816 rgbQuads
[i
].rgbReserved
= 0;
821 for(i
= 0; i
< 256; i
++)
822 ((WORD
*)rgbQuads
)[i
] = i
;
827 if(Usage
== DIB_PAL_COLORS
)
829 for(i
= 0; i
< 256; i
++)
831 ((WORD
*)rgbQuads
)[i
] = i
;
834 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
))
836 /* For color DDBs in native depth (mono DDBs always have
837 a black/white palette):
838 Generate the color map from the selected palette */
839 PPALETTE pDcPal
= PALETTE_ShareLockPalette(pDC
->dclevel
.hpal
);
845 for (i
= 0; i
< pDcPal
->NumColors
; i
++)
847 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
848 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
849 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
850 rgbQuads
[i
].rgbReserved
= 0;
852 PALETTE_ShareUnlockPalette(pDcPal
);
859 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
= rgbQuads
[0].rgbBlue
= 0;
860 rgbQuads
[0].rgbReserved
= 0;
861 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
= rgbQuads
[1].rgbBlue
= 0xff;
862 rgbQuads
[1].rgbReserved
= 0;
866 RtlCopyMemory(rgbQuads
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
873 memcpy(rgbQuads
, DefLogPaletteQuads
, 10 * sizeof(RGBQUAD
));
874 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10, 10 * sizeof(RGBQUAD
));
876 for (i
= 10; i
< 246; i
++)
878 rgbQuads
[i
].rgbRed
= (i
& 0x07) << 5;
879 rgbQuads
[i
].rgbGreen
= (i
& 0x38) << 2;
880 rgbQuads
[i
].rgbBlue
= i
& 0xc0;
881 rgbQuads
[i
].rgbReserved
= 0;
890 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
892 ((PDWORD
)Info
->bmiColors
)[0] = 0x7c00;
893 ((PDWORD
)Info
->bmiColors
)[1] = 0x03e0;
894 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
899 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
903 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
904 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
905 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
909 ((PDWORD
)Info
->bmiColors
)[0] = 0xf800;
910 ((PDWORD
)Info
->bmiColors
)[1] = 0x07e0;
911 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
918 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
922 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
923 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
924 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
928 ((PDWORD
)Info
->bmiColors
)[0] = 0xff0000;
929 ((PDWORD
)Info
->bmiColors
)[1] = 0x00ff00;
930 ((PDWORD
)Info
->bmiColors
)[2] = 0x0000ff;
935 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(width
, height
, bpp
);
937 if(Bits
&& ScanLines
)
939 /* Create a DIBSECTION, blt it, profit */
948 if (StartScan
> (ULONG
)psurf
->SurfObj
.sizlBitmap
.cy
)
955 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
959 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
960 Info
->bmiHeader
.biHeight
= (height
< 0) ?
961 -(LONG
)ScanLines
: ScanLines
;
963 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
965 Info
->bmiHeader
.biWidth
= width
;
966 Info
->bmiHeader
.biHeight
= height
;
970 DPRINT1("Unable to create a DIB Section!\n");
971 EngSetLastError(ERROR_INVALID_PARAMETER
);
976 psurfDest
= SURFACE_ShareLockSurface(hBmpDest
);
978 RECTL_vSetRect(&rcDest
, 0, 0, psurf
->SurfObj
.sizlBitmap
.cx
, ScanLines
);
986 if(ScanLines
<= StartScan
)
989 SURFACE_ShareUnlockSurface(psurfDest
);
990 GreDeleteObject(hBmpDest
);
994 ScanLines
-= StartScan
;
998 srcPoint
.y
= StartScan
;
1001 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xffffff, 0xffffff, 0);
1003 ret
= IntEngCopyBits(&psurfDest
->SurfObj
,
1010 SURFACE_ShareUnlockSurface(psurfDest
);
1016 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, ScanLines
, bpp
));
1019 GreDeleteObject(hBmpDest
);
1020 EXLATEOBJ_vCleanup(&exlo
);
1022 else ScanLines
= abs(height
);
1026 if(pDC
) DC_UnlockDc(pDC
);
1027 if(psurf
) SURFACE_ShareUnlockSurface(psurf
);
1028 if(pbmci
) DIB_FreeConvertedBitmapInfo(Info
, (BITMAPINFO
*)pbmci
, Usage
);
1033 _Success_(return!=0)
1037 NtGdiGetDIBitsInternal(
1040 _In_ UINT iStartScan
,
1042 _Out_writes_bytes_opt_(cjMaxBits
) LPBYTE pjBits
,
1043 _Inout_ LPBITMAPINFO pbmi
,
1045 _In_ UINT cjMaxBits
,
1046 _In_ UINT cjMaxInfo
)
1048 PBITMAPINFO pbmiSafe
;
1049 HANDLE hSecure
= NULL
;
1053 /* Check for bad iUsage */
1054 if (iUsage
> 2) return 0;
1056 /* Check if the size of the bitmap info is large enough */
1057 if (cjMaxInfo
< sizeof(BITMAPCOREHEADER
))
1062 /* Use maximum size */
1063 cjMaxInfo
= min(cjMaxInfo
, sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
));
1065 // HACK: the underlying code sucks and doesn't care for the size, so we
1066 // give it the maximum ever needed
1067 cjAlloc
= sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
);
1069 /* Allocate a buffer the bitmapinfo */
1070 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjAlloc
, 'imBG');
1080 /* Probe and copy the BITMAPINFO */
1081 ProbeForRead(pbmi
, cjMaxInfo
, 1);
1082 RtlCopyMemory(pbmiSafe
, pbmi
, cjMaxInfo
);
1084 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1086 _SEH2_YIELD(goto cleanup
;)
1090 /* Check if the header size is large enough */
1091 if ((pbmiSafe
->bmiHeader
.biSize
< sizeof(BITMAPCOREHEADER
)) ||
1092 (pbmiSafe
->bmiHeader
.biSize
> cjMaxInfo
))
1097 /* Check if the caller provided bitmap bits */
1100 /* Secure the user mode memory */
1101 hSecure
= EngSecureMem(pjBits
, cjMaxBits
);
1108 /* Now call the internal function */
1109 iResult
= GreGetDIBitsInternal(hdc
,
1119 /* Check for success */
1122 /* Use SEH to copy back to user mode */
1125 /* Copy the data back */
1126 cjMaxInfo
= min(cjMaxInfo
, (UINT
)DIB_BitmapInfoSize(pbmiSafe
, (WORD
)iUsage
));
1127 ProbeForWrite(pbmi
, cjMaxInfo
, 1);
1128 RtlCopyMemory(pbmi
, pbmiSafe
, cjMaxInfo
);
1130 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1139 if (hSecure
) EngUnsecureMem(hSecure
);
1140 ExFreePoolWithTag(pbmiSafe
, 'imBG');
1149 NtGdiStretchDIBitsInternal(
1159 IN OPTIONAL LPBYTE pjInit
,
1160 IN LPBITMAPINFO pbmi
,
1162 IN DWORD dwRop
, // MS ntgdi.h says dwRop4(?)
1167 BOOL bResult
= FALSE
;
1172 PSURFACE psurfTmp
= 0, psurfDst
= 0;
1173 PPALETTE ppalDIB
= 0;
1177 if (!(pdc
= DC_LockDc(hdc
)))
1179 EngSetLastError(ERROR_INVALID_HANDLE
);
1183 /* Check for info / mem DC without surface */
1184 if (!pdc
->dclevel
.pSurface
)
1191 /* Transform dest size */
1194 IntLPtoDP(pdc
, (POINTL
*)&sizel
, 1);
1197 /* Check if we can use NtGdiSetDIBitsToDeviceInternal */
1198 if ((sizel
.cx
== cxSrc
) && (sizel
.cy
== cySrc
) && (dwRop
== SRCCOPY
))
1201 return NtGdiSetDIBitsToDeviceInternal(hdc
,
1219 if (pjInit
&& (cjMaxBits
> 0))
1221 pvBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, 'pmeT');
1229 ProbeForRead(pjInit
, cjMaxBits
, 1);
1230 RtlCopyMemory(pvBits
, pjInit
, cjMaxBits
);
1232 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1234 ExFreePoolWithTag(pvBits
, 'pmeT');
1235 _SEH2_YIELD(return 0);
1244 /* FIXME: Locking twice is cheesy, coord tranlation in UM will fix it */
1245 if (!(pdc
= DC_LockDc(hdc
)))
1247 DPRINT1("Could not lock dc\n");
1248 EngSetLastError(ERROR_INVALID_HANDLE
);
1252 /* Calculate source and destination rect */
1255 rcSrc
.right
= xSrc
+ abs(cxSrc
);
1256 rcSrc
.bottom
= ySrc
+ abs(cySrc
);
1259 rcDst
.right
= rcDst
.left
+ cxDst
;
1260 rcDst
.bottom
= rcDst
.top
+ cyDst
;
1261 IntLPtoDP(pdc
, (POINTL
*)&rcDst
, 2);
1262 RECTL_vOffsetRect(&rcDst
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1264 if (pdc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
1266 IntUpdateBoundsRect(pdc
, &rcDst
);
1269 hbmTmp
= GreCreateBitmapEx(pbmi
->bmiHeader
.biWidth
,
1270 abs(pbmi
->bmiHeader
.biHeight
),
1272 BitmapFormat(pbmi
->bmiHeader
.biBitCount
,
1273 pbmi
->bmiHeader
.biCompression
),
1274 pbmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
1275 pbmi
->bmiHeader
.biSizeImage
,
1285 psurfTmp
= SURFACE_ShareLockSurface(hbmTmp
);
1292 /* Create a palette for the DIB */
1293 ppalDIB
= CreateDIBPalette(pbmi
, pdc
, dwUsage
);
1300 /* Prepare DC for blit */
1301 DC_vPrepareDCsForBlit(pdc
, &rcDst
, NULL
, NULL
);
1303 psurfDst
= pdc
->dclevel
.pSurface
;
1305 /* Initialize XLATEOBJ */
1306 EXLATEOBJ_vInitialize(&exlo
,
1309 RGB(0xff, 0xff, 0xff),
1310 pdc
->pdcattr
->crBackgroundClr
,
1311 pdc
->pdcattr
->crForegroundClr
);
1313 /* Perform the stretch operation */
1314 bResult
= IntEngStretchBlt(&psurfDst
->SurfObj
,
1323 &pdc
->eboFill
.BrushObject
,
1325 WIN32_ROP3_TO_ENG_ROP4(dwRop
));
1328 DC_vFinishBlit(pdc
, NULL
);
1329 EXLATEOBJ_vCleanup(&exlo
);
1331 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
1332 if (psurfTmp
) SURFACE_ShareUnlockSurface(psurfTmp
);
1333 if (hbmTmp
) GreDeleteObject(hbmTmp
);
1334 if (pdc
) DC_UnlockDc(pdc
);
1335 if (pvBits
) ExFreePoolWithTag(pvBits
, 'pmeT');
1358 ULONG BmpFormat
= 0;
1361 BmpFormat
= BitmapFormat(planes
* bpp
, compression
);
1363 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1364 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1366 if (BmpFormat
!= BMF_1BPP
) fColor
= TRUE
;
1367 else if ((coloruse
> DIB_RGB_COLORS
) || ((init
& CBM_INIT
) == 0) || !data
) fColor
= FALSE
;
1370 const RGBQUAD
*rgb
= (RGBQUAD
*)((PBYTE
)data
+ data
->bmiHeader
.biSize
);
1371 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1373 // Check if the first color of the colormap is black
1374 if (col
== RGB(0, 0, 0))
1377 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1379 // If the second color is white, create a monochrome bitmap
1380 fColor
= (col
!= RGB(0xff,0xff,0xff));
1385 // Now create the bitmap
1388 if (init
& CBM_CREATDIB
)
1393 /* Undocumented flag which creates a DDB of the format specified by the bitmap info. */
1394 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
, planes
, bpp
);
1397 DPRINT1("IntCreateCompatibleBitmap() failed!\n");
1401 /* The palette must also match the given data */
1402 Surface
= SURFACE_ShareLockSurface(handle
);
1404 Palette
= CreateDIBPalette(data
, Dc
, coloruse
);
1406 SURFACE_vSetPalette(Surface
, Palette
);
1408 PALETTE_ShareUnlockPalette(Palette
);
1409 SURFACE_ShareUnlockSurface(Surface
);
1413 /* Create a regular compatible bitmap, in the same format as the device */
1414 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
, 0, 0);
1419 handle
= GreCreateBitmap(width
,
1429 if ((NULL
!= handle
) && (CBM_INIT
& init
))
1431 IntSetDIBits(Dc
, handle
, 0, height
, bits
, cjMaxBits
, data
, coloruse
);
1437 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1438 // The DDB that is created will be whatever bit depth your reference DC is
1441 NtGdiCreateDIBitmapInternal(
1446 IN OPTIONAL LPBYTE pjInit
,
1447 IN OPTIONAL LPBITMAPINFO pbmi
,
1449 IN UINT cjMaxInitInfo
,
1454 NTSTATUS Status
= STATUS_SUCCESS
;
1455 PBYTE safeBits
= NULL
;
1456 HBITMAP hbmResult
= NULL
;
1463 if(pjInit
&& (fInit
& CBM_INIT
))
1465 if (cjMaxBits
== 0) return NULL
;
1466 safeBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, TAG_DIB
);
1469 DPRINT1("Failed to allocate %lu bytes\n", cjMaxBits
);
1470 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1477 if(pbmi
) ProbeForRead(pbmi
, cjMaxInitInfo
, 1);
1478 if(pjInit
&& (fInit
& CBM_INIT
))
1480 ProbeForRead(pjInit
, cjMaxBits
, 1);
1481 RtlCopyMemory(safeBits
, pjInit
, cjMaxBits
);
1484 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1486 Status
= _SEH2_GetExceptionCode();
1490 if(!NT_SUCCESS(Status
))
1492 DPRINT1("Got an exception! pjInit = %p\n", pjInit
);
1493 SetLastNtError(Status
);
1497 hbmResult
= GreCreateDIBitmapInternal(hDc
,
1509 if (safeBits
) ExFreePoolWithTag(safeBits
, TAG_DIB
);
1515 GreCreateDIBitmapInternal(
1520 IN OPTIONAL LPBYTE pjInit
,
1521 IN OPTIONAL PBITMAPINFO pbmi
,
1533 if (!hDc
) /* 1bpp monochrome bitmap */
1535 // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1536 hdcDest
= NtGdiCreateCompatibleDC(0);
1539 DPRINT1("NtGdiCreateCompatibleDC failed\n");
1548 Dc
= DC_LockDc(hdcDest
);
1551 DPRINT1("Failed to lock hdcDest %p\n", hdcDest
);
1552 EngSetLastError(ERROR_INVALID_HANDLE
);
1555 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1556 * if bpp != 1 and ignore the real value that was passed */
1559 if (pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1561 BITMAPCOREHEADER
* CoreHeader
= (BITMAPCOREHEADER
*)&pbmi
->bmiHeader
;
1562 bpp
= CoreHeader
->bcBitCount
;
1563 planes
= CoreHeader
->bcPlanes
? CoreHeader
->bcPlanes
: 1;
1564 compression
= BI_RGB
;
1568 bpp
= pbmi
->bmiHeader
.biBitCount
;
1569 planes
= pbmi
->bmiHeader
.biPlanes
? pbmi
->bmiHeader
.biPlanes
: 1;
1570 compression
= pbmi
->bmiHeader
.biCompression
;
1579 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, planes
, bpp
, compression
, fInit
, pjInit
, cjMaxBits
, pbmi
, iUsage
);
1584 NtGdiDeleteObjectApp(hdcDest
);
1591 GreCreateDIBitmapFromPackedDIB(
1592 _In_reads_(cjPackedDIB
)PVOID pvPackedDIB
,
1593 _In_ UINT cjPackedDIB
,
1598 UINT cjInfo
, cjBits
;
1601 /* We only support BITMAPINFOHEADER, make sure the size is ok */
1602 if (cjPackedDIB
< sizeof(BITMAPINFOHEADER
))
1607 /* The packed DIB starts with the BITMAPINFOHEADER */
1610 if (cjPackedDIB
< pbmi
->bmiHeader
.biSize
)
1615 /* Calculate the info size and make sure the packed DIB is large enough */
1616 cjInfo
= DIB_BitmapInfoSize(pbmi
, uUsage
);
1617 if (cjPackedDIB
<= cjInfo
)
1622 /* The bitmap bits start after the header */
1623 pjBits
= (PBYTE
)pvPackedDIB
+ cjInfo
;
1624 cjBits
= cjPackedDIB
- cjInfo
;
1626 hbm
= GreCreateDIBitmapInternal(NULL
,
1627 pbmi
->bmiHeader
.biWidth
,
1628 abs(pbmi
->bmiHeader
.biHeight
),
1629 CBM_INIT
| CBM_CREATDIB
,
1642 NtGdiCreateDIBSection(
1644 IN OPTIONAL HANDLE hSection
,
1650 IN ULONG_PTR dwColorSpace
,
1653 HBITMAP hbitmap
= 0;
1655 BOOL bDesktopDC
= FALSE
;
1656 NTSTATUS Status
= STATUS_SUCCESS
;
1658 if (!bmi
) return hbitmap
; // Make sure.
1662 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1663 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1664 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, (WORD
)Usage
), 1);
1666 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1668 Status
= _SEH2_GetExceptionCode();
1672 if(!NT_SUCCESS(Status
))
1674 SetLastNtError(Status
);
1678 // If the reference hdc is null, take the desktop dc
1681 hDC
= NtGdiCreateCompatibleDC(0);
1685 if ((dc
= DC_LockDc(hDC
)))
1687 hbitmap
= DIB_CreateDIBSection(dc
,
1698 EngSetLastError(ERROR_INVALID_HANDLE
);
1702 NtGdiDeleteObjectApp(hDC
);
1709 DIB_CreateDIBSection(
1711 CONST BITMAPINFO
*bmi
,
1719 SURFACE
*bmp
= NULL
;
1720 void *mapBits
= NULL
;
1721 PPALETTE ppalDIB
= NULL
;
1723 // Fill BITMAP32 structure with DIB data
1724 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1731 DPRINT("format (%ld,%ld), planes %u, bpp %u, size %lu, colors %lu (%s)\n",
1732 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1733 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1735 /* CreateDIBSection should fail for compressed formats */
1736 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1738 DPRINT1("no compressed format allowed\n");
1739 return (HBITMAP
)NULL
;
1742 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1744 bm
.bmWidth
= bi
->biWidth
;
1745 bm
.bmHeight
= effHeight
;
1746 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: WIDTH_BYTES_ALIGN32(bm
.bmWidth
, bi
->biBitCount
);
1748 bm
.bmPlanes
= bi
->biPlanes
;
1749 bm
.bmBitsPixel
= bi
->biBitCount
;
1752 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1753 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1754 totalSize
= (bi
->biSizeImage
&& (bi
->biCompression
!= BI_RGB
) && (bi
->biCompression
!= BI_BITFIELDS
))
1755 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1759 SYSTEM_BASIC_INFORMATION Sbi
;
1762 LARGE_INTEGER SectionOffset
;
1765 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1769 if (!NT_SUCCESS(Status
))
1771 DPRINT1("ZwQuerySystemInformation failed (0x%lx)\n", Status
);
1775 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1776 mapSize
= totalSize
+ (offset
- mapOffset
);
1778 SectionOffset
.LowPart
= mapOffset
;
1779 SectionOffset
.HighPart
= 0;
1781 Status
= ZwMapViewOfSection(section
,
1791 if (!NT_SUCCESS(Status
))
1793 DPRINT1("ZwMapViewOfSection failed (0x%lx)\n", Status
);
1794 EngSetLastError(ERROR_INVALID_PARAMETER
);
1798 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1800 else if (ovr_pitch
&& offset
)
1801 bm
.bmBits
= (LPVOID
) offset
;
1805 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1808 DPRINT1("Failed to allocate memory\n");
1813 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1814 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1817 // Create Device Dependent Bitmap and add DIB pointer
1818 //Size.cx = bm.bmWidth;
1819 //Size.cy = abs(bm.bmHeight);
1820 res
= GreCreateBitmapEx(bm
.bmWidth
,
1823 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1824 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1825 ((bi
->biHeight
< 0) ? BMF_TOPDOWN
: 0),
1831 DPRINT1("GreCreateBitmapEx failed\n");
1832 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
1835 bmp
= SURFACE_ShareLockSurface(res
); // HACK
1838 DPRINT1("SURFACE_LockSurface failed\n");
1839 EngSetLastError(ERROR_INVALID_HANDLE
);
1843 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1844 table between the DIB and the X physical device. Obviously,
1845 this is left out of the ReactOS implementation. Instead,
1846 we call NtGdiSetDIBColorTable. */
1847 bmp
->hDIBSection
= section
;
1848 bmp
->hSecure
= hSecure
;
1849 bmp
->dwOffset
= offset
;
1850 bmp
->flags
= API_BITMAP
;
1851 bmp
->biClrImportant
= bi
->biClrImportant
;
1853 /* Create a palette for the DIB */
1854 ppalDIB
= CreateDIBPalette(bmi
, dc
, usage
);
1856 // Clean up in case of errors
1858 if (!res
|| !bmp
|| !bm
.bmBits
|| !ppalDIB
)
1860 DPRINT("Got an error res=%p, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1863 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1866 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1870 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1875 SURFACE_ShareUnlockSurface(bmp
);
1881 GreDeleteObject(res
);
1887 PALETTE_ShareUnlockPalette(ppalDIB
);
1893 /* If we're here, everything went fine */
1894 SURFACE_vSetPalette(bmp
, ppalDIB
);
1895 PALETTE_ShareUnlockPalette(ppalDIB
);
1896 SURFACE_ShareUnlockSurface(bmp
);
1899 // Return BITMAP handle and storage location
1900 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1908 /***********************************************************************
1911 * Get the info from a bitmap header.
1912 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
1916 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
1917 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
1919 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1921 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
1922 *width
= core
->bcWidth
;
1923 *height
= core
->bcHeight
;
1924 *planes
= core
->bcPlanes
;
1925 *bpp
= core
->bcBitCount
;
1930 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* Assume BITMAPINFOHEADER */
1932 *width
= header
->biWidth
;
1933 *height
= header
->biHeight
;
1934 *planes
= header
->biPlanes
;
1935 *bpp
= header
->biBitCount
;
1936 *compr
= header
->biCompression
;
1937 *size
= header
->biSizeImage
;
1940 DPRINT1("(%u): unknown/wrong size for header\n", header
->biSize
);
1944 /***********************************************************************
1945 * DIB_GetDIBImageBytes
1947 * Return the number of bytes used to hold the image in a DIB bitmap.
1948 * 11/16/1999 (RJJ) lifted from wine
1951 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1953 return WIDTH_BYTES_ALIGN32(width
, depth
) * (height
< 0 ? -height
: height
);
1956 /***********************************************************************
1957 * DIB_BitmapInfoSize
1959 * Return the size of the bitmap info structure including color table.
1960 * 11/16/1999 (RJJ) lifted from wine
1963 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1965 unsigned int colors
, size
, masks
= 0;
1966 unsigned int colorsize
;
1968 colorsize
= (coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) :
1969 (coloruse
== DIB_PAL_INDICES
) ? 0 :
1972 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1974 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
1975 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1976 return sizeof(BITMAPCOREHEADER
) + colors
* colorsize
;
1978 else /* Assume BITMAPINFOHEADER */
1980 colors
= info
->bmiHeader
.biClrUsed
;
1981 if (colors
> 256) colors
= 256;
1982 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
1983 colors
= 1 << info
->bmiHeader
.biBitCount
;
1984 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
1985 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
1986 return size
+ colors
* colorsize
;
1992 DIB_MapPaletteColors(PPALETTE ppalDc
, CONST BITMAPINFO
* lpbmi
)
1999 if (!(ppalDc
->flFlags
& PAL_INDEXED
))
2004 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
2005 if (lpbmi
->bmiHeader
.biClrUsed
)
2007 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
2010 ppalNew
= PALETTE_AllocPalWithHandle(PAL_INDEXED
, nNumColors
, NULL
, 0, 0, 0);
2011 if (ppalNew
== NULL
)
2013 DPRINT1("Could not allocate palette\n");
2017 lpIndex
= (USHORT
*)((PBYTE
)lpbmi
+ lpbmi
->bmiHeader
.biSize
);
2019 for (i
= 0; i
< nNumColors
; i
++)
2021 ULONG iColorIndex
= *lpIndex
% ppalDc
->NumColors
;
2022 ppalNew
->IndexedColors
[i
] = ppalDc
->IndexedColors
[iColorIndex
];
2026 hpal
= ppalNew
->BaseObject
.hHmgr
;
2027 PALETTE_UnlockPalette(ppalNew
);
2032 /* Converts a BITMAPCOREINFO to a BITMAPINFO structure,
2033 * or does nothing if it's already a BITMAPINFO (or V4 or V5) */
2036 DIB_ConvertBitmapInfo (CONST BITMAPINFO
* pbmi
, DWORD Usage
)
2038 CONST BITMAPCOREINFO
* pbmci
= (BITMAPCOREINFO
*)pbmi
;
2039 BITMAPINFO
* pNewBmi
;
2040 UINT numColors
= 0, ColorsSize
= 0;
2042 if(pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) return (BITMAPINFO
*)pbmi
;
2043 if(pbmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) return NULL
;
2045 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
2047 numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
2048 if(Usage
== DIB_PAL_COLORS
)
2050 ColorsSize
= numColors
* sizeof(WORD
);
2054 ColorsSize
= numColors
* sizeof(RGBQUAD
);
2057 else if (Usage
== DIB_PAL_COLORS
)
2059 /* Invalid at high-res */
2063 pNewBmi
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + ColorsSize
, TAG_DIB
);
2064 if(!pNewBmi
) return NULL
;
2066 RtlZeroMemory(pNewBmi
, sizeof(BITMAPINFOHEADER
) + ColorsSize
);
2068 pNewBmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
2069 pNewBmi
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
2070 pNewBmi
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
2071 pNewBmi
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
2072 pNewBmi
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
2073 pNewBmi
->bmiHeader
.biCompression
= BI_RGB
;
2074 pNewBmi
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(pNewBmi
->bmiHeader
.biWidth
,
2075 pNewBmi
->bmiHeader
.biHeight
,
2076 pNewBmi
->bmiHeader
.biBitCount
);
2078 if(Usage
== DIB_PAL_COLORS
)
2080 RtlCopyMemory(pNewBmi
->bmiColors
, pbmci
->bmciColors
, ColorsSize
);
2085 for(i
=0; i
<numColors
; i
++)
2087 pNewBmi
->bmiColors
[i
].rgbRed
= pbmci
->bmciColors
[i
].rgbtRed
;
2088 pNewBmi
->bmiColors
[i
].rgbGreen
= pbmci
->bmciColors
[i
].rgbtGreen
;
2089 pNewBmi
->bmiColors
[i
].rgbBlue
= pbmci
->bmciColors
[i
].rgbtBlue
;
2096 /* Frees a BITMAPINFO created with DIB_ConvertBitmapInfo */
2099 DIB_FreeConvertedBitmapInfo(BITMAPINFO
* converted
, BITMAPINFO
* orig
, DWORD usage
)
2101 BITMAPCOREINFO
* pbmci
;
2102 if(converted
== orig
)
2107 /* Caller don't want any conversion */
2108 ExFreePoolWithTag(converted
, TAG_DIB
);
2112 /* Perform inverse conversion */
2113 pbmci
= (BITMAPCOREINFO
*)orig
;
2115 ASSERT(pbmci
->bmciHeader
.bcSize
== sizeof(BITMAPCOREHEADER
));
2116 pbmci
->bmciHeader
.bcBitCount
= converted
->bmiHeader
.biBitCount
;
2117 pbmci
->bmciHeader
.bcWidth
= converted
->bmiHeader
.biWidth
;
2118 pbmci
->bmciHeader
.bcHeight
= converted
->bmiHeader
.biHeight
;
2119 pbmci
->bmciHeader
.bcPlanes
= converted
->bmiHeader
.biPlanes
;
2121 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
2123 UINT numColors
= converted
->bmiHeader
.biClrUsed
;
2124 if(!numColors
) numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
2125 if(usage
== DIB_PAL_COLORS
)
2127 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(WORD
));
2128 RtlCopyMemory(pbmci
->bmciColors
, converted
->bmiColors
, numColors
* sizeof(WORD
));
2133 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(RGBTRIPLE
));
2134 for(i
=0; i
<numColors
; i
++)
2136 pbmci
->bmciColors
[i
].rgbtRed
= converted
->bmiColors
[i
].rgbRed
;
2137 pbmci
->bmciColors
[i
].rgbtGreen
= converted
->bmiColors
[i
].rgbGreen
;
2138 pbmci
->bmciColors
[i
].rgbtBlue
= converted
->bmiColors
[i
].rgbBlue
;
2142 /* Now free it, it's not needed anymore */
2143 ExFreePoolWithTag(converted
, TAG_DIB
);