From 9c1a25aba45de5e1ca3fb9ec1cd98b6f63fbfff0 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Thu, 31 Aug 2017 20:39:29 +0000 Subject: [PATCH] Changed QrCode.getModule() in {Java, JavaScript, Python, C++} language versions to return Boolean instead of 0/1 - to match {C, Rust} language versions - and updated comments and usages. --- cpp/QrCode.cpp | 9 +++------ cpp/QrCode.hpp | 6 +++--- cpp/QrCodeGeneratorDemo.cpp | 2 +- cpp/QrCodeGeneratorWorker.cpp | 2 +- java/io/nayuki/qrcodegen/QrCode.java | 19 ++++++++----------- .../qrcodegen/QrCodeGeneratorWorker.java | 2 +- javascript/qrcodegen.js | 17 +++++++---------- python/qrcodegen-demo.py | 2 +- python/qrcodegen-worker.py | 2 +- python/qrcodegen.py | 10 +++++----- python/setup.py | 2 +- 11 files changed, 32 insertions(+), 41 deletions(-) diff --git a/cpp/QrCode.cpp b/cpp/QrCode.cpp index 3636d0c..be47050 100644 --- a/cpp/QrCode.cpp +++ b/cpp/QrCode.cpp @@ -159,11 +159,8 @@ int QrCode::getMask() const { } -int QrCode::getModule(int x, int y) const { - if (0 <= x && x < size && 0 <= y && y < size) - return module(x, y) ? 1 : 0; - else - return 0; // Infinite white border +bool QrCode::getModule(int x, int y) const { + return 0 <= x && x < size && 0 <= y && y < size && module(x, y); } @@ -180,7 +177,7 @@ std::string QrCode::toSvgString(int border) const { bool head = true; for (int y = -border; y < size + border; y++) { for (int x = -border; x < size + border; x++) { - if (getModule(x, y) == 1) { + if (getModule(x, y)) { if (head) head = false; else diff --git a/cpp/QrCode.hpp b/cpp/QrCode.hpp index c5bbaf7..20f4129 100644 --- a/cpp/QrCode.hpp +++ b/cpp/QrCode.hpp @@ -142,10 +142,10 @@ class QrCode final { /* * Returns the color of the module (pixel) at the given coordinates, which is either - * 0 for white or 1 for black. The top left corner has the coordinates (x=0, y=0). - * If the given coordinates are out of bounds, then 0 (white) is returned. + * false for white or true for black. The top left corner has the coordinates (x=0, y=0). + * If the given coordinates are out of bounds, then false (white) is returned. */ - public: int getModule(int x, int y) const; + public: bool getModule(int x, int y) const; /* diff --git a/cpp/QrCodeGeneratorDemo.cpp b/cpp/QrCodeGeneratorDemo.cpp index befecb0..523fee9 100644 --- a/cpp/QrCodeGeneratorDemo.cpp +++ b/cpp/QrCodeGeneratorDemo.cpp @@ -168,7 +168,7 @@ static void printQr(const QrCode &qr) { int border = 4; for (int y = -border; y < qr.size + border; y++) { for (int x = -border; x < qr.size + border; x++) { - std::cout << (qr.getModule(x, y) == 1 ? "##" : " "); + std::cout << (qr.getModule(x, y) ? "##" : " "); } std::cout << std::endl; } diff --git a/cpp/QrCodeGeneratorWorker.cpp b/cpp/QrCodeGeneratorWorker.cpp index 2033feb..b99ebd2 100644 --- a/cpp/QrCodeGeneratorWorker.cpp +++ b/cpp/QrCodeGeneratorWorker.cpp @@ -88,7 +88,7 @@ int main() { std::cout << qr.version << std::endl; for (int y = 0; y < qr.size; y++) { for (int x = 0; x < qr.size; x++) - std::cout << qr.getModule(x, y) << std::endl; + std::cout << (qr.getModule(x, y) ? 1 : 0) << std::endl; } } catch (const char *msg) { diff --git a/java/io/nayuki/qrcodegen/QrCode.java b/java/io/nayuki/qrcodegen/QrCode.java index 361ba3a..76bfa81 100644 --- a/java/io/nayuki/qrcodegen/QrCode.java +++ b/java/io/nayuki/qrcodegen/QrCode.java @@ -259,17 +259,14 @@ public final class QrCode { /** * Returns the color of the module (pixel) at the specified coordinates, which is either - * 0 for white or 1 for black. The top left corner has the coordinates (x=0, y=0). - * If the specified coordinates are out of bounds, then 0 (white) is returned. + * false for white or true for black. The top left corner has the coordinates (x=0, y=0). + * If the specified coordinates are out of bounds, then false (white) is returned. * @param x the x coordinate, where 0 is the left edge and size−1 is the right edge * @param y the y coordinate, where 0 is the top edge and size−1 is the bottom edge - * @return the module's color, which is either 0 (white) or 1 (black) + * @return the module's color, which is either false (white) or true (black) */ - public int getModule(int x, int y) { - if (0 <= x && x < size && 0 <= y && y < size) - return modules[y][x] ? 1 : 0; - else - return 0; // Infinite white border + public boolean getModule(int x, int y) { + return 0 <= x && x < size && 0 <= y && y < size && modules[y][x]; } @@ -289,8 +286,8 @@ public final class QrCode { BufferedImage result = new BufferedImage((size + border * 2) * scale, (size + border * 2) * scale, BufferedImage.TYPE_INT_RGB); for (int y = 0; y < result.getHeight(); y++) { for (int x = 0; x < result.getWidth(); x++) { - int val = getModule(x / scale - border, y / scale - border); // 0 or 1 - result.setRGB(x, y, val == 1 ? 0x000000 : 0xFFFFFF); + boolean val = getModule(x / scale - border, y / scale - border); + result.setRGB(x, y, val ? 0x000000 : 0xFFFFFF); } } return result; @@ -318,7 +315,7 @@ public final class QrCode { boolean head = true; for (int y = -border; y < size + border; y++) { for (int x = -border; x < size + border; x++) { - if (getModule(x, y) == 1) { + if (getModule(x, y)) { if (head) head = false; else diff --git a/java/io/nayuki/qrcodegen/QrCodeGeneratorWorker.java b/java/io/nayuki/qrcodegen/QrCodeGeneratorWorker.java index ba61335..30a105d 100644 --- a/java/io/nayuki/qrcodegen/QrCodeGeneratorWorker.java +++ b/java/io/nayuki/qrcodegen/QrCodeGeneratorWorker.java @@ -84,7 +84,7 @@ public final class QrCodeGeneratorWorker { System.out.println(qr.version); for (int y = 0; y < qr.size; y++) { for (int x = 0; x < qr.size; x++) - System.out.println(qr.getModule(x, y)); + System.out.println(qr.getModule(x, y) ? 1 : 0); } } catch (IllegalArgumentException e) { diff --git a/javascript/qrcodegen.js b/javascript/qrcodegen.js index a035436..3115097 100644 --- a/javascript/qrcodegen.js +++ b/javascript/qrcodegen.js @@ -35,7 +35,7 @@ * - Constructor QrCode(list datacodewords, int mask, int version, QrCode.Ecc ecl) * - Fields int version, size, mask * - Field QrCode.Ecc errorCorrectionLevel - * - Method getModule(int x, int y) -> int + * - Method getModule(int x, int y) -> bool * - Method drawCanvas(int scale, int border, HTMLCanvasElement canvas) -> void * - Method toSvgString(int border) -> str * - Enum Ecc: @@ -115,7 +115,7 @@ var qrcodegen = new function() { } else if (initData instanceof qrcodegen.QrCode) { for (var y = 0; y < size; y++) { for (var x = 0; x < size; x++) { - modules[y][x] = initData.getModule(x, y) == 1; + modules[y][x] = initData.getModule(x, y); isFunction[y][x] = initData.isFunctionModule(x, y); } } @@ -164,13 +164,10 @@ var qrcodegen = new function() { /*---- Accessor methods ----*/ // (Public) Returns the color of the module (pixel) at the given coordinates, which is either - // 0 for white or 1 for black. The top left corner has the coordinates (x=0, y=0). - // If the given coordinates are out of bounds, then 0 (white) is returned. + // false for white or true for black. The top left corner has the coordinates (x=0, y=0). + // If the given coordinates are out of bounds, then false (white) is returned. this.getModule = function(x, y) { - if (0 <= x && x < size && 0 <= y && y < size) - return modules[y][x] ? 1 : 0; - else - return 0; // Infinite white border + return 0 <= x && x < size && 0 <= y && y < size && modules[y][x]; }; // (Package-private) Tests whether the module at the given coordinates is a function module (true) or not (false). @@ -198,7 +195,7 @@ var qrcodegen = new function() { var ctx = canvas.getContext("2d"); for (var y = -border; y < size + border; y++) { for (var x = -border; x < size + border; x++) { - ctx.fillStyle = this.getModule(x, y) == 1 ? "#000000" : "#FFFFFF"; + ctx.fillStyle = this.getModule(x, y) ? "#000000" : "#FFFFFF"; ctx.fillRect((x + border) * scale, (y + border) * scale, scale, scale); } } @@ -219,7 +216,7 @@ var qrcodegen = new function() { var head = true; for (var y = -border; y < size + border; y++) { for (var x = -border; x < size + border; x++) { - if (this.getModule(x, y) == 1) { + if (this.getModule(x, y)) { if (head) head = false; else diff --git a/python/qrcodegen-demo.py b/python/qrcodegen-demo.py index 86fc688..035820d 100644 --- a/python/qrcodegen-demo.py +++ b/python/qrcodegen-demo.py @@ -161,7 +161,7 @@ def print_qr(qrcode): border = 4 for y in range(-border, qrcode.get_size() + border): for x in range(-border, qrcode.get_size() + border): - print(u"\u2588 "[qrcode.get_module(x,y)] * 2, end="") + print(u"\u2588 "[1 if qrcode.get_module(x,y) else 0] * 2, end="") print() print() diff --git a/python/qrcodegen-worker.py b/python/qrcodegen-worker.py index 5262e1e..c52548d 100644 --- a/python/qrcodegen-worker.py +++ b/python/qrcodegen-worker.py @@ -66,7 +66,7 @@ def main(): print(qr.get_version()) for y in range(qr.get_size()): for x in range(qr.get_size()): - print(qr.get_module(x, y)) + print(1 if qr.get_module(x, y) else 0) except ValueError as e: if e.args[0] != "Data too long": diff --git a/python/qrcodegen.py b/python/qrcodegen.py index fb59542..b175ce8 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -37,7 +37,7 @@ This module "qrcodegen", public members: - Method get_size() -> int - Method get_error_correction_level() -> QrCode.Ecc - Method get_mask() -> int - - Method get_module(int x, int y) -> int + - Method get_module(int x, int y) -> bool - Method to_svg_str(int border) -> str - Enum Ecc: - Constants LOW, MEDIUM, QUARTILE, HIGH @@ -226,9 +226,9 @@ class QrCode(object): def get_module(self, x, y): """Returns the color of the module (pixel) at the given coordinates, which is either - 0 for white or 1 for black. The top left corner has the coordinates (x=0, y=0). - If the given coordinates are out of bounds, then 0 (white) is returned.""" - return 1 if ((0 <= x < self._size) and (0 <= y < self._size) and self._modules[y][x]) else 0 + False for white or True for black. The top left corner has the coordinates (x=0, y=0). + If the given coordinates are out of bounds, then False (white) is returned.""" + return (0 <= x < self._size) and (0 <= y < self._size) and self._modules[y][x] # ---- Public instance methods ---- @@ -241,7 +241,7 @@ class QrCode(object): parts = [] for y in range(-border, self._size + border): for x in range(-border, self._size + border): - if self.get_module(x, y) == 1: + if self.get_module(x, y): parts.append("M{},{}h1v1h-1z".format(x + border, y + border)) return """ diff --git a/python/setup.py b/python/setup.py index 75f6cc6..52f5a56 100644 --- a/python/setup.py +++ b/python/setup.py @@ -100,7 +100,7 @@ Examples: border = 4 for y in range(-border, qr1.get_size() + border): for x in range(-border, qr1.get_size() + border): - color = qr1.get_module(x, y) # 0 for white, 1 for black + color = qr1.get_module(x, y) # False for white, True for black # (... paint the module onto pixels ...) More complete set of examples: https://github.com/nayuki/QR-Code-generator/blob/master/python/qrcodegen-demo.py .