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_SCANLINE_READ_HPP
9 #define BOOST_GIL_EXTENSION_IO_TIFF_DETAIL_SCANLINE_READ_HPP
11 #include <boost/gil/extension/io/tiff/detail/device.hpp>
12 #include <boost/gil/extension/io/tiff/detail/is_allowed.hpp>
13 #include <boost/gil/extension/io/tiff/detail/reader_backend.hpp>
15 #include <boost/gil/io/base.hpp>
16 #include <boost/gil/io/bit_operations.hpp>
17 #include <boost/gil/io/conversion_policies.hpp>
18 #include <boost/gil/io/device.hpp>
19 #include <boost/gil/io/reader_base.hpp>
20 #include <boost/gil/io/row_buffer_helper.hpp>
21 #include <boost/gil/io/scanline_read_iterator.hpp>
26 #include <type_traits>
29 // taken from jpegxx - https://bitbucket.org/edd/jpegxx/src/ea2492a1a4a6/src/ijg_headers.hpp
30 #ifndef BOOST_GIL_EXTENSION_IO_TIFF_C_LIB_COMPILED_AS_CPLUSPLUS
37 #ifndef BOOST_GIL_EXTENSION_IO_TIFF_C_LIB_COMPILED_AS_CPLUSPLUS
41 namespace boost { namespace gil {
44 /// TIFF scanline reader
46 template< typename Device >
47 class scanline_reader< Device
50 : public reader_backend< Device
56 using tag_t = tiff_tag;
57 using backend_t = reader_backend<Device, tag_t>;
58 using this_t = scanline_reader<Device, tag_t>;
59 using iterator_t = scanline_read_iterator<this_t>;
61 scanline_reader( Device& device
62 , const image_read_settings< tiff_tag >& settings
71 /// Read part of image defined by View and return the data.
72 void read( byte_t* dst, int pos )
74 _read_function( this, dst, pos );
77 /// Skip over a scanline.
78 void skip( byte_t* dst, int pos )
80 this->_read_function( this, dst, pos );
83 iterator_t begin() { return iterator_t( *this ); }
84 iterator_t end() { return iterator_t( *this, this->_info._height ); }
90 io_error_if( this->_info._is_tiled
91 , "scanline_reader doesn't support tiled tiff images."
94 if( this->_info._photometric_interpretation == PHOTOMETRIC_PALETTE )
97 this->_scanline_length = this->_info._width
98 * num_channels< rgb16_view_t >::value
99 * sizeof( channel_type<rgb16_view_t>::type );
101 this->_io_dev.get_field_defaulted( this->_red
106 _buffer = std::vector< byte_t >( this->_io_dev.get_scanline_size() );
108 switch( this->_info._bits_per_sample )
112 using channel_t = channel_type<get_pixel_type<gray1_image_t::view_t>::type>::type;
114 int num_colors = channel_traits< channel_t >::max_value() + 1;
116 this->_palette = planar_rgb_view( num_colors
121 , sizeof(uint16_t) * num_colors
124 _read_function = std::mem_fn(&this_t::read_1_bit_index_image);
131 using channel_t = channel_type<get_pixel_type<gray2_image_t::view_t>::type>::type;
133 int num_colors = channel_traits< channel_t >::max_value() + 1;
135 this->_palette = planar_rgb_view( num_colors
140 , sizeof(uint16_t) * num_colors
143 _read_function = std::mem_fn(&this_t::read_2_bits_index_image);
149 using channel_t = channel_type<get_pixel_type<gray4_image_t::view_t>::type>::type;
151 int num_colors = channel_traits< channel_t >::max_value() + 1;
153 this->_palette = planar_rgb_view( num_colors
158 , sizeof(uint16_t) * num_colors
161 _read_function = std::mem_fn(&this_t::read_4_bits_index_image);
168 using channel_t = channel_type<get_pixel_type<gray8_image_t::view_t>::type>::type;
170 int num_colors = channel_traits< channel_t >::max_value() + 1;
172 this->_palette = planar_rgb_view( num_colors
177 , sizeof(uint16_t) * num_colors
180 _read_function = std::mem_fn(&this_t::read_8_bits_index_image);
187 using channel_t = channel_type<get_pixel_type<gray16_image_t::view_t>::type>::type;
189 int num_colors = channel_traits< channel_t >::max_value() + 1;
191 this->_palette = planar_rgb_view( num_colors
196 , sizeof(uint16_t) * num_colors
199 _read_function = std::mem_fn(&this_t::read_16_bits_index_image);
206 using channel_t = channel_type<get_pixel_type<gray24_image_t::view_t>::type>::type;
208 int num_colors = channel_traits< channel_t >::max_value() + 1;
210 this->_palette = planar_rgb_view( num_colors
215 , sizeof(uint16_t) * num_colors
218 _read_function = std::mem_fn(&this_t::read_24_bits_index_image);
225 using channel_t = channel_type<get_pixel_type<gray32_image_t::view_t>::type>::type;
227 int num_colors = channel_traits< channel_t >::max_value() + 1;
229 this->_palette = planar_rgb_view( num_colors
234 , sizeof(uint16_t) * num_colors
237 _read_function = std::mem_fn(&this_t::read_32_bits_index_image);
241 default: { io_error( "Not supported palette " ); }
246 this->_scanline_length = this->_io_dev.get_scanline_size();
248 if( this->_info._planar_configuration == PLANARCONFIG_SEPARATE )
250 io_error( "scanline_reader doesn't support planar tiff images." );
252 else if( this->_info._planar_configuration == PLANARCONFIG_CONTIG )
255 // the read_data function needs to know what gil type the source image is
256 // to have the default color converter function correctly
258 switch( this->_info._photometric_interpretation )
260 case PHOTOMETRIC_MINISWHITE:
261 case PHOTOMETRIC_MINISBLACK:
263 switch( this->_info._bits_per_sample )
275 case 32: { _read_function = std::mem_fn(&this_t::read_row); break; }
276 default: { io_error( "Image type is not supported." ); }
282 case PHOTOMETRIC_RGB:
284 switch( this->_info._samples_per_pixel )
288 switch( this->_info._bits_per_sample )
298 case 32: { _read_function = std::mem_fn(&this_t::read_row); break; }
299 default: { io_error( "Image type is not supported." ); }
307 switch( this->_info._bits_per_sample )
317 case 32: { _read_function = std::mem_fn(&this_t::read_row); break; }
318 default: { io_error( "Image type is not supported." ); }
324 default: { io_error( "Image type is not supported." ); }
329 case PHOTOMETRIC_SEPARATED: // CYMK
331 switch( this->_info._bits_per_sample )
341 case 32: { _read_function = std::mem_fn(&this_t::read_row); break; }
342 default: { io_error( "Image type is not supported." ); }
348 default: { io_error( "Image type is not supported." ); }
353 io_error( "Wrong planar configuration setting." );
358 template< typename Src_View >
359 void read_n_bits_row( byte_t* dst, int pos )
361 using dst_view_t = rgb16_view_t;
363 this->_io_dev.read_scanline( _buffer
368 Src_View src_view = interleaved_view( this->_info._width
370 , (typename Src_View::x_iterator) &_buffer.front()
371 , this->_scanline_length
374 dst_view_t dst_view = interleaved_view( this->_info._width
376 , (typename dst_view_t::value_type*) dst
377 , num_channels< dst_view_t >::value * 2 * this->_info._width
381 typename Src_View::x_iterator src_it = src_view.row_begin( 0 );
382 typename dst_view_t::x_iterator dst_it = dst_view.row_begin( 0 );
384 for( dst_view_t::x_coord_t i = 0
385 ; i < this->_info._width
386 ; ++i, src_it++, dst_it++
389 auto const c = static_cast<std::uint16_t>(get_color(*src_it, gray_color_t()));
390 *dst_it = this->_palette[c];
394 void read_1_bit_index_image( byte_t* dst, int pos )
396 read_n_bits_row< gray1_image_t::view_t >( dst, pos );
399 void read_2_bits_index_image( byte_t* dst, int pos )
401 read_n_bits_row< gray2_image_t::view_t >( dst, pos );
404 void read_4_bits_index_image( byte_t* dst, int pos )
406 read_n_bits_row< gray4_image_t::view_t >( dst, pos );
409 void read_8_bits_index_image( byte_t* dst, int pos )
411 read_n_bits_row< gray8_image_t::view_t >( dst, pos );
414 void read_16_bits_index_image( byte_t* dst, int pos )
416 read_n_bits_row< gray16_image_t::view_t >( dst, pos );
419 void read_24_bits_index_image( byte_t* dst, int pos )
421 read_n_bits_row< gray24_image_t::view_t >( dst, pos );
424 void read_32_bits_index_image( byte_t* dst, int pos )
426 read_n_bits_row< gray32_image_t::view_t >( dst, pos );
429 void read_row(byte_t* dst, int pos )
431 this->_io_dev.read_scanline( dst
439 std::vector< byte_t> _buffer;
440 detail::mirror_bits<std::vector<byte_t>, std::true_type> _mirror_bites;
441 std::function<void(this_t*, byte_t*, int)> _read_function;