jixel 0.5.11

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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
 * // 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.
 */

//! Learned-tree frame and section encoding.

use super::encoder::TreeSearch;
use super::frame::keep_smaller_writer;
use super::groups::group_lz_threads;
use super::header::{write_frame_header_modular_kind, write_patch_dictionary, write_toc_entry};
use super::lz77::{
    ConstantTail, build_lz_pixel_code_tails, estimate_coded_bits, write_lz_section_tail,
    write_tree_lz77,
};
use super::ma_local::{LOCAL_TREE_MAX_BITS_PER_VALUE, write_local_tree_sections};
use super::ma_lookup::MaLookup;
use super::ma_sampling::tokenize_channel_ma;
use super::ma_source::{
    LearnedCandidate, MaChannel, MaPlacement, MaSource, MaTransform, ac_stream_id, with_ma_pixels,
};
use super::ma_streams::{TreeStreams, VARIANT_WRITE_TOLERANCE, choose_tree_streams};
use super::palette::write_palette_transform;
use super::tokens::{RawTokenStreams, RawTokens};
use super::{GroupLayout, lz77};
use crate::bit_writer::BitWriter;
use crate::coder_scratch::CoderScratch;
use crate::patches::ModularFrameKind;
use crate::thread_pool::ThreadPool;
use crate::weighted_predictor::{WpParams, write_wp_header};

// Compile both representations once; use the compact path only when every
// stream fits. The wide fallback retains the same stream order and values.
macro_rules! with_raw_streams {
    ($raw:expr, |$streams:ident| $body:block) => {
        match RawTokenStreams::new($raw) {
            RawTokenStreams::Compact($streams) => $body,
            RawTokenStreams::Wide($streams) => $body,
        }
    };
}

