~ubuntu-branches/debian/sid/libsane-perl/sid

« back to all changes in this revision

Viewing changes to examples/scanimage.pl

  • Committer: Bazaar Package Importer
  • Author(s): Jeffrey Ratcliffe
  • Date: 2008-11-25 20:31:30 UTC
  • Revision ID: james.westby@ubuntu.com-20081125203130-6j89phvesi73dxui
Tags: upstream-0.02
ImportĀ upstreamĀ versionĀ 0.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
use warnings;
 
4
use strict;
 
5
use Sane;
 
6
use Data::Dumper;
 
7
use Getopt::Long qw(:config no_ignore_case pass_through);
 
8
use File::Basename;
 
9
use IO::Handle;
 
10
 
 
11
#$Sane::DEBUG = 1;
 
12
 
 
13
my (%options, @window_val_user, @window_option, @window_val,
 
14
    @window, $num_dev_options, $device, $format, $devname, %option_number);
 
15
my $verbose = 0;
 
16
my $help = 0;
 
17
my $test = 0;
 
18
my $batch = 0;
 
19
my $batch_start_at = 1;
 
20
my $batch_count = -1;
 
21
my $batch_increment = 1;
 
22
my $buffer_size = (32 * 1024);  # default size
 
23
my $tl_x = 0;
 
24
my $tl_y = 0;
 
25
my $br_x = 0;
 
26
my $br_y = 0;
 
27
my $w_x = 0;
 
28
my $h_y = 0;
 
29
my $resolution_optind = -1;
 
30
my $resolution_value = 0;
 
31
my $progress = 0;
 
32
my $batch_double = 0;
 
33
my $batch_prompt = 0;
 
34
my $dont_scan = 0;
 
35
my $prog_name = basename($0);
 
36
my @args = (\%options, 'd|device-name=s' => \$devname,
 
37
                       'L|list-devices',
 
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,
 
47
                       'T|test' => \$test,
 
48
                       'h|help' => \$help,
 
49
                       'v|verbose+' => \$verbose,
 
50
                       'B|buffer-size' => \$buffer_size,
 
51
                       'V|version');
 
52
 
 
53
 
 
54
sub sighandler {
 
55
 my $signum = shift;
 
56
 
 
57
 my $first_time = SANE_TRUE;
 
58
 
 
59
 if ($device) {
 
60
  print STDERR "$prog_name: received signal $signum\n";
 
61
  if ($first_time) {
 
62
   $first_time = SANE_FALSE;
 
63
   print STDERR "$prog_name: trying to stop scanner\n";
 
64
   $device->cancel;
 
65
  }
 
66
  else {
 
67
   print STDERR "$prog_name: aborting\n";
 
68
   _exit (0);
 
69
  }
 
70
 }
 
71
}
 
72
 
 
73
 
 
74
sub print_unit {
 
75
 my ($unit) = @_;
 
76
 
 
77
 if ($unit == SANE_UNIT_PIXEL) {
 
78
  print "pel";
 
79
 }
 
80
 elsif ($unit == SANE_UNIT_BIT) {
 
81
  print "bit";
 
82
 }
 
83
 elsif ($unit == SANE_UNIT_MM) {
 
84
  print "mm";
 
85
 }
 
86
 elsif ($unit == SANE_UNIT_DPI) {
 
87
  print "dpi";
 
88
 }
 
89
 elsif ($unit == SANE_UNIT_PERCENT) {
 
90
  print "%";
 
91
 }
 
92
 elsif ($unit == SANE_UNIT_MICROSECOND) {
 
93
  print "us";
 
94
 }
 
95
}
 
96
 
 
97
 
 
98
sub print_option {
 
99
 my ($device, $opt_num, $short_name) = @_;
 
100
 
 
101
 my $not_first = SANE_FALSE;
 
102
 my $maxwindow = 0;
 
103
 
 
104
 my $opt = $device->get_option_descriptor ($opt_num);
 
105
 
 
106
 if ($short_name) {
 
107
  printf "    -%s", $short_name;
 
108
 }
 
109
 else {
 
110
  printf "    --%s", $opt->{name};
 
111
 }
 
112
 
 
113
 if ($opt->{type} == SANE_TYPE_BOOL) {
 
114
  print "[=(";
 
115
  print "auto|" if ($opt->{cap} & SANE_CAP_AUTOMATIC);
 
116
  print "yes|no)]";
 
117
 }
 
118
 elsif ($opt->{type} != SANE_TYPE_BUTTON) {
 
119
  print ' ';
 
120
  if ($opt->{cap} & SANE_CAP_AUTOMATIC) {
 
121
   print "auto|";
 
122
   $not_first = SANE_TRUE;
 
123
  }
 
124
  if ($opt->{constraint_type} == SANE_CONSTRAINT_NONE) {
 
125
   if ($opt->{type} == SANE_TYPE_INT) {
 
126
    print "<int>";
 
127
   }
 
128
   elsif ($opt->{type} == SANE_TYPE_FIXED) {
 
129
    print "<float>";
 
130
   }
 
131
   elsif ($opt->{type} == SANE_TYPE_STRING) {
 
132
    print "<string>";
 
133
   }
 
134
   print ",..." if ($opt->{max_values} > 1);
 
135
  }
 
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;
 
142
   }
 
143
   elsif ($opt->{name} eq SANE_NAME_SCAN_BR_Y) {
 
144
    $maxwindow = $opt->{constraint}{max} - $tl_y;
 
145
    printf $format, $opt->{constraint}{min}, $maxwindow;
 
146
   }
 
147
   else {
 
148
    printf $format, $opt->{constraint}{min}, $opt->{constraint}{max};
 
149
   }
 
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});
 
154
  }
 
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);
 
159
 
 
160
    print $opt->{constraint}[$i];
 
161
   }
 
162
   if ($opt->{constraint_type} == SANE_CONSTRAINT_WORD_LIST) {
 
163
    print_unit ($opt->{unit});
 
164
    print ",..." if ($opt->{max_values} > 1);
 
165
   }
 
166
  }
 
167
 }
 
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);
 
172
   print " [";
 
173
   if ($opt->{type} == SANE_TYPE_BOOL) {
 
174
    print ($val ? "yes" : "no");
 
175
   }
 
