webp-rust 0.2.1

Pure Rust implementation of a WebP encoder and decoder
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
//! Huffman coding and image-stream emission for lossless encoding.

use super::plans::*;
use super::tokens::*;
use super::*;

/// Internal helper for prefix encode.
pub(super) fn prefix_encode(value: usize) -> Result<PrefixCode, EncoderError> {
    if value == 0 {
        return Err(EncoderError::InvalidParam("prefix value must be non-zero"));
    }

    if value <= 4 {
        return Ok(PrefixCode {
            symbol: value - 1,
            extra_bits: 0,
            extra_value: 0,
        });
    }

    let value = value - 1;
    let highest_bit = usize::BITS as usize - 1 - value.leading_zeros() as usize;
    let second_highest_bit = (value >> (highest_bit - 1)) & 1;
    let extra_bits = highest_bit - 1;
    let extra_value = value & ((1usize << extra_bits) - 1);

    Ok(PrefixCode {
        symbol: 2 * highest_bit + second_highest_bit,
        extra_bits,
        extra_value,
    })
}

/// Internal helper for distance to plane code.
pub(super) fn distance_to_plane_code(width: usize, distance: usize) -> usize {
    let yoffset = distance / width;
    let xoffset = distance - yoffset * width;

    if xoffset <= 8 && yoffset < 8 {
        PLANE_TO_CODE_LUT[yoffset * 16 + 8 - xoffset] as usize + 1
    } else if xoffset > width.saturating_sub(8) && yoffset < 7 {
        PLANE_TO_CODE_LUT[(yoffset + 1) * 16 + 8 + (width - xoffset)] as usize + 1
    } else {
        distance + 120
    }
}

/// Writes simple huffman tree.
pub(super) fn write_simple_huffman_tree(
    bw: &mut BitWriter,
    symbols: &[usize],
) -> Result<(), EncoderError> {
    if symbols.is_empty() || symbols.len() > 2 {
        return Err(EncoderError::InvalidParam(
            "simple Huffman tree expects one or two symbols",
        ));
    }

    for &symbol in symbols {
        if symbol >= (1 << 8) {
            return Err(EncoderError::InvalidParam(
                "simple Huffman symbol is too large",
            ));
        }
    }

    bw.put_bits(1, 1)?;
    bw.put_bits((symbols.len() - 1) as u32, 1)?;

    let first = symbols[0];
    if first <= 1 {
        bw.put_bits(0, 1)?;
        bw.put_bits(first as u32, 1)?;
    } else {
        bw.put_bits(1, 1)?;
        bw.put_bits(first as u32, 8)?;
    }

    if let Some(&second) = symbols.get(1) {
        bw.put_bits(second as u32, 8)?;
    }

    Ok(())
}

/// Writes trimmed length.
pub(super) fn write_trimmed_length(
    bw: &mut BitWriter,
    trimmed_length: usize,
) -> Result<(), EncoderError> {
    if trimmed_length < 2 {
        return Err(EncoderError::Bitstream("trimmed Huffman span is too small"));
    }
    if trimmed_length == 2 {
        bw.put_bits(0, 5)?;
        return Ok(());
    }

    let nbits = usize::BITS as usize - 1 - (trimmed_length - 2).leading_zeros() as usize;
    let nbitpairs = nbits / 2 + 1;
    if nbitpairs > 8 {
        return Err(EncoderError::Bitstream("trimmed Huffman span is too large"));
    }
    bw.put_bits((nbitpairs - 1) as u32, 3)?;
    bw.put_bits((trimmed_length - 2) as u32, nbitpairs * 2)
}