/// Write the complete learned-tree frame under `layout`: a local tree in the
/// single section when the frame is one group, otherwise a global tree in the
/// DC-global section (followed by any global-stream channels) with every AC
/// group routed through it (fresh WP state and group-local coordinates per
/// group, matching the decoder). Returns the candidate's estimated fractional
/// saving over the flat path.
#[allow(clippy::too_many_arguments)]
pub(super) fn write_learned_tree_frame(
    speed: crate::Speed,
    source: &MaSource<'_>,
    has_alpha: bool,
    frame_kind: ModularFrameKind<'_>,
    xsize: usize,
    ysize: usize,
    layout: GroupLayout,
    min_symbol: u32,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    wp_params: WpParams,
    use_wp: bool,
    cand: &LearnedCandidate,
    tree_search: TreeSearch,
    writer: &mut BitWriter,
) -> f64 {
    let prune_trailing_lz = matches!(tree_search, TreeSearch::Vector)
        && matches!(source.transform, MaTransform::Palette { .. });
    let gdim = layout.dim();
    let xsize_groups = xsize.div_ceil(gdim);
    let ysize_groups = ysize.div_ceil(gdim);
    let num_ac_groups = xsize_groups * ysize_groups;
    let num_dc_groups = xsize.div_ceil(layout.lf_dim()) * ysize.div_ceil(layout.lf_dim());
    let single_group = num_ac_groups == 1;
    let placement = source.placement(layout, single_group);
    let constant = source.constant_placement(&placement);
    let wrapped;
    let cand = match constant {
        Some((p, value)) => {
            wrapped = cand.with_constant_channel(p.chan_id, value);
            &wrapped
        }
        None => cand,
    };
    let num_ctx = cand.num_ctx;
    let distance_ctx = num_ctx;
    let tokenize = |p: &MaPlacement,
                    stream: i32,
                    x0: usize,
                    y0: usize,
                    w: usize,
                    h: usize,
                    out: &mut RawTokens| {
        tokenize_ma_rect(
            source, &placement, p, stream, x0, y0, w, h, cand, wp_params, use_wp, out,
        );
    };

    write_frame_header_modular_kind(has_alpha, frame_kind, layout, writer);
    // A patched frame's dictionary opens its first section, before the
    // dequant/tree bits, exactly as on the flat path.
    let dictionary: Option<BitWriter> = match frame_kind {
        ModularFrameKind::Patched { references, .. } => {
            let mut w = BitWriter::new();
            write_patch_dictionary(references, has_alpha, scratch, &mut w);
            Some(w)
        }
        _ => None,
    };
    let dictionary = dictionary.as_ref();

    if single_group {
        let channel_tokens = pool.steal_map(scratch, placement.len(), |i, _scratch| {
            let p = &placement[i];
            let ch = &source.channels[p.channel];
            if ch.constant.is_some() {
                return RawTokens::with_capacity(0);
            }
            let mut tokens = RawTokens::with_capacity(ch.w * ch.h);
            tokenize(p, 0, 0, 0, ch.w, ch.h, &mut tokens);
            tokens
        });
        let tail = constant.map_or(ConstantTail::NONE, |(p, _)| {
            let ch = &source.channels[p.channel];
            cand.constant_tail(ch.w, ch.h)
        });
        let mut section = with_raw_streams!(channel_tokens, |channel_tokens| {
            let mut tokens = Vec::with_capacity(source.total_values());
            for channel in channel_tokens {
                tokens.extend(channel);
            }

            let variants = choose_tree_streams(
                speed,
                vec![tokens],
                distance_ctx,
                num_ctx as usize + 1,
                min_symbol,
                source.deep_lz_max_ratio(),
                prune_trailing_lz,
                pool,
                scratch,
            );
            let mut best: Option<BitWriter> = None;
            let mut best_estimate = f64::INFINITY;
            for variant in variants {
                match variant {
                    TreeStreams::Literals(streams) => {
                        let stream = streams.into_iter().next().expect("one stream");
                        write_learned_single_variant(
                            frame_kind,
                            speed,
                            &stream,
                            tail,
                            source,
                            cand,
                            min_symbol,
                            wp_params,
                            dictionary,
                            pool,
                            scratch,
                            &mut best,
                            &mut best_estimate,
                        );
                    }
                    TreeStreams::Lz(streams) => {
                        let stream = streams.into_iter().next().expect("one stream");
                        write_learned_single_variant(
                            frame_kind,
                            speed,
                            &stream,
                            tail,
                            source,
                            cand,
                            min_symbol,
                            wp_params,
                            dictionary,
                            pool,
                            scratch,
                            &mut best,
                            &mut best_estimate,
                        );
                    }
                }
            }
            best.expect("learned-tree section")
        });

        writer.write(1, 0);
        writer.zero_pad_to_byte();
        write_toc_entry(section.bits_written() / 8, writer);
        writer.zero_pad_to_byte();
        writer.append_byte_aligned(std::slice::from_mut(&mut section));
        writer.zero_pad_to_byte();
        return cand.estimated_savings;
    }

    // Global stream: channels that fit a group, coded in the DC-global section.
    let mut global_tokens = RawTokens::with_capacity(0);
    for p in placement.iter().filter(|p| p.global) {
        let ch = &source.channels[p.channel];
        if ch.constant.is_some() {
            continue;
        }
        tokenize(p, 0, 0, 0, ch.w, ch.h, &mut global_tokens);
    }
    let global_tail = constant
        .filter(|(p, _)| p.global)
        .map_or(ConstantTail::NONE, |(p, _)| {
            let ch = &source.channels[p.channel];
            cand.constant_tail(ch.w, ch.h)
        });
    let has_global_stream = !global_tokens.is_empty() || global_tail.count > 0;

    let group_placement: Vec<&MaPlacement> = placement
        .iter()
        .filter(|p| !p.global && source.channels[p.channel].constant.is_none())
        .collect();
    let group_constant = constant.filter(|(p, _)| !p.global);
    let group_tail = |group_index: usize| -> ConstantTail {
        if source.group_override(group_index).is_some() {
            return ConstantTail::NONE;
        }
        group_constant.map_or(ConstantTail::NONE, |(p, _)| {
            let ch = &source.channels[p.channel];
            let x0 = (group_index % xsize_groups) * gdim;
            let y0 = (group_index / xsize_groups) * gdim;
            cand.constant_tail(gdim.min(ch.w - x0), gdim.min(ch.h - y0))
        })
    };
    let group_tokens: Vec<RawTokens> = pool.steal_map_with_threads(
        scratch,
        num_ac_groups,
        group_lz_threads(speed, pool),
        |group_index, _scratch| {
            let x0 = (group_index % xsize_groups) * gdim;
            let y0 = (group_index / xsize_groups) * gdim;
            if let Some(over) = source.group_override(group_index) {
                let num_values = over.channels.iter().map(|ch| ch.w * ch.h).sum();
                let mut toks = RawTokens::with_capacity(num_values);
                for (chan_id, ch) in over.channels.iter().enumerate() {
                    tokenize_ma_group_channel(
                        ch,
                        chan_id as u32,
                        ac_stream_id(num_dc_groups, group_index),
                        cand,
                        wp_params,
                        use_wp,
                        &mut toks,
                    );
                }
                return toks;
            }
            let num_values = group_placement
                .iter()
                .map(|p| {
                    let ch = &source.channels[p.channel];
                    gdim.min(ch.w - x0) * gdim.min(ch.h - y0)
                })
                .sum();
            let mut toks = RawTokens::with_capacity(num_values);
            for p in &group_placement {
                let ch = &source.channels[p.channel];
                let gw = gdim.min(ch.w - x0);
                let gh = gdim.min(ch.h - y0);
                tokenize(
                    p,
                    ac_stream_id(num_dc_groups, group_index),
                    x0,
                    y0,
                    gw,
                    gh,
                    &mut toks,
                );
            }
            toks
        },
    );
    // Stream 0 is the global stream (possibly empty), then one per AC group.
    let mut all_tokens: Vec<RawTokens> = Vec::with_capacity(1 + num_ac_groups);
    all_tokens.push(global_tokens);
    all_tokens.extend(group_tokens);
    let tails: Vec<ConstantTail> = std::iter::once(global_tail)
        .chain((0..num_ac_groups).map(group_tail))
        .collect();
    let mut sections = with_raw_streams!(all_tokens, |all_tokens| {
        let variants = choose_tree_streams(
            speed,
            all_tokens,
            distance_ctx,
            num_ctx as usize + 1,
            min_symbol,
            source.deep_lz_max_ratio(),
            prune_trailing_lz,
            pool,
            scratch,
        );
        let mut best: Option<Vec<BitWriter>> = None;
        let mut best_bits = usize::MAX;
        let mut best_estimate = f64::INFINITY;
        for variant in variants {
            match variant {
                TreeStreams::Literals(streams) => {
                    write_learned_grouped_variant(
                        frame_kind,
                        speed,
                        &streams,
                        &tails,
                        source,
                        cand,
                        min_symbol,
                        wp_params,
                        pool,
                        scratch,
                        num_ac_groups,
                        num_dc_groups,
                        dictionary,
                        &mut best,
                        &mut best_bits,
                        &mut best_estimate,
                    );
                }
                TreeStreams::Lz(streams) => {
                    write_learned_grouped_variant(
                        frame_kind,
                        speed,
                        &streams,
                        &tails,
                        source,
                        cand,
                        min_symbol,
                        wp_params,
                        pool,
                        scratch,
                        num_ac_groups,
                        num_dc_groups,
                        dictionary,
                        &mut best,
                        &mut best_bits,
                        &mut best_estimate,
                    );
                }
            }
        }
        best.expect("learned-tree sections")
    });

    // Local per-group trees (libjxl's streaming layout) compete on size,
    // whole frame or group by group.
    let local_gate = matches!(tree_search, TreeSearch::Full)
        && cand.est_real <= LOCAL_TREE_MAX_BITS_PER_VALUE * source.total_values() as f64;
    if !has_global_stream
        && local_gate
        && let Some(local) = write_local_tree_sections(
            frame_kind,
            speed,
            source,
            &placement,
            xsize,
            ysize,
            layout,
            num_dc_groups,
            min_symbol,
            wp_params,
            use_wp,
            dictionary,
            pool,
            scratch,
        )
    {
        debug_assert_eq!(local.len(), sections.len());
        let global_bits: usize = sections.iter().map(BitWriter::bits_written).sum();
        let local_bits: usize = local.iter().map(BitWriter::bits_written).sum();
        // Hybrid: the global tree and histograms stay in the DC-global
        // section; each AC group section is self-contained either way
        // (its own use_global_tree bit), so it takes the smaller one.
        let first_ac = 2 + num_dc_groups;
        let hybrid_bits = global_bits
            - (first_ac..sections.len())
                .map(|i| {
                    sections[i]
                        .bits_written()
                        .saturating_sub(local[i].bits_written())
                })
                .sum::<usize>();
        if local_bits <= hybrid_bits {
            if local_bits < global_bits {
                sections = local;
            }
        } else if hybrid_bits < global_bits {
            for (i, section) in local.into_iter().enumerate().skip(first_ac) {
                if section.bits_written() < sections[i].bits_written() {
                    sections[i] = section;
                }
            }
        }
    }

    writer.write(1, 0);
    writer.zero_pad_to_byte();
    for s in &sections {
        write_toc_entry(s.bits_written() / 8, writer);
    }
    writer.zero_pad_to_byte();
    writer.append_byte_aligned(&mut sections);
    writer.zero_pad_to_byte();
    cand.estimated_savings
}

