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 "wincodecs_private.h"
38 #include "wine/debug.h"
39 #include "wine/library.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
45 /* Workaround for broken libtiff 4.x headers on some 64-bit hosts which
46 * define TIFF_UINT64_T/toff_t as 32-bit for 32-bit builds, while they
47 * are supposed to be always 64-bit.
48 * TIFF_UINT64_T doesn't exist in libtiff 3.x, it was introduced in 4.x.
52 # define toff_t UINT64
55 static CRITICAL_SECTION init_tiff_cs
;
56 static CRITICAL_SECTION_DEBUG init_tiff_cs_debug
=
59 { &init_tiff_cs_debug
.ProcessLocksList
,
60 &init_tiff_cs_debug
.ProcessLocksList
},
61 0, 0, { (DWORD_PTR
)(__FILE__
": init_tiff_cs") }
63 static CRITICAL_SECTION init_tiff_cs
= { &init_tiff_cs_debug
, -1, 0, 0, 0, 0 };
65 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};
66 static const WCHAR wszCompressionQuality
[] = {'C','o','m','p','r','e','s','s','i','o','n','Q','u','a','l','i','t','y',0};
68 static void *libtiff_handle
;
69 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
70 MAKE_FUNCPTR(TIFFClientOpen
);
71 MAKE_FUNCPTR(TIFFClose
);
72 MAKE_FUNCPTR(TIFFCurrentDirOffset
);
73 MAKE_FUNCPTR(TIFFGetField
);
74 MAKE_FUNCPTR(TIFFIsByteSwapped
);
75 MAKE_FUNCPTR(TIFFNumberOfDirectories
);
76 MAKE_FUNCPTR(TIFFReadDirectory
);
77 MAKE_FUNCPTR(TIFFReadEncodedStrip
);
78 MAKE_FUNCPTR(TIFFReadEncodedTile
);
79 MAKE_FUNCPTR(TIFFSetDirectory
);
80 MAKE_FUNCPTR(TIFFSetField
);
81 MAKE_FUNCPTR(TIFFWriteDirectory
);
82 MAKE_FUNCPTR(TIFFWriteScanline
);
85 static void *load_libtiff(void)
89 EnterCriticalSection(&init_tiff_cs
);
91 if (!libtiff_handle
&&
92 (libtiff_handle
= wine_dlopen(SONAME_LIBTIFF
, RTLD_NOW
, NULL
, 0)) != NULL
)
94 void * (*pTIFFSetWarningHandler
)(void *);
95 void * (*pTIFFSetWarningHandlerExt
)(void *);
97 #define LOAD_FUNCPTR(f) \
98 if((p##f = wine_dlsym(libtiff_handle, #f, NULL, 0)) == NULL) { \
99 ERR("failed to load symbol %s\n", #f); \
100 libtiff_handle = NULL; \
101 LeaveCriticalSection(&init_tiff_cs); \
104 LOAD_FUNCPTR(TIFFClientOpen
);
105 LOAD_FUNCPTR(TIFFClose
);
106 LOAD_FUNCPTR(TIFFCurrentDirOffset
);
107 LOAD_FUNCPTR(TIFFGetField
);
108 LOAD_FUNCPTR(TIFFIsByteSwapped
);
109 LOAD_FUNCPTR(TIFFNumberOfDirectories
);
110 LOAD_FUNCPTR(TIFFReadDirectory
);
111 LOAD_FUNCPTR(TIFFReadEncodedStrip
);
112 LOAD_FUNCPTR(TIFFReadEncodedTile
);
113 LOAD_FUNCPTR(TIFFSetDirectory
);
114 LOAD_FUNCPTR(TIFFSetField
);
115 LOAD_FUNCPTR(TIFFWriteDirectory
);
116 LOAD_FUNCPTR(TIFFWriteScanline
);
119 if ((pTIFFSetWarningHandler
= wine_dlsym(libtiff_handle
, "TIFFSetWarningHandler", NULL
, 0)))
120 pTIFFSetWarningHandler(NULL
);
121 if ((pTIFFSetWarningHandlerExt
= wine_dlsym(libtiff_handle
, "TIFFSetWarningHandlerExt", NULL
, 0)))
122 pTIFFSetWarningHandlerExt(NULL
);
125 result
= libtiff_handle
;
127 LeaveCriticalSection(&init_tiff_cs
);
131 static tsize_t
tiff_stream_read(thandle_t client_data
, tdata_t data
, tsize_t size
)
133 IStream
*stream
= (IStream
*)client_data
;
137 hr
= IStream_Read(stream
, data
, size
, &bytes_read
);
138 if (FAILED(hr
)) bytes_read
= 0;
142 static tsize_t
tiff_stream_write(thandle_t client_data
, tdata_t data
, tsize_t size
)
144 IStream
*stream
= (IStream
*)client_data
;
148 hr
= IStream_Write(stream
, data
, size
, &bytes_written
);
149 if (FAILED(hr
)) bytes_written
= 0;
150 return bytes_written
;
153 static toff_t
tiff_stream_seek(thandle_t client_data
, toff_t offset
, int whence
)
155 IStream
*stream
= (IStream
*)client_data
;
158 ULARGE_INTEGER new_position
;
161 move
.QuadPart
= offset
;
165 origin
= STREAM_SEEK_SET
;
168 origin
= STREAM_SEEK_CUR
;
171 origin
= STREAM_SEEK_END
;
174 ERR("unknown whence value %i\n", whence
);
178 hr
= IStream_Seek(stream
, move
, origin
, &new_position
);
179 if (SUCCEEDED(hr
)) return new_position
.QuadPart
;
183 static int tiff_stream_close(thandle_t client_data
)
185 /* Caller is responsible for releasing the stream object. */
189 static toff_t
tiff_stream_size(thandle_t client_data
)
191 IStream
*stream
= (IStream
*)client_data
;
195 hr
= IStream_Stat(stream
, &statstg
, STATFLAG_NONAME
);
197 if (SUCCEEDED(hr
)) return statstg
.cbSize
.QuadPart
;
201 static int tiff_stream_map(thandle_t client_data
, tdata_t
*addr
, toff_t
*size
)
203 /* Cannot mmap streams */
207 static void tiff_stream_unmap(thandle_t client_data
, tdata_t addr
, toff_t size
)
209 /* No need to ever do this, since we can't map things. */
212 static TIFF
* tiff_open_stream(IStream
*stream
, const char *mode
)
217 IStream_Seek(stream
, zero
, STREAM_SEEK_SET
, NULL
);
219 return pTIFFClientOpen("<IStream object>", mode
, stream
, tiff_stream_read
,
220 tiff_stream_write
, (void *)tiff_stream_seek
, tiff_stream_close
,
221 (void *)tiff_stream_size
, (void *)tiff_stream_map
, (void *)tiff_stream_unmap
);
225 IWICBitmapDecoder IWICBitmapDecoder_iface
;
228 CRITICAL_SECTION lock
; /* Must be held when tiff is used or initialized is set */
234 const WICPixelFormatGUID
*format
;
241 int invert_grayscale
;
243 UINT tile_width
, tile_height
;
248 UINT resolution_unit
;
253 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
254 IWICMetadataBlockReader IWICMetadataBlockReader_iface
;
258 tiff_decode_info decode_info
;
259 INT cached_tile_x
, cached_tile_y
;
263 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl
;
264 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl
;
266 static inline TiffDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
268 return CONTAINING_RECORD(iface
, TiffDecoder
, IWICBitmapDecoder_iface
);
271 static inline TiffFrameDecode
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
273 return CONTAINING_RECORD(iface
, TiffFrameDecode
, IWICBitmapFrameDecode_iface
);
276 static inline TiffFrameDecode
*impl_from_IWICMetadataBlockReader(IWICMetadataBlockReader
*iface
)
278 return CONTAINING_RECORD(iface
, TiffFrameDecode
, IWICMetadataBlockReader_iface
);
281 static HRESULT
tiff_get_decode_info(TIFF
*tiff
, tiff_decode_info
*decode_info
)
283 uint16 photometric
, bps
, samples
, planar
;
284 uint16 extra_sample_count
, extra_sample
, *extra_samples
;
287 decode_info
->indexed
= 0;
288 decode_info
->reverse_bgr
= 0;
289 decode_info
->invert_grayscale
= 0;
290 decode_info
->tiled
= 0;
292 ret
= pTIFFGetField(tiff
, TIFFTAG_PHOTOMETRIC
, &photometric
);
295 WARN("missing PhotometricInterpretation tag\n");
299 ret
= pTIFFGetField(tiff
, TIFFTAG_BITSPERSAMPLE
, &bps
);
301 decode_info
->bps
= bps
;
303 ret
= pTIFFGetField(tiff
, TIFFTAG_SAMPLESPERPIXEL
, &samples
);
304 if (!ret
) samples
= 1;
305 decode_info
->samples
= samples
;
311 ret
= pTIFFGetField(tiff
, TIFFTAG_PLANARCONFIG
, &planar
);
312 if (!ret
) planar
= 1;
315 FIXME("unhandled planar configuration %u\n", planar
);
319 decode_info
->planar
= planar
;
323 case 0: /* WhiteIsZero */
324 decode_info
->invert_grayscale
= 1;
326 case 1: /* BlackIsZero */
329 ret
= pTIFFGetField(tiff
, TIFFTAG_EXTRASAMPLES
, &extra_sample_count
, &extra_samples
);
332 extra_sample_count
= 1;
334 extra_samples
= &extra_sample
;
337 else if (samples
!= 1)
339 FIXME("unhandled %dbpp sample count %u\n", bps
, samples
);
343 decode_info
->bpp
= bps
* samples
;
344 decode_info
->source_bpp
= decode_info
->bpp
;
350 FIXME("unhandled 1bpp sample count %u\n", samples
);
353 decode_info
->format
= &GUID_WICPixelFormatBlackWhite
;
358 FIXME("unhandled 4bpp grayscale sample count %u\n", samples
);
361 decode_info
->format
= &GUID_WICPixelFormat4bppGray
;
365 decode_info
->format
= &GUID_WICPixelFormat8bppGray
;
368 decode_info
->bpp
= 32;
370 switch(extra_samples
[0])
372 case 1: /* Associated (pre-multiplied) alpha data */
373 decode_info
->format
= &GUID_WICPixelFormat32bppPBGRA
;
375 case 0: /* Unspecified data */
376 case 2: /* Unassociated alpha data */
377 decode_info
->format
= &GUID_WICPixelFormat32bppBGRA
;
380 FIXME("unhandled extra sample type %u\n", extra_samples
[0]);
386 FIXME("unhandled greyscale bit count %u\n", bps
);
391 decode_info
->bpp
= bps
* samples
;
395 ret
= pTIFFGetField(tiff
, TIFFTAG_EXTRASAMPLES
, &extra_sample_count
, &extra_samples
);
398 extra_sample_count
= 1;
400 extra_samples
= &extra_sample
;
403 else if (samples
!= 3)
405 FIXME("unhandled RGB sample count %u\n", samples
);
412 decode_info
->reverse_bgr
= 1;
414 decode_info
->format
= &GUID_WICPixelFormat24bppBGR
;
416 switch(extra_samples
[0])
418 case 1: /* Associated (pre-multiplied) alpha data */
419 decode_info
->format
= &GUID_WICPixelFormat32bppPBGRA
;
421 case 0: /* Unspecified data */
422 case 2: /* Unassociated alpha data */
423 decode_info
->format
= &GUID_WICPixelFormat32bppBGRA
;
426 FIXME("unhandled extra sample type %i\n", extra_samples
[0]);
432 decode_info
->format
= &GUID_WICPixelFormat48bppRGB
;
434 switch(extra_samples
[0])
436 case 1: /* Associated (pre-multiplied) alpha data */
437 decode_info
->format
= &GUID_WICPixelFormat64bppPRGBA
;
439 case 0: /* Unspecified data */
440 case 2: /* Unassociated alpha data */
441 decode_info
->format
= &GUID_WICPixelFormat64bppRGBA
;
444 FIXME("unhandled extra sample type %i\n", extra_samples
[0]);
449 FIXME("unhandled RGB bit count %u\n", bps
);
453 case 3: /* RGB Palette */
456 FIXME("unhandled indexed sample count %u\n", samples
);
460 decode_info
->indexed
= 1;
461 decode_info
->bpp
= bps
;
465 decode_info
->format
= &GUID_WICPixelFormat4bppIndexed
;
468 decode_info
->format
= &GUID_WICPixelFormat8bppIndexed
;
471 FIXME("unhandled indexed bit count %u\n", bps
);
475 case 4: /* Transparency mask */
480 FIXME("unhandled PhotometricInterpretation %u\n", photometric
);
484 ret
= pTIFFGetField(tiff
, TIFFTAG_IMAGEWIDTH
, &decode_info
->width
);
487 WARN("missing image width\n");
491 ret
= pTIFFGetField(tiff
, TIFFTAG_IMAGELENGTH
, &decode_info
->height
);
494 WARN("missing image length\n");
498 if ((ret
= pTIFFGetField(tiff
, TIFFTAG_TILEWIDTH
, &decode_info
->tile_width
)))
500 decode_info
->tiled
= 1;
502 ret
= pTIFFGetField(tiff
, TIFFTAG_TILELENGTH
, &decode_info
->tile_height
);
505 WARN("missing tile height\n");
509 decode_info
->tile_stride
= ((decode_info
->bpp
* decode_info
->tile_width
+ 7)/8);
510 decode_info
->tile_size
= decode_info
->tile_height
* decode_info
->tile_stride
;
511 decode_info
->tiles_across
= (decode_info
->width
+ decode_info
->tile_width
- 1) / decode_info
->tile_width
;
513 else if ((ret
= pTIFFGetField(tiff
, TIFFTAG_ROWSPERSTRIP
, &decode_info
->tile_height
)))
515 if (decode_info
->tile_height
> decode_info
->height
)
516 decode_info
->tile_height
= decode_info
->height
;
517 decode_info
->tile_width
= decode_info
->width
;
518 decode_info
->tile_stride
= ((decode_info
->bpp
* decode_info
->tile_width
+ 7)/8);
519 decode_info
->tile_size
= decode_info
->tile_height
* decode_info
->tile_stride
;
523 /* Some broken TIFF files have a single strip and lack the RowsPerStrip tag */
524 decode_info
->tile_height
= decode_info
->height
;
525 decode_info
->tile_width
= decode_info
->width
;
526 decode_info
->tile_stride
= ((decode_info
->bpp
* decode_info
->tile_width
+ 7)/8);
527 decode_info
->tile_size
= decode_info
->tile_height
* decode_info
->tile_stride
;
530 decode_info
->resolution_unit
= 0;
531 pTIFFGetField(tiff
, TIFFTAG_RESOLUTIONUNIT
, &decode_info
->resolution_unit
);
532 if (decode_info
->resolution_unit
!= 0)
534 ret
= pTIFFGetField(tiff
, TIFFTAG_XRESOLUTION
, &decode_info
->xres
);
537 WARN("missing X resolution\n");
538 decode_info
->resolution_unit
= 0;
541 ret
= pTIFFGetField(tiff
, TIFFTAG_YRESOLUTION
, &decode_info
->yres
);
544 WARN("missing Y resolution\n");
545 decode_info
->resolution_unit
= 0;
552 static HRESULT WINAPI
TiffDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
555 TiffDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
556 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
558 if (!ppv
) return E_INVALIDARG
;
560 if (IsEqualIID(&IID_IUnknown
, iid
) ||
561 IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
563 *ppv
= &This
->IWICBitmapDecoder_iface
;
568 return E_NOINTERFACE
;
571 IUnknown_AddRef((IUnknown
*)*ppv
);
575 static ULONG WINAPI
TiffDecoder_AddRef(IWICBitmapDecoder
*iface
)
577 TiffDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
578 ULONG ref
= InterlockedIncrement(&This
->ref
);
580 TRACE("(%p) refcount=%u\n", iface
, ref
);
585 static ULONG WINAPI
TiffDecoder_Release(IWICBitmapDecoder
*iface
)
587 TiffDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
588 ULONG ref
= InterlockedDecrement(&This
->ref
);
590 TRACE("(%p) refcount=%u\n", iface
, ref
);
594 if (This
->tiff
) pTIFFClose(This
->tiff
);
595 if (This
->stream
) IStream_Release(This
->stream
);
596 This
->lock
.DebugInfo
->Spare
[0] = 0;
597 DeleteCriticalSection(&This
->lock
);
598 HeapFree(GetProcessHeap(), 0, This
);
604 static HRESULT WINAPI
TiffDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
609 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
611 if (!stream
|| !capability
) return E_INVALIDARG
;
613 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
614 if (hr
!= S_OK
) return hr
;
616 *capability
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
617 WICBitmapDecoderCapabilityCanDecodeSomeImages
|
618 WICBitmapDecoderCapabilityCanEnumerateMetadata
;
622 static HRESULT WINAPI
TiffDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
623 WICDecodeOptions cacheOptions
)
625 TiffDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
629 TRACE("(%p,%p,%x): stub\n", iface
, pIStream
, cacheOptions
);
631 EnterCriticalSection(&This
->lock
);
633 if (This
->initialized
)
635 hr
= WINCODEC_ERR_WRONGSTATE
;
639 tiff
= tiff_open_stream(pIStream
, "r");
648 This
->stream
= pIStream
;
649 IStream_AddRef(pIStream
);
650 This
->initialized
= TRUE
;
653 LeaveCriticalSection(&This
->lock
);
657 static HRESULT WINAPI
TiffDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
658 GUID
*pguidContainerFormat
)
660 if (!pguidContainerFormat
) return E_INVALIDARG
;
662 memcpy(pguidContainerFormat
, &GUID_ContainerFormatTiff
, sizeof(GUID
));
666 static HRESULT WINAPI
TiffDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
667 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
670 IWICComponentInfo
*compinfo
;
672 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
674 hr
= CreateComponentInfo(&CLSID_WICTiffDecoder
, &compinfo
);
675 if (FAILED(hr
)) return hr
;
677 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
678 (void**)ppIDecoderInfo
);
680 IWICComponentInfo_Release(compinfo
);
685 static HRESULT WINAPI
TiffDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
686 IWICPalette
*pIPalette
)
688 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
692 static HRESULT WINAPI
TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
693 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
695 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
699 static HRESULT WINAPI
TiffDecoder_GetPreview(IWICBitmapDecoder
*iface
,
700 IWICBitmapSource
**ppIBitmapSource
)
702 TRACE("(%p,%p)\n", iface
, ppIBitmapSource
);
704 if (!ppIBitmapSource
) return E_INVALIDARG
;
706 *ppIBitmapSource
= NULL
;
707 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
710 static HRESULT WINAPI
TiffDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
711 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
713 FIXME("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
714 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
717 static HRESULT WINAPI
TiffDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
718 IWICBitmapSource
**ppIThumbnail
)
720 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
722 if (!ppIThumbnail
) return E_INVALIDARG
;
724 *ppIThumbnail
= NULL
;
725 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
728 static HRESULT WINAPI
TiffDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
731 TiffDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
733 if (!pCount
) return E_INVALIDARG
;
735 EnterCriticalSection(&This
->lock
);
736 *pCount
= This
->tiff
? pTIFFNumberOfDirectories(This
->tiff
) : 0;
737 LeaveCriticalSection(&This
->lock
);
739 TRACE("(%p) <-- %i\n", iface
, *pCount
);
744 static HRESULT WINAPI
TiffDecoder_GetFrame(IWICBitmapDecoder
*iface
,
745 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
747 TiffDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
748 TiffFrameDecode
*result
;
750 tiff_decode_info decode_info
;
753 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
756 return WINCODEC_ERR_FRAMEMISSING
;
758 EnterCriticalSection(&This
->lock
);
759 res
= pTIFFSetDirectory(This
->tiff
, index
);
760 if (!res
) hr
= E_INVALIDARG
;
761 else hr
= tiff_get_decode_info(This
->tiff
, &decode_info
);
762 LeaveCriticalSection(&This
->lock
);
766 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode
));
770 result
->IWICBitmapFrameDecode_iface
.lpVtbl
= &TiffFrameDecode_Vtbl
;
771 result
->IWICMetadataBlockReader_iface
.lpVtbl
= &TiffFrameDecode_BlockVtbl
;
773 result
->parent
= This
;
774 result
->index
= index
;
775 result
->decode_info
= decode_info
;
776 result
->cached_tile_x
= -1;
777 result
->cached_tile
= HeapAlloc(GetProcessHeap(), 0, decode_info
.tile_size
);
779 if (result
->cached_tile
)
780 *ppIBitmapFrame
= &result
->IWICBitmapFrameDecode_iface
;
784 HeapFree(GetProcessHeap(), 0, result
);
787 else hr
= E_OUTOFMEMORY
;
790 if (FAILED(hr
)) *ppIBitmapFrame
= NULL
;
795 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl
= {
796 TiffDecoder_QueryInterface
,
799 TiffDecoder_QueryCapability
,
800 TiffDecoder_Initialize
,
801 TiffDecoder_GetContainerFormat
,
802 TiffDecoder_GetDecoderInfo
,
803 TiffDecoder_CopyPalette
,
804 TiffDecoder_GetMetadataQueryReader
,
805 TiffDecoder_GetPreview
,
806 TiffDecoder_GetColorContexts
,
807 TiffDecoder_GetThumbnail
,
808 TiffDecoder_GetFrameCount
,
812 static HRESULT WINAPI
TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
815 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
816 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
818 if (!ppv
) return E_INVALIDARG
;
820 if (IsEqualIID(&IID_IUnknown
, iid
) ||
821 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
822 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
824 *ppv
= &This
->IWICBitmapFrameDecode_iface
;
826 else if (IsEqualIID(&IID_IWICMetadataBlockReader
, iid
))
828 *ppv
= &This
->IWICMetadataBlockReader_iface
;
833 return E_NOINTERFACE
;
836 IUnknown_AddRef((IUnknown
*)*ppv
);
840 static ULONG WINAPI
TiffFrameDecode_AddRef(IWICBitmapFrameDecode
*iface
)
842 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
843 ULONG ref
= InterlockedIncrement(&This
->ref
);
845 TRACE("(%p) refcount=%u\n", iface
, ref
);
850 static ULONG WINAPI
TiffFrameDecode_Release(IWICBitmapFrameDecode
*iface
)
852 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
853 ULONG ref
= InterlockedDecrement(&This
->ref
);
855 TRACE("(%p) refcount=%u\n", iface
, ref
);
859 HeapFree(GetProcessHeap(), 0, This
->cached_tile
);
860 HeapFree(GetProcessHeap(), 0, This
);
866 static HRESULT WINAPI
TiffFrameDecode_GetSize(IWICBitmapFrameDecode
*iface
,
867 UINT
*puiWidth
, UINT
*puiHeight
)
869 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
871 *puiWidth
= This
->decode_info
.width
;
872 *puiHeight
= This
->decode_info
.height
;
874 TRACE("(%p) <-- %ux%u\n", iface
, *puiWidth
, *puiHeight
);
879 static HRESULT WINAPI
TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
880 WICPixelFormatGUID
*pPixelFormat
)
882 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
884 memcpy(pPixelFormat
, This
->decode_info
.format
, sizeof(GUID
));
886 TRACE("(%p) <-- %s\n", This
, debugstr_guid(This
->decode_info
.format
));
891 static HRESULT WINAPI
TiffFrameDecode_GetResolution(IWICBitmapFrameDecode
*iface
,
892 double *pDpiX
, double *pDpiY
)
894 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
896 switch (This
->decode_info
.resolution_unit
)
899 FIXME("unknown resolution unit %i\n", This
->decode_info
.resolution_unit
);
901 case 0: /* Not set */
902 *pDpiX
= *pDpiY
= 96.0;
904 case 1: /* Relative measurements */
906 *pDpiY
= 96.0 * This
->decode_info
.yres
/ This
->decode_info
.xres
;
909 *pDpiX
= This
->decode_info
.xres
;
910 *pDpiY
= This
->decode_info
.yres
;
912 case 3: /* Centimeter */
913 *pDpiX
= This
->decode_info
.xres
/ 2.54;
914 *pDpiY
= This
->decode_info
.yres
/ 2.54;
918 TRACE("(%p) <-- %f,%f unit=%i\n", iface
, *pDpiX
, *pDpiY
, This
->decode_info
.resolution_unit
);
923 static HRESULT WINAPI
TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode
*iface
,
924 IWICPalette
*pIPalette
)
926 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
927 uint16
*red
, *green
, *blue
;
928 WICColor colors
[256];
929 int color_count
, ret
, i
;
931 TRACE("(%p,%p)\n", iface
, pIPalette
);
933 color_count
= 1<<This
->decode_info
.bps
;
935 EnterCriticalSection(&This
->parent
->lock
);
936 ret
= pTIFFGetField(This
->parent
->tiff
, TIFFTAG_COLORMAP
, &red
, &green
, &blue
);
937 LeaveCriticalSection(&This
->parent
->lock
);
941 WARN("Couldn't read color map\n");
942 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
945 for (i
=0; i
<color_count
; i
++)
947 colors
[i
] = 0xff000000 |
948 ((red
[i
]<<8) & 0xff0000) |
949 (green
[i
] & 0xff00) |
950 ((blue
[i
]>>8) & 0xff);
953 return IWICPalette_InitializeCustom(pIPalette
, colors
, color_count
);
956 static HRESULT
TiffFrameDecode_ReadTile(TiffFrameDecode
*This
, UINT tile_x
, UINT tile_y
)
962 swap_bytes
= pTIFFIsByteSwapped(This
->parent
->tiff
);
964 ret
= pTIFFSetDirectory(This
->parent
->tiff
, This
->index
);
971 if (This
->decode_info
.tiled
)
973 ret
= pTIFFReadEncodedTile(This
->parent
->tiff
, tile_x
+ tile_y
* This
->decode_info
.tiles_across
, This
->cached_tile
, This
->decode_info
.tile_size
);
977 ret
= pTIFFReadEncodedStrip(This
->parent
->tiff
, tile_y
, This
->cached_tile
, This
->decode_info
.tile_size
);
984 /* 8bpp grayscale with extra alpha */
985 if (hr
== S_OK
&& This
->decode_info
.source_bpp
== 16 && This
->decode_info
.samples
== 2 && This
->decode_info
.bpp
== 32)
988 DWORD
*dst
, count
= This
->decode_info
.tile_width
* This
->decode_info
.tile_height
;
990 src
= This
->cached_tile
+ This
->decode_info
.tile_width
* This
->decode_info
.tile_height
* 2 - 2;
991 dst
= (DWORD
*)(This
->cached_tile
+ This
->decode_info
.tile_size
- 4);
995 *dst
-- = src
[0] | (src
[0] << 8) | (src
[0] << 16) | (src
[1] << 24);
1000 if (hr
== S_OK
&& This
->decode_info
.reverse_bgr
)
1002 if (This
->decode_info
.bps
== 8)
1004 UINT sample_count
= This
->decode_info
.samples
;
1006 reverse_bgr8(sample_count
, This
->cached_tile
, This
->decode_info
.tile_width
,
1007 This
->decode_info
.tile_height
, This
->decode_info
.tile_width
* sample_count
);
1011 if (hr
== S_OK
&& swap_bytes
&& This
->decode_info
.bps
> 8)
1013 UINT row
, i
, samples_per_row
;
1016 samples_per_row
= This
->decode_info
.tile_width
* This
->decode_info
.samples
;
1018 switch(This
->decode_info
.bps
)
1021 for (row
=0; row
<This
->decode_info
.tile_height
; row
++)
1023 sample
= This
->cached_tile
+ row
* This
->decode_info
.tile_stride
;
1024 for (i
=0; i
<samples_per_row
; i
++)
1027 sample
[1] = sample
[0];
1034 ERR("unhandled bps for byte swap %u\n", This
->decode_info
.bps
);
1039 if (hr
== S_OK
&& This
->decode_info
.invert_grayscale
)
1043 if (This
->decode_info
.samples
!= 1)
1045 ERR("cannot invert grayscale image with %u samples\n", This
->decode_info
.samples
);
1049 end
= This
->cached_tile
+This
->decode_info
.tile_size
;
1051 for (byte
= This
->cached_tile
; byte
!= end
; byte
++)
1057 This
->cached_tile_x
= tile_x
;
1058 This
->cached_tile_y
= tile_y
;
1064 static HRESULT WINAPI
TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode
*iface
,
1065 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
1067 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
1068 UINT min_tile_x
, max_tile_x
, min_tile_y
, max_tile_y
;
1069 UINT tile_x
, tile_y
;
1076 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
1082 rect
.Width
= This
->decode_info
.width
;
1083 rect
.Height
= This
->decode_info
.height
;
1088 if (prc
->X
< 0 || prc
->Y
< 0 || prc
->X
+prc
->Width
> This
->decode_info
.width
||
1089 prc
->Y
+prc
->Height
> This
->decode_info
.height
)
1090 return E_INVALIDARG
;
1093 bytesperrow
= ((This
->decode_info
.bpp
* prc
->Width
)+7)/8;
1095 if (cbStride
< bytesperrow
)
1096 return E_INVALIDARG
;
1098 if ((cbStride
* prc
->Height
) > cbBufferSize
)
1099 return E_INVALIDARG
;
1101 min_tile_x
= prc
->X
/ This
->decode_info
.tile_width
;
1102 min_tile_y
= prc
->Y
/ This
->decode_info
.tile_height
;
1103 max_tile_x
= (prc
->X
+prc
->Width
-1) / This
->decode_info
.tile_width
;
1104 max_tile_y
= (prc
->Y
+prc
->Height
-1) / This
->decode_info
.tile_height
;
1106 EnterCriticalSection(&This
->parent
->lock
);
1108 for (tile_x
=min_tile_x
; tile_x
<= max_tile_x
; tile_x
++)
1110 for (tile_y
=min_tile_y
; tile_y
<= max_tile_y
; tile_y
++)
1112 if (tile_x
!= This
->cached_tile_x
|| tile_y
!= This
->cached_tile_y
)
1114 hr
= TiffFrameDecode_ReadTile(This
, tile_x
, tile_y
);
1119 if (prc
->X
< tile_x
* This
->decode_info
.tile_width
)
1122 rc
.X
= prc
->X
- tile_x
* This
->decode_info
.tile_width
;
1124 if (prc
->Y
< tile_y
* This
->decode_info
.tile_height
)
1127 rc
.Y
= prc
->Y
- tile_y
* This
->decode_info
.tile_height
;
1129 if (prc
->X
+prc
->Width
> (tile_x
+1) * This
->decode_info
.tile_width
)
1130 rc
.Width
= This
->decode_info
.tile_width
- rc
.X
;
1131 else if (prc
->X
< tile_x
* This
->decode_info
.tile_width
)
1132 rc
.Width
= prc
->Width
+ prc
->X
- tile_x
* This
->decode_info
.tile_width
;
1134 rc
.Width
= prc
->Width
;
1136 if (prc
->Y
+prc
->Height
> (tile_y
+1) * This
->decode_info
.tile_height
)
1137 rc
.Height
= This
->decode_info
.tile_height
- rc
.Y
;
1138 else if (prc
->Y
< tile_y
* This
->decode_info
.tile_height
)
1139 rc
.Height
= prc
->Height
+ prc
->Y
- tile_y
* This
->decode_info
.tile_height
;
1141 rc
.Height
= prc
->Height
;
1143 dst_tilepos
= pbBuffer
+ (cbStride
* ((rc
.Y
+ tile_y
* This
->decode_info
.tile_height
) - prc
->Y
)) +
1144 ((This
->decode_info
.bpp
* ((rc
.X
+ tile_x
* This
->decode_info
.tile_width
) - prc
->X
) + 7) / 8);
1146 hr
= copy_pixels(This
->decode_info
.bpp
, This
->cached_tile
,
1147 This
->decode_info
.tile_width
, This
->decode_info
.tile_height
, This
->decode_info
.tile_stride
,
1148 &rc
, cbStride
, cbBufferSize
, dst_tilepos
);
1153 LeaveCriticalSection(&This
->parent
->lock
);
1154 TRACE("<-- 0x%x\n", hr
);
1160 LeaveCriticalSection(&This
->parent
->lock
);
1165 static HRESULT WINAPI
TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
1166 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
1168 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
1170 TRACE("(%p,%p)\n", iface
, ppIMetadataQueryReader
);
1172 if (!ppIMetadataQueryReader
)
1173 return E_INVALIDARG
;
1175 return MetadataQueryReader_CreateInstance(&This
->IWICMetadataBlockReader_iface
, ppIMetadataQueryReader
);
1178 static HRESULT WINAPI
TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode
*iface
,
1179 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
1181 TiffFrameDecode
*This
= impl_from_IWICBitmapFrameDecode(iface
);
1182 const BYTE
*profile
;
1186 TRACE("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
1188 EnterCriticalSection(&This
->parent
->lock
);
1190 if (pTIFFGetField(This
->parent
->tiff
, TIFFTAG_ICCPROFILE
, &len
, &profile
))
1192 if (cCount
&& ppIColorContexts
)
1194 hr
= IWICColorContext_InitializeFromMemory(*ppIColorContexts
, profile
, len
);
1197 LeaveCriticalSection(&This
->parent
->lock
);
1206 LeaveCriticalSection(&This
->parent
->lock
);
1211 static HRESULT WINAPI
TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode
*iface
,
1212 IWICBitmapSource
**ppIThumbnail
)
1214 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
1216 if (!ppIThumbnail
) return E_INVALIDARG
;
1218 *ppIThumbnail
= NULL
;
1219 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
1222 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl
= {
1223 TiffFrameDecode_QueryInterface
,
1224 TiffFrameDecode_AddRef
,
1225 TiffFrameDecode_Release
,
1226 TiffFrameDecode_GetSize
,
1227 TiffFrameDecode_GetPixelFormat
,
1228 TiffFrameDecode_GetResolution
,
1229 TiffFrameDecode_CopyPalette
,
1230 TiffFrameDecode_CopyPixels
,
1231 TiffFrameDecode_GetMetadataQueryReader
,
1232 TiffFrameDecode_GetColorContexts
,
1233 TiffFrameDecode_GetThumbnail
1236 static HRESULT WINAPI
TiffFrameDecode_Block_QueryInterface(IWICMetadataBlockReader
*iface
,
1237 REFIID iid
, void **ppv
)
1239 TiffFrameDecode
*This
= impl_from_IWICMetadataBlockReader(iface
);
1240 return IWICBitmapFrameDecode_QueryInterface(&This
->IWICBitmapFrameDecode_iface
, iid
, ppv
);
1243 static ULONG WINAPI
TiffFrameDecode_Block_AddRef(IWICMetadataBlockReader
*iface
)
1245 TiffFrameDecode
*This
= impl_from_IWICMetadataBlockReader(iface
);
1246 return IWICBitmapFrameDecode_AddRef(&This
->IWICBitmapFrameDecode_iface
);
1249 static ULONG WINAPI
TiffFrameDecode_Block_Release(IWICMetadataBlockReader
*iface
)
1251 TiffFrameDecode
*This
= impl_from_IWICMetadataBlockReader(iface
);
1252 return IWICBitmapFrameDecode_Release(&This
->IWICBitmapFrameDecode_iface
);
1255 static HRESULT WINAPI
TiffFrameDecode_Block_GetContainerFormat(IWICMetadataBlockReader
*iface
,
1258 TRACE("(%p,%p)\n", iface
, guid
);
1260 if (!guid
) return E_INVALIDARG
;
1262 *guid
= GUID_ContainerFormatTiff
;
1266 static HRESULT WINAPI
TiffFrameDecode_Block_GetCount(IWICMetadataBlockReader
*iface
,
1269 TRACE("%p,%p\n", iface
, count
);
1271 if (!count
) return E_INVALIDARG
;
1277 static HRESULT
create_metadata_reader(TiffFrameDecode
*This
, IWICMetadataReader
**reader
)
1280 LARGE_INTEGER dir_offset
;
1281 IWICMetadataReader
*metadata_reader
;
1282 IWICPersistStream
*persist
;
1284 /* FIXME: Use IWICComponentFactory_CreateMetadataReader once it's implemented */
1286 hr
= IfdMetadataReader_CreateInstance(&IID_IWICMetadataReader
, (void **)&metadata_reader
);
1287 if (FAILED(hr
)) return hr
;
1289 hr
= IWICMetadataReader_QueryInterface(metadata_reader
, &IID_IWICPersistStream
, (void **)&persist
);
1292 IWICMetadataReader_Release(metadata_reader
);
1296 EnterCriticalSection(&This
->parent
->lock
);
1298 dir_offset
.QuadPart
= pTIFFCurrentDirOffset(This
->parent
->tiff
);
1299 hr
= IStream_Seek(This
->parent
->stream
, dir_offset
, STREAM_SEEK_SET
, NULL
);
1302 BOOL byte_swapped
= pTIFFIsByteSwapped(This
->parent
->tiff
);
1303 #ifdef WORDS_BIGENDIAN
1304 DWORD persist_options
= byte_swapped
? WICPersistOptionsLittleEndian
: WICPersistOptionsBigEndian
;
1306 DWORD persist_options
= byte_swapped
? WICPersistOptionsBigEndian
: WICPersistOptionsLittleEndian
;
1308 persist_options
|= WICPersistOptionsNoCacheStream
;
1309 hr
= IWICPersistStream_LoadEx(persist
, This
->parent
->stream
, NULL
, persist_options
);
1311 ERR("IWICPersistStream_LoadEx error %#x\n", hr
);
1314 LeaveCriticalSection(&This
->parent
->lock
);
1316 IWICPersistStream_Release(persist
);
1320 IWICMetadataReader_Release(metadata_reader
);
1324 *reader
= metadata_reader
;
1328 static HRESULT WINAPI
TiffFrameDecode_Block_GetReaderByIndex(IWICMetadataBlockReader
*iface
,
1329 UINT index
, IWICMetadataReader
**reader
)
1331 TiffFrameDecode
*This
= impl_from_IWICMetadataBlockReader(iface
);
1333 TRACE("(%p,%u,%p)\n", iface
, index
, reader
);
1335 if (!reader
|| index
!= 0) return E_INVALIDARG
;
1337 return create_metadata_reader(This
, reader
);
1340 static HRESULT WINAPI
TiffFrameDecode_Block_GetEnumerator(IWICMetadataBlockReader
*iface
,
1341 IEnumUnknown
**enum_metadata
)
1343 FIXME("(%p,%p): stub\n", iface
, enum_metadata
);
1347 static const IWICMetadataBlockReaderVtbl TiffFrameDecode_BlockVtbl
=
1349 TiffFrameDecode_Block_QueryInterface
,
1350 TiffFrameDecode_Block_AddRef
,
1351 TiffFrameDecode_Block_Release
,
1352 TiffFrameDecode_Block_GetContainerFormat
,
1353 TiffFrameDecode_Block_GetCount
,
1354 TiffFrameDecode_Block_GetReaderByIndex
,
1355 TiffFrameDecode_Block_GetEnumerator
1358 HRESULT
TiffDecoder_CreateInstance(REFIID iid
, void** ppv
)
1363 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
1367 if (!load_libtiff())
1369 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF
);
1373 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder
));
1374 if (!This
) return E_OUTOFMEMORY
;
1376 This
->IWICBitmapDecoder_iface
.lpVtbl
= &TiffDecoder_Vtbl
;
1378 This
->stream
= NULL
;
1379 InitializeCriticalSection(&This
->lock
);
1380 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TiffDecoder.lock");
1382 This
->initialized
= FALSE
;
1384 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
1385 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
1390 struct tiff_encode_format
{
1391 const WICPixelFormatGUID
*guid
;
1397 int extra_sample_type
;
1401 static const struct tiff_encode_format formats
[] = {
1402 {&GUID_WICPixelFormat24bppBGR
, 2, 8, 3, 24, 0, 0, 1},
1403 {&GUID_WICPixelFormat24bppRGB
, 2, 8, 3, 24, 0, 0, 0},
1404 {&GUID_WICPixelFormatBlackWhite
, 1, 1, 1, 1, 0, 0, 0},
1405 {&GUID_WICPixelFormat4bppGray
, 1, 4, 1, 4, 0, 0, 0},
1406 {&GUID_WICPixelFormat8bppGray
, 1, 8, 1, 8, 0, 0, 0},
1407 {&GUID_WICPixelFormat32bppBGRA
, 2, 8, 4, 32, 1, 2, 1},
1408 {&GUID_WICPixelFormat32bppPBGRA
, 2, 8, 4, 32, 1, 1, 1},
1409 {&GUID_WICPixelFormat48bppRGB
, 2, 16, 3, 48, 0, 0, 0},
1410 {&GUID_WICPixelFormat64bppRGBA
, 2, 16, 4, 64, 1, 2, 0},
1411 {&GUID_WICPixelFormat64bppPRGBA
, 2, 16, 4, 64, 1, 1, 0},
1415 typedef struct TiffEncoder
{
1416 IWICBitmapEncoder IWICBitmapEncoder_iface
;
1419 CRITICAL_SECTION lock
; /* Must be held when tiff is used or fields below are set */
1424 ULONG num_frames_committed
;
1427 static inline TiffEncoder
*impl_from_IWICBitmapEncoder(IWICBitmapEncoder
*iface
)
1429 return CONTAINING_RECORD(iface
, TiffEncoder
, IWICBitmapEncoder_iface
);
1432 typedef struct TiffFrameEncode
{
1433 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface
;
1435 TiffEncoder
*parent
;
1436 /* fields below are protected by parent->lock */
1440 const struct tiff_encode_format
*format
;
1446 static inline TiffFrameEncode
*impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode
*iface
)
1448 return CONTAINING_RECORD(iface
, TiffFrameEncode
, IWICBitmapFrameEncode_iface
);
1451 static HRESULT WINAPI
TiffFrameEncode_QueryInterface(IWICBitmapFrameEncode
*iface
, REFIID iid
,
1454 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1455 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1457 if (!ppv
) return E_INVALIDARG
;
1459 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1460 IsEqualIID(&IID_IWICBitmapFrameEncode
, iid
))
1462 *ppv
= &This
->IWICBitmapFrameEncode_iface
;
1467 return E_NOINTERFACE
;
1470 IUnknown_AddRef((IUnknown
*)*ppv
);
1474 static ULONG WINAPI
TiffFrameEncode_AddRef(IWICBitmapFrameEncode
*iface
)
1476 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1477 ULONG ref
= InterlockedIncrement(&This
->ref
);
1479 TRACE("(%p) refcount=%u\n", iface
, ref
);
1484 static ULONG WINAPI
TiffFrameEncode_Release(IWICBitmapFrameEncode
*iface
)
1486 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1487 ULONG ref
= InterlockedDecrement(&This
->ref
);
1489 TRACE("(%p) refcount=%u\n", iface
, ref
);
1493 IWICBitmapEncoder_Release(&This
->parent
->IWICBitmapEncoder_iface
);
1494 HeapFree(GetProcessHeap(), 0, This
);
1500 static HRESULT WINAPI
TiffFrameEncode_Initialize(IWICBitmapFrameEncode
*iface
,
1501 IPropertyBag2
*pIEncoderOptions
)
1503 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1504 TRACE("(%p,%p)\n", iface
, pIEncoderOptions
);
1506 EnterCriticalSection(&This
->parent
->lock
);
1508 if (This
->initialized
)
1510 LeaveCriticalSection(&This
->parent
->lock
);
1511 return WINCODEC_ERR_WRONGSTATE
;
1514 This
->initialized
= TRUE
;
1516 LeaveCriticalSection(&This
->parent
->lock
);
1521 static HRESULT WINAPI
TiffFrameEncode_SetSize(IWICBitmapFrameEncode
*iface
,
1522 UINT uiWidth
, UINT uiHeight
)
1524 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1525 TRACE("(%p,%u,%u)\n", iface
, uiWidth
, uiHeight
);
1527 EnterCriticalSection(&This
->parent
->lock
);
1529 if (!This
->initialized
|| This
->info_written
)
1531 LeaveCriticalSection(&This
->parent
->lock
);
1532 return WINCODEC_ERR_WRONGSTATE
;
1535 This
->width
= uiWidth
;
1536 This
->height
= uiHeight
;
1538 LeaveCriticalSection(&This
->parent
->lock
);
1543 static HRESULT WINAPI
TiffFrameEncode_SetResolution(IWICBitmapFrameEncode
*iface
,
1544 double dpiX
, double dpiY
)
1546 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1547 TRACE("(%p,%0.2f,%0.2f)\n", iface
, dpiX
, dpiY
);
1549 EnterCriticalSection(&This
->parent
->lock
);
1551 if (!This
->initialized
|| This
->info_written
)
1553 LeaveCriticalSection(&This
->parent
->lock
);
1554 return WINCODEC_ERR_WRONGSTATE
;
1560 LeaveCriticalSection(&This
->parent
->lock
);
1565 static HRESULT WINAPI
TiffFrameEncode_SetPixelFormat(IWICBitmapFrameEncode
*iface
,
1566 WICPixelFormatGUID
*pPixelFormat
)
1568 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1571 TRACE("(%p,%s)\n", iface
, debugstr_guid(pPixelFormat
));
1573 EnterCriticalSection(&This
->parent
->lock
);
1575 if (!This
->initialized
|| This
->info_written
)
1577 LeaveCriticalSection(&This
->parent
->lock
);
1578 return WINCODEC_ERR_WRONGSTATE
;
1581 for (i
=0; formats
[i
].guid
; i
++)
1583 if (memcmp(formats
[i
].guid
, pPixelFormat
, sizeof(GUID
)) == 0)
1587 if (!formats
[i
].guid
) i
= 0;
1589 This
->format
= &formats
[i
];
1590 memcpy(pPixelFormat
, This
->format
->guid
, sizeof(GUID
));
1592 LeaveCriticalSection(&This
->parent
->lock
);
1597 static HRESULT WINAPI
TiffFrameEncode_SetColorContexts(IWICBitmapFrameEncode
*iface
,
1598 UINT cCount
, IWICColorContext
**ppIColorContext
)
1600 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1604 static HRESULT WINAPI
TiffFrameEncode_SetPalette(IWICBitmapFrameEncode
*iface
,
1605 IWICPalette
*pIPalette
)
1607 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
1608 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1611 static HRESULT WINAPI
TiffFrameEncode_SetThumbnail(IWICBitmapFrameEncode
*iface
,
1612 IWICBitmapSource
*pIThumbnail
)
1614 FIXME("(%p,%p): stub\n", iface
, pIThumbnail
);
1615 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1618 static HRESULT WINAPI
TiffFrameEncode_WritePixels(IWICBitmapFrameEncode
*iface
,
1619 UINT lineCount
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbPixels
)
1621 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1622 BYTE
*row_data
, *swapped_data
= NULL
;
1623 UINT i
, j
, line_size
;
1625 TRACE("(%p,%u,%u,%u,%p)\n", iface
, lineCount
, cbStride
, cbBufferSize
, pbPixels
);
1627 EnterCriticalSection(&This
->parent
->lock
);
1629 if (!This
->initialized
|| !This
->width
|| !This
->height
|| !This
->format
)
1631 LeaveCriticalSection(&This
->parent
->lock
);
1632 return WINCODEC_ERR_WRONGSTATE
;
1635 if (lineCount
== 0 || lineCount
+ This
->lines_written
> This
->height
)
1637 LeaveCriticalSection(&This
->parent
->lock
);
1638 return E_INVALIDARG
;
1641 line_size
= ((This
->width
* This
->format
->bpp
)+7)/8;
1643 if (This
->format
->reverse_bgr
)
1645 swapped_data
= HeapAlloc(GetProcessHeap(), 0, line_size
);
1648 LeaveCriticalSection(&This
->parent
->lock
);
1649 return E_OUTOFMEMORY
;
1653 if (!This
->info_written
)
1655 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_PHOTOMETRIC
, (uint16
)This
->format
->photometric
);
1656 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_PLANARCONFIG
, (uint16
)1);
1657 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_BITSPERSAMPLE
, (uint16
)This
->format
->bps
);
1658 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_SAMPLESPERPIXEL
, (uint16
)This
->format
->samples
);
1660 if (This
->format
->extra_sample
)
1662 uint16 extra_samples
;
1663 extra_samples
= This
->format
->extra_sample_type
;
1665 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_EXTRASAMPLES
, (uint16
)1, &extra_samples
);
1668 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_IMAGEWIDTH
, (uint32
)This
->width
);
1669 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_IMAGELENGTH
, (uint32
)This
->height
);
1671 if (This
->xres
!= 0.0 && This
->yres
!= 0.0)
1673 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_RESOLUTIONUNIT
, (uint16
)2); /* Inch */
1674 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_XRESOLUTION
, (float)This
->xres
);
1675 pTIFFSetField(This
->parent
->tiff
, TIFFTAG_YRESOLUTION
, (float)This
->yres
);
1678 This
->info_written
= TRUE
;
1681 for (i
=0; i
<lineCount
; i
++)
1683 row_data
= pbPixels
+ i
* cbStride
;
1685 if (This
->format
->reverse_bgr
&& This
->format
->bps
== 8)
1687 memcpy(swapped_data
, row_data
, line_size
);
1688 for (j
=0; j
<line_size
; j
+= This
->format
->samples
)
1691 temp
= swapped_data
[j
];
1692 swapped_data
[j
] = swapped_data
[j
+2];
1693 swapped_data
[j
+2] = temp
;
1695 row_data
= swapped_data
;
1698 pTIFFWriteScanline(This
->parent
->tiff
, (tdata_t
)row_data
, i
+This
->lines_written
, 0);
1701 This
->lines_written
+= lineCount
;
1703 LeaveCriticalSection(&This
->parent
->lock
);
1705 HeapFree(GetProcessHeap(), 0, swapped_data
);
1710 static HRESULT WINAPI
TiffFrameEncode_WriteSource(IWICBitmapFrameEncode
*iface
,
1711 IWICBitmapSource
*pIBitmapSource
, WICRect
*prc
)
1713 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1716 TRACE("(%p,%p,%p)\n", iface
, pIBitmapSource
, prc
);
1718 if (!This
->initialized
)
1719 return WINCODEC_ERR_WRONGSTATE
;
1721 hr
= configure_write_source(iface
, pIBitmapSource
, prc
,
1722 This
->format
? This
->format
->guid
: NULL
, This
->width
, This
->height
,
1723 This
->xres
, This
->yres
);
1727 hr
= write_source(iface
, pIBitmapSource
, prc
,
1728 This
->format
->guid
, This
->format
->bpp
, This
->width
, This
->height
);
1734 static HRESULT WINAPI
TiffFrameEncode_Commit(IWICBitmapFrameEncode
*iface
)
1736 TiffFrameEncode
*This
= impl_from_IWICBitmapFrameEncode(iface
);
1738 TRACE("(%p)\n", iface
);
1740 EnterCriticalSection(&This
->parent
->lock
);
1742 if (!This
->info_written
|| This
->lines_written
!= This
->height
|| This
->committed
)
1744 LeaveCriticalSection(&This
->parent
->lock
);
1745 return WINCODEC_ERR_WRONGSTATE
;
1748 /* libtiff will commit the data when creating a new frame or closing the file */
1750 This
->committed
= TRUE
;
1751 This
->parent
->num_frames_committed
++;
1753 LeaveCriticalSection(&This
->parent
->lock
);
1758 static HRESULT WINAPI
TiffFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode
*iface
,
1759 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
1761 FIXME("(%p, %p): stub\n", iface
, ppIMetadataQueryWriter
);
1765 static const IWICBitmapFrameEncodeVtbl TiffFrameEncode_Vtbl
= {
1766 TiffFrameEncode_QueryInterface
,
1767 TiffFrameEncode_AddRef
,
1768 TiffFrameEncode_Release
,
1769 TiffFrameEncode_Initialize
,
1770 TiffFrameEncode_SetSize
,
1771 TiffFrameEncode_SetResolution
,
1772 TiffFrameEncode_SetPixelFormat
,
1773 TiffFrameEncode_SetColorContexts
,
1774 TiffFrameEncode_SetPalette
,
1775 TiffFrameEncode_SetThumbnail
,
1776 TiffFrameEncode_WritePixels
,
1777 TiffFrameEncode_WriteSource
,
1778 TiffFrameEncode_Commit
,
1779 TiffFrameEncode_GetMetadataQueryWriter
1782 static HRESULT WINAPI
TiffEncoder_QueryInterface(IWICBitmapEncoder
*iface
, REFIID iid
,
1785 TiffEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1786 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
1788 if (!ppv
) return E_INVALIDARG
;
1790 if (IsEqualIID(&IID_IUnknown
, iid
) ||
1791 IsEqualIID(&IID_IWICBitmapEncoder
, iid
))
1793 *ppv
= &This
->IWICBitmapEncoder_iface
;
1798 return E_NOINTERFACE
;
1801 IUnknown_AddRef((IUnknown
*)*ppv
);
1805 static ULONG WINAPI
TiffEncoder_AddRef(IWICBitmapEncoder
*iface
)
1807 TiffEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1808 ULONG ref
= InterlockedIncrement(&This
->ref
);
1810 TRACE("(%p) refcount=%u\n", iface
, ref
);
1815 static ULONG WINAPI
TiffEncoder_Release(IWICBitmapEncoder
*iface
)
1817 TiffEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1818 ULONG ref
= InterlockedDecrement(&This
->ref
);
1820 TRACE("(%p) refcount=%u\n", iface
, ref
);
1824 if (This
->tiff
) pTIFFClose(This
->tiff
);
1825 if (This
->stream
) IStream_Release(This
->stream
);
1826 This
->lock
.DebugInfo
->Spare
[0] = 0;
1827 DeleteCriticalSection(&This
->lock
);
1828 HeapFree(GetProcessHeap(), 0, This
);
1834 static HRESULT WINAPI
TiffEncoder_Initialize(IWICBitmapEncoder
*iface
,
1835 IStream
*pIStream
, WICBitmapEncoderCacheOption cacheOption
)
1837 TiffEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1841 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOption
);
1843 EnterCriticalSection(&This
->lock
);
1845 if (This
->initialized
|| This
->committed
)
1847 hr
= WINCODEC_ERR_WRONGSTATE
;
1851 tiff
= tiff_open_stream(pIStream
, "w");
1860 This
->stream
= pIStream
;
1861 IStream_AddRef(pIStream
);
1862 This
->initialized
= TRUE
;
1865 LeaveCriticalSection(&This
->lock
);
1869 static HRESULT WINAPI
TiffEncoder_GetContainerFormat(IWICBitmapEncoder
*iface
,
1870 GUID
*pguidContainerFormat
)
1872 memcpy(pguidContainerFormat
, &GUID_ContainerFormatTiff
, sizeof(GUID
));
1876 static HRESULT WINAPI
TiffEncoder_GetEncoderInfo(IWICBitmapEncoder
*iface
,
1877 IWICBitmapEncoderInfo
**ppIEncoderInfo
)
1879 FIXME("(%p,%p): stub\n", iface
, ppIEncoderInfo
);
1883 static HRESULT WINAPI
TiffEncoder_SetColorContexts(IWICBitmapEncoder
*iface
,
1884 UINT cCount
, IWICColorContext
**ppIColorContext
)
1886 FIXME("(%p,%u,%p): stub\n", iface
, cCount
, ppIColorContext
);
1890 static HRESULT WINAPI
TiffEncoder_SetPalette(IWICBitmapEncoder
*iface
, IWICPalette
*pIPalette
)
1892 TRACE("(%p,%p)\n", iface
, pIPalette
);
1893 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1896 static HRESULT WINAPI
TiffEncoder_SetThumbnail(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIThumbnail
)
1898 TRACE("(%p,%p)\n", iface
, pIThumbnail
);
1899 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1902 static HRESULT WINAPI
TiffEncoder_SetPreview(IWICBitmapEncoder
*iface
, IWICBitmapSource
*pIPreview
)
1904 TRACE("(%p,%p)\n", iface
, pIPreview
);
1905 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
1908 static HRESULT WINAPI
TiffEncoder_CreateNewFrame(IWICBitmapEncoder
*iface
,
1909 IWICBitmapFrameEncode
**ppIFrameEncode
, IPropertyBag2
**ppIEncoderOptions
)
1911 TiffEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
1912 TiffFrameEncode
*result
;
1916 TRACE("(%p,%p,%p)\n", iface
, ppIFrameEncode
, ppIEncoderOptions
);
1918 EnterCriticalSection(&This
->lock
);
1920 if (!This
->initialized
|| This
->committed
)
1922 hr
= WINCODEC_ERR_WRONGSTATE
;
1924 else if (This
->num_frames
!= This
->num_frames_committed
)
1926 FIXME("New frame created before previous frame was committed\n");
1932 PROPBAG2 opts
[2]= {{0}};
1933 opts
[0].pstrName
= (LPOLESTR
)wszTiffCompressionMethod
;
1934 opts
[0].vt
= VT_UI1
;
1935 opts
[0].dwType
= PROPBAG2_TYPE_DATA
;
1937 opts
[1].pstrName
= (LPOLESTR
)wszCompressionQuality
;
1939 opts
[1].dwType
= PROPBAG2_TYPE_DATA
;
1941 hr
= CreatePropertyBag2(opts
, 2, ppIEncoderOptions
);
1948 V_UNION(&v
, bVal
) = WICTiffCompressionDontCare
;
1949 hr
= IPropertyBag2_Write(*ppIEncoderOptions
, 1, opts
, &v
);
1953 IPropertyBag2_Release(*ppIEncoderOptions
);
1954 *ppIEncoderOptions
= NULL
;
1961 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(*result
));
1965 result
->IWICBitmapFrameEncode_iface
.lpVtbl
= &TiffFrameEncode_Vtbl
;
1967 result
->parent
= This
;
1968 result
->initialized
= FALSE
;
1969 result
->info_written
= FALSE
;
1970 result
->committed
= FALSE
;
1971 result
->format
= NULL
;
1976 result
->lines_written
= 0;
1978 IWICBitmapEncoder_AddRef(iface
);
1979 *ppIFrameEncode
= &result
->IWICBitmapFrameEncode_iface
;
1981 if (This
->num_frames
!= 0)
1982 pTIFFWriteDirectory(This
->tiff
);
1991 IPropertyBag2_Release(*ppIEncoderOptions
);
1992 *ppIEncoderOptions
= NULL
;
1996 LeaveCriticalSection(&This
->lock
);
2001 static HRESULT WINAPI
TiffEncoder_Commit(IWICBitmapEncoder
*iface
)
2003 TiffEncoder
*This
= impl_from_IWICBitmapEncoder(iface
);
2005 TRACE("(%p)\n", iface
);
2007 EnterCriticalSection(&This
->lock
);
2009 if (!This
->initialized
|| This
->committed
)
2011 LeaveCriticalSection(&This
->lock
);
2012 return WINCODEC_ERR_WRONGSTATE
;
2015 pTIFFClose(This
->tiff
);
2016 IStream_Release(This
->stream
);
2017 This
->stream
= NULL
;
2020 This
->committed
= TRUE
;
2022 LeaveCriticalSection(&This
->lock
);
2027 static HRESULT WINAPI
TiffEncoder_GetMetadataQueryWriter(IWICBitmapEncoder
*iface
,
2028 IWICMetadataQueryWriter
**ppIMetadataQueryWriter
)
2030 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryWriter
);
2034 static const IWICBitmapEncoderVtbl TiffEncoder_Vtbl
= {
2035 TiffEncoder_QueryInterface
,
2037 TiffEncoder_Release
,
2038 TiffEncoder_Initialize
,
2039 TiffEncoder_GetContainerFormat
,
2040 TiffEncoder_GetEncoderInfo
,
2041 TiffEncoder_SetColorContexts
,
2042 TiffEncoder_SetPalette
,
2043 TiffEncoder_SetThumbnail
,
2044 TiffEncoder_SetPreview
,
2045 TiffEncoder_CreateNewFrame
,
2047 TiffEncoder_GetMetadataQueryWriter
2050 HRESULT
TiffEncoder_CreateInstance(REFIID iid
, void** ppv
)
2055 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
2059 if (!load_libtiff())
2061 ERR("Failed writing TIFF because unable to load %s\n",SONAME_LIBTIFF
);
2065 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(TiffEncoder
));
2066 if (!This
) return E_OUTOFMEMORY
;
2068 This
->IWICBitmapEncoder_iface
.lpVtbl
= &TiffEncoder_Vtbl
;
2070 This
->stream
= NULL
;
2071 InitializeCriticalSection(&This
->lock
);
2072 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TiffEncoder.lock");
2074 This
->initialized
= FALSE
;
2075 This
->num_frames
= 0;
2076 This
->num_frames_committed
= 0;
2077 This
->committed
= FALSE
;
2079 ret
= IWICBitmapEncoder_QueryInterface(&This
->IWICBitmapEncoder_iface
, iid
, ppv
);
2080 IWICBitmapEncoder_Release(&This
->IWICBitmapEncoder_iface
);
2085 #else /* !SONAME_LIBTIFF */
2087 HRESULT
TiffDecoder_CreateInstance(REFIID iid
, void** ppv
)
2089 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
2093 HRESULT
TiffEncoder_CreateInstance(REFIID iid
, void** ppv
)
2095 ERR("Trying to save TIFF picture, but Wine was compiled without TIFF support.\n");