Coverage Report

Created: 2025-06-16 07:00

/src/libjxl/lib/jxl/blending.cc
Line
Count
Source (jump to first uncovered line)
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
21.7k
bool NeedsBlending(const FrameHeader& frame_header) {
24
21.7k
  if (!(frame_header.frame_type == FrameType::kRegularFrame ||
25
21.7k
        frame_header.frame_type == FrameType::kSkipProgressive)) {
26
10.4k
    return false;
27
10.4k
  }
28
11.3k
  const auto& info = frame_header.blending_info;
29
11.3k
  bool replace_all = (info.mode == BlendMode::kReplace);
30
11.3k
  for (const auto& ec_i : frame_header.extra_channel_blending_info) {
31
5.95k
    if (ec_i.mode != BlendMode::kReplace) {
32
1.37k
      replace_all = false;
33
1.37k
    }
34
5.95k
  }
35
  // Replace the full frame: nothing to do.
36
11.3k
  if (!frame_header.custom_size_or_origin && replace_all) {
37
5.75k
    return false;
38
5.75k
  }
39
5.56k
  return true;
40
11.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
4.66M
    const std::vector<ExtraChannelInfo>& extra_channel_info) {
47
4.66M
  bool has_alpha = false;
48
4.66M
  size_t num_ec = extra_channel_info.size();
49
4.66M
  for (size_t i = 0; i < num_ec; i++) {
50
124k
    if (extra_channel_info[i].type == jxl::ExtraChannel::kAlpha) {
51
124k
      has_alpha = true;
52
124k
      break;
53
124k
    }
54
124k
  }
55
4.66M
  JXL_ASSIGN_OR_RETURN(ImageF tmp,
56
4.66M
                       ImageF::Create(memory_manager, xsize, 3 + num_ec));
57
  // Blend extra channels first so that we use the pre-blending alpha.
58
4.88M
  for (size_t i = 0; i < num_ec; i++) {
59
220k
    switch (ec_blending[i].mode) {
60
19.1k
      case PatchBlendMode::kAdd:
61
2.37M
        for (size_t x = 0; x < xsize; x++) {
62
2.35M
          tmp.Row(3 + i)[x] = bg[3 + i][x + x0] + fg[3 + i][x + x0];
63
2.35M
        }
64
19.1k
        continue;
65
66
17.9k
      case PatchBlendMode::kBlendAbove: {
67
17.9k
        size_t alpha = ec_blending[i].alpha_channel;
68
17.9k
        bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
69
17.9k
        PerformAlphaBlending(bg[3 + i] + x0, bg[3 + alpha] + x0, fg[3 + i] + x0,
70
17.9k
                             fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
71
17.9k
                             is_premultiplied, ec_blending[i].clamp);
72
17.9k
        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.9k
      case PatchBlendMode::kAlphaWeightedAddAbove: {
85
10.9k
        size_t alpha = ec_blending[i].alpha_channel;
86
10.9k
        PerformAlphaWeightedAdd(bg[3 + i] + x0, fg[3 + i] + x0,
87
10.9k
                                fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
88
10.9k
                                ec_blending[i].clamp);
89
10.9k
        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
5.27k
      case PatchBlendMode::kMul:
101
5.27k
        PerformMulBlending(bg[3 + i] + x0, fg[3 + i] + x0, tmp.Row(3 + i),
102
5.27k
                           xsize, ec_blending[i].clamp);
103
5.27k
        continue;
104
105
166k
      case PatchBlendMode::kReplace:
106
166k
        if (xsize) memcpy(tmp.Row(3 + i), fg[3 + i] + x0, xsize * sizeof(**fg));
107
166k
        continue;
108
109
139
      case PatchBlendMode::kNone:
110
139
        if (xsize) memcpy(tmp.Row(3 + i), bg[3 + i] + x0, xsize * sizeof(**fg));
111
139
        continue;
112
220k
    }
113
220k
  }
114
4.66M
  size_t alpha = color_blending.alpha_channel;
115
116
4.66M
  const auto add = [&]() {
117
16.9M
    for (int p = 0; p < 3; p++) {
118
12.6M
      float* out = tmp.Row(p);
119
207M
      for (size_t x = 0; x < xsize; x++) {
120
195M
        out[x] = bg[p][x + x0] + fg[p][x + x0];
121
195M
      }
122
12.6M
    }
123
4.22M
  };
124
125
4.66M
  const auto blend_weighted = [&](const float* const* bottom,
126
4.66M
                                  const float* const* top) {
127
6.61k
    bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
128
6.61k
    PerformAlphaBlending(
129
6.61k
        {bottom[0] + x0, bottom[1] + x0, bottom[2] + x0,
130
6.61k
         bottom[3 + alpha] + x0},
131
6.61k
        {top[0] + x0, top[1] + x0, top[2] + x0, top[3 + alpha] + x0},
132
6.61k
        {tmp.Row(0), tmp.Row(1), tmp.Row(2), tmp.Row(3 + alpha)}, xsize,
133
6.61k
        is_premultiplied, color_blending.clamp);
134
6.61k
  };
135
136
4.66M
  const auto add_weighted = [&](const float* const* bottom,
137
4.66M
                                const float* const* top) {
138
59.2k
    for (size_t c = 0; c < 3; c++) {
139
44.4k
      PerformAlphaWeightedAdd(bottom[c] + x0, top[c] + x0, top[3 + alpha] + x0,
140
44.4k
                              tmp.Row(c), xsize, color_blending.clamp);
141
44.4k
    }
142
14.8k
  };
143
144
4.66M
  const auto copy = [&](const float* const* src) {
145
1.39M
    for (size_t p = 0; p < 3; p++) {
146
1.04M
      memcpy(tmp.Row(p), src[p] + x0, xsize * sizeof(**src));
147
1.04M
    }
148
348k
  };
149
150
4.66M
  switch (color_blending.mode) {
151
4.20M
    case PatchBlendMode::kAdd:
152
4.20M
      add();
153
4.20M
      break;
154
155
32.7k
    case PatchBlendMode::kAlphaWeightedAddAbove:
156
32.7k
      has_alpha ? add_weighted(bg, fg) : add();
157
32.7k
      break;
158
159
36
    case PatchBlendMode::kAlphaWeightedAddBelow:
160
36
      has_alpha ? add_weighted(fg, bg) : add();
161
36
      break;
162
163
329k
    case PatchBlendMode::kBlendAbove:
164
329k
      has_alpha ? blend_weighted(bg, fg) : copy(fg);
165
329k
      break;
166
167
52
    case PatchBlendMode::kBlendBelow:
168
52
      has_alpha ? blend_weighted(fg, bg) : copy(fg);
169
52
      break;
170
171
70.7k
    case PatchBlendMode::kMul:
172
282k
      for (int p = 0; p < 3; p++) {
173
212k
        PerformMulBlending(bg[p] + x0, fg[p] + x0, tmp.Row(p), xsize,
174
212k
                           color_blending.clamp);
175
212k
      }
176
70.7k
      break;
177
178
22.5k
    case PatchBlendMode::kReplace:
179
22.5k
      copy(fg);
180
22.5k
      break;
181
182
3.00k
    case PatchBlendMode::kNone:
183
3.00k
      copy(bg);
184
4.66M
  }
185
186
18.8M
  for (size_t i = 0; i < 3 + num_ec; i++) {
187
14.2M
    if (xsize != 0) memcpy(out[i] + x0, tmp.Row(i), xsize * sizeof(**out));
188
14.2M
  }
189
4.66M
  return true;
190
4.66M
}
191
192
}  // namespace jxl