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

math_interp.c « intern « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5a9e8244a469832543abec6f01034a8210fa9191 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * ***** 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2012 by Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Sergey Sharybin
 *
 * ***** END GPL LICENSE BLOCK *****
 *
 */

#include <math.h>

#include "BLI_math.h"

/**************************************************************************
 *                            INTERPOLATIONS
 *
 * Reference and docs:
 * http://wiki.blender.org/index.php/User:Damiles#Interpolations_Algorithms
 ***************************************************************************/

/* BICUBIC Interpolation functions
 *  More info: http://wiki.blender.org/index.php/User:Damiles#Bicubic_pixel_interpolation
 * function assumes out to be zero'ed, only does RGBA */

static float P(float k)
{
	float p1, p2, p3, p4;
	p1 = MAX2(k + 2.0f, 0);
	p2 = MAX2(k + 1.0f, 0);
	p3 = MAX2(k, 0);
	p4 = MAX2(k - 1.0f, 0);
	return (float)(1.0f / 6.0f) * (p1 * p1 * p1 - 4.0f * p2 * p2 * p2 + 6.0f * p3 * p3 * p3 - 4.0f * p4 * p4 * p4);
}


#if 0
/* older, slower function, works the same as above */
static float P(float k)
{
	return (float)(1.0f / 6.0f) * (pow(MAX2(k + 2.0f, 0), 3.0f) - 4.0f * pow(MAX2(k + 1.0f, 0), 3.0f) + 6.0f * pow(MAX2(k, 0), 3.0f) - 4.0f * pow(MAX2(k - 1.0f, 0), 3.0f));
}
#endif

static void vector_from_float(const float *data, float vector[4], int components)
{
	if (components == 1) {
		vector[0] = data[0];
	}
	else if (components == 3) {
		copy_v3_v3(vector, data);
	}
	else {
		copy_v4_v4(vector, data);
	}
}

static void vector_from_byte(const unsigned char *data, float vector[4], int components)
{
	if (components == 1) {
		vector[0] = data[0];
	}
	else if (components == 3) {
		vector[0] = data[0];
		vector[1] = data[1];
		vector[2] = data[2];
	}
	else {
		vector[0] = data[0];
		vector[1] = data[1];
		vector[2] = data[2];
		vector[3] = data[3];
	}
}

/* BICUBIC INTERPOLATION */
BLI_INLINE void bicubic_interpolation(const unsigned char *byte_buffer, const float *float_buffer,
                                      unsigned char *byte_output, float *float_output, int width, int height,
                                      int components, float u, float v)
{
	int i, j, n, m, x1, y1;
	float a, b, w, wx, wy[4], out[4];

	/* sample area entirely outside image? */
	if (ceil(u) < 0 || floor(u) > width - 1 || ceil(v) < 0 || floor(v) > height - 1) {
		return;
	}

	i = (int)floor(u);
	j = (int)floor(v);
	a = u - i;
	b = v - j;

	zero_v4(out);

/* Optimized and not so easy to read */

	/* avoid calling multiple times */
	wy[0] = P(b - (-1));
	wy[1] = P(b -  0);
	wy[2] = P(b -  1);
	wy[3] = P(b -  2);

	for (n = -1; n <= 2; n++) {
		x1 = i + n;
		CLAMP(x1, 0, width - 1);
		wx = P(n - a);
		for (m = -1; m <= 2; m++) {
			float data[4];

			y1 = j + m;
			CLAMP(y1, 0, height - 1);
			/* normally we could do this */
			/* w = P(n-a) * P(b-m); */
			/* except that would call P() 16 times per pixel therefor pow() 64 times, better precalc these */
			w = wx * wy[m + 1];

			if (float_output) {
				const float *float_data = float_buffer + width * y1 * components + components * x1;

				vector_from_float(float_data, data, components);
			}
			else {
				const unsigned char *byte_data = byte_buffer + width * y1 * components + components * x1;

				vector_from_byte(byte_data, data, components);
			}

			if (components == 1) {
				out[0] += data[0] * w;
			}
			else if (components == 3) {
				out[0] += data[0] * w;
				out[1] += data[1] * w;
				out[2] += data[2] * w;
			}
			else {
				out[0] += data[0] * w;
				out[1] += data[1] * w;
				out[2] += data[2] * w;
				out[3] += data[3] * w;
			}
		}
	}

/* Done with optimized part */

#if 0
	/* older, slower function, works the same as above */
	for (n = -1; n <= 2; n++) {
		for (m = -1; m <= 2; m++) {
			x1 = i + n;
			y1 = j + m;
			if (x1 > 0 && x1 < width && y1 > 0 && y1 < height) {
				float data[4];

				if (float_output) {
					const float *float_data = float_buffer + width * y1 * components + components * x1;

					vector_from_float(float_data, data, components);
				}
				else {
					const unsigned char *byte_data = byte_buffer + width * y1 * components + components * x1;

					vector_from_byte(byte_data, data, components);
				}

				if (components == 1) {
					out[0] += data[0] * P(n - a) * P(b - m);
				}
				else if (components == 3) {
					out[0] += data[0] * P(n - a) * P(b - m);
					out[1] += data[1] * P(n - a) * P(b - m);
					out[2] += data[2] * P(n - a) * P(b - m);
				}
				else {
					out[0] += data[0] * P(n - a) * P(b - m);
					out[1] += data[1] * P(n - a) * P(b - m);
					out[2] += data[2] * P(n - a) * P(b - m);
					out[3] += data[3] * P(n - a) * P(b - m);
				}
			}
		}
	}
#endif

	if (float_output) {
		if (components == 1) {
			float_output[0] = out[0];
		}
		else if (components == 3) {
			copy_v3_v3(float_output, out);
		}
		else {
			copy_v4_v4(float_output, out);
		}
	}
	else {
		if (components == 1) {
			byte_output[0] = out[0];
		}
		else if (components == 3) {
			byte_output[0] = out[0];
			byte_output[1] = out[1];
			byte_output[2] = out[2];
		}
		else {
			byte_output[0] = out[0];
			byte_output[1] = out[1];
			byte_output[2] = out[2];
			byte_output[3] = out[3];
		}
	}
}

