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> 14 #include <boost/assert.hpp> 15 #include <boost/core/ignore_unused.hpp> 19 #include <type_traits> 21 namespace boost {
namespace gil {
23 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) 25 #pragma warning(disable:4512) //assignment operator could not be generated 30 template <
typename T >
struct buff_item
32 static const unsigned int size =
sizeof( T );
35 template <>
struct buff_item< void >
37 static const unsigned int size = 1;
50 template<
typename FormatTag >
55 using format_tag_t = FormatTag;
72 io_error_if( ( file = fopen( file_name.c_str(),
"rb" )) == nullptr
73 ,
"file_stream_device: failed to open file" 76 _file = file_ptr_t( file
90 io_error_if( ( file = fopen( file_name,
"rb" )) ==
nullptr 91 ,
"file_stream_device: failed to open file" 94 _file = file_ptr_t( file
106 FILE* file =
nullptr;
108 io_error_if( ( file = fopen( file_name.c_str(),
"wb" )) == nullptr
109 ,
"file_stream_device: failed to open file" 112 _file = file_ptr_t( file
124 FILE* file =
nullptr;
126 io_error_if( ( file = fopen( file_name,
"wb" )) ==
nullptr 127 ,
"file_stream_device: failed to open file" 130 _file = file_ptr_t( file
144 FILE*
get() {
return _file.get(); }
145 const FILE*
get()
const {
return _file.get(); }
149 return std::getc(
get() );
156 if(( ch = std::getc(
get() )) == EOF )
157 io_error(
"file_stream_device: unexpected EOF" );
167 std::size_t num_elements = fread( data
169 , static_cast<int>( count )
181 BOOST_ASSERT(num_elements > 0 );
190 std::size_t
read( T (&buf)[N] )
192 return read( buf, N );
210 return (m[1] << 8) | m[0];
219 return (m[3] << 24) | (m[2] << 16) | (m[1] << 8) | m[0];
223 template <
typename T >
229 std::size_t num_elements = fwrite( buf
235 BOOST_ASSERT(num_elements == count);
240 template <
typename T
243 std::size_t
write(
const T (&buf)[N] )
throw()
245 return write( buf, N );
260 m[0] = byte_t( x >> 0 );
261 m[1] = byte_t( x >> 8 );
271 m[0] = byte_t( x >> 0 );
272 m[1] = byte_t( x >> 8 );
273 m[2] = byte_t( x >> 16 );
274 m[3] = byte_t( x >> 24 );
279 void seek(
long count,
int whence = SEEK_SET )
281 io_error_if( fseek(
get()
291 long int pos = ftell(
get() );
293 io_error_if( pos == -1L
308 std::size_t num_elements = fwrite( line.c_str()
314 BOOST_ASSERT(num_elements == line.size());
315 boost::ignore_unused(num_elements);
320 return ferror(
get() );
325 static void file_deleter( FILE* file )
335 using file_ptr_t = std::shared_ptr<FILE> ;
342 template<
typename FormatTag >
352 io_error(
"Stream is not valid.");
365 if(( ch = _in.get() ) == EOF )
366 io_error(
"file_stream_device: unexpected EOF" );
371 std::size_t read( byte_t* data
372 , std::size_t count )
374 std::streamsize cr = 0;
379 std::streamsize c = _in.readsome( reinterpret_cast< char* >( data )
380 , static_cast< std::streamsize >( count ));
382 count -=
static_cast< std::size_t
>( c );
386 }
while( count && _in );
388 return static_cast< std::size_t
>( cr );
397 return read( buf, N );
415 return (m[1] << 8) | m[0];
424 return (m[3] << 24) | (m[2] << 16) | (m[1] << 8) | m[0];
427 void seek(
long count,
int whence = SEEK_SET )
430 , whence == SEEK_SET ? std::ios::beg
431 :( whence == SEEK_CUR ? std::ios::cur
436 void write(
const byte_t*, std::size_t)
438 io_error(
"Bad io error." );
451 template<
typename FormatTag >
460 std::size_t read(byte_t *, std::size_t)
462 io_error(
"Bad io error." );
466 void seek(
long count,
int whence )
471 : ( whence == SEEK_CUR
477 void write(
const byte_t* data
478 , std::size_t count )
480 _out.write( reinterpret_cast<char const*>( data )
481 , static_cast<std::streamsize>( count )
486 template <
typename T
489 void write(
const T (&buf)[N] )
throw()
506 m[0] = byte_t( x >> 0 );
507 m[1] = byte_t( x >> 8 );
517 m[0] = byte_t( x >> 0 );
518 m[1] = byte_t( x >> 8 );
519 m[2] = byte_t( x >> 16 );
520 m[3] = byte_t( x >> 24 );
549 template<
typename FormatTag >
struct is_input_device< file_stream_device< FormatTag > > : std::true_type{};
550 template<
typename FormatTag >
struct is_input_device< istream_device< FormatTag > > : std::true_type{};
552 template<
typename FormatTag
556 struct is_adaptable_input_device : std::false_type{};
558 template <
typename FormatTag,
typename T>
559 struct is_adaptable_input_device
563 typename std::enable_if
567 std::is_base_of<std::istream, T>,
568 std::is_same<std::istream, T>
576 template<
typename FormatTag >
577 struct is_adaptable_input_device< FormatTag
589 template<
typename FormatTag
596 template <
typename FormatTag,
typename T>
601 typename std::enable_if
605 is_input_device<FormatTag>,
606 is_adaptable_input_device<FormatTag, T>
620 template<
typename FormatTag >
struct is_output_device< file_stream_device< FormatTag > > : std::true_type{};
621 template<
typename FormatTag >
struct is_output_device< ostream_device < FormatTag > > : std::true_type{};
623 template<
typename FormatTag
627 struct is_adaptable_output_device : std::false_type {};
629 template <
typename FormatTag,
typename T>
630 struct is_adaptable_output_device
634 typename std::enable_if
638 std::is_base_of<std::ostream, T>,
639 std::is_same<std::ostream, T>
647 template<
typename FormatTag>
struct is_adaptable_output_device<FormatTag,FILE*,
void>
657 template<
typename FormatTag
664 template <
typename FormatTag,
typename T>
669 typename std::enable_if
673 is_output_device<FormatTag>,
674 is_adaptable_output_device<FormatTag, T>
683 template<
typename Device,
typename FormatTag >
class scanline_reader;
684 template<
typename Device,
typename FormatTag,
typename ConversionPolicy >
class reader;
686 template<
typename Device,
typename FormatTag,
typename Log = no_log >
class writer;
688 template<
typename Device,
typename FormatTag >
class dynamic_image_reader;
689 template<
typename Device,
typename FormatTag,
typename Log = no_log >
class dynamic_image_writer;
694 template<
typename T >
695 struct is_reader : std::false_type
698 template<
typename Device
700 ,
typename ConversionPolicy
702 struct is_reader< reader< Device
709 template<
typename T >
710 struct is_dynamic_image_reader : std::false_type
713 template<
typename Device
716 struct is_dynamic_image_reader< dynamic_image_reader< Device
722 template<
typename T >
723 struct is_writer : std::false_type
726 template<
typename Device
729 struct is_writer< writer< Device
735 template<
typename T >
736 struct is_dynamic_image_writer : std::false_type
739 template<
typename Device
742 struct is_dynamic_image_writer< dynamic_image_writer< Device
750 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
void write_uint8(uint8_t x)
Writes byte.
Definition: device.hpp:495
Definition: algorithm.hpp:30
std::size_t read(byte_t *data, std::size_t count)
Definition: device.hpp:163
void print_line(const std::string &line)
Prints formatted ASCII text.
Definition: device.hpp:306
std::size_t read(T(&buf)[N])
Reads array.
Definition: device.hpp:190
uint16_t read_uint16()
Reads 16 bit little endian integer.
Definition: device.hpp:205
size_t read(T(&buf)[N])
Reads array.
Definition: device.hpp:395
std::size_t write(const T *buf, std::size_t count)
Writes number of elements from a buffer.
Definition: device.hpp:224
uint16_t read_uint16()
Reads 16 bit little endian integer.
Definition: device.hpp:410
std::size_t write(const T(&buf)[N])
Writes array.
Definition: device.hpp:243
Definition: device.hpp:593
void write_uint32(uint32_t x)
Writes 32 bit little endian integer.
Definition: device.hpp:267
void write_uint8(uint8_t x)
Writes byte.
Definition: device.hpp:249
uint32_t read_uint32()
Reads 32 bit little endian integer.
Definition: device.hpp:214
void write_uint16(uint16_t x)
Writes 16 bit little endian integer.
Definition: device.hpp:256
uint8_t read_uint8()
Reads byte.
Definition: device.hpp:401
Used to overload the constructor.
Definition: device.hpp:60
uint8_t read_uint8()
Reads byte.
Definition: device.hpp:196
Definition: device.hpp:618
uint32_t read_uint32()
Reads 32 bit little endian integer.
Definition: device.hpp:419
file_stream_device(const char *file_name, read_tag=read_tag())
Definition: device.hpp:84
void write(const T(&buf)[N])
Writes array.
Definition: device.hpp:489
file_stream_device(const std::string &file_name, write_tag)
Definition: device.hpp:102
Definition: device.hpp:51
Definition: device.hpp:661
void write_uint16(uint16_t x)
Writes 16 bit little endian integer.
Definition: device.hpp:502
Definition: device.hpp:452
Definition: device.hpp:343
file_stream_device(FILE *file)
Definition: device.hpp:138
void print_line(const std::string &line)
Prints formatted ASCII text.
Definition: device.hpp:531
file_stream_device(const char *file_name, write_tag)
Definition: device.hpp:120
void write_uint32(uint32_t x)
Writes 32 bit little endian integer.
Definition: device.hpp:513
file_stream_device(const std::string &file_name, read_tag=read_tag())
Definition: device.hpp:66