~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/compositor/operations/COM_CalculateMeanOperation.cpp

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2011, Blender Foundation.
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 *
 
18
 * Contributor: 
 
19
 *              Jeroen Bakker 
 
20
 *              Monique Dewanchand
 
21
 */
 
22
 
 
23
#include "COM_CalculateMeanOperation.h"
 
24
#include "BLI_math.h"
 
25
#include "BLI_utildefines.h"
 
26
 
 
27
 
 
28
 
 
29
CalculateMeanOperation::CalculateMeanOperation() : NodeOperation()
 
30
{
 
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;
 
35
        this->m_setting = 1;
 
36
        this->setComplex(true);
 
37
}
 
38
void CalculateMeanOperation::initExecution()
 
39
{
 
40
        this->m_imageReader = this->getInputSocketReader(0);
 
41
        this->m_iscalculated = false;
 
42
        NodeOperation::initMutex();
 
43
}
 
44
 
 
45
void CalculateMeanOperation::executePixel(float output[4], int x, int y, void *data)
 
46
{
 
47
        output[0] = this->m_result;
 
48
}
 
49
 
 
50
void CalculateMeanOperation::deinitExecution()
 
51
{
 
52
        this->m_imageReader = NULL;
 
53
        NodeOperation::deinitMutex();
 
54
}
 
55
 
 
56
bool CalculateMeanOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
 
57
{
 
58
        rcti imageInput;
 
59
        if (this->m_iscalculated) {
 
60
                return false;
 
61
        }
 
62
        NodeOperation *operation = getInputOperation(0);
 
63
        imageInput.xmax = operation->getWidth();
 
64
        imageInput.xmin = 0;
 
65
        imageInput.ymax = operation->getHeight();
 
66
        imageInput.ymin = 0;
 
67
        if (operation->determineDependingAreaOfInterest(&imageInput, readOperation, output) ) {
 
68
                return true;
 
69
        }
 
70
        return false;
 
71
}
 
72
 
 
73
void *CalculateMeanOperation::initializeTileData(rcti *rect)
 
74
{
 
75
        lockMutex();
 
76
        if (!this->m_iscalculated) {
 
77
                MemoryBuffer *tile = (MemoryBuffer *)this->m_imageReader->initializeTileData(rect);
 
78
                calculateMean(tile);
 
79
                this->m_iscalculated = true;
 
80
        }
 
81
        unlockMutex();
 
82
        return NULL;
 
83
}
 
84
 
 
85
void CalculateMeanOperation::calculateMean(MemoryBuffer *tile)
 
86
{
 
87
        this->m_result = 0.0f;
 
88
        float *buffer = tile->getBuffer();
 
89
        int size = tile->getWidth() * tile->getHeight();
 
90
        int pixels = 0;
 
91
        float sum = 0.0f;
 
92
        for (int i = 0, offset = 0; i < size; i++, offset += 4) {
 
93
                if (buffer[offset + 3] > 0) {
 
94
                        pixels++;
 
95
        
 
96
                        switch (this->m_setting) {
 
97
                                case 1:
 
98
                                {
 
99
                                        sum += rgb_to_bw(&buffer[offset]);
 
100
                                        break;
 
101
                                }
 
102
                                case 2:
 
103
                                {
 
104
                                        sum += buffer[offset];
 
105
                                        break;
 
106
                                }
 
107
                                case 3:
 
108
                                {
 
109
                                        sum += buffer[offset + 1];
 
110
                                        break;
 
111
                                }
 
112
                                case 4:
 
113
                                {
 
114
                                        sum += buffer[offset + 2];
 
115
                                        break;
 
116
                                }
 
117
                                case 5:
 
118
                                {
 
119
                                        float yuv[3];
 
120
                                        rgb_to_yuv(buffer[offset], buffer[offset + 1], buffer[offset + 2], &yuv[0], &yuv[1], &yuv[2]);
 
121
                                        sum += yuv[0];
 
122
                                        break;
 
123
                                }
 
124
                        }
 
125
                }
 
126
        }
 
127
        this->m_result = sum / pixels;
 
128
}