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 NtGdiSetDIBColorTable(HDC hDC
, UINT StartIndex
, UINT Entries
, CONST RGBQUAD
*Colors
)
35 if (!(dc
= DC_LockDc(hDC
))) return 0;
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 for (Index
= StartIndex
;
68 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
71 PalGDI
->IndexedColors
[Index
].peRed
= Colors
[Index
- StartIndex
].rgbRed
;
72 PalGDI
->IndexedColors
[Index
].peGreen
= Colors
[Index
- StartIndex
].rgbGreen
;
73 PalGDI
->IndexedColors
[Index
].peBlue
= Colors
[Index
- StartIndex
].rgbBlue
;
81 PALETTE_UnlockPalette(PalGDI
);
86 BITMAPOBJ_UnlockBitmap(BitmapObj
);
93 NtGdiGetDIBColorTable(HDC hDC
, UINT StartIndex
, UINT Entries
, RGBQUAD
*Colors
)
100 if (!(dc
= DC_LockDc(hDC
))) return 0;
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 for (Index
= StartIndex
;
133 Index
< StartIndex
+ Entries
&& Index
< PalGDI
->NumColors
;
136 Colors
[Index
- StartIndex
].rgbRed
= PalGDI
->IndexedColors
[Index
].peRed
;
137 Colors
[Index
- StartIndex
].rgbGreen
= PalGDI
->IndexedColors
[Index
].peGreen
;
138 Colors
[Index
- StartIndex
].rgbBlue
= PalGDI
->IndexedColors
[Index
].peBlue
;
146 PALETTE_UnlockPalette(PalGDI
);
151 BITMAPOBJ_UnlockBitmap(BitmapObj
);
157 // Converts a DIB to a device-dependent bitmap
165 CONST BITMAPINFO
*bmi
,
169 HBITMAP SourceBitmap
;
172 SURFOBJ
*DestSurf
, *SourceSurf
;
179 HPALETTE DDB_Palette
, DIB_Palette
;
180 ULONG DDB_Palette_Type
, DIB_Palette_Type
;
184 if (!(bitmap
= BITMAPOBJ_LockBitmap(hBitmap
)))
190 //if (ColorUse == DIB_PAL_COLORS)
191 // lpRGB = DIB_MapPaletteColors(hDC, bmi);
193 // lpRGB = &bmi->bmiColors;
195 DestSurf
= &bitmap
->SurfObj
;
197 // Create source surface
198 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
199 SourceSize
.cy
= ScanLines
;
201 // Determine width of DIB
202 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
);
204 SourceBitmap
= EngCreateBitmap(SourceSize
,
206 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
207 bmi
->bmiHeader
.biHeight
< 0 ? BMF_TOPDOWN
: 0,
209 if (0 == SourceBitmap
)
211 BITMAPOBJ_UnlockBitmap(bitmap
);
212 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
216 SourceSurf
= EngLockSurface((HSURF
)SourceBitmap
);
217 if (NULL
== SourceSurf
)
219 EngDeleteSurface((HSURF
)SourceBitmap
);
220 BITMAPOBJ_UnlockBitmap(bitmap
);
221 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
225 // Destination palette obtained from the hDC
226 hDCPalette
= PALETTE_LockPalette(DC
->DevInfo
->hpalDefault
);
227 if (NULL
== hDCPalette
)
229 EngUnlockSurface(SourceSurf
);
230 EngDeleteSurface((HSURF
)SourceBitmap
);
231 BITMAPOBJ_UnlockBitmap(bitmap
);
232 SetLastWin32Error(ERROR_INVALID_HANDLE
);
235 DDB_Palette_Type
= hDCPalette
->Mode
;
236 DDB_Palette
= DC
->DevInfo
->hpalDefault
;
237 PALETTE_UnlockPalette(hDCPalette
);
239 // Source palette obtained from the BITMAPINFO
240 DIB_Palette
= BuildDIBPalette ( (PBITMAPINFO
)bmi
, (PINT
)&DIB_Palette_Type
);
241 if (NULL
== DIB_Palette
)
243 EngUnlockSurface(SourceSurf
);
244 EngDeleteSurface((HSURF
)SourceBitmap
);
245 BITMAPOBJ_UnlockBitmap(bitmap
);
246 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
250 // Determine XLATEOBJ for color translation
251 XlateObj
= IntEngCreateXlate(DDB_Palette_Type
, DIB_Palette_Type
, DDB_Palette
, DIB_Palette
);
252 if (NULL
== XlateObj
)
254 PALETTE_FreePalette(DIB_Palette
);
255 EngUnlockSurface(SourceSurf
);
256 EngDeleteSurface((HSURF
)SourceBitmap
);
257 BITMAPOBJ_UnlockBitmap(bitmap
);
258 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
266 // Determine destination rectangle
268 DestRect
.top
= abs(bmi
->bmiHeader
.biHeight
) - StartScan
- ScanLines
;
269 DestRect
.right
= SourceSize
.cx
;
270 DestRect
.bottom
= DestRect
.top
+ ScanLines
;
272 copyBitsResult
= EngCopyBits(DestSurf
, SourceSurf
, NULL
, XlateObj
, &DestRect
, &ZeroPoint
);
274 // If it succeeded, return number of scanlines copies
275 if(copyBitsResult
== TRUE
)
277 result
= SourceSize
.cy
- 1;
281 EngDeleteXlate(XlateObj
);
282 PALETTE_FreePalette(DIB_Palette
);
283 EngUnlockSurface(SourceSurf
);
284 EngDeleteSurface((HSURF
)SourceBitmap
);
286 // if (ColorUse == DIB_PAL_COLORS)
287 // WinFree((LPSTR)lpRGB);
289 BITMAPOBJ_UnlockBitmap(bitmap
);
294 // Converts a DIB to a device-dependent bitmap
302 CONST BITMAPINFO
*bmi
,
311 SetLastWin32Error(ERROR_INVALID_HANDLE
);
320 Ret
= IntSetDIBits(Dc
, hBitmap
, StartScan
, ScanLines
, Bits
, bmi
, ColorUse
);
328 NtGdiSetDIBitsToDevice(
339 CONST BITMAPINFO
*bmi
,
346 /* Converts a device-dependent bitmap to a DIB */
348 NtGdiGetDIBits(HDC hDC
,
356 BITMAPOBJ
*BitmapObj
;
357 SURFOBJ
*DestSurfObj
;
361 HPALETTE hSourcePalette
;
362 HPALETTE hDestPalette
;
363 PPALGDI SourcePalette
;
365 ULONG SourcePaletteType
;
366 ULONG DestPaletteType
;
373 /* Get handle for the palette in DC. */
377 SetLastWin32Error(ERROR_INVALID_HANDLE
);
385 hSourcePalette
= Dc
->w
.hPalette
;
386 /* FIXME: This is incorrect. hDestPalette should be something other. */
387 hDestPalette
= Dc
->DevInfo
->hpalDefault
;
390 /* Get pointer to the source bitmap object. */
391 BitmapObj
= BITMAPOBJ_LockBitmap(hBitmap
);
392 if (BitmapObj
== NULL
)
394 SetLastWin32Error(ERROR_INVALID_HANDLE
);
400 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
) ||
401 Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
403 Info
->bmiHeader
.biWidth
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
404 Info
->bmiHeader
.biHeight
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
405 /* Report negtive height for top-down bitmaps. */
406 if (BitmapObj
->SurfObj
.lDelta
> 0)
407 Info
->bmiHeader
.biHeight
= -Info
->bmiHeader
.biHeight
;
408 Info
->bmiHeader
.biPlanes
= 1;
409 Info
->bmiHeader
.biBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
410 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
412 switch (BitmapObj
->SurfObj
.iBitmapFormat
)
414 case BMF_1BPP
: case BMF_4BPP
: case BMF_8BPP
:
415 case BMF_16BPP
: case BMF_24BPP
: case BMF_32BPP
:
416 Info
->bmiHeader
.biCompression
= BI_RGB
;
419 Info
->bmiHeader
.biCompression
= BI_RLE4
;
422 Info
->bmiHeader
.biCompression
= BI_RLE8
;
425 Info
->bmiHeader
.biCompression
= BI_JPEG
;
428 Info
->bmiHeader
.biCompression
= BI_PNG
;
431 Info
->bmiHeader
.biSizeImage
= BitmapObj
->SurfObj
.cjBits
;
432 Info
->bmiHeader
.biXPelsPerMeter
= 0; /* FIXME */
433 Info
->bmiHeader
.biYPelsPerMeter
= 0; /* FIXME */
434 Info
->bmiHeader
.biClrUsed
=
435 Info
->bmiHeader
.biClrImportant
= 1 << Info
->bmiHeader
.biBitCount
; /* FIXME */
436 Result
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
442 if (StartScan
> BitmapObj
->SurfObj
.sizlBitmap
.cy
)
448 ScanLines
= min(ScanLines
, BitmapObj
->SurfObj
.sizlBitmap
.cy
- StartScan
);
449 DestSize
.cx
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
450 DestSize
.cy
= ScanLines
;
451 DestBitmap
= EngCreateBitmap(
452 DestSize
, DIB_GetDIBWidthBytes(DestSize
.cx
,
453 Info
->bmiHeader
.biBitCount
),
454 BitmapFormat(Info
->bmiHeader
.biBitCount
, Info
->bmiHeader
.biCompression
),
455 0 < Info
->bmiHeader
.biHeight
? 0 : BMF_TOPDOWN
,
457 DestSurfObj
= EngLockSurface((HSURF
)DestBitmap
);
459 SourcePalette
= PALETTE_LockPalette(hSourcePalette
);
460 /* FIXME - SourcePalette can be NULL!!! Don't assert here! */
461 ASSERT(SourcePalette
);
462 SourcePaletteType
= SourcePalette
->Mode
;
463 PALETTE_UnlockPalette(SourcePalette
);
465 DestPalette
= PALETTE_LockPalette(hDestPalette
);
466 /* FIXME - DestPalette can be NULL!!!! Don't assert here!!! */
468 DestPaletteType
= DestPalette
->Mode
;
471 /* FIXME: This is largely incomplete. */
472 if (Info
->bmiHeader
.biBitCount
<= 8)
474 if (Usage
== DIB_RGB_COLORS
)
477 Index
< (1 << Info
->bmiHeader
.biBitCount
) &&
478 Index
< DestPalette
->NumColors
;
481 Info
->bmiColors
[Index
].rgbRed
=
482 DestPalette
->IndexedColors
[Index
].peRed
;
483 Info
->bmiColors
[Index
].rgbGreen
=
484 DestPalette
->IndexedColors
[Index
].peGreen
;
485 Info
->bmiColors
[Index
].rgbBlue
=
486 DestPalette
->IndexedColors
[Index
].peBlue
;
489 if (Usage
== DIB_PAL_COLORS
)
491 DbgPrint("GetDIBits with DIB_PAL_COLORS isn't implemented yet.");
495 PALETTE_UnlockPalette(DestPalette
);
497 XlateObj
= IntEngCreateXlate(
498 DestPaletteType
, SourcePaletteType
, hDestPalette
, hSourcePalette
);
501 SourcePoint
.y
= BitmapObj
->SurfObj
.sizlBitmap
.cy
-
502 (StartScan
+ ScanLines
);
504 /* Determine destination rectangle */
507 DestRect
.right
= DestSize
.cx
;
508 DestRect
.bottom
= DestSize
.cy
;
510 if (EngCopyBits(DestSurfObj
, &BitmapObj
->SurfObj
,
511 NULL
, XlateObj
, &DestRect
, &SourcePoint
))
516 EngDeleteXlate(XlateObj
);
517 EngUnlockSurface(DestSurfObj
);
521 BITMAPOBJ_UnlockBitmap(BitmapObj
);
526 INT STDCALL
NtGdiStretchDIBits(HDC hDC
,
536 CONST BITMAPINFO
*BitsInfo
,
540 HBITMAP hBitmap
, hOldBitmap
;
543 if (!Bits
|| !BitsInfo
)
545 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
549 hdcMem
= NtGdiCreateCompatibleDC(hDC
);
550 hBitmap
= NtGdiCreateCompatibleBitmap(hDC
, BitsInfo
->bmiHeader
.biWidth
,
551 BitsInfo
->bmiHeader
.biHeight
);
552 hOldBitmap
= NtGdiSelectObject(hdcMem
, hBitmap
);
554 if (BitsInfo
->bmiHeader
.biCompression
== BI_RLE4
||
555 BitsInfo
->bmiHeader
.biCompression
== BI_RLE8
)
557 /* copy existing bitmap from destination dc */
558 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
559 NtGdiBitBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
560 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, ROP
, 0, 0);
562 NtGdiStretchBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
563 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
567 NtGdiSetDIBits(hdcMem
, hBitmap
, 0, BitsInfo
->bmiHeader
.biHeight
, Bits
,
570 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
571 left (negative biHeight) */
572 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
573 NtGdiBitBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
574 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
577 NtGdiStretchBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
578 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
579 SrcWidth
, SrcHeight
, ROP
, 0);
581 NtGdiSelectObject(hdcMem
, hOldBitmap
);
582 NtGdiDeleteDC(hdcMem
);
583 NtGdiDeleteObject(hBitmap
);
588 LONG STDCALL
NtGdiGetBitmapBits(HBITMAP hBitmap
,
590 OUT OPTIONAL PBYTE Bits
)
595 bmp
= BITMAPOBJ_LockBitmap (hBitmap
);
601 /* If the bits vector is null, the function should return the read size */
604 ret
= bmp
->SurfObj
.cjBits
;
605 BITMAPOBJ_UnlockBitmap (bmp
);
611 DPRINT ("(%ld): Negative number of bytes passed???\n", Count
);
615 /* Only get entire lines */
616 height
= Count
/ abs(bmp
->SurfObj
.lDelta
);
617 if (height
> bmp
->SurfObj
.sizlBitmap
.cy
)
619 height
= bmp
->SurfObj
.sizlBitmap
.cy
;
621 Count
= height
* abs(bmp
->SurfObj
.lDelta
);
624 DPRINT("Less then one entire line requested\n");
625 BITMAPOBJ_UnlockBitmap (bmp
);
629 DPRINT("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
630 hBitmap
, Count
, Bits
, bmp
->SurfObj
.sizlBitmap
.cx
,
631 bmp
->SurfObj
.sizlBitmap
.cy
,
632 1 << BitsPerFormat(bmp
->SurfObj
.iBitmapFormat
), height
);
634 /* FIXME: Call DDI CopyBits here if available */
637 DPRINT("Calling device specific BitmapBits\n");
638 if(bmp
->DDBitmap
->funcs
->pBitmapBits
)
640 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hbitmap
, bits
, count
,
645 ERR_(bitmap
)("BitmapBits == NULL??\n");
652 memcpy(Bits
, bmp
->SurfObj
.pvBits
, Count
);
656 BITMAPOBJ_UnlockBitmap (bmp
);
662 IntCreateDIBitmap(PDC Dc
, const BITMAPINFOHEADER
*header
,
663 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
674 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
, &compr
) == -1) return 0;
676 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
677 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
679 if (bpp
!= 1) fColor
= TRUE
;
680 else if ((coloruse
!= DIB_RGB_COLORS
) ||
681 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
684 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
686 RGBQUAD
*rgb
= data
->bmiColors
;
687 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
689 // Check if the first color of the colormap is black
690 if ((col
== RGB(0, 0, 0)))
693 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
695 // If the second color is white, create a monochrome bitmap
696 fColor
= (col
!= RGB(0xff,0xff,0xff));
700 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
702 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
703 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
705 if ((col
== RGB(0,0,0)))
708 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
709 fColor
= (col
!= RGB(0xff,0xff,0xff));
715 DPRINT("(%ld): wrong size for data\n", data
->bmiHeader
.biSize
);
720 // Now create the bitmap
723 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
728 size
.cy
= abs(height
);
730 handle
= IntCreateBitmap(size
, DIB_GetDIBWidthBytes(width
, 1), BMF_1BPP
,
731 (height
< 0 ? BMF_TOPDOWN
: 0) | BMF_NOZEROINIT
,
738 if (NULL
!= handle
&& CBM_INIT
== init
)
740 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
746 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
747 // The DDB that is created will be whatever bit depth your reference DC is
748 HBITMAP STDCALL
NtGdiCreateDIBitmap(HDC hDc
, const BITMAPINFOHEADER
*Header
,
749 DWORD Init
, LPCVOID Bits
, const BITMAPINFO
*Data
,
758 SetLastWin32Error(ERROR_INVALID_HANDLE
);
762 Bmp
= IntCreateDIBitmap(Dc
, Header
, Init
, Bits
, Data
, ColorUse
);
769 HBITMAP STDCALL
NtGdiCreateDIBSection(HDC hDC
,
770 IN OPTIONAL HANDLE hSection
,
776 IN ULONG_PTR dwColorSpace
,
781 BOOL bDesktopDC
= FALSE
;
783 // If the reference hdc is null, take the desktop dc
786 hDC
= NtGdiCreateCompatibleDC(0);
790 if ((dc
= DC_LockDc(hDC
)))
792 hbitmap
= DIB_CreateDIBSection ( dc
, (BITMAPINFO
*)bmi
, Usage
, Bits
,
793 hSection
, dwOffset
, 0);
798 SetLastWin32Error(ERROR_INVALID_HANDLE
);
808 DIB_CreateDIBSection(
809 PDC dc
, BITMAPINFO
*bmi
, UINT usage
,
810 LPVOID
*bits
, HANDLE section
,
811 DWORD offset
, DWORD ovr_pitch
)
814 BITMAPOBJ
*bmp
= NULL
;
815 DIBSECTION
*dib
= NULL
;
817 // Fill BITMAP32 structure with DIB data
818 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
826 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
827 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
828 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
830 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
832 bm
.bmWidth
= bi
->biWidth
;
833 bm
.bmHeight
= effHeight
;
834 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
836 bm
.bmPlanes
= bi
->biPlanes
;
837 bm
.bmBitsPixel
= bi
->biBitCount
;
840 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
841 // we're dealing with a compressed bitmap. Otherwise, use width * height.
842 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
843 ? bi
->biSizeImage
: (ULONG
) (bm
.bmWidthBytes
* effHeight
);
847 /* bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
848 0L, offset, totalSize); */
849 DbgPrint("DIB_CreateDIBSection: Cannot yet handle section DIBs\n");
850 SetLastWin32Error(ERROR_CALL_NOT_IMPLEMENTED
);
853 else if (ovr_pitch
&& offset
)
854 bm
.bmBits
= (LPVOID
) offset
;
857 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
860 if(usage
== DIB_PAL_COLORS
)
861 lpRGB
= DIB_MapPaletteColors(dc
, bmi
);
863 lpRGB
= bmi
->bmiColors
;
865 // Allocate Memory for DIB and fill structure
868 dib
= ExAllocatePoolWithTag(PagedPool
, sizeof(DIBSECTION
), TAG_DIB
);
869 if (dib
!= NULL
) RtlZeroMemory(dib
, sizeof(DIBSECTION
));
876 dib
->dsBmih
.biSizeImage
= totalSize
;
878 /* Set dsBitfields values */
879 if ( usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
881 dib
->dsBitfields
[0] = dib
->dsBitfields
[1] = dib
->dsBitfields
[2] = 0;
883 else switch(bi
->biBitCount
)
886 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)lpRGB
: 0x7c00;
887 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 1) : 0x03e0;
888 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 2) : 0x001f; break;
891 dib
->dsBitfields
[0] = 0xff0000;
892 dib
->dsBitfields
[1] = 0x00ff00;
893 dib
->dsBitfields
[2] = 0x0000ff;
897 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)lpRGB
: 0xff0000;
898 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 1) : 0x00ff00;
899 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)lpRGB
+ 2) : 0x0000ff;
902 dib
->dshSection
= section
;
903 dib
->dsOffset
= offset
;
906 // Create Device Dependent Bitmap and add DIB pointer
909 Size
.cx
= bm
.bmWidth
;
910 Size
.cy
= abs(bm
.bmHeight
);
911 res
= IntCreateBitmap(Size
, bm
.bmWidthBytes
,
912 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
913 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
914 (bi
->biHeight
< 0 ? BMF_TOPDOWN
: 0),
918 if (lpRGB
!= bmi
->bmiColors
)
922 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
925 bmp
= BITMAPOBJ_LockBitmap(res
);
928 if (lpRGB
!= bmi
->bmiColors
)
932 SetLastWin32Error(ERROR_INVALID_HANDLE
);
933 NtGdiDeleteObject(bmp
);
936 bmp
->dib
= (DIBSECTION
*) dib
;
937 bmp
->flFlags
= BITMAPOBJ_IS_APIBITMAP
;
939 /* WINE NOTE: WINE makes use of a colormap, which is a color translation table between the DIB and the X physical
940 device. Obviously, this is left out of the ReactOS implementation. Instead, we call
941 NtGdiSetDIBColorTable. */
942 if(bi
->biBitCount
== 1) { Entries
= 2; } else
943 if(bi
->biBitCount
== 4) { Entries
= 16; } else
944 if(bi
->biBitCount
== 8) { Entries
= 256; }
947 bmp
->hDIBPalette
= PALETTE_AllocPaletteIndexedRGB(Entries
, lpRGB
);
949 bmp
->hDIBPalette
= PALETTE_AllocPalette(PAL_BITFIELDS
, 0, NULL
,
952 dib
->dsBitfields
[2]);
955 // Clean up in case of errors
956 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
)
958 DPRINT("got an error res=%08x, bmp=%p, dib=%p, bm.bmBits=%p\n", res
, bmp
, dib
, bm
.bmBits
);
962 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
964 VirtualFree(bm.bmBits, 0L, MEM_RELEASE), bm.bmBits = NULL;
967 if (dib
) { ExFreePool(dib
); dib
= NULL
; }
968 if (bmp
) { bmp
= NULL
; }
969 if (res
) { BITMAPOBJ_FreeBitmap(res
); res
= 0; }
972 if (lpRGB
!= bmi
->bmiColors
)
979 BITMAPOBJ_UnlockBitmap(bmp
);
982 // Return BITMAP handle and storage location
983 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
991 /***********************************************************************
992 * DIB_GetDIBWidthBytes
994 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
995 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
996 * 11/16/1999 (RJJ) lifted from wine
998 INT FASTCALL
DIB_GetDIBWidthBytes (INT width
, INT depth
)
1000 return ((width
* depth
+ 31) & ~31) >> 3;
1003 /***********************************************************************
1004 * DIB_GetDIBImageBytes
1006 * Return the number of bytes used to hold the image in a DIB bitmap.
1007 * 11/16/1999 (RJJ) lifted from wine
1010 INT STDCALL
DIB_GetDIBImageBytes (INT width
, INT height
, INT depth
)
1012 return DIB_GetDIBWidthBytes( width
, depth
) * (height
< 0 ? -height
: height
);
1015 /***********************************************************************
1016 * DIB_BitmapInfoSize
1018 * Return the size of the bitmap info structure including color table.
1019 * 11/16/1999 (RJJ) lifted from wine
1022 INT FASTCALL
DIB_BitmapInfoSize (const BITMAPINFO
* info
, WORD coloruse
)
1026 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
1028 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
1029 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
1030 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
1032 else /* assume BITMAPINFOHEADER */
1034 colors
= info
->bmiHeader
.biClrUsed
;
1035 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
1036 return sizeof(BITMAPINFOHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
1040 INT STDCALL
DIB_GetBitmapInfo (const BITMAPINFOHEADER
*header
,
1046 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
1048 *width
= header
->biWidth
;
1049 *height
= header
->biHeight
;
1050 *bpp
= header
->biBitCount
;
1051 *compr
= header
->biCompression
;
1054 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
1056 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
1057 *width
= core
->bcWidth
;
1058 *height
= core
->bcHeight
;
1059 *bpp
= core
->bcBitCount
;
1063 DPRINT("(%ld): wrong size for header\n", header
->biSize
);
1067 // Converts a Device Independent Bitmap (DIB) to a Device Dependant Bitmap (DDB)
1068 // The specified Device Context (DC) defines what the DIB should be converted to
1069 PBITMAPOBJ FASTCALL
DIBtoDDB(HGLOBAL hPackedDIB
, HDC hdc
) // FIXME: This should be removed. All references to this function should
1070 // change to NtGdiSetDIBits
1073 PBITMAPOBJ pBmp
= NULL
;
1075 LPBYTE pbits
= NULL
;
1077 // Get a pointer to the packed DIB's data
1078 // pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
1081 pbits
= (LPBYTE
)(dib
+ DIB_BitmapInfoSize((BITMAPINFO
*)&dib
->dsBmih
, DIB_RGB_COLORS
));
1083 // Create a DDB from the DIB
1084 hBmp
= NtGdiCreateDIBitmap ( hdc
, &dib
->dsBmih
, CBM_INIT
,
1085 (LPVOID
)pbits
, (BITMAPINFO
*)&dib
->dsBmih
, DIB_RGB_COLORS
);
1087 // GlobalUnlock(hPackedDIB);
1089 // Retrieve the internal Pixmap from the DDB
1090 pBmp
= BITMAPOBJ_LockBitmap(hBmp
);
1096 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1103 palGDI
= PALETTE_LockPalette(dc
->w
.hPalette
);
1107 // RELEASEDCINFO(hDC);
1111 if (palGDI
->Mode
!= PAL_INDEXED
)
1113 PALETTE_UnlockPalette(palGDI
);
1114 palGDI
= PALETTE_LockPalette(dc
->PalIndexed
);
1115 if (palGDI
->Mode
!= PAL_INDEXED
)
1121 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1122 if (lpbmi
->bmiHeader
.biClrUsed
)
1124 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1127 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1130 PALETTE_UnlockPalette(palGDI
);
1134 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1136 for (i
= 0; i
< nNumColors
; i
++)
1138 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1139 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1140 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1143 // RELEASEDCINFO(hDC);
1144 PALETTE_UnlockPalette(palGDI
);
1150 BuildDIBPalette (CONST BITMAPINFO
*bmi
, PINT paletteType
)
1154 PALETTEENTRY
*palEntries
= NULL
;
1156 ULONG RedMask
, GreenMask
, BlueMask
;
1158 // Determine Bits Per Pixel
1159 bits
= bmi
->bmiHeader
.biBitCount
;
1161 // Determine paletteType from Bits Per Pixel
1164 *paletteType
= PAL_INDEXED
;
1165 RedMask
= GreenMask
= BlueMask
= 0;
1169 *paletteType
= PAL_BITFIELDS
;
1176 *paletteType
= PAL_BGR
;
1178 GreenMask
= 0x00ff00;
1179 BlueMask
= 0x0000ff;
1182 if (bmi
->bmiHeader
.biClrUsed
== 0)
1184 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1188 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1191 if (PAL_INDEXED
== *paletteType
)
1193 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)bmi
->bmiColors
);
1197 hPal
= PALETTE_AllocPalette(*paletteType
, ColorCount
,
1198 (ULONG
*) palEntries
,
1199 RedMask
, GreenMask
, BlueMask
);