forked from hean01/iscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepkowa.c
6513 lines (5408 loc) · 180 KB
/
epkowa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* epkowa.c - SANE backend for EPSON flatbed scanners
(Image Scan! version)
Based on the SANE Epson backend (originally from sane-1.0.3)
- updated to sane-backends-1.0.6
- renamed from epson to epkowa to avoid confusion
- updated to sane-backends-1.0.12
- updated to sane-backends-1.0.15
Based on Kazuhiro Sasayama previous
Work on epson.[ch] file from the SANE package.
Original code taken from sane-0.71
Copyright (C) 1997 Hypercore Software Design, Ltd.
modifications
Copyright (C) 1998-1999 Christian Bucher <bucher@vernetzt.at>
Copyright (C) 1998-1999 Kling & Hautzinger GmbH
Copyright (C) 1999 Norihiko Sawa <sawa@yb3.so-net.ne.jp>
Copyright (C) 2000 Mike Porter <mike@udel.edu> (mjp)
Copyright (C) 1999-2004 Karl Heinz Kremer <khk@khk.net>
Copyright (C) 2001-2016 SEIKO EPSON CORPORATION
This file is part of the EPKOWA SANE backend.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
As a special exception, the authors of SANE give permission for
additional uses of the libraries contained in this release of SANE.
The exception is that, if you link a SANE library with other files
to produce an executable, this does not by itself cause the
resulting executable to be covered by the GNU General Public
License. Your use of that executable is in no way restricted on
account of linking the SANE library code into it.
This exception does not, however, invalidate any other reasons why
the executable file might be covered by the GNU General Public
License.
If you submit changes to SANE to the maintainers to be included in
a subsequent release, you agree by submitting the changes that
those changes may be distributed with this exception intact.
If you write modifications of your own for SANE, it is your choice
whether to permit this exception to apply to your modifications.
If you do not wish that, delete this exception notice.
*/
#ifndef SANE_I18N
#define SANE_I18N(text) (text)
#endif
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <limits.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>
#include <sane/saneopts.h>
#ifndef BACKEND_NAME
#define BACKEND_NAME epkowa
#endif
#include "backend.h"
#include "command.h"
#include "hw-data.h"
#include "utils.h"
#include "timing.h"
#include "cfg-obj.h"
#include "dip-obj.h"
#include "model-info.h"
#include "utils.h"
#include "epkowa_scsi.h"
#include "sane/sanei_pio.h"
#include "epkowa_ip.h" /* interpreter-based scanner support */
#include "sane/sanei.h"
#define DEFAULT_RESOLUTION 300 /* dpi */
#define DEFAULT_X_RESOLUTION DEFAULT_RESOLUTION
#define DEFAULT_Y_RESOLUTION DEFAULT_RESOLUTION
#define S_ACK "\006"
#define S_CAN "\030"
/* Usable values when defining EPSON_LEVEL_DEFAULT
* There is also a function level "A5", used for the GT-300, a
* monochrome only scanner. This level is not supported.
*/
#define EPSON_LEVEL_A1 0
#define EPSON_LEVEL_A2 1
#define EPSON_LEVEL_B1 2
#define EPSON_LEVEL_B2 3
#define EPSON_LEVEL_B3 4
#define EPSON_LEVEL_B4 5
#define EPSON_LEVEL_B5 6
#define EPSON_LEVEL_B6 7
#define EPSON_LEVEL_B7 8
#define EPSON_LEVEL_B8 9
#define EPSON_LEVEL_F5 10
#define EPSON_LEVEL_D1 11
#define EPSON_LEVEL_D2 12
#define EPSON_LEVEL_D7 13
#define EPSON_LEVEL_D8 14
#define EPSON_LEVEL_DEFAULT EPSON_LEVEL_B3
static EpsonCmdRec epson_cmd[] = {
/*
* request identity
* | request identity2
* | | request status
* | | | request condition
* | | | | set color mode
* | | | | | start scanning
* | | | | | | set data format
* | | | | | | | set resolution
* | | | | | | | | set zoom
* | | | | | | | | | set scan area
* | | | | | | | | | | set brightness
* | | | | | | | | | | | set gamma
* | | | | | | | | | | | | set halftoning
* | | | | | | | | | | | | | set color correction
* | | | | | | | | | | | | | | initialize scanner
* | | | | | | | | | | | | | | | set speed
* | | | | | | | | | | | | | | | | set lcount
* | | | | | | | | | | | | | | | | | mirror image
* | | | | | | | | | | | | | | | | | | set gamma table
* | | | | | | | | | | | | | | | | | | | set outline emphasis
* | | | | | | | | | | | | | | | | | | | | set dither
* | | | | | | | | | | | | | | | | | | | | | set color correction coefficients
* | | | | | | | | | | | | | | | | | | | | | | request extension status
* | | | | | | | | | | | | | | | | | | | | | | | control an extension
* | | | | | | | | | | | | | | | | | | | | | | | | forward feed / eject
* | | | | | | | | | | | | | | | | | | | | | | | | | feed
* | | | | | | | | | | | | | | | | | | | | | | | | | | request push button status
* | | | | | | | | | | | | | | | | | | | | | | | | | | | control auto area segmentation
* | | | | | | | | | | | | | | | | | | | | | | | | | | | | set film type
* | | | | | | | | | | | | | | | | | | | | | | | | | | | | | set exposure time
* | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | set bay
* | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | set threshold
* | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | set focus position
* | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | request focus position
* | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
* | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
*/
{"A1",'I', 0 ,'F','S', 0 ,'G', 0 ,'R', 0 ,'A', 0 ,{ 0, 0, 0}, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{"A2",'I', 0 ,'F','S', 0 ,'G','D','R','H','A','L',{-3, 3, 0},'Z','B', 0 ,'@', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{"B1",'I', 0 ,'F','S','C','G','D','R', 0 ,'A', 0 ,{ 0, 0, 0}, 0 ,'B', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{"B2",'I', 0 ,'F','S','C','G','D','R','H','A','L',{-3, 3, 0},'Z','B', 0 ,'@', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{"B3",'I', 0 ,'F','S','C','G','D','R','H','A','L',{-3, 3, 0},'Z','B','M','@', 0 , 0 , 0 , 0 , 0 , 0 ,'m','f','e', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{"B4",'I', 0 ,'F','S','C','G','D','R','H','A','L',{-3, 3, 0},'Z','B','M','@','g','d', 0 ,'z','Q','b','m','f','e', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{"B5",'I', 0 ,'F','S','C','G','D','R','H','A','L',{-3, 3, 0},'Z','B','M','@','g','d','K','z','Q','b','m','f','e', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{"B6",'I', 0 ,'F','S','C','G','D','R','H','A','L',{-3, 3, 0},'Z','B','M','@','g','d','K','z','Q','b','m','f','e', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
{"B7",'I', 0 ,'F','S','C','G','D','R','H','A','L',{-3, 3, 0},'Z','B','M','@','g','d','K','z','Q','b','m','f','e','\f', 0 ,'!','s','N', 0 , 0 ,'t', 0 , 0 },
{"B8",'I', 0 ,'F','S','C','G','D','R','H','A','L',{-3, 3, 0},'Z','B','M','@','g','d','K','z','Q','b','m','f','e','\f', 0x19,'!','s','N', 0 , 0 ,'t','p','q'},
{"F5",'I', 0 ,'F','S','C','G','D','R','H','A','L',{-3, 3, 0},'Z', 0 ,'M','@','g','d','K','z','Q', 0 ,'m','f','e','\f', 0 , 0 , 0 ,'N','T','P', 0 , 0 , 0 },
{"D1",'I','i','F', 0 ,'C','G','D','R', 0 ,'A', 0 ,{ 0, 0, 0},'Z', 0 , 0 ,'@','g','d', 0 ,'z', 0 , 0 , 0 ,'f', 0 , 0 , 0 ,'!', 0 , 0 , 0 , 0 ,'t', 0 , 0 },
{"D2",'I','i','F', 0 ,'C','G','D','R', 0 ,'A', 0 ,{ 0, 0, 0},'Z', 0 , 0 ,'@','g','d', 0 ,'z', 0 , 0 , 0 ,'f','e', 0 , 0 ,'!', 0 ,'N', 0 , 0 ,'t', 0 , 0 },
{"D7",'I','i','F', 0 ,'C','G','D','R', 0 ,'A', 0 ,{ 0, 0, 0},'Z', 0 , 0 ,'@','g','d', 0 ,'z', 0 , 0 , 0 ,'f','e','\f', 0 ,'!', 0 ,'N', 0 , 0 ,'t', 0 , 0 },
{"D8",'I','i','F', 0 ,'C','G','D','R', 0 ,'A', 0 ,{ 0, 0, 0},'Z', 0 , 0 ,'@','g','d', 0 ,'z', 0 , 0 , 0 ,'f','e','\f', 0 ,'!', 0 ,'N', 0 , 0 ,'t', 0 , 0 },
};
/* Definition of the mode_param struct, that is used to
* specify the valid parameters for the different scan modes.
*
* The depth variable gets updated when the bit depth is modified.
*/
struct mode_param
{
int color;
int mode_flags;
int dropout_mask;
int depth;
};
static struct mode_param mode_params[] = {
{0, 0x00, 0x30, 1},
{0, 0x00, 0x30, 8},
{1, 0x02, 0x00, 8}
};
static const SANE_String_Const mode_list[] = {
SANE_I18N ("Binary"),
SANE_I18N ("Gray"),
SANE_I18N ("Color"),
NULL
};
static const SANE_String_Const adf_mode_list[] = {
SANE_I18N ("Simplex"),
SANE_I18N ("Duplex"),
NULL
};
static const SANE_String_Const dfd_sensitivity[] = {
SANE_I18N ("None"),
SANE_I18N ("Low"),
SANE_I18N ("High"),
NULL
};
/* Define the different scan sources:
*/
#define FBF_STR SANE_I18N("Flatbed")
#define TPU_STR SANE_I18N("Transparency Unit")
#define ADF_STR SANE_I18N("Automatic Document Feeder")
#define FILM_TYPE_POSITIVE (0)
#define FILM_TYPE_NEGATIVE (1)
static const SANE_String_Const film_list[] = {
SANE_I18N ("Positive Film"),
SANE_I18N ("Negative Film"),
NULL
};
static const SANE_String_Const focus_list[] = {
SANE_I18N ("Focus on glass"),
SANE_I18N ("Focus 2.5mm above glass"),
NULL
};
#define HALFTONE_NONE 0x01
#define HALFTONE_TET 0x03
static int halftone_params[] = {
HALFTONE_NONE,
0x00,
0x10,
0x20,
0x80,
0x90,
0xa0,
0xb0,
HALFTONE_TET,
0xc0,
0xd0
};
static const SANE_String_Const halftone_list[] = {
SANE_I18N ("None"),
SANE_I18N ("Halftone A (Hard Tone)"),
SANE_I18N ("Halftone B (Soft Tone)"),
SANE_I18N ("Halftone C (Net Screen)"),
NULL
};
static const SANE_String_Const halftone_list_4[] = {
SANE_I18N ("None"),
SANE_I18N ("Halftone A (Hard Tone)"),
SANE_I18N ("Halftone B (Soft Tone)"),
SANE_I18N ("Halftone C (Net Screen)"),
SANE_I18N ("Dither A (4x4 Bayer)"),
SANE_I18N ("Dither B (4x4 Spiral)"),
SANE_I18N ("Dither C (4x4 Net Screen)"),
SANE_I18N ("Dither D (8x4 Net Screen)"),
NULL
};
static const SANE_String_Const halftone_list_7[] = {
SANE_I18N ("None"),
SANE_I18N ("Halftone A (Hard Tone)"),
SANE_I18N ("Halftone B (Soft Tone)"),
SANE_I18N ("Halftone C (Net Screen)"),
SANE_I18N ("Dither A (4x4 Bayer)"),
SANE_I18N ("Dither B (4x4 Spiral)"),
SANE_I18N ("Dither C (4x4 Net Screen)"),
SANE_I18N ("Dither D (8x4 Net Screen)"),
SANE_I18N ("Text Enhanced Technology"),
SANE_I18N ("Download pattern A"),
SANE_I18N ("Download pattern B"),
NULL
};
static int dropout_params[] = {
0x00, /* none */
0x10, /* red */
0x20, /* green */
0x30 /* blue */
};
static const SANE_String_Const dropout_list[] = {
SANE_I18N ("None"),
SANE_I18N ("Red"),
SANE_I18N ("Green"),
SANE_I18N ("Blue"),
NULL
};
static const SANE_String_Const brightness_method_list[] = {
SANE_I18N ("hardware"), /* needs to be first */
SANE_I18N ("iscan"),
SANE_I18N ("gimp"),
NULL
};
#define max_val 100 /* any integer value will do */
static const SANE_Range brightness_range = { -max_val, max_val, 1};
static const SANE_Range contrast_range = { -max_val, max_val, 1};
#if 0
/* We don't provide highlight and shadow options yet because we
* haven't worked out the GIMP part of that and how that should
* interact with the brightness-method option. For the "iscan"
* method, the values below are OK.
*/
static const SANE_Range highlight_range = { 0, max_val, 0 };
static const SANE_Range shadow_range = { 0, max_val, 0 };
#endif
#undef max_val
/* Color correction:
* One array for the actual parameters that get sent to the scanner (color_params[]),
* one array for the strings that get displayed in the user interface (color_list[])
* and one array to mark the user defined color correction (dolor_userdefined[]).
*/
static int color_params_ab[] = {
0x00,
0x01,
0x10,
0x20,
0x40,
0x80
};
static SANE_Bool color_userdefined_ab[] = {
SANE_FALSE,
SANE_TRUE,
SANE_FALSE,
SANE_FALSE,
SANE_FALSE,
SANE_FALSE
};
static const SANE_String_Const color_list_ab[] = {
SANE_I18N ("No Correction"),
SANE_I18N ("User defined"),
SANE_I18N ("Impact-dot printers"),
SANE_I18N ("Thermal printers"),
SANE_I18N ("Ink-jet printers"),
SANE_I18N ("CRT monitors"),
NULL
};
static int color_params_d[] = {
0x00
};
static SANE_Bool color_userdefined_d[] = {
SANE_TRUE
};
static const SANE_String_Const color_list_d[] = {
"User defined",
NULL
};
/* Gamma correction:
* The A and B level scanners work differently than the D level scanners, therefore
* I define two different sets of arrays, plus one set of variables that get set to
* the actally used params and list arrays at runtime.
*/
static int gamma_params_ab[] = {
0x01,
0x03,
0x04,
0x00,
0x10,
0x20
};
static const SANE_String_Const gamma_list_ab[] = {
SANE_I18N ("Default"),
SANE_I18N ("User defined (Gamma=1.0)"),
SANE_I18N ("User defined (Gamma=1.8)"),
SANE_I18N ("High density printing"),
SANE_I18N ("Low density printing"),
SANE_I18N ("High contrast printing"),
NULL
};
static SANE_Bool gamma_userdefined_ab[] = {
SANE_FALSE,
SANE_TRUE,
SANE_TRUE,
SANE_FALSE,
SANE_FALSE,
SANE_FALSE,
};
static int gamma_params_d[] = {
0x03,
0x04
};
static const SANE_String_Const gamma_list_d[] = {
SANE_I18N ("User defined (Gamma=1.0)"),
SANE_I18N ("User defined (Gamma=1.8)"),
NULL
};
static SANE_Bool gamma_userdefined_d[] = {
SANE_TRUE,
SANE_TRUE
};
/* Bay list:
* this is used for the FilmScan
*/
static const SANE_String_Const bay_list[] = {
" 1 ",
" 2 ",
" 3 ",
" 4 ",
" 5 ",
" 6 ",
NULL
};
static const SANE_Range u8_range = { 0, 255, 0 };
static const SANE_Range s8_range = { -127, 127, 0 };
static const SANE_Range zoom_range = { 50, 200, 0 };
/* The "switch_params" are used for several boolean choices
*/
static int switch_params[] = {
0,
1
};
#define mirror_params switch_params
#define speed_params switch_params
#define film_params switch_params
static const SANE_Range outline_emphasis_range = { -2, 2, 0 };
enum
{
EXT_SANE_STATUS_NONE,
EXT_SANE_STATUS_MULTI_FEED,
EXT_SANE_STATUS_TRAY_CLOSED,
EXT_SANE_STATUS_MAX,
};
static const SANE_Range ext_sane_status = {
EXT_SANE_STATUS_NONE,
EXT_SANE_STATUS_MAX - 1,
0
};
typedef struct {
double width; /* in mm */
double height; /* in mm */
SANE_String_Const name;
} media_type;
static SANE_String_Const media_maximum = SANE_I18N ("Maximum");
static SANE_String_Const media_automatic = SANE_I18N ("Automatic");
/*! \brief List of preset media sizes
* This list is used to populate the constraint list for the
* scan-area option.
*
* \remark When adding Landscape and Portrait variants of a medium
* size, make sure to add three(!) entries using the same
* pattern as used for those already listed. The algorithm
* used to populate the constraint list depends on this
* convention to create the most user-friendly list.
*/
static const media_type media_list[] = {
/* ISO A Series */
{ 297, 420, SANE_I18N ("A3") },
{ 297, 210, SANE_I18N ("A4 Landscape") },
{ 210, 297, SANE_I18N ("A4 Portrait") },
{ 210, 297, SANE_I18N ("A4") },
{ 210, 148, SANE_I18N ("A5 Landscape") },
{ 148, 210, SANE_I18N ("A5 Portrait") },
{ 148, 210, SANE_I18N ("A5") },
/* JIS B Series */
{ 257, 364, SANE_I18N ("B4") },
{ 257, 184, SANE_I18N ("B5 Landscape") },
{ 184, 257, SANE_I18N ("B5 Portrait") },
{ 184, 257, SANE_I18N ("B5") },
/* North American sizes */
{ 11.00 * MM_PER_INCH, 17.00 * MM_PER_INCH, SANE_I18N ("Ledger") },
{ 8.50 * MM_PER_INCH, 14.00 * MM_PER_INCH, SANE_I18N ("Legal") },
{ 11.00 * MM_PER_INCH, 8.50 * MM_PER_INCH, SANE_I18N ("Letter Landscape") },
{ 8.50 * MM_PER_INCH, 11.00 * MM_PER_INCH, SANE_I18N ("Letter Portrait") },
{ 8.50 * MM_PER_INCH, 11.00 * MM_PER_INCH, SANE_I18N ("Letter") },
{ 10.50 * MM_PER_INCH, 7.25 * MM_PER_INCH, SANE_I18N ("Executive Landscape") },
{ 7.25 * MM_PER_INCH, 10.50 * MM_PER_INCH, SANE_I18N ("Executive Portrait") },
{ 7.25 * MM_PER_INCH, 10.50 * MM_PER_INCH, SANE_I18N ("Executive") },
/* Miscellaneous */
{ 120, 120, SANE_I18N ("CD")},
};
static SANE_Word *bitDepthList = NULL;
/* Some utility macros
*/
/*! Returns the larger of the arguments \a a and \a b.
*/
#define max(a, b) ((a) < (b) ? (b) : (a))
/*! Returns the smaller of the arguments \a a and \a b.
*/
#define min(a, b) ((a) < (b) ? (a) : (b))
static size_t
max_string_size (const SANE_String_Const strings[])
{
size_t size, max_size = 0;
int i;
for (i = 0; strings[i]; i++)
{
size = strlen (strings[i]) + 1;
if (size > max_size)
max_size = size;
}
return max_size;
}
static inline
bool
need_autocrop_override (const Epson_Scanner *s)
{
return (SANE_OPTION_IS_ACTIVE (s->opt[OPT_AUTOCROP].cap)
&& s->val[OPT_AUTOCROP].b
&& 0 != autocrop_max_y (s->hw));
}
typedef struct
{
u_char code;
u_char status;
u_char count1;
u_char count2;
u_char type;
u_char level;
u_char buf[1];
} EpsonIdentRec, *EpsonIdent;
typedef struct
{
u_char code;
u_char status;
u_char count1;
u_char count2;
u_char buf[1];
} EpsonHdrRec, *EpsonHdr;
static SANE_Status
read_image_info_block (device *hw);
static SANE_Status
get_identity_information (device *hw);
static SANE_Status
get_hardware_property (device *hw);
static SANE_Status
get_identity2_information (device *hw, Epson_Scanner *s);
static SANE_Status
check_warmup (device *hw);
static SANE_Status
check_ext_status (device *hw);
SANE_Status
control_option_unit (device *hw, SANE_Bool use_duplex);
static SANE_Status
initialize (device *hw);
static SANE_Status
get_resolution_constraints (device *hw, Epson_Scanner *s);
static SANE_Status
get_push_button_status (device *hw, SANE_Bool *button_pushed);
static SANE_Status color_shuffle (Epson_Scanner *s, int *new_length);
static void activateOption (Epson_Scanner * s, SANE_Int option,
SANE_Bool * change);
static void deactivateOption (Epson_Scanner * s, SANE_Int option,
SANE_Bool * change);
static void setOptionState (Epson_Scanner * s, SANE_Bool state,
SANE_Int option, SANE_Bool * change);
static void filter_resolution_list (Epson_Scanner * s);
static void scan_finish (Epson_Scanner * s);
static void get_colorcoeff_from_profile (double *profile,
unsigned char *color_coeff);
static void round_cct (double org_cct[], int rnd_cct[]);
static int get_roundup_index (double frac[], int n);
static int get_rounddown_index (double frac[], int n);
static void handle_mode (Epson_Scanner * s, SANE_Int optindex,
SANE_Bool * reload);
static void handle_resolution (Epson_Scanner * s, SANE_Int option,
SANE_Word value);
static void change_profile_matrix (Epson_Scanner * s);
static void handle_depth_halftone (Epson_Scanner * s, SANE_Int optindex,
SANE_Bool * reload);
static SANE_Status create_epson_device (device **dev, channel* ch);
static SANE_Status create_epson_scanner (device *dev, Epson_Scanner **scanner);
static SANE_Status create_sane_handle (Epson_Scanner **scanner, const char *name, const void *dip);
static SANE_Status init_options (Epson_Scanner * s);
static scan_area_t get_model_info_max_scan_area (device *hw, int adf_mode);
static SANE_Status handle_scan_area(Epson_Scanner *s, int adf_mode);
static SANE_Status handle_source (Epson_Scanner * s,
SANE_Int optindex,
char *value);
static void adf_handle_out_of_paper (Epson_Scanner * s);
static void adf_handle_adjust_alignment (Epson_Scanner *s, SANE_Bool finalize);
static void handle_autocrop (Epson_Scanner *s, SANE_Bool *value, SANE_Bool *reload);
static void handle_deskew (Epson_Scanner *s, SANE_Bool *value, SANE_Bool *reload);
static void handle_detect_doc_size (Epson_Scanner *s, SANE_Bool *value, SANE_Bool *reload);
static void handle_preview (Epson_Scanner *s, SANE_Bool *value, SANE_Bool *reload);
static SANE_Status
expect_ack (device *hw)
{
u_char result[1];
size_t len;
SANE_Status status;
log_call ();
len = sizeof (result);
channel_recv (hw->channel, result, len, &status);
if (SANE_STATUS_GOOD != status)
return status;
if (ACK != result[0])
return SANE_STATUS_INVAL;
return SANE_STATUS_GOOD;
}
static SANE_Status
set_cmd (device *hw, u_char cmd, u_char val)
{
SANE_Status status;
u_char params[2];
if (!cmd)
return SANE_STATUS_UNSUPPORTED;
log_call ("(%c)", cmd);
/* Override with extended parameter command (FS W).
* Do not override A, R and e, they are handled separately.
*/
if (hw->using_fs && strchr ("CDgdZLMBtsQKN", cmd))
return dev_set_scanning_parameter (hw, cmd, &val);
params[0] = ESC;
params[1] = cmd;
channel_send (hw->channel, params, 2, &status);
if (SANE_STATUS_GOOD != (status = expect_ack (hw)))
return status;
params[0] = val;
channel_send (hw->channel, params, 1, &status);
status = expect_ack (hw);
return status;
}
static void
print_params (const SANE_Parameters params)
{
log_data ("params.format = %d", params.format);
log_data ("params.last_frame = %d", params.last_frame);
log_data ("params.bytes_per_line = %d", params.bytes_per_line);
log_data ("params.pixels_per_line = %d", params.pixels_per_line);
log_data ("params.lines = %d", params.lines);
log_data ("params.depth = %d", params.depth);
}
#define set_focus_position(h,v) set_cmd (h,(h)->cmd->set_focus_position,v)
#define set_color_mode(h,v) set_cmd (h,(h)->cmd->set_color_mode,v)
#define set_data_format(h,v) set_cmd (h,(h)->cmd->set_data_format, v)
#define set_halftoning(h,v) set_cmd (h,(h)->cmd->set_halftoning, v)
#define set_gamma(h,v) set_cmd (h,(h)->cmd->set_gamma, v)
#define set_color_correction(h,v) set_cmd (h,(h)->cmd->set_color_correction, v)
#define set_lcount(h,v) set_cmd (h,(h)->cmd->set_lcount, v)
#define set_bright(h,v) set_cmd (h,(h)->cmd->set_bright, v)
#define mirror_image(h,v) set_cmd (h,(h)->cmd->mirror_image, v)
#define set_speed(h,v) set_cmd (h,(h)->cmd->set_speed, v)
#define set_outline_emphasis(h,v) set_cmd (h,(h)->cmd->set_outline_emphasis, v)
#define control_auto_area_segmentation(h,v) set_cmd (h,(h)->cmd->control_auto_area_segmentation, v)
#define set_film_type(h,v) set_cmd (h,(h)->cmd->set_film_type, v)
#define set_exposure_time(h,v) set_cmd (h,(h)->cmd->set_exposure_time, v)
#define set_bay(h,v) set_cmd (h,(h)->cmd->set_bay, v)
#define set_threshold(h,v) set_cmd (h,(h)->cmd->set_threshold, v)
static SANE_Status
set_zoom (device *hw, int x_zoom, int y_zoom)
{
SANE_Status status;
u_char cmd[2];
u_char params[2];
if (!hw->cmd->set_zoom)
return SANE_STATUS_GOOD;
log_call ();
cmd[0] = ESC;
cmd[1] = hw->cmd->set_zoom;
channel_send (hw->channel, cmd, 2, &status);
status = expect_ack (hw);
if (status != SANE_STATUS_GOOD)
return status;
params[0] = x_zoom;
params[1] = y_zoom;
channel_send (hw->channel, params, 2, &status);
status = expect_ack (hw);
return status;
}
static SANE_Status
set_resolution (device *hw, int xres, int yres)
{
SANE_Status status;
u_char params[4];
if (!hw->cmd->set_resolution)
return SANE_STATUS_GOOD;
log_call ();
params[0] = ESC;
params[1] = hw->cmd->set_resolution;
channel_send (hw->channel, params, 2, &status);
status = expect_ack (hw);
if (status != SANE_STATUS_GOOD)
return status;
params[0] = xres;
params[1] = xres >> 8;
params[2] = yres;
params[3] = yres >> 8;
channel_send (hw->channel, params, 4, &status);
status = expect_ack (hw);
return status;
}
/* set_scan_area()
*
* Sends the "set scan area" command to the scanner with the currently selected
* scan area. This scan area is already corrected for "color shuffling" if
* necessary.
*/
static SANE_Status
set_scan_area (device *hw, int x, int y, int width, int height)
{
SANE_Status status;
u_char params[8];
log_call ("(%d, %d, %d, %d)", x, y, width, height);
if (!hw->cmd->set_scan_area)
{
err_major ("set_scan_area not supported");
return SANE_STATUS_GOOD;
}
/* verify the scan area */
if (x < 0 || y < 0 || width <= 0 || height <= 0)
return SANE_STATUS_INVAL;
params[0] = ESC;
params[1] = hw->cmd->set_scan_area;
channel_send (hw->channel, params, 2, &status);
status = expect_ack (hw);
if (status != SANE_STATUS_GOOD)
return status;
params[0] = x;
params[1] = x >> 8;
params[2] = y;
params[3] = y >> 8;
params[4] = width;
params[5] = width >> 8;
params[6] = height;
params[7] = height >> 8;
channel_send (hw->channel, params, 8, &status);
status = expect_ack (hw);
return status;
}
/* set_color_correction_coefficients()
*
* Sends the "set color correction coefficients" command with the currently selected
* parameters to the scanner.
*/
static SANE_Status
set_color_correction_coefficients (device *hw, Epson_Scanner *s)
{
SANE_Status status;
u_char cmd = hw->cmd->set_color_correction_coefficients;
u_char params[2];
const int length = 9;
unsigned char cccoeff[9];
log_call ();
s->cct[0] = SANE_UNFIX (s->val[OPT_CCT_1].w);
s->cct[1] = SANE_UNFIX (s->val[OPT_CCT_2].w);
s->cct[2] = SANE_UNFIX (s->val[OPT_CCT_3].w);
s->cct[3] = SANE_UNFIX (s->val[OPT_CCT_4].w);
s->cct[4] = SANE_UNFIX (s->val[OPT_CCT_5].w);
s->cct[5] = SANE_UNFIX (s->val[OPT_CCT_6].w);
s->cct[6] = SANE_UNFIX (s->val[OPT_CCT_7].w);
s->cct[7] = SANE_UNFIX (s->val[OPT_CCT_8].w);
s->cct[8] = SANE_UNFIX (s->val[OPT_CCT_9].w);
if (!cmd) /* effect will be emulated */
return SANE_STATUS_GOOD;
params[0] = ESC;
params[1] = cmd;
channel_send (hw->channel, params, 2, &status);
if (SANE_STATUS_GOOD != (status = expect_ack (hw)))
return status;
get_colorcoeff_from_profile (s->cct, cccoeff);
log_data ("[ %d %d %d ][ %d %d %d ][ %d %d %d]",
cccoeff[0], cccoeff[1], cccoeff[2],
cccoeff[3], cccoeff[4], cccoeff[5],
cccoeff[6], cccoeff[7], cccoeff[8]);
channel_send (hw->channel, cccoeff, length, &status);
status = expect_ack (hw);
log_call ("exit");
return status;
}
static SANE_Status
set_gamma_table (device *hw, const Epson_Scanner *s)
{
SANE_Status status;
u_char cmd = hw->cmd->set_gamma_table;
u_char params[2];
const int length = 257;
u_char gamma[257];
int n;
int table;
static char gamma_cmds[] = { 'R', 'G', 'B' };
if (!cmd)
return SANE_STATUS_UNSUPPORTED;
log_call ();
params[0] = ESC;
params[1] = cmd;
/* When handling inverted images, we must also invert the user
* supplied gamma function. This is *not* just 255-gamma -
* this gives a negative image.
*/
if (strcmp_c ("GT-6600", hw->fw_name) == 0 ||
strcmp_c ("Perfection 610", hw->fw_name) == 0)
{
gamma_cmds[0] = 'R';
gamma_cmds[1] = 'B';
gamma_cmds[2] = 'G';
}
for (table = 0; table < 3; table++)
{
gamma[0] = gamma_cmds[table];
if (s->invert_image)
{
for (n = 0; n < 256; ++n)
{
gamma[n + 1] = 255 - s->gamma_table[table][255 - n];
}
}
else
{
for (n = 0; n < 256; ++n)
{
gamma[n + 1] = s->gamma_table[table][n];
}
}
channel_send (hw->channel, params, 2, &status);
if (SANE_STATUS_GOOD != (status = expect_ack (hw)))
return status;
channel_send (hw->channel, gamma, length, &status);
if (SANE_STATUS_GOOD != (status = expect_ack (hw)))
return status;
}
log_call ("exit");
return status;
}
static SANE_Status
check_warmup (device *hw)
{
SANE_Status status = check_ext_status (hw);
log_call ();
if (status == SANE_STATUS_DEVICE_BUSY)
{
int timeout;
for (timeout = 0; timeout < 60; timeout++)
{
status = check_ext_status (hw);
if (status == SANE_STATUS_DEVICE_BUSY)
sleep (1);
else
{
return status;
}
}
}
else
return status;