jixel 0.5.12

JPEG XL encoder
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
 * // Copyright (c) Radzivon Bartoshyk 5/2026. All rights reserved.
 * //
 * // Redistribution and use in source and binary forms, with or without modification,
 * // are permitted provided that the following conditions are met:
 * //
 * // 1.  Redistributions of source code must retain the above copyright notice, this
 * // list of conditions and the following disclaimer.
 * //
 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
 * // this list of conditions and the following disclaimer in the documentation
 * // and/or other materials provided with the distribution.
 * //
 * // 3.  Neither the name of the copyright holder nor the names of its
 * // contributors may be used to endorse or promote products derived from
 * // this software without specific prior written permission.
 * //
 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

//! One lossless frame, coded step by step.
//!
//! 1. Palette transform: an image of few colors (per frame or per group)
//!    codes as a palette and an index channel and is done.
//! 2. Squeeze: a progressive frame codes its Squeeze pyramid and is done.
//! 3. Color transform: the RCTs are ranked; YCoCg stays unless one wins.
//! 4. Weighted-predictor parameters for the chosen transform.
//! 5. Learned trees ([`trees`]): the RGB planes, a global palette and
//!    per-group palettes each grow a tree and write a complete frame. A tree
//!    estimated to win clearly ends the search here.
//! 6. Per-channel predictors for the remaining candidates.
//! 7. The v1 context tree (WP-activity contexts) writes its frame.
//! 8. The flat coder ([`flat`]) writes its frame.
//!
//! Below Slow only the flat frame is written; otherwise every written frame
//! competes on its complete byte-aligned size.

mod flat;
mod trees;

use super::context::{try_encode_context_tree_multi_group, try_encode_context_tree_single_group};
use super::frame::keep_smaller_writer;
use super::palette::{try_encode_local_palette_multi_group, try_encode_palette_single_group};
use super::predictor::{
    GradPackInteriorFn, choose_predictors_with_wp, choose_wp_params, fixed_predictor,
    selected_grad_pack_interior_fn,
};
use super::rct::{rank_rcts, rct_candidates, rct_planes};
use super::squeeze::{encode_squeeze_multigroup, encode_squeeze_single_group};
use super::{GroupGrid, GroupLayout, LosslessSettings, constant_alpha_value, lossless_min_symbol};
use crate::bit_writer::BitWriter;
use crate::coder_scratch::CoderScratch;
use crate::effort::Effort;
use crate::encode_image::AlphaPlane;
use crate::image::Image3Si;
use crate::patches::ModularFrameKind;
use crate::thread_pool::ThreadPool;
use crate::weighted_predictor::WpParams;
use flat::FlatCoder;

/// The learned-tree entropy proxy must beat its flat alternative decisively
/// before Slow mode trusts it enough to omit the context/flat final encodes.
/// Near decisions retain the exact byte-level tournament.
pub(super) const MA_DECISIVE_MIN_SAVINGS: f64 = 0.18;

/// Row steps of the RCT ranking under `rct_dense_rows` and below it.
const RCT_DENSE_ROW_STEP: usize = 4;

const RCT_SPARSE_ROW_STEP: usize = 16;

#[inline]
pub(super) fn learned_tree_is_decisive(estimated_savings: f64) -> bool {
    estimated_savings >= MA_DECISIVE_MIN_SAVINGS
}

#[derive(Clone, Copy)]
pub(super) enum TreeSearch {
    /// Include a second, per-group tree when its rate estimate qualifies.
    Full,
    /// Use the frame-wide learned trees only.
    Global,
    /// Use frame-wide trees and rank the vector palette before the full race.
    Vector,
}

/// Codes one lossless frame of `linear` (YCoCg planes, or one gray plane)
/// and `alpha`. Returns whether a learned tree won decisively, which tells
/// the caller a tile-patch plan cannot beat this frame.
pub(crate) fn encode_frame_lossless_core(
    linear: &Image3Si,
    alpha: Option<&AlphaPlane>,
    settings: LosslessSettings,
    frame_kind: ModularFrameKind<'_>,
    local_trees: bool,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    writer: &mut BitWriter,
) -> bool {
    encode_frame_lossless_core_impl(
        linear,
        alpha,
        settings,
        frame_kind,
        true,
        if local_trees {
            TreeSearch::Full
        } else {
            TreeSearch::Global
        },
        pool,
        scratch,
        writer,
    )
}

