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

93 lines
2.5 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 "Bitmap.hpp"
#include <algorithm>
#include <spdlog/spdlog.h>
#include <iostream>
#include <iterator>
#include <optional>
#include <vector>
namespace ptprnt::graphics {
template <class T>
Bitmap<T>::Bitmap(uint16_t width, uint16_t height)
: mWidth{width}, mHeight{height}, mPixels(width * height) {}
template <class T>
[[nodiscard]] uint16_t Bitmap<T>::getWidth() const {
return mWidth;
}
template <class T>
[[nodiscard]] uint16_t Bitmap<T>::getHeight() const {
return mHeight;
}
template <class T>
bool Bitmap<T>::setPixels(const std::vector<T>& pixels) {
if (pixels.size() != mPixels.size()) {
spdlog::error("Invalid pixel buffer size (got {} vs. {} bitmap size).", pixels.size(),
mPixels.size());
return false;
}
mPixels = pixels;
return true;
}
template <class T>
[[nodiscard]] std::vector<T> Bitmap<T>::getPixelsCpy() const {
return mPixels;
}
template <class T>
[[nodiscard]] std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) const {
if (line >= mHeight) {
// out of bound
return std::nullopt;
}
auto lineStart = mPixels.begin() + (line * mWidth);
auto lineEnd = mPixels.begin() + ((line + 1) * mWidth);
return std::vector<T>(lineStart, lineEnd);
}
template <class T>
[[nodiscard]] std::optional<std::vector<T>> Bitmap<T>::getCol(uint16_t col) const {
if (col >= mWidth) {
// out of bound
return std::nullopt;
}
// first pixel is always beginning of the col
std::vector<T> colPixels(mHeight);
auto it = std::next(mPixels.begin(), col);
for (auto& colElement : colPixels) {
colElement = *it;
std::advance(it, mWidth);
}
return colPixels;
}
} // namespace ptprnt::graphics