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
668
669
670
671
672
673
674
675
676
677
678
679
680
/*
 * // 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.
 */

//! Palette, lossless and squeezed coding choices for extra channels.

use super::extra_tree;
use super::{ImageDim, K_GROUP_DIM};
use crate::bit_writer::BitWriter;
use crate::coder_scratch::CoderScratch;
use crate::encode_image::AlphaPlane;
use crate::encoding_context::EncodingContext;

/// Modular data of the extra channel, coded ahead of the frame sections:
/// the LfGlobal part, one part per DC group and one per AC group.
#[derive(Clone)]
pub(super) struct ExtraSections {
    pub(super) global: BitWriter,
    pub(super) lf: Vec<BitWriter>,
    pub(super) ac: Vec<BitWriter>,
}

impl ExtraSections {
    pub(super) fn bits(&self) -> usize {
        self.global.bits_written()
            + self.lf.iter().map(BitWriter::bits_written).sum::<usize>()
            + self.ac.iter().map(BitWriter::bits_written).sum::<usize>()
    }

    /// Final section padding and TOC size with this extra-channel encoding.
    fn serialized_size(&self, color_bits: &[usize], dim: &ImageDim, last_pass: usize) -> usize {
        let mut bits = color_bits.to_vec();
        bits[0] += self.global.bits_written();
        for (i, stream) in self.lf.iter().enumerate() {
            bits[1 + i] += stream.bits_written();
        }
        let ac_start = 2 + dim.num_dc_groups + last_pass * dim.num_groups;
        for (i, stream) in self.ac.iter().enumerate() {
            bits[ac_start + i] += stream.bits_written();
        }
        // A group permutation is independent of the extra representation;
        // its aligned header has the same size for every candidate.
        super::sections::combined_sections_size(&bits)
    }
}

/// Preserve the original lossless-versus-lossy choice before racing exact
/// representations. A smaller squeezed representation must not turn a plane
/// that previously stayed lossless into a lossy one.
pub(super) struct PreparedExtra {
    sections: ExtraSections,
    squeezed: Option<crate::lossy_modular::ExtraSqueeze>,
    /// The tail race's candidates when they are already coded.
    folds: Vec<ExtraSections>,
    global_code_ans: bool,
}

impl PreparedExtra {
    pub(crate) fn finish(
        self,
        ctx: &EncodingContext,
        dim: &ImageDim,
        dc_group_coords: &[(usize, usize)],
        last_pass: usize,
        color_bits: &[usize],
        scratch: &mut CoderScratch,
    ) -> ExtraSections {
        let original = self.sections;
        let Some(squeezed) = self.squeezed else {
            return original;
        };
        let Some(start) = squeezed.exact_tail_start() else {
            return original;
        };
        let mut best = None;
        let mut best_size = original.serialized_size(color_bits, dim, last_pass);
        if !self.folds.is_empty() {
            for sections in self.folds {
                let size = sections.serialized_size(color_bits, dim, last_pass);
                if size < best_size {
                    best = Some(sections);
                    best_size = size;
                }
            }
            return best.unwrap_or(original);
        }
        // The earliest exact cutoff and one more H/V pair give different
        // coarse prediction contexts. Keep this search bounded at all speeds.
        for retained in [start, start + 2] {
            let Some(candidate) = squeezed.without_exact_tail(retained) else {
                continue;
            };
            let sections = squeezed_sections(
                ctx,
                &candidate,
                dim,
                dc_group_coords,
                self.global_code_ans,
                Some((&squeezed, &original)),
                scratch,
            );
            let size = sections.serialized_size(color_bits, dim, last_pass);
            if size < best_size {
                best = Some(sections);
                best_size = size;
            }
        }
        best.unwrap_or(original)
    }
}

/// A lossless extra channel of few distinct values, coded through a Palette
/// transform: the values in index order and one index per sample.
pub(super) struct ExtraPalette {
    pub(super) entries: Vec<i32>,
    pub(super) indices: AlphaPlane,
}

/// Largest palette an extra channel is coded through.
const EXTRA_PALETTE_MAX_COLORS: usize = 256;
/// The global tree has not cut a plane's fixed lossless coding below a
/// quarter; past that the squeezed coding is out of its reach.
const GLOBAL_TREE_MAX_SAVING: usize = 4;
/// A gradient-only tree has cut it by at most 52 % (32 RGBA images, every
/// tier up to Fast), so below 1 / 2.5 of it the squeeze always stays.
const GRADIENT_TREE_MAX_SAVING: f64 = 2.5;

