8 #ifndef BOOST_GIL_IO_DEVICE_HPP 9 #define BOOST_GIL_IO_DEVICE_HPP 11 #include <boost/gil/detail/mp11.hpp> 12 #include <boost/gil/io/base.hpp> 16 #include <type_traits> 18 namespace boost {
namespace gil {
20 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) 22 #pragma warning(disable:4512) //assignment operator could not be generated 27 template <
typename T >
struct buff_item
29 static const unsigned int size =
sizeof( T );
32 template <>
struct buff_item< void >
34 static const unsigned int size = 1;
47 template<
typename FormatTag >
52 using format_tag_t = FormatTag;
78 io_error_if( ( file = fopen( file_name,
"rb" )) ==
nullptr 79 ,
"file_stream_device: failed to open file for reading" 82 _file = file_ptr_t( file
103 FILE* file =
nullptr;
105 io_error_if( ( file = fopen( file_name,
"wb" )) ==
nullptr 106 ,
"file_stream_device: failed to open file for writing" 109 _file = file_ptr_t( file
123 auto get() -> FILE* {
return _file.get(); }
124 auto get() const -> FILE const* {
return _file.get(); }
128 return std::getc( get() );
135 io_error_if( ( ch = std::getc( get() )) == EOF
136 ,
"file_stream_device: unexpected EOF" 143 auto read(byte_t* data, std::size_t count) -> std::size_t
145 std::size_t num_elements = fread( data
147 , static_cast<int>( count )
152 io_error_if( ferror( get() )
153 ,
"file_stream_device: file read error" 163 template<
typename T,
int N>
166 io_error_if(
read( buf, N ) < N
167 ,
"file_stream_device: file read error" 186 return (m[1] << 8) | m[0];
195 return (m[3] << 24) | (m[2] << 16) | (m[1] << 8) | m[0];
199 template <
typename T >
200 auto write(T
const* buf, std::size_t count) -> std::size_t
202 std::size_t num_elements = fwrite( buf
214 template <
typename T
219 io_error_if(
write( buf, N ) < N
220 ,
"file_stream_device: file write error" 237 m[0] = byte_t( x >> 0 );
238 m[1] = byte_t( x >> 8 );
248 m[0] = byte_t( x >> 0 );
249 m[1] = byte_t( x >> 8 );
250 m[2] = byte_t( x >> 16 );
251 m[3] = byte_t( x >> 24 );
256 void seek(
long count,
int whence = SEEK_SET )
258 io_error_if( fseek( get()
262 ,
"file_stream_device: file seek error" 268 long int pos = ftell( get() );
270 io_error_if( pos == -1L
271 ,
"file_stream_device: file position error" 285 std::size_t num_elements = fwrite( line.c_str()
291 io_error_if( num_elements < line.size()
292 ,
"file_stream_device: line print error" 298 return ferror( get() );
303 static void file_deleter( FILE* file )
313 using file_ptr_t = std::shared_ptr<FILE> ;
320 template<
typename FormatTag >
329 ,
"istream_device: Stream is not valid." 342 io_error_if( ( ch = _in.get() ) == EOF
343 ,
"istream_device: unexpected EOF" 349 std::size_t read( byte_t* data
350 , std::size_t count )
352 std::streamsize cr = 0;
357 std::streamsize c = _in.readsome( reinterpret_cast< char* >( data )
358 , static_cast< std::streamsize >( count ));
360 count -= static_cast< std::size_t >( c );
364 }
while( count && _in );
366 return static_cast< std::size_t >( cr );
370 template<
typename T,
int N>
391 return (m[1] << 8) | m[0];
400 return (m[3] << 24) | (m[2] << 16) | (m[1] << 8) | m[0];
403 void seek(
long count,
int whence = SEEK_SET )
406 , whence == SEEK_SET ? std::ios::beg
407 :( whence == SEEK_CUR ? std::ios::cur
412 void write(
const byte_t*, std::size_t)
414 io_error(
"istream_device: Bad io error." );
427 template<
typename FormatTag >
436 std::size_t read(byte_t *, std::size_t)
438 io_error(
"ostream_device: Bad io error." );
442 void seek(
long count,
int whence )
447 : ( whence == SEEK_CUR
453 void write(
const byte_t* data
454 , std::size_t count )
456 _out.write( reinterpret_cast<char const*>( data )
457 , static_cast<std::streamsize>( count )
462 template <
typename T
482 m[0] = byte_t( x >> 0 );
483 m[1] = byte_t( x >> 8 );
493 m[0] = byte_t( x >> 0 );
494 m[1] = byte_t( x >> 8 );
495 m[2] = byte_t( x >> 16 );
496 m[3] = byte_t( x >> 24 );
526 template<
typename FormatTag >
struct is_input_device< istream_device< FormatTag > > : std::true_type{};
528 template<
typename FormatTag
532 struct is_adaptable_input_device : std::false_type{};
534 template <
typename FormatTag,
typename T>
535 struct is_adaptable_input_device
539 typename std::enable_if
543 std::is_base_of<std::istream, T>,
544 std::is_same<std::istream, T>
549 using device_type = istream_device<FormatTag>;
552 template<
typename FormatTag >
553 struct is_adaptable_input_device< FormatTag
559 using device_type = file_stream_device<FormatTag>;
565 template<
typename FormatTag
572 template <
typename FormatTag,
typename T>
577 typename std::enable_if
581 is_input_device<FormatTag>,
582 is_adaptable_input_device<FormatTag, T>
597 template<
typename FormatTag >
struct is_output_device< ostream_device < FormatTag > > : std::true_type{};
599 template<
typename FormatTag
603 struct is_adaptable_output_device : std::false_type {};
605 template <
typename FormatTag,
typename T>
606 struct is_adaptable_output_device
610 typename std::enable_if
614 std::is_base_of<std::ostream, T>,
615 std::is_same<std::ostream, T>
620 using device_type = ostream_device<FormatTag>;
623 template<
typename FormatTag>
struct is_adaptable_output_device<FormatTag,FILE*,
void>
626 using device_type = file_stream_device<FormatTag>;
633 template<
typename FormatTag
640 template <
typename FormatTag,
typename T>
645 typename std::enable_if
649 is_output_device<FormatTag>,
650 is_adaptable_output_device<FormatTag, T>
659 template<
typename Device,
typename FormatTag >
class scanline_reader;
660 template<
typename Device,
typename FormatTag,
typename ConversionPolicy >
class reader;
662 template<
typename Device,
typename FormatTag,
typename Log = no_log >
class writer;
664 template<
typename Device,
typename FormatTag >
class dynamic_image_reader;
665 template<
typename Device,
typename FormatTag,
typename Log = no_log >
class dynamic_image_writer;
670 template<
typename T >
671 struct is_reader : std::false_type
674 template<
typename Device
676 ,
typename ConversionPolicy
678 struct is_reader< reader< Device
685 template<
typename T >
686 struct is_dynamic_image_reader : std::false_type
689 template<
typename Device
692 struct is_dynamic_image_reader< dynamic_image_reader< Device
698 template<
typename T >
699 struct is_writer : std::false_type
702 template<
typename Device
705 struct is_writer< writer< Device
711 template<
typename T >
712 struct is_dynamic_image_writer : std::false_type
715 template<
typename Device
718 struct is_dynamic_image_writer< dynamic_image_writer< Device
726 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
void write_uint8(uint8_t x)
Writes byte.
Definition: device.hpp:471
file_stream_device(const std::string &file_name, write_tag tag)
Definition: device.hpp:90
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
void print_line(const std::string &line)
Prints formatted ASCII text.
Definition: device.hpp:283
void read(T(&buf)[N])
Reads array.
Definition: device.hpp:371
auto read(byte_t *data, std::size_t count) -> std::size_t
Definition: device.hpp:143
uint16_t read_uint16()
Reads 16 bit little endian integer.
Definition: device.hpp:181
auto write(T const *buf, std::size_t count) -> std::size_t
Writes number of elements from a buffer.
Definition: device.hpp:200
uint16_t read_uint16()
Reads 16 bit little endian integer.
Definition: device.hpp:386
void write(const T(&buf)[N])
Writes array.
Definition: device.hpp:217
Definition: device.hpp:569
void write_uint32(uint32_t x)
Writes 32 bit little endian integer.
Definition: device.hpp:244
void write_uint8(uint8_t x)
Writes byte.
Definition: device.hpp:226
uint32_t read_uint32()
Reads 32 bit little endian integer.
Definition: device.hpp:190
void write_uint16(uint16_t x)
Writes 16 bit little endian integer.
Definition: device.hpp:233
uint8_t read_uint8()
Reads byte.
Definition: device.hpp:377
Used to overload the constructor.
Definition: device.hpp:57
uint8_t read_uint8()
Reads byte.
Definition: device.hpp:172
Definition: device.hpp:594
uint32_t read_uint32()
Reads 32 bit little endian integer.
Definition: device.hpp:395
file_stream_device(const char *file_name, read_tag=read_tag())
Definition: device.hpp:72
void write(const T(&buf)[N])
Writes array.
Definition: device.hpp:465
file_stream_device(const std::string &file_name, read_tag tag=read_tag())
Definition: device.hpp:63
Definition: device.hpp:48
Definition: device.hpp:637
void write_uint16(uint16_t x)
Writes 16 bit little endian integer.
Definition: device.hpp:478
Definition: device.hpp:428
Definition: device.hpp:321
file_stream_device(FILE *file)
Definition: device.hpp:117
void print_line(const std::string &line)
Prints formatted ASCII text.
Definition: device.hpp:507
void read(T(&buf)[N])
Reads array.
Definition: device.hpp:164
file_stream_device(const char *file_name, write_tag)
Definition: device.hpp:99
void write_uint32(uint32_t x)
Writes 32 bit little endian integer.
Definition: device.hpp:489