oxihuman-core 0.2.1

Core data structures, algorithms, and asset management for OxiHuman
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
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! GIF89a encoder and decoder with proper LZW compression.
//!
//! This module implements a complete GIF89a codec in pure Rust:
//! - Encoder: RGB pixels → GIF89a bytes, using median-cut color quantization
//!   (≤256 colors) and variable-width LSB-first LZW compression.
//! - Decoder: GIF89a / GIF87a bytes → RGB pixels, handling extension blocks,
//!   local color tables, and the standard LZW special cases.

use super::image_codec::RawDecodeResult;
use std::collections::HashMap;

// ─────────────────────────────────────────────────────────────────────────────
// Error type
// ─────────────────────────────────────────────────────────────────────────────

/// Errors that can occur during GIF encode / decode operations.
#[derive(Debug, thiserror::Error)]
pub enum GifError {
    /// The data is structurally invalid.
    #[error("Invalid GIF: {0}")]
    Invalid(String),
    /// The byte stream ended prematurely.
    #[error("Truncated GIF data")]
    Truncated,
    /// An error in the LZW layer.
    #[error("LZW error: {0}")]
    LzwError(String),
}

// ─────────────────────────────────────────────────────────────────────────────
// Colour quantisation — median-cut algorithm
// ─────────────────────────────────────────────────────────────────────────────

/// One bucket of pixels used during median-cut quantisation.
struct Bucket {
    /// Flat list of (R, G, B) triples belonging to this bucket.
    pixels: Vec<[u8; 3]>,
}

impl Bucket {
    fn new(pixels: Vec<[u8; 3]>) -> Self {
        Self { pixels }
    }

    /// Range of each channel.
    fn channel_ranges(&self) -> ([u8; 3], [u8; 3]) {
        let mut min = [255u8; 3];
        let mut max = [0u8; 3];
        for p in &self.pixels {
            for c in 0..3 {
                if p[c] < min[c] {
                    min[c] = p[c];
                }
                if p[c] > max[c] {
                    max[c] = p[c];
                }
            }
        }
        (min, max)
    }

    /// Widest channel index.
    fn widest_channel(&self) -> usize {
        let (min, max) = self.channel_ranges();
        let ranges = [
            max[0].saturating_sub(min[0]),
            max[1].saturating_sub(min[1]),
            max[2].saturating_sub(min[2]),
        ];
        if ranges[0] >= ranges[1] && ranges[0] >= ranges[2] {
            0
        } else if ranges[1] >= ranges[2] {
            1
        } else {
            2
        }
    }

    /// Split the bucket along the median of the widest channel.
    /// Returns `None` if the bucket cannot be split (single pixel or all same).
    fn split(mut self) -> Option<(Bucket, Bucket)> {
        if self.pixels.len() < 2 {
            return None;
        }
        let ch = self.widest_channel();
        self.pixels.sort_unstable_by_key(|p| p[ch]);
        let mid = self.pixels.len() / 2;
        // Require that the two halves actually differ in the split channel,
        // otherwise there is no real division to be made.
        if self.pixels[0][ch] == self.pixels[self.pixels.len() - 1][ch] {
            return None;
        }
        let right = self.pixels.split_off(mid);
        Some((Bucket::new(self.pixels), Bucket::new(right)))
    }

    /// Representative colour: the unweighted average of all pixels.
    fn representative(&self) -> [u8; 3] {
        if self.pixels.is_empty() {
            return [0, 0, 0];
        }
        let mut sum = [0u64; 3];
        for p in &self.pixels {
            sum[0] += p[0] as u64;
            sum[1] += p[1] as u64;
            sum[2] += p[2] as u64;
        }
        let n = self.pixels.len() as u64;
        [(sum[0] / n) as u8, (sum[1] / n) as u8, (sum[2] / n) as u8]
    }
}

