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"
37 #include "wincodecs_private.h"
39 #include "wine/debug.h"
40 #include "wine/library.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
46 static CRITICAL_SECTION init_tiff_cs;
47 static CRITICAL_SECTION_DEBUG init_tiff_cs_debug =
50 { &init_tiff_cs_debug.ProcessLocksList,
51 &init_tiff_cs_debug.ProcessLocksList },
52 0, 0, { (DWORD_PTR)(__FILE__ ": init_tiff_cs") }
54 static CRITICAL_SECTION init_tiff_cs = { &init_tiff_cs_debug, -1, 0, 0, 0, 0 };
56 static void *libtiff_handle;
57 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
58 MAKE_FUNCPTR(TIFFClientOpen);
59 MAKE_FUNCPTR(TIFFClose);
60 MAKE_FUNCPTR(TIFFCurrentDirectory);
61 MAKE_FUNCPTR(TIFFGetField);
62 MAKE_FUNCPTR(TIFFReadDirectory);
63 MAKE_FUNCPTR(TIFFReadEncodedStrip);
64 MAKE_FUNCPTR(TIFFSetDirectory);
67 static void *load_libtiff(void)
71 EnterCriticalSection(&init_tiff_cs);
73 if (!libtiff_handle &&
74 (libtiff_handle = wine_dlopen(SONAME_LIBTIFF, RTLD_NOW, NULL, 0)) != NULL)
77 #define LOAD_FUNCPTR(f) \
78 if((p##f = wine_dlsym(libtiff_handle, #f, NULL, 0)) == NULL) { \
79 ERR("failed to load symbol %s\n", #f); \
80 libtiff_handle = NULL; \
81 LeaveCriticalSection(&init_tiff_cs); \
84 LOAD_FUNCPTR(TIFFClientOpen);
85 LOAD_FUNCPTR(TIFFClose);
86 LOAD_FUNCPTR(TIFFCurrentDirectory);
87 LOAD_FUNCPTR(TIFFGetField);
88 LOAD_FUNCPTR(TIFFReadDirectory);
89 LOAD_FUNCPTR(TIFFReadEncodedStrip);
90 LOAD_FUNCPTR(TIFFSetDirectory);
95 result = libtiff_handle;
97 LeaveCriticalSection(&init_tiff_cs);
101 static tsize_t tiff_stream_read(thandle_t client_data, tdata_t data, tsize_t size)
103 IStream *stream = (IStream*)client_data;
107 hr = IStream_Read(stream, data, size, &bytes_read);
108 if (FAILED(hr)) bytes_read = 0;
112 static tsize_t tiff_stream_write(thandle_t client_data, tdata_t data, tsize_t size)
114 IStream *stream = (IStream*)client_data;
118 hr = IStream_Write(stream, data, size, &bytes_written);
119 if (FAILED(hr)) bytes_written = 0;
120 return bytes_written;
123 static toff_t tiff_stream_seek(thandle_t client_data, toff_t offset, int whence)
125 IStream *stream = (IStream*)client_data;
128 ULARGE_INTEGER new_position;
131 move.QuadPart = offset;
135 origin = STREAM_SEEK_SET;
138 origin = STREAM_SEEK_CUR;
141 origin = STREAM_SEEK_END;
144 ERR("unknown whence value %i\n", whence);
148 hr = IStream_Seek(stream, move, origin, &new_position);
149 if (SUCCEEDED(hr)) return new_position.QuadPart;
153 static int tiff_stream_close(thandle_t client_data)
155 /* Caller is responsible for releasing the stream object. */
159 static toff_t tiff_stream_size(thandle_t client_data)
161 IStream *stream = (IStream*)client_data;
165 hr = IStream_Stat(stream, &statstg, STATFLAG_NONAME);
167 if (SUCCEEDED(hr)) return statstg.cbSize.QuadPart;
171 static int tiff_stream_map(thandle_t client_data, tdata_t *addr, toff_t *size)
173 /* Cannot mmap streams */
177 static void tiff_stream_unmap(thandle_t client_data, tdata_t addr, toff_t size)
179 /* No need to ever do this, since we can't map things. */
182 static TIFF* tiff_open_stream(IStream *stream, const char *mode)
184 return pTIFFClientOpen("<IStream object>", mode, stream, tiff_stream_read,
185 tiff_stream_write, tiff_stream_seek, tiff_stream_close,
186 tiff_stream_size, tiff_stream_map, tiff_stream_unmap);
190 const IWICBitmapDecoderVtbl *lpVtbl;
193 CRITICAL_SECTION lock; /* Must be held when tiff is used or initiailzed is set */
199 const WICPixelFormatGUID *format;
204 UINT tile_width, tile_height;
210 const IWICBitmapFrameDecodeVtbl *lpVtbl;
214 tiff_decode_info decode_info;
215 INT cached_tile_x, cached_tile_y;
219 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl;
221 static HRESULT tiff_get_decode_info(TIFF *tiff, tiff_decode_info *decode_info)
223 uint16 photometric, bps, samples, planar;
226 decode_info->indexed = 0;
227 decode_info->reverse_bgr = 0;
229 ret = pTIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
232 WARN("missing PhotometricInterpretation tag\n");
236 ret = pTIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bps);
241 case 1: /* BlackIsZero */
242 decode_info->bpp = bps;
246 decode_info->format = &GUID_WICPixelFormatBlackWhite;
249 decode_info->format = &GUID_WICPixelFormat4bppGray;
252 decode_info->format = &GUID_WICPixelFormat8bppGray;
255 FIXME("unhandled greyscale bit count %u\n", bps);
262 FIXME("unhandled RGB bit count %u\n", bps);
265 ret = pTIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samples);
268 FIXME("unhandled RGB sample count %u\n", samples);
271 ret = pTIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planar);
272 if (!ret) planar = 1;
275 FIXME("unhandled planar configuration %u\n", planar);
278 decode_info->bpp = bps * samples;
279 decode_info->reverse_bgr = 1;
280 decode_info->format = &GUID_WICPixelFormat24bppBGR;
282 case 3: /* RGB Palette */
283 decode_info->indexed = 1;
284 decode_info->bpp = bps;
288 decode_info->format = &GUID_WICPixelFormat4bppIndexed;
291 decode_info->format = &GUID_WICPixelFormat8bppIndexed;
294 FIXME("unhandled indexed bit count %u\n", bps);
298 case 0: /* WhiteIsZero */
299 case 4: /* Transparency mask */
304 FIXME("unhandled PhotometricInterpretation %u\n", photometric);
308 ret = pTIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &decode_info->width);
311 WARN("missing image width\n");
315 ret = pTIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &decode_info->height);
318 WARN("missing image length\n");
322 ret = pTIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &decode_info->tile_height);
325 decode_info->tile_width = decode_info->width;
326 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
327 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
331 /* Probably a tiled image */
332 FIXME("missing RowsPerStrip value\n");
339 static HRESULT WINAPI TiffDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
342 TiffDecoder *This = (TiffDecoder*)iface;
343 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
345 if (!ppv) return E_INVALIDARG;
347 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
354 return E_NOINTERFACE;
357 IUnknown_AddRef((IUnknown*)*ppv);
361 static ULONG WINAPI TiffDecoder_AddRef(IWICBitmapDecoder *iface)
363 TiffDecoder *This = (TiffDecoder*)iface;
364 ULONG ref = InterlockedIncrement(&This->ref);
366 TRACE("(%p) refcount=%u\n", iface, ref);
371 static ULONG WINAPI TiffDecoder_Release(IWICBitmapDecoder *iface)
373 TiffDecoder *This = (TiffDecoder*)iface;
374 ULONG ref = InterlockedDecrement(&This->ref);
376 TRACE("(%p) refcount=%u\n", iface, ref);
380 if (This->tiff) pTIFFClose(This->tiff);
381 if (This->stream) IStream_Release(This->stream);
382 This->lock.DebugInfo->Spare[0] = 0;
383 DeleteCriticalSection(&This->lock);
384 HeapFree(GetProcessHeap(), 0, This);
390 static HRESULT WINAPI TiffDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
391 DWORD *pdwCapability)
393 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
397 static HRESULT WINAPI TiffDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
398 WICDecodeOptions cacheOptions)
400 TiffDecoder *This = (TiffDecoder*)iface;
404 TRACE("(%p,%p,%x): stub\n", iface, pIStream, cacheOptions);
406 EnterCriticalSection(&This->lock);
408 if (This->initialized)
410 hr = WINCODEC_ERR_WRONGSTATE;
414 tiff = tiff_open_stream(pIStream, "r");
423 This->stream = pIStream;
424 IStream_AddRef(pIStream);
425 This->initialized = TRUE;
428 LeaveCriticalSection(&This->lock);
432 static HRESULT WINAPI TiffDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
433 GUID *pguidContainerFormat)
435 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
439 static HRESULT WINAPI TiffDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
440 IWICBitmapDecoderInfo **ppIDecoderInfo)
442 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
446 static HRESULT WINAPI TiffDecoder_CopyPalette(IWICBitmapDecoder *iface,
447 IWICPalette *pIPalette)
449 FIXME("(%p,%p): stub\n", iface, pIPalette);
453 static HRESULT WINAPI TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
454 IWICMetadataQueryReader **ppIMetadataQueryReader)
456 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
460 static HRESULT WINAPI TiffDecoder_GetPreview(IWICBitmapDecoder *iface,
461 IWICBitmapSource **ppIBitmapSource)
463 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
467 static HRESULT WINAPI TiffDecoder_GetColorContexts(IWICBitmapDecoder *iface,
468 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
470 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
474 static HRESULT WINAPI TiffDecoder_GetThumbnail(IWICBitmapDecoder *iface,
475 IWICBitmapSource **ppIThumbnail)
477 TRACE("(%p,%p)\n", iface, ppIThumbnail);
478 return WINCODEC_ERR_CODECNOTHUMBNAIL;
481 static HRESULT WINAPI TiffDecoder_GetFrameCount(IWICBitmapDecoder *iface,
484 TiffDecoder *This = (TiffDecoder*)iface;
488 WARN("(%p) <-- WINCODEC_ERR_WRONGSTATE\n", iface);
489 return WINCODEC_ERR_WRONGSTATE;
492 EnterCriticalSection(&This->lock);
493 while (pTIFFReadDirectory(This->tiff)) { }
494 *pCount = pTIFFCurrentDirectory(This->tiff)+1;
495 LeaveCriticalSection(&This->lock);
497 TRACE("(%p) <-- %i\n", iface, *pCount);
502 static HRESULT WINAPI TiffDecoder_GetFrame(IWICBitmapDecoder *iface,
503 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
505 TiffDecoder *This = (TiffDecoder*)iface;
506 TiffFrameDecode *result;
508 tiff_decode_info decode_info;
511 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
514 return WINCODEC_ERR_WRONGSTATE;
516 EnterCriticalSection(&This->lock);
517 res = pTIFFSetDirectory(This->tiff, index);
518 if (!res) hr = E_INVALIDARG;
519 else hr = tiff_get_decode_info(This->tiff, &decode_info);
520 LeaveCriticalSection(&This->lock);
524 result = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode));
528 result->lpVtbl = &TiffFrameDecode_Vtbl;
530 result->parent = This;
531 result->index = index;
532 result->decode_info = decode_info;
533 result->cached_tile_x = -1;
534 result->cached_tile = HeapAlloc(GetProcessHeap(), 0, decode_info.tile_size);
536 if (result->cached_tile)
537 *ppIBitmapFrame = (IWICBitmapFrameDecode*)result;
541 HeapFree(GetProcessHeap(), 0, result);
544 else hr = E_OUTOFMEMORY;
547 if (FAILED(hr)) *ppIBitmapFrame = NULL;
552 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl = {
553 TiffDecoder_QueryInterface,
556 TiffDecoder_QueryCapability,
557 TiffDecoder_Initialize,
558 TiffDecoder_GetContainerFormat,
559 TiffDecoder_GetDecoderInfo,
560 TiffDecoder_CopyPalette,
561 TiffDecoder_GetMetadataQueryReader,
562 TiffDecoder_GetPreview,
563 TiffDecoder_GetColorContexts,
564 TiffDecoder_GetThumbnail,
565 TiffDecoder_GetFrameCount,
569 static HRESULT WINAPI TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
572 TiffFrameDecode *This = (TiffFrameDecode*)iface;
573 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
575 if (!ppv) return E_INVALIDARG;
577 if (IsEqualIID(&IID_IUnknown, iid) ||
578 IsEqualIID(&IID_IWICBitmapSource, iid) ||
579 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
586 return E_NOINTERFACE;
589 IUnknown_AddRef((IUnknown*)*ppv);
593 static ULONG WINAPI TiffFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
595 TiffFrameDecode *This = (TiffFrameDecode*)iface;
596 ULONG ref = InterlockedIncrement(&This->ref);
598 TRACE("(%p) refcount=%u\n", iface, ref);
603 static ULONG WINAPI TiffFrameDecode_Release(IWICBitmapFrameDecode *iface)
605 TiffFrameDecode *This = (TiffFrameDecode*)iface;
606 ULONG ref = InterlockedDecrement(&This->ref);
608 TRACE("(%p) refcount=%u\n", iface, ref);
612 HeapFree(GetProcessHeap(), 0, This->cached_tile);
613 HeapFree(GetProcessHeap(), 0, This);
619 static HRESULT WINAPI TiffFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
620 UINT *puiWidth, UINT *puiHeight)
622 TiffFrameDecode *This = (TiffFrameDecode*)iface;
624 *puiWidth = This->decode_info.width;
625 *puiHeight = This->decode_info.height;
627 TRACE("(%p) <-- %ux%u\n", iface, *puiWidth, *puiHeight);
632 static HRESULT WINAPI TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
633 WICPixelFormatGUID *pPixelFormat)
635 TiffFrameDecode *This = (TiffFrameDecode*)iface;
637 memcpy(pPixelFormat, This->decode_info.format, sizeof(GUID));
639 TRACE("(%p) <-- %s\n", This, debugstr_guid(This->decode_info.format));
644 static HRESULT WINAPI TiffFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
645 double *pDpiX, double *pDpiY)
647 FIXME("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
651 static HRESULT WINAPI TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
652 IWICPalette *pIPalette)
654 FIXME("(%p,%p)\n", iface, pIPalette);
658 static HRESULT TiffFrameDecode_ReadTile(TiffFrameDecode *This, UINT tile_x, UINT tile_y)
663 ret = pTIFFSetDirectory(This->parent->tiff, This->index);
670 ret = pTIFFReadEncodedStrip(This->parent->tiff, tile_y, This->cached_tile, This->decode_info.tile_size);
676 if (hr == S_OK && This->decode_info.reverse_bgr)
678 if (This->decode_info.format == &GUID_WICPixelFormat24bppBGR)
680 UINT i, total_pixels;
683 total_pixels = This->decode_info.tile_width * This->decode_info.tile_height;
684 pixel = This->cached_tile;
685 for (i=0; i<total_pixels; i++)
697 This->cached_tile_x = tile_x;
698 This->cached_tile_y = tile_y;
704 static HRESULT WINAPI TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
705 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
707 TiffFrameDecode *This = (TiffFrameDecode*)iface;
708 UINT min_tile_x, max_tile_x, min_tile_y, max_tile_y;
715 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
717 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->decode_info.width ||
718 prc->Y+prc->Height > This->decode_info.height)
721 bytesperrow = ((This->decode_info.bpp * prc->Width)+7)/8;
723 if (cbStride < bytesperrow)
726 if ((cbStride * prc->Height) > cbBufferSize)
729 min_tile_x = prc->X / This->decode_info.tile_width;
730 min_tile_y = prc->Y / This->decode_info.tile_height;
731 max_tile_x = (prc->X+prc->Width-1) / This->decode_info.tile_width;
732 max_tile_y = (prc->Y+prc->Height-1) / This->decode_info.tile_height;
734 EnterCriticalSection(&This->parent->lock);
736 for (tile_x=min_tile_x; tile_x <= max_tile_x; tile_x++)
738 for (tile_y=min_tile_y; tile_y <= max_tile_y; tile_y++)
740 if (tile_x != This->cached_tile_x || tile_y != This->cached_tile_y)
742 hr = TiffFrameDecode_ReadTile(This, tile_x, tile_y);
747 if (prc->X < tile_x * This->decode_info.tile_width)
750 rc.X = prc->X - tile_x * This->decode_info.tile_width;
752 if (prc->Y < tile_y * This->decode_info.tile_height)
755 rc.Y = prc->Y - tile_y * This->decode_info.tile_height;
757 if (prc->X+prc->Width > (tile_x+1) * This->decode_info.tile_width)
758 rc.Width = This->decode_info.tile_width - rc.X;
759 else if (prc->X < tile_x * This->decode_info.tile_width)
760 rc.Width = prc->Width + prc->X - tile_x * This->decode_info.tile_width;
762 rc.Width = prc->Width;
764 if (prc->Y+prc->Height > (tile_y+1) * This->decode_info.tile_height)
765 rc.Height = This->decode_info.tile_height - rc.Y;
766 else if (prc->Y < tile_y * This->decode_info.tile_height)
767 rc.Height = prc->Height + prc->Y - tile_y * This->decode_info.tile_height;
769 rc.Height = prc->Height;
771 dst_tilepos = pbBuffer + (cbStride * ((rc.Y + tile_y * This->decode_info.tile_height) - prc->Y)) +
772 ((This->decode_info.bpp * ((rc.X + tile_x * This->decode_info.tile_width) - prc->X) + 7) / 8);
774 hr = copy_pixels(This->decode_info.bpp, This->cached_tile,
775 This->decode_info.tile_width, This->decode_info.tile_height, This->decode_info.tile_stride,
776 &rc, cbStride, cbBufferSize, dst_tilepos);
781 LeaveCriticalSection(&This->parent->lock);
782 TRACE("<-- 0x%x\n", hr);
788 LeaveCriticalSection(&This->parent->lock);
793 static HRESULT WINAPI TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
794 IWICMetadataQueryReader **ppIMetadataQueryReader)
796 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
800 static HRESULT WINAPI TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
801 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
803 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
807 static HRESULT WINAPI TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
808 IWICBitmapSource **ppIThumbnail)
810 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
814 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl = {
815 TiffFrameDecode_QueryInterface,
816 TiffFrameDecode_AddRef,
817 TiffFrameDecode_Release,
818 TiffFrameDecode_GetSize,
819 TiffFrameDecode_GetPixelFormat,
820 TiffFrameDecode_GetResolution,
821 TiffFrameDecode_CopyPalette,
822 TiffFrameDecode_CopyPixels,
823 TiffFrameDecode_GetMetadataQueryReader,
824 TiffFrameDecode_GetColorContexts,
825 TiffFrameDecode_GetThumbnail
828 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
833 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
837 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
841 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF);
845 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder));
846 if (!This) return E_OUTOFMEMORY;
848 This->lpVtbl = &TiffDecoder_Vtbl;
851 InitializeCriticalSection(&This->lock);
852 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffDecoder.lock");
854 This->initialized = FALSE;
856 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
857 IUnknown_Release((IUnknown*)This);
862 #else /* !SONAME_LIBTIFF */
864 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
866 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");