secur32: Recognize the ARC4 cipher in schannel_get_cipher_algid().
[wine] / dlls / 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 } PaletteImpl;
44
45 static HRESULT WINAPI PaletteImpl_QueryInterface(IWICPalette *iface, REFIID iid,
46     void **ppv)
47 {
48     PaletteImpl *This = (PaletteImpl*)iface;
49     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
50
51     if (!ppv) return E_INVALIDARG;
52
53     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICPalette, iid))
54     {
55         *ppv = This;
56     }
57     else
58     {
59         *ppv = NULL;
60         return E_NOINTERFACE;
61     }
62
63     IUnknown_AddRef((IUnknown*)*ppv);
64     return S_OK;
65 }
66
67 static ULONG WINAPI PaletteImpl_AddRef(IWICPalette *iface)
68 {
69     PaletteImpl *This = (PaletteImpl*)iface;
70     ULONG ref = InterlockedIncrement(&This->ref);
71
72     TRACE("(%p) refcount=%u\n", iface, ref);
73
74     return ref;
75 }
76
77 static ULONG WINAPI PaletteImpl_Release(IWICPalette *iface)
78 {
79     PaletteImpl *This = (PaletteImpl*)iface;
80     ULONG ref = InterlockedDecrement(&This->ref);
81
82     TRACE("(%p) refcount=%u\n", iface, ref);
83
84     if (ref == 0)
85     {
86         HeapFree(GetProcessHeap(), 0, This->colors);
87         HeapFree(GetProcessHeap(), 0, This);
88     }
89
90     return ref;
91 }
92
93 static HRESULT WINAPI PaletteImpl_InitializePredefined(IWICPalette *iface,
94     WICBitmapPaletteType ePaletteType, BOOL fAddTransparentColor)
95 {
96     FIXME("(%p,%u,%i): stub\n", iface, ePaletteType, fAddTransparentColor);
97     return E_NOTIMPL;
98 }
99
100 static HRESULT WINAPI PaletteImpl_InitializeCustom(IWICPalette *iface,
101     WICColor *pColors, UINT colorCount)
102 {
103     PaletteImpl *This = (PaletteImpl*)iface;
104     WICColor *new_colors;
105
106     TRACE("(%p,%p,%u)\n", iface, pColors, colorCount);
107
108     if (colorCount == 0)
109     {
110         new_colors = NULL;
111     }
112     else
113     {
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);
118     }
119
120     HeapFree(GetProcessHeap(), 0, This->colors);
121     This->colors = new_colors;
122     This->count = colorCount;
123     This->type = WICBitmapPaletteTypeCustom;
124
125     return S_OK;
126 }
127
128 static HRESULT WINAPI PaletteImpl_InitializeFromBitmap(IWICPalette *iface,
129     IWICBitmapSource *pISurface, UINT colorCount, BOOL fAddTransparentColor)
130 {
131     FIXME("(%p,%p,%u,%i): stub\n", iface, pISurface, colorCount, fAddTransparentColor);
132     return E_NOTIMPL;
133 }
134
135 static HRESULT WINAPI PaletteImpl_InitializeFromPalette(IWICPalette *iface,
136     IWICPalette *pIPalette)
137 {
138     FIXME("(%p,%p): stub\n", iface, pIPalette);
139     return E_NOTIMPL;
140 }
141
142 static HRESULT WINAPI PaletteImpl_GetType(IWICPalette *iface,
143     WICBitmapPaletteType *pePaletteType)
144 {
145     PaletteImpl *This = (PaletteImpl*)iface;
146
147     TRACE("(%p,%p)\n", iface, pePaletteType);
148
149     if (!pePaletteType) return E_INVALIDARG;
150
151     *pePaletteType = This->type;
152
153     return S_OK;
154 }
155
156 static HRESULT WINAPI PaletteImpl_GetColorCount(IWICPalette *iface, UINT *pcCount)
157 {
158     PaletteImpl *This = (PaletteImpl*)iface;
159
160     TRACE("(%p,%p)\n", iface, pcCount);
161
162     if (!pcCount) return E_INVALIDARG;
163
164     *pcCount = This->count;
165
166     return S_OK;
167 }
168
169 static HRESULT WINAPI PaletteImpl_GetColors(IWICPalette *iface, UINT colorCount,
170     WICColor *pColors, UINT *pcActualColors)
171 {
172     PaletteImpl *This = (PaletteImpl*)iface;
173
174     TRACE("(%p,%i,%p,%p)\n", iface, colorCount, pColors, pcActualColors);
175
176     if (!pColors || !pcActualColors) return E_INVALIDARG;
177
178     if (This->count < colorCount) colorCount = This->count;
179
180     memcpy(pColors, This->colors, sizeof(WICColor) * colorCount);
181
182     *pcActualColors = colorCount;
183
184     return S_OK;
185 }
186
187 static HRESULT WINAPI PaletteImpl_IsBlackWhite(IWICPalette *iface, BOOL *pfIsBlackWhite)
188 {
189     PaletteImpl *This = (PaletteImpl*)iface;
190
191     TRACE("(%p,%p)\n", iface, pfIsBlackWhite);
192
193     if (!pfIsBlackWhite) return E_INVALIDARG;
194
195     if (This->type == WICBitmapPaletteTypeFixedBW)
196         *pfIsBlackWhite = TRUE;
197     else
198         *pfIsBlackWhite = FALSE;
199
200     return S_OK;
201 }
202
203 static HRESULT WINAPI PaletteImpl_IsGrayscale(IWICPalette *iface, BOOL *pfIsGrayscale)
204 {
205     PaletteImpl *This = (PaletteImpl*)iface;
206
207     TRACE("(%p,%p)\n", iface, pfIsGrayscale);
208
209     if (!pfIsGrayscale) return E_INVALIDARG;
210
211     switch(This->type)
212     {
213         case WICBitmapPaletteTypeFixedBW:
214         case WICBitmapPaletteTypeFixedGray4:
215         case WICBitmapPaletteTypeFixedGray16:
216         case WICBitmapPaletteTypeFixedGray256:
217             *pfIsGrayscale = TRUE;
218             break;
219         default:
220             *pfIsGrayscale = FALSE;
221     }
222
223     return S_OK;
224 }
225
226 static HRESULT WINAPI PaletteImpl_HasAlpha(IWICPalette *iface, BOOL *pfHasAlpha)
227 {
228     PaletteImpl *This = (PaletteImpl*)iface;
229     int i;
230
231     TRACE("(%p,%p)\n", iface, pfHasAlpha);
232
233     if (!pfHasAlpha) return E_INVALIDARG;
234
235     *pfHasAlpha = FALSE;
236
237     for (i=0; i<This->count; i++)
238         if ((This->colors[i]&0xff000000) != 0xff000000)
239         {
240             *pfHasAlpha = TRUE;
241             break;
242         }
243
244     return S_OK;
245 }
246
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
261 };
262
263 HRESULT PaletteImpl_Create(IWICPalette **palette)
264 {
265     PaletteImpl *This;
266
267     This = HeapAlloc(GetProcessHeap(), 0, sizeof(PaletteImpl));
268     if (!This) return E_OUTOFMEMORY;
269
270     This->lpIWICPaletteVtbl = &PaletteImpl_Vtbl;
271     This->ref = 1;
272     This->count = 0;
273     This->colors = NULL;
274     This->type = WICBitmapPaletteTypeCustom;
275
276     *palette = (IWICPalette*)This;
277
278     return S_OK;
279 }