/// Quantise `pixels` (tightly-packed RGB) to at most `max_colors` palette entries.
///
/// Returns `(palette, index_map)` where `palette` is the list of representative
/// colours and `index_map[i]` gives the palette index for pixel `i`.
fn median_cut_quantize(pixels: &[[u8; 3]], max_colors: usize) -> (Vec<[u8; 3]>, Vec<u8>) {
    if pixels.is_empty() {
        return (vec![[0, 0, 0]], vec![]);
    }

    // Seed with one bucket holding every pixel.
    let mut buckets: Vec<Bucket> = vec![Bucket::new(pixels.to_vec())];

    // Split until we reach max_colors or no more splits are possible.
    while buckets.len() < max_colors {
        // Pick the bucket with the largest range (most "spreadable").
        let pick = buckets
            .iter()
            .enumerate()
            .map(|(i, b)| {
                let (mn, mx) = b.channel_ranges();
                let spread = (mx[0].saturating_sub(mn[0]) as u32)
                    + (mx[1].saturating_sub(mn[1]) as u32)
                    + (mx[2].saturating_sub(mn[2]) as u32);
                (spread, i)
            })
            .max_by_key(|&(s, _)| s);

        let idx = match pick {
            Some((0, _)) | None => break, // No splittable bucket.
            Some((_, i)) => i,
        };

        let bucket = buckets.remove(idx);
        match bucket.split() {
            Some((a, b)) => {
                buckets.push(a);
                buckets.push(b);
            }
            None => {
                // Put it back — it can't be split.
                buckets.insert(idx, Bucket::new(vec![]));
                break;
            }
        }
    }

    // Build the palette from bucket representatives.
    let palette: Vec<[u8; 3]> = buckets.iter().map(|b| b.representative()).collect();

    // Assign each pixel to the nearest palette entry (Euclidean² distance).
    let index_map: Vec<u8> = pixels
        .iter()
        .map(|p| nearest_palette_index(p, &palette))
        .collect();

    (palette, index_map)
}

/// Return the index of the palette entry closest to `pixel` in Euclidean² space.
fn nearest_palette_index(pixel: &[u8; 3], palette: &[[u8; 3]]) -> u8 {
    let mut best_idx = 0usize;
    let mut best_dist = u32::MAX;
    for (i, entry) in palette.iter().enumerate() {
        let dr = pixel[0] as i32 - entry[0] as i32;
        let dg = pixel[1] as i32 - entry[1] as i32;
        let db = pixel[2] as i32 - entry[2] as i32;
        let dist = (dr * dr + dg * dg + db * db) as u32;
        if dist < best_dist {
            best_dist = dist;
            best_idx = i;
            if dist == 0 {
                break;
            }
        }
    }
    best_idx as u8
}

// ─────────────────────────────────────────────────────────────────────────────
// LZW compressor (GIF variant — LSB-first bit packing)
// ─────────────────────────────────────────────────────────────────────────────

/// Compress `indices` using GIF LZW with the given `min_code_size`.
///
/// Returns the raw bit-stream bytes (not yet wrapped in sub-blocks).
fn lzw_compress(indices: &[u8], min_code_size: u8) -> Vec<u8> {
    let clear_code = 1u32 << min_code_size;
    let end_code = clear_code + 1;
    let initial_next = end_code + 1;

    // The code table maps (prefix_code, suffix_byte) → new_code.
    let mut table: HashMap<(u32, u8), u32> = HashMap::new();
    let mut next_code = initial_next;
    let mut code_width = min_code_size as u32 + 1;
    // Threshold at which we increase code_width.
    let mut next_threshold = 1u32 << code_width;

    let mut bit_buf = 0u64; // Bit accumulator (LSB-first).
    let mut bit_len = 0u32; // Number of valid bits in bit_buf.
    let mut out: Vec<u8> = Vec::new();

    /// Flush `bits` bits from bit_buf into `out` (LSB first).
    fn emit(code: u32, bits: u32, buf: &mut u64, len: &mut u32, out: &mut Vec<u8>) {
        *buf |= (code as u64) << *len;
        *len += bits;
        while *len >= 8 {
            out.push((*buf & 0xFF) as u8);
            *buf >>= 8;
            *len -= 8;
        }
    }

    // Helper to reset the code table to initial state.
    macro_rules! reset_table {
        () => {{
            table.clear();
            next_code = initial_next;
            code_width = min_code_size as u32 + 1;
            next_threshold = 1u32 << code_width;
        }};
    }

    // Emit the initial Clear code.
    emit(clear_code, code_width, &mut bit_buf, &mut bit_len, &mut out);

    if indices.is_empty() {
        emit(end_code, code_width, &mut bit_buf, &mut bit_len, &mut out);
        // Flush remaining bits.
        if bit_len > 0 {
            out.push((bit_buf & 0xFF) as u8);
        }
        return out;
    }

    let mut string_code = indices[0] as u32;

    for &byte in &indices[1..] {
        let key = (string_code, byte);
        match table.get(&key) {
            Some(&existing) => {
                string_code = existing;
            }
            None => {
                // Emit the current string code.
                emit(
                    string_code,
                    code_width,
                    &mut bit_buf,
                    &mut bit_len,
                    &mut out,
                );

                if next_code < 4096 {
                    table.insert(key, next_code);
                    next_code += 1;
                    if next_code > next_threshold && code_width < 12 {
                        code_width += 1;
                        next_threshold = 1u32 << code_width;
                    }
                } else {
                    // Table full: emit Clear and reset.
                    emit(clear_code, code_width, &mut bit_buf, &mut bit_len, &mut out);
                    reset_table!();
                }

                string_code = byte as u32;
            }
        }
    }

    // Emit the remaining string.
    emit(
        string_code,
        code_width,
        &mut bit_buf,
        &mut bit_len,
        &mut out,
    );
    // Emit End code.
    emit(end_code, code_width, &mut bit_buf, &mut bit_len, &mut out);
    // Flush any remaining bits.
    if bit_len > 0 {
        out.push((bit_buf & 0xFF) as u8);
    }

    out
}

