Coverage Report

Created: 2025-11-16 07:22

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
32.4k
bool NeedsBlending(const FrameHeader& frame_header) {
24
32.4k
  if (!(frame_header.frame_type == FrameType::kRegularFrame ||
25
14.3k
        frame_header.frame_type == FrameType::kSkipProgressive)) {
26
9.34k
    return false;
27
9.34k
  }
28
23.0k
  const auto& info = frame_header.blending_info;
29
23.0k
  bool replace_all = (info.mode == BlendMode::kReplace);
30
23.0k
  for (const auto& ec_i : frame_header.extra_channel_blending_info) {
31
8.73k
    if (ec_i.mode != BlendMode::kReplace) {
32
884
      replace_all = false;
33
884
    }
34
8.73k
  }
35
  // Replace the full frame: nothing to do.
36
23.0k
  if (!frame_header.custom_size_or_origin && replace_all) {
37
13.2k
    return false;
38
13.2k
  }
39
9.82k
  return true;
40
23.0k
}
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
13.4M
    const std::vector<ExtraChannelInfo>& extra_channel_info) {
47
13.4M
  bool has_alpha = false;
48
13.4M
  size_t num_ec = extra_channel_info.size();
49
13.4M
  for (size_t i = 0; i < num_ec; i++) {
50
22.9k
    if (extra_channel_info[i].type == jxl::ExtraChannel::kAlpha) {
51
7.34k
      has_alpha = true;
52
7.34k
      break;
53
7.34k
    }
54
22.9k
  }
55
13.4M
  JXL_ASSIGN_OR_RETURN(ImageF tmp,
56
13.4M
                       ImageF::Create(memory_manager, xsize, 3 + num_ec));
57
  // Blend extra channels first so that we use the pre-blending alpha.
58
13.5M
  for (size_t i = 0; i < num_ec; i++) {
59
87.6k
    switch (ec_blending[i].mode) {
60
11.6k
      case PatchBlendMode::kAdd:
61
2.13M
        for (size_t x = 0; x < xsize; x++) {
62
2.12M
          tmp.Row(3 + i)[x] = bg[3 + i][x + x0] + fg[3 + i][x + x0];
63
2.12M
        }
64
11.6k
        continue;
65
66
15.5k
      case PatchBlendMode::kBlendAbove: {
67
15.5k
        size_t alpha = ec_blending[i].alpha_channel;
68
15.5k
        bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
69
15.5k
        PerformAlphaBlending(bg[3 + i] + x0, bg[3 + alpha] + x0, fg[3 + i] + x0,
70
15.5k
                             fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
71
15.5k
                             is_premultiplied, ec_blending[i].clamp);
72
15.5k
        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
10.8k
      case PatchBlendMode::kAlphaWeightedAddAbove: {
85
10.8k
        size_t alpha = ec_blending[i].alpha_channel;
86
10.8k
        PerformAlphaWeightedAdd(bg[3 + i] + x0, fg[3 + i] + x0,
87
10.8k
                                fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
88
10.8k
                                ec_blending[i].clamp);
89
10.8k
        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
2.45k
      case PatchBlendMode::kMul:
101
2.45k
        PerformMulBlending(bg[3 + i] + x0, fg[3 + i] + x0, tmp.Row(3 + i),
102
2.45k
                           xsize, ec_blending[i].clamp);
103
2.45k
        continue;
104
105
47.0k
      case PatchBlendMode::kReplace:
106
47.0k
        if (xsize) memcpy(tmp.Row(3 + i), fg[3 + i] + x0, xsize * sizeof(**fg));
107
47.0k
        continue;
108
109
0
      case PatchBlendMode::kNone:
110
0
        if (xsize) memcpy(tmp.Row(3 + i), bg[3 + i] + x0, xsize * sizeof(**fg));
111
0
        continue;
112
87.6k
    }
113
87.6k
  }
114
13.4M
  size_t alpha = color_blending.alpha_channel;
115
116
13.4M
  const auto add = [&]() {
117
52.9M
    for (int p = 0; p < 3; p++) {
118
39.7M
      float* out = tmp.Row(p);
119
1.22G
      for (size_t x = 0; x < xsize; x++) {
120
1.18G
        out[x] = bg[p][x + x0] + fg[p][x + x0];
121
1.18G
      }
122
39.7M
    }
123
13.2M
  };
124
125
13.4M
  const auto blend_weighted = [&](const float* const* bottom,
126
13.4M
                                  const float* const* top) {
127
1.25k
    bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
128
1.25k
    PerformAlphaBlending(
129
1.25k
        {bottom[0] + x0, bottom[1] + x0, bottom[2] + x0,
130
1.25k
         bottom[3 + alpha] + x0},
131
1.25k
        {top[0] + x0, top[1] + x0, top[2] + x0, top[3 + alpha] + x0},
132
1.25k
        {tmp.Row(0), tmp.Row(1), tmp.Row(2), tmp.Row(3 + alpha)}, xsize,
133
1.25k
        is_premultiplied, color_blending.clamp);
134
1.25k
  };
135
136
13.4M
  const auto add_weighted = [&](const float* const* bottom,
137
13.4M
                                const float* const* top) {
138
2.83k
    for (size_t c = 0; c < 3; c++) {
139
2.12k
      PerformAlphaWeightedAdd(bottom[c] + x0, top[c] + x0, top[3 + alpha] + x0,
140
2.12k
                              tmp.Row(c), xsize, color_blending.clamp);
141
2.12k
    }
142
708
  };
143
144
13.4M
  const auto copy = [&](const float* const* src) {
145
732k
    for (size_t p = 0; p < 3; p++) {
146
549k
      memcpy(tmp.Row(p), src[p] + x0, xsize * sizeof(**src));
147
549k
    }
148
183k
  };
149
150
13.4M
  switch (color_blending.mode) {
151
13.2M
    case PatchBlendMode::kAdd:
152
13.2M
      add();
153
13.2M
      break;
154
155
6.80k
    case PatchBlendMode::kAlphaWeightedAddAbove:
156
6.80k
      has_alpha ? add_weighted(bg, fg) : add();
157
6.80k
      break;
158
159
0
    case PatchBlendMode::kAlphaWeightedAddBelow:
160
0
      has_alpha ? add_weighted(fg, bg) : add();
161
0
      break;
162
163
174k
    case PatchBlendMode::kBlendAbove:
164
174k
      has_alpha ? blend_weighted(bg, fg) : copy(fg);
165
174k
      break;
166
167
0
    case PatchBlendMode::kBlendBelow:
168
0
      has_alpha ? blend_weighted(fg, bg) : copy(fg);
169
0
      break;
170
171
5.52k
    case PatchBlendMode::kMul:
172
22.0k
      for (int p = 0; p < 3; p++) {
173
16.5k
        PerformMulBlending(bg[p] + x0, fg[p] + x0, tmp.Row(p), xsize,
174
16.5k
                           color_blending.clamp);
175
16.5k
      }
176
5.52k
      break;
177
178
9.31k
    case PatchBlendMode::kReplace:
179
9.31k
      copy(fg);
180
9.31k
      break;
181
182
563
    case PatchBlendMode::kNone:
183
563
      copy(bg);
184
13.4M
  }
185
186
53.8M
  for (size_t i = 0; i < 3 + num_ec; i++) {
187
40.3M
    if (xsize != 0) memcpy(out[i] + x0, tmp.Row(i), xsize * sizeof(**out));
188
40.3M
  }
189
13.4M
  return true;
190
13.4M
}
191
192
}  // namespace jxl