Most recent update: 12th February 2021 - 06:30:16 - 1968 characters

Zero dependency images in Rust

One of the most upvoted posts on the Rust subreddit has been A fractal I rendered with rust without any external libraries. Whilst the output is beautiful there were a few complaints as zero dependencies still included Tokio, image, and rand.

In recent days I had a desire to recreate my experience of an early foray into numerical computing. Rust seemed like an obvious candidate!

In brief: chaos and the logistic map

Rather than fractals we'll be playing with chaos by producing a plot - a bifurcation diagram, specifically the logistic map. This diagram represents the behaviour a simple repeated equation, \(x_{t+1} = rx_t(1 - x_t)\), repeated over and over.

Exporting images: the Portable GrayMap (PGM)

The PGM format is the simplest way of producing an image, representing the pixels in a standard space delimited text format. If you're interested in colour you can look to the Portable PixMap (PPM) as well.

Converting to PNG

convert

Multi-threading for speed

Threads. Threads are shiny and make many cores nice.

Is this better than tokio? Absolutely not. Does it work well enough, for a varying definition of well enough? Absolutely.

What about rand?

In this we've provided (minimal) replacements for the tokio and imagecrates. What about rand however?

Assuming you don't need a strong random number generated, and especially if you don't have any intent of letting it even within a billion bytes of usage in cryptography, you have a number of options.