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);
67 MAKE_FUNCPTR(TIFFSetField);
68 MAKE_FUNCPTR(TIFFWriteDirectory);
69 MAKE_FUNCPTR(TIFFWriteScanline);
72 static void *load_libtiff(void)
76 EnterCriticalSection(&init_tiff_cs);
78 if (!libtiff_handle &&
79 (libtiff_handle = wine_dlopen(SONAME_LIBTIFF, RTLD_NOW, NULL, 0)) != NULL)
82 #define LOAD_FUNCPTR(f) \
83 if((p##f = wine_dlsym(libtiff_handle, #f, NULL, 0)) == NULL) { \
84 ERR("failed to load symbol %s\n", #f); \
85 libtiff_handle = NULL; \
86 LeaveCriticalSection(&init_tiff_cs); \
89 LOAD_FUNCPTR(TIFFClientOpen);
90 LOAD_FUNCPTR(TIFFClose);
91 LOAD_FUNCPTR(TIFFCurrentDirectory);
92 LOAD_FUNCPTR(TIFFGetField);
93 LOAD_FUNCPTR(TIFFIsByteSwapped);
94 LOAD_FUNCPTR(TIFFReadDirectory);
95 LOAD_FUNCPTR(TIFFReadEncodedStrip);
96 LOAD_FUNCPTR(TIFFReadEncodedTile);
97 LOAD_FUNCPTR(TIFFSetDirectory);
98 LOAD_FUNCPTR(TIFFSetField);
99 LOAD_FUNCPTR(TIFFWriteDirectory);
100 LOAD_FUNCPTR(TIFFWriteScanline);
105 result = libtiff_handle;
107 LeaveCriticalSection(&init_tiff_cs);
111 static tsize_t tiff_stream_read(thandle_t client_data, tdata_t data, tsize_t size)
113 IStream *stream = (IStream*)client_data;
117 hr = IStream_Read(stream, data, size, &bytes_read);
118 if (FAILED(hr)) bytes_read = 0;
122 static tsize_t tiff_stream_write(thandle_t client_data, tdata_t data, tsize_t size)
124 IStream *stream = (IStream*)client_data;
128 hr = IStream_Write(stream, data, size, &bytes_written);
129 if (FAILED(hr)) bytes_written = 0;
130 return bytes_written;
133 static toff_t tiff_stream_seek(thandle_t client_data, toff_t offset, int whence)
135 IStream *stream = (IStream*)client_data;
138 ULARGE_INTEGER new_position;
141 move.QuadPart = offset;
145 origin = STREAM_SEEK_SET;
148 origin = STREAM_SEEK_CUR;
151 origin = STREAM_SEEK_END;
154 ERR("unknown whence value %i\n", whence);
158 hr = IStream_Seek(stream, move, origin, &new_position);
159 if (SUCCEEDED(hr)) return new_position.QuadPart;
163 static int tiff_stream_close(thandle_t client_data)
165 /* Caller is responsible for releasing the stream object. */
169 static toff_t tiff_stream_size(thandle_t client_data)
171 IStream *stream = (IStream*)client_data;
175 hr = IStream_Stat(stream, &statstg, STATFLAG_NONAME);
177 if (SUCCEEDED(hr)) return statstg.cbSize.QuadPart;
181 static int tiff_stream_map(thandle_t client_data, tdata_t *addr, toff_t *size)
183 /* Cannot mmap streams */
187 static void tiff_stream_unmap(thandle_t client_data, tdata_t addr, toff_t size)
189 /* No need to ever do this, since we can't map things. */
192 static TIFF* tiff_open_stream(IStream *stream, const char *mode)
197 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
199 return pTIFFClientOpen("<IStream object>", mode, stream, tiff_stream_read,
200 tiff_stream_write, tiff_stream_seek, tiff_stream_close,
201 tiff_stream_size, tiff_stream_map, tiff_stream_unmap);
205 IWICBitmapDecoder IWICBitmapDecoder_iface;
208 CRITICAL_SECTION lock; /* Must be held when tiff is used or initiailzed is set */
214 const WICPixelFormatGUID *format;
221 int invert_grayscale;
223 UINT tile_width, tile_height;
228 UINT resolution_unit;
233 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
237 tiff_decode_info decode_info;
238 INT cached_tile_x, cached_tile_y;
242 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl;
244 static inline TiffDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
246 return CONTAINING_RECORD(iface, TiffDecoder, IWICBitmapDecoder_iface);
249 static inline TiffFrameDecode *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
251 return CONTAINING_RECORD(iface, TiffFrameDecode, IWICBitmapFrameDecode_iface);
254 static HRESULT tiff_get_decode_info(TIFF *tiff, tiff_decode_info *decode_info)
256 uint16 photometric, bps, samples, planar;
257 uint16 extra_sample_count, extra_sample, *extra_samples;
260 decode_info->indexed = 0;
261 decode_info->reverse_bgr = 0;
262 decode_info->invert_grayscale = 0;
263 decode_info->tiled = 0;
265 ret = pTIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
268 WARN("missing PhotometricInterpretation tag\n");
272 ret = pTIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bps);
274 decode_info->bps = bps;
276 ret = pTIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &samples);
277 if (!ret) samples = 1;
278 decode_info->samples = samples;
284 ret = pTIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planar);
285 if (!ret) planar = 1;
288 FIXME("unhandled planar configuration %u\n", planar);
292 decode_info->planar = planar;
296 case 0: /* WhiteIsZero */
297 decode_info->invert_grayscale = 1;
298 case 1: /* BlackIsZero */
301 FIXME("unhandled grayscale sample count %u\n", samples);
305 decode_info->bpp = bps;
309 decode_info->format = &GUID_WICPixelFormatBlackWhite;
312 decode_info->format = &GUID_WICPixelFormat4bppGray;
315 decode_info->format = &GUID_WICPixelFormat8bppGray;
318 FIXME("unhandled greyscale bit count %u\n", bps);
323 decode_info->bpp = bps * samples;
327 ret = pTIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extra_sample_count, &extra_samples);
330 extra_sample_count = 1;
332 extra_samples = &extra_sample;
335 else if (samples != 3)
337 FIXME("unhandled RGB sample count %u\n", samples);
344 decode_info->reverse_bgr = 1;
346 decode_info->format = &GUID_WICPixelFormat24bppBGR;
348 switch(extra_samples[0])
350 case 0: /* Unspecified data */
351 decode_info->format = &GUID_WICPixelFormat32bppBGR;
353 case 1: /* Associated (pre-multiplied) alpha data */
354 decode_info->format = &GUID_WICPixelFormat32bppPBGRA;
356 case 2: /* Unassociated alpha data */
357 decode_info->format = &GUID_WICPixelFormat32bppBGRA;
360 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
366 decode_info->format = &GUID_WICPixelFormat48bppRGB;
368 switch(extra_samples[0])
370 case 0: /* Unspecified data */
371 /* decode_info->format = &GUID_WICPixelFormat64bppRGB; */
372 FIXME("64-bit RGB is unsupported\n");
374 case 1: /* Associated (pre-multiplied) alpha data */
375 decode_info->format = &GUID_WICPixelFormat64bppPRGBA;
377 case 2: /* Unassociated alpha data */
378 decode_info->format = &GUID_WICPixelFormat64bppRGBA;
381 FIXME("unhandled extra sample type %i\n", extra_samples[0]);
386 FIXME("unhandled RGB bit count %u\n", bps);
390 case 3: /* RGB Palette */
393 FIXME("unhandled indexed sample count %u\n", samples);
397 decode_info->indexed = 1;
398 decode_info->bpp = bps;
402 decode_info->format = &GUID_WICPixelFormat4bppIndexed;
405 decode_info->format = &GUID_WICPixelFormat8bppIndexed;
408 FIXME("unhandled indexed bit count %u\n", bps);
412 case 4: /* Transparency mask */
417 FIXME("unhandled PhotometricInterpretation %u\n", photometric);
421 ret = pTIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &decode_info->width);
424 WARN("missing image width\n");
428 ret = pTIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &decode_info->height);
431 WARN("missing image length\n");
435 if ((ret = pTIFFGetField(tiff, TIFFTAG_TILEWIDTH, &decode_info->tile_width)))
437 decode_info->tiled = 1;
441 WARN("missing tile width\n");
445 ret = pTIFFGetField(tiff, TIFFTAG_TILELENGTH, &decode_info->tile_height);
448 WARN("missing tile height\n");
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;
454 decode_info->tiles_across = (decode_info->width + decode_info->tile_width - 1) / decode_info->tile_width;
456 else if ((ret = pTIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &decode_info->tile_height)))
458 if (decode_info->tile_height > decode_info->height)
459 decode_info->tile_height = decode_info->height;
460 decode_info->tile_width = decode_info->width;
461 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
462 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
466 /* Some broken TIFF files have a single strip and lack the RowsPerStrip tag */
467 decode_info->tile_height = decode_info->height;
468 decode_info->tile_width = decode_info->width;
469 decode_info->tile_stride = ((decode_info->bpp * decode_info->tile_width + 7)/8);
470 decode_info->tile_size = decode_info->tile_height * decode_info->tile_stride;
473 decode_info->resolution_unit = 0;
474 pTIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &decode_info->resolution_unit);
475 if (decode_info->resolution_unit != 0)
477 ret = pTIFFGetField(tiff, TIFFTAG_XRESOLUTION, &decode_info->xres);
480 WARN("missing X resolution\n");
481 decode_info->resolution_unit = 0;
484 ret = pTIFFGetField(tiff, TIFFTAG_YRESOLUTION, &decode_info->yres);
487 WARN("missing Y resolution\n");
488 decode_info->resolution_unit = 0;
495 static HRESULT WINAPI TiffDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
498 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
499 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
501 if (!ppv) return E_INVALIDARG;
503 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
510 return E_NOINTERFACE;
513 IUnknown_AddRef((IUnknown*)*ppv);
517 static ULONG WINAPI TiffDecoder_AddRef(IWICBitmapDecoder *iface)
519 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
520 ULONG ref = InterlockedIncrement(&This->ref);
522 TRACE("(%p) refcount=%u\n", iface, ref);
527 static ULONG WINAPI TiffDecoder_Release(IWICBitmapDecoder *iface)
529 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
530 ULONG ref = InterlockedDecrement(&This->ref);
532 TRACE("(%p) refcount=%u\n", iface, ref);
536 if (This->tiff) pTIFFClose(This->tiff);
537 if (This->stream) IStream_Release(This->stream);
538 This->lock.DebugInfo->Spare[0] = 0;
539 DeleteCriticalSection(&This->lock);
540 HeapFree(GetProcessHeap(), 0, This);
546 static HRESULT WINAPI TiffDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
547 DWORD *pdwCapability)
549 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
553 static HRESULT WINAPI TiffDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
554 WICDecodeOptions cacheOptions)
556 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
560 TRACE("(%p,%p,%x): stub\n", iface, pIStream, cacheOptions);
562 EnterCriticalSection(&This->lock);
564 if (This->initialized)
566 hr = WINCODEC_ERR_WRONGSTATE;
570 tiff = tiff_open_stream(pIStream, "r");
579 This->stream = pIStream;
580 IStream_AddRef(pIStream);
581 This->initialized = TRUE;
584 LeaveCriticalSection(&This->lock);
588 static HRESULT WINAPI TiffDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
589 GUID *pguidContainerFormat)
591 memcpy(pguidContainerFormat, &GUID_ContainerFormatTiff, sizeof(GUID));
595 static HRESULT WINAPI TiffDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
596 IWICBitmapDecoderInfo **ppIDecoderInfo)
598 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
602 static HRESULT WINAPI TiffDecoder_CopyPalette(IWICBitmapDecoder *iface,
603 IWICPalette *pIPalette)
605 FIXME("(%p,%p): stub\n", iface, pIPalette);
609 static HRESULT WINAPI TiffDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
610 IWICMetadataQueryReader **ppIMetadataQueryReader)
612 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
616 static HRESULT WINAPI TiffDecoder_GetPreview(IWICBitmapDecoder *iface,
617 IWICBitmapSource **ppIBitmapSource)
619 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
623 static HRESULT WINAPI TiffDecoder_GetColorContexts(IWICBitmapDecoder *iface,
624 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
626 FIXME("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
630 static HRESULT WINAPI TiffDecoder_GetThumbnail(IWICBitmapDecoder *iface,
631 IWICBitmapSource **ppIThumbnail)
633 TRACE("(%p,%p)\n", iface, ppIThumbnail);
634 return WINCODEC_ERR_CODECNOTHUMBNAIL;
637 static HRESULT WINAPI TiffDecoder_GetFrameCount(IWICBitmapDecoder *iface,
640 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
644 WARN("(%p) <-- WINCODEC_ERR_WRONGSTATE\n", iface);
645 return WINCODEC_ERR_WRONGSTATE;
648 EnterCriticalSection(&This->lock);
649 while (pTIFFReadDirectory(This->tiff)) { }
650 *pCount = pTIFFCurrentDirectory(This->tiff)+1;
651 LeaveCriticalSection(&This->lock);
653 TRACE("(%p) <-- %i\n", iface, *pCount);
658 static HRESULT WINAPI TiffDecoder_GetFrame(IWICBitmapDecoder *iface,
659 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
661 TiffDecoder *This = impl_from_IWICBitmapDecoder(iface);
662 TiffFrameDecode *result;
664 tiff_decode_info decode_info;
667 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
670 return WINCODEC_ERR_WRONGSTATE;
672 EnterCriticalSection(&This->lock);
673 res = pTIFFSetDirectory(This->tiff, index);
674 if (!res) hr = E_INVALIDARG;
675 else hr = tiff_get_decode_info(This->tiff, &decode_info);
676 LeaveCriticalSection(&This->lock);
680 result = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffFrameDecode));
684 result->IWICBitmapFrameDecode_iface.lpVtbl = &TiffFrameDecode_Vtbl;
686 result->parent = This;
687 result->index = index;
688 result->decode_info = decode_info;
689 result->cached_tile_x = -1;
690 result->cached_tile = HeapAlloc(GetProcessHeap(), 0, decode_info.tile_size);
692 if (result->cached_tile)
693 *ppIBitmapFrame = (IWICBitmapFrameDecode*)result;
697 HeapFree(GetProcessHeap(), 0, result);
700 else hr = E_OUTOFMEMORY;
703 if (FAILED(hr)) *ppIBitmapFrame = NULL;
708 static const IWICBitmapDecoderVtbl TiffDecoder_Vtbl = {
709 TiffDecoder_QueryInterface,
712 TiffDecoder_QueryCapability,
713 TiffDecoder_Initialize,
714 TiffDecoder_GetContainerFormat,
715 TiffDecoder_GetDecoderInfo,
716 TiffDecoder_CopyPalette,
717 TiffDecoder_GetMetadataQueryReader,
718 TiffDecoder_GetPreview,
719 TiffDecoder_GetColorContexts,
720 TiffDecoder_GetThumbnail,
721 TiffDecoder_GetFrameCount,
725 static HRESULT WINAPI TiffFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
728 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
729 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
731 if (!ppv) return E_INVALIDARG;
733 if (IsEqualIID(&IID_IUnknown, iid) ||
734 IsEqualIID(&IID_IWICBitmapSource, iid) ||
735 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
742 return E_NOINTERFACE;
745 IUnknown_AddRef((IUnknown*)*ppv);
749 static ULONG WINAPI TiffFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
751 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
752 ULONG ref = InterlockedIncrement(&This->ref);
754 TRACE("(%p) refcount=%u\n", iface, ref);
759 static ULONG WINAPI TiffFrameDecode_Release(IWICBitmapFrameDecode *iface)
761 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
762 ULONG ref = InterlockedDecrement(&This->ref);
764 TRACE("(%p) refcount=%u\n", iface, ref);
768 HeapFree(GetProcessHeap(), 0, This->cached_tile);
769 HeapFree(GetProcessHeap(), 0, This);
775 static HRESULT WINAPI TiffFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
776 UINT *puiWidth, UINT *puiHeight)
778 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
780 *puiWidth = This->decode_info.width;
781 *puiHeight = This->decode_info.height;
783 TRACE("(%p) <-- %ux%u\n", iface, *puiWidth, *puiHeight);
788 static HRESULT WINAPI TiffFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
789 WICPixelFormatGUID *pPixelFormat)
791 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
793 memcpy(pPixelFormat, This->decode_info.format, sizeof(GUID));
795 TRACE("(%p) <-- %s\n", This, debugstr_guid(This->decode_info.format));
800 static HRESULT WINAPI TiffFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
801 double *pDpiX, double *pDpiY)
803 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
805 switch (This->decode_info.resolution_unit)
808 FIXME("unknown resolution unit %i\n", This->decode_info.resolution_unit);
810 case 0: /* Not set */
811 *pDpiX = *pDpiY = 96.0;
813 case 1: /* Relative measurements */
815 *pDpiY = 96.0 * This->decode_info.yres / This->decode_info.xres;
818 *pDpiX = This->decode_info.xres;
819 *pDpiY = This->decode_info.yres;
821 case 3: /* Centimeter */
822 *pDpiX = This->decode_info.xres / 2.54;
823 *pDpiY = This->decode_info.yres / 2.54;
827 TRACE("(%p) <-- %f,%f unit=%i\n", iface, *pDpiX, *pDpiY, This->decode_info.resolution_unit);
832 static HRESULT WINAPI TiffFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
833 IWICPalette *pIPalette)
835 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
836 uint16 *red, *green, *blue;
837 WICColor colors[256];
838 int color_count, ret, i;
840 TRACE("(%p,%p)\n", iface, pIPalette);
842 color_count = 1<<This->decode_info.bps;
844 EnterCriticalSection(&This->parent->lock);
845 ret = pTIFFGetField(This->parent->tiff, TIFFTAG_COLORMAP, &red, &green, &blue);
846 LeaveCriticalSection(&This->parent->lock);
850 WARN("Couldn't read color map\n");
854 for (i=0; i<color_count; i++)
856 colors[i] = 0xff000000 |
857 ((red[i]<<8) & 0xff0000) |
858 (green[i] & 0xff00) |
859 ((blue[i]>>8) & 0xff);
862 return IWICPalette_InitializeCustom(pIPalette, colors, color_count);
865 static HRESULT TiffFrameDecode_ReadTile(TiffFrameDecode *This, UINT tile_x, UINT tile_y)
871 swap_bytes = pTIFFIsByteSwapped(This->parent->tiff);
873 ret = pTIFFSetDirectory(This->parent->tiff, This->index);
880 if (This->decode_info.tiled)
882 ret = pTIFFReadEncodedTile(This->parent->tiff, tile_x + tile_y * This->decode_info.tiles_across, This->cached_tile, This->decode_info.tile_size);
886 ret = pTIFFReadEncodedStrip(This->parent->tiff, tile_y, This->cached_tile, This->decode_info.tile_size);
893 if (hr == S_OK && This->decode_info.reverse_bgr)
895 if (This->decode_info.bps == 8)
897 UINT sample_count = This->decode_info.samples;
899 reverse_bgr8(sample_count, This->cached_tile, This->decode_info.tile_width,
900 This->decode_info.tile_height, This->decode_info.tile_width * sample_count);
904 if (hr == S_OK && swap_bytes && This->decode_info.bps > 8)
906 UINT row, i, samples_per_row;
909 samples_per_row = This->decode_info.tile_width * This->decode_info.samples;
911 switch(This->decode_info.bps)
914 for (row=0; row<This->decode_info.tile_height; row++)
916 sample = This->cached_tile + row * This->decode_info.tile_stride;
917 for (i=0; i<samples_per_row; i++)
920 sample[1] = sample[0];
927 ERR("unhandled bps for byte swap %u\n", This->decode_info.bps);
932 if (hr == S_OK && This->decode_info.invert_grayscale)
936 if (This->decode_info.samples != 1)
938 ERR("cannot invert grayscale image with %u samples\n", This->decode_info.samples);
942 end = This->cached_tile+This->decode_info.tile_size;
944 for (byte = This->cached_tile; byte != end; byte++)
950 This->cached_tile_x = tile_x;
951 This->cached_tile_y = tile_y;
957 static HRESULT WINAPI TiffFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
958 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
960 TiffFrameDecode *This = impl_from_IWICBitmapFrameDecode(iface);
961 UINT min_tile_x, max_tile_x, min_tile_y, max_tile_y;
969 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
975 rect.Width = This->decode_info.width;
976 rect.Height = This->decode_info.height;
981 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->decode_info.width ||
982 prc->Y+prc->Height > This->decode_info.height)
986 bytesperrow = ((This->decode_info.bpp * prc->Width)+7)/8;
988 if (cbStride < bytesperrow)
991 if ((cbStride * prc->Height) > cbBufferSize)
994 min_tile_x = prc->X / This->decode_info.tile_width;
995 min_tile_y = prc->Y / This->decode_info.tile_height;
996 max_tile_x = (prc->X+prc->Width-1) / This->decode_info.tile_width;
997 max_tile_y = (prc->Y+prc->Height-1) / This->decode_info.tile_height;
999 EnterCriticalSection(&This->parent->lock);
1001 for (tile_x=min_tile_x; tile_x <= max_tile_x; tile_x++)
1003 for (tile_y=min_tile_y; tile_y <= max_tile_y; tile_y++)
1005 if (tile_x != This->cached_tile_x || tile_y != This->cached_tile_y)
1007 hr = TiffFrameDecode_ReadTile(This, tile_x, tile_y);
1012 if (prc->X < tile_x * This->decode_info.tile_width)
1015 rc.X = prc->X - tile_x * This->decode_info.tile_width;
1017 if (prc->Y < tile_y * This->decode_info.tile_height)
1020 rc.Y = prc->Y - tile_y * This->decode_info.tile_height;
1022 if (prc->X+prc->Width > (tile_x+1) * This->decode_info.tile_width)
1023 rc.Width = This->decode_info.tile_width - rc.X;
1024 else if (prc->X < tile_x * This->decode_info.tile_width)
1025 rc.Width = prc->Width + prc->X - tile_x * This->decode_info.tile_width;
1027 rc.Width = prc->Width;
1029 if (prc->Y+prc->Height > (tile_y+1) * This->decode_info.tile_height)
1030 rc.Height = This->decode_info.tile_height - rc.Y;
1031 else if (prc->Y < tile_y * This->decode_info.tile_height)
1032 rc.Height = prc->Height + prc->Y - tile_y * This->decode_info.tile_height;
1034 rc.Height = prc->Height;
1036 dst_tilepos = pbBuffer + (cbStride * ((rc.Y + tile_y * This->decode_info.tile_height) - prc->Y)) +
1037 ((This->decode_info.bpp * ((rc.X + tile_x * This->decode_info.tile_width) - prc->X) + 7) / 8);
1039 hr = copy_pixels(This->decode_info.bpp, This->cached_tile,
1040 This->decode_info.tile_width, This->decode_info.tile_height, This->decode_info.tile_stride,
1041 &rc, cbStride, cbBufferSize, dst_tilepos);
1046 LeaveCriticalSection(&This->parent->lock);
1047 TRACE("<-- 0x%x\n", hr);
1053 LeaveCriticalSection(&This->parent->lock);
1058 static HRESULT WINAPI TiffFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
1059 IWICMetadataQueryReader **ppIMetadataQueryReader)
1061 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
1065 static HRESULT WINAPI TiffFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
1066 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1068 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
1072 static HRESULT WINAPI TiffFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
1073 IWICBitmapSource **ppIThumbnail)
1075 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
1079 static const IWICBitmapFrameDecodeVtbl TiffFrameDecode_Vtbl = {
1080 TiffFrameDecode_QueryInterface,
1081 TiffFrameDecode_AddRef,
1082 TiffFrameDecode_Release,
1083 TiffFrameDecode_GetSize,
1084 TiffFrameDecode_GetPixelFormat,
1085 TiffFrameDecode_GetResolution,
1086 TiffFrameDecode_CopyPalette,
1087 TiffFrameDecode_CopyPixels,
1088 TiffFrameDecode_GetMetadataQueryReader,
1089 TiffFrameDecode_GetColorContexts,
1090 TiffFrameDecode_GetThumbnail
1093 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1098 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1102 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1104 if (!load_libtiff())
1106 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF);
1110 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffDecoder));
1111 if (!This) return E_OUTOFMEMORY;
1113 This->IWICBitmapDecoder_iface.lpVtbl = &TiffDecoder_Vtbl;
1115 This->stream = NULL;
1116 InitializeCriticalSection(&This->lock);
1117 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffDecoder.lock");
1119 This->initialized = FALSE;
1121 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1122 IUnknown_Release((IUnknown*)This);
1127 struct tiff_encode_format {
1128 const WICPixelFormatGUID *guid;
1134 int extra_sample_type;
1138 static const struct tiff_encode_format formats[] = {
1139 {&GUID_WICPixelFormat24bppBGR, 2, 8, 3, 24, 0, 0, 1},
1140 {&GUID_WICPixelFormatBlackWhite, 1, 1, 1, 1, 0, 0, 0},
1141 {&GUID_WICPixelFormat4bppGray, 1, 4, 1, 4, 0, 0, 0},
1142 {&GUID_WICPixelFormat8bppGray, 1, 8, 1, 8, 0, 0, 0},
1143 {&GUID_WICPixelFormat32bppBGRA, 2, 8, 4, 32, 1, 2, 1},
1144 {&GUID_WICPixelFormat32bppPBGRA, 2, 8, 4, 32, 1, 1, 1},
1145 {&GUID_WICPixelFormat48bppRGB, 2, 16, 3, 48, 0, 0, 0},
1146 {&GUID_WICPixelFormat64bppRGBA, 2, 16, 4, 64, 1, 2, 0},
1147 {&GUID_WICPixelFormat64bppPRGBA, 2, 16, 4, 64, 1, 1, 0},
1151 typedef struct TiffEncoder {
1152 IWICBitmapEncoder IWICBitmapEncoder_iface;
1155 CRITICAL_SECTION lock; /* Must be held when tiff is used or fields below are set */
1160 ULONG num_frames_committed;
1163 static inline TiffEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
1165 return CONTAINING_RECORD(iface, TiffEncoder, IWICBitmapEncoder_iface);
1168 typedef struct TiffFrameEncode {
1169 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
1171 TiffEncoder *parent;
1172 /* fields below are protected by parent->lock */
1176 const struct tiff_encode_format *format;
1182 static inline TiffFrameEncode *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
1184 return CONTAINING_RECORD(iface, TiffFrameEncode, IWICBitmapFrameEncode_iface);
1187 static HRESULT WINAPI TiffFrameEncode_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
1190 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1191 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1193 if (!ppv) return E_INVALIDARG;
1195 if (IsEqualIID(&IID_IUnknown, iid) ||
1196 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
1198 *ppv = &This->IWICBitmapFrameEncode_iface;
1203 return E_NOINTERFACE;
1206 IUnknown_AddRef((IUnknown*)*ppv);
1210 static ULONG WINAPI TiffFrameEncode_AddRef(IWICBitmapFrameEncode *iface)
1212 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1213 ULONG ref = InterlockedIncrement(&This->ref);
1215 TRACE("(%p) refcount=%u\n", iface, ref);
1220 static ULONG WINAPI TiffFrameEncode_Release(IWICBitmapFrameEncode *iface)
1222 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1223 ULONG ref = InterlockedDecrement(&This->ref);
1225 TRACE("(%p) refcount=%u\n", iface, ref);
1229 IWICBitmapEncoder_Release(&This->parent->IWICBitmapEncoder_iface);
1230 HeapFree(GetProcessHeap(), 0, This);
1236 static HRESULT WINAPI TiffFrameEncode_Initialize(IWICBitmapFrameEncode *iface,
1237 IPropertyBag2 *pIEncoderOptions)
1239 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1240 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
1242 EnterCriticalSection(&This->parent->lock);
1244 if (This->initialized)
1246 LeaveCriticalSection(&This->parent->lock);
1247 return WINCODEC_ERR_WRONGSTATE;
1250 This->initialized = TRUE;
1252 LeaveCriticalSection(&This->parent->lock);
1257 static HRESULT WINAPI TiffFrameEncode_SetSize(IWICBitmapFrameEncode *iface,
1258 UINT uiWidth, UINT uiHeight)
1260 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1261 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
1263 EnterCriticalSection(&This->parent->lock);
1265 if (!This->initialized || This->info_written)
1267 LeaveCriticalSection(&This->parent->lock);
1268 return WINCODEC_ERR_WRONGSTATE;
1271 This->width = uiWidth;
1272 This->height = uiHeight;
1274 LeaveCriticalSection(&This->parent->lock);
1279 static HRESULT WINAPI TiffFrameEncode_SetResolution(IWICBitmapFrameEncode *iface,
1280 double dpiX, double dpiY)
1282 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1283 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
1285 EnterCriticalSection(&This->parent->lock);
1287 if (!This->initialized || This->info_written)
1289 LeaveCriticalSection(&This->parent->lock);
1290 return WINCODEC_ERR_WRONGSTATE;
1296 LeaveCriticalSection(&This->parent->lock);
1301 static HRESULT WINAPI TiffFrameEncode_SetPixelFormat(IWICBitmapFrameEncode *iface,
1302 WICPixelFormatGUID *pPixelFormat)
1304 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1307 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
1309 EnterCriticalSection(&This->parent->lock);
1311 if (!This->initialized || This->info_written)
1313 LeaveCriticalSection(&This->parent->lock);
1314 return WINCODEC_ERR_WRONGSTATE;
1317 for (i=0; formats[i].guid; i++)
1319 if (memcmp(formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
1323 if (!formats[i].guid) i = 0;
1325 This->format = &formats[i];
1326 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
1328 LeaveCriticalSection(&This->parent->lock);
1333 static HRESULT WINAPI TiffFrameEncode_SetColorContexts(IWICBitmapFrameEncode *iface,
1334 UINT cCount, IWICColorContext **ppIColorContext)
1336 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1340 static HRESULT WINAPI TiffFrameEncode_SetPalette(IWICBitmapFrameEncode *iface,
1341 IWICPalette *pIPalette)
1343 FIXME("(%p,%p): stub\n", iface, pIPalette);
1344 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1347 static HRESULT WINAPI TiffFrameEncode_SetThumbnail(IWICBitmapFrameEncode *iface,
1348 IWICBitmapSource *pIThumbnail)
1350 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
1351 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1354 static HRESULT WINAPI TiffFrameEncode_WritePixels(IWICBitmapFrameEncode *iface,
1355 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
1357 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1358 BYTE *row_data, *swapped_data = NULL;
1359 UINT i, j, line_size;
1361 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
1363 EnterCriticalSection(&This->parent->lock);
1365 if (!This->initialized || !This->width || !This->height || !This->format)
1367 LeaveCriticalSection(&This->parent->lock);
1368 return WINCODEC_ERR_WRONGSTATE;
1371 if (lineCount == 0 || lineCount + This->lines_written > This->height)
1373 LeaveCriticalSection(&This->parent->lock);
1374 return E_INVALIDARG;
1377 line_size = ((This->width * This->format->bpp)+7)/8;
1379 if (This->format->reverse_bgr)
1381 swapped_data = HeapAlloc(GetProcessHeap(), 0, line_size);
1384 LeaveCriticalSection(&This->parent->lock);
1385 return E_OUTOFMEMORY;
1389 if (!This->info_written)
1391 pTIFFSetField(This->parent->tiff, TIFFTAG_PHOTOMETRIC, (uint16)This->format->photometric);
1392 pTIFFSetField(This->parent->tiff, TIFFTAG_PLANARCONFIG, (uint16)1);
1393 pTIFFSetField(This->parent->tiff, TIFFTAG_BITSPERSAMPLE, (uint16)This->format->bps);
1394 pTIFFSetField(This->parent->tiff, TIFFTAG_SAMPLESPERPIXEL, (uint16)This->format->samples);
1396 if (This->format->extra_sample)
1398 uint16 extra_samples;
1399 extra_samples = This->format->extra_sample_type;
1401 pTIFFSetField(This->parent->tiff, TIFFTAG_EXTRASAMPLES, (uint16)1, &extra_samples);
1404 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGEWIDTH, (uint32)This->width);
1405 pTIFFSetField(This->parent->tiff, TIFFTAG_IMAGELENGTH, (uint32)This->height);
1407 if (This->xres != 0.0 && This->yres != 0.0)
1409 pTIFFSetField(This->parent->tiff, TIFFTAG_RESOLUTIONUNIT, (uint16)2); /* Inch */
1410 pTIFFSetField(This->parent->tiff, TIFFTAG_XRESOLUTION, (float)This->xres);
1411 pTIFFSetField(This->parent->tiff, TIFFTAG_YRESOLUTION, (float)This->yres);
1414 This->info_written = TRUE;
1417 for (i=0; i<lineCount; i++)
1419 row_data = pbPixels + i * cbStride;
1421 if (This->format->reverse_bgr && This->format->bps == 8)
1423 memcpy(swapped_data, row_data, line_size);
1424 for (j=0; j<line_size; j += This->format->samples)
1427 temp = swapped_data[j];
1428 swapped_data[j] = swapped_data[j+2];
1429 swapped_data[j+2] = temp;
1431 row_data = swapped_data;
1434 pTIFFWriteScanline(This->parent->tiff, (tdata_t)row_data, i+This->lines_written, 0);
1437 This->lines_written += lineCount;
1439 LeaveCriticalSection(&This->parent->lock);
1441 HeapFree(GetProcessHeap(), 0, swapped_data);
1446 static HRESULT WINAPI TiffFrameEncode_WriteSource(IWICBitmapFrameEncode *iface,
1447 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1449 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1452 WICPixelFormatGUID guid;
1456 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1458 if (!This->initialized || !This->width || !This->height)
1459 return WINCODEC_ERR_WRONGSTATE;
1463 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1464 if (FAILED(hr)) return hr;
1465 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1466 if (FAILED(hr)) return hr;
1469 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1470 if (FAILED(hr)) return hr;
1471 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1473 /* FIXME: should use WICConvertBitmapSource to convert */
1474 ERR("format %s unsupported\n", debugstr_guid(&guid));
1478 if (This->xres == 0.0 || This->yres == 0.0)
1481 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1482 if (FAILED(hr)) return hr;
1483 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1484 if (FAILED(hr)) return hr;
1490 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1491 if (FAILED(hr)) return hr;
1499 if (prc->Width != This->width) return E_INVALIDARG;
1501 stride = (This->format->bpp * This->width + 7)/8;
1503 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1504 if (!pixeldata) return E_OUTOFMEMORY;
1506 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1507 stride*prc->Height, pixeldata);
1511 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1512 stride*prc->Height, pixeldata);
1515 HeapFree(GetProcessHeap(), 0, pixeldata);
1520 static HRESULT WINAPI TiffFrameEncode_Commit(IWICBitmapFrameEncode *iface)
1522 TiffFrameEncode *This = impl_from_IWICBitmapFrameEncode(iface);
1524 TRACE("(%p)\n", iface);
1526 EnterCriticalSection(&This->parent->lock);
1528 if (!This->info_written || This->lines_written != This->height || This->committed)
1530 LeaveCriticalSection(&This->parent->lock);
1531 return WINCODEC_ERR_WRONGSTATE;
1534 /* libtiff will commit the data when creating a new frame or closing the file */
1536 This->committed = TRUE;
1537 This->parent->num_frames_committed++;
1539 LeaveCriticalSection(&This->parent->lock);
1544 static HRESULT WINAPI TiffFrameEncode_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1545 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1547 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1551 static const IWICBitmapFrameEncodeVtbl TiffFrameEncode_Vtbl = {
1552 TiffFrameEncode_QueryInterface,
1553 TiffFrameEncode_AddRef,
1554 TiffFrameEncode_Release,
1555 TiffFrameEncode_Initialize,
1556 TiffFrameEncode_SetSize,
1557 TiffFrameEncode_SetResolution,
1558 TiffFrameEncode_SetPixelFormat,
1559 TiffFrameEncode_SetColorContexts,
1560 TiffFrameEncode_SetPalette,
1561 TiffFrameEncode_SetThumbnail,
1562 TiffFrameEncode_WritePixels,
1563 TiffFrameEncode_WriteSource,
1564 TiffFrameEncode_Commit,
1565 TiffFrameEncode_GetMetadataQueryWriter
1568 static HRESULT WINAPI TiffEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1571 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1572 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1574 if (!ppv) return E_INVALIDARG;
1576 if (IsEqualIID(&IID_IUnknown, iid) ||
1577 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1584 return E_NOINTERFACE;
1587 IUnknown_AddRef((IUnknown*)*ppv);
1591 static ULONG WINAPI TiffEncoder_AddRef(IWICBitmapEncoder *iface)
1593 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1594 ULONG ref = InterlockedIncrement(&This->ref);
1596 TRACE("(%p) refcount=%u\n", iface, ref);
1601 static ULONG WINAPI TiffEncoder_Release(IWICBitmapEncoder *iface)
1603 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1604 ULONG ref = InterlockedDecrement(&This->ref);
1606 TRACE("(%p) refcount=%u\n", iface, ref);
1610 if (This->tiff) pTIFFClose(This->tiff);
1611 if (This->stream) IStream_Release(This->stream);
1612 This->lock.DebugInfo->Spare[0] = 0;
1613 DeleteCriticalSection(&This->lock);
1614 HeapFree(GetProcessHeap(), 0, This);
1620 static HRESULT WINAPI TiffEncoder_Initialize(IWICBitmapEncoder *iface,
1621 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1623 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1627 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1629 EnterCriticalSection(&This->lock);
1631 if (This->initialized || This->committed)
1633 hr = WINCODEC_ERR_WRONGSTATE;
1637 tiff = tiff_open_stream(pIStream, "w");
1646 This->stream = pIStream;
1647 IStream_AddRef(pIStream);
1648 This->initialized = TRUE;
1651 LeaveCriticalSection(&This->lock);
1655 static HRESULT WINAPI TiffEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1656 GUID *pguidContainerFormat)
1658 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1662 static HRESULT WINAPI TiffEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1663 IWICBitmapEncoderInfo **ppIEncoderInfo)
1665 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1669 static HRESULT WINAPI TiffEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1670 UINT cCount, IWICColorContext **ppIColorContext)
1672 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1676 static HRESULT WINAPI TiffEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1678 TRACE("(%p,%p)\n", iface, pIPalette);
1679 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1682 static HRESULT WINAPI TiffEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1684 TRACE("(%p,%p)\n", iface, pIThumbnail);
1685 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1688 static HRESULT WINAPI TiffEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1690 TRACE("(%p,%p)\n", iface, pIPreview);
1691 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1694 static HRESULT WINAPI TiffEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1695 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1697 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1698 TiffFrameEncode *result;
1702 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1704 EnterCriticalSection(&This->lock);
1706 if (!This->initialized || This->committed)
1708 hr = WINCODEC_ERR_WRONGSTATE;
1710 else if (This->num_frames != This->num_frames_committed)
1712 FIXME("New frame created before previous frame was committed\n");
1718 hr = CreatePropertyBag2(ppIEncoderOptions);
1723 result = HeapAlloc(GetProcessHeap(), 0, sizeof(*result));
1727 result->IWICBitmapFrameEncode_iface.lpVtbl = &TiffFrameEncode_Vtbl;
1729 result->parent = This;
1730 result->initialized = FALSE;
1731 result->info_written = FALSE;
1732 result->committed = FALSE;
1733 result->format = NULL;
1738 result->lines_written = 0;
1740 IWICBitmapEncoder_AddRef(iface);
1741 *ppIFrameEncode = &result->IWICBitmapFrameEncode_iface;
1743 if (This->num_frames != 0)
1744 pTIFFWriteDirectory(This->tiff);
1753 IPropertyBag2_Release(*ppIEncoderOptions);
1754 *ppIEncoderOptions = NULL;
1758 LeaveCriticalSection(&This->lock);
1763 static HRESULT WINAPI TiffEncoder_Commit(IWICBitmapEncoder *iface)
1765 TiffEncoder *This = impl_from_IWICBitmapEncoder(iface);
1767 TRACE("(%p)\n", iface);
1769 EnterCriticalSection(&This->lock);
1771 if (!This->initialized || This->committed)
1773 LeaveCriticalSection(&This->lock);
1774 return WINCODEC_ERR_WRONGSTATE;
1777 pTIFFClose(This->tiff);
1778 IStream_Release(This->stream);
1779 This->stream = NULL;
1782 This->committed = TRUE;
1784 LeaveCriticalSection(&This->lock);
1789 static HRESULT WINAPI TiffEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1790 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1792 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1796 static const IWICBitmapEncoderVtbl TiffEncoder_Vtbl = {
1797 TiffEncoder_QueryInterface,
1799 TiffEncoder_Release,
1800 TiffEncoder_Initialize,
1801 TiffEncoder_GetContainerFormat,
1802 TiffEncoder_GetEncoderInfo,
1803 TiffEncoder_SetColorContexts,
1804 TiffEncoder_SetPalette,
1805 TiffEncoder_SetThumbnail,
1806 TiffEncoder_SetPreview,
1807 TiffEncoder_CreateNewFrame,
1809 TiffEncoder_GetMetadataQueryWriter
1812 HRESULT TiffEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1817 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1821 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1823 if (!load_libtiff())
1825 ERR("Failed writing TIFF because unable to load %s\n",SONAME_LIBTIFF);
1829 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TiffEncoder));
1830 if (!This) return E_OUTOFMEMORY;
1832 This->IWICBitmapEncoder_iface.lpVtbl = &TiffEncoder_Vtbl;
1834 This->stream = NULL;
1835 InitializeCriticalSection(&This->lock);
1836 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TiffEncoder.lock");
1838 This->initialized = FALSE;
1839 This->num_frames = 0;
1840 This->num_frames_committed = 0;
1841 This->committed = FALSE;
1843 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1844 IUnknown_Release((IUnknown*)This);
1849 #else /* !SONAME_LIBTIFF */
1851 HRESULT TiffDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1853 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
1857 HRESULT TiffEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1859 ERR("Trying to save TIFF picture, but Wine was compiled without TIFF support.\n");