device.hpp 14 KB

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