Blender  V2.93
COM_ConvertOperation.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2011, Blender Foundation.
17  */
18 
19 #include "COM_ConvertOperation.h"
20 
21 #include "IMB_colormanagement.h"
22 
23 namespace blender::compositor {
24 
26 {
27  this->m_inputOperation = nullptr;
28 }
29 
31 {
32  this->m_inputOperation = this->getInputSocketReader(0);
33 }
34 
36 {
37  this->m_inputOperation = nullptr;
38 }
39 
40 /* ******** Value to Color ******** */
41 
43 {
46 }
47 
49  float x,
50  float y,
51  PixelSampler sampler)
52 {
53  float value;
54  this->m_inputOperation->readSampled(&value, x, y, sampler);
55  output[0] = output[1] = output[2] = value;
56  output[3] = 1.0f;
57 }
58 
59 /* ******** Color to Value ******** */
60 
62 {
65 }
66 
68  float x,
69  float y,
70  PixelSampler sampler)
71 {
72  float inputColor[4];
73  this->m_inputOperation->readSampled(inputColor, x, y, sampler);
74  output[0] = (inputColor[0] + inputColor[1] + inputColor[2]) / 3.0f;
75 }
76 
77 /* ******** Color to BW ******** */
78 
80 {
83 }
84 
86  float x,
87  float y,
88  PixelSampler sampler)
89 {
90  float inputColor[4];
91  this->m_inputOperation->readSampled(inputColor, x, y, sampler);
93 }
94 
95 /* ******** Color to Vector ******** */
96 
98 {
101 }
102 
104  float x,
105  float y,
106  PixelSampler sampler)
107 {
108  float color[4];
109  this->m_inputOperation->readSampled(color, x, y, sampler);
110  copy_v3_v3(output, color);
111 }
112 
113 /* ******** Value to Vector ******** */
114 
116 {
119 }
120 
122  float x,
123  float y,
124  PixelSampler sampler)
125 {
126  float value;
127  this->m_inputOperation->readSampled(&value, x, y, sampler);
128  output[0] = output[1] = output[2] = value;
129 }
130 
131 /* ******** Vector to Color ******** */
132 
134 {
137 }
138 
140  float x,
141  float y,
142  PixelSampler sampler)
143 {
144  this->m_inputOperation->readSampled(output, x, y, sampler);
145  output[3] = 1.0f;
146 }
147 
148 /* ******** Vector to Value ******** */
149 
151 {
154 }
155 
157  float x,
158  float y,
159  PixelSampler sampler)
160 {
161  float input[4];
162  this->m_inputOperation->readSampled(input, x, y, sampler);
163  output[0] = (input[0] + input[1] + input[2]) / 3.0f;
164 }
165 
166 /* ******** RGB to YCC ******** */
167 
169 {
172 }
173 
175 {
176  switch (mode) {
177  case 0:
178  this->m_mode = BLI_YCC_ITU_BT601;
179  break;
180  case 2:
181  this->m_mode = BLI_YCC_JFIF_0_255;
182  break;
183  case 1:
184  default:
185  this->m_mode = BLI_YCC_ITU_BT709;
186  break;
187  }
188 }
189 
191  float x,
192  float y,
193  PixelSampler sampler)
194 {
195  float inputColor[4];
196  float color[3];
197 
198  this->m_inputOperation->readSampled(inputColor, x, y, sampler);
199  rgb_to_ycc(
200  inputColor[0], inputColor[1], inputColor[2], &color[0], &color[1], &color[2], this->m_mode);
201 
202  /* divided by 255 to normalize for viewing in */
203  /* R,G,B --> Y,Cb,Cr */
204  mul_v3_v3fl(output, color, 1.0f / 255.0f);
205  output[3] = inputColor[3];
206 }
207 
208 /* ******** YCC to RGB ******** */
209 
211 {
214 }
215 
217 {
218  switch (mode) {
219  case 0:
220  this->m_mode = BLI_YCC_ITU_BT601;
221  break;
222  case 2:
223  this->m_mode = BLI_YCC_JFIF_0_255;
224  break;
225  case 1:
226  default:
227  this->m_mode = BLI_YCC_ITU_BT709;
228  break;
229  }
230 }
231 
233  float x,
234  float y,
235  PixelSampler sampler)
236 {
237  float inputColor[4];
238  this->m_inputOperation->readSampled(inputColor, x, y, sampler);
239 
240  /* need to un-normalize the data */
241  /* R,G,B --> Y,Cb,Cr */
242  mul_v3_fl(inputColor, 255.0f);
243 
244  ycc_to_rgb(inputColor[0],
245  inputColor[1],
246  inputColor[2],
247  &output[0],
248  &output[1],
249  &output[2],
250  this->m_mode);
251  output[3] = inputColor[3];
252 }
253 
254 /* ******** RGB to YUV ******** */
255 
257 {
260 }
261 
263  float x,
264  float y,
265  PixelSampler sampler)
266 {
267  float inputColor[4];
268  this->m_inputOperation->readSampled(inputColor, x, y, sampler);
269  rgb_to_yuv(inputColor[0],
270  inputColor[1],
271  inputColor[2],
272  &output[0],
273  &output[1],
274  &output[2],
276  output[3] = inputColor[3];
277 }
278 
279 /* ******** YUV to RGB ******** */
280 
282 {
285 }
286 
288  float x,
289  float y,
290  PixelSampler sampler)
291 {
292  float inputColor[4];
293  this->m_inputOperation->readSampled(inputColor, x, y, sampler);
294  yuv_to_rgb(inputColor[0],
295  inputColor[1],
296  inputColor[2],
297  &output[0],
298  &output[1],
299  &output[2],
301  output[3] = inputColor[3];
302 }
303 
304 /* ******** RGB to HSV ******** */
305 
307 {
310 }
311 
313  float x,
314  float y,
315  PixelSampler sampler)
316 {
317  float inputColor[4];
318  this->m_inputOperation->readSampled(inputColor, x, y, sampler);
319  rgb_to_hsv_v(inputColor, output);
320  output[3] = inputColor[3];
321 }
322 
323 /* ******** HSV to RGB ******** */
324 
326 {
329 }
330 
332  float x,
333  float y,
334  PixelSampler sampler)
335 {
336  float inputColor[4];
337  this->m_inputOperation->readSampled(inputColor, x, y, sampler);
338  hsv_to_rgb_v(inputColor, output);
339  output[0] = max_ff(output[0], 0.0f);
340  output[1] = max_ff(output[1], 0.0f);
341  output[2] = max_ff(output[2], 0.0f);
342  output[3] = inputColor[3];
343 }
344 
345 /* ******** Premul to Straight ******** */
346 
348 {
351 }
352 
354  float x,
355  float y,
356  PixelSampler sampler)
357 {
358  float inputValue[4];
359  float alpha;
360 
361  this->m_inputOperation->readSampled(inputValue, x, y, sampler);
362  alpha = inputValue[3];
363 
364  if (fabsf(alpha) < 1e-5f) {
365  zero_v3(output);
366  }
367  else {
368  mul_v3_v3fl(output, inputValue, 1.0f / alpha);
369  }
370 
371  /* never touches the alpha */
372  output[3] = alpha;
373 }
374 
375 /* ******** Straight to Premul ******** */
376 
378 {
381 }
382 
384  float x,
385  float y,
386  PixelSampler sampler)
387 {
388  float inputValue[4];
389  float alpha;
390 
391  this->m_inputOperation->readSampled(inputValue, x, y, sampler);
392  alpha = inputValue[3];
393 
394  mul_v3_v3fl(output, inputValue, alpha);
395 
396  /* never touches the alpha */
397  output[3] = alpha;
398 }
399 
400 /* ******** Separate Channels ******** */
401 
403 {
406  this->m_inputOperation = nullptr;
407 }
409 {
410  this->m_inputOperation = this->getInputSocketReader(0);
411 }
412 
414 {
415  this->m_inputOperation = nullptr;
416 }
417 
419  float x,
420  float y,
421  PixelSampler sampler)
422 {
423  float input[4];
424  this->m_inputOperation->readSampled(input, x, y, sampler);
425  output[0] = input[this->m_channel];
426 }
427 
428 /* ******** Combine Channels ******** */
429 
431 {
438  this->m_inputChannel1Operation = nullptr;
439  this->m_inputChannel2Operation = nullptr;
440  this->m_inputChannel3Operation = nullptr;
441  this->m_inputChannel4Operation = nullptr;
442 }
443 
445 {
446  this->m_inputChannel1Operation = this->getInputSocketReader(0);
447  this->m_inputChannel2Operation = this->getInputSocketReader(1);
448  this->m_inputChannel3Operation = this->getInputSocketReader(2);
449  this->m_inputChannel4Operation = this->getInputSocketReader(3);
450 }
451 
453 {
454  this->m_inputChannel1Operation = nullptr;
455  this->m_inputChannel2Operation = nullptr;
456  this->m_inputChannel3Operation = nullptr;
457  this->m_inputChannel4Operation = nullptr;
458 }
459 
461  float x,
462  float y,
463  PixelSampler sampler)
464 {
465  float input[4];
466  if (this->m_inputChannel1Operation) {
467  this->m_inputChannel1Operation->readSampled(input, x, y, sampler);
468  output[0] = input[0];
469  }
470  if (this->m_inputChannel2Operation) {
471  this->m_inputChannel2Operation->readSampled(input, x, y, sampler);
472  output[1] = input[0];
473  }
474  if (this->m_inputChannel3Operation) {
475  this->m_inputChannel3Operation->readSampled(input, x, y, sampler);
476  output[2] = input[0];
477  }
478  if (this->m_inputChannel4Operation) {
479  this->m_inputChannel4Operation->readSampled(input, x, y, sampler);
480  output[3] = input[0];
481  }
482 }
483 
484 } // namespace blender::compositor
MINLINE float max_ff(float a, float b)
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition: math_color.c:68
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
Definition: math_color.c:254
#define BLI_YUV_ITU_BT709
#define BLI_YCC_JFIF_0_255
void ycc_to_rgb(float y, float cb, float cr, float *r_r, float *r_g, float *r_b, int colorspace)
Definition: math_color.c:169
#define BLI_YCC_ITU_BT601
void rgb_to_ycc(float r, float g, float b, float *r_y, float *r_cb, float *r_cr, int colorspace)
Definition: math_color.c:130
void rgb_to_yuv(float r, float g, float b, float *r_y, float *r_u, float *r_v, int colorspace)
Definition: math_color.c:79
void yuv_to_rgb(float y, float u, float v, float *r_r, float *r_g, float *r_b, int colorspace)
Definition: math_color.c:103
#define BLI_YCC_ITU_BT709
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void zero_v3(float r[3])
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
_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
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
#define output
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void readSampled(float result[4], float x, float y, PixelSampler sampler)
void addInputSocket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
void addOutputSocket(DataType datatype)
void setResolutionInputSocketIndex(unsigned int index)
set the index of the input socket that will determine the resolution of this operation
SocketReader * getInputSocketReader(unsigned int inputSocketindex)
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
static CCL_NAMESPACE_BEGIN const double alpha
@ Vector
Vector data type.
#define fabsf(x)