/// Wrap `lzw_data` in GIF sub-blocks (length-prefixed chunks of ≤255 bytes).
fn wrap_in_sub_blocks(lzw_data: &[u8]) -> Vec<u8> {
    let mut out = Vec::new();
    for chunk in lzw_data.chunks(255) {
        out.push(chunk.len() as u8);
        out.extend_from_slice(chunk);
    }
    out.push(0); // Block terminator.
    out
}

// ─────────────────────────────────────────────────────────────────────────────
// Public encoder
// ─────────────────────────────────────────────────────────────────────────────

/// Encode a raw RGB pixel buffer as a GIF89a byte stream.
///
/// `pixels` must contain exactly `width * height * 3` bytes in row-major,
/// top-to-bottom, R-G-B order.
pub fn gif_encode_rgb(width: u32, height: u32, pixels: &[u8]) -> Result<Vec<u8>, GifError> {
    let pixel_count = (width as usize) * (height as usize);
    if pixels.len() < pixel_count * 3 {
        return Err(GifError::Invalid(format!(
            "pixel buffer too short: expected {} bytes, got {}",
            pixel_count * 3,
            pixels.len()
        )));
    }
    if width == 0 || height == 0 {
        return Err(GifError::Invalid("width and height must be > 0".into()));
    }

    // Reinterpret the flat u8 slice as an array of RGB triples.
    let rgb_pixels: Vec<[u8; 3]> = pixels
        .chunks_exact(3)
        .take(pixel_count)
        .map(|c| [c[0], c[1], c[2]])
        .collect();

    // Quantise to ≤256 colours.
    let max_colors = 256usize;
    let (mut palette, index_map) = median_cut_quantize(&rgb_pixels, max_colors);

    // GIF requires the palette length to be a power of two.  Find the smallest
    // power-of-two size ≥ palette.len() that is also ≥ 2.
    let palette_len = palette.len().max(2);
    let depth = {
        let mut d = 1u8;
        while (1usize << d) < palette_len {
            d += 1;
        }
        d // actual bit depth; palette contains 2^depth entries
    };
    // Pad palette to exactly 2^depth entries.
    while palette.len() < (1 << depth) {
        palette.push([0, 0, 0]);
    }

    let gct_size_field = depth - 1; // field value n means 2^(n+1) entries
    let packed_byte: u8 = 0x80 |                       // Global Color Table flag
        ((depth - 1) << 4) |         // colour resolution (depth - 1)
        gct_size_field; // GCT size field

    let min_code_size = depth.max(2); // GIF spec: minimum is 2

    let mut out: Vec<u8> = Vec::new();

    // ── Header ────────────────────────────────────────────────────────────────
    out.extend_from_slice(b"GIF89a");

    // ── Logical Screen Descriptor ─────────────────────────────────────────────
    out.extend_from_slice(&(width as u16).to_le_bytes());
    out.extend_from_slice(&(height as u16).to_le_bytes());
    out.push(packed_byte);
    out.push(0); // Background colour index.
    out.push(0); // Pixel aspect ratio.

    // ── Global Color Table ────────────────────────────────────────────────────
    for entry in &palette {
        out.push(entry[0]);
        out.push(entry[1]);
        out.push(entry[2]);
    }

    // ── Image Descriptor ──────────────────────────────────────────────────────
    out.push(0x2C); // Image Separator.
    out.extend_from_slice(&0u16.to_le_bytes()); // Left.
    out.extend_from_slice(&0u16.to_le_bytes()); // Top.
    out.extend_from_slice(&(width as u16).to_le_bytes());
    out.extend_from_slice(&(height as u16).to_le_bytes());
    out.push(0x00); // Packed: no local table, not interlaced.

    // ── LZW Minimum Code Size ─────────────────────────────────────────────────
    out.push(min_code_size);

    // ── Image Data ────────────────────────────────────────────────────────────
    let lzw_bytes = lzw_compress(&index_map, min_code_size);
    let sub_blocks = wrap_in_sub_blocks(&lzw_bytes);
    out.extend_from_slice(&sub_blocks);

    // ── Trailer ───────────────────────────────────────────────────────────────
    out.push(0x3B);

    Ok(out)
}