176
   elsif ($opt->{type} == SANE_TYPE_INT or $opt->{type} == SANE_TYPE_FIXED) {
 
177
    my $format = "%g";
 
178
    $format = "%d" if ($opt->{type} == SANE_TYPE_INT);
 
179
    if ($opt->{name} eq SANE_NAME_SCAN_TL_X) {
 
180
     $tl_x = $val;
 
181
     printf $format, $tl_x;
 
182
    }
 
183
    elsif ($opt->{name} eq SANE_NAME_SCAN_TL_Y) {
 
184
     $tl_y = $val;
 
185
     printf $format, $tl_y;
 
186
    }
 
187
    elsif ($opt->{name} eq SANE_NAME_SCAN_BR_X) {
 
188
     $br_x = $val;
 
189
     $w_x = $br_x - $tl_x;
 
190
     printf $format, $w_x;
 
191
    }
 
192
    elsif ($opt->{name} eq SANE_NAME_SCAN_BR_Y) {
 
193
     $br_y = $val;
 
194
     $h_y = $br_y - $tl_y;
 
195
     printf $format, $h_y;
 
196
    }
 
197
    else {
 
198
     printf $format, $val;
 
199
    }
 
200
   }
 
201
   elsif ($opt->{type} == SANE_TYPE_STRING) {
 
202
    print $val;
 
203
   }
 
204
   print ']';
 
205
  }
 
206
 }
 
207
 
 
208
 print " [inactive]" if ($opt->{cap} & SANE_CAP_INACTIVE);
 
209
 
 
210
 print "\n        ";
 
211
 
 
212
 if ($short_name eq 'x') {
 
213
  print "Width of scan-area.";
 
214
 }
 
215
 elsif ($short_name eq 'y') {
 
216
  print "Height of scan-area.";
 
217
 }
 
218
 else {
 
219
  my $column = 8;
 
220
  my $last_break = 0;
 
221
  my $start = 0;
 
222
  for (my $pos = 0; $pos < length($opt->{desc}); ++$pos) {
 
223
   ++$column;
 
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
 
228
    print "\n        ";
 
229
    $column = 8 + $pos - $start;
 
230
   }
 
231
  }
 
232
  print substr($opt->{desc}, $start++, 1) while ($start < length($opt->{desc}));
 
233
 }
 
234
 print "\n";
 
235
}
 
236
 
 
237
 
 
238
# A scalar has the following syntax:
 
239
#
 
240
#     V [ U ]
 
241
#
 
242
#   V is the value of the scalar.  It is either an integer or a
 
243
#   floating point number, depending on the option type.
 
244
#
 
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:
 
248
#
 
249
#     Option's unit:    Allowed units:
 
250
#
 
251
#     SANE_UNIT_NONE:
 
252
#     SANE_UNIT_PIXEL:  pel
 
253
#     SANE_UNIT_BIT:    b (bit), B (byte)
 
254
#     SANE_UNIT_MM:     mm (millimeter), cm (centimeter), in or " (inches),
 
255
#     SANE_UNIT_DPI:    dpi
 
256
#     SANE_UNIT_PERCENT:        %
 
257
#     SANE_UNIT_MICROSECOND:    us
 
258
 
 
259
sub parse_scalar {
 
260
 my ($opt, $str) = @_;
 
261
 
 
262
 my ($v, $unit);
 
263
 if ($str =~ /^(\d*\.?\d*)(cm|mm|in|\"|b|B|dpi|%|us)?/) {
 
264
  $v = $1;
 
265
  $unit = $2;
 
266
  $unit = '' if not defined $unit;
 
267
 }
 
268
 else {
 
269
  print STDERR
 
270
             "$prog_name: option --$opt->{name}: bad option value (rest of option: $str)\n";
 
271
  exit (1);
 
272
 }
 
273
 
 
274
 if ($opt->{unit} == SANE_UNIT_BIT) {
 
275
  $v *= 8 if ($unit eq 'B');
 
276
 }
 
277
 elsif ($opt->{unit} == SANE_UNIT_MM) {
 
278
  if ($unit eq 'cm') {
 
279
   $v *= 10;
 
280
  }
 
281
  elsif ($unit eq 'in') {
 
282
   $v *= 25.4;
 
283
  }
 
284
 }
 
285
 return $v, substr($str, length($v) + length($unit), length($str));
 
286
}
 
287
 
 
288
 
 
289
# A vector has the following syntax:
 
290
#
 
291
#     [ '[' I ']' ] S { [','|'-'] [ '[' I ']' S }
 
292
#
 
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.
 
297
#
 
298
#   S is a scalar value as defined by parse_scalar().
 
299
#
 
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
 
307
#   255.
 
308
 
 
309
sub parse_vector {
 
310
 my ($opt, $str) = @_;
 
311
 
 
312
 my $index = -1;
 
313
 my $prev_value = 0;
 
314
 my $prev_index = 0;
 
315
 my $separator = '';
 
316
 my (@vector, $value);
 
317
 do {
 
318
  if ($str =~ /^\[/) {
 
319
   if ($str =~ /^\[(\d*\.?\d*)\]/) {
 
320
    $index = $1;
 
321
   }
 
322
   else {
 
323
    print STDERR
 
324
             "$prog_name: option --$opt->{name}: closing bracket missing "
 
325
                ."(rest of option: $str)\n";
 
326
    exit (1);
 
327
   }
 
328
  }
 
329
  else {
 
330
   ++$index;
 
331
  }
 
332
 
 
333
  if ($index < 0 or $index >= length($str)) {
 
334
   printf STDERR
 
335
             "$prog_name: option --$opt->{name}: index $index out of range [0..%d]\n",
 
336
               length($str);
 
337
   exit (1);
 
338
  }
 
339
 
 
340
  # read value
 
341
  ($value, $str) = parse_scalar ($opt, $str);
 
342
 
 
343
  if ($str ne '' and $str !~ /^[-,]/) {
 
344
   print STDERR
 
345
             "$prog_name: option --$opt->{name}: illegal separator (rest of option: $str)\n";
 
346
   exit (1);
 
347
  }
 
348
 
 
349
  # store value:
 
350
  $vector[$index] = $value;
 
351
  if ($separator eq '-') {
 
352
   # interpolate
 
353
   my $v = $prev_value;
 
354
   my $slope = ($value - $v) / ($index - $prev_index);
 
355
 
 
356
   for (my $i = $prev_index + 1; $i < $index; ++$i) {
 
357
    $v += $slope;
 
358
    $vector[$i] = $v;
 
359
   }
 
360
  }
 
361
 
 
362
  $prev_index = $index;
 
363
  $prev_value = $value;
 
364
  $separator = substr($str, 0, 1);
 
365
 }
 
366
 while ($separator eq ',' || $separator eq '-');
 
367
 
 
368
 if ($verbose > 2) {
 
369
  print STDERR "$prog_name: value for --$opt->{name} is: ";
 
370
  for (@vector) {
 
371
   print STDERR "$_ ";
 
372
  }
 
373
  print STDERR "\n";
 
374
 }
 
375
 
 
376
 return @vector;
 
377
}
 
378
 
 
379
 
 
380
sub fetch_options {
 
381
 my $device = shift;
 
382
 
 
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";
 
387
  exit (1);
 
388
 }
 
389
 
 
390
 for (my $i = 0; $i < $num_dev_options; ++$i) {
 
391
  my $opt = $device->get_option_descriptor ($i);
 
392
 
 
393
  next if (! ($opt->{cap} & SANE_CAP_SOFT_SELECT));
 
394
 
 
395
  $option_number{$opt->{name}} = $i;
 
396
 
 
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));
 
402
 
 
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) {
 
409
    $window[2] = $i;
 
410
    $opt->{name} = 'l';
 
411
   }
 
