1 /*
2     Copyright 2005-2007 Adobe Systems Incorporated
3 
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).
7 
8     See http://opensource.adobe.com/gil for most recent version including documentation.
9 */
10 
11 /*************************************************************************************************/
12 
13 #ifndef GIL_TIFF_DYNAMIC_IO_H
14 #define GIL_TIFF_DYNAMIC_IO_H
15 
16 /// \file
17 /// \brief  Support for reading and writing TIFF files
18 ///         Requires libtiff!
19 /// \author Hailin Jin and Lubomir Bourdev \n
20 ///         Adobe Systems Incorporated
21 /// \date   2005-2007 \n Last updated June 10, 2006
22 //
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>)
26 //
27 
28 #include <string>
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"
34 
35 namespace boost { namespace gil {
36 
37 namespace detail {
38 
39 struct tiff_write_is_supported {
40     template<typename View> struct apply
41         : public mpl::bool_<tiff_write_support<View>::is_supported> {};
42 };
43 
44 class tiff_writer_dynamic : public tiff_writer {
45 public:
46     typedef void result_type;
tiff_writer_dynamic(const char * filename)47     tiff_writer_dynamic(const char* filename) : tiff_writer(filename) {}
48 
49     template <typename Views>
write_view(const any_image_view<Views> & runtime_view)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);
53     }
54 };
55 
56 class tiff_type_format_checker {
57     int _bit_depth;
58     int _color_type;
59 public:
tiff_type_format_checker(int bit_depth_in,int color_type_in)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>
apply()63     bool apply() {
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;
66     }
67 };
68 
69 struct tiff_read_is_supported {
70     template<typename View> struct apply
71         : public mpl::bool_<tiff_read_support<View>::is_supported> {};
72 };
73 
74 class tiff_reader_dynamic : public tiff_reader {
75 public:
tiff_reader_dynamic(const char * filename)76     tiff_reader_dynamic(const char* filename) : tiff_reader(filename) {}
77 
78     template <typename Images>
read_image(any_image<Images> & im)79     void read_image(any_image<Images>& im) {
80         int width,height;
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");
88         } else {
89             im.recreate(width,height);
90             dynamic_io_fnobj<tiff_read_is_supported, tiff_reader> op(this);
91             apply_operation(view(im),op);
92         }
93     }
94 };
95 
96 } // namespace detail
97 
98 /// \ingroup TIFF_IO
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>
tiff_read_image(const char * filename,any_image<Images> & im)104 inline void tiff_read_image(const char* filename,any_image<Images>& im) {
105     detail::tiff_reader_dynamic m(filename);
106     m.read_image(im);
107 }
108 
109 /// \ingroup TIFF_IO
110 /// \brief reads a TIFF image into a run-time instantiated image
111 template <typename Images>
tiff_read_image(const std::string & filename,any_image<Images> & im)112 inline void tiff_read_image(const std::string& filename,any_image<Images>& im) {
113     tiff_read_image(filename.c_str(),im);
114 }
115 
116 /// \ingroup TIFF_IO
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>
tiff_write_view(const char * filename,const any_image_view<Views> & runtime_view)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);
124 }
125 
126 /// \ingroup TIFF_IO
127 /// \brief Saves the currently instantiated view to a tiff file specified by the given tiff image file name.
128 template <typename Views>
tiff_write_view(const std::string & filename,const any_image_view<Views> & runtime_view)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);
131 }
132 
133 } }  // namespace boost::gil
134 
135 #endif
136