Coverage Report

Created: 2025-12-13 07:57

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
27.5k
bool NeedsBlending(const FrameHeader& frame_header) {
24
27.5k
  if (!(frame_header.frame_type == FrameType::kRegularFrame ||
25
13.6k
        frame_header.frame_type == FrameType::kSkipProgressive)) {
26
11.6k
    return false;
27
11.6k
  }
28
15.9k
  const auto& info = frame_header.blending_info;
29
15.9k
  bool replace_all = (info.mode == BlendMode::kReplace);
30
15.9k
  for (const auto& ec_i : frame_header.extra_channel_blending_info) {
31
8.86k
    if (ec_i.mode != BlendMode::kReplace) {
32
2.92k
      replace_all = false;
33
2.92k
    }
34
8.86k
  }
35
  // Replace the full frame: nothing to do.
36
15.9k
  if (!frame_header.custom_size_or_origin && replace_all) {
37
8.99k
    return false;
38
8.99k
  }
39
6.94k
  return true;
40
15.9k
}
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
29.8M
    const std::vector<ExtraChannelInfo>& extra_channel_info) {
47
29.8M
  bool has_alpha = false;
48
29.8M
  size_t num_ec = extra_channel_info.size();
49
29.8M
  for (size_t i = 0; i < num_ec; i++) {
50
112k
    if (extra_channel_info[i].type == jxl::ExtraChannel::kAlpha) {
51
84.0k
      has_alpha = true;
52
84.0k
      break;
53
84.0k
    }
54
112k
  }
55
29.8M
  JXL_ASSIGN_OR_RETURN(ImageF tmp,
56
29.8M
                       ImageF::Create(memory_manager, xsize, 3 + num_ec));
57
  // Blend extra channels first so that we use the pre-blending alpha.
58
30.1M
  for (size_t i = 0; i < num_ec; i++) {
59
286k
    switch (ec_blending[i].mode) {
60
13.9k
      case PatchBlendMode::kAdd:
61
891k
        for (size_t x = 0; x < xsize; x++) {
62
877k
          tmp.Row(3 + i)[x] = bg[3 + i][x + x0] + fg[3 + i][x + x0];
63
877k
        }
64
13.9k
        continue;
65
66
57.8k
      case PatchBlendMode::kBlendAbove: {
67
57.8k
        size_t alpha = ec_blending[i].alpha_channel;
68
57.8k
        bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
69
57.8k
        PerformAlphaBlending(bg[3 + i] + x0, bg[3 + alpha] + x0, fg[3 + i] + x0,
70
57.8k
                             fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
71
57.8k
                             is_premultiplied, ec_blending[i].clamp);
72
57.8k
        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
16.6k
      case PatchBlendMode::kAlphaWeightedAddAbove: {
85
16.6k
        size_t alpha = ec_blending[i].alpha_channel;
86
16.6k
        PerformAlphaWeightedAdd(bg[3 + i] + x0, fg[3 + i] + x0,
87
16.6k
                                fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
88
16.6k
                                ec_blending[i].clamp);
89
16.6k
        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.0k
      case PatchBlendMode::kMul:
101
54.0k
        PerformMulBlending(bg[3 + i] + x0, fg[3 + i] + x0, tmp.Row(3 + i),
102
54.0k
                           xsize, ec_blending[i].clamp);
103
54.0k
        continue;
104
105
144k
      case PatchBlendMode::kReplace:
106
144k
        if (xsize) memcpy(tmp.Row(3 + i), fg[3 + i] + x0, xsize * sizeof(**fg));
107
144k
        continue;
108
109
150
      case PatchBlendMode::kNone:
110
150
        if (xsize) memcpy(tmp.Row(3 + i), bg[3 + i] + x0, xsize * sizeof(**fg));
111
150
        continue;
112
286k
    }
113
286k
  }
114
29.8M
  size_t alpha = color_blending.alpha_channel;
115
116
29.8M
  const auto add = [&]() {
117
117M
    for (int p = 0; p < 3; p++) {
118
88.3M
      float* out = tmp.Row(p);
119
3.10G
      for (size_t x = 0; x < xsize; x++) {
120
3.01G
        out[x] = bg[p][x + x0] + fg[p][x + x0];
121
3.01G
      }
122
88.3M
    }
123
29.4M
  };
124
125
29.8M
  const auto blend_weighted = [&](const float* const* bottom,
126
29.8M
                                  const float* const* top) {
127
19.9k
    bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
128
19.9k
    PerformAlphaBlending(
129
19.9k
        {bottom[0] + x0, bottom[1] + x0, bottom[2] + x0,
130
19.9k
         bottom[3 + alpha] + x0},
131
19.9k
        {top[0] + x0, top[1] + x0, top[2] + x0, top[3 + alpha] + x0},
132
19.9k
        {tmp.Row(0), tmp.Row(1), tmp.Row(2), tmp.Row(3 + alpha)}, xsize,
133
19.9k
        is_premultiplied, color_blending.clamp);
134
19.9k
  };
135
136
29.8M
  const auto add_weighted = [&](const float* const* bottom,
137
29.8M
                                const float* const* top) {
138
78.5k
    for (size_t c = 0; c < 3; c++) {
139
58.9k
      PerformAlphaWeightedAdd(bottom[c] + x0, top[c] + x0, top[3 + alpha] + x0,
140
58.9k
                              tmp.Row(c), xsize, color_blending.clamp);
141
58.9k
    }
142
19.6k
  };
143
144
29.8M
  const auto copy = [&](const float* const* src) {
145
1.26M
    for (size_t p = 0; p < 3; p++) {
146
951k
      memcpy(tmp.Row(p), src[p] + x0, xsize * sizeof(**src));
147
951k
    }
148
317k
  };
149
150
29.8M
  switch (color_blending.mode) {
151
29.3M
    case PatchBlendMode::kAdd:
152
29.3M
      add();
153
29.3M
      break;
154
155
109k
    case PatchBlendMode::kAlphaWeightedAddAbove:
156
109k
      has_alpha ? add_weighted(bg, fg) : add();
157
109k
      break;
158
159
1.63k
    case PatchBlendMode::kAlphaWeightedAddBelow:
160
1.63k
      has_alpha ? add_weighted(fg, bg) : add();
161
1.63k
      break;
162
163
255k
    case PatchBlendMode::kBlendAbove:
164
255k
      has_alpha ? blend_weighted(bg, fg) : copy(fg);
165
255k
      break;
166
167
1.37k
    case PatchBlendMode::kBlendBelow:
168
1.37k
      has_alpha ? blend_weighted(fg, bg) : copy(fg);
169
1.37k
      break;
170
171
5.40k
    case PatchBlendMode::kMul:
172
21.6k
      for (int p = 0; p < 3; p++) {
173
16.2k
        PerformMulBlending(bg[p] + x0, fg[p] + x0, tmp.Row(p), xsize,
174
16.2k
                           color_blending.clamp);
175
16.2k
      }
176
5.40k
      break;
177
178
32.1k
    case PatchBlendMode::kReplace:
179
32.1k
      copy(fg);
180
32.1k
      break;
181
182
47.9k
    case PatchBlendMode::kNone:
183
47.9k
      copy(bg);
184
29.8M
  }
185
186
119M
  for (size_t i = 0; i < 3 + num_ec; i++) {
187
89.7M
    if (xsize != 0) memcpy(out[i] + x0, tmp.Row(i), xsize * sizeof(**out));
188
89.7M
  }
189
29.8M
  return true;
190
29.8M
}
191
192
}  // namespace jxl