// ─────────────────────────────────────────────────────────────────────────────
// LZW decompressor (GIF variant — LSB-first bit reading)
// ─────────────────────────────────────────────────────────────────────────────

/// Bit-level reader that extracts variable-width codes (LSB-first) from a byte
/// slice.
struct BitReader<'a> {
    data: &'a [u8],
    pos: usize, // byte position
    bit_buf: u32,
    bit_len: u32,
}

impl<'a> BitReader<'a> {
    fn new(data: &'a [u8]) -> Self {
        Self {
            data,
            pos: 0,
            bit_buf: 0,
            bit_len: 0,
        }
    }

    /// Read `n` bits LSB-first.  Returns `None` if the stream is exhausted.
    fn read_bits(&mut self, n: u32) -> Option<u32> {
        while self.bit_len < n {
            if self.pos >= self.data.len() {
                return None;
            }
            self.bit_buf |= (self.data[self.pos] as u32) << self.bit_len;
            self.pos += 1;
            self.bit_len += 8;
        }
        let mask = (1u32 << n) - 1;
        let val = self.bit_buf & mask;
        self.bit_buf >>= n;
        self.bit_len -= n;
        Some(val)
    }
}

/// Concatenate all sub-block data bytes from a GIF image data section.
///
/// `bytes` must point to the start of the first sub-block (after the LZW
/// minimum code size byte).  Returns the concatenated payload and the number
/// of source bytes consumed (including the trailing zero-length block).
fn read_sub_blocks(bytes: &[u8]) -> Result<(Vec<u8>, usize), GifError> {
    let mut out: Vec<u8> = Vec::new();
    let mut pos = 0usize;
    loop {
        if pos >= bytes.len() {
            return Err(GifError::Truncated);
        }
        let block_len = bytes[pos] as usize;
        pos += 1;
        if block_len == 0 {
            break;
        }
        if pos + block_len > bytes.len() {
            return Err(GifError::Truncated);
        }
        out.extend_from_slice(&bytes[pos..pos + block_len]);
        pos += block_len;
    }
    Ok((out, pos))
}