412
   elsif ($opt->{name} eq SANE_NAME_SCAN_TL_Y) {
 
413
    $window[3] = $i;
 
414
    $opt->{name} = 't';
 
415
   }
 
416
   elsif ($opt->{name} eq SANE_NAME_SCAN_BR_X) {
 
417
    $window[0] = $i;
 
418
    $opt->{name} = '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]);
 
424
   }
 
425
   elsif ($opt->{name} eq SANE_NAME_SCAN_BR_Y) {
 
426
    $window[1] = $i;
 
427
    $opt->{name} = '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]);
 
433
   }
 
434
  }
 
435
 
 
436
  if ($opt->{type} == SANE_TYPE_BOOL) {
 
437
   push @args, "$opt->{name}:s";
 
438
  }
 
439
  elsif ($opt->{type} == SANE_TYPE_BUTTON) {
 
440
   push @args, $opt->{name};
 
441
  }
 
442
  else {
 
443
   push @args, "$opt->{name}=s";
 
444
  }
 
445
 }
 
446
 
 
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);
 
453
  }
 
454
 }
 
455
}
 
456
 
 
457
 
 
458
sub set_option {
 
459
 my ($device, $optnum, $value) = @_;
 
460
 
 
461
 my $opt = $device->get_option_descriptor ($optnum);
 
462
 if ($opt and ($opt->{cap} & SANE_CAP_INACTIVE)) {
 
463
  print STDERR
 
464
             "$prog_name: ignored request to set inactive option $opt->{name}\n"
 
465
   if ($verbose > 0);
 
466
  return;
 
467
 }
 
468
 
 
469
 my $info = $device->set_option($optnum, $value);
 
470
 if ($Sane::STATUS != SANE_STATUS_GOOD) {
 
471
  print STDERR
 
472
            "$prog_name: setting of option --$opt->{name} failed ($Sane::STATUS)\n";
 
473
  exit (1);
 
474
 }
 
475
 
 
476
 if (($info & SANE_INFO_INEXACT) and $opt->{max_values} == 1) {
 
477
  my $orig = $value;
 
478
  $value = $device->get_option($optnum);
 
479
  if ($opt->{type} == SANE_TYPE_INT) {
 
480
   printf STDERR
 
481
     "$prog_name: rounded value of $opt->{name} from %d to %d\n", $orig, $value;
 
482
  }
 
483
  elsif ($opt->{type} == SANE_TYPE_FIXED) {
 
484
   printf STDERR
 
485
     "$prog_name: rounded value of $opt->{name} from %g to %g\n", $orig, $value;
 
486
  }
 
487
 }
 
488
 fetch_options ($device) if ($info & SANE_INFO_RELOAD_OPTIONS);
 
489
}
 
490
 
 
491
 
 
492
sub process_backend_option {
 
493
 my ($device, $optnum, $optarg) = @_;
 
494
 
 
495
 my $opt = $device->get_option_descriptor ($optnum);
 
496
 
 
497
 if ($opt and ($opt->{cap} & SANE_CAP_INACTIVE)) {
 
498
  print STDERR "$prog_name: attempted to set inactive option $opt->{name}\n";
 
499
  exit (1);
 
500
 }
 
501
 
 
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";
 
506
   exit (1);
 
507
  }
 
508
  return;
 
509
 }
 
510
 
 
511
 my $value;
 
512
 if ($opt->{type} == SANE_TYPE_BOOL) {
 
513
  $value = 1;                # no argument means option is set
 
514
  if ($optarg) {
 
515
   if ($optarg =~ /^yes$/i) {
 
516
    $value = 1;
 
517
   }
 
518
   elsif ($optarg =~ /^no$/i) {
 
519
    $value = 0;
 
520
   }
 
521
   else {
 
522
    printf STDERR "$prog_name: option --$opt->{name}: bad option value `$optarg'\n";
 
523
    exit (1);
 
524
   }
 
525
  }
 
526
 }
 
527
 elsif ($opt->{type} == SANE_TYPE_INT or $opt->{type} == SANE_TYPE_FIXED) {
 
528
  my @vector = parse_vector ($opt, $optarg);
 
529
  $value = \@vector;
 
530
 }
 
531
 elsif ($opt->{type} == SANE_TYPE_STRING) {
 
532
  $value = $optarg;
 
533
 }
 
534
 elsif ($opt->{type} == SANE_TYPE_BUTTON) {
 
535
  $value = 0;                # value doesn't matter
 
536
 }
 
537
 else {
 
538
  printf STDERR "$prog_name: duh, got unknown option type $opt->{type}\n";
 
539
  return;
 
540
 }
 
541
 set_option ($device, $optnum, $value);
 
542
}
 
543
 
 
544
 
 
545
sub write_pnm_header {
 
546
 my ($format, $width, $height, $depth) = @_;
 
547
 
 
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
 
550
# to read the image.
 
551
 
 
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;
 
556
 }
 
557
 else {
 
558
  if ($depth == 1) {
 
559
   printf "P4\n# SANE data follows\n%d %d\n", $width, $height;
 
560
  }
 
561
  else {
 
562
   printf "P5\n# SANE data follows\n%d %d\n%d\n", $width, $height,
 
563
                ($depth <= 8) ? 255 : 65535;
 
564
  }
 
565
 }
 