/// [`encode_frame_lossless_core`], with the palette-transform step
/// optional: the step's own size comparison codes the frame without it.
#[allow(clippy::too_many_arguments)]
pub(super) fn encode_frame_lossless_core_impl(
    linear: &Image3Si,
    alpha: Option<&AlphaPlane>,
    settings: LosslessSettings,
    frame_kind: ModularFrameKind<'_>,
    allow_palette_candidates: bool,
    tree_search: TreeSearch,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    writer: &mut BitWriter,
) -> bool {
    let frame = CoreFrame::new(linear, alpha, settings, frame_kind, tree_search);

    // Step 1: a few-color image codes as a palette and an index channel.
    if allow_palette_candidates
        && let Some(decisive) = frame.encode_palette_transform(pool, scratch, writer)
    {
        return decisive;
    }

    // Step 2: a progressive frame codes its Squeeze pyramid. A multi-group
    // frame whose squeezed channels outgrow the global stream falls through
    // to the non-progressive path.
    if frame.encode_squeeze(pool, scratch, writer) {
        return false;
    }

    // Step 3: the color transform.
    let mut color = ColorTransform::rank(&frame, pool, scratch);

    // Step 4: the Weighted Predictor's parameters under that transform.
    let wp_params = frame.choose_wp_params(color.rct_type, pool, scratch);

    // Step 5: learned-tree candidates. Each one written is a complete frame;
    // the smallest so far is kept.
    let mut best = SmallestFrame::default();
    let trees =
        trees::learn_tree_candidates(&frame, &mut color, wp_params, &mut best, pool, scratch);
    if trees.decisive || trees.skip_alternatives {
        writer.append(best.get().expect("decisive learned-tree candidate"));
        return trees.decisive;
    }

    // Step 6: per-channel predictors for the v1 and flat candidates, under
    // the learned tree's group layout (deferred: a decisive tree never
    // needs them).
    let layout = trees.layout;
    let grid = GroupGrid::new(frame.xsize, frame.ysize, layout);
    let rct_type = color.rct_type;
    let planes = color.planes(pool, scratch);
    let predictors = if frame.effort.predictor_search {
        choose_predictors_with_wp(
            planes,
            frame.alpha.filter(|_| frame.alpha_constant.is_none()),
            frame.xsize,
            frame.ysize,
            frame.num_color,
            pool,
            scratch,
            trees.wp_params,
            frame.use_wp,
        )
    } else {
        [fixed_predictor(frame.use_wp); 4]
    };
    let flat = FlatCoder::new(
        &frame,
        planes,
        rct_type,
        layout,
        &predictors,
        trees.wp_params,
    );

    // Step 7: the v1 context tree splits each channel's context on the WP
    // activity property; a big win on smooth+edge content.
    if frame.compare_tree_candidates && frame.effort.tree_ctx_v1 && frame.use_wp {
        let mut candidate = BitWriter::new();
        let written = if grid.single_group() {
            try_encode_context_tree_single_group(
                frame.settings.speed,
                frame.num_color,
                planes,
                frame.alpha,
                frame.alpha_constant,
                frame.xsize,
                frame.ysize,
                layout,
                frame.kind,
                &predictors,
                frame.min_symbol,
                rct_type,
                pool,
                scratch,
                trees.wp_params,
                &mut candidate,
            )
        } else {
            try_encode_context_tree_multi_group(
                frame.settings.speed,
                frame.num_color,
                planes,
                frame.alpha,
                frame.alpha_constant,
                frame.xsize,
                frame.ysize,
                layout,
                frame.kind,
                &predictors,
                grid.x_groups,
                grid.y_groups,
                grid.num_dc_groups(),
                frame.min_symbol,
                rct_type,
                pool,
                scratch,
                trees.wp_params,
                &mut candidate,
            )
        };
        if written {
            best.offer(candidate);
        }
    }

    // Step 8: the flat coder, the last candidate (and below Slow the only
    // one, written straight to the output).
    if frame.compare_tree_candidates {
        let mut candidate = BitWriter::new();
        flat.write_frame(pool, scratch, &mut candidate);
        best.offer(candidate);
        writer.append(best.get().expect("flat lossless candidate"));
    } else {
        flat.write_frame(pool, scratch, writer);
    }
    false
}

