jixel 0.5.10

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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*
 * // 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.
 */

//! Modular frame headers, transforms, patch dictionaries, and table of contents.

use super::GroupLayout;
use super::rct::write_rct_transform;
use crate::bit_writer::BitWriter;
use crate::coder_scratch::CoderScratch;
use crate::entropy::{Token, pack_signed};
use crate::patches::{ModularFrameKind, NUM_PATCH_CONTEXTS, PATCH_REF_ID, PatchReference};
use crate::path_order;
use crate::shared_sort::SharedSort;

pub(crate) fn write_u64(value: u64, w: &mut BitWriter) {
    match value {
        0 => w.write(2, 0),
        1..=16 => {
            w.write(2, 1);
            w.write(4, value - 1);
        }
        17..=272 => {
            w.write(2, 2);
            w.write(8, value - 17);
        }
        _ => unreachable!("lossless frame flags fit the short U64 form"),
    }
}

pub(crate) fn write_frame_dimension(value: usize, w: &mut BitWriter) {
    let value = value as u64;
    if value < 256 {
        w.write(2, 0);
        w.write(8, value);
    } else if value < 2304 {
        w.write(2, 1);
        w.write(11, value - 256);
    } else if value < 18688 {
        w.write(2, 2);
        w.write(14, value - 2304);
    } else {
        w.write(2, 3);
        w.write(30, value - 18688);
    }
}

/// The LfGlobal dc_quant field of a modular frame: all_default, or the
/// refined XYB lattice of a crop or ink frame ([`crate::xyb::CROP_LATTICE_SCALE`]).
pub(super) fn write_dc_quant(kind: ModularFrameKind<'_>, w: &mut BitWriter) {
    let scale = match kind {
        ModularFrameKind::XybCrop { .. } | ModularFrameKind::XybBlend { .. } => {
            crate::xyb::CROP_LATTICE_SCALE
        }
        _ => 1,
    };
    if scale == 1 {
        w.write(1, 1); // dc_quant all_default = 1
        return;
    }
    use crate::quant_weights::INV_DC_QUANT;
    w.write(1, 0);
    for inv in INV_DC_QUANT {
        // Read as value / 128; powers of two keep every step F16-exact.
        let step_x128 = 128.0 / (inv * scale as f32);
        w.write(16, crate::util::f32_to_f16_bits(step_x128) as u64);
    }
}

pub(super) fn write_frame_header_modular_kind(
    has_alpha: bool,
    kind: ModularFrameKind<'_>,
    layout: GroupLayout,
    w: &mut BitWriter,
) {
    match kind {
        ModularFrameKind::Regular => write_frame_header_modular(has_alpha, layout, w),
        ModularFrameKind::RgbSavedFirst => {
            write_frame_header_modular_layer(has_alpha, false, layout, w)
        }
        ModularFrameKind::RgbBlendLast => {
            write_frame_header_modular_layer(has_alpha, true, layout, w)
        }
        ModularFrameKind::Patched { saved_first, .. } => {
            write_frame_header_modular_flags(has_alpha, 2, saved_first, layout, w)
        }
        ModularFrameKind::RgbCrop {
            x0,
            y0,
            width,
            height,
            last,
        } => write_frame_header_modular_crop(
            has_alpha,
            false,
            [x0, y0, width, height],
            layout,
            last,
            w,
        ),
        ModularFrameKind::XybCrop {
            x0,
            y0,
            width,
            height,
            last,
        } => write_frame_header_modular_crop(
            has_alpha,
            true,
            [x0, y0, width, height],
            layout,
            last,
            w,
        ),
        ModularFrameKind::RgbBlendCrop {
            x0,
            y0,
            width,
            height,
        } => write_frame_header_modular_blend_crop([x0, y0, width, height], layout, w),
        ModularFrameKind::XybReferenceOnly {
            width,
            height,
            slot,
        } => write_frame_header_modular_xyb_reference(width, height, slot, has_alpha, layout, w),
        ModularFrameKind::XybBlend { last } => {
            write_frame_header_modular_xyb_blend(has_alpha, last, layout, w)
        }
        ModularFrameKind::ReferenceOnly { width, height } => {
            w.write(1, 0); // all_default = false
            w.write(2, 0b10); // reference-only frame
            w.write(1, 1); // Modular
            write_u64(0, w); // flags
            w.write(1, 0); // color transform = None
            w.write(2, 0); // upsampling = 1
            if has_alpha {
                w.write(2, 0); // extra-channel upsampling = 1
            }
            w.write(2, layout.shift as u64);
            // Reference-only frames do not serialize Passes.
            w.write(1, 1); // custom size
            write_frame_dimension(width, w);
            write_frame_dimension(height, w);
            // No blending and no is_last for reference-only frames.
            w.write(2, PATCH_REF_ID as u64); // save_as_reference = 3
            w.write(1, 1); // save_before_color_transform = true
            w.write(2, 0); // empty name
            w.write(1, 0); // loop filter not all-default
            w.write(1, 0); // no gaborish
            w.write(2, 0); // no EPF
            w.write(2, 0); // no loop-filter extensions
            w.write(2, 0); // no frame-header extensions
        }
    }
}

