Coverage Report

Created: 2026-09-14 07:15

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
24.4k
bool NeedsBlending(const FrameHeader& frame_header) {
24
24.4k
  if (!(frame_header.frame_type == FrameType::kRegularFrame ||
25
11.3k
        frame_header.frame_type == FrameType::kSkipProgressive)) {
26
8.42k
    return false;
27
8.42k
  }
28
16.0k
  const auto& info = frame_header.blending_info;
29
16.0k
  bool replace_all = (info.mode == BlendMode::kReplace);
30
16.0k
  for (const auto& ec_i : frame_header.extra_channel_blending_info) {
31
3.15k
    if (ec_i.mode != BlendMode::kReplace) {
32
455
      replace_all = false;
33
455
    }
34
3.15k
  }
35
  // Replace the full frame: nothing to do.
36
16.0k
  if (!frame_header.custom_size_or_origin && replace_all) {
37
11.5k
    return false;
38
11.5k
  }
39
4.45k
  return true;
40
16.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
30.2M
    const std::vector<ExtraChannelInfo>& extra_channel_info) {
47
30.2M
  bool has_alpha = false;
48
30.2M
  size_t num_ec = extra_channel_info.size();
49
30.2M
  for (size_t i = 0; i < num_ec; i++) {
50
80.1k
    if (extra_channel_info[i].type == jxl::ExtraChannel::kAlpha) {
51
55.1k
      has_alpha = true;
52
55.1k
      break;
53
55.1k
    }
54
80.1k
  }
55
30.2M
  JXL_ASSIGN_OR_RETURN(ImageF tmp,
56
30.2M
                       ImageF::Create(memory_manager, xsize, 3 + num_ec));
57
  // Blend extra channels first so that we use the pre-blending alpha.
58
30.3M
  for (size_t i = 0; i < num_ec; i++) {
59
81.6k
    switch (ec_blending[i].mode) {
60
1.65k
      case PatchBlendMode::kAdd:
61
16.7k
        for (size_t x = 0; x < xsize; x++) {
62
15.0k
          tmp.Row(3 + i)[x] = bg[3 + i][x + x0] + fg[3 + i][x + x0];
63
15.0k
        }
64
1.65k
        continue;
65
66
3.13k
      case PatchBlendMode::kBlendAbove: {
67
3.13k
        size_t alpha = ec_blending[i].alpha_channel;
68
3.13k
        bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
69
3.13k
        PerformAlphaBlending(bg[3 + i] + x0, bg[3 + alpha] + x0, fg[3 + i] + x0,
70
3.13k
                             fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
71
3.13k
                             is_premultiplied, ec_blending[i].clamp);
72
3.13k
        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
24
      case PatchBlendMode::kAlphaWeightedAddAbove: {
85
24
        size_t alpha = ec_blending[i].alpha_channel;
86
24
        PerformAlphaWeightedAdd(bg[3 + i] + x0, fg[3 + i] + x0,
87
24
                                fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
88
24
                                ec_blending[i].clamp);
89
24
        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
14
      case PatchBlendMode::kMul:
101
14
        PerformMulBlending(bg[3 + i] + x0, fg[3 + i] + x0, tmp.Row(3 + i),
102
14
                           xsize, ec_blending[i].clamp);
103
14
        continue;
104
105
34.4k
      case PatchBlendMode::kReplace:
106
34.4k
        if (xsize) memcpy(tmp.Row(3 + i), fg[3 + i] + x0, xsize * sizeof(**fg));
107
34.4k
        continue;
108
109
42.3k
      case PatchBlendMode::kNone:
110
42.3k
        if (xsize) memcpy(tmp.Row(3 + i), bg[3 + i] + x0, xsize * sizeof(**fg));
111
42.3k
        continue;
112
81.6k
    }
113
81.6k
  }
114
30.2M
  size_t alpha = color_blending.alpha_channel;
115
116
30.2M
  const auto add = [&]() {
117
120M
    for (int p = 0; p < 3; p++) {
118
90.4M
      float* out = tmp.Row(p);
119
4.24G
      for (size_t x = 0; x < xsize; x++) {
120
4.15G
        out[x] = bg[p][x + x0] + fg[p][x + x0];
121
4.15G
      }
122
90.4M
    }
123
30.1M
  };
124
125
30.2M
  const auto blend_weighted = [&](const float* const* bottom,
126
30.2M
                                  const float* const* top) {
127
771
    bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
128
771
    PerformAlphaBlending(
129
771
        {bottom[0] + x0, bottom[1] + x0, bottom[2] + x0,
130
771
         bottom[3 + alpha] + x0},
131
771
        {top[0] + x0, top[1] + x0, top[2] + x0, top[3 + alpha] + x0},
132
771
        {tmp.Row(0), tmp.Row(1), tmp.Row(2), tmp.Row(3 + alpha)}, xsize,
133
771
        is_premultiplied, color_blending.clamp);
134
771
  };
135
136
30.2M
  const auto add_weighted = [&](const float* const* bottom,
137
30.2M
                                const float* const* top) {
138
2.23k
    for (size_t c = 0; c < 3; c++) {
139
1.67k
      PerformAlphaWeightedAdd(bottom[c] + x0, top[c] + x0, top[3 + alpha] + x0,
140
1.67k
                              tmp.Row(c), xsize, color_blending.clamp);
141
1.67k
    }
142
558
  };
143
144
30.2M
  const auto copy = [&](const float* const* src) {
145
361k
    for (size_t p = 0; p < 3; p++) {
146
271k
      memcpy(tmp.Row(p), src[p] + x0, xsize * sizeof(**src));
147
271k
    }
148
90.3k
  };
149
150
30.2M
  switch (color_blending.mode) {
151
30.0M
    case PatchBlendMode::kAdd:
152
30.0M
      add();
153
30.0M
      break;
154
155
59.1k
    case PatchBlendMode::kAlphaWeightedAddAbove:
156
59.1k
      has_alpha ? add_weighted(bg, fg) : add();
157
59.1k
      break;
158
159
75
    case PatchBlendMode::kAlphaWeightedAddBelow:
160
75
      has_alpha ? add_weighted(fg, bg) : add();
161
75
      break;
162
163
56.6k
    case PatchBlendMode::kBlendAbove:
164
56.6k
      has_alpha ? blend_weighted(bg, fg) : copy(fg);
165
56.6k
      break;
166
167
0
    case PatchBlendMode::kBlendBelow:
168
0
      has_alpha ? blend_weighted(fg, bg) : copy(fg);
169
0
      break;
170
171
1.49k
    case PatchBlendMode::kMul:
172
5.98k
      for (int p = 0; p < 3; p++) {
173
4.48k
        PerformMulBlending(bg[p] + x0, fg[p] + x0, tmp.Row(p), xsize,
174
4.48k
                           color_blending.clamp);
175
4.48k
      }
176
1.49k
      break;
177
178
6.54k
    case PatchBlendMode::kReplace:
179
6.54k
      copy(fg);
180
6.54k
      break;
181
182
27.9k
    case PatchBlendMode::kNone:
183
27.9k
      copy(bg);
184
30.2M
  }
185
186
121M
  for (size_t i = 0; i < 3 + num_ec; i++) {
187
90.7M
    if (xsize != 0) memcpy(out[i] + x0, tmp.Row(i), xsize * sizeof(**out));
188
90.7M
  }
189
30.2M
  return true;
190
30.2M
}
191
192
}  // namespace jxl