8 #ifndef BOOST_GIL_IO_DEVICE_HPP 9 #define BOOST_GIL_IO_DEVICE_HPP 11 #include <boost/gil/io/base.hpp> 13 #include <boost/assert.hpp> 14 #include <boost/core/ignore_unused.hpp> 18 #include <type_traits> 20 namespace boost {
namespace gil {
22 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) 24 #pragma warning(disable:4512) //assignment operator could not be generated 29 template <
typename T >
struct buff_item
31 static const unsigned int size =
sizeof( T );
34 template <>
struct buff_item< void >
36 static const unsigned int size = 1;
49 template<
typename FormatTag >
54 using format_tag_t = FormatTag;
71 io_error_if( ( file = fopen( file_name.c_str(),
"rb" )) == nullptr
72 ,
"file_stream_device: failed to open file" 75 _file = file_ptr_t( file
89 io_error_if( ( file = fopen( file_name,
"rb" )) ==
nullptr 90 ,
"file_stream_device: failed to open file" 93 _file = file_ptr_t( file
105 FILE* file =
nullptr;
107 io_error_if( ( file = fopen( file_name.c_str(),
"wb" )) == nullptr
108 ,
"file_stream_device: failed to open file" 111 _file = file_ptr_t( file
123 FILE* file =
nullptr;
125 io_error_if( ( file = fopen( file_name,
"wb" )) ==
nullptr 126 ,
"file_stream_device: failed to open file" 129 _file = file_ptr_t( file
143 FILE*
get() {
return _file.get(); }
144 const FILE*
get()
const {
return _file.get(); }
148 return std::getc(
get() );
155 if(( ch = std::getc(
get() )) == EOF )
156 io_error(
"file_stream_device: unexpected EOF" );
166 std::size_t num_elements = fread( data
168 , static_cast<int>( count )
180 BOOST_ASSERT(num_elements > 0 );
189 std::size_t
read( T (&buf)[N] )
191 return read( buf, N );
209 return (m[1] << 8) | m[0];
218 return (m[3] << 24) | (m[2] << 16) | (m[1] << 8) | m[0];
222 template <
typename T >
228 std::size_t num_elements = fwrite( buf
234 BOOST_ASSERT(num_elements == count);
239 template <
typename T
242 std::size_t
write(
const T (&buf)[N] )
throw()
244 return write( buf, N );
259 m[0] = byte_t( x >> 0 );
260 m[1] = byte_t( x >> 8 );
270 m[0] = byte_t( x >> 0 );
271 m[1] = byte_t( x >> 8 );
272 m[2] = byte_t( x >> 16 );
273 m[3] = byte_t( x >> 24 );
278 void seek(
long count,
int whence = SEEK_SET )
280 io_error_if( fseek(
get()
290 long int pos = ftell(
get() );
292 io_error_if( pos == -1L
307 std::size_t num_elements = fwrite( line.c_str()
313 BOOST_ASSERT(num_elements == line.size());
314 boost::ignore_unused(num_elements);
319 return ferror(
get() );
324 static void file_deleter( FILE* file )
334 using file_ptr_t = std::shared_ptr<FILE> ;
341 template<
typename FormatTag >
351 io_error(
"Stream is not valid.");
364 if(( ch = _in.get() ) == EOF )
365 io_error(
"file_stream_device: unexpected EOF" );
370 std::size_t read( byte_t* data
371 , std::size_t count )
373 std::streamsize cr = 0;
378 std::streamsize c = _in.readsome( reinterpret_cast< char* >( data )
379 , static_cast< std::streamsize >( count ));
381 count -=
static_cast< std::size_t
>( c );
385 }
while( count && _in );
387 return static_cast< std::size_t
>( cr );
396 return read( buf, N );
414 return (m[1] << 8) | m[0];
423 return (m[3] << 24) | (m[2] << 16) | (m[1] << 8) | m[0];
426 void seek(
long count,
int whence = SEEK_SET )
429 , whence == SEEK_SET ? std::ios::beg
430 :( whence == SEEK_CUR ? std::ios::cur
435 void write(
const byte_t*, std::size_t)
437 io_error(
"Bad io error." );
450 template<
typename FormatTag >
459 std::size_t read(byte_t *, std::size_t)
461 io_error(
"Bad io error." );
465 void seek(
long count,
int whence )
470 : ( whence == SEEK_CUR
476 void write(
const byte_t* data
477 , std::size_t count )
479 _out.write( reinterpret_cast<char const*>( data )
480 , static_cast<std::streamsize>( count )
485 template <
typename T
488 void write(
const T (&buf)[N] )
throw()
505 m[0] = byte_t( x >> 0 );
506 m[1] = byte_t( x >> 8 );
516 m[0] = byte_t( x >> 0 );
517 m[1] = byte_t( x >> 8 );
518 m[2] = byte_t( x >> 16 );
519 m[3] = byte_t( x >> 24 );
548 template<
typename FormatTag >
struct is_input_device< file_stream_device< FormatTag > > : mpl::true_{};
549 template<
typename FormatTag >
struct is_input_device< istream_device< FormatTag > > : mpl::true_{};
551 template<
typename FormatTag
555 struct is_adaptable_input_device : mpl::false_{};
557 template <
typename FormatTag,
typename T>
558 struct is_adaptable_input_device
562 typename std::enable_if
566 is_base_and_derived<std::istream, T>,
567 is_same<std::istream, T>
575 template<
typename FormatTag >
576 struct is_adaptable_input_device< FormatTag
588 template<
typename FormatTag
595 template <
typename FormatTag,
typename T>
600 typename std::enable_if
604 is_input_device<FormatTag>,
605 is_adaptable_input_device<FormatTag, T>
619 template<
typename FormatTag >
struct is_output_device< file_stream_device< FormatTag > > : mpl::true_{};
620 template<
typename FormatTag >
struct is_output_device< ostream_device < FormatTag > > : mpl::true_{};
622 template<
typename FormatTag
626 struct is_adaptable_output_device : mpl::false_ {};
628 template <
typename FormatTag,
typename T>
629 struct is_adaptable_output_device
633 typename std::enable_if
637 is_base_and_derived<std::ostream, T>,
638 is_same<std::ostream, T>
646 template<
typename FormatTag>
struct is_adaptable_output_device<FormatTag,FILE*,
void>
656 template<
typename FormatTag
663 template <
typename FormatTag,
typename T>
668 typename std::enable_if
672 is_output_device<FormatTag>,
673 is_adaptable_output_device<FormatTag, T>
682 template<
typename Device,
typename FormatTag >
class scanline_reader;
683 template<
typename Device,
typename FormatTag,
typename ConversionPolicy >
class reader;
685 template<
typename Device,
typename FormatTag,
typename Log = no_log >
class writer;
687 template<
typename Device,
typename FormatTag >
class dynamic_image_reader;
688 template<
typename Device,
typename FormatTag,
typename Log = no_log >
class dynamic_image_writer;
693 template<
typename T >
694 struct is_reader : mpl::false_
697 template<
typename Device
699 ,
typename ConversionPolicy
701 struct is_reader< reader< Device
708 template<
typename T >
709 struct is_dynamic_image_reader : mpl::false_
712 template<
typename Device
715 struct is_dynamic_image_reader< dynamic_image_reader< Device
721 template<
typename T >
722 struct is_writer : mpl::false_
725 template<
typename Device
728 struct is_writer< writer< Device
734 template<
typename T >
735 struct is_dynamic_image_writer : mpl::false_
738 template<
typename Device
741 struct is_dynamic_image_writer< dynamic_image_writer< Device
749 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
void write_uint8(uint8_t x)
Writes byte.
Definition: device.hpp:494
Definition: algorithm.hpp:30
std::size_t read(byte_t *data, std::size_t count)
Definition: device.hpp:162
void print_line(const std::string &line)
Prints formatted ASCII text.
Definition: device.hpp:305
std::size_t read(T(&buf)[N])
Reads array.
Definition: device.hpp:189
uint16_t read_uint16()
Reads 16 bit little endian integer.
Definition: device.hpp:204
size_t read(T(&buf)[N])
Reads array.
Definition: device.hpp:394
std::size_t write(const T *buf, std::size_t count)
Writes number of elements from a buffer.
Definition: device.hpp:223
uint16_t read_uint16()
Reads 16 bit little endian integer.
Definition: device.hpp:409
std::size_t write(const T(&buf)[N])
Writes array.
Definition: device.hpp:242
Definition: device.hpp:592
void write_uint32(uint32_t x)
Writes 32 bit little endian integer.
Definition: device.hpp:266
void write_uint8(uint8_t x)
Writes byte.
Definition: device.hpp:248
uint32_t read_uint32()
Reads 32 bit little endian integer.
Definition: device.hpp:213
void write_uint16(uint16_t x)
Writes 16 bit little endian integer.
Definition: device.hpp:255
uint8_t read_uint8()
Reads byte.
Definition: device.hpp:400
Used to overload the constructor.
Definition: device.hpp:59
uint8_t read_uint8()
Reads byte.
Definition: device.hpp:195
Definition: device.hpp:617
uint32_t read_uint32()
Reads 32 bit little endian integer.
Definition: device.hpp:418
file_stream_device(const char *file_name, read_tag=read_tag())
Definition: device.hpp:83
void write(const T(&buf)[N])
Writes array.
Definition: device.hpp:488
file_stream_device(const std::string &file_name, write_tag)
Definition: device.hpp:101
Definition: device.hpp:50
Definition: device.hpp:660
void write_uint16(uint16_t x)
Writes 16 bit little endian integer.
Definition: device.hpp:501
Definition: device.hpp:451
Definition: device.hpp:342
file_stream_device(FILE *file)
Definition: device.hpp:137
void print_line(const std::string &line)
Prints formatted ASCII text.
Definition: device.hpp:530
file_stream_device(const char *file_name, write_tag)
Definition: device.hpp:119
void write_uint32(uint32_t x)
Writes 32 bit little endian integer.
Definition: device.hpp:512
file_stream_device(const std::string &file_name, read_tag=read_tag())
Definition: device.hpp:65