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"
45 #include "unsupported.h"
46 #include "implication.h"
60 #include "../ale_pos.h"
61 #include "../ale_real.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"
104 * Flag for global options.
107 static int global_options
;
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
))
123 else if (!strcmp(argv
[i
], "--"))
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
)))
139 else if (!strcmp(argv
[i
], "--"))
146 * Reallocation function
148 static void *local_realloc(void *ptr
, size_t size
) {
149 void *new_ptr
= realloc(ptr
, size
);
152 ui::get()->memory_error_location("main()");
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
{
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.
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
);
217 ui::get()->memory_error_location("environment::set_ptr");
219 int count
= snprintf(c
, chars
, "%p", pointer
);
221 assert (count
>= 0 && count
< chars
);
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"))
241 void name_check(const char *name
) {
242 if (!name_ok(name
)) {
243 fprintf(stderr
, "Bad set operation.");
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
) {
265 internal_set(name
, value
);
268 void unset(const char *name
) {
270 internal_unset(name
);
273 void set_ptr(const char *name
, const void *pointer
) {
275 internal_set_ptr(name
, pointer
);
278 const char *get(const char *name
) {
279 if (environment_map
.count(name
) == 0)
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
;
294 environment_set
.insert(s
);
297 static int is_env(const char *name
) {
299 sscanf(name
, "%p", &ptr_value
);
302 * Check for bad pointers.
305 if (!environment_set
.count((environment
*) ptr_value
)) {
312 const char *get_option_name(const char *name
) {
313 if (strncmp(name
, "0 ", strlen("0 ")))
316 name
+= strlen("0 ");
318 if (!isdigit(name
[0]))
321 while (isdigit(name
[0]))
324 if (!isspace(name
[0]))
327 while (isspace(name
[0]))
330 if (!isalnum(name
[0]))
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
);
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
];
384 free(desired_string
);
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
);
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
));
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
);
413 fprintf(stderr
, "\n\nError: bad argument in `%s'.\n\n", get_option_name(name
));
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
);
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
));
436 static environment
*get_env(const char *name
) {
441 sscanf(name
, "%p", &ptr_value
);
444 * Check for bad pointers.
447 if (!environment_set
.count((environment
*) ptr_value
)) {
449 fprintf(stderr
, "Bad environment pointer.\n");
453 return (environment
*) ptr_value
;
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
))
482 if (is_env(i
->second
)) {
483 e
->set_ptr(i
->first
, get_env(i
->second
)->clone());
485 e
->set(i
->first
, i
->second
);
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();
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() {
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
) {
552 environment
*dup_item
= get_env(get("---dup"));
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"));
566 * Read tokens from a stream.
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) {
590 virtual ~token_reader() {
594 class argument_parsing_token_reader
: public token_reader
{
596 const char *separators
;
598 argument_parsing_token_reader(const char *s
) {
603 int expects_exactly_one_option(void) {
607 virtual const char *get() {
608 int length
= strcspn(index
, separators
);
613 const char *result
= strndup(index
, length
);
616 if (strspn(index
, separators
) >= 1)
624 virtual const char *peek() {
625 int length
= strcspn(index
, separators
);
630 const char *result
= strndup(index
, length
);
635 virtual token_reader
*divert(const char *open_token
, const char *close_token
) {
641 class cstring_token_reader
: public token_reader
{
642 const char *separators
;
646 cstring_token_reader(const char *s
, int ephemeral
) {
647 assert(ephemeral
== 1);
649 separators
= "\n \t";
655 cstring_token_reader(const char *s
) {
656 separators
= "\n \t";
663 string
+= strspn(string
, separators
);
665 size_t length_to_next
= strcspn(string
, separators
);
667 if (length_to_next
== 0)
670 const char *result
= strndup(string
, length_to_next
);
672 string
+= length_to_next
;
678 string
+= strspn(string
, separators
);
680 size_t length_to_next
= strcspn(string
, separators
);
682 if (length_to_next
== 0)
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.
696 int next
= strcspn(string
, separators
);
699 while (*(string
+ search
) != '\0' &&
700 (depth
|| strcmp(close_token
, (string
+ search
)))) {
701 if (!strcmp(close_token
, (string
+ search
)))
703 if (!strcmp(open_token
, (string
+ search
)))
706 next
= strcspn((string
+ next
), separators
);
709 if (*(string
+ search
) == '\0') {
710 fprintf(stderr
, "Parse error: End of scope not found.");
714 cstring_token_reader
*result
= new cstring_token_reader(strndup(string
, search
), 1);
719 * Eat the closing token.
727 ~cstring_token_reader() {
729 free((void *) string
);
733 class cli_token_reader
: public token_reader
{
740 cli_token_reader(int c
, const char *v
[]) {
748 if (arg_index
< argc
)
749 return argv
[arg_index
++];
757 if (arg_index
< argc
)
758 return argv
[arg_index
];
764 cli_token_reader
*divert(const char *open_token
, const char *close_token
) {
768 while (arg_index
+ search
< argc
769 && (depth
|| strcmp(argv
[arg_index
+ search
], close_token
))) {
770 if (!strcmp(close_token
, argv
[arg_index
+ search
]))
772 if (!strcmp(open_token
, argv
[arg_index
+ search
]))
777 if (arg_index
+ search
== argc
) {
778 fprintf(stderr
, "Parse error: end of scope not found.\n");
782 cli_token_reader
*result
= new cli_token_reader(search
, argv
+ arg_index
);
787 * Eat the closing token.
797 struct simple_option
{
799 const char *map_name
;
800 const char *map_value
;
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) {
812 if (!strcmp(unadorned
, token
) && !require_ornamentation
)
815 while (token
[0] == '-' && strip_max
) {
818 if (!strcmp(unadorned
, token
))
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
))
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;
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
);
851 snprintf(result
, length
, "%u 0 %s", arg_num
, unadorned
);
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.
864 snprintf(result
, length
, "%u %u %s", arg_num
, multi_counter
, unadorned
);
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
);
883 static int table_contains(const char **haystack
, const char *needle
, int prefix_length
= 0) {
888 while (*haystack
!= NULL
) {
889 if (prefix_length
== 0 && !strcmp(*haystack
, needle
))
891 if (prefix_length
> 0 && !strncmp(*haystack
, needle
, prefix_length
))
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
))
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
))
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
))
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
);
933 static void remove_nonglobals(environment
*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
))
944 if (!table_contains(supported_nonglobal_option_table
, a
->get_option_name(i
->first
)))
947 removal_stack
.push(i
->first
);
950 while (!removal_stack
.empty()) {
951 remove_option(a
, removal_stack
.top());
956 static void option_intersect(environment
*a
, environment
*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
))
968 if (option_is_identical(a
, b
, i
->first
))
971 removal_stack
.push(i
->first
);
974 while (!removal_stack
.empty()) {
975 remove_option(a
, removal_stack
.top());
980 static void option_difference(environment
*a
, environment
*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
))
992 if (!option_is_identical(a
, b
, i
->first
))
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
) {
1007 int end_of_options
= 0;
1009 while ((token
= tr
->get())) {
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
);
1021 } else if (!strcmp(token
, "[") && !end_of_options
) {
1023 environment::push();
1024 token_reader
*tr_nest
= tr
->divert("[", "]");
1025 evaluate_stream(tr_nest
, files
);
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
);
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
);
1054 * Trap the end-of-option indicator.
1057 else if (!strcmp(token
, "--")) {
1063 * Check for options and filenames
1071 if (strncmp("-", token
, strlen("-")) || end_of_options
) {
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
);
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"));
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"));
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
))
1134 * Handle the match case.
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
),
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
);
1174 * Reject scope operators as options,
1178 if (is_scope_operator(option
)) {
1179 fprintf(stderr
, "\n\nError: illegal argument to `%s'.\n\n", token
);
1183 target
->set_with_dup(option_name_gen(simple_option_table
[i
].name
,
1184 simple_option_table
[i
].map_name
,
1186 simple_option_table
[i
].multi
),
1192 * Trap illegal options.
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
);
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
[] = {
1249 "ALE_COUNT_THREADS",
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
: "");
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"));
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
)
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("--"))
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
;
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");
1342 d2::image
*i
= d2::new_image_ale_real((int)h
, (int)w
, 3);
1343 d2::transformation t
= d2::transformation::gpt_identity(i
, 1);
1345 d2::point(tly
, tlx
),
1346 d2::point(bly
, blx
),
1347 d2::point(bry
, brx
),
1348 d2::point(tr_y
, trx
)
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]);
1366 * Thread initialization.
1372 * Flags and variables
1375 double scale_factor
= 1;
1376 double vise_scale_factor
= 1;
1378 double usm_multiplier
= 0.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;
1388 enum { psf_linear
, psf_nonlinear
, psf_N
};
1389 const char *psf
[psf_N
] = {NULL
, NULL
};
1390 const char *device
= NULL
;
1392 double psf_match_args
[6];
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
;
1400 d2::exclusion
*ex_parameters
= NULL
;
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
;
1411 const char **visp
= NULL
;
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";
1439 * Handle default settings
1442 if (arg_prefix_count(argc
, argv
, "--q") > 0)
1443 ui::get()->error("Default settings --q* are no longer recognized.");
1446 #if ALE_COLORS == FIXED16
1447 const char *defaults
=
1448 "--dchain auto:triangle:2,fine:box:1,triangle:2 "
1449 "--achain triangle:2 "
1451 "--3d-chain fine:triangle:2,fine:gauss:0.75,triangle:2 ";
1453 const char *defaults
=
1454 "--dchain auto:triangle:2,fine:box:1,triangle:2 "
1455 "--achain triangle:2 "
1457 "--3d-chain fine:triangle:2,fine:gauss:0.75,triangle:2 ";
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
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) {
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
))
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",
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
))
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();
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();
1570 } else if (!strcmp(option_name
, "align")) {
1571 if (!strcmp(env
->get_string_arg(i
->first
, 0), "align-all"))
1573 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "align-green"))
1575 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "align-sum"))
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();
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();
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();
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();
1609 } else if (!strcmp(option_name
, "profile")) {
1611 } else if (!strcmp(option_name
, "extend")) {
1612 if (env
->get_int_arg(i
->first
, 0))
1616 } else if (!strcmp(option_name
, "oc")) {
1617 if (env
->get_int_arg(i
->first
, 0))
1621 } else if (!strcmp(option_name
, "focus")) {
1625 double inf
= one
/ zero
;
1627 assert (isinf(inf
) && inf
> 0);
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")) {
1641 distance
= env
->get_double_arg(i
->first
, 2);
1643 } else if (!strcmp(env
->get_string_arg(i
->first
, 1), "p")) {
1647 px
= env
->get_double_arg(i
->first
, 2);
1648 py
= env
->get_double_arg(i
->first
, 3);
1651 bad_arg(option_name
);
1658 unsigned int ci
= 0;
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)
1678 } else if (!strncmp(option
, "fr=", 3)) {
1679 if(sscanf(option
+ 3, "%lf", &fr
) != 1)
1681 } else if (!strncmp(option
, "ht=", 3)) {
1682 if(sscanf(option
+ 3, "%lf", &ht
) != 1)
1684 } else if (!strncmp(option
, "vt=", 3)) {
1685 if(sscanf(option
+ 3, "%lf", &vt
) != 1)
1687 } else if (!strncmp(option
, "sy=", 3)) {
1688 if(sscanf(option
+ 3, "%lf", &sy
) != 1)
1690 } else if (!strncmp(option
, "ey=", 3)) {
1691 if(sscanf(option
+ 3, "%lf", &ey
) != 1)
1693 } else if (!strncmp(option
, "sx=", 3)) {
1694 if(sscanf(option
+ 3, "%lf", &sx
) != 1)
1696 } else if (!strncmp(option
, "ex=", 3)) {
1697 if(sscanf(option
+ 3, "%lf", &ex
) != 1)
1699 } else if (!strncmp(option
, "sd=", 3)) {
1700 if(sscanf(option
+ 3, "%lf", &sd
) != 1)
1702 } else if (!strncmp(option
, "ed=", 3)) {
1703 if(sscanf(option
+ 3, "%lf", &ed
) != 1)
1705 } else if (!strncmp(option
, "ap=", 3)) {
1706 if(sscanf(option
+ 3, "%lf", &ap
) != 1)
1708 } else if (!strncmp(option
, "sc=", 3)) {
1709 if(sscanf(option
+ 3, "%u", &sc
) != 1)
1711 } else if (!strncmp(option
, "sr=", 3)) {
1712 if (!strcmp(option
, "sr=aperture")) {
1714 } else if (!strcmp(option
, "sr=pixel")) {
1719 } else if (!strncmp(option
, "fs=", 3)) {
1720 if (!strcmp(option
, "fs=mean")) {
1722 } else if (!strcmp(option
, "fs=median")) {
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")) {
1736 * Unsupported configurations
1740 unsupported::fornow("3D modeling with Irani-Peleg rendering");
1744 unsupported::fornow("3D modeling with unsharp mask");
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
) {
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
;
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;
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
;
1793 } else if (!strcmp(option_name
, "3dv")) {
1796 unsigned int frame_no
;
1799 * Unsupported configurations
1803 unsupported::fornow("3D modeling with Irani-Peleg rendering");
1807 unsupported::fornow("3D modeling with unsharp mask");
1811 * Initialize if necessary
1814 if (d3_output
== NULL
) {
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")) {
1834 unsigned int frame_no
;
1837 * Unsupported configurations
1841 unsupported::fornow("3D modeling with Irani-Peleg rendering");
1845 unsupported::fornow("3D modeling with unsharp mask");
1849 * Initialize if necessary
1852 if (d3_output
== NULL
) {
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"))
1876 else if (!strcmp(env
->get_string_arg(i
->first
, 1), "cpu"))
1878 else if (!strcmp(env
->get_string_arg(i
->first
, 1), "accel"))
1880 else if (!strcmp(env
->get_string_arg(i
->first
, 1), "auto"))
1883 fprintf(stderr
, "Error: Unknown acceleration type '%s'\n",
1884 env
->get_string_arg(i
->first
, 1));
1887 } else if (!strcmp(option_name
, "ui")) {
1888 if (!strcmp(env
->get_string_arg(i
->first
, 1), "stream"))
1890 else if (!strcmp(env
->get_string_arg(i
->first
, 1), "tty"))
1892 else if (!strcmp(env
->get_string_arg(i
->first
, 1), "log"))
1894 else if (!strcmp(env
->get_string_arg(i
->first
, 1), "quiet"))
1897 fprintf(stderr
, "Error: Unknown user interface type '%s'\n",
1898 env
->get_string_arg(i
->first
, 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")) {
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));
1958 ui::get()->error_hint("--fl is not supported", "rebuild ALE with FFTW support");
1960 } else if (!strcmp(option_name
, "wmx")) {
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));
1966 ui::get()->error_hint("--wmx is not supported", "rebuild ALE with support for --wmx");
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
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
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
;
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
2092 } else if (!strcmp(option_name
, "exshow")) {
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);
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);
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
;
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")) {
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);
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);
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();
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();
2205 d3::align::vo_noadjust();
2206 } else if (!strcmp(option_name
, "ip-statistic")) {
2207 if (!strcmp(env
->get_string_arg(i
->first
, 0), "mean"))
2209 else if (!strcmp(env
->get_string_arg(i
->first
, 0), "median"))
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);
2216 ipwl
= env
->get_double_arg(i
->first
, 1);
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();
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();
2247 d3::scene::nofilter();
2248 } else if (!strcmp(option_name
, "occ-norm")) {
2249 if (env
->get_int_arg(i
->first
, 0))
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
;
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);
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);
2309 sscanf(option
, "%lf%n", &va_upper
, &characters
);
2310 if (*(option
+ characters
) == '%')
2311 ui::get()->error("--va-upper= does not accept '%' arguments\n");
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
;
2318 sscanf(option
, "%lf%n", &perturb_upper
, &characters
);
2319 if (*(option
+ characters
) == '%')
2320 ui::get()->error("--cpp-upper= does not currently accept '%' arguments\n");
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
;
2327 sscanf(option
, "%lf%n", &perturb_lower
, &characters
);
2328 if (*(option
+ characters
) == '%')
2329 ui::get()->error("--cpp-lower= does not currently accept '%' arguments\n");
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));
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));
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));
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
);
2389 * Initialize the interface.
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.",
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");
2413 if (extend
== 0 && d3_output
!= NULL
) {
2414 implication::changed("3D modeling requires increased image extents.",
2415 "Image extension is now enabled.",
2422 if (cx_parameter
!= 0 && !exposure_register
) {
2423 implication::changed("Certainty-based rendering requires exposure registration.",
2424 "Exposure registration is now enabled.",
2426 d2::align::exp_register();
2427 exposure_register
= 1;
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
);
2447 d2::exposure::set_confidence(cx_parameter
);
2450 * Keep transformations for Irani-Peleg, psf-match, and
2454 if (ip_iterations
> 0 || psf_match
|| vise_count
> 0) {
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();
2533 ui::get()->unknown_device(device
);
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.
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
];
2589 * Default point-spread function.
2592 if (n
== psf_linear
) {
2595 * Default lpsf is a box filter
2596 * of diameter 1.0 (radius
2600 response
[n
] = new d2::box(0.5);
2602 } else if (n
== psf_nonlinear
) {
2605 * nlpsf is disabled by default.
2614 * First file argument. Print general file information as well
2615 * as information specific to this argument. Initialize image
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.
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
2682 d2::align::set_reference(achain
);
2685 * Tell the alignment class about the scale factor.
2688 d2::align::set_scale(scale_factor
);
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]),
2704 * Initialize non-incremental renderers
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]);
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
],
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
))
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);
2779 sscanf(option
, "%lf%n", &gs_mo
, &characters
);
2780 if (*(option
+ characters
) == '%')
2781 d2::align::gs_mo(gs_mo
, 1);
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
);
2789 d2::exposure::set_gain_reference(gain_value
);
2791 input_exposure
[j
]->set_gain_multiplier(
2792 (double) d2::exposure::get_gain_reference()
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
;
2802 sscanf(option
, "%lf%n", &perturb_upper
, &characters
);
2803 if (*(option
+ characters
) == '%')
2804 d2::align::set_perturb_upper(perturb_upper
, 1);
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));
2813 * This error should be encountered earlier.
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",
2830 * Write comment information about original frame and
2831 * target image to the transformation save file, if we
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);
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();
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
);
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
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());
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();
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
);
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);
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
);
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
);
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);
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
3028 tsave_delete(tsave
);
3029 tload_delete(tload
);