2 Copyright 2005-2007 Adobe Systems Incorporated
4 Use, modification and distribution are subject to the Boost Software License,
5 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 http://www.boost.org/LICENSE_1_0.txt).
8 See http://opensource.adobe.com/gil for most recent version including documentation.
11 /*************************************************************************************************/
13 #ifndef GIL_TIFF_DYNAMIC_IO_H
14 #define GIL_TIFF_DYNAMIC_IO_H
17 /// \brief Support for reading and writing TIFF files
19 /// \author Hailin Jin and Lubomir Bourdev \n
20 /// Adobe Systems Incorporated
21 /// \date 2005-2007 \n Last updated June 10, 2006
23 // We are currently providing the following functions:
24 // template <typename Images> void tiff_read_image(const char*,any_image<Images>)
25 // template <typename Views> void tiff_write_view(const char*,any_image_view<Views>)
29 #include <boost/mpl/bool.hpp>
30 #include "../dynamic_image/dynamic_image_all.hpp"
31 #include "io_error.hpp"
32 #include "tiff_io.hpp"
33 #include "dynamic_io.hpp"
35 namespace boost { namespace gil {
39 struct tiff_write_is_supported {
40 template<typename View> struct apply
41 : public mpl::bool_<tiff_write_support<View>::is_supported> {};
44 class tiff_writer_dynamic : public tiff_writer {
46 typedef void result_type;
47 tiff_writer_dynamic(const char* filename) : tiff_writer(filename) {}
49 template <typename Views>
50 void write_view(const any_image_view<Views>& runtime_view) {
51 dynamic_io_fnobj<tiff_write_is_supported, tiff_writer> op(this);
52 apply_operation(runtime_view,op);
56 class tiff_type_format_checker {
60 tiff_type_format_checker(int bit_depth_in,int color_type_in) :
61 _bit_depth(bit_depth_in),_color_type(color_type_in) {}
62 template <typename Image>
64 return tiff_read_support<typename Image::view_t>::bit_depth==_bit_depth &&
65 tiff_read_support<typename Image::view_t>::color_type==_color_type;
69 struct tiff_read_is_supported {
70 template<typename View> struct apply
71 : public mpl::bool_<tiff_read_support<View>::is_supported> {};
74 class tiff_reader_dynamic : public tiff_reader {
76 tiff_reader_dynamic(const char* filename) : tiff_reader(filename) {}
78 template <typename Images>
79 void read_image(any_image<Images>& im) {
81 unsigned short bps,photometric;
82 TIFFGetField(_tp,TIFFTAG_IMAGEWIDTH,&width);
83 TIFFGetField(_tp,TIFFTAG_IMAGELENGTH,&height);
84 TIFFGetField(_tp,TIFFTAG_BITSPERSAMPLE,&bps);
85 TIFFGetField(_tp,TIFFTAG_PHOTOMETRIC,&photometric);
86 if (!construct_matched(im,tiff_type_format_checker(bps,photometric))) {
87 io_error("tiff_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
89 im.recreate(width,height);
90 dynamic_io_fnobj<tiff_read_is_supported, tiff_reader> op(this);
91 apply_operation(view(im),op);
99 /// \brief reads a TIFF image into a run-time instantiated image
100 /// Opens the given tiff file name, selects the first type in Images whose color space and channel are compatible to those of the image file
101 /// and creates a new image of that type with the dimensions specified by the image file.
102 /// Throws std::ios_base::failure if none of the types in Images are compatible with the type on disk.
103 template <typename Images>
104 inline void tiff_read_image(const char* filename,any_image<Images>& im) {
105 detail::tiff_reader_dynamic m(filename);
110 /// \brief reads a TIFF image into a run-time instantiated image
111 template <typename Images>
112 inline void tiff_read_image(const std::string& filename,any_image<Images>& im) {
113 tiff_read_image(filename.c_str(),im);
117 /// \brief Saves the currently instantiated view to a tiff file specified by the given tiff image file name.
118 /// Throws std::ios_base::failure if the currently instantiated view type is not supported for writing by the I/O extension
119 /// or if it fails to create the file.
120 template <typename Views>
121 inline void tiff_write_view(const char* filename,const any_image_view<Views>& runtime_view) {
122 detail::tiff_writer_dynamic m(filename);
123 m.write_view(runtime_view);
127 /// \brief Saves the currently instantiated view to a tiff file specified by the given tiff image file name.
128 template <typename Views>
129 inline void tiff_write_view(const std::string& filename,const any_image_view<Views>& runtime_view) {
130 tiff_write_view(filename.c_str(),runtime_view);
133 } } // namespace boost::gil