Blender V5.3
texture_image.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 <algorithm>
10#include <cfloat>
11#include <cmath>
12#include <cstring>
13#include <fcntl.h>
14#ifndef WIN32
15# include <unistd.h>
16#else
17# include <io.h>
18#endif
19
20#include "IMB_imbuf.hh"
21#include "IMB_imbuf_types.hh"
22
23#include "DNA_image_types.h"
24#include "DNA_texture_types.h"
25
26#include "BLI_math_vector_c.hh"
27#include "BLI_rect.hh"
28#include "BLI_threads.hh"
29#include "BLI_utildefines.hh"
30
31#include "BKE_image.hh"
32
33#include "RE_texture.h"
34
35#include "texture_common.h"
36
37namespace blender {
38
39static void boxsample(ImBuf *ibuf,
40 float minx,
41 float miny,
42 float maxx,
43 float maxy,
44 TexResult *texres,
45 const short imaprepeat,
46 const short imapextend);
47
48/* *********** IMAGEWRAPPING ****************** */
49
50/* x and y have to be checked for image size */
51static void ibuf_get_color(float col[4], const ImBuf *ibuf, int x, int y)
52{
53 const int64_t ofs = int64_t(y) * ibuf->x + x;
54
55 if (ibuf->float_data()) {
56 if (ibuf->channels == 4) {
57 const float *fp = ibuf->float_data() + 4 * ofs;
58 copy_v4_v4(col, fp);
59 }
60 else if (ibuf->channels == 3) {
61 const float *fp = ibuf->float_data() + 3 * ofs;
62 copy_v3_v3(col, fp);
63 col[3] = 1.0f;
64 }
65 else {
66 const float *fp = ibuf->float_data() + ofs;
67 col[0] = col[1] = col[2] = col[3] = *fp;
68 }
69 }
70 else {
71 const uchar *rect = ibuf->byte_data() + 4 * ofs;
72
73 col[0] = float(rect[0]) * (1.0f / 255.0f);
74 col[1] = float(rect[1]) * (1.0f / 255.0f);
75 col[2] = float(rect[2]) * (1.0f / 255.0f);
76 col[3] = float(rect[3]) * (1.0f / 255.0f);
77
78 /* Bytes are internally straight, however render pipeline seems to expect pre-multiplied. */
79 col[0] *= col[3];
80 col[1] *= col[3];
81 col[2] *= col[3];
82 }
83}
84
85int imagewrap(Tex *tex,
86 Image *ima,
87 const float texvec[3],
88 TexResult *texres,
89 ImagePool *pool,
90 const bool skip_load_image)
91{
92 float fx, fy;
93 int x, y, retval;
94 int xi, yi; /* original values */
95
96 texres->tin = texres->trgba[3] = texres->trgba[0] = texres->trgba[1] = texres->trgba[2] = 0.0f;
97
98 retval = TEX_RGB;
99
100 /* quick tests */
101 if (ima == nullptr) {
102 return retval;
103 }
104
105 /* hack for icon render */
106 if (skip_load_image && !BKE_image_has_loaded_ibuf(ima)) {
107 return retval;
108 }
109
110 ImageUser *iuser = &tex->iuser;
111 ImageUser local_iuser;
112 if (ima->source == IMA_SRC_TILED) {
113 /* tex->iuser might be shared by threads, so create a local copy. */
114 local_iuser = tex->iuser;
115 iuser = &local_iuser;
116
117 float new_uv[2];
118 iuser->tile = BKE_image_get_tile_from_pos(ima, texvec, new_uv, nullptr);
119 fx = new_uv[0];
120 fy = new_uv[1];
121 }
122 else {
123 fx = texvec[0];
124 fy = texvec[1];
125 }
126
127 ImBuf *ibuf = BKE_image_pool_acquire_ibuf(ima, iuser, pool);
128
129 ima->flag |= IMA_USED_FOR_RENDER;
130
131 if (ibuf == nullptr || (ibuf->byte_data() == nullptr && ibuf->float_data() == nullptr)) {
132 BKE_image_pool_release_ibuf(ima, ibuf, pool);
133 return retval;
134 }
135
136 /* setup mapping */
137 if (tex->imaflag & TEX_IMAROT) {
138 std::swap(fx, fy);
139 }
140
141 if (tex->extend == TEX_CHECKER) {
142 int xs, ys;
143
144 xs = int(floor(fx));
145 ys = int(floor(fy));
146 fx -= xs;
147 fy -= ys;
148
149 if ((tex->flag & TEX_CHECKER_ODD) == 0) {
150 if ((xs + ys) & 1) {
151 /* pass */
152 }
153 else {
154 if (ima) {
155 BKE_image_pool_release_ibuf(ima, ibuf, pool);
156 }
157 return retval;
158 }
159 }
160 if ((tex->flag & TEX_CHECKER_EVEN) == 0) {
161 if ((xs + ys) & 1) {
162 if (ima) {
163 BKE_image_pool_release_ibuf(ima, ibuf, pool);
164 }
165 return retval;
166 }
167 }
168 /* scale around center, (0.5, 0.5) */
169 if (tex->checkerdist < 1.0f) {
170 fx = (fx - 0.5f) / (1.0f - tex->checkerdist) + 0.5f;
171 fy = (fy - 0.5f) / (1.0f - tex->checkerdist) + 0.5f;
172 }
173 }
174
175 x = xi = int(floorf(fx * ibuf->x));
176 y = yi = int(floorf(fy * ibuf->y));
177
178 if (tex->extend == TEX_CLIPCUBE) {
179 if (x < 0 || y < 0 || x >= ibuf->x || y >= ibuf->y || texvec[2] < -1.0f || texvec[2] > 1.0f) {
180 if (ima) {
181 BKE_image_pool_release_ibuf(ima, ibuf, pool);
182 }
183 return retval;
184 }
185 }
186 else if (ELEM(tex->extend, TEX_CLIP, TEX_CHECKER)) {
187 if (x < 0 || y < 0 || x >= ibuf->x || y >= ibuf->y) {
188 if (ima) {
189 BKE_image_pool_release_ibuf(ima, ibuf, pool);
190 }
191 return retval;
192 }
193 }
194 else {
195 if (tex->extend == TEX_EXTEND) {
196 if (x >= ibuf->x) {
197 x = ibuf->x - 1;
198 }
199 else if (x < 0) {
200 x = 0;
201 }
202 }
203 else {
204 x = x % ibuf->x;
205 if (x < 0) {
206 x += ibuf->x;
207 }
208 }
209 if (tex->extend == TEX_EXTEND) {
210 if (y >= ibuf->y) {
211 y = ibuf->y - 1;
212 }
213 else if (y < 0) {
214 y = 0;
215 }
216 }
217 else {
218 y = y % ibuf->y;
219 if (y < 0) {
220 y += ibuf->y;
221 }
222 }
223 }
224
225 /* Keep this before interpolation #29761. */
226 if (ima) {
227 if ((tex->imaflag & TEX_USEALPHA) && (ima->alpha_mode != IMA_ALPHA_IGNORE)) {
228 if ((tex->imaflag & TEX_CALCALPHA) == 0) {
229 texres->talpha = true;
230 }
231 }
232 }
233
234 /* interpolate */
235 if (tex->imaflag & TEX_INTERPOL) {
236 float filterx, filtery;
237 filterx = (0.5f * tex->filtersize) / ibuf->x;
238 filtery = (0.5f * tex->filtersize) / ibuf->y;
239
240 /* Important that this value is wrapped #27782. */
241 if (tex->extend == TEX_EXTEND) {
242 CLAMP(fx, 0.0f, 1.0f);
243 CLAMP(fy, 0.0f, 1.0f);
244 }
245 else {
246 /* this applies the modifications made by the checks above,
247 * back to the floating point values */
248 fx -= float(xi - x) / float(ibuf->x);
249 fy -= float(yi - y) / float(ibuf->y);
250 }
251 boxsample(ibuf,
252 fx - filterx,
253 fy - filtery,
254 fx + filterx,
255 fy + filtery,
256 texres,
257 (tex->extend == TEX_REPEAT),
258 (tex->extend == TEX_EXTEND));
259 }
260 else { /* no filtering */
261 ibuf_get_color(texres->trgba, ibuf, x, y);
262 }
263
264 if (texres->talpha) {
265 texres->tin = texres->trgba[3];
266 }
267 else if (tex->imaflag & TEX_CALCALPHA) {
268 texres->trgba[3] = texres->tin = std::max(
269 {texres->trgba[0], texres->trgba[1], texres->trgba[2]});
270 }
271 else {
272 texres->trgba[3] = texres->tin = 1.0;
273 }
274
275 if (tex->flag & TEX_NEGALPHA) {
276 texres->trgba[3] = 1.0f - texres->trgba[3];
277 }
278
279 /* De-pre-multiply, this is being pre-multiplied in #shade_input_do_shade()
280 * do not de-pre-multiply for generated alpha, it is already in straight. */
281 if (texres->trgba[3] != 1.0f && texres->trgba[3] > 1e-4f && !(tex->imaflag & TEX_CALCALPHA)) {
282 fx = 1.0f / texres->trgba[3];
283 texres->trgba[0] *= fx;
284 texres->trgba[1] *= fx;
285 texres->trgba[2] *= fx;
286 }
287
288 if (ima) {
289 BKE_image_pool_release_ibuf(ima, ibuf, pool);
290 }
291
293
294 return retval;
295}
296
297static void clipx_rctf_swap(rctf *stack, short *count, float x1, float x2)
298{
299 rctf *rf, *newrct;
300 short a;
301
302 a = *count;
303 rf = stack;
304 for (; a > 0; a--) {
305 if (rf->xmin < x1) {
306 if (rf->xmax < x1) {
307 rf->xmin += (x2 - x1);
308 rf->xmax += (x2 - x1);
309 }
310 else {
311 rf->xmax = std::min(rf->xmax, x2);
312 newrct = stack + *count;
313 (*count)++;
314
315 newrct->xmax = x2;
316 newrct->xmin = rf->xmin + (x2 - x1);
317 newrct->ymin = rf->ymin;
318 newrct->ymax = rf->ymax;
319
320 if (newrct->xmin == newrct->xmax) {
321 (*count)--;
322 }
323
324 rf->xmin = x1;
325 }
326 }
327 else if (rf->xmax > x2) {
328 if (rf->xmin > x2) {
329 rf->xmin -= (x2 - x1);
330 rf->xmax -= (x2 - x1);
331 }
332 else {
333 rf->xmin = std::max(rf->xmin, x1);
334 newrct = stack + *count;
335 (*count)++;
336
337 newrct->xmin = x1;
338 newrct->xmax = rf->xmax - (x2 - x1);
339 newrct->ymin = rf->ymin;
340 newrct->ymax = rf->ymax;
341
342 if (newrct->xmin == newrct->xmax) {
343 (*count)--;
344 }
345
346 rf->xmax = x2;
347 }
348 }
349 rf++;
350 }
351}
352
353static void clipy_rctf_swap(rctf *stack, short *count, float y1, float y2)
354{
355 rctf *rf, *newrct;
356 short a;
357
358 a = *count;
359 rf = stack;
360 for (; a > 0; a--) {
361 if (rf->ymin < y1) {
362 if (rf->ymax < y1) {
363 rf->ymin += (y2 - y1);
364 rf->ymax += (y2 - y1);
365 }
366 else {
367 rf->ymax = std::min(rf->ymax, y2);
368 newrct = stack + *count;
369 (*count)++;
370
371 newrct->ymax = y2;
372 newrct->ymin = rf->ymin + (y2 - y1);
373 newrct->xmin = rf->xmin;
374 newrct->xmax = rf->xmax;
375
376 if (newrct->ymin == newrct->ymax) {
377 (*count)--;
378 }
379
380 rf->ymin = y1;
381 }
382 }
383 else if (rf->ymax > y2) {
384 if (rf->ymin > y2) {
385 rf->ymin -= (y2 - y1);
386 rf->ymax -= (y2 - y1);
387 }
388 else {
389 rf->ymin = std::max(rf->ymin, y1);
390 newrct = stack + *count;
391 (*count)++;
392
393 newrct->ymin = y1;
394 newrct->ymax = rf->ymax - (y2 - y1);
395 newrct->xmin = rf->xmin;
396 newrct->xmax = rf->xmax;
397
398 if (newrct->ymin == newrct->ymax) {
399 (*count)--;
400 }
401
402 rf->ymax = y2;
403 }
404 }
405 rf++;
406 }
407}
408
409static float square_rctf(const rctf *rf)
410{
411 float x, y;
412
413 x = BLI_rctf_size_x(rf);
414 y = BLI_rctf_size_y(rf);
415 return x * y;
416}
417
418static float clipx_rctf(rctf *rf, float x1, float x2)
419{
420 float size;
421
422 size = BLI_rctf_size_x(rf);
423
424 rf->xmin = std::max(rf->xmin, x1);
425 rf->xmax = std::min(rf->xmax, x2);
426 if (rf->xmin > rf->xmax) {
427 rf->xmin = rf->xmax;
428 return 0.0;
429 }
430 if (size != 0.0f) {
431 return BLI_rctf_size_x(rf) / size;
432 }
433 return 1.0;
434}
435
436static float clipy_rctf(rctf *rf, float y1, float y2)
437{
438 float size;
439
440 size = BLI_rctf_size_y(rf);
441
442 rf->ymin = std::max(rf->ymin, y1);
443 rf->ymax = std::min(rf->ymax, y2);
444
445 if (rf->ymin > rf->ymax) {
446 rf->ymin = rf->ymax;
447 return 0.0;
448 }
449 if (size != 0.0f) {
450 return BLI_rctf_size_y(rf) / size;
451 }
452 return 1.0;
453}
454
455static void boxsampleclip(ImBuf *ibuf, const rctf *rf, TexResult *texres)
456{
457 /* Sample box, is clipped already, and minx etc. have been set at ibuf size.
458 * Enlarge with anti-aliased edges of the pixels. */
459
460 float muly, mulx, div, col[4];
461 int x, y, startx, endx, starty, endy;
462
463 startx = int(floor(rf->xmin));
464 endx = int(floor(rf->xmax));
465 starty = int(floor(rf->ymin));
466 endy = int(floor(rf->ymax));
467
468 startx = std::max(startx, 0);
469 starty = std::max(starty, 0);
470 if (endx >= ibuf->x) {
471 endx = ibuf->x - 1;
472 }
473 if (endy >= ibuf->y) {
474 endy = ibuf->y - 1;
475 }
476
477 if (starty == endy && startx == endx) {
478 ibuf_get_color(texres->trgba, ibuf, startx, starty);
479 }
480 else {
481 div = texres->trgba[0] = texres->trgba[1] = texres->trgba[2] = texres->trgba[3] = 0.0;
482 for (y = starty; y <= endy; y++) {
483
484 muly = 1.0;
485
486 if (starty == endy) {
487 /* pass */
488 }
489 else {
490 if (y == starty) {
491 muly = 1.0f - (rf->ymin - y);
492 }
493 if (y == endy) {
494 muly = (rf->ymax - y);
495 }
496 }
497
498 if (startx == endx) {
499 mulx = muly;
500
501 ibuf_get_color(col, ibuf, startx, y);
502 madd_v4_v4fl(texres->trgba, col, mulx);
503 div += mulx;
504 }
505 else {
506 for (x = startx; x <= endx; x++) {
507 mulx = muly;
508 if (x == startx) {
509 mulx *= 1.0f - (rf->xmin - x);
510 }
511 if (x == endx) {
512 mulx *= (rf->xmax - x);
513 }
514
515 ibuf_get_color(col, ibuf, x, y);
516 /* TODO(jbakker): No need to do manual optimization. Branching is slower than multiplying
517 * with 1. */
518 if (mulx == 1.0f) {
519 add_v4_v4(texres->trgba, col);
520 div += 1.0f;
521 }
522 else {
523 madd_v4_v4fl(texres->trgba, col, mulx);
524 div += mulx;
525 }
526 }
527 }
528 }
529
530 if (div != 0.0f) {
531 div = 1.0f / div;
532 mul_v4_fl(texres->trgba, div);
533 }
534 else {
535 zero_v4(texres->trgba);
536 }
537 }
538}
539
540static void boxsample(ImBuf *ibuf,
541 float minx,
542 float miny,
543 float maxx,
544 float maxy,
545 TexResult *texres,
546 const short imaprepeat,
547 const short imapextend)
548{
549 /* Sample box, performs clip. minx etc are in range 0.0 - 1.0 .
550 * Enlarge with anti-aliased edges of pixels.
551 * If variable 'imaprepeat' has been set, the
552 * clipped-away parts are sampled as well.
553 */
554 /* NOTE: actually minx etc isn't in the proper range...
555 * this due to filter size and offset vectors for bump. */
556 /* NOTE: talpha must be initialized. */
557 /* NOTE: even when 'imaprepeat' is set, this can only repeat once in any direction.
558 * the point which min/max is derived from is assumed to be wrapped. */
559 TexResult texr;
560 rctf *rf, stack[8];
561 float opp, tot, alphaclip = 1.0;
562 short count = 1;
563
564 rf = stack;
565 rf->xmin = minx * (ibuf->x);
566 rf->xmax = maxx * (ibuf->x);
567 rf->ymin = miny * (ibuf->y);
568 rf->ymax = maxy * (ibuf->y);
569
570 texr.talpha = texres->talpha; /* is read by boxsample_clip */
571
572 if (imapextend) {
573 CLAMP(rf->xmin, 0.0f, ibuf->x - 1);
574 CLAMP(rf->xmax, 0.0f, ibuf->x - 1);
575 }
576 else if (imaprepeat) {
577 clipx_rctf_swap(stack, &count, 0.0, float(ibuf->x));
578 }
579 else {
580 alphaclip = clipx_rctf(rf, 0.0, float(ibuf->x));
581
582 if (alphaclip <= 0.0f) {
583 texres->trgba[0] = texres->trgba[2] = texres->trgba[1] = texres->trgba[3] = 0.0;
584 return;
585 }
586 }
587
588 if (imapextend) {
589 CLAMP(rf->ymin, 0.0f, ibuf->y - 1);
590 CLAMP(rf->ymax, 0.0f, ibuf->y - 1);
591 }
592 else if (imaprepeat) {
593 clipy_rctf_swap(stack, &count, 0.0, float(ibuf->y));
594 }
595 else {
596 alphaclip *= clipy_rctf(rf, 0.0, float(ibuf->y));
597
598 if (alphaclip <= 0.0f) {
599 texres->trgba[0] = texres->trgba[2] = texres->trgba[1] = texres->trgba[3] = 0.0;
600 return;
601 }
602 }
603
604 if (count > 1) {
605 tot = texres->trgba[0] = texres->trgba[2] = texres->trgba[1] = texres->trgba[3] = 0.0;
606 while (count--) {
607 boxsampleclip(ibuf, rf, &texr);
608
609 opp = square_rctf(rf);
610 tot += opp;
611
612 texres->trgba[0] += opp * texr.trgba[0];
613 texres->trgba[1] += opp * texr.trgba[1];
614 texres->trgba[2] += opp * texr.trgba[2];
615 if (texres->talpha) {
616 texres->trgba[3] += opp * texr.trgba[3];
617 }
618 rf++;
619 }
620 if (tot != 0.0f) {
621 texres->trgba[0] /= tot;
622 texres->trgba[1] /= tot;
623 texres->trgba[2] /= tot;
624 if (texres->talpha) {
625 texres->trgba[3] /= tot;
626 }
627 }
628 }
629 else {
630 boxsampleclip(ibuf, rf, texres);
631 }
632
633 if (texres->talpha == 0) {
634 texres->trgba[3] = 1.0;
635 }
636
637 if (alphaclip != 1.0f) {
638 /* Pre-multiply it all. */
639 texres->trgba[0] *= alphaclip;
640 texres->trgba[1] *= alphaclip;
641 texres->trgba[2] *= alphaclip;
642 texres->trgba[3] *= alphaclip;
643 }
644}
645
647 Image *ima, float fx, float fy, float dx, float dy, float result[4], ImagePool *pool)
648{
649 TexResult texres;
650 ImBuf *ibuf = BKE_image_pool_acquire_ibuf(ima, nullptr, pool);
651
652 if (ibuf == nullptr) [[unlikely]] {
654 return;
655 }
656
657 texres.talpha = true; /* boxsample expects to be initialized */
658 boxsample(ibuf, fx, fy, fx + dx, fy + dy, &texres, 0, 1);
659 copy_v4_v4(result, texres.trgba);
660
661 ima->flag |= IMA_USED_FOR_RENDER;
662
663 BKE_image_pool_release_ibuf(ima, ibuf, pool);
664}
665
666} // namespace blender
#define CLAMP(a, b, c)
#define ELEM(...)
#define floor
BMesh Iterators.
int BKE_image_get_tile_from_pos(Image *ima, const float uv[2], float r_uv[2], float r_ofs[2])
MINLINE void madd_v4_v4fl(float r[4], const float a[4], float f)
static void clipx_rctf_swap(rctf *stack, short *count, float x1, float x2)
void image_sample(struct Image *ima, float fx, float fy, float dx, float dy, float result[4], struct ImagePool *pool)
__int64 int64_t
Definition BKE_text.h:90
@ IMA_USED_FOR_RENDER
MINLINE void copy_v3_v3(float r[3], const float a[3])
bool BKE_image_has_loaded_ibuf(Image *image)
static float square_rctf(const rctf *rf)
MINLINE void zero_v4(float r[4])
unsigned char uchar
Definition BKE_text.h:58
static void boxsampleclip(ImBuf *ibuf, const rctf *rf, TexResult *texres)
MINLINE void add_v4_v4(float r[4], const float a[4])
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
static float clipx_rctf(rctf *rf, float x1, float x2)
static void ibuf_get_color(float col[4], const ImBuf *ibuf, int x, int y)
static void boxsample(ImBuf *ibuf, float minx, float miny, float maxx, float maxy, TexResult *texres, const short imaprepeat, const short imapextend)
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
MINLINE void copy_v4_v4(float r[4], const float a[4])
static void clipy_rctf_swap(rctf *stack, short *count, float y1, float y2)
MINLINE void mul_v4_fl(float r[4], float f)
ImBuf * BKE_image_pool_acquire_ibuf(Image *ima, ImageUser *iuser, ImagePool *pool)
static float clipy_rctf(rctf *rf, float y1, float y2)
void BKE_image_pool_release_ibuf(Image *ima, ImBuf *ibuf, ImagePool *pool)
int imagewrap(struct Tex *tex, struct Image *ima, const float texvec[3], struct TexResult *texres, struct ImagePool *pool, bool skip_load_image)
#define floorf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const uint8_t * byte_data() const
const float * float_data() const
struct ImageUser iuser
eTex_ImaFlag imaflag
eTex_Extend extend
#define BRICONTRGB