/// Writes huffman tree.
pub(super) fn write_huffman_tree(
    bw: &mut BitWriter,
    code: &HuffmanCode,
) -> Result<(), EncoderError> {
    let symbols = code.used_symbols();
    if symbols.is_empty() {
        return Err(EncoderError::Bitstream("empty Huffman tree"));
    }
    if symbols.len() <= 2 && symbols.iter().all(|&symbol| symbol < (1 << 8)) {
        return write_simple_huffman_tree(bw, &symbols);
    }

    bw.put_bits(0, 1)?;
    let tokens = compress_huffman_tree(code.code_lengths());

    let mut token_histogram = vec![0u32; NUM_CODE_LENGTH_CODES];
    for token in &tokens {
        token_histogram[token.code as usize] += 1;
    }
    let token_code = HuffmanCode::from_histogram(&token_histogram, 7)?;

    let code_length_bitdepth = token_code.code_lengths();
    let mut codes_to_store = NUM_CODE_LENGTH_CODES;
    while codes_to_store > 4
        && code_length_bitdepth[CODE_LENGTH_CODE_ORDER[codes_to_store - 1]] == 0
    {
        codes_to_store -= 1;
    }
    bw.put_bits((codes_to_store - 4) as u32, 4)?;
    for &ordered_symbol in CODE_LENGTH_CODE_ORDER.iter().take(codes_to_store) {
        bw.put_bits(code_length_bitdepth[ordered_symbol] as u32, 3)?;
    }

    let mut trailing_zero_bits = 0usize;
    let mut trimmed_length = tokens.len();
    let mut index = tokens.len();
    while index > 0 {
        index -= 1;
        let token = tokens[index];
        if token.code == 0 || token.code == 17 || token.code == 18 {
            trimmed_length -= 1;
            trailing_zero_bits += code_length_bitdepth[token.code as usize] as usize;
            if token.code == 17 {
                trailing_zero_bits += 3;
            } else if token.code == 18 {
                trailing_zero_bits += 7;
            }
        } else {
            break;
        }
    }

    let write_trimmed = trimmed_length > 1 && trailing_zero_bits > 12;
    bw.put_bits(write_trimmed as u32, 1)?;
    let length = if write_trimmed {
        write_trimmed_length(bw, trimmed_length)?;
        trimmed_length
    } else {
        tokens.len()
    };

    for token in tokens.iter().take(length) {
        token_code.write_symbol(bw, token.code as usize)?;
        match token.code {
            16 => bw.put_bits(token.extra_bits as u32, 2)?,
            17 => bw.put_bits(token.extra_bits as u32, 3)?,
            18 => bw.put_bits(token.extra_bits as u32, 7)?,
            _ => {}
        }
    }

    Ok(())
}

/// Builds histograms.
pub(super) fn build_histograms(
    tokens: &[Token],
    width: usize,
    color_cache_bits: usize,
) -> Result<HistogramSet, EncoderError> {
    let mut histograms = new_histograms(color_cache_bits);
    for &token in tokens {
        add_token_to_histograms(&mut histograms, width, token)?;
    }
    normalize_histograms(&mut histograms);
    Ok(histograms)
}

/// Internal helper for new histograms.
pub(super) fn new_histograms(color_cache_bits: usize) -> HistogramSet {
    [
        vec![
            0u32;
            NUM_LITERAL_CODES
                + NUM_LENGTH_CODES
                + if color_cache_bits > 0 {
                    1usize << color_cache_bits
                } else {
                    0
                }
        ],
        vec![0u32; NUM_LITERAL_CODES],
        vec![0u32; NUM_LITERAL_CODES],
        vec![0u32; NUM_LITERAL_CODES],
        vec![0u32; NUM_DISTANCE_CODES],
    ]
}

/// Internal helper for add token to histograms.
pub(super) fn add_token_to_histograms(
    histograms: &mut HistogramSet,
    width: usize,
    token: Token,
) -> Result<(), EncoderError> {
    match token {
        Token::Literal(argb) => {
            histograms[0][((argb >> 8) & 0xff) as usize] += 1;
            histograms[1][((argb >> 16) & 0xff) as usize] += 1;
            histograms[2][(argb & 0xff) as usize] += 1;
            histograms[3][((argb >> 24) & 0xff) as usize] += 1;
        }
        Token::Cache(key) => {
            histograms[0][NUM_LITERAL_CODES + NUM_LENGTH_CODES + key] += 1;
        }
        Token::Copy { distance, length } => {
            let length_prefix = prefix_encode(length)?;
            histograms[0][NUM_LITERAL_CODES + length_prefix.symbol] += 1;

            let plane_code = distance_to_plane_code(width, distance);
            let dist_prefix = prefix_encode(plane_code)?;
            histograms[4][dist_prefix.symbol] += 1;
        }
    }
    Ok(())
}

/// Internal helper for normalize histograms.
pub(super) fn normalize_histograms(histograms: &mut HistogramSet) {
    for histogram in histograms.iter_mut().take(4) {
        if histogram.iter().all(|&count| count == 0) {
            histogram[0] = 1;
        }
    }
    if histograms[4].iter().all(|&count| count == 0) {
        histograms[4][0] = 1;
    }
}

/// Internal helper for merge histograms.
pub(super) fn merge_histograms(dst: &mut HistogramSet, src: &HistogramSet) {
    for (dst_histogram, src_histogram) in dst.iter_mut().zip(src.iter()) {
        for (dst_count, src_count) in dst_histogram.iter_mut().zip(src_histogram.iter()) {
            *dst_count += *src_count;
        }
    }
}

/// Builds group codes.
pub(super) fn build_group_codes(
    histograms: &HistogramSet,
) -> Result<HuffmanGroupCodes, EncoderError> {
    Ok(HuffmanGroupCodes {
        green: HuffmanCode::from_histogram(&histograms[0], 15)?,
        red: HuffmanCode::from_histogram(&histograms[1], 15)?,
        blue: HuffmanCode::from_histogram(&histograms[2], 15)?,
        alpha: HuffmanCode::from_histogram(&histograms[3], 15)?,
        dist: HuffmanCode::from_histogram(&histograms[4], 15)?,
    })
}