/// Frame header of the ink frame: a regular modular frame of the lossy
/// (`xyb_encoded`) codestream, kBlend over reference slot 0 with extra
/// channel 0 as its alpha, for the color channels and the alpha itself.
fn write_frame_header_modular_xyb_blend(
    has_alpha: bool,
    last: bool,
    layout: GroupLayout,
    w: &mut BitWriter,
) {
    w.write(1, 0); // all_default = false
    w.write(2, 0b00); // regular frame
    w.write(1, 1); // encoding = Modular
    write_u64(0, w); // flags
    // No do_YCbCr bit: with xyb_encoded set the color transform is XYB.
    w.write(2, 0b00); // upsampling = 1
    if has_alpha {
        w.write(2, 0b00); // ec_upsampling[0] = 1
    }
    w.write(2, layout.shift as u64);
    w.write(2, 0b00); // num_passes = 1
    w.write(1, 0); // have_crop = false
    for _ in 0..1 + usize::from(has_alpha) {
        w.write(2, 2); // mode = kBlend
        w.write(2, 0); // alpha_channel = 0
        w.write(1, 0); // clamp = false
        w.write(2, 0); // source = 0
    }
    if last {
        w.write(1, 1); // is_last
    } else {
        w.write(1, 0); // not the last frame
        // The blended canvas replaces reference slot 0 for the next ink
        // frame; kBlend does not reset the canvas, so no save_before_ct bit.
        w.write(2, 0); // save_as_reference = 0
    }
    w.write(2, 0b00); // name length = 0
    w.write(1, 0); // loop_filter NOT all_default
    w.write(1, 0); // no gaborish
    w.write(2, 0); // 0 EPF iters
    w.write(2, 0b00); // no LF extensions
    w.write(2, 0b00); // no FH extensions
}

/// Frame header for a modular reference-only frame inside the lossy
/// (`xyb_encoded`) codestream. Identical to the lossless variant except that
/// the container sets `xyb_encoded`, so the do_YCbCr bit is absent and the
/// frame's color transform is implicitly XYB. Saved to the modular slot so a
/// hybrid plan can also emit the VarDCT atlas in slot 3.
pub(super) fn write_frame_header_modular_xyb_reference(
    width: usize,
    height: usize,
    slot: u32,
    has_alpha: bool,
    layout: GroupLayout,
    w: &mut BitWriter,
) {
    w.write(1, 0); // all_default = false
    w.write(2, 0b10); // reference-only frame
    w.write(1, 1); // encoding = Modular
    write_u64(0, w); // flags
    // No do_YCbCr bit: with xyb_encoded set the color transform is XYB.
    w.write(2, 0); // upsampling = 1
    if has_alpha {
        w.write(2, 0); // ec_upsampling[0] = 1
    }
    w.write(2, layout.shift as u64);
    // Reference-only frames do not serialize Passes.
    w.write(1, 1); // custom size
    write_frame_dimension(width, w);
    write_frame_dimension(height, w);
    // No blending and no is_last for reference-only frames.
    w.write(2, slot as u64); // save_as_reference
    w.write(1, 1); // save_before_color_transform = true
    w.write(2, 0); // empty name
    // Loop filter off: the atlas is stored exactly as coded; gaborish or EPF
    // would smear the glyph edges patches exist to preserve.
    w.write(1, 0); // loop filter not all-default
    w.write(1, 0); // no gaborish
    w.write(2, 0); // no EPF
    w.write(2, 0); // no loop-filter extensions
    w.write(2, 0); // no frame-header extensions
}

