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_IMAGE_BUFFER_H
43 #define BACKEND_GENESYS_IMAGE_BUFFER_H
44 
45 #include "enums.h"
46 #include "row_buffer.h"
47 #include <algorithm>
48 #include <functional>
49 
50 namespace genesys {
51 
52 // This class allows reading from row-based source in smaller or larger chunks of data
53 class ImageBuffer
54 {
55 public:
56     using ProducerCallback = std::function<bool(std::size_t size, std::uint8_t* out_data)>;
57     static constexpr std::uint64_t BUFFER_SIZE_UNSET = std::numeric_limits<std::uint64_t>::max();
58 
ImageBuffer()59     ImageBuffer() {}
60     ImageBuffer(std::size_t size, ProducerCallback producer);
61 
available()62     std::size_t available() const { return curr_size_ - buffer_offset_; }
63 
64     // allows adjusting the amount of data left so that we don't do a full size read from the
65     // producer on the last iteration. Set to BUFFER_SIZE_UNSET to ignore buffer size.
remaining_size()66     std::uint64_t remaining_size() const { return remaining_size_; }
set_remaining_size(std::uint64_t bytes)67     void set_remaining_size(std::uint64_t bytes) { remaining_size_ = bytes; }
68 
69     // 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)70     void set_last_read_multiple(std::uint64_t bytes) { last_read_multiple_ = bytes; }
71 
72     bool get_data(std::size_t size, std::uint8_t* out_data);
73 
74 private:
75     ProducerCallback producer_;
76     std::size_t size_ = 0;
77     std::size_t curr_size_ = 0;
78 
79     std::uint64_t remaining_size_ = BUFFER_SIZE_UNSET;
80     std::uint64_t last_read_multiple_ = BUFFER_SIZE_UNSET;
81 
82     std::size_t buffer_offset_ = 0;
83     std::vector<std::uint8_t> buffer_;
84 };
85 
86 } // namespace genesys
87 
88 #endif // BACKEND_GENESYS_IMAGE_BUFFER_H
89