lerc-rs 0.1.0

Pure Rust LERC (Limited Error Raster Compression) 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
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
use alloc::vec;
use alloc::vec::Vec;

use crate::error::{LercError, Result};

pub fn num_bytes_uint(k: u32) -> usize {
    if k < 256 {
        1
    } else if k < (1 << 16) {
        2
    } else {
        4
    }
}

fn num_tail_bytes_not_needed(num_elem: u32, num_bits: u32) -> u32 {
    let num_bits_tail = ((num_elem as u64 * num_bits as u64) & 31) as u32;
    let num_bytes_tail = (num_bits_tail + 7) >> 3;
    if num_bytes_tail > 0 {
        4 - num_bytes_tail
    } else {
        0
    }
}

fn encode_uint(buf: &mut Vec<u8>, k: u32, num_bytes: usize) {
    match num_bytes {
        1 => buf.push(k as u8),
        2 => buf.extend_from_slice(&(k as u16).to_le_bytes()),
        4 => buf.extend_from_slice(&k.to_le_bytes()),
        _ => unreachable!(),
    }
}

/// Public wrapper for encode_uint.
#[inline]
pub fn encode_uint_pub(buf: &mut Vec<u8>, k: u32, num_bytes: usize) {
    encode_uint(buf, k, num_bytes);
}

/// Append bit-stuffed data directly to `buf`.
#[inline]
pub fn bit_stuff_append(buf: &mut Vec<u8>, data: &[u32], num_bits: u32) {
    if num_bits == 0 || data.is_empty() {
        return;
    }
    let num_elements = data.len() as u32;
    let num_uints = (num_elements as u64 * num_bits as u64).div_ceil(32) as usize;
    let num_bytes_used =
        (num_uints * 4) - num_tail_bytes_not_needed(num_elements, num_bits) as usize;
    let start = buf.len();
    buf.resize(start + num_bytes_used, 0);
    bit_stuff_into(&mut buf[start..], data, num_bits);
}

fn decode_uint(data: &[u8], pos: &mut usize, num_bytes: usize) -> Result<u32> {
    if *pos + num_bytes > data.len() {
        return Err(LercError::BufferTooSmall {
            needed: *pos + num_bytes,
            available: data.len(),
        });
    }
    let val = match num_bytes {
        1 => data[*pos] as u32,
        2 => u16::from_le_bytes(data[*pos..*pos + 2].try_into().unwrap()) as u32,
        4 => u32::from_le_bytes(data[*pos..*pos + 4].try_into().unwrap()),
        _ => return Err(LercError::InvalidData("invalid uint size".into())),
    };
    *pos += num_bytes;
    Ok(val)
}

/// Bit-stuff values into a buffer (v3+ format: LSB-first packing into u32 words).
fn bit_stuff(data: &[u32], num_bits: u32) -> Vec<u8> {
    if num_bits == 0 || data.is_empty() {
        return Vec::new();
    }

    let num_elements = data.len() as u32;
    let num_uints = (num_elements as u64 * num_bits as u64).div_ceil(32) as usize;
    let num_bytes_used =
        (num_uints * 4) - num_tail_bytes_not_needed(num_elements, num_bits) as usize;

    // Allocate output bytes directly (little-endian) and pack using a 64-bit accumulator
    let mut out = vec![0u8; num_bytes_used];
    bit_stuff_into(&mut out, data, num_bits);
    out
}

/// Bit-stuff values into an already-allocated byte slice using a 64-bit accumulator.
/// The output slice must be at least as large as the bit-stuffed data.
#[inline]
fn bit_stuff_into(out: &mut [u8], data: &[u32], num_bits: u32) {
    if num_bits == 0 || data.is_empty() {
        return;
    }

    let mut accum: u64 = 0;
    let mut bits_in_accum: u32 = 0;
    let mut dst = 0usize;

    for &val in data {
        accum |= (val as u64) << bits_in_accum;
        bits_in_accum += num_bits;

        if bits_in_accum >= 32 {
            let word = accum as u32;
            // Write the u32 word as little-endian bytes
            out[dst..dst + 4].copy_from_slice(&word.to_le_bytes());
            dst += 4;
            accum >>= 32;
            bits_in_accum -= 32;
        }
    }

    // Flush remaining bits
    if bits_in_accum > 0 {
        let remaining_bytes = bits_in_accum.div_ceil(8) as usize;
        let word_bytes = (accum as u32).to_le_bytes();
        let n = remaining_bytes.min(out.len() - dst);
        out[dst..dst + n].copy_from_slice(&word_bytes[..n]);
    }
}

