~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/nodes/composite/nodes/node_composite_lensdist.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
#include "node_composite_util.h"
34
34
 
35
 
static bNodeSocketTemplate cmp_node_lensdist_in[]= {
36
 
        {       SOCK_RGBA, 1, "Image",                  1.0f, 1.0f, 1.0f, 1.0f},
37
 
        {       SOCK_FLOAT, 1, "Distort",       0.f, 0.f, 0.f, 0.f, -0.999f, 1.f, PROP_NONE},
38
 
        {       SOCK_FLOAT, 1, "Dispersion", 0.f, 0.f, 0.f, 0.f, 0.f, 1.f, PROP_NONE},
39
 
        {       -1, 0, ""       }
40
 
};
41
 
static bNodeSocketTemplate cmp_node_lensdist_out[]= {
42
 
        {       SOCK_RGBA, 0, "Image"},
43
 
        {       -1, 0, ""       }
44
 
};
45
 
 
46
 
/* assumes *dst is type RGBA */
47
 
static void lensDistort(CompBuf *dst, CompBuf *src, float kr, float kg, float kb, int jit, int proj, int fit)
48
 
{
49
 
        int x, y, z;
50
 
        const float cx = 0.5f*(float)dst->x, cy = 0.5f*(float)dst->y;
51
 
 
52
 
        if (proj) {
53
 
                // shift
54
 
                CompBuf *tsrc = dupalloc_compbuf(src);
55
 
                
56
 
                for (z=0; z<tsrc->type; ++z)
57
 
                        IIR_gauss(tsrc, (kr+0.5f)*(kr+0.5f), z, 1);
58
 
                kr *= 20.f;
59
 
                
60
 
                for (y=0; y<dst->y; y++) {
61
 
                        fRGB *colp = (fRGB*)&dst->rect[y*dst->x*dst->type];
62
 
                        const float v = (y + 0.5f)/(float)dst->y;
63
 
                        
64
 
                        for (x=0; x<dst->x; x++) {
65
 
                                const float u = (x + 0.5f)/(float)dst->x;
66
 
                                
67
 
                                qd_getPixelLerpChan(tsrc, (u*dst->x + kr) - 0.5f, v*dst->y - 0.5f, 0, colp[x]);
68
 
                                if (tsrc->type == CB_VAL)
69
 
                                        colp[x][1] = tsrc->rect[x + y*tsrc->x];
70
 
                                else
71
 
                                        colp[x][1] = tsrc->rect[(x + y*tsrc->x)*tsrc->type + 1];
72
 
                                qd_getPixelLerpChan(tsrc, (u*dst->x - kr) - 0.5f, v*dst->y - 0.5f, 2, colp[x]+2);
73
 
                                
74
 
                                /* set alpha */
75
 
                                colp[x][3]= 1.0f;
76
 
                        }
77
 
                }
78
 
                free_compbuf(tsrc);
79
 
        }
80
 
        else {
81
 
                // Spherical
82
 
                // Scale factor to make bottom/top & right/left sides fit in window after deform
83
 
                // so in the case of pincushion (kn < 0), corners will be outside window.
84
 
                // Now also optionally scales image such that black areas are not visible when distort factor is positive
85
 
                // (makes distorted corners match window corners, but really only valid if mk<=0.5)
86
 
                const float mk = MAX3(kr, kg, kb);
87
 
                const float sc = (fit && (mk > 0.f)) ? (1.f/(1.f + 2.f*mk)) : (1.f/(1.f + mk));
88
 
                const float drg = 4.f*(kg - kr), dgb = 4.f*(kb - kg);
89
 
                
90
 
                kr *= 4.f, kg *= 4.f, kb *= 4.f;
91
 
 
92
 
                for (y=0; y<dst->y; y++) {
93
 
                        fRGB *colp = (fRGB*)&dst->rect[y*dst->x*dst->type];
94
 
                        const float v = sc*((y + 0.5f) - cy)/cy;
95
 
                        
96
 
                        for (x=0; x<dst->x; x++) {
97
 
                                int dr = 0, dg = 0, db = 0;
98
 
                                float d, t, ln[6] = {0, 0, 0, 0, 0, 0};
99
 
                                fRGB c1, tc = {0, 0, 0, 0};
100
 
                                const float u = sc*((x + 0.5f) - cx)/cx;
101
 
                                int sta = 0, mid = 0, end = 0;
102
 
                                
103
 
                                if ((t = 1.f - kr*(u*u + v*v)) >= 0.f) {
104
 
                                        d = 1.f/(1.f + sqrtf(t));
105
 
                                        ln[0] = (u*d + 0.5f)*dst->x - 0.5f, ln[1] = (v*d + 0.5f)*dst->y - 0.5f;
106
 
                                        sta = 1;
107
 
                                }
108
 
                                if ((t = 1.f - kg*(u*u + v*v)) >= 0.f) {
109
 
                                        d = 1.f/(1.f + sqrtf(t));
110
 
                                        ln[2] = (u*d + 0.5f)*dst->x - 0.5f, ln[3] = (v*d + 0.5f)*dst->y - 0.5f;
111
 
                                        mid = 1;
112
 
                                }
113
 
                                if ((t = 1.f - kb*(u*u + v*v)) >= 0.f) {
114
 
                                        d = 1.f/(1.f + sqrtf(t));
115
 
                                        ln[4] = (u*d + 0.5f)*dst->x - 0.5f, ln[5] = (v*d + 0.5f)*dst->y - 0.5f;
116
 
                                        end = 1;
117
 
                                }
118
 
        
119
 
                                if (sta && mid && end) {
120
 
                                        // RG
121
 
                                        const int dx = ln[2] - ln[0], dy = ln[3] - ln[1];
122
 
                                        const float dsf = sqrtf(dx*dx + dy*dy) + 1.f;
123
 
                                        const int ds = (int)(jit ? ((dsf < 4.f) ? 2.f : sqrtf(dsf)) : dsf);
124
 
                                        const float sd = 1.f/(float)ds;
125
 
                                        
126
 
                                        for (z=0; z<ds; ++z) {
127
 
                                                const float tz = ((float)z + (jit ? BLI_frand() : 0.5f))*sd;
128
 
                                                t = 1.f - (kr + tz*drg)*(u*u + v*v);
129
 
                                                d = 1.f / (1.f + sqrtf(t));
130
 
                                                qd_getPixelLerp(src, (u*d + 0.5f)*dst->x - 0.5f, (v*d + 0.5f)*dst->y - 0.5f, c1);
131
 
                                                if (src->type == CB_VAL) c1[1] = c1[2] = c1[0];
132
 
                                                tc[0] += (1.f-tz)*c1[0], tc[1] += tz*c1[1];
133
 
                                                dr++, dg++;
134
 
                                        }
135
 
                                        // GB
136
 
                                        {
137
 
                                                const int dx = ln[4] - ln[2], dy = ln[5] - ln[3];
138
 
                                                const float dsf = sqrtf(dx*dx + dy*dy) + 1.f;
139
 
                                                const int ds = (int)(jit ? ((dsf < 4.f) ? 2.f : sqrtf(dsf)) : dsf);
140
 
                                                const float sd = 1.f/(float)ds;
141
 
                                                
142
 
                                                for (z=0; z<ds; ++z) {
143
 
                                                        const float tz = ((float)z + (jit ? BLI_frand() : 0.5f))*sd;
144
 
                                                        t = 1.f - (kg + tz*dgb)*(u*u + v*v);
145
 
                                                        d = 1.f / (1.f + sqrtf(t));
146
 
                                                        qd_getPixelLerp(src, (u*d + 0.5f)*dst->x - 0.5f, (v*d + 0.5f)*dst->y - 0.5f, c1);
147
 
                                                        if (src->type == CB_VAL) c1[1] = c1[2] = c1[0];
148
 
                                                        tc[1] += (1.f-tz)*c1[1], tc[2] += tz*c1[2];
149
 
                                                        dg++, db++;
150
 
                                                }
151
 
                                        }
152
 
                                }
153
 
        
154
 
                                if (dr) colp[x][0] = 2.f*tc[0] / (float)dr;
155
 
                                if (dg) colp[x][1] = 2.f*tc[1] / (float)dg;
156
 
                                if (db) colp[x][2] = 2.f*tc[2] / (float)db;
157
 
        
158
 
                                /* set alpha */
159
 
                                colp[x][3]= 1.0f;
160
 
                        }
161
 
                }
162
 
        }
163
 
}
164
 
 
165
 
 
166
 
