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