mshtml: Use lazy allocation for connection points.
[wine] / dlls / windowscodecs / colorcontext.c
1 /*
2  * Copyright 2012 Hans Leidekker 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 "objbase.h"
28 #include "wincodec.h"
29
30 #include "wincodecs_private.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
35
36 typedef struct ColorContext {
37     IWICColorContext IWICColorContext_iface;
38     LONG ref;
39     WICColorContextType type;
40     BYTE *profile;
41     UINT profile_len;
42     UINT exif_color_space;
43 } ColorContext;
44
45 static inline ColorContext *impl_from_IWICColorContext(IWICColorContext *iface)
46 {
47     return CONTAINING_RECORD(iface, ColorContext, IWICColorContext_iface);
48 }
49
50 static HRESULT WINAPI ColorContext_QueryInterface(IWICColorContext *iface, REFIID iid,
51     void **ppv)
52 {
53     ColorContext *This = impl_from_IWICColorContext(iface);
54     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
55
56     if (!ppv) return E_INVALIDARG;
57
58     if (IsEqualIID(&IID_IUnknown, iid) ||
59         IsEqualIID(&IID_IWICColorContext, iid))
60     {
61         *ppv = &This->IWICColorContext_iface;
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 ColorContext_AddRef(IWICColorContext *iface)
74 {
75     ColorContext *This = impl_from_IWICColorContext(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 ColorContext_Release(IWICColorContext *iface)
84 {
85     ColorContext *This = impl_from_IWICColorContext(iface);
86     ULONG ref = InterlockedDecrement(&This->ref);
87
88     TRACE("(%p) refcount=%u\n", iface, ref);
89
90     if (ref == 0)
91     {
92         HeapFree(GetProcessHeap(), 0, This->profile);
93         HeapFree(GetProcessHeap(), 0, This);
94     }
95
96     return ref;
97 }
98
99 static HRESULT load_profile(const WCHAR *filename, BYTE **profile, UINT *len)
100 {
101     HANDLE handle;
102     DWORD count;
103     LARGE_INTEGER size;
104     BOOL ret;
105
106     *len = 0;
107     *profile = NULL;
108     handle = CreateFileW(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
109     if (handle == INVALID_HANDLE_VALUE) return HRESULT_FROM_WIN32(GetLastError());
110
111     if (!(GetFileSizeEx(handle, &size)))
112     {
113         CloseHandle(handle);
114         return HRESULT_FROM_WIN32(GetLastError());
115     }
116     if (size.u.HighPart)
117     {
118         WARN("file too large\n");
119         CloseHandle(handle);
120         return E_FAIL;
121     }
122     if (!(*profile = HeapAlloc(GetProcessHeap(), 0, size.u.LowPart)))
123     {
124         CloseHandle(handle);
125         return E_OUTOFMEMORY;
126     }
127     ret = ReadFile(handle, *profile, size.u.LowPart, &count, NULL);
128     CloseHandle(handle);
129     if (!ret) return HRESULT_FROM_WIN32(GetLastError());
130     if (count != size.u.LowPart) return E_FAIL;
131     *len = count;
132     return S_OK;
133 }
134
135 static HRESULT WINAPI ColorContext_InitializeFromFilename(IWICColorContext *iface,
136     LPCWSTR wzFilename)
137 {
138     ColorContext *This = impl_from_IWICColorContext(iface);
139     BYTE *profile;
140     UINT len;
141     HRESULT hr;
142     TRACE("(%p,%s)\n", iface, debugstr_w(wzFilename));
143
144     if (This->type != WICColorContextUninitialized && This->type != WICColorContextProfile)
145         return WINCODEC_ERR_WRONGSTATE;
146
147     if (!wzFilename) return E_INVALIDARG;
148
149     hr = load_profile(wzFilename, &profile, &len);
150     if (FAILED(hr)) return hr;
151
152     HeapFree(GetProcessHeap(), 0, This->profile);
153     This->profile = profile;
154     This->profile_len = len;
155     This->type = WICColorContextProfile;
156
157     return S_OK;
158 }
159
160 static HRESULT WINAPI ColorContext_InitializeFromMemory(IWICColorContext *iface,
161     const BYTE *pbBuffer, UINT cbBufferSize)
162 {
163     ColorContext *This = impl_from_IWICColorContext(iface);
164     BYTE *profile;
165     TRACE("(%p,%p,%u)\n", iface, pbBuffer, cbBufferSize);
166
167     if (This->type != WICColorContextUninitialized && This->type != WICColorContextProfile)
168         return WINCODEC_ERR_WRONGSTATE;
169
170     if (!(profile = HeapAlloc(GetProcessHeap(), 0, cbBufferSize))) return E_OUTOFMEMORY;
171     memcpy(profile, pbBuffer, cbBufferSize);
172
173     HeapFree(GetProcessHeap(), 0, This->profile);
174     This->profile = profile;
175     This->profile_len = cbBufferSize;
176     This->type = WICColorContextProfile;
177
178     return S_OK;
179 }
180
181 static HRESULT WINAPI ColorContext_InitializeFromExifColorSpace(IWICColorContext *iface,
182     UINT value)
183 {
184     ColorContext *This = impl_from_IWICColorContext(iface);
185     TRACE("(%p,%u)\n", iface, value);
186
187     if (This->type != WICColorContextUninitialized && This->type != WICColorContextExifColorSpace)
188         return WINCODEC_ERR_WRONGSTATE;
189
190     This->exif_color_space = value;
191     This->type = WICColorContextExifColorSpace;
192
193     return S_OK;
194 }
195
196 static HRESULT WINAPI ColorContext_GetType(IWICColorContext *iface,
197     WICColorContextType *pType)
198 {
199     ColorContext *This = impl_from_IWICColorContext(iface);
200     TRACE("(%p,%p)\n", iface, pType);
201
202     if (!pType) return E_INVALIDARG;
203
204     *pType = This->type;
205     return S_OK;
206 }
207
208 static HRESULT WINAPI ColorContext_GetProfileBytes(IWICColorContext *iface,
209     UINT cbBuffer, BYTE *pbBuffer, UINT *pcbActual)
210 {
211     ColorContext *This = impl_from_IWICColorContext(iface);
212     TRACE("(%p,%u,%p,%p)\n", iface, cbBuffer, pbBuffer, pcbActual);
213
214     if (This->type != WICColorContextProfile)
215         return WINCODEC_ERR_NOTINITIALIZED;
216
217     if (!pcbActual) return E_INVALIDARG;
218
219     if (cbBuffer >= This->profile_len && pbBuffer)
220         memcpy(pbBuffer, This->profile, This->profile_len);
221
222     *pcbActual = This->profile_len;
223
224     return S_OK;
225 }
226
227 static HRESULT WINAPI ColorContext_GetExifColorSpace(IWICColorContext *iface,
228     UINT *pValue)
229 {
230     ColorContext *This = impl_from_IWICColorContext(iface);
231     TRACE("(%p,%p)\n", iface, pValue);
232
233     if (!pValue) return E_INVALIDARG;
234
235     *pValue = This->exif_color_space;
236     return S_OK;
237 }
238
239 static const IWICColorContextVtbl ColorContext_Vtbl = {
240     ColorContext_QueryInterface,
241     ColorContext_AddRef,
242     ColorContext_Release,
243     ColorContext_InitializeFromFilename,
244     ColorContext_InitializeFromMemory,
245     ColorContext_InitializeFromExifColorSpace,
246     ColorContext_GetType,
247     ColorContext_GetProfileBytes,
248     ColorContext_GetExifColorSpace
249 };
250
251 HRESULT ColorContext_Create(IWICColorContext **colorcontext)
252 {
253     ColorContext *This;
254
255     if (!colorcontext) return E_INVALIDARG;
256
257     This = HeapAlloc(GetProcessHeap(), 0, sizeof(ColorContext));
258     if (!This) return E_OUTOFMEMORY;
259
260     This->IWICColorContext_iface.lpVtbl = &ColorContext_Vtbl;
261     This->ref = 1;
262     This->type = 0;
263     This->profile = NULL;
264     This->profile_len = 0;
265     This->exif_color_space = ~0u;
266
267     *colorcontext = &This->IWICColorContext_iface;
268
269     return S_OK;
270 }