/// The palette of an integer extra channel whose distinct values number at
/// most `EXTRA_PALETTE_MAX_COLORS` and at most half its span, so that the
/// index image is narrower than the values.
fn extra_palette(plane: &AlphaPlane) -> Option<ExtraPalette> {
    let range = match plane {
        AlphaPlane::U8(_) => 1usize << 8,
        AlphaPlane::U16 { bits, .. } => 1usize << bits,
        AlphaPlane::F32(_) => return None,
    };
    let mut seen = vec![false; range];
    for i in 0..plane.len() {
        let v = plane.get_i32(i);
        if v < 0 || v as usize >= range {
            return None;
        }
        seen[v as usize] = true;
    }
    let entries: Vec<i32> = (0..range as i32).filter(|&v| seen[v as usize]).collect();
    let (Some(&lo), Some(&hi)) = (entries.first(), entries.last()) else {
        return None;
    };
    if entries.len() < 2
        || entries.len() > EXTRA_PALETTE_MAX_COLORS
        || 2 * entries.len() as i64 > i64::from(hi - lo) + 1
    {
        return None;
    }
    let mut index_of = vec![0u8; range];
    for (index, &v) in entries.iter().enumerate() {
        index_of[v as usize] = index as u8;
    }
    let indices = (0..plane.len())
        .map(|i| index_of[plane.get_i32(i) as usize])
        .collect();
    Some(ExtraPalette {
        entries,
        indices: AlphaPlane::U8(indices),
    })
}

/// The lossless sections of the extra channel, through `palette` when given:
/// its Palette transform and entries in the global stream and the indices per
/// AC group. Otherwise the values themselves.
fn lossless_sections(
    ctx: &EncodingContext,
    plane: &AlphaPlane,
    palette: Option<&ExtraPalette>,
    dim: &ImageDim,
    num_dc_groups: usize,
    global_code_ans: bool,
    scratch: &mut CoderScratch,
) -> ExtraSections {
    let mut sections = ExtraSections {
        global: BitWriter::new(),
        lf: (0..num_dc_groups).map(|_| BitWriter::new()).collect(),
        ac: Vec::new(),
    };
    match palette {
        Some(p) => crate::modular::write_global_palette_alpha_modular(
            &p.entries,
            plane.bits(),
            scratch,
            &mut sections.global,
        ),
        None => crate::modular::write_global_alpha_modular(
            plane,
            dim.xsize,
            dim.ysize,
            ctx.extra_step,
            global_code_ans,
            scratch,
            &mut sections.global,
        ),
    }
    let ac_plane = palette.map_or(plane, |p| &p.indices);
    sections.ac = ctx
        .thread_pool
        .steal_map(scratch, dim.num_groups, |group_id, scratch| {
            let x0 = (group_id % dim.xsize_groups) * K_GROUP_DIM;
            let y0 = (group_id / dim.xsize_groups) * K_GROUP_DIM;
            let mut w = BitWriter::new();
            crate::modular::write_ac_group_alpha(
                ac_plane,
                dim.xsize,
                dim.ysize,
                x0,
                y0,
                K_GROUP_DIM.min(dim.xsize - x0),
                K_GROUP_DIM.min(dim.ysize - y0),
                ctx.extra_step,
                scratch,
                &mut w,
            );
            w
        });
    sections
}

fn squeezed_sections(
    ctx: &EncodingContext,
    squeezed: &crate::lossy_modular::ExtraSqueeze,
    dim: &ImageDim,
    dc_group_coords: &[(usize, usize)],
    global_code_ans: bool,
    previous: Option<(&crate::lossy_modular::ExtraSqueeze, &ExtraSections)>,
    scratch: &mut CoderScratch,
) -> ExtraSections {
    let mut global = BitWriter::new();
    squeezed.write_global(global_code_ans, scratch, &mut global);
    let lf = if let Some((_, previous)) =
        previous.filter(|(original, _)| original.exact_tail_groups_unchanged(squeezed))
    {
        previous.lf.clone()
    } else {
        ctx.thread_pool
            .steal_map(scratch, dc_group_coords.len(), |i, scratch| {
                let (gx, gy) = dc_group_coords[i];
                let mut w = BitWriter::new();
                squeezed.write_lf_group(gx, gy, scratch, &mut w);
                w
            })
    };
    let ac = if let Some((_, previous)) =
        previous.filter(|(original, _)| original.exact_tail_ac_unchanged(squeezed))
    {
        previous.ac.clone()
    } else {
        ctx.thread_pool
            .steal_map(scratch, dim.num_groups, |group_id, scratch| {
                let mut w = BitWriter::new();
                squeezed.write_ac_group(
                    group_id % dim.xsize_groups,
                    group_id / dim.xsize_groups,
                    scratch,
                    &mut w,
                );
                w
            })
    };
    ExtraSections { global, lf, ac }
}