/// Bit-unstuff values from a buffer (v3+ format: LSB-first packing).
/// Uses a 64-bit accumulator to avoid branching in the inner loop.
fn bit_unstuff(data: &[u8], pos: &mut usize, num_elements: u32, num_bits: u32) -> Result<Vec<u32>> {
    if num_elements == 0 || num_bits >= 32 {
        return Err(LercError::InvalidData("bitstuffer: invalid params".into()));
    }

    let num_uints = (num_elements as u64 * num_bits as u64).div_ceil(32) as usize;
    let num_bytes_used =
        (num_uints * 4) - num_tail_bytes_not_needed(num_elements, num_bits) as usize;

    if *pos + num_bytes_used > data.len() {
        return Err(LercError::BufferTooSmall {
            needed: *pos + num_bytes_used,
            available: data.len(),
        });
    }

    let bytes = &data[*pos..*pos + num_bytes_used];
    let mut result = vec![0u32; num_elements as usize];
    let mask = (1u64 << num_bits) - 1;
    let num_bits_usize = num_bits as usize;

    // Use a 64-bit accumulator: read bytes as needed, shift and mask.
    // This avoids the branch per element that the u32-word approach requires.
    let mut accum: u64 = 0;
    let mut bits_in_accum: usize = 0;
    let mut byte_idx: usize = 0;

    for dst in result.iter_mut() {
        // Refill the accumulator when we need more bits.
        // We can always read enough to have at least num_bits available.
        while bits_in_accum < num_bits_usize {
            // Read up to 4 bytes at a time for efficiency
            if byte_idx + 4 <= bytes.len() {
                let word = u32::from_le_bytes([
                    bytes[byte_idx],
                    bytes[byte_idx + 1],
                    bytes[byte_idx + 2],
                    bytes[byte_idx + 3],
                ]);
                accum |= (word as u64) << bits_in_accum;
                bits_in_accum += 32;
                byte_idx += 4;
            } else if byte_idx < bytes.len() {
                accum |= (bytes[byte_idx] as u64) << bits_in_accum;
                bits_in_accum += 8;
                byte_idx += 1;
            } else {
                break;
            }
        }
        *dst = (accum & mask) as u32;
        accum >>= num_bits_usize;
        bits_in_accum -= num_bits_usize;
    }

    *pos += num_bytes_used;
    Ok(result)
}

/// Bit-unstuff values from a buffer (pre-v3 format: MSB-first packing).
fn bit_unstuff_before_v3(
    data: &[u8],
    pos: &mut usize,
    num_elements: u32,
    num_bits: u32,
) -> Result<Vec<u32>> {
    if num_elements == 0 || num_bits >= 32 {
        return Err(LercError::InvalidData("bitstuffer: invalid params".into()));
    }

    let num_uints = (num_elements as u64 * num_bits as u64).div_ceil(32) as usize;
    let ntbnn = num_tail_bytes_not_needed(num_elements, num_bits) as usize;

    let num_bytes_to_copy = (num_elements as u64 * num_bits as u64).div_ceil(8) as usize;

    if *pos + num_bytes_to_copy > data.len() {
        return Err(LercError::BufferTooSmall {
            needed: *pos + num_bytes_to_copy,
            available: data.len(),
        });
    }

    // Copy into aligned u32 buffer
    let mut src = vec![0u32; num_uints];
    let bytes = &data[*pos..*pos + num_bytes_to_copy];
    for (i, word) in src.iter_mut().enumerate() {
        let start = i * 4;
        if start + 4 <= bytes.len() {
            *word = u32::from_le_bytes(bytes[start..start + 4].try_into().unwrap());
        } else {
            let mut tmp = [0u8; 4];
            tmp[..bytes.len() - start].copy_from_slice(&bytes[start..]);
            *word = u32::from_le_bytes(tmp);
        }
    }

    // Shift the last uint left to compensate for unused tail bytes
    if let Some(last) = src.last_mut() {
        for _ in 0..ntbnn {
            *last <<= 8;
        }
    }

    let mut result = vec![0u32; num_elements as usize];
    let mut bit_pos: i32 = 0;
    let mut src_idx = 0;

    for dst in result.iter_mut() {
        if 32 - bit_pos >= num_bits as i32 {
            let val = src[src_idx];
            let n = val << bit_pos as u32;
            *dst = n >> (32 - num_bits);
            bit_pos += num_bits as i32;
            if bit_pos == 32 {
                bit_pos = 0;
                src_idx += 1;
            }
        } else {
            let val1 = src[src_idx];
            src_idx += 1;
            let n = val1 << bit_pos as u32;
            *dst = n >> (32 - num_bits);
            bit_pos -= 32 - num_bits as i32;
            let val2 = src[src_idx];
            *dst |= val2 >> (32 - bit_pos);
        }
    }

    *pos += num_bytes_to_copy;
    Ok(result)
}

/// Encode an array of unsigned ints using simple mode (no LUT).
pub fn encode_simple(data: &[u32]) -> Vec<u8> {
    if data.is_empty() {
        return Vec::new();
    }

    let max_elem = data.iter().copied().max().unwrap_or(0);
    let mut num_bits: u32 = 0;
    while num_bits < 32 && (max_elem >> num_bits) != 0 {
        num_bits += 1;
    }

    let num_elements = data.len() as u32;
    let n = num_bytes_uint(num_elements);
    let bits67 = if n == 4 { 0u8 } else { (3 - n) as u8 };

    let mut header_byte = num_bits as u8;
    header_byte |= bits67 << 6;
    // bit 5 = 0 for simple mode

    let mut buf = Vec::new();
    buf.push(header_byte);
    encode_uint(&mut buf, num_elements, n);

    if num_bits > 0 {
        let stuffed = bit_stuff(data, num_bits);
        buf.extend_from_slice(&stuffed);
    }

    buf
}

