Just another Random Renegade Bits site

Rust Placeholder PNG Generator

Decided to try my hand at a little rust. Figured a simple CGI application that generates solid color placeholder PNG files as quickly as possible would be a reasonably interesting project.

It’s probably terribly written since I’ve never written Rust before, and it’s syntax is pretty arcane. Nevertheless, the source is available.

How it works: The CGI application reads width and height out of the PATH_INFO tacked onto the end of the URL. It’s pretty simple – just looks for numbers and makes sure they are between 10 and 4000. The first one encountered is used as the width and the height is the second. (I was going to add a color and border setting, but decided this was good enough as a learning exercise. It would not be hard to add either, however.)

A bunch of Vec:<u8> shennanigans ensue and the necessary headers and PNG contents are printed to STDOUT. Partly in an effort to learn, I build the PNG contents without the aid of a PNG library.

The resulting is indeed pretty fast. Once the connection is established, it can usually return even large PNGs in about 40-60 ms.

Some example URLs:

yellow block 1920x1080
/bin/komari.rust/1920/1080
Yellow block 128x128
/bin/komari.rust/128/128
Yellow block 256x256
/bin/komari.rust/256/256

Some take-aways from my first dive into Rust: