device.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. //
  2. // Copyright 2007-2008 Andreas Pokorny, 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_DEVICE_HPP
  9. #define BOOST_GIL_EXTENSION_IO_TIFF_DETAIL_DEVICE_HPP
  10. #include <boost/gil/extension/io/tiff/tags.hpp>
  11. #include <boost/gil/extension/io/tiff/detail/log.hpp>
  12. #include <boost/gil/detail/mp11.hpp>
  13. #include <boost/gil/io/base.hpp>
  14. #include <boost/gil/io/device.hpp>
  15. #include <algorithm>
  16. #include <cstdint>
  17. #include <memory>
  18. #include <sstream>
  19. #include <type_traits>
  20. // taken from jpegxx - https://bitbucket.org/edd/jpegxx/src/ea2492a1a4a6/src/ijg_headers.hpp
  21. #ifndef BOOST_GIL_EXTENSION_IO_TIFF_C_LIB_COMPILED_AS_CPLUSPLUS
  22. extern "C" {
  23. #endif
  24. #include <tiff.h>
  25. #include <tiffio.h>
  26. #ifndef BOOST_GIL_EXTENSION_IO_TIFF_C_LIB_COMPILED_AS_CPLUSPLUS
  27. }
  28. #endif
  29. #include <tiffio.hxx>
  30. namespace boost { namespace gil { namespace detail {
  31. template <int n_args>
  32. struct get_property_f {
  33. template <typename Property>
  34. bool call_me(typename Property:: type& value, std::shared_ptr<TIFF>& file);
  35. };
  36. template <int n_args>
  37. struct set_property_f {
  38. template <typename Property>
  39. bool call_me(const typename Property:: type& value, std::shared_ptr<TIFF>& file) const;
  40. };
  41. template <> struct get_property_f <1>
  42. {
  43. // For single-valued properties
  44. template <typename Property>
  45. bool call_me(typename Property::type & value, std::shared_ptr<TIFF>& file) const
  46. {
  47. // @todo: defaulted, really?
  48. return (1 == TIFFGetFieldDefaulted( file.get()
  49. , Property:: tag
  50. , & value));
  51. }
  52. };
  53. template <> struct get_property_f <2>
  54. {
  55. // Specialisation for multi-valued properties. @todo: add one of
  56. // these for the three-parameter fields too.
  57. template <typename Property>
  58. bool call_me(typename Property::type& vs, std::shared_ptr<TIFF>& file) const
  59. {
  60. mp11::mp_at<typename Property::arg_types, std::integral_constant<int, 0>> length;
  61. mp11::mp_at<typename Property::arg_types, std::integral_constant<int, 1>> pointer;
  62. if (1 == TIFFGetFieldDefaulted(file.get(), Property:: tag, & length, & pointer))
  63. {
  64. std:: copy_n(static_cast<typename Property::type::const_pointer>(pointer), length, std:: back_inserter(vs));
  65. return true;
  66. } else
  67. return false;
  68. }
  69. };
  70. template <> struct set_property_f <1>
  71. {
  72. // For single-valued properties
  73. template <typename Property>
  74. inline
  75. bool call_me(typename Property:: type const & value, std::shared_ptr<TIFF>& file) const
  76. {
  77. return (1 == TIFFSetField( file.get()
  78. , Property:: tag
  79. , value));
  80. }
  81. };
  82. template <> struct set_property_f <2>
  83. {
  84. // Specialisation for multi-valued properties. @todo: add one
  85. // of these for the three-parameter fields too. Actually we
  86. // will need further templation / specialisation for the
  87. // two-element fields which aren't a length and a data buffer
  88. // (e.g. http://www.awaresystems.be/imaging/tiff/tifftags/dotrange.html
  89. // )
  90. template <typename Property>
  91. inline
  92. bool call_me(typename Property:: type const & values, std::shared_ptr<TIFF>& file) const
  93. {
  94. using length_t = mp11::mp_at_c<typename Property::arg_types, 0>;
  95. auto const length = static_cast<length_t>(values.size());
  96. using pointer_t = mp11::mp_at_c<typename Property::arg_types, 1>;
  97. auto const pointer = static_cast<pointer_t>(&(values.front()));
  98. return (1 == TIFFSetField( file.get(), Property:: tag, length, pointer));
  99. }
  100. };
  101. template< typename Log >
  102. class tiff_device_base
  103. {
  104. public:
  105. using tiff_file_t = std::shared_ptr<TIFF>;
  106. tiff_device_base()
  107. {}
  108. tiff_device_base( TIFF* tiff_file )
  109. : _tiff_file( tiff_file
  110. , TIFFClose )
  111. {}
  112. template <typename Property>
  113. bool get_property( typename Property::type& value )
  114. {
  115. return get_property_f<mp11::mp_size<typename Property::arg_types>::value>().template call_me<Property>(value, _tiff_file);
  116. }
  117. template <typename Property>
  118. inline
  119. bool set_property( const typename Property::type& value )
  120. {
  121. // http://www.remotesensing.org/libtiff/man/TIFFSetField.3tiff.html
  122. return set_property_f<mp11::mp_size<typename Property::arg_types>::value>().template call_me<Property>(value, _tiff_file);
  123. }
  124. // TIFFIsByteSwapped returns a non-zero value if the image data was in a different
  125. // byte-order than the host machine. Zero is returned if the TIFF file and local
  126. // host byte-orders are the same. Note that TIFFReadTile(), TIFFReadStrip() and TIFFReadScanline()
  127. // functions already normally perform byte swapping to local host order if needed.
  128. bool are_bytes_swapped()
  129. {
  130. return ( TIFFIsByteSwapped( _tiff_file.get() )) ? true : false;
  131. }
  132. bool is_tiled() const
  133. {
  134. return ( TIFFIsTiled( _tiff_file.get() )) ? true : false;
  135. }
  136. unsigned int get_default_strip_size()
  137. {
  138. return TIFFDefaultStripSize( _tiff_file.get()
  139. , 0 );
  140. }
  141. std::size_t get_scanline_size()
  142. {
  143. return TIFFScanlineSize( _tiff_file.get() );
  144. }
  145. std::size_t get_tile_size()
  146. {
  147. return TIFFTileSize( _tiff_file.get() );
  148. }
  149. int get_field_defaulted( uint16_t*& red
  150. , uint16_t*& green
  151. , uint16_t*& blue
  152. )
  153. {
  154. return TIFFGetFieldDefaulted( _tiff_file.get()
  155. , TIFFTAG_COLORMAP
  156. , &red
  157. , &green
  158. , &blue
  159. );
  160. }
  161. template< typename Buffer >
  162. void read_scanline( Buffer& buffer
  163. , std::ptrdiff_t row
  164. , tsample_t plane
  165. )
  166. {
  167. io_error_if( TIFFReadScanline( _tiff_file.get()
  168. , reinterpret_cast< tdata_t >( &buffer.front() )
  169. , static_cast<std::uint32_t>( row )
  170. , plane ) == -1
  171. , "Read error."
  172. );
  173. }
  174. void read_scanline( byte_t* buffer
  175. , std::ptrdiff_t row
  176. , tsample_t plane
  177. )
  178. {
  179. io_error_if( TIFFReadScanline( _tiff_file.get()
  180. , reinterpret_cast< tdata_t >( buffer )
  181. , static_cast<std::uint32_t>( row )
  182. , plane ) == -1
  183. , "Read error."
  184. );
  185. }
  186. template< typename Buffer >
  187. void read_tile( Buffer& buffer
  188. , std::ptrdiff_t x
  189. , std::ptrdiff_t y
  190. , std::ptrdiff_t z
  191. , tsample_t plane
  192. )
  193. {
  194. if( TIFFReadTile( _tiff_file.get()
  195. , reinterpret_cast< tdata_t >( &buffer.front() )
  196. , static_cast< std::uint32_t >( x )
  197. , static_cast< std::uint32_t >( y )
  198. , static_cast< std::uint32_t >( z )
  199. , plane
  200. ) == -1 )
  201. {
  202. std::ostringstream oss;
  203. oss << "Read tile error (" << x << "," << y << "," << z << "," << plane << ").";
  204. io_error(oss.str().c_str());
  205. }
  206. }
  207. template< typename Buffer >
  208. void write_scaline( Buffer& buffer
  209. , std::uint32_t row
  210. , tsample_t plane
  211. )
  212. {
  213. io_error_if( TIFFWriteScanline( _tiff_file.get()
  214. , &buffer.front()
  215. , row
  216. , plane
  217. ) == -1
  218. , "Write error"
  219. );
  220. }
  221. void write_scaline( byte_t* buffer
  222. , std::uint32_t row
  223. , tsample_t plane
  224. )
  225. {
  226. io_error_if( TIFFWriteScanline( _tiff_file.get()
  227. , buffer
  228. , row
  229. , plane
  230. ) == -1
  231. , "Write error"
  232. );
  233. }
  234. template< typename Buffer >
  235. void write_tile( Buffer& buffer
  236. , std::uint32_t x
  237. , std::uint32_t y
  238. , std::uint32_t z
  239. , tsample_t plane
  240. )
  241. {
  242. if( TIFFWriteTile( _tiff_file.get()
  243. , &buffer.front()
  244. , x
  245. , y
  246. , z
  247. , plane
  248. ) == -1 )
  249. {
  250. std::ostringstream oss;
  251. oss << "Write tile error (" << x << "," << y << "," << z << "," << plane << ").";
  252. io_error(oss.str().c_str());
  253. }
  254. }
  255. void set_directory( tdir_t directory )
  256. {
  257. io_error_if( TIFFSetDirectory( _tiff_file.get()
  258. , directory
  259. ) != 1
  260. , "Failing to set directory"
  261. );
  262. }
  263. // return false if the given tile width or height is not TIFF compliant (multiple of 16) or larger than image size, true otherwise
  264. bool check_tile_size( tiff_tile_width::type& width
  265. , tiff_tile_length::type& height
  266. )
  267. {
  268. bool result = true;
  269. std::uint32_t tw = static_cast< std::uint32_t >( width );
  270. std::uint32_t th = static_cast< std::uint32_t >( height );
  271. TIFFDefaultTileSize( _tiff_file.get()
  272. , &tw
  273. , &th
  274. );
  275. if(width==0 || width%16!=0)
  276. {
  277. width = tw;
  278. result = false;
  279. }
  280. if(height==0 || height%16!=0)
  281. {
  282. height = th;
  283. result = false;
  284. }
  285. return result;
  286. }
  287. protected:
  288. tiff_file_t _tiff_file;
  289. Log _log;
  290. };
  291. /*!
  292. *
  293. * file_stream_device specialization for tiff images, which are based on TIFF*.
  294. */
  295. template<>
  296. class file_stream_device< tiff_tag > : public tiff_device_base< tiff_no_log >
  297. {
  298. public:
  299. struct read_tag {};
  300. struct write_tag {};
  301. file_stream_device( std::string const& file_name, read_tag )
  302. {
  303. TIFF* tiff;
  304. io_error_if( ( tiff = TIFFOpen( file_name.c_str(), "r" )) == nullptr
  305. , "file_stream_device: failed to open file" );
  306. _tiff_file = tiff_file_t( tiff, TIFFClose );
  307. }
  308. file_stream_device( std::string const& file_name, write_tag )
  309. {
  310. TIFF* tiff;
  311. io_error_if( ( tiff = TIFFOpen( file_name.c_str(), "w" )) == nullptr
  312. , "file_stream_device: failed to open file" );
  313. _tiff_file = tiff_file_t( tiff, TIFFClose );
  314. }
  315. file_stream_device( TIFF* tiff_file )
  316. : tiff_device_base( tiff_file )
  317. {}
  318. };
  319. /*!
  320. *
  321. * ostream_device specialization for tiff images.
  322. */
  323. template<>
  324. class ostream_device< tiff_tag > : public tiff_device_base< tiff_no_log >
  325. {
  326. public:
  327. ostream_device( std::ostream & out )
  328. : _out( out )
  329. {
  330. TIFF* tiff;
  331. io_error_if( ( tiff = TIFFStreamOpen( ""
  332. , &_out
  333. )
  334. ) == nullptr
  335. , "ostream_device: failed to stream"
  336. );
  337. _tiff_file = tiff_file_t( tiff, TIFFClose );
  338. }
  339. private:
  340. ostream_device& operator=( const ostream_device& ) { return *this; }
  341. private:
  342. std::ostream& _out;
  343. };
  344. /*!
  345. *
  346. * ostream_device specialization for tiff images.
  347. */
  348. template<>
  349. class istream_device< tiff_tag > : public tiff_device_base< tiff_no_log >
  350. {
  351. public:
  352. istream_device( std::istream & in )
  353. : _in( in )
  354. {
  355. TIFF* tiff;
  356. io_error_if( ( tiff = TIFFStreamOpen( ""
  357. , &_in
  358. )
  359. ) == nullptr
  360. , "istream_device: failed to stream"
  361. );
  362. _tiff_file = tiff_file_t( tiff, TIFFClose );
  363. }
  364. private:
  365. istream_device& operator=( const istream_device& ) { return *this; }
  366. private:
  367. std::istream& _in;
  368. };
  369. /*
  370. template< typename T, typename D >
  371. struct is_adaptable_input_device< tiff_tag, T, D > : std::false_type {};
  372. */
  373. template<typename FormatTag>
  374. struct is_adaptable_input_device<FormatTag, TIFF*, void> : std::true_type
  375. {
  376. using device_type = file_stream_device<FormatTag>;
  377. };
  378. template<typename FormatTag>
  379. struct is_adaptable_output_device<FormatTag, TIFF*, void> : std::true_type
  380. {
  381. using device_type = file_stream_device<FormatTag>;
  382. };
  383. template <typename Channel>
  384. struct sample_format : std::integral_constant<int, SAMPLEFORMAT_UINT> {};
  385. template<>
  386. struct sample_format<uint8_t> : std::integral_constant<int, SAMPLEFORMAT_UINT> {};
  387. template<>
  388. struct sample_format<uint16_t> : std::integral_constant<int, SAMPLEFORMAT_UINT> {};
  389. template<>
  390. struct sample_format<uint32_t> : std::integral_constant<int, SAMPLEFORMAT_UINT> {};
  391. template<>
  392. struct sample_format<float32_t> : std::integral_constant<int, SAMPLEFORMAT_IEEEFP> {};
  393. template<>
  394. struct sample_format<double> : std::integral_constant<int, SAMPLEFORMAT_IEEEFP> {};
  395. template<>
  396. struct sample_format<int8_t> : std::integral_constant<int, SAMPLEFORMAT_INT> {};
  397. template<>
  398. struct sample_format<int16_t> : std::integral_constant<int, SAMPLEFORMAT_INT> {};
  399. template<>
  400. struct sample_format<int32_t> : std::integral_constant<int, SAMPLEFORMAT_INT> {};
  401. template <typename Channel>
  402. struct photometric_interpretation {};
  403. template<>
  404. struct photometric_interpretation<gray_t>
  405. : std::integral_constant<int, PHOTOMETRIC_MINISBLACK> {};
  406. template<>
  407. struct photometric_interpretation<rgb_t>
  408. : std::integral_constant<int, PHOTOMETRIC_RGB> {};
  409. template<>
  410. struct photometric_interpretation<rgba_t>
  411. : std::integral_constant<int, PHOTOMETRIC_RGB> {};
  412. template<>
  413. struct photometric_interpretation<cmyk_t>
  414. : std::integral_constant<int, PHOTOMETRIC_SEPARATED> {};
  415. } // namespace detail
  416. } // namespace gil
  417. } // namespace boost
  418. #endif