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