1 /* Local definitions used by the mosaicing program
2  * If VIPS_MAXPOINTS change please ensure that it is still a multiple of
3  * AREAS or else AREAS must change as well.  Initial setup is for
4  * VIPS_MAXPOINTS = 60, AREAS = 3.
5  *
6  * Copyright: 1990, 1991 N. Dessipris
7  * Author: Nicos Dessipris
8  * Written on: 07/11/1989
9  * Modified on : 29/11/1989
10  */
11 
12 /*
13 
14     Copyright (C) 1991-2003 The National Gallery
15 
16     This program is free software; you can redistribute it and/or modify
17     it under the terms of the GNU Lesser General Public License as published by
18     the Free Software Foundation; either version 2 of the License, or
19     (at your option) any later version.
20 
21     This program is distributed in the hope that it will be useful,
22     but WITHOUT ANY WARRANTY; without even the implied warranty of
23     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24     GNU Lesser General Public License for more details.
25 
26     You should have received a copy of the GNU Lesser General Public License
27     along with this program; if not, write to the Free Software
28     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29     02110-1301  USA
30 
31  */
32 
33 /*
34 
35     These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
36 
37  */
38 
39 /* TODO(kleisauke): This import is needed for vips__affinei */
40 #include <vips/transform.h>
41 
42 /* Number of entries in blend table. As a power of two as well, for >>ing.
43  */
44 #define BLEND_SHIFT (10)
45 #define BLEND_SIZE (1<<BLEND_SHIFT)
46 
47 /* How much we scale the int version up by.
48  */
49 #define BLEND_SCALE (4096)
50 
51 struct _MergeInfo;
52 struct _Overlapping;
53 
54 typedef int (*VipsBlendFn)( VipsRegion *or,
55 	struct _MergeInfo *inf, struct _Overlapping *ovlap, VipsRect *oreg );
56 
57 /* Keep state for each call in one of these.
58  */
59 typedef struct _Overlapping {
60 	VipsImage *ref;			/* Arguments */
61 	VipsImage *sec;
62 	VipsImage *out;
63 	int dx, dy;
64 	int mwidth;
65 
66 	/* Ref and sec images, overlap, output area. We normalise these, so
67 	 * that the output image is always positioned at (0,0) - ie. all these
68 	 * coordinates are in output image space.
69 	 */
70 	VipsRect rarea;
71 	VipsRect sarea;
72 	VipsRect overlap;
73 	VipsRect oarea;
74 	int blsize;			/* Max blend length */
75 	int flsize;			/* first/last cache size */
76 
77 	/* Sections of ref and sec which we use in output, excluding
78 	 * overlap area.
79 	 */
80 	VipsRect rpart;
81 	VipsRect spart;
82 
83 	/* Overlap start/end cache
84 	 */
85 	GMutex *fl_lock;		/* Need to lock on build */
86 	int *first, *last;
87 
88 	/* Blend function.
89 	 */
90 	VipsBlendFn blend;
91 } Overlapping;
92 
93 /* Keep per-thread state here.
94  */
95 typedef struct _MergeInfo {
96 	VipsRegion *rir;			/* Two input regions */
97 	VipsRegion *sir;
98 
99 	float *from1;			/* VIPS_CODING_LABQ buffers */
100 	float *from2;
101 	float *merge;
102 } MergeInfo;
103 
104 /* Functions shared between lr and tb.
105  */
106 extern double *vips__coef1;
107 extern double *vips__coef2;
108 extern int *vips__icoef1;
109 extern int *vips__icoef2;
110 int vips__make_blend_luts( void );
111 
112 void vips__add_mosaic_name( VipsImage *image );
113 const char *vips__get_mosaic_name( VipsImage *image );
114 
115 int vips__affinei( VipsImage *in, VipsImage *out, VipsTransformation *trn );
116 
117 int vips__attach_input( VipsRegion *or, VipsRegion *ir, VipsRect *area );
118 int vips__copy_input( VipsRegion *or, VipsRegion *ir, VipsRect *area, VipsRect *reg );
119 Overlapping *vips__build_mergestate( const char *domain,
120 	VipsImage *ref, VipsImage *sec, VipsImage *out, int dx, int dy, int mwidth );
121 void *vips__start_merge( VipsImage *out, void *, void * );
122 int vips__merge_gen( VipsRegion *or, void *seq, void *a, void *,
123 	gboolean *stop );
124 int vips__stop_merge( void *seq, void *, void * );
125 
126 int vips__lrmerge( VipsImage *ref, VipsImage *sec, VipsImage *out,
127 	int dx, int dy, int mwidth );
128 int vips__tbmerge( VipsImage *ref, VipsImage *sec, VipsImage *out,
129 	int dx, int dy, int mwidth );
130 
131 int vips__lrmerge1( VipsImage *ref, VipsImage *sec, VipsImage *out,
132 	double a, double b, double dx, double dy,
133 	int mwidth );
134 int vips__tbmerge1( VipsImage *ref, VipsImage *sec, VipsImage *out,
135 	double a, double b, double dx, double dy,
136 	int mwidth );
137 
138 #define VIPS_MAXPOINTS (60)	/* VIPS_MAXPOINTS % AREAS must be zero */
139 #define AREAS (3)
140 
141 typedef struct {
142 	char *reference;	/* filename of reference */
143 	char *secondary;	/* filename of secondary */
144 	int deltax;		/* initial estimate of displacement */
145 	int deltay;		/* initial estimate of displacement */
146 	int nopoints;   	/* must be multiple of AREAS and <= VIPS_MAXPOINTS */
147 	int halfcorsize;	/* recommended 5 */
148 	int halfareasize;	/* recommended 8 */
149 
150 	/* x, y_reference and contrast found by vips_calcon()
151 	 */
152 	int x_reference[VIPS_MAXPOINTS], y_reference[VIPS_MAXPOINTS];
153 	int contrast[VIPS_MAXPOINTS];
154 
155 	/* x, y_secondary and correlation set by vips_chkpair()
156 	 */
157 	int x_secondary[VIPS_MAXPOINTS], y_secondary[VIPS_MAXPOINTS];
158 
159 	/* returns the corrected best correlation
160 	 * as detected in 2*halfareasize+1
161 	 * centered at point (x2, y2) and using
162 	 * correlation area 2*halfareasize+1
163 	 */
164 	double correlation[VIPS_MAXPOINTS];
165 
166 	/* Coefficients calculated by vips_clinear()
167 	 */
168 	double l_scale, l_angle, l_deltax, l_deltay;
169 
170 	/* used by vips_clinear()
171 	 */
172 	double dx[VIPS_MAXPOINTS], dy[VIPS_MAXPOINTS];
173 	double deviation[VIPS_MAXPOINTS];
174 } TiePoints;
175 
176 int vips__chkpair( VipsImage *, VipsImage *, TiePoints *point );
177 int vips__initialize( TiePoints *points );
178 int vips__improve( TiePoints *inpoints, TiePoints *outpoints );
179 int vips__avgdxdy( TiePoints *points, int *dx, int *dy );
180 int vips__lrcalcon( VipsImage *ref, TiePoints *points );
181 int vips__tbcalcon( VipsImage *ref, TiePoints *points );
182 int vips__coeff( int xr1, int yr1, int xs1, int ys1,
183 	int xr2, int yr2, int xs2, int ys2,
184 	double *a, double *b, double *dx, double *dy );
185 int vips__clinear( TiePoints *points );
186 int vips__find_lroverlap( VipsImage *ref_in, VipsImage *sec_in, VipsImage *out,
187 	int bandno_in,
188 	int xref, int yref, int xsec, int ysec,
189 	int halfcorrelation, int halfarea,
190 	int *dx0, int *dy0,
191 	double *scale1, double *angle1, double *dx1, double *dy1 );
192 int vips__find_tboverlap( VipsImage *ref_in, VipsImage *sec_in, VipsImage *out,
193 	int bandno_in,
194 	int xref, int yref, int xsec, int ysec,
195 	int halfcorrelation, int halfarea,
196 	int *dx0, int *dy0,
197 	double *scale1, double *angle1, double *dx1, double *dy1 );
198 int vips__find_best_contrast( VipsImage *image,
199 	int xpos, int ypos, int xsize, int ysize,
200 	int xarray[], int yarray[], int cont[],
201 	int nbest, int hcorsize );
202