void BLI_bicubic_interpolation_fl(const float *buffer, float *output, int width, int height,
                                  int components, float u, float v)
{
	bicubic_interpolation(NULL, buffer, NULL, output, width, height, components, u, v);
}

void BLI_bicubic_interpolation_char(const unsigned char *buffer, unsigned char *output, int width, int height,
                                    int components, float u, float v)
{
	bicubic_interpolation(buffer, NULL, output, NULL, width, height, components, u, v);
}

/* BILINEAR INTERPOLATION */
BLI_INLINE void bilinear_interpolation(const unsigned char *byte_buffer, const float *float_buffer,
                                       unsigned char *byte_output, float *float_output, int width, int height,
                                       int components, float u, float v)
{
	float a, b;
	float a_b, ma_b, a_mb, ma_mb;
	int y1, y2, x1, x2;

	/* ImBuf in must have a valid rect or rect_float, assume this is already checked */

	x1 = (int)floor(u);
	x2 = (int)ceil(u);
	y1 = (int)floor(v);
	y2 = (int)ceil(v);

	/* sample area entirely outside image? */
	if (x2 < 0 || x1 > width - 1 || y2 < 0 || y1 > height - 1) {
		return;
	}

	if (float_output) {
		const float *row1, *row2, *row3, *row4;
		float empty[4] = {0.0f, 0.0f, 0.0f, 0.0f};

		/* sample including outside of edges of image */
		if (x1 < 0 || y1 < 0) row1 = empty;
		else row1 = float_buffer + width * y1 * components + components * x1;

		if (x1 < 0 || y2 > height - 1) row2 = empty;
		else row2 = float_buffer + width * y2 * components + components * x1;

		if (x2 > width - 1 || y1 < 0) row3 = empty;
		else row3 = float_buffer + width * y1 * components + components * x2;

		if (x2 > width - 1 || y2 > height - 1) row4 = empty;
		else row4 = float_buffer + width * y2 * components + components * x2;

		a = u - floorf(u);
		b = v - floorf(v);
		a_b = a * b; ma_b = (1.0f - a) * b; a_mb = a * (1.0f - b); ma_mb = (1.0f - a) * (1.0f - b);

		if (components == 1) {
			float_output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
		}
		else if (components == 3) {
			float_output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
			float_output[1] = ma_mb * row1[1] + a_mb * row3[1] + ma_b * row2[1] + a_b * row4[1];
			float_output[2] = ma_mb * row1[2] + a_mb * row3[2] + ma_b * row2[2] + a_b * row4[2];
		}
		else {
			float_output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
			float_output[1] = ma_mb * row1[1] + a_mb * row3[1] + ma_b * row2[1] + a_b * row4[1];
			float_output[2] = ma_mb * row1[2] + a_mb * row3[2] + ma_b * row2[2] + a_b * row4[2];
			float_output[3] = ma_mb * row1[3] + a_mb * row3[3] + ma_b * row2[3] + a_b * row4[3];
		}
	}
	else {
		const unsigned char *row1, *row2, *row3, *row4;
		unsigned char empty[4] = {0, 0, 0, 0};

		/* sample including outside of edges of image */
		if (x1 < 0 || y1 < 0) row1 = empty;
		else row1 = byte_buffer + width * y1 * components + components * x1;

		if (x1 < 0 || y2 > height - 1) row2 = empty;
		else row2 = byte_buffer + width * y2 * components + components * x1;

		if (x2 > width - 1 || y1 < 0) row3 = empty;
		else row3 = byte_buffer + width * y1 * components + components * x2;

		if (x2 > width - 1 || y2 > height - 1) row4 = empty;
		else row4 = byte_buffer + width * y2 * components + components * x2;

		a = u - floorf(u);
		b = v - floorf(v);
		a_b = a * b; ma_b = (1.0f - a) * b; a_mb = a * (1.0f - b); ma_mb = (1.0f - a) * (1.0f - b);

		if (components == 1) {
			byte_output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
		}
		else if (components == 3) {
			byte_output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
			byte_output[1] = ma_mb * row1[1] + a_mb * row3[1] + ma_b * row2[1] + a_b * row4[1];
			byte_output[2] = ma_mb * row1[2] + a_mb * row3[2] + ma_b * row2[2] + a_b * row4[2];
		}
		else {
			byte_output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
			byte_output[1] = ma_mb * row1[1] + a_mb * row3[1] + ma_b * row2[1] + a_b * row4[1];
			byte_output[2] = ma_mb * row1[2] + a_mb * row3[2] + ma_b * row2[2] + a_b * row4[2];
			byte_output[3] = ma_mb * row1[3] + a_mb * row3[3] + ma_b * row2[3] + a_b * row4[3];
		}
	}
}

void BLI_bilinear_interpolation_fl(const float *buffer, float *output, int width, int height,
                                   int components, float u, float v)
{
	bilinear_interpolation(NULL, buffer, NULL, output, width, height, components, u, v);
}

void BLI_bilinear_interpolation_char(const unsigned char *buffer, unsigned char *output, int width, int height,
                                     int components, float u, float v)
{
	bilinear_interpolation(buffer, NULL, output, NULL, width, height, components, u, v);
}