change copy packed clip to unpacked in move_edits
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / interp.h
1 #ifndef __INTERP__
2 #define __INTERP__
3
4 /* fractional input interpolation
5  interp = nearest, bi_linear, bi_cubic
6  typical use:
7
8  type **in_rows = (type**)in->get_rows();
9  type **out_rows = (type**)out->get_rows();
10  int ow = out->get_w(), oh = out->get_h();
11  type offset[4] = { 0, chroma, chroma, 0 };
12  type bkgrnd[4] = { 0, chroma, chroma, 0 };
13  INTERP_SETUP(in_rows, max, 0,0, width,height); 
14
15  for( int oy=0; oy<oh; ++oy ) {
16    type *out_row = out_rows[y];
17    for( ox=0; ox<ow; ++ox ) {
18      float x_in = remap_x(ox,oy), y_in = remap_y(ox,oy);
19      interp##_SETUP(type, components, x_in, y_in); \
20      *out_row++ = interp##_interp(offset[i], bkgrnd[i]); \
21      for( int i=1; i<components; ++i ) {
22        interp##_next();
23        *out_row++ = interp##_interp(offset[i], bkgrnd[i]); \
24      }
25    }
26  }
27 */
28
29 static inline float in_clip(float v, float mx)
30 {
31         return v < 0 ? 0 : v > mx ? mx : v;
32 }
33
34 static inline float interp_linear(float dx, float p1, float p2)
35 {
36         return p1 * (1-dx) + p2 * dx;
37 }
38
39 static inline float interp_cubic(float dx, float p0, float p1, float p2, float p3)
40 { /* Catmull-Rom - not bad */
41         return ((( (- p0 + 3*p1 - 3*p2 + p3) * dx +
42                 ( 2*p0 - 5*p1 + 4*p2 - p3 ) ) * dx +
43                 ( - p0 + p2 ) ) * dx + (p1 + p1)) / 2;
44 }
45
46
47 #define INTERP_SETUP(rows, max, x_in_min,y_in_min, x_in_max,y_in_max) \
48         void **interp_rows = (void **)(rows);  float in_max = (max);  \
49         int in_min_x = (x_in_min), in_max_x = (x_in_max); \
50         int in_min_y = (y_in_min), in_max_y = (y_in_max)
51
52
53 #define nearest_SETUP(typ, components, tx, ty) \
54         int itx = (tx), ity = (ty), in_comps = (components); \
55         int c0 = itx+0, r0 = ity+0; \
56         typ *r0p = r0>=in_min_y && r0<in_max_y ? ((typ**)interp_rows)[r0] : 0
57
58 #define NEAREST_ROW(in_row, ofs, bg) (!in_row ? bg - ofs : ( \
59         (c0>=in_min_x && c0<in_max_x ? in_row[c0*in_comps] : bg) - ofs))
60
61 #define nearest_interp(ofs, bg) in_clip(NEAREST_ROW(r0p, ofs, bg) + ofs, in_max)
62
63 #define nearest_next(s) { if(r0p) ++r0p; }
64
65
66 #define bi_linear_SETUP(typ, components, tx, ty) \
67         float dx = (tx)-0.5, dy = (ty)-0.5; \
68         int itx = dx, ity = dy, in_comps = (components); \
69         if( (dx -= itx) < 0 ) dx += 1; \
70         if( (dy -= ity) < 0 ) dy += 1; \
71         int c0 = itx+0, c1 = itx+1, r0 = ity+0, r1 = ity+1; \
72         typ *r0p = r0>=in_min_y && r0<in_max_y ? ((typ**)interp_rows)[r0] : 0; \
73         typ *r1p = r1>=in_min_y && r1<in_max_y ? ((typ**)interp_rows)[r1] : 0
74
75 #define LINEAR_ROW(in_row, ofs, bg) (!in_row ? bg - ofs : interp_linear(dx, \
76         (c0>=in_min_x && c0<in_max_x ? in_row[c0*in_comps] : bg) - ofs, \
77         (c1>=in_min_x && c1<in_max_x ? in_row[c1*in_comps] : bg) - ofs))
78
79 #define bi_linear_interp(ofs, bg) in_clip(interp_linear(dy, \
80         LINEAR_ROW(r0p, ofs, bg), LINEAR_ROW(r1p, ofs, bg)) + ofs, in_max)
81
82 #define bi_linear_next(s) { if(r0p) ++r0p;  if(r1p) ++r1p; }
83
84
85 #define bi_cubic_SETUP(typ, components, tx, ty) \
86         float dx = (tx)-0.5, dy = (ty)-0.5; \
87         int itx = dx, ity = dy, in_comps = (components); \
88         if( (dx -= itx) < 0 ) dx += 1; \
89         if( (dy -= ity) < 0 ) dy += 1; \
90         int cp = itx-1, c0 = itx+0, c1 = itx+1, c2 = itx+2; \
91         int rp = ity-1, r0 = ity+0, r1 = ity+1, r2 = ity+2; \
92         typ *rpp = rp>=in_min_y && rp<in_max_y ? ((typ**)interp_rows)[rp] : 0; \
93         typ *r0p = r0>=in_min_y && r0<in_max_y ? ((typ**)interp_rows)[r0] : 0; \
94         typ *r1p = r1>=in_min_y && r1<in_max_y ? ((typ**)interp_rows)[r1] : 0; \
95         typ *r2p = r2>=in_min_y && r2<in_max_y ? ((typ**)interp_rows)[r2] : 0
96
97 #define CUBIC_ROW(in_row, ofs, bg) (!in_row ? bg - ofs : interp_cubic(dx, \
98         (cp>=in_min_x && cp<in_max_x ? in_row[cp*in_comps] : bg) - ofs, \
99         (c0>=in_min_x && c0<in_max_x ? in_row[c0*in_comps] : bg) - ofs, \
100         (c1>=in_min_x && c1<in_max_x ? in_row[c1*in_comps] : bg) - ofs, \
101         (c2>=in_min_x && c2<in_max_x ? in_row[c2*in_comps] : bg) - ofs))
102
103 #define bi_cubic_interp(ofs, bg) in_clip(interp_cubic(dy, \
104         CUBIC_ROW(rpp, ofs, bg), CUBIC_ROW(r0p, ofs, bg), \
105         CUBIC_ROW(r1p, ofs, bg), CUBIC_ROW(r2p, ofs, bg)) + ofs, in_max)
106
107 #define bi_cubic_next(s) { if(rpp) ++rpp;  if(r0p) ++r0p;  if(r1p) ++r1p;  if(r2p) ++r2p; }
108
109 #endif