60 lines
1.9 KiB
C++
60 lines
1.9 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 <iostream>
|
|
#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 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));
|
|
}
|
|
}
|
|
if (mShouldInvert) {
|
|
outPixels[outIndex] = ~outPixels[outIndex];
|
|
}
|
|
outIndex++;
|
|
}
|
|
return outPixels;
|
|
}
|
|
} // namespace ptprnt::graphics
|