windowscodecs: Add stub JPEG decoder.
[wine] / dlls / windowscodecs / jpegformat.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 #include "wine/port.h"
21
22 #ifdef HAVE_UNISTD_H
23 # include <unistd.h>
24 #endif
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #ifdef SONAME_LIBJPEG
30 /* This is a hack, so jpeglib.h does not redefine INT32 and the like*/
31 #define XMD_H
32 #define UINT8 JPEG_UINT8
33 #define UINT16 JPEG_UINT16
34 #define boolean jpeg_boolean
35 #undef HAVE_STDLIB_H
36 # include <jpeglib.h>
37 #undef HAVE_STDLIB_H
38 #define HAVE_STDLIB_H 1
39 #undef UINT8
40 #undef UINT16
41 #undef boolean
42 #endif
43
44 #define COBJMACROS
45
46 #include "windef.h"
47 #include "winbase.h"
48 #include "objbase.h"
49 #include "wincodec.h"
50
51 #include "wincodecs_private.h"
52
53 #include "wine/debug.h"
54 #include "wine/library.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
57
58 #ifdef SONAME_LIBJPEG
59
60 static void *libjpeg_handle;
61
62 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
63 MAKE_FUNCPTR(jpeg_std_error);
64 #undef MAKE_FUNCPTR
65
66 static void *load_libjpeg(void)
67 {
68     if((libjpeg_handle = wine_dlopen(SONAME_LIBJPEG, RTLD_NOW, NULL, 0)) != NULL) {
69
70 #define LOAD_FUNCPTR(f) \
71     if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
72         libjpeg_handle = NULL; \
73         return NULL; \
74     }
75
76         LOAD_FUNCPTR(jpeg_std_error);
77 #undef LOAD_FUNCPTR
78     }
79     return libjpeg_handle;
80 }
81
82 typedef struct {
83     const IWICBitmapDecoderVtbl *lpVtbl;
84     LONG ref;
85 } JpegDecoder;
86
87 static HRESULT WINAPI JpegDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
88     void **ppv)
89 {
90     JpegDecoder *This = (JpegDecoder*)iface;
91     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
92
93     if (!ppv) return E_INVALIDARG;
94
95     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
96     {
97         *ppv = This;
98     }
99     else
100     {
101         *ppv = NULL;
102         return E_NOINTERFACE;
103     }
104
105     IUnknown_AddRef((IUnknown*)*ppv);
106     return S_OK;
107 }
108
109 static ULONG WINAPI JpegDecoder_AddRef(IWICBitmapDecoder *iface)
110 {
111     JpegDecoder *This = (JpegDecoder*)iface;
112     ULONG ref = InterlockedIncrement(&This->ref);
113
114     TRACE("(%p) refcount=%u\n", iface, ref);
115
116     return ref;
117 }
118
119 static ULONG WINAPI JpegDecoder_Release(IWICBitmapDecoder *iface)
120 {
121     JpegDecoder *This = (JpegDecoder*)iface;
122     ULONG ref = InterlockedDecrement(&This->ref);
123
124     TRACE("(%p) refcount=%u\n", iface, ref);
125
126     if (ref == 0)
127     {
128         HeapFree(GetProcessHeap(), 0, This);
129     }
130
131     return ref;
132 }
133
134 static HRESULT WINAPI JpegDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
135     DWORD *pdwCapability)
136 {
137     FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
138     return E_NOTIMPL;
139 }
140
141 static HRESULT WINAPI JpegDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
142     WICDecodeOptions cacheOptions)
143 {
144     FIXME("(%p,%p,%u): stub\n", iface, pIStream, cacheOptions);
145     return E_NOTIMPL;
146 }
147
148 static HRESULT WINAPI JpegDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
149     GUID *pguidContainerFormat)
150 {
151     memcpy(pguidContainerFormat, &GUID_ContainerFormatJpeg, sizeof(GUID));
152     return S_OK;
153 }
154
155 static HRESULT WINAPI JpegDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
156     IWICBitmapDecoderInfo **ppIDecoderInfo)
157 {
158     FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
159     return E_NOTIMPL;
160 }
161
162 static HRESULT WINAPI JpegDecoder_CopyPalette(IWICBitmapDecoder *iface,
163     IWICPalette *pIPalette)
164 {
165     TRACE("(%p,%p)\n", iface, pIPalette);
166
167     return WINCODEC_ERR_PALETTEUNAVAILABLE;
168 }
169
170 static HRESULT WINAPI JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
171     IWICMetadataQueryReader **ppIMetadataQueryReader)
172 {
173     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
174     return E_NOTIMPL;
175 }
176
177 static HRESULT WINAPI JpegDecoder_GetPreview(IWICBitmapDecoder *iface,
178     IWICBitmapSource **ppIBitmapSource)
179 {
180     FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
181     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
182 }
183
184 static HRESULT WINAPI JpegDecoder_GetColorContexts(IWICBitmapDecoder *iface,
185     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
186 {
187     FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
188     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
189 }
190
191 static HRESULT WINAPI JpegDecoder_GetThumbnail(IWICBitmapDecoder *iface,
192     IWICBitmapSource **ppIThumbnail)
193 {
194     FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
195     return WINCODEC_ERR_CODECNOTHUMBNAIL;
196 }
197
198 static HRESULT WINAPI JpegDecoder_GetFrameCount(IWICBitmapDecoder *iface,
199     UINT *pCount)
200 {
201     *pCount = 1;
202     return S_OK;
203 }
204
205 static HRESULT WINAPI JpegDecoder_GetFrame(IWICBitmapDecoder *iface,
206     UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
207 {
208     FIXME("(%p,%u,%p): stub\n", iface, index, ppIBitmapFrame);
209     return E_NOTIMPL;
210 }
211
212 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl = {
213     JpegDecoder_QueryInterface,
214     JpegDecoder_AddRef,
215     JpegDecoder_Release,
216     JpegDecoder_QueryCapability,
217     JpegDecoder_Initialize,
218     JpegDecoder_GetContainerFormat,
219     JpegDecoder_GetDecoderInfo,
220     JpegDecoder_CopyPalette,
221     JpegDecoder_GetMetadataQueryReader,
222     JpegDecoder_GetPreview,
223     JpegDecoder_GetColorContexts,
224     JpegDecoder_GetThumbnail,
225     JpegDecoder_GetFrameCount,
226     JpegDecoder_GetFrame
227 };
228
229 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
230 {
231     JpegDecoder *This;
232     HRESULT ret;
233
234     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
235
236     if (!libjpeg_handle && !load_libjpeg())
237     {
238         ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
239         return E_FAIL;
240     }
241
242     *ppv = NULL;
243
244     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
245
246     This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder));
247     if (!This) return E_OUTOFMEMORY;
248
249     This->lpVtbl = &JpegDecoder_Vtbl;
250     This->ref = 1;
251
252     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
253     IUnknown_Release((IUnknown*)This);
254
255     return ret;
256 }
257
258 #else /* !defined(SONAME_LIBJPEG) */
259
260 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
261 {
262     ERR("Trying to load JPEG picture, but JPEG supported not compiled in.\n");
263     return E_FAIL;
264 }
265
266 #endif