Moved graphics classes into own namespace, added unit testing with gtest

This commit is contained in:
2023-09-23 13:29:54 +02:00
parent 7ab817793d
commit c0811df4bb
18 changed files with 334 additions and 136 deletions

55
src/graphics/Bitmap.cpp Normal file
View File

@@ -0,0 +1,55 @@
#include "Bitmap.hpp"
#include <algorithm>
#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>
uint16_t Bitmap<T>::getWidth() {
return mWidth;
}
template <class T>
uint16_t Bitmap<T>::getHeight() {
return mHeight;
}
template <class T>
std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) {
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>
std::optional<std::vector<T>> Bitmap<T>::getRow(uint16_t row) {
if (row >= mWidth) {
// out of bound
return std::nullopt;
}
// first pixel is always beginning of the row
std::vector<T> rowPixels(mHeight);
auto it = mPixels.begin() + row;
for (auto& rowElement : rowPixels) {
rowElement = *it;
it += mWidth;
}
return rowPixels;
}
} // namespace ptprnt::graphics