7
use Getopt::Long qw(:config no_ignore_case pass_through);
13
my (%options, @window_val_user, @window_option, @window_val,
14
@window, $num_dev_options, $device, $format, $devname, %option_number);
19
my $batch_start_at = 1;
21
my $batch_increment = 1;
22
my $buffer_size = (32 * 1024); # default size
29
my $resolution_optind = -1;
30
my $resolution_value = 0;
35
my $prog_name = basename($0);
36
my @args = (\%options, 'd|device-name=s' => \$devname,
38
'f|formatted-device-list=s',
39
'b|batch:s' => \$format,
40
'batch-start=i' => \$batch_start_at,
41
'batch-count=i' => \$batch_count,
42
'batch-increment=i' => \$batch_increment,
43
'batch-double' => \$batch_double,
44
'batch-prompt' => \$batch_prompt,
45
'p|progress' => \$progress,
46
'n|dont-scan' => \$dont_scan,
49
'v|verbose+' => \$verbose,
50
'B|buffer-size' => \$buffer_size,
57
my $first_time = SANE_TRUE;
60
print STDERR "$prog_name: received signal $signum\n";
62
$first_time = SANE_FALSE;
63
print STDERR "$prog_name: trying to stop scanner\n";
67
print STDERR "$prog_name: aborting\n";
77
if ($unit == SANE_UNIT_PIXEL) {
80
elsif ($unit == SANE_UNIT_BIT) {
83
elsif ($unit == SANE_UNIT_MM) {
86
elsif ($unit == SANE_UNIT_DPI) {
89
elsif ($unit == SANE_UNIT_PERCENT) {
92
elsif ($unit == SANE_UNIT_MICROSECOND) {
99
my ($device, $opt_num, $short_name) = @_;
101
my $not_first = SANE_FALSE;
104
my $opt = $device->get_option_descriptor ($opt_num);
107
printf " -%s", $short_name;
110
printf " --%s", $opt->{name};
113
if ($opt->{type} == SANE_TYPE_BOOL) {
115
print "auto|" if ($opt->{cap} & SANE_CAP_AUTOMATIC);
118
elsif ($opt->{type} != SANE_TYPE_BUTTON) {
120
if ($opt->{cap} & SANE_CAP_AUTOMATIC) {
122
$not_first = SANE_TRUE;
124
if ($opt->{constraint_type} == SANE_CONSTRAINT_NONE) {
125
if ($opt->{type} == SANE_TYPE_INT) {
128
elsif ($opt->{type} == SANE_TYPE_FIXED) {
131
elsif ($opt->{type} == SANE_TYPE_STRING) {
134
print ",..." if ($opt->{max_values} > 1);
136
elsif ($opt->{constraint_type} == SANE_CONSTRAINT_RANGE) {
137
my $format = "%g..%g";
138
$format = "%d..%d" if ($opt->{type} == SANE_TYPE_INT);
139
if ($opt->{name} eq SANE_NAME_SCAN_BR_X) {
140
$maxwindow = $opt->{constraint}{max} - $tl_x;
141
printf $format, $opt->{constraint}{min}, $maxwindow;
143
elsif ($opt->{name} eq SANE_NAME_SCAN_BR_Y) {
144
$maxwindow = $opt->{constraint}{max} - $tl_y;
145
printf $format, $opt->{constraint}{min}, $maxwindow;
148
printf $format, $opt->{constraint}{min}, $opt->{constraint}{max};
150
print_unit ($opt->{unit});
151
print ",..." if ($opt->{max_values} > 1);
152
print " (in steps of $opt->{constraint}{quant})"
153
if ($opt->{constraint}{quant});
155
elsif ($opt->{constraint_type} == SANE_CONSTRAINT_STRING_LIST
156
or $opt->{constraint_type} == SANE_CONSTRAINT_WORD_LIST) {
157
for (my $i = 0; $i < @{$opt->{constraint}}; ++$i) {
158
print '|' if ($i > 0);
160
print $opt->{constraint}[$i];
162
if ($opt->{constraint_type} == SANE_CONSTRAINT_WORD_LIST) {
163
print_unit ($opt->{unit});
164
print ",..." if ($opt->{max_values} > 1);
168
if ($opt->{max_values} == 1) {
169
# print current option value
170
if (! ($opt->{cap} & SANE_CAP_INACTIVE)) {
171
my $val = $device->get_option ($opt_num);
173
if ($opt->{type} == SANE_TYPE_BOOL) {
174
print ($val ? "yes" : "no");
176
elsif ($opt->{type} == SANE_TYPE_INT or $opt->{type} == SANE_TYPE_FIXED) {
178
$format = "%d" if ($opt->{type} == SANE_TYPE_INT);
179
if ($opt->{name} eq SANE_NAME_SCAN_TL_X) {
181
printf $format, $tl_x;
183
elsif ($opt->{name} eq SANE_NAME_SCAN_TL_Y) {
185
printf $format, $tl_y;
187
elsif ($opt->{name} eq SANE_NAME_SCAN_BR_X) {
189
$w_x = $br_x - $tl_x;
190
printf $format, $w_x;
192
elsif ($opt->{name} eq SANE_NAME_SCAN_BR_Y) {
194
$h_y = $br_y - $tl_y;
195
printf $format, $h_y;
198
printf $format, $val;
201
elsif ($opt->{type} == SANE_TYPE_STRING) {
208
print " [inactive]" if ($opt->{cap} & SANE_CAP_INACTIVE);
212
if ($short_name eq 'x') {
213
print "Width of scan-area.";
215
elsif ($short_name eq 'y') {
216
print "Height of scan-area.";
222
for (my $pos = 0; $pos < length($opt->{desc}); ++$pos) {
224
$last_break = $pos if (substr($opt->{desc}, $pos, 1) eq ' ');
225
if ($column >= 79 and $last_break) {
226
print substr($opt->{desc}, $start++, 1) while ($start < $last_break);
227
$start = $last_break + 1; # skip blank
229
$column = 8 + $pos - $start;
232
print substr($opt->{desc}, $start++, 1) while ($start < length($opt->{desc}));
238
# A scalar has the following syntax:
242
# V is the value of the scalar. It is either an integer or a
243
# floating point number, depending on the option type.
245
# U is an optional unit. If not specified, the default unit is used.
246
# The following table lists which units are supported depending on
247
# what the option's default unit is:
249
# Option's unit: Allowed units:
252
# SANE_UNIT_PIXEL: pel
253
# SANE_UNIT_BIT: b (bit), B (byte)
254
# SANE_UNIT_MM: mm (millimeter), cm (centimeter), in or " (inches),
256
# SANE_UNIT_PERCENT: %
257
# SANE_UNIT_MICROSECOND: us
260
my ($opt, $str) = @_;
263
if ($str =~ /^(\d*\.?\d*)(cm|mm|in|\"|b|B|dpi|%|us)?/) {
266
$unit = '' if not defined $unit;
270
"$prog_name: option --$opt->{name}: bad option value (rest of option: $str)\n";
274
if ($opt->{unit} == SANE_UNIT_BIT) {
275
$v *= 8 if ($unit eq 'B');
277
elsif ($opt->{unit} == SANE_UNIT_MM) {
281
elsif ($unit eq 'in') {
285
return $v, substr($str, length($v) + length($unit), length($str));
289
# A vector has the following syntax:
291
# [ '[' I ']' ] S { [','|'-'] [ '[' I ']' S }
293
# The number in brackets (I), if present, determines the index of the
294
# vector element to be set next. If I is not present, the value of
295
# last index used plus 1 is used. The first index value used is 0
296
# unless I is present.
298
# S is a scalar value as defined by parse_scalar().
300
# If two consecutive value specs are separated by a comma (,) their
301
# values are set independently. If they are separated by a dash (-),
302
# they define the endpoints of a line and all vector values between
303
# the two endpoints are set according to the value of the
304
# interpolated line. For example, [0]15-[255]15 defines a vector of
305
# 256 elements whose value is 15. Similarly, [0]0-[255]255 defines a
306
# vector of 256 elements whose value starts at 0 and increases to
310
my ($opt, $str) = @_;
316
my (@vector, $value);
319
if ($str =~ /^\[(\d*\.?\d*)\]/) {
324
"$prog_name: option --$opt->{name}: closing bracket missing "
325
."(rest of option: $str)\n";
333
if ($index < 0 or $index >= length($str)) {
335
"$prog_name: option --$opt->{name}: index $index out of range [0..%d]\n",
341
($value, $str) = parse_scalar ($opt, $str);
343
if ($str ne '' and $str !~ /^[-,]/) {
345
"$prog_name: option --$opt->{name}: illegal separator (rest of option: $str)\n";
350
$vector[$index] = $value;
351
if ($separator eq '-') {
354
my $slope = ($value - $v) / ($index - $prev_index);
356
for (my $i = $prev_index + 1; $i < $index; ++$i) {
362
$prev_index = $index;
363
$prev_value = $value;
364
$separator = substr($str, 0, 1);
366
while ($separator eq ',' || $separator eq '-');
369
print STDERR "$prog_name: value for --$opt->{name} is: ";
383
# We got a device, find out how many options it has:
384
$num_dev_options = $device->get_option(0);
385
if ($Sane::STATUS != SANE_STATUS_GOOD) {
386
print STDERR "$prog_name: unable to determine option count\n";
390
for (my $i = 0; $i < $num_dev_options; ++$i) {
391
my $opt = $device->get_option_descriptor ($i);
393
next if (! ($opt->{cap} & SANE_CAP_SOFT_SELECT));
395
$option_number{$opt->{name}} = $i;
397
# Look for scan resolution
398
$resolution_optind = $i
399
if (($opt->{type} == SANE_TYPE_FIXED || $opt->{type} == SANE_TYPE_INT)
400
and ($opt->{unit} == SANE_UNIT_DPI)
401
and ($opt->{name} eq SANE_NAME_SCAN_RESOLUTION));
403
# Keep track of top-left corner options (if they exist at
404
# all) and replace the bottom-right corner options by a
405
# width/height option (if they exist at all).
406
if (($opt->{type} == SANE_TYPE_FIXED || $opt->{type} == SANE_TYPE_INT)
407
and ($opt->{unit} == SANE_UNIT_MM || $opt->{unit} == SANE_UNIT_PIXEL)) {
408
if ($opt->{name} eq SANE_NAME_SCAN_TL_X) {
412
elsif ($opt->{name} eq SANE_NAME_SCAN_TL_Y) {
416
elsif ($opt->{name} eq SANE_NAME_SCAN_BR_X) {
419
$window_option[0] = $opt;
420
$window_option[0]->{title} = 'Scan width';
421
$window_option[0]->{desc} = 'Width of scanning area.';
422
$window_val[0] = $device->get_option ($i)
423
if (!$window_val_user[0]);
425
elsif ($opt->{name} eq SANE_NAME_SCAN_BR_Y) {
428
$window_option[1] = $opt;
429
$window_option[1]->{title} = 'Scan height';
430
$window_option[1]->{desc} = 'Height of scanning area.';
431
$window_val[1] = $device->get_option ($i)
432
if (!$window_val_user[1]);
436
if ($opt->{type} == SANE_TYPE_BOOL) {
437
push @args, "$opt->{name}:s";
439
elsif ($opt->{type} == SANE_TYPE_BUTTON) {
440
push @args, $opt->{name};
443
push @args, "$opt->{name}=s";
447
# Initialize width & height options based on backend default
448
# values for top-left x/y and bottom-right x/y:
449
for (my $i = 0; $i < 2; ++$i) {
450
if ($window[$i] and $window[$i + 2] and !$window_val_user[$i]) {
451
my $pos = $device->get_option ($window[$i + 2]);
452
$window_val[$i] = $window_val[$i] - $pos if (defined $pos);
459
my ($device, $optnum, $value) = @_;
461
my $opt = $device->get_option_descriptor ($optnum);
462
if ($opt and ($opt->{cap} & SANE_CAP_INACTIVE)) {
464
"$prog_name: ignored request to set inactive option $opt->{name}\n"
469
my $info = $device->set_option($optnum, $value);
470
if ($Sane::STATUS != SANE_STATUS_GOOD) {
472
"$prog_name: setting of option --$opt->{name} failed ($Sane::STATUS)\n";
476
if (($info & SANE_INFO_INEXACT) and $opt->{max_values} == 1) {
478
$value = $device->get_option($optnum);
479
if ($opt->{type} == SANE_TYPE_INT) {
481
"$prog_name: rounded value of $opt->{name} from %d to %d\n", $orig, $value;
483
elsif ($opt->{type} == SANE_TYPE_FIXED) {
485
"$prog_name: rounded value of $opt->{name} from %g to %g\n", $orig, $value;
488
fetch_options ($device) if ($info & SANE_INFO_RELOAD_OPTIONS);
492
sub process_backend_option {
493
my ($device, $optnum, $optarg) = @_;
495
my $opt = $device->get_option_descriptor ($optnum);
497
if ($opt and ($opt->{cap} & SANE_CAP_INACTIVE)) {
498
print STDERR "$prog_name: attempted to set inactive option $opt->{name}\n";
502
if (($opt->{cap} & SANE_CAP_AUTOMATIC) and $optarg and $optarg =~ /^auto$/i) {
503
$device->set_auto($optnum);
504
if ($Sane::STATUS != SANE_STATUS_GOOD) {
505
printf STDERR "$prog_name: failed to set option --$opt->{name} to automatic ($Sane::STATUS)\n";
512
if ($opt->{type} == SANE_TYPE_BOOL) {
513
$value = 1; # no argument means option is set
515
if ($optarg =~ /^yes$/i) {
518
elsif ($optarg =~ /^no$/i) {
522
printf STDERR "$prog_name: option --$opt->{name}: bad option value `$optarg'\n";
527
elsif ($opt->{type} == SANE_TYPE_INT or $opt->{type} == SANE_TYPE_FIXED) {
528
my @vector = parse_vector ($opt, $optarg);
531
elsif ($opt->{type} == SANE_TYPE_STRING) {
534
elsif ($opt->{type} == SANE_TYPE_BUTTON) {
535
$value = 0; # value doesn't matter
538
printf STDERR "$prog_name: duh, got unknown option type $opt->{type}\n";
541
set_option ($device, $optnum, $value);
545
sub write_pnm_header {
546
my ($format, $width, $height, $depth) = @_;
548
# The netpbm-package does not define raw image data with maxval > 255.
549
# But writing maxval 65535 for 16bit data gives at least a chance
552
if ($format == SANE_FRAME_RED or $format == SANE_FRAME_GREEN or
553
$format == SANE_FRAME_BLUE or $format == SANE_FRAME_RGB) {
554
printf "P6\n# SANE data follows\n%d %d\n%d\n", $width, $height,
555
($depth <= 8) ? 255 : 65535;
559
printf "P4\n# SANE data follows\n%d %d\n", $width, $height;
562
printf "P5\n# SANE data follows\n%d %d\n%d\n", $width, $height,
563
($depth <= 8) ? 255 : 65535;
577
"gray", "RGB", "red", "green", "blue"
583
{do { # extra braces to get last to work.
586
if ($Sane::STATUS != SANE_STATUS_GOOD) {
587
printf STDERR "$prog_name: sane_start: $Sane::STATUS\n";
592
$parm = $device->get_parameters;
593
if ($Sane::STATUS != SANE_STATUS_GOOD) {
594
printf STDERR "$prog_name: sane_get_parameters: $Sane::STATUS\n";
600
if ($parm->{lines} >= 0) {
601
printf STDERR "$prog_name: scanning image of size %dx%d pixels at "
603
$parm->{pixels_per_line}, $parm->{lines},
604
8 * $parm->{bytes_per_line} / $parm->{pixels_per_line};
607
printf STDERR "$prog_name: scanning image %d pixels wide and "
608
."variable height at %d bits/pixel\n",
609
$parm->{pixels_per_line},
610
8 * $parm->{bytes_per_line} / $parm->{pixels_per_line};
614
printf STDERR "$prog_name: acquiring %s frame\n",
615
$parm->{format} <= SANE_FRAME_BLUE ? $format_name[$parm->{format}]:"Unknown";
619
if ($parm->{format} == SANE_FRAME_RED
620
or $parm->{format} == SANE_FRAME_GREEN
621
or $parm->{format} == SANE_FRAME_BLUE) {
622
die unless ($parm->{depth} == 8);
624
$offset = $parm->{format} - SANE_FRAME_RED;
626
elsif ($parm->{format} == SANE_FRAME_RGB) {
627
die unless (($parm->{depth} == 8) || ($parm->{depth} == 16));
629
if ($parm->{format} == SANE_FRAME_RGB or $parm->{format} == SANE_FRAME_GRAY) {
630
die unless (($parm->{depth} == 1) || ($parm->{depth} == 8)
631
|| ($parm->{depth} == 16));
632
if ($parm->{lines} < 0) {
637
# if ($output_format == OUTPUT_TIFF) {
638
# sanei_write_tiff_header ($parm->{format},
639
# $parm->{pixels_per_line}, $parm->{lines},
640
# $parm->{depth}, $resolution_value,
643
write_pnm_header ($parm->{format}, $parm->{pixels_per_line},
644
$parm->{lines}, $parm->{depth});
650
die unless ($parm->{format} >= SANE_FRAME_RED
651
&& $parm->{format} <= SANE_FRAME_BLUE);
652
$offset = $parm->{format} - SANE_FRAME_RED;
653
$image{x} = $image{y} = 0;
655
my $hundred_percent = $parm->{bytes_per_line} * $parm->{lines}
656
* (($parm->{format} == SANE_FRAME_RGB || $parm->{format} == SANE_FRAME_GRAY) ? 1:3);
659
my ($buffer, $len) = $device->read ($buffer_size);
660
$total_bytes += $len;
661
my $progr = (($total_bytes * 100.) / $hundred_percent);
662
$progr = 100. if ($progr > 100.);
663
printf STDERR "Progress: %3.1f%%\r", $progr if ($progress);
665
if ($Sane::STATUS != SANE_STATUS_GOOD) {
666
printf STDERR "$prog_name: min/max graylevel value = %d/%d\n", $min, $max
667
if ($verbose && $parm->{depth} == 8);
668
if ($Sane::STATUS != SANE_STATUS_EOF) {
669
print STDERR "$prog_name: sane_read: $Sane::STATUS\n";
676
# We're either scanning a multi-frame image or the
677
# scanner doesn't know what the eventual image height
678
# will be (common for hand-held scanners). In either
679
# case, we need to buffer all data before we can write
681
if ($parm->{format} == SANE_FRAME_RED
682
or $parm->{format} == SANE_FRAME_GREEN
683
or $parm->{format} == SANE_FRAME_BLUE) {
684
for (my $i = 0; $i < $len; ++$i) {
685
$image{data}[$offset + 3 * $i] = substr($buffer, $i, 1);
689
elsif ($parm->{format} == SANE_FRAME_RGB
690
or $parm->{format} == SANE_FRAME_GRAY) {
691
for (my $i = 0; $i < $len; ++$i) {
692
$image{data}[$offset + $i] = substr($buffer, $i, 1);
697
else { # ! must_buffer
698
# if (($output_format == OUTPUT_TIFF) || ($parm->{depth} != 16)) {
702
##if !defined(WORDS_BIGENDIAN)
705
# # check if we have saved one byte from the last sane_read
706
# if ($hang_over > -1) {
709
# $buffer = $hang_over.$buffer;
714
# # now do the byte-swapping
715
# $buffer = reverse $buffer;
717
# # check if we have an odd number of bytes
718
# if ((($len - $start) % 2) != 0) {
719
# $hang_over = substr($buffer, $len - 1, 1);
727
if ($verbose && $parm->{depth} == 8) {
728
for (split(//, $buffer)) {
742
while (!$parm->{last_frame});}
745
if ($parm->{lines} > 0) {
746
$image{height} = $parm->{lines};
749
$image{height} = @{$image{data}}/$parm->{pixels_per_line};
750
$image{height} /= 3 if ($parm->{format} == SANE_FRAME_RED
751
or $parm->{format} == SANE_FRAME_GREEN
752
or $parm->{format} == SANE_FRAME_BLUE);
754
# if ($output_format == OUTPUT_TIFF) {
755
# sanei_write_tiff_header ($parm->{format}, $parm->{pixels_per_line},
756
# $parm->{lines}, $parm->{depth}, $resolution_value,
760
write_pnm_header ($parm->{format}, $parm->{pixels_per_line}, $image{height}, $parm->{depth});
762
# if (($output_format == OUTPUT_TIFF) || ($image{Bpp} == 1)
763
# || ($image{Bpp} == 3)) {
764
# print $image{data};
766
# else { # $image{Bpp} == 2 or $image{Bpp} == 6 assumed
767
##if !defined(WORDS_BIGENDIAN)
768
# for (my $i = 0; $i < $image{Bpp} * $image{height} * $image{width}; $i += 2) {
769
# my $LSB = $image{data}[$i];
770
# $image{data}[$i] = $image{data}[$i + 1];
771
# $image{data}[$i + 1] = $LSB;
774
for (@{$image{data}}) {print;}
778
# flush the output buffer
782
my $expected_bytes = $parm->{bytes_per_line} * $parm->{lines} *
783
(($parm->{format} == SANE_FRAME_RGB
784
|| $parm->{format} == SANE_FRAME_GRAY) ? 1 : 3);
785
$expected_bytes = 0 if ($parm->{lines} < 0);
786
if ($total_bytes > $expected_bytes && $expected_bytes != 0) {
788
"%s: WARNING: read more data than announced by backend "
789
."(%u/%u)\n", $prog_name, $total_bytes, $expected_bytes;
792
printf STDERR "%s: read %u bytes in total\n", $prog_name, $total_bytes;
799
my ($max, $len, $buffer) = @_;
801
if ($Sane::STATUS != SANE_STATUS_GOOD) {
802
print STDERR "FAIL Error: $Sane::STATUS\n";
804
elsif ($len < length($buffer)) {
805
printf STDERR "FAIL Cheat: %d bytes\n", length($buffer);
807
elsif ($len > $max) {
808
printf STDERR "FAIL Overflow: %d bytes\n", $len;
811
print STDERR "FAIL No data\n";
814
print STDERR "PASS\n";
822
"gray", "RGB", "red", "green", "blue"
826
if ($Sane::STATUS != SANE_STATUS_GOOD) {
827
print STDERR "$prog_name: sane_start: $Sane::STATUS\n";
831
my $parm = $device->get_parameters;
832
if ($Sane::STATUS != SANE_STATUS_GOOD) {
833
print STDERR "$prog_name: sane_get_parameters: $Sane::STATUS\n";
837
if ($parm->{lines} >= 0) {
838
printf STDERR "$prog_name: scanning image of size %dx%d pixels at "
839
."%d bits/pixel\n", $parm->{pixels_per_line}, $parm->{lines},
840
8 * $parm->{bytes_per_line} / $parm->{pixels_per_line};
843
printf STDERR "$prog_name: scanning image %d pixels wide and "
844
."variable height at %d bits/pixel\n",$parm->{pixels_per_line},
845
8 * $parm->{bytes_per_line} / $parm->{pixels_per_line};
847
printf STDERR "$prog_name: acquiring %s frame, %d bits/sample\n",
848
$parm->{format} <= SANE_FRAME_BLUE ? $format_name[$parm->{format}]:"Unknown",
851
printf STDERR "$prog_name: reading one scanline, %d bytes...\t",
852
$parm->{bytes_per_line};
853
($image{data}, my $len) = $device->read ($parm->{bytes_per_line});
854
pass_fail ($parm->{bytes_per_line}, $len, $image{data});
855
goto cleanup if ($Sane::STATUS != SANE_STATUS_GOOD);
857
print STDERR "$prog_name: reading one byte...\t\t";
858
($image{data}, $len) = $device->read (1);
859
pass_fail (1, $len, $image{data});
860
goto cleanup if ($Sane::STATUS != SANE_STATUS_GOOD);
863
for ($i = 2; $i < $parm->{bytes_per_line} * 2; $i *= 2) {
864
printf STDERR "$prog_name: stepped read, %d bytes... \t", $i;
865
($image{data}, $len) = $device->read ($i);
866
pass_fail ($i, $len, $image{data});
867
goto cleanup if ($Sane::STATUS != SANE_STATUS_GOOD);
870
for ($i /= 2; $i > 2; $i /= 2) {
871
printf STDERR "$prog_name: stepped read, %d bytes... \t", $i - 1;
872
($image{data}, $len) = $device->read ($i - 1);
873
pass_fail ($i - 1, $len, $image{data});
874
goto cleanup if ($Sane::STATUS != SANE_STATUS_GOOD);
882
# There seems to be a bug in Getopt::Long 2.37 where l is treated as L whilst
883
# l is not in @args. Therefore the workaround is to rename l to m for the first
884
# scan and back to l for the second.
886
$_ = '-m' if ($_ eq '-l');
887
$_ = '-u' if ($_ eq '-t');
889
# make a first pass through the options with error printing and argument
890
# permutation disabled:
893
if (defined($options{L}) or defined($options{f})) {
894
my @device_list = Sane->get_devices;
895
if ($Sane::STATUS != SANE_STATUS_GOOD) {
896
print STDERR "$prog_name: sane_get_devices() failed: $Sane::STATUS\n";
899
if (defined($options{L})) {
900
foreach (@device_list) {
901
printf "device `%s' is a %s %s %s\n", $_->{name}, $_->{vendor},
902
$_->{model}, $_->{type};
904
printf "\nNo scanners were identified. If you were expecting "
905
."something different,\ncheck that the scanner is plugged "
906
."in, turned on and detected by the\nsane-find-scanner tool "
907
."(if appropriate). Please read the documentation\nwhich came "
908
."with this software (README, FAQ, manpages).\n"
909
if ($#device_list == -1);
912
for (my $i = 0; $i < @device_list; $i++) {
913
my $format = $options{f};
914
$format =~ s/%d/$device_list[$i]->{name}/g;
915
$format =~ s/%v/$device_list[$i]->{vendor}/g;
916
$format =~ s/%m/$device_list[$i]->{model}/g;
917
$format =~ s/%t/$device_list[$i]->{type}/g;
918
$format =~ s/%i/$i/g;
922
printf "default device is `%s'\n", $ENV{'SANE_DEFAULT_DEVICE'}
923
if (defined($ENV{'SANE_DEFAULT_DEVICE'}));
927
if (defined($options{V})) {
928
printf "%s %s; backend version %d.%d.%d\n", $prog_name,
929
$Sane::VERSION, Sane->get_version;
934
printf "Usage: %s [OPTION]...
936
Start image acquisition on a scanner device and write PNM image data to
939
Parameters are separated by a blank from single-character options (e.g.
940
-d epson) and by a \"=\" from multi-character options (e.g. --device-name=epson).
941
-d, --device-name=DEVICE use a given scanner device (e.g. hp:/dev/scanner)
942
--format=pnm|tiff file format of output file
943
-i, --icc-profile=PROFILE include this ICC profile into TIFF file", $prog_name;
945
-L, --list-devices show available scanner devices
946
-f, --formatted-device-list=FORMAT similar to -L, but the FORMAT of the output
947
can be specified: %%d (device name), %%v (vendor),
948
%%m (model), %%t (type), and %%i (index number)
949
-b, --batch[=FORMAT] working in batch mode, FORMAT is `out%%d.pnm' or
950
`out%%d.tif' by default depending on --format";
952
--batch-start=# page number to start naming files with
953
--batch-count=# how many pages to scan in batch mode
954
--batch-increment=# increase number in filename by an amount of #
955
--batch-double increment page number by two for 2sided originals
956
being scanned in a single sided scanner
957
--batch-prompt ask for pressing a key before scanning a page
958
--accept-md5-only only accept authorization requests using md5";
960
-p, --progress print progress messages
961
-n, --dont-scan only set options, don't actually scan
962
-T, --test test backend thoroughly
963
-h, --help display this help message and exit
964
-v, --verbose give even more status messages
965
-B, --buffer-size change default input buffersize
966
-V, --version print version information\n";
970
# If no device name was specified explicitly, we look at the
971
# environment variable SANE_DEFAULT_DEVICE. If this variable
972
# is not set, we open the first device we find (if any):
973
if (defined($ENV{'SANE_DEFAULT_DEVICE'})) {
974
$devname = $ENV{'SANE_DEFAULT_DEVICE'};
977
my @device_list = Sane->get_devices;
978
if ($Sane::STATUS != SANE_STATUS_GOOD) {
979
print STDERR "$prog_name: sane_get_devices() failed: $Sane::STATUS\n";
982
if ($#device_list == -1) {
983
print STDERR "$prog_name: no SANE devices found\n";
986
$devname = $device_list[0]{name};
990
$device = Sane::Device->open($devname);
991
if ($Sane::STATUS != SANE_STATUS_GOOD) {
992
print STDERR "$prog_name: open of device $devname failed: $Sane::STATUS\n";
993
print STDERR "\nYou seem to have specified a UNIX device name, "
994
."or filename instead of selecting\nthe SANE scanner or "
995
."image acquisition device you want to use. As an example,\n"
996
."you might want \"epson:/dev/sg0\" or "
997
."\"hp:/dev/usbscanner0\". If any supported\ndevices are "
998
."installed in your system, you should be able to see a "
999
."list with\n\"$prog_name --list-devices\".\n"
1000
if ($devname =~ /^\//);
1009
if (defined($device)) {
1010
fetch_options($device);
1011
# re-enable error printing and arg permutation
1012
Getopt::Long::Configure('no_pass_through');
1013
# There seems to be a bug in Getopt::Long 2.37 where l is treated as L whilst
1014
# l is not in @args. Therefore the workaround is to rename l to m for the first
1015
# scan and back to l for the second.
1017
$_ = '-l' if ($_ eq '-m');
1018
$_ = '-t' if ($_ eq '-u');
1020
my @ARGV_old = @ARGV;
1021
exit 1 if (! GetOptions (@args));
1022
# As it isn't possible to get the argument order from Getopt::Long 2.37, do
1028
my $i = index($ch, '=');
1029
$ch = substr($ch, 0, $i) if ($i > -1);
1037
if (defined $options{$ch}) {
1039
$window_val_user[0] = 1;
1040
($window_val[0]) = parse_vector ($window_option[0], $options{x});
1042
elsif ($ch eq 'y') {
1043
$window_val_user[1] = 1;
1044
($window_val[1]) = parse_vector ($window_option[1], $options{y});
1046
elsif ($ch eq 'l') { # tl-x
1047
process_backend_option ($device, $window[2], $options{l});
1049
elsif ($ch eq 't') { # tl-y
1050
process_backend_option ($device, $window[3], $options{t});
1053
process_backend_option ($device, $option_number{$ch}, $options{$ch});
1058
for (my $index = 0; $index < 2; ++$index) {
1059
if ($window[$index] and defined($window_val[$index])) {
1060
my $val = $window_val[$index] - 1;
1061
if ($window[$index + 2]) {
1062
my $pos = $device->get_option ($window[$index + 2]);
1063
$val = $pos + $window_val[$index] if (defined $pos);
1065
set_option ($device, $window[$index], $val);
1069
printf "\nOptions specific to device `%s':\n", $devname;
1071
for (my $i = 0; $i < $num_dev_options; ++$i) {
1072
my $short_name = '';
1075
for (my $j = 0; $j < 4; ++$j) {
1076
if ($i == $window[$j]) {
1077
$short_name = substr("xylt", $j, 1);
1078
$opt = $window_option[$j] if ($j < 2);
1081
$opt = $device->get_option_descriptor ($i) if (!$opt);
1083
printf " %s:\n", $opt->{title} if ($opt->{type} == SANE_TYPE_GROUP);
1085
next if (! ($opt->{cap} & SANE_CAP_SOFT_SELECT));
1087
print_option ($device, $i, $short_name);
1089
print "\n" if ($num_dev_options);
1094
printf "Type ``$prog_name --help -d DEVICE'' to get list of all options for DEVICE.\n\nList of available devices:";
1095
my @device_list = Sane->get_devices;
1096
if ($Sane::STATUS == SANE_STATUS_GOOD) {
1099
foreach (@device_list) {
1100
if ($column + length ($_->{name}) + 1 >= 80) {
1109
$column += length ($_->{name});
1116
exit (0) if ($dont_scan);
1118
$SIG{HUP} = \&sighandler;
1119
$SIG{INT} = \&sighandler;
1120
$SIG{PIPE} = \&sighandler;
1121
$SIG{TERM} = \&sighandler;
1123
$batch_increment = 2 if ($batch_double);
1124
$batch = 1 if ($batch_count);
1127
my $n = $batch_start_at;
1128
$batch = 1 if (defined $format);
1130
if ($batch && (! defined($format) || $format eq '')) {
1131
# if ($output_format == OUTPUT_TIFF) {
1132
# $format = "out%d.tif";
1135
$format = "out%d.pnm";
1140
"Scanning %d pages, incrementing by %d, numbering from %d\n",
1141
$batch_count, $batch_increment, $batch_start_at if ($batch);
1143
{do { # extra braces to get last to work.
1145
$path = sprintf $format, $n if ($batch); # format is NULL unless batch mode
1148
if ($batch_prompt) {
1149
printf STDERR "Place document no. %d on the scanner.\n", $n;
1150
printf STDERR "Press <RETURN> to continue.\n";
1151
printf STDERR "Press Ctrl + D to terminate.\n";
1153
if (! defined(<STDIN>)) {
1154
printf STDERR "Batch terminated, %d pages scanned\n",
1155
($n - $batch_increment);
1156
last; # get out of this loop
1159
printf STDERR "Scanning page %d\n", $n;
1163
if ($Sane::STATUS != SANE_STATUS_GOOD) {
1164
print STDERR "$prog_name: sane_start: $Sane::STATUS\n";
1168
if ($batch && ! (open($fh, '>', $path) && STDOUT->fdopen($fh, '>'))) {
1169
print STDERR "cannot open $path\n";
1171
exit SANE_STATUS_ACCESS_DENIED;
1176
printf STDERR "Scanned page %d.", $n;
1177
printf STDERR " (scanner status = %d)\n", $Sane::STATUS;
1180
if ($Sane::STATUS == SANE_STATUS_GOOD) {}
1181
elsif ($Sane::STATUS == SANE_STATUS_EOF) {
1182
$Sane::_status = SANE_STATUS_GOOD;
1191
$n += $batch_increment;
1194
&& ($batch_count == -1 || --$batch_count))
1195
&& SANE_STATUS_GOOD == $Sane::STATUS);}
1200
$Sane::_status = test_it ();
1209
scanimage \- scan an image
1214
B<[ -d | --device-name>
1218
B<[ -i | --icc-profile>
1220
B<[ -L | --list-devices ]>
1221
B<[ -f | --formatted-device-list >
1229
B<[ --batch-increment>
1231
B<[ --batch-double ]>
1232
B<[ --accept-md5-only ]>
1233
B<[ -p | --progress ]>
1234
B<[ -n | --dont-scan ]>
1237
B<[ -v | --verbose ]>
1238
B<[ -B | --buffersize ]>
1239
B<[ -V | --version ]>
1240
I<[ device-specific-options ]>
1245
is a command-line interface to control image acquisition devices such
1246
as flatbed scanners or cameras. The device is controlled via
1247
command-line options. After command-line processing,
1249
normally proceeds to acquire an image. The image data is written to
1250
standard output in one of the PNM (portable aNyMaP) formats (PBM for
1251
black-and-white images, PGM for grayscale images, and PPM for color
1252
images) or in TIFF (black-and-white, grayscale or color).
1254
accesses image acquisition devices through the
1256
(Scanner Access Now Easy) interface and can thus support any device for which
1262
to get a list of available backends).
1266
To get a list of devices:
1270
To scan with default settings to the file image.pnm:
1272
scanimage >image.pnm
1274
To scan 100x100 mm to the file image.tiff (-x and -y may not be available with
1277
scanimage -x 100 -y 100 --format=tiff >image.tiff
1279
To print all available options:
1285
Parameters are separated by a blank from single-character options (e.g.
1286
-d epson) and by a "=" from multi-character options (e.g. --device-name=epson).
1292
options must be followed by a
1295
I<` epson:/dev/sg0 '>
1297
I<` hp:/dev/usbscanner0 '.>
1298
A (partial) list of available devices can be obtained with the
1300
option (see below). If no device-name is specified explicitly,
1302
reads a device-name from the environment variable
1303
B<SANE_DEFAULT_DEVICE .>
1304
If this variable is not set,
1306
will attempt to open the first available device.
1311
option selects how image data is written to standard output.
1319
is not used, PNM is written.
1325
option is used to include an ICC profile into a TIFF file.
1331
option requests a (partial) list of devices that are available. The
1332
list is not complete since some devices may be available, but are not
1333
listed in any of the configuration files (which are typically stored
1335
I</caehome/ra28145/etc/sane.d ).>
1336
This is particularly the case when accessing scanners through the network. If
1337
a device is not listed in a configuration file, the only way to access it is
1338
by its full device name. You may need to consult your system administrator to
1339
find out the names of such devices.
1344
B<--formatted-device-list>
1345
option works similar to
1347
but requires a format string.
1349
replaces the placeholders
1351
with the device name, vendor name, model name, scanner type and an index
1352
number respectively. The command
1356
=item B<scanimage -f>
1357
I<\*(lq scanner number %i device %d is a %t, model %m, produced by %v \*(rq>
1361
will produce something like:
1365
=item scanner number 0 device sharp:/dev/sg1 is a flatbed scanner, model JX250
1366
SCSI, produced by SHARP
1372
options provide the features for scanning documents using document
1376
is used to specify the format of the filename that each page will be written
1377
to. Each page is written out to a single file. If
1379
is not specified, the default of out%d.pnm (or out%d.tif for --format tiff)
1382
is given as a printf style string with one integer parameter.
1385
selects the page number to start naming files with. If this option is not
1386
given, the counter will start at 0.
1389
specifies the number of pages to attempt to scan. If not given,
1390
scanimage will continue scanning until the scanner returns a state
1391
other than OK. Not all scanners with document feeders signal when the
1392
ADF is empty, use this command to work around them.
1394
B<--batch-increment>
1396
you can change the amount that the number in the filename is incremented
1397
by. Generally this is used when you are scanning double-sided documents
1398
on a single-sided document feeder. A specific command is provided to
1401
will automatically set the increment to 2.
1403
will ask for pressing RETURN before scanning a page. This can be used for
1404
scanning multiple pages without an automatic document feeder.
1407
B<--accept-md5-only>
1408
option only accepts user authorization requests that support MD5 security. The
1412
is capable of doing such requests. See
1419
option requests that
1421
prints a progress counter. It shows how much image data of the current image has
1422
already been received by
1430
option requests that
1432
only sets the options provided by the user but doesn't actually perform a
1433
scan. This option can be used to e.g. turn off the scanner's lamp (if
1434
supported by the backend).
1440
option requests that
1442
performs a few simple sanity tests to make sure the backend works as
1445
API (in particular the
1447
function is exercised by this test).
1453
options request help information. The information is printed on
1454
standard output and in this case, no attempt will be made to acquire
1461
options increase the verbosity of the operation of
1463
The option may be specified repeatedly, each time increasing the verbosity
1470
option changes the input buffersize that
1472
uses from default 32*1024 to 1024*1024 kbytes.
1478
option requests that
1480
prints the program and package name, the version number of
1483
distribution that it came with and the version of the backend that it
1484
loads. Usually that's the dll backend. If more information about the version
1485
numbers of the backends are necessary, the
1487
variable for the dll backend can be used. Example: SANE_DEBUG_DLL=3 scanimage
1490
As you might imagine, much of the power of
1492
comes from the fact that it can control any
1494
backend. Thus, the exact set of command-line options depends on the
1495
capabilities of the selected device. To see the options for a device named
1499
via a command-line of the form:
1503
=item B<scanimage --help --device-name>
1508
The documentation for the device-specific options printed by
1510
is best explained with a few examples:
1513
Top-left x position of scan area.
1517
=item The description above shows that option
1519
expects an option value in the range from 0 to 218 mm. The
1520
value in square brackets indicates that the current option value is 0
1521
mm. Most backends provide similar geometry options for top-left y position (-t),
1522
width (-x) and height of scan-area (-y).
1526
--brightness -100..100% [0]
1527
Controls the brightness of the acquired image.
1531
=item The description above shows that option
1533
expects an option value in the range from -100 to 100 percent. The
1534
value in square brackets indicates that the current option value is 0
1539
--default-enhancements
1540
Set default values for enhancement controls.
1544
=item The description above shows that option
1545
B<--default-enhancements>
1546
has no option value. It should be thought of as having an immediate
1547
effect at the point of the command-line at which it appears. For
1548
example, since this option resets the
1550
option, the option-pair
1551
B<--brightness 50 --default-enhancements>
1552
would effectively be a no-op.
1556
--mode Lineart|Gray|Color [Gray]
1557
Selects the scan mode (e.g., lineart or color).
1561
=item The description above shows that option
1563
accepts an argument that must be one of the strings
1568
The value in the square bracket indicates that the option is currently
1571
For convenience, it is legal to abbreviate the string values as long as
1572
they remain unique. Also, the case of the spelling doesn't matter. For
1573
example, option setting
1580
--custom-gamma[=(yes|no)] [inactive]
1581
Determines whether a builtin or a custom gamma-table
1586
=item The description above shows that option
1588
expects either no option value, a "yes" string, or a "no" string.
1589
Specifying the option with no value is equivalent to specifying "yes".
1590
The value in square-brackets indicates that the option is not
1591
currently active. That is, attempting to set the option would result
1592
in an error message. The set of available options typically depends
1593
on the settings of other options. For example, the
1595
table might be active only when a grayscale or color scan-mode has
1602
option is processed only after all other options have been processed.
1603
This makes it possible to see the option settings for a particular
1604
mode by specifying the appropriate mode-options along
1607
option. For example, the command-line:
1609
B< scanimage --help --mode>
1612
would print the option settings that are in effect when the color-mode
1617
--gamma-table 0..255,...
1618
Gamma-correction table. In color mode this option
1619
equally affects the red, green, and blue channels
1620
simultaneously (i.e., it is an intensity gamma table).
1624
=item The description above shows that option
1626
expects zero or more values in the range 0 to 255. For example, a
1627
legal value for this option would be "3,4,5,6,7,8,9,10,11,12". Since
1628
it's cumbersome to specify long vectors in this form, the same can be
1629
expressed by the abbreviated form "[0]3-[9]12". What this means is
1630
that the first vector element is set to 3, the 9-th element is set to
1631
12 and the values in between are interpolated linearly. Of course, it
1632
is possible to specify multiple such linear segments. For example,
1633
"[0]3-[2]3-[6]7,[7]10-[9]6" is equivalent to "3,3,3,4,5,6,7,10,8,6".
1636
can be used to generate such gamma tables (see
1637
B<gamma4scanimage (1)>
1642
--filename <string> [/tmp/input.ppm]
1643
The filename of the image to be loaded.
1647
=item The description above is an example of an option that takes an
1648
arbitrary string value (which happens to be a filename). Again,
1649
the value in brackets show that the option is current set to the
1659
=item B<SANE_DEFAULT_DEVICE>
1661
The default device-name.
1669
=item I</caehome/ra28145/etc/sane.d>
1671
This directory holds various configuration files. For details, please
1672
refer to the manual pages listed below.
1674
=item I<~/.sane/pass>
1676
This file contains lines of the form
1678
=item user:password:resource
1680
scanimage uses this information to answer user authorization requests
1681
automatically. The file must have 0600 permissions or stricter. You should
1682
use this file in conjunction with the --accept-md5-only option to avoid
1683
server-side attacks. The resource may contain any character but is limited
1691
B<gamma4scanimage (1),>
1698
B<sane-"backendname" (5)>
1702
Transliterated from the C original by Jeffrey Ratcliffe.
1706
All the bugs of scanimage and much, much more.