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

37
src/graphics/Bitmap.hpp Normal file
View File

@@ -0,0 +1,37 @@
#pragma once
#include <stdint.h>
#include <cstdint>
#include <optional>
#include <vector>
namespace ptprnt::graphics {
typedef uint8_t ALPHA8; // Alpha only, 8 bit per pixel
typedef uint32_t RGBX8; // RGB, least significant byte unused, 8 bit per channel
typedef uint32_t RGBA8; // RGB, least significant byte alpha, 8 bit per channel
typedef uint32_t ARGB8; // RGB, most significant byte alpha, 8 bit per channel
template <class T>
class Bitmap {
public:
Bitmap(uint16_t width, uint16_t height);
~Bitmap() = default;
uint16_t getWidth();
uint16_t getHeight();
std::optional<std::vector<T>> getLine(uint16_t line);
std::optional<std::vector<T>> getRow(uint16_t row);
private:
uint16_t mWidth;
uint16_t mHeight;
std::vector<T> mPixels;
};
// force compiler to generate class for type ALPHA8 and RGBX8
template class Bitmap<ALPHA8>;
template class Bitmap<RGBX8>;
} // namespace ptprnt::graphics

33
src/graphics/Image.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include "Image.hpp"
#include "pango/pango-font.h"
namespace ptprnt::graphics {
Image::Image() {
mSurface = cairo_image_surface_create(CAIRO_FORMAT_A1, 512, 128);
cairo_t* cr = cairo_create(mSurface);
mFontDescription = pango_font_description_new();
pango_font_description_set_family(mFontDescription, "sans");
pango_font_description_set_weight(mFontDescription, PANGO_WEIGHT_SEMIBOLD);
pango_font_description_set_size(mFontDescription, 60 * PANGO_SCALE);
mLayout = pango_cairo_create_layout(cr);
pango_layout_set_font_description(mLayout, mFontDescription);
pango_layout_set_text(mLayout, "Hello, world", -1);
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_move_to(cr, 10.0, 94.0);
pango_cairo_show_layout_line(cr, pango_layout_get_line(mLayout, 0));
cairo_surface_write_to_png(mSurface, "hello.png");
}
uint8_t* Image::getRaw() {
return cairo_image_surface_get_data(mSurface);
}
Image::~Image() {
g_object_unref(mLayout);
pango_font_description_free(mFontDescription);
}
} // namespace ptprnt::graphics

17
src/graphics/Image.hpp Normal file
View File

@@ -0,0 +1,17 @@
#include <pango/pangocairo.h>
#include <cstdint>
namespace ptprnt::graphics {
class Image {
public:
Image();
~Image();
uint8_t* getRaw();
private:
PangoLayout* mLayout;
PangoFontDescription* mFontDescription;
cairo_surface_t* mSurface;
};
} // namespace ptprnt::graphics