Blender  V2.93
COM_CalculateStandardDeviationOperation.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 
20 #include "BLI_math.h"
21 #include "BLI_utildefines.h"
22 
23 #include "IMB_colormanagement.h"
24 
25 namespace blender::compositor {
26 
28  int /*x*/,
29  int /*y*/,
30  void * /*data*/)
31 {
32  output[0] = this->m_standardDeviation;
33 }
34 
36 {
37  lockMutex();
38  if (!this->m_iscalculated) {
41  this->m_standardDeviation = 0.0f;
42  float *buffer = tile->getBuffer();
43  int size = tile->getWidth() * tile->getHeight();
44  int pixels = 0;
45  float sum = 0.0f;
46  float mean = this->m_result;
47  for (int i = 0, offset = 0; i < size; i++, offset += 4) {
48  if (buffer[offset + 3] > 0) {
49  pixels++;
50 
51  switch (this->m_setting) {
52  case 1: /* rgb combined */
53  {
54  float value = IMB_colormanagement_get_luminance(&buffer[offset]);
55  sum += (value - mean) * (value - mean);
56  break;
57  }
58  case 2: /* red */
59  {
60  float value = buffer[offset];
61  sum += (value - mean) * (value - mean);
62  break;
63  }
64  case 3: /* green */
65  {
66  float value = buffer[offset + 1];
67  sum += (value - mean) * (value - mean);
68  break;
69  }
70  case 4: /* blue */
71  {
72  float value = buffer[offset + 2];
73  sum += (value - mean) * (value - mean);
74  break;
75  }
76  case 5: /* luminance */
77  {
78  float yuv[3];
79  rgb_to_yuv(buffer[offset],
80  buffer[offset + 1],
81  buffer[offset + 2],
82  &yuv[0],
83  &yuv[1],
84  &yuv[2],
86  sum += (yuv[0] - mean) * (yuv[0] - mean);
87  break;
88  }
89  }
90  }
91  }
92  this->m_standardDeviation = sqrt(sum / (float)(pixels - 1));
93  this->m_iscalculated = true;
94  }
95  unlockMutex();
96  return nullptr;
97 }
98 
99 } // 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:79
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
#define output
static T sum(const btAlignedObjectArray< T > &items)
SocketReader * m_imageReader
Cached reference to the reader.
void executePixel(float output[4], int x, int y, void *data) override
a MemoryBuffer contains access to the data of a chunk
const int getHeight() const
get the height of this MemoryBuffer
const int getWidth() const
get the width of this MemoryBuffer
float * getBuffer()
get the data of this MemoryBuffer
virtual void * initializeTileData(rcti *)
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer