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(TIFFCurrentDirectory);
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(TIFFCurrentDirectory);
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;
445 WARN("missing tile width\n");
449 ret = pTIFFGetField(tiff, TIFFTAG_TILELENGTH, &decode_info->tile_height);
452 WARN("missing tile height\n");
456 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
457 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
458 decode_info->tiles_across = (decode_info->width + decode_info->tile_width - 1) / decode_info->tile_width;
460 else if ((ret = pTIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &decode_info->tile_height)))
462 if (decode_info->tile_height > decode_info->height)
463 decode_info->tile_height = decode_info->height;
464 decode_info->tile_width = decode_info->width;
465 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
466 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
470 /* Some broken TIFF files have a single strip and lack the RowsPerStrip tag */
471 decode_info->tile_height = decode_info->height;
472 decode_info->tile_width = decode_info->width;
473 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
474 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
477 decode_info->resolution_unit = 0;
478 pTIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &decode_info->resolution_unit);
479 if (decode_info->resolution_unit != 0)
481 ret = pTIFFGetField(tiff, TIFFTAG_XRESOLUTION, &decode_info->xres);
484 WARN("missing X resolution\n");
485 decode_info->resolution_unit = 0;
488 ret = pTIFFGetField(tiff, TIFFTAG_YRESOLUTION, &decode_info->yres);
491 WARN("missing Y resolution\n");
492 decode_info->resolution_unit = 0;
499 static HRESULT WINAPI TiffDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
502 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
503 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
505 if (!ppv) return E_INVALIDARG;
507 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
514 return E_NOINTERFACE;
517 IUnknown_AddRef((IUnknown*)*ppv);
521 static ULONG WINAPI TiffDecoder_AddRef(IWICBitmapDecoder *iface)
523 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
524 ULONG ref = InterlockedIncrement(&This->ref);
526 TRACE("(%p) refcount=%u\n", iface, ref);
531 static ULONG WINAPI TiffDecoder_Release(IWICBitmapDecoder *iface)
533 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
534 ULONG ref = InterlockedDecrement(&This->ref);
536 TRACE("(%p) refcount=%u\n", iface, ref);
540 if (This->tiff) pTIFFClose(This->tiff);
541 if (This->stream) IStream_Release(This->stream);
542 This->lock.DebugInfo->Spare[0] = 0;
543 DeleteCriticalSection(&This->lock);
544 HeapFree(GetProcessHeap(), 0, This);
550 static HRESULT WINAPI TiffDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
551 DWORD *pdwCapability)
553 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
557 static HRESULT WINAPI TiffDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
558 WICDecodeOptions cacheOptions)
560 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
564 TRACE("(%p,%p,%x): stub\n", iface, pIStream, cacheOptions);
566 EnterCriticalSection(&This->lock);
568 if (This->initialized)
570 hr = WINCODEC_ERR_WRONGSTATE;
574 tiff = tiff_open_stream(pIStream, "r");
583 This->stream = pIStream;
584 IStream_AddRef(pIStream);
585 This->initialized = TRUE;
588 LeaveCriticalSection(&This->lock);
592 static HRESULT WINAPI TiffDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
593 GUID *pguidContainerFormat)
595 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
599 static HRESULT WINAPI TiffDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
600 IWICBitmapDecoderInfo **ppIDecoderInfo)
603 IWICComponentInfo *compinfo;
605 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
607 hr = CreateComponentInfo(&CLSID_WICTiffDecoder, &compinfo);
608 if (FAILED(hr)) return hr;
610 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
611 (void**)ppIDecoderInfo);
613 IWICComponentInfo_Release(compinfo);
618 static HRESULT WINAPI TiffDecoder_CopyPalette(IWICBitmapDecoder *iface,
619 IWICPalette *pIPalette)
621 FIXME("(%p,%p): stub\n", iface, pIPalette);
625 static HRESULT WINAPI TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
626 IWICMetadataQueryReader **ppIMetadataQueryReader)
628 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
632 static HRESULT WINAPI TiffDecoder_GetPreview(IWICBitmapDecoder *iface,
633 IWICBitmapSource **ppIBitmapSource)
635 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
639 static HRESULT WINAPI TiffDecoder_GetColorContexts(IWICBitmapDecoder *iface,
640 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
642 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
646 static HRESULT WINAPI TiffDecoder_GetThumbnail(IWICBitmapDecoder *iface,
647 IWICBitmapSource **ppIThumbnail)
649 TRACE("(%p,%p)\n", iface, ppIThumbnail);
650 return WINCODEC_ERR_CODECNOTHUMBNAIL;
653 static HRESULT WINAPI TiffDecoder_GetFrameCount(IWICBitmapDecoder *iface,
656 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
660 WARN("(%p) <-- WINCODEC_ERR_WRONGSTATE\n", iface);
661 return WINCODEC_ERR_WRONGSTATE;
664 EnterCriticalSection(&This->lock);
665 while (pTIFFReadDirectory(This->tiff)) { }
666 *pCount = pTIFFCurrentDirectory(This->tiff)+1;
667 LeaveCriticalSection(&This->lock);
669 TRACE("(%p) <-- %i\n", iface, *pCount);
674 static HRESULT WINAPI TiffDecoder_GetFrame(IWICBitmapDecoder *iface,
675 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
677 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
678 TiffFrameDecode *result;
680 tiff_decode_info decode_info;
683 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
686 return WINCODEC_ERR_WRONGSTATE;
688 EnterCriticalSection(&This->lock);
689 res = pTIFFSetDirectory(This->tiff, index);
690 if (!res) hr = E_INVALIDARG;
691 else hr = tiff_get_decode_info(This->tiff, &decode_info);
692 LeaveCriticalSection(&This->lock);
696 result = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode));
700 result->IWICBitmapFrameDecode_iface.lpVtbl = &TiffFrameDecode_Vtbl;
701 result->IWICMetadataBlockReader_iface.lpVtbl = &TiffFrameDecode_BlockVtbl;
703 result->parent = This;
704 result->index = index;
705 result->decode_info = decode_info;
706 result->cached_tile_x = -1;
707 result->cached_tile = HeapAlloc(GetProcessHeap(), 0, decode_info.tile_size);
709 if (result->cached_tile)
710 *ppIBitmapFrame = (IWICBitmapFrameDecode*)result;
714 HeapFree(GetProcessHeap(), 0, result);
717 else hr = E_OUTOFMEMORY;
720 if (FAILED(hr)) *ppIBitmapFrame = NULL;
725 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl = {
726 TiffDecoder_QueryInterface,
729 TiffDecoder_QueryCapability,
730 TiffDecoder_Initialize,
731 TiffDecoder_GetContainerFormat,
732 TiffDecoder_GetDecoderInfo,
733 TiffDecoder_CopyPalette,
734 TiffDecoder_GetMetadataQueryReader,
735 TiffDecoder_GetPreview,
736 TiffDecoder_GetColorContexts,
737 TiffDecoder_GetThumbnail,
738 TiffDecoder_GetFrameCount,
742 static HRESULT WINAPI TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
745 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
746 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
748 if (!ppv) return E_INVALIDARG;
750 if (IsEqualIID(&IID_IUnknown, iid) ||
751 IsEqualIID(&IID_IWICBitmapSource, iid) ||
752 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
756 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
758 *ppv = &This->IWICMetadataBlockReader_iface;
763 return E_NOINTERFACE;
766 IUnknown_AddRef((IUnknown*)*ppv);
770 static ULONG WINAPI TiffFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
772 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
773 ULONG ref = InterlockedIncrement(&This->ref);
775 TRACE("(%p) refcount=%u\n", iface, ref);
780 static ULONG WINAPI TiffFrameDecode_Release(IWICBitmapFrameDecode *iface)
782 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
783 ULONG ref = InterlockedDecrement(&This->ref);
785 TRACE("(%p) refcount=%u\n", iface, ref);
789 HeapFree(GetProcessHeap(), 0, This->cached_tile);
790 HeapFree(GetProcessHeap(), 0, This);
796 static HRESULT WINAPI TiffFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
797 UINT *puiWidth, UINT *puiHeight)
799 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
801 *puiWidth = This->decode_info.width;
802 *puiHeight = This->decode_info.height;
804 TRACE("(%p) <-- %ux%u\n", iface, *puiWidth, *puiHeight);
809 static HRESULT WINAPI TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
810 WICPixelFormatGUID *pPixelFormat)
812 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
814 memcpy(pPixelFormat, This->decode_info.format, sizeof(GUID));
816 TRACE("(%p) <-- %s\n", This, debugstr_guid(This->decode_info.format));
821 static HRESULT WINAPI TiffFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
822 double *pDpiX, double *pDpiY)
824 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
826 switch (This->decode_info.resolution_unit)
829 FIXME("unknown resolution unit %i\n", This->decode_info.resolution_unit);
831 case 0: /* Not set */
832 *pDpiX = *pDpiY = 96.0;
834 case 1: /* Relative measurements */
836 *pDpiY = 96.0 * This->decode_info.yres / This->decode_info.xres;
839 *pDpiX = This->decode_info.xres;
840 *pDpiY = This->decode_info.yres;
842 case 3: /* Centimeter */
843 *pDpiX = This->decode_info.xres / 2.54;
844 *pDpiY = This->decode_info.yres / 2.54;
848 TRACE("(%p) <-- %f,%f unit=%i\n", iface, *pDpiX, *pDpiY, This->decode_info.resolution_unit);
853 static HRESULT WINAPI TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
854 IWICPalette *pIPalette)
856 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
857 uint16 *red, *green, *blue;
858 WICColor colors[256];
859 int color_count, ret, i;
861 TRACE("(%p,%p)\n", iface, pIPalette);
863 color_count = 1<<This->decode_info.bps;
865 EnterCriticalSection(&This->parent->lock);
866 ret = pTIFFGetField(This->parent->tiff, TIFFTAG_COLORMAP, &red, &green, &blue);
867 LeaveCriticalSection(&This->parent->lock);
871 WARN("Couldn't read color map\n");
875 for (i=0; i<color_count; i++)
877 colors[i] = 0xff000000 |
878 ((red[i]<<8) & 0xff0000) |
879 (green[i] & 0xff00) |
880 ((blue[i]>>8) & 0xff);
883 return IWICPalette_InitializeCustom(pIPalette, colors, color_count);
886 static HRESULT TiffFrameDecode_ReadTile(TiffFrameDecode *This, UINT tile_x, UINT tile_y)
892 swap_bytes = pTIFFIsByteSwapped(This->parent->tiff);
894 ret = pTIFFSetDirectory(This->parent->tiff, This->index);
901 if (This->decode_info.tiled)
903 ret = pTIFFReadEncodedTile(This->parent->tiff, tile_x + tile_y * This->decode_info.tiles_across, This->cached_tile, This->decode_info.tile_size);
907 ret = pTIFFReadEncodedStrip(This->parent->tiff, tile_y, This->cached_tile, This->decode_info.tile_size);
914 if (hr == S_OK && This->decode_info.reverse_bgr)
916 if (This->decode_info.bps == 8)
918 UINT sample_count = This->decode_info.samples;
920 reverse_bgr8(sample_count, This->cached_tile, This->decode_info.tile_width,
921 This->decode_info.tile_height, This->decode_info.tile_width * sample_count);
925 if (hr == S_OK && swap_bytes && This->decode_info.bps > 8)
927 UINT row, i, samples_per_row;
930 samples_per_row = This->decode_info.tile_width * This->decode_info.samples;
932 switch(This->decode_info.bps)
935 for (row=0; row<This->decode_info.tile_height; row++)
937 sample = This->cached_tile + row * This->decode_info.tile_stride;
938 for (i=0; i<samples_per_row; i++)
941 sample[1] = sample[0];
948 ERR("unhandled bps for byte swap %u\n", This->decode_info.bps);
953 if (hr == S_OK && This->decode_info.invert_grayscale)
957 if (This->decode_info.samples != 1)
959 ERR("cannot invert grayscale image with %u samples\n", This->decode_info.samples);
963 end = This->cached_tile+This->decode_info.tile_size;
965 for (byte = This->cached_tile; byte != end; byte++)
971 This->cached_tile_x = tile_x;
972 This->cached_tile_y = tile_y;
978 static HRESULT WINAPI TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
979 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
981 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
982 UINT min_tile_x, max_tile_x, min_tile_y, max_tile_y;
990 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
996 rect.Width = This->decode_info.width;
997 rect.Height = This->decode_info.height;
1002 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->decode_info.width ||
1003 prc->Y+prc->Height > This->decode_info.height)
1004 return E_INVALIDARG;
1007 bytesperrow = ((This->decode_info.bpp * prc->Width)+7)/8;
1009 if (cbStride < bytesperrow)
1010 return E_INVALIDARG;
1012 if ((cbStride * prc->Height) > cbBufferSize)
1013 return E_INVALIDARG;
1015 min_tile_x = prc->X / This->decode_info.tile_width;
1016 min_tile_y = prc->Y / This->decode_info.tile_height;
1017 max_tile_x = (prc->X+prc->Width-1) / This->decode_info.tile_width;
1018 max_tile_y = (prc->Y+prc->Height-1) / This->decode_info.tile_height;
1020 EnterCriticalSection(&This->parent->lock);
1022 for (tile_x=min_tile_x; tile_x <= max_tile_x; tile_x++)
1024 for (tile_y=min_tile_y; tile_y <= max_tile_y; tile_y++)
1026 if (tile_x != This->cached_tile_x || tile_y != This->cached_tile_y)
1028 hr = TiffFrameDecode_ReadTile(This, tile_x, tile_y);
1033 if (prc->X < tile_x * This->decode_info.tile_width)
1036 rc.X = prc->X - tile_x * This->decode_info.tile_width;
1038 if (prc->Y < tile_y * This->decode_info.tile_height)
1041 rc.Y = prc->Y - tile_y * This->decode_info.tile_height;
1043 if (prc->X+prc->Width > (tile_x+1) * This->decode_info.tile_width)
1044 rc.Width = This->decode_info.tile_width - rc.X;
1045 else if (prc->X < tile_x * This->decode_info.tile_width)
1046 rc.Width = prc->Width + prc->X - tile_x * This->decode_info.tile_width;
1048 rc.Width = prc->Width;
1050 if (prc->Y+prc->Height > (tile_y+1) * This->decode_info.tile_height)
1051 rc.Height = This->decode_info.tile_height - rc.Y;
1052 else if (prc->Y < tile_y * This->decode_info.tile_height)
1053 rc.Height = prc->Height + prc->Y - tile_y * This->decode_info.tile_height;
1055 rc.Height = prc->Height;
1057 dst_tilepos = pbBuffer + (cbStride * ((rc.Y + tile_y * This->decode_info.tile_height) - prc->Y)) +
1058 ((This->decode_info.bpp * ((rc.X + tile_x * This->decode_info.tile_width) - prc->X) + 7) / 8);
1060 hr = copy_pixels(This->decode_info.bpp, This->cached_tile,
1061 This->decode_info.tile_width, This->decode_info.tile_height, This->decode_info.tile_stride,
1062 &rc, cbStride, cbBufferSize, dst_tilepos);
1067 LeaveCriticalSection(&This->parent->lock);
1068 TRACE("<-- 0x%x\n", hr);
1074 LeaveCriticalSection(&This->parent->lock);
1079 static HRESULT WINAPI TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
1080 IWICMetadataQueryReader **ppIMetadataQueryReader)
1082 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
1086 static HRESULT WINAPI TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
1087 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1089 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
1093 static HRESULT WINAPI TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
1094 IWICBitmapSource **ppIThumbnail)
1096 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
1100 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl = {
1101 TiffFrameDecode_QueryInterface,
1102 TiffFrameDecode_AddRef,
1103 TiffFrameDecode_Release,
1104 TiffFrameDecode_GetSize,
1105 TiffFrameDecode_GetPixelFormat,
1106 TiffFrameDecode_GetResolution,
1107 TiffFrameDecode_CopyPalette,
1108 TiffFrameDecode_CopyPixels,
1109 TiffFrameDecode_GetMetadataQueryReader,
1110 TiffFrameDecode_GetColorContexts,
1111 TiffFrameDecode_GetThumbnail
1114 static HRESULT WINAPI TiffFrameDecode_Block_QueryInterface(IWICMetadataBlockReader *iface,
1115 REFIID iid, void **ppv)
1117 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1118 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
1121 static ULONG WINAPI TiffFrameDecode_Block_AddRef(IWICMetadataBlockReader *iface)
1123 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1124 return IWICBitmapFrameDecode_AddRef(&This->IWICBitmapFrameDecode_iface);
1127 static ULONG WINAPI TiffFrameDecode_Block_Release(IWICMetadataBlockReader *iface)
1129 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1130 return IWICBitmapFrameDecode_Release(&This->IWICBitmapFrameDecode_iface);
1133 static HRESULT WINAPI TiffFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
1136 FIXME("(%p,%p): stub\n", iface, guid);
1140 static HRESULT WINAPI TiffFrameDecode_Block_GetCount(IWICMetadataBlockReader *iface,
1143 FIXME("(%p,%p): stub\n", iface, count);
1147 static HRESULT WINAPI TiffFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
1148 UINT index, IWICMetadataReader **reader)
1150 FIXME("(%p,%u,%p): stub\n", iface, index, reader);
1154 static HRESULT WINAPI TiffFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader *iface,
1155 IEnumUnknown **enum_metadata)
1157 FIXME("(%p,%p): stub\n", iface, enum_metadata);
1161 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl =
1163 TiffFrameDecode_Block_QueryInterface,
1164 TiffFrameDecode_Block_AddRef,
1165 TiffFrameDecode_Block_Release,
1166 TiffFrameDecode_Block_GetContainerFormat,
1167 TiffFrameDecode_Block_GetCount,
1168 TiffFrameDecode_Block_GetReaderByIndex,
1169 TiffFrameDecode_Block_GetEnumerator
1172 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1177 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1181 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1183 if (!load_libtiff())
1185 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF);
1189 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder));
1190 if (!This) return E_OUTOFMEMORY;
1192 This->IWICBitmapDecoder_iface.lpVtbl = &TiffDecoder_Vtbl;
1194 This->stream = NULL;
1195 InitializeCriticalSection(&This->lock);
1196 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffDecoder.lock");
1198 This->initialized = FALSE;
1200 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1201 IUnknown_Release((IUnknown*)This);
1206 struct tiff_encode_format {
1207 const WICPixelFormatGUID *guid;
1213 int extra_sample_type;
1217 static const struct tiff_encode_format formats[] = {
1218 {&GUID_WICPixelFormat24bppBGR, 2, 8, 3, 24, 0, 0, 1},
1219 {&GUID_WICPixelFormatBlackWhite, 1, 1, 1, 1, 0, 0, 0},
1220 {&GUID_WICPixelFormat4bppGray, 1, 4, 1, 4, 0, 0, 0},
1221 {&GUID_WICPixelFormat8bppGray, 1, 8, 1, 8, 0, 0, 0},
1222 {&GUID_WICPixelFormat32bppBGRA, 2, 8, 4, 32, 1, 2, 1},
1223 {&GUID_WICPixelFormat32bppPBGRA, 2, 8, 4, 32, 1, 1, 1},
1224 {&GUID_WICPixelFormat48bppRGB, 2, 16, 3, 48, 0, 0, 0},
1225 {&GUID_WICPixelFormat64bppRGBA, 2, 16, 4, 64, 1, 2, 0},
1226 {&GUID_WICPixelFormat64bppPRGBA, 2, 16, 4, 64, 1, 1, 0},
1230 typedef struct TiffEncoder {
1231 IWICBitmapEncoder IWICBitmapEncoder_iface;
1234 CRITICAL_SECTION lock; /* Must be held when tiff is used or fields below are set */
1239 ULONG num_frames_committed;
1242 static inline TiffEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1244 return CONTAINING_RECORD(iface, TiffEncoder, IWICBitmapEncoder_iface);
1247 typedef struct TiffFrameEncode {
1248 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1250 TiffEncoder *parent;
1251 /* fields below are protected by parent->lock */
1255 const struct tiff_encode_format *format;
1261 static inline TiffFrameEncode *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1263 return CONTAINING_RECORD(iface, TiffFrameEncode, IWICBitmapFrameEncode_iface);
1266 static HRESULT WINAPI TiffFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1269 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1270 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1272 if (!ppv) return E_INVALIDARG;
1274 if (IsEqualIID(&IID_IUnknown, iid) ||
1275 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1277 *ppv = &This->IWICBitmapFrameEncode_iface;
1282 return E_NOINTERFACE;
1285 IUnknown_AddRef((IUnknown*)*ppv);
1289 static ULONG WINAPI TiffFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1291 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1292 ULONG ref = InterlockedIncrement(&This->ref);
1294 TRACE("(%p) refcount=%u\n", iface, ref);
1299 static ULONG WINAPI TiffFrameEncode_Release(IWICBitmapFrameEncode *iface)
1301 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1302 ULONG ref = InterlockedDecrement(&This->ref);
1304 TRACE("(%p) refcount=%u\n", iface, ref);
1308 IWICBitmapEncoder_Release(&This->parent->IWICBitmapEncoder_iface);
1309 HeapFree(GetProcessHeap(), 0, This);
1315 static HRESULT WINAPI TiffFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1316 IPropertyBag2 *pIEncoderOptions)
1318 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1319 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1321 EnterCriticalSection(&This->parent->lock);
1323 if (This->initialized)
1325 LeaveCriticalSection(&This->parent->lock);
1326 return WINCODEC_ERR_WRONGSTATE;
1329 This->initialized = TRUE;
1331 LeaveCriticalSection(&This->parent->lock);
1336 static HRESULT WINAPI TiffFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1337 UINT uiWidth, UINT uiHeight)
1339 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1340 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1342 EnterCriticalSection(&This->parent->lock);
1344 if (!This->initialized || This->info_written)
1346 LeaveCriticalSection(&This->parent->lock);
1347 return WINCODEC_ERR_WRONGSTATE;
1350 This->width = uiWidth;
1351 This->height = uiHeight;
1353 LeaveCriticalSection(&This->parent->lock);
1358 static HRESULT WINAPI TiffFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1359 double dpiX, double dpiY)
1361 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1362 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1364 EnterCriticalSection(&This->parent->lock);
1366 if (!This->initialized || This->info_written)
1368 LeaveCriticalSection(&This->parent->lock);
1369 return WINCODEC_ERR_WRONGSTATE;
1375 LeaveCriticalSection(&This->parent->lock);
1380 static HRESULT WINAPI TiffFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1381 WICPixelFormatGUID *pPixelFormat)
1383 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1386 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1388 EnterCriticalSection(&This->parent->lock);
1390 if (!This->initialized || This->info_written)
1392 LeaveCriticalSection(&This->parent->lock);
1393 return WINCODEC_ERR_WRONGSTATE;
1396 for (i=0; formats[i].guid; i++)
1398 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1402 if (!formats[i].guid) i = 0;
1404 This->format = &formats[i];
1405 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1407 LeaveCriticalSection(&This->parent->lock);
1412 static HRESULT WINAPI TiffFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1413 UINT cCount, IWICColorContext **ppIColorContext)
1415 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1419 static HRESULT WINAPI TiffFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1420 IWICPalette *pIPalette)
1422 FIXME("(%p,%p): stub\n", iface, pIPalette);
1423 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1426 static HRESULT WINAPI TiffFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1427 IWICBitmapSource *pIThumbnail)
1429 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1430 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1433 static HRESULT WINAPI TiffFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1434 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1436 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1437 BYTE *row_data, *swapped_data = NULL;
1438 UINT i, j, line_size;
1440 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1442 EnterCriticalSection(&This->parent->lock);
1444 if (!This->initialized || !This->width || !This->height || !This->format)
1446 LeaveCriticalSection(&This->parent->lock);
1447 return WINCODEC_ERR_WRONGSTATE;
1450 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1452 LeaveCriticalSection(&This->parent->lock);
1453 return E_INVALIDARG;
1456 line_size = ((This->width * This->format->bpp)+7)/8;
1458 if (This->format->reverse_bgr)
1460 swapped_data = HeapAlloc(GetProcessHeap(), 0, line_size);
1463 LeaveCriticalSection(&This->parent->lock);
1464 return E_OUTOFMEMORY;
1468 if (!This->info_written)
1470 pTIFFSetField(This->parent->tiff, TIFFTAG_PHOTOMETRIC, (uint16)This->format->photometric);
1471 pTIFFSetField(This->parent->tiff, TIFFTAG_PLANARCONFIG, (uint16)1);
1472 pTIFFSetField(This->parent->tiff, TIFFTAG_BITSPERSAMPLE, (uint16)This->format->bps);
1473 pTIFFSetField(This->parent->tiff, TIFFTAG_SAMPLESPERPIXEL, (uint16)This->format->samples);
1475 if (This->format->extra_sample)
1477 uint16 extra_samples;
1478 extra_samples = This->format->extra_sample_type;
1480 pTIFFSetField(This->parent->tiff, TIFFTAG_EXTRASAMPLES, (uint16)1, &extra_samples);
1483 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGEWIDTH, (uint32)This->width);
1484 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGELENGTH, (uint32)This->height);
1486 if (This->xres != 0.0 && This->yres != 0.0)
1488 pTIFFSetField(This->parent->tiff, TIFFTAG_RESOLUTIONUNIT, (uint16)2); /* Inch */
1489 pTIFFSetField(This->parent->tiff, TIFFTAG_XRESOLUTION, (float)This->xres);
1490 pTIFFSetField(This->parent->tiff, TIFFTAG_YRESOLUTION, (float)This->yres);
1493 This->info_written = TRUE;
1496 for (i=0; i<lineCount; i++)
1498 row_data = pbPixels + i * cbStride;
1500 if (This->format->reverse_bgr && This->format->bps == 8)
1502 memcpy(swapped_data, row_data, line_size);
1503 for (j=0; j<line_size; j += This->format->samples)
1506 temp = swapped_data[j];
1507 swapped_data[j] = swapped_data[j+2];
1508 swapped_data[j+2] = temp;
1510 row_data = swapped_data;
1513 pTIFFWriteScanline(This->parent->tiff, (tdata_t)row_data, i+This->lines_written, 0);
1516 This->lines_written += lineCount;
1518 LeaveCriticalSection(&This->parent->lock);
1520 HeapFree(GetProcessHeap(), 0, swapped_data);
1525 static HRESULT WINAPI TiffFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1526 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1528 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1531 WICPixelFormatGUID guid;
1535 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1537 if (!This->initialized || !This->width || !This->height)
1538 return WINCODEC_ERR_WRONGSTATE;
1542 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1543 if (FAILED(hr)) return hr;
1544 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1545 if (FAILED(hr)) return hr;
1548 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1549 if (FAILED(hr)) return hr;
1550 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1552 /* FIXME: should use WICConvertBitmapSource to convert */
1553 ERR("format %s unsupported\n", debugstr_guid(&guid));
1557 if (This->xres == 0.0 || This->yres == 0.0)
1560 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1561 if (FAILED(hr)) return hr;
1562 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1563 if (FAILED(hr)) return hr;
1569 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1570 if (FAILED(hr)) return hr;
1578 if (prc->Width != This->width) return E_INVALIDARG;
1580 stride = (This->format->bpp * This->width + 7)/8;
1582 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1583 if (!pixeldata) return E_OUTOFMEMORY;
1585 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1586 stride*prc->Height, pixeldata);
1590 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1591 stride*prc->Height, pixeldata);
1594 HeapFree(GetProcessHeap(), 0, pixeldata);
1599 static HRESULT WINAPI TiffFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1601 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1603 TRACE("(%p)\n", iface);
1605 EnterCriticalSection(&This->parent->lock);
1607 if (!This->info_written || This->lines_written != This->height || This->committed)
1609 LeaveCriticalSection(&This->parent->lock);
1610 return WINCODEC_ERR_WRONGSTATE;
1613 /* libtiff will commit the data when creating a new frame or closing the file */
1615 This->committed = TRUE;
1616 This->parent->num_frames_committed++;
1618 LeaveCriticalSection(&This->parent->lock);
1623 static HRESULT WINAPI TiffFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1624 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1626 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1630 static const IWICBitmapFrameEncodeVtbl TiffFrameEncode_Vtbl = {
1631 TiffFrameEncode_QueryInterface,
1632 TiffFrameEncode_AddRef,
1633 TiffFrameEncode_Release,
1634 TiffFrameEncode_Initialize,
1635 TiffFrameEncode_SetSize,
1636 TiffFrameEncode_SetResolution,
1637 TiffFrameEncode_SetPixelFormat,
1638 TiffFrameEncode_SetColorContexts,
1639 TiffFrameEncode_SetPalette,
1640 TiffFrameEncode_SetThumbnail,
1641 TiffFrameEncode_WritePixels,
1642 TiffFrameEncode_WriteSource,
1643 TiffFrameEncode_Commit,
1644 TiffFrameEncode_GetMetadataQueryWriter
1647 static HRESULT WINAPI TiffEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1650 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1651 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1653 if (!ppv) return E_INVALIDARG;
1655 if (IsEqualIID(&IID_IUnknown, iid) ||
1656 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1663 return E_NOINTERFACE;
1666 IUnknown_AddRef((IUnknown*)*ppv);
1670 static ULONG WINAPI TiffEncoder_AddRef(IWICBitmapEncoder *iface)
1672 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1673 ULONG ref = InterlockedIncrement(&This->ref);
1675 TRACE("(%p) refcount=%u\n", iface, ref);
1680 static ULONG WINAPI TiffEncoder_Release(IWICBitmapEncoder *iface)
1682 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1683 ULONG ref = InterlockedDecrement(&This->ref);
1685 TRACE("(%p) refcount=%u\n", iface, ref);
1689 if (This->tiff) pTIFFClose(This->tiff);
1690 if (This->stream) IStream_Release(This->stream);
1691 This->lock.DebugInfo->Spare[0] = 0;
1692 DeleteCriticalSection(&This->lock);
1693 HeapFree(GetProcessHeap(), 0, This);
1699 static HRESULT WINAPI TiffEncoder_Initialize(IWICBitmapEncoder *iface,
1700 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1702 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1706 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1708 EnterCriticalSection(&This->lock);
1710 if (This->initialized || This->committed)
1712 hr = WINCODEC_ERR_WRONGSTATE;
1716 tiff = tiff_open_stream(pIStream, "w");
1725 This->stream = pIStream;
1726 IStream_AddRef(pIStream);
1727 This->initialized = TRUE;
1730 LeaveCriticalSection(&This->lock);
1734 static HRESULT WINAPI TiffEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1735 GUID *pguidContainerFormat)
1737 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
1741 static HRESULT WINAPI TiffEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1742 IWICBitmapEncoderInfo **ppIEncoderInfo)
1744 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1748 static HRESULT WINAPI TiffEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1749 UINT cCount, IWICColorContext **ppIColorContext)
1751 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1755 static HRESULT WINAPI TiffEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1757 TRACE("(%p,%p)\n", iface, pIPalette);
1758 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1761 static HRESULT WINAPI TiffEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1763 TRACE("(%p,%p)\n", iface, pIThumbnail);
1764 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1767 static HRESULT WINAPI TiffEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1769 TRACE("(%p,%p)\n", iface, pIPreview);
1770 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1773 static HRESULT WINAPI TiffEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1774 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1776 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1777 TiffFrameEncode *result;
1781 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1783 EnterCriticalSection(&This->lock);
1785 if (!This->initialized || This->committed)
1787 hr = WINCODEC_ERR_WRONGSTATE;
1789 else if (This->num_frames != This->num_frames_committed)
1791 FIXME("New frame created before previous frame was committed\n");
1797 hr = CreatePropertyBag2(ppIEncoderOptions);
1802 result = HeapAlloc(GetProcessHeap(), 0, sizeof(*result));
1806 result->IWICBitmapFrameEncode_iface.lpVtbl = &TiffFrameEncode_Vtbl;
1808 result->parent = This;
1809 result->initialized = FALSE;
1810 result->info_written = FALSE;
1811 result->committed = FALSE;
1812 result->format = NULL;
1817 result->lines_written = 0;
1819 IWICBitmapEncoder_AddRef(iface);
1820 *ppIFrameEncode = &result->IWICBitmapFrameEncode_iface;
1822 if (This->num_frames != 0)
1823 pTIFFWriteDirectory(This->tiff);
1832 IPropertyBag2_Release(*ppIEncoderOptions);
1833 *ppIEncoderOptions = NULL;
1837 LeaveCriticalSection(&This->lock);
1842 static HRESULT WINAPI TiffEncoder_Commit(IWICBitmapEncoder *iface)
1844 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1846 TRACE("(%p)\n", iface);
1848 EnterCriticalSection(&This->lock);
1850 if (!This->initialized || This->committed)
1852 LeaveCriticalSection(&This->lock);
1853 return WINCODEC_ERR_WRONGSTATE;
1856 pTIFFClose(This->tiff);
1857 IStream_Release(This->stream);
1858 This->stream = NULL;
1861 This->committed = TRUE;
1863 LeaveCriticalSection(&This->lock);
1868 static HRESULT WINAPI TiffEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1869 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1871 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1875 static const IWICBitmapEncoderVtbl TiffEncoder_Vtbl = {
1876 TiffEncoder_QueryInterface,
1878 TiffEncoder_Release,
1879 TiffEncoder_Initialize,
1880 TiffEncoder_GetContainerFormat,
1881 TiffEncoder_GetEncoderInfo,
1882 TiffEncoder_SetColorContexts,
1883 TiffEncoder_SetPalette,
1884 TiffEncoder_SetThumbnail,
1885 TiffEncoder_SetPreview,
1886 TiffEncoder_CreateNewFrame,
1888 TiffEncoder_GetMetadataQueryWriter
1891 HRESULT TiffEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1896 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1900 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1902 if (!load_libtiff())
1904 ERR("Failed writing TIFF because unable to load %s\n",SONAME_LIBTIFF);
1908 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffEncoder));
1909 if (!This) return E_OUTOFMEMORY;
1911 This->IWICBitmapEncoder_iface.lpVtbl = &TiffEncoder_Vtbl;
1913 This->stream = NULL;
1914 InitializeCriticalSection(&This->lock);
1915 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffEncoder.lock");
1917 This->initialized = FALSE;
1918 This->num_frames = 0;
1919 This->num_frames_committed = 0;
1920 This->committed = FALSE;
1922 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1923 IUnknown_Release((IUnknown*)This);
1928 #else /* !SONAME_LIBTIFF */
1930 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1932 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
1936 HRESULT TiffEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1938 ERR("Trying to save TIFF picture, but Wine was compiled without TIFF support.\n");