summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/extras/enc/encode.h
blob: 63cabaf30f986fc967bea0a6f6e2484fc3d98d20 (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
// 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.

#ifndef LIB_EXTRAS_ENC_ENCODE_H_
#define LIB_EXTRAS_ENC_ENCODE_H_

// Facade for image encoders.

#include <string>
#include <unordered_map>

#include "lib/extras/dec/decode.h"
#include "lib/jxl/base/data_parallel.h"
#include "lib/jxl/base/status.h"

namespace jxl {
namespace extras {

struct EncodedImage {
  // One (if the format supports animations or the image has only one frame) or
  // more sequential bitstreams.
  std::vector<std::vector<uint8_t>> bitstreams;

  // For each extra channel one or more sequential bitstreams.
  std::vector<std::vector<std::vector<uint8_t>>> extra_channel_bitstreams;

  std::vector<uint8_t> preview_bitstream;

  // If the format does not support embedding color profiles into the bitstreams
  // above, it will be present here, to be written as a separate file. If it
  // does support them, this field will be empty.
  std::vector<uint8_t> icc;

  // Additional output for conformance testing, only filled in by NumPyEncoder.
  std::vector<uint8_t> metadata;
};

class Encoder {
 public:
  static std::unique_ptr<Encoder> FromExtension(std::string extension);

  virtual ~Encoder() = default;

  virtual std::vector<JxlPixelFormat> AcceptedFormats() const = 0;

  // Any existing data in encoded_image is discarded.
  virtual Status Encode(const PackedPixelFile& ppf, EncodedImage* encoded_image,
                        ThreadPool* pool = nullptr) const = 0;

  void SetOption(std::string name, std::string value) {
    options_[std::move(name)] = std::move(value);
  }

  static Status VerifyBasicInfo(const JxlBasicInfo& info);
  static Status VerifyImageSize(const PackedImage& image,
                                const JxlBasicInfo& info);
  static Status VerifyBitDepth(JxlDataType data_type, uint32_t bits_per_sample,
                               uint32_t exponent_bits);

 protected:
  const std::unordered_map<std::string, std::string>& options() const {
    return options_;
  }

  Status VerifyFormat(const JxlPixelFormat& format) const;

  Status VerifyPackedImage(const PackedImage& image,
                           const JxlBasicInfo& info) const;

 private:
  std::unordered_map<std::string, std::string> options_;
};

// TODO(sboukortt): consider exposing this as part of the C API.
Status SelectFormat(const std::vector<JxlPixelFormat>& accepted_formats,
                    const JxlBasicInfo& basic_info, JxlPixelFormat* format);

}  // namespace extras
}  // namespace jxl

#endif  // LIB_EXTRAS_ENC_ENCODE_H_