Skip to main content

pltrs_codec/
lib.rs

1//! Self-contained image and compression codecs for pltrs.
2//!
3//! The crate has no dependencies and no `unsafe` code. It provides:
4//!
5//! * [`av1`] — an intra-only AV1 encoder for still pictures: 4:2:0,
6//!   4:4:4 and monochrome, lossy or lossless.
7//! * [`avif`] — AVIF files of RGBA pixels, coded with [`av1`] and laid out
8//!   as Pillow writes them through libavif.
9//! * [`base64`] — base64 encoding for embedding binary data in text, and
10//!   decoding.
11//! * [`checksum`] — CRC-32 (PNG, gzip) and Adler-32 (zlib) checksums, both as
12//!   incremental hashers and as one-shot helpers.
13//! * [`gif`] — animated GIF encoding with a shared, quantised palette, and
14//!   decoding of a file's first frame as Pillow opens it.
15//! * [`jpeg`] — baseline JPEG encoding matching Pillow's output byte for
16//!   byte, and baseline, extended and progressive decoding whose pixels are
17//!   Pillow's (libjpeg-turbo's arithmetic, ported).
18//! * [`deflate`] — raw DEFLATE (RFC 1951): a compressor writing zlib-ng
19//!   2.3's bytes at levels 0–9 and every strategy, and a strictly
20//!   validating, table-driven decompressor.
21//! * [`tiff`] — uncompressed RGBA TIFF files, laid out as Pillow writes them,
22//!   and decoding of the TIFF layouts and compressions Pillow reads.
23//! * [`raster`] — decoded JPEG, TIFF, GIF and WebP images in the Pillow mode
24//!   the same file opens in, convertible to RGBA as Pillow converts them.
25//! * [`sha1`] — the SHA-1 digest, as the WebSocket handshake needs it.
26//! * [`sha256`] — the SHA-256 digest (SVG ids, the `text.usetex` cache).
27//! * [`webp`] — decoding of WebP files as Pillow opens them, lossless
28//!   (VP8L) and lossy (VP8, with alpha) bitstreams, still or animated, and
29//!   lossless encoding of still images and animations.
30//! * [`zlib`] — the RFC 1950 container (header and Adler-32 trailer) around
31//!   DEFLATE, in both directions.
32//! * [`png`] — a PNG encoder (grey, grey + alpha, RGB and RGBA at 8 or 16 bits,
33//!   rows filtered and compressed as Pillow does with zlib-ng, `pHYs`
34//!   resolution and text metadata) and a
35//!   decoder for every standard PNG flavour: all colour types and bit depths,
36//!   palettes, `tRNS` transparency and Adam7 interlacing.
37//!
38//! Every decoder in this crate treats its input as untrusted: malformed data
39//! produces a descriptive error value, never a panic, and declared image
40//! sizes are checked against the input before anything is allocated.
41//!
42//! # Example
43//!
44//! ```
45//! use pltrs_codec::png::{self, ImageRef, PhysicalDimensions};
46//!
47//! // A 2×1 RGBA image: an opaque red pixel and a half-transparent blue one.
48//! let pixels = [255, 0, 0, 255, 0, 0, 255, 128];
49//! let image = ImageRef::rgba8(2, 1, &pixels)?;
50//! let bytes = png::Encoder::new().dpi(100.0).encode(image)?;
51//!
52//! let decoded = png::decode(&bytes)?;
53//! assert_eq!(decoded.to_rgba8(), pixels);
54//! assert_eq!(decoded.metadata().physical, PhysicalDimensions::from_dpi(100.0));
55//! # Ok::<(), Box<dyn std::error::Error>>(())
56//! ```
57#![forbid(unsafe_code)]
58
59pub mod av1;
60pub mod avif;
61pub mod base64;
62pub mod checksum;
63pub mod deflate;
64pub mod gif;
65pub mod jpeg;
66pub mod png;
67pub mod raster;
68pub mod sha1;
69pub mod sha256;
70pub mod tiff;
71pub mod webp;
72pub mod zlib;
73
74pub use deflate::CompressionLevel;
75
76#[cfg(test)]
77mod test_util;