33
33
#include "node_composite_util.h"
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},
41
static bNodeSocketTemplate cmp_node_lensdist_out[]= {
42
{ SOCK_RGBA, 0, "Image"},
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)
50
const float cx = 0.5f*(float)dst->x, cy = 0.5f*(float)dst->y;
54
CompBuf *tsrc = dupalloc_compbuf(src);
56
for (z=0; z<tsrc->type; ++z)
57
IIR_gauss(tsrc, (kr+0.5f)*(kr+0.5f), z, 1);
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;
64
for (x=0; x<dst->x; x++) {
65
const float u = (x + 0.5f)/(float)dst->x;
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];
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);
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);
90
kr *= 4.f, kg *= 4.f, kb *= 4.f;
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;
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;
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;
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;
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;
119
if (sta && mid && end) {
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;
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];
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;
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];
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;
166
static void node_composit_exec_lensdist(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
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);
175
if ((img==NULL) || (out[0]->hasoutput==0)) return;
177
new = alloc_compbuf(img->x, img->y, CB_RGBA, 1);
179
lensDistort(new, img, (nld->proj ? d : kr), k, kb, nld->jit, nld->proj, nld->fit);
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},
41
static bNodeSocketTemplate cmp_node_lensdist_out[] = {
42
{ SOCK_RGBA, 0, N_("Image")},
46
static void node_composit_init_lensdist(bNodeTree *UNUSED(ntree), bNode *node, bNodeTemplate *UNUSED(ntemp))
187
48
NodeLensDist *nld = MEM_callocN(sizeof(NodeLensDist), "node lensdist data");
188
49
nld->jit = nld->proj = nld->fit = 0;