Skip to main content

image_formats/
image_formats.rs

1//! One render, four raster formats. Requires every writer feature:
2//!
3//! ```sh
4//! cargo run --example image_formats --features jpeg,tiff,webp
5//! ```
6//!
7//! The plot is rendered over a *transparent* background, which is what makes
8//! the formats differ:
9//!
10//! - `image_formats.png`, `image_formats.tiff`, `image_formats.webp` — carry
11//!   the alpha channel, so the panel floats on transparency.
12//! - `image_formats.jpg` — JPEG has no alpha channel, so the buffer is
13//!   composited onto the background color handed to `write_jpeg`.
14//!
15//! The printed file sizes are the other point of the example: on plot output
16//! WebP is the smallest of the three lossless formats, and JPEG is the
17//! largest of the four — flat fills and hard edges are the worst case for a
18//! DCT codec, and the best case for lossless entropy coding.
19
20use hephaestus::backend::vello::VelloRenderer;
21use hephaestus::color::{rgb8, Color};
22use hephaestus::composition::{Composition, Patch, Span};
23use hephaestus::geometry::Size;
24use hephaestus::image::{write_jpeg, write_png, write_tiff, write_webp, TiffCompression};
25use hephaestus::plot::chrome::axis::{Axis, AxisPlacement};
26use hephaestus::plot::{scale, Plot, PlotComposition, PointGeom};
27use hephaestus::scales::chrome::AxisSide;
28use hephaestus::scene::SceneBuilder;
29use hephaestus::Renderer;
30
31/// Fully transparent: every format that carries alpha keeps it, and JPEG has
32/// to composite it away.
33const TRANSPARENT: Color = Color::new([0.0, 0.0, 0.0, 0.0]);
34
35fn main() {
36    let (w, h) = (900u32, 500u32);
37    let dpi = 96.0;
38
39    let comp = || Composition::empty(1, 1).place(1, 1, Span::cell(), Patch::new("panel"));
40
41    let xs: Vec<f64> = (0..60).map(|i| i as f64 * 1.7).collect();
42    let ys: Vec<f64> = xs
43        .iter()
44        .map(|x| 50.0 + 25.0 * (x * 0.07).sin() - 0.15 * x)
45        .collect();
46
47    let mut plot = Plot::new(&comp(), "panel").bind("x", "x").bind("y", "y");
48    plot.add_geom(
49        PointGeom::builder()
50            .set("x", xs)
51            .set("y", ys)
52            .set("fill", rgb8(70, 120, 220))
53            .set("size", 6.0_f64)
54            .build(),
55    );
56    plot.set_title("One render, four formats");
57    plot.add_axis(Axis::rail("x", AxisPlacement::Cartesian(AxisSide::Bottom)));
58    plot.add_axis(Axis::rail("y", AxisPlacement::Cartesian(AxisSide::Left)));
59
60    let mut view = PlotComposition::new(&comp())
61        .add_scale("x", scale::continuous(0.0..=100.0))
62        .add_scale("y", scale::continuous(20.0..=80.0))
63        .with_plot(plot);
64
65    let mut renderer = VelloRenderer::new().expect("vello renderer init");
66    {
67        let scene = renderer.scene();
68        scene.clear();
69        view.render(scene, Size::new(w as f64, h as f64), dpi);
70    }
71
72    let mut pixels = vec![0u8; (w * h * 4) as usize];
73    renderer
74        .render_to_buffer(w, h, TRANSPARENT, &mut pixels)
75        .expect("render");
76
77    let dir = std::env::current_dir().unwrap().join("examples");
78    let png = dir.join("image_formats.png");
79    let jpg = dir.join("image_formats.jpg");
80    let tif = dir.join("image_formats.tiff");
81    let webp = dir.join("image_formats.webp");
82
83    write_png(&png, w, h, &pixels).expect("write png");
84    // Quality 90, composited onto the light background the plot theme assumes.
85    write_jpeg(&jpg, w, h, &pixels, 90, rgb8(248, 248, 252)).expect("write jpeg");
86    write_tiff(&tif, w, h, &pixels, TiffCompression::Deflate).expect("write tiff");
87    write_webp(&webp, w, h, &pixels).expect("write webp");
88
89    for path in [&png, &jpg, &tif, &webp] {
90        let bytes = std::fs::metadata(path).expect("stat").len();
91        println!(
92            "wrote {} ({:.1} KiB)",
93            path.display(),
94            bytes as f64 / 1024.0
95        );
96    }
97}