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