2 * PROJECT: ReactOS win32 kernel mode subsystem
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: subsystems/win32/win32k/objects/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 RGBTRIPLE EGAColorsTriples
[16] =
37 /* rgbBlue, rgbGreen, rgbRed */
56 static const RGBQUAD DefLogPaletteQuads
[20] = /* Copy of Default Logical Palette */
58 /* rgbBlue, rgbGreen, rgbRed, rgbReserved */
59 { 0x00, 0x00, 0x00, 0x00 },
60 { 0x00, 0x00, 0x80, 0x00 },
61 { 0x00, 0x80, 0x00, 0x00 },
62 { 0x00, 0x80, 0x80, 0x00 },
63 { 0x80, 0x00, 0x00, 0x00 },
64 { 0x80, 0x00, 0x80, 0x00 },
65 { 0x80, 0x80, 0x00, 0x00 },
66 { 0xc0, 0xc0, 0xc0, 0x00 },
67 { 0xc0, 0xdc, 0xc0, 0x00 },
68 { 0xf0, 0xca, 0xa6, 0x00 },
69 { 0xf0, 0xfb, 0xff, 0x00 },
70 { 0xa4, 0xa0, 0xa0, 0x00 },
71 { 0x80, 0x80, 0x80, 0x00 },
72 { 0x00, 0x00, 0xf0, 0x00 },
73 { 0x00, 0xff, 0x00, 0x00 },
74 { 0x00, 0xff, 0xff, 0x00 },
75 { 0xff, 0x00, 0x00, 0x00 },
76 { 0xff, 0x00, 0xff, 0x00 },
77 { 0xff, 0xff, 0x00, 0x00 },
78 { 0xff, 0xff, 0xff, 0x00 }
81 static const RGBQUAD DefLogPaletteTriples
[20] = /* Copy of Default Logical Palette */
83 /* rgbBlue, rgbGreen, rgbRed, rgbReserved */
100 { 0xff, 0x00, 0x00 },
101 { 0xff, 0x00, 0xff },
102 { 0xff, 0xff, 0x00 },
109 _In_
const BITMAPINFO
*pbmi
,
114 ULONG i
, cBitsPixel
, cColors
;
116 if (pbmi
->bmiHeader
.biSize
< sizeof(BITMAPINFOHEADER
))
118 PBITMAPCOREINFO pbci
= (PBITMAPCOREINFO
)pbmi
;
119 cBitsPixel
= pbci
->bmciHeader
.bcBitCount
;
123 cBitsPixel
= pbmi
->bmiHeader
.biBitCount
;
126 /* Check if the colors are indexed */
129 /* We create a "full" palette */
130 cColors
= 1 << cBitsPixel
;
132 /* Allocate the palette */
133 ppal
= PALETTE_AllocPalette(PAL_INDEXED
,
140 /* Check if the BITMAPINFO specifies how many colors to use */
141 if ((pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) &&
142 (pbmi
->bmiHeader
.biClrUsed
!= 0))
144 /* This is how many colors we can actually process */
145 cColors
= min(cColors
, pbmi
->bmiHeader
.biClrUsed
);
148 /* Check how to use the colors */
149 if (iUsage
== DIB_PAL_COLORS
)
153 /* The colors are an array of WORD indices into the DC palette */
154 PWORD pwColors
= (PWORD
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
156 /* Use the DCs palette or, if no DC is given, the default one */
157 PPALETTE ppalDC
= pdc
? pdc
->dclevel
.ppal
: gppalDefault
;
159 /* Loop all color indices in the DIB */
160 for (i
= 0; i
< cColors
; i
++)
162 /* Get the palette index and handle wraparound when exceeding
163 the number of colors in the DC palette */
164 WORD wIndex
= pwColors
[i
] % ppalDC
->NumColors
;
166 /* USe the RGB value from the DC palette */
167 crColor
= PALETTE_ulGetRGBColorFromIndex(ppalDC
, wIndex
);
168 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
171 else if (iUsage
== DIB_PAL_BRUSHHACK
)
173 /* The colors are an array of WORD indices into the DC palette */
174 PWORD pwColors
= (PWORD
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
176 /* Loop all color indices in the DIB */
177 for (i
= 0; i
< cColors
; i
++)
179 /* Set the index directly as the RGB color, the real palette
180 containing RGB values will be calculated when the brush is
182 PALETTE_vSetRGBColorForIndex(ppal
, i
, pwColors
[i
]);
185 /* Mark the palette as a brush hack palette */
186 ppal
->flFlags
|= PAL_BRUSHHACK
;
188 // else if (iUsage == 2)
190 // FIXME: this one is undocumented
193 else if (pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
))
195 /* The colors are an array of RGBQUAD values */
196 RGBQUAD
*prgb
= (RGBQUAD
*)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
198 // FIXME: do we need to handle PALETTEINDEX / PALETTERGB macro?
200 /* Loop all color indices in the DIB */
201 for (i
= 0; i
< cColors
; i
++)
203 /* Get the color value and translate it to a COLORREF */
204 RGBQUAD rgb
= prgb
[i
];
205 COLORREF crColor
= RGB(rgb
.rgbRed
, rgb
.rgbGreen
, rgb
.rgbBlue
);
207 /* Set the RGB value in the palette */
208 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
213 /* The colors are an array of RGBTRIPLE values */
214 RGBTRIPLE
*prgb
= (RGBTRIPLE
*)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
216 /* Loop all color indices in the DIB */
217 for (i
= 0; i
< cColors
; i
++)
219 /* Get the color value and translate it to a COLORREF */
220 RGBTRIPLE rgb
= prgb
[i
];
221 COLORREF crColor
= RGB(rgb
.rgbtRed
, rgb
.rgbtGreen
, rgb
.rgbtBlue
);
223 /* Set the RGB value in the palette */
224 PALETTE_vSetRGBColorForIndex(ppal
, i
, crColor
);
230 /* This is a bitfield / RGB palette */
231 ULONG flRedMask
, flGreenMask
, flBlueMask
;
233 /* Check if the DIB contains bitfield values */
234 if ((pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) &&
235 (pbmi
->bmiHeader
.biCompression
== BI_BITFIELDS
))
237 /* Check if we have a v4/v5 header */
238 if (pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPV4HEADER
))
240 /* The masks are dedicated fields in the header */
241 PBITMAPV4HEADER pbmV4Header
= (PBITMAPV4HEADER
)&pbmi
->bmiHeader
;
242 flRedMask
= pbmV4Header
->bV4RedMask
;
243 flGreenMask
= pbmV4Header
->bV4GreenMask
;
244 flBlueMask
= pbmV4Header
->bV4BlueMask
;
248 /* The masks are the first 3 values in the DIB color table */
249 PDWORD pdwColors
= (PVOID
)((PCHAR
)pbmi
+ pbmi
->bmiHeader
.biSize
);
250 flRedMask
= pdwColors
[0];
251 flGreenMask
= pdwColors
[1];
252 flBlueMask
= pdwColors
[2];
257 /* Check what bit depth we have. Note: optimization flags are
258 calculated in PALETTE_AllocPalette() */
259 if (cBitsPixel
== 16)
261 /* This is an RGB 555 palette */
263 flGreenMask
= 0x03E0;
268 /* This is an RGB 888 palette */
269 flRedMask
= 0xFF0000;
270 flGreenMask
= 0x00FF00;
271 flBlueMask
= 0x0000FF;
275 /* Allocate the bitfield palette */
276 ppal
= PALETTE_AllocPalette(PAL_BITFIELDS
,
284 /* We're done, return the palette */
288 // Converts a DIB to a device-dependent bitmap
297 CONST BITMAPINFO
*bmi
,
300 HBITMAP SourceBitmap
;
301 PSURFACE psurfDst
, psurfSrc
;
306 PPALETTE ppalDIB
= 0;
310 SourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
313 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
314 bmi
->bmiHeader
.biCompression
),
315 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
316 bmi
->bmiHeader
.biSizeImage
,
321 DPRINT1("Error: Could not create a bitmap.\n");
322 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
326 psurfDst
= SURFACE_ShareLockSurface(hBitmap
);
327 psurfSrc
= SURFACE_ShareLockSurface(SourceBitmap
);
329 if(!(psurfSrc
&& psurfDst
))
331 DPRINT1("Error: Could not lock surfaces\n");
335 /* Create a palette for the DIB */
336 ppalDIB
= CreateDIBPalette(bmi
, DC
, ColorUse
);
339 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
343 /* Initialize EXLATEOBJ */
344 EXLATEOBJ_vInitialize(&exlo
,
347 RGB(0xff, 0xff, 0xff),
348 RGB(0xff, 0xff, 0xff), //DC->pdcattr->crBackgroundClr,
349 0); // DC->pdcattr->crForegroundClr);
351 rcDst
.top
= StartScan
;
353 rcDst
.bottom
= rcDst
.top
+ ScanLines
;
354 rcDst
.right
= psurfDst
->SurfObj
.sizlBitmap
.cx
;
358 result
= IntEngCopyBits(&psurfDst
->SurfObj
,
367 EXLATEOBJ_vCleanup(&exlo
);
370 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
371 if(psurfSrc
) SURFACE_ShareUnlockSurface(psurfSrc
);
372 if(psurfDst
) SURFACE_ShareUnlockSurface(psurfDst
);
373 GreDeleteObject(SourceBitmap
);
381 NtGdiSetDIBitsToDeviceInternal(
396 IN BOOL bTransformCoordinates
,
397 IN OPTIONAL HANDLE hcmXform
)
400 NTSTATUS Status
= STATUS_SUCCESS
;
402 HBITMAP hSourceBitmap
= NULL
;
403 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
;
410 PPALETTE ppalDIB
= NULL
;
411 LPBITMAPINFO pbmiSafe
;
415 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjMaxInfo
, 'pmTG');
416 if (!pbmiSafe
) return 0;
420 ProbeForRead(bmi
, cjMaxInfo
, 1);
421 ProbeForRead(Bits
, cjMaxBits
, 1);
422 RtlCopyMemory(pbmiSafe
, bmi
, cjMaxInfo
);
425 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
427 Status
= _SEH2_GetExceptionCode();
431 if (!NT_SUCCESS(Status
))
436 ScanLines
= min(ScanLines
, abs(bmi
->bmiHeader
.biHeight
) - StartScan
);
438 pDC
= DC_LockDc(hDC
);
441 EngSetLastError(ERROR_INVALID_HANDLE
);
445 if (pDC
->dctype
== DC_TYPE_INFO
)
453 if (bTransformCoordinates
)
455 IntLPtoDP(pDC
, (LPPOINT
)&rcDest
, 2);
457 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
458 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
459 rcDest
.right
= rcDest
.left
+ Width
;
460 rcDest
.bottom
= rcDest
.top
+ Height
;
461 rcDest
.top
+= StartScan
;
466 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
467 SourceSize
.cy
= ScanLines
;
469 //DIBWidth = WIDTH_BYTES_ALIGN32(SourceSize.cx, bmi->bmiHeader.biBitCount);
471 hSourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
474 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
475 bmi
->bmiHeader
.biCompression
),
476 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
477 bmi
->bmiHeader
.biSizeImage
,
483 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
484 Status
= STATUS_NO_MEMORY
;
488 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
491 Status
= STATUS_UNSUCCESSFUL
;
495 /* Create a palette for the DIB */
496 ppalDIB
= CreateDIBPalette(bmi
, pDC
, ColorUse
);
499 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
500 Status
= STATUS_NO_MEMORY
;
504 /* This is actually a blit */
505 DC_vPrepareDCsForBlit(pDC
, rcDest
, NULL
, rcDest
);
506 pSurf
= pDC
->dclevel
.pSurface
;
509 DC_vFinishBlit(pDC
, NULL
);
516 /* Initialize EXLATEOBJ */
517 EXLATEOBJ_vInitialize(&exlo
,
520 RGB(0xff, 0xff, 0xff),
521 pDC
->pdcattr
->crBackgroundClr
,
522 pDC
->pdcattr
->crForegroundClr
);
524 pDestSurf
= &pSurf
->SurfObj
;
527 DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
528 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
529 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
530 Status
= IntEngBitBlt(pDestSurf
,
533 pDC
->rosdc
.CombinedClip
,
540 ROP4_FROM_INDEX(R3_OPINDEX_SRCCOPY
));
542 /* Cleanup EXLATEOBJ */
543 EXLATEOBJ_vCleanup(&exlo
);
546 DC_vFinishBlit(pDC
, NULL
);
549 if (NT_SUCCESS(Status
))
554 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
556 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
557 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
560 ExFreePoolWithTag(pbmiSafe
, 'pmTG');
565 /* Converts a device-dependent bitmap to a DIB */
568 GreGetDIBitsInternal(
579 BITMAPCOREINFO
* pbmci
= NULL
;
580 PSURFACE psurf
= NULL
;
590 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
592 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
595 colorPtr
= (LPBYTE
)Info
+ Info
->bmiHeader
.biSize
;
598 bitmap_type
= DIB_GetBitmapInfo(&Info
->bmiHeader
,
605 if(bitmap_type
== -1)
607 DPRINT("Wrong bitmap format\n");
608 EngSetLastError(ERROR_INVALID_PARAMETER
);
611 else if(bitmap_type
== 0)
613 /* We need a BITMAPINFO to create a DIB, but we have to fill
614 * the BITMAPCOREINFO we're provided */
615 pbmci
= (BITMAPCOREINFO
*)Info
;
616 Info
= DIB_ConvertBitmapInfo((BITMAPINFO
*)pbmci
, Usage
);
619 DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
622 rgbQuads
= Info
->bmiColors
;
625 pDC
= DC_LockDc(hDC
);
626 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
632 /* Get a pointer to the source bitmap object */
633 psurf
= SURFACE_ShareLockSurface(hBitmap
);
640 /* Fill in the structure */
643 case 0: /* Only info */
644 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
645 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
646 -psurf
->SurfObj
.sizlBitmap
.cy
:
647 psurf
->SurfObj
.sizlBitmap
.cy
;;
648 Info
->bmiHeader
.biPlanes
= 1;
649 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
650 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
651 Info
->bmiHeader
.biHeight
,
652 Info
->bmiHeader
.biBitCount
);
655 switch(Info
->bmiHeader
.biBitCount
)
659 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
662 Info
->bmiHeader
.biCompression
= BI_RGB
;
666 else if(Info
->bmiHeader
.biBitCount
> 8)
668 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
672 Info
->bmiHeader
.biCompression
= BI_RGB
;
674 Info
->bmiHeader
.biXPelsPerMeter
= 0;
675 Info
->bmiHeader
.biYPelsPerMeter
= 0;
676 Info
->bmiHeader
.biClrUsed
= 0;
677 Info
->bmiHeader
.biClrImportant
= 0;
678 ScanLines
= abs(Info
->bmiHeader
.biHeight
);
684 Info
->bmiHeader
.biClrUsed
= 0;
686 /* If the bitmap if a DIB section and has the same format than what
687 * we're asked, go ahead! */
688 if((psurf
->hSecure
) &&
689 (BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bpp
))
691 if(Usage
== DIB_RGB_COLORS
)
693 ULONG colors
= min(psurf
->ppal
->NumColors
, 256);
694 if(colors
!= 256) Info
->bmiHeader
.biClrUsed
= colors
;
695 for(i
= 0; i
< colors
; i
++)
697 rgbQuads
[i
].rgbRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
698 rgbQuads
[i
].rgbGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
699 rgbQuads
[i
].rgbBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
700 rgbQuads
[i
].rgbReserved
= 0;
705 for(i
= 0; i
< 256; i
++)
706 ((WORD
*)rgbQuads
)[i
] = i
;
711 if(Usage
== DIB_PAL_COLORS
)
713 for(i
= 0; i
< 256; i
++)
715 ((WORD
*)rgbQuads
)[i
] = i
;
718 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
))
720 /* For color DDBs in native depth (mono DDBs always have
721 a black/white palette):
722 Generate the color map from the selected palette */
723 PPALETTE pDcPal
= PALETTE_ShareLockPalette(pDC
->dclevel
.hpal
);
729 for (i
= 0; i
< pDcPal
->NumColors
; i
++)
731 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
732 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
733 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
734 rgbQuads
[i
].rgbReserved
= 0;
736 PALETTE_ShareUnlockPalette(pDcPal
);
743 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
= rgbQuads
[0].rgbBlue
= 0;
744 rgbQuads
[0].rgbReserved
= 0;
745 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
= rgbQuads
[1].rgbBlue
= 0xff;
746 rgbQuads
[1].rgbReserved
= 0;
750 RtlCopyMemory(rgbQuads
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
757 memcpy(rgbQuads
, DefLogPaletteQuads
,
758 10 * sizeof(RGBQUAD
));
759 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10,
760 10 * sizeof(RGBQUAD
));
761 color
= rgbQuads
+ 10;
762 for(r
= 0; r
<= 5; r
++) /* FIXME */
764 for(g
= 0; g
<= 5; g
++)
766 for(b
= 0; b
<= 5; b
++)
768 color
->rgbRed
= (r
* 0xff) / 5;
769 color
->rgbGreen
= (g
* 0xff) / 5;
770 color
->rgbBlue
= (b
* 0xff) / 5;
771 color
->rgbReserved
= 0;
783 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
785 ((PDWORD
)Info
->bmiColors
)[0] = 0x7c00;
786 ((PDWORD
)Info
->bmiColors
)[1] = 0x03e0;
787 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
792 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
796 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
797 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
798 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
802 ((PDWORD
)Info
->bmiColors
)[0] = 0xf800;
803 ((PDWORD
)Info
->bmiColors
)[1] = 0x07e0;
804 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
811 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
815 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
816 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
817 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
821 ((PDWORD
)Info
->bmiColors
)[0] = 0xff0000;
822 ((PDWORD
)Info
->bmiColors
)[1] = 0x00ff00;
823 ((PDWORD
)Info
->bmiColors
)[2] = 0x0000ff;
828 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(width
, height
, bpp
);
830 if(Bits
&& ScanLines
)
832 /* Create a DIBSECTION, blt it, profit */
841 if (StartScan
> (ULONG
)psurf
->SurfObj
.sizlBitmap
.cy
)
848 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
852 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
853 Info
->bmiHeader
.biHeight
= (height
< 0) ?
854 -(LONG
)ScanLines
: ScanLines
;
856 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
858 Info
->bmiHeader
.biWidth
= width
;
859 Info
->bmiHeader
.biHeight
= height
;
863 DPRINT1("Unable to create a DIB Section!\n");
864 EngSetLastError(ERROR_INVALID_PARAMETER
);
869 psurfDest
= SURFACE_ShareLockSurface(hBmpDest
);
871 RECTL_vSetRect(&rcDest
, 0, 0, ScanLines
, psurf
->SurfObj
.sizlBitmap
.cx
);
879 if(ScanLines
<= StartScan
)
882 SURFACE_ShareUnlockSurface(psurfDest
);
883 GreDeleteObject(hBmpDest
);
887 ScanLines
-= StartScan
;
891 srcPoint
.y
= StartScan
;
894 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xffffff, 0xffffff, 0);
896 ret
= IntEngCopyBits(&psurfDest
->SurfObj
,
903 SURFACE_ShareUnlockSurface(psurfDest
);
909 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, ScanLines
, bpp
));
912 GreDeleteObject(hBmpDest
);
913 EXLATEOBJ_vCleanup(&exlo
);
915 else ScanLines
= abs(height
);
919 if(pDC
) DC_UnlockDc(pDC
);
920 if(psurf
) SURFACE_ShareUnlockSurface(psurf
);
921 if(pbmci
) DIB_FreeConvertedBitmapInfo(Info
, (BITMAPINFO
*)pbmci
, Usage
);
928 NtGdiGetDIBitsInternal(
931 _In_ UINT iStartScan
,
933 _Out_opt_ LPBYTE pjBits
,
934 _Inout_ LPBITMAPINFO pbmiUser
,
940 HANDLE hSecure
= NULL
;
944 /* Check for bad iUsage */
945 if (iUsage
> 2) return 0;
947 /* Check if the size of the bitmap info is large enough */
948 if (cjMaxInfo
< sizeof(BITMAPCOREHEADER
))
953 /* Use maximum size */
954 cjMaxInfo
= min(cjMaxInfo
, sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
));
956 // HACK: the underlying code sucks and doesn't care for the size, so we
957 // give it the maximum ever needed
958 cjAlloc
= sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
);
960 /* Allocate a buffer the bitmapinfo */
961 pbmi
= ExAllocatePoolWithTag(PagedPool
, cjAlloc
, 'imBG');
971 /* Probe and copy the BITMAPINFO */
972 ProbeForRead(pbmiUser
, cjMaxInfo
, 1);
973 RtlCopyMemory(pbmi
, pbmiUser
, cjMaxInfo
);
975 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
977 _SEH2_YIELD(goto cleanup
;)
981 /* Check if the header size is large enough */
982 if ((pbmi
->bmiHeader
.biSize
< sizeof(BITMAPCOREHEADER
)) ||
983 (pbmi
->bmiHeader
.biSize
> cjMaxInfo
))
988 /* Check if the caller provided bitmap bits */
991 /* Secure the user mode memory */
992 hSecure
= EngSecureMem(pjBits
, cjMaxBits
);
999 /* Now call the internal function */
1000 iResult
= GreGetDIBitsInternal(hdc
,
1010 /* Check for success */
1013 /* Use SEH to copy back to user mode */
1016 /* Copy the data back */
1017 ProbeForWrite(pbmiUser
, cjMaxInfo
, 1);
1018 RtlCopyMemory(pbmiUser
, pbmi
, cjMaxInfo
);
1020 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1028 if (hSecure
) EngUnsecureMem(hSecure
);
1029 ExFreePoolWithTag(pbmi
, 'imBG');
1035 #define ROP_TO_ROP4(Rop) ((Rop) >> 16)
1040 NtGdiStretchDIBitsInternal(
1050 IN OPTIONAL LPBYTE pjInit
,
1051 IN LPBITMAPINFO pbmi
,
1053 IN DWORD dwRop
, // MS ntgdi.h says dwRop4(?)
1058 BOOL bResult
= FALSE
;
1063 PSURFACE psurfTmp
= 0, psurfDst
= 0;
1064 PPALETTE ppalDIB
= 0;
1068 if (!(pdc
= DC_LockDc(hdc
)))
1070 EngSetLastError(ERROR_INVALID_HANDLE
);
1074 /* Transform dest size */
1077 IntLPtoDP(pdc
, (POINTL
*)&sizel
, 1);
1080 /* Check if we can use NtGdiSetDIBitsToDeviceInternal */
1081 if (sizel
.cx
== cxSrc
&& sizel
.cy
== cySrc
&& dwRop
== SRCCOPY
)
1084 return NtGdiSetDIBitsToDeviceInternal(hdc
,
1102 pvBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, 'pmeT');
1110 ProbeForRead(pjInit
, cjMaxBits
, 1);
1111 RtlCopyMemory(pvBits
, pjInit
, cjMaxBits
);
1113 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1115 _SEH2_YIELD(return 0);
1119 /* FIXME: Locking twice is cheesy, coord tranlation in UM will fix it */
1120 if (!(pdc
= DC_LockDc(hdc
)))
1122 DPRINT1("Could not lock dc\n");
1123 EngSetLastError(ERROR_INVALID_HANDLE
);
1127 /* Calculate source and destination rect */
1130 rcSrc
.right
= xSrc
+ abs(cxSrc
);
1131 rcSrc
.bottom
= ySrc
+ abs(cySrc
);
1134 rcDst
.right
= rcDst
.left
+ cxDst
;
1135 rcDst
.bottom
= rcDst
.top
+ cyDst
;
1136 IntLPtoDP(pdc
, (POINTL
*)&rcDst
, 2);
1137 RECTL_vOffsetRect(&rcDst
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1139 hbmTmp
= GreCreateBitmapEx(pbmi
->bmiHeader
.biWidth
,
1140 pbmi
->bmiHeader
.biHeight
,
1142 BitmapFormat(pbmi
->bmiHeader
.biBitCount
,
1143 pbmi
->bmiHeader
.biCompression
),
1144 pbmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
1145 pbmi
->bmiHeader
.biSizeImage
,
1155 psurfTmp
= SURFACE_ShareLockSurface(hbmTmp
);
1162 /* Create a palette for the DIB */
1163 ppalDIB
= CreateDIBPalette(pbmi
, pdc
, dwUsage
);
1170 /* Prepare DC for blit */
1171 DC_vPrepareDCsForBlit(pdc
, rcDst
, NULL
, rcSrc
);
1173 psurfDst
= pdc
->dclevel
.pSurface
;
1176 DC_vFinishBlit(pdc
, NULL
);
1182 /* Initialize XLATEOBJ */
1183 EXLATEOBJ_vInitialize(&exlo
,
1186 RGB(0xff, 0xff, 0xff),
1187 pdc
->pdcattr
->crBackgroundClr
,
1188 pdc
->pdcattr
->crForegroundClr
);
1190 /* Perform the stretch operation */
1191 bResult
= IntEngStretchBlt(&psurfDst
->SurfObj
,
1194 pdc
->rosdc
.CombinedClip
,
1200 &pdc
->eboFill
.BrushObject
,
1202 ROP_TO_ROP4(dwRop
));
1205 DC_vFinishBlit(pdc
, NULL
);
1206 EXLATEOBJ_vCleanup(&exlo
);
1208 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
1209 if (psurfTmp
) SURFACE_ShareUnlockSurface(psurfTmp
);
1210 if (hbmTmp
) GreDeleteObject(hbmTmp
);
1211 if (pdc
) DC_UnlockDc(pdc
);
1212 ExFreePoolWithTag(pvBits
, 'pmeT');
1233 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1234 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1236 if (bpp
!= 1) fColor
= TRUE
;
1237 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1240 const RGBQUAD
*rgb
= (RGBQUAD
*)((PBYTE
)data
+ data
->bmiHeader
.biSize
);
1241 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1243 // Check if the first color of the colormap is black
1244 if ((col
== RGB(0, 0, 0)))
1247 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1249 // If the second color is white, create a monochrome bitmap
1250 fColor
= (col
!= RGB(0xff,0xff,0xff));
1255 // Now create the bitmap
1258 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
1262 handle
= GreCreateBitmap(width
,
1272 if (NULL
!= handle
&& CBM_INIT
== init
)
1274 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
1280 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1281 // The DDB that is created will be whatever bit depth your reference DC is
1284 NtGdiCreateDIBitmapInternal(
1289 IN OPTIONAL LPBYTE pjInit
,
1290 IN OPTIONAL LPBITMAPINFO pbmi
,
1292 IN UINT cjMaxInitInfo
,
1297 NTSTATUS Status
= STATUS_SUCCESS
;
1298 PBYTE safeBits
= NULL
;
1299 HBITMAP hbmResult
= NULL
;
1301 if(pjInit
&& (fInit
== CBM_INIT
))
1303 if (cjMaxBits
== 0) return NULL
;
1304 safeBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, TAG_DIB
);
1307 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1314 if(pbmi
) ProbeForRead(pbmi
, cjMaxInitInfo
, 1);
1315 if(pjInit
&& (fInit
== CBM_INIT
))
1317 ProbeForRead(pjInit
, cjMaxBits
, 1);
1318 RtlCopyMemory(safeBits
, pjInit
, cjMaxBits
);
1321 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1323 Status
= _SEH2_GetExceptionCode();
1327 if(!NT_SUCCESS(Status
))
1329 SetLastNtError(Status
);
1333 hbmResult
= GreCreateDIBitmapInternal(hDc
,
1345 if (safeBits
) ExFreePoolWithTag(safeBits
, TAG_DIB
);
1351 GreCreateDIBitmapInternal(
1356 IN OPTIONAL LPBYTE pjInit
,
1357 IN OPTIONAL PBITMAPINFO pbmi
,
1368 if (!hDc
) /* 1bpp monochrome bitmap */
1370 // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1371 hdcDest
= NtGdiCreateCompatibleDC(0);
1382 Dc
= DC_LockDc(hdcDest
);
1385 EngSetLastError(ERROR_INVALID_HANDLE
);
1388 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1389 * if bpp != 1 and ignore the real value that was passed */
1391 bpp
= pbmi
->bmiHeader
.biBitCount
;
1394 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
, iUsage
);
1399 NtGdiDeleteObjectApp(hdcDest
);
1407 NtGdiCreateDIBSection(
1409 IN OPTIONAL HANDLE hSection
,
1415 IN ULONG_PTR dwColorSpace
,
1418 HBITMAP hbitmap
= 0;
1420 BOOL bDesktopDC
= FALSE
;
1421 NTSTATUS Status
= STATUS_SUCCESS
;
1423 if (!bmi
) return hbitmap
; // Make sure.
1427 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1428 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1429 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, (WORD
)Usage
), 1);
1431 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1433 Status
= _SEH2_GetExceptionCode();
1437 if(!NT_SUCCESS(Status
))
1439 SetLastNtError(Status
);
1443 // If the reference hdc is null, take the desktop dc
1446 hDC
= NtGdiCreateCompatibleDC(0);
1450 if ((dc
= DC_LockDc(hDC
)))
1452 hbitmap
= DIB_CreateDIBSection(dc
,
1463 EngSetLastError(ERROR_INVALID_HANDLE
);
1467 NtGdiDeleteObjectApp(hDC
);
1474 DIB_CreateDIBSection(
1476 CONST BITMAPINFO
*bmi
,
1484 SURFACE
*bmp
= NULL
;
1485 void *mapBits
= NULL
;
1486 PPALETTE ppalDIB
= NULL
;
1488 // Fill BITMAP32 structure with DIB data
1489 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1496 DPRINT("format (%ld,%ld), planes %u, bpp %u, size %lu, colors %lu (%s)\n",
1497 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1498 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1500 /* CreateDIBSection should fail for compressed formats */
1501 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1503 DPRINT1("no compressed format allowed\n");
1504 return (HBITMAP
)NULL
;
1507 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1509 bm
.bmWidth
= bi
->biWidth
;
1510 bm
.bmHeight
= effHeight
;
1511 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: WIDTH_BYTES_ALIGN32(bm
.bmWidth
, bi
->biBitCount
);
1513 bm
.bmPlanes
= bi
->biPlanes
;
1514 bm
.bmBitsPixel
= bi
->biBitCount
;
1517 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1518 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1519 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
&& bi
->biCompression
!= BI_BITFIELDS
1520 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1524 SYSTEM_BASIC_INFORMATION Sbi
;
1527 LARGE_INTEGER SectionOffset
;
1530 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1534 if (!NT_SUCCESS(Status
))
1536 DPRINT1("ZwQuerySystemInformation failed (0x%lx)\n", Status
);
1540 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1541 mapSize
= bi
->biSizeImage
+ (offset
- mapOffset
);
1543 SectionOffset
.LowPart
= mapOffset
;
1544 SectionOffset
.HighPart
= 0;
1546 Status
= ZwMapViewOfSection(section
,
1556 if (!NT_SUCCESS(Status
))
1558 DPRINT1("ZwMapViewOfSection failed (0x%lx)\n", Status
);
1559 EngSetLastError(ERROR_INVALID_PARAMETER
);
1563 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1565 else if (ovr_pitch
&& offset
)
1566 bm
.bmBits
= (LPVOID
) offset
;
1570 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1573 DPRINT1("Failed to allocate memory\n");
1578 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1579 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1582 // Create Device Dependent Bitmap and add DIB pointer
1583 //Size.cx = bm.bmWidth;
1584 //Size.cy = abs(bm.bmHeight);
1585 res
= GreCreateBitmapEx(bm
.bmWidth
,
1588 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1589 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1590 ((bi
->biHeight
< 0) ? BMF_TOPDOWN
: 0),
1596 DPRINT1("GreCreateBitmapEx failed\n");
1597 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
1600 bmp
= SURFACE_ShareLockSurface(res
); // HACK
1603 DPRINT1("SURFACE_LockSurface failed\n");
1604 EngSetLastError(ERROR_INVALID_HANDLE
);
1608 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1609 table between the DIB and the X physical device. Obviously,
1610 this is left out of the ReactOS implementation. Instead,
1611 we call NtGdiSetDIBColorTable. */
1612 bmp
->hDIBSection
= section
;
1613 bmp
->hSecure
= hSecure
;
1614 bmp
->dwOffset
= offset
;
1615 bmp
->flags
= API_BITMAP
;
1616 bmp
->biClrImportant
= bi
->biClrImportant
;
1618 /* Create a palette for the DIB */
1619 ppalDIB
= CreateDIBPalette(bmi
, dc
, usage
);
1621 // Clean up in case of errors
1623 if (!res
|| !bmp
|| !bm
.bmBits
|| !ppalDIB
)
1625 DPRINT("Got an error res=%p, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1628 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1631 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1635 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1640 SURFACE_ShareUnlockSurface(bmp
);
1646 GreDeleteObject(res
);
1652 PALETTE_ShareUnlockPalette(ppalDIB
);
1658 /* If we're here, everything went fine */
1659 SURFACE_vSetPalette(bmp
, ppalDIB
);
1660 PALETTE_ShareUnlockPalette(ppalDIB
);
1661 SURFACE_ShareUnlockSurface(bmp
);
1664 // Return BITMAP handle and storage location
1665 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1673 /***********************************************************************
1676 * Get the info from a bitmap header.
1677 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
1681 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
1682 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
1684 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1686 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
1687 *width
= core
->bcWidth
;
1688 *height
= core
->bcHeight
;
1689 *planes
= core
->bcPlanes
;
1690 *bpp
= core
->bcBitCount
;
1695 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* Assume BITMAPINFOHEADER */
1697 *width
= header
->biWidth
;
1698 *height
= header
->biHeight
;
1699 *planes
= header
->biPlanes
;
1700 *bpp
= header
->biBitCount
;
1701 *compr
= header
->biCompression
;
1702 *size
= header
->biSizeImage
;
1705 DPRINT1("(%u): unknown/wrong size for header\n", header
->biSize
);
1709 /***********************************************************************
1710 * DIB_GetDIBImageBytes
1712 * Return the number of bytes used to hold the image in a DIB bitmap.
1713 * 11/16/1999 (RJJ) lifted from wine
1716 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1718 return WIDTH_BYTES_ALIGN32(width
, depth
) * (height
< 0 ? -height
: height
);
1721 /***********************************************************************
1722 * DIB_BitmapInfoSize
1724 * Return the size of the bitmap info structure including color table.
1725 * 11/16/1999 (RJJ) lifted from wine
1728 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1730 unsigned int colors
, size
, masks
= 0;
1732 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1734 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
1735 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1736 return sizeof(BITMAPCOREHEADER
) + colors
*
1737 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1739 else /* Assume BITMAPINFOHEADER */
1741 colors
= info
->bmiHeader
.biClrUsed
;
1742 if (colors
> 256) colors
= 256;
1743 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
1744 colors
= 1 << info
->bmiHeader
.biBitCount
;
1745 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
1746 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
1747 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1753 DIB_MapPaletteColors(PPALETTE ppalDc
, CONST BITMAPINFO
* lpbmi
)
1760 if (!(ppalDc
->flFlags
& PAL_INDEXED
))
1765 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1766 if (lpbmi
->bmiHeader
.biClrUsed
)
1768 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1771 ppalNew
= PALETTE_AllocPalWithHandle(PAL_INDEXED
, nNumColors
, NULL
, 0, 0, 0);
1772 if (ppalNew
== NULL
)
1774 DPRINT1("Could not allocate palette\n");
1778 lpIndex
= (USHORT
*)((PBYTE
)lpbmi
+ lpbmi
->bmiHeader
.biSize
);
1780 for (i
= 0; i
< nNumColors
; i
++)
1782 ULONG iColorIndex
= *lpIndex
% ppalDc
->NumColors
;
1783 ppalNew
->IndexedColors
[i
] = ppalDc
->IndexedColors
[iColorIndex
];
1787 hpal
= ppalNew
->BaseObject
.hHmgr
;
1788 PALETTE_UnlockPalette(ppalNew
);
1793 /* Converts a BITMAPCOREINFO to a BITMAPINFO structure,
1794 * or does nothing if it's already a BITMAPINFO (or V4 or V5) */
1797 DIB_ConvertBitmapInfo (CONST BITMAPINFO
* pbmi
, DWORD Usage
)
1799 CONST BITMAPCOREINFO
* pbmci
= (BITMAPCOREINFO
*)pbmi
;
1800 BITMAPINFO
* pNewBmi
;
1801 UINT numColors
= 0, ColorsSize
= 0;
1803 if(pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) return (BITMAPINFO
*)pbmi
;
1804 if(pbmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) return NULL
;
1806 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
1808 numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
1809 if(Usage
== DIB_PAL_COLORS
)
1811 ColorsSize
= numColors
* sizeof(WORD
);
1815 ColorsSize
= numColors
* sizeof(RGBQUAD
);
1818 else if (Usage
== DIB_PAL_COLORS
)
1820 /* Invalid at high-res */
1824 pNewBmi
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + ColorsSize
, TAG_DIB
);
1825 if(!pNewBmi
) return NULL
;
1827 RtlZeroMemory(pNewBmi
, sizeof(BITMAPINFOHEADER
) + ColorsSize
);
1829 pNewBmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1830 pNewBmi
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
1831 pNewBmi
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
1832 pNewBmi
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
1833 pNewBmi
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
1834 pNewBmi
->bmiHeader
.biCompression
= BI_RGB
;
1835 pNewBmi
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(pNewBmi
->bmiHeader
.biWidth
,
1836 pNewBmi
->bmiHeader
.biHeight
,
1837 pNewBmi
->bmiHeader
.biBitCount
);
1839 if(Usage
== DIB_PAL_COLORS
)
1841 RtlCopyMemory(pNewBmi
->bmiColors
, pbmci
->bmciColors
, ColorsSize
);
1846 for(i
=0; i
<numColors
; i
++)
1848 pNewBmi
->bmiColors
[i
].rgbRed
= pbmci
->bmciColors
[i
].rgbtRed
;
1849 pNewBmi
->bmiColors
[i
].rgbGreen
= pbmci
->bmciColors
[i
].rgbtGreen
;
1850 pNewBmi
->bmiColors
[i
].rgbBlue
= pbmci
->bmciColors
[i
].rgbtBlue
;
1857 /* Frees a BITMAPINFO created with DIB_ConvertBitmapInfo */
1860 DIB_FreeConvertedBitmapInfo(BITMAPINFO
* converted
, BITMAPINFO
* orig
, DWORD usage
)
1862 BITMAPCOREINFO
* pbmci
;
1863 if(converted
== orig
)
1868 /* Caller don't want any conversion */
1869 ExFreePoolWithTag(converted
, TAG_DIB
);
1873 /* Perform inverse conversion */
1874 pbmci
= (BITMAPCOREINFO
*)orig
;
1876 ASSERT(pbmci
->bmciHeader
.bcSize
== sizeof(BITMAPCOREHEADER
));
1877 pbmci
->bmciHeader
.bcBitCount
= converted
->bmiHeader
.biBitCount
;
1878 pbmci
->bmciHeader
.bcWidth
= converted
->bmiHeader
.biWidth
;
1879 pbmci
->bmciHeader
.bcHeight
= converted
->bmiHeader
.biHeight
;
1880 pbmci
->bmciHeader
.bcPlanes
= converted
->bmiHeader
.biPlanes
;
1882 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
1884 UINT numColors
= converted
->bmiHeader
.biClrUsed
;
1885 if(!numColors
) numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
1886 if(usage
== DIB_PAL_COLORS
)
1888 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(WORD
));
1889 RtlCopyMemory(pbmci
->bmciColors
, converted
->bmiColors
, numColors
* sizeof(WORD
));
1894 RtlZeroMemory(pbmci
->bmciColors
, (1 << pbmci
->bmciHeader
.bcBitCount
) * sizeof(RGBTRIPLE
));
1895 for(i
=0; i
<numColors
; i
++)
1897 pbmci
->bmciColors
[i
].rgbtRed
= converted
->bmiColors
[i
].rgbRed
;
1898 pbmci
->bmciColors
[i
].rgbtGreen
= converted
->bmiColors
[i
].rgbGreen
;
1899 pbmci
->bmciColors
[i
].rgbtBlue
= converted
->bmiColors
[i
].rgbBlue
;
1903 /* Now free it, it's not needed anymore */
1904 ExFreePoolWithTag(converted
, TAG_DIB
);