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