/// One frame's input and every setting derived from it before a candidate
/// is tried.
struct CoreFrame<'a> {
    /// The lossless front end's YCoCg planes (one plane for gray).
    ycocg: &'a Image3Si,
    alpha: Option<&'a AlphaPlane>,
    settings: LosslessSettings,
    effort: Effort,
    kind: ModularFrameKind<'a>,
    tree_search: TreeSearch,
    xsize: usize,
    ysize: usize,
    num_color: usize,
    /// Modular channels: the color planes, then alpha.
    nb_chans: usize,
    /// The value of a solid alpha plane. It is coded as a constant channel
    /// (Zero predictor, offset = the value, one-symbol histogram): no bits,
    /// no predictor and no context-tree work in the decoder, which otherwise
    /// ran the Weighted Predictor over the whole plane.
    alpha_constant: Option<i32>,
    /// Channels that carry samples: size gates treat a solid-alpha image as
    /// its color image.
    coded_chans: usize,
    /// Near-lossless samples share a `2k+1` grid. The Weighted predictor's
    /// averaging leaves it, so the tiers without a learned tree keep their
    /// fixed predictor on the grid with Gradient instead.
    use_wp: bool,
    /// The first LZ77 symbol (see [`lossless_min_symbol`]).
    min_symbol: u32,
    /// The codestream-default (256-px) group grid.
    grid: GroupGrid,
    grad_pack_fn: GradPackInteriorFn,
    /// The learned-tree tournament runs: every candidate writes a complete
    /// frame and the smallest wins. Tree headers, clustering, hybrid uint
    /// choices and LZ77 can otherwise reverse the order predicted from
    /// residual entropy.
    compare_tree_candidates: bool,
}

impl<'a> CoreFrame<'a> {
    fn new(
        ycocg: &'a Image3Si,
        alpha: Option<&'a AlphaPlane>,
        settings: LosslessSettings,
        kind: ModularFrameKind<'a>,
        tree_search: TreeSearch,
    ) -> Self {
        let effort = *settings.speed.effort();
        let (xsize, ysize) = (ycocg.xsize(), ycocg.ysize());
        let num_color = settings.num_color;
        let alpha_constant = alpha.and_then(constant_alpha_value);
        Self {
            ycocg,
            alpha,
            settings,
            effort,
            kind,
            tree_search,
            xsize,
            ysize,
            num_color,
            nb_chans: num_color + usize::from(alpha.is_some()),
            alpha_constant,
            coded_chans: num_color + usize::from(alpha.is_some() && alpha_constant.is_none()),
            use_wp: settings.decoding_speed.use_weighted_predictor()
                && (effort.learned_tree || !settings.near_lossless),
            min_symbol: lossless_min_symbol(settings.max_bits),
            grid: GroupGrid::new(xsize, ysize, GroupLayout::DEFAULT),
            grad_pack_fn: selected_grad_pack_interior_fn(),
            compare_tree_candidates: kind.allows_learned_tree()
                && effort.learned_tree
                && (num_color == 3 || num_color == 1)
                && settings.decoding_speed.use_ma_trees(),
        }
    }

    /// The codestream-default layout at the frame's upsampling.
    fn default_layout(&self) -> GroupLayout {
        GroupLayout::DEFAULT.with_upsampling(self.settings.upsampling)
    }

    /// WP headers a frame signals: one per section that has a GroupHeader.
    fn wp_header_count(&self) -> usize {
        if self.grid.single_group() {
            1
        } else {
            2 + self.grid.num_dc_groups() + self.grid.num_groups()
        }
    }

    /// Bits the frame's WP headers cost over the default preset's.
    fn wp_header_bits(&self, params: WpParams) -> f64 {
        if params == WpParams::DEFAULT {
            0.0
        } else {
            (51 * self.wp_header_count()) as f64
        }
    }

