device.hpp 14 KB

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