src/align: Update copyright header for raw imports.
[libale.git] / src / align.c
blob7a5fe3dd0fea74c9a0fa28eca4f6a0ac9039f8da
1 /*
2 * Copyright 2002, 2004, 2005, 2006, 2007, 2008, 2009 David Hilvert <dhilvert@gmail.com>
4 * This file is part of libale.
6 * libale is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU Affero General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
9 * any later version.
11 * libale is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
14 * more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with libale. If not, see <http://www.gnu.org/licenses/>.
20 #include "libale.h"
23 * API Implementation.
26 #warning raw imported code should be revised for Libale.
27 #if 0
29 /* XXX: Raw import of code from ALE.
33 * Alignment state
35 * This structure contains alignment state information. The change
36 * between the non-default old initial alignment and old final
37 * alignment is used to adjust the non-default current initial
38 * alignment. If either the old or new initial alignment is a default
39 * alignment, the old --follow semantics are preserved.
42 class astate_t {
43 ale_trans old_initial_alignment;
44 ale_trans old_final_alignment;
45 ale_trans default_initial_alignment;
46 int old_is_default;
47 std::vector<int> is_default;
48 ale_image input_frame;
50 public:
51 astate_t() :
52 is_default(1) {
54 old_initial_alignment = ale_new_trans(accel::context(), NULL);
55 old_final_alignment = ale_new_trans(accel::context(), NULL);
56 default_initial_alignment = ale_new_trans(accel::context(), NULL);
58 input_frame = NULL;
59 is_default[0] = 1;
60 old_is_default = 1;
63 ale_image get_input_frame() const {
64 return input_frame;
67 void set_is_default(unsigned int index, int value) {
70 * Expand the array, if necessary.
72 if (index == is_default.size());
73 is_default.resize(index + 1);
75 assert (index < is_default.size());
76 is_default[index] = value;
79 int get_is_default(unsigned int index) {
80 assert (index < is_default.size());
81 return is_default[index];
84 ale_trans get_default() {
85 return default_initial_alignment;
88 void set_default(ale_trans t) {
89 default_initial_alignment = t;
92 void default_set_original_bounds(ale_image i) {
93 ale_trans_set_original_bounds(default_initial_alignment, i);
96 void set_final(ale_trans t) {
97 old_final_alignment = t;
100 void set_input_frame(ale_image i) {
101 input_frame = i;
105 * Implement new delta --follow semantics.
107 * If we have a transformation T such that
109 * prev_final == T(prev_init)
111 * Then we also have
113 * current_init_follow == T(current_init)
115 * We can calculate T as follows:
117 * T == prev_final(prev_init^-1)
119 * Where ^-1 is the inverse operator.
121 static trans_single follow(trans_single a, trans_single b, trans_single c) {
122 trans_single cc = c;
124 if (alignment_class == 0) {
126 * Translational transformations
129 ale_pos t0 = -a.eu_get(0) + b.eu_get(0);
130 ale_pos t1 = -a.eu_get(1) + b.eu_get(1);
132 cc.eu_modify(0, t0);
133 cc.eu_modify(1, t1);
135 } else if (alignment_class == 1) {
137 * Euclidean transformations
140 ale_pos t2 = -a.eu_get(2) + b.eu_get(2);
142 cc.eu_modify(2, t2);
144 point p( c.scaled_height()/2 + c.eu_get(0) - a.eu_get(0),
145 c.scaled_width()/2 + c.eu_get(1) - a.eu_get(1) );
147 p = b.transform_scaled(p);
149 cc.eu_modify(0, p[0] - c.scaled_height()/2 - c.eu_get(0));
150 cc.eu_modify(1, p[1] - c.scaled_width()/2 - c.eu_get(1));
152 } else if (alignment_class == 2) {
154 * Projective transformations
157 point p[4];
159 p[0] = b.transform_scaled(a
160 . scaled_inverse_transform(c.transform_scaled(point( 0 , 0 ))));
161 p[1] = b.transform_scaled(a
162 . scaled_inverse_transform(c.transform_scaled(point(c.scaled_height(), 0 ))));
163 p[2] = b.transform_scaled(a
164 . scaled_inverse_transform(c.transform_scaled(point(c.scaled_height(), c.scaled_width()))));
165 p[3] = b.transform_scaled(a
166 . scaled_inverse_transform(c.transform_scaled(point( 0 , c.scaled_width()))));
168 cc.gpt_set(p);
171 return cc;
175 * For multi-alignment following, we use the following approach, not
176 * guaranteed to work with large changes in scene or perspective, but
177 * which should be somewhat flexible:
179 * For
181 * t[][] calculated final alignments
182 * s[][] alignments as loaded from file
183 * previous frame n
184 * current frame n+1
185 * fundamental (primary) 0
186 * non-fundamental (non-primary) m!=0
187 * parent element m'
188 * follow(a, b, c) applying the (a, b) delta T=b(a^-1) to c
190 * following in the case of missing file data might be generated by
192 * t[n+1][0] = t[n][0]
193 * t[n+1][m!=0] = follow(t[n][m'], t[n+1][m'], t[n][m])
195 * cases with all noted file data present might be generated by
197 * t[n+1][0] = follow(s[n][0], t[n][0], s[n+1][0])
198 * t[n+1][m!=0] = follow(s[n+1][m'], t[n+1][m'], s[n+1][m])
200 * For non-following behavior, or where assigning the above is
201 * impossible, we assign the following default
203 * t[n+1][0] = Identity
204 * t[n+1][m!=0] = t[n+1][m']
207 void init_frame_alignment_primary(transformation *offset, int lod, ale_pos perturb) {
209 if (perturb > 0 && !old_is_default && !get_is_default(0)
210 && default_initial_alignment_type == 1) {
213 * Apply following logic for the primary element.
216 ui::get()->following();
218 trans_single new_offset = follow(old_initial_alignment.get_element(0),
219 old_final_alignment.get_element(0),
220 offset->get_element(0));
222 old_initial_alignment = *offset;
224 offset->set_element(0, new_offset);
226 ui::get()->set_offset(new_offset);
227 } else {
228 old_initial_alignment = *offset;
231 is_default.resize(old_initial_alignment.stack_depth());
234 void init_frame_alignment_nonprimary(transformation *offset,
235 int lod, ale_pos perturb, unsigned int index) {
237 assert (index > 0);
239 unsigned int parent_index = offset->parent_index(index);
241 if (perturb > 0
242 && !get_is_default(parent_index)
243 && !get_is_default(index)
244 && default_initial_alignment_type == 1) {
247 * Apply file-based following logic for the
248 * given element.
251 ui::get()->following();
253 trans_single new_offset = follow(old_initial_alignment.get_element(parent_index),
254 offset->get_element(parent_index),
255 offset->get_element(index));
257 old_initial_alignment.set_element(index, offset->get_element(index));
258 offset->set_element(index, new_offset);
260 ui::get()->set_offset(new_offset);
262 return;
265 offset->get_coordinate(parent_index);
268 if (perturb > 0
269 && old_final_alignment.exists(offset->get_coordinate(parent_index))
270 && old_final_alignment.exists(offset->get_current_coordinate())
271 && default_initial_alignment_type == 1) {
274 * Apply nonfile-based following logic for
275 * the given element.
278 ui::get()->following();
281 * XXX: Although it is different, the below
282 * should be equivalent to the comment
283 * description.
286 trans_single a = old_final_alignment.get_element(offset->get_coordinate(parent_index));
287 trans_single b = old_final_alignment.get_element(offset->get_current_coordinate());
288 trans_single c = offset->get_element(parent_index);
290 trans_single new_offset = follow(a, b, c);
292 offset->set_element(index, new_offset);
293 ui::get()->set_offset(new_offset);
295 return;
299 * Handle other cases.
302 if (get_is_default(index)) {
303 offset->set_element(index, offset->get_element(parent_index));
304 ui::get()->set_offset(offset->get_element(index));
308 void init_default() {
310 if (default_initial_alignment_type == 0) {
313 * Follow the transformation of the original frame,
314 * setting new image dimensions.
317 // astate->default_initial_alignment = orig_t;
318 default_initial_alignment.set_current_element(orig_t.get_element(0));
319 default_initial_alignment.set_dimensions(input_frame);
321 } else if (default_initial_alignment_type == 1)
324 * Follow previous transformation, setting new image
325 * dimensions.
328 default_initial_alignment.set_dimensions(input_frame);
330 else
331 assert(0);
333 old_is_default = get_is_default(0);
338 static struct scale_cluster *init_clusters(int frame, ale_pos scale_factor,
339 const image *input_frame, unsigned int steps,
340 int *local_ax_count) {
343 * Allocate memory for the array.
346 struct scale_cluster *scale_clusters =
347 (struct scale_cluster *) malloc(steps * sizeof(struct scale_cluster));
349 assert (scale_clusters);
351 if (!scale_clusters)
352 ui::get()->memory_error("alignment");
355 * Prepare images and exclusion regions for the highest level
356 * of detail.
359 scale_clusters[0].accum = reference_image;
361 ui::get()->constructing_lod_clusters(0.0);
362 scale_clusters[0].input_scale = scale_factor;
363 if (scale_factor < 1.0 && interpolant == NULL)
364 scale_clusters[0].input = input_frame->scale(scale_factor, "alignment");
365 else
366 scale_clusters[0].input = input_frame;
368 scale_clusters[0].certainty = reference_defined;
369 scale_clusters[0].aweight = alignment_weights;
370 scale_clusters[0].ax_parameters = filter_ax_parameters(frame, local_ax_count);
373 * Allocate and determine input frame certainty.
376 if (scale_clusters[0].input->get_bayer() != IMAGE_BAYER_NONE) {
377 scale_clusters[0].input_certainty = new_image_bayer_ale_real(
378 scale_clusters[0].input->height(),
379 scale_clusters[0].input->width(),
380 scale_clusters[0].input->depth(),
381 scale_clusters[0].input->get_bayer());
382 } else {
383 scale_clusters[0].input_certainty = scale_clusters[0].input->clone("certainty");
386 for (unsigned int i = 0; i < scale_clusters[0].input_certainty->height(); i++)
387 for (unsigned int j = 0; j < scale_clusters[0].input_certainty->width(); j++)
388 for (unsigned int k = 0; k < 3; k++)
389 if (scale_clusters[0].input->get_channels(i, j) & (1 << k))
390 ((image *) scale_clusters[0].input_certainty)->set_chan(i, j, k,
391 scale_clusters[0].input->
392 exp().confidence(scale_clusters[0].input->get_pixel(i, j))[k]);
394 scale_ax_parameters(*local_ax_count, scale_clusters[0].ax_parameters, scale_factor,
395 (scale_factor < 1.0 && interpolant == NULL) ? scale_factor : (ale_pos) 1);
397 init_nl_cluster(&(scale_clusters[0]));
400 * Prepare reduced-detail images and exclusion
401 * regions.
404 for (unsigned int step = 1; step < steps; step++) {
405 ui::get()->constructing_lod_clusters(step);
406 scale_clusters[step].accum = prepare_lod(scale_clusters[step - 1].accum);
407 scale_clusters[step].certainty = prepare_lod_def(scale_clusters[step - 1].certainty);
408 scale_clusters[step].aweight = prepare_lod_def(scale_clusters[step - 1].aweight);
409 scale_clusters[step].ax_parameters
410 = copy_ax_parameters(*local_ax_count, scale_clusters[step - 1].ax_parameters);
412 double sf = scale_clusters[step - 1].input_scale / 2;
413 scale_clusters[step].input_scale = sf;
415 if (sf >= 1.0 || interpolant != NULL) {
416 scale_clusters[step].input = scale_clusters[step - 1].input;
417 scale_clusters[step].input_certainty = scale_clusters[step - 1].input_certainty;
418 scale_ax_parameters(*local_ax_count, scale_clusters[step].ax_parameters, 0.5, 1);
419 } else if (sf > 0.5) {
420 scale_clusters[step].input = scale_clusters[step - 1].input->scale(sf, "alignment");
421 scale_clusters[step].input_certainty = scale_clusters[step - 1].input->scale(sf, "alignment", 1);
422 scale_ax_parameters(*local_ax_count, scale_clusters[step].ax_parameters, 0.5, sf);
423 } else {
424 scale_clusters[step].input = scale_clusters[step - 1].input->scale(0.5, "alignment");
425 scale_clusters[step].input_certainty = scale_clusters[step - 1].input_certainty->scale(0.5, "alignment", 1);
426 scale_ax_parameters(*local_ax_count, scale_clusters[step].ax_parameters, 0.5, 0.5);
429 init_nl_cluster(&(scale_clusters[step]));
432 return scale_clusters;
435 static diff_stat_t _align_element(ale_pos perturb, ale_pos local_lower,
436 scale_cluster *scale_clusters, diff_stat_t here,
437 ale_pos adj_p, ale_pos adj_o, ale_pos adj_b,
438 ale_pos *current_bd, ale_pos *modified_bd,
439 astate_t *astate, int lod, scale_cluster si) {
442 * Run initial tests to get perturbation multipliers and error
443 * centroids.
446 std::vector<d2::trans_single> t_set;
448 here.get_perturb_set(&t_set, adj_p, adj_o, adj_b, current_bd, modified_bd);
450 int stable_count = 0;
452 while (perturb >= local_lower) {
454 ui::get()->alignment_dims(scale_clusters[lod].accum->height(), scale_clusters[lod].accum->width(),
455 scale_clusters[lod].input->height(), scale_clusters[lod].input->width());
458 * Orientational adjustment value in degrees.
460 * Since rotational perturbation is now specified as an
461 * arclength, we have to convert.
464 ale_pos adj_o = 2 * (double) perturb
465 / sqrt(pow(scale_clusters[0].input->height(), 2)
466 + pow(scale_clusters[0].input->width(), 2))
467 * 180
468 / M_PI;
471 * Barrel distortion adjustment value
474 ale_pos adj_b = perturb * bda_mult;
476 trans_single old_offset = here.get_offset();
478 here.perturb_test(perturb, adj_p, adj_o, adj_b, current_bd, modified_bd,
479 stable_count);
481 if (here.get_offset() == old_offset)
482 stable_count++;
483 else
484 stable_count = 0;
486 if (stable_count == 3) {
488 stable_count = 0;
490 perturb *= 0.5;
492 if (lod > 0
493 && lod > lrint(log(perturb) / log(2)) - lod_preferred) {
496 * Work with images twice as large
499 lod--;
500 si = scale_clusters[lod];
503 * Rescale the transforms.
506 ale_pos rescale_factor = (double) scale_factor
507 / (double) pow(2, lod)
508 / (double) here.get_offset().scale();
510 here.rescale(rescale_factor, si);
512 } else {
513 adj_p = perturb / pow(2, lod);
517 * Announce changes
520 ui::get()->alignment_perturbation_level(perturb, lod);
523 ui::get()->set_match(here.get_error());
524 ui::get()->set_offset(here.get_offset());
527 if (lod > 0) {
528 ale_pos rescale_factor = (double) scale_factor
529 / (double) here.get_offset().scale();
531 here.rescale(rescale_factor, scale_clusters[0]);
534 return here;
538 * Align frame m against the reference.
540 * XXX: the transformation class currently combines ordinary
541 * transformations with scaling. This is somewhat convenient for
542 * some things, but can also be confusing. This method, _align(), is
543 * one case where special care must be taken to ensure that the scale
544 * is always set correctly (by using the 'rescale' method).
546 static diff_stat_multi _align(int m, int local_gs, astate_t *astate) {
548 const image *input_frame = astate->get_input_frame();
551 * Local upper/lower data, possibly dependent on image
552 * dimensions.
555 ale_pos local_lower, local_upper;
556 ale_accum local_gs_mo;
559 * Select the minimum dimension as the reference.
562 ale_pos reference_size = input_frame->height();
563 if (input_frame->width() < reference_size)
564 reference_size = input_frame->width();
565 ale_accum reference_area = input_frame->height()
566 * input_frame->width();
568 if (perturb_lower_percent)
569 local_lower = (double) perturb_lower
570 * (double) reference_size
571 * (double) 0.01
572 * (double) scale_factor;
573 else
574 local_lower = perturb_lower;
576 if (perturb_upper_percent)
577 local_upper = (double) perturb_upper
578 * (double) reference_size
579 * (double) 0.01
580 * (double) scale_factor;
581 else
582 local_upper = perturb_upper;
584 local_upper = pow(2, floor(log(local_upper) / log(2)));
586 if (gs_mo_percent)
587 local_gs_mo = (double) _gs_mo
588 * (double) reference_area
589 * (double) 0.01
590 * (double) scale_factor;
591 else
592 local_gs_mo = _gs_mo;
595 * Logarithms aren't exact, so we divide repeatedly to discover
596 * how many steps will occur, and pass this information to the
597 * user interface.
600 int step_count = 0;
601 double step_variable = local_upper;
602 while (step_variable >= local_lower) {
603 step_variable /= 2;
604 step_count++;
607 ale_pos perturb = local_upper;
609 if (_keep) {
610 kept_t[latest] = latest_t;
611 kept_ok[latest] = latest_ok;
615 * Determine how many levels of detail should be prepared, by
616 * calculating the initial (largest) value for the
617 * level-of-detail variable.
620 int lod = lrint(log(perturb) / log(2)) - lod_preferred;
622 if (lod < 0)
623 lod = 0;
625 while (lod > 0 && (reference_image->width() < pow(2, lod) * min_dimension
626 || reference_image->height() < pow(2, lod) * min_dimension))
627 lod--;
629 unsigned int steps = (unsigned int) lod + 1;
632 * Prepare multiple levels of detail.
635 int local_ax_count;
636 struct scale_cluster *scale_clusters = init_clusters(m,
637 scale_factor, input_frame, steps,
638 &local_ax_count);
641 * Initialize the default initial transform
644 astate->init_default();
647 * Set the default transformation.
650 transformation offset = astate->get_default();
653 * Establish boundaries
656 offset.set_current_bounds(reference_image);
658 ui::get()->alignment_degree_max(offset.get_coordinate(offset.stack_depth() - 1).degree);
660 if (offset.stack_depth() == 1) {
661 ui::get()->set_steps(step_count, 0);
662 } else {
663 ui::get()->set_steps(offset.get_coordinate(offset.stack_depth() - 1).degree + 1, 1);
667 * Load any file-specified transformations
670 for (unsigned int index = 0; index < offset.stack_depth(); index++) {
671 int is_default = 1;
672 unsigned int index_2;
673 offset.set_current_index(index);
675 offset = tload_next(tload, alignment_class == 2,
676 offset,
677 &is_default, offset.get_current_index() == 0);
679 index_2 = offset.get_current_index();
681 if (index_2 > index) {
682 for (unsigned int index_3 = index; index_3 < index_2; index_3++)
683 astate->set_is_default(index_3, 1);
685 index = index_2;
688 astate->set_is_default(index, is_default);
691 offset.set_current_index(0);
693 astate->init_frame_alignment_primary(&offset, lod, perturb);
696 * Control point alignment
699 if (local_gs == 5) {
701 transformation o = offset;
704 * Determine centroid data
707 point current, previous;
708 centroids(m, &current, &previous);
710 if (current.defined() && previous.defined()) {
711 o = orig_t;
712 o.set_dimensions(input_frame);
713 o.translate((previous - current) * o.scale());
714 current = previous;
718 * Determine rotation for alignment classes other than translation.
721 ale_pos lowest_error = cp_rms_error(m, o);
723 ale_pos rot_lower = 2 * (double) local_lower
724 / sqrt(pow(scale_clusters[0].input->height(), 2)
725 + pow(scale_clusters[0].input->width(), 2))
726 * 180
727 / M_PI;
729 if (alignment_class > 0)
730 for (double rot = 30; rot > rot_lower; rot /= 2)
731 for (double srot = -rot; srot < rot * 1.5; srot += rot * 2) {
732 int is_improved = 1;
733 while (is_improved) {
734 is_improved = 0;
735 transformation test_t = o;
737 * XXX: is this right?
739 test_t.rotate(current * o.scale(), srot);
740 ale_pos test_v = cp_rms_error(m, test_t);
742 if (test_v < lowest_error) {
743 lowest_error = test_v;
744 o = test_t;
745 srot += 3 * rot;
746 is_improved = 1;
752 * Determine projective parameters through a local
753 * minimum search.
756 if (alignment_class == 2) {
757 ale_pos adj_p = lowest_error;
759 if (adj_p < local_lower)
760 adj_p = local_lower;
762 while (adj_p >= local_lower) {
763 transformation test_t = o;
764 int is_improved = 1;
765 ale_pos test_v;
766 ale_pos adj_s;
768 while (is_improved) {
769 is_improved = 0;
771 for (int i = 0; i < 4; i++)
772 for (int j = 0; j < 2; j++)
773 for (adj_s = -adj_p; adj_s <= adj_p; adj_s += 2 * adj_p) {
775 test_t = o;
777 if (perturb_type == 0)
778 test_t.gpt_modify(j, i, adj_s);
779 else if (perturb_type == 1)
780 test_t.gr_modify(j, i, adj_s);
781 else
782 assert(0);
784 test_v = cp_rms_error(m, test_t);
786 if (test_v < lowest_error) {
787 lowest_error = test_v;
788 o = test_t;
789 adj_s += 3 * adj_p;
790 is_improved = 1;
794 adj_p /= 2;
800 * Pre-alignment exposure adjustment
803 if (_exp_register) {
804 ui::get()->exposure_1();
805 set_exposure_ratio(m, scale_clusters[0], offset, local_ax_count, 0);
809 * Scale transform for lod
812 for (int lod_ = 0; lod_ < lod; lod_++) {
813 transformation s = offset;
814 transformation t = offset;
816 t.rescale(1 / (double) 2);
818 if (!(t.scaled_height() > 0 && t.scaled_height() < s.scaled_height())
819 || !(t.scaled_width() > 0 && t.scaled_width() < s.scaled_width())) {
820 perturb /= pow(2, lod - lod_);
821 lod = lod_;
822 break;
823 } else {
824 offset = t;
828 ui::get()->set_offset(offset);
830 struct scale_cluster si = scale_clusters[lod];
833 * Projective adjustment value
836 ale_pos adj_p = perturb / pow(2, lod);
839 * Orientational adjustment value in degrees.
841 * Since rotational perturbation is now specified as an
842 * arclength, we have to convert.
845 ale_pos adj_o = (double) 2 * (double) perturb
846 / sqrt(pow((double) scale_clusters[0].input->height(), (double) 2)
847 + pow((double) scale_clusters[0].input->width(), (double) 2))
848 * (double) 180
849 / M_PI;
852 * Barrel distortion adjustment value
855 ale_pos adj_b = perturb * bda_mult;
858 * Global search overlap requirements.
861 local_gs_mo = (double) local_gs_mo / pow(pow(2, lod), 2);
864 * Alignment statistics.
867 diff_stat_t here(offset.elem_bounds());
870 * Current difference (error) value
873 ui::get()->prematching();
874 here.diff(si, offset.get_current_element(), local_ax_count, m);
875 ui::get()->set_match(here.get_error());
878 * Current and modified barrel distortion parameters
881 ale_pos current_bd[BARREL_DEGREE];
882 ale_pos modified_bd[BARREL_DEGREE];
883 offset.bd_get(current_bd);
884 offset.bd_get(modified_bd);
887 * Translational global search step
890 if (perturb >= local_lower && local_gs != 0 && local_gs != 5
891 && (local_gs != 6 || astate->get_is_default(0))) {
893 ui::get()->global_alignment(perturb, lod);
894 ui::get()->gs_mo(local_gs_mo);
896 test_globals(&here, si, offset, local_gs, adj_p,
897 local_ax_count, m, local_gs_mo, perturb);
899 ui::get()->set_match(here.get_error());
900 ui::get()->set_offset(here.get_offset());
904 * Perturbation adjustment loop.
907 offset.set_current_element(here.get_offset());
909 for (unsigned int i = 0; i < offset.stack_depth(); i++) {
911 ui::get()->aligning_element(i, offset.stack_depth());
913 offset.set_current_index(i);
915 ui::get()->start_multi_alignment_element(offset);
917 ui::get()->set_offset(offset);
919 if (i > 0) {
920 astate->init_frame_alignment_nonprimary(&offset, lod, perturb, i);
922 if (_exp_register == 1) {
923 ui::get()->exposure_1();
924 pixel_accum asum(0, 0, 0), bsum(0, 0, 0);
925 exposure_ratio_iterate eri(&asum, &bsum, scale_clusters[0], offset, local_ax_count, 0,
926 offset.elem_bounds().scale_to_bounds(scale_clusters[0].accum->height(),
927 scale_clusters[0].accum->width()));
929 eri.run();
930 pixel_accum tr = asum / bsum;
931 ui::get()->exp_multiplier(tr[0], tr[1], tr[2]);
932 offset.set_tonal_multiplier(tr);
936 int e_lod = lod;
937 int e_div = offset.get_current_coordinate().degree;
938 ale_pos e_perturb = perturb;
939 ale_pos e_adj_p = adj_p;
940 ale_pos e_adj_b = adj_b;
942 for (int d = 0; d < e_div; d++) {
943 e_adj_b = 0;
944 e_perturb *= 0.5;
945 if (e_lod > 0) {
946 e_lod--;
947 } else {
948 e_adj_p *= 0.5;
952 if (i > 0) {
954 d2::trans_multi::elem_bounds_t b = offset.elem_bounds();
956 for (int dim_satisfied = 0; e_lod > 0 && !dim_satisfied; ) {
957 int height = scale_clusters[e_lod].accum->height();
958 int width = scale_clusters[e_lod].accum->width();
960 d2::trans_multi::elem_bounds_int_t bi = b.scale_to_bounds(height, width);
962 dim_satisfied = bi.satisfies_min_dim(min_dimension);
964 if (!dim_satisfied) {
965 e_lod--;
966 e_adj_p *= 2;
971 * Scale transform for lod
974 for (int lod_ = 0; lod_ < e_lod; lod_++) {
975 trans_single s = offset.get_element(i);
976 trans_single t = offset.get_element(i);
978 t.rescale(1 / (double) 2);
980 if (!(t.scaled_height() > 0 && t.scaled_height() < s.scaled_height())
981 || !(t.scaled_width() > 0 && t.scaled_width() < s.scaled_width())) {
982 e_perturb /= pow(2, e_lod - lod_);
983 e_lod = lod_;
984 break;
985 } else {
986 offset.set_element(i, t);
990 ui::get()->set_offset(offset);
994 * Announce perturbation size
997 ui::get()->aligning(e_perturb, e_lod);
999 si = scale_clusters[e_lod];
1001 here.set_elem_bounds(offset.elem_bounds());
1003 here.diff(si, offset.get_current_element(), local_ax_count, m);
1005 here.confirm();
1007 here = check_ancestor_path(offset, si, here, local_ax_count, m);
1009 here = _align_element(e_perturb, local_lower, scale_clusters,
1010 here, e_adj_p, adj_o, e_adj_b, current_bd, modified_bd,
1011 astate, e_lod, si);
1013 offset.rescale(here.get_offset().scale() / offset.scale());
1015 offset.set_current_element(here.get_offset());
1017 if (i > 0 && _exp_register == 1) {
1018 if (ma_cert_satisfied(scale_clusters[0], offset, i)) {
1019 ui::get()->exposure_2();
1020 pixel_accum asum(0, 0, 0), bsum(0, 0, 0);
1021 exposure_ratio_iterate eri(&asum, &bsum, scale_clusters[0], offset, local_ax_count, 1,
1022 offset.elem_bounds().scale_to_bounds(scale_clusters[0].accum->height(),
1023 scale_clusters[0].accum->width()));
1025 eri.run();
1026 pixel_accum tr = asum / bsum;
1027 ui::get()->exp_multiplier(tr[0], tr[1], tr[2]);
1028 offset.set_tonal_multiplier(tr);
1029 } else {
1030 offset.set_tonal_multiplier(offset.get_element(offset.parent_index(i)).get_tonal_multiplier(point(0, 0)));
1032 } else if (_exp_register == 1) {
1033 ui::get()->exposure_2();
1034 set_exposure_ratio(m, scale_clusters[0], offset, local_ax_count, 1);
1037 ui::get()->set_offset(offset);
1039 if (i + 1 == offset.stack_depth())
1040 ui::get()->alignment_degree_complete(offset.get_coordinate(i).degree);
1041 else if (offset.get_coordinate(i).degree != offset.get_coordinate(i + 1).degree)
1042 ui::get()->alignment_degree_complete(offset.get_coordinate(i + 1).degree);
1045 offset.set_current_index(0);
1047 ui::get()->multi();
1048 offset.set_multi(reference_image, input_frame);
1051 * Recalculate error on whole frame.
1054 ui::get()->postmatching();
1055 diff_stat_generic<transformation> multi_here(offset.elem_bounds());
1056 multi_here.diff(scale_clusters[0], offset, local_ax_count, m);
1057 ui::get()->set_match(multi_here.get_error());
1060 * Free the level-of-detail structures
1063 final_clusters(scale_clusters, scale_factor, steps);
1066 * Ensure that the match meets the threshold.
1069 if (threshold_ok(multi_here.get_error())) {
1071 * Update alignment variables
1073 latest_ok = 1;
1074 astate->set_default(offset);
1075 astate->set_final(offset);
1076 ui::get()->alignment_match_ok();
1077 } else if (local_gs == 4) {
1080 * Align with outer starting points.
1084 * XXX: This probably isn't exactly the right thing to do,
1085 * since variables like old_initial_value have been overwritten.
1088 diff_stat_multi nested_result = _align(m, -1, astate);
1090 if (threshold_ok(nested_result.get_error())) {
1091 return nested_result;
1092 } else if (nested_result.get_error() < multi_here.get_error()) {
1093 multi_here = nested_result;
1096 if (is_fail_default)
1097 offset = astate->get_default();
1099 ui::get()->set_match(multi_here.get_error());
1100 ui::get()->alignment_no_match();
1102 } else if (local_gs == -1) {
1104 latest_ok = 0;
1105 latest_t = offset;
1106 return multi_here;
1108 } else {
1109 if (is_fail_default)
1110 offset = astate->get_default();
1111 latest_ok = 0;
1112 ui::get()->alignment_no_match();
1116 * Write the tonal registration multiplier as a comment.
1119 pixel trm = image_rw::exp(m).get_multiplier();
1120 tsave_trm(tsave, trm[0], trm[1], trm[2]);
1123 * Save the transformation information
1126 for (unsigned int index = 0; index < offset.stack_depth(); index++) {
1127 offset.set_current_index(index);
1129 tsave_next(tsave, offset, alignment_class == 2,
1130 offset.get_current_index() == 0);
1133 offset.set_current_index(0);
1136 * Update match statistics.
1139 match_sum += (1 - multi_here.get_error()) * (ale_accum) 100;
1140 match_count++;
1141 latest = m;
1142 latest_t = offset;
1144 return multi_here;
1147 #endif
1149 int ale_align(ale_image a, ale_image b, ale_trans start,
1150 ale_align_properties align_properties, ale_trans result) {
1151 #warning function unfinished.