mfreadwrite/reader: Enable DXGI device manager path.
[wine/zf.git] / dlls / windowscodecs / gifformat.c
blob53aeaec7e8d39a3c874a5a3fa258ef5c57fbfa86
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2012,2016 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 <stdarg.h>
22 #define COBJMACROS
23 #define NONAMELESSUNION
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
29 #include "ungif.h"
31 #include "wincodecs_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
37 #include "pshpack1.h"
39 struct logical_screen_descriptor
41 char signature[6];
42 USHORT width;
43 USHORT height;
44 BYTE packed;
45 /* global_color_table_flag : 1;
46 * color_resolution : 3;
47 * sort_flag : 1;
48 * global_color_table_size : 3;
50 BYTE background_color_index;
51 BYTE pixel_aspect_ratio;
54 struct image_descriptor
56 USHORT left;
57 USHORT top;
58 USHORT width;
59 USHORT height;
60 BYTE packed;
61 /* local_color_table_flag : 1;
62 * interlace_flag : 1;
63 * sort_flag : 1;
64 * reserved : 2;
65 * local_color_table_size : 3;
69 #include "poppack.h"
71 static LPWSTR strdupAtoW(const char *src)
73 int len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
74 LPWSTR dst = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
75 if (dst) MultiByteToWideChar(CP_ACP, 0, src, -1, dst, len);
76 return dst;
79 static HRESULT load_LSD_metadata(IStream *stream, const GUID *vendor, DWORD options,
80 MetadataItem **items, DWORD *count)
82 struct logical_screen_descriptor lsd_data;
83 HRESULT hr;
84 ULONG bytesread, i;
85 MetadataItem *result;
87 *items = NULL;
88 *count = 0;
90 hr = IStream_Read(stream, &lsd_data, sizeof(lsd_data), &bytesread);
91 if (FAILED(hr) || bytesread != sizeof(lsd_data)) return S_OK;
93 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem) * 9);
94 if (!result) return E_OUTOFMEMORY;
96 for (i = 0; i < 9; i++)
98 PropVariantInit(&result[i].schema);
99 PropVariantInit(&result[i].id);
100 PropVariantInit(&result[i].value);
103 result[0].id.vt = VT_LPWSTR;
104 result[0].id.u.pwszVal = strdupAtoW("Signature");
105 result[0].value.vt = VT_UI1|VT_VECTOR;
106 result[0].value.u.caub.cElems = sizeof(lsd_data.signature);
107 result[0].value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, sizeof(lsd_data.signature));
108 memcpy(result[0].value.u.caub.pElems, lsd_data.signature, sizeof(lsd_data.signature));
110 result[1].id.vt = VT_LPWSTR;
111 result[1].id.u.pwszVal = strdupAtoW("Width");
112 result[1].value.vt = VT_UI2;
113 result[1].value.u.uiVal = lsd_data.width;
115 result[2].id.vt = VT_LPWSTR;
116 result[2].id.u.pwszVal = strdupAtoW("Height");
117 result[2].value.vt = VT_UI2;
118 result[2].value.u.uiVal = lsd_data.height;
120 result[3].id.vt = VT_LPWSTR;
121 result[3].id.u.pwszVal = strdupAtoW("GlobalColorTableFlag");
122 result[3].value.vt = VT_BOOL;
123 result[3].value.u.boolVal = (lsd_data.packed >> 7) & 1;
125 result[4].id.vt = VT_LPWSTR;
126 result[4].id.u.pwszVal = strdupAtoW("ColorResolution");
127 result[4].value.vt = VT_UI1;
128 result[4].value.u.bVal = (lsd_data.packed >> 4) & 7;
130 result[5].id.vt = VT_LPWSTR;
131 result[5].id.u.pwszVal = strdupAtoW("SortFlag");
132 result[5].value.vt = VT_BOOL;
133 result[5].value.u.boolVal = (lsd_data.packed >> 3) & 1;
135 result[6].id.vt = VT_LPWSTR;
136 result[6].id.u.pwszVal = strdupAtoW("GlobalColorTableSize");
137 result[6].value.vt = VT_UI1;
138 result[6].value.u.bVal = lsd_data.packed & 7;
140 result[7].id.vt = VT_LPWSTR;
141 result[7].id.u.pwszVal = strdupAtoW("BackgroundColorIndex");
142 result[7].value.vt = VT_UI1;
143 result[7].value.u.bVal = lsd_data.background_color_index;
145 result[8].id.vt = VT_LPWSTR;
146 result[8].id.u.pwszVal = strdupAtoW("PixelAspectRatio");
147 result[8].value.vt = VT_UI1;
148 result[8].value.u.bVal = lsd_data.pixel_aspect_ratio;
150 *items = result;
151 *count = 9;
153 return S_OK;
156 static const MetadataHandlerVtbl LSDReader_Vtbl = {
158 &CLSID_WICLSDMetadataReader,
159 load_LSD_metadata
162 HRESULT LSDReader_CreateInstance(REFIID iid, void **ppv)
164 return MetadataReader_Create(&LSDReader_Vtbl, iid, ppv);
167 static HRESULT load_IMD_metadata(IStream *stream, const GUID *vendor, DWORD options,
168 MetadataItem **items, DWORD *count)
170 struct image_descriptor imd_data;
171 HRESULT hr;
172 ULONG bytesread, i;
173 MetadataItem *result;
175 *items = NULL;
176 *count = 0;
178 hr = IStream_Read(stream, &imd_data, sizeof(imd_data), &bytesread);
179 if (FAILED(hr) || bytesread != sizeof(imd_data)) return S_OK;
181 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem) * 8);
182 if (!result) return E_OUTOFMEMORY;
184 for (i = 0; i < 8; i++)
186 PropVariantInit(&result[i].schema);
187 PropVariantInit(&result[i].id);
188 PropVariantInit(&result[i].value);
191 result[0].id.vt = VT_LPWSTR;
192 result[0].id.u.pwszVal = strdupAtoW("Left");
193 result[0].value.vt = VT_UI2;
194 result[0].value.u.uiVal = imd_data.left;
196 result[1].id.vt = VT_LPWSTR;
197 result[1].id.u.pwszVal = strdupAtoW("Top");
198 result[1].value.vt = VT_UI2;
199 result[1].value.u.uiVal = imd_data.top;
201 result[2].id.vt = VT_LPWSTR;
202 result[2].id.u.pwszVal = strdupAtoW("Width");
203 result[2].value.vt = VT_UI2;
204 result[2].value.u.uiVal = imd_data.width;
206 result[3].id.vt = VT_LPWSTR;
207 result[3].id.u.pwszVal = strdupAtoW("Height");
208 result[3].value.vt = VT_UI2;
209 result[3].value.u.uiVal = imd_data.height;
211 result[4].id.vt = VT_LPWSTR;
212 result[4].id.u.pwszVal = strdupAtoW("LocalColorTableFlag");
213 result[4].value.vt = VT_BOOL;
214 result[4].value.u.boolVal = (imd_data.packed >> 7) & 1;
216 result[5].id.vt = VT_LPWSTR;
217 result[5].id.u.pwszVal = strdupAtoW("InterlaceFlag");
218 result[5].value.vt = VT_BOOL;
219 result[5].value.u.boolVal = (imd_data.packed >> 6) & 1;
221 result[6].id.vt = VT_LPWSTR;
222 result[6].id.u.pwszVal = strdupAtoW("SortFlag");
223 result[6].value.vt = VT_BOOL;
224 result[6].value.u.boolVal = (imd_data.packed >> 5) & 1;
226 result[7].id.vt = VT_LPWSTR;
227 result[7].id.u.pwszVal = strdupAtoW("LocalColorTableSize");
228 result[7].value.vt = VT_UI1;
229 result[7].value.u.bVal = imd_data.packed & 7;
231 *items = result;
232 *count = 8;
234 return S_OK;
237 static const MetadataHandlerVtbl IMDReader_Vtbl = {
239 &CLSID_WICIMDMetadataReader,
240 load_IMD_metadata
243 HRESULT IMDReader_CreateInstance(REFIID iid, void **ppv)
245 return MetadataReader_Create(&IMDReader_Vtbl, iid, ppv);
248 static HRESULT load_GCE_metadata(IStream *stream, const GUID *vendor, DWORD options,
249 MetadataItem **items, DWORD *count)
251 #include "pshpack1.h"
252 struct graphic_control_extension
254 BYTE packed;
255 /* reservred: 3;
256 * disposal : 3;
257 * user_input_flag : 1;
258 * transparency_flag : 1;
260 USHORT delay;
261 BYTE transparent_color_index;
262 } gce_data;
263 #include "poppack.h"
264 HRESULT hr;
265 ULONG bytesread, i;
266 MetadataItem *result;
268 *items = NULL;
269 *count = 0;
271 hr = IStream_Read(stream, &gce_data, sizeof(gce_data), &bytesread);
272 if (FAILED(hr) || bytesread != sizeof(gce_data)) return S_OK;
274 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem) * 5);
275 if (!result) return E_OUTOFMEMORY;
277 for (i = 0; i < 5; i++)
279 PropVariantInit(&result[i].schema);
280 PropVariantInit(&result[i].id);
281 PropVariantInit(&result[i].value);
284 result[0].id.vt = VT_LPWSTR;
285 result[0].id.u.pwszVal = strdupAtoW("Disposal");
286 result[0].value.vt = VT_UI1;
287 result[0].value.u.bVal = (gce_data.packed >> 2) & 7;
289 result[1].id.vt = VT_LPWSTR;
290 result[1].id.u.pwszVal = strdupAtoW("UserInputFlag");
291 result[1].value.vt = VT_BOOL;
292 result[1].value.u.boolVal = (gce_data.packed >> 1) & 1;
294 result[2].id.vt = VT_LPWSTR;
295 result[2].id.u.pwszVal = strdupAtoW("TransparencyFlag");
296 result[2].value.vt = VT_BOOL;
297 result[2].value.u.boolVal = gce_data.packed & 1;
299 result[3].id.vt = VT_LPWSTR;
300 result[3].id.u.pwszVal = strdupAtoW("Delay");
301 result[3].value.vt = VT_UI2;
302 result[3].value.u.uiVal = gce_data.delay;
304 result[4].id.vt = VT_LPWSTR;
305 result[4].id.u.pwszVal = strdupAtoW("TransparentColorIndex");
306 result[4].value.vt = VT_UI1;
307 result[4].value.u.bVal = gce_data.transparent_color_index;
309 *items = result;
310 *count = 5;
312 return S_OK;
315 static const MetadataHandlerVtbl GCEReader_Vtbl = {
317 &CLSID_WICGCEMetadataReader,
318 load_GCE_metadata
321 HRESULT GCEReader_CreateInstance(REFIID iid, void **ppv)
323 return MetadataReader_Create(&GCEReader_Vtbl, iid, ppv);
326 static HRESULT load_APE_metadata(IStream *stream, const GUID *vendor, DWORD options,
327 MetadataItem **items, DWORD *count)
329 #include "pshpack1.h"
330 struct application_extension
332 BYTE extension_introducer;
333 BYTE extension_label;
334 BYTE block_size;
335 BYTE application[11];
336 } ape_data;
337 #include "poppack.h"
338 HRESULT hr;
339 ULONG bytesread, data_size, i;
340 MetadataItem *result;
341 BYTE subblock_size;
342 BYTE *data;
344 *items = NULL;
345 *count = 0;
347 hr = IStream_Read(stream, &ape_data, sizeof(ape_data), &bytesread);
348 if (FAILED(hr) || bytesread != sizeof(ape_data)) return S_OK;
349 if (ape_data.extension_introducer != 0x21 ||
350 ape_data.extension_label != APPLICATION_EXT_FUNC_CODE ||
351 ape_data.block_size != 11)
352 return S_OK;
354 data = NULL;
355 data_size = 0;
357 for (;;)
359 hr = IStream_Read(stream, &subblock_size, sizeof(subblock_size), &bytesread);
360 if (FAILED(hr) || bytesread != sizeof(subblock_size))
362 HeapFree(GetProcessHeap(), 0, data);
363 return S_OK;
365 if (!subblock_size) break;
367 if (!data)
368 data = HeapAlloc(GetProcessHeap(), 0, subblock_size + 1);
369 else
371 BYTE *new_data = HeapReAlloc(GetProcessHeap(), 0, data, data_size + subblock_size + 1);
372 if (!new_data)
374 HeapFree(GetProcessHeap(), 0, data);
375 return S_OK;
377 data = new_data;
379 data[data_size] = subblock_size;
380 hr = IStream_Read(stream, data + data_size + 1, subblock_size, &bytesread);
381 if (FAILED(hr) || bytesread != subblock_size)
383 HeapFree(GetProcessHeap(), 0, data);
384 return S_OK;
386 data_size += subblock_size + 1;
389 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem) * 2);
390 if (!result)
392 HeapFree(GetProcessHeap(), 0, data);
393 return E_OUTOFMEMORY;
396 for (i = 0; i < 2; i++)
398 PropVariantInit(&result[i].schema);
399 PropVariantInit(&result[i].id);
400 PropVariantInit(&result[i].value);
403 result[0].id.vt = VT_LPWSTR;
404 result[0].id.u.pwszVal = strdupAtoW("Application");
405 result[0].value.vt = VT_UI1|VT_VECTOR;
406 result[0].value.u.caub.cElems = sizeof(ape_data.application);
407 result[0].value.u.caub.pElems = HeapAlloc(GetProcessHeap(), 0, sizeof(ape_data.application));
408 memcpy(result[0].value.u.caub.pElems, ape_data.application, sizeof(ape_data.application));
410 result[1].id.vt = VT_LPWSTR;
411 result[1].id.u.pwszVal = strdupAtoW("Data");
412 result[1].value.vt = VT_UI1|VT_VECTOR;
413 result[1].value.u.caub.cElems = data_size;
414 result[1].value.u.caub.pElems = data;
416 *items = result;
417 *count = 2;
419 return S_OK;
422 static const MetadataHandlerVtbl APEReader_Vtbl = {
424 &CLSID_WICAPEMetadataReader,
425 load_APE_metadata
428 HRESULT APEReader_CreateInstance(REFIID iid, void **ppv)
430 return MetadataReader_Create(&APEReader_Vtbl, iid, ppv);
433 static HRESULT load_GifComment_metadata(IStream *stream, const GUID *vendor, DWORD options,
434 MetadataItem **items, DWORD *count)
436 #include "pshpack1.h"
437 struct gif_extension
439 BYTE extension_introducer;
440 BYTE extension_label;
441 } ext_data;
442 #include "poppack.h"
443 HRESULT hr;
444 ULONG bytesread, data_size;
445 MetadataItem *result;
446 BYTE subblock_size;
447 char *data;
449 *items = NULL;
450 *count = 0;
452 hr = IStream_Read(stream, &ext_data, sizeof(ext_data), &bytesread);
453 if (FAILED(hr) || bytesread != sizeof(ext_data)) return S_OK;
454 if (ext_data.extension_introducer != 0x21 ||
455 ext_data.extension_label != COMMENT_EXT_FUNC_CODE)
456 return S_OK;
458 data = NULL;
459 data_size = 0;
461 for (;;)
463 hr = IStream_Read(stream, &subblock_size, sizeof(subblock_size), &bytesread);
464 if (FAILED(hr) || bytesread != sizeof(subblock_size))
466 HeapFree(GetProcessHeap(), 0, data);
467 return S_OK;
469 if (!subblock_size) break;
471 if (!data)
472 data = HeapAlloc(GetProcessHeap(), 0, subblock_size + 1);
473 else
475 char *new_data = HeapReAlloc(GetProcessHeap(), 0, data, data_size + subblock_size + 1);
476 if (!new_data)
478 HeapFree(GetProcessHeap(), 0, data);
479 return S_OK;
481 data = new_data;
483 hr = IStream_Read(stream, data + data_size, subblock_size, &bytesread);
484 if (FAILED(hr) || bytesread != subblock_size)
486 HeapFree(GetProcessHeap(), 0, data);
487 return S_OK;
489 data_size += subblock_size;
492 data[data_size] = 0;
494 result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MetadataItem));
495 if (!result)
497 HeapFree(GetProcessHeap(), 0, data);
498 return E_OUTOFMEMORY;
501 PropVariantInit(&result->schema);
502 PropVariantInit(&result->id);
503 PropVariantInit(&result->value);
505 result->id.vt = VT_LPWSTR;
506 result->id.u.pwszVal = strdupAtoW("TextEntry");
507 result->value.vt = VT_LPSTR;
508 result->value.u.pszVal = data;
510 *items = result;
511 *count = 1;
513 return S_OK;
516 static const MetadataHandlerVtbl GifCommentReader_Vtbl = {
518 &CLSID_WICGifCommentMetadataReader,
519 load_GifComment_metadata
522 HRESULT GifCommentReader_CreateInstance(REFIID iid, void **ppv)
524 return MetadataReader_Create(&GifCommentReader_Vtbl, iid, ppv);
527 static IStream *create_stream(const void *data, int data_size)
529 HRESULT hr;
530 IStream *stream;
531 HGLOBAL hdata;
532 void *locked_data;
534 hdata = GlobalAlloc(GMEM_MOVEABLE, data_size);
535 if (!hdata) return NULL;
537 locked_data = GlobalLock(hdata);
538 memcpy(locked_data, data, data_size);
539 GlobalUnlock(hdata);
541 hr = CreateStreamOnHGlobal(hdata, TRUE, &stream);
542 return FAILED(hr) ? NULL : stream;
545 static HRESULT create_metadata_reader(const void *data, int data_size,
546 class_constructor constructor,
547 IWICMetadataReader **reader)
549 HRESULT hr;
550 IWICMetadataReader *metadata_reader;
551 IWICPersistStream *persist;
552 IStream *stream;
554 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
556 hr = constructor(&IID_IWICMetadataReader, (void**)&metadata_reader);
557 if (FAILED(hr)) return hr;
559 hr = IWICMetadataReader_QueryInterface(metadata_reader, &IID_IWICPersistStream, (void **)&persist);
560 if (FAILED(hr))
562 IWICMetadataReader_Release(metadata_reader);
563 return hr;
566 stream = create_stream(data, data_size);
567 IWICPersistStream_LoadEx(persist, stream, NULL, WICPersistOptionDefault);
568 IStream_Release(stream);
570 IWICPersistStream_Release(persist);
572 *reader = metadata_reader;
573 return S_OK;
576 typedef struct {
577 IWICBitmapDecoder IWICBitmapDecoder_iface;
578 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
579 IStream *stream;
580 BYTE LSD_data[13]; /* Logical Screen Descriptor */
581 LONG ref;
582 BOOL initialized;
583 GifFileType *gif;
584 UINT current_frame;
585 CRITICAL_SECTION lock;
586 } GifDecoder;
588 typedef struct {
589 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
590 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
591 LONG ref;
592 SavedImage *frame;
593 GifDecoder *parent;
594 } GifFrameDecode;
596 static inline GifDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
598 return CONTAINING_RECORD(iface, GifDecoder, IWICBitmapDecoder_iface);
601 static inline GifDecoder *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
603 return CONTAINING_RECORD(iface, GifDecoder, IWICMetadataBlockReader_iface);
606 static inline GifFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
608 return CONTAINING_RECORD(iface, GifFrameDecode, IWICBitmapFrameDecode_iface);
611 static inline GifFrameDecode *frame_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
613 return CONTAINING_RECORD(iface, GifFrameDecode, IWICMetadataBlockReader_iface);
616 static HRESULT WINAPI GifFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
617 void **ppv)
619 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
620 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
622 if (!ppv) return E_INVALIDARG;
624 if (IsEqualIID(&IID_IUnknown, iid) ||
625 IsEqualIID(&IID_IWICBitmapSource, iid) ||
626 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
628 *ppv = &This->IWICBitmapFrameDecode_iface;
630 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
632 *ppv = &This->IWICMetadataBlockReader_iface;
634 else
636 *ppv = NULL;
637 return E_NOINTERFACE;
640 IUnknown_AddRef((IUnknown*)*ppv);
641 return S_OK;
644 static ULONG WINAPI GifFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
646 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
647 ULONG ref = InterlockedIncrement(&This->ref);
649 TRACE("(%p) refcount=%u\n", iface, ref);
651 return ref;
654 static ULONG WINAPI GifFrameDecode_Release(IWICBitmapFrameDecode *iface)
656 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
657 ULONG ref = InterlockedDecrement(&This->ref);
659 TRACE("(%p) refcount=%u\n", iface, ref);
661 if (ref == 0)
663 IWICBitmapDecoder_Release(&This->parent->IWICBitmapDecoder_iface);
664 HeapFree(GetProcessHeap(), 0, This);
667 return ref;
670 static HRESULT WINAPI GifFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
671 UINT *puiWidth, UINT *puiHeight)
673 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
674 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
676 *puiWidth = This->frame->ImageDesc.Width;
677 *puiHeight = This->frame->ImageDesc.Height;
679 return S_OK;
682 static HRESULT WINAPI GifFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
683 WICPixelFormatGUID *pPixelFormat)
685 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppIndexed, sizeof(GUID));
687 return S_OK;
690 static HRESULT WINAPI GifFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
691 double *pDpiX, double *pDpiY)
693 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
694 const GifWord aspect_word = This->parent->gif->SAspectRatio;
695 const double aspect = (aspect_word > 0) ? ((aspect_word + 15.0) / 64.0) : 1.0;
696 TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
698 *pDpiX = 96.0 / aspect;
699 *pDpiY = 96.0;
701 return S_OK;
704 static HRESULT WINAPI GifFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
705 IWICPalette *pIPalette)
707 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
708 WICColor colors[256];
709 ColorMapObject *cm = This->frame->ImageDesc.ColorMap;
710 int i, trans;
711 ExtensionBlock *eb;
712 TRACE("(%p,%p)\n", iface, pIPalette);
714 if (!cm) cm = This->parent->gif->SColorMap;
716 if (cm->ColorCount > 256)
718 ERR("GIF contains %i colors???\n", cm->ColorCount);
719 return E_FAIL;
722 for (i = 0; i < cm->ColorCount; i++) {
723 colors[i] = 0xff000000| /* alpha */
724 cm->Colors[i].Red << 16|
725 cm->Colors[i].Green << 8|
726 cm->Colors[i].Blue;
729 /* look for the transparent color extension */
730 for (i = 0; i < This->frame->Extensions.ExtensionBlockCount; ++i) {
731 eb = This->frame->Extensions.ExtensionBlocks + i;
732 if (eb->Function == GRAPHICS_EXT_FUNC_CODE && eb->ByteCount == 8) {
733 if (eb->Bytes[3] & 1) {
734 trans = (unsigned char)eb->Bytes[6];
735 colors[trans] &= 0xffffff; /* set alpha to 0 */
736 break;
741 return IWICPalette_InitializeCustom(pIPalette, colors, cm->ColorCount);
744 static HRESULT copy_interlaced_pixels(const BYTE *srcbuffer,
745 UINT srcwidth, UINT srcheight, INT srcstride, const WICRect *rc,
746 UINT dststride, UINT dstbuffersize, BYTE *dstbuffer)
748 UINT row_offset; /* number of bytes into the source rows where the data starts */
749 const BYTE *src;
750 BYTE *dst;
751 UINT y;
752 WICRect rect;
754 if (!rc)
756 rect.X = 0;
757 rect.Y = 0;
758 rect.Width = srcwidth;
759 rect.Height = srcheight;
760 rc = &rect;
762 else
764 if (rc->X < 0 || rc->Y < 0 || rc->X+rc->Width > srcwidth || rc->Y+rc->Height > srcheight)
765 return E_INVALIDARG;
768 if (dststride < rc->Width)
769 return E_INVALIDARG;
771 if ((dststride * rc->Height) > dstbuffersize)
772 return E_INVALIDARG;
774 row_offset = rc->X;
776 dst = dstbuffer;
777 for (y=rc->Y; y-rc->Y < rc->Height; y++)
779 if (y%8 == 0)
780 src = srcbuffer + srcstride * (y/8);
781 else if (y%4 == 0)
782 src = srcbuffer + srcstride * ((srcheight+7)/8 + y/8);
783 else if (y%2 == 0)
784 src = srcbuffer + srcstride * ((srcheight+3)/4 + y/4);
785 else /* y%2 == 1 */
786 src = srcbuffer + srcstride * ((srcheight+1)/2 + y/2);
787 src += row_offset;
788 memcpy(dst, src, rc->Width);
789 dst += dststride;
791 return S_OK;
794 static HRESULT WINAPI GifFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
795 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
797 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
798 TRACE("(%p,%s,%u,%u,%p)\n", iface, debug_wic_rect(prc), cbStride, cbBufferSize, pbBuffer);
800 if (This->frame->ImageDesc.Interlace)
802 return copy_interlaced_pixels(This->frame->RasterBits, This->frame->ImageDesc.Width,
803 This->frame->ImageDesc.Height, This->frame->ImageDesc.Width,
804 prc, cbStride, cbBufferSize, pbBuffer);
806 else
808 return copy_pixels(8, This->frame->RasterBits, This->frame->ImageDesc.Width,
809 This->frame->ImageDesc.Height, This->frame->ImageDesc.Width,
810 prc, cbStride, cbBufferSize, pbBuffer);
814 static HRESULT WINAPI GifFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
815 IWICMetadataQueryReader **ppIMetadataQueryReader)
817 GifFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
819 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
821 if (!ppIMetadataQueryReader)
822 return E_INVALIDARG;
824 return MetadataQueryReader_CreateInstance(&This->IWICMetadataBlockReader_iface, NULL, ppIMetadataQueryReader);
827 static HRESULT WINAPI GifFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
828 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
830 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
831 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
834 static HRESULT WINAPI GifFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
835 IWICBitmapSource **ppIThumbnail)
837 TRACE("(%p,%p)\n", iface, ppIThumbnail);
838 return WINCODEC_ERR_CODECNOTHUMBNAIL;
841 static const IWICBitmapFrameDecodeVtbl GifFrameDecode_Vtbl = {
842 GifFrameDecode_QueryInterface,
843 GifFrameDecode_AddRef,
844 GifFrameDecode_Release,
845 GifFrameDecode_GetSize,
846 GifFrameDecode_GetPixelFormat,
847 GifFrameDecode_GetResolution,
848 GifFrameDecode_CopyPalette,
849 GifFrameDecode_CopyPixels,
850 GifFrameDecode_GetMetadataQueryReader,
851 GifFrameDecode_GetColorContexts,
852 GifFrameDecode_GetThumbnail
855 static HRESULT WINAPI GifFrameDecode_Block_QueryInterface(IWICMetadataBlockReader *iface,
856 REFIID iid, void **ppv)
858 GifFrameDecode *This = frame_from_IWICMetadataBlockReader(iface);
859 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
862 static ULONG WINAPI GifFrameDecode_Block_AddRef(IWICMetadataBlockReader *iface)
864 GifFrameDecode *This = frame_from_IWICMetadataBlockReader(iface);
865 return IWICBitmapFrameDecode_AddRef(&This->IWICBitmapFrameDecode_iface);
868 static ULONG WINAPI GifFrameDecode_Block_Release(IWICMetadataBlockReader *iface)
870 GifFrameDecode *This = frame_from_IWICMetadataBlockReader(iface);
871 return IWICBitmapFrameDecode_Release(&This->IWICBitmapFrameDecode_iface);
874 static HRESULT WINAPI GifFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
875 GUID *guid)
877 TRACE("(%p,%p)\n", iface, guid);
879 if (!guid) return E_INVALIDARG;
881 *guid = GUID_ContainerFormatGif;
882 return S_OK;
885 static HRESULT WINAPI GifFrameDecode_Block_GetCount(IWICMetadataBlockReader *iface,
886 UINT *count)
888 GifFrameDecode *This = frame_from_IWICMetadataBlockReader(iface);
890 TRACE("%p,%p\n", iface, count);
892 if (!count) return E_INVALIDARG;
894 *count = This->frame->Extensions.ExtensionBlockCount + 1;
895 return S_OK;
898 static HRESULT create_IMD_metadata_reader(GifFrameDecode *This, IWICMetadataReader **reader)
900 HRESULT hr;
901 IWICMetadataReader *metadata_reader;
902 IWICPersistStream *persist;
903 IStream *stream;
904 struct image_descriptor IMD_data;
906 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
908 hr = IMDReader_CreateInstance(&IID_IWICMetadataReader, (void **)&metadata_reader);
909 if (FAILED(hr)) return hr;
911 hr = IWICMetadataReader_QueryInterface(metadata_reader, &IID_IWICPersistStream, (void **)&persist);
912 if (FAILED(hr))
914 IWICMetadataReader_Release(metadata_reader);
915 return hr;
918 /* recreate IMD structure from GIF decoder data */
919 IMD_data.left = This->frame->ImageDesc.Left;
920 IMD_data.top = This->frame->ImageDesc.Top;
921 IMD_data.width = This->frame->ImageDesc.Width;
922 IMD_data.height = This->frame->ImageDesc.Height;
923 IMD_data.packed = 0;
924 /* interlace_flag */
925 IMD_data.packed |= This->frame->ImageDesc.Interlace ? (1 << 6) : 0;
926 if (This->frame->ImageDesc.ColorMap)
928 /* local_color_table_flag */
929 IMD_data.packed |= 1 << 7;
930 /* local_color_table_size */
931 IMD_data.packed |= This->frame->ImageDesc.ColorMap->BitsPerPixel - 1;
932 /* sort_flag */
933 IMD_data.packed |= This->frame->ImageDesc.ColorMap->SortFlag ? 0x20 : 0;
936 stream = create_stream(&IMD_data, sizeof(IMD_data));
937 IWICPersistStream_LoadEx(persist, stream, NULL, WICPersistOptionDefault);
938 IStream_Release(stream);
940 IWICPersistStream_Release(persist);
942 *reader = metadata_reader;
943 return S_OK;
946 static HRESULT WINAPI GifFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
947 UINT index, IWICMetadataReader **reader)
949 GifFrameDecode *This = frame_from_IWICMetadataBlockReader(iface);
950 int i, gce_index = -1, gce_skipped = 0;
952 TRACE("(%p,%u,%p)\n", iface, index, reader);
954 if (!reader) return E_INVALIDARG;
956 if (index == 0)
957 return create_IMD_metadata_reader(This, reader);
959 if (index >= This->frame->Extensions.ExtensionBlockCount + 1)
960 return E_INVALIDARG;
962 for (i = 0; i < This->frame->Extensions.ExtensionBlockCount; i++)
964 class_constructor constructor;
965 const void *data;
966 int data_size;
968 if (index != i + 1 - gce_skipped) continue;
970 if (This->frame->Extensions.ExtensionBlocks[i].Function == GRAPHICS_EXT_FUNC_CODE)
972 gce_index = i;
973 gce_skipped = 1;
974 continue;
976 else if (This->frame->Extensions.ExtensionBlocks[i].Function == COMMENT_EXT_FUNC_CODE)
978 constructor = GifCommentReader_CreateInstance;
979 data = This->frame->Extensions.ExtensionBlocks[i].Bytes;
980 data_size = This->frame->Extensions.ExtensionBlocks[i].ByteCount;
982 else
984 constructor = UnknownMetadataReader_CreateInstance;
985 data = This->frame->Extensions.ExtensionBlocks[i].Bytes;
986 data_size = This->frame->Extensions.ExtensionBlocks[i].ByteCount;
988 return create_metadata_reader(data, data_size, constructor, reader);
991 if (gce_index == -1) return E_INVALIDARG;
993 return create_metadata_reader(This->frame->Extensions.ExtensionBlocks[gce_index].Bytes + 3,
994 This->frame->Extensions.ExtensionBlocks[gce_index].ByteCount - 4,
995 GCEReader_CreateInstance, reader);
998 static HRESULT WINAPI GifFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader *iface,
999 IEnumUnknown **enumerator)
1001 FIXME("(%p,%p): stub\n", iface, enumerator);
1002 return E_NOTIMPL;
1005 static const IWICMetadataBlockReaderVtbl GifFrameDecode_BlockVtbl =
1007 GifFrameDecode_Block_QueryInterface,
1008 GifFrameDecode_Block_AddRef,
1009 GifFrameDecode_Block_Release,
1010 GifFrameDecode_Block_GetContainerFormat,
1011 GifFrameDecode_Block_GetCount,
1012 GifFrameDecode_Block_GetReaderByIndex,
1013 GifFrameDecode_Block_GetEnumerator
1016 static HRESULT WINAPI GifDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
1017 void **ppv)
1019 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1020 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1022 if (!ppv) return E_INVALIDARG;
1024 if (IsEqualIID(&IID_IUnknown, iid) ||
1025 IsEqualIID(&IID_IWICBitmapDecoder, iid))
1027 *ppv = &This->IWICBitmapDecoder_iface;
1029 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
1031 *ppv = &This->IWICMetadataBlockReader_iface;
1033 else
1035 *ppv = NULL;
1036 return E_NOINTERFACE;
1039 IUnknown_AddRef((IUnknown*)*ppv);
1040 return S_OK;
1043 static ULONG WINAPI GifDecoder_AddRef(IWICBitmapDecoder *iface)
1045 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1046 ULONG ref = InterlockedIncrement(&This->ref);
1048 TRACE("(%p) refcount=%u\n", iface, ref);
1050 return ref;
1053 static ULONG WINAPI GifDecoder_Release(IWICBitmapDecoder *iface)
1055 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1056 ULONG ref = InterlockedDecrement(&This->ref);
1058 TRACE("(%p) refcount=%u\n", iface, ref);
1060 if (ref == 0)
1062 if (This->stream)
1064 IStream_Release(This->stream);
1065 DGifCloseFile(This->gif);
1067 This->lock.DebugInfo->Spare[0] = 0;
1068 DeleteCriticalSection(&This->lock);
1069 HeapFree(GetProcessHeap(), 0, This);
1072 return ref;
1075 static HRESULT WINAPI GifDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
1076 DWORD *capability)
1078 HRESULT hr;
1080 TRACE("(%p,%p,%p)\n", iface, stream, capability);
1082 if (!stream || !capability) return E_INVALIDARG;
1084 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
1085 if (hr != S_OK) return hr;
1087 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
1088 WICBitmapDecoderCapabilityCanDecodeSomeImages |
1089 WICBitmapDecoderCapabilityCanEnumerateMetadata;
1090 return S_OK;
1093 static int _gif_inputfunc(GifFileType *gif, GifByteType *data, int len) {
1094 IStream *stream = gif->UserData;
1095 ULONG bytesread;
1096 HRESULT hr;
1098 if (!stream)
1100 ERR("attempting to read file after initialization\n");
1101 return 0;
1104 hr = IStream_Read(stream, data, len, &bytesread);
1105 if (FAILED(hr)) bytesread = 0;
1106 return bytesread;
1109 static HRESULT WINAPI GifDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
1110 WICDecodeOptions cacheOptions)
1112 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1113 LARGE_INTEGER seek;
1114 int ret;
1116 TRACE("(%p,%p,%x)\n", iface, pIStream, cacheOptions);
1118 EnterCriticalSection(&This->lock);
1120 if (This->initialized || This->gif)
1122 WARN("already initialized\n");
1123 LeaveCriticalSection(&This->lock);
1124 return WINCODEC_ERR_WRONGSTATE;
1127 /* seek to start of stream */
1128 seek.QuadPart = 0;
1129 IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
1131 /* read all data from the stream */
1132 This->gif = DGifOpen((void*)pIStream, _gif_inputfunc);
1133 if (!This->gif)
1135 LeaveCriticalSection(&This->lock);
1136 return E_FAIL;
1139 ret = DGifSlurp(This->gif);
1140 if (ret == GIF_ERROR)
1142 LeaveCriticalSection(&This->lock);
1143 return E_FAIL;
1146 /* make sure we don't use the stream after this method returns */
1147 This->gif->UserData = NULL;
1149 seek.QuadPart = 0;
1150 IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
1151 IStream_Read(pIStream, This->LSD_data, sizeof(This->LSD_data), NULL);
1153 This->stream = pIStream;
1154 IStream_AddRef(This->stream);
1156 This->initialized = TRUE;
1158 LeaveCriticalSection(&This->lock);
1160 return S_OK;
1163 static HRESULT WINAPI GifDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
1164 GUID *pguidContainerFormat)
1166 memcpy(pguidContainerFormat, &GUID_ContainerFormatGif, sizeof(GUID));
1167 return S_OK;
1170 static HRESULT WINAPI GifDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
1171 IWICBitmapDecoderInfo **ppIDecoderInfo)
1173 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
1175 return get_decoder_info(&CLSID_WICGifDecoder, ppIDecoderInfo);
1178 static HRESULT WINAPI GifDecoder_CopyPalette(IWICBitmapDecoder *iface, IWICPalette *palette)
1180 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1181 WICColor colors[256];
1182 ColorMapObject *cm;
1183 int i, trans, count;
1184 ExtensionBlock *eb;
1186 TRACE("(%p,%p)\n", iface, palette);
1188 if (!This->gif)
1189 return WINCODEC_ERR_WRONGSTATE;
1191 cm = This->gif->SColorMap;
1192 if (cm)
1194 if (cm->ColorCount > 256)
1196 ERR("GIF contains invalid number of colors: %d\n", cm->ColorCount);
1197 return E_FAIL;
1200 for (i = 0; i < cm->ColorCount; i++)
1202 colors[i] = 0xff000000 | /* alpha */
1203 cm->Colors[i].Red << 16 |
1204 cm->Colors[i].Green << 8 |
1205 cm->Colors[i].Blue;
1208 count = cm->ColorCount;
1210 else
1212 colors[0] = 0xff000000;
1213 colors[1] = 0xffffffff;
1215 for (i = 2; i < 256; i++)
1216 colors[i] = 0xff000000;
1218 count = 256;
1221 /* look for the transparent color extension */
1222 for (i = 0; i < This->gif->SavedImages[This->current_frame].Extensions.ExtensionBlockCount; i++)
1224 eb = This->gif->SavedImages[This->current_frame].Extensions.ExtensionBlocks + i;
1225 if (eb->Function == GRAPHICS_EXT_FUNC_CODE && eb->ByteCount == 8)
1227 if (eb->Bytes[3] & 1)
1229 trans = (unsigned char)eb->Bytes[6];
1230 colors[trans] &= 0xffffff; /* set alpha to 0 */
1231 break;
1236 return IWICPalette_InitializeCustom(palette, colors, count);
1239 static HRESULT WINAPI GifDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
1240 IWICMetadataQueryReader **ppIMetadataQueryReader)
1242 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1244 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
1246 if (!ppIMetadataQueryReader) return E_INVALIDARG;
1248 return MetadataQueryReader_CreateInstance(&This->IWICMetadataBlockReader_iface, NULL, ppIMetadataQueryReader);
1251 static HRESULT WINAPI GifDecoder_GetPreview(IWICBitmapDecoder *iface,
1252 IWICBitmapSource **ppIBitmapSource)
1254 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
1255 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1258 static HRESULT WINAPI GifDecoder_GetColorContexts(IWICBitmapDecoder *iface,
1259 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1261 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
1262 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1265 static HRESULT WINAPI GifDecoder_GetThumbnail(IWICBitmapDecoder *iface,
1266 IWICBitmapSource **ppIThumbnail)
1268 TRACE("(%p,%p)\n", iface, ppIThumbnail);
1269 return WINCODEC_ERR_CODECNOTHUMBNAIL;
1272 static HRESULT WINAPI GifDecoder_GetFrameCount(IWICBitmapDecoder *iface,
1273 UINT *pCount)
1275 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1277 if (!pCount) return E_INVALIDARG;
1279 EnterCriticalSection(&This->lock);
1280 *pCount = This->gif ? This->gif->ImageCount : 0;
1281 LeaveCriticalSection(&This->lock);
1283 TRACE("(%p) <-- %d\n", iface, *pCount);
1285 return S_OK;
1288 static HRESULT WINAPI GifDecoder_GetFrame(IWICBitmapDecoder *iface,
1289 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
1291 GifDecoder *This = impl_from_IWICBitmapDecoder(iface);
1292 GifFrameDecode *result;
1293 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
1295 if (!This->initialized) return WINCODEC_ERR_FRAMEMISSING;
1297 if (index >= This->gif->ImageCount) return E_INVALIDARG;
1299 result = HeapAlloc(GetProcessHeap(), 0, sizeof(GifFrameDecode));
1300 if (!result) return E_OUTOFMEMORY;
1302 result->IWICBitmapFrameDecode_iface.lpVtbl = &GifFrameDecode_Vtbl;
1303 result->IWICMetadataBlockReader_iface.lpVtbl = &GifFrameDecode_BlockVtbl;
1304 result->ref = 1;
1305 result->frame = &This->gif->SavedImages[index];
1306 IWICBitmapDecoder_AddRef(iface);
1307 result->parent = This;
1308 This->current_frame = index;
1310 *ppIBitmapFrame = &result->IWICBitmapFrameDecode_iface;
1312 return S_OK;
1315 static const IWICBitmapDecoderVtbl GifDecoder_Vtbl = {
1316 GifDecoder_QueryInterface,
1317 GifDecoder_AddRef,
1318 GifDecoder_Release,
1319 GifDecoder_QueryCapability,
1320 GifDecoder_Initialize,
1321 GifDecoder_GetContainerFormat,
1322 GifDecoder_GetDecoderInfo,
1323 GifDecoder_CopyPalette,
1324 GifDecoder_GetMetadataQueryReader,
1325 GifDecoder_GetPreview,
1326 GifDecoder_GetColorContexts,
1327 GifDecoder_GetThumbnail,
1328 GifDecoder_GetFrameCount,
1329 GifDecoder_GetFrame
1332 static HRESULT WINAPI GifDecoder_Block_QueryInterface(IWICMetadataBlockReader *iface,
1333 REFIID iid, void **ppv)
1335 GifDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1336 return IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1339 static ULONG WINAPI GifDecoder_Block_AddRef(IWICMetadataBlockReader *iface)
1341 GifDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1342 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
1345 static ULONG WINAPI GifDecoder_Block_Release(IWICMetadataBlockReader *iface)
1347 GifDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1348 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1351 static HRESULT WINAPI GifDecoder_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
1352 GUID *guid)
1354 TRACE("(%p,%p)\n", iface, guid);
1356 if (!guid) return E_INVALIDARG;
1358 *guid = GUID_ContainerFormatGif;
1359 return S_OK;
1362 static HRESULT WINAPI GifDecoder_Block_GetCount(IWICMetadataBlockReader *iface,
1363 UINT *count)
1365 GifDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1367 TRACE("%p,%p\n", iface, count);
1369 if (!count) return E_INVALIDARG;
1371 *count = This->gif->Extensions.ExtensionBlockCount + 1;
1372 return S_OK;
1375 static HRESULT WINAPI GifDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
1376 UINT index, IWICMetadataReader **reader)
1378 GifDecoder *This = impl_from_IWICMetadataBlockReader(iface);
1379 int i;
1381 TRACE("(%p,%u,%p)\n", iface, index, reader);
1383 if (!reader) return E_INVALIDARG;
1385 if (index == 0)
1386 return create_metadata_reader(This->LSD_data, sizeof(This->LSD_data),
1387 LSDReader_CreateInstance, reader);
1389 for (i = 0; i < This->gif->Extensions.ExtensionBlockCount; i++)
1391 class_constructor constructor;
1393 if (index != i + 1) continue;
1395 if (This->gif->Extensions.ExtensionBlocks[i].Function == APPLICATION_EXT_FUNC_CODE)
1396 constructor = APEReader_CreateInstance;
1397 else if (This->gif->Extensions.ExtensionBlocks[i].Function == COMMENT_EXT_FUNC_CODE)
1398 constructor = GifCommentReader_CreateInstance;
1399 else
1400 constructor = UnknownMetadataReader_CreateInstance;
1402 return create_metadata_reader(This->gif->Extensions.ExtensionBlocks[i].Bytes,
1403 This->gif->Extensions.ExtensionBlocks[i].ByteCount,
1404 constructor, reader);
1407 return E_INVALIDARG;
1410 static HRESULT WINAPI GifDecoder_Block_GetEnumerator(IWICMetadataBlockReader *iface,
1411 IEnumUnknown **enumerator)
1413 FIXME("(%p,%p): stub\n", iface, enumerator);
1414 return E_NOTIMPL;
1417 static const IWICMetadataBlockReaderVtbl GifDecoder_BlockVtbl =
1419 GifDecoder_Block_QueryInterface,
1420 GifDecoder_Block_AddRef,
1421 GifDecoder_Block_Release,
1422 GifDecoder_Block_GetContainerFormat,
1423 GifDecoder_Block_GetCount,
1424 GifDecoder_Block_GetReaderByIndex,
1425 GifDecoder_Block_GetEnumerator
1428 HRESULT GifDecoder_CreateInstance(REFIID iid, void** ppv)
1430 GifDecoder *This;
1431 HRESULT ret;
1433 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1435 *ppv = NULL;
1437 This = HeapAlloc(GetProcessHeap(), 0, sizeof(GifDecoder));
1438 if (!This) return E_OUTOFMEMORY;
1440 This->IWICBitmapDecoder_iface.lpVtbl = &GifDecoder_Vtbl;
1441 This->IWICMetadataBlockReader_iface.lpVtbl = &GifDecoder_BlockVtbl;
1442 This->stream = NULL;
1443 This->ref = 1;
1444 This->initialized = FALSE;
1445 This->gif = NULL;
1446 This->current_frame = 0;
1447 InitializeCriticalSection(&This->lock);
1448 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": GifDecoder.lock");
1450 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1451 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1453 return ret;
1456 typedef struct GifEncoder
1458 IWICBitmapEncoder IWICBitmapEncoder_iface;
1459 LONG ref;
1460 IStream *stream;
1461 CRITICAL_SECTION lock;
1462 BOOL initialized, info_written, committed;
1463 UINT n_frames;
1464 WICColor palette[256];
1465 UINT colors;
1466 } GifEncoder;
1468 static inline GifEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1470 return CONTAINING_RECORD(iface, GifEncoder, IWICBitmapEncoder_iface);
1473 typedef struct GifFrameEncode
1475 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1476 IWICMetadataBlockWriter IWICMetadataBlockWriter_iface;
1477 LONG ref;
1478 GifEncoder *encoder;
1479 BOOL initialized, interlace, committed;
1480 UINT width, height, lines;
1481 double xres, yres;
1482 WICColor palette[256];
1483 UINT colors;
1484 BYTE *image_data;
1485 } GifFrameEncode;
1487 static inline GifFrameEncode *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1489 return CONTAINING_RECORD(iface, GifFrameEncode, IWICBitmapFrameEncode_iface);
1492 static inline GifFrameEncode *impl_from_IWICMetadataBlockWriter(IWICMetadataBlockWriter *iface)
1494 return CONTAINING_RECORD(iface, GifFrameEncode, IWICMetadataBlockWriter_iface);
1497 static HRESULT WINAPI GifFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid, void **ppv)
1499 GifFrameEncode *encoder = impl_from_IWICBitmapFrameEncode(iface);
1501 TRACE("%p,%s,%p\n", iface, debugstr_guid(iid), ppv);
1503 if (!ppv) return E_INVALIDARG;
1505 if (IsEqualIID(&IID_IUnknown, iid) ||
1506 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1508 *ppv = iface;
1510 else if (IsEqualIID(&IID_IWICMetadataBlockWriter, iid))
1512 *ppv = &encoder->IWICMetadataBlockWriter_iface;
1514 else
1516 *ppv = NULL;
1517 return E_NOINTERFACE;
1520 IUnknown_AddRef((IUnknown *)*ppv);
1521 return S_OK;
1524 static ULONG WINAPI GifFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1526 GifFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1527 ULONG ref = InterlockedIncrement(&This->ref);
1529 TRACE("%p -> %u\n", iface, ref);
1530 return ref;
1533 static ULONG WINAPI GifFrameEncode_Release(IWICBitmapFrameEncode *iface)
1535 GifFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1536 ULONG ref = InterlockedDecrement(&This->ref);
1538 TRACE("%p -> %u\n", iface, ref);
1540 if (!ref)
1542 IWICBitmapEncoder_Release(&This->encoder->IWICBitmapEncoder_iface);
1543 HeapFree(GetProcessHeap(), 0, This->image_data);
1544 HeapFree(GetProcessHeap(), 0, This);
1547 return ref;
1550 static HRESULT WINAPI GifFrameEncode_Initialize(IWICBitmapFrameEncode *iface, IPropertyBag2 *options)
1552 GifFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1553 HRESULT hr;
1555 TRACE("%p,%p\n", iface, options);
1557 EnterCriticalSection(&This->encoder->lock);
1559 if (!This->initialized)
1561 This->initialized = TRUE;
1562 hr = S_OK;
1564 else
1565 hr = WINCODEC_ERR_WRONGSTATE;
1567 LeaveCriticalSection(&This->encoder->lock);
1569 return hr;
1572 static HRESULT WINAPI GifFrameEncode_SetSize(IWICBitmapFrameEncode *iface, UINT width, UINT height)
1574 GifFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1575 HRESULT hr;
1577 TRACE("%p,%u,%u\n", iface, width, height);
1579 if (!width || !height) return E_INVALIDARG;
1581 EnterCriticalSection(&This->encoder->lock);
1583 if (This->initialized)
1585 HeapFree(GetProcessHeap(), 0, This->image_data);
1587 This->image_data = HeapAlloc(GetProcessHeap(), 0, width * height);
1588 if (This->image_data)
1590 This->width = width;
1591 This->height = height;
1592 hr = S_OK;
1594 else
1595 hr = E_OUTOFMEMORY;
1597 else
1598 hr = WINCODEC_ERR_WRONGSTATE;
1600 LeaveCriticalSection(&This->encoder->lock);
1602 return hr;
1605 static HRESULT WINAPI GifFrameEncode_SetResolution(IWICBitmapFrameEncode *iface, double xres, double yres)
1607 GifFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1608 HRESULT hr;
1610 TRACE("%p,%f,%f\n", iface, xres, yres);
1612 EnterCriticalSection(&This->encoder->lock);
1614 if (This->initialized)
1616 This->xres = xres;
1617 This->yres = yres;
1618 hr = S_OK;
1620 else
1621 hr = WINCODEC_ERR_WRONGSTATE;
1623 LeaveCriticalSection(&This->encoder->lock);
1625 return hr;
1628 static HRESULT WINAPI GifFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface, WICPixelFormatGUID *format)
1630 GifFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1631 HRESULT hr;
1633 TRACE("%p,%s\n", iface, debugstr_guid(format));
1635 if (!format) return E_INVALIDARG;
1637 EnterCriticalSection(&This->encoder->lock);
1639 if (This->initialized)
1641 *format = GUID_WICPixelFormat8bppIndexed;
1642 hr = S_OK;
1644 else
1645 hr = WINCODEC_ERR_WRONGSTATE;
1647 LeaveCriticalSection(&This->encoder->lock);
1649 return hr;
1652 static HRESULT WINAPI GifFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface, UINT count, IWICColorContext **context)
1654 FIXME("%p,%u,%p: stub\n", iface, count, context);
1655 return E_NOTIMPL;
1658 static HRESULT WINAPI GifFrameEncode_SetPalette(IWICBitmapFrameEncode *iface, IWICPalette *palette)
1660 GifFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1661 HRESULT hr;
1663 TRACE("%p,%p\n", iface, palette);
1665 if (!palette) return E_INVALIDARG;
1667 EnterCriticalSection(&This->encoder->lock);
1669 if (This->initialized)
1670 hr = IWICPalette_GetColors(palette, 256, This->palette, &This->colors);
1671 else
1672 hr = WINCODEC_ERR_NOTINITIALIZED;
1674 LeaveCriticalSection(&This->encoder->lock);
1675 return hr;
1678 static HRESULT WINAPI GifFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface, IWICBitmapSource *thumbnail)
1680 FIXME("%p,%p: stub\n", iface, thumbnail);
1681 return E_NOTIMPL;
1684 static HRESULT WINAPI GifFrameEncode_WritePixels(IWICBitmapFrameEncode *iface, UINT lines, UINT stride, UINT size, BYTE *pixels)
1686 GifFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1687 HRESULT hr;
1689 TRACE("%p,%u,%u,%u,%p\n", iface, lines, stride, size, pixels);
1691 if (!pixels) return E_INVALIDARG;
1693 EnterCriticalSection(&This->encoder->lock);
1695 if (This->initialized && This->image_data)
1697 if (This->lines + lines <= This->height)
1699 UINT i;
1700 BYTE *src, *dst;
1702 src = pixels;
1703 dst = This->image_data + This->lines * This->width;
1705 for (i = 0; i < lines; i++)
1707 memcpy(dst, src, This->width);
1708 src += stride;
1709 dst += This->width;
1712 This->lines += lines;
1713 hr = S_OK;
1715 else
1716 hr = E_INVALIDARG;
1718 else
1719 hr = WINCODEC_ERR_WRONGSTATE;
1721 LeaveCriticalSection(&This->encoder->lock);
1722 return hr;
1725 static HRESULT WINAPI GifFrameEncode_WriteSource(IWICBitmapFrameEncode *iface, IWICBitmapSource *source, WICRect *rc)
1727 GifFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1728 HRESULT hr;
1730 TRACE("%p,%p,%p\n", iface, source, rc);
1732 if (!source) return E_INVALIDARG;
1734 EnterCriticalSection(&This->encoder->lock);
1736 if (This->initialized)
1738 const GUID *format = &GUID_WICPixelFormat8bppIndexed;
1740 hr = configure_write_source(iface, source, rc, format,
1741 This->width, This->height, This->xres, This->yres);
1742 if (hr == S_OK)
1743 hr = write_source(iface, source, rc, format, 8, !This->colors, This->width, This->height);
1745 else
1746 hr = WINCODEC_ERR_WRONGSTATE;
1748 LeaveCriticalSection(&This->encoder->lock);
1749 return hr;
1752 #define LZW_DICT_SIZE (1 << 12)
1754 struct lzw_dict
1756 short prefix[LZW_DICT_SIZE];
1757 unsigned char suffix[LZW_DICT_SIZE];
1760 struct lzw_state
1762 struct lzw_dict dict;
1763 short init_code_bits, code_bits, next_code, clear_code, eof_code;
1764 unsigned bits_buf;
1765 int bits_count;
1766 int (*user_write_data)(void *user_ptr, void *data, int length);
1767 void *user_ptr;
1770 struct input_stream
1772 unsigned len;
1773 const BYTE *in;
1776 struct output_stream
1778 struct
1780 unsigned char len;
1781 char data[255];
1782 } gif_block;
1783 IStream *out;
1786 static int lzw_output_code(struct lzw_state *state, short code)
1788 state->bits_buf |= code << state->bits_count;
1789 state->bits_count += state->code_bits;
1791 while (state->bits_count >= 8)
1793 unsigned char byte = (unsigned char)state->bits_buf;
1794 if (state->user_write_data(state->user_ptr, &byte, 1) != 1)
1795 return 0;
1796 state->bits_buf >>= 8;
1797 state->bits_count -= 8;
1800 return 1;
1803 static inline int lzw_output_clear_code(struct lzw_state *state)
1805 return lzw_output_code(state, state->clear_code);
1808 static inline int lzw_output_eof_code(struct lzw_state *state)
1810 return lzw_output_code(state, state->eof_code);
1813 static int lzw_flush_bits(struct lzw_state *state)
1815 unsigned char byte;
1817 while (state->bits_count >= 8)
1819 byte = (unsigned char)state->bits_buf;
1820 if (state->user_write_data(state->user_ptr, &byte, 1) != 1)
1821 return 0;
1822 state->bits_buf >>= 8;
1823 state->bits_count -= 8;
1826 if (state->bits_count)
1828 static const char mask[8] = { 0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f };
1830 byte = (unsigned char)state->bits_buf & mask[state->bits_count];
1831 if (state->user_write_data(state->user_ptr, &byte, 1) != 1)
1832 return 0;
1835 state->bits_buf = 0;
1836 state->bits_count = 0;
1838 return 1;
1841 static void lzw_dict_reset(struct lzw_state *state)
1843 int i;
1845 state->code_bits = state->init_code_bits + 1;
1846 state->next_code = (1 << state->init_code_bits) + 2;
1848 for(i = 0; i < LZW_DICT_SIZE; i++)
1850 state->dict.prefix[i] = 1 << 12; /* impossible LZW code value */
1851 state->dict.suffix[i] = 0;
1855 static void lzw_state_init(struct lzw_state *state, short init_code_bits, void *user_write_data, void *user_ptr)
1857 state->init_code_bits = init_code_bits;
1858 state->clear_code = 1 << init_code_bits;
1859 state->eof_code = state->clear_code + 1;
1860 state->bits_buf = 0;
1861 state->bits_count = 0;
1862 state->user_write_data = user_write_data;
1863 state->user_ptr = user_ptr;
1865 lzw_dict_reset(state);
1868 static int lzw_dict_add(struct lzw_state *state, short prefix, unsigned char suffix)
1870 if (state->next_code < LZW_DICT_SIZE)
1872 state->dict.prefix[state->next_code] = prefix;
1873 state->dict.suffix[state->next_code] = suffix;
1875 if ((state->next_code & (state->next_code - 1)) == 0)
1876 state->code_bits++;
1878 state->next_code++;
1879 return state->next_code;
1882 return -1;
1885 static short lzw_dict_lookup(const struct lzw_state *state, short prefix, unsigned char suffix)
1887 short i;
1889 for (i = 0; i < state->next_code; i++)
1891 if (state->dict.prefix[i] == prefix && state->dict.suffix[i] == suffix)
1892 return i;
1895 return -1;
1898 static inline int write_byte(struct output_stream *out, char byte)
1900 if (out->gif_block.len == 255)
1902 if (IStream_Write(out->out, &out->gif_block, sizeof(out->gif_block), NULL) != S_OK)
1903 return 0;
1905 out->gif_block.len = 0;
1908 out->gif_block.data[out->gif_block.len++] = byte;
1910 return 1;
1913 static int write_data(void *user_ptr, void *user_data, int length)
1915 unsigned char *data = user_data;
1916 struct output_stream *out = user_ptr;
1917 int len = length;
1919 while (len-- > 0)
1921 if (!write_byte(out, *data++)) return 0;
1924 return length;
1927 static int flush_output_data(void *user_ptr)
1929 struct output_stream *out = user_ptr;
1931 if (out->gif_block.len)
1933 if (IStream_Write(out->out, &out->gif_block, out->gif_block.len + sizeof(out->gif_block.len), NULL) != S_OK)
1934 return 0;
1937 /* write GIF block terminator */
1938 out->gif_block.len = 0;
1939 return IStream_Write(out->out, &out->gif_block, sizeof(out->gif_block.len), NULL) == S_OK;
1942 static inline int read_byte(struct input_stream *in, unsigned char *byte)
1944 if (in->len)
1946 in->len--;
1947 *byte = *in->in++;
1948 return 1;
1951 return 0;
1954 static HRESULT gif_compress(IStream *out_stream, const BYTE *in_data, ULONG in_size)
1956 struct input_stream in;
1957 struct output_stream out;
1958 struct lzw_state state;
1959 short init_code_bits, prefix, code;
1960 unsigned char suffix;
1962 in.in = in_data;
1963 in.len = in_size;
1965 out.gif_block.len = 0;
1966 out.out = out_stream;
1968 init_code_bits = suffix = 8;
1969 if (IStream_Write(out.out, &suffix, sizeof(suffix), NULL) != S_OK)
1970 return E_FAIL;
1972 lzw_state_init(&state, init_code_bits, write_data, &out);
1974 if (!lzw_output_clear_code(&state))
1975 return E_FAIL;
1977 if (read_byte(&in, &suffix))
1979 prefix = suffix;
1981 while (read_byte(&in, &suffix))
1983 code = lzw_dict_lookup(&state, prefix, suffix);
1984 if (code == -1)
1986 if (!lzw_output_code(&state, prefix))
1987 return E_FAIL;
1989 if (lzw_dict_add(&state, prefix, suffix) == -1)
1991 if (!lzw_output_clear_code(&state))
1992 return E_FAIL;
1993 lzw_dict_reset(&state);
1996 prefix = suffix;
1998 else
1999 prefix = code;
2002 if (!lzw_output_code(&state, prefix))
2003 return E_FAIL;
2004 if (!lzw_output_eof_code(&state))
2005 return E_FAIL;
2006 if (!lzw_flush_bits(&state))
2007 return E_FAIL;
2010 return flush_output_data(&out) ? S_OK : E_FAIL;
2013 static HRESULT WINAPI GifFrameEncode_Commit(IWICBitmapFrameEncode *iface)
2015 GifFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
2016 HRESULT hr;
2018 TRACE("%p\n", iface);
2020 EnterCriticalSection(&This->encoder->lock);
2022 if (This->image_data && This->lines == This->height && !This->committed)
2024 BYTE gif_palette[256][3];
2026 hr = S_OK;
2028 if (!This->encoder->info_written)
2030 struct logical_screen_descriptor lsd;
2032 /* Logical Screen Descriptor */
2033 memcpy(lsd.signature, "GIF89a", 6);
2034 lsd.width = This->width;
2035 lsd.height = This->height;
2036 lsd.packed = 0;
2037 if (This->encoder->colors)
2038 lsd.packed |= 0x80; /* global color table flag */
2039 lsd.packed |= 0x07 << 4; /* color resolution */
2040 lsd.packed |= 0x07; /* global color table size */
2041 lsd.background_color_index = 0; /* FIXME */
2042 lsd.pixel_aspect_ratio = 0;
2043 hr = IStream_Write(This->encoder->stream, &lsd, sizeof(lsd), NULL);
2044 if (hr == S_OK && This->encoder->colors)
2046 UINT i;
2048 /* Global Color Table */
2049 memset(gif_palette, 0, sizeof(gif_palette));
2050 for (i = 0; i < This->encoder->colors; i++)
2052 gif_palette[i][0] = (This->encoder->palette[i] >> 16) & 0xff;
2053 gif_palette[i][1] = (This->encoder->palette[i] >> 8) & 0xff;
2054 gif_palette[i][2] = This->encoder->palette[i] & 0xff;
2056 hr = IStream_Write(This->encoder->stream, gif_palette, sizeof(gif_palette), NULL);
2059 /* FIXME: write GCE, APE, etc. GIF extensions */
2061 if (hr == S_OK)
2062 This->encoder->info_written = TRUE;
2065 if (hr == S_OK)
2067 char image_separator = 0x2c;
2069 hr = IStream_Write(This->encoder->stream, &image_separator, sizeof(image_separator), NULL);
2070 if (hr == S_OK)
2072 struct image_descriptor imd;
2074 /* Image Descriptor */
2075 imd.left = 0;
2076 imd.top = 0;
2077 imd.width = This->width;
2078 imd.height = This->height;
2079 imd.packed = 0;
2080 if (This->colors)
2082 imd.packed |= 0x80; /* local color table flag */
2083 imd.packed |= 0x07; /* local color table size */
2085 /* FIXME: interlace flag */
2086 hr = IStream_Write(This->encoder->stream, &imd, sizeof(imd), NULL);
2087 if (hr == S_OK && This->colors)
2089 UINT i;
2091 /* Local Color Table */
2092 memset(gif_palette, 0, sizeof(gif_palette));
2093 for (i = 0; i < This->colors; i++)
2095 gif_palette[i][0] = (This->palette[i] >> 16) & 0xff;
2096 gif_palette[i][1] = (This->palette[i] >> 8) & 0xff;
2097 gif_palette[i][2] = This->palette[i] & 0xff;
2099 hr = IStream_Write(This->encoder->stream, gif_palette, sizeof(gif_palette), NULL);
2100 if (hr == S_OK)
2102 /* Image Data */
2103 hr = gif_compress(This->encoder->stream, This->image_data, This->width * This->height);
2104 if (hr == S_OK)
2105 This->committed = TRUE;
2111 else
2112 hr = WINCODEC_ERR_WRONGSTATE;
2114 LeaveCriticalSection(&This->encoder->lock);
2115 return hr;
2118 static HRESULT WINAPI GifFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface, IWICMetadataQueryWriter **writer)
2120 GifFrameEncode *encode = impl_from_IWICBitmapFrameEncode(iface);
2122 TRACE("iface, %p, writer %p.\n", iface, writer);
2124 if (!writer)
2125 return E_INVALIDARG;
2127 if (!encode->initialized)
2128 return WINCODEC_ERR_NOTINITIALIZED;
2130 return MetadataQueryWriter_CreateInstance(&encode->IWICMetadataBlockWriter_iface, NULL, writer);
2133 static const IWICBitmapFrameEncodeVtbl GifFrameEncode_Vtbl =
2135 GifFrameEncode_QueryInterface,
2136 GifFrameEncode_AddRef,
2137 GifFrameEncode_Release,
2138 GifFrameEncode_Initialize,
2139 GifFrameEncode_SetSize,
2140 GifFrameEncode_SetResolution,
2141 GifFrameEncode_SetPixelFormat,
2142 GifFrameEncode_SetColorContexts,
2143 GifFrameEncode_SetPalette,
2144 GifFrameEncode_SetThumbnail,
2145 GifFrameEncode_WritePixels,
2146 GifFrameEncode_WriteSource,
2147 GifFrameEncode_Commit,
2148 GifFrameEncode_GetMetadataQueryWriter
2151 static HRESULT WINAPI GifEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid, void **ppv)
2153 TRACE("%p,%s,%p\n", iface, debugstr_guid(iid), ppv);
2155 if (!ppv) return E_INVALIDARG;
2157 if (IsEqualIID(&IID_IUnknown, iid) ||
2158 IsEqualIID(&IID_IWICBitmapEncoder, iid))
2160 IWICBitmapEncoder_AddRef(iface);
2161 *ppv = iface;
2162 return S_OK;
2165 *ppv = NULL;
2166 return E_NOINTERFACE;
2169 static ULONG WINAPI GifEncoder_AddRef(IWICBitmapEncoder *iface)
2171 GifEncoder *This = impl_from_IWICBitmapEncoder(iface);
2172 ULONG ref = InterlockedIncrement(&This->ref);
2174 TRACE("%p -> %u\n", iface, ref);
2175 return ref;
2178 static ULONG WINAPI GifEncoder_Release(IWICBitmapEncoder *iface)
2180 GifEncoder *This = impl_from_IWICBitmapEncoder(iface);
2181 ULONG ref = InterlockedDecrement(&This->ref);
2183 TRACE("%p -> %u\n", iface, ref);
2185 if (!ref)
2187 if (This->stream) IStream_Release(This->stream);
2188 This->lock.DebugInfo->Spare[0] = 0;
2189 DeleteCriticalSection(&This->lock);
2190 HeapFree(GetProcessHeap(), 0, This);
2193 return ref;
2196 static HRESULT WINAPI GifEncoder_Initialize(IWICBitmapEncoder *iface, IStream *stream, WICBitmapEncoderCacheOption option)
2198 GifEncoder *This = impl_from_IWICBitmapEncoder(iface);
2199 HRESULT hr;
2201 TRACE("%p,%p,%#x\n", iface, stream, option);
2203 if (!stream) return E_INVALIDARG;
2205 EnterCriticalSection(&This->lock);
2207 if (!This->initialized)
2209 IStream_AddRef(stream);
2210 This->stream = stream;
2211 This->initialized = TRUE;
2212 hr = S_OK;
2214 else
2215 hr = WINCODEC_ERR_WRONGSTATE;
2217 LeaveCriticalSection(&This->lock);
2219 return hr;
2222 static HRESULT WINAPI GifEncoder_GetContainerFormat(IWICBitmapEncoder *iface, GUID *format)
2224 if (!format) return E_INVALIDARG;
2226 *format = GUID_ContainerFormatGif;
2227 return S_OK;
2230 static HRESULT WINAPI GifEncoder_GetEncoderInfo(IWICBitmapEncoder *iface, IWICBitmapEncoderInfo **info)
2232 IWICComponentInfo *comp_info;
2233 HRESULT hr;
2235 TRACE("%p,%p\n", iface, info);
2237 if (!info) return E_INVALIDARG;
2239 hr = CreateComponentInfo(&CLSID_WICGifEncoder, &comp_info);
2240 if (hr == S_OK)
2242 hr = IWICComponentInfo_QueryInterface(comp_info, &IID_IWICBitmapEncoderInfo, (void **)info);
2243 IWICComponentInfo_Release(comp_info);
2245 return hr;
2248 static HRESULT WINAPI GifEncoder_SetColorContexts(IWICBitmapEncoder *iface, UINT count, IWICColorContext **context)
2250 FIXME("%p,%u,%p: stub\n", iface, count, context);
2251 return E_NOTIMPL;
2254 static HRESULT WINAPI GifEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *palette)
2256 GifEncoder *This = impl_from_IWICBitmapEncoder(iface);
2257 HRESULT hr;
2259 TRACE("%p,%p\n", iface, palette);
2261 if (!palette) return E_INVALIDARG;
2263 EnterCriticalSection(&This->lock);
2265 if (This->initialized)
2266 hr = IWICPalette_GetColors(palette, 256, This->palette, &This->colors);
2267 else
2268 hr = WINCODEC_ERR_NOTINITIALIZED;
2270 LeaveCriticalSection(&This->lock);
2271 return hr;
2274 static HRESULT WINAPI GifEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *thumbnail)
2276 TRACE("%p,%p\n", iface, thumbnail);
2277 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
2280 static HRESULT WINAPI GifEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *preview)
2282 TRACE("%p,%p\n", iface, preview);
2283 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
2286 static HRESULT WINAPI GifEncoderFrame_Block_QueryInterface(IWICMetadataBlockWriter *iface, REFIID iid, void **ppv)
2288 GifFrameEncode *frame_encoder = impl_from_IWICMetadataBlockWriter(iface);
2290 return IWICBitmapFrameEncode_QueryInterface(&frame_encoder->IWICBitmapFrameEncode_iface, iid, ppv);
2293 static ULONG WINAPI GifEncoderFrame_Block_AddRef(IWICMetadataBlockWriter *iface)
2295 GifFrameEncode *frame_encoder = impl_from_IWICMetadataBlockWriter(iface);
2297 return IWICBitmapFrameEncode_AddRef(&frame_encoder->IWICBitmapFrameEncode_iface);
2300 static ULONG WINAPI GifEncoderFrame_Block_Release(IWICMetadataBlockWriter *iface)
2302 GifFrameEncode *frame_encoder = impl_from_IWICMetadataBlockWriter(iface);
2304 return IWICBitmapFrameEncode_Release(&frame_encoder->IWICBitmapFrameEncode_iface);
2307 static HRESULT WINAPI GifEncoderFrame_Block_GetContainerFormat(IWICMetadataBlockWriter *iface, GUID *container_format)
2309 FIXME("iface %p, container_format %p stub.\n", iface, container_format);
2311 return E_NOTIMPL;
2314 static HRESULT WINAPI GifEncoderFrame_Block_GetCount(IWICMetadataBlockWriter *iface, UINT *count)
2316 FIXME("iface %p, count %p stub.\n", iface, count);
2318 return E_NOTIMPL;
2321 static HRESULT WINAPI GifEncoderFrame_Block_GetReaderByIndex(IWICMetadataBlockWriter *iface,
2322 UINT index, IWICMetadataReader **metadata_reader)
2324 FIXME("iface %p, index %d, metadata_reader %p stub.\n", iface, index, metadata_reader);
2326 return E_NOTIMPL;
2329 static HRESULT WINAPI GifEncoderFrame_Block_GetEnumerator(IWICMetadataBlockWriter *iface, IEnumUnknown **enum_metadata)
2331 FIXME("iface %p, enum_metadata %p stub.\n", iface, enum_metadata);
2333 return E_NOTIMPL;
2336 static HRESULT WINAPI GifEncoderFrame_Block_InitializeFromBlockReader(IWICMetadataBlockWriter *iface,
2337 IWICMetadataBlockReader *block_reader)
2339 FIXME("iface %p, block_reader %p stub.\n", iface, block_reader);
2341 return E_NOTIMPL;
2344 static HRESULT WINAPI GifEncoderFrame_Block_GetWriterByIndex(IWICMetadataBlockWriter *iface, UINT index,
2345 IWICMetadataWriter **metadata_writer)
2347 FIXME("iface %p, index %u, metadata_writer %p stub.\n", iface, index, metadata_writer);
2349 return E_NOTIMPL;
2352 static HRESULT WINAPI GifEncoderFrame_Block_AddWriter(IWICMetadataBlockWriter *iface, IWICMetadataWriter *metadata_writer)
2354 FIXME("iface %p, metadata_writer %p stub.\n", iface, metadata_writer);
2356 return E_NOTIMPL;
2359 static HRESULT WINAPI GifEncoderFrame_Block_SetWriterByIndex(IWICMetadataBlockWriter *iface, UINT index,
2360 IWICMetadataWriter *metadata_writer)
2362 FIXME("iface %p, index %u, metadata_writer %p stub.\n", iface, index, metadata_writer);
2364 return E_NOTIMPL;
2367 static HRESULT WINAPI GifEncoderFrame_Block_RemoveWriterByIndex(IWICMetadataBlockWriter *iface, UINT index)
2369 FIXME("iface %p, index %u stub.\n", iface, index);
2371 return E_NOTIMPL;
2374 static const IWICMetadataBlockWriterVtbl GifFrameEncode_BlockVtbl = {
2375 GifEncoderFrame_Block_QueryInterface,
2376 GifEncoderFrame_Block_AddRef,
2377 GifEncoderFrame_Block_Release,
2378 GifEncoderFrame_Block_GetContainerFormat,
2379 GifEncoderFrame_Block_GetCount,
2380 GifEncoderFrame_Block_GetReaderByIndex,
2381 GifEncoderFrame_Block_GetEnumerator,
2382 GifEncoderFrame_Block_InitializeFromBlockReader,
2383 GifEncoderFrame_Block_GetWriterByIndex,
2384 GifEncoderFrame_Block_AddWriter,
2385 GifEncoderFrame_Block_SetWriterByIndex,
2386 GifEncoderFrame_Block_RemoveWriterByIndex,
2389 static HRESULT WINAPI GifEncoder_CreateNewFrame(IWICBitmapEncoder *iface, IWICBitmapFrameEncode **frame, IPropertyBag2 **options)
2391 GifEncoder *This = impl_from_IWICBitmapEncoder(iface);
2392 HRESULT hr;
2394 TRACE("%p,%p,%p\n", iface, frame, options);
2396 if (!frame) return E_INVALIDARG;
2398 EnterCriticalSection(&This->lock);
2400 if (This->initialized && !This->committed)
2402 GifFrameEncode *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(*ret));
2403 if (ret)
2405 This->n_frames++;
2407 ret->IWICBitmapFrameEncode_iface.lpVtbl = &GifFrameEncode_Vtbl;
2408 ret->IWICMetadataBlockWriter_iface.lpVtbl = &GifFrameEncode_BlockVtbl;
2410 ret->ref = 1;
2411 ret->encoder = This;
2412 ret->initialized = FALSE;
2413 ret->interlace = FALSE; /* FIXME: read from the properties */
2414 ret->committed = FALSE;
2415 ret->width = 0;
2416 ret->height = 0;
2417 ret->lines = 0;
2418 ret->xres = 0.0;
2419 ret->yres = 0.0;
2420 ret->colors = 0;
2421 ret->image_data = NULL;
2422 IWICBitmapEncoder_AddRef(iface);
2423 *frame = &ret->IWICBitmapFrameEncode_iface;
2425 hr = S_OK;
2427 if (options)
2429 hr = CreatePropertyBag2(NULL, 0, options);
2430 if (hr != S_OK)
2432 IWICBitmapFrameEncode_Release(*frame);
2433 *frame = NULL;
2437 else
2438 hr = E_OUTOFMEMORY;
2440 else
2441 hr = WINCODEC_ERR_WRONGSTATE;
2443 LeaveCriticalSection(&This->lock);
2445 return hr;
2449 static HRESULT WINAPI GifEncoder_Commit(IWICBitmapEncoder *iface)
2451 GifEncoder *This = impl_from_IWICBitmapEncoder(iface);
2452 HRESULT hr;
2454 TRACE("%p\n", iface);
2456 EnterCriticalSection(&This->lock);
2458 if (This->initialized && !This->committed)
2460 char gif_trailer = 0x3b;
2462 /* FIXME: write text, comment GIF extensions */
2464 hr = IStream_Write(This->stream, &gif_trailer, sizeof(gif_trailer), NULL);
2465 if (hr == S_OK)
2466 This->committed = TRUE;
2468 else
2469 hr = WINCODEC_ERR_WRONGSTATE;
2471 LeaveCriticalSection(&This->lock);
2472 return hr;
2475 static HRESULT WINAPI GifEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface, IWICMetadataQueryWriter **writer)
2477 FIXME("%p,%p: stub\n", iface, writer);
2478 return E_NOTIMPL;
2481 static const IWICBitmapEncoderVtbl GifEncoder_Vtbl =
2483 GifEncoder_QueryInterface,
2484 GifEncoder_AddRef,
2485 GifEncoder_Release,
2486 GifEncoder_Initialize,
2487 GifEncoder_GetContainerFormat,
2488 GifEncoder_GetEncoderInfo,
2489 GifEncoder_SetColorContexts,
2490 GifEncoder_SetPalette,
2491 GifEncoder_SetThumbnail,
2492 GifEncoder_SetPreview,
2493 GifEncoder_CreateNewFrame,
2494 GifEncoder_Commit,
2495 GifEncoder_GetMetadataQueryWriter
2498 HRESULT GifEncoder_CreateInstance(REFIID iid, void **ppv)
2500 GifEncoder *This;
2501 HRESULT ret;
2503 TRACE("%s,%p\n", debugstr_guid(iid), ppv);
2505 *ppv = NULL;
2507 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
2508 if (!This) return E_OUTOFMEMORY;
2510 This->IWICBitmapEncoder_iface.lpVtbl = &GifEncoder_Vtbl;
2511 This->ref = 1;
2512 This->stream = NULL;
2513 InitializeCriticalSection(&This->lock);
2514 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": GifEncoder.lock");
2515 This->initialized = FALSE;
2516 This->info_written = FALSE;
2517 This->committed = FALSE;
2518 This->n_frames = 0;
2519 This->colors = 0;
2521 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
2522 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
2524 return ret;