summaryrefslogtreecommitdiff
path: root/options.c
diff options
context:
space:
mode:
authorNicolas Schodet2019-09-27 23:18:00 +0200
committerNicolas Schodet2019-11-14 00:41:59 +0100
commit51675360e87297cbf4423a1b12ae0c6a06a92158 (patch)
tree0524d9cd11a89218434f6939bb86a365fa01c644 /options.c
parent7d16e5a24b597fe4982a0583339e869ec6e91544 (diff)
Now camicro, the microscope camera viewer
Diffstat (limited to 'options.c')
-rw-r--r--options.c150
1 files changed, 150 insertions, 0 deletions
diff --git a/options.c b/options.c
new file mode 100644
index 0000000..80c49ed
--- /dev/null
+++ b/options.c
@@ -0,0 +1,150 @@
+/* Camicro - Microscope camera viewer.
+ *
+ * Copyright (C) 2019 Nicolas Schodet
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Contact :
+ * Web: http://ni.fr.eu.org/
+ * Email: <nico at ni.fr.eu.org>
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <getopt.h>
+#include <printf.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "options.h"
+
+/* Report command line usage, optionally prefixed with an error message. */
+static void
+usage(int status, const char *msg)
+{
+ if (msg)
+ fprintf(stderr, "%s: %s", program_invocation_short_name, msg);
+ fprintf(status == EXIT_SUCCESS ? stdout : stderr,
+ "usage: %s [options] [FILE]\n"
+ "\n"
+ "Microscope camera viewer.\n"
+ "\n"
+ "positional arguments:\n"
+ " FILE output file pattern (default: out%%02d.png"
+ " or out for raw output)\n"
+ "\n"
+ "optional arguments:\n"
+ " -h, --help show this help message and exit\n"
+ " -w, --width VALUE image width (512, 1024 or 2048,"
+ " default: 1024)\n"
+ " -e, --exposure MS exposure value (1 to 5000,"
+ " default: 100)\n"
+ " -g, --gain VALUE gain value (0.33 to 42.66,"
+ " default: 1)\n"
+ " -n, --count N number of image to take"
+ " (default: live video)\n"
+ " -r, --raw save raw images\n"
+ , program_invocation_name);
+ exit(status);
+}
+
+void
+options_parse(int argc, char **argv, struct options *options)
+{
+ options->width = 1024;
+ options->height = 768;
+ options->exposure_ms = 100.0;
+ options->gain = 1.0;
+ options->count = 0;
+ options->raw = false;
+ options->out = NULL;
+ char *tail;
+ while (1) {
+ static struct option long_options[] = {
+ { "help", no_argument, 0, 'h' },
+ { "width", required_argument, 0, 'w' },
+ { "exposure", required_argument, 0, 'e' },
+ { "gain", required_argument, 0, 'g' },
+ { "count", required_argument, 0, 'n' },
+ { "raw", required_argument, 0, 'r' },
+ { NULL },
+ };
+ int option_index = 0;
+ int c = getopt_long(argc, argv, "hw:e:g:n:r", long_options,
+ &option_index);
+ if (c == -1)
+ break;
+ switch (c) {
+ case 'h':
+ usage(EXIT_SUCCESS, NULL);
+ break;
+ case 'w':
+ if (strcmp(optarg, "512") == 0) {
+ options->width = 512;
+ options->height = 384;
+ } else if (strcmp(optarg, "1024") == 0) {
+ options->width = 1024;
+ options->height = 768;
+ } else if (strcmp(optarg, "2048") == 0) {
+ options->width = 2048;
+ options->height = 1536;
+ } else
+ usage(EXIT_FAILURE, "bad width value");
+ break;
+ case 'e':
+ errno = 0;
+ options->exposure_ms = strtod(optarg, &tail);
+ if (*tail != '\0' || errno || options->exposure_ms < 1
+ || options->exposure_ms > 5000)
+ usage(EXIT_FAILURE, "bad exposure value");
+ break;
+ case 'g':
+ errno = 0;
+ options->gain = strtod(optarg, &tail);
+ if (*tail != '\0' || errno || options->gain <= 0.0
+ || options->gain >= 43.0)
+ usage(EXIT_FAILURE, "bad gain value");
+ break;
+ case 'n':
+ errno = 0;
+ options->count = strtoul(optarg, &tail, 10);
+ if (*tail != '\0' || errno)
+ usage(EXIT_FAILURE, "bad count value");
+ break;
+ case 'r':
+ options->raw = true;
+ break;
+ case '?':
+ usage(EXIT_FAILURE, NULL);
+ break;
+ default:
+ abort();
+ }
+ }
+ if (optind < argc)
+ options->out = argv[optind++];
+ if (optind < argc)
+ usage(EXIT_FAILURE, "too many arguments");
+ if (!options->out)
+ options->out = options->raw ? "out" : "out%02d.png";
+ if (!options->raw)
+ {
+ int argtypes[1];
+ int formats = parse_printf_format(options->out, 1, argtypes);
+ if (formats != 1 || argtypes[0] != PA_INT)
+ usage(EXIT_FAILURE, "bad file pattern, use one %d");
+ }
+}
+