Coverage Report

Created: 2026-03-31 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/blending.cc
Line
Count
Source
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
66.1k
bool NeedsBlending(const FrameHeader& frame_header) {
24
66.1k
  if (!(frame_header.frame_type == FrameType::kRegularFrame ||
25
21.6k
        frame_header.frame_type == FrameType::kSkipProgressive)) {
26
14.8k
    return false;
27
14.8k
  }
28
51.2k
  const auto& info = frame_header.blending_info;
29
51.2k
  bool replace_all = (info.mode == BlendMode::kReplace);
30
56.9k
  for (const auto& ec_i : frame_header.extra_channel_blending_info) {
31
56.9k
    if (ec_i.mode != BlendMode::kReplace) {
32
22.3k
      replace_all = false;
33
22.3k
    }
34
56.9k
  }
35
  // Replace the full frame: nothing to do.
36
51.2k
  if (!frame_header.custom_size_or_origin && replace_all) {
37
30.0k
    return false;
38
30.0k
  }
39
21.2k
  return true;
40
51.2k
}
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
21.6M
    const std::vector<ExtraChannelInfo>& extra_channel_info) {
47
21.6M
  bool has_alpha = false;
48
21.6M
  size_t num_ec = extra_channel_info.size();
49
21.8M
  for (size_t i = 0; i < num_ec; i++) {
50
547k
    if (extra_channel_info[i].type == jxl::ExtraChannel::kAlpha) {
51
404k
      has_alpha = true;
52
404k
      break;
53
404k
    }
54
547k
  }
55
21.6M
  JXL_ASSIGN_OR_RETURN(ImageF tmp,
56
21.6M
                       ImageF::Create(memory_manager, xsize, 3 + num_ec));
57
  // Blend extra channels first so that we use the pre-blending alpha.
58
24.9M
  for (size_t i = 0; i < num_ec; i++) {
59
3.28M
    switch (ec_blending[i].mode) {
60
511k
      case PatchBlendMode::kAdd:
61
110M
        for (size_t x = 0; x < xsize; x++) {
62
109M
          tmp.Row(3 + i)[x] = bg[3 + i][x + x0] + fg[3 + i][x + x0];
63
109M
        }
64
511k
        continue;
65
66
566k
      case PatchBlendMode::kBlendAbove: {
67
566k
        size_t alpha = ec_blending[i].alpha_channel;
68
566k
        bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
69
566k
        PerformAlphaBlending(bg[3 + i] + x0, bg[3 + alpha] + x0, fg[3 + i] + x0,
70
566k
                             fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
71
566k
                             is_premultiplied, ec_blending[i].clamp);
72
566k
        continue;
73
0
      }
74
75
69
      case PatchBlendMode::kBlendBelow: {
76
69
        size_t alpha = ec_blending[i].alpha_channel;
77
69
        bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
78
69
        PerformAlphaBlending(fg[3 + i] + x0, fg[3 + alpha] + x0, bg[3 + i] + x0,
79
69
                             bg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
80
69
                             is_premultiplied, ec_blending[i].clamp);
81
69
        continue;
82
0
      }
83
84
285k
      case PatchBlendMode::kAlphaWeightedAddAbove: {
85
285k
        size_t alpha = ec_blending[i].alpha_channel;
86
285k
        PerformAlphaWeightedAdd(bg[3 + i] + x0, fg[3 + i] + x0,
87
285k
                                fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
88
285k
                                ec_blending[i].clamp);
89
285k
        continue;
90
0
      }
91
92
105
      case PatchBlendMode::kAlphaWeightedAddBelow: {
93
105
        size_t alpha = ec_blending[i].alpha_channel;
94
105
        PerformAlphaWeightedAdd(fg[3 + i] + x0, bg[3 + i] + x0,
95
105
                                bg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
96
105
                                ec_blending[i].clamp);
97
105
        continue;
98
0
      }
99
100
200k
      case PatchBlendMode::kMul:
101
200k
        PerformMulBlending(bg[3 + i] + x0, fg[3 + i] + x0, tmp.Row(3 + i),
102
200k
                           xsize, ec_blending[i].clamp);
103
200k
        continue;
104
105
1.71M
      case PatchBlendMode::kReplace:
106
1.71M
        if (xsize) memcpy(tmp.Row(3 + i), fg[3 + i] + x0, xsize * sizeof(**fg));
107
1.71M
        continue;
108
109
6.38k
      case PatchBlendMode::kNone:
110
6.38k
        if (xsize) memcpy(tmp.Row(3 + i), bg[3 + i] + x0, xsize * sizeof(**fg));
111
6.38k
        continue;
112
3.28M
    }
113
3.28M
  }
114
21.6M
  size_t alpha = color_blending.alpha_channel;
115
116
21.6M
  const auto add = [&]() {
117
83.4M
    for (int p = 0; p < 3; p++) {
118
62.5M
      float* out = tmp.Row(p);
119
2.96G
      for (size_t x = 0; x < xsize; x++) {
120
2.90G
        out[x] = bg[p][x + x0] + fg[p][x + x0];
121
2.90G
      }
122
62.5M
    }
123
20.8M
  };
124
125
21.6M
  const auto blend_weighted = [&](const float* const* bottom,
126
21.6M
                                  const float* const* top) {
127
74.4k
    bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
128
74.4k
    PerformAlphaBlending(
129
74.4k
        {bottom[0] + x0, bottom[1] + x0, bottom[2] + x0,
130
74.4k
         bottom[3 + alpha] + x0},
131
74.4k
        {top[0] + x0, top[1] + x0, top[2] + x0, top[3 + alpha] + x0},
132
74.4k
        {tmp.Row(0), tmp.Row(1), tmp.Row(2), tmp.Row(3 + alpha)}, xsize,
133
74.4k
        is_premultiplied, color_blending.clamp);
134
74.4k
  };
135
136
21.6M
  const auto add_weighted = [&](const float* const* bottom,
137
21.6M
                                const float* const* top) {
138
38.8k
    for (size_t c = 0; c < 3; c++) {
139
29.1k
      PerformAlphaWeightedAdd(bottom[c] + x0, top[c] + x0, top[3 + alpha] + x0,
140
29.1k
                              tmp.Row(c), xsize, color_blending.clamp);
141
29.1k
    }
142
9.71k
  };
143
144
21.6M
  const auto copy = [&](const float* const* src) {
145
2.78M
    for (size_t p = 0; p < 3; p++) {
146
2.09M
      memcpy(tmp.Row(p), src[p] + x0, xsize * sizeof(**src));
147
2.09M
    }
148
697k
  };
149
150
21.6M
  switch (color_blending.mode) {
151
20.7M
    case PatchBlendMode::kAdd:
152
20.7M
      add();
153
20.7M
      break;
154
155
82.0k
    case PatchBlendMode::kAlphaWeightedAddAbove:
156
82.0k
      has_alpha ? add_weighted(bg, fg) : add();
157
82.0k
      break;
158
159
19.5k
    case PatchBlendMode::kAlphaWeightedAddBelow:
160
19.5k
      has_alpha ? add_weighted(fg, bg) : add();
161
19.5k
      break;
162
163
334k
    case PatchBlendMode::kBlendAbove:
164
334k
      has_alpha ? blend_weighted(bg, fg) : copy(fg);
165
334k
      break;
166
167
1.24k
    case PatchBlendMode::kBlendBelow:
168
1.24k
      has_alpha ? blend_weighted(fg, bg) : copy(fg);
169
1.24k
      break;
170
171
24.9k
    case PatchBlendMode::kMul:
172
99.7k
      for (int p = 0; p < 3; p++) {
173
74.7k
        PerformMulBlending(bg[p] + x0, fg[p] + x0, tmp.Row(p), xsize,
174
74.7k
                           color_blending.clamp);
175
74.7k
      }
176
24.9k
      break;
177
178
295k
    case PatchBlendMode::kReplace:
179
295k
      copy(fg);
180
295k
      break;
181
182
140k
    case PatchBlendMode::kNone:
183
140k
      copy(bg);
184
21.6M
  }
185
186
89.9M
  for (size_t i = 0; i < 3 + num_ec; i++) {
187
68.2M
    if (xsize != 0) memcpy(out[i] + x0, tmp.Row(i), xsize * sizeof(**out));
188
68.2M
  }
189
21.6M
  return true;
190
21.6M
}
191
192
}  // namespace jxl