2 * Copyright (c) 2001-2002 artofcode LLC.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include "ijs_client.h"
32 example_list_params (IjsClientCtx *ctx)
38 status = ijs_client_list_params (ctx, 0, buf, sizeof(buf) - 1);
42 fprintf (stderr, "settable params: %s\n", buf);
46 status = ijs_client_enum_param (ctx, 0, p, ebuf, sizeof(ebuf) - 1);
50 fprintf (stderr, " %s: %s\n", p, ebuf);
54 fprintf (stderr, "Error %d getting param %s\n", status, p);
56 p = strtok(NULL, ",");
61 fprintf (stderr, "Error %d listing params\n", status);
67 send_pnm_file (IjsClientCtx *ctx, FILE *f, int xres, int yres)
71 int total_bytes, bytes_left;
76 lp = fgets (buf, sizeof(buf), f);
79 fprintf (stderr, "error reading file\n");
82 if (lp[0] != 'P' || lp[1] < '4' || lp[1] > '6')
84 fprintf (stderr, "need pnmraw file\n");
90 lp = fgets (buf, sizeof(buf), f);
92 while (lp != NULL && lp[0] == '#');
93 if (sscanf (lp, "%d %d", &width, &height) != 2)
95 fprintf (stderr, "format error\n");
103 lp = fgets (buf, sizeof(buf), f);
105 while (lp != NULL && lp[0] == '#');
108 n_chan = (type == '6') ? 3 : 1;
109 bps = (type == '4') ? 1 : 8;
111 /* Set required parameters. Note: we should be checking the return
113 sprintf (buf, "%d", n_chan);
114 ijs_client_set_param (ctx, 0, "NumChan", buf, strlen (buf));
115 sprintf (buf, "%d", bps);
116 ijs_client_set_param (ctx, 0, "BitsPerSample", buf, strlen (buf));
117 strcpy (buf, (n_chan == 3) ? "DeviceRGB" : "DeviceGray");
118 ijs_client_set_param (ctx, 0, "ColorSpace", buf, strlen (buf));
119 sprintf (buf, "%d", width);
120 ijs_client_set_param (ctx, 0, "Width", buf, strlen (buf));
121 sprintf (buf, "%d", height);
122 ijs_client_set_param (ctx, 0, "Height", buf, strlen (buf));
123 sprintf (buf, "%dx%d", xres, yres);
124 ijs_client_set_param (ctx, 0, "Dpi", buf, strlen (buf));
126 ijs_client_begin_page (ctx, 0);
128 total_bytes = ((n_chan * bps * width + 7) >> 3) * height;
129 bytes_left = total_bytes;
132 int n_bytes = bytes_left;
133 if (n_bytes > sizeof(buf))
134 n_bytes = sizeof(buf);
135 fread (buf, 1, n_bytes, f); /* todo: check error */
138 /* invert pbm so black is 0, as per DeviceGray color space */
140 for (i = 0; i < n_bytes; i++)
143 status = ijs_client_send_data_wait (ctx, 0, buf, n_bytes);
146 bytes_left -= n_bytes;
149 ijs_client_end_page (ctx, 0);
155 verify_context (IjsClientCtx *ctx)
159 fprintf (stderr, "Must specify valid server with -s flag\n");
168 fprintf (stderr, "parameter list must be in key=value, key=value format\n");
172 example_set_params (IjsClientCtx *ctx, const char *arg)
180 for (i = 0; arg[i] != 0; i = inext)
185 for (ibeg = i; arg[ibeg] == ' '; ibeg++);
187 for (ieq = ibeg; arg[ieq] != 0; ieq++)
197 for (iend = ieq; iend >= ibeg; iend--)
198 if (arg[iend - 1] != ' ')
205 key_size = iend - ibeg;
206 if (key_size + 1 > sizeof(key))
208 fprintf (stderr, "Key exceeds %d bytes\n", sizeof(key));
211 memcpy (key, arg + ibeg, key_size);
214 for (i = ieq + 1; arg[i] == ' '; i++);
215 for (; arg[i] != 0; i++)
219 if (buf_ix == sizeof(buf))
221 fprintf (stderr, "Value for %s exceeds %d bytes\n",
225 if (arg[i] == '\\' && arg[i + 1] != 0)
226 buf[buf_ix++] = arg[++i];
228 buf[buf_ix++] = arg[i];
234 code = ijs_client_set_param (ctx, 0, key, buf, buf_ix);
236 fprintf (stderr, "Warning: error %d setting parameter %s\n",
242 example_get_param (IjsClientCtx *ctx, const char *arg)
247 status = ijs_client_get_param (ctx, 0, arg, buf, sizeof(buf) - 1);
251 fprintf (stderr, "value of param %s = %s\n", arg, buf);
255 fprintf (stderr, "Error %d getting param %s\n", status, arg);
260 example_enum_param (IjsClientCtx *ctx, const char *arg)
265 status = ijs_client_enum_param (ctx, 0, arg, buf, sizeof(buf) - 1);
269 fprintf (stderr, "enumeration of param %s: %s\n", arg, buf);
273 fprintf (stderr, "Error %d getting param %s\n", status, arg);
278 get_arg (int argc, char **argv, int *pi, const char *arg)
293 main (int argc, char **argv)
297 int xres = 300, yres = 300;
301 for (i = 1; i < argc; i++)
303 const char *arg = argv[i];
313 arg = get_arg (argc, argv, &i, arg + 2);
314 xres = strtol (arg, &tail, 10);
317 else if (tail[0] == 'x')
318 yres = strtol (tail + 1, &tail, 10);
322 arg = get_arg (argc, argv, &i, arg + 2);
323 ctx = ijs_invoke_server (arg);
324 ijs_client_open (ctx);
325 ijs_client_begin_job (ctx, 0);
328 arg = get_arg (argc, argv, &i, arg + 2);
329 verify_context (ctx);
330 example_set_params (ctx, arg);
333 arg = get_arg (argc, argv, &i, arg + 2);
334 verify_context (ctx);
335 example_get_param (ctx, arg);
338 arg = get_arg (argc, argv, &i, arg + 2);
339 verify_context (ctx);
340 example_enum_param (ctx, arg);
343 verify_context (ctx);
344 example_list_params (ctx);
347 verify_context (ctx);
348 send_pnm_file (ctx, stdin, xres, yres);
354 FILE *f = fopen (arg, "rb");
358 fprintf (stderr, "error opening %s\n", arg);
361 verify_context (ctx);
362 send_pnm_file (ctx, f, xres, yres);
367 verify_context (ctx);
369 ijs_client_end_job (ctx, 0);
370 ijs_client_close (ctx);
372 /* todo - suffield race, proceduralize */
373 ijs_client_begin_cmd (ctx, IJS_CMD_EXIT);
374 ijs_client_send_cmd_wait (ctx);