566
}
 
567
 
 
568
 
 
569
sub scan_it {
 
570
 my $first_frame = 1;
 
571
 my $offset = 0;
 
572
 my $must_buffer = 0;
 
573
 my $min = 0xff;
 
574
 my $max = 0;
 
575
 my %image;
 
576
 my @format_name = (
 
577
   "gray", "RGB", "red", "green", "blue"
 
578
 );
 
579
 my $total_bytes = 0;
 
580
 my $hang_over = -1;
 
581
 
 
582
 my $parm;
 
583
 {do { # extra braces to get last to work.
 
584
  if (!$first_frame) {
 
585
   $device->start;
 
586
   if ($Sane::STATUS != SANE_STATUS_GOOD) {
 
587
    printf STDERR "$prog_name: sane_start: $Sane::STATUS\n";
 
588
    goto cleanup;
 
589
   }
 
590
  }
 
591
 
 
592
  $parm = $device->get_parameters;
 
593
  if ($Sane::STATUS != SANE_STATUS_GOOD) {
 
594
   printf STDERR "$prog_name: sane_get_parameters: $Sane::STATUS\n";
 
595
   goto cleanup;
 
596
  }
 
597
 
 
598
  if ($verbose) {
 
599
   if ($first_frame) {
 
600
    if ($parm->{lines} >= 0) {
 
601
     printf STDERR "$prog_name: scanning image of size %dx%d pixels at "
 
602
               ."%d bits/pixel\n",
 
603
               $parm->{pixels_per_line}, $parm->{lines},
 
604
               8 * $parm->{bytes_per_line} / $parm->{pixels_per_line};
 
605
    }
 
606
    else {
 
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};
 
611
    }
 
612
   }
 
613
 
 
614
   printf STDERR "$prog_name: acquiring %s frame\n",
 
615
    $parm->{format} <= SANE_FRAME_BLUE ? $format_name[$parm->{format}]:"Unknown";
 
616
  }
 
617
 
 
618
  if ($first_frame) {
 
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);
 
623
    $must_buffer = 1;
 
624
    $offset = $parm->{format} - SANE_FRAME_RED;
 
625
   }
 
626
   elsif ($parm->{format} == SANE_FRAME_RGB) {
 
627
    die unless (($parm->{depth} == 8) || ($parm->{depth} == 16));
 
628
   }
 
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) {
 
633
     $must_buffer = 1;
 
634
     $offset = 0;
 
635
    }
 
636
    else {
 
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,
 
641
#                                icc_profile);
 
642
#     else {
 
643
       write_pnm_header ($parm->{format}, $parm->{pixels_per_line},
 
644
                         $parm->{lines}, $parm->{depth});
 
645
#      }
 
646
    }
 
647
   }
 
648
  }
 
649
  else {
 
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;
 
654
  }
 
655
  my $hundred_percent = $parm->{bytes_per_line} * $parm->{lines}
 
656
    * (($parm->{format} == SANE_FRAME_RGB || $parm->{format} == SANE_FRAME_GRAY) ? 1:3);
 
657
 
 
658
  while (1) {
 
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);
 
664
 
 
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";
 
670
     return;
 
671
    }
 
672
    last;
 
673
   }
 
674
 
 
675
   if ($must_buffer) {
 
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
 
680
    # the image
 
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);
 
686
     }
 
687
     $offset += 3 * $len;
 
688
    }
 
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);
 
693
     }
 
694
     $offset += $len;
 
695
    }
 
696
   }
 
697
   else { # ! must_buffer
 
698
#    if (($output_format == OUTPUT_TIFF) || ($parm->{depth} != 16)) {
 
699
#     print $buffer;
 
700
#    }
 
701
#    else {
 
702
##if !defined(WORDS_BIGENDIAN)
 
703
#     my $start = 0;
 
704
#
 
705
#     # check if we have saved one byte from the last sane_read
 
706
#     if ($hang_over > -1) {
 
707
#      if ($len > 0) {
 
708
#       print $buffer;
 
709
#       $buffer = $hang_over.$buffer;
 
710
#       $hang_over = -1;
 
711
#       $start = 1;
 
712
#      }
 
713
#     }
 
714
#     # now do the byte-swapping
 
715
#     $buffer = reverse $buffer;
 
716
#
 
717
#     # check if we have an odd number of bytes
 
718
#     if ((($len - $start) % 2) != 0) {
 
719
#      $hang_over = substr($buffer, $len - 1, 1);
 
720
#      $len--;
 
721
#     }
 
722
##endif
 
723
     print $buffer;
 
724
#    }
 
725
   }
 
726
 
 
727
   if ($verbose && $parm->{depth} == 8) {
 
728
    for (split(//, $buffer)) {
 
729
     my $c = ord;
 
730
     if ($c >= $max) {
 
731
      $max = $c;
 
732
     }
 
733
     elsif ($c < $min) {
 
734
      $min = $c;
 
735
     }
 
736
    }
 
737
   }
 
738
 
 
739
  }
 
740
  $first_frame = 0;
 
741
 }
 
742
 while (!$parm->{last_frame});}
 
743
 
 
744
 if ($must_buffer) {
 
745
  if ($parm->{lines} > 0) {
 
746
   $image{height} = $parm->{lines};
 
747
  }
 
748
  else {
 
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);
 
753
  }
 
754
#  if ($output_format == OUTPUT_TIFF) {
 
755
#   sanei_write_tiff_header ($parm->{format}, $parm->{pixels_per_line},
 
756
#                             $parm->{lines}, $parm->{depth}, $resolution_value,
 
757
#                             icc_profile);
 
758
#  }
 
759
#  else {
 
760
   write_pnm_header ($parm->{format}, $parm->{pixels_per_line}, $image{height}, $parm->{depth});
 
761
#  }
 
762
#  if (($output_format == OUTPUT_TIFF) || ($image{Bpp} == 1)
 
763
#      || ($image{Bpp} == 3)) {
 
764
#   print $image{data};
 
765
#  }
 
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;
 
772
#   }
 
773
##endif
 
774
   for (@{$image{data}}) {print;}
 
775
#  }
 
776
 }
 
777
 
 
778
 # flush the output buffer
 
779
 STDOUT->flush;
 