/// Returns the number of pixels consumed by a lossless token.
pub(super) fn token_len(token: Token) -> usize {
    match token {
        Token::Copy { length, .. } => length,
        Token::Literal(_) | Token::Cache(_) => 1,
    }
}

/// Maps a token position to its histogram tile index.
pub(super) fn tile_index_for_pos(
    width: usize,
    huffman_bits: usize,
    huffman_xsize: usize,
    pos: usize,
) -> usize {
    let x = pos % width;
    let y = pos / width;
    (y >> huffman_bits) * huffman_xsize + (x >> huffman_bits)
}

/// Internal helper for histogram cost.
pub(super) fn histogram_cost(histograms: &HistogramSet, codes: &HuffmanGroupCodes) -> usize {
    histograms[0]
        .iter()
        .zip(codes.green.code_lengths())
        .map(|(&count, &bits)| count as usize * bits as usize)
        .sum::<usize>()
        + histograms[1]
            .iter()
            .zip(codes.red.code_lengths())
            .map(|(&count, &bits)| count as usize * bits as usize)
            .sum::<usize>()
        + histograms[2]
            .iter()
            .zip(codes.blue.code_lengths())
            .map(|(&count, &bits)| count as usize * bits as usize)
            .sum::<usize>()
        + histograms[3]
            .iter()
            .zip(codes.alpha.code_lengths())
            .map(|(&count, &bits)| count as usize * bits as usize)
            .sum::<usize>()
        + histograms[4]
            .iter()
            .zip(codes.dist.code_lengths())
            .map(|(&count, &bits)| count as usize * bits as usize)
            .sum::<usize>()
}

/// Internal helper for histogram entropy cost.
pub(super) fn histogram_entropy_cost(histogram: &[u32]) -> f64 {
    let total = histogram.iter().map(|&count| count as f64).sum::<f64>();
    if total == 0.0 {
        return 0.0;
    }

    histogram
        .iter()
        .filter(|&&count| count != 0)
        .map(|&count| {
            let count = count as f64;
            count * (total / count).log2()
        })
        .sum()
}

/// Internal helper for histogram signature costs.
pub(super) fn histogram_signature_costs(histograms: &HistogramSet) -> [f64; 3] {
    [
        histogram_entropy_cost(&histograms[0]),
        histogram_entropy_cost(&histograms[1]),
        histogram_entropy_cost(&histograms[2]),
    ]
}

/// Internal helper for histogram set entropy cost.
pub(super) fn histogram_set_entropy_cost(histograms: &HistogramSet) -> f64 {
    histograms
        .iter()
        .map(|histogram| histogram_entropy_cost(histogram))
        .sum()
}

/// Internal helper for histogram merge penalty.
pub(super) fn histogram_merge_penalty(lhs: &HistogramSet, rhs: &HistogramSet) -> f64 {
    let mut merged = lhs.clone();
    merge_histograms(&mut merged, rhs);
    histogram_set_entropy_cost(&merged)
        - histogram_set_entropy_cost(lhs)
        - histogram_set_entropy_cost(rhs)
}

/// Internal helper for histogram partition index.
pub(super) fn histogram_partition_index(
    value: f64,
    min_value: f64,
    max_value: f64,
    partitions: usize,
) -> usize {
    if partitions <= 1 || max_value <= min_value {
        return 0;
    }

    let normalized = ((value - min_value) / (max_value - min_value)).clamp(0.0, 1.0);
    let index = (normalized * partitions as f64) as usize;
    index.min(partitions - 1)
}

