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
All checks were successful
Build ptprnt / build (push) Successful in 3m51s
This commit is contained in:
@@ -25,7 +25,11 @@
|
||||
#include <vector>
|
||||
|
||||
namespace ptprnt::graphics {
|
||||
Monochrome::Monochrome(const std::vector<uint8_t>& grayscale) : mPixels(std::move(grayscale)) {}
|
||||
Monochrome::Monochrome(const std::vector<uint8_t>& grayscale, uint32_t width, uint32_t height)
|
||||
: mPixels(grayscale), mWidth(width), mHeight(height) {}
|
||||
|
||||
Monochrome::Monochrome(const std::span<uint8_t> grayscale, uint32_t width, uint32_t height)
|
||||
: mPixels(grayscale.begin(), grayscale.end()), mWidth(width), mHeight(height) {}
|
||||
|
||||
void Monochrome::setThreshold(uint8_t threshhold) {
|
||||
mThreshhold = threshhold;
|
||||
@@ -36,23 +40,30 @@ void Monochrome::invert(bool shouldInvert) {
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Monochrome::get() {
|
||||
std::vector<uint8_t> outPixels(
|
||||
(static_cast<unsigned int>((mPixels.size() / 8)) + (std::floor(mPixels.size() % 8 + 0.9))));
|
||||
unsigned int outIndex = 0;
|
||||
// Calculate output size for packed format: (width + 7) / 8 bytes per row
|
||||
uint32_t stride = (mWidth + 7) / 8;
|
||||
size_t outputSize = stride * mHeight;
|
||||
std::vector<uint8_t> outPixels(outputSize, 0);
|
||||
|
||||
for (unsigned int byteNo = 0; byteNo < mPixels.size(); byteNo += 8) {
|
||||
for (unsigned int bitNo = 0; bitNo <= 7 && (byteNo + bitNo < mPixels.size()); bitNo++) {
|
||||
if (mPixels[byteNo + bitNo] > mThreshhold) {
|
||||
outPixels[outIndex] |= (1 << (7 - bitNo));
|
||||
} else {
|
||||
outPixels[outIndex] &= ~(1 << (7 - bitNo));
|
||||
// Pack pixels row by row for correct 2D layout
|
||||
for (uint32_t y = 0; y < mHeight; ++y) {
|
||||
for (uint32_t x = 0; x < mWidth; ++x) {
|
||||
size_t pixelIndex = y * mWidth + x; // Row-major index in input
|
||||
size_t byteIndex = y * stride + x / 8; // Byte index in packed output
|
||||
size_t bitIndex = 7 - (x % 8); // MSB first
|
||||
|
||||
// Convert grayscale pixel to bit based on threshold
|
||||
bool pixelOn = mPixels[pixelIndex] > mThreshhold;
|
||||
if (mShouldInvert) {
|
||||
pixelOn = !pixelOn;
|
||||
}
|
||||
|
||||
if (pixelOn) {
|
||||
outPixels[byteIndex] |= (1 << bitIndex);
|
||||
}
|
||||
}
|
||||
if (mShouldInvert) {
|
||||
outPixels[outIndex] = ~outPixels[outIndex];
|
||||
}
|
||||
outIndex++;
|
||||
}
|
||||
|
||||
return outPixels;
|
||||
}
|
||||
|
||||
@@ -71,4 +82,164 @@ void Monochrome::visualize() {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
MonochromeData Monochrome::getMonochromeData() {
|
||||
auto processedBytes = get();
|
||||
// Calculate stride based on packed monochrome data (1 bit per pixel, 8 pixels per byte)
|
||||
auto stride = static_cast<uint32_t>((mWidth + 7) / 8);
|
||||
return {std::move(processedBytes), stride, Orientation::LANDSCAPE, mWidth, mHeight};
|
||||
}
|
||||
|
||||
// MonochromeData transformation methods implementation
|
||||
void MonochromeData::transformTo(Orientation targetOrientation) {
|
||||
if (orientation == targetOrientation) {
|
||||
return; // No transformation needed
|
||||
}
|
||||
|
||||
auto rotatedData = createRotatedData(targetOrientation);
|
||||
bytes = std::move(rotatedData);
|
||||
|
||||
// Update dimensions and stride based on rotation
|
||||
switch (targetOrientation) {
|
||||
case Orientation::PORTRAIT:
|
||||
case Orientation::PORTRAIT_FLIPPED:
|
||||
// Swap width and height for portrait orientations
|
||||
std::swap(width, height);
|
||||
stride = (width + 7) / 8; // Recalculate stride for new width
|
||||
break;
|
||||
case Orientation::LANDSCAPE:
|
||||
case Orientation::LANDSCAPE_FLIPPED:
|
||||
// Keep original stride calculation
|
||||
stride = (width + 7) / 8;
|
||||
break;
|
||||
}
|
||||
|
||||
orientation = targetOrientation;
|
||||
}
|
||||
|
||||
bool MonochromeData::getBit(uint32_t x, uint32_t y) const {
|
||||
if (x >= width || y >= height) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t byteIndex = y * stride + x / 8;
|
||||
uint32_t bitIndex = 7 - (x % 8); // MSB first
|
||||
|
||||
if (byteIndex >= bytes.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bytes[byteIndex] >> bitIndex) & 1;
|
||||
}
|
||||
|
||||
void MonochromeData::setBit(uint32_t x, uint32_t y, bool value) {
|
||||
if (x >= width || y >= height) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t byteIndex = y * stride + x / 8;
|
||||
uint32_t bitIndex = 7 - (x % 8); // MSB first
|
||||
|
||||
if (byteIndex >= bytes.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (value) {
|
||||
bytes[byteIndex] |= (1 << bitIndex);
|
||||
} else {
|
||||
bytes[byteIndex] &= ~(1 << bitIndex);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> MonochromeData::createRotatedData(Orientation targetOrientation) const {
|
||||
uint32_t newWidth, newHeight;
|
||||
|
||||
// Determine new dimensions
|
||||
switch (targetOrientation) {
|
||||
case Orientation::PORTRAIT:
|
||||
case Orientation::PORTRAIT_FLIPPED:
|
||||
newWidth = height;
|
||||
newHeight = width;
|
||||
break;
|
||||
case Orientation::LANDSCAPE:
|
||||
case Orientation::LANDSCAPE_FLIPPED:
|
||||
default:
|
||||
newWidth = width;
|
||||
newHeight = height;
|
||||
break;
|
||||
}
|
||||
|
||||
uint32_t newStride = (newWidth + 7) / 8;
|
||||
std::vector<uint8_t> newBytes(newStride * newHeight, 0);
|
||||
|
||||
// Create a temporary MonochromeData for the new image
|
||||
MonochromeData tempData;
|
||||
tempData.bytes = std::move(newBytes);
|
||||
tempData.stride = newStride;
|
||||
tempData.width = newWidth;
|
||||
tempData.height = newHeight;
|
||||
tempData.orientation = targetOrientation;
|
||||
|
||||
// Copy pixels with appropriate transformation
|
||||
for (uint32_t y = 0; y < height; ++y) {
|
||||
for (uint32_t x = 0; x < width; ++x) {
|
||||
bool pixel = getBit(x, y);
|
||||
uint32_t newX, newY;
|
||||
|
||||
switch (targetOrientation) {
|
||||
case Orientation::LANDSCAPE:
|
||||
newX = x;
|
||||
newY = y;
|
||||
break;
|
||||
case Orientation::PORTRAIT: // 90 degrees clockwise
|
||||
newX = height - 1 - y;
|
||||
newY = x;
|
||||
break;
|
||||
case Orientation::LANDSCAPE_FLIPPED: // 180 degrees
|
||||
newX = width - 1 - x;
|
||||
newY = height - 1 - y;
|
||||
break;
|
||||
case Orientation::PORTRAIT_FLIPPED: // 270 degrees clockwise
|
||||
newX = y;
|
||||
newY = width - 1 - x;
|
||||
break;
|
||||
}
|
||||
|
||||
tempData.setBit(newX, newY, pixel);
|
||||
}
|
||||
}
|
||||
|
||||
return std::move(tempData.bytes);
|
||||
}
|
||||
|
||||
void MonochromeData::visualize() const {
|
||||
std::cout << "MonochromeData visualization (" << width << "x" << height << ", orientation: ";
|
||||
|
||||
switch (orientation) {
|
||||
case Orientation::LANDSCAPE:
|
||||
std::cout << "LANDSCAPE";
|
||||
break;
|
||||
case Orientation::PORTRAIT:
|
||||
std::cout << "PORTRAIT";
|
||||
break;
|
||||
case Orientation::LANDSCAPE_FLIPPED:
|
||||
std::cout << "LANDSCAPE_FLIPPED";
|
||||
break;
|
||||
case Orientation::PORTRAIT_FLIPPED:
|
||||
std::cout << "PORTRAIT_FLIPPED";
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout << "):" << std::endl;
|
||||
|
||||
// Print the image row by row
|
||||
for (uint32_t y = 0; y < height; ++y) {
|
||||
for (uint32_t x = 0; x < width; ++x) {
|
||||
bool pixel = getBit(x, y);
|
||||
std::cout << (pixel ? "█" : ".");
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
} // namespace ptprnt::graphics
|
||||
Reference in New Issue
Block a user