image_rw, ui/input: Proposed new sequence logic and input processor cache logic.
[Ale.git] / ui / input.h
blob359d7f1b60bbdc5e42608c933553889b34cf3886
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 3 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>
37 #include "../ale_math.h"
40 * Interface files
43 #include "ui.h"
44 #include "accel.h"
45 #include "unsupported.h"
46 #include "implication.h"
49 * Configuration
52 #if HAVE_CONFIG_H
53 # include <config.h>
54 #endif
57 * Types
60 #include "../ale_pos.h"
61 #include "../ale_real.h"
64 * 2D include files
67 #include "../d2.h"
70 * 3D include files
73 #include "../d3.h"
76 * Thread include files
79 #include "../thread.h"
82 * Device configuration files
85 #include "../device/xvp610_320x240.h"
86 #include "../device/xvp610_640x480.h"
87 #include "../device/ov7620_raw_linear.h"
88 #include "../device/canon_300d_raw_linear.h"
89 #include "../device/canon_300d_raw_linear_85mm_1_8.h"
90 #include "../device/canon_300d_raw_linear_50mm_1_8.h"
91 #include "../device/canon_300d_raw_linear_50mm_1_4.h"
92 #include "../device/canon_300d_raw_linear_50mm_1_4_1_4.h"
93 #include "../device/nikon_d50.h"
96 * Help files
99 #include "help.h"
101 class input {
104 * Flag for global options.
107 static int global_options;
110 * Helper functions.
114 * Argument counter.
116 * Counts instances of a given option.
118 static unsigned int arg_count(int argc, const char *argv[], const char *arg) {
119 unsigned int count = 0;
120 for (int i = 0; i < argc; i++) {
121 if (!strcmp(argv[i], arg))
122 count++;
123 else if (!strcmp(argv[i], "--"))
124 return count;
126 return count;
130 * Argument prefix counter.
132 * Counts instances of a given option prefix.
134 static unsigned int arg_prefix_count(int argc, const char *argv[], const char *pfix) {
135 unsigned int count = 0;
136 for (int i = 0; i < argc; i++) {
137 if (!strncmp(argv[i], pfix, strlen(pfix)))
138 count++;
139 else if (!strcmp(argv[i], "--"))
140 return count;
142 return count;
146 * Reallocation function
148 static void *local_realloc(void *ptr, size_t size) {
149 void *new_ptr = realloc(ptr, size);
151 if (new_ptr == NULL)
152 ui::get()->memory_error_location("main()");
154 return new_ptr;
158 * Not enough arguments function.
160 static void not_enough(const char *opt_name) {
161 ui::get()->cli_not_enough(opt_name);
165 * Bad argument function
167 static void bad_arg(const char *opt_name) {
168 ui::get()->cli_bad_arg(opt_name);
172 * String comparison class.
175 class compare_strings {
176 public:
177 int operator()(const char *A, const char *B) const {
178 return strcmp(A, B) < 0;
183 * Environment structures.
185 * XXX: It's arguable that these should be public members of the
186 * 'input' class in order to allow passing environment values to other
187 * classes, but, since we're currently using them only to prepare state
188 * for an internal 'input' function, they can stay private for now. A
189 * more nuanced approach will likely be required later.
192 class environment {
193 static std::stack<environment *> environment_stack;
194 static std::set<environment *> environment_set;
196 std::map<const char *, const char *, compare_strings> environment_map;
199 * Internal set operations do not protect any data.
202 void internal_set(const char *name, const char *value) {
203 environment_map[name] = value;
206 void internal_unset(const char *name) {
207 environment_map.erase(name);
210 const char *internal_convert_pointer(const void *pointer) {
211 int chars = sizeof(void *) * 2 + 3;
212 char *c = (char *) malloc(sizeof(char) * chars);
214 assert(c);
216 if (!c)
217 ui::get()->memory_error_location("environment::set_ptr");
219 int count = snprintf(c, chars, "%p", pointer);
221 assert (count >= 0 && count < chars);
223 return c;
226 void internal_set_ptr(const char *name, const void *pointer) {
227 internal_set(name, internal_convert_pointer(pointer));
231 * Check for restricted names.
234 int name_ok(const char *name) {
235 if (!strcmp(name, "---chain") || !strcmp(name, "---this"))
236 return 0;
238 return 1;
241 void name_check(const char *name) {
242 if (!name_ok(name)) {
243 fprintf(stderr, "Bad set operation.");
244 assert(0);
245 exit(1);
249 public:
252 * Get the environment map.
255 std::map<const char *, const char *, compare_strings> &get_map() {
256 return environment_map;
260 * Public set operations restrict valid names.
263 void set(const char *name, const char *value) {
264 name_check(name);
265 internal_set(name, value);
268 void unset(const char *name) {
269 name_check(name);
270 internal_unset(name);
273 void set_ptr(const char *name, const void *pointer) {
274 name_check(name);
275 internal_set_ptr(name, pointer);
278 const char *get(const char *name) {
279 if (environment_map.count(name) == 0)
280 return NULL;
282 return environment_map[name];
286 * Make an environment substructure. Note that since deep
287 * structures are currently referenced rather than copied when
288 * the stack is pushed, there is no current need for any
289 * chaining mechanism.
291 void make_substructure(const char *name) {
292 environment *s = new environment;
293 set_ptr(name, s);
294 environment_set.insert(s);
297 static int is_env(const char *name) {
298 void *ptr_value;
299 sscanf(name, "%p", &ptr_value);
302 * Check for bad pointers.
305 if (!environment_set.count((environment *) ptr_value)) {
306 return 0;
309 return 1;
312 const char *get_option_name(const char *name) {
313 if (strncmp(name, "0 ", strlen("0 ")))
314 return NULL;
316 name += strlen("0 ");
318 if (!isdigit(name[0]))
319 return NULL;
321 while (isdigit(name[0]))
322 name++;
324 if (!isspace(name[0]))
325 return NULL;
327 while (isspace(name[0]))
328 name++;
330 if (!isalnum(name[0]))
331 return NULL;
333 return name;
336 int is_option(const char *name) {
337 return (get_option_name(name) != NULL);
340 int is_arg(const char *name, unsigned int arg) {
341 assert (is_option(name));
343 int length = strlen(name) + 3 * sizeof(unsigned int);
345 char *desired_string = (char *) malloc(sizeof(char) * length);
347 snprintf(desired_string, length, "%u %s", arg, name + strlen("0 "));
349 int result = environment_map.count(desired_string);
351 free(desired_string);
353 return result > 0;
356 void remove_arg(const char *name, unsigned int arg) {
357 assert (is_option(name));
358 assert (is_arg(name, arg));
360 int length = strlen(name) + 3 * sizeof(unsigned int);
362 char *desired_string = (char *) malloc(sizeof(char) * length);
364 snprintf(desired_string, length, "%u %s", arg, name + strlen("0 "));
366 environment_map.erase(desired_string);
368 free(desired_string);
371 const char *get_string_arg(const char *name, unsigned int arg) {
372 assert (is_option(name));
374 int length = strlen(name) + 3 * sizeof(unsigned int);
376 char *desired_string = (char *) malloc(sizeof(char) * length);
378 snprintf(desired_string, length, "%u %s", arg, name + strlen("0 "));
380 const char *result = environment_map[desired_string];
382 assert (result);
384 free(desired_string);
386 return result;
389 long int get_long_arg(const char *name, unsigned int arg) {
390 assert (is_option(name));
392 const char *string = get_string_arg(name, arg);
393 char *endptr;
395 long int result = strtol(string, &endptr, 0);
397 if (endptr[0] != '\0') {
398 fprintf(stderr, "\n\nError: bad argument in `%s'.\n\n", get_option_name(name));
399 exit(1);
402 return result;
405 int get_int_arg(const char *name, unsigned int arg) {
406 return (int) get_long_arg(name, arg);
409 unsigned int get_unsigned_arg(const char *name, unsigned int arg) {
410 long int result = get_long_arg(name, arg);
412 if (result < 0) {
413 fprintf(stderr, "\n\nError: bad argument in `%s'.\n\n", get_option_name(name));
414 exit(1);
417 return (unsigned int) result;
420 double get_double_arg(const char *name, unsigned int arg) {
421 assert (is_option(name));
423 const char *string = get_string_arg(name, arg);
424 char *endptr;
426 double result = strtod(string, &endptr);
428 if (endptr[0] != '\0') {
429 fprintf(stderr, "\n\nError: bad argument in `%s'.\n\n", get_option_name(name));
430 exit(1);
433 return result;
436 static environment *get_env(const char *name) {
438 assert(name);
440 void *ptr_value;
441 sscanf(name, "%p", &ptr_value);
444 * Check for bad pointers.
447 if (!environment_set.count((environment *) ptr_value)) {
448 assert(0);
449 fprintf(stderr, "Bad environment pointer.\n");
450 exit(1);
453 return (environment *) ptr_value;
457 * Prepend to a list.
459 void prepend(const char *list, const char *element) {
460 environment *d = get_env(get(list));
461 make_substructure(list);
462 get_env(get(list))->set("a", element);
463 get_env(get(list))->set_ptr("d", d);
466 void prepend_ptr(const char *list, void *ptr) {
467 prepend(list, internal_convert_pointer(ptr));
471 * Clone the environment.
473 environment *clone() {
474 environment *e = new environment();
476 for (std::map<const char *, const char *, compare_strings>::iterator i = environment_map.begin();
477 i != environment_map.end(); i++) {
479 if (!name_ok(i->first))
480 continue;
482 if (is_env(i->second)) {
483 e->set_ptr(i->first, get_env(i->second)->clone());
484 } else {
485 e->set(i->first, i->second);
489 return e;
492 static environment *top() {
493 if (environment_stack.empty()) {
494 environment_stack.push(new environment);
495 environment_set.insert(environment_stack.top());
497 return environment_stack.top();
500 static void push() {
501 environment *e = new environment;
503 e->environment_map = environment_stack.top()->environment_map;
505 e->internal_set_ptr("---chain", environment_stack.top());
506 e->internal_set_ptr("---this", e);
507 e->make_substructure("---dup");
509 environment_stack.push(e);
510 environment_set.insert(e);
513 static void dup_second() {
514 environment_stack.top()->prepend_ptr("---dup",
515 environment::get_env(environment_stack.top()->get("---chain")));
518 static void push_and_dup_output() {
519 push();
520 dup_second();
523 static void pop() {
524 assert(!environment_stack.empty());
527 * Execution environments should never be referenced by
528 * structures further up the call chain, so they can
529 * safely be deleted. (XXX: In particular, while
530 * lexical scoping may require copying of execution
531 * environments from lower on the call chain, there is
532 * no obvious reason that a reference should be used in
533 * this case; a shallow copy should be used instead.)
536 environment_set.erase(environment_stack.top());
537 delete environment_stack.top();
539 environment_stack.pop();
543 * Set with duplication.
546 void set_with_dup(const char *name, const char *value) {
547 set(name, value);
549 if (!get("---dup"))
550 return;
552 environment *dup_item = get_env(get("---dup"));
554 assert (dup_item);
556 while (dup_item->get("a")) {
557 get_env(dup_item->get("a"))->set_with_dup(name, value);
558 assert(dup_item->get("d"));
559 dup_item = get_env(dup_item->get("d"));
560 assert(dup_item);
566 * Read tokens from a stream.
568 class token_reader {
569 public:
571 * Get the next token
573 virtual const char *get() = 0;
576 * Peek at the next token.
579 virtual const char *peek() = 0;
582 * Divert the stream until the next occurrence of TOKEN.
584 virtual token_reader *divert(const char *open_token, const char *close_token) = 0;
586 virtual int expects_exactly_one_option(void) {
587 return 0;
590 virtual ~token_reader() {
594 class argument_parsing_token_reader : public token_reader {
595 const char *index;
596 const char *separators;
597 public:
598 argument_parsing_token_reader(const char *s) {
599 index = s;
600 separators = "=";
603 int expects_exactly_one_option(void) {
604 return 1;
607 virtual const char *get() {
608 int length = strcspn(index, separators);
610 if (length == 0)
611 return NULL;
613 const char *result = strndup(index, length);
614 index += length;
616 if (strspn(index, separators) >= 1)
617 index++;
619 separators = ",";
621 return result;
624 virtual const char *peek() {
625 int length = strcspn(index, separators);
627 if (length == 0)
628 return NULL;
630 const char *result = strndup(index, length);
632 return result;
635 virtual token_reader *divert(const char *open_token, const char *close_token) {
636 assert(0);
637 return NULL;
641 class cstring_token_reader : public token_reader {
642 const char *separators;
643 const char *string;
644 int ephemeral;
646 cstring_token_reader(const char *s, int ephemeral) {
647 assert(ephemeral == 1);
649 separators = "\n \t";
650 string = s;
651 this->ephemeral = 1;
654 public:
655 cstring_token_reader(const char *s) {
656 separators = "\n \t";
657 string = s;
658 ephemeral = 0;
661 const char *get() {
663 string += strspn(string, separators);
665 size_t length_to_next = strcspn(string, separators);
667 if (length_to_next == 0)
668 return NULL;
670 const char *result = strndup(string, length_to_next);
672 string += length_to_next;
674 return result;
677 const char *peek() {
678 string += strspn(string, separators);
680 size_t length_to_next = strcspn(string, separators);
682 if (length_to_next == 0)
683 return NULL;
685 return strndup(string, length_to_next);
688 cstring_token_reader *divert(const char *open_token, const char *close_token) {
690 * This function might be broken.
693 assert(0);
695 int search = 0;
696 int next = strcspn(string, separators);
697 int depth = 0;
699 while (*(string + search) != '\0' &&
700 (depth || strcmp(close_token, (string + search)))) {
701 if (!strcmp(close_token, (string + search)))
702 depth--;
703 if (!strcmp(open_token, (string + search)))
704 depth++;
705 search = next;
706 next = strcspn((string + next), separators);
709 if (*(string + search) == '\0') {
710 fprintf(stderr, "Parse error: End of scope not found.");
711 exit(1);
714 cstring_token_reader *result = new cstring_token_reader(strndup(string, search), 1);
716 string += search;
719 * Eat the closing token.
722 get();
724 return result;
727 ~cstring_token_reader() {
728 if (ephemeral)
729 free((void *) string);
733 class cli_token_reader : public token_reader {
735 int arg_index;
736 int argc;
737 const char **argv;
739 public:
740 cli_token_reader(int c, const char *v[]) {
741 argc = c;
742 argv = v;
743 arg_index = 0;
746 const char *get() {
748 if (arg_index < argc)
749 return argv[arg_index++];
750 else
751 return NULL;
755 const char *peek() {
757 if (arg_index < argc)
758 return argv[arg_index];
759 else
760 return NULL;
764 cli_token_reader *divert(const char *open_token, const char *close_token) {
765 int search = 0;
766 int depth = 0;
768 while (arg_index + search < argc
769 && (depth || strcmp(argv[arg_index + search], close_token))) {
770 if (!strcmp(close_token, argv[arg_index + search]))
771 depth--;
772 if (!strcmp(open_token, argv[arg_index + search]))
773 depth++;
774 search++;
777 if (arg_index + search == argc) {
778 fprintf(stderr, "Parse error: end of scope not found.\n");
779 exit(1);
782 cli_token_reader *result = new cli_token_reader(search, argv + arg_index);
784 arg_index += search;
787 * Eat the closing token.
790 get();
792 return result;
797 struct simple_option {
798 const char *name;
799 const char *map_name;
800 const char *map_value;
801 int arg_count;
802 int multi;
805 static const char *supported_nonglobal_option_table[];
806 static const char *focus_prefixes[];
807 static simple_option simple_option_table[];
809 static int option_name_match(const char *unadorned, const char *token, int require_ornamentation = 1) {
810 int strip_max = 2;
812 if (!strcmp(unadorned, token) && !require_ornamentation)
813 return 1;
815 while (token[0] == '-' && strip_max) {
816 token++;
817 strip_max--;
818 if (!strcmp(unadorned, token))
819 return 1;
822 return 0;
825 static int is_scope_operator(const char *string) {
826 if (!strcmp("{", string)
827 || !strcmp("}", string)
828 || !strcmp("[", string)
829 || !strcmp("]", string)
830 || !strcmp("<", string)
831 || !strcmp(">", string))
832 return 1;
834 return 0;
837 static const char *option_name_gen(const char *unadorned, const char *map_name, int arg_num, int multi) {
838 static unsigned int multi_counter = 0;
840 if (map_name) {
841 unadorned = map_name;
844 int length = (strlen(unadorned) + sizeof(unsigned int) * 3 + sizeof(int) * 3 + 2) + 1;
846 char *result = (char *) malloc(sizeof(char) * length);
848 assert (result);
850 if (!multi) {
851 snprintf(result, length, "%u 0 %s", arg_num, unadorned);
852 } else {
855 * XXX: This assumes that generating calls for
856 * options other than 0 exist in the same
857 * multiplicity group as the most recently
858 * generated 0-option multiplicity.
861 if (arg_num == 0)
862 multi_counter++;
864 snprintf(result, length, "%u %u %s", arg_num, multi_counter, unadorned);
867 return result;
870 static environment *genv;
872 static const char *get_next(token_reader *tr, const char *option_name) {
873 const char *argument = tr->get();
875 if (argument == NULL) {
876 fprintf(stderr, "\n\nError: not enough arguments for `%s'.\n\n", option_name);
877 exit(1);
880 return argument;
883 static int table_contains(const char **haystack, const char *needle, int prefix_length = 0) {
885 if (needle == NULL)
886 return 0;
888 while (*haystack != NULL) {
889 if (prefix_length == 0 && !strcmp(*haystack, needle))
890 return 1;
891 if (prefix_length > 0 && !strncmp(*haystack, needle, prefix_length))
892 return 1;
893 haystack++;
896 return 0;
899 static int option_is_identical(environment *a, environment *b, const char *option_name) {
900 if (!a->is_option(option_name) || !b->is_option(option_name))
901 return 0;
903 int option_number = 0;
905 while (a->is_arg(option_name, option_number) || b->is_arg(option_name, option_number)) {
906 if (!a->is_arg(option_name, option_number)
907 || !b->is_arg(option_name, option_number))
908 return 0;
910 const char *a_str = a->get_string_arg(option_name, option_number);
911 const char *b_str = b->get_string_arg(option_name, option_number);
913 if (strcmp(a_str, b_str))
914 return 0;
916 option_number++;
919 return 1;
922 static void remove_option(environment *a, const char *option_name) {
923 assert(a->is_option(option_name));
925 int option_number = 0;
927 while (a->is_arg(option_name, option_number)) {
928 a->remove_arg(option_name, option_number);
929 option_number++;
933 static void remove_nonglobals(environment *a) {
934 assert(a);
936 std::stack<const char *> removal_stack;
938 for (std::map<const char *, const char *, compare_strings>::iterator i = a->get_map().begin();
939 i != a->get_map().end(); i++) {
941 if (!a->is_option(i->first))
942 continue;
944 if (!table_contains(supported_nonglobal_option_table, a->get_option_name(i->first)))
945 continue;
947 removal_stack.push(i->first);
950 while (!removal_stack.empty()) {
951 remove_option(a, removal_stack.top());
952 removal_stack.pop();
956 static void option_intersect(environment *a, environment *b) {
957 assert(a);
958 assert(b);
960 std::stack<const char *> removal_stack;
962 for (std::map<const char *, const char *, compare_strings>::iterator i = a->get_map().begin();
963 i != a->get_map().end(); i++) {
965 if (!a->is_option(i->first))
966 continue;
968 if (option_is_identical(a, b, i->first))
969 continue;
971 removal_stack.push(i->first);
974 while (!removal_stack.empty()) {
975 remove_option(a, removal_stack.top());
976 removal_stack.pop();
980 static void option_difference(environment *a, environment *b) {
981 assert(a);
982 assert(b);
984 std::stack<const char *> removal_stack;
986 for (std::map<const char *, const char *, compare_strings>::iterator i = a->get_map().begin();
987 i != a->get_map().end(); i++) {
989 if (!a->is_option(i->first))
990 continue;
992 if (!option_is_identical(a, b, i->first))
993 continue;
995 removal_stack.push(i->first);
998 while (!removal_stack.empty()) {
999 remove_option(a, removal_stack.top());
1000 removal_stack.pop();
1004 static void evaluate_stream(token_reader *tr,
1005 std::vector<std::pair<const char *, environment *> > *files) {
1006 const char *token;
1007 int end_of_options = 0;
1009 while ((token = tr->get())) {
1012 * Check for nesting
1015 if (!strcmp(token, "{") && !end_of_options) {
1016 environment::push_and_dup_output();
1017 token_reader *tr_nest = tr->divert("{", "}");
1018 evaluate_stream(tr_nest, files);
1019 delete tr_nest;
1020 environment::pop();
1021 } else if (!strcmp(token, "[") && !end_of_options) {
1022 global_options = 0;
1023 environment::push();
1024 token_reader *tr_nest = tr->divert("[", "]");
1025 evaluate_stream(tr_nest, files);
1026 delete tr_nest;
1027 environment::pop();
1028 } else if (!strcmp(token, "<") && !end_of_options) {
1029 environment *dup_list = environment::get_env(environment::top()->get("---dup"));
1030 assert (dup_list != NULL);
1031 dup_list = dup_list->clone();
1033 environment::dup_second();
1034 token_reader *tr_nest = tr->divert("<", ">");
1035 evaluate_stream(tr_nest, files);
1036 delete tr_nest;
1038 environment::top()->set_ptr("---dup", dup_list);
1042 * Check for non-whitespace argument separators
1045 else if (!end_of_options && token && token[0] == '-' && strchr(token, '=')) {
1046 environment::push_and_dup_output();
1047 token_reader *tr_nest = new argument_parsing_token_reader(token);
1048 evaluate_stream(tr_nest, files);
1049 delete tr_nest;
1050 environment::pop();
1054 * Trap the end-of-option indicator.
1057 else if (!strcmp(token, "--")) {
1058 global_options = 0;
1059 end_of_options = 1;
1063 * Check for options and filenames
1066 else {
1068 * Handle filenames.
1071 if (strncmp("-", token, strlen("-")) || end_of_options) {
1073 assert(files);
1075 global_options = 0;
1076 files->push_back(std::pair<const char *, environment *>(strdup(token),
1077 environment::top()->clone()));
1079 if (tr->expects_exactly_one_option() && tr->get()) {
1080 fprintf(stderr, "\n\nError: Too many arguments for `%s'.\n\n", token);
1081 exit(1);
1084 continue;
1088 * Handle focus option.
1091 if (option_name_match("focus", token)) {
1093 environment *target;
1094 target = environment::top();
1096 target->set_with_dup(option_name_gen("focus", NULL, 0, 0), "1");
1098 const char *option = get_next(tr, "focus");
1100 target->set_with_dup(option_name_gen("focus", NULL, 1, 0), option);
1102 if (!strcmp(option, "d")) {
1103 target->set_with_dup(option_name_gen("focus", NULL, 2, 0),
1104 get_next(tr, "focus"));
1105 } else if (!strcmp(option, "p")) {
1106 target->set_with_dup(option_name_gen("focus", NULL, 2, 0),
1107 get_next(tr, "focus"));
1108 target->set_with_dup(option_name_gen("focus", NULL, 3, 0),
1109 get_next(tr, "focus"));
1110 } else
1111 bad_arg("focus");
1113 int arg = 0;
1115 while (table_contains(focus_prefixes, tr->peek(), 3)) {
1116 target->set_with_dup(option_name_gen("focus", NULL, 4 + arg, 0),
1117 get_next(tr, "focus"));
1118 arg++;
1121 continue;
1125 * Handle simple options.
1128 int found_option = 0;
1129 for (int i = 0; simple_option_table[i].name; i++) {
1130 if (!option_name_match(simple_option_table[i].name, token))
1131 continue;
1134 * Handle the match case.
1137 found_option = 1;
1140 * Determine which environment should be modified
1143 environment *target;
1144 target = environment::top();
1147 * Store information required for
1148 * handling the local case later.
1151 const char *map_value = "1";
1153 if (simple_option_table[i].map_value) {
1154 map_value = simple_option_table[i].map_value;
1155 } else if (simple_option_table[i].map_name) {
1156 map_value = simple_option_table[i].name;
1159 target->set_with_dup(option_name_gen(simple_option_table[i].name,
1160 simple_option_table[i].map_name,
1162 simple_option_table[i].multi),
1163 map_value);
1165 for (int j = 0; j < simple_option_table[i].arg_count; j++) {
1166 const char *option = tr->get();
1168 if (option == NULL) {
1169 fprintf(stderr, "\n\nError: not enough options for `%s'.\n\n", token);
1170 exit(1);
1174 * Reject scope operators as options,
1175 * at least for now.
1178 if (is_scope_operator(option)) {
1179 fprintf(stderr, "\n\nError: illegal argument to `%s'.\n\n", token);
1180 exit(1);
1183 target->set_with_dup(option_name_gen(simple_option_table[i].name,
1184 simple_option_table[i].map_name,
1185 j + 1,
1186 simple_option_table[i].multi),
1187 option);
1192 * Trap illegal options.
1195 if (!found_option)
1196 ui::get()->illegal_option(token);
1199 if (tr->expects_exactly_one_option() && tr->get()) {
1200 fprintf(stderr, "\n\nError: Too many arguments for `%s'.\n\n", token);
1201 exit(1);
1206 public:
1208 * Input handler.
1210 * Does one of two things:
1212 * (1) Output version information if called with '--version'
1214 * (2) Read options and file arguments, and if the arguments are correct,
1215 * write output. If an error is detected, print the usage statement.
1219 static void handle(int argc, const char *argv[], const char *package, const char *short_version, const char *version) {
1222 * Initialize help object
1225 help hi(package, argv[0], short_version);
1228 * Output version information if --version appears
1229 * on the command line.
1232 if (arg_count(argc, argv, "--version")) {
1234 * Output the version
1237 fprintf(stdout, "%s", version);
1240 * Output relevant environment variables
1243 fprintf(stdout, "Environment:\n");
1245 const char *env_names[] = {
1246 "ALE_BIN",
1247 "DCRAW",
1248 "EXIF_UTILITY",
1249 "ALE_COUNT_THREADS",
1250 "PAGER",
1251 NULL
1254 for (int i = 0; env_names[i]; i++) {
1255 char *value = getenv(env_names[i]);
1257 fprintf(stdout, " %s=%s\n",
1258 env_names[i], value ? value : "");
1261 return;
1265 * Handle help options
1268 if (arg_prefix_count(argc, argv, "--h"))
1269 for (int i = 1; i < argc; i++) {
1270 int all = !strcmp(argv[i], "--hA");
1271 int is_help_option = !strncmp(argv[i], "--h", strlen("--h"));
1272 int found_help = 0;
1274 if (!strcmp(argv[i], "--hu") || all)
1275 hi.usage(), found_help = 1;
1276 if (!strcmp(argv[i], "--hq") || all)
1277 hi.defaults(), found_help = 1;
1278 if (!strcmp(argv[i], "--hf") || all)
1279 hi.file(), found_help = 1;
1280 if (!strcmp(argv[i], "--he") || all)
1281 hi.exclusion(), found_help = 1;
1282 if (!strcmp(argv[i], "--ha") || all)
1283 hi.alignment(), found_help = 1;
1284 if (!strcmp(argv[i], "--hr") || all)
1285 hi.rendering(), found_help = 1;
1286 if (!strcmp(argv[i], "--hx") || all)
1287 hi.exposure(), found_help = 1;
1288 if (!strcmp(argv[i], "--ht") || all)
1289 hi.tdf(), found_help = 1;
1290 if (!strcmp(argv[i], "--hl") || all)
1291 hi.filtering(), found_help = 1;
1292 if (!strcmp(argv[i], "--hd") || all)
1293 hi.device(), found_help = 1;
1294 if (!strcmp(argv[i], "--hi") || all)
1295 hi.interface(), found_help = 1;
1296 if (!strcmp(argv[i], "--hv") || all)
1297 hi.visp(), found_help = 1;
1298 if (!strcmp(argv[i], "--hc") || all)
1299 hi.cp(), found_help = 1;
1300 if (!strcmp(argv[i], "--h3") || all)
1301 hi.d3(), found_help = 1;
1302 if (!strcmp(argv[i], "--hs") || all)
1303 hi.scope(), found_help = 1;
1304 if (!strcmp(argv[i], "--hp") || all)
1305 hi.process(), found_help = 1;
1306 if (!strcmp(argv[i], "--hz") || all)
1307 hi.undocumented(), found_help = 1;
1309 if (is_help_option && !found_help)
1310 hi.usage();
1313 * Check for the end-of-options marker, a non-option argument,
1314 * or the end of arguments. In all of these cases, we exit.
1317 if (!strcmp(argv[i], "--")
1318 || strncmp(argv[i], "--", strlen("--"))
1319 || i == argc - 1)
1320 return;
1324 * Undocumented projective transformation utility
1327 if (arg_count(argc, argv, "--ptcalc") > 0) {
1328 fprintf(stderr, "\n\n*** Warning: this feature is not documented ***\n\n");
1329 printf("Enter: w h tlx tly blx bly brx bry trx try x y\n\n");
1331 double w, h, tlx, tly, blx, bly, brx, bry, trx, tr_y, x, y;
1333 printf("> ");
1335 if (scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
1336 &w, &h, &tlx, &tly, &blx, &bly, &brx, &bry, &trx, &tr_y, &x, &y) != 12) {
1338 fprintf(stderr, "Error reading input.\n");
1339 exit(1);
1342 d2::image *i = d2::new_image_ale_real((int)h, (int)w, 3);
1343 d2::transformation t = d2::transformation::gpt_identity(i, 1);
1344 d2::point q[4] = {
1345 d2::point(tly, tlx),
1346 d2::point(bly, blx),
1347 d2::point(bry, brx),
1348 d2::point(tr_y, trx)
1350 t.gpt_set(q);
1352 d2::point a(y, x), b;
1354 b = t.transform_scaled(a);
1356 printf("TRANSFORM t(a): (%f, %f)\n", (double) b[1], (double) b[0]);
1358 b = t.scaled_inverse_transform(a);
1360 printf("INVERSE t^-1(a): (%f, %f)\n", (double) b[1], (double) b[0]);
1362 exit(0);
1366 * Thread initialization.
1369 thread::init();
1372 * Flags and variables
1375 double scale_factor = 1;
1376 double vise_scale_factor = 1;
1377 #if 0
1378 double usm_multiplier = 0.0;
1379 #endif
1380 int extend = 0;
1381 struct d2::tload_t *tload = NULL;
1382 struct d2::tsave_t *tsave = NULL;
1383 struct d3::tload_t *d3_tload = NULL;
1384 struct d3::tsave_t *d3_tsave = NULL;
1385 int ip_iterations = 0;
1386 int ip_use_median = 0;
1387 double ipwl = 0;
1388 enum { psf_linear, psf_nonlinear, psf_N };
1389 const char *psf[psf_N] = {NULL, NULL};
1390 const char *device = NULL;
1391 int psf_match = 0;
1392 double psf_match_args[6];
1393 int inc = 0;
1394 int exposure_register = 1;
1395 const char *wm_filename = NULL;
1396 int wm_offsetx = 0, wm_offsety = 0;
1397 double cx_parameter = 1;
1398 double *d3px_parameters = NULL;
1399 int d3px_count = 0;
1400 d2::exclusion *ex_parameters = NULL;
1401 int ex_count = 0;
1402 int ex_show = 0;
1403 d2::render *achain;
1404 const char *achain_type = "triangle:2";
1405 const char *afilter_type = "internal";
1406 d2::render **ochain = NULL;
1407 const char **ochain_names = NULL;
1408 const char **ochain_types = NULL;
1409 const char *d3chain_type = NULL;
1410 int oc_count = 0;
1411 const char **visp = NULL;
1412 int vise_count = 0;
1413 const char **d3_output = NULL;
1414 const char **d3_depth = NULL;
1415 unsigned int d3_count = 0;
1416 double user_view_angle = 0;
1417 int user_bayer = IMAGE_BAYER_DEFAULT;
1418 d2::pixel exp_mult = d2::pixel(1, 1, 1);
1419 std::map<const char *, d3::pt> d3_output_pt;
1420 std::map<const char *, d3::pt> d3_depth_pt;
1421 double cache = 256; /* MB */
1424 * dchain is ochain[0].
1427 ochain = (d2::render **) local_realloc(ochain,
1428 (oc_count + 1) * sizeof(d2::render *));
1429 ochain_names = (const char **) local_realloc((void *)ochain_names,
1430 (oc_count + 1) * sizeof(const char *));
1431 ochain_types = (const char **) local_realloc((void *)ochain_types,
1432 (oc_count + 1) * sizeof(const char *));
1434 ochain_types[0] = "sinc*lanc:8";
1436 oc_count = 1;
1439 * Handle default settings
1442 if (arg_prefix_count(argc, argv, "--q") > 0)
1443 ui::get()->error("Default settings --q* are no longer recognized.");
1445 #define FIXED16 4
1446 #if ALE_COLORS == FIXED16
1447 const char *defaults =
1448 "--dchain auto:triangle:2,fine:box:1,triangle:2 "
1449 "--achain triangle:2 "
1450 "--ips 0 "
1451 "--3d-chain fine:triangle:2,fine:gauss:0.75,triangle:2 ";
1452 #else
1453 const char *defaults =
1454 "--dchain auto:triangle:2,fine:box:1,triangle:2 "
1455 "--achain triangle:2 "
1456 "--ips 1 "
1457 "--3d-chain fine:triangle:2,fine:gauss:0.75,triangle:2 ";
1458 #endif
1459 #undef FIXED16
1461 token_reader *default_reader = new cstring_token_reader(defaults);
1463 evaluate_stream(default_reader, NULL);
1466 * Set basic program information in the environment.
1469 environment::top()->set_with_dup("---package", package);
1470 environment::top()->set_with_dup("---short-version", short_version);
1471 environment::top()->set_with_dup("---version", version);
1472 environment::top()->set_with_dup("---invocation", argv[0]);
1475 * Initialize the top-level token-reader and generate
1476 * an environment variable for it.
1479 token_reader *tr = new cli_token_reader(argc - 1, argv + 1);
1480 environment::top()->set_ptr("---token-reader", tr);
1483 * Evaluate the command-line arguments to generate environment
1484 * structures.
1487 std::vector<std::pair<const char *, environment *> > files;
1489 evaluate_stream(tr, &files);
1492 * If there are fewer than two files, then output usage information.
1495 if (files.size() < 2) {
1496 hi.usage();
1497 exit(1);
1501 * Extract the global environment and check non-globals
1502 * against a list of supported non-global options.
1505 genv = files[0].second->clone();
1507 remove_nonglobals(genv);
1509 for (unsigned int i = 0; i < files.size(); i++) {
1510 option_intersect(genv, files[i].second);
1513 for (unsigned int i = 0; i < files.size(); i++) {
1514 option_difference(files[i].second, genv);
1516 for (std::map<const char *, const char *>::iterator j = files[i].second->get_map().begin();
1517 j != files[i].second->get_map().end(); j++) {
1519 environment *env = files[i].second;
1521 if (!env->is_option(j->first))
1522 continue;
1524 const char *option_name = env->get_option_name(j->first);
1526 if (!table_contains(supported_nonglobal_option_table, option_name)) {
1527 fprintf(stderr, "\n\nError: option `%s' must be applied globally.", option_name);
1528 fprintf(stderr, "\n\nHint: Move option `%s' prior to file and scope operators.\n\n",
1529 option_name);
1530 exit(1);
1536 * Iterate through the global environment,
1537 * looking for options.
1540 for (std::map<const char *, const char *>::iterator i = genv->get_map().begin();
1541 i != genv->get_map().end(); i++) {
1543 environment *env = genv;
1545 if (!env->is_option(i->first))
1546 continue;
1548 const char *option_name = env->get_option_name(i->first);
1550 if (!strcmp(option_name, "default")) {
1552 * Do nothing. Defaults have already been set.
1554 } else if (!strcmp(option_name, "bpc")) {
1555 if (!strcmp(env->get_string_arg(i->first, 0), "8bpc"))
1556 d2::image_rw::depth8();
1557 else if (!strcmp(env->get_string_arg(i->first, 0), "16bpc"))
1558 d2::image_rw::depth16();
1559 else
1560 assert(0);
1561 } else if (!strcmp(option_name, "format")) {
1562 if (!strcmp(env->get_string_arg(i->first, 0), "plain"))
1563 d2::image_rw::ppm_plain();
1564 else if (!strcmp(env->get_string_arg(i->first, 0), "raw"))
1565 d2::image_rw::ppm_raw();
1566 else if (!strcmp(env->get_string_arg(i->first, 0), "auto"))
1567 d2::image_rw::ppm_auto();
1568 else
1569 assert(0);
1570 } else if (!strcmp(option_name, "align")) {
1571 if (!strcmp(env->get_string_arg(i->first, 0), "align-all"))
1572 d2::align::all();
1573 else if (!strcmp(env->get_string_arg(i->first, 0), "align-green"))
1574 d2::align::green();
1575 else if (!strcmp(env->get_string_arg(i->first, 0), "align-sum"))
1576 d2::align::sum();
1577 else
1578 assert(0);
1579 } else if (!strcmp(option_name, "transformation")) {
1580 if (!strcmp(env->get_string_arg(i->first, 0), "translation"))
1581 d2::align::class_translation();
1582 else if (!strcmp(env->get_string_arg(i->first, 0), "euclidean"))
1583 d2::align::class_euclidean();
1584 else if (!strcmp(env->get_string_arg(i->first, 0), "projective"))
1585 d2::align::class_projective();
1586 else
1587 assert(0);
1588 } else if (!strcmp(option_name, "transformation-default")) {
1589 if (!strcmp(env->get_string_arg(i->first, 0), "identity"))
1590 d2::align::initial_default_identity();
1591 else if (!strcmp(env->get_string_arg(i->first, 0), "follow"))
1592 d2::align::initial_default_follow();
1593 else
1594 assert(0);
1595 } else if (!strcmp(option_name, "perturb")) {
1596 if (!strcmp(env->get_string_arg(i->first, 0), "perturb-output"))
1597 d2::align::perturb_output();
1598 else if (!strcmp(env->get_string_arg(i->first, 0), "perturb-source"))
1599 d2::align::perturb_source();
1600 else
1601 assert(0);
1602 } else if (!strcmp(option_name, "fail")) {
1603 if (!strcmp(env->get_string_arg(i->first, 0), "fail-optimal"))
1604 d2::align::fail_optimal();
1605 else if (!strcmp(env->get_string_arg(i->first, 0), "fail-default"))
1606 d2::align::fail_default();
1607 else
1608 assert(0);
1609 } else if (!strcmp(option_name, "profile")) {
1610 ui::set_profile();
1611 } else if (!strcmp(option_name, "extend")) {
1612 if (env->get_int_arg(i->first, 0))
1613 extend = 1;
1614 else
1615 extend = 0;
1616 } else if (!strcmp(option_name, "oc")) {
1617 if (env->get_int_arg(i->first, 0))
1618 d3::scene::oc();
1619 else
1620 d3::scene::no_oc();
1621 } else if (!strcmp(option_name, "focus")) {
1623 double one = +1;
1624 double zero = +0;
1625 double inf = one / zero;
1627 assert (isinf(inf) && inf > 0);
1630 * Focus type
1633 unsigned int type = 0;
1634 double distance = 0;
1635 double px = 0, py = 0;
1637 if (!strcmp(env->get_string_arg(i->first, 1), "d")) {
1639 type = 0;
1641 distance = env->get_double_arg(i->first, 2);
1643 } else if (!strcmp(env->get_string_arg(i->first, 1), "p")) {
1645 type = 1;
1647 px = env->get_double_arg(i->first, 2);
1648 py = env->get_double_arg(i->first, 3);
1650 } else {
1651 bad_arg(option_name);
1655 * Options
1658 unsigned int ci = 0;
1659 double fr = 0;
1660 double ht = 0;
1661 double vt = 0;
1662 double sd = 0;
1663 double ed = inf;
1664 double sx = -inf;
1665 double ex = inf;
1666 double sy = -inf;
1667 double ey = inf;
1668 double ap = 3;
1669 unsigned int sc = 3;
1670 unsigned int fs = 0;
1671 unsigned int sr = 0;
1673 for (int arg_num = 4; env->is_arg(i->first, arg_num); arg_num++) {
1674 const char *option = env->get_string_arg(i->first, arg_num);
1675 if (!strncmp(option, "ci=", 3)) {
1676 if(sscanf(option + 3, "%u", &ci) != 1)
1677 bad_arg("--focus");
1678 } else if (!strncmp(option, "fr=", 3)) {
1679 if(sscanf(option + 3, "%lf", &fr) != 1)
1680 bad_arg("--focus");
1681 } else if (!strncmp(option, "ht=", 3)) {
1682 if(sscanf(option + 3, "%lf", &ht) != 1)
1683 bad_arg("--focus");
1684 } else if (!strncmp(option, "vt=", 3)) {
1685 if(sscanf(option + 3, "%lf", &vt) != 1)
1686 bad_arg("--focus");
1687 } else if (!strncmp(option, "sy=", 3)) {
1688 if(sscanf(option + 3, "%lf", &sy) != 1)
1689 bad_arg("--focus");
1690 } else if (!strncmp(option, "ey=", 3)) {
1691 if(sscanf(option + 3, "%lf", &ey) != 1)
1692 bad_arg("--focus");
1693 } else if (!strncmp(option, "sx=", 3)) {
1694 if(sscanf(option + 3, "%lf", &sx) != 1)
1695 bad_arg("--focus");
1696 } else if (!strncmp(option, "ex=", 3)) {
1697 if(sscanf(option + 3, "%lf", &ex) != 1)
1698 bad_arg("--focus");
1699 } else if (!strncmp(option, "sd=", 3)) {
1700 if(sscanf(option + 3, "%lf", &sd) != 1)
1701 bad_arg("--focus");
1702 } else if (!strncmp(option, "ed=", 3)) {
1703 if(sscanf(option + 3, "%lf", &ed) != 1)
1704 bad_arg("--focus");
1705 } else if (!strncmp(option, "ap=", 3)) {
1706 if(sscanf(option + 3, "%lf", &ap) != 1)
1707 bad_arg("--focus");
1708 } else if (!strncmp(option, "sc=", 3)) {
1709 if(sscanf(option + 3, "%u", &sc) != 1)
1710 bad_arg("--focus");
1711 } else if (!strncmp(option, "sr=", 3)) {
1712 if (!strcmp(option, "sr=aperture")) {
1713 sr = 0;
1714 } else if (!strcmp(option, "sr=pixel")) {
1715 sr = 1;
1716 } else
1717 bad_arg("--focus");
1719 } else if (!strncmp(option, "fs=", 3)) {
1720 if (!strcmp(option, "fs=mean")) {
1721 fs = 0;
1722 } else if (!strcmp(option, "fs=median")) {
1723 fs = 1;
1724 } else
1725 bad_arg("--focus");
1726 } else
1727 bad_arg("--focus");
1730 d3::focus::add_region(type, distance, px, py, ci, fr, ht, vt, sd, ed, sx, ex, sy, ey, ap, sc, fs, sr);
1732 } else if (!strcmp(option_name, "3ddp") || !strcmp(option_name, "3dvp")) {
1733 d2::align::keep();
1736 * Unsupported configurations
1739 if (ip_iterations)
1740 unsupported::fornow("3D modeling with Irani-Peleg rendering");
1742 #if 0
1743 if (usm_multiplier)
1744 unsupported::fornow("3D modeling with unsharp mask");
1745 #endif
1748 * Initialize if necessary
1750 * Note: because their existence is checked as an
1751 * indicator of the presence of 3D arguments, we
1752 * initialize these structures here.
1755 if (d3_output == NULL) {
1756 d3_count = argc;
1757 d3_output = (const char **) calloc(d3_count, sizeof(char *));
1758 d3_depth = (const char **) calloc(d3_count, sizeof(char *));
1761 unsigned int width, height;
1762 double view_angle;
1763 double x, y, z;
1764 double P, Y, R;
1766 width = env->get_unsigned_arg(i->first, 1);
1767 height = env->get_unsigned_arg(i->first, 2);
1768 view_angle = env->get_double_arg(i->first, 3);
1769 x = env->get_double_arg(i->first, 4);
1770 y = env->get_double_arg(i->first, 5);
1771 z = env->get_double_arg(i->first, 6);
1772 P = env->get_double_arg(i->first, 7);
1773 Y = env->get_double_arg(i->first, 8);
1774 R = env->get_double_arg(i->first, 9);
1776 view_angle *= M_PI / 180;
1777 P *= M_PI / 180;
1778 Y *= M_PI / 180;
1779 R *= M_PI / 180;
1781 d2::transformation t =
1782 d2::transformation::eu_identity();
1783 t.set_domain(height, width);
1784 d3::pt _pt(t, d3::et(y, x, z, Y, P, R), view_angle);
1786 if (!strcmp(option_name, "3dvp")) {
1787 d3_output_pt[env->get_string_arg(i->first, 10)] = _pt;
1788 } else if (!strcmp(option_name, "3ddp")) {
1789 d3_depth_pt[env->get_string_arg(i->first, 10)] = _pt;
1790 } else {
1791 assert(0);
1793 } else if (!strcmp(option_name, "3dv")) {
1794 d2::align::keep();
1796 unsigned int frame_no;
1799 * Unsupported configurations
1802 if (ip_iterations)
1803 unsupported::fornow("3D modeling with Irani-Peleg rendering");
1805 #if 0
1806 if (usm_multiplier)
1807 unsupported::fornow("3D modeling with unsharp mask");
1808 #endif
1811 * Initialize if necessary
1814 if (d3_output == NULL) {
1815 d3_count = argc;
1816 d3_output = (const char **) calloc(d3_count, sizeof(char *));
1817 d3_depth = (const char **) calloc(d3_count, sizeof(char *));
1820 frame_no = env->get_int_arg(i->first, 1);
1822 if (frame_no >= d3_count)
1823 ui::get()->error("--3dv argument 0 is too large");
1825 if (d3_output[frame_no] != NULL) {
1826 unsupported::fornow ("Writing a single 3D view to more than one output file");
1829 d3_output[frame_no] = env->get_string_arg(i->first, 2);
1831 } else if (!strcmp(option_name, "3dd")) {
1832 d2::align::keep();
1834 unsigned int frame_no;
1837 * Unsupported configurations
1840 if (ip_iterations)
1841 unsupported::fornow("3D modeling with Irani-Peleg rendering");
1843 #if 0
1844 if (usm_multiplier)
1845 unsupported::fornow("3D modeling with unsharp mask");
1846 #endif
1849 * Initialize if necessary
1852 if (d3_output == NULL) {
1853 d3_count = argc;
1854 d3_output = (const char **) calloc(d3_count, sizeof(char *));
1855 d3_depth = (const char **) calloc(d3_count, sizeof(char *));
1858 frame_no = env->get_int_arg(i->first, 1);
1860 if (frame_no >= d3_count)
1861 ui::get()->error("--3dd argument 0 is too large");
1863 if (d3_depth[frame_no] != NULL) {
1864 unsupported::fornow ("Writing a single frame's depth info to more than one output file");
1867 d3_depth[frame_no] = env->get_string_arg(i->first, 2);
1869 } else if (!strcmp(option_name, "view-angle")) {
1870 user_view_angle = env->get_double_arg(i->first, 1) * M_PI / 180;
1871 } else if (!strcmp(option_name, "cpf-load")) {
1872 d3::cpf::init_loadfile(env->get_string_arg(i->first, 1));
1873 } else if (!strcmp(option_name, "accel")) {
1874 if (!strcmp(env->get_string_arg(i->first, 1), "gpu"))
1875 accel::set_gpu();
1876 else if (!strcmp(env->get_string_arg(i->first, 1), "cpu"))
1877 accel::set_cpu();
1878 else if (!strcmp(env->get_string_arg(i->first, 1), "accel"))
1879 accel::set_accel();
1880 else if (!strcmp(env->get_string_arg(i->first, 1), "auto"))
1881 accel::set_auto();
1882 else {
1883 fprintf(stderr, "Error: Unknown acceleration type '%s'\n",
1884 env->get_string_arg(i->first, 1));
1885 exit(1);
1887 } else if (!strcmp(option_name, "ui")) {
1888 if (!strcmp(env->get_string_arg(i->first, 1), "stream"))
1889 ui::set_stream();
1890 else if (!strcmp(env->get_string_arg(i->first, 1), "tty"))
1891 ui::set_tty();
1892 else if (!strcmp(env->get_string_arg(i->first, 1), "log"))
1893 ui::set_log();
1894 else if (!strcmp(env->get_string_arg(i->first, 1), "quiet"))
1895 ui::set_quiet();
1896 else {
1897 fprintf(stderr, "Error: Unknown user interface type '%s'\n",
1898 env->get_string_arg(i->first, 1));
1899 exit(1);
1901 } else if (!strcmp(option_name, "3d-fmr")) {
1902 d3::scene::fmr(env->get_double_arg(i->first, 1));
1903 } else if (!strcmp(option_name, "3d-dmr")) {
1904 d3::scene::dmr(env->get_double_arg(i->first, 1));
1905 } else if (!strcmp(option_name, "et")) {
1906 d3::scene::et(env->get_double_arg(i->first, 1));
1907 } else if (!strcmp(option_name, "st")) {
1908 d3::cpf::st(env->get_double_arg(i->first, 1));
1909 } else if (!strcmp(option_name, "di-lower")) {
1910 d3::scene::di_lower(env->get_double_arg(i->first, 1));
1911 } else if (!strcmp(option_name, "rc")) {
1912 d3::scene::rc(env->get_double_arg(i->first, 1));
1913 } else if (!strcmp(option_name, "do-try")) {
1914 d3::scene::do_try(env->get_double_arg(i->first, 1));
1915 } else if (!strcmp(option_name, "di-upper")) {
1916 d3::scene::di_upper(env->get_double_arg(i->first, 1));
1917 } else if (!strcmp(option_name, "fc")) {
1918 d3::scene::fc(env->get_double_arg(i->first, 1));
1919 } else if (!strcmp(option_name, "ecm")) {
1920 unsupported::discontinued("--ecm <x>");
1921 } else if (!strcmp(option_name, "acm")) {
1922 unsupported::discontinued("--acm <x>");
1923 } else if (!strcmp(option_name, "def-nn")) {
1924 d2::image_rw::def_nn(env->get_double_arg(i->first, 1));
1926 if (env->get_double_arg(i->first, 1) > 2) {
1927 fprintf(stderr, "\n\n*** Warning: --def-nn implementation is currently "
1928 "inefficient for large radii. ***\n\n");
1931 } else if (!strcmp(option_name, "fx")) {
1932 d3::scene::fx(env->get_double_arg(i->first, 1));
1933 } else if (!strcmp(option_name, "tcem")) {
1934 d3::scene::tcem(env->get_double_arg(i->first, 1));
1935 } else if (!strcmp(option_name, "oui")) {
1936 d3::scene::oui(env->get_unsigned_arg(i->first, 1));
1937 } else if (!strcmp(option_name, "pa")) {
1938 d3::scene::pa(env->get_unsigned_arg(i->first, 1));
1939 } else if (!strcmp(option_name, "pc")) {
1940 d3::scene::pc(env->get_string_arg(i->first, 1));
1941 } else if (!strcmp(option_name, "cw")) {
1942 d2::align::certainty_weighted(env->get_unsigned_arg(i->first, 0));
1943 } else if (!strcmp(option_name, "wm")) {
1944 if (wm_filename != NULL)
1945 ui::get()->error("only one weight map can be specified");
1947 wm_filename = env->get_string_arg(i->first, 1);
1948 wm_offsetx = env->get_int_arg(i->first, 2);
1949 wm_offsety = env->get_int_arg(i->first, 3);
1951 } else if (!strcmp(option_name, "fl")) {
1952 #ifdef USE_FFTW
1953 d2::align::set_frequency_cut(env->get_double_arg(i->first, 1),
1954 env->get_double_arg(i->first, 2),
1955 env->get_double_arg(i->first, 3));
1957 #else
1958 ui::get()->error_hint("--fl is not supported", "rebuild ALE with FFTW support");
1959 #endif
1960 } else if (!strcmp(option_name, "wmx")) {
1961 #ifdef USE_UNIX
1962 d2::align::set_wmx(env->get_string_arg(i->first, 1),
1963 env->get_string_arg(i->first, 2),
1964 env->get_string_arg(i->first, 3));
1965 #else
1966 ui::get()->error_hint("--wmx is not supported", "rebuild ALE with support for --wmx");
1967 #endif
1968 } else if (!strcmp(option_name, "flshow")) {
1969 d2::align::set_fl_show(env->get_string_arg(i->first, 1));
1970 } else if (!strcmp(option_name, "3dpx")) {
1972 d3px_parameters = (double *) local_realloc(d3px_parameters, (d3px_count + 1) * 6 * sizeof(double));
1974 for (int param = 0; param < 6; param++)
1975 d3px_parameters[6 * d3px_count + param] = env->get_double_arg(i->first, param + 1);
1978 * Swap x and y, since their internal meanings differ from their external meanings.
1981 for (int param = 0; param < 2; param++) {
1982 double temp = d3px_parameters[6 * d3px_count + 2 + param];
1983 d3px_parameters[6 * d3px_count + 2 + param] = d3px_parameters[6 * d3px_count + 0 + param];
1984 d3px_parameters[6 * d3px_count + 0 + param] = temp;
1989 * Increment counters
1992 d3px_count++;
1994 } else if (!strcmp(option_name, "ex") || !strcmp(option_name, "fex")) {
1996 ex_parameters = (d2::exclusion *) local_realloc(ex_parameters,
1997 (ex_count + 1) * sizeof(d2::exclusion));
1999 ex_parameters[ex_count].type = (!strcmp(option_name, "ex"))
2000 ? d2::exclusion::RENDER
2001 : d2::exclusion::FRAME;
2004 * Get parameters, swapping x and y coordinates
2007 ex_parameters[ex_count].x[0] = env->get_int_arg(i->first, 1 + 2);
2008 ex_parameters[ex_count].x[1] = env->get_int_arg(i->first, 1 + 3);
2009 ex_parameters[ex_count].x[2] = env->get_int_arg(i->first, 1 + 0);
2010 ex_parameters[ex_count].x[3] = env->get_int_arg(i->first, 1 + 1);
2011 ex_parameters[ex_count].x[4] = env->get_int_arg(i->first, 1 + 4);
2012 ex_parameters[ex_count].x[5] = env->get_int_arg(i->first, 1 + 5);
2015 * Increment counters
2018 ex_count++;
2020 } else if (!strcmp(option_name, "crop") || !strcmp(option_name, "fcrop")) {
2022 ex_parameters = (d2::exclusion *) local_realloc(ex_parameters,
2023 (ex_count + 4) * sizeof(d2::exclusion));
2025 for (int r = 0; r < 4; r++)
2026 ex_parameters[ex_count + r].type = (!strcmp(option_name, "crop"))
2027 ? d2::exclusion::RENDER
2028 : d2::exclusion::FRAME;
2031 int crop_args[6];
2033 for (int param = 0; param < 6; param++)
2034 crop_args[param] = env->get_int_arg(i->first, param + 1);
2037 * Construct exclusion regions from the crop area,
2038 * swapping x and y, since their internal meanings
2039 * differ from their external meanings.
2043 * Exclusion region 1: low x
2046 ex_parameters[ex_count + 0].x[0] = INT_MIN;
2047 ex_parameters[ex_count + 0].x[1] = crop_args[2] - 1;
2048 ex_parameters[ex_count + 0].x[2] = INT_MIN;
2049 ex_parameters[ex_count + 0].x[3] = INT_MAX;
2050 ex_parameters[ex_count + 0].x[4] = crop_args[4];
2051 ex_parameters[ex_count + 0].x[5] = crop_args[5];
2054 * Exclusion region 2: low y
2057 ex_parameters[ex_count + 1].x[0] = INT_MIN;
2058 ex_parameters[ex_count + 1].x[1] = INT_MAX;
2059 ex_parameters[ex_count + 1].x[2] = INT_MIN;
2060 ex_parameters[ex_count + 1].x[3] = crop_args[0] - 1;
2061 ex_parameters[ex_count + 1].x[4] = crop_args[4];
2062 ex_parameters[ex_count + 1].x[5] = crop_args[5];
2065 * Exclusion region 3: high y
2068 ex_parameters[ex_count + 2].x[0] = INT_MIN;
2069 ex_parameters[ex_count + 2].x[1] = INT_MAX;
2070 ex_parameters[ex_count + 2].x[2] = crop_args[1] + 1;
2071 ex_parameters[ex_count + 2].x[3] = INT_MAX;
2072 ex_parameters[ex_count + 2].x[4] = crop_args[4];
2073 ex_parameters[ex_count + 2].x[5] = crop_args[5];
2076 * Exclusion region 4: high x
2079 ex_parameters[ex_count + 3].x[0] = crop_args[3] + 1;
2080 ex_parameters[ex_count + 3].x[1] = INT_MAX;
2081 ex_parameters[ex_count + 3].x[2] = INT_MIN;
2082 ex_parameters[ex_count + 3].x[3] = INT_MAX;
2083 ex_parameters[ex_count + 3].x[4] = crop_args[4];
2084 ex_parameters[ex_count + 3].x[5] = crop_args[5];
2087 * Increment counters
2090 ex_count += 4;
2092 } else if (!strcmp(option_name, "exshow")) {
2093 ex_show = 1;
2094 } else if (!strcmp(option_name, "wt")) {
2095 d2::render::set_wt(env->get_double_arg(i->first, 1));
2096 } else if (!strcmp(option_name, "3d-chain")) {
2097 d3chain_type = env->get_string_arg(i->first, 1);
2098 } else if (!strcmp(option_name, "dchain")) {
2099 ochain_types[0] = env->get_string_arg(i->first, 1);
2100 } else if (!strcmp(option_name, "achain")) {
2101 achain_type = env->get_string_arg(i->first, 1);
2102 } else if (!strcmp(option_name, "afilter")) {
2103 afilter_type = env->get_string_arg(i->first, 1);
2104 } else if (!strcmp(option_name, "ochain")) {
2106 ochain = (d2::render **) local_realloc(ochain,
2107 (oc_count + 1) * sizeof(d2::render *));
2108 ochain_names = (const char **) local_realloc((void *)ochain_names,
2109 (oc_count + 1) * sizeof(const char *));
2110 ochain_types = (const char **) local_realloc((void *)ochain_types,
2111 (oc_count + 1) * sizeof(const char *));
2113 ochain_types[oc_count] = env->get_string_arg(i->first, 1);
2114 ochain_names[oc_count] = env->get_string_arg(i->first, 2);
2116 oc_count++;
2118 } else if (!strcmp(option_name, "visp")) {
2120 visp = (const char **) local_realloc((void *)visp, 4 *
2121 (vise_count + 1) * sizeof(const char *));
2123 for (int param = 0; param < 4; param++)
2124 visp[vise_count * 4 + param] = env->get_string_arg(i->first, param + 1);
2126 vise_count++;
2128 } else if (!strcmp(option_name, "cx")) {
2129 cx_parameter = env->get_int_arg(i->first, 0) ? env->get_double_arg(i->first, 1) : 0;
2130 } else if (!strcmp(option_name, "ip")) {
2131 unsupported::discontinued("--ip <r> <i>", "--lpsf box=<r> --ips <i>");
2132 } else if (!strcmp(option_name, "cache")) {
2133 cache = env->get_double_arg(i->first, 1);
2134 } else if (!strcmp(option_name, "resident")) {
2135 double resident = env->get_double_arg(i->first, 1);
2137 d2::image::set_resident(resident);
2139 } else if (!strcmp(option_name, "bayer")) {
2142 * External order is clockwise from top-left. Internal
2143 * order is counter-clockwise from top-left.
2146 const char *option = env->get_string_arg(i->first, 1);
2148 if (!strcmp(option, "rgbg")) {
2149 user_bayer = IMAGE_BAYER_RGBG;
2150 } else if (!strcmp(option, "bgrg")) {
2151 user_bayer = IMAGE_BAYER_BGRG;
2152 } else if (!strcmp(option, "gbgr")) {
2153 user_bayer = IMAGE_BAYER_GRGB;
2154 } else if (!strcmp(option, "grgb")) {
2155 user_bayer = IMAGE_BAYER_GBGR;
2156 } else if (!strcmp(option, "none")) {
2157 user_bayer = IMAGE_BAYER_NONE;
2158 } else {
2159 bad_arg("--bayer");
2162 } else if (!strcmp(option_name, "lpsf")) {
2163 psf[psf_linear] = env->get_string_arg(i->first, 1);
2164 } else if (!strcmp(option_name, "nlpsf")) {
2165 psf[psf_nonlinear] = env->get_string_arg(i->first, 1);
2166 } else if (!strcmp(option_name, "psf-match")) {
2168 psf_match = 1;
2170 for (int index = 0; index < 6; index++) {
2171 psf_match_args[index] = env->get_double_arg(i->first, index + 1);
2174 } else if (!strcmp(option_name, "device")) {
2175 device = env->get_string_arg(i->first, 1);
2176 #if 0
2177 } else if (!strcmp(option_name, "usm")) {
2179 if (d3_output != NULL)
2180 unsupported::fornow("3D modeling with unsharp mask");
2182 usm_multiplier = env->get_double_arg(i->first, 1);
2183 #endif
2185 } else if (!strcmp(option_name, "ipr")) {
2187 ip_iterations = env->get_int_arg(i->first, 1);
2189 ui::get()->warn("--ipr is deprecated. Use --ips instead");
2191 } else if (!strcmp(option_name, "cpp-err")) {
2192 if (!strcmp(env->get_string_arg(i->first, 0), "median"))
2193 d3::cpf::err_median();
2194 else if (!strcmp(env->get_string_arg(i->first, 0), "mean"))
2195 d3::cpf::err_mean();
2196 } else if (!strcmp(option_name, "vp-adjust")) {
2197 if (env->get_int_arg(i->first, 0))
2198 d3::align::vp_adjust();
2199 else
2200 d3::align::vp_noadjust();
2201 } else if (!strcmp(option_name, "vo-adjust")) {
2202 if (env->get_int_arg(i->first, 0))
2203 d3::align::vo_adjust();
2204 else
2205 d3::align::vo_noadjust();
2206 } else if (!strcmp(option_name, "ip-statistic")) {
2207 if (!strcmp(env->get_string_arg(i->first, 0), "mean"))
2208 ip_use_median = 0;
2209 else if (!strcmp(env->get_string_arg(i->first, 0), "median"))
2210 ip_use_median = 1;
2211 } else if (!strcmp(option_name, "ips")) {
2212 ip_iterations = env->get_int_arg(i->first, 1);
2213 } else if (!strcmp(option_name, "ip-wl")) {
2214 int limited = env->get_int_arg(i->first, 0);
2215 if (limited) {
2216 ipwl = env->get_double_arg(i->first, 1);
2217 } else {
2218 ipwl = 0;
2220 } else if (!strcmp(option_name, "ipc")) {
2221 unsupported::discontinued("--ipc <c> <i>", "--ips <i> --lpsf <c>", "--ips <i> --device <c>");
2222 } else if (!strcmp(option_name, "exp-extend")) {
2223 if (env->get_int_arg(i->first, 0))
2224 d2::image_rw::exp_scale();
2225 else
2226 d2::image_rw::exp_noscale();
2227 } else if (!strcmp(option_name, "exp-register")) {
2228 if (env->get_int_arg(i->first, 0) == 1) {
2229 exposure_register = 1;
2230 d2::align::exp_register();
2231 } else if (env->get_int_arg(i->first, 0) == 0) {
2232 exposure_register = 0;
2233 d2::align::exp_noregister();
2234 } else if (env->get_int_arg(i->first, 0) == 2) {
2235 exposure_register = 2;
2236 d2::align::exp_meta_only();
2238 } else if (!strcmp(option_name, "drizzle-only")) {
2239 unsupported::discontinued("--drizzle-only", "--dchain box:1");
2240 } else if (!strcmp(option_name, "subspace-traverse")) {
2241 unsupported::undocumented("--subspace-traverse");
2242 d3::scene::set_subspace_traverse();
2243 } else if (!strcmp(option_name, "3d-filter")) {
2244 if (env->get_int_arg(i->first, 0))
2245 d3::scene::filter();
2246 else
2247 d3::scene::nofilter();
2248 } else if (!strcmp(option_name, "occ-norm")) {
2249 if (env->get_int_arg(i->first, 0))
2250 d3::scene::nw();
2251 else
2252 d3::scene::no_nw();
2253 } else if (!strcmp(option_name, "inc")) {
2254 inc = env->get_int_arg(i->first, 0);
2255 } else if (!strcmp(option_name, "exp-mult")) {
2256 double exp_c, exp_r, exp_b;
2258 exp_c = env->get_double_arg(i->first, 1);
2259 exp_r = env->get_double_arg(i->first, 2);
2260 exp_b = env->get_double_arg(i->first, 3);
2262 exp_mult = d2::pixel(1/(exp_r * exp_c), 1/exp_c, 1/(exp_b * exp_c));
2264 } else if (!strcmp(option_name, "visp-scale")) {
2266 vise_scale_factor = env->get_double_arg(i->first, 1);
2268 if (vise_scale_factor <= 0.0)
2269 ui::get()->error("VISP scale must be greater than zero");
2271 if (!finite(vise_scale_factor))
2272 ui::get()->error("VISP scale must be finite");
2274 } else if (!strcmp(option_name, "scale")) {
2276 scale_factor = env->get_double_arg(i->first, 1);
2278 if (scale_factor <= 0)
2279 ui::get()->error("Scale factor must be greater than zero");
2281 if (!finite(scale_factor))
2282 ui::get()->error("Scale factor must be finite");
2284 } else if (!strcmp(option_name, "metric")) {
2285 d2::align::set_metric_exponent(env->get_double_arg(i->first, 1));
2286 } else if (!strcmp(option_name, "threshold")) {
2287 d2::align::set_match_threshold(env->get_double_arg(i->first, 1));
2288 } else if (!strcmp(option_name, "drizzle-diam")) {
2289 unsupported::discontinued("--drizzle-diam=<x>", "--dchain box:1");
2290 } else if (!strcmp(option_name, "perturb-lower")) {
2291 const char *option = env->get_string_arg(i->first, 1);
2292 double perturb_lower;
2293 int characters;
2294 sscanf(option, "%lf%n", &perturb_lower, &characters);
2295 if (perturb_lower <= 0)
2296 ui::get()->error("--perturb-lower= value is non-positive");
2298 if (*(option + characters) == '%')
2299 d2::align::set_perturb_lower(perturb_lower, 1);
2300 else
2301 d2::align::set_perturb_lower(perturb_lower, 0);
2302 } else if (!strcmp(option_name, "stepsize")) {
2303 ui::get()->warn("--stepsize is deprecated. Use --perturb-lower instead");
2304 d2::align::set_perturb_lower(env->get_double_arg(i->first, 1), 0);
2305 } else if (!strcmp(option_name, "va-upper")) {
2306 const char *option = env->get_string_arg(i->first, 1);
2307 double va_upper;
2308 int characters;
2309 sscanf(option, "%lf%n", &va_upper, &characters);
2310 if (*(option + characters) == '%')
2311 ui::get()->error("--va-upper= does not accept '%' arguments\n");
2312 else
2313 d3::cpf::set_va_upper(va_upper);
2314 } else if (!strcmp(option_name, "cpp-upper")) {
2315 const char *option = env->get_string_arg(i->first, 1);
2316 double perturb_upper;
2317 int characters;
2318 sscanf(option, "%lf%n", &perturb_upper, &characters);
2319 if (*(option + characters) == '%')
2320 ui::get()->error("--cpp-upper= does not currently accept '%' arguments\n");
2321 else
2322 d3::cpf::set_cpp_upper(perturb_upper);
2323 } else if (!strcmp(option_name, "cpp-lower")) {
2324 const char *option = env->get_string_arg(i->first, 1);
2325 double perturb_lower;
2326 int characters;
2327 sscanf(option, "%lf%n", &perturb_lower, &characters);
2328 if (*(option + characters) == '%')
2329 ui::get()->error("--cpp-lower= does not currently accept '%' arguments\n");
2330 else
2331 d3::cpf::set_cpp_lower(perturb_lower);
2332 } else if (!strcmp(option_name, "hf-enhance")) {
2333 unsupported::discontinued("--hf-enhance=<x>");
2334 } else if (!strcmp(option_name, "multi")) {
2335 d2::trans_multi::set_multi(env->get_string_arg(i->first, 1));
2336 } else if (!strcmp(option_name, "track")) {
2337 if (!strcmp(env->get_string_arg(i->first, 0), "none")) {
2338 d2::trans_multi::track_none();
2339 } else if (!strcmp(env->get_string_arg(i->first, 0), "primary")) {
2340 d2::trans_multi::track_primary();
2341 } else if (!strcmp(env->get_string_arg(i->first, 0), "point")) {
2342 d2::trans_multi::track_point(env->get_double_arg(i->first, 1),
2343 env->get_double_arg(i->first, 2));
2344 } else {
2345 assert(0);
2347 } else if (!strcmp(option_name, "gs")) {
2348 d2::align::gs(env->get_string_arg(i->first, 1));
2349 } else if (!strcmp(option_name, "rot-upper")) {
2350 d2::align::set_rot_max((int) floor(env->get_double_arg(i->first, 1)));
2351 } else if (!strcmp(option_name, "bda-mult")) {
2352 d2::align::set_bda_mult(env->get_double_arg(i->first, 1));
2353 } else if (!strcmp(option_name, "bda-rate")) {
2354 d2::align::set_bda_rate(env->get_double_arg(i->first, 1));
2355 } else if (!strcmp(option_name, "lod-preferred")) {
2356 d2::align::set_lod_preferred((int) floor(env->get_double_arg(i->first, 1)));
2357 } else if (!strcmp(option_name, "min-dimension")) {
2358 d2::align::set_min_dimension((int) ceil(env->get_double_arg(i->first, 1)));
2359 } else if (!strcmp(option_name, "cpf-load")) {
2360 d3::cpf::init_loadfile(env->get_string_arg(i->first, 1));
2361 #if 0
2362 } else if (!strcmp(option_name, "model-load")) {
2363 d3::scene::load_model(env->get_string_arg(i->first, 1));
2364 } else if (!strcmp(option_name, "model-save")) {
2365 d3::scene::save_model(env->get_string_arg(i->first, 1));
2366 #endif
2367 } else if (!strcmp(option_name, "trans-load")) {
2368 d2::tload_delete(tload);
2369 tload = d2::tload_new(env->get_string_arg(i->first, 1));
2370 d2::align::set_tload(tload);
2371 } else if (!strcmp(option_name, "trans-save")) {
2372 tsave_delete(tsave);
2373 tsave = d2::tsave_new(env->get_string_arg(i->first, 1));
2374 d2::align::set_tsave(tsave);
2375 } else if (!strcmp(option_name, "3d-trans-load")) {
2376 d3::tload_delete(d3_tload);
2377 d3_tload = d3::tload_new(env->get_string_arg(i->first, 1));
2378 d3::align::set_tload(d3_tload);
2379 } else if (!strcmp(option_name, "3d-trans-save")) {
2380 d3::tsave_delete(d3_tsave);
2381 d3_tsave = d3::tsave_new(env->get_string_arg(i->first, 1));
2382 d3::align::set_tsave(d3_tsave);
2383 } else {
2384 assert(0);
2389 * Initialize the interface.
2392 ui::get();
2395 * Apply implication logic.
2398 if (extend == 0 && vise_count != 0) {
2399 implication::changed("VISP requires increased image extents.",
2400 "Image extension is now enabled.",
2401 "--extend");
2402 extend = 1;
2405 if (psf_match && ex_count)
2406 unsupported::fornow("PSF calibration with exclusion regions.");
2409 if (d3_output != NULL && ip_iterations != 0)
2410 unsupported::fornow("3D modeling with Irani-Peleg rendering");
2412 #if 0
2413 if (extend == 0 && d3_output != NULL) {
2414 implication::changed("3D modeling requires increased image extents.",
2415 "Image extension is now enabled.",
2416 "--extend");
2417 extend = 1;
2419 #endif
2421 #if 0
2422 if (cx_parameter != 0 && !exposure_register) {
2423 implication::changed("Certainty-based rendering requires exposure registration.",
2424 "Exposure registration is now enabled.",
2425 "--exp-register");
2426 d2::align::exp_register();
2427 exposure_register = 1;
2429 #endif
2432 * Set alignment class exclusion region static variables
2435 d2::align::set_exclusion(ex_parameters, ex_count);
2438 * Initialize renderer class statics.
2441 d2::render::render_init(ex_count, ex_parameters, ex_show, extend, scale_factor);
2444 * Set confidence
2447 d2::exposure::set_confidence(cx_parameter);
2450 * Keep transformations for Irani-Peleg, psf-match, and
2451 * VISE
2454 if (ip_iterations > 0 || psf_match || vise_count > 0) {
2455 d2::align::keep();
2459 * Initialize device-specific variables
2462 // int input_file_count = argc - i - 1;
2463 int input_file_count = files.size() - 1;
2465 d2::psf *device_response[psf_N] = { NULL, NULL };
2466 d2::exposure **input_exposure = NULL;
2467 ale_pos view_angle = 43.7 * M_PI / 180;
2468 // ale_pos view_angle = 90 * M_PI / 180;
2469 input_exposure = (d2::exposure **)
2470 // malloc((argc - i - 1) * sizeof(d2::exposure *));
2471 malloc(input_file_count * sizeof(d2::exposure *));
2473 if (device != NULL) {
2474 if (!strcmp(device, "xvp610_640x480")) {
2475 device_response[psf_linear] = new xvp610_640x480::lpsf();
2476 device_response[psf_nonlinear] = new xvp610_640x480::nlpsf();
2477 for (int ii = 0; ii < input_file_count; ii++)
2478 input_exposure[ii] = new xvp610_640x480::exposure();
2479 view_angle = xvp610_640x480::view_angle();
2480 } else if (!strcmp(device, "xvp610_320x240")) {
2481 device_response[psf_linear] = new xvp610_320x240::lpsf();
2482 device_response[psf_nonlinear] = new xvp610_320x240::nlpsf();
2483 for (int ii = 0; ii < input_file_count; ii++)
2484 input_exposure[ii] = new xvp610_320x240::exposure();
2485 view_angle = xvp610_320x240::view_angle();
2486 } else if (!strcmp(device, "ov7620")) {
2487 device_response[psf_linear] = new ov7620_raw_linear::lpsf();
2488 device_response[psf_nonlinear] = NULL;
2489 for (int ii = 0; ii < input_file_count; ii++)
2490 input_exposure[ii] = new ov7620_raw_linear::exposure();
2491 d2::image_rw::set_default_bayer(IMAGE_BAYER_BGRG);
2492 } else if (!strcmp(device, "canon_300d")) {
2493 device_response[psf_linear] = new canon_300d_raw_linear::lpsf();
2494 device_response[psf_nonlinear] = NULL;
2495 for (int ii = 0; ii < input_file_count; ii++)
2496 input_exposure[ii] = new canon_300d_raw_linear::exposure();
2497 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG);
2498 } else if (!strcmp(device, "nikon_d50")) {
2499 device_response[psf_linear] = nikon_d50::lpsf();
2500 device_response[psf_nonlinear] = nikon_d50::nlpsf();
2501 for (int ii = 0; ii < input_file_count; ii++)
2502 input_exposure[ii] = new nikon_d50::exposure();
2503 d2::image_rw::set_default_bayer( nikon_d50::bayer() );
2504 } else if (!strcmp(device, "canon_300d+85mm_1.8")) {
2505 device_response[psf_linear] = new canon_300d_raw_linear_85mm_1_8::lpsf();
2506 device_response[psf_nonlinear] = NULL;
2507 for (int ii = 0; ii < input_file_count; ii++)
2508 input_exposure[ii] = new canon_300d_raw_linear_85mm_1_8::exposure();
2509 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG);
2510 view_angle = canon_300d_raw_linear_85mm_1_8::view_angle();
2511 } else if (!strcmp(device, "canon_300d+50mm_1.8")) {
2512 device_response[psf_linear] = new canon_300d_raw_linear_50mm_1_8::lpsf();
2513 device_response[psf_nonlinear] = NULL;
2514 for (int ii = 0; ii < input_file_count; ii++)
2515 input_exposure[ii] = new canon_300d_raw_linear_50mm_1_8::exposure();
2516 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG);
2517 view_angle = canon_300d_raw_linear_50mm_1_8::view_angle();
2518 } else if (!strcmp(device, "canon_300d+50mm_1.4")) {
2519 device_response[psf_linear] = new canon_300d_raw_linear_50mm_1_4::lpsf();
2520 device_response[psf_nonlinear] = NULL;
2521 for (int ii = 0; ii < input_file_count; ii++)
2522 input_exposure[ii] = new canon_300d_raw_linear_50mm_1_4::exposure();
2523 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG);
2524 view_angle = canon_300d_raw_linear_50mm_1_4::view_angle();
2525 } else if (!strcmp(device, "canon_300d+50mm_1.4@1.4")) {
2526 device_response[psf_linear] = new canon_300d_raw_linear_50mm_1_4_1_4::lpsf();
2527 device_response[psf_nonlinear] = NULL;
2528 for (int ii = 0; ii < input_file_count; ii++)
2529 input_exposure[ii] = new canon_300d_raw_linear_50mm_1_4_1_4::exposure();
2530 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG);
2531 view_angle = canon_300d_raw_linear_50mm_1_4_1_4::view_angle();
2532 } else {
2533 ui::get()->unknown_device(device);
2535 } else {
2536 for (int ii = 0; ii < input_file_count; ii++)
2537 input_exposure[ii] = new d2::exposure_default();
2541 * User-specified variables.
2544 if (user_view_angle != 0) {
2545 view_angle = user_view_angle;
2548 if (user_bayer != IMAGE_BAYER_DEFAULT) {
2549 d2::image_rw::set_default_bayer(user_bayer);
2553 * PSF-match exposure.
2555 if (psf_match) {
2556 delete input_exposure[input_file_count - 1];
2557 input_exposure[input_file_count - 1] = new d2::exposure_default();
2561 * Initialize output exposure
2564 d2::exposure *output_exposure = new d2::exposure_default();
2565 output_exposure->set_multiplier(exp_mult);
2568 * Configure the response function.
2571 d2::psf *response[2] = {NULL, NULL};
2573 for (int n = 0; n < psf_N; n++ ) {
2574 if (psf[n] != NULL) {
2576 response[n] = d2::psf_parse::get((n == psf_linear), psf[n]);
2578 } else if (device_response[n] != NULL) {
2581 * Device-specific response
2584 response[n] = device_response[n];
2586 } else {
2589 * Default point-spread function.
2592 if (n == psf_linear) {
2595 * Default lpsf is a box filter
2596 * of diameter 1.0 (radius
2597 * 0.5).
2600 response[n] = new d2::box(0.5);
2602 } else if (n == psf_nonlinear) {
2605 * nlpsf is disabled by default.
2608 response[n] = NULL;
2614 * First file argument. Print general file information as well
2615 * as information specific to this argument. Initialize image
2616 * file handler.
2619 // d2::image_rw::init(argc - i - 1, argv + i, argv[argc - 1], input_exposure, output_exposure);
2620 // ochain_names[0] = argv[argc - 1];
2622 const char **input_files = (const char **) malloc(sizeof(const char *) * input_file_count);
2623 for (int i = 0; i < input_file_count; i++) {
2624 input_files[i] = files[i].first;
2627 d2::image_rw::init(input_file_count, input_files, files[files.size() - 1].first,
2628 input_exposure, output_exposure);
2630 ale_image_seq image_seq = ale_new_image_seq(open_file_by_number, NULL, cache);
2632 ochain_names[0] = files[files.size() - 1].first;
2635 * Handle control point data for alignment
2637 d2::align::set_cp_count(d3::cpf::count());
2638 for (unsigned int ii = 0; ii < d3::cpf::count(); ii++)
2639 d2::align::set_cp(ii, d3::cpf::get_2d(ii));
2642 * PSF-match bayer patterns.
2645 if (psf_match) {
2646 // d2::image_rw::set_specific_bayer(argc - i - 2, IMAGE_BAYER_NONE);
2647 d2::image_rw::set_specific_bayer(input_file_count - 1, IMAGE_BAYER_NONE);
2651 * Handle alignment weight map, if necessary
2654 if (wm_filename != NULL) {
2655 d2::image *weight_map;
2656 weight_map = d2::image_rw::read_image(wm_filename, new d2::exposure_linear());
2657 weight_map->set_offset(wm_offsety, wm_offsetx);
2658 d2::align::set_weight_map(weight_map);
2662 * Initialize alignment interpolant.
2665 if (strcmp(afilter_type, "internal"))
2666 d2::align::set_interpolant(d2::render_parse::get_SSF(afilter_type));
2669 * Initialize achain and ochain.
2672 achain = d2::render_parse::get(achain_type);
2674 for (int chain = 0; chain < oc_count; chain++)
2675 ochain[chain] = d2::render_parse::get(ochain_types[chain]);
2678 * Use merged renderings as reference images in
2679 * alignment.
2682 d2::align::set_reference(achain);
2685 * Tell the alignment class about the scale factor.
2688 d2::align::set_scale(scale_factor);
2691 * Initialize visp.
2694 d2::vise_core::set_scale(vise_scale_factor);
2696 for (int opt = 0; opt < vise_count; opt++) {
2697 d2::vise_core::add(d2::render_parse::get(visp[opt * 4 + 0]),
2698 visp[opt * 4 + 1],
2699 visp[opt * 4 + 2],
2700 visp[opt * 4 + 3]);
2704 * Initialize non-incremental renderers
2707 #if 0
2708 if (usm_multiplier != 0) {
2711 * Unsharp Mask renderer
2714 ochain[0] = new d2::usm(ochain[0], scale_factor,
2715 usm_multiplier, inc, response[psf_linear],
2716 response[psf_nonlinear], &input_exposure[0]);
2718 #endif
2720 if (psf_match) {
2723 * Point-spread function calibration renderer.
2724 * This renderer does not produce image output.
2725 * It is reserved for use with the point-spread
2726 * function calibration script
2727 * ale-psf-calibrate.
2730 ochain[0] = new d2::psf_calibrate(ochain[0],
2731 1, inc, response[psf_linear],
2732 response[psf_nonlinear],
2733 psf_match_args);
2735 } else if (ip_iterations != 0) {
2738 * Irani-Peleg renderer
2741 ochain[0] = new d2::ipc( ochain[0], ip_iterations,
2742 inc, response[psf_linear],
2743 response[psf_nonlinear],
2744 (exposure_register == 1), ip_use_median, ipwl);
2748 * Iterate through all files.
2751 for (unsigned int j = 0; j < d2::image_rw::count(); j++) {
2754 * Iterate through non-global options
2757 environment *env = files[j].second;
2759 for (std::map<const char *, const char *>::iterator i = env->get_map().begin();
2760 i != env->get_map().end(); i++) {
2762 if (!env->is_option(i->first))
2763 continue;
2765 const char *option_name = env->get_option_name(i->first);
2767 if (!strcmp(option_name, "mc")) {
2768 d2::align::mc(env->get_double_arg(i->first, 1));
2769 } else if (!strcmp(option_name, "md")) {
2770 d2::trans_multi::set_md(env->get_double_arg(i->first, 1));
2771 } else if (!strcmp(option_name, "ma-cert")) {
2772 d2::align::set_ma_cert(env->get_double_arg(i->first, 1));
2773 } else if (!strcmp(option_name, "mi")) {
2774 d2::trans_multi::set_mi(env->get_double_arg(i->first, 1));
2775 } else if (!strcmp(option_name, "gs-mo")) {
2776 const char *option = env->get_string_arg(i->first, 1);
2777 double gs_mo;
2778 int characters;
2779 sscanf(option, "%lf%n", &gs_mo, &characters);
2780 if (*(option + characters) == '%')
2781 d2::align::gs_mo(gs_mo, 1);
2782 else
2783 d2::align::gs_mo(gs_mo, 0);
2784 } else if (!strcmp(option_name, "ev")) {
2785 double ev = env->get_double_arg(i->first, 1);
2786 double gain_value = pow(2, -ev);
2788 if (j == 0)
2789 d2::exposure::set_gain_reference(gain_value);
2790 else
2791 input_exposure[j]->set_gain_multiplier(
2792 (double) d2::exposure::get_gain_reference()
2793 / gain_value);
2795 } else if (!strcmp(option_name, "black")) {
2796 double black = env->get_double_arg(i->first, 1);
2797 input_exposure[j]->set_black_level(black);
2798 } else if (!strcmp(option_name, "perturb-upper")) {
2799 const char *option = env->get_string_arg(i->first, 1);
2800 double perturb_upper;
2801 int characters;
2802 sscanf(option, "%lf%n", &perturb_upper, &characters);
2803 if (*(option + characters) == '%')
2804 d2::align::set_perturb_upper(perturb_upper, 1);
2805 else
2806 d2::align::set_perturb_upper(perturb_upper, 0);
2807 } else if (!strcmp(option_name, "threads")) {
2808 thread::set_count((unsigned int) env->get_int_arg(i->first, 1));
2809 } else if (!strcmp(option_name, "per-cpu")) {
2810 thread::set_per_cpu((unsigned int) env->get_int_arg(i->first, 1));
2811 } else {
2813 * This error should be encountered earlier.
2816 assert(0);
2818 fprintf(stderr, "\n\nError: option `%s' must be applied globally.", option_name);
2819 fprintf(stderr, "\n\nHint: Move option `%s' prior to file and scope operators.\n\n",
2820 option_name);
2821 exit(1);
2827 if (j == 0) {
2830 * Write comment information about original frame and
2831 * target image to the transformation save file, if we
2832 * have one.
2835 tsave_orig(tsave, files[0].first);
2836 tsave_target(tsave, files[files.size() - 1].first);
2839 * Handle the original frame.
2842 // ui::get()->original_frame_start(argv[i]);
2843 ui::get()->original_frame_start(files[0].first);
2845 for (int opt = 0; opt < oc_count; opt++) {
2846 ui::get()->set_orender_current(opt);
2847 ochain[opt]->sync(0);
2848 if (inc) {
2849 ui::get()->writing_output(opt);
2850 d2::image_rw::write_image(ochain_names[opt],
2851 ochain[opt]->get_image(0));
2855 d2::vise_core::frame_queue_add(0);
2857 ui::get()->original_frame_done();
2859 continue;
2863 * Handle supplemental frames.
2866 const char *name = d2::image_rw::name(j);
2868 ui::get()->supplemental_frame_start(name);
2871 * Write comment information about the
2872 * supplemental frame to the transformation
2873 * save file, if we have one.
2876 tsave_info (tsave, name);
2878 for (int opt = 0; opt < oc_count; opt++) {
2879 ui::get()->set_orender_current(opt);
2880 ochain[opt]->sync(j);
2881 if (inc) {
2882 ui::get()->writing_output(opt);
2883 d2::image_rw::write_image(ochain_names[opt],
2884 ochain[opt]->get_image(j));
2888 d2::vise_core::frame_queue_add(j);
2890 ui::get()->supplemental_frame_done();
2894 * Do any post-processing and output final image
2896 * XXX: note that the Irani-Peleg renderer currently
2897 * returns zero for ochain[0]->sync(), since it writes
2898 * output internally when inc != 0.
2900 * XXX: Leave ochain[0] last, since this may allow disposal of
2901 * rendering structures not required by the Irani-Peleg
2902 * renderer.
2905 for (int opt = 1; opt < oc_count; opt++)
2906 if ((ochain[opt]->sync() || !inc) && !psf_match) {
2907 ui::get()->writing_output(opt);
2908 d2::image_rw::write_image(ochain_names[opt], ochain[opt]->get_image());
2911 if (oc_count > 0)
2912 if ((ochain[0]->sync() || !inc) && !psf_match) {
2913 ui::get()->writing_output(0);
2914 d2::image_rw::write_image(ochain_names[0], ochain[0]->get_image());
2918 * Output a summary match statistic.
2921 ui::get()->ale_2d_done((double) d2::align::match_summary());
2924 * Perform any 3D tasks
2927 optimizations::begin_3d_work();
2929 if (d3_count > 0) {
2931 ui::get()->d3_start();
2933 d3::align::init_angle(view_angle);
2935 ui::get()->d3_init_view_angle((double) view_angle / M_PI * 180);
2937 d3::align::init_from_d2();
2939 if (d3::cpf::count() > 0) {
2940 ui::get()->d3_control_point_solve();
2941 d3::cpf::solve_3d();
2942 ui::get()->d3_control_point_solve_done();
2945 ui::get()->d3_final_view_angle(d3::align::angle_of(0) / M_PI * 180);
2947 d3::align::write_alignments();
2949 d3::scene::set_filter_type(d3chain_type);
2951 d3::scene::init_from_d2();
2953 ui::get()->d3_subdividing_space();
2954 d3::scene::make_space(d3_depth, d3_output, &d3_depth_pt, &d3_output_pt);
2955 ui::get()->d3_subdividing_space_done();
2957 ui::get()->d3_updating_occupancy();
2958 d3::scene::reduce_cost_to_search_depth(output_exposure, inc);
2959 ui::get()->d3_updating_occupancy_done();
2961 d3::scene::d3px(d3px_count, d3px_parameters);
2962 int view_count = 0;
2963 for (unsigned int i = 0; i < d2::image_rw::count(); i++) {
2964 assert (i < d3_count);
2966 if (d3_depth[i] != NULL) {
2967 ui::get()->d3_writing_output(d3_depth[i]);
2968 ui::get()->d3_render_status(0, 0, -1, -1, -1, -1, 0);
2969 const d2::image *im = d3::scene::depth(i);
2970 d2::image_rw::write_image(d3_depth[i], im, output_exposure, 1, 1);
2971 delete im;
2972 ui::get()->d3_writing_output_done();
2975 if (d3_output[i] != NULL) {
2976 ui::get()->d3_writing_output(d3_output[i]);
2977 const d2::image *im = d3::scene::view(i);
2978 d2::image_rw::write_image(d3_output[i], im, output_exposure);
2979 delete im;
2980 d3::focus::set_camera(view_count++);
2981 ui::get()->d3_writing_output_done();
2984 for (std::map<const char *, d3::pt>::iterator i = d3_output_pt.begin();
2985 i != d3_output_pt.end(); i++) {
2987 ui::get()->d3_writing_output(i->first);
2988 const d2::image *im = d3::scene::view(i->second);
2989 d2::image_rw::write_image(i->first, im, output_exposure);
2990 delete im;
2991 d3::focus::set_camera(view_count++);
2992 ui::get()->d3_writing_output_done();
2995 for (std::map<const char *, d3::pt>::iterator i = d3_depth_pt.begin();
2996 i != d3_depth_pt.end(); i++) {
2998 ui::get()->d3_writing_output(i->first);
2999 ui::get()->d3_render_status(0, 0, -1, -1, -1, -1, 0);
3000 const d2::image *im = d3::scene::depth(i->second);
3001 d2::image_rw::write_image(i->first, im, output_exposure, 1, 1);
3002 delete im;
3003 ui::get()->d3_writing_output_done();
3007 for (unsigned int i = d2::image_rw::count(); i < d3_count; i++) {
3008 if (d3_depth[i] != NULL) {
3009 fprintf(stderr, "\n\n*** Frame number for --3dd too high. ***\n\n");
3011 if (d3_output[i] != NULL) {
3012 fprintf(stderr, "\n\n*** Frame number for --3dv too high. ***\n\n");
3018 * Destroy the image file handler
3021 d2::image_rw::destroy();
3024 * Delete the transformation file structures, if any
3025 * exist.
3028 tsave_delete(tsave);
3029 tload_delete(tload);
3032 * We're done.
3035 exit(0);
3039 #endif