File: scanimage.pl

package info (click to toggle)
libsane-perl 0.03-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 216 kB
  • ctags: 40
  • sloc: perl: 2,127; makefile: 16; ansic: 5
file content (1706 lines) | stat: -rwxr-xr-x 46,301 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
#!/usr/bin/perl

use warnings;
use strict;
use Sane;
use Data::Dumper;
use Getopt::Long qw(:config no_ignore_case pass_through);
use File::Basename;
use IO::Handle;

#$Sane::DEBUG = 1;

my (%options, @window_val_user, @window_option, @window_val,
    @window, $num_dev_options, $device, $format, $devname, %option_number);
my $verbose = 0;
my $help = 0;
my $test = 0;
my $batch = 0;
my $batch_start_at = 1;
my $batch_count = -1;
my $batch_increment = 1;
my $buffer_size = (32 * 1024);	# default size
my $tl_x = 0;
my $tl_y = 0;
my $br_x = 0;
my $br_y = 0;
my $w_x = 0;
my $h_y = 0;
my $resolution_optind = -1;
my $resolution_value = 0;
my $progress = 0;
my $batch_double = 0;
my $batch_prompt = 0;
my $dont_scan = 0;
my $prog_name = basename($0);
my @args = (\%options, 'd|device-name=s' => \$devname,
                       'L|list-devices',
                       'f|formatted-device-list=s',
                       'b|batch:s' => \$format,
                       'batch-start=i' => \$batch_start_at,
                       'batch-count=i' => \$batch_count,
                       'batch-increment=i' => \$batch_increment,
                       'batch-double' => \$batch_double,
                       'batch-prompt' => \$batch_prompt,
                       'p|progress' => \$progress,
                       'n|dont-scan' => \$dont_scan,
                       'T|test' => \$test,
                       'h|help' => \$help,
                       'v|verbose+' => \$verbose,
                       'B|buffer-size' => \$buffer_size,
                       'V|version');


sub sighandler {
 my $signum = shift;

 my $first_time = SANE_TRUE;

 if ($device) {
  print STDERR "$prog_name: received signal $signum\n";
  if ($first_time) {
   $first_time = SANE_FALSE;
   print STDERR "$prog_name: trying to stop scanner\n";
   $device->cancel;
  }
  else {
   print STDERR "$prog_name: aborting\n";
   _exit (0);
  }
 }
}


sub print_unit {
 my ($unit) = @_;

 if ($unit == SANE_UNIT_PIXEL) {
  print "pel";
 }
 elsif ($unit == SANE_UNIT_BIT) {
  print "bit";
 }
 elsif ($unit == SANE_UNIT_MM) {
  print "mm";
 }
 elsif ($unit == SANE_UNIT_DPI) {
  print "dpi";
 }
 elsif ($unit == SANE_UNIT_PERCENT) {
  print "%";
 }
 elsif ($unit == SANE_UNIT_MICROSECOND) {
  print "us";
 }
}


sub print_option {
 my ($device, $opt_num, $short_name) = @_;

 my $not_first = SANE_FALSE;
 my $maxwindow = 0;

 my $opt = $device->get_option_descriptor ($opt_num);

 if ($short_name) {
  printf "    -%s", $short_name;
 }
 else {
  printf "    --%s", $opt->{name};
 }

 if ($opt->{type} == SANE_TYPE_BOOL) {
  print "[=(";
  print "auto|" if ($opt->{cap} & SANE_CAP_AUTOMATIC);
  print "yes|no)]";
 }
 elsif ($opt->{type} != SANE_TYPE_BUTTON) {
  print ' ';
  if ($opt->{cap} & SANE_CAP_AUTOMATIC) {
   print "auto|";
   $not_first = SANE_TRUE;
  }
  if ($opt->{constraint_type} == SANE_CONSTRAINT_NONE) {
   if ($opt->{type} == SANE_TYPE_INT) {
    print "<int>";
   }
   elsif ($opt->{type} == SANE_TYPE_FIXED) {
    print "<float>";
   }
   elsif ($opt->{type} == SANE_TYPE_STRING) {
    print "<string>";
   }
   print ",..." if ($opt->{max_values} > 1);
  }
  elsif ($opt->{constraint_type} == SANE_CONSTRAINT_RANGE) {
   my $format = "%g..%g";
   $format = "%d..%d" if ($opt->{type} == SANE_TYPE_INT);
   if ($opt->{name} eq SANE_NAME_SCAN_BR_X) {
    $maxwindow = $opt->{constraint}{max} - $tl_x;
    printf $format, $opt->{constraint}{min}, $maxwindow;
   }
   elsif ($opt->{name} eq SANE_NAME_SCAN_BR_Y) {
    $maxwindow = $opt->{constraint}{max} - $tl_y;
    printf $format, $opt->{constraint}{min}, $maxwindow;
   }
   else {
    printf $format, $opt->{constraint}{min}, $opt->{constraint}{max};
   }
   print_unit ($opt->{unit});
   print ",..." if ($opt->{max_values} > 1);
   print " (in steps of $opt->{constraint}{quant})"
    if ($opt->{constraint}{quant});
  }
  elsif ($opt->{constraint_type} == SANE_CONSTRAINT_STRING_LIST
                      or $opt->{constraint_type} == SANE_CONSTRAINT_WORD_LIST) {
   for (my $i = 0; $i < @{$opt->{constraint}}; ++$i) {
    print '|' if ($i > 0);

    print $opt->{constraint}[$i];
   }
   if ($opt->{constraint_type} == SANE_CONSTRAINT_WORD_LIST) {
    print_unit ($opt->{unit});
    print ",..." if ($opt->{max_values} > 1);
   }
  }
 }
 if ($opt->{max_values} == 1) {
  # print current option value
  if (! ($opt->{cap} & SANE_CAP_INACTIVE)) {
   my $val = $device->get_option ($opt_num);
   print " [";
   if ($opt->{type} == SANE_TYPE_BOOL) {
    print ($val ? "yes" : "no");
   }
   elsif ($opt->{type} == SANE_TYPE_INT or $opt->{type} == SANE_TYPE_FIXED) {
    my $format = "%g";
    $format = "%d" if ($opt->{type} == SANE_TYPE_INT);
    if ($opt->{name} eq SANE_NAME_SCAN_TL_X) {
     $tl_x = $val;
     printf $format, $tl_x;
    }
    elsif ($opt->{name} eq SANE_NAME_SCAN_TL_Y) {
     $tl_y = $val;
     printf $format, $tl_y;
    }
    elsif ($opt->{name} eq SANE_NAME_SCAN_BR_X) {
     $br_x = $val;
     $w_x = $br_x - $tl_x;
     printf $format, $w_x;
    }
    elsif ($opt->{name} eq SANE_NAME_SCAN_BR_Y) {
     $br_y = $val;
     $h_y = $br_y - $tl_y;
     printf $format, $h_y;
    }
    else {
     printf $format, $val;
    }
   }
   elsif ($opt->{type} == SANE_TYPE_STRING) {
    print $val;
   }
   print ']';
  }
 }

 print " [inactive]" if ($opt->{cap} & SANE_CAP_INACTIVE);

 print "\n        ";

 if ($short_name eq 'x') {
  print "Width of scan-area.";
 }
 elsif ($short_name eq 'y') {
  print "Height of scan-area.";
 }
 else {
  my $column = 8;
  my $last_break = 0;
  my $start = 0;
  for (my $pos = 0; $pos < length($opt->{desc}); ++$pos) {
   ++$column;
   $last_break = $pos if (substr($opt->{desc}, $pos, 1) eq ' ');
   if ($column >= 79 and $last_break) {
    print substr($opt->{desc}, $start++, 1) while ($start < $last_break);
    $start = $last_break + 1;   # skip blank
    print "\n        ";
    $column = 8 + $pos - $start;
   }
  }
  print substr($opt->{desc}, $start++, 1) while ($start < length($opt->{desc}));
 }
 print "\n";
}


