Blender V5.0
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
7#if defined(WITH_OPENCOLORIO)
8
9# include "OCIO_packed_image.hh"
10
11# include "BLI_assert.h"
12
13# include "error_handling.hh"
14
15namespace blender::ocio {
16
17LibOCIOCPUProcessor::LibOCIOCPUProcessor(
18 const OCIO_NAMESPACE::ConstCPUProcessorRcPtr &ocio_cpu_processor)
19 : ocio_cpu_processor_(ocio_cpu_processor)
20{
21 BLI_assert(ocio_cpu_processor_);
22}
23
24void LibOCIOCPUProcessor::apply_rgb(float rgb[3]) const
25{
26 ocio_cpu_processor_->applyRGB(rgb);
27}
28
29void LibOCIOCPUProcessor::apply_rgba(float rgba[4]) const
30{
31 ocio_cpu_processor_->applyRGBA(rgba);
32}
33
34void LibOCIOCPUProcessor::apply_rgba_predivide(float rgba[4]) const
35{
36 if (ELEM(rgba[3], 1.0f, 0.0f)) {
37 apply_rgba(rgba);
38 return;
39 }
40
41 const float alpha = rgba[3];
42 const float inv_alpha = 1.0f / alpha;
43
44 rgba[0] *= inv_alpha;
45 rgba[1] *= inv_alpha;
46 rgba[2] *= inv_alpha;
47
48 apply_rgba(rgba);
49
50 rgba[0] *= alpha;
51 rgba[1] *= alpha;
52 rgba[2] *= alpha;
53}
54
55void LibOCIOCPUProcessor::apply(const PackedImage &image) const
56{
57 /* TODO(sergey): Support other bit depths. */
58 if (image.get_bit_depth() != BitDepth::BIT_DEPTH_F32) {
59 return;
60 }
61
62 try {
63 ocio_cpu_processor_->apply(image);
64 }
65 catch (OCIO_NAMESPACE::Exception &exception) {
66 report_exception(exception);
67 }
68}
69
70void LibOCIOCPUProcessor::apply_predivide(const PackedImage &image) const
71{
72 /* TODO(sergey): Support other bit depths. */
73 if (image.get_bit_depth() != BitDepth::BIT_DEPTH_F32) {
74 return;
75 }
76
77 if (image.get_num_channels() == 4) {
78 /* Convert from premultiplied alpha to straight alpha. */
79 float *pixel = reinterpret_cast<float *>(image.get_data());
80 const size_t pixel_count = image.get_width() * image.get_height();
81 for (size_t i = 0; i < pixel_count; i++, pixel += 4) {
82 const float alpha = pixel[3];
83 if (!ELEM(alpha, 0.0f, 1.0f)) {
84 const float inv_alpha = 1.0f / alpha;
85 pixel[0] *= inv_alpha;
86 pixel[1] *= inv_alpha;
87 pixel[2] *= inv_alpha;
88 }
89 }
90 }
91
92 apply(image);
93
94 if (image.get_num_channels() == 4) {
95 /* Back to premultiplied alpha. */
96 float *pixel = reinterpret_cast<float *>(image.get_data());
97 const size_t pixel_count = image.get_width() * image.get_height();
98 for (size_t i = 0; i < pixel_count; i++, pixel += 4) {
99 const float alpha = pixel[3];
100 if (!ELEM(alpha, 0.0f, 1.0f)) {
101 pixel[0] *= alpha;
102 pixel[1] *= alpha;
103 pixel[2] *= alpha;
104 }
105 }
106 }
107}
108
109} // namespace blender::ocio
110
111#endif
#define BLI_assert(a)
Definition BLI_assert.h:46
#define ELEM(...)
void apply(bContext &C, GestureData &gesture_data, wmOperator &op)
i
Definition text_draw.cc:230