device.hpp 14 KB

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