windowscodecs: Implement IWICColorContext::InitializeFromMemory.
[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 } ColorContext;
43
44 static inline ColorContext *impl_from_IWICColorContext(IWICColorContext *iface)
45 {
46     return CONTAINING_RECORD(iface, ColorContext, IWICColorContext_iface);
47 }
48
49 static HRESULT WINAPI ColorContext_QueryInterface(IWICColorContext *iface, REFIID iid,
50     void **ppv)
51 {
52     ColorContext *This = impl_from_IWICColorContext(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_IWICColorContext, iid))
59     {
60         *ppv = &This->IWICColorContext_iface;
61     }
62     else
63     {
64         *ppv = NULL;
65         return E_NOINTERFACE;
66     }
67
68     IUnknown_AddRef((IUnknown*)*ppv);
69     return S_OK;
70 }
71
72 static ULONG WINAPI ColorContext_AddRef(IWICColorContext *iface)
73 {
74     ColorContext *This = impl_from_IWICColorContext(iface);
75     ULONG ref = InterlockedIncrement(&This->ref);
76
77     TRACE("(%p) refcount=%u\n", iface, ref);
78
79     return ref;
80 }
81
82 static ULONG WINAPI ColorContext_Release(IWICColorContext *iface)
83 {
84     ColorContext *This = impl_from_IWICColorContext(iface);
85     ULONG ref = InterlockedDecrement(&This->ref);
86
87     TRACE("(%p) refcount=%u\n", iface, ref);
88
89     if (ref == 0)
90     {
91         HeapFree(GetProcessHeap(), 0, This->profile);
92         HeapFree(GetProcessHeap(), 0, This);
93     }
94
95     return ref;
96 }
97
98 static HRESULT WINAPI ColorContext_InitializeFromFilename(IWICColorContext *iface,
99     LPCWSTR wzFilename)
100 {
101     FIXME("(%p,%s)\n", iface, debugstr_w(wzFilename));
102     return E_NOTIMPL;
103 }
104
105 static HRESULT WINAPI ColorContext_InitializeFromMemory(IWICColorContext *iface,
106     const BYTE *pbBuffer, UINT cbBufferSize)
107 {
108     ColorContext *This = impl_from_IWICColorContext(iface);
109     TRACE("(%p,%p,%u)\n", iface, pbBuffer, cbBufferSize);
110
111     if (This->type != WICColorContextUninitialized)
112         return WINCODEC_ERR_WRONGSTATE;
113
114     HeapFree(GetProcessHeap(), 0, This->profile);
115     if (!(This->profile = HeapAlloc(GetProcessHeap(), 0, cbBufferSize)))
116         return E_OUTOFMEMORY;
117
118     memcpy(This->profile, pbBuffer, cbBufferSize);
119     This->profile_len = cbBufferSize;
120     This->type = WICColorContextProfile;
121
122     return S_OK;
123 }
124
125 static HRESULT WINAPI ColorContext_InitializeFromExifColorSpace(IWICColorContext *iface,
126     UINT value)
127 {
128     FIXME("(%p,%u)\n", iface, value);
129     return E_NOTIMPL;
130 }
131
132 static HRESULT WINAPI ColorContext_GetType(IWICColorContext *iface,
133     WICColorContextType *pType)
134 {
135     ColorContext *This = impl_from_IWICColorContext(iface);
136     TRACE("(%p,%p)\n", iface, pType);
137
138     *pType = This->type;
139     return S_OK;
140 }
141
142 static HRESULT WINAPI ColorContext_GetProfileBytes(IWICColorContext *iface,
143     UINT cbBuffer, BYTE *pbBuffer, UINT *pcbActual)
144 {
145     FIXME("(%p,%u,%p,%p)\n", iface, cbBuffer, pbBuffer, pcbActual);
146     return E_NOTIMPL;
147 }
148
149 static HRESULT WINAPI ColorContext_GetExifColorSpace(IWICColorContext *iface,
150     UINT *pValue)
151 {
152     FIXME("(%p,%p)\n", iface, pValue);
153     return E_NOTIMPL;
154 }
155
156 static const IWICColorContextVtbl ColorContext_Vtbl = {
157     ColorContext_QueryInterface,
158     ColorContext_AddRef,
159     ColorContext_Release,
160     ColorContext_InitializeFromFilename,
161     ColorContext_InitializeFromMemory,
162     ColorContext_InitializeFromExifColorSpace,
163     ColorContext_GetType,
164     ColorContext_GetProfileBytes,
165     ColorContext_GetExifColorSpace
166 };
167
168 HRESULT ColorContext_Create(IWICColorContext **colorcontext)
169 {
170     ColorContext *This;
171
172     This = HeapAlloc(GetProcessHeap(), 0, sizeof(ColorContext));
173     if (!This) return E_OUTOFMEMORY;
174
175     This->IWICColorContext_iface.lpVtbl = &ColorContext_Vtbl;
176     This->ref = 1;
177     This->type = 0;
178     This->profile = NULL;
179     This->profile_len = 0;
180
181     *colorcontext = &This->IWICColorContext_iface;
182
183     return S_OK;
184 }