windowscodecs: Implement Initialize for BMP encoder.
[wine] / dlls / windowscodecs / imgfactory.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 IWICImagingFactoryVtbl    *lpIWICImagingFactoryVtbl;
39     LONG ref;
40 } ImagingFactory;
41
42 static HRESULT WINAPI ImagingFactory_QueryInterface(IWICImagingFactory *iface, REFIID iid,
43     void **ppv)
44 {
45     ImagingFactory *This = (ImagingFactory*)iface;
46     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
47
48     if (!ppv) return E_INVALIDARG;
49
50     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICImagingFactory, iid))
51     {
52         *ppv = This;
53     }
54     else
55     {
56         *ppv = NULL;
57         return E_NOINTERFACE;
58     }
59
60     IUnknown_AddRef((IUnknown*)*ppv);
61     return S_OK;
62 }
63
64 static ULONG WINAPI ImagingFactory_AddRef(IWICImagingFactory *iface)
65 {
66     ImagingFactory *This = (ImagingFactory*)iface;
67     ULONG ref = InterlockedIncrement(&This->ref);
68
69     TRACE("(%p) refcount=%u\n", iface, ref);
70
71     return ref;
72 }
73
74 static ULONG WINAPI ImagingFactory_Release(IWICImagingFactory *iface)
75 {
76     ImagingFactory *This = (ImagingFactory*)iface;
77     ULONG ref = InterlockedDecrement(&This->ref);
78
79     TRACE("(%p) refcount=%u\n", iface, ref);
80
81     if (ref == 0)
82         HeapFree(GetProcessHeap(), 0, This);
83
84     return ref;
85 }
86
87 static HRESULT WINAPI ImagingFactory_CreateDecoderFromFilename(
88     IWICImagingFactory *iface, LPCWSTR wzFilename, const GUID *pguidVendor,
89     DWORD dwDesiredAccess, WICDecodeOptions metadataOptions,
90     IWICBitmapDecoder **ppIDecoder)
91 {
92     FIXME("(%p,%s,%s,%u,%u,%p): stub\n", iface, debugstr_w(wzFilename),
93         debugstr_guid(pguidVendor), dwDesiredAccess, metadataOptions, ppIDecoder);
94     return E_NOTIMPL;
95 }
96
97 static HRESULT WINAPI ImagingFactory_CreateDecoderFromStream(
98     IWICImagingFactory *iface, IStream *pIStream, const GUID *pguidVendor,
99     WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
100 {
101     static int fixme=0;
102     IEnumUnknown *enumdecoders;
103     IUnknown *unkdecoderinfo;
104     IWICBitmapDecoderInfo *decoderinfo;
105     IWICBitmapDecoder *decoder=NULL;
106     HRESULT res=S_OK;
107     ULONG num_fetched;
108     BOOL matches;
109
110     TRACE("(%p,%p,%s,%u,%p)\n", iface, pIStream, debugstr_guid(pguidVendor),
111         metadataOptions, ppIDecoder);
112
113     if (pguidVendor && !fixme++)
114         FIXME("ignoring vendor GUID\n");
115
116     res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
117     if (FAILED(res)) return res;
118
119     while (!decoder)
120     {
121         res = IEnumUnknown_Next(enumdecoders, 1, &unkdecoderinfo, &num_fetched);
122
123         if (res == S_OK)
124         {
125             res = IUnknown_QueryInterface(unkdecoderinfo, &IID_IWICBitmapDecoderInfo, (void**)&decoderinfo);
126
127             if (SUCCEEDED(res))
128             {
129                 res = IWICBitmapDecoderInfo_MatchesPattern(decoderinfo, pIStream, &matches);
130
131                 if (SUCCEEDED(res) && matches)
132                 {
133                     res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, &decoder);
134
135                     /* FIXME: should use QueryCapability to choose a decoder */
136
137                     if (SUCCEEDED(res))
138                     {
139                         res = IWICBitmapDecoder_Initialize(decoder, pIStream, metadataOptions);
140
141                         if (FAILED(res))
142                         {
143                             IWICBitmapDecoder_Release(decoder);
144                             decoder = NULL;
145                         }
146                     }
147                 }
148
149                 IWICBitmapDecoderInfo_Release(decoderinfo);
150             }
151
152             IUnknown_Release(unkdecoderinfo);
153         }
154         else
155             break;
156     }
157
158     IEnumUnknown_Release(enumdecoders);
159
160     if (decoder)
161     {
162         *ppIDecoder = decoder;
163         return S_OK;
164     }
165     else
166     {
167         *ppIDecoder = NULL;
168         return WINCODEC_ERR_COMPONENTNOTFOUND;
169     }
170 }
171
172 static HRESULT WINAPI ImagingFactory_CreateDecoderFromFileHandle(
173     IWICImagingFactory *iface, ULONG_PTR hFile, const GUID *pguidVendor,
174     WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
175 {
176     FIXME("(%p,%lx,%s,%u,%p): stub\n", iface, hFile, debugstr_guid(pguidVendor),
177         metadataOptions, ppIDecoder);
178     return E_NOTIMPL;
179 }
180
181 static HRESULT WINAPI ImagingFactory_CreateComponentInfo(IWICImagingFactory *iface,
182     REFCLSID clsidComponent, IWICComponentInfo **ppIInfo)
183 {
184     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(clsidComponent), ppIInfo);
185     return CreateComponentInfo(clsidComponent, ppIInfo);
186 }
187
188 static HRESULT WINAPI ImagingFactory_CreateDecoder(IWICImagingFactory *iface,
189     REFGUID guidContainerFormat, const GUID *pguidVendor,
190     IWICBitmapDecoder **ppIDecoder)
191 {
192     FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidContainerFormat),
193         debugstr_guid(pguidVendor), ppIDecoder);
194     return E_NOTIMPL;
195 }
196
197 static HRESULT WINAPI ImagingFactory_CreateEncoder(IWICImagingFactory *iface,
198     REFGUID guidContainerFormat, const GUID *pguidVendor,
199     IWICBitmapEncoder **ppIEncoder)
200 {
201     FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidContainerFormat),
202         debugstr_guid(pguidVendor), ppIEncoder);
203     return E_NOTIMPL;
204 }
205
206 static HRESULT WINAPI ImagingFactory_CreatePalette(IWICImagingFactory *iface,
207     IWICPalette **ppIPalette)
208 {
209     TRACE("(%p,%p)\n", iface, ppIPalette);
210     return PaletteImpl_Create(ppIPalette);
211 }
212
213 static HRESULT WINAPI ImagingFactory_CreateFormatConverter(IWICImagingFactory *iface,
214     IWICFormatConverter **ppIFormatConverter)
215 {
216     FIXME("(%p,%p): stub\n", iface, ppIFormatConverter);
217     return E_NOTIMPL;
218 }
219
220 static HRESULT WINAPI ImagingFactory_CreateBitmapScaler(IWICImagingFactory *iface,
221     IWICBitmapScaler **ppIBitmapScaler)
222 {
223     FIXME("(%p,%p): stub\n", iface, ppIBitmapScaler);
224     return E_NOTIMPL;
225 }
226
227 static HRESULT WINAPI ImagingFactory_CreateBitmapClipper(IWICImagingFactory *iface,
228     IWICBitmapClipper **ppIBitmapClipper)
229 {
230     FIXME("(%p,%p): stub\n", iface, ppIBitmapClipper);
231     return E_NOTIMPL;
232 }
233
234 static HRESULT WINAPI ImagingFactory_CreateBitmapFlipRotator(IWICImagingFactory *iface,
235     IWICBitmapFlipRotator **ppIBitmapFlipRotator)
236 {
237     FIXME("(%p,%p): stub\n", iface, ppIBitmapFlipRotator);
238     return E_NOTIMPL;
239 }
240
241 static HRESULT WINAPI ImagingFactory_CreateStream(IWICImagingFactory *iface,
242     IWICStream **ppIWICStream)
243 {
244     FIXME("(%p,%p): stub\n", iface, ppIWICStream);
245     return E_NOTIMPL;
246 }
247
248 static HRESULT WINAPI ImagingFactory_CreateColorContext(IWICImagingFactory *iface,
249     IWICColorContext **ppIColorContext)
250 {
251     FIXME("(%p,%p): stub\n", iface, ppIColorContext);
252     return E_NOTIMPL;
253 }
254
255 static HRESULT WINAPI ImagingFactory_CreateColorTransformer(IWICImagingFactory *iface,
256     IWICColorTransform **ppIColorTransform)
257 {
258     FIXME("(%p,%p): stub\n", iface, ppIColorTransform);
259     return E_NOTIMPL;
260 }
261
262 static HRESULT WINAPI ImagingFactory_CreateBitmap(IWICImagingFactory *iface,
263     UINT uiWidth, UINT uiHeight, REFWICPixelFormatGUID pixelFormat,
264     WICBitmapCreateCacheOption option, IWICBitmap **ppIBitmap)
265 {
266     FIXME("(%p,%u,%u,%s,%u,%p): stub\n", iface, uiWidth, uiHeight,
267         debugstr_guid(pixelFormat), option, ppIBitmap);
268     return E_NOTIMPL;
269 }
270
271 static HRESULT WINAPI ImagingFactory_CreateBitmapFromSource(IWICImagingFactory *iface,
272     IWICBitmapSource *piBitmapSource, WICBitmapCreateCacheOption option,
273     IWICBitmap **ppIBitmap)
274 {
275     FIXME("(%p,%p,%u,%p): stub\n", iface, piBitmapSource, option, ppIBitmap);
276     return E_NOTIMPL;
277 }
278
279 static HRESULT WINAPI ImagingFactory_CreateBitmapFromSourceRect(IWICImagingFactory *iface,
280     IWICBitmapSource *piBitmapSource, UINT x, UINT y, UINT width, UINT height,
281     IWICBitmap **ppIBitmap)
282 {
283     FIXME("(%p,%p,%u,%u,%u,%u,%p): stub\n", iface, piBitmapSource, x, y, width,
284         height, ppIBitmap);
285     return E_NOTIMPL;
286 }
287
288 static HRESULT WINAPI ImagingFactory_CreateBitmapFromMemory(IWICImagingFactory *iface,
289     UINT uiWidth, UINT uiHeight, REFWICPixelFormatGUID pixelFormat, UINT cbStride,
290     UINT cbBufferSize, BYTE *pbBuffer, IWICBitmap **ppIBitmap)
291 {
292     FIXME("(%p,%u,%u,%s,%u,%u,%p,%p): stub\n", iface, uiWidth, uiHeight,
293         debugstr_guid(pixelFormat), cbStride, cbBufferSize, pbBuffer, ppIBitmap);
294     return E_NOTIMPL;
295 }
296
297 static HRESULT WINAPI ImagingFactory_CreateBitmapFromHBITMAP(IWICImagingFactory *iface,
298     HBITMAP hBitmap, HPALETTE hPalette, WICBitmapAlphaChannelOption options,
299     IWICBitmap **ppIBitmap)
300 {
301     FIXME("(%p,%p,%p,%u,%p): stub\n", iface, hBitmap, hPalette, options, ppIBitmap);
302     return E_NOTIMPL;
303 }
304
305 static HRESULT WINAPI ImagingFactory_CreateBitmapFromHICON(IWICImagingFactory *iface,
306     HICON hIcon, IWICBitmap **ppIBitmap)
307 {
308     FIXME("(%p,%p,%p): stub\n", iface, hIcon, ppIBitmap);
309     return E_NOTIMPL;
310 }
311
312 static HRESULT WINAPI ImagingFactory_CreateComponentEnumerator(IWICImagingFactory *iface,
313     DWORD componentTypes, DWORD options, IEnumUnknown **ppIEnumUnknown)
314 {
315     TRACE("(%p,%u,%u,%p)\n", iface, componentTypes, options, ppIEnumUnknown);
316     return CreateComponentEnumerator(componentTypes, options, ppIEnumUnknown);
317 }
318
319 static HRESULT WINAPI ImagingFactory_CreateFastMetadataEncoderFromDecoder(
320     IWICImagingFactory *iface, IWICBitmapDecoder *pIDecoder,
321     IWICFastMetadataEncoder **ppIFastEncoder)
322 {
323     FIXME("(%p,%p,%p): stub\n", iface, pIDecoder, ppIFastEncoder);
324     return E_NOTIMPL;
325 }
326
327 static HRESULT WINAPI ImagingFactory_CreateFastMetadataEncoderFromFrameDecode(
328     IWICImagingFactory *iface, IWICBitmapFrameDecode *pIFrameDecoder,
329     IWICFastMetadataEncoder **ppIFastEncoder)
330 {
331     FIXME("(%p,%p,%p): stub\n", iface, pIFrameDecoder, ppIFastEncoder);
332     return E_NOTIMPL;
333 }
334
335 static HRESULT WINAPI ImagingFactory_CreateQueryWriter(IWICImagingFactory *iface,
336     REFGUID guidMetadataFormat, const GUID *pguidVendor,
337     IWICMetadataQueryWriter **ppIQueryWriter)
338 {
339     FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidMetadataFormat),
340         debugstr_guid(pguidVendor), ppIQueryWriter);
341     return E_NOTIMPL;
342 }
343
344 static HRESULT WINAPI ImagingFactory_CreateQueryWriterFromReader(IWICImagingFactory *iface,
345     IWICMetadataQueryReader *pIQueryReader, const GUID *pguidVendor,
346     IWICMetadataQueryWriter **ppIQueryWriter)
347 {
348     FIXME("(%p,%p,%s,%p): stub\n", iface, pIQueryReader, debugstr_guid(pguidVendor),
349         ppIQueryWriter);
350     return E_NOTIMPL;
351 }
352
353 static const IWICImagingFactoryVtbl ImagingFactory_Vtbl = {
354     ImagingFactory_QueryInterface,
355     ImagingFactory_AddRef,
356     ImagingFactory_Release,
357     ImagingFactory_CreateDecoderFromFilename,
358     ImagingFactory_CreateDecoderFromStream,
359     ImagingFactory_CreateDecoderFromFileHandle,
360     ImagingFactory_CreateComponentInfo,
361     ImagingFactory_CreateDecoder,
362     ImagingFactory_CreateEncoder,
363     ImagingFactory_CreatePalette,
364     ImagingFactory_CreateFormatConverter,
365     ImagingFactory_CreateBitmapScaler,
366     ImagingFactory_CreateBitmapClipper,
367     ImagingFactory_CreateBitmapFlipRotator,
368     ImagingFactory_CreateStream,
369     ImagingFactory_CreateColorContext,
370     ImagingFactory_CreateColorTransformer,
371     ImagingFactory_CreateBitmap,
372     ImagingFactory_CreateBitmapFromSource,
373     ImagingFactory_CreateBitmapFromSourceRect,
374     ImagingFactory_CreateBitmapFromMemory,
375     ImagingFactory_CreateBitmapFromHBITMAP,
376     ImagingFactory_CreateBitmapFromHICON,
377     ImagingFactory_CreateComponentEnumerator,
378     ImagingFactory_CreateFastMetadataEncoderFromDecoder,
379     ImagingFactory_CreateFastMetadataEncoderFromFrameDecode,
380     ImagingFactory_CreateQueryWriter,
381     ImagingFactory_CreateQueryWriterFromReader
382 };
383
384 HRESULT ImagingFactory_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
385 {
386     ImagingFactory *This;
387     HRESULT ret;
388
389     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
390
391     *ppv = NULL;
392
393     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
394
395     This = HeapAlloc(GetProcessHeap(), 0, sizeof(ImagingFactory));
396     if (!This) return E_OUTOFMEMORY;
397
398     This->lpIWICImagingFactoryVtbl = &ImagingFactory_Vtbl;
399     This->ref = 1;
400
401     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
402     IUnknown_Release((IUnknown*)This);
403
404     return ret;
405 }