780
 
 
781
cleanup:
 
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) {
 
787
  printf STDERR
 
788
              "%s: WARNING: read more data than announced by backend "
 
789
              ."(%u/%u)\n", $prog_name, $total_bytes, $expected_bytes;
 
790
 }
 
791
 elsif ($verbose) {
 
792
  printf STDERR "%s: read %u bytes in total\n", $prog_name, $total_bytes;
 
793
 }
 
794
 return;
 
795
}
 
796
 
 
797
 
 
798
sub pass_fail {
 
799
 my ($max, $len, $buffer) = @_;
 
800
 
 
801
 if ($Sane::STATUS != SANE_STATUS_GOOD) {
 
802
  print STDERR "FAIL Error: $Sane::STATUS\n";
 
803
 }
 
804
 elsif ($len < length($buffer)) {
 
805
  printf STDERR "FAIL Cheat: %d bytes\n", length($buffer);
 
806
 }
 
807
 elsif ($len > $max) {
 
808
  printf STDERR "FAIL Overflow: %d bytes\n", $len;
 
809
 }
 
810
 elsif ($len == 0) {
 
811
  print STDERR "FAIL No data\n";
 
812
 }
 
813
 else {
 
814
  print STDERR "PASS\n";
 
815
 }
 
816
}
 
817
 
 
818
 
 
819
sub test_it {
 
820
 my %image;
 
821
 my @format_name = (
 
822
   "gray", "RGB", "red", "green", "blue"
 
823
 );
 
824
 
 
825
 $device->start;
 
826
 if ($Sane::STATUS != SANE_STATUS_GOOD) {
 
827
  print STDERR "$prog_name: sane_start: $Sane::STATUS\n";
 
828
  goto cleanup;
 
829
 }
 
830
 
 
831
 my $parm = $device->get_parameters;
 
832
 if ($Sane::STATUS != SANE_STATUS_GOOD) {
 
833
  print STDERR "$prog_name: sane_get_parameters: $Sane::STATUS\n";
 
834
  goto cleanup;
 
835
 }
 
836
 
 
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};
 
841
 }
 
842
 else {
 
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};
 
846
 }
 
847
 printf STDERR "$prog_name: acquiring %s frame, %d bits/sample\n",
 
848
          $parm->{format} <= SANE_FRAME_BLUE ? $format_name[$parm->{format}]:"Unknown",
 
849
          $parm->{depth};
 
850
 
 
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);
 
856
 
 
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);
 
861
 
 
862
 my $i;
 
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);
 
868
 }
 
869
 
 
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);
 
875
 }
 
876
 
 
877
cleanup:
 
878
 $device->cancel;
 
879
 return;
 
880
}
 
881
 
 
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.
 
885
for (@ARGV) {
 
886
 $_ = '-m' if ($_ eq '-l');
 
887
 $_ = '-u' if ($_ eq '-t');
 
888
}
 
889
# make a first pass through the options with error printing and argument
 
890
# permutation disabled:
 
891
GetOptions (@args);
 
892
 
 
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";
 
897
  exit (1);
 
898
 }
 
899
 if (defined($options{L})) {
 
900
  foreach (@device_list) {
 
901
   printf "device `%s' is a %s %s %s\n", $_->{name}, $_->{vendor},
 
902
                                          $_->{model}, $_->{type};
 
903
  }
 
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);
 
910
 }
 
911
 else {
 
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;
 
919
   print $format;
 
920
  }
 
921
 }
 
922
 printf "default device is `%s'\n", $ENV{'SANE_DEFAULT_DEVICE'}
 
923
  if (defined($ENV{'SANE_DEFAULT_DEVICE'}));
 
924
 exit (0);
 
925
}
 
926
 
 
927
if (defined($options{V})) {
 
928
 printf "%s %s; backend version %d.%d.%d\n", $prog_name,
 
929
          $Sane::VERSION, Sane->get_version;
 
930
 exit (0);
 
931
}
 
932
 
 
933
if ($help) {
 
934
 printf "Usage: %s [OPTION]...
 
935
 
 
936
Start image acquisition on a scanner device and write PNM image data to
 
937
standard output.
 
938
 
 
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;
 
944
 printf "
 
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";
 
951
 printf "
 
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";
 
959
 printf "
 
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";
 
967
}
 
968
 
 
969
if (! $devname) {
 
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'};
 
975
 }
 
976
 else {
 
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";
 
980
   exit (1);
 
981
  }
 
982
  if ($#device_list == -1) {
 
983
   print STDERR "$prog_name: no SANE devices found\n";
 
984
   exit (1);
 
985
  }
 
986
  $devname = $device_list[0]{name};
 
987
 }
 
988
}
 
989
 
 
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 =~ /^\//);
 
1001
 if ($help) {
 
1002
  undef $device;
 
1003
 }
 
1004
 else {
 
1005
  exit (1);
 
1006
 }
 
1007
}
 
1008
 
 
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.
 
1016
 for (@ARGV) {
 
1017
  $_ = '-l' if ($_ eq '-m');
 
1018
  $_ = '-t' if ($_ eq '-u');
 
1019
 }
 
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
 
1023
# this myself
 
1024
 for (@ARGV_old) {
 
1025
  my $ch;
 
1026
  if (/--(.*)/) {
 
1027
   $ch = $1;
 
1028
   my $i = index($ch, '=');
 
1029
   $ch = substr($ch, 0, $i) if ($i > -1);
 
1030
  }
 
1031
  elsif (/-(.)/) {
 
1032
   $ch = $1;
 
1033
  }
 
1034
  else {
 
1035
   next;
 
1036
  }
 
1037
  if (defined $options{$ch}) {
 
1038
   if ($ch eq 'x') {
 
1039
    $window_val_user[0] = 1;
 
1040
    ($window_val[0]) = parse_vector ($window_option[0], $options{x});
 
1041
   }
 
1042
   elsif ($ch eq 'y') {
 
1043
    $window_val_user[1] = 1;
 
1044
    ($window_val[1]) = parse_vector ($window_option[1], $options{y});
 
1045
   }
 
1046
   elsif ($ch eq 'l') { # tl-x
 
1047
    process_backend_option ($device, $window[2], $options{l});
 
1048
   }
 
1049
   elsif ($ch eq 't') { # tl-y
 
1050
    process_backend_option ($device, $window[3], $options{t});
 
1051
   }
 
1052
   else {
 
1053
    process_backend_option ($device, $option_number{$ch}, $options{$ch});
 
1054
   }
 
1055
  }
 
1056
 }
 
