Somewhere around 2003, I was drawing things with Turbo C++ on Windows XP. I remembered writing a little program I called SNG, and that it worked around some limitation. Twenty-three years later, I could still remember the compiler and the operating system. The actual problem? Something about screenshots, maybe?

Then I opened the backup. The opening comment in grab.H gave it away.

My hack for taking screenshots of DOS full screen mode under XP

There it is! The problem was getting the picture out of the full-screen DOS graphics session. The files don't record what happened when I pressed Print Screen, or even whether that was the exact thing I tried. They do record the workaround. The header says 2005 and the surviving source and sample files are dated August 2005. So circa 2003 is how I remember the project, and August 2005 is the version I can actually put my hands on. Time flies.

Original black-on-white BGI capture with a sine wave, axes, a square, a circle, and the words Rights Copyleft

The original image.png from the backup, all 4,186 bytes of it. The tiny “Rights Copyleft ;)” in the bottom right survived too.

What Turbo C++ could see

The sample was a C program with Borland's <graphics.h>. It called initgraph(), pointed it at ..\\BGI, and drew a sine wave, a square, a circle, and some text. BGI stood for Borland Graphics Interface. The backup even has EGAVGA.BGI, the graphics driver.

Once the picture was on screen, the same library that drew the pixels could read them back with getpixel(x, y). This was the way out. I could ask the program to save its own graphics before it left graphics mode, then hand that file to a different program to make the PNG. The whole trip fit on one line.

Turbo C++ / BGI  →  getpixel()  →  image01.sng  →  sng.exe  →  image01.png

The backup has a little trail of prototypes. old_code/LATEST.C writes just the rows of digits to image.data. old_code/MSHT.c puts the SNG header in front of the rows and writes to a fixed image.sng. The reusable grab.H takes a filename. At that point I could add the include and one call to an existing graphics program. That's also the approach documented in README!!!.txt. Three exclamation marks felt about right.

Calling it from Turbo C++

Here is a lightly tidied version of USR.C. It keeps the drawing calls and the original 3.14. Put grab.h and template.sng in the working directory, and adjust the BGI driver path for your installation. The original grabber expects the graphics mode to be 640 × 480.

#include <conio.h>
#include <graphics.h>
#include <math.h>
#include "grab.h"

int main(void)
{
    int driver = DETECT, mode;
    int x, max_x, max_y, mid_y;

    initgraph(&driver, &mode, "..\\BGI");
    if (graphresult() != grOk)
        return 1;

    max_x = getmaxx();
    max_y = getmaxy();
    if (max_x != 639 || max_y != 479) {
        closegraph();
        return 1;
    }
    mid_y = max_y / 2;

    line(0, 0, 0, max_y);
    setcolor(GREEN);
    line(0, mid_y, max_x, mid_y);

    setcolor(RED);
    outtextxy(20, 10, "Sine Waveform");
    outtextxy(400, 400, "Rights Copyleft ;)");
    rectangle(100, 100, 200, 200);
    circle(max_x / 2, mid_y, 100);

    for (x = 0; x < max_x; ++x) {
        double y = sin(x * 3.14 / 180);
        putpixel(x, mid_y - 60 * y, WHITE);
    }

    grab("image01.sng");
    getch();
    closegraph();
    return 0;
}

The important line is grab("image01.sng"). It goes after the last drawing call and before closegraph(), while BGI can still read the image. This is the pixel loop from the grabber, reformatted.

/* The contents of template.sng have already been copied to fp. */
for (j = 0; j < 480; ++j) {
    for (i = 0; i < 640; ++i) {
        c = getpixel(i, j);
        putc(c == 0 ? '0' : '1', fp);
    }
    putc('\n', fp);
}
putc('}', fp);

That is 307,200 calls to getpixel(), one printable character per pixel, and a newline after each row. The two fopen() calls in the original go unchecked. The size is hard-coded, and the grabber expects template.sng to be in the current directory. As a little utility for my own graphics programs, it had exactly as many options as I needed that day.

Why all those zeros and ones work

I wrote the DOS-side grabber. The converter in the backup is Eric S. Raymond's Scriptable Network Graphics, a text representation of PNG. The tarball I saved was sng-1.0.2.tar.gz, a release from November 2003. SNG lets you describe the image header, palette, metadata, and pixels in a text file. SNG project · Version 1.0.2 release archive

Here is a complete 4 × 3 version of the idea. Save it as tiny.sng.

#SNG: A tiny cousin of template.sng
IHDR {
    width: 4; height: 3; bitdepth: 8;
    using color palette;
}
PLTE {
    (255,255,255)
    (0,0,0)
}
IMAGE {
    pixels base64
    0110
    1001
    0110
}

The words pixels base64 look odd above a grid of zeros and ones. In SNG this is a one-character-per-value encoding. For this 8-bit indexed image, 0 means palette index zero, 1 means palette index one, and the converter skips the line breaks. The real template.sng also asks for interlacing and supplies chromaticity, significant-bit information, a title, and an author tag of dsk. The template ends partway through the IMAGE block so the grabber can append the pixels and the closing brace. SNG manual

Palette index zero is white in the PNG, so the zero-valued BGI background becomes white paper. Every other BGI color becomes black ink. The green axis, red lettering, and white sine wave all come out black. The README explicitly says the result can be viewed and printed. That palette makes a lot of sense for a printout.

The Windows side of the operation

The README says to click makeupng.pif and enter image01.sng when asked. The actual Program Information File in the backup is named makeupng_run.PIF. It points at makeupng.bat and has ? in its parameters field. The batch file basically comes down to this one line:

sng %1

There was also makepng.bat for the fixed-name case, which simply ran sng image.sng. The bundled sng.exe is a 32-bit Windows console executable with imports from Cygwin and libpng. Sitting next to it were cygwin1.dll, cygpng12.dll, and cygz.dll. Two programs on the same machine, two environments, and a text file they could both understand.

Replay the conversion

The source and sample archive contains the original C and header files, template, batch files, README, and saved SNGs. With sng on your path, you can replay the conversion on a current machine using standard input and output.

sng < tiny.sng > tiny.png
sng < IMAGE01.SNG > image01.png

The second command uses the saved capture directly. The backup's IMAGE01.SNG is 308,741 bytes. I compared all of its pixel indices, using the palette in the file, with the pixels in the surviving 4,186-byte image.png. They match exactly. The two files have different base names in the backup and the PNG has fewer metadata chunks, so the match establishes that they contain the same picture.

Original simpler capture with a sine wave crossing a horizontal axis

The backup also has sample1.png, a simpler capture with just the wave and axes. This one weighs 2,528 bytes.

I still don't remember what I needed the first screenshot for. Looking at it now, I'm just glad I went to the trouble of saving it. There's the square, the circle, the sine wave calculated with 3.14, and a little message in the corner from 2005.

PS: After writing this article, I vaguely recall that this stuff was for some college course I was pursuing back then.