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

« back to all changes in this revision

Viewing changes to source/blender/compositor/operations/COM_BokehImageOperation.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_BokehImageOperation.h"
 
24
#include "BLI_math.h"
 
25
 
 
26
BokehImageOperation::BokehImageOperation() : NodeOperation()
 
27
{
 
28
        this->addOutputSocket(COM_DT_COLOR);
 
29
        this->m_deleteData = false;
 
30
}
 
31
void BokehImageOperation::initExecution()
 
32
{
 
33
        this->m_center[0] = getWidth() / 2;
 
34
        this->m_center[1] = getHeight() / 2;
 
35
        this->m_inverseRounding = 1.0f - this->m_data->rounding;
 
36
        this->m_circularDistance = getWidth() / 2;
 
37
        this->m_flapRad = (float)(M_PI * 2) / this->m_data->flaps;
 
38
        this->m_flapRadAdd = (this->m_data->angle / 360.0f) * (float)(M_PI * 2.0);
 
39
        while (this->m_flapRadAdd < 0.0f) {
 
40
                this->m_flapRadAdd += (float)(M_PI * 2.0);
 
41
        }
 
42
        while (this->m_flapRadAdd > (float)M_PI) {
 
43
                this->m_flapRadAdd -= (float)(M_PI * 2.0);
 
44
        }
 
45
}
 
46
void BokehImageOperation::detemineStartPointOfFlap(float r[2], int flapNumber, float distance)
 
47
{
 
48
        r[0] = sinf(this->m_flapRad * flapNumber + this->m_flapRadAdd) * distance + this->m_center[0];
 
49
        r[1] = cosf(this->m_flapRad * flapNumber + this->m_flapRadAdd) * distance + this->m_center[1];
 
50
}
 
51
float BokehImageOperation::isInsideBokeh(float distance, float x, float y)
 
52
{
 
53
        float insideBokeh = 0.0f;
 
54
        const float deltaX = x - this->m_center[0];
 
55
        const float deltaY = y - this->m_center[1];
 
56
        float closestPoint[2];
 
57
        float lineP1[2];
 
58
        float lineP2[2];
 
59
        float point[2];
 
60
        point[0] = x;
 
61
        point[1] = y;
 
62
 
 
63
        const float distanceToCenter = len_v2v2(point, this->m_center);
 
64
        const float bearing = (atan2f(deltaX, deltaY) + (float)(M_PI * 2.0));
 
65
        int flapNumber = (int)((bearing - this->m_flapRadAdd) / this->m_flapRad);
 
66
 
 
67
        detemineStartPointOfFlap(lineP1, flapNumber, distance);
 
68
        detemineStartPointOfFlap(lineP2, flapNumber + 1, distance);
 
69
        closest_to_line_v2(closestPoint, point, lineP1, lineP2);
 
70
 
 
71
        const float distanceLineToCenter = len_v2v2(this->m_center, closestPoint);
 
72
        const float distanceRoundingToCenter = this->m_inverseRounding * distanceLineToCenter + this->m_data->rounding * distance;
 
73
 
 
74
        const float catadioptricDistanceToCenter = distanceRoundingToCenter * this->m_data->catadioptric;
 
75
        if (distanceRoundingToCenter >= distanceToCenter && catadioptricDistanceToCenter <= distanceToCenter) {
 
76
                if (distanceRoundingToCenter - distanceToCenter < 1.0f) {
 
77
                        insideBokeh = (distanceRoundingToCenter - distanceToCenter);
 
78
                }
 
79
                else if (this->m_data->catadioptric != 0.0f && distanceToCenter - catadioptricDistanceToCenter < 1.0f) {
 
80
                        insideBokeh = (distanceToCenter - catadioptricDistanceToCenter);
 
81
                }
 
82
                else {
 
83
                        insideBokeh = 1.0f;
 
84
                }
 
85
        }
 
86
        return insideBokeh;
 
87
}
 
88
void BokehImageOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
 
89
{
 
90
        float shift = this->m_data->lensshift;
 
91
        float shift2 = shift / 2.0f;
 
92
        float distance = this->m_circularDistance;
 
93
        float insideBokehMax = isInsideBokeh(distance, x, y);
 
94
        float insideBokehMed = isInsideBokeh(distance - fabsf(shift2 * distance), x, y);
 
95
        float insideBokehMin = isInsideBokeh(distance - fabsf(shift * distance), x, y);
 
96
        if (shift < 0) {
 
97
                output[0] = insideBokehMax;
 
98
                output[1] = insideBokehMed;
 
99
                output[2] = insideBokehMin;
 
100
        }
 
101
        else {
 
102
                output[0] = insideBokehMin;
 
103
                output[1] = insideBokehMed;
 
104
                output[2] = insideBokehMax;
 
105
        }
 
106
        output[3] = (insideBokehMax + insideBokehMed + insideBokehMin) / 3.0f;
 
107
}
 
108
 
 
109
void BokehImageOperation::deinitExecution()
 
110
{
 
111
        if (this->m_deleteData) {
 
112
                if (this->m_data) {
 
113
                        delete this->m_data;
 
114
                        this->m_data = NULL;
 
115
                }
 
116
        }
 
117
}
 
118
 
 
119
void BokehImageOperation::determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2])
 
120
{
 
121
        resolution[0] = COM_BLUR_BOKEH_PIXELS;
 
122
        resolution[1] = COM_BLUR_BOKEH_PIXELS;
 
123
}