Moved graphics classes into own namespace, added unit testing with gtest
This commit is contained in:
55
src/graphics/Bitmap.cpp
Normal file
55
src/graphics/Bitmap.cpp
Normal 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
|
Reference in New Issue
Block a user