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
25 * ANSI C and POSIX include files.
37 #include "../ale_math.h"
44 #include "unsupported.h"
45 #include "implication.h"
67 #include "../ale_pos.h"
68 #include "../ale_real.h"
83 * Thread include files
86 #include "../thread.h"
89 * Device configuration files
92 #include "../device/xvp610_320x240.h"
93 #include "../device/xvp610_640x480.h"
94 #include "../device/ov7620_raw_linear.h"
95 #include "../device/canon_300d_raw_linear.h"
96 #include "../device/canon_300d_raw_linear_85mm_1_8.h"
97 #include "../device/canon_300d_raw_linear_50mm_1_8.h"
98 #include "../device/canon_300d_raw_linear_50mm_1_4.h"
99 #include "../device/canon_300d_raw_linear_50mm_1_4_1_4.h"
100 #include "../device/nikon_d50.h"
111 * Flag for global options.
114 static int global_options
;
123 * Counts instances of a given option.
125 static unsigned int arg_count(int argc
, const char *argv
[], const char *arg
) {
126 unsigned int count
= 0;
127 for (int i
= 0; i
< argc
; i
++) {
128 if (!strcmp(argv
[i
], arg
))
130 else if (!strcmp(argv
[i
], "--"))
137 * Argument prefix counter.
139 * Counts instances of a given option prefix.
141 static unsigned int arg_prefix_count(int argc
, const char *argv
[], const char *pfix
) {
142 unsigned int count
= 0;
143 for (int i
= 0; i
< argc
; i
++) {
144 if (!strncmp(argv
[i
], pfix
, strlen(pfix
)))
146 else if (!strcmp(argv
[i
], "--"))
153 * Reallocation function
155 static void *local_realloc(void *ptr
, size_t size
) {
156 void *new_ptr
= realloc(ptr
, size
);
159 ui::get()->memory_error_location("main()");
165 * Not enough arguments function.
167 static void not_enough(const char *opt_name
) {
168 ui::get()->cli_not_enough(opt_name
);
172 * Bad argument function
174 static void bad_arg(const char *opt_name
) {
175 ui::get()->cli_bad_arg(opt_name
);
179 * String comparison class.
182 class compare_strings
{
184 int operator()(const char *A
, const char *B
) const {
185 return strcmp(A
, B
) < 0;
190 * Environment structures.
192 * XXX: It's arguable that these should be public members of the
193 * 'input' class in order to allow passing environment values to other
194 * classes, but, since we're currently using them only to prepare state
195 * for an internal 'input' function, they can stay private for now. A
196 * more nuanced approach will likely be required later.
200 static std::stack
<environment
*> environment_stack
;
201 static std::set
<environment
*> environment_set
;
203 std::map
<const char *, const char *, compare_strings
> environment_map
;
206 * Internal set operations do not protect any data.
209 void internal_set(const char *name
, const char *value
) {
210 environment_map
[name
] = value
;
213 void internal_unset(const char *name
) {
214 environment_map
.erase(name
);
217 const char *internal_convert_pointer(const void *pointer
) {
218 int chars
= sizeof(void *) * 2 + 3;
219 char *c
= (char *) malloc(sizeof(char) * chars
);
224 ui::get()->memory_error_location("environment::set_ptr");
226 int count
= snprintf(c
, chars
, "%p", pointer
);
228 assert (count
>= 0 && count
< chars
);
233 void internal_set_ptr(const char *name
, const void *pointer
) {
234 internal_set(name
, internal_convert_pointer(pointer
));
238 * Check for restricted names.
241 int name_ok(const char *name
) {
242 if (!strcmp(name
, "---chain") || !strcmp(name
, "---this"))
248 void name_check(const char *name
) {
249 if (!name_ok(name
)) {
250 fprintf(stderr
, "Bad set operation.");
259 * Get the environment map.
262 std::map
<const char *, const char *, compare_strings
> &get_map() {
263 return environment_map
;
267 * Public set operations restrict valid names.
270 void set(const char *name
, const char *value
) {
272 internal_set(name
, value
);
275 void unset(const char *name
) {
277 internal_unset(name
);
280 void set_ptr(const char *name
, const void *pointer
) {
282 internal_set_ptr(name
, pointer
);
285 const char *get(const char *name
) {
286 if (environment_map
.count(name
) == 0)
289 return environment_map
[name
];
293 * Make an environment substructure. Note that since deep
294 * structures are currently referenced rather than copied when
295 * the stack is pushed, there is no current need for any
296 * chaining mechanism.
298 void make_substructure(const char *name
) {
299 environment
*s
= new environment
;
301 environment_set
.insert(s
);
304 static int is_env(const char *name
) {
306 sscanf(name
, "%p", &ptr_value
);
309 * Check for bad pointers.
312 if (!environment_set
.count((environment
*) ptr_value
)) {
319 const char *get_option_name(const char *name
) {
320 if (strncmp(name
, "0 ", strlen("0 ")))
323 name
+= strlen("0 ");
325 if (!isdigit(name
[0]))
328 while (isdigit(name
[0]))
331 if (!isspace(name
[0]))
334 while (isspace(name
[0]))
337 if (!isalnum(name
[0]))
343 int is_option(const char *name
) {
344 return (get_option_name(name
) != NULL
);
347 int is_arg(const char *name
, unsigned int arg
) {
348 assert (is_option(name
));
350 int length
= strlen(name
) + 3 * sizeof(unsigned int);
352 char *desired_string
= (char *) malloc(sizeof(char) * length
);
354 snprintf(desired_string
, length
, "%u %s", arg
, name
+ strlen("0 "));
356 int result
= environment_map
.count(desired_string
);
358 free(desired_string
);
363 void remove_arg(const char *name
, unsigned int arg
) {
364 assert (is_option(name
));
365 assert (is_arg(name
, arg
));
367 int length
= strlen(name
) + 3 * sizeof(unsigned int);
369 char *desired_string
= (char *) malloc(sizeof(char) * length
);
371 snprintf(desired_string
, length
, "%u %s", arg
, name
+ strlen("0 "));
373 environment_map
.erase(desired_string
);
376 const char *get_string_arg(const char *name
, unsigned int arg
) {
377 assert (is_option(name
));
379 int length
= strlen(name
) + 3 * sizeof(unsigned int);
381 char *desired_string
= (char *) malloc(sizeof(char) * length
);
383 snprintf(desired_string
, length
, "%u %s", arg
, name
+ strlen("0 "));
385 const char *result
= environment_map
[desired_string
];
389 free(desired_string
);
394 long int get_long_arg(const char *name
, unsigned int arg
) {
395 assert (is_option(name
));
397 const char *string
= get_string_arg(name
, arg
);
400 long int result
= strtol(string
, &endptr
, 0);
402 if (endptr
[0] != '\0') {
403 fprintf(stderr
, "\n\nError: bad argument in `%s'.\n\n", get_option_name(name
));
410 int get_int_arg(const char *name
, unsigned int arg
) {
411 return (int) get_long_arg(name
, arg
);
414 unsigned int get_unsigned_arg(const char *name
, unsigned int arg
) {
415 long int result
= get_long_arg(name
, arg
);
418 fprintf(stderr
, "\n\nError: bad argument in `%s'.\n\n", get_option_name(name
));
422 return (unsigned int) result
;
425 double get_double_arg(const char *name
, unsigned int arg
) {
426 assert (is_option(name
));
428 const char *string
= get_string_arg(name
, arg
);
431 double result
= strtod(string
, &endptr
);
433 if (endptr
[0] != '\0') {
434 fprintf(stderr
, "\n\nError: bad argument in `%s'.\n\n", get_option_name(name
));
441 static environment
*get_env(const char *name
) {
446 sscanf(name
, "%p", &ptr_value
);
449 * Check for bad pointers.
452 if (!environment_set
.count((environment
*) ptr_value
)) {
454 fprintf(stderr
, "Bad environment pointer.\n");
458 return (environment
*) ptr_value
;
464 void prepend(const char *list
, const char *element
) {
465 environment
*d
= get_env(get(list
));
466 make_substructure(list
);
467 get_env(get(list
))->set("a", element
);
468 get_env(get(list
))->set_ptr("d", d
);
471 void prepend_ptr(const char *list
, void *ptr
) {
472 prepend(list
, internal_convert_pointer(ptr
));
476 * Clone the environment.
478 environment
*clone() {
479 environment
*e
= new environment();
481 for (std::map
<const char *, const char *, compare_strings
>::iterator i
= environment_map
.begin();
482 i
!= environment_map
.end(); i
++) {
484 if (!name_ok(i
->first
))
487 if (is_env(i
->second
)) {
488 e
->set_ptr(i
->first
, get_env(i
->second
)->clone());
490 e
->set(i
->first
, i
->second
);
497 static environment
*top() {
498 if (environment_stack
.empty()) {
499 environment_stack
.push(new environment
);
500 environment_set
.insert(environment_stack
.top());
502 return environment_stack
.top();
506 environment
*e
= new environment
;
508 e
->environment_map
= environment_stack
.top()->environment_map
;
510 e
->internal_set_ptr("---chain", environment_stack
.top());
511 e
->internal_set_ptr("---this", e
);
512 e
->make_substructure("---dup");
514 environment_stack
.push(e
);
515 environment_set
.insert(e
);
518 static void dup_second() {
519 environment_stack
.top()->prepend_ptr("---dup",
520 environment::get_env(environment_stack
.top()->get("---chain")));
523 static void push_and_dup_output() {
529 assert(!environment_stack
.empty());
532 * Execution environments should never be referenced by
533 * structures further up the call chain, so they can
534 * safely be deleted. (XXX: In particular, while
535 * lexical scoping may require copying of execution
536 * environments from lower on the call chain, there is
537 * no obvious reason that a reference should be used in
538 * this case; a shallow copy should be used instead.)
541 environment_set
.erase(environment_stack
.top());
542 delete environment_stack
.top();
544 environment_stack
.pop();
548 * Set with duplication.
551 void set_with_dup(const char *name
, const char *value
) {
557 environment
*dup_item
= get_env(get("---dup"));
561 while (dup_item
->get("a")) {
562 get_env(dup_item
->get("a"))->set_with_dup(name
, value
);
563 assert(dup_item
->get("d"));
564 dup_item
= get_env(dup_item
->get("d"));
571 * Read tokens from a stream.
578 virtual const char *get() = 0;
581 * Peek at the next token.
584 virtual const char *peek() = 0;
587 * Divert the stream until the next occurrence of TOKEN.
589 virtual token_reader
*divert(const char *open_token
, const char *close_token
) = 0;
591 virtual int expects_exactly_one_option(void) {
595 virtual ~token_reader() {
599 class argument_parsing_token_reader
: public token_reader
{
601 const char *separators
;
603 argument_parsing_token_reader(const char *s
) {
608 int expects_exactly_one_option(void) {
612 virtual const char *get() {
613 int length
= strcspn(index
, separators
);
618 const char *result
= strndup(index
, length
);
621 if (strspn(index
, separators
) >= 1)
629 virtual const char *peek() {
630 int length
= strcspn(index
, separators
);
635 const char *result
= strndup(index
, length
);
640 virtual token_reader
*divert(const char *open_token
, const char *close_token
) {
646 class cstring_token_reader
: public token_reader
{
647 const char *separators
;
651 cstring_token_reader(const char *s
, int ephemeral
) {
652 assert(ephemeral
== 1);
654 separators
= "\n \t";
660 cstring_token_reader(const char *s
) {
661 separators
= "\n \t";
668 string
+= strspn(string
, separators
);
670 size_t length_to_next
= strcspn(string
, separators
);
672 if (length_to_next
== 0)
675 const char *result
= strndup(string
, length_to_next
);
677 string
+= length_to_next
;
683 string
+= strspn(string
, separators
);
685 size_t length_to_next
= strcspn(string
, separators
);
687 if (length_to_next
== 0)
690 return strndup(string
, length_to_next
);
693 cstring_token_reader
*divert(const char *open_token
, const char *close_token
) {
695 * This function might be broken.
701 int next
= strcspn(string
, separators
);
704 while (*(string
+ search
) != '\0' &&
705 (depth
|| strcmp(close_token
, (string
+ search
)))) {
706 if (!strcmp(close_token
, (string
+ search
)))
708 if (!strcmp(open_token
, (string
+ search
)))
711 next
= strcspn((string
+ next
), separators
);
714 if (*(string
+ search
) == '\0') {
715 fprintf(stderr
, "Parse error: End of scope not found.");
719 cstring_token_reader
*result
= new cstring_token_reader(strndup(string
, search
), 1);
724 * Eat the closing token.
732 ~cstring_token_reader() {
734 free((void *) string
);
738 class cli_token_reader
: public token_reader
{
745 cli_token_reader(int c
, const char *v
[]) {
753 if (arg_index
< argc
)
754 return argv
[arg_index
++];
762 if (arg_index
< argc
)
763 return argv
[arg_index
];
769 cli_token_reader
*divert(const char *open_token
, const char *close_token
) {
773 while (arg_index
+ search
< argc
774 && (depth
|| strcmp(argv
[arg_index
+ search
], close_token
))) {
775 if (!strcmp(close_token
, argv
[arg_index
+ search
]))
777 if (!strcmp(open_token
, argv
[arg_index
+ search
]))
782 if (arg_index
+ search
== argc
) {
783 fprintf(stderr
, "Parse error: end of scope not found.\n");
787 cli_token_reader
*result
= new cli_token_reader(search
, argv
+ arg_index
);
792 * Eat the closing token.
802 struct simple_option
{
804 const char *map_name
;
805 const char *map_value
;
810 static const char *supported_nonglobal_option_table
[];
811 static const char *focus_prefixes
[];
812 static simple_option simple_option_table
[];
814 static int option_name_match(const char *unadorned
, const char *token
, int require_ornamentation
= 1) {
817 if (!strcmp(unadorned
, token
) && !require_ornamentation
)
820 while (token
[0] == '-' && strip_max
) {
823 if (!strcmp(unadorned
, token
))
830 static int is_scope_operator(const char *string
) {
831 if (!strcmp("{", string
)
832 || !strcmp("}", string
)
833 || !strcmp("[", string
)
834 || !strcmp("]", string
)
835 || !strcmp("<", string
)
836 || !strcmp(">", string
))
842 static const char *option_name_gen(const char *unadorned
, const char *map_name
, int arg_num
, int multi
) {
843 static unsigned int multi_counter
= 0;
846 unadorned
= map_name
;
849 int length
= (strlen(unadorned
) + sizeof(unsigned int) * 3 + sizeof(int) * 3 + 2) + 1;
851 char *result
= (char *) malloc(sizeof(char) * length
);
856 snprintf(result
, length
, "%u 0 %s", arg_num
, unadorned
);
860 * XXX: This assumes that generating calls for
861 * options other than 0 exist in the same
862 * multiplicity group as the most recently
863 * generated 0-option multiplicity.
869 snprintf(result
, length
, "%u %u %s", arg_num
, multi_counter
, unadorned
);
875 static environment
*genv
;
877 static const char *get_next(token_reader
*tr
, const char *option_name
) {
878 const char *argument
= tr
->get();
880 if (argument
== NULL
) {
881 fprintf(stderr
, "\n\nError: not enough arguments for `%s'.\n\n", option_name
);
888 static int table_contains(const char **haystack
, const char *needle
, int prefix_length
= 0) {
893 while (*haystack
!= NULL
) {
894 if (prefix_length
== 0 && !strcmp(*haystack
, needle
))
896 if (prefix_length
> 0 && !strncmp(*haystack
, needle
, prefix_length
))
904 static int option_is_identical(environment
*a
, environment
*b
, const char *option_name
) {
905 if (!a
->is_option(option_name
) || !b
->is_option(option_name
))
908 int option_number
= 0;
910 while (a
->is_arg(option_name
, option_number
) || b
->is_arg(option_name
, option_number
)) {
911 if (!a
->is_arg(option_name
, option_number
)
912 || !b
->is_arg(option_name
, option_number
))
915 const char *a_str
= a
->get_string_arg(option_name
, option_number
);
916 const char *b_str
= b
->get_string_arg(option_name
, option_number
);
918 if (strcmp(a_str
, b_str
))
927 static void remove_option(environment
*a
, const char *option_name
) {
928 assert(a
->is_option(option_name
));
930 int option_number
= 0;
932 while (a
->is_arg(option_name
, option_number
)) {
933 a
->remove_arg(option_name
, option_number
);
938 static void remove_nonglobals(environment
*a
) {
941 std::stack
<const char *> removal_stack
;
943 for (std::map
<const char *, const char *, compare_strings
>::iterator i
= a
->get_map().begin();
944 i
!= a
->get_map().end(); i
++) {
946 if (!a
->is_option(i
->first
))
949 if (!table_contains(supported_nonglobal_option_table
, a
->get_option_name(i
->first
)))
952 removal_stack
.push(i
->first
);
955 while (!removal_stack
.empty()) {
956 remove_option(a
, removal_stack
.top());
961 static void option_intersect(environment
*a
, environment
*b
) {
965 std::stack
<const char *> removal_stack
;
967 for (std::map
<const char *, const char *, compare_strings
>::iterator i
= a
->get_map().begin();
968 i
!= a
->get_map().end(); i
++) {
970 if (!a
->is_option(i
->first
))
973 if (option_is_identical(a
, b
, i
->first
))
976 removal_stack
.push(i
->first
);
979 while (!removal_stack
.empty()) {
980 remove_option(a
, removal_stack
.top());
985 static void option_difference(environment
*a
, environment
*b
) {
989 std::stack
<const char *> removal_stack
;
991 for (std::map
<const char *, const char *, compare_strings
>::iterator i
= a
->get_map().begin();
992 i
!= a
->get_map().end(); i
++) {
994 if (!a
->is_option(i
->first
))
997 if (!option_is_identical(a
, b
, i
->first
))
1000 removal_stack
.push(i
->first
);
1003 while (!removal_stack
.empty()) {
1004 remove_option(a
, removal_stack
.top());
1005 removal_stack
.pop();
1009 static void evaluate_stream(token_reader
*tr
,
1010 std::vector
<std::pair
<const char *, environment
*> > *files
) {
1012 int end_of_options
= 0;
1014 while ((token
= tr
->get())) {
1020 if (!strcmp(token
, "{") && !end_of_options
) {
1021 environment::push_and_dup_output();
1022 token_reader
*tr_nest
= tr
->divert("{", "}");
1023 evaluate_stream(tr_nest
, files
);
1026 } else if (!strcmp(token
, "[") && !end_of_options
) {
1028 environment::push();
1029 token_reader
*tr_nest
= tr
->divert("[", "]");
1030 evaluate_stream(tr_nest
, files
);
1033 } else if (!strcmp(token
, "<") && !end_of_options
) {
1034 environment
*dup_list
= environment::get_env(environment::top()->get("---dup"));
1035 assert (dup_list
!= NULL
);
1036 dup_list
= dup_list
->clone();
1038 environment::dup_second();
1039 token_reader
*tr_nest
= tr
->divert("<", ">");
1040 evaluate_stream(tr_nest
, files
);
1043 environment::top()->set_ptr("---dup", dup_list
);
1047 * Check for non-whitespace argument separators
1050 else if (!end_of_options
&& token
&& token
[0] == '-' && strchr(token
, '=')) {
1051 environment::push_and_dup_output();
1052 token_reader
*tr_nest
= new argument_parsing_token_reader(token
);
1053 evaluate_stream(tr_nest
, files
);
1059 * Trap the end-of-option indicator.
1062 else if (!strcmp(token
, "--")) {
1068 * Check for options and filenames
1076 if (strncmp("-", token
, strlen("-")) || end_of_options
) {
1081 files
->push_back(std::pair
<const char *, environment
*>(strdup(token
),
1082 environment::top()->clone()));
1084 if (tr
->expects_exactly_one_option() && tr
->get()) {
1085 fprintf(stderr
, "\n\nError: Too many arguments for `%s'.\n\n", token
);
1093 * Handle focus option.
1096 if (option_name_match("focus", token
)) {
1098 environment
*target
;
1099 target
= environment::top();
1101 target
->set_with_dup(option_name_gen("focus", NULL
, 0, 0), "1");
1103 const char *option
= get_next(tr
, "focus");
1105 target
->set_with_dup(option_name_gen("focus", NULL
, 1, 0), option
);
1107 if (!strcmp(option
, "d")) {
1108 target
->set_with_dup(option_name_gen("focus", NULL
, 2, 0),
1109 get_next(tr
, "focus"));
1110 } else if (!strcmp(option
, "p")) {
1111 target
->set_with_dup(option_name_gen("focus", NULL
, 2, 0),
1112 get_next(tr
, "focus"));
1113 target
->set_with_dup(option_name_gen("focus", NULL
, 3, 0),
1114 get_next(tr
, "focus"));
1120 while (table_contains(focus_prefixes
, tr
->peek(), 3)) {
1121 target
->set_with_dup(option_name_gen("focus", NULL
, 4 + arg
, 0),
1122 get_next(tr
, "focus"));
1130 * Handle simple options.
1133 int found_option
= 0;
1134 for (int i
= 0; simple_option_table
[i
].name
; i
++) {
1135 if (!option_name_match(simple_option_table
[i
].name
, token
))
1139 * Handle the match case.
1145 * Determine which environment should be modified
1148 environment
*target
;
1149 target
= environment::top();
1152 * Store information required for
1153 * handling the local case later.
1156 const char *map_value
= "1";
1158 if (simple_option_table
[i
].map_value
) {
1159 map_value
= simple_option_table
[i
].map_value
;
1160 } else if (simple_option_table
[i
].map_name
) {
1161 map_value
= simple_option_table
[i
].name
;
1164 target
->set_with_dup(option_name_gen(simple_option_table
[i
].name
,
1165 simple_option_table
[i
].map_name
,
1167 simple_option_table
[i
].multi
),
1170 for (int j
= 0; j
< simple_option_table
[i
].arg_count
; j
++) {
1171 const char *option
= tr
->get();
1173 if (option
== NULL
) {
1174 fprintf(stderr
, "\n\nError: not enough options for `%s'.\n\n", token
);
1179 * Reject scope operators as options,
1183 if (is_scope_operator(option
)) {
1184 fprintf(stderr
, "\n\nError: illegal argument to `%s'.\n\n", token
);
1188 target
->set_with_dup(option_name_gen(simple_option_table
[i
].name
,
1189 simple_option_table
[i
].map_name
,
1191 simple_option_table
[i
].multi
),
1197 * Trap illegal options.
1201 ui::get()->illegal_option(token
);
1204 if (tr
->expects_exactly_one_option() && tr
->get()) {
1205 fprintf(stderr
, "\n\nError: Too many arguments for `%s'.\n\n", token
);
1215 * Does one of two things:
1217 * (1) Output version information if called with '--version'
1219 * (2) Read options and file arguments, and if the arguments are correct,
1220 * write output. If an error is detected, print the usage statement.
1224 static void handle(int argc
, const char *argv
[], const char *package
, const char *short_version
, const char *version
) {
1227 * Initialize help object
1230 help
hi(package
, argv
[0], short_version
);
1233 * Output version information if --version appears
1234 * on the command line.
1237 if (arg_count(argc
, argv
, "--version")) {
1239 * Output the version
1242 fprintf(stdout
, "%s", version
);
1248 * Handle help options
1251 if (arg_prefix_count(argc
, argv
, "--h"))
1252 for (int i
= 1; i
< argc
; i
++) {
1253 int all
= !strcmp(argv
[i
], "--hA");
1254 int is_help_option
= !strncmp(argv
[i
], "--h", strlen("--h"));
1257 if (!strcmp(argv
[i
], "--hu") || all
)
1258 hi
.usage(), found_help
= 1;
1259 if (!strcmp(argv
[i
], "--hq") || all
)
1260 hi
.defaults(), found_help
= 1;
1261 if (!strcmp(argv
[i
], "--hf") || all
)
1262 hi
.file(), found_help
= 1;
1263 if (!strcmp(argv
[i
], "--he") || all
)
1264 hi
.exclusion(), found_help
= 1;
1265 if (!strcmp(argv
[i
], "--ha") || all
)
1266 hi
.alignment(), found_help
= 1;
1267 if (!strcmp(argv
[i
], "--hr") || all
)
1268 hi
.rendering(), found_help
= 1;
1269 if (!strcmp(argv
[i
], "--hx") || all
)
1270 hi
.exposure(), found_help
= 1;
1271 if (!strcmp(argv
[i
], "--ht") || all
)
1272 hi
.tdf(), found_help
= 1;
1273 if (!strcmp(argv
[i
], "--hl") || all
)
1274 hi
.filtering(), found_help
= 1;
1275 if (!strcmp(argv
[i
], "--hd") || all
)
1276 hi
.device(), found_help
= 1;
1277 if (!strcmp(argv
[i
], "--hi") || all
)
1278 hi
.interface(), found_help
= 1;
1279 if (!strcmp(argv
[i
], "--hv") || all
)
1280 hi
.visp(), found_help
= 1;
1281 if (!strcmp(argv
[i
], "--hc") || all
)
1282 hi
.cp(), found_help
= 1;
1283 if (!strcmp(argv
[i
], "--h3") || all
)
1284 hi
.d3(), found_help
= 1;
1285 if (!strcmp(argv
[i
], "--hs") || all
)
1286 hi
.scope(), found_help
= 1;
1287 if (!strcmp(argv
[i
], "--hp") || all
)
1288 hi
.process(), found_help
= 1;
1289 if (!strcmp(argv
[i
], "--hz") || all
)
1290 hi
.undocumented(), found_help
= 1;
1292 if (is_help_option
&& !found_help
)
1296 * Check for the end-of-options marker, a non-option argument,
1297 * or the end of arguments. In all of these cases, we exit.
1300 if (!strcmp(argv
[i
], "--")
1301 || strncmp(argv
[i
], "--", strlen("--"))
1307 * Undocumented projective transformation utility
1310 if (arg_count(argc
, argv
, "--ptcalc") > 0) {
1311 fprintf(stderr
, "\n\n*** Warning: this feature is not documented ***\n\n");
1312 printf("Enter: w h tlx tly blx bly brx bry trx try x y\n\n");
1314 double w
, h
, tlx
, tly
, blx
, bly
, brx
, bry
, trx
, tr_y
, x
, y
;
1318 if (scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
1319 &w
, &h
, &tlx
, &tly
, &blx
, &bly
, &brx
, &bry
, &trx
, &tr_y
, &x
, &y
) != 12) {
1321 fprintf(stderr
, "Error reading input.\n");
1325 d2::image
*i
= new d2::image_ale_real((int)h
, (int)w
, 3);
1326 d2::transformation t
= d2::transformation::gpt_identity(i
, 1);
1328 d2::point(tly
, tlx
),
1329 d2::point(bly
, blx
),
1330 d2::point(bry
, brx
),
1331 d2::point(tr_y
, trx
)
1335 d2::point
a(y
, x
), b
;
1337 b
= t
.transform_scaled(a
);
1339 printf("TRANSFORM t(a): (%f, %f)\n", (double) b
[1], (double) b
[0]);
1341 b
= t
.scaled_inverse_transform(a
);
1343 printf("INVERSE t^-1(a): (%f, %f)\n", (double) b
[1], (double) b
[0]);
1349 * Thread initialization.
1355 * Flags and variables
1358 double scale_factor
= 1;
1359 double vise_scale_factor
= 1;
1361 double usm_multiplier
= 0.0;
1364 struct d2::tload_t
*tload
= NULL
;
1365 struct d2::tsave_t
*tsave
= NULL
;
1366 struct d3::tload_t
*d3_tload
= NULL
;
1367 struct d3::tsave_t
*d3_tsave
= NULL
;
1368 int ip_iterations
= 0;
1369 int ip_use_median
= 0;
1371 enum { psf_linear
, psf_nonlinear
, psf_N
};
1372 const char *psf
[psf_N
] = {NULL
, NULL
};
1373 const char *device
= NULL
;
1375 double psf_match_args
[6];
1377 int exposure_register
= 1;
1378 const char *wm_filename
= NULL
;
1379 int wm_offsetx
= 0, wm_offsety
= 0;
1380 double cx_parameter
= 1;
1381 double *d3px_parameters
= NULL
;
1383 d2::exclusion
*ex_parameters
= NULL
;
1387 const char *achain_type
= "triangle:2";
1388 const char *afilter_type
= "internal";
1389 d2::render
**ochain
= NULL
;
1390 const char **ochain_names
= NULL
;
1391 const char **ochain_types
= NULL
;
1392 const char *d3chain_type
= NULL
;
1394 const char **visp
= NULL
;
1396 const char **d3_output
= NULL
;
1397 const char **d3_depth
= NULL
;
1398 unsigned int d3_count
= 0;
1399 double user_view_angle
= 0;
1400 int user_bayer
= IMAGE_BAYER_DEFAULT
;
1401 d2::pixel exp_mult
= d2::pixel(1, 1, 1);
1402 std::map
<const char *, d3::pt
> d3_output_pt
;
1403 std::map
<const char *, d3::pt
> d3_depth_pt
;
1406 * dchain is ochain[0].
1409 ochain
= (d2::render
**) local_realloc(ochain
,
1410 (oc_count
+ 1) * sizeof(d2::render
*));
1411 ochain_names
= (const char **) local_realloc((void *)ochain_names
,
1412 (oc_count
+ 1) * sizeof(const char *));
1413 ochain_types
= (const char **) local_realloc((void *)ochain_types
,
1414 (oc_count
+ 1) * sizeof(const char *));
1416 ochain_types
[0] = "sinc*lanc:8";
1421 * Handle default settings
1424 if (arg_prefix_count(argc
, argv
, "--q") > 0)
1425 ui::get()->error("Default settings --q* are no longer recognized.");
1427 const char *defaults
=
1428 "--dchain auto:triangle:2,fine:triangle:2,triangle:2 "
1429 "--achain triangle:2 "
1431 "--3d-chain fine:triangle:2,fine:gauss:0.75,triangle:2 ";
1433 token_reader
*default_reader
= new cstring_token_reader(defaults
);
1435 evaluate_stream(default_reader
, NULL
);
1438 * Set basic program information in the environment.
1441 environment::top()->set_with_dup("---package", package
);
1442 environment::top()->set_with_dup("---short-version", short_version
);
1443 environment::top()->set_with_dup("---version", version
);
1444 environment::top()->set_with_dup("---invocation", argv
[0]);
1447 * Initialize the top-level token-reader and generate
1448 * an environment variable for it.
1451 token_reader
*tr
= new cli_token_reader(argc
- 1, argv
+ 1);
1452 environment::top()->set_ptr("---token-reader", tr
);
1455 * Evaluate the command-line arguments to generate environment
1459 std::vector
<std::pair
<const char *, environment
*> > files
;
1461 evaluate_stream(tr
, &files
);
1464 * If there are fewer than two files, then output usage information.
1467 if (files
.size() < 2) {
1473 * Extract the global environment and check non-globals
1474 * against a list of supported non-global options.
1477 genv
= files
[0].second
->clone();
1479 remove_nonglobals(genv
);
1481 for (unsigned int i
= 0; i
< files
.size(); i
++) {
1482 option_intersect(genv
, files
[i
].second
);
1485 for (unsigned int i
= 0; i
< files
.size(); i
++) {
1486 option_difference(files
[i
].second
, genv
);
1488 for (std::map
<const char *, const char *>::iterator j
= files
[i
].second
->get_map().begin();
1489 j
!= files
[i
].second
->get_map().end(); j
++) {
1491 environment
*env
= files
[i
].second
;
1493 if (!env
->is_option(j
->first
))
1496 const char *option_name
= env
->get_option_name(j
->first
);
1498 if (!table_contains(supported_nonglobal_option_table
, option_name
)) {
1499 fprintf(stderr
, "\n\nError: option `%s' must be applied globally.", option_name
);
1500 fprintf(stderr
, "\n\nHint: Move option `%s' prior to file and scope operators.\n\n",
1508 * Iterate through the global environment,
1509 * looking for options.
1512 for (std::map
<const char *, const char *>::iterator i
= genv
->get_map().begin();
1513 i
!= genv
->get_map().end(); i
++) {
1515 environment
*env
= genv
;
1517 if (!env
->is_option(i
->first
))
1520 const char *option_name
= env
->get_option_name(i
->first
);
1522 if (!strcmp(option_name
, "default")) {
1524 * Do nothing. Defaults have already been set.
1526 } else if (!strcmp(option_name
, "bpc")) {
1527 if (!strcmp(env
->get_string_arg(i
->first
, 0), "8bpc"))
1528 d2::image_rw::depth8();
1529 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "16bpc"))
1530 d2::image_rw::depth16();
1533 } else if (!strcmp(option_name
, "format")) {
1534 if (!strcmp(env
->get_string_arg(i
->first
, 0), "plain"))
1535 d2::image_rw::ppm_plain();
1536 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "raw"))
1537 d2::image_rw::ppm_raw();
1538 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "auto"))
1539 d2::image_rw::ppm_auto();
1542 } else if (!strcmp(option_name
, "align")) {
1543 if (!strcmp(env
->get_string_arg(i
->first
, 0), "align-all"))
1545 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "align-green"))
1547 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "align-sum"))
1551 } else if (!strcmp(option_name
, "transformation")) {
1552 if (!strcmp(env
->get_string_arg(i
->first
, 0), "translation"))
1553 d2::align::class_translation();
1554 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "euclidean"))
1555 d2::align::class_euclidean();
1556 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "projective"))
1557 d2::align::class_projective();
1560 } else if (!strcmp(option_name
, "transformation-default")) {
1561 if (!strcmp(env
->get_string_arg(i
->first
, 0), "identity"))
1562 d2::align::initial_default_identity();
1563 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "follow"))
1564 d2::align::initial_default_follow();
1567 } else if (!strcmp(option_name
, "perturb")) {
1568 if (!strcmp(env
->get_string_arg(i
->first
, 0), "perturb-output"))
1569 d2::align::perturb_output();
1570 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "perturb-source"))
1571 d2::align::perturb_source();
1574 } else if (!strcmp(option_name
, "fail")) {
1575 if (!strcmp(env
->get_string_arg(i
->first
, 0), "fail-optimal"))
1576 d2::align::fail_optimal();
1577 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "fail-default"))
1578 d2::align::fail_default();
1581 } else if (!strcmp(option_name
, "profile")) {
1583 } else if (!strcmp(option_name
, "extend")) {
1584 if (env
->get_int_arg(i
->first
, 0))
1588 } else if (!strcmp(option_name
, "oc")) {
1589 if (env
->get_int_arg(i
->first
, 0))
1593 } else if (!strcmp(option_name
, "focus")) {
1597 double inf
= one
/ zero
;
1599 assert (isinf(inf
) && inf
> 0);
1605 unsigned int type
= 0;
1606 double distance
= 0;
1607 double px
= 0, py
= 0;
1609 if (!strcmp(env
->get_string_arg(i
->first
, 1), "d")) {
1613 distance
= env
->get_double_arg(i
->first
, 2);
1615 } else if (!strcmp(env
->get_string_arg(i
->first
, 1), "p")) {
1619 px
= env
->get_double_arg(i
->first
, 2);
1620 py
= env
->get_double_arg(i
->first
, 3);
1623 bad_arg(option_name
);
1630 unsigned int ci
= 0;
1641 unsigned int sc
= 3;
1642 unsigned int fs
= 0;
1643 unsigned int sr
= 0;
1645 for (int arg_num
= 4; env
->is_arg(i
->first
, arg_num
); arg_num
++) {
1646 const char *option
= env
->get_string_arg(i
->first
, arg_num
);
1647 if (!strncmp(option
, "ci=", 3)) {
1648 if(sscanf(option
+ 3, "%u", &ci
) != 1)
1650 } else if (!strncmp(option
, "fr=", 3)) {
1651 if(sscanf(option
+ 3, "%lf", &fr
) != 1)
1653 } else if (!strncmp(option
, "ht=", 3)) {
1654 if(sscanf(option
+ 3, "%lf", &ht
) != 1)
1656 } else if (!strncmp(option
, "vt=", 3)) {
1657 if(sscanf(option
+ 3, "%lf", &vt
) != 1)
1659 } else if (!strncmp(option
, "sy=", 3)) {
1660 if(sscanf(option
+ 3, "%lf", &sy
) != 1)
1662 } else if (!strncmp(option
, "ey=", 3)) {
1663 if(sscanf(option
+ 3, "%lf", &ey
) != 1)
1665 } else if (!strncmp(option
, "sx=", 3)) {
1666 if(sscanf(option
+ 3, "%lf", &sx
) != 1)
1668 } else if (!strncmp(option
, "ex=", 3)) {
1669 if(sscanf(option
+ 3, "%lf", &ex
) != 1)
1671 } else if (!strncmp(option
, "sd=", 3)) {
1672 if(sscanf(option
+ 3, "%lf", &sd
) != 1)
1674 } else if (!strncmp(option
, "ed=", 3)) {
1675 if(sscanf(option
+ 3, "%lf", &ed
) != 1)
1677 } else if (!strncmp(option
, "ap=", 3)) {
1678 if(sscanf(option
+ 3, "%lf", &ap
) != 1)
1680 } else if (!strncmp(option
, "sc=", 3)) {
1681 if(sscanf(option
+ 3, "%u", &sc
) != 1)
1683 } else if (!strncmp(option
, "sr=", 3)) {
1684 if (!strcmp(option
, "sr=aperture")) {
1686 } else if (!strcmp(option
, "sr=pixel")) {
1691 } else if (!strncmp(option
, "fs=", 3)) {
1692 if (!strcmp(option
, "fs=mean")) {
1694 } else if (!strcmp(option
, "fs=median")) {
1702 d3::focus::add_region(type
, distance
, px
, py
, ci
, fr
, ht
, vt
, sd
, ed
, sx
, ex
, sy
, ey
, ap
, sc
, fs
, sr
);
1704 } else if (!strcmp(option_name
, "3ddp") || !strcmp(option_name
, "3dvp")) {
1708 * Unsupported configurations
1712 unsupported::fornow("3D modeling with Irani-Peleg rendering");
1716 unsupported::fornow("3D modeling with unsharp mask");
1720 * Initialize if necessary
1722 * Note: because their existence is checked as an
1723 * indicator of the presence of 3D arguments, we
1724 * initialize these structures here.
1727 if (d3_output
== NULL
) {
1729 d3_output
= (const char **) calloc(d3_count
, sizeof(char *));
1730 d3_depth
= (const char **) calloc(d3_count
, sizeof(char *));
1733 unsigned int width
, height
;
1738 width
= env
->get_unsigned_arg(i
->first
, 1);
1739 height
= env
->get_unsigned_arg(i
->first
, 2);
1740 view_angle
= env
->get_double_arg(i
->first
, 3);
1741 x
= env
->get_double_arg(i
->first
, 4);
1742 y
= env
->get_double_arg(i
->first
, 5);
1743 z
= env
->get_double_arg(i
->first
, 6);
1744 P
= env
->get_double_arg(i
->first
, 7);
1745 Y
= env
->get_double_arg(i
->first
, 8);
1746 R
= env
->get_double_arg(i
->first
, 9);
1748 view_angle
*= M_PI
/ 180;
1753 d2::transformation t
=
1754 d2::transformation::eu_identity();
1755 t
.set_domain(height
, width
);
1756 d3::pt
_pt(t
, d3::et(y
, x
, z
, Y
, P
, R
), view_angle
);
1758 if (!strcmp(option_name
, "3dvp")) {
1759 d3_output_pt
[env
->get_string_arg(i
->first
, 10)] = _pt
;
1760 } else if (!strcmp(option_name
, "3ddp")) {
1761 d3_depth_pt
[env
->get_string_arg(i
->first
, 10)] = _pt
;
1765 } else if (!strcmp(option_name
, "3dv")) {
1768 unsigned int frame_no
;
1771 * Unsupported configurations
1775 unsupported::fornow("3D modeling with Irani-Peleg rendering");
1779 unsupported::fornow("3D modeling with unsharp mask");
1783 * Initialize if necessary
1786 if (d3_output
== NULL
) {
1788 d3_output
= (const char **) calloc(d3_count
, sizeof(char *));
1789 d3_depth
= (const char **) calloc(d3_count
, sizeof(char *));
1792 frame_no
= env
->get_int_arg(i
->first
, 1);
1794 if (frame_no
>= d3_count
)
1795 ui::get()->error("--3dv argument 0 is too large");
1797 if (d3_output
[frame_no
] != NULL
) {
1798 unsupported::fornow ("Writing a single 3D view to more than one output file");
1801 d3_output
[frame_no
] = env
->get_string_arg(i
->first
, 2);
1803 } else if (!strcmp(option_name
, "3dd")) {
1806 unsigned int frame_no
;
1809 * Unsupported configurations
1813 unsupported::fornow("3D modeling with Irani-Peleg rendering");
1817 unsupported::fornow("3D modeling with unsharp mask");
1821 * Initialize if necessary
1824 if (d3_output
== NULL
) {
1826 d3_output
= (const char **) calloc(d3_count
, sizeof(char *));
1827 d3_depth
= (const char **) calloc(d3_count
, sizeof(char *));
1830 frame_no
= env
->get_int_arg(i
->first
, 1);
1832 if (frame_no
>= d3_count
)
1833 ui::get()->error("--3dd argument 0 is too large");
1835 if (d3_depth
[frame_no
] != NULL
) {
1836 unsupported::fornow ("Writing a single frame's depth info to more than one output file");
1839 d3_depth
[frame_no
] = env
->get_string_arg(i
->first
, 2);
1841 } else if (!strcmp(option_name
, "view-angle")) {
1842 user_view_angle
= env
->get_double_arg(i
->first
, 1) * M_PI
/ 180;
1843 } else if (!strcmp(option_name
, "cpf-load")) {
1844 d3::cpf::init_loadfile(env
->get_string_arg(i
->first
, 1));
1845 } else if (!strcmp(option_name
, "ui")) {
1846 if (!strcmp(env
->get_string_arg(i
->first
, 1), "stream"))
1848 else if (!strcmp(env
->get_string_arg(i
->first
, 1), "tty"))
1850 else if (!strcmp(env
->get_string_arg(i
->first
, 1), "log"))
1852 else if (!strcmp(env
->get_string_arg(i
->first
, 1), "quiet"))
1855 fprintf(stderr
, "Error: Unknown user interface type '%s'\n",
1856 env
->get_string_arg(i
->first
, 1));
1859 } else if (!strcmp(option_name
, "3d-fmr")) {
1860 d3::scene::fmr(env
->get_double_arg(i
->first
, 1));
1861 } else if (!strcmp(option_name
, "3d-dmr")) {
1862 d3::scene::dmr(env
->get_double_arg(i
->first
, 1));
1863 } else if (!strcmp(option_name
, "et")) {
1864 d3::scene::et(env
->get_double_arg(i
->first
, 1));
1865 } else if (!strcmp(option_name
, "st")) {
1866 d3::cpf::st(env
->get_double_arg(i
->first
, 1));
1867 } else if (!strcmp(option_name
, "di-lower")) {
1868 d3::scene::di_lower(env
->get_double_arg(i
->first
, 1));
1869 } else if (!strcmp(option_name
, "rc")) {
1870 d3::scene::rc(env
->get_double_arg(i
->first
, 1));
1871 } else if (!strcmp(option_name
, "do-try")) {
1872 d3::scene::do_try(env
->get_double_arg(i
->first
, 1));
1873 } else if (!strcmp(option_name
, "di-upper")) {
1874 d3::scene::di_upper(env
->get_double_arg(i
->first
, 1));
1875 } else if (!strcmp(option_name
, "fc")) {
1876 d3::scene::fc(env
->get_double_arg(i
->first
, 1));
1877 } else if (!strcmp(option_name
, "ecm")) {
1878 unsupported::discontinued("--ecm <x>");
1879 } else if (!strcmp(option_name
, "acm")) {
1880 unsupported::discontinued("--acm <x>");
1881 } else if (!strcmp(option_name
, "def-nn")) {
1882 d2::image_rw::def_nn(env
->get_double_arg(i
->first
, 1));
1884 if (env
->get_double_arg(i
->first
, 1) > 2) {
1885 fprintf(stderr
, "\n\n*** Warning: --def-nn implementation is currently "
1886 "inefficient for large radii. ***\n\n");
1889 } else if (!strcmp(option_name
, "fx")) {
1890 d3::scene::fx(env
->get_double_arg(i
->first
, 1));
1891 } else if (!strcmp(option_name
, "tcem")) {
1892 d3::scene::tcem(env
->get_double_arg(i
->first
, 1));
1893 } else if (!strcmp(option_name
, "oui")) {
1894 d3::scene::oui(env
->get_unsigned_arg(i
->first
, 1));
1895 } else if (!strcmp(option_name
, "pa")) {
1896 d3::scene::pa(env
->get_unsigned_arg(i
->first
, 1));
1897 } else if (!strcmp(option_name
, "pc")) {
1898 d3::scene::pc(env
->get_string_arg(i
->first
, 1));
1899 } else if (!strcmp(option_name
, "cw")) {
1900 d2::align::certainty_weighted(env
->get_unsigned_arg(i
->first
, 0));
1901 } else if (!strcmp(option_name
, "wm")) {
1902 if (wm_filename
!= NULL
)
1903 ui::get()->error("only one weight map can be specified");
1905 wm_filename
= env
->get_string_arg(i
->first
, 1);
1906 wm_offsetx
= env
->get_int_arg(i
->first
, 2);
1907 wm_offsety
= env
->get_int_arg(i
->first
, 3);
1909 } else if (!strcmp(option_name
, "fl")) {
1911 d2::align::set_frequency_cut(env
->get_double_arg(i
->first
, 1),
1912 env
->get_double_arg(i
->first
, 2),
1913 env
->get_double_arg(i
->first
, 3));
1916 ui::get()->error_hint("--fl is not supported", "rebuild ALE with FFTW support");
1918 } else if (!strcmp(option_name
, "wmx")) {
1920 d2::align::set_wmx(env
->get_string_arg(i
->first
, 1),
1921 env
->get_string_arg(i
->first
, 2),
1922 env
->get_string_arg(i
->first
, 3));
1924 ui::get()->error_hint("--wmx is not supported", "rebuild ALE with support for --wmx");
1926 } else if (!strcmp(option_name
, "flshow")) {
1927 d2::align::set_fl_show(env
->get_string_arg(i
->first
, 1));
1928 } else if (!strcmp(option_name
, "3dpx")) {
1930 d3px_parameters
= (double *) local_realloc(d3px_parameters
, (d3px_count
+ 1) * 6 * sizeof(double));
1932 for (int param
= 0; param
< 6; param
++)
1933 d3px_parameters
[6 * d3px_count
+ param
] = env
->get_double_arg(i
->first
, param
+ 1);
1936 * Swap x and y, since their internal meanings differ from their external meanings.
1939 for (int param
= 0; param
< 2; param
++) {
1940 double temp
= d3px_parameters
[6 * d3px_count
+ 2 + param
];
1941 d3px_parameters
[6 * d3px_count
+ 2 + param
] = d3px_parameters
[6 * d3px_count
+ 0 + param
];
1942 d3px_parameters
[6 * d3px_count
+ 0 + param
] = temp
;
1947 * Increment counters
1952 } else if (!strcmp(option_name
, "ex") || !strcmp(option_name
, "fex")) {
1954 ex_parameters
= (d2::exclusion
*) local_realloc(ex_parameters
,
1955 (ex_count
+ 1) * sizeof(d2::exclusion
));
1957 ex_parameters
[ex_count
].type
= (!strcmp(option_name
, "ex"))
1958 ? d2::exclusion::RENDER
1959 : d2::exclusion::FRAME
;
1962 * Get parameters, swapping x and y coordinates
1965 ex_parameters
[ex_count
].x
[0] = env
->get_int_arg(i
->first
, 1 + 2);
1966 ex_parameters
[ex_count
].x
[1] = env
->get_int_arg(i
->first
, 1 + 3);
1967 ex_parameters
[ex_count
].x
[2] = env
->get_int_arg(i
->first
, 1 + 0);
1968 ex_parameters
[ex_count
].x
[3] = env
->get_int_arg(i
->first
, 1 + 1);
1969 ex_parameters
[ex_count
].x
[4] = env
->get_int_arg(i
->first
, 1 + 4);
1970 ex_parameters
[ex_count
].x
[5] = env
->get_int_arg(i
->first
, 1 + 5);
1973 * Increment counters
1978 } else if (!strcmp(option_name
, "crop") || !strcmp(option_name
, "fcrop")) {
1980 ex_parameters
= (d2::exclusion
*) local_realloc(ex_parameters
,
1981 (ex_count
+ 4) * sizeof(d2::exclusion
));
1983 for (int r
= 0; r
< 4; r
++)
1984 ex_parameters
[ex_count
+ r
].type
= (!strcmp(option_name
, "crop"))
1985 ? d2::exclusion::RENDER
1986 : d2::exclusion::FRAME
;
1991 for (int param
= 0; param
< 6; param
++)
1992 crop_args
[param
] = env
->get_int_arg(i
->first
, param
+ 1);
1995 * Construct exclusion regions from the crop area,
1996 * swapping x and y, since their internal meanings
1997 * differ from their external meanings.
2001 * Exclusion region 1: low x
2004 ex_parameters
[ex_count
+ 0].x
[0] = INT_MIN
;
2005 ex_parameters
[ex_count
+ 0].x
[1] = crop_args
[2] - 1;
2006 ex_parameters
[ex_count
+ 0].x
[2] = INT_MIN
;
2007 ex_parameters
[ex_count
+ 0].x
[3] = INT_MAX
;
2008 ex_parameters
[ex_count
+ 0].x
[4] = crop_args
[4];
2009 ex_parameters
[ex_count
+ 0].x
[5] = crop_args
[5];
2012 * Exclusion region 2: low y
2015 ex_parameters
[ex_count
+ 1].x
[0] = INT_MIN
;
2016 ex_parameters
[ex_count
+ 1].x
[1] = INT_MAX
;
2017 ex_parameters
[ex_count
+ 1].x
[2] = INT_MIN
;
2018 ex_parameters
[ex_count
+ 1].x
[3] = crop_args
[0] - 1;
2019 ex_parameters
[ex_count
+ 1].x
[4] = crop_args
[4];
2020 ex_parameters
[ex_count
+ 1].x
[5] = crop_args
[5];
2023 * Exclusion region 3: high y
2026 ex_parameters
[ex_count
+ 2].x
[0] = INT_MIN
;
2027 ex_parameters
[ex_count
+ 2].x
[1] = INT_MAX
;
2028 ex_parameters
[ex_count
+ 2].x
[2] = crop_args
[1] + 1;
2029 ex_parameters
[ex_count
+ 2].x
[3] = INT_MAX
;
2030 ex_parameters
[ex_count
+ 2].x
[4] = crop_args
[4];
2031 ex_parameters
[ex_count
+ 2].x
[5] = crop_args
[5];
2034 * Exclusion region 4: high x
2037 ex_parameters
[ex_count
+ 3].x
[0] = crop_args
[3] + 1;
2038 ex_parameters
[ex_count
+ 3].x
[1] = INT_MAX
;
2039 ex_parameters
[ex_count
+ 3].x
[2] = INT_MIN
;
2040 ex_parameters
[ex_count
+ 3].x
[3] = INT_MAX
;
2041 ex_parameters
[ex_count
+ 3].x
[4] = crop_args
[4];
2042 ex_parameters
[ex_count
+ 3].x
[5] = crop_args
[5];
2045 * Increment counters
2050 } else if (!strcmp(option_name
, "exshow")) {
2052 } else if (!strcmp(option_name
, "wt")) {
2053 d2::render::set_wt(env
->get_double_arg(i
->first
, 1));
2054 } else if (!strcmp(option_name
, "3d-chain")) {
2055 d3chain_type
= env
->get_string_arg(i
->first
, 1);
2056 } else if (!strcmp(option_name
, "dchain")) {
2057 ochain_types
[0] = env
->get_string_arg(i
->first
, 1);
2058 } else if (!strcmp(option_name
, "achain")) {
2059 achain_type
= env
->get_string_arg(i
->first
, 1);
2060 } else if (!strcmp(option_name
, "afilter")) {
2061 afilter_type
= env
->get_string_arg(i
->first
, 1);
2062 } else if (!strcmp(option_name
, "ochain")) {
2064 ochain
= (d2::render
**) local_realloc(ochain
,
2065 (oc_count
+ 1) * sizeof(d2::render
*));
2066 ochain_names
= (const char **) local_realloc((void *)ochain_names
,
2067 (oc_count
+ 1) * sizeof(const char *));
2068 ochain_types
= (const char **) local_realloc((void *)ochain_types
,
2069 (oc_count
+ 1) * sizeof(const char *));
2071 ochain_types
[oc_count
] = env
->get_string_arg(i
->first
, 1);
2072 ochain_names
[oc_count
] = env
->get_string_arg(i
->first
, 2);
2076 } else if (!strcmp(option_name
, "visp")) {
2078 visp
= (const char **) local_realloc((void *)visp
, 4 *
2079 (vise_count
+ 1) * sizeof(const char *));
2081 for (int param
= 0; param
< 4; param
++)
2082 visp
[vise_count
* 4 + param
] = env
->get_string_arg(i
->first
, param
+ 1);
2086 } else if (!strcmp(option_name
, "cx")) {
2087 cx_parameter
= env
->get_int_arg(i
->first
, 0) ? env
->get_double_arg(i
->first
, 1) : 0;
2088 } else if (!strcmp(option_name
, "ip")) {
2089 unsupported::discontinued("--ip <r> <i>", "--lpsf box=<r> --ips <i>");
2090 } else if (!strcmp(option_name
, "cache")) {
2091 double cache
= env
->get_double_arg(i
->first
, 1);
2093 d2::image_rw::set_cache(cache
);
2095 } else if (!strcmp(option_name
, "bayer")) {
2098 * External order is clockwise from top-left. Internal
2099 * order is counter-clockwise from top-left.
2102 const char *option
= env
->get_string_arg(i
->first
, 1);
2104 if (!strcmp(option
, "rgbg")) {
2105 user_bayer
= IMAGE_BAYER_RGBG
;
2106 } else if (!strcmp(option
, "bgrg")) {
2107 user_bayer
= IMAGE_BAYER_BGRG
;
2108 } else if (!strcmp(option
, "gbgr")) {
2109 user_bayer
= IMAGE_BAYER_GRGB
;
2110 } else if (!strcmp(option
, "grgb")) {
2111 user_bayer
= IMAGE_BAYER_GBGR
;
2112 } else if (!strcmp(option
, "none")) {
2113 user_bayer
= IMAGE_BAYER_NONE
;
2118 } else if (!strcmp(option_name
, "lpsf")) {
2119 psf
[psf_linear
] = env
->get_string_arg(i
->first
, 1);
2120 } else if (!strcmp(option_name
, "nlpsf")) {
2121 psf
[psf_nonlinear
] = env
->get_string_arg(i
->first
, 1);
2122 } else if (!strcmp(option_name
, "psf-match")) {
2126 for (int index
= 0; index
< 6; index
++) {
2127 psf_match_args
[index
] = env
->get_double_arg(i
->first
, index
+ 1);
2130 } else if (!strcmp(option_name
, "device")) {
2131 device
= env
->get_string_arg(i
->first
, 1);
2133 } else if (!strcmp(option_name
, "usm")) {
2135 if (d3_output
!= NULL
)
2136 unsupported::fornow("3D modeling with unsharp mask");
2138 usm_multiplier
= env
->get_double_arg(i
->first
, 1);
2141 } else if (!strcmp(option_name
, "ipr")) {
2143 ip_iterations
= env
->get_int_arg(i
->first
, 1);
2145 ui::get()->warn("--ipr is deprecated. Use --ips instead");
2147 } else if (!strcmp(option_name
, "cpp-err")) {
2148 if (!strcmp(env
->get_string_arg(i
->first
, 0), "median"))
2149 d3::cpf::err_median();
2150 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "mean"))
2151 d3::cpf::err_mean();
2152 } else if (!strcmp(option_name
, "vp-adjust")) {
2153 if (env
->get_int_arg(i
->first
, 0))
2154 d3::align::vp_adjust();
2156 d3::align::vp_noadjust();
2157 } else if (!strcmp(option_name
, "vo-adjust")) {
2158 if (env
->get_int_arg(i
->first
, 0))
2159 d3::align::vo_adjust();
2161 d3::align::vo_noadjust();
2162 } else if (!strcmp(option_name
, "ip-statistic")) {
2163 if (!strcmp(env
->get_string_arg(i
->first
, 0), "mean"))
2165 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "median"))
2167 } else if (!strcmp(option_name
, "ips")) {
2168 ip_iterations
= env
->get_int_arg(i
->first
, 1);
2169 } else if (!strcmp(option_name
, "ip-wl")) {
2170 int limited
= env
->get_int_arg(i
->first
, 0);
2172 ipwl
= env
->get_double_arg(i
->first
, 1);
2176 } else if (!strcmp(option_name
, "ipc")) {
2177 unsupported::discontinued("--ipc <c> <i>", "--ips <i> --lpsf <c>", "--ips <i> --device <c>");
2178 } else if (!strcmp(option_name
, "exp-extend")) {
2179 if (env
->get_int_arg(i
->first
, 0))
2180 d2::image_rw::exp_scale();
2182 d2::image_rw::exp_noscale();
2183 } else if (!strcmp(option_name
, "exp-register")) {
2184 if (env
->get_int_arg(i
->first
, 0) == 1) {
2185 exposure_register
= 1;
2186 d2::align::exp_register();
2187 } else if (env
->get_int_arg(i
->first
, 0) == 0) {
2188 exposure_register
= 0;
2189 d2::align::exp_noregister();
2190 } else if (env
->get_int_arg(i
->first
, 0) == 2) {
2191 exposure_register
= 2;
2192 d2::align::exp_meta_only();
2194 } else if (!strcmp(option_name
, "drizzle-only")) {
2195 unsupported::discontinued("--drizzle-only", "--dchain box:1");
2196 } else if (!strcmp(option_name
, "subspace-traverse")) {
2197 unsupported::undocumented("--subspace-traverse");
2198 d3::scene::set_subspace_traverse();
2199 } else if (!strcmp(option_name
, "3d-filter")) {
2200 if (env
->get_int_arg(i
->first
, 0))
2201 d3::scene::filter();
2203 d3::scene::nofilter();
2204 } else if (!strcmp(option_name
, "occ-norm")) {
2205 if (env
->get_int_arg(i
->first
, 0))
2209 } else if (!strcmp(option_name
, "inc")) {
2210 inc
= env
->get_int_arg(i
->first
, 0);
2211 } else if (!strcmp(option_name
, "exp-mult")) {
2212 double exp_c
, exp_r
, exp_b
;
2214 exp_c
= env
->get_double_arg(i
->first
, 1);
2215 exp_r
= env
->get_double_arg(i
->first
, 2);
2216 exp_b
= env
->get_double_arg(i
->first
, 3);
2218 exp_mult
= d2::pixel(1/(exp_r
* exp_c
), 1/exp_c
, 1/(exp_b
* exp_c
));
2220 } else if (!strcmp(option_name
, "visp-scale")) {
2222 vise_scale_factor
= env
->get_double_arg(i
->first
, 1);
2224 if (vise_scale_factor
<= 0.0)
2225 ui::get()->error("VISP scale must be greater than zero");
2227 if (!finite(vise_scale_factor
))
2228 ui::get()->error("VISP scale must be finite");
2230 } else if (!strcmp(option_name
, "scale")) {
2232 scale_factor
= env
->get_double_arg(i
->first
, 1);
2234 if (scale_factor
<= 0)
2235 ui::get()->error("Scale factor must be greater than zero");
2237 if (!finite(scale_factor
))
2238 ui::get()->error("Scale factor must be finite");
2240 } else if (!strcmp(option_name
, "metric")) {
2241 d2::align::set_metric_exponent(env
->get_double_arg(i
->first
, 1));
2242 } else if (!strcmp(option_name
, "threshold")) {
2243 d2::align::set_match_threshold(env
->get_double_arg(i
->first
, 1));
2244 } else if (!strcmp(option_name
, "drizzle-diam")) {
2245 unsupported::discontinued("--drizzle-diam=<x>", "--dchain box:1");
2246 } else if (!strcmp(option_name
, "perturb-lower")) {
2247 const char *option
= env
->get_string_arg(i
->first
, 1);
2248 double perturb_lower
;
2250 sscanf(option
, "%lf%n", &perturb_lower
, &characters
);
2251 if (perturb_lower
<= 0)
2252 ui::get()->error("--perturb-lower= value is non-positive");
2254 if (*(option
+ characters
) == '%')
2255 d2::align::set_perturb_lower(perturb_lower
, 1);
2257 d2::align::set_perturb_lower(perturb_lower
, 0);
2258 } else if (!strcmp(option_name
, "stepsize")) {
2259 ui::get()->warn("--stepsize is deprecated. Use --perturb-lower instead");
2260 d2::align::set_perturb_lower(env
->get_double_arg(i
->first
, 1), 0);
2261 } else if (!strcmp(option_name
, "va-upper")) {
2262 const char *option
= env
->get_string_arg(i
->first
, 1);
2265 sscanf(option
, "%lf%n", &va_upper
, &characters
);
2266 if (*(option
+ characters
) == '%')
2267 ui::get()->error("--va-upper= does not accept '%' arguments\n");
2269 d3::cpf::set_va_upper(va_upper
);
2270 } else if (!strcmp(option_name
, "cpp-upper")) {
2271 const char *option
= env
->get_string_arg(i
->first
, 1);
2272 double perturb_upper
;
2274 sscanf(option
, "%lf%n", &perturb_upper
, &characters
);
2275 if (*(option
+ characters
) == '%')
2276 ui::get()->error("--cpp-upper= does not currently accept '%' arguments\n");
2278 d3::cpf::set_cpp_upper(perturb_upper
);
2279 } else if (!strcmp(option_name
, "cpp-lower")) {
2280 const char *option
= env
->get_string_arg(i
->first
, 1);
2281 double perturb_lower
;
2283 sscanf(option
, "%lf%n", &perturb_lower
, &characters
);
2284 if (*(option
+ characters
) == '%')
2285 ui::get()->error("--cpp-lower= does not currently accept '%' arguments\n");
2287 d3::cpf::set_cpp_lower(perturb_lower
);
2288 } else if (!strcmp(option_name
, "hf-enhance")) {
2289 unsupported::discontinued("--hf-enhance=<x>");
2290 } else if (!strcmp(option_name
, "rot-upper")) {
2291 d2::align::set_rot_max((int) floor(env
->get_double_arg(i
->first
, 1)));
2292 } else if (!strcmp(option_name
, "bda-mult")) {
2293 d2::align::set_bda_mult(env
->get_double_arg(i
->first
, 1));
2294 } else if (!strcmp(option_name
, "bda-rate")) {
2295 d2::align::set_bda_rate(env
->get_double_arg(i
->first
, 1));
2296 } else if (!strcmp(option_name
, "lod-max")) {
2297 d2::align::set_lod_max((int) floor(env
->get_double_arg(i
->first
, 1)));
2298 } else if (!strcmp(option_name
, "cpf-load")) {
2299 d3::cpf::init_loadfile(env
->get_string_arg(i
->first
, 1));
2301 } else if (!strcmp(option_name
, "model-load")) {
2302 d3::scene::load_model(env
->get_string_arg(i
->first
, 1));
2303 } else if (!strcmp(option_name
, "model-save")) {
2304 d3::scene::save_model(env
->get_string_arg(i
->first
, 1));
2306 } else if (!strcmp(option_name
, "trans-load")) {
2307 d2::tload_delete(tload
);
2308 tload
= d2::tload_new(env
->get_string_arg(i
->first
, 1));
2309 d2::align::set_tload(tload
);
2310 } else if (!strcmp(option_name
, "trans-save")) {
2311 tsave_delete(tsave
);
2312 tsave
= d2::tsave_new(env
->get_string_arg(i
->first
, 1));
2313 d2::align::set_tsave(tsave
);
2314 } else if (!strcmp(option_name
, "3d-trans-load")) {
2315 d3::tload_delete(d3_tload
);
2316 d3_tload
= d3::tload_new(env
->get_string_arg(i
->first
, 1));
2317 d3::align::set_tload(d3_tload
);
2318 } else if (!strcmp(option_name
, "3d-trans-save")) {
2319 d3::tsave_delete(d3_tsave
);
2320 d3_tsave
= d3::tsave_new(env
->get_string_arg(i
->first
, 1));
2321 d3::align::set_tsave(d3_tsave
);
2328 * Apply implication logic.
2331 if (extend
== 0 && vise_count
!= 0) {
2332 implication::changed("VISP requires increased image extents.",
2333 "Image extension is now enabled.",
2338 if (psf_match
&& ex_count
)
2339 unsupported::fornow("PSF calibration with exclusion regions.");
2342 if (d3_output
!= NULL
&& ip_iterations
!= 0)
2343 unsupported::fornow("3D modeling with Irani-Peleg rendering");
2346 if (extend
== 0 && d3_output
!= NULL
) {
2347 implication::changed("3D modeling requires increased image extents.",
2348 "Image extension is now enabled.",
2355 if (cx_parameter
!= 0 && !exposure_register
) {
2356 implication::changed("Certainty-based rendering requires exposure registration.",
2357 "Exposure registration is now enabled.",
2359 d2::align::exp_register();
2360 exposure_register
= 1;
2365 * Set alignment class exclusion region static variables
2368 d2::align::set_exclusion(ex_parameters
, ex_count
);
2371 * Initialize renderer class statics.
2374 d2::render::render_init(ex_count
, ex_parameters
, ex_show
, extend
, scale_factor
);
2380 d2::exposure::set_confidence(cx_parameter
);
2383 * Keep transformations for Irani-Peleg, psf-match, and
2387 if (ip_iterations
> 0 || psf_match
|| vise_count
> 0) {
2392 * Initialize device-specific variables
2395 // int input_file_count = argc - i - 1;
2396 int input_file_count
= files
.size() - 1;
2398 d2::psf
*device_response
[psf_N
] = { NULL
, NULL
};
2399 d2::exposure
**input_exposure
= NULL
;
2400 ale_pos view_angle
= 43.7 * M_PI
/ 180;
2401 // ale_pos view_angle = 90 * M_PI / 180;
2402 input_exposure
= (d2::exposure
**)
2403 // malloc((argc - i - 1) * sizeof(d2::exposure *));
2404 malloc(input_file_count
* sizeof(d2::exposure
*));
2406 if (device
!= NULL
) {
2407 if (!strcmp(device
, "xvp610_640x480")) {
2408 device_response
[psf_linear
] = new xvp610_640x480::lpsf();
2409 device_response
[psf_nonlinear
] = new xvp610_640x480::nlpsf();
2410 for (int ii
= 0; ii
< input_file_count
; ii
++)
2411 input_exposure
[ii
] = new xvp610_640x480::exposure();
2412 view_angle
= xvp610_640x480::view_angle();
2413 } else if (!strcmp(device
, "xvp610_320x240")) {
2414 device_response
[psf_linear
] = new xvp610_320x240::lpsf();
2415 device_response
[psf_nonlinear
] = new xvp610_320x240::nlpsf();
2416 for (int ii
= 0; ii
< input_file_count
; ii
++)
2417 input_exposure
[ii
] = new xvp610_320x240::exposure();
2418 view_angle
= xvp610_320x240::view_angle();
2419 } else if (!strcmp(device
, "ov7620")) {
2420 device_response
[psf_linear
] = new ov7620_raw_linear::lpsf();
2421 device_response
[psf_nonlinear
] = NULL
;
2422 for (int ii
= 0; ii
< input_file_count
; ii
++)
2423 input_exposure
[ii
] = new ov7620_raw_linear::exposure();
2424 d2::image_rw::set_default_bayer(IMAGE_BAYER_BGRG
);
2425 } else if (!strcmp(device
, "canon_300d")) {
2426 device_response
[psf_linear
] = new canon_300d_raw_linear::lpsf();
2427 device_response
[psf_nonlinear
] = NULL
;
2428 for (int ii
= 0; ii
< input_file_count
; ii
++)
2429 input_exposure
[ii
] = new canon_300d_raw_linear::exposure();
2430 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG
);
2431 } else if (!strcmp(device
, "nikon_d50")) {
2432 device_response
[psf_linear
] = nikon_d50::lpsf();
2433 device_response
[psf_nonlinear
] = nikon_d50::nlpsf();
2434 for (int ii
= 0; ii
< input_file_count
; ii
++)
2435 input_exposure
[ii
] = new nikon_d50::exposure();
2436 d2::image_rw::set_default_bayer( nikon_d50::bayer() );
2437 } else if (!strcmp(device
, "canon_300d+85mm_1.8")) {
2438 device_response
[psf_linear
] = new canon_300d_raw_linear_85mm_1_8::lpsf();
2439 device_response
[psf_nonlinear
] = NULL
;
2440 for (int ii
= 0; ii
< input_file_count
; ii
++)
2441 input_exposure
[ii
] = new canon_300d_raw_linear_85mm_1_8::exposure();
2442 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG
);
2443 view_angle
= canon_300d_raw_linear_85mm_1_8::view_angle();
2444 } else if (!strcmp(device
, "canon_300d+50mm_1.8")) {
2445 device_response
[psf_linear
] = new canon_300d_raw_linear_50mm_1_8::lpsf();
2446 device_response
[psf_nonlinear
] = NULL
;
2447 for (int ii
= 0; ii
< input_file_count
; ii
++)
2448 input_exposure
[ii
] = new canon_300d_raw_linear_50mm_1_8::exposure();
2449 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG
);
2450 view_angle
= canon_300d_raw_linear_50mm_1_8::view_angle();
2451 } else if (!strcmp(device
, "canon_300d+50mm_1.4")) {
2452 device_response
[psf_linear
] = new canon_300d_raw_linear_50mm_1_4::lpsf();
2453 device_response
[psf_nonlinear
] = NULL
;
2454 for (int ii
= 0; ii
< input_file_count
; ii
++)
2455 input_exposure
[ii
] = new canon_300d_raw_linear_50mm_1_4::exposure();
2456 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG
);
2457 view_angle
= canon_300d_raw_linear_50mm_1_4::view_angle();
2458 } else if (!strcmp(device
, "canon_300d+50mm_1.4@1.4")) {
2459 device_response
[psf_linear
] = new canon_300d_raw_linear_50mm_1_4_1_4::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_50mm_1_4_1_4::exposure();
2463 d2::image_rw::set_default_bayer(IMAGE_BAYER_RGBG
);
2464 view_angle
= canon_300d_raw_linear_50mm_1_4_1_4::view_angle();
2466 ui::get()->unknown_device(device
);
2469 for (int ii
= 0; ii
< input_file_count
; ii
++)
2470 input_exposure
[ii
] = new d2::exposure_default();
2474 * User-specified variables.
2477 if (user_view_angle
!= 0) {
2478 view_angle
= user_view_angle
;
2481 if (user_bayer
!= IMAGE_BAYER_DEFAULT
) {
2482 d2::image_rw::set_default_bayer(user_bayer
);
2486 * PSF-match exposure.
2489 delete input_exposure
[input_file_count
- 1];
2490 input_exposure
[input_file_count
- 1] = new d2::exposure_default();
2494 * Initialize output exposure
2497 d2::exposure
*output_exposure
= new d2::exposure_default();
2498 output_exposure
->set_multiplier(exp_mult
);
2501 * Configure the response function.
2504 d2::psf
*response
[2] = {NULL
, NULL
};
2506 for (int n
= 0; n
< psf_N
; n
++ ) {
2507 if (psf
[n
] != NULL
) {
2509 response
[n
] = d2::psf_parse::get((n
== psf_linear
), psf
[n
]);
2511 } else if (device_response
[n
] != NULL
) {
2514 * Device-specific response
2517 response
[n
] = device_response
[n
];
2522 * Default point-spread function.
2525 if (n
== psf_linear
) {
2528 * Default lpsf is a box filter
2529 * of diameter 1.0 (radius
2533 response
[n
] = new d2::box(0.5);
2535 } else if (n
== psf_nonlinear
) {
2538 * nlpsf is disabled by default.
2547 * First file argument. Print general file information as well
2548 * as information specific to this argument. Initialize image
2552 // d2::image_rw::init(argc - i - 1, argv + i, argv[argc - 1], input_exposure, output_exposure);
2553 // ochain_names[0] = argv[argc - 1];
2555 const char **input_files
= (const char **) malloc(sizeof(const char *) * input_file_count
);
2556 for (int i
= 0; i
< input_file_count
; i
++) {
2557 input_files
[i
] = files
[i
].first
;
2560 d2::image_rw::init(input_file_count
, input_files
, files
[files
.size() - 1].first
,
2561 input_exposure
, output_exposure
);
2563 ochain_names
[0] = files
[files
.size() - 1].first
;
2566 * Handle control point data for alignment
2568 d2::align::set_cp_count(d3::cpf::count());
2569 for (unsigned int ii
= 0; ii
< d3::cpf::count(); ii
++)
2570 d2::align::set_cp(ii
, d3::cpf::get_2d(ii
));
2573 * PSF-match bayer patterns.
2577 // d2::image_rw::set_specific_bayer(argc - i - 2, IMAGE_BAYER_NONE);
2578 d2::image_rw::set_specific_bayer(input_file_count
- 1, IMAGE_BAYER_NONE
);
2582 * Handle alignment weight map, if necessary
2585 if (wm_filename
!= NULL
) {
2586 d2::image
*weight_map
;
2587 weight_map
= d2::image_rw::read_image(wm_filename
, new d2::exposure_linear());
2588 weight_map
->set_offset(wm_offsety
, wm_offsetx
);
2589 d2::align::set_weight_map(weight_map
);
2593 * Initialize alignment interpolant.
2596 if (afilter_type
!= "internal")
2597 d2::align::set_interpolant(d2::render_parse::get_SSF(afilter_type
));
2600 * Initialize achain and ochain.
2603 achain
= d2::render_parse::get(achain_type
);
2605 for (int chain
= 0; chain
< oc_count
; chain
++)
2606 ochain
[chain
] = d2::render_parse::get(ochain_types
[chain
]);
2609 * Use merged renderings as reference images in
2613 d2::align::set_reference(achain
);
2616 * Tell the alignment class about the scale factor.
2619 d2::align::set_scale(scale_factor
);
2625 d2::vise_core::set_scale(vise_scale_factor
);
2627 for (int opt
= 0; opt
< vise_count
; opt
++) {
2628 d2::vise_core::add(d2::render_parse::get(visp
[opt
* 4 + 0]),
2635 * Initialize non-incremental renderers
2639 if (usm_multiplier
!= 0) {
2642 * Unsharp Mask renderer
2645 ochain
[0] = new d2::usm(ochain
[0], scale_factor
,
2646 usm_multiplier
, inc
, response
[psf_linear
],
2647 response
[psf_nonlinear
], &input_exposure
[0]);
2654 * Point-spread function calibration renderer.
2655 * This renderer does not produce image output.
2656 * It is reserved for use with the point-spread
2657 * function calibration script
2658 * ale-psf-calibrate.
2661 ochain
[0] = new d2::psf_calibrate(ochain
[0],
2662 1, inc
, response
[psf_linear
],
2663 response
[psf_nonlinear
],
2666 } else if (ip_iterations
!= 0) {
2669 * Irani-Peleg renderer
2672 ochain
[0] = new d2::ipc( ochain
[0], ip_iterations
,
2673 inc
, response
[psf_linear
],
2674 response
[psf_nonlinear
],
2675 (exposure_register
== 1), ip_use_median
, ipwl
);
2679 * Iterate through all files.
2682 for (unsigned int j
= 0; j
< d2::image_rw::count(); j
++) {
2685 * Iterate through non-global options
2688 environment
*env
= files
[j
].second
;
2690 for (std::map
<const char *, const char *>::iterator i
= env
->get_map().begin();
2691 i
!= env
->get_map().end(); i
++) {
2693 if (!env
->is_option(i
->first
))
2696 const char *option_name
= env
->get_option_name(i
->first
);
2698 if (!strcmp(option_name
, "ma-card")) {
2699 int card
= env
->get_int_arg(i
->first
, 1);
2701 ui::get()->error("--ma-card requires a positive integer");
2702 d2::align::ma_card(card
);
2703 } else if (!strcmp(option_name
, "ma-cont")) {
2704 d2::align::ma_cont(env
->get_double_arg(i
->first
, 1));
2705 } else if (!strcmp(option_name
, "gs")) {
2706 d2::align::gs(env
->get_string_arg(i
->first
, 1));
2707 } else if (!strcmp(option_name
, "gs-mo")) {
2708 const char *option
= env
->get_string_arg(i
->first
, 1);
2711 sscanf(option
, "%lf%n", &gs_mo
, &characters
);
2712 if (*(option
+ characters
) == '%')
2713 d2::align::gs_mo(gs_mo
, 1);
2715 d2::align::gs_mo(gs_mo
, 0);
2716 } else if (!strcmp(option_name
, "ev")) {
2717 double ev
= env
->get_double_arg(i
->first
, 1);
2718 double gain_value
= pow(2, -ev
);
2721 d2::exposure::set_gain_reference(gain_value
);
2723 input_exposure
[j
]->set_gain_multiplier(
2724 d2::exposure::get_gain_reference()
2727 } else if (!strcmp(option_name
, "black")) {
2728 double black
= env
->get_double_arg(i
->first
, 1);
2729 input_exposure
[j
]->set_black_level(black
);
2730 } else if (!strcmp(option_name
, "perturb-upper")) {
2731 const char *option
= env
->get_string_arg(i
->first
, 1);
2732 double perturb_upper
;
2734 sscanf(option
, "%lf%n", &perturb_upper
, &characters
);
2735 if (*(option
+ characters
) == '%')
2736 d2::align::set_perturb_upper(perturb_upper
, 1);
2738 d2::align::set_perturb_upper(perturb_upper
, 0);
2739 } else if (!strcmp(option_name
, "threads")) {
2740 thread::set_count((unsigned int) env
->get_int_arg(i
->first
, 1));
2741 } else if (!strcmp(option_name
, "per-cpu")) {
2742 thread::set_per_cpu((unsigned int) env
->get_int_arg(i
->first
, 1));
2745 * This error should be encountered earlier.
2750 fprintf(stderr
, "\n\nError: option `%s' must be applied globally.", option_name
);
2751 fprintf(stderr
, "\n\nHint: Move option `%s' prior to file and scope operators.\n\n",
2762 * Write comment information about original frame and
2763 * target image to the transformation save file, if we
2767 const d2::image
*im
= d2::image_rw::open(0);
2768 // tsave_orig(tsave, argv[i], im->avg_channel_magnitude());
2769 // tsave_target(tsave, argv[argc - 1]);
2770 tsave_orig(tsave
, files
[0].first
, im
->avg_channel_magnitude());
2771 tsave_target(tsave
, files
[files
.size() - 1].first
);
2772 d2::image_rw::close(0);
2775 * Handle the original frame.
2778 // ui::get()->original_frame_start(argv[i]);
2779 ui::get()->original_frame_start(files
[0].first
);
2781 for (int opt
= 0; opt
< oc_count
; opt
++) {
2782 ui::get()->set_orender_current(opt
);
2783 ochain
[opt
]->sync(0);
2785 ui::get()->writing_output(opt
);
2786 d2::image_rw::write_image(ochain_names
[opt
],
2787 ochain
[opt
]->get_image(0));
2791 d2::vise_core::frame_queue_add(0);
2793 ui::get()->original_frame_done();
2799 * Handle supplemental frames.
2802 const char *name
= d2::image_rw::name(j
);
2804 ui::get()->supplemental_frame_start(name
);
2807 * Write comment information about the
2808 * supplemental frame to the transformation
2809 * save file, if we have one.
2812 tsave_info (tsave
, name
);
2814 const d2::image
*im
= d2::image_rw::open(j
);
2815 d2::pixel apm
= im
->avg_channel_magnitude();
2816 tsave_apm(tsave
, apm
[0], apm
[1], apm
[2]);
2817 d2::image_rw::close(j
);
2819 for (int opt
= 0; opt
< oc_count
; opt
++) {
2820 ui::get()->set_orender_current(opt
);
2821 ochain
[opt
]->sync(j
);
2823 ui::get()->writing_output(opt
);
2824 d2::image_rw::write_image(ochain_names
[opt
],
2825 ochain
[opt
]->get_image(j
));
2829 d2::vise_core::frame_queue_add(j
);
2831 ui::get()->supplemental_frame_done();
2835 * Do any post-processing and output final image
2837 * XXX: note that the Irani-Peleg renderer currently
2838 * returns zero for ochain[0]->sync(), since it writes
2839 * output internally when inc != 0.
2842 for (int opt
= 0; opt
< oc_count
; opt
++)
2843 if ((ochain
[opt
]->sync() || !inc
) && !psf_match
) {
2844 ui::get()->writing_output(opt
);
2845 d2::image_rw::write_image(ochain_names
[opt
], ochain
[opt
]->get_image());
2849 * Output a summary match statistic.
2852 ui::get()->ale_2d_done((double) d2::align::match_summary());
2855 * Perform any 3D tasks
2858 optimizations::begin_3d_work();
2862 ui::get()->d3_start();
2864 d3::align::init_angle(view_angle
);
2866 ui::get()->d3_init_view_angle(view_angle
/ M_PI
* 180);
2868 d3::align::init_from_d2();
2870 if (d3::cpf::count() > 0) {
2871 ui::get()->d3_control_point_solve();
2872 d3::cpf::solve_3d();
2873 ui::get()->d3_control_point_solve_done();
2876 ui::get()->d3_final_view_angle(d3::align::angle_of(0) / M_PI
* 180);
2878 d3::align::write_alignments();
2880 d3::scene::set_filter_type(d3chain_type
);
2882 d3::scene::init_from_d2();
2884 ui::get()->d3_subdividing_space();
2885 d3::scene::make_space(d3_depth
, d3_output
, &d3_depth_pt
, &d3_output_pt
);
2886 ui::get()->d3_subdividing_space_done();
2888 ui::get()->d3_updating_occupancy();
2889 d3::scene::reduce_cost_to_search_depth(output_exposure
, inc
);
2890 ui::get()->d3_updating_occupancy_done();
2892 d3::scene::d3px(d3px_count
, d3px_parameters
);
2894 for (unsigned int i
= 0; i
< d2::image_rw::count(); i
++) {
2895 assert (i
< d3_count
);
2897 if (d3_depth
[i
] != NULL
) {
2898 ui::get()->d3_writing_output(d3_depth
[i
]);
2899 ui::get()->d3_render_status(0, 0, -1, -1, -1, -1, 0);
2900 const d2::image
*im
= d3::scene::depth(i
);
2901 d2::image_rw::write_image(d3_depth
[i
], im
, output_exposure
, 1, 1);
2903 ui::get()->d3_writing_output_done();
2906 if (d3_output
[i
] != NULL
) {
2907 ui::get()->d3_writing_output(d3_output
[i
]);
2908 const d2::image
*im
= d3::scene::view(i
);
2909 d2::image_rw::write_image(d3_output
[i
], im
, output_exposure
);
2911 d3::focus::set_camera(view_count
++);
2912 ui::get()->d3_writing_output_done();
2915 for (std::map
<const char *, d3::pt
>::iterator i
= d3_output_pt
.begin();
2916 i
!= d3_output_pt
.end(); i
++) {
2918 ui::get()->d3_writing_output(i
->first
);
2919 const d2::image
*im
= d3::scene::view(i
->second
);
2920 d2::image_rw::write_image(i
->first
, im
, output_exposure
);
2922 d3::focus::set_camera(view_count
++);
2923 ui::get()->d3_writing_output_done();
2926 for (std::map
<const char *, d3::pt
>::iterator i
= d3_depth_pt
.begin();
2927 i
!= d3_depth_pt
.end(); i
++) {
2929 ui::get()->d3_writing_output(i
->first
);
2930 ui::get()->d3_render_status(0, 0, -1, -1, -1, -1, 0);
2931 const d2::image
*im
= d3::scene::depth(i
->second
);
2932 d2::image_rw::write_image(i
->first
, im
, output_exposure
, 1, 1);
2934 ui::get()->d3_writing_output_done();
2938 for (unsigned int i
= d2::image_rw::count(); i
< d3_count
; i
++) {
2939 if (d3_depth
[i
] != NULL
) {
2940 fprintf(stderr
, "\n\n*** Frame number for --3dd too high. ***\n\n");
2942 if (d3_output
[i
] != NULL
) {
2943 fprintf(stderr
, "\n\n*** Frame number for --3dv too high. ***\n\n");
2949 * Destroy the image file handler
2952 d2::image_rw::destroy();
2955 * Delete the transformation file structures, if any
2959 tsave_delete(tsave
);
2960 tload_delete(tload
);