/// Frame header of a lossless layer frame: the palette base saved to
/// reference slot 0 (not last), or the coverage frame blended over it
/// (kBlend with extra channel 0 as alpha, last).
fn write_frame_header_modular_layer(
    has_alpha: bool,
    blend_last: bool,
    layout: GroupLayout,
    w: &mut BitWriter,
) {
    w.write(1, 0); // all_default = false
    w.write(2, 0b00); // regular frame
    w.write(1, 1); // encoding = Modular
    write_u64(0, w); // flags
    w.write(1, 0); // do_ycbcr = false
    write_upsampling(has_alpha, layout, w);
    w.write(2, layout.shift as u64);
    w.write(2, 0b00); // num_passes = 1
    w.write(1, 0); // have_crop = false
    if blend_last {
        for _ in 0..1 + usize::from(has_alpha) {
            w.write(2, 2); // mode = kBlend
            w.write(2, 0); // alpha_channel = 0
            w.write(1, 0); // clamp = false
            w.write(2, 0); // source = 0
        }
        w.write(1, 1); // is_last
    } else {
        w.write(2, 0b00); // blending = Replace
        if has_alpha {
            w.write(2, 0b00); // ec blending = Replace
        }
        w.write(1, 0); // not the last frame
        w.write(2, 0); // save_as_reference = 0
        w.write(1, 0); // save_before_color_transform = false
    }
    w.write(2, 0b00); // name length = 0
    w.write(1, 0); // loop_filter NOT all_default
    w.write(1, 0); // no gaborish
    w.write(2, 0); // 0 EPF iters
    w.write(2, 0b00); // no LF extensions
    w.write(2, 0b00); // no FH extensions
}

/// The frame's `upsampling` and, with alpha, the matching `ec_upsampling`
/// (the extra channel is coded on the same grid, which the codestream
/// requires to be no finer than the color one).
fn write_upsampling(has_alpha: bool, layout: GroupLayout, w: &mut BitWriter) {
    w.write(2, layout.upsampling_selector()); // upsampling: U32(1, 2, 4, 8)
    if has_alpha {
        w.write(2, layout.upsampling_selector()); // ec_upsampling[0]
    }
}

pub(super) fn write_frame_header_modular(has_alpha: bool, layout: GroupLayout, w: &mut BitWriter) {
    write_frame_header_modular_flags(has_alpha, 0, false, layout, w);
}

/// Frame header of a region frame: a regular modular frame cropped to
/// `rect` (x0, y0, width, height), kReplace over reference slot 0; the
/// composite goes back to slot 0 unless this is the last frame. A partial
/// frame always signals its source and never `save_before_color_transform`.
fn write_frame_header_modular_crop(
    has_alpha: bool,
    xyb_encoded: bool,
    rect: [usize; 4],
    layout: GroupLayout,
    last: bool,
    w: &mut BitWriter,
) {
    w.write(1, 0); // all_default = false
    w.write(2, 0b00); // regular frame
    w.write(1, 1); // encoding = Modular
    write_u64(0, w); // flags
    if !xyb_encoded {
        // With xyb_encoded set the color transform is XYB and the bit is absent.
        w.write(1, 0); // do_ycbcr = false
    }
    // The crop's origin and size are in image coordinates whatever the
    // frame's upsampling: the decoder codes the frame at the size over the
    // factor and scales it back onto the rectangle.
    write_upsampling(has_alpha, layout, w);
    w.write(2, layout.shift as u64);
    w.write(2, 0b00); // num_passes = 1
    w.write(1, 1); // have_crop
    write_frame_dimension(2 * rect[0], w); // PackSigned(x0)
    write_frame_dimension(2 * rect[1], w);
    write_frame_dimension(rect[2], w);
    write_frame_dimension(rect[3], w);
    for _ in 0..1 + usize::from(has_alpha) {
        w.write(2, 0b00); // blending = Replace
        w.write(2, 0); // source = 0
    }
    if last {
        w.write(1, 1); // is_last
    } else {
        w.write(1, 0); // not the last frame
        w.write(2, 0); // save_as_reference = 0
    }
    w.write(2, 0b00); // name length = 0
    w.write(1, 0); // loop_filter NOT all_default
    w.write(1, 0); // no gaborish
    w.write(2, 0); // 0 EPF iters
    w.write(2, 0b00); // no LF extensions
    w.write(2, 0b00); // no FH extensions
}

