add mask color radio btn sel, fix del all mask btn, fix mask dflt kfrm draw name...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / overlayframe.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * Copyright (C) 2012 Monty <monty@xiph.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include "clip.h"
24 #include "edl.inc"
25 #include "mutex.h"
26 #include "overlayframe.h"
27 #include "units.h"
28
29 /*
30  * New resampler code; replace the original somehwat blurry engine
31  * with a fairly standard kernel resampling core.  This could be used
32  * for full affine transformation but only implements scale/translate.
33  * Mostly reuses the old blending macro code.
34  *
35  * Pixel convention:
36  *
37  *  1) Pixels are points, not areas or squares.
38  *
39  *  2) To maintain the usual edge and scaling conventions, pixels are
40  *     set inward from the image edge, eg, the left edge of an image is
41  *     at pixel location x=-.5, not x=0.  Although pixels are not
42  *     squares, the usual way of stating this is 'the pixel is located
43  *     at the center of its square'.
44  *
45  *  3) Because of 1 and 2, we must truncate and weight the kernel
46  *     convolution at the edge of the input area.  Otherwise, all
47  *     resampled areas would be bordered by a transparency halo. E.g.
48  *     in the old engine, upsampling HDV to 1920x1080 results in the
49  *     left and right edges being partially transparent and underlying
50  *     layers shining through.
51  *
52  *   4) The contribution of fractional pixels at the edges of input
53  *     ranges are weighted according to the fraction.  Note that the
54  *     kernel weighting is adjusted, not the opacity.  This is one
55  *     exception to 'pixels have no area'.
56  *
57  *  5) The opacity of fractional pixels at the edges of the output
58  *     range is adjusted according to the fraction. This is the other
59  *     exception to 'pixels have no area'.
60  *
61  * Fractional alpha blending has been modified across the board from:
62  *    output_alpha = input_alpha > output_alpha ? input_alpha : output_alpha;
63  *  to:
64  *    output_alpha = output_alpha + ((max - output_alpha) * input_alpha) / max;
65  */
66
67 /* Sinc needed for Lanczos kernel */
68 static float sinc(const float x)
69 {
70         if(fabsf(x) < TRANSFORM_MIN) return 1.0f;
71         float y = x * M_PI;
72         return sinf(y) / y;
73 }
74
75 /*
76  * All resampling (except Nearest Neighbor) is performed via
77  *   transformed 2D resampling kernels bult from 1D lookups.
78  */
79 OverlayKernel::OverlayKernel(int interpolation_type)
80 {
81         int i;
82         this->type = interpolation_type;
83
84         switch(interpolation_type)
85         {
86         case BILINEAR:
87                 width = 1.f;
88                 lookup = new float[(n = TRANSFORM_SPP) + 1];
89                 for (i = 0; i <= TRANSFORM_SPP; i++)
90                         lookup[i] = (float)(TRANSFORM_SPP - i) / TRANSFORM_SPP;
91                 break;
92
93         /* Use a Catmull-Rom filter (not b-spline) */
94         case BICUBIC:
95                 width = 2.;
96                 lookup = new float[(n = 2 * TRANSFORM_SPP) + 1];
97                 for(i = 0; i <= TRANSFORM_SPP; i++) {
98                         float x = i / (float)TRANSFORM_SPP;
99                         lookup[i] = 1.f - 2.5f * x * x + 1.5f * x * x * x;
100                 }
101                 for(; i <= 2 * TRANSFORM_SPP; i++) {
102                         float x = i / (float)TRANSFORM_SPP;
103                         lookup[i] = 2.f - 4.f * x  + 2.5f * x * x - .5f * x * x * x;
104                 }
105                 break;
106
107         case LANCZOS:
108                 width = 3.;
109                 lookup = new float[(n = 3 * TRANSFORM_SPP) + 1];
110                 for (i = 0; i <= 3 * TRANSFORM_SPP; i++)
111                         lookup[i] = sinc((float)i / TRANSFORM_SPP) *
112                                 sinc((float)i / TRANSFORM_SPP / 3.0f);
113                 break;
114
115         default:
116                 width = 0.;
117                 lookup = 0;
118                 n = 0;
119                 break;
120         }
121 }
122
123 OverlayKernel::~OverlayKernel()
124 {
125         delete [] lookup;
126 }
127
128 OverlayFrame::OverlayFrame(int cpus)
129 {
130         direct_engine = 0;
131         nn_engine = 0;
132         sample_engine = 0;
133         temp_frame = 0;
134         memset(kernel, 0, sizeof(kernel));
135         this->cpus = cpus;
136 }
137
138 OverlayFrame::~OverlayFrame()
139 {
140         delete direct_engine;
141         delete nn_engine;
142         delete sample_engine;
143         delete temp_frame;
144         delete kernel[NEAREST_NEIGHBOR];
145         delete kernel[BILINEAR];
146         delete kernel[BICUBIC];
147         delete kernel[LANCZOS];
148 }
149
150 static float epsilon_snap(float f)
151 {
152         return rintf(f * 1024) / 1024.;
153 }
154
155 int OverlayFrame::overlay(VFrame *output, VFrame *input,
156         float in_x1, float in_y1, float in_x2, float in_y2,
157         float out_x1, float out_y1, float out_x2, float out_y2,
158         float alpha, int mode, int interpolation_type)
159 {
160         in_x1 = epsilon_snap(in_x1);
161         in_x2 = epsilon_snap(in_x2);
162         in_y1 = epsilon_snap(in_y1);
163         in_y2 = epsilon_snap(in_y2);
164         out_x1 = epsilon_snap(out_x1);
165         out_x2 = epsilon_snap(out_x2);
166         out_y1 = epsilon_snap(out_y1);
167         out_y2 = epsilon_snap(out_y2);
168
169         if (isnan(in_x1) || isnan(in_x2) ||
170                 isnan(in_y1) || isnan(in_y2) ||
171                 isnan(out_x1) || isnan(out_x2) ||
172                 isnan(out_y1) || isnan(out_y2)) return 1;
173
174         if( in_x2 <= in_x1 || in_y2 <= in_y1 ) return 1;
175         if( out_x2 <= out_x1 || out_y2 <= out_y1 ) return 1;
176
177         float xscale = (out_x2 - out_x1) / (in_x2 - in_x1);
178         float yscale = (out_y2 - out_y1) / (in_y2 - in_y1);
179         int in_w = input->get_w(), in_h = input->get_h();
180         int out_w = output->get_w(), out_h = output->get_h();
181
182         if( in_x1 < 0 ) {
183                 out_x1 -= in_x1 * xscale;
184                 in_x1 = 0;
185         }
186         if( in_x2 > in_w ) {
187                 out_x2 -= (in_x2 - in_w) * xscale;
188                 in_x2 = in_w;
189         }
190         if( in_y1 < 0 ) {
191                 out_y1 -= in_y1 * yscale;
192                 in_y1 = 0;
193         }
194         if( in_y2 > in_h ) {
195                 out_y2 -= (in_y2 - in_h) * yscale;
196                 in_y2 = in_h;
197         }
198
199         if( out_x1 < 0 ) {
200                 in_x1 -= out_x1 / xscale;
201                 out_x1 = 0;
202         }
203         if( out_x2 > out_w ) {
204                 in_x2 -= (out_x2 - out_w) / xscale;
205                 out_x2 = out_w;
206         }
207         if( out_y1 < 0 ) {
208                 in_y1 -= out_y1 / yscale;
209                 out_y1 = 0;
210         }
211         if( out_y2 > out_h ) {
212                 in_y2 -= (out_y2 - out_h) / yscale;
213                 out_y2 = out_h;
214         }
215
216         if( in_x1 < 0) in_x1 = 0;
217         if( in_y1 < 0) in_y1 = 0;
218         if( in_x2 > in_w ) in_x2 = in_w;
219         if( in_y2 > in_h ) in_y2 = in_h;
220         if( out_x1 < 0) out_x1 = 0;
221         if( out_y1 < 0) out_y1 = 0;
222         if( out_x2 > out_w ) out_x2 = out_w;
223         if( out_y2 > out_h ) out_y2 = out_h;
224
225         if( in_x2 <= in_x1 || in_y2 <= in_y1 ) return 1;
226         if( out_x2 <= out_x1 || out_y2 <= out_y1 ) return 1;
227         xscale = (out_x2 - out_x1) / (in_x2 - in_x1);
228         yscale = (out_y2 - out_y1) / (in_y2 - in_y1);
229
230         /* don't interpolate integer translations, or scale no-ops */
231         if(xscale == 1. && yscale == 1. &&
232                 (int)in_x1 == in_x1 && (int)in_x2 == in_x2 &&
233                 (int)in_y1 == in_y1 && (int)in_y2 == in_y2 &&
234                 (int)out_x1 == out_x1 && (int)out_x2 == out_x2 &&
235                 (int)out_y1 == out_y1 && (int)out_y2 == out_y2) {
236                 if(!direct_engine) direct_engine = new DirectEngine(cpus);
237
238                 direct_engine->output = output;   direct_engine->input = input;
239                 direct_engine->in_x1 = in_x1;     direct_engine->in_y1 = in_y1;
240                 direct_engine->out_x1 = out_x1;   direct_engine->out_x2 = out_x2;
241                 direct_engine->out_y1 = out_y1;   direct_engine->out_y2 = out_y2;
242                 direct_engine->alpha = alpha;     direct_engine->mode = mode;
243                 direct_engine->process_packages();
244         }
245         else if(interpolation_type == NEAREST_NEIGHBOR) {
246                 if(!nn_engine) nn_engine = new NNEngine(cpus);
247                 nn_engine->output = output;       nn_engine->input = input;
248                 nn_engine->in_x1 = in_x1;         nn_engine->in_x2 = in_x2;
249                 nn_engine->in_y1 = in_y1;         nn_engine->in_y2 = in_y2;
250                 nn_engine->out_x1 = out_x1;       nn_engine->out_x2 = out_x2;
251                 nn_engine->out_y1 = out_y1;       nn_engine->out_y2 = out_y2;
252                 nn_engine->alpha = alpha;         nn_engine->mode = mode;
253                 nn_engine->process_packages();
254         }
255         else {
256                 int xtype = BILINEAR;
257                 int ytype = BILINEAR;
258
259                 switch(interpolation_type)
260                 {
261                 case CUBIC_CUBIC: // Bicubic enlargement and reduction
262                         xtype = ytype = BICUBIC;
263                         break;
264                 case CUBIC_LINEAR: // Bicubic enlargement and bilinear reduction
265                         xtype = xscale > 1. ? BICUBIC : BILINEAR;
266                         ytype = yscale > 1. ? BICUBIC : BILINEAR;
267                         break;
268                 case LINEAR_LINEAR: // Bilinear enlargement and bilinear reduction
269                         xtype = ytype = BILINEAR;
270                         break;
271                 case LANCZOS_LANCZOS: // Because we can
272                         xtype = ytype = LANCZOS;
273                         break;
274                 }
275
276                 if(xscale == 1. && (int)in_x1 == in_x1 && (int)in_x2 == in_x2 &&
277                                 (int)out_x1 == out_x1 && (int)out_x2 == out_x2)
278                         xtype = DIRECT_COPY;
279
280                 if(yscale == 1. && (int)in_y1 == in_y1 && (int)in_y2 == in_y2 &&
281                                 (int)out_y1 == out_y1 && (int)out_y2 == out_y2)
282                         ytype = DIRECT_COPY;
283
284                 if(!kernel[xtype])
285                         kernel[xtype] = new OverlayKernel(xtype);
286                 if(!kernel[ytype])
287                         kernel[ytype] = new OverlayKernel(ytype);
288
289 /*
290  * horizontal and vertical are separately resampled.  First we
291  * resample the input along X into a transposed, temporary frame,
292  * then resample/transpose the temporary space along X into the
293  * output.  Fractional pixels along the edge are handled in the X
294  * direction of each step
295  */
296                 // resampled dimension matches the transposed output space
297                 float temp_y1 = out_x1 - floor(out_x1);
298                 float temp_y2 = temp_y1 + (out_x2 - out_x1);
299                 int temp_h = ceil(temp_y2);
300
301                 // non-resampled dimension merely cropped
302                 float temp_x1 = in_y1 - floor(in_y1);
303                 float temp_x2 = temp_x1 + (in_y2 - in_y1);
304                 int temp_w = ceil(temp_x2);
305
306                 if( temp_frame &&
307                    (temp_frame->get_color_model() != input->get_color_model() ||
308                     temp_frame->get_w() != temp_w || temp_frame->get_h() != temp_h) ) {
309                         delete temp_frame;
310                         temp_frame = 0;
311                 }
312
313                 if(!temp_frame) {
314                         temp_frame =
315                                 new VFrame(temp_w, temp_h, input->get_color_model(), 0);
316                 }
317
318                 temp_frame->clear_frame();
319
320                 if(!sample_engine) sample_engine = new SampleEngine(cpus);
321
322                 sample_engine->output = temp_frame;
323                 sample_engine->input = input;
324                 sample_engine->kernel = kernel[xtype];
325                 sample_engine->col_out1 = 0;
326                 sample_engine->col_out2 = temp_w;
327                 sample_engine->row_in = floor(in_y1);
328
329                 sample_engine->in1 = in_x1;
330                 sample_engine->in2 = in_x2;
331                 sample_engine->out1 = temp_y1;
332                 sample_engine->out2 = temp_y2;
333                 sample_engine->alpha = 1.;
334                 sample_engine->mode = TRANSFER_REPLACE;
335                 sample_engine->process_packages();
336
337                 sample_engine->output = output;
338                 sample_engine->input = temp_frame;
339                 sample_engine->kernel = kernel[ytype];
340                 sample_engine->col_out1 = floor(out_x1);
341                 sample_engine->col_out2 = ceil(out_x2);
342                 sample_engine->row_in = 0;
343
344                 sample_engine->in1 = temp_x1;
345                 sample_engine->in2 = temp_x2;
346                 sample_engine->out1 = out_y1;
347                 sample_engine->out2 = out_y2;
348                 sample_engine->alpha = alpha;
349                 sample_engine->mode = mode;
350                 sample_engine->process_packages();
351         }
352         return 0;
353 }
354
355