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