Skip to main content

Crate bic

Crate bic 

Source
Expand description

BIC: an experimental lossless and near-lossless image codec for images with large solid areas, such as screenshots, user interfaces, diagrams, charts, icons and pixel art. On those it produces smaller files than optimized PNG. On photographs it offers little or no advantage: a JPEG source is stored unchanged, and lossless photographs gain little.

BIC files are optimized for size first and decoding speed second. Each image is coded with a palette or with a subtract-green transform, then row prediction, run-length blocks and Zstandard. The encoder tries every combination and keeps the smallest. When a source file (PNG, JPEG, WebP or HEIF) is smaller than that, it is stored unchanged. The wire format is specified in FORMAT.md in the repository.

§Encoding pixels

use bic::{decode_rgba, encode_rgba, EncodeOptions};

// A 2×2 RGBA image.
let rgba = [255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 255, 255, 255];
let file = encode_rgba(2, 2, &rgba, EncodeOptions::default())?;
let (width, height, pixels) = decode_rgba(&file)?;
assert_eq!((width, height, pixels.as_slice()), (2, 2, &rgba[..]));

§Encoding a file

encode_file reads a PNG, JPEG, WebP or HEIF file (and BMP, GIF, TIFF or QOI with the cli feature), applies its orientation, keeps its ICC profile, and stores the original unchanged when that is smaller:

let original = std::fs::read("photo.jpg")?;
let file = bic::encode_file(&original, bic::EncodeOptions::default())?;
std::fs::write("photo.bic", file)?;

§Untrusted input

Decoding checks the declared size against DecodeLimits before allocating. Use decode_rgba_with_limits or decode_with_metadata with tighter limits for files from untrusted sources.

§Features

  • cli (default): the bic and bic-view programs and their dependencies. Library users can disable it with default-features = false.

Structs§

DecodeLimits
Resource caps applied before the decoder allocates anything.
Decoded
A decoded image.
EncodeOptions
Encoder settings. The defaults give the smallest lossless file.
Metadata
Metadata stored alongside the pixels.
Source
A decoded source image.

Enums§

EmbeddableFormat
Formats a BIC file may store unchanged.
Storage
How a BIC file stores its image.

Functions§

decode_rgba
Decodes a BIC file to (width, height, rgba) with the default limits.
decode_rgba_with_limits
Decodes a BIC file to (width, height, rgba), refusing files that exceed limits before allocating.
decode_with_metadata
Decodes pixels and metadata.
embeddable_format
Identifies an embeddable format from the file’s signature.
encode_file
Encodes an image file (any format read_source reads). See EncodeOptions::keep_original.
encode_rgba
Encodes 8-bit RGBA pixels (row-major, width × height × 4 bytes).
encode_rgba_with_metadata
Like encode_rgba, also storing metadata (such as an ICC profile).
encode_source
Encodes a source already read from original with read_source.
read_source
Decodes an image file. Sizes are checked against limits before the pixels are decoded.
storage
How file stores its image, from the header alone.