Files
ptouch-prnt/src/graphics/Monochrome.cpp

59 lines
1.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 <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