/// Encode an array of unsigned ints using simple mode, appending directly to `buf`.
#[inline]
pub fn encode_simple_into(buf: &mut Vec<u8>, data: &[u32], max_elem: u32) {
    if data.is_empty() {
        return;
    }

    let num_bits = num_bits_needed(max_elem);
    let num_elements = data.len() as u32;
    let n = num_bytes_uint(num_elements);
    let bits67 = if n == 4 { 0u8 } else { (3 - n) as u8 };

    let mut header_byte = num_bits as u8;
    header_byte |= bits67 << 6;

    buf.push(header_byte);
    encode_uint(buf, num_elements, n);

    if num_bits > 0 {
        let num_uints = (num_elements as u64 * num_bits as u64).div_ceil(32) as usize;
        let num_bytes_used =
            (num_uints * 4) - num_tail_bytes_not_needed(num_elements, num_bits) as usize;
        let start = buf.len();
        buf.resize(start + num_bytes_used, 0);
        bit_stuff_into(&mut buf[start..], data, num_bits);
    }
}

/// Encode an array of unsigned ints using LUT mode, appending directly to `buf`.
pub fn encode_lut_into(buf: &mut Vec<u8>, info: &LutInfo, num_elem: u32) {
    if num_elem == 0 {
        return;
    }

    let n = num_bytes_uint(num_elem);
    let bits67 = if n == 4 { 0u8 } else { (3 - n) as u8 };

    let mut header_byte = info.num_bits as u8;
    header_byte |= bits67 << 6;
    header_byte |= 1 << 5; // bit 5 = 1 for LUT mode

    let n_lut = info.lut_vec.len() as u32;
    let lut_bytes = if info.num_bits > 0 {
        let num_uints = (n_lut as u64 * info.num_bits as u64).div_ceil(32) as usize;
        (num_uints * 4) - num_tail_bytes_not_needed(n_lut, info.num_bits) as usize
    } else {
        0
    };
    let idx_bytes = if info.n_bits_lut > 0 {
        let num_uints = (num_elem as u64 * info.n_bits_lut as u64).div_ceil(32) as usize;
        (num_uints * 4) - num_tail_bytes_not_needed(num_elem, info.n_bits_lut) as usize
    } else {
        0
    };

    buf.reserve(1 + n + 1 + lut_bytes + idx_bytes);
    buf.push(header_byte);
    encode_uint(buf, num_elem, n);
    buf.push((n_lut + 1) as u8);

    if info.num_bits > 0 {
        let start = buf.len();
        buf.resize(start + lut_bytes, 0);
        bit_stuff_into(&mut buf[start..], &info.lut_vec, info.num_bits);
    }

    if info.n_bits_lut > 0 {
        let start = buf.len();
        buf.resize(start + idx_bytes, 0);
        bit_stuff_into(&mut buf[start..], &info.index_vec, info.n_bits_lut);
    }
}

