Blender V5.3
filter.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_math_base_c.hh"
14#include "BLI_utildefines.hh"
15
16#include "IMB_filter.hh"
17#include "IMB_imbuf.hh"
18#include "IMB_imbuf_types.hh"
19
20namespace blender {
21
22static void filtcolum(uchar *point, int y, int skip)
23{
24 uint c1, c2, c3, error;
25 uchar *point2;
26
27 if (y > 1) {
28 c1 = c2 = *point;
29 point2 = point;
30 error = 2;
31 for (y--; y > 0; y--) {
32 point2 += skip;
33 c3 = *point2;
34 c1 += (c2 << 1) + c3 + error;
35 error = c1 & 3;
36 *point = c1 >> 2;
37 point = point2;
38 c1 = c2;
39 c2 = c3;
40 }
41 *point = (c1 + (c2 << 1) + c2 + error) >> 2;
42 }
43}
44
45static void filtcolumf(float *point, int y, int skip)
46{
47 float c1, c2, c3, *point2;
48
49 if (y > 1) {
50 c1 = c2 = *point;
51 point2 = point;
52 for (y--; y > 0; y--) {
53 point2 += skip;
54 c3 = *point2;
55 c1 += (c2 * 2) + c3;
56 *point = 0.25f * c1;
57 point = point2;
58 c1 = c2;
59 c2 = c3;
60 }
61 *point = 0.25f * (c1 + (c2 * 2) + c2);
62 }
63}
64
65void IMB_filtery(ImBuf *ibuf)
66{
67 uchar *point = ibuf->byte_data_for_write();
68 float *pointf = ibuf->float_data_for_write();
69
70 int x = ibuf->x;
71 int y = ibuf->y;
72 int skip = x << 2;
73
74 for (; x > 0; x--) {
75 if (point) {
76 if (ibuf->color_mode == ImColorMode::RGBA) {
77 filtcolum(point, y, skip);
78 }
79 point++;
80 filtcolum(point, y, skip);
81 point++;
82 filtcolum(point, y, skip);
83 point++;
84 filtcolum(point, y, skip);
85 point++;
86 }
87 if (pointf) {
88 if (ibuf->color_mode == ImColorMode::RGBA) {
89 filtcolumf(pointf, y, skip);
90 }
91 pointf++;
92 filtcolumf(pointf, y, skip);
93 pointf++;
94 filtcolumf(pointf, y, skip);
95 pointf++;
96 filtcolumf(pointf, y, skip);
97 pointf++;
98 }
99 }
100}
101
102void IMB_mask_filter_extend(char *mask, int width, int height)
103{
104 const char *row1, *row2, *row3;
105 int rowlen, x, y;
106 char *temprect;
107
108 rowlen = width;
109
110 /* make a copy, to prevent flooding */
111 temprect = MEM_dupalloc(mask);
112
113 for (y = 1; y <= height; y++) {
114 /* setup rows */
115 row1 = static_cast<char *>(temprect + (y - 2) * rowlen);
116 row2 = row1 + rowlen;
117 row3 = row2 + rowlen;
118 if (y == 1) {
119 row1 = row2;
120 }
121 else if (y == height) {
122 row3 = row2;
123 }
124
125 for (x = 0; x < rowlen; x++) {
126 if (mask[((y - 1) * rowlen) + x] == 0) {
127 if (*row1 || *row2 || *row3 || *(row1 + 1) || *(row3 + 1)) {
128 mask[((y - 1) * rowlen) + x] = FILTER_MASK_MARGIN;
129 }
130 else if ((x != rowlen - 1) && (*(row1 + 2) || *(row2 + 2) || *(row3 + 2))) {
131 mask[((y - 1) * rowlen) + x] = FILTER_MASK_MARGIN;
132 }
133 }
134
135 if (x != 0) {
136 row1++;
137 row2++;
138 row3++;
139 }
140 }
141 }
142
143 MEM_delete(temprect);
144}
145
146void IMB_mask_clear(ImBuf *ibuf, const char *mask, int val)
147{
148 int x, y;
149 if (float *float_data = ibuf->float_data_for_write()) {
150 for (x = 0; x < ibuf->x; x++) {
151 for (y = 0; y < ibuf->y; y++) {
152 if (mask[ibuf->x * y + x] == val) {
153 float *col = float_data + 4 * (ibuf->x * y + x);
154 col[0] = col[1] = col[2] = col[3] = 0.0f;
155 }
156 }
157 }
158 }
159 else {
160 uchar *byte_data = ibuf->byte_data_for_write();
161 /* char buffer */
162 for (x = 0; x < ibuf->x; x++) {
163 for (y = 0; y < ibuf->y; y++) {
164 if (mask[ibuf->x * y + x] == val) {
165 char *col = reinterpret_cast<char *>(byte_data + 4 * ibuf->x * y + x);
166 col[0] = col[1] = col[2] = col[3] = 0;
167 }
168 }
169 }
170 }
171}
172
173static int filter_make_index(const int x, const int y, const int w, const int h)
174{
175 if (x < 0 || x >= w || y < 0 || y >= h) {
176 return -1; /* return bad index */
177 }
178
179 return y * w + x;
180}
181
183 const void *buffer, const char *mask, const int index, const int depth, const bool is_float)
184{
185 int res = 0;
186
187 if (index >= 0) {
188 const int alpha_index = depth * index + (depth - 1);
189
190 if (mask != nullptr) {
191 res = mask[index] != 0 ? 1 : 0;
192 }
193 else if ((is_float && (static_cast<const float *>(buffer))[alpha_index] != 0.0f) ||
194 (!is_float && (static_cast<const uchar *>(buffer))[alpha_index] != 0))
195 {
196 res = 1;
197 }
198 }
199
200 return res;
201}
202
203void IMB_filter_extend(ImBuf *ibuf, char *mask, int filter)
204{
205 const int width = ibuf->x;
206 const int height = ibuf->y;
207 const int depth = 4; /* always 4 channels */
208 const int chsize = ibuf->float_data() ? sizeof(float) : sizeof(uchar);
209 const size_t bsize = size_t(width) * height * depth * chsize;
210 const bool is_float = (ibuf->float_data() != nullptr);
211 void *dstbuf = ibuf->float_data() ? static_cast<void *>(MEM_dupalloc(ibuf->float_data())) :
212 static_cast<void *>(MEM_dupalloc(ibuf->byte_data()));
213 char *dstmask = mask == nullptr ? nullptr : MEM_dupalloc(mask);
214 void *srcbuf = ibuf->float_data() ? static_cast<void *>(ibuf->float_data_for_write()) :
215 static_cast<void *>(ibuf->byte_data_for_write());
216 char *srcmask = mask;
217 int cannot_early_out = 1, r, n, k, i, j, c;
218 float weight[25];
219
220 /* build a weights buffer */
221 n = 1;
222
223#if 0
224 k = 0;
225 for (i = -n; i <= n; i++) {
226 for (j = -n; j <= n; j++) {
227 weight[k++] = sqrt(float(i) * i + j * j);
228 }
229 }
230#endif
231
232 weight[0] = 1;
233 weight[1] = 2;
234 weight[2] = 1;
235 weight[3] = 2;
236 weight[4] = 0;
237 weight[5] = 2;
238 weight[6] = 1;
239 weight[7] = 2;
240 weight[8] = 1;
241
242 /* run passes */
243 for (r = 0; cannot_early_out == 1 && r < filter; r++) {
244 int x, y;
245 cannot_early_out = 0;
246
247 for (y = 0; y < height; y++) {
248 for (x = 0; x < width; x++) {
249 const int index = filter_make_index(x, y, width, height);
250
251 /* only update unassigned pixels */
252 if (!check_pixel_assigned(srcbuf, srcmask, index, depth, is_float)) {
253 float tmp[4];
254 float wsum = 0;
255 float acc[4] = {0, 0, 0, 0};
256 k = 0;
257
259 srcbuf, srcmask, filter_make_index(x - 1, y, width, height), depth, is_float) ||
261 srcbuf, srcmask, filter_make_index(x + 1, y, width, height), depth, is_float) ||
263 srcbuf, srcmask, filter_make_index(x, y - 1, width, height), depth, is_float) ||
265 srcbuf, srcmask, filter_make_index(x, y + 1, width, height), depth, is_float))
266 {
267 for (i = -n; i <= n; i++) {
268 for (j = -n; j <= n; j++) {
269 if (i != 0 || j != 0) {
270 const int tmpindex = filter_make_index(x + i, y + j, width, height);
271
272 if (check_pixel_assigned(srcbuf, srcmask, tmpindex, depth, is_float)) {
273 if (is_float) {
274 for (c = 0; c < depth; c++) {
275 tmp[c] = (static_cast<const float *>(srcbuf))[depth * tmpindex + c];
276 }
277 }
278 else {
279 for (c = 0; c < depth; c++) {
280 tmp[c] = float((static_cast<const uchar *>(srcbuf))[depth * tmpindex + c]);
281 }
282 }
283
284 wsum += weight[k];
285
286 for (c = 0; c < depth; c++) {
287 acc[c] += weight[k] * tmp[c];
288 }
289 }
290 }
291 k++;
292 }
293 }
294
295 if (wsum != 0) {
296 for (c = 0; c < depth; c++) {
297 acc[c] /= wsum;
298 }
299
300 if (is_float) {
301 for (c = 0; c < depth; c++) {
302 (static_cast<float *>(dstbuf))[depth * index + c] = acc[c];
303 }
304 }
305 else {
306 for (c = 0; c < depth; c++) {
307 (static_cast<uchar *>(dstbuf))[depth * index + c] =
308 acc[c] > 255 ? 255 : (acc[c] < 0 ? 0 : uchar(roundf(acc[c])));
309 }
310 }
311
312 if (dstmask != nullptr) {
313 dstmask[index] = FILTER_MASK_MARGIN; /* assigned */
314 }
315 cannot_early_out = 1;
316 }
317 }
318 }
319 }
320 }
321
322 /* keep the original buffer up to date. */
323 memcpy(srcbuf, dstbuf, bsize);
324 if (dstmask != nullptr) {
325 memcpy(srcmask, dstmask, size_t(width) * height);
326 }
327 }
328
329 /* free memory */
330 MEM_delete_void(dstbuf);
331 if (dstmask != nullptr) {
332 MEM_delete(dstmask);
333 }
334}
335
336void IMB_premultiply_rect(uint8_t *rect, ImColorMode color_mode, int w, int h)
337{
338 uint8_t *cp;
339 int x, y, val;
340
341 if (color_mode == ImColorMode::RGB) { /* put alpha at 255 */
342 cp = rect;
343
344 for (y = 0; y < h; y++) {
345 for (x = 0; x < w; x++, cp += 4) {
346 cp[3] = 255;
347 }
348 }
349 }
350 else {
351 cp = rect;
352
353 for (y = 0; y < h; y++) {
354 for (x = 0; x < w; x++, cp += 4) {
355 val = cp[3];
356 cp[0] = (cp[0] * val) >> 8;
357 cp[1] = (cp[1] * val) >> 8;
358 cp[2] = (cp[2] * val) >> 8;
359 }
360 }
361 }
362}
363
364void IMB_premultiply_rect_float(float *rect_float, int channels, int w, int h)
365{
366 float val, *cp;
367 int x, y;
368
369 if (channels == 4) {
370 cp = rect_float;
371 for (y = 0; y < h; y++) {
372 for (x = 0; x < w; x++, cp += 4) {
373 val = cp[3];
374 cp[0] = cp[0] * val;
375 cp[1] = cp[1] * val;
376 cp[2] = cp[2] * val;
377 }
378 }
379 }
380}
381
383{
384 if (ibuf == nullptr) {
385 return;
386 }
387
388 if (uchar *byte_data = ibuf->byte_data_for_write()) {
389 IMB_premultiply_rect(byte_data, ibuf->color_mode, ibuf->x, ibuf->y);
390 }
391
392 if (float *float_data = ibuf->float_data_for_write()) {
393 IMB_premultiply_rect_float(float_data, ibuf->channels, ibuf->x, ibuf->y);
394 }
395}
396
397void IMB_unpremultiply_rect(uint8_t *rect, ImColorMode color_mode, int w, int h)
398{
399 uchar *cp;
400 int x, y;
401 float val;
402
403 if (color_mode == ImColorMode::RGB) { /* put alpha at 255 */
404 cp = rect;
405
406 for (y = 0; y < h; y++) {
407 for (x = 0; x < w; x++, cp += 4) {
408 cp[3] = 255;
409 }
410 }
411 }
412 else {
413 cp = rect;
414
415 for (y = 0; y < h; y++) {
416 for (x = 0; x < w; x++, cp += 4) {
417 val = cp[3] != 0 ? 1.0f / float(cp[3]) : 1.0f;
418 cp[0] = unit_float_to_uchar_clamp(cp[0] * val);
419 cp[1] = unit_float_to_uchar_clamp(cp[1] * val);
420 cp[2] = unit_float_to_uchar_clamp(cp[2] * val);
421 }
422 }
423 }
424}
425
426void IMB_unpremultiply_rect_float(float *rect_float, int channels, int w, int h)
427{
428 float val, *fp;
429 int x, y;
430
431 if (channels == 4) {
432 fp = rect_float;
433 for (y = 0; y < h; y++) {
434 for (x = 0; x < w; x++, fp += 4) {
435 val = fp[3] != 0.0f ? 1.0f / fp[3] : 1.0f;
436 fp[0] = fp[0] * val;
437 fp[1] = fp[1] * val;
438 fp[2] = fp[2] * val;
439 }
440 }
441 }
442}
443
445{
446 if (ibuf == nullptr) {
447 return;
448 }
449
450 if (uchar *byte_data = ibuf->byte_data_for_write()) {
451 IMB_unpremultiply_rect(byte_data, ibuf->color_mode, ibuf->x, ibuf->y);
452 }
453
454 if (float *float_data = ibuf->float_data_for_write()) {
455 IMB_unpremultiply_rect_float(float_data, ibuf->channels, ibuf->x, ibuf->y);
456 }
457}
458
459} // namespace blender
Function declarations for filter.cc.
#define FILTER_MASK_MARGIN
Definition IMB_imbuf.hh:346
Read Blender memory allocation and freeing.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define roundf(x)
BMesh Iterators.
void IMB_mask_filter_extend(char *mask, int width, int height)
Definition filter.cc:102
MINLINE unsigned char unit_float_to_uchar_clamp(float val)
unsigned int uint
Definition BKE_text.h:55
static int check_pixel_assigned(const void *buffer, const char *mask, const int index, const int depth, const bool is_float)
Definition filter.cc:182
void IMB_unpremultiply_alpha(ImBuf *ibuf)
Definition filter.cc:444
void IMB_mask_clear(ImBuf *ibuf, const char *mask, int val)
Definition filter.cc:146
void IMB_premultiply_rect_float(float *rect_float, int channels, int w, int h)
Definition filter.cc:364
static void filtcolum(uchar *point, int y, int skip)
Definition filter.cc:22
void MEM_delete_void(void *vmemh)
unsigned char uchar
Definition BKE_text.h:58
void IMB_premultiply_rect(uint8_t *rect, ImColorMode color_mode, int w, int h)
Definition filter.cc:336
unsigned char uint8_t
Definition BKE_text.h:79
static int filter_make_index(const int x, const int y, const int w, const int h)
Definition filter.cc:173
sqrt(x)+1/max(0
static void filtcolumf(float *point, int y, int skip)
Definition filter.cc:45
void IMB_filter_extend(ImBuf *ibuf, char *mask, int filter)
Definition filter.cc:203
void IMB_unpremultiply_rect_float(float *rect_float, int channels, int w, int h)
Definition filter.cc:426
void IMB_unpremultiply_rect(uint8_t *rect, ImColorMode color_mode, int w, int h)
Definition filter.cc:397
void IMB_filtery(ImBuf *ibuf)
Definition filter.cc:65
void IMB_premultiply_alpha(ImBuf *ibuf)
Definition filter.cc:382
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
ImColorMode color_mode
float * float_data_for_write()
Definition allocimbuf.cc:79