1057
 
 
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);
 
1064
   }
 
1065
   set_option ($device, $window[$index], $val);
 
1066
  }
 
1067
 }
 
1068
 if ($help) {
 
1069
  printf "\nOptions specific to device `%s':\n", $devname;
 
1070
 
 
1071
  for (my $i = 0; $i < $num_dev_options; ++$i) {
 
1072
   my $short_name = '';
 
1073
 
 
1074
   my $opt = 0;
 
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);
 
1079
    }
 
1080
   }
 
1081
   $opt = $device->get_option_descriptor ($i) if (!$opt);
 
1082
 
 
1083
   printf "  %s:\n", $opt->{title} if ($opt->{type} == SANE_TYPE_GROUP);
 
1084
 
 
1085
   next if (! ($opt->{cap} & SANE_CAP_SOFT_SELECT));
 
1086
 
 
1087
   print_option ($device, $i, $short_name);
 
1088
  }
 
1089
  print "\n" if ($num_dev_options);
 
1090
 }
 
1091
}
 
1092
 
 
1093
if ($help) {
 
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) {
 
1097
  my $column = 80;
 
1098
 
 
1099
  foreach (@device_list) {
 
1100
   if ($column + length ($_->{name}) + 1 >= 80) {
 
1101
    printf "\n    ";
 
1102
    $column = 4;
 
1103
   }
 
1104
   if ($column > 4) {
 
1105
    print ' ';
 
1106
    $column += 1;
 
1107
   }
 
1108
   print $_->{name};
 
1109
   $column += length ($_->{name});
 
1110
  }
 
1111
 }
 
1112
 print "\n";
 
1113
 exit (0);
 
1114
}
 
1115
 
 
1116
exit (0) if ($dont_scan);
 
1117
 
 
1118
$SIG{HUP} = \&sighandler;
 
1119
$SIG{INT} = \&sighandler;
 
1120
$SIG{PIPE} = \&sighandler;
 
1121
$SIG{TERM} = \&sighandler;
 
1122
 
 
1123
$batch_increment = 2 if ($batch_double);
 
1124
$batch = 1 if ($batch_count);
 
1125
 
 
1126
if ($test == 0) {
 
1127
 my $n = $batch_start_at;
 
1128
 $batch = 1 if (defined $format);
 
1129
 
 
1130
 if ($batch && (! defined($format) || $format eq '')) {
 
1131
#  if ($output_format == OUTPUT_TIFF) {
 
1132
#   $format = "out%d.tif";
 
1133
#  }
 
1134
#  else {
 
1135
   $format = "out%d.pnm";
 
1136
#  }
 
1137
 }
 
1138
 
 
1139
 printf STDERR
 
1140
            "Scanning %d pages, incrementing by %d, numbering from %d\n",
 
1141
            $batch_count, $batch_increment, $batch_start_at if ($batch);
 
1142
 
 
1143
 {do { # extra braces to get last to work.
 
1144
  my ($path, $fh);
 
1145
  $path = sprintf $format, $n if ($batch);    # format is NULL unless batch mode
 
1146
 
 
1147
  if ($batch) {
 
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";
 
1152
 
 
1153
    if (! defined(<STDIN>)) {
 
1154
     printf STDERR "Batch terminated, %d pages scanned\n",
 
1155
              ($n - $batch_increment);
 
1156
     last;    # get out of this loop
 
1157
    }
 
1158
   }
 
1159
   printf STDERR "Scanning page %d\n", $n;
 
1160
  }
 
1161
 
 
1162
  $device->start;
 
1163
  if ($Sane::STATUS != SANE_STATUS_GOOD) {
 
1164
   print STDERR "$prog_name: sane_start: $Sane::STATUS\n";
 
1165
   last;
 
1166
  }
 
1167
 
 
1168
  if ($batch && ! (open($fh, '>', $path) && STDOUT->fdopen($fh, '>'))) {
 
1169
   print STDERR "cannot open $path\n";
 
1170
   $device->cancel;
 
1171
   exit SANE_STATUS_ACCESS_DENIED;
 
1172
  }
 
1173
 
 
1174
  scan_it();
 
1175
  if ($batch) {
 
1176
   printf STDERR "Scanned page %d.", $n;
 
1177
   printf STDERR " (scanner status = %d)\n", $Sane::STATUS;
 
1178
  }
 
1179
 
 
1180
  if ($Sane::STATUS == SANE_STATUS_GOOD) {}
 
1181
  elsif ($Sane::STATUS == SANE_STATUS_EOF) {
 
1182
   $Sane::_status = SANE_STATUS_GOOD;
 
1183
  }
 
1184
  else {
 
1185
   if ($batch) {
 
1186
    close ($fh);
 
1187
    unlink ($path);
 
1188
   }
 
1189
   last;
 
1190
  }
 
1191
  $n += $batch_increment;
 
1192
 }
 
1193
 while (($batch
 
1194
         && ($batch_count == -1 || --$batch_count))
 
1195
         && SANE_STATUS_GOOD == $Sane::STATUS);}
 
1196
 
 
1197
 $device->cancel;
 
1198
}
 
1199
else {
 
1200
 $Sane::_status = test_it ();
 
1201
}
 
1202
 
 
1203
exit $Sane::STATUS;
 
1204
 
 
1205
__END__
 
1206
 
 
1207
=head1 NAME
 
1208
 
 
1209
scanimage \- scan an image
 
1210
 
 
1211
=head1 SYNOPSIS
 
1212
 
 
1213
B<scanimage>
 
1214
B<[ -d | --device-name>
 
1215
I<dev ]>
 
1216
B<[ --format>
 
1217
I<format ]>
 
1218
B<[ -i | --icc-profile>
 
1219
I<profile ]>
 
1220
B<[ -L | --list-devices ]>
 
1221
B<[ -f | --formatted-device-list >
 
1222
I<format ]>
 
1223
B<[ --batch >
 
1224
I<[= format ]]>
 
1225
B<[ --batch-start>
 
1226
I<start ]>
 
1227
B<[ --batch-count>
 
1228
I<count ]>
 
1229
B<[ --batch-increment>
 
1230
I<increment ]>
 
1231
B<[ --batch-double ]>
 
1232
B<[ --accept-md5-only ]>
 
1233
B<[ -p | --progress ]>
 
1234
B<[ -n | --dont-scan ]>
 