# A scalar has the following syntax:
#
#     V [ U ]
#
#   V is the value of the scalar.  It is either an integer or a
#   floating point number, depending on the option type.
#
#   U is an optional unit.  If not specified, the default unit is used.
#   The following table lists which units are supported depending on
#   what the option's default unit is:
#
#     Option's unit:	Allowed units:
#
#     SANE_UNIT_NONE:
#     SANE_UNIT_PIXEL:	pel
#     SANE_UNIT_BIT:	b (bit), B (byte)
#     SANE_UNIT_MM:	mm (millimeter), cm (centimeter), in or " (inches),
#     SANE_UNIT_DPI:	dpi
#     SANE_UNIT_PERCENT:	%
#     SANE_UNIT_MICROSECOND:	us

sub parse_scalar {
 my ($opt, $str) = @_;

 my ($v, $unit);
 if ($str =~ /^(\d*\.?\d*)(cm|mm|in|\"|b|B|dpi|%|us)?/) {
  $v = $1;
  $unit = $2;
  $unit = '' if not defined $unit;
 }
 else {
  print STDERR
             "$prog_name: option --$opt->{name}: bad option value (rest of option: $str)\n";
  exit (1);
 }

 if ($opt->{unit} == SANE_UNIT_BIT) {
  $v *= 8 if ($unit eq 'B');
 }
 elsif ($opt->{unit} == SANE_UNIT_MM) {
  if ($unit eq 'cm') {
   $v *= 10;
  }
  elsif ($unit eq 'in') {
   $v *= 25.4;
  }
 }
 return $v, substr($str, length($v) + length($unit), length($str));
}


# A vector has the following syntax:
#
#     [ '[' I ']' ] S { [','|'-'] [ '[' I ']' S }
#
#   The number in brackets (I), if present, determines the index of the
#   vector element to be set next.  If I is not present, the value of
#   last index used plus 1 is used.  The first index value used is 0
#   unless I is present.
#
#   S is a scalar value as defined by parse_scalar().
#
#   If two consecutive value specs are separated by a comma (,) their
#   values are set independently.  If they are separated by a dash (-),
#   they define the endpoints of a line and all vector values between
#   the two endpoints are set according to the value of the
#   interpolated line.  For example, [0]15-[255]15 defines a vector of
#   256 elements whose value is 15.  Similarly, [0]0-[255]255 defines a
#   vector of 256 elements whose value starts at 0 and increases to
#   255.

sub parse_vector {
 my ($opt, $str) = @_;

 my $index = -1;
 my $prev_value = 0;
 my $prev_index = 0;
 my $separator = '';
 my (@vector, $value);
 do {
  if ($str =~ /^\[/) {
   if ($str =~ /^\[(\d*\.?\d*)\]/) {
    $index = $1;
   }
   else {
    print STDERR
             "$prog_name: option --$opt->{name}: closing bracket missing "
                ."(rest of option: $str)\n";
    exit (1);
   }
  }
  else {
   ++$index;
  }

  if ($index < 0 or $index >= length($str)) {
   printf STDERR
             "$prog_name: option --$opt->{name}: index $index out of range [0..%d]\n",
               length($str);
   exit (1);
  }

  # read value
  ($value, $str) = parse_scalar ($opt, $str);

  if ($str ne '' and $str !~ /^[-,]/) {
   print STDERR
             "$prog_name: option --$opt->{name}: illegal separator (rest of option: $str)\n";
   exit (1);
  }

  # store value:
  $vector[$index] = $value;
  if ($separator eq '-') {
   # interpolate
   my $v = $prev_value;
   my $slope = ($value - $v) / ($index - $prev_index);

   for (my $i = $prev_index + 1; $i < $index; ++$i) {
    $v += $slope;
    $vector[$i] = $v;
   }
  }

  $prev_index = $index;
  $prev_value = $value;
  $separator = substr($str, 0, 1);
 }
 while ($separator eq ',' || $separator eq '-');

 if ($verbose > 2) {
  print STDERR "$prog_name: value for --$opt->{name} is: ";
  for (@vector) {
   print STDERR "$_ ";
  }
  print STDERR "\n";
 }
 
 return @vector;
}


sub fetch_options {
 my $device = shift;

# We got a device, find out how many options it has:
 $num_dev_options = $device->get_option(0);
 if ($Sane::STATUS != SANE_STATUS_GOOD) {
  print STDERR "$prog_name: unable to determine option count\n";
  exit (1);
 }

 for (my $i = 0; $i < $num_dev_options; ++$i) {
  my $opt = $device->get_option_descriptor ($i);

  next if (! ($opt->{cap} & SANE_CAP_SOFT_SELECT));

  $option_number{$opt->{name}} = $i;

  # Look for scan resolution
  $resolution_optind = $i
   if (($opt->{type} == SANE_TYPE_FIXED || $opt->{type} == SANE_TYPE_INT)
      and ($opt->{unit} == SANE_UNIT_DPI)
      and ($opt->{name} eq SANE_NAME_SCAN_RESOLUTION));

# Keep track of top-left corner options (if they exist at
# all) and replace the bottom-right corner options by a
# width/height option (if they exist at all).
  if (($opt->{type} == SANE_TYPE_FIXED || $opt->{type} == SANE_TYPE_INT)
      and ($opt->{unit} == SANE_UNIT_MM || $opt->{unit} == SANE_UNIT_PIXEL)) {
   if ($opt->{name} eq SANE_NAME_SCAN_TL_X) {
    $window[2] = $i;
    $opt->{name} = 'l';
   }
   elsif ($opt->{name} eq SANE_NAME_SCAN_TL_Y) {
    $window[3] = $i;
    $opt->{name} = 't';
   }
   elsif ($opt->{name} eq SANE_NAME_SCAN_BR_X) {
    $window[0] = $i;
    $opt->{name} = 'x';
    $window_option[0] = $opt;
    $window_option[0]->{title} = 'Scan width';
    $window_option[0]->{desc} = 'Width of scanning area.';
    $window_val[0] = $device->get_option ($i)
     if (!$window_val_user[0]);
   }
   elsif ($opt->{name} eq SANE_NAME_SCAN_BR_Y) {
    $window[1] = $i;
    $opt->{name} = 'y';
    $window_option[1] = $opt;
    $window_option[1]->{title} = 'Scan height';
    $window_option[1]->{desc} = 'Height of scanning area.';
    $window_val[1] = $device->get_option ($i)
     if (!$window_val_user[1]);
   }
  }

  if ($opt->{type} == SANE_TYPE_BOOL) {
   push @args, "$opt->{name}:s";
  }
  elsif ($opt->{type} == SANE_TYPE_BUTTON) {
   push @args, $opt->{name};
  }
  else {
   push @args, "$opt->{name}=s";
  }
 }

# Initialize width & height options based on backend default
# values for top-left x/y and bottom-right x/y:
 for (my $i = 0; $i < 2; ++$i) {
  if ($window[$i] and $window[$i + 2] and !$window_val_user[$i]) {
   my $pos = $device->get_option ($window[$i + 2]);
   $window_val[$i] = $window_val[$i] - $pos if (defined $pos);
  }
 }
}


sub set_option {
 my ($device, $optnum, $value) = @_;

 my $opt = $device->get_option_descriptor ($optnum);
 if ($opt and ($opt->{cap} & SANE_CAP_INACTIVE)) {
  print STDERR
             "$prog_name: ignored request to set inactive option $opt->{name}\n"
   if ($verbose > 0);
  return;
 }

 my $info = $device->set_option($optnum, $value);
 if ($Sane::STATUS != SANE_STATUS_GOOD) {
  print STDERR
            "$prog_name: setting of option --$opt->{name} failed ($Sane::STATUS)\n";
  exit (1);
 }

 if (($info & SANE_INFO_INEXACT) and $opt->{max_values} == 1) {
  my $orig = $value;
  $value = $device->get_option($optnum);
  if ($opt->{type} == SANE_TYPE_INT) {
   printf STDERR
     "$prog_name: rounded value of $opt->{name} from %d to %d\n", $orig, $value;
  }
  elsif ($opt->{type} == SANE_TYPE_FIXED) {
   printf STDERR
     "$prog_name: rounded value of $opt->{name} from %g to %g\n", $orig, $value;
  }
 }
 fetch_options ($device) if ($info & SANE_INFO_RELOAD_OPTIONS);
}


sub process_backend_option {
 my ($device, $optnum, $optarg) = @_;

 my $opt = $device->get_option_descriptor ($optnum);

 if ($opt and ($opt->{cap} & SANE_CAP_INACTIVE)) {
  print STDERR "$prog_name: attempted to set inactive option $opt->{name}\n";
  exit (1);
 }

 if (($opt->{cap} & SANE_CAP_AUTOMATIC) and $optarg and $optarg =~ /^auto$/i) {
  $device->set_auto($optnum);
  if ($Sane::STATUS != SANE_STATUS_GOOD) {
   printf STDERR "$prog_name: failed to set option --$opt->{name} to automatic ($Sane::STATUS)\n";
   exit (1);
  }
  return;
 }

 my $value;
 if ($opt->{type} == SANE_TYPE_BOOL) {
  $value = 1;                # no argument means option is set
  if ($optarg) {
   if ($optarg =~ /^yes$/i) {
    $value = 1;
   }
   elsif ($optarg =~ /^no$/i) {
    $value = 0;
   }
   else {
    printf STDERR "$prog_name: option --$opt->{name}: bad option value `$optarg'\n";
    exit (1);
   }
  }
 }
 elsif ($opt->{type} == SANE_TYPE_INT or $opt->{type} == SANE_TYPE_FIXED) {
  my @vector = parse_vector ($opt, $optarg);
  $value = \@vector;
 }
 elsif ($opt->{type} == SANE_TYPE_STRING) {
  $value = $optarg;
 }
 elsif ($opt->{type} == SANE_TYPE_BUTTON) {
  $value = 0;                # value doesn't matter
 }
 else {
  printf STDERR "$prog_name: duh, got unknown option type $opt->{type}\n";
  return;
 }
 set_option ($device, $optnum, $value);
}


sub write_pnm_header {
 my ($format, $width, $height, $depth) = @_;

# The netpbm-package does not define raw image data with maxval > 255.
# But writing maxval 65535 for 16bit data gives at least a chance
# to read the image.

 if ($format == SANE_FRAME_RED or $format == SANE_FRAME_GREEN or
                      $format == SANE_FRAME_BLUE or $format == SANE_FRAME_RGB) {
  printf "P6\n# SANE data follows\n%d %d\n%d\n", $width, $height,
	      ($depth <= 8) ? 255 : 65535;
 }
 else {
  if ($depth == 1) {
   printf "P4\n# SANE data follows\n%d %d\n", $width, $height;
  }
  else {
   printf "P5\n# SANE data follows\n%d %d\n%d\n", $width, $height,
		($depth <= 8) ? 255 : 65535;
  }
 }
}


sub scan_it {
 my $first_frame = 1;
 my $offset = 0;
 my $must_buffer = 0;
 my $min = 0xff;
 my $max = 0;
 my %image;
 my @format_name = (
   "gray", "RGB", "red", "green", "blue"
 );
 my $total_bytes = 0;
 my $hang_over = -1;

 my $parm;
 {do { # extra braces to get last to work.
  if (!$first_frame) {
   $device->start;
   if ($Sane::STATUS != SANE_STATUS_GOOD) {
    printf STDERR "$prog_name: sane_start: $Sane::STATUS\n";
    goto cleanup;
   }
  }

  $parm = $device->get_parameters;
  if ($Sane::STATUS != SANE_STATUS_GOOD) {
   printf STDERR "$prog_name: sane_get_parameters: $Sane::STATUS\n";
   goto cleanup;
  }

  if ($verbose) {
   if ($first_frame) {
    if ($parm->{lines} >= 0) {
     printf STDERR "$prog_name: scanning image of size %dx%d pixels at "
               ."%d bits/pixel\n",
               $parm->{pixels_per_line}, $parm->{lines},
               8 * $parm->{bytes_per_line} / $parm->{pixels_per_line};
    }
    else {
      printf STDERR "$prog_name: scanning image %d pixels wide and "
               ."variable height at %d bits/pixel\n",
               $parm->{pixels_per_line},
               8 * $parm->{bytes_per_line} / $parm->{pixels_per_line};
    }
   }

   printf STDERR "$prog_name: acquiring %s frame\n",
    $parm->{format} <= SANE_FRAME_BLUE ? $format_name[$parm->{format}]:"Unknown";
  }

  if ($first_frame) {
   if ($parm->{format} == SANE_FRAME_RED
       or $parm->{format} == SANE_FRAME_GREEN
       or $parm->{format} == SANE_FRAME_BLUE) {
    die unless ($parm->{depth} == 8);
    $must_buffer = 1;
    $offset = $parm->{format} - SANE_FRAME_RED;
   }
   elsif ($parm->{format} == SANE_FRAME_RGB) {
    die unless (($parm->{depth} == 8) || ($parm->{depth} == 16));
   }
   if ($parm->{format} == SANE_FRAME_RGB or $parm->{format} == SANE_FRAME_GRAY) {
    die unless (($parm->{depth} == 1) || ($parm->{depth} == 8)
            || ($parm->{depth} == 16));
    if ($parm->{lines} < 0) {
     $must_buffer = 1;
     $offset = 0;
    }
    else {
#     if ($output_format == OUTPUT_TIFF) {
#       sanei_write_tiff_header ($parm->{format},
#                                $parm->{pixels_per_line}, $parm->{lines},
#                                $parm->{depth}, $resolution_value,
#                                icc_profile);
#     else {
       write_pnm_header ($parm->{format}, $parm->{pixels_per_line},
                         $parm->{lines}, $parm->{depth});
#      }
    }
   }
  }
  else {
   die unless ($parm->{format} >= SANE_FRAME_RED
           && $parm->{format} <= SANE_FRAME_BLUE);
   $offset = $parm->{format} - SANE_FRAME_RED;
   $image{x} = $image{y} = 0;
  }
  my $hundred_percent = $parm->{bytes_per_line} * $parm->{lines}
    * (($parm->{format} == SANE_FRAME_RGB || $parm->{format} == SANE_FRAME_GRAY) ? 1:3);

  while (1) {
   my ($buffer, $len) = $device->read ($buffer_size);
   $total_bytes += $len;
   my $progr = (($total_bytes * 100.) / $hundred_percent);
   $progr = 100. if ($progr > 100.);
   printf STDERR "Progress: %3.1f%%\r", $progr if ($progress);

   if ($Sane::STATUS != SANE_STATUS_GOOD) {
    printf STDERR "$prog_name: min/max graylevel value = %d/%d\n", $min, $max
     if ($verbose && $parm->{depth} == 8);
    if ($Sane::STATUS != SANE_STATUS_EOF) {
     print STDERR "$prog_name: sane_read: $Sane::STATUS\n";
     return;
    }
    last;
   }

   if ($must_buffer) {
    # We're either scanning a multi-frame image or the
    # scanner doesn't know what the eventual image height
    # will be (common for hand-held scanners).  In either
    # case, we need to buffer all data before we can write
    # the image
    if ($parm->{format} == SANE_FRAME_RED
        or $parm->{format} == SANE_FRAME_GREEN
        or $parm->{format} == SANE_FRAME_BLUE) {
     for (my $i = 0; $i < $len; ++$i) {
      $image{data}[$offset + 3 * $i] = substr($buffer, $i, 1);
     }
     $offset += 3 * $len;
    }
    elsif ($parm->{format} == SANE_FRAME_RGB
           or $parm->{format} == SANE_FRAME_GRAY) {
     for (my $i = 0; $i < $len; ++$i) {
      $image{data}[$offset + $i] = substr($buffer, $i, 1);
     }
     $offset += $len;
    }
   }
   else { # ! must_buffer
#    if (($output_format == OUTPUT_TIFF) || ($parm->{depth} != 16)) {
#     print $buffer;
#    }
#    else {
##if !defined(WORDS_BIGENDIAN)
#     my $start = 0;
#
#     # check if we have saved one byte from the last sane_read
#     if ($hang_over > -1) {
#      if ($len > 0) {
#       print $buffer;
#       $buffer = $hang_over.$buffer;
#       $hang_over = -1;
#       $start = 1;
#      }
#     }
#     # now do the byte-swapping
#     $buffer = reverse $buffer;
#
#     # check if we have an odd number of bytes
#     if ((($len - $start) % 2) != 0) {
#      $hang_over = substr($buffer, $len - 1, 1);
#      $len--;
#     }
##endif
     print $buffer;
#    }
   }

   if ($verbose && $parm->{depth} == 8) {
    for (split(//, $buffer)) {
     my $c = ord;
     if ($c >= $max) {
      $max = $c;
     }
     elsif ($c < $min) {
      $min = $c;
     }
    }
   }

  }
  $first_frame = 0;
 }
 while (!$parm->{last_frame});}

 if ($must_buffer) {
  if ($parm->{lines} > 0) {
   $image{height} = $parm->{lines};
  }
  else {
   $image{height} = @{$image{data}}/$parm->{pixels_per_line};
   $image{height} /= 3 if ($parm->{format} == SANE_FRAME_RED
                         or $parm->{format} == SANE_FRAME_GREEN
                         or $parm->{format} == SANE_FRAME_BLUE);
  }
#  if ($output_format == OUTPUT_TIFF) {
#   sanei_write_tiff_header ($parm->{format}, $parm->{pixels_per_line},
#                             $parm->{lines}, $parm->{depth}, $resolution_value,
#                             icc_profile);
#  }
#  else {
   write_pnm_header ($parm->{format}, $parm->{pixels_per_line}, $image{height}, $parm->{depth});
#  }
#  if (($output_format == OUTPUT_TIFF) || ($image{Bpp} == 1)
#      || ($image{Bpp} == 3)) {
#   print $image{data};
#  }
#  else { # $image{Bpp} == 2 or $image{Bpp} == 6 assumed
##if !defined(WORDS_BIGENDIAN)
#   for (my $i = 0; $i < $image{Bpp} * $image{height} * $image{width}; $i += 2) {
#    my $LSB = $image{data}[$i];
#    $image{data}[$i] = $image{data}[$i + 1];
#    $image{data}[$i + 1] = $LSB;
#   }
##endif
   for (@{$image{data}}) {print;}
#  }
 }

 # flush the output buffer
 STDOUT->flush;

cleanup:
 my $expected_bytes = $parm->{bytes_per_line} * $parm->{lines} *
   (($parm->{format} == SANE_FRAME_RGB
     || $parm->{format} == SANE_FRAME_GRAY) ? 1 : 3);
 $expected_bytes = 0 if ($parm->{lines} < 0);
 if ($total_bytes > $expected_bytes && $expected_bytes != 0) {
  printf STDERR
              "%s: WARNING: read more data than announced by backend "
              ."(%u/%u)\n", $prog_name, $total_bytes, $expected_bytes;
 }
 elsif ($verbose) {
  printf STDERR "%s: read %u bytes in total\n", $prog_name, $total_bytes;
 }
 return;
}


sub pass_fail {
 my ($max, $len, $buffer) = @_;

 if ($Sane::STATUS != SANE_STATUS_GOOD) {
  print STDERR "FAIL Error: $Sane::STATUS\n";
 }
 elsif ($len < length($buffer)) {
  printf STDERR "FAIL Cheat: %d bytes\n", length($buffer);
 }
 elsif ($len > $max) {
  printf STDERR "FAIL Overflow: %d bytes\n", $len;
 }
 elsif ($len == 0) {
  print STDERR "FAIL No data\n";
 }
 else {
  print STDERR "PASS\n";
 }
}


sub test_it {
 my %image;
 my @format_name = (
   "gray", "RGB", "red", "green", "blue"
 );

 $device->start;
 if ($Sane::STATUS != SANE_STATUS_GOOD) {
  print STDERR "$prog_name: sane_start: $Sane::STATUS\n";
  goto cleanup;
 }

 my $parm = $device->get_parameters;
 if ($Sane::STATUS != SANE_STATUS_GOOD) {
  print STDERR "$prog_name: sane_get_parameters: $Sane::STATUS\n";
  goto cleanup;
 }

 if ($parm->{lines} >= 0) {
  printf STDERR "$prog_name: scanning image of size %dx%d pixels at "
            ."%d bits/pixel\n", $parm->{pixels_per_line}, $parm->{lines},
            8 * $parm->{bytes_per_line} / $parm->{pixels_per_line};
 }
 else {
  printf STDERR "$prog_name: scanning image %d pixels wide and "
            ."variable height at %d bits/pixel\n",$parm->{pixels_per_line},
            8 * $parm->{bytes_per_line} / $parm->{pixels_per_line};
 }
 printf STDERR "$prog_name: acquiring %s frame, %d bits/sample\n",
          $parm->{format} <= SANE_FRAME_BLUE ? $format_name[$parm->{format}]:"Unknown",
          $parm->{depth};

 printf STDERR "$prog_name: reading one scanline, %d bytes...\t",
          $parm->{bytes_per_line};
 ($image{data}, my $len) = $device->read ($parm->{bytes_per_line});
 pass_fail ($parm->{bytes_per_line}, $len, $image{data});
 goto cleanup if ($Sane::STATUS != SANE_STATUS_GOOD);

 print STDERR "$prog_name: reading one byte...\t\t";
 ($image{data}, $len) = $device->read (1);
 pass_fail (1, $len, $image{data});
 goto cleanup if ($Sane::STATUS != SANE_STATUS_GOOD);

 my $i;
 for ($i = 2; $i < $parm->{bytes_per_line} * 2; $i *= 2) {
  printf STDERR "$prog_name: stepped read, %d bytes... \t", $i;
  ($image{data}, $len) = $device->read ($i);
  pass_fail ($i, $len, $image{data});
  goto cleanup if ($Sane::STATUS != SANE_STATUS_GOOD);
 }

 for ($i /= 2; $i > 2; $i /= 2) {
  printf STDERR "$prog_name: stepped read, %d bytes... \t", $i - 1;
  ($image{data}, $len) = $device->read ($i - 1);
  pass_fail ($i - 1, $len, $image{data});
  goto cleanup if ($Sane::STATUS != SANE_STATUS_GOOD);
 }

cleanup:
 $device->cancel;
 return;
}

# There seems to be a bug in Getopt::Long 2.37 where l is treated as L whilst
# l is not in @args. Therefore the workaround is to rename l to m for the first
# scan and back to l for the second.
for (@ARGV) {
 $_ = '-m' if ($_ eq '-l');
 $_ = '-u' if ($_ eq '-t');
}
# make a first pass through the options with error printing and argument
# permutation disabled:
GetOptions (@args);

if (defined($options{L}) or defined($options{f})) {
 my @device_list = Sane->get_devices;
 if ($Sane::STATUS != SANE_STATUS_GOOD) {
  print STDERR "$prog_name: sane_get_devices() failed: $Sane::STATUS\n";
  exit (1);
 }
 if (defined($options{L})) {
  foreach (@device_list) {
   printf "device `%s' is a %s %s %s\n", $_->{name}, $_->{vendor},
					  $_->{model}, $_->{type};
  }
  printf "\nNo scanners were identified. If you were expecting "
          ."something different,\ncheck that the scanner is plugged "
          ."in, turned on and detected by the\nsane-find-scanner tool "
          ."(if appropriate). Please read the documentation\nwhich came "
          ."with this software (README, FAQ, manpages).\n"
   if ($#device_list == -1);
 }
 else {
  for (my $i = 0; $i < @device_list; $i++) {
   my $format = $options{f};
   $format =~ s/%d/$device_list[$i]->{name}/g;
   $format =~ s/%v/$device_list[$i]->{vendor}/g;
   $format =~ s/%m/$device_list[$i]->{model}/g;
   $format =~ s/%t/$device_list[$i]->{type}/g;
   $format =~ s/%i/$i/g;
   print $format;
  }
 }
 printf "default device is `%s'\n", $ENV{'SANE_DEFAULT_DEVICE'}
  if (defined($ENV{'SANE_DEFAULT_DEVICE'}));
 exit (0);
}

if (defined($options{V})) {
 printf "%s %s; backend version %d.%d.%d\n", $prog_name,
	  $Sane::VERSION, Sane->get_version;
 exit (0);
}

if ($help) {
 printf "Usage: %s [OPTION]...

Start image acquisition on a scanner device and write PNM image data to
standard output.

Parameters are separated by a blank from single-character options (e.g.
-d epson) and by a \"=\" from multi-character options (e.g. --device-name=epson).
-d, --device-name=DEVICE   use a given scanner device (e.g. hp:/dev/scanner)
    --format=pnm|tiff      file format of output file
-i, --icc-profile=PROFILE  include this ICC profile into TIFF file", $prog_name;
 printf "
-L, --list-devices         show available scanner devices
-f, --formatted-device-list=FORMAT similar to -L, but the FORMAT of the output
                           can be specified: %%d (device name), %%v (vendor),
                           %%m (model), %%t (type), and %%i (index number)
-b, --batch[=FORMAT]       working in batch mode, FORMAT is `out%%d.pnm' or
                           `out%%d.tif' by default depending on --format";
 printf "
    --batch-start=#        page number to start naming files with
    --batch-count=#        how many pages to scan in batch mode
    --batch-increment=#    increase number in filename by an amount of #
    --batch-double         increment page number by two for 2sided originals
                           being scanned in a single sided scanner
    --batch-prompt         ask for pressing a key before scanning a page
    --accept-md5-only      only accept authorization requests using md5";
 printf "
-p, --progress             print progress messages
-n, --dont-scan            only set options, don't actually scan
-T, --test                 test backend thoroughly
-h, --help                 display this help message and exit
-v, --verbose              give even more status messages
-B, --buffer-size          change default input buffersize
-V, --version              print version information\n";
}

if (! $devname) {
# If no device name was specified explicitly, we look at the
# environment variable SANE_DEFAULT_DEVICE.  If this variable
# is not set, we open the first device we find (if any):
 if (defined($ENV{'SANE_DEFAULT_DEVICE'})) {
  $devname = $ENV{'SANE_DEFAULT_DEVICE'};
 }
 else {
  my @device_list = Sane->get_devices;
  if ($Sane::STATUS != SANE_STATUS_GOOD) {
   print STDERR "$prog_name: sane_get_devices() failed: $Sane::STATUS\n";
   exit (1);
  }
  if ($#device_list == -1) {
   print STDERR "$prog_name: no SANE devices found\n";
   exit (1);
  }
  $devname = $device_list[0]{name};
 }
}

$device = Sane::Device->open($devname);
if ($Sane::STATUS != SANE_STATUS_GOOD) {
 print STDERR "$prog_name: open of device $devname failed: $Sane::STATUS\n";
 print STDERR "\nYou seem to have specified a UNIX device name, "
            ."or filename instead of selecting\nthe SANE scanner or "
            ."image acquisition device you want to use. As an example,\n"
            ."you might want \"epson:/dev/sg0\" or "
            ."\"hp:/dev/usbscanner0\". If any supported\ndevices are "
            ."installed in your system, you should be able to see a "
            ."list with\n\"$prog_name --list-devices\".\n"
  if ($devname =~ /^\//);
 if ($help) {
  undef $device;
 }
 else {
  exit (1);
 }
}

if (defined($device)) {
 fetch_options($device);
# re-enable error printing and arg permutation
 Getopt::Long::Configure('no_pass_through');
# There seems to be a bug in Getopt::Long 2.37 where l is treated as L whilst
# l is not in @args. Therefore the workaround is to rename l to m for the first
# scan and back to l for the second.
 for (@ARGV) {
  $_ = '-l' if ($_ eq '-m');
  $_ = '-t' if ($_ eq '-u');
 }
 my @ARGV_old = @ARGV;
 exit 1 if (! GetOptions (@args));
# As it isn't possible to get the argument order from Getopt::Long 2.37, do
# this myself
 for (@ARGV_old) {
  my $ch;
  if (/--(.*)/) {
   $ch = $1;
   my $i = index($ch, '=');
   $ch = substr($ch, 0, $i) if ($i > -1);
  }
  elsif (/-(.)/) {
   $ch = $1;
  }
  else {
   next;
  }
  if (defined $options{$ch}) {
   if ($ch eq 'x') {
    $window_val_user[0] = 1;
    ($window_val[0]) = parse_vector ($window_option[0], $options{x});
   }
   elsif ($ch eq 'y') {
    $window_val_user[1] = 1;
    ($window_val[1]) = parse_vector ($window_option[1], $options{y});
   }
   elsif ($ch eq 'l') { # tl-x
    process_backend_option ($device, $window[2], $options{l});
   }
   elsif ($ch eq 't') { # tl-y
    process_backend_option ($device, $window[3], $options{t});
   }
   else {
    process_backend_option ($device, $option_number{$ch}, $options{$ch});
   }
  }
 }

 for (my $index = 0; $index < 2; ++$index) {
  if ($window[$index] and defined($window_val[$index])) {
   my $val = $window_val[$index] - 1;
   if ($window[$index + 2]) {
    my $pos = $device->get_option ($window[$index + 2]);
    $val = $pos + $window_val[$index] if (defined $pos);
   }
   set_option ($device, $window[$index], $val);
  }
 }
 if ($help) {
  printf "\nOptions specific to device `%s':\n", $devname;

  for (my $i = 0; $i < $num_dev_options; ++$i) {
   my $short_name = '';

   my $opt = 0;
   for (my $j = 0; $j < 4; ++$j) {
    if ($i == $window[$j]) {
     $short_name = substr("xylt", $j, 1);
     $opt = $window_option[$j] if ($j < 2);
    }
   }
   $opt = $device->get_option_descriptor ($i) if (!$opt);

   printf "  %s:\n", $opt->{title} if ($opt->{type} == SANE_TYPE_GROUP);

   next if (! ($opt->{cap} & SANE_CAP_SOFT_SELECT));

   print_option ($device, $i, $short_name);
  }
  print "\n" if ($num_dev_options);
 }
}

if ($help) {
 printf "Type ``$prog_name --help -d DEVICE'' to get list of all options for DEVICE.\n\nList of available devices:";
 my @device_list = Sane->get_devices;
 if ($Sane::STATUS == SANE_STATUS_GOOD) {
  my $column = 80;

  foreach (@device_list) {
   if ($column + length ($_->{name}) + 1 >= 80) {
    printf "\n    ";
    $column = 4;
   }
   if ($column > 4) {
    print ' ';
    $column += 1;
   }
   print $_->{name};
   $column += length ($_->{name});
  }
 }
 print "\n";
 exit (0);
}

exit (0) if ($dont_scan);

$SIG{HUP} = \&sighandler;
$SIG{INT} = \&sighandler;
$SIG{PIPE} = \&sighandler;
$SIG{TERM} = \&sighandler;

$batch_increment = 2 if ($batch_double);
$batch = 1 if ($batch_count);

if ($test == 0) {
 my $n = $batch_start_at;
 $batch = 1 if (defined $format);

 if ($batch && (! defined($format) || $format eq '')) {
#  if ($output_format == OUTPUT_TIFF) {
#   $format = "out%d.tif";
#  }
#  else {
   $format = "out%d.pnm";
#  }
 }

 printf STDERR
            "Scanning %d pages, incrementing by %d, numbering from %d\n",
            $batch_count, $batch_increment, $batch_start_at if ($batch);

 {do { # extra braces to get last to work.
  my ($path, $fh);
  $path = sprintf $format, $n if ($batch);    # format is NULL unless batch mode

  if ($batch) {
   if ($batch_prompt) {
    printf STDERR "Place document no. %d on the scanner.\n", $n;
    printf STDERR "Press <RETURN> to continue.\n";
    printf STDERR "Press Ctrl + D to terminate.\n";

    if (! defined(<STDIN>)) {
     printf STDERR "Batch terminated, %d pages scanned\n",
              ($n - $batch_increment);
     last;    # get out of this loop
    }
   }
   printf STDERR "Scanning page %d\n", $n;
  }

  $device->start;
  if ($Sane::STATUS != SANE_STATUS_GOOD) {
   print STDERR "$prog_name: sane_start: $Sane::STATUS\n";
   last;
  }

  if ($batch && ! (open($fh, '>', $path) && STDOUT->fdopen($fh, '>'))) {
   print STDERR "cannot open $path\n";
   $device->cancel;
   exit SANE_STATUS_ACCESS_DENIED;
  }

  scan_it();
  if ($batch) {
   printf STDERR "Scanned page %d.", $n;
   printf STDERR " (scanner status = %d)\n", $Sane::STATUS;
  }

  if ($Sane::STATUS == SANE_STATUS_GOOD) {}
  elsif ($Sane::STATUS == SANE_STATUS_EOF) {
   $Sane::_status = SANE_STATUS_GOOD;
  }
  else {
   if ($batch) {
    close ($fh);
    unlink ($path);
   }
   last;
  }
  $n += $batch_increment;
 }
 while (($batch
         && ($batch_count == -1 || --$batch_count))
         && SANE_STATUS_GOOD == $Sane::STATUS);}

 $device->cancel;
}
else {
 $Sane::_status = test_it ();
}

exit $Sane::STATUS;

__END__

=head1 NAME

scanimage \- scan an image

=head1 SYNOPSIS

B<scanimage>
B<[ -d | --device-name>
I<dev ]>
B<[ --format>
I<format ]>
B<[ -i | --icc-profile>
I<profile ]>
B<[ -L | --list-devices ]>
B<[ -f | --formatted-device-list >
I<format ]>
B<[ --batch >
I<[= format ]]>
B<[ --batch-start>
I<start ]>
B<[ --batch-count>
I<count ]>
B<[ --batch-increment>
I<increment ]>
B<[ --batch-double ]>
B<[ --accept-md5-only ]>
B<[ -p | --progress ]>
B<[ -n | --dont-scan ]>
B<[ -T | --test ]>
B<[ -h | --help ]>
B<[ -v | --verbose ]>
B<[ -B | --buffersize ]>
B<[ -V | --version ]>
I<[ device-specific-options ]>

=head1 DESCRIPTION

B<scanimage>
is a command-line interface to control image acquisition devices such
as flatbed scanners or cameras.  The device is controlled via
command-line options.  After command-line processing,
B<scanimage>
normally proceeds to acquire an image.  The image data is written to
standard output in one of the PNM (portable aNyMaP) formats (PBM for
black-and-white images, PGM for grayscale images, and PPM for color
images) or in TIFF (black-and-white, grayscale or color).
B<scanimage>
accesses image acquisition devices through the
B<SANE>
(Scanner Access Now Easy) interface and can thus support any device for which
there exists a
B<SANE>
backend (try
B<apropos>
I<sane->
to get a list of available backends).

=head1 EXAMPLES

To get a list of devices:

  scanimage -L

To scan with default settings to the file image.pnm:

  scanimage >image.pnm

To scan 100x100 mm to the file image.tiff (-x and -y may not be available with
all devices):

  scanimage -x 100 -y 100 --format=tiff >image.tiff

To print all available options:

  scanimage -h

=head1 OPTIONS

Parameters are separated by a blank from single-character options (e.g.
-d epson) and by a "=" from multi-character options (e.g. --device-name=epson).

The
B<-d>
or
B<--device-name>
options must be followed by a
B<SANE>
device-name like 
I<` epson:/dev/sg0 '>
or 
I<` hp:/dev/usbscanner0 '.>
A (partial) list of available devices can be obtained with the
B<--list-devices>
option (see below).  If no device-name is specified explicitly,
B<scanimage>
reads a device-name from the environment variable
B<SANE_DEFAULT_DEVICE .>
If this variable is not set, 
B<scanimage>
will attempt to open the first available device.

The
B<--format >
I<format>
option selects how image data is written to standard output.
I<format>
can be
B<pnm>
or
B<tiff.>
If
B<--format>
is not used, PNM is written.

The
B<-i>
or
B<--icc-profile>
option is used to include an ICC profile into a TIFF file.

The
B<-L>
or
B<--list-devices>
option requests a (partial) list of devices that are available.  The
list is not complete since some devices may be available, but are not
listed in any of the configuration files (which are typically stored
in directory 
I</caehome/ra28145/etc/sane.d ).>
This is particularly the case when accessing scanners through the network.  If
a device is not listed in a configuration file, the only way to access it is
by its full device name.  You may need to consult your system administrator to
find out the names of such devices.

The
B<-f>
or
B<--formatted-device-list>
option works similar to
B<--list-devices ,>
but requires a format string.
B<scanimage>
replaces the placeholders
B<%d %v %m %t %i>
with the device name, vendor name, model name, scanner type and an index
number respectively. The command

=over

=item B<scanimage -f>
I<\*(lq scanner number %i device %d is a %t, model %m, produced by %v \*(rq>

=back

will produce something like:

=over

=item scanner number 0  device sharp:/dev/sg1 is  a  flatbed scanner, model JX250
SCSI, produced by SHARP

=back

The
B<--batch*>
options provide the features for scanning documents using document
feeders.  
B<--batch>
I<[ format ]>
is used to specify the format of the filename that each page will be written
to.  Each page is written out to a single file.  If
I<format>
is not specified, the default of out%d.pnm (or out%d.tif for --format tiff)
will be used.  
I<format>
is given as a printf style string with one integer parameter.
B<--batch-start>
I<start>
selects the page number to start naming files with. If this option is not
given, the counter will start at 0.
B<--batch-count>
I<count>
specifies the number of pages to attempt to scan.  If not given, 
scanimage will continue scanning until the scanner returns a state
other than OK.  Not all scanners with document feeders signal when the
ADF is empty, use this command to work around them.
With 
B<--batch-increment>
I<increment>
you can change the amount that the number in the filename is incremented
by.  Generally this is used when you are scanning double-sided documents
on a single-sided document feeder.  A specific command is provided to
aid this:
B<--batch-double>
will automatically set the increment to 2.
B<--batch-prompt>
will ask for pressing RETURN before scanning a page. This can be used for
scanning multiple pages without an automatic document feeder.

The
B<--accept-md5-only>
option only accepts user authorization requests that support MD5 security. The
B<SANE>
network daemon
B<( saned )>
is capable of doing such requests. See
B<saned (8).>

The
B<-p>
or
B<--progress>
option requests that
B<scanimage>
prints a progress counter. It shows how much image data of the current image has
already been received by
B<scanimage >
(in percent).

The
B<-n>
or
B<--dont-scan>
option requests that
B<scanimage>
only sets the options provided by the user but doesn't actually perform a
scan. This option can be used to e.g. turn off the scanner's lamp (if
supported by the backend).

The
B<-T>
or
B<--test>
option requests that
B<scanimage>
performs a few simple sanity tests to make sure the backend works as
defined by the
B<SANE>
API (in particular the
B<sane_read>
function is exercised by this test).

The
B<-h>
or
B<--help>
options request help information.  The information is printed on
standard output and in this case, no attempt will be made to acquire
an image.

The
B<-v>
or
B<--verbose>
options increase the verbosity of the operation of
B<scanimage.>
The option may be specified repeatedly, each time increasing the verbosity
level.

The
B<-B>
or
B<--buffersize>
option changes the input buffersize that
B<scanimage>
uses from default 32*1024 to 1024*1024 kbytes.

The
B<-V>
or
B<--version>
option requests that
B<scanimage>
prints the program and package name, the version number of
the
B<SANE>
distribution that it came with and the version of the backend that it
loads. Usually that's the dll backend. If more information about the version
numbers of the backends are necessary, the
B<DEBUG>
variable for the dll backend can be used. Example: SANE_DEBUG_DLL=3 scanimage
-L.

As you might imagine, much of the power of
B<scanimage>
comes from the fact that it can control any
B<SANE>
backend.  Thus, the exact set of command-line options depends on the
capabilities of the selected device.  To see the options for a device named
I<dev ,>
invoke
B<scanimage>
via a command-line of the form:

=over

=item B<scanimage --help --device-name>
I<dev>

=back

The documentation for the device-specific options printed by
B<--help>
is best explained with a few examples:

 -l 0..218mm [0]
    Top-left x position of scan area.

=over

=item The description above shows that option
B<-l>
expects an option value in the range from 0 to 218 mm.  The
value in square brackets indicates that the current option value is 0
mm. Most backends provide similar geometry options for top-left y position (-t),
width (-x) and height of scan-area (-y).

=back

 --brightness -100..100% [0]
    Controls the brightness of the acquired image.

=over

=item The description above shows that option
B<--brightness>
expects an option value in the range from -100 to 100 percent.  The
value in square brackets indicates that the current option value is 0
percent.

=back

 --default-enhancements
    Set default values for enhancement controls.

=over

=item The description above shows that option
B<--default-enhancements>
has no option value.  It should be thought of as having an immediate
effect at the point of the command-line at which it appears.  For
example, since this option resets the
B<--brightness>
option, the option-pair
B<--brightness 50 --default-enhancements>
would effectively be a no-op.

=back

 --mode Lineart|Gray|Color [Gray]
    Selects the scan mode (e.g., lineart or color).

=over

=item The description above shows that option
B<--mode>
accepts an argument that must be one of the strings
B<Lineart ,>
B<Gray ,>
or
B<Color .>
The value in the square bracket indicates that the option is currently
set to
B<Gray .>
For convenience, it is legal to abbreviate the string values as long as
they remain unique.  Also, the case of the spelling doesn't matter.  For
example, option setting
B<--mode col>
is identical to
B<"--mode Color" .>

=back

 --custom-gamma[=(yes|no)] [inactive]
    Determines whether a builtin or a custom gamma-table
    should be used.

=over

=item The description above shows that option
B<--custom-gamma>
expects either no option value, a "yes" string, or a "no" string.
Specifying the option with no value is equivalent to specifying "yes".
The value in square-brackets indicates that the option is not
currently active.  That is, attempting to set the option would result
in an error message.  The set of available options typically depends
on the settings of other options.  For example, the
B<--custom-gamma>
table might be active only when a grayscale or color scan-mode has
been requested.

=back

Note that the
B<--help>
option is processed only after all other options have been processed.
This makes it possible to see the option settings for a particular
mode by specifying the appropriate mode-options along
with the
B<--help>
option.  For example, the command-line:

B< scanimage --help --mode>
I<color>

would print the option settings that are in effect when the color-mode
is selected.

=back

 --gamma-table 0..255,...
    Gamma-correction table.  In color mode this option
    equally affects the red, green, and blue channels
    simultaneously (i.e., it is an intensity gamma table).

=over

=item The description above shows that option
B<--gamma-table>
expects zero or more values in the range 0 to 255.  For example, a
legal value for this option would be "3,4,5,6,7,8,9,10,11,12".  Since
it's cumbersome to specify long vectors in this form, the same can be
expressed by the abbreviated form "[0]3-[9]12".  What this means is
that the first vector element is set to 3, the 9-th element is set to
12 and the values in between are interpolated linearly.  Of course, it
is possible to specify multiple such linear segments.  For example,
"[0]3-[2]3-[6]7,[7]10-[9]6" is equivalent to "3,3,3,4,5,6,7,10,8,6".
The program
B<gamma4scanimage>
can be used to generate such gamma tables (see 
B<gamma4scanimage (1)>
for details).

=back

 --filename <string> [/tmp/input.ppm]
    The filename of the image to be loaded.

=over

=item The description above is an example of an option that takes an
arbitrary string value (which happens to be a filename).  Again,
the value in brackets show that the option is current set to the
filename 
B</tmp/input.ppm .>

=back

=head1 ENVIRONMENT

=over

=item B<SANE_DEFAULT_DEVICE>

The default device-name.

=back

=head1 FILES

=over

=item I</caehome/ra28145/etc/sane.d>

This directory holds various configuration files.  For details, please
refer to the manual pages listed below.

=item I<~/.sane/pass>

This file contains lines of the form

=item user:password:resource

scanimage uses this information to answer user authorization requests
automatically. The file must have 0600 permissions or stricter. You should
use this file in conjunction with the --accept-md5-only option to avoid
server-side attacks. The resource may contain any character but is limited
to 127 characters.

=back

=head1 "SEE ALSO"

B<sane (7),>
B<gamma4scanimage (1),>
B<xscanimage (1),>
B<xcam(1) ,>
B<xsane(1) ,>
B<scanadf (1),>
B<sane-dll (5),>
B<sane-net (5),>
B<sane-"backendname" (5)>

=head1 AUTHOR

Transliterated from the C original by Jeffrey Ratcliffe.

=head1 BUGS

All the bugs of scanimage and much, much more.