Blender  V3.3
COM_NormalizeOperation.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2012 Blender Foundation. */
3 
5 
6 namespace blender::compositor {
7 
9 {
12  image_reader_ = nullptr;
13  cached_instance_ = nullptr;
14  flags_.complex = true;
15  flags_.can_be_constant = true;
16 }
18 {
21 }
22 
23 void NormalizeOperation::execute_pixel(float output[4], int x, int y, void *data)
24 {
25  /* using generic two floats struct to store `x: min`, `y: multiply` */
26  NodeTwoFloats *minmult = (NodeTwoFloats *)data;
27 
28  image_reader_->read(output, x, y, nullptr);
29 
30  output[0] = (output[0] - minmult->x) * minmult->y;
31 
32  /* clamp infinities */
33  if (output[0] > 1.0f) {
34  output[0] = 1.0f;
35  }
36  else if (output[0] < 0.0f) {
37  output[0] = 0.0f;
38  }
39 }
40 
42 {
43  image_reader_ = nullptr;
44  delete cached_instance_;
45  cached_instance_ = nullptr;
47 }
48 
50  ReadBufferOperation *read_operation,
51  rcti *output)
52 {
53  rcti image_input;
54  if (cached_instance_) {
55  return false;
56  }
57 
58  NodeOperation *operation = get_input_operation(0);
59  image_input.xmax = operation->get_width();
60  image_input.xmin = 0;
61  image_input.ymax = operation->get_height();
62  image_input.ymin = 0;
63 
64  if (operation->determine_depending_area_of_interest(&image_input, read_operation, output)) {
65  return true;
66  }
67  return false;
68 }
69 
70 /* The code below assumes all data is inside range +- this, and that input buffer is single channel
71  */
72 #define BLENDER_ZMAX 10000.0f
73 
75 {
76  lock_mutex();
77  if (cached_instance_ == nullptr) {
79  /* using generic two floats struct to store `x: min`, `y: multiply`. */
80  NodeTwoFloats *minmult = new NodeTwoFloats();
81 
82  float *buffer = tile->get_buffer();
83  int p = tile->get_width() * tile->get_height();
84  float *bc = buffer;
85 
86  float minv = 1.0f + BLENDER_ZMAX;
87  float maxv = -1.0f - BLENDER_ZMAX;
88 
89  float value;
90  while (p--) {
91  value = bc[0];
92  if ((value > maxv) && (value <= BLENDER_ZMAX)) {
93  maxv = value;
94  }
95  if ((value < minv) && (value >= -BLENDER_ZMAX)) {
96  minv = value;
97  }
98  bc++;
99  }
100 
101  minmult->x = minv;
102  /* The rare case of flat buffer would cause a divide by 0 */
103  minmult->y = ((maxv != minv) ? 1.0f / (maxv - minv) : 0.0f);
104 
105  cached_instance_ = minmult;
106  }
107 
108  unlock_mutex();
109  return cached_instance_;
110 }
111 
112 void NormalizeOperation::deinitialize_tile_data(rcti * /*rect*/, void * /*data*/)
113 {
114  /* pass */
115 }
116 
118  const rcti &UNUSED(output_area),
119  rcti &r_input_area)
120 {
121  r_input_area = get_input_operation(0)->get_canvas();
122 }
123 
125  const rcti &UNUSED(area),
127 {
128  if (cached_instance_ == nullptr) {
129  MemoryBuffer *input = inputs[0];
130 
131  /* Using generic two floats struct to store `x: min`, `y: multiply`. */
132  NodeTwoFloats *minmult = new NodeTwoFloats();
133 
134  float minv = 1.0f + BLENDER_ZMAX;
135  float maxv = -1.0f - BLENDER_ZMAX;
136  for (const float *elem : input->as_range()) {
137  const float value = *elem;
138  if ((value > maxv) && (value <= BLENDER_ZMAX)) {
139  maxv = value;
140  }
141  if ((value < minv) && (value >= -BLENDER_ZMAX)) {
142  minv = value;
143  }
144  }
145 
146  minmult->x = minv;
147  /* The case of a flat buffer would cause a divide by 0. */
148  minmult->y = ((maxv != minv) ? 1.0f / (maxv - minv) : 0.0f);
149 
150  cached_instance_ = minmult;
151  }
152 }
153 
155  const rcti &area,
157 {
158  NodeTwoFloats *minmult = cached_instance_;
159  for (BuffersIterator<float> it = output->iterate_with(inputs, area); !it.is_end(); ++it) {
160  const float input_value = *it.in(0);
161 
162  *it.out = (input_value - minmult->x) * minmult->y;
163 
164  /* Clamp infinities. */
165  CLAMP(*it.out, 0.0f, 1.0f);
166  }
167 }
168 
169 } // namespace blender::compositor
#define UNUSED(x)
#define BLENDER_ZMAX
struct NodeTwoFloats NodeTwoFloats
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position CLAMP
a MemoryBuffer contains access to the data of a chunk
NodeOperation contains calculation logic.
void add_output_socket(DataType datatype)
SocketReader * get_input_socket_reader(unsigned int index)
NodeOperation * get_input_operation(int index)
void read(float result[4], int x, int y, void *chunk_data)
virtual bool determine_depending_area_of_interest(rcti *input, ReadBufferOperation *read_operation, rcti *output)
void add_input_socket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
virtual void * initialize_tile_data(rcti *)
void deinitialize_tile_data(rcti *rect, void *data) override
void * initialize_tile_data(rcti *rect) override
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.
SocketReader * image_reader_
Cached reference to the reader.
void update_memory_buffer_started(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
void execute_pixel(float output[4], int x, int y, void *data) override
void update_memory_buffer_partial(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
bool determine_depending_area_of_interest(rcti *input, ReadBufferOperation *read_operation, rcti *output) override
NodeTwoFloats * cached_instance_
temporarily cache of the execution storage it stores x->min and y->multiply.
ccl_global float * buffer
ccl_global KernelShaderEvalInput ccl_global float * output
ccl_global const KernelWorkTile * tile
ccl_global KernelShaderEvalInput * input
static void area(int d1, int d2, int e1, int e2, float weights[2])
typename BuffersIteratorBuilder< T >::Iterator BuffersIterator
static bNodeSocketTemplate inputs[]
int ymin
Definition: DNA_vec_types.h:64
int ymax
Definition: DNA_vec_types.h:64
int xmin
Definition: DNA_vec_types.h:63
int xmax
Definition: DNA_vec_types.h:63