2 * Copyright 2009 Vincent Povirk for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wincodecs_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
38 const IWICPaletteVtbl *lpIWICPaletteVtbl;
42 WICBitmapPaletteType type;
43 CRITICAL_SECTION lock; /* must be held when count, colors, or type is accessed */
46 static HRESULT WINAPI PaletteImpl_QueryInterface(IWICPalette *iface, REFIID iid,
49 PaletteImpl *This = (PaletteImpl*)iface;
50 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
52 if (!ppv) return E_INVALIDARG;
54 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICPalette, iid))
64 IUnknown_AddRef((IUnknown*)*ppv);
68 static ULONG WINAPI PaletteImpl_AddRef(IWICPalette *iface)
70 PaletteImpl *This = (PaletteImpl*)iface;
71 ULONG ref = InterlockedIncrement(&This->ref);
73 TRACE("(%p) refcount=%u\n", iface, ref);
78 static ULONG WINAPI PaletteImpl_Release(IWICPalette *iface)
80 PaletteImpl *This = (PaletteImpl*)iface;
81 ULONG ref = InterlockedDecrement(&This->ref);
83 TRACE("(%p) refcount=%u\n", iface, ref);
87 This->lock.DebugInfo->Spare[0] = 0;
88 DeleteCriticalSection(&This->lock);
89 HeapFree(GetProcessHeap(), 0, This->colors);
90 HeapFree(GetProcessHeap(), 0, This);
96 static HRESULT WINAPI PaletteImpl_InitializePredefined(IWICPalette *iface,
97 WICBitmapPaletteType ePaletteType, BOOL fAddTransparentColor)
99 FIXME("(%p,%u,%i): stub\n", iface, ePaletteType, fAddTransparentColor);
103 static HRESULT WINAPI PaletteImpl_InitializeCustom(IWICPalette *iface,
104 WICColor *pColors, UINT colorCount)
106 PaletteImpl *This = (PaletteImpl*)iface;
107 WICColor *new_colors;
109 TRACE("(%p,%p,%u)\n", iface, pColors, colorCount);
117 if (!pColors) return E_INVALIDARG;
118 new_colors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * colorCount);
119 if (!new_colors) return E_OUTOFMEMORY;
120 memcpy(new_colors, pColors, sizeof(WICColor) * colorCount);
123 EnterCriticalSection(&This->lock);
124 HeapFree(GetProcessHeap(), 0, This->colors);
125 This->colors = new_colors;
126 This->count = colorCount;
127 This->type = WICBitmapPaletteTypeCustom;
128 LeaveCriticalSection(&This->lock);
133 static HRESULT WINAPI PaletteImpl_InitializeFromBitmap(IWICPalette *iface,
134 IWICBitmapSource *pISurface, UINT colorCount, BOOL fAddTransparentColor)
136 FIXME("(%p,%p,%u,%i): stub\n", iface, pISurface, colorCount, fAddTransparentColor);
140 static HRESULT WINAPI PaletteImpl_InitializeFromPalette(IWICPalette *iface,
141 IWICPalette *pIPalette)
143 FIXME("(%p,%p): stub\n", iface, pIPalette);
147 static HRESULT WINAPI PaletteImpl_GetType(IWICPalette *iface,
148 WICBitmapPaletteType *pePaletteType)
150 PaletteImpl *This = (PaletteImpl*)iface;
152 TRACE("(%p,%p)\n", iface, pePaletteType);
154 if (!pePaletteType) return E_INVALIDARG;
156 EnterCriticalSection(&This->lock);
157 *pePaletteType = This->type;
158 LeaveCriticalSection(&This->lock);
163 static HRESULT WINAPI PaletteImpl_GetColorCount(IWICPalette *iface, UINT *pcCount)
165 PaletteImpl *This = (PaletteImpl*)iface;
167 TRACE("(%p,%p)\n", iface, pcCount);
169 if (!pcCount) return E_INVALIDARG;
171 EnterCriticalSection(&This->lock);
172 *pcCount = This->count;
173 LeaveCriticalSection(&This->lock);
178 static HRESULT WINAPI PaletteImpl_GetColors(IWICPalette *iface, UINT colorCount,
179 WICColor *pColors, UINT *pcActualColors)
181 PaletteImpl *This = (PaletteImpl*)iface;
183 TRACE("(%p,%i,%p,%p)\n", iface, colorCount, pColors, pcActualColors);
185 if (!pColors || !pcActualColors) return E_INVALIDARG;
187 EnterCriticalSection(&This->lock);
189 if (This->count < colorCount) colorCount = This->count;
191 memcpy(pColors, This->colors, sizeof(WICColor) * colorCount);
193 *pcActualColors = colorCount;
195 LeaveCriticalSection(&This->lock);
200 static HRESULT WINAPI PaletteImpl_IsBlackWhite(IWICPalette *iface, BOOL *pfIsBlackWhite)
202 PaletteImpl *This = (PaletteImpl*)iface;
204 TRACE("(%p,%p)\n", iface, pfIsBlackWhite);
206 if (!pfIsBlackWhite) return E_INVALIDARG;
208 EnterCriticalSection(&This->lock);
209 if (This->type == WICBitmapPaletteTypeFixedBW)
210 *pfIsBlackWhite = TRUE;
212 *pfIsBlackWhite = FALSE;
213 LeaveCriticalSection(&This->lock);
218 static HRESULT WINAPI PaletteImpl_IsGrayscale(IWICPalette *iface, BOOL *pfIsGrayscale)
220 PaletteImpl *This = (PaletteImpl*)iface;
222 TRACE("(%p,%p)\n", iface, pfIsGrayscale);
224 if (!pfIsGrayscale) return E_INVALIDARG;
226 EnterCriticalSection(&This->lock);
229 case WICBitmapPaletteTypeFixedBW:
230 case WICBitmapPaletteTypeFixedGray4:
231 case WICBitmapPaletteTypeFixedGray16:
232 case WICBitmapPaletteTypeFixedGray256:
233 *pfIsGrayscale = TRUE;
236 *pfIsGrayscale = FALSE;
238 LeaveCriticalSection(&This->lock);
243 static HRESULT WINAPI PaletteImpl_HasAlpha(IWICPalette *iface, BOOL *pfHasAlpha)
245 PaletteImpl *This = (PaletteImpl*)iface;
248 TRACE("(%p,%p)\n", iface, pfHasAlpha);
250 if (!pfHasAlpha) return E_INVALIDARG;
254 EnterCriticalSection(&This->lock);
255 for (i=0; i<This->count; i++)
256 if ((This->colors[i]&0xff000000) != 0xff000000)
261 LeaveCriticalSection(&This->lock);
266 static const IWICPaletteVtbl PaletteImpl_Vtbl = {
267 PaletteImpl_QueryInterface,
270 PaletteImpl_InitializePredefined,
271 PaletteImpl_InitializeCustom,
272 PaletteImpl_InitializeFromBitmap,
273 PaletteImpl_InitializeFromPalette,
275 PaletteImpl_GetColorCount,
276 PaletteImpl_GetColors,
277 PaletteImpl_IsBlackWhite,
278 PaletteImpl_IsGrayscale,
282 HRESULT PaletteImpl_Create(IWICPalette **palette)
286 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PaletteImpl));
287 if (!This) return E_OUTOFMEMORY;
289 This->lpIWICPaletteVtbl = &PaletteImpl_Vtbl;
293 This->type = WICBitmapPaletteTypeCustom;
294 InitializeCriticalSection(&This->lock);
295 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PaletteImpl.lock");
297 *palette = (IWICPalette*)This;