summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/extras/dec/color_hints.cc
blob: 53f7cd05439449fbe90c5fabbefaa5778efcdff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "lib/extras/dec/color_hints.h"

#include <jxl/encode.h>

#include "lib/extras/dec/color_description.h"
#include "lib/jxl/base/file_io.h"

namespace jxl {
namespace extras {

Status ApplyColorHints(const ColorHints& color_hints,
                       const bool color_already_set, const bool is_gray,
                       PackedPixelFile* ppf) {
  if (color_already_set) {
    return color_hints.Foreach(
        [](const std::string& key, const std::string& /*value*/) {
          JXL_WARNING("Decoder ignoring %s hint", key.c_str());
          return true;
        });
  }

  bool got_color_space = false;

  JXL_RETURN_IF_ERROR(color_hints.Foreach(
      [is_gray, ppf, &got_color_space](const std::string& key,
                                       const std::string& value) -> Status {
        if (key == "color_space") {
          JxlColorEncoding c_original_external;
          if (!ParseDescription(value, &c_original_external)) {
            return JXL_FAILURE("Failed to apply color_space");
          }
          ppf->color_encoding = c_original_external;

          if (is_gray !=
              (ppf->color_encoding.color_space == JXL_COLOR_SPACE_GRAY)) {
            return JXL_FAILURE("mismatch between file and color_space hint");
          }

          got_color_space = true;
        } else if (key == "icc_pathname") {
          JXL_RETURN_IF_ERROR(ReadFile(value, &ppf->icc));
          got_color_space = true;
        } else {
          JXL_WARNING("Ignoring %s hint", key.c_str());
        }
        return true;
      }));

  if (!got_color_space) {
    ppf->color_encoding.color_space =
        is_gray ? JXL_COLOR_SPACE_GRAY : JXL_COLOR_SPACE_RGB;
    ppf->color_encoding.white_point = JXL_WHITE_POINT_D65;
    ppf->color_encoding.primaries = JXL_PRIMARIES_SRGB;
    ppf->color_encoding.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
  }

  return true;
}

}  // namespace extras
}  // namespace jxl