lafarren.com

// Darren Lafreniere
// Purveyor of fine code and assorted algorithms
#include <assert.h>
#include <fstream>
#include <iostream>

int main(int argc, char *argv[])
{
    const char* filename = "hw.ppm";

    const int scale =  10;
    const int bg    = 255;
    const int fg0   = 224;
    const int fg1   =  32;

    const int intBits = sizeof(int) * 8;
    assert(intBits == 32);
    const int cols = 2;
    const unsigned int data[][cols] =
    {
        {0}, {0}, {0}, {0}, {0}, {0},
        {39653568, 2308738624},
        {38867232, 2320664896},
        {64819488, 2858191168},
        {38867232, 2857535744},
        {39705792, 1368583744},
        {0}, {0}, {0}, {0}, {0}, {0},
    };
    const int rows = sizeof(data) / sizeof(data[0]);

    std::ofstream f(filename, std::ios::out | std::ios::binary);
    f << "P6\n" << (cols * intBits * scale) << " " << (rows * scale) << "\n255\n";

    for (int r = 0; r < rows; ++r)
    {
        for (int y = 0; y < scale; ++y)
        {
            const float a = (r * scale + y) / (rows * scale - 1.0f);
            const unsigned char fg = static_cast<unsigned char>((fg1 - fg0) * a + fg0);

            for (int c = 0; c < cols; ++c)
            {
                const unsigned int d = data[r][c];
                for (int bit = intBits; --bit >= 0; )
                {
                    const unsigned char i = (d >> bit & 1) ? fg : bg;
                    for (int x = 0; x < scale; ++x)
                    {
                        f << i << i << i;
                    }
                }
            }
        }
    }

    std::cout << "Wrote " << filename << std::endl;
    return 0;
}