/// The squeezed coding through the frame's global tree. The tail race picks
/// the representation by its local coding; its AC groups then move onto the
/// global tree where that beats every local coding. Otherwise the folds'
/// local sections go on to the exact tail race.
struct JointSqueeze {
    squeezed: crate::lossy_modular::ExtraSqueeze,
    local: ExtraSections,
    folds: Vec<(crate::lossy_modular::ExtraSqueeze, ExtraSections)>,
    /// The fold the global tree codes (None: the unfolded squeeze).
    rep: Option<usize>,
    tree: Option<extra_tree::GlobalExtraTree>,
    ans: bool,
}

impl JointSqueeze {
    #[allow(clippy::too_many_arguments)]
    fn plan(
        ctx: &EncodingContext,
        squeezed: crate::lossy_modular::ExtraSqueeze,
        local: ExtraSections,
        dim: &ImageDim,
        dc_group_coords: &[(usize, usize)],
        ans: bool,
        streams: &extra_tree::GlobalStreams<'_>,
        scratch: &mut CoderScratch,
    ) -> Self {
        let folds: Vec<(crate::lossy_modular::ExtraSqueeze, ExtraSections)> = squeezed
            .exact_tail_start()
            .map(|start| {
                [start, start + 2]
                    .into_iter()
                    .filter_map(|retained| squeezed.without_exact_tail(retained))
                    .map(|fold| {
                        let sections = squeezed_sections(
                            ctx,
                            &fold,
                            dim,
                            dc_group_coords,
                            ans,
                            Some((&squeezed, &local)),
                            scratch,
                        );
                        (fold, sections)
                    })
                    .collect()
            })
            .unwrap_or_default();
        let rep = (0..folds.len())
            .min_by_key(|&i| folds[i].1.bits())
            .filter(|&i| folds[i].1.bits() < local.bits());
        let (rep_squeeze, rep_sections) =
            rep.map_or((&squeezed, &local), |i| (&folds[i].0, &folds[i].1));
        let tree = extra_tree::plan_global_squeeze_tree(
            ctx,
            rep_squeeze,
            dim,
            streams,
            rep_sections,
            scratch,
        );
        Self {
            squeezed,
            local,
            folds,
            rep,
            tree,
            ans,
        }
    }

    /// The smallest coding the plan reaches, before section padding.
    fn bits(&self) -> usize {
        let rep = self.rep.map_or(&self.local, |i| &self.folds[i].1);
        rep.bits() - self.tree.as_ref().map_or(0, |t| t.saving())
    }

    fn finish(self, streams: &mut extra_tree::GlobalStreams<'_>) -> PreparedExtra {
        if let Some(tree) = self.tree {
            let mut sections = self.rep.map_or(self.local, |i| self.folds[i].1.clone());
            tree.apply(streams, &mut sections);
            return PreparedExtra {
                sections,
                squeezed: None,
                folds: Vec::new(),
                global_code_ans: self.ans,
            };
        }
        PreparedExtra {
            sections: self.local,
            squeezed: Some(self.squeezed),
            folds: self
                .folds
                .into_iter()
                .map(|(_, sections)| sections)
                .collect(),
            global_code_ans: self.ans,
        }
    }
}