/// Fast-path for encoding small tiles (up to 256 elements) with values in [0, 255].
/// Combines LUT decision + encoding in a single pass with stack-allocated arrays.
/// Writes directly to `buf`. Returns the max quantized value for caller's use.
///
/// The `quant_data` slice contains quantized u32 values where max_elem < 256.
/// `max_elem` is the maximum value in quant_data.
pub fn encode_small_tile_into(buf: &mut Vec<u8>, quant_data: &[u32], max_elem: u32) {
    debug_assert!(max_elem < 256);
    debug_assert!(quant_data.len() <= 256);

    if quant_data.is_empty() {
        return;
    }

    let num_elem = quant_data.len() as u32;
    let num_bits = num_bits_needed(max_elem);

    // Build histogram using stack-allocated array
    let mut counts = [0u16; 256];
    for &val in quant_data {
        counts[val as usize] += 1;
    }

    // Count distinct values
    let mut n_distinct = 0u32;
    for &c in &counts[..=max_elem as usize] {
        if c > 0 {
            n_distinct += 1;
        }
    }

    // Quick LUT vs simple size comparison
    let use_lut = if (2..=255).contains(&n_distinct) {
        let n_lut = n_distinct - 1;
        let mut n_bits_lut = 0u32;
        while (n_lut >> n_bits_lut) != 0 {
            n_bits_lut += 1;
        }
        let num_bytes_simple =
            1 + num_bytes_uint(num_elem) as u32 + ((num_elem * num_bits + 7) >> 3);
        let num_bytes_lut = 1
            + num_bytes_uint(num_elem) as u32
            + 1
            + ((n_lut * num_bits + 7) >> 3)
            + ((num_elem * n_bits_lut + 7) >> 3);
        num_bytes_lut < num_bytes_simple
    } else {
        false
    };

    if !use_lut {
        // Simple encoding: write directly
        encode_simple_into(buf, quant_data, max_elem);
        return;
    }

    // LUT encoding with stack-allocated mapping
    let n_lut = n_distinct - 1;
    let mut n_bits_lut = 0u32;
    while (n_lut >> n_bits_lut) != 0 {
        n_bits_lut += 1;
    }

    // Build value-to-index mapping and lut_vec on the stack
    let mut value_to_index = [0u8; 256];
    let mut lut_values = [0u32; 255]; // max 255 LUT entries (excluding the min value)
    let mut lut_count = 0u32;
    let mut idx = 0u8;
    for v in 0..=max_elem as usize {
        if counts[v] > 0 {
            value_to_index[v] = idx;
            if idx > 0 {
                lut_values[lut_count as usize] = v as u32;
                lut_count += 1;
            }
            idx += 1;
        }
    }

    // Build index array on the stack (max 256 elements for small tiles)
    let mut index_arr = [0u32; 256];
    for (i, &val) in quant_data.iter().enumerate() {
        index_arr[i] = value_to_index[val as usize] as u32;
    }
    let index_data = &index_arr[..quant_data.len()];
    let lut_data = &lut_values[..lut_count as usize];

    // Write LUT-encoded data directly to buf
    let n = num_bytes_uint(num_elem);
    let bits67 = if n == 4 { 0u8 } else { (3 - n) as u8 };
    let mut header_byte = num_bits as u8;
    header_byte |= bits67 << 6;
    header_byte |= 1 << 5; // LUT mode

    let lut_bytes = if num_bits > 0 {
        let num_uints = (lut_count as u64 * num_bits as u64).div_ceil(32) as usize;
        (num_uints * 4) - num_tail_bytes_not_needed(lut_count, num_bits) as usize
    } else {
        0
    };
    let idx_bytes = if n_bits_lut > 0 {
        let num_uints = (num_elem as u64 * n_bits_lut as u64).div_ceil(32) as usize;
        (num_uints * 4) - num_tail_bytes_not_needed(num_elem, n_bits_lut) as usize
    } else {
        0
    };

    buf.reserve(1 + n + 1 + lut_bytes + idx_bytes);
    buf.push(header_byte);
    encode_uint(buf, num_elem, n);
    buf.push((lut_count + 1) as u8);

    if num_bits > 0 {
        let start = buf.len();
        buf.resize(start + lut_bytes, 0);
        bit_stuff_into(&mut buf[start..], lut_data, num_bits);
    }

    if n_bits_lut > 0 {
        let start = buf.len();
        buf.resize(start + idx_bytes, 0);
        bit_stuff_into(&mut buf[start..], index_data, n_bits_lut);
    }
}

/// Pre-computed LUT encoding info from `should_use_lut`, avoiding recomputation in `encode_lut_into`.
pub struct LutInfo {
    /// Distinct values excluding the minimum (which maps to index 0)
    pub lut_vec: Vec<u32>,
    /// Per-element LUT index
    pub index_vec: Vec<u32>,
    /// Number of bits needed for the max value in lut_vec
    pub num_bits: u32,
    /// Number of bits needed for lut indices
    pub n_bits_lut: u32,
}

/// Decode a bit-stuffed array (auto-detects simple vs LUT mode).
pub fn decode(
    data: &[u8],
    pos: &mut usize,
    max_element_count: usize,
    lerc2_version: i32,
) -> Result<Vec<u32>> {
    if *pos >= data.len() {
        return Err(LercError::BufferTooSmall {
            needed: *pos + 1,
            available: data.len(),
        });
    }

    let header_byte = data[*pos];
    *pos += 1;

    let bits67 = header_byte >> 6;
    let nb = if bits67 == 0 {
        4
    } else {
        (3 - bits67) as usize
    };
    let do_lut = (header_byte & (1 << 5)) != 0;
    let num_bits = (header_byte & 31) as u32;

    let num_elements = decode_uint(data, pos, nb)?;
    if num_elements as usize > max_element_count {
        return Err(LercError::InvalidData("too many elements".into()));
    }

    if !do_lut {
        if num_bits > 0 {
            if lerc2_version >= 3 {
                bit_unstuff(data, pos, num_elements, num_bits)
            } else {
                bit_unstuff_before_v3(data, pos, num_elements, num_bits)
            }
        } else {
            // numBits == 0 means all values are 0
            Ok(vec![0u32; num_elements as usize])
        }
    } else {
        if num_bits == 0 {
            return Err(LercError::InvalidData("LUT mode with 0 bits".into()));
        }

        if *pos >= data.len() {
            return Err(LercError::BufferTooSmall {
                needed: *pos + 1,
                available: data.len(),
            });
        }

        let n_lut_byte = data[*pos];
        *pos += 1;
        let n_lut = (n_lut_byte as u32).wrapping_sub(1);

        // Unstuff LUT values (without the 0)
        let mut lut_vec = if lerc2_version >= 3 {
            bit_unstuff(data, pos, n_lut, num_bits)?
        } else {
            bit_unstuff_before_v3(data, pos, n_lut, num_bits)?
        };

        let mut n_bits_lut: u32 = 0;
        while (n_lut >> n_bits_lut) != 0 {
            n_bits_lut += 1;
        }
        if n_bits_lut == 0 {
            return Err(LercError::InvalidData("LUT with 0 index bits".into()));
        }

        // Unstuff indexes
        let mut result = if lerc2_version >= 3 {
            bit_unstuff(data, pos, num_elements, n_bits_lut)?
        } else {
            bit_unstuff_before_v3(data, pos, num_elements, n_bits_lut)?
        };

        // Replace indexes by values
        lut_vec.insert(0, 0); // put back the 0
        for val in result.iter_mut() {
            let idx = *val as usize;
            if idx >= lut_vec.len() {
                return Err(LercError::InvalidData("LUT index out of bounds".into()));
            }
            *val = lut_vec[idx];
        }

        Ok(result)
    }
}

