Skip to main content

Crate rust_ppm

Crate rust_ppm 

Source
Expand description

§rust-ppm

rust-ppm is a small RGB image and plotting library that writes binary PPM (P6) files.

§Image API

use rust_ppm::{Image, Pixel};

let image = Image::from_pixel_fn(128, 128, |x, y| {
    Pixel::rgb(x as u8, y as u8, 128)
});
image.save("gradient.ppm")?;

§Plot API

Plot is the primary API. It automatically fits finite data, supports multiple styled series, and renders directly to an Image or PPM file:

use rust_ppm::{Axes, LineStyle, MarkerStyle, Pixel, Plot};

let axes = Axes::new()
    .labels("x", "x squared")
    .title("Quadratic samples");
let points = [(-1.0, 1.0), (0.0, 0.0), (2.0, 4.0), (4.0, 16.0)];

Plot::new()
    .size(800, 600)
    .margins(80, 60)
    .axes(axes)
    .line(
        &points,
        LineStyle::new().width(3).color(Pixel::rgb(30, 100, 180)),
    )
    .markers(
        &points,
        MarkerStyle::new().size(5).color(Pixel::rgb(210, 45, 45)),
    )
    .save("plot.ppm")?;

Axes::new() automatically fits each axis to all finite series data. Override either axis with x_limits or y_limits, and provide exact tick positions with x_ticks or y_ticks.

For larger, proportionally scaled output, add .scale(2) or greater. For example, .size(800, 600).scale(2) writes a 1600 by 1200 image and scales margins, text, markers, and line widths. PPM stores pixels rather than physical DPI metadata, so dimensions and scale are the relevant quality controls.

Use Canvas for low-level incremental drawing or changing limits after series have been added. Its styled methods are line and markers; the older plot and scatter methods remain available. The line_plot* and scatter_plot* convenience functions are also retained for compatibility.

See src/main.rs for examples of automatic and manual limits, custom ticks, colors, sizes, multiple series, direct rendering, direct saving, and high-resolution output.

§Notes

  • Coordinates use a bottom-left data origin; image pixels are stored from the top-left.
  • Text uses a compact 8x8 bitmap font. Labels render only when the configured inset has enough room.
  • PPM reading supports binary P6 images with a maximum channel value of 255.

§Development

cargo fmt --check
cargo test --all-targets
cargo clippy --all-targets -- -D warnings
cargo doc --no-deps

rust-ppm is a small RGB image and plotting library for generating binary PPM files. It combines low-level image operations with a compact plotting API for quick visualization.

Re-exports§

pub use image::Image;
pub use image::Pixel;
pub use plot::Axes;
pub use plot::LineStyle;
pub use plot::MarkerStyle;
pub use plot::Plot;
pub use plot::line_plot;
pub use plot::line_plot_with_width;
pub use plot::line_plot_with_width_and_inset;
pub use plot::scatter_plot;
pub use plot::scatter_plot_with_size;
pub use plot::scatter_plot_with_size_and_inset;

Modules§

image
Image primitives and helpers. RGB pixel and image storage.
plot
Plot builders, axes, and rendering utilities. Axes, canvas rendering, and line/scatter convenience functions.
ppm
Binary PPM input/output support. Binary PPM (P6) encoding and decoding.
ppmplot
Backward-compatible re-export module for plot helpers.