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
;
587 RGBTRIPLE
* rgbTriples
;
591 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
593 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
596 colorPtr
= (LPBYTE
)Info
+ Info
->bmiHeader
.biSize
;
597 rgbTriples
= colorPtr
;
600 bitmap_type
= DIB_GetBitmapInfo(&Info
->bmiHeader
,
607 if(bitmap_type
== -1)
609 DPRINT("Wrong bitmap format\n");
610 EngSetLastError(ERROR_INVALID_PARAMETER
);
613 else if(bitmap_type
== 0)
615 /* We need a BITMAPINFO to create a DIB, but we have to fill
616 * the BITMAPCOREINFO we're provided */
617 pbmci
= (BITMAPCOREINFO
*)Info
;
618 Info
= DIB_ConvertBitmapInfo((BITMAPINFO
*)pbmci
, Usage
);
621 DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
624 rgbQuads
= Info
->bmiColors
;
627 pDC
= DC_LockDc(hDC
);
628 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
634 /* Get a pointer to the source bitmap object */
635 psurf
= SURFACE_ShareLockSurface(hBitmap
);
642 /* Fill in the structure */
645 case 0: /* Only info */
648 pbmci
->bmciHeader
.bcWidth
= (WORD
)psurf
->SurfObj
.sizlBitmap
.cx
;
649 pbmci
->bmciHeader
.bcHeight
= (WORD
)((psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
650 -psurf
->SurfObj
.sizlBitmap
.cy
:
651 psurf
->SurfObj
.sizlBitmap
.cy
);
652 pbmci
->bmciHeader
.bcPlanes
= 1;
653 pbmci
->bmciHeader
.bcBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
655 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
656 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
657 -psurf
->SurfObj
.sizlBitmap
.cy
:
658 psurf
->SurfObj
.sizlBitmap
.cy
;;
659 Info
->bmiHeader
.biPlanes
= 1;
660 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
661 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
662 Info
->bmiHeader
.biHeight
,
663 Info
->bmiHeader
.biBitCount
);
666 switch(Info
->bmiHeader
.biBitCount
)
670 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
673 Info
->bmiHeader
.biCompression
= BI_RGB
;
677 else if(Info
->bmiHeader
.biBitCount
> 8)
679 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
683 Info
->bmiHeader
.biCompression
= BI_RGB
;
685 Info
->bmiHeader
.biXPelsPerMeter
= 0;
686 Info
->bmiHeader
.biYPelsPerMeter
= 0;
687 Info
->bmiHeader
.biClrUsed
= 0;
688 Info
->bmiHeader
.biClrImportant
= 0;
689 ScanLines
= abs(Info
->bmiHeader
.biHeight
);
695 Info
->bmiHeader
.biClrUsed
= 0;
697 /* If the bitmap if a DIB section and has the same format than what
698 * we're asked, go ahead! */
699 if((psurf
->hSecure
) &&
700 (BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bpp
))
702 if(Usage
== DIB_RGB_COLORS
)
704 ULONG colors
= min(psurf
->ppal
->NumColors
, 256);
708 for(i
= 0; i
< colors
; i
++)
710 rgbTriples
[i
].rgbtRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
711 rgbTriples
[i
].rgbtGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
712 rgbTriples
[i
].rgbtBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
715 if(colors
!= 256) Info
->bmiHeader
.biClrUsed
= colors
;
716 for(i
= 0; i
< colors
; i
++)
718 rgbQuads
[i
].rgbRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
719 rgbQuads
[i
].rgbGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
720 rgbQuads
[i
].rgbBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
725 for(i
= 0; i
< 256; i
++)
727 if(pbmci
) ((WORD
*)rgbTriples
)[i
] = i
;
728 ((WORD
*)rgbQuads
)[i
] = i
;
734 if(Usage
== DIB_PAL_COLORS
)
736 for(i
= 0; i
< 256; i
++)
738 if(pbmci
) ((WORD
*)rgbTriples
)[i
] = i
;
739 ((WORD
*)rgbQuads
)[i
] = i
;
742 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
))
744 /* For color DDBs in native depth (mono DDBs always have
745 a black/white palette):
746 Generate the color map from the selected palette */
747 PPALETTE pDcPal
= PALETTE_ShareLockPalette(pDC
->dclevel
.hpal
);
753 for (i
= 0; i
< pDcPal
->NumColors
; i
++)
757 rgbTriples
[i
].rgbtRed
= pDcPal
->IndexedColors
[i
].peRed
;
758 rgbTriples
[i
].rgbtGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
759 rgbTriples
[i
].rgbtBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
762 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
763 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
764 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
765 rgbQuads
[i
].rgbReserved
= 0;
767 PALETTE_ShareUnlockPalette(pDcPal
);
776 rgbTriples
[0].rgbtRed
= rgbTriples
[0].rgbtGreen
=
777 rgbTriples
[0].rgbtBlue
= 0;
778 rgbTriples
[1].rgbtRed
= rgbTriples
[1].rgbtGreen
=
779 rgbTriples
[1].rgbtBlue
= 0xff;
781 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
=
782 rgbQuads
[0].rgbBlue
= 0;
783 rgbQuads
[0].rgbReserved
= 0;
784 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
=
785 rgbQuads
[1].rgbBlue
= 0xff;
786 rgbQuads
[1].rgbReserved
= 0;
791 RtlCopyMemory(rgbTriples
, EGAColorsTriples
, sizeof(EGAColorsTriples
));
792 RtlCopyMemory(rgbQuads
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
802 RGBTRIPLE
*colorTriple
;
804 RtlCopyMemory(rgbTriples
, DefLogPaletteTriples
,
805 10 * sizeof(RGBTRIPLE
));
806 RtlCopyMemory(rgbTriples
+ 246, DefLogPaletteTriples
+ 10,
807 10 * sizeof(RGBTRIPLE
));
808 colorTriple
= rgbTriples
+ 10;
809 for(r
= 0; r
<= 5; r
++) /* FIXME */
811 for(g
= 0; g
<= 5; g
++)
813 for(b
= 0; b
<= 5; b
++)
815 colorTriple
->rgbtRed
= (r
* 0xff) / 5;
816 colorTriple
->rgbtGreen
= (g
* 0xff) / 5;
817 colorTriple
->rgbtBlue
= (b
* 0xff) / 5;
823 memcpy(rgbQuads
, DefLogPaletteQuads
,
824 10 * sizeof(RGBQUAD
));
825 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10,
826 10 * sizeof(RGBQUAD
));
827 color
= rgbQuads
+ 10;
828 for(r
= 0; r
<= 5; r
++) /* FIXME */
830 for(g
= 0; g
<= 5; g
++)
832 for(b
= 0; b
<= 5; b
++)
834 color
->rgbRed
= (r
* 0xff) / 5;
835 color
->rgbGreen
= (g
* 0xff) / 5;
836 color
->rgbBlue
= (b
* 0xff) / 5;
837 color
->rgbReserved
= 0;
849 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
851 ((PDWORD
)Info
->bmiColors
)[0] = 0x7c00;
852 ((PDWORD
)Info
->bmiColors
)[1] = 0x03e0;
853 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
858 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
862 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
863 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
864 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
868 ((PDWORD
)Info
->bmiColors
)[0] = 0xf800;
869 ((PDWORD
)Info
->bmiColors
)[1] = 0x07e0;
870 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
877 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
881 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
882 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
883 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
887 ((PDWORD
)Info
->bmiColors
)[0] = 0xff0000;
888 ((PDWORD
)Info
->bmiColors
)[1] = 0x00ff00;
889 ((PDWORD
)Info
->bmiColors
)[2] = 0x0000ff;
894 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(width
, height
, bpp
);
896 if(Bits
&& ScanLines
)
898 /* Create a DIBSECTION, blt it, profit */
907 if (StartScan
> (ULONG
)psurf
->SurfObj
.sizlBitmap
.cy
)
914 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
918 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
919 Info
->bmiHeader
.biHeight
= (height
< 0) ?
920 -(LONG
)ScanLines
: ScanLines
;
922 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
924 Info
->bmiHeader
.biWidth
= width
;
925 Info
->bmiHeader
.biHeight
= height
;
929 DPRINT1("Unable to create a DIB Section!\n");
930 EngSetLastError(ERROR_INVALID_PARAMETER
);
935 psurfDest
= SURFACE_ShareLockSurface(hBmpDest
);
939 rcDest
.bottom
= ScanLines
;
940 rcDest
.right
= psurf
->SurfObj
.sizlBitmap
.cx
;
948 if(ScanLines
<= StartScan
)
951 SURFACE_ShareUnlockSurface(psurfDest
);
952 GreDeleteObject(hBmpDest
);
956 ScanLines
-= StartScan
;
960 srcPoint
.y
= StartScan
;
963 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xffffff, 0xffffff, 0);
965 ret
= IntEngCopyBits(&psurfDest
->SurfObj
,
972 SURFACE_ShareUnlockSurface(psurfDest
);
978 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, ScanLines
, bpp
));
981 GreDeleteObject(hBmpDest
);
982 EXLATEOBJ_vCleanup(&exlo
);
984 else ScanLines
= abs(height
);
988 if(pDC
) DC_UnlockDc(pDC
);
989 if(psurf
) SURFACE_ShareUnlockSurface(psurf
);
990 if(pbmci
) DIB_FreeConvertedBitmapInfo(Info
, (BITMAPINFO
*)pbmci
);
997 NtGdiGetDIBitsInternal(
1000 _In_ UINT iStartScan
,
1002 _Out_opt_ LPBYTE pjBits
,
1003 _Inout_ LPBITMAPINFO pbmiUser
,
1005 _In_ UINT cjMaxBits
,
1006 _In_ UINT cjMaxInfo
)
1009 HANDLE hSecure
= NULL
;
1013 /* Check for bad iUsage */
1014 if (iUsage
> 2) return 0;
1016 /* Check if the size of the bitmap info is large enough */
1017 if (cjMaxInfo
< sizeof(BITMAPCOREHEADER
))
1022 /* Use maximum size */
1023 cjMaxInfo
= min(cjMaxInfo
, sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
));
1025 // HACK: the underlying code sucks and doesn't care for the size, so we
1026 // give it the maximum ever needed
1027 cjAlloc
= sizeof(BITMAPV5HEADER
) + 256 * sizeof(RGBQUAD
);
1029 /* Allocate a buffer the bitmapinfo */
1030 pbmi
= ExAllocatePoolWithTag(PagedPool
, cjAlloc
, 'imBG');
1040 /* Probe and copy the BITMAPINFO */
1041 ProbeForRead(pbmiUser
, cjMaxInfo
, 1);
1042 RtlCopyMemory(pbmi
, pbmiUser
, cjMaxInfo
);
1044 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1046 _SEH2_YIELD(goto cleanup
;)
1050 /* Check if the header size is large enough */
1051 if ((pbmi
->bmiHeader
.biSize
< sizeof(BITMAPCOREHEADER
)) ||
1052 (pbmi
->bmiHeader
.biSize
> cjMaxInfo
))
1057 /* Check if the caller provided bitmap bits */
1060 /* Secure the user mode memory */
1061 hSecure
= EngSecureMem(pjBits
, cjMaxBits
);
1068 /* Now call the internal function */
1069 iResult
= GreGetDIBitsInternal(hdc
,
1079 /* Check for success */
1082 /* Use SEH to copy back to user mode */
1085 /* Copy the data back */
1086 ProbeForWrite(pbmiUser
, cjMaxInfo
, 1);
1087 RtlCopyMemory(pbmiUser
, pbmi
, cjMaxInfo
);
1089 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1097 if (hSecure
) EngUnsecureMem(hSecure
);
1098 ExFreePoolWithTag(pbmi
, 'imBG');
1104 #define ROP_TO_ROP4(Rop) ((Rop) >> 16)
1109 NtGdiStretchDIBitsInternal(
1119 IN OPTIONAL LPBYTE pjInit
,
1120 IN LPBITMAPINFO pbmi
,
1122 IN DWORD dwRop
, // MS ntgdi.h says dwRop4(?)
1127 BOOL bResult
= FALSE
;
1132 PSURFACE psurfTmp
= 0, psurfDst
= 0;
1133 PPALETTE ppalDIB
= 0;
1137 if (!(pdc
= DC_LockDc(hdc
)))
1139 EngSetLastError(ERROR_INVALID_HANDLE
);
1143 /* Transform dest size */
1146 IntLPtoDP(pdc
, (POINTL
*)&sizel
, 1);
1149 /* Check if we can use NtGdiSetDIBitsToDeviceInternal */
1150 if (sizel
.cx
== cxSrc
&& sizel
.cy
== cySrc
&& dwRop
== SRCCOPY
)
1153 return NtGdiSetDIBitsToDeviceInternal(hdc
,
1171 pvBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, 'pmeT');
1179 ProbeForRead(pjInit
, cjMaxBits
, 1);
1180 RtlCopyMemory(pvBits
, pjInit
, cjMaxBits
);
1182 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1184 _SEH2_YIELD(return 0);
1188 /* FIXME: Locking twice is cheesy, coord tranlation in UM will fix it */
1189 if (!(pdc
= DC_LockDc(hdc
)))
1191 DPRINT1("Could not lock dc\n");
1192 EngSetLastError(ERROR_INVALID_HANDLE
);
1196 /* Calculate source and destination rect */
1199 rcSrc
.right
= xSrc
+ abs(cxSrc
);
1200 rcSrc
.bottom
= ySrc
+ abs(cySrc
);
1203 rcDst
.right
= rcDst
.left
+ cxDst
;
1204 rcDst
.bottom
= rcDst
.top
+ cyDst
;
1205 IntLPtoDP(pdc
, (POINTL
*)&rcDst
, 2);
1206 RECTL_vOffsetRect(&rcDst
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1208 hbmTmp
= GreCreateBitmapEx(pbmi
->bmiHeader
.biWidth
,
1209 pbmi
->bmiHeader
.biHeight
,
1211 BitmapFormat(pbmi
->bmiHeader
.biBitCount
,
1212 pbmi
->bmiHeader
.biCompression
),
1213 pbmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
1214 pbmi
->bmiHeader
.biSizeImage
,
1224 psurfTmp
= SURFACE_ShareLockSurface(hbmTmp
);
1231 /* Create a palette for the DIB */
1232 ppalDIB
= CreateDIBPalette(pbmi
, pdc
, dwUsage
);
1239 /* Prepare DC for blit */
1240 DC_vPrepareDCsForBlit(pdc
, rcDst
, NULL
, rcSrc
);
1242 psurfDst
= pdc
->dclevel
.pSurface
;
1245 DC_vFinishBlit(pdc
, NULL
);
1251 /* Initialize XLATEOBJ */
1252 EXLATEOBJ_vInitialize(&exlo
,
1255 RGB(0xff, 0xff, 0xff),
1256 pdc
->pdcattr
->crBackgroundClr
,
1257 pdc
->pdcattr
->crForegroundClr
);
1259 /* Perform the stretch operation */
1260 bResult
= IntEngStretchBlt(&psurfDst
->SurfObj
,
1263 pdc
->rosdc
.CombinedClip
,
1269 &pdc
->eboFill
.BrushObject
,
1271 ROP_TO_ROP4(dwRop
));
1274 DC_vFinishBlit(pdc
, NULL
);
1275 EXLATEOBJ_vCleanup(&exlo
);
1277 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
1278 if (psurfTmp
) SURFACE_ShareUnlockSurface(psurfTmp
);
1279 if (hbmTmp
) GreDeleteObject(hbmTmp
);
1280 if (pdc
) DC_UnlockDc(pdc
);
1281 ExFreePoolWithTag(pvBits
, 'pmeT');
1302 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1303 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1305 if (bpp
!= 1) fColor
= TRUE
;
1306 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1309 const RGBQUAD
*rgb
= (RGBQUAD
*)((PBYTE
)data
+ data
->bmiHeader
.biSize
);
1310 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1312 // Check if the first color of the colormap is black
1313 if ((col
== RGB(0, 0, 0)))
1316 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1318 // If the second color is white, create a monochrome bitmap
1319 fColor
= (col
!= RGB(0xff,0xff,0xff));
1324 // Now create the bitmap
1327 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
1331 handle
= GreCreateBitmap(width
,
1341 if (NULL
!= handle
&& CBM_INIT
== init
)
1343 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
1349 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1350 // The DDB that is created will be whatever bit depth your reference DC is
1353 NtGdiCreateDIBitmapInternal(
1358 IN OPTIONAL LPBYTE pjInit
,
1359 IN OPTIONAL LPBITMAPINFO pbmi
,
1361 IN UINT cjMaxInitInfo
,
1366 NTSTATUS Status
= STATUS_SUCCESS
;
1367 PBYTE safeBits
= NULL
;
1368 HBITMAP hbmResult
= NULL
;
1370 if(pjInit
&& (fInit
== CBM_INIT
))
1372 if (cjMaxBits
== 0) return NULL
;
1373 safeBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, TAG_DIB
);
1376 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1383 if(pbmi
) ProbeForRead(pbmi
, cjMaxInitInfo
, 1);
1384 if(pjInit
&& (fInit
== CBM_INIT
))
1386 ProbeForRead(pjInit
, cjMaxBits
, 1);
1387 RtlCopyMemory(safeBits
, pjInit
, cjMaxBits
);
1390 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1392 Status
= _SEH2_GetExceptionCode();
1396 if(!NT_SUCCESS(Status
))
1398 SetLastNtError(Status
);
1402 hbmResult
= GreCreateDIBitmapInternal(hDc
,
1414 if (safeBits
) ExFreePoolWithTag(safeBits
, TAG_DIB
);
1420 GreCreateDIBitmapInternal(
1425 IN OPTIONAL LPBYTE pjInit
,
1426 IN OPTIONAL PBITMAPINFO pbmi
,
1437 if (!hDc
) /* 1bpp monochrome bitmap */
1439 // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1440 hdcDest
= NtGdiCreateCompatibleDC(0);
1451 Dc
= DC_LockDc(hdcDest
);
1454 EngSetLastError(ERROR_INVALID_HANDLE
);
1457 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1458 * if bpp != 1 and ignore the real value that was passed */
1460 bpp
= pbmi
->bmiHeader
.biBitCount
;
1463 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
, iUsage
);
1468 NtGdiDeleteObjectApp(hdcDest
);
1476 NtGdiCreateDIBSection(
1478 IN OPTIONAL HANDLE hSection
,
1484 IN ULONG_PTR dwColorSpace
,
1487 HBITMAP hbitmap
= 0;
1489 BOOL bDesktopDC
= FALSE
;
1490 NTSTATUS Status
= STATUS_SUCCESS
;
1492 if (!bmi
) return hbitmap
; // Make sure.
1496 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1497 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1498 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, (WORD
)Usage
), 1);
1500 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1502 Status
= _SEH2_GetExceptionCode();
1506 if(!NT_SUCCESS(Status
))
1508 SetLastNtError(Status
);
1512 // If the reference hdc is null, take the desktop dc
1515 hDC
= NtGdiCreateCompatibleDC(0);
1519 if ((dc
= DC_LockDc(hDC
)))
1521 hbitmap
= DIB_CreateDIBSection(dc
,
1532 EngSetLastError(ERROR_INVALID_HANDLE
);
1536 NtGdiDeleteObjectApp(hDC
);
1543 DIB_CreateDIBSection(
1545 CONST BITMAPINFO
*bmi
,
1553 SURFACE
*bmp
= NULL
;
1554 void *mapBits
= NULL
;
1555 PPALETTE ppalDIB
= NULL
;
1557 // Fill BITMAP32 structure with DIB data
1558 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1565 DPRINT("format (%ld,%ld), planes %u, bpp %u, size %lu, colors %lu (%s)\n",
1566 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1567 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1569 /* CreateDIBSection should fail for compressed formats */
1570 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1572 DPRINT1("no compressed format allowed\n");
1573 return (HBITMAP
)NULL
;
1576 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1578 bm
.bmWidth
= bi
->biWidth
;
1579 bm
.bmHeight
= effHeight
;
1580 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: WIDTH_BYTES_ALIGN32(bm
.bmWidth
, bi
->biBitCount
);
1582 bm
.bmPlanes
= bi
->biPlanes
;
1583 bm
.bmBitsPixel
= bi
->biBitCount
;
1586 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1587 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1588 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
&& bi
->biCompression
!= BI_BITFIELDS
1589 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1593 SYSTEM_BASIC_INFORMATION Sbi
;
1596 LARGE_INTEGER SectionOffset
;
1599 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1603 if (!NT_SUCCESS(Status
))
1605 DPRINT1("ZwQuerySystemInformation failed (0x%lx)\n", Status
);
1609 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1610 mapSize
= bi
->biSizeImage
+ (offset
- mapOffset
);
1612 SectionOffset
.LowPart
= mapOffset
;
1613 SectionOffset
.HighPart
= 0;
1615 Status
= ZwMapViewOfSection(section
,
1625 if (!NT_SUCCESS(Status
))
1627 DPRINT1("ZwMapViewOfSection failed (0x%lx)\n", Status
);
1628 EngSetLastError(ERROR_INVALID_PARAMETER
);
1632 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1634 else if (ovr_pitch
&& offset
)
1635 bm
.bmBits
= (LPVOID
) offset
;
1639 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1642 DPRINT1("Failed to allocate memory\n");
1647 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1648 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1651 // Create Device Dependent Bitmap and add DIB pointer
1652 //Size.cx = bm.bmWidth;
1653 //Size.cy = abs(bm.bmHeight);
1654 res
= GreCreateBitmapEx(bm
.bmWidth
,
1657 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1658 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1659 ((bi
->biHeight
< 0) ? BMF_TOPDOWN
: 0),
1665 DPRINT1("GreCreateBitmapEx failed\n");
1666 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
1669 bmp
= SURFACE_ShareLockSurface(res
); // HACK
1672 DPRINT1("SURFACE_LockSurface failed\n");
1673 EngSetLastError(ERROR_INVALID_HANDLE
);
1677 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1678 table between the DIB and the X physical device. Obviously,
1679 this is left out of the ReactOS implementation. Instead,
1680 we call NtGdiSetDIBColorTable. */
1681 bmp
->hDIBSection
= section
;
1682 bmp
->hSecure
= hSecure
;
1683 bmp
->dwOffset
= offset
;
1684 bmp
->flags
= API_BITMAP
;
1685 bmp
->biClrImportant
= bi
->biClrImportant
;
1687 /* Create a palette for the DIB */
1688 ppalDIB
= CreateDIBPalette(bmi
, dc
, usage
);
1690 // Clean up in case of errors
1692 if (!res
|| !bmp
|| !bm
.bmBits
|| !ppalDIB
)
1694 DPRINT("Got an error res=%p, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1697 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1700 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1704 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1709 SURFACE_ShareUnlockSurface(bmp
);
1715 GreDeleteObject(res
);
1721 PALETTE_ShareUnlockPalette(ppalDIB
);
1727 /* If we're here, everything went fine */
1728 SURFACE_vSetPalette(bmp
, ppalDIB
);
1729 PALETTE_ShareUnlockPalette(ppalDIB
);
1730 SURFACE_ShareUnlockSurface(bmp
);
1733 // Return BITMAP handle and storage location
1734 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1742 /***********************************************************************
1745 * Get the info from a bitmap header.
1746 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
1750 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
1751 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
1753 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1755 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
1756 *width
= core
->bcWidth
;
1757 *height
= core
->bcHeight
;
1758 *planes
= core
->bcPlanes
;
1759 *bpp
= core
->bcBitCount
;
1764 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* Assume BITMAPINFOHEADER */
1766 *width
= header
->biWidth
;
1767 *height
= header
->biHeight
;
1768 *planes
= header
->biPlanes
;
1769 *bpp
= header
->biBitCount
;
1770 *compr
= header
->biCompression
;
1771 *size
= header
->biSizeImage
;
1774 DPRINT1("(%u): unknown/wrong size for header\n", header
->biSize
);
1778 /***********************************************************************
1779 * DIB_GetDIBImageBytes
1781 * Return the number of bytes used to hold the image in a DIB bitmap.
1782 * 11/16/1999 (RJJ) lifted from wine
1785 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1787 return WIDTH_BYTES_ALIGN32(width
, depth
) * (height
< 0 ? -height
: height
);
1790 /***********************************************************************
1791 * DIB_BitmapInfoSize
1793 * Return the size of the bitmap info structure including color table.
1794 * 11/16/1999 (RJJ) lifted from wine
1797 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1799 unsigned int colors
, size
, masks
= 0;
1801 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1803 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
1804 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1805 return sizeof(BITMAPCOREHEADER
) + colors
*
1806 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1808 else /* Assume BITMAPINFOHEADER */
1810 colors
= info
->bmiHeader
.biClrUsed
;
1811 if (colors
> 256) colors
= 256;
1812 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
1813 colors
= 1 << info
->bmiHeader
.biBitCount
;
1814 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
1815 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
1816 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1822 DIB_MapPaletteColors(PPALETTE ppalDc
, CONST BITMAPINFO
* lpbmi
)
1829 if (!(ppalDc
->flFlags
& PAL_INDEXED
))
1834 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1835 if (lpbmi
->bmiHeader
.biClrUsed
)
1837 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1840 ppalNew
= PALETTE_AllocPalWithHandle(PAL_INDEXED
, nNumColors
, NULL
, 0, 0, 0);
1841 if (ppalNew
== NULL
)
1843 DPRINT1("Could not allocate palette\n");
1847 lpIndex
= (USHORT
*)((PBYTE
)lpbmi
+ lpbmi
->bmiHeader
.biSize
);
1849 for (i
= 0; i
< nNumColors
; i
++)
1851 ULONG iColorIndex
= *lpIndex
% ppalDc
->NumColors
;
1852 ppalNew
->IndexedColors
[i
] = ppalDc
->IndexedColors
[iColorIndex
];
1856 hpal
= ppalNew
->BaseObject
.hHmgr
;
1857 PALETTE_UnlockPalette(ppalNew
);
1862 /* Converts a BITMAPCOREINFO to a BITMAPINFO structure,
1863 * or does nothing if it's already a BITMAPINFO (or V4 or V5) */
1866 DIB_ConvertBitmapInfo (CONST BITMAPINFO
* pbmi
, DWORD Usage
)
1868 CONST BITMAPCOREINFO
* pbmci
= (BITMAPCOREINFO
*)pbmi
;
1869 BITMAPINFO
* pNewBmi
;
1870 UINT numColors
= 0, ColorsSize
= 0;
1872 if(pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) return (BITMAPINFO
*)pbmi
;
1873 if(pbmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) return NULL
;
1875 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
1877 numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
1878 if(Usage
== DIB_PAL_COLORS
)
1880 ColorsSize
= numColors
* sizeof(WORD
);
1884 ColorsSize
= numColors
* sizeof(RGBQUAD
);
1887 else if (Usage
== DIB_PAL_COLORS
)
1889 /* Invalid at high-res */
1893 pNewBmi
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + ColorsSize
, TAG_DIB
);
1894 if(!pNewBmi
) return NULL
;
1896 RtlZeroMemory(pNewBmi
, sizeof(BITMAPINFOHEADER
) + ColorsSize
);
1898 pNewBmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1899 pNewBmi
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
1900 pNewBmi
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
1901 pNewBmi
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
1902 pNewBmi
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
1903 pNewBmi
->bmiHeader
.biCompression
= BI_RGB
;
1904 pNewBmi
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(pNewBmi
->bmiHeader
.biWidth
,
1905 pNewBmi
->bmiHeader
.biHeight
,
1906 pNewBmi
->bmiHeader
.biBitCount
);
1908 if(Usage
== DIB_PAL_COLORS
)
1910 RtlCopyMemory(pNewBmi
->bmiColors
, pbmci
->bmciColors
, ColorsSize
);
1915 for(i
=0; i
<numColors
; i
++)
1917 pNewBmi
->bmiColors
[i
].rgbRed
= pbmci
->bmciColors
[i
].rgbtRed
;
1918 pNewBmi
->bmiColors
[i
].rgbGreen
= pbmci
->bmciColors
[i
].rgbtGreen
;
1919 pNewBmi
->bmiColors
[i
].rgbBlue
= pbmci
->bmciColors
[i
].rgbtBlue
;
1926 /* Frees a BITMAPINFO created with DIB_ConvertBitmapInfo */
1929 DIB_FreeConvertedBitmapInfo(BITMAPINFO
* converted
, BITMAPINFO
* orig
)
1931 if(converted
!= orig
)
1932 ExFreePoolWithTag(converted
, TAG_DIB
);