// 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 #include "lib/extras/codec.h" #include "lib/jxl/base/span.h" #include "lib/jxl/codec_in_out.h" #include "lib/jxl/dec_file.h" #include "lib/jxl/enc_cache.h" #include "lib/jxl/enc_color_management.h" #include "lib/jxl/enc_file.h" extern "C" { /* NOTA BENE: see file history to uncover how to decode HDR JPEGs to pixels. */ /** Result: uint32_t 'size' followed by compressed image (JXL). */ uint8_t* jxlCompress(const uint8_t* data, size_t size) { jxl::PaddedBytes compressed; jxl::CodecInOut io; jxl::extras::Codec input_codec; if (!jxl::SetFromBytes(jxl::Span(data, size), &io, nullptr, &input_codec)) { return nullptr; } jxl::CompressParams params; jxl::PassesEncoderState passes_encoder_state; if (!jxl::EncodeFile(params, &io, &passes_encoder_state, &compressed, jxl::GetJxlCms(), nullptr, nullptr)) { return nullptr; } uint32_t compressed_size = compressed.size(); uint8_t* result = reinterpret_cast(malloc(compressed_size + 4)); memcpy(result, &compressed_size, sizeof(compressed_size)); memcpy(result + 4, compressed.data(), compressed_size); return result; } /** Result: uint32_t 'size' followed by decompressed image (JPG). */ uint8_t* jxlDecompress(const uint8_t* data, size_t size) { std::vector decompressed; jxl::CodecInOut io; jxl::DecompressParams params; if (!jxl::DecodeFile(params, jxl::Span(data, size), &io, nullptr)) { return nullptr; } io.use_sjpeg = false; io.jpeg_quality = 100; if (!jxl::Encode(io, jxl::extras::Codec::kJPG, io.Main().c_current(), 8, &decompressed, nullptr)) { return nullptr; } uint32_t decompressed_size = decompressed.size(); uint8_t* result = reinterpret_cast(malloc(decompressed_size + 4)); memcpy(result, &decompressed_size, sizeof(decompressed_size)); memcpy(result + 4, decompressed.data(), decompressed_size); return result; } } // extern "C"