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(TIFFCurrentDirOffset);
62 MAKE_FUNCPTR(TIFFGetField);
63 MAKE_FUNCPTR(TIFFIsByteSwapped);
64 MAKE_FUNCPTR(TIFFNumberOfDirectories);
65 MAKE_FUNCPTR(TIFFReadDirectory);
66 MAKE_FUNCPTR(TIFFReadEncodedStrip);
67 MAKE_FUNCPTR(TIFFReadEncodedTile);
68 MAKE_FUNCPTR(TIFFSetDirectory);
69 MAKE_FUNCPTR(TIFFSetField);
70 MAKE_FUNCPTR(TIFFWriteDirectory);
71 MAKE_FUNCPTR(TIFFWriteScanline);
74 static void *load_libtiff(void)
78 EnterCriticalSection(&init_tiff_cs);
80 if (!libtiff_handle &&
81 (libtiff_handle = wine_dlopen(SONAME_LIBTIFF, RTLD_NOW, NULL, 0)) != NULL)
83 void * (*pTIFFSetWarningHandler)(void *);
84 void * (*pTIFFSetWarningHandlerExt)(void *);
86 #define LOAD_FUNCPTR(f) \
87 if((p##f = wine_dlsym(libtiff_handle, #f, NULL, 0)) == NULL) { \
88 ERR("failed to load symbol %s\n", #f); \
89 libtiff_handle = NULL; \
90 LeaveCriticalSection(&init_tiff_cs); \
93 LOAD_FUNCPTR(TIFFClientOpen);
94 LOAD_FUNCPTR(TIFFClose);
95 LOAD_FUNCPTR(TIFFCurrentDirOffset);
96 LOAD_FUNCPTR(TIFFGetField);
97 LOAD_FUNCPTR(TIFFIsByteSwapped);
98 LOAD_FUNCPTR(TIFFNumberOfDirectories);
99 LOAD_FUNCPTR(TIFFReadDirectory);
100 LOAD_FUNCPTR(TIFFReadEncodedStrip);
101 LOAD_FUNCPTR(TIFFReadEncodedTile);
102 LOAD_FUNCPTR(TIFFSetDirectory);
103 LOAD_FUNCPTR(TIFFSetField);
104 LOAD_FUNCPTR(TIFFWriteDirectory);
105 LOAD_FUNCPTR(TIFFWriteScanline);
108 if ((pTIFFSetWarningHandler = wine_dlsym(libtiff_handle, "TIFFSetWarningHandler", NULL, 0)))
109 pTIFFSetWarningHandler(NULL);
110 if ((pTIFFSetWarningHandlerExt = wine_dlsym(libtiff_handle, "TIFFSetWarningHandlerExt", NULL, 0)))
111 pTIFFSetWarningHandlerExt(NULL);
114 result = libtiff_handle;
116 LeaveCriticalSection(&init_tiff_cs);
120 static tsize_t tiff_stream_read(thandle_t client_data, tdata_t data, tsize_t size)
122 IStream *stream = (IStream*)client_data;
126 hr = IStream_Read(stream, data, size, &bytes_read);
127 if (FAILED(hr)) bytes_read = 0;
131 static tsize_t tiff_stream_write(thandle_t client_data, tdata_t data, tsize_t size)
133 IStream *stream = (IStream*)client_data;
137 hr = IStream_Write(stream, data, size, &bytes_written);
138 if (FAILED(hr)) bytes_written = 0;
139 return bytes_written;
142 static toff_t tiff_stream_seek(thandle_t client_data, toff_t offset, int whence)
144 IStream *stream = (IStream*)client_data;
147 ULARGE_INTEGER new_position;
150 move.QuadPart = offset;
154 origin = STREAM_SEEK_SET;
157 origin = STREAM_SEEK_CUR;
160 origin = STREAM_SEEK_END;
163 ERR("unknown whence value %i\n", whence);
167 hr = IStream_Seek(stream, move, origin, &new_position);
168 if (SUCCEEDED(hr)) return new_position.QuadPart;
172 static int tiff_stream_close(thandle_t client_data)
174 /* Caller is responsible for releasing the stream object. */
178 static toff_t tiff_stream_size(thandle_t client_data)
180 IStream *stream = (IStream*)client_data;
184 hr = IStream_Stat(stream, &statstg, STATFLAG_NONAME);
186 if (SUCCEEDED(hr)) return statstg.cbSize.QuadPart;
190 static int tiff_stream_map(thandle_t client_data, tdata_t *addr, toff_t *size)
192 /* Cannot mmap streams */
196 static void tiff_stream_unmap(thandle_t client_data, tdata_t addr, toff_t size)
198 /* No need to ever do this, since we can't map things. */
201 static TIFF* tiff_open_stream(IStream *stream, const char *mode)
206 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
208 return pTIFFClientOpen("<IStream object>", mode, stream, tiff_stream_read,
209 tiff_stream_write, tiff_stream_seek, tiff_stream_close,
210 tiff_stream_size, tiff_stream_map, tiff_stream_unmap);
214 IWICBitmapDecoder IWICBitmapDecoder_iface;
217 CRITICAL_SECTION lock; /* Must be held when tiff is used or initiailzed is set */
223 const WICPixelFormatGUID *format;
230 int invert_grayscale;
232 UINT tile_width, tile_height;
237 UINT resolution_unit;
242 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
243 IWICMetadataBlockReader IWICMetadataBlockReader_iface;
247 tiff_decode_info decode_info;
248 INT cached_tile_x, cached_tile_y;
252 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl;
253 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl;
255 static inline TiffDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
257 return CONTAINING_RECORD(iface, TiffDecoder, IWICBitmapDecoder_iface);
260 static inline TiffFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
262 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICBitmapFrameDecode_iface);
265 static inline TiffFrameDecode *impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader *iface)
267 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICMetadataBlockReader_iface);
270 static HRESULT tiff_get_decode_info(TIFF *tiff, tiff_decode_info *decode_info)
272 uint16 photometric, bps, samples, planar;
273 uint16 extra_sample_count, extra_sample, *extra_samples;
276 decode_info->indexed = 0;
277 decode_info->reverse_bgr = 0;
278 decode_info->invert_grayscale = 0;
279 decode_info->tiled = 0;
281 ret = pTIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
284 WARN("missing PhotometricInterpretation tag\n");
288 ret = pTIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bps);
290 decode_info->bps = bps;
292 ret = pTIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samples);
293 if (!ret) samples = 1;
294 decode_info->samples = samples;
300 ret = pTIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planar);
301 if (!ret) planar = 1;
304 FIXME("unhandled planar configuration %u\n", planar);
308 decode_info->planar = planar;
312 case 0: /* WhiteIsZero */
313 decode_info->invert_grayscale = 1;
315 case 1: /* BlackIsZero */
318 FIXME("unhandled grayscale sample count %u\n", samples);
322 decode_info->bpp = bps;
326 decode_info->format = &GUID_WICPixelFormatBlackWhite;
329 decode_info->format = &GUID_WICPixelFormat4bppGray;
332 decode_info->format = &GUID_WICPixelFormat8bppGray;
335 FIXME("unhandled greyscale bit count %u\n", bps);
340 decode_info->bpp = bps * samples;
344 ret = pTIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extra_sample_count, &extra_samples);
347 extra_sample_count = 1;
349 extra_samples = &extra_sample;
352 else if (samples != 3)
354 FIXME("unhandled RGB sample count %u\n", samples);
361 decode_info->reverse_bgr = 1;
363 decode_info->format = &GUID_WICPixelFormat24bppBGR;
365 switch(extra_samples[0])
367 case 1: /* Associated (pre-multiplied) alpha data */
368 decode_info->format = &GUID_WICPixelFormat32bppPBGRA;
370 case 0: /* Unspecified data */
371 case 2: /* Unassociated alpha data */
372 decode_info->format = &GUID_WICPixelFormat32bppBGRA;
375 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
381 decode_info->format = &GUID_WICPixelFormat48bppRGB;
383 switch(extra_samples[0])
385 case 1: /* Associated (pre-multiplied) alpha data */
386 decode_info->format = &GUID_WICPixelFormat64bppPRGBA;
388 case 0: /* Unspecified data */
389 case 2: /* Unassociated alpha data */
390 decode_info->format = &GUID_WICPixelFormat64bppRGBA;
393 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
398 FIXME("unhandled RGB bit count %u\n", bps);
402 case 3: /* RGB Palette */
405 FIXME("unhandled indexed sample count %u\n", samples);
409 decode_info->indexed = 1;
410 decode_info->bpp = bps;
414 decode_info->format = &GUID_WICPixelFormat4bppIndexed;
417 decode_info->format = &GUID_WICPixelFormat8bppIndexed;
420 FIXME("unhandled indexed bit count %u\n", bps);
424 case 4: /* Transparency mask */
429 FIXME("unhandled PhotometricInterpretation %u\n", photometric);
433 ret = pTIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &decode_info->width);
436 WARN("missing image width\n");
440 ret = pTIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &decode_info->height);
443 WARN("missing image length\n");
447 if ((ret = pTIFFGetField(tiff, TIFFTAG_TILEWIDTH, &decode_info->tile_width)))
449 decode_info->tiled = 1;
451 ret = pTIFFGetField(tiff, TIFFTAG_TILELENGTH, &decode_info->tile_height);
454 WARN("missing tile height\n");
458 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
459 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
460 decode_info->tiles_across = (decode_info->width + decode_info->tile_width - 1) / decode_info->tile_width;
462 else if ((ret = pTIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &decode_info->tile_height)))
464 if (decode_info->tile_height > decode_info->height)
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;
472 /* Some broken TIFF files have a single strip and lack the RowsPerStrip tag */
473 decode_info->tile_height = decode_info->height;
474 decode_info->tile_width = decode_info->width;
475 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
476 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
479 decode_info->resolution_unit = 0;
480 pTIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &decode_info->resolution_unit);
481 if (decode_info->resolution_unit != 0)
483 ret = pTIFFGetField(tiff, TIFFTAG_XRESOLUTION, &decode_info->xres);
486 WARN("missing X resolution\n");
487 decode_info->resolution_unit = 0;
490 ret = pTIFFGetField(tiff, TIFFTAG_YRESOLUTION, &decode_info->yres);
493 WARN("missing Y resolution\n");
494 decode_info->resolution_unit = 0;
501 static HRESULT WINAPI TiffDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
504 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
505 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
507 if (!ppv) return E_INVALIDARG;
509 if (IsEqualIID(&IID_IUnknown, iid) ||
510 IsEqualIID(&IID_IWICBitmapDecoder, iid))
512 *ppv = &This->IWICBitmapDecoder_iface;
517 return E_NOINTERFACE;
520 IUnknown_AddRef((IUnknown*)*ppv);
524 static ULONG WINAPI TiffDecoder_AddRef(IWICBitmapDecoder *iface)
526 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
527 ULONG ref = InterlockedIncrement(&This->ref);
529 TRACE("(%p) refcount=%u\n", iface, ref);
534 static ULONG WINAPI TiffDecoder_Release(IWICBitmapDecoder *iface)
536 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
537 ULONG ref = InterlockedDecrement(&This->ref);
539 TRACE("(%p) refcount=%u\n", iface, ref);
543 if (This->tiff) pTIFFClose(This->tiff);
544 if (This->stream) IStream_Release(This->stream);
545 This->lock.DebugInfo->Spare[0] = 0;
546 DeleteCriticalSection(&This->lock);
547 HeapFree(GetProcessHeap(), 0, This);
553 static HRESULT WINAPI TiffDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
558 TRACE("(%p,%p,%p)\n", iface, stream, capability);
560 if (!stream || !capability) return E_INVALIDARG;
562 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
563 if (hr != S_OK) return hr;
565 *capability = WICBitmapDecoderCapabilityCanDecodeAllImages |
566 WICBitmapDecoderCapabilityCanDecodeSomeImages |
567 WICBitmapDecoderCapabilityCanEnumerateMetadata;
571 static HRESULT WINAPI TiffDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
572 WICDecodeOptions cacheOptions)
574 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
578 TRACE("(%p,%p,%x): stub\n", iface, pIStream, cacheOptions);
580 EnterCriticalSection(&This->lock);
582 if (This->initialized)
584 hr = WINCODEC_ERR_WRONGSTATE;
588 tiff = tiff_open_stream(pIStream, "r");
597 This->stream = pIStream;
598 IStream_AddRef(pIStream);
599 This->initialized = TRUE;
602 LeaveCriticalSection(&This->lock);
606 static HRESULT WINAPI TiffDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
607 GUID *pguidContainerFormat)
609 if (!pguidContainerFormat) return E_INVALIDARG;
611 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
615 static HRESULT WINAPI TiffDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
616 IWICBitmapDecoderInfo **ppIDecoderInfo)
619 IWICComponentInfo *compinfo;
621 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
623 hr = CreateComponentInfo(&CLSID_WICTiffDecoder, &compinfo);
624 if (FAILED(hr)) return hr;
626 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
627 (void**)ppIDecoderInfo);
629 IWICComponentInfo_Release(compinfo);
634 static HRESULT WINAPI TiffDecoder_CopyPalette(IWICBitmapDecoder *iface,
635 IWICPalette *pIPalette)
637 FIXME("(%p,%p): stub\n", iface, pIPalette);
641 static HRESULT WINAPI TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
642 IWICMetadataQueryReader **ppIMetadataQueryReader)
644 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
648 static HRESULT WINAPI TiffDecoder_GetPreview(IWICBitmapDecoder *iface,
649 IWICBitmapSource **ppIBitmapSource)
651 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
653 if (!ppIBitmapSource) return E_INVALIDARG;
655 *ppIBitmapSource = NULL;
656 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
659 static HRESULT WINAPI TiffDecoder_GetColorContexts(IWICBitmapDecoder *iface,
660 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
662 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
663 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
666 static HRESULT WINAPI TiffDecoder_GetThumbnail(IWICBitmapDecoder *iface,
667 IWICBitmapSource **ppIThumbnail)
669 TRACE("(%p,%p)\n", iface, ppIThumbnail);
671 if (!ppIThumbnail) return E_INVALIDARG;
673 *ppIThumbnail = NULL;
674 return WINCODEC_ERR_CODECNOTHUMBNAIL;
677 static HRESULT WINAPI TiffDecoder_GetFrameCount(IWICBitmapDecoder *iface,
680 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
682 if (!pCount) return E_INVALIDARG;
684 EnterCriticalSection(&This->lock);
685 *pCount = This->tiff ? pTIFFNumberOfDirectories(This->tiff) : 0;
686 LeaveCriticalSection(&This->lock);
688 TRACE("(%p) <-- %i\n", iface, *pCount);
693 static HRESULT WINAPI TiffDecoder_GetFrame(IWICBitmapDecoder *iface,
694 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
696 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
697 TiffFrameDecode *result;
699 tiff_decode_info decode_info;
702 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
705 return WINCODEC_ERR_FRAMEMISSING;
707 EnterCriticalSection(&This->lock);
708 res = pTIFFSetDirectory(This->tiff, index);
709 if (!res) hr = E_INVALIDARG;
710 else hr = tiff_get_decode_info(This->tiff, &decode_info);
711 LeaveCriticalSection(&This->lock);
715 result = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode));
719 result->IWICBitmapFrameDecode_iface.lpVtbl = &TiffFrameDecode_Vtbl;
720 result->IWICMetadataBlockReader_iface.lpVtbl = &TiffFrameDecode_BlockVtbl;
722 result->parent = This;
723 result->index = index;
724 result->decode_info = decode_info;
725 result->cached_tile_x = -1;
726 result->cached_tile = HeapAlloc(GetProcessHeap(), 0, decode_info.tile_size);
728 if (result->cached_tile)
729 *ppIBitmapFrame = &result->IWICBitmapFrameDecode_iface;
733 HeapFree(GetProcessHeap(), 0, result);
736 else hr = E_OUTOFMEMORY;
739 if (FAILED(hr)) *ppIBitmapFrame = NULL;
744 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl = {
745 TiffDecoder_QueryInterface,
748 TiffDecoder_QueryCapability,
749 TiffDecoder_Initialize,
750 TiffDecoder_GetContainerFormat,
751 TiffDecoder_GetDecoderInfo,
752 TiffDecoder_CopyPalette,
753 TiffDecoder_GetMetadataQueryReader,
754 TiffDecoder_GetPreview,
755 TiffDecoder_GetColorContexts,
756 TiffDecoder_GetThumbnail,
757 TiffDecoder_GetFrameCount,
761 static HRESULT WINAPI TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
764 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
765 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
767 if (!ppv) return E_INVALIDARG;
769 if (IsEqualIID(&IID_IUnknown, iid) ||
770 IsEqualIID(&IID_IWICBitmapSource, iid) ||
771 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
773 *ppv = &This->IWICBitmapFrameDecode_iface;
775 else if (IsEqualIID(&IID_IWICMetadataBlockReader, iid))
777 *ppv = &This->IWICMetadataBlockReader_iface;
782 return E_NOINTERFACE;
785 IUnknown_AddRef((IUnknown*)*ppv);
789 static ULONG WINAPI TiffFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
791 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
792 ULONG ref = InterlockedIncrement(&This->ref);
794 TRACE("(%p) refcount=%u\n", iface, ref);
799 static ULONG WINAPI TiffFrameDecode_Release(IWICBitmapFrameDecode *iface)
801 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
802 ULONG ref = InterlockedDecrement(&This->ref);
804 TRACE("(%p) refcount=%u\n", iface, ref);
808 HeapFree(GetProcessHeap(), 0, This->cached_tile);
809 HeapFree(GetProcessHeap(), 0, This);
815 static HRESULT WINAPI TiffFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
816 UINT *puiWidth, UINT *puiHeight)
818 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
820 *puiWidth = This->decode_info.width;
821 *puiHeight = This->decode_info.height;
823 TRACE("(%p) <-- %ux%u\n", iface, *puiWidth, *puiHeight);
828 static HRESULT WINAPI TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
829 WICPixelFormatGUID *pPixelFormat)
831 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
833 memcpy(pPixelFormat, This->decode_info.format, sizeof(GUID));
835 TRACE("(%p) <-- %s\n", This, debugstr_guid(This->decode_info.format));
840 static HRESULT WINAPI TiffFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
841 double *pDpiX, double *pDpiY)
843 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
845 switch (This->decode_info.resolution_unit)
848 FIXME("unknown resolution unit %i\n", This->decode_info.resolution_unit);
850 case 0: /* Not set */
851 *pDpiX = *pDpiY = 96.0;
853 case 1: /* Relative measurements */
855 *pDpiY = 96.0 * This->decode_info.yres / This->decode_info.xres;
858 *pDpiX = This->decode_info.xres;
859 *pDpiY = This->decode_info.yres;
861 case 3: /* Centimeter */
862 *pDpiX = This->decode_info.xres / 2.54;
863 *pDpiY = This->decode_info.yres / 2.54;
867 TRACE("(%p) <-- %f,%f unit=%i\n", iface, *pDpiX, *pDpiY, This->decode_info.resolution_unit);
872 static HRESULT WINAPI TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
873 IWICPalette *pIPalette)
875 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
876 uint16 *red, *green, *blue;
877 WICColor colors[256];
878 int color_count, ret, i;
880 TRACE("(%p,%p)\n", iface, pIPalette);
882 color_count = 1<<This->decode_info.bps;
884 EnterCriticalSection(&This->parent->lock);
885 ret = pTIFFGetField(This->parent->tiff, TIFFTAG_COLORMAP, &red, &green, &blue);
886 LeaveCriticalSection(&This->parent->lock);
890 WARN("Couldn't read color map\n");
891 return WINCODEC_ERR_PALETTEUNAVAILABLE;
894 for (i=0; i<color_count; i++)
896 colors[i] = 0xff000000 |
897 ((red[i]<<8) & 0xff0000) |
898 (green[i] & 0xff00) |
899 ((blue[i]>>8) & 0xff);
902 return IWICPalette_InitializeCustom(pIPalette, colors, color_count);
905 static HRESULT TiffFrameDecode_ReadTile(TiffFrameDecode *This, UINT tile_x, UINT tile_y)
911 swap_bytes = pTIFFIsByteSwapped(This->parent->tiff);
913 ret = pTIFFSetDirectory(This->parent->tiff, This->index);
920 if (This->decode_info.tiled)
922 ret = pTIFFReadEncodedTile(This->parent->tiff, tile_x + tile_y * This->decode_info.tiles_across, This->cached_tile, This->decode_info.tile_size);
926 ret = pTIFFReadEncodedStrip(This->parent->tiff, tile_y, This->cached_tile, This->decode_info.tile_size);
933 if (hr == S_OK && This->decode_info.reverse_bgr)
935 if (This->decode_info.bps == 8)
937 UINT sample_count = This->decode_info.samples;
939 reverse_bgr8(sample_count, This->cached_tile, This->decode_info.tile_width,
940 This->decode_info.tile_height, This->decode_info.tile_width * sample_count);
944 if (hr == S_OK && swap_bytes && This->decode_info.bps > 8)
946 UINT row, i, samples_per_row;
949 samples_per_row = This->decode_info.tile_width * This->decode_info.samples;
951 switch(This->decode_info.bps)
954 for (row=0; row<This->decode_info.tile_height; row++)
956 sample = This->cached_tile + row * This->decode_info.tile_stride;
957 for (i=0; i<samples_per_row; i++)
960 sample[1] = sample[0];
967 ERR("unhandled bps for byte swap %u\n", This->decode_info.bps);
972 if (hr == S_OK && This->decode_info.invert_grayscale)
976 if (This->decode_info.samples != 1)
978 ERR("cannot invert grayscale image with %u samples\n", This->decode_info.samples);
982 end = This->cached_tile+This->decode_info.tile_size;
984 for (byte = This->cached_tile; byte != end; byte++)
990 This->cached_tile_x = tile_x;
991 This->cached_tile_y = tile_y;
997 static HRESULT WINAPI TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
998 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
1000 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
1001 UINT min_tile_x, max_tile_x, min_tile_y, max_tile_y;
1002 UINT tile_x, tile_y;
1009 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
1015 rect.Width = This->decode_info.width;
1016 rect.Height = This->decode_info.height;
1021 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->decode_info.width ||
1022 prc->Y+prc->Height > This->decode_info.height)
1023 return E_INVALIDARG;
1026 bytesperrow = ((This->decode_info.bpp * prc->Width)+7)/8;
1028 if (cbStride < bytesperrow)
1029 return E_INVALIDARG;
1031 if ((cbStride * prc->Height) > cbBufferSize)
1032 return E_INVALIDARG;
1034 min_tile_x = prc->X / This->decode_info.tile_width;
1035 min_tile_y = prc->Y / This->decode_info.tile_height;
1036 max_tile_x = (prc->X+prc->Width-1) / This->decode_info.tile_width;
1037 max_tile_y = (prc->Y+prc->Height-1) / This->decode_info.tile_height;
1039 EnterCriticalSection(&This->parent->lock);
1041 for (tile_x=min_tile_x; tile_x <= max_tile_x; tile_x++)
1043 for (tile_y=min_tile_y; tile_y <= max_tile_y; tile_y++)
1045 if (tile_x != This->cached_tile_x || tile_y != This->cached_tile_y)
1047 hr = TiffFrameDecode_ReadTile(This, tile_x, tile_y);
1052 if (prc->X < tile_x * This->decode_info.tile_width)
1055 rc.X = prc->X - tile_x * This->decode_info.tile_width;
1057 if (prc->Y < tile_y * This->decode_info.tile_height)
1060 rc.Y = prc->Y - tile_y * This->decode_info.tile_height;
1062 if (prc->X+prc->Width > (tile_x+1) * This->decode_info.tile_width)
1063 rc.Width = This->decode_info.tile_width - rc.X;
1064 else if (prc->X < tile_x * This->decode_info.tile_width)
1065 rc.Width = prc->Width + prc->X - tile_x * This->decode_info.tile_width;
1067 rc.Width = prc->Width;
1069 if (prc->Y+prc->Height > (tile_y+1) * This->decode_info.tile_height)
1070 rc.Height = This->decode_info.tile_height - rc.Y;
1071 else if (prc->Y < tile_y * This->decode_info.tile_height)
1072 rc.Height = prc->Height + prc->Y - tile_y * This->decode_info.tile_height;
1074 rc.Height = prc->Height;
1076 dst_tilepos = pbBuffer + (cbStride * ((rc.Y + tile_y * This->decode_info.tile_height) - prc->Y)) +
1077 ((This->decode_info.bpp * ((rc.X + tile_x * This->decode_info.tile_width) - prc->X) + 7) / 8);
1079 hr = copy_pixels(This->decode_info.bpp, This->cached_tile,
1080 This->decode_info.tile_width, This->decode_info.tile_height, This->decode_info.tile_stride,
1081 &rc, cbStride, cbBufferSize, dst_tilepos);
1086 LeaveCriticalSection(&This->parent->lock);
1087 TRACE("<-- 0x%x\n", hr);
1093 LeaveCriticalSection(&This->parent->lock);
1098 static HRESULT WINAPI TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
1099 IWICMetadataQueryReader **ppIMetadataQueryReader)
1101 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
1105 static HRESULT WINAPI TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
1106 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1108 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
1109 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1112 static HRESULT WINAPI TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
1113 IWICBitmapSource **ppIThumbnail)
1115 TRACE("(%p,%p)\n", iface, ppIThumbnail);
1117 if (!ppIThumbnail) return E_INVALIDARG;
1119 *ppIThumbnail = NULL;
1120 return WINCODEC_ERR_CODECNOTHUMBNAIL;
1123 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl = {
1124 TiffFrameDecode_QueryInterface,
1125 TiffFrameDecode_AddRef,
1126 TiffFrameDecode_Release,
1127 TiffFrameDecode_GetSize,
1128 TiffFrameDecode_GetPixelFormat,
1129 TiffFrameDecode_GetResolution,
1130 TiffFrameDecode_CopyPalette,
1131 TiffFrameDecode_CopyPixels,
1132 TiffFrameDecode_GetMetadataQueryReader,
1133 TiffFrameDecode_GetColorContexts,
1134 TiffFrameDecode_GetThumbnail
1137 static HRESULT WINAPI TiffFrameDecode_Block_QueryInterface(IWICMetadataBlockReader *iface,
1138 REFIID iid, void **ppv)
1140 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1141 return IWICBitmapFrameDecode_QueryInterface(&This->IWICBitmapFrameDecode_iface, iid, ppv);
1144 static ULONG WINAPI TiffFrameDecode_Block_AddRef(IWICMetadataBlockReader *iface)
1146 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1147 return IWICBitmapFrameDecode_AddRef(&This->IWICBitmapFrameDecode_iface);
1150 static ULONG WINAPI TiffFrameDecode_Block_Release(IWICMetadataBlockReader *iface)
1152 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1153 return IWICBitmapFrameDecode_Release(&This->IWICBitmapFrameDecode_iface);
1156 static HRESULT WINAPI TiffFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader *iface,
1159 TRACE("(%p,%p)\n", iface, guid);
1161 if (!guid) return E_INVALIDARG;
1163 *guid = GUID_ContainerFormatTiff;
1167 static HRESULT WINAPI TiffFrameDecode_Block_GetCount(IWICMetadataBlockReader *iface,
1170 TRACE("%p,%p\n", iface, count);
1172 if (!count) return E_INVALIDARG;
1178 static HRESULT create_metadata_reader(TiffFrameDecode *This, IWICMetadataReader **reader)
1181 LARGE_INTEGER dir_offset;
1182 IWICMetadataReader *metadata_reader;
1183 IWICPersistStream *persist;
1185 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
1187 hr = CoCreateInstance(&CLSID_WICIfdMetadataReader, NULL, CLSCTX_INPROC_SERVER,
1188 &IID_IWICMetadataReader, (void **)&metadata_reader);
1189 if (FAILED(hr)) return hr;
1191 hr = IWICMetadataReader_QueryInterface(metadata_reader, &IID_IWICPersistStream, (void **)&persist);
1194 IWICMetadataReader_Release(metadata_reader);
1198 EnterCriticalSection(&This->parent->lock);
1200 dir_offset.QuadPart = pTIFFCurrentDirOffset(This->parent->tiff);
1201 hr = IStream_Seek(This->parent->stream, dir_offset, STREAM_SEEK_SET, NULL);
1204 BOOL byte_swapped = pTIFFIsByteSwapped(This->parent->tiff);
1205 #ifdef WORDS_BIGENDIAN
1206 DWORD persist_options = byte_swapped ? WICPersistOptionsLittleEndian : WICPersistOptionsBigEndian;
1208 DWORD persist_options = byte_swapped ? WICPersistOptionsBigEndian : WICPersistOptionsLittleEndian;
1210 persist_options |= WICPersistOptionsNoCacheStream;
1211 hr = IWICPersistStream_LoadEx(persist, This->parent->stream, NULL, persist_options);
1213 ERR("IWICPersistStream_LoadEx error %#x\n", hr);
1216 LeaveCriticalSection(&This->parent->lock);
1218 IWICPersistStream_Release(persist);
1222 IWICMetadataReader_Release(metadata_reader);
1226 *reader = metadata_reader;
1230 static HRESULT WINAPI TiffFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader *iface,
1231 UINT index, IWICMetadataReader **reader)
1233 TiffFrameDecode *This = impl_from_IWICMetadataBlockReader(iface);
1235 TRACE("(%p,%u,%p)\n", iface, index, reader);
1237 if (!reader || index != 0) return E_INVALIDARG;
1239 return create_metadata_reader(This, reader);
1242 static HRESULT WINAPI TiffFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader *iface,
1243 IEnumUnknown **enum_metadata)
1245 FIXME("(%p,%p): stub\n", iface, enum_metadata);
1249 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl =
1251 TiffFrameDecode_Block_QueryInterface,
1252 TiffFrameDecode_Block_AddRef,
1253 TiffFrameDecode_Block_Release,
1254 TiffFrameDecode_Block_GetContainerFormat,
1255 TiffFrameDecode_Block_GetCount,
1256 TiffFrameDecode_Block_GetReaderByIndex,
1257 TiffFrameDecode_Block_GetEnumerator
1260 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1265 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1269 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1271 if (!load_libtiff())
1273 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF);
1277 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder));
1278 if (!This) return E_OUTOFMEMORY;
1280 This->IWICBitmapDecoder_iface.lpVtbl = &TiffDecoder_Vtbl;
1282 This->stream = NULL;
1283 InitializeCriticalSection(&This->lock);
1284 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffDecoder.lock");
1286 This->initialized = FALSE;
1288 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1289 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1294 struct tiff_encode_format {
1295 const WICPixelFormatGUID *guid;
1301 int extra_sample_type;
1305 static const struct tiff_encode_format formats[] = {
1306 {&GUID_WICPixelFormat24bppBGR, 2, 8, 3, 24, 0, 0, 1},
1307 {&GUID_WICPixelFormat24bppRGB, 2, 8, 3, 24, 0, 0, 0},
1308 {&GUID_WICPixelFormatBlackWhite, 1, 1, 1, 1, 0, 0, 0},
1309 {&GUID_WICPixelFormat4bppGray, 1, 4, 1, 4, 0, 0, 0},
1310 {&GUID_WICPixelFormat8bppGray, 1, 8, 1, 8, 0, 0, 0},
1311 {&GUID_WICPixelFormat32bppBGRA, 2, 8, 4, 32, 1, 2, 1},
1312 {&GUID_WICPixelFormat32bppPBGRA, 2, 8, 4, 32, 1, 1, 1},
1313 {&GUID_WICPixelFormat48bppRGB, 2, 16, 3, 48, 0, 0, 0},
1314 {&GUID_WICPixelFormat64bppRGBA, 2, 16, 4, 64, 1, 2, 0},
1315 {&GUID_WICPixelFormat64bppPRGBA, 2, 16, 4, 64, 1, 1, 0},
1319 typedef struct TiffEncoder {
1320 IWICBitmapEncoder IWICBitmapEncoder_iface;
1323 CRITICAL_SECTION lock; /* Must be held when tiff is used or fields below are set */
1328 ULONG num_frames_committed;
1331 static inline TiffEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1333 return CONTAINING_RECORD(iface, TiffEncoder, IWICBitmapEncoder_iface);
1336 typedef struct TiffFrameEncode {
1337 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1339 TiffEncoder *parent;
1340 /* fields below are protected by parent->lock */
1344 const struct tiff_encode_format *format;
1350 static inline TiffFrameEncode *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1352 return CONTAINING_RECORD(iface, TiffFrameEncode, IWICBitmapFrameEncode_iface);
1355 static HRESULT WINAPI TiffFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1358 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1359 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1361 if (!ppv) return E_INVALIDARG;
1363 if (IsEqualIID(&IID_IUnknown, iid) ||
1364 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1366 *ppv = &This->IWICBitmapFrameEncode_iface;
1371 return E_NOINTERFACE;
1374 IUnknown_AddRef((IUnknown*)*ppv);
1378 static ULONG WINAPI TiffFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1380 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1381 ULONG ref = InterlockedIncrement(&This->ref);
1383 TRACE("(%p) refcount=%u\n", iface, ref);
1388 static ULONG WINAPI TiffFrameEncode_Release(IWICBitmapFrameEncode *iface)
1390 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1391 ULONG ref = InterlockedDecrement(&This->ref);
1393 TRACE("(%p) refcount=%u\n", iface, ref);
1397 IWICBitmapEncoder_Release(&This->parent->IWICBitmapEncoder_iface);
1398 HeapFree(GetProcessHeap(), 0, This);
1404 static HRESULT WINAPI TiffFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1405 IPropertyBag2 *pIEncoderOptions)
1407 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1408 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1410 EnterCriticalSection(&This->parent->lock);
1412 if (This->initialized)
1414 LeaveCriticalSection(&This->parent->lock);
1415 return WINCODEC_ERR_WRONGSTATE;
1418 This->initialized = TRUE;
1420 LeaveCriticalSection(&This->parent->lock);
1425 static HRESULT WINAPI TiffFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1426 UINT uiWidth, UINT uiHeight)
1428 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1429 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1431 EnterCriticalSection(&This->parent->lock);
1433 if (!This->initialized || This->info_written)
1435 LeaveCriticalSection(&This->parent->lock);
1436 return WINCODEC_ERR_WRONGSTATE;
1439 This->width = uiWidth;
1440 This->height = uiHeight;
1442 LeaveCriticalSection(&This->parent->lock);
1447 static HRESULT WINAPI TiffFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1448 double dpiX, double dpiY)
1450 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1451 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1453 EnterCriticalSection(&This->parent->lock);
1455 if (!This->initialized || This->info_written)
1457 LeaveCriticalSection(&This->parent->lock);
1458 return WINCODEC_ERR_WRONGSTATE;
1464 LeaveCriticalSection(&This->parent->lock);
1469 static HRESULT WINAPI TiffFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1470 WICPixelFormatGUID *pPixelFormat)
1472 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1475 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1477 EnterCriticalSection(&This->parent->lock);
1479 if (!This->initialized || This->info_written)
1481 LeaveCriticalSection(&This->parent->lock);
1482 return WINCODEC_ERR_WRONGSTATE;
1485 for (i=0; formats[i].guid; i++)
1487 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1491 if (!formats[i].guid) i = 0;
1493 This->format = &formats[i];
1494 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1496 LeaveCriticalSection(&This->parent->lock);
1501 static HRESULT WINAPI TiffFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1502 UINT cCount, IWICColorContext **ppIColorContext)
1504 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1508 static HRESULT WINAPI TiffFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1509 IWICPalette *pIPalette)
1511 FIXME("(%p,%p): stub\n", iface, pIPalette);
1512 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1515 static HRESULT WINAPI TiffFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1516 IWICBitmapSource *pIThumbnail)
1518 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1519 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1522 static HRESULT WINAPI TiffFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1523 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1525 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1526 BYTE *row_data, *swapped_data = NULL;
1527 UINT i, j, line_size;
1529 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1531 EnterCriticalSection(&This->parent->lock);
1533 if (!This->initialized || !This->width || !This->height || !This->format)
1535 LeaveCriticalSection(&This->parent->lock);
1536 return WINCODEC_ERR_WRONGSTATE;
1539 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1541 LeaveCriticalSection(&This->parent->lock);
1542 return E_INVALIDARG;
1545 line_size = ((This->width * This->format->bpp)+7)/8;
1547 if (This->format->reverse_bgr)
1549 swapped_data = HeapAlloc(GetProcessHeap(), 0, line_size);
1552 LeaveCriticalSection(&This->parent->lock);
1553 return E_OUTOFMEMORY;
1557 if (!This->info_written)
1559 pTIFFSetField(This->parent->tiff, TIFFTAG_PHOTOMETRIC, (uint16)This->format->photometric);
1560 pTIFFSetField(This->parent->tiff, TIFFTAG_PLANARCONFIG, (uint16)1);
1561 pTIFFSetField(This->parent->tiff, TIFFTAG_BITSPERSAMPLE, (uint16)This->format->bps);
1562 pTIFFSetField(This->parent->tiff, TIFFTAG_SAMPLESPERPIXEL, (uint16)This->format->samples);
1564 if (This->format->extra_sample)
1566 uint16 extra_samples;
1567 extra_samples = This->format->extra_sample_type;
1569 pTIFFSetField(This->parent->tiff, TIFFTAG_EXTRASAMPLES, (uint16)1, &extra_samples);
1572 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGEWIDTH, (uint32)This->width);
1573 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGELENGTH, (uint32)This->height);
1575 if (This->xres != 0.0 && This->yres != 0.0)
1577 pTIFFSetField(This->parent->tiff, TIFFTAG_RESOLUTIONUNIT, (uint16)2); /* Inch */
1578 pTIFFSetField(This->parent->tiff, TIFFTAG_XRESOLUTION, (float)This->xres);
1579 pTIFFSetField(This->parent->tiff, TIFFTAG_YRESOLUTION, (float)This->yres);
1582 This->info_written = TRUE;
1585 for (i=0; i<lineCount; i++)
1587 row_data = pbPixels + i * cbStride;
1589 if (This->format->reverse_bgr && This->format->bps == 8)
1591 memcpy(swapped_data, row_data, line_size);
1592 for (j=0; j<line_size; j += This->format->samples)
1595 temp = swapped_data[j];
1596 swapped_data[j] = swapped_data[j+2];
1597 swapped_data[j+2] = temp;
1599 row_data = swapped_data;
1602 pTIFFWriteScanline(This->parent->tiff, (tdata_t)row_data, i+This->lines_written, 0);
1605 This->lines_written += lineCount;
1607 LeaveCriticalSection(&This->parent->lock);
1609 HeapFree(GetProcessHeap(), 0, swapped_data);
1614 static HRESULT WINAPI TiffFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1615 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1617 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1620 WICPixelFormatGUID guid;
1624 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1626 if (!This->initialized || !This->width || !This->height)
1627 return WINCODEC_ERR_WRONGSTATE;
1631 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1632 if (FAILED(hr)) return hr;
1633 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1634 if (FAILED(hr)) return hr;
1637 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1638 if (FAILED(hr)) return hr;
1639 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1641 /* FIXME: should use WICConvertBitmapSource to convert */
1642 ERR("format %s unsupported\n", debugstr_guid(&guid));
1646 if (This->xres == 0.0 || This->yres == 0.0)
1649 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1650 if (FAILED(hr)) return hr;
1651 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1652 if (FAILED(hr)) return hr;
1658 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1659 if (FAILED(hr)) return hr;
1667 if (prc->Width != This->width) return E_INVALIDARG;
1669 stride = (This->format->bpp * This->width + 7)/8;
1671 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1672 if (!pixeldata) return E_OUTOFMEMORY;
1674 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1675 stride*prc->Height, pixeldata);
1679 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1680 stride*prc->Height, pixeldata);
1683 HeapFree(GetProcessHeap(), 0, pixeldata);
1688 static HRESULT WINAPI TiffFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1690 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1692 TRACE("(%p)\n", iface);
1694 EnterCriticalSection(&This->parent->lock);
1696 if (!This->info_written || This->lines_written != This->height || This->committed)
1698 LeaveCriticalSection(&This->parent->lock);
1699 return WINCODEC_ERR_WRONGSTATE;
1702 /* libtiff will commit the data when creating a new frame or closing the file */
1704 This->committed = TRUE;
1705 This->parent->num_frames_committed++;
1707 LeaveCriticalSection(&This->parent->lock);
1712 static HRESULT WINAPI TiffFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1713 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1715 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1719 static const IWICBitmapFrameEncodeVtbl TiffFrameEncode_Vtbl = {
1720 TiffFrameEncode_QueryInterface,
1721 TiffFrameEncode_AddRef,
1722 TiffFrameEncode_Release,
1723 TiffFrameEncode_Initialize,
1724 TiffFrameEncode_SetSize,
1725 TiffFrameEncode_SetResolution,
1726 TiffFrameEncode_SetPixelFormat,
1727 TiffFrameEncode_SetColorContexts,
1728 TiffFrameEncode_SetPalette,
1729 TiffFrameEncode_SetThumbnail,
1730 TiffFrameEncode_WritePixels,
1731 TiffFrameEncode_WriteSource,
1732 TiffFrameEncode_Commit,
1733 TiffFrameEncode_GetMetadataQueryWriter
1736 static HRESULT WINAPI TiffEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1739 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1740 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1742 if (!ppv) return E_INVALIDARG;
1744 if (IsEqualIID(&IID_IUnknown, iid) ||
1745 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1747 *ppv = &This->IWICBitmapEncoder_iface;
1752 return E_NOINTERFACE;
1755 IUnknown_AddRef((IUnknown*)*ppv);
1759 static ULONG WINAPI TiffEncoder_AddRef(IWICBitmapEncoder *iface)
1761 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1762 ULONG ref = InterlockedIncrement(&This->ref);
1764 TRACE("(%p) refcount=%u\n", iface, ref);
1769 static ULONG WINAPI TiffEncoder_Release(IWICBitmapEncoder *iface)
1771 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1772 ULONG ref = InterlockedDecrement(&This->ref);
1774 TRACE("(%p) refcount=%u\n", iface, ref);
1778 if (This->tiff) pTIFFClose(This->tiff);
1779 if (This->stream) IStream_Release(This->stream);
1780 This->lock.DebugInfo->Spare[0] = 0;
1781 DeleteCriticalSection(&This->lock);
1782 HeapFree(GetProcessHeap(), 0, This);
1788 static HRESULT WINAPI TiffEncoder_Initialize(IWICBitmapEncoder *iface,
1789 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1791 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1795 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1797 EnterCriticalSection(&This->lock);
1799 if (This->initialized || This->committed)
1801 hr = WINCODEC_ERR_WRONGSTATE;
1805 tiff = tiff_open_stream(pIStream, "w");
1814 This->stream = pIStream;
1815 IStream_AddRef(pIStream);
1816 This->initialized = TRUE;
1819 LeaveCriticalSection(&This->lock);
1823 static HRESULT WINAPI TiffEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1824 GUID *pguidContainerFormat)
1826 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
1830 static HRESULT WINAPI TiffEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1831 IWICBitmapEncoderInfo **ppIEncoderInfo)
1833 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1837 static HRESULT WINAPI TiffEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1838 UINT cCount, IWICColorContext **ppIColorContext)
1840 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1844 static HRESULT WINAPI TiffEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1846 TRACE("(%p,%p)\n", iface, pIPalette);
1847 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1850 static HRESULT WINAPI TiffEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1852 TRACE("(%p,%p)\n", iface, pIThumbnail);
1853 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1856 static HRESULT WINAPI TiffEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1858 TRACE("(%p,%p)\n", iface, pIPreview);
1859 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1862 static HRESULT WINAPI TiffEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1863 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1865 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1866 TiffFrameEncode *result;
1870 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1872 EnterCriticalSection(&This->lock);
1874 if (!This->initialized || This->committed)
1876 hr = WINCODEC_ERR_WRONGSTATE;
1878 else if (This->num_frames != This->num_frames_committed)
1880 FIXME("New frame created before previous frame was committed\n");
1886 hr = CreatePropertyBag2(ppIEncoderOptions);
1891 result = HeapAlloc(GetProcessHeap(), 0, sizeof(*result));
1895 result->IWICBitmapFrameEncode_iface.lpVtbl = &TiffFrameEncode_Vtbl;
1897 result->parent = This;
1898 result->initialized = FALSE;
1899 result->info_written = FALSE;
1900 result->committed = FALSE;
1901 result->format = NULL;
1906 result->lines_written = 0;
1908 IWICBitmapEncoder_AddRef(iface);
1909 *ppIFrameEncode = &result->IWICBitmapFrameEncode_iface;
1911 if (This->num_frames != 0)
1912 pTIFFWriteDirectory(This->tiff);
1921 IPropertyBag2_Release(*ppIEncoderOptions);
1922 *ppIEncoderOptions = NULL;
1926 LeaveCriticalSection(&This->lock);
1931 static HRESULT WINAPI TiffEncoder_Commit(IWICBitmapEncoder *iface)
1933 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1935 TRACE("(%p)\n", iface);
1937 EnterCriticalSection(&This->lock);
1939 if (!This->initialized || This->committed)
1941 LeaveCriticalSection(&This->lock);
1942 return WINCODEC_ERR_WRONGSTATE;
1945 pTIFFClose(This->tiff);
1946 IStream_Release(This->stream);
1947 This->stream = NULL;
1950 This->committed = TRUE;
1952 LeaveCriticalSection(&This->lock);
1957 static HRESULT WINAPI TiffEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1958 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1960 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1964 static const IWICBitmapEncoderVtbl TiffEncoder_Vtbl = {
1965 TiffEncoder_QueryInterface,
1967 TiffEncoder_Release,
1968 TiffEncoder_Initialize,
1969 TiffEncoder_GetContainerFormat,
1970 TiffEncoder_GetEncoderInfo,
1971 TiffEncoder_SetColorContexts,
1972 TiffEncoder_SetPalette,
1973 TiffEncoder_SetThumbnail,
1974 TiffEncoder_SetPreview,
1975 TiffEncoder_CreateNewFrame,
1977 TiffEncoder_GetMetadataQueryWriter
1980 HRESULT TiffEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1985 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1989 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1991 if (!load_libtiff())
1993 ERR("Failed writing TIFF because unable to load %s\n",SONAME_LIBTIFF);
1997 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffEncoder));
1998 if (!This) return E_OUTOFMEMORY;
2000 This->IWICBitmapEncoder_iface.lpVtbl = &TiffEncoder_Vtbl;
2002 This->stream = NULL;
2003 InitializeCriticalSection(&This->lock);
2004 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffEncoder.lock");
2006 This->initialized = FALSE;
2007 This->num_frames = 0;
2008 This->num_frames_committed = 0;
2009 This->committed = FALSE;
2011 ret = IWICBitmapEncoder_QueryInterface(&This->IWICBitmapEncoder_iface, iid, ppv);
2012 IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
2017 #else /* !SONAME_LIBTIFF */
2019 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
2021 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
2025 HRESULT TiffEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
2027 ERR("Trying to save TIFF picture, but Wine was compiled without TIFF support.\n");