/// Internal helper for entropy histogram candidates.
pub(super) fn entropy_histogram_candidates(
    non_empty_tiles: &[(usize, usize)],
    tile_histograms: &[HistogramSet],
    target_count: usize,
) -> Vec<HistogramCandidate> {
    if target_count == 0 || non_empty_tiles.is_empty() {
        return Vec::new();
    }

    let signatures = non_empty_tiles
        .iter()
        .map(|&(tile, _)| histogram_signature_costs(&tile_histograms[tile]))
        .collect::<Vec<_>>();

    let mins = [
        signatures
            .iter()
            .map(|costs| costs[0])
            .fold(f64::INFINITY, f64::min),
        signatures
            .iter()
            .map(|costs| costs[1])
            .fold(f64::INFINITY, f64::min),
        signatures
            .iter()
            .map(|costs| costs[2])
            .fold(f64::INFINITY, f64::min),
    ];
    let maxs = [
        signatures
            .iter()
            .map(|costs| costs[0])
            .fold(f64::NEG_INFINITY, f64::max),
        signatures
            .iter()
            .map(|costs| costs[1])
            .fold(f64::NEG_INFINITY, f64::max),
        signatures
            .iter()
            .map(|costs| costs[2])
            .fold(f64::NEG_INFINITY, f64::max),
    ];

    let bin_count = NUM_HISTOGRAM_PARTITIONS * NUM_HISTOGRAM_PARTITIONS * NUM_HISTOGRAM_PARTITIONS;
    let mut bins = vec![None::<HistogramCandidate>; bin_count];

    for (&(tile, weight), signature) in non_empty_tiles.iter().zip(signatures.iter()) {
        let green_bin =
            histogram_partition_index(signature[0], mins[0], maxs[0], NUM_HISTOGRAM_PARTITIONS);
        let red_bin =
            histogram_partition_index(signature[1], mins[1], maxs[1], NUM_HISTOGRAM_PARTITIONS);
        let blue_bin =
            histogram_partition_index(signature[2], mins[2], maxs[2], NUM_HISTOGRAM_PARTITIONS);
        let bin_index = green_bin * NUM_HISTOGRAM_PARTITIONS * NUM_HISTOGRAM_PARTITIONS
            + red_bin * NUM_HISTOGRAM_PARTITIONS
            + blue_bin;

        match &mut bins[bin_index] {
            Some(candidate) => {
                merge_histograms(&mut candidate.histograms, &tile_histograms[tile]);
                candidate.weight += weight;
            }
            slot @ None => {
                let mut histograms = tile_histograms[tile].clone();
                normalize_histograms(&mut histograms);
                *slot = Some(HistogramCandidate { histograms, weight });
            }
        }
    }

    let mut candidates = bins.into_iter().flatten().collect::<Vec<_>>();
    if candidates.is_empty() {
        return Vec::new();
    }

    while candidates.len() > target_count {
        let mut best_pair = None;
        let mut best_penalty = f64::INFINITY;
        for lhs in 0..candidates.len() {
            for rhs in lhs + 1..candidates.len() {
                let penalty = histogram_merge_penalty(
                    &candidates[lhs].histograms,
                    &candidates[rhs].histograms,
                );
                if penalty < best_penalty {
                    best_penalty = penalty;
                    best_pair = Some((lhs, rhs));
                }
            }
        }

        let Some((lhs, rhs)) = best_pair else {
            break;
        };
        let rhs_candidate = candidates.swap_remove(rhs);
        merge_histograms(&mut candidates[lhs].histograms, &rhs_candidate.histograms);
        normalize_histograms(&mut candidates[lhs].histograms);
        candidates[lhs].weight += rhs_candidate.weight;
    }

    candidates
}

/// Builds entropy seed histograms.
pub(super) fn build_entropy_seed_histograms(
    non_empty_tiles: &[(usize, usize)],
    tile_histograms: &[HistogramSet],
    group_count: usize,
) -> Vec<HistogramSet> {
    let mut candidates =
        entropy_histogram_candidates(non_empty_tiles, tile_histograms, group_count);
    candidates.sort_by(|lhs, rhs| rhs.weight.cmp(&lhs.weight));
    candidates
        .into_iter()
        .take(group_count)
        .map(|candidate| candidate.histograms)
        .collect()
}

/// Builds weighted seed histograms.
pub(super) fn build_weighted_seed_histograms(
    non_empty_tiles: &[(usize, usize)],
    tile_histograms: &[HistogramSet],
    group_count: usize,
) -> Vec<HistogramSet> {
    non_empty_tiles
        .iter()
        .take(group_count)
        .map(|&(tile, _)| {
            let mut histograms = tile_histograms[tile].clone();
            normalize_histograms(&mut histograms);
            histograms
        })
        .collect()
}

/// Internal helper for assign tiles to groups.
pub(super) fn assign_tiles_to_groups(
    non_empty_tiles: &[(usize, usize)],
    tile_histograms: &[HistogramSet],
    group_codes: &[HuffmanGroupCodes],
    assignments: &mut [usize],
) {
    for &(tile, _) in non_empty_tiles {
        let mut best_group = 0usize;
        let mut best_cost = usize::MAX;
        for (group_index, codes) in group_codes.iter().enumerate() {
            let cost = histogram_cost(&tile_histograms[tile], codes);
            if cost < best_cost {
                best_cost = cost;
                best_group = group_index;
            }
        }
        assignments[tile] = best_group;
    }
}