/// Tokenize the `w` x `h` rectangle at (`x0`, `y0`) of placement `p`'s
/// channel through `cand`'s tree. Placement is in source-channel order; its
/// channel ids already account for the independent global and group streams
/// (including palettes). The compiled lookup folds the channel and stream
/// ids, so it is built per (channel, stream): cheap next to tokenizing the
/// stream itself.
/// Tokenize one palettised group's own channel (whole channel, no reference
/// properties, channel ids numbered inside that group's stream).
#[allow(clippy::too_many_arguments)]
fn tokenize_ma_group_channel(
    ch: &MaChannel<'_>,
    chan_id: u32,
    stream: i32,
    cand: &LearnedCandidate,
    wp_params: WpParams,
    use_wp: bool,
    out: &mut RawTokens,
) {
    let lookup = MaLookup::new(&cand.tree, &cand.leaf_ctx, chan_id, stream);
    with_ma_pixels!(ch, |pixels| tokenize_channel_ma(
        |y| &pixels[y * ch.w..][..ch.w],
        ch.w,
        ch.h,
        chan_id,
        stream,
        &[],
        wp_params,
        use_wp,
        &lookup,
        &cand.leaf_offset,
        out,
    ));
}

pub(super) fn tokenize_ma_rect(
    source: &MaSource<'_>,
    placement: &[MaPlacement],
    p: &MaPlacement,
    stream: i32,
    x0: usize,
    y0: usize,
    w: usize,
    h: usize,
    cand: &LearnedCandidate,
    wp_params: WpParams,
    use_wp: bool,
    out: &mut RawTokens,
) {
    let ch = &source.channels[p.channel];
    debug_assert!(ch.constant.is_none(), "constant channels are tails");
    let lookup = MaLookup::new(&cand.tree, &cand.leaf_ctx, p.chan_id, stream);
    let refs = source.ref_planes(placement, p, x0, y0);
    with_ma_pixels!(ch, |pixels| tokenize_channel_ma(
        |y| &pixels[(y0 + y) * ch.w + x0..][..w],
        w,
        h,
        p.chan_id,
        stream,
        &refs,
        wp_params,
        use_wp,
        &lookup,
        &cand.leaf_offset,
        out,
    ));
}