/// GIF LZW decompressor.
///
/// Decompresses `data` using `min_code_size` and returns the index stream.
///
/// The code table uses a linked-list representation: each entry stores
/// (prefix_code, suffix_byte).  For the initial leaf codes prefix_code is
/// `u16::MAX` (sentinel meaning "no prefix").  Strings are reconstructed by
/// walking the chain backwards and then reversing.
///
/// Code-width growth rule (GIF spec / Netscape interpretation):
///   After emitting a code that fills the current range — i.e. after the table
///   size reaches `2^code_width` — the *next* code read uses `code_width + 1`
///   bits (up to a maximum of 12).
fn lzw_decompress(data: &[u8], min_code_size: u8) -> Result<Vec<u8>, GifError> {
    let clear_code = 1u16 << min_code_size;
    let end_code = clear_code + 1;
    let table_initial_size = (end_code + 1) as usize;

    // prefix[i] == u16::MAX  →  leaf (no prefix)
    let mut prefix: Vec<u16> = Vec::with_capacity(4096);
    let mut suffix: Vec<u8> = Vec::with_capacity(4096);

    // Seed: single-byte leaf codes 0..clear_code.
    for i in 0..(clear_code as usize) {
        prefix.push(u16::MAX);
        suffix.push(i as u8);
    }
    // clear_code slot
    prefix.push(u16::MAX);
    suffix.push(0);
    // end_code slot
    prefix.push(u16::MAX);
    suffix.push(0);

    let mut code_width = min_code_size as u32 + 1;

    let mut reader = BitReader::new(data);
    let mut output: Vec<u8> = Vec::new();

    // Reusable scratch buffer for string reconstruction.
    let mut scratch: Vec<u8> = Vec::new();

    // Walk the chain for `code` and write the string (reversed) into `scratch`.
    let walk_chain =
        |code: u16, pfx: &[u16], sfx: &[u8], scratch: &mut Vec<u8>| -> Result<(), GifError> {
            scratch.clear();
            let mut c = code;
            let mut depth = 0usize;
            loop {
                if (c as usize) >= pfx.len() {
                    return Err(GifError::LzwError(format!("code {} not in table", c)));
                }
                scratch.push(sfx[c as usize]);
                let p = pfx[c as usize];
                if p == u16::MAX {
                    break;
                }
                c = p;
                depth += 1;
                if depth > 4096 {
                    return Err(GifError::LzwError("cycle in LZW chain".into()));
                }
            }
            Ok(())
        };

    // Returns the first (root) byte for `code`.
    let first_byte = |code: u16, pfx: &[u16], sfx: &[u8]| -> u8 {
        let mut c = code;
        loop {
            let p = pfx[c as usize];
            if p == u16::MAX {
                return sfx[c as usize];
            }
            c = p;
        }
    };

    let mut prev_code: Option<u16> = None;
    // Track whether we are waiting for the first non-clear code.
    let mut after_clear = true;

    loop {
        let raw = reader.read_bits(code_width).ok_or(GifError::Truncated)?;
        let code = raw as u16;

        if code == end_code {
            break;
        }

        if code == clear_code {
            prefix.truncate(table_initial_size);
            suffix.truncate(table_initial_size);
            code_width = min_code_size as u32 + 1;
            prev_code = None;
            after_clear = true;
            continue;
        }

        // ── First code after a clear ──────────────────────────────────────────
        if after_clear {
            // The first code after Clear must be in the initial table.
            if (code as usize) >= table_initial_size {
                return Err(GifError::LzwError(format!(
                    "first code after clear ({}) exceeds initial table size ({})",
                    code, table_initial_size
                )));
            }
            output.push(suffix[code as usize]);
            prev_code = Some(code);
            after_clear = false;
            // No new entry is added for the first code.
            continue;
        }

        let prev = prev_code.ok_or_else(|| GifError::LzwError("no prev code".into()))?;
        let next_code = prefix.len() as u16;

        if (code as usize) < prefix.len() {
            // ── Known code path ───────────────────────────────────────────────
            // Decode the string for `code`.
            walk_chain(code, &prefix, &suffix, &mut scratch)?;
            scratch.reverse();

            // New table entry: prev + first_byte(code).
            let fb = scratch[0];
            if next_code < 4096 {
                prefix.push(prev);
                suffix.push(fb);
            }

            output.extend_from_slice(&scratch);
        } else if code == next_code {
            // ── Special case: code == next table slot ─────────────────────────
            // The string is prev_string + prev_string[0].
            let fb = first_byte(prev, &prefix, &suffix);

            if next_code < 4096 {
                prefix.push(prev);
                suffix.push(fb);
            }

            // Now decode the newly-added entry (which equals `prev` + `fb`).
            walk_chain(prev, &prefix, &suffix, &mut scratch)?;
            scratch.reverse();
            scratch.push(fb);

            output.extend_from_slice(&scratch);
        } else {
            return Err(GifError::LzwError(format!(
                "code {} is beyond next expected entry {}",
                code, next_code
            )));
        }

        prev_code = Some(code);

        // Grow code_width once the table has filled the current slot range.
        // The threshold is: when table size == 2^code_width (i.e. when the
        // table is exactly full), the *next* read uses code_width + 1.
        if code_width < 12 && prefix.len() == (1 << code_width) {
            code_width += 1;
        }
    }

    Ok(output)
}

