1 #include "../include/sane/config.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 
7 #include <sys/fcntl.h>
8 
9 #include "../include/sane/sane.h"
10 #include "../include/sane/sanei.h"
11 #include "../include/sane/sanei_wire.h"
12 #include "../include/sane/sanei_codec_ascii.h"
13 #include "../include/sane/sanei_codec_bin.h"
14 
15 static Wire w;
16 
17 static SANE_Word dpi_word_list[] = {
18   4,				/* # of elements */
19   3, 30, 300, -600
20 };
21 
22 static SANE_String_Const mode_list[] = {
23   "Lineart", "Grayscale", "Color", 0
24 };
25 
26 static char *program_name;
27 static char *default_codec = "bin";
28 static char *default_outfile = "test_wire.out";
29 
30 static int
usage(int code)31 usage (int code)
32 {
33   if (code == 0)
34     {
35       printf ("Usage: %s [OPTION]...\n\
36 \n\
37 Test the SANE wire manipulation library.\n\
38 \n\
39     --codec=CODEC        set the codec [default=%s]\n\
40     --help               display this message and exit\n\
41 -o, --output=FILE        set the output file [default=%s]\n\
42     --readonly           do not create FILE, just read it\n\
43     --version            print version information\n\
44 \n\
45 Valid CODECs are: `ascii' `bin'\n", program_name, default_codec, default_outfile);
46     }
47   else
48     {
49       fprintf (stderr, "Type ``%s --help'' for more information.\n",
50 	       program_name);
51     }
52   exit (code);
53 }
54 
55 
56 int
main(int __sane_unused__ arg, char **argv)57 main (int __sane_unused__ arg, char **argv)
58 {
59   SANE_Option_Descriptor desc[2], *desc_ptr;
60   SANE_Word len;
61   char *codec = default_codec;
62   char *outfile = default_outfile;
63   int readonly = 0;
64 
65   program_name = argv[0];
66   argv++;
67   while (*argv != 0)
68     {
69       if (!strcmp (*argv, "--codec"))
70 	{
71 	  if (argv[1] == 0)
72 	    {
73 	      fprintf (stderr, "%s: option `%s' requires an argument\n",
74 		       program_name, *argv);
75 	      usage (1);
76 	    }
77 
78 	  argv++;
79 	  codec = *argv;
80 	}
81       else if (!strncmp (*argv, "--codec=", 8))
82 	{
83 	  codec = *argv + 8;
84 	}
85       else if (!strcmp (*argv, "--help"))
86 	{
87 	  usage (0);
88 	}
89       else if (!strcmp (*argv, "-o") || !strcmp (*argv, "--output"))
90 	{
91 	  if (argv[1] == 0)
92 	    {
93 	      fprintf (stderr, "%s: option `%s' requires an argument\n",
94 		       program_name, *argv);
95 	      usage (1);
96 	    }
97 
98 	  argv++;
99 	  outfile = *argv;
100 	}
101       else if (!strncmp (*argv, "--output=", 9))
102 	{
103 	  outfile = *argv + 9;
104 	}
105       else if (!strcmp (*argv, "--readonly"))
106 	{
107 	  readonly = 1;
108 	}
109       else if (!strcmp (*argv, "--version"))
110 	{
111 	  printf ("test_wire (%s) %s\n", PACKAGE, VERSION);
112 	  exit (0);
113 	}
114       else if (**argv == '-')
115 	{
116 	  fprintf (stderr, "%s: unrecognized option `%s'\n",
117 		   program_name, *argv);
118 	  usage (1);
119 	}
120       else
121 	{
122 	  fprintf (stderr, "%s: too many arguments\n", program_name);
123 	}
124 
125       argv++;
126     }
127 
128 
129   if (!strcmp (codec, "bin"))
130     sanei_w_init (&w, sanei_codec_bin_init);
131   else if (!strcmp (codec, "ascii"))
132     sanei_w_init (&w, sanei_codec_ascii_init);
133   else
134     {
135       fprintf (stderr, "%s: unknown codec type `%s'\n", program_name, codec);
136       usage (1);
137     }
138 
139   desc[0].name = "resolution";
140   desc[0].title = 0;
141   desc[0].desc = "Determines scan resolution in dots/inch (\"DPI\").";
142   desc[0].type = SANE_TYPE_FIXED;
143   desc[0].unit = SANE_UNIT_DPI;
144   desc[0].size = sizeof (SANE_Word);
145   desc[0].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
146   desc[0].constraint_type = SANE_CONSTRAINT_WORD_LIST;
147   desc[0].constraint.word_list = dpi_word_list;
148 
149   desc[1].name = "mode";
150   desc[1].title = "Scan Mode";
151   desc[1].desc = "Determines scan mode.";
152   desc[1].type = SANE_TYPE_STRING;
153   desc[1].unit = SANE_UNIT_NONE;
154   desc[1].size = 10;
155   desc[1].cap = (SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT
156 		 | SANE_CAP_AUTOMATIC);
157   desc[1].constraint_type = SANE_CONSTRAINT_STRING_LIST;
158   desc[1].constraint.string_list = mode_list;
159 
160   {
161     int flags;
162     if (readonly)
163       {
164 	printf ("reading %s output from %s\n", codec, outfile);
165 	flags = O_RDONLY;
166       }
167     else
168       {
169 	printf ("creating %s\n", outfile);
170 	flags = O_RDWR | O_CREAT | O_TRUNC;
171       }
172 
173     w.io.fd = open (outfile, flags, 0666);
174 
175     if (w.io.fd < 0)
176       {
177 	perror (outfile);
178 	return -1;
179       }
180   }
181   w.io.read = read;
182   w.io.write = readonly ? 0 : write;
183 
184   if (!readonly)
185     {
186       sanei_w_set_dir (&w, WIRE_ENCODE);
187       w.status = 0;
188 
189       len = NELEMS (desc);
190       desc_ptr = desc;
191       sanei_w_array (&w, &len, (void **) &desc_ptr,
192 		     (WireCodecFunc) sanei_w_option_descriptor,
193 		     sizeof (desc[0]));
194 
195       if (w.status == 0)
196 	printf ("%s encode successful\n", codec);
197       else
198 	fprintf (stderr, "%s: %s encode error %d: %s\n",
199 		 codec, program_name, w.status, strerror (w.status));
200 
201       printf ("%s output written to %s\n", codec, outfile);
202     }
203 
204   sanei_w_set_dir (&w, WIRE_DECODE);
205   w.status = 0;
206 
207   if (!readonly)
208     {
209       printf ("reading %s output from %s\n", codec, outfile);
210       lseek (w.io.fd, 0, SEEK_SET);
211     }
212 
213   sanei_w_array (&w, &len, (void **) &desc_ptr,
214 		 (WireCodecFunc) sanei_w_option_descriptor, sizeof (desc[0]));
215 
216   if (w.status == 0)
217     printf ("%s decode successful\n", codec);
218   else
219     fprintf (stderr, "%s: %s decode error %d: %s\n",
220 	     program_name, codec, w.status, strerror (w.status));
221 
222   sanei_w_set_dir (&w, WIRE_FREE);
223   w.status = 0;
224   sanei_w_array (&w, &len, (void **) &desc_ptr,
225 		 (WireCodecFunc) sanei_w_option_descriptor, sizeof (desc[0]));
226 
227   if (w.status == 0)
228     printf ("free successful\n");
229   else
230     fprintf (stderr, "%s: free error %d: %s\n",
231 	     program_name, w.status, strerror (w.status));
232 
233   close (w.io.fd);
234 
235   return 0;
236 }
237