/// Internal helper for refine meta huffman plan.
pub(super) fn refine_meta_huffman_plan(
    tile_count: usize,
    color_cache_bits: usize,
    non_empty_tiles: &[(usize, usize)],
    tile_histograms: &[HistogramSet],
    seed_histograms: Vec<HistogramSet>,
) -> Result<Option<MetaHuffmanPlan>, EncoderError> {
    if seed_histograms.len() <= 1 {
        return Ok(None);
    }

    let mut group_codes = seed_histograms
        .iter()
        .map(build_group_codes)
        .collect::<Result<Vec<_>, _>>()?;
    let mut assignments = vec![0usize; tile_count];

    for _ in 0..4 {
        assign_tiles_to_groups(
            non_empty_tiles,
            tile_histograms,
            &group_codes,
            &mut assignments,
        );

        let mut remap = vec![usize::MAX; group_codes.len()];
        let mut merged_histograms = Vec::new();
        for (group_index, _) in group_codes.iter().enumerate() {
            let mut histograms = new_histograms(color_cache_bits);
            let mut used = false;
            for &(tile, _) in non_empty_tiles {
                if assignments[tile] == group_index {
                    merge_histograms(&mut histograms, &tile_histograms[tile]);
                    used = true;
                }
            }
            if used {
                normalize_histograms(&mut histograms);
                remap[group_index] = merged_histograms.len();
                merged_histograms.push(histograms);
            }
        }
        if merged_histograms.len() <= 1 {
            return Ok(None);
        }
        for &(tile, _) in non_empty_tiles {
            assignments[tile] = remap[assignments[tile]];
        }
        group_codes = merged_histograms
            .iter()
            .map(build_group_codes)
            .collect::<Result<Vec<_>, _>>()?;
    }

    Ok(Some(MetaHuffmanPlan {
        huffman_bits: 0,
        huffman_xsize: 0,
        assignments,
        groups: group_codes,
    }))
}

/// Internal helper for meta huffman assignment cost.
pub(super) fn meta_huffman_assignment_cost(
    non_empty_tiles: &[(usize, usize)],
    tile_histograms: &[HistogramSet],
    plan: &MetaHuffmanPlan,
) -> usize {
    non_empty_tiles
        .iter()
        .map(|&(tile, _)| {
            histogram_cost(&tile_histograms[tile], &plan.groups[plan.assignments[tile]])
        })
        .sum()
}

/// Applies color cache to tokens.
pub(super) fn apply_color_cache_to_tokens(
    argb: &[u32],
    tokens: &[Token],
    color_cache_bits: usize,
) -> Result<Vec<Token>, EncoderError> {
    if color_cache_bits == 0 {
        return Ok(tokens.to_vec());
    }

    let mut cache = ColorCache::new(color_cache_bits)?;
    let mut cached_tokens = Vec::with_capacity(tokens.len());
    let mut pixel_index = 0usize;

    for &token in tokens {
        match token {
            Token::Literal(pixel) => {
                if let Some(key) = cache.lookup(pixel) {
                    cached_tokens.push(Token::Cache(key));
                } else {
                    cached_tokens.push(Token::Literal(pixel));
                    cache.insert(pixel);
                }
                pixel_index += 1;
            }
            Token::Cache(key) => {
                cached_tokens.push(Token::Cache(key));
                pixel_index += 1;
            }
            Token::Copy { distance, length } => {
                cached_tokens.push(Token::Copy { distance, length });
                for &pixel in &argb[pixel_index..pixel_index + length] {
                    cache.insert(pixel);
                }
                pixel_index += length;
            }
        }
    }

    Ok(cached_tokens)
}

/// Builds meta huffman plan.
pub(super) fn build_meta_huffman_plan(
    width: usize,
    height: usize,
    tokens: &[Token],
    color_cache_bits: usize,
    huffman_bits: usize,
    max_groups: usize,
) -> Result<Option<MetaHuffmanPlan>, EncoderError> {
    if !(MIN_HUFFMAN_BITS..MIN_HUFFMAN_BITS + (1 << NUM_HUFFMAN_BITS)).contains(&huffman_bits) {
        return Ok(None);
    }

    let huffman_xsize = subsample_size(width, huffman_bits);
    let huffman_ysize = subsample_size(height, huffman_bits);
    let tile_count = huffman_xsize * huffman_ysize;
    if tile_count <= 1 {
        return Ok(None);
    }

    let mut tile_histograms = vec![new_histograms(color_cache_bits); tile_count];
    let mut tile_weights = vec![0usize; tile_count];
    let mut pos = 0usize;
    for &token in tokens {
        let tile = tile_index_for_pos(width, huffman_bits, huffman_xsize, pos);
        add_token_to_histograms(&mut tile_histograms[tile], width, token)?;
        tile_weights[tile] += token_len(token);
        pos += token_len(token);
    }

    let mut non_empty_tiles = tile_weights
        .iter()
        .enumerate()
        .filter_map(|(index, &weight)| (weight != 0).then_some((index, weight)))
        .collect::<Vec<_>>();
    if non_empty_tiles.len() <= 1 {
        return Ok(None);
    }
    non_empty_tiles.sort_by(|lhs, rhs| rhs.1.cmp(&lhs.1));

    let group_count = max_groups.min(non_empty_tiles.len());
    if group_count <= 1 {
        return Ok(None);
    }

    let seed_candidates = vec![
        build_weighted_seed_histograms(&non_empty_tiles, &tile_histograms, group_count),
        build_entropy_seed_histograms(&non_empty_tiles, &tile_histograms, group_count),
    ];

    let mut best_plan = None;
    let mut best_cost = usize::MAX;
    for seed_histograms in seed_candidates {
        if let Some(mut plan) = refine_meta_huffman_plan(
            tile_count,
            color_cache_bits,
            &non_empty_tiles,
            &tile_histograms,
            seed_histograms,
        )? {
            plan.huffman_bits = huffman_bits;
            plan.huffman_xsize = huffman_xsize;
            let cost = meta_huffman_assignment_cost(&non_empty_tiles, &tile_histograms, &plan);
            if cost < best_cost {
                best_cost = cost;
                best_plan = Some(plan);
            }
        }
    }

    Ok(best_plan)
}

