Class SimpleBMPImage

A simple hacked BMP image class. Image can be drawn to, and saved as a .bmp file

class SimpleBMPImage ;

Constructors

NameDescription
this (w, h) Create a RGB image (3 byte per pixel, no alpha)
this (w, h, pxdata) Create a RGB image (3 byte per pixel, no alpha)
this (w, h, iterMap) Import a iteration map using uint[]. Scale colores over range 0..max
this (w, h, signedMap) Import a iteration map using float[]. Scales 2 color rangeses over range -max..0..max

Methods

NameDescription
encodeAsBMPData () Encode Data as BMP image and return data
getPixel (x, y) Gets the color of the pixel at the specified position.
modifyPixel (x, y, red, green, blue) Add colors (-255 to 255) to pixel at x,y (origin at lower left)
saveBMP (filename) Save Data as BMP
setPixel (x, y, red, green, blue) Sets the color of a pixel at a specified position in a 2D grid.
setPixel (x, y, px) dito
toBinary () Return the pixelarray as ubyte array.

Example

{
    auto imgObj = new SimpleBMPImage(2, 2);
    auto bmpData = imgObj.encodeAsBMPData();
    // writeln(bmpData);
    static ubyte[] expectBmpData = [
        66, 77, 66, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, 2, 0, 0, 0,
        2, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 12, 0, 0, 0, 184, 11, 0, 0, 184,
        11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    ];
    if (bmpData != expectBmpData) {

        write(toRawDataDiff(bmpData, expectBmpData));
    }
    assert(bmpData == expectBmpData, "Should be reference data.");

    imgObj.modifyPixel(0, 0, 64, 64, 64);
    imgObj.modifyPixel(0, 0, -64, -64, -64);
    auto bmpData2 = imgObj.encodeAsBMPData();
    assert(bmpData2[] == expectBmpData, "Should be reference data.");

    auto px = imgObj.getPixel(1, 1);
    imgObj.setPixel(1, 1, 200, 150, 100);
    imgObj.setPixel(1, 1, px);
    bmpData2 = imgObj.encodeAsBMPData();
    assert(bmpData2[] == expectBmpData, "Should be reference data.");
}
{
    auto imgObj = new SimpleBMPImage(2, 2, new uint[4]);
    auto bmpData = imgObj.encodeAsBMPData();
    // writeln(bmpData);
    static ubyte[] expectBmpData = [
        66, 77, 66, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, 2, 0, 0, 0,
        2, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 12, 0, 0, 0, 184, 11, 0, 0, 184,
        11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    ];
    if (bmpData != expectBmpData) {

        write(toRawDataDiff(bmpData, expectBmpData));
    }
    assert(bmpData == expectBmpData, "Should be reference data.");
}
{
    const bool[] boolPmp = [true, false, false, true];
    auto imgObj = new SimpleBMPImage(2, 2, boolPmp);
    auto bmpData = imgObj.encodeAsBMPData();
    // writeln(bmpData);
    static ubyte[] expectBmpData = [
        66, 77, 66, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, 2, 0, 0, 0,
        2, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 12, 0, 0, 0, 184, 11, 0, 0, 184,
        11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 0, 0, 0, 0, 0, 0, 255,
        255, 255
    ];
    assert(bmpData[] == expectBmpData, "Should be reference data.");
}
{
    const uint[] uintPmp = [10, 20, 30, 40];
    auto imgObj = new SimpleBMPImage(2, 2, uintPmp);
    auto bmpData = imgObj.encodeAsBMPData();
    // writeln(bmpData);
    static ubyte[] expectBmpData = [
        66, 77, 66, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, 2, 0, 0, 0,
        2, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 12, 0, 0, 0, 184, 11, 0, 0, 184,
        11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 143, 239, 135, 237, 164, 38, 199, 51,
        4, 25, 25, 25
    ];
    assert(bmpData[] == expectBmpData, "Should be reference data.");
    imgObj.saveBMP("tests/tmp/u8pcm-2x2.bmp");

}
{
    const float[] uintPmp = [10, 20, 30, 40];
    auto imgObj = new SimpleBMPImage(2, 2, uintPmp);
    auto bmpData = imgObj.encodeAsBMPData();
    // writeln(bmpData);
    static ubyte[] expectBmpData = [
        66, 77, 66, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, 2, 0, 0, 0,
        2, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 12, 0, 0, 0, 184, 11, 0, 0, 184,
        11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 63, 2, 5, 127, 5, 7, 191, 7, 10,
        255, 10
    ];
    assert(bmpData[] == expectBmpData, "Should be reference data.");
    imgObj.saveBMP("tests/tmp/float-2x2.bmp");
}