1235
B<[ -T | --test ]>
 
1236
B<[ -h | --help ]>
 
1237
B<[ -v | --verbose ]>
 
1238
B<[ -B | --buffersize ]>
 
1239
B<[ -V | --version ]>
 
1240
I<[ device-specific-options ]>
 
1241
 
 
1242
=head1 DESCRIPTION
 
1243
 
 
1244
B<scanimage>
 
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,
 
1248
B<scanimage>
 
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).
 
1253
B<scanimage>
 
1254
accesses image acquisition devices through the
 
1255
B<SANE>
 
1256
(Scanner Access Now Easy) interface and can thus support any device for which
 
1257
there exists a
 
1258
B<SANE>
 
1259
backend (try
 
1260
B<apropos>
 
1261
I<sane->
 
1262
to get a list of available backends).
 
1263
 
 
1264
=head1 EXAMPLES
 
1265
 
 
1266
To get a list of devices:
 
1267
 
 
1268
  scanimage -L
 
1269
 
 
1270
To scan with default settings to the file image.pnm:
 
1271
 
 
1272
  scanimage >image.pnm
 
1273
 
 
1274
To scan 100x100 mm to the file image.tiff (-x and -y may not be available with
 
1275
all devices):
 
1276
 
 
1277
  scanimage -x 100 -y 100 --format=tiff >image.tiff
 
1278
 
 
1279
To print all available options:
 
1280
 
 
1281
  scanimage -h
 
1282
 
 
1283
=head1 OPTIONS
 
1284
 
 
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).
 
1287
 
 
1288
The
 
1289
B<-d>
 
1290
or
 
1291
B<--device-name>
 
1292
options must be followed by a
 
1293
B<SANE>
 
1294
device-name like 
 
1295
I<` epson:/dev/sg0 '>
 
1296
or 
 
1297
I<` hp:/dev/usbscanner0 '.>
 
1298
A (partial) list of available devices can be obtained with the
 
1299
B<--list-devices>
 
1300
option (see below).  If no device-name is specified explicitly,
 
1301
B<scanimage>
 
1302
reads a device-name from the environment variable
 
1303
B<SANE_DEFAULT_DEVICE .>
 
1304
If this variable is not set, 
 
1305
B<scanimage>
 
1306
will attempt to open the first available device.
 
1307
 
 
1308
The
 
1309
B<--format >
 
1310
I<format>
 
1311
option selects how image data is written to standard output.
 
1312
I<format>
 
1313
can be
 
1314
B<pnm>
 
1315
or
 
1316
B<tiff.>
 
1317
If
 
1318
B<--format>
 
1319
is not used, PNM is written.
 
1320
 
 
1321
The
 
1322
B<-i>
 
1323
or
 
1324
B<--icc-profile>
 
1325
option is used to include an ICC profile into a TIFF file.
 
1326
 
 
1327
The
 
1328
B<-L>
 
1329
or
 
1330
B<--list-devices>
 
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
 
1334
in directory 
 
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.
 
1340
 
 
1341
The
 
1342
B<-f>
 
1343
or
 
1344
B<--formatted-device-list>
 
1345
option works similar to
 
1346
B<--list-devices ,>
 
1347
but requires a format string.
 
1348
B<scanimage>
 
1349
replaces the placeholders
 
1350
B<%d %v %m %t %i>
 
1351
with the device name, vendor name, model name, scanner type and an index
 
1352
number respectively. The command
 
1353
 
 
1354
=over
 
1355
 
 
1356
=item B<scanimage -f>
 
1357
I<\*(lq scanner number %i device %d is a %t, model %m, produced by %v \*(rq>
 
1358
 
 
1359
=back
 
1360
 
 
1361
will produce something like:
 
1362
 
 
1363
=over
 
1364
 
 
1365
=item scanner number 0  device sharp:/dev/sg1 is  a  flatbed scanner, model JX250
 
1366
SCSI, produced by SHARP
 
1367
 
 
1368
=back
 
1369
 
 
1370
The
 
1371
B<--batch*>
 
1372
options provide the features for scanning documents using document
 
1373
feeders.  
 
1374
B<--batch>
 
1375
I<[ format ]>
 
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
 
1378
I<format>
 
1379
is not specified, the default of out%d.pnm (or out%d.tif for --format tiff)
 
1380
will be used.  
 
1381
I<format>
 
1382
is given as a printf style string with one integer parameter.
 
1383
B<--batch-start>
 
1384
I<start>
 
1385
selects the page number to start naming files with. If this option is not
 
1386
given, the counter will start at 0.
 
1387
B<--batch-count>
 
1388
I<count>
 
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.
 
1393
With 
 
1394
B<--batch-increment>
 
1395
I<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
 
1399
aid this:
 
1400
B<--batch-double>
 
1401
will automatically set the increment to 2.
 
1402
B<--batch-prompt>
 
1403
will ask for pressing RETURN before scanning a page. This can be used for
 
1404
scanning multiple pages without an automatic document feeder.
 
1405
 
 
1406
The
 
1407
B<--accept-md5-only>
 
1408
option only accepts user authorization requests that support MD5 security. The
 
1409
B<SANE>
 
1410
network daemon
 
1411
B<( saned )>
 
1412
is capable of doing such requests. See
 
1413
B<saned (8).>
 
1414
 
 
1415
The
 
1416
B<-p>
 
1417
or
 
1418
B<--progress>
 
1419
option requests that
 
1420
B<scanimage>
 
1421
prints a progress counter. It shows how much image data of the current image has
 
1422
already been received by
 
1423
B<scanimage >
 
1424
(in percent).
 
1425
 
 
1426
The
 
1427
B<-n>
 
1428
or
 
1429
B<--dont-scan>
 
1430
option requests that
 
1431
B<scanimage>
 
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).
 
1435
 
 
1436
The
 
1437
B<-T>
 
1438
or
 
1439
B<--test>
 
1440
option requests that
 
1441
B<scanimage>
 
1442
performs a few simple sanity tests to make sure the backend works as
 
1443
defined by the
 
1444
B<SANE>
 
1445
API (in particular the
 
1446
B<sane_read>
 
1447
function is exercised by this test).
 
1448
 
 
1449
The
 
1450
B<-h>
 
1451
or
 
1452
B<--help>
 
1453
options request help information.  The information is printed on
 
1454
standard output and in this case, no attempt will be made to acquire
 
1455
an image.
 
1456
 
 
1457
The
 
1458
B<-v>
 
1459
or
 
1460
B<--verbose>
 
1461
options increase the verbosity of the operation of
 
1462
B<scanimage.>
 
1463
The option may be specified repeatedly, each time increasing the verbosity
 
1464
level.
 
1465
 
 
1466
The
 
1467
B<-B>
 
1468
or
 
1469
B<--buffersize>
 
1470
option changes the input buffersize that
 
1471
B<scanimage>
 
1472
uses from default 32*1024 to 1024*1024 kbytes.
 
1473
 
 
1474
The
 
1475
B<-V>
 
1476
or
 
1477
B<--version>
 
1478
option requests that
 
1479
B<scanimage>
 
1480
prints the program and package name, the version number of
 
1481
the
 
1482
B<SANE>
 
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
 
1486
B<DEBUG>
 
1487
variable for the dll backend can be used. Example: SANE_DEBUG_DLL=3 scanimage
 
1488
-L.
 
1489
 
 
1490
As you might imagine, much of the power of
 
1491
B<scanimage>
 
1492
comes from the fact that it can control any
 
1493
B<SANE>
 
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
 
1496
I<dev ,>
 
1497
invoke
 
1498
B<scanimage>
 
1499
via a command-line of the form:
 
1500
 
 
1501
=over
 
1502
 
 
1503
=item B<scanimage --help --device-name>
 
1504
I<dev>
 
1505
 
 
1506
=back
 
1507
 
 
1508
The documentation for the device-specific options printed by
 
1509
B<--help>
 
1510
is best explained with a few examples:
 
1511
 
 
1512
 -l 0..218mm [0]
 
1513
    Top-left x position of scan area.
 
1514
 
 
1515
=over
 
1516
 
 
1517
=item The description above shows that option
 
1518
B<-l>
 
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).
 
