Compare commits

..

4 Commits

  1. 2
      CMakeLists.txt
  2. 6
      main.cpp
  3. 1
      src/QR.cpp
  4. 16
      src/QR.hpp
  5. 9
      src/Renderer.hpp
  6. 9
      src/renderers/ConsoleRenderer.hpp
  7. 19
      src/renderers/PixelRenderer.hpp
  8. 2
      src/renderers/SVGRenderer.hpp
  9. 12
      src/util.hpp

@ -1,7 +1,5 @@
cmake_minimum_required(VERSION 3.14...3.25) cmake_minimum_required(VERSION 3.14...3.25)
set (CXX_STANDARD 23)
project( project(
qr qr
VERSION 1.1.0 VERSION 1.1.0

@ -3,10 +3,10 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "qrcodegen.hpp" #include <qrcodegen.hpp>
#include "QR.hpp"
#define UNUSED(var) (void) var; #include "util.hpp"
#include "QR.hpp"
// Version is injected through cmake // Version is injected through cmake
constexpr const char* VERSION = "@qr_VERSION@"; constexpr const char* VERSION = "@qr_VERSION@";

@ -14,6 +14,7 @@ namespace massivedynamic {
QR::QR(const std::string& data, std::string outputFile, size_t size, qrcodegen::QrCode::Ecc type) : outputFile(std::move(outputFile)), QR::QR(const std::string& data, std::string outputFile, size_t size, qrcodegen::QrCode::Ecc type) : outputFile(std::move(outputFile)),
qr(qrcodegen::QrCode::encodeText(data.c_str(), type)), size(size) { qr(qrcodegen::QrCode::encodeText(data.c_str(), type)), size(size) {
this->pixels.reserve(this->qr.getSize() * this->qr.getSize());
// this is inherently stupid, but "qrcodegen::QrCode" does not give access to the // this is inherently stupid, but "qrcodegen::QrCode" does not give access to the
// `segments` vector member and the class itself is marked final. // `segments` vector member and the class itself is marked final.
for (int y = 0; y < qr.getSize(); y++) { for (int y = 0; y < qr.getSize(); y++) {

@ -2,15 +2,12 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <qrcodegen.hpp>
#include "qrcodegen.hpp" #include "util.hpp"
#include "Renderer.hpp" #include "Renderer.hpp"
#define UNUSED_CASE(name) case name: break;
namespace massivedynamic { namespace massivedynamic {
typedef uint32_t Color;
enum class Format { enum class Format {
CONSOLE, CONSOLE,
SVG, SVG,
@ -29,9 +26,10 @@ namespace massivedynamic {
size_t size; size_t size;
public: public:
QR(const std::string& data, std::string outputFile, size_t size, qrcodegen::QrCode::Ecc type); QR(const std::string& data, std::string outputFile, size_t size, qrcodegen::QrCode::Ecc type);
QR(const QR&&) = delete; QR(const QR&&) = delete;
QR(QR&) = delete; QR(QR&) = delete;
~QR() = default; ~QR() = default;
QR& operator=(const QR&) = delete;
void render(Format format); void render(Format format);
@ -42,4 +40,4 @@ namespace massivedynamic {
} }
}; };
} }

@ -1,6 +1,8 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include <string>
#include <cstddef>
namespace massivedynamic { namespace massivedynamic {
@ -11,9 +13,10 @@ class Renderer {
size_t targetSize; size_t targetSize;
public: public:
Renderer(const std::vector<bool>& pixels, size_t sourceSize, size_t targetSize) : pixels(pixels), sourceSize(sourceSize), targetSize(targetSize) {} Renderer(const std::vector<bool>& pixels, size_t sourceSize, size_t targetSize) : pixels(pixels), sourceSize(sourceSize), targetSize(targetSize) {}
Renderer(const Renderer&&) = delete; Renderer(const Renderer&&) = delete;
Renderer(Renderer&) = delete; Renderer(Renderer&) = delete;
virtual ~Renderer() = default; virtual ~Renderer() = default;
Renderer& operator=(const Renderer&) = delete;
virtual void render(const std::string& filename) = 0; virtual void render(const std::string& filename) = 0;
}; };

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <iomanip>
#include "Renderer.hpp" #include "Renderer.hpp"
namespace massivedynamic { namespace massivedynamic {
@ -10,17 +12,16 @@ class ConsoleRenderer : public Renderer {
virtual void render(const std::string&) override { virtual void render(const std::string&) override {
int border = 1; int border = 1;
std::cout << std::string(border, '\n'); std::cout << std::setfill('\n') << std::setw(border) << "";
for (int y = 0; y < sourceSize; y++) { for (int y = 0; y < sourceSize; y++) {
std::cout << std::string(border * 2, ' '); std::cout << std::string(border * 2, ' ');
for (int x = 0; x < sourceSize; x++) { for (int x = 0; x < sourceSize; x++) {
bool isFilled = pixels.at(y * sourceSize + x); bool isFilled = pixels.at(y * sourceSize + x);
std::cout << (isFilled ? "██" : " "); std::cout << (isFilled ? "██" : " ");
} }
std::cout << std::string(border * 2, ' '); std::cout << std::setfill(' ') << std::setw(border * 2) << '\n';
std::cout << '\n';
} }
std::cout << std::string(border, '\n'); std::cout << std::setfill('\n') << std::setw(border) << "";
std::cout.flush(); std::cout.flush();
} }
}; };

@ -4,12 +4,13 @@
#include <iostream> #include <iostream>
#include <cassert> #include <cassert>
#include "Renderer.hpp"
#define STB_IMAGE_WRITE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.hpp" #include <stb_image_write.hpp>
#include "util.hpp"
#include "Renderer.hpp"
namespace massivedynamic { namespace massivedynamic {
typedef uint32_t Color;
class PixelRenderer : public Renderer { class PixelRenderer : public Renderer {
protected: protected:
@ -37,13 +38,11 @@ namespace massivedynamic {
WHITE = 0xFFFFFFFF WHITE = 0xFFFFFFFF
}; };
PixelRenderer(const std::vector<bool>& pixels, size_t sourceSize, size_t targetSize) : Renderer(pixels, sourceSize, targetSize), pixelSize(0) { PixelRenderer(const std::vector<bool>& pixels, size_t sourceSize, size_t targetSize) :
this->targetSize = this->targetSize == 0 ? (sourceSize + 2) * 2 : this->targetSize; Renderer(pixels, sourceSize, targetSize == 0 ? (sourceSize + 2) * 2 : targetSize),
this->pixelSize = round(static_cast<float>(this->targetSize) / static_cast<float>(this->sourceSize + 2)); pixelSize(round(static_cast<float>(this->targetSize) / static_cast<float>(this->sourceSize + 2))),
this->border = round(static_cast<float>(this->targetSize - this->pixelSize * this->sourceSize) / 2.0f); border(round(static_cast<float>(this->targetSize - this->pixelSize * this->sourceSize) / 2.0f)),
bitmap(std::vector<Color>(this->targetSize * this->targetSize, Colors::WHITE)) {}
this->bitmap = std::vector<Color>(this->targetSize * this->targetSize, Colors::WHITE);
}
virtual void generateBuffer() { virtual void generateBuffer() {
for (size_t y = 0; y < sourceSize; y++) { for (size_t y = 0; y < sourceSize; y++) {

@ -40,7 +40,7 @@ namespace massivedynamic {
} }
} }
file << "</svg>" << std::endl; file << "</svg>\n";
file.close(); file.close();
} }

@ -0,0 +1,12 @@
#pragma once
#include <stdint.h>
#define UNUSED(var) (void) var;
#define UNUSED_CASE(name) case name: break;
namespace massivedynamic {
using Color = uint32_t;
}
Loading…
Cancel
Save