/// Writes huffman group.
pub(super) fn write_huffman_group(
    bw: &mut BitWriter,
    group: &HuffmanGroupCodes,
) -> Result<(), EncoderError> {
    write_huffman_tree(bw, &group.green)?;
    write_huffman_tree(bw, &group.red)?;
    write_huffman_tree(bw, &group.blue)?;
    write_huffman_tree(bw, &group.alpha)?;
    write_huffman_tree(bw, &group.dist)
}

/// Writes tokens with meta.
pub(super) fn write_tokens_with_meta(
    bw: &mut BitWriter,
    tokens: &[Token],
    width: usize,
    plan: &MetaHuffmanPlan,
) -> Result<(), EncoderError> {
    let mut pos = 0usize;
    for &token in tokens {
        let tile = tile_index_for_pos(width, plan.huffman_bits, plan.huffman_xsize, pos);
        let group = &plan.groups[plan.assignments[tile]];
        match token {
            Token::Literal(argb) => {
                let green = ((argb >> 8) & 0xff) as usize;
                let red = ((argb >> 16) & 0xff) as usize;
                let blue = (argb & 0xff) as usize;
                let alpha = ((argb >> 24) & 0xff) as usize;

                group.green.write_symbol(bw, green)?;
                group.red.write_symbol(bw, red)?;
                group.blue.write_symbol(bw, blue)?;
                group.alpha.write_symbol(bw, alpha)?;
            }
            Token::Cache(key) => {
                group
                    .green
                    .write_symbol(bw, NUM_LITERAL_CODES + NUM_LENGTH_CODES + key)?;
            }
            Token::Copy { distance, length } => {
                let length_prefix = prefix_encode(length)?;
                group
                    .green
                    .write_symbol(bw, NUM_LITERAL_CODES + length_prefix.symbol)?;
                if length_prefix.extra_bits > 0 {
                    bw.put_bits(length_prefix.extra_value as u32, length_prefix.extra_bits)?;
                }

                let plane_code = distance_to_plane_code(width, distance);
                let dist_prefix = prefix_encode(plane_code)?;
                group.dist.write_symbol(bw, dist_prefix.symbol)?;
                if dist_prefix.extra_bits > 0 {
                    bw.put_bits(dist_prefix.extra_value as u32, dist_prefix.extra_bits)?;
                }
            }
        }
        pos += token_len(token);
    }
    Ok(())
}

/// Writes tokens.
pub(super) fn write_tokens(
    bw: &mut BitWriter,
    tokens: &[Token],
    width: usize,
    green_codes: &HuffmanCode,
    red_codes: &HuffmanCode,
    blue_codes: &HuffmanCode,
    alpha_codes: &HuffmanCode,
    dist_codes: &HuffmanCode,
) -> Result<(), EncoderError> {
    for token in tokens {
        match *token {
            Token::Literal(argb) => {
                let green = ((argb >> 8) & 0xff) as usize;
                let red = ((argb >> 16) & 0xff) as usize;
                let blue = (argb & 0xff) as usize;
                let alpha = ((argb >> 24) & 0xff) as usize;

                green_codes.write_symbol(bw, green)?;
                red_codes.write_symbol(bw, red)?;
                blue_codes.write_symbol(bw, blue)?;
                alpha_codes.write_symbol(bw, alpha)?;
            }
            Token::Cache(key) => {
                green_codes.write_symbol(bw, NUM_LITERAL_CODES + NUM_LENGTH_CODES + key)?;
            }
            Token::Copy { distance, length } => {
                let length_prefix = prefix_encode(length)?;
                green_codes.write_symbol(bw, NUM_LITERAL_CODES + length_prefix.symbol)?;
                if length_prefix.extra_bits > 0 {
                    bw.put_bits(length_prefix.extra_value as u32, length_prefix.extra_bits)?;
                }

                let plane_code = distance_to_plane_code(width, distance);
                let dist_prefix = prefix_encode(plane_code)?;
                dist_codes.write_symbol(bw, dist_prefix.symbol)?;
                if dist_prefix.extra_bits > 0 {
                    bw.put_bits(dist_prefix.extra_value as u32, dist_prefix.extra_bits)?;
                }
            }
        }
    }

    Ok(())
}

