Welcome to mirror list, hosted at ThFree Co, Russian Federation.

COM_ScreenLensDistortionOperation.cpp « operations « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d1060224444e094bfbc3607f3ebe8378604ad98b (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright 2011, Blender Foundation.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor: 
 *		Jeroen Bakker 
 *		Monique Dewanchand
 */

#include "COM_ScreenLensDistortionOperation.h"

extern "C" {
	#include "BLI_math.h"
	#include "BLI_utildefines.h"
	#include "BLI_rand.h"
}

ScreenLensDistortionOperation::ScreenLensDistortionOperation() : NodeOperation()
{
	this->addInputSocket(COM_DT_COLOR);
	this->addInputSocket(COM_DT_VALUE);
	this->addInputSocket(COM_DT_VALUE);
	this->addOutputSocket(COM_DT_COLOR);
	this->setComplex(true);
	this->m_inputProgram = NULL;
	this->m_valuesAvailable = false;
	this->m_dispersion = 0.0f;
	this->m_distortion = 0.0f;
}
void ScreenLensDistortionOperation::initExecution()
{
	this->m_inputProgram = this->getInputSocketReader(0);
	this->initMutex();
	this->m_cx = 0.5f * (float)getWidth();
	this->m_cy = 0.5f * (float)getHeight();
	
}

void *ScreenLensDistortionOperation::initializeTileData(rcti *rect)
{
	void *buffer = this->m_inputProgram->initializeTileData(NULL);
	updateDispersionAndDistortion();
	return buffer;
}

void ScreenLensDistortionOperation::executePixel(float output[4], int x, int y, void *data)
{
	const float height = this->getHeight();
	const float width = this->getWidth();
	MemoryBuffer *buffer = (MemoryBuffer *)data;

	int dr = 0, dg = 0, db = 0;
	float d, t, ln[6] = {0, 0, 0, 0, 0, 0};
	float tc[4] = {0, 0, 0, 0};
	const float v = this->m_sc * ((y + 0.5f) - this->m_cy) / this->m_cy;
	const float u = this->m_sc * ((x + 0.5f) - this->m_cx) / this->m_cx;
	const float uv_dot = u * u + v * v;
	int sta = 0, mid = 0, end = 0;

	if ((t = 1.0f - this->m_kr4 * uv_dot) >= 0.0f) {
		d = 1.0f / (1.0f + sqrtf(t));
		ln[0] = (u * d + 0.5f) * width - 0.5f, ln[1] = (v * d + 0.5f) * height - 0.5f;
		sta = 1;
	}
	if ((t = 1.0f - this->m_kg4 * uv_dot) >= 0.0f) {
		d = 1.0f / (1.0f + sqrtf(t));
		ln[2] = (u * d + 0.5f) * width - 0.5f, ln[3] = (v * d + 0.5f) * height - 0.5f;
		mid = 1;
	}
	if ((t = 1.0f - this->m_kb4 * uv_dot) >= 0.0f) {
		d = 1.0f / (1.0f + sqrtf(t));
		ln[4] = (u * d + 0.5f) * width - 0.5f, ln[5] = (v * d + 0.5f) * height - 0.5f;
		end = 1;
	}

	if (sta && mid && end) {
		float jit = this->m_data->jit;
		float z;
		float color[4];
		{
			// RG
			const int dx = ln[2] - ln[0], dy = ln[3] - ln[1];
			const float dsf = sqrtf((float)dx * dx + dy * dy) + 1.0f;
			const int ds = (int)(jit ? ((dsf < 4.0f) ? 2.0f : sqrtf(dsf)) : dsf);
			const float sd = 1.0f / (float)ds;

			for (z = 0; z < ds; ++z) {
				const float tz = (z + (jit ? BLI_frand() : 0.5f)) * sd;
				t = 1.0f - (this->m_kr4 + tz * this->m_drg) * uv_dot;
				d = 1.0f / (1.0f + sqrtf(t));
				const float nx = (u * d + 0.5f) * width - 0.5f;
				const float ny = (v * d + 0.5f) * height - 0.5f;
				buffer->readBilinear(color, nx, ny);
				tc[0] += (1.0f - tz) * color[0], tc[1] += tz * color[1];
				dr++, dg++;
			}
		}
		{
			// GB
			const int dx = ln[4] - ln[2], dy = ln[5] - ln[3];
			const float dsf = sqrtf((float)dx * dx + dy * dy) + 1.0f;
			const int ds = (int)(jit ? ((dsf < 4.0f) ? 2.0f : sqrtf(dsf)) : dsf);
			const float sd = 1.0f / (float)ds;

			for (z = 0; z < ds; ++z) {
				const float tz = (z + (jit ? BLI_frand() : 0.5f)) * sd;
				t = 1.0f - (this->m_kg4 + tz * this->m_dgb) * uv_dot;
				d = 1.0f / (1.0f + sqrtf(t));
				const float nx = (u * d + 0.5f) * width - 0.5f;
				const float ny = (v * d + 0.5f) * height - 0.5f;
				buffer->readBilinear(color, nx, ny);
				tc[1] += (1.0f - tz) * color[1], tc[2] += tz * color[2];
				dg++, db++;
			}

		}
		if (dr) output[0] = 2.0f * tc[0] / (float)dr;
		if (dg) output[1] = 2.0f * tc[1] / (float)dg;
		if (db) output[2] = 2.0f * tc[2] / (float)db;

		/* set alpha */
		output[3] = 1.0f;
	}
	else {
		zero_v4(output);
	}
}

void ScreenLensDistortionOperation::deinitExecution()
{
	this->deinitMutex();
	this->m_inputProgram = NULL;
}

void ScreenLensDistortionOperation::determineUV(float result[6], float x, float y, float distortion, float dispersion) 
{
	if (!this->m_valuesAvailable) {
		updateVariables(distortion, dispersion);
	}
	determineUV(result, x, y);
}

void ScreenLensDistortionOperation::determineUV(float result[6], float x, float y) const
{
	const float height = this->getHeight();
	const float width = this->getWidth();
	
	result[0] = x;
	result[1] = y;
	result[2] = x;
	result[3] = y;
	result[4] = x;
	result[5] = y;
	
	float d, t;
	const float v = this->m_sc * ((y + 0.5f) - this->m_cy) / this->m_cy;
	const float u = this->m_sc * ((x + 0.5f) - this->m_cx) / this->m_cx;
	const float uv_dot = u * u + v * v;

	if ((t = 1.0f - this->m_kr4 * uv_dot) >= 0.0f) {
		d = 1.0f / (1.0f + sqrtf(t));
		result[0] = (u * d + 0.5f) * width - 0.5f, result[1] = (v * d + 0.5f) * height - 0.5f;
	}
	if ((t = 1.0f - this->m_kg4 * uv_dot) >= 0.0f) {
		d = 1.0f / (1.0f + sqrtf(t));
		result[2] = (u * d + 0.5f) * width - 0.5f, result[3] = (v * d + 0.5f) * height - 0.5f;
	}
	if ((t = 1.0f - this->m_kb4 * uv_dot) >= 0.0f) {
		d = 1.0f / (1.0f + sqrtf(t));
		result[4] = (u * d + 0.5f) * width - 0.5f, result[5] = (v * d + 0.5f) * height - 0.5f;
	}
	
}

bool ScreenLensDistortionOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
{
	rcti newInputValue;
	newInputValue.xmin = 0;
	newInputValue.ymin = 0;
	newInputValue.xmax = 2;
	newInputValue.ymax = 2;
	
	NodeOperation *operation = getInputOperation(1);
	if (operation->determineDependingAreaOfInterest(&newInputValue, readOperation, output) ) {
		return true;
	}

	operation = getInputOperation(2);
	if (operation->determineDependingAreaOfInterest(&newInputValue, readOperation, output) ) {
		return true;
	}

#define UPDATE_INPUT  { \
		newInput.xmin = min_ffff(newInput.xmin, coords[0], coords[2], coords[4]); \
		newInput.ymin = min_ffff(newInput.ymin, coords[1], coords[3], coords[5]); \
		newInput.xmax = max_ffff(newInput.xmax, coords[0], coords[2], coords[4]); \
		newInput.ymax = max_ffff(newInput.ymax, coords[1], coords[3], coords[5]); \
	} (void)0
	
	rcti newInput;
	const float margin = 2;
	float coords[6];
	if (m_valuesAvailable) {
		determineUV(coords, input->xmin, input->ymin);
		newInput.xmin = coords[0];
		newInput.ymin = coords[1];
		newInput.xmax = coords[0];
		newInput.ymax = coords[1];
		UPDATE_INPUT;
		determineUV(coords, input->xmin, input->ymax);
		UPDATE_INPUT;
		determineUV(coords, input->xmax, input->ymax);
		UPDATE_INPUT;
		determineUV(coords, input->xmax, input->ymin);
		UPDATE_INPUT;
	}
	else {
		determineUV(coords, input->xmin, input->ymin, 1.0f, 1.0f);
		newInput.xmin = coords[0];
		newInput.ymin = coords[1];
		newInput.xmax = coords[0];
		newInput.ymax = coords[1];
		UPDATE_INPUT;
		determineUV(coords, input->xmin, input->ymin, -1.0f, 1.0f);
		UPDATE_INPUT;
		
		determineUV(coords, input->xmin, input->ymax, -1.0f, 1.0f);
		UPDATE_INPUT;
		determineUV(coords, input->xmin, input->ymax, 1.0f, 1.0f);
		UPDATE_INPUT;
		
		determineUV(coords, input->xmax, input->ymax, -1.0f, 1.0f);
		UPDATE_INPUT;
		determineUV(coords, input->xmax, input->ymax, 1.0f, 1.0f);
		UPDATE_INPUT;
		
		determineUV(coords, input->xmax, input->ymin, -1.0f, 1.0f);
		UPDATE_INPUT;
		determineUV(coords, input->xmax, input->ymin, 1.0f, 1.0f);
		UPDATE_INPUT;
	}

#undef UPDATE_INPUT
	newInput.xmin -= margin;
	newInput.ymin -= margin;
	newInput.xmax += margin;
	newInput.ymax += margin;

	operation = getInputOperation(0);
	if (operation->determineDependingAreaOfInterest(&newInput, readOperation, output)) {
		return true;
	}
	return false;
}

