encode.h (2699B)
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_EXTRAS_ENC_ENCODE_H_ 7 #define LIB_EXTRAS_ENC_ENCODE_H_ 8 9 // Facade for image encoders. 10 11 #include <jxl/codestream_header.h> 12 #include <jxl/types.h> 13 14 #include <cstdint> 15 #include <memory> 16 #include <string> 17 #include <unordered_map> 18 #include <utility> 19 #include <vector> 20 21 #include "lib/extras/packed_image.h" 22 #include "lib/jxl/base/data_parallel.h" 23 #include "lib/jxl/base/status.h" 24 25 namespace jxl { 26 namespace extras { 27 28 struct EncodedImage { 29 // One (if the format supports animations or the image has only one frame) or 30 // more 1quential bitstreams. 31 std::vector<std::vector<uint8_t>> bitstreams; 32 33 // For each extra channel one or more sequential bitstreams. 34 std::vector<std::vector<std::vector<uint8_t>>> extra_channel_bitstreams; 35 36 std::vector<uint8_t> preview_bitstream; 37 38 // If the format does not support embedding color profiles into the bitstreams 39 // above, it will be present here, to be written as a separate file. If it 40 // does support them, this field will be empty. 41 std::vector<uint8_t> icc; 42 43 // Additional output for conformance testing, only filled in by NumPyEncoder. 44 std::vector<uint8_t> metadata; 45 }; 46 47 class Encoder { 48 public: 49 static std::unique_ptr<Encoder> FromExtension(std::string extension); 50 51 virtual ~Encoder() = default; 52 53 // Set of pixel formats that this encoder takes as input. 54 // If empty, the 'encoder' does not need any pixels (it's metadata-only). 55 virtual std::vector<JxlPixelFormat> AcceptedFormats() const = 0; 56 57 // Any existing data in encoded_image is discarded. 58 virtual Status Encode(const PackedPixelFile& ppf, EncodedImage* encoded_image, 59 ThreadPool* pool) const = 0; 60 61 void SetOption(std::string name, std::string value) { 62 options_[std::move(name)] = std::move(value); 63 } 64 65 static Status VerifyBasicInfo(const JxlBasicInfo& info); 66 static Status VerifyImageSize(const PackedImage& image, 67 const JxlBasicInfo& info); 68 static Status VerifyBitDepth(JxlDataType data_type, uint32_t bits_per_sample, 69 uint32_t exponent_bits); 70 71 protected: 72 const std::unordered_map<std::string, std::string>& options() const { 73 return options_; 74 } 75 76 Status VerifyFormat(const JxlPixelFormat& format) const; 77 78 Status VerifyPackedImage(const PackedImage& image, 79 const JxlBasicInfo& info) const; 80 81 private: 82 std::unordered_map<std::string, std::string> options_; 83 }; 84 85 } // namespace extras 86 } // namespace jxl 87 88 #endif // LIB_EXTRAS_ENC_ENCODE_H_