4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5 * Copyright (C) 2012 Monty <monty@xiph.org>
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
33 #include "overlayframe.h"
37 static inline int mabs(int32_t v) { return abs(v); }
38 static inline int mabs(int64_t v) { return llabs(v); }
39 static inline float mabs(float v) { return fabsf(v); }
41 static inline int32_t aclip(int32_t v, int mx) {
42 return v < 0 ? 0 : v > mx ? mx : v;
44 static inline int64_t aclip(int64_t v, int mx) {
45 return v < 0 ? 0 : v > mx ? mx : v;
47 static inline float aclip(float v, float mx) {
48 return v < 0 ? 0 : v > mx ? mx : v;
50 static inline float aclip(float v, int mx) {
51 return v < 0 ? 0 : v > mx ? mx : v;
53 static inline int aclip(int v, float mx) {
54 return v < 0 ? 0 : v > mx ? mx : v;
56 static inline int32_t cclip(int32_t v, int mx) {
57 return v > (mx/=2) ? mx : v < (mx=(-mx-1)) ? mx : v;
59 static inline int64_t cclip(int64_t v, int mx) {
60 return v > (mx/=2) ? mx : v < (mx=(-mx-1)) ? mx : v;
62 static inline float cclip(float v, float mx) {
63 return v > (mx/=2) ? mx : v < (mx=(-mx)) ? mx : v;
65 static inline float cclip(float v, int mx) {
66 return v > (mx/=2) ? mx : v < (mx=(-mx-1)) ? mx : v;
68 static inline int cclip(int v, float mx) {
69 return v > (mx/=2) ? mx : v < (mx=(-mx-1)) ? mx : v;
73 * New resampler code; replace the original somehwat blurry engine
74 * with a fairly standard kernel resampling core. This could be used
75 * for full affine transformation but only implements scale/translate.
76 * Mostly reuses the old blending macro code.
80 * 1) Pixels are points, not areas or squares.
82 * 2) To maintain the usual edge and scaling conventions, pixels are
83 * set inward from the image edge, eg, the left edge of an image is
84 * at pixel location x=-.5, not x=0. Although pixels are not
85 * squares, the usual way of stating this is 'the pixel is located
86 * at the center of its square'.
88 * 3) Because of 1 and 2, we must truncate and weight the kernel
89 * convolution at the edge of the input area. Otherwise, all
90 * resampled areas would be bordered by a transparency halo. E.g.
91 * in the old engine, upsampling HDV to 1920x1080 results in the
92 * left and right edges being partially transparent and underlying
93 * layers shining through.
95 * 4) The contribution of fractional pixels at the edges of input
96 * ranges are weighted according to the fraction. Note that the
97 * kernel weighting is adjusted, not the opacity. This is one
98 * exception to 'pixels have no area'.
100 * 5) The opacity of fractional pixels at the edges of the output
101 * range is adjusted according to the fraction. This is the other
102 * exception to 'pixels have no area'.
104 * Fractional alpha blending has been modified across the board from:
105 * output_alpha = input_alpha > output_alpha ? input_alpha : output_alpha;
107 * output_alpha = output_alpha + ((max - output_alpha) * input_alpha) / max;
110 #define TRANSFORM_SPP (4096) /* number of data pts per unit x in lookup table */
111 #define INDEX_FRACTION (8) /* bits of fraction past TRANSFORM_SPP on kernel
112 index accumulation */
113 #define TRANSFORM_MIN (.5 / TRANSFORM_SPP)
115 /* Sinc needed for Lanczos kernel */
116 static float sinc(const float x)
120 if(fabsf(x) < TRANSFORM_MIN)
127 * All resampling (except Nearest Neighbor) is performed via
128 * transformed 2D resampling kernels bult from 1D lookups.
130 OverlayKernel::OverlayKernel(int interpolation_type)
133 this->type = interpolation_type;
135 switch(interpolation_type)
139 lookup = new float[(n = TRANSFORM_SPP) + 1];
140 for (i = 0; i <= TRANSFORM_SPP; i++)
141 lookup[i] = (float)(TRANSFORM_SPP - i) / TRANSFORM_SPP;
144 /* Use a Catmull-Rom filter (not b-spline) */
147 lookup = new float[(n = 2 * TRANSFORM_SPP) + 1];
148 for(i = 0; i <= TRANSFORM_SPP; i++) {
149 float x = i / (float)TRANSFORM_SPP;
150 lookup[i] = 1.f - 2.5f * x * x + 1.5f * x * x * x;
152 for(; i <= 2 * TRANSFORM_SPP; i++) {
153 float x = i / (float)TRANSFORM_SPP;
154 lookup[i] = 2.f - 4.f * x + 2.5f * x * x - .5f * x * x * x;
160 lookup = new float[(n = 3 * TRANSFORM_SPP) + 1];
161 for (i = 0; i <= 3 * TRANSFORM_SPP; i++)
162 lookup[i] = sinc((float)i / TRANSFORM_SPP) *
163 sinc((float)i / TRANSFORM_SPP / 3.0f);
174 OverlayKernel::~OverlayKernel()
176 if(lookup) delete [] lookup;
179 OverlayFrame::OverlayFrame(int cpus)
185 memset(kernel, 0, sizeof(kernel));
189 OverlayFrame::~OverlayFrame()
191 if(temp_frame) delete temp_frame;
193 if(direct_engine) delete direct_engine;
194 if(nn_engine) delete nn_engine;
195 if(sample_engine) delete sample_engine;
197 if(kernel[NEAREST_NEIGHBOR]) delete kernel[NEAREST_NEIGHBOR];
198 if(kernel[BILINEAR]) delete kernel[BILINEAR];
199 if(kernel[BICUBIC]) delete kernel[BICUBIC];
200 if(kernel[LANCZOS]) delete kernel[LANCZOS];
203 static float epsilon_snap(float f)
205 return rintf(f * 1024) / 1024.;
208 int OverlayFrame::overlay(VFrame *output, VFrame *input,
209 float in_x1, float in_y1, float in_x2, float in_y2,
210 float out_x1, float out_y1, float out_x2, float out_y2,
211 float alpha, int mode, int interpolation_type)
213 in_x1 = epsilon_snap(in_x1);
214 in_x2 = epsilon_snap(in_x2);
215 in_y1 = epsilon_snap(in_y1);
216 in_y2 = epsilon_snap(in_y2);
217 out_x1 = epsilon_snap(out_x1);
218 out_x2 = epsilon_snap(out_x2);
219 out_y1 = epsilon_snap(out_y1);
220 out_y2 = epsilon_snap(out_y2);
222 if (isnan(in_x1) || isnan(in_x2) ||
223 isnan(in_y1) || isnan(in_y2) ||
224 isnan(out_x1) || isnan(out_x2) ||
225 isnan(out_y1) || isnan(out_y2)) return 1;
227 if(in_x1 < 0) in_x1 = 0;
228 if(in_y1 < 0) in_y1 = 0;
229 if(in_x2 > input->get_w()) in_x2 = input->get_w();
230 if(in_y2 > input->get_h()) in_y2 = input->get_h();
231 if(out_x1 < 0) out_x1 = 0;
232 if(out_y1 < 0) out_y1 = 0;
233 if(out_x2 > output->get_w()) out_x2 = output->get_w();
234 if(out_y2 > output->get_h()) out_y2 = output->get_h();
236 float xscale = (out_x2 - out_x1) / (in_x2 - in_x1);
237 float yscale = (out_y2 - out_y1) / (in_y2 - in_y1);
239 /* don't interpolate integer translations, or scale no-ops */
240 if(xscale == 1. && yscale == 1. &&
241 (int)in_x1 == in_x1 && (int)in_x2 == in_x2 &&
242 (int)in_y1 == in_y1 && (int)in_y2 == in_y2 &&
243 (int)out_x1 == out_x1 && (int)out_x2 == out_x2 &&
244 (int)out_y1 == out_y1 && (int)out_y2 == out_y2) {
245 if(!direct_engine) direct_engine = new DirectEngine(cpus);
247 direct_engine->output = output; direct_engine->input = input;
248 direct_engine->in_x1 = in_x1; direct_engine->in_y1 = in_y1;
249 direct_engine->out_x1 = out_x1; direct_engine->out_x2 = out_x2;
250 direct_engine->out_y1 = out_y1; direct_engine->out_y2 = out_y2;
251 direct_engine->alpha = alpha; direct_engine->mode = mode;
252 direct_engine->process_packages();
254 else if(interpolation_type == NEAREST_NEIGHBOR) {
255 if(!nn_engine) nn_engine = new NNEngine(cpus);
256 nn_engine->output = output; nn_engine->input = input;
257 nn_engine->in_x1 = in_x1; nn_engine->in_x2 = in_x2;
258 nn_engine->in_y1 = in_y1; nn_engine->in_y2 = in_y2;
259 nn_engine->out_x1 = out_x1; nn_engine->out_x2 = out_x2;
260 nn_engine->out_y1 = out_y1; nn_engine->out_y2 = out_y2;
261 nn_engine->alpha = alpha; nn_engine->mode = mode;
262 nn_engine->process_packages();
265 int xtype = BILINEAR;
266 int ytype = BILINEAR;
268 switch(interpolation_type)
270 case CUBIC_CUBIC: // Bicubic enlargement and reduction
271 xtype = ytype = BICUBIC;
273 case CUBIC_LINEAR: // Bicubic enlargement and bilinear reduction
274 xtype = xscale > 1. ? BICUBIC : BILINEAR;
275 ytype = yscale > 1. ? BICUBIC : BILINEAR;
277 case LINEAR_LINEAR: // Bilinear enlargement and bilinear reduction
278 xtype = ytype = BILINEAR;
280 case LANCZOS_LANCZOS: // Because we can
281 xtype = ytype = LANCZOS;
285 if(xscale == 1. && (int)in_x1 == in_x1 && (int)in_x2 == in_x2 &&
286 (int)out_x1 == out_x1 && (int)out_x2 == out_x2)
289 if(yscale == 1. && (int)in_y1 == in_y1 && (int)in_y2 == in_y2 &&
290 (int)out_y1 == out_y1 && (int)out_y2 == out_y2)
294 kernel[xtype] = new OverlayKernel(xtype);
296 kernel[ytype] = new OverlayKernel(ytype);
299 * horizontal and vertical are separately resampled. First we
300 * resample the input along X into a transposed, temporary frame,
301 * then resample/transpose the temporary space along X into the
302 * output. Fractional pixels along the edge are handled in the X
303 * direction of each step
305 // resampled dimension matches the transposed output space
306 float temp_y1 = out_x1 - floor(out_x1);
307 float temp_y2 = temp_y1 + (out_x2 - out_x1);
308 int temp_h = ceil(temp_y2);
310 // non-resampled dimension merely cropped
311 float temp_x1 = in_y1 - floor(in_y1);
312 float temp_x2 = temp_x1 + (in_y2 - in_y1);
313 int temp_w = ceil(temp_x2);
316 (temp_frame->get_color_model() != input->get_color_model() ||
317 temp_frame->get_w() != temp_w || temp_frame->get_h() != temp_h) ) {
323 temp_frame = new VFrame(0, -1, temp_w, temp_h,
324 input->get_color_model(), -1);
327 temp_frame->clear_frame();
329 if(!sample_engine) sample_engine = new SampleEngine(cpus);
331 sample_engine->output = temp_frame;
332 sample_engine->input = input;
333 sample_engine->kernel = kernel[xtype];
334 sample_engine->col_out1 = 0;
335 sample_engine->col_out2 = temp_w;
336 sample_engine->row_in = floor(in_y1);
338 sample_engine->in1 = in_x1;
339 sample_engine->in2 = in_x2;
340 sample_engine->out1 = temp_y1;
341 sample_engine->out2 = temp_y2;
342 sample_engine->alpha = 1.;
343 sample_engine->mode = TRANSFER_REPLACE;
344 sample_engine->process_packages();
346 sample_engine->output = output;
347 sample_engine->input = temp_frame;
348 sample_engine->kernel = kernel[ytype];
349 sample_engine->col_out1 = floor(out_x1);
350 sample_engine->col_out2 = ceil(out_x2);
351 sample_engine->row_in = 0;
353 sample_engine->in1 = temp_x1;
354 sample_engine->in2 = temp_x2;
355 sample_engine->out1 = out_y1;
356 sample_engine->out2 = out_y2;
357 sample_engine->alpha = alpha;
358 sample_engine->mode = mode;
359 sample_engine->process_packages();
364 // NORMAL [Sa * Sa + Da * (1 - Sa), Sc * Sa + Dc * (1 - Sa)])
365 #define ALPHA_NORMAL(mx, Sa, Da) ((Sa * Sa + Da * (mx - Sa)) / mx)
366 #define COLOR_NORMAL(mx, Sc, Sa, Dc, Da) ((Sc * Sa + Dc * (mx - Sa)) / mx)
367 #define CHROMA_NORMAL COLOR_NORMAL
369 // ADDITION [(Sa + Da), (Sc + Dc)]
370 #define ALPHA_ADDITION(mx, Sa, Da) (Sa + Da)
371 #define COLOR_ADDITION(mx, Sc, Sa, Dc, Da) (Sc + Dc)
372 #define CHROMA_ADDITION(mx, Sc, Sa, Dc, Da) (Sc + Dc)
374 // SUBTRACT [(Sa - Da), (Sc - Dc)]
375 #define ALPHA_SUBTRACT(mx, Sa, Da) (Sa - Da)
376 #define COLOR_SUBTRACT(mx, Sc, Sa, Dc, Da) (Sc - Dc)
377 #define CHROMA_SUBTRACT(mx, Sc, Sa, Dc, Da) (Sc - Dc)
379 // MULTIPLY [(Sa * Da), Sc * Dc]
380 #define ALPHA_MULTIPLY(mx, Sa, Da) ((Sa * Da) / mx)
381 #define COLOR_MULTIPLY(mx, Sc, Sa, Dc, Da) ((Sc * Dc) / mx)
382 #define CHROMA_MULTIPLY(mx, Sc, Sa, Dc, Da) ((Sc * Dc) / mx)
384 // DIVIDE [(Sa / Da), (Sc / Dc)]
385 #define ALPHA_DIVIDE(mx, Sa, Da) (Da ? ((Sa * mx) / Da) : mx)
386 #define COLOR_DIVIDE(mx, Sc, Sa, Dc, Da) (Dc ? ((Sc * mx) / Dc) : mx)
387 #define CHROMA_DIVIDE(mx, Sc, Sa, Dc, Da) (Dc ? ((Sc * mx) / Dc) : mx)
389 // REPLACE [Sa, Sc] (fade = 1)
390 #define ALPHA_REPLACE(mx, Sa, Da) Sa
391 #define COLOR_REPLACE(mx, Sc, Sa, Dc, Da) Sc
392 #define CHROMA_REPLACE COLOR_REPLACE
394 // MAX [max(Sa, Da), MAX(Sc, Dc)]
395 #define ALPHA_MAX(mx, Sa, Da) (Sa > Da ? Sa : Da)
396 #define COLOR_MAX(mx, Sc, Sa, Dc, Da) (Sc > Dc ? Sc : Dc)
397 #define CHROMA_MAX(mx, Sc, Sa, Dc, Da) (Sc > Dc ? Sc : Dc)
399 // MIN [min(Sa, Da), MIN(Sc, Dc)]
400 #define ALPHA_MIN(mx, Sa, Da) (Sa < Da ? Sa : Da)
401 #define COLOR_MIN(mx, Sc, Sa, Dc, Da) (Sc < Dc ? Sc : Dc)
402 #define CHROMA_MIN(mx, Sc, Sa, Dc, Da) (Sc < Dc ? Sc : Dc)
404 // AVERAGE [(Sa + Da) * 0.5, (Sc + Dc) * 0.5]
405 #define ALPHA_AVERAGE(mx, Sa, Da) ((Sa + Da) / 2)
406 #define COLOR_AVERAGE(mx, Sc, Sa, Dc, Da) ((Sc + Dc) / 2)
407 #define CHROMA_AVERAGE COLOR_AVERAGE
409 // DARKEN [Sa + Da - Sa*Da, Sc*(1 - Da) + Dc*(1 - Sa) + min(Sc, Dc)]
410 #define ALPHA_DARKEN(mx, Sa, Da) (Sa + Da - (Sa * Da) / mx)
411 #define COLOR_DARKEN(mx, Sc, Sa, Dc, Da) ((Sc * (mx - Da) + Dc * (mx - Sa)) / mx + (Sc < Dc ? Sc : Dc))
412 #define CHROMA_DARKEN COLOR_DARKEN
414 // LIGHTEN [Sa + Da - Sa*Da, Sc*(1 - Da) + Dc*(1 - Sa) + max(Sc, Dc)]
415 #define ALPHA_LIGHTEN(mx, Sa, Da) (Sa + Da - Sa * Da / mx)
416 #define COLOR_LIGHTEN(mx, Sc, Sa, Dc, Da) ((Sc * (mx - Da) + Dc * (mx - Sa)) / mx + (Sc > Dc ? Sc : Dc))
417 #define CHROMA_LIGHTEN COLOR_LIGHTEN
420 #define ALPHA_DST(mx, Sa, Da) Da
421 #define COLOR_DST(mx, Sc, Sa, Dc, Da) Dc
422 #define CHROMA_DST COLOR_DST
424 // DST_ATOP [Sa, Sc * (1 - Da) + Dc * Sa]
425 #define ALPHA_DST_ATOP(mx, Sa, Da) Sa
426 #define COLOR_DST_ATOP(mx, Sc, Sa, Dc, Da) ((Sc * (mx - Da) + Dc * Sa) / mx)
427 #define CHROMA_DST_ATOP COLOR_DST_ATOP
429 // DST_IN [Da * Sa, Dc * Sa]
430 #define ALPHA_DST_IN(mx, Sa, Da) ((Da * Sa) / mx)
431 #define COLOR_DST_IN(mx, Sc, Sa, Dc, Da) ((Dc * Sa) / mx)
432 #define CHROMA_DST_IN COLOR_DST_IN
434 // DST_OUT [Da * (1 - Sa), Dc * (1 - Sa)]
435 #define ALPHA_DST_OUT(mx, Sa, Da) (Da * (mx - Sa) / mx)
436 #define COLOR_DST_OUT(mx, Sc, Sa, Dc, Da) (Dc * (mx - Sa) / mx)
437 #define CHROMA_DST_OUT COLOR_DST_OUT
439 // DST_OVER [Sa * (1 - Da) + Da, Sc * (1 - Da) + Dc]
440 #define ALPHA_DST_OVER(mx, Sa, Da) ((Sa * (mx - Da)) / mx + Da)
441 #define COLOR_DST_OVER(mx, Sc, Sa, Dc, Da) (Sc * (mx - Da)/ mx + Dc)
442 #define CHROMA_DST_OVER COLOR_DST_OVER
445 #define ALPHA_SRC(mx, Sa, Da) Sa
446 #define COLOR_SRC(mx, Sc, Sa, Dc, Da) Sc
447 #define CHROMA_SRC COLOR_SRC
449 // SRC_ATOP [Da, Sc * Da + Dc * (1 - Sa)]
450 #define ALPHA_SRC_ATOP(mx, Sa, Da) Da
451 #define COLOR_SRC_ATOP(mx, Sc, Sa, Dc, Da) ((Sc * Da + Dc * (mx - Sa)) / mx)
452 #define CHROMA_SRC_ATOP COLOR_SRC_ATOP
454 // SRC_IN [Sa * Da, Sc * Da]
455 #define ALPHA_SRC_IN(mx, Sa, Da) ((Sa * Da) / mx)
456 #define COLOR_SRC_IN(mx, Sc, Sa, Dc, Da) (Sc * Da / mx)
457 #define CHROMA_SRC_IN COLOR_SRC_IN
459 // SRC_OUT [Sa * (1 - Da), Sc * (1 - Da)]
460 #define ALPHA_SRC_OUT(mx, Sa, Da) (Sa * (mx - Da) / mx)
461 #define COLOR_SRC_OUT(mx, Sc, Sa, Dc, Da) (Sc * (mx - Da) / mx)
462 #define CHROMA_SRC_OUT COLOR_SRC_OUT
464 // SRC_OVER [Sa + Da * (1 - Sa), Sc + (1 - Sa) * Dc]
465 #define ALPHA_SRC_OVER(mx, Sa, Da) (Sa + Da * (mx - Sa) / mx)
466 #define COLOR_SRC_OVER(mx, Sc, Sa, Dc, Da) (Sc + Dc * (mx - Sa) / mx)
467 #define CHROMA_SRC_OVER COLOR_SRC_OVER
469 // OR [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]
470 #define ALPHA_OR(mx, Sa, Da) (Sa + Da - (Sa * Da) / mx)
471 #define COLOR_OR(mx, Sc, Sa, Dc, Da) (Sc + Dc - (Sc * Dc) / mx)
472 #define CHROMA_OR COLOR_OR
474 // XOR [Sa * (1 - Da) + Da * (1 - Sa), Sc * (1 - Da) + Dc * (1 - Sa)]
475 #define ALPHA_XOR(mx, Sa, Da) ((Sa * (mx - Da) + Da * (mx - Sa)) / mx)
476 #define COLOR_XOR(mx, Sc, Sa, Dc, Da) ((Sc * (mx - Da) + Dc * (mx - Sa)) / mx)
477 #define CHROMA_XOR COLOR_XOR
479 #define ZTYP(ty) typedef ty z_##ty __attribute__ ((__unused__))
480 ZTYP(int8_t); ZTYP(uint8_t);
481 ZTYP(int16_t); ZTYP(uint16_t);
482 ZTYP(int32_t); ZTYP(uint32_t);
483 ZTYP(int64_t); ZTYP(uint64_t);
484 ZTYP(float); ZTYP(double);
486 #define ALPHA3_BLEND(FN, typ, inp, out, mx, ofs, rnd) \
487 typ inp0 = (typ)inp[0], inp1 = (typ)inp[1] - ofs; \
488 typ inp2 = (typ)inp[2] - ofs, inp3 = fade * mx + rnd; \
489 typ out0 = (typ)out[0], out1 = (typ)out[1] - ofs; \
490 typ out2 = (typ)out[2] - ofs, out3 = mx; \
491 r = COLOR_##FN(mx, inp0, inp3, out0, out3); \
493 g = CHROMA_##FN(mx, inp1, inp3, out1, out3); \
494 b = CHROMA_##FN(mx, inp2, inp3, out2, out3); \
497 g = COLOR_##FN(mx, inp1, inp3, out1, out3); \
498 b = COLOR_##FN(mx, inp2, inp3, out2, out3); \
501 #define ALPHA4_BLEND(FN, typ, inp, out, mx, ofs, rnd) \
502 typ inp0 = (typ)inp[0], inp1 = (typ)inp[1] - ofs; \
503 typ inp2 = (typ)inp[2] - ofs, inp3 = (typ)inp[3] * fade + rnd; \
504 typ out0 = (typ)out[0], out1 = (typ)out[1] - ofs; \
505 typ out2 = (typ)out[2] - ofs, out3 = out[3]; \
506 r = COLOR_##FN(mx, inp0, inp3, out0, out3); \
508 g = CHROMA_##FN(mx, inp1, inp3, out1, out3); \
509 b = CHROMA_##FN(mx, inp2, inp3, out2, out3); \
512 g = COLOR_##FN(mx, inp1, inp3, out1, out3); \
513 b = COLOR_##FN(mx, inp2, inp3, out2, out3); \
515 a = ALPHA_##FN(mx, inp3, out3)
517 #define ALPHA_STORE(out, ofs, mx) \
522 #define ALPHA3_STORE(out, ofs, mx) \
524 g = ofs ? cclip(g, mx) : aclip(g, mx); \
525 b = ofs ? cclip(b, mx) : aclip(b, mx); \
527 r = (r * opcty + out0 * trnsp) / mx; \
528 g = (g * opcty + out1 * trnsp) / mx; \
529 b = (b * opcty + out2 * trnsp) / mx; \
531 ALPHA_STORE(out, ofs, mx)
533 #define ALPHA4_STORE(out, ofs, mx) \
535 g = ofs ? cclip(g, mx) : aclip(g, mx); \
536 b = ofs ? cclip(b, mx) : aclip(b, mx); \
538 r = (r * opcty + out0 * trnsp) / mx; \
539 g = (g * opcty + out1 * trnsp) / mx; \
540 b = (b * opcty + out2 * trnsp) / mx; \
541 a = (a * opcty + out3 * trnsp) / mx; \
543 ALPHA_STORE(out, ofs, mx); \
544 out[3] = aclip(a, mx)
546 #define XBLEND(FN, temp_type, type, max, components, chroma_offset, round) { \
547 temp_type opcty = alpha * max + round, trnsp = max - opcty; \
548 type** output_rows = (type**)output->get_rows(); \
549 type** input_rows = (type**)input->get_rows(); \
550 ix *= components; ox *= components; \
552 for(int i = pkg->out_row1; i < pkg->out_row2; i++) { \
553 type* in_row = input_rows[i + iy] + ix; \
554 type* output = output_rows[i] + ox; \
555 for(int j = 0; j < ow; j++) { \
556 if( components == 4 ) { \
557 temp_type r, g, b, a; \
558 ALPHA4_BLEND(FN, temp_type, in_row, output, max, chroma_offset, round); \
559 ALPHA4_STORE(output, chroma_offset, max); \
563 ALPHA3_BLEND(FN, temp_type, in_row, output, max, chroma_offset, round); \
564 ALPHA3_STORE(output, chroma_offset, max); \
566 in_row += components; output += components; \
572 #define XBLEND_ONLY(FN) { \
573 switch(input->get_color_model()) { \
574 case BC_RGB_FLOAT: XBLEND(FN, z_float, z_float, 1.f, 3, 0, 0.f); \
575 case BC_RGBA_FLOAT: XBLEND(FN, z_float, z_float, 1.f, 4, 0, 0.f); \
576 case BC_RGB888: XBLEND(FN, z_int32_t, z_uint8_t, 0xff, 3, 0, .5f); \
577 case BC_YUV888: XBLEND(FN, z_int32_t, z_uint8_t, 0xff, 3, 0x80, .5f); \
578 case BC_RGBA8888: XBLEND(FN, z_int32_t, z_uint8_t, 0xff, 4, 0, .5f); \
579 case BC_YUVA8888: XBLEND(FN, z_int32_t, z_uint8_t, 0xff, 4, 0x80, .5f); \
580 case BC_RGB161616: XBLEND(FN, z_int64_t, z_uint16_t, 0xffff, 3, 0, .5f); \
581 case BC_YUV161616: XBLEND(FN, z_int64_t, z_uint16_t, 0xffff, 3, 0x8000, .5f); \
582 case BC_RGBA16161616: XBLEND(FN, z_int64_t, z_uint16_t, 0xffff, 4, 0, .5f); \
583 case BC_YUVA16161616: XBLEND(FN, z_int64_t, z_uint16_t, 0xffff, 4, 0x8000, .5f); \
588 /* Direct translate / blend **********************************************/
590 DirectPackage::DirectPackage()
594 DirectUnit::DirectUnit(DirectEngine *server)
597 this->engine = server;
600 DirectUnit::~DirectUnit()
604 void DirectUnit::process_package(LoadPackage *package)
606 DirectPackage *pkg = (DirectPackage*)package;
608 VFrame *output = engine->output;
609 VFrame *input = engine->input;
610 int mode = engine->mode;
611 float fade = engine->alpha;
613 BC_CModels::has_alpha(input->get_color_model()) &&
614 mode == TRANSFER_REPLACE ? 1.f : engine->alpha;
616 int ix = engine->in_x1;
617 int ox = engine->out_x1;
618 int ow = engine->out_x2 - ox;
619 int iy = engine->in_y1 - engine->out_y1;
622 case TRANSFER_NORMAL: XBLEND_ONLY(NORMAL);
623 case TRANSFER_ADDITION: XBLEND_ONLY(ADDITION);
624 case TRANSFER_SUBTRACT: XBLEND_ONLY(SUBTRACT);
625 case TRANSFER_MULTIPLY: XBLEND_ONLY(MULTIPLY);
626 case TRANSFER_DIVIDE: XBLEND_ONLY(DIVIDE);
627 case TRANSFER_REPLACE: XBLEND_ONLY(REPLACE);
628 case TRANSFER_MAX: XBLEND_ONLY(MAX);
629 case TRANSFER_MIN: XBLEND_ONLY(MIN);
630 case TRANSFER_AVERAGE: XBLEND_ONLY(AVERAGE);
631 case TRANSFER_DARKEN: XBLEND_ONLY(DARKEN);
632 case TRANSFER_LIGHTEN: XBLEND_ONLY(LIGHTEN);
633 case TRANSFER_DST: XBLEND_ONLY(DST);
634 case TRANSFER_DST_ATOP: XBLEND_ONLY(DST_ATOP);
635 case TRANSFER_DST_IN: XBLEND_ONLY(DST_IN);
636 case TRANSFER_DST_OUT: XBLEND_ONLY(DST_OUT);
637 case TRANSFER_DST_OVER: XBLEND_ONLY(DST_OVER);
638 case TRANSFER_SRC: XBLEND_ONLY(SRC);
639 case TRANSFER_SRC_ATOP: XBLEND_ONLY(SRC_ATOP);
640 case TRANSFER_SRC_IN: XBLEND_ONLY(SRC_IN);
641 case TRANSFER_SRC_OUT: XBLEND_ONLY(SRC_OUT);
642 case TRANSFER_SRC_OVER: XBLEND_ONLY(SRC_OVER);
643 case TRANSFER_OR: XBLEND_ONLY(OR);
644 case TRANSFER_XOR: XBLEND_ONLY(XOR);
648 DirectEngine::DirectEngine(int cpus)
649 : LoadServer(cpus, cpus)
653 DirectEngine::~DirectEngine()
657 void DirectEngine::init_packages()
659 if(in_x1 < 0) { out_x1 -= in_x1; in_x1 = 0; }
660 if(in_y1 < 0) { out_y1 -= in_y1; in_y1 = 0; }
661 if(out_x1 < 0) { in_x1 -= out_x1; out_x1 = 0; }
662 if(out_y1 < 0) { in_y1 -= out_y1; out_y1 = 0; }
663 if(out_x2 > output->get_w()) out_x2 = output->get_w();
664 if(out_y2 > output->get_h()) out_y2 = output->get_h();
665 int out_w = out_x2 - out_x1;
666 int out_h = out_y2 - out_y1;
667 if( !out_w || !out_h ) return;
670 int pkgs = get_total_packages();
671 int row1 = out_y1, row2 = row1;
672 for(int i = 0; i < pkgs; row1=row2 ) {
673 DirectPackage *package = (DirectPackage*)get_package(i);
674 row2 = ++i * rows / pkgs + out_y1;
675 package->out_row1 = row1;
676 package->out_row2 = row2;
680 LoadClient* DirectEngine::new_client()
682 return new DirectUnit(this);
685 LoadPackage* DirectEngine::new_package()
687 return new DirectPackage;
690 /* Nearest Neighbor scale / translate / blend ********************/
692 #define XBLEND_3NN(FN, temp_type, type, max, components, chroma_offset, round) { \
693 temp_type opcty = alpha * max + round, trnsp = max - opcty; \
694 type** output_rows = (type**)output->get_rows(); \
695 type** input_rows = (type**)input->get_rows(); \
698 for(int i = pkg->out_row1; i < pkg->out_row2; i++) { \
699 int *lx = engine->in_lookup_x; \
700 type* in_row = input_rows[*ly++]; \
701 type* output = output_rows[i] + ox; \
702 for(int j = 0; j < ow; j++) { \
704 if( components == 4 ) { \
705 temp_type r, g, b, a; \
706 ALPHA4_BLEND(FN, temp_type, in_row, output, max, chroma_offset, round); \
707 ALPHA4_STORE(output, chroma_offset, max); \
711 ALPHA3_BLEND(FN, temp_type, in_row, output, max, chroma_offset, round); \
712 ALPHA3_STORE(output, chroma_offset, max); \
714 output += components; \
720 #define XBLEND_NN(FN) { \
721 switch(input->get_color_model()) { \
722 case BC_RGB_FLOAT: XBLEND_3NN(FN, z_float, z_float, 1.f, 3, 0, 0.f); \
723 case BC_RGBA_FLOAT: XBLEND_3NN(FN, z_float, z_float, 1.f, 4, 0, 0.f); \
724 case BC_RGB888: XBLEND_3NN(FN, z_int32_t, z_uint8_t, 0xff, 3, 0, .5f); \
725 case BC_YUV888: XBLEND_3NN(FN, z_int32_t, z_uint8_t, 0xff, 3, 0x80, .5f); \
726 case BC_RGBA8888: XBLEND_3NN(FN, z_int32_t, z_uint8_t, 0xff, 4, 0, .5f); \
727 case BC_YUVA8888: XBLEND_3NN(FN, z_int32_t, z_uint8_t, 0xff, 4, 0x80, .5f); \
728 case BC_RGB161616: XBLEND_3NN(FN, z_int64_t, z_uint16_t, 0xffff, 3, 0, .5f); \
729 case BC_YUV161616: XBLEND_3NN(FN, z_int64_t, z_uint16_t, 0xffff, 3, 0x8000, .5f); \
730 case BC_RGBA16161616: XBLEND_3NN(FN, z_int64_t, z_uint16_t, 0xffff, 4, 0, .5f); \
731 case BC_YUVA16161616: XBLEND_3NN(FN, z_int64_t, z_uint16_t, 0xffff, 4, 0x8000, .5f); \
736 NNPackage::NNPackage()
740 NNUnit::NNUnit(NNEngine *server)
743 this->engine = server;
750 void NNUnit::process_package(LoadPackage *package)
752 NNPackage *pkg = (NNPackage*)package;
753 VFrame *output = engine->output;
754 VFrame *input = engine->input;
755 int mode = engine->mode;
756 float fade = engine->alpha;
758 BC_CModels::has_alpha(input->get_color_model()) &&
759 mode == TRANSFER_REPLACE ? 1.f : engine->alpha;
761 int ox = engine->out_x1i;
762 int ow = engine->out_x2i - ox;
763 int *ly = engine->in_lookup_y + pkg->out_row1;
766 case TRANSFER_NORMAL: XBLEND_NN(NORMAL);
767 case TRANSFER_ADDITION: XBLEND_NN(ADDITION);
768 case TRANSFER_SUBTRACT: XBLEND_NN(SUBTRACT);
769 case TRANSFER_MULTIPLY: XBLEND_NN(MULTIPLY);
770 case TRANSFER_DIVIDE: XBLEND_NN(DIVIDE);
771 case TRANSFER_REPLACE: XBLEND_NN(REPLACE);
772 case TRANSFER_MAX: XBLEND_NN(MAX);
773 case TRANSFER_MIN: XBLEND_NN(MIN);
774 case TRANSFER_AVERAGE: XBLEND_NN(AVERAGE);
775 case TRANSFER_DARKEN: XBLEND_NN(DARKEN);
776 case TRANSFER_LIGHTEN: XBLEND_NN(LIGHTEN);
777 case TRANSFER_DST: XBLEND_NN(DST);
778 case TRANSFER_DST_ATOP: XBLEND_NN(DST_ATOP);
779 case TRANSFER_DST_IN: XBLEND_NN(DST_IN);
780 case TRANSFER_DST_OUT: XBLEND_NN(DST_OUT);
781 case TRANSFER_DST_OVER: XBLEND_NN(DST_OVER);
782 case TRANSFER_SRC: XBLEND_NN(SRC);
783 case TRANSFER_SRC_ATOP: XBLEND_NN(SRC_ATOP);
784 case TRANSFER_SRC_IN: XBLEND_NN(SRC_IN);
785 case TRANSFER_SRC_OUT: XBLEND_NN(SRC_OUT);
786 case TRANSFER_SRC_OVER: XBLEND_NN(SRC_OVER);
787 case TRANSFER_OR: XBLEND_NN(OR);
788 case TRANSFER_XOR: XBLEND_NN(XOR);
792 NNEngine::NNEngine(int cpus)
793 : LoadServer(cpus, cpus)
799 NNEngine::~NNEngine()
802 delete[] in_lookup_x;
804 delete[] in_lookup_y;
807 void NNEngine::init_packages()
809 int in_w = input->get_w();
810 int in_h = input->get_h();
811 int out_w = output->get_w();
812 int out_h = output->get_h();
814 float in_subw = in_x2 - in_x1;
815 float in_subh = in_y2 - in_y1;
816 float out_subw = out_x2 - out_x1;
817 float out_subh = out_y2 - out_y1;
818 int first, last, count, i;
821 out_x1i = rint(out_x1);
822 out_x2i = rint(out_x2);
823 if(out_x1i < 0) out_x1i = 0;
824 if(out_x1i > out_w) out_x1i = out_w;
825 if(out_x2i < 0) out_x2i = 0;
826 if(out_x2i > out_w) out_x2i = out_w;
827 int out_wi = out_x2i - out_x1i;
828 if( !out_wi ) return;
830 delete[] in_lookup_x;
831 in_lookup_x = new int[out_wi];
832 delete[] in_lookup_y;
833 in_lookup_y = new int[out_h];
835 switch(input->get_color_model()) {
839 case BC_RGBA16161616:
846 for(i = out_x1i; i < out_x2i; i++) {
847 int in = (i - out_x1 + .5) * in_subw / out_subw + in_x1;
853 if(in >= 0 && in < in_w && in >= in_x1 && i >= 0 && i < out_w) {
856 in_lookup_x[0] = in * components;
859 in_lookup_x[count] = (in-last)*components;
868 out_x2i = first + count;
871 for(i = out_y1; i < out_y2; i++) {
872 int in = (i - out_y1+.5) * in_subh / out_subh + in_y1;
873 if(in < in_y1) in = in_y1;
874 if(in > in_y2) in = in_y2;
875 if(in >= 0 && in < in_h && i >= 0 && i < out_h) {
876 if(count == 0) first = i;
884 out_y2 = first + count;
887 int pkgs = get_total_packages();
888 int row1 = out_y1, row2 = row1;
889 for(int i = 0; i < pkgs; row1=row2 ) {
890 NNPackage *package = (NNPackage*)get_package(i);
891 row2 = ++i * rows / pkgs + out_y1;
892 package->out_row1 = row1;
893 package->out_row2 = row2;
897 LoadClient* NNEngine::new_client()
899 return new NNUnit(this);
902 LoadPackage* NNEngine::new_package()
904 return new NNPackage;
907 /* Fully resampled scale / translate / blend ******************************/
908 /* resample into a temporary row vector, then blend */
910 #define XSAMPLE(FN, temp_type, type, max, components, chroma_offset, round) { \
911 float temp[oh*components]; \
912 temp_type opcty = alpha * max + round, trnsp = max - opcty; \
913 type **output_rows = (type**)voutput->get_rows() + o1i; \
914 type **input_rows = (type**)vinput->get_rows(); \
916 for(int i = pkg->out_col1; i < pkg->out_col2; i++) { \
917 type *input = input_rows[i - engine->col_out1 + engine->row_in]; \
918 float *tempp = temp; \
919 if( !k ) { /* direct copy case */ \
920 type *ip = input + i1i * components; \
921 for(int j = 0; j < oh; j++) { \
923 *tempp++ = *ip++ - chroma_offset; \
924 *tempp++ = *ip++ - chroma_offset; \
925 if( components == 4 ) *tempp++ = *ip++; \
928 else { /* resample */ \
929 for(int j = 0; j < oh; j++) { \
930 float racc=0.f, gacc=0.f, bacc=0.f, aacc=0.f; \
931 int ki = lookup_sk[j], x = lookup_sx0[j]; \
932 type *ip = input + x * components; \
933 float wacc = 0, awacc = 0; \
934 while(x++ < lookup_sx1[j]) { \
935 float kv = k[abs(ki >> INDEX_FRACTION)]; \
936 /* handle fractional pixels on edges of input */ \
937 if(x == i1i) kv *= i1f; \
938 if(x + 1 == i2i) kv *= i2f; \
939 if( components == 4 ) { awacc += kv; kv *= ip[3]; } \
941 racc += kv * *ip++; \
942 gacc += kv * (*ip++ - chroma_offset); \
943 bacc += kv * (*ip++ - chroma_offset); \
944 if( components == 4 ) { aacc += kv; ++ip; } \
947 if(wacc > 0.) wacc = 1. / wacc; \
948 *tempp++ = racc * wacc; \
949 *tempp++ = gacc * wacc; \
950 *tempp++ = bacc * wacc; \
951 if( components == 4 ) { \
952 if(awacc > 0.) awacc = 1. / awacc; \
953 *tempp++ = aacc * awacc; \
958 /* handle fractional pixels on edges of output */ \
959 temp[0] *= o1f; temp[1] *= o1f; temp[2] *= o1f; \
960 if( components == 4 ) temp[3] *= o1f; \
961 tempp = temp + (oh-1)*components; \
962 tempp[0] *= o2f; tempp[1] *= o2f; tempp[2] *= o2f; \
963 if( components == 4 ) tempp[3] *= o2f; \
966 for(int j = 0; j < oh; j++) { \
967 type *output = output_rows[j] + i * components; \
968 if( components == 4 ) { \
969 temp_type r, g, b, a; \
970 ALPHA4_BLEND(FN, temp_type, tempp, output, max, 0, round); \
971 ALPHA4_STORE(output, chroma_offset, max); \
975 ALPHA3_BLEND(FN, temp_type, tempp, output, max, 0, round); \
976 ALPHA3_STORE(output, chroma_offset, max); \
978 tempp += components; \
984 #define XBLEND_SAMPLE(FN) { \
985 switch(vinput->get_color_model()) { \
986 case BC_RGB_FLOAT: XSAMPLE(FN, z_float, z_float, 1.f, 3, 0.f, 0.f); \
987 case BC_RGBA_FLOAT: XSAMPLE(FN, z_float, z_float, 1.f, 4, 0.f, 0.f); \
988 case BC_RGB888: XSAMPLE(FN, z_int32_t, z_uint8_t, 0xff, 3, 0, .5f); \
989 case BC_YUV888: XSAMPLE(FN, z_int32_t, z_uint8_t, 0xff, 3, 0x80, .5f); \
990 case BC_RGBA8888: XSAMPLE(FN, z_int32_t, z_uint8_t, 0xff, 4, 0, .5f); \
991 case BC_YUVA8888: XSAMPLE(FN, z_int32_t, z_uint8_t, 0xff, 4, 0x80, .5f); \
992 case BC_RGB161616: XSAMPLE(FN, z_int64_t, z_uint16_t, 0xffff, 3, 0, .5f); \
993 case BC_YUV161616: XSAMPLE(FN, z_int64_t, z_uint16_t, 0xffff, 3, 0x8000, .5f); \
994 case BC_RGBA16161616: XSAMPLE(FN, z_int64_t, z_uint16_t, 0xffff, 4, 0, .5f); \
995 case BC_YUVA16161616: XSAMPLE(FN, z_int64_t, z_uint16_t, 0xffff, 4, 0x8000, .5f); \
1001 SamplePackage::SamplePackage()
1005 SampleUnit::SampleUnit(SampleEngine *server)
1006 : LoadClient(server)
1008 this->engine = server;
1011 SampleUnit::~SampleUnit()
1015 void SampleUnit::process_package(LoadPackage *package)
1017 SamplePackage *pkg = (SamplePackage*)package;
1019 float i1 = engine->in1;
1020 float i2 = engine->in2;
1021 float o1 = engine->out1;
1022 float o2 = engine->out2;
1024 if(i2 - i1 <= 0 || o2 - o1 <= 0)
1027 VFrame *voutput = engine->output;
1028 VFrame *vinput = engine->input;
1029 int mode = engine->mode;
1030 float fade = engine->alpha;
1032 BC_CModels::has_alpha(vinput->get_color_model()) &&
1033 mode == TRANSFER_REPLACE ? 1.f : engine->alpha;
1035 //int iw = vinput->get_w();
1036 int i1i = floor(i1);
1038 float i1f = 1.f - i1 + i1i;
1039 float i2f = 1.f - i2i + i2;
1041 int o1i = floor(o1);
1043 float o1f = 1.f - o1 + o1i;
1044 float o2f = 1.f - o2i + o2;
1047 float *k = engine->kernel->lookup;
1048 //float kw = engine->kernel->width;
1049 //int kn = engine->kernel->n;
1050 int kd = engine->kd;
1052 int *lookup_sx0 = engine->lookup_sx0;
1053 int *lookup_sx1 = engine->lookup_sx1;
1054 int *lookup_sk = engine->lookup_sk;
1055 //float *lookup_wacc = engine->lookup_wacc;
1058 case TRANSFER_NORMAL: XBLEND_SAMPLE(NORMAL);
1059 case TRANSFER_ADDITION: XBLEND_SAMPLE(ADDITION);
1060 case TRANSFER_SUBTRACT: XBLEND_SAMPLE(SUBTRACT);
1061 case TRANSFER_MULTIPLY: XBLEND_SAMPLE(MULTIPLY);
1062 case TRANSFER_DIVIDE: XBLEND_SAMPLE(DIVIDE);
1063 case TRANSFER_REPLACE: XBLEND_SAMPLE(REPLACE);
1064 case TRANSFER_MAX: XBLEND_SAMPLE(MAX);
1065 case TRANSFER_MIN: XBLEND_SAMPLE(MIN);
1066 case TRANSFER_AVERAGE: XBLEND_SAMPLE(AVERAGE);
1067 case TRANSFER_DARKEN: XBLEND_SAMPLE(DARKEN);
1068 case TRANSFER_LIGHTEN: XBLEND_SAMPLE(LIGHTEN);
1069 case TRANSFER_DST: XBLEND_SAMPLE(DST);
1070 case TRANSFER_DST_ATOP: XBLEND_SAMPLE(DST_ATOP);
1071 case TRANSFER_DST_IN: XBLEND_SAMPLE(DST_IN);
1072 case TRANSFER_DST_OUT: XBLEND_SAMPLE(DST_OUT);
1073 case TRANSFER_DST_OVER: XBLEND_SAMPLE(DST_OVER);
1074 case TRANSFER_SRC: XBLEND_SAMPLE(SRC);
1075 case TRANSFER_SRC_ATOP: XBLEND_SAMPLE(SRC_ATOP);
1076 case TRANSFER_SRC_IN: XBLEND_SAMPLE(SRC_IN);
1077 case TRANSFER_SRC_OUT: XBLEND_SAMPLE(SRC_OUT);
1078 case TRANSFER_SRC_OVER: XBLEND_SAMPLE(SRC_OVER);
1079 case TRANSFER_OR: XBLEND_SAMPLE(OR);
1080 case TRANSFER_XOR: XBLEND_SAMPLE(XOR);
1085 SampleEngine::SampleEngine(int cpus)
1086 : LoadServer(cpus, cpus)
1095 SampleEngine::~SampleEngine()
1097 if(lookup_sx0) delete [] lookup_sx0;
1098 if(lookup_sx1) delete [] lookup_sx1;
1099 if(lookup_sk) delete [] lookup_sk;
1100 if(lookup_wacc) delete [] lookup_wacc;
1104 * unlike the Direct and NN engines, the Sample engine works across
1105 * output columns (it makes for more economical memory addressing
1106 * during convolution)
1108 void SampleEngine::init_packages()
1110 int iw = input->get_w();
1111 int i1i = floor(in1);
1112 int i2i = ceil(in2);
1113 float i1f = 1.f - in1 + i1i;
1114 float i2f = 1.f - i2i + in2;
1116 int oy = floor(out1);
1117 float oyf = out1 - oy;
1118 int oh = ceil(out2) - oy;
1120 float *k = kernel->lookup;
1121 float kw = kernel->width;
1124 if(in2 - in1 <= 0 || out2 - out1 <= 0)
1127 /* determine kernel spatial coverage */
1128 float scale = (out2 - out1) / (in2 - in1);
1129 float iscale = (in2 - in1) / (out2 - out1);
1130 float coverage = fabs(1.f / scale);
1131 float bound = (coverage < 1.f ? kw : kw * coverage) - (.5f / TRANSFORM_SPP);
1132 float coeff = (coverage < 1.f ? 1.f : scale) * TRANSFORM_SPP;
1134 delete [] lookup_sx0;
1135 delete [] lookup_sx1;
1136 delete [] lookup_sk;
1137 delete [] lookup_wacc;
1139 lookup_sx0 = new int[oh];
1140 lookup_sx1 = new int[oh];
1141 lookup_sk = new int[oh];
1142 lookup_wacc = new float[oh];
1144 kd = (double)coeff * (1 << INDEX_FRACTION) + .5;
1146 /* precompute kernel values and weight sums */
1147 for(int i = 0; i < oh; i++) {
1148 /* map destination back to source */
1149 double sx = (i - oyf + .5) * iscale + in1 - .5;
1152 * clip iteration to source area but not source plane. Points
1153 * outside the source plane count as transparent. Points outside
1154 * the source area don't count at all. The actual convolution
1155 * later will be clipped to both, but we need to compute
1158 int sx0 = MAX((int)floor(sx - bound) + 1, i1i);
1159 int sx1 = MIN((int)ceil(sx + bound), i2i);
1160 int ki = (double)(sx0 - sx) * coeff * (1 << INDEX_FRACTION)
1161 + (1 << (INDEX_FRACTION - 1)) + .5;
1167 for(int j= sx0; j < sx1; j++) {
1168 int kv = (ki >> INDEX_FRACTION);
1172 * the contribution of the first and last input pixel (if
1173 * fractional) are linearly weighted by the fraction
1176 wacc += k[abs(kv)] * i1f;
1177 else if(j + 1 == i2i)
1178 wacc += k[abs(kv)] * i2f;
1182 /* this is where we clip the kernel convolution to the source plane */
1183 if(j >= 0 && j < iw) {
1184 if(lookup_sx0[i] == -1) {
1188 lookup_sx1[i] = j + 1;
1193 lookup_wacc[i] = wacc > 0. ? 1. / wacc : 0.;
1196 int cols = col_out2 - col_out1;
1197 int pkgs = get_total_packages();
1198 int col1 = col_out1, col2 = col1;
1199 for(int i = 0; i < pkgs; col1=col2 ) {
1200 SamplePackage *package = (SamplePackage*)get_package(i);
1201 col2 = ++i * cols / pkgs + col_out1;
1202 package->out_col1 = col1;
1203 package->out_col2 = col2;
1207 LoadClient* SampleEngine::new_client()
1209 return new SampleUnit(this);
1212 LoadPackage* SampleEngine::new_package()
1214 return new SamplePackage;