#[allow(clippy::too_many_arguments)]
fn write_learned_single_variant<T: lz77::LzTokenSource>(
    frame_kind: ModularFrameKind<'_>,
    speed: crate::Speed,
    stream: &[T],
    tail: ConstantTail,
    source: &MaSource<'_>,
    cand: &LearnedCandidate,
    min_symbol: u32,
    wp_params: WpParams,
    dictionary: Option<&BitWriter>,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    best: &mut Option<BitWriter>,
    best_estimate: &mut f64,
) {
    let num_ctx = cand.num_ctx;
    let distance_ctx = num_ctx;
    let code = build_lz_pixel_code_tails(
        speed.effort().lz_exhaustive_entropy(),
        &[stream],
        &[tail],
        num_ctx as usize,
        min_symbol,
        true,
        true,
        Some(pool),
        &mut scratch.lz_entropy,
        &mut scratch.huffman_pool,
    );
    let estimate = estimate_coded_bits(&[stream], distance_ctx, &code, min_symbol, Some(pool));
    if estimate >= *best_estimate * VARIANT_WRITE_TOLERANCE {
        return;
    }
    *best_estimate = (*best_estimate).min(estimate);
    let mut body = BitWriter::new();
    if let Some(dictionary) = dictionary {
        body.append(dictionary);
    }
    super::header::write_dc_quant(frame_kind, &mut body);
    body.write(1, 0); // has_tree = 0 (local tree in GroupHeader)
    body.write(1, 0); // use_global_tree = 0
    write_wp_header(wp_params, &mut body);
    source.write_transforms(&mut body);
    write_tree_lz77(
        &cand.tree_tokens,
        &code,
        min_symbol,
        &mut scratch.huffman_pool,
        &mut body,
    );
    write_lz_section_tail(stream, tail, distance_ctx, &code, min_symbol, &mut body);
    body.zero_pad_to_byte();
    keep_smaller_writer(best, body);
}

