40 lines
1.1 KiB
C++
40 lines
1.1 KiB
C++
#include "graphics/Monochrome.hpp"
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdint>
|
|
#include <vector>
|
|
|
|
namespace ptprnt::graphics {
|
|
Monochrome::Monochrome(const std::vector<uint8_t>& grayscale) : mPixels(std::move(grayscale)) {}
|
|
|
|
void Monochrome::setThreshold(uint8_t threshhold) {
|
|
mThreshhold = threshhold;
|
|
}
|
|
|
|
void Monochrome::invert(bool shouldInvert) {
|
|
mShouldInvert = 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;
|
|
|
|
for (unsigned int byteNum = 0; byteNum < mPixels.size(); byteNum += 8) {
|
|
for (unsigned int bitNo = 0; bitNo < 8; bitNo++) {
|
|
if (mPixels[byteNum + bitNo] > mThreshhold) {
|
|
outPixels[outIndex] |= (1 << (7 - bitNo));
|
|
} else {
|
|
outPixels[outIndex] &= ~(1 << (7 - bitNo));
|
|
}
|
|
}
|
|
if (mShouldInvert) {
|
|
outPixels[outIndex] = ~outPixels[outIndex];
|
|
}
|
|
outIndex++;
|
|
}
|
|
return outPixels;
|
|
}
|
|
} // namespace ptprnt::graphics
|