    /// Step 1: the Palette transform, for regular RGB(A) frames whose single
    /// group holds at most 256 colors, or whose 256-px groups are each
    /// low-color although the frame is not (large graphics). High-color
    /// groups stay on the normal path. Returns `None` when it does not apply,
    /// else the decisive flag of the frame written.
    fn encode_palette_transform(
        &self,
        pool: &ThreadPool,
        scratch: &mut CoderScratch,
        writer: &mut BitWriter,
    ) -> Option<bool> {
        if !self.kind.is_regular() || self.num_color != 3 {
            return None;
        }
        let mut palette_writer = BitWriter::new();
        let applies = if self.grid.single_group() {
            try_encode_palette_single_group(
                self.ycocg,
                self.alpha,
                self.xsize,
                self.ysize,
                self.min_symbol,
                self.grad_pack_fn,
                self.settings.speed,
                self.use_wp,
                self.kind,
                self.default_layout(),
                scratch,
                &mut palette_writer,
            )
        } else {
            try_encode_local_palette_multi_group(
                self.ycocg,
                self.alpha,
                self.xsize,
                self.ysize,
                self.grid.x_groups,
                self.grid.y_groups,
                self.grid.num_dc_groups(),
                self.min_symbol,
                self.grad_pack_fn,
                self.settings.speed,
                self.use_wp,
                self.kind,
                self.default_layout(),
                pool,
                scratch,
                &mut palette_writer,
            )
        };
        if !applies {
            return None;
        }
        // `palette_dual`: the frame without the transform competes on size.
        if self.effort.palette_dual {
            let mut normal_writer = BitWriter::new();
            let normal_decisive = encode_frame_lossless_core_impl(
                self.ycocg,
                self.alpha,
                self.settings,
                self.kind,
                false,
                self.tree_search,
                pool,
                scratch,
                &mut normal_writer,
            );
            if normal_writer.bits_written() < palette_writer.bits_written() {
                writer.append(&normal_writer);
                return Some(normal_decisive);
            }
        }
        writer.append(&palette_writer);
        Some(false)
    }

    /// Step 2: the Squeeze pyramid of a progressive regular RGB(A) frame.
    /// Returns whether it was written: a multi-group frame needs every
    /// squeezed channel to fit the global stream.
    fn encode_squeeze(
        &self,
        pool: &ThreadPool,
        scratch: &mut CoderScratch,
        writer: &mut BitWriter,
    ) -> bool {
        if !self.settings.progressive || !self.kind.is_regular() || self.num_color != 3 {
            return false;
        }
        if self.grid.single_group() {
            encode_squeeze_single_group(
                self.ycocg,
                self.alpha,
                self.xsize,
                self.ysize,
                self.min_symbol,
                self.grad_pack_fn,
                self.settings.speed,
                self.use_wp,
                self.kind,
                self.default_layout(),
                pool,
                scratch,
                writer,
            );
            true
        } else {
            encode_squeeze_multigroup(
                self.ycocg,
                self.alpha,
                self.xsize,
                self.ysize,
                self.min_symbol,
                self.grid.x_groups,
                self.grid.y_groups,
                self.grid.x_dc_groups,
                self.grid.y_dc_groups,
                self.grid.num_dc_groups(),
                self.grid.num_groups(),
                self.grad_pack_fn,
                self.settings.speed,
                self.use_wp,
                self.kind,
                self.default_layout(),
                pool,
                scratch,
                writer,
            )
        }
    }

    /// Step 4: the WP preset searched under `wp_search`, else the default.
    fn choose_wp_params(
        &self,
        rct_type: u32,
        pool: &ThreadPool,
        scratch: &mut CoderScratch,
    ) -> WpParams {
        if self.effort.wp_search && self.use_wp {
            choose_wp_params(
                self.ycocg,
                rct_type,
                self.alpha,
                self.alpha_constant,
                self.num_color,
                self.wp_header_count(),
                pool,
                scratch,
            )
        } else {
            WpParams::DEFAULT
        }
    }
}

