2 * $Id: dib.c,v 1.58 2004/12/18 17:15:10 royce 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 if (0 == SourceBitmap
)
164 BITMAPOBJ_UnlockBitmap(hBitmap
);
165 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
169 SourceSurf
= EngLockSurface((HSURF
)SourceBitmap
);
170 if (NULL
== SourceSurf
)
172 EngDeleteSurface((HSURF
)SourceBitmap
);
173 BITMAPOBJ_UnlockBitmap(hBitmap
);
174 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
178 // Destination palette obtained from the hDC
179 hDCPalette
= PALETTE_LockPalette(DC
->DevInfo
->hpalDefault
);
180 if (NULL
== hDCPalette
)
182 EngUnlockSurface(SourceSurf
);
183 EngDeleteSurface((HSURF
)SourceBitmap
);
184 BITMAPOBJ_UnlockBitmap(hBitmap
);
185 SetLastWin32Error(ERROR_INVALID_HANDLE
);
188 DDB_Palette_Type
= hDCPalette
->Mode
;
189 DDB_Palette
= DC
->DevInfo
->hpalDefault
;
190 PALETTE_UnlockPalette(DC
->DevInfo
->hpalDefault
);
192 // Source palette obtained from the BITMAPINFO
193 DIB_Palette
= BuildDIBPalette ( (PBITMAPINFO
)bmi
, (PINT
)&DIB_Palette_Type
);
194 if (NULL
== DIB_Palette
)
196 EngUnlockSurface(SourceSurf
);
197 EngDeleteSurface((HSURF
)SourceBitmap
);
198 BITMAPOBJ_UnlockBitmap(hBitmap
);
199 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
203 // Determine XLATEOBJ for color translation
204 XlateObj
= IntEngCreateXlate(DDB_Palette_Type
, DIB_Palette_Type
, DDB_Palette
, DIB_Palette
);
205 if (NULL
== XlateObj
)
207 PALETTE_FreePalette(DIB_Palette
);
208 EngUnlockSurface(SourceSurf
);
209 EngDeleteSurface((HSURF
)SourceBitmap
);
210 BITMAPOBJ_UnlockBitmap(hBitmap
);
211 SetLastWin32Error(ERROR_NO_SYSTEM_RESOURCES
);
219 // Determine destination rectangle
222 DestRect
.right
= SourceSize
.cx
;
223 DestRect
.bottom
= SourceSize
.cy
;
225 copyBitsResult
= EngCopyBits(DestSurf
, SourceSurf
, NULL
, XlateObj
, &DestRect
, &ZeroPoint
);
227 // If it succeeded, return number of scanlines copies
228 if(copyBitsResult
== TRUE
)
230 result
= SourceSize
.cy
- 1;
234 EngDeleteXlate(XlateObj
);
235 PALETTE_FreePalette(DIB_Palette
);
236 EngUnlockSurface(SourceSurf
);
237 EngDeleteSurface((HSURF
)SourceBitmap
);
239 // if (ColorUse == DIB_PAL_COLORS)
240 // WinFree((LPSTR)lpRGB);
242 BITMAPOBJ_UnlockBitmap(hBitmap
);
247 // Converts a DIB to a device-dependent bitmap
255 CONST BITMAPINFO
*bmi
,
264 SetLastWin32Error(ERROR_INVALID_HANDLE
);
268 Ret
= IntSetDIBits(Dc
, hBitmap
, StartScan
, ScanLines
, Bits
, bmi
, ColorUse
);
276 NtGdiSetDIBitsToDevice(
287 CONST BITMAPINFO
*bmi
,
294 /* Converts a device-dependent bitmap to a DIB */
296 NtGdiGetDIBits(HDC hDC
,
304 BITMAPOBJ
*BitmapObj
;
305 SURFOBJ
*DestSurfObj
;
309 HPALETTE hSourcePalette
;
310 HPALETTE hDestPalette
;
311 PPALGDI SourcePalette
;
313 ULONG SourcePaletteType
;
314 ULONG DestPaletteType
;
321 /* Get handle for the palette in DC. */
325 SetLastWin32Error(ERROR_INVALID_HANDLE
);
328 hSourcePalette
= Dc
->w
.hPalette
;
329 /* FIXME: This is incorrect. hDestPalette should be something other. */
330 hDestPalette
= Dc
->DevInfo
->hpalDefault
;
333 /* Get pointer to the source bitmap object. */
334 BitmapObj
= BITMAPOBJ_LockBitmap(hBitmap
);
335 if (BitmapObj
== NULL
)
337 SetLastWin32Error(ERROR_INVALID_HANDLE
);
343 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
) ||
344 Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
346 Info
->bmiHeader
.biWidth
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
347 Info
->bmiHeader
.biHeight
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
348 /* Report negtive height for top-down bitmaps. */
349 if (BitmapObj
->SurfObj
.lDelta
> 0)
350 Info
->bmiHeader
.biHeight
= -Info
->bmiHeader
.biHeight
;
351 Info
->bmiHeader
.biPlanes
= 1;
352 Info
->bmiHeader
.biBitCount
= BitsPerFormat(BitmapObj
->SurfObj
.iBitmapFormat
);
353 if (Info
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
355 switch (BitmapObj
->SurfObj
.iBitmapFormat
)
357 case BMF_1BPP
: case BMF_4BPP
: case BMF_8BPP
:
358 case BMF_16BPP
: case BMF_24BPP
: case BMF_32BPP
:
359 Info
->bmiHeader
.biCompression
= BI_RGB
;
362 Info
->bmiHeader
.biCompression
= BI_RLE4
;
365 Info
->bmiHeader
.biCompression
= BI_RLE8
;
368 Info
->bmiHeader
.biCompression
= BI_JPEG
;
371 Info
->bmiHeader
.biCompression
= BI_PNG
;
374 Info
->bmiHeader
.biSizeImage
= BitmapObj
->SurfObj
.cjBits
;
375 Info
->bmiHeader
.biXPelsPerMeter
= 0; /* FIXME */
376 Info
->bmiHeader
.biYPelsPerMeter
= 0; /* FIXME */
377 Info
->bmiHeader
.biClrUsed
=
378 Info
->bmiHeader
.biClrImportant
= 1 << Info
->bmiHeader
.biBitCount
; /* FIXME */
379 Result
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
385 if (StartScan
> BitmapObj
->SurfObj
.sizlBitmap
.cy
)
391 ScanLines
= min(ScanLines
, BitmapObj
->SurfObj
.sizlBitmap
.cy
- StartScan
);
392 DestSize
.cx
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
393 DestSize
.cy
= ScanLines
;
394 DestBitmap
= EngCreateBitmap(
395 DestSize
, BitmapObj
->SurfObj
.lDelta
,
396 BitmapFormat(Info
->bmiHeader
.biBitCount
, Info
->bmiHeader
.biCompression
),
397 0 < Info
->bmiHeader
.biHeight
? 0 : BMF_TOPDOWN
,
399 DestSurfObj
= EngLockSurface((HSURF
)DestBitmap
);
401 SourcePalette
= PALETTE_LockPalette(hSourcePalette
);
402 /* FIXME - SourcePalette can be NULL!!! Don't assert here! */
403 ASSERT(SourcePalette
);
404 SourcePaletteType
= SourcePalette
->Mode
;
405 PALETTE_UnlockPalette(hSourcePalette
);
407 DestPalette
= PALETTE_LockPalette(hDestPalette
);
408 /* FIXME - DestPalette can be NULL!!!! Don't assert here!!! */
410 DestPaletteType
= DestPalette
->Mode
;
413 /* FIXME: This is largely incomplete. */
414 if (Info
->bmiHeader
.biBitCount
<= 8)
416 if (Usage
== DIB_RGB_COLORS
)
419 Index
< (1 << Info
->bmiHeader
.biBitCount
) &&
420 Index
< DestPalette
->NumColors
;
423 Info
->bmiColors
[Index
].rgbRed
=
424 DestPalette
->IndexedColors
[Index
].peRed
;
425 Info
->bmiColors
[Index
].rgbGreen
=
426 DestPalette
->IndexedColors
[Index
].peGreen
;
427 Info
->bmiColors
[Index
].rgbBlue
=
428 DestPalette
->IndexedColors
[Index
].peBlue
;
431 if (Usage
== DIB_PAL_COLORS
)
433 DbgPrint("GetDIBits with DIB_PAL_COLORS isn't implemented yet.");
437 PALETTE_UnlockPalette(hDestPalette
);
439 XlateObj
= IntEngCreateXlate(
440 DestPaletteType
, SourcePaletteType
, hDestPalette
, hSourcePalette
);
443 SourcePoint
.y
= StartScan
;
445 /* Determine destination rectangle */
448 DestRect
.right
= DestSize
.cx
;
449 DestRect
.bottom
= DestSize
.cy
;
451 if (EngCopyBits(DestSurfObj
, &BitmapObj
->SurfObj
,
452 NULL
, XlateObj
, &DestRect
, &SourcePoint
))
457 EngDeleteXlate(XlateObj
);
458 EngUnlockSurface(DestSurfObj
);
462 BITMAPOBJ_UnlockBitmap(hBitmap
);
467 INT STDCALL
NtGdiStretchDIBits(HDC hDC
,
477 CONST BITMAPINFO
*BitsInfo
,
481 HBITMAP hBitmap
, hOldBitmap
;
484 if (!Bits
|| !BitsInfo
)
486 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
490 hdcMem
= NtGdiCreateCompatableDC(hDC
);
491 hBitmap
= NtGdiCreateCompatibleBitmap(hDC
, BitsInfo
->bmiHeader
.biWidth
,
492 BitsInfo
->bmiHeader
.biHeight
);
493 hOldBitmap
= NtGdiSelectObject(hdcMem
, hBitmap
);
495 if (BitsInfo
->bmiHeader
.biCompression
== BI_RLE4
||
496 BitsInfo
->bmiHeader
.biCompression
== BI_RLE8
)
498 /* copy existing bitmap from destination dc */
499 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
500 NtGdiBitBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
501 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, ROP
);
503 NtGdiStretchBlt(hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
504 SrcWidth
, SrcHeight
, hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
508 NtGdiSetDIBits(hdcMem
, hBitmap
, 0, BitsInfo
->bmiHeader
.biHeight
, Bits
,
511 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
512 left (negative biHeight) */
513 if (SrcWidth
== DestWidth
&& SrcHeight
== DestHeight
)
514 NtGdiBitBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
515 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
518 NtGdiStretchBlt(hDC
, XDest
, YDest
, DestWidth
, DestHeight
,
519 hdcMem
, XSrc
, abs(BitsInfo
->bmiHeader
.biHeight
) - SrcHeight
- YSrc
,
520 SrcWidth
, SrcHeight
, ROP
);
522 NtGdiSelectObject(hdcMem
, hOldBitmap
);
523 NtGdiDeleteDC(hdcMem
);
524 NtGdiDeleteObject(hBitmap
);
529 LONG STDCALL
NtGdiGetBitmapBits(HBITMAP hBitmap
,
536 bmp
= BITMAPOBJ_LockBitmap (hBitmap
);
542 /* If the bits vector is null, the function should return the read size */
545 ret
= bmp
->SurfObj
.cjBits
;
546 BITMAPOBJ_UnlockBitmap (hBitmap
);
552 DPRINT ("(%ld): Negative number of bytes passed???\n", Count
);
556 /* Only get entire lines */
557 height
= Count
/ abs(bmp
->SurfObj
.lDelta
);
558 if (height
> bmp
->SurfObj
.sizlBitmap
.cy
)
560 height
= bmp
->SurfObj
.sizlBitmap
.cy
;
562 Count
= height
* abs(bmp
->SurfObj
.lDelta
);
565 DPRINT("Less then one entire line requested\n");
566 BITMAPOBJ_UnlockBitmap (hBitmap
);
570 DPRINT("(%08x, %ld, %p) %dx%d %d colors fetched height: %ld\n",
571 hBitmap
, Count
, Bits
, bmp
->SurfObj
.sizlBitmap
.cx
,
572 bmp
->SurfObj
.sizlBitmap
.cy
,
573 1 << BitsPerFormat(bmp
->SurfObj
.iBitmapFormat
), height
);
575 /* FIXME: Call DDI CopyBits here if available */
578 DPRINT("Calling device specific BitmapBits\n");
579 if(bmp
->DDBitmap
->funcs
->pBitmapBits
)
581 ret
= bmp
->DDBitmap
->funcs
->pBitmapBits(hbitmap
, bits
, count
,
586 ERR_(bitmap
)("BitmapBits == NULL??\n");
593 memcpy(Bits
, bmp
->SurfObj
.pvBits
, Count
);
597 BITMAPOBJ_UnlockBitmap (hBitmap
);
602 static HBITMAP FASTCALL
603 IntCreateDIBitmap(PDC Dc
, const BITMAPINFOHEADER
*header
,
604 DWORD init
, LPCVOID bits
, const BITMAPINFO
*data
,
614 if (DIB_GetBitmapInfo( header
, &width
, &height
, &bpp
, &compr
) == -1) return 0;
616 // Check if we should create a monochrome or color bitmap. We create a monochrome bitmap only if it has exactly 2
617 // colors, which are black followed by white, nothing else. In all other cases, we create a color bitmap.
619 if (bpp
!= 1) fColor
= TRUE
;
620 else if ((coloruse
!= DIB_RGB_COLORS
) ||
621 (init
!= CBM_INIT
) || !data
) fColor
= FALSE
;
624 if (data
->bmiHeader
.biSize
== sizeof(BITMAPINFOHEADER
))
626 RGBQUAD
*rgb
= data
->bmiColors
;
627 DWORD col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
629 // Check if the first color of the colormap is black
630 if ((col
== RGB(0, 0, 0)))
633 col
= RGB( rgb
->rgbRed
, rgb
->rgbGreen
, rgb
->rgbBlue
);
635 // If the second color is white, create a monochrome bitmap
636 fColor
= (col
!= RGB(0xff,0xff,0xff));
640 else if (data
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
642 RGBTRIPLE
*rgb
= ((BITMAPCOREINFO
*)data
)->bmciColors
;
643 DWORD col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
645 if ((col
== RGB(0,0,0)))
648 col
= RGB( rgb
->rgbtRed
, rgb
->rgbtGreen
, rgb
->rgbtBlue
);
649 fColor
= (col
!= RGB(0xff,0xff,0xff));
655 DPRINT("(%ld): wrong size for data\n", data
->bmiHeader
.biSize
);
660 // Now create the bitmap
663 handle
= IntCreateCompatibleBitmap(Dc
, width
, height
);
667 handle
= NtGdiCreateBitmap( width
, height
, 1, 1, NULL
);
673 if (NULL
!= handle
&& CBM_INIT
== init
)
675 IntSetDIBits(Dc
, handle
, 0, height
, bits
, data
, coloruse
);
681 // The CreateDIBitmap function creates a device-dependent bitmap (DDB) from a DIB and, optionally, sets the bitmap bits
682 // The DDB that is created will be whatever bit depth your reference DC is
683 HBITMAP STDCALL
NtGdiCreateDIBitmap(HDC hDc
, const BITMAPINFOHEADER
*Header
,
684 DWORD Init
, LPCVOID Bits
, const BITMAPINFO
*Data
,
693 SetLastWin32Error(ERROR_INVALID_HANDLE
);
697 Bmp
= IntCreateDIBitmap(Dc
, Header
, Init
, Bits
, Data
, ColorUse
);
704 HBITMAP STDCALL
NtGdiCreateDIBSection(HDC hDC
,
705 CONST BITMAPINFO
*bmi
,
713 BOOL bDesktopDC
= FALSE
;
715 // If the reference hdc is null, take the desktop dc
718 hDC
= NtGdiCreateCompatableDC(0);
722 if ((dc
= DC_LockDc(hDC
)))
724 hbitmap
= DIB_CreateDIBSection ( dc
, (BITMAPINFO
*)bmi
, Usage
, Bits
,
725 hSection
, dwOffset
, 0);
736 DIB_CreateDIBSection(
737 PDC dc
, BITMAPINFO
*bmi
, UINT usage
,
738 LPVOID
*bits
, HANDLE section
,
739 DWORD offset
, DWORD ovr_pitch
)
742 BITMAPOBJ
*bmp
= NULL
;
743 DIBSECTION
*dib
= NULL
;
745 // Fill BITMAP32 structure with DIB data
746 BITMAPINFOHEADER
*bi
= &bmi
->bmiHeader
;
753 DPRINT("format (%ld,%ld), planes %d, bpp %d, size %ld, colors %ld (%s)\n",
754 bi
->biWidth
, bi
->biHeight
, bi
->biPlanes
, bi
->biBitCount
,
755 bi
->biSizeImage
, bi
->biClrUsed
, usage
== DIB_PAL_COLORS
? "PAL" : "RGB");
757 effHeight
= bi
->biHeight
>= 0 ? bi
->biHeight
: -bi
->biHeight
;
759 bm
.bmWidth
= bi
->biWidth
;
760 bm
.bmHeight
= effHeight
;
761 bm
.bmWidthBytes
= ovr_pitch
? ovr_pitch
: (ULONG
) DIB_GetDIBWidthBytes(bm
.bmWidth
, bi
->biBitCount
);
763 bm
.bmPlanes
= bi
->biPlanes
;
764 bm
.bmBitsPixel
= bi
->biBitCount
;
767 // Get storage location for DIB bits. Only use biSizeImage if it's valid and
768 // we're dealing with a compressed bitmap. Otherwise, use width * height.
769 totalSize
= bi
->biSizeImage
&& bi
->biCompression
!= BI_RGB
770 ? bi
->biSizeImage
: (ULONG
) (bm
.bmWidthBytes
* effHeight
);
774 /* bm.bmBits = MapViewOfFile(section, FILE_MAP_ALL_ACCESS,
775 0L, offset, totalSize); */
776 DbgPrint("DIB_CreateDIBSection: Cannot yet handle section DIBs\n");
779 else if (ovr_pitch
&& offset
)
780 bm
.bmBits
= (LPVOID
) offset
;
783 bm
.bmBits
= EngAllocUserMem(totalSize
, 0);
786 if(usage
== DIB_PAL_COLORS
)
787 memcpy(bmi
->bmiColors
, (UINT
*)DIB_MapPaletteColors(dc
, bmi
), sizeof(UINT
*));
789 // Allocate Memory for DIB and fill structure
792 dib
= ExAllocatePoolWithTag(PagedPool
, sizeof(DIBSECTION
), TAG_DIB
);
793 RtlZeroMemory(dib
, sizeof(DIBSECTION
));
800 dib
->dsBmih
.biSizeImage
= totalSize
;
802 /* Set dsBitfields values */
803 if ( usage
== DIB_PAL_COLORS
|| bi
->biBitCount
<= 8)
805 dib
->dsBitfields
[0] = dib
->dsBitfields
[1] = dib
->dsBitfields
[2] = 0;
807 else switch(bi
->biBitCount
)
810 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)bmi
->bmiColors
: 0x7c00;
811 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 1) : 0x03e0;
812 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 2) : 0x001f; break;
815 dib
->dsBitfields
[0] = 0xff0000;
816 dib
->dsBitfields
[1] = 0x00ff00;
817 dib
->dsBitfields
[2] = 0x0000ff;
821 dib
->dsBitfields
[0] = (bi
->biCompression
== BI_BITFIELDS
) ? *(DWORD
*)bmi
->bmiColors
: 0xff0000;
822 dib
->dsBitfields
[1] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 1) : 0x00ff00;
823 dib
->dsBitfields
[2] = (bi
->biCompression
== BI_BITFIELDS
) ? *((DWORD
*)bmi
->bmiColors
+ 2) : 0x0000ff;
826 dib
->dshSection
= section
;
827 dib
->dsOffset
= offset
;
830 // Create Device Dependent Bitmap and add DIB pointer
833 Size
.cx
= bm
.bmWidth
;
834 Size
.cy
= bm
.bmHeight
;
835 res
= IntCreateBitmap(Size
, bm
.bmWidthBytes
,
836 BitmapFormat(bi
->biBitCount
* bi
->biPlanes
, bi
->biCompression
),
837 BMF_DONTCACHE
| BMF_USERMEM
| BMF_NOZEROINIT
|
838 (bi
->biHeight
> 0 ? 0 : BMF_TOPDOWN
),
844 bmp
= BITMAPOBJ_LockBitmap(res
);
847 NtGdiDeleteObject(bmp
);
850 bmp
->dib
= (DIBSECTION
*) dib
;
852 /* WINE NOTE: WINE makes use of a colormap, which is a color translation table between the DIB and the X physical
853 device. Obviously, this is left out of the ReactOS implementation. Instead, we call
854 NtGdiSetDIBColorTable. */
855 if(bi
->biBitCount
== 1) { Entries
= 2; } else
856 if(bi
->biBitCount
== 4) { Entries
= 16; } else
857 if(bi
->biBitCount
== 8) { Entries
= 256; }
859 bmp
->ColorMap
= ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
)*Entries
, TAG_COLORMAP
);
860 RtlCopyMemory(bmp
->ColorMap
, bmi
->bmiColors
, sizeof(RGBQUAD
)*Entries
);
863 // Clean up in case of errors
864 if (!res
|| !bmp
|| !dib
|| !bm
.bmBits
)
866 DPRINT("got an error res=%08x, bmp=%p, dib=%p, bm.bmBits=%p\n", res
, bmp
, dib
, bm
.bmBits
);
870 UnmapViewOfFile(bm.bmBits), bm.bmBits = NULL;
872 VirtualFree(bm.bmBits, 0L, MEM_RELEASE), bm.bmBits = NULL;
875 if (dib
) { ExFreePool(dib
); dib
= NULL
; }
876 if (bmp
) { bmp
= NULL
; }
877 if (res
) { BITMAPOBJ_FreeBitmap(res
); res
= 0; }
882 BITMAPOBJ_UnlockBitmap(res
);
885 // Return BITMAP handle and storage location
886 if (NULL
!= bm
.bmBits
&& NULL
!= bits
)
894 /***********************************************************************
895 * DIB_GetDIBWidthBytes
897 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
898 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
899 * 11/16/1999 (RJJ) lifted from wine
901 INT FASTCALL
DIB_GetDIBWidthBytes (INT width
, INT depth
)
907 case 1: words
= (width
+ 31) >> 5; break;
908 case 4: words
= (width
+ 7) >> 3; break;
909 case 8: words
= (width
+ 3) >> 2; break;
911 case 16: words
= (width
+ 1) >> 1; break;
912 case 24: words
= (width
* 3 + 3) >> 2; break;
915 DPRINT("(%d): Unsupported depth\n", depth
);
923 /***********************************************************************
924 * DIB_GetDIBImageBytes
926 * Return the number of bytes used to hold the image in a DIB bitmap.
927 * 11/16/1999 (RJJ) lifted from wine
930 INT STDCALL
DIB_GetDIBImageBytes (INT width
, INT height
, INT depth
)
932 return DIB_GetDIBWidthBytes( width
, depth
) * (height
< 0 ? -height
: height
);
935 /***********************************************************************
938 * Return the size of the bitmap info structure including color table.
939 * 11/16/1999 (RJJ) lifted from wine
942 INT FASTCALL
DIB_BitmapInfoSize (const BITMAPINFO
* info
, WORD coloruse
)
946 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
948 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)info
;
949 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
950 return sizeof(BITMAPCOREHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
952 else /* assume BITMAPINFOHEADER */
954 colors
= info
->bmiHeader
.biClrUsed
;
955 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8)) colors
= 1 << info
->bmiHeader
.biBitCount
;
956 return sizeof(BITMAPINFOHEADER
) + colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
960 INT STDCALL
DIB_GetBitmapInfo (const BITMAPINFOHEADER
*header
,
966 if (header
->biSize
== sizeof(BITMAPINFOHEADER
))
968 *width
= header
->biWidth
;
969 *height
= header
->biHeight
;
970 *bpp
= header
->biBitCount
;
971 *compr
= header
->biCompression
;
974 if (header
->biSize
== sizeof(BITMAPCOREHEADER
))
976 BITMAPCOREHEADER
*core
= (BITMAPCOREHEADER
*)header
;
977 *width
= core
->bcWidth
;
978 *height
= core
->bcHeight
;
979 *bpp
= core
->bcBitCount
;
983 DPRINT("(%ld): wrong size for header\n", header
->biSize
);
987 // Converts a Device Independent Bitmap (DIB) to a Device Dependant Bitmap (DDB)
988 // The specified Device Context (DC) defines what the DIB should be converted to
989 PBITMAPOBJ FASTCALL
DIBtoDDB(HGLOBAL hPackedDIB
, HDC hdc
) // FIXME: This should be removed. All references to this function should
990 // change to NtGdiSetDIBits
993 PBITMAPOBJ pBmp
= NULL
;
997 // Get a pointer to the packed DIB's data
998 // pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
1001 pbits
= (LPBYTE
)(dib
+ DIB_BitmapInfoSize((BITMAPINFO
*)&dib
->dsBmih
, DIB_RGB_COLORS
));
1003 // Create a DDB from the DIB
1004 hBmp
= NtGdiCreateDIBitmap ( hdc
, &dib
->dsBmih
, CBM_INIT
,
1005 (LPVOID
)pbits
, (BITMAPINFO
*)&dib
->dsBmih
, DIB_RGB_COLORS
);
1007 // GlobalUnlock(hPackedDIB);
1009 // Retrieve the internal Pixmap from the DDB
1010 pBmp
= BITMAPOBJ_LockBitmap(hBmp
);
1016 DIB_MapPaletteColors(PDC dc
, CONST BITMAPINFO
* lpbmi
)
1023 palGDI
= PALETTE_LockPalette(dc
->w
.hPalette
);
1027 // RELEASEDCINFO(hDC);
1031 nNumColors
= 1 << lpbmi
->bmiHeader
.biBitCount
;
1032 if (lpbmi
->bmiHeader
.biClrUsed
)
1034 nNumColors
= min(nNumColors
, lpbmi
->bmiHeader
.biClrUsed
);
1037 lpRGB
= (RGBQUAD
*)ExAllocatePoolWithTag(PagedPool
, sizeof(RGBQUAD
) * nNumColors
, TAG_COLORMAP
);
1038 lpIndex
= (USHORT
*)&lpbmi
->bmiColors
[0];
1040 for (i
= 0; i
< nNumColors
; i
++)
1042 lpRGB
[i
].rgbRed
= palGDI
->IndexedColors
[*lpIndex
].peRed
;
1043 lpRGB
[i
].rgbGreen
= palGDI
->IndexedColors
[*lpIndex
].peGreen
;
1044 lpRGB
[i
].rgbBlue
= palGDI
->IndexedColors
[*lpIndex
].peBlue
;
1047 // RELEASEDCINFO(hDC);
1048 PALETTE_UnlockPalette(dc
->w
.hPalette
);
1054 BuildDIBPalette (PBITMAPINFO bmi
, PINT paletteType
)
1058 PALETTEENTRY
*palEntries
= NULL
;
1061 // Determine Bits Per Pixel
1062 bits
= bmi
->bmiHeader
.biBitCount
;
1064 // Determine paletteType from Bits Per Pixel
1067 *paletteType
= PAL_INDEXED
;
1071 *paletteType
= PAL_BITFIELDS
;
1075 *paletteType
= PAL_BGR
;
1078 if (bmi
->bmiHeader
.biClrUsed
== 0)
1080 ColorCount
= 1 << bmi
->bmiHeader
.biBitCount
;
1084 ColorCount
= bmi
->bmiHeader
.biClrUsed
;
1087 if (PAL_INDEXED
== *paletteType
)
1089 hPal
= PALETTE_AllocPaletteIndexedRGB(ColorCount
, (RGBQUAD
*)bmi
->bmiColors
);
1093 hPal
= PALETTE_AllocPalette( *paletteType
, ColorCount
, (ULONG
*)palEntries
, 0, 0, 0 );