Coverage Report

Created: 2025-07-16 07:53

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