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(TIFFIsByteSwapped);
63 MAKE_FUNCPTR(TIFFReadDirectory);
64 MAKE_FUNCPTR(TIFFReadEncodedStrip);
65 MAKE_FUNCPTR(TIFFReadEncodedTile);
66 MAKE_FUNCPTR(TIFFSetDirectory);
69 static void *load_libtiff(void)
73 EnterCriticalSection(&init_tiff_cs);
75 if (!libtiff_handle &&
76 (libtiff_handle = wine_dlopen(SONAME_LIBTIFF, RTLD_NOW, NULL, 0)) != NULL)
79 #define LOAD_FUNCPTR(f) \
80 if((p##f = wine_dlsym(libtiff_handle, #f, NULL, 0)) == NULL) { \
81 ERR("failed to load symbol %s\n", #f); \
82 libtiff_handle = NULL; \
83 LeaveCriticalSection(&init_tiff_cs); \
86 LOAD_FUNCPTR(TIFFClientOpen);
87 LOAD_FUNCPTR(TIFFClose);
88 LOAD_FUNCPTR(TIFFCurrentDirectory);
89 LOAD_FUNCPTR(TIFFGetField);
90 LOAD_FUNCPTR(TIFFIsByteSwapped);
91 LOAD_FUNCPTR(TIFFReadDirectory);
92 LOAD_FUNCPTR(TIFFReadEncodedStrip);
93 LOAD_FUNCPTR(TIFFReadEncodedTile);
94 LOAD_FUNCPTR(TIFFSetDirectory);
99 result = libtiff_handle;
101 LeaveCriticalSection(&init_tiff_cs);
105 static tsize_t tiff_stream_read(thandle_t client_data, tdata_t data, tsize_t size)
107 IStream *stream = (IStream*)client_data;
111 hr = IStream_Read(stream, data, size, &bytes_read);
112 if (FAILED(hr)) bytes_read = 0;
116 static tsize_t tiff_stream_write(thandle_t client_data, tdata_t data, tsize_t size)
118 IStream *stream = (IStream*)client_data;
122 hr = IStream_Write(stream, data, size, &bytes_written);
123 if (FAILED(hr)) bytes_written = 0;
124 return bytes_written;
127 static toff_t tiff_stream_seek(thandle_t client_data, toff_t offset, int whence)
129 IStream *stream = (IStream*)client_data;
132 ULARGE_INTEGER new_position;
135 move.QuadPart = offset;
139 origin = STREAM_SEEK_SET;
142 origin = STREAM_SEEK_CUR;
145 origin = STREAM_SEEK_END;
148 ERR("unknown whence value %i\n", whence);
152 hr = IStream_Seek(stream, move, origin, &new_position);
153 if (SUCCEEDED(hr)) return new_position.QuadPart;
157 static int tiff_stream_close(thandle_t client_data)
159 /* Caller is responsible for releasing the stream object. */
163 static toff_t tiff_stream_size(thandle_t client_data)
165 IStream *stream = (IStream*)client_data;
169 hr = IStream_Stat(stream, &statstg, STATFLAG_NONAME);
171 if (SUCCEEDED(hr)) return statstg.cbSize.QuadPart;
175 static int tiff_stream_map(thandle_t client_data, tdata_t *addr, toff_t *size)
177 /* Cannot mmap streams */
181 static void tiff_stream_unmap(thandle_t client_data, tdata_t addr, toff_t size)
183 /* No need to ever do this, since we can't map things. */
186 static TIFF* tiff_open_stream(IStream *stream, const char *mode)
191 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
193 return pTIFFClientOpen("<IStream object>", mode, stream, tiff_stream_read,
194 tiff_stream_write, tiff_stream_seek, tiff_stream_close,
195 tiff_stream_size, tiff_stream_map, tiff_stream_unmap);
199 IWICBitmapDecoder IWICBitmapDecoder_iface;
202 CRITICAL_SECTION lock; /* Must be held when tiff is used or initiailzed is set */
208 const WICPixelFormatGUID *format;
215 int invert_grayscale;
217 UINT tile_width, tile_height;
225 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
229 tiff_decode_info decode_info;
230 INT cached_tile_x, cached_tile_y;
234 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl;
236 static inline TiffDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
238 return CONTAINING_RECORD(iface, TiffDecoder, IWICBitmapDecoder_iface);
241 static inline TiffFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
243 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICBitmapFrameDecode_iface);
246 static HRESULT tiff_get_decode_info(TIFF *tiff, tiff_decode_info *decode_info)
248 uint16 photometric, bps, samples, planar;
249 uint16 extra_sample_count, *extra_samples;
252 decode_info->indexed = 0;
253 decode_info->reverse_bgr = 0;
254 decode_info->invert_grayscale = 0;
255 decode_info->tiled = 0;
257 ret = pTIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
260 WARN("missing PhotometricInterpretation tag\n");
264 ret = pTIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bps);
266 decode_info->bps = bps;
268 ret = pTIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samples);
269 if (!ret) samples = 1;
270 decode_info->samples = samples;
276 ret = pTIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planar);
277 if (!ret) planar = 1;
280 FIXME("unhandled planar configuration %u\n", planar);
284 decode_info->planar = planar;
288 case 0: /* WhiteIsZero */
289 decode_info->invert_grayscale = 1;
290 case 1: /* BlackIsZero */
293 FIXME("unhandled grayscale sample count %u\n", samples);
297 decode_info->bpp = bps;
301 decode_info->format = &GUID_WICPixelFormatBlackWhite;
304 decode_info->format = &GUID_WICPixelFormat4bppGray;
307 decode_info->format = &GUID_WICPixelFormat8bppGray;
310 FIXME("unhandled greyscale bit count %u\n", bps);
315 decode_info->bpp = bps * samples;
319 ret = pTIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extra_sample_count, &extra_samples);
322 WARN("Cannot get extra sample type for RGB data, ret=%i count=%i\n", ret, extra_sample_count);
326 else if (samples != 3)
328 FIXME("unhandled RGB sample count %u\n", samples);
335 decode_info->reverse_bgr = 1;
337 decode_info->format = &GUID_WICPixelFormat24bppBGR;
339 switch(extra_samples[0])
341 case 0: /* Unspecified data */
342 decode_info->format = &GUID_WICPixelFormat32bppBGR;
344 case 1: /* Associated (pre-multiplied) alpha data */
345 decode_info->format = &GUID_WICPixelFormat32bppPBGRA;
347 case 2: /* Unassociated alpha data */
348 decode_info->format = &GUID_WICPixelFormat32bppBGRA;
351 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
357 decode_info->format = &GUID_WICPixelFormat48bppRGB;
359 switch(extra_samples[0])
361 case 0: /* Unspecified data */
362 /* decode_info->format = &GUID_WICPixelFormat64bppRGB; */
363 FIXME("64-bit RGB is unsupported\n");
365 case 1: /* Associated (pre-multiplied) alpha data */
366 decode_info->format = &GUID_WICPixelFormat64bppPRGBA;
368 case 2: /* Unassociated alpha data */
369 decode_info->format = &GUID_WICPixelFormat64bppRGBA;
372 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
377 FIXME("unhandled RGB bit count %u\n", bps);
381 case 3: /* RGB Palette */
384 FIXME("unhandled indexed sample count %u\n", samples);
388 decode_info->indexed = 1;
389 decode_info->bpp = bps;
393 decode_info->format = &GUID_WICPixelFormat4bppIndexed;
396 decode_info->format = &GUID_WICPixelFormat8bppIndexed;
399 FIXME("unhandled indexed bit count %u\n", bps);
403 case 4: /* Transparency mask */
408 FIXME("unhandled PhotometricInterpretation %u\n", photometric);
412 ret = pTIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &decode_info->width);
415 WARN("missing image width\n");
419 ret = pTIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &decode_info->height);
422 WARN("missing image length\n");
426 if ((ret = pTIFFGetField(tiff, TIFFTAG_TILEWIDTH, &decode_info->tile_width)))
428 decode_info->tiled = 1;
432 WARN("missing tile width\n");
436 ret = pTIFFGetField(tiff, TIFFTAG_TILELENGTH, &decode_info->tile_height);
439 WARN("missing tile height\n");
443 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
444 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
445 decode_info->tiles_across = (decode_info->width + decode_info->tile_width - 1) / decode_info->tile_width;
447 else if ((ret = pTIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &decode_info->tile_height)))
449 if (decode_info->tile_height > decode_info->height)
450 decode_info->tile_height = decode_info->height;
451 decode_info->tile_width = decode_info->width;
452 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
453 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
457 FIXME("missing RowsPerStrip value\n");
464 static HRESULT WINAPI TiffDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
467 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
468 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
470 if (!ppv) return E_INVALIDARG;
472 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
479 return E_NOINTERFACE;
482 IUnknown_AddRef((IUnknown*)*ppv);
486 static ULONG WINAPI TiffDecoder_AddRef(IWICBitmapDecoder *iface)
488 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
489 ULONG ref = InterlockedIncrement(&This->ref);
491 TRACE("(%p) refcount=%u\n", iface, ref);
496 static ULONG WINAPI TiffDecoder_Release(IWICBitmapDecoder *iface)
498 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
499 ULONG ref = InterlockedDecrement(&This->ref);
501 TRACE("(%p) refcount=%u\n", iface, ref);
505 if (This->tiff) pTIFFClose(This->tiff);
506 if (This->stream) IStream_Release(This->stream);
507 This->lock.DebugInfo->Spare[0] = 0;
508 DeleteCriticalSection(&This->lock);
509 HeapFree(GetProcessHeap(), 0, This);
515 static HRESULT WINAPI TiffDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
516 DWORD *pdwCapability)
518 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
522 static HRESULT WINAPI TiffDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
523 WICDecodeOptions cacheOptions)
525 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
529 TRACE("(%p,%p,%x): stub\n", iface, pIStream, cacheOptions);
531 EnterCriticalSection(&This->lock);
533 if (This->initialized)
535 hr = WINCODEC_ERR_WRONGSTATE;
539 tiff = tiff_open_stream(pIStream, "r");
548 This->stream = pIStream;
549 IStream_AddRef(pIStream);
550 This->initialized = TRUE;
553 LeaveCriticalSection(&This->lock);
557 static HRESULT WINAPI TiffDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
558 GUID *pguidContainerFormat)
560 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
564 static HRESULT WINAPI TiffDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
565 IWICBitmapDecoderInfo **ppIDecoderInfo)
567 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
571 static HRESULT WINAPI TiffDecoder_CopyPalette(IWICBitmapDecoder *iface,
572 IWICPalette *pIPalette)
574 FIXME("(%p,%p): stub\n", iface, pIPalette);
578 static HRESULT WINAPI TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
579 IWICMetadataQueryReader **ppIMetadataQueryReader)
581 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
585 static HRESULT WINAPI TiffDecoder_GetPreview(IWICBitmapDecoder *iface,
586 IWICBitmapSource **ppIBitmapSource)
588 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
592 static HRESULT WINAPI TiffDecoder_GetColorContexts(IWICBitmapDecoder *iface,
593 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
595 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
599 static HRESULT WINAPI TiffDecoder_GetThumbnail(IWICBitmapDecoder *iface,
600 IWICBitmapSource **ppIThumbnail)
602 TRACE("(%p,%p)\n", iface, ppIThumbnail);
603 return WINCODEC_ERR_CODECNOTHUMBNAIL;
606 static HRESULT WINAPI TiffDecoder_GetFrameCount(IWICBitmapDecoder *iface,
609 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
613 WARN("(%p) <-- WINCODEC_ERR_WRONGSTATE\n", iface);
614 return WINCODEC_ERR_WRONGSTATE;
617 EnterCriticalSection(&This->lock);
618 while (pTIFFReadDirectory(This->tiff)) { }
619 *pCount = pTIFFCurrentDirectory(This->tiff)+1;
620 LeaveCriticalSection(&This->lock);
622 TRACE("(%p) <-- %i\n", iface, *pCount);
627 static HRESULT WINAPI TiffDecoder_GetFrame(IWICBitmapDecoder *iface,
628 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
630 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
631 TiffFrameDecode *result;
633 tiff_decode_info decode_info;
636 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
639 return WINCODEC_ERR_WRONGSTATE;
641 EnterCriticalSection(&This->lock);
642 res = pTIFFSetDirectory(This->tiff, index);
643 if (!res) hr = E_INVALIDARG;
644 else hr = tiff_get_decode_info(This->tiff, &decode_info);
645 LeaveCriticalSection(&This->lock);
649 result = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode));
653 result->IWICBitmapFrameDecode_iface.lpVtbl = &TiffFrameDecode_Vtbl;
655 result->parent = This;
656 result->index = index;
657 result->decode_info = decode_info;
658 result->cached_tile_x = -1;
659 result->cached_tile = HeapAlloc(GetProcessHeap(), 0, decode_info.tile_size);
661 if (result->cached_tile)
662 *ppIBitmapFrame = (IWICBitmapFrameDecode*)result;
666 HeapFree(GetProcessHeap(), 0, result);
669 else hr = E_OUTOFMEMORY;
672 if (FAILED(hr)) *ppIBitmapFrame = NULL;
677 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl = {
678 TiffDecoder_QueryInterface,
681 TiffDecoder_QueryCapability,
682 TiffDecoder_Initialize,
683 TiffDecoder_GetContainerFormat,
684 TiffDecoder_GetDecoderInfo,
685 TiffDecoder_CopyPalette,
686 TiffDecoder_GetMetadataQueryReader,
687 TiffDecoder_GetPreview,
688 TiffDecoder_GetColorContexts,
689 TiffDecoder_GetThumbnail,
690 TiffDecoder_GetFrameCount,
694 static HRESULT WINAPI TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
697 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
698 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
700 if (!ppv) return E_INVALIDARG;
702 if (IsEqualIID(&IID_IUnknown, iid) ||
703 IsEqualIID(&IID_IWICBitmapSource, iid) ||
704 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
711 return E_NOINTERFACE;
714 IUnknown_AddRef((IUnknown*)*ppv);
718 static ULONG WINAPI TiffFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
720 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
721 ULONG ref = InterlockedIncrement(&This->ref);
723 TRACE("(%p) refcount=%u\n", iface, ref);
728 static ULONG WINAPI TiffFrameDecode_Release(IWICBitmapFrameDecode *iface)
730 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
731 ULONG ref = InterlockedDecrement(&This->ref);
733 TRACE("(%p) refcount=%u\n", iface, ref);
737 HeapFree(GetProcessHeap(), 0, This->cached_tile);
738 HeapFree(GetProcessHeap(), 0, This);
744 static HRESULT WINAPI TiffFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
745 UINT *puiWidth, UINT *puiHeight)
747 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
749 *puiWidth = This->decode_info.width;
750 *puiHeight = This->decode_info.height;
752 TRACE("(%p) <-- %ux%u\n", iface, *puiWidth, *puiHeight);
757 static HRESULT WINAPI TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
758 WICPixelFormatGUID *pPixelFormat)
760 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
762 memcpy(pPixelFormat, This->decode_info.format, sizeof(GUID));
764 TRACE("(%p) <-- %s\n", This, debugstr_guid(This->decode_info.format));
769 static HRESULT WINAPI TiffFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
770 double *pDpiX, double *pDpiY)
772 FIXME("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
776 static HRESULT WINAPI TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
777 IWICPalette *pIPalette)
779 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
780 uint16 *red, *green, *blue;
781 WICColor colors[256];
782 int color_count, ret, i;
784 TRACE("(%p,%p)\n", iface, pIPalette);
786 color_count = 1<<This->decode_info.bps;
788 EnterCriticalSection(&This->parent->lock);
789 ret = pTIFFGetField(This->parent->tiff, TIFFTAG_COLORMAP, &red, &green, &blue);
790 LeaveCriticalSection(&This->parent->lock);
794 WARN("Couldn't read color map\n");
798 for (i=0; i<color_count; i++)
800 colors[i] = 0xff000000 |
801 ((red[i]<<8) & 0xff0000) |
802 (green[i] & 0xff00) |
803 ((blue[i]>>8) & 0xff);
806 return IWICPalette_InitializeCustom(pIPalette, colors, color_count);
809 static HRESULT TiffFrameDecode_ReadTile(TiffFrameDecode *This, UINT tile_x, UINT tile_y)
815 swap_bytes = pTIFFIsByteSwapped(This->parent->tiff);
817 ret = pTIFFSetDirectory(This->parent->tiff, This->index);
824 if (This->decode_info.tiled)
826 ret = pTIFFReadEncodedTile(This->parent->tiff, tile_x + tile_y * This->decode_info.tiles_across, This->cached_tile, This->decode_info.tile_size);
830 ret = pTIFFReadEncodedStrip(This->parent->tiff, tile_y, This->cached_tile, This->decode_info.tile_size);
837 if (hr == S_OK && This->decode_info.reverse_bgr)
839 if (This->decode_info.bps == 8)
841 UINT sample_count = This->decode_info.samples;
843 reverse_bgr8(sample_count, This->cached_tile, This->decode_info.tile_width,
844 This->decode_info.tile_height, This->decode_info.tile_width * sample_count);
848 if (hr == S_OK && swap_bytes && This->decode_info.bps > 8)
850 UINT row, i, samples_per_row;
853 samples_per_row = This->decode_info.tile_width * This->decode_info.samples;
855 switch(This->decode_info.bps)
858 for (row=0; row<This->decode_info.tile_height; row++)
860 sample = This->cached_tile + row * This->decode_info.tile_stride;
861 for (i=0; i<samples_per_row; i++)
864 sample[1] = sample[0];
871 ERR("unhandled bps for byte swap %u\n", This->decode_info.bps);
876 if (hr == S_OK && This->decode_info.invert_grayscale)
880 if (This->decode_info.samples != 1)
882 ERR("cannot invert grayscale image with %u samples\n", This->decode_info.samples);
886 end = This->cached_tile+This->decode_info.tile_size;
888 for (byte = This->cached_tile; byte != end; byte++)
894 This->cached_tile_x = tile_x;
895 This->cached_tile_y = tile_y;
901 static HRESULT WINAPI TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
902 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
904 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
905 UINT min_tile_x, max_tile_x, min_tile_y, max_tile_y;
913 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
919 rect.Width = This->decode_info.width;
920 rect.Height = This->decode_info.height;
925 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->decode_info.width ||
926 prc->Y+prc->Height > This->decode_info.height)
930 bytesperrow = ((This->decode_info.bpp * prc->Width)+7)/8;
932 if (cbStride < bytesperrow)
935 if ((cbStride * prc->Height) > cbBufferSize)
938 min_tile_x = prc->X / This->decode_info.tile_width;
939 min_tile_y = prc->Y / This->decode_info.tile_height;
940 max_tile_x = (prc->X+prc->Width-1) / This->decode_info.tile_width;
941 max_tile_y = (prc->Y+prc->Height-1) / This->decode_info.tile_height;
943 EnterCriticalSection(&This->parent->lock);
945 for (tile_x=min_tile_x; tile_x <= max_tile_x; tile_x++)
947 for (tile_y=min_tile_y; tile_y <= max_tile_y; tile_y++)
949 if (tile_x != This->cached_tile_x || tile_y != This->cached_tile_y)
951 hr = TiffFrameDecode_ReadTile(This, tile_x, tile_y);
956 if (prc->X < tile_x * This->decode_info.tile_width)
959 rc.X = prc->X - tile_x * This->decode_info.tile_width;
961 if (prc->Y < tile_y * This->decode_info.tile_height)
964 rc.Y = prc->Y - tile_y * This->decode_info.tile_height;
966 if (prc->X+prc->Width > (tile_x+1) * This->decode_info.tile_width)
967 rc.Width = This->decode_info.tile_width - rc.X;
968 else if (prc->X < tile_x * This->decode_info.tile_width)
969 rc.Width = prc->Width + prc->X - tile_x * This->decode_info.tile_width;
971 rc.Width = prc->Width;
973 if (prc->Y+prc->Height > (tile_y+1) * This->decode_info.tile_height)
974 rc.Height = This->decode_info.tile_height - rc.Y;
975 else if (prc->Y < tile_y * This->decode_info.tile_height)
976 rc.Height = prc->Height + prc->Y - tile_y * This->decode_info.tile_height;
978 rc.Height = prc->Height;
980 dst_tilepos = pbBuffer + (cbStride * ((rc.Y + tile_y * This->decode_info.tile_height) - prc->Y)) +
981 ((This->decode_info.bpp * ((rc.X + tile_x * This->decode_info.tile_width) - prc->X) + 7) / 8);
983 hr = copy_pixels(This->decode_info.bpp, This->cached_tile,
984 This->decode_info.tile_width, This->decode_info.tile_height, This->decode_info.tile_stride,
985 &rc, cbStride, cbBufferSize, dst_tilepos);
990 LeaveCriticalSection(&This->parent->lock);
991 TRACE("<-- 0x%x\n", hr);
997 LeaveCriticalSection(&This->parent->lock);
1002 static HRESULT WINAPI TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
1003 IWICMetadataQueryReader **ppIMetadataQueryReader)
1005 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
1009 static HRESULT WINAPI TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
1010 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1012 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
1016 static HRESULT WINAPI TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
1017 IWICBitmapSource **ppIThumbnail)
1019 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
1023 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl = {
1024 TiffFrameDecode_QueryInterface,
1025 TiffFrameDecode_AddRef,
1026 TiffFrameDecode_Release,
1027 TiffFrameDecode_GetSize,
1028 TiffFrameDecode_GetPixelFormat,
1029 TiffFrameDecode_GetResolution,
1030 TiffFrameDecode_CopyPalette,
1031 TiffFrameDecode_CopyPixels,
1032 TiffFrameDecode_GetMetadataQueryReader,
1033 TiffFrameDecode_GetColorContexts,
1034 TiffFrameDecode_GetThumbnail
1037 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1042 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1046 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1048 if (!load_libtiff())
1050 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF);
1054 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder));
1055 if (!This) return E_OUTOFMEMORY;
1057 This->IWICBitmapDecoder_iface.lpVtbl = &TiffDecoder_Vtbl;
1059 This->stream = NULL;
1060 InitializeCriticalSection(&This->lock);
1061 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffDecoder.lock");
1063 This->initialized = FALSE;
1065 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1066 IUnknown_Release((IUnknown*)This);
1071 #else /* !SONAME_LIBTIFF */
1073 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1075 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");