Blender V5.3
libocio_cpu_processor.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
8
9#include "BLI_assert.hh"
10
11#include "error_handling.hh"
12
13namespace blender::ocio {
14
16 const OCIO_NAMESPACE::ConstCPUProcessorRcPtr &ocio_cpu_processor)
17 : ocio_cpu_processor_(ocio_cpu_processor)
18{
19 BLI_assert(ocio_cpu_processor_);
20}
21
22void LibOCIOCPUProcessor::apply_rgb(float rgb[3]) const
23{
24 ocio_cpu_processor_->applyRGB(rgb);
25}
26
27void LibOCIOCPUProcessor::apply_rgba(float rgba[4]) const
28{
29 ocio_cpu_processor_->applyRGBA(rgba);
30}
31
33{
34 if (ELEM(rgba[3], 1.0f, 0.0f)) {
35 apply_rgba(rgba);
36 return;
37 }
38
39 const float alpha = rgba[3];
40 const float inv_alpha = 1.0f / alpha;
41
42 rgba[0] *= inv_alpha;
43 rgba[1] *= inv_alpha;
44 rgba[2] *= inv_alpha;
45
46 apply_rgba(rgba);
47
48 rgba[0] *= alpha;
49 rgba[1] *= alpha;
50 rgba[2] *= alpha;
51}
52
54{
55 /* TODO(sergey): Support other bit depths. */
56 if (image.get_bit_depth() != BitDepth::BIT_DEPTH_F32) {
57 return;
58 }
59
60 try {
61 ocio_cpu_processor_->apply(image);
62 }
63 catch (OCIO_NAMESPACE::Exception &exception) {
64 report_exception(exception);
65 }
66}
67
69{
70 /* TODO(sergey): Support other bit depths. */
71 if (image.get_bit_depth() != BitDepth::BIT_DEPTH_F32) {
72 return;
73 }
74
75 if (image.get_num_channels() == 4) {
76 /* Convert from premultiplied alpha to straight alpha. */
77 float *pixel = reinterpret_cast<float *>(image.get_data());
78 const size_t pixel_count = image.get_width() * image.get_height();
79 for (size_t i = 0; i < pixel_count; i++, pixel += 4) {
80 const float alpha = pixel[3];
81 if (!ELEM(alpha, 0.0f, 1.0f)) {
82 const float inv_alpha = 1.0f / alpha;
83 pixel[0] *= inv_alpha;
84 pixel[1] *= inv_alpha;
85 pixel[2] *= inv_alpha;
86 }
87 }
88 }
89
90 apply(image);
91
92 if (image.get_num_channels() == 4) {
93 /* Back to premultiplied alpha. */
94 float *pixel = reinterpret_cast<float *>(image.get_data());
95 const size_t pixel_count = image.get_width() * image.get_height();
96 for (size_t i = 0; i < pixel_count; i++, pixel += 4) {
97 const float alpha = pixel[3];
98 if (!ELEM(alpha, 0.0f, 1.0f)) {
99 pixel[0] *= alpha;
100 pixel[1] *= alpha;
101 pixel[2] *= alpha;
102 }
103 }
104 }
105}
106
107} // namespace blender::ocio
#define ELEM(...)
void apply(const PackedImage &image) const override
void apply_rgb(float rgb[3]) const override
void apply_rgba_predivide(float rgba[4]) const override
void apply_rgba(float rgba[4]) const override
LibOCIOCPUProcessor(const OCIO_NAMESPACE::ConstCPUProcessorRcPtr &ocio_cpu_processor)
void apply_predivide(const PackedImage &image) const override
void report_exception(const OCIO_NAMESPACE::Exception &exception)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)