summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/color_encoding_internal.cc
blob: 895faaa07d14d24fce402760986bb7fa31d68332 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// 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/jxl/color_encoding_internal.h"

#include <array>

#include "lib/jxl/base/common.h"
#include "lib/jxl/cms/color_encoding_cms.h"
#include "lib/jxl/cms/jxl_cms_internal.h"
#include "lib/jxl/fields.h"
#include "lib/jxl/pack_signed.h"

namespace jxl {

bool CustomTransferFunction::SetImplicit() {
  if (nonserialized_color_space == ColorSpace::kXYB) {
    if (!storage_.SetGamma(1.0 / 3)) JXL_ASSERT(false);
    return true;
  }
  return false;
}

std::array<ColorEncoding, 2> ColorEncoding::CreateC2(Primaries pr,
                                                     TransferFunction tf) {
  std::array<ColorEncoding, 2> c2;

  ColorEncoding* c_rgb = c2.data() + 0;
  c_rgb->SetColorSpace(ColorSpace::kRGB);
  c_rgb->storage_.white_point = WhitePoint::kD65;
  c_rgb->storage_.primaries = pr;
  c_rgb->storage_.tf.SetTransferFunction(tf);
  JXL_CHECK(c_rgb->CreateICC());

  ColorEncoding* c_gray = c2.data() + 1;
  c_gray->SetColorSpace(ColorSpace::kGray);
  c_gray->storage_.white_point = WhitePoint::kD65;
  c_gray->storage_.primaries = pr;
  c_gray->storage_.tf.SetTransferFunction(tf);
  JXL_CHECK(c_gray->CreateICC());

  return c2;
}

const ColorEncoding& ColorEncoding::SRGB(bool is_gray) {
  static std::array<ColorEncoding, 2> c2 =
      CreateC2(Primaries::kSRGB, TransferFunction::kSRGB);
  return c2[is_gray ? 1 : 0];
}
const ColorEncoding& ColorEncoding::LinearSRGB(bool is_gray) {
  static std::array<ColorEncoding, 2> c2 =
      CreateC2(Primaries::kSRGB, TransferFunction::kLinear);
  return c2[is_gray ? 1 : 0];
}

Status ColorEncoding::SetWhitePointType(const WhitePoint& wp) {
  JXL_DASSERT(storage_.have_fields);
  storage_.white_point = wp;
  return true;
}

Status ColorEncoding::SetPrimariesType(const Primaries& p) {
  JXL_DASSERT(storage_.have_fields);
  JXL_ASSERT(HasPrimaries());
  storage_.primaries = p;
  return true;
}

void ColorEncoding::DecideIfWantICC(const JxlCmsInterface& cms) {
  if (storage_.icc.empty()) return;

  JxlColorEncoding c;
  JXL_BOOL cmyk;
  if (!cms.set_fields_from_icc(cms.set_fields_data, storage_.icc.data(),
                               storage_.icc.size(), &c, &cmyk)) {
    return;
  }
  if (cmyk) return;

  std::vector<uint8_t> icc;
  if (!MaybeCreateProfile(c, &icc)) return;

  want_icc_ = false;
}

Customxy::Customxy() { Bundle::Init(this); }
Status Customxy::VisitFields(Visitor* JXL_RESTRICT visitor) {
  uint32_t ux = PackSigned(storage_.x);
  JXL_QUIET_RETURN_IF_ERROR(visitor->U32(Bits(19), BitsOffset(19, 524288),
                                         BitsOffset(20, 1048576),
                                         BitsOffset(21, 2097152), 0, &ux));
  storage_.x = UnpackSigned(ux);
  uint32_t uy = PackSigned(storage_.y);
  JXL_QUIET_RETURN_IF_ERROR(visitor->U32(Bits(19), BitsOffset(19, 524288),
                                         BitsOffset(20, 1048576),
                                         BitsOffset(21, 2097152), 0, &uy));
  storage_.y = UnpackSigned(uy);
  return true;
}

CustomTransferFunction::CustomTransferFunction() { Bundle::Init(this); }
Status CustomTransferFunction::VisitFields(Visitor* JXL_RESTRICT visitor) {
  if (visitor->Conditional(!SetImplicit())) {
    JXL_QUIET_RETURN_IF_ERROR(visitor->Bool(false, &storage_.have_gamma));

    if (visitor->Conditional(storage_.have_gamma)) {
      // Gamma is represented as a 24-bit int, the exponent used is
      // gamma_ / 1e7. Valid values are (0, 1]. On the low end side, we also
      // limit it to kMaxGamma/1e7.
      JXL_QUIET_RETURN_IF_ERROR(visitor->Bits(
          24, ::jxl::cms::CustomTransferFunction::kGammaMul, &storage_.gamma));
      if (storage_.gamma > ::jxl::cms::CustomTransferFunction::kGammaMul ||
          static_cast<uint64_t>(storage_.gamma) *
                  ::jxl::cms::CustomTransferFunction::kMaxGamma <
              ::jxl::cms::CustomTransferFunction::kGammaMul) {
        return JXL_FAILURE("Invalid gamma %u", storage_.gamma);
      }
    }

    if (visitor->Conditional(!storage_.have_gamma)) {
      JXL_QUIET_RETURN_IF_ERROR(
          visitor->Enum(TransferFunction::kSRGB, &storage_.transfer_function));
    }
  }

  return true;
}

ColorEncoding::ColorEncoding() { Bundle::Init(this); }
Status ColorEncoding::VisitFields(Visitor* JXL_RESTRICT visitor) {
  if (visitor->AllDefault(*this, &all_default)) {
    // Overwrite all serialized fields, but not any nonserialized_*.
    visitor->SetDefault(this);
    return true;
  }

  JXL_QUIET_RETURN_IF_ERROR(visitor->Bool(false, &want_icc_));

  // Always send even if want_icc_ because this affects decoding.
  // We can skip the white point/primaries because they do not.
  JXL_QUIET_RETURN_IF_ERROR(
      visitor->Enum(ColorSpace::kRGB, &storage_.color_space));

  if (visitor->Conditional(!WantICC())) {
    // Serialize enums. NOTE: we set the defaults to the most common values so
    // ImageMetadata.all_default is true in the common case.

    if (visitor->Conditional(!ImplicitWhitePoint())) {
      JXL_QUIET_RETURN_IF_ERROR(
          visitor->Enum(WhitePoint::kD65, &storage_.white_point));
      if (visitor->Conditional(storage_.white_point == WhitePoint::kCustom)) {
        white_.storage_ = storage_.white;
        JXL_QUIET_RETURN_IF_ERROR(visitor->VisitNested(&white_));
        storage_.white = white_.storage_;
      }
    }

    if (visitor->Conditional(HasPrimaries())) {
      JXL_QUIET_RETURN_IF_ERROR(
          visitor->Enum(Primaries::kSRGB, &storage_.primaries));
      if (visitor->Conditional(storage_.primaries == Primaries::kCustom)) {
        red_.storage_ = storage_.red;
        JXL_QUIET_RETURN_IF_ERROR(visitor->VisitNested(&red_));
        storage_.red = red_.storage_;
        green_.storage_ = storage_.green;
        JXL_QUIET_RETURN_IF_ERROR(visitor->VisitNested(&green_));
        storage_.green = green_.storage_;
        blue_.storage_ = storage_.blue;
        JXL_QUIET_RETURN_IF_ERROR(visitor->VisitNested(&blue_));
        storage_.blue = blue_.storage_;
      }
    }

    tf_.nonserialized_color_space = storage_.color_space;
    tf_.storage_ = storage_.tf;
    JXL_QUIET_RETURN_IF_ERROR(visitor->VisitNested(&tf_));
    storage_.tf = tf_.storage_;

    JXL_QUIET_RETURN_IF_ERROR(
        visitor->Enum(RenderingIntent::kRelative, &storage_.rendering_intent));

    // We didn't have ICC, so all fields should be known.
    if (storage_.color_space == ColorSpace::kUnknown ||
        storage_.tf.IsUnknown()) {
      return JXL_FAILURE(
          "No ICC but cs %u and tf %u%s",
          static_cast<unsigned int>(storage_.color_space),
          storage_.tf.have_gamma
              ? 0
              : static_cast<unsigned int>(storage_.tf.transfer_function),
          storage_.tf.have_gamma ? "(gamma)" : "");
    }

    JXL_RETURN_IF_ERROR(CreateICC());
  }

  if (WantICC() && visitor->IsReading()) {
    // Haven't called SetICC() yet, do nothing.
  } else {
    if (ICC().empty()) return JXL_FAILURE("Empty ICC");
  }

  return true;
}

}  // namespace jxl