#memory #gamedev #encoding #vector #image-compression

palettevec

A palette compressed vector library for potentially insane runtime compression ratios

3 unstable releases

0.2.1 Jan 7, 2025
0.2.0 Jan 6, 2025
0.1.0 Jan 5, 2025

#551 in Compression

Download history 166/week @ 2024-12-31 193/week @ 2025-01-07

359 downloads per month

MIT/Apache

32KB
769 lines

PaletteVec

PaletteVec is space efficient data structure for storing and managing items with a limited set of repeated elements, using a palette-based encoding scheme.

Palette compression has the following advantages:

  • Potentially insane compression ratios
  • Buffer can be manipulated without decompressing
  • Easy to use

Disadvantages:

  • Buffer accesses come with a runtime cost
  • Large palettes (large amount of distinct items) come with a runtime cost of O(palette entries) per buffer access

Use cases

Palette compression has potential to save huge amounts of memory at runtime while still allowing for manipulation of the buffer elements. Most notably, palette compression is used in minecraft for block chunk storage. For most sophisticated voxel games, palette compression is a must.

Palette compression is also used in image compression (Indexed Color Images) and audio compression.

Example

Creating and using a PaletteVec:

use palettevec::PaletteVec;

fn main() {
    let mut vec = PaletteVec::new();

    // Push elements
    vec.push("apple");
    vec.push("banana");
    vec.push("apple");

    // Access elements
    assert_eq!(vec[0], "apple");
    assert_eq!(vec[1], "banana");

    // Modify elements
    vec.set(1, "cherry");
    assert_eq!(vec[1], "cherry");

    // Remove elements
    assert_eq!(vec.pop(), Some(&"apple"));

    // Iterate over elements
    for item in &vec {
        println!("{}", item);
    }
    // Optimizing the palette
    vec.optimize();
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~25–270KB