reader_backend.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_READER_BACKEND_HPP
  9. #define BOOST_GIL_EXTENSION_IO_TIFF_DETAIL_READER_BACKEND_HPP
  10. #include <boost/gil/extension/io/tiff/tags.hpp>
  11. namespace boost { namespace gil {
  12. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  13. #pragma warning(push)
  14. #pragma warning(disable:4512) //assignment operator could not be generated
  15. #endif
  16. ///
  17. /// TIFF Backend
  18. ///
  19. template< typename Device >
  20. struct reader_backend< Device
  21. , tiff_tag
  22. >
  23. {
  24. public:
  25. using format_tag_t = tiff_tag;
  26. public:
  27. reader_backend( const Device& io_dev
  28. , const image_read_settings< tiff_tag >& settings
  29. )
  30. : _io_dev ( io_dev )
  31. , _settings( settings )
  32. , _info()
  33. , _scanline_length( 0 )
  34. , _red ( nullptr )
  35. , _green( nullptr )
  36. , _blue ( nullptr )
  37. {
  38. init_multipage_read( settings );
  39. read_header();
  40. if( _settings._dim.x == 0 )
  41. {
  42. _settings._dim.x = _info._width;
  43. }
  44. if( _settings._dim.y == 0 )
  45. {
  46. _settings._dim.y = _info._height;
  47. }
  48. }
  49. void read_header()
  50. {
  51. io_error_if( _io_dev.template get_property<tiff_image_width> ( _info._width ) == false
  52. , "cannot read tiff tag." );
  53. io_error_if( _io_dev.template get_property<tiff_image_height> ( _info._height ) == false
  54. , "cannot read tiff tag." );
  55. io_error_if( _io_dev.template get_property<tiff_compression> ( _info._compression ) == false
  56. , "cannot read tiff tag." );
  57. io_error_if( _io_dev.template get_property<tiff_samples_per_pixel> ( _info._samples_per_pixel ) == false
  58. , "cannot read tiff tag." );
  59. io_error_if( _io_dev.template get_property<tiff_bits_per_sample> ( _info._bits_per_sample ) == false
  60. , "cannot read tiff tag." );
  61. io_error_if( _io_dev.template get_property<tiff_sample_format> ( _info._sample_format ) == false
  62. , "cannot read tiff tag." );
  63. io_error_if( _io_dev.template get_property<tiff_planar_configuration> ( _info._planar_configuration ) == false
  64. , "cannot read tiff tag." );
  65. io_error_if( _io_dev.template get_property<tiff_photometric_interpretation>( _info._photometric_interpretation ) == false
  66. , "cannot read tiff tag." );
  67. _info._is_tiled = false;
  68. // Tile tags
  69. if( _io_dev.is_tiled() )
  70. {
  71. _info._is_tiled = true;
  72. io_error_if( !_io_dev.template get_property< tiff_tile_width >( _info._tile_width )
  73. , "cannot read tiff_tile_width tag." );
  74. io_error_if( !_io_dev.template get_property< tiff_tile_length >( _info._tile_length )
  75. , "cannot read tiff_tile_length tag." );
  76. }
  77. io_error_if( _io_dev.template get_property<tiff_resolution_unit>( _info._resolution_unit) == false
  78. , "cannot read tiff tag");
  79. io_error_if( _io_dev. template get_property<tiff_x_resolution>( _info._x_resolution ) == false
  80. , "cannot read tiff tag" );
  81. io_error_if( _io_dev. template get_property<tiff_y_resolution>( _info._y_resolution ) == false
  82. , "cannot read tiff tag" );
  83. /// optional and non-baseline properties below here
  84. _io_dev. template get_property <tiff_icc_profile> ( _info._icc_profile );
  85. }
  86. /// Check if image is large enough.
  87. void check_image_size( const point_t& img_dim )
  88. {
  89. if( _settings._dim.x > 0 )
  90. {
  91. if( img_dim.x < _settings._dim.x ) { io_error( "Supplied image is too small" ); }
  92. }
  93. else
  94. {
  95. if( (tiff_image_width::type) img_dim.x < _info._width ) { io_error( "Supplied image is too small" ); }
  96. }
  97. if( _settings._dim.y > 0 )
  98. {
  99. if( img_dim.y < _settings._dim.y ) { io_error( "Supplied image is too small" ); }
  100. }
  101. else
  102. {
  103. if( (tiff_image_height::type) img_dim.y < _info._height ) { io_error( "Supplied image is too small" ); }
  104. }
  105. }
  106. private:
  107. void init_multipage_read( const image_read_settings< tiff_tag >& settings )
  108. {
  109. if( settings._directory > 0 )
  110. {
  111. _io_dev.set_directory( settings._directory );
  112. }
  113. }
  114. public:
  115. Device _io_dev;
  116. image_read_settings< tiff_tag > _settings;
  117. image_read_info< tiff_tag > _info;
  118. std::size_t _scanline_length;
  119. // palette
  120. tiff_color_map::red_t _red;
  121. tiff_color_map::green_t _green;
  122. tiff_color_map::blue_t _blue;
  123. rgb16_planar_view_t _palette;
  124. };
  125. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  126. #pragma warning(pop)
  127. #endif
  128. } // namespace gil
  129. } // namespace boost
  130. #endif