- Sync rsaenh, schannel, shlwapi, sti, urlmon, usp10, version, windowscodecs, wininet...
[reactos.git] / reactos / dll / win32 / windowscodecs / palette.c
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "objbase.h"
29 #include "wincodec.h"
30
31 #include "wincodecs_private.h"
32
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
36
37 typedef struct {
38 const IWICPaletteVtbl *lpIWICPaletteVtbl;
39 LONG ref;
40 UINT count;
41 WICColor *colors;
42 WICBitmapPaletteType type;
43 CRITICAL_SECTION lock; /* must be held when count, colors, or type is accessed */
44 } PaletteImpl;
45
46 static HRESULT WINAPI PaletteImpl_QueryInterface(IWICPalette *iface, REFIID iid,
47 void **ppv)
48 {
49 PaletteImpl *This = (PaletteImpl*)iface;
50 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
51
52 if (!ppv) return E_INVALIDARG;
53
54 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICPalette, iid))
55 {
56 *ppv = This;
57 }
58 else
59 {
60 *ppv = NULL;
61 return E_NOINTERFACE;
62 }
63
64 IUnknown_AddRef((IUnknown*)*ppv);
65 return S_OK;
66 }
67
68 static ULONG WINAPI PaletteImpl_AddRef(IWICPalette *iface)
69 {
70 PaletteImpl *This = (PaletteImpl*)iface;
71 ULONG ref = InterlockedIncrement(&This->ref);
72
73 TRACE("(%p) refcount=%u\n", iface, ref);
74
75 return ref;
76 }
77
78 static ULONG WINAPI PaletteImpl_Release(IWICPalette *iface)
79 {
80 PaletteImpl *This = (PaletteImpl*)iface;
81 ULONG ref = InterlockedDecrement(&This->ref);
82
83 TRACE("(%p) refcount=%u\n", iface, ref);
84
85 if (ref == 0)
86 {
87 This->lock.DebugInfo->Spare[0] = 0;
88 DeleteCriticalSection(&This->lock);
89 HeapFree(GetProcessHeap(), 0, This->colors);
90 HeapFree(GetProcessHeap(), 0, This);
91 }
92
93 return ref;
94 }
95
96 static HRESULT WINAPI PaletteImpl_InitializePredefined(IWICPalette *iface,
97 WICBitmapPaletteType ePaletteType, BOOL fAddTransparentColor)
98 {
99 FIXME("(%p,%u,%i): stub\n", iface, ePaletteType, fAddTransparentColor);
100 return E_NOTIMPL;
101 }
102
103 static HRESULT WINAPI PaletteImpl_InitializeCustom(IWICPalette *iface,
104 WICColor *pColors, UINT colorCount)
105 {
106 PaletteImpl *This = (PaletteImpl*)iface;
107 WICColor *new_colors;
108
109 TRACE("(%p,%p,%u)\n", iface, pColors, colorCount);
110
111 if (colorCount == 0)
112 {
113 new_colors = NULL;
114 }
115 else
116 {
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);
121 }
122
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);
129
130 return S_OK;
131 }
132
133 static HRESULT WINAPI PaletteImpl_InitializeFromBitmap(IWICPalette *iface,
134 IWICBitmapSource *pISurface, UINT colorCount, BOOL fAddTransparentColor)
135 {
136 FIXME("(%p,%p,%u,%i): stub\n", iface, pISurface, colorCount, fAddTransparentColor);
137 return E_NOTIMPL;
138 }
139
140 static HRESULT WINAPI PaletteImpl_InitializeFromPalette(IWICPalette *iface,
141 IWICPalette *pIPalette)
142 {
143 FIXME("(%p,%p): stub\n", iface, pIPalette);
144 return E_NOTIMPL;
145 }
146
147 static HRESULT WINAPI PaletteImpl_GetType(IWICPalette *iface,
148 WICBitmapPaletteType *pePaletteType)
149 {
150 PaletteImpl *This = (PaletteImpl*)iface;
151
152 TRACE("(%p,%p)\n", iface, pePaletteType);
153
154 if (!pePaletteType) return E_INVALIDARG;
155
156 EnterCriticalSection(&This->lock);
157 *pePaletteType = This->type;
158 LeaveCriticalSection(&This->lock);
159
160 return S_OK;
161 }
162
163 static HRESULT WINAPI PaletteImpl_GetColorCount(IWICPalette *iface, UINT *pcCount)
164 {
165 PaletteImpl *This = (PaletteImpl*)iface;
166
167 TRACE("(%p,%p)\n", iface, pcCount);
168
169 if (!pcCount) return E_INVALIDARG;
170
171 EnterCriticalSection(&This->lock);
172 *pcCount = This->count;
173 LeaveCriticalSection(&This->lock);
174
175 return S_OK;
176 }
177
178 static HRESULT WINAPI PaletteImpl_GetColors(IWICPalette *iface, UINT colorCount,
179 WICColor *pColors, UINT *pcActualColors)
180 {
181 PaletteImpl *This = (PaletteImpl*)iface;
182
183 TRACE("(%p,%i,%p,%p)\n", iface, colorCount, pColors, pcActualColors);
184
185 if (!pColors || !pcActualColors) return E_INVALIDARG;
186
187 EnterCriticalSection(&This->lock);
188
189 if (This->count < colorCount) colorCount = This->count;
190
191 memcpy(pColors, This->colors, sizeof(WICColor) * colorCount);
192
193 *pcActualColors = colorCount;
194
195 LeaveCriticalSection(&This->lock);
196
197 return S_OK;
198 }
199
200 static HRESULT WINAPI PaletteImpl_IsBlackWhite(IWICPalette *iface, BOOL *pfIsBlackWhite)
201 {
202 PaletteImpl *This = (PaletteImpl*)iface;
203
204 TRACE("(%p,%p)\n", iface, pfIsBlackWhite);
205
206 if (!pfIsBlackWhite) return E_INVALIDARG;
207
208 EnterCriticalSection(&This->lock);
209 if (This->type == WICBitmapPaletteTypeFixedBW)
210 *pfIsBlackWhite = TRUE;
211 else
212 *pfIsBlackWhite = FALSE;
213 LeaveCriticalSection(&This->lock);
214
215 return S_OK;
216 }
217
218 static HRESULT WINAPI PaletteImpl_IsGrayscale(IWICPalette *iface, BOOL *pfIsGrayscale)
219 {
220 PaletteImpl *This = (PaletteImpl*)iface;
221
222 TRACE("(%p,%p)\n", iface, pfIsGrayscale);
223
224 if (!pfIsGrayscale) return E_INVALIDARG;
225
226 EnterCriticalSection(&This->lock);
227 switch(This->type)
228 {
229 case WICBitmapPaletteTypeFixedBW:
230 case WICBitmapPaletteTypeFixedGray4:
231 case WICBitmapPaletteTypeFixedGray16:
232 case WICBitmapPaletteTypeFixedGray256:
233 *pfIsGrayscale = TRUE;
234 break;
235 default:
236 *pfIsGrayscale = FALSE;
237 }
238 LeaveCriticalSection(&This->lock);
239
240 return S_OK;
241 }
242
243 static HRESULT WINAPI PaletteImpl_HasAlpha(IWICPalette *iface, BOOL *pfHasAlpha)
244 {
245 PaletteImpl *This = (PaletteImpl*)iface;
246 int i;
247
248 TRACE("(%p,%p)\n", iface, pfHasAlpha);
249
250 if (!pfHasAlpha) return E_INVALIDARG;
251
252 *pfHasAlpha = FALSE;
253
254 EnterCriticalSection(&This->lock);
255 for (i=0; i<This->count; i++)
256 if ((This->colors[i]&0xff000000) != 0xff000000)
257 {
258 *pfHasAlpha = TRUE;
259 break;
260 }
261 LeaveCriticalSection(&This->lock);
262
263 return S_OK;
264 }
265
266 static const IWICPaletteVtbl PaletteImpl_Vtbl = {
267 PaletteImpl_QueryInterface,
268 PaletteImpl_AddRef,
269 PaletteImpl_Release,
270 PaletteImpl_InitializePredefined,
271 PaletteImpl_InitializeCustom,
272 PaletteImpl_InitializeFromBitmap,
273 PaletteImpl_InitializeFromPalette,
274 PaletteImpl_GetType,
275 PaletteImpl_GetColorCount,
276 PaletteImpl_GetColors,
277 PaletteImpl_IsBlackWhite,
278 PaletteImpl_IsGrayscale,
279 PaletteImpl_HasAlpha
280 };
281
282 HRESULT PaletteImpl_Create(IWICPalette **palette)
283 {
284 PaletteImpl *This;
285
286 This = HeapAlloc(GetProcessHeap(), 0, sizeof(PaletteImpl));
287 if (!This) return E_OUTOFMEMORY;
288
289 This->lpIWICPaletteVtbl = &PaletteImpl_Vtbl;
290 This->ref = 1;
291 This->count = 0;
292 This->colors = NULL;
293 This->type = WICBitmapPaletteTypeCustom;
294 InitializeCriticalSection(&This->lock);
295 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PaletteImpl.lock");
296
297 *palette = (IWICPalette*)This;
298
299 return S_OK;
300 }