Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / gil / extension / io / tiff / detail / writer_backend.hpp
1 //
2 // Copyright 2012 Christian Henning
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 #ifndef BOOST_GIL_EXTENSION_IO_TIFF_DETAIL_WRITER_BACKEND_HPP
9 #define BOOST_GIL_EXTENSION_IO_TIFF_DETAIL_WRITER_BACKEND_HPP
10
11 #include <boost/gil/extension/io/tiff/tags.hpp>
12 #include <boost/gil/extension/io/tiff/detail/device.hpp>
13
14 #include <boost/gil/detail/mp11.hpp>
15
16 namespace boost { namespace gil {
17
18 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
19 #pragma warning(push)
20 #pragma warning(disable:4512) //assignment operator could not be generated
21 #endif
22
23 ///
24 /// TIFF Writer Backend
25 ///
26 template< typename Device >
27 struct writer_backend< Device
28                      , tiff_tag
29                      >
30 {
31 public:
32
33     using format_tag_t = tiff_tag;
34
35 public:
36
37     writer_backend( const Device&                       io_dev
38                   , const image_write_info< tiff_tag >& info
39                   )
40     : _io_dev( io_dev )
41     , _info( info )
42     {}
43
44 protected:
45
46     template< typename View >
47     void write_header( const View& view )
48     {
49         using pixel_t = typename View::value_type;
50
51         // get the type of the first channel (heterogeneous pixels might be broken for now!)
52         using channel_t = typename channel_traits<typename element_type<pixel_t>::type>::value_type;
53                                 using color_space_t = typename color_space_type<View>::type;
54
55         if(! this->_info._photometric_interpretation_user_defined )
56         {
57             // write photometric interpretion - Warning: This value is rather
58             // subjective. The user should better set this value itself. There
59             // is no way to decide if a image is PHOTOMETRIC_MINISWHITE or
60             // PHOTOMETRIC_MINISBLACK. If the user has not manually set it, then
61             // this writer will assume PHOTOMETRIC_MINISBLACK for gray_t images,
62             // PHOTOMETRIC_RGB for rgb_t images, and PHOTOMETRIC_SEPARATED (as
63             // is conventional) for cmyk_t images.
64             this->_info._photometric_interpretation = detail::photometric_interpretation< color_space_t >::value;
65         }
66
67         // write dimensions
68         tiff_image_width::type  width  = (tiff_image_width::type)  view.width();
69         tiff_image_height::type height = (tiff_image_height::type) view.height();
70
71         this->_io_dev.template set_property< tiff_image_width  >( width  );
72         this->_io_dev.template set_property< tiff_image_height >( height );
73
74         // write planar configuration
75         this->_io_dev.template set_property<tiff_planar_configuration>( this->_info._planar_configuration );
76
77         // write samples per pixel
78         tiff_samples_per_pixel::type samples_per_pixel = num_channels< pixel_t >::value;
79         this->_io_dev.template set_property<tiff_samples_per_pixel>( samples_per_pixel );
80
81         if /*constexpr*/ (mp11::mp_contains<color_space_t, alpha_t>::value)
82         {
83           std:: vector <uint16_t> extra_samples {EXTRASAMPLE_ASSOCALPHA};
84           this->_io_dev.template set_property<tiff_extra_samples>( extra_samples );
85         }
86
87         // write bits per sample
88         // @todo: Settings this value usually requires to write for each sample the bit
89         // value seperately in case they are different, like rgb556.
90         tiff_bits_per_sample::type bits_per_sample = detail::unsigned_integral_num_bits< channel_t >::value;
91         this->_io_dev.template set_property<tiff_bits_per_sample>( bits_per_sample );
92
93         // write sample format
94         tiff_sample_format::type sampl_format = detail::sample_format< channel_t >::value;
95         this->_io_dev.template set_property<tiff_sample_format>( sampl_format );
96
97         // write photometric format
98         this->_io_dev.template set_property<tiff_photometric_interpretation>( this->_info._photometric_interpretation );
99
100         // write compression
101         this->_io_dev.template set_property<tiff_compression>( this->_info._compression );
102
103         // write orientation
104         this->_io_dev.template set_property<tiff_orientation>( this->_info._orientation );
105
106         // write rows per strip
107         this->_io_dev.template set_property<tiff_rows_per_strip>( this->_io_dev.get_default_strip_size() );
108
109         // write x, y resolution and units
110         this->_io_dev.template set_property<tiff_resolution_unit>( this->_info._resolution_unit );
111         this->_io_dev.template set_property<tiff_x_resolution>( this->_info._x_resolution );
112         this->_io_dev.template set_property<tiff_y_resolution>( this->_info._y_resolution );
113
114         /// Optional and / or non-baseline tags below here
115
116         // write ICC colour profile, if it's there
117         // http://www.color.org/icc_specs2.xalter
118         if ( 0 != this->_info._icc_profile.size())
119           this->_io_dev.template set_property<tiff_icc_profile>( this->_info._icc_profile );
120     }
121
122
123 public:
124
125     Device _io_dev;
126
127     image_write_info< tiff_tag > _info;
128 };
129
130 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
131 #pragma warning(pop)
132 #endif
133
134 } // namespace gil
135 } // namespace boost
136
137 #endif