From 6f3827427b64241e4ab44cce4081aa740f730228 Mon Sep 17 00:00:00 2001 From: Michael Ochmann Date: Sun, 26 Feb 2023 12:52:51 +0100 Subject: [PATCH] PixelRenderer: now respecting the specified output size exactly output is now padded by the border to prevent changing the output size specified by the user. this may result in the QR code not being exactly in the center. it may be off by 1 pixel. --- main.cpp | 5 +++-- src/renderers/PixelRenderer.hpp | 11 +++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index f2883ae..7d8fb89 100644 --- a/main.cpp +++ b/main.cpp @@ -20,8 +20,9 @@ Usage: qr [OPTION]... --input "data to encapsulate" *Caveats* With no `--input` parameter defined, read STDIN - In pixel based renderers (i.e. PNG), the output size may not be exactly as - specified but be the nearest multiple of the actual cell size for the QR code. + In pixel based renderers (i.e. PNG), the output code may not be exactly + centered, as the amount of segment may not correspond to the specified + output size. Options: -f --format output file format. can be one of "cli, png, svg, jpg, bmp" diff --git a/src/renderers/PixelRenderer.hpp b/src/renderers/PixelRenderer.hpp index ec71b6a..8ffd810 100644 --- a/src/renderers/PixelRenderer.hpp +++ b/src/renderers/PixelRenderer.hpp @@ -13,6 +13,7 @@ namespace massivedynamic { class PixelRenderer : public Renderer { protected: size_t pixelSize; + size_t border; std::vector bitmap; void drawPixelScaled(size_t x, size_t y, Color color) { @@ -23,8 +24,8 @@ namespace massivedynamic { exit(1); } - size_t absoluteX = pixelSize * x + pixelSize; - size_t absoluteY = pixelSize * y + pixelSize; + size_t absoluteX = pixelSize * x + this->border; + size_t absoluteY = pixelSize * y + this->border; for (size_t localY = absoluteY; localY < absoluteY + pixelSize; localY++) { for (size_t localX = absoluteX; localX < absoluteX + pixelSize; localX++) { @@ -40,10 +41,8 @@ namespace massivedynamic { PixelRenderer(const std::vector& pixels, size_t sourceSize, size_t targetSize) : Renderer(pixels, sourceSize, targetSize), pixelSize(0) { this->targetSize = this->targetSize == 0 ? (sourceSize + 2) * 2 : this->targetSize; - - // here we make shure, `targetSize` will be a multiple of `sourceSize` - this->pixelSize = round(static_cast(this->targetSize) / static_cast(this->sourceSize + 2)); - this->targetSize = (this->sourceSize + 2) * this->pixelSize; + this->pixelSize = round(static_cast(this->targetSize) / static_cast(this->sourceSize + 2)); + this->border = round((this->targetSize - this->pixelSize * this->sourceSize) / 2); this->bitmap = std::vector(this->targetSize * this->targetSize, Colors::WHITE); }