2 * Copyright 2010 Vincent Povirk for CodeWeavers
3 * Copyright 2016 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
37 #define WIN32_NO_STATUS
43 #include "wincodecs_private.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
51 /* Workaround for broken libtiff 4.x headers on some 64-bit hosts which
52 * define TIFF_UINT64_T/toff_t as 32-bit for 32-bit builds, while they
53 * are supposed to be always 64-bit.
54 * TIFF_UINT64_T doesn't exist in libtiff 3.x, it was introduced in 4.x.
58 # define toff_t UINT64
61 static CRITICAL_SECTION init_tiff_cs
;
62 static CRITICAL_SECTION_DEBUG init_tiff_cs_debug
=
65 { &init_tiff_cs_debug
.ProcessLocksList
,
66 &init_tiff_cs_debug
.ProcessLocksList
},
67 0, 0, { (DWORD_PTR
)(__FILE__
": init_tiff_cs") }
69 static CRITICAL_SECTION init_tiff_cs
= { &init_tiff_cs_debug
, -1, 0, 0, 0, 0 };
71 static void *libtiff_handle
;
72 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
73 MAKE_FUNCPTR(TIFFClientOpen
);
74 MAKE_FUNCPTR(TIFFClose
);
75 MAKE_FUNCPTR(TIFFCurrentDirOffset
);
76 MAKE_FUNCPTR(TIFFGetField
);
77 MAKE_FUNCPTR(TIFFIsByteSwapped
);
78 MAKE_FUNCPTR(TIFFNumberOfDirectories
);
79 MAKE_FUNCPTR(TIFFReadDirectory
);
80 MAKE_FUNCPTR(TIFFReadEncodedStrip
);
81 MAKE_FUNCPTR(TIFFReadEncodedTile
);
82 MAKE_FUNCPTR(TIFFSetDirectory
);
83 MAKE_FUNCPTR(TIFFSetField
);
84 MAKE_FUNCPTR(TIFFWriteDirectory
);
85 MAKE_FUNCPTR(TIFFWriteScanline
);
88 static void *load_libtiff(void)
92 RtlEnterCriticalSection(&init_tiff_cs
);
94 if (!libtiff_handle
&&
95 (libtiff_handle
= dlopen(SONAME_LIBTIFF
, RTLD_NOW
)) != NULL
)
97 void * (*pTIFFSetWarningHandler
)(void *);
98 void * (*pTIFFSetWarningHandlerExt
)(void *);
100 #define LOAD_FUNCPTR(f) \
101 if((p##f = dlsym(libtiff_handle, #f)) == NULL) { \
102 ERR("failed to load symbol %s\n", #f); \
103 libtiff_handle = NULL; \
104 RtlLeaveCriticalSection(&init_tiff_cs); \
107 LOAD_FUNCPTR(TIFFClientOpen
);
108 LOAD_FUNCPTR(TIFFClose
);
109 LOAD_FUNCPTR(TIFFCurrentDirOffset
);
110 LOAD_FUNCPTR(TIFFGetField
);
111 LOAD_FUNCPTR(TIFFIsByteSwapped
);
112 LOAD_FUNCPTR(TIFFNumberOfDirectories
);
113 LOAD_FUNCPTR(TIFFReadDirectory
);
114 LOAD_FUNCPTR(TIFFReadEncodedStrip
);
115 LOAD_FUNCPTR(TIFFReadEncodedTile
);
116 LOAD_FUNCPTR(TIFFSetDirectory
);
117 LOAD_FUNCPTR(TIFFSetField
);
118 LOAD_FUNCPTR(TIFFWriteDirectory
);
119 LOAD_FUNCPTR(TIFFWriteScanline
);
122 if ((pTIFFSetWarningHandler
= dlsym(libtiff_handle
, "TIFFSetWarningHandler")))
123 pTIFFSetWarningHandler(NULL
);
124 if ((pTIFFSetWarningHandlerExt
= dlsym(libtiff_handle
, "TIFFSetWarningHandlerExt")))
125 pTIFFSetWarningHandlerExt(NULL
);
128 result
= libtiff_handle
;
130 RtlLeaveCriticalSection(&init_tiff_cs
);
134 static tsize_t
tiff_stream_read(thandle_t client_data
, tdata_t data
, tsize_t size
)
136 IStream
*stream
= (IStream
*)client_data
;
140 hr
= stream_read(stream
, data
, size
, &bytes_read
);
141 if (FAILED(hr
)) bytes_read
= 0;
145 static tsize_t
tiff_stream_write(thandle_t client_data
, tdata_t data
, tsize_t size
)
147 IStream
*stream
= (IStream
*)client_data
;
151 hr
= stream_write(stream
, data
, size
, &bytes_written
);
152 if (FAILED(hr
)) bytes_written
= 0;
153 return bytes_written
;
156 static toff_t
tiff_stream_seek(thandle_t client_data
, toff_t offset
, int whence
)
158 IStream
*stream
= (IStream
*)client_data
;
160 ULONGLONG new_position
;
166 origin
= STREAM_SEEK_SET
;
169 origin
= STREAM_SEEK_CUR
;
172 origin
= STREAM_SEEK_END
;
175 ERR("unknown whence value %i\n", whence
);
179 hr
= stream_seek(stream
, offset
, origin
, &new_position
);
180 if (SUCCEEDED(hr
)) return new_position
;
184 static int tiff_stream_close(thandle_t client_data
)
186 /* Caller is responsible for releasing the stream object. */
190 static toff_t
tiff_stream_size(thandle_t client_data
)
192 IStream
*stream
= (IStream
*)client_data
;
196 hr
= stream_getsize(stream
, &size
);
198 if (SUCCEEDED(hr
)) return size
;
202 static int tiff_stream_map(thandle_t client_data
, tdata_t
*addr
, toff_t
*size
)
204 /* Cannot mmap streams */
208 static void tiff_stream_unmap(thandle_t client_data
, tdata_t addr
, toff_t size
)
210 /* No need to ever do this, since we can't map things. */
213 static TIFF
* tiff_open_stream(IStream
*stream
, const char *mode
)
215 stream_seek(stream
, 0, STREAM_SEEK_SET
, NULL
);
217 return pTIFFClientOpen("<IStream object>", mode
, stream
, tiff_stream_read
,
218 tiff_stream_write
, (void *)tiff_stream_seek
, tiff_stream_close
,
219 (void *)tiff_stream_size
, (void *)tiff_stream_map
, (void *)tiff_stream_unmap
);
223 struct decoder_frame frame
;
230 int invert_grayscale
;
231 UINT tile_width
, tile_height
;
240 struct decoder decoder
;
245 tiff_decode_info cached_decode_info
;
246 INT cached_tile_x
, cached_tile_y
;
250 static inline struct tiff_decoder
*impl_from_decoder(struct decoder
* iface
)
252 return CONTAINING_RECORD(iface
, struct tiff_decoder
, decoder
);
255 static HRESULT
tiff_get_decode_info(TIFF
*tiff
, tiff_decode_info
*decode_info
)
257 uint16_t photometric
, bps
, samples
, planar
;
258 uint16_t extra_sample_count
, extra_sample
, *extra_samples
;
259 uint16_t *red
, *green
, *blue
;
260 UINT resolution_unit
;
261 float xres
=0.0, yres
=0.0;
266 decode_info
->indexed
= 0;
267 decode_info
->reverse_bgr
= 0;
268 decode_info
->invert_grayscale
= 0;
269 decode_info
->tiled
= 0;
270 decode_info
->source_bpp
= 0;
272 ret
= pTIFFGetField(tiff
, TIFFTAG_PHOTOMETRIC
, &photometric
);
275 WARN("missing PhotometricInterpretation tag\n");
279 ret
= pTIFFGetField(tiff
, TIFFTAG_BITSPERSAMPLE
, &bps
);
281 decode_info
->bps
= bps
;
283 ret
= pTIFFGetField(tiff
, TIFFTAG_SAMPLESPERPIXEL
, &samples
);
284 if (!ret
) samples
= 1;
285 decode_info
->samples
= samples
;
291 ret
= pTIFFGetField(tiff
, TIFFTAG_PLANARCONFIG
, &planar
);
292 if (!ret
) planar
= 1;
295 FIXME("unhandled planar configuration %u\n", planar
);
299 decode_info
->planar
= planar
;
301 TRACE("planar %u, photometric %u, samples %u, bps %u\n", planar
, photometric
, samples
, bps
);
305 case 0: /* WhiteIsZero */
306 decode_info
->invert_grayscale
= 1;
308 case 1: /* BlackIsZero */
311 ret
= pTIFFGetField(tiff
, TIFFTAG_EXTRASAMPLES
, &extra_sample_count
, &extra_samples
);
314 extra_sample_count
= 1;
316 extra_samples
= &extra_sample
;
319 else if (samples
!= 1)
321 FIXME("unhandled %dbpp sample count %u\n", bps
, samples
);
325 decode_info
->frame
.bpp
= bps
* samples
;
326 decode_info
->source_bpp
= decode_info
->frame
.bpp
;
332 FIXME("unhandled 1bpp sample count %u\n", samples
);
335 decode_info
->frame
.pixel_format
= GUID_WICPixelFormatBlackWhite
;
340 FIXME("unhandled 4bpp grayscale sample count %u\n", samples
);
343 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat4bppGray
;
347 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat8bppGray
;
350 decode_info
->frame
.bpp
= 32;
352 switch(extra_samples
[0])
354 case 1: /* Associated (pre-multiplied) alpha data */
355 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat32bppPBGRA
;
357 case 0: /* Unspecified data */
358 case 2: /* Unassociated alpha data */
359 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat32bppBGRA
;
362 FIXME("unhandled extra sample type %u\n", extra_samples
[0]);
370 FIXME("unhandled 16bpp grayscale sample count %u\n", samples
);
371 return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT
;
373 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat16bppGray
;
378 FIXME("unhandled 32bpp grayscale sample count %u\n", samples
);
379 return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT
;
381 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat32bppGrayFloat
;
384 WARN("unhandled greyscale bit count %u\n", bps
);
385 return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT
;
391 ret
= pTIFFGetField(tiff
, TIFFTAG_EXTRASAMPLES
, &extra_sample_count
, &extra_samples
);
394 extra_sample_count
= 1;
396 extra_samples
= &extra_sample
;
399 else if (samples
!= 3)
401 FIXME("unhandled RGB sample count %u\n", samples
);
405 decode_info
->frame
.bpp
= max(bps
, 8) * samples
;
406 decode_info
->source_bpp
= bps
* samples
;
412 decode_info
->reverse_bgr
= 1;
414 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat24bppBGR
;
416 switch(extra_samples
[0])
418 case 1: /* Associated (pre-multiplied) alpha data */
419 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat32bppPBGRA
;
421 case 0: /* Unspecified data */
422 case 2: /* Unassociated alpha data */
423 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat32bppBGRA
;
426 FIXME("unhandled extra sample type %i\n", extra_samples
[0]);
432 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat48bppRGB
;
434 switch(extra_samples
[0])
436 case 1: /* Associated (pre-multiplied) alpha data */
437 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat64bppPRGBA
;
439 case 0: /* Unspecified data */
440 case 2: /* Unassociated alpha data */
441 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat64bppRGBA
;
444 FIXME("unhandled extra sample type %i\n", extra_samples
[0]);
450 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat96bppRGBFloat
;
452 switch(extra_samples
[0])
454 case 1: /* Associated (pre-multiplied) alpha data */
455 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat128bppPRGBAFloat
;
457 case 0: /* Unspecified data */
458 case 2: /* Unassociated alpha data */
459 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat128bppRGBAFloat
;
462 FIXME("unhandled extra sample type %i\n", extra_samples
[0]);
467 WARN("unhandled RGB bit count %u\n", bps
);
468 return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT
;
471 case 3: /* RGB Palette */
474 FIXME("unhandled indexed sample count %u\n", samples
);
478 decode_info
->indexed
= 1;
479 decode_info
->frame
.bpp
= bps
;
483 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat1bppIndexed
;
486 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat2bppIndexed
;
489 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat4bppIndexed
;
492 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat8bppIndexed
;
495 FIXME("unhandled indexed bit count %u\n", bps
);
500 case 5: /* Separated */
503 FIXME("unhandled Separated sample count %u\n", samples
);
507 decode_info
->frame
.bpp
= bps
* samples
;
511 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat32bppCMYK
;
514 decode_info
->frame
.pixel_format
= GUID_WICPixelFormat64bppCMYK
;
518 WARN("unhandled Separated bit count %u\n", bps
);
519 return WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT
;
523 case 4: /* Transparency mask */
527 FIXME("unhandled PhotometricInterpretation %u\n", photometric
);
531 ret
= pTIFFGetField(tiff
, TIFFTAG_IMAGEWIDTH
, &decode_info
->frame
.width
);
534 WARN("missing image width\n");
538 ret
= pTIFFGetField(tiff
, TIFFTAG_IMAGELENGTH
, &decode_info
->frame
.height
);
541 WARN("missing image length\n");
545 if ((ret
= pTIFFGetField(tiff
, TIFFTAG_TILEWIDTH
, &decode_info
->tile_width
)))
547 decode_info
->tiled
= 1;
549 ret
= pTIFFGetField(tiff
, TIFFTAG_TILELENGTH
, &decode_info
->tile_height
);
552 WARN("missing tile height\n");
556 decode_info
->tile_stride
= ((decode_info
->frame
.bpp
* decode_info
->tile_width
+ 7)/8);
557 decode_info
->tile_size
= decode_info
->tile_height
* decode_info
->tile_stride
;
558 decode_info
->tiles_across
= (decode_info
->frame
.width
+ decode_info
->tile_width
- 1) / decode_info
->tile_width
;
560 else if ((ret
= pTIFFGetField(tiff
, TIFFTAG_ROWSPERSTRIP
, &decode_info
->tile_height
)))
562 if (decode_info
->tile_height
> decode_info
->frame
.height
)
563 decode_info
->tile_height
= decode_info
->frame
.height
;
564 decode_info
->tile_width
= decode_info
->frame
.width
;
565 decode_info
->tile_stride
= ((decode_info
->frame
.bpp
* decode_info
->tile_width
+ 7)/8);
566 decode_info
->tile_size
= decode_info
->tile_height
* decode_info
->tile_stride
;
570 /* Some broken TIFF files have a single strip and lack the RowsPerStrip tag */
571 decode_info
->tile_height
= decode_info
->frame
.height
;
572 decode_info
->tile_width
= decode_info
->frame
.width
;
573 decode_info
->tile_stride
= ((decode_info
->frame
.bpp
* decode_info
->tile_width
+ 7)/8);
574 decode_info
->tile_size
= decode_info
->tile_height
* decode_info
->tile_stride
;
578 pTIFFGetField(tiff
, TIFFTAG_RESOLUTIONUNIT
, &resolution_unit
);
580 ret
= pTIFFGetField(tiff
, TIFFTAG_XRESOLUTION
, &xres
);
583 WARN("missing X resolution\n");
585 /* Emulate the behavior of current libtiff versions (libtiff commit a39f6131)
586 * yielding 0 instead of INFINITY for IFD_RATIONAL fields with denominator 0. */
592 ret
= pTIFFGetField(tiff
, TIFFTAG_YRESOLUTION
, &yres
);
595 WARN("missing Y resolution\n");
602 if (xres
== 0.0 || yres
== 0.0)
604 decode_info
->frame
.dpix
= decode_info
->frame
.dpiy
= 96.0;
608 switch (resolution_unit
)
611 FIXME("unknown resolution unit %i\n", resolution_unit
);
613 case 0: /* Not set */
614 case 1: /* Relative measurements */
616 decode_info
->frame
.dpix
= xres
;
617 decode_info
->frame
.dpiy
= yres
;
619 case 3: /* Centimeter */
620 decode_info
->frame
.dpix
= xres
* 2.54;
621 decode_info
->frame
.dpiy
= yres
* 2.54;
626 if (decode_info
->indexed
&&
627 pTIFFGetField(tiff
, TIFFTAG_COLORMAP
, &red
, &green
, &blue
))
629 decode_info
->frame
.num_colors
= 1 << decode_info
->bps
;
630 for (i
=0; i
<decode_info
->frame
.num_colors
; i
++)
632 decode_info
->frame
.palette
[i
] = 0xff000000 |
633 ((red
[i
]<<8) & 0xff0000) |
634 (green
[i
] & 0xff00) |
635 ((blue
[i
]>>8) & 0xff);
640 decode_info
->frame
.num_colors
= 0;
643 if (pTIFFGetField(tiff
, TIFFTAG_ICCPROFILE
, &len
, &profile
))
644 decode_info
->frame
.num_color_contexts
= 1;
646 decode_info
->frame
.num_color_contexts
= 0;
651 static HRESULT CDECL
tiff_decoder_initialize(struct decoder
* iface
, IStream
*stream
, struct decoder_stat
*st
)
653 struct tiff_decoder
*This
= impl_from_decoder(iface
);
656 This
->tiff
= tiff_open_stream(stream
, "r");
660 This
->frame_count
= pTIFFNumberOfDirectories(This
->tiff
);
661 This
->cached_frame
= 0;
662 hr
= tiff_get_decode_info(This
->tiff
, &This
->cached_decode_info
);
666 st
->frame_count
= This
->frame_count
;
667 st
->flags
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
668 WICBitmapDecoderCapabilityCanDecodeSomeImages
|
669 WICBitmapDecoderCapabilityCanEnumerateMetadata
;
673 pTIFFClose(This
->tiff
);
678 static HRESULT
tiff_decoder_select_frame(struct tiff_decoder
* This
, DWORD frame
)
684 if (frame
>= This
->frame_count
)
687 if (This
->cached_frame
== frame
)
690 prev_tile_size
= This
->cached_tile
? This
->cached_decode_info
.tile_size
: 0;
692 res
= pTIFFSetDirectory(This
->tiff
, frame
);
696 hr
= tiff_get_decode_info(This
->tiff
, &This
->cached_decode_info
);
698 This
->cached_tile_x
= -1;
702 This
->cached_frame
= frame
;
703 if (This
->cached_decode_info
.tile_size
> prev_tile_size
)
705 free(This
->cached_tile
);
706 This
->cached_tile
= NULL
;
711 /* Set an invalid value to ensure we'll refresh cached_decode_info before using it. */
712 This
->cached_frame
= This
->frame_count
;
713 free(This
->cached_tile
);
714 This
->cached_tile
= NULL
;
720 static HRESULT CDECL
tiff_decoder_get_frame_info(struct decoder
* iface
, UINT frame
, struct decoder_frame
*info
)
722 struct tiff_decoder
*This
= impl_from_decoder(iface
);
725 hr
= tiff_decoder_select_frame(This
, frame
);
728 *info
= This
->cached_decode_info
.frame
;
734 static HRESULT
tiff_decoder_read_tile(struct tiff_decoder
*This
, UINT tile_x
, UINT tile_y
)
738 tiff_decode_info
*info
= &This
->cached_decode_info
;
740 swap_bytes
= pTIFFIsByteSwapped(This
->tiff
);
743 ret
= pTIFFReadEncodedTile(This
->tiff
, tile_x
+ tile_y
* info
->tiles_across
, This
->cached_tile
, info
->tile_size
);
745 ret
= pTIFFReadEncodedStrip(This
->tiff
, tile_y
, This
->cached_tile
, info
->tile_size
);
751 if (info
->source_bpp
== 3 && info
->samples
== 3 && info
->frame
.bpp
== 24)
753 BYTE
*srcdata
, *src
, *dst
;
754 DWORD x
, y
, count
, width_bytes
= (info
->tile_width
* 3 + 7) / 8;
756 count
= width_bytes
* info
->tile_height
;
758 srcdata
= malloc(count
);
759 if (!srcdata
) return E_OUTOFMEMORY
;
760 memcpy(srcdata
, This
->cached_tile
, count
);
762 for (y
= 0; y
< info
->tile_height
; y
++)
764 src
= srcdata
+ y
* width_bytes
;
765 dst
= This
->cached_tile
+ y
* info
->tile_width
* 3;
767 for (x
= 0; x
< info
->tile_width
; x
+= 8)
769 dst
[2] = (src
[0] & 0x80) ? 0xff : 0; /* R */
770 dst
[1] = (src
[0] & 0x40) ? 0xff : 0; /* G */
771 dst
[0] = (src
[0] & 0x20) ? 0xff : 0; /* B */
772 if (x
+ 1 < info
->tile_width
)
774 dst
[5] = (src
[0] & 0x10) ? 0xff : 0; /* R */
775 dst
[4] = (src
[0] & 0x08) ? 0xff : 0; /* G */
776 dst
[3] = (src
[0] & 0x04) ? 0xff : 0; /* B */
778 if (x
+ 2 < info
->tile_width
)
780 dst
[8] = (src
[0] & 0x02) ? 0xff : 0; /* R */
781 dst
[7] = (src
[0] & 0x01) ? 0xff : 0; /* G */
782 dst
[6] = (src
[1] & 0x80) ? 0xff : 0; /* B */
784 if (x
+ 3 < info
->tile_width
)
786 dst
[11] = (src
[1] & 0x40) ? 0xff : 0; /* R */
787 dst
[10] = (src
[1] & 0x20) ? 0xff : 0; /* G */
788 dst
[9] = (src
[1] & 0x10) ? 0xff : 0; /* B */
790 if (x
+ 4 < info
->tile_width
)
792 dst
[14] = (src
[1] & 0x08) ? 0xff : 0; /* R */
793 dst
[13] = (src
[1] & 0x04) ? 0xff : 0; /* G */
794 dst
[12] = (src
[1] & 0x02) ? 0xff : 0; /* B */
796 if (x
+ 5 < info
->tile_width
)
798 dst
[17] = (src
[1] & 0x01) ? 0xff : 0; /* R */
799 dst
[16] = (src
[2] & 0x80) ? 0xff : 0; /* G */
800 dst
[15] = (src
[2] & 0x40) ? 0xff : 0; /* B */
802 if (x
+ 6 < info
->tile_width
)
804 dst
[20] = (src
[2] & 0x20) ? 0xff : 0; /* R */
805 dst
[19] = (src
[2] & 0x10) ? 0xff : 0; /* G */
806 dst
[18] = (src
[2] & 0x08) ? 0xff : 0; /* B */
808 if (x
+ 7 < info
->tile_width
)
810 dst
[23] = (src
[2] & 0x04) ? 0xff : 0; /* R */
811 dst
[22] = (src
[2] & 0x02) ? 0xff : 0; /* G */
812 dst
[21] = (src
[2] & 0x01) ? 0xff : 0; /* B */
822 else if (info
->source_bpp
== 12 && info
->samples
== 3 && info
->frame
.bpp
== 24)
824 BYTE
*srcdata
, *src
, *dst
;
825 DWORD x
, y
, count
, width_bytes
= (info
->tile_width
* 12 + 7) / 8;
827 count
= width_bytes
* info
->tile_height
;
829 srcdata
= malloc(count
);
830 if (!srcdata
) return E_OUTOFMEMORY
;
831 memcpy(srcdata
, This
->cached_tile
, count
);
833 for (y
= 0; y
< info
->tile_height
; y
++)
835 src
= srcdata
+ y
* width_bytes
;
836 dst
= This
->cached_tile
+ y
* info
->tile_width
* 3;
838 for (x
= 0; x
< info
->tile_width
; x
+= 2)
840 dst
[0] = ((src
[1] & 0xf0) >> 4) * 17; /* B */
841 dst
[1] = (src
[0] & 0x0f) * 17; /* G */
842 dst
[2] = ((src
[0] & 0xf0) >> 4) * 17; /* R */
843 if (x
+ 1 < info
->tile_width
)
845 dst
[5] = (src
[1] & 0x0f) * 17; /* B */
846 dst
[4] = ((src
[2] & 0xf0) >> 4) * 17; /* G */
847 dst
[3] = (src
[2] & 0x0f) * 17; /* R */
857 else if (info
->source_bpp
== 4 && info
->samples
== 4 && info
->frame
.bpp
== 32)
859 BYTE
*srcdata
, *src
, *dst
;
860 DWORD x
, y
, count
, width_bytes
= (info
->tile_width
* 3 + 7) / 8;
862 count
= width_bytes
* info
->tile_height
;
864 srcdata
= malloc(count
);
865 if (!srcdata
) return E_OUTOFMEMORY
;
866 memcpy(srcdata
, This
->cached_tile
, count
);
868 for (y
= 0; y
< info
->tile_height
; y
++)
870 src
= srcdata
+ y
* width_bytes
;
871 dst
= This
->cached_tile
+ y
* info
->tile_width
* 4;
873 /* 1 source byte expands to 2 BGRA samples */
875 for (x
= 0; x
< info
->tile_width
; x
+= 2)
877 dst
[0] = (src
[0] & 0x20) ? 0xff : 0; /* B */
878 dst
[1] = (src
[0] & 0x40) ? 0xff : 0; /* G */
879 dst
[2] = (src
[0] & 0x80) ? 0xff : 0; /* R */
880 dst
[3] = (src
[0] & 0x10) ? 0xff : 0; /* A */
881 if (x
+ 1 < info
->tile_width
)
883 dst
[4] = (src
[0] & 0x02) ? 0xff : 0; /* B */
884 dst
[5] = (src
[0] & 0x04) ? 0xff : 0; /* G */
885 dst
[6] = (src
[0] & 0x08) ? 0xff : 0; /* R */
886 dst
[7] = (src
[0] & 0x01) ? 0xff : 0; /* A */
896 else if (info
->source_bpp
== 16 && info
->samples
== 4 && info
->frame
.bpp
== 32)
898 BYTE
*srcdata
, *src
, *dst
;
899 DWORD x
, y
, count
, width_bytes
= (info
->tile_width
* 12 + 7) / 8;
901 count
= width_bytes
* info
->tile_height
;
903 srcdata
= malloc(count
);
904 if (!srcdata
) return E_OUTOFMEMORY
;
905 memcpy(srcdata
, This
->cached_tile
, count
);
907 for (y
= 0; y
< info
->tile_height
; y
++)
909 src
= srcdata
+ y
* width_bytes
;
910 dst
= This
->cached_tile
+ y
* info
->tile_width
* 4;
912 for (x
= 0; x
< info
->tile_width
; x
++)
914 dst
[0] = ((src
[1] & 0xf0) >> 4) * 17; /* B */
915 dst
[1] = (src
[0] & 0x0f) * 17; /* G */
916 dst
[2] = ((src
[0] & 0xf0) >> 4) * 17; /* R */
917 dst
[3] = (src
[1] & 0x0f) * 17; /* A */
925 /* 8bpp grayscale with extra alpha */
926 else if (info
->source_bpp
== 16 && info
->samples
== 2 && info
->frame
.bpp
== 32)
929 DWORD
*dst
, count
= info
->tile_width
* info
->tile_height
;
931 src
= This
->cached_tile
+ info
->tile_width
* info
->tile_height
* 2 - 2;
932 dst
= (DWORD
*)(This
->cached_tile
+ info
->tile_size
- 4);
936 *dst
-- = src
[0] | (src
[0] << 8) | (src
[0] << 16) | (src
[1] << 24);
941 if (info
->reverse_bgr
)
945 UINT sample_count
= info
->samples
;
947 reverse_bgr8(sample_count
, This
->cached_tile
, info
->tile_width
,
948 info
->tile_height
, info
->tile_width
* sample_count
);
952 if (swap_bytes
&& info
->bps
> 8)
954 UINT row
, i
, samples_per_row
;
957 samples_per_row
= info
->tile_width
* info
->samples
;
962 for (row
=0; row
<info
->tile_height
; row
++)
964 sample
= This
->cached_tile
+ row
* info
->tile_stride
;
965 for (i
=0; i
<samples_per_row
; i
++)
968 sample
[1] = sample
[0];
975 ERR("unhandled bps for byte swap %u\n", info
->bps
);
980 if (info
->invert_grayscale
)
984 if (info
->samples
!= 1)
986 ERR("cannot invert grayscale image with %u samples\n", info
->samples
);
990 end
= This
->cached_tile
+info
->tile_size
;
992 for (byte
= This
->cached_tile
; byte
!= end
; byte
++)
996 This
->cached_tile_x
= tile_x
;
997 This
->cached_tile_y
= tile_y
;
1002 static HRESULT CDECL
tiff_decoder_copy_pixels(struct decoder
* iface
, UINT frame
,
1003 const WICRect
*prc
, UINT stride
, UINT buffersize
, BYTE
*buffer
)
1005 struct tiff_decoder
*This
= impl_from_decoder(iface
);
1007 UINT min_tile_x
, max_tile_x
, min_tile_y
, max_tile_y
;
1008 UINT tile_x
, tile_y
;
1011 tiff_decode_info
*info
= &This
->cached_decode_info
;
1013 hr
= tiff_decoder_select_frame(This
, frame
);
1017 if (!This
->cached_tile
)
1019 This
->cached_tile
= malloc(info
->tile_size
);
1020 if (!This
->cached_tile
)
1021 return E_OUTOFMEMORY
;
1024 min_tile_x
= prc
->X
/ info
->tile_width
;
1025 min_tile_y
= prc
->Y
/ info
->tile_height
;
1026 max_tile_x
= (prc
->X
+prc
->Width
-1) / info
->tile_width
;
1027 max_tile_y
= (prc
->Y
+prc
->Height
-1) / info
->tile_height
;
1029 for (tile_x
=min_tile_x
; tile_x
<= max_tile_x
; tile_x
++)
1031 for (tile_y
=min_tile_y
; tile_y
<= max_tile_y
; tile_y
++)
1033 if (tile_x
!= This
->cached_tile_x
|| tile_y
!= This
->cached_tile_y
)
1035 hr
= tiff_decoder_read_tile(This
, tile_x
, tile_y
);
1040 if (prc
->X
< tile_x
* info
->tile_width
)
1043 rc
.X
= prc
->X
- tile_x
* info
->tile_width
;
1045 if (prc
->Y
< tile_y
* info
->tile_height
)
1048 rc
.Y
= prc
->Y
- tile_y
* info
->tile_height
;
1050 if (prc
->X
+prc
->Width
> (tile_x
+1) * info
->tile_width
)
1051 rc
.Width
= info
->tile_width
- rc
.X
;
1052 else if (prc
->X
< tile_x
* info
->tile_width
)
1053 rc
.Width
= prc
->Width
+ prc
->X
- tile_x
* info
->tile_width
;
1055 rc
.Width
= prc
->Width
;
1057 if (prc
->Y
+prc
->Height
> (tile_y
+1) * info
->tile_height
)
1058 rc
.Height
= info
->tile_height
- rc
.Y
;
1059 else if (prc
->Y
< tile_y
* info
->tile_height
)
1060 rc
.Height
= prc
->Height
+ prc
->Y
- tile_y
* info
->tile_height
;
1062 rc
.Height
= prc
->Height
;
1064 dst_tilepos
= buffer
+ (stride
* ((rc
.Y
+ tile_y
* info
->tile_height
) - prc
->Y
)) +
1065 ((info
->frame
.bpp
* ((rc
.X
+ tile_x
* info
->tile_width
) - prc
->X
) + 7) / 8);
1067 hr
= copy_pixels(info
->frame
.bpp
, This
->cached_tile
,
1068 info
->tile_width
, info
->tile_height
, info
->tile_stride
,
1069 &rc
, stride
, buffersize
, dst_tilepos
);
1074 TRACE("<-- 0x%x\n", hr
);
1083 static HRESULT CDECL
tiff_decoder_get_color_context(struct decoder
*iface
,
1084 UINT frame
, UINT num
, BYTE
**data
, DWORD
*datasize
)
1086 struct tiff_decoder
*This
= impl_from_decoder(iface
);
1087 const BYTE
*profile
;
1091 hr
= tiff_decoder_select_frame(This
, frame
);
1095 if (!pTIFFGetField(This
->tiff
, TIFFTAG_ICCPROFILE
, &len
, &profile
))
1097 return E_UNEXPECTED
;
1101 *data
= RtlAllocateHeap(GetProcessHeap(), 0, len
);
1103 return E_OUTOFMEMORY
;
1105 memcpy(*data
, profile
, len
);
1110 static HRESULT CDECL
tiff_decoder_get_metadata_blocks(struct decoder
*iface
,
1111 UINT frame
, UINT
*count
, struct decoder_block
**blocks
)
1113 struct tiff_decoder
*This
= impl_from_decoder(iface
);
1116 struct decoder_block result
;
1118 hr
= tiff_decoder_select_frame(This
, frame
);
1124 result
.offset
= pTIFFCurrentDirOffset(This
->tiff
);
1127 byte_swapped
= pTIFFIsByteSwapped(This
->tiff
);
1128 #ifdef WORDS_BIGENDIAN
1129 result
.options
= byte_swapped
? WICPersistOptionLittleEndian
: WICPersistOptionBigEndian
;
1131 result
.options
= byte_swapped
? WICPersistOptionBigEndian
: WICPersistOptionLittleEndian
;
1133 result
.options
|= WICPersistOptionNoCacheStream
|DECODER_BLOCK_FULL_STREAM
|DECODER_BLOCK_READER_CLSID
;
1134 result
.reader_clsid
= CLSID_WICIfdMetadataReader
;
1136 *blocks
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(**blocks
));
1142 static void CDECL
tiff_decoder_destroy(struct decoder
* iface
)
1144 struct tiff_decoder
*This
= impl_from_decoder(iface
);
1145 if (This
->tiff
) pTIFFClose(This
->tiff
);
1146 free(This
->cached_tile
);
1147 RtlFreeHeap(GetProcessHeap(), 0, This
);
1150 static const struct decoder_funcs tiff_decoder_vtable
= {
1151 tiff_decoder_initialize
,
1152 tiff_decoder_get_frame_info
,
1153 tiff_decoder_copy_pixels
,
1154 tiff_decoder_get_metadata_blocks
,
1155 tiff_decoder_get_color_context
,
1156 tiff_decoder_destroy
1159 HRESULT CDECL
tiff_decoder_create(struct decoder_info
*info
, struct decoder
**result
)
1161 struct tiff_decoder
*This
;
1163 if (!load_libtiff())
1165 ERR("Failed reading TIFF because unable to load %s\n",SONAME_LIBTIFF
);
1169 This
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*This
));
1170 if (!This
) return E_OUTOFMEMORY
;
1172 This
->decoder
.vtable
= &tiff_decoder_vtable
;
1174 This
->cached_tile
= NULL
;
1175 This
->cached_tile_x
= -1;
1176 *result
= &This
->decoder
;
1178 info
->container_format
= GUID_ContainerFormatTiff
;
1179 info
->block_format
= GUID_ContainerFormatTiff
;
1180 info
->clsid
= CLSID_WICTiffDecoder
;
1185 struct tiff_encode_format
{
1186 const WICPixelFormatGUID
*guid
;
1192 int extra_sample_type
;
1197 static const struct tiff_encode_format formats
[] = {
1198 {&GUID_WICPixelFormat24bppBGR
, 2, 8, 3, 24, 0, 0, 1},
1199 {&GUID_WICPixelFormat24bppRGB
, 2, 8, 3, 24, 0, 0, 0},
1200 {&GUID_WICPixelFormatBlackWhite
, 1, 1, 1, 1, 0, 0, 0},
1201 {&GUID_WICPixelFormat4bppGray
, 1, 4, 1, 4, 0, 0, 0},
1202 {&GUID_WICPixelFormat8bppGray
, 1, 8, 1, 8, 0, 0, 0},
1203 {&GUID_WICPixelFormat32bppBGRA
, 2, 8, 4, 32, 1, 2, 1},
1204 {&GUID_WICPixelFormat32bppPBGRA
, 2, 8, 4, 32, 1, 1, 1},
1205 {&GUID_WICPixelFormat48bppRGB
, 2, 16, 3, 48, 0, 0, 0},
1206 {&GUID_WICPixelFormat64bppRGBA
, 2, 16, 4, 64, 1, 2, 0},
1207 {&GUID_WICPixelFormat64bppPRGBA
, 2, 16, 4, 64, 1, 1, 0},
1208 {&GUID_WICPixelFormat1bppIndexed
, 3, 1, 1, 1, 0, 0, 0, 1},
1209 {&GUID_WICPixelFormat4bppIndexed
, 3, 4, 1, 4, 0, 0, 0, 1},
1210 {&GUID_WICPixelFormat8bppIndexed
, 3, 8, 1, 8, 0, 0, 0, 1},
1214 typedef struct tiff_encoder
{
1215 struct encoder encoder
;
1217 const struct tiff_encode_format
*format
;
1218 struct encoder_frame encoder_frame
;
1220 DWORD lines_written
;
1223 static inline struct tiff_encoder
*impl_from_encoder(struct encoder
* iface
)
1225 return CONTAINING_RECORD(iface
, struct tiff_encoder
, encoder
);
1228 static HRESULT CDECL
tiff_encoder_initialize(struct encoder
* iface
, IStream
*stream
)
1230 struct tiff_encoder
* This
= impl_from_encoder(iface
);
1233 tiff
= tiff_open_stream(stream
, "w");
1243 static HRESULT CDECL
tiff_encoder_get_supported_format(struct encoder
*iface
,
1244 GUID
*pixel_format
, DWORD
*bpp
, BOOL
*indexed
)
1248 if (IsEqualGUID(pixel_format
, &GUID_WICPixelFormat2bppIndexed
))
1249 *pixel_format
= GUID_WICPixelFormat4bppIndexed
;
1251 for (i
=0; formats
[i
].guid
; i
++)
1253 if (IsEqualGUID(formats
[i
].guid
, pixel_format
))
1257 if (!formats
[i
].guid
) i
= 0;
1259 *pixel_format
= *formats
[i
].guid
;
1260 *bpp
= formats
[i
].bpp
;
1261 *indexed
= formats
[i
].indexed
;
1266 static HRESULT CDECL
tiff_encoder_create_frame(struct encoder
* iface
, const struct encoder_frame
*frame
)
1268 struct tiff_encoder
* This
= impl_from_encoder(iface
);
1271 if (This
->num_frames
!= 0)
1272 pTIFFWriteDirectory(This
->tiff
);
1275 This
->lines_written
= 0;
1276 This
->encoder_frame
= *frame
;
1278 for (i
=0; formats
[i
].guid
; i
++)
1280 if (IsEqualGUID(formats
[i
].guid
, &frame
->pixel_format
))
1284 This
->format
= &formats
[i
];
1286 pTIFFSetField(This
->tiff
, TIFFTAG_PHOTOMETRIC
, (uint16_t)This
->format
->photometric
);
1287 pTIFFSetField(This
->tiff
, TIFFTAG_PLANARCONFIG
, (uint16_t)1);
1288 pTIFFSetField(This
->tiff
, TIFFTAG_BITSPERSAMPLE
, (uint16_t)This
->format
->bps
);
1289 pTIFFSetField(This
->tiff
, TIFFTAG_SAMPLESPERPIXEL
, (uint16_t)This
->format
->samples
);
1291 if (This
->format
->extra_sample
)
1293 uint16_t extra_samples
;
1294 extra_samples
= This
->format
->extra_sample_type
;
1296 pTIFFSetField(This
->tiff
, TIFFTAG_EXTRASAMPLES
, (uint16_t)1, &extra_samples
);
1299 pTIFFSetField(This
->tiff
, TIFFTAG_IMAGEWIDTH
, (uint32_t)frame
->width
);
1300 pTIFFSetField(This
->tiff
, TIFFTAG_IMAGELENGTH
, (uint32_t)frame
->height
);
1302 if (frame
->dpix
!= 0.0 && frame
->dpiy
!= 0.0)
1304 pTIFFSetField(This
->tiff
, TIFFTAG_RESOLUTIONUNIT
, (uint16_t)2); /* Inch */
1305 pTIFFSetField(This
->tiff
, TIFFTAG_XRESOLUTION
, (float)frame
->dpix
);
1306 pTIFFSetField(This
->tiff
, TIFFTAG_YRESOLUTION
, (float)frame
->dpiy
);
1309 if (This
->format
->bpp
<= 8 && frame
->num_colors
&& This
->format
->indexed
)
1311 uint16_t red
[256], green
[256], blue
[256];
1314 for (i
= 0; i
< frame
->num_colors
; i
++)
1316 red
[i
] = (frame
->palette
[i
] >> 8) & 0xff00;
1317 green
[i
] = frame
->palette
[i
] & 0xff00;
1318 blue
[i
] = (frame
->palette
[i
] << 8) & 0xff00;
1321 pTIFFSetField(This
->tiff
, TIFFTAG_COLORMAP
, red
, green
, blue
);
1327 static HRESULT CDECL
tiff_encoder_write_lines(struct encoder
* iface
,
1328 BYTE
*data
, DWORD line_count
, DWORD stride
)
1330 struct tiff_encoder
* This
= impl_from_encoder(iface
);
1331 BYTE
*row_data
, *swapped_data
= NULL
;
1332 UINT i
, j
, line_size
;
1334 line_size
= ((This
->encoder_frame
.width
* This
->format
->bpp
)+7)/8;
1336 if (This
->format
->reverse_bgr
)
1338 swapped_data
= malloc(line_size
);
1340 return E_OUTOFMEMORY
;
1343 for (i
=0; i
<line_count
; i
++)
1345 row_data
= data
+ i
* stride
;
1347 if (This
->format
->reverse_bgr
&& This
->format
->bps
== 8)
1349 memcpy(swapped_data
, row_data
, line_size
);
1350 for (j
=0; j
<line_size
; j
+= This
->format
->samples
)
1353 temp
= swapped_data
[j
];
1354 swapped_data
[j
] = swapped_data
[j
+2];
1355 swapped_data
[j
+2] = temp
;
1357 row_data
= swapped_data
;
1360 pTIFFWriteScanline(This
->tiff
, (tdata_t
)row_data
, i
+This
->lines_written
, 0);
1363 This
->lines_written
+= line_count
;
1368 static HRESULT CDECL
tiff_encoder_commit_frame(struct encoder
* iface
)
1373 static HRESULT CDECL
tiff_encoder_commit_file(struct encoder
* iface
)
1375 struct tiff_encoder
* This
= impl_from_encoder(iface
);
1377 pTIFFClose(This
->tiff
);
1383 static void CDECL
tiff_encoder_destroy(struct encoder
* iface
)
1385 struct tiff_encoder
*This
= impl_from_encoder(iface
);
1387 if (This
->tiff
) pTIFFClose(This
->tiff
);
1388 RtlFreeHeap(GetProcessHeap(), 0, This
);
1391 static const struct encoder_funcs tiff_encoder_vtable
= {
1392 tiff_encoder_initialize
,
1393 tiff_encoder_get_supported_format
,
1394 tiff_encoder_create_frame
,
1395 tiff_encoder_write_lines
,
1396 tiff_encoder_commit_frame
,
1397 tiff_encoder_commit_file
,
1398 tiff_encoder_destroy
1401 HRESULT CDECL
tiff_encoder_create(struct encoder_info
*info
, struct encoder
**result
)
1403 struct tiff_encoder
*This
;
1405 if (!load_libtiff())
1407 ERR("Failed writing TIFF because unable to load %s\n",SONAME_LIBTIFF
);
1411 This
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*This
));
1412 if (!This
) return E_OUTOFMEMORY
;
1414 This
->encoder
.vtable
= &tiff_encoder_vtable
;
1416 This
->num_frames
= 0;
1418 info
->flags
= ENCODER_FLAGS_MULTI_FRAME
| ENCODER_FLAGS_SUPPORTS_METADATA
;
1419 info
->container_format
= GUID_ContainerFormatTiff
;
1420 info
->clsid
= CLSID_WICTiffEncoder
;
1421 info
->encoder_options
[0] = ENCODER_OPTION_COMPRESSION_METHOD
;
1422 info
->encoder_options
[1] = ENCODER_OPTION_COMPRESSION_QUALITY
;
1423 info
->encoder_options
[2] = ENCODER_OPTION_END
;
1425 *result
= &This
->encoder
;
1430 #else /* !SONAME_LIBTIFF */
1432 HRESULT CDECL
tiff_decoder_create(struct decoder_info
*info
, struct decoder
**result
)
1434 ERR("Trying to load TIFF picture, but Wine was compiled without TIFF support.\n");
1438 HRESULT CDECL
tiff_encoder_create(struct encoder_info
*info
, struct encoder
**result
)
1440 ERR("Trying to save TIFF picture, but Wine was compiled without TIFF support.\n");