jxl.h (2981B)
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_JXL_H_ 7 #define LIB_EXTRAS_ENC_JXL_H_ 8 9 #include <jxl/encode.h> 10 #include <jxl/parallel_runner.h> 11 #include <jxl/thread_parallel_runner.h> 12 #include <jxl/types.h> 13 #include <stdint.h> 14 15 #include <vector> 16 17 #include "lib/extras/packed_image.h" 18 19 namespace jxl { 20 namespace extras { 21 22 struct JXLOption { 23 JXLOption(JxlEncoderFrameSettingId id, int64_t val, size_t frame_index) 24 : id(id), is_float(false), ival(val), frame_index(frame_index) {} 25 JXLOption(JxlEncoderFrameSettingId id, float val, size_t frame_index) 26 : id(id), is_float(true), fval(val), frame_index(frame_index) {} 27 28 JxlEncoderFrameSettingId id; 29 bool is_float; 30 union { 31 int64_t ival; 32 float fval; 33 }; 34 size_t frame_index; 35 }; 36 37 struct JXLCompressParams { 38 std::vector<JXLOption> options; 39 // Target butteraugli distance, 0.0 means lossless. 40 float distance = 1.0f; 41 float alpha_distance = 0.0f; 42 // If set to true, forces container mode. 43 bool use_container = false; 44 // Whether to enable/disable byte-exact jpeg reconstruction for jpeg inputs. 45 bool jpeg_store_metadata = true; 46 bool jpeg_strip_exif = false; 47 bool jpeg_strip_xmp = false; 48 bool jpeg_strip_jumbf = false; 49 // Whether to create brob boxes. 50 bool compress_boxes = true; 51 // Upper bound on the intensity level present in the image in nits (zero means 52 // that the library chooses a default). 53 float intensity_target = 0; 54 int already_downsampled = 1; 55 int upsampling_mode = -1; 56 // Overrides for bitdepth, codestream level and alpha premultiply. 57 size_t override_bitdepth = 0; 58 int32_t codestream_level = -1; 59 int32_t premultiply = -1; 60 // Override input buffer interpretation. 61 JxlBitDepth input_bitdepth = {JXL_BIT_DEPTH_FROM_PIXEL_FORMAT, 0, 0}; 62 // If runner_opaque is set, the decoder uses this parallel runner. 63 JxlParallelRunner runner = JxlThreadParallelRunner; 64 void* runner_opaque = nullptr; 65 JxlEncoderOutputProcessor output_processor = {}; 66 JxlDebugImageCallback debug_image = nullptr; 67 void* debug_image_opaque = nullptr; 68 JxlEncoderStats* stats = nullptr; 69 bool allow_expert_options = false; 70 71 void AddOption(JxlEncoderFrameSettingId id, int64_t val) { 72 options.emplace_back(id, val, 0); 73 } 74 void AddFloatOption(JxlEncoderFrameSettingId id, float val) { 75 options.emplace_back(id, val, 0); 76 } 77 bool HasOutputProcessor() const { 78 return (output_processor.get_buffer != nullptr && 79 output_processor.release_buffer != nullptr && 80 output_processor.set_finalized_position != nullptr); 81 } 82 }; 83 84 bool EncodeImageJXL(const JXLCompressParams& params, const PackedPixelFile& ppf, 85 const std::vector<uint8_t>* jpeg_bytes, 86 std::vector<uint8_t>* compressed); 87 88 } // namespace extras 89 } // namespace jxl 90 91 #endif // LIB_EXTRAS_ENC_JXL_H_