Blender  V3.3
COM_CalculateStandardDeviationOperation.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2011 Blender Foundation. */
3 
5 
6 #include "COM_ExecutionSystem.h"
7 
8 #include "IMB_colormanagement.h"
9 
10 namespace blender::compositor {
11 
13  int /*x*/,
14  int /*y*/,
15  void * /*data*/)
16 {
18 }
19 
21 {
22  lock_mutex();
23  if (!iscalculated_) {
26  standard_deviation_ = 0.0f;
27  float *buffer = tile->get_buffer();
28  int size = tile->get_width() * tile->get_height();
29  int pixels = 0;
30  float sum = 0.0f;
31  float mean = result_;
32  for (int i = 0, offset = 0; i < size; i++, offset += 4) {
33  if (buffer[offset + 3] > 0) {
34  pixels++;
35 
36  switch (setting_) {
37  case 1: /* rgb combined */
38  {
40  sum += (value - mean) * (value - mean);
41  break;
42  }
43  case 2: /* red */
44  {
45  float value = buffer[offset];
46  sum += (value - mean) * (value - mean);
47  break;
48  }
49  case 3: /* green */
50  {
51  float value = buffer[offset + 1];
52  sum += (value - mean) * (value - mean);
53  break;
54  }
55  case 4: /* blue */
56  {
57  float value = buffer[offset + 2];
58  sum += (value - mean) * (value - mean);
59  break;
60  }
61  case 5: /* luminance */
62  {
63  float yuv[3];
65  buffer[offset + 1],
66  buffer[offset + 2],
67  &yuv[0],
68  &yuv[1],
69  &yuv[2],
71  sum += (yuv[0] - mean) * (yuv[0] - mean);
72  break;
73  }
74  }
75  }
76  }
77  standard_deviation_ = sqrt(sum / (float)(pixels - 1));
78  iscalculated_ = true;
79  }
80  unlock_mutex();
81  return nullptr;
82 }
83 
86 {
87  if (!iscalculated_) {
88  const MemoryBuffer *input = inputs[0];
89  const float mean = CalculateMeanOperation::calc_mean(input);
90 
91  PixelsSum total = {0};
93  input->get_rect(),
94  [=](const rcti &split) { return calc_area_sum(input, split, mean); },
95  total,
96  [](PixelsSum &join, const PixelsSum &chunk) {
97  join.sum += chunk.sum;
98  join.num_pixels += chunk.num_pixels;
99  });
100  standard_deviation_ = total.num_pixels <= 1 ? 0.0f :
101  sqrt(total.sum / (float)(total.num_pixels - 1));
102  iscalculated_ = true;
103  }
104 }
105 
108 {
110 }
111 
113 PixelsSum CalculateStandardDeviationOperation::calc_area_sum(const MemoryBuffer *input,
114  const rcti &area,
115  const float mean)
116 {
117  PixelsSum result = {0};
118  for (const float *elem : input->get_buffer_area(area)) {
119  if (elem[3] <= 0.0f) {
120  continue;
121  }
122  const float value = setting_func_(elem);
123  result.sum += (value - mean) * (value - mean);
124  result.num_pixels++;
125  }
126  return result;
127 }
128 
129 } // namespace blender::compositor
sqrt(x)+1/max(0
#define BLI_YUV_ITU_BT709
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:59
#define UNUSED(x)
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
static T sum(const btAlignedObjectArray< T > &items)
std::function< float(const float *elem)> setting_func_
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
void execute_work(const rcti &work_rect, std::function< void(const rcti &split_rect)> work_func)
a MemoryBuffer contains access to the data of a chunk
virtual void * initialize_tile_data(rcti *)
ccl_global float * buffer
ccl_global KernelShaderEvalInput ccl_global float * output
ccl_global const KernelWorkTile * tile
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
ccl_global KernelShaderEvalInput * input
static void area(int d1, int d2, int e1, int e2, float weights[2])
CalculateMeanOperation::PixelsSum PixelsSum
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition: abc_util.cc:92
static bNodeSocketTemplate inputs[]