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