• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #ifndef BACKEND_GENESYS_IMAGE_BUFFER_H
22 #define BACKEND_GENESYS_IMAGE_BUFFER_H
23 
24 #include "enums.h"
25 #include "row_buffer.h"
26 #include <algorithm>
27 #include <functional>
28 
29 namespace genesys {
30 
31 // This class allows reading from row-based source in smaller or larger chunks of data
32 class ImageBuffer
33 {
34 public:
35     using ProducerCallback = std::function<bool(std::size_t size, std::uint8_t* out_data)>;
36     static constexpr std::uint64_t BUFFER_SIZE_UNSET = std::numeric_limits<std::uint64_t>::max();
37 
ImageBuffer()38     ImageBuffer() {}
39     ImageBuffer(std::size_t size, ProducerCallback producer);
40 
available()41     std::size_t available() const { return curr_size_ - buffer_offset_; }
42 
43     // allows adjusting the amount of data left so that we don't do a full size read from the
44     // producer on the last iteration. Set to BUFFER_SIZE_UNSET to ignore buffer size.
remaining_size()45     std::uint64_t remaining_size() const { return remaining_size_; }
set_remaining_size(std::uint64_t bytes)46     void set_remaining_size(std::uint64_t bytes) { remaining_size_ = bytes; }
47 
48     // May be used to force the last read to be rounded up of a certain number of bytes
set_last_read_multiple(std::uint64_t bytes)49     void set_last_read_multiple(std::uint64_t bytes) { last_read_multiple_ = bytes; }
50 
51     bool get_data(std::size_t size, std::uint8_t* out_data);
52 
53 private:
54     ProducerCallback producer_;
55     std::size_t size_ = 0;
56     std::size_t curr_size_ = 0;
57 
58     std::uint64_t remaining_size_ = BUFFER_SIZE_UNSET;
59     std::uint64_t last_read_multiple_ = BUFFER_SIZE_UNSET;
60 
61     std::size_t buffer_offset_ = 0;
62     std::vector<std::uint8_t> buffer_;
63 };
64 
65 } // namespace genesys
66 
67 #endif // BACKEND_GENESYS_IMAGE_BUFFER_H
68