Blender V4.3
COM_MapUVOperation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7namespace blender::compositor {
8
10{
14 alpha_ = 0.0f;
15 nearest_neighbour_ = false;
17 set_canvas_input_index(UV_INPUT_INDEX);
18}
19
21{
22 NodeOperation *image_input = get_input_operation(IMAGE_INPUT_INDEX);
23 image_width_ = image_input->get_width();
24 image_height_ = image_input->get_height();
25
26 NodeOperation *uv_input = get_input_operation(UV_INPUT_INDEX);
27 uv_width_ = uv_input->get_width();
28 uv_height_ = uv_input->get_height();
29}
30
31bool MapUVOperation::read_uv(float x, float y, float &r_u, float &r_v, float &r_alpha)
32{
33 if (x < 0.0f || x >= uv_width_ || y < 0.0f || y >= uv_height_) {
34 r_u = 0.0f;
35 r_v = 0.0f;
36 r_alpha = 0.0f;
37 return false;
38 }
39
40 float vector[3];
41 uv_input_read_fn_(x, y, vector);
42 r_u = vector[0] * image_width_;
43 r_v = vector[1] * image_height_;
44 r_alpha = vector[2];
45 return true;
46}
47
49 float r_uv[2],
50 float r_deriv[2][2],
51 float &r_alpha)
52{
53 float uv[2], alpha; /* temporary variables for derivative estimation */
54 int num;
55
56 read_uv(xy[0], xy[1], r_uv[0], r_uv[1], r_alpha);
57
58 /* Estimate partial derivatives using 1-pixel offsets */
59 const float epsilon[2] = {1.0f, 1.0f};
60
61 zero_v2(r_deriv[0]);
62 zero_v2(r_deriv[1]);
63
64 num = 0;
65 if (read_uv(xy[0] + epsilon[0], xy[1], uv[0], uv[1], alpha)) {
66 r_deriv[0][0] += uv[0] - r_uv[0];
67 r_deriv[1][0] += uv[1] - r_uv[1];
68 num++;
69 }
70 if (read_uv(xy[0] - epsilon[0], xy[1], uv[0], uv[1], alpha)) {
71 r_deriv[0][0] += r_uv[0] - uv[0];
72 r_deriv[1][0] += r_uv[1] - uv[1];
73 num++;
74 }
75 if (num > 0) {
76 float numinv = 1.0f / float(num);
77 r_deriv[0][0] *= numinv;
78 r_deriv[1][0] *= numinv;
79 }
80
81 num = 0;
82 if (read_uv(xy[0], xy[1] + epsilon[1], uv[0], uv[1], alpha)) {
83 r_deriv[0][1] += uv[0] - r_uv[0];
84 r_deriv[1][1] += uv[1] - r_uv[1];
85 num++;
86 }
87 if (read_uv(xy[0], xy[1] - epsilon[1], uv[0], uv[1], alpha)) {
88 r_deriv[0][1] += r_uv[0] - uv[0];
89 r_deriv[1][1] += r_uv[1] - uv[1];
90 num++;
91 }
92 if (num > 0) {
93 float numinv = 1.0f / float(num);
94 r_deriv[0][1] *= numinv;
95 r_deriv[1][1] *= numinv;
96 }
97}
98
99void MapUVOperation::get_area_of_interest(const int input_idx,
100 const rcti &output_area,
101 rcti &r_input_area)
102{
103 switch (input_idx) {
104 case IMAGE_INPUT_INDEX: {
105 r_input_area = get_input_operation(IMAGE_INPUT_INDEX)->get_canvas();
106 break;
107 }
108 case UV_INPUT_INDEX: {
109 r_input_area = output_area;
111 break;
112 }
113 }
114}
115
117 const rcti & /*area*/,
119{
120 const MemoryBuffer *uv_input = inputs[UV_INPUT_INDEX];
121 uv_input_read_fn_ = [=](float x, float y, float *out) {
122 uv_input->read_elem_bilinear(x, y, out);
123 };
124}
125
127 const rcti &area,
129{
130 const MemoryBuffer *input_image = inputs[IMAGE_INPUT_INDEX];
131 for (BuffersIterator<float> it = output->iterate_with({}, area); !it.is_end(); ++it) {
132 float xy[2] = {float(it.x), float(it.y)};
133 float uv[2];
134 float deriv[2][2];
135 float alpha;
136 pixel_transform(xy, uv, deriv, alpha);
137 if (alpha == 0.0f) {
138 zero_v4(it.out);
139 continue;
140 }
141
142 if (nearest_neighbour_) {
143 input_image->read_elem_sampled(uv[0], uv[1], PixelSampler::Nearest, it.out);
144 }
145 else {
146 /* EWA filtering. */
147 input_image->read_elem_filtered(uv[0], uv[1], deriv[0], deriv[1], false, it.out);
148
149 /* UV to alpha threshold. */
150 const float threshold = alpha_ * 0.05f;
151 /* XXX alpha threshold is used to fade out pixels on boundaries with invalid derivatives.
152 * this calculation is not very well defined, should be looked into if it becomes a problem
153 * ...
154 */
155 const float du = len_v2(deriv[0]);
156 const float dv = len_v2(deriv[1]);
157 const float factor = 1.0f - threshold * (du / image_width_ + dv / image_height_);
158 if (factor < 0.0f) {
159 alpha = 0.0f;
160 }
161 else {
162 alpha *= factor;
163 }
164 }
165 /* "pre-multiplied" */
166 if (alpha < 1.0f) {
167 mul_v4_fl(it.out, alpha);
168 }
169 }
170}
171
172} // namespace blender::compositor
MINLINE void mul_v4_fl(float r[4], float f)
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void zero_v4(float r[4])
MINLINE void zero_v2(float r[2])
void get_area_of_interest(int input_idx, const rcti &output_area, rcti &r_input_area) override
Get input operation area being read by this operation on rendering given output area.
void update_memory_buffer_partial(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
void update_memory_buffer_started(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
void pixel_transform(const float xy[2], float r_uv[2], float r_deriv[2][2], float &r_alpha)
a MemoryBuffer contains access to the data
void read_elem_bilinear(float x, float y, float *out) const
void read_elem_filtered(float x, float y, float dx[2], float dy[2], bool extend_boundary, float *out) const
void read_elem_sampled(float x, float y, PixelSampler sampler, float *out) const
NodeOperation contains calculation logic.
void add_output_socket(DataType datatype)
NodeOperation * get_input_operation(int index)
void add_input_socket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
void set_canvas_input_index(unsigned int index)
set the index of the input socket that will determine the canvas of this operation
draw_view in_light_buf[] float
@ Vector
Vector data type.
void expand_area_for_sampler(rcti &area, PixelSampler sampler)
Definition COM_Enums.cc:9
typename BuffersIteratorBuilder< T >::Iterator BuffersIterator
int xy[2]
Definition wm_draw.cc:170