raster.rs

crates/veilvoice-video/src/raster.rs

veilvoice-video · 621 lines · read the source here · or on GitHub

Pixels, and a PNG to put them in.

Why the video's pictures are drawn here rather than converted

Everything else this crate draws is SVG, which is right for a page: the reader's browser has a renderer and a font library and neither is this project's problem. A video file is not a page. ffmpeg reads a directory of raster images, and no build of ffmpeg can be assumed to read SVG: that needs librsvg, which most builds do not have, and a picture that fails to encode on somebody's machine is worse than one that was never offered.

The other way is to convert the SVG here, which means an SVG rasteriser. Every usable one is large, and crate::ffmpeg already spends a page explaining why this project will not pull in a large library to make a video file. Doing it anyway, one module over, would make that argument a thing this project says rather than a thing it does.

So the picture is a few hundred lines: rectangles, circles, a face (crate::font) and a PNG writer. That is the whole of what the drawing needs, and it is small enough to read.

The same picture on every machine

No system fonts, no floating-point that varies by platform in a way that reaches a pixel, and no dependency that could quietly change its output. Two people rendering the same recording get identical files, which is the same property the reproducible builds have and is checked the same way: by comparing bytes.

One dependency, and what it is for

miniz_oxide deflates the pixel data, because PNG is deflate and a PNG written with stored blocks is roughly six megabytes a frame. It is pure Rust and it is already in this tree, underneath flate2, which lofty and pgp both pull in, so naming it here adds nothing to the dependency graph that was not being compiled already.

In plain words

The part that draws the video's pictures, dot by dot, and writes them as PNG files.

It is written here rather than borrowed because the video tool this hands its pictures to cannot be relied on to read drawings, and converting them would mean carrying a large piece of somebody else's code, which is the thing this program is careful not to do.

WHAT THIS FILE CONTAINS

621 lines defining 19 functions (14 public), 2 types and 0 constants. Everything below is read out of the source, so it cannot disagree with the code.

The types it owns.

  • struct Canvas line 75 · A picture being drawn, one byte per channel, three channels per pixel.
  • struct Crc line 368 · The CRC-32 PNG puts on every chunk.

What happens when it runs. These are the ways in: public, and nothing else in this file calls them, so they are what an outside caller reaches first.

  • colour line 61 · Read #rrggbb into three bytes.
  • Canvas::width line 96 · How wide it is.
  • Canvas::height line 101 · How tall it is.
  • Canvas::set line 115 · Set the pixel at x, y, doing nothing outside the canvas.
  • Canvas::rgb line 128 · The pixels, row by row from the top, three bytes each.
  • Canvas::rounded_rect line 176 · A rectangle with rounded ends, which is how the level bars are drawn.
    reaches circle, rect, blend
  • Canvas::ring line 241 · A ring, drawn as a filled disc with the middle taken back out.
    reaches at, blend, circle
  • Canvas::text_centred line 302 · Draw text centred on centre_x, with its top at y.
    reaches text, rect
  • Canvas::png line 325 · The picture as a PNG file.
    reaches chunk, new

WHAT CALLS WHAT

colour line 61 Canvas::new line 83 Canvas::width line 96 Canvas::height line 101 Canvas::at line 106 Canvas::set line 115 Canvas::rgb line 128 Canvas::blend line 137 Canvas::rect line 156 Canvas::rounded_rect line 176 Canvas::circle line 202 Canvas::ring line 241 Canvas::text line 274 Canvas::text_centred line 302 Canvas::png line 325 chunk line 354 Crc::new line 373 Crc::eat line 380 Crc::done line 397 entry: a way in: public, and nothing in this file calls it api: public, and also used inside this file helper: private to this file dashed: a call that goes back up, or across a wrapped rank The functions this file defines, and the calls between them. An edge means the callee's name appears, called, inside the caller's body. This is a syntactic reading, not a type-resolved one.

The functions this file defines, and the calls between them. An edge means the callee's name appears, called, inside the caller's body. This is a syntactic reading, not a type-resolved one.

