Nanotechnology Promises Much

    @thi.ng/rle-pack
    TypeScript icon, indicating that this package has built-in type declarations

    3.1.6 • Public • Published

    rle-pack

    npm version npm downloads Twitter Follow

    This project is part of the @thi.ng/umbrella monorepo.

    About

    Binary run-length encoding packer/unpacker with support for customizable input word sizes (1 - 32 bits) and repeat count (run-length) bit sizes (1 - 16 bits). The encoder uses 4 different repeat group sizes (thresholds) to minimize the number of bits used to store the run lengths. The range of supported run lengths is 16 bits (i.e. 65536 repetitions). If a value is repeated more often than that, the remainder will be encoded using additional RLE chunks...

    Encoding format

    data layout

    • 32 bits - original number of words
    • 5 bits - word size
    • 16 bits - 4x RLE repeat group / chunk sizes (in bits)

    The default group sizes are: 3, 4, 8, 16, i.e. 8, 16, 256, 65536 repetitions

    Then per value:

    • 1 bit - encoding flag (1 = RLE encoded, 0 = single occurrence)
    • 2 bits - repeat or chunk class ID
    • m bits - repeat count or chunk size (if greater than max group size then split into chunks...)
    • n bits - value(s)

    Status

    STABLE - used in production

    Search or submit any issues for this package

    Related packages

    • @thi.ng/binary - 100+ assorted binary / bitwise operations, conversions, utilities, lookup tables
    • @thi.ng/bitstream - ES6 iterator based read/write bit streams with support for variable word widths
    • @thi.ng/range-coder - Binary data range encoder / decoder

    Installation

    yarn add @thi.ng/rle-pack

    ES module import:

    <script type="module" src="https://cdn.skypack.dev/@thi.ng/rle-pack"></script>

    Skypack documentation

    For Node.js REPL:

    # with flag only for < v16
    node --experimental-repl-await
    
    > const rlePack = await import("@thi.ng/rle-pack");
    

    Package sizes (gzipped, pre-treeshake): ESM: 688 bytes

    Dependencies

    API

    Generated API docs

    import { encode, decode } from "@thi.ng/rle-pack";
    
    // prepare dummy data
    src = new Uint8Array(1024);
    src.set([1,1,1,1,1,2,2,2,2,3,3,3,4,4,5,4,4,3,3,3,2,2,2,2,1,1,1,1,1], 512);
    
    // pack data
    packed = encode(src, src.length);
    packed.length
    // 30 => 2.93% of original
    
    // pack with custom word size (3 bits, i.e. our value range is only 0-7)
    // and use custom repeat group sizes suitable for our data
    alt = encode(src, src.length, 3, [1, 2, 3, 9]);
    alt.length
    // 20 => 1.95% of original, 66% of default config
    
    // unpack
    unpacked = new Uint8Array(decode(alt));

    Authors

    Karsten Schmidt

    If this project contributes to an academic publication, please cite it as:

    @misc{thing-rle-pack,
      title = "@thi.ng/rle-pack",
      author = "Karsten Schmidt",
      note = "https://thi.ng/rle-pack",
      year = 2017
    }

    License

    © 2017 - 2022 Karsten Schmidt // Apache Software License 2.0

    Install

    npm i @thi.ng/rle-pack

    DownloadsWeekly Downloads

    1,450

    Version

    3.1.6

    License

    Apache-2.0

    Unpacked Size

    26.2 kB

    Total Files

    6

    Last publish

    Collaborators

    • thi.ng