Coverage Report

Created: 2025-07-16 07:53

/src/libjxl/lib/jxl/enc_cluster.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
// Functions for clustering similar histograms together.
7
8
#ifndef LIB_JXL_ENC_CLUSTER_H_
9
#define LIB_JXL_ENC_CLUSTER_H_
10
11
#include <cstddef>
12
#include <cstdint>
13
#include <cstring>
14
#include <vector>
15
16
#include "lib/jxl/base/common.h"
17
#include "lib/jxl/base/status.h"
18
#include "lib/jxl/enc_ans_params.h"
19
20
namespace jxl {
21
22
struct Histogram {
23
0
  Histogram() {
24
0
    total_count_ = 0;
25
0
    entropy_ = 0.0;
26
0
  }
27
0
  void Clear() {
28
0
    data_.clear();
29
0
    total_count_ = 0;
30
0
  }
31
0
  void Add(size_t symbol) {
32
0
    if (data_.size() <= symbol) {
33
0
      data_.resize(DivCeil(symbol + 1, kRounding) * kRounding);
34
0
    }
35
0
    ++data_[symbol];
36
0
    ++total_count_;
37
0
  }
38
0
  void AddHistogram(const Histogram& other) {
39
0
    if (other.data_.size() > data_.size()) {
40
0
      data_.resize(other.data_.size());
41
0
    }
42
0
    for (size_t i = 0; i < other.data_.size(); ++i) {
43
0
      data_[i] += other.data_[i];
44
0
    }
45
0
    total_count_ += other.total_count_;
46
0
  }
47
0
  size_t alphabet_size() const {
48
0
    for (int i = data_.size() - 1; i >= 0; --i) {
49
0
      if (data_[i] > 0) {
50
0
        return i + 1;
51
0
      }
52
0
    }
53
0
    return 1;
54
0
  }
55
  StatusOr<float> PopulationCost() const;
56
  float ShannonEntropy() const;
57
58
  std::vector<ANSHistBin> data_;
59
  size_t total_count_;
60
  mutable float entropy_;  // WARNING: not kept up-to-date.
61
  static constexpr size_t kRounding = 8;
62
};
63
64
Status ClusterHistograms(const HistogramParams& params,
65
                         const std::vector<Histogram>& in,
66
                         size_t max_histograms, std::vector<Histogram>* out,
67
                         std::vector<uint32_t>* histogram_symbols);
68
}  // namespace jxl
69
70
#endif  // LIB_JXL_ENC_CLUSTER_H_