Coverage Report

Created: 2026-02-14 07:11

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
29.4k
bool NeedsBlending(const FrameHeader& frame_header) {
24
29.4k
  if (!(frame_header.frame_type == FrameType::kRegularFrame ||
25
14.2k
        frame_header.frame_type == FrameType::kSkipProgressive)) {
26
12.1k
    return false;
27
12.1k
  }
28
17.3k
  const auto& info = frame_header.blending_info;
29
17.3k
  bool replace_all = (info.mode == BlendMode::kReplace);
30
17.3k
  for (const auto& ec_i : frame_header.extra_channel_blending_info) {
31
7.40k
    if (ec_i.mode != BlendMode::kReplace) {
32
2.87k
      replace_all = false;
33
2.87k
    }
34
7.40k
  }
35
  // Replace the full frame: nothing to do.
36
17.3k
  if (!frame_header.custom_size_or_origin && replace_all) {
37
8.53k
    return false;
38
8.53k
  }
39
8.82k
  return true;
40
17.3k
}
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
30.9M
    const std::vector<ExtraChannelInfo>& extra_channel_info) {
47
30.9M
  bool has_alpha = false;
48
30.9M
  size_t num_ec = extra_channel_info.size();
49
31.0M
  for (size_t i = 0; i < num_ec; i++) {
50
149k
    if (extra_channel_info[i].type == jxl::ExtraChannel::kAlpha) {
51
122k
      has_alpha = true;
52
122k
      break;
53
122k
    }
54
149k
  }
55
30.9M
  JXL_ASSIGN_OR_RETURN(ImageF tmp,
56
30.9M
                       ImageF::Create(memory_manager, xsize, 3 + num_ec));
57
  // Blend extra channels first so that we use the pre-blending alpha.
58
31.2M
  for (size_t i = 0; i < num_ec; i++) {
59
283k
    switch (ec_blending[i].mode) {
60
15.3k
      case PatchBlendMode::kAdd:
61
1.12M
        for (size_t x = 0; x < xsize; x++) {
62
1.10M
          tmp.Row(3 + i)[x] = bg[3 + i][x + x0] + fg[3 + i][x + x0];
63
1.10M
        }
64
15.3k
        continue;
65
66
54.0k
      case PatchBlendMode::kBlendAbove: {
67
54.0k
        size_t alpha = ec_blending[i].alpha_channel;
68
54.0k
        bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
69
54.0k
        PerformAlphaBlending(bg[3 + i] + x0, bg[3 + alpha] + x0, fg[3 + i] + x0,
70
54.0k
                             fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
71
54.0k
                             is_premultiplied, ec_blending[i].clamp);
72
54.0k
        continue;
73
0
      }
74
75
0
      case PatchBlendMode::kBlendBelow: {
76
0
        size_t alpha = ec_blending[i].alpha_channel;
77
0
        bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
78
0
        PerformAlphaBlending(fg[3 + i] + x0, fg[3 + alpha] + x0, bg[3 + i] + x0,
79
0
                             bg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
80
0
                             is_premultiplied, ec_blending[i].clamp);
81
0
        continue;
82
0
      }
83
84
12.1k
      case PatchBlendMode::kAlphaWeightedAddAbove: {
85
12.1k
        size_t alpha = ec_blending[i].alpha_channel;
86
12.1k
        PerformAlphaWeightedAdd(bg[3 + i] + x0, fg[3 + i] + x0,
87
12.1k
                                fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
88
12.1k
                                ec_blending[i].clamp);
89
12.1k
        continue;
90
0
      }
91
92
0
      case PatchBlendMode::kAlphaWeightedAddBelow: {
93
0
        size_t alpha = ec_blending[i].alpha_channel;
94
0
        PerformAlphaWeightedAdd(fg[3 + i] + x0, bg[3 + i] + x0,
95
0
                                bg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
96
0
                                ec_blending[i].clamp);
97
0
        continue;
98
0
      }
99
100
54.6k
      case PatchBlendMode::kMul:
101
54.6k
        PerformMulBlending(bg[3 + i] + x0, fg[3 + i] + x0, tmp.Row(3 + i),
102
54.6k
                           xsize, ec_blending[i].clamp);
103
54.6k
        continue;
104
105
146k
      case PatchBlendMode::kReplace:
106
146k
        if (xsize) memcpy(tmp.Row(3 + i), fg[3 + i] + x0, xsize * sizeof(**fg));
107
146k
        continue;
108
109
124
      case PatchBlendMode::kNone:
110
124
        if (xsize) memcpy(tmp.Row(3 + i), bg[3 + i] + x0, xsize * sizeof(**fg));
111
124
        continue;
112
283k
    }
113
283k
  }
114
30.9M
  size_t alpha = color_blending.alpha_channel;
115
116
30.9M
  const auto add = [&]() {
117
122M
    for (int p = 0; p < 3; p++) {
118
91.5M
      float* out = tmp.Row(p);
119
3.23G
      for (size_t x = 0; x < xsize; x++) {
120
3.14G
        out[x] = bg[p][x + x0] + fg[p][x + x0];
121
3.14G
      }
122
91.5M
    }
123
30.5M
  };
124
125
30.9M
  const auto blend_weighted = [&](const float* const* bottom,
126
30.9M
                                  const float* const* top) {
127
22.2k
    bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
128
22.2k
    PerformAlphaBlending(
129
22.2k
        {bottom[0] + x0, bottom[1] + x0, bottom[2] + x0,
130
22.2k
         bottom[3 + alpha] + x0},
131
22.2k
        {top[0] + x0, top[1] + x0, top[2] + x0, top[3 + alpha] + x0},
132
22.2k
        {tmp.Row(0), tmp.Row(1), tmp.Row(2), tmp.Row(3 + alpha)}, xsize,
133
22.2k
        is_premultiplied, color_blending.clamp);
134
22.2k
  };
135
136
30.9M
  const auto add_weighted = [&](const float* const* bottom,
137
30.9M
                                const float* const* top) {
138
86.8k
    for (size_t c = 0; c < 3; c++) {
139
65.1k
      PerformAlphaWeightedAdd(bottom[c] + x0, top[c] + x0, top[3 + alpha] + x0,
140
65.1k
                              tmp.Row(c), xsize, color_blending.clamp);
141
65.1k
    }
142
21.7k
  };
143
144
30.9M
  const auto copy = [&](const float* const* src) {
145
1.52M
    for (size_t p = 0; p < 3; p++) {
146
1.14M
      memcpy(tmp.Row(p), src[p] + x0, xsize * sizeof(**src));
147
1.14M
    }
148
382k
  };
149
150
30.9M
  switch (color_blending.mode) {
151
30.4M
    case PatchBlendMode::kAdd:
152
30.4M
      add();
153
30.4M
      break;
154
155
100k
    case PatchBlendMode::kAlphaWeightedAddAbove:
156
100k
      has_alpha ? add_weighted(bg, fg) : add();
157
100k
      break;
158
159
5.21k
    case PatchBlendMode::kAlphaWeightedAddBelow:
160
5.21k
      has_alpha ? add_weighted(fg, bg) : add();
161
5.21k
      break;
162
163
316k
    case PatchBlendMode::kBlendAbove:
164
316k
      has_alpha ? blend_weighted(bg, fg) : copy(fg);
165
316k
      break;
166
167
2.41k
    case PatchBlendMode::kBlendBelow:
168
2.41k
      has_alpha ? blend_weighted(fg, bg) : copy(fg);
169
2.41k
      break;
170
171
42.6k
    case PatchBlendMode::kMul:
172
170k
      for (int p = 0; p < 3; p++) {
173
128k
        PerformMulBlending(bg[p] + x0, fg[p] + x0, tmp.Row(p), xsize,
174
128k
                           color_blending.clamp);
175
128k
      }
176
42.6k
      break;
177
178
28.1k
    case PatchBlendMode::kReplace:
179
28.1k
      copy(fg);
180
28.1k
      break;
181
182
57.7k
    case PatchBlendMode::kNone:
183
57.7k
      copy(bg);
184
30.9M
  }
185
186
124M
  for (size_t i = 0; i < 3 + num_ec; i++) {
187
93.2M
    if (xsize != 0) memcpy(out[i] + x0, tmp.Row(i), xsize * sizeof(**out));
188
93.2M
  }
189
30.9M
  return true;
190
30.9M
}
191
192
}  // namespace jxl