Coverage Report

Created: 2024-05-21 06:24

/src/libjxl/lib/jxl/splines.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/splines.h"
7
8
#include <jxl/memory_manager.h>
9
10
#include <algorithm>
11
#include <cinttypes>  // PRIu64
12
#include <cmath>
13
#include <limits>
14
15
#include "lib/jxl/base/common.h"
16
#include "lib/jxl/base/printf_macros.h"
17
#include "lib/jxl/base/rect.h"
18
#include "lib/jxl/base/status.h"
19
#include "lib/jxl/chroma_from_luma.h"
20
#include "lib/jxl/common.h"  // JXL_HIGH_PRECISION
21
#include "lib/jxl/dct_scales.h"
22
#include "lib/jxl/dec_ans.h"
23
#include "lib/jxl/dec_bit_reader.h"
24
#include "lib/jxl/pack_signed.h"
25
26
#undef HWY_TARGET_INCLUDE
27
#define HWY_TARGET_INCLUDE "lib/jxl/splines.cc"
28
#include <hwy/foreach_target.h>
29
#include <hwy/highway.h>
30
31
#include "lib/jxl/base/fast_math-inl.h"
32
HWY_BEFORE_NAMESPACE();
33
namespace jxl {
34
namespace HWY_NAMESPACE {
35
namespace {
36
37
// These templates are not found via ADL.
38
using hwy::HWY_NAMESPACE::Mul;
39
using hwy::HWY_NAMESPACE::MulAdd;
40
using hwy::HWY_NAMESPACE::MulSub;
41
using hwy::HWY_NAMESPACE::Sqrt;
42
using hwy::HWY_NAMESPACE::Sub;
43
44
// Given a set of DCT coefficients, this returns the result of performing cosine
45
// interpolation on the original samples.
46
0
float ContinuousIDCT(const Dct32& dct, const float t) {
47
  // We compute here the DCT-3 of the `dct` vector, rescaled by a factor of
48
  // sqrt(32). This is such that an input vector vector {x, 0, ..., 0} produces
49
  // a constant result of x. dct[0] was scaled in Dequantize() to allow uniform
50
  // treatment of all the coefficients.
51
0
  constexpr float kMultipliers[32] = {
52
0
      kPi / 32 * 0,  kPi / 32 * 1,  kPi / 32 * 2,  kPi / 32 * 3,  kPi / 32 * 4,
53
0
      kPi / 32 * 5,  kPi / 32 * 6,  kPi / 32 * 7,  kPi / 32 * 8,  kPi / 32 * 9,
54
0
      kPi / 32 * 10, kPi / 32 * 11, kPi / 32 * 12, kPi / 32 * 13, kPi / 32 * 14,
55
0
      kPi / 32 * 15, kPi / 32 * 16, kPi / 32 * 17, kPi / 32 * 18, kPi / 32 * 19,
56
0
      kPi / 32 * 20, kPi / 32 * 21, kPi / 32 * 22, kPi / 32 * 23, kPi / 32 * 24,
57
0
      kPi / 32 * 25, kPi / 32 * 26, kPi / 32 * 27, kPi / 32 * 28, kPi / 32 * 29,
58
0
      kPi / 32 * 30, kPi / 32 * 31,
59
0
  };
60
0
  HWY_CAPPED(float, 32) df;
61
0
  auto result = Zero(df);
62
0
  const auto tandhalf = Set(df, t + 0.5f);
63
0
  for (int i = 0; i < 32; i += Lanes(df)) {
64
0
    auto cos_arg = Mul(LoadU(df, kMultipliers + i), tandhalf);
65
0
    auto cos = FastCosf(df, cos_arg);
66
0
    auto local_res = Mul(LoadU(df, dct.data() + i), cos);
67
0
    result = MulAdd(Set(df, kSqrt2), local_res, result);
68
0
  }
69
0
  return GetLane(SumOfLanes(df, result));
70
0
}
Unexecuted instantiation: splines.cc:jxl::N_AVX2::(anonymous namespace)::ContinuousIDCT(std::__1::array<float, 32ul> const&, float)
Unexecuted instantiation: splines.cc:jxl::N_SSE4::(anonymous namespace)::ContinuousIDCT(std::__1::array<float, 32ul> const&, float)
Unexecuted instantiation: splines.cc:jxl::N_SSE2::(anonymous namespace)::ContinuousIDCT(std::__1::array<float, 32ul> const&, float)
71
72
template <typename DF>
73
void DrawSegment(DF df, const SplineSegment& segment, const bool add,
74
0
                 const size_t y, const size_t x, float* JXL_RESTRICT rows[3]) {
75
0
  Rebind<int32_t, DF> di;
76
0
  const auto inv_sigma = Set(df, segment.inv_sigma);
77
0
  const auto half = Set(df, 0.5f);
78
0
  const auto one_over_2s2 = Set(df, 0.353553391f);
79
0
  const auto sigma_over_4_times_intensity =
80
0
      Set(df, segment.sigma_over_4_times_intensity);
81
0
  const auto dx = Sub(ConvertTo(df, Iota(di, x)), Set(df, segment.center_x));
82
0
  const auto dy = Set(df, y - segment.center_y);
83
0
  const auto sqd = MulAdd(dx, dx, Mul(dy, dy));
84
0
  const auto distance = Sqrt(sqd);
85
0
  const auto one_dimensional_factor =
86
0
      Sub(FastErff(df, Mul(MulAdd(distance, half, one_over_2s2), inv_sigma)),
87
0
          FastErff(df, Mul(MulSub(distance, half, one_over_2s2), inv_sigma)));
88
0
  auto local_intensity =
89
0
      Mul(sigma_over_4_times_intensity,
90
0
          Mul(one_dimensional_factor, one_dimensional_factor));
91
0
  for (size_t c = 0; c < 3; ++c) {
92
0
    const auto cm = Set(df, add ? segment.color[c] : -segment.color[c]);
93
0
    const auto in = LoadU(df, rows[c] + x);
94
0
    StoreU(MulAdd(cm, local_intensity, in), df, rows[c] + x);
95
0
  }
96
0
}
Unexecuted instantiation: splines.cc:void jxl::N_AVX2::(anonymous namespace)::DrawSegment<hwy::N_AVX2::Simd<float, 8ul, 0> >(hwy::N_AVX2::Simd<float, 8ul, 0>, jxl::SplineSegment const&, bool, unsigned long, unsigned long, float* restrict*)
Unexecuted instantiation: splines.cc:void jxl::N_AVX2::(anonymous namespace)::DrawSegment<hwy::N_AVX2::Simd<float, 1ul, 0> >(hwy::N_AVX2::Simd<float, 1ul, 0>, jxl::SplineSegment const&, bool, unsigned long, unsigned long, float* restrict*)
Unexecuted instantiation: splines.cc:void jxl::N_SSE4::(anonymous namespace)::DrawSegment<hwy::N_SSE4::Simd<float, 4ul, 0> >(hwy::N_SSE4::Simd<float, 4ul, 0>, jxl::SplineSegment const&, bool, unsigned long, unsigned long, float* restrict*)
Unexecuted instantiation: splines.cc:void jxl::N_SSE4::(anonymous namespace)::DrawSegment<hwy::N_SSE4::Simd<float, 1ul, 0> >(hwy::N_SSE4::Simd<float, 1ul, 0>, jxl::SplineSegment const&, bool, unsigned long, unsigned long, float* restrict*)
Unexecuted instantiation: splines.cc:void jxl::N_SSE2::(anonymous namespace)::DrawSegment<hwy::N_SSE2::Simd<float, 4ul, 0> >(hwy::N_SSE2::Simd<float, 4ul, 0>, jxl::SplineSegment const&, bool, unsigned long, unsigned long, float* restrict*)
Unexecuted instantiation: splines.cc:void jxl::N_SSE2::(anonymous namespace)::DrawSegment<hwy::N_SSE2::Simd<float, 1ul, 0> >(hwy::N_SSE2::Simd<float, 1ul, 0>, jxl::SplineSegment const&, bool, unsigned long, unsigned long, float* restrict*)
97
98
void DrawSegment(const SplineSegment& segment, const bool add, const size_t y,
99
0
                 const ssize_t x0, ssize_t x1, float* JXL_RESTRICT rows[3]) {
100
0
  ssize_t x = std::max<ssize_t>(
101
0
      x0, std::llround(segment.center_x - segment.maximum_distance));
102
  // one-past-the-end
103
0
  x1 = std::min<ssize_t>(
104
0
      x1, std::llround(segment.center_x + segment.maximum_distance) + 1);
105
0
  HWY_FULL(float) df;
106
0
  for (; x + static_cast<ssize_t>(Lanes(df)) <= x1; x += Lanes(df)) {
107
0
    DrawSegment(df, segment, add, y, x, rows);
108
0
  }
109
0
  for (; x < x1; ++x) {
110
0
    DrawSegment(HWY_CAPPED(float, 1)(), segment, add, y, x, rows);
111
0
  }
112
0
}
Unexecuted instantiation: splines.cc:jxl::N_AVX2::(anonymous namespace)::DrawSegment(jxl::SplineSegment const&, bool, unsigned long, long, long, float* restrict*)
Unexecuted instantiation: splines.cc:jxl::N_SSE4::(anonymous namespace)::DrawSegment(jxl::SplineSegment const&, bool, unsigned long, long, long, float* restrict*)
Unexecuted instantiation: splines.cc:jxl::N_SSE2::(anonymous namespace)::DrawSegment(jxl::SplineSegment const&, bool, unsigned long, long, long, float* restrict*)
113
114
void ComputeSegments(const Spline::Point& center, const float intensity,
115
                     const float color[3], const float sigma,
116
                     std::vector<SplineSegment>& segments,
117
0
                     std::vector<std::pair<size_t, size_t>>& segments_by_y) {
118
  // Sanity check sigma, inverse sigma and intensity
119
0
  if (!(std::isfinite(sigma) && sigma != 0.0f && std::isfinite(1.0f / sigma) &&
120
0
        std::isfinite(intensity))) {
121
0
    return;
122
0
  }
123
0
#if JXL_HIGH_PRECISION
124
0
  constexpr float kDistanceExp = 5;
125
#else
126
  // About 30% faster.
127
  constexpr float kDistanceExp = 3;
128
#endif
129
  // We cap from below colors to at least 0.01.
130
0
  float max_color = 0.01f;
131
0
  for (size_t c = 0; c < 3; c++) {
132
0
    max_color = std::max(max_color, std::abs(color[c] * intensity));
133
0
  }
134
  // Distance beyond which max_color*intensity*exp(-d^2 / (2 * sigma^2)) drops
135
  // below 10^-kDistanceExp.
136
0
  const float maximum_distance =
137
0
      std::sqrt(-2 * sigma * sigma *
138
0
                (std::log(0.1) * kDistanceExp - std::log(max_color)));
139
0
  SplineSegment segment;
140
0
  segment.center_y = center.y;
141
0
  segment.center_x = center.x;
142
0
  memcpy(segment.color, color, sizeof(segment.color));
143
0
  segment.inv_sigma = 1.0f / sigma;
144
0
  segment.sigma_over_4_times_intensity = .25f * sigma * intensity;
145
0
  segment.maximum_distance = maximum_distance;
146
0
  ssize_t y0 = std::llround(center.y - maximum_distance);
147
0
  ssize_t y1 =
148
0
      std::llround(center.y + maximum_distance) + 1;  // one-past-the-end
149
0
  for (ssize_t y = std::max<ssize_t>(y0, 0); y < y1; y++) {
150
0
    segments_by_y.emplace_back(y, segments.size());
151
0
  }
152
0
  segments.push_back(segment);
153
0
}
Unexecuted instantiation: splines.cc:jxl::N_AVX2::(anonymous namespace)::ComputeSegments(jxl::Spline::Point const&, float, float const*, float, std::__1::vector<jxl::SplineSegment, std::__1::allocator<jxl::SplineSegment> >&, std::__1::vector<std::__1::pair<unsigned long, unsigned long>, std::__1::allocator<std::__1::pair<unsigned long, unsigned long> > >&)
Unexecuted instantiation: splines.cc:jxl::N_SSE4::(anonymous namespace)::ComputeSegments(jxl::Spline::Point const&, float, float const*, float, std::__1::vector<jxl::SplineSegment, std::__1::allocator<jxl::SplineSegment> >&, std::__1::vector<std::__1::pair<unsigned long, unsigned long>, std::__1::allocator<std::__1::pair<unsigned long, unsigned long> > >&)
Unexecuted instantiation: splines.cc:jxl::N_SSE2::(anonymous namespace)::ComputeSegments(jxl::Spline::Point const&, float, float const*, float, std::__1::vector<jxl::SplineSegment, std::__1::allocator<jxl::SplineSegment> >&, std::__1::vector<std::__1::pair<unsigned long, unsigned long>, std::__1::allocator<std::__1::pair<unsigned long, unsigned long> > >&)
154
155
void DrawSegments(float* JXL_RESTRICT row_x, float* JXL_RESTRICT row_y,
156
                  float* JXL_RESTRICT row_b, const Rect& image_rect,
157
                  const bool add, const SplineSegment* segments,
158
                  const size_t* segment_indices,
159
0
                  const size_t* segment_y_start) {
160
0
  JXL_ASSERT(image_rect.ysize() == 1);
161
0
  float* JXL_RESTRICT rows[3] = {row_x - image_rect.x0(),
162
0
                                 row_y - image_rect.x0(),
163
0
                                 row_b - image_rect.x0()};
164
0
  size_t y = image_rect.y0();
165
0
  for (size_t i = segment_y_start[y]; i < segment_y_start[y + 1]; i++) {
166
0
    DrawSegment(segments[segment_indices[i]], add, y, image_rect.x0(),
167
0
                image_rect.x0() + image_rect.xsize(), rows);
168
0
  }
169
0
}
Unexecuted instantiation: splines.cc:jxl::N_AVX2::(anonymous namespace)::DrawSegments(float*, float*, float*, jxl::RectT<unsigned long> const&, bool, jxl::SplineSegment const*, unsigned long const*, unsigned long const*)
Unexecuted instantiation: splines.cc:jxl::N_SSE4::(anonymous namespace)::DrawSegments(float*, float*, float*, jxl::RectT<unsigned long> const&, bool, jxl::SplineSegment const*, unsigned long const*, unsigned long const*)
Unexecuted instantiation: splines.cc:jxl::N_SSE2::(anonymous namespace)::DrawSegments(float*, float*, float*, jxl::RectT<unsigned long> const&, bool, jxl::SplineSegment const*, unsigned long const*, unsigned long const*)
170
171
void SegmentsFromPoints(
172
    const Spline& spline,
173
    const std::vector<std::pair<Spline::Point, float>>& points_to_draw,
174
    const float arc_length, std::vector<SplineSegment>& segments,
175
0
    std::vector<std::pair<size_t, size_t>>& segments_by_y) {
176
0
  const float inv_arc_length = 1.0f / arc_length;
177
0
  int k = 0;
178
0
  for (const auto& point_to_draw : points_to_draw) {
179
0
    const Spline::Point& point = point_to_draw.first;
180
0
    const float multiplier = point_to_draw.second;
181
0
    const float progress_along_arc =
182
0
        std::min(1.f, (k * kDesiredRenderingDistance) * inv_arc_length);
183
0
    ++k;
184
0
    float color[3];
185
0
    for (size_t c = 0; c < 3; ++c) {
186
0
      color[c] =
187
0
          ContinuousIDCT(spline.color_dct[c], (32 - 1) * progress_along_arc);
188
0
    }
189
0
    const float sigma =
190
0
        ContinuousIDCT(spline.sigma_dct, (32 - 1) * progress_along_arc);
191
0
    ComputeSegments(point, multiplier, color, sigma, segments, segments_by_y);
192
0
  }
193
0
}
Unexecuted instantiation: splines.cc:jxl::N_AVX2::(anonymous namespace)::SegmentsFromPoints(jxl::Spline const&, std::__1::vector<std::__1::pair<jxl::Spline::Point, float>, std::__1::allocator<std::__1::pair<jxl::Spline::Point, float> > > const&, float, std::__1::vector<jxl::SplineSegment, std::__1::allocator<jxl::SplineSegment> >&, std::__1::vector<std::__1::pair<unsigned long, unsigned long>, std::__1::allocator<std::__1::pair<unsigned long, unsigned long> > >&)
Unexecuted instantiation: splines.cc:jxl::N_SSE4::(anonymous namespace)::SegmentsFromPoints(jxl::Spline const&, std::__1::vector<std::__1::pair<jxl::Spline::Point, float>, std::__1::allocator<std::__1::pair<jxl::Spline::Point, float> > > const&, float, std::__1::vector<jxl::SplineSegment, std::__1::allocator<jxl::SplineSegment> >&, std::__1::vector<std::__1::pair<unsigned long, unsigned long>, std::__1::allocator<std::__1::pair<unsigned long, unsigned long> > >&)
Unexecuted instantiation: splines.cc:jxl::N_SSE2::(anonymous namespace)::SegmentsFromPoints(jxl::Spline const&, std::__1::vector<std::__1::pair<jxl::Spline::Point, float>, std::__1::allocator<std::__1::pair<jxl::Spline::Point, float> > > const&, float, std::__1::vector<jxl::SplineSegment, std::__1::allocator<jxl::SplineSegment> >&, std::__1::vector<std::__1::pair<unsigned long, unsigned long>, std::__1::allocator<std::__1::pair<unsigned long, unsigned long> > >&)
194
}  // namespace
195
// NOLINTNEXTLINE(google-readability-namespace-comments)
196
}  // namespace HWY_NAMESPACE
197
}  // namespace jxl
198
HWY_AFTER_NAMESPACE();
199
200
#if HWY_ONCE
201
namespace jxl {
202
HWY_EXPORT(SegmentsFromPoints);
203
HWY_EXPORT(DrawSegments);
204
205
namespace {
206
207
// It is not in spec, but reasonable limit to avoid overflows.
208
template <typename T>
209
0
Status ValidateSplinePointPos(const T& x, const T& y) {
210
0
  constexpr T kSplinePosLimit = 1u << 23;
211
0
  if ((x >= kSplinePosLimit) || (x <= -kSplinePosLimit) ||
212
0
      (y >= kSplinePosLimit) || (y <= -kSplinePosLimit)) {
213
0
    return JXL_FAILURE("Spline coordinates out of bounds");
214
0
  }
215
0
  return true;
216
0
}
Unexecuted instantiation: splines.cc:jxl::Status jxl::(anonymous namespace)::ValidateSplinePointPos<long>(long const&, long const&)
Unexecuted instantiation: splines.cc:jxl::Status jxl::(anonymous namespace)::ValidateSplinePointPos<float>(float const&, float const&)
Unexecuted instantiation: splines.cc:jxl::Status jxl::(anonymous namespace)::ValidateSplinePointPos<int>(int const&, int const&)
217
218
// Maximum number of spline control points per frame is
219
//   std::min(kMaxNumControlPoints, xsize * ysize / 2)
220
constexpr size_t kMaxNumControlPoints = 1u << 20u;
221
constexpr size_t kMaxNumControlPointsPerPixelRatio = 2;
222
223
0
float AdjustedQuant(const int32_t adjustment) {
224
0
  return (adjustment >= 0) ? (1.f + .125f * adjustment)
225
0
                           : 1.f / (1.f - .125f * adjustment);
226
0
}
227
228
0
float InvAdjustedQuant(const int32_t adjustment) {
229
0
  return (adjustment >= 0) ? 1.f / (1.f + .125f * adjustment)
230
0
                           : (1.f - .125f * adjustment);
231
0
}
232
233
// X, Y, B, sigma.
234
constexpr float kChannelWeight[] = {0.0042f, 0.075f, 0.07f, .3333f};
235
236
Status DecodeAllStartingPoints(std::vector<Spline::Point>* const points,
237
                               BitReader* const br, ANSSymbolReader* reader,
238
                               const std::vector<uint8_t>& context_map,
239
0
                               const size_t num_splines) {
240
0
  points->clear();
241
0
  points->reserve(num_splines);
242
0
  int64_t last_x = 0;
243
0
  int64_t last_y = 0;
244
0
  for (size_t i = 0; i < num_splines; i++) {
245
0
    int64_t x =
246
0
        reader->ReadHybridUint(kStartingPositionContext, br, context_map);
247
0
    int64_t y =
248
0
        reader->ReadHybridUint(kStartingPositionContext, br, context_map);
249
0
    if (i != 0) {
250
0
      x = UnpackSigned(x) + last_x;
251
0
      y = UnpackSigned(y) + last_y;
252
0
    }
253
0
    JXL_RETURN_IF_ERROR(ValidateSplinePointPos(x, y));
254
0
    points->emplace_back(static_cast<float>(x), static_cast<float>(y));
255
0
    last_x = x;
256
0
    last_y = y;
257
0
  }
258
0
  return true;
259
0
}
260
261
struct Vector {
262
  float x, y;
263
0
  Vector operator-() const { return {-x, -y}; }
264
0
  Vector operator+(const Vector& other) const {
265
0
    return {x + other.x, y + other.y};
266
0
  }
267
0
  float SquaredNorm() const { return x * x + y * y; }
268
};
269
0
Vector operator*(const float k, const Vector& vec) {
270
0
  return {k * vec.x, k * vec.y};
271
0
}
272
273
0
Spline::Point operator+(const Spline::Point& p, const Vector& vec) {
274
0
  return {p.x + vec.x, p.y + vec.y};
275
0
}
276
0
Vector operator-(const Spline::Point& a, const Spline::Point& b) {
277
0
  return {a.x - b.x, a.y - b.y};
278
0
}
279
280
// TODO(eustas): avoid making a copy of "points".
281
void DrawCentripetalCatmullRomSpline(std::vector<Spline::Point> points,
282
0
                                     std::vector<Spline::Point>& result) {
283
0
  if (points.empty()) return;
284
0
  if (points.size() == 1) {
285
0
    result.push_back(points[0]);
286
0
    return;
287
0
  }
288
  // Number of points to compute between each control point.
289
0
  static constexpr int kNumPoints = 16;
290
0
  result.reserve((points.size() - 1) * kNumPoints + 1);
291
0
  points.insert(points.begin(), points[0] + (points[0] - points[1]));
292
0
  points.push_back(points[points.size() - 1] +
293
0
                   (points[points.size() - 1] - points[points.size() - 2]));
294
  // points has at least 4 elements at this point.
295
0
  for (size_t start = 0; start < points.size() - 3; ++start) {
296
    // 4 of them are used, and we draw from p[1] to p[2].
297
0
    const Spline::Point* const p = &points[start];
298
0
    result.push_back(p[1]);
299
0
    float d[3];
300
0
    float t[4];
301
0
    t[0] = 0;
302
0
    for (int k = 0; k < 3; ++k) {
303
      // TODO(eustas): for each segment delta is calculated 3 times...
304
      // TODO(eustas): restrict d[k] with reasonable limit and spec it.
305
0
      d[k] = std::sqrt(hypotf(p[k + 1].x - p[k].x, p[k + 1].y - p[k].y));
306
0
      t[k + 1] = t[k] + d[k];
307
0
    }
308
0
    for (int i = 1; i < kNumPoints; ++i) {
309
0
      const float tt = d[0] + (static_cast<float>(i) / kNumPoints) * d[1];
310
0
      Spline::Point a[3];
311
0
      for (int k = 0; k < 3; ++k) {
312
        // TODO(eustas): reciprocal multiplication would be faster.
313
0
        a[k] = p[k] + ((tt - t[k]) / d[k]) * (p[k + 1] - p[k]);
314
0
      }
315
0
      Spline::Point b[2];
316
0
      for (int k = 0; k < 2; ++k) {
317
0
        b[k] = a[k] + ((tt - t[k]) / (d[k] + d[k + 1])) * (a[k + 1] - a[k]);
318
0
      }
319
0
      result.push_back(b[0] + ((tt - t[1]) / d[1]) * (b[1] - b[0]));
320
0
    }
321
0
  }
322
0
  result.push_back(points[points.size() - 2]);
323
0
}
324
325
// Move along the line segments defined by `points`, `kDesiredRenderingDistance`
326
// pixels at a time, and call `functor` with each point and the actual distance
327
// to the previous point (which will always be kDesiredRenderingDistance except
328
// possibly for the very last point).
329
// TODO(eustas): this method always adds the last point, but never the first
330
//               (unless those are one); I believe both ends matter.
331
template <typename Points, typename Functor>
332
0
void ForEachEquallySpacedPoint(const Points& points, const Functor& functor) {
333
0
  JXL_ASSERT(!points.empty());
334
0
  Spline::Point current = points.front();
335
0
  functor(current, kDesiredRenderingDistance);
336
0
  auto next = points.begin();
337
0
  while (next != points.end()) {
338
0
    const Spline::Point* previous = &current;
339
0
    float arclength_from_previous = 0.f;
340
0
    for (;;) {
341
0
      if (next == points.end()) {
342
0
        functor(*previous, arclength_from_previous);
343
0
        return;
344
0
      }
345
0
      const float arclength_to_next =
346
0
          std::sqrt((*next - *previous).SquaredNorm());
347
0
      if (arclength_from_previous + arclength_to_next >=
348
0
          kDesiredRenderingDistance) {
349
0
        current =
350
0
            *previous + ((kDesiredRenderingDistance - arclength_from_previous) /
351
0
                         arclength_to_next) *
352
0
                            (*next - *previous);
353
0
        functor(current, kDesiredRenderingDistance);
354
0
        break;
355
0
      }
356
0
      arclength_from_previous += arclength_to_next;
357
0
      previous = &*next;
358
0
      ++next;
359
0
    }
360
0
  }
361
0
}
362
363
}  // namespace
364
365
QuantizedSpline::QuantizedSpline(const Spline& original,
366
                                 const int32_t quantization_adjustment,
367
0
                                 const float y_to_x, const float y_to_b) {
368
0
  JXL_ASSERT(!original.control_points.empty());
369
0
  control_points_.reserve(original.control_points.size() - 1);
370
0
  const Spline::Point& starting_point = original.control_points.front();
371
0
  int previous_x = static_cast<int>(std::roundf(starting_point.x));
372
0
  int previous_y = static_cast<int>(std::roundf(starting_point.y));
373
0
  int previous_delta_x = 0;
374
0
  int previous_delta_y = 0;
375
0
  for (auto it = original.control_points.begin() + 1;
376
0
       it != original.control_points.end(); ++it) {
377
0
    const int new_x = static_cast<int>(std::roundf(it->x));
378
0
    const int new_y = static_cast<int>(std::roundf(it->y));
379
0
    const int new_delta_x = new_x - previous_x;
380
0
    const int new_delta_y = new_y - previous_y;
381
0
    control_points_.emplace_back(new_delta_x - previous_delta_x,
382
0
                                 new_delta_y - previous_delta_y);
383
0
    previous_delta_x = new_delta_x;
384
0
    previous_delta_y = new_delta_y;
385
0
    previous_x = new_x;
386
0
    previous_y = new_y;
387
0
  }
388
389
0
  const auto to_int = [](float v) -> int {
390
    // Maximal int representable with float.
391
0
    constexpr float kMax = std::numeric_limits<int>::max() - 127;
392
0
    constexpr float kMin = -kMax;
393
0
    return static_cast<int>(std::roundf(Clamp1(v, kMin, kMax)));
394
0
  };
395
396
0
  const auto quant = AdjustedQuant(quantization_adjustment);
397
0
  const auto inv_quant = InvAdjustedQuant(quantization_adjustment);
398
0
  for (int c : {1, 0, 2}) {
399
0
    float factor = (c == 0) ? y_to_x : (c == 1) ? 0 : y_to_b;
400
0
    for (int i = 0; i < 32; ++i) {
401
0
      const float dct_factor = (i == 0) ? kSqrt2 : 1.0f;
402
0
      const float inv_dct_factor = (i == 0) ? kSqrt0_5 : 1.0f;
403
0
      auto restored_y =
404
0
          color_dct_[1][i] * inv_dct_factor * kChannelWeight[1] * inv_quant;
405
0
      auto decorrelated = original.color_dct[c][i] - factor * restored_y;
406
0
      color_dct_[c][i] =
407
0
          to_int(decorrelated * dct_factor * quant / kChannelWeight[c]);
408
0
    }
409
0
  }
410
0
  for (int i = 0; i < 32; ++i) {
411
0
    const float dct_factor = (i == 0) ? kSqrt2 : 1.0f;
412
0
    sigma_dct_[i] =
413
0
        to_int(original.sigma_dct[i] * dct_factor * quant / kChannelWeight[3]);
414
0
  }
415
0
}
416
417
Status QuantizedSpline::Dequantize(const Spline::Point& starting_point,
418
                                   const int32_t quantization_adjustment,
419
                                   const float y_to_x, const float y_to_b,
420
                                   const uint64_t image_size,
421
                                   uint64_t* total_estimated_area_reached,
422
0
                                   Spline& result) const {
423
0
  constexpr uint64_t kOne = static_cast<uint64_t>(1);
424
0
  const uint64_t area_limit =
425
0
      std::min(1024 * image_size + (kOne << 32), kOne << 42);
426
427
0
  result.control_points.clear();
428
0
  result.control_points.reserve(control_points_.size() + 1);
429
0
  float px = std::roundf(starting_point.x);
430
0
  float py = std::roundf(starting_point.y);
431
0
  JXL_RETURN_IF_ERROR(ValidateSplinePointPos(px, py));
432
0
  int current_x = static_cast<int>(px);
433
0
  int current_y = static_cast<int>(py);
434
0
  result.control_points.emplace_back(static_cast<float>(current_x),
435
0
                                     static_cast<float>(current_y));
436
0
  int current_delta_x = 0;
437
0
  int current_delta_y = 0;
438
0
  uint64_t manhattan_distance = 0;
439
0
  for (const auto& point : control_points_) {
440
0
    current_delta_x += point.first;
441
0
    current_delta_y += point.second;
442
0
    manhattan_distance += std::abs(current_delta_x) + std::abs(current_delta_y);
443
0
    if (manhattan_distance > area_limit) {
444
0
      return JXL_FAILURE("Too large manhattan_distance reached: %" PRIu64,
445
0
                         manhattan_distance);
446
0
    }
447
0
    JXL_RETURN_IF_ERROR(
448
0
        ValidateSplinePointPos(current_delta_x, current_delta_y));
449
0
    current_x += current_delta_x;
450
0
    current_y += current_delta_y;
451
0
    JXL_RETURN_IF_ERROR(ValidateSplinePointPos(current_x, current_y));
452
0
    result.control_points.emplace_back(static_cast<float>(current_x),
453
0
                                       static_cast<float>(current_y));
454
0
  }
455
456
0
  const auto inv_quant = InvAdjustedQuant(quantization_adjustment);
457
0
  for (int c = 0; c < 3; ++c) {
458
0
    for (int i = 0; i < 32; ++i) {
459
0
      const float inv_dct_factor = (i == 0) ? kSqrt0_5 : 1.0f;
460
0
      result.color_dct[c][i] =
461
0
          color_dct_[c][i] * inv_dct_factor * kChannelWeight[c] * inv_quant;
462
0
    }
463
0
  }
464
0
  for (int i = 0; i < 32; ++i) {
465
0
    result.color_dct[0][i] += y_to_x * result.color_dct[1][i];
466
0
    result.color_dct[2][i] += y_to_b * result.color_dct[1][i];
467
0
  }
468
0
  uint64_t width_estimate = 0;
469
470
0
  uint64_t color[3] = {};
471
0
  for (int c = 0; c < 3; ++c) {
472
0
    for (int i = 0; i < 32; ++i) {
473
0
      color[c] += static_cast<uint64_t>(
474
0
          std::ceil(inv_quant * std::abs(color_dct_[c][i])));
475
0
    }
476
0
  }
477
0
  color[0] += static_cast<uint64_t>(std::ceil(std::abs(y_to_x))) * color[1];
478
0
  color[2] += static_cast<uint64_t>(std::ceil(std::abs(y_to_b))) * color[1];
479
  // This is not taking kChannelWeight into account, but up to constant factors
480
  // it gives an indication of the influence of the color values on the area
481
  // that will need to be rendered.
482
0
  const uint64_t max_color = std::max({color[1], color[0], color[2]});
483
0
  uint64_t logcolor =
484
0
      std::max(kOne, static_cast<uint64_t>(CeilLog2Nonzero(kOne + max_color)));
485
486
0
  const float weight_limit =
487
0
      std::ceil(std::sqrt((static_cast<float>(area_limit) / logcolor) /
488
0
                          std::max<size_t>(1, manhattan_distance)));
489
490
0
  for (int i = 0; i < 32; ++i) {
491
0
    const float inv_dct_factor = (i == 0) ? kSqrt0_5 : 1.0f;
492
0
    result.sigma_dct[i] =
493
0
        sigma_dct_[i] * inv_dct_factor * kChannelWeight[3] * inv_quant;
494
    // If we include the factor kChannelWeight[3]=.3333f here, we get a
495
    // realistic area estimate. We leave it out to simplify the calculations,
496
    // and understand that this way we underestimate the area by a factor of
497
    // 1/(0.3333*0.3333). This is taken into account in the limits below.
498
0
    float weight_f = std::ceil(inv_quant * std::abs(sigma_dct_[i]));
499
0
    uint64_t weight =
500
0
        static_cast<uint64_t>(std::min(weight_limit, std::max(1.0f, weight_f)));
501
0
    width_estimate += weight * weight * logcolor;
502
0
  }
503
0
  *total_estimated_area_reached += (width_estimate * manhattan_distance);
504
0
  if (*total_estimated_area_reached > area_limit) {
505
0
    return JXL_FAILURE("Too large total_estimated_area eached: %" PRIu64,
506
0
                       *total_estimated_area_reached);
507
0
  }
508
509
0
  return true;
510
0
}
511
512
Status QuantizedSpline::Decode(const std::vector<uint8_t>& context_map,
513
                               ANSSymbolReader* const decoder,
514
                               BitReader* const br,
515
                               const size_t max_control_points,
516
0
                               size_t* total_num_control_points) {
517
0
  const size_t num_control_points =
518
0
      decoder->ReadHybridUint(kNumControlPointsContext, br, context_map);
519
0
  if (num_control_points > max_control_points) {
520
0
    return JXL_FAILURE("Too many control points: %" PRIuS, num_control_points);
521
0
  }
522
0
  *total_num_control_points += num_control_points;
523
0
  if (*total_num_control_points > max_control_points) {
524
0
    return JXL_FAILURE("Too many control points: %" PRIuS,
525
0
                       *total_num_control_points);
526
0
  }
527
0
  control_points_.resize(num_control_points);
528
  // Maximal image dimension.
529
0
  constexpr int64_t kDeltaLimit = 1u << 30;
530
0
  for (std::pair<int64_t, int64_t>& control_point : control_points_) {
531
0
    control_point.first = UnpackSigned(
532
0
        decoder->ReadHybridUint(kControlPointsContext, br, context_map));
533
0
    control_point.second = UnpackSigned(
534
0
        decoder->ReadHybridUint(kControlPointsContext, br, context_map));
535
    // Check delta-deltas are not outrageous; it is not in spec, but there is
536
    // no reason to allow larger values.
537
0
    if ((control_point.first >= kDeltaLimit) ||
538
0
        (control_point.first <= -kDeltaLimit) ||
539
0
        (control_point.second >= kDeltaLimit) ||
540
0
        (control_point.second <= -kDeltaLimit)) {
541
0
      return JXL_FAILURE("Spline delta-delta is out of bounds");
542
0
    }
543
0
  }
544
545
0
  const auto decode_dct = [decoder, br, &context_map](int dct[32]) -> Status {
546
0
    constexpr int kWeirdNumber = std::numeric_limits<int>::min();
547
0
    for (int i = 0; i < 32; ++i) {
548
0
      dct[i] =
549
0
          UnpackSigned(decoder->ReadHybridUint(kDCTContext, br, context_map));
550
0
      if (dct[i] == kWeirdNumber) {
551
0
        return JXL_FAILURE("The weird number in spline DCT");
552
0
      }
553
0
    }
554
0
    return true;
555
0
  };
556
0
  for (auto& dct : color_dct_) {
557
0
    JXL_RETURN_IF_ERROR(decode_dct(dct));
558
0
  }
559
0
  JXL_RETURN_IF_ERROR(decode_dct(sigma_dct_));
560
0
  return true;
561
0
}
562
563
0
void Splines::Clear() {
564
0
  quantization_adjustment_ = 0;
565
0
  splines_.clear();
566
0
  starting_points_.clear();
567
0
  segments_.clear();
568
0
  segment_indices_.clear();
569
0
  segment_y_start_.clear();
570
0
}
571
572
Status Splines::Decode(JxlMemoryManager* memory_manager, jxl::BitReader* br,
573
0
                       const size_t num_pixels) {
574
0
  std::vector<uint8_t> context_map;
575
0
  ANSCode code;
576
0
  JXL_RETURN_IF_ERROR(DecodeHistograms(memory_manager, br, kNumSplineContexts,
577
0
                                       &code, &context_map));
578
0
  JXL_ASSIGN_OR_RETURN(ANSSymbolReader decoder,
579
0
                       ANSSymbolReader::Create(&code, br));
580
0
  size_t num_splines =
581
0
      decoder.ReadHybridUint(kNumSplinesContext, br, context_map);
582
0
  size_t max_control_points = std::min(
583
0
      kMaxNumControlPoints, num_pixels / kMaxNumControlPointsPerPixelRatio);
584
0
  if (num_splines > max_control_points ||
585
0
      num_splines + 1 > max_control_points) {
586
0
    return JXL_FAILURE("Too many splines: %" PRIuS, num_splines);
587
0
  }
588
0
  num_splines++;
589
0
  JXL_RETURN_IF_ERROR(DecodeAllStartingPoints(&starting_points_, br, &decoder,
590
0
                                              context_map, num_splines));
591
592
0
  quantization_adjustment_ = UnpackSigned(
593
0
      decoder.ReadHybridUint(kQuantizationAdjustmentContext, br, context_map));
594
595
0
  splines_.clear();
596
0
  splines_.reserve(num_splines);
597
0
  size_t num_control_points = num_splines;
598
0
  for (size_t i = 0; i < num_splines; ++i) {
599
0
    QuantizedSpline spline;
600
0
    JXL_RETURN_IF_ERROR(spline.Decode(context_map, &decoder, br,
601
0
                                      max_control_points, &num_control_points));
602
0
    splines_.push_back(std::move(spline));
603
0
  }
604
605
0
  JXL_RETURN_IF_ERROR(decoder.CheckANSFinalState());
606
607
0
  if (!HasAny()) {
608
0
    return JXL_FAILURE("Decoded splines but got none");
609
0
  }
610
611
0
  return true;
612
0
}
613
614
void Splines::AddTo(Image3F* const opsin, const Rect& opsin_rect,
615
0
                    const Rect& image_rect) const {
616
0
  Apply</*add=*/true>(opsin, opsin_rect, image_rect);
617
0
}
618
void Splines::AddToRow(float* JXL_RESTRICT row_x, float* JXL_RESTRICT row_y,
619
0
                       float* JXL_RESTRICT row_b, const Rect& image_row) const {
620
0
  ApplyToRow</*add=*/true>(row_x, row_y, row_b, image_row);
621
0
}
622
623
0
void Splines::SubtractFrom(Image3F* const opsin) const {
624
0
  Apply</*add=*/false>(opsin, Rect(*opsin), Rect(*opsin));
625
0
}
626
627
Status Splines::InitializeDrawCache(const size_t image_xsize,
628
                                    const size_t image_ysize,
629
0
                                    const ColorCorrelation& color_correlation) {
630
  // TODO(veluca): avoid storing segments that are entirely outside image
631
  // boundaries.
632
0
  segments_.clear();
633
0
  segment_indices_.clear();
634
0
  segment_y_start_.clear();
635
0
  std::vector<std::pair<size_t, size_t>> segments_by_y;
636
0
  std::vector<Spline::Point> intermediate_points;
637
0
  uint64_t total_estimated_area_reached = 0;
638
0
  std::vector<Spline> splines;
639
0
  for (size_t i = 0; i < splines_.size(); ++i) {
640
0
    Spline spline;
641
0
    JXL_RETURN_IF_ERROR(splines_[i].Dequantize(
642
0
        starting_points_[i], quantization_adjustment_,
643
0
        color_correlation.YtoXRatio(0), color_correlation.YtoBRatio(0),
644
0
        image_xsize * image_ysize, &total_estimated_area_reached, spline));
645
0
    if (std::adjacent_find(spline.control_points.begin(),
646
0
                           spline.control_points.end()) !=
647
0
        spline.control_points.end()) {
648
      // Otherwise division by zero might occur. Once control points coincide,
649
      // the direction of curve is undefined...
650
0
      return JXL_FAILURE(
651
0
          "identical successive control points in spline %" PRIuS, i);
652
0
    }
653
0
    splines.push_back(spline);
654
0
  }
655
  // TODO(firsching) Change this into a JXL_FAILURE for level 5 codestreams.
656
0
  if (total_estimated_area_reached >
657
0
      std::min(
658
0
          (8 * image_xsize * image_ysize + (static_cast<uint64_t>(1) << 25)),
659
0
          (static_cast<uint64_t>(1) << 30))) {
660
0
    JXL_WARNING(
661
0
        "Large total_estimated_area_reached, expect slower decoding: %" PRIu64,
662
0
        total_estimated_area_reached);
663
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
664
0
    return JXL_FAILURE("Total spline area is too large");
665
0
#endif
666
0
  }
667
668
0
  for (Spline& spline : splines) {
669
0
    std::vector<std::pair<Spline::Point, float>> points_to_draw;
670
0
    auto add_point = [&](const Spline::Point& point, const float multiplier) {
671
0
      points_to_draw.emplace_back(point, multiplier);
672
0
    };
673
0
    intermediate_points.clear();
674
0
    DrawCentripetalCatmullRomSpline(spline.control_points, intermediate_points);
675
0
    ForEachEquallySpacedPoint(intermediate_points, add_point);
676
0
    const float arc_length =
677
0
        (points_to_draw.size() - 2) * kDesiredRenderingDistance +
678
0
        points_to_draw.back().second;
679
0
    if (arc_length <= 0.f) {
680
      // This spline wouldn't have any effect.
681
0
      continue;
682
0
    }
683
0
    HWY_DYNAMIC_DISPATCH(SegmentsFromPoints)
684
0
    (spline, points_to_draw, arc_length, segments_, segments_by_y);
685
0
  }
686
687
  // TODO(eustas): consider linear sorting here.
688
0
  std::sort(segments_by_y.begin(), segments_by_y.end());
689
0
  segment_indices_.resize(segments_by_y.size());
690
0
  segment_y_start_.resize(image_ysize + 1);
691
0
  for (size_t i = 0; i < segments_by_y.size(); i++) {
692
0
    segment_indices_[i] = segments_by_y[i].second;
693
0
    size_t y = segments_by_y[i].first;
694
0
    if (y < image_ysize) {
695
0
      segment_y_start_[y + 1]++;
696
0
    }
697
0
  }
698
0
  for (size_t y = 0; y < image_ysize; y++) {
699
0
    segment_y_start_[y + 1] += segment_y_start_[y];
700
0
  }
701
0
  return true;
702
0
}
703
704
template <bool add>
705
void Splines::ApplyToRow(float* JXL_RESTRICT row_x, float* JXL_RESTRICT row_y,
706
                         float* JXL_RESTRICT row_b,
707
0
                         const Rect& image_row) const {
708
0
  if (segments_.empty()) return;
709
0
  JXL_ASSERT(image_row.ysize() == 1);
710
0
  for (size_t iy = 0; iy < image_row.ysize(); iy++) {
711
0
    HWY_DYNAMIC_DISPATCH(DrawSegments)
712
0
    (row_x, row_y, row_b, image_row.Line(iy), add, segments_.data(),
713
0
     segment_indices_.data(), segment_y_start_.data());
714
0
  }
715
0
}
Unexecuted instantiation: void jxl::Splines::ApplyToRow<true>(float*, float*, float*, jxl::RectT<unsigned long> const&) const
Unexecuted instantiation: void jxl::Splines::ApplyToRow<false>(float*, float*, float*, jxl::RectT<unsigned long> const&) const
716
717
template <bool add>
718
void Splines::Apply(Image3F* const opsin, const Rect& opsin_rect,
719
0
                    const Rect& image_rect) const {
720
0
  if (segments_.empty()) return;
721
0
  for (size_t iy = 0; iy < image_rect.ysize(); iy++) {
722
0
    const size_t y0 = opsin_rect.Line(iy).y0();
723
0
    const size_t x0 = opsin_rect.x0();
724
0
    ApplyToRow<add>(opsin->PlaneRow(0, y0) + x0, opsin->PlaneRow(1, y0) + x0,
725
0
                    opsin->PlaneRow(2, y0) + x0, image_rect.Line(iy));
726
0
  }
727
0
}
Unexecuted instantiation: void jxl::Splines::Apply<true>(jxl::Image3<float>*, jxl::RectT<unsigned long> const&, jxl::RectT<unsigned long> const&) const
Unexecuted instantiation: void jxl::Splines::Apply<false>(jxl::Image3<float>*, jxl::RectT<unsigned long> const&, jxl::RectT<unsigned long> const&) const
728
729
}  // namespace jxl
730
#endif  // HWY_ONCE