libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

args.h (2746B)


      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 <stdint.h>
     12 #include <stdio.h>
     13 #include <string.h>
     14 
     15 #include <string>
     16 #include <utility>
     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 "tools/file_io.h"
     22 
     23 namespace jpegxl {
     24 namespace tools {
     25 
     26 static inline bool ParseOverride(const char* arg, jxl::Override* out) {
     27   const std::string s_arg(arg);
     28   if (s_arg == "1") {
     29     *out = jxl::Override::kOn;
     30     return true;
     31   }
     32   if (s_arg == "0") {
     33     *out = jxl::Override::kOff;
     34     return true;
     35   }
     36   fprintf(stderr, "Invalid flag, %s must be 0 or 1\n", arg);
     37   return JXL_FAILURE("Args");
     38 }
     39 
     40 static inline bool ParseFloatPair(const char* arg,
     41                                   std::pair<float, float>* out) {
     42   int parsed = sscanf(arg, "%f,%f", &out->first, &out->second);
     43   if (parsed == 1) {
     44     out->second = out->first;
     45   } else if (parsed != 2) {
     46     fprintf(stderr,
     47             "Unable to interpret as float pair separated by a comma: %s.\n",
     48             arg);
     49     return JXL_FAILURE("Args");
     50   }
     51   return true;
     52 }
     53 
     54 template <typename Callback>
     55 static inline bool ParseAndAppendKeyValue(const char* arg, Callback* cb) {
     56   const char* eq = strchr(arg, '=');
     57   if (!eq) {
     58     fprintf(stderr, "Expected argument as 'key=value' but received '%s'\n",
     59             arg);
     60     return false;
     61   }
     62   std::string key(arg, eq);
     63   return (*cb)(key, std::string(eq + 1));
     64 }
     65 
     66 static inline bool ParseCString(const char* arg, const char** out) {
     67   *out = arg;
     68   return true;
     69 }
     70 
     71 static inline bool IncrementUnsigned(size_t* out) {
     72   (*out)++;
     73   return true;
     74 }
     75 
     76 struct ColorHintsProxy {
     77   jxl::extras::ColorHints target;
     78   bool operator()(const std::string& key, const std::string& value) {
     79     if (key == "icc_pathname") {
     80       std::vector<uint8_t> icc;
     81       JXL_RETURN_IF_ERROR(ReadFile(value, &icc));
     82       const char* data = reinterpret_cast<const char*>(icc.data());
     83       target.Add("icc", std::string(data, data + icc.size()));
     84     } else if (key == "exif" || key == "xmp" || key == "jumbf") {
     85       std::vector<uint8_t> metadata;
     86       JXL_RETURN_IF_ERROR(ReadFile(value, &metadata));
     87       const char* data = reinterpret_cast<const char*>(metadata.data());
     88       target.Add(key, std::string(data, data + metadata.size()));
     89     } else if (key == "strip") {
     90       target.Add(value, "");
     91     } else {
     92       target.Add(key, value);
     93     }
     94     return true;
     95   }
     96 };
     97 
     98 }  // namespace tools
     99 }  // namespace jpegxl
    100 
    101 #endif  // TOOLS_ARGS_H_