The same graph as Mermaid source
%%{init: {"theme":"base","themeVariables":{"background":"#1a1b26","primaryColor":"#1f2335","primaryTextColor":"#c0caf5","primaryBorderColor":"#7aa2f7","secondaryColor":"#16161e","tertiaryColor":"#16161e","lineColor":"#737aa2","textColor":"#c0caf5","mainBkg":"#1f2335","nodeBorder":"#7aa2f7","clusterBkg":"#16161e","clusterBorder":"#2f3549","fontFamily":"ui-monospace, SFMono-Regular, Consolas, monospace","fontSize":"14px"}}}%%
flowchart TD
    n_colour(["colour<br/>line 61"])
    n_new["Canvas::new<br/>line 83"]
    n_width(["Canvas::width<br/>line 96"])
    n_height(["Canvas::height<br/>line 101"])
    n_at["Canvas::at<br/>line 106"]
    n_set(["Canvas::set<br/>line 115"])
    n_rgb(["Canvas::rgb<br/>line 128"])
    n_blend["Canvas::blend<br/>line 137"]
    n_rect["Canvas::rect<br/>line 156"]
    n_rounded_rect(["Canvas::rounded_rect<br/>line 176"])
    n_circle["Canvas::circle<br/>line 202"]
    n_ring(["Canvas::ring<br/>line 241"])
    n_text["Canvas::text<br/>line 274"]
    n_text_centred(["Canvas::text_centred<br/>line 302"])
    n_png(["Canvas::png<br/>line 325"])
    n_chunk["chunk<br/>line 354"]
    n_new["Crc::new<br/>line 373"]
    n_eat["Crc::eat<br/>line 380"]
    n_done["Crc::done<br/>line 397"]
    n_chunk --> n_new
    n_circle --> n_blend
    n_png --> n_chunk
    n_ring --> n_at
    n_ring --> n_blend
    n_ring --> n_circle
    n_rounded_rect --> n_circle
    n_rounded_rect --> n_rect
    n_text --> n_rect
    n_text_centred --> n_text
    click n_colour href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L61" "open the source"
    click n_new href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L83" "open the source"
    click n_width href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L96" "open the source"
    click n_height href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L101" "open the source"
    click n_at href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L106" "open the source"
    click n_set href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L115" "open the source"
    click n_rgb href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L128" "open the source"
    click n_blend href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L137" "open the source"
    click n_rect href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L156" "open the source"
    click n_rounded_rect href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L176" "open the source"
    click n_circle href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L202" "open the source"
    click n_ring href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L241" "open the source"
    click n_text href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L274" "open the source"
    click n_text_centred href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L302" "open the source"
    click n_png href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L325" "open the source"
    click n_chunk href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L354" "open the source"
    click n_new href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L373" "open the source"
    click n_eat href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L380" "open the source"
    click n_done href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-video/src/raster.rs#L397" "open the source"
    classDef entry fill:#1f2335,stroke:#7aa2f7,color:#c0caf5
    class n_colour,n_width,n_height,n_set,n_rgb,n_rounded_rect,n_ring,n_text_centred,n_png entry
    classDef api fill:#1f2335,stroke:#7dcfff,color:#c0caf5
    class n_new,n_at,n_rect,n_circle,n_text api
    classDef helper fill:#1f2335,stroke:#bb9af7,color:#c0caf5
    class n_blend,n_chunk,n_new,n_eat,n_done helper
    subgraph chart_key ["Key"]
        direction LR
        key_entry["entry: a way in: public, and nothing in this file calls it"]
        key_api["api: public, and also used inside this file"]
        key_helper["helper: private to this file"]
    end
    class key_entry entry
    class key_api api
    class key_helper helper

This site loads no third-party script, so it cannot run Mermaid; the diagram above is the same nodes and edges drawn by the generator instead. GitHub renders the source below directly.

ITEMS

ItemLineDocumentation
Rgb pub type52A colour, as the three bytes a PNG stores.
colour pub fn61Read #rrggbb into three bytes.
Canvas pub struct75A picture being drawn, one byte per channel, three channels per pixel.
Canvas::new pub fn83A canvas of width by height, filled with background.
Canvas::width pub fn96How wide it is.
Canvas::height pub fn101How tall it is.
Canvas::at pub fn106The colour at a point, or None outside the canvas.
Canvas::set pub fn115Set the pixel at x, y, doing nothing outside the canvas.
Canvas::rgb pub fn128The pixels, row by row from the top, three bytes each.
Canvas::blend fn137Mix colour into the pixel at x, y by alpha, from 0.0 to 1.0.
Canvas::rect pub fn156A filled rectangle, clipped to the canvas.
Canvas::rounded_rect pub fn176A rectangle with rounded ends, which is how the level bars are drawn.
Canvas::circle pub fn202A filled circle with a smooth edge.
Canvas::ring pub fn241A ring, drawn as a filled disc with the middle taken back out.
Canvas::text pub fn274Draw text with its left edge at x and its top at y.
Canvas::text_centred pub fn302Draw text centred on centre_x, with its top at y.
Canvas::png pub fn325The picture as a PNG file.
chunk fn354Append one PNG chunk: length, type, data, and the checksum over both.
Crc struct368The CRC-32 PNG puts on every chunk.
Crc::new fn373A fresh checksum, which PNG specifies as starting from all ones rather than from zero.
Crc::eat fn380Fold bytes in.
Crc::done fn397The value to write, which is the running state inverted.