ui::input: Correct arguments for thread options.
[Ale.git] / ui / input.h
blob251a7f2ea70377a9e9e4955be32d33e6d86a53b0
1 // Copyright 2002, 2003, 2004, 2005, 2006 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>
4 /* This file is part of the Anti-Lamenessing Engine.
6 The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with the Anti-Lamenessing Engine; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef __input_h__
22 #define __input_h__
25 * ANSI C and POSIX include files.
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <assert.h>
32 #include <time.h>
33 #include <math.h>
34 #include <stack>
35 #include <map>
38 * Interface files
41 #include "ui.h"
42 #include "unsupported.h"
43 #include "implication.h"
46 * Configuration
49 #if HAVE_CONFIG_H
50 # include <config.h>
51 #endif
54 * GNU extensions
57 extern "C" {
58 #include "strndup.h"
62 * Types
65 #include "../ale_pos.h"
66 #include "../ale_real.h"
69 * 2D include files
72 #include "../d2.h"
75 * 3D include files
78 #include "../d3.h"
81 * Thread include files
84 #include "../thread.h"
87 * Device configuration files
90 #include "../device/xvp610_320x240.h"
91 #include "../device/xvp610_640x480.h"
92 #include "../device/ov7620_raw_linear.h"
93 #include "../device/canon_300d_raw_linear.h"
94 #include "../device/canon_300d_raw_linear_85mm_1_8.h"
95 #include "../device/canon_300d_raw_linear_50mm_1_8.h"
96 #include "../device/canon_300d_raw_linear_50mm_1_4.h"
97 #include "../device/canon_300d_raw_linear_50mm_1_4_1_4.h"
100 * Help files
103 #include "help.h"
105 class input {
108 * Flag for global options.
111 static int global_options;
114 * Helper functions.
118 * Argument counter.
120 * Counts instances of a given option.
122 static unsigned int arg_count(int argc, const char *argv[], const char *arg) {
123 unsigned int count = 0;
124 for (int i = 0; i < argc; i++) {
125 if (!strcmp(argv[i], arg))
126 count++;
127 else if (!strcmp(argv[i], "--"))
128 return count;
130 return count;
134 * Argument prefix counter.
136 * Counts instances of a given option prefix.
138 static unsigned int arg_prefix_count(int argc, const char *argv[], const char *pfix) {
139 unsigned int count = 0;
140 for (int i = 0; i < argc; i++) {
141 if (!strncmp(argv[i], pfix, strlen(pfix)))
142 count++;
143 else if (!strcmp(argv[i], "--"))
144 return count;
146 return count;
150 * Reallocation function
152 static void *local_realloc(void *ptr, size_t size) {
153 void *new_ptr = realloc(ptr, size);
155 if (new_ptr == NULL)
156 ui::get()->memory_error_location("main()");
158 return new_ptr;
162 * Not enough arguments function.
164 static void not_enough(const char *opt_name) {
165 ui::get()->cli_not_enough(opt_name);
169 * Bad argument function
171 static void bad_arg(const char *opt_name) {
172 ui::get()->cli_bad_arg(opt_name);
176 * String comparison class.
179 class compare_strings {
180 public:
181 int operator()(const char *A, const char *B) const {
182 return strcmp(A, B) < 0;
187 * Environment structures.
189 * XXX: It's arguable that these should be public members of the
190 * 'input' class in order to allow passing environment values to other
191 * classes, but, since we're currently using them only to prepare state
192 * for an internal 'input' function, they can stay private for now. A
193 * more nuanced approach will likely be required later.
196 class environment {
197 static std::stack<environment *> environment_stack;
198 static std::set<environment *> environment_set;
200 std::map<const char *, const char *, compare_strings> environment_map;
203 * Internal set operations do not protect any data.
206 void internal_set(const char *name, const char *value) {
207 environment_map[name] = value;
210 void internal_unset(const char *name) {
211 environment_map.erase(name);
214 const char *internal_convert_pointer(const void *pointer) {
215 int chars = sizeof(void *) * 2 + 3;
216 char *c = (char *) malloc(sizeof(char) * chars);
218 assert(c);
220 if (!c)
221 ui::get()->memory_error_location("environment::set_ptr");
223 int count = snprintf(c, chars, "%p", pointer);
225 assert (count >= 0 && count < chars);
227 return c;
230 void internal_set_ptr(const char *name, const void *pointer) {
231 internal_set(name, internal_convert_pointer(pointer));
235 * Check for restricted names.
238 int name_ok(const char *name) {
239 if (!strcmp(name, "---chain") || !strcmp(name, "---this"))
240 return 0;
242 return 1;
245 void name_check(const char *name) {
246 if (!name_ok(name)) {
247 fprintf(stderr, "Bad set operation.");
248 assert(0);
249 exit(1);
253 public:
256 * Get the environment map.
259 std::map<const char *, const char *, compare_strings> &get_map() {
260 return environment_map;
264 * Public set operations restrict valid names.
267 void set(const char *name, const char *value) {
268 name_check(name);
269 internal_set(name, value);
272 void unset(const char *name) {
273 name_check(name);
274 internal_unset(name);
277 void set_ptr(const char *name, const void *pointer) {
278 name_check(name);
279 internal_set_ptr(name, pointer);
282 const char *get(const char *name) {
283 if (environment_map.count(name) == 0)
284 return NULL;
286 return environment_map[name];
290 * Make an environment substructure. Note that since deep
291 * structures are currently referenced rather than copied when
292 * the stack is pushed, there is no current need for any
293 * chaining mechanism.
295 void make_substructure(const char *name) {
296 environment *s = new environment;
297 set_ptr(name, s);
298 environment_set.insert(s);
301 static int is_env(const char *name) {
302 void *ptr_value;
303 sscanf(name, "%p", &ptr_value);
306 * Check for bad pointers.
309 if (!environment_set.count((environment *) ptr_value)) {
310 return 0;
313 return 1;
316 const char *get_option_name(const char *name) {
317 if (strncmp(name, "0 ", strlen("0 ")))
318 return NULL;
320 name += strlen("0 ");
322 if (!isdigit(name[0]))
323 return NULL;
325 while (isdigit(name[0]))
326 name++;
328 if (!isspace(name[0]))
329 return NULL;
331 while (isspace(name[0]))
332 name++;
334 if (!isalnum(name[0]))
335 return NULL;
337 return name;
340 int is_option(const char *name) {
341 return (get_option_name(name) != NULL);
344 int is_arg(const char *name, unsigned int arg) {
345 assert (is_option(name));
347 int length = strlen(name) + 3 * sizeof(unsigned int);
349 char *desired_string = (char *) malloc(sizeof(char) * length);
351 snprintf(desired_string, length, "%u %s", arg, name + strlen("0 "));
353 int result = environment_map.count(desired_string);
355 free(desired_string);
357 return result > 0;
360 void remove_arg(const char *name, unsigned int arg) {
361 assert (is_option(name));
362 assert (is_arg(name, arg));
364 int length = strlen(name) + 3 * sizeof(unsigned int);
366 char *desired_string = (char *) malloc(sizeof(char) * length);
368 snprintf(desired_string, length, "%u %s", arg, name + strlen("0 "));
370 environment_map.erase(desired_string);
373 const char *get_string_arg(const char *name, unsigned int arg) {
374 assert (is_option(name));
376 int length = strlen(name) + 3 * sizeof(unsigned int);
378 char *desired_string = (char *) malloc(sizeof(char) * length);
380 snprintf(desired_string, length, "%u %s", arg, name + strlen("0 "));
382 const char *result = environment_map[desired_string];
384 assert (result);
386 free(desired_string);
388 return result;
391 long int get_long_arg(const char *name, unsigned int arg) {
392 assert (is_option(name));
394 const char *string = get_string_arg(name, arg);
395 char *endptr;
397 long int result = strtol(string, &endptr, 0);
399 if (endptr[0] != '\0') {
400 fprintf(stderr, "\n\nError: bad argument in `%s'.\n\n", get_option_name(name));
401 exit(1);
404 return result;
407 int get_int_arg(const char *name, unsigned int arg) {
408 return (int) get_long_arg(name, arg);
411 unsigned int get_unsigned_arg(const char *name, unsigned int arg) {
412 long int result = get_long_arg(name, arg);
414 if (result < 0) {
415 fprintf(stderr, "\n\nError: bad argument in `%s'.\n\n", get_option_name(name));
416 exit(1);
419 return (unsigned int) result;
422 double get_double_arg(const char *name, unsigned int arg) {
423 assert (is_option(name));
425 const char *string = get_string_arg(name, arg);
426 char *endptr;
428 double result = strtod(string, &endptr);
430 if (endptr[0] != '\0') {
431 fprintf(stderr, "\n\nError: bad argument in `%s'.\n\n", get_option_name(name));
432 exit(1);
435 return result;
438 static environment *get_env(const char *name) {
440 assert(name);
442 void *ptr_value;
443 sscanf(name, "%p", &ptr_value);
446 * Check for bad pointers.
449 if (!environment_set.count((environment *) ptr_value)) {
450 assert(0);
451 fprintf(stderr, "Bad environment pointer.\n");
452 exit(1);
455 return (environment *) ptr_value;
459 * Prepend to a list.
461 void prepend(const char *list, const char *element) {
462 environment *d = get_env(get(list));
463 make_substructure(list);
464 get_env(get(list))->set("a", element);
465 get_env(get(list))->set_ptr("d", d);
468 void prepend_ptr(const char *list, void *ptr) {
469 prepend(list, internal_convert_pointer(ptr));
473 * Clone the environment.
475 environment *clone() {
476 environment *e = new environment();
478 for (std::map<const char *, const char *, compare_strings>::iterator i = environment_map.begin();
479 i != environment_map.end(); i++) {
481 if (!name_ok(i->first))
482 continue;
484 if (is_env(i->second)) {
485 e->set_ptr(i->first, get_env(i->second)->clone());
486 } else {
487 e->set(i->first, i->second);
491 return e;
494 static environment *top() {
495 if (environment_stack.empty()) {
496 environment_stack.push(new environment);
497 environment_set.insert(environment_stack.top());
499 return environment_stack.top();
502 static void push() {
503 environment *e = new environment;
505 e->environment_map = environment_stack.top()->environment_map;
507 e->internal_set_ptr("---chain", environment_stack.top());
508 e->internal_set_ptr("---this", e);
509 e->make_substructure("---dup");
511 environment_stack.push(e);
512 environment_set.insert(e);
515 static void dup_second() {
516 environment_stack.top()->prepend_ptr("---dup",
517 environment::get_env(environment_stack.top()->get("---chain")));
520 static void push_and_dup_output() {
521 push();
522 dup_second();
525 static void pop() {
526 assert(!environment_stack.empty());
529 * Execution environments should never be referenced by
530 * structures further up the call chain, so they can
531 * safely be deleted. (XXX: In particular, while
532 * lexical scoping may require copying of execution
533 * environments from lower on the call chain, there is
534 * no obvious reason that a reference should be used in
535 * this case; a shallow copy should be used instead.)
538 environment_set.erase(environment_stack.top());
539 delete environment_stack.top();
541 environment_stack.pop();
545 * Set with duplication.
548 void set_with_dup(const char *name, const char *value) {
549 set(name, value);
551 if (!get("---dup"))
552 return;
554 environment *dup_item = get_env(get("---dup"));
556 assert (dup_item);
558 while (dup_item->get("a")) {
559 get_env(dup_item->get("a"))->set_with_dup(name, value);
560 assert(dup_item->get("d"));
561 dup_item = get_env(dup_item->get("d"));
562 assert(dup_item);
568 * Read tokens from a stream.
570 class token_reader {
571 public:
573 * Get the next token
575 virtual const char *get() = 0;
578 * Peek at the next token.
581 virtual const char *peek() = 0;
584 * Divert the stream until the next occurrence of TOKEN.
586 virtual token_reader *divert(const char *open_token, const char *close_token) = 0;
588 virtual int expects_exactly_one_option(void) {
589 return 0;
592 virtual ~token_reader() {
596 class argument_parsing_token_reader : public token_reader {
597 const char *index;
598 const char *separators;
599 public:
600 argument_parsing_token_reader(const char *s) {
601 index = s;
602 separators = "=";
605 int expects_exactly_one_option(void) {
606 return 1;
609 virtual const char *get() {
610 int length = strcspn(index, separators);
612 if (length == 0)
613 return NULL;
615 const char *result = strndup(index, length);
616 index += length;
618 if (strspn(index, separators) >= 1)
619 index++;
621 separators = ",";
623 return result;
626 virtual const char *peek() {
627 int length = strcspn(index, separators);
629 if (length == 0)
630 return NULL;
632 const char *result = strndup(index, length);
634 return result;
637 virtual token_reader *divert(const char *open_token, const char *close_token) {
638 assert(0);
639 return NULL;
643 class cstring_token_reader : public token_reader {
644 const char *separators;
645 const char *string;
646 int ephemeral;
648 cstring_token_reader(const char *s, int ephemeral) {
649 assert(ephemeral == 1);
651 separators = "\n \t";
652 string = s;
653 this->ephemeral = 1;
656 public:
657 cstring_token_reader(const char *s) {
658 separators = "\n \t";
659 string = s;
660 ephemeral = 0;
663 const char *get() {
665 string += strspn(string, separators);
667 size_t length_to_next = strcspn(string, separators);
669 if (length_to_next == 0)
670 return NULL;
672 const char *result = strndup(string, length_to_next);
674 string += length_to_next;
676 return result;
679 const char *peek() {
680 string += strspn(string, separators);
682 size_t length_to_next = strcspn(string, separators);
684 if (length_to_next == 0)
685 return NULL;
687 return strndup(string, length_to_next);
690 cstring_token_reader *divert(const char *open_token, const char *close_token) {
692 * This function might be broken.
695 assert(0);
697 int search = 0;
698 int next = strcspn(string, separators);
699 int depth = 0;
701 while (*(string + search) != '\0' &&
702 (depth || strcmp(close_token, (string + search)))) {
703 if (!strcmp(close_token, (string + search)))
704 depth--;
705 if (!strcmp(open_token, (string + search)))
706 depth++;
707 search = next;
708 next = strcspn((string + next), separators);
711 if (*(string + search) == '\0') {
712 fprintf(stderr, "Parse error: End of scope not found.");
713 exit(1);
716 cstring_token_reader *result = new cstring_token_reader(strndup(string, search), 1);
718 string += search;
721 * Eat the closing token.
724 get();
726 return result;
729 ~cstring_token_reader() {
730 if (ephemeral)
731 free((void *) string);
735 class cli_token_reader : public token_reader {
737 int arg_index;
738 int argc;
739 const char **argv;
741 public:
742 cli_token_reader(int c, const char *v[]) {
743 argc = c;
744 argv = v;
745 arg_index = 0;
748 const char *get() {
750 if (arg_index < argc)
751 return argv[arg_index++];
752 else
753 return NULL;
757 const char *peek() {
759 if (arg_index < argc)
760 return argv[arg_index];
761 else
762 return NULL;
766 cli_token_reader *divert(const char *open_token, const char *close_token) {
767 int search = 0;
768 int depth = 0;
770 while (arg_index + search < argc
771 && (depth || strcmp(argv[arg_index + search], close_token))) {
772 if (!strcmp(close_token, argv[arg_index + search]))
773 depth--;
774 if (!strcmp(open_token, argv[arg_index + search]))
775 depth++;
776 search++;
779 if (arg_index + search == argc) {
780 fprintf(stderr, "Parse error: end of scope not found.\n");
781 exit(1);
784 cli_token_reader *result = new cli_token_reader(search, argv + arg_index);
786 arg_index += search;
789 * Eat the closing token.
792 get();
794 return result;
799 struct simple_option {
800 const char *name;
801 const char *map_name;
802 const char *map_value;
803 int arg_count;
804 int multi;
807 static const char *supported_nonglobal_option_table[];
808 static const char *focus_prefixes[];
809 static simple_option simple_option_table[];
811 static int option_name_match(const char *unadorned, const char *token, int require_ornamentation = 1) {
812 int strip_max = 2;
814 if (!strcmp(unadorned, token) && !require_ornamentation)
815 return 1;
817 while (token[0] == '-' && strip_max) {
818 token++;
819 strip_max--;
820 if (!strcmp(unadorned, token))
821 return 1;
824 return 0;
827 static int is_scope_operator(const char *string) {
828 if (!strcmp("{", string)
829 || !strcmp("}", string)
830 || !strcmp("[", string)
831 || !strcmp("]", string)
832 || !strcmp("<", string)
833 || !strcmp(">", string))
834 return 1;
836 return 0;
839 static const char *option_name_gen(const char *unadorned, const char *map_name, int arg_num, int multi) {
840 static unsigned int multi_counter = 0;
842 if (map_name) {
843 unadorned = map_name;
846 int length = (strlen(unadorned) + sizeof(unsigned int) * 3 + sizeof(int) * 3 + 2) + 1;
848 char *result = (char *) malloc(sizeof(char) * length);
850 assert (result);
852 if (!multi) {
853 snprintf(result, length, "%u 0 %s", arg_num, unadorned);
854 } else {
857 * XXX: This assumes that generating calls for
858 * options other than 0 exist in the same
859 * multiplicity group as the most recently
860 * generated 0-option multiplicity.
863 if (arg_num == 0)
864 multi_counter++;
866 snprintf(result, length, "%u %u %s", arg_num, multi_counter, unadorned);
869 return result;
872 static environment *genv;
874 static const char *get_next(token_reader *tr, const char *option_name) {
875 const char *argument = tr->get();
877 if (argument == NULL) {
878 fprintf(stderr, "\n\nError: not enough arguments for `%s'.\n\n", option_name);
879 exit(1);
882 return argument;
885 static int table_contains(const char **haystack, const char *needle, int prefix_length = 0) {
887 if (needle == NULL)
888 return 0;
890 while (*haystack != NULL) {
891 if (prefix_length == 0 && !strcmp(*haystack, needle))
892 return 1;
893 if (prefix_length > 0 && !strncmp(*haystack, needle, prefix_length))
894 return 1;
895 haystack++;
898 return 0;
901 static int option_is_identical(environment *a, environment *b, const char *option_name) {
902 if (!a->is_option(option_name) || !b->is_option(option_name))
903 return 0;
905 int option_number = 0;
907 while (a->is_arg(option_name, option_number) || b->is_arg(option_name, option_number)) {
908 if (!a->is_arg(option_name, option_number)
909 || !b->is_arg(option_name, option_number))
910 return 0;
912 const char *a_str = a->get_string_arg(option_name, option_number);
913 const char *b_str = b->get_string_arg(option_name, option_number);
915 if (strcmp(a_str, b_str))
916 return 0;
918 option_number++;
921 return 1;
924 static void remove_option(environment *a, const char *option_name) {
925 assert(a->is_option(option_name));
927 int option_number = 0;
929 while (a->is_arg(option_name, option_number)) {
930 a->remove_arg(option_name, option_number);
931 option_number++;
935 static void remove_nonglobals(environment *a) {
936 assert(a);
938 std::stack<const char *> removal_stack;
940 for (std::map<const char *, const char *, compare_strings>::iterator i = a->get_map().begin();
941 i != a->get_map().end(); i++) {
943 if (!a->is_option(i->first))
944 continue;
946 if (!table_contains(supported_nonglobal_option_table, a->get_option_name(i->first)))
947 continue;
949 removal_stack.push(i->first);
952 while (!removal_stack.empty()) {
953 remove_option(a, removal_stack.top());
954 removal_stack.pop();
958 static void option_intersect(environment *a, environment *b) {
959 assert(a);
960 assert(b);
962 std::stack<const char *> removal_stack;
964 for (std::map<const char *, const char *, compare_strings>::iterator i = a->get_map().begin();
965 i != a->get_map().end(); i++) {
967 if (!a->is_option(i->first))
968 continue;
970 if (option_is_identical(a, b, i->first))
971 continue;
973 removal_stack.push(i->first);
976 while (!removal_stack.empty()) {
977 remove_option(a, removal_stack.top());
978 removal_stack.pop();
982 static void option_difference(environment *a, environment *b) {
983 assert(a);
984 assert(b);
986 std::stack<const char *> removal_stack;
988 for (std::map<const char *, const char *, compare_strings>::iterator i = a->get_map().begin();
989 i != a->get_map().end(); i++) {
991 if (!a->is_option(i->first))
992 continue;
994 if (!option_is_identical(a, b, i->first))
995 continue;
997 removal_stack.push(i->first);
1000 while (!removal_stack.empty()) {
1001 remove_option(a, removal_stack.top());
1002 removal_stack.pop();
1006 static void evaluate_stream(token_reader *tr,
1007 std::vector<std::pair<const char *, environment *> > *files) {
1008 const char *token;
1009 int end_of_options = 0;
1011 while ((token = tr->get())) {
1014 * Check for nesting
1017 if (!strcmp(token, "{") && !end_of_options) {
1018 environment::push_and_dup_output();
1019 token_reader *tr_nest = tr->divert("{", "}");
1020 evaluate_stream(tr_nest, files);
1021 delete tr_nest;
1022 environment::pop();
1023 } else if (!strcmp(token, "[") && !end_of_options) {
1024 global_options = 0;
1025 environment::push();
1026 token_reader *tr_nest = tr->divert("[", "]");
1027 evaluate_stream(tr_nest, files);
1028 delete tr_nest;
1029 environment::pop();
1030 } else if (!strcmp(token, "<") && !end_of_options) {
1031 environment *dup_list = environment::get_env(environment::top()->get("---dup"));
1032 assert (dup_list != NULL);
1033 dup_list = dup_list->clone();
1035 environment::dup_second();
1036 token_reader *tr_nest = tr->divert("<", ">");
1037 evaluate_stream(tr_nest, files);
1038 delete tr_nest;
1040 environment::top()->set_ptr("---dup", dup_list);
1044 * Check for non-whitespace argument separators
1047 else if (!end_of_options && token && token[0] == '-' && strchr(token, '=')) {
1048 environment::push_and_dup_output();
1049 token_reader *tr_nest = new argument_parsing_token_reader(token);
1050 evaluate_stream(tr_nest, files);
1051 delete tr_nest;
1052 environment::pop();
1056 * Trap the end-of-option indicator.
1059 else if (!strcmp(token, "--")) {
1060 global_options = 0;
1061 end_of_options = 1;
1065 * Check for options and filenames
1068 else {
1070 * Handle filenames.
1073 if (strncmp("-", token, strlen("-")) || end_of_options) {
1075 assert(files);
1077 global_options = 0;
1078 files->push_back(std::pair<const char *, environment *>(strdup(token),
1079 environment::top()->clone()));
1081 if (tr->expects_exactly_one_option() && tr->get()) {
1082 fprintf(stderr, "\n\nError: Too many arguments for `%s'.\n\n", token);
1083 exit(1);
1086 continue;
1090 * Handle focus option.
1093 if (option_name_match("focus", token)) {
1095 environment *target;
1096 target = environment::top();
1098 target->set_with_dup(option_name_gen("focus", NULL, 0, 0), "1");
1100 const char *option = get_next(tr, "focus");
1102 target->set_with_dup(option_name_gen("focus", NULL, 1, 0), option);
1104 if (!strcmp(option, "d")) {
1105 target->set_with_dup(option_name_gen("focus", NULL, 2, 0),
1106 get_next(tr, "focus"));
1107 } else if (!strcmp(option, "p")) {
1108 target->set_with_dup(option_name_gen("focus", NULL, 2, 0),
1109 get_next(tr, "focus"));
1110 target->set_with_dup(option_name_gen("focus", NULL, 3, 0),
1111 get_next(tr, "focus"));
1112 } else
1113 bad_arg("focus");
1115 int arg = 0;
1117 while (table_contains(focus_prefixes, tr->peek(), 3)) {
1118 target->set_with_dup(option_name_gen("focus", NULL, 4 + arg, 0),
1119 get_next(tr, "focus"));
1120 arg++;
1123 continue;
1127 * Handle simple options.
1130 int found_option = 0;
1131 for (int i = 0; simple_option_table[i].name; i++) {
1132 if (!option_name_match(simple_option_table[i].name, token))
1133 continue;
1136 * Handle the match case.
1139 found_option = 1;
1142 * Determine which environment should be modified
1145 environment *target;
1146 target = environment::top();
1149 * Store information required for
1150 * handling the local case later.
1153 const char *map_value = "1";
1155 if (simple_option_table[i].map_value) {
1156 map_value = simple_option_table[i].map_value;
1157 } else if (simple_option_table[i].map_name) {
1158 map_value = simple_option_table[i].name;
1161 target->set_with_dup(option_name_gen(simple_option_table[i].name,
1162 simple_option_table[i].map_name,
1164 simple_option_table[i].multi),
1165 map_value);
1167 for (int j = 0; j < simple_option_table[i].arg_count; j++) {
1168 const char *option = tr->get();
1170 if (option == NULL) {
1171 fprintf(stderr, "\n\nError: not enough options for `%s'.\n\n", token);
1172 exit(1);
1176 * Reject scope operators as options,
1177 * at least for now.
1180 if (is_scope_operator(option)) {
1181 fprintf(stderr, "\n\nError: illegal argument to `%s'.\n\n", token);
1182 exit(1);
1185 target->set_with_dup(option_name_gen(simple_option_table[i].name,
1186 simple_option_table[i].map_name,
1187 j + 1,
1188 simple_option_table[i].multi),
1189 option);
1194 * Trap illegal options.
1197 if (!found_option)
1198 ui::get()->illegal_option(token);
1201 if (tr->expects_exactly_one_option() && tr->get()) {
1202 fprintf(stderr, "\n\nError: Too many arguments for `%s'.\n\n", token);
1203 exit(1);
1208 public:
1210 * Input handler.
1212 * Does one of two things:
1214 * (1) Output version information if called with '--version'
1216 * (2) Read options and file arguments, and if the arguments are correct,
1217 * write output. If an error is detected, print the usage statement.
1221 static void handle(int argc, const char *argv[], const char *package, const char *short_version, const char *version) {
1224 * Initialize help object
1227 help hi(package, argv[0], short_version);
1230 * Output version information if --version appears
1231 * on the command line.
1234 if (arg_count(argc, argv, "--version")) {
1236 * Output the version
1239 fprintf(stdout, "%s", version);
1241 return;
1245 * Handle help options
1248 if (arg_prefix_count(argc, argv, "--h"))
1249 for (int i = 1; i < argc; i++) {
1250 int all = !strcmp(argv[i], "--hA");
1251 int is_help_option = !strncmp(argv[i], "--h", strlen("--h"));
1252 int found_help = 0;
1254 if (!strcmp(argv[i], "--hu") || all)
1255 hi.usage(), found_help = 1;
1256 if (!strcmp(argv[i], "--hq") || all)
1257 hi.defaults(), found_help = 1;
1258 if (!strcmp(argv[i], "--hf") || all)
1259 hi.file(), found_help = 1;
1260 if (!strcmp(argv[i], "--he") || all)
1261 hi.exclusion(), found_help = 1;
1262 if (!strcmp(argv[i], "--ha") || all)
1263 hi.alignment(), found_help = 1;
1264 if (!strcmp(argv[i], "--hr") || all)
1265 hi.rendering(), found_help = 1;
1266 if (!strcmp(argv[i], "--hx") || all)
1267 hi.exposure(), found_help = 1;
1268 if (!strcmp(argv[i], "--ht") || all)
1269 hi.tdf(), found_help = 1;
1270 if (!strcmp(argv[i], "--hl") || all)
1271 hi.filtering(), found_help = 1;
1272 if (!strcmp(argv[i], "--hd") || all)
1273 hi.device(), found_help = 1;
1274 if (!strcmp(argv[i], "--hi") || all)
1275 hi.interface(), found_help = 1;
1276 if (!strcmp(argv[i], "--hv") || all)
1277 hi.visp(), found_help = 1;
1278 if (!strcmp(argv[i], "--hc") || all)
1279 hi.cp(), found_help = 1;
1280 if (!strcmp(argv[i], "--h3") || all)
1281 hi.d3(), found_help = 1;
1282 if (!strcmp(argv[i], "--hp") || all)
1283 hi.parallel(), found_help = 1;
1284 if (!strcmp(argv[i], "--hz") || all)
1285 hi.undocumented(), found_help = 1;
1287 if (is_help_option && !found_help)
1288 hi.usage();
1291 * Check for the end-of-options marker, a non-option argument,
1292 * or the end of arguments. In all of these cases, we exit.
1295 if (!strcmp(argv[i], "--")
1296 || strncmp(argv[i], "--", strlen("--"))
1297 || i == argc - 1)
1298 return;
1302 * Undocumented projective transformation utility
1305 if (arg_count(argc, argv, "--ptcalc") > 0) {
1306 fprintf(stderr, "\n\n*** Warning: this feature is not documented ***\n\n");
1307 printf("Enter: w h tlx tly blx bly brx bry trx try x y\n\n");
1309 double w, h, tlx, tly, blx, bly, brx, bry, trx, tr_y, x, y;
1311 printf("> ");
1313 if (scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
1314 &w, &h, &tlx, &tly, &blx, &bly, &brx, &bry, &trx, &tr_y, &x, &y) != 12) {
1316 fprintf(stderr, "Error reading input.\n");
1317 exit(1);
1320 d2::image *i = new d2::image_ale_real((int)h, (int)w, 3);
1321 d2::transformation t = d2::transformation::gpt_identity(i, 1);
1322 d2::point q[4] = {
1323 d2::point(tly, tlx),
1324 d2::point(bly, blx),
1325 d2::point(bry, brx),
1326 d2::point(tr_y, trx)
1328 t.gpt_set(q);
1330 d2::point a(y, x), b;
1332 b = t.transform_scaled(a);
1334 printf("TRANSFORM t(a): (%f, %f)\n", (double) b[1], (double) b[0]);
1336 b = t.scaled_inverse_transform(a);
1338 printf("INVERSE t^-1(a): (%f, %f)\n", (double) b[1], (double) b[0]);
1340 exit(0);
1344 * Thread initialization.
1347 thread::init();
1350 * Flags and variables
1353 double scale_factor = 1;
1354 double vise_scale_factor = 1;
1355 #if 0
1356 double usm_multiplier = 0.0;
1357 #endif
1358 int extend = 0;
1359 struct d2::tload_t *tload = NULL;
1360 struct d2::tsave_t *tsave = NULL;
1361 struct d3::tload_t *d3_tload = NULL;
1362 struct d3::tsave_t *d3_tsave = NULL;
1363 int ip_iterations = 0;
1364 int ip_use_median = 0;
1365 enum { psf_linear, psf_nonlinear, psf_N };
1366 const char *psf[psf_N] = {NULL, NULL};
1367 const char *device = NULL;
1368 int psf_match = 0;
1369 double psf_match_args[6];
1370 int inc = 1;
1371 int exposure_register = 1;
1372 const char *wm_filename = NULL;
1373 int wm_offsetx = 0, wm_offsety = 0;
1374 double cx_parameter = 0;
1375 double *d3px_parameters = NULL;
1376 int d3px_count = 0;
1377 d2::exclusion *ex_parameters = NULL;
1378 int ex_count = 0;
1379 int ex_show = 0;
1380 d2::render *achain;
1381 const char *achain_type = "triangle:2";
1382 const char *afilter_type = "internal";
1383 d2::render **ochain = NULL;
1384 const char **ochain_names = NULL;
1385 const char **ochain_types = NULL;
1386 const char *d3chain_type = NULL;
1387 int oc_count = 0;
1388 const char **visp = NULL;
1389 int vise_count = 0;
1390 const char **d3_output = NULL;
1391 const char **d3_depth = NULL;
1392 unsigned int d3_count = 0;
1393 double user_view_angle = 0;
1394 int user_bayer = IMAGE_BAYER_DEFAULT;
1395 d2::pixel exp_mult = d2::pixel(1, 1, 1);
1396 std::map<const char *, d3::pt> d3_output_pt;
1397 std::map<const char *, d3::pt> d3_depth_pt;
1400 * dchain is ochain[0].
1403 ochain = (d2::render **) local_realloc(ochain,
1404 (oc_count + 1) * sizeof(d2::render *));
1405 ochain_names = (const char **) local_realloc((void *)ochain_names,
1406 (oc_count + 1) * sizeof(const char *));
1407 ochain_types = (const char **) local_realloc((void *)ochain_types,
1408 (oc_count + 1) * sizeof(const char *));
1410 ochain_types[0] = "sinc*lanc:8";
1412 oc_count = 1;
1415 * Handle default settings
1418 if (arg_prefix_count(argc, argv, "--q") > 1)
1419 ui::get()->error("more than one default setting option --q* was specified");
1421 const char *defaults[] = {
1422 "--dchain fine:box:1,triangle:2 "
1423 "--achain triangle:2 "
1424 "--ips 0 "
1425 "--exp-noextend "
1426 "--no-cx "
1427 "--3d-chain fine:box:1,triangle:2",
1429 "--dchain sinc*lanc:6 "
1430 "--achain sinc*lanc:6 "
1431 "--ips 0 "
1432 "--exp-noextend "
1433 "--no-cx "
1434 "--3d-chain sinc*lanc:6 ",
1436 "--dchain median:fine:sinc*lanc:8,triangle:2 "
1437 "--achain triangle:2 "
1438 "--ips 0 "
1439 "--exp-noextend "
1440 "--no-cx "
1441 "--3d-chain median:fine:sinc*lanc:8,triangle:2 ",
1443 "--dchain sinc*lanc:8 "
1444 "--achain sinc*lanc:8 "
1445 "--ips 4 "
1446 "--exp-noextend "
1447 "--no-cx "
1448 "--3d-chain sinc*lanc:8",
1450 "--dchain sinc*lanc:8 "
1451 "--achain sinc*lanc:8 "
1452 "--ips 6 "
1453 "--exp-extend "
1454 "--cx 0.7 "
1455 "--3d-chain sinc*lanc:8"
1458 int default_index;
1459 if (arg_count(argc, argv, "--q0")) {
1460 default_index = 0;
1461 } else if (arg_count(argc, argv, "--qn")) {
1462 default_index = 1;
1463 } else if (arg_count(argc, argv, "--q1")) {
1464 default_index = 2;
1465 } else if (arg_count(argc, argv, "--q2")) {
1466 default_index = 3;
1467 } else if (arg_count(argc, argv, "--qr")) {
1468 default_index = 4;
1469 } else {
1471 * Same as --q0
1473 default_index = 0;
1476 token_reader *default_reader = new cstring_token_reader(defaults[default_index]);
1478 evaluate_stream(default_reader, NULL);
1481 * Set basic program information in the environment.
1484 environment::top()->set_with_dup("---package", package);
1485 environment::top()->set_with_dup("---short-version", short_version);
1486 environment::top()->set_with_dup("---version", version);
1487 environment::top()->set_with_dup("---invocation", argv[0]);
1490 * Initialize the top-level token-reader and generate
1491 * an environment variable for it.
1494 token_reader *tr = new cli_token_reader(argc - 1, argv + 1);
1495 environment::top()->set_ptr("---token-reader", tr);
1498 * Evaluate the command-line arguments to generate environment
1499 * structures.
1502 std::vector<std::pair<const char *, environment *> > files;
1504 evaluate_stream(tr, &files);
1507 * If there are fewer than two files, then output usage information.
1510 if (files.size() < 2) {
1511 hi.usage();
1512 exit(1);
1516 * Extract the global environment and check non-globals
1517 * against a list of supported non-global options.
1520 genv = files[0].second->clone();
1522 remove_nonglobals(genv);
1524 for (unsigned int i = 0; i < files.size(); i++) {
1525 option_intersect(genv, files[i].second);
1528 for (unsigned int i = 0; i < files.size(); i++) {
1529 option_difference(files[i].second, genv);
1531 for (std::map<const char *, const char *>::iterator j = files[i].second->get_map().begin();
1532 j != files[i].second->get_map().end(); j++) {
1534 environment *env = files[i].second;
1536 if (!env->is_option(j->first))
1537 continue;
1539 const char *option_name = env->get_option_name(j->first);
1541 if (!table_contains(supported_nonglobal_option_table, option_name)) {
1542 fprintf(stderr, "\n\nError: option `%s' must be applied globally.", option_name);
1543 fprintf(stderr, "\n\nHint: Move option `%s' prior to file and scope operators.\n\n",
1544 option_name);
1545 exit(1);
1551 * Iterate through the global environment,
1552 * looking for options.
1555 for (std::map<const char *, const char *>::iterator i = genv->get_map().begin();
1556 i != genv->get_map().end(); i++) {
1558 environment *env = genv;
1560 if (!env->is_option(i->first))
1561 continue;
1563 const char *option_name = env->get_option_name(i->first);
1565 if (!strcmp(option_name, "default")) {
1567 * Do nothing. Defaults have already been set.
1569 } else if (!strcmp(option_name, "bpc")) {
1570 if (!strcmp(env->get_string_arg(i->first, 0), "8bpc"))
1571 d2::image_rw::depth8();
1572 else if (!strcmp(env->get_string_arg(i->first, 0), "16bpc"))
1573 d2::image_rw::depth16();
1574 else
1575 assert(0);
1576 } else if (!strcmp(option_name, "format")) {
1577 if (!strcmp(env->get_string_arg(i->first, 0), "plain"))
1578 d2::image_rw::ppm_plain();
1579 else if (!strcmp(env->get_string_arg(i->first, 0), "raw"))
1580 d2::image_rw::ppm_raw();
1581 else if (!strcmp(env->get_string_arg(i->first, 0), "auto"))
1582 d2::image_rw::ppm_auto();
1583 else
1584 assert(0);
1585 } else if (!strcmp(option_name, "align")) {
1586 if (!strcmp(env->get_string_arg(i->first, 0), "align-all"))
1587 d2::align::all();
1588 else if (!strcmp(env->get_string_arg(i->first, 0), "align-green"))
1589 d2::align::green();
1590 else if (!strcmp(env->get_string_arg(i->first, 0), "align-sum"))
1591 d2::align::sum();
1592 else
1593 assert(0);
1594 } else if (!strcmp(option_name, "transformation")) {
1595 if (!strcmp(env->get_string_arg(i->first, 0), "translation"))
1596 d2::align::class_translation();
1597 else if (!strcmp(env->get_string_arg(i->first, 0), "euclidean"))
1598 d2::align::class_euclidean();
1599 else if (!strcmp(env->get_string_arg(i->first, 0), "projective"))
1600 d2::align::class_projective();
1601 else
1602 assert(0);
1603 } else if (!strcmp(option_name, "transformation-default")) {
1604 if (!strcmp(env->get_string_arg(i->first, 0), "identity"))
1605 d2::align::initial_default_identity();
1606 else if (!strcmp(env->get_string_arg(i->first, 0), "follow"))
1607 d2::align::initial_default_follow();
1608 else
1609 assert(0);
1610 } else if (!strcmp(option_name, "perturb")) {
1611 if (!strcmp(env->get_string_arg(i->first, 0), "perturb-output"))
1612 d2::align::perturb_output();
1613 else if (!strcmp(env->get_string_arg(i->first, 0), "perturb-source"))
1614 d2::align::perturb_source();
1615 else
1616 assert(0);
1617 } else if (!strcmp(option_name, "fail")) {
1618 if (!strcmp(env->get_string_arg(i->first, 0), "fail-optimal"))
1619 d2::align::fail_optimal();
1620 else if (!strcmp(env->get_string_arg(i->first, 0), "fail-default"))
1621 d2::align::fail_default();
1622 else
1623 assert(0);
1624 } else if (!strcmp(option_name, "extend")) {
1625 if (env->get_int_arg(i->first, 0))
1626 extend = 1;
1627 else
1628 extend = 0;
1629 } else if (!strcmp(option_name, "oc")) {
1630 if (env->get_int_arg(i->first, 0))
1631 d3::scene::oc();
1632 else
1633 d3::scene::no_oc();
1634 } else if (!strcmp(option_name, "gs")) {
1635 d2::align::gs(env->get_string_arg(i->first, 1));
1636 } else if (!strcmp(option_name, "gs-mo")) {
1637 d2::align::gs_mo(env->get_unsigned_arg(i->first, 1));
1638 } else if (!strcmp(option_name, "focus")) {
1640 double one = +1;
1641 double zero = +0;
1642 double inf = one / zero;
1644 assert (isinf(inf) == +1);
1647 * Focus type
1650 unsigned int type = 0;
1651 double distance = 0;
1652 double px = 0, py = 0;
1654 if (!strcmp(env->get_string_arg(i->first, 1), "d")) {
1656 type = 0;
1658 distance = env->get_double_arg(i->first, 2);
1660 } else if (!strcmp(env->get_string_arg(i->first, 1), "p")) {
1662 type = 1;
1664 px = env->get_double_arg(i->first, 2);
1665 py = env->get_double_arg(i->first, 3);
1667 } else {
1668 bad_arg(option_name);
1672 * Options
1675 unsigned int ci = 0;
1676 double fr = 0;
1677 double ht = 0;
1678 double vt = 0;
1679 double sd = 0;
1680 double ed = inf;
1681 double sx = -inf;
1682 double ex = inf;
1683 double sy = -inf;
1684 double ey = inf;
1685 double ap = 3;
1686 unsigned int sc = 3;
1687 unsigned int fs = 0;
1688 unsigned int sr = 0;
1690 for (int arg_num = 4; env->is_arg(i->first, arg_num); arg_num++) {
1691 const char *option = env->get_string_arg(i->first, arg_num);
1692 if (!strncmp(option, "ci=", 3)) {
1693 if(sscanf(option + 3, "%u", &ci) != 1)
1694 bad_arg("--focus");
1695 } else if (!strncmp(option, "fr=", 3)) {
1696 if(sscanf(option + 3, "%lf", &fr) != 1)
1697 bad_arg("--focus");
1698 } else if (!strncmp(option, "ht=", 3)) {
1699 if(sscanf(option + 3, "%lf", &ht) != 1)
1700 bad_arg("--focus");
1701 } else if (!strncmp(option, "vt=", 3)) {
1702 if(sscanf(option + 3, "%lf", &vt) != 1)
1703 bad_arg("--focus");
1704 } else if (!strncmp(option, "sy=", 3)) {
1705 if(sscanf(option + 3, "%lf", &sy) != 1)
1706 bad_arg("--focus");
1707 } else if (!strncmp(option, "ey=", 3)) {
1708 if(sscanf(option + 3, "%lf", &ey) != 1)
1709 bad_arg("--focus");
1710 } else if (!strncmp(option, "sx=", 3)) {
1711 if(sscanf(option + 3, "%lf", &sx) != 1)
1712 bad_arg("--focus");
1713 } else if (!strncmp(option, "ex=", 3)) {
1714 if(sscanf(option + 3, "%lf", &ex) != 1)
1715 bad_arg("--focus");
1716 } else if (!strncmp(option, "sd=", 3)) {
1717 if(sscanf(option + 3, "%lf", &sd) != 1)
1718 bad_arg("--focus");
1719 } else if (!strncmp(option, "ed=", 3)) {
1720 if(sscanf(option + 3, "%lf", &ed) != 1)
1721 bad_arg("--focus");
1722 } else if (!strncmp(option, "ap=", 3)) {
1723 if(sscanf(option + 3, "%lf", &ap) != 1)
1724 bad_arg("--focus");
1725 } else if (!strncmp(option, "sc=", 3)) {
1726 if(sscanf(option + 3, "%u", &sc) != 1)
1727 bad_arg("--focus");
1728 } else if (!strncmp(option, "sr=", 3)) {
1729 if (!strcmp(option, "sr=aperture")) {
1730 sr = 0;
1731 } else if (!strcmp(option, "sr=pixel")) {
1732 sr = 1;
1733 } else
1734 bad_arg("--focus");
1736 } else if (!strncmp(option, "fs=", 3)) {
1737 if (!strcmp(option, "fs=mean")) {
1738 fs = 0;
1739 } else if (!strcmp(option, "fs=median")) {
1740 fs = 1;
1741 } else
1742 bad_arg("--focus");
1743 } else
1744 bad_arg("--focus");
1747 d3::focus::add_region(type, distance, px, py, ci, fr, ht, vt, sd, ed, sx, ex, sy, ey, ap, sc, fs, sr);
1749 } else if (!strcmp(option_name, "3ddp") || !strcmp(option_name, "3dvp")) {
1750 d2::align::keep();
1753 * Unsupported configurations
1756 if (ip_iterations)
1757 unsupported::fornow("3D modeling with Irani-Peleg rendering");
1759 #if 0
1760 if (usm_multiplier)
1761 unsupported::fornow("3D modeling with unsharp mask");
1762 #endif
1765 * Initialize if necessary
1767 * Note: because their existence is checked as an
1768 * indicator of the presence of 3D arguments, we
1769 * initialize these structures here.
1772 if (d3_output == NULL) {
1773 d3_count = argc;
1774 d3_output = (const char **) calloc(d3_count, sizeof(char *));
1775 d3_depth = (const char **) calloc(d3_count, sizeof(char *));
1778 unsigned int width, height;
1779 double view_angle;
1780 double x, y, z;
1781 double P, Y, R;
1783 width = env->get_unsigned_arg(i->first, 1);
1784 height = env->get_unsigned_arg(i->first, 2);
1785 view_angle = env->get_double_arg(i->first, 3);
1786 x = env->get_double_arg(i->first, 4);
1787 y = env->get_double_arg(i->first, 5);
1788 z = env->get_double_arg(i->first, 6);
1789 P = env->get_double_arg(i->first, 7);
1790 Y = env->get_double_arg(i->first, 8);
1791 R = env->get_double_arg(i->first, 9);
1793 view_angle *= M_PI / 180;
1794 P *= M_PI / 180;
1795 Y *= M_PI / 180;
1796 R *= M_PI / 180;
1798 d2::transformation t =
1799 d2::transformation::eu_identity();
1800 t.set_domain(height, width);
1801 d3::pt _pt(t, d3::et(y, x, z, Y, P, R), view_angle);
1803 if (!strcmp(option_name, "3dvp")) {
1804 d3_output_pt[env->get_string_arg(i->first, 10)] = _pt;
1805 } else if (!strcmp(option_name, "3ddp")) {
1806 d3_depth_pt[env->get_string_arg(i->first, 10)] = _pt;
1807 } else {
1808 assert(0);
1810 } else if (!strcmp(option_name, "3dv")) {
1811 d2::align::keep();
1813 unsigned int frame_no;
1816 * Unsupported configurations
1819 if (ip_iterations)
1820 unsupported::fornow("3D modeling with Irani-Peleg rendering");
1822 #if 0
1823 if (usm_multiplier)
1824 unsupported::fornow("3D modeling with unsharp mask");
1825 #endif
1828 * Initialize if necessary
1831 if (d3_output == NULL) {
1832 d3_count = argc;
1833 d3_output = (const char **) calloc(d3_count, sizeof(char *));
1834 d3_depth = (const char **) calloc(d3_count, sizeof(char *));
1837 frame_no = env->get_int_arg(i->first, 1);
1839 if (frame_no >= d3_count)
1840 ui::get()->error("--3dv argument 0 is too large");
1842 if (d3_output[frame_no] != NULL) {
1843 unsupported::fornow ("Writing a single 3D view to more than one output file");
1846 d3_output[frame_no] = env->get_string_arg(i->first, 2);
1848 } else if (!strcmp(option_name, "3dd")) {
1849 d2::align::keep();
1851 unsigned int frame_no;
1854 * Unsupported configurations
1857 if (ip_iterations)
1858 unsupported::fornow("3D modeling with Irani-Peleg rendering");
1860 #if 0
1861 if (usm_multiplier)
1862 unsupported::fornow("3D modeling with unsharp mask");
1863 #endif
1866 * Initialize if necessary
1869 if (d3_output == NULL) {
1870 d3_count = argc;
1871 d3_output = (const char **) calloc(d3_count, sizeof(char *));
1872 d3_depth = (const char **) calloc(d3_count, sizeof(char *));
1875 frame_no = env->get_int_arg(i->first, 1);
1877 if (frame_no >= d3_count)
1878 ui::get()->error("--3dd argument 0 is too large");
1880 if (d3_depth[frame_no] != NULL) {
1881 unsupported::fornow ("Writing a single frame's depth info to more than one output file");
1884 d3_depth[frame_no] = env->get_string_arg(i->first, 2);
1886 } else if (!strcmp(option_name, "view-angle")) {
1887 user_view_angle = env->get_double_arg(i->first, 1) * M_PI / 180;
1888 } else if (!strcmp(option_name, "cpf-load")) {
1889 d3::cpf::init_loadfile(env->get_string_arg(i->first, 1));
1890 } else if (!strcmp(option_name, "ui")) {
1891 if (!strcmp(env->get_string_arg(i->first, 1), "stream"))
1892 ui::set_stream();
1893 else if (!strcmp(env->get_string_arg(i->first, 1), "tty"))
1894 ui::set_tty();
1895 else
1896 assert(0);
1897 } else if (!strcmp(option_name, "3d-fmr")) {
1898 d3::scene::fmr(env->get_double_arg(i->first, 1));
1899 } else if (!strcmp(option_name, "3d-dmr")) {
1900 d3::scene::dmr(env->get_double_arg(i->first, 1));
1901 } else if (!strcmp(option_name, "et")) {
1902 d3::scene::et(env->get_double_arg(i->first, 1));
1903 } else if (!strcmp(option_name, "st")) {
1904 d3::cpf::st(env->get_double_arg(i->first, 1));
1905 } else if (!strcmp(option_name, "di-lower")) {
1906 d3::scene::di_lower(env->get_double_arg(i->first, 1));
1907 } else if (!strcmp(option_name, "rc")) {
1908 d3::scene::rc(env->get_double_arg(i->first, 1));
1909 } else if (!strcmp(option_name, "do-try")) {
1910 d3::scene::do_try(env->get_double_arg(i->first, 1));
1911 } else if (!strcmp(option_name, "di-upper")) {
1912 d3::scene::di_upper(env->get_double_arg(i->first, 1));
1913 } else if (!strcmp(option_name, "fc")) {
1914 d3::scene::fc(env->get_double_arg(i->first, 1));
1915 } else if (!strcmp(option_name, "ecm")) {
1916 unsupported::discontinued("--ecm <x>");
1917 } else if (!strcmp(option_name, "acm")) {
1918 unsupported::discontinued("--acm <x>");
1919 } else if (!strcmp(option_name, "def-nn")) {
1920 d2::image_rw::def_nn(env->get_double_arg(i->first, 1));
1922 if (env->get_double_arg(i->first, 1) > 2) {
1923 fprintf(stderr, "\n\n*** Warning: --def-nn implementation is currently "
1924 "inefficient for large radii. ***\n\n");
1927 } else if (!strcmp(option_name, "fx")) {
1928 d3::scene::fx(env->get_double_arg(i->first, 1));
1929 } else if (!strcmp(option_name, "tcem")) {
1930 d3::scene::tcem(env->get_double_arg(i->first, 1));
1931 } else if (!strcmp(option_name, "oui")) {
1932 d3::scene::oui(env->get_unsigned_arg(i->first, 1));
1933 } else if (!strcmp(option_name, "pa")) {
1934 d3::scene::pa(env->get_unsigned_arg(i->first, 1));
1935 } else if (!strcmp(option_name, "pc")) {
1936 d3::scene::pc(env->get_string_arg(i->first, 1));
1937 } else if (!strcmp(option_name, "cw")) {
1938 d2::align::certainty_weighted(env->get_unsigned_arg(i->first, 0));
1939 } else if (!strcmp(option_name, "wm")) {
1940 if (wm_filename != NULL)
1941 ui::get()->error("only one weight map can be specified");
1943 wm_filename = env->get_string_arg(i->first, 1);
1944 wm_offsetx = env->get_int_arg(i->first, 2);
1945 wm_offsety = env->get_int_arg(i->first, 3);
1947 } else if (!strcmp(option_name, "fl")) {
1948 #ifdef USE_FFTW
1949 d2::align::set_frequency_cut(env->get_double_arg(i->first, 1),
1950 env->get_double_arg(i->first, 2),
1951 env->get_double_arg(i->first, 3));
1953 #else
1954 ui::get()->error_hint("--fl is not supported", "rebuild ALE with FFTW support");
1955 #endif
1956 } else if (!strcmp(option_name, "wmx")) {
1957 #ifdef USE_UNIX
1958 d2::align::set_wmx(env->get_string_arg(i->first, 1),
1959 env->get_string_arg(i->first, 2),
1960 env->get_string_arg(i->first, 3));
1961 #else
1962 ui::get()->error_hint("--wmx is not supported", "rebuild ALE with support for --wmx");
1963 #endif
1964 } else if (!strcmp(option_name, "flshow")) {
1965 d2::align::set_fl_show(env->get_string_arg(i->first, 1));
1966 } else if (!strcmp(option_name, "3dpx")) {
1968 d3px_parameters = (double *) local_realloc(d3px_parameters, (d3px_count + 1) * 6 * sizeof(double));
1970 for (int param = 0; param < 6; param++)
1971 d3px_parameters[6 * d3px_count + param] = env->get_double_arg(i->first, param + 1);
1974 * Swap x and y, since their internal meanings differ from their external meanings.
1977 for (int param = 0; param < 2; param++) {
1978 double temp = d3px_parameters[6 * d3px_count + 2 + param];
1979 d3px_parameters[6 * d3px_count + 2 + param] = d3px_parameters[6 * d3px_count + 0 + param];
1980 d3px_parameters[6 * d3px_count + 0 + param] = temp;
1985 * Increment counters
1988 d3px_count++;
1990 } else if (!strcmp(option_name, "ex") || !strcmp(option_name, "fex")) {
1992 ex_parameters = (d2::exclusion *) local_realloc(ex_parameters,
1993 (ex_count + 1) * sizeof(d2::exclusion));
1995 ex_parameters[ex_count].type = (!strcmp(option_name, "ex"))
1996 ? d2::exclusion::RENDER
1997 : d2::exclusion::FRAME;
2000 * Get parameters, swapping x and y coordinates
2003 ex_parameters[ex_count].x[0] = env->get_int_arg(i->first, 1 + 2);
2004 ex_parameters[ex_count].x[1] = env->get_int_arg(i->first, 1 + 3);
2005 ex_parameters[ex_count].x[2] = env->get_int_arg(i->first, 1 + 0);
2006 ex_parameters[ex_count].x[3] = env->get_int_arg(i->first, 1 + 1);
2007 ex_parameters[ex_count].x[4] = env->get_int_arg(i->first, 1 + 4);
2008 ex_parameters[ex_count].x[5] = env->get_int_arg(i->first, 1 + 5);
2011 * Increment counters
2014 ex_count++;
2016 } else if (!strcmp(option_name, "crop") || !strcmp(option_name, "fcrop")) {
2018 ex_parameters = (d2::exclusion *) local_realloc(ex_parameters,
2019 (ex_count + 4) * sizeof(d2::exclusion));
2021 for (int r = 0; r < 4; r++)
2022 ex_parameters[ex_count + r].type = (!strcmp(option_name, "crop"))
2023 ? d2::exclusion::RENDER
2024 : d2::exclusion::FRAME;
2027 int crop_args[6];
2029 for (int param = 0; param < 6; param++)
2030 crop_args[param] = env->get_int_arg(i->first, param + 1);
2033 * Construct exclusion regions from the crop area,
2034 * swapping x and y, since their internal meanings
2035 * differ from their external meanings.
2039 * Exclusion region 1: low x
2042 ex_parameters[ex_count + 0].x[0] = INT_MIN;
2043 ex_parameters[ex_count + 0].x[1] = crop_args[2] - 1;
2044 ex_parameters[ex_count + 0].x[2] = INT_MIN;
2045 ex_parameters[ex_count + 0].x[3] = INT_MAX;
2046 ex_parameters[ex_count + 0].x[4] = crop_args[4];
2047 ex_parameters[ex_count + 0].x[5] = crop_args[5];
2050 * Exclusion region 2: low y
2053 ex_parameters[ex_count + 1].x[0] = INT_MIN;
2054 ex_parameters[ex_count + 1].x[1] = INT_MAX;
2055 ex_parameters[ex_count + 1].x[2] = INT_MIN;
2056 ex_parameters[ex_count + 1].x[3] = crop_args[0] - 1;
2057 ex_parameters[ex_count + 1].x[4] = crop_args[4];
2058 ex_parameters[ex_count + 1].x[5] = crop_args[5];
2061 * Exclusion region 3: high y
2064 ex_parameters[ex_count + 2].x[0] = INT_MIN;
2065 ex_parameters[ex_count + 2].x[1] = INT_MAX;
2066 ex_parameters[ex_count + 2].x[2] = crop_args[1] + 1;
2067 ex_parameters[ex_count + 2].x[3] = INT_MAX;
2068 ex_parameters[ex_count + 2].x[4] = crop_args[4];
2069 ex_parameters[ex_count + 2].x[5] = crop_args[5];
2072 * Exclusion region 4: high x
2075 ex_parameters[ex_count + 3].x[0] = crop_args[3] + 1;
2076 ex_parameters[ex_count + 3].x[1] = INT_MAX;
2077 ex_parameters[ex_count + 3].x[2] = INT_MIN;
2078 ex_parameters[ex_count + 3].x[3] = INT_MAX;
2079 ex_parameters[ex_count + 3].x[4] = crop_args[4];
2080 ex_parameters[ex_count + 3].x[5] = crop_args[5];
2083 * Increment counters
2086 ex_count += 4;
2088 } else if (!strcmp(option_name, "exshow")) {
2089 ex_show = 1;
2090 } else if (!strcmp(option_name, "wt")) {
2091 d2::render::set_wt(env->get_double_arg(i->first, 1));
2092 } else if (!strcmp(option_name, "3d-chain")) {
2093 d3chain_type = env->get_string_arg(i->first, 1);
2094 } else if (!strcmp(option_name, "dchain")) {
2095 ochain_types[0] = env->get_string_arg(i->first, 1);
2096 } else if (!strcmp(option_name, "achain")) {
2097 achain_type = env->get_string_arg(i->first, 1);
2098 } else if (!strcmp(option_name, "afilter")) {
2099 afilter_type = env->get_string_arg(i->first, 1);
2100 } else if (!strcmp(option_name, "ochain")) {
2102 ochain = (d2::render **) local_realloc(ochain,
2103 (oc_count + 1) * sizeof(d2::render *));
2104 ochain_names = (const char **) local_realloc((void *)ochain_names,
2105 (oc_count + 1) * sizeof(const char *));
2106 ochain_types = (const char **) local_realloc((void *)ochain_types,
2107 (oc_count + 1) * sizeof(const char *));
2109 ochain_types[oc_count] = env->get_string_arg(i->first, 1);
2110 ochain_names[oc_count] = env->get_string_arg(i->first, 2);
2112 oc_count++;
2114 } else if (!strcmp(option_name, "visp")) {
2116 visp = (const char **) local_realloc((void *)visp, 4 *
2117 (vise_count + 1) * sizeof(const char *));
2119 for (int param = 0; param < 4; param++)
2120 visp[vise_count * 4 + param] = env->get_string_arg(i->first, param + 1);
2122 vise_count++;
2124 } else if (!strcmp(option_name, "cx")) {
2125 cx_parameter = env->get_int_arg(i->first, 0) ? env->get_double_arg(i->first, 1) : 0;
2126 } else if (!strcmp(option_name, "ip")) {
2127 unsupported::discontinued("--ip <r> <i>", "--lpsf box=<r> --ips <i>");
2128 } else if (!strcmp(option_name, "bayer")) {
2131 * External order is clockwise from top-left. Internal
2132 * order is counter-clockwise from top-left.
2135 const char *option = env->get_string_arg(i->first, 1);
2137 if (!strcmp(option, "rgbg")) {
2138 user_bayer = IMAGE_BAYER_RGBG;
2139 } else if (!strcmp(option, "bgrg")) {
2140 user_bayer = IMAGE_BAYER_BGRG;
2141 } else if (!strcmp(option, "gbgr")) {
2142 user_bayer = IMAGE_BAYER_GRGB;
2143 } else if (!strcmp(option, "grgb")) {
2144 user_bayer = IMAGE_BAYER_GBGR;
2145 } else if (!strcmp(option, "none")) {
2146 user_bayer = IMAGE_BAYER_NONE;
2147 } else {
2148 bad_arg("--bayer");
2151 } else if (!strcmp(option_name, "lpsf")) {
2152 psf[psf_linear] = env->get_string_arg(i->first, 1);
2153 } else if (!strcmp(option_name, "nlpsf")) {
2154 psf[psf_nonlinear] = env->get_string_arg(i->first, 1);
2155 } else if (!strcmp(option_name, "psf-match")) {
2157 psf_match = 1;
2159 for (int index = 0; index < 6; index++) {
2160 psf_match_args[index] = env->get_double_arg(i->first, index + 1);
2163 } else if (!strcmp(option_name, "device")) {
2164 device = env->get_string_arg(i->first, 1);
2165 #if 0
2166 } else if (!strcmp(option_name, "usm")) {
2168 if (d3_output != NULL)
2169 unsupported::fornow("3D modeling with unsharp mask");
2171 usm_multiplier = env->get_double_arg(i->first, 1);
2172 #endif
2174 } else if (!strcmp(option_name, "ipr")) {
2176 ip_iterations = env->get_int_arg(i->first, 1);
2178 ui::get()->warn("--ipr is deprecated. Use --ips instead");
2180 } else if (!strcmp(option_name, "cpp-err")) {
2181 if (!strcmp(env->get_string_arg(i->first, 0), "median"))
2182 d3::cpf::err_median();
2183 else if (!strcmp(env->get_string_arg(i->first, 0), "mean"))
2184 d3::cpf::err_mean();
2185 } else if (!strcmp(option_name, "vp-adjust")) {
2186 if (env->get_int_arg(i->first, 0))
2187 d3::align::vp_adjust();
2188 else
2189 d3::align::vp_noadjust();
2190 } else if (!strcmp(option_name, "vo-adjust")) {
2191 if (env->get_int_arg(i->first, 0))
2192 d3::align::vo_adjust();
2193 else
2194 d3::align::vo_noadjust();
2195 } else if (!strcmp(option_name, "ip-statistic")) {
2196 if (!strcmp(env->get_string_arg(i->first, 1), "mean"))
2197 ip_use_median = 0;
2198 else if (!strcmp(env->get_string_arg(i->first, 1), "median"))
2199 ip_use_median = 1;
2200 } else if (!strcmp(option_name, "ips")) {
2201 ip_iterations = env->get_int_arg(i->first, 1);
2202 } else if (!strcmp(option_name, "ipc")) {
2203 unsupported::discontinued("--ipc <c> <i>", "--ips <i> --lpsf <c>", "--ips <i> --device <c>");
2204 } else if (!strcmp(option_name, "exp-extend")) {
2205 if (env->get_int_arg(i->first, 0))
2206 d2::image_rw::exp_scale();
2207 else
2208 d2::image_rw::exp_noscale();
2209 } else if (!strcmp(option_name, "exp-register")) {
2210 if (env->get_int_arg(i->first, 0) == 1) {
2211 exposure_register = 1;
2212 d2::align::exp_register();
2213 } else if (env->get_int_arg(i->first, 0) == 0) {
2214 exposure_register = 0;
2215 d2::align::exp_noregister();
2216 } else if (env->get_int_arg(i->first, 0) == 2) {
2217 exposure_register = 2;
2218 d2::align::exp_meta_only();
2220 } else if (!strcmp(option_name, "drizzle-only")) {
2221 unsupported::discontinued("--drizzle-only", "--dchain box:1");
2222 } else if (!strcmp(option_name, "subspace-traverse")) {
2223 unsupported::undocumented("--subspace-traverse");
2224 d3::scene::set_subspace_traverse();
2225 } else if (!strcmp(option_name, "3d-filter")) {
2226 if (env->get_int_arg(i->first, 0))
2227 d3::scene::filter();
2228 else
2229 d3::scene::nofilter();
2230 } else if (!strcmp(option_name, "occ-norm")) {
2231 if (env->get_int_arg(i->first, 0))
2232 d3::scene::nw();
2233 else
2234 d3::scene::no_nw();
2235 } else if (!strcmp(option_name, "inc")) {
2236 inc = env->get_int_arg(i->first, 0);
2237 } else if (!strcmp(option_name, "exp-mult")) {
2238 double exp_c, exp_r, exp_b;
2240 exp_c = env->get_double_arg(i->first, 1);
2241 exp_r = env->get_double_arg(i->first, 2);
2242 exp_b = env->get_double_arg(i->first, 3);
2244 exp_mult = d2::pixel(1/(exp_r * exp_c), 1/exp_c, 1/(exp_b * exp_c));
2246 } else if (!strcmp(option_name, "visp-scale")) {
2248 vise_scale_factor = env->get_double_arg(i->first, 1);
2250 if (vise_scale_factor <= 0.0)
2251 ui::get()->error("VISP scale must be greater than zero");
2253 if (!finite(vise_scale_factor))
2254 ui::get()->error("VISP scale must be finite");
2256 } else if (!strcmp(option_name, "scale")) {
2258 scale_factor = env->get_double_arg(i->first, 1);
2260 if (scale_factor <= 0)
2261 ui::get()->error("Scale factor must be greater than zero");
2263 if (!finite(scale_factor))
2264 ui::get()->error("Scale factor must be finite");
2266 } else if (!strcmp(option_name, "metric")) {
2267 d2::align::set_metric_exponent(env->get_double_arg(i->first, 1));
2268 } else if (!strcmp(option_name, "threshold")) {
2269 d2::align::set_match_threshold(env->get_double_arg(i->first, 1));
2270 } else if (!strcmp(option_name, "drizzle-diam")) {
2271 unsupported::discontinued("--drizzle-diam=<x>", "--dchain box:1");
2272 } else if (!strcmp(option_name, "perturb-upper")) {
2273 const char *option = env->get_string_arg(i->first, 1);
2274 double perturb_upper;
2275 int characters;
2276 sscanf(option, "%lf%n", &perturb_upper, &characters);
2277 if (*(option + characters) == '%')
2278 d2::align::set_perturb_upper(perturb_upper, 1);
2279 else
2280 d2::align::set_perturb_upper(perturb_upper, 0);
2281 } else if (!strcmp(option_name, "perturb-lower")) {
2282 const char *option = env->get_string_arg(i->first, 1);
2283 double perturb_lower;
2284 int characters;
2285 sscanf(option, "%lf%n", &perturb_lower, &characters);
2286 if (perturb_lower <= 0)
2287 ui::get()->error("--perturb-lower= value is non-positive");
2289 if (*(option + characters) == '%')
2290 d2::align::set_perturb_lower(perturb_lower, 1);
2291 else
2292 d2::align::set_perturb_lower(perturb_lower, 0);
2293 } else if (!strcmp(option_name, "stepsize")) {
2294 ui::get()->warn("--stepsize is deprecated. Use --perturb-lower instead");
2295 d2::align::set_perturb_lower(env->get_double_arg(i->first, 1), 0);
2296 } else if (!strcmp(option_name, "va-upper")) {
2297 const char *option = env->get_string_arg(i->first, 1);
2298 double va_upper;
2299 int characters;
2300 sscanf(option, "%lf%n", &va_upper, &characters);
2301 if (*(option + characters) == '%')
2302 ui::get()->error("--va-upper= does not accept '%' arguments\n");
2303 else
2304 d3::cpf::set_va_upper(va_upper);
2305 } else if (!strcmp(option_name, "cpp-upper")) {
2306 const char *option = env->get_string_arg(i->first, 1);
2307 double perturb_upper;
2308 int characters;
2309 sscanf(option, "%lf%n", &perturb_upper, &characters);
2310 if (*(option + characters) == '%')
2311 ui::get()->error("--cpp-upper= does not currently accept '%' arguments\n");
2312 else
2313 d3::cpf::set_cpp_upper(perturb_upper);
2314 } else if (!strcmp(option_name, "cpp-lower")) {
2315 const char *option = env->get_string_arg(i->first, 1);
2316 double perturb_lower;
2317 int characters;
2318 sscanf(option, "%lf%n", &perturb_lower, &characters);
2319 if (*(option + characters) == '%')
2320 ui::get()->error("--cpp-lower= does not currently accept '%' arguments\n");
2321 else
2322 d3::cpf::set_cpp_lower(perturb_lower);
2323 } else if (!strcmp(option_name, "hf-enhance")) {
2324 unsupported::discontinued("--hf-enhance=<x>");
2325 } else if (!strcmp(option_name, "rot-upper")) {
2326 d2::align::set_rot_max((int) floor(env->get_double_arg(i->first, 1)));
2327 } else if (!strcmp(option_name, "bda-mult")) {
2328 d2::align::set_bda_mult(env->get_double_arg(i->first, 1));
2329 } else if (!strcmp(option_name, "bda-rate")) {
2330 d2::align::set_bda_rate(env->get_double_arg(i->first, 1));
2331 } else if (!strcmp(option_name, "lod-max")) {
2332 d2::align::set_lod_max((int) floor(env->get_double_arg(i->first, 1)));
2333 } else if (!strcmp(option_name, "cpf-load")) {
2334 d3::cpf::init_loadfile(env->get_string_arg(i->first, 1));
2335 #if 0
2336 } else if (!strcmp(option_name, "model-load")) {
2337 d3::scene::load_model(env->get_string_arg(i->first, 1));
2338 } else if (!strcmp(option_name, "model-save")) {
2339 d3::scene::save_model(env->get_string_arg(i->first, 1));
2340 #endif
2341 } else if (!strcmp(option_name, "trans-load")) {
2342 d2::tload_delete(tload);
2343 tload = d2::tload_new(env->get_string_arg(i->first, 1));
2344 d2::align::set_tload(tload);
2345 } else if (!strcmp(option_name, "trans-save")) {
2346 tsave_delete(tsave);
2347 tsave = d2::tsave_new(env->get_string_arg(i->first, 1));
2348 d2::align::set_tsave(tsave);
2349 } else if (!strcmp(option_name, "3d-trans-load")) {
2350 d3::tload_delete(d3_tload);
2351 d3_tload = d3::tload_new(env->get_string_arg(i->first, 1));
2352 d3::align::set_tload(d3_tload);
2353 } else if (!strcmp(option_name, "3d-trans-save")) {
2354 d3::tsave_delete(d3_tsave);
2355 d3_tsave = d3::tsave_new(env->get_string_arg(i->first, 1));
2356 d3::align::set_tsave(d3_tsave);
2357 } else {
2358 assert(0);
2363 * Apply implication logic.
2366 if (extend == 0 && vise_count != 0) {
2367 implication::changed("VISP requires increased image extents.",
2368 "Image extension is now enabled.",
2369 "--extend");
2370 extend = 1;
2373 if (psf_match && ex_count)
2374 unsupported::fornow("PSF calibration with exclusion regions.");
2377 if (d3_output != NULL && ip_iterations != 0)
2378 unsupported::fornow("3D modeling with Irani-Peleg rendering");
2380 #if 0
2381 if (extend == 0 && d3_output != NULL) {
2382 implication::changed("3D modeling requires increased image extents.",
2383 "Image extension is now enabled.",
2384 "--extend");
2385 extend = 1;
2387 #endif
2389 if (cx_parameter != 0 && !exposure_register) {
2390 implication::changed("Certainty-based rendering requires exposure registration.",
2391 "Exposure registration is now enabled.",
2392 "--exp-register");
2393 d2::align::exp_register();
2394 exposure_register = 1;
2398 * Set alignment class exclusion region static variables
2401 d2::align::set_exclusion(ex_parameters, ex_count);
2404 * Initialize renderer class statics.
2407 d2::render::render_init(ex_count, ex_parameters, ex_show, extend, scale_factor);
2410 * Set confidence
2413 d2::exposure::set_confidence(cx_parameter);
2416 * Keep transformations for Irani-Peleg, psf-match, and
2417 * VISE
2420 if (ip_iterations > 0 || psf_match || vise_count > 0) {
2421 d2::align::keep();
2425 * Initialize device-specific variables
2428 // int input_file_count = argc - i - 1;
2429 int input_file_count = files.size() - 1;
2431 d2::psf *device_response[psf_N] = { NULL, NULL };
2432 d2::exposure **input_exposure = NULL;
2433 ale_pos view_angle = 43.7 * M_PI / 180;
2434 // ale_pos view_angle = 90 * M_PI / 180;
2435 input_exposure = (d2::exposure **)
2436 // malloc((argc - i - 1) * sizeof(d2::exposure *));
2437 malloc(input_file_count * sizeof(d2::exposure *));
2439 if (device != NULL) {
2440 if (!strcmp(device, "xvp610_640x480")) {
2441 device_response[psf_linear] = new xvp610_640x480::lpsf();
2442 device_response[psf_nonlinear] = new xvp610_640x480::nlpsf();
2443 for (int ii = 0; ii < input_file_count; ii++)
2444 input_exposure[ii] = new xvp610_640x480::exposure();
2445 view_angle = xvp610_640x480::view_angle();
2446 } else if (!strcmp(device, "xvp610_320x240")) {
2447 device_response[psf_linear] = new xvp610_320x240::lpsf();
2448 device_response[psf_nonlinear] = new xvp610_320x240::nlpsf();
2449 for (int ii = 0; ii < input_file_count; ii++)
2450 input_exposure[ii] = new xvp610_320x240::exposure();
2451 view_angle = xvp610_320x240::view_angle();
2452 } else if (!strcmp(device, "ov7620_raw_linear")) {
2453 device_response[psf_linear] = new ov7620_raw_linear::lpsf();
2454 device_response[psf_nonlinear] = NULL;
2455 for (int ii = 0; ii < input_file_count; ii++)
2456 input_exposure[ii] = new ov7620_raw_linear::exposure();
2457 d2::image_rw::set_default_bayer(IMAGE_BAYER_BGRG);
2458 } else if (!strcmp(device, "canon_300d_raw_linear")) {
2459 device_response[psf_linear] = new canon_300d_raw_linear::lpsf();
2460 device_response[psf_nonlinear] = NULL;
2461 for (int ii = 0; ii < input_file_count; ii++)
2462 input_exposure[ii] = new canon_300d_raw_linear::exposure();
2463 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG);
2464 } else if (!strcmp(device, "canon_300d_raw_linear+85mm_1.8")) {
2465 device_response[psf_linear] = new canon_300d_raw_linear_85mm_1_8::lpsf();
2466 device_response[psf_nonlinear] = NULL;
2467 for (int ii = 0; ii < input_file_count; ii++)
2468 input_exposure[ii] = new canon_300d_raw_linear_85mm_1_8::exposure();
2469 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG);
2470 view_angle = canon_300d_raw_linear_85mm_1_8::view_angle();
2471 } else if (!strcmp(device, "canon_300d_raw_linear+50mm_1.8")) {
2472 device_response[psf_linear] = new canon_300d_raw_linear_50mm_1_8::lpsf();
2473 device_response[psf_nonlinear] = NULL;
2474 for (int ii = 0; ii < input_file_count; ii++)
2475 input_exposure[ii] = new canon_300d_raw_linear_50mm_1_8::exposure();
2476 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG);
2477 view_angle = canon_300d_raw_linear_50mm_1_8::view_angle();
2478 } else if (!strcmp(device, "canon_300d_raw_linear+50mm_1.4")) {
2479 device_response[psf_linear] = new canon_300d_raw_linear_50mm_1_4::lpsf();
2480 device_response[psf_nonlinear] = NULL;
2481 for (int ii = 0; ii < input_file_count; ii++)
2482 input_exposure[ii] = new canon_300d_raw_linear_50mm_1_4::exposure();
2483 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG);
2484 view_angle = canon_300d_raw_linear_50mm_1_4::view_angle();
2485 } else if (!strcmp(device, "canon_300d_raw_linear+50mm_1.4@1.4")) {
2486 device_response[psf_linear] = new canon_300d_raw_linear_50mm_1_4_1_4::lpsf();
2487 device_response[psf_nonlinear] = NULL;
2488 for (int ii = 0; ii < input_file_count; ii++)
2489 input_exposure[ii] = new canon_300d_raw_linear_50mm_1_4_1_4::exposure();
2490 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG);
2491 view_angle = canon_300d_raw_linear_50mm_1_4_1_4::view_angle();
2492 } else {
2493 ui::get()->unknown_device(device);
2495 } else {
2496 for (int ii = 0; ii < input_file_count; ii++)
2497 input_exposure[ii] = new d2::exposure_default();
2501 * User-specified variables.
2504 if (user_view_angle != 0) {
2505 view_angle = user_view_angle;
2508 if (user_bayer != IMAGE_BAYER_DEFAULT) {
2509 d2::image_rw::set_default_bayer(user_bayer);
2513 * PSF-match exposure.
2515 if (psf_match) {
2516 delete input_exposure[input_file_count - 1];
2517 input_exposure[input_file_count - 1] = new d2::exposure_default();
2521 * Initialize output exposure
2524 d2::exposure *output_exposure = new d2::exposure_default();
2525 output_exposure->set_multiplier(exp_mult);
2528 * Configure the response function.
2531 d2::psf *response[2] = {NULL, NULL};
2533 for (int n = 0; n < psf_N; n++ ) {
2534 if (psf[n] != NULL) {
2536 response[n] = d2::psf_parse::get((n == psf_linear), psf[n]);
2538 } else if (device_response[n] != NULL) {
2541 * Device-specific response
2544 response[n] = device_response[n];
2546 } else {
2549 * Default point-spread function.
2552 if (n == psf_linear) {
2555 * Default lpsf is a box filter
2556 * of diameter 1.0 (radius
2557 * 0.5).
2560 response[n] = new d2::box(0.5);
2562 } else if (n == psf_nonlinear) {
2565 * nlpsf is disabled by default.
2568 response[n] = NULL;
2574 * First file argument. Print general file information as well
2575 * as information specific to this argument. Initialize image
2576 * file handler.
2579 // d2::image_rw::init(argc - i - 1, argv + i, argv[argc - 1], input_exposure, output_exposure);
2580 // ochain_names[0] = argv[argc - 1];
2582 const char **input_files = (const char **) malloc(sizeof(const char *) * input_file_count);
2583 for (int i = 0; i < input_file_count; i++) {
2584 input_files[i] = files[i].first;
2587 d2::image_rw::init(input_file_count, input_files, files[files.size() - 1].first,
2588 input_exposure, output_exposure);
2590 ochain_names[0] = files[files.size() - 1].first;
2593 * Handle control point data for alignment
2595 d2::align::set_cp_count(d3::cpf::count());
2596 for (unsigned int ii = 0; ii < d3::cpf::count(); ii++)
2597 d2::align::set_cp(ii, d3::cpf::get_2d(ii));
2600 * PSF-match bayer patterns.
2603 if (psf_match) {
2604 // d2::image_rw::set_specific_bayer(argc - i - 2, IMAGE_BAYER_NONE);
2605 d2::image_rw::set_specific_bayer(input_file_count - 1, IMAGE_BAYER_NONE);
2609 * Handle alignment weight map, if necessary
2612 if (wm_filename != NULL) {
2613 d2::image *weight_map;
2614 weight_map = d2::image_rw::read_image(wm_filename, new d2::exposure_linear());
2615 weight_map->set_offset(wm_offsety, wm_offsetx);
2616 d2::align::set_weight_map(weight_map);
2620 * Write comment information about original frame and
2621 * target image to the transformation save file, if we
2622 * have one.
2625 const d2::image *im = d2::image_rw::open(0);
2626 // tsave_orig(tsave, argv[i], im->avg_channel_magnitude());
2627 // tsave_target(tsave, argv[argc - 1]);
2628 tsave_orig(tsave, files[0].first, im->avg_channel_magnitude());
2629 tsave_target(tsave, files[files.size() - 1].first);
2630 d2::image_rw::close(0);
2633 * Initialize alignment interpolant.
2636 if (afilter_type != "internal")
2637 d2::align::set_interpolant(d2::render_parse::get_SSF(afilter_type));
2640 * Initialize achain and ochain.
2643 achain = d2::render_parse::get(achain_type);
2645 for (int chain = 0; chain < oc_count; chain++)
2646 ochain[chain] = d2::render_parse::get(ochain_types[chain]);
2649 * Use merged renderings as reference images in
2650 * alignment.
2653 d2::align::set_reference(achain);
2656 * Tell the alignment class about the scale factor.
2659 d2::align::set_scale(scale_factor);
2662 * Initialize visp.
2665 d2::vise_core::set_scale(vise_scale_factor);
2667 for (int opt = 0; opt < vise_count; opt++) {
2668 d2::vise_core::add(d2::render_parse::get(visp[opt * 4 + 0]),
2669 visp[opt * 4 + 1],
2670 visp[opt * 4 + 2],
2671 visp[opt * 4 + 3]);
2675 * Initialize non-incremental renderers
2678 #if 0
2679 if (usm_multiplier != 0) {
2682 * Unsharp Mask renderer
2685 ochain[0] = new d2::usm(ochain[0], scale_factor,
2686 usm_multiplier, inc, response[psf_linear],
2687 response[psf_nonlinear], &input_exposure[0]);
2689 #endif
2691 if (psf_match) {
2694 * Point-spread function calibration renderer.
2695 * This renderer does not produce image output.
2696 * It is reserved for use with the point-spread
2697 * function calibration script
2698 * ale-psf-calibrate.
2701 ochain[0] = new d2::psf_calibrate(ochain[0],
2702 1, inc, response[psf_linear],
2703 response[psf_nonlinear],
2704 psf_match_args);
2706 } else if (ip_iterations != 0) {
2709 * Irani-Peleg renderer
2712 ochain[0] = new d2::ipc( ochain[0], ip_iterations,
2713 inc, response[psf_linear],
2714 response[psf_nonlinear],
2715 (exposure_register == 1), ip_use_median);
2719 * Iterate through all files.
2722 for (unsigned int j = 0; j < d2::image_rw::count(); j++) {
2725 * Iterate through non-global options
2728 environment *env = files[j].second;
2730 for (std::map<const char *, const char *>::iterator i = env->get_map().begin();
2731 i != env->get_map().end(); i++) {
2733 if (!env->is_option(i->first))
2734 continue;
2736 const char *option_name = env->get_option_name(i->first);
2738 if (!strcmp(option_name, "mc")) {
2739 d2::align::mc(env->get_int_arg(i->first, 0) ? env->get_double_arg(i->first, 1) / 100 : 0);
2740 } else if (!strcmp(option_name, "threads")) {
2741 thread::set_count((unsigned int) env->get_int_arg(i->first, 1));
2742 } else if (!strcmp(option_name, "per-cpu")) {
2743 thread::set_per_cpu((unsigned int) env->get_int_arg(i->first, 1));
2744 } else {
2746 * This error should be encountered earlier.
2749 assert(0);
2751 fprintf(stderr, "\n\nError: option `%s' must be applied globally.", option_name);
2752 fprintf(stderr, "\n\nHint: Move option `%s' prior to file and scope operators.\n\n",
2753 option_name);
2754 exit(1);
2760 if (j == 0) {
2762 * Handle the original frame.
2765 // ui::get()->original_frame_start(argv[i]);
2766 ui::get()->original_frame_start(files[0].first);
2768 for (int opt = 0; opt < oc_count; opt++) {
2769 ui::get()->set_orender_current(opt);
2770 ochain[opt]->sync(0);
2771 if (inc) {
2772 ui::get()->writing_output(opt);
2773 d2::image_rw::write_image(ochain_names[opt],
2774 ochain[opt]->get_image(0));
2778 d2::vise_core::frame_queue_add(0);
2780 ui::get()->original_frame_done();
2782 continue;
2786 * Handle supplemental frames.
2789 const char *name = d2::image_rw::name(j);
2791 ui::get()->supplemental_frame_start(name);
2794 * Write comment information about the
2795 * supplemental frame to the transformation
2796 * save file, if we have one.
2799 tsave_info (tsave, name);
2801 const d2::image *im = d2::image_rw::open(j);
2802 d2::pixel apm = im->avg_channel_magnitude();
2803 tsave_apm(tsave, apm[0], apm[1], apm[2]);
2804 d2::image_rw::close(j);
2806 for (int opt = 0; opt < oc_count; opt++) {
2807 ui::get()->set_orender_current(opt);
2808 ochain[opt]->sync(j);
2809 if (inc) {
2810 ui::get()->writing_output(opt);
2811 d2::image_rw::write_image(ochain_names[opt],
2812 ochain[opt]->get_image(j));
2816 d2::vise_core::frame_queue_add(j);
2818 ui::get()->supplemental_frame_done();
2822 * Do any post-processing and output final image
2824 * XXX: note that non-incremental renderers currently
2825 * return zero for ochain[0]->sync(), since they write
2826 * output internally when inc != 0.
2829 for (int opt = 0; opt < oc_count; opt++)
2830 if ((ochain[opt]->sync() || !inc) && !psf_match)
2831 d2::image_rw::write_image(ochain_names[opt], ochain[opt]->get_image());
2834 * Output a summary match statistic.
2837 ui::get()->ale_2d_done((double) d2::align::match_summary());
2840 * Perform any 3D tasks
2843 optimizations::begin_3d_work();
2845 if (d3_count > 0) {
2847 ui::get()->d3_start();
2849 d3::align::init_angle(view_angle);
2851 ui::get()->d3_init_view_angle(view_angle / M_PI * 180);
2853 d3::align::init_from_d2();
2855 if (d3::cpf::count() > 0) {
2856 ui::get()->d3_control_point_solve();
2857 d3::cpf::solve_3d();
2858 ui::get()->d3_control_point_solve_done();
2861 ui::get()->d3_final_view_angle(d3::align::angle_of(0) / M_PI * 180);
2863 d3::align::write_alignments();
2865 d3::scene::set_filter_type(d3chain_type);
2867 d3::scene::init_from_d2();
2869 ui::get()->d3_subdividing_space();
2870 d3::scene::make_space(d3_depth, d3_output, &d3_depth_pt, &d3_output_pt);
2871 ui::get()->d3_subdividing_space_done();
2873 ui::get()->d3_updating_occupancy();
2874 d3::scene::reduce_cost_to_search_depth(output_exposure, inc);
2875 ui::get()->d3_updating_occupancy_done();
2877 d3::scene::d3px(d3px_count, d3px_parameters);
2878 int view_count = 0;
2879 for (unsigned int i = 0; i < d2::image_rw::count(); i++) {
2880 assert (i < d3_count);
2882 if (d3_depth[i] != NULL) {
2883 ui::get()->d3_writing_output(d3_depth[i]);
2884 ui::get()->d3_render_status(0, 0, -1, -1, -1, -1, 0);
2885 const d2::image *im = d3::scene::depth(i);
2886 d2::image_rw::write_image(d3_depth[i], im, output_exposure, 1, 1);
2887 delete im;
2888 ui::get()->d3_writing_output_done();
2891 if (d3_output[i] != NULL) {
2892 ui::get()->d3_writing_output(d3_output[i]);
2893 const d2::image *im = d3::scene::view(i);
2894 d2::image_rw::write_image(d3_output[i], im, output_exposure);
2895 delete im;
2896 d3::focus::set_camera(view_count++);
2897 ui::get()->d3_writing_output_done();
2900 for (std::map<const char *, d3::pt>::iterator i = d3_output_pt.begin();
2901 i != d3_output_pt.end(); i++) {
2903 ui::get()->d3_writing_output(i->first);
2904 const d2::image *im = d3::scene::view(i->second);
2905 d2::image_rw::write_image(i->first, im, output_exposure);
2906 delete im;
2907 d3::focus::set_camera(view_count++);
2908 ui::get()->d3_writing_output_done();
2911 for (std::map<const char *, d3::pt>::iterator i = d3_depth_pt.begin();
2912 i != d3_depth_pt.end(); i++) {
2914 ui::get()->d3_writing_output(i->first);
2915 ui::get()->d3_render_status(0, 0, -1, -1, -1, -1, 0);
2916 const d2::image *im = d3::scene::depth(i->second);
2917 d2::image_rw::write_image(i->first, im, output_exposure, 1, 1);
2918 delete im;
2919 ui::get()->d3_writing_output_done();
2923 for (unsigned int i = d2::image_rw::count(); i < d3_count; i++) {
2924 if (d3_depth[i] != NULL) {
2925 fprintf(stderr, "\n\n*** Frame number for --3dd too high. ***\n\n");
2927 if (d3_output[i] != NULL) {
2928 fprintf(stderr, "\n\n*** Frame number for --3dv too high. ***\n\n");
2934 * Destroy the image file handler
2937 d2::image_rw::destroy();
2940 * Delete the transformation file structures, if any
2941 * exist.
2944 tsave_delete(tsave);
2945 tload_delete(tload);
2948 * We're done.
2951 exit(0);
2955 #endif