-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtiffscan.c
2019 lines (1603 loc) · 44.2 KB
/
tiffscan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tiffscan -- command line scanning utility
* Copyright (C) 2007-15 by Alessandro Zummo <[email protected]>
*
* Loosely based on scanimage,
* Copyright (C) 1996, 1997, 1998 Andreas Beck and David Mosberger
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#define _GNU_SOURCE
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <sane/sane.h>
#if defined(SANE_HAS_EVOLVED)
#define SANE_HAS_WARMING_UP
#endif
#ifndef SANE_FRAME_IR
#define SANE_FRAME_IR 0x0F
#endif
#ifndef SANE_FRAME_RGBI
#define SANE_FRAME_RGBI 0x10
#endif
#define SANE_HAS_INFRARED
#include <sane/saneopts.h>
#ifdef SANE_HAS_EVOLVED
#include <sane/helper.h>
#endif
#include <tiff.h>
#include <tiffio.h>
#include <popt.h>
#include <paper.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
/* XXX move to hacks.h */
enum optio
{
OPT_HELP = 1, OPT_LIST_DEVS, OPT_VERSION, OPT_SCAN,
OPT_VERBOSE, OPT_DEVICE
};
#define BATCH_COUNT_UNLIMITED -1
static void tiffscan_exit(void);
/*
static SANE_Word tl_x = 0;
static SANE_Word tl_y = 0;
static SANE_Word br_x = 0;
static SANE_Word br_y = 0;
static SANE_Word w_x = 0;
static SANE_Word h_y = 0;
*/
/* main options */
static char *devname = NULL;
static int verbose = 0;
static int progress = 0;
static int scanlines = 200;
/* tiff tags */
static const char *tiff_artist = NULL;
static const char *tiff_copyright = NULL;
static const char *tiff_documentname = NULL;
static const char *tiff_imagedesc = NULL;
static const char *tiff_orientation = NULL;
/* batch options */
static int batch = 0;
static int batch_prompt = 0;
static int batch_amount = BATCH_COUNT_UNLIMITED;
static int batch_start_at = 1;
static int batch_increment = 1;
/* output options */
static char *output_file = NULL;
static char *output_path = NULL;
static const char *icc_profile = NULL;
static int compress = 1;
static int multi = 1;
/* pdf options */
static int pdf_mode = 0;
static char *pdf_options = "-z -p a4";
/* misc options */
static const char *paper = NULL;
/* globals */
static SANE_Handle handle;
static int batch_count = 0;
static int resolution_optind = -1;
static int corners[4];
static char output_file_buf[34];
#ifdef SANE_HAS_EVOLVED
static SANE_Scanner_Info si;
#endif
static struct poptOption options[] = {
{"device", 'd', POPT_ARG_STRING, NULL, OPT_DEVICE, "device name", NULL},
{"scan", 's', POPT_ARG_NONE, NULL, OPT_SCAN,
"this should be obvious :)", NULL},
{"list-devices", 'L', POPT_ARG_NONE, NULL, OPT_LIST_DEVS,
"list known devices", NULL},
/* informational */
{"version", 0, POPT_ARG_NONE, NULL, OPT_VERSION,
"show program version", NULL},
{"progress", 'p', POPT_ARG_NONE, &progress, 0,
"show progress information", NULL},
{"verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE,
"gives detailed status messages", NULL},
{"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, "this help message",
NULL},
/* output options */
{"output-file", 'o', POPT_ARG_STRING, &output_file, 0,
"output file name, use %d to insert page number", "FILE"},
{"output-dir", 'O', POPT_ARG_STRING, &output_path, 0,
"output directory path, will cwd to it", "PATH"},
{"multi-page", 0, POPT_BIT_SET | POPT_ARGFLAG_TOGGLE, &multi, 1,
"create a multi-page TIFF file", NULL},
{"compress", 0, POPT_BIT_SET | POPT_ARGFLAG_TOGGLE, &compress, 1,
"use TIFF lossless compression", NULL},
{"icc-profile", 0, POPT_ARG_STRING, &icc_profile, 0,
"embed an ICC profile in the TIFF file", "FILE"},
/* tiff tags */
{"artist", 0, POPT_ARG_STRING, &tiff_artist, 0,
"TIFF tag: Artist", NULL},
{"copyright", 0, POPT_ARG_STRING, &tiff_copyright, 0,
"TIFF tag: Copyright", NULL},
{"document-name", 0, POPT_ARG_STRING, &tiff_documentname, 0,
"TIFF tag: DocumentName", NULL},
{"image-description", 0, POPT_ARG_STRING, &tiff_imagedesc, 0,
"TIFF tag: ImageDescription", NULL},
{"orientation", 0, POPT_ARG_STRING, &tiff_orientation, 0,
"TIFF tag: Orientation", NULL},
/* batch options */
{"batch", 0, POPT_ARG_NONE, &batch, 0, "batch mode", NULL},
{"batch-count", 0, POPT_ARG_INT, &batch_amount, 0,
"number of pages to scan", NULL},
{"batch-start", 0, POPT_ARG_INT, &batch_start_at, 0,
"number of the first page", NULL},
{"batch-increment", 0, POPT_ARG_INT, &batch_increment, 0,
"page number increment amount", NULL},
{"batch-prompt", 0, POPT_ARG_NONE, &batch_prompt, 0,
"manual prompt before scanning", NULL},
/* pdf options */
{"pdf", 0, POPT_ARG_NONE, &pdf_mode, 0, "invoke tiff2pdf on the scanned tiff file(s)", NULL},
/* other options */
{"paper", 0, POPT_ARG_STRING, &paper, 0,
"scanning area as paper name (A4, Letter, ...)", NULL},
POPT_TABLEEND, /* this entry will be used for device options */
POPT_TABLEEND,
};
static void
sighandler(int signum)
{
static SANE_Bool first_time = SANE_TRUE;
if (handle) {
printf("\nreceived signal %d\n", signum);
if (first_time) {
first_time = SANE_FALSE;
printf("trying to stop the scanner, one more CTRL-C will exit tiffscan.\n");
sane_cancel(handle);
} else {
printf("aborting\n");
_exit(0);
}
}
}
/* 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) {
printf("option --%s: bad option value (rest of option: %s)\n",
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 {
printf("option --%s: illegal unit (rest of option: %s)\n", 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.
*
*/
/* XXX exit is not appropriate in this func */
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 != ']') {
printf("option --%s: closing bracket missing "
"(rest of option: %s)\n", opt->name,
str);
exit(1);
}
str = end + 1;
} else
++index;
if (index < 0 || index >= (int) vector_length) {
printf("option --%s: index %d out of range [0..%ld]\n", 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 != ',') {
printf("option --%s: illegal separator (rest of option: %s)\n", 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 > 1) {
int i;
printf("value for --%s is: ", opt->name);
for (i = 0; i < (int) vector_length; ++i)
if (opt->type == SANE_TYPE_FIXED)
printf("%g ", SANE_UNFIX(vector[i]));
else
printf("%d ", vector[i]);
fputc('\n', stdout);
}
}
/* XXX move to strings.c */
static void
strext(char **dst, const char *src)
{
int len = strlen(src);
if (dst == NULL)
return;
if (*dst) {
len += strlen(*dst);
*dst = realloc(*dst, len + 1);
strcat(*dst, src);
} else {
*dst = malloc(len + 1);
strcpy(*dst, src);
}
}
static char *
strjoin(char **src, char sep)
{
char *dst;
int i = 0;
int len = 0;
/* calc len */
for (i = 0; src[i]; i++)
len += strlen(src[i]);
/* account for separators and '\0' */
len += i + 1;
dst = realloc(NULL, len);
if (dst == NULL)
return NULL;
dst[0] = '\0';
for (i = 0; src[i]; i++) {
strcat(dst, src[i]);
strncat(dst, &sep, 1);
}
/* remove last sep */
dst[len - 2] = '\0';
return dst;
}
static char *
itoa(int v)
{
char *s;
int l = log10(abs(v) + 1) + 3;
/* + 3 included sign and '\0' */
s = malloc(l);
if (s == NULL)
return NULL;
snprintf(s, l, "%d", v);
return s;
}
static char *
dtoa(double v)
{
char *s;
int l = log10(v + 1) + 1 + 1 + 3;
s = malloc(l);
if (s == NULL)
return NULL;
snprintf(s, l, "%.02f", v);
return s;
}
/* XXX move to options.c */
static void
add_default_option(char **dst, SANE_Handle handle,
const SANE_Option_Descriptor *opt, int opt_num)
{
void *val;
if (!SANE_OPTION_IS_ACTIVE(opt->cap)) {
strext(dst, " [inactive]");
return;
}
val = malloc(opt->size);
if (val == NULL)
return;
sane_control_option(handle, opt_num, SANE_ACTION_GET_VALUE, val, 0);
strext(dst, " [");
switch (opt->type) {
case SANE_TYPE_BOOL:
strext(dst, *(SANE_Bool *) val ? "yes" : "no");
break;
case SANE_TYPE_STRING:
strext(dst, (char *) val);
break;
case SANE_TYPE_INT:
{
char *s = itoa(*(SANE_Int *) val);
strext(dst, s);
free(s);
break;
}
case SANE_TYPE_FIXED:
{
char *s = dtoa(SANE_UNFIX(*(SANE_Fixed *) val));
strext(dst, s);
free(s);
break;
}
default:
break;
}
strext(dst, "]");
free(val);
}
static void
track_corners(const int index, const SANE_Option_Descriptor * opt)
{
/* XXX only SANE_TYPE_FIXED right now */
/* if (!(opt->type == SANE_TYPE_FIXED || opt->type == SANE_TYPE_INT)) */
if (opt->type != SANE_TYPE_FIXED)
return;
/* SANE_Int == SANE_Word == SANE_Fixed */
if (opt->size != sizeof(SANE_Word))
return;
/* XXX we handle only mm right now */
if (opt->unit != SANE_UNIT_MM)
return;
if (strcmp(opt->name, SANE_NAME_SCAN_TL_X) == 0)
corners[0] = index;
else if (strcmp(opt->name, SANE_NAME_SCAN_TL_Y) == 0)
corners[1] = index;
else if (strcmp(opt->name, SANE_NAME_SCAN_BR_X) == 0)
corners[2] = index;
else if (strcmp(opt->name, SANE_NAME_SCAN_BR_Y) == 0)
corners[3] = index;
}
static struct poptOption *
fetch_options(SANE_Handle handle)
{
SANE_Status status;
struct poptOption *options;
const SANE_Option_Descriptor *opt;
SANE_Int num_dev_options;
int i, count;
/* init corners tracking */
memset(corners, -1, sizeof(corners));
/* query number of options */
status = sane_control_option(handle, 0, SANE_ACTION_GET_VALUE,
&num_dev_options, 0);
if (status != SANE_STATUS_GOOD)
return NULL;
options = malloc(sizeof(struct poptOption) * (num_dev_options + 1));
if (options == NULL)
return NULL;
/* clear. last entry will be all zero */
memset(options, 0x00,
sizeof(struct poptOption) * (num_dev_options + 1));
/* build backend options table */
for (count = i = 0; i < num_dev_options; i++) {
struct poptOption *thisopt = &options[count];
opt = sane_get_option_descriptor(handle, i);
if (!SANE_OPTION_IS_SETTABLE(opt->cap))
continue;
if (opt->type == SANE_TYPE_GROUP)
continue;
if (opt->type == SANE_TYPE_BUTTON && !SANE_OPTION_IS_ACTIVE(opt->cap))
continue;
/* search and save resolution */
if ((opt->type == SANE_TYPE_FIXED
|| opt->type == SANE_TYPE_INT)
&& opt->size == sizeof(SANE_Int)
&& (opt->unit == SANE_UNIT_DPI)
&& (strcmp(opt->name, SANE_NAME_SCAN_RESOLUTION) == 0))
resolution_optind = i;
thisopt->longName = opt->name ? opt->name : "unknown";
thisopt->shortName = 0;
thisopt->arg = NULL;
thisopt->val = 1000 + i;
thisopt->descrip = (opt->desc
&& strlen(opt->desc)) ? opt->desc : " ";
thisopt->argDescrip = NULL;
switch (opt->type) {
case SANE_TYPE_BOOL:
thisopt->argInfo = POPT_ARG_INT;
if (opt->cap & SANE_CAP_AUTOMATIC)
thisopt->argDescrip = strdup("yes|no|auto");
else
thisopt->argDescrip = strdup("yes|no");
break;
case SANE_TYPE_BUTTON:
thisopt->argInfo = POPT_ARG_NONE;
break;
default:
thisopt->argInfo = POPT_ARG_STRING;
switch (opt->constraint_type) {
case SANE_CONSTRAINT_NONE:
switch (opt->type) {
case SANE_TYPE_INT:
// thisopt->argDescrip = strdup("<int>");
break; /* FIXED STRING */
default:
break;
}
break;
case SANE_CONSTRAINT_RANGE: {
/* an ugly, fixed size, buffer */
char range[60];
if (opt->type == SANE_TYPE_FIXED) {
if (SANE_UNFIX(opt->constraint.range->min) == 0) {
sprintf(range, "0..%.02f",
SANE_UNFIX(opt->constraint.range->max));
} else {
sprintf(range, "%.02f..%.02f",
SANE_UNFIX(opt->constraint.range->min),
SANE_UNFIX(opt->constraint.range->max));
}
} else if (opt->type == SANE_TYPE_INT) {
sprintf(range, "%d..%d",
opt->constraint.range->min,
opt->constraint.range->max);
}
strext((char **)&thisopt->argDescrip, range);
if (opt->constraint.range->quant){
if (opt->type == SANE_TYPE_FIXED) {
sprintf(range, " (in steps of %.02f)",
SANE_UNFIX(opt->constraint.range->quant));
} else if (opt->type == SANE_TYPE_INT) {
sprintf(range, " (in steps of %d)",
opt->constraint.range->quant);
}
}
}
break;
case SANE_CONSTRAINT_WORD_LIST: {
int len = 0;
char *p = malloc(1);
for (int i = 0; i < opt->constraint.word_list[0]; i++) {
int size = (int)log10(opt->constraint.word_list[i + 1]) + 1;
// old len + separator + terminator
p = realloc(p, len + size + 1 + 1);
sprintf(p + len, "%d", opt->constraint.word_list[i + 1]);
if (i < (opt->constraint.word_list[0] - 1)) {
strcat(p, "|");
len++;
}
len += size;
}
thisopt->argDescrip = p;
}
break;
case SANE_CONSTRAINT_STRING_LIST:
thisopt->argDescrip =
strjoin((char **) opt->constraint.
string_list, '|');
break;
}
break;
}
switch (opt->unit) {
case SANE_UNIT_PIXEL:
strext((char **)&thisopt->argDescrip, "pixel");
break;
case SANE_UNIT_BIT:
strext((char **)&thisopt->argDescrip, "bit");
break;
case SANE_UNIT_MM:
strext((char **)&thisopt->argDescrip, "mm");
break;
case SANE_UNIT_DPI:
strext((char **)&thisopt->argDescrip, "dpi");
break;
case SANE_UNIT_PERCENT:
strext((char **)&thisopt->argDescrip, "%");
break;
case SANE_UNIT_MICROSECOND:
strext((char **)&thisopt->argDescrip, "ms");
break;
case SANE_UNIT_NONE:
break;
}
add_default_option((char **)&thisopt->argDescrip, handle,
opt, i);
count++;
/* Keep track of corner options */
track_corners(i, opt);
}
return options;
}
/* XXX options.c */
static SANE_Status
sane_set_opt_word(SANE_Handle handle, SANE_Word index, double v)
{
SANE_Word info, *p, value, orig;
SANE_Status status;
const SANE_Option_Descriptor *opt;
opt = sane_get_option_descriptor(handle, index);
if (opt == NULL) {
printf("Couldn't get option descriptor for option %d\n",
index);
return SANE_STATUS_INVAL;
}
if (opt->type == SANE_TYPE_FIXED)
orig = value = SANE_FIX(v);
else
orig = value = (SANE_Word)v;
p = &value;
status = sane_control_option(handle, index,
SANE_ACTION_SET_VALUE, p, &info);
if (status != SANE_STATUS_GOOD)
return status;
if (info & SANE_INFO_INEXACT) {
if (opt->type == SANE_TYPE_INT)
printf("Rounded value of %s from %d to %d\n",
opt->name, orig, *(SANE_Word *) p);
else if (opt->type == SANE_TYPE_FIXED)
printf("Rounded value of %s from %g to %g\n",
opt->name, SANE_UNFIX(orig),
SANE_UNFIX(*(SANE_Word *) p));
}
return status;
}
#define ADF_STR "Automatic Document Feeder"
static SANE_Status
set_option(SANE_Handle handle, int optnum, void *valuep)
{
const SANE_Option_Descriptor *opt;
SANE_Status status;
SANE_Int info;
opt = sane_get_option_descriptor(handle, optnum);
if (opt == NULL)
return SANE_STATUS_INVAL;
// auto batch mode
if (strncmp(opt->name, SANE_NAME_SCAN_SOURCE, strlen(SANE_NAME_SCAN_SOURCE)) == 0) {
if (strncmp(valuep, ADF_STR, strlen(ADF_STR)) == 0) {
batch = 1;
}
}
if (opt->type == SANE_TYPE_INT && opt->size == sizeof(SANE_Word))
status = sane_set_opt_word(handle, optnum,
*(SANE_Word *) valuep);
else if (opt->type == SANE_TYPE_FIXED && opt->size == sizeof(SANE_Word))
status = sane_set_opt_word(handle, optnum,
SANE_UNFIX(*(SANE_Word *) valuep));
else {
status = sane_control_option(handle, optnum,
SANE_ACTION_SET_VALUE, valuep, &info);
if (status != SANE_STATUS_GOOD && strcmp(opt->name, "mode") == 0
&& strcmp(valuep, "binary") == 0) {
strcpy(valuep, "lineart");
status = sane_control_option(handle, optnum,
SANE_ACTION_SET_VALUE, valuep, &info);
}
}
if (status != SANE_STATUS_GOOD)
printf("setting of option --%s failed (%s)\n",
opt->name, sane_strstatus(status));
return status;
}
static SANE_Status
process_backend_option(SANE_Handle handle, 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(handle, optnum);
if (!opt)
return SANE_STATUS_INVAL;
if (!SANE_OPTION_IS_ACTIVE(opt->cap)) {
printf("attempted to set inactive option %s, ignoring\n",
opt->name);
return SANE_STATUS_GOOD;
}
if ((opt->cap & SANE_CAP_AUTOMATIC) && optarg &&
strncasecmp(optarg, "auto", 4) == 0) {
status = sane_control_option(handle, optnum,
SANE_ACTION_SET_AUTO, 0, 0);
if (status != SANE_STATUS_GOOD)
printf("failed to set option --%s to automatic (%s)\n", opt->name, sane_strstatus(status));
return status;
}
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 {
printf("option --%s: bad option value `%s'\n",
opt->name, optarg);
return SANE_STATUS_INVAL;
}
}
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) {
printf("out of memory\n");
return SANE_STATUS_NO_MEM;
}
}
parse_vector(opt, optarg, vector, vector_length);
valuep = vector;
break;
case SANE_TYPE_STRING:
valuep = malloc(opt->size);
if (valuep == NULL) {
printf("out of memory\n");
return SANE_STATUS_NO_MEM;
}
strncpy(valuep, optarg, opt->size);
((char *) valuep)[opt->size - 1] = 0;
break;
case SANE_TYPE_BUTTON:
value = 0; /* value doesn't matter */
break;
default:
printf("got unknown option type %d\n", opt->type);
return SANE_STATUS_INVAL;
}
status = set_option(handle, optnum, valuep);
if (opt->type == SANE_TYPE_STRING)
free(valuep);
return status;
}
/* XXX tiff.c */
static void
embed_icc_profile(TIFF * image, const char *file)
{
FILE *fd;
char *buf;
struct stat info;
if (stat(file, &info) != 0)
return;
buf = malloc(info.st_size);
if (buf == NULL)
return;
if (verbose)
printf("using ICC profile '%s', %d bytes\n", file,
(int) info.st_size);
if (info.st_size < 44) {
printf("ICC profile is too short (%d)\n", (int) info.st_size);
return;
}
fd = fopen(file, "r");
if (fd) {
if (fread(buf, 1, info.st_size, fd) == info.st_size) {
if (strncmp(&buf[36], "acsp", 4) == 0) {
TIFFSetField(image, TIFFTAG_ICCPROFILE,
(uint32) info.st_size, buf);
} else {
printf("%s is not a valid ICC profile\n",
file);
}
}
fclose(fd);
}
free(buf);
}
static int
check_sane_format(SANE_Parameters * parm)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
switch (parm->format) {
case SANE_FRAME_RED:
case SANE_FRAME_GREEN:
case SANE_FRAME_BLUE:
if (parm->depth == 8)
return 1;
break;