module

CrImage::WEBP::Writer

Writer handles VP8L bitstream generation for WebP lossless encoding.

Implements the VP8L lossless compression algorithm including:

  • Transform encoding (predictor, subtract green, color, palette)
  • LZ77 backward references
  • Color cache
  • Huffman encoding
  • Bitstream formatting

Constants

DISTANCE_MAP = [96, 73, 55, 39, 23, 13, 5, 1, 255, 255, 255, 255, 255, 255, 255, 255, 101, 78, 58, 42, 26, 16, 8, 2, 0, 3, 9, 17, 27, 43, 59, 79, 102, 86, 62, 46, 32, 20, 10, 6, 4, 7, 11, 21, 33, 47, 63, 87, 105, 90, 70, 52, 37, 28, 18, 14, 12, 15, 19, 29, 38, 53, 71, 91, 110, 99, 82, 66, 48, 35, 30, 24, 22, 25, 31, 36, 49, 67, 83, 100, 115, 108, 94, 76, 64, 50, 44, 40, 34, 41, 45, 51, 65, 77, 95, 109, 118, 113, 103, 92, 80, 68, 60, 56, 54, 57, 61, 69, 81, 93, 104, 114, 119, 116, 111, 106, 97, 88, 84, 74, 72, 75, 85, 89, 98, 107, 112, 117]

Distance map for nearby pixels (WebP specification)

TRANSFORM_COLOR = 1
TRANSFORM_COLOR_INDEXING = 3
TRANSFORM_PREDICT = 0

Transform types

TRANSFORM_SUB_GREEN = 2

Class methods

compute_histograms(encoded : Array(Int32), color_cache_bits : Int32) : Array(Array(Int32))

Computes histograms for 5 alphabets.

VP8L uses 5 separate Huffman alphabets:

  • 0: Green/literals + length codes + cache codes
  • 1: Red
  • 2: Blue
  • 3: Alpha
  • 4: Distance codes
Source
encode_image_data(pixels : Array(CrImage::Color::NRGBA), width : Int32, height : Int32, color_cache_bits : Int32) : Array(Int32)

Encodes image data with LZ77 and color cache.

Applies LZ77 backward references to find repeated pixel sequences, uses color cache for recently seen colors, and encodes literals for unique pixels. Returns array of encoded symbols.

Source
write_bitstream(img : CrImage::NRGBA) : Tuple(Bytes, Bool)

Generates VP8L bitstream from NRGBA image.

Applies transforms, encodes with LZ77 and Huffman coding, and generates the complete VP8L bitstream.

Returns: Tuple of {bitstream_data, has_alpha}

Source