windowscodecs: Add a stubbed ICNS encoder.
[wine] / dlls / windowscodecs / icnsformat.c
1 /*
2  * Copyright 2010 Damjan Jovanovic
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 #include "wine/port.h"
21
22 #include <stdarg.h>
23
24 #ifdef HAVE_ICNS_H
25 #include <icns.h>
26 #endif
27
28 #define COBJMACROS
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "objbase.h"
33 #include "wincodec.h"
34
35 #include "wincodecs_private.h"
36
37 #include "wine/debug.h"
38 #include "wine/library.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
41
42 #ifdef SONAME_LIBICNS
43
44 static void *libicns_handle;
45 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
46 MAKE_FUNCPTR(icns_create_family);
47 MAKE_FUNCPTR(icns_export_family_data);
48 MAKE_FUNCPTR(icns_free_image);
49 MAKE_FUNCPTR(icns_get_mask_type_for_icon_type);
50 MAKE_FUNCPTR(icns_get_type_from_image_info);
51 MAKE_FUNCPTR(icns_init_image_for_type);
52 MAKE_FUNCPTR(icns_new_element_from_image);
53 MAKE_FUNCPTR(icns_set_element_in_family);
54 #undef MAKE_FUNCPTR
55
56 static void *load_libicns(void)
57 {
58     if((libicns_handle = wine_dlopen(SONAME_LIBICNS, RTLD_NOW, NULL, 0)) != NULL) {
59
60 #define LOAD_FUNCPTR(f) \
61     if((p##f = wine_dlsym(libicns_handle, #f, NULL, 0)) == NULL) { \
62         libicns_handle = NULL; \
63         return NULL; \
64     }
65         LOAD_FUNCPTR(icns_create_family);
66         LOAD_FUNCPTR(icns_export_family_data);
67         LOAD_FUNCPTR(icns_free_image);
68         LOAD_FUNCPTR(icns_get_mask_type_for_icon_type);
69         LOAD_FUNCPTR(icns_get_type_from_image_info);
70         LOAD_FUNCPTR(icns_init_image_for_type);
71         LOAD_FUNCPTR(icns_new_element_from_image);
72         LOAD_FUNCPTR(icns_set_element_in_family);
73 #undef LOAD_FUNCPTR
74     }
75     return libicns_handle;
76 }
77
78 typedef struct IcnsEncoder {
79     const IWICBitmapEncoderVtbl *lpVtbl;
80     LONG ref;
81     IStream *stream;
82     CRITICAL_SECTION lock;
83 } IcnsEncoder;
84
85 static HRESULT WINAPI IcnsEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
86     void **ppv)
87 {
88     IcnsEncoder *This = (IcnsEncoder*)iface;
89     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
90
91     if (!ppv) return E_INVALIDARG;
92
93     if (IsEqualIID(&IID_IUnknown, iid) ||
94         IsEqualIID(&IID_IWICBitmapEncoder, iid))
95     {
96         *ppv = This;
97     }
98     else
99     {
100         *ppv = NULL;
101         return E_NOINTERFACE;
102     }
103
104     IUnknown_AddRef((IUnknown*)*ppv);
105     return S_OK;
106 }
107
108 static ULONG WINAPI IcnsEncoder_AddRef(IWICBitmapEncoder *iface)
109 {
110     IcnsEncoder *This = (IcnsEncoder*)iface;
111     ULONG ref = InterlockedIncrement(&This->ref);
112
113     TRACE("(%p) refcount=%u\n", iface, ref);
114
115     return ref;
116 }
117
118 static ULONG WINAPI IcnsEncoder_Release(IWICBitmapEncoder *iface)
119 {
120     IcnsEncoder *This = (IcnsEncoder*)iface;
121     ULONG ref = InterlockedDecrement(&This->ref);
122
123     TRACE("(%p) refcount=%u\n", iface, ref);
124
125     if (ref == 0)
126     {
127         This->lock.DebugInfo->Spare[0] = 0;
128         DeleteCriticalSection(&This->lock);
129         if (This->stream)
130             IStream_Release(This->stream);
131         HeapFree(GetProcessHeap(), 0, This);
132     }
133
134     return ref;
135 }
136
137 static HRESULT WINAPI IcnsEncoder_Initialize(IWICBitmapEncoder *iface,
138     IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
139 {
140     IcnsEncoder *This = (IcnsEncoder*)iface;
141
142     TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
143
144     EnterCriticalSection(&This->lock);
145
146     IStream_AddRef(pIStream);
147     This->stream = pIStream;
148
149     LeaveCriticalSection(&This->lock);
150
151     return S_OK;
152 }
153
154 static HRESULT WINAPI IcnsEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
155     GUID *pguidContainerFormat)
156 {
157     FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
158     return E_NOTIMPL;
159 }
160
161 static HRESULT WINAPI IcnsEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
162     IWICBitmapEncoderInfo **ppIEncoderInfo)
163 {
164     FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
165     return E_NOTIMPL;
166 }
167
168 static HRESULT WINAPI IcnsEncoder_SetColorContexts(IWICBitmapEncoder *iface,
169     UINT cCount, IWICColorContext **ppIColorContext)
170 {
171     FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
172     return E_NOTIMPL;
173 }
174
175 static HRESULT WINAPI IcnsEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
176 {
177     TRACE("(%p,%p)\n", iface, pIPalette);
178     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
179 }
180
181 static HRESULT WINAPI IcnsEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
182 {
183     TRACE("(%p,%p)\n", iface, pIThumbnail);
184     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
185 }
186
187 static HRESULT WINAPI IcnsEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
188 {
189     TRACE("(%p,%p)\n", iface, pIPreview);
190     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
191 }
192
193 static HRESULT WINAPI IcnsEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
194     IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
195 {
196     FIXME("(%p,%p,%p): stub\n", iface, ppIFrameEncode, ppIEncoderOptions);
197
198     return E_NOTIMPL;
199 }
200
201 static HRESULT WINAPI IcnsEncoder_Commit(IWICBitmapEncoder *iface)
202 {
203     FIXME("(%p): stub\n", iface);
204
205     return E_NOTIMPL;
206 }
207
208 static HRESULT WINAPI IcnsEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
209     IWICMetadataQueryWriter **ppIMetadataQueryWriter)
210 {
211     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
212     return E_NOTIMPL;
213 }
214
215 static const IWICBitmapEncoderVtbl IcnsEncoder_Vtbl = {
216     IcnsEncoder_QueryInterface,
217     IcnsEncoder_AddRef,
218     IcnsEncoder_Release,
219     IcnsEncoder_Initialize,
220     IcnsEncoder_GetContainerFormat,
221     IcnsEncoder_GetEncoderInfo,
222     IcnsEncoder_SetColorContexts,
223     IcnsEncoder_SetPalette,
224     IcnsEncoder_SetThumbnail,
225     IcnsEncoder_SetPreview,
226     IcnsEncoder_CreateNewFrame,
227     IcnsEncoder_Commit,
228     IcnsEncoder_GetMetadataQueryWriter
229 };
230
231 HRESULT IcnsEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
232 {
233     IcnsEncoder *This;
234     HRESULT ret;
235
236     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
237
238     *ppv = NULL;
239
240     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
241
242     if (!libicns_handle && !load_libicns())
243     {
244         ERR("Failed writing ICNS because unable to find %s\n",SONAME_LIBICNS);
245         return E_FAIL;
246     }
247
248     This = HeapAlloc(GetProcessHeap(), 0, sizeof(IcnsEncoder));
249     if (!This) return E_OUTOFMEMORY;
250
251     This->lpVtbl = &IcnsEncoder_Vtbl;
252     This->ref = 1;
253     This->stream = NULL;
254     InitializeCriticalSection(&This->lock);
255     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IcnsEncoder.lock");
256
257     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
258     IUnknown_Release((IUnknown*)This);
259
260     return ret;
261 }
262
263 #else /* !HAVE_ICNS_H */
264
265 HRESULT IcnsEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
266 {
267     ERR("Trying to save ICNS picture, but ICNS support is not compiled in.\n");
268     return E_FAIL;
269 }
270
271 #endif