wined3d: Introduce struct wined3d_rendertarget_view_desc.
[wine.git] / dlls / windowscodecs / tiffformat.c
blob74a73bcb7ec6007a443cea5dbe64a8b3b085807c
1 /*
2 * Copyright 2010 Vincent Povirk for CodeWeavers
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.
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.
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdarg.h>
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #ifdef HAVE_TIFFIO_H
27 #include <tiffio.h>
28 #endif
30 #define COBJMACROS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "objbase.h"
35 #include "wincodec.h"
36 #include "wincodecsdk.h"
38 #include "wincodecs_private.h"
40 #include "wine/debug.h"
41 #include "wine/library.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
45 #ifdef SONAME_LIBTIFF
47 /* Workaround for broken libtiff 4.x headers on some 64-bit hosts which
48 * define TIFF_UINT64_T/toff_t as 32-bit for 32-bit builds, while they
49 * are supposed to be always 64-bit.
50 * TIFF_UINT64_T doesn't exist in libtiff 3.x, it was introduced in 4.x.
52 #ifdef TIFF_UINT64_T
53 # undef toff_t
54 # define toff_t UINT64
55 #endif
57 static CRITICAL_SECTION init_tiff_cs;
58 static CRITICAL_SECTION_DEBUG init_tiff_cs_debug =
60 0, 0, &init_tiff_cs,
61 { &init_tiff_cs_debug.ProcessLocksList,
62 &init_tiff_cs_debug.ProcessLocksList },
63 0, 0, { (DWORD_PTR)(__FILE__ ": init_tiff_cs") }
65 static CRITICAL_SECTION init_tiff_cs = { &init_tiff_cs_debug, -1, 0, 0, 0, 0 };
67 static const WCHAR wszTiffCompressionMethod[] = {'T','i','f','f','C','o','m','p','r','e','s','s','i','o','n','M','e','t','h','o','d',0};
68 static const WCHAR wszCompressionQuality[] = {'C','o','m','p','r','e','s','s','i','o','n','Q','u','a','l','i','t','y',0};
70 static void *libtiff_handle;
71 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
72 MAKE_FUNCPTR(TIFFClientOpen);
73 MAKE_FUNCPTR(TIFFClose);
74 MAKE_FUNCPTR(TIFFCurrentDirOffset);
75 MAKE_FUNCPTR(TIFFGetField);
76 MAKE_FUNCPTR(TIFFIsByteSwapped);
77 MAKE_FUNCPTR(TIFFNumberOfDirectories);
78 MAKE_FUNCPTR(TIFFReadDirectory);
79 MAKE_FUNCPTR(TIFFReadEncodedStrip);
80 MAKE_FUNCPTR(TIFFReadEncodedTile);
81 MAKE_FUNCPTR(TIFFSetDirectory);
82 MAKE_FUNCPTR(TIFFSetField);
83 MAKE_FUNCPTR(TIFFWriteDirectory);
84 MAKE_FUNCPTR(TIFFWriteScanline);
85 #undef MAKE_FUNCPTR
87 static void *load_libtiff(void)
89 void *result;
91 EnterCriticalSection(&init_tiff_cs);
93 if (!libtiff_handle &&
94 (libtiff_handle = wine_dlopen(SONAME_LIBTIFF, RTLD_NOW, NULL, 0)) != NULL)
96 void * (*pTIFFSetWarningHandler)(void *);
97 void * (*pTIFFSetWarningHandlerExt)(void *);
99 #define LOAD_FUNCPTR(f) \
100 if((p##f = wine_dlsym(libtiff_handle, #f, NULL, 0)) == NULL) { \
101 ERR("failed to load symbol %s\n", #f); \
102 libtiff_handle = NULL; \
103 LeaveCriticalSection(&init_tiff_cs); \
104 return NULL; \
106 LOAD_FUNCPTR(TIFFClientOpen);
107 LOAD_FUNCPTR(TIFFClose);
108 LOAD_FUNCPTR(TIFFCurrentDirOffset);
109 LOAD_FUNCPTR(TIFFGetField);
110 LOAD_FUNCPTR(TIFFIsByteSwapped);
111 LOAD_FUNCPTR(TIFFNumberOfDirectories);
112 LOAD_FUNCPTR(TIFFReadDirectory);
113 LOAD_FUNCPTR(TIFFReadEncodedStrip);
114 LOAD_FUNCPTR(TIFFReadEncodedTile);
115 LOAD_FUNCPTR(TIFFSetDirectory);
116 LOAD_FUNCPTR(TIFFSetField);
117 LOAD_FUNCPTR(TIFFWriteDirectory);
118 LOAD_FUNCPTR(TIFFWriteScanline);
119 #undef LOAD_FUNCPTR
121 if ((pTIFFSetWarningHandler = wine_dlsym(libtiff_handle, "TIFFSetWarningHandler", NULL, 0)))
122 pTIFFSetWarningHandler(NULL);
123 if ((pTIFFSetWarningHandlerExt = wine_dlsym(libtiff_handle, "TIFFSetWarningHandlerExt", NULL, 0)))
124 pTIFFSetWarningHandlerExt(NULL);
127 result = libtiff_handle;
129 LeaveCriticalSection(&init_tiff_cs);
130 return result;
133 static tsize_t tiff_stream_read(thandle_t client_data, tdata_t data, tsize_t size)
135 IStream *stream = (IStream*)client_data;
136 ULONG bytes_read;
137 HRESULT hr;
139 hr = IStream_Read(stream, data, size, &bytes_read);
140 if (FAILED(hr)) bytes_read = 0;
141 return bytes_read;
144 static tsize_t tiff_stream_write(thandle_t client_data, tdata_t data, tsize_t size)
146 IStream *stream = (IStream*)client_data;
147 ULONG bytes_written;
148 HRESULT hr;
150 hr = IStream_Write(stream, data, size, &bytes_written);
151 if (FAILED(hr)) bytes_written = 0;
152 return bytes_written;
155 static toff_t tiff_stream_seek(thandle_t client_data, toff_t offset, int whence)
157 IStream *stream = (IStream*)client_data;
158 LARGE_INTEGER move;
159 DWORD origin;
160 ULARGE_INTEGER new_position;
161 HRESULT hr;
163 move.QuadPart = offset;
164 switch (whence)
166 case SEEK_SET:
167 origin = STREAM_SEEK_SET;
168 break;
169 case SEEK_CUR:
170 origin = STREAM_SEEK_CUR;
171 break;
172 case SEEK_END:
173 origin = STREAM_SEEK_END;
174 break;
175 default:
176 ERR("unknown whence value %i\n", whence);
177 return -1;
180 hr = IStream_Seek(stream, move, origin, &new_position);
181 if (SUCCEEDED(hr)) return new_position.QuadPart;
182 else return -1;
185 static int tiff_stream_close(thandle_t client_data)
187 /* Caller is responsible for releasing the stream object. */
188 return 0;
191 static toff_t tiff_stream_size(thandle_t client_data)
193 IStream *stream = (IStream*)client_data;
194 STATSTG statstg;
195 HRESULT hr;
197 hr = IStream_Stat(stream, &statstg, STATFLAG_NONAME);
199 if (SUCCEEDED(hr)) return statstg.cbSize.QuadPart;
200 else return -1;
203 static int tiff_stream_map(thandle_t client_data, tdata_t *addr, toff_t *size)
205 /* Cannot mmap streams */
206 return 0;
209 static void tiff_stream_unmap(thandle_t client_data, tdata_t addr, toff_t size)
211 /* No need to ever do this, since we can't map things. */
214 static TIFF* tiff_open_stream(IStream *stream, const char *mode)
216 LARGE_INTEGER zero;
218 zero.QuadPart = 0;
219 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
221 return pTIFFClientOpen("<IStream object>", mode, stream, tiff_stream_read,
222 tiff_stream_write, (void *)tiff_stream_seek, tiff_stream_close,
223 (void *)tiff_stream_size, (void *)tiff_stream_map, (void *)tiff_stream_unmap);
226 typedef struct {
227 IWICBitmapDecoder IWICBitmapDecoder_iface;
228 LONG ref;
229 IStream *stream;
230 CRITICAL_SECTION lock; /* Must be held when tiff is used or initiailzed is set */
231 TIFF *tiff;
232 BOOL initialized;
233 } TiffDecoder;
235 typedef struct {
236 const WICPixelFormatGUID *format;
237 int bps;
238 int samples;
239 int bpp;
240 int planar;
241 int indexed;
242 int reverse_bgr;
243 int invert_grayscale;
244 UINT width, height;
245 UINT tile_width, tile_height;
246 UINT tile_stride;
247 UINT tile_size;
248 int tiled;
249 UINT tiles_across;
250 UINT resolution_unit;
251 float xres, yres;
252 } tiff_decode_info;
254 typedef struct {
255 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
256 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
257 LONG ref;
258 TiffDecoder *parent;
259 UINT index;
260 tiff_decode_info decode_info;
261 INT cached_tile_x, cached_tile_y;
262 BYTE *cached_tile;
263 } TiffFrameDecode;
265 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl;
266 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl;
268 static inline TiffDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
270 return CONTAINING_RECORD(iface, TiffDecoder, IWICBitmapDecoder_iface);
273 static inline TiffFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
275 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICBitmapFrameDecode_iface);
278 static inline TiffFrameDecode *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
280 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICMetadataBlockReader_iface);
283 static HRESULT tiff_get_decode_info(TIFF *tiff, tiff_decode_info *decode_info)
285 uint16 photometric, bps, samples, planar;
286 uint16 extra_sample_count, extra_sample, *extra_samples;
287 int ret;
289 decode_info->indexed = 0;
290 decode_info->reverse_bgr = 0;
291 decode_info->invert_grayscale = 0;
292 decode_info->tiled = 0;
294 ret = pTIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
295 if (!ret)
297 WARN("missing PhotometricInterpretation tag\n");
298 return E_FAIL;
301 ret = pTIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bps);
302 if (!ret) bps = 1;
303 decode_info->bps = bps;
305 ret = pTIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samples);
306 if (!ret) samples = 1;
307 decode_info->samples = samples;
309 if (samples == 1)
310 planar = 1;
311 else
313 ret = pTIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planar);
314 if (!ret) planar = 1;
315 if (planar != 1)
317 FIXME("unhandled planar configuration %u\n", planar);
318 return E_FAIL;
321 decode_info->planar = planar;
323 switch(photometric)
325 case 0: /* WhiteIsZero */
326 decode_info->invert_grayscale = 1;
327 /* fall through */
328 case 1: /* BlackIsZero */
329 if (samples != 1)
331 FIXME("unhandled grayscale sample count %u\n", samples);
332 return E_FAIL;
335 decode_info->bpp = bps;
336 switch (bps)
338 case 1:
339 decode_info->format = &GUID_WICPixelFormatBlackWhite;
340 break;
341 case 4:
342 decode_info->format = &GUID_WICPixelFormat4bppGray;
343 break;
344 case 8:
345 decode_info->format = &GUID_WICPixelFormat8bppGray;
346 break;
347 default:
348 FIXME("unhandled greyscale bit count %u\n", bps);
349 return E_FAIL;
351 break;
352 case 2: /* RGB */
353 decode_info->bpp = bps * samples;
355 if (samples == 4)
357 ret = pTIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extra_sample_count, &extra_samples);
358 if (!ret)
360 extra_sample_count = 1;
361 extra_sample = 0;
362 extra_samples = &extra_sample;
365 else if (samples != 3)
367 FIXME("unhandled RGB sample count %u\n", samples);
368 return E_FAIL;
371 switch(bps)
373 case 8:
374 decode_info->reverse_bgr = 1;
375 if (samples == 3)
376 decode_info->format = &GUID_WICPixelFormat24bppBGR;
377 else
378 switch(extra_samples[0])
380 case 1: /* Associated (pre-multiplied) alpha data */
381 decode_info->format = &GUID_WICPixelFormat32bppPBGRA;
382 break;
383 case 0: /* Unspecified data */
384 case 2: /* Unassociated alpha data */
385 decode_info->format = &GUID_WICPixelFormat32bppBGRA;
386 break;
387 default:
388 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
389 return E_FAIL;
391 break;
392 case 16:
393 if (samples == 3)
394 decode_info->format = &GUID_WICPixelFormat48bppRGB;
395 else
396 switch(extra_samples[0])
398 case 1: /* Associated (pre-multiplied) alpha data */
399 decode_info->format = &GUID_WICPixelFormat64bppPRGBA;
400 break;
401 case 0: /* Unspecified data */
402 case 2: /* Unassociated alpha data */
403 decode_info->format = &GUID_WICPixelFormat64bppRGBA;
404 break;
405 default:
406 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
407 return E_FAIL;
409 break;
410 default:
411 FIXME("unhandled RGB bit count %u\n", bps);
412 return E_FAIL;
414 break;
415 case 3: /* RGB Palette */
416 if (samples != 1)
418 FIXME("unhandled indexed sample count %u\n", samples);
419 return E_FAIL;
422 decode_info->indexed = 1;
423 decode_info->bpp = bps;
424 switch (bps)
426 case 4:
427 decode_info->format = &GUID_WICPixelFormat4bppIndexed;
428 break;
429 case 8:
430 decode_info->format = &GUID_WICPixelFormat8bppIndexed;
431 break;
432 default:
433 FIXME("unhandled indexed bit count %u\n", bps);
434 return E_FAIL;
436 break;
437 case 4: /* Transparency mask */
438 case 5: /* CMYK */
439 case 6: /* YCbCr */
440 case 8: /* CIELab */
441 default:
442 FIXME("unhandled PhotometricInterpretation %u\n", photometric);
443 return E_FAIL;
446 ret = pTIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &decode_info->width);
447 if (!ret)
449 WARN("missing image width\n");
450 return E_FAIL;
453 ret = pTIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &decode_info->height);
454 if (!ret)
456 WARN("missing image length\n");
457 return E_FAIL;
460 if ((ret = pTIFFGetField(tiff, TIFFTAG_TILEWIDTH, &decode_info->tile_width)))
462 decode_info->tiled = 1;
464 ret = pTIFFGetField(tiff, TIFFTAG_TILELENGTH, &decode_info->tile_height);
465 if (!ret)
467 WARN("missing tile height\n");
468 return E_FAIL;
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 decode_info->tiles_across = (decode_info->width + decode_info->tile_width - 1) / decode_info->tile_width;
475 else if ((ret = pTIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &decode_info->tile_height)))
477 if (decode_info->tile_height > decode_info->height)
478 decode_info->tile_height = decode_info->height;
479 decode_info->tile_width = decode_info->width;
480 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
481 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
483 else
485 /* Some broken TIFF files have a single strip and lack the RowsPerStrip tag */
486 decode_info->tile_height = decode_info->height;
487 decode_info->tile_width = decode_info->width;
488 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
489 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
492 decode_info->resolution_unit = 0;
493 pTIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &decode_info->resolution_unit);
494 if (decode_info->resolution_unit != 0)
496 ret = pTIFFGetField(tiff, TIFFTAG_XRESOLUTION, &decode_info->xres);
497 if (!ret)
499 WARN("missing X resolution\n");
500 decode_info->resolution_unit = 0;
503 ret = pTIFFGetField(tiff, TIFFTAG_YRESOLUTION, &decode_info->yres);
504 if (!ret)
506 WARN("missing Y resolution\n");
507 decode_info->resolution_unit = 0;
511 return S_OK;
514 static HRESULT WINAPI TiffDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
515 void **ppv)
517 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
518 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
520 if (!ppv) return E_INVALIDARG;
522 if (IsEqualIID(&IID_IUnknown, iid) ||
523 IsEqualIID(&IID_IWICBitmapDecoder, iid))
525 *ppv = &This->IWICBitmapDecoder_iface;
527 else
529 *ppv = NULL;
530 return E_NOINTERFACE;
533 IUnknown_AddRef((IUnknown*)*ppv);
534 return S_OK;
537 static ULONG WINAPI TiffDecoder_AddRef(IWICBitmapDecoder *iface)
539 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
540 ULONG ref = InterlockedIncrement(&This->ref);
542 TRACE("(%p) refcount=%u\n", iface, ref);
544 return ref;
547 static ULONG WINAPI TiffDecoder_Release(IWICBitmapDecoder *iface)
549 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
550 ULONG ref = InterlockedDecrement(&This->ref);
552 TRACE("(%p) refcount=%u\n", iface, ref);
554 if (ref == 0)
556 if (This->tiff) pTIFFClose(This->tiff);
557 if (This->stream) IStream_Release(This->stream);
558 This->lock.DebugInfo->Spare[0] = 0;
559 DeleteCriticalSection(&This->lock);
560 HeapFree(GetProcessHeap(), 0, This);
563 return ref;
566 static HRESULT WINAPI TiffDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
567 DWORD *capability)
569 HRESULT hr;
571 TRACE("(%p,%p,%p)\n", iface, stream, capability);
573 if (!stream || !capability) return E_INVALIDARG;
575 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
576 if (hr != S_OK) return hr;
578 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
579 WICBitmapDecoderCapabilityCanDecodeSomeImages |
580 WICBitmapDecoderCapabilityCanEnumerateMetadata;
581 return S_OK;
584 static HRESULT WINAPI TiffDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
585 WICDecodeOptions cacheOptions)
587 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
588 TIFF *tiff;
589 HRESULT hr=S_OK;
591 TRACE("(%p,%p,%x): stub\n", iface, pIStream, cacheOptions);
593 EnterCriticalSection(&This->lock);
595 if (This->initialized)
597 hr = WINCODEC_ERR_WRONGSTATE;
598 goto exit;
601 tiff = tiff_open_stream(pIStream, "r");
603 if (!tiff)
605 hr = E_FAIL;
606 goto exit;
609 This->tiff = tiff;
610 This->stream = pIStream;
611 IStream_AddRef(pIStream);
612 This->initialized = TRUE;
614 exit:
615 LeaveCriticalSection(&This->lock);
616 return hr;
619 static HRESULT WINAPI TiffDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
620 GUID *pguidContainerFormat)
622 if (!pguidContainerFormat) return E_INVALIDARG;
624 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
625 return S_OK;
628 static HRESULT WINAPI TiffDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
629 IWICBitmapDecoderInfo **ppIDecoderInfo)
631 HRESULT hr;
632 IWICComponentInfo *compinfo;
634 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
636 hr = CreateComponentInfo(&CLSID_WICTiffDecoder, &compinfo);
637 if (FAILED(hr)) return hr;
639 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
640 (void**)ppIDecoderInfo);
642 IWICComponentInfo_Release(compinfo);
644 return hr;
647 static HRESULT WINAPI TiffDecoder_CopyPalette(IWICBitmapDecoder *iface,
648 IWICPalette *pIPalette)
650 FIXME("(%p,%p): stub\n", iface, pIPalette);
651 return E_NOTIMPL;
654 static HRESULT WINAPI TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
655 IWICMetadataQueryReader **ppIMetadataQueryReader)
657 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
658 return E_NOTIMPL;
661 static HRESULT WINAPI TiffDecoder_GetPreview(IWICBitmapDecoder *iface,
662 IWICBitmapSource **ppIBitmapSource)
664 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
666 if (!ppIBitmapSource) return E_INVALIDARG;
668 *ppIBitmapSource = NULL;
669 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
672 static HRESULT WINAPI TiffDecoder_GetColorContexts(IWICBitmapDecoder *iface,
673 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
675 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
676 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
679 static HRESULT WINAPI TiffDecoder_GetThumbnail(IWICBitmapDecoder *iface,
680 IWICBitmapSource **ppIThumbnail)
682 TRACE("(%p,%p)\n", iface, ppIThumbnail);
684 if (!ppIThumbnail) return E_INVALIDARG;
686 *ppIThumbnail = NULL;
687 return WINCODEC_ERR_CODECNOTHUMBNAIL;
690 static HRESULT WINAPI TiffDecoder_GetFrameCount(IWICBitmapDecoder *iface,
691 UINT *pCount)
693 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
695 if (!pCount) return E_INVALIDARG;
697 EnterCriticalSection(&This->lock);
698 *pCount = This->tiff ? pTIFFNumberOfDirectories(This->tiff) : 0;
699 LeaveCriticalSection(&This->lock);
701 TRACE("(%p) <-- %i\n", iface, *pCount);
703 return S_OK;
706 static HRESULT WINAPI TiffDecoder_GetFrame(IWICBitmapDecoder *iface,
707 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
709 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
710 TiffFrameDecode *result;
711 int res;
712 tiff_decode_info decode_info;
713 HRESULT hr;
715 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
717 if (!This->tiff)
718 return WINCODEC_ERR_FRAMEMISSING;
720 EnterCriticalSection(&This->lock);
721 res = pTIFFSetDirectory(This->tiff, index);
722 if (!res) hr = E_INVALIDARG;
723 else hr = tiff_get_decode_info(This->tiff, &decode_info);
724 LeaveCriticalSection(&This->lock);
726 if (SUCCEEDED(hr))
728 result = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode));
730 if (result)
732 result->IWICBitmapFrameDecode_iface.lpVtbl = &TiffFrameDecode_Vtbl;
733 result->IWICMetadataBlockReader_iface.lpVtbl = &TiffFrameDecode_BlockVtbl;
734 result->ref = 1;
735 result->parent = This;
736 result->index = index;
737 result->decode_info = decode_info;
738 result->cached_tile_x = -1;
739 result->cached_tile = HeapAlloc(GetProcessHeap(), 0, decode_info.tile_size);
741 if (result->cached_tile)
742 *ppIBitmapFrame = &result->IWICBitmapFrameDecode_iface;
743 else
745 hr = E_OUTOFMEMORY;
746 HeapFree(GetProcessHeap(), 0, result);
749 else hr = E_OUTOFMEMORY;
752 if (FAILED(hr)) *ppIBitmapFrame = NULL;
754 return hr;
757 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl = {
758 TiffDecoder_QueryInterface,
759 TiffDecoder_AddRef,
760 TiffDecoder_Release,
761 TiffDecoder_QueryCapability,
762 TiffDecoder_Initialize,
763 TiffDecoder_GetContainerFormat,
764 TiffDecoder_GetDecoderInfo,
765 TiffDecoder_CopyPalette,
766 TiffDecoder_GetMetadataQueryReader,
767 TiffDecoder_GetPreview,
768 TiffDecoder_GetColorContexts,
769 TiffDecoder_GetThumbnail,
770 TiffDecoder_GetFrameCount,
771 TiffDecoder_GetFrame
774 static HRESULT WINAPI TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
775 void **ppv)
777 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
778 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
780 if (!ppv) return E_INVALIDARG;
782 if (IsEqualIID(&IID_IUnknown, iid) ||
783 IsEqualIID(&IID_IWICBitmapSource, iid) ||
784 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
786 *ppv = &This->IWICBitmapFrameDecode_iface;
788 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
790 *ppv = &This->IWICMetadataBlockReader_iface;
792 else
794 *ppv = NULL;
795 return E_NOINTERFACE;
798 IUnknown_AddRef((IUnknown*)*ppv);
799 return S_OK;
802 static ULONG WINAPI TiffFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
804 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
805 ULONG ref = InterlockedIncrement(&This->ref);
807 TRACE("(%p) refcount=%u\n", iface, ref);
809 return ref;
812 static ULONG WINAPI TiffFrameDecode_Release(IWICBitmapFrameDecode *iface)
814 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
815 ULONG ref = InterlockedDecrement(&This->ref);
817 TRACE("(%p) refcount=%u\n", iface, ref);
819 if (ref == 0)
821 HeapFree(GetProcessHeap(), 0, This->cached_tile);
822 HeapFree(GetProcessHeap(), 0, This);
825 return ref;
828 static HRESULT WINAPI TiffFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
829 UINT *puiWidth, UINT *puiHeight)
831 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
833 *puiWidth = This->decode_info.width;
834 *puiHeight = This->decode_info.height;
836 TRACE("(%p) <-- %ux%u\n", iface, *puiWidth, *puiHeight);
838 return S_OK;
841 static HRESULT WINAPI TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
842 WICPixelFormatGUID *pPixelFormat)
844 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
846 memcpy(pPixelFormat, This->decode_info.format, sizeof(GUID));
848 TRACE("(%p) <-- %s\n", This, debugstr_guid(This->decode_info.format));
850 return S_OK;
853 static HRESULT WINAPI TiffFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
854 double *pDpiX, double *pDpiY)
856 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
858 switch (This->decode_info.resolution_unit)
860 default:
861 FIXME("unknown resolution unit %i\n", This->decode_info.resolution_unit);
862 /* fall through */
863 case 0: /* Not set */
864 *pDpiX = *pDpiY = 96.0;
865 break;
866 case 1: /* Relative measurements */
867 *pDpiX = 96.0;
868 *pDpiY = 96.0 * This->decode_info.yres / This->decode_info.xres;
869 break;
870 case 2: /* Inch */
871 *pDpiX = This->decode_info.xres;
872 *pDpiY = This->decode_info.yres;
873 break;
874 case 3: /* Centimeter */
875 *pDpiX = This->decode_info.xres / 2.54;
876 *pDpiY = This->decode_info.yres / 2.54;
877 break;
880 TRACE("(%p) <-- %f,%f unit=%i\n", iface, *pDpiX, *pDpiY, This->decode_info.resolution_unit);
882 return S_OK;
885 static HRESULT WINAPI TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
886 IWICPalette *pIPalette)
888 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
889 uint16 *red, *green, *blue;
890 WICColor colors[256];
891 int color_count, ret, i;
893 TRACE("(%p,%p)\n", iface, pIPalette);
895 color_count = 1<<This->decode_info.bps;
897 EnterCriticalSection(&This->parent->lock);
898 ret = pTIFFGetField(This->parent->tiff, TIFFTAG_COLORMAP, &red, &green, &blue);
899 LeaveCriticalSection(&This->parent->lock);
901 if (!ret)
903 WARN("Couldn't read color map\n");
904 return WINCODEC_ERR_PALETTEUNAVAILABLE;
907 for (i=0; i<color_count; i++)
909 colors[i] = 0xff000000 |
910 ((red[i]<<8) & 0xff0000) |
911 (green[i] & 0xff00) |
912 ((blue[i]>>8) & 0xff);
915 return IWICPalette_InitializeCustom(pIPalette, colors, color_count);
918 static HRESULT TiffFrameDecode_ReadTile(TiffFrameDecode *This, UINT tile_x, UINT tile_y)
920 HRESULT hr=S_OK;
921 tsize_t ret;
922 int swap_bytes;
924 swap_bytes = pTIFFIsByteSwapped(This->parent->tiff);
926 ret = pTIFFSetDirectory(This->parent->tiff, This->index);
928 if (ret == -1)
929 hr = E_FAIL;
931 if (hr == S_OK)
933 if (This->decode_info.tiled)
935 ret = pTIFFReadEncodedTile(This->parent->tiff, tile_x + tile_y * This->decode_info.tiles_across, This->cached_tile, This->decode_info.tile_size);
937 else
939 ret = pTIFFReadEncodedStrip(This->parent->tiff, tile_y, This->cached_tile, This->decode_info.tile_size);
942 if (ret == -1)
943 hr = E_FAIL;
946 if (hr == S_OK && This->decode_info.reverse_bgr)
948 if (This->decode_info.bps == 8)
950 UINT sample_count = This->decode_info.samples;
952 reverse_bgr8(sample_count, This->cached_tile, This->decode_info.tile_width,
953 This->decode_info.tile_height, This->decode_info.tile_width * sample_count);
957 if (hr == S_OK && swap_bytes && This->decode_info.bps > 8)
959 UINT row, i, samples_per_row;
960 BYTE *sample, temp;
962 samples_per_row = This->decode_info.tile_width * This->decode_info.samples;
964 switch(This->decode_info.bps)
966 case 16:
967 for (row=0; row<This->decode_info.tile_height; row++)
969 sample = This->cached_tile + row * This->decode_info.tile_stride;
970 for (i=0; i<samples_per_row; i++)
972 temp = sample[1];
973 sample[1] = sample[0];
974 sample[0] = temp;
975 sample += 2;
978 break;
979 default:
980 ERR("unhandled bps for byte swap %u\n", This->decode_info.bps);
981 return E_FAIL;
985 if (hr == S_OK && This->decode_info.invert_grayscale)
987 BYTE *byte, *end;
989 if (This->decode_info.samples != 1)
991 ERR("cannot invert grayscale image with %u samples\n", This->decode_info.samples);
992 return E_FAIL;
995 end = This->cached_tile+This->decode_info.tile_size;
997 for (byte = This->cached_tile; byte != end; byte++)
998 *byte = ~(*byte);
1001 if (hr == S_OK)
1003 This->cached_tile_x = tile_x;
1004 This->cached_tile_y = tile_y;
1007 return hr;
1010 static HRESULT WINAPI TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
1011 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
1013 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
1014 UINT min_tile_x, max_tile_x, min_tile_y, max_tile_y;
1015 UINT tile_x, tile_y;
1016 WICRect rc;
1017 HRESULT hr=S_OK;
1018 BYTE *dst_tilepos;
1019 UINT bytesperrow;
1020 WICRect rect;
1022 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
1024 if (!prc)
1026 rect.X = 0;
1027 rect.Y = 0;
1028 rect.Width = This->decode_info.width;
1029 rect.Height = This->decode_info.height;
1030 prc = &rect;
1032 else
1034 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->decode_info.width ||
1035 prc->Y+prc->Height > This->decode_info.height)
1036 return E_INVALIDARG;
1039 bytesperrow = ((This->decode_info.bpp * prc->Width)+7)/8;
1041 if (cbStride < bytesperrow)
1042 return E_INVALIDARG;
1044 if ((cbStride * prc->Height) > cbBufferSize)
1045 return E_INVALIDARG;
1047 min_tile_x = prc->X / This->decode_info.tile_width;
1048 min_tile_y = prc->Y / This->decode_info.tile_height;
1049 max_tile_x = (prc->X+prc->Width-1) / This->decode_info.tile_width;
1050 max_tile_y = (prc->Y+prc->Height-1) / This->decode_info.tile_height;
1052 EnterCriticalSection(&This->parent->lock);
1054 for (tile_x=min_tile_x; tile_x <= max_tile_x; tile_x++)
1056 for (tile_y=min_tile_y; tile_y <= max_tile_y; tile_y++)
1058 if (tile_x != This->cached_tile_x || tile_y != This->cached_tile_y)
1060 hr = TiffFrameDecode_ReadTile(This, tile_x, tile_y);
1063 if (SUCCEEDED(hr))
1065 if (prc->X < tile_x * This->decode_info.tile_width)
1066 rc.X = 0;
1067 else
1068 rc.X = prc->X - tile_x * This->decode_info.tile_width;
1070 if (prc->Y < tile_y * This->decode_info.tile_height)
1071 rc.Y = 0;
1072 else
1073 rc.Y = prc->Y - tile_y * This->decode_info.tile_height;
1075 if (prc->X+prc->Width > (tile_x+1) * This->decode_info.tile_width)
1076 rc.Width = This->decode_info.tile_width - rc.X;
1077 else if (prc->X < tile_x * This->decode_info.tile_width)
1078 rc.Width = prc->Width + prc->X - tile_x * This->decode_info.tile_width;
1079 else
1080 rc.Width = prc->Width;
1082 if (prc->Y+prc->Height > (tile_y+1) * This->decode_info.tile_height)
1083 rc.Height = This->decode_info.tile_height - rc.Y;
1084 else if (prc->Y < tile_y * This->decode_info.tile_height)
1085 rc.Height = prc->Height + prc->Y - tile_y * This->decode_info.tile_height;
1086 else
1087 rc.Height = prc->Height;
1089 dst_tilepos = pbBuffer + (cbStride * ((rc.Y + tile_y * This->decode_info.tile_height) - prc->Y)) +
1090 ((This->decode_info.bpp * ((rc.X + tile_x * This->decode_info.tile_width) - prc->X) + 7) / 8);
1092 hr = copy_pixels(This->decode_info.bpp, This->cached_tile,
1093 This->decode_info.tile_width, This->decode_info.tile_height, This->decode_info.tile_stride,
1094 &rc, cbStride, cbBufferSize, dst_tilepos);
1097 if (FAILED(hr))
1099 LeaveCriticalSection(&This->parent->lock);
1100 TRACE("<-- 0x%x\n", hr);
1101 return hr;
1106 LeaveCriticalSection(&This->parent->lock);
1108 return S_OK;
1111 static HRESULT WINAPI TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
1112 IWICMetadataQueryReader **ppIMetadataQueryReader)
1114 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
1115 return E_NOTIMPL;
1118 static HRESULT WINAPI TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
1119 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1121 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
1122 const BYTE *profile;
1123 UINT len;
1124 HRESULT hr;
1126 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
1128 EnterCriticalSection(&This->parent->lock);
1130 if (pTIFFGetField(This->parent->tiff, TIFFTAG_ICCPROFILE, &len, &profile))
1132 if (cCount && ppIColorContexts)
1134 hr = IWICColorContext_InitializeFromMemory(*ppIColorContexts, profile, len);
1135 if (FAILED(hr))
1137 LeaveCriticalSection(&This->parent->lock);
1138 return hr;
1141 *pcActualCount = 1;
1143 else
1144 *pcActualCount = 0;
1146 LeaveCriticalSection(&This->parent->lock);
1148 return S_OK;
1151 static HRESULT WINAPI TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
1152 IWICBitmapSource **ppIThumbnail)
1154 TRACE("(%p,%p)\n", iface, ppIThumbnail);
1156 if (!ppIThumbnail) return E_INVALIDARG;
1158 *ppIThumbnail = NULL;
1159 return WINCODEC_ERR_CODECNOTHUMBNAIL;
1162 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl = {
1163 TiffFrameDecode_QueryInterface,
1164 TiffFrameDecode_AddRef,
1165 TiffFrameDecode_Release,
1166 TiffFrameDecode_GetSize,
1167 TiffFrameDecode_GetPixelFormat,
1168 TiffFrameDecode_GetResolution,
1169 TiffFrameDecode_CopyPalette,
1170 TiffFrameDecode_CopyPixels,
1171 TiffFrameDecode_GetMetadataQueryReader,
1172 TiffFrameDecode_GetColorContexts,
1173 TiffFrameDecode_GetThumbnail
1176 static HRESULT WINAPI TiffFrameDecode_Block_QueryInterface(IWICMetadataBlockReader *iface,
1177 REFIID iid, void **ppv)
1179 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1180 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
1183 static ULONG WINAPI TiffFrameDecode_Block_AddRef(IWICMetadataBlockReader *iface)
1185 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1186 return IWICBitmapFrameDecode_AddRef(&This->IWICBitmapFrameDecode_iface);
1189 static ULONG WINAPI TiffFrameDecode_Block_Release(IWICMetadataBlockReader *iface)
1191 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1192 return IWICBitmapFrameDecode_Release(&This->IWICBitmapFrameDecode_iface);
1195 static HRESULT WINAPI TiffFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
1196 GUID *guid)
1198 TRACE("(%p,%p)\n", iface, guid);
1200 if (!guid) return E_INVALIDARG;
1202 *guid = GUID_ContainerFormatTiff;
1203 return S_OK;
1206 static HRESULT WINAPI TiffFrameDecode_Block_GetCount(IWICMetadataBlockReader *iface,
1207 UINT *count)
1209 TRACE("%p,%p\n", iface, count);
1211 if (!count) return E_INVALIDARG;
1213 *count = 1;
1214 return S_OK;
1217 static HRESULT create_metadata_reader(TiffFrameDecode *This, IWICMetadataReader **reader)
1219 HRESULT hr;
1220 LARGE_INTEGER dir_offset;
1221 IWICMetadataReader *metadata_reader;
1222 IWICPersistStream *persist;
1224 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
1226 hr = CoCreateInstance(&CLSID_WICIfdMetadataReader, NULL, CLSCTX_INPROC_SERVER,
1227 &IID_IWICMetadataReader, (void **)&metadata_reader);
1228 if (FAILED(hr)) return hr;
1230 hr = IWICMetadataReader_QueryInterface(metadata_reader, &IID_IWICPersistStream, (void **)&persist);
1231 if (FAILED(hr))
1233 IWICMetadataReader_Release(metadata_reader);
1234 return hr;
1237 EnterCriticalSection(&This->parent->lock);
1239 dir_offset.QuadPart = pTIFFCurrentDirOffset(This->parent->tiff);
1240 hr = IStream_Seek(This->parent->stream, dir_offset, STREAM_SEEK_SET, NULL);
1241 if (SUCCEEDED(hr))
1243 BOOL byte_swapped = pTIFFIsByteSwapped(This->parent->tiff);
1244 #ifdef WORDS_BIGENDIAN
1245 DWORD persist_options = byte_swapped ? WICPersistOptionsLittleEndian : WICPersistOptionsBigEndian;
1246 #else
1247 DWORD persist_options = byte_swapped ? WICPersistOptionsBigEndian : WICPersistOptionsLittleEndian;
1248 #endif
1249 persist_options |= WICPersistOptionsNoCacheStream;
1250 hr = IWICPersistStream_LoadEx(persist, This->parent->stream, NULL, persist_options);
1251 if (FAILED(hr))
1252 ERR("IWICPersistStream_LoadEx error %#x\n", hr);
1255 LeaveCriticalSection(&This->parent->lock);
1257 IWICPersistStream_Release(persist);
1259 if (FAILED(hr))
1261 IWICMetadataReader_Release(metadata_reader);
1262 return hr;
1265 *reader = metadata_reader;
1266 return S_OK;
1269 static HRESULT WINAPI TiffFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
1270 UINT index, IWICMetadataReader **reader)
1272 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1274 TRACE("(%p,%u,%p)\n", iface, index, reader);
1276 if (!reader || index != 0) return E_INVALIDARG;
1278 return create_metadata_reader(This, reader);
1281 static HRESULT WINAPI TiffFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader *iface,
1282 IEnumUnknown **enum_metadata)
1284 FIXME("(%p,%p): stub\n", iface, enum_metadata);
1285 return E_NOTIMPL;
1288 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl =
1290 TiffFrameDecode_Block_QueryInterface,
1291 TiffFrameDecode_Block_AddRef,
1292 TiffFrameDecode_Block_Release,
1293 TiffFrameDecode_Block_GetContainerFormat,
1294 TiffFrameDecode_Block_GetCount,
1295 TiffFrameDecode_Block_GetReaderByIndex,
1296 TiffFrameDecode_Block_GetEnumerator
1299 HRESULT TiffDecoder_CreateInstance(REFIID iid, void** ppv)
1301 HRESULT ret;
1302 TiffDecoder *This;
1304 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1306 *ppv = NULL;
1308 if (!load_libtiff())
1310 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF);
1311 return E_FAIL;
1314 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder));
1315 if (!This) return E_OUTOFMEMORY;
1317 This->IWICBitmapDecoder_iface.lpVtbl = &TiffDecoder_Vtbl;
1318 This->ref = 1;
1319 This->stream = NULL;
1320 InitializeCriticalSection(&This->lock);
1321 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffDecoder.lock");
1322 This->tiff = NULL;
1323 This->initialized = FALSE;
1325 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1326 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1328 return ret;
1331 struct tiff_encode_format {
1332 const WICPixelFormatGUID *guid;
1333 int photometric;
1334 int bps;
1335 int samples;
1336 int bpp;
1337 int extra_sample;
1338 int extra_sample_type;
1339 int reverse_bgr;
1342 static const struct tiff_encode_format formats[] = {
1343 {&GUID_WICPixelFormat24bppBGR, 2, 8, 3, 24, 0, 0, 1},
1344 {&GUID_WICPixelFormat24bppRGB, 2, 8, 3, 24, 0, 0, 0},
1345 {&GUID_WICPixelFormatBlackWhite, 1, 1, 1, 1, 0, 0, 0},
1346 {&GUID_WICPixelFormat4bppGray, 1, 4, 1, 4, 0, 0, 0},
1347 {&GUID_WICPixelFormat8bppGray, 1, 8, 1, 8, 0, 0, 0},
1348 {&GUID_WICPixelFormat32bppBGRA, 2, 8, 4, 32, 1, 2, 1},
1349 {&GUID_WICPixelFormat32bppPBGRA, 2, 8, 4, 32, 1, 1, 1},
1350 {&GUID_WICPixelFormat48bppRGB, 2, 16, 3, 48, 0, 0, 0},
1351 {&GUID_WICPixelFormat64bppRGBA, 2, 16, 4, 64, 1, 2, 0},
1352 {&GUID_WICPixelFormat64bppPRGBA, 2, 16, 4, 64, 1, 1, 0},
1356 typedef struct TiffEncoder {
1357 IWICBitmapEncoder IWICBitmapEncoder_iface;
1358 LONG ref;
1359 IStream *stream;
1360 CRITICAL_SECTION lock; /* Must be held when tiff is used or fields below are set */
1361 TIFF *tiff;
1362 BOOL initialized;
1363 BOOL committed;
1364 ULONG num_frames;
1365 ULONG num_frames_committed;
1366 } TiffEncoder;
1368 static inline TiffEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1370 return CONTAINING_RECORD(iface, TiffEncoder, IWICBitmapEncoder_iface);
1373 typedef struct TiffFrameEncode {
1374 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1375 LONG ref;
1376 TiffEncoder *parent;
1377 /* fields below are protected by parent->lock */
1378 BOOL initialized;
1379 BOOL info_written;
1380 BOOL committed;
1381 const struct tiff_encode_format *format;
1382 UINT width, height;
1383 double xres, yres;
1384 UINT lines_written;
1385 } TiffFrameEncode;
1387 static inline TiffFrameEncode *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1389 return CONTAINING_RECORD(iface, TiffFrameEncode, IWICBitmapFrameEncode_iface);
1392 static HRESULT WINAPI TiffFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1393 void **ppv)
1395 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1396 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1398 if (!ppv) return E_INVALIDARG;
1400 if (IsEqualIID(&IID_IUnknown, iid) ||
1401 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1403 *ppv = &This->IWICBitmapFrameEncode_iface;
1405 else
1407 *ppv = NULL;
1408 return E_NOINTERFACE;
1411 IUnknown_AddRef((IUnknown*)*ppv);
1412 return S_OK;
1415 static ULONG WINAPI TiffFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1417 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1418 ULONG ref = InterlockedIncrement(&This->ref);
1420 TRACE("(%p) refcount=%u\n", iface, ref);
1422 return ref;
1425 static ULONG WINAPI TiffFrameEncode_Release(IWICBitmapFrameEncode *iface)
1427 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1428 ULONG ref = InterlockedDecrement(&This->ref);
1430 TRACE("(%p) refcount=%u\n", iface, ref);
1432 if (ref == 0)
1434 IWICBitmapEncoder_Release(&This->parent->IWICBitmapEncoder_iface);
1435 HeapFree(GetProcessHeap(), 0, This);
1438 return ref;
1441 static HRESULT WINAPI TiffFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1442 IPropertyBag2 *pIEncoderOptions)
1444 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1445 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1447 EnterCriticalSection(&This->parent->lock);
1449 if (This->initialized)
1451 LeaveCriticalSection(&This->parent->lock);
1452 return WINCODEC_ERR_WRONGSTATE;
1455 This->initialized = TRUE;
1457 LeaveCriticalSection(&This->parent->lock);
1459 return S_OK;
1462 static HRESULT WINAPI TiffFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1463 UINT uiWidth, UINT uiHeight)
1465 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1466 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1468 EnterCriticalSection(&This->parent->lock);
1470 if (!This->initialized || This->info_written)
1472 LeaveCriticalSection(&This->parent->lock);
1473 return WINCODEC_ERR_WRONGSTATE;
1476 This->width = uiWidth;
1477 This->height = uiHeight;
1479 LeaveCriticalSection(&This->parent->lock);
1481 return S_OK;
1484 static HRESULT WINAPI TiffFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1485 double dpiX, double dpiY)
1487 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1488 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1490 EnterCriticalSection(&This->parent->lock);
1492 if (!This->initialized || This->info_written)
1494 LeaveCriticalSection(&This->parent->lock);
1495 return WINCODEC_ERR_WRONGSTATE;
1498 This->xres = dpiX;
1499 This->yres = dpiY;
1501 LeaveCriticalSection(&This->parent->lock);
1503 return S_OK;
1506 static HRESULT WINAPI TiffFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1507 WICPixelFormatGUID *pPixelFormat)
1509 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1510 int i;
1512 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1514 EnterCriticalSection(&This->parent->lock);
1516 if (!This->initialized || This->info_written)
1518 LeaveCriticalSection(&This->parent->lock);
1519 return WINCODEC_ERR_WRONGSTATE;
1522 for (i=0; formats[i].guid; i++)
1524 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1525 break;
1528 if (!formats[i].guid) i = 0;
1530 This->format = &formats[i];
1531 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1533 LeaveCriticalSection(&This->parent->lock);
1535 return S_OK;
1538 static HRESULT WINAPI TiffFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1539 UINT cCount, IWICColorContext **ppIColorContext)
1541 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1542 return E_NOTIMPL;
1545 static HRESULT WINAPI TiffFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1546 IWICPalette *pIPalette)
1548 FIXME("(%p,%p): stub\n", iface, pIPalette);
1549 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1552 static HRESULT WINAPI TiffFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1553 IWICBitmapSource *pIThumbnail)
1555 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1556 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1559 static HRESULT WINAPI TiffFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1560 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1562 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1563 BYTE *row_data, *swapped_data = NULL;
1564 UINT i, j, line_size;
1566 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1568 EnterCriticalSection(&This->parent->lock);
1570 if (!This->initialized || !This->width || !This->height || !This->format)
1572 LeaveCriticalSection(&This->parent->lock);
1573 return WINCODEC_ERR_WRONGSTATE;
1576 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1578 LeaveCriticalSection(&This->parent->lock);
1579 return E_INVALIDARG;
1582 line_size = ((This->width * This->format->bpp)+7)/8;
1584 if (This->format->reverse_bgr)
1586 swapped_data = HeapAlloc(GetProcessHeap(), 0, line_size);
1587 if (!swapped_data)
1589 LeaveCriticalSection(&This->parent->lock);
1590 return E_OUTOFMEMORY;
1594 if (!This->info_written)
1596 pTIFFSetField(This->parent->tiff, TIFFTAG_PHOTOMETRIC, (uint16)This->format->photometric);
1597 pTIFFSetField(This->parent->tiff, TIFFTAG_PLANARCONFIG, (uint16)1);
1598 pTIFFSetField(This->parent->tiff, TIFFTAG_BITSPERSAMPLE, (uint16)This->format->bps);
1599 pTIFFSetField(This->parent->tiff, TIFFTAG_SAMPLESPERPIXEL, (uint16)This->format->samples);
1601 if (This->format->extra_sample)
1603 uint16 extra_samples;
1604 extra_samples = This->format->extra_sample_type;
1606 pTIFFSetField(This->parent->tiff, TIFFTAG_EXTRASAMPLES, (uint16)1, &extra_samples);
1609 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGEWIDTH, (uint32)This->width);
1610 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGELENGTH, (uint32)This->height);
1612 if (This->xres != 0.0 && This->yres != 0.0)
1614 pTIFFSetField(This->parent->tiff, TIFFTAG_RESOLUTIONUNIT, (uint16)2); /* Inch */
1615 pTIFFSetField(This->parent->tiff, TIFFTAG_XRESOLUTION, (float)This->xres);
1616 pTIFFSetField(This->parent->tiff, TIFFTAG_YRESOLUTION, (float)This->yres);
1619 This->info_written = TRUE;
1622 for (i=0; i<lineCount; i++)
1624 row_data = pbPixels + i * cbStride;
1626 if (This->format->reverse_bgr && This->format->bps == 8)
1628 memcpy(swapped_data, row_data, line_size);
1629 for (j=0; j<line_size; j += This->format->samples)
1631 BYTE temp;
1632 temp = swapped_data[j];
1633 swapped_data[j] = swapped_data[j+2];
1634 swapped_data[j+2] = temp;
1636 row_data = swapped_data;
1639 pTIFFWriteScanline(This->parent->tiff, (tdata_t)row_data, i+This->lines_written, 0);
1642 This->lines_written += lineCount;
1644 LeaveCriticalSection(&This->parent->lock);
1646 HeapFree(GetProcessHeap(), 0, swapped_data);
1648 return S_OK;
1651 static HRESULT WINAPI TiffFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1652 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1654 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1655 HRESULT hr;
1657 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1659 if (!This->initialized)
1660 return WINCODEC_ERR_WRONGSTATE;
1662 hr = configure_write_source(iface, pIBitmapSource, prc,
1663 This->format ? This->format->guid : NULL, This->width, This->height,
1664 This->xres, This->yres);
1666 if (SUCCEEDED(hr))
1668 hr = write_source(iface, pIBitmapSource, prc,
1669 This->format->guid, This->format->bpp, This->width, This->height);
1672 return hr;
1675 static HRESULT WINAPI TiffFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1677 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1679 TRACE("(%p)\n", iface);
1681 EnterCriticalSection(&This->parent->lock);
1683 if (!This->info_written || This->lines_written != This->height || This->committed)
1685 LeaveCriticalSection(&This->parent->lock);
1686 return WINCODEC_ERR_WRONGSTATE;
1689 /* libtiff will commit the data when creating a new frame or closing the file */
1691 This->committed = TRUE;
1692 This->parent->num_frames_committed++;
1694 LeaveCriticalSection(&This->parent->lock);
1696 return S_OK;
1699 static HRESULT WINAPI TiffFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1700 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1702 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1703 return E_NOTIMPL;
1706 static const IWICBitmapFrameEncodeVtbl TiffFrameEncode_Vtbl = {
1707 TiffFrameEncode_QueryInterface,
1708 TiffFrameEncode_AddRef,
1709 TiffFrameEncode_Release,
1710 TiffFrameEncode_Initialize,
1711 TiffFrameEncode_SetSize,
1712 TiffFrameEncode_SetResolution,
1713 TiffFrameEncode_SetPixelFormat,
1714 TiffFrameEncode_SetColorContexts,
1715 TiffFrameEncode_SetPalette,
1716 TiffFrameEncode_SetThumbnail,
1717 TiffFrameEncode_WritePixels,
1718 TiffFrameEncode_WriteSource,
1719 TiffFrameEncode_Commit,
1720 TiffFrameEncode_GetMetadataQueryWriter
1723 static HRESULT WINAPI TiffEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1724 void **ppv)
1726 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1727 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1729 if (!ppv) return E_INVALIDARG;
1731 if (IsEqualIID(&IID_IUnknown, iid) ||
1732 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1734 *ppv = &This->IWICBitmapEncoder_iface;
1736 else
1738 *ppv = NULL;
1739 return E_NOINTERFACE;
1742 IUnknown_AddRef((IUnknown*)*ppv);
1743 return S_OK;
1746 static ULONG WINAPI TiffEncoder_AddRef(IWICBitmapEncoder *iface)
1748 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1749 ULONG ref = InterlockedIncrement(&This->ref);
1751 TRACE("(%p) refcount=%u\n", iface, ref);
1753 return ref;
1756 static ULONG WINAPI TiffEncoder_Release(IWICBitmapEncoder *iface)
1758 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1759 ULONG ref = InterlockedDecrement(&This->ref);
1761 TRACE("(%p) refcount=%u\n", iface, ref);
1763 if (ref == 0)
1765 if (This->tiff) pTIFFClose(This->tiff);
1766 if (This->stream) IStream_Release(This->stream);
1767 This->lock.DebugInfo->Spare[0] = 0;
1768 DeleteCriticalSection(&This->lock);
1769 HeapFree(GetProcessHeap(), 0, This);
1772 return ref;
1775 static HRESULT WINAPI TiffEncoder_Initialize(IWICBitmapEncoder *iface,
1776 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1778 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1779 TIFF *tiff;
1780 HRESULT hr=S_OK;
1782 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1784 EnterCriticalSection(&This->lock);
1786 if (This->initialized || This->committed)
1788 hr = WINCODEC_ERR_WRONGSTATE;
1789 goto exit;
1792 tiff = tiff_open_stream(pIStream, "w");
1794 if (!tiff)
1796 hr = E_FAIL;
1797 goto exit;
1800 This->tiff = tiff;
1801 This->stream = pIStream;
1802 IStream_AddRef(pIStream);
1803 This->initialized = TRUE;
1805 exit:
1806 LeaveCriticalSection(&This->lock);
1807 return hr;
1810 static HRESULT WINAPI TiffEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1811 GUID *pguidContainerFormat)
1813 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
1814 return S_OK;
1817 static HRESULT WINAPI TiffEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1818 IWICBitmapEncoderInfo **ppIEncoderInfo)
1820 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1821 return E_NOTIMPL;
1824 static HRESULT WINAPI TiffEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1825 UINT cCount, IWICColorContext **ppIColorContext)
1827 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1828 return E_NOTIMPL;
1831 static HRESULT WINAPI TiffEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1833 TRACE("(%p,%p)\n", iface, pIPalette);
1834 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1837 static HRESULT WINAPI TiffEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1839 TRACE("(%p,%p)\n", iface, pIThumbnail);
1840 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1843 static HRESULT WINAPI TiffEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1845 TRACE("(%p,%p)\n", iface, pIPreview);
1846 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1849 static HRESULT WINAPI TiffEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1850 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1852 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1853 TiffFrameEncode *result;
1855 HRESULT hr=S_OK;
1857 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1859 EnterCriticalSection(&This->lock);
1861 if (!This->initialized || This->committed)
1863 hr = WINCODEC_ERR_WRONGSTATE;
1865 else if (This->num_frames != This->num_frames_committed)
1867 FIXME("New frame created before previous frame was committed\n");
1868 hr = E_FAIL;
1871 if (SUCCEEDED(hr))
1873 PROPBAG2 opts[2]= {{0}};
1874 opts[0].pstrName = (LPOLESTR)wszTiffCompressionMethod;
1875 opts[0].vt = VT_UI1;
1876 opts[0].dwType = PROPBAG2_TYPE_DATA;
1878 opts[1].pstrName = (LPOLESTR)wszCompressionQuality;
1879 opts[1].vt = VT_R4;
1880 opts[1].dwType = PROPBAG2_TYPE_DATA;
1882 hr = CreatePropertyBag2(opts, 2, ppIEncoderOptions);
1884 if (SUCCEEDED(hr))
1886 VARIANT v;
1887 VariantInit(&v);
1888 V_VT(&v) = VT_UI1;
1889 V_UNION(&v, bVal) = WICTiffCompressionDontCare;
1890 hr = IPropertyBag2_Write(*ppIEncoderOptions, 1, opts, &v);
1891 VariantClear(&v);
1892 if (FAILED(hr))
1894 IPropertyBag2_Release(*ppIEncoderOptions);
1895 *ppIEncoderOptions = NULL;
1900 if (SUCCEEDED(hr))
1902 result = HeapAlloc(GetProcessHeap(), 0, sizeof(*result));
1904 if (result)
1906 result->IWICBitmapFrameEncode_iface.lpVtbl = &TiffFrameEncode_Vtbl;
1907 result->ref = 1;
1908 result->parent = This;
1909 result->initialized = FALSE;
1910 result->info_written = FALSE;
1911 result->committed = FALSE;
1912 result->format = NULL;
1913 result->width = 0;
1914 result->height = 0;
1915 result->xres = 0.0;
1916 result->yres = 0.0;
1917 result->lines_written = 0;
1919 IWICBitmapEncoder_AddRef(iface);
1920 *ppIFrameEncode = &result->IWICBitmapFrameEncode_iface;
1922 if (This->num_frames != 0)
1923 pTIFFWriteDirectory(This->tiff);
1925 This->num_frames++;
1927 else
1928 hr = E_OUTOFMEMORY;
1930 if (FAILED(hr))
1932 IPropertyBag2_Release(*ppIEncoderOptions);
1933 *ppIEncoderOptions = NULL;
1937 LeaveCriticalSection(&This->lock);
1939 return hr;
1942 static HRESULT WINAPI TiffEncoder_Commit(IWICBitmapEncoder *iface)
1944 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1946 TRACE("(%p)\n", iface);
1948 EnterCriticalSection(&This->lock);
1950 if (!This->initialized || This->committed)
1952 LeaveCriticalSection(&This->lock);
1953 return WINCODEC_ERR_WRONGSTATE;
1956 pTIFFClose(This->tiff);
1957 IStream_Release(This->stream);
1958 This->stream = NULL;
1959 This->tiff = NULL;
1961 This->committed = TRUE;
1963 LeaveCriticalSection(&This->lock);
1965 return S_OK;
1968 static HRESULT WINAPI TiffEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1969 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1971 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1972 return E_NOTIMPL;
1975 static const IWICBitmapEncoderVtbl TiffEncoder_Vtbl = {
1976 TiffEncoder_QueryInterface,
1977 TiffEncoder_AddRef,
1978 TiffEncoder_Release,
1979 TiffEncoder_Initialize,
1980 TiffEncoder_GetContainerFormat,
1981 TiffEncoder_GetEncoderInfo,
1982 TiffEncoder_SetColorContexts,
1983 TiffEncoder_SetPalette,
1984 TiffEncoder_SetThumbnail,
1985 TiffEncoder_SetPreview,
1986 TiffEncoder_CreateNewFrame,
1987 TiffEncoder_Commit,
1988 TiffEncoder_GetMetadataQueryWriter
1991 HRESULT TiffEncoder_CreateInstance(REFIID iid, void** ppv)
1993 TiffEncoder *This;
1994 HRESULT ret;
1996 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1998 *ppv = NULL;
2000 if (!load_libtiff())
2002 ERR("Failed writing TIFF because unable to load %s\n",SONAME_LIBTIFF);
2003 return E_FAIL;
2006 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffEncoder));
2007 if (!This) return E_OUTOFMEMORY;
2009 This->IWICBitmapEncoder_iface.lpVtbl = &TiffEncoder_Vtbl;
2010 This->ref = 1;
2011 This->stream = NULL;
2012 InitializeCriticalSection(&This->lock);
2013 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffEncoder.lock");
2014 This->tiff = NULL;
2015 This->initialized = FALSE;
2016 This->num_frames = 0;
2017 This->num_frames_committed = 0;
2018 This->committed = FALSE;
2020 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
2021 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
2023 return ret;
2026 #else /* !SONAME_LIBTIFF */
2028 HRESULT TiffDecoder_CreateInstance(REFIID iid, void** ppv)
2030 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
2031 return E_FAIL;
2034 HRESULT TiffEncoder_CreateInstance(REFIID iid, void** ppv)
2036 ERR("Trying to save TIFF picture, but Wine was compiled without TIFF support.\n");
2037 return E_FAIL;
2040 #endif