windowscodecs: Implement IWICColorContext::InitializeFromExifColorSpace.
[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 WINAPI ColorContext_InitializeFromFilename(IWICColorContext *iface,
100     LPCWSTR wzFilename)
101 {
102     FIXME("(%p,%s)\n", iface, debugstr_w(wzFilename));
103     return E_NOTIMPL;
104 }
105
106 static HRESULT WINAPI ColorContext_InitializeFromMemory(IWICColorContext *iface,
107     const BYTE *pbBuffer, UINT cbBufferSize)
108 {
109     ColorContext *This = impl_from_IWICColorContext(iface);
110     TRACE("(%p,%p,%u)\n", iface, pbBuffer, cbBufferSize);
111
112     if (This->type != WICColorContextUninitialized)
113         return WINCODEC_ERR_WRONGSTATE;
114
115     HeapFree(GetProcessHeap(), 0, This->profile);
116     if (!(This->profile = HeapAlloc(GetProcessHeap(), 0, cbBufferSize)))
117         return E_OUTOFMEMORY;
118
119     memcpy(This->profile, pbBuffer, cbBufferSize);
120     This->profile_len = cbBufferSize;
121     This->type = WICColorContextProfile;
122
123     return S_OK;
124 }
125
126 static HRESULT WINAPI ColorContext_InitializeFromExifColorSpace(IWICColorContext *iface,
127     UINT value)
128 {
129     ColorContext *This = impl_from_IWICColorContext(iface);
130     TRACE("(%p,%u)\n", iface, value);
131
132     if (This->type != WICColorContextUninitialized && This->type != WICColorContextExifColorSpace)
133         return WINCODEC_ERR_WRONGSTATE;
134
135     This->exif_color_space = value;
136     This->type = WICColorContextExifColorSpace;
137
138     return S_OK;
139 }
140
141 static HRESULT WINAPI ColorContext_GetType(IWICColorContext *iface,
142     WICColorContextType *pType)
143 {
144     ColorContext *This = impl_from_IWICColorContext(iface);
145     TRACE("(%p,%p)\n", iface, pType);
146
147     *pType = This->type;
148     return S_OK;
149 }
150
151 static HRESULT WINAPI ColorContext_GetProfileBytes(IWICColorContext *iface,
152     UINT cbBuffer, BYTE *pbBuffer, UINT *pcbActual)
153 {
154     FIXME("(%p,%u,%p,%p)\n", iface, cbBuffer, pbBuffer, pcbActual);
155     return E_NOTIMPL;
156 }
157
158 static HRESULT WINAPI ColorContext_GetExifColorSpace(IWICColorContext *iface,
159     UINT *pValue)
160 {
161     FIXME("(%p,%p)\n", iface, pValue);
162     return E_NOTIMPL;
163 }
164
165 static const IWICColorContextVtbl ColorContext_Vtbl = {
166     ColorContext_QueryInterface,
167     ColorContext_AddRef,
168     ColorContext_Release,
169     ColorContext_InitializeFromFilename,
170     ColorContext_InitializeFromMemory,
171     ColorContext_InitializeFromExifColorSpace,
172     ColorContext_GetType,
173     ColorContext_GetProfileBytes,
174     ColorContext_GetExifColorSpace
175 };
176
177 HRESULT ColorContext_Create(IWICColorContext **colorcontext)
178 {
179     ColorContext *This;
180
181     This = HeapAlloc(GetProcessHeap(), 0, sizeof(ColorContext));
182     if (!This) return E_OUTOFMEMORY;
183
184     This->IWICColorContext_iface.lpVtbl = &ColorContext_Vtbl;
185     This->ref = 1;
186     This->type = 0;
187     This->profile = NULL;
188     This->profile_len = 0;
189     This->exif_color_space = ~0u;
190
191     *colorcontext = &This->IWICColorContext_iface;
192
193     return S_OK;
194 }