/// Frame header of a cell layer: the last frame, a crop at `rect` (x0, y0,
/// width, height) in image pixels coded at that size over the layout's
/// upsampling, kBlend over reference slot 0 with extra channel 0 (the
/// coverage, coded on the same grid) as its alpha.
fn write_frame_header_modular_blend_crop(rect: [usize; 4], layout: GroupLayout, w: &mut BitWriter) {
    w.write(1, 0); // all_default = false
    w.write(2, 0b00); // regular frame
    w.write(1, 1); // encoding = Modular
    write_u64(0, w); // flags
    w.write(1, 0); // do_ycbcr = false
    write_upsampling(true, layout, w);
    w.write(2, layout.shift as u64);
    w.write(2, 0b00); // num_passes = 1
    w.write(1, 1); // have_crop
    write_frame_dimension(2 * rect[0], w); // PackSigned(x0)
    write_frame_dimension(2 * rect[1], w);
    write_frame_dimension(rect[2], w);
    write_frame_dimension(rect[3], w);
    for _ in 0..2 {
        w.write(2, 2); // mode = kBlend
        w.write(2, 0); // alpha_channel = 0
        w.write(1, 0); // clamp = false
        w.write(2, 0); // source = 0
    }
    w.write(1, 1); // is_last
    w.write(2, 0b00); // name length = 0
    w.write(1, 0); // loop_filter NOT all_default
    w.write(1, 0); // no gaborish
    w.write(2, 0); // 0 EPF iters
    w.write(2, 0b00); // no LF extensions
    w.write(2, 0b00); // no FH extensions
}

fn write_frame_header_modular_flags(
    has_alpha: bool,
    flags: u64,
    saved_first: bool,
    layout: GroupLayout,
    w: &mut BitWriter,
) {
    w.write(1, 0); // all_default = false
    w.write(2, 0b00); // regular frame
    w.write(1, 1); // encoding = Modular
    write_u64(flags, w);
    w.write(1, 0); // do_ycbcr = false   (xyb_encoded=0 so this is serialized)
    write_upsampling(has_alpha, layout, w);
    w.write(2, layout.shift as u64);
    w.write(2, 0b00); // num_passes = 1
    w.write(1, 0); // have_crop = false
    w.write(2, 0b00); // blending = Replace
    if has_alpha {
        w.write(2, 0b00);
    }
    if saved_first {
        w.write(1, 0); // not the last frame
        w.write(2, 0); // save_as_reference = 0
        w.write(1, 0); // save_before_color_transform = false
    } else {
        w.write(1, 1); // is_last
    }
    w.write(2, 0b00); // name length = 0
    w.write(1, 0); // loop_filter NOT all_default
    w.write(1, 0); // no gaborish
    w.write(2, 0); // 0 EPF iters
    w.write(2, 0b00); // no LF extensions
    w.write(2, 0b00); // no FH extensions
}

pub(crate) fn write_patch_dictionary(
    references: &[PatchReference],
    has_alpha: bool,
    scratch: &mut CoderScratch,
    w: &mut BitWriter,
) {
    write_patch_dictionary_with_pool(references, has_alpha, scratch, w, None);
}

pub(crate) fn write_patch_dictionary_with_pool(
    references: &[PatchReference],
    has_alpha: bool,
    scratch: &mut CoderScratch,
    w: &mut BitWriter,
    pool: Option<&crate::thread_pool::ThreadPool>,
) {
    let given = encode_patch_dictionary(&patch_dictionary_tokens(references, has_alpha), scratch);
    // Occurrences are coded as deltas from the previous one; race a priced
    // shortest-path order against the given one on the real stream.
    if let Some(reordered) = reorder_occurrences(references, pool.map(|pool| (pool, &mut *scratch)))
    {
        let candidate =
            encode_patch_dictionary(&patch_dictionary_tokens(&reordered, has_alpha), scratch);
        if candidate.bits_written() < given.bits_written() {
            w.append(&candidate);
            return;
        }
    }
    w.append(&given);
}

const PATCH_POSITION: u32 = 4;
const PATCH_OFFSET: u32 = 6;