static void node_composit_exec_lensdist(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
167
 
{
168
 
        CompBuf *new, *img = in[0]->data;
169
 
        NodeLensDist *nld = node->storage;
170
 
        const float k = MAX2(MIN2(in[1]->vec[0], 1.f), -0.999f);
171
 
        // smaller dispersion range for somewhat more control
172
 
        const float d = 0.25f*MAX2(MIN2(in[2]->vec[0], 1.f), 0.f);
173
 
        const float kr = MAX2(MIN2((k+d), 1.f), -0.999f), kb = MAX2(MIN2((k-d), 1.f), -0.999f);
174
 
 
175
 
        if ((img==NULL) || (out[0]->hasoutput==0)) return;
176
 
 
177
 
        new = alloc_compbuf(img->x, img->y, CB_RGBA, 1);
178
 
 
179
 
        lensDistort(new, img, (nld->proj ? d : kr), k, kb, nld->jit, nld->proj, nld->fit);
180
 
 
181
 
        out[0]->data = new;
182
 
}
183
 
 
184
 
 
185
 
static void node_composit_init_lensdist(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
 
35
static bNodeSocketTemplate cmp_node_lensdist_in[] = {
 
36
        {       SOCK_RGBA, 1, N_("Image"),                      1.0f, 1.0f, 1.0f, 1.0f},
 
37
        {       SOCK_FLOAT, 1, N_("Distort"),   0.f, 0.f, 0.f, 0.f, -0.999f, 1.f, PROP_NONE},
 
38
        {       SOCK_FLOAT, 1, N_("Dispersion"), 0.f, 0.f, 0.f, 0.f, 0.f, 1.f, PROP_NONE},
 
39
        {       -1, 0, ""       }
 
40
};
 
41
static bNodeSocketTemplate cmp_node_lensdist_out[] = {
 
42
        {       SOCK_RGBA, 0, N_("Image")},
 
43
        {       -1, 0, ""       }
 
44
};
 
45
 
 
46
static void node_composit_init_lensdist(bNodeTree *UNUSED(ntree), bNode *node, bNodeTemplate *UNUSED(ntemp))
186
47
{
187
48
        NodeLensDist *nld = MEM_callocN(sizeof(NodeLensDist), "node lensdist data");
188
49
        nld->jit = nld->proj = nld->fit = 0;
199
60
        node_type_size(&ntype, 150, 120, 200);
200
61
        node_type_init(&ntype, node_composit_init_lensdist);
201
62
        node_type_storage(&ntype, "NodeLensDist", node_free_standard_storage, node_copy_standard_storage);
202
 
        node_type_exec(&ntype, node_composit_exec_lensdist);
203
63
 
204
64
        nodeRegisterType(ttype, &ntype);
205
65
}