libjxl

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

color_hints.cc (2949B)


      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 #include "lib/extras/dec/color_hints.h"
      7 
      8 #include <jxl/encode.h>
      9 
     10 #include <vector>
     11 
     12 #include "lib/extras/dec/color_description.h"
     13 #include "lib/jxl/base/status.h"
     14 
     15 namespace jxl {
     16 namespace extras {
     17 
     18 Status ApplyColorHints(const ColorHints& color_hints,
     19                        const bool color_already_set, const bool is_gray,
     20                        PackedPixelFile* ppf) {
     21   bool got_color_space = color_already_set;
     22 
     23   JXL_RETURN_IF_ERROR(color_hints.Foreach(
     24       [color_already_set, is_gray, ppf, &got_color_space](
     25           const std::string& key, const std::string& value) -> Status {
     26         if (color_already_set && (key == "color_space" || key == "icc")) {
     27           JXL_WARNING("Decoder ignoring %s hint", key.c_str());
     28           return true;
     29         }
     30         if (key == "color_space") {
     31           JxlColorEncoding c_original_external;
     32           if (!ParseDescription(value, &c_original_external)) {
     33             return JXL_FAILURE("Failed to apply color_space");
     34           }
     35           ppf->color_encoding = c_original_external;
     36 
     37           if (is_gray !=
     38               (ppf->color_encoding.color_space == JXL_COLOR_SPACE_GRAY)) {
     39             return JXL_FAILURE("mismatch between file and color_space hint");
     40           }
     41 
     42           got_color_space = true;
     43         } else if (key == "icc") {
     44           const uint8_t* data = reinterpret_cast<const uint8_t*>(value.data());
     45           std::vector<uint8_t> icc(data, data + value.size());
     46           ppf->icc = std::move(icc);
     47           ppf->primary_color_representation = PackedPixelFile::kIccIsPrimary;
     48           got_color_space = true;
     49         } else if (key == "exif") {
     50           const uint8_t* data = reinterpret_cast<const uint8_t*>(value.data());
     51           std::vector<uint8_t> blob(data, data + value.size());
     52           ppf->metadata.exif = std::move(blob);
     53         } else if (key == "xmp") {
     54           const uint8_t* data = reinterpret_cast<const uint8_t*>(value.data());
     55           std::vector<uint8_t> blob(data, data + value.size());
     56           ppf->metadata.xmp = std::move(blob);
     57         } else if (key == "jumbf") {
     58           const uint8_t* data = reinterpret_cast<const uint8_t*>(value.data());
     59           std::vector<uint8_t> blob(data, data + value.size());
     60           ppf->metadata.jumbf = std::move(blob);
     61         } else {
     62           JXL_WARNING("Ignoring %s hint", key.c_str());
     63         }
     64         return true;
     65       }));
     66 
     67   if (!got_color_space) {
     68     ppf->color_encoding.color_space =
     69         is_gray ? JXL_COLOR_SPACE_GRAY : JXL_COLOR_SPACE_RGB;
     70     ppf->color_encoding.white_point = JXL_WHITE_POINT_D65;
     71     ppf->color_encoding.primaries = JXL_PRIMARIES_SRGB;
     72     ppf->color_encoding.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
     73   }
     74 
     75   return true;
     76 }
     77 
     78 }  // namespace extras
     79 }  // namespace jxl