Coverage Report

Created: 2025-07-23 08:18

/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
29.0k
bool NeedsBlending(const FrameHeader& frame_header) {
24
29.0k
  if (!(frame_header.frame_type == FrameType::kRegularFrame ||
25
29.0k
        frame_header.frame_type == FrameType::kSkipProgressive)) {
26
6.53k
    return false;
27
6.53k
  }
28
22.4k
  const auto& info = frame_header.blending_info;
29
22.4k
  bool replace_all = (info.mode == BlendMode::kReplace);
30
22.4k
  for (const auto& ec_i : frame_header.extra_channel_blending_info) {
31
2.29k
    if (ec_i.mode != BlendMode::kReplace) {
32
609
      replace_all = false;
33
609
    }
34
2.29k
  }
35
  // Replace the full frame: nothing to do.
36
22.4k
  if (!frame_header.custom_size_or_origin && replace_all) {
37
18.2k
    return false;
38
18.2k
  }
39
4.21k
  return true;
40
22.4k
}
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
10.1M
    const std::vector<ExtraChannelInfo>& extra_channel_info) {
47
10.1M
  bool has_alpha = false;
48
10.1M
  size_t num_ec = extra_channel_info.size();
49
10.1M
  for (size_t i = 0; i < num_ec; i++) {
50
16.9k
    if (extra_channel_info[i].type == jxl::ExtraChannel::kAlpha) {
51
5.13k
      has_alpha = true;
52
5.13k
      break;
53
5.13k
    }
54
16.9k
  }
55
10.1M
  JXL_ASSIGN_OR_RETURN(ImageF tmp,
56
10.1M
                       ImageF::Create(memory_manager, xsize, 3 + num_ec));
57
  // Blend extra channels first so that we use the pre-blending alpha.
58
10.2M
  for (size_t i = 0; i < num_ec; i++) {
59
69.6k
    switch (ec_blending[i].mode) {
60
9.98k
      case PatchBlendMode::kAdd:
61
1.67M
        for (size_t x = 0; x < xsize; x++) {
62
1.66M
          tmp.Row(3 + i)[x] = bg[3 + i][x + x0] + fg[3 + i][x + x0];
63
1.66M
        }
64
9.98k
        continue;
65
66
12.0k
      case PatchBlendMode::kBlendAbove: {
67
12.0k
        size_t alpha = ec_blending[i].alpha_channel;
68
12.0k
        bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
69
12.0k
        PerformAlphaBlending(bg[3 + i] + x0, bg[3 + alpha] + x0, fg[3 + i] + x0,
70
12.0k
                             fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
71
12.0k
                             is_premultiplied, ec_blending[i].clamp);
72
12.0k
        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
9.13k
      case PatchBlendMode::kAlphaWeightedAddAbove: {
85
9.13k
        size_t alpha = ec_blending[i].alpha_channel;
86
9.13k
        PerformAlphaWeightedAdd(bg[3 + i] + x0, fg[3 + i] + x0,
87
9.13k
                                fg[3 + alpha] + x0, tmp.Row(3 + i), xsize,
88
9.13k
                                ec_blending[i].clamp);
89
9.13k
        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.96k
      case PatchBlendMode::kMul:
101
2.96k
        PerformMulBlending(bg[3 + i] + x0, fg[3 + i] + x0, tmp.Row(3 + i),
102
2.96k
                           xsize, ec_blending[i].clamp);
103
2.96k
        continue;
104
105
35.5k
      case PatchBlendMode::kReplace:
106
35.5k
        if (xsize) memcpy(tmp.Row(3 + i), fg[3 + i] + x0, xsize * sizeof(**fg));
107
35.5k
        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
69.6k
    }
113
69.6k
  }
114
10.1M
  size_t alpha = color_blending.alpha_channel;
115
116
10.1M
  const auto add = [&]() {
117
39.8M
    for (int p = 0; p < 3; p++) {
118
29.9M
      float* out = tmp.Row(p);
119
750M
      for (size_t x = 0; x < xsize; x++) {
120
721M
        out[x] = bg[p][x + x0] + fg[p][x + x0];
121
721M
      }
122
29.9M
    }
123
9.96M
  };
124
125
10.1M
  const auto blend_weighted = [&](const float* const* bottom,
126
10.1M
                                  const float* const* top) {
127
1.14k
    bool is_premultiplied = extra_channel_info[alpha].alpha_associated;
128
1.14k
    PerformAlphaBlending(
129
1.14k
        {bottom[0] + x0, bottom[1] + x0, bottom[2] + x0,
130
1.14k
         bottom[3 + alpha] + x0},
131
1.14k
        {top[0] + x0, top[1] + x0, top[2] + x0, top[3 + alpha] + x0},
132
1.14k
        {tmp.Row(0), tmp.Row(1), tmp.Row(2), tmp.Row(3 + alpha)}, xsize,
133
1.14k
        is_premultiplied, color_blending.clamp);
134
1.14k
  };
135
136
10.1M
  const auto add_weighted = [&](const float* const* bottom,
137
10.1M
                                const float* const* top) {
138
3.30k
    for (size_t c = 0; c < 3; c++) {
139
2.48k
      PerformAlphaWeightedAdd(bottom[c] + x0, top[c] + x0, top[3 + alpha] + x0,
140
2.48k
                              tmp.Row(c), xsize, color_blending.clamp);
141
2.48k
    }
142
827
  };
143
144
10.1M
  const auto copy = [&](const float* const* src) {
145
646k
    for (size_t p = 0; p < 3; p++) {
146
484k
      memcpy(tmp.Row(p), src[p] + x0, xsize * sizeof(**src));
147
484k
    }
148
161k
  };
149
150
10.1M
  switch (color_blending.mode) {
151
9.96M
    case PatchBlendMode::kAdd:
152
9.96M
      add();
153
9.96M
      break;
154
155
4.35k
    case PatchBlendMode::kAlphaWeightedAddAbove:
156
4.35k
      has_alpha ? add_weighted(bg, fg) : add();
157
4.35k
      break;
158
159
0
    case PatchBlendMode::kAlphaWeightedAddBelow:
160
0
      has_alpha ? add_weighted(fg, bg) : add();
161
0
      break;
162
163
159k
    case PatchBlendMode::kBlendAbove:
164
159k
      has_alpha ? blend_weighted(bg, fg) : copy(fg);
165
159k
      break;
166
167
0
    case PatchBlendMode::kBlendBelow:
168
0
      has_alpha ? blend_weighted(fg, bg) : copy(fg);
169
0
      break;
170
171
3.74k
    case PatchBlendMode::kMul:
172
14.9k
      for (int p = 0; p < 3; p++) {
173
11.2k
        PerformMulBlending(bg[p] + x0, fg[p] + x0, tmp.Row(p), xsize,
174
11.2k
                           color_blending.clamp);
175
11.2k
      }
176
3.74k
      break;
177
178
2.79k
    case PatchBlendMode::kReplace:
179
2.79k
      copy(fg);
180
2.79k
      break;
181
182
385
    case PatchBlendMode::kNone:
183
385
      copy(bg);
184
10.1M
  }
185
186
40.6M
  for (size_t i = 0; i < 3 + num_ec; i++) {
187
30.4M
    if (xsize != 0) memcpy(out[i] + x0, tmp.Row(i), xsize * sizeof(**out));
188
30.4M
  }
189
10.1M
  return true;
190
10.1M
}
191
192
}  // namespace jxl