2 // Copyright 2007-2012 Christian Henning, Lubomir Bourdev
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
8 #ifndef BOOST_GIL_EXTENSION_IO_TIFF_DETAIL_WRITE_HPP
9 #define BOOST_GIL_EXTENSION_IO_TIFF_DETAIL_WRITE_HPP
11 #include <boost/gil/extension/io/tiff/tags.hpp>
12 #include <boost/gil/extension/io/tiff/detail/writer_backend.hpp>
13 #include <boost/gil/extension/io/tiff/detail/device.hpp>
15 #include <boost/gil/premultiply.hpp>
16 #include <boost/gil/io/base.hpp>
17 #include <boost/gil/io/device.hpp>
18 #include <boost/gil/io/dynamic_io_new.hpp>
22 #include <type_traits>
30 namespace boost { namespace gil {
32 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
34 #pragma warning(disable:4512) //assignment operator could not be generated
39 template <typename PixelReference>
40 struct my_interleaved_pixel_iterator_type_from_pixel_reference
43 using pixel_t = typename std::remove_reference<PixelReference>::type::value_type;
46 using type = typename iterator_type_from_pixel
56 template< typename Channel
60 struct my_interleaved_pixel_iterator_type_from_pixel_reference< const bit_aligned_pixel_reference< byte_t
66 : public iterator_type_from_pixel< const bit_aligned_pixel_reference< uint8_t
76 struct tiff_write_is_supported
78 template< typename View >
80 : public is_write_supported< typename get_pixel_type< View >::type
91 template < typename Device, typename Log >
96 : public writer_backend< Device
101 using backend_t = writer_backend<Device, tiff_tag>;
105 writer( const Device& io_dev
106 , const image_write_info< tiff_tag >& info
113 template<typename View>
114 void apply( const View& view )
121 template< typename View >
122 void write_view( const View& view )
124 using pixel_t = typename View::value_type;
125 // get the type of the first channel (heterogeneous pixels might be broken for now!)
126 using channel_t = typename channel_traits<typename element_type<pixel_t>::type>::value_type;
127 tiff_bits_per_sample::type bits_per_sample = detail::unsigned_integral_num_bits< channel_t >::value;
129 tiff_samples_per_pixel::type samples_per_pixel = num_channels< pixel_t >::value;
131 this->write_header( view );
133 if( this->_info._is_tiled == false )
136 , (view.width() * samples_per_pixel * bits_per_sample + 7) / 8
137 , typename is_bit_aligned< pixel_t >::type()
142 tiff_tile_width::type tw = this->_info._tile_width;
143 tiff_tile_length::type th = this->_info._tile_length;
145 if(!this->_io_dev.check_tile_size( tw, th ))
147 io_error( "Tile sizes need to be multiples of 16." );
151 this->_io_dev.template set_property<tiff_tile_width> ( tw );
152 this->_io_dev.template set_property<tiff_tile_length>( th );
154 write_tiled_data( view
157 , typename is_bit_aligned< pixel_t >::type()
162 //////////////////////////////
164 template<typename View>
165 void write_bit_aligned_view_to_dev( const View& view
166 , const std::size_t row_size_in_bytes
167 , const std::true_type& // has_alpha
170 byte_vector_t row( row_size_in_bytes );
172 using x_it_t = typename View::x_iterator;
173 x_it_t row_it = x_it_t( &(*row.begin()));
175 auto pm_view = premultiply_view <typename View:: value_type> (view);
177 for( typename View::y_coord_t y = 0; y < pm_view.height(); ++y )
179 std::copy( pm_view.row_begin( y )
180 , pm_view.row_end( y )
185 this->_io_dev.write_scaline( row
190 // @todo: do optional bit swapping here if you need to...
194 template<typename View>
195 void write_bit_aligned_view_to_dev( const View& view
196 , const std::size_t row_size_in_bytes
197 , const std::false_type& // has_alpha
200 byte_vector_t row( row_size_in_bytes );
202 using x_it_t = typename View::x_iterator;
203 x_it_t row_it = x_it_t( &(*row.begin()));
205 for( typename View::y_coord_t y = 0; y < view.height(); ++y )
207 std::copy( view.row_begin( y )
213 this->_io_dev.write_scaline( row
218 // @todo: do optional bit swapping here if you need to...
222 /////////////////////////////
224 template< typename View >
225 void write_data( const View& view
226 , std::size_t row_size_in_bytes
227 , const std::true_type& // bit_aligned
230 using colour_space_t = typename color_space_type<typename View::value_type>::type;
231 using has_alpha_t = mp11::mp_contains<colour_space_t, alpha_t>;
233 write_bit_aligned_view_to_dev(view, row_size_in_bytes, has_alpha_t());
236 template< typename View>
237 void write_tiled_data( const View& view
238 , tiff_tile_width::type tw
239 , tiff_tile_length::type th
240 , const std::true_type& // bit_aligned
243 byte_vector_t row( this->_io_dev.get_tile_size() );
245 using x_it_t = typename View::x_iterator;
246 x_it_t row_it = x_it_t( &(*row.begin()));
248 internal_write_tiled_data(view, tw, th, row, row_it);
251 template< typename View >
252 void write_data( const View& view
254 , const std::false_type& // bit_aligned
257 std::vector< pixel< typename channel_type< View >::type
258 , layout<typename color_space_type< View >::type >
260 > row( view.size() );
262 byte_t* row_addr = reinterpret_cast< byte_t* >( &row.front() );
264 // @todo: is there an overhead to doing this when there's no
265 // alpha to premultiply by? I'd hope it's optimised out.
266 auto pm_view = premultiply_view <typename View:: value_type> (view);
268 for( typename View::y_coord_t y = 0; y < pm_view.height(); ++y )
270 std::copy( pm_view.row_begin( y )
271 , pm_view.row_end( y )
275 this->_io_dev.write_scaline( row_addr
280 // @todo: do optional bit swapping here if you need to...
284 template< typename View >
285 void write_tiled_data( const View& view
286 , tiff_tile_width::type tw
287 , tiff_tile_length::type th
288 , const std::false_type& // bit_aligned
291 byte_vector_t row( this->_io_dev.get_tile_size() );
293 using x_iterator = typename detail::my_interleaved_pixel_iterator_type_from_pixel_reference<typename View::reference>::type;
294 x_iterator row_it = x_iterator( &(*row.begin()));
296 internal_write_tiled_data(view, tw, th, row, row_it);
300 //////////////////////////////
302 template< typename View
303 , typename IteratorType
305 void write_tiled_view_to_dev( const View& view
307 , const std::true_type& // has_alpha
310 auto pm_view = premultiply_view <typename View:: value_type>( view );
312 std::copy( pm_view.begin()
319 template< typename View
320 , typename IteratorType
322 void write_tiled_view_to_dev( const View& view
324 , const std::false_type& // has_alpha
327 std::copy( view.begin()
333 /////////////////////////////
337 template< typename View,
338 typename IteratorType
340 void internal_write_tiled_data( const View& view
341 , tiff_tile_width::type tw
342 , tiff_tile_length::type th
347 std::ptrdiff_t i = 0, j = 0;
348 View tile_subimage_view;
349 while( i < view.height() )
351 while( j < view.width() )
353 if( j + tw < view.width() && i + th < view.height() )
355 // a tile is fully included in the image: just copy values
356 tile_subimage_view = subimage_view( view
357 , static_cast< int >( j )
358 , static_cast< int >( i )
359 , static_cast< int >( tw )
360 , static_cast< int >( th )
363 using colour_space_t = typename color_space_type<typename View::value_type>::type;
364 using has_alpha_t = mp11::mp_contains<colour_space_t, alpha_t>;
366 write_tiled_view_to_dev(tile_subimage_view, it, has_alpha_t());
370 std::ptrdiff_t width = view.width();
371 std::ptrdiff_t height = view.height();
373 std::ptrdiff_t current_tile_width = ( j + tw < width ) ? tw : width - j;
374 std::ptrdiff_t current_tile_length = ( i + th < height) ? th : height - i;
376 tile_subimage_view = subimage_view( view
377 , static_cast< int >( j )
378 , static_cast< int >( i )
379 , static_cast< int >( current_tile_width )
380 , static_cast< int >( current_tile_length )
383 for( typename View::y_coord_t y = 0; y < tile_subimage_view.height(); ++y )
385 std::copy( tile_subimage_view.row_begin( y )
386 , tile_subimage_view.row_end( y )
389 std::advance(it, tw);
392 it = IteratorType( &(*row.begin()));
395 this->_io_dev.write_tile( row
396 , static_cast< uint32 >( j )
397 , static_cast< uint32 >( i )
406 // @todo: do optional bit swapping here if you need to...
411 /// TIFF Dynamic Image Writer
413 template< typename Device >
414 class dynamic_image_writer< Device
417 : public writer< Device
421 using parent_t = writer<Device, tiff_tag>;
425 dynamic_image_writer( const Device& io_dev
426 , const image_write_info< tiff_tag >& info
433 template< typename Views >
434 void apply( const any_image_view< Views >& views )
436 detail::dynamic_io_fnobj< detail::tiff_write_is_supported
440 apply_operation( views, op );
444 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)