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