• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* sane - Scanner Access Now Easy.
2 
3    Copyright (C) 2000 Mustek.
4    Originally maintained by Tom Wang <tom.wang@mustek.com.tw>
5 
6    Copyright (C) 2001, 2002 by Henning Meier-Geinitz.
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    This file implements a SANE backend for Mustek 1200UB and similar
45    USB flatbed scanners.  */
46 
47 #ifndef mustek_usb_high_h
48 #define mustek_usb_high_h
49 #include "mustek_usb_mid.h"
50 
51 /* ---------------------------------- macros ------------------------------ */
52 
53 #define I8O8RGB 0
54 #define I8O8MONO 1
55 #define I4O1MONO 2
56 
57 /* ---------------------------------- types ------------------------------- */
58 
59 struct Mustek_Usb_Device;
60 
61 typedef SANE_Status (*Powerdelay_Function) (ma1017 *, SANE_Byte);
62 
63 typedef SANE_Status
64   (*Getline_Function) (struct Mustek_Usb_Device * dev, SANE_Byte *,
65 		       SANE_Bool is_order_invert);
66 
67 typedef SANE_Status (*Backtrack_Function) (struct Mustek_Usb_Device * dev);
68 
69 typedef enum Colormode
70 {
71   RGB48 = 0,
72   RGB42 = 1,
73   RGB36 = 2,
74   RGB30 = 3,
75   RGB24 = 4,
76   GRAY16 = 5,
77   GRAY14 = 6,
78   GRAY12 = 7,
79   GRAY10 = 8,
80   GRAY8 = 9,
81   TEXT = 10,
82   RGB48EXT = 11,
83   RGB42EXT = 12,
84   RGB36EXT = 13,
85   RGB30EXT = 14,
86   RGB24EXT = 15,
87   GRAY16EXT = 16,
88   GRAY14EXT = 17,
89   GRAY12EXT = 18,
90   GRAY10EXT = 19,
91   GRAY8EXT = 20,
92   TEXTEXT = 21
93 }
94 Colormode;
95 
96 typedef enum Signal_State
97 {
98   SS_UNKNOWN = 0,
99   SS_BRIGHTER = 1,
100   SS_DARKER = 2,
101   SS_EQUAL = 3
102 }
103 Signal_State;
104 
105 typedef struct Calibrator
106 {
107   /* Calibration Data */
108   SANE_Bool is_prepared;
109   SANE_Word *k_white;
110   SANE_Word *k_dark;
111   /* Working Buffer */
112   double *white_line;
113   double *dark_line;
114   SANE_Int *white_buffer;
115   /* Necessary Parameters */
116   SANE_Word k_white_level;
117   SANE_Word k_dark_level;
118   SANE_Word major_average;
119   SANE_Word minor_average;
120   SANE_Word filter;
121   SANE_Word white_needed;
122   SANE_Word dark_needed;
123   SANE_Word max_width;
124   SANE_Word width;
125   SANE_Word threshold;
126   SANE_Word *gamma_table;
127   SANE_Byte calibrator_type;
128 }
129 Calibrator;
130 
131 enum Mustek_Usb_Modes
132 {
133   MUSTEK_USB_MODE_LINEART = 0,
134   MUSTEK_USB_MODE_GRAY,
135   MUSTEK_USB_MODE_COLOR
136 };
137 
138 enum Mustek_Usb_Option
139 {
140   OPT_NUM_OPTS = 0,
141 
142   OPT_MODE_GROUP,
143   OPT_MODE,
144   OPT_RESOLUTION,
145   OPT_PREVIEW,
146 
147   OPT_GEOMETRY_GROUP,		/* 5 */
148   OPT_TL_X,			/* top-left x */
149   OPT_TL_Y,			/* top-left y */
150   OPT_BR_X,			/* bottom-right x */
151   OPT_BR_Y,			/* bottom-right y */
152 
153   OPT_ENHANCEMENT_GROUP,	/* 10 */
154   OPT_THRESHOLD,
155   OPT_CUSTOM_GAMMA,
156   OPT_GAMMA_VECTOR,
157   OPT_GAMMA_VECTOR_R,
158   OPT_GAMMA_VECTOR_G,
159   OPT_GAMMA_VECTOR_B,
160 
161   /* must come last: */
162   NUM_OPTIONS
163 };
164 
165 typedef struct Mustek_Usb_Device
166 {
167   struct Mustek_Usb_Device *next;
168   SANE_String name;
169   SANE_Device sane;
170   SANE_Range dpi_range;
171   SANE_Range x_range;
172   SANE_Range y_range;
173   /* max width & max height in 300 dpi */
174   SANE_Int max_width;
175   SANE_Int max_height;
176 
177   ma1017 *chip;			/* registers of the scanner controller chip */
178 
179   Colormode scan_mode;
180   SANE_Word x_dpi;
181   SANE_Word y_dpi;
182   SANE_Word x;
183   SANE_Word y;
184   SANE_Word width;
185   SANE_Word height;
186   SANE_Word bytes_per_row;
187   SANE_Word bpp;
188 
189   SANE_Byte *scan_buffer;
190   SANE_Byte *scan_buffer_start;
191   size_t scan_buffer_len;
192 
193   SANE_Byte *temp_buffer;
194   SANE_Byte *temp_buffer_start;
195   size_t temp_buffer_len;
196 
197   SANE_Word line_switch;
198   SANE_Word line_offset;
199 
200   SANE_Bool is_cis_detected;
201 
202   SANE_Word init_bytes_per_strip;
203   SANE_Word adjust_length_300;
204   SANE_Word adjust_length_600;
205   SANE_Word init_min_expose_time;
206   SANE_Word init_skips_per_row_300;
207   SANE_Word init_skips_per_row_600;
208   SANE_Word init_j_lines;
209   SANE_Word init_k_lines;
210   SANE_Word init_k_filter;
211   SANE_Int init_k_loops;
212   SANE_Word init_pixel_rate_lines;
213   SANE_Word init_pixel_rate_filts;
214   SANE_Word init_powerdelay_lines;
215   SANE_Word init_home_lines;
216   SANE_Word init_dark_lines;
217   SANE_Word init_k_level;
218   SANE_Byte init_max_power_delay;
219   SANE_Byte init_min_power_delay;
220   SANE_Byte init_adjust_way;
221   double init_green_black_factor;
222   double init_blue_black_factor;
223   double init_red_black_factor;
224   double init_gray_black_factor;
225   double init_green_factor;
226   double init_blue_factor;
227   double init_red_factor;
228   double init_gray_factor;
229 
230   SANE_Int init_red_rgb_600_pga;
231   SANE_Int init_green_rgb_600_pga;
232   SANE_Int init_blue_rgb_600_pga;
233   SANE_Int init_mono_600_pga;
234   SANE_Int init_red_rgb_300_pga;
235   SANE_Int init_green_rgb_300_pga;
236   SANE_Int init_blue_rgb_300_pga;
237   SANE_Int init_mono_300_pga;
238   SANE_Word init_expose_time;
239   SANE_Byte init_red_rgb_600_power_delay;
240   SANE_Byte init_green_rgb_600_power_delay;
241   SANE_Byte init_blue_rgb_600_power_delay;
242   SANE_Byte init_red_mono_600_power_delay;
243   SANE_Byte init_green_mono_600_power_delay;
244   SANE_Byte init_blue_mono_600_power_delay;
245   SANE_Byte init_red_rgb_300_power_delay;
246   SANE_Byte init_green_rgb_300_power_delay;
247   SANE_Byte init_blue_rgb_300_power_delay;
248   SANE_Byte init_red_mono_300_power_delay;
249   SANE_Byte init_green_mono_300_power_delay;
250   SANE_Byte init_blue_mono_300_power_delay;
251   SANE_Byte init_threshold;
252 
253   SANE_Byte init_top_ref;
254   SANE_Byte init_front_end;
255   SANE_Byte init_red_offset;
256   SANE_Byte init_green_offset;
257   SANE_Byte init_blue_offset;
258 
259   SANE_Int init_rgb_24_back_track;
260   SANE_Int init_mono_8_back_track;
261 
262   SANE_Bool is_open;
263   SANE_Bool is_prepared;
264   SANE_Word expose_time;
265   SANE_Word dummy;
266   SANE_Word bytes_per_strip;
267   SANE_Byte *image_buffer;
268   SANE_Byte *red;
269   SANE_Byte *green;
270   SANE_Byte *blue;
271   Getline_Function get_line;
272   Backtrack_Function backtrack;
273   SANE_Bool is_adjusted_rgb_600_power_delay;
274   SANE_Bool is_adjusted_mono_600_power_delay;
275   SANE_Bool is_adjusted_rgb_300_power_delay;
276   SANE_Bool is_adjusted_mono_300_power_delay;
277   SANE_Bool is_evaluate_pixel_rate;
278   SANE_Int red_rgb_600_pga;
279   SANE_Int green_rgb_600_pga;
280   SANE_Int blue_rgb_600_pga;
281   SANE_Int mono_600_pga;
282   SANE_Byte red_rgb_600_power_delay;
283   SANE_Byte green_rgb_600_power_delay;
284   SANE_Byte blue_rgb_600_power_delay;
285   SANE_Byte red_mono_600_power_delay;
286   SANE_Byte green_mono_600_power_delay;
287   SANE_Byte blue_mono_600_power_delay;
288   SANE_Int red_rgb_300_pga;
289   SANE_Int green_rgb_300_pga;
290   SANE_Int blue_rgb_300_pga;
291   SANE_Int mono_300_pga;
292   SANE_Byte red_rgb_300_power_delay;
293   SANE_Byte green_rgb_300_power_delay;
294   SANE_Byte blue_rgb_300_power_delay;
295   SANE_Byte red_mono_300_power_delay;
296   SANE_Byte green_mono_300_power_delay;
297   SANE_Byte blue_mono_300_power_delay;
298   SANE_Word pixel_rate;
299   SANE_Byte threshold;
300   SANE_Word *gamma_table;
301   SANE_Word skips_per_row;
302 
303   /* CCD */
304   SANE_Bool is_adjusted_mono_600_offset;
305   SANE_Bool is_adjusted_mono_600_exposure;
306   SANE_Word mono_600_exposure;
307 
308   Calibrator *red_calibrator;
309   Calibrator *green_calibrator;
310   Calibrator *blue_calibrator;
311   Calibrator *mono_calibrator;
312 
313   SANE_Char device_name[256];
314 
315   SANE_Bool is_sensor_detected;
316 }
317 Mustek_Usb_Device;
318 
319 typedef struct Mustek_Usb_Scanner
320 {
321   /* all the state needed to define a scan request: */
322   struct Mustek_Usb_Scanner *next;
323 
324   SANE_Option_Descriptor opt[NUM_OPTIONS];
325   Option_Value val[NUM_OPTIONS];
326 
327   SANE_Int channels;
328 
329   /* scan window in inches: top left x+y and width+height */
330   double tl_x;
331   double tl_y;
332   double width;
333   double height;
334   /* scan window in dots (at current resolution):
335      top left x+y and width+height */
336   SANE_Int tl_x_dots;
337   SANE_Int tl_y_dots;
338   SANE_Int width_dots;
339   SANE_Int height_dots;
340 
341   SANE_Word bpp;
342 
343   SANE_Bool scanning;
344   SANE_Parameters params;
345   SANE_Word read_rows;
346   SANE_Word red_gamma_table[256];
347   SANE_Word green_gamma_table[256];
348   SANE_Word blue_gamma_table[256];
349   SANE_Word gray_gamma_table[256];
350   SANE_Word linear_gamma_table[256];
351   SANE_Word *red_table;
352   SANE_Word *green_table;
353   SANE_Word *blue_table;
354   SANE_Word *gray_table;
355   SANE_Word total_bytes;
356   SANE_Word total_lines;
357   /* scanner dependent/low-level state: */
358   Mustek_Usb_Device *hw;
359 }
360 Mustek_Usb_Scanner;
361 
362 
363 /* ------------------- calibration function declarations ------------------ */
364 
365 
366 static SANE_Status
367 usb_high_cal_init (Calibrator * cal, SANE_Byte type, SANE_Word target_white,
368 		   SANE_Word target_dark);
369 
370 static SANE_Status usb_high_cal_exit (Calibrator * cal);
371 
372 static SANE_Status
373 usb_high_cal_embed_gamma (Calibrator * cal, SANE_Word * gamma_table);
374 
375 static SANE_Status
376 usb_high_cal_prepare (Calibrator * cal, SANE_Word max_width);
377 
378 static SANE_Status
379 usb_high_cal_setup (Calibrator * cal, SANE_Word major_average,
380 		    SANE_Word minor_average, SANE_Word filter,
381 		    SANE_Word width, SANE_Word * white_needed,
382 		    SANE_Word * dark_needed);
383 
384 static SANE_Status
385 usb_high_cal_evaluate_white (Calibrator * cal, double factor);
386 
387 static SANE_Status
388 usb_high_cal_evaluate_dark (Calibrator * cal, double factor);
389 
390 static SANE_Status usb_high_cal_evaluate_calibrator (Calibrator * cal);
391 
392 static SANE_Status
393 usb_high_cal_fill_in_white (Calibrator * cal, SANE_Word major,
394 			    SANE_Word minor, void *white_pattern);
395 
396 static SANE_Status
397 usb_high_cal_fill_in_dark (Calibrator * cal, SANE_Word major,
398 			   SANE_Word minor, void *dark_pattern);
399 
400 static SANE_Status
401 usb_high_cal_calibrate (Calibrator * cal, void *src, void *target);
402 
403 static SANE_Status
404 usb_high_cal_i8o8_fill_in_white (Calibrator * cal, SANE_Word major,
405 				 SANE_Word minor, void *white_pattern);
406 
407 static SANE_Status
408 usb_high_cal_i8o8_fill_in_dark (Calibrator * cal, SANE_Word major,
409 				SANE_Word minor, void *dark_pattern);
410 
411 static SANE_Status
412 usb_high_cal_i8o8_mono_calibrate (Calibrator * cal, void *src, void *target);
413 
414 static SANE_Status
415 usb_high_cal_i8o8_rgb_calibrate (Calibrator * cal, void *src, void *target);
416 
417 static SANE_Status
418 usb_high_cal_i4o1_fill_in_white (Calibrator * cal, SANE_Word major,
419 				 SANE_Word minor, void *white_pattern);
420 
421 static SANE_Status
422 usb_high_cal_i4o1_fill_in_dark (Calibrator * cal, SANE_Word major,
423 				SANE_Word minor, void *dark_pattern);
424 
425 static SANE_Status
426 usb_high_cal_i4o1_calibrate (Calibrator * cal, void *src, void *target);
427 
428 /* -------------------- scanning function declarations -------------------- */
429 
430 static SANE_Status usb_high_scan_init (Mustek_Usb_Device * dev);
431 
432 static SANE_Status usb_high_scan_exit (Mustek_Usb_Device * dev);
433 
434 static SANE_Status usb_high_scan_prepare (Mustek_Usb_Device * dev);
435 
436 static SANE_Status usb_high_scan_clearup (Mustek_Usb_Device * dev);
437 
438 static SANE_Status
439 usb_high_scan_turn_power (Mustek_Usb_Device * dev, SANE_Bool is_on);
440 
441 static SANE_Status usb_high_scan_back_home (Mustek_Usb_Device * dev);
442 
443 static SANE_Status
444 usb_high_scan_set_threshold (Mustek_Usb_Device * dev, SANE_Byte threshold);
445 
446 static SANE_Status
447 usb_high_scan_embed_gamma (Mustek_Usb_Device * dev, SANE_Word * gamma_table);
448 
449 static SANE_Status usb_high_scan_reset (Mustek_Usb_Device * dev);
450 
451 static SANE_Status
452 usb_high_scan_suggest_parameters (Mustek_Usb_Device * dev, SANE_Word dpi,
453 				  SANE_Word x, SANE_Word y, SANE_Word width,
454 				  SANE_Word height, Colormode color_mode);
455 static SANE_Status usb_high_scan_detect_sensor (Mustek_Usb_Device * dev);
456 
457 static SANE_Status
458 usb_high_scan_setup_scan (Mustek_Usb_Device * dev, Colormode color_mode,
459 			  SANE_Word x_dpi, SANE_Word y_dpi,
460 			  SANE_Bool is_invert, SANE_Word x, SANE_Word y,
461 			  SANE_Word width);
462 
463 static SANE_Status
464 usb_high_scan_get_rows (Mustek_Usb_Device * dev, SANE_Byte * block,
465 			SANE_Word rows, SANE_Bool is_order_invert);
466 
467 static SANE_Status usb_high_scan_stop_scan (Mustek_Usb_Device * dev);
468 
469 static SANE_Status
470 usb_high_scan_step_forward (Mustek_Usb_Device * dev, SANE_Int step_count);
471 
472 static SANE_Status
473 usb_high_scan_safe_forward (Mustek_Usb_Device * dev, SANE_Int step_count);
474 
475 static SANE_Status
476 usb_high_scan_init_asic (Mustek_Usb_Device * dev, Sensor_Type sensor);
477 
478 static SANE_Status usb_high_scan_wait_carriage_home (Mustek_Usb_Device * dev);
479 
480 static SANE_Status
481 usb_high_scan_hardware_calibration (Mustek_Usb_Device * dev);
482 
483 static SANE_Status usb_high_scan_line_calibration (Mustek_Usb_Device * dev);
484 
485 static SANE_Status usb_high_scan_prepare_scan (Mustek_Usb_Device * dev);
486 
487 static SANE_Word
488 usb_high_scan_calculate_max_rgb_600_expose (Mustek_Usb_Device * dev,
489 					    SANE_Byte * ideal_red_pd,
490 					    SANE_Byte * ideal_green_pd,
491 					    SANE_Byte * ideal_blue_pd);
492 
493 static SANE_Word
494 usb_high_scan_calculate_max_mono_600_expose (Mustek_Usb_Device * dev,
495 					     SANE_Byte * ideal_red_pd,
496 					     SANE_Byte * ideal_green_pd,
497 					     SANE_Byte * ideal_blue_pd);
498 
499 static SANE_Status
500 usb_high_scan_prepare_rgb_signal_600_dpi (Mustek_Usb_Device * dev);
501 
502 static SANE_Status
503 usb_high_scan_prepare_mono_signal_600_dpi (Mustek_Usb_Device * dev);
504 
505 static SANE_Word
506 usb_high_scan_calculate_max_rgb_300_expose (Mustek_Usb_Device * dev,
507 					    SANE_Byte * ideal_red_pd,
508 					    SANE_Byte * ideal_green_pd,
509 					    SANE_Byte * ideal_blue_pd);
510 
511 static SANE_Word
512 usb_high_scan_calculate_max_mono_300_expose (Mustek_Usb_Device * dev,
513 					     SANE_Byte * ideal_red_pd,
514 					     SANE_Byte * ideal_green_pd,
515 					     SANE_Byte * ideal_blue_pd);
516 
517 static SANE_Status
518 usb_high_scan_prepare_rgb_signal_300_dpi (Mustek_Usb_Device * dev);
519 
520 static SANE_Status
521 usb_high_scan_prepare_mono_signal_300_dpi (Mustek_Usb_Device * dev);
522 
523 static SANE_Status
524 usb_high_scan_evaluate_max_level (Mustek_Usb_Device * dev,
525 				  SANE_Word sample_lines,
526 				  SANE_Int sample_length,
527 				  SANE_Byte * ret_max_level);
528 
529 static SANE_Status
530 usb_high_scan_bssc_power_delay (Mustek_Usb_Device * dev,
531 				Powerdelay_Function set_power_delay,
532 				Signal_State * signal_state,
533 				SANE_Byte * target, SANE_Byte max,
534 				SANE_Byte min, SANE_Byte threshold,
535 				SANE_Int length);
536 
537 static SANE_Status
538 usb_high_scan_adjust_rgb_600_power_delay (Mustek_Usb_Device * dev);
539 
540 static SANE_Status
541 usb_high_scan_adjust_mono_600_power_delay (Mustek_Usb_Device * dev);
542 
543 static SANE_Status
544 usb_high_scan_adjust_mono_600_exposure (Mustek_Usb_Device * dev);
545 
546 #if 0
547 /* CCD */
548 static SANE_Status
549 usb_high_scan_adjust_mono_600_offset (Mustek_Usb_Device * dev);
550 
551 static SANE_Status
552 usb_high_scan_adjust_mono_600_pga (Mustek_Usb_Device * dev);
553 
554 static SANE_Status
555 usb_high_scan_adjust_mono_600_skips_per_row (Mustek_Usb_Device * dev);
556 #endif
557 
558 static SANE_Status
559 usb_high_scan_adjust_rgb_300_power_delay (Mustek_Usb_Device * dev);
560 
561 static SANE_Status
562 usb_high_scan_adjust_mono_300_power_delay (Mustek_Usb_Device * dev);
563 
564 static SANE_Status
565 usb_high_scan_evaluate_pixel_rate (Mustek_Usb_Device * dev);
566 
567 static SANE_Status usb_high_scan_calibration_rgb_24 (Mustek_Usb_Device * dev);
568 
569 static SANE_Status usb_high_scan_calibration_mono_8 (Mustek_Usb_Device * dev);
570 
571 static SANE_Status usb_high_scan_prepare_rgb_24 (Mustek_Usb_Device * dev);
572 
573 static SANE_Status usb_high_scan_prepare_mono_8 (Mustek_Usb_Device * dev);
574 
575 static SANE_Status
576 usb_high_scan_get_rgb_24_bit_line (Mustek_Usb_Device * dev,
577 				   SANE_Byte * line,
578 				   SANE_Bool is_order_invert);
579 
580 static SANE_Status
581 usb_high_scan_get_mono_8_bit_line (Mustek_Usb_Device * dev,
582 				   SANE_Byte * line,
583 				   SANE_Bool is_order_invert);
584 
585 static SANE_Status usb_high_scan_backtrack_rgb_24 (Mustek_Usb_Device * dev);
586 
587 static SANE_Status usb_high_scan_backtrack_mono_8 (Mustek_Usb_Device * dev);
588 
589 #endif /* mustek_usb_high_h */
590