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