windowscodecs: Add support for 8bpp grayscale TIFF with 8bpp alpha.
[wine.git] / dlls / windowscodecs / tiffformat.c
blob51e940f65ce381bd68d3ae06b1b33408c03c36bb
1 /*
2 * Copyright 2010 Vincent Povirk for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdarg.h>
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #ifdef HAVE_TIFFIO_H
27 #include <tiffio.h>
28 #endif
30 #define COBJMACROS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "objbase.h"
35 #include "wincodec.h"
36 #include "wincodecsdk.h"
38 #include "wincodecs_private.h"
40 #include "wine/debug.h"
41 #include "wine/library.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
45 #ifdef SONAME_LIBTIFF
47 /* Workaround for broken libtiff 4.x headers on some 64-bit hosts which
48 * define TIFF_UINT64_T/toff_t as 32-bit for 32-bit builds, while they
49 * are supposed to be always 64-bit.
50 * TIFF_UINT64_T doesn't exist in libtiff 3.x, it was introduced in 4.x.
52 #ifdef TIFF_UINT64_T
53 # undef toff_t
54 # define toff_t UINT64
55 #endif
57 static CRITICAL_SECTION init_tiff_cs;
58 static CRITICAL_SECTION_DEBUG init_tiff_cs_debug =
60 0, 0, &init_tiff_cs,
61 { &init_tiff_cs_debug.ProcessLocksList,
62 &init_tiff_cs_debug.ProcessLocksList },
63 0, 0, { (DWORD_PTR)(__FILE__ ": init_tiff_cs") }
65 static CRITICAL_SECTION init_tiff_cs = { &init_tiff_cs_debug, -1, 0, 0, 0, 0 };
67 static const WCHAR wszTiffCompressionMethod[] = {'T','i','f','f','C','o','m','p','r','e','s','s','i','o','n','M','e','t','h','o','d',0};
68 static const WCHAR wszCompressionQuality[] = {'C','o','m','p','r','e','s','s','i','o','n','Q','u','a','l','i','t','y',0};
70 static void *libtiff_handle;
71 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
72 MAKE_FUNCPTR(TIFFClientOpen);
73 MAKE_FUNCPTR(TIFFClose);
74 MAKE_FUNCPTR(TIFFCurrentDirOffset);
75 MAKE_FUNCPTR(TIFFGetField);
76 MAKE_FUNCPTR(TIFFIsByteSwapped);
77 MAKE_FUNCPTR(TIFFNumberOfDirectories);
78 MAKE_FUNCPTR(TIFFReadDirectory);
79 MAKE_FUNCPTR(TIFFReadEncodedStrip);
80 MAKE_FUNCPTR(TIFFReadEncodedTile);
81 MAKE_FUNCPTR(TIFFSetDirectory);
82 MAKE_FUNCPTR(TIFFSetField);
83 MAKE_FUNCPTR(TIFFWriteDirectory);
84 MAKE_FUNCPTR(TIFFWriteScanline);
85 #undef MAKE_FUNCPTR
87 static void *load_libtiff(void)
89 void *result;
91 EnterCriticalSection(&init_tiff_cs);
93 if (!libtiff_handle &&
94 (libtiff_handle = wine_dlopen(SONAME_LIBTIFF, RTLD_NOW, NULL, 0)) != NULL)
96 void * (*pTIFFSetWarningHandler)(void *);
97 void * (*pTIFFSetWarningHandlerExt)(void *);
99 #define LOAD_FUNCPTR(f) \
100 if((p##f = wine_dlsym(libtiff_handle, #f, NULL, 0)) == NULL) { \
101 ERR("failed to load symbol %s\n", #f); \
102 libtiff_handle = NULL; \
103 LeaveCriticalSection(&init_tiff_cs); \
104 return NULL; \
106 LOAD_FUNCPTR(TIFFClientOpen);
107 LOAD_FUNCPTR(TIFFClose);
108 LOAD_FUNCPTR(TIFFCurrentDirOffset);
109 LOAD_FUNCPTR(TIFFGetField);
110 LOAD_FUNCPTR(TIFFIsByteSwapped);
111 LOAD_FUNCPTR(TIFFNumberOfDirectories);
112 LOAD_FUNCPTR(TIFFReadDirectory);
113 LOAD_FUNCPTR(TIFFReadEncodedStrip);
114 LOAD_FUNCPTR(TIFFReadEncodedTile);
115 LOAD_FUNCPTR(TIFFSetDirectory);
116 LOAD_FUNCPTR(TIFFSetField);
117 LOAD_FUNCPTR(TIFFWriteDirectory);
118 LOAD_FUNCPTR(TIFFWriteScanline);
119 #undef LOAD_FUNCPTR
121 if ((pTIFFSetWarningHandler = wine_dlsym(libtiff_handle, "TIFFSetWarningHandler", NULL, 0)))
122 pTIFFSetWarningHandler(NULL);
123 if ((pTIFFSetWarningHandlerExt = wine_dlsym(libtiff_handle, "TIFFSetWarningHandlerExt", NULL, 0)))
124 pTIFFSetWarningHandlerExt(NULL);
127 result = libtiff_handle;
129 LeaveCriticalSection(&init_tiff_cs);
130 return result;
133 static tsize_t tiff_stream_read(thandle_t client_data, tdata_t data, tsize_t size)
135 IStream *stream = (IStream*)client_data;
136 ULONG bytes_read;
137 HRESULT hr;
139 hr = IStream_Read(stream, data, size, &bytes_read);
140 if (FAILED(hr)) bytes_read = 0;
141 return bytes_read;
144 static tsize_t tiff_stream_write(thandle_t client_data, tdata_t data, tsize_t size)
146 IStream *stream = (IStream*)client_data;
147 ULONG bytes_written;
148 HRESULT hr;
150 hr = IStream_Write(stream, data, size, &bytes_written);
151 if (FAILED(hr)) bytes_written = 0;
152 return bytes_written;
155 static toff_t tiff_stream_seek(thandle_t client_data, toff_t offset, int whence)
157 IStream *stream = (IStream*)client_data;
158 LARGE_INTEGER move;
159 DWORD origin;
160 ULARGE_INTEGER new_position;
161 HRESULT hr;
163 move.QuadPart = offset;
164 switch (whence)
166 case SEEK_SET:
167 origin = STREAM_SEEK_SET;
168 break;
169 case SEEK_CUR:
170 origin = STREAM_SEEK_CUR;
171 break;
172 case SEEK_END:
173 origin = STREAM_SEEK_END;
174 break;
175 default:
176 ERR("unknown whence value %i\n", whence);
177 return -1;
180 hr = IStream_Seek(stream, move, origin, &new_position);
181 if (SUCCEEDED(hr)) return new_position.QuadPart;
182 else return -1;
185 static int tiff_stream_close(thandle_t client_data)
187 /* Caller is responsible for releasing the stream object. */
188 return 0;
191 static toff_t tiff_stream_size(thandle_t client_data)
193 IStream *stream = (IStream*)client_data;
194 STATSTG statstg;
195 HRESULT hr;
197 hr = IStream_Stat(stream, &statstg, STATFLAG_NONAME);
199 if (SUCCEEDED(hr)) return statstg.cbSize.QuadPart;
200 else return -1;
203 static int tiff_stream_map(thandle_t client_data, tdata_t *addr, toff_t *size)
205 /* Cannot mmap streams */
206 return 0;
209 static void tiff_stream_unmap(thandle_t client_data, tdata_t addr, toff_t size)
211 /* No need to ever do this, since we can't map things. */
214 static TIFF* tiff_open_stream(IStream *stream, const char *mode)
216 LARGE_INTEGER zero;
218 zero.QuadPart = 0;
219 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
221 return pTIFFClientOpen("<IStream object>", mode, stream, tiff_stream_read,
222 tiff_stream_write, (void *)tiff_stream_seek, tiff_stream_close,
223 (void *)tiff_stream_size, (void *)tiff_stream_map, (void *)tiff_stream_unmap);
226 typedef struct {
227 IWICBitmapDecoder IWICBitmapDecoder_iface;
228 LONG ref;
229 IStream *stream;
230 CRITICAL_SECTION lock; /* Must be held when tiff is used or initiailzed is set */
231 TIFF *tiff;
232 BOOL initialized;
233 } TiffDecoder;
235 typedef struct {
236 const WICPixelFormatGUID *format;
237 int bps;
238 int samples;
239 int bpp, source_bpp;
240 int planar;
241 int indexed;
242 int reverse_bgr;
243 int invert_grayscale;
244 UINT width, height;
245 UINT tile_width, tile_height;
246 UINT tile_stride;
247 UINT tile_size;
248 int tiled;
249 UINT tiles_across;
250 UINT resolution_unit;
251 float xres, yres;
252 } tiff_decode_info;
254 typedef struct {
255 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
256 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
257 LONG ref;
258 TiffDecoder *parent;
259 UINT index;
260 tiff_decode_info decode_info;
261 INT cached_tile_x, cached_tile_y;
262 BYTE *cached_tile;
263 } TiffFrameDecode;
265 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl;
266 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl;
268 static inline TiffDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
270 return CONTAINING_RECORD(iface, TiffDecoder, IWICBitmapDecoder_iface);
273 static inline TiffFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
275 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICBitmapFrameDecode_iface);
278 static inline TiffFrameDecode *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
280 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICMetadataBlockReader_iface);
283 static HRESULT tiff_get_decode_info(TIFF *tiff, tiff_decode_info *decode_info)
285 uint16 photometric, bps, samples, planar;
286 uint16 extra_sample_count, extra_sample, *extra_samples;
287 int ret;
289 decode_info->indexed = 0;
290 decode_info->reverse_bgr = 0;
291 decode_info->invert_grayscale = 0;
292 decode_info->tiled = 0;
294 ret = pTIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
295 if (!ret)
297 WARN("missing PhotometricInterpretation tag\n");
298 return E_FAIL;
301 ret = pTIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bps);
302 if (!ret) bps = 1;
303 decode_info->bps = bps;
305 ret = pTIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samples);
306 if (!ret) samples = 1;
307 decode_info->samples = samples;
309 if (samples == 1)
310 planar = 1;
311 else
313 ret = pTIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planar);
314 if (!ret) planar = 1;
315 if (planar != 1)
317 FIXME("unhandled planar configuration %u\n", planar);
318 return E_FAIL;
321 decode_info->planar = planar;
323 switch(photometric)
325 case 0: /* WhiteIsZero */
326 decode_info->invert_grayscale = 1;
327 /* fall through */
328 case 1: /* BlackIsZero */
329 if (samples == 2)
331 ret = pTIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extra_sample_count, &extra_samples);
332 if (!ret)
334 extra_sample_count = 1;
335 extra_sample = 0;
336 extra_samples = &extra_sample;
339 else if (samples != 1)
341 FIXME("unhandled %dbpp sample count %u\n", bps, samples);
342 return E_FAIL;
345 decode_info->bpp = bps * samples;
346 decode_info->source_bpp = decode_info->bpp;
347 switch (bps)
349 case 1:
350 if (samples != 1)
352 FIXME("unhandled 1bpp sample count %u\n", samples);
353 return E_FAIL;
355 decode_info->format = &GUID_WICPixelFormatBlackWhite;
356 break;
357 case 4:
358 if (samples != 1)
360 FIXME("unhandled 4bpp grayscale sample count %u\n", samples);
361 return E_FAIL;
363 decode_info->format = &GUID_WICPixelFormat4bppGray;
364 break;
365 case 8:
366 if (samples == 1)
367 decode_info->format = &GUID_WICPixelFormat8bppGray;
368 else
370 decode_info->bpp = 32;
372 switch(extra_samples[0])
374 case 1: /* Associated (pre-multiplied) alpha data */
375 decode_info->format = &GUID_WICPixelFormat32bppPBGRA;
376 break;
377 case 0: /* Unspecified data */
378 case 2: /* Unassociated alpha data */
379 decode_info->format = &GUID_WICPixelFormat32bppBGRA;
380 break;
381 default:
382 FIXME("unhandled extra sample type %u\n", extra_samples[0]);
383 return E_FAIL;
386 break;
387 default:
388 FIXME("unhandled greyscale bit count %u\n", bps);
389 return E_FAIL;
391 break;
392 case 2: /* RGB */
393 decode_info->bpp = bps * samples;
395 if (samples == 4)
397 ret = pTIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extra_sample_count, &extra_samples);
398 if (!ret)
400 extra_sample_count = 1;
401 extra_sample = 0;
402 extra_samples = &extra_sample;
405 else if (samples != 3)
407 FIXME("unhandled RGB sample count %u\n", samples);
408 return E_FAIL;
411 switch(bps)
413 case 8:
414 decode_info->reverse_bgr = 1;
415 if (samples == 3)
416 decode_info->format = &GUID_WICPixelFormat24bppBGR;
417 else
418 switch(extra_samples[0])
420 case 1: /* Associated (pre-multiplied) alpha data */
421 decode_info->format = &GUID_WICPixelFormat32bppPBGRA;
422 break;
423 case 0: /* Unspecified data */
424 case 2: /* Unassociated alpha data */
425 decode_info->format = &GUID_WICPixelFormat32bppBGRA;
426 break;
427 default:
428 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
429 return E_FAIL;
431 break;
432 case 16:
433 if (samples == 3)
434 decode_info->format = &GUID_WICPixelFormat48bppRGB;
435 else
436 switch(extra_samples[0])
438 case 1: /* Associated (pre-multiplied) alpha data */
439 decode_info->format = &GUID_WICPixelFormat64bppPRGBA;
440 break;
441 case 0: /* Unspecified data */
442 case 2: /* Unassociated alpha data */
443 decode_info->format = &GUID_WICPixelFormat64bppRGBA;
444 break;
445 default:
446 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
447 return E_FAIL;
449 break;
450 default:
451 FIXME("unhandled RGB bit count %u\n", bps);
452 return E_FAIL;
454 break;
455 case 3: /* RGB Palette */
456 if (samples != 1)
458 FIXME("unhandled indexed sample count %u\n", samples);
459 return E_FAIL;
462 decode_info->indexed = 1;
463 decode_info->bpp = bps;
464 switch (bps)
466 case 4:
467 decode_info->format = &GUID_WICPixelFormat4bppIndexed;
468 break;
469 case 8:
470 decode_info->format = &GUID_WICPixelFormat8bppIndexed;
471 break;
472 default:
473 FIXME("unhandled indexed bit count %u\n", bps);
474 return E_FAIL;
476 break;
477 case 4: /* Transparency mask */
478 case 5: /* CMYK */
479 case 6: /* YCbCr */
480 case 8: /* CIELab */
481 default:
482 FIXME("unhandled PhotometricInterpretation %u\n", photometric);
483 return E_FAIL;
486 ret = pTIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &decode_info->width);
487 if (!ret)
489 WARN("missing image width\n");
490 return E_FAIL;
493 ret = pTIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &decode_info->height);
494 if (!ret)
496 WARN("missing image length\n");
497 return E_FAIL;
500 if ((ret = pTIFFGetField(tiff, TIFFTAG_TILEWIDTH, &decode_info->tile_width)))
502 decode_info->tiled = 1;
504 ret = pTIFFGetField(tiff, TIFFTAG_TILELENGTH, &decode_info->tile_height);
505 if (!ret)
507 WARN("missing tile height\n");
508 return E_FAIL;
511 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
512 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
513 decode_info->tiles_across = (decode_info->width + decode_info->tile_width - 1) / decode_info->tile_width;
515 else if ((ret = pTIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &decode_info->tile_height)))
517 if (decode_info->tile_height > decode_info->height)
518 decode_info->tile_height = decode_info->height;
519 decode_info->tile_width = decode_info->width;
520 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
521 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
523 else
525 /* Some broken TIFF files have a single strip and lack the RowsPerStrip tag */
526 decode_info->tile_height = decode_info->height;
527 decode_info->tile_width = decode_info->width;
528 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
529 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
532 decode_info->resolution_unit = 0;
533 pTIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &decode_info->resolution_unit);
534 if (decode_info->resolution_unit != 0)
536 ret = pTIFFGetField(tiff, TIFFTAG_XRESOLUTION, &decode_info->xres);
537 if (!ret)
539 WARN("missing X resolution\n");
540 decode_info->resolution_unit = 0;
543 ret = pTIFFGetField(tiff, TIFFTAG_YRESOLUTION, &decode_info->yres);
544 if (!ret)
546 WARN("missing Y resolution\n");
547 decode_info->resolution_unit = 0;
551 return S_OK;
554 static HRESULT WINAPI TiffDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
555 void **ppv)
557 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
558 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
560 if (!ppv) return E_INVALIDARG;
562 if (IsEqualIID(&IID_IUnknown, iid) ||
563 IsEqualIID(&IID_IWICBitmapDecoder, iid))
565 *ppv = &This->IWICBitmapDecoder_iface;
567 else
569 *ppv = NULL;
570 return E_NOINTERFACE;
573 IUnknown_AddRef((IUnknown*)*ppv);
574 return S_OK;
577 static ULONG WINAPI TiffDecoder_AddRef(IWICBitmapDecoder *iface)
579 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
580 ULONG ref = InterlockedIncrement(&This->ref);
582 TRACE("(%p) refcount=%u\n", iface, ref);
584 return ref;
587 static ULONG WINAPI TiffDecoder_Release(IWICBitmapDecoder *iface)
589 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
590 ULONG ref = InterlockedDecrement(&This->ref);
592 TRACE("(%p) refcount=%u\n", iface, ref);
594 if (ref == 0)
596 if (This->tiff) pTIFFClose(This->tiff);
597 if (This->stream) IStream_Release(This->stream);
598 This->lock.DebugInfo->Spare[0] = 0;
599 DeleteCriticalSection(&This->lock);
600 HeapFree(GetProcessHeap(), 0, This);
603 return ref;
606 static HRESULT WINAPI TiffDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
607 DWORD *capability)
609 HRESULT hr;
611 TRACE("(%p,%p,%p)\n", iface, stream, capability);
613 if (!stream || !capability) return E_INVALIDARG;
615 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
616 if (hr != S_OK) return hr;
618 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
619 WICBitmapDecoderCapabilityCanDecodeSomeImages |
620 WICBitmapDecoderCapabilityCanEnumerateMetadata;
621 return S_OK;
624 static HRESULT WINAPI TiffDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
625 WICDecodeOptions cacheOptions)
627 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
628 TIFF *tiff;
629 HRESULT hr=S_OK;
631 TRACE("(%p,%p,%x): stub\n", iface, pIStream, cacheOptions);
633 EnterCriticalSection(&This->lock);
635 if (This->initialized)
637 hr = WINCODEC_ERR_WRONGSTATE;
638 goto exit;
641 tiff = tiff_open_stream(pIStream, "r");
643 if (!tiff)
645 hr = E_FAIL;
646 goto exit;
649 This->tiff = tiff;
650 This->stream = pIStream;
651 IStream_AddRef(pIStream);
652 This->initialized = TRUE;
654 exit:
655 LeaveCriticalSection(&This->lock);
656 return hr;
659 static HRESULT WINAPI TiffDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
660 GUID *pguidContainerFormat)
662 if (!pguidContainerFormat) return E_INVALIDARG;
664 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
665 return S_OK;
668 static HRESULT WINAPI TiffDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
669 IWICBitmapDecoderInfo **ppIDecoderInfo)
671 HRESULT hr;
672 IWICComponentInfo *compinfo;
674 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
676 hr = CreateComponentInfo(&CLSID_WICTiffDecoder, &compinfo);
677 if (FAILED(hr)) return hr;
679 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
680 (void**)ppIDecoderInfo);
682 IWICComponentInfo_Release(compinfo);
684 return hr;
687 static HRESULT WINAPI TiffDecoder_CopyPalette(IWICBitmapDecoder *iface,
688 IWICPalette *pIPalette)
690 FIXME("(%p,%p): stub\n", iface, pIPalette);
691 return E_NOTIMPL;
694 static HRESULT WINAPI TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
695 IWICMetadataQueryReader **ppIMetadataQueryReader)
697 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
698 return E_NOTIMPL;
701 static HRESULT WINAPI TiffDecoder_GetPreview(IWICBitmapDecoder *iface,
702 IWICBitmapSource **ppIBitmapSource)
704 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
706 if (!ppIBitmapSource) return E_INVALIDARG;
708 *ppIBitmapSource = NULL;
709 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
712 static HRESULT WINAPI TiffDecoder_GetColorContexts(IWICBitmapDecoder *iface,
713 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
715 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
716 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
719 static HRESULT WINAPI TiffDecoder_GetThumbnail(IWICBitmapDecoder *iface,
720 IWICBitmapSource **ppIThumbnail)
722 TRACE("(%p,%p)\n", iface, ppIThumbnail);
724 if (!ppIThumbnail) return E_INVALIDARG;
726 *ppIThumbnail = NULL;
727 return WINCODEC_ERR_CODECNOTHUMBNAIL;
730 static HRESULT WINAPI TiffDecoder_GetFrameCount(IWICBitmapDecoder *iface,
731 UINT *pCount)
733 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
735 if (!pCount) return E_INVALIDARG;
737 EnterCriticalSection(&This->lock);
738 *pCount = This->tiff ? pTIFFNumberOfDirectories(This->tiff) : 0;
739 LeaveCriticalSection(&This->lock);
741 TRACE("(%p) <-- %i\n", iface, *pCount);
743 return S_OK;
746 static HRESULT WINAPI TiffDecoder_GetFrame(IWICBitmapDecoder *iface,
747 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
749 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
750 TiffFrameDecode *result;
751 int res;
752 tiff_decode_info decode_info;
753 HRESULT hr;
755 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
757 if (!This->tiff)
758 return WINCODEC_ERR_FRAMEMISSING;
760 EnterCriticalSection(&This->lock);
761 res = pTIFFSetDirectory(This->tiff, index);
762 if (!res) hr = E_INVALIDARG;
763 else hr = tiff_get_decode_info(This->tiff, &decode_info);
764 LeaveCriticalSection(&This->lock);
766 if (SUCCEEDED(hr))
768 result = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode));
770 if (result)
772 result->IWICBitmapFrameDecode_iface.lpVtbl = &TiffFrameDecode_Vtbl;
773 result->IWICMetadataBlockReader_iface.lpVtbl = &TiffFrameDecode_BlockVtbl;
774 result->ref = 1;
775 result->parent = This;
776 result->index = index;
777 result->decode_info = decode_info;
778 result->cached_tile_x = -1;
779 result->cached_tile = HeapAlloc(GetProcessHeap(), 0, decode_info.tile_size);
781 if (result->cached_tile)
782 *ppIBitmapFrame = &result->IWICBitmapFrameDecode_iface;
783 else
785 hr = E_OUTOFMEMORY;
786 HeapFree(GetProcessHeap(), 0, result);
789 else hr = E_OUTOFMEMORY;
792 if (FAILED(hr)) *ppIBitmapFrame = NULL;
794 return hr;
797 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl = {
798 TiffDecoder_QueryInterface,
799 TiffDecoder_AddRef,
800 TiffDecoder_Release,
801 TiffDecoder_QueryCapability,
802 TiffDecoder_Initialize,
803 TiffDecoder_GetContainerFormat,
804 TiffDecoder_GetDecoderInfo,
805 TiffDecoder_CopyPalette,
806 TiffDecoder_GetMetadataQueryReader,
807 TiffDecoder_GetPreview,
808 TiffDecoder_GetColorContexts,
809 TiffDecoder_GetThumbnail,
810 TiffDecoder_GetFrameCount,
811 TiffDecoder_GetFrame
814 static HRESULT WINAPI TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
815 void **ppv)
817 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
818 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
820 if (!ppv) return E_INVALIDARG;
822 if (IsEqualIID(&IID_IUnknown, iid) ||
823 IsEqualIID(&IID_IWICBitmapSource, iid) ||
824 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
826 *ppv = &This->IWICBitmapFrameDecode_iface;
828 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
830 *ppv = &This->IWICMetadataBlockReader_iface;
832 else
834 *ppv = NULL;
835 return E_NOINTERFACE;
838 IUnknown_AddRef((IUnknown*)*ppv);
839 return S_OK;
842 static ULONG WINAPI TiffFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
844 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
845 ULONG ref = InterlockedIncrement(&This->ref);
847 TRACE("(%p) refcount=%u\n", iface, ref);
849 return ref;
852 static ULONG WINAPI TiffFrameDecode_Release(IWICBitmapFrameDecode *iface)
854 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
855 ULONG ref = InterlockedDecrement(&This->ref);
857 TRACE("(%p) refcount=%u\n", iface, ref);
859 if (ref == 0)
861 HeapFree(GetProcessHeap(), 0, This->cached_tile);
862 HeapFree(GetProcessHeap(), 0, This);
865 return ref;
868 static HRESULT WINAPI TiffFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
869 UINT *puiWidth, UINT *puiHeight)
871 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
873 *puiWidth = This->decode_info.width;
874 *puiHeight = This->decode_info.height;
876 TRACE("(%p) <-- %ux%u\n", iface, *puiWidth, *puiHeight);
878 return S_OK;
881 static HRESULT WINAPI TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
882 WICPixelFormatGUID *pPixelFormat)
884 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
886 memcpy(pPixelFormat, This->decode_info.format, sizeof(GUID));
888 TRACE("(%p) <-- %s\n", This, debugstr_guid(This->decode_info.format));
890 return S_OK;
893 static HRESULT WINAPI TiffFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
894 double *pDpiX, double *pDpiY)
896 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
898 switch (This->decode_info.resolution_unit)
900 default:
901 FIXME("unknown resolution unit %i\n", This->decode_info.resolution_unit);
902 /* fall through */
903 case 0: /* Not set */
904 *pDpiX = *pDpiY = 96.0;
905 break;
906 case 1: /* Relative measurements */
907 *pDpiX = 96.0;
908 *pDpiY = 96.0 * This->decode_info.yres / This->decode_info.xres;
909 break;
910 case 2: /* Inch */
911 *pDpiX = This->decode_info.xres;
912 *pDpiY = This->decode_info.yres;
913 break;
914 case 3: /* Centimeter */
915 *pDpiX = This->decode_info.xres / 2.54;
916 *pDpiY = This->decode_info.yres / 2.54;
917 break;
920 TRACE("(%p) <-- %f,%f unit=%i\n", iface, *pDpiX, *pDpiY, This->decode_info.resolution_unit);
922 return S_OK;
925 static HRESULT WINAPI TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
926 IWICPalette *pIPalette)
928 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
929 uint16 *red, *green, *blue;
930 WICColor colors[256];
931 int color_count, ret, i;
933 TRACE("(%p,%p)\n", iface, pIPalette);
935 color_count = 1<<This->decode_info.bps;
937 EnterCriticalSection(&This->parent->lock);
938 ret = pTIFFGetField(This->parent->tiff, TIFFTAG_COLORMAP, &red, &green, &blue);
939 LeaveCriticalSection(&This->parent->lock);
941 if (!ret)
943 WARN("Couldn't read color map\n");
944 return WINCODEC_ERR_PALETTEUNAVAILABLE;
947 for (i=0; i<color_count; i++)
949 colors[i] = 0xff000000 |
950 ((red[i]<<8) & 0xff0000) |
951 (green[i] & 0xff00) |
952 ((blue[i]>>8) & 0xff);
955 return IWICPalette_InitializeCustom(pIPalette, colors, color_count);
958 static HRESULT TiffFrameDecode_ReadTile(TiffFrameDecode *This, UINT tile_x, UINT tile_y)
960 HRESULT hr=S_OK;
961 tsize_t ret;
962 int swap_bytes;
964 swap_bytes = pTIFFIsByteSwapped(This->parent->tiff);
966 ret = pTIFFSetDirectory(This->parent->tiff, This->index);
968 if (ret == -1)
969 hr = E_FAIL;
971 if (hr == S_OK)
973 if (This->decode_info.tiled)
975 ret = pTIFFReadEncodedTile(This->parent->tiff, tile_x + tile_y * This->decode_info.tiles_across, This->cached_tile, This->decode_info.tile_size);
977 else
979 ret = pTIFFReadEncodedStrip(This->parent->tiff, tile_y, This->cached_tile, This->decode_info.tile_size);
982 if (ret == -1)
983 hr = E_FAIL;
986 /* 8bpp grayscale with extra alpha */
987 if (hr == S_OK && This->decode_info.source_bpp == 16 && This->decode_info.samples == 2 && This->decode_info.bpp == 32)
989 BYTE *src;
990 DWORD *dst, count = This->decode_info.tile_width * This->decode_info.tile_height;
992 src = This->cached_tile + This->decode_info.tile_width * This->decode_info.tile_height * 2 - 2;
993 dst = (DWORD *)(This->cached_tile + This->decode_info.tile_size - 4);
995 while (count--)
997 *dst-- = src[0] | (src[0] << 8) | (src[0] << 16) | (src[1] << 24);
998 src -= 2;
1002 if (hr == S_OK && This->decode_info.reverse_bgr)
1004 if (This->decode_info.bps == 8)
1006 UINT sample_count = This->decode_info.samples;
1008 reverse_bgr8(sample_count, This->cached_tile, This->decode_info.tile_width,
1009 This->decode_info.tile_height, This->decode_info.tile_width * sample_count);
1013 if (hr == S_OK && swap_bytes && This->decode_info.bps > 8)
1015 UINT row, i, samples_per_row;
1016 BYTE *sample, temp;
1018 samples_per_row = This->decode_info.tile_width * This->decode_info.samples;
1020 switch(This->decode_info.bps)
1022 case 16:
1023 for (row=0; row<This->decode_info.tile_height; row++)
1025 sample = This->cached_tile + row * This->decode_info.tile_stride;
1026 for (i=0; i<samples_per_row; i++)
1028 temp = sample[1];
1029 sample[1] = sample[0];
1030 sample[0] = temp;
1031 sample += 2;
1034 break;
1035 default:
1036 ERR("unhandled bps for byte swap %u\n", This->decode_info.bps);
1037 return E_FAIL;
1041 if (hr == S_OK && This->decode_info.invert_grayscale)
1043 BYTE *byte, *end;
1045 if (This->decode_info.samples != 1)
1047 ERR("cannot invert grayscale image with %u samples\n", This->decode_info.samples);
1048 return E_FAIL;
1051 end = This->cached_tile+This->decode_info.tile_size;
1053 for (byte = This->cached_tile; byte != end; byte++)
1054 *byte = ~(*byte);
1057 if (hr == S_OK)
1059 This->cached_tile_x = tile_x;
1060 This->cached_tile_y = tile_y;
1063 return hr;
1066 static HRESULT WINAPI TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
1067 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
1069 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
1070 UINT min_tile_x, max_tile_x, min_tile_y, max_tile_y;
1071 UINT tile_x, tile_y;
1072 WICRect rc;
1073 HRESULT hr=S_OK;
1074 BYTE *dst_tilepos;
1075 UINT bytesperrow;
1076 WICRect rect;
1078 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
1080 if (!prc)
1082 rect.X = 0;
1083 rect.Y = 0;
1084 rect.Width = This->decode_info.width;
1085 rect.Height = This->decode_info.height;
1086 prc = &rect;
1088 else
1090 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->decode_info.width ||
1091 prc->Y+prc->Height > This->decode_info.height)
1092 return E_INVALIDARG;
1095 bytesperrow = ((This->decode_info.bpp * prc->Width)+7)/8;
1097 if (cbStride < bytesperrow)
1098 return E_INVALIDARG;
1100 if ((cbStride * prc->Height) > cbBufferSize)
1101 return E_INVALIDARG;
1103 min_tile_x = prc->X / This->decode_info.tile_width;
1104 min_tile_y = prc->Y / This->decode_info.tile_height;
1105 max_tile_x = (prc->X+prc->Width-1) / This->decode_info.tile_width;
1106 max_tile_y = (prc->Y+prc->Height-1) / This->decode_info.tile_height;
1108 EnterCriticalSection(&This->parent->lock);
1110 for (tile_x=min_tile_x; tile_x <= max_tile_x; tile_x++)
1112 for (tile_y=min_tile_y; tile_y <= max_tile_y; tile_y++)
1114 if (tile_x != This->cached_tile_x || tile_y != This->cached_tile_y)
1116 hr = TiffFrameDecode_ReadTile(This, tile_x, tile_y);
1119 if (SUCCEEDED(hr))
1121 if (prc->X < tile_x * This->decode_info.tile_width)
1122 rc.X = 0;
1123 else
1124 rc.X = prc->X - tile_x * This->decode_info.tile_width;
1126 if (prc->Y < tile_y * This->decode_info.tile_height)
1127 rc.Y = 0;
1128 else
1129 rc.Y = prc->Y - tile_y * This->decode_info.tile_height;
1131 if (prc->X+prc->Width > (tile_x+1) * This->decode_info.tile_width)
1132 rc.Width = This->decode_info.tile_width - rc.X;
1133 else if (prc->X < tile_x * This->decode_info.tile_width)
1134 rc.Width = prc->Width + prc->X - tile_x * This->decode_info.tile_width;
1135 else
1136 rc.Width = prc->Width;
1138 if (prc->Y+prc->Height > (tile_y+1) * This->decode_info.tile_height)
1139 rc.Height = This->decode_info.tile_height - rc.Y;
1140 else if (prc->Y < tile_y * This->decode_info.tile_height)
1141 rc.Height = prc->Height + prc->Y - tile_y * This->decode_info.tile_height;
1142 else
1143 rc.Height = prc->Height;
1145 dst_tilepos = pbBuffer + (cbStride * ((rc.Y + tile_y * This->decode_info.tile_height) - prc->Y)) +
1146 ((This->decode_info.bpp * ((rc.X + tile_x * This->decode_info.tile_width) - prc->X) + 7) / 8);
1148 hr = copy_pixels(This->decode_info.bpp, This->cached_tile,
1149 This->decode_info.tile_width, This->decode_info.tile_height, This->decode_info.tile_stride,
1150 &rc, cbStride, cbBufferSize, dst_tilepos);
1153 if (FAILED(hr))
1155 LeaveCriticalSection(&This->parent->lock);
1156 TRACE("<-- 0x%x\n", hr);
1157 return hr;
1162 LeaveCriticalSection(&This->parent->lock);
1164 return S_OK;
1167 static HRESULT WINAPI TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
1168 IWICMetadataQueryReader **ppIMetadataQueryReader)
1170 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
1171 return E_NOTIMPL;
1174 static HRESULT WINAPI TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
1175 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1177 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
1178 const BYTE *profile;
1179 UINT len;
1180 HRESULT hr;
1182 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
1184 EnterCriticalSection(&This->parent->lock);
1186 if (pTIFFGetField(This->parent->tiff, TIFFTAG_ICCPROFILE, &len, &profile))
1188 if (cCount && ppIColorContexts)
1190 hr = IWICColorContext_InitializeFromMemory(*ppIColorContexts, profile, len);
1191 if (FAILED(hr))
1193 LeaveCriticalSection(&This->parent->lock);
1194 return hr;
1197 *pcActualCount = 1;
1199 else
1200 *pcActualCount = 0;
1202 LeaveCriticalSection(&This->parent->lock);
1204 return S_OK;
1207 static HRESULT WINAPI TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
1208 IWICBitmapSource **ppIThumbnail)
1210 TRACE("(%p,%p)\n", iface, ppIThumbnail);
1212 if (!ppIThumbnail) return E_INVALIDARG;
1214 *ppIThumbnail = NULL;
1215 return WINCODEC_ERR_CODECNOTHUMBNAIL;
1218 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl = {
1219 TiffFrameDecode_QueryInterface,
1220 TiffFrameDecode_AddRef,
1221 TiffFrameDecode_Release,
1222 TiffFrameDecode_GetSize,
1223 TiffFrameDecode_GetPixelFormat,
1224 TiffFrameDecode_GetResolution,
1225 TiffFrameDecode_CopyPalette,
1226 TiffFrameDecode_CopyPixels,
1227 TiffFrameDecode_GetMetadataQueryReader,
1228 TiffFrameDecode_GetColorContexts,
1229 TiffFrameDecode_GetThumbnail
1232 static HRESULT WINAPI TiffFrameDecode_Block_QueryInterface(IWICMetadataBlockReader *iface,
1233 REFIID iid, void **ppv)
1235 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1236 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
1239 static ULONG WINAPI TiffFrameDecode_Block_AddRef(IWICMetadataBlockReader *iface)
1241 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1242 return IWICBitmapFrameDecode_AddRef(&This->IWICBitmapFrameDecode_iface);
1245 static ULONG WINAPI TiffFrameDecode_Block_Release(IWICMetadataBlockReader *iface)
1247 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1248 return IWICBitmapFrameDecode_Release(&This->IWICBitmapFrameDecode_iface);
1251 static HRESULT WINAPI TiffFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
1252 GUID *guid)
1254 TRACE("(%p,%p)\n", iface, guid);
1256 if (!guid) return E_INVALIDARG;
1258 *guid = GUID_ContainerFormatTiff;
1259 return S_OK;
1262 static HRESULT WINAPI TiffFrameDecode_Block_GetCount(IWICMetadataBlockReader *iface,
1263 UINT *count)
1265 TRACE("%p,%p\n", iface, count);
1267 if (!count) return E_INVALIDARG;
1269 *count = 1;
1270 return S_OK;
1273 static HRESULT create_metadata_reader(TiffFrameDecode *This, IWICMetadataReader **reader)
1275 HRESULT hr;
1276 LARGE_INTEGER dir_offset;
1277 IWICMetadataReader *metadata_reader;
1278 IWICPersistStream *persist;
1280 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
1282 hr = IfdMetadataReader_CreateInstance(&IID_IWICMetadataReader, (void **)&metadata_reader);
1283 if (FAILED(hr)) return hr;
1285 hr = IWICMetadataReader_QueryInterface(metadata_reader, &IID_IWICPersistStream, (void **)&persist);
1286 if (FAILED(hr))
1288 IWICMetadataReader_Release(metadata_reader);
1289 return hr;
1292 EnterCriticalSection(&This->parent->lock);
1294 dir_offset.QuadPart = pTIFFCurrentDirOffset(This->parent->tiff);
1295 hr = IStream_Seek(This->parent->stream, dir_offset, STREAM_SEEK_SET, NULL);
1296 if (SUCCEEDED(hr))
1298 BOOL byte_swapped = pTIFFIsByteSwapped(This->parent->tiff);
1299 #ifdef WORDS_BIGENDIAN
1300 DWORD persist_options = byte_swapped ? WICPersistOptionsLittleEndian : WICPersistOptionsBigEndian;
1301 #else
1302 DWORD persist_options = byte_swapped ? WICPersistOptionsBigEndian : WICPersistOptionsLittleEndian;
1303 #endif
1304 persist_options |= WICPersistOptionsNoCacheStream;
1305 hr = IWICPersistStream_LoadEx(persist, This->parent->stream, NULL, persist_options);
1306 if (FAILED(hr))
1307 ERR("IWICPersistStream_LoadEx error %#x\n", hr);
1310 LeaveCriticalSection(&This->parent->lock);
1312 IWICPersistStream_Release(persist);
1314 if (FAILED(hr))
1316 IWICMetadataReader_Release(metadata_reader);
1317 return hr;
1320 *reader = metadata_reader;
1321 return S_OK;
1324 static HRESULT WINAPI TiffFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
1325 UINT index, IWICMetadataReader **reader)
1327 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1329 TRACE("(%p,%u,%p)\n", iface, index, reader);
1331 if (!reader || index != 0) return E_INVALIDARG;
1333 return create_metadata_reader(This, reader);
1336 static HRESULT WINAPI TiffFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader *iface,
1337 IEnumUnknown **enum_metadata)
1339 FIXME("(%p,%p): stub\n", iface, enum_metadata);
1340 return E_NOTIMPL;
1343 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl =
1345 TiffFrameDecode_Block_QueryInterface,
1346 TiffFrameDecode_Block_AddRef,
1347 TiffFrameDecode_Block_Release,
1348 TiffFrameDecode_Block_GetContainerFormat,
1349 TiffFrameDecode_Block_GetCount,
1350 TiffFrameDecode_Block_GetReaderByIndex,
1351 TiffFrameDecode_Block_GetEnumerator
1354 HRESULT TiffDecoder_CreateInstance(REFIID iid, void** ppv)
1356 HRESULT ret;
1357 TiffDecoder *This;
1359 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1361 *ppv = NULL;
1363 if (!load_libtiff())
1365 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF);
1366 return E_FAIL;
1369 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder));
1370 if (!This) return E_OUTOFMEMORY;
1372 This->IWICBitmapDecoder_iface.lpVtbl = &TiffDecoder_Vtbl;
1373 This->ref = 1;
1374 This->stream = NULL;
1375 InitializeCriticalSection(&This->lock);
1376 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffDecoder.lock");
1377 This->tiff = NULL;
1378 This->initialized = FALSE;
1380 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1381 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1383 return ret;
1386 struct tiff_encode_format {
1387 const WICPixelFormatGUID *guid;
1388 int photometric;
1389 int bps;
1390 int samples;
1391 int bpp;
1392 int extra_sample;
1393 int extra_sample_type;
1394 int reverse_bgr;
1397 static const struct tiff_encode_format formats[] = {
1398 {&GUID_WICPixelFormat24bppBGR, 2, 8, 3, 24, 0, 0, 1},
1399 {&GUID_WICPixelFormat24bppRGB, 2, 8, 3, 24, 0, 0, 0},
1400 {&GUID_WICPixelFormatBlackWhite, 1, 1, 1, 1, 0, 0, 0},
1401 {&GUID_WICPixelFormat4bppGray, 1, 4, 1, 4, 0, 0, 0},
1402 {&GUID_WICPixelFormat8bppGray, 1, 8, 1, 8, 0, 0, 0},
1403 {&GUID_WICPixelFormat32bppBGRA, 2, 8, 4, 32, 1, 2, 1},
1404 {&GUID_WICPixelFormat32bppPBGRA, 2, 8, 4, 32, 1, 1, 1},
1405 {&GUID_WICPixelFormat48bppRGB, 2, 16, 3, 48, 0, 0, 0},
1406 {&GUID_WICPixelFormat64bppRGBA, 2, 16, 4, 64, 1, 2, 0},
1407 {&GUID_WICPixelFormat64bppPRGBA, 2, 16, 4, 64, 1, 1, 0},
1411 typedef struct TiffEncoder {
1412 IWICBitmapEncoder IWICBitmapEncoder_iface;
1413 LONG ref;
1414 IStream *stream;
1415 CRITICAL_SECTION lock; /* Must be held when tiff is used or fields below are set */
1416 TIFF *tiff;
1417 BOOL initialized;
1418 BOOL committed;
1419 ULONG num_frames;
1420 ULONG num_frames_committed;
1421 } TiffEncoder;
1423 static inline TiffEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1425 return CONTAINING_RECORD(iface, TiffEncoder, IWICBitmapEncoder_iface);
1428 typedef struct TiffFrameEncode {
1429 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1430 LONG ref;
1431 TiffEncoder *parent;
1432 /* fields below are protected by parent->lock */
1433 BOOL initialized;
1434 BOOL info_written;
1435 BOOL committed;
1436 const struct tiff_encode_format *format;
1437 UINT width, height;
1438 double xres, yres;
1439 UINT lines_written;
1440 } TiffFrameEncode;
1442 static inline TiffFrameEncode *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1444 return CONTAINING_RECORD(iface, TiffFrameEncode, IWICBitmapFrameEncode_iface);
1447 static HRESULT WINAPI TiffFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1448 void **ppv)
1450 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1451 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1453 if (!ppv) return E_INVALIDARG;
1455 if (IsEqualIID(&IID_IUnknown, iid) ||
1456 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1458 *ppv = &This->IWICBitmapFrameEncode_iface;
1460 else
1462 *ppv = NULL;
1463 return E_NOINTERFACE;
1466 IUnknown_AddRef((IUnknown*)*ppv);
1467 return S_OK;
1470 static ULONG WINAPI TiffFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1472 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1473 ULONG ref = InterlockedIncrement(&This->ref);
1475 TRACE("(%p) refcount=%u\n", iface, ref);
1477 return ref;
1480 static ULONG WINAPI TiffFrameEncode_Release(IWICBitmapFrameEncode *iface)
1482 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1483 ULONG ref = InterlockedDecrement(&This->ref);
1485 TRACE("(%p) refcount=%u\n", iface, ref);
1487 if (ref == 0)
1489 IWICBitmapEncoder_Release(&This->parent->IWICBitmapEncoder_iface);
1490 HeapFree(GetProcessHeap(), 0, This);
1493 return ref;
1496 static HRESULT WINAPI TiffFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1497 IPropertyBag2 *pIEncoderOptions)
1499 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1500 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1502 EnterCriticalSection(&This->parent->lock);
1504 if (This->initialized)
1506 LeaveCriticalSection(&This->parent->lock);
1507 return WINCODEC_ERR_WRONGSTATE;
1510 This->initialized = TRUE;
1512 LeaveCriticalSection(&This->parent->lock);
1514 return S_OK;
1517 static HRESULT WINAPI TiffFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1518 UINT uiWidth, UINT uiHeight)
1520 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1521 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1523 EnterCriticalSection(&This->parent->lock);
1525 if (!This->initialized || This->info_written)
1527 LeaveCriticalSection(&This->parent->lock);
1528 return WINCODEC_ERR_WRONGSTATE;
1531 This->width = uiWidth;
1532 This->height = uiHeight;
1534 LeaveCriticalSection(&This->parent->lock);
1536 return S_OK;
1539 static HRESULT WINAPI TiffFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1540 double dpiX, double dpiY)
1542 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1543 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1545 EnterCriticalSection(&This->parent->lock);
1547 if (!This->initialized || This->info_written)
1549 LeaveCriticalSection(&This->parent->lock);
1550 return WINCODEC_ERR_WRONGSTATE;
1553 This->xres = dpiX;
1554 This->yres = dpiY;
1556 LeaveCriticalSection(&This->parent->lock);
1558 return S_OK;
1561 static HRESULT WINAPI TiffFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1562 WICPixelFormatGUID *pPixelFormat)
1564 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1565 int i;
1567 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1569 EnterCriticalSection(&This->parent->lock);
1571 if (!This->initialized || This->info_written)
1573 LeaveCriticalSection(&This->parent->lock);
1574 return WINCODEC_ERR_WRONGSTATE;
1577 for (i=0; formats[i].guid; i++)
1579 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1580 break;
1583 if (!formats[i].guid) i = 0;
1585 This->format = &formats[i];
1586 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1588 LeaveCriticalSection(&This->parent->lock);
1590 return S_OK;
1593 static HRESULT WINAPI TiffFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1594 UINT cCount, IWICColorContext **ppIColorContext)
1596 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1597 return E_NOTIMPL;
1600 static HRESULT WINAPI TiffFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1601 IWICPalette *pIPalette)
1603 FIXME("(%p,%p): stub\n", iface, pIPalette);
1604 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1607 static HRESULT WINAPI TiffFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1608 IWICBitmapSource *pIThumbnail)
1610 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1611 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1614 static HRESULT WINAPI TiffFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1615 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1617 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1618 BYTE *row_data, *swapped_data = NULL;
1619 UINT i, j, line_size;
1621 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1623 EnterCriticalSection(&This->parent->lock);
1625 if (!This->initialized || !This->width || !This->height || !This->format)
1627 LeaveCriticalSection(&This->parent->lock);
1628 return WINCODEC_ERR_WRONGSTATE;
1631 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1633 LeaveCriticalSection(&This->parent->lock);
1634 return E_INVALIDARG;
1637 line_size = ((This->width * This->format->bpp)+7)/8;
1639 if (This->format->reverse_bgr)
1641 swapped_data = HeapAlloc(GetProcessHeap(), 0, line_size);
1642 if (!swapped_data)
1644 LeaveCriticalSection(&This->parent->lock);
1645 return E_OUTOFMEMORY;
1649 if (!This->info_written)
1651 pTIFFSetField(This->parent->tiff, TIFFTAG_PHOTOMETRIC, (uint16)This->format->photometric);
1652 pTIFFSetField(This->parent->tiff, TIFFTAG_PLANARCONFIG, (uint16)1);
1653 pTIFFSetField(This->parent->tiff, TIFFTAG_BITSPERSAMPLE, (uint16)This->format->bps);
1654 pTIFFSetField(This->parent->tiff, TIFFTAG_SAMPLESPERPIXEL, (uint16)This->format->samples);
1656 if (This->format->extra_sample)
1658 uint16 extra_samples;
1659 extra_samples = This->format->extra_sample_type;
1661 pTIFFSetField(This->parent->tiff, TIFFTAG_EXTRASAMPLES, (uint16)1, &extra_samples);
1664 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGEWIDTH, (uint32)This->width);
1665 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGELENGTH, (uint32)This->height);
1667 if (This->xres != 0.0 && This->yres != 0.0)
1669 pTIFFSetField(This->parent->tiff, TIFFTAG_RESOLUTIONUNIT, (uint16)2); /* Inch */
1670 pTIFFSetField(This->parent->tiff, TIFFTAG_XRESOLUTION, (float)This->xres);
1671 pTIFFSetField(This->parent->tiff, TIFFTAG_YRESOLUTION, (float)This->yres);
1674 This->info_written = TRUE;
1677 for (i=0; i<lineCount; i++)
1679 row_data = pbPixels + i * cbStride;
1681 if (This->format->reverse_bgr && This->format->bps == 8)
1683 memcpy(swapped_data, row_data, line_size);
1684 for (j=0; j<line_size; j += This->format->samples)
1686 BYTE temp;
1687 temp = swapped_data[j];
1688 swapped_data[j] = swapped_data[j+2];
1689 swapped_data[j+2] = temp;
1691 row_data = swapped_data;
1694 pTIFFWriteScanline(This->parent->tiff, (tdata_t)row_data, i+This->lines_written, 0);
1697 This->lines_written += lineCount;
1699 LeaveCriticalSection(&This->parent->lock);
1701 HeapFree(GetProcessHeap(), 0, swapped_data);
1703 return S_OK;
1706 static HRESULT WINAPI TiffFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1707 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1709 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1710 HRESULT hr;
1712 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1714 if (!This->initialized)
1715 return WINCODEC_ERR_WRONGSTATE;
1717 hr = configure_write_source(iface, pIBitmapSource, prc,
1718 This->format ? This->format->guid : NULL, This->width, This->height,
1719 This->xres, This->yres);
1721 if (SUCCEEDED(hr))
1723 hr = write_source(iface, pIBitmapSource, prc,
1724 This->format->guid, This->format->bpp, This->width, This->height);
1727 return hr;
1730 static HRESULT WINAPI TiffFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1732 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1734 TRACE("(%p)\n", iface);
1736 EnterCriticalSection(&This->parent->lock);
1738 if (!This->info_written || This->lines_written != This->height || This->committed)
1740 LeaveCriticalSection(&This->parent->lock);
1741 return WINCODEC_ERR_WRONGSTATE;
1744 /* libtiff will commit the data when creating a new frame or closing the file */
1746 This->committed = TRUE;
1747 This->parent->num_frames_committed++;
1749 LeaveCriticalSection(&This->parent->lock);
1751 return S_OK;
1754 static HRESULT WINAPI TiffFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1755 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1757 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1758 return E_NOTIMPL;
1761 static const IWICBitmapFrameEncodeVtbl TiffFrameEncode_Vtbl = {
1762 TiffFrameEncode_QueryInterface,
1763 TiffFrameEncode_AddRef,
1764 TiffFrameEncode_Release,
1765 TiffFrameEncode_Initialize,
1766 TiffFrameEncode_SetSize,
1767 TiffFrameEncode_SetResolution,
1768 TiffFrameEncode_SetPixelFormat,
1769 TiffFrameEncode_SetColorContexts,
1770 TiffFrameEncode_SetPalette,
1771 TiffFrameEncode_SetThumbnail,
1772 TiffFrameEncode_WritePixels,
1773 TiffFrameEncode_WriteSource,
1774 TiffFrameEncode_Commit,
1775 TiffFrameEncode_GetMetadataQueryWriter
1778 static HRESULT WINAPI TiffEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1779 void **ppv)
1781 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1782 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1784 if (!ppv) return E_INVALIDARG;
1786 if (IsEqualIID(&IID_IUnknown, iid) ||
1787 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1789 *ppv = &This->IWICBitmapEncoder_iface;
1791 else
1793 *ppv = NULL;
1794 return E_NOINTERFACE;
1797 IUnknown_AddRef((IUnknown*)*ppv);
1798 return S_OK;
1801 static ULONG WINAPI TiffEncoder_AddRef(IWICBitmapEncoder *iface)
1803 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1804 ULONG ref = InterlockedIncrement(&This->ref);
1806 TRACE("(%p) refcount=%u\n", iface, ref);
1808 return ref;
1811 static ULONG WINAPI TiffEncoder_Release(IWICBitmapEncoder *iface)
1813 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1814 ULONG ref = InterlockedDecrement(&This->ref);
1816 TRACE("(%p) refcount=%u\n", iface, ref);
1818 if (ref == 0)
1820 if (This->tiff) pTIFFClose(This->tiff);
1821 if (This->stream) IStream_Release(This->stream);
1822 This->lock.DebugInfo->Spare[0] = 0;
1823 DeleteCriticalSection(&This->lock);
1824 HeapFree(GetProcessHeap(), 0, This);
1827 return ref;
1830 static HRESULT WINAPI TiffEncoder_Initialize(IWICBitmapEncoder *iface,
1831 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1833 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1834 TIFF *tiff;
1835 HRESULT hr=S_OK;
1837 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1839 EnterCriticalSection(&This->lock);
1841 if (This->initialized || This->committed)
1843 hr = WINCODEC_ERR_WRONGSTATE;
1844 goto exit;
1847 tiff = tiff_open_stream(pIStream, "w");
1849 if (!tiff)
1851 hr = E_FAIL;
1852 goto exit;
1855 This->tiff = tiff;
1856 This->stream = pIStream;
1857 IStream_AddRef(pIStream);
1858 This->initialized = TRUE;
1860 exit:
1861 LeaveCriticalSection(&This->lock);
1862 return hr;
1865 static HRESULT WINAPI TiffEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1866 GUID *pguidContainerFormat)
1868 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
1869 return S_OK;
1872 static HRESULT WINAPI TiffEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1873 IWICBitmapEncoderInfo **ppIEncoderInfo)
1875 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1876 return E_NOTIMPL;
1879 static HRESULT WINAPI TiffEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1880 UINT cCount, IWICColorContext **ppIColorContext)
1882 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1883 return E_NOTIMPL;
1886 static HRESULT WINAPI TiffEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1888 TRACE("(%p,%p)\n", iface, pIPalette);
1889 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1892 static HRESULT WINAPI TiffEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1894 TRACE("(%p,%p)\n", iface, pIThumbnail);
1895 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1898 static HRESULT WINAPI TiffEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1900 TRACE("(%p,%p)\n", iface, pIPreview);
1901 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1904 static HRESULT WINAPI TiffEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1905 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1907 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1908 TiffFrameEncode *result;
1910 HRESULT hr=S_OK;
1912 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1914 EnterCriticalSection(&This->lock);
1916 if (!This->initialized || This->committed)
1918 hr = WINCODEC_ERR_WRONGSTATE;
1920 else if (This->num_frames != This->num_frames_committed)
1922 FIXME("New frame created before previous frame was committed\n");
1923 hr = E_FAIL;
1926 if (SUCCEEDED(hr))
1928 PROPBAG2 opts[2]= {{0}};
1929 opts[0].pstrName = (LPOLESTR)wszTiffCompressionMethod;
1930 opts[0].vt = VT_UI1;
1931 opts[0].dwType = PROPBAG2_TYPE_DATA;
1933 opts[1].pstrName = (LPOLESTR)wszCompressionQuality;
1934 opts[1].vt = VT_R4;
1935 opts[1].dwType = PROPBAG2_TYPE_DATA;
1937 hr = CreatePropertyBag2(opts, 2, ppIEncoderOptions);
1939 if (SUCCEEDED(hr))
1941 VARIANT v;
1942 VariantInit(&v);
1943 V_VT(&v) = VT_UI1;
1944 V_UNION(&v, bVal) = WICTiffCompressionDontCare;
1945 hr = IPropertyBag2_Write(*ppIEncoderOptions, 1, opts, &v);
1946 VariantClear(&v);
1947 if (FAILED(hr))
1949 IPropertyBag2_Release(*ppIEncoderOptions);
1950 *ppIEncoderOptions = NULL;
1955 if (SUCCEEDED(hr))
1957 result = HeapAlloc(GetProcessHeap(), 0, sizeof(*result));
1959 if (result)
1961 result->IWICBitmapFrameEncode_iface.lpVtbl = &TiffFrameEncode_Vtbl;
1962 result->ref = 1;
1963 result->parent = This;
1964 result->initialized = FALSE;
1965 result->info_written = FALSE;
1966 result->committed = FALSE;
1967 result->format = NULL;
1968 result->width = 0;
1969 result->height = 0;
1970 result->xres = 0.0;
1971 result->yres = 0.0;
1972 result->lines_written = 0;
1974 IWICBitmapEncoder_AddRef(iface);
1975 *ppIFrameEncode = &result->IWICBitmapFrameEncode_iface;
1977 if (This->num_frames != 0)
1978 pTIFFWriteDirectory(This->tiff);
1980 This->num_frames++;
1982 else
1983 hr = E_OUTOFMEMORY;
1985 if (FAILED(hr))
1987 IPropertyBag2_Release(*ppIEncoderOptions);
1988 *ppIEncoderOptions = NULL;
1992 LeaveCriticalSection(&This->lock);
1994 return hr;
1997 static HRESULT WINAPI TiffEncoder_Commit(IWICBitmapEncoder *iface)
1999 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
2001 TRACE("(%p)\n", iface);
2003 EnterCriticalSection(&This->lock);
2005 if (!This->initialized || This->committed)
2007 LeaveCriticalSection(&This->lock);
2008 return WINCODEC_ERR_WRONGSTATE;
2011 pTIFFClose(This->tiff);
2012 IStream_Release(This->stream);
2013 This->stream = NULL;
2014 This->tiff = NULL;
2016 This->committed = TRUE;
2018 LeaveCriticalSection(&This->lock);
2020 return S_OK;
2023 static HRESULT WINAPI TiffEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
2024 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
2026 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
2027 return E_NOTIMPL;
2030 static const IWICBitmapEncoderVtbl TiffEncoder_Vtbl = {
2031 TiffEncoder_QueryInterface,
2032 TiffEncoder_AddRef,
2033 TiffEncoder_Release,
2034 TiffEncoder_Initialize,
2035 TiffEncoder_GetContainerFormat,
2036 TiffEncoder_GetEncoderInfo,
2037 TiffEncoder_SetColorContexts,
2038 TiffEncoder_SetPalette,
2039 TiffEncoder_SetThumbnail,
2040 TiffEncoder_SetPreview,
2041 TiffEncoder_CreateNewFrame,
2042 TiffEncoder_Commit,
2043 TiffEncoder_GetMetadataQueryWriter
2046 HRESULT TiffEncoder_CreateInstance(REFIID iid, void** ppv)
2048 TiffEncoder *This;
2049 HRESULT ret;
2051 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
2053 *ppv = NULL;
2055 if (!load_libtiff())
2057 ERR("Failed writing TIFF because unable to load %s\n",SONAME_LIBTIFF);
2058 return E_FAIL;
2061 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffEncoder));
2062 if (!This) return E_OUTOFMEMORY;
2064 This->IWICBitmapEncoder_iface.lpVtbl = &TiffEncoder_Vtbl;
2065 This->ref = 1;
2066 This->stream = NULL;
2067 InitializeCriticalSection(&This->lock);
2068 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffEncoder.lock");
2069 This->tiff = NULL;
2070 This->initialized = FALSE;
2071 This->num_frames = 0;
2072 This->num_frames_committed = 0;
2073 This->committed = FALSE;
2075 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
2076 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
2078 return ret;
2081 #else /* !SONAME_LIBTIFF */
2083 HRESULT TiffDecoder_CreateInstance(REFIID iid, void** ppv)
2085 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
2086 return E_FAIL;
2089 HRESULT TiffEncoder_CreateInstance(REFIID iid, void** ppv)
2091 ERR("Trying to save TIFF picture, but Wine was compiled without TIFF support.\n");
2092 return E_FAIL;
2095 #endif