1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/**
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2006 Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
#include "../CMP_util.h"
/* **************** Displace ******************** */
static bNodeSocketType cmp_node_displace_in[]= {
{ SOCK_RGBA, 1, "Image", 0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
{ SOCK_VECTOR, 1, "Vector", 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
{ SOCK_VALUE, 1, "X Scale", 0.0f, 0.0f, 0.0f, 0.0f, -1000.0f, 1000.0f},
{ SOCK_VALUE, 1, "Y Scale", 0.0f, 0.0f, 0.0f, 0.0f, -1000.0f, 1000.0f},
{ -1, 0, "" }
};
static bNodeSocketType cmp_node_displace_out[]= {
{ SOCK_RGBA, 0, "Image", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
{ -1, 0, "" }
};
static void do_displace(CompBuf *stackbuf, CompBuf *cbuf, CompBuf *vecbuf, float *veccol, float *xscale, float *yscale)
{
ImBuf *ibuf;
int x, y, vx, vy, sx, sy;
float dx=0.0, dy=0.0;
float dspx, dspy;
float uv[2];
float *out= stackbuf->rect, *vec=vecbuf->rect, *in= cbuf->rect;
float *vp, *vpnext, *vpprev;
int row = 3*vecbuf->x;
/* ibuf needed for sampling */
ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0, 0);
ibuf->rect_float= cbuf->rect;
vec = vecbuf->rect;
sx= stackbuf->x;
sy= stackbuf->y;
for(y=0; y<sy; y++) {
for(x= 0; x< sx; x++, out+=4, in+=4, vec+=3) {
/* the x-xrad stuff is a bit weird, but i seem to need it otherwise
* my returned pixels are offset weirdly */
vp = compbuf_get_pixel(vecbuf, veccol, x-vecbuf->xrad, y-vecbuf->yrad, vecbuf->xrad, vecbuf->yrad);
/* this happens in compbuf_get_pixel, need to make sure the following
* check takes them into account */
vx= x-vecbuf->xof;
vy= y-vecbuf->yof;
/* find the new displaced co-ords, also correcting for translate offset */
dspx = vx - (*xscale * vp[0]);
dspy = vy - (*yscale * vp[1]);
/* convert image space to 0.0-1.0 UV space for sampling, correcting for translate offset */
uv[0] = dspx / (float)sx;
uv[1] = dspy / (float)sy;
if(vx>0 && vx< vecbuf->x-1 && vy>0 && vy< vecbuf->y-1) {
vpnext = vp+row;
vpprev = vp-row;
/* adaptive sampling, X channel */
dx= 0.5f*(fabs(vp[0]-vp[-3]) + fabs(vp[0]-vp[3]));
dx+= 0.25f*(fabs(vp[0]-vpprev[-3]) + fabs(vp[0]-vpnext[-3]));
dx+= 0.25f*(fabs(vp[0]-vpprev[+3]) + fabs(vp[0]-vpnext[+3]));
/* adaptive sampling, Y channel */
dy= 0.5f*(fabs(vp[1]-vp[-row+1]) + fabs(vp[1]-vp[row+1]));
dy+= 0.25f*(fabs(vp[1]-vpprev[+1-3]) + fabs(vp[1]-vpnext[+1-3]));
dy+= 0.25f*(fabs(vp[1]-vpprev[+1+3]) + fabs(vp[1]-vpnext[+1+3]));
/* scaled down to prevent blurriness */
/* 8: magic number, provides a good level of sharpness without getting too aliased */
dx /= 8;
dy /= 8;
}
/* should use mipmap */
if(dx > 0.006f) dx= 0.006f;
if(dy > 0.006f) dy= 0.006f;
if ((vp[0]> 0.0) && (dx < 0.004)) dx = 0.004;
if ((vp[1]> 0.0) && (dy < 0.004)) dy = 0.004;
ibuf_sample(ibuf, uv[0], uv[1], dx, dy, out);
}
}
IMB_freeImBuf(ibuf);
}
static void node_composit_exec_displace(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
{
if(out[0]->hasoutput==0)
return;
if(in[0]->data && in[1]->data) {
CompBuf *cbuf= in[0]->data;
CompBuf *vecbuf= in[1]->data;
CompBuf *stackbuf;
cbuf= typecheck_compbuf(cbuf, CB_RGBA);
vecbuf= typecheck_compbuf(vecbuf, CB_VEC3);
stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); /* allocs */
do_displace(stackbuf, cbuf, vecbuf, in[1]->vec, in[2]->vec, in[3]->vec);
out[0]->data= stackbuf;
if(cbuf!=in[0]->data)
free_compbuf(cbuf);
if(vecbuf!=in[1]->data)
free_compbuf(vecbuf);
}
}
bNodeType cmp_node_displace= {
/* *next,*prev */ NULL, NULL,
/* type code */ CMP_NODE_DISPLACE,
/* name */ "Displace",
/* width+range */ 140, 100, 320,
/* class+opts */ NODE_CLASS_DISTORT, NODE_OPTIONS,
/* input sock */ cmp_node_displace_in,
/* output sock */ cmp_node_displace_out,
/* storage */ "",
/* execfunc */ node_composit_exec_displace,
/* butfunc */ NULL,
/* initfunc */ NULL,
/* freestoragefunc */ NULL,
/* copystoragefunc */ NULL,
/* id */ NULL
};
|