1 /* sane - Scanner Access Now Easy.
2 
3    Copyright (C) 2003, 2004 Henning Meier-Geinitz <henning@meier-geinitz.de>
4    Copyright (C) 2005-2013 Stephane Voltz <stef.dev@free.fr>
5    Copyright (C) 2006 Laurent Charpentier <laurent_pubs@yahoo.com>
6    Copyright (C) 2009 Pierre Willenbrock <pierre@pirsoft.dnsalias.org>
7 
8    This file is part of the SANE package.
9 
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of the
13    License, or (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <https://www.gnu.org/licenses/>.
22 
23    As a special exception, the authors of SANE give permission for
24    additional uses of the libraries contained in this release of SANE.
25 
26    The exception is that, if you link a SANE library with other files
27    to produce an executable, this does not by itself cause the
28    resulting executable to be covered by the GNU General Public
29    License.  Your use of that executable is in no way restricted on
30    account of linking the SANE library code into it.
31 
32    This exception does not, however, invalidate any other reasons why
33    the executable file might be covered by the GNU General Public
34    License.
35 
36    If you submit changes to SANE to the maintainers to be included in
37    a subsequent release, you agree by submitting the changes that
38    those changes may be distributed with this exception intact.
39 
40    If you write modifications of your own for SANE, it is your choice
41    whether to permit this exception to apply to your modifications.
42    If you do not wish that, delete this exception notice.
43 */
44 
45 #ifndef GENESYS_H
46 #define GENESYS_H
47 
48 #ifndef BACKEND_NAME
49 # define BACKEND_NAME genesys
50 #endif
51 
52 #include "low.h"
53 #include <queue>
54 
55 #ifndef PATH_MAX
56 # define PATH_MAX	1024
57 #endif
58 
59 #if defined(_WIN32) || defined(HAVE_OS2_H)
60 # define PATH_SEP	'\\'
61 #else
62 # define PATH_SEP	'/'
63 #endif
64 
65 
66 #define ENABLE(OPTION)  s->opt[OPTION].cap &= ~SANE_CAP_INACTIVE
67 #define DISABLE(OPTION) s->opt[OPTION].cap |=  SANE_CAP_INACTIVE
68 #define IS_ACTIVE(OPTION) (((s->opt[OPTION].cap) & SANE_CAP_INACTIVE) == 0)
69 
70 #define GENESYS_CONFIG_FILE "genesys.conf"
71 
72 #ifndef SANE_I18N
73 #define SANE_I18N(text) text
74 #endif
75 
76 #define STR_FLATBED SANE_I18N("Flatbed")
77 #define STR_TRANSPARENCY_ADAPTER SANE_I18N("Transparency Adapter")
78 #define STR_TRANSPARENCY_ADAPTER_INFRARED SANE_I18N("Transparency Adapter Infrared")
79 
80 namespace genesys {
81 
82 /** List of SANE options
83  */
84 enum Genesys_Option
85 {
86   OPT_NUM_OPTS = 0,
87 
88   OPT_MODE_GROUP,
89   OPT_MODE,
90   OPT_SOURCE,
91   OPT_PREVIEW,
92   OPT_BIT_DEPTH,
93   OPT_RESOLUTION,
94 
95   OPT_GEOMETRY_GROUP,
96   OPT_TL_X,			/* top-left x */
97   OPT_TL_Y,			/* top-left y */
98   OPT_BR_X,			/* bottom-right x */
99   OPT_BR_Y,			/* bottom-right y */
100 
101   /* advanced image enhancement options */
102   OPT_ENHANCEMENT_GROUP,
103   OPT_CUSTOM_GAMMA,		/* toggle to enable custom gamma tables */
104   OPT_GAMMA_VECTOR,
105   OPT_GAMMA_VECTOR_R,
106   OPT_GAMMA_VECTOR_G,
107   OPT_GAMMA_VECTOR_B,
108   OPT_BRIGHTNESS,
109   OPT_CONTRAST,
110 
111   OPT_EXTRAS_GROUP,
112   OPT_LAMP_OFF_TIME,
113   OPT_LAMP_OFF,
114   OPT_COLOR_FILTER,
115   OPT_CALIBRATION_FILE,
116   OPT_EXPIRATION_TIME,
117 
118   OPT_SENSOR_GROUP,
119   OPT_SCAN_SW,
120   OPT_FILE_SW,
121   OPT_EMAIL_SW,
122   OPT_COPY_SW,
123   OPT_PAGE_LOADED_SW,
124   OPT_OCR_SW,
125   OPT_POWER_SW,
126   OPT_EXTRA_SW,
127   OPT_NEED_CALIBRATION_SW,
128   OPT_BUTTON_GROUP,
129   OPT_CALIBRATE,
130   OPT_CLEAR_CALIBRATION,
131   OPT_FORCE_CALIBRATION,
132   OPT_IGNORE_OFFSETS,
133 
134   /* must come last: */
135   NUM_OPTIONS
136 };
137 
138 enum GenesysButtonName : unsigned {
139     BUTTON_SCAN_SW = 0,
140     BUTTON_FILE_SW,
141     BUTTON_EMAIL_SW,
142     BUTTON_COPY_SW,
143     BUTTON_PAGE_LOADED_SW,
144     BUTTON_OCR_SW,
145     BUTTON_POWER_SW,
146     BUTTON_EXTRA_SW,
147     NUM_BUTTONS
148 };
149 
150 GenesysButtonName genesys_option_to_button(int option);
151 
152 class GenesysButton {
153 public:
write(bool value)154     void write(bool value)
155     {
156         if (value == value_) {
157             return;
158         }
159         values_to_read_.push(value);
160         value_ = value;
161     }
162 
read()163     bool read()
164     {
165         if (values_to_read_.empty()) {
166             return value_;
167         }
168         bool ret = values_to_read_.front();
169         values_to_read_.pop();
170         return ret;
171     }
172 
173 private:
174     bool value_ = false;
175     std::queue<bool> values_to_read_;
176 };
177 
178 /** Scanner object. Should have better be called Session than Scanner
179  */
180 struct Genesys_Scanner
181 {
182     Genesys_Scanner() = default;
183     ~Genesys_Scanner() = default;
184 
185     // Next scanner in list
186     struct Genesys_Scanner *next;
187 
188     // Low-level device object
189     Genesys_Device* dev = nullptr;
190 
191     // SANE data
192     // We are currently scanning
193     bool scanning;
194     // Option descriptors
195     SANE_Option_Descriptor opt[NUM_OPTIONS];
196 
197     std::vector<SANE_Word> opt_resolution_values;
198     SANE_Range opt_x_range = {};
199     SANE_Range opt_y_range = {};
200     std::vector<const char*> opt_source_values;
201 
202     // Option values
203     SANE_Word bit_depth = 0;
204     SANE_Word resolution = 0;
205     bool preview = false; // TODO: currently not used
206     bool lamp_off = false;
207     SANE_Word lamp_off_time = 0;
208     SANE_Word contrast = 0;
209     SANE_Word brightness = 0;
210     SANE_Word expiration_time = 0;
211     bool custom_gamma = false;
212 
213     SANE_Word pos_top_left_y = 0;
214     SANE_Word pos_top_left_x = 0;
215     SANE_Word pos_bottom_right_y = 0;
216     SANE_Word pos_bottom_right_x = 0;
217 
218     std::string mode, color_filter;
219 
220     // the value of the source option
221     ScanMethod scan_method = ScanMethod::FLATBED;
222 
223     std::string calibration_file;
224     // Button states
225     GenesysButton buttons[NUM_BUTTONS];
226 
227     // SANE Parameters
228     SANE_Parameters params = {};
229     SANE_Int bpp_list[5] = {};
230 };
231 
232 void write_calibration(std::ostream& str, Genesys_Device::Calibration& cache);
233 bool read_calibration(std::istream& str, Genesys_Device::Calibration& cache,
234                       const std::string& path);
235 
236 } // namespace genesys
237 
238 #endif /* not GENESYS_H */
239