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 },
113 CONST RGBQUAD
*Colors
)
121 if (!(dc
= DC_LockDc(hDC
))) return 0;
122 if (dc
->dctype
== DC_TYPE_INFO
)
128 psurf
= dc
->dclevel
.pSurface
;
132 EngSetLastError(ERROR_INVALID_PARAMETER
);
136 if (psurf
->hSecure
== NULL
)
139 EngSetLastError(ERROR_INVALID_PARAMETER
);
143 biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
144 if ((biBitCount
<= 8) && (StartIndex
< (1UL << biBitCount
)))
146 if (StartIndex
+ Entries
> (1UL << biBitCount
))
147 Entries
= (1 << biBitCount
) - StartIndex
;
149 if (psurf
->ppal
== NULL
)
152 EngSetLastError(ERROR_INVALID_HANDLE
);
156 PalGDI
= psurf
->ppal
;
158 for (Index
= StartIndex
;
159 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
162 PalGDI
->IndexedColors
[Index
].peRed
= Colors
[Index
- StartIndex
].rgbRed
;
163 PalGDI
->IndexedColors
[Index
].peGreen
= Colors
[Index
- StartIndex
].rgbGreen
;
164 PalGDI
->IndexedColors
[Index
].peBlue
= Colors
[Index
- StartIndex
].rgbBlue
;
170 /* Mark the brushes invalid */
171 dc
->pdcattr
->ulDirty_
|= DIRTY_FILL
|DIRTY_LINE
|DIRTY_BACKGROUND
|DIRTY_TEXT
;
189 UINT Index
, Count
= 0;
191 if (!(dc
= DC_LockDc(hDC
))) return 0;
192 if (dc
->dctype
== DC_TYPE_INFO
)
198 psurf
= dc
->dclevel
.pSurface
;
202 EngSetLastError(ERROR_INVALID_PARAMETER
);
206 if (psurf
->hSecure
== NULL
)
209 EngSetLastError(ERROR_INVALID_PARAMETER
);
216 if (ppal
->flFlags
& PAL_INDEXED
)
219 for (Index
= StartIndex
;
220 Index
< StartIndex
+ Entries
&& Index
< ppal
->NumColors
;
223 Colors
[Index
- StartIndex
].rgbRed
= ppal
->IndexedColors
[Index
].peRed
;
224 Colors
[Index
- StartIndex
].rgbGreen
= ppal
->IndexedColors
[Index
].peGreen
;
225 Colors
[Index
- StartIndex
].rgbBlue
= ppal
->IndexedColors
[Index
].peBlue
;
226 Colors
[Index
- StartIndex
].rgbReserved
= 0;
236 // Converts a DIB to a device-dependent bitmap
245 CONST BITMAPINFO
*bmi
,
248 HBITMAP SourceBitmap
;
249 PSURFACE psurfDst
, psurfSrc
;
254 HPALETTE hpalDIB
= 0;
255 PPALETTE ppalDIB
= 0;
257 SourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
260 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
261 bmi
->bmiHeader
.biCompression
),
262 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
263 bmi
->bmiHeader
.biSizeImage
,
268 DPRINT1("Error: Could not create a bitmap.\n");
269 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
273 psurfDst
= SURFACE_ShareLockSurface(hBitmap
);
274 psurfSrc
= SURFACE_ShareLockSurface(SourceBitmap
);
276 if(!(psurfSrc
&& psurfDst
))
278 DPRINT1("Error: Could not lock surfaces\n");
282 /* Create a palette for the DIB */
283 hpalDIB
= BuildDIBPalette(bmi
);
286 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
290 /* Lock the DIB palette */
291 ppalDIB
= PALETTE_ShareLockPalette(hpalDIB
);
294 EngSetLastError(ERROR_INVALID_HANDLE
);
298 /* Initialize EXLATEOBJ */
299 EXLATEOBJ_vInitialize(&exlo
,
302 RGB(0xff, 0xff, 0xff),
303 RGB(0xff, 0xff, 0xff), //DC->pdcattr->crBackgroundClr,
304 0); // DC->pdcattr->crForegroundClr);
306 rcDst
.top
= StartScan
;
308 rcDst
.bottom
= rcDst
.top
+ ScanLines
;
309 rcDst
.right
= psurfDst
->SurfObj
.sizlBitmap
.cx
;
313 result
= IntEngCopyBits(&psurfDst
->SurfObj
,
322 EXLATEOBJ_vCleanup(&exlo
);
325 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
326 if (hpalDIB
) GreDeleteObject(hpalDIB
);
327 if(psurfSrc
) SURFACE_ShareUnlockSurface(psurfSrc
);
328 if(psurfDst
) SURFACE_ShareUnlockSurface(psurfDst
);
329 GreDeleteObject(SourceBitmap
);
337 NtGdiSetDIBitsToDeviceInternal(
352 IN BOOL bTransformCoordinates
,
353 IN OPTIONAL HANDLE hcmXform
)
356 NTSTATUS Status
= STATUS_SUCCESS
;
358 HBITMAP hSourceBitmap
= NULL
;
359 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
;
366 PPALETTE ppalDIB
= NULL
;
367 HPALETTE hpalDIB
= NULL
;
368 LPBITMAPINFO pbmiSafe
;
372 pbmiSafe
= ExAllocatePoolWithTag(PagedPool
, cjMaxInfo
, 'pmTG');
373 if (!pbmiSafe
) return 0;
377 ProbeForRead(bmi
, cjMaxInfo
, 1);
378 ProbeForRead(Bits
, cjMaxBits
, 1);
379 RtlCopyMemory(pbmiSafe
, bmi
, cjMaxInfo
);
382 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
384 Status
= _SEH2_GetExceptionCode();
388 if (!NT_SUCCESS(Status
))
393 pDC
= DC_LockDc(hDC
);
396 EngSetLastError(ERROR_INVALID_HANDLE
);
399 if (pDC
->dctype
== DC_TYPE_INFO
)
405 pSurf
= pDC
->dclevel
.pSurface
;
407 pDestSurf
= pSurf
? &pSurf
->SurfObj
: NULL
;
409 ScanLines
= min(ScanLines
, abs(bmi
->bmiHeader
.biHeight
) - StartScan
);
413 if (bTransformCoordinates
)
415 IntLPtoDP(pDC
, (LPPOINT
)&rcDest
, 2);
417 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
418 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
419 rcDest
.right
= rcDest
.left
+ Width
;
420 rcDest
.bottom
= rcDest
.top
+ Height
;
421 rcDest
.top
+= StartScan
;
426 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
427 SourceSize
.cy
= ScanLines
;
429 //DIBWidth = WIDTH_BYTES_ALIGN32(SourceSize.cx, bmi->bmiHeader.biBitCount);
431 hSourceBitmap
= GreCreateBitmapEx(bmi
->bmiHeader
.biWidth
,
434 BitmapFormat(bmi
->bmiHeader
.biBitCount
,
435 bmi
->bmiHeader
.biCompression
),
436 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
437 bmi
->bmiHeader
.biSizeImage
,
443 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
444 Status
= STATUS_NO_MEMORY
;
448 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
451 Status
= STATUS_UNSUCCESSFUL
;
457 /* Create a palette for the DIB */
458 hpalDIB
= BuildDIBPalette(bmi
);
461 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
462 Status
= STATUS_NO_MEMORY
;
466 /* Lock the DIB palette */
467 ppalDIB
= PALETTE_ShareLockPalette(hpalDIB
);
470 EngSetLastError(ERROR_INVALID_HANDLE
);
471 Status
= STATUS_UNSUCCESSFUL
;
475 /* Initialize EXLATEOBJ */
476 EXLATEOBJ_vInitialize(&exlo
,
479 RGB(0xff, 0xff, 0xff),
480 pDC
->pdcattr
->crBackgroundClr
,
481 pDC
->pdcattr
->crForegroundClr
);
484 DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
485 rcDest
.left
, rcDest
.top
, rcDest
.right
, rcDest
.bottom
,
486 ptSource
.x
, ptSource
.y
, SourceSize
.cx
, SourceSize
.cy
);
487 Status
= IntEngBitBlt(pDestSurf
,
490 pDC
->rosdc
.CombinedClip
,
497 ROP4_FROM_INDEX(R3_OPINDEX_SRCCOPY
));
499 /* Cleanup EXLATEOBJ */
500 EXLATEOBJ_vCleanup(&exlo
);
503 if (NT_SUCCESS(Status
))
508 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
510 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
511 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
512 if (hpalDIB
) GreDeleteObject(hpalDIB
);
515 ExFreePool(pbmiSafe
);
520 /* Converts a device-dependent bitmap to a DIB */
523 NtGdiGetDIBitsInternal(
534 BITMAPCOREINFO
* pbmci
= NULL
;
535 PSURFACE psurf
= NULL
;
542 RGBTRIPLE
* rgbTriples
;
545 NTSTATUS Status
= STATUS_SUCCESS
;
547 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
549 if ((Usage
&& Usage
!= DIB_PAL_COLORS
) || !Info
|| !hBitmap
)
554 /* Probe for read and write */
555 ProbeForRead(Info
, MaxInfo
, 1);
556 ProbeForWrite(Info
, MaxInfo
, 1);
557 if (Bits
) ProbeForWrite(Bits
, MaxBits
, 1);
559 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
561 Status
= _SEH2_GetExceptionCode();
565 if (!NT_SUCCESS(Status
))
570 colorPtr
= (LPBYTE
)Info
+ Info
->bmiHeader
.biSize
;
571 rgbTriples
= colorPtr
;
574 bitmap_type
= DIB_GetBitmapInfo(&Info
->bmiHeader
,
581 if(bitmap_type
== -1)
583 DPRINT("Wrong bitmap format\n");
584 EngSetLastError(ERROR_INVALID_PARAMETER
);
587 else if(bitmap_type
== 0)
589 /* We need a BITMAPINFO to create a DIB, but we have to fill
590 * the BITMAPCOREINFO we're provided */
591 pbmci
= (BITMAPCOREINFO
*)Info
;
592 Info
= DIB_ConvertBitmapInfo((BITMAPINFO
*)pbmci
, Usage
);
595 DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
598 rgbQuads
= Info
->bmiColors
;
601 pDC
= DC_LockDc(hDC
);
602 if (pDC
== NULL
|| pDC
->dctype
== DC_TYPE_INFO
)
608 /* Get a pointer to the source bitmap object */
609 psurf
= SURFACE_ShareLockSurface(hBitmap
);
616 /* Fill in the structure */
619 case 0: /* Only info */
622 pbmci
->bmciHeader
.bcWidth
= (WORD
)psurf
->SurfObj
.sizlBitmap
.cx
;
623 pbmci
->bmciHeader
.bcHeight
= (WORD
)((psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
624 -psurf
->SurfObj
.sizlBitmap
.cy
:
625 psurf
->SurfObj
.sizlBitmap
.cy
);
626 pbmci
->bmciHeader
.bcPlanes
= 1;
627 pbmci
->bmciHeader
.bcBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
629 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
630 Info
->bmiHeader
.biHeight
= (psurf
->SurfObj
.fjBitmap
& BMF_TOPDOWN
) ?
631 -psurf
->SurfObj
.sizlBitmap
.cy
:
632 psurf
->SurfObj
.sizlBitmap
.cy
;;
633 Info
->bmiHeader
.biPlanes
= 1;
634 Info
->bmiHeader
.biBitCount
= BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
);
635 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes( Info
->bmiHeader
.biWidth
,
636 Info
->bmiHeader
.biHeight
,
637 Info
->bmiHeader
.biBitCount
);
640 switch(Info
->bmiHeader
.biBitCount
)
644 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
647 Info
->bmiHeader
.biCompression
= BI_RGB
;
651 else if(Info
->bmiHeader
.biBitCount
> 8)
653 Info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
657 Info
->bmiHeader
.biCompression
= BI_RGB
;
659 Info
->bmiHeader
.biXPelsPerMeter
= 0;
660 Info
->bmiHeader
.biYPelsPerMeter
= 0;
661 Info
->bmiHeader
.biClrUsed
= 0;
662 Info
->bmiHeader
.biClrImportant
= 0;
663 ScanLines
= abs(Info
->bmiHeader
.biHeight
);
669 Info
->bmiHeader
.biClrUsed
= 0;
671 /* If the bitmap if a DIB section and has the same format than what
672 * we're asked, go ahead! */
673 if((psurf
->hSecure
) &&
674 (BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
) == bpp
))
676 if(Usage
== DIB_RGB_COLORS
)
678 ULONG colors
= min(psurf
->ppal
->NumColors
, 256);
682 for(i
= 0; i
< colors
; i
++)
684 rgbTriples
[i
].rgbtRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
685 rgbTriples
[i
].rgbtGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
686 rgbTriples
[i
].rgbtBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
689 if(colors
!= 256) Info
->bmiHeader
.biClrUsed
= colors
;
690 for(i
= 0; i
< colors
; i
++)
692 rgbQuads
[i
].rgbRed
= psurf
->ppal
->IndexedColors
[i
].peRed
;
693 rgbQuads
[i
].rgbGreen
= psurf
->ppal
->IndexedColors
[i
].peGreen
;
694 rgbQuads
[i
].rgbBlue
= psurf
->ppal
->IndexedColors
[i
].peBlue
;
699 for(i
= 0; i
< 256; i
++)
701 if(pbmci
) ((WORD
*)rgbTriples
)[i
] = i
;
702 ((WORD
*)rgbQuads
)[i
] = i
;
708 if(Usage
== DIB_PAL_COLORS
)
710 for(i
= 0; i
< 256; i
++)
712 if(pbmci
) ((WORD
*)rgbTriples
)[i
] = i
;
713 ((WORD
*)rgbQuads
)[i
] = i
;
716 else if(bpp
> 1 && bpp
== BitsPerFormat(psurf
->SurfObj
.iBitmapFormat
))
718 /* For color DDBs in native depth (mono DDBs always have
719 a black/white palette):
720 Generate the color map from the selected palette */
721 PPALETTE pDcPal
= PALETTE_ShareLockPalette(pDC
->dclevel
.hpal
);
727 for (i
= 0; i
< pDcPal
->NumColors
; i
++)
731 rgbTriples
[i
].rgbtRed
= pDcPal
->IndexedColors
[i
].peRed
;
732 rgbTriples
[i
].rgbtGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
733 rgbTriples
[i
].rgbtBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
736 rgbQuads
[i
].rgbRed
= pDcPal
->IndexedColors
[i
].peRed
;
737 rgbQuads
[i
].rgbGreen
= pDcPal
->IndexedColors
[i
].peGreen
;
738 rgbQuads
[i
].rgbBlue
= pDcPal
->IndexedColors
[i
].peBlue
;
739 rgbQuads
[i
].rgbReserved
= 0;
741 PALETTE_ShareUnlockPalette(pDcPal
);
750 rgbTriples
[0].rgbtRed
= rgbTriples
[0].rgbtGreen
=
751 rgbTriples
[0].rgbtBlue
= 0;
752 rgbTriples
[1].rgbtRed
= rgbTriples
[1].rgbtGreen
=
753 rgbTriples
[1].rgbtBlue
= 0xff;
755 rgbQuads
[0].rgbRed
= rgbQuads
[0].rgbGreen
=
756 rgbQuads
[0].rgbBlue
= 0;
757 rgbQuads
[0].rgbReserved
= 0;
758 rgbQuads
[1].rgbRed
= rgbQuads
[1].rgbGreen
=
759 rgbQuads
[1].rgbBlue
= 0xff;
760 rgbQuads
[1].rgbReserved
= 0;
765 RtlCopyMemory(rgbTriples
, EGAColorsTriples
, sizeof(EGAColorsTriples
));
766 RtlCopyMemory(rgbQuads
, EGAColorsQuads
, sizeof(EGAColorsQuads
));
776 RGBTRIPLE
*colorTriple
;
778 RtlCopyMemory(rgbTriples
, DefLogPaletteTriples
,
779 10 * sizeof(RGBTRIPLE
));
780 RtlCopyMemory(rgbTriples
+ 246, DefLogPaletteTriples
+ 10,
781 10 * sizeof(RGBTRIPLE
));
782 colorTriple
= rgbTriples
+ 10;
783 for(r
= 0; r
<= 5; r
++) /* FIXME */
785 for(g
= 0; g
<= 5; g
++)
787 for(b
= 0; b
<= 5; b
++)
789 colorTriple
->rgbtRed
= (r
* 0xff) / 5;
790 colorTriple
->rgbtGreen
= (g
* 0xff) / 5;
791 colorTriple
->rgbtBlue
= (b
* 0xff) / 5;
797 memcpy(rgbQuads
, DefLogPaletteQuads
,
798 10 * sizeof(RGBQUAD
));
799 memcpy(rgbQuads
+ 246, DefLogPaletteQuads
+ 10,
800 10 * sizeof(RGBQUAD
));
801 color
= rgbQuads
+ 10;
802 for(r
= 0; r
<= 5; r
++) /* FIXME */
804 for(g
= 0; g
<= 5; g
++)
806 for(b
= 0; b
<= 5; b
++)
808 color
->rgbRed
= (r
* 0xff) / 5;
809 color
->rgbGreen
= (g
* 0xff) / 5;
810 color
->rgbBlue
= (b
* 0xff) / 5;
811 color
->rgbReserved
= 0;
823 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
825 ((PDWORD
)Info
->bmiColors
)[0] = 0x7c00;
826 ((PDWORD
)Info
->bmiColors
)[1] = 0x03e0;
827 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
832 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
836 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
837 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
838 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
842 ((PDWORD
)Info
->bmiColors
)[0] = 0xf800;
843 ((PDWORD
)Info
->bmiColors
)[1] = 0x07e0;
844 ((PDWORD
)Info
->bmiColors
)[2] = 0x001f;
851 if (Info
->bmiHeader
.biCompression
== BI_BITFIELDS
)
855 ((PDWORD
)Info
->bmiColors
)[0] = psurf
->ppal
->RedMask
;
856 ((PDWORD
)Info
->bmiColors
)[1] = psurf
->ppal
->GreenMask
;
857 ((PDWORD
)Info
->bmiColors
)[2] = psurf
->ppal
->BlueMask
;
861 ((PDWORD
)Info
->bmiColors
)[0] = 0xff0000;
862 ((PDWORD
)Info
->bmiColors
)[1] = 0x00ff00;
863 ((PDWORD
)Info
->bmiColors
)[2] = 0x0000ff;
868 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(width
, height
, bpp
);
870 if(Bits
&& ScanLines
)
872 /* Create a DIBSECTION, blt it, profit */
881 if (StartScan
> (ULONG
)psurf
->SurfObj
.sizlBitmap
.cy
)
888 ScanLines
= min(ScanLines
, psurf
->SurfObj
.sizlBitmap
.cy
- StartScan
);
892 Info
->bmiHeader
.biWidth
= psurf
->SurfObj
.sizlBitmap
.cx
;
893 Info
->bmiHeader
.biHeight
= (height
< 0) ?
894 -(LONG
)ScanLines
: ScanLines
;
896 hBmpDest
= DIB_CreateDIBSection(pDC
, Info
, Usage
, &pDIBits
, NULL
, 0, 0);
898 Info
->bmiHeader
.biWidth
= width
;
899 Info
->bmiHeader
.biHeight
= height
;
903 DPRINT1("Unable to create a DIB Section!\n");
904 EngSetLastError(ERROR_INVALID_PARAMETER
);
909 psurfDest
= SURFACE_ShareLockSurface(hBmpDest
);
913 rcDest
.bottom
= ScanLines
;
914 rcDest
.right
= psurf
->SurfObj
.sizlBitmap
.cx
;
922 if(ScanLines
<= StartScan
)
925 SURFACE_ShareUnlockSurface(psurfDest
);
926 GreDeleteObject(hBmpDest
);
930 ScanLines
-= StartScan
;
934 srcPoint
.y
= StartScan
;
937 EXLATEOBJ_vInitialize(&exlo
, psurf
->ppal
, psurfDest
->ppal
, 0xffffff, 0xffffff, 0);
939 ret
= IntEngCopyBits(&psurfDest
->SurfObj
,
946 SURFACE_ShareUnlockSurface(psurfDest
);
952 Status
= STATUS_SUCCESS
;
955 RtlCopyMemory(Bits
, pDIBits
, DIB_GetDIBImageBytes (width
, ScanLines
, bpp
));
957 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
959 Status
= _SEH2_GetExceptionCode();
963 if(!NT_SUCCESS(Status
))
965 DPRINT1("Unable to copy bits to the user provided pointer\n");
970 GreDeleteObject(hBmpDest
);
971 EXLATEOBJ_vCleanup(&exlo
);
973 else ScanLines
= abs(height
);
977 if(pDC
) DC_UnlockDc(pDC
);
978 if(psurf
) SURFACE_ShareUnlockSurface(psurf
);
979 if(pbmci
) DIB_FreeConvertedBitmapInfo(Info
, (BITMAPINFO
*)pbmci
);
984 #define ROP_TO_ROP4(Rop) ((Rop) >> 16)
989 NtGdiStretchDIBitsInternal(
999 IN OPTIONAL LPBYTE pjInit
,
1000 IN LPBITMAPINFO pbmi
,
1002 IN DWORD dwRop
, // MS ntgdi.h says dwRop4(?)
1007 BOOL bResult
= FALSE
;
1012 PSURFACE psurfTmp
= 0, psurfDst
= 0;
1013 HPALETTE hpalDIB
= 0;
1014 PPALETTE ppalDIB
= 0;
1018 if (!(pdc
= DC_LockDc(hdc
)))
1020 EngSetLastError(ERROR_INVALID_HANDLE
);
1024 /* Transform dest size */
1027 IntLPtoDP(pdc
, (POINTL
*)&sizel
, 1);
1030 /* Check if we can use NtGdiSetDIBitsToDeviceInternal */
1031 if (sizel
.cx
== cxSrc
&& sizel
.cy
== cySrc
&& dwRop
== SRCCOPY
)
1034 return NtGdiSetDIBitsToDeviceInternal(hdc
,
1052 pvBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, 'pmeT');
1060 ProbeForRead(pjInit
, cjMaxBits
, 1);
1061 RtlCopyMemory(pvBits
, pjInit
, cjMaxBits
);
1063 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1065 _SEH2_YIELD(return 0);
1069 /* FIXME: Locking twice is cheesy, coord tranlation in UM will fix it */
1070 if (!(pdc
= DC_LockDc(hdc
)))
1072 DPRINT1("Could not lock dc\n");
1073 EngSetLastError(ERROR_INVALID_HANDLE
);
1077 psurfDst
= pdc
->dclevel
.pSurface
;
1085 /* Calculate source and destination rect */
1088 rcSrc
.right
= xSrc
+ abs(cxSrc
);
1089 rcSrc
.bottom
= ySrc
+ abs(cySrc
);
1092 rcDst
.right
= rcDst
.left
+ cxDst
;
1093 rcDst
.bottom
= rcDst
.top
+ cyDst
;
1094 IntLPtoDP(pdc
, (POINTL
*)&rcDst
, 2);
1095 RECTL_vOffsetRect(&rcDst
, pdc
->ptlDCOrig
.x
, pdc
->ptlDCOrig
.y
);
1097 hbmTmp
= GreCreateBitmapEx(pbmi
->bmiHeader
.biWidth
,
1098 pbmi
->bmiHeader
.biHeight
,
1100 BitmapFormat(pbmi
->bmiHeader
.biBitCount
,
1101 pbmi
->bmiHeader
.biCompression
),
1102 pbmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
1103 pbmi
->bmiHeader
.biSizeImage
,
1113 psurfTmp
= SURFACE_ShareLockSurface(hbmTmp
);
1120 /* Create a palette for the DIB */
1121 hpalDIB
= BuildDIBPalette(pbmi
);
1128 /* Lock the DIB palette */
1129 ppalDIB
= PALETTE_ShareLockPalette(hpalDIB
);
1136 /* Initialize XLATEOBJ */
1137 EXLATEOBJ_vInitialize(&exlo
,
1140 RGB(0xff, 0xff, 0xff),
1141 pdc
->pdcattr
->crBackgroundClr
,
1142 pdc
->pdcattr
->crForegroundClr
);
1144 /* Prepare DC for blit */
1145 DC_vPrepareDCsForBlit(pdc
, rcDst
, NULL
, rcSrc
);
1147 /* Perform the stretch operation */
1148 bResult
= IntEngStretchBlt(&psurfDst
->SurfObj
,
1151 pdc
->rosdc
.CombinedClip
,
1156 &pdc
->eboFill
.BrushObject
,
1158 ROP_TO_ROP4(dwRop
));
1161 DC_vFinishBlit(pdc
, NULL
);
1162 EXLATEOBJ_vCleanup(&exlo
);
1164 if (ppalDIB
) PALETTE_ShareUnlockPalette(ppalDIB
);
1165 if (hpalDIB
) GreDeleteObject(hpalDIB
);
1166 if (psurfTmp
) SURFACE_ShareUnlockSurface(psurfTmp
);
1167 if (hbmTmp
) GreDeleteObject(hbmTmp
);
1168 if (pdc
) DC_UnlockDc(pdc
);
1169 ExFreePoolWithTag(pvBits
, 'pmeT');
1190 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
1191 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
1193 if (bpp
!= 1) fColor
= TRUE
;
1194 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
1197 const RGBQUAD
*rgb
= (RGBQUAD
*)((PBYTE
)data
+ data
->bmiHeader
.biSize
);
1198 DWORD col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1200 // Check if the first color of the colormap is black
1201 if ((col
== RGB(0, 0, 0)))
1204 col
= RGB(rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
1206 // If the second color is white, create a monochrome bitmap
1207 fColor
= (col
!= RGB(0xff,0xff,0xff));
1212 // Now create the bitmap
1215 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
1219 handle
= GreCreateBitmap(width
,
1229 if (NULL
!= handle
&& CBM_INIT
== init
)
1231 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
1237 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
1238 // The DDB that is created will be whatever bit depth your reference DC is
1241 NtGdiCreateDIBitmapInternal(
1246 IN OPTIONAL LPBYTE pjInit
,
1247 IN OPTIONAL LPBITMAPINFO pbmi
,
1249 IN UINT cjMaxInitInfo
,
1254 NTSTATUS Status
= STATUS_SUCCESS
;
1255 PBYTE safeBits
= NULL
;
1256 HBITMAP hbmResult
= NULL
;
1258 if(pjInit
&& (fInit
== CBM_INIT
))
1260 safeBits
= ExAllocatePoolWithTag(PagedPool
, cjMaxBits
, TAG_DIB
);
1263 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1270 if(pbmi
) ProbeForRead(pbmi
, cjMaxInitInfo
, 1);
1271 if(pjInit
&& (fInit
== CBM_INIT
))
1273 ProbeForRead(pjInit
, cjMaxBits
, 1);
1274 RtlCopyMemory(safeBits
, pjInit
, cjMaxBits
);
1277 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1279 Status
= _SEH2_GetExceptionCode();
1283 if(!NT_SUCCESS(Status
))
1285 SetLastNtError(Status
);
1289 hbmResult
= GreCreateDIBitmapInternal(hDc
,
1301 if (safeBits
) ExFreePoolWithTag(safeBits
, TAG_DIB
);
1307 GreCreateDIBitmapInternal(
1312 IN OPTIONAL LPBYTE pjInit
,
1313 IN OPTIONAL PBITMAPINFO pbmi
,
1324 if (!hDc
) /* 1bpp monochrome bitmap */
1326 // Should use System Bitmap DC hSystemBM, with CreateCompatibleDC for this.
1327 hdcDest
= NtGdiCreateCompatibleDC(0);
1338 Dc
= DC_LockDc(hdcDest
);
1341 EngSetLastError(ERROR_INVALID_HANDLE
);
1344 /* It's OK to set bpp=0 here, as IntCreateDIBitmap will create a compatible Bitmap
1345 * if bpp != 1 and ignore the real value that was passed */
1347 bpp
= pbmi
->bmiHeader
.biBitCount
;
1350 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, bpp
, fInit
, pjInit
, pbmi
, iUsage
);
1355 NtGdiDeleteObjectApp(hdcDest
);
1363 NtGdiCreateDIBSection(
1365 IN OPTIONAL HANDLE hSection
,
1371 IN ULONG_PTR dwColorSpace
,
1374 HBITMAP hbitmap
= 0;
1376 BOOL bDesktopDC
= FALSE
;
1377 NTSTATUS Status
= STATUS_SUCCESS
;
1379 if (!bmi
) return hbitmap
; // Make sure.
1383 ProbeForRead(&bmi
->bmiHeader
.biSize
, sizeof(DWORD
), 1);
1384 ProbeForRead(bmi
, bmi
->bmiHeader
.biSize
, 1);
1385 ProbeForRead(bmi
, DIB_BitmapInfoSize(bmi
, (WORD
)Usage
), 1);
1387 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1389 Status
= _SEH2_GetExceptionCode();
1393 if(!NT_SUCCESS(Status
))
1395 SetLastNtError(Status
);
1399 // If the reference hdc is null, take the desktop dc
1402 hDC
= NtGdiCreateCompatibleDC(0);
1406 if ((dc
= DC_LockDc(hDC
)))
1408 hbitmap
= DIB_CreateDIBSection(dc
,
1419 EngSetLastError(ERROR_INVALID_HANDLE
);
1423 NtGdiDeleteObjectApp(hDC
);
1430 DIB_CreateDIBSection(
1432 CONST BITMAPINFO
*bmi
,
1440 SURFACE
*bmp
= NULL
;
1441 void *mapBits
= NULL
;
1444 // Fill BITMAP32 structure with DIB data
1445 CONST BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1452 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1453 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1454 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1456 /* CreateDIBSection should fail for compressed formats */
1457 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1459 DPRINT1("no compressed format allowed\n");
1460 return (HBITMAP
)NULL
;
1463 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1465 bm
.bmWidth
= bi
->biWidth
;
1466 bm
.bmHeight
= effHeight
;
1467 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: WIDTH_BYTES_ALIGN32(bm
.bmWidth
, bi
->biBitCount
);
1469 bm
.bmPlanes
= bi
->biPlanes
;
1470 bm
.bmBitsPixel
= bi
->biBitCount
;
1473 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1474 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1475 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
&& bi
->biCompression
!= BI_BITFIELDS
1476 ? bi
->biSizeImage
: (ULONG
)(bm
.bmWidthBytes
* effHeight
);
1480 SYSTEM_BASIC_INFORMATION Sbi
;
1483 LARGE_INTEGER SectionOffset
;
1486 Status
= ZwQuerySystemInformation(SystemBasicInformation
,
1490 if (!NT_SUCCESS(Status
))
1492 DPRINT1("ZwQuerySystemInformation failed (0x%lx)\n", Status
);
1496 mapOffset
= offset
- (offset
% Sbi
.AllocationGranularity
);
1497 mapSize
= bi
->biSizeImage
+ (offset
- mapOffset
);
1499 SectionOffset
.LowPart
= mapOffset
;
1500 SectionOffset
.HighPart
= 0;
1502 Status
= ZwMapViewOfSection(section
,
1512 if (!NT_SUCCESS(Status
))
1514 DPRINT1("ZwMapViewOfSection failed (0x%lx)\n", Status
);
1515 EngSetLastError(ERROR_INVALID_PARAMETER
);
1519 if (mapBits
) bm
.bmBits
= (char *)mapBits
+ (offset
- mapOffset
);
1521 else if (ovr_pitch
&& offset
)
1522 bm
.bmBits
= (LPVOID
) offset
;
1526 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1529 DPRINT1("Failed to allocate memory\n");
1534 // hSecure = MmSecureVirtualMemory(bm.bmBits, totalSize, PAGE_READWRITE);
1535 hSecure
= (HANDLE
)0x1; // HACK OF UNIMPLEMENTED KERNEL STUFF !!!!
1537 if (usage
== DIB_PAL_COLORS
)
1542 ppalDc
= PALETTE_ShareLockPalette(dc
->dclevel
.hpal
);
1543 hpal
= DIB_MapPaletteColors(ppalDc
, bmi
);
1544 PALETTE_ShareUnlockPalette(ppalDc
);
1548 /* For DIB Brushes */
1549 DPRINT1("FIXME: Unsupported DIB_PAL_COLORS without a DC to map colors.\n");
1551 hpal
= (HPALETTE
) 0xFFFFFFFF;
1556 hpal
= BuildDIBPalette(bmi
);
1561 DPRINT1("Error: Could not create a palette for the DIB.\n");
1565 // Create Device Dependent Bitmap and add DIB pointer
1566 //Size.cx = bm.bmWidth;
1567 //Size.cy = abs(bm.bmHeight);
1568 res
= GreCreateBitmapEx(bm
.bmWidth
,
1571 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1572 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1573 ((bi
->biHeight
< 0) ? BMF_TOPDOWN
: 0),
1579 DPRINT1("GreCreateBitmapEx failed\n");
1580 EngSetLastError(ERROR_NO_SYSTEM_RESOURCES
);
1583 bmp
= SURFACE_ShareLockSurface(res
); // HACK
1586 DPRINT1("SURFACE_LockSurface failed\n");
1587 EngSetLastError(ERROR_INVALID_HANDLE
);
1591 /* WINE NOTE: WINE makes use of a colormap, which is a color translation
1592 table between the DIB and the X physical device. Obviously,
1593 this is left out of the ReactOS implementation. Instead,
1594 we call NtGdiSetDIBColorTable. */
1595 bmp
->hDIBSection
= section
;
1596 bmp
->hSecure
= hSecure
;
1597 bmp
->dwOffset
= offset
;
1598 bmp
->flags
= API_BITMAP
;
1599 bmp
->biClrImportant
= bi
->biClrImportant
;
1602 if(hpal
!= (HPALETTE
)0xFFFFFFFF)
1604 PPALETTE ppal
= PALETTE_ShareLockPalette(hpal
);
1608 if (bmp
->ppal
) PALETTE_ShareUnlockPalette(bmp
->ppal
);
1612 /* Lazy delete hpal, it will be freed at surface release */
1613 GreDeleteObject(hpal
);
1616 // Clean up in case of errors
1618 if (!res
|| !bmp
|| !bm
.bmBits
)
1620 DPRINT("Got an error res=%08x, bmp=%p, bm.bmBits=%p\n", res
, bmp
, bm
.bmBits
);
1623 // MmUnsecureVirtualMemory(hSecure); // FIXME: Implement this!
1626 ZwUnmapViewOfSection(NtCurrentProcess(), mapBits
);
1630 EngFreeUserMem(bm
.bmBits
), bm
.bmBits
= NULL
;
1638 GreDeleteObject(res
);
1645 SURFACE_ShareUnlockSurface(bmp
);
1648 // Return BITMAP handle and storage location
1649 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1657 /***********************************************************************
1660 * Get the info from a bitmap header.
1661 * Return 0 for COREHEADER, 1 for INFOHEADER, -1 for error.
1665 DIB_GetBitmapInfo( const BITMAPINFOHEADER
*header
, LONG
*width
,
1666 LONG
*height
, WORD
*planes
, WORD
*bpp
, DWORD
*compr
, DWORD
*size
)
1668 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1670 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)header
;
1671 *width
= core
->bcWidth
;
1672 *height
= core
->bcHeight
;
1673 *planes
= core
->bcPlanes
;
1674 *bpp
= core
->bcBitCount
;
1679 if (header
->biSize
>= sizeof(BITMAPINFOHEADER
)) /* Assume BITMAPINFOHEADER */
1681 *width
= header
->biWidth
;
1682 *height
= header
->biHeight
;
1683 *planes
= header
->biPlanes
;
1684 *bpp
= header
->biBitCount
;
1685 *compr
= header
->biCompression
;
1686 *size
= header
->biSizeImage
;
1689 DPRINT1("(%d): unknown/wrong size for header\n", header
->biSize
);
1693 /***********************************************************************
1694 * DIB_GetDIBImageBytes
1696 * Return the number of bytes used to hold the image in a DIB bitmap.
1697 * 11/16/1999 (RJJ) lifted from wine
1700 INT APIENTRY
DIB_GetDIBImageBytes(INT width
, INT height
, INT depth
)
1702 return WIDTH_BYTES_ALIGN32(width
, depth
) * (height
< 0 ? -height
: height
);
1705 /***********************************************************************
1706 * DIB_BitmapInfoSize
1708 * Return the size of the bitmap info structure including color table.
1709 * 11/16/1999 (RJJ) lifted from wine
1712 INT FASTCALL
DIB_BitmapInfoSize(const BITMAPINFO
* info
, WORD coloruse
)
1714 unsigned int colors
, size
, masks
= 0;
1716 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1718 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
1719 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1720 return sizeof(BITMAPCOREHEADER
) + colors
*
1721 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1723 else /* Assume BITMAPINFOHEADER */
1725 colors
= info
->bmiHeader
.biClrUsed
;
1726 if (colors
> 256) colors
= 256;
1727 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
1728 colors
= 1 << info
->bmiHeader
.biBitCount
;
1729 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
1730 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
1731 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1737 DIB_MapPaletteColors(PPALETTE ppalDc
, CONST BITMAPINFO
* lpbmi
)
1744 if (!(ppalDc
->flFlags
& PAL_INDEXED
))
1749 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1750 if (lpbmi
->bmiHeader
.biClrUsed
)
1752 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1755 ppalNew
= PALETTE_AllocPalWithHandle(PAL_INDEXED
, nNumColors
, NULL
, 0, 0, 0);
1756 if (ppalNew
== NULL
)
1758 DPRINT1("Could not allocate palette\n");
1762 lpIndex
= (USHORT
*)((PBYTE
)lpbmi
+ lpbmi
->bmiHeader
.biSize
);
1764 for (i
= 0; i
< nNumColors
; i
++)
1766 ULONG iColorIndex
= *lpIndex
% ppalDc
->NumColors
;
1767 ppalNew
->IndexedColors
[i
] = ppalDc
->IndexedColors
[iColorIndex
];
1771 hpal
= ppalNew
->BaseObject
.hHmgr
;
1772 PALETTE_UnlockPalette(ppalNew
);
1779 BuildDIBPalette(CONST BITMAPINFO
*bmi
)
1785 ULONG RedMask
= 0, GreenMask
= 0, BlueMask
= 0;
1786 PDWORD pdwColors
= (PDWORD
)((PBYTE
)bmi
+ bmi
->bmiHeader
.biSize
);
1787 ULONG paletteType
, i
;
1789 // Determine Bits Per Pixel
1790 bits
= bmi
->bmiHeader
.biBitCount
;
1792 // Determine paletteType from Bits Per Pixel
1795 paletteType
= PAL_INDEXED
;
1796 RedMask
= GreenMask
= BlueMask
= 0;
1798 else if (bmi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
1800 paletteType
= PAL_BITFIELDS
;
1801 if (bmi
->bmiHeader
.biSize
>= sizeof(BITMAPV4HEADER
))
1803 PBITMAPV4HEADER pV4Header
= (PBITMAPV4HEADER
)&bmi
->bmiHeader
;
1804 RedMask
= pV4Header
->bV4RedMask
;
1805 GreenMask
= pV4Header
->bV4GreenMask
;
1806 BlueMask
= pV4Header
->bV4BlueMask
;
1810 RedMask
= pdwColors
[0];
1811 GreenMask
= pdwColors
[1];
1812 BlueMask
= pdwColors
[2];
1817 paletteType
= PAL_BITFIELDS
;
1821 paletteType
|= PAL_RGB16_555
;
1829 paletteType
|= PAL_BGR
;
1831 GreenMask
= 0x00FF00;
1832 BlueMask
= 0x0000FF;
1837 if (bmi
->bmiHeader
.biClrUsed
== 0)
1839 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1843 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1846 if (paletteType
== PAL_INDEXED
)
1848 RGBQUAD
* pColors
= (RGBQUAD
*)((PBYTE
)bmi
+ bmi
->bmiHeader
.biSize
);
1850 /* Allocate a palette */
1851 ppal
= PALETTE_AllocPalWithHandle(PAL_INDEXED
,
1855 if (!ppal
) return NULL
;
1857 /* Copy all colors */
1858 for (i
= 0; i
< ColorCount
; i
++)
1860 ppal
->IndexedColors
[i
].peRed
= pColors
[i
].rgbRed
;
1861 ppal
->IndexedColors
[i
].peGreen
= pColors
[i
].rgbGreen
;
1862 ppal
->IndexedColors
[i
].peBlue
= pColors
[i
].rgbBlue
;
1863 ppal
->IndexedColors
[i
].peFlags
= 0;
1866 /* Get palette handle and unlock the palette */
1867 hpal
= ppal
->BaseObject
.hHmgr
;
1868 PALETTE_UnlockPalette(ppal
);
1872 ppal
= PALETTE_AllocPalWithHandle(paletteType
, 0,
1874 RedMask
, GreenMask
, BlueMask
);
1876 hpal
= ppal
->BaseObject
.hHmgr
;
1877 PALETTE_UnlockPalette(ppal
);
1883 /* Converts a BITMAPCOREINFO to a BITMAPINFO structure,
1884 * or does nothing if it's already a BITMAPINFO (or V4 or V5) */
1887 DIB_ConvertBitmapInfo (CONST BITMAPINFO
* pbmi
, DWORD Usage
)
1889 CONST BITMAPCOREINFO
* pbmci
= (BITMAPCOREINFO
*)pbmi
;
1890 BITMAPINFO
* pNewBmi
;
1891 UINT numColors
= 0, ColorsSize
= 0;
1893 if(pbmi
->bmiHeader
.biSize
>= sizeof(BITMAPINFOHEADER
)) return (BITMAPINFO
*)pbmi
;
1894 if(pbmi
->bmiHeader
.biSize
!= sizeof(BITMAPCOREHEADER
)) return NULL
;
1896 if(pbmci
->bmciHeader
.bcBitCount
<= 8)
1898 numColors
= 1 << pbmci
->bmciHeader
.bcBitCount
;
1899 if(Usage
== DIB_PAL_COLORS
)
1901 ColorsSize
= numColors
* sizeof(WORD
);
1905 ColorsSize
= numColors
* sizeof(RGBQUAD
);
1908 else if (Usage
== DIB_PAL_COLORS
)
1910 /* Invalid at high-res */
1914 pNewBmi
= ExAllocatePoolWithTag(PagedPool
, sizeof(BITMAPINFOHEADER
) + ColorsSize
, TAG_DIB
);
1915 if(!pNewBmi
) return NULL
;
1917 RtlZeroMemory(pNewBmi
, sizeof(BITMAPINFOHEADER
) + ColorsSize
);
1919 pNewBmi
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1920 pNewBmi
->bmiHeader
.biBitCount
= pbmci
->bmciHeader
.bcBitCount
;
1921 pNewBmi
->bmiHeader
.biWidth
= pbmci
->bmciHeader
.bcWidth
;
1922 pNewBmi
->bmiHeader
.biHeight
= pbmci
->bmciHeader
.bcHeight
;
1923 pNewBmi
->bmiHeader
.biPlanes
= pbmci
->bmciHeader
.bcPlanes
;
1924 pNewBmi
->bmiHeader
.biCompression
= BI_RGB
;
1925 pNewBmi
->bmiHeader
.biSizeImage
= DIB_GetDIBImageBytes(pNewBmi
->bmiHeader
.biWidth
,
1926 pNewBmi
->bmiHeader
.biHeight
,
1927 pNewBmi
->bmiHeader
.biBitCount
);
1929 if(Usage
== DIB_PAL_COLORS
)
1931 RtlCopyMemory(pNewBmi
->bmiColors
, pbmci
->bmciColors
, ColorsSize
);
1936 for(i
=0; i
<numColors
; i
++)
1938 pNewBmi
->bmiColors
[i
].rgbRed
= pbmci
->bmciColors
[i
].rgbtRed
;
1939 pNewBmi
->bmiColors
[i
].rgbGreen
= pbmci
->bmciColors
[i
].rgbtGreen
;
1940 pNewBmi
->bmiColors
[i
].rgbBlue
= pbmci
->bmciColors
[i
].rgbtBlue
;
1947 /* Frees a BITMAPINFO created with DIB_ConvertBitmapInfo */
1950 DIB_FreeConvertedBitmapInfo(BITMAPINFO
* converted
, BITMAPINFO
* orig
)
1952 if(converted
!= orig
)
1953 ExFreePoolWithTag(converted
, TAG_DIB
);