1 /* sane - Scanner Access Now Easy.
2
3 Copyright (C) 2004 -2006 Gerard Klaver (gerard at gkall dot hobby dot nl)
4
5 The teco2 and gl646 backend (Frank Zago) are used as a template for
6 this backend.
7
8 For the usb commands and bayer decoding parts of the following
9 program are used:
10 The pencam2 program (GNU GPL license 2)
11
12 For the usb commands parts of the following programs are used:
13 The libgphoto2 (camlib stv0680) (GNU GPL license 2)
14 The stv680.c/.h kernel module (GNU GPL license 2)
15
16 For the stv680_add_text routine the add_text routine and font_6x11.h file
17 are taken from the webcam.c file, part of xawtv program,
18 (c) 1998-2002 Gerd Knorr (GNU GPL license 2).
19
20 This file is part of the SANE package.
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2 of the
25 License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful, but
28 WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with this program. If not, see <https://www.gnu.org/licenses/>.
34 ---------------------------------------------------------------------
35 */
36
37 /* Commands supported by the vidcam. */
38
39 /*--------------------------------------------------------------------------*/
40
41 static inline int
getbitfield(unsigned char * pageaddr,int mask,int shift)42 getbitfield (unsigned char *pageaddr, int mask, int shift)
43 {
44 return ((*pageaddr >> shift) & mask);
45 }
46
47 /*--------------------------------------------------------------------------*/
48
49 #include <stdio.h>
50
51 #define LIBUSB_TIMEOUT 1000 /* ms */
52
53 typedef unsigned char byte;
54
55 /*--------------------------------------------------------------------------*/
56
57 /* Black magic for color adjustment. */
58 struct dpi_color_adjust
59 {
60 int resolution_x; /* x-resolution */
61 int resolution_y; /* y-resolution */
62
63 int z1_color_0; /* 0, 1 or 2 */
64 int z1_color_1; /* idem */
65 int z1_color_2; /* idem */
66 };
67
68 /*--------------------------------------------------------------------------*/
69
70 enum Stv680_Option
71 {
72 /* Must come first */
73 OPT_NUM_OPTS = 0,
74
75 OPT_MODE_GROUP,
76 OPT_MODE, /* vidcam modes */
77 OPT_RESOLUTION, /* X and Y resolution */
78 OPT_BRIGHTNESS, /* brightness */
79
80 OPT_ENHANCEMENT_GROUP,
81 OPT_WHITE_LEVEL_R, /*white level red correction */
82 OPT_WHITE_LEVEL_G, /*white level green correction */
83 OPT_WHITE_LEVEL_B, /*white level blue correction */
84
85 /* must come last: */
86 OPT_NUM_OPTIONS
87 };
88
89 /*--------------------------------------------------------------------------*/
90
91 /*
92 * Video Camera supported by this backend.
93 */
94 struct vidcam_hardware
95 {
96 /* USB stuff */
97 SANE_Word vendor;
98 SANE_Word product;
99 SANE_Word class;
100
101 /* Readable names */
102 const char *vendor_name; /* brand on the box */
103 const char *product_name; /* name on the box */
104
105 /* Resolutions supported in color mode. */
106 const struct dpi_color_adjust *color_adjust;
107 };
108
109 #define COLOR_RAW_STR SANE_I18N("Color RAW")
110 #define COLOR_RGB_STR SANE_I18N("Color RGB")
111 #define COLOR_RGB_TEXT_STR SANE_I18N("Color RGB TEXT")
112 /*--------------------------------------------------------------------------*/
113
114 /* Define a vidcam occurrence. */
115 typedef struct Stv680_Vidcam
116 {
117 struct Stv680_Vidcam *next;
118 SANE_Device sane;
119
120 char *devicename;
121 SANE_Int fd; /* device handle */
122
123 /* USB handling */
124 size_t buffer_size; /* size of the buffer */
125 SANE_Byte *buffer; /* for USB transfer. */
126
127 /* Bayer handling */
128 size_t output_size; /* size of the output */
129 SANE_Byte *output; /* for bayer conversion */
130
131 size_t image_size; /* allocated size of image */
132 size_t image_begin; /* first significant byte in image */
133 size_t image_end; /* first free byte in image */
134 SANE_Byte *image; /* keep the raw image here */
135
136 /* USB control messages handling */
137 size_t windoww_size; /* size of window write */
138 size_t windowr_size; /* size of window read */
139 SANE_Byte *windoww; /* for window write */
140 SANE_Byte *windowr; /* for window read */
141
142 /* Scanner infos. */
143 const struct vidcam_hardware *hw; /* default options for that vidcam */
144
145 SANE_Word *resolutions_list;
146 SANE_Word *color_sequence_list;
147
148 /* Scanning handling. */
149 SANE_Bool scanning; /* TRUE if a scan is running. */
150 SANE_Bool deliver_eof;
151 int x_resolution; /* X resolution */
152 int y_resolution; /* Y resolution */
153 int depth; /* depth per color */
154 unsigned int colour;
155 int red_s;
156 int green_s;
157 int blue_s;
158
159 SANE_Parameters s_params;
160 enum
161 {
162 STV680_COLOR_RGB,
163 STV680_COLOR_RGB_TEXT,
164 STV680_COLOR,
165 STV680_COLOR_RAW
166 }
167 scan_mode;
168
169 size_t bytes_left; /* number of bytes left to give to the backend */
170 size_t real_bytes_left; /* number of bytes left the vidcam will return. */
171 int bytes_pixel;
172
173 const struct dpi_color_adjust *color_adjust;
174
175 SANE_Parameters params;
176
177 /* Options */
178 SANE_Option_Descriptor opt[OPT_NUM_OPTIONS];
179 Option_Value val[OPT_NUM_OPTIONS];
180
181 unsigned int video_mode; /* 0x0100 = VGA, 0x0000 = CIF,
182 * 0x0300 = QVGA, 0x0200 = QCIF*/
183 unsigned int video_status; /* 0x01=start, 0x02=video, 0x04=busy, 0x08=idle */
184
185 int SupportedModes;
186 int HardwareConfig;
187 int QSIF;
188 int CIF;
189 int VGA;
190 int QVGA;
191 int QCIF;
192 int cwidth; /* camera width */
193 int cheight;
194 int subsample;
195
196 int framecount;
197 char picmsg_ps[50];
198
199 }
200 Stv680_Vidcam;
201
202 /*--------------------------------------------------------------------------*/
203
204 /* Debug levels.
205 * Should be common to all backends. */
206
207 #define DBG_error0 0
208 #define DBG_error 1
209 #define DBG_sense 2
210 #define DBG_warning 3
211 #define DBG_inquiry 4
212 #define DBG_info 5
213 #define DBG_info2 6
214 #define DBG_proc 7
215 #define DBG_read 8
216 #define DBG_sane_init 10
217 #define DBG_sane_proc 11
218 #define DBG_sane_info 12
219 #define DBG_sane_option 13
220
221 /*--------------------------------------------------------*/
222
223 static SANE_Byte red_g[256] = {
224 0, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
225 18, 18, 18, 18, 18, 18, 18, 25, 30, 35, 38, 42,
226 44, 47, 50, 53, 54, 57, 59, 61, 63, 65, 67, 69,
227 71, 71, 73, 75, 77, 78, 80, 81, 82, 84, 85, 87,
228 88, 89, 90, 91, 93, 94, 95, 97, 98, 98, 99, 101,
229 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
230 114, 115, 116, 116, 117, 118, 119, 120, 121, 122, 123, 124,
231 125, 125, 126, 127, 128, 129, 129, 130, 131, 132, 133, 134,
232 134, 135, 135, 136, 137, 138, 139, 140, 140, 141, 142, 143,
233 143, 143, 144, 145, 146, 147, 147, 148, 149, 150, 150, 151,
234 152, 152, 152, 153, 154, 154, 155, 156, 157, 157, 158, 159,
235 159, 160, 161, 161, 161, 162, 163, 163, 164, 165, 165, 166,
236 167, 167, 168, 168, 169, 170, 170, 170, 171, 171, 172, 173,
237 173, 174, 174, 175, 176, 176, 177, 178, 178, 179, 179, 179,
238 180, 180, 181, 181, 182, 183, 183, 184, 184, 185, 185, 186,
239 187, 187, 188, 188, 188, 188, 189, 190, 190, 191, 191, 192,
240 192, 193, 193, 194, 195, 195, 196, 196, 197, 197, 197, 197,
241 198, 198, 199, 199, 200, 201, 201, 202, 202, 203, 203, 204,
242 204, 205, 205, 206, 206, 206, 206, 207, 207, 208, 208, 209,
243 209, 210, 210, 211, 211, 212, 212, 213, 213, 214, 214, 215,
244 215, 215, 215, 216, 216, 217, 217, 218, 218, 218, 219, 219,
245 220, 220, 221, 221
246 };
247
248 static SANE_Byte green_g[256] = {
249 0, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
250 21, 21, 21, 21, 21, 21, 21, 28, 34, 39, 43, 47,
251 50, 53, 56, 59, 61, 64, 66, 68, 71, 73, 75, 77,
252 79, 80, 82, 84, 86, 87, 89, 91, 92, 94, 95, 97,
253 98, 100, 101, 102, 104, 105, 106, 108, 109, 110, 111, 113,
254 114, 115, 116, 117, 118, 120, 121, 122, 123, 124, 125, 126,
255 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
256 139, 140, 141, 142, 143, 144, 144, 145, 146, 147, 148, 149,
257 150, 151, 151, 152, 153, 154, 155, 156, 156, 157, 158, 159,
258 160, 160, 161, 162, 163, 164, 164, 165, 166, 167, 167, 168,
259 169, 170, 170, 171, 172, 172, 173, 174, 175, 175, 176, 177,
260 177, 178, 179, 179, 180, 181, 182, 182, 183, 184, 184, 185,
261 186, 186, 187, 187, 188, 189, 189, 190, 191, 191, 192, 193,
262 193, 194, 194, 195, 196, 196, 197, 198, 198, 199, 199, 200,
263 201, 201, 202, 202, 203, 204, 204, 205, 205, 206, 206, 207,
264 208, 208, 209, 209, 210, 210, 211, 212, 212, 213, 213, 214,
265 214, 215, 215, 216, 217, 217, 218, 218, 219, 219, 220, 220,
266 221, 221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227,
267 227, 228, 228, 229, 229, 230, 230, 231, 231, 232, 232, 233,
268 233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 238, 239,
269 239, 240, 240, 241, 241, 242, 242, 243, 243, 243, 244, 244,
270 245, 245, 246, 246
271 };
272
273 static SANE_Byte blue_g[256] = {
274 0, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
275 23, 23, 23, 23, 23, 23, 23, 30, 37, 42, 47, 51,
276 55, 58, 61, 64, 67, 70, 72, 74, 78, 80, 82, 84,
277 86, 88, 90, 92, 94, 95, 97, 100, 101, 103, 104, 106,
278 107, 110, 111, 112, 114, 115, 116, 118, 119, 121, 122, 124,
279 125, 126, 127, 128, 129, 132, 133, 134, 135, 136, 137, 138,
280 139, 140, 141, 143, 144, 145, 146, 147, 148, 149, 150, 151,
281 152, 154, 155, 156, 157, 158, 158, 159, 160, 161, 162, 163,
282 165, 166, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174,
283 176, 176, 177, 178, 179, 180, 180, 181, 182, 183, 183, 184,
284 185, 187, 187, 188, 189, 189, 190, 191, 192, 192, 193, 194,
285 194, 195, 196, 196, 198, 199, 200, 200, 201, 202, 202, 203,
286 204, 204, 205, 205, 206, 207, 207, 209, 210, 210, 211, 212,
287 212, 213, 213, 214, 215, 215, 216, 217, 217, 218, 218, 220,
288 221, 221, 222, 222, 223, 224, 224, 225, 225, 226, 226, 227,
289 228, 228, 229, 229, 231, 231, 232, 233, 233, 234, 234, 235,
290 235, 236, 236, 237, 238, 238, 239, 239, 240, 240, 242, 242,
291 243, 243, 244, 244, 245, 246, 246, 247, 247, 248, 248, 249,
292 249, 250, 250, 251, 251, 253, 253, 254, 254, 255, 255, 255,
293 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
294 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
295 255, 255, 255, 255
296 };
297