2 * $Id: dib.c,v 1.57 2004/12/12 01:40:38 weiden Exp $
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.
24 NtGdiSetDIBColorTable(HDC hDC
, UINT StartIndex
, UINT Entries
, CONST RGBQUAD
*Colors
)
29 if (!(dc
= DC_LockDc(hDC
))) return 0;
31 BitmapObj
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
32 if (BitmapObj
== NULL
)
35 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
39 if (BitmapObj
->dib
== NULL
)
41 BITMAPOBJ_UnlockBitmap(dc
->w
.hBitmap
);
43 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
47 if (BitmapObj
->dib
->dsBmih
.biBitCount
<= 8 &&
48 StartIndex
< (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
50 if (StartIndex
+ Entries
> (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
51 Entries
= (1 << BitmapObj
->dib
->dsBmih
.biBitCount
) - StartIndex
;
53 MmCopyFromCaller(BitmapObj
->ColorMap
+ StartIndex
, Colors
, Entries
* sizeof(RGBQUAD
));
55 /* Rebuild the palette. */
56 NtGdiDeleteObject(dc
->w
.hPalette
);
57 dc
->w
.hPalette
= PALETTE_AllocPaletteIndexedRGB(1 << BitmapObj
->dib
->dsBmih
.biBitCount
,
63 BITMAPOBJ_UnlockBitmap(dc
->w
.hBitmap
);
70 NtGdiGetDIBColorTable(HDC hDC
, UINT StartIndex
, UINT Entries
, RGBQUAD
*Colors
)
75 if (!(dc
= DC_LockDc(hDC
))) return 0;
77 BitmapObj
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
78 if (BitmapObj
== NULL
)
81 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
85 if (BitmapObj
->dib
== NULL
)
87 BITMAPOBJ_UnlockBitmap(dc
->w
.hBitmap
);
89 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
93 if (BitmapObj
->dib
->dsBmih
.biBitCount
<= 8 &&
94 StartIndex
< (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
96 if (StartIndex
+ Entries
> (1 << BitmapObj
->dib
->dsBmih
.biBitCount
))
97 Entries
= (1 << BitmapObj
->dib
->dsBmih
.biBitCount
) - StartIndex
;
99 MmCopyToCaller(Colors
, BitmapObj
->ColorMap
+ StartIndex
, Entries
* sizeof(RGBQUAD
));
104 BITMAPOBJ_UnlockBitmap(dc
->w
.hBitmap
);
110 // Converts a DIB to a device-dependent bitmap
118 CONST BITMAPINFO
*bmi
,
122 HBITMAP SourceBitmap
;
125 SURFOBJ
*DestSurf
, *SourceSurf
;
132 HPALETTE DDB_Palette
, DIB_Palette
;
133 ULONG DDB_Palette_Type
, DIB_Palette_Type
;
137 if (!(bitmap
= BITMAPOBJ_LockBitmap(hBitmap
)))
143 //if (ColorUse == DIB_PAL_COLORS)
144 // lpRGB = DIB_MapPaletteColors(hDC, bmi);
146 // lpRGB = &bmi->bmiColors[0];
148 DestSurf
= &bitmap
->SurfObj
;
150 // Create source surface
151 SourceSize
.cx
= bmi
->bmiHeader
.biWidth
;
152 SourceSize
.cy
= abs(bmi
->bmiHeader
.biHeight
);
154 // Determine width of DIB
155 DIBWidth
= DIB_GetDIBWidthBytes(SourceSize
.cx
, bmi
->bmiHeader
.biBitCount
);
157 SourceBitmap
= EngCreateBitmap(SourceSize
,
159 BitmapFormat(bmi
->bmiHeader
.biBitCount
, bmi
->bmiHeader
.biCompression
),
160 0 < bmi
->bmiHeader
.biHeight
? 0 : BMF_TOPDOWN
,
162 SourceSurf
= EngLockSurface((HSURF
)SourceBitmap
);
164 // Destination palette obtained from the hDC
165 hDCPalette
= PALETTE_LockPalette(DC
->DevInfo
->hpalDefault
);
166 if (NULL
== hDCPalette
)
168 EngUnlockSurface(SourceSurf
);
169 EngDeleteSurface((HSURF
)SourceBitmap
);
170 BITMAPOBJ_UnlockBitmap(hBitmap
);
171 SetLastWin32Error(ERROR_INVALID_HANDLE
);
174 DDB_Palette_Type
= hDCPalette
->Mode
;
175 DDB_Palette
= DC
->DevInfo
->hpalDefault
;
176 PALETTE_UnlockPalette(DC
->DevInfo
->hpalDefault
);
178 // Source palette obtained from the BITMAPINFO
179 DIB_Palette
= BuildDIBPalette ( (PBITMAPINFO
)bmi
, (PINT
)&DIB_Palette_Type
);
180 if (NULL
== DIB_Palette
)
182 EngUnlockSurface(SourceSurf
);
183 EngDeleteSurface((HSURF
)SourceBitmap
);
184 BITMAPOBJ_UnlockBitmap(hBitmap
);
185 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
189 // Determine XLATEOBJ for color translation
190 XlateObj
= IntEngCreateXlate(DDB_Palette_Type
, DIB_Palette_Type
, DDB_Palette
, DIB_Palette
);
191 if (NULL
== XlateObj
)
193 PALETTE_FreePalette(DIB_Palette
);
194 EngUnlockSurface(SourceSurf
);
195 EngDeleteSurface((HSURF
)SourceBitmap
);
196 BITMAPOBJ_UnlockBitmap(hBitmap
);
197 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
205 // Determine destination rectangle
208 DestRect
.right
= SourceSize
.cx
;
209 DestRect
.bottom
= SourceSize
.cy
;
211 copyBitsResult
= EngCopyBits(DestSurf
, SourceSurf
, NULL
, XlateObj
, &DestRect
, &ZeroPoint
);
213 // If it succeeded, return number of scanlines copies
214 if(copyBitsResult
== TRUE
)
216 result
= SourceSize
.cy
- 1;
220 EngDeleteXlate(XlateObj
);
221 PALETTE_FreePalette(DIB_Palette
);
222 EngUnlockSurface(SourceSurf
);
223 EngDeleteSurface((HSURF
)SourceBitmap
);
225 // if (ColorUse == DIB_PAL_COLORS)
226 // WinFree((LPSTR)lpRGB);
228 BITMAPOBJ_UnlockBitmap(hBitmap
);
233 // Converts a DIB to a device-dependent bitmap
241 CONST BITMAPINFO
*bmi
,
250 SetLastWin32Error(ERROR_INVALID_HANDLE
);
254 Ret
= IntSetDIBits(Dc
, hBitmap
, StartScan
, ScanLines
, Bits
, bmi
, ColorUse
);
262 NtGdiSetDIBitsToDevice(
273 CONST BITMAPINFO
*bmi
,
280 /* Converts a device-dependent bitmap to a DIB */
282 NtGdiGetDIBits(HDC hDC
,
290 BITMAPOBJ
*BitmapObj
;
291 SURFOBJ
*DestSurfObj
;
295 HPALETTE hSourcePalette
;
296 HPALETTE hDestPalette
;
297 PPALGDI SourcePalette
;
299 ULONG SourcePaletteType
;
300 ULONG DestPaletteType
;
307 /* Get handle for the palette in DC. */
311 SetLastWin32Error(ERROR_INVALID_HANDLE
);
314 hSourcePalette
= Dc
->w
.hPalette
;
315 /* FIXME: This is incorrect. hDestPalette should be something other. */
316 hDestPalette
= Dc
->DevInfo
->hpalDefault
;
319 /* Get pointer to the source bitmap object. */
320 BitmapObj
= BITMAPOBJ_LockBitmap(hBitmap
);
321 if (BitmapObj
== NULL
)
323 SetLastWin32Error(ERROR_INVALID_HANDLE
);
329 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
) ||
330 Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
332 Info
->bmiHeader
.biWidth
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
333 Info
->bmiHeader
.biHeight
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
334 /* Report negtive height for top-down bitmaps. */
335 if (BitmapObj
->SurfObj
.lDelta
> 0)
336 Info
->bmiHeader
.biHeight
= -Info
->bmiHeader
.biHeight
;
337 Info
->bmiHeader
.biPlanes
= 1;
338 Info
->bmiHeader
.biBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
339 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
341 switch (BitmapObj
->SurfObj
.iBitmapFormat
)
343 case BMF_1BPP
: case BMF_4BPP
: case BMF_8BPP
:
344 case BMF_16BPP
: case BMF_24BPP
: case BMF_32BPP
:
345 Info
->bmiHeader
.biCompression
= BI_RGB
;
348 Info
->bmiHeader
.biCompression
= BI_RLE4
;
351 Info
->bmiHeader
.biCompression
= BI_RLE8
;
354 Info
->bmiHeader
.biCompression
= BI_JPEG
;
357 Info
->bmiHeader
.biCompression
= BI_PNG
;
360 Info
->bmiHeader
.biSizeImage
= BitmapObj
->SurfObj
.cjBits
;
361 Info
->bmiHeader
.biXPelsPerMeter
= 0; /* FIXME */
362 Info
->bmiHeader
.biYPelsPerMeter
= 0; /* FIXME */
363 Info
->bmiHeader
.biClrUsed
=
364 Info
->bmiHeader
.biClrImportant
= 1 << Info
->bmiHeader
.biBitCount
; /* FIXME */
365 Result
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
371 if (StartScan
> BitmapObj
->SurfObj
.sizlBitmap
.cy
)
377 ScanLines
= min(ScanLines
, BitmapObj
->SurfObj
.sizlBitmap
.cy
- StartScan
);
378 DestSize
.cx
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
379 DestSize
.cy
= ScanLines
;
380 DestBitmap
= EngCreateBitmap(
381 DestSize
, BitmapObj
->SurfObj
.lDelta
,
382 BitmapFormat(Info
->bmiHeader
.biBitCount
, Info
->bmiHeader
.biCompression
),
383 0 < Info
->bmiHeader
.biHeight
? 0 : BMF_TOPDOWN
,
385 DestSurfObj
= EngLockSurface((HSURF
)DestBitmap
);
387 SourcePalette
= PALETTE_LockPalette(hSourcePalette
);
388 /* FIXME - SourcePalette can be NULL!!! Don't assert here! */
389 ASSERT(SourcePalette
);
390 SourcePaletteType
= SourcePalette
->Mode
;
391 PALETTE_UnlockPalette(hSourcePalette
);
393 DestPalette
= PALETTE_LockPalette(hDestPalette
);
394 /* FIXME - DestPalette can be NULL!!!! Don't assert here!!! */
396 DestPaletteType
= DestPalette
->Mode
;
399 /* FIXME: This is largely incomplete. */
400 if (Info
->bmiHeader
.biBitCount
<= 8)
402 if (Usage
== DIB_RGB_COLORS
)
405 Index
< (1 << Info
->bmiHeader
.biBitCount
) &&
406 Index
< DestPalette
->NumColors
;
409 Info
->bmiColors
[Index
].rgbRed
=
410 DestPalette
->IndexedColors
[Index
].peRed
;
411 Info
->bmiColors
[Index
].rgbGreen
=
412 DestPalette
->IndexedColors
[Index
].peGreen
;
413 Info
->bmiColors
[Index
].rgbBlue
=
414 DestPalette
->IndexedColors
[Index
].peBlue
;
417 if (Usage
== DIB_PAL_COLORS
)
419 DbgPrint("GetDIBits with DIB_PAL_COLORS isn't implemented yet.");
423 PALETTE_UnlockPalette(hDestPalette
);
425 XlateObj
= IntEngCreateXlate(
426 DestPaletteType
, SourcePaletteType
, hDestPalette
, hSourcePalette
);
429 SourcePoint
.y
= StartScan
;
431 /* Determine destination rectangle */
434 DestRect
.right
= DestSize
.cx
;
435 DestRect
.bottom
= DestSize
.cy
;
437 if (EngCopyBits(DestSurfObj
, &BitmapObj
->SurfObj
,
438 NULL
, XlateObj
, &DestRect
, &SourcePoint
))
443 EngDeleteXlate(XlateObj
);
444 EngUnlockSurface(DestSurfObj
);
448 BITMAPOBJ_UnlockBitmap(hBitmap
);
453 INT STDCALL
NtGdiStretchDIBits(HDC hDC
,
463 CONST BITMAPINFO
*BitsInfo
,
467 HBITMAP hBitmap
, hOldBitmap
;
470 if (!Bits
|| !BitsInfo
)
472 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
476 hdcMem
= NtGdiCreateCompatableDC(hDC
);
477 hBitmap
= NtGdiCreateCompatibleBitmap(hDC
, BitsInfo
->bmiHeader
.biWidth
,
478 BitsInfo
->bmiHeader
.biHeight
);
479 hOldBitmap
= NtGdiSelectObject(hdcMem
, hBitmap
);
481 if (BitsInfo
->bmiHeader
.biCompression
== BI_RLE4
||
482 BitsInfo
->bmiHeader
.biCompression
== BI_RLE8
)
484 /* copy existing bitmap from destination dc */
485 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
486 NtGdiBitBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
487 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, ROP
);
489 NtGdiStretchBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
490 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
494 NtGdiSetDIBits(hdcMem
, hBitmap
, 0, BitsInfo
->bmiHeader
.biHeight
, Bits
,
497 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
498 left (negative biHeight) */
499 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
500 NtGdiBitBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
501 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
504 NtGdiStretchBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
505 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
506 SrcWidth
, SrcHeight
, ROP
);
508 NtGdiSelectObject(hdcMem
, hOldBitmap
);
509 NtGdiDeleteDC(hdcMem
);
510 NtGdiDeleteObject(hBitmap
);
515 LONG STDCALL
NtGdiGetBitmapBits(HBITMAP hBitmap
,
522 bmp
= BITMAPOBJ_LockBitmap (hBitmap
);
528 /* If the bits vector is null, the function should return the read size */
531 ret
= bmp
->SurfObj
.cjBits
;
532 BITMAPOBJ_UnlockBitmap (hBitmap
);
538 DPRINT ("(%ld): Negative number of bytes passed???\n", Count
);
542 /* Only get entire lines */
543 height
= Count
/ abs(bmp
->SurfObj
.lDelta
);
544 if (height
> bmp
->SurfObj
.sizlBitmap
.cy
)
546 height
= bmp
->SurfObj
.sizlBitmap
.cy
;
548 Count
= height
* abs(bmp
->SurfObj
.lDelta
);
551 DPRINT("Less then one entire line requested\n");
552 BITMAPOBJ_UnlockBitmap (hBitmap
);
556 DPRINT("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
557 hBitmap
, Count
, Bits
, bmp
->SurfObj
.sizlBitmap
.cx
,
558 bmp
->SurfObj
.sizlBitmap
.cy
,
559 1 << BitsPerFormat(bmp
->SurfObj
.iBitmapFormat
), height
);
561 /* FIXME: Call DDI CopyBits here if available */
564 DPRINT("Calling device specific BitmapBits\n");
565 if(bmp
->DDBitmap
->funcs
->pBitmapBits
)
567 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hbitmap
, bits
, count
,
572 ERR_(bitmap
)("BitmapBits == NULL??\n");
579 memcpy(Bits
, bmp
->SurfObj
.pvBits
, Count
);
583 BITMAPOBJ_UnlockBitmap (hBitmap
);
588 static HBITMAP FASTCALL
589 IntCreateDIBitmap(PDC Dc
, const BITMAPINFOHEADER
*header
,
590 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
600 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
, &compr
) == -1) return 0;
602 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
603 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
605 if (bpp
!= 1) fColor
= TRUE
;
606 else if ((coloruse
!= DIB_RGB_COLORS
) ||
607 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
610 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
612 RGBQUAD
*rgb
= data
->bmiColors
;
613 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
615 // Check if the first color of the colormap is black
616 if ((col
== RGB(0, 0, 0)))
619 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
621 // If the second color is white, create a monochrome bitmap
622 fColor
= (col
!= RGB(0xff,0xff,0xff));
626 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
628 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
629 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
631 if ((col
== RGB(0,0,0)))
634 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
635 fColor
= (col
!= RGB(0xff,0xff,0xff));
641 DPRINT("(%ld): wrong size for data\n", data
->bmiHeader
.biSize
);
646 // Now create the bitmap
649 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
653 handle
= NtGdiCreateBitmap( width
, height
, 1, 1, NULL
);
659 if (NULL
!= handle
&& CBM_INIT
== init
)
661 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
667 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
668 // The DDB that is created will be whatever bit depth your reference DC is
669 HBITMAP STDCALL
NtGdiCreateDIBitmap(HDC hDc
, const BITMAPINFOHEADER
*Header
,
670 DWORD Init
, LPCVOID Bits
, const BITMAPINFO
*Data
,
679 SetLastWin32Error(ERROR_INVALID_HANDLE
);
683 Bmp
= IntCreateDIBitmap(Dc
, Header
, Init
, Bits
, Data
, ColorUse
);
690 HBITMAP STDCALL
NtGdiCreateDIBSection(HDC hDC
,
691 CONST BITMAPINFO
*bmi
,
699 BOOL bDesktopDC
= FALSE
;
701 // If the reference hdc is null, take the desktop dc
704 hDC
= NtGdiCreateCompatableDC(0);
708 if ((dc
= DC_LockDc(hDC
)))
710 hbitmap
= DIB_CreateDIBSection ( dc
, (BITMAPINFO
*)bmi
, Usage
, Bits
,
711 hSection
, dwOffset
, 0);
722 DIB_CreateDIBSection(
723 PDC dc
, BITMAPINFO
*bmi
, UINT usage
,
724 LPVOID
*bits
, HANDLE section
,
725 DWORD offset
, DWORD ovr_pitch
)
728 BITMAPOBJ
*bmp
= NULL
;
729 DIBSECTION
*dib
= NULL
;
731 // Fill BITMAP32 structure with DIB data
732 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
739 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
740 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
741 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
743 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
745 bm
.bmWidth
= bi
->biWidth
;
746 bm
.bmHeight
= effHeight
;
747 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
749 bm
.bmPlanes
= bi
->biPlanes
;
750 bm
.bmBitsPixel
= bi
->biBitCount
;
753 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
754 // we're dealing with a compressed bitmap. Otherwise, use width * height.
755 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
756 ? bi
->biSizeImage
: (ULONG
) (bm
.bmWidthBytes
* effHeight
);
760 /* bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
761 0L, offset, totalSize); */
762 DbgPrint("DIB_CreateDIBSection: Cannot yet handle section DIBs\n");
765 else if (ovr_pitch
&& offset
)
766 bm
.bmBits
= (LPVOID
) offset
;
769 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
772 if(usage
== DIB_PAL_COLORS
)
773 memcpy(bmi
->bmiColors
, (UINT
*)DIB_MapPaletteColors(dc
, bmi
), sizeof(UINT
*));
775 // Allocate Memory for DIB and fill structure
778 dib
= ExAllocatePoolWithTag(PagedPool
, sizeof(DIBSECTION
), TAG_DIB
);
779 RtlZeroMemory(dib
, sizeof(DIBSECTION
));
786 dib
->dsBmih
.biSizeImage
= totalSize
;
788 /* Set dsBitfields values */
789 if ( usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
791 dib
->dsBitfields
[0] = dib
->dsBitfields
[1] = dib
->dsBitfields
[2] = 0;
793 else switch(bi
->biBitCount
)
796 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)bmi
->bmiColors
: 0x7c00;
797 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 1) : 0x03e0;
798 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 2) : 0x001f; break;
801 dib
->dsBitfields
[0] = 0xff0000;
802 dib
->dsBitfields
[1] = 0x00ff00;
803 dib
->dsBitfields
[2] = 0x0000ff;
807 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)bmi
->bmiColors
: 0xff0000;
808 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 1) : 0x00ff00;
809 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 2) : 0x0000ff;
812 dib
->dshSection
= section
;
813 dib
->dsOffset
= offset
;
816 // Create Device Dependent Bitmap and add DIB pointer
819 Size
.cx
= bm
.bmWidth
;
820 Size
.cy
= bm
.bmHeight
;
821 res
= IntCreateBitmap(Size
, bm
.bmWidthBytes
,
822 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
823 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
824 (bi
->biHeight
> 0 ? 0 : BMF_TOPDOWN
),
830 bmp
= BITMAPOBJ_LockBitmap(res
);
833 NtGdiDeleteObject(bmp
);
836 bmp
->dib
= (DIBSECTION
*) dib
;
838 /* WINE NOTE: WINE makes use of a colormap, which is a color translation table between the DIB and the X physical
839 device. Obviously, this is left out of the ReactOS implementation. Instead, we call
840 NtGdiSetDIBColorTable. */
841 if(bi
->biBitCount
== 1) { Entries
= 2; } else
842 if(bi
->biBitCount
== 4) { Entries
= 16; } else
843 if(bi
->biBitCount
== 8) { Entries
= 256; }
845 bmp
->ColorMap
= ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
)*Entries
, TAG_COLORMAP
);
846 RtlCopyMemory(bmp
->ColorMap
, bmi
->bmiColors
, sizeof(RGBQUAD
)*Entries
);
849 // Clean up in case of errors
850 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
)
852 DPRINT("got an error res=%08x, bmp=%p, dib=%p, bm.bmBits=%p\n", res
, bmp
, dib
, bm
.bmBits
);
856 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
858 VirtualFree(bm.bmBits, 0L, MEM_RELEASE), bm.bmBits = NULL;
861 if (dib
) { ExFreePool(dib
); dib
= NULL
; }
862 if (bmp
) { bmp
= NULL
; }
863 if (res
) { BITMAPOBJ_FreeBitmap(res
); res
= 0; }
868 BITMAPOBJ_UnlockBitmap(res
);
871 // Return BITMAP handle and storage location
872 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
880 /***********************************************************************
881 * DIB_GetDIBWidthBytes
883 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
884 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
885 * 11/16/1999 (RJJ) lifted from wine
887 INT FASTCALL
DIB_GetDIBWidthBytes (INT width
, INT depth
)
893 case 1: words
= (width
+ 31) >> 5; break;
894 case 4: words
= (width
+ 7) >> 3; break;
895 case 8: words
= (width
+ 3) >> 2; break;
897 case 16: words
= (width
+ 1) >> 1; break;
898 case 24: words
= (width
* 3 + 3) >> 2; break;
901 DPRINT("(%d): Unsupported depth\n", depth
);
909 /***********************************************************************
910 * DIB_GetDIBImageBytes
912 * Return the number of bytes used to hold the image in a DIB bitmap.
913 * 11/16/1999 (RJJ) lifted from wine
916 INT STDCALL
DIB_GetDIBImageBytes (INT width
, INT height
, INT depth
)
918 return DIB_GetDIBWidthBytes( width
, depth
) * (height
< 0 ? -height
: height
);
921 /***********************************************************************
924 * Return the size of the bitmap info structure including color table.
925 * 11/16/1999 (RJJ) lifted from wine
928 INT FASTCALL
DIB_BitmapInfoSize (const BITMAPINFO
* info
, WORD coloruse
)
932 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
934 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
935 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
936 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
938 else /* assume BITMAPINFOHEADER */
940 colors
= info
->bmiHeader
.biClrUsed
;
941 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
942 return sizeof(BITMAPINFOHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
946 INT STDCALL
DIB_GetBitmapInfo (const BITMAPINFOHEADER
*header
,
952 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
954 *width
= header
->biWidth
;
955 *height
= header
->biHeight
;
956 *bpp
= header
->biBitCount
;
957 *compr
= header
->biCompression
;
960 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
962 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
963 *width
= core
->bcWidth
;
964 *height
= core
->bcHeight
;
965 *bpp
= core
->bcBitCount
;
969 DPRINT("(%ld): wrong size for header\n", header
->biSize
);
973 // Converts a Device Independent Bitmap (DIB) to a Device Dependant Bitmap (DDB)
974 // The specified Device Context (DC) defines what the DIB should be converted to
975 PBITMAPOBJ FASTCALL
DIBtoDDB(HGLOBAL hPackedDIB
, HDC hdc
) // FIXME: This should be removed. All references to this function should
976 // change to NtGdiSetDIBits
979 PBITMAPOBJ pBmp
= NULL
;
983 // Get a pointer to the packed DIB's data
984 // pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
987 pbits
= (LPBYTE
)(dib
+ DIB_BitmapInfoSize((BITMAPINFO
*)&dib
->dsBmih
, DIB_RGB_COLORS
));
989 // Create a DDB from the DIB
990 hBmp
= NtGdiCreateDIBitmap ( hdc
, &dib
->dsBmih
, CBM_INIT
,
991 (LPVOID
)pbits
, (BITMAPINFO
*)&dib
->dsBmih
, DIB_RGB_COLORS
);
993 // GlobalUnlock(hPackedDIB);
995 // Retrieve the internal Pixmap from the DDB
996 pBmp
= BITMAPOBJ_LockBitmap(hBmp
);
1002 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1009 palGDI
= PALETTE_LockPalette(dc
->w
.hPalette
);
1013 // RELEASEDCINFO(hDC);
1017 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1018 if (lpbmi
->bmiHeader
.biClrUsed
)
1020 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1023 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1024 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1026 for (i
= 0; i
< nNumColors
; i
++)
1028 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1029 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1030 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1033 // RELEASEDCINFO(hDC);
1034 PALETTE_UnlockPalette(dc
->w
.hPalette
);
1040 BuildDIBPalette (PBITMAPINFO bmi
, PINT paletteType
)
1044 PALETTEENTRY
*palEntries
= NULL
;
1047 // Determine Bits Per Pixel
1048 bits
= bmi
->bmiHeader
.biBitCount
;
1050 // Determine paletteType from Bits Per Pixel
1053 *paletteType
= PAL_INDEXED
;
1057 *paletteType
= PAL_BITFIELDS
;
1061 *paletteType
= PAL_BGR
;
1064 if (bmi
->bmiHeader
.biClrUsed
== 0)
1066 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1070 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1073 if (PAL_INDEXED
== *paletteType
)
1075 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)bmi
->bmiColors
);
1079 hPal
= PALETTE_AllocPalette( *paletteType
, ColorCount
, (ULONG
*)palEntries
, 0, 0, 0 );