#[allow(clippy::too_many_arguments)]
fn write_learned_grouped_variant<T: lz77::LzTokenSource>(
    frame_kind: ModularFrameKind<'_>,
    speed: crate::Speed,
    streams: &[Vec<T>],
    tails: &[ConstantTail],
    source: &MaSource<'_>,
    cand: &LearnedCandidate,
    min_symbol: u32,
    wp_params: WpParams,
    pool: &ThreadPool,
    scratch: &mut CoderScratch,
    num_ac_groups: usize,
    num_dc_groups: usize,
    dictionary: Option<&BitWriter>,
    best: &mut Option<Vec<BitWriter>>,
    best_bits: &mut usize,
    best_estimate: &mut f64,
) {
    let num_ctx = cand.num_ctx;
    let distance_ctx = num_ctx;
    let num_sections = 1 + num_dc_groups + 1 + num_ac_groups;
    let slices: Vec<&[T]> = streams.iter().map(Vec::as_slice).collect();
    let code = build_lz_pixel_code_tails(
        speed.effort().lz_exhaustive_entropy(),
        &slices,
        tails,
        num_ctx as usize,
        min_symbol,
        true,
        true,
        Some(pool),
        &mut scratch.lz_entropy,
        &mut scratch.huffman_pool,
    );
    let estimate = estimate_coded_bits(&slices, distance_ctx, &code, min_symbol, Some(pool));
    if estimate >= *best_estimate * VARIANT_WRITE_TOLERANCE {
        return;
    }
    *best_estimate = (*best_estimate).min(estimate);
    let mut sections: Vec<BitWriter> = (0..num_sections).map(|_| BitWriter::new()).collect();

    if let Some(dictionary) = dictionary {
        sections[0].append(dictionary);
    }
    super::header::write_dc_quant(frame_kind, &mut sections[0]);
    sections[0].write(1, 1); // has_tree = 1
    write_tree_lz77(
        &cand.tree_tokens,
        &code,
        min_symbol,
        &mut scratch.huffman_pool,
        &mut sections[0],
    );
    sections[0].write(1, 1); // use_global_tree
    write_wp_header(wp_params, &mut sections[0]);
    source.write_transforms(&mut sections[0]);
    // An empty global stream still carries its ANS state: jxl-oxide reads it.
    write_lz_section_tail(
        &streams[0],
        tails[0],
        distance_ctx,
        &code,
        min_symbol,
        &mut sections[0],
    );
    sections[0].zero_pad_to_byte();

    for section in sections[1..num_dc_groups + 1].iter_mut() {
        section.write(1, 1);
        write_wp_header(wp_params, section);
        section.write(2, 0);
        section.zero_pad_to_byte();
    }

    let ac_global_idx = 1 + num_dc_groups;
    sections[ac_global_idx].write(1, 1);
    write_wp_header(wp_params, &mut sections[ac_global_idx]);
    sections[ac_global_idx].zero_pad_to_byte();

    // The code borrows caller scratch; scratch-free maps let the same pool
    // write the independent group sections without aliasing that storage.
    let group_sections = pool.map(num_ac_groups, |group| {
        let mut section = BitWriter::new();
        section.write(1, 1);
        write_wp_header(wp_params, &mut section);
        // A palettised group declares its own Palette transform here.
        match source.group_override(group) {
            Some(over) => write_palette_transform(over.num_c, over.nb_colors, &mut section),
            None => section.write(2, 0),
        }
        write_lz_section_tail(
            &streams[1 + group],
            tails[1 + group],
            distance_ctx,
            &code,
            min_symbol,
            &mut section,
        );
        section.zero_pad_to_byte();
        section
    });
    for (group_index, section) in group_sections.into_iter().enumerate() {
        sections[2 + num_dc_groups + group_index] = section;
    }
    let bits: usize = sections.iter().map(BitWriter::bits_written).sum();
    if bits < *best_bits {
        *best_bits = bits;
        *best = Some(sections);
    }
}