msvcp90: Added num_put<char>::put(double) implementation.
[wine] / dlls / windowscodecs / tiffformat.c
1 /*
2  * Copyright 2010 Vincent Povirk for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
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
29
30 #define COBJMACROS
31
32 #include "windef.h"
33 #include "winbase.h"
34 #include "objbase.h"
35 #include "wincodec.h"
36 #include "wincodecsdk.h"
37
38 #include "wincodecs_private.h"
39
40 #include "wine/debug.h"
41 #include "wine/library.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
44
45 #ifdef SONAME_LIBTIFF
46
47 static CRITICAL_SECTION init_tiff_cs;
48 static CRITICAL_SECTION_DEBUG init_tiff_cs_debug =
49 {
50     0, 0, &init_tiff_cs,
51     { &init_tiff_cs_debug.ProcessLocksList,
52       &init_tiff_cs_debug.ProcessLocksList },
53     0, 0, { (DWORD_PTR)(__FILE__ ": init_tiff_cs") }
54 };
55 static CRITICAL_SECTION init_tiff_cs = { &init_tiff_cs_debug, -1, 0, 0, 0, 0 };
56
57 static void *libtiff_handle;
58 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
59 MAKE_FUNCPTR(TIFFClientOpen);
60 MAKE_FUNCPTR(TIFFClose);
61 MAKE_FUNCPTR(TIFFNumberOfDirectories);
62 MAKE_FUNCPTR(TIFFGetField);
63 MAKE_FUNCPTR(TIFFIsByteSwapped);
64 MAKE_FUNCPTR(TIFFReadDirectory);
65 MAKE_FUNCPTR(TIFFReadEncodedStrip);
66 MAKE_FUNCPTR(TIFFReadEncodedTile);
67 MAKE_FUNCPTR(TIFFSetDirectory);
68 MAKE_FUNCPTR(TIFFSetField);
69 MAKE_FUNCPTR(TIFFWriteDirectory);
70 MAKE_FUNCPTR(TIFFWriteScanline);
71 #undef MAKE_FUNCPTR
72
73 static void *load_libtiff(void)
74 {
75     void *result;
76
77     EnterCriticalSection(&init_tiff_cs);
78
79     if (!libtiff_handle &&
80         (libtiff_handle = wine_dlopen(SONAME_LIBTIFF, RTLD_NOW, NULL, 0)) != NULL)
81     {
82
83 #define LOAD_FUNCPTR(f) \
84     if((p##f = wine_dlsym(libtiff_handle, #f, NULL, 0)) == NULL) { \
85         ERR("failed to load symbol %s\n", #f); \
86         libtiff_handle = NULL; \
87         LeaveCriticalSection(&init_tiff_cs); \
88         return NULL; \
89     }
90         LOAD_FUNCPTR(TIFFClientOpen);
91         LOAD_FUNCPTR(TIFFClose);
92         LOAD_FUNCPTR(TIFFNumberOfDirectories);
93         LOAD_FUNCPTR(TIFFGetField);
94         LOAD_FUNCPTR(TIFFIsByteSwapped);
95         LOAD_FUNCPTR(TIFFReadDirectory);
96         LOAD_FUNCPTR(TIFFReadEncodedStrip);
97         LOAD_FUNCPTR(TIFFReadEncodedTile);
98         LOAD_FUNCPTR(TIFFSetDirectory);
99         LOAD_FUNCPTR(TIFFSetField);
100         LOAD_FUNCPTR(TIFFWriteDirectory);
101         LOAD_FUNCPTR(TIFFWriteScanline);
102 #undef LOAD_FUNCPTR
103
104     }
105
106     result = libtiff_handle;
107
108     LeaveCriticalSection(&init_tiff_cs);
109     return result;
110 }
111
112 static tsize_t tiff_stream_read(thandle_t client_data, tdata_t data, tsize_t size)
113 {
114     IStream *stream = (IStream*)client_data;
115     ULONG bytes_read;
116     HRESULT hr;
117
118     hr = IStream_Read(stream, data, size, &bytes_read);
119     if (FAILED(hr)) bytes_read = 0;
120     return bytes_read;
121 }
122
123 static tsize_t tiff_stream_write(thandle_t client_data, tdata_t data, tsize_t size)
124 {
125     IStream *stream = (IStream*)client_data;
126     ULONG bytes_written;
127     HRESULT hr;
128
129     hr = IStream_Write(stream, data, size, &bytes_written);
130     if (FAILED(hr)) bytes_written = 0;
131     return bytes_written;
132 }
133
134 static toff_t tiff_stream_seek(thandle_t client_data, toff_t offset, int whence)
135 {
136     IStream *stream = (IStream*)client_data;
137     LARGE_INTEGER move;
138     DWORD origin;
139     ULARGE_INTEGER new_position;
140     HRESULT hr;
141
142     move.QuadPart = offset;
143     switch (whence)
144     {
145         case SEEK_SET:
146             origin = STREAM_SEEK_SET;
147             break;
148         case SEEK_CUR:
149             origin = STREAM_SEEK_CUR;
150             break;
151         case SEEK_END:
152             origin = STREAM_SEEK_END;
153             break;
154         default:
155             ERR("unknown whence value %i\n", whence);
156             return -1;
157     }
158
159     hr = IStream_Seek(stream, move, origin, &new_position);
160     if (SUCCEEDED(hr)) return new_position.QuadPart;
161     else return -1;
162 }
163
164 static int tiff_stream_close(thandle_t client_data)
165 {
166     /* Caller is responsible for releasing the stream object. */
167     return 0;
168 }
169
170 static toff_t tiff_stream_size(thandle_t client_data)
171 {
172     IStream *stream = (IStream*)client_data;
173     STATSTG statstg;
174     HRESULT hr;
175
176     hr = IStream_Stat(stream, &statstg, STATFLAG_NONAME);
177
178     if (SUCCEEDED(hr)) return statstg.cbSize.QuadPart;
179     else return -1;
180 }
181
182 static int tiff_stream_map(thandle_t client_data, tdata_t *addr, toff_t *size)
183 {
184     /* Cannot mmap streams */
185     return 0;
186 }
187
188 static void tiff_stream_unmap(thandle_t client_data, tdata_t addr, toff_t size)
189 {
190     /* No need to ever do this, since we can't map things. */
191 }
192
193 static TIFF* tiff_open_stream(IStream *stream, const char *mode)
194 {
195     LARGE_INTEGER zero;
196
197     zero.QuadPart = 0;
198     IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
199
200     return pTIFFClientOpen("<IStream object>", mode, stream, tiff_stream_read,
201         tiff_stream_write, tiff_stream_seek, tiff_stream_close,
202         tiff_stream_size, tiff_stream_map, tiff_stream_unmap);
203 }
204
205 typedef struct {
206     IWICBitmapDecoder IWICBitmapDecoder_iface;
207     LONG ref;
208     IStream *stream;
209     CRITICAL_SECTION lock; /* Must be held when tiff is used or initiailzed is set */
210     TIFF *tiff;
211     BOOL initialized;
212 } TiffDecoder;
213
214 typedef struct {
215     const WICPixelFormatGUID *format;
216     int bps;
217     int samples;
218     int bpp;
219     int planar;
220     int indexed;
221     int reverse_bgr;
222     int invert_grayscale;
223     UINT width, height;
224     UINT tile_width, tile_height;
225     UINT tile_stride;
226     UINT tile_size;
227     int tiled;
228     UINT tiles_across;
229     UINT resolution_unit;
230     float xres, yres;
231 } tiff_decode_info;
232
233 typedef struct {
234     IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
235     IWICMetadataBlockReader IWICMetadataBlockReader_iface;
236     LONG ref;
237     TiffDecoder *parent;
238     UINT index;
239     tiff_decode_info decode_info;
240     INT cached_tile_x, cached_tile_y;
241     BYTE *cached_tile;
242 } TiffFrameDecode;
243
244 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl;
245 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl;
246
247 static inline TiffDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
248 {
249     return CONTAINING_RECORD(iface, TiffDecoder, IWICBitmapDecoder_iface);
250 }
251
252 static inline TiffFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
253 {
254     return CONTAINING_RECORD(iface, TiffFrameDecode, IWICBitmapFrameDecode_iface);
255 }
256
257 static inline TiffFrameDecode *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
258 {
259     return CONTAINING_RECORD(iface, TiffFrameDecode, IWICMetadataBlockReader_iface);
260 }
261
262 static HRESULT tiff_get_decode_info(TIFF *tiff, tiff_decode_info *decode_info)
263 {
264     uint16 photometric, bps, samples, planar;
265     uint16 extra_sample_count, extra_sample, *extra_samples;
266     int ret;
267
268     decode_info->indexed = 0;
269     decode_info->reverse_bgr = 0;
270     decode_info->invert_grayscale = 0;
271     decode_info->tiled = 0;
272
273     ret = pTIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
274     if (!ret)
275     {
276         WARN("missing PhotometricInterpretation tag\n");
277         return E_FAIL;
278     }
279
280     ret = pTIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bps);
281     if (!ret) bps = 1;
282     decode_info->bps = bps;
283
284     ret = pTIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samples);
285     if (!ret) samples = 1;
286     decode_info->samples = samples;
287
288     if (samples == 1)
289         planar = 1;
290     else
291     {
292         ret = pTIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planar);
293         if (!ret) planar = 1;
294         if (planar != 1)
295         {
296             FIXME("unhandled planar configuration %u\n", planar);
297             return E_FAIL;
298         }
299     }
300     decode_info->planar = planar;
301
302     switch(photometric)
303     {
304     case 0: /* WhiteIsZero */
305         decode_info->invert_grayscale = 1;
306         /* fall through */
307     case 1: /* BlackIsZero */
308         if (samples != 1)
309         {
310             FIXME("unhandled grayscale sample count %u\n", samples);
311             return E_FAIL;
312         }
313
314         decode_info->bpp = bps;
315         switch (bps)
316         {
317         case 1:
318             decode_info->format = &GUID_WICPixelFormatBlackWhite;
319             break;
320         case 4:
321             decode_info->format = &GUID_WICPixelFormat4bppGray;
322             break;
323         case 8:
324             decode_info->format = &GUID_WICPixelFormat8bppGray;
325             break;
326         default:
327             FIXME("unhandled greyscale bit count %u\n", bps);
328             return E_FAIL;
329         }
330         break;
331     case 2: /* RGB */
332         decode_info->bpp = bps * samples;
333
334         if (samples == 4)
335         {
336             ret = pTIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extra_sample_count, &extra_samples);
337             if (!ret)
338             {
339                 extra_sample_count = 1;
340                 extra_sample = 0;
341                 extra_samples = &extra_sample;
342             }
343         }
344         else if (samples != 3)
345         {
346             FIXME("unhandled RGB sample count %u\n", samples);
347             return E_FAIL;
348         }
349
350         switch(bps)
351         {
352         case 8:
353             decode_info->reverse_bgr = 1;
354             if (samples == 3)
355                 decode_info->format = &GUID_WICPixelFormat24bppBGR;
356             else
357                 switch(extra_samples[0])
358                 {
359                 case 1: /* Associated (pre-multiplied) alpha data */
360                     decode_info->format = &GUID_WICPixelFormat32bppPBGRA;
361                     break;
362                 case 0: /* Unspecified data */
363                 case 2: /* Unassociated alpha data */
364                     decode_info->format = &GUID_WICPixelFormat32bppBGRA;
365                     break;
366                 default:
367                     FIXME("unhandled extra sample type %i\n", extra_samples[0]);
368                     return E_FAIL;
369                 }
370             break;
371         case 16:
372             if (samples == 3)
373                 decode_info->format = &GUID_WICPixelFormat48bppRGB;
374             else
375                 switch(extra_samples[0])
376                 {
377                 case 1: /* Associated (pre-multiplied) alpha data */
378                     decode_info->format = &GUID_WICPixelFormat64bppPRGBA;
379                     break;
380                 case 0: /* Unspecified data */
381                 case 2: /* Unassociated alpha data */
382                     decode_info->format = &GUID_WICPixelFormat64bppRGBA;
383                     break;
384                 default:
385                     FIXME("unhandled extra sample type %i\n", extra_samples[0]);
386                     return E_FAIL;
387                 }
388             break;
389         default:
390             FIXME("unhandled RGB bit count %u\n", bps);
391             return E_FAIL;
392         }
393         break;
394     case 3: /* RGB Palette */
395         if (samples != 1)
396         {
397             FIXME("unhandled indexed sample count %u\n", samples);
398             return E_FAIL;
399         }
400
401         decode_info->indexed = 1;
402         decode_info->bpp = bps;
403         switch (bps)
404         {
405         case 4:
406             decode_info->format = &GUID_WICPixelFormat4bppIndexed;
407             break;
408         case 8:
409             decode_info->format = &GUID_WICPixelFormat8bppIndexed;
410             break;
411         default:
412             FIXME("unhandled indexed bit count %u\n", bps);
413             return E_FAIL;
414         }
415         break;
416     case 4: /* Transparency mask */
417     case 5: /* CMYK */
418     case 6: /* YCbCr */
419     case 8: /* CIELab */
420     default:
421         FIXME("unhandled PhotometricInterpretation %u\n", photometric);
422         return E_FAIL;
423     }
424
425     ret = pTIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &decode_info->width);
426     if (!ret)
427     {
428         WARN("missing image width\n");
429         return E_FAIL;
430     }
431
432     ret = pTIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &decode_info->height);
433     if (!ret)
434     {
435         WARN("missing image length\n");
436         return E_FAIL;
437     }
438
439     if ((ret = pTIFFGetField(tiff, TIFFTAG_TILEWIDTH, &decode_info->tile_width)))
440     {
441         decode_info->tiled = 1;
442
443         ret = pTIFFGetField(tiff, TIFFTAG_TILELENGTH, &decode_info->tile_height);
444         if (!ret)
445         {
446             WARN("missing tile height\n");
447             return E_FAIL;
448         }
449
450         decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
451         decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
452         decode_info->tiles_across = (decode_info->width + decode_info->tile_width - 1) / decode_info->tile_width;
453     }
454     else if ((ret = pTIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &decode_info->tile_height)))
455     {
456         if (decode_info->tile_height > decode_info->height)
457             decode_info->tile_height = decode_info->height;
458         decode_info->tile_width = decode_info->width;
459         decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
460         decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
461     }
462     else
463     {
464         /* Some broken TIFF files have a single strip and lack the RowsPerStrip tag */
465         decode_info->tile_height = decode_info->height;
466         decode_info->tile_width = decode_info->width;
467         decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
468         decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
469     }
470
471     decode_info->resolution_unit = 0;
472     pTIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &decode_info->resolution_unit);
473     if (decode_info->resolution_unit != 0)
474     {
475         ret = pTIFFGetField(tiff, TIFFTAG_XRESOLUTION, &decode_info->xres);
476         if (!ret)
477         {
478             WARN("missing X resolution\n");
479             decode_info->resolution_unit = 0;
480         }
481
482         ret = pTIFFGetField(tiff, TIFFTAG_YRESOLUTION, &decode_info->yres);
483         if (!ret)
484         {
485             WARN("missing Y resolution\n");
486             decode_info->resolution_unit = 0;
487         }
488     }
489
490     return S_OK;
491 }
492
493 static HRESULT WINAPI TiffDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
494     void **ppv)
495 {
496     TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
497     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
498
499     if (!ppv) return E_INVALIDARG;
500
501     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
502     {
503         *ppv = This;
504     }
505     else
506     {
507         *ppv = NULL;
508         return E_NOINTERFACE;
509     }
510
511     IUnknown_AddRef((IUnknown*)*ppv);
512     return S_OK;
513 }
514
515 static ULONG WINAPI TiffDecoder_AddRef(IWICBitmapDecoder *iface)
516 {
517     TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
518     ULONG ref = InterlockedIncrement(&This->ref);
519
520     TRACE("(%p) refcount=%u\n", iface, ref);
521
522     return ref;
523 }
524
525 static ULONG WINAPI TiffDecoder_Release(IWICBitmapDecoder *iface)
526 {
527     TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
528     ULONG ref = InterlockedDecrement(&This->ref);
529
530     TRACE("(%p) refcount=%u\n", iface, ref);
531
532     if (ref == 0)
533     {
534         if (This->tiff) pTIFFClose(This->tiff);
535         if (This->stream) IStream_Release(This->stream);
536         This->lock.DebugInfo->Spare[0] = 0;
537         DeleteCriticalSection(&This->lock);
538         HeapFree(GetProcessHeap(), 0, This);
539     }
540
541     return ref;
542 }
543
544 static HRESULT WINAPI TiffDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
545     DWORD *pdwCapability)
546 {
547     FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
548     return E_NOTIMPL;
549 }
550
551 static HRESULT WINAPI TiffDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
552     WICDecodeOptions cacheOptions)
553 {
554     TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
555     TIFF *tiff;
556     HRESULT hr=S_OK;
557
558     TRACE("(%p,%p,%x): stub\n", iface, pIStream, cacheOptions);
559
560     EnterCriticalSection(&This->lock);
561
562     if (This->initialized)
563     {
564         hr = WINCODEC_ERR_WRONGSTATE;
565         goto exit;
566     }
567
568     tiff = tiff_open_stream(pIStream, "r");
569
570     if (!tiff)
571     {
572         hr = E_FAIL;
573         goto exit;
574     }
575
576     This->tiff = tiff;
577     This->stream = pIStream;
578     IStream_AddRef(pIStream);
579     This->initialized = TRUE;
580
581 exit:
582     LeaveCriticalSection(&This->lock);
583     return hr;
584 }
585
586 static HRESULT WINAPI TiffDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
587     GUID *pguidContainerFormat)
588 {
589     memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
590     return S_OK;
591 }
592
593 static HRESULT WINAPI TiffDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
594     IWICBitmapDecoderInfo **ppIDecoderInfo)
595 {
596     HRESULT hr;
597     IWICComponentInfo *compinfo;
598
599     TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
600
601     hr = CreateComponentInfo(&CLSID_WICTiffDecoder, &compinfo);
602     if (FAILED(hr)) return hr;
603
604     hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
605         (void**)ppIDecoderInfo);
606
607     IWICComponentInfo_Release(compinfo);
608
609     return hr;
610 }
611
612 static HRESULT WINAPI TiffDecoder_CopyPalette(IWICBitmapDecoder *iface,
613     IWICPalette *pIPalette)
614 {
615     FIXME("(%p,%p): stub\n", iface, pIPalette);
616     return E_NOTIMPL;
617 }
618
619 static HRESULT WINAPI TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
620     IWICMetadataQueryReader **ppIMetadataQueryReader)
621 {
622     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
623     return E_NOTIMPL;
624 }
625
626 static HRESULT WINAPI TiffDecoder_GetPreview(IWICBitmapDecoder *iface,
627     IWICBitmapSource **ppIBitmapSource)
628 {
629     FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
630     return E_NOTIMPL;
631 }
632
633 static HRESULT WINAPI TiffDecoder_GetColorContexts(IWICBitmapDecoder *iface,
634     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
635 {
636     FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
637     return E_NOTIMPL;
638 }
639
640 static HRESULT WINAPI TiffDecoder_GetThumbnail(IWICBitmapDecoder *iface,
641     IWICBitmapSource **ppIThumbnail)
642 {
643     TRACE("(%p,%p)\n", iface, ppIThumbnail);
644     return WINCODEC_ERR_CODECNOTHUMBNAIL;
645 }
646
647 static HRESULT WINAPI TiffDecoder_GetFrameCount(IWICBitmapDecoder *iface,
648     UINT *pCount)
649 {
650     TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
651
652     if (!This->tiff)
653     {
654         WARN("(%p) <-- WINCODEC_ERR_WRONGSTATE\n", iface);
655         return WINCODEC_ERR_WRONGSTATE;
656     }
657
658     EnterCriticalSection(&This->lock);
659     *pCount = pTIFFNumberOfDirectories(This->tiff);
660     LeaveCriticalSection(&This->lock);
661
662     TRACE("(%p) <-- %i\n", iface, *pCount);
663
664     return S_OK;
665 }
666
667 static HRESULT WINAPI TiffDecoder_GetFrame(IWICBitmapDecoder *iface,
668     UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
669 {
670     TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
671     TiffFrameDecode *result;
672     int res;
673     tiff_decode_info decode_info;
674     HRESULT hr;
675
676     TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
677
678     if (!This->tiff)
679         return WINCODEC_ERR_WRONGSTATE;
680
681     EnterCriticalSection(&This->lock);
682     res = pTIFFSetDirectory(This->tiff, index);
683     if (!res) hr = E_INVALIDARG;
684     else hr = tiff_get_decode_info(This->tiff, &decode_info);
685     LeaveCriticalSection(&This->lock);
686
687     if (SUCCEEDED(hr))
688     {
689         result = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode));
690
691         if (result)
692         {
693             result->IWICBitmapFrameDecode_iface.lpVtbl = &TiffFrameDecode_Vtbl;
694             result->IWICMetadataBlockReader_iface.lpVtbl = &TiffFrameDecode_BlockVtbl;
695             result->ref = 1;
696             result->parent = This;
697             result->index = index;
698             result->decode_info = decode_info;
699             result->cached_tile_x = -1;
700             result->cached_tile = HeapAlloc(GetProcessHeap(), 0, decode_info.tile_size);
701
702             if (result->cached_tile)
703                 *ppIBitmapFrame = (IWICBitmapFrameDecode*)result;
704             else
705             {
706                 hr = E_OUTOFMEMORY;
707                 HeapFree(GetProcessHeap(), 0, result);
708             }
709         }
710         else hr = E_OUTOFMEMORY;
711     }
712
713     if (FAILED(hr)) *ppIBitmapFrame = NULL;
714
715     return hr;
716 }
717
718 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl = {
719     TiffDecoder_QueryInterface,
720     TiffDecoder_AddRef,
721     TiffDecoder_Release,
722     TiffDecoder_QueryCapability,
723     TiffDecoder_Initialize,
724     TiffDecoder_GetContainerFormat,
725     TiffDecoder_GetDecoderInfo,
726     TiffDecoder_CopyPalette,
727     TiffDecoder_GetMetadataQueryReader,
728     TiffDecoder_GetPreview,
729     TiffDecoder_GetColorContexts,
730     TiffDecoder_GetThumbnail,
731     TiffDecoder_GetFrameCount,
732     TiffDecoder_GetFrame
733 };
734
735 static HRESULT WINAPI TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
736     void **ppv)
737 {
738     TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
739     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
740
741     if (!ppv) return E_INVALIDARG;
742
743     if (IsEqualIID(&IID_IUnknown, iid) ||
744         IsEqualIID(&IID_IWICBitmapSource, iid) ||
745         IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
746     {
747         *ppv = This;
748     }
749     else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
750     {
751         *ppv = &This->IWICMetadataBlockReader_iface;
752     }
753     else
754     {
755         *ppv = NULL;
756         return E_NOINTERFACE;
757     }
758
759     IUnknown_AddRef((IUnknown*)*ppv);
760     return S_OK;
761 }
762
763 static ULONG WINAPI TiffFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
764 {
765     TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
766     ULONG ref = InterlockedIncrement(&This->ref);
767
768     TRACE("(%p) refcount=%u\n", iface, ref);
769
770     return ref;
771 }
772
773 static ULONG WINAPI TiffFrameDecode_Release(IWICBitmapFrameDecode *iface)
774 {
775     TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
776     ULONG ref = InterlockedDecrement(&This->ref);
777
778     TRACE("(%p) refcount=%u\n", iface, ref);
779
780     if (ref == 0)
781     {
782         HeapFree(GetProcessHeap(), 0, This->cached_tile);
783         HeapFree(GetProcessHeap(), 0, This);
784     }
785
786     return ref;
787 }
788
789 static HRESULT WINAPI TiffFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
790     UINT *puiWidth, UINT *puiHeight)
791 {
792     TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
793
794     *puiWidth = This->decode_info.width;
795     *puiHeight = This->decode_info.height;
796
797     TRACE("(%p) <-- %ux%u\n", iface, *puiWidth, *puiHeight);
798
799     return S_OK;
800 }
801
802 static HRESULT WINAPI TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
803     WICPixelFormatGUID *pPixelFormat)
804 {
805     TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
806
807     memcpy(pPixelFormat, This->decode_info.format, sizeof(GUID));
808
809     TRACE("(%p) <-- %s\n", This, debugstr_guid(This->decode_info.format));
810
811     return S_OK;
812 }
813
814 static HRESULT WINAPI TiffFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
815     double *pDpiX, double *pDpiY)
816 {
817     TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
818
819     switch (This->decode_info.resolution_unit)
820     {
821     default:
822         FIXME("unknown resolution unit %i\n", This->decode_info.resolution_unit);
823         /* fall through */
824     case 0: /* Not set */
825         *pDpiX = *pDpiY = 96.0;
826         break;
827     case 1: /* Relative measurements */
828         *pDpiX = 96.0;
829         *pDpiY = 96.0 * This->decode_info.yres / This->decode_info.xres;
830         break;
831     case 2: /* Inch */
832         *pDpiX = This->decode_info.xres;
833         *pDpiY = This->decode_info.yres;
834         break;
835     case 3: /* Centimeter */
836         *pDpiX = This->decode_info.xres / 2.54;
837         *pDpiY = This->decode_info.yres / 2.54;
838         break;
839     }
840
841     TRACE("(%p) <-- %f,%f unit=%i\n", iface, *pDpiX, *pDpiY, This->decode_info.resolution_unit);
842
843     return S_OK;
844 }
845
846 static HRESULT WINAPI TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
847     IWICPalette *pIPalette)
848 {
849     TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
850     uint16 *red, *green, *blue;
851     WICColor colors[256];
852     int color_count, ret, i;
853
854     TRACE("(%p,%p)\n", iface, pIPalette);
855
856     color_count = 1<<This->decode_info.bps;
857
858     EnterCriticalSection(&This->parent->lock);
859     ret = pTIFFGetField(This->parent->tiff, TIFFTAG_COLORMAP, &red, &green, &blue);
860     LeaveCriticalSection(&This->parent->lock);
861
862     if (!ret)
863     {
864         WARN("Couldn't read color map\n");
865         return E_FAIL;
866     }
867
868     for (i=0; i<color_count; i++)
869     {
870         colors[i] = 0xff000000 |
871             ((red[i]<<8) & 0xff0000) |
872             (green[i] & 0xff00) |
873             ((blue[i]>>8) & 0xff);
874     }
875
876     return IWICPalette_InitializeCustom(pIPalette, colors, color_count);
877 }
878
879 static HRESULT TiffFrameDecode_ReadTile(TiffFrameDecode *This, UINT tile_x, UINT tile_y)
880 {
881     HRESULT hr=S_OK;
882     tsize_t ret;
883     int swap_bytes;
884
885     swap_bytes = pTIFFIsByteSwapped(This->parent->tiff);
886
887     ret = pTIFFSetDirectory(This->parent->tiff, This->index);
888
889     if (ret == -1)
890         hr = E_FAIL;
891
892     if (hr == S_OK)
893     {
894         if (This->decode_info.tiled)
895         {
896             ret = pTIFFReadEncodedTile(This->parent->tiff, tile_x + tile_y * This->decode_info.tiles_across, This->cached_tile, This->decode_info.tile_size);
897         }
898         else
899         {
900             ret = pTIFFReadEncodedStrip(This->parent->tiff, tile_y, This->cached_tile, This->decode_info.tile_size);
901         }
902
903         if (ret == -1)
904             hr = E_FAIL;
905     }
906
907     if (hr == S_OK && This->decode_info.reverse_bgr)
908     {
909         if (This->decode_info.bps == 8)
910         {
911             UINT sample_count = This->decode_info.samples;
912
913             reverse_bgr8(sample_count, This->cached_tile, This->decode_info.tile_width,
914                 This->decode_info.tile_height, This->decode_info.tile_width * sample_count);
915         }
916     }
917
918     if (hr == S_OK && swap_bytes && This->decode_info.bps > 8)
919     {
920         UINT row, i, samples_per_row;
921         BYTE *sample, temp;
922
923         samples_per_row = This->decode_info.tile_width * This->decode_info.samples;
924
925         switch(This->decode_info.bps)
926         {
927         case 16:
928             for (row=0; row<This->decode_info.tile_height; row++)
929             {
930                 sample = This->cached_tile + row * This->decode_info.tile_stride;
931                 for (i=0; i<samples_per_row; i++)
932                 {
933                     temp = sample[1];
934                     sample[1] = sample[0];
935                     sample[0] = temp;
936                     sample += 2;
937                 }
938             }
939             break;
940         default:
941             ERR("unhandled bps for byte swap %u\n", This->decode_info.bps);
942             return E_FAIL;
943         }
944     }
945
946     if (hr == S_OK && This->decode_info.invert_grayscale)
947     {
948         BYTE *byte, *end;
949
950         if (This->decode_info.samples != 1)
951         {
952             ERR("cannot invert grayscale image with %u samples\n", This->decode_info.samples);
953             return E_FAIL;
954         }
955
956         end = This->cached_tile+This->decode_info.tile_size;
957
958         for (byte = This->cached_tile; byte != end; byte++)
959             *byte = ~(*byte);
960     }
961
962     if (hr == S_OK)
963     {
964         This->cached_tile_x = tile_x;
965         This->cached_tile_y = tile_y;
966     }
967
968     return hr;
969 }
970
971 static HRESULT WINAPI TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
972     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
973 {
974     TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
975     UINT min_tile_x, max_tile_x, min_tile_y, max_tile_y;
976     UINT tile_x, tile_y;
977     WICRect rc;
978     HRESULT hr=S_OK;
979     BYTE *dst_tilepos;
980     UINT bytesperrow;
981     WICRect rect;
982
983     TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
984
985     if (!prc)
986     {
987         rect.X = 0;
988         rect.Y = 0;
989         rect.Width = This->decode_info.width;
990         rect.Height = This->decode_info.height;
991         prc = &rect;
992     }
993     else
994     {
995         if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->decode_info.width ||
996             prc->Y+prc->Height > This->decode_info.height)
997             return E_INVALIDARG;
998     }
999
1000     bytesperrow = ((This->decode_info.bpp * prc->Width)+7)/8;
1001
1002     if (cbStride < bytesperrow)
1003         return E_INVALIDARG;
1004
1005     if ((cbStride * prc->Height) > cbBufferSize)
1006         return E_INVALIDARG;
1007
1008     min_tile_x = prc->X / This->decode_info.tile_width;
1009     min_tile_y = prc->Y / This->decode_info.tile_height;
1010     max_tile_x = (prc->X+prc->Width-1) / This->decode_info.tile_width;
1011     max_tile_y = (prc->Y+prc->Height-1) / This->decode_info.tile_height;
1012
1013     EnterCriticalSection(&This->parent->lock);
1014
1015     for (tile_x=min_tile_x; tile_x <= max_tile_x; tile_x++)
1016     {
1017         for (tile_y=min_tile_y; tile_y <= max_tile_y; tile_y++)
1018         {
1019             if (tile_x != This->cached_tile_x || tile_y != This->cached_tile_y)
1020             {
1021                 hr = TiffFrameDecode_ReadTile(This, tile_x, tile_y);
1022             }
1023
1024             if (SUCCEEDED(hr))
1025             {
1026                 if (prc->X < tile_x * This->decode_info.tile_width)
1027                     rc.X = 0;
1028                 else
1029                     rc.X = prc->X - tile_x * This->decode_info.tile_width;
1030
1031                 if (prc->Y < tile_y * This->decode_info.tile_height)
1032                     rc.Y = 0;
1033                 else
1034                     rc.Y = prc->Y - tile_y * This->decode_info.tile_height;
1035
1036                 if (prc->X+prc->Width > (tile_x+1) * This->decode_info.tile_width)
1037                     rc.Width = This->decode_info.tile_width - rc.X;
1038                 else if (prc->X < tile_x * This->decode_info.tile_width)
1039                     rc.Width = prc->Width + prc->X - tile_x * This->decode_info.tile_width;
1040                 else
1041                     rc.Width = prc->Width;
1042
1043                 if (prc->Y+prc->Height > (tile_y+1) * This->decode_info.tile_height)
1044                     rc.Height = This->decode_info.tile_height - rc.Y;
1045                 else if (prc->Y < tile_y * This->decode_info.tile_height)
1046                     rc.Height = prc->Height + prc->Y - tile_y * This->decode_info.tile_height;
1047                 else
1048                     rc.Height = prc->Height;
1049
1050                 dst_tilepos = pbBuffer + (cbStride * ((rc.Y + tile_y * This->decode_info.tile_height) - prc->Y)) +
1051                     ((This->decode_info.bpp * ((rc.X + tile_x * This->decode_info.tile_width) - prc->X) + 7) / 8);
1052
1053                 hr = copy_pixels(This->decode_info.bpp, This->cached_tile,
1054                     This->decode_info.tile_width, This->decode_info.tile_height, This->decode_info.tile_stride,
1055                     &rc, cbStride, cbBufferSize, dst_tilepos);
1056             }
1057
1058             if (FAILED(hr))
1059             {
1060                 LeaveCriticalSection(&This->parent->lock);
1061                 TRACE("<-- 0x%x\n", hr);
1062                 return hr;
1063             }
1064         }
1065     }
1066
1067     LeaveCriticalSection(&This->parent->lock);
1068
1069     return S_OK;
1070 }
1071
1072 static HRESULT WINAPI TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
1073     IWICMetadataQueryReader **ppIMetadataQueryReader)
1074 {
1075     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
1076     return E_NOTIMPL;
1077 }
1078
1079 static HRESULT WINAPI TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
1080     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1081 {
1082     FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
1083     return E_NOTIMPL;
1084 }
1085
1086 static HRESULT WINAPI TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
1087     IWICBitmapSource **ppIThumbnail)
1088 {
1089     FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
1090     return E_NOTIMPL;
1091 }
1092
1093 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl = {
1094     TiffFrameDecode_QueryInterface,
1095     TiffFrameDecode_AddRef,
1096     TiffFrameDecode_Release,
1097     TiffFrameDecode_GetSize,
1098     TiffFrameDecode_GetPixelFormat,
1099     TiffFrameDecode_GetResolution,
1100     TiffFrameDecode_CopyPalette,
1101     TiffFrameDecode_CopyPixels,
1102     TiffFrameDecode_GetMetadataQueryReader,
1103     TiffFrameDecode_GetColorContexts,
1104     TiffFrameDecode_GetThumbnail
1105 };
1106
1107 static HRESULT WINAPI TiffFrameDecode_Block_QueryInterface(IWICMetadataBlockReader *iface,
1108     REFIID iid, void **ppv)
1109 {
1110     TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1111     return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
1112 }
1113
1114 static ULONG WINAPI TiffFrameDecode_Block_AddRef(IWICMetadataBlockReader *iface)
1115 {
1116     TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1117     return IWICBitmapFrameDecode_AddRef(&This->IWICBitmapFrameDecode_iface);
1118 }
1119
1120 static ULONG WINAPI TiffFrameDecode_Block_Release(IWICMetadataBlockReader *iface)
1121 {
1122     TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1123     return IWICBitmapFrameDecode_Release(&This->IWICBitmapFrameDecode_iface);
1124 }
1125
1126 static HRESULT WINAPI TiffFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
1127     GUID *guid)
1128 {
1129     FIXME("(%p,%p): stub\n", iface, guid);
1130     return E_NOTIMPL;
1131 }
1132
1133 static HRESULT WINAPI TiffFrameDecode_Block_GetCount(IWICMetadataBlockReader *iface,
1134     UINT *count)
1135 {
1136     FIXME("(%p,%p): stub\n", iface, count);
1137     return E_NOTIMPL;
1138 }
1139
1140 static HRESULT WINAPI TiffFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
1141     UINT index, IWICMetadataReader **reader)
1142 {
1143     FIXME("(%p,%u,%p): stub\n", iface, index, reader);
1144     return E_NOTIMPL;
1145 }
1146
1147 static HRESULT WINAPI TiffFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader *iface,
1148     IEnumUnknown **enum_metadata)
1149 {
1150     FIXME("(%p,%p): stub\n", iface, enum_metadata);
1151     return E_NOTIMPL;
1152 }
1153
1154 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl =
1155 {
1156     TiffFrameDecode_Block_QueryInterface,
1157     TiffFrameDecode_Block_AddRef,
1158     TiffFrameDecode_Block_Release,
1159     TiffFrameDecode_Block_GetContainerFormat,
1160     TiffFrameDecode_Block_GetCount,
1161     TiffFrameDecode_Block_GetReaderByIndex,
1162     TiffFrameDecode_Block_GetEnumerator
1163 };
1164
1165 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1166 {
1167     HRESULT ret;
1168     TiffDecoder *This;
1169
1170     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1171
1172     *ppv = NULL;
1173
1174     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1175
1176     if (!load_libtiff())
1177     {
1178         ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF);
1179         return E_FAIL;
1180     }
1181
1182     This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder));
1183     if (!This) return E_OUTOFMEMORY;
1184
1185     This->IWICBitmapDecoder_iface.lpVtbl = &TiffDecoder_Vtbl;
1186     This->ref = 1;
1187     This->stream = NULL;
1188     InitializeCriticalSection(&This->lock);
1189     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffDecoder.lock");
1190     This->tiff = NULL;
1191     This->initialized = FALSE;
1192
1193     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1194     IUnknown_Release((IUnknown*)This);
1195
1196     return ret;
1197 }
1198
1199 struct tiff_encode_format {
1200     const WICPixelFormatGUID *guid;
1201     int photometric;
1202     int bps;
1203     int samples;
1204     int bpp;
1205     int extra_sample;
1206     int extra_sample_type;
1207     int reverse_bgr;
1208 };
1209
1210 static const struct tiff_encode_format formats[] = {
1211     {&GUID_WICPixelFormat24bppBGR, 2, 8, 3, 24, 0, 0, 1},
1212     {&GUID_WICPixelFormatBlackWhite, 1, 1, 1, 1, 0, 0, 0},
1213     {&GUID_WICPixelFormat4bppGray, 1, 4, 1, 4, 0, 0, 0},
1214     {&GUID_WICPixelFormat8bppGray, 1, 8, 1, 8, 0, 0, 0},
1215     {&GUID_WICPixelFormat32bppBGRA, 2, 8, 4, 32, 1, 2, 1},
1216     {&GUID_WICPixelFormat32bppPBGRA, 2, 8, 4, 32, 1, 1, 1},
1217     {&GUID_WICPixelFormat48bppRGB, 2, 16, 3, 48, 0, 0, 0},
1218     {&GUID_WICPixelFormat64bppRGBA, 2, 16, 4, 64, 1, 2, 0},
1219     {&GUID_WICPixelFormat64bppPRGBA, 2, 16, 4, 64, 1, 1, 0},
1220     {0}
1221 };
1222
1223 typedef struct TiffEncoder {
1224     IWICBitmapEncoder IWICBitmapEncoder_iface;
1225     LONG ref;
1226     IStream *stream;
1227     CRITICAL_SECTION lock; /* Must be held when tiff is used or fields below are set */
1228     TIFF *tiff;
1229     BOOL initialized;
1230     BOOL committed;
1231     ULONG num_frames;
1232     ULONG num_frames_committed;
1233 } TiffEncoder;
1234
1235 static inline TiffEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1236 {
1237     return CONTAINING_RECORD(iface, TiffEncoder, IWICBitmapEncoder_iface);
1238 }
1239
1240 typedef struct TiffFrameEncode {
1241     IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1242     LONG ref;
1243     TiffEncoder *parent;
1244     /* fields below are protected by parent->lock */
1245     BOOL initialized;
1246     BOOL info_written;
1247     BOOL committed;
1248     const struct tiff_encode_format *format;
1249     UINT width, height;
1250     double xres, yres;
1251     UINT lines_written;
1252 } TiffFrameEncode;
1253
1254 static inline TiffFrameEncode *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1255 {
1256     return CONTAINING_RECORD(iface, TiffFrameEncode, IWICBitmapFrameEncode_iface);
1257 }
1258
1259 static HRESULT WINAPI TiffFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1260     void **ppv)
1261 {
1262     TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1263     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1264
1265     if (!ppv) return E_INVALIDARG;
1266
1267     if (IsEqualIID(&IID_IUnknown, iid) ||
1268         IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1269     {
1270         *ppv = &This->IWICBitmapFrameEncode_iface;
1271     }
1272     else
1273     {
1274         *ppv = NULL;
1275         return E_NOINTERFACE;
1276     }
1277
1278     IUnknown_AddRef((IUnknown*)*ppv);
1279     return S_OK;
1280 }
1281
1282 static ULONG WINAPI TiffFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1283 {
1284     TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1285     ULONG ref = InterlockedIncrement(&This->ref);
1286
1287     TRACE("(%p) refcount=%u\n", iface, ref);
1288
1289     return ref;
1290 }
1291
1292 static ULONG WINAPI TiffFrameEncode_Release(IWICBitmapFrameEncode *iface)
1293 {
1294     TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1295     ULONG ref = InterlockedDecrement(&This->ref);
1296
1297     TRACE("(%p) refcount=%u\n", iface, ref);
1298
1299     if (ref == 0)
1300     {
1301         IWICBitmapEncoder_Release(&This->parent->IWICBitmapEncoder_iface);
1302         HeapFree(GetProcessHeap(), 0, This);
1303     }
1304
1305     return ref;
1306 }
1307
1308 static HRESULT WINAPI TiffFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1309     IPropertyBag2 *pIEncoderOptions)
1310 {
1311     TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1312     TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1313
1314     EnterCriticalSection(&This->parent->lock);
1315
1316     if (This->initialized)
1317     {
1318         LeaveCriticalSection(&This->parent->lock);
1319         return WINCODEC_ERR_WRONGSTATE;
1320     }
1321
1322     This->initialized = TRUE;
1323
1324     LeaveCriticalSection(&This->parent->lock);
1325
1326     return S_OK;
1327 }
1328
1329 static HRESULT WINAPI TiffFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1330     UINT uiWidth, UINT uiHeight)
1331 {
1332     TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1333     TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1334
1335     EnterCriticalSection(&This->parent->lock);
1336
1337     if (!This->initialized || This->info_written)
1338     {
1339         LeaveCriticalSection(&This->parent->lock);
1340         return WINCODEC_ERR_WRONGSTATE;
1341     }
1342
1343     This->width = uiWidth;
1344     This->height = uiHeight;
1345
1346     LeaveCriticalSection(&This->parent->lock);
1347
1348     return S_OK;
1349 }
1350
1351 static HRESULT WINAPI TiffFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1352     double dpiX, double dpiY)
1353 {
1354     TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1355     TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1356
1357     EnterCriticalSection(&This->parent->lock);
1358
1359     if (!This->initialized || This->info_written)
1360     {
1361         LeaveCriticalSection(&This->parent->lock);
1362         return WINCODEC_ERR_WRONGSTATE;
1363     }
1364
1365     This->xres = dpiX;
1366     This->yres = dpiY;
1367
1368     LeaveCriticalSection(&This->parent->lock);
1369
1370     return S_OK;
1371 }
1372
1373 static HRESULT WINAPI TiffFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1374     WICPixelFormatGUID *pPixelFormat)
1375 {
1376     TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1377     int i;
1378
1379     TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1380
1381     EnterCriticalSection(&This->parent->lock);
1382
1383     if (!This->initialized || This->info_written)
1384     {
1385         LeaveCriticalSection(&This->parent->lock);
1386         return WINCODEC_ERR_WRONGSTATE;
1387     }
1388
1389     for (i=0; formats[i].guid; i++)
1390     {
1391         if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1392             break;
1393     }
1394
1395     if (!formats[i].guid) i = 0;
1396
1397     This->format = &formats[i];
1398     memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1399
1400     LeaveCriticalSection(&This->parent->lock);
1401
1402     return S_OK;
1403 }
1404
1405 static HRESULT WINAPI TiffFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1406     UINT cCount, IWICColorContext **ppIColorContext)
1407 {
1408     FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1409     return E_NOTIMPL;
1410 }
1411
1412 static HRESULT WINAPI TiffFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1413     IWICPalette *pIPalette)
1414 {
1415     FIXME("(%p,%p): stub\n", iface, pIPalette);
1416     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1417 }
1418
1419 static HRESULT WINAPI TiffFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1420     IWICBitmapSource *pIThumbnail)
1421 {
1422     FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1423     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1424 }
1425
1426 static HRESULT WINAPI TiffFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1427     UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1428 {
1429     TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1430     BYTE *row_data, *swapped_data = NULL;
1431     UINT i, j, line_size;
1432
1433     TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1434
1435     EnterCriticalSection(&This->parent->lock);
1436
1437     if (!This->initialized || !This->width || !This->height || !This->format)
1438     {
1439         LeaveCriticalSection(&This->parent->lock);
1440         return WINCODEC_ERR_WRONGSTATE;
1441     }
1442
1443     if (lineCount == 0 || lineCount + This->lines_written > This->height)
1444     {
1445         LeaveCriticalSection(&This->parent->lock);
1446         return E_INVALIDARG;
1447     }
1448
1449     line_size = ((This->width * This->format->bpp)+7)/8;
1450
1451     if (This->format->reverse_bgr)
1452     {
1453         swapped_data = HeapAlloc(GetProcessHeap(), 0, line_size);
1454         if (!swapped_data)
1455         {
1456             LeaveCriticalSection(&This->parent->lock);
1457             return E_OUTOFMEMORY;
1458         }
1459     }
1460
1461     if (!This->info_written)
1462     {
1463         pTIFFSetField(This->parent->tiff, TIFFTAG_PHOTOMETRIC, (uint16)This->format->photometric);
1464         pTIFFSetField(This->parent->tiff, TIFFTAG_PLANARCONFIG, (uint16)1);
1465         pTIFFSetField(This->parent->tiff, TIFFTAG_BITSPERSAMPLE, (uint16)This->format->bps);
1466         pTIFFSetField(This->parent->tiff, TIFFTAG_SAMPLESPERPIXEL, (uint16)This->format->samples);
1467
1468         if (This->format->extra_sample)
1469         {
1470             uint16 extra_samples;
1471             extra_samples = This->format->extra_sample_type;
1472
1473             pTIFFSetField(This->parent->tiff, TIFFTAG_EXTRASAMPLES, (uint16)1, &extra_samples);
1474         }
1475
1476         pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGEWIDTH, (uint32)This->width);
1477         pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGELENGTH, (uint32)This->height);
1478
1479         if (This->xres != 0.0 && This->yres != 0.0)
1480         {
1481             pTIFFSetField(This->parent->tiff, TIFFTAG_RESOLUTIONUNIT, (uint16)2); /* Inch */
1482             pTIFFSetField(This->parent->tiff, TIFFTAG_XRESOLUTION, (float)This->xres);
1483             pTIFFSetField(This->parent->tiff, TIFFTAG_YRESOLUTION, (float)This->yres);
1484         }
1485
1486         This->info_written = TRUE;
1487     }
1488
1489     for (i=0; i<lineCount; i++)
1490     {
1491         row_data = pbPixels + i * cbStride;
1492
1493         if (This->format->reverse_bgr && This->format->bps == 8)
1494         {
1495             memcpy(swapped_data, row_data, line_size);
1496             for (j=0; j<line_size; j += This->format->samples)
1497             {
1498                 BYTE temp;
1499                 temp = swapped_data[j];
1500                 swapped_data[j] = swapped_data[j+2];
1501                 swapped_data[j+2] = temp;
1502             }
1503             row_data = swapped_data;
1504         }
1505
1506         pTIFFWriteScanline(This->parent->tiff, (tdata_t)row_data, i+This->lines_written, 0);
1507     }
1508
1509     This->lines_written += lineCount;
1510
1511     LeaveCriticalSection(&This->parent->lock);
1512
1513     HeapFree(GetProcessHeap(), 0, swapped_data);
1514
1515     return S_OK;
1516 }
1517
1518 static HRESULT WINAPI TiffFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1519     IWICBitmapSource *pIBitmapSource, WICRect *prc)
1520 {
1521     TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1522     HRESULT hr;
1523     WICRect rc;
1524     WICPixelFormatGUID guid;
1525     UINT stride;
1526     BYTE *pixeldata;
1527
1528     TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1529
1530     if (!This->initialized || !This->width || !This->height)
1531         return WINCODEC_ERR_WRONGSTATE;
1532
1533     if (!This->format)
1534     {
1535         hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1536         if (FAILED(hr)) return hr;
1537         hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1538         if (FAILED(hr)) return hr;
1539     }
1540
1541     hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1542     if (FAILED(hr)) return hr;
1543     if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1544     {
1545         /* FIXME: should use WICConvertBitmapSource to convert */
1546         ERR("format %s unsupported\n", debugstr_guid(&guid));
1547         return E_FAIL;
1548     }
1549
1550     if (This->xres == 0.0 || This->yres == 0.0)
1551     {
1552         double xres, yres;
1553         hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1554         if (FAILED(hr)) return hr;
1555         hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1556         if (FAILED(hr)) return hr;
1557     }
1558
1559     if (!prc)
1560     {
1561         UINT width, height;
1562         hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1563         if (FAILED(hr)) return hr;
1564         rc.X = 0;
1565         rc.Y = 0;
1566         rc.Width = width;
1567         rc.Height = height;
1568         prc = &rc;
1569     }
1570
1571     if (prc->Width != This->width) return E_INVALIDARG;
1572
1573     stride = (This->format->bpp * This->width + 7)/8;
1574
1575     pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1576     if (!pixeldata) return E_OUTOFMEMORY;
1577
1578     hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1579         stride*prc->Height, pixeldata);
1580
1581     if (SUCCEEDED(hr))
1582     {
1583         hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1584             stride*prc->Height, pixeldata);
1585     }
1586
1587     HeapFree(GetProcessHeap(), 0, pixeldata);
1588
1589     return S_OK;
1590 }
1591
1592 static HRESULT WINAPI TiffFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1593 {
1594     TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1595
1596     TRACE("(%p)\n", iface);
1597
1598     EnterCriticalSection(&This->parent->lock);
1599
1600     if (!This->info_written || This->lines_written != This->height || This->committed)
1601     {
1602         LeaveCriticalSection(&This->parent->lock);
1603         return WINCODEC_ERR_WRONGSTATE;
1604     }
1605
1606     /* libtiff will commit the data when creating a new frame or closing the file */
1607
1608     This->committed = TRUE;
1609     This->parent->num_frames_committed++;
1610
1611     LeaveCriticalSection(&This->parent->lock);
1612
1613     return S_OK;
1614 }
1615
1616 static HRESULT WINAPI TiffFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1617     IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1618 {
1619     FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1620     return E_NOTIMPL;
1621 }
1622
1623 static const IWICBitmapFrameEncodeVtbl TiffFrameEncode_Vtbl = {
1624     TiffFrameEncode_QueryInterface,
1625     TiffFrameEncode_AddRef,
1626     TiffFrameEncode_Release,
1627     TiffFrameEncode_Initialize,
1628     TiffFrameEncode_SetSize,
1629     TiffFrameEncode_SetResolution,
1630     TiffFrameEncode_SetPixelFormat,
1631     TiffFrameEncode_SetColorContexts,
1632     TiffFrameEncode_SetPalette,
1633     TiffFrameEncode_SetThumbnail,
1634     TiffFrameEncode_WritePixels,
1635     TiffFrameEncode_WriteSource,
1636     TiffFrameEncode_Commit,
1637     TiffFrameEncode_GetMetadataQueryWriter
1638 };
1639
1640 static HRESULT WINAPI TiffEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1641     void **ppv)
1642 {
1643     TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1644     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1645
1646     if (!ppv) return E_INVALIDARG;
1647
1648     if (IsEqualIID(&IID_IUnknown, iid) ||
1649         IsEqualIID(&IID_IWICBitmapEncoder, iid))
1650     {
1651         *ppv = This;
1652     }
1653     else
1654     {
1655         *ppv = NULL;
1656         return E_NOINTERFACE;
1657     }
1658
1659     IUnknown_AddRef((IUnknown*)*ppv);
1660     return S_OK;
1661 }
1662
1663 static ULONG WINAPI TiffEncoder_AddRef(IWICBitmapEncoder *iface)
1664 {
1665     TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1666     ULONG ref = InterlockedIncrement(&This->ref);
1667
1668     TRACE("(%p) refcount=%u\n", iface, ref);
1669
1670     return ref;
1671 }
1672
1673 static ULONG WINAPI TiffEncoder_Release(IWICBitmapEncoder *iface)
1674 {
1675     TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1676     ULONG ref = InterlockedDecrement(&This->ref);
1677
1678     TRACE("(%p) refcount=%u\n", iface, ref);
1679
1680     if (ref == 0)
1681     {
1682         if (This->tiff) pTIFFClose(This->tiff);
1683         if (This->stream) IStream_Release(This->stream);
1684         This->lock.DebugInfo->Spare[0] = 0;
1685         DeleteCriticalSection(&This->lock);
1686         HeapFree(GetProcessHeap(), 0, This);
1687     }
1688
1689     return ref;
1690 }
1691
1692 static HRESULT WINAPI TiffEncoder_Initialize(IWICBitmapEncoder *iface,
1693     IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1694 {
1695     TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1696     TIFF *tiff;
1697     HRESULT hr=S_OK;
1698
1699     TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1700
1701     EnterCriticalSection(&This->lock);
1702
1703     if (This->initialized || This->committed)
1704     {
1705         hr = WINCODEC_ERR_WRONGSTATE;
1706         goto exit;
1707     }
1708
1709     tiff = tiff_open_stream(pIStream, "w");
1710
1711     if (!tiff)
1712     {
1713         hr = E_FAIL;
1714         goto exit;
1715     }
1716
1717     This->tiff = tiff;
1718     This->stream = pIStream;
1719     IStream_AddRef(pIStream);
1720     This->initialized = TRUE;
1721
1722 exit:
1723     LeaveCriticalSection(&This->lock);
1724     return hr;
1725 }
1726
1727 static HRESULT WINAPI TiffEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1728     GUID *pguidContainerFormat)
1729 {
1730     memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
1731     return S_OK;
1732 }
1733
1734 static HRESULT WINAPI TiffEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1735     IWICBitmapEncoderInfo **ppIEncoderInfo)
1736 {
1737     FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1738     return E_NOTIMPL;
1739 }
1740
1741 static HRESULT WINAPI TiffEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1742     UINT cCount, IWICColorContext **ppIColorContext)
1743 {
1744     FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1745     return E_NOTIMPL;
1746 }
1747
1748 static HRESULT WINAPI TiffEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1749 {
1750     TRACE("(%p,%p)\n", iface, pIPalette);
1751     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1752 }
1753
1754 static HRESULT WINAPI TiffEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1755 {
1756     TRACE("(%p,%p)\n", iface, pIThumbnail);
1757     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1758 }
1759
1760 static HRESULT WINAPI TiffEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1761 {
1762     TRACE("(%p,%p)\n", iface, pIPreview);
1763     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1764 }
1765
1766 static HRESULT WINAPI TiffEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1767     IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1768 {
1769     TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1770     TiffFrameEncode *result;
1771
1772     HRESULT hr=S_OK;
1773
1774     TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1775
1776     EnterCriticalSection(&This->lock);
1777
1778     if (!This->initialized || This->committed)
1779     {
1780         hr = WINCODEC_ERR_WRONGSTATE;
1781     }
1782     else if (This->num_frames != This->num_frames_committed)
1783     {
1784         FIXME("New frame created before previous frame was committed\n");
1785         hr = E_FAIL;
1786     }
1787
1788     if (SUCCEEDED(hr))
1789     {
1790         hr = CreatePropertyBag2(ppIEncoderOptions);
1791     }
1792
1793     if (SUCCEEDED(hr))
1794     {
1795         result = HeapAlloc(GetProcessHeap(), 0, sizeof(*result));
1796
1797         if (result)
1798         {
1799             result->IWICBitmapFrameEncode_iface.lpVtbl = &TiffFrameEncode_Vtbl;
1800             result->ref = 1;
1801             result->parent = This;
1802             result->initialized = FALSE;
1803             result->info_written = FALSE;
1804             result->committed = FALSE;
1805             result->format = NULL;
1806             result->width = 0;
1807             result->height = 0;
1808             result->xres = 0.0;
1809             result->yres = 0.0;
1810             result->lines_written = 0;
1811
1812             IWICBitmapEncoder_AddRef(iface);
1813             *ppIFrameEncode = &result->IWICBitmapFrameEncode_iface;
1814
1815             if (This->num_frames != 0)
1816                 pTIFFWriteDirectory(This->tiff);
1817
1818             This->num_frames++;
1819         }
1820         else
1821             hr = E_OUTOFMEMORY;
1822
1823         if (FAILED(hr))
1824         {
1825             IPropertyBag2_Release(*ppIEncoderOptions);
1826             *ppIEncoderOptions = NULL;
1827         }
1828     }
1829
1830     LeaveCriticalSection(&This->lock);
1831
1832     return hr;
1833 }
1834
1835 static HRESULT WINAPI TiffEncoder_Commit(IWICBitmapEncoder *iface)
1836 {
1837     TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1838
1839     TRACE("(%p)\n", iface);
1840
1841     EnterCriticalSection(&This->lock);
1842
1843     if (!This->initialized || This->committed)
1844     {
1845         LeaveCriticalSection(&This->lock);
1846         return WINCODEC_ERR_WRONGSTATE;
1847     }
1848
1849     pTIFFClose(This->tiff);
1850     IStream_Release(This->stream);
1851     This->stream = NULL;
1852     This->tiff = NULL;
1853
1854     This->committed = TRUE;
1855
1856     LeaveCriticalSection(&This->lock);
1857
1858     return S_OK;
1859 }
1860
1861 static HRESULT WINAPI TiffEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1862     IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1863 {
1864     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1865     return E_NOTIMPL;
1866 }
1867
1868 static const IWICBitmapEncoderVtbl TiffEncoder_Vtbl = {
1869     TiffEncoder_QueryInterface,
1870     TiffEncoder_AddRef,
1871     TiffEncoder_Release,
1872     TiffEncoder_Initialize,
1873     TiffEncoder_GetContainerFormat,
1874     TiffEncoder_GetEncoderInfo,
1875     TiffEncoder_SetColorContexts,
1876     TiffEncoder_SetPalette,
1877     TiffEncoder_SetThumbnail,
1878     TiffEncoder_SetPreview,
1879     TiffEncoder_CreateNewFrame,
1880     TiffEncoder_Commit,
1881     TiffEncoder_GetMetadataQueryWriter
1882 };
1883
1884 HRESULT TiffEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1885 {
1886     TiffEncoder *This;
1887     HRESULT ret;
1888
1889     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1890
1891     *ppv = NULL;
1892
1893     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1894
1895     if (!load_libtiff())
1896     {
1897         ERR("Failed writing TIFF because unable to load %s\n",SONAME_LIBTIFF);
1898         return E_FAIL;
1899     }
1900
1901     This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffEncoder));
1902     if (!This) return E_OUTOFMEMORY;
1903
1904     This->IWICBitmapEncoder_iface.lpVtbl = &TiffEncoder_Vtbl;
1905     This->ref = 1;
1906     This->stream = NULL;
1907     InitializeCriticalSection(&This->lock);
1908     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffEncoder.lock");
1909     This->tiff = NULL;
1910     This->initialized = FALSE;
1911     This->num_frames = 0;
1912     This->num_frames_committed = 0;
1913     This->committed = FALSE;
1914
1915     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1916     IUnknown_Release((IUnknown*)This);
1917
1918     return ret;
1919 }
1920
1921 #else /* !SONAME_LIBTIFF */
1922
1923 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1924 {
1925     ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
1926     return E_FAIL;
1927 }
1928
1929 HRESULT TiffEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1930 {
1931     ERR("Trying to save TIFF picture, but Wine was compiled without TIFF support.\n");
1932     return E_FAIL;
1933 }
1934
1935 #endif