/// Code the extra channel losslessly — through a Palette transform when it
/// has few distinct values and that is smaller, and through the frame's
/// global tree where that beats the fixed local trees — and, when the context
/// asks for a lossy one, also squeezed and quantized; keep the smaller. The
/// lossless coding wins wherever it is no larger, so solid and hard-edged
/// planes stay exact. A winning global-tree coding moves the frame's DC
/// streams onto the merged tree through `streams`.
pub(super) fn prepare_extra_sections(
    ctx: &EncodingContext,
    plane: &AlphaPlane,
    dim: &ImageDim,
    dc_group_coords: &[(usize, usize)],
    last_pass: usize,
    mut streams: extra_tree::GlobalStreams<'_>,
    scratch: &mut CoderScratch,
) -> PreparedExtra {
    let single = dim.xsize <= K_GROUP_DIM && dim.ysize <= K_GROUP_DIM;
    let lossless_plane = ctx.extra_step == 1 && !plane.is_constant();
    let mut palette = lossless_plane.then(|| extra_palette(plane)).flatten();
    let ans = !streams.code.use_prefix_code;
    let groups = dc_group_coords.len();
    // A multi-group palette-coded channel codes its palette locally in the
    // global stream and its indices per AC group. Renumbering can break the
    // gradients the predictor follows, so without a global tree it races the
    // values coded directly. A global tree can reverse that order and costs
    // more to plan twice than the race saves; it keeps the palette.
    // A single-group one keeps the values' section as the local alternative
    // of its global-tree coding, which carries the palette.
    let mut lossless = match palette.as_ref().filter(|_| !single) {
        Some(p) => {
            let indexed = lossless_sections(ctx, plane, Some(p), dim, groups, ans, scratch);
            if ctx.extra_plane_tree.is_some() {
                indexed
            } else {
                let plain = lossless_sections(ctx, plane, None, dim, groups, ans, scratch);
                if plain.bits() < indexed.bits() {
                    palette = None;
                    plain
                } else {
                    indexed
                }
            }
        }
        None => lossless_sections(ctx, plane, None, dim, groups, ans, scratch),
    };
    let squeezed = (ctx.extra_squeeze_distance > 0.0 && !plane.is_constant())
        .then(|| {
            crate::lossy_modular::ExtraSqueeze::new(
                plane,
                dim.xsize,
                dim.ysize,
                ctx.extra_squeeze_distance,
                ctx.extra_learned_trees,
                ctx.extra_context_trees,
            )
        })
        .flatten();
    let lossy = squeezed
        .as_ref()
        .map(|s| squeezed_sections(ctx, s, dim, dc_group_coords, ans, None, scratch));
    let global_tree = (lossless_plane
        && ctx.extra_plane_tree.is_some_and(|budget| {
            lossy.as_ref().is_none_or(|lossy| {
                if budget.gradient_only {
                    (lossless.bits() as f64) < GRADIENT_TREE_MAX_SAVING * lossy.bits() as f64
                } else {
                    lossless.bits() < GLOBAL_TREE_MAX_SAVING * lossy.bits()
                }
            })
        }))
    .then(|| {
        extra_tree::plan_global_extra_tree(
            ctx,
            plane,
            palette.as_ref(),
            dim,
            last_pass,
            &streams,
            &lossless,
            scratch,
        )
    })
    .flatten();
    let lossless_bits = lossless.bits() - global_tree.as_ref().map_or(0, |t| t.saving());
    match lossy {
        Some(lossy) if lossy.bits() < lossless_bits => {
            let squeezed = squeezed.expect("lossy sections come from a squeeze");
            if ctx.speed.effort().extra_global_tree {
                let joint = JointSqueeze::plan(
                    ctx,
                    squeezed,
                    lossy,
                    dim,
                    dc_group_coords,
                    ans,
                    &streams,
                    scratch,
                );
                let levels = lossy_palette(
                    ctx,
                    plane,
                    &joint.squeezed,
                    joint.bits(),
                    dim,
                    last_pass,
                    &mut streams,
                    scratch,
                );
                return levels.unwrap_or_else(|| joint.finish(&mut streams));
            }
            if let Some(levels) = lossy_palette(
                ctx,
                plane,
                &squeezed,
                lossy.bits(),
                dim,
                last_pass,
                &mut streams,
                scratch,
            ) {
                return levels;
            }
            PreparedExtra {
                sections: lossy,
                squeezed: squeezed.exact_tail_start().is_some().then_some(squeezed),
                folds: Vec::new(),
                global_code_ans: ans,
            }
        }
        _ => {
            if let Some(tree) = global_tree {
                tree.apply(&mut streams, &mut lossless);
            }
            PreparedExtra {
                sections: lossless,
                squeezed: None,
                folds: Vec::new(),
                global_code_ans: ans,
            }
        }
    }
}

/// The squeezed plane's weighted squared error after the decoder's clamp.
fn squeezed_error(
    plane: &AlphaPlane,
    squeezed: &crate::lossy_modular::ExtraSqueeze,
    weights: &[f32],
) -> f64 {
    let max = (1i64 << plane.bits()) - 1;
    squeezed
        .reconstruct()
        .iter()
        .zip(weights)
        .enumerate()
        .map(|(i, (&v, &w))| {
            let e = i64::from(v).clamp(0, max) - i64::from(plane.get_i32(i));
            f64::from(w) * (e * e) as f64
        })
        .sum()
}

/// The lossy palette arm skips its exact coding when the order-0 estimate of
/// its indices ([`crate::lossy_modular::palette_index_bits`]) is above this
/// many times the squeeze's coded bits.
const PALETTE_SCREEN: f64 = 4.0;

