abstracted a generic `PixelRenderer` so we can have more pixel based

output formats
development
Michael Ochmann 2 years ago
parent e10065fa13
commit c6bbf4274a
  1. 50
      src/PNGRenderer.hpp
  2. 59
      src/PixelRenderer.hpp

@ -1,59 +1,17 @@
#pragma once
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <vector>
#include <string>
#include <iostream>
#include <math.h>
#include "stb_image_write.hpp"
#include "Renderer.hpp"
#include "PixelRenderer.hpp"
namespace massivedynamic {
typedef uint32_t Color;
class PNGRenderer : public Renderer {
private:
constexpr static Color BLACK = 0xFF000000;
constexpr static Color WHITE = 0xFFFFFFFF;
std::vector<Color> bitmap;
void drawPixelScaled(size_t x, size_t y, Color color) {
size_t pixelSize = floor(static_cast<float>(this->targetSize) / static_cast<float>(this->sourceSize + 2));
if (pixelSize < 1) {
std::cerr << "ERROR: output file size is too small" << std::endl;
exit(1);
}
size_t absoluteX = pixelSize * x;
size_t absoluteY = pixelSize * y;
for (size_t localY = absoluteY + pixelSize; localY < absoluteY + pixelSize + pixelSize; localY++) {
for (size_t localX = absoluteX + pixelSize; localX < absoluteX + pixelSize + pixelSize; localX++) {
size_t absPos = localY * this->targetSize + localX;
this->bitmap.at(absPos) = color;
}
}
}
class PNGRenderer : public PixelRenderer {
public:
PNGRenderer(const std::vector<bool>& pixels, size_t sourceSize, size_t targetSize) : Renderer(pixels, sourceSize, targetSize) {
if (this->targetSize == 0)
this->targetSize = (sourceSize + 2) * 2;
this->bitmap = std::vector<Color>(this->targetSize * this->targetSize, PNGRenderer::WHITE);
}
PNGRenderer(const std::vector<bool>& pixels, size_t sourceSize, size_t targetSize) : PixelRenderer(pixels, sourceSize, targetSize) {}
virtual void render(const std::string& filename) override {
for (size_t y = 0; y < sourceSize; y++) {
for (size_t x = 0; x < sourceSize; x++) {
if (!pixels.at(y * sourceSize + x))
continue;
this->drawPixelScaled(x, y, PNGRenderer::BLACK);
}
}
this->generateBuffer();
stbi_write_png(filename.c_str(), targetSize, targetSize, 4, this->bitmap.data(), sizeof(Color) * targetSize);
}

@ -0,0 +1,59 @@
#pragma once
#include <math.h>
#include <iostream>
#include "Renderer.hpp"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.hpp"
namespace massivedynamic {
typedef uint32_t Color;
class PixelRenderer : public Renderer {
protected:
std::vector<Color> bitmap;
void drawPixelScaled(size_t x, size_t y, Color color) {
size_t pixelSize = floor(static_cast<float>(this->targetSize) / static_cast<float>(this->sourceSize + 2));
if (pixelSize < 1) {
std::cerr << "ERROR: output file size is too small" << std::endl;
exit(1);
}
size_t absoluteX = pixelSize * x;
size_t absoluteY = pixelSize * y;
for (size_t localY = absoluteY + pixelSize; localY < absoluteY + pixelSize + pixelSize; localY++) {
for (size_t localX = absoluteX + pixelSize; localX < absoluteX + pixelSize + pixelSize; localX++) {
size_t absPos = localY * this->targetSize + localX;
this->bitmap.at(absPos) = color;
}
}
}
public:
enum Colors : Color {
BLACK = 0xFF000000,
WHITE = 0xFFFFFFFF
};
PixelRenderer(const std::vector<bool>& pixels, size_t sourceSize, size_t targetSize) : Renderer(pixels, sourceSize, targetSize) {
if (this->targetSize == 0)
this->targetSize = (sourceSize + 2) * 2;
this->bitmap = std::vector<Color>(this->targetSize * this->targetSize, Colors::WHITE);
}
void generateBuffer() {
for (size_t y = 0; y < sourceSize; y++) {
for (size_t x = 0; x < sourceSize; x++) {
if (!pixels.at(y * sourceSize + x))
continue;
this->drawPixelScaled(x, y, Colors::BLACK);
}
}
}
};
}
Loading…
Cancel
Save