1 /*
2  * Brute force align the bands of an image.
3  *
4  * Copyright: Nottingham Trent University
5  * Author: Tom Vajzovic
6  * Written on: 2008-02-04
7  */
8 
9 /*
10 
11     This file is part of VIPS.
12 
13     VIPS is free software; you can redistribute it and/or modify
14     it under the terms of the GNU Lesser General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17 
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU Lesser General Public License for more details.
22 
23     You should have received a copy of the GNU Lesser General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26     02110-1301  USA
27 
28  */
29 
30 /*
31 
32     These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
33 
34  */
35 
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif /*HAVE_CONFIG_H*/
39 
40 #include <vips/intl.h>
41 #include <vips/vips.h>
42 #include <vips/vips7compat.h>
43 
44 /**
45  * im_align_bands:
46  * @in: image to align
47  * @out: output image
48  *
49  * This operation uses im_phasecor_fft() to find an integer displacement to
50  * align all image bands band 0. It is very slow and not very accurate.
51  *
52  * Use im_estpar() in preference: it's fast and accurate.
53  *
54  * See also: im_global_balancef(), im_remosaic().
55  *
56  * Returns: 0 on success, -1 on error
57  */
im_align_bands(IMAGE * in,IMAGE * out)58 int im_align_bands( IMAGE *in, IMAGE *out ){
59 #define FUNCTION_NAME "im_align_bands"
60   if( im_piocheck( in, out ))
61     return -1;
62 
63   if( 1 == in-> Bands )
64     return im_copy( in, out );
65   {
66     IMAGE **bands= IM_ARRAY( out, 2 * in-> Bands, IMAGE* );
67     IMAGE **wrapped_bands= bands + in-> Bands;
68     double x= 0.0;
69     double y= 0.0;
70     int i;
71 
72     if( ! bands || im_open_local_array( out, bands, in-> Bands, FUNCTION_NAME ": bands", "p" )
73         || im_open_local_array( out, wrapped_bands + 1, in-> Bands - 1, FUNCTION_NAME ": wrapped_bands", "p" ))
74       return -1;
75 
76     for( i= 0; i < in-> Bands; ++i )
77       if( im_extract_band( in, bands[i], i ))
78         return -1;
79 
80     wrapped_bands[ 0 ]= bands[0];
81 
82     for( i= 1; i < in-> Bands; ++i ){
83       IMAGE *temp= im_open( FUNCTION_NAME ": temp", "t" );
84       double this_x, this_y, val;
85 
86       if( ! temp || im_phasecor_fft( bands[i-1], bands[i], temp )
87           || im_maxpos_avg( temp, & this_x, & this_y, & val ) || im_close( temp ))
88         return -1;
89 
90       x+= this_x;
91       y+= this_y;
92 
93       if( im_wrap( bands[i], wrapped_bands[i], (int) x, (int) y ))
94         return -1;
95     }
96     return im_gbandjoin( wrapped_bands, out, in-> Bands );
97   }
98 #undef FUNCTION_NAME
99 }
100