/// Compute the number of bits needed to represent `max_elem`.
pub fn num_bits_needed(max_elem: u32) -> u32 {
    let mut num_bits: u32 = 0;
    while num_bits < 32 && (max_elem >> num_bits) != 0 {
        num_bits += 1;
    }
    num_bits
}

/// Decide whether LUT encoding is beneficial, and if so build the LUT info directly.
///
/// For small domains (max_elem < 256, common for u8 data), uses a histogram-based
/// approach that avoids sorting entirely. For larger domains, uses a bitset to count
/// distinct values first, then builds the LUT via counting sort when possible.
///
/// Returns `Some(LutInfo)` with pre-computed LUT data ready for `encode_lut_into`,
/// or `None` if simple encoding would be smaller.
pub fn should_use_lut(data: &[u32], max_elem: u32) -> Option<LutInfo> {
    if data.len() < 2 {
        return None;
    }

    let num_elem = data.len() as u32;
    let num_bits = num_bits_needed(max_elem);

    if max_elem < 256 {
        // Fast path for u8-range data: histogram-based, no sorting needed
        should_use_lut_small(data, num_elem, num_bits, max_elem)
    } else if max_elem < 65536 {
        // Medium domain: bitset count then counting sort
        should_use_lut_medium(data, num_elem, num_bits, max_elem)
    } else {
        // Large domain: sample-based estimate
        should_use_lut_large(data, num_elem, num_bits)
    }
}

/// Fast LUT construction for u8-range data (max_elem < 256).
/// Uses a 256-entry histogram to count distinct values AND build the value-to-index
/// mapping in a single pass, completely avoiding any sorting.
fn should_use_lut_small(
    data: &[u32],
    num_elem: u32,
    num_bits: u32,
    max_elem: u32,
) -> Option<LutInfo> {
    // Build histogram
    let mut counts = [0u32; 256];
    for &val in data {
        counts[val as usize] += 1;
    }

    // Count distinct values
    let n_distinct: u32 = counts.iter().filter(|&&c| c > 0).count() as u32;

    if !(2..=255).contains(&n_distinct) {
        return None;
    }

    let n_lut = n_distinct - 1;

    // Estimate size before building the full LUT
    let num_bytes_simple = 1 + num_bytes_uint(num_elem) as u32 + ((num_elem * num_bits + 7) >> 3);

    let mut n_bits_lut = 0u32;
    while (n_lut >> n_bits_lut) != 0 {
        n_bits_lut += 1;
    }

    let num_bytes_lut = 1
        + num_bytes_uint(num_elem) as u32
        + 1
        + ((n_lut * num_bits + 7) >> 3)
        + ((num_elem * n_bits_lut + 7) >> 3);

    if num_bytes_lut >= num_bytes_simple {
        return None;
    }

    // Build value-to-lut-index mapping: iterate values in order, assign indices.
    // Index 0 = minimum value, then 1, 2, ... for subsequent distinct values.
    let mut value_to_index = [0u32; 256];
    let mut lut_vec = Vec::with_capacity(n_lut as usize);
    let mut idx = 0u32;
    for v in 0..=max_elem as usize {
        if counts[v] > 0 {
            value_to_index[v] = idx;
            if idx > 0 {
                lut_vec.push(v as u32);
            }
            idx += 1;
        }
    }

    // Build index_vec by looking up each data value
    let mut index_vec = vec![0u32; num_elem as usize];
    for (i, &val) in data.iter().enumerate() {
        index_vec[i] = value_to_index[val as usize];
    }

    Some(LutInfo {
        lut_vec,
        index_vec,
        num_bits,
        n_bits_lut,
    })
}

