color_hints.h (2342B)
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 LIB_EXTRAS_COLOR_HINTS_H_ 7 #define LIB_EXTRAS_COLOR_HINTS_H_ 8 9 // Not all the formats implemented in the extras lib support bundling color 10 // information into the file, and those that support it may not have it. 11 // To allow attaching color information to those file formats the caller can 12 // define these color hints. 13 // Besides color space information, 'ColorHints' may also include other 14 // additional information such as Exif, XMP and JUMBF metadata. 15 16 #include <stddef.h> 17 #include <stdint.h> 18 19 #include <string> 20 #include <vector> 21 22 #include "lib/extras/packed_image.h" 23 #include "lib/jxl/base/status.h" 24 25 namespace jxl { 26 namespace extras { 27 28 class ColorHints { 29 public: 30 // key=color_space, value=Description(c/pp): specify the ColorEncoding of 31 // the pixels for decoding. Otherwise, if the codec did not obtain an ICC 32 // profile from the image, assume sRGB. 33 // 34 // Strings are taken from the command line, so avoid spaces for convenience. 35 void Add(const std::string& key, const std::string& value) { 36 kv_.emplace_back(key, value); 37 } 38 39 // Calls `func(key, value)` for each key/value in the order they were added, 40 // returning false immediately if `func` returns false. 41 template <class Func> 42 Status Foreach(const Func& func) const { 43 for (const KeyValue& kv : kv_) { 44 Status ok = func(kv.key, kv.value); 45 if (!ok) { 46 return JXL_FAILURE("ColorHints::Foreach returned false"); 47 } 48 } 49 return true; 50 } 51 52 private: 53 // Splitting into key/value avoids parsing in each codec. 54 struct KeyValue { 55 KeyValue(std::string key, std::string value) 56 : key(std::move(key)), value(std::move(value)) {} 57 58 std::string key; 59 std::string value; 60 }; 61 62 std::vector<KeyValue> kv_; 63 }; 64 65 // Apply the color hints to the decoded image in PackedPixelFile if any. 66 // color_already_set tells whether the color encoding was already set, in which 67 // case the hints are ignored if any hint is passed. 68 Status ApplyColorHints(const ColorHints& color_hints, bool color_already_set, 69 bool is_gray, PackedPixelFile* ppf); 70 71 } // namespace extras 72 } // namespace jxl 73 74 #endif // LIB_EXTRAS_COLOR_HINTS_H_