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 /* Workaround for broken libtiff 4.x headers on some 64-bit hosts which
48 * define TIFF_UINT64_T/toff_t as 32-bit for 32-bit builds, while they
49 * are supposed to be always 64-bit.
50 * TIFF_UINT64_T doesn't exist in libtiff 3.x, it was introduced in 4.x.
54 # define toff_t UINT64
57 static CRITICAL_SECTION init_tiff_cs
;
58 static CRITICAL_SECTION_DEBUG init_tiff_cs_debug
=
61 { &init_tiff_cs_debug
.ProcessLocksList
,
62 &init_tiff_cs_debug
.ProcessLocksList
},
63 0, 0, { (DWORD_PTR
)(__FILE__
": init_tiff_cs") }
65 static CRITICAL_SECTION init_tiff_cs
= { &init_tiff_cs_debug
, -1, 0, 0, 0, 0 };
67 static const WCHAR wszTiffCompressionMethod
[] = {'T','i','f','f','C','o','m','p','r','e','s','s','i','o','n','M','e','t','h','o','d',0};
68 static const WCHAR wszCompressionQuality
[] = {'C','o','m','p','r','e','s','s','i','o','n','Q','u','a','l','i','t','y',0};
70 static void *libtiff_handle
;
71 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
72 MAKE_FUNCPTR(TIFFClientOpen
);
73 MAKE_FUNCPTR(TIFFClose
);
74 MAKE_FUNCPTR(TIFFCurrentDirOffset
);
75 MAKE_FUNCPTR(TIFFGetField
);
76 MAKE_FUNCPTR(TIFFIsByteSwapped
);
77 MAKE_FUNCPTR(TIFFNumberOfDirectories
);
78 MAKE_FUNCPTR(TIFFReadDirectory
);
79 MAKE_FUNCPTR(TIFFReadEncodedStrip
);
80 MAKE_FUNCPTR(TIFFReadEncodedTile
);
81 MAKE_FUNCPTR(TIFFSetDirectory
);
82 MAKE_FUNCPTR(TIFFSetField
);
83 MAKE_FUNCPTR(TIFFWriteDirectory
);
84 MAKE_FUNCPTR(TIFFWriteScanline
);
87 static void *load_libtiff(void)
91 EnterCriticalSection(&init_tiff_cs
);
93 if (!libtiff_handle
&&
94 (libtiff_handle
= wine_dlopen(SONAME_LIBTIFF
, RTLD_NOW
, NULL
, 0)) != NULL
)
96 void * (*pTIFFSetWarningHandler
)(void *);
97 void * (*pTIFFSetWarningHandlerExt
)(void *);
99 #define LOAD_FUNCPTR(f) \
100 if((p##f = wine_dlsym(libtiff_handle, #f, NULL, 0)) == NULL) { \
101 ERR("failed to load symbol %s\n", #f); \
102 libtiff_handle = NULL; \
103 LeaveCriticalSection(&init_tiff_cs); \
106 LOAD_FUNCPTR(TIFFClientOpen
);
107 LOAD_FUNCPTR(TIFFClose
);
108 LOAD_FUNCPTR(TIFFCurrentDirOffset
);
109 LOAD_FUNCPTR(TIFFGetField
);
110 LOAD_FUNCPTR(TIFFIsByteSwapped
);
111 LOAD_FUNCPTR(TIFFNumberOfDirectories
);
112 LOAD_FUNCPTR(TIFFReadDirectory
);
113 LOAD_FUNCPTR(TIFFReadEncodedStrip
);
114 LOAD_FUNCPTR(TIFFReadEncodedTile
);
115 LOAD_FUNCPTR(TIFFSetDirectory
);
116 LOAD_FUNCPTR(TIFFSetField
);
117 LOAD_FUNCPTR(TIFFWriteDirectory
);
118 LOAD_FUNCPTR(TIFFWriteScanline
);
121 if ((pTIFFSetWarningHandler
= wine_dlsym(libtiff_handle
, "TIFFSetWarningHandler", NULL
, 0)))
122 pTIFFSetWarningHandler(NULL
);
123 if ((pTIFFSetWarningHandlerExt
= wine_dlsym(libtiff_handle
, "TIFFSetWarningHandlerExt", NULL
, 0)))
124 pTIFFSetWarningHandlerExt(NULL
);
127 result
= libtiff_handle
;
129 LeaveCriticalSection(&init_tiff_cs
);
133 static tsize_t
tiff_stream_read(thandle_t client_data
, tdata_t data
, tsize_t size
)
135 IStream
*stream
= (IStream
*)client_data
;
139 hr
= IStream_Read(stream
, data
, size
, &bytes_read
);
140 if (FAILED(hr
)) bytes_read
= 0;
144 static tsize_t
tiff_stream_write(thandle_t client_data
, tdata_t data
, tsize_t size
)
146 IStream
*stream
= (IStream
*)client_data
;
150 hr
= IStream_Write(stream
, data
, size
, &bytes_written
);
151 if (FAILED(hr
)) bytes_written
= 0;
152 return bytes_written
;
155 static toff_t
tiff_stream_seek(thandle_t client_data
, toff_t offset
, int whence
)
157 IStream
*stream
= (IStream
*)client_data
;
160 ULARGE_INTEGER new_position
;
163 move
.QuadPart
= offset
;
167 origin
= STREAM_SEEK_SET
;
170 origin
= STREAM_SEEK_CUR
;
173 origin
= STREAM_SEEK_END
;
176 ERR("unknown whence value %i\n", whence
);
180 hr
= IStream_Seek(stream
, move
, origin
, &new_position
);
181 if (SUCCEEDED(hr
)) return new_position
.QuadPart
;
185 static int tiff_stream_close(thandle_t client_data
)
187 /* Caller is responsible for releasing the stream object. */
191 static toff_t
tiff_stream_size(thandle_t client_data
)
193 IStream
*stream
= (IStream
*)client_data
;
197 hr
= IStream_Stat(stream
, &statstg
, STATFLAG_NONAME
);
199 if (SUCCEEDED(hr
)) return statstg
.cbSize
.QuadPart
;
203 static int tiff_stream_map(thandle_t client_data
, tdata_t
*addr
, toff_t
*size
)
205 /* Cannot mmap streams */
209 static void tiff_stream_unmap(thandle_t client_data
, tdata_t addr
, toff_t size
)
211 /* No need to ever do this, since we can't map things. */
214 static TIFF
* tiff_open_stream(IStream
*stream
, const char *mode
)
219 IStream_Seek(stream
, zero
, STREAM_SEEK_SET
, NULL
);
221 return pTIFFClientOpen("<IStream object>", mode
, stream
, tiff_stream_read
,
222 tiff_stream_write
, (void *)tiff_stream_seek
, tiff_stream_close
,
223 (void *)tiff_stream_size
, (void *)tiff_stream_map
, (void *)tiff_stream_unmap
);
227 IWICBitmapDecoder IWICBitmapDecoder_iface
;
230 CRITICAL_SECTION lock
; /* Must be held when tiff is used or initiailzed is set */
236 const WICPixelFormatGUID
*format
;
243 int invert_grayscale
;
245 UINT tile_width
, tile_height
;
250 UINT resolution_unit
;
255 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
256 IWICMetadataBlockReader IWICMetadataBlockReader_iface
;
260 tiff_decode_info decode_info
;
261 INT cached_tile_x
, cached_tile_y
;
265 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl
;
266 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl
;
268 static inline TiffDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
270 return CONTAINING_RECORD(iface
, TiffDecoder
, IWICBitmapDecoder_iface
);
273 static inline TiffFrameDecode
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
275 return CONTAINING_RECORD(iface
, TiffFrameDecode
, IWICBitmapFrameDecode_iface
);
278 static inline TiffFrameDecode
*impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader
*iface
)
280 return CONTAINING_RECORD(iface
, TiffFrameDecode
, IWICMetadataBlockReader_iface
);
283 static HRESULT
tiff_get_decode_info(TIFF
*tiff
, tiff_decode_info
*decode_info
)
285 uint16 photometric
, bps
, samples
, planar
;
286 uint16 extra_sample_count
, extra_sample
, *extra_samples
;
289 decode_info
->indexed
= 0;
290 decode_info
->reverse_bgr
= 0;
291 decode_info
->invert_grayscale
= 0;
292 decode_info
->tiled
= 0;
294 ret
= pTIFFGetField(tiff
, TIFFTAG_PHOTOMETRIC
, &photometric
);
297 WARN("missing PhotometricInterpretation tag\n");
301 ret
= pTIFFGetField(tiff
, TIFFTAG_BITSPERSAMPLE
, &bps
);
303 decode_info
->bps
= bps
;
305 ret
= pTIFFGetField(tiff
, TIFFTAG_SAMPLESPERPIXEL
, &samples
);
306 if (!ret
) samples
= 1;
307 decode_info
->samples
= samples
;
313 ret
= pTIFFGetField(tiff
, TIFFTAG_PLANARCONFIG
, &planar
);
314 if (!ret
) planar
= 1;
317 FIXME("unhandled planar configuration %u\n", planar
);
321 decode_info
->planar
= planar
;
325 case 0: /* WhiteIsZero */
326 decode_info
->invert_grayscale
= 1;
328 case 1: /* BlackIsZero */
331 ret
= pTIFFGetField(tiff
, TIFFTAG_EXTRASAMPLES
, &extra_sample_count
, &extra_samples
);
334 extra_sample_count
= 1;
336 extra_samples
= &extra_sample
;
339 else if (samples
!= 1)
341 FIXME("unhandled %dbpp sample count %u\n", bps
, samples
);
345 decode_info
->bpp
= bps
* samples
;
346 decode_info
->source_bpp
= decode_info
->bpp
;
352 FIXME("unhandled 1bpp sample count %u\n", samples
);
355 decode_info
->format
= &GUID_WICPixelFormatBlackWhite
;
360 FIXME("unhandled 4bpp grayscale sample count %u\n", samples
);
363 decode_info
->format
= &GUID_WICPixelFormat4bppGray
;
367 decode_info
->format
= &GUID_WICPixelFormat8bppGray
;
370 decode_info
->bpp
= 32;
372 switch(extra_samples
[0])
374 case 1: /* Associated (pre-multiplied) alpha data */
375 decode_info
->format
= &GUID_WICPixelFormat32bppPBGRA
;
377 case 0: /* Unspecified data */
378 case 2: /* Unassociated alpha data */
379 decode_info
->format
= &GUID_WICPixelFormat32bppBGRA
;
382 FIXME("unhandled extra sample type %u\n", extra_samples
[0]);
388 FIXME("unhandled greyscale bit count %u\n", bps
);
393 decode_info
->bpp
= bps
* samples
;
397 ret
= pTIFFGetField(tiff
, TIFFTAG_EXTRASAMPLES
, &extra_sample_count
, &extra_samples
);
400 extra_sample_count
= 1;
402 extra_samples
= &extra_sample
;
405 else if (samples
!= 3)
407 FIXME("unhandled RGB sample count %u\n", samples
);
414 decode_info
->reverse_bgr
= 1;
416 decode_info
->format
= &GUID_WICPixelFormat24bppBGR
;
418 switch(extra_samples
[0])
420 case 1: /* Associated (pre-multiplied) alpha data */
421 decode_info
->format
= &GUID_WICPixelFormat32bppPBGRA
;
423 case 0: /* Unspecified data */
424 case 2: /* Unassociated alpha data */
425 decode_info
->format
= &GUID_WICPixelFormat32bppBGRA
;
428 FIXME("unhandled extra sample type %i\n", extra_samples
[0]);
434 decode_info
->format
= &GUID_WICPixelFormat48bppRGB
;
436 switch(extra_samples
[0])
438 case 1: /* Associated (pre-multiplied) alpha data */
439 decode_info
->format
= &GUID_WICPixelFormat64bppPRGBA
;
441 case 0: /* Unspecified data */
442 case 2: /* Unassociated alpha data */
443 decode_info
->format
= &GUID_WICPixelFormat64bppRGBA
;
446 FIXME("unhandled extra sample type %i\n", extra_samples
[0]);
451 FIXME("unhandled RGB bit count %u\n", bps
);
455 case 3: /* RGB Palette */
458 FIXME("unhandled indexed sample count %u\n", samples
);
462 decode_info
->indexed
= 1;
463 decode_info
->bpp
= bps
;
467 decode_info
->format
= &GUID_WICPixelFormat4bppIndexed
;
470 decode_info
->format
= &GUID_WICPixelFormat8bppIndexed
;
473 FIXME("unhandled indexed bit count %u\n", bps
);
477 case 4: /* Transparency mask */
482 FIXME("unhandled PhotometricInterpretation %u\n", photometric
);
486 ret
= pTIFFGetField(tiff
, TIFFTAG_IMAGEWIDTH
, &decode_info
->width
);
489 WARN("missing image width\n");
493 ret
= pTIFFGetField(tiff
, TIFFTAG_IMAGELENGTH
, &decode_info
->height
);
496 WARN("missing image length\n");
500 if ((ret
= pTIFFGetField(tiff
, TIFFTAG_TILEWIDTH
, &decode_info
->tile_width
)))
502 decode_info
->tiled
= 1;
504 ret
= pTIFFGetField(tiff
, TIFFTAG_TILELENGTH
, &decode_info
->tile_height
);
507 WARN("missing tile height\n");
511 decode_info
->tile_stride
= ((decode_info
->bpp
* decode_info
->tile_width
+ 7)/8);
512 decode_info
->tile_size
= decode_info
->tile_height
* decode_info
->tile_stride
;
513 decode_info
->tiles_across
= (decode_info
->width
+ decode_info
->tile_width
- 1) / decode_info
->tile_width
;
515 else if ((ret
= pTIFFGetField(tiff
, TIFFTAG_ROWSPERSTRIP
, &decode_info
->tile_height
)))
517 if (decode_info
->tile_height
> decode_info
->height
)
518 decode_info
->tile_height
= decode_info
->height
;
519 decode_info
->tile_width
= decode_info
->width
;
520 decode_info
->tile_stride
= ((decode_info
->bpp
* decode_info
->tile_width
+ 7)/8);
521 decode_info
->tile_size
= decode_info
->tile_height
* decode_info
->tile_stride
;
525 /* Some broken TIFF files have a single strip and lack the RowsPerStrip tag */
526 decode_info
->tile_height
= decode_info
->height
;
527 decode_info
->tile_width
= decode_info
->width
;
528 decode_info
->tile_stride
= ((decode_info
->bpp
* decode_info
->tile_width
+ 7)/8);
529 decode_info
->tile_size
= decode_info
->tile_height
* decode_info
->tile_stride
;
532 decode_info
->resolution_unit
= 0;
533 pTIFFGetField(tiff
, TIFFTAG_RESOLUTIONUNIT
, &decode_info
->resolution_unit
);
534 if (decode_info
->resolution_unit
!= 0)
536 ret
= pTIFFGetField(tiff
, TIFFTAG_XRESOLUTION
, &decode_info
->xres
);
539 WARN("missing X resolution\n");
540 decode_info
->resolution_unit
= 0;
543 ret
= pTIFFGetField(tiff
, TIFFTAG_YRESOLUTION
, &decode_info
->yres
);
546 WARN("missing Y resolution\n");
547 decode_info
->resolution_unit
= 0;
554 static HRESULT WINAPI
TiffDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
557 TiffDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
558 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
560 if (!ppv
) return E_INVALIDARG
;
562 if (IsEqualIID(&IID_IUnknown
, iid
) ||
563 IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
565 *ppv
= &This
->IWICBitmapDecoder_iface
;
570 return E_NOINTERFACE
;
573 IUnknown_AddRef((IUnknown
*)*ppv
);
577 static ULONG WINAPI
TiffDecoder_AddRef(IWICBitmapDecoder
*iface
)
579 TiffDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
580 ULONG ref
= InterlockedIncrement(&This
->ref
);
582 TRACE("(%p) refcount=%u\n", iface
, ref
);
587 static ULONG WINAPI
TiffDecoder_Release(IWICBitmapDecoder
*iface
)
589 TiffDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
590 ULONG ref
= InterlockedDecrement(&This
->ref
);
592 TRACE("(%p) refcount=%u\n", iface
, ref
);
596 if (This
->tiff
) pTIFFClose(This
->tiff
);
597 if (This
->stream
) IStream_Release(This
->stream
);
598 This
->lock
.DebugInfo
->Spare
[0] = 0;
599 DeleteCriticalSection(&This
->lock
);
600 HeapFree(GetProcessHeap(), 0, This
);
606 static HRESULT WINAPI
TiffDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
611 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
613 if (!stream
|| !capability
) return E_INVALIDARG
;
615 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
616 if (hr
!= S_OK
) return hr
;
618 *capability
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
619 WICBitmapDecoderCapabilityCanDecodeSomeImages
|
620 WICBitmapDecoderCapabilityCanEnumerateMetadata
;
624 static HRESULT WINAPI
TiffDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
625 WICDecodeOptions cacheOptions
)
627 TiffDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
631 TRACE("(%p,%p,%x): stub\n", iface
, pIStream
, cacheOptions
);
633 EnterCriticalSection(&This
->lock
);
635 if (This
->initialized
)
637 hr
= WINCODEC_ERR_WRONGSTATE
;
641 tiff
= tiff_open_stream(pIStream
, "r");
650 This
->stream
= pIStream
;
651 IStream_AddRef(pIStream
);
652 This
->initialized
= TRUE
;
655 LeaveCriticalSection(&This
->lock
);
659 static HRESULT WINAPI
TiffDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
660 GUID
*pguidContainerFormat
)
662 if (!pguidContainerFormat
) return E_INVALIDARG
;
664 memcpy(pguidContainerFormat
, &GUID_ContainerFormatTiff
, sizeof(GUID
));
668 static HRESULT WINAPI
TiffDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
669 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
672 IWICComponentInfo
*compinfo
;
674 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
676 hr
= CreateComponentInfo(&CLSID_WICTiffDecoder
, &compinfo
);
677 if (FAILED(hr
)) return hr
;
679 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
680 (void**)ppIDecoderInfo
);
682 IWICComponentInfo_Release(compinfo
);
687 static HRESULT WINAPI
TiffDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
688 IWICPalette
*pIPalette
)
690 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
694 static HRESULT WINAPI
TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
695 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
697 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
701 static HRESULT WINAPI
TiffDecoder_GetPreview(IWICBitmapDecoder
*iface
,
702 IWICBitmapSource
**ppIBitmapSource
)
704 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
706 if (!ppIBitmapSource
) return E_INVALIDARG
;
708 *ppIBitmapSource
= NULL
;
709 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
712 static HRESULT WINAPI
TiffDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
713 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
715 FIXME("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
716 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
719 static HRESULT WINAPI
TiffDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
720 IWICBitmapSource
**ppIThumbnail
)
722 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
724 if (!ppIThumbnail
) return E_INVALIDARG
;
726 *ppIThumbnail
= NULL
;
727 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
730 static HRESULT WINAPI
TiffDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
733 TiffDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
735 if (!pCount
) return E_INVALIDARG
;
737 EnterCriticalSection(&This
->lock
);
738 *pCount
= This
->tiff
? pTIFFNumberOfDirectories(This
->tiff
) : 0;
739 LeaveCriticalSection(&This
->lock
);
741 TRACE("(%p) <-- %i\n", iface
, *pCount
);
746 static HRESULT WINAPI
TiffDecoder_GetFrame(IWICBitmapDecoder
*iface
,
747 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
749 TiffDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
750 TiffFrameDecode
*result
;
752 tiff_decode_info decode_info
;
755 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
758 return WINCODEC_ERR_FRAMEMISSING
;
760 EnterCriticalSection(&This
->lock
);
761 res
= pTIFFSetDirectory(This
->tiff
, index
);
762 if (!res
) hr
= E_INVALIDARG
;
763 else hr
= tiff_get_decode_info(This
->tiff
, &decode_info
);
764 LeaveCriticalSection(&This
->lock
);
768 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode
));
772 result
->IWICBitmapFrameDecode_iface
.lpVtbl
= &TiffFrameDecode_Vtbl
;
773 result
->IWICMetadataBlockReader_iface
.lpVtbl
= &TiffFrameDecode_BlockVtbl
;
775 result
->parent
= This
;
776 result
->index
= index
;
777 result
->decode_info
= decode_info
;
778 result
->cached_tile_x
= -1;
779 result
->cached_tile
= HeapAlloc(GetProcessHeap(), 0, decode_info
.tile_size
);
781 if (result
->cached_tile
)
782 *ppIBitmapFrame
= &result
->IWICBitmapFrameDecode_iface
;
786 HeapFree(GetProcessHeap(), 0, result
);
789 else hr
= E_OUTOFMEMORY
;
792 if (FAILED(hr
)) *ppIBitmapFrame
= NULL
;
797 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl
= {
798 TiffDecoder_QueryInterface
,
801 TiffDecoder_QueryCapability
,
802 TiffDecoder_Initialize
,
803 TiffDecoder_GetContainerFormat
,
804 TiffDecoder_GetDecoderInfo
,
805 TiffDecoder_CopyPalette
,
806 TiffDecoder_GetMetadataQueryReader
,
807 TiffDecoder_GetPreview
,
808 TiffDecoder_GetColorContexts
,
809 TiffDecoder_GetThumbnail
,
810 TiffDecoder_GetFrameCount
,
814 static HRESULT WINAPI
TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
817 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
818 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
820 if (!ppv
) return E_INVALIDARG
;
822 if (IsEqualIID(&IID_IUnknown
, iid
) ||
823 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
824 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
826 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
828 else if (IsEqualIID(&IID_IWICMetadataBlockReader
, iid
))
830 *ppv
= &This
->IWICMetadataBlockReader_iface
;
835 return E_NOINTERFACE
;
838 IUnknown_AddRef((IUnknown
*)*ppv
);
842 static ULONG WINAPI
TiffFrameDecode_AddRef(IWICBitmapFrameDecode
*iface
)
844 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
845 ULONG ref
= InterlockedIncrement(&This
->ref
);
847 TRACE("(%p) refcount=%u\n", iface
, ref
);
852 static ULONG WINAPI
TiffFrameDecode_Release(IWICBitmapFrameDecode
*iface
)
854 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
855 ULONG ref
= InterlockedDecrement(&This
->ref
);
857 TRACE("(%p) refcount=%u\n", iface
, ref
);
861 HeapFree(GetProcessHeap(), 0, This
->cached_tile
);
862 HeapFree(GetProcessHeap(), 0, This
);
868 static HRESULT WINAPI
TiffFrameDecode_GetSize(IWICBitmapFrameDecode
*iface
,
869 UINT
*puiWidth
, UINT
*puiHeight
)
871 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
873 *puiWidth
= This
->decode_info
.width
;
874 *puiHeight
= This
->decode_info
.height
;
876 TRACE("(%p) <-- %ux%u\n", iface
, *puiWidth
, *puiHeight
);
881 static HRESULT WINAPI
TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
882 WICPixelFormatGUID
*pPixelFormat
)
884 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
886 memcpy(pPixelFormat
, This
->decode_info
.format
, sizeof(GUID
));
888 TRACE("(%p) <-- %s\n", This
, debugstr_guid(This
->decode_info
.format
));
893 static HRESULT WINAPI
TiffFrameDecode_GetResolution(IWICBitmapFrameDecode
*iface
,
894 double *pDpiX
, double *pDpiY
)
896 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
898 switch (This
->decode_info
.resolution_unit
)
901 FIXME("unknown resolution unit %i\n", This
->decode_info
.resolution_unit
);
903 case 0: /* Not set */
904 *pDpiX
= *pDpiY
= 96.0;
906 case 1: /* Relative measurements */
908 *pDpiY
= 96.0 * This
->decode_info
.yres
/ This
->decode_info
.xres
;
911 *pDpiX
= This
->decode_info
.xres
;
912 *pDpiY
= This
->decode_info
.yres
;
914 case 3: /* Centimeter */
915 *pDpiX
= This
->decode_info
.xres
/ 2.54;
916 *pDpiY
= This
->decode_info
.yres
/ 2.54;
920 TRACE("(%p) <-- %f,%f unit=%i\n", iface
, *pDpiX
, *pDpiY
, This
->decode_info
.resolution_unit
);
925 static HRESULT WINAPI
TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode
*iface
,
926 IWICPalette
*pIPalette
)
928 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
929 uint16
*red
, *green
, *blue
;
930 WICColor colors
[256];
931 int color_count
, ret
, i
;
933 TRACE("(%p,%p)\n", iface
, pIPalette
);
935 color_count
= 1<<This
->decode_info
.bps
;
937 EnterCriticalSection(&This
->parent
->lock
);
938 ret
= pTIFFGetField(This
->parent
->tiff
, TIFFTAG_COLORMAP
, &red
, &green
, &blue
);
939 LeaveCriticalSection(&This
->parent
->lock
);
943 WARN("Couldn't read color map\n");
944 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
947 for (i
=0; i
<color_count
; i
++)
949 colors
[i
] = 0xff000000 |
950 ((red
[i
]<<8) & 0xff0000) |
951 (green
[i
] & 0xff00) |
952 ((blue
[i
]>>8) & 0xff);
955 return IWICPalette_InitializeCustom(pIPalette
, colors
, color_count
);
958 static HRESULT
TiffFrameDecode_ReadTile(TiffFrameDecode
*This
, UINT tile_x
, UINT tile_y
)
964 swap_bytes
= pTIFFIsByteSwapped(This
->parent
->tiff
);
966 ret
= pTIFFSetDirectory(This
->parent
->tiff
, This
->index
);
973 if (This
->decode_info
.tiled
)
975 ret
= pTIFFReadEncodedTile(This
->parent
->tiff
, tile_x
+ tile_y
* This
->decode_info
.tiles_across
, This
->cached_tile
, This
->decode_info
.tile_size
);
979 ret
= pTIFFReadEncodedStrip(This
->parent
->tiff
, tile_y
, This
->cached_tile
, This
->decode_info
.tile_size
);
986 /* 8bpp grayscale with extra alpha */
987 if (hr
== S_OK
&& This
->decode_info
.source_bpp
== 16 && This
->decode_info
.samples
== 2 && This
->decode_info
.bpp
== 32)
990 DWORD
*dst
, count
= This
->decode_info
.tile_width
* This
->decode_info
.tile_height
;
992 src
= This
->cached_tile
+ This
->decode_info
.tile_width
* This
->decode_info
.tile_height
* 2 - 2;
993 dst
= (DWORD
*)(This
->cached_tile
+ This
->decode_info
.tile_size
- 4);
997 *dst
-- = src
[0] | (src
[0] << 8) | (src
[0] << 16) | (src
[1] << 24);
1002 if (hr
== S_OK
&& This
->decode_info
.reverse_bgr
)
1004 if (This
->decode_info
.bps
== 8)
1006 UINT sample_count
= This
->decode_info
.samples
;
1008 reverse_bgr8(sample_count
, This
->cached_tile
, This
->decode_info
.tile_width
,
1009 This
->decode_info
.tile_height
, This
->decode_info
.tile_width
* sample_count
);
1013 if (hr
== S_OK
&& swap_bytes
&& This
->decode_info
.bps
> 8)
1015 UINT row
, i
, samples_per_row
;
1018 samples_per_row
= This
->decode_info
.tile_width
* This
->decode_info
.samples
;
1020 switch(This
->decode_info
.bps
)
1023 for (row
=0; row
<This
->decode_info
.tile_height
; row
++)
1025 sample
= This
->cached_tile
+ row
* This
->decode_info
.tile_stride
;
1026 for (i
=0; i
<samples_per_row
; i
++)
1029 sample
[1] = sample
[0];
1036 ERR("unhandled bps for byte swap %u\n", This
->decode_info
.bps
);
1041 if (hr
== S_OK
&& This
->decode_info
.invert_grayscale
)
1045 if (This
->decode_info
.samples
!= 1)
1047 ERR("cannot invert grayscale image with %u samples\n", This
->decode_info
.samples
);
1051 end
= This
->cached_tile
+This
->decode_info
.tile_size
;
1053 for (byte
= This
->cached_tile
; byte
!= end
; byte
++)
1059 This
->cached_tile_x
= tile_x
;
1060 This
->cached_tile_y
= tile_y
;
1066 static HRESULT WINAPI
TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode
*iface
,
1067 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
1069 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
1070 UINT min_tile_x
, max_tile_x
, min_tile_y
, max_tile_y
;
1071 UINT tile_x
, tile_y
;
1078 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
1084 rect
.Width
= This
->decode_info
.width
;
1085 rect
.Height
= This
->decode_info
.height
;
1090 if (prc
->X
< 0 || prc
->Y
< 0 || prc
->X
+prc
->Width
> This
->decode_info
.width
||
1091 prc
->Y
+prc
->Height
> This
->decode_info
.height
)
1092 return E_INVALIDARG
;
1095 bytesperrow
= ((This
->decode_info
.bpp
* prc
->Width
)+7)/8;
1097 if (cbStride
< bytesperrow
)
1098 return E_INVALIDARG
;
1100 if ((cbStride
* prc
->Height
) > cbBufferSize
)
1101 return E_INVALIDARG
;
1103 min_tile_x
= prc
->X
/ This
->decode_info
.tile_width
;
1104 min_tile_y
= prc
->Y
/ This
->decode_info
.tile_height
;
1105 max_tile_x
= (prc
->X
+prc
->Width
-1) / This
->decode_info
.tile_width
;
1106 max_tile_y
= (prc
->Y
+prc
->Height
-1) / This
->decode_info
.tile_height
;
1108 EnterCriticalSection(&This
->parent
->lock
);
1110 for (tile_x
=min_tile_x
; tile_x
<= max_tile_x
; tile_x
++)
1112 for (tile_y
=min_tile_y
; tile_y
<= max_tile_y
; tile_y
++)
1114 if (tile_x
!= This
->cached_tile_x
|| tile_y
!= This
->cached_tile_y
)
1116 hr
= TiffFrameDecode_ReadTile(This
, tile_x
, tile_y
);
1121 if (prc
->X
< tile_x
* This
->decode_info
.tile_width
)
1124 rc
.X
= prc
->X
- tile_x
* This
->decode_info
.tile_width
;
1126 if (prc
->Y
< tile_y
* This
->decode_info
.tile_height
)
1129 rc
.Y
= prc
->Y
- tile_y
* This
->decode_info
.tile_height
;
1131 if (prc
->X
+prc
->Width
> (tile_x
+1) * This
->decode_info
.tile_width
)
1132 rc
.Width
= This
->decode_info
.tile_width
- rc
.X
;
1133 else if (prc
->X
< tile_x
* This
->decode_info
.tile_width
)
1134 rc
.Width
= prc
->Width
+ prc
->X
- tile_x
* This
->decode_info
.tile_width
;
1136 rc
.Width
= prc
->Width
;
1138 if (prc
->Y
+prc
->Height
> (tile_y
+1) * This
->decode_info
.tile_height
)
1139 rc
.Height
= This
->decode_info
.tile_height
- rc
.Y
;
1140 else if (prc
->Y
< tile_y
* This
->decode_info
.tile_height
)
1141 rc
.Height
= prc
->Height
+ prc
->Y
- tile_y
* This
->decode_info
.tile_height
;
1143 rc
.Height
= prc
->Height
;
1145 dst_tilepos
= pbBuffer
+ (cbStride
* ((rc
.Y
+ tile_y
* This
->decode_info
.tile_height
) - prc
->Y
)) +
1146 ((This
->decode_info
.bpp
* ((rc
.X
+ tile_x
* This
->decode_info
.tile_width
) - prc
->X
) + 7) / 8);
1148 hr
= copy_pixels(This
->decode_info
.bpp
, This
->cached_tile
,
1149 This
->decode_info
.tile_width
, This
->decode_info
.tile_height
, This
->decode_info
.tile_stride
,
1150 &rc
, cbStride
, cbBufferSize
, dst_tilepos
);
1155 LeaveCriticalSection(&This
->parent
->lock
);
1156 TRACE("<-- 0x%x\n", hr
);
1162 LeaveCriticalSection(&This
->parent
->lock
);
1167 static HRESULT WINAPI
TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
1168 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
1170 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
1174 static HRESULT WINAPI
TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode
*iface
,
1175 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
1177 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
1178 const BYTE
*profile
;
1182 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
1184 EnterCriticalSection(&This
->parent
->lock
);
1186 if (pTIFFGetField(This
->parent
->tiff
, TIFFTAG_ICCPROFILE
, &len
, &profile
))
1188 if (cCount
&& ppIColorContexts
)
1190 hr
= IWICColorContext_InitializeFromMemory(*ppIColorContexts
, profile
, len
);
1193 LeaveCriticalSection(&This
->parent
->lock
);
1202 LeaveCriticalSection(&This
->parent
->lock
);
1207 static HRESULT WINAPI
TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode
*iface
,
1208 IWICBitmapSource
**ppIThumbnail
)
1210 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
1212 if (!ppIThumbnail
) return E_INVALIDARG
;
1214 *ppIThumbnail
= NULL
;
1215 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
1218 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl
= {
1219 TiffFrameDecode_QueryInterface
,
1220 TiffFrameDecode_AddRef
,
1221 TiffFrameDecode_Release
,
1222 TiffFrameDecode_GetSize
,
1223 TiffFrameDecode_GetPixelFormat
,
1224 TiffFrameDecode_GetResolution
,
1225 TiffFrameDecode_CopyPalette
,
1226 TiffFrameDecode_CopyPixels
,
1227 TiffFrameDecode_GetMetadataQueryReader
,
1228 TiffFrameDecode_GetColorContexts
,
1229 TiffFrameDecode_GetThumbnail
1232 static HRESULT WINAPI
TiffFrameDecode_Block_QueryInterface(IWICMetadataBlockReader
*iface
,
1233 REFIID iid
, void **ppv
)
1235 TiffFrameDecode
*This
= impl_from_IWICMetadataBlockReader(iface
);
1236 return IWICBitmapFrameDecode_QueryInterface(&This
->IWICBitmapFrameDecode_iface
, iid
, ppv
);
1239 static ULONG WINAPI
TiffFrameDecode_Block_AddRef(IWICMetadataBlockReader
*iface
)
1241 TiffFrameDecode
*This
= impl_from_IWICMetadataBlockReader(iface
);
1242 return IWICBitmapFrameDecode_AddRef(&This
->IWICBitmapFrameDecode_iface
);
1245 static ULONG WINAPI
TiffFrameDecode_Block_Release(IWICMetadataBlockReader
*iface
)
1247 TiffFrameDecode
*This
= impl_from_IWICMetadataBlockReader(iface
);
1248 return IWICBitmapFrameDecode_Release(&This
->IWICBitmapFrameDecode_iface
);
1251 static HRESULT WINAPI
TiffFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader
*iface
,
1254 TRACE("(%p,%p)\n", iface
, guid
);
1256 if (!guid
) return E_INVALIDARG
;
1258 *guid
= GUID_ContainerFormatTiff
;
1262 static HRESULT WINAPI
TiffFrameDecode_Block_GetCount(IWICMetadataBlockReader
*iface
,
1265 TRACE("%p,%p\n", iface
, count
);
1267 if (!count
) return E_INVALIDARG
;
1273 static HRESULT
create_metadata_reader(TiffFrameDecode
*This
, IWICMetadataReader
**reader
)
1276 LARGE_INTEGER dir_offset
;
1277 IWICMetadataReader
*metadata_reader
;
1278 IWICPersistStream
*persist
;
1280 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
1282 hr
= IfdMetadataReader_CreateInstance(&IID_IWICMetadataReader
, (void **)&metadata_reader
);
1283 if (FAILED(hr
)) return hr
;
1285 hr
= IWICMetadataReader_QueryInterface(metadata_reader
, &IID_IWICPersistStream
, (void **)&persist
);
1288 IWICMetadataReader_Release(metadata_reader
);
1292 EnterCriticalSection(&This
->parent
->lock
);
1294 dir_offset
.QuadPart
= pTIFFCurrentDirOffset(This
->parent
->tiff
);
1295 hr
= IStream_Seek(This
->parent
->stream
, dir_offset
, STREAM_SEEK_SET
, NULL
);
1298 BOOL byte_swapped
= pTIFFIsByteSwapped(This
->parent
->tiff
);
1299 #ifdef WORDS_BIGENDIAN
1300 DWORD persist_options
= byte_swapped
? WICPersistOptionsLittleEndian
: WICPersistOptionsBigEndian
;
1302 DWORD persist_options
= byte_swapped
? WICPersistOptionsBigEndian
: WICPersistOptionsLittleEndian
;
1304 persist_options
|= WICPersistOptionsNoCacheStream
;
1305 hr
= IWICPersistStream_LoadEx(persist
, This
->parent
->stream
, NULL
, persist_options
);
1307 ERR("IWICPersistStream_LoadEx error %#x\n", hr
);
1310 LeaveCriticalSection(&This
->parent
->lock
);
1312 IWICPersistStream_Release(persist
);
1316 IWICMetadataReader_Release(metadata_reader
);
1320 *reader
= metadata_reader
;
1324 static HRESULT WINAPI
TiffFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader
*iface
,
1325 UINT index
, IWICMetadataReader
**reader
)
1327 TiffFrameDecode
*This
= impl_from_IWICMetadataBlockReader(iface
);
1329 TRACE("(%p,%u,%p)\n", iface
, index
, reader
);
1331 if (!reader
|| index
!= 0) return E_INVALIDARG
;
1333 return create_metadata_reader(This
, reader
);
1336 static HRESULT WINAPI
TiffFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader
*iface
,
1337 IEnumUnknown
**enum_metadata
)
1339 FIXME("(%p,%p): stub\n", iface
, enum_metadata
);
1343 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl
=
1345 TiffFrameDecode_Block_QueryInterface
,
1346 TiffFrameDecode_Block_AddRef
,
1347 TiffFrameDecode_Block_Release
,
1348 TiffFrameDecode_Block_GetContainerFormat
,
1349 TiffFrameDecode_Block_GetCount
,
1350 TiffFrameDecode_Block_GetReaderByIndex
,
1351 TiffFrameDecode_Block_GetEnumerator
1354 HRESULT
TiffDecoder_CreateInstance(REFIID iid
, void** ppv
)
1359 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1363 if (!load_libtiff())
1365 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF
);
1369 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder
));
1370 if (!This
) return E_OUTOFMEMORY
;
1372 This
->IWICBitmapDecoder_iface
.lpVtbl
= &TiffDecoder_Vtbl
;
1374 This
->stream
= NULL
;
1375 InitializeCriticalSection(&This
->lock
);
1376 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TiffDecoder.lock");
1378 This
->initialized
= FALSE
;
1380 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1381 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1386 struct tiff_encode_format
{
1387 const WICPixelFormatGUID
*guid
;
1393 int extra_sample_type
;
1397 static const struct tiff_encode_format formats
[] = {
1398 {&GUID_WICPixelFormat24bppBGR
, 2, 8, 3, 24, 0, 0, 1},
1399 {&GUID_WICPixelFormat24bppRGB
, 2, 8, 3, 24, 0, 0, 0},
1400 {&GUID_WICPixelFormatBlackWhite
, 1, 1, 1, 1, 0, 0, 0},
1401 {&GUID_WICPixelFormat4bppGray
, 1, 4, 1, 4, 0, 0, 0},
1402 {&GUID_WICPixelFormat8bppGray
, 1, 8, 1, 8, 0, 0, 0},
1403 {&GUID_WICPixelFormat32bppBGRA
, 2, 8, 4, 32, 1, 2, 1},
1404 {&GUID_WICPixelFormat32bppPBGRA
, 2, 8, 4, 32, 1, 1, 1},
1405 {&GUID_WICPixelFormat48bppRGB
, 2, 16, 3, 48, 0, 0, 0},
1406 {&GUID_WICPixelFormat64bppRGBA
, 2, 16, 4, 64, 1, 2, 0},
1407 {&GUID_WICPixelFormat64bppPRGBA
, 2, 16, 4, 64, 1, 1, 0},
1411 typedef struct TiffEncoder
{
1412 IWICBitmapEncoder IWICBitmapEncoder_iface
;
1415 CRITICAL_SECTION lock
; /* Must be held when tiff is used or fields below are set */
1420 ULONG num_frames_committed
;
1423 static inline TiffEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
1425 return CONTAINING_RECORD(iface
, TiffEncoder
, IWICBitmapEncoder_iface
);
1428 typedef struct TiffFrameEncode
{
1429 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
1431 TiffEncoder
*parent
;
1432 /* fields below are protected by parent->lock */
1436 const struct tiff_encode_format
*format
;
1442 static inline TiffFrameEncode
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
1444 return CONTAINING_RECORD(iface
, TiffFrameEncode
, IWICBitmapFrameEncode_iface
);
1447 static HRESULT WINAPI
TiffFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
1450 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1451 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1453 if (!ppv
) return E_INVALIDARG
;
1455 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1456 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
1458 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
1463 return E_NOINTERFACE
;
1466 IUnknown_AddRef((IUnknown
*)*ppv
);
1470 static ULONG WINAPI
TiffFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
1472 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1473 ULONG ref
= InterlockedIncrement(&This
->ref
);
1475 TRACE("(%p) refcount=%u\n", iface
, ref
);
1480 static ULONG WINAPI
TiffFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
1482 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1483 ULONG ref
= InterlockedDecrement(&This
->ref
);
1485 TRACE("(%p) refcount=%u\n", iface
, ref
);
1489 IWICBitmapEncoder_Release(&This
->parent
->IWICBitmapEncoder_iface
);
1490 HeapFree(GetProcessHeap(), 0, This
);
1496 static HRESULT WINAPI
TiffFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
1497 IPropertyBag2
*pIEncoderOptions
)
1499 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1500 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
1502 EnterCriticalSection(&This
->parent
->lock
);
1504 if (This
->initialized
)
1506 LeaveCriticalSection(&This
->parent
->lock
);
1507 return WINCODEC_ERR_WRONGSTATE
;
1510 This
->initialized
= TRUE
;
1512 LeaveCriticalSection(&This
->parent
->lock
);
1517 static HRESULT WINAPI
TiffFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
1518 UINT uiWidth
, UINT uiHeight
)
1520 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1521 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
1523 EnterCriticalSection(&This
->parent
->lock
);
1525 if (!This
->initialized
|| This
->info_written
)
1527 LeaveCriticalSection(&This
->parent
->lock
);
1528 return WINCODEC_ERR_WRONGSTATE
;
1531 This
->width
= uiWidth
;
1532 This
->height
= uiHeight
;
1534 LeaveCriticalSection(&This
->parent
->lock
);
1539 static HRESULT WINAPI
TiffFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
1540 double dpiX
, double dpiY
)
1542 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1543 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
1545 EnterCriticalSection(&This
->parent
->lock
);
1547 if (!This
->initialized
|| This
->info_written
)
1549 LeaveCriticalSection(&This
->parent
->lock
);
1550 return WINCODEC_ERR_WRONGSTATE
;
1556 LeaveCriticalSection(&This
->parent
->lock
);
1561 static HRESULT WINAPI
TiffFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
1562 WICPixelFormatGUID
*pPixelFormat
)
1564 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1567 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
1569 EnterCriticalSection(&This
->parent
->lock
);
1571 if (!This
->initialized
|| This
->info_written
)
1573 LeaveCriticalSection(&This
->parent
->lock
);
1574 return WINCODEC_ERR_WRONGSTATE
;
1577 for (i
=0; formats
[i
].guid
; i
++)
1579 if (memcmp(formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
1583 if (!formats
[i
].guid
) i
= 0;
1585 This
->format
= &formats
[i
];
1586 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
1588 LeaveCriticalSection(&This
->parent
->lock
);
1593 static HRESULT WINAPI
TiffFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
1594 UINT cCount
, IWICColorContext
**ppIColorContext
)
1596 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1600 static HRESULT WINAPI
TiffFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
1601 IWICPalette
*pIPalette
)
1603 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
1604 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1607 static HRESULT WINAPI
TiffFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
1608 IWICBitmapSource
*pIThumbnail
)
1610 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
1611 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1614 static HRESULT WINAPI
TiffFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
1615 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
1617 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1618 BYTE
*row_data
, *swapped_data
= NULL
;
1619 UINT i
, j
, line_size
;
1621 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
1623 EnterCriticalSection(&This
->parent
->lock
);
1625 if (!This
->initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
1627 LeaveCriticalSection(&This
->parent
->lock
);
1628 return WINCODEC_ERR_WRONGSTATE
;
1631 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
1633 LeaveCriticalSection(&This
->parent
->lock
);
1634 return E_INVALIDARG
;
1637 line_size
= ((This
->width
* This
->format
->bpp
)+7)/8;
1639 if (This
->format
->reverse_bgr
)
1641 swapped_data
= HeapAlloc(GetProcessHeap(), 0, line_size
);
1644 LeaveCriticalSection(&This
->parent
->lock
);
1645 return E_OUTOFMEMORY
;
1649 if (!This
->info_written
)
1651 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_PHOTOMETRIC
, (uint16
)This
->format
->photometric
);
1652 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_PLANARCONFIG
, (uint16
)1);
1653 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_BITSPERSAMPLE
, (uint16
)This
->format
->bps
);
1654 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_SAMPLESPERPIXEL
, (uint16
)This
->format
->samples
);
1656 if (This
->format
->extra_sample
)
1658 uint16 extra_samples
;
1659 extra_samples
= This
->format
->extra_sample_type
;
1661 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_EXTRASAMPLES
, (uint16
)1, &extra_samples
);
1664 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_IMAGEWIDTH
, (uint32
)This
->width
);
1665 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_IMAGELENGTH
, (uint32
)This
->height
);
1667 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
1669 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_RESOLUTIONUNIT
, (uint16
)2); /* Inch */
1670 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_XRESOLUTION
, (float)This
->xres
);
1671 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_YRESOLUTION
, (float)This
->yres
);
1674 This
->info_written
= TRUE
;
1677 for (i
=0; i
<lineCount
; i
++)
1679 row_data
= pbPixels
+ i
* cbStride
;
1681 if (This
->format
->reverse_bgr
&& This
->format
->bps
== 8)
1683 memcpy(swapped_data
, row_data
, line_size
);
1684 for (j
=0; j
<line_size
; j
+= This
->format
->samples
)
1687 temp
= swapped_data
[j
];
1688 swapped_data
[j
] = swapped_data
[j
+2];
1689 swapped_data
[j
+2] = temp
;
1691 row_data
= swapped_data
;
1694 pTIFFWriteScanline(This
->parent
->tiff
, (tdata_t
)row_data
, i
+This
->lines_written
, 0);
1697 This
->lines_written
+= lineCount
;
1699 LeaveCriticalSection(&This
->parent
->lock
);
1701 HeapFree(GetProcessHeap(), 0, swapped_data
);
1706 static HRESULT WINAPI
TiffFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
1707 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
1709 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1712 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
1714 if (!This
->initialized
)
1715 return WINCODEC_ERR_WRONGSTATE
;
1717 hr
= configure_write_source(iface
, pIBitmapSource
, prc
,
1718 This
->format
? This
->format
->guid
: NULL
, This
->width
, This
->height
,
1719 This
->xres
, This
->yres
);
1723 hr
= write_source(iface
, pIBitmapSource
, prc
,
1724 This
->format
->guid
, This
->format
->bpp
, This
->width
, This
->height
);
1730 static HRESULT WINAPI
TiffFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
1732 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1734 TRACE("(%p)\n", iface
);
1736 EnterCriticalSection(&This
->parent
->lock
);
1738 if (!This
->info_written
|| This
->lines_written
!= This
->height
|| This
->committed
)
1740 LeaveCriticalSection(&This
->parent
->lock
);
1741 return WINCODEC_ERR_WRONGSTATE
;
1744 /* libtiff will commit the data when creating a new frame or closing the file */
1746 This
->committed
= TRUE
;
1747 This
->parent
->num_frames_committed
++;
1749 LeaveCriticalSection(&This
->parent
->lock
);
1754 static HRESULT WINAPI
TiffFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1755 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1757 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1761 static const IWICBitmapFrameEncodeVtbl TiffFrameEncode_Vtbl
= {
1762 TiffFrameEncode_QueryInterface
,
1763 TiffFrameEncode_AddRef
,
1764 TiffFrameEncode_Release
,
1765 TiffFrameEncode_Initialize
,
1766 TiffFrameEncode_SetSize
,
1767 TiffFrameEncode_SetResolution
,
1768 TiffFrameEncode_SetPixelFormat
,
1769 TiffFrameEncode_SetColorContexts
,
1770 TiffFrameEncode_SetPalette
,
1771 TiffFrameEncode_SetThumbnail
,
1772 TiffFrameEncode_WritePixels
,
1773 TiffFrameEncode_WriteSource
,
1774 TiffFrameEncode_Commit
,
1775 TiffFrameEncode_GetMetadataQueryWriter
1778 static HRESULT WINAPI
TiffEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1781 TiffEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1782 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1784 if (!ppv
) return E_INVALIDARG
;
1786 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1787 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1789 *ppv
= &This
->IWICBitmapEncoder_iface
;
1794 return E_NOINTERFACE
;
1797 IUnknown_AddRef((IUnknown
*)*ppv
);
1801 static ULONG WINAPI
TiffEncoder_AddRef(IWICBitmapEncoder
*iface
)
1803 TiffEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1804 ULONG ref
= InterlockedIncrement(&This
->ref
);
1806 TRACE("(%p) refcount=%u\n", iface
, ref
);
1811 static ULONG WINAPI
TiffEncoder_Release(IWICBitmapEncoder
*iface
)
1813 TiffEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1814 ULONG ref
= InterlockedDecrement(&This
->ref
);
1816 TRACE("(%p) refcount=%u\n", iface
, ref
);
1820 if (This
->tiff
) pTIFFClose(This
->tiff
);
1821 if (This
->stream
) IStream_Release(This
->stream
);
1822 This
->lock
.DebugInfo
->Spare
[0] = 0;
1823 DeleteCriticalSection(&This
->lock
);
1824 HeapFree(GetProcessHeap(), 0, This
);
1830 static HRESULT WINAPI
TiffEncoder_Initialize(IWICBitmapEncoder
*iface
,
1831 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1833 TiffEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1837 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1839 EnterCriticalSection(&This
->lock
);
1841 if (This
->initialized
|| This
->committed
)
1843 hr
= WINCODEC_ERR_WRONGSTATE
;
1847 tiff
= tiff_open_stream(pIStream
, "w");
1856 This
->stream
= pIStream
;
1857 IStream_AddRef(pIStream
);
1858 This
->initialized
= TRUE
;
1861 LeaveCriticalSection(&This
->lock
);
1865 static HRESULT WINAPI
TiffEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1866 GUID
*pguidContainerFormat
)
1868 memcpy(pguidContainerFormat
, &GUID_ContainerFormatTiff
, sizeof(GUID
));
1872 static HRESULT WINAPI
TiffEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1873 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1875 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1879 static HRESULT WINAPI
TiffEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1880 UINT cCount
, IWICColorContext
**ppIColorContext
)
1882 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1886 static HRESULT WINAPI
TiffEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
1888 TRACE("(%p,%p)\n", iface
, pIPalette
);
1889 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1892 static HRESULT WINAPI
TiffEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
1894 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
1895 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1898 static HRESULT WINAPI
TiffEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
1900 TRACE("(%p,%p)\n", iface
, pIPreview
);
1901 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1904 static HRESULT WINAPI
TiffEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
1905 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
1907 TiffEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1908 TiffFrameEncode
*result
;
1912 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
1914 EnterCriticalSection(&This
->lock
);
1916 if (!This
->initialized
|| This
->committed
)
1918 hr
= WINCODEC_ERR_WRONGSTATE
;
1920 else if (This
->num_frames
!= This
->num_frames_committed
)
1922 FIXME("New frame created before previous frame was committed\n");
1928 PROPBAG2 opts
[2]= {{0}};
1929 opts
[0].pstrName
= (LPOLESTR
)wszTiffCompressionMethod
;
1930 opts
[0].vt
= VT_UI1
;
1931 opts
[0].dwType
= PROPBAG2_TYPE_DATA
;
1933 opts
[1].pstrName
= (LPOLESTR
)wszCompressionQuality
;
1935 opts
[1].dwType
= PROPBAG2_TYPE_DATA
;
1937 hr
= CreatePropertyBag2(opts
, 2, ppIEncoderOptions
);
1944 V_UNION(&v
, bVal
) = WICTiffCompressionDontCare
;
1945 hr
= IPropertyBag2_Write(*ppIEncoderOptions
, 1, opts
, &v
);
1949 IPropertyBag2_Release(*ppIEncoderOptions
);
1950 *ppIEncoderOptions
= NULL
;
1957 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(*result
));
1961 result
->IWICBitmapFrameEncode_iface
.lpVtbl
= &TiffFrameEncode_Vtbl
;
1963 result
->parent
= This
;
1964 result
->initialized
= FALSE
;
1965 result
->info_written
= FALSE
;
1966 result
->committed
= FALSE
;
1967 result
->format
= NULL
;
1972 result
->lines_written
= 0;
1974 IWICBitmapEncoder_AddRef(iface
);
1975 *ppIFrameEncode
= &result
->IWICBitmapFrameEncode_iface
;
1977 if (This
->num_frames
!= 0)
1978 pTIFFWriteDirectory(This
->tiff
);
1987 IPropertyBag2_Release(*ppIEncoderOptions
);
1988 *ppIEncoderOptions
= NULL
;
1992 LeaveCriticalSection(&This
->lock
);
1997 static HRESULT WINAPI
TiffEncoder_Commit(IWICBitmapEncoder
*iface
)
1999 TiffEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
2001 TRACE("(%p)\n", iface
);
2003 EnterCriticalSection(&This
->lock
);
2005 if (!This
->initialized
|| This
->committed
)
2007 LeaveCriticalSection(&This
->lock
);
2008 return WINCODEC_ERR_WRONGSTATE
;
2011 pTIFFClose(This
->tiff
);
2012 IStream_Release(This
->stream
);
2013 This
->stream
= NULL
;
2016 This
->committed
= TRUE
;
2018 LeaveCriticalSection(&This
->lock
);
2023 static HRESULT WINAPI
TiffEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
2024 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
2026 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
2030 static const IWICBitmapEncoderVtbl TiffEncoder_Vtbl
= {
2031 TiffEncoder_QueryInterface
,
2033 TiffEncoder_Release
,
2034 TiffEncoder_Initialize
,
2035 TiffEncoder_GetContainerFormat
,
2036 TiffEncoder_GetEncoderInfo
,
2037 TiffEncoder_SetColorContexts
,
2038 TiffEncoder_SetPalette
,
2039 TiffEncoder_SetThumbnail
,
2040 TiffEncoder_SetPreview
,
2041 TiffEncoder_CreateNewFrame
,
2043 TiffEncoder_GetMetadataQueryWriter
2046 HRESULT
TiffEncoder_CreateInstance(REFIID iid
, void** ppv
)
2051 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
2055 if (!load_libtiff())
2057 ERR("Failed writing TIFF because unable to load %s\n",SONAME_LIBTIFF
);
2061 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(TiffEncoder
));
2062 if (!This
) return E_OUTOFMEMORY
;
2064 This
->IWICBitmapEncoder_iface
.lpVtbl
= &TiffEncoder_Vtbl
;
2066 This
->stream
= NULL
;
2067 InitializeCriticalSection(&This
->lock
);
2068 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TiffEncoder.lock");
2070 This
->initialized
= FALSE
;
2071 This
->num_frames
= 0;
2072 This
->num_frames_committed
= 0;
2073 This
->committed
= FALSE
;
2075 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
2076 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
2081 #else /* !SONAME_LIBTIFF */
2083 HRESULT
TiffDecoder_CreateInstance(REFIID iid
, void** ppv
)
2085 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
2089 HRESULT
TiffEncoder_CreateInstance(REFIID iid
, void** ppv
)
2091 ERR("Trying to save TIFF picture, but Wine was compiled without TIFF support.\n");