JFLib

img_jxl#

JPEG XL -- crate root lib/img_jxl/lib.rs

img_jxl -- JPEG XL reader (ISO/IEC 18181) and writer (lossless modular, lossy VarDCT)
                                                          [src/img/jxl.s, jxl_*.s]

  CODEC                         the codec descriptor (jxl_codec)
  probe(data, meta) -> bool     jxl_probe
  decode(data, sf, dec, meta) -> 0 / ERR_*   jxl_decode: displayed frame dec.frame
                                (ERR_LIMIT past the last) as FMT_ARGB, 8 bits; meta.delay of
                                that frame; ICC, EXIF (Exif box), XMP (xml box) blobs;
                                ERR_UNSUPPORTED: float extra channels, PQ / HLG / 709
                                output.  jxl-plus: DC frames (progressive DC); the groups of
                                a frame decode on the worker pool (same pixels).  jxl-tools:
                                splines (spline.rs: libjxl 0.7's rendering, same pixels)
  open(data) -> Dec / ERR_*     jx_open: codestream copy (bare FF 0A or the box container:
                                jxlc / jxlp), image headers
  jx_frames(jx, br) -> u32      displayed frames (walks the frame headers)
  jx_meta(jx, br, meta)         image properties into an ImgMeta
  enc::encode(sf, opt, out) -> 0 / ERR_*   jxl_encode: the jxl-plus writer (crate img_jxl_enc:
                                enew.rs lossless modular, evdct.rs lossy VarDCT, options
                                below) when the app has one (writer.rs, jxl-module: linked in
                                with img_jxl_enc::use_static(), or the module file jxlenc.dll /
                                .so with writer::use_module); with IOF_JXL_COMPAT, without a
                                new writer or without the module file the assembly's writer
                                (enc.rs / venc.rs)
  jpeg::reconstruct(data) -> JPEG bytes / ERR_*   jxl-plus: the original JPEG file of a JPEG
                                transcode (jbrd box; byte-identical to libjxl's); jpeg::has_jbrd
  box_contents(data, box)       jxl-plus: an Exif / xml box (Brotli decompressed when it is a
                                brob box: libjxl's JPEG transcodes; decode reads them as EXIF /
                                XMP too)

Writer options (ImgOpt) -- the save dialog's controls                        [jxl-plus]
  quality   1..99 lossy (VarDCT), 100 lossless; 0 = 90.  Butteraugli distance as libjxl:
            q >= 30: 0.1 + (100 - q) * 0.09, else 53 / 3000 q^2 - 23 / 20 q + 25
  effort    1..9 like libjxl's (0 = 7, 1 works like 2); higher = smaller, slower.  What
            each effort adds:
              lossless  1-2 gradient predictor, fixed tree; 3 weighted predictor, fixed
                        tree; 1-4 YCoCg; 4+ learned MA tree (both predictors); 5+ local
                        palettes, RCT search per group, patches (jxl-tools: repeated
                        text glyphs / UI pieces in a reference frame; 9 keeps the smaller
                        file with / without); 8+ WP parameter search, LZ77, more tree
                        properties
              lossy     1-3 DCT8, one quant value; 4 near-lossless DC, block context
                        model, own coefficient orders; 5 adaptive quantization, AC
                        strategy search (DCT2 .. DCT64, identity), chroma from luma,
                        gaborish; 6 DCT4x8 / AFV, unaligned blocks; 7 (default) better
                        CfL, chroma scaling from the pixels, patches (jxl-tools; from
                        distance 3 also dots when there are no text-like patches); 8
                        learned DC / metadata trees, the Butteraugli quantization loop
                        (jxl-tools: 2 round trips of the frame, libjxl's
                        FindBestQuantization); 9 LZ77, 4 round trips
  flags     IOF_LOSSLESS          lossless whatever the quality
            IOF_EMBED_ICC         write meta.icc (RGB / gray profiles).  Lossy: an sRGB
                                  profile is written as plain sRGB; other profiles keep
                                  their values (coded as YCbCr, no XYB without colour
                                  management) and the profile
            IOF_EMBED_EXIF / _XMP meta.exif / meta.xmp as Exif / xml boxes (container)
            IOF_JXL_PROGRESSIVE   0x400000 (enew.rs): lossy: a 1:8 DC frame and three AC
                                  passes; lossless: squeeze (coarse to fine).  A few %
                                  larger
            IOF_JXL_1THREAD       0x200000: no worker threads (the same file, slower)
            IOF_JXL_COMPAT        0x100000: the assembly-compatible writer (byte-identical
                                  to the asm; quality / lossless / EXIF / XMP only)
            IOF_JXL_NOPATCHES     0x800000 (jxl-tools): no patches at any effort
            bits 24..31           (jxl-tools, IOF_JXL_NOISE_SHIFT / _MASK) photon noise:
                                  ISO / 100 (0 = off, the default; 16 = ISO 1600): lossy XYB
                                  frames carry libjxl's noise LUT for that ISO (cjxl
                                  --photon_noise_iso), the decoder adds the grain; lossless
                                  and YCbCr frames ignore it
  cancel    polled between the stages: ERR_CANCEL
The file never depends on the number of threads.  Extra channels other than alpha, more
than 8 bits, animation: not written; splines only by the test-only espline.rs (codectest
op 197).

Modules (one per assembly file): bits (jxl_bits.s), hdr (jxl_hdr.s), icc (jxl_icc.s), ent
(jxl_ent.s), frm (jxl_frm.s), dec (jxl_dec.s), modular (jxl_mod.s), mtr (jxl_mtr.s), vdc
(jxl_vdc.s), vac (jxl_vac.s), vqt (jxl_vqt.s), vrc (jxl_vrc.s), dct (jxl_dct.s), rnd
(jxl_rnd.s), epf (jxl_epf.s), blend (jxl_blend.s), noise (jxl_noise.s), tab (jxl_tab.s,
generated by tools/gen_jxl.py); the writer: ewr (jxl_ewr.s), enc (jxl_enc.s), venc
(jxl_venc.s).  jxl-plus (no asm): emath (libjxl's fast math), jpeg (JPEG reconstruction) with
brotli (the call of compress::brotli, the RFC 7932 decoder that lived here until 0.2),
par (the pool helpers).  jxl-tools (no asm): spline (decoder:
splines).  jxl-module: writer (the new writer's options, samples and hook).  The new writer
itself (libjxl 0.11.1's encoder: enew, evdct, emod, ema, eans, efrm, eicc, etx, dctk, eaq,
eacs, ecfl, epatch, edots, espline, enoise, efeat, ebutter, ebq) is the crate img_jxl_enc
(lib/img_jxl_enc/lib.rs), linked into codectest and built into the module jxlenc
(modules/jxlenc) that codectest --jxlenc loads; Recon and decode_linear below are its reconstruction
helpers.

img_jxl API#

  • struct Dec an opened file: the codestream copy (with JXB_PAD zero bytes) and the decoder record
    • cs: Vec<u8>
    • jx: Jx

impl Dec

  • fn reader(&self) -> Jb<'_> the header reader at the first frame header

  • fn box_contents(data: &[u8], b: (usize, usize, bool)) -> Result<Vec<u8>, i32> the contents of a metadata box (jxl-plus): (start, size, brob) from Jx.exifbox / xmlbox; a brob box is Brotli decompressed

  • fn open(data: &[u8]) -> Result<Dec, i32> jx_open: codestream copy and image headers

  • fn jx_frames(jx: &Jx, br: &mut Jb) -> u32 jx_frames: displayed frames (0 on a damaged frame list); br is the header reader

  • fn jx_meta(jx: &Jx, br: &mut Jb, m: &mut ImgMeta) jx_meta: image properties into meta

  • fn probe(data: &[u8], meta: &mut ImgMeta) -> bool jxl_probe

  • fn decode(data: &[u8], sf: &mut Surface, dec: &ImgDec, meta: &mut ImgMeta) -> i32 jxl_decode (see the module header)

  • fn decode_opt(data: &[u8], sf: &mut Surface, dec: &ImgDec, meta: Option<&mut ImgMeta>) -> i32 jxl_decode with an optional ImgMeta (the assembly's ID_META may be 0: then the image properties are not read, which leaves the header reader's state alone)

  • struct Recon the writer's reconstruction for its EPF sharpness search (jxl-plus; img_jxl_enc): the displayed frame decoded once up to the EPF; planes(sharpness) runs the EPF with that sharpness per block on a copy

impl Recon

  • fn new(cs: &[u8]) -> Result<Recon, i32>

  • fn planes(&mut self, sharp: &[u8]) -> Result<(u32, u32, usize, [&[f32]; 3]), i32> (width, height, row stride, X / Y / B) after the EPF with this sharpness per block (jxl-speed: the padding of the frame for the first stage is made once, the output planes are reused: every sample the EPF writes is rewritten each call, the rest is the frame's as in a fresh copy)

  • fn decode_linear(cs: &[u8], w: usize, h: usize) -> Result<[Vec<f32>; 3], i32> the writer's Butteraugli round trips (jxl-tools): the shown frame of a codestream of the writer (XYB, no blending) decoded to linear sRGB planes of w x h

  • static CODEC: Codec the codec descriptor [jxl_codec]

From lib/img_jxl/jpeg.rs (img_jxl::jpeg):

  • fn has_jbrd(data: &[u8]) -> bool has_jbrd: whether data is a container with a JPEG reconstruction box

  • fn reconstruct(data: &[u8]) -> Result<Vec<u8>, i32> reconstruct: the JPEG file of a JPEG transcode (see the module header)

From lib/img_jxl/writer.rs (img_jxl::writer):

/// IO_FLAGS bit: the assembly-compatible writer (byte-identical to the asm reference)
IOF_JXL_COMPAT: u32 = 0x10_0000
/// IO_FLAGS bit: no worker threads (the output is the same either way)
IOF_JXL_1THREAD: u32 = 0x20_0000
/// IO_FLAGS bit: progressive (VarDCT: a DC frame + 3 AC passes; lossless: squeeze, responsive)
IOF_JXL_PROGRESSIVE: u32 = 0x40_0000
/// IO_FLAGS bit (jxl-tools): no patches (libjxl's patches = 0; for comparisons)
IOF_JXL_NOPATCHES: u32 = 0x80_0000
/// IO_FLAGS bits 24..31 (jxl-tools): photon noise ISO / 100 (lossy only; 0 = off)
IOF_JXL_NOISE_SHIFT: u32 = 24
/// the mask of the photon noise bits (jxl-tools)
IOF_JXL_NOISE_MASK: u32 = 0xFF00_0000
  • struct Opts<'a> the encoder options
    • effort: u32
    • quality: u32
    • flags: u32
    • icc: Option<&'a [u8]>
    • exif: Option<&'a [u8]>
    • xmp: Option<&'a [u8]>
    • cancel: Option<&'a AtomicU32>
    • splines: Option<&'a crate::spline::Quantized> -- jxl-tools: the test-only spline writer

impl<'a> Opts<'a>

  • fn from_imgopt(opt: &ImgOpt<'a>) -> Opts<'a> the options of an ImgOpt: the ICC profile with IOF_EMBED_ICC, EXIF / XMP when present (the container writer looks at IOF_EMBED_EXIF / _XMP)

  • fn effort(&self) -> u32 the effort 1..9 (0 = 7)

  • fn threads(&self) -> bool worker threads allowed (no IOF_JXL_1THREAD)

  • fn lossless(&self) -> bool lossless: IOF_LOSSLESS or quality 100 and more

  • fn cancelled(&self) -> bool the cancel flag is set

  • fn noise_iso(&self) -> u32 the photon noise ISO (0 = none) [jxl-tools]

  • fn patches(&self, lossless: bool) -> bool patches allowed at this effort (libjxl: lossless 5+, lossy 7+) [jxl-tools]

  • enum Kind which writer saves JPEG XL files without IOF_JXL_COMPAT

    • Static -- the writer linked into the app (img_jxl_enc)
    • Module -- the loadable module (jxlenc.dll / jxlenc.so next to the exe)
    • Compat -- none: the assembly-compatible writer
  • fn set_static(f: WriterFn) the writer linked into the app (img_jxl_enc::use_static() installs enew::encode_px)

  • fn use_module(path: Option<&str>) the writer of the module file: None = <exe folder>/jxlenc.dll (.so), Some(path) = that file, Some("off") = none (the assembly-compatible writer; tests). Loaded on first use.

  • fn kind() -> Kind which writer saves (Module only when the module file loads: this loads it)

  • fn write(sf: &Surface, o: &Opts) -> Option<Result<Vec<u8>, i32>> the new writer on sf: Some(file / ERR_*), None when there is none (no writer installed, or the module file is missing / refused)

img_jxl modules#

The codec's building blocks (public for the codec test app codectest; see their source headers):

ModuleContent
img_jxl::bitsJPEG XL bit reader and field coding (fields.cc; model: tools/jxl_ref.py Bits)
img_jxl::blendJPEG XL reference frames, frame blending and patches (blending.cc, stage_blending.cc, dec_patch_dictionary.cc; model: tools/jxl_ref.py perform_blending, ...
img_jxl::brotliBrotli decompression (RFC 7932) of the JPEG reconstruction data (jbrd box) of JPEG transcodes, with img_core's error codes
img_jxl::dctJPEG XL VarDCT transforms (dct-inl.h IDCT1DImpl / DCT1DImpl, dec_transforms-inl.h TransformToPixels / LowestFrequenciesFromDC; model: tools/jxl_ref.py ...
img_jxl::decJPEG XL frame decoding driver (dec_frame.cc, dec_modular.cc; model: tools/jxl_ref.py Frame.decode, decode_global_modular, decode_group, pass_bracket)
img_jxl::emathfloat helpers of the new encoder (libjxl base/fast_math-inl.h, rational_polynomial-inl.h)
img_jxl::encJPEG XL writer, lossless modular (model: tools/jxl_enc_ref.py encode_lossless; byte-exact)
img_jxl::entJPEG XL entropy decoding (dec_ans.cc, ans_common.cc, dec_huffman.cc, dec_context_map.cc; model: tools/jxl_ref.py read_histogram .. Reader)
img_jxl::epfJPEG XL edge preserving filter (stage_epf.cc; model: tools/jxl_ref.py epf0, epf1, epf2, epf_sigma_pixels, epf_weight, epf_finish)
img_jxl::ewrJPEG XL writer: bit writer, hybrid uint tokens, length-limited Huffman codes, prefix codes and histogram sets (model: tools/jxl_enc_ref.py BW, hybrid_token, ...
img_jxl::frmJPEG XL frame header, loop filter, TOC (frame_header.cc, loop_filter.cc, toc.cc; model: tools/jxl_ref.py Blending, LoopFilter, FrameHeader, read_toc)
img_jxl::hdrJPEG XL image headers (headers.cc, image_metadata.cc, color_encoding_internal.cc; model: tools/jxl_ref.py SizeHeader .. TransformData, read_headers)
img_jxl::iccJPEG XL ICC profile decoding (icc_codec.cc, icc_codec_common.cc; model: tools/jxl_ref.py read_icc, unpredict_icc)
img_jxl::jpegJPEG reconstruction: the original JPEG file of a JPEG XL JPEG transcode, from its jbrd box and the quantized DCT coefficients of its VarDCT frame (libjxl ...
img_jxl::modularJPEG XL modular image decoding (modular/encoding/*.cc; model: tools/jxl_ref.py WPHeader .. decode_channel)
img_jxl::mtrJPEG XL modular inverse transforms (modular/transform/rct.cc, palette.cc, squeeze.cc; model: tools/jxl_ref.py ModImage.undo_transforms, inv_rct, ...
img_jxl::noiseJPEG XL noise synthesis (dec_noise.cc, stage_noise.cc; model: tools/jxl_ref.py Xorshift128Plus, random_rect, noise_planes, convolve_noise, noise_strength, ...
img_jxl::parthe worker-pool helpers of the JPEG XL reader and writer (jxl-plus / jxl-speed; moved out of the writer's emod.rs by jxl-module: the decoder uses them too)
img_jxl::rndJPEG XL render pipeline (render_pipeline/stage_*.cc; model: tools/jxl_ref.py Frame.render, modular_color, ec_planes, chroma_up_h/v, gaborish, upsample, ...
img_jxl::splineJPEG XL splines: the bundle of a frame's LfGlobal section and their rendering (libjxl 0.7 splines.cc: Splines::Decode, QuantizedSpline::Decode / Dequantize, ...
img_jxl::tabJPEG XL tables (GENERATED by tools/gen_jxl.py from ASMLIB/src/img/jxl_tab.s, which tools/jxl_gen.py derives from the model tools/jxl_ref.py; do not edit)
img_jxl::vacJPEG XL VarDCT AC side: quantization encodings, coefficient orders, AC histograms and coefficients (quant_weights.cc Decode, coeff_order.cc, dec_group.cc ...
img_jxl::vdcJPEG XL VarDCT DC side (quantizer.cc, entropy_coder.cc DecodeBlockCtxMap, chroma_from_luma.cc, dec_modular.cc DecodeVarDCTDC / DecodeAcMetadata, ...
img_jxl::vencJPEG XL writer, lossy VarDCT (model: tools/jxl_enc_ref.py encode_vardct; byte-exact)
img_jxl::vqtJPEG XL dequantization tables (quant_weights.cc ComputeQuantTable, GetQuantWeights; fast_math-inl.h FastPowf; model: tools/jxl_ref.py quant_weights, ...
img_jxl::vrcJPEG XL VarDCT reconstruction of a group: dequantization with colour correlation, LLF from the DC image and inverse transforms into the pixel planes ...
img_jxl::writerthe interface of the new JPEG XL writer (jxl-plus) as the decoder crate sees it: its options, the image samples it reads, and which writer runs (jxl-module)

Source: docs/API.md in the JFLib repository.