windowscodecs: Implement Initialize for the GIF decoder.
[wine/hacks.git] / dlls / windowscodecs / gifformat.c
blob77010dd31f724803c346a175a28845b1f12d8d51
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
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.
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.
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
19 #include "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
28 #include "wincodec.h"
30 #include "ungif.h"
32 #include "wincodecs_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
38 typedef struct {
39 const IWICBitmapDecoderVtbl *lpVtbl;
40 LONG ref;
41 BOOL initialized;
42 GifFileType *gif;
43 } GifDecoder;
45 static HRESULT WINAPI GifDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
46 void **ppv)
48 GifDecoder *This = (GifDecoder*)iface;
49 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
51 if (!ppv) return E_INVALIDARG;
53 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
55 *ppv = This;
57 else
59 *ppv = NULL;
60 return E_NOINTERFACE;
63 IUnknown_AddRef((IUnknown*)*ppv);
64 return S_OK;
67 static ULONG WINAPI GifDecoder_AddRef(IWICBitmapDecoder *iface)
69 GifDecoder *This = (GifDecoder*)iface;
70 ULONG ref = InterlockedIncrement(&This->ref);
72 TRACE("(%p) refcount=%u\n", iface, ref);
74 return ref;
77 static ULONG WINAPI GifDecoder_Release(IWICBitmapDecoder *iface)
79 GifDecoder *This = (GifDecoder*)iface;
80 ULONG ref = InterlockedDecrement(&This->ref);
82 TRACE("(%p) refcount=%u\n", iface, ref);
84 if (ref == 0)
86 DGifCloseFile(This->gif);
87 HeapFree(GetProcessHeap(), 0, This);
90 return ref;
93 static HRESULT WINAPI GifDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
94 DWORD *pdwCapability)
96 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
97 return E_NOTIMPL;
100 static int _gif_inputfunc(GifFileType *gif, GifByteType *data, int len) {
101 IStream *stream = gif->UserData;
102 ULONG bytesread;
103 HRESULT hr;
105 if (!stream)
107 ERR("attempting to read file after initialization\n");
108 return 0;
111 hr = IStream_Read(stream, data, len, &bytesread);
112 if (hr != S_OK) bytesread = 0;
113 return bytesread;
116 static HRESULT WINAPI GifDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
117 WICDecodeOptions cacheOptions)
119 GifDecoder *This = (GifDecoder*)iface;
120 LARGE_INTEGER seek;
121 int ret;
123 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
125 if (This->initialized || This->gif)
127 WARN("already initialized\n");
128 return WINCODEC_ERR_WRONGSTATE;
131 /* seek to start of stream */
132 seek.QuadPart = 0;
133 IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
135 /* read all data from the stream */
136 This->gif = DGifOpen((void*)pIStream, _gif_inputfunc);
137 if (!This->gif) return E_FAIL;
139 ret = DGifSlurp(This->gif);
140 if (ret == GIF_ERROR) return E_FAIL;
142 /* make sure we don't use the stream after this method returns */
143 This->gif->UserData = NULL;
145 This->initialized = TRUE;
147 return S_OK;
150 static HRESULT WINAPI GifDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
151 GUID *pguidContainerFormat)
153 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
154 return E_NOTIMPL;
157 static HRESULT WINAPI GifDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
158 IWICBitmapDecoderInfo **ppIDecoderInfo)
160 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
161 return E_NOTIMPL;
164 static HRESULT WINAPI GifDecoder_CopyPalette(IWICBitmapDecoder *iface,
165 IWICPalette *pIPalette)
167 TRACE("(%p,%p)\n", iface, pIPalette);
168 return WINCODEC_ERR_PALETTEUNAVAILABLE;
171 static HRESULT WINAPI GifDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
172 IWICMetadataQueryReader **ppIMetadataQueryReader)
174 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
175 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
178 static HRESULT WINAPI GifDecoder_GetPreview(IWICBitmapDecoder *iface,
179 IWICBitmapSource **ppIBitmapSource)
181 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
182 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
185 static HRESULT WINAPI GifDecoder_GetColorContexts(IWICBitmapDecoder *iface,
186 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
188 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
189 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
192 static HRESULT WINAPI GifDecoder_GetThumbnail(IWICBitmapDecoder *iface,
193 IWICBitmapSource **ppIThumbnail)
195 TRACE("(%p,%p)\n", iface, ppIThumbnail);
196 return WINCODEC_ERR_CODECNOTHUMBNAIL;
199 static HRESULT WINAPI GifDecoder_GetFrameCount(IWICBitmapDecoder *iface,
200 UINT *pCount)
202 FIXME("(%p,%p): stub\n", iface, pCount);
203 return E_NOTIMPL;
206 static HRESULT WINAPI GifDecoder_GetFrame(IWICBitmapDecoder *iface,
207 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
209 FIXME("(%p,%u,%p): stub\n", iface, index, ppIBitmapFrame);
210 return E_NOTIMPL;
213 static const IWICBitmapDecoderVtbl GifDecoder_Vtbl = {
214 GifDecoder_QueryInterface,
215 GifDecoder_AddRef,
216 GifDecoder_Release,
217 GifDecoder_QueryCapability,
218 GifDecoder_Initialize,
219 GifDecoder_GetContainerFormat,
220 GifDecoder_GetDecoderInfo,
221 GifDecoder_CopyPalette,
222 GifDecoder_GetMetadataQueryReader,
223 GifDecoder_GetPreview,
224 GifDecoder_GetColorContexts,
225 GifDecoder_GetThumbnail,
226 GifDecoder_GetFrameCount,
227 GifDecoder_GetFrame
230 HRESULT GifDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
232 GifDecoder *This;
233 HRESULT ret;
235 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
237 *ppv = NULL;
239 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
241 This = HeapAlloc(GetProcessHeap(), 0, sizeof(GifDecoder));
242 if (!This) return E_OUTOFMEMORY;
244 This->lpVtbl = &GifDecoder_Vtbl;
245 This->ref = 1;
246 This->initialized = FALSE;
247 This->gif = NULL;
249 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
250 IUnknown_Release((IUnknown*)This);
252 return ret;