A simple hacked BMP image class. Image can be drawn to, and saved as a .bmp file
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");
}