Imported Upstream version 0.7.0
[platform/upstream/libjxl.git] / lib / jxl / enc_color_management.h
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_JXL_ENC_COLOR_MANAGEMENT_H_
7 #define LIB_JXL_ENC_COLOR_MANAGEMENT_H_
8
9 // ICC profiles and color space conversions.
10
11 #include <stddef.h>
12 #include <stdint.h>
13
14 #include <vector>
15
16 #include "jxl/cms_interface.h"
17 #include "lib/jxl/base/padded_bytes.h"
18 #include "lib/jxl/base/status.h"
19 #include "lib/jxl/color_encoding_internal.h"
20 #include "lib/jxl/color_management.h"
21 #include "lib/jxl/common.h"
22 #include "lib/jxl/image.h"
23
24 namespace jxl {
25
26 // Internal C++ wrapper for a JxlCmsInterface.
27 class ColorSpaceTransform {
28  public:
29   explicit ColorSpaceTransform(const JxlCmsInterface& cms) : cms_(cms) {}
30   ~ColorSpaceTransform() {
31     if (cms_data_ != nullptr) {
32       cms_.destroy(cms_data_);
33     }
34   }
35
36   // Cannot copy.
37   ColorSpaceTransform(const ColorSpaceTransform&) = delete;
38   ColorSpaceTransform& operator=(const ColorSpaceTransform&) = delete;
39
40   Status Init(const ColorEncoding& c_src, const ColorEncoding& c_dst,
41               float intensity_target, size_t xsize, size_t num_threads) {
42     xsize_ = xsize;
43     JxlColorProfile input_profile;
44     icc_src_ = c_src.ICC();
45     input_profile.icc.data = icc_src_.data();
46     input_profile.icc.size = icc_src_.size();
47     ConvertInternalToExternalColorEncoding(c_src,
48                                            &input_profile.color_encoding);
49     input_profile.num_channels = c_src.IsCMYK() ? 4 : c_src.Channels();
50     JxlColorProfile output_profile;
51     icc_dst_ = c_dst.ICC();
52     output_profile.icc.data = icc_dst_.data();
53     output_profile.icc.size = icc_dst_.size();
54     ConvertInternalToExternalColorEncoding(c_dst,
55                                            &output_profile.color_encoding);
56     if (c_dst.IsCMYK())
57       return JXL_FAILURE("Conversion to CMYK is not supported");
58     output_profile.num_channels = c_dst.Channels();
59     cms_data_ = cms_.init(cms_.init_data, num_threads, xsize, &input_profile,
60                           &output_profile, intensity_target);
61     JXL_RETURN_IF_ERROR(cms_data_ != nullptr);
62     return true;
63   }
64
65   float* BufSrc(const size_t thread) const {
66     return cms_.get_src_buf(cms_data_, thread);
67   }
68
69   float* BufDst(const size_t thread) const {
70     return cms_.get_dst_buf(cms_data_, thread);
71   }
72
73   Status Run(const size_t thread, const float* buf_src, float* buf_dst) {
74     return cms_.run(cms_data_, thread, buf_src, buf_dst, xsize_);
75   }
76
77  private:
78   JxlCmsInterface cms_;
79   void* cms_data_ = nullptr;
80   // The interface may retain pointers into these.
81   PaddedBytes icc_src_;
82   PaddedBytes icc_dst_;
83   size_t xsize_;
84 };
85
86 const JxlCmsInterface& GetJxlCms();
87
88 }  // namespace jxl
89
90 #endif  // LIB_JXL_ENC_COLOR_MANAGEMENT_H_