/// LUT construction for medium domains (256..65536).
fn should_use_lut_medium(
    data: &[u32],
    num_elem: u32,
    num_bits: u32,
    max_elem: u32,
) -> Option<LutInfo> {
    // Use a bitset to count distinct values
    let num_words = (max_elem as usize / 64) + 1;
    let mut bitset = vec![0u64; num_words];
    for &val in data {
        let word = val as usize / 64;
        let bit = val as usize % 64;
        bitset[word] |= 1u64 << bit;
    }
    let n_distinct: u32 = bitset.iter().map(|w| w.count_ones()).sum();

    if !(2..=255).contains(&n_distinct) {
        return None;
    }

    let n_lut = n_distinct - 1;

    let num_bytes_simple = 1 + num_bytes_uint(num_elem) as u32 + ((num_elem * num_bits + 7) >> 3);

    let mut n_bits_lut = 0u32;
    while (n_lut >> n_bits_lut) != 0 {
        n_bits_lut += 1;
    }

    let num_bytes_lut = 1
        + num_bytes_uint(num_elem) as u32
        + 1
        + ((n_lut * num_bits + 7) >> 3)
        + ((num_elem * n_bits_lut + 7) >> 3);

    if num_bytes_lut >= num_bytes_simple {
        return None;
    }

    // Build value-to-index map using the bitset
    // We need a map from value -> lut index. Use a Vec since domain is < 65536.
    let mut value_to_index = vec![0u32; max_elem as usize + 1];
    let mut lut_vec = Vec::with_capacity(n_lut as usize);
    let mut idx = 0u32;
    for (v, slot) in value_to_index.iter_mut().enumerate() {
        let word = v / 64;
        let bit = v % 64;
        if (bitset[word] >> bit) & 1 != 0 {
            *slot = idx;
            if idx > 0 {
                lut_vec.push(v as u32);
            }
            idx += 1;
        }
    }

    let mut index_vec = vec![0u32; num_elem as usize];
    for (i, &val) in data.iter().enumerate() {
        index_vec[i] = value_to_index[val as usize];
    }

    Some(LutInfo {
        lut_vec,
        index_vec,
        num_bits,
        n_bits_lut,
    })
}

