/src/libjxl/lib/jxl/blending.cc
Line | Count | Source (jump to first uncovered line) |
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/blending.h" |
7 | | |
8 | | #include <jxl/memory_manager.h> |
9 | | |
10 | | #include <cstddef> |
11 | | #include <cstring> |
12 | | #include <vector> |
13 | | |
14 | | #include "lib/jxl/alpha.h" |
15 | | #include "lib/jxl/base/status.h" |
16 | | #include "lib/jxl/dec_patch_dictionary.h" |
17 | | #include "lib/jxl/frame_header.h" |
18 | | #include "lib/jxl/image.h" |
19 | | #include "lib/jxl/image_metadata.h" |
20 | | |
21 | | namespace jxl { |
22 | | |
23 | 37.8k | bool NeedsBlending(const FrameHeader& frame_header) { |
24 | 37.8k | if (!(frame_header.frame_type == FrameType::kRegularFrame || |
25 | 37.8k | frame_header.frame_type == FrameType::kSkipProgressive)) { |
26 | 15.4k | return false; |
27 | 15.4k | } |
28 | 22.4k | const auto& info = frame_header.blending_info; |
29 | 22.4k | bool replace_all = (info.mode == BlendMode::kReplace); |
30 | 22.4k | for (const auto& ec_i : frame_header.extra_channel_blending_info) { |
31 | 5.76k | if (ec_i.mode != BlendMode::kReplace) { |
32 | 2.68k | replace_all = false; |
33 | 2.68k | } |
34 | 5.76k | } |
35 | | // Replace the full frame: nothing to do. |
36 | 22.4k | if (!frame_header.custom_size_or_origin && replace_all) { |
37 | 15.8k | return false; |
38 | 15.8k | } |
39 | 6.56k | return true; |
40 | 22.4k | } |
41 | | |
42 | | Status PerformBlending( |
43 | | JxlMemoryManager* memory_manager, const float* const* bg, |
44 | | const float* const* fg, float* const* out, size_t x0, size_t xsize, |
45 | | const PatchBlending& color_blending, const PatchBlending* ec_blending, |
46 | 48.3M | const std::vector<ExtraChannelInfo>& extra_channel_info) { |
47 | 48.3M | bool has_alpha = false; |
48 | 48.3M | size_t num_ec = extra_channel_info.size(); |
49 | 48.3M | for (size_t i = 0; i < num_ec; i++) { |
50 | 301k | if (extra_channel_info[i].type == jxl::ExtraChannel::kAlpha) { |
51 | 290k | has_alpha = true; |
52 | 290k | break; |
53 | 290k | } |
54 | 301k | } |
55 | 48.3M | JXL_ASSIGN_OR_RETURN(ImageF tmp, |
56 | 48.3M | ImageF::Create(memory_manager, xsize, 3 + num_ec)); |
57 | | // Blend extra channels first so that we use the pre-blending alpha. |
58 | 48.8M | for (size_t i = 0; i < num_ec; i++) { |
59 | 458k | switch (ec_blending[i].mode) { |
60 | 82.5k | case PatchBlendMode::kAdd: |
61 | 26.9M | for (size_t x = 0; x < xsize; x++) { |
62 | 26.8M | tmp.Row(3 + i)[x] = bg[3 + i][x + x0] + fg[3 + i][x + x0]; |
63 | 26.8M | } |
64 | 82.5k | continue; |
65 | | |
66 | 43.1k | case PatchBlendMode::kBlendAbove: { |
67 | 43.1k | size_t alpha = ec_blending[i].alpha_channel; |
68 | 43.1k | bool is_premultiplied = extra_channel_info[alpha].alpha_associated; |
69 | 43.1k | PerformAlphaBlending(bg[3 + i] + x0, bg[3 + alpha] + x0, fg[3 + i] + x0, |
70 | 43.1k | fg[3 + alpha] + x0, tmp.Row(3 + i), xsize, |
71 | 43.1k | is_premultiplied, ec_blending[i].clamp); |
72 | 43.1k | continue; |
73 | 0 | } |
74 | | |
75 | 445 | case PatchBlendMode::kBlendBelow: { |
76 | 445 | size_t alpha = ec_blending[i].alpha_channel; |
77 | 445 | bool is_premultiplied = extra_channel_info[alpha].alpha_associated; |
78 | 445 | PerformAlphaBlending(fg[3 + i] + x0, fg[3 + alpha] + x0, bg[3 + i] + x0, |
79 | 445 | bg[3 + alpha] + x0, tmp.Row(3 + i), xsize, |
80 | 445 | is_premultiplied, ec_blending[i].clamp); |
81 | 445 | continue; |
82 | 0 | } |
83 | | |
84 | 7.59k | case PatchBlendMode::kAlphaWeightedAddAbove: { |
85 | 7.59k | size_t alpha = ec_blending[i].alpha_channel; |
86 | 7.59k | PerformAlphaWeightedAdd(bg[3 + i] + x0, fg[3 + i] + x0, |
87 | 7.59k | fg[3 + alpha] + x0, tmp.Row(3 + i), xsize, |
88 | 7.59k | ec_blending[i].clamp); |
89 | 7.59k | continue; |
90 | 0 | } |
91 | | |
92 | 1.02k | case PatchBlendMode::kAlphaWeightedAddBelow: { |
93 | 1.02k | size_t alpha = ec_blending[i].alpha_channel; |
94 | 1.02k | PerformAlphaWeightedAdd(fg[3 + i] + x0, bg[3 + i] + x0, |
95 | 1.02k | bg[3 + alpha] + x0, tmp.Row(3 + i), xsize, |
96 | 1.02k | ec_blending[i].clamp); |
97 | 1.02k | continue; |
98 | 0 | } |
99 | | |
100 | 21.3k | case PatchBlendMode::kMul: |
101 | 21.3k | PerformMulBlending(bg[3 + i] + x0, fg[3 + i] + x0, tmp.Row(3 + i), |
102 | 21.3k | xsize, ec_blending[i].clamp); |
103 | 21.3k | continue; |
104 | | |
105 | 141k | case PatchBlendMode::kReplace: |
106 | 141k | if (xsize) memcpy(tmp.Row(3 + i), fg[3 + i] + x0, xsize * sizeof(**fg)); |
107 | 141k | continue; |
108 | | |
109 | 161k | case PatchBlendMode::kNone: |
110 | 161k | if (xsize) memcpy(tmp.Row(3 + i), bg[3 + i] + x0, xsize * sizeof(**fg)); |
111 | 161k | continue; |
112 | 458k | } |
113 | 458k | } |
114 | 48.3M | size_t alpha = color_blending.alpha_channel; |
115 | | |
116 | 48.3M | const auto add = [&]() { |
117 | 192M | for (int p = 0; p < 3; p++) { |
118 | 144M | float* out = tmp.Row(p); |
119 | 6.83G | for (size_t x = 0; x < xsize; x++) { |
120 | 6.68G | out[x] = bg[p][x + x0] + fg[p][x + x0]; |
121 | 6.68G | } |
122 | 144M | } |
123 | 48.1M | }; |
124 | | |
125 | 48.3M | const auto blend_weighted = [&](const float* const* bottom, |
126 | 48.3M | const float* const* top) { |
127 | 41.6k | bool is_premultiplied = extra_channel_info[alpha].alpha_associated; |
128 | 41.6k | PerformAlphaBlending( |
129 | 41.6k | {bottom[0] + x0, bottom[1] + x0, bottom[2] + x0, |
130 | 41.6k | bottom[3 + alpha] + x0}, |
131 | 41.6k | {top[0] + x0, top[1] + x0, top[2] + x0, top[3 + alpha] + x0}, |
132 | 41.6k | {tmp.Row(0), tmp.Row(1), tmp.Row(2), tmp.Row(3 + alpha)}, xsize, |
133 | 41.6k | is_premultiplied, color_blending.clamp); |
134 | 41.6k | }; |
135 | | |
136 | 48.3M | const auto add_weighted = [&](const float* const* bottom, |
137 | 48.3M | const float* const* top) { |
138 | 96.4k | for (size_t c = 0; c < 3; c++) { |
139 | 72.3k | PerformAlphaWeightedAdd(bottom[c] + x0, top[c] + x0, top[3 + alpha] + x0, |
140 | 72.3k | tmp.Row(c), xsize, color_blending.clamp); |
141 | 72.3k | } |
142 | 24.1k | }; |
143 | | |
144 | 48.3M | const auto copy = [&](const float* const* src) { |
145 | 574k | for (size_t p = 0; p < 3; p++) { |
146 | 430k | memcpy(tmp.Row(p), src[p] + x0, xsize * sizeof(**src)); |
147 | 430k | } |
148 | 143k | }; |
149 | | |
150 | 48.3M | switch (color_blending.mode) { |
151 | 47.9M | case PatchBlendMode::kAdd: |
152 | 47.9M | add(); |
153 | 47.9M | break; |
154 | | |
155 | 236k | case PatchBlendMode::kAlphaWeightedAddAbove: |
156 | 236k | has_alpha ? add_weighted(bg, fg) : add(); |
157 | 236k | break; |
158 | | |
159 | 1.08k | case PatchBlendMode::kAlphaWeightedAddBelow: |
160 | 1.08k | has_alpha ? add_weighted(fg, bg) : add(); |
161 | 1.08k | break; |
162 | | |
163 | 88.6k | case PatchBlendMode::kBlendAbove: |
164 | 88.6k | has_alpha ? blend_weighted(bg, fg) : copy(fg); |
165 | 88.6k | break; |
166 | | |
167 | 436 | case PatchBlendMode::kBlendBelow: |
168 | 436 | has_alpha ? blend_weighted(fg, bg) : copy(fg); |
169 | 436 | break; |
170 | | |
171 | 9.26k | case PatchBlendMode::kMul: |
172 | 37.0k | for (int p = 0; p < 3; p++) { |
173 | 27.7k | PerformMulBlending(bg[p] + x0, fg[p] + x0, tmp.Row(p), xsize, |
174 | 27.7k | color_blending.clamp); |
175 | 27.7k | } |
176 | 9.26k | break; |
177 | | |
178 | 33.9k | case PatchBlendMode::kReplace: |
179 | 33.9k | copy(fg); |
180 | 33.9k | break; |
181 | | |
182 | 62.2k | case PatchBlendMode::kNone: |
183 | 62.2k | copy(bg); |
184 | 48.3M | } |
185 | | |
186 | 193M | for (size_t i = 0; i < 3 + num_ec; i++) { |
187 | 145M | if (xsize != 0) memcpy(out[i] + x0, tmp.Row(i), xsize * sizeof(**out)); |
188 | 145M | } |
189 | 48.3M | return true; |
190 | 48.3M | } |
191 | | |
192 | | } // namespace jxl |