4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
26 #include "overlayframe.h"
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.
37 * 1) Pixels are points, not areas or squares.
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'.
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.
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'.
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'.
61 * Fractional alpha blending has been modified across the board from:
62 * output_alpha = input_alpha > output_alpha ? input_alpha : output_alpha;
64 * output_alpha = output_alpha + ((max - output_alpha) * input_alpha) / max;
67 /* Sinc needed for Lanczos kernel */
68 static float sinc(const float x)
70 if(fabsf(x) < TRANSFORM_MIN) return 1.0f;
76 * All resampling (except Nearest Neighbor) is performed via
77 * transformed 2D resampling kernels bult from 1D lookups.
79 OverlayKernel::OverlayKernel(int interpolation_type)
82 this->type = interpolation_type;
84 switch(interpolation_type)
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;
93 /* Use a Catmull-Rom filter (not b-spline) */
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;
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;
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);
123 OverlayKernel::~OverlayKernel()
128 OverlayFrame::OverlayFrame(int cpus)
134 memset(kernel, 0, sizeof(kernel));
138 OverlayFrame::~OverlayFrame()
140 delete direct_engine;
142 delete sample_engine;
144 delete kernel[NEAREST_NEIGHBOR];
145 delete kernel[BILINEAR];
146 delete kernel[BICUBIC];
147 delete kernel[LANCZOS];
150 static float epsilon_snap(float f)
152 return rintf(f * 1024) / 1024.;
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)
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);
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;
174 if( in_x2 <= in_x1 || in_y2 <= in_y1 ) return 1;
175 if( out_x2 <= out_x1 || out_y2 <= out_y1 ) return 1;
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();
183 out_x1 -= in_x1 * xscale;
187 out_x2 -= (in_x2 - in_w) * xscale;
191 out_y1 -= in_y1 * yscale;
195 out_y2 -= (in_y2 - in_h) * yscale;
200 in_x1 -= out_x1 / xscale;
203 if( out_x2 > out_w ) {
204 in_x2 -= (out_x2 - out_w) / xscale;
208 in_y1 -= out_y1 / yscale;
211 if( out_y2 > out_h ) {
212 in_y2 -= (out_y2 - out_h) / yscale;
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;
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);
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);
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();
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();
256 int xtype = BILINEAR;
257 int ytype = BILINEAR;
259 switch(interpolation_type)
261 case CUBIC_CUBIC: // Bicubic enlargement and reduction
262 xtype = ytype = BICUBIC;
264 case CUBIC_LINEAR: // Bicubic enlargement and bilinear reduction
265 xtype = xscale > 1. ? BICUBIC : BILINEAR;
266 ytype = yscale > 1. ? BICUBIC : BILINEAR;
268 case LINEAR_LINEAR: // Bilinear enlargement and bilinear reduction
269 xtype = ytype = BILINEAR;
271 case LANCZOS_LANCZOS: // Because we can
272 xtype = ytype = LANCZOS;
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)
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)
285 kernel[xtype] = new OverlayKernel(xtype);
287 kernel[ytype] = new OverlayKernel(ytype);
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
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);
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);
307 (temp_frame->get_color_model() != input->get_color_model() ||
308 temp_frame->get_w() != temp_w || temp_frame->get_h() != temp_h) ) {
315 new VFrame(temp_w, temp_h, input->get_color_model(), 0);
318 temp_frame->clear_frame();
320 if(!sample_engine) sample_engine = new SampleEngine(cpus);
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);
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();
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;
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();