NDEVR
API Documentation
QRCode.h
1/*
2 * QR Code generator library (C++)
3 *
4 * Copyright (c) Project Nayuki. (MIT License)
5 * https://www.nayuki.io/page/qr-code-generator-library
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 * - The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 * - The Software is provided "as is", without warranty of any kind, express or
16 * implied, including but not limited to the warranties of merchantability,
17 * fitness for a particular purpose and noninfringement. In no event shall the
18 * authors or copyright holders be liable for any claim, damages or other
19 * liability, whether in an action of contract, tort or otherwise, arising from,
20 * out of or in connection with the Software or the use or other dealings in the
21 * Software.
22 */
23
24#pragma once
25#include "DLLInfo.h"
26#include <NDEVR/Buffer.h>
27#include <NDEVR/String.h>
28#include <NDEVR/RGBColor.h>
29namespace NDEVR {
30
31 class QrSegment;
32
34 typedef AlocatingAlignedBuffer<QrSegment, sizeof(void*) == 4 ? 16 : 32> QrSegmentBuffer;
35
48 class QrSegment final
49 {
50
51 /*---- Public helper enumeration ----*/
52
53 public:
60 class Mode final
61 {
62
63 /*-- Constants --*/
64
65 public:
66 static const Mode NUMERIC;
67 static const Mode ALPHANUMERIC;
68 static const Mode BYTE;
69 static const Mode KANJI;
70 static const Mode ECI;
71
72
73 /*-- Fields --*/
74
75 private:
76 int modeBits;
77
78 int numBitsCharCount[3];
79
80
81 /*-- Constructor --*/
82
90 Mode(int mode, int cc0, int cc1, int cc2);
91 public:
96 int getModeBits() const;
97
104 int numCharCountBits(int ver) const;
105
106 };
107 public:
117 static QrSegment makeBytes(const uint01* data, uint04 size);
118
119
125 static QrSegment makeNumeric(const char* digits);
126
127
136 static QrSegment makeAlphanumeric(const char* text);
137
138
147 static QrSegmentBuffer makeSegments(const char* text);
148
149
156 static QrSegment makeEci(long assignVal);
157
158
159 /*---- Public static helper functions ----*/
160
168 static bool isNumeric(const char* text);
169
170
179 static bool isAlphanumeric(const char* text);
180
181
182
183 /*---- Instance fields ----*/
184
185 private:
186 const Mode* mode;
187
188 int numChars;
189
190 Buffer<bool> data;
191
192
193 public:
194 /*---- Constructors (low level) ----*/
195
205 QrSegment(const Mode& md, int numCh, const Buffer<bool>& dt);
206
207
217 public: QrSegment(const Mode& md, int numCh, Buffer<bool>&& dt);
218
219
220 /*---- Methods ----*/
221
226 public: const Mode& getMode() const;
227
228
233 public: int getNumChars() const;
234
235
240 public: const Buffer<bool>& getData() const;
241
242
253 public: static int getTotalBits(const QrSegmentBuffer& segs, int version);
254
255
256 /*---- Private constant ----*/
257
258 private:
259 static const char* ALPHANUMERIC_CHARSET;
260
261 };
262
263
264
281 class NDEVR_DESIGN_API QrCode final {
282
283 /*---- Public helper enumeration ----*/
284
288 public: enum class Ecc
289 {
290 LOW = 0,
291 MEDIUM,
292 QUARTILE,
293 HIGH,
294 };
295
296
302 private: static int getFormatBits(Ecc ecl);
303
304
305
306 /*---- Static factory functions (high level) ----*/
307
320 public: static QrCode encodeText(const char* text, Ecc ecl);
321
331 static QrCode encodeLink(const char* text, Ecc ecl);
332
345 public: static QrCode encodeBinary(const uint01* data, uint04 size, Ecc ecl);
346
347
348 /*---- Static factory functions (mid level) ----*/
349
368 public: static QrCode encodeSegments(const QrSegmentBuffer& segs, Ecc ecl,
369 int minVersion = 1, int maxVersion = 40, int mask = -1, bool boostEcl = true); // All optional parameters
370
378 String toSVG(uint04 border, RGBColor color, RGBColor background = Constant<RGBColor>::Invalid) const;
379
380
381 /*---- Instance fields ----*/
382
383 private:
384 int version;
385
386 uint04 m_size;
387
388 Ecc errorCorrectionLevel;
389
390 int mask;
391
392 Buffer<Buffer<bool>> modules;
393
394 Buffer<Buffer<bool>> isFunction;
395
396
397
398 /*---- Constructor (low level) ----*/
399
411 public: QrCode(int ver, Ecc ecl, const Buffer<std::uint8_t>& dataCodewords, int msk);
412
413
414
415 /*---- Public instance methods ----*/
416
421 public: int getVersion() const;
422
423
428 public: int getSize() const;
429
430
436
437
442 public: int getMask() const;
443
444
455 public: bool getModule(int x, int y) const;
456
457
458
459 /*---- Private helper methods for constructor: Drawing function modules ----*/
460
464 private: void drawFunctionPatterns();
465
466
472 private: void drawFormatBits(int msk);
473
474
479 void drawVersion();
480
481
488 void drawFinderPattern(int x, int y);
489
490
497 void drawAlignmentPattern(int x, int y);
498
499
508 void setFunctionModule(uint04 x, uint04 y, bool isDark);
509
510
517 bool module(uint04 x, uint04 y) const;
518
519
520 /*---- Private helper methods for constructor: Codewords and masking ----*/
521
528 Buffer<std::uint8_t> addEccAndInterleave(const Buffer<std::uint8_t>& data) const;
529
530
538 void drawCodewords(const Buffer<std::uint8_t>& data);
539
540
550 void applyMask(int msk);
551
552
561 long getPenaltyScore() const;
562
563
564
565 /*---- Private helper functions ----*/
566
573 Buffer<int> getAlignmentPatternPositions() const;
574
575
585 static int getNumRawDataModules(int ver);
586
587
596 static int getNumDataCodewords(int ver, Ecc ecl);
597
603 static Buffer<std::uint8_t> reedSolomonComputeDivisor(int degree);
604
612 static Buffer<std::uint8_t> reedSolomonComputeRemainder(const Buffer<std::uint8_t>& data, const Buffer<std::uint8_t>& divisor);
613
622 static std::uint8_t reedSolomonMultiply(std::uint8_t x, std::uint8_t y);
623
632 uint04 finderPenaltyCountPatterns(const std::array<int, 7>& runHistory) const;
633
644 uint04 finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, std::array<int, 7>& runHistory) const;
645
653 void finderPenaltyAddHistory(int currentRunLength, std::array<int, 7>& runHistory) const;
654
661 static bool getBit(long x, int i);
662
663 public:
664 static constexpr int MIN_VERSION = 1;
665 static constexpr int MAX_VERSION = 40;
666
667
668 private:
669 static const int PENALTY_N1;
670 static const int PENALTY_N2;
671 static const int PENALTY_N3;
672 static const int PENALTY_N4;
673 static const sint01 ECC_CODEWORDS_PER_BLOCK[4][41];
674 static const sint01 NUM_ERROR_CORRECTION_BLOCKS[4][41];
675
676 };
677
678
679
680 /*---- Public exception class ----*/
681
694 class data_too_long : public std::length_error
695 {
696 public:
701 explicit data_too_long(const std::string& msg);
702
703 };
704
705
706
710 class QRBitBuffer final : public Buffer<bool>
711 {
712 public:
717
718
719
720 /*---- Method ----*/
721
722 public:
730 void appendBits(uint04 val, uint04 len);
731
732 };
733
734}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
constexpr Buffer() noexcept
Definition Buffer.hpp:65
void appendBits(uint04 val, uint04 len)
Appends the given number of low-order bits of the given value to this buffer.
QRBitBuffer()
Constructs an empty bit buffer.
static constexpr int MAX_VERSION
The maximum QR Code version number (40).
Definition QRCode.h:665
QrCode(int ver, Ecc ecl, const Buffer< std::uint8_t > &dataCodewords, int msk)
Creates a new QR Code with the given version number, error correction level, data codeword bytes,...
int getSize() const
Returns this QR Code's size (width and height in modules).
String toSVG(uint04 border, RGBColor color, RGBColor background=Constant< RGBColor >::Invalid) const
Generates an SVG (Scalable Vector Graphics) string representation of this QR Code.
int getVersion() const
Returns this QR Code's version number.
bool getModule(int x, int y) const
Returns the color of the module (pixel) at the given coordinates.
static QrCode encodeBinary(const uint01 *data, uint04 size, Ecc ecl)
Returns a QR Code representing the given binary data at the given error correction level.
Ecc getErrorCorrectionLevel() const
Returns this QR Code's error correction level.
static QrCode encodeSegments(const QrSegmentBuffer &segs, Ecc ecl, int minVersion=1, int maxVersion=40, int mask=-1, bool boostEcl=true)
Returns a QR Code representing the given segments with the given encoding parameters.
int getMask() const
Returns this QR Code's mask pattern index.
static constexpr int MIN_VERSION
The minimum QR Code version number (1).
Definition QRCode.h:664
static QrCode encodeText(const char *text, Ecc ecl)
Returns a QR Code representing the given Unicode text string at the given error correction level.
static QrCode encodeLink(const char *text, Ecc ecl)
Returns a QR Code representing the given link text at the given error correction level.
Ecc
The error correction level in a QR Code symbol.
Definition QRCode.h:289
Describes how a segment's data bits are interpreted.
Definition QRCode.h:61
static const Mode BYTE
Mode for encoding arbitrary binary data.
Definition QRCode.h:68
int numCharCountBits(int ver) const
Returns the bit width of the character count field for a segment in this mode at the given QR Code ve...
static const Mode ECI
Mode for Extended Channel Interpretation designators.
Definition QRCode.h:70
static const Mode NUMERIC
Mode for encoding decimal digit strings.
Definition QRCode.h:66
static const Mode ALPHANUMERIC
Mode for encoding alphanumeric text (0-9, A-Z, space, and select symbols).
Definition QRCode.h:67
static const Mode KANJI
Mode for encoding Shift JIS Kanji characters.
Definition QRCode.h:69
int getModeBits() const
Returns the mode indicator bits as an unsigned 4-bit value (range 0 to 15).
A segment of character, binary, or control data in a QR Code symbol.
Definition QRCode.h:49
const Mode & getMode() const
Returns the mode field of this segment.
static QrSegment makeAlphanumeric(const char *text)
Returns a segment representing the given text string encoded in alphanumeric mode.
static int getTotalBits(const QrSegmentBuffer &segs, int version)
Calculates the total number of bits needed to encode the given segments at the given version.
QrSegment(const Mode &md, int numCh, Buffer< bool > &&dt)
Creates a new QR Code segment with the given attributes and data (move).
static QrSegment makeBytes(const uint01 *data, uint04 size)
Returns a segment representing the given binary data encoded in byte mode.
static QrSegmentBuffer makeSegments(const char *text)
Returns a list of zero or more segments to represent the given text string.
static bool isAlphanumeric(const char *text)
Tests whether the given string can be encoded as a segment in alphanumeric mode.
const Buffer< bool > & getData() const
Returns the data bits of this segment.
int getNumChars() const
Returns the character count field of this segment.
static QrSegment makeEci(long assignVal)
Returns a segment representing an Extended Channel Interpretation (ECI) designator with the given ass...
static QrSegment makeNumeric(const char *digits)
Returns a segment representing the given string of decimal digits encoded in numeric mode.
QrSegment(const Mode &md, int numCh, const Buffer< bool > &dt)
Creates a new QR Code segment with the given attributes and data (copy).
static bool isNumeric(const char *text)
Tests whether the given string can be encoded as a segment in numeric mode.
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:57
The core String class for the NDEVR API.
Definition String.h:95
data_too_long(const std::string &msg)
Constructs the exception with a descriptive message.
The primary namespace for the NDEVR SDK.
AlocatingAlignedBuffer< QrSegment, sizeof(void *)==4 ? 16 :32 > QrSegmentBuffer
Aligned buffer type alias for storing QrSegment objects.
Definition QRCode.h:34
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
int8_t sint01
-Defines an alias representing a 1 byte, signed integer.