enc_cluster.h (1801B)
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 Histogram() { 24 total_count_ = 0; 25 entropy_ = 0.0; 26 } 27 void Clear() { 28 data_.clear(); 29 total_count_ = 0; 30 } 31 void Add(size_t symbol) { 32 if (data_.size() <= symbol) { 33 data_.resize(DivCeil(symbol + 1, kRounding) * kRounding); 34 } 35 ++data_[symbol]; 36 ++total_count_; 37 } 38 void AddHistogram(const Histogram& other) { 39 if (other.data_.size() > data_.size()) { 40 data_.resize(other.data_.size()); 41 } 42 for (size_t i = 0; i < other.data_.size(); ++i) { 43 data_[i] += other.data_[i]; 44 } 45 total_count_ += other.total_count_; 46 } 47 size_t alphabet_size() const { 48 for (int i = data_.size() - 1; i >= 0; --i) { 49 if (data_[i] > 0) { 50 return i + 1; 51 } 52 } 53 return 1; 54 } 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_