/// Writes single group image stream.
pub(super) fn write_single_group_image_stream(
    bw: &mut BitWriter,
    width: usize,
    tokens: &[Token],
    allow_meta_huffman: bool,
    color_cache_bits: usize,
    group: &HuffmanGroupCodes,
) -> Result<(), EncoderError> {
    bw.put_bits((color_cache_bits > 0) as u32, 1)?;
    if color_cache_bits > 0 {
        bw.put_bits(color_cache_bits as u32, 4)?;
    }
    if allow_meta_huffman {
        bw.put_bits(0, 1)?;
    }

    write_huffman_group(bw, group)?;

    write_tokens(
        bw,
        tokens,
        width,
        &group.green,
        &group.red,
        &group.blue,
        &group.alpha,
        &group.dist,
    )
}

/// Writes meta huffman image stream.
pub(super) fn write_meta_huffman_image_stream(
    bw: &mut BitWriter,
    width: usize,
    tokens: &[Token],
    color_cache_bits: usize,
    plan: &MetaHuffmanPlan,
) -> Result<(), EncoderError> {
    bw.put_bits((color_cache_bits > 0) as u32, 1)?;
    if color_cache_bits > 0 {
        bw.put_bits(color_cache_bits as u32, 4)?;
    }
    bw.put_bits(1, 1)?;
    bw.put_bits(
        (plan.huffman_bits - MIN_HUFFMAN_BITS) as u32,
        NUM_HUFFMAN_BITS,
    )?;

    let huffman_image = plan
        .assignments
        .iter()
        .map(|&group| (((group >> 8) as u32) << 16) | (((group & 0xff) as u32) << 8))
        .collect::<Vec<_>>();
    write_image_stream(
        bw,
        plan.huffman_xsize,
        &huffman_image,
        false,
        0,
        TokenBuildOptions {
            color_cache_bits: 0,
            match_chain_depth: 0,
            use_window_offsets: false,
            window_offset_limit: 0,
            lazy_matching: false,
            use_traceback: false,
            traceback_max_candidates: 0,
        },
    )?;

    for group in &plan.groups {
        write_huffman_group(bw, group)?;
    }
    write_tokens_with_meta(bw, tokens, width, plan)
}

/// Writes image stream from tokens.
pub(super) fn write_image_stream_from_tokens(
    bw: &mut BitWriter,
    width: usize,
    height: usize,
    tokens: &[Token],
    emit_meta_huffman_flag: bool,
    entropy_search_level: u8,
    color_cache_bits: usize,
) -> Result<(), EncoderError> {
    let histograms = build_histograms(tokens, width, color_cache_bits)?;
    let group = build_group_codes(&histograms)?;

    let meta_candidates = if emit_meta_huffman_flag {
        meta_huffman_candidates(entropy_search_level, width, height)
    } else {
        &[]
    };
    if !meta_candidates.is_empty() {
        let single_size =
            estimate_single_group_image_stream_size(width, tokens, color_cache_bits, true, &group)?;
        let mut best_meta = None;
        let mut best_meta_size = usize::MAX;
        for &(huffman_bits, group_count) in meta_candidates {
            if let Some(plan) = build_meta_huffman_plan(
                width,
                height,
                tokens,
                color_cache_bits,
                huffman_bits,
                group_count,
            )? {
                let size = estimate_meta_huffman_image_stream_size(
                    width,
                    tokens,
                    color_cache_bits,
                    &plan,
                )?;
                if size < best_meta_size {
                    best_meta_size = size;
                    best_meta = Some(plan);
                }
            }
        }
        if let Some(plan) = best_meta {
            if best_meta_size < single_size {
                return write_meta_huffman_image_stream(bw, width, tokens, color_cache_bits, &plan);
            }
        }
    }

    write_single_group_image_stream(
        bw,
        width,
        tokens,
        emit_meta_huffman_flag,
        color_cache_bits,
        &group,
    )
}

