fixed illegal memory accesses

development
Michael Ochmann 2 years ago
parent 94b17c6fd8
commit 12b19fab33
  1. 14
      src/PNGRenderer.hpp

@ -5,6 +5,7 @@
#include <vector>
#include <string>
#include <iostream>
#include <math.h>
#include "stb_image_write.hpp"
#include "Renderer.hpp"
@ -21,7 +22,7 @@ class PNGRenderer : public Renderer {
std::vector<Color> bitmap;
void drawPixelScaled(size_t x, size_t y, Color color) {
size_t pixelSize = this->targetSize / (this->sourceSize + 2);
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;
@ -41,19 +42,20 @@ class PNGRenderer : public Renderer {
public:
PNGRenderer(const std::vector<bool>& pixels, size_t sourceSize, size_t targetSize) : Renderer(pixels, sourceSize, targetSize) {
if (this->targetSize == 0)
this->targetSize = sourceSize;
this->bitmap = std::vector<Color>(targetSize * targetSize, 0xFFFFFFFF);
this->targetSize = (sourceSize + 2) * 2;
this->bitmap = std::vector<Color>(this->targetSize * this->targetSize, PNGRenderer::WHITE);
}
virtual void render(const std::string& filename) override {
for (size_t y = 0; y < sourceSize; y++) {
for (size_t x = 0; x < sourceSize; x++) {
Color color = pixels.at(y * sourceSize + x) ? PNGRenderer::BLACK : PNGRenderer::WHITE;
this->drawPixelScaled(x, y, color);
if (!pixels.at(y * sourceSize + x))
continue;
this->drawPixelScaled(x, y, PNGRenderer::BLACK);
}
}
stbi_write_png(filename.c_str(), targetSize, targetSize, 4, this->bitmap.data(), targetSize * sizeof(Color));
stbi_write_png(filename.c_str(), targetSize, targetSize, 4, this->bitmap.data(), sizeof(Color) * targetSize);
}
};

Loading…
Cancel
Save