From 710a09d490cb8d57340af80ba058bfc7a636c472 Mon Sep 17 00:00:00 2001 From: Timo Kreuzer Date: Wed, 2 May 2012 13:56:55 +0000 Subject: [PATCH] [WIN32K] - Fix a palette leak in DIB_CreateDIBSection - Remove PALETTE_AllocPalette and PALETTE_AllocPaletteIndexedRGB, use PALETTE_AllocPalWithHandle instead svn path=/trunk/; revision=56476 --- reactos/win32ss/gdi/ntgdi/dibobj.c | 49 +++++++++++++---- reactos/win32ss/gdi/ntgdi/palette.c | 81 ----------------------------- reactos/win32ss/gdi/ntgdi/palette.h | 6 --- 3 files changed, 40 insertions(+), 96 deletions(-) diff --git a/reactos/win32ss/gdi/ntgdi/dibobj.c b/reactos/win32ss/gdi/ntgdi/dibobj.c index 768aa318b9f..b5d8cfc412c 100644 --- a/reactos/win32ss/gdi/ntgdi/dibobj.c +++ b/reactos/win32ss/gdi/ntgdi/dibobj.c @@ -1599,7 +1599,14 @@ DIB_CreateDIBSection( /* HACK */ if(hpal != (HPALETTE)0xFFFFFFFF) { - bmp->ppal = PALETTE_ShareLockPalette(hpal); + PPALETTE ppal = PALETTE_ShareLockPalette(hpal); + + if (ppal) + { + if (bmp->ppal) PALETTE_ShareUnlockPalette(bmp->ppal); + bmp->ppal = ppal; + } + /* Lazy delete hpal, it will be freed at surface release */ GreDeleteObject(hpal); } @@ -1771,10 +1778,11 @@ BuildDIBPalette(CONST BITMAPINFO *bmi) { WORD bits; ULONG ColorCount; - HPALETTE hPal; + HPALETTE hpal; + PPALETTE ppal; ULONG RedMask = 0, GreenMask = 0, BlueMask = 0; PDWORD pdwColors = (PDWORD)((PBYTE)bmi + bmi->bmiHeader.biSize); - INT paletteType; + ULONG paletteType, i; // Determine Bits Per Pixel bits = bmi->bmiHeader.biBitCount; @@ -1833,18 +1841,41 @@ BuildDIBPalette(CONST BITMAPINFO *bmi) ColorCount = bmi->bmiHeader.biClrUsed; } - if (PAL_INDEXED == paletteType) + if (paletteType == PAL_INDEXED) { - hPal = PALETTE_AllocPaletteIndexedRGB(ColorCount, (RGBQUAD*)pdwColors); + RGBQUAD* pColors = (RGBQUAD*)((PBYTE)bmi + bmi->bmiHeader.biSize); + + /* Allocate a palette */ + ppal = PALETTE_AllocPalWithHandle(PAL_INDEXED, + ColorCount, + NULL, + 0, 0, 0); + if (!ppal) return NULL; + + /* Copy all colors */ + for (i = 0; i < ColorCount; i++) + { + ppal->IndexedColors[i].peRed = pColors[i].rgbRed; + ppal->IndexedColors[i].peGreen = pColors[i].rgbGreen; + ppal->IndexedColors[i].peBlue = pColors[i].rgbBlue; + ppal->IndexedColors[i].peFlags = 0; + } + + /* Get palette handle and unlock the palette */ + hpal = ppal->BaseObject.hHmgr; + PALETTE_UnlockPalette(ppal); } else { - hPal = PALETTE_AllocPalette(paletteType, 0, - NULL, - RedMask, GreenMask, BlueMask); + ppal = PALETTE_AllocPalWithHandle(paletteType, 0, + NULL, + RedMask, GreenMask, BlueMask); + + hpal = ppal->BaseObject.hHmgr; + PALETTE_UnlockPalette(ppal); } - return hPal; + return hpal; } /* Converts a BITMAPCOREINFO to a BITMAPINFO structure, diff --git a/reactos/win32ss/gdi/ntgdi/palette.c b/reactos/win32ss/gdi/ntgdi/palette.c index e7f70aef790..6f759dac561 100644 --- a/reactos/win32ss/gdi/ntgdi/palette.c +++ b/reactos/win32ss/gdi/ntgdi/palette.c @@ -230,87 +230,6 @@ PALETTE_AllocPalWithHandle( return ppal; } -HPALETTE -FASTCALL -PALETTE_AllocPalette(ULONG Mode, - ULONG NumColors, - ULONG *Colors, - ULONG Red, - ULONG Green, - ULONG Blue) -{ - PPALETTE ppal; - HPALETTE hpal; - - ppal = PALETTE_AllocPalette2(Mode, NumColors, Colors, Red, Green, Blue); - if (!ppal) return NULL; - - hpal = GDIOBJ_hInsertObject(&ppal->BaseObject, GDI_OBJ_HMGR_POWNED); - if (!hpal) - { - DPRINT1("Could not insert palette into handle table.\n"); - GDIOBJ_vFreeObject(&ppal->BaseObject); - return NULL; - } - - PALETTE_UnlockPalette(ppal); - - return hpal; -} - -HPALETTE -FASTCALL -PALETTE_AllocPaletteIndexedRGB(ULONG NumColors, - CONST RGBQUAD *Colors) -{ - HPALETTE NewPalette; - PPALETTE PalGDI; - UINT i; - - PalGDI = (PPALETTE)GDIOBJ_AllocateObject(GDIObjType_PAL_TYPE, - sizeof(PALETTE), - BASEFLAG_LOOKASIDE); - if (!PalGDI) - { - DPRINT1("Could not allocate a palette.\n"); - return NULL; - } - - if (!GDIOBJ_hInsertObject(&PalGDI->BaseObject, GDI_OBJ_HMGR_POWNED)) - { - DPRINT1("Could not insert palette into handle table.\n"); - GDIOBJ_vFreeObject(&PalGDI->BaseObject); - return NULL; - } - - NewPalette = PalGDI->BaseObject.hHmgr; - - PalGDI->flFlags = PAL_INDEXED; - - PalGDI->IndexedColors = ExAllocatePoolWithTag(PagedPool, - sizeof(PALETTEENTRY) * NumColors, - TAG_PALETTE); - if (NULL == PalGDI->IndexedColors) - { - GDIOBJ_vDeleteObject(&PalGDI->BaseObject); - return NULL; - } - - for (i = 0; i < NumColors; i++) - { - PalGDI->IndexedColors[i].peRed = Colors[i].rgbRed; - PalGDI->IndexedColors[i].peGreen = Colors[i].rgbGreen; - PalGDI->IndexedColors[i].peBlue = Colors[i].rgbBlue; - PalGDI->IndexedColors[i].peFlags = 0; - } - - PalGDI->NumColors = NumColors; - - PALETTE_UnlockPalette(PalGDI); - - return NewPalette; -} - BOOL NTAPI PALETTE_Cleanup(PVOID ObjectBody) diff --git a/reactos/win32ss/gdi/ntgdi/palette.h b/reactos/win32ss/gdi/ntgdi/palette.h index e31178bb88e..31240dcc518 100644 --- a/reactos/win32ss/gdi/ntgdi/palette.h +++ b/reactos/win32ss/gdi/ntgdi/palette.h @@ -156,9 +156,3 @@ PALETTE_AllocPalette(ULONG Mode, ULONG Red, ULONG Green, ULONG Blue); - -HPALETTE -FASTCALL -PALETTE_AllocPaletteIndexedRGB(ULONG NumColors, - CONST RGBQUAD *Colors); - -- 2.17.1