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 FILE*
get() {
return _file.get(); }
124 const FILE*
get()
const {
return _file.get(); }
128 return std::getc(
get() );
135 io_error_if( ( ch = std::getc(
get() )) == EOF
136 ,
"file_stream_device: unexpected EOF" 147 std::size_t num_elements = fread( data
149 , static_cast<int>( count )
154 io_error_if( ferror(
get() )
155 ,
"file_stream_device: file read error" 170 io_error_if( read( buf, N ) < N
171 ,
"file_stream_device: file read error" 190 return (m[1] << 8) | m[0];
199 return (m[3] << 24) | (m[2] << 16) | (m[1] << 8) | m[0];
203 template <
typename T >
208 std::size_t num_elements = fwrite( buf
220 template <
typename T
225 io_error_if( write( buf, N ) < N
226 ,
"file_stream_device: file write error" 243 m[0] = byte_t( x >> 0 );
244 m[1] = byte_t( x >> 8 );
254 m[0] = byte_t( x >> 0 );
255 m[1] = byte_t( x >> 8 );
256 m[2] = byte_t( x >> 16 );
257 m[3] = byte_t( x >> 24 );
262 void seek(
long count,
int whence = SEEK_SET )
264 io_error_if( fseek(
get()
268 ,
"file_stream_device: file seek error" 274 long int pos = ftell(
get() );
276 io_error_if( pos == -1L
277 ,
"file_stream_device: file position error" 291 std::size_t num_elements = fwrite( line.c_str()
297 io_error_if( num_elements < line.size()
298 ,
"file_stream_device: line print error" 304 return ferror(
get() );
309 static void file_deleter( FILE* file )
319 using file_ptr_t = std::shared_ptr<FILE> ;
326 template<
typename FormatTag >
335 ,
"istream_device: Stream is not valid." 348 io_error_if( ( ch = _in.get() ) == EOF
349 ,
"istream_device: unexpected EOF" 355 std::size_t read( byte_t* data
356 , std::size_t count )
358 std::streamsize cr = 0;
363 std::streamsize c = _in.readsome( reinterpret_cast< char* >( data )
364 , static_cast< std::streamsize >( count ));
366 count -=
static_cast< std::size_t
>( c );
370 }
while( count && _in );
372 return static_cast< std::size_t
>( cr );
381 return read( buf, N );
399 return (m[1] << 8) | m[0];
408 return (m[3] << 24) | (m[2] << 16) | (m[1] << 8) | m[0];
411 void seek(
long count,
int whence = SEEK_SET )
414 , whence == SEEK_SET ? std::ios::beg
415 :( whence == SEEK_CUR ? std::ios::cur
420 void write(
const byte_t*, std::size_t)
422 io_error(
"istream_device: Bad io error." );
435 template<
typename FormatTag >
444 std::size_t read(byte_t *, std::size_t)
446 io_error(
"ostream_device: Bad io error." );
450 void seek(
long count,
int whence )
455 : ( whence == SEEK_CUR
461 void write(
const byte_t* data
462 , std::size_t count )
464 _out.write( reinterpret_cast<char const*>( data )
465 , static_cast<std::streamsize>( count )
470 template <
typename T
490 m[0] = byte_t( x >> 0 );
491 m[1] = byte_t( x >> 8 );
501 m[0] = byte_t( x >> 0 );
502 m[1] = byte_t( x >> 8 );
503 m[2] = byte_t( x >> 16 );
504 m[3] = byte_t( x >> 24 );
533 template<
typename FormatTag >
struct is_input_device< file_stream_device< FormatTag > > : std::true_type{};
534 template<
typename FormatTag >
struct is_input_device< istream_device< FormatTag > > : std::true_type{};
536 template<
typename FormatTag
540 struct is_adaptable_input_device : std::false_type{};
542 template <
typename FormatTag,
typename T>
543 struct is_adaptable_input_device
547 typename std::enable_if
551 std::is_base_of<std::istream, T>,
552 std::is_same<std::istream, T>
560 template<
typename FormatTag >
561 struct is_adaptable_input_device< FormatTag
573 template<
typename FormatTag
580 template <
typename FormatTag,
typename T>
585 typename std::enable_if
589 is_input_device<FormatTag>,
590 is_adaptable_input_device<FormatTag, T>
604 template<
typename FormatTag >
struct is_output_device< file_stream_device< FormatTag > > : std::true_type{};
605 template<
typename FormatTag >
struct is_output_device< ostream_device < FormatTag > > : std::true_type{};
607 template<
typename FormatTag
611 struct is_adaptable_output_device : std::false_type {};
613 template <
typename FormatTag,
typename T>
614 struct is_adaptable_output_device
618 typename std::enable_if
622 std::is_base_of<std::ostream, T>,
623 std::is_same<std::ostream, T>
631 template<
typename FormatTag>
struct is_adaptable_output_device<FormatTag,FILE*,
void>
641 template<
typename FormatTag
648 template <
typename FormatTag,
typename T>
653 typename std::enable_if
657 is_output_device<FormatTag>,
658 is_adaptable_output_device<FormatTag, T>
667 template<
typename Device,
typename FormatTag >
class scanline_reader;
668 template<
typename Device,
typename FormatTag,
typename ConversionPolicy >
class reader;
670 template<
typename Device,
typename FormatTag,
typename Log = no_log >
class writer;
672 template<
typename Device,
typename FormatTag >
class dynamic_image_reader;
673 template<
typename Device,
typename FormatTag,
typename Log = no_log >
class dynamic_image_writer;
678 template<
typename T >
679 struct is_reader : std::false_type
682 template<
typename Device
684 ,
typename ConversionPolicy
686 struct is_reader< reader< Device
693 template<
typename T >
694 struct is_dynamic_image_reader : std::false_type
697 template<
typename Device
700 struct is_dynamic_image_reader< dynamic_image_reader< Device
706 template<
typename T >
707 struct is_writer : std::false_type
710 template<
typename Device
713 struct is_writer< writer< Device
719 template<
typename T >
720 struct is_dynamic_image_writer : std::false_type
723 template<
typename Device
726 struct is_dynamic_image_writer< dynamic_image_writer< Device
734 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
void write_uint8(uint8_t x)
Writes byte.
Definition: device.hpp:479
file_stream_device(const std::string &file_name, write_tag tag)
Definition: device.hpp:90
Definition: algorithm.hpp:30
std::size_t read(byte_t *data, std::size_t count)
Definition: device.hpp:143
void print_line(const std::string &line)
Prints formatted ASCII text.
Definition: device.hpp:289
void read(T(&buf)[N])
Reads array.
Definition: device.hpp:379
uint16_t read_uint16()
Reads 16 bit little endian integer.
Definition: device.hpp:185
std::size_t write(const T *buf, std::size_t count)
Writes number of elements from a buffer.
Definition: device.hpp:204
uint16_t read_uint16()
Reads 16 bit little endian integer.
Definition: device.hpp:394
void write(const T(&buf)[N])
Writes array.
Definition: device.hpp:223
Definition: device.hpp:577
void write_uint32(uint32_t x)
Writes 32 bit little endian integer.
Definition: device.hpp:250
void write_uint8(uint8_t x)
Writes byte.
Definition: device.hpp:232
uint32_t read_uint32()
Reads 32 bit little endian integer.
Definition: device.hpp:194
void write_uint16(uint16_t x)
Writes 16 bit little endian integer.
Definition: device.hpp:239
uint8_t read_uint8()
Reads byte.
Definition: device.hpp:385
Used to overload the constructor.
Definition: device.hpp:57
uint8_t read_uint8()
Reads byte.
Definition: device.hpp:176
Definition: device.hpp:602
uint32_t read_uint32()
Reads 32 bit little endian integer.
Definition: device.hpp:403
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:473
file_stream_device(const std::string &file_name, read_tag tag=read_tag())
Definition: device.hpp:63
Definition: device.hpp:48
Definition: device.hpp:645
void write_uint16(uint16_t x)
Writes 16 bit little endian integer.
Definition: device.hpp:486
Definition: device.hpp:436
Definition: device.hpp:327
file_stream_device(FILE *file)
Definition: device.hpp:117
void print_line(const std::string &line)
Prints formatted ASCII text.
Definition: device.hpp:515
void read(T(&buf)[N])
Reads array.
Definition: device.hpp:168
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:497