Imported Upstream version 0.9.0
[platform/upstream/libjxl.git] / tools / args.h
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_ARGS_H_
7 #define TOOLS_ARGS_H_
8
9 // Helpers for parsing command line arguments. No include guard needed.
10
11 #include <inttypes.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <string>
17 #include <utility>
18
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"
23
24 namespace jpegxl {
25 namespace tools {
26
27 static inline bool ParseOverride(const char* arg, jxl::Override* out) {
28   const std::string s_arg(arg);
29   if (s_arg == "1") {
30     *out = jxl::Override::kOn;
31     return true;
32   }
33   if (s_arg == "0") {
34     *out = jxl::Override::kOff;
35     return true;
36   }
37   fprintf(stderr, "Invalid flag, %s must be 0 or 1\n", arg);
38   return JXL_FAILURE("Args");
39 }
40
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);
44   if (parsed == 1) {
45     out->second = out->first;
46   } else if (parsed != 2) {
47     fprintf(stderr,
48             "Unable to interpret as float pair separated by a comma: %s.\n",
49             arg);
50     return JXL_FAILURE("Args");
51   }
52   return true;
53 }
54
55 template <typename Callback>
56 static inline bool ParseAndAppendKeyValue(const char* arg, Callback* cb) {
57   const char* eq = strchr(arg, '=');
58   if (!eq) {
59     fprintf(stderr, "Expected argument as 'key=value' but received '%s'\n",
60             arg);
61     return false;
62   }
63   std::string key(arg, eq);
64   return (*cb)(key, std::string(eq + 1));
65 }
66
67 static inline bool ParseCString(const char* arg, const char** out) {
68   *out = arg;
69   return true;
70 }
71
72 static inline bool IncrementUnsigned(size_t* out) {
73   (*out)++;
74   return true;
75 }
76
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, "");
92     } else {
93       target.Add(key, value);
94     }
95     return true;
96   }
97 };
98
99 }  // namespace tools
100 }  // namespace jpegxl
101
102 #endif  // TOOLS_ARGS_H_