Welcome to mirror list, hosted at ThFree Co, Russian Federation.

COM_BokehImageOperation.cc « operations « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9e7c11620527bc4cffdf25b6a2c670aaf567781a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Copyright 2011, Blender Foundation.
 */

#include "COM_BokehImageOperation.h"

namespace blender::compositor {

BokehImageOperation::BokehImageOperation()
{
  this->add_output_socket(DataType::Color);
  delete_data_ = false;
}
void BokehImageOperation::init_execution()
{
  center_[0] = get_width() / 2;
  center_[1] = get_height() / 2;
  inverse_rounding_ = 1.0f - data_->rounding;
  circular_distance_ = get_width() / 2;
  flap_rad_ = (float)(M_PI * 2) / data_->flaps;
  flap_rad_add_ = data_->angle;
  while (flap_rad_add_ < 0.0f) {
    flap_rad_add_ += (float)(M_PI * 2.0);
  }
  while (flap_rad_add_ > (float)M_PI) {
    flap_rad_add_ -= (float)(M_PI * 2.0);
  }
}
void BokehImageOperation::detemine_start_point_of_flap(float r[2], int flap_number, float distance)
{
  r[0] = sinf(flap_rad_ * flap_number + flap_rad_add_) * distance + center_[0];
  r[1] = cosf(flap_rad_ * flap_number + flap_rad_add_) * distance + center_[1];
}
float BokehImageOperation::is_inside_bokeh(float distance, float x, float y)
{
  float inside_bokeh = 0.0f;
  const float deltaX = x - center_[0];
  const float deltaY = y - center_[1];
  float closest_point[2];
  float line_p1[2];
  float line_p2[2];
  float point[2];
  point[0] = x;
  point[1] = y;

  const float distance_to_center = len_v2v2(point, center_);
  const float bearing = (atan2f(deltaX, deltaY) + (float)(M_PI * 2.0));
  int flap_number = (int)((bearing - flap_rad_add_) / flap_rad_);

  detemine_start_point_of_flap(line_p1, flap_number, distance);
  detemine_start_point_of_flap(line_p2, flap_number + 1, distance);
  closest_to_line_v2(closest_point, point, line_p1, line_p2);

  const float distance_line_to_center = len_v2v2(center_, closest_point);
  const float distance_rounding_to_center = inverse_rounding_ * distance_line_to_center +
                                            data_->rounding * distance;

  const float catadioptric_distance_to_center = distance_rounding_to_center * data_->catadioptric;
  if (distance_rounding_to_center >= distance_to_center &&
      catadioptric_distance_to_center <= distance_to_center) {
    if (distance_rounding_to_center - distance_to_center < 1.0f) {
      inside_bokeh = (distance_rounding_to_center - distance_to_center);
    }
    else if (data_->catadioptric != 0.0f &&
             distance_to_center - catadioptric_distance_to_center < 1.0f) {
      inside_bokeh = (distance_to_center - catadioptric_distance_to_center);
    }
    else {
      inside_bokeh = 1.0f;
    }
  }
  return inside_bokeh;
}
void BokehImageOperation::execute_pixel_sampled(float output[4],
                                                float x,
                                                float y,
                                                PixelSampler /*sampler*/)
{
  float shift = data_->lensshift;
  float shift2 = shift / 2.0f;
  float distance = circular_distance_;
  float inside_bokeh_max = is_inside_bokeh(distance, x, y);
  float inside_bokeh_med = is_inside_bokeh(distance - fabsf(shift2 * distance), x, y);
  float inside_bokeh_min = is_inside_bokeh(distance - fabsf(shift * distance), x, y);
  if (shift < 0) {
    output[0] = inside_bokeh_max;
    output[1] = inside_bokeh_med;
    output[2] = inside_bokeh_min;
  }
  else {
    output[0] = inside_bokeh_min;
    output[1] = inside_bokeh_med;
    output[2] = inside_bokeh_max;
  }
  output[3] = (inside_bokeh_max + inside_bokeh_med + inside_bokeh_min) / 3.0f;
}

void BokehImageOperation::update_memory_buffer_partial(MemoryBuffer *output,
                                                       const rcti &area,
                                                       Span<MemoryBuffer *> UNUSED(inputs))
{
  const float shift = data_->lensshift;
  const float shift2 = shift / 2.0f;
  const float distance = circular_distance_;
  for (BuffersIterator<float> it = output->iterate_with({}, area); !it.is_end(); ++it) {
    const float inside_bokeh_max = is_inside_bokeh(distance, it.x, it.y);
    const float inside_bokeh_med = is_inside_bokeh(
        distance - fabsf(shift2 * distance), it.x, it.y);
    const float inside_bokeh_min = is_inside_bokeh(distance - fabsf(shift * distance), it.x, it.y);
    if (shift < 0) {
      it.out[0] = inside_bokeh_max;
      it.out[1] = inside_bokeh_med;
      it.out[2] = inside_bokeh_min;
    }
    else {
      it.out[0] = inside_bokeh_min;
      it.out[1] = inside_bokeh_med;
      it.out[2] = inside_bokeh_max;
    }
    it.out[3] = (inside_bokeh_max + inside_bokeh_med + inside_bokeh_min) / 3.0f;
  }
}

void BokehImageOperation::deinit_execution()
{
  if (delete_data_) {
    if (data_) {
      delete data_;
      data_ = nullptr;
    }
  }
}

void BokehImageOperation::determine_canvas(const rcti &preferred_area, rcti &r_area)
{
  BLI_rcti_init(&r_area,
                preferred_area.xmin,
                preferred_area.xmin + COM_BLUR_BOKEH_PIXELS,
                preferred_area.ymin,
                preferred_area.ymin + COM_BLUR_BOKEH_PIXELS);
}

}  // namespace blender::compositor