2
* Copyright 2011, Blender Foundation.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
#include "COM_CalculateMeanOperation.h"
25
#include "BLI_utildefines.h"
29
CalculateMeanOperation::CalculateMeanOperation() : NodeOperation()
31
this->addInputSocket(COM_DT_COLOR, COM_SC_NO_RESIZE);
32
this->addOutputSocket(COM_DT_VALUE);
33
this->m_imageReader = NULL;
34
this->m_iscalculated = false;
36
this->setComplex(true);
38
void CalculateMeanOperation::initExecution()
40
this->m_imageReader = this->getInputSocketReader(0);
41
this->m_iscalculated = false;
42
NodeOperation::initMutex();
45
void CalculateMeanOperation::executePixel(float output[4], int x, int y, void *data)
47
output[0] = this->m_result;
50
void CalculateMeanOperation::deinitExecution()
52
this->m_imageReader = NULL;
53
NodeOperation::deinitMutex();
56
bool CalculateMeanOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
59
if (this->m_iscalculated) {
62
NodeOperation *operation = getInputOperation(0);
63
imageInput.xmax = operation->getWidth();
65
imageInput.ymax = operation->getHeight();
67
if (operation->determineDependingAreaOfInterest(&imageInput, readOperation, output) ) {
73
void *CalculateMeanOperation::initializeTileData(rcti *rect)
76
if (!this->m_iscalculated) {
77
MemoryBuffer *tile = (MemoryBuffer *)this->m_imageReader->initializeTileData(rect);
79
this->m_iscalculated = true;
85
void CalculateMeanOperation::calculateMean(MemoryBuffer *tile)
87
this->m_result = 0.0f;
88
float *buffer = tile->getBuffer();
89
int size = tile->getWidth() * tile->getHeight();
92
for (int i = 0, offset = 0; i < size; i++, offset += 4) {
93
if (buffer[offset + 3] > 0) {
96
switch (this->m_setting) {
99
sum += rgb_to_bw(&buffer[offset]);
104
sum += buffer[offset];
109
sum += buffer[offset + 1];
114
sum += buffer[offset + 2];
120
rgb_to_yuv(buffer[offset], buffer[offset + 1], buffer[offset + 2], &yuv[0], &yuv[1], &yuv[2]);
127
this->m_result = sum / pixels;