push(f) 02fc09c39e59d4e65f728da07b0bc5b717866e58
[wine/hacks.git] / dlls / windowscodecs / palette.c
blob842d3b51c3064cdd4981943963af32638d831e6c
1 /*
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
19 #include "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "objbase.h"
29 #include "wincodec.h"
31 #include "wincodecs_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
37 typedef struct {
38 const IWICPaletteVtbl *lpIWICPaletteVtbl;
39 LONG ref;
40 UINT count;
41 WICColor *colors;
42 WICBitmapPaletteType type;
43 } PaletteImpl;
45 static HRESULT WINAPI PaletteImpl_QueryInterface(IWICPalette *iface, REFIID iid,
46 void **ppv)
48 PaletteImpl *This = (PaletteImpl*)iface;
49 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
51 if (!ppv) return E_INVALIDARG;
53 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICPalette, iid))
55 *ppv = This;
57 else
59 *ppv = NULL;
60 return E_NOINTERFACE;
63 IUnknown_AddRef((IUnknown*)*ppv);
64 return S_OK;
67 static ULONG WINAPI PaletteImpl_AddRef(IWICPalette *iface)
69 PaletteImpl *This = (PaletteImpl*)iface;
70 ULONG ref = InterlockedIncrement(&This->ref);
72 TRACE("(%p) refcount=%u\n", iface, ref);
74 return ref;
77 static ULONG WINAPI PaletteImpl_Release(IWICPalette *iface)
79 PaletteImpl *This = (PaletteImpl*)iface;
80 ULONG ref = InterlockedDecrement(&This->ref);
82 TRACE("(%p) refcount=%u\n", iface, ref);
84 if (ref == 0)
86 HeapFree(GetProcessHeap(), 0, This->colors);
87 HeapFree(GetProcessHeap(), 0, This);
90 return ref;
93 static HRESULT WINAPI PaletteImpl_InitializePredefined(IWICPalette *iface,
94 WICBitmapPaletteType ePaletteType, BOOL fAddTransparentColor)
96 FIXME("(%p,%u,%i): stub\n", iface, ePaletteType, fAddTransparentColor);
97 return E_NOTIMPL;
100 static HRESULT WINAPI PaletteImpl_InitializeCustom(IWICPalette *iface,
101 WICColor *pColors, UINT colorCount)
103 PaletteImpl *This = (PaletteImpl*)iface;
104 WICColor *new_colors;
106 TRACE("(%p,%p,%u)\n", iface, pColors, colorCount);
108 if (colorCount == 0)
110 new_colors = NULL;
112 else
114 if (!pColors) return E_INVALIDARG;
115 new_colors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * colorCount);
116 if (!new_colors) return E_OUTOFMEMORY;
117 memcpy(new_colors, pColors, sizeof(WICColor) * colorCount);
120 HeapFree(GetProcessHeap(), 0, This->colors);
121 This->colors = new_colors;
122 This->count = colorCount;
123 This->type = WICBitmapPaletteTypeCustom;
125 return S_OK;
128 static HRESULT WINAPI PaletteImpl_InitializeFromBitmap(IWICPalette *iface,
129 IWICBitmapSource *pISurface, UINT colorCount, BOOL fAddTransparentColor)
131 FIXME("(%p,%p,%u,%i): stub\n", iface, pISurface, colorCount, fAddTransparentColor);
132 return E_NOTIMPL;
135 static HRESULT WINAPI PaletteImpl_InitializeFromPalette(IWICPalette *iface,
136 IWICPalette *pIPalette)
138 FIXME("(%p,%p): stub\n", iface, pIPalette);
139 return E_NOTIMPL;
142 static HRESULT WINAPI PaletteImpl_GetType(IWICPalette *iface,
143 WICBitmapPaletteType *pePaletteType)
145 PaletteImpl *This = (PaletteImpl*)iface;
147 TRACE("(%p,%p)\n", iface, pePaletteType);
149 if (!pePaletteType) return E_INVALIDARG;
151 *pePaletteType = This->type;
153 return S_OK;
156 static HRESULT WINAPI PaletteImpl_GetColorCount(IWICPalette *iface, UINT *pcCount)
158 PaletteImpl *This = (PaletteImpl*)iface;
160 TRACE("(%p,%p)\n", iface, pcCount);
162 if (!pcCount) return E_INVALIDARG;
164 *pcCount = This->count;
166 return S_OK;
169 static HRESULT WINAPI PaletteImpl_GetColors(IWICPalette *iface, UINT colorCount,
170 WICColor *pColors, UINT *pcActualColors)
172 PaletteImpl *This = (PaletteImpl*)iface;
174 TRACE("(%p,%i,%p,%p)\n", iface, colorCount, pColors, pcActualColors);
176 if (!pColors || !pcActualColors) return E_INVALIDARG;
178 if (This->count < colorCount) colorCount = This->count;
180 memcpy(pColors, This->colors, sizeof(WICColor) * colorCount);
182 *pcActualColors = colorCount;
184 return S_OK;
187 static HRESULT WINAPI PaletteImpl_IsBlackWhite(IWICPalette *iface, BOOL *pfIsBlackWhite)
189 PaletteImpl *This = (PaletteImpl*)iface;
191 TRACE("(%p,%p)\n", iface, pfIsBlackWhite);
193 if (!pfIsBlackWhite) return E_INVALIDARG;
195 if (This->type == WICBitmapPaletteTypeFixedBW)
196 *pfIsBlackWhite = TRUE;
197 else
198 *pfIsBlackWhite = FALSE;
200 return S_OK;
203 static HRESULT WINAPI PaletteImpl_IsGrayscale(IWICPalette *iface, BOOL *pfIsGrayscale)
205 PaletteImpl *This = (PaletteImpl*)iface;
207 TRACE("(%p,%p)\n", iface, pfIsGrayscale);
209 if (!pfIsGrayscale) return E_INVALIDARG;
211 switch(This->type)
213 case WICBitmapPaletteTypeFixedBW:
214 case WICBitmapPaletteTypeFixedGray4:
215 case WICBitmapPaletteTypeFixedGray16:
216 case WICBitmapPaletteTypeFixedGray256:
217 *pfIsGrayscale = TRUE;
218 break;
219 default:
220 *pfIsGrayscale = FALSE;
223 return S_OK;
226 static HRESULT WINAPI PaletteImpl_HasAlpha(IWICPalette *iface, BOOL *pfHasAlpha)
228 PaletteImpl *This = (PaletteImpl*)iface;
229 int i;
231 TRACE("(%p,%p)\n", iface, pfHasAlpha);
233 if (!pfHasAlpha) return E_INVALIDARG;
235 *pfHasAlpha = FALSE;
237 for (i=0; i<This->count; i++)
238 if ((This->colors[i]&0xff000000) != 0xff000000)
240 *pfHasAlpha = TRUE;
241 break;
244 return S_OK;
247 static const IWICPaletteVtbl PaletteImpl_Vtbl = {
248 PaletteImpl_QueryInterface,
249 PaletteImpl_AddRef,
250 PaletteImpl_Release,
251 PaletteImpl_InitializePredefined,
252 PaletteImpl_InitializeCustom,
253 PaletteImpl_InitializeFromBitmap,
254 PaletteImpl_InitializeFromPalette,
255 PaletteImpl_GetType,
256 PaletteImpl_GetColorCount,
257 PaletteImpl_GetColors,
258 PaletteImpl_IsBlackWhite,
259 PaletteImpl_IsGrayscale,
260 PaletteImpl_HasAlpha
263 HRESULT PaletteImpl_Create(IWICPalette **palette)
265 PaletteImpl *This;
267 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PaletteImpl));
268 if (!This) return E_OUTOFMEMORY;
270 This->lpIWICPaletteVtbl = &PaletteImpl_Vtbl;
271 This->ref = 1;
272 This->count = 0;
273 This->colors = NULL;
274 This->type = WICBitmapPaletteTypeCustom;
276 *palette = (IWICPalette*)This;
278 return S_OK;