d3dx9: Implement D3DXSaveTextureToFileInMemory.
[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 #include "wincodecsdk.h"
32
33 #include "wincodecs_private.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
38
39 typedef struct {
40     IWICComponentFactory IWICComponentFactory_iface;
41     LONG ref;
42 } ComponentFactory;
43
44 static inline ComponentFactory *impl_from_IWICComponentFactory(IWICComponentFactory *iface)
45 {
46     return CONTAINING_RECORD(iface, ComponentFactory, IWICComponentFactory_iface);
47 }
48
49 static HRESULT WINAPI ComponentFactory_QueryInterface(IWICComponentFactory *iface, REFIID iid,
50     void **ppv)
51 {
52     ComponentFactory *This = impl_from_IWICComponentFactory(iface);
53     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
54
55     if (!ppv) return E_INVALIDARG;
56
57     if (IsEqualIID(&IID_IUnknown, iid) ||
58         IsEqualIID(&IID_IWICImagingFactory, iid) ||
59         IsEqualIID(&IID_IWICComponentFactory, iid))
60     {
61         *ppv = This;
62     }
63     else
64     {
65         *ppv = NULL;
66         return E_NOINTERFACE;
67     }
68
69     IUnknown_AddRef((IUnknown*)*ppv);
70     return S_OK;
71 }
72
73 static ULONG WINAPI ComponentFactory_AddRef(IWICComponentFactory *iface)
74 {
75     ComponentFactory *This = impl_from_IWICComponentFactory(iface);
76     ULONG ref = InterlockedIncrement(&This->ref);
77
78     TRACE("(%p) refcount=%u\n", iface, ref);
79
80     return ref;
81 }
82
83 static ULONG WINAPI ComponentFactory_Release(IWICComponentFactory *iface)
84 {
85     ComponentFactory *This = impl_from_IWICComponentFactory(iface);
86     ULONG ref = InterlockedDecrement(&This->ref);
87
88     TRACE("(%p) refcount=%u\n", iface, ref);
89
90     if (ref == 0)
91         HeapFree(GetProcessHeap(), 0, This);
92
93     return ref;
94 }
95
96 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFilename(
97     IWICComponentFactory *iface, LPCWSTR wzFilename, const GUID *pguidVendor,
98     DWORD dwDesiredAccess, WICDecodeOptions metadataOptions,
99     IWICBitmapDecoder **ppIDecoder)
100 {
101     IWICStream *stream;
102     HRESULT hr;
103
104     TRACE("(%p,%s,%s,%u,%u,%p)\n", iface, debugstr_w(wzFilename),
105         debugstr_guid(pguidVendor), dwDesiredAccess, metadataOptions, ppIDecoder);
106
107     hr = StreamImpl_Create(&stream);
108     if (SUCCEEDED(hr))
109     {
110         hr = IWICStream_InitializeFromFilename(stream, wzFilename, dwDesiredAccess);
111
112         if (SUCCEEDED(hr))
113         {
114             hr = IWICComponentFactory_CreateDecoderFromStream(iface, (IStream*)stream,
115                 pguidVendor, metadataOptions, ppIDecoder);
116         }
117
118         IWICStream_Release(stream);
119     }
120
121     return hr;
122 }
123
124 static HRESULT WINAPI ComponentFactory_CreateDecoderFromStream(
125     IWICComponentFactory *iface, IStream *pIStream, const GUID *pguidVendor,
126     WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
127 {
128     IEnumUnknown *enumdecoders;
129     IUnknown *unkdecoderinfo;
130     IWICBitmapDecoderInfo *decoderinfo;
131     IWICBitmapDecoder *decoder = NULL, *preferred_decoder = NULL;
132     GUID vendor;
133     HRESULT res=S_OK;
134     ULONG num_fetched;
135     BOOL matches;
136
137     TRACE("(%p,%p,%s,%u,%p)\n", iface, pIStream, debugstr_guid(pguidVendor),
138         metadataOptions, ppIDecoder);
139
140     res = CreateComponentEnumerator(WICDecoder, WICComponentEnumerateDefault, &enumdecoders);
141     if (FAILED(res)) return res;
142
143     while (!preferred_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                     IWICBitmapDecoder *new_decoder;
158
159                     res = IWICBitmapDecoderInfo_CreateInstance(decoderinfo, &new_decoder);
160
161                     /* FIXME: should use QueryCapability to choose a decoder */
162
163                     if (SUCCEEDED(res))
164                     {
165                         res = IWICBitmapDecoder_Initialize(new_decoder, pIStream, metadataOptions);
166
167                         if (SUCCEEDED(res))
168                         {
169                             if (pguidVendor)
170                             {
171                                 res = IWICBitmapDecoderInfo_GetVendorGUID(decoderinfo, &vendor);
172                                 if (SUCCEEDED(res) && IsEqualIID(&vendor, pguidVendor))
173                                 {
174                                     preferred_decoder = new_decoder;
175                                     new_decoder = NULL;
176                                 }
177                             }
178
179                             if (new_decoder && !decoder)
180                             {
181                                 decoder = new_decoder;
182                                 new_decoder = NULL;
183                             }
184                         }
185
186                         if (new_decoder) IWICBitmapDecoder_Release(new_decoder);
187                     }
188                 }
189
190                 IWICBitmapDecoderInfo_Release(decoderinfo);
191             }
192
193             IUnknown_Release(unkdecoderinfo);
194         }
195         else
196             break;
197     }
198
199     IEnumUnknown_Release(enumdecoders);
200
201     if (preferred_decoder)
202     {
203         *ppIDecoder = preferred_decoder;
204         if (decoder) IWICBitmapDecoder_Release(decoder);
205         return S_OK;
206     }
207
208     if (decoder)
209     {
210         *ppIDecoder = decoder;
211         return S_OK;
212     }
213     else
214     {
215         if (WARN_ON(wincodecs))
216         {
217             LARGE_INTEGER seek;
218             BYTE data[4];
219             ULONG bytesread;
220
221             WARN("failed to load from a stream\n");
222
223             seek.QuadPart = 0;
224             res = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
225             if (SUCCEEDED(res))
226                 res = IStream_Read(pIStream, data, 4, &bytesread);
227             if (SUCCEEDED(res))
228                 WARN("first %i bytes of stream=%x %x %x %x\n", bytesread, data[0], data[1], data[2], data[3]);
229         }
230         *ppIDecoder = NULL;
231         return WINCODEC_ERR_COMPONENTNOTFOUND;
232     }
233 }
234
235 static HRESULT WINAPI ComponentFactory_CreateDecoderFromFileHandle(
236     IWICComponentFactory *iface, ULONG_PTR hFile, const GUID *pguidVendor,
237     WICDecodeOptions metadataOptions, IWICBitmapDecoder **ppIDecoder)
238 {
239     FIXME("(%p,%lx,%s,%u,%p): stub\n", iface, hFile, debugstr_guid(pguidVendor),
240         metadataOptions, ppIDecoder);
241     return E_NOTIMPL;
242 }
243
244 static HRESULT WINAPI ComponentFactory_CreateComponentInfo(IWICComponentFactory *iface,
245     REFCLSID clsidComponent, IWICComponentInfo **ppIInfo)
246 {
247     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(clsidComponent), ppIInfo);
248     return CreateComponentInfo(clsidComponent, ppIInfo);
249 }
250
251 static HRESULT WINAPI ComponentFactory_CreateDecoder(IWICComponentFactory *iface,
252     REFGUID guidContainerFormat, const GUID *pguidVendor,
253     IWICBitmapDecoder **ppIDecoder)
254 {
255     FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidContainerFormat),
256         debugstr_guid(pguidVendor), ppIDecoder);
257     return E_NOTIMPL;
258 }
259
260 static HRESULT WINAPI ComponentFactory_CreateEncoder(IWICComponentFactory *iface,
261     REFGUID guidContainerFormat, const GUID *pguidVendor,
262     IWICBitmapEncoder **ppIEncoder)
263 {
264     FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidContainerFormat),
265         debugstr_guid(pguidVendor), ppIEncoder);
266     return E_NOTIMPL;
267 }
268
269 static HRESULT WINAPI ComponentFactory_CreatePalette(IWICComponentFactory *iface,
270     IWICPalette **ppIPalette)
271 {
272     TRACE("(%p,%p)\n", iface, ppIPalette);
273     return PaletteImpl_Create(ppIPalette);
274 }
275
276 static HRESULT WINAPI ComponentFactory_CreateFormatConverter(IWICComponentFactory *iface,
277     IWICFormatConverter **ppIFormatConverter)
278 {
279     return FormatConverter_CreateInstance(NULL, &IID_IWICFormatConverter, (void**)ppIFormatConverter);
280 }
281
282 static HRESULT WINAPI ComponentFactory_CreateBitmapScaler(IWICComponentFactory *iface,
283     IWICBitmapScaler **ppIBitmapScaler)
284 {
285     FIXME("(%p,%p): stub\n", iface, ppIBitmapScaler);
286     return E_NOTIMPL;
287 }
288
289 static HRESULT WINAPI ComponentFactory_CreateBitmapClipper(IWICComponentFactory *iface,
290     IWICBitmapClipper **ppIBitmapClipper)
291 {
292     FIXME("(%p,%p): stub\n", iface, ppIBitmapClipper);
293     return E_NOTIMPL;
294 }
295
296 static HRESULT WINAPI ComponentFactory_CreateBitmapFlipRotator(IWICComponentFactory *iface,
297     IWICBitmapFlipRotator **ppIBitmapFlipRotator)
298 {
299     TRACE("(%p,%p)\n", iface, ppIBitmapFlipRotator);
300     return FlipRotator_Create(ppIBitmapFlipRotator);
301 }
302
303 static HRESULT WINAPI ComponentFactory_CreateStream(IWICComponentFactory *iface,
304     IWICStream **ppIWICStream)
305 {
306     TRACE("(%p,%p)\n", iface, ppIWICStream);
307     return StreamImpl_Create(ppIWICStream);
308 }
309
310 static HRESULT WINAPI ComponentFactory_CreateColorContext(IWICComponentFactory *iface,
311     IWICColorContext **ppIColorContext)
312 {
313     FIXME("(%p,%p): stub\n", iface, ppIColorContext);
314     return E_NOTIMPL;
315 }
316
317 static HRESULT WINAPI ComponentFactory_CreateColorTransformer(IWICComponentFactory *iface,
318     IWICColorTransform **ppIColorTransform)
319 {
320     FIXME("(%p,%p): stub\n", iface, ppIColorTransform);
321     return E_NOTIMPL;
322 }
323
324 static HRESULT WINAPI ComponentFactory_CreateBitmap(IWICComponentFactory *iface,
325     UINT uiWidth, UINT uiHeight, REFWICPixelFormatGUID pixelFormat,
326     WICBitmapCreateCacheOption option, IWICBitmap **ppIBitmap)
327 {
328     FIXME("(%p,%u,%u,%s,%u,%p): stub\n", iface, uiWidth, uiHeight,
329         debugstr_guid(pixelFormat), option, ppIBitmap);
330     return E_NOTIMPL;
331 }
332
333 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSource(IWICComponentFactory *iface,
334     IWICBitmapSource *piBitmapSource, WICBitmapCreateCacheOption option,
335     IWICBitmap **ppIBitmap)
336 {
337     FIXME("(%p,%p,%u,%p): stub\n", iface, piBitmapSource, option, ppIBitmap);
338     return E_NOTIMPL;
339 }
340
341 static HRESULT WINAPI ComponentFactory_CreateBitmapFromSourceRect(IWICComponentFactory *iface,
342     IWICBitmapSource *piBitmapSource, UINT x, UINT y, UINT width, UINT height,
343     IWICBitmap **ppIBitmap)
344 {
345     FIXME("(%p,%p,%u,%u,%u,%u,%p): stub\n", iface, piBitmapSource, x, y, width,
346         height, ppIBitmap);
347     return E_NOTIMPL;
348 }
349
350 static HRESULT WINAPI ComponentFactory_CreateBitmapFromMemory(IWICComponentFactory *iface,
351     UINT uiWidth, UINT uiHeight, REFWICPixelFormatGUID pixelFormat, UINT cbStride,
352     UINT cbBufferSize, BYTE *pbBuffer, IWICBitmap **ppIBitmap)
353 {
354     FIXME("(%p,%u,%u,%s,%u,%u,%p,%p): stub\n", iface, uiWidth, uiHeight,
355         debugstr_guid(pixelFormat), cbStride, cbBufferSize, pbBuffer, ppIBitmap);
356     return E_NOTIMPL;
357 }
358
359 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHBITMAP(IWICComponentFactory *iface,
360     HBITMAP hBitmap, HPALETTE hPalette, WICBitmapAlphaChannelOption options,
361     IWICBitmap **ppIBitmap)
362 {
363     FIXME("(%p,%p,%p,%u,%p): stub\n", iface, hBitmap, hPalette, options, ppIBitmap);
364     return E_NOTIMPL;
365 }
366
367 static HRESULT WINAPI ComponentFactory_CreateBitmapFromHICON(IWICComponentFactory *iface,
368     HICON hIcon, IWICBitmap **ppIBitmap)
369 {
370     FIXME("(%p,%p,%p): stub\n", iface, hIcon, ppIBitmap);
371     return E_NOTIMPL;
372 }
373
374 static HRESULT WINAPI ComponentFactory_CreateComponentEnumerator(IWICComponentFactory *iface,
375     DWORD componentTypes, DWORD options, IEnumUnknown **ppIEnumUnknown)
376 {
377     TRACE("(%p,%u,%u,%p)\n", iface, componentTypes, options, ppIEnumUnknown);
378     return CreateComponentEnumerator(componentTypes, options, ppIEnumUnknown);
379 }
380
381 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromDecoder(
382     IWICComponentFactory *iface, IWICBitmapDecoder *pIDecoder,
383     IWICFastMetadataEncoder **ppIFastEncoder)
384 {
385     FIXME("(%p,%p,%p): stub\n", iface, pIDecoder, ppIFastEncoder);
386     return E_NOTIMPL;
387 }
388
389 static HRESULT WINAPI ComponentFactory_CreateFastMetadataEncoderFromFrameDecode(
390     IWICComponentFactory *iface, IWICBitmapFrameDecode *pIFrameDecoder,
391     IWICFastMetadataEncoder **ppIFastEncoder)
392 {
393     FIXME("(%p,%p,%p): stub\n", iface, pIFrameDecoder, ppIFastEncoder);
394     return E_NOTIMPL;
395 }
396
397 static HRESULT WINAPI ComponentFactory_CreateQueryWriter(IWICComponentFactory *iface,
398     REFGUID guidMetadataFormat, const GUID *pguidVendor,
399     IWICMetadataQueryWriter **ppIQueryWriter)
400 {
401     FIXME("(%p,%s,%s,%p): stub\n", iface, debugstr_guid(guidMetadataFormat),
402         debugstr_guid(pguidVendor), ppIQueryWriter);
403     return E_NOTIMPL;
404 }
405
406 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromReader(IWICComponentFactory *iface,
407     IWICMetadataQueryReader *pIQueryReader, const GUID *pguidVendor,
408     IWICMetadataQueryWriter **ppIQueryWriter)
409 {
410     FIXME("(%p,%p,%s,%p): stub\n", iface, pIQueryReader, debugstr_guid(pguidVendor),
411         ppIQueryWriter);
412     return E_NOTIMPL;
413 }
414
415 static HRESULT WINAPI ComponentFactory_CreateMetadataReader(IWICComponentFactory *iface,
416         REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
417 {
418     FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
419         options, stream, reader);
420     return E_NOTIMPL;
421 }
422
423 static HRESULT WINAPI ComponentFactory_CreateMetadataReaderFromContainer(IWICComponentFactory *iface,
424         REFGUID format, const GUID *vendor, DWORD options, IStream *stream, IWICMetadataReader **reader)
425 {
426     FIXME("%p,%s,%s,%x,%p,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor),
427         options, stream, reader);
428     return E_NOTIMPL;
429 }
430
431 static HRESULT WINAPI ComponentFactory_CreateMetadataWriter(IWICComponentFactory *iface,
432         REFGUID format, const GUID *vendor, DWORD options, IWICMetadataWriter **writer)
433 {
434     FIXME("%p,%s,%s,%x,%p: stub\n", iface, debugstr_guid(format), debugstr_guid(vendor), options, writer);
435     return E_NOTIMPL;
436 }
437
438 static HRESULT WINAPI ComponentFactory_CreateMetadataWriterFromReader(IWICComponentFactory *iface,
439         IWICMetadataReader *reader, const GUID *vendor, IWICMetadataWriter **writer)
440 {
441     FIXME("%p,%p,%s,%p: stub\n", iface, reader, debugstr_guid(vendor), writer);
442     return E_NOTIMPL;
443 }
444
445 static HRESULT WINAPI ComponentFactory_CreateQueryReaderFromBlockReader(IWICComponentFactory *iface,
446         IWICMetadataBlockReader *block_reader, IWICMetadataQueryReader **query_reader)
447 {
448     FIXME("%p,%p,%p: stub\n", iface, block_reader, query_reader);
449     return E_NOTIMPL;
450 }
451
452 static HRESULT WINAPI ComponentFactory_CreateQueryWriterFromBlockWriter(IWICComponentFactory *iface,
453         IWICMetadataBlockWriter *block_writer, IWICMetadataQueryWriter **query_writer)
454 {
455     FIXME("%p,%p,%p: stub\n", iface, block_writer, query_writer);
456     return E_NOTIMPL;
457 }
458
459 static HRESULT WINAPI ComponentFactory_CreateEncoderPropertyBag(IWICComponentFactory *iface,
460         PROPBAG2 *options, UINT count, IPropertyBag2 **property)
461 {
462     FIXME("%p,%p,%u,%p: stub\n", iface, options, count, property);
463     return E_NOTIMPL;
464 }
465
466 static const IWICComponentFactoryVtbl ComponentFactory_Vtbl = {
467     ComponentFactory_QueryInterface,
468     ComponentFactory_AddRef,
469     ComponentFactory_Release,
470     ComponentFactory_CreateDecoderFromFilename,
471     ComponentFactory_CreateDecoderFromStream,
472     ComponentFactory_CreateDecoderFromFileHandle,
473     ComponentFactory_CreateComponentInfo,
474     ComponentFactory_CreateDecoder,
475     ComponentFactory_CreateEncoder,
476     ComponentFactory_CreatePalette,
477     ComponentFactory_CreateFormatConverter,
478     ComponentFactory_CreateBitmapScaler,
479     ComponentFactory_CreateBitmapClipper,
480     ComponentFactory_CreateBitmapFlipRotator,
481     ComponentFactory_CreateStream,
482     ComponentFactory_CreateColorContext,
483     ComponentFactory_CreateColorTransformer,
484     ComponentFactory_CreateBitmap,
485     ComponentFactory_CreateBitmapFromSource,
486     ComponentFactory_CreateBitmapFromSourceRect,
487     ComponentFactory_CreateBitmapFromMemory,
488     ComponentFactory_CreateBitmapFromHBITMAP,
489     ComponentFactory_CreateBitmapFromHICON,
490     ComponentFactory_CreateComponentEnumerator,
491     ComponentFactory_CreateFastMetadataEncoderFromDecoder,
492     ComponentFactory_CreateFastMetadataEncoderFromFrameDecode,
493     ComponentFactory_CreateQueryWriter,
494     ComponentFactory_CreateQueryWriterFromReader,
495     ComponentFactory_CreateMetadataReader,
496     ComponentFactory_CreateMetadataReaderFromContainer,
497     ComponentFactory_CreateMetadataWriter,
498     ComponentFactory_CreateMetadataWriterFromReader,
499     ComponentFactory_CreateQueryReaderFromBlockReader,
500     ComponentFactory_CreateQueryWriterFromBlockWriter,
501     ComponentFactory_CreateEncoderPropertyBag
502 };
503
504 HRESULT ComponentFactory_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
505 {
506     ComponentFactory *This;
507     HRESULT ret;
508
509     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
510
511     *ppv = NULL;
512
513     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
514
515     This = HeapAlloc(GetProcessHeap(), 0, sizeof(ComponentFactory));
516     if (!This) return E_OUTOFMEMORY;
517
518     This->IWICComponentFactory_iface.lpVtbl = &ComponentFactory_Vtbl;
519     This->ref = 1;
520
521     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
522     IUnknown_Release((IUnknown*)This);
523
524     return ret;
525 }