device.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. //
  2. // Copyright 2007-2012 Christian Henning, Andreas Pokorny
  3. // Copyright 2024 Dirk Stolle
  4. //
  5. // Distributed under the Boost Software License, Version 1.0
  6. // See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt
  8. //
  9. #ifndef BOOST_GIL_IO_DEVICE_HPP
  10. #define BOOST_GIL_IO_DEVICE_HPP
  11. #include <boost/gil/detail/mp11.hpp>
  12. #include <boost/gil/io/base.hpp>
  13. #include <cstdio>
  14. #include <memory>
  15. #include <type_traits>
  16. namespace boost { namespace gil {
  17. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  18. #pragma warning(push)
  19. #pragma warning(disable:4512) //assignment operator could not be generated
  20. #endif
  21. namespace detail {
  22. template < typename T > struct buff_item
  23. {
  24. static const unsigned int size = sizeof( T );
  25. };
  26. template <> struct buff_item< void >
  27. {
  28. static const unsigned int size = 1;
  29. };
  30. /*!
  31. * Implements the IODevice concept c.f. to \ref IODevice required by Image libraries like
  32. * libjpeg and libpng.
  33. *
  34. * \todo switch to a sane interface as soon as there is
  35. * something good in boost. I.E. the IOChains library
  36. * would fit very well here.
  37. *
  38. * This implementation is based on FILE*.
  39. */
  40. template< typename FormatTag >
  41. class file_stream_device
  42. {
  43. public:
  44. using format_tag_t = FormatTag;
  45. public:
  46. /// Used to overload the constructor.
  47. struct read_tag {};
  48. struct write_tag {};
  49. ///
  50. /// Constructor
  51. ///
  52. file_stream_device( const std::string& file_name
  53. , read_tag tag = read_tag()
  54. )
  55. : file_stream_device(file_name.c_str(), tag)
  56. {}
  57. ///
  58. /// Constructor
  59. ///
  60. file_stream_device( const char* file_name
  61. , read_tag = read_tag()
  62. )
  63. {
  64. FILE* file = nullptr;
  65. io_error_if( ( file = fopen( file_name, "rb" )) == nullptr
  66. , "file_stream_device: failed to open file for reading"
  67. );
  68. _file = file_ptr_t( file
  69. , file_deleter
  70. );
  71. }
  72. ///
  73. /// Constructor
  74. ///
  75. file_stream_device( const std::string& file_name
  76. , write_tag tag
  77. )
  78. : file_stream_device(file_name.c_str(), tag)
  79. {}
  80. ///
  81. /// Constructor
  82. ///
  83. file_stream_device( const char* file_name
  84. , write_tag
  85. )
  86. {
  87. FILE* file = nullptr;
  88. io_error_if( ( file = fopen( file_name, "wb" )) == nullptr
  89. , "file_stream_device: failed to open file for writing"
  90. );
  91. _file = file_ptr_t( file
  92. , file_deleter
  93. );
  94. }
  95. ///
  96. /// Constructor
  97. ///
  98. file_stream_device( FILE* file )
  99. : _file( file
  100. , file_deleter
  101. )
  102. {}
  103. auto get() -> FILE* { return _file.get(); }
  104. auto get() const -> FILE const* { return _file.get(); }
  105. int getc_unchecked()
  106. {
  107. return std::getc( get() );
  108. }
  109. char getc()
  110. {
  111. int ch;
  112. io_error_if( ( ch = std::getc( get() )) == EOF
  113. , "file_stream_device: unexpected EOF"
  114. );
  115. return ( char ) ch;
  116. }
  117. ///@todo: change byte_t* to void*
  118. auto read(byte_t* data, std::size_t count) -> std::size_t
  119. {
  120. std::size_t num_elements = fread( data
  121. , 1
  122. , static_cast<int>( count )
  123. , get()
  124. );
  125. ///@todo: add compiler symbol to turn error checking on and off.
  126. io_error_if( ferror( get() )
  127. , "file_stream_device: file read error"
  128. );
  129. //libjpeg sometimes reads blocks in 4096 bytes even when the file is smaller than that.
  130. //return value indicates how much was actually read
  131. //returning less than "count" is not an error
  132. return num_elements;
  133. }
  134. /// Reads array
  135. template< typename T, int N>
  136. void read( T (&buf)[N] )
  137. {
  138. io_error_if( read( buf, N ) < N
  139. , "file_stream_device: file read error"
  140. );
  141. }
  142. /// Reads byte
  143. uint8_t read_uint8()
  144. {
  145. byte_t m[1];
  146. read( m );
  147. return m[0];
  148. }
  149. /// Reads 16 bit little endian integer
  150. uint16_t read_uint16()
  151. {
  152. byte_t m[2];
  153. read( m );
  154. return (m[1] << 8) | m[0];
  155. }
  156. /// Reads 32 bit little endian integer
  157. uint32_t read_uint32()
  158. {
  159. byte_t m[4];
  160. read( m );
  161. return (m[3] << 24) | (m[2] << 16) | (m[1] << 8) | m[0];
  162. }
  163. /// Writes number of elements from a buffer
  164. template < typename T >
  165. auto write(T const* buf, std::size_t count) -> std::size_t
  166. {
  167. std::size_t num_elements = fwrite( buf
  168. , buff_item<T>::size
  169. , count
  170. , get()
  171. );
  172. //return value indicates how much was actually written
  173. //returning less than "count" is not an error
  174. return num_elements;
  175. }
  176. /// Writes array
  177. template < typename T
  178. , std::size_t N
  179. >
  180. void write( const T (&buf)[N] )
  181. {
  182. io_error_if( write( buf, N ) < N
  183. , "file_stream_device: file write error"
  184. );
  185. return ;
  186. }
  187. /// Writes byte
  188. void write_uint8( uint8_t x )
  189. {
  190. byte_t m[1] = { x };
  191. write(m);
  192. }
  193. /// Writes 16 bit little endian integer
  194. void write_uint16( uint16_t x )
  195. {
  196. byte_t m[2];
  197. m[0] = byte_t( x >> 0 );
  198. m[1] = byte_t( x >> 8 );
  199. write( m );
  200. }
  201. /// Writes 32 bit little endian integer
  202. void write_uint32( uint32_t x )
  203. {
  204. byte_t m[4];
  205. m[0] = byte_t( x >> 0 );
  206. m[1] = byte_t( x >> 8 );
  207. m[2] = byte_t( x >> 16 );
  208. m[3] = byte_t( x >> 24 );
  209. write( m );
  210. }
  211. void seek( long count, int whence = SEEK_SET )
  212. {
  213. io_error_if( fseek( get()
  214. , count
  215. , whence
  216. ) != 0
  217. , "file_stream_device: file seek error"
  218. );
  219. }
  220. long int tell()
  221. {
  222. long int pos = ftell( get() );
  223. io_error_if( pos == -1L
  224. , "file_stream_device: file position error"
  225. );
  226. return pos;
  227. }
  228. void flush()
  229. {
  230. fflush( get() );
  231. }
  232. /// Prints formatted ASCII text
  233. void print_line( const std::string& line )
  234. {
  235. std::size_t num_elements = fwrite( line.c_str()
  236. , sizeof( char )
  237. , line.size()
  238. , get()
  239. );
  240. io_error_if( num_elements < line.size()
  241. , "file_stream_device: line print error"
  242. );
  243. }
  244. int error()
  245. {
  246. return ferror( get() );
  247. }
  248. private:
  249. static void file_deleter( FILE* file )
  250. {
  251. if( file )
  252. {
  253. fclose( file );
  254. }
  255. }
  256. private:
  257. using file_ptr_t = std::shared_ptr<FILE> ;
  258. file_ptr_t _file;
  259. };
  260. /**
  261. * Input stream device
  262. */
  263. template< typename FormatTag >
  264. class istream_device
  265. {
  266. public:
  267. istream_device( std::istream& in )
  268. : _in( in )
  269. {
  270. // does the file exists?
  271. io_error_if( !in
  272. , "istream_device: Stream is not valid."
  273. );
  274. }
  275. int getc_unchecked()
  276. {
  277. return _in.get();
  278. }
  279. char getc()
  280. {
  281. int ch;
  282. io_error_if( ( ch = _in.get() ) == EOF
  283. , "istream_device: unexpected EOF"
  284. );
  285. return ( char ) ch;
  286. }
  287. std::size_t read( byte_t* data
  288. , std::size_t count )
  289. {
  290. std::streamsize cr = 0;
  291. do
  292. {
  293. _in.peek();
  294. std::streamsize c = _in.readsome( reinterpret_cast< char* >( data )
  295. , static_cast< std::streamsize >( count ));
  296. count -= static_cast< std::size_t >( c );
  297. data += c;
  298. cr += c;
  299. } while( count && _in );
  300. return static_cast< std::size_t >( cr );
  301. }
  302. /// Reads array
  303. template<typename T, int N>
  304. void read(T (&buf)[N])
  305. {
  306. read(buf, N);
  307. }
  308. /// Reads byte
  309. uint8_t read_uint8()
  310. {
  311. byte_t m[1];
  312. read( m );
  313. return m[0];
  314. }
  315. /// Reads 16 bit little endian integer
  316. uint16_t read_uint16()
  317. {
  318. byte_t m[2];
  319. read( m );
  320. return (m[1] << 8) | m[0];
  321. }
  322. /// Reads 32 bit little endian integer
  323. uint32_t read_uint32()
  324. {
  325. byte_t m[4];
  326. read( m );
  327. return (m[3] << 24) | (m[2] << 16) | (m[1] << 8) | m[0];
  328. }
  329. void seek( long count, int whence = SEEK_SET )
  330. {
  331. _in.seekg( count
  332. , whence == SEEK_SET ? std::ios::beg
  333. :( whence == SEEK_CUR ? std::ios::cur
  334. : std::ios::end )
  335. );
  336. }
  337. long int tell()
  338. {
  339. auto pos = _in.tellg();
  340. io_error_if( pos == std::istream::pos_type(-1)
  341. , "istream_device: file position error"
  342. );
  343. return static_cast<long int>(pos);
  344. }
  345. void write(const byte_t*, std::size_t)
  346. {
  347. io_error( "istream_device: Bad io error." );
  348. }
  349. void flush() {}
  350. int error()
  351. {
  352. return _in.fail();
  353. }
  354. private:
  355. std::istream& _in;
  356. };
  357. /**
  358. * Output stream device
  359. */
  360. template< typename FormatTag >
  361. class ostream_device
  362. {
  363. public:
  364. ostream_device( std::ostream & out )
  365. : _out( out )
  366. {
  367. }
  368. std::size_t read(byte_t *, std::size_t)
  369. {
  370. io_error( "ostream_device: Bad io error." );
  371. return 0;
  372. }
  373. void seek( long count, int whence )
  374. {
  375. _out.seekp( count
  376. , whence == SEEK_SET
  377. ? std::ios::beg
  378. : ( whence == SEEK_CUR
  379. ?std::ios::cur
  380. :std::ios::end )
  381. );
  382. }
  383. long int tell()
  384. {
  385. auto pos = _out.tellp();
  386. io_error_if( pos == std::ostream::pos_type(-1)
  387. , "ostream_device: file position error"
  388. );
  389. return static_cast<long int>(pos);
  390. }
  391. void write( const byte_t* data
  392. , std::size_t count )
  393. {
  394. _out.write( reinterpret_cast<char const*>( data )
  395. , static_cast<std::streamsize>( count )
  396. );
  397. }
  398. /// Writes array
  399. template < typename T
  400. , std::size_t N
  401. >
  402. void write( const T (&buf)[N] )
  403. {
  404. write( buf, N );
  405. }
  406. /// Writes byte
  407. void write_uint8( uint8_t x )
  408. {
  409. byte_t m[1] = { x };
  410. write(m);
  411. }
  412. /// Writes 16 bit little endian integer
  413. void write_uint16( uint16_t x )
  414. {
  415. byte_t m[2];
  416. m[0] = byte_t( x >> 0 );
  417. m[1] = byte_t( x >> 8 );
  418. write( m );
  419. }
  420. /// Writes 32 bit little endian integer
  421. void write_uint32( uint32_t x )
  422. {
  423. byte_t m[4];
  424. m[0] = byte_t( x >> 0 );
  425. m[1] = byte_t( x >> 8 );
  426. m[2] = byte_t( x >> 16 );
  427. m[3] = byte_t( x >> 24 );
  428. write( m );
  429. }
  430. void flush()
  431. {
  432. _out << std::flush;
  433. }
  434. /// Prints formatted ASCII text
  435. void print_line( const std::string& line )
  436. {
  437. _out << line;
  438. }
  439. int error()
  440. {
  441. return _out.fail();
  442. }
  443. private:
  444. std::ostream& _out;
  445. };
  446. /**
  447. * Metafunction to detect input devices.
  448. * Should be replaced by an external facility in the future.
  449. */
  450. template< typename IODevice > struct is_input_device : std::false_type{};
  451. template< typename FormatTag > struct is_input_device< file_stream_device< FormatTag > > : std::true_type{};
  452. template< typename FormatTag > struct is_input_device< istream_device< FormatTag > > : std::true_type{};
  453. template< typename FormatTag
  454. , typename T
  455. , typename D = void
  456. >
  457. struct is_adaptable_input_device : std::false_type{};
  458. template <typename FormatTag, typename T>
  459. struct is_adaptable_input_device
  460. <
  461. FormatTag,
  462. T,
  463. typename std::enable_if
  464. <
  465. mp11::mp_or
  466. <
  467. std::is_base_of<std::istream, T>,
  468. std::is_same<std::istream, T>
  469. >::value
  470. >::type
  471. > : std::true_type
  472. {
  473. using device_type = istream_device<FormatTag>;
  474. };
  475. template< typename FormatTag >
  476. struct is_adaptable_input_device< FormatTag
  477. , FILE*
  478. , void
  479. >
  480. : std::true_type
  481. {
  482. using device_type = file_stream_device<FormatTag>;
  483. };
  484. ///
  485. /// Metafunction to decide if a given type is an acceptable read device type.
  486. ///
  487. template< typename FormatTag
  488. , typename T
  489. , typename D = void
  490. >
  491. struct is_read_device : std::false_type
  492. {};
  493. template <typename FormatTag, typename T>
  494. struct is_read_device
  495. <
  496. FormatTag,
  497. T,
  498. typename std::enable_if
  499. <
  500. mp11::mp_or
  501. <
  502. is_input_device<FormatTag>,
  503. is_adaptable_input_device<FormatTag, T>
  504. >::value
  505. >::type
  506. > : std::true_type
  507. {
  508. };
  509. /**
  510. * Metafunction to detect output devices.
  511. * Should be replaced by an external facility in the future.
  512. */
  513. template<typename IODevice> struct is_output_device : std::false_type{};
  514. template< typename FormatTag > struct is_output_device< file_stream_device< FormatTag > > : std::true_type{};
  515. template< typename FormatTag > struct is_output_device< ostream_device < FormatTag > > : std::true_type{};
  516. template< typename FormatTag
  517. , typename IODevice
  518. , typename D = void
  519. >
  520. struct is_adaptable_output_device : std::false_type {};
  521. template <typename FormatTag, typename T>
  522. struct is_adaptable_output_device
  523. <
  524. FormatTag,
  525. T,
  526. typename std::enable_if
  527. <
  528. mp11::mp_or
  529. <
  530. std::is_base_of<std::ostream, T>,
  531. std::is_same<std::ostream, T>
  532. >::value
  533. >::type
  534. > : std::true_type
  535. {
  536. using device_type = ostream_device<FormatTag>;
  537. };
  538. template<typename FormatTag> struct is_adaptable_output_device<FormatTag,FILE*,void>
  539. : std::true_type
  540. {
  541. using device_type = file_stream_device<FormatTag>;
  542. };
  543. ///
  544. /// Metafunction to decide if a given type is an acceptable read device type.
  545. ///
  546. template< typename FormatTag
  547. , typename T
  548. , typename D = void
  549. >
  550. struct is_write_device : std::false_type
  551. {};
  552. template <typename FormatTag, typename T>
  553. struct is_write_device
  554. <
  555. FormatTag,
  556. T,
  557. typename std::enable_if
  558. <
  559. mp11::mp_or
  560. <
  561. is_output_device<FormatTag>,
  562. is_adaptable_output_device<FormatTag, T>
  563. >::value
  564. >::type
  565. > : std::true_type
  566. {
  567. };
  568. } // namespace detail
  569. template< typename Device, typename FormatTag > class scanline_reader;
  570. template< typename Device, typename FormatTag, typename ConversionPolicy > class reader;
  571. template< typename Device, typename FormatTag, typename Log = no_log > class writer;
  572. template< typename Device, typename FormatTag > class dynamic_image_reader;
  573. template< typename Device, typename FormatTag, typename Log = no_log > class dynamic_image_writer;
  574. namespace detail {
  575. template< typename T >
  576. struct is_reader : std::false_type
  577. {};
  578. template< typename Device
  579. , typename FormatTag
  580. , typename ConversionPolicy
  581. >
  582. struct is_reader< reader< Device
  583. , FormatTag
  584. , ConversionPolicy
  585. >
  586. > : std::true_type
  587. {};
  588. template< typename T >
  589. struct is_dynamic_image_reader : std::false_type
  590. {};
  591. template< typename Device
  592. , typename FormatTag
  593. >
  594. struct is_dynamic_image_reader< dynamic_image_reader< Device
  595. , FormatTag
  596. >
  597. > : std::true_type
  598. {};
  599. template< typename T >
  600. struct is_writer : std::false_type
  601. {};
  602. template< typename Device
  603. , typename FormatTag
  604. >
  605. struct is_writer< writer< Device
  606. , FormatTag
  607. >
  608. > : std::true_type
  609. {};
  610. template< typename T >
  611. struct is_dynamic_image_writer : std::false_type
  612. {};
  613. template< typename Device
  614. , typename FormatTag
  615. >
  616. struct is_dynamic_image_writer< dynamic_image_writer< Device
  617. , FormatTag
  618. >
  619. > : std::true_type
  620. {};
  621. } // namespace detail
  622. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  623. #pragma warning(pop)
  624. #endif
  625. } // namespace gil
  626. } // namespace boost
  627. #endif