1 /* sane - Scanner Access Now Easy.
2 
3    Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
4 
5    This file is part of the SANE package.
6 
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 
20    As a special exception, the authors of SANE give permission for
21    additional uses of the libraries contained in this release of SANE.
22 
23    The exception is that, if you link a SANE library with other files
24    to produce an executable, this does not by itself cause the
25    resulting executable to be covered by the GNU General Public
26    License.  Your use of that executable is in no way restricted on
27    account of linking the SANE library code into it.
28 
29    This exception does not, however, invalidate any other reasons why
30    the executable file might be covered by the GNU General Public
31    License.
32 
33    If you submit changes to SANE to the maintainers to be included in
34    a subsequent release, you agree by submitting the changes that
35    those changes may be distributed with this exception intact.
36 
37    If you write modifications of your own for SANE, it is your choice
38    whether to permit this exception to apply to your modifications.
39    If you do not wish that, delete this exception notice.
40 */
41 
42 #ifndef BACKEND_GENESYS_LINE_BUFFER_H
43 #define BACKEND_GENESYS_LINE_BUFFER_H
44 
45 #include "error.h"
46 
47 #include <algorithm>
48 #include <cstdint>
49 #include <cstddef>
50 #include <vector>
51 
52 namespace genesys {
53 
54 class RowBuffer
55 {
56 public:
RowBuffer(std::size_t line_bytes)57     RowBuffer(std::size_t line_bytes) : row_bytes_{line_bytes} {}
58     RowBuffer(const RowBuffer&) = default;
59     RowBuffer& operator=(const RowBuffer&) = default;
60     ~RowBuffer() = default;
61 
get_row_ptr(std::size_t y)62     const std::uint8_t* get_row_ptr(std::size_t y) const
63     {
64         if (y >= height()) {
65             throw SaneException("y %zu is out of range", y);
66         }
67         return data_.data() + row_bytes_ * get_row_index(y);
68     }
69 
get_row_ptr(std::size_t y)70     std::uint8_t* get_row_ptr(std::size_t y)
71     {
72         if (y >= height()) {
73             throw SaneException("y %zu is out of range", y);
74         }
75         return data_.data() + row_bytes_ * get_row_index(y);
76     }
77 
get_front_row_ptr()78     const std::uint8_t* get_front_row_ptr() const { return get_row_ptr(0); }
get_front_row_ptr()79     std::uint8_t* get_front_row_ptr() { return get_row_ptr(0); }
get_back_row_ptr()80     const std::uint8_t* get_back_row_ptr() const { return get_row_ptr(height() - 1); }
get_back_row_ptr()81     std::uint8_t* get_back_row_ptr() { return get_row_ptr(height() - 1); }
82 
empty()83     bool empty() const { return is_linear_ && first_ == last_; }
84 
full()85     bool full()
86     {
87         if (is_linear_) {
88             return last_ == buffer_end_;
89         }
90         return first_ == last_;
91     }
92 
is_linear()93     bool is_linear() const { return is_linear_; }
94 
linearize()95     void linearize()
96     {
97         if (!is_linear_) {
98             std::rotate(data_.begin(), data_.begin() + row_bytes_ * first_, data_.end());
99             last_ = height();
100             first_ = 0;
101             is_linear_ = true;
102         }
103     }
104 
pop_front()105     void pop_front()
106     {
107         if (empty()) {
108             throw SaneException("Trying to pop out of empty() line buffer");
109         }
110 
111         first_++;
112         if (first_ == last_) {
113             first_ = 0;
114             last_ = 0;
115             is_linear_ = true;
116         } else  if (first_ == buffer_end_) {
117             first_ = 0;
118             is_linear_ = true;
119         }
120     }
121 
push_front()122     void push_front()
123     {
124         if (height() + 1 >= height_capacity()) {
125             ensure_capacity(std::max<std::size_t>(1, height() * 2));
126         }
127 
128         if (first_ == 0) {
129             is_linear_ = false;
130             first_ = buffer_end_;
131         }
132         first_--;
133     }
134 
pop_back()135     void pop_back()
136     {
137         if (empty()) {
138             throw SaneException("Trying to pop out of empty() line buffer");
139         }
140         if (last_ == 0) {
141             last_ = buffer_end_;
142             is_linear_ = true;
143         }
144         last_--;
145         if (first_ == last_) {
146             first_ = 0;
147             last_ = 0;
148             is_linear_ = true;
149         }
150     }
151 
push_back()152     void push_back()
153     {
154         if (height() + 1 >= height_capacity()) {
155             ensure_capacity(std::max<std::size_t>(1, height() * 2));
156         }
157 
158         if (last_ == buffer_end_) {
159             is_linear_ = false;
160             last_ = 0;
161         }
162         last_++;
163     }
164 
row_bytes()165     std::size_t row_bytes() const { return row_bytes_; }
166 
height()167     std::size_t height() const
168     {
169         if (!is_linear_) {
170             return last_ + buffer_end_ - first_;
171         }
172         return last_ - first_;
173     }
174 
height_capacity()175     std::size_t height_capacity() const { return buffer_end_; }
176 
clear()177     void clear()
178     {
179         first_ = 0;
180         last_ = 0;
181     }
182 
183 private:
get_row_index(std::size_t index)184     std::size_t get_row_index(std::size_t index) const
185     {
186         if (index >= buffer_end_ - first_) {
187             return index - (buffer_end_ - first_);
188         }
189         return index + first_;
190     }
191 
ensure_capacity(std::size_t capacity)192     void ensure_capacity(std::size_t capacity)
193     {
194         if (capacity < height_capacity())
195             return;
196         linearize();
197         data_.resize(capacity * row_bytes_);
198         buffer_end_ = capacity;
199     }
200 
201 private:
202     std::size_t row_bytes_ = 0;
203     std::size_t first_ = 0;
204     std::size_t last_ = 0;
205     std::size_t buffer_end_ = 0;
206     bool is_linear_ = true;
207     std::vector<std::uint8_t> data_;
208 };
209 
210 } // namespace genesys
211 
212 #endif // BACKEND_GENESYS_LINE_BUFFER_H
213