Files
ptouch-prnt/tests/monochrome_test/monochrome_test.cpp
2025-09-11 10:02:43 +02:00

163 lines
6.8 KiB
C++

/*
ptrnt - print labels on linux
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "graphics/Monochrome.hpp"
#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> expected({0b10101010, 0b10101010});
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> expected({0b01010101, 0b01010101});
auto mono = ptprnt::graphics::Monochrome(pixels, 16, 1);
mono.invert(true);
auto out = mono.get();
EXPECT_EQ(out, expected);
}
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> expected({0b01010101, 0b01010101});
auto mono = ptprnt::graphics::Monochrome(pixels, 16, 1);
mono.setThreshold(16);
auto out = mono.get();
EXPECT_EQ(out, expected);
}
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> expected({0b10101010, 0b10101010, 0b10000000});
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
}