oleaut32: Improve TKIND_COCLASS handling while creating typelib.
[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         if (WARN_ON(wincodecs))
168         {
169             LARGE_INTEGER seek;
170             BYTE data[4];
171             ULONG bytesread;
172
173             WARN("failed to load from a stream\n");
174
175             seek.QuadPart = 0;
176             res = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
177             if (SUCCEEDED(res))
178                 res = IStream_Read(pIStream, data, 4, &bytesread);
179             if (SUCCEEDED(res))
180                 WARN("first %i bytes of stream=%x %x %x %x\n", bytesread, data[0], data[1], data[2], data[3]);
181         }
182         *ppIDecoder = NULL;
183         return WINCODEC_ERR_COMPONENTNOTFOUND;
184     }
185 }
186
187 static HRESULT WINAPI ImagingFactory_CreateDecoderFromFileHandle(
188     IWICImagingFactory *iface, ULONG_PTR hFile, const GUID *pguidVendor,
189     WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
190 {
191     FIXME("(%p,%lx,%s,%u,%p): stub\n", iface, hFile, debugstr_guid(pguidVendor),
192         metadataOptions, ppIDecoder);
193     return E_NOTIMPL;
194 }
195
196 static HRESULT WINAPI ImagingFactory_CreateComponentInfo(IWICImagingFactory *iface,
197     REFCLSID clsidComponent, IWICComponentInfo **ppIInfo)
198 {
199     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(clsidComponent), ppIInfo);
200     return CreateComponentInfo(clsidComponent, ppIInfo);
201 }
202
203 static HRESULT WINAPI ImagingFactory_CreateDecoder(IWICImagingFactory *iface,
204     REFGUID guidContainerFormat, const GUID *pguidVendor,
205     IWICBitmapDecoder **ppIDecoder)
206 {
207     FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidContainerFormat),
208         debugstr_guid(pguidVendor), ppIDecoder);
209     return E_NOTIMPL;
210 }
211
212 static HRESULT WINAPI ImagingFactory_CreateEncoder(IWICImagingFactory *iface,
213     REFGUID guidContainerFormat, const GUID *pguidVendor,
214     IWICBitmapEncoder **ppIEncoder)
215 {
216     FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidContainerFormat),
217         debugstr_guid(pguidVendor), ppIEncoder);
218     return E_NOTIMPL;
219 }
220
221 static HRESULT WINAPI ImagingFactory_CreatePalette(IWICImagingFactory *iface,
222     IWICPalette **ppIPalette)
223 {
224     TRACE("(%p,%p)\n", iface, ppIPalette);
225     return PaletteImpl_Create(ppIPalette);
226 }
227
228 static HRESULT WINAPI ImagingFactory_CreateFormatConverter(IWICImagingFactory *iface,
229     IWICFormatConverter **ppIFormatConverter)
230 {
231     FIXME("(%p,%p): stub\n", iface, ppIFormatConverter);
232     return E_NOTIMPL;
233 }
234
235 static HRESULT WINAPI ImagingFactory_CreateBitmapScaler(IWICImagingFactory *iface,
236     IWICBitmapScaler **ppIBitmapScaler)
237 {
238     FIXME("(%p,%p): stub\n", iface, ppIBitmapScaler);
239     return E_NOTIMPL;
240 }
241
242 static HRESULT WINAPI ImagingFactory_CreateBitmapClipper(IWICImagingFactory *iface,
243     IWICBitmapClipper **ppIBitmapClipper)
244 {
245     FIXME("(%p,%p): stub\n", iface, ppIBitmapClipper);
246     return E_NOTIMPL;
247 }
248
249 static HRESULT WINAPI ImagingFactory_CreateBitmapFlipRotator(IWICImagingFactory *iface,
250     IWICBitmapFlipRotator **ppIBitmapFlipRotator)
251 {
252     FIXME("(%p,%p): stub\n", iface, ppIBitmapFlipRotator);
253     return E_NOTIMPL;
254 }
255
256 static HRESULT WINAPI ImagingFactory_CreateStream(IWICImagingFactory *iface,
257     IWICStream **ppIWICStream)
258 {
259     TRACE("(%p,%p)\n", iface, ppIWICStream);
260     return StreamImpl_Create(ppIWICStream);
261 }
262
263 static HRESULT WINAPI ImagingFactory_CreateColorContext(IWICImagingFactory *iface,
264     IWICColorContext **ppIColorContext)
265 {
266     FIXME("(%p,%p): stub\n", iface, ppIColorContext);
267     return E_NOTIMPL;
268 }
269
270 static HRESULT WINAPI ImagingFactory_CreateColorTransformer(IWICImagingFactory *iface,
271     IWICColorTransform **ppIColorTransform)
272 {
273     FIXME("(%p,%p): stub\n", iface, ppIColorTransform);
274     return E_NOTIMPL;
275 }
276
277 static HRESULT WINAPI ImagingFactory_CreateBitmap(IWICImagingFactory *iface,
278     UINT uiWidth, UINT uiHeight, REFWICPixelFormatGUID pixelFormat,
279     WICBitmapCreateCacheOption option, IWICBitmap **ppIBitmap)
280 {
281     FIXME("(%p,%u,%u,%s,%u,%p): stub\n", iface, uiWidth, uiHeight,
282         debugstr_guid(pixelFormat), option, ppIBitmap);
283     return E_NOTIMPL;
284 }
285
286 static HRESULT WINAPI ImagingFactory_CreateBitmapFromSource(IWICImagingFactory *iface,
287     IWICBitmapSource *piBitmapSource, WICBitmapCreateCacheOption option,
288     IWICBitmap **ppIBitmap)
289 {
290     FIXME("(%p,%p,%u,%p): stub\n", iface, piBitmapSource, option, ppIBitmap);
291     return E_NOTIMPL;
292 }
293
294 static HRESULT WINAPI ImagingFactory_CreateBitmapFromSourceRect(IWICImagingFactory *iface,
295     IWICBitmapSource *piBitmapSource, UINT x, UINT y, UINT width, UINT height,
296     IWICBitmap **ppIBitmap)
297 {
298     FIXME("(%p,%p,%u,%u,%u,%u,%p): stub\n", iface, piBitmapSource, x, y, width,
299         height, ppIBitmap);
300     return E_NOTIMPL;
301 }
302
303 static HRESULT WINAPI ImagingFactory_CreateBitmapFromMemory(IWICImagingFactory *iface,
304     UINT uiWidth, UINT uiHeight, REFWICPixelFormatGUID pixelFormat, UINT cbStride,
305     UINT cbBufferSize, BYTE *pbBuffer, IWICBitmap **ppIBitmap)
306 {
307     FIXME("(%p,%u,%u,%s,%u,%u,%p,%p): stub\n", iface, uiWidth, uiHeight,
308         debugstr_guid(pixelFormat), cbStride, cbBufferSize, pbBuffer, ppIBitmap);
309     return E_NOTIMPL;
310 }
311
312 static HRESULT WINAPI ImagingFactory_CreateBitmapFromHBITMAP(IWICImagingFactory *iface,
313     HBITMAP hBitmap, HPALETTE hPalette, WICBitmapAlphaChannelOption options,
314     IWICBitmap **ppIBitmap)
315 {
316     FIXME("(%p,%p,%p,%u,%p): stub\n", iface, hBitmap, hPalette, options, ppIBitmap);
317     return E_NOTIMPL;
318 }
319
320 static HRESULT WINAPI ImagingFactory_CreateBitmapFromHICON(IWICImagingFactory *iface,
321     HICON hIcon, IWICBitmap **ppIBitmap)
322 {
323     FIXME("(%p,%p,%p): stub\n", iface, hIcon, ppIBitmap);
324     return E_NOTIMPL;
325 }
326
327 static HRESULT WINAPI ImagingFactory_CreateComponentEnumerator(IWICImagingFactory *iface,
328     DWORD componentTypes, DWORD options, IEnumUnknown **ppIEnumUnknown)
329 {
330     TRACE("(%p,%u,%u,%p)\n", iface, componentTypes, options, ppIEnumUnknown);
331     return CreateComponentEnumerator(componentTypes, options, ppIEnumUnknown);
332 }
333
334 static HRESULT WINAPI ImagingFactory_CreateFastMetadataEncoderFromDecoder(
335     IWICImagingFactory *iface, IWICBitmapDecoder *pIDecoder,
336     IWICFastMetadataEncoder **ppIFastEncoder)
337 {
338     FIXME("(%p,%p,%p): stub\n", iface, pIDecoder, ppIFastEncoder);
339     return E_NOTIMPL;
340 }
341
342 static HRESULT WINAPI ImagingFactory_CreateFastMetadataEncoderFromFrameDecode(
343     IWICImagingFactory *iface, IWICBitmapFrameDecode *pIFrameDecoder,
344     IWICFastMetadataEncoder **ppIFastEncoder)
345 {
346     FIXME("(%p,%p,%p): stub\n", iface, pIFrameDecoder, ppIFastEncoder);
347     return E_NOTIMPL;
348 }
349
350 static HRESULT WINAPI ImagingFactory_CreateQueryWriter(IWICImagingFactory *iface,
351     REFGUID guidMetadataFormat, const GUID *pguidVendor,
352     IWICMetadataQueryWriter **ppIQueryWriter)
353 {
354     FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidMetadataFormat),
355         debugstr_guid(pguidVendor), ppIQueryWriter);
356     return E_NOTIMPL;
357 }
358
359 static HRESULT WINAPI ImagingFactory_CreateQueryWriterFromReader(IWICImagingFactory *iface,
360     IWICMetadataQueryReader *pIQueryReader, const GUID *pguidVendor,
361     IWICMetadataQueryWriter **ppIQueryWriter)
362 {
363     FIXME("(%p,%p,%s,%p): stub\n", iface, pIQueryReader, debugstr_guid(pguidVendor),
364         ppIQueryWriter);
365     return E_NOTIMPL;
366 }
367
368 static const IWICImagingFactoryVtbl ImagingFactory_Vtbl = {
369     ImagingFactory_QueryInterface,
370     ImagingFactory_AddRef,
371     ImagingFactory_Release,
372     ImagingFactory_CreateDecoderFromFilename,
373     ImagingFactory_CreateDecoderFromStream,
374     ImagingFactory_CreateDecoderFromFileHandle,
375     ImagingFactory_CreateComponentInfo,
376     ImagingFactory_CreateDecoder,
377     ImagingFactory_CreateEncoder,
378     ImagingFactory_CreatePalette,
379     ImagingFactory_CreateFormatConverter,
380     ImagingFactory_CreateBitmapScaler,
381     ImagingFactory_CreateBitmapClipper,
382     ImagingFactory_CreateBitmapFlipRotator,
383     ImagingFactory_CreateStream,
384     ImagingFactory_CreateColorContext,
385     ImagingFactory_CreateColorTransformer,
386     ImagingFactory_CreateBitmap,
387     ImagingFactory_CreateBitmapFromSource,
388     ImagingFactory_CreateBitmapFromSourceRect,
389     ImagingFactory_CreateBitmapFromMemory,
390     ImagingFactory_CreateBitmapFromHBITMAP,
391     ImagingFactory_CreateBitmapFromHICON,
392     ImagingFactory_CreateComponentEnumerator,
393     ImagingFactory_CreateFastMetadataEncoderFromDecoder,
394     ImagingFactory_CreateFastMetadataEncoderFromFrameDecode,
395     ImagingFactory_CreateQueryWriter,
396     ImagingFactory_CreateQueryWriterFromReader
397 };
398
399 HRESULT ImagingFactory_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
400 {
401     ImagingFactory *This;
402     HRESULT ret;
403
404     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
405
406     *ppv = NULL;
407
408     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
409
410     This = HeapAlloc(GetProcessHeap(), 0, sizeof(ImagingFactory));
411     if (!This) return E_OUTOFMEMORY;
412
413     This->lpIWICImagingFactoryVtbl = &ImagingFactory_Vtbl;
414     This->ref = 1;
415
416     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
417     IUnknown_Release((IUnknown*)This);
418
419     return ret;
420 }