1523
 
 
1524
=back
 
1525
 
 
1526
 --brightness -100..100% [0]
 
1527
    Controls the brightness of the acquired image.
 
1528
 
 
1529
=over
 
1530
 
 
1531
=item The description above shows that option
 
1532
B<--brightness>
 
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
 
1535
percent.
 
1536
 
 
1537
=back
 
1538
 
 
1539
 --default-enhancements
 
1540
    Set default values for enhancement controls.
 
1541
 
 
1542
=over
 
1543
 
 
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
 
1549
B<--brightness>
 
1550
option, the option-pair
 
1551
B<--brightness 50 --default-enhancements>
 
1552
would effectively be a no-op.
 
1553
 
 
1554
=back
 
1555
 
 
1556
 --mode Lineart|Gray|Color [Gray]
 
1557
    Selects the scan mode (e.g., lineart or color).
 
1558
 
 
1559
=over
 
1560
 
 
1561
=item The description above shows that option
 
1562
B<--mode>
 
1563
accepts an argument that must be one of the strings
 
1564
B<Lineart ,>
 
1565
B<Gray ,>
 
1566
or
 
1567
B<Color .>
 
1568
The value in the square bracket indicates that the option is currently
 
1569
set to
 
1570
B<Gray .>
 
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
 
1574
B<--mode col>
 
1575
is identical to
 
1576
B<"--mode Color" .>
 
1577
 
 
1578
=back
 
1579
 
 
1580
 --custom-gamma[=(yes|no)] [inactive]
 
1581
    Determines whether a builtin or a custom gamma-table
 
1582
    should be used.
 
1583
 
 
1584
=over
 
1585
 
 
1586
=item The description above shows that option
 
1587
B<--custom-gamma>
 
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
 
1594
B<--custom-gamma>
 
1595
table might be active only when a grayscale or color scan-mode has
 
1596
been requested.
 
1597
 
 
1598
=back
 
1599
 
 
1600
Note that the
 
1601
B<--help>
 
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
 
1605
with the
 
1606
B<--help>
 
1607
option.  For example, the command-line:
 
1608
 
 
1609
B< scanimage --help --mode>
 
1610
I<color>
 
1611
 
 
1612
would print the option settings that are in effect when the color-mode
 
1613
is selected.
 
1614
 
 
1615
=back
 
1616
 
 
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).
 
1621
 
 
1622
=over
 
1623
 
 
1624
=item The description above shows that option
 
1625
B<--gamma-table>
 
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".
 
1634
The program
 
1635
B<gamma4scanimage>
 
1636
can be used to generate such gamma tables (see 
 
1637
B<gamma4scanimage (1)>
 
1638
for details).
 
1639
 
 
1640
=back
 
1641
 
 
1642
 --filename <string> [/tmp/input.ppm]
 
1643
    The filename of the image to be loaded.
 
1644
 
 
1645
=over
 
1646
 
 
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
 
1650
filename 
 
1651
B</tmp/input.ppm .>
 
1652
 
 
1653
=back
 
1654
 
 
1655
=head1 ENVIRONMENT
 
1656
 
 
1657
=over
 
1658
 
 
1659
=item B<SANE_DEFAULT_DEVICE>
 
1660
 
 
1661
The default device-name.
 
1662
 
 
1663
=back
 
1664
 
 
1665
=head1 FILES
 
1666
 
 
1667
=over
 
1668
 
 
1669
=item I</caehome/ra28145/etc/sane.d>
 
1670
 
 
1671
This directory holds various configuration files.  For details, please
 
1672
refer to the manual pages listed below.
 
1673
 
 
1674
=item I<~/.sane/pass>
 
1675
 
 
1676
This file contains lines of the form
 
1677
 
 
1678
=item user:password:resource
 
1679
 
 
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
 
1684
to 127 characters.
 
1685
 
 
1686
=back
 
1687
 
 
1688
=head1 "SEE ALSO"
 
1689
 
 
1690
B<sane (7),>
 
1691
B<gamma4scanimage (1),>
 
1692
B<xscanimage (1),>
 
1693
B<xcam(1) ,>
 
1694
B<xsane(1) ,>
 
1695
B<scanadf (1),>
 
1696
B<sane-dll (5),>
 
1697
B<sane-net (5),>
 
1698
B<sane-"backendname" (5)>
 
1699
 
 
1700
=head1 AUTHOR
 
1701
 
 
1702
Transliterated from the C original by Jeffrey Ratcliffe.
 
1703
 
 
1704
=head1 BUGS
 
1705
 
 
1706
All the bugs of scanimage and much, much more.