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
|
/* saneadf - a SANE front end for document scanning
based on
bnhscan by tummy.com and
scanimage by Andreas Beck and David Mosberger
Copyright (C) 1999 Tom Martone
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef _AIX
# include <lalloca.h> /* MUST come first for AIX! */
#endif
#include "sane/config.h"
#include <lalloca.h>
#include <assert.h>
#include <getopt.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "sane/sane.h"
#include "sane/sanei.h"
#include "sane/saneopts.h"
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
#ifndef sane_isbasicframe
#define SANE_FRAME_TEXT 10
#define SANE_FRAME_JPEG 11
#define SANE_FRAME_G31D 12
#define SANE_FRAME_G32D 13
#define SANE_FRAME_G42D 14
#define sane_strframe(f) ( (f) == SANE_FRAME_GRAY ? "gray" : \
(f) == SANE_FRAME_RGB ? "RGB" : \
(f) == SANE_FRAME_RED ? "red" : \
(f) == SANE_FRAME_GREEN ? "green" : \
(f) == SANE_FRAME_BLUE ? "blue" : \
(f) == SANE_FRAME_TEXT ? "text" : \
(f) == SANE_FRAME_JPEG ? "jpeg" : \
(f) == SANE_FRAME_G31D ? "g31d" : \
(f) == SANE_FRAME_G32D ? "g32d" : \
(f) == SANE_FRAME_G42D ? "g42d" : \
"unknown" )
#define sane_isbasicframe(f) ( (f) == SANE_FRAME_GRAY || \
(f) == SANE_FRAME_RGB || \
(f) == SANE_FRAME_RED || \
(f) == SANE_FRAME_GREEN || \
(f) == SANE_FRAME_BLUE )
#endif
#ifndef HAVE_ATEXIT
# define atexit(func) on_exit(func, 0) /* works for SunOS, at least */
#endif
typedef struct
{
u_char *data;
int Bpp; /* bytes/pixel */
int width;
int height;
int x;
int y;
}
Image;
static struct option basic_options[] =
{
{"device-name", required_argument, NULL, 'd'},
{"list-devices", no_argument, NULL, 'L'},
{"help", no_argument, NULL, 'h'},
{"verbose", no_argument, NULL, 'v'},
{"version", no_argument, NULL, 'V'},
{"no-overwrite", no_argument, NULL, 'N'},
{ "output-file", required_argument, 0, 'o' },
{ "start-count", required_argument, 0, 's' },
{ "end-count", required_argument, 0, 'e' },
{ "scan-script", required_argument, 0, 'S' },
{ "script-wait", no_argument, 0, 128 },
{ "raw", no_argument, 0, 'r' },
{0, }
};
#define BASE_OPTSTRING "d:hLvVNTo:s:e:S:r"
#define STRIP_HEIGHT 256 /* # lines we increment image height */
static struct option * all_options;
static int option_number_len;
static int * option_number;
static SANE_Handle device;
static int verbose;
static int help;
static const char * prog_name;
static SANE_Option_Descriptor window_option[2];
static int window[4];
static int resolution_opt = -1, x_resolution_opt = -1, y_resolution_opt = -1;
static SANE_Word window_val[2];
static int window_val_user[2]; /* is width/height user-specified? */
static const char usage[] = "Usage: %s [OPTION]...\n\
\n\
Start image acquisition on a scanner device and write image data to\n\
output files.\n\
\n\
[ -d | --device-name <device> ] use a given scanner device.\n\
[ -h | --help ] display this help message and exit.\n\
[ -L | --list-devices ] show available scanner devices.\n\
[ -v | --verbose ] give even more status messages.\n\
[ -V | --version ] print version information.\n\
[ -N | --no-overwrite ] don't overwrite existing files.\n\
\n\
[ -o | --output-file <name> ] name of file to write image data\n\
(%%d replacement in output file name).\n\
[ -S | --scan-script <name> ] name of script to run after every scan.\n\
[ --script-wait ] wait for scripts to finish before exit\n\
[ -s | --start-count <num> ] page count of first scanned image.\n\
[ -e | --end-count <num> ] last page number to scan.\n\
[ -r | --raw ] write raw image data to file.\n";
static RETSIGTYPE
sighandler (int signum)
{
if (device)
{
fprintf (stderr, "%s: stopping scanner...\n", prog_name);
sane_cancel (device);
}
}
static void
print_unit (SANE_Unit unit)
{
switch (unit)
{
case SANE_UNIT_NONE: break;
case SANE_UNIT_PIXEL: fputs ("pel", stdout); break;
case SANE_UNIT_BIT: fputs ("bit", stdout); break;
case SANE_UNIT_MM: fputs ("mm", stdout); break;
case SANE_UNIT_DPI: fputs ("dpi", stdout); break;
case SANE_UNIT_PERCENT: fputc ('%', stdout); break;
case SANE_UNIT_MICROSECOND: fputs ("us", stdout); break;
}
}
static void
print_option (SANE_Device *device, int opt_num, char short_name)
{
const char *str, *last_break, *start;
const SANE_Option_Descriptor *opt;
SANE_Bool not_first = SANE_FALSE;
int i, column;
opt = sane_get_option_descriptor (device, opt_num);
if (short_name)
printf (" -%c", short_name);
else
printf (" --%s", opt->name);
if (opt->type == SANE_TYPE_BOOL)
{
fputs ("[=(", stdout);
if (opt->cap & SANE_CAP_AUTOMATIC)
fputs ("auto|", stdout);
fputs ("yes|no)]", stdout);
}
else if (opt->type != SANE_TYPE_BUTTON)
{
fputc (' ', stdout);
if (opt->cap & SANE_CAP_AUTOMATIC)
{
fputs ("auto|", stdout);
not_first = SANE_TRUE;
}
switch (opt->constraint_type)
{
case SANE_CONSTRAINT_NONE:
switch (opt->type)
{
case SANE_TYPE_INT: fputs ("<int>", stdout); break;
case SANE_TYPE_FIXED: fputs ("<float>", stdout); break;
case SANE_TYPE_STRING: fputs ("<string>", stdout); break;
default:
break;
}
if (opt->type != SANE_TYPE_STRING && opt->size > sizeof (SANE_Word))
fputs (",...", stdout);
break;
case SANE_CONSTRAINT_RANGE:
if (opt->type == SANE_TYPE_INT)
{
printf ("%d..%d",
opt->constraint.range->min, opt->constraint.range->max);
print_unit (opt->unit);
if (opt->size > sizeof (SANE_Word))
fputs (",...", stdout);
if (opt->constraint.range->quant)
printf (" (in steps of %d)",
opt->constraint.range->quant);
}
else
{
printf ("%g..%g",
SANE_UNFIX(opt->constraint.range->min),
SANE_UNFIX(opt->constraint.range->max));
print_unit (opt->unit);
if (opt->size > sizeof (SANE_Word))
fputs (",...", stdout);
if (opt->constraint.range->quant)
printf (" (in steps of %g)",
SANE_UNFIX(opt->constraint.range->quant));
}
break;
case SANE_CONSTRAINT_WORD_LIST:
for (i = 0; i < opt->constraint.word_list[0]; ++i)
{
if (not_first)
fputc ('|', stdout);
not_first = SANE_TRUE;
if (opt->type == SANE_TYPE_INT)
printf ("%d", opt->constraint.word_list[i + 1]);
else
printf ("%g",
SANE_UNFIX(opt->constraint.word_list[i + 1]));
}
print_unit (opt->unit);
if (opt->size > sizeof (SANE_Word))
fputs (",...", stdout);
break;
case SANE_CONSTRAINT_STRING_LIST:
for (i = 0; opt->constraint.string_list[i]; ++i)
{
if (i > 0)
fputc ('|', stdout);
fputs (opt->constraint.string_list[i], stdout);
}
break;
}
}
if (opt->type == SANE_TYPE_STRING ||
opt->type == SANE_TYPE_BOOL ||
opt->type == SANE_TYPE_INT ||
opt->type == SANE_TYPE_FIXED)
{
/* print current option value */
fputs (" [", stdout);
if (SANE_OPTION_IS_ACTIVE(opt->cap))
{
void * val = alloca (opt->size);
sane_control_option (device, opt_num, SANE_ACTION_GET_VALUE, val, 0);
switch (opt->type)
{
case SANE_TYPE_BOOL:
fputs (*(SANE_Bool *)val ? "yes" : "no", stdout);
break;
case SANE_TYPE_INT:
printf ("%d", *(SANE_Int *)val);
break;
case SANE_TYPE_FIXED:
printf ("%g", SANE_UNFIX(*(SANE_Fixed *)val));
break;
case SANE_TYPE_STRING:
fputs ((char *) val, stdout);
break;
default:
break;
}
}
else
fputs ("inactive", stdout);
fputc (']', stdout);
}
fputs ("\n ", stdout);
switch (short_name)
{
case 'x':
fputs ("Width of scan-area.", stdout);
break;
case 'y':
fputs ("Height of scan-area.", stdout);
break;
default:
column = 8; last_break = 0; start = opt->desc;
for (str = opt->desc; *str; ++str)
{
++column;
if (*str == ' ')
last_break = str;
if (column >= 79 && last_break)
{
while (start < last_break)
fputc (*start++, stdout);
start = last_break + 1; /* skip blank */
fputs ("\n ", stdout);
column = 8 + (str - start);
}
}
while (*start)
fputc (*start++, stdout);
}
fputc ('\n', stdout);
}
/* 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_PERCENT: us
*/
static const char *
parse_scalar (const SANE_Option_Descriptor * opt, const char * str,
SANE_Word * value)
{
char * end;
double v;
if (opt->type == SANE_TYPE_FIXED)
v = strtod (str, &end) * (1 << SANE_FIXED_SCALE_SHIFT);
else
v = strtol (str, &end, 10);
if (str == end)
{
fprintf (stderr,
"%s: option --%s: bad option value (rest of option: %s)\n",
prog_name, opt->name, str);
exit (1);
}
str = end;
switch (opt->unit)
{
case SANE_UNIT_NONE:
case SANE_UNIT_PIXEL:
break;
case SANE_UNIT_BIT:
if (*str == 'b' || *str == 'B')
{
if (*str++ == 'B')
v *= 8;
}
break;
case SANE_UNIT_MM:
if (str[0] == '\0')
v *= 1.0; /* default to mm */
else if (strcmp (str, "mm") == 0)
str += sizeof ("mm") - 1;
else if (strcmp (str, "cm") == 0)
{
str += sizeof ("cm") - 1;
v *= 10.0;
}
else if (strcmp (str, "in") == 0 || *str == '"')
{
if (*str++ != '"')
++str;
v *= 25.4; /* 25.4 mm/inch */
}
else
{
fprintf (stderr,
"%s: option --%s: illegal unit (rest of option: %s)\n",
prog_name, opt->name, str);
return 0;
}
break;
case SANE_UNIT_DPI:
if (strcmp (str, "dpi") == 0)
str += sizeof ("dpi") - 1;
break;
case SANE_UNIT_PERCENT:
if (*str == '%')
++str;
break;
case SANE_UNIT_MICROSECOND:
if (strcmp (str, "us") == 0)
str += sizeof ("us") - 1;
break;
}
*value = v + 0.5;
return 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. */
static void
parse_vector (const SANE_Option_Descriptor * opt, const char * str,
SANE_Word * vector, size_t vector_length)
{
SANE_Word value, prev_value = 0;
int index = -1, prev_index = 0;
char * end, separator = '\0';
/* initialize vector to all zeroes: */
memset (vector, 0, vector_length * sizeof (SANE_Word));
do {
if (*str == '[')
{
/* read index */
index = strtol (++str, &end, 10);
if (str == end || *end != ']')
{
fprintf (stderr, "%s: option --%s: closing bracket missing "
"(rest of option: %s)\n", prog_name, opt->name, str);
exit (1);
}
str = end + 1;
}
else
++index;
if (index < 0 || index >= vector_length)
{
fprintf (stderr, "%s: option --%s: index %d out of range [0..%ld]\n",
prog_name, opt->name, index, (long) vector_length - 1);
exit (1);
}
/* read value */
str = parse_scalar (opt, str, &value);
if (!str)
exit (1);
if (*str && *str != '-' && *str != ',')
{
fprintf (stderr,
"%s: option --%s: illegal separator (rest of option: %s)\n",
prog_name, opt->name, str);
exit (1);
}
/* store value: */
vector[index] = value;
if (separator == '-')
{
/* interpolate */
double v, slope;
int i;
v = (double) prev_value;
slope = ((double) value - v) / (index - prev_index);
for (i = prev_index + 1; i < index; ++i)
{
v += slope;
vector[i] = (SANE_Word) v;
}
}
prev_index = index;
prev_value = value;
separator = *str++;
} while (separator == ',' || separator == '-');
if (verbose > 2)
{
int i;
fprintf (stderr, "%s: value for --%s is: ", prog_name, opt->name);
for (i = 0; i < vector_length; ++i)
if (opt->type == SANE_TYPE_FIXED)
fprintf (stderr, "%g ", SANE_UNFIX(vector[i]));
else
fprintf (stderr, "%d ", vector[i]);
fputc ('\n', stderr);
}
}
void
fetch_options (SANE_Device * device)
{
const SANE_Option_Descriptor * opt;
SANE_Int num_dev_options;
int i, option_count;
/* and now build the full table of long options: */
sane_control_option (device, 0, SANE_ACTION_GET_VALUE, &num_dev_options, 0);
option_count = 0;
for (i = 0; i < num_dev_options; ++i)
{
opt = sane_get_option_descriptor (device, i);
if (!SANE_OPTION_IS_SETTABLE (opt->cap))
continue;
option_number[option_count] = i;
all_options[option_count].name = (char *) opt->name;
all_options[option_count].flag = 0;
all_options[option_count].val = 0;
if (opt->type == SANE_TYPE_BOOL)
all_options[option_count].has_arg = optional_argument;
else if (opt->type == SANE_TYPE_BUTTON)
all_options[option_count].has_arg = no_argument;
else
all_options[option_count].has_arg = required_argument;
/* Keep track of resolution options */
if (strcmp (opt->name, SANE_NAME_SCAN_RESOLUTION) == 0)
{
resolution_opt = i;
}
else if (strcmp (opt->name, SANE_NAME_SCAN_X_RESOLUTION) == 0)
{
x_resolution_opt = i;
}
if (strcmp (opt->name, SANE_NAME_SCAN_Y_RESOLUTION) == 0)
{
y_resolution_opt = i;
}
/* 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)
&& opt->size == sizeof (SANE_Int)
&& (opt->unit == SANE_UNIT_MM || opt->unit == SANE_UNIT_PIXEL))
{
if (strcmp (opt->name, SANE_NAME_SCAN_TL_X) == 0)
{
window[2] = i;
all_options[option_count].val = 'l';
}
else if (strcmp (opt->name, SANE_NAME_SCAN_TL_Y) == 0)
{
window[3] = i;
all_options[option_count].val = 't';
}
else if (strcmp (opt->name, SANE_NAME_SCAN_BR_X) == 0)
{
window[0] = i;
all_options[option_count].name = "width";
all_options[option_count].val = 'x';
window_option[0] = *opt;
window_option[0].title = "Scan width";
window_option[0].desc = "Width of scanning area.";
if (!window_val_user[0])
sane_control_option (device, i, SANE_ACTION_GET_VALUE,
&window_val[0], 0);
}
else if (strcmp (opt->name, SANE_NAME_SCAN_BR_Y) == 0)
{
window[1] = i;
all_options[option_count].name = "height";
all_options[option_count].val = 'y';
window_option[1] = *opt;
window_option[1].title = "Scan height";
window_option[1].desc = "Height of scanning area.";
if (!window_val_user[1])
sane_control_option (device, i, SANE_ACTION_GET_VALUE,
&window_val[1], 0);
}
}
++option_count;
}
memcpy (all_options + option_count, basic_options, sizeof (basic_options));
option_count += NELEMS(basic_options);
memset (all_options + option_count, 0, sizeof (all_options[0]));
/* Initialize width & height options based on backend default
values for top-left x/y and bottom-right x/y: */
for (i = 0; i < 2; ++i)
{
if (window[i] && window[i + 2] && !window_val_user[i])
{
SANE_Word pos;
sane_control_option (device, window[i + 2],
SANE_ACTION_GET_VALUE, &pos, 0);
window_val[i] = window_val[i] - pos + 1;
}
}
}
static void
set_option (SANE_Handle device, int optnum, void * valuep)
{
const SANE_Option_Descriptor * opt;
SANE_Status status;
SANE_Word orig = 0;
SANE_Int info;
opt = sane_get_option_descriptor (device, optnum);
if (opt->size == sizeof (SANE_Word) && opt->type != SANE_TYPE_STRING)
orig = *(SANE_Word *) valuep;
status = sane_control_option (device, optnum, SANE_ACTION_SET_VALUE,
valuep, &info);
if (status != SANE_STATUS_GOOD)
{
fprintf (stderr, "%s: setting of option --%s failed (%s)\n",
prog_name, opt->name, sane_strstatus (status));
exit (1);
}
if ((info & SANE_INFO_INEXACT) && opt->size == sizeof (SANE_Word))
{
if (opt->type == SANE_TYPE_INT)
fprintf (stderr, "%s: rounded value of %s from %d to %d\n",
prog_name, opt->name, orig, *(SANE_Word *) valuep);
else if (opt->type == SANE_TYPE_FIXED)
fprintf (stderr, "%s: rounded value of %s from %g to %g\n",
prog_name, opt->name,
SANE_UNFIX(orig), SANE_UNFIX(*(SANE_Word *) valuep));
}
if (info & SANE_INFO_RELOAD_OPTIONS)
fetch_options (device);
}
static void
process_backend_option (SANE_Handle device, int optnum, const char * optarg)
{
static SANE_Word * vector = 0;
static size_t vector_size = 0;
const SANE_Option_Descriptor * opt;
size_t vector_length;
SANE_Status status;
SANE_Word value;
void * valuep;
opt = sane_get_option_descriptor (device, optnum);
if (!SANE_OPTION_IS_ACTIVE(opt->cap))
{
fprintf (stderr, "%s: attempted to set inactive option %s\n",
prog_name, opt->name);
exit (1);
}
if ((opt->cap & SANE_CAP_AUTOMATIC) && strncasecmp (optarg, "auto", 4) == 0)
{
status = sane_control_option (device, optnum, SANE_ACTION_SET_AUTO,
0, 0);
if (status != SANE_STATUS_GOOD)
{
fprintf (stderr, "%s: failed to set option --%s to automatic (%s)\n",
prog_name, opt->name, sane_strstatus (status));
exit (1);
}
return;
}
valuep = &value;
switch (opt->type)
{
case SANE_TYPE_BOOL:
value = 1; /* no argument means option is set */
if (optarg)
{
if (strncasecmp (optarg, "yes", strlen (optarg)) == 0)
value = 1;
else if (strncasecmp (optarg, "no", strlen (optarg)) == 0)
value = 0;
else
{
fprintf (stderr, "%s: option --%s: bad option value `%s'\n",
prog_name, opt->name, optarg);
exit (1);
}
}
break;
case SANE_TYPE_INT:
case SANE_TYPE_FIXED:
/* ensure vector is long enough: */
vector_length = opt->size / sizeof (SANE_Word);
if (vector_size < vector_length)
{
vector_size = vector_length;
vector = realloc (vector, vector_length * sizeof (SANE_Word));
if (!vector)
{
fprintf (stderr, "%s: out of memory\n", prog_name);
exit (1);
}
}
parse_vector (opt, optarg, vector, vector_length);
valuep = vector;
break;
case SANE_TYPE_STRING:
valuep = (void *) optarg;
break;
case SANE_TYPE_BUTTON:
value = 0; /* value doesn't matter */
break;
default:
fprintf (stderr, "%s: duh, got unknown option type %d\n",
prog_name, opt->type);
return;
}
set_option (device, optnum, valuep);
}
static void
write_pnm_header_to_file (FILE *fp, SANE_Frame format, int width, int height, int depth)
{
switch (format)
{
case SANE_FRAME_RED:
case SANE_FRAME_GREEN:
case SANE_FRAME_BLUE:
case SANE_FRAME_RGB:
fprintf (fp, "P6\n# SANE data follows\n%d %d\n255\n", width, height);
break;
case SANE_FRAME_GRAY:
if (depth == 1)
fprintf (fp, "P4\n# SANE data follows\n%d %d\n", width, height);
else
fprintf (fp, "P5\n# SANE data follows\n%d %d\n255\n", width, height);
break;
default:
/* default action for unknown frametypes; don't write a header */
break;
}
#ifdef __EMX__ /* OS2 - write in binary mode. */
_fsetmode(fp, "b");
#endif
}
static void *
advance (Image *image)
{
if (++image->x >= image->width)
{
image->x = 0;
if (++image->y >= image->height || !image->data)
{
size_t old_size = 0, new_size;
if (image->data)
old_size = image->height * image->width * image->Bpp;
image->height += STRIP_HEIGHT;
new_size = image->height * image->width * image->Bpp;
if (image->data)
image->data = realloc (image->data, new_size);
else
image->data = malloc (new_size);
if (image->data)
memset (image->data + old_size, 0, new_size - old_size);
}
}
if (!image->data)
fprintf (stderr, "%s: can't allocate image buffer (%dx%d)\n",
prog_name, image->width, image->height);
return image->data;
}
static SANE_Int
get_resolution(SANE_Device *device)
{
SANE_Int res = 200;
SANE_Word val;
const SANE_Option_Descriptor *opt;
if (resolution_opt >= 0)
{
opt = sane_get_option_descriptor (device, resolution_opt);
sane_control_option (device, resolution_opt,
SANE_ACTION_GET_VALUE, &val, 0);
switch (opt->type)
{
case SANE_TYPE_INT:
res = val;
break;
case SANE_TYPE_FIXED:
res = (SANE_Int) SANE_UNFIX(val);
break;
case SANE_TYPE_STRING:
case SANE_TYPE_BOOL:
default:
if (verbose)
fprintf(stderr,
"Peculiar option data type for resolution, "
"using default value.\n");
break;
}
}
else
{
if (verbose)
fprintf(stderr, "No resolution option found, using default value.\n");
}
return res;
}
static SANE_Status
scan_it_raw (const char *fname, SANE_Bool raw, const char *script)
{
int i, len, first_frame = 1, offset = 0, must_buffer = 0;
SANE_Byte buffer[32*1024], min = 0xff, max = 0;
SANE_Parameters parm;
SANE_Status status;
Image image = {0, };
FILE *fp = NULL;
do
{
status = sane_start (device);
if (status != SANE_STATUS_GOOD)
{
if (status != SANE_STATUS_NO_DOCS)
{
fprintf (stderr, "%s: sane_start: %s\n",
prog_name, sane_strstatus (status));
}
goto cleanup;
}
status = sane_get_parameters (device, &parm);
if (status != SANE_STATUS_GOOD)
{
fprintf (stderr, "%s: sane_get_parameters: %s\n",
prog_name, sane_strstatus (status));
goto cleanup;
}
fp = fopen(fname, "wb");
if (!fp) {
fprintf(stderr, "Error opening output `%s': %s (%d)\n",
fname, strerror(errno), errno);
status = SANE_STATUS_IO_ERROR;
goto cleanup;
}
if (verbose)
{
if (first_frame)
{
if (sane_isbasicframe(parm.format))
{
if (parm.lines >= 0)
fprintf (stderr, "%s: scanning image of size %dx%d pixels"
" at %d bits/pixel\n",
prog_name, parm.pixels_per_line, parm.lines,
8 * parm.bytes_per_line / parm.pixels_per_line);
else
fprintf (stderr, "%s: scanning image %d pixels wide and "
"variable height at %d bits/pixel\n",
prog_name, parm.pixels_per_line,
8 * parm.bytes_per_line / parm.pixels_per_line);
}
else
{
fprintf (stderr, "%s: receiving %s frame "
"bytes/line=%d, "
"pixels/line=%d, "
"lines=%d, "
"depth=%d\n",
prog_name, sane_strframe(parm.format),
parm.bytes_per_line,
parm.pixels_per_line,
parm.lines,
parm.depth);
}
}
fprintf (stderr, "%s: acquiring %s frame\n", prog_name,
sane_strframe(parm.format));
}
if (first_frame)
{
switch (parm.format)
{
case SANE_FRAME_RED:
case SANE_FRAME_GREEN:
case SANE_FRAME_BLUE:
assert (parm.depth == 8);
must_buffer = 1;
offset = parm.format - SANE_FRAME_RED;
break;
case SANE_FRAME_RGB:
assert (parm.depth == 8);
case SANE_FRAME_GRAY:
assert (parm.depth == 1 || parm.depth == 8);
/* if we're writing raw, we skip the header and never
* have to buffer a single frame format.
*/
if (raw == SANE_FALSE)
{
if (parm.lines < 0)
{
must_buffer = 1;
offset = 0;
}
else
{
write_pnm_header_to_file (fp, parm.format,
parm.pixels_per_line,
parm.lines, parm.depth);
}
}
break;
case SANE_FRAME_TEXT:
case SANE_FRAME_JPEG:
case SANE_FRAME_G31D:
case SANE_FRAME_G32D:
case SANE_FRAME_G42D:
if (!parm.last_frame)
{
status = SANE_STATUS_INVAL;
fprintf (stderr, "%s: bad %s frame: must be last_frame\n",
prog_name, sane_strframe (parm.format));
goto cleanup;
}
/* write them out without a header; don't buffer */
break;
default:
/* Default action for unknown frametypes; write them out
* without a header; issue a warning in verbose mode.
* Since we're not writing a header, there's no need to
* buffer.
*/
if (verbose)
{
fprintf(stderr, "scanadf: unknown frame format %d\n",
(int) parm.format);
}
if (!parm.last_frame)
{
status = SANE_STATUS_INVAL;
fprintf (stderr, "%s: bad %s frame: must be last_frame\n",
prog_name, sane_strframe (parm.format));
goto cleanup;
}
break;
}
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. */
image.width = parm.pixels_per_line;
if (parm.lines >= 0)
/* See advance(); we allocate one extra line so we
don't end up realloc'ing in when the image has been
filled in. */
image.height = parm.lines - STRIP_HEIGHT + 1;
else
image.height = 0;
image.Bpp = 3;
if (parm.format == SANE_FRAME_GRAY ||
!sane_isbasicframe(parm.format))
image.Bpp = 1;
image.x = image.width - 1;
image.y = -1;
if (!advance (&image))
goto cleanup;
}
}
else
{
assert (parm.format >= SANE_FRAME_RED
&& parm.format <= SANE_FRAME_BLUE);
offset = parm.format - SANE_FRAME_RED;
image.x = image.y = 0;
}
while (1)
{
status = sane_read (device, buffer, sizeof (buffer), &len);
if (status != SANE_STATUS_GOOD)
{
if (verbose && parm.depth == 8)
fprintf (stderr, "%s: min/max graylevel value = %d/%d\n",
prog_name, min, max);
if (status != SANE_STATUS_EOF)
{
fprintf (stderr, "%s: sane_read: %s\n",
prog_name, sane_strstatus (status));
goto cleanup;
}
break;
}
if (must_buffer)
{
switch (parm.format)
{
case SANE_FRAME_RED:
case SANE_FRAME_GREEN:
case SANE_FRAME_BLUE:
for (i = 0; i < len; ++i)
{
image.data[offset + 3*i] = buffer[i];
if (!advance (&image))
goto cleanup;
}
offset += 3*len;
break;
case SANE_FRAME_RGB:
for (i = 0; i < len; ++i)
{
image.data[offset + i] = buffer[i];
if ((offset + i) % 3 == 0 && !advance (&image))
goto cleanup;
}
offset += len;
break;
case SANE_FRAME_GRAY:
for (i = 0; i < len; ++i)
{
image.data[offset + i] = buffer[i];
if (!advance (&image))
goto cleanup;
}
offset += len;
break;
default:
/* optional frametypes are never buffered */
fprintf(stderr, "%s: ERROR: trying to buffer %s"
" frametype\n",
prog_name,
sane_strframe(parm.format));
break;
}
}
else
fwrite (buffer, 1, len, fp);
if (verbose && parm.depth == 8)
{
for (i = 0; i < len; ++i)
if (buffer[i] >= max)
max = buffer[i];
else if (buffer[i] < min)
min = buffer[i];
}
}
first_frame = 0;
}
while (!parm.last_frame);
if (must_buffer)
{
image.height = image.y;
if (raw == SANE_FALSE)
{
/* if we're writing raw, we skip the header */
write_pnm_header_to_file (fp, parm.format, image.width,
image.height, parm.depth);
}
fwrite (image.data, image.Bpp, image.height * image.width, fp);
}
if (fp)
{
fclose(fp);
fp = NULL;
}
if (script)
{
static char cmd[PATH_MAX * 2];
static char env[6][PATH_MAX * 2];
int pid;
SANE_Int res;
SANE_Frame format;
extern char **environ;
res = get_resolution(device);
format = parm.format;
if (format == SANE_FRAME_RED ||
format == SANE_FRAME_GREEN ||
format == SANE_FRAME_BLUE)
{
/* the resultant format is RGB */
format = SANE_FRAME_RGB;
}
sprintf(env[0], "SCAN_RES=%d", res);
if (putenv(env[0]))
fprintf(stderr, "putenv:failed\n");
sprintf(env[1], "SCAN_WIDTH=%d", parm.pixels_per_line);
if (putenv(env[1]))
fprintf(stderr, "putenv:failed\n");
sprintf(env[2], "SCAN_HEIGHT=%d", parm.lines);
if (putenv(env[2]))
fprintf(stderr, "putenv:failed\n");
sprintf(env[3], "SCAN_FORMAT_ID=%d", (int) parm.format);
if (putenv(env[3]))
fprintf(stderr, "putenv:failed\n");
sprintf(env[4], "SCAN_FORMAT=%s",
sane_strframe(parm.format));
if (putenv(env[4]))
fprintf(stderr, "putenv:failed\n");
sprintf(env[5], "SCAN_DEPTH=%d", parm.depth);
if (putenv(env[5]))
fprintf(stderr, "putenv:failed\n");
signal(SIGCHLD, SIG_IGN);
switch ((pid = fork()))
{
case -1:
/* fork failed */
fprintf(stderr, "Error forking: %s (%d)\n", strerror(errno), errno);
break;
case 0:
/* in child process */
sprintf(cmd, "%s '%s'", script, fname);
/* system(cmd); */
execle(script, script, fname, NULL, environ);
exit(0);
default:
if (verbose)
fprintf(stderr, "Started script `%s' as pid=%d\n", script, pid);
break;
}
}
cleanup:
if (image.data)
free (image.data);
if (fp) fclose(fp);
return status;
}
static SANE_Int
scan_docs (int start, int end, int no_overwrite, SANE_Bool raw, const char *outfmt, const char *script)
{
SANE_Status status = SANE_STATUS_GOOD;
SANE_Int scannedPages = 0;
SANE_Char fname[PATH_MAX];
struct stat statbuf;
int res;
while (end < 0 || start <= end)
{
/*!!! buffer overflow; need protection */
sprintf(fname, outfmt, start);
/* does the filename already exist? */
if (no_overwrite)
{
res = stat (fname, &statbuf);
if (res == 0)
{
status = SANE_STATUS_INVAL;
fprintf (stderr, "Filename %s already exists; will not overwrite\n", fname);
}
}
/* Scan the document */
if (status == SANE_STATUS_GOOD)
status = scan_it_raw(fname, raw, script);
/* Any scan errors? */
if (status == SANE_STATUS_NO_DOCS)
{
/* out of paper in the hopper; this is our normal exit */
status = SANE_STATUS_GOOD;
break;
}
else if (status == SANE_STATUS_EOF)
{
/* done with this doc */
status = SANE_STATUS_GOOD;
fprintf(stderr, "Scanned document %s\n", fname);
scannedPages++;
start++;
}
else
{
/* unexpected error */
fprintf(stderr, "%s\n", sane_strstatus(status));
break;
}
}
fprintf(stderr, "Scanned %d pages\n", scannedPages);
return status;
}
int
main (int argc, char **argv)
{
int ch, i, index, all_options_len;
const SANE_Option_Descriptor * opt;
const SANE_Device ** device_list;
SANE_Int num_dev_options = 0;
const char * devname = 0;
SANE_Status status;
char *full_optstring;
SANE_Bool raw = SANE_FALSE;
const char *scanScript = NULL; /* script to run at end of scan */
int scriptWait = 0;
const char *outputFile = "image-%04d"; /* file name(format) to write output to */
int startNum = 1, endNum = -1; /* start/end numbers of pages to scan */
int no_overwrite = 0;
atexit (sane_exit);
prog_name = strrchr (argv[0], '/');
if (prog_name)
++prog_name;
else
prog_name = argv[0];
sane_init (0, 0);
/* make a first pass through the options with error printing and argument
permutation disabled: */
opterr = 0;
while ((ch = getopt_long (argc, argv, "-" BASE_OPTSTRING, basic_options,
&index))
!= EOF)
{
switch (ch)
{
case ':':
case '?':
break; /* may be an option that we'll parse later on */
case 'd': devname = optarg; break;
case 'h': help = 1; break;
case 'N': no_overwrite = 1; break;
case 'v': ++verbose; break;
case 'L':
{
int i;
status = sane_get_devices (&device_list, SANE_FALSE);
if (status != SANE_STATUS_GOOD)
{
fprintf (stderr, "%s: sane_get_devices() failed: %s\n",
prog_name, sane_strstatus (status));
exit (1);
}
for (i = 0; device_list[i]; ++i)
{
printf ("device `%s' is a %s %s %s\n",
device_list[i]->name, device_list[i]->vendor,
device_list[i]->model, device_list[i]->type);
}
exit (0);
}
case 'o': outputFile = optarg; break;
case 'S': scanScript = optarg; break;
case 128: scriptWait = 1; break;
case 's': startNum = atoi(optarg); break;
case 'e': endNum = atoi(optarg); break;
case 'r': raw = SANE_TRUE; break;
case 'V':
printf ("scanadf (%s) %s\n", PACKAGE, VERSION);
exit (0);
default:
break; /* ignore device specific options for now */
}
}
if (scriptWait && !scanScript)
scriptWait = 0;
if (help)
printf (usage, prog_name);
if (!devname)
{
/* If no device name was specified explicitly, we open the first
device we find (if any): */
status = sane_get_devices (&device_list, SANE_FALSE);
if (status != SANE_STATUS_GOOD)
{
fprintf (stderr, "%s: sane_get_devices() failed: %s\n",
prog_name, sane_strstatus (status));
exit (1);
}
if (!device_list[0])
{
fprintf (stderr, "%s: no SANE devices found\n", prog_name);
exit (1);
}
devname = device_list[0]->name;
}
status = sane_open (devname, &device);
if (status != SANE_STATUS_GOOD)
{
fprintf (stderr, "%s: open of device %s failed: %s\n",
prog_name, devname, sane_strstatus (status));
if (help)
device = 0;
else
exit (1);
}
if (device)
{
/* We got a device, find out how many options it has: */
status = sane_control_option (device, 0, SANE_ACTION_GET_VALUE,
&num_dev_options, 0);
if (status != SANE_STATUS_GOOD)
{
fprintf (stderr, "%s: unable to determine option count\n",
prog_name);
exit (1);
}
all_options_len = num_dev_options + NELEMS(basic_options) + 1;
all_options = malloc (all_options_len * sizeof (all_options[0]));
option_number_len = num_dev_options;
option_number = malloc (option_number_len * sizeof (option_number[0]));
if (!all_options || !option_number)
{
fprintf (stderr, "%s: out of memory in fetch_options()\n",
prog_name);
exit (1);
}
fetch_options (device);
{
char *larg, *targ, *xarg, *yarg;
larg = targ = xarg = yarg = "";
/* Maybe accept t, l, x, and y options. */
if (window[0])
xarg = "x:";
if (window[1])
yarg = "y:";
if (window[2])
larg = "l:";
if (window[3])
targ = "t:";
/* Now allocate the full option list. */
full_optstring = malloc (strlen (BASE_OPTSTRING)
+ strlen (larg) + strlen (targ)
+ strlen (xarg) + strlen (yarg) + 1);
if (!full_optstring)
{
fprintf (stderr, "%s: out of memory\n", prog_name);
exit (1);
}
strcpy (full_optstring, BASE_OPTSTRING);
strcat (full_optstring, larg);
strcat (full_optstring, targ);
strcat (full_optstring, xarg);
strcat (full_optstring, yarg);
}
optind = 0;
opterr = 1; /* re-enable error printing and arg permutation */
while ((ch = getopt_long (argc, argv, full_optstring, all_options,
&index))
!= EOF)
{
switch (ch)
{
case ':':
case '?':
exit (1); /* error message is printed by getopt_long() */
case 'd': case 'h': case 'v': case 'V': case 'T':
case 'o': case 'S': case 's': case 'e': case 'r':
/* previously handled options */
break;
case 'x':
window_val_user[0] = 1;
parse_vector (&window_option[0], optarg, &window_val[0], 1);
break;
case 'y':
window_val_user[1] = 1;
parse_vector (&window_option[1], optarg, &window_val[1], 1);
break;
case 'l': /* tl-x */
process_backend_option (device, window[2], optarg);
break;
case 't': /* tl-y */
process_backend_option (device, window[3], optarg);
break;
case 0:
process_backend_option (device, option_number[index], optarg);
break;
}
}
free (full_optstring);
for (index = 0; index < 2; ++index)
if (window[index])
{
SANE_Word val, pos;
if (window[index + 2])
sane_control_option (device, window[index + 2],
SANE_ACTION_GET_VALUE, &pos, 0);
val = pos + window_val[index] - 1;
set_option (device, window[index], &val);
}
if (help)
{
printf ("\nOptions specific to device `%s':\n", devname);
for (i = 0; i < num_dev_options; ++i)
{
char short_name = '\0';
int j;
opt = 0;
for (j = 0; j < 4; ++j)
if (i == window[j])
{
short_name = "xylt"[j];
if (j < 2)
opt = window_option + j;
}
if (!opt)
opt = sane_get_option_descriptor (device, i);
if (opt->type == SANE_TYPE_GROUP)
printf (" %s:\n", opt->title);
if (!SANE_OPTION_IS_SETTABLE (opt->cap))
continue;
print_option (device, i, short_name);
}
if (num_dev_options)
fputc ('\n', stdout);
}
}
if (help)
{
printf ("\
Type ``%s --help -d DEVICE'' to get list of all options for DEVICE.\n\
\n\
List of available devices:", prog_name);
if (device)
sane_close(device);
status = sane_get_devices (&device_list, SANE_FALSE);
if (status == SANE_STATUS_GOOD)
{
int column = 80;
for (i = 0; device_list[i]; ++i)
{
if (column + strlen (device_list[i]->name) + 1 >= 80)
{
printf ("\n ");
column = 4;
}
if (column > 4)
{
fputc (' ', stdout);
column += 1;
}
fputs (device_list[i]->name, stdout);
column += strlen (device_list[i]->name);
}
}
fputc ('\n', stdout);
exit (0);
}
signal (SIGHUP, sighandler);
signal (SIGINT, sighandler);
signal (SIGPIPE, sighandler);
signal (SIGTERM, sighandler);
status = scan_docs (startNum, endNum, no_overwrite, raw, outputFile, scanScript);
sane_cancel (device);
sane_close (device);
if (scriptWait)
while (wait(NULL) != -1);
return (status == SANE_STATUS_GOOD) ? 0 : 1;
}
|