benchmark_args.h (5271B)
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 TOOLS_BENCHMARK_BENCHMARK_ARGS_H_ 7 #define TOOLS_BENCHMARK_BENCHMARK_ARGS_H_ 8 9 // Command line parsing and arguments for benchmark_xl 10 11 #include <stddef.h> 12 13 #include <algorithm> 14 #include <deque> 15 #include <string> 16 #include <vector> 17 18 #include "lib/extras/dec/color_hints.h" 19 #include "lib/jxl/base/override.h" 20 #include "lib/jxl/base/status.h" 21 #include "lib/jxl/butteraugli/butteraugli.h" 22 #include "lib/jxl/color_encoding_internal.h" 23 #include "tools/args.h" 24 #include "tools/cmdline.h" 25 26 namespace jpegxl { 27 namespace tools { 28 29 using ::jxl::ColorEncoding; 30 using ::jxl::Override; 31 using ::jxl::Status; 32 33 std::vector<std::string> SplitString(const std::string& s, char c); 34 35 int ParseIntParam(const std::string& param, int lower_bound, int upper_bound); 36 37 struct BenchmarkArgs { 38 using OptionId = jpegxl::tools::CommandLineParser::OptionId; 39 40 void AddFlag(bool* field, const char* longName, const char* help, 41 bool defaultValue) { 42 const char* noName = RememberString_(std::string("no") + longName); 43 cmdline.AddOptionFlag('\0', longName, nullptr, field, 44 &jpegxl::tools::SetBooleanTrue); 45 cmdline.AddOptionFlag('\0', noName, help, field, 46 &jpegxl::tools::SetBooleanFalse); 47 *field = defaultValue; 48 } 49 50 OptionId AddOverride(Override* field, const char* longName, 51 const char* help) { 52 OptionId result = cmdline.AddOptionValue('\0', longName, "0|1", help, field, 53 &jpegxl::tools::ParseOverride); 54 *field = Override::kDefault; 55 return result; 56 } 57 58 OptionId AddString(std::string* field, const char* longName, const char* help, 59 const std::string& defaultValue = "") { 60 OptionId result = cmdline.AddOptionValue( 61 '\0', longName, "<string>", help, field, &jpegxl::tools::ParseString); 62 *field = defaultValue; 63 return result; 64 } 65 66 OptionId AddFloat(float* field, const char* longName, const char* help, 67 float defaultValue) { 68 OptionId result = cmdline.AddOptionValue('\0', longName, "<scalar>", help, 69 field, &jpegxl::tools::ParseFloat); 70 *field = defaultValue; 71 return result; 72 } 73 74 OptionId AddDouble(double* field, const char* longName, const char* help, 75 double defaultValue) { 76 OptionId result = cmdline.AddOptionValue( 77 '\0', longName, "<scalar>", help, field, &jpegxl::tools::ParseDouble); 78 *field = defaultValue; 79 return result; 80 } 81 82 OptionId AddSigned(int* field, const char* longName, const char* help, 83 int defaultValue) { 84 OptionId result = cmdline.AddOptionValue( 85 '\0', longName, "<integer>", help, field, &jpegxl::tools::ParseSigned); 86 *field = defaultValue; 87 return result; 88 } 89 90 OptionId AddUnsigned(size_t* field, const char* longName, const char* help, 91 size_t defaultValue) { 92 OptionId result = 93 cmdline.AddOptionValue('\0', longName, "<unsigned>", help, field, 94 &jpegxl::tools::ParseUnsigned); 95 *field = defaultValue; 96 return result; 97 } 98 99 Status AddCommandLineOptions(); 100 101 Status ValidateArgs(); 102 103 bool Parse(int argc, const char** argv) { return cmdline.Parse(argc, argv); } 104 105 void PrintHelp() const { cmdline.PrintHelp(); } 106 107 std::string input; 108 std::string codec; 109 bool print_details; 110 bool print_details_csv; 111 bool print_more_stats; 112 bool print_distance_percentiles; 113 bool silent_errors; 114 bool save_compressed; 115 bool save_decompressed; 116 std::string output_extension; // see CodecFromPath 117 std::string output_description; // see ParseDescription 118 ColorEncoding output_encoding; // determined by output_description 119 120 bool decode_only; 121 bool skip_butteraugli; 122 123 float intensity_target; 124 125 std::string color_hints_string; 126 jxl::extras::ColorHints color_hints; 127 128 size_t override_bitdepth; 129 130 double mul_output; 131 double heatmap_good; 132 double heatmap_bad; 133 134 bool save_heatmap; 135 bool write_html_report; 136 bool html_report_self_contained; 137 bool html_report_use_decompressed; 138 bool html_report_add_heatmap; 139 bool markdown; 140 bool more_columns; 141 142 std::string originals_url; 143 std::string output_dir; 144 145 int num_threads; 146 int inner_threads; 147 size_t decode_reps; 148 size_t encode_reps; 149 150 std::string sample_tmp_dir; 151 152 int num_samples; 153 int sample_dimensions; 154 155 double error_pnorm; 156 bool show_progress; 157 158 std::string extra_metrics; 159 160 jpegxl::tools::CommandLineParser cmdline; 161 162 private: 163 const char* RememberString_(const std::string& text) { 164 const char* data = text.c_str(); 165 std::vector<char> copy(data, data + text.size() + 1); 166 string_pool_.push_back(copy); 167 return string_pool_.back().data(); 168 } 169 170 // A memory pool with stable addresses for strings to provide stable 171 // const char pointers to cmdline.h for dynamic help/name strings. 172 std::deque<std::vector<char>> string_pool_; 173 }; 174 175 // Returns singleton 176 BenchmarkArgs* Args(); 177 178 } // namespace tools 179 } // namespace jpegxl 180 181 #endif // TOOLS_BENCHMARK_BENCHMARK_ARGS_H_