Fix Monochrome class with new data structure, unit tests. There is work to be done still
All checks were successful
Build ptprnt / build (push) Successful in 3m51s

This commit is contained in:
2025-09-11 10:02:43 +02:00
parent 5132eab6fa
commit 3dc5da6fc8
16 changed files with 436 additions and 131 deletions

View File

@@ -22,7 +22,6 @@
#include <gtest/gtest.h>
#include <cstdint>
#include <optional>
#include <vector>
TEST(basic_test, Bitmap_createBitmapWithCertainSize_yieldsSpecifiedSize) {
@@ -36,34 +35,28 @@ TEST(basic_test, Bitmap_createBitmapWithCertainSize_yieldsSpecifiedSize) {
TEST(basic_test, Bitmap_getBitmapLineOutsideOfImage_yieldsNullopt) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
// line 8 is out of bounds, count begins with 0
auto outOfBoundsLine = bm.getLine(8);
ASSERT_EQ(std::nullopt, outOfBoundsLine);
EXPECT_ANY_THROW(auto outOfBoundsLine = bm.getLine(8));
}
TEST(basic_test, Bitmap_getBitmapLineInsideOfImage_yieldsValidLineSize) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
auto line = bm.getLine(7);
if (!line) {
FAIL() << "Returned line is invalid";
}
auto lineSize = line->size();
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
auto line = bm.getLine(7);
auto lineSize = line.size();
ASSERT_EQ(16, lineSize);
}
TEST(basic_test, Bitmap_getBitmapColOutsideOfImage_yieldsNullopt) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
// col 16 is out of bounds, count begins with 0
auto outOfBoundsCol = bm.getCol(16);
ASSERT_EQ(std::nullopt, outOfBoundsCol);
EXPECT_ANY_THROW(auto outOfBoundsCol = bm.getCol(16));
}
TEST(basic_test, Bitmap_getBitmapColInsideOfImage_yieldsValidColSize) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
auto col = bm.getCol(15);
if (!col) {
FAIL() << "Returned Col is invalid";
}
auto colSize = col->size();
auto colSize = col.size();
ASSERT_EQ(8, colSize);
}

View File

