1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
9 // Helpers for parsing command line arguments. No include guard needed.
19 #include "lib/extras/dec/color_hints.h"
20 #include "lib/jxl/base/override.h"
21 #include "lib/jxl/base/status.h"
22 #include "tools/file_io.h"
27 static inline bool ParseOverride(const char* arg, jxl::Override* out) {
28 const std::string s_arg(arg);
30 *out = jxl::Override::kOn;
34 *out = jxl::Override::kOff;
37 fprintf(stderr, "Invalid flag, %s must be 0 or 1\n", arg);
38 return JXL_FAILURE("Args");
41 static inline bool ParseFloatPair(const char* arg,
42 std::pair<float, float>* out) {
43 int parsed = sscanf(arg, "%f,%f", &out->first, &out->second);
45 out->second = out->first;
46 } else if (parsed != 2) {
48 "Unable to interpret as float pair separated by a comma: %s.\n",
50 return JXL_FAILURE("Args");
55 template <typename Callback>
56 static inline bool ParseAndAppendKeyValue(const char* arg, Callback* cb) {
57 const char* eq = strchr(arg, '=');
59 fprintf(stderr, "Expected argument as 'key=value' but received '%s'\n",
63 std::string key(arg, eq);
64 return (*cb)(key, std::string(eq + 1));
67 static inline bool ParseCString(const char* arg, const char** out) {
72 static inline bool IncrementUnsigned(size_t* out) {
77 struct ColorHintsProxy {
78 jxl::extras::ColorHints target;
79 bool operator()(const std::string& key, const std::string& value) {
80 if (key == "icc_pathname") {
81 std::vector<uint8_t> icc;
82 JXL_RETURN_IF_ERROR(ReadFile(value, &icc));
83 const char* data = reinterpret_cast<const char*>(icc.data());
84 target.Add("icc", std::string(data, data + icc.size()));
85 } else if (key == "exif" || key == "xmp" || key == "jumbf") {
86 std::vector<uint8_t> metadata;
87 JXL_RETURN_IF_ERROR(ReadFile(value, &metadata));
88 const char* data = reinterpret_cast<const char*>(metadata.data());
89 target.Add(key, std::string(data, data + metadata.size()));
90 } else if (key == "strip") {
91 target.Add(value, "");
93 target.Add(key, value);
100 } // namespace jpegxl
102 #endif // TOOLS_ARGS_H_