fn patch_dictionary_tokens(references: &[PatchReference], has_alpha: bool) -> Vec<Token> {
    const NUM_REF: u32 = 0;
    const REFERENCE_FRAME: u32 = 1;
    const PATCH_SIZE: u32 = 2;
    const REF_POSITION: u32 = 3;
    const BLEND_MODE: u32 = 5;
    const COUNT: u32 = 7;
    const CLAMP: u32 = 9;

    let mut tokens = Vec::new();
    tokens.push(Token::new(NUM_REF, references.len() as u32));
    for reference in references {
        tokens.push(Token::new(REFERENCE_FRAME, reference.ref_frame));
        tokens.push(Token::new(REF_POSITION, reference.atlas_x as u32));
        tokens.push(Token::new(REF_POSITION, reference.atlas_y as u32));
        tokens.push(Token::new(PATCH_SIZE, (reference.width - 1) as u32));
        tokens.push(Token::new(PATCH_SIZE, (reference.height - 1) as u32));
        tokens.push(Token::new(COUNT, (reference.positions.len() - 1) as u32));
        for (i, &(x, y)) in reference.positions.iter().enumerate() {
            if i == 0 {
                tokens.push(Token::new(PATCH_POSITION, x as u32));
                tokens.push(Token::new(PATCH_POSITION, y as u32));
            } else {
                let (px, py) = reference.positions[i - 1];
                tokens.push(Token::new(PATCH_OFFSET, pack_signed(x as i32 - px as i32)));
                tokens.push(Token::new(PATCH_OFFSET, pack_signed(y as i32 - py as i32)));
            }
            // color = BlendAbove (4), Mul (3), Add (2) or Replace (1); Mul and
            // BlendAbove also signal clamp = false. BlendAbove takes the alpha
            // of the only extra channel (glyph templates run on RGB + alpha).
            let alpha_only = reference.alpha_only;
            tokens.push(Token::new(
                BLEND_MODE,
                if alpha_only {
                    0
                } else if reference.mul && !reference.add {
                    4
                } else if reference.mul {
                    3
                } else if reference.add {
                    2
                } else {
                    1
                },
            ));
            if reference.mul {
                tokens.push(Token::new(CLAMP, 0));
            }
            if has_alpha {
                tokens.push(Token::new(BLEND_MODE, if alpha_only { 1 } else { 0 }));
            }
        }
    }
    tokens
}

fn encode_patch_dictionary(tokens: &[Token], scratch: &mut CoderScratch) -> BitWriter {
    // Dense text makes the dictionary a real share of the frame (a third of
    // a text page): the prefix code raced against config-selected codes in
    // both modes by exact bits.
    crate::entropy::raced_token_stream(tokens, NUM_PATCH_CONTEXTS, &mut scratch.huffman_pool)
}

/// Whether the occurrences of `reference` may be applied in any order: Add
/// and Mul commute, Replace only while no two of its boxes overlap (the
/// decoder keeps entry order, so only same-entry pairs can swap).
fn occurrences_commute(reference: &PatchReference) -> bool {
    if reference.add || reference.mul {
        return true;
    }
    let mut boxes = reference.positions.clone();
    boxes.shared_sort_unstable_by_key(|&(x, y)| (y, x));
    for (k, &(x, y)) in boxes.iter().enumerate() {
        for &(ox, oy) in &boxes[k + 1..] {
            if oy >= y + reference.height {
                break;
            }
            if ox.abs_diff(x) < reference.width {
                return false;
            }
        }
    }
    true
}

/// Occurrence orders minimizing the priced position and offset tokens, or
/// `None` when no entry may move or none moved.
fn reorder_occurrences(
    references: &[PatchReference],
    workers: Option<(&crate::thread_pool::ThreadPool, &mut CoderScratch)>,
) -> Option<Vec<PatchReference>> {
    let movable: Vec<bool> = references
        .iter()
        .map(|r| r.positions.len() > 1 && occurrences_commute(r))
        .collect();
    if !movable.contains(&true) {
        return None;
    }
    let points: Vec<Vec<path_order::Pos>> = references
        .iter()
        .map(|r| {
            r.positions
                .iter()
                .map(|&(x, y)| (x as i32, y as i32))
                .collect()
        })
        .collect();
    let given = points.iter().map(|p| (0..p.len()).collect()).collect();
    let orders = path_order::order_sets(&points, &movable, given, false, workers);
    if orders
        .iter()
        .all(|order| order.iter().enumerate().all(|(k, &i)| k == i))
    {
        return None;
    }
    Some(
        references
            .iter()
            .zip(&orders)
            .map(|(reference, order)| PatchReference {
                positions: order.iter().map(|&i| reference.positions[i]).collect(),
                ..reference.clone()
            })
            .collect(),
    )
}

pub(super) fn write_modular_transforms(nb_chans: usize, rct_type: u32, w: &mut BitWriter) {
    if nb_chans >= 3 {
        // transforms count u2S(0, 1, Bits(4)+2, Bits(8)+18): selector 1 = Val(1) → 1 transform.
        w.write(2, 0b01);
        write_rct_transform(rct_type, w);
    } else {
        w.write(2, 0b00); // 0 transforms
    }
}

