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
20 #include "wine/port.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);
47 static CRITICAL_SECTION init_tiff_cs;
48 static CRITICAL_SECTION_DEBUG init_tiff_cs_debug =
51 { &init_tiff_cs_debug.ProcessLocksList,
52 &init_tiff_cs_debug.ProcessLocksList },
53 0, 0, { (DWORD_PTR)(__FILE__ ": init_tiff_cs") }
55 static CRITICAL_SECTION init_tiff_cs = { &init_tiff_cs_debug, -1, 0, 0, 0, 0 };
57 static void *libtiff_handle;
58 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
59 MAKE_FUNCPTR(TIFFClientOpen);
60 MAKE_FUNCPTR(TIFFClose);
61 MAKE_FUNCPTR(TIFFNumberOfDirectories);
62 MAKE_FUNCPTR(TIFFGetField);
63 MAKE_FUNCPTR(TIFFIsByteSwapped);
64 MAKE_FUNCPTR(TIFFReadDirectory);
65 MAKE_FUNCPTR(TIFFReadEncodedStrip);
66 MAKE_FUNCPTR(TIFFReadEncodedTile);
67 MAKE_FUNCPTR(TIFFSetDirectory);
68 MAKE_FUNCPTR(TIFFSetField);
69 MAKE_FUNCPTR(TIFFWriteDirectory);
70 MAKE_FUNCPTR(TIFFWriteScanline);
73 static void *load_libtiff(void)
77 EnterCriticalSection(&init_tiff_cs);
79 if (!libtiff_handle &&
80 (libtiff_handle = wine_dlopen(SONAME_LIBTIFF, RTLD_NOW, NULL, 0)) != NULL)
83 #define LOAD_FUNCPTR(f) \
84 if((p##f = wine_dlsym(libtiff_handle, #f, NULL, 0)) == NULL) { \
85 ERR("failed to load symbol %s\n", #f); \
86 libtiff_handle = NULL; \
87 LeaveCriticalSection(&init_tiff_cs); \
90 LOAD_FUNCPTR(TIFFClientOpen);
91 LOAD_FUNCPTR(TIFFClose);
92 LOAD_FUNCPTR(TIFFNumberOfDirectories);
93 LOAD_FUNCPTR(TIFFGetField);
94 LOAD_FUNCPTR(TIFFIsByteSwapped);
95 LOAD_FUNCPTR(TIFFReadDirectory);
96 LOAD_FUNCPTR(TIFFReadEncodedStrip);
97 LOAD_FUNCPTR(TIFFReadEncodedTile);
98 LOAD_FUNCPTR(TIFFSetDirectory);
99 LOAD_FUNCPTR(TIFFSetField);
100 LOAD_FUNCPTR(TIFFWriteDirectory);
101 LOAD_FUNCPTR(TIFFWriteScanline);
106 result = libtiff_handle;
108 LeaveCriticalSection(&init_tiff_cs);
112 static tsize_t tiff_stream_read(thandle_t client_data, tdata_t data, tsize_t size)
114 IStream *stream = (IStream*)client_data;
118 hr = IStream_Read(stream, data, size, &bytes_read);
119 if (FAILED(hr)) bytes_read = 0;
123 static tsize_t tiff_stream_write(thandle_t client_data, tdata_t data, tsize_t size)
125 IStream *stream = (IStream*)client_data;
129 hr = IStream_Write(stream, data, size, &bytes_written);
130 if (FAILED(hr)) bytes_written = 0;
131 return bytes_written;
134 static toff_t tiff_stream_seek(thandle_t client_data, toff_t offset, int whence)
136 IStream *stream = (IStream*)client_data;
139 ULARGE_INTEGER new_position;
142 move.QuadPart = offset;
146 origin = STREAM_SEEK_SET;
149 origin = STREAM_SEEK_CUR;
152 origin = STREAM_SEEK_END;
155 ERR("unknown whence value %i\n", whence);
159 hr = IStream_Seek(stream, move, origin, &new_position);
160 if (SUCCEEDED(hr)) return new_position.QuadPart;
164 static int tiff_stream_close(thandle_t client_data)
166 /* Caller is responsible for releasing the stream object. */
170 static toff_t tiff_stream_size(thandle_t client_data)
172 IStream *stream = (IStream*)client_data;
176 hr = IStream_Stat(stream, &statstg, STATFLAG_NONAME);
178 if (SUCCEEDED(hr)) return statstg.cbSize.QuadPart;
182 static int tiff_stream_map(thandle_t client_data, tdata_t *addr, toff_t *size)
184 /* Cannot mmap streams */
188 static void tiff_stream_unmap(thandle_t client_data, tdata_t addr, toff_t size)
190 /* No need to ever do this, since we can't map things. */
193 static TIFF* tiff_open_stream(IStream *stream, const char *mode)
198 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
200 return pTIFFClientOpen("<IStream object>", mode, stream, tiff_stream_read,
201 tiff_stream_write, tiff_stream_seek, tiff_stream_close,
202 tiff_stream_size, tiff_stream_map, tiff_stream_unmap);
206 IWICBitmapDecoder IWICBitmapDecoder_iface;
209 CRITICAL_SECTION lock; /* Must be held when tiff is used or initiailzed is set */
215 const WICPixelFormatGUID *format;
222 int invert_grayscale;
224 UINT tile_width, tile_height;
229 UINT resolution_unit;
234 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
235 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
239 tiff_decode_info decode_info;
240 INT cached_tile_x, cached_tile_y;
244 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl;
245 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl;
247 static inline TiffDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
249 return CONTAINING_RECORD(iface, TiffDecoder, IWICBitmapDecoder_iface);
252 static inline TiffFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
254 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICBitmapFrameDecode_iface);
257 static inline TiffFrameDecode *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
259 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICMetadataBlockReader_iface);
262 static HRESULT tiff_get_decode_info(TIFF *tiff, tiff_decode_info *decode_info)
264 uint16 photometric, bps, samples, planar;
265 uint16 extra_sample_count, extra_sample, *extra_samples;
268 decode_info->indexed = 0;
269 decode_info->reverse_bgr = 0;
270 decode_info->invert_grayscale = 0;
271 decode_info->tiled = 0;
273 ret = pTIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
276 WARN("missing PhotometricInterpretation tag\n");
280 ret = pTIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bps);
282 decode_info->bps = bps;
284 ret = pTIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samples);
285 if (!ret) samples = 1;
286 decode_info->samples = samples;
292 ret = pTIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planar);
293 if (!ret) planar = 1;
296 FIXME("unhandled planar configuration %u\n", planar);
300 decode_info->planar = planar;
304 case 0: /* WhiteIsZero */
305 decode_info->invert_grayscale = 1;
307 case 1: /* BlackIsZero */
310 FIXME("unhandled grayscale sample count %u\n", samples);
314 decode_info->bpp = bps;
318 decode_info->format = &GUID_WICPixelFormatBlackWhite;
321 decode_info->format = &GUID_WICPixelFormat4bppGray;
324 decode_info->format = &GUID_WICPixelFormat8bppGray;
327 FIXME("unhandled greyscale bit count %u\n", bps);
332 decode_info->bpp = bps * samples;
336 ret = pTIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extra_sample_count, &extra_samples);
339 extra_sample_count = 1;
341 extra_samples = &extra_sample;
344 else if (samples != 3)
346 FIXME("unhandled RGB sample count %u\n", samples);
353 decode_info->reverse_bgr = 1;
355 decode_info->format = &GUID_WICPixelFormat24bppBGR;
357 switch(extra_samples[0])
359 case 1: /* Associated (pre-multiplied) alpha data */
360 decode_info->format = &GUID_WICPixelFormat32bppPBGRA;
362 case 0: /* Unspecified data */
363 case 2: /* Unassociated alpha data */
364 decode_info->format = &GUID_WICPixelFormat32bppBGRA;
367 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
373 decode_info->format = &GUID_WICPixelFormat48bppRGB;
375 switch(extra_samples[0])
377 case 1: /* Associated (pre-multiplied) alpha data */
378 decode_info->format = &GUID_WICPixelFormat64bppPRGBA;
380 case 0: /* Unspecified data */
381 case 2: /* Unassociated alpha data */
382 decode_info->format = &GUID_WICPixelFormat64bppRGBA;
385 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
390 FIXME("unhandled RGB bit count %u\n", bps);
394 case 3: /* RGB Palette */
397 FIXME("unhandled indexed sample count %u\n", samples);
401 decode_info->indexed = 1;
402 decode_info->bpp = bps;
406 decode_info->format = &GUID_WICPixelFormat4bppIndexed;
409 decode_info->format = &GUID_WICPixelFormat8bppIndexed;
412 FIXME("unhandled indexed bit count %u\n", bps);
416 case 4: /* Transparency mask */
421 FIXME("unhandled PhotometricInterpretation %u\n", photometric);
425 ret = pTIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &decode_info->width);
428 WARN("missing image width\n");
432 ret = pTIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &decode_info->height);
435 WARN("missing image length\n");
439 if ((ret = pTIFFGetField(tiff, TIFFTAG_TILEWIDTH, &decode_info->tile_width)))
441 decode_info->tiled = 1;
443 ret = pTIFFGetField(tiff, TIFFTAG_TILELENGTH, &decode_info->tile_height);
446 WARN("missing tile height\n");
450 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
451 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
452 decode_info->tiles_across = (decode_info->width + decode_info->tile_width - 1) / decode_info->tile_width;
454 else if ((ret = pTIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &decode_info->tile_height)))
456 if (decode_info->tile_height > decode_info->height)
457 decode_info->tile_height = decode_info->height;
458 decode_info->tile_width = decode_info->width;
459 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
460 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
464 /* Some broken TIFF files have a single strip and lack the RowsPerStrip tag */
465 decode_info->tile_height = decode_info->height;
466 decode_info->tile_width = decode_info->width;
467 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
468 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
471 decode_info->resolution_unit = 0;
472 pTIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &decode_info->resolution_unit);
473 if (decode_info->resolution_unit != 0)
475 ret = pTIFFGetField(tiff, TIFFTAG_XRESOLUTION, &decode_info->xres);
478 WARN("missing X resolution\n");
479 decode_info->resolution_unit = 0;
482 ret = pTIFFGetField(tiff, TIFFTAG_YRESOLUTION, &decode_info->yres);
485 WARN("missing Y resolution\n");
486 decode_info->resolution_unit = 0;
493 static HRESULT WINAPI TiffDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
496 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
497 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
499 if (!ppv) return E_INVALIDARG;
501 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
508 return E_NOINTERFACE;
511 IUnknown_AddRef((IUnknown*)*ppv);
515 static ULONG WINAPI TiffDecoder_AddRef(IWICBitmapDecoder *iface)
517 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
518 ULONG ref = InterlockedIncrement(&This->ref);
520 TRACE("(%p) refcount=%u\n", iface, ref);
525 static ULONG WINAPI TiffDecoder_Release(IWICBitmapDecoder *iface)
527 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
528 ULONG ref = InterlockedDecrement(&This->ref);
530 TRACE("(%p) refcount=%u\n", iface, ref);
534 if (This->tiff) pTIFFClose(This->tiff);
535 if (This->stream) IStream_Release(This->stream);
536 This->lock.DebugInfo->Spare[0] = 0;
537 DeleteCriticalSection(&This->lock);
538 HeapFree(GetProcessHeap(), 0, This);
544 static HRESULT WINAPI TiffDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
545 DWORD *pdwCapability)
547 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
551 static HRESULT WINAPI TiffDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
552 WICDecodeOptions cacheOptions)
554 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
558 TRACE("(%p,%p,%x): stub\n", iface, pIStream, cacheOptions);
560 EnterCriticalSection(&This->lock);
562 if (This->initialized)
564 hr = WINCODEC_ERR_WRONGSTATE;
568 tiff = tiff_open_stream(pIStream, "r");
577 This->stream = pIStream;
578 IStream_AddRef(pIStream);
579 This->initialized = TRUE;
582 LeaveCriticalSection(&This->lock);
586 static HRESULT WINAPI TiffDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
587 GUID *pguidContainerFormat)
589 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
593 static HRESULT WINAPI TiffDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
594 IWICBitmapDecoderInfo **ppIDecoderInfo)
597 IWICComponentInfo *compinfo;
599 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
601 hr = CreateComponentInfo(&CLSID_WICTiffDecoder, &compinfo);
602 if (FAILED(hr)) return hr;
604 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
605 (void**)ppIDecoderInfo);
607 IWICComponentInfo_Release(compinfo);
612 static HRESULT WINAPI TiffDecoder_CopyPalette(IWICBitmapDecoder *iface,
613 IWICPalette *pIPalette)
615 FIXME("(%p,%p): stub\n", iface, pIPalette);
619 static HRESULT WINAPI TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
620 IWICMetadataQueryReader **ppIMetadataQueryReader)
622 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
626 static HRESULT WINAPI TiffDecoder_GetPreview(IWICBitmapDecoder *iface,
627 IWICBitmapSource **ppIBitmapSource)
629 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
633 static HRESULT WINAPI TiffDecoder_GetColorContexts(IWICBitmapDecoder *iface,
634 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
636 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
640 static HRESULT WINAPI TiffDecoder_GetThumbnail(IWICBitmapDecoder *iface,
641 IWICBitmapSource **ppIThumbnail)
643 TRACE("(%p,%p)\n", iface, ppIThumbnail);
644 return WINCODEC_ERR_CODECNOTHUMBNAIL;
647 static HRESULT WINAPI TiffDecoder_GetFrameCount(IWICBitmapDecoder *iface,
650 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
654 WARN("(%p) <-- WINCODEC_ERR_WRONGSTATE\n", iface);
655 return WINCODEC_ERR_WRONGSTATE;
658 EnterCriticalSection(&This->lock);
659 *pCount = pTIFFNumberOfDirectories(This->tiff);
660 LeaveCriticalSection(&This->lock);
662 TRACE("(%p) <-- %i\n", iface, *pCount);
667 static HRESULT WINAPI TiffDecoder_GetFrame(IWICBitmapDecoder *iface,
668 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
670 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
671 TiffFrameDecode *result;
673 tiff_decode_info decode_info;
676 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
679 return WINCODEC_ERR_WRONGSTATE;
681 EnterCriticalSection(&This->lock);
682 res = pTIFFSetDirectory(This->tiff, index);
683 if (!res) hr = E_INVALIDARG;
684 else hr = tiff_get_decode_info(This->tiff, &decode_info);
685 LeaveCriticalSection(&This->lock);
689 result = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode));
693 result->IWICBitmapFrameDecode_iface.lpVtbl = &TiffFrameDecode_Vtbl;
694 result->IWICMetadataBlockReader_iface.lpVtbl = &TiffFrameDecode_BlockVtbl;
696 result->parent = This;
697 result->index = index;
698 result->decode_info = decode_info;
699 result->cached_tile_x = -1;
700 result->cached_tile = HeapAlloc(GetProcessHeap(), 0, decode_info.tile_size);
702 if (result->cached_tile)
703 *ppIBitmapFrame = (IWICBitmapFrameDecode*)result;
707 HeapFree(GetProcessHeap(), 0, result);
710 else hr = E_OUTOFMEMORY;
713 if (FAILED(hr)) *ppIBitmapFrame = NULL;
718 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl = {
719 TiffDecoder_QueryInterface,
722 TiffDecoder_QueryCapability,
723 TiffDecoder_Initialize,
724 TiffDecoder_GetContainerFormat,
725 TiffDecoder_GetDecoderInfo,
726 TiffDecoder_CopyPalette,
727 TiffDecoder_GetMetadataQueryReader,
728 TiffDecoder_GetPreview,
729 TiffDecoder_GetColorContexts,
730 TiffDecoder_GetThumbnail,
731 TiffDecoder_GetFrameCount,
735 static HRESULT WINAPI TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
738 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
739 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
741 if (!ppv) return E_INVALIDARG;
743 if (IsEqualIID(&IID_IUnknown, iid) ||
744 IsEqualIID(&IID_IWICBitmapSource, iid) ||
745 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
749 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
751 *ppv = &This->IWICMetadataBlockReader_iface;
756 return E_NOINTERFACE;
759 IUnknown_AddRef((IUnknown*)*ppv);
763 static ULONG WINAPI TiffFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
765 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
766 ULONG ref = InterlockedIncrement(&This->ref);
768 TRACE("(%p) refcount=%u\n", iface, ref);
773 static ULONG WINAPI TiffFrameDecode_Release(IWICBitmapFrameDecode *iface)
775 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
776 ULONG ref = InterlockedDecrement(&This->ref);
778 TRACE("(%p) refcount=%u\n", iface, ref);
782 HeapFree(GetProcessHeap(), 0, This->cached_tile);
783 HeapFree(GetProcessHeap(), 0, This);
789 static HRESULT WINAPI TiffFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
790 UINT *puiWidth, UINT *puiHeight)
792 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
794 *puiWidth = This->decode_info.width;
795 *puiHeight = This->decode_info.height;
797 TRACE("(%p) <-- %ux%u\n", iface, *puiWidth, *puiHeight);
802 static HRESULT WINAPI TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
803 WICPixelFormatGUID *pPixelFormat)
805 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
807 memcpy(pPixelFormat, This->decode_info.format, sizeof(GUID));
809 TRACE("(%p) <-- %s\n", This, debugstr_guid(This->decode_info.format));
814 static HRESULT WINAPI TiffFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
815 double *pDpiX, double *pDpiY)
817 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
819 switch (This->decode_info.resolution_unit)
822 FIXME("unknown resolution unit %i\n", This->decode_info.resolution_unit);
824 case 0: /* Not set */
825 *pDpiX = *pDpiY = 96.0;
827 case 1: /* Relative measurements */
829 *pDpiY = 96.0 * This->decode_info.yres / This->decode_info.xres;
832 *pDpiX = This->decode_info.xres;
833 *pDpiY = This->decode_info.yres;
835 case 3: /* Centimeter */
836 *pDpiX = This->decode_info.xres / 2.54;
837 *pDpiY = This->decode_info.yres / 2.54;
841 TRACE("(%p) <-- %f,%f unit=%i\n", iface, *pDpiX, *pDpiY, This->decode_info.resolution_unit);
846 static HRESULT WINAPI TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
847 IWICPalette *pIPalette)
849 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
850 uint16 *red, *green, *blue;
851 WICColor colors[256];
852 int color_count, ret, i;
854 TRACE("(%p,%p)\n", iface, pIPalette);
856 color_count = 1<<This->decode_info.bps;
858 EnterCriticalSection(&This->parent->lock);
859 ret = pTIFFGetField(This->parent->tiff, TIFFTAG_COLORMAP, &red, &green, &blue);
860 LeaveCriticalSection(&This->parent->lock);
864 WARN("Couldn't read color map\n");
868 for (i=0; i<color_count; i++)
870 colors[i] = 0xff000000 |
871 ((red[i]<<8) & 0xff0000) |
872 (green[i] & 0xff00) |
873 ((blue[i]>>8) & 0xff);
876 return IWICPalette_InitializeCustom(pIPalette, colors, color_count);
879 static HRESULT TiffFrameDecode_ReadTile(TiffFrameDecode *This, UINT tile_x, UINT tile_y)
885 swap_bytes = pTIFFIsByteSwapped(This->parent->tiff);
887 ret = pTIFFSetDirectory(This->parent->tiff, This->index);
894 if (This->decode_info.tiled)
896 ret = pTIFFReadEncodedTile(This->parent->tiff, tile_x + tile_y * This->decode_info.tiles_across, This->cached_tile, This->decode_info.tile_size);
900 ret = pTIFFReadEncodedStrip(This->parent->tiff, tile_y, This->cached_tile, This->decode_info.tile_size);
907 if (hr == S_OK && This->decode_info.reverse_bgr)
909 if (This->decode_info.bps == 8)
911 UINT sample_count = This->decode_info.samples;
913 reverse_bgr8(sample_count, This->cached_tile, This->decode_info.tile_width,
914 This->decode_info.tile_height, This->decode_info.tile_width * sample_count);
918 if (hr == S_OK && swap_bytes && This->decode_info.bps > 8)
920 UINT row, i, samples_per_row;
923 samples_per_row = This->decode_info.tile_width * This->decode_info.samples;
925 switch(This->decode_info.bps)
928 for (row=0; row<This->decode_info.tile_height; row++)
930 sample = This->cached_tile + row * This->decode_info.tile_stride;
931 for (i=0; i<samples_per_row; i++)
934 sample[1] = sample[0];
941 ERR("unhandled bps for byte swap %u\n", This->decode_info.bps);
946 if (hr == S_OK && This->decode_info.invert_grayscale)
950 if (This->decode_info.samples != 1)
952 ERR("cannot invert grayscale image with %u samples\n", This->decode_info.samples);
956 end = This->cached_tile+This->decode_info.tile_size;
958 for (byte = This->cached_tile; byte != end; byte++)
964 This->cached_tile_x = tile_x;
965 This->cached_tile_y = tile_y;
971 static HRESULT WINAPI TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
972 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
974 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
975 UINT min_tile_x, max_tile_x, min_tile_y, max_tile_y;
983 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
989 rect.Width = This->decode_info.width;
990 rect.Height = This->decode_info.height;
995 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->decode_info.width ||
996 prc->Y+prc->Height > This->decode_info.height)
1000 bytesperrow = ((This->decode_info.bpp * prc->Width)+7)/8;
1002 if (cbStride < bytesperrow)
1003 return E_INVALIDARG;
1005 if ((cbStride * prc->Height) > cbBufferSize)
1006 return E_INVALIDARG;
1008 min_tile_x = prc->X / This->decode_info.tile_width;
1009 min_tile_y = prc->Y / This->decode_info.tile_height;
1010 max_tile_x = (prc->X+prc->Width-1) / This->decode_info.tile_width;
1011 max_tile_y = (prc->Y+prc->Height-1) / This->decode_info.tile_height;
1013 EnterCriticalSection(&This->parent->lock);
1015 for (tile_x=min_tile_x; tile_x <= max_tile_x; tile_x++)
1017 for (tile_y=min_tile_y; tile_y <= max_tile_y; tile_y++)
1019 if (tile_x != This->cached_tile_x || tile_y != This->cached_tile_y)
1021 hr = TiffFrameDecode_ReadTile(This, tile_x, tile_y);
1026 if (prc->X < tile_x * This->decode_info.tile_width)
1029 rc.X = prc->X - tile_x * This->decode_info.tile_width;
1031 if (prc->Y < tile_y * This->decode_info.tile_height)
1034 rc.Y = prc->Y - tile_y * This->decode_info.tile_height;
1036 if (prc->X+prc->Width > (tile_x+1) * This->decode_info.tile_width)
1037 rc.Width = This->decode_info.tile_width - rc.X;
1038 else if (prc->X < tile_x * This->decode_info.tile_width)
1039 rc.Width = prc->Width + prc->X - tile_x * This->decode_info.tile_width;
1041 rc.Width = prc->Width;
1043 if (prc->Y+prc->Height > (tile_y+1) * This->decode_info.tile_height)
1044 rc.Height = This->decode_info.tile_height - rc.Y;
1045 else if (prc->Y < tile_y * This->decode_info.tile_height)
1046 rc.Height = prc->Height + prc->Y - tile_y * This->decode_info.tile_height;
1048 rc.Height = prc->Height;
1050 dst_tilepos = pbBuffer + (cbStride * ((rc.Y + tile_y * This->decode_info.tile_height) - prc->Y)) +
1051 ((This->decode_info.bpp * ((rc.X + tile_x * This->decode_info.tile_width) - prc->X) + 7) / 8);
1053 hr = copy_pixels(This->decode_info.bpp, This->cached_tile,
1054 This->decode_info.tile_width, This->decode_info.tile_height, This->decode_info.tile_stride,
1055 &rc, cbStride, cbBufferSize, dst_tilepos);
1060 LeaveCriticalSection(&This->parent->lock);
1061 TRACE("<-- 0x%x\n", hr);
1067 LeaveCriticalSection(&This->parent->lock);
1072 static HRESULT WINAPI TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
1073 IWICMetadataQueryReader **ppIMetadataQueryReader)
1075 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
1079 static HRESULT WINAPI TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
1080 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1082 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
1086 static HRESULT WINAPI TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
1087 IWICBitmapSource **ppIThumbnail)
1089 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
1093 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl = {
1094 TiffFrameDecode_QueryInterface,
1095 TiffFrameDecode_AddRef,
1096 TiffFrameDecode_Release,
1097 TiffFrameDecode_GetSize,
1098 TiffFrameDecode_GetPixelFormat,
1099 TiffFrameDecode_GetResolution,
1100 TiffFrameDecode_CopyPalette,
1101 TiffFrameDecode_CopyPixels,
1102 TiffFrameDecode_GetMetadataQueryReader,
1103 TiffFrameDecode_GetColorContexts,
1104 TiffFrameDecode_GetThumbnail
1107 static HRESULT WINAPI TiffFrameDecode_Block_QueryInterface(IWICMetadataBlockReader *iface,
1108 REFIID iid, void **ppv)
1110 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1111 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
1114 static ULONG WINAPI TiffFrameDecode_Block_AddRef(IWICMetadataBlockReader *iface)
1116 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1117 return IWICBitmapFrameDecode_AddRef(&This->IWICBitmapFrameDecode_iface);
1120 static ULONG WINAPI TiffFrameDecode_Block_Release(IWICMetadataBlockReader *iface)
1122 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1123 return IWICBitmapFrameDecode_Release(&This->IWICBitmapFrameDecode_iface);
1126 static HRESULT WINAPI TiffFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
1129 FIXME("(%p,%p): stub\n", iface, guid);
1133 static HRESULT WINAPI TiffFrameDecode_Block_GetCount(IWICMetadataBlockReader *iface,
1136 FIXME("(%p,%p): stub\n", iface, count);
1140 static HRESULT WINAPI TiffFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
1141 UINT index, IWICMetadataReader **reader)
1143 FIXME("(%p,%u,%p): stub\n", iface, index, reader);
1147 static HRESULT WINAPI TiffFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader *iface,
1148 IEnumUnknown **enum_metadata)
1150 FIXME("(%p,%p): stub\n", iface, enum_metadata);
1154 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl =
1156 TiffFrameDecode_Block_QueryInterface,
1157 TiffFrameDecode_Block_AddRef,
1158 TiffFrameDecode_Block_Release,
1159 TiffFrameDecode_Block_GetContainerFormat,
1160 TiffFrameDecode_Block_GetCount,
1161 TiffFrameDecode_Block_GetReaderByIndex,
1162 TiffFrameDecode_Block_GetEnumerator
1165 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1170 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1174 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1176 if (!load_libtiff())
1178 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF);
1182 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder));
1183 if (!This) return E_OUTOFMEMORY;
1185 This->IWICBitmapDecoder_iface.lpVtbl = &TiffDecoder_Vtbl;
1187 This->stream = NULL;
1188 InitializeCriticalSection(&This->lock);
1189 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffDecoder.lock");
1191 This->initialized = FALSE;
1193 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1194 IUnknown_Release((IUnknown*)This);
1199 struct tiff_encode_format {
1200 const WICPixelFormatGUID *guid;
1206 int extra_sample_type;
1210 static const struct tiff_encode_format formats[] = {
1211 {&GUID_WICPixelFormat24bppBGR, 2, 8, 3, 24, 0, 0, 1},
1212 {&GUID_WICPixelFormatBlackWhite, 1, 1, 1, 1, 0, 0, 0},
1213 {&GUID_WICPixelFormat4bppGray, 1, 4, 1, 4, 0, 0, 0},
1214 {&GUID_WICPixelFormat8bppGray, 1, 8, 1, 8, 0, 0, 0},
1215 {&GUID_WICPixelFormat32bppBGRA, 2, 8, 4, 32, 1, 2, 1},
1216 {&GUID_WICPixelFormat32bppPBGRA, 2, 8, 4, 32, 1, 1, 1},
1217 {&GUID_WICPixelFormat48bppRGB, 2, 16, 3, 48, 0, 0, 0},
1218 {&GUID_WICPixelFormat64bppRGBA, 2, 16, 4, 64, 1, 2, 0},
1219 {&GUID_WICPixelFormat64bppPRGBA, 2, 16, 4, 64, 1, 1, 0},
1223 typedef struct TiffEncoder {
1224 IWICBitmapEncoder IWICBitmapEncoder_iface;
1227 CRITICAL_SECTION lock; /* Must be held when tiff is used or fields below are set */
1232 ULONG num_frames_committed;
1235 static inline TiffEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1237 return CONTAINING_RECORD(iface, TiffEncoder, IWICBitmapEncoder_iface);
1240 typedef struct TiffFrameEncode {
1241 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1243 TiffEncoder *parent;
1244 /* fields below are protected by parent->lock */
1248 const struct tiff_encode_format *format;
1254 static inline TiffFrameEncode *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1256 return CONTAINING_RECORD(iface, TiffFrameEncode, IWICBitmapFrameEncode_iface);
1259 static HRESULT WINAPI TiffFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1262 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1263 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1265 if (!ppv) return E_INVALIDARG;
1267 if (IsEqualIID(&IID_IUnknown, iid) ||
1268 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1270 *ppv = &This->IWICBitmapFrameEncode_iface;
1275 return E_NOINTERFACE;
1278 IUnknown_AddRef((IUnknown*)*ppv);
1282 static ULONG WINAPI TiffFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1284 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1285 ULONG ref = InterlockedIncrement(&This->ref);
1287 TRACE("(%p) refcount=%u\n", iface, ref);
1292 static ULONG WINAPI TiffFrameEncode_Release(IWICBitmapFrameEncode *iface)
1294 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1295 ULONG ref = InterlockedDecrement(&This->ref);
1297 TRACE("(%p) refcount=%u\n", iface, ref);
1301 IWICBitmapEncoder_Release(&This->parent->IWICBitmapEncoder_iface);
1302 HeapFree(GetProcessHeap(), 0, This);
1308 static HRESULT WINAPI TiffFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1309 IPropertyBag2 *pIEncoderOptions)
1311 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1312 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1314 EnterCriticalSection(&This->parent->lock);
1316 if (This->initialized)
1318 LeaveCriticalSection(&This->parent->lock);
1319 return WINCODEC_ERR_WRONGSTATE;
1322 This->initialized = TRUE;
1324 LeaveCriticalSection(&This->parent->lock);
1329 static HRESULT WINAPI TiffFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1330 UINT uiWidth, UINT uiHeight)
1332 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1333 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1335 EnterCriticalSection(&This->parent->lock);
1337 if (!This->initialized || This->info_written)
1339 LeaveCriticalSection(&This->parent->lock);
1340 return WINCODEC_ERR_WRONGSTATE;
1343 This->width = uiWidth;
1344 This->height = uiHeight;
1346 LeaveCriticalSection(&This->parent->lock);
1351 static HRESULT WINAPI TiffFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1352 double dpiX, double dpiY)
1354 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1355 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1357 EnterCriticalSection(&This->parent->lock);
1359 if (!This->initialized || This->info_written)
1361 LeaveCriticalSection(&This->parent->lock);
1362 return WINCODEC_ERR_WRONGSTATE;
1368 LeaveCriticalSection(&This->parent->lock);
1373 static HRESULT WINAPI TiffFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1374 WICPixelFormatGUID *pPixelFormat)
1376 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1379 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1381 EnterCriticalSection(&This->parent->lock);
1383 if (!This->initialized || This->info_written)
1385 LeaveCriticalSection(&This->parent->lock);
1386 return WINCODEC_ERR_WRONGSTATE;
1389 for (i=0; formats[i].guid; i++)
1391 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1395 if (!formats[i].guid) i = 0;
1397 This->format = &formats[i];
1398 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1400 LeaveCriticalSection(&This->parent->lock);
1405 static HRESULT WINAPI TiffFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1406 UINT cCount, IWICColorContext **ppIColorContext)
1408 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1412 static HRESULT WINAPI TiffFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1413 IWICPalette *pIPalette)
1415 FIXME("(%p,%p): stub\n", iface, pIPalette);
1416 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1419 static HRESULT WINAPI TiffFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1420 IWICBitmapSource *pIThumbnail)
1422 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1423 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1426 static HRESULT WINAPI TiffFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1427 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1429 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1430 BYTE *row_data, *swapped_data = NULL;
1431 UINT i, j, line_size;
1433 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1435 EnterCriticalSection(&This->parent->lock);
1437 if (!This->initialized || !This->width || !This->height || !This->format)
1439 LeaveCriticalSection(&This->parent->lock);
1440 return WINCODEC_ERR_WRONGSTATE;
1443 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1445 LeaveCriticalSection(&This->parent->lock);
1446 return E_INVALIDARG;
1449 line_size = ((This->width * This->format->bpp)+7)/8;
1451 if (This->format->reverse_bgr)
1453 swapped_data = HeapAlloc(GetProcessHeap(), 0, line_size);
1456 LeaveCriticalSection(&This->parent->lock);
1457 return E_OUTOFMEMORY;
1461 if (!This->info_written)
1463 pTIFFSetField(This->parent->tiff, TIFFTAG_PHOTOMETRIC, (uint16)This->format->photometric);
1464 pTIFFSetField(This->parent->tiff, TIFFTAG_PLANARCONFIG, (uint16)1);
1465 pTIFFSetField(This->parent->tiff, TIFFTAG_BITSPERSAMPLE, (uint16)This->format->bps);
1466 pTIFFSetField(This->parent->tiff, TIFFTAG_SAMPLESPERPIXEL, (uint16)This->format->samples);
1468 if (This->format->extra_sample)
1470 uint16 extra_samples;
1471 extra_samples = This->format->extra_sample_type;
1473 pTIFFSetField(This->parent->tiff, TIFFTAG_EXTRASAMPLES, (uint16)1, &extra_samples);
1476 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGEWIDTH, (uint32)This->width);
1477 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGELENGTH, (uint32)This->height);
1479 if (This->xres != 0.0 && This->yres != 0.0)
1481 pTIFFSetField(This->parent->tiff, TIFFTAG_RESOLUTIONUNIT, (uint16)2); /* Inch */
1482 pTIFFSetField(This->parent->tiff, TIFFTAG_XRESOLUTION, (float)This->xres);
1483 pTIFFSetField(This->parent->tiff, TIFFTAG_YRESOLUTION, (float)This->yres);
1486 This->info_written = TRUE;
1489 for (i=0; i<lineCount; i++)
1491 row_data = pbPixels + i * cbStride;
1493 if (This->format->reverse_bgr && This->format->bps == 8)
1495 memcpy(swapped_data, row_data, line_size);
1496 for (j=0; j<line_size; j += This->format->samples)
1499 temp = swapped_data[j];
1500 swapped_data[j] = swapped_data[j+2];
1501 swapped_data[j+2] = temp;
1503 row_data = swapped_data;
1506 pTIFFWriteScanline(This->parent->tiff, (tdata_t)row_data, i+This->lines_written, 0);
1509 This->lines_written += lineCount;
1511 LeaveCriticalSection(&This->parent->lock);
1513 HeapFree(GetProcessHeap(), 0, swapped_data);
1518 static HRESULT WINAPI TiffFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1519 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1521 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1524 WICPixelFormatGUID guid;
1528 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1530 if (!This->initialized || !This->width || !This->height)
1531 return WINCODEC_ERR_WRONGSTATE;
1535 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1536 if (FAILED(hr)) return hr;
1537 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1538 if (FAILED(hr)) return hr;
1541 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1542 if (FAILED(hr)) return hr;
1543 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1545 /* FIXME: should use WICConvertBitmapSource to convert */
1546 ERR("format %s unsupported\n", debugstr_guid(&guid));
1550 if (This->xres == 0.0 || This->yres == 0.0)
1553 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1554 if (FAILED(hr)) return hr;
1555 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1556 if (FAILED(hr)) return hr;
1562 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1563 if (FAILED(hr)) return hr;
1571 if (prc->Width != This->width) return E_INVALIDARG;
1573 stride = (This->format->bpp * This->width + 7)/8;
1575 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1576 if (!pixeldata) return E_OUTOFMEMORY;
1578 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1579 stride*prc->Height, pixeldata);
1583 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1584 stride*prc->Height, pixeldata);
1587 HeapFree(GetProcessHeap(), 0, pixeldata);
1592 static HRESULT WINAPI TiffFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1594 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1596 TRACE("(%p)\n", iface);
1598 EnterCriticalSection(&This->parent->lock);
1600 if (!This->info_written || This->lines_written != This->height || This->committed)
1602 LeaveCriticalSection(&This->parent->lock);
1603 return WINCODEC_ERR_WRONGSTATE;
1606 /* libtiff will commit the data when creating a new frame or closing the file */
1608 This->committed = TRUE;
1609 This->parent->num_frames_committed++;
1611 LeaveCriticalSection(&This->parent->lock);
1616 static HRESULT WINAPI TiffFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1617 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1619 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1623 static const IWICBitmapFrameEncodeVtbl TiffFrameEncode_Vtbl = {
1624 TiffFrameEncode_QueryInterface,
1625 TiffFrameEncode_AddRef,
1626 TiffFrameEncode_Release,
1627 TiffFrameEncode_Initialize,
1628 TiffFrameEncode_SetSize,
1629 TiffFrameEncode_SetResolution,
1630 TiffFrameEncode_SetPixelFormat,
1631 TiffFrameEncode_SetColorContexts,
1632 TiffFrameEncode_SetPalette,
1633 TiffFrameEncode_SetThumbnail,
1634 TiffFrameEncode_WritePixels,
1635 TiffFrameEncode_WriteSource,
1636 TiffFrameEncode_Commit,
1637 TiffFrameEncode_GetMetadataQueryWriter
1640 static HRESULT WINAPI TiffEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1643 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1644 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1646 if (!ppv) return E_INVALIDARG;
1648 if (IsEqualIID(&IID_IUnknown, iid) ||
1649 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1656 return E_NOINTERFACE;
1659 IUnknown_AddRef((IUnknown*)*ppv);
1663 static ULONG WINAPI TiffEncoder_AddRef(IWICBitmapEncoder *iface)
1665 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1666 ULONG ref = InterlockedIncrement(&This->ref);
1668 TRACE("(%p) refcount=%u\n", iface, ref);
1673 static ULONG WINAPI TiffEncoder_Release(IWICBitmapEncoder *iface)
1675 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1676 ULONG ref = InterlockedDecrement(&This->ref);
1678 TRACE("(%p) refcount=%u\n", iface, ref);
1682 if (This->tiff) pTIFFClose(This->tiff);
1683 if (This->stream) IStream_Release(This->stream);
1684 This->lock.DebugInfo->Spare[0] = 0;
1685 DeleteCriticalSection(&This->lock);
1686 HeapFree(GetProcessHeap(), 0, This);
1692 static HRESULT WINAPI TiffEncoder_Initialize(IWICBitmapEncoder *iface,
1693 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1695 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1699 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1701 EnterCriticalSection(&This->lock);
1703 if (This->initialized || This->committed)
1705 hr = WINCODEC_ERR_WRONGSTATE;
1709 tiff = tiff_open_stream(pIStream, "w");
1718 This->stream = pIStream;
1719 IStream_AddRef(pIStream);
1720 This->initialized = TRUE;
1723 LeaveCriticalSection(&This->lock);
1727 static HRESULT WINAPI TiffEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1728 GUID *pguidContainerFormat)
1730 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
1734 static HRESULT WINAPI TiffEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1735 IWICBitmapEncoderInfo **ppIEncoderInfo)
1737 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1741 static HRESULT WINAPI TiffEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1742 UINT cCount, IWICColorContext **ppIColorContext)
1744 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1748 static HRESULT WINAPI TiffEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1750 TRACE("(%p,%p)\n", iface, pIPalette);
1751 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1754 static HRESULT WINAPI TiffEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1756 TRACE("(%p,%p)\n", iface, pIThumbnail);
1757 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1760 static HRESULT WINAPI TiffEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1762 TRACE("(%p,%p)\n", iface, pIPreview);
1763 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1766 static HRESULT WINAPI TiffEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1767 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1769 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1770 TiffFrameEncode *result;
1774 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1776 EnterCriticalSection(&This->lock);
1778 if (!This->initialized || This->committed)
1780 hr = WINCODEC_ERR_WRONGSTATE;
1782 else if (This->num_frames != This->num_frames_committed)
1784 FIXME("New frame created before previous frame was committed\n");
1790 hr = CreatePropertyBag2(ppIEncoderOptions);
1795 result = HeapAlloc(GetProcessHeap(), 0, sizeof(*result));
1799 result->IWICBitmapFrameEncode_iface.lpVtbl = &TiffFrameEncode_Vtbl;
1801 result->parent = This;
1802 result->initialized = FALSE;
1803 result->info_written = FALSE;
1804 result->committed = FALSE;
1805 result->format = NULL;
1810 result->lines_written = 0;
1812 IWICBitmapEncoder_AddRef(iface);
1813 *ppIFrameEncode = &result->IWICBitmapFrameEncode_iface;
1815 if (This->num_frames != 0)
1816 pTIFFWriteDirectory(This->tiff);
1825 IPropertyBag2_Release(*ppIEncoderOptions);
1826 *ppIEncoderOptions = NULL;
1830 LeaveCriticalSection(&This->lock);
1835 static HRESULT WINAPI TiffEncoder_Commit(IWICBitmapEncoder *iface)
1837 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1839 TRACE("(%p)\n", iface);
1841 EnterCriticalSection(&This->lock);
1843 if (!This->initialized || This->committed)
1845 LeaveCriticalSection(&This->lock);
1846 return WINCODEC_ERR_WRONGSTATE;
1849 pTIFFClose(This->tiff);
1850 IStream_Release(This->stream);
1851 This->stream = NULL;
1854 This->committed = TRUE;
1856 LeaveCriticalSection(&This->lock);
1861 static HRESULT WINAPI TiffEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1862 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1864 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1868 static const IWICBitmapEncoderVtbl TiffEncoder_Vtbl = {
1869 TiffEncoder_QueryInterface,
1871 TiffEncoder_Release,
1872 TiffEncoder_Initialize,
1873 TiffEncoder_GetContainerFormat,
1874 TiffEncoder_GetEncoderInfo,
1875 TiffEncoder_SetColorContexts,
1876 TiffEncoder_SetPalette,
1877 TiffEncoder_SetThumbnail,
1878 TiffEncoder_SetPreview,
1879 TiffEncoder_CreateNewFrame,
1881 TiffEncoder_GetMetadataQueryWriter
1884 HRESULT TiffEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1889 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1893 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1895 if (!load_libtiff())
1897 ERR("Failed writing TIFF because unable to load %s\n",SONAME_LIBTIFF);
1901 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffEncoder));
1902 if (!This) return E_OUTOFMEMORY;
1904 This->IWICBitmapEncoder_iface.lpVtbl = &TiffEncoder_Vtbl;
1906 This->stream = NULL;
1907 InitializeCriticalSection(&This->lock);
1908 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffEncoder.lock");
1910 This->initialized = FALSE;
1911 This->num_frames = 0;
1912 This->num_frames_committed = 0;
1913 This->committed = FALSE;
1915 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1916 IUnknown_Release((IUnknown*)This);
1921 #else /* !SONAME_LIBTIFF */
1923 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1925 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
1929 HRESULT TiffEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1931 ERR("Trying to save TIFF picture, but Wine was compiled without TIFF support.\n");