/*
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 .
*/
#include "graphics/Monochrome.hpp"
#include
#include
#include
namespace ptprnt::graphics {
Monochrome::Monochrome(const std::vector& grayscale) : mPixels(std::move(grayscale)) {}
void Monochrome::setThreshold(uint8_t threshhold) {
mThreshhold = threshhold;
}
void Monochrome::invert(bool shouldInvert) {
mShouldInvert = shouldInvert;
}
std::vector Monochrome::get() {
std::vector outPixels(
(static_cast((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