/// Writes image stream.
pub(super) fn write_image_stream(
    bw: &mut BitWriter,
    width: usize,
    argb: &[u32],
    emit_meta_huffman_flag: bool,
    entropy_search_level: u8,
    options: TokenBuildOptions,
) -> Result<(), EncoderError> {
    let tokens = build_tokens(width, argb, options)?;
    write_image_stream_from_tokens(
        bw,
        width,
        argb.len() / width,
        &tokens,
        emit_meta_huffman_flag,
        entropy_search_level,
        options.color_cache_bits,
    )
}

/// Estimates single group image stream size.
pub(super) fn estimate_single_group_image_stream_size(
    width: usize,
    tokens: &[Token],
    color_cache_bits: usize,
    allow_meta_huffman: bool,
    group: &HuffmanGroupCodes,
) -> Result<usize, EncoderError> {
    let mut bw = BitWriter::default();
    write_single_group_image_stream(
        &mut bw,
        width,
        tokens,
        allow_meta_huffman,
        color_cache_bits,
        group,
    )?;
    Ok(bw.into_bytes().len())
}

/// Estimates meta huffman image stream size.
pub(super) fn estimate_meta_huffman_image_stream_size(
    width: usize,
    tokens: &[Token],
    color_cache_bits: usize,
    plan: &MetaHuffmanPlan,
) -> Result<usize, EncoderError> {
    let mut bw = BitWriter::default();
    write_meta_huffman_image_stream(&mut bw, width, tokens, color_cache_bits, plan)?;
    Ok(bw.into_bytes().len())
}

/// Estimates image stream size.
pub(super) fn estimate_image_stream_size(
    width: usize,
    height: usize,
    tokens: &[Token],
    color_cache_bits: usize,
    emit_meta_huffman_flag: bool,
    entropy_search_level: u8,
) -> Result<usize, EncoderError> {
    let mut bw = BitWriter::default();
    write_image_stream_from_tokens(
        &mut bw,
        width,
        height,
        tokens,
        emit_meta_huffman_flag,
        entropy_search_level,
        color_cache_bits,
    )?;
    Ok(bw.into_bytes().len())
}

/// Estimates cache candidate cost.
pub(super) fn estimate_cache_candidate_cost(
    width: usize,
    tokens: &[Token],
    color_cache_bits: usize,
) -> Result<usize, EncoderError> {
    let histograms = build_histograms(tokens, width, color_cache_bits)?;
    let group = build_group_codes(&histograms)?;
    Ok(histogram_cost(&histograms, &group))
}

/// Selects best color cache bits.
pub(super) fn select_best_color_cache_bits(
    width: usize,
    height: usize,
    argb: &[u32],
    base_tokens: &[Token],
    profile: &LosslessSearchProfile,
) -> Result<usize, EncoderError> {
    let max_cache_bits =
        suggested_max_color_cache_bits(argb, max_color_cache_bits_for_profile(profile));
    let shortlist_size = shortlist_color_cache_candidates_for_profile(profile);

    let mut cheap_candidates = Vec::with_capacity(max_cache_bits + 1);
    cheap_candidates.push((
        estimate_cache_candidate_cost(width, base_tokens, 0)?,
        0usize,
    ));
    for cache_bits in 1..=max_cache_bits {
        let tokens = apply_color_cache_to_tokens(argb, base_tokens, cache_bits)?;
        let cost = estimate_cache_candidate_cost(width, &tokens, cache_bits)?;
        cheap_candidates.push((cost, cache_bits));
    }

    cheap_candidates.sort_by_key(|(cost, bits)| (*cost, *bits));
    let mut shortlist = cheap_candidates
        .into_iter()
        .take(shortlist_size.max(1))
        .map(|(_, bits)| bits)
        .collect::<Vec<_>>();
    if !shortlist.contains(&0) {
        shortlist.push(0);
    }

    let mut best_cache_bits = 0usize;
    let mut best_size = usize::MAX;
    for cache_bits in shortlist {
        let cheap_cost = if cache_bits == 0 {
            estimate_cache_candidate_cost(width, base_tokens, 0)?
        } else {
            let tokens = apply_color_cache_to_tokens(argb, base_tokens, cache_bits)?;
            estimate_cache_candidate_cost(width, &tokens, cache_bits)?
        };
        if best_size != usize::MAX
            && should_stop_transform_search(best_size, cheap_cost.div_ceil(8), profile)
        {
            break;
        }
        let size = if cache_bits == 0 {
            estimate_image_stream_size(width, height, base_tokens, 0, false, 0)?
        } else {
            let tokens = build_tokens(
                width,
                argb,
                token_build_options(profile.match_search_level, cache_bits),
            )?;
            estimate_image_stream_size(width, height, &tokens, cache_bits, false, 0)?
        };
        if size < best_size {
            best_size = size;
            best_cache_bits = cache_bits;
        }
    }

    Ok(best_cache_bits)
}