pub(crate) fn write_toc_entry(byte_len: usize, w: &mut BitWriter) {
    static OFFSETS: [usize; 4] = [0, 1024, 17_408, 4_211_712];
    static BITS: [usize; 4] = [10, 14, 22, 30];
    let mut bucket = 0usize;
    while bucket < 3 && byte_len >= OFFSETS[bucket + 1] {
        bucket += 1;
    }
    w.write(2, bucket as u64);
    w.write(BITS[bucket], (byte_len - OFFSETS[bucket]) as u64);
}

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

    fn entry(positions: Vec<(usize, usize)>, add: bool) -> PatchReference {
        PatchReference {
            atlas_x: 0,
            atlas_y: 0,
            width: 8,
            height: 10,
            ref_frame: PATCH_REF_ID,
            alpha_only: false,
            add,
            mul: false,
            positions,
        }
    }

    fn scattered(n: usize) -> Vec<(usize, usize)> {
        // Boxes on a 20x20 lattice in a scrambled order never overlap.
        (0..n)
            .map(|k| (k * 37 % n, k))
            .map(|(a, _)| ((a % 25) * 20, (a / 25) * 20))
            .collect()
    }

    #[test]
    fn alpha_patch_keeps_color_patch_operations_independent() {
        let color = entry(vec![(0, 0)], false);
        let mut alpha = entry(vec![(20, 0)], false);
        alpha.alpha_only = true;
        let tokens = patch_dictionary_tokens(&[color, alpha], true);
        let modes: Vec<_> = tokens
            .iter()
            .filter(|t| t.context == 5)
            .map(|t| t.value)
            .collect();
        assert_eq!(modes, [1, 0, 0, 1]); // color Replace/A None; color None/A Replace
    }

    #[test]
    fn replace_occurrences_commute_only_without_overlap() {
        assert!(occurrences_commute(&entry(
            vec![(0, 0), (8, 0), (0, 10)],
            false
        )));
        assert!(!occurrences_commute(&entry(
            vec![(0, 0), (40, 3), (7, 9)],
            false
        )));
        assert!(occurrences_commute(&entry(
            vec![(0, 0), (40, 3), (7, 9)],
            true
        )));
    }

    #[test]
    fn reorder_moves_only_commuting_entries_and_keeps_their_occurrences() {
        let overlapping = entry(vec![(300, 0), (0, 0), (150, 0), (5, 5)], false);
        let free = entry(scattered(200), false);
        let reordered = reorder_occurrences(&[overlapping.clone(), free.clone()], None).unwrap();
        assert_eq!(reordered[0].positions, overlapping.positions);
        assert_ne!(reordered[1].positions, free.positions);
        let mut a = reordered[1].positions.clone();
        let mut b = free.positions.clone();
        a.shared_sort();
        b.shared_sort();
        assert_eq!(a, b);
    }

    #[test]
    fn dictionary_is_never_larger_than_in_given_order() {
        let mut scratch = CoderScratch::default();
        let references = [entry(scattered(200), false), entry(scattered(50), true)];
        let given =
            encode_patch_dictionary(&patch_dictionary_tokens(&references, false), &mut scratch);
        let mut w = BitWriter::new();
        write_patch_dictionary(&references, false, &mut scratch, &mut w);
        assert!(w.bits_written() < given.bits_written());
    }

    #[test]
    fn parallel_dictionary_matches_serial_with_shared_prices() {
        let pool = crate::thread_pool::ThreadPool::new_lossless(4);
        let references = [
            entry(scattered(823), true),
            entry(scattered(701), true),
            entry(scattered(57), false),
            entry(vec![(0, 0), (1, 0), (0, 1)], false),
        ];
        let mut scratch = CoderScratch::lossless();
        let serial_order = reorder_occurrences(&references, None).unwrap();
        let parallel_order = reorder_occurrences(&references, Some((&pool, &mut scratch))).unwrap();
        for (a, b) in serial_order.iter().zip(&parallel_order) {
            assert_eq!(a.positions, b.positions);
        }
        let mut serial = BitWriter::new();
        let mut parallel = BitWriter::new();
        write_patch_dictionary(&references, false, &mut scratch, &mut serial);
        write_patch_dictionary_with_pool(
            &references,
            false,
            &mut scratch,
            &mut parallel,
            Some(&pool),
        );
        assert_eq!(serial.bits_written(), parallel.bits_written());
        serial.zero_pad_to_byte();
        parallel.zero_pad_to_byte();
        assert_eq!(serial.into_bytes(), parallel.into_bytes());
    }
}