wined3d: Use a shader for unconverted blits.
[wine.git] / dlls / windowscodecs / gifformat.c
blob3a82c09778c89f3e02c77bead1dc15486e73490f
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2012 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
22 #include <stdarg.h>
24 #define COBJMACROS
25 #define NONAMELESSUNION
27 #include "windef.h"
28 #include "winbase.h"
29 #include "objbase.h"
30 #include "wincodec.h"
31 #include "wincodecsdk.h"
33 #include "ungif.h"
35 #include "wincodecs_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
41 static LPWSTR strdupAtoW(const char *src)
43 int len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
44 LPWSTR dst = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
45 if (dst) MultiByteToWideChar(CP_ACP, 0, src, -1, dst, len);
46 return dst;
49 static HRESULT load_LSD_metadata(IStream *stream, const GUID *vendor, DWORD options,
50 MetadataItem **items, DWORD *count)
52 #include "pshpack1.h"
53 struct logical_screen_descriptor
55 char signature[6];
56 USHORT width;
57 USHORT height;
58 BYTE packed;
59 /* global_color_table_flag : 1;
60 * color_resolution : 3;
61 * sort_flag : 1;
62 * global_color_table_size : 3;
64 BYTE background_color_index;
65 BYTE pixel_aspect_ratio;
66 } lsd_data;
67 #include "poppack.h"
68 HRESULT hr;
69 ULONG bytesread, i;
70 MetadataItem *result;
72 *items = NULL;
73 *count = 0;
75 hr = IStream_Read(stream, &lsd_data, sizeof(lsd_data), &bytesread);
76 if (FAILED(hr) || bytesread != sizeof(lsd_data)) return S_OK;
78 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem) * 9);
79 if (!result) return E_OUTOFMEMORY;
81 for (i = 0; i < 9; i++)
83 PropVariantInit(&result[i].schema);
84 PropVariantInit(&result[i].id);
85 PropVariantInit(&result[i].value);
88 result[0].id.vt = VT_LPWSTR;
89 result[0].id.u.pwszVal = strdupAtoW("Signature");
90 result[0].value.vt = VT_UI1|VT_VECTOR;
91 result[0].value.u.caub.cElems = sizeof(lsd_data.signature);
92 result[0].value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, sizeof(lsd_data.signature));
93 memcpy(result[0].value.u.caub.pElems, lsd_data.signature, sizeof(lsd_data.signature));
95 result[1].id.vt = VT_LPWSTR;
96 result[1].id.u.pwszVal = strdupAtoW("Width");
97 result[1].value.vt = VT_UI2;
98 result[1].value.u.uiVal = lsd_data.width;
100 result[2].id.vt = VT_LPWSTR;
101 result[2].id.u.pwszVal = strdupAtoW("Height");
102 result[2].value.vt = VT_UI2;
103 result[2].value.u.uiVal = lsd_data.height;
105 result[3].id.vt = VT_LPWSTR;
106 result[3].id.u.pwszVal = strdupAtoW("GlobalColorTableFlag");
107 result[3].value.vt = VT_BOOL;
108 result[3].value.u.boolVal = (lsd_data.packed >> 7) & 1;
110 result[4].id.vt = VT_LPWSTR;
111 result[4].id.u.pwszVal = strdupAtoW("ColorResolution");
112 result[4].value.vt = VT_UI1;
113 result[4].value.u.bVal = (lsd_data.packed >> 4) & 7;
115 result[5].id.vt = VT_LPWSTR;
116 result[5].id.u.pwszVal = strdupAtoW("SortFlag");
117 result[5].value.vt = VT_BOOL;
118 result[5].value.u.boolVal = (lsd_data.packed >> 3) & 1;
120 result[6].id.vt = VT_LPWSTR;
121 result[6].id.u.pwszVal = strdupAtoW("GlobalColorTableSize");
122 result[6].value.vt = VT_UI1;
123 result[6].value.u.bVal = lsd_data.packed & 7;
125 result[7].id.vt = VT_LPWSTR;
126 result[7].id.u.pwszVal = strdupAtoW("BackgroundColorIndex");
127 result[7].value.vt = VT_UI1;
128 result[7].value.u.bVal = lsd_data.background_color_index;
130 result[8].id.vt = VT_LPWSTR;
131 result[8].id.u.pwszVal = strdupAtoW("PixelAspectRatio");
132 result[8].value.vt = VT_UI1;
133 result[8].value.u.bVal = lsd_data.pixel_aspect_ratio;
135 *items = result;
136 *count = 9;
138 return S_OK;
141 static const MetadataHandlerVtbl LSDReader_Vtbl = {
143 &CLSID_WICLSDMetadataReader,
144 load_LSD_metadata
147 HRESULT LSDReader_CreateInstance(REFIID iid, void **ppv)
149 return MetadataReader_Create(&LSDReader_Vtbl, iid, ppv);
152 #include "pshpack1.h"
153 struct image_descriptor
155 USHORT left;
156 USHORT top;
157 USHORT width;
158 USHORT height;
159 BYTE packed;
160 /* local_color_table_flag : 1;
161 * interlace_flag : 1;
162 * sort_flag : 1;
163 * reserved : 2;
164 * local_color_table_size : 3;
167 #include "poppack.h"
169 static HRESULT load_IMD_metadata(IStream *stream, const GUID *vendor, DWORD options,
170 MetadataItem **items, DWORD *count)
172 struct image_descriptor imd_data;
173 HRESULT hr;
174 ULONG bytesread, i;
175 MetadataItem *result;
177 *items = NULL;
178 *count = 0;
180 hr = IStream_Read(stream, &imd_data, sizeof(imd_data), &bytesread);
181 if (FAILED(hr) || bytesread != sizeof(imd_data)) return S_OK;
183 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem) * 8);
184 if (!result) return E_OUTOFMEMORY;
186 for (i = 0; i < 8; i++)
188 PropVariantInit(&result[i].schema);
189 PropVariantInit(&result[i].id);
190 PropVariantInit(&result[i].value);
193 result[0].id.vt = VT_LPWSTR;
194 result[0].id.u.pwszVal = strdupAtoW("Left");
195 result[0].value.vt = VT_UI2;
196 result[0].value.u.uiVal = imd_data.left;
198 result[1].id.vt = VT_LPWSTR;
199 result[1].id.u.pwszVal = strdupAtoW("Top");
200 result[1].value.vt = VT_UI2;
201 result[1].value.u.uiVal = imd_data.top;
203 result[2].id.vt = VT_LPWSTR;
204 result[2].id.u.pwszVal = strdupAtoW("Width");
205 result[2].value.vt = VT_UI2;
206 result[2].value.u.uiVal = imd_data.width;
208 result[3].id.vt = VT_LPWSTR;
209 result[3].id.u.pwszVal = strdupAtoW("Height");
210 result[3].value.vt = VT_UI2;
211 result[3].value.u.uiVal = imd_data.height;
213 result[4].id.vt = VT_LPWSTR;
214 result[4].id.u.pwszVal = strdupAtoW("LocalColorTableFlag");
215 result[4].value.vt = VT_BOOL;
216 result[4].value.u.boolVal = (imd_data.packed >> 7) & 1;
218 result[5].id.vt = VT_LPWSTR;
219 result[5].id.u.pwszVal = strdupAtoW("InterlaceFlag");
220 result[5].value.vt = VT_BOOL;
221 result[5].value.u.boolVal = (imd_data.packed >> 6) & 1;
223 result[6].id.vt = VT_LPWSTR;
224 result[6].id.u.pwszVal = strdupAtoW("SortFlag");
225 result[6].value.vt = VT_BOOL;
226 result[6].value.u.boolVal = (imd_data.packed >> 5) & 1;
228 result[7].id.vt = VT_LPWSTR;
229 result[7].id.u.pwszVal = strdupAtoW("LocalColorTableSize");
230 result[7].value.vt = VT_UI1;
231 result[7].value.u.bVal = imd_data.packed & 7;
233 *items = result;
234 *count = 8;
236 return S_OK;
239 static const MetadataHandlerVtbl IMDReader_Vtbl = {
241 &CLSID_WICIMDMetadataReader,
242 load_IMD_metadata
245 HRESULT IMDReader_CreateInstance(REFIID iid, void **ppv)
247 return MetadataReader_Create(&IMDReader_Vtbl, iid, ppv);
250 static HRESULT load_GCE_metadata(IStream *stream, const GUID *vendor, DWORD options,
251 MetadataItem **items, DWORD *count)
253 #include "pshpack1.h"
254 struct graphic_control_extenstion
256 BYTE packed;
257 /* reservred: 3;
258 * disposal : 3;
259 * user_input_flag : 1;
260 * transparency_flag : 1;
262 USHORT delay;
263 BYTE transparent_color_index;
264 } gce_data;
265 #include "poppack.h"
266 HRESULT hr;
267 ULONG bytesread, i;
268 MetadataItem *result;
270 *items = NULL;
271 *count = 0;
273 hr = IStream_Read(stream, &gce_data, sizeof(gce_data), &bytesread);
274 if (FAILED(hr) || bytesread != sizeof(gce_data)) return S_OK;
276 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem) * 5);
277 if (!result) return E_OUTOFMEMORY;
279 for (i = 0; i < 5; i++)
281 PropVariantInit(&result[i].schema);
282 PropVariantInit(&result[i].id);
283 PropVariantInit(&result[i].value);
286 result[0].id.vt = VT_LPWSTR;
287 result[0].id.u.pwszVal = strdupAtoW("Disposal");
288 result[0].value.vt = VT_UI1;
289 result[0].value.u.bVal = (gce_data.packed >> 2) & 7;
291 result[1].id.vt = VT_LPWSTR;
292 result[1].id.u.pwszVal = strdupAtoW("UserInputFlag");
293 result[1].value.vt = VT_BOOL;
294 result[1].value.u.boolVal = (gce_data.packed >> 1) & 1;
296 result[2].id.vt = VT_LPWSTR;
297 result[2].id.u.pwszVal = strdupAtoW("TransparencyFlag");
298 result[2].value.vt = VT_BOOL;
299 result[2].value.u.boolVal = gce_data.packed & 1;
301 result[3].id.vt = VT_LPWSTR;
302 result[3].id.u.pwszVal = strdupAtoW("Delay");
303 result[3].value.vt = VT_UI2;
304 result[3].value.u.uiVal = gce_data.delay;
306 result[4].id.vt = VT_LPWSTR;
307 result[4].id.u.pwszVal = strdupAtoW("TransparentColorIndex");
308 result[4].value.vt = VT_UI1;
309 result[4].value.u.bVal = gce_data.transparent_color_index;
311 *items = result;
312 *count = 5;
314 return S_OK;
317 static const MetadataHandlerVtbl GCEReader_Vtbl = {
319 &CLSID_WICGCEMetadataReader,
320 load_GCE_metadata
323 HRESULT GCEReader_CreateInstance(REFIID iid, void **ppv)
325 return MetadataReader_Create(&GCEReader_Vtbl, iid, ppv);
328 static HRESULT load_APE_metadata(IStream *stream, const GUID *vendor, DWORD options,
329 MetadataItem **items, DWORD *count)
331 #include "pshpack1.h"
332 struct application_extenstion
334 BYTE extension_introducer;
335 BYTE extension_label;
336 BYTE block_size;
337 BYTE application[11];
338 } ape_data;
339 #include "poppack.h"
340 HRESULT hr;
341 ULONG bytesread, data_size, i;
342 MetadataItem *result;
343 BYTE subblock_size;
344 BYTE *data;
346 *items = NULL;
347 *count = 0;
349 hr = IStream_Read(stream, &ape_data, sizeof(ape_data), &bytesread);
350 if (FAILED(hr) || bytesread != sizeof(ape_data)) return S_OK;
351 if (ape_data.extension_introducer != 0x21 ||
352 ape_data.extension_label != APPLICATION_EXT_FUNC_CODE ||
353 ape_data.block_size != 11)
354 return S_OK;
356 data = NULL;
357 data_size = 0;
359 for (;;)
361 hr = IStream_Read(stream, &subblock_size, sizeof(subblock_size), &bytesread);
362 if (FAILED(hr) || bytesread != sizeof(subblock_size))
364 HeapFree(GetProcessHeap(), 0, data);
365 return S_OK;
367 if (!subblock_size) break;
369 if (!data)
370 data = HeapAlloc(GetProcessHeap(), 0, subblock_size + 1);
371 else
373 BYTE *new_data = HeapReAlloc(GetProcessHeap(), 0, data, data_size + subblock_size + 1);
374 if (!new_data)
376 HeapFree(GetProcessHeap(), 0, data);
377 return S_OK;
379 data = new_data;
381 data[data_size] = subblock_size;
382 hr = IStream_Read(stream, data + data_size + 1, subblock_size, &bytesread);
383 if (FAILED(hr) || bytesread != subblock_size)
385 HeapFree(GetProcessHeap(), 0, data);
386 return S_OK;
388 data_size += subblock_size + 1;
391 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem) * 2);
392 if (!result)
394 HeapFree(GetProcessHeap(), 0, data);
395 return E_OUTOFMEMORY;
398 for (i = 0; i < 2; i++)
400 PropVariantInit(&result[i].schema);
401 PropVariantInit(&result[i].id);
402 PropVariantInit(&result[i].value);
405 result[0].id.vt = VT_LPWSTR;
406 result[0].id.u.pwszVal = strdupAtoW("Application");
407 result[0].value.vt = VT_UI1|VT_VECTOR;
408 result[0].value.u.caub.cElems = sizeof(ape_data.application);
409 result[0].value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, sizeof(ape_data.application));
410 memcpy(result[0].value.u.caub.pElems, ape_data.application, sizeof(ape_data.application));
412 result[1].id.vt = VT_LPWSTR;
413 result[1].id.u.pwszVal = strdupAtoW("Data");
414 result[1].value.vt = VT_UI1|VT_VECTOR;
415 result[1].value.u.caub.cElems = data_size;
416 result[1].value.u.caub.pElems = data;
418 *items = result;
419 *count = 2;
421 return S_OK;
424 static const MetadataHandlerVtbl APEReader_Vtbl = {
426 &CLSID_WICAPEMetadataReader,
427 load_APE_metadata
430 HRESULT APEReader_CreateInstance(REFIID iid, void **ppv)
432 return MetadataReader_Create(&APEReader_Vtbl, iid, ppv);
435 static HRESULT load_GifComment_metadata(IStream *stream, const GUID *vendor, DWORD options,
436 MetadataItem **items, DWORD *count)
438 #include "pshpack1.h"
439 struct gif_extenstion
441 BYTE extension_introducer;
442 BYTE extension_label;
443 } ext_data;
444 #include "poppack.h"
445 HRESULT hr;
446 ULONG bytesread, data_size;
447 MetadataItem *result;
448 BYTE subblock_size;
449 char *data;
451 *items = NULL;
452 *count = 0;
454 hr = IStream_Read(stream, &ext_data, sizeof(ext_data), &bytesread);
455 if (FAILED(hr) || bytesread != sizeof(ext_data)) return S_OK;
456 if (ext_data.extension_introducer != 0x21 ||
457 ext_data.extension_label != COMMENT_EXT_FUNC_CODE)
458 return S_OK;
460 data = NULL;
461 data_size = 0;
463 for (;;)
465 hr = IStream_Read(stream, &subblock_size, sizeof(subblock_size), &bytesread);
466 if (FAILED(hr) || bytesread != sizeof(subblock_size))
468 HeapFree(GetProcessHeap(), 0, data);
469 return S_OK;
471 if (!subblock_size) break;
473 if (!data)
474 data = HeapAlloc(GetProcessHeap(), 0, subblock_size + 1);
475 else
477 char *new_data = HeapReAlloc(GetProcessHeap(), 0, data, data_size + subblock_size + 1);
478 if (!new_data)
480 HeapFree(GetProcessHeap(), 0, data);
481 return S_OK;
483 data = new_data;
485 hr = IStream_Read(stream, data + data_size, subblock_size, &bytesread);
486 if (FAILED(hr) || bytesread != subblock_size)
488 HeapFree(GetProcessHeap(), 0, data);
489 return S_OK;
491 data_size += subblock_size;
494 data[data_size] = 0;
496 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem));
497 if (!result)
499 HeapFree(GetProcessHeap(), 0, data);
500 return E_OUTOFMEMORY;
503 PropVariantInit(&result->schema);
504 PropVariantInit(&result->id);
505 PropVariantInit(&result->value);
507 result->id.vt = VT_LPWSTR;
508 result->id.u.pwszVal = strdupAtoW("TextEntry");
509 result->value.vt = VT_LPSTR;
510 result->value.u.pszVal = data;
512 *items = result;
513 *count = 1;
515 return S_OK;
518 static const MetadataHandlerVtbl GifCommentReader_Vtbl = {
520 &CLSID_WICGifCommentMetadataReader,
521 load_GifComment_metadata
524 HRESULT GifCommentReader_CreateInstance(REFIID iid, void **ppv)
526 return MetadataReader_Create(&GifCommentReader_Vtbl, iid, ppv);
529 static IStream *create_stream(const void *data, int data_size)
531 HRESULT hr;
532 IStream *stream;
533 HGLOBAL hdata;
534 void *locked_data;
536 hdata = GlobalAlloc(GMEM_MOVEABLE, data_size);
537 if (!hdata) return NULL;
539 locked_data = GlobalLock(hdata);
540 memcpy(locked_data, data, data_size);
541 GlobalUnlock(hdata);
543 hr = CreateStreamOnHGlobal(hdata, TRUE, &stream);
544 return FAILED(hr) ? NULL : stream;
547 static HRESULT create_metadata_reader(const void *data, int data_size,
548 class_constructor constructor,
549 IWICMetadataReader **reader)
551 HRESULT hr;
552 IWICMetadataReader *metadata_reader;
553 IWICPersistStream *persist;
554 IStream *stream;
556 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
558 hr = constructor(&IID_IWICMetadataReader, (void**)&metadata_reader);
559 if (FAILED(hr)) return hr;
561 hr = IWICMetadataReader_QueryInterface(metadata_reader, &IID_IWICPersistStream, (void **)&persist);
562 if (FAILED(hr))
564 IWICMetadataReader_Release(metadata_reader);
565 return hr;
568 stream = create_stream(data, data_size);
569 IWICPersistStream_LoadEx(persist, stream, NULL, WICPersistOptionsDefault);
570 IStream_Release(stream);
572 IWICPersistStream_Release(persist);
574 *reader = metadata_reader;
575 return S_OK;
578 typedef struct {
579 IWICBitmapDecoder IWICBitmapDecoder_iface;
580 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
581 IStream *stream;
582 BYTE LSD_data[13]; /* Logical Screen Descriptor */
583 LONG ref;
584 BOOL initialized;
585 GifFileType *gif;
586 UINT current_frame;
587 CRITICAL_SECTION lock;
588 } GifDecoder;
590 typedef struct {
591 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
592 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
593 LONG ref;
594 SavedImage *frame;
595 GifDecoder *parent;
596 } GifFrameDecode;
598 static inline GifDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
600 return CONTAINING_RECORD(iface, GifDecoder, IWICBitmapDecoder_iface);
603 static inline GifDecoder *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
605 return CONTAINING_RECORD(iface, GifDecoder, IWICMetadataBlockReader_iface);
608 static inline GifFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
610 return CONTAINING_RECORD(iface, GifFrameDecode, IWICBitmapFrameDecode_iface);
613 static inline GifFrameDecode *frame_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
615 return CONTAINING_RECORD(iface, GifFrameDecode, IWICMetadataBlockReader_iface);
618 static HRESULT WINAPI GifFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
619 void **ppv)
621 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
622 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
624 if (!ppv) return E_INVALIDARG;
626 if (IsEqualIID(&IID_IUnknown, iid) ||
627 IsEqualIID(&IID_IWICBitmapSource, iid) ||
628 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
630 *ppv = &This->IWICBitmapFrameDecode_iface;
632 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
634 *ppv = &This->IWICMetadataBlockReader_iface;
636 else
638 *ppv = NULL;
639 return E_NOINTERFACE;
642 IUnknown_AddRef((IUnknown*)*ppv);
643 return S_OK;
646 static ULONG WINAPI GifFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
648 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
649 ULONG ref = InterlockedIncrement(&This->ref);
651 TRACE("(%p) refcount=%u\n", iface, ref);
653 return ref;
656 static ULONG WINAPI GifFrameDecode_Release(IWICBitmapFrameDecode *iface)
658 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
659 ULONG ref = InterlockedDecrement(&This->ref);
661 TRACE("(%p) refcount=%u\n", iface, ref);
663 if (ref == 0)
665 IWICBitmapDecoder_Release(&This->parent->IWICBitmapDecoder_iface);
666 HeapFree(GetProcessHeap(), 0, This);
669 return ref;
672 static HRESULT WINAPI GifFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
673 UINT *puiWidth, UINT *puiHeight)
675 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
676 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
678 *puiWidth = This->frame->ImageDesc.Width;
679 *puiHeight = This->frame->ImageDesc.Height;
681 return S_OK;
684 static HRESULT WINAPI GifFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
685 WICPixelFormatGUID *pPixelFormat)
687 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppIndexed, sizeof(GUID));
689 return S_OK;
692 static HRESULT WINAPI GifFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
693 double *pDpiX, double *pDpiY)
695 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
696 const GifWord aspect_word = This->parent->gif->SAspectRatio;
697 const double aspect = (aspect_word > 0) ? ((aspect_word + 15.0) / 64.0) : 1.0;
698 TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
700 *pDpiX = 96.0 / aspect;
701 *pDpiY = 96.0;
703 return S_OK;
706 static HRESULT WINAPI GifFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
707 IWICPalette *pIPalette)
709 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
710 WICColor colors[256];
711 ColorMapObject *cm = This->frame->ImageDesc.ColorMap;
712 int i, trans;
713 ExtensionBlock *eb;
714 TRACE("(%p,%p)\n", iface, pIPalette);
716 if (!cm) cm = This->parent->gif->SColorMap;
718 if (cm->ColorCount > 256)
720 ERR("GIF contains %i colors???\n", cm->ColorCount);
721 return E_FAIL;
724 for (i = 0; i < cm->ColorCount; i++) {
725 colors[i] = 0xff000000| /* alpha */
726 cm->Colors[i].Red << 16|
727 cm->Colors[i].Green << 8|
728 cm->Colors[i].Blue;
731 /* look for the transparent color extension */
732 for (i = 0; i < This->frame->Extensions.ExtensionBlockCount; ++i) {
733 eb = This->frame->Extensions.ExtensionBlocks + i;
734 if (eb->Function == GRAPHICS_EXT_FUNC_CODE && eb->ByteCount == 8) {
735 if (eb->Bytes[3] & 1) {
736 trans = (unsigned char)eb->Bytes[6];
737 colors[trans] &= 0xffffff; /* set alpha to 0 */
738 break;
743 return IWICPalette_InitializeCustom(pIPalette, colors, cm->ColorCount);
746 static HRESULT copy_interlaced_pixels(const BYTE *srcbuffer,
747 UINT srcwidth, UINT srcheight, INT srcstride, const WICRect *rc,
748 UINT dststride, UINT dstbuffersize, BYTE *dstbuffer)
750 UINT row_offset; /* number of bytes into the source rows where the data starts */
751 const BYTE *src;
752 BYTE *dst;
753 UINT y;
754 WICRect rect;
756 if (!rc)
758 rect.X = 0;
759 rect.Y = 0;
760 rect.Width = srcwidth;
761 rect.Height = srcheight;
762 rc = &rect;
764 else
766 if (rc->X < 0 || rc->Y < 0 || rc->X+rc->Width > srcwidth || rc->Y+rc->Height > srcheight)
767 return E_INVALIDARG;
770 if (dststride < rc->Width)
771 return E_INVALIDARG;
773 if ((dststride * rc->Height) > dstbuffersize)
774 return E_INVALIDARG;
776 row_offset = rc->X;
778 dst = dstbuffer;
779 for (y=rc->Y; y-rc->Y < rc->Height; y++)
781 if (y%8 == 0)
782 src = srcbuffer + srcstride * (y/8);
783 else if (y%4 == 0)
784 src = srcbuffer + srcstride * ((srcheight+7)/8 + y/8);
785 else if (y%2 == 0)
786 src = srcbuffer + srcstride * ((srcheight+3)/4 + y/4);
787 else /* y%2 == 1 */
788 src = srcbuffer + srcstride * ((srcheight+1)/2 + y/2);
789 src += row_offset;
790 memcpy(dst, src, rc->Width);
791 dst += dststride;
793 return S_OK;
796 static HRESULT WINAPI GifFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
797 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
799 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
800 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
802 if (This->frame->ImageDesc.Interlace)
804 return copy_interlaced_pixels(This->frame->RasterBits, This->frame->ImageDesc.Width,
805 This->frame->ImageDesc.Height, This->frame->ImageDesc.Width,
806 prc, cbStride, cbBufferSize, pbBuffer);
808 else
810 return copy_pixels(8, This->frame->RasterBits, This->frame->ImageDesc.Width,
811 This->frame->ImageDesc.Height, This->frame->ImageDesc.Width,
812 prc, cbStride, cbBufferSize, pbBuffer);
816 static HRESULT WINAPI GifFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
817 IWICMetadataQueryReader **ppIMetadataQueryReader)
819 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
820 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
823 static HRESULT WINAPI GifFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
824 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
826 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
827 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
830 static HRESULT WINAPI GifFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
831 IWICBitmapSource **ppIThumbnail)
833 TRACE("(%p,%p)\n", iface, ppIThumbnail);
834 return WINCODEC_ERR_CODECNOTHUMBNAIL;
837 static const IWICBitmapFrameDecodeVtbl GifFrameDecode_Vtbl = {
838 GifFrameDecode_QueryInterface,
839 GifFrameDecode_AddRef,
840 GifFrameDecode_Release,
841 GifFrameDecode_GetSize,
842 GifFrameDecode_GetPixelFormat,
843 GifFrameDecode_GetResolution,
844 GifFrameDecode_CopyPalette,
845 GifFrameDecode_CopyPixels,
846 GifFrameDecode_GetMetadataQueryReader,
847 GifFrameDecode_GetColorContexts,
848 GifFrameDecode_GetThumbnail
851 static HRESULT WINAPI GifFrameDecode_Block_QueryInterface(IWICMetadataBlockReader *iface,
852 REFIID iid, void **ppv)
854 GifFrameDecode *This = frame_from_IWICMetadataBlockReader(iface);
855 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
858 static ULONG WINAPI GifFrameDecode_Block_AddRef(IWICMetadataBlockReader *iface)
860 GifFrameDecode *This = frame_from_IWICMetadataBlockReader(iface);
861 return IWICBitmapFrameDecode_AddRef(&This->IWICBitmapFrameDecode_iface);
864 static ULONG WINAPI GifFrameDecode_Block_Release(IWICMetadataBlockReader *iface)
866 GifFrameDecode *This = frame_from_IWICMetadataBlockReader(iface);
867 return IWICBitmapFrameDecode_Release(&This->IWICBitmapFrameDecode_iface);
870 static HRESULT WINAPI GifFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
871 GUID *guid)
873 TRACE("(%p,%p)\n", iface, guid);
875 if (!guid) return E_INVALIDARG;
877 *guid = GUID_ContainerFormatGif;
878 return S_OK;
881 static HRESULT WINAPI GifFrameDecode_Block_GetCount(IWICMetadataBlockReader *iface,
882 UINT *count)
884 GifFrameDecode *This = frame_from_IWICMetadataBlockReader(iface);
886 TRACE("%p,%p\n", iface, count);
888 if (!count) return E_INVALIDARG;
890 *count = This->frame->Extensions.ExtensionBlockCount + 1;
891 return S_OK;
894 static HRESULT create_IMD_metadata_reader(GifFrameDecode *This, IWICMetadataReader **reader)
896 HRESULT hr;
897 IWICMetadataReader *metadata_reader;
898 IWICPersistStream *persist;
899 IStream *stream;
900 struct image_descriptor IMD_data;
902 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
904 hr = IMDReader_CreateInstance(&IID_IWICMetadataReader, (void **)&metadata_reader);
905 if (FAILED(hr)) return hr;
907 hr = IWICMetadataReader_QueryInterface(metadata_reader, &IID_IWICPersistStream, (void **)&persist);
908 if (FAILED(hr))
910 IWICMetadataReader_Release(metadata_reader);
911 return hr;
914 /* recreate IMD structure from GIF decoder data */
915 IMD_data.left = This->frame->ImageDesc.Left;
916 IMD_data.top = This->frame->ImageDesc.Top;
917 IMD_data.width = This->frame->ImageDesc.Width;
918 IMD_data.height = This->frame->ImageDesc.Height;
919 IMD_data.packed = 0;
920 /* interlace_flag */
921 IMD_data.packed |= This->frame->ImageDesc.Interlace ? (1 << 6) : 0;
922 if (This->frame->ImageDesc.ColorMap)
924 /* local_color_table_flag */
925 IMD_data.packed |= 1 << 7;
926 /* local_color_table_size */
927 IMD_data.packed |= This->frame->ImageDesc.ColorMap->BitsPerPixel - 1;
928 /* sort_flag */
929 IMD_data.packed |= This->frame->ImageDesc.ColorMap->SortFlag ? 0x20 : 0;
932 stream = create_stream(&IMD_data, sizeof(IMD_data));
933 IWICPersistStream_LoadEx(persist, stream, NULL, WICPersistOptionsDefault);
934 IStream_Release(stream);
936 IWICPersistStream_Release(persist);
938 *reader = metadata_reader;
939 return S_OK;
942 static HRESULT WINAPI GifFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
943 UINT index, IWICMetadataReader **reader)
945 GifFrameDecode *This = frame_from_IWICMetadataBlockReader(iface);
946 int i, gce_index = -1, gce_skipped = 0;
948 TRACE("(%p,%u,%p)\n", iface, index, reader);
950 if (!reader) return E_INVALIDARG;
952 if (index == 0)
953 return create_IMD_metadata_reader(This, reader);
955 if (index >= This->frame->Extensions.ExtensionBlockCount + 1)
956 return E_INVALIDARG;
958 for (i = 0; i < This->frame->Extensions.ExtensionBlockCount; i++)
960 class_constructor constructor;
961 const void *data;
962 int data_size;
964 if (index != i + 1 - gce_skipped) continue;
966 if (This->frame->Extensions.ExtensionBlocks[i].Function == GRAPHICS_EXT_FUNC_CODE)
968 gce_index = i;
969 gce_skipped = 1;
970 continue;
972 else if (This->frame->Extensions.ExtensionBlocks[i].Function == COMMENT_EXT_FUNC_CODE)
974 constructor = GifCommentReader_CreateInstance;
975 data = This->frame->Extensions.ExtensionBlocks[i].Bytes;
976 data_size = This->frame->Extensions.ExtensionBlocks[i].ByteCount;
978 else
980 constructor = UnknownMetadataReader_CreateInstance;
981 data = This->frame->Extensions.ExtensionBlocks[i].Bytes;
982 data_size = This->frame->Extensions.ExtensionBlocks[i].ByteCount;
984 return create_metadata_reader(data, data_size, constructor, reader);
987 if (gce_index == -1) return E_INVALIDARG;
989 return create_metadata_reader(This->frame->Extensions.ExtensionBlocks[gce_index].Bytes + 3,
990 This->frame->Extensions.ExtensionBlocks[gce_index].ByteCount - 4,
991 GCEReader_CreateInstance, reader);
994 static HRESULT WINAPI GifFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader *iface,
995 IEnumUnknown **enumerator)
997 FIXME("(%p,%p): stub\n", iface, enumerator);
998 return E_NOTIMPL;
1001 static const IWICMetadataBlockReaderVtbl GifFrameDecode_BlockVtbl =
1003 GifFrameDecode_Block_QueryInterface,
1004 GifFrameDecode_Block_AddRef,
1005 GifFrameDecode_Block_Release,
1006 GifFrameDecode_Block_GetContainerFormat,
1007 GifFrameDecode_Block_GetCount,
1008 GifFrameDecode_Block_GetReaderByIndex,
1009 GifFrameDecode_Block_GetEnumerator
1012 static HRESULT WINAPI GifDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
1013 void **ppv)
1015 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1016 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1018 if (!ppv) return E_INVALIDARG;
1020 if (IsEqualIID(&IID_IUnknown, iid) ||
1021 IsEqualIID(&IID_IWICBitmapDecoder, iid))
1023 *ppv = &This->IWICBitmapDecoder_iface;
1025 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
1027 *ppv = &This->IWICMetadataBlockReader_iface;
1029 else
1031 *ppv = NULL;
1032 return E_NOINTERFACE;
1035 IUnknown_AddRef((IUnknown*)*ppv);
1036 return S_OK;
1039 static ULONG WINAPI GifDecoder_AddRef(IWICBitmapDecoder *iface)
1041 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1042 ULONG ref = InterlockedIncrement(&This->ref);
1044 TRACE("(%p) refcount=%u\n", iface, ref);
1046 return ref;
1049 static ULONG WINAPI GifDecoder_Release(IWICBitmapDecoder *iface)
1051 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1052 ULONG ref = InterlockedDecrement(&This->ref);
1054 TRACE("(%p) refcount=%u\n", iface, ref);
1056 if (ref == 0)
1058 IStream_Release(This->stream);
1059 This->lock.DebugInfo->Spare[0] = 0;
1060 DeleteCriticalSection(&This->lock);
1061 DGifCloseFile(This->gif);
1062 HeapFree(GetProcessHeap(), 0, This);
1065 return ref;
1068 static HRESULT WINAPI GifDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
1069 DWORD *capability)
1071 HRESULT hr;
1073 TRACE("(%p,%p,%p)\n", iface, stream, capability);
1075 if (!stream || !capability) return E_INVALIDARG;
1077 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
1078 if (hr != S_OK) return hr;
1080 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
1081 WICBitmapDecoderCapabilityCanDecodeSomeImages |
1082 WICBitmapDecoderCapabilityCanEnumerateMetadata;
1083 return S_OK;
1086 static int _gif_inputfunc(GifFileType *gif, GifByteType *data, int len) {
1087 IStream *stream = gif->UserData;
1088 ULONG bytesread;
1089 HRESULT hr;
1091 if (!stream)
1093 ERR("attempting to read file after initialization\n");
1094 return 0;
1097 hr = IStream_Read(stream, data, len, &bytesread);
1098 if (FAILED(hr)) bytesread = 0;
1099 return bytesread;
1102 static HRESULT WINAPI GifDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
1103 WICDecodeOptions cacheOptions)
1105 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1106 LARGE_INTEGER seek;
1107 int ret;
1109 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
1111 EnterCriticalSection(&This->lock);
1113 if (This->initialized || This->gif)
1115 WARN("already initialized\n");
1116 LeaveCriticalSection(&This->lock);
1117 return WINCODEC_ERR_WRONGSTATE;
1120 /* seek to start of stream */
1121 seek.QuadPart = 0;
1122 IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
1124 /* read all data from the stream */
1125 This->gif = DGifOpen((void*)pIStream, _gif_inputfunc);
1126 if (!This->gif)
1128 LeaveCriticalSection(&This->lock);
1129 return E_FAIL;
1132 ret = DGifSlurp(This->gif);
1133 if (ret == GIF_ERROR)
1135 LeaveCriticalSection(&This->lock);
1136 return E_FAIL;
1139 /* make sure we don't use the stream after this method returns */
1140 This->gif->UserData = NULL;
1142 seek.QuadPart = 0;
1143 IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
1144 IStream_Read(pIStream, This->LSD_data, sizeof(This->LSD_data), NULL);
1146 This->stream = pIStream;
1147 IStream_AddRef(This->stream);
1149 This->initialized = TRUE;
1151 LeaveCriticalSection(&This->lock);
1153 return S_OK;
1156 static HRESULT WINAPI GifDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
1157 GUID *pguidContainerFormat)
1159 memcpy(pguidContainerFormat, &GUID_ContainerFormatGif, sizeof(GUID));
1160 return S_OK;
1163 static HRESULT WINAPI GifDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
1164 IWICBitmapDecoderInfo **ppIDecoderInfo)
1166 HRESULT hr;
1167 IWICComponentInfo *compinfo;
1169 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
1171 hr = CreateComponentInfo(&CLSID_WICGifDecoder, &compinfo);
1172 if (FAILED(hr)) return hr;
1174 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
1175 (void**)ppIDecoderInfo);
1177 IWICComponentInfo_Release(compinfo);
1179 return hr;
1182 static HRESULT WINAPI GifDecoder_CopyPalette(IWICBitmapDecoder *iface, IWICPalette *palette)
1184 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1185 WICColor colors[256];
1186 ColorMapObject *cm;
1187 int i, trans, count;
1188 ExtensionBlock *eb;
1190 TRACE("(%p,%p)\n", iface, palette);
1192 cm = This->gif->SColorMap;
1193 if (cm)
1195 if (cm->ColorCount > 256)
1197 ERR("GIF contains invalid number of colors: %d\n", cm->ColorCount);
1198 return E_FAIL;
1201 for (i = 0; i < cm->ColorCount; i++)
1203 colors[i] = 0xff000000 | /* alpha */
1204 cm->Colors[i].Red << 16 |
1205 cm->Colors[i].Green << 8 |
1206 cm->Colors[i].Blue;
1209 count = cm->ColorCount;
1211 else
1213 colors[0] = 0xff000000;
1214 colors[1] = 0xffffffff;
1216 for (i = 2; i < 256; i++)
1217 colors[i] = 0xff000000;
1219 count = 256;
1222 /* look for the transparent color extension */
1223 for (i = 0; i < This->gif->SavedImages[This->current_frame].Extensions.ExtensionBlockCount; i++)
1225 eb = This->gif->SavedImages[This->current_frame].Extensions.ExtensionBlocks + i;
1226 if (eb->Function == GRAPHICS_EXT_FUNC_CODE && eb->ByteCount == 8)
1228 if (eb->Bytes[3] & 1)
1230 trans = (unsigned char)eb->Bytes[6];
1231 colors[trans] &= 0xffffff; /* set alpha to 0 */
1232 break;
1237 return IWICPalette_InitializeCustom(palette, colors, count);
1240 static HRESULT WINAPI GifDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
1241 IWICMetadataQueryReader **ppIMetadataQueryReader)
1243 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
1244 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1247 static HRESULT WINAPI GifDecoder_GetPreview(IWICBitmapDecoder *iface,
1248 IWICBitmapSource **ppIBitmapSource)
1250 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
1251 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1254 static HRESULT WINAPI GifDecoder_GetColorContexts(IWICBitmapDecoder *iface,
1255 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1257 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
1258 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1261 static HRESULT WINAPI GifDecoder_GetThumbnail(IWICBitmapDecoder *iface,
1262 IWICBitmapSource **ppIThumbnail)
1264 TRACE("(%p,%p)\n", iface, ppIThumbnail);
1265 return WINCODEC_ERR_CODECNOTHUMBNAIL;
1268 static HRESULT WINAPI GifDecoder_GetFrameCount(IWICBitmapDecoder *iface,
1269 UINT *pCount)
1271 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1273 if (!pCount) return E_INVALIDARG;
1275 EnterCriticalSection(&This->lock);
1276 *pCount = This->gif ? This->gif->ImageCount : 0;
1277 LeaveCriticalSection(&This->lock);
1279 TRACE("(%p) <-- %d\n", iface, *pCount);
1281 return S_OK;
1284 static HRESULT WINAPI GifDecoder_GetFrame(IWICBitmapDecoder *iface,
1285 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
1287 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1288 GifFrameDecode *result;
1289 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
1291 if (!This->initialized) return WINCODEC_ERR_FRAMEMISSING;
1293 if (index >= This->gif->ImageCount) return E_INVALIDARG;
1295 result = HeapAlloc(GetProcessHeap(), 0, sizeof(GifFrameDecode));
1296 if (!result) return E_OUTOFMEMORY;
1298 result->IWICBitmapFrameDecode_iface.lpVtbl = &GifFrameDecode_Vtbl;
1299 result->IWICMetadataBlockReader_iface.lpVtbl = &GifFrameDecode_BlockVtbl;
1300 result->ref = 1;
1301 result->frame = &This->gif->SavedImages[index];
1302 IWICBitmapDecoder_AddRef(iface);
1303 result->parent = This;
1304 This->current_frame = index;
1306 *ppIBitmapFrame = &result->IWICBitmapFrameDecode_iface;
1308 return S_OK;
1311 static const IWICBitmapDecoderVtbl GifDecoder_Vtbl = {
1312 GifDecoder_QueryInterface,
1313 GifDecoder_AddRef,
1314 GifDecoder_Release,
1315 GifDecoder_QueryCapability,
1316 GifDecoder_Initialize,
1317 GifDecoder_GetContainerFormat,
1318 GifDecoder_GetDecoderInfo,
1319 GifDecoder_CopyPalette,
1320 GifDecoder_GetMetadataQueryReader,
1321 GifDecoder_GetPreview,
1322 GifDecoder_GetColorContexts,
1323 GifDecoder_GetThumbnail,
1324 GifDecoder_GetFrameCount,
1325 GifDecoder_GetFrame
1328 static HRESULT WINAPI GifDecoder_Block_QueryInterface(IWICMetadataBlockReader *iface,
1329 REFIID iid, void **ppv)
1331 GifDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1332 return IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1335 static ULONG WINAPI GifDecoder_Block_AddRef(IWICMetadataBlockReader *iface)
1337 GifDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1338 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
1341 static ULONG WINAPI GifDecoder_Block_Release(IWICMetadataBlockReader *iface)
1343 GifDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1344 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1347 static HRESULT WINAPI GifDecoder_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
1348 GUID *guid)
1350 TRACE("(%p,%p)\n", iface, guid);
1352 if (!guid) return E_INVALIDARG;
1354 *guid = GUID_ContainerFormatGif;
1355 return S_OK;
1358 static HRESULT WINAPI GifDecoder_Block_GetCount(IWICMetadataBlockReader *iface,
1359 UINT *count)
1361 GifDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1363 TRACE("%p,%p\n", iface, count);
1365 if (!count) return E_INVALIDARG;
1367 *count = This->gif->Extensions.ExtensionBlockCount + 1;
1368 return S_OK;
1371 static HRESULT WINAPI GifDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
1372 UINT index, IWICMetadataReader **reader)
1374 GifDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1375 int i;
1377 TRACE("(%p,%u,%p)\n", iface, index, reader);
1379 if (!reader) return E_INVALIDARG;
1381 if (index == 0)
1382 return create_metadata_reader(This->LSD_data, sizeof(This->LSD_data),
1383 LSDReader_CreateInstance, reader);
1385 for (i = 0; i < This->gif->Extensions.ExtensionBlockCount; i++)
1387 class_constructor constructor;
1389 if (index != i + 1) continue;
1391 if (This->gif->Extensions.ExtensionBlocks[i].Function == APPLICATION_EXT_FUNC_CODE)
1392 constructor = APEReader_CreateInstance;
1393 else if (This->gif->Extensions.ExtensionBlocks[i].Function == COMMENT_EXT_FUNC_CODE)
1394 constructor = GifCommentReader_CreateInstance;
1395 else
1396 constructor = UnknownMetadataReader_CreateInstance;
1398 return create_metadata_reader(This->gif->Extensions.ExtensionBlocks[i].Bytes,
1399 This->gif->Extensions.ExtensionBlocks[i].ByteCount,
1400 constructor, reader);
1403 return E_INVALIDARG;
1406 static HRESULT WINAPI GifDecoder_Block_GetEnumerator(IWICMetadataBlockReader *iface,
1407 IEnumUnknown **enumerator)
1409 FIXME("(%p,%p): stub\n", iface, enumerator);
1410 return E_NOTIMPL;
1413 static const IWICMetadataBlockReaderVtbl GifDecoder_BlockVtbl =
1415 GifDecoder_Block_QueryInterface,
1416 GifDecoder_Block_AddRef,
1417 GifDecoder_Block_Release,
1418 GifDecoder_Block_GetContainerFormat,
1419 GifDecoder_Block_GetCount,
1420 GifDecoder_Block_GetReaderByIndex,
1421 GifDecoder_Block_GetEnumerator
1424 HRESULT GifDecoder_CreateInstance(REFIID iid, void** ppv)
1426 GifDecoder *This;
1427 HRESULT ret;
1429 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1431 *ppv = NULL;
1433 This = HeapAlloc(GetProcessHeap(), 0, sizeof(GifDecoder));
1434 if (!This) return E_OUTOFMEMORY;
1436 This->IWICBitmapDecoder_iface.lpVtbl = &GifDecoder_Vtbl;
1437 This->IWICMetadataBlockReader_iface.lpVtbl = &GifDecoder_BlockVtbl;
1438 This->ref = 1;
1439 This->initialized = FALSE;
1440 This->gif = NULL;
1441 This->current_frame = 0;
1442 InitializeCriticalSection(&This->lock);
1443 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": GifDecoder.lock");
1445 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1446 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1448 return ret;