Debian Codegraph @

lib/jxl/render_pipeline/stage_cms.cc

raw
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 #include "lib/jxl/render_pipeline/stage_cms.h"
7
8 #include <memory>
9
10 #include "lib/jxl/base/status.h"
11 #include "lib/jxl/color_encoding_internal.h"
12 #include "lib/jxl/dec_xyb.h"
13
14 #undef HWY_TARGET_INCLUDE
15 #define HWY_TARGET_INCLUDE "lib/jxl/render_pipeline/stage_cms.cc"
16 #include <hwy/foreach_target.h>
17 #include <hwy/highway.h>
18
19 #include "lib/jxl/dec_xyb-inl.h"
20
21 HWY_BEFORE_NAMESPACE();
22 namespace jxl {
23 namespace HWY_NAMESPACE {
24
25 class CmsStage : public RenderPipelineStage {
26 public:
27 explicit CmsStage(OutputEncodingInfo output_encoding_info)
28 : RenderPipelineStage(RenderPipelineStage::Settings()),
29 output_encoding_info_(std::move(output_encoding_info)) {
30 c_src_ = output_encoding_info_.linear_color_encoding;
31 }
32
33 bool IsNeeded() const {
34 const size_t channels_src = (c_src_.IsCMYK() ? 4 : c_src_.Channels());
35 const size_t channels_dst = output_encoding_info_.color_encoding.Channels();
36 const bool not_mixing_color_and_grey =
37 (channels_src == channels_dst ||
38 (channels_src == 4 && channels_dst == 3));
39 return (output_encoding_info_.cms_set) &&
40 !c_src_.SameColorEncoding(output_encoding_info_.color_encoding) &&
41 not_mixing_color_and_grey;
42 }
43
44 Status ProcessRow(const RowInfo& input_rows, const RowInfo& output_rows,
45 size_t xextra, size_t xsize, size_t xpos, size_t ypos,
46 size_t thread_id) const final {
47 JXL_ENSURE(xsize <= xsize_);
48 bool gray_src = (c_src_.Channels() == 1);
49 bool gray_dst = (output_encoding_info_.color_encoding.Channels() == 1);
50 float* mutable_buf_src = color_space_transform->BufSrc(thread_id);
51 float* JXL_RESTRICT buf_dst = color_space_transform->BufDst(thread_id);
52 // interleave
53 if (gray_src) {
54 float* JXL_RESTRICT row0 = GetInputRow(input_rows, 0, 0);
55 memcpy(mutable_buf_src, row0, xsize * sizeof(float));
56 } else {
57 float* JXL_RESTRICT row0 = GetInputRow(input_rows, 0, 0);
58 float* JXL_RESTRICT row1 = GetInputRow(input_rows, 1, 0);
59 float* JXL_RESTRICT row2 = GetInputRow(input_rows, 2, 0);
60
61 for (size_t x = 0; x < xsize; x++) {
62 mutable_buf_src[3 * x + 0] = row0[x];
63 mutable_buf_src[3 * x + 1] = row1[x];
64 mutable_buf_src[3 * x + 2] = row2[x];
65 }
66 }
67 const float* buf_src = mutable_buf_src;
68 JXL_RETURN_IF_ERROR(
69 color_space_transform->Run(thread_id, buf_src, buf_dst, xsize));
70 // de-interleave
71 if (gray_dst) {
72 float* JXL_RESTRICT row0 = GetInputRow(input_rows, 0, 0);
73 float* JXL_RESTRICT row1 = GetInputRow(input_rows, 1, 0);
74 float* JXL_RESTRICT row2 = GetInputRow(input_rows, 2, 0);
75 memcpy(row0, buf_dst, xsize * sizeof(float));
76 memcpy(row1, buf_dst, xsize * sizeof(float));
77 memcpy(row2, buf_dst, xsize * sizeof(float));
78 } else {
79 float* JXL_RESTRICT row0 = GetInputRow(input_rows, 0, 0);
80 float* JXL_RESTRICT row1 = GetInputRow(input_rows, 1, 0);
81 float* JXL_RESTRICT row2 = GetInputRow(input_rows, 2, 0);
82 for (size_t x = 0; x < xsize; x++) {
83 row0[x] = buf_dst[3 * x + 0];
84 row1[x] = buf_dst[3 * x + 1];
85 row2[x] = buf_dst[3 * x + 2];
86 }
87 }
88 return true;
89 }
90 RenderPipelineChannelMode GetChannelMode(size_t c) const final {
91 return c < 3 ? RenderPipelineChannelMode::kInPlace
92 : RenderPipelineChannelMode::kIgnored;
93 }
94
95 const char* GetName() const override { return "Cms"; }
96
97 private:
98 OutputEncodingInfo output_encoding_info_;
99 size_t xsize_;
100 std::unique_ptr<jxl::ColorSpaceTransform> color_space_transform;
101 ColorEncoding c_src_;
102
103 Status SetInputSizes(
104 const std::vector<std::pair<size_t, size_t>>& input_sizes) override {
105 JXL_ENSURE(input_sizes.size() >= 3);
106 for (size_t c = 1; c < input_sizes.size(); c++) {
107 JXL_ENSURE(input_sizes[c].first == input_sizes[0].first);
108 JXL_ENSURE(input_sizes[c].second == input_sizes[0].second);
109 }
110 xsize_ = input_sizes[0].first;
111 return true;
112 }
113
114 Status PrepareForThreads(size_t num_threads) override {
115 color_space_transform = jxl::make_unique<jxl::ColorSpaceTransform>(
116 output_encoding_info_.color_management_system);
117 JXL_RETURN_IF_ERROR(color_space_transform->Init(
118 c_src_, output_encoding_info_.color_encoding,
119 output_encoding_info_.desired_intensity_target, xsize_, num_threads));
120 return true;
121 }
122 };
123
124 std::unique_ptr<RenderPipelineStage> GetCmsStage(
125 const OutputEncodingInfo& output_encoding_info) {
126 auto stage = jxl::make_unique<CmsStage>(output_encoding_info);
127 if (!stage->IsNeeded()) return nullptr;
128 return stage;
129 }
130
131 // NOLINTNEXTLINE(google-readability-namespace-comments)
132 } // namespace HWY_NAMESPACE
133 } // namespace jxl
134 HWY_AFTER_NAMESPACE();
135
136 #if HWY_ONCE
137 namespace jxl {
138
139 HWY_EXPORT(GetCmsStage);
140
141 std::unique_ptr<RenderPipelineStage> GetCmsStage(
142 const OutputEncodingInfo& output_encoding_info) {
143 return HWY_DYNAMIC_DISPATCH(GetCmsStage)(output_encoding_info);
144 }
145
146 } // namespace jxl
147 #endif