[REACTOS]
[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 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
1113 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1114 }
1115
1116 static HRESULT WINAPI TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
1117 IWICBitmapSource **ppIThumbnail)
1118 {
1119 TRACE("(%p,%p)\n", iface, ppIThumbnail);
1120
1121 if (!ppIThumbnail) return E_INVALIDARG;
1122
1123 *ppIThumbnail = NULL;
1124 return WINCODEC_ERR_CODECNOTHUMBNAIL;
1125 }
1126
1127 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl = {
1128 TiffFrameDecode_QueryInterface,
1129 TiffFrameDecode_AddRef,
1130 TiffFrameDecode_Release,
1131 TiffFrameDecode_GetSize,
1132 TiffFrameDecode_GetPixelFormat,
1133 TiffFrameDecode_GetResolution,
1134 TiffFrameDecode_CopyPalette,
1135 TiffFrameDecode_CopyPixels,
1136 TiffFrameDecode_GetMetadataQueryReader,
1137 TiffFrameDecode_GetColorContexts,
1138 TiffFrameDecode_GetThumbnail
1139 };
1140
1141 static HRESULT WINAPI TiffFrameDecode_Block_QueryInterface(IWICMetadataBlockReader *iface,
1142 REFIID iid, void **ppv)
1143 {
1144 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1145 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
1146 }
1147
1148 static ULONG WINAPI TiffFrameDecode_Block_AddRef(IWICMetadataBlockReader *iface)
1149 {
1150 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1151 return IWICBitmapFrameDecode_AddRef(&This->IWICBitmapFrameDecode_iface);
1152 }
1153
1154 static ULONG WINAPI TiffFrameDecode_Block_Release(IWICMetadataBlockReader *iface)
1155 {
1156 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1157 return IWICBitmapFrameDecode_Release(&This->IWICBitmapFrameDecode_iface);
1158 }
1159
1160 static HRESULT WINAPI TiffFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
1161 GUID *guid)
1162 {
1163 TRACE("(%p,%p)\n", iface, guid);
1164
1165 if (!guid) return E_INVALIDARG;
1166
1167 *guid = GUID_ContainerFormatTiff;
1168 return S_OK;
1169 }
1170
1171 static HRESULT WINAPI TiffFrameDecode_Block_GetCount(IWICMetadataBlockReader *iface,
1172 UINT *count)
1173 {
1174 TRACE("%p,%p\n", iface, count);
1175
1176 if (!count) return E_INVALIDARG;
1177
1178 *count = 1;
1179 return S_OK;
1180 }
1181
1182 static HRESULT create_metadata_reader(TiffFrameDecode *This, IWICMetadataReader **reader)
1183 {
1184 HRESULT hr;
1185 LARGE_INTEGER dir_offset;
1186 IWICMetadataReader *metadata_reader;
1187 IWICPersistStream *persist;
1188
1189 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
1190
1191 hr = CoCreateInstance(&CLSID_WICIfdMetadataReader, NULL, CLSCTX_INPROC_SERVER,
1192 &IID_IWICMetadataReader, (void **)&metadata_reader);
1193 if (FAILED(hr)) return hr;
1194
1195 hr = IWICMetadataReader_QueryInterface(metadata_reader, &IID_IWICPersistStream, (void **)&persist);
1196 if (FAILED(hr))
1197 {
1198 IWICMetadataReader_Release(metadata_reader);
1199 return hr;
1200 }
1201
1202 EnterCriticalSection(&This->parent->lock);
1203
1204 dir_offset.QuadPart = pTIFFCurrentDirOffset(This->parent->tiff);
1205 hr = IStream_Seek(This->parent->stream, dir_offset, STREAM_SEEK_SET, NULL);
1206 if (SUCCEEDED(hr))
1207 {
1208 BOOL byte_swapped = pTIFFIsByteSwapped(This->parent->tiff);
1209 #ifdef WORDS_BIGENDIAN
1210 DWORD persist_options = byte_swapped ? WICPersistOptionsLittleEndian : WICPersistOptionsBigEndian;
1211 #else
1212 DWORD persist_options = byte_swapped ? WICPersistOptionsBigEndian : WICPersistOptionsLittleEndian;
1213 #endif
1214 persist_options |= WICPersistOptionsNoCacheStream;
1215 hr = IWICPersistStream_LoadEx(persist, This->parent->stream, NULL, persist_options);
1216 if (FAILED(hr))
1217 ERR("IWICPersistStream_LoadEx error %#x\n", hr);
1218 }
1219
1220 LeaveCriticalSection(&This->parent->lock);
1221
1222 IWICPersistStream_Release(persist);
1223
1224 if (FAILED(hr))
1225 {
1226 IWICMetadataReader_Release(metadata_reader);
1227 return hr;
1228 }
1229
1230 *reader = metadata_reader;
1231 return S_OK;
1232 }
1233
1234 static HRESULT WINAPI TiffFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
1235 UINT index, IWICMetadataReader **reader)
1236 {
1237 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1238
1239 TRACE("(%p,%u,%p)\n", iface, index, reader);
1240
1241 if (!reader || index != 0) return E_INVALIDARG;
1242
1243 return create_metadata_reader(This, reader);
1244 }
1245
1246 static HRESULT WINAPI TiffFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader *iface,
1247 IEnumUnknown **enum_metadata)
1248 {
1249 FIXME("(%p,%p): stub\n", iface, enum_metadata);
1250 return E_NOTIMPL;
1251 }
1252
1253 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl =
1254 {
1255 TiffFrameDecode_Block_QueryInterface,
1256 TiffFrameDecode_Block_AddRef,
1257 TiffFrameDecode_Block_Release,
1258 TiffFrameDecode_Block_GetContainerFormat,
1259 TiffFrameDecode_Block_GetCount,
1260 TiffFrameDecode_Block_GetReaderByIndex,
1261 TiffFrameDecode_Block_GetEnumerator
1262 };
1263
1264 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1265 {
1266 HRESULT ret;
1267 TiffDecoder *This;
1268
1269 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1270
1271 *ppv = NULL;
1272
1273 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1274
1275 if (!load_libtiff())
1276 {
1277 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF);
1278 return E_FAIL;
1279 }
1280
1281 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder));
1282 if (!This) return E_OUTOFMEMORY;
1283
1284 This->IWICBitmapDecoder_iface.lpVtbl = &TiffDecoder_Vtbl;
1285 This->ref = 1;
1286 This->stream = NULL;
1287 InitializeCriticalSection(&This->lock);
1288 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffDecoder.lock");
1289 This->tiff = NULL;
1290 This->initialized = FALSE;
1291
1292 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1293 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1294
1295 return ret;
1296 }
1297
1298 struct tiff_encode_format {
1299 const WICPixelFormatGUID *guid;
1300 int photometric;
1301 int bps;
1302 int samples;
1303 int bpp;
1304 int extra_sample;
1305 int extra_sample_type;
1306 int reverse_bgr;
1307 };
1308
1309 static const struct tiff_encode_format formats[] = {
1310 {&GUID_WICPixelFormat24bppBGR, 2, 8, 3, 24, 0, 0, 1},
1311 {&GUID_WICPixelFormatBlackWhite, 1, 1, 1, 1, 0, 0, 0},
1312 {&GUID_WICPixelFormat4bppGray, 1, 4, 1, 4, 0, 0, 0},
1313 {&GUID_WICPixelFormat8bppGray, 1, 8, 1, 8, 0, 0, 0},
1314 {&GUID_WICPixelFormat32bppBGRA, 2, 8, 4, 32, 1, 2, 1},
1315 {&GUID_WICPixelFormat32bppPBGRA, 2, 8, 4, 32, 1, 1, 1},
1316 {&GUID_WICPixelFormat48bppRGB, 2, 16, 3, 48, 0, 0, 0},
1317 {&GUID_WICPixelFormat64bppRGBA, 2, 16, 4, 64, 1, 2, 0},
1318 {&GUID_WICPixelFormat64bppPRGBA, 2, 16, 4, 64, 1, 1, 0},
1319 {0}
1320 };
1321
1322 typedef struct TiffEncoder {
1323 IWICBitmapEncoder IWICBitmapEncoder_iface;
1324 LONG ref;
1325 IStream *stream;
1326 CRITICAL_SECTION lock; /* Must be held when tiff is used or fields below are set */
1327 TIFF *tiff;
1328 BOOL initialized;
1329 BOOL committed;
1330 ULONG num_frames;
1331 ULONG num_frames_committed;
1332 } TiffEncoder;
1333
1334 static inline TiffEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1335 {
1336 return CONTAINING_RECORD(iface, TiffEncoder, IWICBitmapEncoder_iface);
1337 }
1338
1339 typedef struct TiffFrameEncode {
1340 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1341 LONG ref;
1342 TiffEncoder *parent;
1343 /* fields below are protected by parent->lock */
1344 BOOL initialized;
1345 BOOL info_written;
1346 BOOL committed;
1347 const struct tiff_encode_format *format;
1348 UINT width, height;
1349 double xres, yres;
1350 UINT lines_written;
1351 } TiffFrameEncode;
1352
1353 static inline TiffFrameEncode *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1354 {
1355 return CONTAINING_RECORD(iface, TiffFrameEncode, IWICBitmapFrameEncode_iface);
1356 }
1357
1358 static HRESULT WINAPI TiffFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1359 void **ppv)
1360 {
1361 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1362 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1363
1364 if (!ppv) return E_INVALIDARG;
1365
1366 if (IsEqualIID(&IID_IUnknown, iid) ||
1367 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1368 {
1369 *ppv = &This->IWICBitmapFrameEncode_iface;
1370 }
1371 else
1372 {
1373 *ppv = NULL;
1374 return E_NOINTERFACE;
1375 }
1376
1377 IUnknown_AddRef((IUnknown*)*ppv);
1378 return S_OK;
1379 }
1380
1381 static ULONG WINAPI TiffFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1382 {
1383 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1384 ULONG ref = InterlockedIncrement(&This->ref);
1385
1386 TRACE("(%p) refcount=%u\n", iface, ref);
1387
1388 return ref;
1389 }
1390
1391 static ULONG WINAPI TiffFrameEncode_Release(IWICBitmapFrameEncode *iface)
1392 {
1393 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1394 ULONG ref = InterlockedDecrement(&This->ref);
1395
1396 TRACE("(%p) refcount=%u\n", iface, ref);
1397
1398 if (ref == 0)
1399 {
1400 IWICBitmapEncoder_Release(&This->parent->IWICBitmapEncoder_iface);
1401 HeapFree(GetProcessHeap(), 0, This);
1402 }
1403
1404 return ref;
1405 }
1406
1407 static HRESULT WINAPI TiffFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1408 IPropertyBag2 *pIEncoderOptions)
1409 {
1410 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1411 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1412
1413 EnterCriticalSection(&This->parent->lock);
1414
1415 if (This->initialized)
1416 {
1417 LeaveCriticalSection(&This->parent->lock);
1418 return WINCODEC_ERR_WRONGSTATE;
1419 }
1420
1421 This->initialized = TRUE;
1422
1423 LeaveCriticalSection(&This->parent->lock);
1424
1425 return S_OK;
1426 }
1427
1428 static HRESULT WINAPI TiffFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1429 UINT uiWidth, UINT uiHeight)
1430 {
1431 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1432 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1433
1434 EnterCriticalSection(&This->parent->lock);
1435
1436 if (!This->initialized || This->info_written)
1437 {
1438 LeaveCriticalSection(&This->parent->lock);
1439 return WINCODEC_ERR_WRONGSTATE;
1440 }
1441
1442 This->width = uiWidth;
1443 This->height = uiHeight;
1444
1445 LeaveCriticalSection(&This->parent->lock);
1446
1447 return S_OK;
1448 }
1449
1450 static HRESULT WINAPI TiffFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1451 double dpiX, double dpiY)
1452 {
1453 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1454 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1455
1456 EnterCriticalSection(&This->parent->lock);
1457
1458 if (!This->initialized || This->info_written)
1459 {
1460 LeaveCriticalSection(&This->parent->lock);
1461 return WINCODEC_ERR_WRONGSTATE;
1462 }
1463
1464 This->xres = dpiX;
1465 This->yres = dpiY;
1466
1467 LeaveCriticalSection(&This->parent->lock);
1468
1469 return S_OK;
1470 }
1471
1472 static HRESULT WINAPI TiffFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1473 WICPixelFormatGUID *pPixelFormat)
1474 {
1475 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1476 int i;
1477
1478 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1479
1480 EnterCriticalSection(&This->parent->lock);
1481
1482 if (!This->initialized || This->info_written)
1483 {
1484 LeaveCriticalSection(&This->parent->lock);
1485 return WINCODEC_ERR_WRONGSTATE;
1486 }
1487
1488 for (i=0; formats[i].guid; i++)
1489 {
1490 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1491 break;
1492 }
1493
1494 if (!formats[i].guid) i = 0;
1495
1496 This->format = &formats[i];
1497 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1498
1499 LeaveCriticalSection(&This->parent->lock);
1500
1501 return S_OK;
1502 }
1503
1504 static HRESULT WINAPI TiffFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1505 UINT cCount, IWICColorContext **ppIColorContext)
1506 {
1507 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1508 return E_NOTIMPL;
1509 }
1510
1511 static HRESULT WINAPI TiffFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1512 IWICPalette *pIPalette)
1513 {
1514 FIXME("(%p,%p): stub\n", iface, pIPalette);
1515 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1516 }
1517
1518 static HRESULT WINAPI TiffFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1519 IWICBitmapSource *pIThumbnail)
1520 {
1521 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1522 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1523 }
1524
1525 static HRESULT WINAPI TiffFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1526 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1527 {
1528 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1529 BYTE *row_data, *swapped_data = NULL;
1530 UINT i, j, line_size;
1531
1532 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1533
1534 EnterCriticalSection(&This->parent->lock);
1535
1536 if (!This->initialized || !This->width || !This->height || !This->format)
1537 {
1538 LeaveCriticalSection(&This->parent->lock);
1539 return WINCODEC_ERR_WRONGSTATE;
1540 }
1541
1542 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1543 {
1544 LeaveCriticalSection(&This->parent->lock);
1545 return E_INVALIDARG;
1546 }
1547
1548 line_size = ((This->width * This->format->bpp)+7)/8;
1549
1550 if (This->format->reverse_bgr)
1551 {
1552 swapped_data = HeapAlloc(GetProcessHeap(), 0, line_size);
1553 if (!swapped_data)
1554 {
1555 LeaveCriticalSection(&This->parent->lock);
1556 return E_OUTOFMEMORY;
1557 }
1558 }
1559
1560 if (!This->info_written)
1561 {
1562 pTIFFSetField(This->parent->tiff, TIFFTAG_PHOTOMETRIC, (uint16)This->format->photometric);
1563 pTIFFSetField(This->parent->tiff, TIFFTAG_PLANARCONFIG, (uint16)1);
1564 pTIFFSetField(This->parent->tiff, TIFFTAG_BITSPERSAMPLE, (uint16)This->format->bps);
1565 pTIFFSetField(This->parent->tiff, TIFFTAG_SAMPLESPERPIXEL, (uint16)This->format->samples);
1566
1567 if (This->format->extra_sample)
1568 {
1569 uint16 extra_samples;
1570 extra_samples = This->format->extra_sample_type;
1571
1572 pTIFFSetField(This->parent->tiff, TIFFTAG_EXTRASAMPLES, (uint16)1, &extra_samples);
1573 }
1574
1575 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGEWIDTH, (uint32)This->width);
1576 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGELENGTH, (uint32)This->height);
1577
1578 if (This->xres != 0.0 && This->yres != 0.0)
1579 {
1580 pTIFFSetField(This->parent->tiff, TIFFTAG_RESOLUTIONUNIT, (uint16)2); /* Inch */
1581 pTIFFSetField(This->parent->tiff, TIFFTAG_XRESOLUTION, (float)This->xres);
1582 pTIFFSetField(This->parent->tiff, TIFFTAG_YRESOLUTION, (float)This->yres);
1583 }
1584
1585 This->info_written = TRUE;
1586 }
1587
1588 for (i=0; i<lineCount; i++)
1589 {
1590 row_data = pbPixels + i * cbStride;
1591
1592 if (This->format->reverse_bgr && This->format->bps == 8)
1593 {
1594 memcpy(swapped_data, row_data, line_size);
1595 for (j=0; j<line_size; j += This->format->samples)
1596 {
1597 BYTE temp;
1598 temp = swapped_data[j];
1599 swapped_data[j] = swapped_data[j+2];
1600 swapped_data[j+2] = temp;
1601 }
1602 row_data = swapped_data;
1603 }
1604
1605 pTIFFWriteScanline(This->parent->tiff, (tdata_t)row_data, i+This->lines_written, 0);
1606 }
1607
1608 This->lines_written += lineCount;
1609
1610 LeaveCriticalSection(&This->parent->lock);
1611
1612 HeapFree(GetProcessHeap(), 0, swapped_data);
1613
1614 return S_OK;
1615 }
1616
1617 static HRESULT WINAPI TiffFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1618 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1619 {
1620 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1621 HRESULT hr;
1622 WICRect rc;
1623 WICPixelFormatGUID guid;
1624 UINT stride;
1625 BYTE *pixeldata;
1626
1627 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1628
1629 if (!This->initialized || !This->width || !This->height)
1630 return WINCODEC_ERR_WRONGSTATE;
1631
1632 if (!This->format)
1633 {
1634 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1635 if (FAILED(hr)) return hr;
1636 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1637 if (FAILED(hr)) return hr;
1638 }
1639
1640 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1641 if (FAILED(hr)) return hr;
1642 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1643 {
1644 /* FIXME: should use WICConvertBitmapSource to convert */
1645 ERR("format %s unsupported\n", debugstr_guid(&guid));
1646 return E_FAIL;
1647 }
1648
1649 if (This->xres == 0.0 || This->yres == 0.0)
1650 {
1651 double xres, yres;
1652 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1653 if (FAILED(hr)) return hr;
1654 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1655 if (FAILED(hr)) return hr;
1656 }
1657
1658 if (!prc)
1659 {
1660 UINT width, height;
1661 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1662 if (FAILED(hr)) return hr;
1663 rc.X = 0;
1664 rc.Y = 0;
1665 rc.Width = width;
1666 rc.Height = height;
1667 prc = &rc;
1668 }
1669
1670 if (prc->Width != This->width) return E_INVALIDARG;
1671
1672 stride = (This->format->bpp * This->width + 7)/8;
1673
1674 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1675 if (!pixeldata) return E_OUTOFMEMORY;
1676
1677 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1678 stride*prc->Height, pixeldata);
1679
1680 if (SUCCEEDED(hr))
1681 {
1682 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1683 stride*prc->Height, pixeldata);
1684 }
1685
1686 HeapFree(GetProcessHeap(), 0, pixeldata);
1687
1688 return S_OK;
1689 }
1690
1691 static HRESULT WINAPI TiffFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1692 {
1693 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1694
1695 TRACE("(%p)\n", iface);
1696
1697 EnterCriticalSection(&This->parent->lock);
1698
1699 if (!This->info_written || This->lines_written != This->height || This->committed)
1700 {
1701 LeaveCriticalSection(&This->parent->lock);
1702 return WINCODEC_ERR_WRONGSTATE;
1703 }
1704
1705 /* libtiff will commit the data when creating a new frame or closing the file */
1706
1707 This->committed = TRUE;
1708 This->parent->num_frames_committed++;
1709
1710 LeaveCriticalSection(&This->parent->lock);
1711
1712 return S_OK;
1713 }
1714
1715 static HRESULT WINAPI TiffFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1716 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1717 {
1718 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1719 return E_NOTIMPL;
1720 }
1721
1722 static const IWICBitmapFrameEncodeVtbl TiffFrameEncode_Vtbl = {
1723 TiffFrameEncode_QueryInterface,
1724 TiffFrameEncode_AddRef,
1725 TiffFrameEncode_Release,
1726 TiffFrameEncode_Initialize,
1727 TiffFrameEncode_SetSize,
1728 TiffFrameEncode_SetResolution,
1729 TiffFrameEncode_SetPixelFormat,
1730 TiffFrameEncode_SetColorContexts,
1731 TiffFrameEncode_SetPalette,
1732 TiffFrameEncode_SetThumbnail,
1733 TiffFrameEncode_WritePixels,
1734 TiffFrameEncode_WriteSource,
1735 TiffFrameEncode_Commit,
1736 TiffFrameEncode_GetMetadataQueryWriter
1737 };
1738
1739 static HRESULT WINAPI TiffEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1740 void **ppv)
1741 {
1742 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1743 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1744
1745 if (!ppv) return E_INVALIDARG;
1746
1747 if (IsEqualIID(&IID_IUnknown, iid) ||
1748 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1749 {
1750 *ppv = &This->IWICBitmapEncoder_iface;
1751 }
1752 else
1753 {
1754 *ppv = NULL;
1755 return E_NOINTERFACE;
1756 }
1757
1758 IUnknown_AddRef((IUnknown*)*ppv);
1759 return S_OK;
1760 }
1761
1762 static ULONG WINAPI TiffEncoder_AddRef(IWICBitmapEncoder *iface)
1763 {
1764 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1765 ULONG ref = InterlockedIncrement(&This->ref);
1766
1767 TRACE("(%p) refcount=%u\n", iface, ref);
1768
1769 return ref;
1770 }
1771
1772 static ULONG WINAPI TiffEncoder_Release(IWICBitmapEncoder *iface)
1773 {
1774 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1775 ULONG ref = InterlockedDecrement(&This->ref);
1776
1777 TRACE("(%p) refcount=%u\n", iface, ref);
1778
1779 if (ref == 0)
1780 {
1781 if (This->tiff) pTIFFClose(This->tiff);
1782 if (This->stream) IStream_Release(This->stream);
1783 This->lock.DebugInfo->Spare[0] = 0;
1784 DeleteCriticalSection(&This->lock);
1785 HeapFree(GetProcessHeap(), 0, This);
1786 }
1787
1788 return ref;
1789 }
1790
1791 static HRESULT WINAPI TiffEncoder_Initialize(IWICBitmapEncoder *iface,
1792 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1793 {
1794 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1795 TIFF *tiff;
1796 HRESULT hr=S_OK;
1797
1798 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1799
1800 EnterCriticalSection(&This->lock);
1801
1802 if (This->initialized || This->committed)
1803 {
1804 hr = WINCODEC_ERR_WRONGSTATE;
1805 goto exit;
1806 }
1807
1808 tiff = tiff_open_stream(pIStream, "w");
1809
1810 if (!tiff)
1811 {
1812 hr = E_FAIL;
1813 goto exit;
1814 }
1815
1816 This->tiff = tiff;
1817 This->stream = pIStream;
1818 IStream_AddRef(pIStream);
1819 This->initialized = TRUE;
1820
1821 exit:
1822 LeaveCriticalSection(&This->lock);
1823 return hr;
1824 }
1825
1826 static HRESULT WINAPI TiffEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1827 GUID *pguidContainerFormat)
1828 {
1829 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
1830 return S_OK;
1831 }
1832
1833 static HRESULT WINAPI TiffEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1834 IWICBitmapEncoderInfo **ppIEncoderInfo)
1835 {
1836 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1837 return E_NOTIMPL;
1838 }
1839
1840 static HRESULT WINAPI TiffEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1841 UINT cCount, IWICColorContext **ppIColorContext)
1842 {
1843 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1844 return E_NOTIMPL;
1845 }
1846
1847 static HRESULT WINAPI TiffEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1848 {
1849 TRACE("(%p,%p)\n", iface, pIPalette);
1850 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1851 }
1852
1853 static HRESULT WINAPI TiffEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1854 {
1855 TRACE("(%p,%p)\n", iface, pIThumbnail);
1856 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1857 }
1858
1859 static HRESULT WINAPI TiffEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1860 {
1861 TRACE("(%p,%p)\n", iface, pIPreview);
1862 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1863 }
1864
1865 static HRESULT WINAPI TiffEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1866 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1867 {
1868 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1869 TiffFrameEncode *result;
1870
1871 HRESULT hr=S_OK;
1872
1873 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1874
1875 EnterCriticalSection(&This->lock);
1876
1877 if (!This->initialized || This->committed)
1878 {
1879 hr = WINCODEC_ERR_WRONGSTATE;
1880 }
1881 else if (This->num_frames != This->num_frames_committed)
1882 {
1883 FIXME("New frame created before previous frame was committed\n");
1884 hr = E_FAIL;
1885 }
1886
1887 if (SUCCEEDED(hr))
1888 {
1889 hr = CreatePropertyBag2(ppIEncoderOptions);
1890 }
1891
1892 if (SUCCEEDED(hr))
1893 {
1894 result = HeapAlloc(GetProcessHeap(), 0, sizeof(*result));
1895
1896 if (result)
1897 {
1898 result->IWICBitmapFrameEncode_iface.lpVtbl = &TiffFrameEncode_Vtbl;
1899 result->ref = 1;
1900 result->parent = This;
1901 result->initialized = FALSE;
1902 result->info_written = FALSE;
1903 result->committed = FALSE;
1904 result->format = NULL;
1905 result->width = 0;
1906 result->height = 0;
1907 result->xres = 0.0;
1908 result->yres = 0.0;
1909 result->lines_written = 0;
1910
1911 IWICBitmapEncoder_AddRef(iface);
1912 *ppIFrameEncode = &result->IWICBitmapFrameEncode_iface;
1913
1914 if (This->num_frames != 0)
1915 pTIFFWriteDirectory(This->tiff);
1916
1917 This->num_frames++;
1918 }
1919 else
1920 hr = E_OUTOFMEMORY;
1921
1922 if (FAILED(hr))
1923 {
1924 IPropertyBag2_Release(*ppIEncoderOptions);
1925 *ppIEncoderOptions = NULL;
1926 }
1927 }
1928
1929 LeaveCriticalSection(&This->lock);
1930
1931 return hr;
1932 }
1933
1934 static HRESULT WINAPI TiffEncoder_Commit(IWICBitmapEncoder *iface)
1935 {
1936 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1937
1938 TRACE("(%p)\n", iface);
1939
1940 EnterCriticalSection(&This->lock);
1941
1942 if (!This->initialized || This->committed)
1943 {
1944 LeaveCriticalSection(&This->lock);
1945 return WINCODEC_ERR_WRONGSTATE;
1946 }
1947
1948 pTIFFClose(This->tiff);
1949 IStream_Release(This->stream);
1950 This->stream = NULL;
1951 This->tiff = NULL;
1952
1953 This->committed = TRUE;
1954
1955 LeaveCriticalSection(&This->lock);
1956
1957 return S_OK;
1958 }
1959
1960 static HRESULT WINAPI TiffEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1961 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1962 {
1963 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1964 return E_NOTIMPL;
1965 }
1966
1967 static const IWICBitmapEncoderVtbl TiffEncoder_Vtbl = {
1968 TiffEncoder_QueryInterface,
1969 TiffEncoder_AddRef,
1970 TiffEncoder_Release,
1971 TiffEncoder_Initialize,
1972 TiffEncoder_GetContainerFormat,
1973 TiffEncoder_GetEncoderInfo,
1974 TiffEncoder_SetColorContexts,
1975 TiffEncoder_SetPalette,
1976 TiffEncoder_SetThumbnail,
1977 TiffEncoder_SetPreview,
1978 TiffEncoder_CreateNewFrame,
1979 TiffEncoder_Commit,
1980 TiffEncoder_GetMetadataQueryWriter
1981 };
1982
1983 HRESULT TiffEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1984 {
1985 TiffEncoder *This;
1986 HRESULT ret;
1987
1988 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1989
1990 *ppv = NULL;
1991
1992 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1993
1994 if (!load_libtiff())
1995 {
1996 ERR("Failed writing TIFF because unable to load %s\n",SONAME_LIBTIFF);
1997 return E_FAIL;
1998 }
1999
2000 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffEncoder));
2001 if (!This) return E_OUTOFMEMORY;
2002
2003 This->IWICBitmapEncoder_iface.lpVtbl = &TiffEncoder_Vtbl;
2004 This->ref = 1;
2005 This->stream = NULL;
2006 InitializeCriticalSection(&This->lock);
2007 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffEncoder.lock");
2008 This->tiff = NULL;
2009 This->initialized = FALSE;
2010 This->num_frames = 0;
2011 This->num_frames_committed = 0;
2012 This->committed = FALSE;
2013
2014 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
2015 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
2016
2017 return ret;
2018 }
2019
2020 #else /* !SONAME_LIBTIFF */
2021
2022 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
2023 {
2024 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
2025 return E_FAIL;
2026 }
2027
2028 HRESULT TiffEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
2029 {
2030 ERR("Trying to save TIFF picture, but Wine was compiled without TIFF support.\n");
2031 return E_FAIL;
2032 }
2033
2034 #endif