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(TIFFSetDirectory
);
68 static void *load_libtiff(void)
72 EnterCriticalSection(&init_tiff_cs
);
74 if (!libtiff_handle
&&
75 (libtiff_handle
= wine_dlopen(SONAME_LIBTIFF
, RTLD_NOW
, NULL
, 0)) != NULL
)
78 #define LOAD_FUNCPTR(f) \
79 if((p##f = wine_dlsym(libtiff_handle, #f, NULL, 0)) == NULL) { \
80 ERR("failed to load symbol %s\n", #f); \
81 libtiff_handle = NULL; \
82 LeaveCriticalSection(&init_tiff_cs); \
85 LOAD_FUNCPTR(TIFFClientOpen
);
86 LOAD_FUNCPTR(TIFFClose
);
87 LOAD_FUNCPTR(TIFFCurrentDirectory
);
88 LOAD_FUNCPTR(TIFFGetField
);
89 LOAD_FUNCPTR(TIFFIsByteSwapped
);
90 LOAD_FUNCPTR(TIFFReadDirectory
);
91 LOAD_FUNCPTR(TIFFReadEncodedStrip
);
92 LOAD_FUNCPTR(TIFFSetDirectory
);
97 result
= libtiff_handle
;
99 LeaveCriticalSection(&init_tiff_cs
);
103 static tsize_t
tiff_stream_read(thandle_t client_data
, tdata_t data
, tsize_t size
)
105 IStream
*stream
= (IStream
*)client_data
;
109 hr
= IStream_Read(stream
, data
, size
, &bytes_read
);
110 if (FAILED(hr
)) bytes_read
= 0;
114 static tsize_t
tiff_stream_write(thandle_t client_data
, tdata_t data
, tsize_t size
)
116 IStream
*stream
= (IStream
*)client_data
;
120 hr
= IStream_Write(stream
, data
, size
, &bytes_written
);
121 if (FAILED(hr
)) bytes_written
= 0;
122 return bytes_written
;
125 static toff_t
tiff_stream_seek(thandle_t client_data
, toff_t offset
, int whence
)
127 IStream
*stream
= (IStream
*)client_data
;
130 ULARGE_INTEGER new_position
;
133 move
.QuadPart
= offset
;
137 origin
= STREAM_SEEK_SET
;
140 origin
= STREAM_SEEK_CUR
;
143 origin
= STREAM_SEEK_END
;
146 ERR("unknown whence value %i\n", whence
);
150 hr
= IStream_Seek(stream
, move
, origin
, &new_position
);
151 if (SUCCEEDED(hr
)) return new_position
.QuadPart
;
155 static int tiff_stream_close(thandle_t client_data
)
157 /* Caller is responsible for releasing the stream object. */
161 static toff_t
tiff_stream_size(thandle_t client_data
)
163 IStream
*stream
= (IStream
*)client_data
;
167 hr
= IStream_Stat(stream
, &statstg
, STATFLAG_NONAME
);
169 if (SUCCEEDED(hr
)) return statstg
.cbSize
.QuadPart
;
173 static int tiff_stream_map(thandle_t client_data
, tdata_t
*addr
, toff_t
*size
)
175 /* Cannot mmap streams */
179 static void tiff_stream_unmap(thandle_t client_data
, tdata_t addr
, toff_t size
)
181 /* No need to ever do this, since we can't map things. */
184 static TIFF
* tiff_open_stream(IStream
*stream
, const char *mode
)
189 IStream_Seek(stream
, zero
, STREAM_SEEK_SET
, NULL
);
191 return pTIFFClientOpen("<IStream object>", mode
, stream
, tiff_stream_read
,
192 tiff_stream_write
, tiff_stream_seek
, tiff_stream_close
,
193 tiff_stream_size
, tiff_stream_map
, tiff_stream_unmap
);
197 const IWICBitmapDecoderVtbl
*lpVtbl
;
200 CRITICAL_SECTION lock
; /* Must be held when tiff is used or initiailzed is set */
206 const WICPixelFormatGUID
*format
;
213 int invert_grayscale
;
215 UINT tile_width
, tile_height
;
221 const IWICBitmapFrameDecodeVtbl
*lpVtbl
;
225 tiff_decode_info decode_info
;
226 INT cached_tile_x
, cached_tile_y
;
230 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl
;
232 static HRESULT
tiff_get_decode_info(TIFF
*tiff
, tiff_decode_info
*decode_info
)
234 uint16 photometric
, bps
, samples
, planar
;
235 uint16 extra_sample_count
, *extra_samples
;
238 decode_info
->indexed
= 0;
239 decode_info
->reverse_bgr
= 0;
240 decode_info
->invert_grayscale
= 0;
242 ret
= pTIFFGetField(tiff
, TIFFTAG_PHOTOMETRIC
, &photometric
);
245 WARN("missing PhotometricInterpretation tag\n");
249 ret
= pTIFFGetField(tiff
, TIFFTAG_BITSPERSAMPLE
, &bps
);
251 decode_info
->bps
= bps
;
253 ret
= pTIFFGetField(tiff
, TIFFTAG_SAMPLESPERPIXEL
, &samples
);
254 if (!ret
) samples
= 1;
255 decode_info
->samples
= samples
;
261 ret
= pTIFFGetField(tiff
, TIFFTAG_PLANARCONFIG
, &planar
);
262 if (!ret
) planar
= 1;
265 FIXME("unhandled planar configuration %u\n", planar
);
269 decode_info
->planar
= planar
;
273 case 0: /* WhiteIsZero */
274 decode_info
->invert_grayscale
= 1;
275 case 1: /* BlackIsZero */
278 FIXME("unhandled grayscale sample count %u\n", samples
);
282 decode_info
->bpp
= bps
;
286 decode_info
->format
= &GUID_WICPixelFormatBlackWhite
;
289 decode_info
->format
= &GUID_WICPixelFormat4bppGray
;
292 decode_info
->format
= &GUID_WICPixelFormat8bppGray
;
295 FIXME("unhandled greyscale bit count %u\n", bps
);
300 decode_info
->bpp
= bps
* samples
;
304 ret
= pTIFFGetField(tiff
, TIFFTAG_EXTRASAMPLES
, &extra_sample_count
, &extra_samples
);
307 WARN("Cannot get extra sample type for RGB data, ret=%i count=%i\n", ret
, extra_sample_count
);
311 else if (samples
!= 3)
313 FIXME("unhandled RGB sample count %u\n", samples
);
320 decode_info
->reverse_bgr
= 1;
322 decode_info
->format
= &GUID_WICPixelFormat24bppBGR
;
324 switch(extra_samples
[0])
326 case 1: /* Associated (pre-multiplied) alpha data */
327 decode_info
->format
= &GUID_WICPixelFormat32bppPBGRA
;
329 case 2: /* Unassociated alpha data */
330 decode_info
->format
= &GUID_WICPixelFormat32bppBGRA
;
333 FIXME("unhandled extra sample type %i\n", extra_samples
[0]);
339 decode_info
->format
= &GUID_WICPixelFormat48bppRGB
;
341 switch(extra_samples
[0])
343 case 1: /* Associated (pre-multiplied) alpha data */
344 decode_info
->format
= &GUID_WICPixelFormat64bppPRGBA
;
346 case 2: /* Unassociated alpha data */
347 decode_info
->format
= &GUID_WICPixelFormat64bppRGBA
;
350 FIXME("unhandled extra sample type %i\n", extra_samples
[0]);
355 FIXME("unhandled RGB bit count %u\n", bps
);
359 case 3: /* RGB Palette */
362 FIXME("unhandled indexed sample count %u\n", samples
);
366 decode_info
->indexed
= 1;
367 decode_info
->bpp
= bps
;
371 decode_info
->format
= &GUID_WICPixelFormat4bppIndexed
;
374 decode_info
->format
= &GUID_WICPixelFormat8bppIndexed
;
377 FIXME("unhandled indexed bit count %u\n", bps
);
381 case 4: /* Transparency mask */
386 FIXME("unhandled PhotometricInterpretation %u\n", photometric
);
390 ret
= pTIFFGetField(tiff
, TIFFTAG_IMAGEWIDTH
, &decode_info
->width
);
393 WARN("missing image width\n");
397 ret
= pTIFFGetField(tiff
, TIFFTAG_IMAGELENGTH
, &decode_info
->height
);
400 WARN("missing image length\n");
404 ret
= pTIFFGetField(tiff
, TIFFTAG_ROWSPERSTRIP
, &decode_info
->tile_height
);
407 if (decode_info
->tile_height
> decode_info
->height
)
408 decode_info
->tile_height
= decode_info
->height
;
409 decode_info
->tile_width
= decode_info
->width
;
410 decode_info
->tile_stride
= ((decode_info
->bpp
* decode_info
->tile_width
+ 7)/8);
411 decode_info
->tile_size
= decode_info
->tile_height
* decode_info
->tile_stride
;
415 /* Probably a tiled image */
416 FIXME("missing RowsPerStrip value\n");
423 static HRESULT WINAPI
TiffDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
426 TiffDecoder
*This
= (TiffDecoder
*)iface
;
427 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
429 if (!ppv
) return E_INVALIDARG
;
431 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
438 return E_NOINTERFACE
;
441 IUnknown_AddRef((IUnknown
*)*ppv
);
445 static ULONG WINAPI
TiffDecoder_AddRef(IWICBitmapDecoder
*iface
)
447 TiffDecoder
*This
= (TiffDecoder
*)iface
;
448 ULONG ref
= InterlockedIncrement(&This
->ref
);
450 TRACE("(%p) refcount=%u\n", iface
, ref
);
455 static ULONG WINAPI
TiffDecoder_Release(IWICBitmapDecoder
*iface
)
457 TiffDecoder
*This
= (TiffDecoder
*)iface
;
458 ULONG ref
= InterlockedDecrement(&This
->ref
);
460 TRACE("(%p) refcount=%u\n", iface
, ref
);
464 if (This
->tiff
) pTIFFClose(This
->tiff
);
465 if (This
->stream
) IStream_Release(This
->stream
);
466 This
->lock
.DebugInfo
->Spare
[0] = 0;
467 DeleteCriticalSection(&This
->lock
);
468 HeapFree(GetProcessHeap(), 0, This
);
474 static HRESULT WINAPI
TiffDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
475 DWORD
*pdwCapability
)
477 FIXME("(%p,%p,%p): stub\n", iface
, pIStream
, pdwCapability
);
481 static HRESULT WINAPI
TiffDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
482 WICDecodeOptions cacheOptions
)
484 TiffDecoder
*This
= (TiffDecoder
*)iface
;
488 TRACE("(%p,%p,%x): stub\n", iface
, pIStream
, cacheOptions
);
490 EnterCriticalSection(&This
->lock
);
492 if (This
->initialized
)
494 hr
= WINCODEC_ERR_WRONGSTATE
;
498 tiff
= tiff_open_stream(pIStream
, "r");
507 This
->stream
= pIStream
;
508 IStream_AddRef(pIStream
);
509 This
->initialized
= TRUE
;
512 LeaveCriticalSection(&This
->lock
);
516 static HRESULT WINAPI
TiffDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
517 GUID
*pguidContainerFormat
)
519 memcpy(pguidContainerFormat
, &GUID_ContainerFormatTiff
, sizeof(GUID
));
523 static HRESULT WINAPI
TiffDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
524 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
526 FIXME("(%p,%p): stub\n", iface
, ppIDecoderInfo
);
530 static HRESULT WINAPI
TiffDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
531 IWICPalette
*pIPalette
)
533 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
537 static HRESULT WINAPI
TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
538 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
540 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
544 static HRESULT WINAPI
TiffDecoder_GetPreview(IWICBitmapDecoder
*iface
,
545 IWICBitmapSource
**ppIBitmapSource
)
547 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
551 static HRESULT WINAPI
TiffDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
552 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
554 FIXME("(%p,%u,%p,%p)\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
558 static HRESULT WINAPI
TiffDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
559 IWICBitmapSource
**ppIThumbnail
)
561 TRACE("(%p,%p)\n", iface
, ppIThumbnail
);
562 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
565 static HRESULT WINAPI
TiffDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
568 TiffDecoder
*This
= (TiffDecoder
*)iface
;
572 WARN("(%p) <-- WINCODEC_ERR_WRONGSTATE\n", iface
);
573 return WINCODEC_ERR_WRONGSTATE
;
576 EnterCriticalSection(&This
->lock
);
577 while (pTIFFReadDirectory(This
->tiff
)) { }
578 *pCount
= pTIFFCurrentDirectory(This
->tiff
)+1;
579 LeaveCriticalSection(&This
->lock
);
581 TRACE("(%p) <-- %i\n", iface
, *pCount
);
586 static HRESULT WINAPI
TiffDecoder_GetFrame(IWICBitmapDecoder
*iface
,
587 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
589 TiffDecoder
*This
= (TiffDecoder
*)iface
;
590 TiffFrameDecode
*result
;
592 tiff_decode_info decode_info
;
595 TRACE("(%p,%u,%p)\n", iface
, index
, ppIBitmapFrame
);
598 return WINCODEC_ERR_WRONGSTATE
;
600 EnterCriticalSection(&This
->lock
);
601 res
= pTIFFSetDirectory(This
->tiff
, index
);
602 if (!res
) hr
= E_INVALIDARG
;
603 else hr
= tiff_get_decode_info(This
->tiff
, &decode_info
);
604 LeaveCriticalSection(&This
->lock
);
608 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode
));
612 result
->lpVtbl
= &TiffFrameDecode_Vtbl
;
614 result
->parent
= This
;
615 result
->index
= index
;
616 result
->decode_info
= decode_info
;
617 result
->cached_tile_x
= -1;
618 result
->cached_tile
= HeapAlloc(GetProcessHeap(), 0, decode_info
.tile_size
);
620 if (result
->cached_tile
)
621 *ppIBitmapFrame
= (IWICBitmapFrameDecode
*)result
;
625 HeapFree(GetProcessHeap(), 0, result
);
628 else hr
= E_OUTOFMEMORY
;
631 if (FAILED(hr
)) *ppIBitmapFrame
= NULL
;
636 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl
= {
637 TiffDecoder_QueryInterface
,
640 TiffDecoder_QueryCapability
,
641 TiffDecoder_Initialize
,
642 TiffDecoder_GetContainerFormat
,
643 TiffDecoder_GetDecoderInfo
,
644 TiffDecoder_CopyPalette
,
645 TiffDecoder_GetMetadataQueryReader
,
646 TiffDecoder_GetPreview
,
647 TiffDecoder_GetColorContexts
,
648 TiffDecoder_GetThumbnail
,
649 TiffDecoder_GetFrameCount
,
653 static HRESULT WINAPI
TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
656 TiffFrameDecode
*This
= (TiffFrameDecode
*)iface
;
657 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
659 if (!ppv
) return E_INVALIDARG
;
661 if (IsEqualIID(&IID_IUnknown
, iid
) ||
662 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
663 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
670 return E_NOINTERFACE
;
673 IUnknown_AddRef((IUnknown
*)*ppv
);
677 static ULONG WINAPI
TiffFrameDecode_AddRef(IWICBitmapFrameDecode
*iface
)
679 TiffFrameDecode
*This
= (TiffFrameDecode
*)iface
;
680 ULONG ref
= InterlockedIncrement(&This
->ref
);
682 TRACE("(%p) refcount=%u\n", iface
, ref
);
687 static ULONG WINAPI
TiffFrameDecode_Release(IWICBitmapFrameDecode
*iface
)
689 TiffFrameDecode
*This
= (TiffFrameDecode
*)iface
;
690 ULONG ref
= InterlockedDecrement(&This
->ref
);
692 TRACE("(%p) refcount=%u\n", iface
, ref
);
696 HeapFree(GetProcessHeap(), 0, This
->cached_tile
);
697 HeapFree(GetProcessHeap(), 0, This
);
703 static HRESULT WINAPI
TiffFrameDecode_GetSize(IWICBitmapFrameDecode
*iface
,
704 UINT
*puiWidth
, UINT
*puiHeight
)
706 TiffFrameDecode
*This
= (TiffFrameDecode
*)iface
;
708 *puiWidth
= This
->decode_info
.width
;
709 *puiHeight
= This
->decode_info
.height
;
711 TRACE("(%p) <-- %ux%u\n", iface
, *puiWidth
, *puiHeight
);
716 static HRESULT WINAPI
TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
717 WICPixelFormatGUID
*pPixelFormat
)
719 TiffFrameDecode
*This
= (TiffFrameDecode
*)iface
;
721 memcpy(pPixelFormat
, This
->decode_info
.format
, sizeof(GUID
));
723 TRACE("(%p) <-- %s\n", This
, debugstr_guid(This
->decode_info
.format
));
728 static HRESULT WINAPI
TiffFrameDecode_GetResolution(IWICBitmapFrameDecode
*iface
,
729 double *pDpiX
, double *pDpiY
)
731 FIXME("(%p,%p,%p)\n", iface
, pDpiX
, pDpiY
);
735 static HRESULT WINAPI
TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode
*iface
,
736 IWICPalette
*pIPalette
)
738 TiffFrameDecode
*This
= (TiffFrameDecode
*)iface
;
739 uint16
*red
, *green
, *blue
;
740 WICColor colors
[256];
741 int color_count
, ret
, i
;
743 TRACE("(%p,%p)\n", iface
, pIPalette
);
745 color_count
= 1<<This
->decode_info
.bps
;
747 EnterCriticalSection(&This
->parent
->lock
);
748 ret
= pTIFFGetField(This
->parent
->tiff
, TIFFTAG_COLORMAP
, &red
, &green
, &blue
);
749 LeaveCriticalSection(&This
->parent
->lock
);
753 WARN("Couldn't read color map\n");
757 for (i
=0; i
<color_count
; i
++)
759 colors
[i
] = 0xff000000 |
760 ((red
[i
]<<8) & 0xff0000) |
761 (green
[i
] & 0xff00) |
762 ((blue
[i
]>>8) & 0xff);
765 return IWICPalette_InitializeCustom(pIPalette
, colors
, color_count
);
768 static HRESULT
TiffFrameDecode_ReadTile(TiffFrameDecode
*This
, UINT tile_x
, UINT tile_y
)
774 swap_bytes
= pTIFFIsByteSwapped(This
->parent
->tiff
);
776 ret
= pTIFFSetDirectory(This
->parent
->tiff
, This
->index
);
783 ret
= pTIFFReadEncodedStrip(This
->parent
->tiff
, tile_y
, This
->cached_tile
, This
->decode_info
.tile_size
);
789 if (hr
== S_OK
&& This
->decode_info
.reverse_bgr
)
791 if (This
->decode_info
.bps
== 8)
793 UINT i
, total_pixels
, sample_count
;
796 total_pixels
= This
->decode_info
.tile_width
* This
->decode_info
.tile_height
;
797 pixel
= This
->cached_tile
;
798 sample_count
= This
->decode_info
.samples
;
799 for (i
=0; i
<total_pixels
; i
++)
804 pixel
+= sample_count
;
809 if (hr
== S_OK
&& swap_bytes
&& This
->decode_info
.bps
> 8)
811 UINT row
, i
, samples_per_row
;
814 samples_per_row
= This
->decode_info
.tile_width
* This
->decode_info
.samples
;
816 switch(This
->decode_info
.bps
)
819 for (row
=0; row
<This
->decode_info
.tile_height
; row
++)
821 sample
= This
->cached_tile
+ row
* This
->decode_info
.tile_stride
;
822 for (i
=0; i
<samples_per_row
; i
++)
825 sample
[1] = sample
[0];
832 ERR("unhandled bps for byte swap %u\n", This
->decode_info
.bps
);
837 if (hr
== S_OK
&& This
->decode_info
.invert_grayscale
)
841 if (This
->decode_info
.samples
!= 1)
843 ERR("cannot invert grayscale image with %u samples\n", This
->decode_info
.samples
);
847 end
= This
->cached_tile
+This
->decode_info
.tile_size
;
849 for (byte
= This
->cached_tile
; byte
!= end
; byte
++)
855 This
->cached_tile_x
= tile_x
;
856 This
->cached_tile_y
= tile_y
;
862 static HRESULT WINAPI
TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode
*iface
,
863 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
865 TiffFrameDecode
*This
= (TiffFrameDecode
*)iface
;
866 UINT min_tile_x
, max_tile_x
, min_tile_y
, max_tile_y
;
874 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
880 rect
.Width
= This
->decode_info
.width
;
881 rect
.Height
= This
->decode_info
.height
;
886 if (prc
->X
< 0 || prc
->Y
< 0 || prc
->X
+prc
->Width
> This
->decode_info
.width
||
887 prc
->Y
+prc
->Height
> This
->decode_info
.height
)
891 bytesperrow
= ((This
->decode_info
.bpp
* prc
->Width
)+7)/8;
893 if (cbStride
< bytesperrow
)
896 if ((cbStride
* prc
->Height
) > cbBufferSize
)
899 min_tile_x
= prc
->X
/ This
->decode_info
.tile_width
;
900 min_tile_y
= prc
->Y
/ This
->decode_info
.tile_height
;
901 max_tile_x
= (prc
->X
+prc
->Width
-1) / This
->decode_info
.tile_width
;
902 max_tile_y
= (prc
->Y
+prc
->Height
-1) / This
->decode_info
.tile_height
;
904 EnterCriticalSection(&This
->parent
->lock
);
906 for (tile_x
=min_tile_x
; tile_x
<= max_tile_x
; tile_x
++)
908 for (tile_y
=min_tile_y
; tile_y
<= max_tile_y
; tile_y
++)
910 if (tile_x
!= This
->cached_tile_x
|| tile_y
!= This
->cached_tile_y
)
912 hr
= TiffFrameDecode_ReadTile(This
, tile_x
, tile_y
);
917 if (prc
->X
< tile_x
* This
->decode_info
.tile_width
)
920 rc
.X
= prc
->X
- tile_x
* This
->decode_info
.tile_width
;
922 if (prc
->Y
< tile_y
* This
->decode_info
.tile_height
)
925 rc
.Y
= prc
->Y
- tile_y
* This
->decode_info
.tile_height
;
927 if (prc
->X
+prc
->Width
> (tile_x
+1) * This
->decode_info
.tile_width
)
928 rc
.Width
= This
->decode_info
.tile_width
- rc
.X
;
929 else if (prc
->X
< tile_x
* This
->decode_info
.tile_width
)
930 rc
.Width
= prc
->Width
+ prc
->X
- tile_x
* This
->decode_info
.tile_width
;
932 rc
.Width
= prc
->Width
;
934 if (prc
->Y
+prc
->Height
> (tile_y
+1) * This
->decode_info
.tile_height
)
935 rc
.Height
= This
->decode_info
.tile_height
- rc
.Y
;
936 else if (prc
->Y
< tile_y
* This
->decode_info
.tile_height
)
937 rc
.Height
= prc
->Height
+ prc
->Y
- tile_y
* This
->decode_info
.tile_height
;
939 rc
.Height
= prc
->Height
;
941 dst_tilepos
= pbBuffer
+ (cbStride
* ((rc
.Y
+ tile_y
* This
->decode_info
.tile_height
) - prc
->Y
)) +
942 ((This
->decode_info
.bpp
* ((rc
.X
+ tile_x
* This
->decode_info
.tile_width
) - prc
->X
) + 7) / 8);
944 hr
= copy_pixels(This
->decode_info
.bpp
, This
->cached_tile
,
945 This
->decode_info
.tile_width
, This
->decode_info
.tile_height
, This
->decode_info
.tile_stride
,
946 &rc
, cbStride
, cbBufferSize
, dst_tilepos
);
951 LeaveCriticalSection(&This
->parent
->lock
);
952 TRACE("<-- 0x%x\n", hr
);
958 LeaveCriticalSection(&This
->parent
->lock
);
963 static HRESULT WINAPI
TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
964 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
966 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
970 static HRESULT WINAPI
TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode
*iface
,
971 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
973 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
977 static HRESULT WINAPI
TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode
*iface
,
978 IWICBitmapSource
**ppIThumbnail
)
980 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
984 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl
= {
985 TiffFrameDecode_QueryInterface
,
986 TiffFrameDecode_AddRef
,
987 TiffFrameDecode_Release
,
988 TiffFrameDecode_GetSize
,
989 TiffFrameDecode_GetPixelFormat
,
990 TiffFrameDecode_GetResolution
,
991 TiffFrameDecode_CopyPalette
,
992 TiffFrameDecode_CopyPixels
,
993 TiffFrameDecode_GetMetadataQueryReader
,
994 TiffFrameDecode_GetColorContexts
,
995 TiffFrameDecode_GetThumbnail
998 HRESULT
TiffDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1003 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
1007 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
1009 if (!load_libtiff())
1011 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF
);
1015 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder
));
1016 if (!This
) return E_OUTOFMEMORY
;
1018 This
->lpVtbl
= &TiffDecoder_Vtbl
;
1020 This
->stream
= NULL
;
1021 InitializeCriticalSection(&This
->lock
);
1022 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TiffDecoder.lock");
1024 This
->initialized
= FALSE
;
1026 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
1027 IUnknown_Release((IUnknown
*)This
);
1032 #else /* !SONAME_LIBTIFF */
1034 HRESULT
TiffDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
1036 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");