Coverage Report

Created: 2024-05-21 06:24

/src/libjxl/lib/jxl/enc_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/base/status.h"
7
#include "lib/jxl/enc_ans.h"
8
#include "lib/jxl/pack_signed.h"
9
#include "lib/jxl/splines.h"
10
11
namespace jxl {
12
13
struct AuxOut;
14
15
class QuantizedSplineEncoder {
16
 public:
17
  // Only call if HasAny().
18
  static void Tokenize(const QuantizedSpline& spline,
19
0
                       std::vector<Token>* const tokens) {
20
0
    tokens->emplace_back(kNumControlPointsContext,
21
0
                         spline.control_points_.size());
22
0
    for (const auto& point : spline.control_points_) {
23
0
      tokens->emplace_back(kControlPointsContext, PackSigned(point.first));
24
0
      tokens->emplace_back(kControlPointsContext, PackSigned(point.second));
25
0
    }
26
0
    const auto encode_dct = [tokens](const int dct[32]) {
27
0
      for (int i = 0; i < 32; ++i) {
28
0
        tokens->emplace_back(kDCTContext, PackSigned(dct[i]));
29
0
      }
30
0
    };
31
0
    for (const auto& dct : spline.color_dct_) {
32
0
      encode_dct(dct);
33
0
    }
34
0
    encode_dct(spline.sigma_dct_);
35
0
  }
36
};
37
38
namespace {
39
40
void EncodeAllStartingPoints(const std::vector<Spline::Point>& points,
41
0
                             std::vector<Token>* tokens) {
42
0
  int64_t last_x = 0;
43
0
  int64_t last_y = 0;
44
0
  for (size_t i = 0; i < points.size(); i++) {
45
0
    const int64_t x = lroundf(points[i].x);
46
0
    const int64_t y = lroundf(points[i].y);
47
0
    if (i == 0) {
48
0
      tokens->emplace_back(kStartingPositionContext, x);
49
0
      tokens->emplace_back(kStartingPositionContext, y);
50
0
    } else {
51
0
      tokens->emplace_back(kStartingPositionContext, PackSigned(x - last_x));
52
0
      tokens->emplace_back(kStartingPositionContext, PackSigned(y - last_y));
53
0
    }
54
0
    last_x = x;
55
0
    last_y = y;
56
0
  }
57
0
}
58
59
}  // namespace
60
61
void EncodeSplines(const Splines& splines, BitWriter* writer,
62
                   const size_t layer, const HistogramParams& histogram_params,
63
0
                   AuxOut* aux_out) {
64
0
  JXL_ASSERT(splines.HasAny());
65
66
0
  const std::vector<QuantizedSpline>& quantized_splines =
67
0
      splines.QuantizedSplines();
68
0
  std::vector<std::vector<Token>> tokens(1);
69
0
  tokens[0].emplace_back(kNumSplinesContext, quantized_splines.size() - 1);
70
0
  EncodeAllStartingPoints(splines.StartingPoints(), tokens.data());
71
72
0
  tokens[0].emplace_back(kQuantizationAdjustmentContext,
73
0
                         PackSigned(splines.GetQuantizationAdjustment()));
74
75
0
  for (const QuantizedSpline& spline : quantized_splines) {
76
0
    QuantizedSplineEncoder::Tokenize(spline, tokens.data());
77
0
  }
78
79
0
  EntropyEncodingData codes;
80
0
  std::vector<uint8_t> context_map;
81
0
  BuildAndEncodeHistograms(writer->memory_manager(), histogram_params,
82
0
                           kNumSplineContexts, tokens, &codes, &context_map,
83
0
                           writer, layer, aux_out);
84
0
  WriteTokens(tokens[0], codes, context_map, 0, writer, layer, aux_out);
85
0
}
86
87
52
Splines FindSplines(const Image3F& opsin) {
88
  // TODO(user): implement spline detection.
89
52
  return {};
90
52
}
91
92
}  // namespace jxl