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

COM_GammaCorrectOperation.cc « operations « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 29a82ddc3d5368fb8e32a6d1b579d7fabe56bfe6 (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
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2011 Blender Foundation. */

#include "COM_GammaCorrectOperation.h"

namespace blender::compositor {

GammaCorrectOperation::GammaCorrectOperation()
{
  this->add_input_socket(DataType::Color);
  this->add_output_socket(DataType::Color);
  input_program_ = nullptr;
  flags_.can_be_constant = true;
}
void GammaCorrectOperation::init_execution()
{
  input_program_ = this->get_input_socket_reader(0);
}

void GammaCorrectOperation::execute_pixel_sampled(float output[4],
                                                  float x,
                                                  float y,
                                                  PixelSampler sampler)
{
  float input_color[4];
  input_program_->read_sampled(input_color, x, y, sampler);
  if (input_color[3] > 0.0f) {
    input_color[0] /= input_color[3];
    input_color[1] /= input_color[3];
    input_color[2] /= input_color[3];
  }

  /* check for negative to avoid nan's */
  output[0] = input_color[0] > 0.0f ? input_color[0] * input_color[0] : 0.0f;
  output[1] = input_color[1] > 0.0f ? input_color[1] * input_color[1] : 0.0f;
  output[2] = input_color[2] > 0.0f ? input_color[2] * input_color[2] : 0.0f;
  output[3] = input_color[3];

  if (input_color[3] > 0.0f) {
    output[0] *= input_color[3];
    output[1] *= input_color[3];
    output[2] *= input_color[3];
  }
}

void GammaCorrectOperation::update_memory_buffer_partial(MemoryBuffer *output,
                                                         const rcti &area,
                                                         Span<MemoryBuffer *> inputs)
{
  const MemoryBuffer *input = inputs[0];
  for (BuffersIterator<float> it = output->iterate_with({}, area); !it.is_end(); ++it) {
    float color[4];
    input->read_elem(it.x, it.y, color);
    if (color[3] > 0.0f) {
      color[0] /= color[3];
      color[1] /= color[3];
      color[2] /= color[3];
    }

    /* Check for negative to avoid nan's. */
    it.out[0] = color[0] > 0.0f ? color[0] * color[0] : 0.0f;
    it.out[1] = color[1] > 0.0f ? color[1] * color[1] : 0.0f;
    it.out[2] = color[2] > 0.0f ? color[2] * color[2] : 0.0f;
    it.out[3] = color[3];

    if (color[3] > 0.0f) {
      it.out[0] *= color[3];
      it.out[1] *= color[3];
      it.out[2] *= color[3];
    }
  }
}

void GammaCorrectOperation::deinit_execution()
{
  input_program_ = nullptr;
}

GammaUncorrectOperation::GammaUncorrectOperation()
{
  this->add_input_socket(DataType::Color);
  this->add_output_socket(DataType::Color);
  input_program_ = nullptr;
  flags_.can_be_constant = true;
}
void GammaUncorrectOperation::init_execution()
{
  input_program_ = this->get_input_socket_reader(0);
}

void GammaUncorrectOperation::execute_pixel_sampled(float output[4],
                                                    float x,
                                                    float y,
                                                    PixelSampler sampler)
{
  float input_color[4];
  input_program_->read_sampled(input_color, x, y, sampler);

  if (input_color[3] > 0.0f) {
    input_color[0] /= input_color[3];
    input_color[1] /= input_color[3];
    input_color[2] /= input_color[3];
  }

  output[0] = input_color[0] > 0.0f ? sqrtf(input_color[0]) : 0.0f;
  output[1] = input_color[1] > 0.0f ? sqrtf(input_color[1]) : 0.0f;
  output[2] = input_color[2] > 0.0f ? sqrtf(input_color[2]) : 0.0f;
  output[3] = input_color[3];

  if (input_color[3] > 0.0f) {
    output[0] *= input_color[3];
    output[1] *= input_color[3];
    output[2] *= input_color[3];
  }
}

void GammaUncorrectOperation::update_memory_buffer_partial(MemoryBuffer *output,
                                                           const rcti &area,
                                                           Span<MemoryBuffer *> inputs)
{
  const MemoryBuffer *input = inputs[0];
  for (BuffersIterator<float> it = output->iterate_with({}, area); !it.is_end(); ++it) {
    float color[4];
    input->read_elem(it.x, it.y, color);
    if (color[3] > 0.0f) {
      color[0] /= color[3];
      color[1] /= color[3];
      color[2] /= color[3];
    }

    it.out[0] = color[0] > 0.0f ? sqrtf(color[0]) : 0.0f;
    it.out[1] = color[1] > 0.0f ? sqrtf(color[1]) : 0.0f;
    it.out[2] = color[2] > 0.0f ? sqrtf(color[2]) : 0.0f;
    it.out[3] = color[3];

    if (color[3] > 0.0f) {
      it.out[0] *= color[3];
      it.out[1] *= color[3];
      it.out[2] *= color[3];
    }
  }
}

void GammaUncorrectOperation::deinit_execution()
{
  input_program_ = nullptr;
}

}  // namespace blender::compositor