Boost GIL


scanline_read_iterator.hpp
1 //
2 // Copyright 2007-2008 Christian Henning
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 #ifndef BOOST_GIL_IO_SCANLINE_READ_ITERATOR_HPP
9 #define BOOST_GIL_IO_SCANLINE_READ_ITERATOR_HPP
10 
11 #include <boost/gil/io/error.hpp>
12 #include <boost/gil/io/typedefs.hpp>
13 
14 #include <boost/iterator/iterator_facade.hpp>
15 
16 #include <iterator>
17 #include <memory>
18 #include <vector>
19 
20 namespace boost { namespace gil {
21 
22 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
23 #pragma warning(push)
24 #pragma warning(disable:4512) //assignment operator could not be generated
25 #endif
26 
28 template< typename Reader >
29 class scanline_read_iterator : public boost::iterator_facade< scanline_read_iterator< Reader >
30  , byte_t*
31  , std::input_iterator_tag
32  >
33 {
34 private:
35 
36  using base_t = boost::iterator_facade
37  <
39  byte_t*,
40  std::input_iterator_tag
41  >;
42 
43 
44 public:
45 
46  scanline_read_iterator( Reader& reader
47  , int pos = 0
48  )
49  : _pos( pos )
50  , _read_scanline( true )
51  , _skip_scanline( true )
52  , _reader( reader )
53  {
54  _buffer = std::make_shared< buffer_t >( buffer_t( _reader._scanline_length ));
55  _buffer_start = &_buffer->front();
56  }
57 
58 private:
59  friend class boost::iterator_core_access;
60 
61  void increment()
62  {
63  if( _skip_scanline == true )
64  {
65  _reader.skip( _buffer_start
66  , _pos
67  );
68  }
69 
70  ++_pos;
71 
72  _skip_scanline = true;
73  _read_scanline = true;
74  }
75 
76  bool equal( const scanline_read_iterator& rhs ) const
77  {
78  return _pos == rhs._pos;
79  }
80 
81  typename base_t::reference dereference() const
82  {
83  if( _read_scanline == true )
84  {
85  _reader.read( _buffer_start
86  , _pos
87  );
88  }
89 
90  _skip_scanline = false;
91  _read_scanline = false;
92 
93  return _buffer_start;
94  }
95 
96 private:
97  Reader& _reader;
98 
99  mutable int _pos;
100 
101  mutable bool _read_scanline;
102  mutable bool _skip_scanline;
103 
104  using buffer_t = std::vector<byte_t>;
105  using buffer_ptr_t = std::shared_ptr<buffer_t>;
106 
107  buffer_ptr_t _buffer;
108  mutable byte_t* _buffer_start;
109 };
110 
111 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
112 #pragma warning(pop)
113 #endif
114 
115 } // namespace gil
116 } // namespace boost
117 
118 #endif
Input iterator to read images.
Definition: scanline_read_iterator.hpp:29
BOOST_FORCEINLINE bool equal(boost::gil::iterator_from_2d< Loc1 > first, boost::gil::iterator_from_2d< Loc1 > last, boost::gil::iterator_from_2d< Loc2 > first2)
std::equal(I1,I1,I2) with I1 and I2 being a iterator_from_2d
Definition: algorithm.hpp:1029