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_SETTINGS_H
22#define BACKEND_GENESYS_SETTINGS_H
23
24#include "enums.h"
25#include "serialize.h"
26#include "utilities.h"
27#include "sensor.h"
28
29namespace genesys {
30
31struct Genesys_Settings
32{
33    ScanMethod scan_method = ScanMethod::FLATBED;
34    ScanColorMode scan_mode = ScanColorMode::LINEART;
35
36    // horizontal dpi
37    unsigned xres = 0;
38    // vertical dpi
39    unsigned yres = 0;
40
41    //x start on scan table in mm
42    float tl_x = 0;
43    // y start on scan table in mm
44    float tl_y = 0;
45
46    // number of lines at scan resolution
47    unsigned int lines = 0;
48    // number of pixels expected from the scanner
49    unsigned int pixels = 0;
50    // number of pixels expected by the frontend
51    unsigned requested_pixels = 0;
52
53    // bit depth of the scan
54    unsigned int depth = 0;
55
56    ColorFilter color_filter = ColorFilter::NONE;
57
58    // value for contrast enhancement in the [-100..100] range
59    int contrast = 0;
60
61    // value for brightness enhancement in the [-100..100] range
62    int brightness = 0;
63
64    // cache entries expiration time
65    int expiration_time = 0;
66
67    unsigned get_channels() const
68    {
69        if (scan_mode == ScanColorMode::COLOR_SINGLE_PASS)
70            return 3;
71        return 1;
72    }
73};
74
75std::ostream& operator<<(std::ostream& out, const Genesys_Settings& settings);
76
77
78struct SetupParams {
79
80    static constexpr unsigned NOT_SET = std::numeric_limits<unsigned>::max();
81    static constexpr unsigned NOT_SET_I = std::numeric_limits<int>::max();
82
83    // resolution in x direction
84    unsigned xres = NOT_SET;
85    // resolution in y direction
86    unsigned yres = NOT_SET;
87    // start pixel in X direction, from dummy_pixel + 1. Counted in terms of xres.
88    unsigned startx = NOT_SET;
89    // start pixel in Y direction, counted according to base_ydpi
90    unsigned starty = NOT_SET;
91    // the number of pixels in X direction. Counted in terms of xres.
92    // Note that each logical pixel may correspond to more than one CCD pixel, see CKSEL and
93    // GenesysSensor::ccd_pixels_per_system_pixel()
94    unsigned pixels = NOT_SET;
95
96    // the number of pixels in the X direction as requested by the frontend. This will be different
97    // from `pixels` if the X resolution requested by the frontend is different than the actual
98    // resolution. This is only needed to compute dev->total_bytes_to_read. If 0, then the value
99    // is the same as pixels.
100    // TODO: move the computation of total_bytes_to_read to a higher layer.
101    unsigned requested_pixels = 0;
102
103    // the number of pixels in Y direction
104    unsigned lines = NOT_SET;
105    // the depth of the scan in bits. Allowed are 1, 8, 16
106    unsigned depth = NOT_SET;
107    // the number of channels
108    unsigned channels = NOT_SET;
109
110    ScanMethod scan_method = static_cast<ScanMethod>(NOT_SET);
111
112    ScanColorMode scan_mode = static_cast<ScanColorMode>(NOT_SET);
113
114    ColorFilter color_filter = static_cast<ColorFilter>(NOT_SET);
115
116    // the values for contrast and brightness adjustment in the range of [-100..100]
117    int contrast_adjustment = NOT_SET_I;
118    int brightness_adjustment = NOT_SET_I;
119
120    ScanFlag flags = ScanFlag::NONE;
121
122    unsigned get_requested_pixels() const
123    {
124        if (requested_pixels != 0) {
125            return requested_pixels;
126        }
127        return pixels;
128    }
129
130    void assert_valid() const
131    {
132        if (xres == NOT_SET || yres == NOT_SET || startx == NOT_SET || starty == NOT_SET ||
133            pixels == NOT_SET || lines == NOT_SET ||depth == NOT_SET || channels == NOT_SET ||
134            scan_method == static_cast<ScanMethod>(NOT_SET) ||
135            scan_mode == static_cast<ScanColorMode>(NOT_SET) ||
136            color_filter == static_cast<ColorFilter>(NOT_SET) ||
137            contrast_adjustment == NOT_SET_I || brightness_adjustment == NOT_SET_I)
138        {
139            throw std::runtime_error("SetupParams are not valid");
140        }
141    }
142
143    bool operator==(const SetupParams& other) const
144    {
145        return xres == other.xres &&
146            yres == other.yres &&
147            startx == other.startx &&
148            starty == other.starty &&
149            pixels == other.pixels &&
150            requested_pixels == other.requested_pixels &&
151            lines == other.lines &&
152            depth == other.depth &&
153            channels == other.channels &&
154            scan_method == other.scan_method &&
155            scan_mode == other.scan_mode &&
156            color_filter == other.color_filter &&
157            contrast_adjustment == other.contrast_adjustment &&
158            brightness_adjustment == other.brightness_adjustment &&
159            flags == other.flags;
160    }
161};
162
163std::ostream& operator<<(std::ostream& out, const SetupParams& params);
164
165template<class Stream>
166void serialize(Stream& str, SetupParams& x)
167{
168    serialize(str, x.xres);
169    serialize(str, x.yres);
170    serialize(str, x.startx);
171    serialize(str, x.starty);
172    serialize(str, x.pixels);
173    serialize(str, x.requested_pixels);
174    serialize(str, x.lines);
175    serialize(str, x.depth);
176    serialize(str, x.channels);
177    serialize(str, x.scan_method);
178    serialize(str, x.scan_mode);
179    serialize(str, x.color_filter);
180    serialize(str, x.contrast_adjustment);
181    serialize(str, x.brightness_adjustment);
182    serialize(str, x.flags);
183}
184
185struct ScanSession {
186    SetupParams params;
187
188    // whether the session setup has been computed via compute_session()
189    bool computed = false;
190
191    // specifies the full resolution of the sensor that is being used.
192    unsigned full_resolution = 0;
193
194    // the optical resolution of the sensor that is being used.
195    unsigned optical_resolution = 0;
196
197    // the number of pixels at the optical resolution, not including segmentation overhead.
198    unsigned optical_pixels = 0;
199
200    // the number of pixels at the optical resolution, including segmentation overhead.
201    // only on gl846, g847
202    unsigned optical_pixels_raw = 0;
203
204    // the number of optical scan lines. Equal to output_line_count on CCD scanners.
205    unsigned optical_line_count = 0;
206
207    // the resolution of the output data.
208    unsigned output_resolution = 0;
209
210    // the offset in pixels from the beginning of output data
211    unsigned output_startx = 0;
212
213    // the number of pixels in output data (after desegmentation)
214    unsigned output_pixels = 0;
215
216    // the number of bytes in the output of a channel of a single line (after desegmentation)
217    unsigned output_channel_bytes = 0;
218
219    // the number of bytes in the output of a single line (after desegmentation)
220    unsigned output_line_bytes = 0;
221
222    // the number of bytes per line in the output data from the scanner (before desegmentation)
223    // Equal to output_line_bytes if sensor does not have segments
224    unsigned output_line_bytes_raw = 0;
225
226    // the number of bytes per line as requested by the frontend
227    unsigned output_line_bytes_requested = 0;
228
229    // the number of lines in the output of the scanner. This must be larger than the user
230    // requested number due to line staggering and color channel shifting.
231    unsigned output_line_count = 0;
232
233    // the total number of bytes to read from the scanner (before desegmentation)
234    unsigned output_total_bytes_raw = 0;
235
236    // the total number of bytes to read from the scanner (after desegmentation)
237    unsigned output_total_bytes = 0;
238
239    // the number of staggered lines (i.e. lines that overlap during scanning due to line being
240    // thinner than the CCD element). Computed according to stagger_y.
241    unsigned num_staggered_lines = 0;
242
243    // the number of lines that color channels shift due to different physical positions of
244    // different color channels.
245    unsigned max_color_shift_lines = 0;
246
247    // actual line shift of the red color
248    unsigned color_shift_lines_r = 0;
249    // actual line shift of the green color
250    unsigned color_shift_lines_g = 0;
251    // actual line shift of the blue color
252    unsigned color_shift_lines_b = 0;
253
254    // The shifts that need to be applied to the output pixels in x direction.
255    StaggerConfig stagger_x;
256    // The shifts that need to be applied to the output pixels in y direction.
257    StaggerConfig stagger_y;
258
259    // the number of scanner segments used in the current scan
260    unsigned segment_count = 1;
261
262    // the physical pixel positions that are sent to the registers
263    unsigned pixel_startx = 0;
264    unsigned pixel_endx = 0;
265
266    /*  The following defines the ratio between logical pixel count and pixel count setting sent to
267        the scanner. The ratio is affected by the following:
268
269        - Certain scanners just like to multiply the pixel number by a multiplier that depends on
270          the resolution.
271
272        - The sensor may be configured to output one value per multiple physical pixels
273
274        - The scanner will automatically average the pixels that come from the sensor using a
275          certain ratio.
276    */
277    Ratio pixel_count_ratio = Ratio{1, 1};
278
279    // Distance in pixels between consecutive pixels, e.g. between odd and even pixels. Note that
280    // the number of segments can be large.
281    // only on gl124, gl846, gl847
282    unsigned conseq_pixel_dist = 0;
283
284    // The number of "even" pixels to scan. This corresponds to the number of pixels that will be
285    // scanned from a single segment
286    // only on gl124, gl846, gl847
287    unsigned output_segment_pixel_group_count = 0;
288
289    // The number of bytes to skip at start of line during desegmentation.
290    // Currently it's always zero.
291    unsigned output_segment_start_offset = 0;
292
293    // How many pixels the shading data is offset to the right from the acquired data. Calculated
294    // in shading resolution.
295    int shading_pixel_offset = 0;
296
297    // the size of the read buffer.
298    size_t buffer_size_read = 0;
299
300    // whether to enable ledadd functionality
301    bool enable_ledadd = false;
302
303    // whether calibration should be performed host-side
304    bool use_host_side_calib = false;
305
306    // whether gray scanning should be performed host-side (scan as color and merge to gray)
307    bool use_host_side_gray = false;
308
309    void assert_computed() const
310    {
311        if (!computed) {
312            throw std::runtime_error("ScanSession is not computed");
313        }
314    }
315
316    bool operator==(const ScanSession& other) const;
317};
318
319std::ostream& operator<<(std::ostream& out, const ScanSession& session);
320
321template<class Stream>
322void serialize(Stream& str, ScanSession& x)
323{
324    serialize(str, x.params);
325    serialize_newline(str);
326    serialize(str, x.computed);
327    serialize(str, x.full_resolution);
328    serialize(str, x.optical_resolution);
329    serialize(str, x.optical_pixels);
330    serialize(str, x.optical_pixels_raw);
331    serialize(str, x.optical_line_count);
332    serialize(str, x.output_resolution);
333    serialize(str, x.output_startx);
334    serialize(str, x.output_pixels);
335    serialize(str, x.output_channel_bytes);
336    serialize(str, x.output_line_bytes);
337    serialize(str, x.output_line_bytes_raw);
338    serialize(str, x.output_line_bytes_requested);
339    serialize(str, x.output_line_count);
340    serialize(str, x.output_total_bytes_raw);
341    serialize(str, x.output_total_bytes);
342    serialize(str, x.num_staggered_lines);
343    serialize(str, x.max_color_shift_lines);
344    serialize(str, x.color_shift_lines_r);
345    serialize(str, x.color_shift_lines_g);
346    serialize(str, x.color_shift_lines_b);
347    serialize(str, x.stagger_x);
348    serialize(str, x.stagger_y);
349    serialize(str, x.segment_count);
350    serialize(str, x.pixel_startx);
351    serialize(str, x.pixel_endx);
352    serialize(str, x.pixel_count_ratio);
353    serialize(str, x.conseq_pixel_dist);
354    serialize(str, x.output_segment_pixel_group_count);
355    serialize(str, x.output_segment_start_offset);
356    serialize(str, x.shading_pixel_offset);
357    serialize(str, x.buffer_size_read);
358    serialize(str, x.enable_ledadd);
359    serialize(str, x.use_host_side_calib);
360    serialize(str, x.use_host_side_gray);
361}
362
363std::ostream& operator<<(std::ostream& out, const SANE_Parameters& params);
364
365} // namespace genesys
366
367#endif // BACKEND_GENESYS_SETTINGS_H
368