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 IWICPalette IWICPalette_iface;
42 WICBitmapPaletteType type;
43 CRITICAL_SECTION lock; /* must be held when count, colors, or type is accessed */
46 static inline PaletteImpl *impl_from_IWICPalette(IWICPalette *iface)
48 return CONTAINING_RECORD(iface, PaletteImpl, IWICPalette_iface);
51 static HRESULT WINAPI PaletteImpl_QueryInterface(IWICPalette *iface, REFIID iid,
54 PaletteImpl *This = impl_from_IWICPalette(iface);
55 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
57 if (!ppv) return E_INVALIDARG;
59 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICPalette, iid))
69 IUnknown_AddRef((IUnknown*)*ppv);
73 static ULONG WINAPI PaletteImpl_AddRef(IWICPalette *iface)
75 PaletteImpl *This = impl_from_IWICPalette(iface);
76 ULONG ref = InterlockedIncrement(&This->ref);
78 TRACE("(%p) refcount=%u\n", iface, ref);
83 static ULONG WINAPI PaletteImpl_Release(IWICPalette *iface)
85 PaletteImpl *This = impl_from_IWICPalette(iface);
86 ULONG ref = InterlockedDecrement(&This->ref);
88 TRACE("(%p) refcount=%u\n", iface, ref);
92 This->lock.DebugInfo->Spare[0] = 0;
93 DeleteCriticalSection(&This->lock);
94 HeapFree(GetProcessHeap(), 0, This->colors);
95 HeapFree(GetProcessHeap(), 0, This);
101 static HRESULT WINAPI PaletteImpl_InitializePredefined(IWICPalette *iface,
102 WICBitmapPaletteType ePaletteType, BOOL fAddTransparentColor)
104 FIXME("(%p,%u,%i): stub\n", iface, ePaletteType, fAddTransparentColor);
108 static HRESULT WINAPI PaletteImpl_InitializeCustom(IWICPalette *iface,
109 WICColor *pColors, UINT colorCount)
111 PaletteImpl *This = impl_from_IWICPalette(iface);
112 WICColor *new_colors;
114 TRACE("(%p,%p,%u)\n", iface, pColors, colorCount);
122 if (!pColors) return E_INVALIDARG;
123 new_colors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * colorCount);
124 if (!new_colors) return E_OUTOFMEMORY;
125 memcpy(new_colors, pColors, sizeof(WICColor) * colorCount);
128 EnterCriticalSection(&This->lock);
129 HeapFree(GetProcessHeap(), 0, This->colors);
130 This->colors = new_colors;
131 This->count = colorCount;
132 This->type = WICBitmapPaletteTypeCustom;
133 LeaveCriticalSection(&This->lock);
138 static HRESULT WINAPI PaletteImpl_InitializeFromBitmap(IWICPalette *iface,
139 IWICBitmapSource *pISurface, UINT colorCount, BOOL fAddTransparentColor)
141 FIXME("(%p,%p,%u,%i): stub\n", iface, pISurface, colorCount, fAddTransparentColor);
145 static HRESULT WINAPI PaletteImpl_InitializeFromPalette(IWICPalette *iface,
146 IWICPalette *pIPalette)
148 FIXME("(%p,%p): stub\n", iface, pIPalette);
152 static HRESULT WINAPI PaletteImpl_GetType(IWICPalette *iface,
153 WICBitmapPaletteType *pePaletteType)
155 PaletteImpl *This = impl_from_IWICPalette(iface);
157 TRACE("(%p,%p)\n", iface, pePaletteType);
159 if (!pePaletteType) return E_INVALIDARG;
161 EnterCriticalSection(&This->lock);
162 *pePaletteType = This->type;
163 LeaveCriticalSection(&This->lock);
168 static HRESULT WINAPI PaletteImpl_GetColorCount(IWICPalette *iface, UINT *pcCount)
170 PaletteImpl *This = impl_from_IWICPalette(iface);
172 TRACE("(%p,%p)\n", iface, pcCount);
174 if (!pcCount) return E_INVALIDARG;
176 EnterCriticalSection(&This->lock);
177 *pcCount = This->count;
178 LeaveCriticalSection(&This->lock);
183 static HRESULT WINAPI PaletteImpl_GetColors(IWICPalette *iface, UINT colorCount,
184 WICColor *pColors, UINT *pcActualColors)
186 PaletteImpl *This = impl_from_IWICPalette(iface);
188 TRACE("(%p,%i,%p,%p)\n", iface, colorCount, pColors, pcActualColors);
190 if (!pColors || !pcActualColors) return E_INVALIDARG;
192 EnterCriticalSection(&This->lock);
194 if (This->count < colorCount) colorCount = This->count;
196 memcpy(pColors, This->colors, sizeof(WICColor) * colorCount);
198 *pcActualColors = colorCount;
200 LeaveCriticalSection(&This->lock);
205 static HRESULT WINAPI PaletteImpl_IsBlackWhite(IWICPalette *iface, BOOL *pfIsBlackWhite)
207 PaletteImpl *This = impl_from_IWICPalette(iface);
209 TRACE("(%p,%p)\n", iface, pfIsBlackWhite);
211 if (!pfIsBlackWhite) return E_INVALIDARG;
213 EnterCriticalSection(&This->lock);
214 if (This->type == WICBitmapPaletteTypeFixedBW)
215 *pfIsBlackWhite = TRUE;
217 *pfIsBlackWhite = FALSE;
218 LeaveCriticalSection(&This->lock);
223 static HRESULT WINAPI PaletteImpl_IsGrayscale(IWICPalette *iface, BOOL *pfIsGrayscale)
225 PaletteImpl *This = impl_from_IWICPalette(iface);
227 TRACE("(%p,%p)\n", iface, pfIsGrayscale);
229 if (!pfIsGrayscale) return E_INVALIDARG;
231 EnterCriticalSection(&This->lock);
234 case WICBitmapPaletteTypeFixedBW:
235 case WICBitmapPaletteTypeFixedGray4:
236 case WICBitmapPaletteTypeFixedGray16:
237 case WICBitmapPaletteTypeFixedGray256:
238 *pfIsGrayscale = TRUE;
241 *pfIsGrayscale = FALSE;
243 LeaveCriticalSection(&This->lock);
248 static HRESULT WINAPI PaletteImpl_HasAlpha(IWICPalette *iface, BOOL *pfHasAlpha)
250 PaletteImpl *This = impl_from_IWICPalette(iface);
253 TRACE("(%p,%p)\n", iface, pfHasAlpha);
255 if (!pfHasAlpha) return E_INVALIDARG;
259 EnterCriticalSection(&This->lock);
260 for (i=0; i<This->count; i++)
261 if ((This->colors[i]&0xff000000) != 0xff000000)
266 LeaveCriticalSection(&This->lock);
271 static const IWICPaletteVtbl PaletteImpl_Vtbl = {
272 PaletteImpl_QueryInterface,
275 PaletteImpl_InitializePredefined,
276 PaletteImpl_InitializeCustom,
277 PaletteImpl_InitializeFromBitmap,
278 PaletteImpl_InitializeFromPalette,
280 PaletteImpl_GetColorCount,
281 PaletteImpl_GetColors,
282 PaletteImpl_IsBlackWhite,
283 PaletteImpl_IsGrayscale,
287 HRESULT PaletteImpl_Create(IWICPalette **palette)
291 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PaletteImpl));
292 if (!This) return E_OUTOFMEMORY;
294 This->IWICPalette_iface.lpVtbl = &PaletteImpl_Vtbl;
298 This->type = WICBitmapPaletteTypeCustom;
299 InitializeCriticalSection(&This->lock);
300 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PaletteImpl.lock");
302 *palette = (IWICPalette*)This;