fwt_software/include/fgc/JpegXlEncoder.h

39 lines
1.7 KiB
C++

#pragma once
#include <cstdint>
#include <string>
namespace fgc {
// Thin wrapper around libjxl for encoding an interleaved image buffer to a JPEG
// XL file. Replaces the header-only JPEG_XL.h class.
class JpegXlEncoder {
public:
// Encode `img` (1 or 3 interleaved channels) to `path`.
//
// `bits` is the bit depth of the values handed in, and is what the file
// records:
// 8 one byte per value (RGB8, Mono8, BayerRG8);
// 9..16 one native-endian 16-bit word per value holding 0..2^bits-1, e.g.
// 12 for BayerRG12p after unpacking. libjxl must be told the values
// use only `bits` bits, or it reads them as 0..65535 and rescales -
// and a "lossless" file then keeps only the top bits.
//
// `distance` is the JPEG XL frame distance: 0 = mathematically lossless,
// higher = lossier (1.0 is meant to be visually lossless). `effort` is
// libjxl's 1..9. `threads` is libjxl's worker count; 0 encodes on the
// calling thread. Returns false (and logs) on any failure; nothing is left
// at `path` unless the encode succeeded.
static bool encodeToFile(const std::string& path, const void* img, int width, int height,
int channels, int bits, float distance, int effort, int threads);
// 8-bit shorthand, as the program has always called it.
static bool encodeToFile(const std::string& path, const uint8_t* img, int width, int height,
int channels, float distance, int effort, int threads = 3) {
return encodeToFile(path, static_cast<const void*>(img), width, height, channels, 8,
distance, effort, threads);
}
};
} // namespace fgc