/// The lossy palette arm: the fewest levels within part of the squeeze's
/// weighted squared error ([`super::extra_levels`]), coded losslessly like an
/// exact plane (palette, the learned plane tree, global tree), when that is
/// smaller than the squeeze's best coding. Only with a plane tree: the fixed
/// per-group coding codes palette indices worse than the squeeze.
#[allow(clippy::too_many_arguments)]
fn lossy_palette(
    ctx: &EncodingContext,
    plane: &AlphaPlane,
    squeezed: &crate::lossy_modular::ExtraSqueeze,
    squeezed_bits: usize,
    dim: &ImageDim,
    last_pass: usize,
    streams: &mut extra_tree::GlobalStreams<'_>,
    scratch: &mut CoderScratch,
) -> Option<PreparedExtra> {
    if ctx.extra_plane_tree.is_none() || ctx.extra_step != 1 {
        return None;
    }
    let weights = super::extra_levels::error_weights(plane, dim.xsize, dim.ysize);
    let budget =
        super::extra_levels::LOSSY_PALETTE_ERROR * squeezed_error(plane, squeezed, &weights);
    let levels = super::extra_levels::fewest_levels(plane, &weights, budget, ctx.extra_black)?;
    let indices: Vec<i32> = (0..levels.len()).map(|i| levels.get_i32(i)).collect();
    let estimate = crate::lossy_modular::palette_index_bits(&indices, dim.xsize, dim.ysize);
    if estimate > PALETTE_SCREEN * squeezed_bits as f64 {
        return None;
    }
    let single = dim.xsize <= K_GROUP_DIM && dim.ysize <= K_GROUP_DIM;
    let ans = !streams.code.use_prefix_code;
    let palette = extra_palette(&levels);
    let groups = dim.num_dc_groups;
    let local_palette = palette.as_ref().filter(|_| !single);
    let mut sections = lossless_sections(ctx, &levels, local_palette, dim, groups, ans, scratch);
    let tree = extra_tree::plan_global_extra_tree(
        ctx,
        &levels,
        palette.as_ref(),
        dim,
        last_pass,
        streams,
        &sections,
        scratch,
    );
    if sections.bits() - tree.as_ref().map_or(0, |t| t.saving()) >= squeezed_bits {
        return None;
    }
    if let Some(tree) = tree {
        tree.apply(streams, &mut sections);
    }
    Some(PreparedExtra {
        sections,
        squeezed: None,
        folds: Vec::new(),
        global_code_ans: ans,
    })
}

pub(super) fn zero_alpha_for_lossy(
    alpha: Option<&AlphaPlane>,
    pixels: usize,
) -> Option<AlphaPlane> {
    match alpha {
        Some(AlphaPlane::U8(_)) => Some(AlphaPlane::U8(vec![0; pixels])),
        Some(AlphaPlane::U16 { bits, .. }) => Some(AlphaPlane::U16 {
            data: vec![0; pixels],
            bits: *bits,
        }),
        Some(AlphaPlane::F32(_)) => Some(AlphaPlane::F32(vec![0; pixels])),
        None => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn writer(bits: usize) -> BitWriter {
        let mut w = BitWriter::new();
        for _ in 0..bits {
            w.write(1, 1);
        }
        w
    }

    #[test]
    fn extra_size_matches_aligned_sections_and_last_pass() {
        for (width, height) in [(63, 61), (513, 17), (2051, 257)] {
            let dim = ImageDim::new(width, height);
            for passes in [1, 3] {
                let count = 2 + dim.num_dc_groups + passes * dim.num_groups;
                for boundary in [7, 8184, 8191, 139256] {
                    let color_bits: Vec<usize> = (0..count).map(|i| boundary + i % 8).collect();
                    let extra = ExtraSections {
                        global: writer(13),
                        lf: (0..dim.num_dc_groups).map(|i| writer(5 + i)).collect(),
                        ac: (0..dim.num_groups).map(|i| writer(9 + i)).collect(),
                    };
                    let mut sections: Vec<BitWriter> =
                        color_bits.iter().map(|&bits| writer(bits)).collect();
                    sections[0].append(&extra.global);
                    for (i, lf) in extra.lf.iter().enumerate() {
                        sections[1 + i].append(lf);
                    }
                    let last_ac = 2 + dim.num_dc_groups + (passes - 1) * dim.num_groups;
                    for (i, ac) in extra.ac.iter().enumerate() {
                        sections[last_ac + i].append(ac);
                    }
                    let mut output = BitWriter::new();
                    super::super::sections::combine_sections(&mut sections, &mut output);
                    assert_eq!(
                        extra.serialized_size(&color_bits, &dim, passes - 1),
                        output.bits_written() / 8,
                    );
                }
            }
        }
    }
}