// ─────────────────────────────────────────────────────────────────────────────
// Public decoder
// ─────────────────────────────────────────────────────────────────────────────

/// Decode a GIF87a or GIF89a byte stream into raw RGB pixels.
///
/// Only the first frame is decoded.  The result always uses 3 bytes per pixel
/// (R, G, B) regardless of the original colour depth.
pub fn gif_decode(bytes: &[u8]) -> Result<RawDecodeResult, GifError> {
    if bytes.len() < 6 {
        return Err(GifError::Truncated);
    }

    // ── Header ────────────────────────────────────────────────────────────────
    if &bytes[..6] != b"GIF89a" && &bytes[..6] != b"GIF87a" {
        return Err(GifError::Invalid(format!(
            "bad GIF signature: {:?}",
            &bytes[..6.min(bytes.len())]
        )));
    }

    if bytes.len() < 13 {
        return Err(GifError::Truncated);
    }

    // ── Logical Screen Descriptor ─────────────────────────────────────────────
    let canvas_width = u16::from_le_bytes([bytes[6], bytes[7]]) as usize;
    let canvas_height = u16::from_le_bytes([bytes[8], bytes[9]]) as usize;
    let lsd_packed = bytes[10];
    let has_gct = (lsd_packed & 0x80) != 0;
    let gct_size_field = lsd_packed & 0x07;
    let gct_entries = 1usize << (gct_size_field as usize + 1);

    let mut pos = 13usize; // Skip LSD (7 bytes past header).

    // ── Global Colour Table ───────────────────────────────────────────────────
    let mut global_palette: Vec<[u8; 3]> = Vec::new();
    if has_gct {
        let gct_bytes = gct_entries * 3;
        if pos + gct_bytes > bytes.len() {
            return Err(GifError::Truncated);
        }
        for i in 0..gct_entries {
            global_palette.push([
                bytes[pos + i * 3],
                bytes[pos + i * 3 + 1],
                bytes[pos + i * 3 + 2],
            ]);
        }
        pos += gct_bytes;
    }

    // ── Walk blocks until we hit an Image Descriptor ──────────────────────────
    loop {
        if pos >= bytes.len() {
            return Err(GifError::Truncated);
        }
        match bytes[pos] {
            0x3B => {
                // Trailer before we found an image.
                return Err(GifError::Invalid("GIF contains no image frames".into()));
            }
            0x21 => {
                // Extension block.
                pos += 1;
                if pos >= bytes.len() {
                    return Err(GifError::Truncated);
                }
                pos += 1; // Skip extension label.
                          // Skip all sub-blocks.
                loop {
                    if pos >= bytes.len() {
                        return Err(GifError::Truncated);
                    }
                    let block_len = bytes[pos] as usize;
                    pos += 1;
                    if block_len == 0 {
                        break;
                    }
                    pos += block_len;
                    if pos > bytes.len() {
                        return Err(GifError::Truncated);
                    }
                }
            }
            0x2C => {
                // Image Descriptor.
                break;
            }
            other => {
                return Err(GifError::Invalid(format!(
                    "unexpected block byte 0x{:02X} at offset {}",
                    other, pos
                )));
            }
        }
    }

    // ── Image Descriptor ──────────────────────────────────────────────────────
    // pos now points at 0x2C.
    if pos + 10 > bytes.len() {
        return Err(GifError::Truncated);
    }
    let img_width = u16::from_le_bytes([bytes[pos + 5], bytes[pos + 6]]) as usize;
    let img_height = u16::from_le_bytes([bytes[pos + 7], bytes[pos + 8]]) as usize;
    let img_packed = bytes[pos + 9];
    let has_lct = (img_packed & 0x80) != 0;
    let lct_size_field = img_packed & 0x07;
    pos += 10; // Consume Image Descriptor.

    // ── Local Colour Table (if present) ───────────────────────────────────────
    let active_palette: Vec<[u8; 3]> = if has_lct {
        let lct_entries = 1usize << (lct_size_field as usize + 1);
        let lct_bytes = lct_entries * 3;
        if pos + lct_bytes > bytes.len() {
            return Err(GifError::Truncated);
        }
        let mut lct = Vec::with_capacity(lct_entries);
        for i in 0..lct_entries {
            lct.push([
                bytes[pos + i * 3],
                bytes[pos + i * 3 + 1],
                bytes[pos + i * 3 + 2],
            ]);
        }
        pos += lct_bytes;
        lct
    } else {
        global_palette
    };

    if active_palette.is_empty() {
        return Err(GifError::Invalid("no colour table available".into()));
    }

    // ── LZW Minimum Code Size ─────────────────────────────────────────────────
    if pos >= bytes.len() {
        return Err(GifError::Truncated);
    }
    let min_code_size = bytes[pos];
    pos += 1;

    if !(2..=11).contains(&min_code_size) {
        return Err(GifError::Invalid(format!(
            "invalid LZW minimum code size: {}",
            min_code_size
        )));
    }

    // ── Image Sub-blocks ──────────────────────────────────────────────────────
    if pos >= bytes.len() {
        return Err(GifError::Truncated);
    }
    let (lzw_data, _consumed) = read_sub_blocks(&bytes[pos..])?;

    // ── LZW Decompress ────────────────────────────────────────────────────────
    let indices = lzw_decompress(&lzw_data, min_code_size)?;

    // Use canvas dimensions if image dimensions are 0 (degenerate case).
    let out_width = if img_width == 0 {
        canvas_width
    } else {
        img_width
    };
    let out_height = if img_height == 0 {
        canvas_height
    } else {
        img_height
    };
    let pixel_count = out_width * out_height;

    // Map indices → RGB.
    let mut pixels: Vec<u8> = Vec::with_capacity(pixel_count * 3);
    for i in 0..pixel_count {
        let idx = if i < indices.len() {
            indices[i] as usize
        } else {
            0
        };
        let color = if idx < active_palette.len() {
            active_palette[idx]
        } else {
            [0, 0, 0]
        };
        pixels.push(color[0]);
        pixels.push(color[1]);
        pixels.push(color[2]);
    }

    Ok(RawDecodeResult {
        width: out_width,
        height: out_height,
        pixels,
    })
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    /// Build a solid-colour `width × height` RGB pixel buffer.
    fn solid_rgb(width: u32, height: u32, r: u8, g: u8, b: u8) -> Vec<u8> {
        let n = (width * height) as usize;
        let mut buf = Vec::with_capacity(n * 3);
        for _ in 0..n {
            buf.push(r);
            buf.push(g);
            buf.push(b);
        }
        buf
    }

    /// Build a simple gradient `width × height` RGB pixel buffer.
    fn gradient_rgb(width: u32, height: u32) -> Vec<u8> {
        let mut buf = Vec::new();
        for y in 0..height {
            for x in 0..width {
                buf.push(((x * 255) / width.max(1)) as u8);
                buf.push(((y * 255) / height.max(1)) as u8);
                buf.push(128u8);
            }
        }
        buf
    }

    #[test]
    fn test_gif_header() {
        let pixels = solid_rgb(4, 4, 200, 10, 10);
        let gif = gif_encode_rgb(4, 4, &pixels).expect("encode");
        assert!(
            gif.starts_with(b"GIF89a"),
            "GIF must start with GIF89a signature"
        );
    }

    #[test]
    fn test_gif_trailer() {
        let pixels = solid_rgb(4, 4, 10, 200, 10);
        let gif = gif_encode_rgb(4, 4, &pixels).expect("encode");
        assert_eq!(
            *gif.last().expect("non-empty"),
            0x3Bu8,
            "GIF must end with 0x3B trailer"
        );
    }

    #[test]
    fn test_gif_roundtrip_solid_color() {
        let pixels = solid_rgb(4, 4, 220, 20, 20);
        let gif = gif_encode_rgb(4, 4, &pixels).expect("encode");
        let result = gif_decode(&gif).expect("decode");
        // At least half the pixels should be "reddish".
        let reddish = result
            .pixels
            .chunks_exact(3)
            .filter(|p| p[0] >= 200 && p[1] <= 50 && p[2] <= 50)
            .count();
        let total = result.width * result.height;
        assert!(
            reddish * 2 >= total,
            "expected reddish pixels: got {}/{} reddish",
            reddish,
            total
        );
    }

    #[test]
    fn test_gif_roundtrip_size() {
        let pixels = gradient_rgb(8, 8);
        let gif = gif_encode_rgb(8, 8, &pixels).expect("encode");
        let result = gif_decode(&gif).expect("decode");
        assert_eq!(result.width, 8);
        assert_eq!(result.height, 8);
    }

    #[test]
    fn test_gif_lzw_clear_code() {
        // The LZW stream for min_code_size=8 starts with the clear code (256 = 0x100).
        // In LSB-first 9-bit packing the first 9 bits are:
        //   0x100 = 0b1_0000_0000 → byte[0] = 0x00, bits [0..1] of byte[1] = 0b1
        // The stream is wrapped in sub-blocks; the first sub-block byte is the
        // length, then the data starts at byte[1] of the encoded output.
        let pixels = solid_rgb(2, 2, 100, 100, 100);
        let gif = gif_encode_rgb(2, 2, &pixels).expect("encode");

        // Locate the LZW minimum code size byte.
        // Offset: 6 (header) + 7 (LSD) + palette_bytes + 10 (image descriptor) + 1 (min code).
        // We search for 0x2C (image separator) to locate the image descriptor.
        let sep_pos = gif
            .iter()
            .position(|&b| b == 0x2C)
            .expect("image separator");
        let min_code_size_pos = sep_pos + 10;
        assert!(
            min_code_size_pos + 2 < gif.len(),
            "GIF too short for LZW data"
        );
        let min_code_size = gif[min_code_size_pos];
        let clear_code = 1u32 << min_code_size;

        // The first sub-block starts right after the min_code_size byte.
        let sub_block_start = min_code_size_pos + 1;
        assert!(
            sub_block_start < gif.len(),
            "No sub-block after min_code_size"
        );
        let sub_block_len = gif[sub_block_start] as usize;
        assert!(sub_block_len > 0, "First sub-block must be non-empty");

        // Extract the first sub-block's data bytes.
        let data_start = sub_block_start + 1;
        assert!(
            data_start + sub_block_len <= gif.len(),
            "Sub-block data truncated"
        );
        let lzw_data = &gif[data_start..data_start + sub_block_len];

        // Read the first `min_code_size + 1` bits LSB-first and verify they
        // equal the clear code.
        let code_width = min_code_size as u32 + 1;
        let mut bit_buf = 0u32;
        for (i, &byte) in lzw_data.iter().take(4).enumerate() {
            bit_buf |= (byte as u32) << (i * 8);
        }
        let mask = (1u32 << code_width) - 1;
        let first_code = bit_buf & mask;
        assert_eq!(
            first_code, clear_code,
            "First LZW code must be the clear code ({}); got {}",
            clear_code, first_code
        );
    }

    #[test]
    fn test_gif_decode_invalid_returns_error() {
        let bad = b"NOT_GIF";
        let result = gif_decode(bad);
        assert!(result.is_err(), "Expected error for invalid GIF magic");
    }

    #[test]
    fn test_gif_decode_empty_returns_error() {
        let result = gif_decode(&[]);
        assert!(result.is_err(), "Expected error for empty input");
    }

    #[test]
    fn test_gif_encode_dimensions_preserved() {
        let pixels = gradient_rgb(10, 6);
        let gif = gif_encode_rgb(10, 6, &pixels).expect("encode");
        // Logical Screen Descriptor width at bytes 6-7, height at bytes 8-9.
        let w = u16::from_le_bytes([gif[6], gif[7]]) as u32;
        let h = u16::from_le_bytes([gif[8], gif[9]]) as u32;
        assert_eq!(w, 10, "encoded width mismatch");
        assert_eq!(h, 6, "encoded height mismatch");
    }
}