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 #define DEBUG_DECLARE_ONLY
43 
44 #include "image_buffer.h"
45 #include "image.h"
46 #include "utilities.h"
47 
48 namespace genesys {
49 
ImageBuffer(std::size_t size,ProducerCallback producer)50 ImageBuffer::ImageBuffer(std::size_t size, ProducerCallback producer) :
51     producer_{producer},
52     size_{size}
53 {
54     buffer_.resize(size_);
55 }
56 
get_data(std::size_t size,std::uint8_t * out_data)57 bool ImageBuffer::get_data(std::size_t size, std::uint8_t* out_data)
58 {
59     const std::uint8_t* out_data_end = out_data + size;
60 
61     auto copy_buffer = [&]()
62     {
63         std::size_t bytes_copy = std::min<std::size_t>(out_data_end - out_data, available());
64         std::memcpy(out_data, buffer_.data() + buffer_offset_, bytes_copy);
65         out_data += bytes_copy;
66         buffer_offset_ += bytes_copy;
67     };
68 
69     // first, read remaining data from buffer
70     if (available() > 0) {
71         copy_buffer();
72     }
73 
74     if (out_data == out_data_end) {
75         return true;
76     }
77 
78     // now the buffer is empty and there's more data to be read
79     bool got_data = true;
80     do {
81         buffer_offset_ = 0;
82 
83         std::size_t size_to_read = size_;
84         if (remaining_size_ != BUFFER_SIZE_UNSET) {
85             size_to_read = std::min<std::uint64_t>(size_to_read, remaining_size_);
86             remaining_size_ -= size_to_read;
87         }
88 
89         std::size_t aligned_size_to_read = size_to_read;
90         if (remaining_size_ == 0 && last_read_multiple_ != BUFFER_SIZE_UNSET) {
91             aligned_size_to_read = align_multiple_ceil(size_to_read, last_read_multiple_);
92         }
93 
94         got_data &= producer_(aligned_size_to_read, buffer_.data());
95         curr_size_ = size_to_read;
96 
97         copy_buffer();
98 
99         if (remaining_size_ == 0 && out_data < out_data_end) {
100             got_data = false;
101         }
102 
103     } while (out_data < out_data_end && got_data);
104 
105     return got_data;
106 }
107 
108 } // namespace genesys
109