Jonathan. Frech’s WebBlog

brain­fuck X (#178)

Jonathan Frech

While browsing StackExchange PCG⁠¹ questions and answers, I came across a challenge re­gard­ing drawing the swiss flag. In particular, I was interested in benzene’s answer, in which they showcased a brain­fuck dialect capable of creating two-di­men­sion­al 24-bit color images. In this post I present this dialect with slight changes of my own, as well as an interpreter I wrote in Python 2.7 (source code is listed below and can also be downloaded).

Urban Müller’s original brain­fuck (my vanilla brain­fuck post can be found here) works similar to a Turing ma­chine, in that the memory consists of a theoretically infinitely large tape with individual cells which can be modified. What allows brain­fuck X (or braindraw, as benzene called their dialect) to create color images is, that instead of a one-di­men­sion­al tape, a three-di­men­sion­al tape is used. This tape extends infinitely in two spacial dimensions and has three color planes. Each cell’s value is limited to a byte (an integer value from 0 to 255) which results in a 24-bit color depth.

Adding to brain­fuck’s eight commands (+-<>[].,), there are two characters to move up and down the tape (^v) and one character to move forwards in the color di­men­sion (*). Starting on the red color plane, continuing with the green and ending in the blue. After the blue color plane, the color planes cycle and the red color plane is selected. benzene’s original lan­guage de­sign which I altered slightly had three characters (rgb) to directly select a color plane. Whilst this version is supported by my interpreter )the flag --colorletters is necessary for that func­tion­al­i­ty(, I find my color star more brainfucky — directly calling color planes by their name seems nearly readable.
brain­fuck’s vanilla eight characters still work in the same way, brain­fuck X can thereby execute any vanilla brain­fuck program⁠². Also, there still is a plaintext output — the tape’s image is a program’s secondary output.

Having executed the final brain­fuck instruction, the interpreter prints out the tape to the terminal — using ANSI escape codes. Because of this, the color depth is truncated in the terminal view, as there are on­ly 216 colors supported.⁠³
For the full 24-bit color depth output, I use the highly inefficient Portable Pixmap Format (.ppm) as an output image file format. To open .ppm files, I recommend using the GNU Image Manipulation Program; specifying the output file name is done via the --output flag.

The Swiss flag image above was gen­er­ated by benzene’s braindraw code (see their StackExchange answer linked to above); the resulting .ppm file was then scaled and converted using GIMP.
Interpreter command: python brainfuckx.py swiss.bfx -l -o swiss.ppm

Usage

Source code: brain­fuck-x.py


[1][2020-08-05] Now sadly renamed and squelched.
[2][2020-08-05] Assuming the added command letters are first discarded from the brain­fuck source.
[3][2020-08-05] When using the 6-depth ANSI color escape codes.