windowscodecs: Ignore an unspecified extra sample in RGB TIFF images.
[wine/multimedia.git] / dlls / windowscodecs / tiffformat.c
blob976870ae5b239103e0c9f1d05bf8f431142f9282
1 /*
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdarg.h>
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #ifdef HAVE_TIFFIO_H
27 #include <tiffio.h>
28 #endif
30 #define COBJMACROS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "objbase.h"
35 #include "wincodec.h"
37 #include "wincodecs_private.h"
39 #include "wine/debug.h"
40 #include "wine/library.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
44 #ifdef SONAME_LIBTIFF
46 static CRITICAL_SECTION init_tiff_cs;
47 static CRITICAL_SECTION_DEBUG init_tiff_cs_debug =
49 0, 0, &init_tiff_cs,
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);
66 #undef MAKE_FUNCPTR
68 static void *load_libtiff(void)
70 void *result;
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); \
83 return NULL; \
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);
93 #undef LOAD_FUNCPTR
97 result = libtiff_handle;
99 LeaveCriticalSection(&init_tiff_cs);
100 return result;
103 static tsize_t tiff_stream_read(thandle_t client_data, tdata_t data, tsize_t size)
105 IStream *stream = (IStream*)client_data;
106 ULONG bytes_read;
107 HRESULT hr;
109 hr = IStream_Read(stream, data, size, &bytes_read);
110 if (FAILED(hr)) bytes_read = 0;
111 return bytes_read;
114 static tsize_t tiff_stream_write(thandle_t client_data, tdata_t data, tsize_t size)
116 IStream *stream = (IStream*)client_data;
117 ULONG bytes_written;
118 HRESULT hr;
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;
128 LARGE_INTEGER move;
129 DWORD origin;
130 ULARGE_INTEGER new_position;
131 HRESULT hr;
133 move.QuadPart = offset;
134 switch (whence)
136 case SEEK_SET:
137 origin = STREAM_SEEK_SET;
138 break;
139 case SEEK_CUR:
140 origin = STREAM_SEEK_CUR;
141 break;
142 case SEEK_END:
143 origin = STREAM_SEEK_END;
144 break;
145 default:
146 ERR("unknown whence value %i\n", whence);
147 return -1;
150 hr = IStream_Seek(stream, move, origin, &new_position);
151 if (SUCCEEDED(hr)) return new_position.QuadPart;
152 else return -1;
155 static int tiff_stream_close(thandle_t client_data)
157 /* Caller is responsible for releasing the stream object. */
158 return 0;
161 static toff_t tiff_stream_size(thandle_t client_data)
163 IStream *stream = (IStream*)client_data;
164 STATSTG statstg;
165 HRESULT hr;
167 hr = IStream_Stat(stream, &statstg, STATFLAG_NONAME);
169 if (SUCCEEDED(hr)) return statstg.cbSize.QuadPart;
170 else return -1;
173 static int tiff_stream_map(thandle_t client_data, tdata_t *addr, toff_t *size)
175 /* Cannot mmap streams */
176 return 0;
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)
186 LARGE_INTEGER zero;
188 zero.QuadPart = 0;
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);
196 typedef struct {
197 IWICBitmapDecoder IWICBitmapDecoder_iface;
198 LONG ref;
199 IStream *stream;
200 CRITICAL_SECTION lock; /* Must be held when tiff is used or initiailzed is set */
201 TIFF *tiff;
202 BOOL initialized;
203 } TiffDecoder;
205 typedef struct {
206 const WICPixelFormatGUID *format;
207 int bps;
208 int samples;
209 int bpp;
210 int planar;
211 int indexed;
212 int reverse_bgr;
213 int invert_grayscale;
214 UINT width, height;
215 UINT tile_width, tile_height;
216 UINT tile_stride;
217 UINT tile_size;
218 } tiff_decode_info;
220 typedef struct {
221 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
222 LONG ref;
223 TiffDecoder *parent;
224 UINT index;
225 tiff_decode_info decode_info;
226 INT cached_tile_x, cached_tile_y;
227 BYTE *cached_tile;
228 } TiffFrameDecode;
230 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl;
232 static inline TiffDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
234 return CONTAINING_RECORD(iface, TiffDecoder, IWICBitmapDecoder_iface);
237 static inline TiffFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
239 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICBitmapFrameDecode_iface);
242 static HRESULT tiff_get_decode_info(TIFF *tiff, tiff_decode_info *decode_info)
244 uint16 photometric, bps, samples, planar;
245 uint16 extra_sample_count, *extra_samples;
246 int ret;
248 decode_info->indexed = 0;
249 decode_info->reverse_bgr = 0;
250 decode_info->invert_grayscale = 0;
252 ret = pTIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
253 if (!ret)
255 WARN("missing PhotometricInterpretation tag\n");
256 return E_FAIL;
259 ret = pTIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bps);
260 if (!ret) bps = 1;
261 decode_info->bps = bps;
263 ret = pTIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samples);
264 if (!ret) samples = 1;
265 decode_info->samples = samples;
267 if (samples == 1)
268 planar = 1;
269 else
271 ret = pTIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planar);
272 if (!ret) planar = 1;
273 if (planar != 1)
275 FIXME("unhandled planar configuration %u\n", planar);
276 return E_FAIL;
279 decode_info->planar = planar;
281 switch(photometric)
283 case 0: /* WhiteIsZero */
284 decode_info->invert_grayscale = 1;
285 case 1: /* BlackIsZero */
286 if (samples != 1)
288 FIXME("unhandled grayscale sample count %u\n", samples);
289 return E_FAIL;
292 decode_info->bpp = bps;
293 switch (bps)
295 case 1:
296 decode_info->format = &GUID_WICPixelFormatBlackWhite;
297 break;
298 case 4:
299 decode_info->format = &GUID_WICPixelFormat4bppGray;
300 break;
301 case 8:
302 decode_info->format = &GUID_WICPixelFormat8bppGray;
303 break;
304 default:
305 FIXME("unhandled greyscale bit count %u\n", bps);
306 return E_FAIL;
308 break;
309 case 2: /* RGB */
310 decode_info->bpp = bps * samples;
312 if (samples == 4)
314 ret = pTIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extra_sample_count, &extra_samples);
315 if (!ret)
317 WARN("Cannot get extra sample type for RGB data, ret=%i count=%i\n", ret, extra_sample_count);
318 return E_FAIL;
321 else if (samples != 3)
323 FIXME("unhandled RGB sample count %u\n", samples);
324 return E_FAIL;
327 switch(bps)
329 case 8:
330 decode_info->reverse_bgr = 1;
331 if (samples == 3)
332 decode_info->format = &GUID_WICPixelFormat24bppBGR;
333 else
334 switch(extra_samples[0])
336 case 0: /* Unspecified data */
337 decode_info->format = &GUID_WICPixelFormat32bppBGR;
338 break;
339 case 1: /* Associated (pre-multiplied) alpha data */
340 decode_info->format = &GUID_WICPixelFormat32bppPBGRA;
341 break;
342 case 2: /* Unassociated alpha data */
343 decode_info->format = &GUID_WICPixelFormat32bppBGRA;
344 break;
345 default:
346 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
347 return E_FAIL;
349 break;
350 case 16:
351 if (samples == 3)
352 decode_info->format = &GUID_WICPixelFormat48bppRGB;
353 else
354 switch(extra_samples[0])
356 case 0: /* Unspecified data */
357 /* decode_info->format = &GUID_WICPixelFormat64bppRGB; */
358 FIXME("64-bit RGB is unsupported\n");
359 return E_FAIL;
360 case 1: /* Associated (pre-multiplied) alpha data */
361 decode_info->format = &GUID_WICPixelFormat64bppPRGBA;
362 break;
363 case 2: /* Unassociated alpha data */
364 decode_info->format = &GUID_WICPixelFormat64bppRGBA;
365 break;
366 default:
367 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
368 return E_FAIL;
370 break;
371 default:
372 FIXME("unhandled RGB bit count %u\n", bps);
373 return E_FAIL;
375 break;
376 case 3: /* RGB Palette */
377 if (samples != 1)
379 FIXME("unhandled indexed sample count %u\n", samples);
380 return E_FAIL;
383 decode_info->indexed = 1;
384 decode_info->bpp = bps;
385 switch (bps)
387 case 4:
388 decode_info->format = &GUID_WICPixelFormat4bppIndexed;
389 break;
390 case 8:
391 decode_info->format = &GUID_WICPixelFormat8bppIndexed;
392 break;
393 default:
394 FIXME("unhandled indexed bit count %u\n", bps);
395 return E_FAIL;
397 break;
398 case 4: /* Transparency mask */
399 case 5: /* CMYK */
400 case 6: /* YCbCr */
401 case 8: /* CIELab */
402 default:
403 FIXME("unhandled PhotometricInterpretation %u\n", photometric);
404 return E_FAIL;
407 ret = pTIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &decode_info->width);
408 if (!ret)
410 WARN("missing image width\n");
411 return E_FAIL;
414 ret = pTIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &decode_info->height);
415 if (!ret)
417 WARN("missing image length\n");
418 return E_FAIL;
421 ret = pTIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &decode_info->tile_height);
422 if (ret)
424 if (decode_info->tile_height > decode_info->height)
425 decode_info->tile_height = decode_info->height;
426 decode_info->tile_width = decode_info->width;
427 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
428 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
430 else
432 /* Probably a tiled image */
433 FIXME("missing RowsPerStrip value\n");
434 return E_FAIL;
437 return S_OK;
440 static HRESULT WINAPI TiffDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
441 void **ppv)
443 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
444 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
446 if (!ppv) return E_INVALIDARG;
448 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
450 *ppv = This;
452 else
454 *ppv = NULL;
455 return E_NOINTERFACE;
458 IUnknown_AddRef((IUnknown*)*ppv);
459 return S_OK;
462 static ULONG WINAPI TiffDecoder_AddRef(IWICBitmapDecoder *iface)
464 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
465 ULONG ref = InterlockedIncrement(&This->ref);
467 TRACE("(%p) refcount=%u\n", iface, ref);
469 return ref;
472 static ULONG WINAPI TiffDecoder_Release(IWICBitmapDecoder *iface)
474 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
475 ULONG ref = InterlockedDecrement(&This->ref);
477 TRACE("(%p) refcount=%u\n", iface, ref);
479 if (ref == 0)
481 if (This->tiff) pTIFFClose(This->tiff);
482 if (This->stream) IStream_Release(This->stream);
483 This->lock.DebugInfo->Spare[0] = 0;
484 DeleteCriticalSection(&This->lock);
485 HeapFree(GetProcessHeap(), 0, This);
488 return ref;
491 static HRESULT WINAPI TiffDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
492 DWORD *pdwCapability)
494 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
495 return E_NOTIMPL;
498 static HRESULT WINAPI TiffDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
499 WICDecodeOptions cacheOptions)
501 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
502 TIFF *tiff;
503 HRESULT hr=S_OK;
505 TRACE("(%p,%p,%x): stub\n", iface, pIStream, cacheOptions);
507 EnterCriticalSection(&This->lock);
509 if (This->initialized)
511 hr = WINCODEC_ERR_WRONGSTATE;
512 goto exit;
515 tiff = tiff_open_stream(pIStream, "r");
517 if (!tiff)
519 hr = E_FAIL;
520 goto exit;
523 This->tiff = tiff;
524 This->stream = pIStream;
525 IStream_AddRef(pIStream);
526 This->initialized = TRUE;
528 exit:
529 LeaveCriticalSection(&This->lock);
530 return hr;
533 static HRESULT WINAPI TiffDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
534 GUID *pguidContainerFormat)
536 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
537 return S_OK;
540 static HRESULT WINAPI TiffDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
541 IWICBitmapDecoderInfo **ppIDecoderInfo)
543 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
544 return E_NOTIMPL;
547 static HRESULT WINAPI TiffDecoder_CopyPalette(IWICBitmapDecoder *iface,
548 IWICPalette *pIPalette)
550 FIXME("(%p,%p): stub\n", iface, pIPalette);
551 return E_NOTIMPL;
554 static HRESULT WINAPI TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
555 IWICMetadataQueryReader **ppIMetadataQueryReader)
557 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
558 return E_NOTIMPL;
561 static HRESULT WINAPI TiffDecoder_GetPreview(IWICBitmapDecoder *iface,
562 IWICBitmapSource **ppIBitmapSource)
564 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
565 return E_NOTIMPL;
568 static HRESULT WINAPI TiffDecoder_GetColorContexts(IWICBitmapDecoder *iface,
569 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
571 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
572 return E_NOTIMPL;
575 static HRESULT WINAPI TiffDecoder_GetThumbnail(IWICBitmapDecoder *iface,
576 IWICBitmapSource **ppIThumbnail)
578 TRACE("(%p,%p)\n", iface, ppIThumbnail);
579 return WINCODEC_ERR_CODECNOTHUMBNAIL;
582 static HRESULT WINAPI TiffDecoder_GetFrameCount(IWICBitmapDecoder *iface,
583 UINT *pCount)
585 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
587 if (!This->tiff)
589 WARN("(%p) <-- WINCODEC_ERR_WRONGSTATE\n", iface);
590 return WINCODEC_ERR_WRONGSTATE;
593 EnterCriticalSection(&This->lock);
594 while (pTIFFReadDirectory(This->tiff)) { }
595 *pCount = pTIFFCurrentDirectory(This->tiff)+1;
596 LeaveCriticalSection(&This->lock);
598 TRACE("(%p) <-- %i\n", iface, *pCount);
600 return S_OK;
603 static HRESULT WINAPI TiffDecoder_GetFrame(IWICBitmapDecoder *iface,
604 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
606 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
607 TiffFrameDecode *result;
608 int res;
609 tiff_decode_info decode_info;
610 HRESULT hr;
612 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
614 if (!This->tiff)
615 return WINCODEC_ERR_WRONGSTATE;
617 EnterCriticalSection(&This->lock);
618 res = pTIFFSetDirectory(This->tiff, index);
619 if (!res) hr = E_INVALIDARG;
620 else hr = tiff_get_decode_info(This->tiff, &decode_info);
621 LeaveCriticalSection(&This->lock);
623 if (SUCCEEDED(hr))
625 result = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode));
627 if (result)
629 result->IWICBitmapFrameDecode_iface.lpVtbl = &TiffFrameDecode_Vtbl;
630 result->ref = 1;
631 result->parent = This;
632 result->index = index;
633 result->decode_info = decode_info;
634 result->cached_tile_x = -1;
635 result->cached_tile = HeapAlloc(GetProcessHeap(), 0, decode_info.tile_size);
637 if (result->cached_tile)
638 *ppIBitmapFrame = (IWICBitmapFrameDecode*)result;
639 else
641 hr = E_OUTOFMEMORY;
642 HeapFree(GetProcessHeap(), 0, result);
645 else hr = E_OUTOFMEMORY;
648 if (FAILED(hr)) *ppIBitmapFrame = NULL;
650 return hr;
653 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl = {
654 TiffDecoder_QueryInterface,
655 TiffDecoder_AddRef,
656 TiffDecoder_Release,
657 TiffDecoder_QueryCapability,
658 TiffDecoder_Initialize,
659 TiffDecoder_GetContainerFormat,
660 TiffDecoder_GetDecoderInfo,
661 TiffDecoder_CopyPalette,
662 TiffDecoder_GetMetadataQueryReader,
663 TiffDecoder_GetPreview,
664 TiffDecoder_GetColorContexts,
665 TiffDecoder_GetThumbnail,
666 TiffDecoder_GetFrameCount,
667 TiffDecoder_GetFrame
670 static HRESULT WINAPI TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
671 void **ppv)
673 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
674 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
676 if (!ppv) return E_INVALIDARG;
678 if (IsEqualIID(&IID_IUnknown, iid) ||
679 IsEqualIID(&IID_IWICBitmapSource, iid) ||
680 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
682 *ppv = This;
684 else
686 *ppv = NULL;
687 return E_NOINTERFACE;
690 IUnknown_AddRef((IUnknown*)*ppv);
691 return S_OK;
694 static ULONG WINAPI TiffFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
696 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
697 ULONG ref = InterlockedIncrement(&This->ref);
699 TRACE("(%p) refcount=%u\n", iface, ref);
701 return ref;
704 static ULONG WINAPI TiffFrameDecode_Release(IWICBitmapFrameDecode *iface)
706 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
707 ULONG ref = InterlockedDecrement(&This->ref);
709 TRACE("(%p) refcount=%u\n", iface, ref);
711 if (ref == 0)
713 HeapFree(GetProcessHeap(), 0, This->cached_tile);
714 HeapFree(GetProcessHeap(), 0, This);
717 return ref;
720 static HRESULT WINAPI TiffFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
721 UINT *puiWidth, UINT *puiHeight)
723 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
725 *puiWidth = This->decode_info.width;
726 *puiHeight = This->decode_info.height;
728 TRACE("(%p) <-- %ux%u\n", iface, *puiWidth, *puiHeight);
730 return S_OK;
733 static HRESULT WINAPI TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
734 WICPixelFormatGUID *pPixelFormat)
736 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
738 memcpy(pPixelFormat, This->decode_info.format, sizeof(GUID));
740 TRACE("(%p) <-- %s\n", This, debugstr_guid(This->decode_info.format));
742 return S_OK;
745 static HRESULT WINAPI TiffFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
746 double *pDpiX, double *pDpiY)
748 FIXME("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
749 return E_NOTIMPL;
752 static HRESULT WINAPI TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
753 IWICPalette *pIPalette)
755 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
756 uint16 *red, *green, *blue;
757 WICColor colors[256];
758 int color_count, ret, i;
760 TRACE("(%p,%p)\n", iface, pIPalette);
762 color_count = 1<<This->decode_info.bps;
764 EnterCriticalSection(&This->parent->lock);
765 ret = pTIFFGetField(This->parent->tiff, TIFFTAG_COLORMAP, &red, &green, &blue);
766 LeaveCriticalSection(&This->parent->lock);
768 if (!ret)
770 WARN("Couldn't read color map\n");
771 return E_FAIL;
774 for (i=0; i<color_count; i++)
776 colors[i] = 0xff000000 |
777 ((red[i]<<8) & 0xff0000) |
778 (green[i] & 0xff00) |
779 ((blue[i]>>8) & 0xff);
782 return IWICPalette_InitializeCustom(pIPalette, colors, color_count);
785 static HRESULT TiffFrameDecode_ReadTile(TiffFrameDecode *This, UINT tile_x, UINT tile_y)
787 HRESULT hr=S_OK;
788 tsize_t ret;
789 int swap_bytes;
791 swap_bytes = pTIFFIsByteSwapped(This->parent->tiff);
793 ret = pTIFFSetDirectory(This->parent->tiff, This->index);
795 if (ret == -1)
796 hr = E_FAIL;
798 if (hr == S_OK)
800 ret = pTIFFReadEncodedStrip(This->parent->tiff, tile_y, This->cached_tile, This->decode_info.tile_size);
802 if (ret == -1)
803 hr = E_FAIL;
806 if (hr == S_OK && This->decode_info.reverse_bgr)
808 if (This->decode_info.bps == 8)
810 UINT sample_count = This->decode_info.samples;
812 reverse_bgr8(sample_count, This->cached_tile, This->decode_info.tile_width,
813 This->decode_info.tile_height, This->decode_info.tile_width * sample_count);
817 if (hr == S_OK && swap_bytes && This->decode_info.bps > 8)
819 UINT row, i, samples_per_row;
820 BYTE *sample, temp;
822 samples_per_row = This->decode_info.tile_width * This->decode_info.samples;
824 switch(This->decode_info.bps)
826 case 16:
827 for (row=0; row<This->decode_info.tile_height; row++)
829 sample = This->cached_tile + row * This->decode_info.tile_stride;
830 for (i=0; i<samples_per_row; i++)
832 temp = sample[1];
833 sample[1] = sample[0];
834 sample[0] = temp;
835 sample += 2;
838 break;
839 default:
840 ERR("unhandled bps for byte swap %u\n", This->decode_info.bps);
841 return E_FAIL;
845 if (hr == S_OK && This->decode_info.invert_grayscale)
847 BYTE *byte, *end;
849 if (This->decode_info.samples != 1)
851 ERR("cannot invert grayscale image with %u samples\n", This->decode_info.samples);
852 return E_FAIL;
855 end = This->cached_tile+This->decode_info.tile_size;
857 for (byte = This->cached_tile; byte != end; byte++)
858 *byte = ~(*byte);
861 if (hr == S_OK)
863 This->cached_tile_x = tile_x;
864 This->cached_tile_y = tile_y;
867 return hr;
870 static HRESULT WINAPI TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
871 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
873 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
874 UINT min_tile_x, max_tile_x, min_tile_y, max_tile_y;
875 UINT tile_x, tile_y;
876 WICRect rc;
877 HRESULT hr=S_OK;
878 BYTE *dst_tilepos;
879 UINT bytesperrow;
880 WICRect rect;
882 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
884 if (!prc)
886 rect.X = 0;
887 rect.Y = 0;
888 rect.Width = This->decode_info.width;
889 rect.Height = This->decode_info.height;
890 prc = &rect;
892 else
894 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->decode_info.width ||
895 prc->Y+prc->Height > This->decode_info.height)
896 return E_INVALIDARG;
899 bytesperrow = ((This->decode_info.bpp * prc->Width)+7)/8;
901 if (cbStride < bytesperrow)
902 return E_INVALIDARG;
904 if ((cbStride * prc->Height) > cbBufferSize)
905 return E_INVALIDARG;
907 min_tile_x = prc->X / This->decode_info.tile_width;
908 min_tile_y = prc->Y / This->decode_info.tile_height;
909 max_tile_x = (prc->X+prc->Width-1) / This->decode_info.tile_width;
910 max_tile_y = (prc->Y+prc->Height-1) / This->decode_info.tile_height;
912 EnterCriticalSection(&This->parent->lock);
914 for (tile_x=min_tile_x; tile_x <= max_tile_x; tile_x++)
916 for (tile_y=min_tile_y; tile_y <= max_tile_y; tile_y++)
918 if (tile_x != This->cached_tile_x || tile_y != This->cached_tile_y)
920 hr = TiffFrameDecode_ReadTile(This, tile_x, tile_y);
923 if (SUCCEEDED(hr))
925 if (prc->X < tile_x * This->decode_info.tile_width)
926 rc.X = 0;
927 else
928 rc.X = prc->X - tile_x * This->decode_info.tile_width;
930 if (prc->Y < tile_y * This->decode_info.tile_height)
931 rc.Y = 0;
932 else
933 rc.Y = prc->Y - tile_y * This->decode_info.tile_height;
935 if (prc->X+prc->Width > (tile_x+1) * This->decode_info.tile_width)
936 rc.Width = This->decode_info.tile_width - rc.X;
937 else if (prc->X < tile_x * This->decode_info.tile_width)
938 rc.Width = prc->Width + prc->X - tile_x * This->decode_info.tile_width;
939 else
940 rc.Width = prc->Width;
942 if (prc->Y+prc->Height > (tile_y+1) * This->decode_info.tile_height)
943 rc.Height = This->decode_info.tile_height - rc.Y;
944 else if (prc->Y < tile_y * This->decode_info.tile_height)
945 rc.Height = prc->Height + prc->Y - tile_y * This->decode_info.tile_height;
946 else
947 rc.Height = prc->Height;
949 dst_tilepos = pbBuffer + (cbStride * ((rc.Y + tile_y * This->decode_info.tile_height) - prc->Y)) +
950 ((This->decode_info.bpp * ((rc.X + tile_x * This->decode_info.tile_width) - prc->X) + 7) / 8);
952 hr = copy_pixels(This->decode_info.bpp, This->cached_tile,
953 This->decode_info.tile_width, This->decode_info.tile_height, This->decode_info.tile_stride,
954 &rc, cbStride, cbBufferSize, dst_tilepos);
957 if (FAILED(hr))
959 LeaveCriticalSection(&This->parent->lock);
960 TRACE("<-- 0x%x\n", hr);
961 return hr;
966 LeaveCriticalSection(&This->parent->lock);
968 return S_OK;
971 static HRESULT WINAPI TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
972 IWICMetadataQueryReader **ppIMetadataQueryReader)
974 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
975 return E_NOTIMPL;
978 static HRESULT WINAPI TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
979 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
981 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
982 return E_NOTIMPL;
985 static HRESULT WINAPI TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
986 IWICBitmapSource **ppIThumbnail)
988 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
989 return E_NOTIMPL;
992 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl = {
993 TiffFrameDecode_QueryInterface,
994 TiffFrameDecode_AddRef,
995 TiffFrameDecode_Release,
996 TiffFrameDecode_GetSize,
997 TiffFrameDecode_GetPixelFormat,
998 TiffFrameDecode_GetResolution,
999 TiffFrameDecode_CopyPalette,
1000 TiffFrameDecode_CopyPixels,
1001 TiffFrameDecode_GetMetadataQueryReader,
1002 TiffFrameDecode_GetColorContexts,
1003 TiffFrameDecode_GetThumbnail
1006 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1008 HRESULT ret;
1009 TiffDecoder *This;
1011 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1013 *ppv = NULL;
1015 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1017 if (!load_libtiff())
1019 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF);
1020 return E_FAIL;
1023 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder));
1024 if (!This) return E_OUTOFMEMORY;
1026 This->IWICBitmapDecoder_iface.lpVtbl = &TiffDecoder_Vtbl;
1027 This->ref = 1;
1028 This->stream = NULL;
1029 InitializeCriticalSection(&This->lock);
1030 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffDecoder.lock");
1031 This->tiff = NULL;
1032 This->initialized = FALSE;
1034 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1035 IUnknown_Release((IUnknown*)This);
1037 return ret;
1040 #else /* !SONAME_LIBTIFF */
1042 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1044 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
1045 return E_FAIL;
1048 #endif