@@ -22,22 +22,22 @@
#include <gtest/gtest.h>
TEST(basic_test, Monochrome_convertGrayscale_yieldsMonochrome) {
const std::vector<uint8_t> pixels({0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00});
const std::vector<uint8_t> pixels(
{0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00});
const std::vector<uint8_t> expected({0b10101010, 0b10101010});
auto mono = ptprnt::graphics::Monochrome(pixels);
auto mono = ptprnt::graphics::Monochrome(pixels, 16, 1);
auto out = mono.get();
EXPECT_EQ(out, expected);
}
TEST(basic_test, Monochrome_convertInvertedGrayscale_yieldsInvertedMonochrome) {
const std::vector<uint8_t> pixels({0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00});
const std::vector<uint8_t> pixels(
{0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00});
const std::vector<uint8_t> expected({0b01010101, 0b01010101});
auto mono = ptprnt::graphics::Monochrome(pixels);
auto mono = ptprnt::graphics::Monochrome(pixels, 16, 1);
mono.invert(true);
auto out = mono.get();
@@ -45,11 +45,11 @@ TEST(basic_test, Monochrome_convertInvertedGrayscale_yieldsInvertedMonochrome) {
}
TEST(basic_test, Monochrome_convertWithCustomThreshhold_yieldsMonochromeRespectingThreshhold) {
const std::vector<uint8_t> pixels({0x0F, 0x11, 0x0F, 0x11, 0x0F, 0x11, 0x0F, 0x11, 0x0F, 0x11,
0x0F, 0x11, 0x0F, 0x11, 0x0F, 0x11});
const std::vector<uint8_t> pixels(
{0x0F, 0x11, 0x0F, 0x11, 0x0F, 0x11, 0x0F, 0x11, 0x0F, 0x11, 0x0F, 0x11, 0x0F, 0x11, 0x0F, 0x11});
const std::vector<uint8_t> expected({0b01010101, 0b01010101});
auto mono = ptprnt::graphics::Monochrome(pixels);
auto mono = ptprnt::graphics::Monochrome(pixels, 16, 1);
mono.setThreshold(16);
auto out = mono.get();
@@ -60,12 +60,104 @@ TEST(basic_test, Monochrome_convertNonAlignedPixels_spillsOverIntoNewByte) {
// TODO: We need to find to access the vector without the possiblity of out-of-bounds access
// Ideas: constexpr? compile time check?
GTEST_SKIP() << "Skipping this test, as ASAN will halt as this is an out-of-bounds access";
const std::vector<uint8_t> pixels({0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF});
const std::vector<uint8_t> pixels(
{0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF});
const std::vector<uint8_t> expected({0b10101010, 0b10101010, 0b10000000});
auto mono = ptprnt::graphics::Monochrome(pixels);
auto mono = ptprnt::graphics::Monochrome(pixels, 17, 1);
auto out = mono.get();
EXPECT_EQ(out, expected);
}
TEST(MonochromeData_test, MonochromeData_getMonochromeData_returnsStructWithCorrectData) {
const std::vector<uint8_t> pixels({0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00});
auto mono = ptprnt::graphics::Monochrome(pixels, 8, 1);
auto monoData = mono.getMonochromeData();
EXPECT_EQ(monoData.bytes.size(), 1);
EXPECT_EQ(monoData.bytes[0], 0b10101010);
EXPECT_EQ(monoData.width, 8);
EXPECT_EQ(monoData.height, 1);
EXPECT_EQ(monoData.stride, 1);
EXPECT_EQ(monoData.orientation, ptprnt::graphics::Orientation::LANDSCAPE);
}
TEST(MonochromeData_test, MonochromeData2x2_transformToPortrait_rotatesCorrectly) {
// Create a 2x2 image with a specific pattern
// Pixels are laid out row-major: row0_col0, row0_col1, row1_col0, ...
const std::vector<uint8_t> pixels({0xFF, 0x00, 0x00, 0xFF});
auto mono = ptprnt::graphics::Monochrome(pixels, 2, 2);
auto monoData = mono.getMonochromeData();
monoData.transformTo(ptprnt::graphics::Orientation::PORTRAIT);
// After 90° clockwise rotation:
// Original: █ . -> Rotated: . █
// . █ █ .
EXPECT_EQ(monoData.width, 2);
EXPECT_EQ(monoData.height, 2);
EXPECT_EQ(monoData.orientation, ptprnt::graphics::Orientation::PORTRAIT);
// check pixel data ...................................... x,y = value
EXPECT_EQ(monoData.getBit(0, 0), false); // 0,0 = white
EXPECT_EQ(monoData.getBit(1, 0), true); // 0,1 = black
EXPECT_EQ(monoData.getBit(0, 1), true); // 1,0 = black
EXPECT_EQ(monoData.getBit(1, 1), false); // 1,1 = white
}
TEST(MonochromeData_test, MonochromeData3x2_transformToPortrait_rotatesCorrectly) {
// Create a 2x3 image with a specific pattern
// Pixels are laid out row-major: row0_col0, row0_col1, row0_col2, row1_col0, ...
const std::vector<uint8_t> pixels({0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF});
auto mono = ptprnt::graphics::Monochrome(pixels, 3, 2);
auto monoData = mono.getMonochromeData();
monoData.transformTo(ptprnt::graphics::Orientation::PORTRAIT);
// After 90° clockwise rotation:
// Original: █ . . -> Rotated: █ █
// █ . █ . .
// █ .
EXPECT_EQ(monoData.width, 2);
EXPECT_EQ(monoData.height, 3);
EXPECT_EQ(monoData.orientation, ptprnt::graphics::Orientation::PORTRAIT);
// check pixel data ...................................... x,y = value
EXPECT_EQ(monoData.getBit(0, 0), true); // 1,1 = black
EXPECT_EQ(monoData.getBit(1, 0), true); // 1,2 = black
EXPECT_EQ(monoData.getBit(0, 1), false); // 2,1 = white
EXPECT_EQ(monoData.getBit(1, 1), false); // 2,2 = white
EXPECT_EQ(monoData.getBit(0, 2), true); // 3,1 = black
EXPECT_EQ(monoData.getBit(1, 2), false); // 3,2 = white
}
TEST(MonochromeData_test, MonochromeData3x2_transformToPortrait_rotatesCorrectlyCounterclockwise) {
// Create a 2x3 image with a specific pattern
// Pixels are laid out row-major: row0_col0, row0_col1, row0_col2, row1_col0, ...
const std::vector<uint8_t> pixels({0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF});
auto mono = ptprnt::graphics::Monochrome(pixels, 3, 2);
auto monoData = mono.getMonochromeData();
monoData.transformTo(ptprnt::graphics::Orientation::PORTRAIT_FLIPPED);
// After 90° anti-clockwise rotation:
// Original: █ . . -> Rotated: . █
// █ . █ . .
// █ █
EXPECT_EQ(monoData.width, 2);
EXPECT_EQ(monoData.height, 3);
EXPECT_EQ(monoData.orientation, ptprnt::graphics::Orientation::PORTRAIT_FLIPPED);
// check pixel data ...................................... x,y = value
EXPECT_EQ(monoData.getBit(0, 0), false); // 1,1 = white
EXPECT_EQ(monoData.getBit(1, 0), true); // 1,2 = black
EXPECT_EQ(monoData.getBit(0, 1), false); // 2,1 = white
EXPECT_EQ(monoData.getBit(1, 1), false); // 2,2 = white
EXPECT_EQ(monoData.getBit(0, 2), true); // 3,1 = black
EXPECT_EQ(monoData.getBit(1, 2), true); // 3,2 = black
}