/// Step 3: the reversible color transform (RCT) the frame is coded under.
///
/// Slow ranks every transform over the same fixed-context gradient cost
/// libjxl uses. When a non-YCoCg transform wins, the color planes are
/// rebuilt (on first use: an ink frame's palette source usually wins
/// without them) and the frame headers signal it. A single gray plane has
/// no RCT. Near-lossless planes rank at every tier: their values share a
/// `2k+1` grid that only the subtract-type transforms preserve, and
/// YCoCg's shifts would scatter it.
struct ColorTransform<'a> {
    ycocg: &'a Image3Si,
    num_color: usize,
    /// The signaled RCT: 6 (YCoCg, the input itself) for color, 0 for gray.
    rct_type: u32,
    /// The color planes under a non-YCoCg `rct_type`, once built.
    planes: Option<Image3Si>,
    /// Every transform with its estimated cost, cheapest first; empty
    /// without the search.
    ranked: Vec<(u32, f32)>,
}

impl<'a> ColorTransform<'a> {
    fn rank(frame: &CoreFrame<'a>, pool: &ThreadPool, scratch: &mut CoderScratch) -> Self {
        let effort = frame.effort;
        let ranked = if frame.kind.allows_learned_tree()
            && (effort.rct_search || frame.settings.near_lossless)
            && frame.num_color == 3
        {
            let row_step = if effort.rct_every_row {
                1
            } else if effort.rct_dense_rows {
                RCT_DENSE_ROW_STEP
            } else {
                RCT_SPARSE_ROW_STEP
            };
            rank_rcts(
                frame.ycocg,
                frame.xsize,
                frame.ysize,
                row_step,
                rct_candidates(effort.rct_all_types),
                pool,
                scratch,
            )
        } else {
            Vec::new()
        };
        let default_rct = if frame.num_color == 3 { 6 } else { 0 };
        Self {
            ycocg: frame.ycocg,
            num_color: frame.num_color,
            rct_type: ranked.first().map_or(default_rct, |&(t, _)| t),
            planes: None,
            ranked,
        }
    }

    /// The best-ranked transform that is not a cost twin of the winner: a
    /// permutation that only flips a channel's sign scores the same bits and
    /// would waste the runner-up race.
    pub(super) fn runner_up(&self) -> Option<(u32, f32)> {
        const TWIN_TOLERANCE: f32 = 1e-4;
        let (_, best) = *self.ranked.first()?;
        self.ranked
            .iter()
            .skip(1)
            .copied()
            .find(|&(_, cost)| cost > best * (1.0 + TWIN_TOLERANCE))
    }

    /// The color planes under `rct_type`, built on first use.
    fn planes(&mut self, pool: &ThreadPool, scratch: &mut CoderScratch) -> &Image3Si {
        if self.num_color != 3 || self.rct_type == 6 {
            return self.ycocg;
        }
        let ycocg = self.ycocg;
        let rct_type = self.rct_type;
        self.planes.get_or_insert_with(|| {
            rct_planes(ycocg, ycocg.xsize(), ycocg.ysize(), rct_type, pool, scratch)
        })
    }

    /// Frees the transformed planes. A decisive learned tree never returns to
    /// the RCT-based v1/flat paths, and the palette candidates need only the
    /// YCoCg input.
    fn release_planes(&mut self) {
        self.planes = None;
    }

    /// Switches to `rct_type`, whose planes (`None` for YCoCg) were built by
    /// the caller.
    fn adopt(&mut self, rct_type: u32, planes: Option<Image3Si>) {
        self.rct_type = rct_type;
        self.planes = planes;
    }
}

/// The smallest complete frame among the candidates written so far.
#[derive(Default)]
struct SmallestFrame {
    writer: Option<BitWriter>,
}

impl SmallestFrame {
    fn offer(&mut self, candidate: BitWriter) {
        keep_smaller_writer(&mut self.writer, candidate);
    }

    fn bits(&self) -> Option<usize> {
        self.writer.as_ref().map(BitWriter::bits_written)
    }

    fn get(&self) -> Option<&BitWriter> {
        self.writer.as_ref()
    }
}