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
)
475 HBITMAP hSourceBitmap
= NULL
, hMaskBitmap
= NULL
;
476 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
, *pMaskSurf
= NULL
;
483 PPALETTE ppalDIB
= NULL
;
484 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
)
506 ScanLines
= min(ScanLines
, abs(bmi
->bmiHeader
.biHeight
) - StartScan
);
509 DPRINT1("ScanLines == 0\n");
514 pDC
= DC_LockDc(hDC
);
517 EngSetLastError(ERROR_INVALID_HANDLE
);
522 if (pDC
->dctype
== DC_TYPE_INFO
)
530 if (bTransformCoordinates
)
532 IntLPtoDP(pDC
, (LPPOINT
)&rcDest
, 2);
534 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
535 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
536 rcDest
.right
= rcDest
.left
+ Width
;
537 rcDest
.bottom
= rcDest
.top
+ Height
;
538 rcDest
.top
+= StartScan
;
540 if (pDC
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
542 IntUpdateBoundsRect(pDC
, &rcDest
);
548 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
549 SourceSize
.cy
= ScanLines
;
551 //DIBWidth = WIDTH_BYTES_ALIGN32(SourceSize.cx, bmi->bmiHeader.biBitCount);
553 hSourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
556 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
557 bmi
->bmiHeader
.biCompression
),
558 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
559 bmi
->bmiHeader
.biSizeImage
,
565 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
570 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
577 /* HACK: If this is a RLE bitmap, only the relevant pixels must be set. */
578 if ((bmi
->bmiHeader
.biCompression
== BI_RLE8
) || (bmi
->bmiHeader
.biCompression
== BI_RLE4
))
580 hMaskBitmap
= IntGdiCreateMaskFromRLE(bmi
->bmiHeader
.biWidth
,
582 bmi
->bmiHeader
.biCompression
,
587 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
591 pMaskSurf
= EngLockSurface((HSURF
)hMaskBitmap
);
599 /* Create a palette for the DIB */
600 ppalDIB
= CreateDIBPalette(bmi
, pDC
, ColorUse
);
603 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
608 /* This is actually a blit */
609 DC_vPrepareDCsForBlit(pDC
, &rcDest
, NULL
, NULL
);
610 pSurf
= pDC
->dclevel
.pSurface
;
613 DC_vFinishBlit(pDC
, NULL
);
620 /* Initialize EXLATEOBJ */
621 EXLATEOBJ_vInitialize(&exlo
,
624 RGB(0xff, 0xff, 0xff),
625 pDC
->pdcattr
->crBackgroundClr
,
626 pDC
->pdcattr
->crForegroundClr
);
628 pDestSurf
= &pSurf
->SurfObj
;
631 DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
632 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
633 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
634 bResult
= IntEngBitBlt(pDestSurf
,
641 pMaskSurf
? &ptSource
: NULL
,
644 pMaskSurf
? ROP4_MASK
: ROP4_FROM_INDEX(R3_OPINDEX_SRCCOPY
));
646 /* Cleanup EXLATEOBJ */
647 EXLATEOBJ_vCleanup(&exlo
);
650 DC_vFinishBlit(pDC
, NULL
);
652 ret
= bResult
? ScanLines
: 0;
656 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
657 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
658 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
659 if (pMaskSurf
) EngUnlockSurface(pMaskSurf
);
660 if (hMaskBitmap
) EngDeleteSurface((HSURF
)hMaskBitmap
);
661 if (pDC
) DC_UnlockDc(pDC
);
662 ExFreePoolWithTag(pbmiSafe
, 'pmTG');
668 /* Converts a device-dependent bitmap to a DIB */
671 GreGetDIBitsInternal(
682 BITMAPCOREINFO
* pbmci
= NULL
;
683 PSURFACE psurf
= NULL
;
693 DPRINT("Entered GreGetDIBitsInternal()\n");
695 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
698 pDC
= DC_LockDc(hDC
);
699 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
705 /* Get a pointer to the source bitmap object */
706 psurf
= SURFACE_ShareLockSurface(hBitmap
);
713 colorPtr
= (LPBYTE
)Info
+ Info
->bmiHeader
.biSize
;
716 bitmap_type
= DIB_GetBitmapInfo(&Info
->bmiHeader
,
723 if(bitmap_type
== -1)
725 DPRINT("Wrong bitmap format\n");
726 EngSetLastError(ERROR_INVALID_PARAMETER
);
730 else if(bitmap_type
== 0)
732 /* We need a BITMAPINFO to create a DIB, but we have to fill
733 * the BITMAPCOREINFO we're provided */
734 pbmci
= (BITMAPCOREINFO
*)Info
;
735 /* fill in the the bit count, so we can calculate the right ColorsSize during the conversion */
736 pbmci
->bmciHeader
.bcBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
737 Info
= DIB_ConvertBitmapInfo((BITMAPINFO
*)pbmci
, Usage
);
740 DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
744 rgbQuads
= Info
->bmiColors
;
748 - negative width is always an invalid value
749 - non-null Bits and zero bpp is an invalid combination
750 - only check the rest of the input params if either bpp is non-zero or Bits are set */
751 if (width
< 0 || (bpp
== 0 && Bits
))
759 if ((height
== 0 || width
== 0) || (compr
&& compr
!= BI_BITFIELDS
&& compr
!= BI_RGB
))
766 Info
->bmiHeader
.biClrUsed
= 0;
767 Info
->bmiHeader
.biClrImportant
= 0;
769 /* Fill in the structure */
772 case 0: /* Only info */
773 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
774 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
775 -psurf
->SurfObj
.sizlBitmap
.cy
:
776 psurf
->SurfObj
.sizlBitmap
.cy
;
777 Info
->bmiHeader
.biPlanes
= 1;
778 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
779 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
780 Info
->bmiHeader
.biHeight
,
781 Info
->bmiHeader
.biBitCount
);
782 Info
->bmiHeader
.biCompression
= (Info
->bmiHeader
.biBitCount
== 16 || Info
->bmiHeader
.biBitCount
== 32) ?
783 BI_BITFIELDS
: BI_RGB
;
784 Info
->bmiHeader
.biXPelsPerMeter
= 0;
785 Info
->bmiHeader
.biYPelsPerMeter
= 0;
787 if (Info
->bmiHeader
.biBitCount
<= 8 && Info
->bmiHeader
.biClrUsed
== 0)
788 Info
->bmiHeader
.biClrUsed
= 1 << Info
->bmiHeader
.biBitCount
;
796 Info
->bmiHeader
.biClrUsed
= 1 << bpp
;
798 /* If the bitmap is a DIB section and has the same format as what
799 * is requested, go ahead! */
800 if((psurf
->hSecure
) &&
801 (BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bpp
))
803 if(Usage
== DIB_RGB_COLORS
)
805 ULONG colors
= min(psurf
->ppal
->NumColors
, 256);
806 if(colors
!= 256) Info
->bmiHeader
.biClrUsed
= colors
;
807 for(i
= 0; i
< colors
; i
++)
809 rgbQuads
[i
].rgbRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
810 rgbQuads
[i
].rgbGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
811 rgbQuads
[i
].rgbBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
812 rgbQuads
[i
].rgbReserved
= 0;
817 for(i
= 0; i
< 256; i
++)
818 ((WORD
*)rgbQuads
)[i
] = i
;
823 if(Usage
== DIB_PAL_COLORS
)
825 for(i
= 0; i
< 256; i
++)
827 ((WORD
*)rgbQuads
)[i
] = i
;
830 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
))
832 /* For color DDBs in native depth (mono DDBs always have
833 a black/white palette):
834 Generate the color map from the selected palette */
835 PPALETTE pDcPal
= PALETTE_ShareLockPalette(pDC
->dclevel
.hpal
);
841 for (i
= 0; i
< pDcPal
->NumColors
; i
++)
843 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
844 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
845 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
846 rgbQuads
[i
].rgbReserved
= 0;
848 PALETTE_ShareUnlockPalette(pDcPal
);
855 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
= rgbQuads
[0].rgbBlue
= 0;
856 rgbQuads
[0].rgbReserved
= 0;
857 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
= rgbQuads
[1].rgbBlue
= 0xff;
858 rgbQuads
[1].rgbReserved
= 0;
862 /* The EGA palette is the first and last 8 colours of the default palette
863 with the innermost pair swapped */
864 RtlCopyMemory(rgbQuads
, DefLogPaletteQuads
, 7 * sizeof(RGBQUAD
));
865 RtlCopyMemory(rgbQuads
+ 7, DefLogPaletteQuads
+ 12, 1 * sizeof(RGBQUAD
));
866 RtlCopyMemory(rgbQuads
+ 8, DefLogPaletteQuads
+ 7, 1 * sizeof(RGBQUAD
));
867 RtlCopyMemory(rgbQuads
+ 9, DefLogPaletteQuads
+ 13, 7 * sizeof(RGBQUAD
));
874 memcpy(rgbQuads
, DefLogPaletteQuads
, 10 * sizeof(RGBQUAD
));
875 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10, 10 * sizeof(RGBQUAD
));
877 for (i
= 10; i
< 246; i
++)
879 rgbQuads
[i
].rgbRed
= (i
& 0x07) << 5;
880 rgbQuads
[i
].rgbGreen
= (i
& 0x38) << 2;
881 rgbQuads
[i
].rgbBlue
= i
& 0xc0;
882 rgbQuads
[i
].rgbReserved
= 0;
891 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
893 ((PDWORD
)Info
->bmiColors
)[0] = 0x7c00;
894 ((PDWORD
)Info
->bmiColors
)[1] = 0x03e0;
895 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
900 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
904 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
905 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
906 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
910 ((PDWORD
)Info
->bmiColors
)[0] = 0xf800;
911 ((PDWORD
)Info
->bmiColors
)[1] = 0x07e0;
912 ((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] = 0xff0000;
930 ((PDWORD
)Info
->bmiColors
)[1] = 0x00ff00;
931 ((PDWORD
)Info
->bmiColors
)[2] = 0x0000ff;
941 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(width
, height
, bpp
);
942 Info
->bmiHeader
.biPlanes
= 1;
944 if(Bits
&& ScanLines
)
946 /* Create a DIBSECTION, blt it, profit */
955 if (StartScan
> (ULONG
)psurf
->SurfObj
.sizlBitmap
.cy
)
962 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
966 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
967 Info
->bmiHeader
.biHeight
= (height
< 0) ?
968 -(LONG
)ScanLines
: ScanLines
;
970 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
972 Info
->bmiHeader
.biWidth
= width
;
973 Info
->bmiHeader
.biHeight
= height
;
977 DPRINT1("Unable to create a DIB Section!\n");
978 EngSetLastError(ERROR_INVALID_PARAMETER
);
983 psurfDest
= SURFACE_ShareLockSurface(hBmpDest
);
985 RECTL_vSetRect(&rcDest
, 0, 0, psurf
->SurfObj
.sizlBitmap
.cx
, ScanLines
);
993 if(ScanLines
<= StartScan
)
996 SURFACE_ShareUnlockSurface(psurfDest
);
997 GreDeleteObject(hBmpDest
);
1001 ScanLines
-= StartScan
;
1005 srcPoint
.y
= StartScan
;
1008 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xffffff, 0xffffff, 0);
1010 ret
= IntEngCopyBits(&psurfDest
->SurfObj
,
1017 SURFACE_ShareUnlockSurface(psurfDest
);
1023 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, ScanLines
, bpp
));
1026 GreDeleteObject(hBmpDest
);
1027 EXLATEOBJ_vCleanup(&exlo
);
1031 /* Signals success and not the actual number of scan lines*/
1038 DIB_FreeConvertedBitmapInfo(Info
, (BITMAPINFO
*)pbmci
, Usage
);
1041 SURFACE_ShareUnlockSurface(psurf
);
1049 _Success_(return!=0)
1053 NtGdiGetDIBitsInternal(
1056 _In_ UINT iStartScan
,
1058 _Out_writes_bytes_opt_(cjMaxBits
) LPBYTE pjBits
,
1059 _Inout_ LPBITMAPINFO pbmi
,
1061 _In_ UINT cjMaxBits
,
1062 _In_ UINT cjMaxInfo
)
1064 PBITMAPINFO pbmiSafe
;
1065 HANDLE hSecure
= NULL
;
1069 /* Check for bad iUsage */
1070 if (iUsage
> 2) return 0;
1072 /* Check if the size of the bitmap info is large enough */
1073 if (cjMaxInfo
< sizeof(BITMAPCOREHEADER
))
1078 /* Use maximum size */
1079 cjMaxInfo
= min(cjMaxInfo
, sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
));
1081 // HACK: the underlying code sucks and doesn't care for the size, so we
1082 // give it the maximum ever needed
1083 cjAlloc
= sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
);
1085 /* Allocate a buffer the bitmapinfo */
1086 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjAlloc
, 'imBG');
1096 /* Probe and copy the BITMAPINFO */
1097 ProbeForRead(pbmi
, cjMaxInfo
, 1);
1098 RtlCopyMemory(pbmiSafe
, pbmi
, cjMaxInfo
);
1100 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1102 _SEH2_YIELD(goto cleanup
;)
1106 /* Check if the header size is large enough */
1107 if ((pbmiSafe
->bmiHeader
.biSize
< sizeof(BITMAPCOREHEADER
)) ||
1108 (pbmiSafe
->bmiHeader
.biSize
> cjMaxInfo
))
1113 /* Check if the caller provided bitmap bits */
1116 /* Secure the user mode memory */
1117 hSecure
= EngSecureMem(pjBits
, cjMaxBits
);
1124 /* Now call the internal function */
1125 iResult
= GreGetDIBitsInternal(hdc
,
1135 /* Check for success */
1138 /* Use SEH to copy back to user mode */
1141 /* Copy the data back */
1142 cjMaxInfo
= min(cjMaxInfo
, (UINT
)DIB_BitmapInfoSize(pbmiSafe
, (WORD
)iUsage
));
1143 ProbeForWrite(pbmi
, cjMaxInfo
, 1);
1144 RtlCopyMemory(pbmi
, pbmiSafe
, cjMaxInfo
);
1146 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1155 if (hSecure
) EngUnsecureMem(hSecure
);
1156 ExFreePoolWithTag(pbmiSafe
, 'imBG');
1165 NtGdiStretchDIBitsInternal(
1175 IN OPTIONAL LPBYTE pjInit
,
1176 IN LPBITMAPINFO pbmi
,
1178 IN DWORD dwRop
, // MS ntgdi.h says dwRop4(?)
1183 BOOL bResult
= FALSE
;
1188 PSURFACE psurfTmp
= 0, psurfDst
= 0;
1189 PPALETTE ppalDIB
= 0;
1193 if (!(pdc
= DC_LockDc(hdc
)))
1195 EngSetLastError(ERROR_INVALID_HANDLE
);
1199 /* Check for info / mem DC without surface */
1200 if (!pdc
->dclevel
.pSurface
)
1207 /* Transform dest size */
1210 IntLPtoDP(pdc
, (POINTL
*)&sizel
, 1);
1213 /* Check if we can use NtGdiSetDIBitsToDeviceInternal */
1214 if ((sizel
.cx
== cxSrc
) && (sizel
.cy
== cySrc
) && (dwRop
== SRCCOPY
))
1217 return NtGdiSetDIBitsToDeviceInternal(hdc
,
1235 if (pjInit
&& (cjMaxBits
> 0))
1237 pvBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, 'pmeT');
1245 ProbeForRead(pjInit
, cjMaxBits
, 1);
1246 RtlCopyMemory(pvBits
, pjInit
, cjMaxBits
);
1248 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1250 ExFreePoolWithTag(pvBits
, 'pmeT');
1251 _SEH2_YIELD(return 0);
1260 /* FIXME: Locking twice is cheesy, coord tranlation in UM will fix it */
1261 if (!(pdc
= DC_LockDc(hdc
)))
1263 DPRINT1("Could not lock dc\n");
1264 EngSetLastError(ERROR_INVALID_HANDLE
);
1268 /* Calculate source and destination rect */
1271 rcSrc
.right
= xSrc
+ abs(cxSrc
);
1272 rcSrc
.bottom
= ySrc
+ abs(cySrc
);
1275 rcDst
.right
= rcDst
.left
+ cxDst
;
1276 rcDst
.bottom
= rcDst
.top
+ cyDst
;
1277 IntLPtoDP(pdc
, (POINTL
*)&rcDst
, 2);
1278 RECTL_vOffsetRect(&rcDst
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1280 if (pdc
->fs
& (DC_ACCUM_APP
|DC_ACCUM_WMGR
))
1282 IntUpdateBoundsRect(pdc
, &rcDst
);
1285 hbmTmp
= GreCreateBitmapEx(pbmi
->bmiHeader
.biWidth
,
1286 abs(pbmi
->bmiHeader
.biHeight
),
1288 BitmapFormat(pbmi
->bmiHeader
.biBitCount
,
1289 pbmi
->bmiHeader
.biCompression
),
1290 pbmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
1291 pbmi
->bmiHeader
.biSizeImage
,
1301 psurfTmp
= SURFACE_ShareLockSurface(hbmTmp
);
1308 /* Create a palette for the DIB */
1309 ppalDIB
= CreateDIBPalette(pbmi
, pdc
, dwUsage
);
1316 /* Prepare DC for blit */
1317 DC_vPrepareDCsForBlit(pdc
, &rcDst
, NULL
, NULL
);
1319 psurfDst
= pdc
->dclevel
.pSurface
;
1321 /* Initialize XLATEOBJ */
1322 EXLATEOBJ_vInitialize(&exlo
,
1325 RGB(0xff, 0xff, 0xff),
1326 pdc
->pdcattr
->crBackgroundClr
,
1327 pdc
->pdcattr
->crForegroundClr
);
1329 /* Perform the stretch operation */
1330 bResult
= IntEngStretchBlt(&psurfDst
->SurfObj
,
1333 (CLIPOBJ
*)&pdc
->co
,
1339 &pdc
->eboFill
.BrushObject
,
1341 WIN32_ROP3_TO_ENG_ROP4(dwRop
));
1344 DC_vFinishBlit(pdc
, NULL
);
1345 EXLATEOBJ_vCleanup(&exlo
);
1347 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
1348 if (psurfTmp
) SURFACE_ShareUnlockSurface(psurfTmp
);
1349 if (hbmTmp
) GreDeleteObject(hbmTmp
);
1350 if (pdc
) DC_UnlockDc(pdc
);
1351 if (pvBits
) ExFreePoolWithTag(pvBits
, 'pmeT');
1374 ULONG BmpFormat
= 0;
1377 BmpFormat
= BitmapFormat(planes
* bpp
, compression
);
1379 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1380 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1382 if (BmpFormat
!= BMF_1BPP
) fColor
= TRUE
;
1383 else if ((coloruse
> DIB_RGB_COLORS
) || ((init
& CBM_INIT
) == 0) || !data
) fColor
= FALSE
;
1386 const RGBQUAD
*rgb
= (RGBQUAD
*)((PBYTE
)data
+ data
->bmiHeader
.biSize
);
1387 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1389 // Check if the first color of the colormap is black
1390 if (col
== RGB(0, 0, 0))
1393 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1395 // If the second color is white, create a monochrome bitmap
1396 fColor
= (col
!= RGB(0xff,0xff,0xff));
1401 // Now create the bitmap
1404 if (init
& CBM_CREATDIB
)
1409 /* Undocumented flag which creates a DDB of the format specified by the bitmap info. */
1410 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
, planes
, bpp
);
1413 DPRINT1("IntCreateCompatibleBitmap() failed!\n");
1417 /* The palette must also match the given data */
1418 Surface
= SURFACE_ShareLockSurface(handle
);
1420 Palette
= CreateDIBPalette(data
, Dc
, coloruse
);
1422 SURFACE_vSetPalette(Surface
, Palette
);
1424 PALETTE_ShareUnlockPalette(Palette
);
1425 SURFACE_ShareUnlockSurface(Surface
);
1429 /* Create a regular compatible bitmap, in the same format as the device */
1430 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
, 0, 0);
1435 handle
= GreCreateBitmap(width
,
1445 if ((NULL
!= handle
) && (CBM_INIT
& init
))
1447 IntSetDIBits(Dc
, handle
, 0, height
, bits
, cjMaxBits
, data
, coloruse
);
1453 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1454 // The DDB that is created will be whatever bit depth your reference DC is
1457 NtGdiCreateDIBitmapInternal(
1462 IN OPTIONAL LPBYTE pjInit
,
1463 IN OPTIONAL LPBITMAPINFO pbmi
,
1465 IN UINT cjMaxInitInfo
,
1470 NTSTATUS Status
= STATUS_SUCCESS
;
1471 PBYTE safeBits
= NULL
;
1472 HBITMAP hbmResult
= NULL
;
1479 if(pjInit
&& (fInit
& CBM_INIT
))
1481 if (cjMaxBits
== 0) return NULL
;
1482 safeBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, TAG_DIB
);
1485 DPRINT1("Failed to allocate %lu bytes\n", cjMaxBits
);
1486 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1493 if(pbmi
) ProbeForRead(pbmi
, cjMaxInitInfo
, 1);
1494 if(pjInit
&& (fInit
& CBM_INIT
))
1496 ProbeForRead(pjInit
, cjMaxBits
, 1);
1497 RtlCopyMemory(safeBits
, pjInit
, cjMaxBits
);
1500 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1502 Status
= _SEH2_GetExceptionCode();
1506 if(!NT_SUCCESS(Status
))
1508 DPRINT1("Got an exception! pjInit = %p\n", pjInit
);
1509 SetLastNtError(Status
);
1513 hbmResult
= GreCreateDIBitmapInternal(hDc
,
1525 if (safeBits
) ExFreePoolWithTag(safeBits
, TAG_DIB
);
1531 GreCreateDIBitmapInternal(
1536 IN OPTIONAL LPBYTE pjInit
,
1537 IN OPTIONAL PBITMAPINFO pbmi
,
1549 if (!hDc
) /* 1bpp monochrome bitmap */
1551 // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1552 hdcDest
= NtGdiCreateCompatibleDC(0);
1555 DPRINT1("NtGdiCreateCompatibleDC failed\n");
1564 Dc
= DC_LockDc(hdcDest
);
1567 DPRINT1("Failed to lock hdcDest %p\n", hdcDest
);
1568 EngSetLastError(ERROR_INVALID_HANDLE
);
1571 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1572 * if bpp != 1 and ignore the real value that was passed */
1575 if (pbmi
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1577 BITMAPCOREHEADER
* CoreHeader
= (BITMAPCOREHEADER
*)&pbmi
->bmiHeader
;
1578 bpp
= CoreHeader
->bcBitCount
;
1579 planes
= CoreHeader
->bcPlanes
? CoreHeader
->bcPlanes
: 1;
1580 compression
= BI_RGB
;
1584 bpp
= pbmi
->bmiHeader
.biBitCount
;
1585 planes
= pbmi
->bmiHeader
.biPlanes
? pbmi
->bmiHeader
.biPlanes
: 1;
1586 compression
= pbmi
->bmiHeader
.biCompression
;
1595 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, planes
, bpp
, compression
, fInit
, pjInit
, cjMaxBits
, pbmi
, iUsage
);
1600 NtGdiDeleteObjectApp(hdcDest
);
1607 GreCreateDIBitmapFromPackedDIB(
1608 _In_reads_(cjPackedDIB
)PVOID pvPackedDIB
,
1609 _In_ UINT cjPackedDIB
,
1614 UINT cjInfo
, cjBits
;
1617 /* We only support BITMAPINFOHEADER, make sure the size is ok */
1618 if (cjPackedDIB
< sizeof(BITMAPINFOHEADER
))
1623 /* The packed DIB starts with the BITMAPINFOHEADER */
1626 if (cjPackedDIB
< pbmi
->bmiHeader
.biSize
)
1631 /* Calculate the info size and make sure the packed DIB is large enough */
1632 cjInfo
= DIB_BitmapInfoSize(pbmi
, uUsage
);
1633 if (cjPackedDIB
<= cjInfo
)
1638 /* The bitmap bits start after the header */
1639 pjBits
= (PBYTE
)pvPackedDIB
+ cjInfo
;
1640 cjBits
= cjPackedDIB
- cjInfo
;
1642 hbm
= GreCreateDIBitmapInternal(NULL
,
1643 pbmi
->bmiHeader
.biWidth
,
1644 abs(pbmi
->bmiHeader
.biHeight
),
1645 CBM_INIT
| CBM_CREATDIB
,
1658 NtGdiCreateDIBSection(
1660 IN OPTIONAL HANDLE hSection
,
1666 IN ULONG_PTR dwColorSpace
,
1669 HBITMAP hbitmap
= 0;
1671 BOOL bDesktopDC
= FALSE
;
1672 NTSTATUS Status
= STATUS_SUCCESS
;
1674 if (!bmi
) return hbitmap
; // Make sure.
1678 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1679 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1680 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, (WORD
)Usage
), 1);
1682 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1684 Status
= _SEH2_GetExceptionCode();
1688 if(!NT_SUCCESS(Status
))
1690 SetLastNtError(Status
);
1694 // If the reference hdc is null, take the desktop dc
1697 hDC
= NtGdiCreateCompatibleDC(0);
1701 if ((dc
= DC_LockDc(hDC
)))
1703 hbitmap
= DIB_CreateDIBSection(dc
,
1714 EngSetLastError(ERROR_INVALID_HANDLE
);
1718 NtGdiDeleteObjectApp(hDC
);
1725 DIB_CreateDIBSection(
1727 CONST BITMAPINFO
*bmi
,
1735 SURFACE
*bmp
= NULL
;
1736 void *mapBits
= NULL
;
1737 PPALETTE ppalDIB
= NULL
;
1739 // Fill BITMAP32 structure with DIB data
1740 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1747 DPRINT("format (%ld,%ld), planes %u, bpp %u, size %lu, colors %lu (%s)\n",
1748 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1749 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1751 /* CreateDIBSection should fail for compressed formats */
1752 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1754 DPRINT1("no compressed format allowed\n");
1755 return (HBITMAP
)NULL
;
1758 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1760 bm
.bmWidth
= bi
->biWidth
;
1761 bm
.bmHeight
= effHeight
;
1762 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: WIDTH_BYTES_ALIGN32(bm
.bmWidth
, bi
->biBitCount
);
1764 bm
.bmPlanes
= bi
->biPlanes
;
1765 bm
.bmBitsPixel
= bi
->biBitCount
;
1768 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1769 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1770 totalSize
= (bi
->biSizeImage
&& (bi
->biCompression
!= BI_RGB
) && (bi
->biCompression
!= BI_BITFIELDS
))
1771 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1775 SYSTEM_BASIC_INFORMATION Sbi
;
1778 LARGE_INTEGER SectionOffset
;
1781 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1785 if (!NT_SUCCESS(Status
))
1787 DPRINT1("ZwQuerySystemInformation failed (0x%lx)\n", Status
);
1791 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1792 mapSize
= totalSize
+ (offset
- mapOffset
);
1794 SectionOffset
.LowPart
= mapOffset
;
1795 SectionOffset
.HighPart
= 0;
1797 Status
= ZwMapViewOfSection(section
,
1807 if (!NT_SUCCESS(Status
))
1809 DPRINT1("ZwMapViewOfSection failed (0x%lx)\n", Status
);
1810 EngSetLastError(ERROR_INVALID_PARAMETER
);
1814 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1816 else if (ovr_pitch
&& offset
)
1817 bm
.bmBits
= UlongToPtr(offset
);
1821 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1824 DPRINT1("Failed to allocate memory\n");
1829 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1830 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1833 // Create Device Dependent Bitmap and add DIB pointer
1834 //Size.cx = bm.bmWidth;
1835 //Size.cy = abs(bm.bmHeight);
1836 res
= GreCreateBitmapEx(bm
.bmWidth
,
1839 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1840 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1841 ((bi
->biHeight
< 0) ? BMF_TOPDOWN
: 0),
1847 DPRINT1("GreCreateBitmapEx failed\n");
1848 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
1851 bmp
= SURFACE_ShareLockSurface(res
); // HACK
1854 DPRINT1("SURFACE_LockSurface failed\n");
1855 EngSetLastError(ERROR_INVALID_HANDLE
);
1859 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1860 table between the DIB and the X physical device. Obviously,
1861 this is left out of the ReactOS implementation. Instead,
1862 we call NtGdiSetDIBColorTable. */
1863 bmp
->hDIBSection
= section
;
1864 bmp
->hSecure
= hSecure
;
1865 bmp
->dwOffset
= offset
;
1866 bmp
->flags
= API_BITMAP
;
1867 bmp
->biClrImportant
= bi
->biClrImportant
;
1869 /* Create a palette for the DIB */
1870 ppalDIB
= CreateDIBPalette(bmi
, dc
, usage
);
1872 // Clean up in case of errors
1874 if (!res
|| !bmp
|| !bm
.bmBits
|| !ppalDIB
)
1876 DPRINT("Got an error res=%p, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1879 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1882 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1886 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1891 SURFACE_ShareUnlockSurface(bmp
);
1897 GreDeleteObject(res
);
1903 PALETTE_ShareUnlockPalette(ppalDIB
);
1909 /* If we're here, everything went fine */
1910 SURFACE_vSetPalette(bmp
, ppalDIB
);
1911 PALETTE_ShareUnlockPalette(ppalDIB
);
1912 SURFACE_ShareUnlockSurface(bmp
);
1915 // Return BITMAP handle and storage location
1916 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1924 /***********************************************************************
1927 * Get the info from a bitmap header.
1928 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
1932 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
1933 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
1935 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1937 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
1938 *width
= core
->bcWidth
;
1939 *height
= core
->bcHeight
;
1940 *planes
= core
->bcPlanes
;
1941 *bpp
= core
->bcBitCount
;
1946 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* Assume BITMAPINFOHEADER */
1948 *width
= header
->biWidth
;
1949 *height
= header
->biHeight
;
1950 *planes
= header
->biPlanes
;
1951 *bpp
= header
->biBitCount
;
1952 *compr
= header
->biCompression
;
1953 *size
= header
->biSizeImage
;
1956 DPRINT1("(%u): unknown/wrong size for header\n", header
->biSize
);
1960 /***********************************************************************
1961 * DIB_GetDIBImageBytes
1963 * Return the number of bytes used to hold the image in a DIB bitmap.
1964 * 11/16/1999 (RJJ) lifted from wine
1967 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1969 return WIDTH_BYTES_ALIGN32(width
, depth
) * (height
< 0 ? -height
: height
);
1972 /***********************************************************************
1973 * DIB_BitmapInfoSize
1975 * Return the size of the bitmap info structure including color table.
1976 * 11/16/1999 (RJJ) lifted from wine
1979 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1981 unsigned int colors
, size
, masks
= 0;
1982 unsigned int colorsize
;
1984 colorsize
= (coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) :
1985 (coloruse
== DIB_PAL_INDICES
) ? 0 :
1988 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1990 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
1991 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1992 return sizeof(BITMAPCOREHEADER
) + colors
* colorsize
;
1994 else /* Assume BITMAPINFOHEADER */
1996 colors
= info
->bmiHeader
.biClrUsed
;
1997 if (colors
> 256) colors
= 256;
1998 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
1999 colors
= 1 << info
->bmiHeader
.biBitCount
;
2000 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
2001 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
2002 return size
+ colors
* colorsize
;
2008 DIB_MapPaletteColors(PPALETTE ppalDc
, CONST BITMAPINFO
* lpbmi
)
2015 if (!(ppalDc
->flFlags
& PAL_INDEXED
))
2020 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
2021 if (lpbmi
->bmiHeader
.biClrUsed
)
2023 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
2026 ppalNew
= PALETTE_AllocPalWithHandle(PAL_INDEXED
, nNumColors
, NULL
, 0, 0, 0);
2027 if (ppalNew
== NULL
)
2029 DPRINT1("Could not allocate palette\n");
2033 lpIndex
= (USHORT
*)((PBYTE
)lpbmi
+ lpbmi
->bmiHeader
.biSize
);
2035 for (i
= 0; i
< nNumColors
; i
++)
2037 ULONG iColorIndex
= *lpIndex
% ppalDc
->NumColors
;
2038 ppalNew
->IndexedColors
[i
] = ppalDc
->IndexedColors
[iColorIndex
];
2042 hpal
= ppalNew
->BaseObject
.hHmgr
;
2043 PALETTE_UnlockPalette(ppalNew
);
2048 /* Converts a BITMAPCOREINFO to a BITMAPINFO structure,
2049 * or does nothing if it's already a BITMAPINFO (or V4 or V5) */
2052 DIB_ConvertBitmapInfo (CONST BITMAPINFO
* pbmi
, DWORD Usage
)
2054 CONST BITMAPCOREINFO
* pbmci
= (BITMAPCOREINFO
*)pbmi
;
2055 BITMAPINFO
* pNewBmi
;
2056 UINT numColors
= 0, ColorsSize
= 0;
2058 if(pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) return (BITMAPINFO
*)pbmi
;
2059 if(pbmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) return NULL
;
2061 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
2063 numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
2064 if(Usage
== DIB_PAL_COLORS
)
2066 ColorsSize
= numColors
* sizeof(WORD
);
2070 ColorsSize
= numColors
* sizeof(RGBQUAD
);
2073 else if (Usage
== DIB_PAL_COLORS
)
2075 /* Invalid at high-res */
2079 pNewBmi
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + ColorsSize
, TAG_DIB
);
2080 if(!pNewBmi
) return NULL
;
2082 RtlZeroMemory(pNewBmi
, sizeof(BITMAPINFOHEADER
) + ColorsSize
);
2084 pNewBmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
2085 pNewBmi
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
2086 pNewBmi
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
2087 pNewBmi
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
2088 pNewBmi
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
2089 pNewBmi
->bmiHeader
.biCompression
= BI_RGB
;
2090 pNewBmi
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(pNewBmi
->bmiHeader
.biWidth
,
2091 pNewBmi
->bmiHeader
.biHeight
,
2092 pNewBmi
->bmiHeader
.biBitCount
);
2093 pNewBmi
->bmiHeader
.biClrUsed
= numColors
;
2095 if(Usage
== DIB_PAL_COLORS
)
2097 RtlCopyMemory(pNewBmi
->bmiColors
, pbmci
->bmciColors
, ColorsSize
);
2102 for(i
=0; i
<numColors
; i
++)
2104 pNewBmi
->bmiColors
[i
].rgbRed
= pbmci
->bmciColors
[i
].rgbtRed
;
2105 pNewBmi
->bmiColors
[i
].rgbGreen
= pbmci
->bmciColors
[i
].rgbtGreen
;
2106 pNewBmi
->bmiColors
[i
].rgbBlue
= pbmci
->bmciColors
[i
].rgbtBlue
;
2113 /* Frees a BITMAPINFO created with DIB_ConvertBitmapInfo */
2116 DIB_FreeConvertedBitmapInfo(BITMAPINFO
* converted
, BITMAPINFO
* orig
, DWORD usage
)
2118 BITMAPCOREINFO
* pbmci
;
2119 if(converted
== orig
)
2124 /* Caller don't want any conversion */
2125 ExFreePoolWithTag(converted
, TAG_DIB
);
2129 /* Perform inverse conversion */
2130 pbmci
= (BITMAPCOREINFO
*)orig
;
2132 ASSERT(pbmci
->bmciHeader
.bcSize
== sizeof(BITMAPCOREHEADER
));
2133 pbmci
->bmciHeader
.bcBitCount
= converted
->bmiHeader
.biBitCount
;
2134 pbmci
->bmciHeader
.bcWidth
= converted
->bmiHeader
.biWidth
;
2135 pbmci
->bmciHeader
.bcHeight
= converted
->bmiHeader
.biHeight
;
2136 pbmci
->bmciHeader
.bcPlanes
= converted
->bmiHeader
.biPlanes
;
2138 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
2140 UINT numColors
= converted
->bmiHeader
.biClrUsed
;
2141 if(!numColors
) numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
2142 if(usage
== DIB_PAL_COLORS
)
2144 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(WORD
));
2145 RtlCopyMemory(pbmci
->bmciColors
, converted
->bmiColors
, numColors
* sizeof(WORD
));
2150 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(RGBTRIPLE
));
2151 for(i
=0; i
<numColors
; i
++)
2153 pbmci
->bmciColors
[i
].rgbtRed
= converted
->bmiColors
[i
].rgbRed
;
2154 pbmci
->bmciColors
[i
].rgbtGreen
= converted
->bmiColors
[i
].rgbGreen
;
2155 pbmci
->bmciColors
[i
].rgbtBlue
= converted
->bmiColors
[i
].rgbBlue
;
2159 /* Now free it, it's not needed anymore */
2160 ExFreePoolWithTag(converted
, TAG_DIB
);