Coverage Report

Created: 2026-02-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/convolve_symmetric5.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 <algorithm>
7
#include <cstddef>
8
#include <cstdint>
9
#include <cstdio>
10
11
#include "lib/jxl/base/compiler_specific.h"
12
#include "lib/jxl/base/data_parallel.h"
13
#include "lib/jxl/base/status.h"
14
#include "lib/jxl/convolve.h"
15
#include "lib/jxl/image.h"
16
17
#undef HWY_TARGET_INCLUDE
18
#define HWY_TARGET_INCLUDE "lib/jxl/convolve_symmetric5.cc"
19
#include <hwy/foreach_target.h>
20
#include <hwy/highway.h>
21
22
#include "lib/jxl/base/common.h"
23
#include "lib/jxl/base/rect.h"
24
#include "lib/jxl/image_ops.h"
25
26
HWY_BEFORE_NAMESPACE();
27
namespace jxl {
28
namespace HWY_NAMESPACE {
29
30
// These templates are not found via ADL.
31
using hwy::HWY_NAMESPACE::Add;
32
using hwy::HWY_NAMESPACE::Mul;
33
using hwy::HWY_NAMESPACE::Vec;
34
35
// Weighted sum of 1x5 pixels around ix, iy with [wx2 wx1 wx0 wx1 wx2].
36
template <class WrapY>
37
static float WeightedSumBorder(const ImageF& in, const WrapY wrap_y,
38
                               const int64_t ix, const int64_t iy,
39
                               const size_t xsize, const size_t ysize,
40
                               const float wx0, const float wx1,
41
0
                               const float wx2) {
42
0
  const WrapMirror wrap_x;
43
0
  const float* JXL_RESTRICT row = in.ConstRow(wrap_y(iy, ysize));
44
0
  const float in_m2 = row[wrap_x(ix - 2, xsize)];
45
0
  const float in_p2 = row[wrap_x(ix + 2, xsize)];
46
0
  const float in_m1 = row[wrap_x(ix - 1, xsize)];
47
0
  const float in_p1 = row[wrap_x(ix + 1, xsize)];
48
0
  const float in_00 = row[ix];
49
0
  const float sum_2 = wx2 * (in_m2 + in_p2);
50
0
  const float sum_1 = wx1 * (in_m1 + in_p1);
51
0
  const float sum_0 = wx0 * in_00;
52
0
  return sum_2 + (sum_1 + sum_0);
53
0
}
Unexecuted instantiation: convolve_symmetric5.cc:float jxl::N_SCALAR::WeightedSumBorder<jxl::WrapMirror>(jxl::Plane<float> const&, jxl::WrapMirror, long, long, unsigned long, unsigned long, float, float, float)
Unexecuted instantiation: convolve_symmetric5.cc:float jxl::N_SCALAR::WeightedSumBorder<jxl::WrapUnchanged>(jxl::Plane<float> const&, jxl::WrapUnchanged, long, long, unsigned long, unsigned long, float, float, float)
54
55
template <class WrapY, class V>
56
static V WeightedSum(const ImageF& in, const WrapY wrap_y, const size_t ix,
57
                     const int64_t iy, const size_t ysize, const V wx0,
58
0
                     const V wx1, const V wx2) {
59
0
  const HWY_FULL(float) d;
60
0
  const float* JXL_RESTRICT center = in.ConstRow(wrap_y(iy, ysize)) + ix;
61
0
  const auto in_m2 = LoadU(d, center - 2);
62
0
  const auto in_p2 = LoadU(d, center + 2);
63
0
  const auto in_m1 = LoadU(d, center - 1);
64
0
  const auto in_p1 = LoadU(d, center + 1);
65
0
  const auto in_00 = LoadU(d, center);
66
0
  const auto sum_2 = Mul(wx2, Add(in_m2, in_p2));
67
0
  const auto sum_1 = Mul(wx1, Add(in_m1, in_p1));
68
0
  const auto sum_0 = Mul(wx0, in_00);
69
0
  return Add(sum_2, Add(sum_1, sum_0));
70
0
}
Unexecuted instantiation: convolve_symmetric5.cc:hwy::N_SCALAR::Vec1<float> jxl::N_SCALAR::WeightedSum<jxl::WrapMirror, hwy::N_SCALAR::Vec1<float> >(jxl::Plane<float> const&, jxl::WrapMirror, unsigned long, long, unsigned long, hwy::N_SCALAR::Vec1<float>, hwy::N_SCALAR::Vec1<float>, hwy::N_SCALAR::Vec1<float>)
Unexecuted instantiation: convolve_symmetric5.cc:hwy::N_SCALAR::Vec1<float> jxl::N_SCALAR::WeightedSum<jxl::WrapUnchanged, hwy::N_SCALAR::Vec1<float> >(jxl::Plane<float> const&, jxl::WrapUnchanged, unsigned long, long, unsigned long, hwy::N_SCALAR::Vec1<float>, hwy::N_SCALAR::Vec1<float>, hwy::N_SCALAR::Vec1<float>)
71
72
// Produces result for one pixel
73
template <class WrapY>
74
float Symmetric5Border(const ImageF& in, const int64_t ix, const int64_t iy,
75
0
                       const WeightsSymmetric5& weights) {
76
0
  const float w0 = weights.c[0];
77
0
  const float w1 = weights.r[0];
78
0
  const float w2 = weights.R[0];
79
0
  const float w4 = weights.d[0];
80
0
  const float w5 = weights.L[0];
81
0
  const float w8 = weights.D[0];
82
83
0
  const size_t xsize = in.xsize();
84
0
  const size_t ysize = in.ysize();
85
0
  const WrapY wrap_y;
86
  // Unrolled loop over all 5 rows of the kernel.
87
0
  float sum0 = WeightedSumBorder(in, wrap_y, ix, iy, xsize, ysize, w0, w1, w2);
88
89
0
  sum0 += WeightedSumBorder(in, wrap_y, ix, iy - 2, xsize, ysize, w2, w5, w8);
90
0
  float sum1 =
91
0
      WeightedSumBorder(in, wrap_y, ix, iy + 2, xsize, ysize, w2, w5, w8);
92
93
0
  sum0 += WeightedSumBorder(in, wrap_y, ix, iy - 1, xsize, ysize, w1, w4, w5);
94
0
  sum1 += WeightedSumBorder(in, wrap_y, ix, iy + 1, xsize, ysize, w1, w4, w5);
95
96
0
  return sum0 + sum1;
97
0
}
Unexecuted instantiation: float jxl::N_SCALAR::Symmetric5Border<jxl::WrapMirror>(jxl::Plane<float> const&, long, long, jxl::WeightsSymmetric5 const&)
Unexecuted instantiation: float jxl::N_SCALAR::Symmetric5Border<jxl::WrapUnchanged>(jxl::Plane<float> const&, long, long, jxl::WeightsSymmetric5 const&)
98
99
// Produces result for one vector's worth of pixels
100
template <class WrapY>
101
static void Symmetric5Interior(const ImageF& in, const int64_t ix,
102
                               const int64_t rix, const int64_t iy,
103
                               const WeightsSymmetric5& weights,
104
0
                               float* JXL_RESTRICT row_out) {
105
0
  const HWY_FULL(float) d;
106
107
0
  const auto w0 = LoadDup128(d, weights.c);
108
0
  const auto w1 = LoadDup128(d, weights.r);
109
0
  const auto w2 = LoadDup128(d, weights.R);
110
0
  const auto w4 = LoadDup128(d, weights.d);
111
0
  const auto w5 = LoadDup128(d, weights.L);
112
0
  const auto w8 = LoadDup128(d, weights.D);
113
114
0
  const size_t ysize = in.ysize();
115
0
  const WrapY wrap_y;
116
  // Unrolled loop over all 5 rows of the kernel.
117
0
  auto sum0 = WeightedSum(in, wrap_y, ix, iy, ysize, w0, w1, w2);
118
119
0
  sum0 = Add(sum0, WeightedSum(in, wrap_y, ix, iy - 2, ysize, w2, w5, w8));
120
0
  auto sum1 = WeightedSum(in, wrap_y, ix, iy + 2, ysize, w2, w5, w8);
121
122
0
  sum0 = Add(sum0, WeightedSum(in, wrap_y, ix, iy - 1, ysize, w1, w4, w5));
123
0
  sum1 = Add(sum1, WeightedSum(in, wrap_y, ix, iy + 1, ysize, w1, w4, w5));
124
125
0
  StoreU(Add(sum0, sum1), d, row_out + rix);
126
0
}
Unexecuted instantiation: convolve_symmetric5.cc:void jxl::N_SCALAR::Symmetric5Interior<jxl::WrapMirror>(jxl::Plane<float> const&, long, long, long, jxl::WeightsSymmetric5 const&, float*)
Unexecuted instantiation: convolve_symmetric5.cc:void jxl::N_SCALAR::Symmetric5Interior<jxl::WrapUnchanged>(jxl::Plane<float> const&, long, long, long, jxl::WeightsSymmetric5 const&, float*)
127
128
template <class WrapY>
129
static void Symmetric5Row(const ImageF& in, const Rect& rect, const int64_t iy,
130
                          const WeightsSymmetric5& weights,
131
0
                          float* JXL_RESTRICT row_out) {
132
0
  const int64_t kRadius = 2;
133
0
  const size_t xend = rect.x1();
134
135
0
  size_t rix = 0;
136
0
  size_t ix = rect.x0();
137
0
  const HWY_FULL(float) d;
138
0
  const size_t N = Lanes(d);
139
0
  const size_t aligned_x = RoundUpTo(kRadius, N);
140
0
  for (; ix < std::min(aligned_x, xend); ++ix, ++rix) {
141
0
    row_out[rix] = Symmetric5Border<WrapY>(in, ix, iy, weights);
142
0
  }
143
0
  for (; ix + N + kRadius <= xend; ix += N, rix += N) {
144
0
    Symmetric5Interior<WrapY>(in, ix, rix, iy, weights, row_out);
145
0
  }
146
0
  for (; ix < xend; ++ix, ++rix) {
147
0
    row_out[rix] = Symmetric5Border<WrapY>(in, ix, iy, weights);
148
0
  }
149
0
}
Unexecuted instantiation: convolve_symmetric5.cc:void jxl::N_SCALAR::Symmetric5Row<jxl::WrapMirror>(jxl::Plane<float> const&, jxl::RectT<unsigned long> const&, long, jxl::WeightsSymmetric5 const&, float*)
Unexecuted instantiation: convolve_symmetric5.cc:void jxl::N_SCALAR::Symmetric5Row<jxl::WrapUnchanged>(jxl::Plane<float> const&, jxl::RectT<unsigned long> const&, long, jxl::WeightsSymmetric5 const&, float*)
150
151
// Semi-vectorized (interior pixels Fonly); called directly like slow::, unlike
152
// the fully vectorized strategies below.
153
Status Symmetric5(const ImageF& in, const Rect& in_rect,
154
                  const WeightsSymmetric5& weights, ThreadPool* pool,
155
0
                  ImageF* JXL_RESTRICT out, const Rect& out_rect) {
156
0
  JXL_ENSURE(in_rect.xsize() == out_rect.xsize());
157
0
  JXL_ENSURE(in_rect.ysize() == out_rect.ysize());
158
0
  const size_t ysize = in_rect.ysize();
159
0
  const auto process_row = [&](const uint32_t task,
160
0
                               size_t /*thread*/) -> Status {
161
0
    const int64_t riy = task;
162
0
    const int64_t iy = in_rect.y0() + riy;
163
164
0
    if (iy < 2 || iy >= static_cast<ptrdiff_t>(in.ysize()) - 2) {
165
0
      Symmetric5Row<WrapMirror>(in, in_rect, iy, weights,
166
0
                                out_rect.Row(out, riy));
167
0
    } else {
168
0
      Symmetric5Row<WrapUnchanged>(in, in_rect, iy, weights,
169
0
                                   out_rect.Row(out, riy));
170
0
    }
171
0
    return true;
172
0
  };
173
0
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, static_cast<uint32_t>(ysize),
174
0
                                ThreadPool::NoInit, process_row,
175
0
                                "Symmetric5x5Convolution"));
176
0
  return true;
177
0
}
178
179
// NOLINTNEXTLINE(google-readability-namespace-comments)
180
}  // namespace HWY_NAMESPACE
181
}  // namespace jxl
182
HWY_AFTER_NAMESPACE();
183
184
#if HWY_ONCE
185
namespace jxl {
186
187
HWY_EXPORT(Symmetric5);
188
Status Symmetric5(const ImageF& in, const Rect& in_rect,
189
                  const WeightsSymmetric5& weights, ThreadPool* pool,
190
0
                  ImageF* JXL_RESTRICT out, const Rect& out_rect) {
191
0
  return HWY_DYNAMIC_DISPATCH(Symmetric5)(in, in_rect, weights, pool, out,
192
0
                                          out_rect);
193
0
}
194
195
}  // namespace jxl
196
#endif  // HWY_ONCE