void ScreenLensDistortionOperation::updateVariables(float distortion, float dispersion)
{
	this->m_kg = max_ff(min_ff(distortion, 1.0f), -0.999f);
	// smaller dispersion range for somewhat more control
	const float d = 0.25f * max_ff(min_ff(dispersion, 1.0f), 0.0f);
	this->m_kr = max_ff(min_ff((this->m_kg + d), 1.0f), -0.999f);
	this->m_kb = max_ff(min_ff((this->m_kg - d), 1.0f), -0.999f);
	this->m_maxk = max_fff(this->m_kr, this->m_kg, this->m_kb);
	this->m_sc = (this->m_data->fit && (this->m_maxk > 0.0f)) ? (1.0f / (1.0f + 2.0f * this->m_maxk)) :
	                                                            (1.0f / (1.0f +        this->m_maxk));
	this->m_drg = 4.0f * (this->m_kg - this->m_kr);
	this->m_dgb = 4.0f * (this->m_kb - this->m_kg);

	this->m_kr4 = this->m_kr * 4.0f;
	this->m_kg4 = this->m_kg * 4.0f;
	this->m_kb4 = this->m_kb * 4.0f;
}

void ScreenLensDistortionOperation::updateDispersionAndDistortion()
{
	if (this->m_valuesAvailable) return;
	
	this->lockMutex();
	if (!this->m_valuesAvailable) {
		float result[4];
		this->getInputSocketReader(1)->read(result, 0, 0, COM_PS_NEAREST);
		this->m_distortion = result[0];
		this->getInputSocketReader(2)->read(result, 0, 0, COM_PS_NEAREST);
		this->m_dispersion = result[0];
		updateVariables(this->m_distortion, this->m_dispersion);
		this->m_valuesAvailable = true;
	}
	this->unlockMutex();
}