4 * ReactOS W32 Subsystem
5 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 IntSetDIBColorTable(HDC hDC
, UINT StartIndex
, UINT Entries
, CONST RGBQUAD
*Colors
)
35 if (!(dc
= DC_LockDc(hDC
))) return 0;
36 if (dc
->DC_Type
== DC_TYPE_INFO
)
42 BitmapObj
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
43 if (BitmapObj
== NULL
)
46 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
50 if (BitmapObj
->dib
== NULL
)
52 BITMAPOBJ_UnlockBitmap(BitmapObj
);
54 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
58 if (BitmapObj
->dib
->dsBmih
.biBitCount
<= 8 &&
59 StartIndex
< (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
61 if (StartIndex
+ Entries
> (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
62 Entries
= (1 << BitmapObj
->dib
->dsBmih
.biBitCount
) - StartIndex
;
64 PalGDI
= PALETTE_LockPalette(BitmapObj
->hDIBPalette
);
67 BITMAPOBJ_UnlockBitmap(BitmapObj
);
69 SetLastWin32Error(ERROR_INVALID_HANDLE
);
73 for (Index
= StartIndex
;
74 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
77 PalGDI
->IndexedColors
[Index
].peRed
= Colors
[Index
- StartIndex
].rgbRed
;
78 PalGDI
->IndexedColors
[Index
].peGreen
= Colors
[Index
- StartIndex
].rgbGreen
;
79 PalGDI
->IndexedColors
[Index
].peBlue
= Colors
[Index
- StartIndex
].rgbBlue
;
81 PALETTE_UnlockPalette(PalGDI
);
86 BITMAPOBJ_UnlockBitmap(BitmapObj
);
93 IntGetDIBColorTable(HDC hDC
, UINT StartIndex
, UINT Entries
, RGBQUAD
*Colors
)
100 if (!(dc
= DC_LockDc(hDC
))) return 0;
101 if (dc
->DC_Type
== DC_TYPE_INFO
)
107 BitmapObj
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
108 if (BitmapObj
== NULL
)
111 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
115 if (BitmapObj
->dib
== NULL
)
117 BITMAPOBJ_UnlockBitmap(BitmapObj
);
119 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
123 if (BitmapObj
->dib
->dsBmih
.biBitCount
<= 8 &&
124 StartIndex
< (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
126 if (StartIndex
+ Entries
> (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
127 Entries
= (1 << BitmapObj
->dib
->dsBmih
.biBitCount
) - StartIndex
;
129 PalGDI
= PALETTE_LockPalette(BitmapObj
->hDIBPalette
);
132 BITMAPOBJ_UnlockBitmap(BitmapObj
);
134 SetLastWin32Error(ERROR_INVALID_HANDLE
);
138 for (Index
= StartIndex
;
139 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
142 Colors
[Index
- StartIndex
].rgbRed
= PalGDI
->IndexedColors
[Index
].peRed
;
143 Colors
[Index
- StartIndex
].rgbGreen
= PalGDI
->IndexedColors
[Index
].peGreen
;
144 Colors
[Index
- StartIndex
].rgbBlue
= PalGDI
->IndexedColors
[Index
].peBlue
;
145 Colors
[Index
- StartIndex
].rgbReserved
= 0;
147 PALETTE_UnlockPalette(PalGDI
);
152 BITMAPOBJ_UnlockBitmap(BitmapObj
);
158 // Converts a DIB to a device-dependent bitmap
166 CONST BITMAPINFO
*bmi
,
170 HBITMAP SourceBitmap
;
173 SURFOBJ
*DestSurf
, *SourceSurf
;
180 HPALETTE DDB_Palette
, DIB_Palette
;
181 ULONG DDB_Palette_Type
, DIB_Palette_Type
;
185 if (!(bitmap
= BITMAPOBJ_LockBitmap(hBitmap
)))
191 //if (ColorUse == DIB_PAL_COLORS)
192 // lpRGB = DIB_MapPaletteColors(hDC, bmi);
194 // lpRGB = &bmi->bmiColors;
196 DestSurf
= &bitmap
->SurfObj
;
198 // Create source surface
199 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
200 SourceSize
.cy
= ScanLines
;
202 // Determine width of DIB
203 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
);
205 SourceBitmap
= EngCreateBitmap(SourceSize
,
207 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
208 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
210 if (0 == SourceBitmap
)
212 BITMAPOBJ_UnlockBitmap(bitmap
);
213 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
217 SourceSurf
= EngLockSurface((HSURF
)SourceBitmap
);
218 if (NULL
== SourceSurf
)
220 EngDeleteSurface((HSURF
)SourceBitmap
);
221 BITMAPOBJ_UnlockBitmap(bitmap
);
222 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
226 // Destination palette obtained from the hDC
227 hDCPalette
= PALETTE_LockPalette(((GDIDEVICE
*)DC
->pPDev
)->DevInfo
.hpalDefault
);
228 if (NULL
== hDCPalette
)
230 EngUnlockSurface(SourceSurf
);
231 EngDeleteSurface((HSURF
)SourceBitmap
);
232 BITMAPOBJ_UnlockBitmap(bitmap
);
233 SetLastWin32Error(ERROR_INVALID_HANDLE
);
236 DDB_Palette_Type
= hDCPalette
->Mode
;
237 DDB_Palette
= ((GDIDEVICE
*)DC
->pPDev
)->DevInfo
.hpalDefault
;
238 PALETTE_UnlockPalette(hDCPalette
);
240 // Source palette obtained from the BITMAPINFO
241 DIB_Palette
= BuildDIBPalette ( (PBITMAPINFO
)bmi
, (PINT
)&DIB_Palette_Type
);
242 if (NULL
== DIB_Palette
)
244 EngUnlockSurface(SourceSurf
);
245 EngDeleteSurface((HSURF
)SourceBitmap
);
246 BITMAPOBJ_UnlockBitmap(bitmap
);
247 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
251 // Determine XLATEOBJ for color translation
252 XlateObj
= IntEngCreateXlate(DDB_Palette_Type
, DIB_Palette_Type
, DDB_Palette
, DIB_Palette
);
253 if (NULL
== XlateObj
)
255 PALETTE_FreePaletteByHandle(DIB_Palette
);
256 EngUnlockSurface(SourceSurf
);
257 EngDeleteSurface((HSURF
)SourceBitmap
);
258 BITMAPOBJ_UnlockBitmap(bitmap
);
259 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
267 // Determine destination rectangle
269 DestRect
.top
= abs(bmi
->bmiHeader
.biHeight
) - StartScan
- ScanLines
;
270 DestRect
.right
= SourceSize
.cx
;
271 DestRect
.bottom
= DestRect
.top
+ ScanLines
;
273 copyBitsResult
= EngCopyBits(DestSurf
, SourceSurf
, NULL
, XlateObj
, &DestRect
, &ZeroPoint
);
275 // If it succeeded, return number of scanlines copies
276 if(copyBitsResult
== TRUE
)
278 result
= SourceSize
.cy
- 1;
282 EngDeleteXlate(XlateObj
);
283 PALETTE_FreePaletteByHandle(DIB_Palette
);
284 EngUnlockSurface(SourceSurf
);
285 EngDeleteSurface((HSURF
)SourceBitmap
);
287 // if (ColorUse == DIB_PAL_COLORS)
288 // WinFree((LPSTR)lpRGB);
290 BITMAPOBJ_UnlockBitmap(bitmap
);
295 // Converts a DIB to a device-dependent bitmap
303 CONST BITMAPINFO
*bmi
,
312 SetLastWin32Error(ERROR_INVALID_HANDLE
);
315 if (Dc
->DC_Type
== DC_TYPE_INFO
)
321 Ret
= IntSetDIBits(Dc
, hBitmap
, StartScan
, ScanLines
, Bits
, bmi
, ColorUse
);
333 NtGdiSetDIBitsToDeviceInternal(
348 IN BOOL bTransformCoordinates
,
349 IN OPTIONAL HANDLE hcmXform
353 NTSTATUS Status
= STATUS_SUCCESS
;
355 HBITMAP hSourceBitmap
= NULL
;
356 SURFOBJ
*pDestSurf
, *pSourceSurf
= NULL
;
361 XLATEOBJ
*XlateObj
= NULL
;
363 HPALETTE DDBPalette
, DIBPalette
= NULL
;
364 ULONG DDBPaletteType
, DIBPaletteType
;
368 pDC
= DC_LockDc(hDC
);
371 SetLastWin32Error(ERROR_INVALID_HANDLE
);
374 if (pDC
->DC_Type
== DC_TYPE_INFO
)
380 pDestSurf
= EngLockSurface((HSURF
)pDC
->w
.hBitmap
);
383 /* FIXME: SetLastError ? */
390 if (bTransformCoordinates
)
392 CoordLPtoDP(pDC
, (LPPOINT
)&rcDest
);
394 rcDest
.left
+= pDC
->ptlDCOrig
.x
;
395 rcDest
.top
+= pDC
->ptlDCOrig
.y
;
396 rcDest
.right
= rcDest
.left
+ Width
;
397 rcDest
.bottom
= rcDest
.top
+ Height
;
401 /* Enter SEH, as the bits are user mode */
404 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
405 SourceSize
.cy
= ScanLines
;
406 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
);
408 ProbeForRead(Bits
, DIBWidth
* abs(bmi
->bmiHeader
.biHeight
), 1);
409 hSourceBitmap
= EngCreateBitmap(SourceSize
,
411 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
412 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
416 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
417 Status
= STATUS_NO_MEMORY
;
421 pSourceSurf
= EngLockSurface((HSURF
)hSourceBitmap
);
424 Status
= STATUS_UNSUCCESSFUL
;
429 /* Obtain destination palette from the DC */
430 pDCPalette
= PALETTE_LockPalette(((GDIDEVICE
*)pDC
->pPDev
)->DevInfo
.hpalDefault
);
433 SetLastWin32Error(ERROR_INVALID_HANDLE
);
434 Status
= STATUS_UNSUCCESSFUL
;
437 DDBPaletteType
= pDCPalette
->Mode
;
438 DDBPalette
= ((GDIDEVICE
*)pDC
->pPDev
)->DevInfo
.hpalDefault
;
439 PALETTE_UnlockPalette(pDCPalette
);
441 DIBPalette
= BuildDIBPalette(bmi
, (PINT
)&DIBPaletteType
);
444 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
445 Status
= STATUS_NO_MEMORY
;
449 /* Determine XlateObj */
450 XlateObj
= IntEngCreateXlate(DDBPaletteType
, DIBPaletteType
, DDBPalette
, DIBPalette
);
453 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
454 Status
= STATUS_NO_MEMORY
;
459 Status
= IntEngBitBlt(pDestSurf
,
469 ROP3_TO_ROP4(SRCCOPY
));
474 Status
= _SEH_GetExceptionCode();
478 if (NT_SUCCESS(Status
))
480 /* FIXME: Should probably be only the number of lines actually copied */
484 if (pSourceSurf
) EngUnlockSurface(pSourceSurf
);
485 if (hSourceBitmap
) EngDeleteSurface((HSURF
)hSourceBitmap
);
486 if (XlateObj
) EngDeleteXlate(XlateObj
);
487 if (DIBPalette
) PALETTE_FreePaletteByHandle(DIBPalette
);
488 EngUnlockSurface(pDestSurf
);
495 /* Converts a device-dependent bitmap to a DIB */
497 NtGdiGetDIBitsInternal(HDC hDC
,
508 BITMAPOBJ
*BitmapObj
= NULL
;
509 HBITMAP hDestBitmap
= NULL
;
510 HPALETTE hSourcePalette
= NULL
;
511 HPALETTE hDestPalette
= NULL
;
512 PPALGDI SourcePalette
= NULL
;
513 PPALGDI DestPalette
= NULL
;
514 NTSTATUS Status
= STATUS_SUCCESS
;
516 BOOL bPaletteMatch
= FALSE
;
518 DPRINT("Entered NtGdiGetDIBitsInternal()\n");
520 /* Get handle for the palette in DC. */
522 if (Dc
== NULL
) return 0;
523 if (Dc
->DC_Type
== DC_TYPE_INFO
)
528 /* Source palette obtained from the windows hdc */
529 hSourcePalette
= Dc
->DcLevel
.hpal
;
532 /* don't do anything if we fail this */
533 if (Usage
!= DIB_RGB_COLORS
&& Usage
!= DIB_PAL_COLORS
)
536 /* Get a pointer to the source bitmap object */
537 BitmapObj
= BITMAPOBJ_LockBitmap(hBitmap
);
538 if (BitmapObj
== NULL
)
541 /* fill out the BITMAPINFO struct */
546 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
548 BITMAPCOREHEADER
* coreheader
= (BITMAPCOREHEADER
*) Info
;
550 ProbeForWrite(Info
, sizeof(BITMAPINFO
), 1);
552 coreheader
= (BITMAPCOREHEADER
*) Info
;
553 coreheader
->bcWidth
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
554 coreheader
->bcPlanes
= 1;
555 coreheader
->bcBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
556 /* Resulting height may be smaller than original height */
557 coreheader
->bcHeight
= min(ScanLines
, BitmapObj
->SurfObj
.sizlBitmap
.cy
- StartScan
);
558 coreheader
->bcSize
= DIB_GetDIBWidthBytes(coreheader
->bcWidth
,
559 coreheader
->bcBitCount
) * coreheader
->bcHeight
;
560 if (BitmapObj
->SurfObj
.lDelta
> 0)
561 coreheader
->bcHeight
= -coreheader
->bcHeight
;
564 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
566 ProbeForWrite(Info
, sizeof(BITMAPINFO
), 1);
568 Info
->bmiHeader
.biWidth
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
569 /* Resulting height may be smaller than original height */
570 Info
->bmiHeader
.biHeight
= min(ScanLines
, BitmapObj
->SurfObj
.sizlBitmap
.cy
- StartScan
);
571 Info
->bmiHeader
.biPlanes
= 1;
572 Info
->bmiHeader
.biBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
573 switch (BitmapObj
->SurfObj
.iBitmapFormat
)
581 Info
->bmiHeader
.biCompression
= BI_RGB
;
584 Info
->bmiHeader
.biCompression
= BI_RLE4
;
587 Info
->bmiHeader
.biCompression
= BI_RLE8
;
590 Info
->bmiHeader
.biCompression
= BI_JPEG
;
593 Info
->bmiHeader
.biCompression
= BI_PNG
;
596 /* Image size has to be calculated */
597 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBWidthBytes(Info
->bmiHeader
.biWidth
,
598 Info
->bmiHeader
.biBitCount
) * Info
->bmiHeader
.biHeight
;
599 Info
->bmiHeader
.biXPelsPerMeter
= 0; /* FIXME */
600 Info
->bmiHeader
.biYPelsPerMeter
= 0; /* FIXME */
601 Info
->bmiHeader
.biClrUsed
= 0;
602 Info
->bmiHeader
.biClrImportant
= 1 << Info
->bmiHeader
.biBitCount
; /* FIXME */
603 /* Report negtive height for top-down bitmaps. */
604 if (BitmapObj
->SurfObj
.lDelta
> 0)
605 Info
->bmiHeader
.biHeight
= -Info
->bmiHeader
.biHeight
;
610 Status
= _SEH_GetExceptionCode();
614 if (NT_SUCCESS(Status
))
616 Result
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
622 ULONG SourcePaletteType
= 0;
623 ULONG DestPaletteType
;
629 ProbeForRead(Info
, sizeof(BITMAPINFO
), 1);
631 if (Info
->bmiHeader
.biBitCount
== BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
))
633 hDestPalette
= hSourcePalette
;
634 bPaletteMatch
= TRUE
;
637 hDestPalette
= BuildDIBPalette(Info
, (PINT
)&DestPaletteType
); //hDestPalette = Dc->DevInfo->hpalDefault;
639 SourcePalette
= PALETTE_LockPalette(hSourcePalette
);
640 /* FIXME - SourcePalette can be NULL!!! Don't assert here! */
641 ASSERT(SourcePalette
);
642 SourcePaletteType
= SourcePalette
->Mode
;
643 PALETTE_UnlockPalette(SourcePalette
);
647 DestPalette
= PALETTE_LockPalette(hDestPalette
);
648 /* FIXME - DestPalette can be NULL!!!! Don't assert here!!! */
649 DPRINT("DestPalette : %p\n", DestPalette
);
651 DestPaletteType
= DestPalette
->Mode
;
655 DestPalette
= SourcePalette
;
659 /* FIXME: This is largely incomplete. */
660 if (Info
->bmiHeader
.biBitCount
<= 8)
662 if (Usage
== DIB_RGB_COLORS
)
665 Index
< (1 << Info
->bmiHeader
.biBitCount
) && Index
< DestPalette
->NumColors
;
668 Info
->bmiColors
[Index
].rgbRed
= DestPalette
->IndexedColors
[Index
].peRed
;
669 Info
->bmiColors
[Index
].rgbGreen
= DestPalette
->IndexedColors
[Index
].peGreen
;
670 Info
->bmiColors
[Index
].rgbBlue
= DestPalette
->IndexedColors
[Index
].peBlue
;
671 Info
->bmiColors
[Index
].rgbReserved
= 0;
675 if (Usage
== DIB_PAL_COLORS
)
677 DPRINT1("GetDIBits with DIB_PAL_COLORS isn't implemented yet\n");
682 PALETTE_UnlockPalette(DestPalette
);
684 /* Create the destination bitmap to for the copy operation */
685 if (StartScan
> BitmapObj
->SurfObj
.sizlBitmap
.cy
)
687 _SEH_YIELD(goto cleanup
);
691 ScanLines
= min(ScanLines
, BitmapObj
->SurfObj
.sizlBitmap
.cy
- StartScan
);
692 DestSize
.cx
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
693 DestSize
.cy
= ScanLines
;
697 ProbeForWrite(Bits
, BitmapObj
->SurfObj
.cjBits
, 1);
699 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
701 BITMAPCOREHEADER
* coreheader
= (BITMAPCOREHEADER
*) Info
;
702 coreheader
->bcSize
= DIB_GetDIBWidthBytes(DestSize
.cx
,
703 coreheader
->bcBitCount
) * DestSize
.cy
;
705 hDestBitmap
= EngCreateBitmap(DestSize
,
706 DIB_GetDIBWidthBytes(DestSize
.cx
, coreheader
->bcBitCount
),
707 BitmapFormat(coreheader
->bcBitCount
, BI_RGB
),
708 0 < coreheader
->bcHeight
? 0 : BMF_TOPDOWN
,
712 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
714 Info
->bmiHeader
.biSizeImage
= DIB_GetDIBWidthBytes(DestSize
.cx
,
715 Info
->bmiHeader
.biBitCount
) * DestSize
.cy
;
717 hDestBitmap
= EngCreateBitmap(DestSize
,
718 DIB_GetDIBWidthBytes(DestSize
.cx
, Info
->bmiHeader
.biBitCount
),
719 BitmapFormat(Info
->bmiHeader
.biBitCount
, Info
->bmiHeader
.biCompression
),
720 0 < Info
->bmiHeader
.biHeight
? 0 : BMF_TOPDOWN
,
724 if (hDestBitmap
== NULL
)
725 _SEH_YIELD(goto cleanup
);
730 Status
= _SEH_GetExceptionCode();
734 if (NT_SUCCESS(Status
))
737 SURFOBJ
*DestSurfObj
;
740 XlateObj
= IntEngCreateXlate(DestPaletteType
,
746 SourcePoint
.y
= BitmapObj
->SurfObj
.sizlBitmap
.cy
- (StartScan
+ ScanLines
);
748 /* Determine destination rectangle */
751 DestRect
.right
= DestSize
.cx
;
752 DestRect
.bottom
= DestSize
.cy
;
754 DestSurfObj
= EngLockSurface((HSURF
)hDestBitmap
);
756 if (EngCopyBits(DestSurfObj
,
766 EngDeleteXlate(XlateObj
);
767 EngUnlockSurface(DestSurfObj
);
772 if (hDestBitmap
!= NULL
)
773 EngDeleteSurface((HSURF
)hDestBitmap
);
775 if (hDestPalette
!= NULL
&& bPaletteMatch
== FALSE
)
776 PALETTE_FreePaletteByHandle(hDestPalette
);
778 BITMAPOBJ_UnlockBitmap(BitmapObj
);
780 DPRINT("leaving NtGdiGetDIBitsInternal\n");
787 NtGdiStretchDIBitsInternal(
798 LPBITMAPINFO BitsInfo
,
805 HBITMAP hBitmap
, hOldBitmap
= NULL
;
807 HPALETTE hPal
= NULL
;
810 if (!Bits
|| !BitsInfo
)
812 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
816 hdcMem
= NtGdiCreateCompatibleDC(hDC
);
819 DPRINT1("NtGdiCreateCompatibleDC fail create hdc\n");
823 hBitmap
= NtGdiCreateCompatibleBitmap(hDC
, BitsInfo
->bmiHeader
.biWidth
,
824 BitsInfo
->bmiHeader
.biHeight
);
827 DPRINT1("NtGdiCreateCompatibleBitmap fail create bitmap\n");
828 DPRINT1("hDC : 0x%08x \n", hDC
);
829 DPRINT1("BitsInfo->bmiHeader.biWidth : 0x%08x \n", BitsInfo
->bmiHeader
.biWidth
);
830 DPRINT1("BitsInfo->bmiHeader.biHeight : 0x%08x \n", BitsInfo
->bmiHeader
.biHeight
);
834 /* Select the bitmap into hdcMem, and save a handle to the old bitmap */
835 hOldBitmap
= NtGdiSelectBitmap(hdcMem
, hBitmap
);
837 if(Usage
== DIB_PAL_COLORS
)
839 hPal
= NtGdiGetDCObject(hDC
, GDI_OBJECT_TYPE_PALETTE
);
840 hPal
= GdiSelectPalette(hdcMem
, hPal
, FALSE
);
843 if (BitsInfo
->bmiHeader
.biCompression
== BI_RLE4
||
844 BitsInfo
->bmiHeader
.biCompression
== BI_RLE8
)
846 /* copy existing bitmap from destination dc */
847 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
848 NtGdiBitBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
849 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, ROP
, 0, 0);
851 NtGdiStretchBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
852 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
856 pDC
= DC_LockDc(hdcMem
);
859 /* Note BitsInfo->bmiHeader.biHeight is the number of scanline,
860 * if it negitve we getting to many scanline for scanline is UINT not
861 * a INT, so we need make the negtive value to positve and that make the
862 * count correct for negtive bitmap, TODO : we need testcase for this api */
863 IntSetDIBits(pDC
, hBitmap
, 0, abs(BitsInfo
->bmiHeader
.biHeight
), Bits
,
870 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
871 left (negative biHeight) */
872 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
873 NtGdiBitBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
874 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
877 NtGdiStretchBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
878 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
879 SrcWidth
, SrcHeight
, ROP
, 0);
883 GdiSelectPalette(hdcMem
, hPal
, FALSE
);
886 NtGdiSelectBitmap(hdcMem
, hOldBitmap
);
888 NtGdiDeleteObjectApp(hdcMem
);
890 NtGdiDeleteObject(hBitmap
);
898 IntCreateDIBitmap(PDC Dc
,
910 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
911 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
913 if (bpp
!= 1) fColor
= TRUE
;
914 else if ((coloruse
!= DIB_RGB_COLORS
) || (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
917 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
919 const RGBQUAD
*rgb
= data
->bmiColors
;
920 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
922 // Check if the first color of the colormap is black
923 if ((col
== RGB(0, 0, 0)))
926 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
928 // If the second color is white, create a monochrome bitmap
929 fColor
= (col
!= RGB(0xff,0xff,0xff));
933 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
935 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
936 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
938 if ((col
== RGB(0,0,0)))
941 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
942 fColor
= (col
!= RGB(0xff,0xff,0xff));
948 DPRINT("(%ld): wrong size for data\n", data
->bmiHeader
.biSize
);
953 // Now create the bitmap
956 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
960 handle
= IntGdiCreateBitmap(width
,
970 if (NULL
!= handle
&& CBM_INIT
== init
)
972 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
978 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
979 // The DDB that is created will be whatever bit depth your reference DC is
982 NtGdiCreateDIBitmapInternal(IN HDC hDc
,
986 IN OPTIONAL LPBYTE pjInit
,
987 IN OPTIONAL LPBITMAPINFO pbmi
,
989 IN UINT cjMaxInitInfo
,
999 hDc
= IntGdiCreateDC(NULL
, NULL
, NULL
, NULL
,FALSE
);
1002 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1006 Dc
= DC_LockDc(hDc
);
1009 NtGdiDeleteObjectApp(hDc
);
1010 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1015 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, cjMaxInitInfo
, fInit
, pjInit
, pbmi
, iUsage
);
1018 NtGdiDeleteObjectApp(hDc
);
1022 Dc
= DC_LockDc(hDc
);
1025 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1029 Bmp
= IntCreateDIBitmap(Dc
, cx
, cy
, cjMaxInitInfo
, fInit
, pjInit
, pbmi
, iUsage
);
1036 HBITMAP STDCALL
NtGdiCreateDIBSection(HDC hDC
,
1037 IN OPTIONAL HANDLE hSection
,
1039 IN LPBITMAPINFO bmi
,
1043 IN ULONG_PTR dwColorSpace
,
1046 HBITMAP hbitmap
= 0;
1048 BOOL bDesktopDC
= FALSE
;
1050 if (!bmi
) return hbitmap
; // Make sure.
1052 // If the reference hdc is null, take the desktop dc
1055 hDC
= NtGdiCreateCompatibleDC(0);
1059 if ((dc
= DC_LockDc(hDC
)))
1061 hbitmap
= DIB_CreateDIBSection ( dc
, (BITMAPINFO
*)bmi
, Usage
, Bits
,
1062 hSection
, dwOffset
, 0);
1067 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1071 NtGdiDeleteObjectApp(hDC
);
1077 DIB_CreateDIBSection(
1078 PDC dc
, BITMAPINFO
*bmi
, UINT usage
,
1079 LPVOID
*bits
, HANDLE section
,
1080 DWORD offset
, DWORD ovr_pitch
)
1083 BITMAPOBJ
*bmp
= NULL
;
1084 DIBSECTION
*dib
= NULL
;
1086 // Fill BITMAP32 structure with DIB data
1087 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
1094 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
1095 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
1096 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
1098 /* CreateDIBSection should fail for compressed formats */
1099 if (bi
->biCompression
== BI_RLE4
|| bi
->biCompression
== BI_RLE8
)
1101 return (HBITMAP
)NULL
;
1104 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
1106 bm
.bmWidth
= bi
->biWidth
;
1107 bm
.bmHeight
= effHeight
;
1108 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
1110 bm
.bmPlanes
= bi
->biPlanes
;
1111 bm
.bmBitsPixel
= bi
->biBitCount
;
1114 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
1115 // we're dealing with a compressed bitmap. Otherwise, use width * height.
1116 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
1117 ? bi
->biSizeImage
: (ULONG
) (bm
.bmWidthBytes
* effHeight
);
1121 /* bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
1122 0L, offset, totalSize); */
1123 DbgPrint("DIB_CreateDIBSection: Cannot yet handle section DIBs\n");
1124 SetLastWin32Error(ERROR_CALL_NOT_IMPLEMENTED
);
1127 else if (ovr_pitch
&& offset
)
1128 bm
.bmBits
= (LPVOID
) offset
;
1131 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
1134 if(usage
== DIB_PAL_COLORS
)
1135 lpRGB
= DIB_MapPaletteColors(dc
, bmi
);
1137 lpRGB
= bmi
->bmiColors
;
1139 // Allocate Memory for DIB and fill structure
1142 dib
= ExAllocatePoolWithTag(PagedPool
, sizeof(DIBSECTION
), TAG_DIB
);
1143 if (dib
!= NULL
) RtlZeroMemory(dib
, sizeof(DIBSECTION
));
1149 /* Set dsBitfields values */
1150 if ( usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
1152 dib
->dsBitfields
[0] = dib
->dsBitfields
[1] = dib
->dsBitfields
[2] = 0;
1154 else switch(bi
->biBitCount
)
1157 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)lpRGB
: 0x7c00;
1158 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 1) : 0x03e0;
1159 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 2) : 0x001f;
1163 dib
->dsBitfields
[0] = 0xff0000;
1164 dib
->dsBitfields
[1] = 0x00ff00;
1165 dib
->dsBitfields
[2] = 0x0000ff;
1169 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)lpRGB
: 0xff0000;
1170 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 1) : 0x00ff00;
1171 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 2) : 0x0000ff;
1174 dib
->dshSection
= section
;
1175 dib
->dsOffset
= offset
;
1177 // Create Device Dependent Bitmap and add DIB pointer
1178 Size
.cx
= bm
.bmWidth
;
1179 Size
.cy
= abs(bm
.bmHeight
);
1180 res
= IntCreateBitmap(Size
, bm
.bmWidthBytes
,
1181 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
1182 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
1183 (bi
->biHeight
< 0 ? BMF_TOPDOWN
: 0),
1187 if (lpRGB
!= bmi
->bmiColors
)
1189 ExFreePoolWithTag(lpRGB
, TAG_COLORMAP
);
1191 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
1194 bmp
= BITMAPOBJ_LockBitmap(res
);
1197 if (lpRGB
!= bmi
->bmiColors
)
1199 ExFreePoolWithTag(lpRGB
, TAG_COLORMAP
);
1201 SetLastWin32Error(ERROR_INVALID_HANDLE
);
1202 NtGdiDeleteObject(bmp
);
1205 bmp
->dib
= (DIBSECTION
*) dib
;
1206 bmp
->flFlags
= BITMAPOBJ_IS_APIBITMAP
;
1208 /* WINE NOTE: WINE makes use of a colormap, which is a color translation table between the DIB and the X physical
1209 device. Obviously, this is left out of the ReactOS implementation. Instead, we call
1210 NtGdiSetDIBColorTable. */
1212 if(bi
->biBitCount
== 1) { bi
->biClrUsed
= 2; } else
1213 if(bi
->biBitCount
== 4) { bi
->biClrUsed
= 16; } else
1214 if(bi
->biBitCount
== 8) { bi
->biClrUsed
= 256; }
1216 if (bi
->biClrUsed
!= 0)
1217 bmp
->hDIBPalette
= PALETTE_AllocPaletteIndexedRGB(bi
->biClrUsed
, lpRGB
);
1219 bmp
->hDIBPalette
= PALETTE_AllocPalette(PAL_BITFIELDS
, 0, NULL
,
1220 dib
->dsBitfields
[0],
1221 dib
->dsBitfields
[1],
1222 dib
->dsBitfields
[2]);
1225 dib
->dsBmih
.biSizeImage
= totalSize
;
1228 // Clean up in case of errors
1229 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
)
1231 DPRINT("got an error res=%08x, bmp=%p, dib=%p, bm.bmBits=%p\n", res
, bmp
, dib
, bm
.bmBits
);
1235 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
1237 VirtualFree(bm.bmBits, 0L, MEM_RELEASE), bm.bmBits = NULL;
1240 if (dib
) { ExFreePoolWithTag(dib
, TAG_DIB
); dib
= NULL
; }
1241 if (bmp
) { bmp
= NULL
; }
1242 if (res
) { BITMAPOBJ_FreeBitmapByHandle(res
); res
= 0; }
1245 if (lpRGB
!= bmi
->bmiColors
)
1247 ExFreePoolWithTag(lpRGB
, TAG_COLORMAP
);
1252 BITMAPOBJ_UnlockBitmap(bmp
);
1255 // Return BITMAP handle and storage location
1256 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
1264 /***********************************************************************
1265 * DIB_GetDIBWidthBytes
1267 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
1268 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
1269 * 11/16/1999 (RJJ) lifted from wine
1271 INT FASTCALL
DIB_GetDIBWidthBytes (INT width
, INT depth
)
1273 return ((width
* depth
+ 31) & ~31) >> 3;
1276 /***********************************************************************
1277 * DIB_GetDIBImageBytes
1279 * Return the number of bytes used to hold the image in a DIB bitmap.
1280 * 11/16/1999 (RJJ) lifted from wine
1283 INT STDCALL
DIB_GetDIBImageBytes (INT width
, INT height
, INT depth
)
1285 return DIB_GetDIBWidthBytes( width
, depth
) * (height
< 0 ? -height
: height
);
1288 /***********************************************************************
1289 * DIB_BitmapInfoSize
1291 * Return the size of the bitmap info structure including color table.
1292 * 11/16/1999 (RJJ) lifted from wine
1295 INT FASTCALL
DIB_BitmapInfoSize (const BITMAPINFO
* info
, WORD coloruse
)
1299 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1301 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
1302 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1303 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1305 else /* assume BITMAPINFOHEADER */
1307 colors
= info
->bmiHeader
.biClrUsed
;
1308 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
1309 return sizeof(BITMAPINFOHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1314 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1321 palGDI
= PALETTE_LockPalette(dc
->DcLevel
.hpal
);
1328 if (palGDI
->Mode
!= PAL_INDEXED
)
1330 PALETTE_UnlockPalette(palGDI
);
1334 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1335 if (lpbmi
->bmiHeader
.biClrUsed
)
1337 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1340 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1343 PALETTE_UnlockPalette(palGDI
);
1347 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1349 for (i
= 0; i
< nNumColors
; i
++)
1351 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1352 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1353 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1354 lpRGB
[i
].rgbReserved
= 0;
1357 PALETTE_UnlockPalette(palGDI
);
1363 BuildDIBPalette (CONST BITMAPINFO
*bmi
, PINT paletteType
)
1367 PALETTEENTRY
*palEntries
= NULL
;
1369 ULONG RedMask
, GreenMask
, BlueMask
;
1371 // Determine Bits Per Pixel
1372 bits
= bmi
->bmiHeader
.biBitCount
;
1374 // Determine paletteType from Bits Per Pixel
1377 *paletteType
= PAL_INDEXED
;
1378 RedMask
= GreenMask
= BlueMask
= 0;
1380 else if(bmi
->bmiHeader
.biCompression
== BI_BITFIELDS
)
1382 *paletteType
= PAL_BITFIELDS
;
1383 RedMask
= ((ULONG
*)bmi
->bmiColors
)[0];
1384 GreenMask
= ((ULONG
*)bmi
->bmiColors
)[1];
1385 BlueMask
= ((ULONG
*)bmi
->bmiColors
)[2];
1389 *paletteType
= PAL_BITFIELDS
;
1396 *paletteType
= PAL_BGR
;
1398 GreenMask
= 0x00ff00;
1399 BlueMask
= 0x0000ff;
1402 if (bmi
->bmiHeader
.biClrUsed
== 0)
1404 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1408 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1411 if (PAL_INDEXED
== *paletteType
)
1413 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)bmi
->bmiColors
);
1417 hPal
= PALETTE_AllocPalette(*paletteType
, ColorCount
,
1418 (ULONG
*) palEntries
,
1419 RedMask
, GreenMask
, BlueMask
);