windowscodecs: Add test for 1-bit indexed color BMP.
[wine] / dlls / windowscodecs / clsfactory.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 "ocidl.h"
30 #include "initguid.h"
31 #include "wincodec.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     REFCLSID classid;
41     HRESULT (*constructor)(IUnknown*,REFIID,void**);
42 } classinfo;
43
44 static classinfo wic_classes[] = {
45     {&CLSID_WICImagingFactory, ImagingFactory_CreateInstance},
46     {&CLSID_WICBmpDecoder, BmpDecoder_CreateInstance},
47     {0}};
48
49 typedef struct {
50     const IClassFactoryVtbl *lpIClassFactoryVtbl;
51     LONG                    ref;
52     classinfo               *info;
53 } ClassFactoryImpl;
54
55 static HRESULT WINAPI ClassFactoryImpl_QueryInterface(IClassFactory *iface,
56     REFIID iid, void **ppv)
57 {
58     ClassFactoryImpl *This = (ClassFactoryImpl*)iface;
59     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
60
61     if (!ppv) return E_INVALIDARG;
62
63     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IClassFactory, iid))
64     {
65         *ppv = This;
66     }
67     else
68     {
69         *ppv = NULL;
70         return E_NOINTERFACE;
71     }
72
73     IUnknown_AddRef((IUnknown*)*ppv);
74     return S_OK;
75 }
76
77 static ULONG WINAPI ClassFactoryImpl_AddRef(IClassFactory *iface)
78 {
79     ClassFactoryImpl *This = (ClassFactoryImpl*)iface;
80     ULONG ref = InterlockedIncrement(&This->ref);
81
82     TRACE("(%p) refcount=%u\n", iface, ref);
83
84     return ref;
85 }
86
87 static ULONG WINAPI ClassFactoryImpl_Release(IClassFactory *iface)
88 {
89     ClassFactoryImpl *This = (ClassFactoryImpl*)iface;
90     ULONG ref = InterlockedDecrement(&This->ref);
91
92     TRACE("(%p) refcount=%u\n", iface, ref);
93
94     if (ref == 0)
95         HeapFree(GetProcessHeap(), 0, This);
96
97     return ref;
98 }
99
100 static HRESULT WINAPI ClassFactoryImpl_CreateInstance(IClassFactory *iface,
101     IUnknown *pUnkOuter, REFIID riid, void **ppv)
102 {
103     ClassFactoryImpl *This = (ClassFactoryImpl*)iface;
104
105     return This->info->constructor(pUnkOuter, riid, ppv);
106 }
107
108 static HRESULT WINAPI ClassFactoryImpl_LockServer(IClassFactory *iface, BOOL lock)
109 {
110     TRACE("(%p, %i): stub\n", iface, lock);
111     return E_NOTIMPL;
112 }
113
114 static const IClassFactoryVtbl ClassFactoryImpl_Vtbl = {
115     ClassFactoryImpl_QueryInterface,
116     ClassFactoryImpl_AddRef,
117     ClassFactoryImpl_Release,
118     ClassFactoryImpl_CreateInstance,
119     ClassFactoryImpl_LockServer
120 };
121
122 static HRESULT ClassFactoryImpl_Constructor(classinfo *info, REFIID riid, LPVOID *ppv)
123 {
124     ClassFactoryImpl *This;
125     HRESULT ret;
126
127     *ppv = NULL;
128
129     This = HeapAlloc(GetProcessHeap(), 0, sizeof(ClassFactoryImpl));
130     if (!This) return E_OUTOFMEMORY;
131
132     This->lpIClassFactoryVtbl = &ClassFactoryImpl_Vtbl;
133     This->ref = 1;
134     This->info = info;
135
136     ret = IClassFactory_QueryInterface((IClassFactory*)This, riid, ppv);
137     IClassFactory_Release((IClassFactory*)This);
138
139     return ret;
140 }
141
142 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
143 {
144     HRESULT ret;
145     classinfo *info=NULL;
146     int i;
147
148     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(iid), ppv);
149
150     if (!rclsid || !iid || !ppv)
151         return E_INVALIDARG;
152
153     *ppv = NULL;
154
155     for (i=0; wic_classes[i].classid; i++)
156     {
157         if (IsEqualCLSID(wic_classes[i].classid, rclsid))
158         {
159             info = &wic_classes[i];
160             break;
161         }
162     }
163
164     if (info)
165         ret = ClassFactoryImpl_Constructor(info, iid, ppv);
166     else
167         ret = CLASS_E_CLASSNOTAVAILABLE;
168
169     TRACE("<-- %08X\n", ret);
170     return ret;
171 }