1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Copyright 2009 Vincent Povirk for CodeWeavers
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
5c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
6c2c66affSColin Finck  * License as published by the Free Software Foundation; either
7c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
8c2c66affSColin Finck  *
9c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
10c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12c2c66affSColin Finck  * Lesser General Public License for more details.
13c2c66affSColin Finck  *
14c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
15c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
16c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17c2c66affSColin Finck  */
18c2c66affSColin Finck 
19ae80686dSAmine Khaldi #include "config.h"
20ae80686dSAmine Khaldi 
21ae80686dSAmine Khaldi #include <stdarg.h>
22ae80686dSAmine Khaldi 
23ae80686dSAmine Khaldi #define COBJMACROS
24ae80686dSAmine Khaldi 
25ae80686dSAmine Khaldi #include "windef.h"
26ae80686dSAmine Khaldi #include "winbase.h"
27ae80686dSAmine Khaldi #include "winreg.h"
28ae80686dSAmine Khaldi #include "objbase.h"
29ae80686dSAmine Khaldi #include "ocidl.h"
30ae80686dSAmine Khaldi #include "initguid.h"
31ae80686dSAmine Khaldi 
32c2c66affSColin Finck #include "wincodecs_private.h"
33c2c66affSColin Finck 
34ae80686dSAmine Khaldi #include "wine/debug.h"
35ae80686dSAmine Khaldi 
36ae80686dSAmine Khaldi WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
37ae80686dSAmine Khaldi 
38c2c66affSColin Finck extern HRESULT WINAPI WIC_DllGetClassObject(REFCLSID, REFIID, LPVOID *) DECLSPEC_HIDDEN;
39c2c66affSColin Finck 
40c2c66affSColin Finck typedef struct {
41c2c66affSColin Finck     REFCLSID classid;
42c2c66affSColin Finck     class_constructor constructor;
43c2c66affSColin Finck } classinfo;
44c2c66affSColin Finck 
45c2c66affSColin Finck static const classinfo wic_classes[] = {
46*e98ef499SAmine Khaldi     {&CLSID_WICImagingFactory, ImagingFactory_CreateInstance},
47*e98ef499SAmine Khaldi     {&CLSID_WICImagingFactory2, ImagingFactory_CreateInstance},
48c2c66affSColin Finck     {&CLSID_WICBmpDecoder, BmpDecoder_CreateInstance},
49c2c66affSColin Finck     {&CLSID_WICPngDecoder, PngDecoder_CreateInstance},
50c2c66affSColin Finck     {&CLSID_WICPngEncoder, PngEncoder_CreateInstance},
51c2c66affSColin Finck     {&CLSID_WICBmpEncoder, BmpEncoder_CreateInstance},
52c2c66affSColin Finck     {&CLSID_WICGifDecoder, GifDecoder_CreateInstance},
53ae80686dSAmine Khaldi     {&CLSID_WICGifEncoder, GifEncoder_CreateInstance},
54c2c66affSColin Finck     {&CLSID_WICIcoDecoder, IcoDecoder_CreateInstance},
55c2c66affSColin Finck     {&CLSID_WICJpegDecoder, JpegDecoder_CreateInstance},
56c2c66affSColin Finck     {&CLSID_WICJpegEncoder, JpegEncoder_CreateInstance},
57c2c66affSColin Finck     {&CLSID_WICTiffDecoder, TiffDecoder_CreateInstance},
58c2c66affSColin Finck     {&CLSID_WICTiffEncoder, TiffEncoder_CreateInstance},
59c2c66affSColin Finck     {&CLSID_WICIcnsEncoder, IcnsEncoder_CreateInstance},
60c2c66affSColin Finck     {&CLSID_WICDefaultFormatConverter, FormatConverter_CreateInstance},
61c2c66affSColin Finck     {&CLSID_WineTgaDecoder, TgaDecoder_CreateInstance},
62c2c66affSColin Finck     {&CLSID_WICUnknownMetadataReader, UnknownMetadataReader_CreateInstance},
63c2c66affSColin Finck     {&CLSID_WICIfdMetadataReader, IfdMetadataReader_CreateInstance},
64c2c66affSColin Finck     {&CLSID_WICPngChrmMetadataReader, PngChrmReader_CreateInstance},
65c2c66affSColin Finck     {&CLSID_WICPngGamaMetadataReader, PngGamaReader_CreateInstance},
66c2c66affSColin Finck     {&CLSID_WICPngTextMetadataReader, PngTextReader_CreateInstance},
67c2c66affSColin Finck     {&CLSID_WICLSDMetadataReader, LSDReader_CreateInstance},
68c2c66affSColin Finck     {&CLSID_WICIMDMetadataReader, IMDReader_CreateInstance},
69c2c66affSColin Finck     {&CLSID_WICGCEMetadataReader, GCEReader_CreateInstance},
70c2c66affSColin Finck     {&CLSID_WICAPEMetadataReader, APEReader_CreateInstance},
71c2c66affSColin Finck     {&CLSID_WICGifCommentMetadataReader, GifCommentReader_CreateInstance},
72c2c66affSColin Finck     {0}};
73c2c66affSColin Finck 
74c2c66affSColin Finck typedef struct {
75c2c66affSColin Finck     IClassFactory           IClassFactory_iface;
76c2c66affSColin Finck     LONG                    ref;
77c2c66affSColin Finck     const classinfo         *info;
78c2c66affSColin Finck } ClassFactoryImpl;
79c2c66affSColin Finck 
impl_from_IClassFactory(IClassFactory * iface)80c2c66affSColin Finck static inline ClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
81c2c66affSColin Finck {
82c2c66affSColin Finck     return CONTAINING_RECORD(iface, ClassFactoryImpl, IClassFactory_iface);
83c2c66affSColin Finck }
84c2c66affSColin Finck 
ClassFactoryImpl_QueryInterface(IClassFactory * iface,REFIID iid,void ** ppv)85c2c66affSColin Finck static HRESULT WINAPI ClassFactoryImpl_QueryInterface(IClassFactory *iface,
86c2c66affSColin Finck     REFIID iid, void **ppv)
87c2c66affSColin Finck {
88c2c66affSColin Finck     ClassFactoryImpl *This = impl_from_IClassFactory(iface);
89c2c66affSColin Finck     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
90c2c66affSColin Finck 
91c2c66affSColin Finck     if (!ppv) return E_INVALIDARG;
92c2c66affSColin Finck 
93c2c66affSColin Finck     if (IsEqualIID(&IID_IUnknown, iid) ||
94c2c66affSColin Finck         IsEqualIID(&IID_IClassFactory, iid))
95c2c66affSColin Finck     {
96c2c66affSColin Finck         *ppv = &This->IClassFactory_iface;
97c2c66affSColin Finck     }
98c2c66affSColin Finck     else
99c2c66affSColin Finck     {
100c2c66affSColin Finck         *ppv = NULL;
101c2c66affSColin Finck         return E_NOINTERFACE;
102c2c66affSColin Finck     }
103c2c66affSColin Finck 
104c2c66affSColin Finck     IUnknown_AddRef((IUnknown*)*ppv);
105c2c66affSColin Finck     return S_OK;
106c2c66affSColin Finck }
107c2c66affSColin Finck 
ClassFactoryImpl_AddRef(IClassFactory * iface)108c2c66affSColin Finck static ULONG WINAPI ClassFactoryImpl_AddRef(IClassFactory *iface)
109c2c66affSColin Finck {
110c2c66affSColin Finck     ClassFactoryImpl *This = impl_from_IClassFactory(iface);
111c2c66affSColin Finck     ULONG ref = InterlockedIncrement(&This->ref);
112c2c66affSColin Finck 
113c2c66affSColin Finck     TRACE("(%p) refcount=%u\n", iface, ref);
114c2c66affSColin Finck 
115c2c66affSColin Finck     return ref;
116c2c66affSColin Finck }
117c2c66affSColin Finck 
ClassFactoryImpl_Release(IClassFactory * iface)118c2c66affSColin Finck static ULONG WINAPI ClassFactoryImpl_Release(IClassFactory *iface)
119c2c66affSColin Finck {
120c2c66affSColin Finck     ClassFactoryImpl *This = impl_from_IClassFactory(iface);
121c2c66affSColin Finck     ULONG ref = InterlockedDecrement(&This->ref);
122c2c66affSColin Finck 
123c2c66affSColin Finck     TRACE("(%p) refcount=%u\n", iface, ref);
124c2c66affSColin Finck 
125c2c66affSColin Finck     if (ref == 0)
126c2c66affSColin Finck         HeapFree(GetProcessHeap(), 0, This);
127c2c66affSColin Finck 
128c2c66affSColin Finck     return ref;
129c2c66affSColin Finck }
130c2c66affSColin Finck 
ClassFactoryImpl_CreateInstance(IClassFactory * iface,IUnknown * pUnkOuter,REFIID riid,void ** ppv)131c2c66affSColin Finck static HRESULT WINAPI ClassFactoryImpl_CreateInstance(IClassFactory *iface,
132c2c66affSColin Finck     IUnknown *pUnkOuter, REFIID riid, void **ppv)
133c2c66affSColin Finck {
134c2c66affSColin Finck     ClassFactoryImpl *This = impl_from_IClassFactory(iface);
135c2c66affSColin Finck 
136c2c66affSColin Finck     *ppv = NULL;
137c2c66affSColin Finck 
138c2c66affSColin Finck     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
139c2c66affSColin Finck 
140c2c66affSColin Finck     return This->info->constructor(riid, ppv);
141c2c66affSColin Finck }
142c2c66affSColin Finck 
ClassFactoryImpl_LockServer(IClassFactory * iface,BOOL lock)143c2c66affSColin Finck static HRESULT WINAPI ClassFactoryImpl_LockServer(IClassFactory *iface, BOOL lock)
144c2c66affSColin Finck {
145c2c66affSColin Finck     TRACE("(%p, %i): stub\n", iface, lock);
146c2c66affSColin Finck     return E_NOTIMPL;
147c2c66affSColin Finck }
148c2c66affSColin Finck 
149c2c66affSColin Finck static const IClassFactoryVtbl ClassFactoryImpl_Vtbl = {
150c2c66affSColin Finck     ClassFactoryImpl_QueryInterface,
151c2c66affSColin Finck     ClassFactoryImpl_AddRef,
152c2c66affSColin Finck     ClassFactoryImpl_Release,
153c2c66affSColin Finck     ClassFactoryImpl_CreateInstance,
154c2c66affSColin Finck     ClassFactoryImpl_LockServer
155c2c66affSColin Finck };
156c2c66affSColin Finck 
ClassFactoryImpl_Constructor(const classinfo * info,REFIID riid,LPVOID * ppv)157c2c66affSColin Finck static HRESULT ClassFactoryImpl_Constructor(const classinfo *info, REFIID riid, LPVOID *ppv)
158c2c66affSColin Finck {
159c2c66affSColin Finck     ClassFactoryImpl *This;
160c2c66affSColin Finck     HRESULT ret;
161c2c66affSColin Finck 
162c2c66affSColin Finck     *ppv = NULL;
163c2c66affSColin Finck 
164c2c66affSColin Finck     This = HeapAlloc(GetProcessHeap(), 0, sizeof(ClassFactoryImpl));
165c2c66affSColin Finck     if (!This) return E_OUTOFMEMORY;
166c2c66affSColin Finck 
167c2c66affSColin Finck     This->IClassFactory_iface.lpVtbl = &ClassFactoryImpl_Vtbl;
168c2c66affSColin Finck     This->ref = 1;
169c2c66affSColin Finck     This->info = info;
170c2c66affSColin Finck 
171c2c66affSColin Finck     ret = IClassFactory_QueryInterface(&This->IClassFactory_iface, riid, ppv);
172c2c66affSColin Finck     IClassFactory_Release(&This->IClassFactory_iface);
173c2c66affSColin Finck 
174c2c66affSColin Finck     return ret;
175c2c66affSColin Finck }
176c2c66affSColin Finck 
DllGetClassObject(REFCLSID rclsid,REFIID iid,LPVOID * ppv)177c2c66affSColin Finck HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
178c2c66affSColin Finck {
179c2c66affSColin Finck     HRESULT ret;
180c2c66affSColin Finck     const classinfo *info=NULL;
181c2c66affSColin Finck     int i;
182c2c66affSColin Finck 
183c2c66affSColin Finck     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(iid), ppv);
184c2c66affSColin Finck 
185c2c66affSColin Finck     if (!rclsid || !iid || !ppv)
186c2c66affSColin Finck         return E_INVALIDARG;
187c2c66affSColin Finck 
188c2c66affSColin Finck     *ppv = NULL;
189c2c66affSColin Finck 
190c2c66affSColin Finck     for (i=0; wic_classes[i].classid; i++)
191c2c66affSColin Finck     {
192c2c66affSColin Finck         if (IsEqualCLSID(wic_classes[i].classid, rclsid))
193c2c66affSColin Finck         {
194c2c66affSColin Finck             info = &wic_classes[i];
195c2c66affSColin Finck             break;
196c2c66affSColin Finck         }
197c2c66affSColin Finck     }
198c2c66affSColin Finck 
199c2c66affSColin Finck     if (info)
200c2c66affSColin Finck         ret = ClassFactoryImpl_Constructor(info, iid, ppv);
201c2c66affSColin Finck     else
202c2c66affSColin Finck         ret = WIC_DllGetClassObject(rclsid, iid, ppv);
203c2c66affSColin Finck 
204c2c66affSColin Finck     TRACE("<-- %08X\n", ret);
205c2c66affSColin Finck     return ret;
206c2c66affSColin Finck }
207c2c66affSColin Finck 
create_instance(CLSID * clsid,const IID * iid,void ** ppv)208c2c66affSColin Finck HRESULT create_instance(CLSID *clsid, const IID *iid, void **ppv)
209c2c66affSColin Finck {
210c2c66affSColin Finck     int i;
211c2c66affSColin Finck 
212c2c66affSColin Finck     for (i=0; wic_classes[i].classid; i++)
213c2c66affSColin Finck         if (IsEqualCLSID(wic_classes[i].classid, clsid))
214c2c66affSColin Finck             return wic_classes[i].constructor(iid, ppv);
215c2c66affSColin Finck 
216c2c66affSColin Finck     return CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, iid, ppv);
217c2c66affSColin Finck }
218