/// LUT construction for large domains (>= 65536).
fn should_use_lut_large(data: &[u32], num_elem: u32, num_bits: u32) -> Option<LutInfo> {
    // Sample to estimate distinct count
    let step = (data.len() / 512).max(1);
    let mut seen = Vec::with_capacity(256);
    for (i, &val) in data.iter().enumerate() {
        if i % step != 0 {
            continue;
        }
        if !seen.contains(&val) {
            seen.push(val);
            if seen.len() > 255 {
                return None; // Exceeds LUT threshold
            }
        }
    }
    let n_distinct_est = seen.len() as u32;

    if !(2..=255).contains(&n_distinct_est) {
        return None;
    }

    // Need to do the full sort for large domains
    let mut sorted: Vec<(u32, u32)> = data.iter().copied().zip(0u32..).collect();
    sorted.sort_unstable_by_key(|&(val, _)| val);

    // Build LUT from sorted data
    let mut lut_vec: Vec<u32> = Vec::new();
    let mut index_vec = vec![0u32; num_elem as usize];
    let mut index_lut = 0u32;

    for i in 1..sorted.len() {
        let prev = sorted[i - 1].0;
        index_vec[sorted[i - 1].1 as usize] = index_lut;
        if sorted[i].0 != prev {
            lut_vec.push(sorted[i].0);
            index_lut += 1;
        }
    }
    index_vec[sorted[num_elem as usize - 1].1 as usize] = index_lut;

    let n_lut = lut_vec.len() as u32;

    // Check if LUT is beneficial
    let num_bytes_simple = 1 + num_bytes_uint(num_elem) as u32 + ((num_elem * num_bits + 7) >> 3);

    let mut n_bits_lut = 0u32;
    while (n_lut >> n_bits_lut) != 0 {
        n_bits_lut += 1;
    }

    let num_bytes_lut = 1
        + num_bytes_uint(num_elem) as u32
        + 1
        + ((n_lut * num_bits + 7) >> 3)
        + ((num_elem * n_bits_lut + 7) >> 3);

    if num_bytes_lut >= num_bytes_simple {
        return None;
    }

    let max_lut_elem = *lut_vec.last().unwrap_or(&0);
    let actual_num_bits = num_bits_needed(max_lut_elem);
    // Use the original num_bits (based on max_elem of the full data) for consistency
    let _ = actual_num_bits;

    Some(LutInfo {
        lut_vec,
        index_vec,
        num_bits,
        n_bits_lut,
    })
}

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

    #[test]
    fn round_trip_simple() {
        let data = vec![0, 1, 2, 3, 100, 255, 42, 7];
        let encoded = encode_simple(&data);
        let mut pos = 0;
        let decoded = decode(&encoded, &mut pos, 100, 6).unwrap();
        assert_eq!(decoded, data);
        assert_eq!(pos, encoded.len());
    }

    #[test]
    fn round_trip_simple_zeros() {
        let data = vec![0, 0, 0, 0, 0];
        let encoded = encode_simple(&data);
        let mut pos = 0;
        let decoded = decode(&encoded, &mut pos, 100, 6).unwrap();
        assert_eq!(decoded, data);
    }

    #[test]
    fn round_trip_simple_large() {
        let data: Vec<u32> = (0..1000).collect();
        let encoded = encode_simple(&data);
        let mut pos = 0;
        let decoded = decode(&encoded, &mut pos, 2000, 6).unwrap();
        assert_eq!(decoded, data);
    }

    #[test]
    fn round_trip_lut() {
        // Data with few distinct values  -- good for LUT
        let data = vec![0, 100, 200, 0, 100, 200, 0, 100, 200, 0];
        let max_elem = *data.iter().max().unwrap();
        let info = should_use_lut(&data, max_elem).expect("should use LUT");

        let mut encoded = Vec::new();
        encode_lut_into(&mut encoded, &info, data.len() as u32);
        let mut pos = 0;
        let decoded = decode(&encoded, &mut pos, 100, 6).unwrap();
        assert_eq!(decoded, data);
    }

    #[test]
    fn num_bits() {
        assert_eq!(num_bits_needed(0), 0);
        assert_eq!(num_bits_needed(1), 1);
        assert_eq!(num_bits_needed(2), 2);
        assert_eq!(num_bits_needed(3), 2);
        assert_eq!(num_bits_needed(255), 8);
        assert_eq!(num_bits_needed(256), 9);
    }

    #[test]
    fn bit_stuff_round_trip_various_widths() {
        for num_bits in 1..32 {
            let max_val = (1u32 << num_bits) - 1;
            let data: Vec<u32> = (0..50).map(|i| i % (max_val + 1)).collect();
            let stuffed = bit_stuff(&data, num_bits);
            let mut pos = 0;
            // Wrap in a full decode context
            let unstuffed = bit_unstuff(&stuffed, &mut pos, data.len() as u32, num_bits).unwrap();
            assert_eq!(unstuffed, data, "failed for num_bits={num_bits}");
        }
    }

    // -----------------------------------------------------------------------
    // _into / _append zero-alloc variant tests
    // -----------------------------------------------------------------------

    #[test]
    fn encode_simple_into_matches_encode_simple() {
        let data = vec![0, 1, 2, 3, 100, 255, 42, 7];
        let vec_result = encode_simple(&data);
        let max_elem = *data.iter().max().unwrap();
        let mut buf = Vec::new();
        encode_simple_into(&mut buf, &data, max_elem);
        assert_eq!(buf, vec_result);
    }

    #[test]
    fn encode_simple_into_zeros() {
        let data = vec![0, 0, 0, 0, 0];
        let vec_result = encode_simple(&data);
        let mut buf = Vec::new();
        encode_simple_into(&mut buf, &data, 0);
        assert_eq!(buf, vec_result);
    }

    #[test]
    fn encode_simple_into_single_element() {
        let data = vec![42];
        let vec_result = encode_simple(&data);
        let mut buf = Vec::new();
        encode_simple_into(&mut buf, &data, 42);
        assert_eq!(buf, vec_result);
    }

    #[test]
    fn encode_simple_into_large() {
        let data: Vec<u32> = (0..1000).collect();
        let vec_result = encode_simple(&data);
        let max_elem = *data.iter().max().unwrap();
        let mut buf = Vec::new();
        encode_simple_into(&mut buf, &data, max_elem);
        assert_eq!(buf, vec_result);
    }

    #[test]
    fn encode_simple_into_empty() {
        let data: Vec<u32> = vec![];
        let vec_result = encode_simple(&data);
        let mut buf = Vec::new();
        encode_simple_into(&mut buf, &data, 0);
        assert_eq!(buf, vec_result);
    }

    #[test]
    fn encode_simple_into_appends_to_existing() {
        // Verify _into appends rather than overwriting
        let data = vec![1, 2, 3];
        let max_elem = 3;
        let mut buf = vec![0xAA, 0xBB];
        encode_simple_into(&mut buf, &data, max_elem);
        assert_eq!(buf[0], 0xAA);
        assert_eq!(buf[1], 0xBB);
        // The rest should match encode_simple output
        let standalone = encode_simple(&data);
        assert_eq!(&buf[2..], &standalone[..]);
    }

    #[test]
    fn bit_stuff_append_matches_bit_stuff() {
        let data = vec![1, 2, 3, 4, 5];
        let num_bits = 3;
        let standalone = bit_stuff(&data, num_bits);
        let mut buf = Vec::new();
        bit_stuff_append(&mut buf, &data, num_bits);
        assert_eq!(buf, standalone);
    }

    #[test]
    fn bit_stuff_append_various_widths() {
        for num_bits in 1..32u32 {
            let max_val = (1u64 << num_bits) - 1;
            let data: Vec<u32> = (0..50).map(|i| (i as u32) % (max_val as u32 + 1)).collect();
            let standalone = bit_stuff(&data, num_bits);
            let mut buf = Vec::new();
            bit_stuff_append(&mut buf, &data, num_bits);
            assert_eq!(buf, standalone, "mismatch for num_bits={num_bits}");
        }
    }

    #[test]
    fn bit_stuff_append_empty() {
        let data: Vec<u32> = vec![];
        let standalone = bit_stuff(&data, 5);
        let mut buf = Vec::new();
        bit_stuff_append(&mut buf, &data, 5);
        assert_eq!(buf, standalone);
        assert!(buf.is_empty());
    }

    #[test]
    fn bit_stuff_append_zero_bits() {
        let data = vec![0, 0, 0];
        let standalone = bit_stuff(&data, 0);
        let mut buf = Vec::new();
        bit_stuff_append(&mut buf, &data, 0);
        assert_eq!(buf, standalone);
        assert!(buf.is_empty());
    }

    #[test]
    fn bit_stuff_append_preserves_prefix() {
        let data = vec![7, 3, 1];
        let num_bits = 3;
        let standalone = bit_stuff(&data, num_bits);
        let mut buf = vec![0xDE, 0xAD];
        bit_stuff_append(&mut buf, &data, num_bits);
        assert_eq!(buf[0], 0xDE);
        assert_eq!(buf[1], 0xAD);
        assert_eq!(&buf[2..], &standalone[..]);
    }

    #[test]
    fn encode_lut_into_round_trips() {
        // Data with few distinct values - good for LUT
        let data = vec![0, 100, 200, 0, 100, 200, 0, 100, 200, 0];
        let max_elem = *data.iter().max().unwrap();
        let info = should_use_lut(&data, max_elem).expect("should use LUT");
        let mut buf = Vec::new();
        encode_lut_into(&mut buf, &info, data.len() as u32);
        let mut pos = 0;
        let decoded = decode(&buf, &mut pos, 100, 6).unwrap();
        assert_eq!(decoded, data);
    }

    #[test]
    fn encode_lut_into_appends_to_existing() {
        let data = vec![0, 100, 200, 0, 100, 200, 0, 100, 200, 0];
        let max_elem = *data.iter().max().unwrap();
        let info = should_use_lut(&data, max_elem).expect("should use LUT");
        let mut buf = vec![0xFF];
        encode_lut_into(&mut buf, &info, data.len() as u32);
        assert_eq!(buf[0], 0xFF);
        // Decode from offset 1
        let mut pos = 1;
        let decoded = decode(&buf, &mut pos, 100, 6).unwrap();
        assert_eq!(decoded, data);
    }

    #[test]
    fn encode_small_tile_into_simple_case() {
        // Small tile with values in [0, 255] -- should use simple encoding
        let data = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        let max_elem = 9;
        let mut buf = Vec::new();
        encode_small_tile_into(&mut buf, &data, max_elem);
        // Verify we can decode it back
        let mut pos = 0;
        let decoded = decode(&buf, &mut pos, 100, 6).unwrap();
        assert_eq!(decoded, data);
    }

    #[test]
    fn encode_small_tile_into_lut_case() {
        // Data with few distinct values in a small tile
        let data = vec![0, 50, 100, 0, 50, 100, 0, 50, 100, 0, 50, 100];
        let max_elem = 100;
        let mut buf = Vec::new();
        encode_small_tile_into(&mut buf, &data, max_elem);
        let mut pos = 0;
        let decoded = decode(&buf, &mut pos, 100, 6).unwrap();
        assert_eq!(decoded, data);
    }

    #[test]
    fn encode_small_tile_into_single_value() {
        // All same nonzero value (max_elem = 0 after quantization offset)
        // After subtracting min, all become 0, so max_elem = 0.
        // encode_small_tile_into with max_elem=0 should encode as all-zeros.
        let data = vec![0, 0, 0, 0];
        let mut buf = Vec::new();
        encode_small_tile_into(&mut buf, &data, 0);
        let mut pos = 0;
        let decoded = decode(&buf, &mut pos, 100, 6).unwrap();
        assert_eq!(decoded, data);
    }

    #[test]
    fn encode_small_tile_into_max_size_tile() {
        // 256-element tile (max for this path)
        let data: Vec<u32> = (0..256).map(|i| (i % 200) as u32).collect();
        let max_elem = 199;
        let mut buf = Vec::new();
        encode_small_tile_into(&mut buf, &data, max_elem);
        let mut pos = 0;
        let decoded = decode(&buf, &mut pos, 300, 6).unwrap();
        assert_eq!(decoded, data);
    }

    #[cfg(not(target_arch = "wasm32"))]
    mod proptest_tests {
        use super::*;
        use proptest::prelude::*;

        proptest! {
            #[test]
            fn bit_stuff_round_trip(num_bits in 1..31u32, len in 1..200usize) {
                let max_val = (1u32 << num_bits) - 1;
                let data: Vec<u32> = (0..len).map(|i| (i as u32) % (max_val + 1)).collect();
                let stuffed = bit_stuff(&data, num_bits);
                let mut pos = 0;
                let unstuffed = bit_unstuff(&stuffed, &mut pos, len as u32, num_bits).unwrap();
                prop_assert_eq!(unstuffed, data);
            }

            #[test]
            fn encode_simple_round_trip(data in prop::collection::vec(0..1000u32, 1..200)) {
                let encoded = encode_simple(&data);
                let mut pos = 0;
                let decoded = decode(&encoded, &mut pos, data.len(), 6).unwrap();
                prop_assert_eq!(decoded, data);
            }

            #[test]
            fn encode_simple_into_matches_encode_simple_prop(
                data in prop::collection::vec(0..1000u32, 1..200)
            ) {
                let vec_result = encode_simple(&data);
                let max_elem = data.iter().copied().max().unwrap_or(0);
                let mut buf = Vec::new();
                encode_simple_into(&mut buf, &data, max_elem);
                prop_assert_eq!(buf, vec_result);
            }

            #[test]
            fn bit_stuff_append_matches_bit_stuff_prop(
                num_bits in 1..31u32, len in 1..200usize
            ) {
                let max_val = (1u32 << num_bits) - 1;
                let data: Vec<u32> = (0..len).map(|i| (i as u32) % (max_val + 1)).collect();
                let standalone = bit_stuff(&data, num_bits);
                let mut buf = Vec::new();
                bit_stuff_append(&mut buf, &data, num_bits);
                prop_assert_eq!(buf, standalone);
            }
        }
    }
}