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
25 #define NONAMELESSUNION
31 #include "wincodecsdk.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
);
49 static HRESULT
load_LSD_metadata(IStream
*stream
, const GUID
*vendor
, DWORD options
,
50 MetadataItem
**items
, DWORD
*count
)
53 struct logical_screen_descriptor
59 /* global_color_table_flag : 1;
60 * color_resolution : 3;
62 * global_color_table_size : 3;
64 BYTE background_color_index
;
65 BYTE pixel_aspect_ratio
;
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(), 0, 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
;
141 static const MetadataHandlerVtbl LSDReader_Vtbl
= {
143 &CLSID_WICLSDMetadataReader
,
147 HRESULT
LSDReader_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void **ppv
)
149 return MetadataReader_Create(&LSDReader_Vtbl
, pUnkOuter
, iid
, ppv
);
152 #include "pshpack1.h"
153 struct image_descriptor
160 /* local_color_table_flag : 1;
161 * interlace_flag : 1;
164 * local_color_table_size : 3;
169 static HRESULT
load_IMD_metadata(IStream
*stream
, const GUID
*vendor
, DWORD options
,
170 MetadataItem
**items
, DWORD
*count
)
172 struct image_descriptor imd_data
;
175 MetadataItem
*result
;
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(), 0, 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;
239 static const MetadataHandlerVtbl IMDReader_Vtbl
= {
241 &CLSID_WICIMDMetadataReader
,
245 HRESULT
IMDReader_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void **ppv
)
247 return MetadataReader_Create(&IMDReader_Vtbl
, pUnkOuter
, 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
259 * user_input_flag : 1;
260 * transparency_flag : 1;
263 BYTE transparent_color_index
;
268 MetadataItem
*result
;
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(), 0, 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
;
317 static const MetadataHandlerVtbl GCEReader_Vtbl
= {
319 &CLSID_WICGCEMetadataReader
,
323 HRESULT
GCEReader_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void **ppv
)
325 return MetadataReader_Create(&GCEReader_Vtbl
, pUnkOuter
, 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
;
337 BYTE application
[11];
341 ULONG bytesread
, data_size
, i
;
342 MetadataItem
*result
;
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 || ape_data
.extension_label
!= 0xff ||
352 ape_data
.block_size
!= 11)
360 hr
= IStream_Read(stream
, &subblock_size
, sizeof(subblock_size
), &bytesread
);
361 if (FAILED(hr
) || bytesread
!= sizeof(subblock_size
))
363 HeapFree(GetProcessHeap(), 0, data
);
366 if (!subblock_size
) break;
369 data
= HeapAlloc(GetProcessHeap(), 0, subblock_size
+ 1);
372 BYTE
*new_data
= HeapReAlloc(GetProcessHeap(), 0, data
, data_size
+ subblock_size
+ 1);
375 HeapFree(GetProcessHeap(), 0, data
);
380 data
[data_size
] = subblock_size
;
381 hr
= IStream_Read(stream
, data
+ data_size
+ 1, subblock_size
, &bytesread
);
382 if (FAILED(hr
) || bytesread
!= subblock_size
)
384 HeapFree(GetProcessHeap(), 0, data
);
387 data_size
+= subblock_size
+ 1;
390 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(MetadataItem
) * 2);
393 HeapFree(GetProcessHeap(), 0, data
);
394 return E_OUTOFMEMORY
;
397 for (i
= 0; i
< 2; i
++)
399 PropVariantInit(&result
[i
].schema
);
400 PropVariantInit(&result
[i
].id
);
401 PropVariantInit(&result
[i
].value
);
404 result
[0].id
.vt
= VT_LPWSTR
;
405 result
[0].id
.u
.pwszVal
= strdupAtoW("Application");
406 result
[0].value
.vt
= VT_UI1
|VT_VECTOR
;
407 result
[0].value
.u
.caub
.cElems
= sizeof(ape_data
.application
);
408 result
[0].value
.u
.caub
.pElems
= HeapAlloc(GetProcessHeap(), 0, sizeof(ape_data
.application
));
409 memcpy(result
[0].value
.u
.caub
.pElems
, ape_data
.application
, sizeof(ape_data
.application
));
411 result
[1].id
.vt
= VT_LPWSTR
;
412 result
[1].id
.u
.pwszVal
= strdupAtoW("Data");
413 result
[1].value
.vt
= VT_UI1
|VT_VECTOR
;
414 result
[1].value
.u
.caub
.cElems
= data_size
;
415 result
[1].value
.u
.caub
.pElems
= HeapAlloc(GetProcessHeap(), 0, data_size
);
416 memcpy(result
[1].value
.u
.caub
.pElems
, data
, data_size
);
418 HeapFree(GetProcessHeap(), 0, data
);
426 static const MetadataHandlerVtbl APEReader_Vtbl
= {
428 &CLSID_WICAPEMetadataReader
,
432 HRESULT
APEReader_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void **ppv
)
434 return MetadataReader_Create(&APEReader_Vtbl
, pUnkOuter
, iid
, ppv
);
437 static IStream
*create_stream(const void *data
, int data_size
)
444 hdata
= GlobalAlloc(GMEM_MOVEABLE
, data_size
);
445 if (!hdata
) return NULL
;
447 locked_data
= GlobalLock(hdata
);
448 memcpy(locked_data
, data
, data_size
);
451 hr
= CreateStreamOnHGlobal(hdata
, TRUE
, &stream
);
452 return FAILED(hr
) ? NULL
: stream
;
456 IWICBitmapDecoder IWICBitmapDecoder_iface
;
457 IWICMetadataBlockReader IWICMetadataBlockReader_iface
;
458 BYTE LSD_data
[13]; /* Logical Screen Descriptor */
462 CRITICAL_SECTION lock
;
466 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
467 IWICMetadataBlockReader IWICMetadataBlockReader_iface
;
473 static inline GifDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
475 return CONTAINING_RECORD(iface
, GifDecoder
, IWICBitmapDecoder_iface
);
478 static inline GifDecoder
*impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader
*iface
)
480 return CONTAINING_RECORD(iface
, GifDecoder
, IWICMetadataBlockReader_iface
);
483 static inline GifFrameDecode
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
485 return CONTAINING_RECORD(iface
, GifFrameDecode
, IWICBitmapFrameDecode_iface
);
488 static inline GifFrameDecode
*frame_from_IWICMetadataBlockReader(IWICMetadataBlockReader
*iface
)
490 return CONTAINING_RECORD(iface
, GifFrameDecode
, IWICMetadataBlockReader_iface
);
493 static HRESULT WINAPI
GifFrameDecode_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
496 GifFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
497 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
499 if (!ppv
) return E_INVALIDARG
;
501 if (IsEqualIID(&IID_IUnknown
, iid
) ||
502 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
503 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
505 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
507 else if (IsEqualIID(&IID_IWICMetadataBlockReader
, iid
))
509 *ppv
= &This
->IWICMetadataBlockReader_iface
;
514 return E_NOINTERFACE
;
517 IUnknown_AddRef((IUnknown
*)*ppv
);
521 static ULONG WINAPI
GifFrameDecode_AddRef(IWICBitmapFrameDecode
*iface
)
523 GifFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
524 ULONG ref
= InterlockedIncrement(&This
->ref
);
526 TRACE("(%p) refcount=%u\n", iface
, ref
);
531 static ULONG WINAPI
GifFrameDecode_Release(IWICBitmapFrameDecode
*iface
)
533 GifFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
534 ULONG ref
= InterlockedDecrement(&This
->ref
);
536 TRACE("(%p) refcount=%u\n", iface
, ref
);
540 IUnknown_Release((IUnknown
*)This
->parent
);
541 HeapFree(GetProcessHeap(), 0, This
);
547 static HRESULT WINAPI
GifFrameDecode_GetSize(IWICBitmapFrameDecode
*iface
,
548 UINT
*puiWidth
, UINT
*puiHeight
)
550 GifFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
551 TRACE("(%p,%p,%p)\n", iface
, puiWidth
, puiHeight
);
553 *puiWidth
= This
->frame
->ImageDesc
.Width
;
554 *puiHeight
= This
->frame
->ImageDesc
.Height
;
559 static HRESULT WINAPI
GifFrameDecode_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
560 WICPixelFormatGUID
*pPixelFormat
)
562 memcpy(pPixelFormat
, &GUID_WICPixelFormat8bppIndexed
, sizeof(GUID
));
567 static HRESULT WINAPI
GifFrameDecode_GetResolution(IWICBitmapFrameDecode
*iface
,
568 double *pDpiX
, double *pDpiY
)
570 GifFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
571 const GifWord aspect_word
= This
->parent
->gif
->SAspectRatio
;
572 const double aspect
= (aspect_word
> 0) ? ((aspect_word
+ 15.0) / 64.0) : 1.0;
573 TRACE("(%p,%p,%p)\n", iface
, pDpiX
, pDpiY
);
575 *pDpiX
= 96.0 / aspect
;
581 static HRESULT WINAPI
GifFrameDecode_CopyPalette(IWICBitmapFrameDecode
*iface
,
582 IWICPalette
*pIPalette
)
584 GifFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
585 WICColor colors
[256];
586 ColorMapObject
*cm
= This
->frame
->ImageDesc
.ColorMap
;
589 TRACE("(%p,%p)\n", iface
, pIPalette
);
591 if (!cm
) cm
= This
->parent
->gif
->SColorMap
;
593 if (cm
->ColorCount
> 256)
595 ERR("GIF contains %i colors???\n", cm
->ColorCount
);
599 for (i
= 0; i
< cm
->ColorCount
; i
++) {
600 colors
[i
] = 0xff000000| /* alpha */
601 cm
->Colors
[i
].Red
<< 16|
602 cm
->Colors
[i
].Green
<< 8|
606 /* look for the transparent color extension */
607 for (i
= 0; i
< This
->frame
->ExtensionBlockCount
; ++i
) {
608 eb
= This
->frame
->ExtensionBlocks
+ i
;
609 if (eb
->Function
== 0xF9 && eb
->ByteCount
== 4) {
610 if ((eb
->Bytes
[0] & 1) == 1) {
611 trans
= (unsigned char)eb
->Bytes
[3];
612 colors
[trans
] &= 0xffffff; /* set alpha to 0 */
618 IWICPalette_InitializeCustom(pIPalette
, colors
, cm
->ColorCount
);
623 static HRESULT
copy_interlaced_pixels(const BYTE
*srcbuffer
,
624 UINT srcwidth
, UINT srcheight
, INT srcstride
, const WICRect
*rc
,
625 UINT dststride
, UINT dstbuffersize
, BYTE
*dstbuffer
)
627 UINT row_offset
; /* number of bytes into the source rows where the data starts */
637 rect
.Width
= srcwidth
;
638 rect
.Height
= srcheight
;
643 if (rc
->X
< 0 || rc
->Y
< 0 || rc
->X
+rc
->Width
> srcwidth
|| rc
->Y
+rc
->Height
> srcheight
)
647 if (dststride
< rc
->Width
)
650 if ((dststride
* rc
->Height
) > dstbuffersize
)
656 for (y
=rc
->Y
; y
-rc
->Y
< rc
->Height
; y
++)
659 src
= srcbuffer
+ srcstride
* (y
/8);
661 src
= srcbuffer
+ srcstride
* ((srcheight
+7)/8 + y
/8);
663 src
= srcbuffer
+ srcstride
* ((srcheight
+3)/4 + y
/4);
665 src
= srcbuffer
+ srcstride
* ((srcheight
+1)/2 + y
/2);
667 memcpy(dst
, src
, rc
->Width
);
673 static HRESULT WINAPI
GifFrameDecode_CopyPixels(IWICBitmapFrameDecode
*iface
,
674 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
676 GifFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
677 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
679 if (This
->frame
->ImageDesc
.Interlace
)
681 return copy_interlaced_pixels(This
->frame
->RasterBits
, This
->frame
->ImageDesc
.Width
,
682 This
->frame
->ImageDesc
.Height
, This
->frame
->ImageDesc
.Width
,
683 prc
, cbStride
, cbBufferSize
, pbBuffer
);
687 return copy_pixels(8, This
->frame
->RasterBits
, This
->frame
->ImageDesc
.Width
,
688 This
->frame
->ImageDesc
.Height
, This
->frame
->ImageDesc
.Width
,
689 prc
, cbStride
, cbBufferSize
, pbBuffer
);
693 static HRESULT WINAPI
GifFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
694 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
696 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
697 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
700 static HRESULT WINAPI
GifFrameDecode_GetColorContexts(IWICBitmapFrameDecode
*iface
,
701 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
703 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
704 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
707 static HRESULT WINAPI
GifFrameDecode_GetThumbnail(IWICBitmapFrameDecode
*iface
,
708 IWICBitmapSource
**ppIThumbnail
)
710 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
711 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
714 static const IWICBitmapFrameDecodeVtbl GifFrameDecode_Vtbl
= {
715 GifFrameDecode_QueryInterface
,
716 GifFrameDecode_AddRef
,
717 GifFrameDecode_Release
,
718 GifFrameDecode_GetSize
,
719 GifFrameDecode_GetPixelFormat
,
720 GifFrameDecode_GetResolution
,
721 GifFrameDecode_CopyPalette
,
722 GifFrameDecode_CopyPixels
,
723 GifFrameDecode_GetMetadataQueryReader
,
724 GifFrameDecode_GetColorContexts
,
725 GifFrameDecode_GetThumbnail
728 static HRESULT WINAPI
GifFrameDecode_Block_QueryInterface(IWICMetadataBlockReader
*iface
,
729 REFIID iid
, void **ppv
)
731 GifFrameDecode
*This
= frame_from_IWICMetadataBlockReader(iface
);
732 return IWICBitmapFrameDecode_QueryInterface(&This
->IWICBitmapFrameDecode_iface
, iid
, ppv
);
735 static ULONG WINAPI
GifFrameDecode_Block_AddRef(IWICMetadataBlockReader
*iface
)
737 GifFrameDecode
*This
= frame_from_IWICMetadataBlockReader(iface
);
738 return IWICBitmapFrameDecode_AddRef(&This
->IWICBitmapFrameDecode_iface
);
741 static ULONG WINAPI
GifFrameDecode_Block_Release(IWICMetadataBlockReader
*iface
)
743 GifFrameDecode
*This
= frame_from_IWICMetadataBlockReader(iface
);
744 return IWICBitmapFrameDecode_Release(&This
->IWICBitmapFrameDecode_iface
);
747 static HRESULT WINAPI
GifFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader
*iface
,
750 TRACE("(%p,%p)\n", iface
, guid
);
752 if (!guid
) return E_INVALIDARG
;
754 *guid
= GUID_ContainerFormatGif
;
758 static const void *get_GCE_data(GifFrameDecode
*This
)
762 for (i
= 0; i
< This
->frame
->ExtensionBlockCount
; i
++)
764 if (This
->frame
->ExtensionBlocks
[i
].Function
== GRAPHICS_EXT_FUNC_CODE
&&
765 This
->frame
->ExtensionBlocks
[i
].ByteCount
== 4)
766 return This
->frame
->ExtensionBlocks
[i
].Bytes
;
771 static HRESULT WINAPI
GifFrameDecode_Block_GetCount(IWICMetadataBlockReader
*iface
,
774 GifFrameDecode
*This
= frame_from_IWICMetadataBlockReader(iface
);
776 TRACE("%p,%p\n", iface
, count
);
778 if (!count
) return E_INVALIDARG
;
781 if (get_GCE_data(This
)) *count
+= 1;
785 static HRESULT
create_IMD_metadata_reader(GifFrameDecode
*This
, IWICMetadataReader
**reader
)
788 IWICMetadataReader
*metadata_reader
;
789 IWICPersistStream
*persist
;
791 struct image_descriptor IMD_data
;
793 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
795 hr
= CoCreateInstance(&CLSID_WICIMDMetadataReader
, NULL
, CLSCTX_INPROC_SERVER
,
796 &IID_IWICMetadataReader
, (void **)&metadata_reader
);
797 if (FAILED(hr
)) return hr
;
799 hr
= IWICMetadataReader_QueryInterface(metadata_reader
, &IID_IWICPersistStream
, (void **)&persist
);
802 IWICMetadataReader_Release(metadata_reader
);
806 /* recreate IMD structure from GIF decoder data */
807 IMD_data
.left
= This
->frame
->ImageDesc
.Left
;
808 IMD_data
.top
= This
->frame
->ImageDesc
.Top
;
809 IMD_data
.width
= This
->frame
->ImageDesc
.Width
;
810 IMD_data
.height
= This
->frame
->ImageDesc
.Height
;
813 IMD_data
.packed
|= This
->frame
->ImageDesc
.Interlace
? (1 << 6) : 0;
814 if (This
->frame
->ImageDesc
.ColorMap
)
816 /* local_color_table_flag */
817 IMD_data
.packed
|= 1 << 7;
818 /* local_color_table_size */
819 IMD_data
.packed
|= This
->frame
->ImageDesc
.ColorMap
->BitsPerPixel
- 1;
820 /* FIXME: sort_flag */
823 stream
= create_stream(&IMD_data
, sizeof(IMD_data
));
824 IWICPersistStream_LoadEx(persist
, stream
, NULL
, WICPersistOptionsDefault
);
825 IStream_Release(stream
);
827 IWICPersistStream_Release(persist
);
829 *reader
= metadata_reader
;
833 static HRESULT
create_GCE_metadata_reader(const void *GCE_data
, IWICMetadataReader
**reader
)
836 IWICMetadataReader
*metadata_reader
;
837 IWICPersistStream
*persist
;
840 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
842 hr
= CoCreateInstance(&CLSID_WICGCEMetadataReader
, NULL
, CLSCTX_INPROC_SERVER
,
843 &IID_IWICMetadataReader
, (void **)&metadata_reader
);
844 if (FAILED(hr
)) return hr
;
846 hr
= IWICMetadataReader_QueryInterface(metadata_reader
, &IID_IWICPersistStream
, (void **)&persist
);
849 IWICMetadataReader_Release(metadata_reader
);
853 stream
= create_stream(GCE_data
, 4);
854 IWICPersistStream_LoadEx(persist
, stream
, NULL
, WICPersistOptionsDefault
);
855 IStream_Release(stream
);
857 IWICPersistStream_Release(persist
);
859 *reader
= metadata_reader
;
863 static HRESULT WINAPI
GifFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader
*iface
,
864 UINT index
, IWICMetadataReader
**reader
)
866 GifFrameDecode
*This
= frame_from_IWICMetadataBlockReader(iface
);
867 UINT block_count
= 1;
868 const void *GCE_data
;
870 TRACE("(%p,%u,%p)\n", iface
, index
, reader
);
872 GCE_data
= get_GCE_data(This
);
873 if (GCE_data
) block_count
++;
874 /* FIXME: add support for Application Extension metadata block
875 APE_data = get_APE_data(This);
876 if (APE_data) block_count++;
878 if (!reader
|| index
>= block_count
) return E_INVALIDARG
;
881 return create_IMD_metadata_reader(This
, reader
);
883 if (index
== 1 && GCE_data
)
884 return create_GCE_metadata_reader(GCE_data
, reader
);
886 /* FIXME: add support for Application Extension metadata block
888 return create_APE_metadata_reader(APE_data, reader);
893 static HRESULT WINAPI
GifFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader
*iface
,
894 IEnumUnknown
**enumerator
)
896 FIXME("(%p,%p): stub\n", iface
, enumerator
);
900 static const IWICMetadataBlockReaderVtbl GifFrameDecode_BlockVtbl
=
902 GifFrameDecode_Block_QueryInterface
,
903 GifFrameDecode_Block_AddRef
,
904 GifFrameDecode_Block_Release
,
905 GifFrameDecode_Block_GetContainerFormat
,
906 GifFrameDecode_Block_GetCount
,
907 GifFrameDecode_Block_GetReaderByIndex
,
908 GifFrameDecode_Block_GetEnumerator
911 static HRESULT WINAPI
GifDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
914 GifDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
915 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
917 if (!ppv
) return E_INVALIDARG
;
919 if (IsEqualIID(&IID_IUnknown
, iid
) ||
920 IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
922 *ppv
= &This
->IWICBitmapDecoder_iface
;
924 else if (IsEqualIID(&IID_IWICMetadataBlockReader
, iid
))
926 *ppv
= &This
->IWICMetadataBlockReader_iface
;
931 return E_NOINTERFACE
;
934 IUnknown_AddRef((IUnknown
*)*ppv
);
938 static ULONG WINAPI
GifDecoder_AddRef(IWICBitmapDecoder
*iface
)
940 GifDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
941 ULONG ref
= InterlockedIncrement(&This
->ref
);
943 TRACE("(%p) refcount=%u\n", iface
, ref
);
948 static ULONG WINAPI
GifDecoder_Release(IWICBitmapDecoder
*iface
)
950 GifDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
951 ULONG ref
= InterlockedDecrement(&This
->ref
);
953 TRACE("(%p) refcount=%u\n", iface
, ref
);
957 This
->lock
.DebugInfo
->Spare
[0] = 0;
958 DeleteCriticalSection(&This
->lock
);
959 DGifCloseFile(This
->gif
);
960 HeapFree(GetProcessHeap(), 0, This
);
966 static HRESULT WINAPI
GifDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
967 DWORD
*pdwCapability
)
969 FIXME("(%p,%p,%p): stub\n", iface
, pIStream
, pdwCapability
);
973 static int _gif_inputfunc(GifFileType
*gif
, GifByteType
*data
, int len
) {
974 IStream
*stream
= gif
->UserData
;
980 ERR("attempting to read file after initialization\n");
984 hr
= IStream_Read(stream
, data
, len
, &bytesread
);
985 if (hr
!= S_OK
) bytesread
= 0;
989 static HRESULT WINAPI
GifDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
990 WICDecodeOptions cacheOptions
)
992 GifDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
996 TRACE("(%p,%p,%x)\n", iface
, pIStream
, cacheOptions
);
998 EnterCriticalSection(&This
->lock
);
1000 if (This
->initialized
|| This
->gif
)
1002 WARN("already initialized\n");
1003 LeaveCriticalSection(&This
->lock
);
1004 return WINCODEC_ERR_WRONGSTATE
;
1007 /* seek to start of stream */
1009 IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
1011 /* read all data from the stream */
1012 This
->gif
= DGifOpen((void*)pIStream
, _gif_inputfunc
);
1015 LeaveCriticalSection(&This
->lock
);
1019 ret
= DGifSlurp(This
->gif
);
1020 if (ret
== GIF_ERROR
)
1022 LeaveCriticalSection(&This
->lock
);
1026 /* make sure we don't use the stream after this method returns */
1027 This
->gif
->UserData
= NULL
;
1030 IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
1031 IStream_Read(pIStream
, &This
->LSD_data
, sizeof(This
->LSD_data
), NULL
);
1033 This
->initialized
= TRUE
;
1035 LeaveCriticalSection(&This
->lock
);
1040 static HRESULT WINAPI
GifDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
1041 GUID
*pguidContainerFormat
)
1043 memcpy(pguidContainerFormat
, &GUID_ContainerFormatGif
, sizeof(GUID
));
1047 static HRESULT WINAPI
GifDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
1048 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
1051 IWICComponentInfo
*compinfo
;
1053 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
1055 hr
= CreateComponentInfo(&CLSID_WICGifDecoder
, &compinfo
);
1056 if (FAILED(hr
)) return hr
;
1058 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
1059 (void**)ppIDecoderInfo
);
1061 IWICComponentInfo_Release(compinfo
);
1066 static HRESULT WINAPI
GifDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
1067 IWICPalette
*pIPalette
)
1069 TRACE("(%p,%p)\n", iface
, pIPalette
);
1070 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
1073 static HRESULT WINAPI
GifDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
1074 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
1076 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
1077 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1080 static HRESULT WINAPI
GifDecoder_GetPreview(IWICBitmapDecoder
*iface
,
1081 IWICBitmapSource
**ppIBitmapSource
)
1083 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
1084 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1087 static HRESULT WINAPI
GifDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
1088 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
1090 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
1091 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1094 static HRESULT WINAPI
GifDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
1095 IWICBitmapSource
**ppIThumbnail
)
1097 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
1098 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
1101 static HRESULT WINAPI
GifDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
1104 GifDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1105 TRACE("(%p,%p)\n", iface
, pCount
);
1107 if (!This
->initialized
) return WINCODEC_ERR_NOTINITIALIZED
;
1109 *pCount
= This
->gif
->ImageCount
;
1111 TRACE("<- %u\n", *pCount
);
1116 static HRESULT WINAPI
GifDecoder_GetFrame(IWICBitmapDecoder
*iface
,
1117 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
1119 GifDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
1120 GifFrameDecode
*result
;
1121 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
1123 if (!This
->initialized
) return WINCODEC_ERR_NOTINITIALIZED
;
1125 if (index
>= This
->gif
->ImageCount
) return E_INVALIDARG
;
1127 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(GifFrameDecode
));
1128 if (!result
) return E_OUTOFMEMORY
;
1130 result
->IWICBitmapFrameDecode_iface
.lpVtbl
= &GifFrameDecode_Vtbl
;
1131 result
->IWICMetadataBlockReader_iface
.lpVtbl
= &GifFrameDecode_BlockVtbl
;
1133 result
->frame
= &This
->gif
->SavedImages
[index
];
1134 IWICBitmapDecoder_AddRef(iface
);
1135 result
->parent
= This
;
1137 *ppIBitmapFrame
= &result
->IWICBitmapFrameDecode_iface
;
1142 static const IWICBitmapDecoderVtbl GifDecoder_Vtbl
= {
1143 GifDecoder_QueryInterface
,
1146 GifDecoder_QueryCapability
,
1147 GifDecoder_Initialize
,
1148 GifDecoder_GetContainerFormat
,
1149 GifDecoder_GetDecoderInfo
,
1150 GifDecoder_CopyPalette
,
1151 GifDecoder_GetMetadataQueryReader
,
1152 GifDecoder_GetPreview
,
1153 GifDecoder_GetColorContexts
,
1154 GifDecoder_GetThumbnail
,
1155 GifDecoder_GetFrameCount
,
1159 static HRESULT WINAPI
GifDecoder_Block_QueryInterface(IWICMetadataBlockReader
*iface
,
1160 REFIID iid
, void **ppv
)
1162 GifDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
1163 return IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1166 static ULONG WINAPI
GifDecoder_Block_AddRef(IWICMetadataBlockReader
*iface
)
1168 GifDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
1169 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
1172 static ULONG WINAPI
GifDecoder_Block_Release(IWICMetadataBlockReader
*iface
)
1174 GifDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
1175 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1178 static HRESULT WINAPI
GifDecoder_Block_GetContainerFormat(IWICMetadataBlockReader
*iface
,
1181 TRACE("(%p,%p)\n", iface
, guid
);
1183 if (!guid
) return E_INVALIDARG
;
1185 *guid
= GUID_ContainerFormatGif
;
1189 static HRESULT WINAPI
GifDecoder_Block_GetCount(IWICMetadataBlockReader
*iface
,
1192 TRACE("%p,%p\n", iface
, count
);
1194 if (!count
) return E_INVALIDARG
;
1200 static HRESULT
create_LSD_metadata_reader(GifDecoder
*This
, IWICMetadataReader
**reader
)
1203 IWICMetadataReader
*metadata_reader
;
1204 IWICPersistStream
*persist
;
1207 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
1209 hr
= CoCreateInstance(&CLSID_WICLSDMetadataReader
, NULL
, CLSCTX_INPROC_SERVER
,
1210 &IID_IWICMetadataReader
, (void **)&metadata_reader
);
1211 if (FAILED(hr
)) return hr
;
1213 hr
= IWICMetadataReader_QueryInterface(metadata_reader
, &IID_IWICPersistStream
, (void **)&persist
);
1216 IWICMetadataReader_Release(metadata_reader
);
1220 stream
= create_stream(This
->LSD_data
, sizeof(This
->LSD_data
));
1221 IWICPersistStream_LoadEx(persist
, stream
, NULL
, WICPersistOptionsDefault
);
1222 IStream_Release(stream
);
1224 IWICPersistStream_Release(persist
);
1226 *reader
= metadata_reader
;
1230 static HRESULT WINAPI
GifDecoder_Block_GetReaderByIndex(IWICMetadataBlockReader
*iface
,
1231 UINT index
, IWICMetadataReader
**reader
)
1233 GifDecoder
*This
= impl_from_IWICMetadataBlockReader(iface
);
1235 TRACE("(%p,%u,%p)\n", iface
, index
, reader
);
1237 if (!reader
|| index
!= 0) return E_INVALIDARG
;
1239 return create_LSD_metadata_reader(This
, reader
);
1242 static HRESULT WINAPI
GifDecoder_Block_GetEnumerator(IWICMetadataBlockReader
*iface
,
1243 IEnumUnknown
**enumerator
)
1245 FIXME("(%p,%p): stub\n", iface
, enumerator
);
1249 static const IWICMetadataBlockReaderVtbl GifDecoder_BlockVtbl
=
1251 GifDecoder_Block_QueryInterface
,
1252 GifDecoder_Block_AddRef
,
1253 GifDecoder_Block_Release
,
1254 GifDecoder_Block_GetContainerFormat
,
1255 GifDecoder_Block_GetCount
,
1256 GifDecoder_Block_GetReaderByIndex
,
1257 GifDecoder_Block_GetEnumerator
1260 HRESULT
GifDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1265 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
1269 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
1271 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(GifDecoder
));
1272 if (!This
) return E_OUTOFMEMORY
;
1274 This
->IWICBitmapDecoder_iface
.lpVtbl
= &GifDecoder_Vtbl
;
1275 This
->IWICMetadataBlockReader_iface
.lpVtbl
= &GifDecoder_BlockVtbl
;
1277 This
->initialized
= FALSE
;
1279 InitializeCriticalSection(&This
->lock
);
1280 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": GifDecoder.lock");
1282 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1283 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);