Coverage Report

Created: 2024-05-21 06:26

/src/libjxl/lib/jxl/splines.h
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
#ifndef LIB_JXL_SPLINES_H_
7
#define LIB_JXL_SPLINES_H_
8
9
#include <jxl/memory_manager.h>
10
11
#include <array>
12
#include <cmath>
13
#include <cstddef>
14
#include <cstdint>
15
#include <utility>
16
#include <vector>
17
18
#include "lib/jxl/base/compiler_specific.h"
19
#include "lib/jxl/base/rect.h"
20
#include "lib/jxl/base/status.h"
21
#include "lib/jxl/chroma_from_luma.h"
22
#include "lib/jxl/image.h"
23
24
namespace jxl {
25
26
class ANSSymbolReader;
27
class BitReader;
28
29
static constexpr float kDesiredRenderingDistance = 1.f;
30
31
typedef std::array<float, 32> Dct32;
32
33
enum SplineEntropyContexts : size_t {
34
  kQuantizationAdjustmentContext = 0,
35
  kStartingPositionContext,
36
  kNumSplinesContext,
37
  kNumControlPointsContext,
38
  kControlPointsContext,
39
  kDCTContext,
40
  kNumSplineContexts
41
};
42
43
struct Spline {
44
  struct Point {
45
0
    Point() : x(0.0f), y(0.0f) {}
46
0
    Point(float x, float y) : x(x), y(y) {}
47
    float x, y;
48
0
    bool operator==(const Point& other) const {
49
0
      return std::fabs(x - other.x) < 1e-3f && std::fabs(y - other.y) < 1e-3f;
50
0
    }
51
  };
52
  std::vector<Point> control_points;
53
  // X, Y, B.
54
  std::array<Dct32, 3> color_dct;
55
  // Splines are draws by normalized Gaussian splatting. This controls the
56
  // Gaussian's parameter along the spline.
57
  Dct32 sigma_dct;
58
};
59
60
class QuantizedSplineEncoder;
61
62
class QuantizedSpline {
63
 public:
64
0
  QuantizedSpline() = default;
65
  explicit QuantizedSpline(const Spline& original,
66
                           int32_t quantization_adjustment, float y_to_x,
67
                           float y_to_b);
68
69
  Status Dequantize(const Spline::Point& starting_point,
70
                    int32_t quantization_adjustment, float y_to_x, float y_to_b,
71
                    uint64_t image_size, uint64_t* total_estimated_area_reached,
72
                    Spline& result) const;
73
74
  Status Decode(const std::vector<uint8_t>& context_map,
75
                ANSSymbolReader* decoder, BitReader* br,
76
                size_t max_control_points, size_t* total_num_control_points);
77
78
 private:
79
  friend class QuantizedSplineEncoder;
80
81
  std::vector<std::pair<int64_t, int64_t>>
82
      control_points_;  // Double delta-encoded.
83
  int color_dct_[3][32] = {};
84
  int sigma_dct_[32] = {};
85
};
86
87
// A single "drawable unit" of a spline, i.e. a line of the region in which we
88
// render each Gaussian. The structure doesn't actually depend on the exact
89
// row, which allows reuse for different y values (which are tracked
90
// separately).
91
struct SplineSegment {
92
  float center_x, center_y;
93
  float maximum_distance;
94
  float inv_sigma;
95
  float sigma_over_4_times_intensity;
96
  float color[3];
97
};
98
99
class Splines {
100
 public:
101
0
  Splines() = default;
102
  explicit Splines(const int32_t quantization_adjustment,
103
                   std::vector<QuantizedSpline> splines,
104
                   std::vector<Spline::Point> starting_points)
105
      : quantization_adjustment_(quantization_adjustment),
106
        splines_(std::move(splines)),
107
0
        starting_points_(std::move(starting_points)) {}
108
109
0
  bool HasAny() const { return !splines_.empty(); }
110
111
  void Clear();
112
113
  Status Decode(JxlMemoryManager* memory_manager, BitReader* br,
114
                size_t num_pixels);
115
116
  void AddTo(Image3F* opsin, const Rect& opsin_rect,
117
             const Rect& image_rect) const;
118
  void AddToRow(float* JXL_RESTRICT row_x, float* JXL_RESTRICT row_y,
119
                float* JXL_RESTRICT row_b, const Rect& image_row) const;
120
  void SubtractFrom(Image3F* opsin) const;
121
122
0
  const std::vector<QuantizedSpline>& QuantizedSplines() const {
123
0
    return splines_;
124
0
  }
125
0
  const std::vector<Spline::Point>& StartingPoints() const {
126
0
    return starting_points_;
127
0
  }
128
129
0
  int32_t GetQuantizationAdjustment() const { return quantization_adjustment_; }
130
131
  Status InitializeDrawCache(size_t image_xsize, size_t image_ysize,
132
                             const ColorCorrelation& color_correlation);
133
134
 private:
135
  template <bool>
136
  void ApplyToRow(float* JXL_RESTRICT row_x, float* JXL_RESTRICT row_y,
137
                  float* JXL_RESTRICT row_b, const Rect& image_row) const;
138
  template <bool>
139
  void Apply(Image3F* opsin, const Rect& opsin_rect,
140
             const Rect& image_rect) const;
141
142
  // If positive, quantization weights are multiplied by 1 + this/8, which
143
  // increases precision. If negative, they are divided by 1 - this/8. If 0,
144
  // they are unchanged.
145
  int32_t quantization_adjustment_ = 0;
146
  std::vector<QuantizedSpline> splines_;
147
  std::vector<Spline::Point> starting_points_;
148
  std::vector<SplineSegment> segments_;
149
  std::vector<size_t> segment_indices_;
150
  std::vector<size_t> segment_y_start_;
151
};
152
153
}  // namespace jxl
154
155
#endif  // LIB_JXL_SPLINES_H_