oledb32/tests: Fix incorrect usage of broken() function.
[wine.git] / dlls / windowscodecs / tiffformat.c
blob1dacd87d004cd476b4bae76308f52bbd0b5f8f3d
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"
36 #include "wincodecs_private.h"
38 #include "wine/debug.h"
39 #include "wine/library.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
43 #ifdef SONAME_LIBTIFF
45 /* Workaround for broken libtiff 4.x headers on some 64-bit hosts which
46 * define TIFF_UINT64_T/toff_t as 32-bit for 32-bit builds, while they
47 * are supposed to be always 64-bit.
48 * TIFF_UINT64_T doesn't exist in libtiff 3.x, it was introduced in 4.x.
50 #ifdef TIFF_UINT64_T
51 # undef toff_t
52 # define toff_t UINT64
53 #endif
55 static CRITICAL_SECTION init_tiff_cs;
56 static CRITICAL_SECTION_DEBUG init_tiff_cs_debug =
58 0, 0, &init_tiff_cs,
59 { &init_tiff_cs_debug.ProcessLocksList,
60 &init_tiff_cs_debug.ProcessLocksList },
61 0, 0, { (DWORD_PTR)(__FILE__ ": init_tiff_cs") }
63 static CRITICAL_SECTION init_tiff_cs = { &init_tiff_cs_debug, -1, 0, 0, 0, 0 };
65 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};
66 static const WCHAR wszCompressionQuality[] = {'C','o','m','p','r','e','s','s','i','o','n','Q','u','a','l','i','t','y',0};
68 static void *libtiff_handle;
69 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
70 MAKE_FUNCPTR(TIFFClientOpen);
71 MAKE_FUNCPTR(TIFFClose);
72 MAKE_FUNCPTR(TIFFCurrentDirOffset);
73 MAKE_FUNCPTR(TIFFGetField);
74 MAKE_FUNCPTR(TIFFIsByteSwapped);
75 MAKE_FUNCPTR(TIFFNumberOfDirectories);
76 MAKE_FUNCPTR(TIFFReadDirectory);
77 MAKE_FUNCPTR(TIFFReadEncodedStrip);
78 MAKE_FUNCPTR(TIFFReadEncodedTile);
79 MAKE_FUNCPTR(TIFFSetDirectory);
80 MAKE_FUNCPTR(TIFFSetField);
81 MAKE_FUNCPTR(TIFFWriteDirectory);
82 MAKE_FUNCPTR(TIFFWriteScanline);
83 #undef MAKE_FUNCPTR
85 static void *load_libtiff(void)
87 void *result;
89 EnterCriticalSection(&init_tiff_cs);
91 if (!libtiff_handle &&
92 (libtiff_handle = wine_dlopen(SONAME_LIBTIFF, RTLD_NOW, NULL, 0)) != NULL)
94 void * (*pTIFFSetWarningHandler)(void *);
95 void * (*pTIFFSetWarningHandlerExt)(void *);
97 #define LOAD_FUNCPTR(f) \
98 if((p##f = wine_dlsym(libtiff_handle, #f, NULL, 0)) == NULL) { \
99 ERR("failed to load symbol %s\n", #f); \
100 libtiff_handle = NULL; \
101 LeaveCriticalSection(&init_tiff_cs); \
102 return NULL; \
104 LOAD_FUNCPTR(TIFFClientOpen);
105 LOAD_FUNCPTR(TIFFClose);
106 LOAD_FUNCPTR(TIFFCurrentDirOffset);
107 LOAD_FUNCPTR(TIFFGetField);
108 LOAD_FUNCPTR(TIFFIsByteSwapped);
109 LOAD_FUNCPTR(TIFFNumberOfDirectories);
110 LOAD_FUNCPTR(TIFFReadDirectory);
111 LOAD_FUNCPTR(TIFFReadEncodedStrip);
112 LOAD_FUNCPTR(TIFFReadEncodedTile);
113 LOAD_FUNCPTR(TIFFSetDirectory);
114 LOAD_FUNCPTR(TIFFSetField);
115 LOAD_FUNCPTR(TIFFWriteDirectory);
116 LOAD_FUNCPTR(TIFFWriteScanline);
117 #undef LOAD_FUNCPTR
119 if ((pTIFFSetWarningHandler = wine_dlsym(libtiff_handle, "TIFFSetWarningHandler", NULL, 0)))
120 pTIFFSetWarningHandler(NULL);
121 if ((pTIFFSetWarningHandlerExt = wine_dlsym(libtiff_handle, "TIFFSetWarningHandlerExt", NULL, 0)))
122 pTIFFSetWarningHandlerExt(NULL);
125 result = libtiff_handle;
127 LeaveCriticalSection(&init_tiff_cs);
128 return result;
131 static tsize_t tiff_stream_read(thandle_t client_data, tdata_t data, tsize_t size)
133 IStream *stream = (IStream*)client_data;
134 ULONG bytes_read;
135 HRESULT hr;
137 hr = IStream_Read(stream, data, size, &bytes_read);
138 if (FAILED(hr)) bytes_read = 0;
139 return bytes_read;
142 static tsize_t tiff_stream_write(thandle_t client_data, tdata_t data, tsize_t size)
144 IStream *stream = (IStream*)client_data;
145 ULONG bytes_written;
146 HRESULT hr;
148 hr = IStream_Write(stream, data, size, &bytes_written);
149 if (FAILED(hr)) bytes_written = 0;
150 return bytes_written;
153 static toff_t tiff_stream_seek(thandle_t client_data, toff_t offset, int whence)
155 IStream *stream = (IStream*)client_data;
156 LARGE_INTEGER move;
157 DWORD origin;
158 ULARGE_INTEGER new_position;
159 HRESULT hr;
161 move.QuadPart = offset;
162 switch (whence)
164 case SEEK_SET:
165 origin = STREAM_SEEK_SET;
166 break;
167 case SEEK_CUR:
168 origin = STREAM_SEEK_CUR;
169 break;
170 case SEEK_END:
171 origin = STREAM_SEEK_END;
172 break;
173 default:
174 ERR("unknown whence value %i\n", whence);
175 return -1;
178 hr = IStream_Seek(stream, move, origin, &new_position);
179 if (SUCCEEDED(hr)) return new_position.QuadPart;
180 else return -1;
183 static int tiff_stream_close(thandle_t client_data)
185 /* Caller is responsible for releasing the stream object. */
186 return 0;
189 static toff_t tiff_stream_size(thandle_t client_data)
191 IStream *stream = (IStream*)client_data;
192 STATSTG statstg;
193 HRESULT hr;
195 hr = IStream_Stat(stream, &statstg, STATFLAG_NONAME);
197 if (SUCCEEDED(hr)) return statstg.cbSize.QuadPart;
198 else return -1;
201 static int tiff_stream_map(thandle_t client_data, tdata_t *addr, toff_t *size)
203 /* Cannot mmap streams */
204 return 0;
207 static void tiff_stream_unmap(thandle_t client_data, tdata_t addr, toff_t size)
209 /* No need to ever do this, since we can't map things. */
212 static TIFF* tiff_open_stream(IStream *stream, const char *mode)
214 LARGE_INTEGER zero;
216 zero.QuadPart = 0;
217 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
219 return pTIFFClientOpen("<IStream object>", mode, stream, tiff_stream_read,
220 tiff_stream_write, (void *)tiff_stream_seek, tiff_stream_close,
221 (void *)tiff_stream_size, (void *)tiff_stream_map, (void *)tiff_stream_unmap);
224 typedef struct {
225 IWICBitmapDecoder IWICBitmapDecoder_iface;
226 LONG ref;
227 IStream *stream;
228 CRITICAL_SECTION lock; /* Must be held when tiff is used or initialized is set */
229 TIFF *tiff;
230 BOOL initialized;
231 } TiffDecoder;
233 typedef struct {
234 const WICPixelFormatGUID *format;
235 int bps;
236 int samples;
237 int bpp, source_bpp;
238 int planar;
239 int indexed;
240 int reverse_bgr;
241 int invert_grayscale;
242 UINT width, height;
243 UINT tile_width, tile_height;
244 UINT tile_stride;
245 UINT tile_size;
246 int tiled;
247 UINT tiles_across;
248 UINT resolution_unit;
249 float xres, yres;
250 } tiff_decode_info;
252 typedef struct {
253 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
254 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
255 LONG ref;
256 TiffDecoder *parent;
257 UINT index;
258 tiff_decode_info decode_info;
259 INT cached_tile_x, cached_tile_y;
260 BYTE *cached_tile;
261 } TiffFrameDecode;
263 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl;
264 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl;
266 static inline TiffDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
268 return CONTAINING_RECORD(iface, TiffDecoder, IWICBitmapDecoder_iface);
271 static inline TiffFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
273 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICBitmapFrameDecode_iface);
276 static inline TiffFrameDecode *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
278 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICMetadataBlockReader_iface);
281 static HRESULT tiff_get_decode_info(TIFF *tiff, tiff_decode_info *decode_info)
283 uint16 photometric, bps, samples, planar;
284 uint16 extra_sample_count, extra_sample, *extra_samples;
285 int ret;
287 decode_info->indexed = 0;
288 decode_info->reverse_bgr = 0;
289 decode_info->invert_grayscale = 0;
290 decode_info->tiled = 0;
292 ret = pTIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
293 if (!ret)
295 WARN("missing PhotometricInterpretation tag\n");
296 return E_FAIL;
299 ret = pTIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bps);
300 if (!ret) bps = 1;
301 decode_info->bps = bps;
303 ret = pTIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samples);
304 if (!ret) samples = 1;
305 decode_info->samples = samples;
307 if (samples == 1)
308 planar = 1;
309 else
311 ret = pTIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planar);
312 if (!ret) planar = 1;
313 if (planar != 1)
315 FIXME("unhandled planar configuration %u\n", planar);
316 return E_FAIL;
319 decode_info->planar = planar;
321 switch(photometric)
323 case 0: /* WhiteIsZero */
324 decode_info->invert_grayscale = 1;
325 /* fall through */
326 case 1: /* BlackIsZero */
327 if (samples == 2)
329 ret = pTIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extra_sample_count, &extra_samples);
330 if (!ret)
332 extra_sample_count = 1;
333 extra_sample = 0;
334 extra_samples = &extra_sample;
337 else if (samples != 1)
339 FIXME("unhandled %dbpp sample count %u\n", bps, samples);
340 return E_FAIL;
343 decode_info->bpp = bps * samples;
344 decode_info->source_bpp = decode_info->bpp;
345 switch (bps)
347 case 1:
348 if (samples != 1)
350 FIXME("unhandled 1bpp sample count %u\n", samples);
351 return E_FAIL;
353 decode_info->format = &GUID_WICPixelFormatBlackWhite;
354 break;
355 case 4:
356 if (samples != 1)
358 FIXME("unhandled 4bpp grayscale sample count %u\n", samples);
359 return E_FAIL;
361 decode_info->format = &GUID_WICPixelFormat4bppGray;
362 break;
363 case 8:
364 if (samples == 1)
365 decode_info->format = &GUID_WICPixelFormat8bppGray;
366 else
368 decode_info->bpp = 32;
370 switch(extra_samples[0])
372 case 1: /* Associated (pre-multiplied) alpha data */
373 decode_info->format = &GUID_WICPixelFormat32bppPBGRA;
374 break;
375 case 0: /* Unspecified data */
376 case 2: /* Unassociated alpha data */
377 decode_info->format = &GUID_WICPixelFormat32bppBGRA;
378 break;
379 default:
380 FIXME("unhandled extra sample type %u\n", extra_samples[0]);
381 return E_FAIL;
384 break;
385 default:
386 FIXME("unhandled greyscale bit count %u\n", bps);
387 return E_FAIL;
389 break;
390 case 2: /* RGB */
391 decode_info->bpp = bps * samples;
393 if (samples == 4)
395 ret = pTIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extra_sample_count, &extra_samples);
396 if (!ret)
398 extra_sample_count = 1;
399 extra_sample = 0;
400 extra_samples = &extra_sample;
403 else if (samples != 3)
405 FIXME("unhandled RGB sample count %u\n", samples);
406 return E_FAIL;
409 switch(bps)
411 case 8:
412 decode_info->reverse_bgr = 1;
413 if (samples == 3)
414 decode_info->format = &GUID_WICPixelFormat24bppBGR;
415 else
416 switch(extra_samples[0])
418 case 1: /* Associated (pre-multiplied) alpha data */
419 decode_info->format = &GUID_WICPixelFormat32bppPBGRA;
420 break;
421 case 0: /* Unspecified data */
422 case 2: /* Unassociated alpha data */
423 decode_info->format = &GUID_WICPixelFormat32bppBGRA;
424 break;
425 default:
426 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
427 return E_FAIL;
429 break;
430 case 16:
431 if (samples == 3)
432 decode_info->format = &GUID_WICPixelFormat48bppRGB;
433 else
434 switch(extra_samples[0])
436 case 1: /* Associated (pre-multiplied) alpha data */
437 decode_info->format = &GUID_WICPixelFormat64bppPRGBA;
438 break;
439 case 0: /* Unspecified data */
440 case 2: /* Unassociated alpha data */
441 decode_info->format = &GUID_WICPixelFormat64bppRGBA;
442 break;
443 default:
444 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
445 return E_FAIL;
447 break;
448 default:
449 FIXME("unhandled RGB bit count %u\n", bps);
450 return E_FAIL;
452 break;
453 case 3: /* RGB Palette */
454 if (samples != 1)
456 FIXME("unhandled indexed sample count %u\n", samples);
457 return E_FAIL;
460 decode_info->indexed = 1;
461 decode_info->bpp = bps;
462 switch (bps)
464 case 4:
465 decode_info->format = &GUID_WICPixelFormat4bppIndexed;
466 break;
467 case 8:
468 decode_info->format = &GUID_WICPixelFormat8bppIndexed;
469 break;
470 default:
471 FIXME("unhandled indexed bit count %u\n", bps);
472 return E_FAIL;
474 break;
475 case 4: /* Transparency mask */
476 case 5: /* CMYK */
477 case 6: /* YCbCr */
478 case 8: /* CIELab */
479 default:
480 FIXME("unhandled PhotometricInterpretation %u\n", photometric);
481 return E_FAIL;
484 ret = pTIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &decode_info->width);
485 if (!ret)
487 WARN("missing image width\n");
488 return E_FAIL;
491 ret = pTIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &decode_info->height);
492 if (!ret)
494 WARN("missing image length\n");
495 return E_FAIL;
498 if ((ret = pTIFFGetField(tiff, TIFFTAG_TILEWIDTH, &decode_info->tile_width)))
500 decode_info->tiled = 1;
502 ret = pTIFFGetField(tiff, TIFFTAG_TILELENGTH, &decode_info->tile_height);
503 if (!ret)
505 WARN("missing tile height\n");
506 return E_FAIL;
509 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
510 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
511 decode_info->tiles_across = (decode_info->width + decode_info->tile_width - 1) / decode_info->tile_width;
513 else if ((ret = pTIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &decode_info->tile_height)))
515 if (decode_info->tile_height > decode_info->height)
516 decode_info->tile_height = decode_info->height;
517 decode_info->tile_width = decode_info->width;
518 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
519 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
521 else
523 /* Some broken TIFF files have a single strip and lack the RowsPerStrip tag */
524 decode_info->tile_height = decode_info->height;
525 decode_info->tile_width = decode_info->width;
526 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
527 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
530 decode_info->resolution_unit = 0;
531 pTIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &decode_info->resolution_unit);
532 if (decode_info->resolution_unit != 0)
534 ret = pTIFFGetField(tiff, TIFFTAG_XRESOLUTION, &decode_info->xres);
535 if (!ret)
537 WARN("missing X resolution\n");
538 decode_info->resolution_unit = 0;
541 ret = pTIFFGetField(tiff, TIFFTAG_YRESOLUTION, &decode_info->yres);
542 if (!ret)
544 WARN("missing Y resolution\n");
545 decode_info->resolution_unit = 0;
549 return S_OK;
552 static HRESULT WINAPI TiffDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
553 void **ppv)
555 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
556 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
558 if (!ppv) return E_INVALIDARG;
560 if (IsEqualIID(&IID_IUnknown, iid) ||
561 IsEqualIID(&IID_IWICBitmapDecoder, iid))
563 *ppv = &This->IWICBitmapDecoder_iface;
565 else
567 *ppv = NULL;
568 return E_NOINTERFACE;
571 IUnknown_AddRef((IUnknown*)*ppv);
572 return S_OK;
575 static ULONG WINAPI TiffDecoder_AddRef(IWICBitmapDecoder *iface)
577 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
578 ULONG ref = InterlockedIncrement(&This->ref);
580 TRACE("(%p) refcount=%u\n", iface, ref);
582 return ref;
585 static ULONG WINAPI TiffDecoder_Release(IWICBitmapDecoder *iface)
587 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
588 ULONG ref = InterlockedDecrement(&This->ref);
590 TRACE("(%p) refcount=%u\n", iface, ref);
592 if (ref == 0)
594 if (This->tiff) pTIFFClose(This->tiff);
595 if (This->stream) IStream_Release(This->stream);
596 This->lock.DebugInfo->Spare[0] = 0;
597 DeleteCriticalSection(&This->lock);
598 HeapFree(GetProcessHeap(), 0, This);
601 return ref;
604 static HRESULT WINAPI TiffDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
605 DWORD *capability)
607 HRESULT hr;
609 TRACE("(%p,%p,%p)\n", iface, stream, capability);
611 if (!stream || !capability) return E_INVALIDARG;
613 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
614 if (hr != S_OK) return hr;
616 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
617 WICBitmapDecoderCapabilityCanDecodeSomeImages |
618 WICBitmapDecoderCapabilityCanEnumerateMetadata;
619 return S_OK;
622 static HRESULT WINAPI TiffDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
623 WICDecodeOptions cacheOptions)
625 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
626 TIFF *tiff;
627 HRESULT hr=S_OK;
629 TRACE("(%p,%p,%x): stub\n", iface, pIStream, cacheOptions);
631 EnterCriticalSection(&This->lock);
633 if (This->initialized)
635 hr = WINCODEC_ERR_WRONGSTATE;
636 goto exit;
639 tiff = tiff_open_stream(pIStream, "r");
641 if (!tiff)
643 hr = E_FAIL;
644 goto exit;
647 This->tiff = tiff;
648 This->stream = pIStream;
649 IStream_AddRef(pIStream);
650 This->initialized = TRUE;
652 exit:
653 LeaveCriticalSection(&This->lock);
654 return hr;
657 static HRESULT WINAPI TiffDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
658 GUID *pguidContainerFormat)
660 if (!pguidContainerFormat) return E_INVALIDARG;
662 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
663 return S_OK;
666 static HRESULT WINAPI TiffDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
667 IWICBitmapDecoderInfo **ppIDecoderInfo)
669 HRESULT hr;
670 IWICComponentInfo *compinfo;
672 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
674 hr = CreateComponentInfo(&CLSID_WICTiffDecoder, &compinfo);
675 if (FAILED(hr)) return hr;
677 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
678 (void**)ppIDecoderInfo);
680 IWICComponentInfo_Release(compinfo);
682 return hr;
685 static HRESULT WINAPI TiffDecoder_CopyPalette(IWICBitmapDecoder *iface,
686 IWICPalette *pIPalette)
688 FIXME("(%p,%p): stub\n", iface, pIPalette);
689 return E_NOTIMPL;
692 static HRESULT WINAPI TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
693 IWICMetadataQueryReader **ppIMetadataQueryReader)
695 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
696 return E_NOTIMPL;
699 static HRESULT WINAPI TiffDecoder_GetPreview(IWICBitmapDecoder *iface,
700 IWICBitmapSource **ppIBitmapSource)
702 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
704 if (!ppIBitmapSource) return E_INVALIDARG;
706 *ppIBitmapSource = NULL;
707 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
710 static HRESULT WINAPI TiffDecoder_GetColorContexts(IWICBitmapDecoder *iface,
711 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
713 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
714 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
717 static HRESULT WINAPI TiffDecoder_GetThumbnail(IWICBitmapDecoder *iface,
718 IWICBitmapSource **ppIThumbnail)
720 TRACE("(%p,%p)\n", iface, ppIThumbnail);
722 if (!ppIThumbnail) return E_INVALIDARG;
724 *ppIThumbnail = NULL;
725 return WINCODEC_ERR_CODECNOTHUMBNAIL;
728 static HRESULT WINAPI TiffDecoder_GetFrameCount(IWICBitmapDecoder *iface,
729 UINT *pCount)
731 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
733 if (!pCount) return E_INVALIDARG;
735 EnterCriticalSection(&This->lock);
736 *pCount = This->tiff ? pTIFFNumberOfDirectories(This->tiff) : 0;
737 LeaveCriticalSection(&This->lock);
739 TRACE("(%p) <-- %i\n", iface, *pCount);
741 return S_OK;
744 static HRESULT WINAPI TiffDecoder_GetFrame(IWICBitmapDecoder *iface,
745 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
747 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
748 TiffFrameDecode *result;
749 int res;
750 tiff_decode_info decode_info;
751 HRESULT hr;
753 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
755 if (!This->tiff)
756 return WINCODEC_ERR_FRAMEMISSING;
758 EnterCriticalSection(&This->lock);
759 res = pTIFFSetDirectory(This->tiff, index);
760 if (!res) hr = E_INVALIDARG;
761 else hr = tiff_get_decode_info(This->tiff, &decode_info);
762 LeaveCriticalSection(&This->lock);
764 if (SUCCEEDED(hr))
766 result = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode));
768 if (result)
770 result->IWICBitmapFrameDecode_iface.lpVtbl = &TiffFrameDecode_Vtbl;
771 result->IWICMetadataBlockReader_iface.lpVtbl = &TiffFrameDecode_BlockVtbl;
772 result->ref = 1;
773 result->parent = This;
774 result->index = index;
775 result->decode_info = decode_info;
776 result->cached_tile_x = -1;
777 result->cached_tile = HeapAlloc(GetProcessHeap(), 0, decode_info.tile_size);
779 if (result->cached_tile)
780 *ppIBitmapFrame = &result->IWICBitmapFrameDecode_iface;
781 else
783 hr = E_OUTOFMEMORY;
784 HeapFree(GetProcessHeap(), 0, result);
787 else hr = E_OUTOFMEMORY;
790 if (FAILED(hr)) *ppIBitmapFrame = NULL;
792 return hr;
795 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl = {
796 TiffDecoder_QueryInterface,
797 TiffDecoder_AddRef,
798 TiffDecoder_Release,
799 TiffDecoder_QueryCapability,
800 TiffDecoder_Initialize,
801 TiffDecoder_GetContainerFormat,
802 TiffDecoder_GetDecoderInfo,
803 TiffDecoder_CopyPalette,
804 TiffDecoder_GetMetadataQueryReader,
805 TiffDecoder_GetPreview,
806 TiffDecoder_GetColorContexts,
807 TiffDecoder_GetThumbnail,
808 TiffDecoder_GetFrameCount,
809 TiffDecoder_GetFrame
812 static HRESULT WINAPI TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
813 void **ppv)
815 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
816 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
818 if (!ppv) return E_INVALIDARG;
820 if (IsEqualIID(&IID_IUnknown, iid) ||
821 IsEqualIID(&IID_IWICBitmapSource, iid) ||
822 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
824 *ppv = &This->IWICBitmapFrameDecode_iface;
826 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
828 *ppv = &This->IWICMetadataBlockReader_iface;
830 else
832 *ppv = NULL;
833 return E_NOINTERFACE;
836 IUnknown_AddRef((IUnknown*)*ppv);
837 return S_OK;
840 static ULONG WINAPI TiffFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
842 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
843 ULONG ref = InterlockedIncrement(&This->ref);
845 TRACE("(%p) refcount=%u\n", iface, ref);
847 return ref;
850 static ULONG WINAPI TiffFrameDecode_Release(IWICBitmapFrameDecode *iface)
852 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
853 ULONG ref = InterlockedDecrement(&This->ref);
855 TRACE("(%p) refcount=%u\n", iface, ref);
857 if (ref == 0)
859 HeapFree(GetProcessHeap(), 0, This->cached_tile);
860 HeapFree(GetProcessHeap(), 0, This);
863 return ref;
866 static HRESULT WINAPI TiffFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
867 UINT *puiWidth, UINT *puiHeight)
869 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
871 *puiWidth = This->decode_info.width;
872 *puiHeight = This->decode_info.height;
874 TRACE("(%p) <-- %ux%u\n", iface, *puiWidth, *puiHeight);
876 return S_OK;
879 static HRESULT WINAPI TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
880 WICPixelFormatGUID *pPixelFormat)
882 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
884 memcpy(pPixelFormat, This->decode_info.format, sizeof(GUID));
886 TRACE("(%p) <-- %s\n", This, debugstr_guid(This->decode_info.format));
888 return S_OK;
891 static HRESULT WINAPI TiffFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
892 double *pDpiX, double *pDpiY)
894 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
896 switch (This->decode_info.resolution_unit)
898 default:
899 FIXME("unknown resolution unit %i\n", This->decode_info.resolution_unit);
900 /* fall through */
901 case 0: /* Not set */
902 *pDpiX = *pDpiY = 96.0;
903 break;
904 case 1: /* Relative measurements */
905 *pDpiX = 96.0;
906 *pDpiY = 96.0 * This->decode_info.yres / This->decode_info.xres;
907 break;
908 case 2: /* Inch */
909 *pDpiX = This->decode_info.xres;
910 *pDpiY = This->decode_info.yres;
911 break;
912 case 3: /* Centimeter */
913 *pDpiX = This->decode_info.xres / 2.54;
914 *pDpiY = This->decode_info.yres / 2.54;
915 break;
918 TRACE("(%p) <-- %f,%f unit=%i\n", iface, *pDpiX, *pDpiY, This->decode_info.resolution_unit);
920 return S_OK;
923 static HRESULT WINAPI TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
924 IWICPalette *pIPalette)
926 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
927 uint16 *red, *green, *blue;
928 WICColor colors[256];
929 int color_count, ret, i;
931 TRACE("(%p,%p)\n", iface, pIPalette);
933 color_count = 1<<This->decode_info.bps;
935 EnterCriticalSection(&This->parent->lock);
936 ret = pTIFFGetField(This->parent->tiff, TIFFTAG_COLORMAP, &red, &green, &blue);
937 LeaveCriticalSection(&This->parent->lock);
939 if (!ret)
941 WARN("Couldn't read color map\n");
942 return WINCODEC_ERR_PALETTEUNAVAILABLE;
945 for (i=0; i<color_count; i++)
947 colors[i] = 0xff000000 |
948 ((red[i]<<8) & 0xff0000) |
949 (green[i] & 0xff00) |
950 ((blue[i]>>8) & 0xff);
953 return IWICPalette_InitializeCustom(pIPalette, colors, color_count);
956 static HRESULT TiffFrameDecode_ReadTile(TiffFrameDecode *This, UINT tile_x, UINT tile_y)
958 HRESULT hr=S_OK;
959 tsize_t ret;
960 int swap_bytes;
962 swap_bytes = pTIFFIsByteSwapped(This->parent->tiff);
964 ret = pTIFFSetDirectory(This->parent->tiff, This->index);
966 if (ret == -1)
967 hr = E_FAIL;
969 if (hr == S_OK)
971 if (This->decode_info.tiled)
973 ret = pTIFFReadEncodedTile(This->parent->tiff, tile_x + tile_y * This->decode_info.tiles_across, This->cached_tile, This->decode_info.tile_size);
975 else
977 ret = pTIFFReadEncodedStrip(This->parent->tiff, tile_y, This->cached_tile, This->decode_info.tile_size);
980 if (ret == -1)
981 hr = E_FAIL;
984 /* 8bpp grayscale with extra alpha */
985 if (hr == S_OK && This->decode_info.source_bpp == 16 && This->decode_info.samples == 2 && This->decode_info.bpp == 32)
987 BYTE *src;
988 DWORD *dst, count = This->decode_info.tile_width * This->decode_info.tile_height;
990 src = This->cached_tile + This->decode_info.tile_width * This->decode_info.tile_height * 2 - 2;
991 dst = (DWORD *)(This->cached_tile + This->decode_info.tile_size - 4);
993 while (count--)
995 *dst-- = src[0] | (src[0] << 8) | (src[0] << 16) | (src[1] << 24);
996 src -= 2;
1000 if (hr == S_OK && This->decode_info.reverse_bgr)
1002 if (This->decode_info.bps == 8)
1004 UINT sample_count = This->decode_info.samples;
1006 reverse_bgr8(sample_count, This->cached_tile, This->decode_info.tile_width,
1007 This->decode_info.tile_height, This->decode_info.tile_width * sample_count);
1011 if (hr == S_OK && swap_bytes && This->decode_info.bps > 8)
1013 UINT row, i, samples_per_row;
1014 BYTE *sample, temp;
1016 samples_per_row = This->decode_info.tile_width * This->decode_info.samples;
1018 switch(This->decode_info.bps)
1020 case 16:
1021 for (row=0; row<This->decode_info.tile_height; row++)
1023 sample = This->cached_tile + row * This->decode_info.tile_stride;
1024 for (i=0; i<samples_per_row; i++)
1026 temp = sample[1];
1027 sample[1] = sample[0];
1028 sample[0] = temp;
1029 sample += 2;
1032 break;
1033 default:
1034 ERR("unhandled bps for byte swap %u\n", This->decode_info.bps);
1035 return E_FAIL;
1039 if (hr == S_OK && This->decode_info.invert_grayscale)
1041 BYTE *byte, *end;
1043 if (This->decode_info.samples != 1)
1045 ERR("cannot invert grayscale image with %u samples\n", This->decode_info.samples);
1046 return E_FAIL;
1049 end = This->cached_tile+This->decode_info.tile_size;
1051 for (byte = This->cached_tile; byte != end; byte++)
1052 *byte = ~(*byte);
1055 if (hr == S_OK)
1057 This->cached_tile_x = tile_x;
1058 This->cached_tile_y = tile_y;
1061 return hr;
1064 static HRESULT WINAPI TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
1065 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
1067 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
1068 UINT min_tile_x, max_tile_x, min_tile_y, max_tile_y;
1069 UINT tile_x, tile_y;
1070 WICRect rc;
1071 HRESULT hr=S_OK;
1072 BYTE *dst_tilepos;
1073 UINT bytesperrow;
1074 WICRect rect;
1076 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
1078 if (!prc)
1080 rect.X = 0;
1081 rect.Y = 0;
1082 rect.Width = This->decode_info.width;
1083 rect.Height = This->decode_info.height;
1084 prc = &rect;
1086 else
1088 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->decode_info.width ||
1089 prc->Y+prc->Height > This->decode_info.height)
1090 return E_INVALIDARG;
1093 bytesperrow = ((This->decode_info.bpp * prc->Width)+7)/8;
1095 if (cbStride < bytesperrow)
1096 return E_INVALIDARG;
1098 if ((cbStride * prc->Height) > cbBufferSize)
1099 return E_INVALIDARG;
1101 min_tile_x = prc->X / This->decode_info.tile_width;
1102 min_tile_y = prc->Y / This->decode_info.tile_height;
1103 max_tile_x = (prc->X+prc->Width-1) / This->decode_info.tile_width;
1104 max_tile_y = (prc->Y+prc->Height-1) / This->decode_info.tile_height;
1106 EnterCriticalSection(&This->parent->lock);
1108 for (tile_x=min_tile_x; tile_x <= max_tile_x; tile_x++)
1110 for (tile_y=min_tile_y; tile_y <= max_tile_y; tile_y++)
1112 if (tile_x != This->cached_tile_x || tile_y != This->cached_tile_y)
1114 hr = TiffFrameDecode_ReadTile(This, tile_x, tile_y);
1117 if (SUCCEEDED(hr))
1119 if (prc->X < tile_x * This->decode_info.tile_width)
1120 rc.X = 0;
1121 else
1122 rc.X = prc->X - tile_x * This->decode_info.tile_width;
1124 if (prc->Y < tile_y * This->decode_info.tile_height)
1125 rc.Y = 0;
1126 else
1127 rc.Y = prc->Y - tile_y * This->decode_info.tile_height;
1129 if (prc->X+prc->Width > (tile_x+1) * This->decode_info.tile_width)
1130 rc.Width = This->decode_info.tile_width - rc.X;
1131 else if (prc->X < tile_x * This->decode_info.tile_width)
1132 rc.Width = prc->Width + prc->X - tile_x * This->decode_info.tile_width;
1133 else
1134 rc.Width = prc->Width;
1136 if (prc->Y+prc->Height > (tile_y+1) * This->decode_info.tile_height)
1137 rc.Height = This->decode_info.tile_height - rc.Y;
1138 else if (prc->Y < tile_y * This->decode_info.tile_height)
1139 rc.Height = prc->Height + prc->Y - tile_y * This->decode_info.tile_height;
1140 else
1141 rc.Height = prc->Height;
1143 dst_tilepos = pbBuffer + (cbStride * ((rc.Y + tile_y * This->decode_info.tile_height) - prc->Y)) +
1144 ((This->decode_info.bpp * ((rc.X + tile_x * This->decode_info.tile_width) - prc->X) + 7) / 8);
1146 hr = copy_pixels(This->decode_info.bpp, This->cached_tile,
1147 This->decode_info.tile_width, This->decode_info.tile_height, This->decode_info.tile_stride,
1148 &rc, cbStride, cbBufferSize, dst_tilepos);
1151 if (FAILED(hr))
1153 LeaveCriticalSection(&This->parent->lock);
1154 TRACE("<-- 0x%x\n", hr);
1155 return hr;
1160 LeaveCriticalSection(&This->parent->lock);
1162 return S_OK;
1165 static HRESULT WINAPI TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
1166 IWICMetadataQueryReader **ppIMetadataQueryReader)
1168 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
1170 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
1172 if (!ppIMetadataQueryReader)
1173 return E_INVALIDARG;
1175 return MetadataQueryReader_CreateInstance(&This->IWICMetadataBlockReader_iface, ppIMetadataQueryReader);
1178 static HRESULT WINAPI TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
1179 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1181 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
1182 const BYTE *profile;
1183 UINT len;
1184 HRESULT hr;
1186 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
1188 EnterCriticalSection(&This->parent->lock);
1190 if (pTIFFGetField(This->parent->tiff, TIFFTAG_ICCPROFILE, &len, &profile))
1192 if (cCount && ppIColorContexts)
1194 hr = IWICColorContext_InitializeFromMemory(*ppIColorContexts, profile, len);
1195 if (FAILED(hr))
1197 LeaveCriticalSection(&This->parent->lock);
1198 return hr;
1201 *pcActualCount = 1;
1203 else
1204 *pcActualCount = 0;
1206 LeaveCriticalSection(&This->parent->lock);
1208 return S_OK;
1211 static HRESULT WINAPI TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
1212 IWICBitmapSource **ppIThumbnail)
1214 TRACE("(%p,%p)\n", iface, ppIThumbnail);
1216 if (!ppIThumbnail) return E_INVALIDARG;
1218 *ppIThumbnail = NULL;
1219 return WINCODEC_ERR_CODECNOTHUMBNAIL;
1222 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl = {
1223 TiffFrameDecode_QueryInterface,
1224 TiffFrameDecode_AddRef,
1225 TiffFrameDecode_Release,
1226 TiffFrameDecode_GetSize,
1227 TiffFrameDecode_GetPixelFormat,
1228 TiffFrameDecode_GetResolution,
1229 TiffFrameDecode_CopyPalette,
1230 TiffFrameDecode_CopyPixels,
1231 TiffFrameDecode_GetMetadataQueryReader,
1232 TiffFrameDecode_GetColorContexts,
1233 TiffFrameDecode_GetThumbnail
1236 static HRESULT WINAPI TiffFrameDecode_Block_QueryInterface(IWICMetadataBlockReader *iface,
1237 REFIID iid, void **ppv)
1239 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1240 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
1243 static ULONG WINAPI TiffFrameDecode_Block_AddRef(IWICMetadataBlockReader *iface)
1245 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1246 return IWICBitmapFrameDecode_AddRef(&This->IWICBitmapFrameDecode_iface);
1249 static ULONG WINAPI TiffFrameDecode_Block_Release(IWICMetadataBlockReader *iface)
1251 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1252 return IWICBitmapFrameDecode_Release(&This->IWICBitmapFrameDecode_iface);
1255 static HRESULT WINAPI TiffFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
1256 GUID *guid)
1258 TRACE("(%p,%p)\n", iface, guid);
1260 if (!guid) return E_INVALIDARG;
1262 *guid = GUID_ContainerFormatTiff;
1263 return S_OK;
1266 static HRESULT WINAPI TiffFrameDecode_Block_GetCount(IWICMetadataBlockReader *iface,
1267 UINT *count)
1269 TRACE("%p,%p\n", iface, count);
1271 if (!count) return E_INVALIDARG;
1273 *count = 1;
1274 return S_OK;
1277 static HRESULT create_metadata_reader(TiffFrameDecode *This, IWICMetadataReader **reader)
1279 HRESULT hr;
1280 LARGE_INTEGER dir_offset;
1281 IWICMetadataReader *metadata_reader;
1282 IWICPersistStream *persist;
1284 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
1286 hr = IfdMetadataReader_CreateInstance(&IID_IWICMetadataReader, (void **)&metadata_reader);
1287 if (FAILED(hr)) return hr;
1289 hr = IWICMetadataReader_QueryInterface(metadata_reader, &IID_IWICPersistStream, (void **)&persist);
1290 if (FAILED(hr))
1292 IWICMetadataReader_Release(metadata_reader);
1293 return hr;
1296 EnterCriticalSection(&This->parent->lock);
1298 dir_offset.QuadPart = pTIFFCurrentDirOffset(This->parent->tiff);
1299 hr = IStream_Seek(This->parent->stream, dir_offset, STREAM_SEEK_SET, NULL);
1300 if (SUCCEEDED(hr))
1302 BOOL byte_swapped = pTIFFIsByteSwapped(This->parent->tiff);
1303 #ifdef WORDS_BIGENDIAN
1304 DWORD persist_options = byte_swapped ? WICPersistOptionsLittleEndian : WICPersistOptionsBigEndian;
1305 #else
1306 DWORD persist_options = byte_swapped ? WICPersistOptionsBigEndian : WICPersistOptionsLittleEndian;
1307 #endif
1308 persist_options |= WICPersistOptionsNoCacheStream;
1309 hr = IWICPersistStream_LoadEx(persist, This->parent->stream, NULL, persist_options);
1310 if (FAILED(hr))
1311 ERR("IWICPersistStream_LoadEx error %#x\n", hr);
1314 LeaveCriticalSection(&This->parent->lock);
1316 IWICPersistStream_Release(persist);
1318 if (FAILED(hr))
1320 IWICMetadataReader_Release(metadata_reader);
1321 return hr;
1324 *reader = metadata_reader;
1325 return S_OK;
1328 static HRESULT WINAPI TiffFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
1329 UINT index, IWICMetadataReader **reader)
1331 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1333 TRACE("(%p,%u,%p)\n", iface, index, reader);
1335 if (!reader || index != 0) return E_INVALIDARG;
1337 return create_metadata_reader(This, reader);
1340 static HRESULT WINAPI TiffFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader *iface,
1341 IEnumUnknown **enum_metadata)
1343 FIXME("(%p,%p): stub\n", iface, enum_metadata);
1344 return E_NOTIMPL;
1347 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl =
1349 TiffFrameDecode_Block_QueryInterface,
1350 TiffFrameDecode_Block_AddRef,
1351 TiffFrameDecode_Block_Release,
1352 TiffFrameDecode_Block_GetContainerFormat,
1353 TiffFrameDecode_Block_GetCount,
1354 TiffFrameDecode_Block_GetReaderByIndex,
1355 TiffFrameDecode_Block_GetEnumerator
1358 HRESULT TiffDecoder_CreateInstance(REFIID iid, void** ppv)
1360 HRESULT ret;
1361 TiffDecoder *This;
1363 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1365 *ppv = NULL;
1367 if (!load_libtiff())
1369 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF);
1370 return E_FAIL;
1373 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder));
1374 if (!This) return E_OUTOFMEMORY;
1376 This->IWICBitmapDecoder_iface.lpVtbl = &TiffDecoder_Vtbl;
1377 This->ref = 1;
1378 This->stream = NULL;
1379 InitializeCriticalSection(&This->lock);
1380 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffDecoder.lock");
1381 This->tiff = NULL;
1382 This->initialized = FALSE;
1384 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1385 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1387 return ret;
1390 struct tiff_encode_format {
1391 const WICPixelFormatGUID *guid;
1392 int photometric;
1393 int bps;
1394 int samples;
1395 int bpp;
1396 int extra_sample;
1397 int extra_sample_type;
1398 int reverse_bgr;
1401 static const struct tiff_encode_format formats[] = {
1402 {&GUID_WICPixelFormat24bppBGR, 2, 8, 3, 24, 0, 0, 1},
1403 {&GUID_WICPixelFormat24bppRGB, 2, 8, 3, 24, 0, 0, 0},
1404 {&GUID_WICPixelFormatBlackWhite, 1, 1, 1, 1, 0, 0, 0},
1405 {&GUID_WICPixelFormat4bppGray, 1, 4, 1, 4, 0, 0, 0},
1406 {&GUID_WICPixelFormat8bppGray, 1, 8, 1, 8, 0, 0, 0},
1407 {&GUID_WICPixelFormat32bppBGRA, 2, 8, 4, 32, 1, 2, 1},
1408 {&GUID_WICPixelFormat32bppPBGRA, 2, 8, 4, 32, 1, 1, 1},
1409 {&GUID_WICPixelFormat48bppRGB, 2, 16, 3, 48, 0, 0, 0},
1410 {&GUID_WICPixelFormat64bppRGBA, 2, 16, 4, 64, 1, 2, 0},
1411 {&GUID_WICPixelFormat64bppPRGBA, 2, 16, 4, 64, 1, 1, 0},
1415 typedef struct TiffEncoder {
1416 IWICBitmapEncoder IWICBitmapEncoder_iface;
1417 LONG ref;
1418 IStream *stream;
1419 CRITICAL_SECTION lock; /* Must be held when tiff is used or fields below are set */
1420 TIFF *tiff;
1421 BOOL initialized;
1422 BOOL committed;
1423 ULONG num_frames;
1424 ULONG num_frames_committed;
1425 } TiffEncoder;
1427 static inline TiffEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1429 return CONTAINING_RECORD(iface, TiffEncoder, IWICBitmapEncoder_iface);
1432 typedef struct TiffFrameEncode {
1433 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1434 LONG ref;
1435 TiffEncoder *parent;
1436 /* fields below are protected by parent->lock */
1437 BOOL initialized;
1438 BOOL info_written;
1439 BOOL committed;
1440 const struct tiff_encode_format *format;
1441 UINT width, height;
1442 double xres, yres;
1443 UINT lines_written;
1444 } TiffFrameEncode;
1446 static inline TiffFrameEncode *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1448 return CONTAINING_RECORD(iface, TiffFrameEncode, IWICBitmapFrameEncode_iface);
1451 static HRESULT WINAPI TiffFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1452 void **ppv)
1454 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1455 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1457 if (!ppv) return E_INVALIDARG;
1459 if (IsEqualIID(&IID_IUnknown, iid) ||
1460 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1462 *ppv = &This->IWICBitmapFrameEncode_iface;
1464 else
1466 *ppv = NULL;
1467 return E_NOINTERFACE;
1470 IUnknown_AddRef((IUnknown*)*ppv);
1471 return S_OK;
1474 static ULONG WINAPI TiffFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1476 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1477 ULONG ref = InterlockedIncrement(&This->ref);
1479 TRACE("(%p) refcount=%u\n", iface, ref);
1481 return ref;
1484 static ULONG WINAPI TiffFrameEncode_Release(IWICBitmapFrameEncode *iface)
1486 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1487 ULONG ref = InterlockedDecrement(&This->ref);
1489 TRACE("(%p) refcount=%u\n", iface, ref);
1491 if (ref == 0)
1493 IWICBitmapEncoder_Release(&This->parent->IWICBitmapEncoder_iface);
1494 HeapFree(GetProcessHeap(), 0, This);
1497 return ref;
1500 static HRESULT WINAPI TiffFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1501 IPropertyBag2 *pIEncoderOptions)
1503 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1504 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1506 EnterCriticalSection(&This->parent->lock);
1508 if (This->initialized)
1510 LeaveCriticalSection(&This->parent->lock);
1511 return WINCODEC_ERR_WRONGSTATE;
1514 This->initialized = TRUE;
1516 LeaveCriticalSection(&This->parent->lock);
1518 return S_OK;
1521 static HRESULT WINAPI TiffFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1522 UINT uiWidth, UINT uiHeight)
1524 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1525 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1527 EnterCriticalSection(&This->parent->lock);
1529 if (!This->initialized || This->info_written)
1531 LeaveCriticalSection(&This->parent->lock);
1532 return WINCODEC_ERR_WRONGSTATE;
1535 This->width = uiWidth;
1536 This->height = uiHeight;
1538 LeaveCriticalSection(&This->parent->lock);
1540 return S_OK;
1543 static HRESULT WINAPI TiffFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1544 double dpiX, double dpiY)
1546 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1547 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1549 EnterCriticalSection(&This->parent->lock);
1551 if (!This->initialized || This->info_written)
1553 LeaveCriticalSection(&This->parent->lock);
1554 return WINCODEC_ERR_WRONGSTATE;
1557 This->xres = dpiX;
1558 This->yres = dpiY;
1560 LeaveCriticalSection(&This->parent->lock);
1562 return S_OK;
1565 static HRESULT WINAPI TiffFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1566 WICPixelFormatGUID *pPixelFormat)
1568 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1569 int i;
1571 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1573 EnterCriticalSection(&This->parent->lock);
1575 if (!This->initialized || This->info_written)
1577 LeaveCriticalSection(&This->parent->lock);
1578 return WINCODEC_ERR_WRONGSTATE;
1581 for (i=0; formats[i].guid; i++)
1583 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1584 break;
1587 if (!formats[i].guid) i = 0;
1589 This->format = &formats[i];
1590 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1592 LeaveCriticalSection(&This->parent->lock);
1594 return S_OK;
1597 static HRESULT WINAPI TiffFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1598 UINT cCount, IWICColorContext **ppIColorContext)
1600 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1601 return E_NOTIMPL;
1604 static HRESULT WINAPI TiffFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1605 IWICPalette *pIPalette)
1607 FIXME("(%p,%p): stub\n", iface, pIPalette);
1608 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1611 static HRESULT WINAPI TiffFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1612 IWICBitmapSource *pIThumbnail)
1614 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1615 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1618 static HRESULT WINAPI TiffFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1619 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1621 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1622 BYTE *row_data, *swapped_data = NULL;
1623 UINT i, j, line_size;
1625 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1627 EnterCriticalSection(&This->parent->lock);
1629 if (!This->initialized || !This->width || !This->height || !This->format)
1631 LeaveCriticalSection(&This->parent->lock);
1632 return WINCODEC_ERR_WRONGSTATE;
1635 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1637 LeaveCriticalSection(&This->parent->lock);
1638 return E_INVALIDARG;
1641 line_size = ((This->width * This->format->bpp)+7)/8;
1643 if (This->format->reverse_bgr)
1645 swapped_data = HeapAlloc(GetProcessHeap(), 0, line_size);
1646 if (!swapped_data)
1648 LeaveCriticalSection(&This->parent->lock);
1649 return E_OUTOFMEMORY;
1653 if (!This->info_written)
1655 pTIFFSetField(This->parent->tiff, TIFFTAG_PHOTOMETRIC, (uint16)This->format->photometric);
1656 pTIFFSetField(This->parent->tiff, TIFFTAG_PLANARCONFIG, (uint16)1);
1657 pTIFFSetField(This->parent->tiff, TIFFTAG_BITSPERSAMPLE, (uint16)This->format->bps);
1658 pTIFFSetField(This->parent->tiff, TIFFTAG_SAMPLESPERPIXEL, (uint16)This->format->samples);
1660 if (This->format->extra_sample)
1662 uint16 extra_samples;
1663 extra_samples = This->format->extra_sample_type;
1665 pTIFFSetField(This->parent->tiff, TIFFTAG_EXTRASAMPLES, (uint16)1, &extra_samples);
1668 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGEWIDTH, (uint32)This->width);
1669 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGELENGTH, (uint32)This->height);
1671 if (This->xres != 0.0 && This->yres != 0.0)
1673 pTIFFSetField(This->parent->tiff, TIFFTAG_RESOLUTIONUNIT, (uint16)2); /* Inch */
1674 pTIFFSetField(This->parent->tiff, TIFFTAG_XRESOLUTION, (float)This->xres);
1675 pTIFFSetField(This->parent->tiff, TIFFTAG_YRESOLUTION, (float)This->yres);
1678 This->info_written = TRUE;
1681 for (i=0; i<lineCount; i++)
1683 row_data = pbPixels + i * cbStride;
1685 if (This->format->reverse_bgr && This->format->bps == 8)
1687 memcpy(swapped_data, row_data, line_size);
1688 for (j=0; j<line_size; j += This->format->samples)
1690 BYTE temp;
1691 temp = swapped_data[j];
1692 swapped_data[j] = swapped_data[j+2];
1693 swapped_data[j+2] = temp;
1695 row_data = swapped_data;
1698 pTIFFWriteScanline(This->parent->tiff, (tdata_t)row_data, i+This->lines_written, 0);
1701 This->lines_written += lineCount;
1703 LeaveCriticalSection(&This->parent->lock);
1705 HeapFree(GetProcessHeap(), 0, swapped_data);
1707 return S_OK;
1710 static HRESULT WINAPI TiffFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1711 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1713 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1714 HRESULT hr;
1716 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1718 if (!This->initialized)
1719 return WINCODEC_ERR_WRONGSTATE;
1721 hr = configure_write_source(iface, pIBitmapSource, prc,
1722 This->format ? This->format->guid : NULL, This->width, This->height,
1723 This->xres, This->yres);
1725 if (SUCCEEDED(hr))
1727 hr = write_source(iface, pIBitmapSource, prc,
1728 This->format->guid, This->format->bpp, This->width, This->height);
1731 return hr;
1734 static HRESULT WINAPI TiffFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1736 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1738 TRACE("(%p)\n", iface);
1740 EnterCriticalSection(&This->parent->lock);
1742 if (!This->info_written || This->lines_written != This->height || This->committed)
1744 LeaveCriticalSection(&This->parent->lock);
1745 return WINCODEC_ERR_WRONGSTATE;
1748 /* libtiff will commit the data when creating a new frame or closing the file */
1750 This->committed = TRUE;
1751 This->parent->num_frames_committed++;
1753 LeaveCriticalSection(&This->parent->lock);
1755 return S_OK;
1758 static HRESULT WINAPI TiffFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1759 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1761 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1762 return E_NOTIMPL;
1765 static const IWICBitmapFrameEncodeVtbl TiffFrameEncode_Vtbl = {
1766 TiffFrameEncode_QueryInterface,
1767 TiffFrameEncode_AddRef,
1768 TiffFrameEncode_Release,
1769 TiffFrameEncode_Initialize,
1770 TiffFrameEncode_SetSize,
1771 TiffFrameEncode_SetResolution,
1772 TiffFrameEncode_SetPixelFormat,
1773 TiffFrameEncode_SetColorContexts,
1774 TiffFrameEncode_SetPalette,
1775 TiffFrameEncode_SetThumbnail,
1776 TiffFrameEncode_WritePixels,
1777 TiffFrameEncode_WriteSource,
1778 TiffFrameEncode_Commit,
1779 TiffFrameEncode_GetMetadataQueryWriter
1782 static HRESULT WINAPI TiffEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1783 void **ppv)
1785 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1786 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1788 if (!ppv) return E_INVALIDARG;
1790 if (IsEqualIID(&IID_IUnknown, iid) ||
1791 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1793 *ppv = &This->IWICBitmapEncoder_iface;
1795 else
1797 *ppv = NULL;
1798 return E_NOINTERFACE;
1801 IUnknown_AddRef((IUnknown*)*ppv);
1802 return S_OK;
1805 static ULONG WINAPI TiffEncoder_AddRef(IWICBitmapEncoder *iface)
1807 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1808 ULONG ref = InterlockedIncrement(&This->ref);
1810 TRACE("(%p) refcount=%u\n", iface, ref);
1812 return ref;
1815 static ULONG WINAPI TiffEncoder_Release(IWICBitmapEncoder *iface)
1817 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1818 ULONG ref = InterlockedDecrement(&This->ref);
1820 TRACE("(%p) refcount=%u\n", iface, ref);
1822 if (ref == 0)
1824 if (This->tiff) pTIFFClose(This->tiff);
1825 if (This->stream) IStream_Release(This->stream);
1826 This->lock.DebugInfo->Spare[0] = 0;
1827 DeleteCriticalSection(&This->lock);
1828 HeapFree(GetProcessHeap(), 0, This);
1831 return ref;
1834 static HRESULT WINAPI TiffEncoder_Initialize(IWICBitmapEncoder *iface,
1835 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1837 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1838 TIFF *tiff;
1839 HRESULT hr=S_OK;
1841 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1843 EnterCriticalSection(&This->lock);
1845 if (This->initialized || This->committed)
1847 hr = WINCODEC_ERR_WRONGSTATE;
1848 goto exit;
1851 tiff = tiff_open_stream(pIStream, "w");
1853 if (!tiff)
1855 hr = E_FAIL;
1856 goto exit;
1859 This->tiff = tiff;
1860 This->stream = pIStream;
1861 IStream_AddRef(pIStream);
1862 This->initialized = TRUE;
1864 exit:
1865 LeaveCriticalSection(&This->lock);
1866 return hr;
1869 static HRESULT WINAPI TiffEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1870 GUID *pguidContainerFormat)
1872 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
1873 return S_OK;
1876 static HRESULT WINAPI TiffEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1877 IWICBitmapEncoderInfo **ppIEncoderInfo)
1879 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1880 return E_NOTIMPL;
1883 static HRESULT WINAPI TiffEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1884 UINT cCount, IWICColorContext **ppIColorContext)
1886 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1887 return E_NOTIMPL;
1890 static HRESULT WINAPI TiffEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1892 TRACE("(%p,%p)\n", iface, pIPalette);
1893 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1896 static HRESULT WINAPI TiffEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1898 TRACE("(%p,%p)\n", iface, pIThumbnail);
1899 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1902 static HRESULT WINAPI TiffEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1904 TRACE("(%p,%p)\n", iface, pIPreview);
1905 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1908 static HRESULT WINAPI TiffEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1909 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1911 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1912 TiffFrameEncode *result;
1914 HRESULT hr=S_OK;
1916 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1918 EnterCriticalSection(&This->lock);
1920 if (!This->initialized || This->committed)
1922 hr = WINCODEC_ERR_WRONGSTATE;
1924 else if (This->num_frames != This->num_frames_committed)
1926 FIXME("New frame created before previous frame was committed\n");
1927 hr = E_FAIL;
1930 if (SUCCEEDED(hr))
1932 PROPBAG2 opts[2]= {{0}};
1933 opts[0].pstrName = (LPOLESTR)wszTiffCompressionMethod;
1934 opts[0].vt = VT_UI1;
1935 opts[0].dwType = PROPBAG2_TYPE_DATA;
1937 opts[1].pstrName = (LPOLESTR)wszCompressionQuality;
1938 opts[1].vt = VT_R4;
1939 opts[1].dwType = PROPBAG2_TYPE_DATA;
1941 hr = CreatePropertyBag2(opts, 2, ppIEncoderOptions);
1943 if (SUCCEEDED(hr))
1945 VARIANT v;
1946 VariantInit(&v);
1947 V_VT(&v) = VT_UI1;
1948 V_UNION(&v, bVal) = WICTiffCompressionDontCare;
1949 hr = IPropertyBag2_Write(*ppIEncoderOptions, 1, opts, &v);
1950 VariantClear(&v);
1951 if (FAILED(hr))
1953 IPropertyBag2_Release(*ppIEncoderOptions);
1954 *ppIEncoderOptions = NULL;
1959 if (SUCCEEDED(hr))
1961 result = HeapAlloc(GetProcessHeap(), 0, sizeof(*result));
1963 if (result)
1965 result->IWICBitmapFrameEncode_iface.lpVtbl = &TiffFrameEncode_Vtbl;
1966 result->ref = 1;
1967 result->parent = This;
1968 result->initialized = FALSE;
1969 result->info_written = FALSE;
1970 result->committed = FALSE;
1971 result->format = NULL;
1972 result->width = 0;
1973 result->height = 0;
1974 result->xres = 0.0;
1975 result->yres = 0.0;
1976 result->lines_written = 0;
1978 IWICBitmapEncoder_AddRef(iface);
1979 *ppIFrameEncode = &result->IWICBitmapFrameEncode_iface;
1981 if (This->num_frames != 0)
1982 pTIFFWriteDirectory(This->tiff);
1984 This->num_frames++;
1986 else
1987 hr = E_OUTOFMEMORY;
1989 if (FAILED(hr))
1991 IPropertyBag2_Release(*ppIEncoderOptions);
1992 *ppIEncoderOptions = NULL;
1996 LeaveCriticalSection(&This->lock);
1998 return hr;
2001 static HRESULT WINAPI TiffEncoder_Commit(IWICBitmapEncoder *iface)
2003 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
2005 TRACE("(%p)\n", iface);
2007 EnterCriticalSection(&This->lock);
2009 if (!This->initialized || This->committed)
2011 LeaveCriticalSection(&This->lock);
2012 return WINCODEC_ERR_WRONGSTATE;
2015 pTIFFClose(This->tiff);
2016 IStream_Release(This->stream);
2017 This->stream = NULL;
2018 This->tiff = NULL;
2020 This->committed = TRUE;
2022 LeaveCriticalSection(&This->lock);
2024 return S_OK;
2027 static HRESULT WINAPI TiffEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
2028 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
2030 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
2031 return E_NOTIMPL;
2034 static const IWICBitmapEncoderVtbl TiffEncoder_Vtbl = {
2035 TiffEncoder_QueryInterface,
2036 TiffEncoder_AddRef,
2037 TiffEncoder_Release,
2038 TiffEncoder_Initialize,
2039 TiffEncoder_GetContainerFormat,
2040 TiffEncoder_GetEncoderInfo,
2041 TiffEncoder_SetColorContexts,
2042 TiffEncoder_SetPalette,
2043 TiffEncoder_SetThumbnail,
2044 TiffEncoder_SetPreview,
2045 TiffEncoder_CreateNewFrame,
2046 TiffEncoder_Commit,
2047 TiffEncoder_GetMetadataQueryWriter
2050 HRESULT TiffEncoder_CreateInstance(REFIID iid, void** ppv)
2052 TiffEncoder *This;
2053 HRESULT ret;
2055 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
2057 *ppv = NULL;
2059 if (!load_libtiff())
2061 ERR("Failed writing TIFF because unable to load %s\n",SONAME_LIBTIFF);
2062 return E_FAIL;
2065 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffEncoder));
2066 if (!This) return E_OUTOFMEMORY;
2068 This->IWICBitmapEncoder_iface.lpVtbl = &TiffEncoder_Vtbl;
2069 This->ref = 1;
2070 This->stream = NULL;
2071 InitializeCriticalSection(&This->lock);
2072 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffEncoder.lock");
2073 This->tiff = NULL;
2074 This->initialized = FALSE;
2075 This->num_frames = 0;
2076 This->num_frames_committed = 0;
2077 This->committed = FALSE;
2079 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
2080 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
2082 return ret;
2085 #else /* !SONAME_LIBTIFF */
2087 HRESULT TiffDecoder_CreateInstance(REFIID iid, void** ppv)
2089 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
2090 return E_FAIL;
2093 HRESULT TiffEncoder_CreateInstance(REFIID iid, void** ppv)
2095 ERR("Trying to save TIFF picture, but Wine was compiled without TIFF support.\n");
2096 return E_FAIL;
2099 #endif