Changed syntax so that every class member is explicitly prefixed with public/private like in Java/C# style, without changing any functionality.
This commit is contained in:
parent
9655cbc12a
commit
654520d0d4
|
@ -37,39 +37,36 @@ namespace qrcodegen {
|
|||
class BitBuffer final {
|
||||
|
||||
/*---- Fields ----*/
|
||||
private:
|
||||
|
||||
std::vector<uint8_t> data;
|
||||
int bitLength;
|
||||
private: std::vector<uint8_t> data;
|
||||
private: int bitLength;
|
||||
|
||||
|
||||
|
||||
/*---- Constructor ----*/
|
||||
public:
|
||||
|
||||
// Creates an empty bit buffer (length 0).
|
||||
BitBuffer();
|
||||
public: BitBuffer();
|
||||
|
||||
|
||||
|
||||
/*---- Methods ----*/
|
||||
public:
|
||||
|
||||
// Returns the number of bits in the buffer, which is a non-negative value.
|
||||
int getBitLength() const;
|
||||
public: int getBitLength() const;
|
||||
|
||||
|
||||
// Returns a copy of all bytes, padding up to the nearest byte.
|
||||
std::vector<uint8_t> getBytes() const;
|
||||
public: std::vector<uint8_t> getBytes() const;
|
||||
|
||||
|
||||
// Appends the given number of bits of the given value to this sequence.
|
||||
// If 0 <= len <= 31, then this requires 0 <= val < 2^len.
|
||||
void appendBits(uint32_t val, int len);
|
||||
public: void appendBits(uint32_t val, int len);
|
||||
|
||||
|
||||
// Appends the data of the given segment to this bit buffer.
|
||||
void appendData(const QrSegment &seg);
|
||||
public: void appendData(const QrSegment &seg);
|
||||
|
||||
};
|
||||
|
||||
|
|
107
cpp/QrCode.hpp
107
cpp/QrCode.hpp
|
@ -41,30 +41,25 @@ namespace qrcodegen {
|
|||
class QrCode final {
|
||||
|
||||
/*---- Public helper enumeration ----*/
|
||||
public:
|
||||
|
||||
/*
|
||||
* Represents the error correction level used in a QR Code symbol.
|
||||
*/
|
||||
class Ecc final {
|
||||
public: class Ecc final {
|
||||
// Constants declared in ascending order of error protection.
|
||||
public:
|
||||
const static Ecc LOW, MEDIUM, QUARTILE, HIGH;
|
||||
public: const static Ecc LOW, MEDIUM, QUARTILE, HIGH;
|
||||
|
||||
// Fields.
|
||||
public:
|
||||
const int ordinal; // (Public) In the range 0 to 3 (unsigned 2-bit integer).
|
||||
const int formatBits; // (Package-private) In the range 0 to 3 (unsigned 2-bit integer).
|
||||
public: const int ordinal; // (Public) In the range 0 to 3 (unsigned 2-bit integer).
|
||||
public: const int formatBits; // (Package-private) In the range 0 to 3 (unsigned 2-bit integer).
|
||||
|
||||
// Constructor.
|
||||
private:
|
||||
Ecc(int ord, int fb);
|
||||
private: Ecc(int ord, int fb);
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*---- Public static factory functions ----*/
|
||||
public:
|
||||
|
||||
/*
|
||||
* Returns a QR Code symbol representing the given Unicode text string at the given error correction level.
|
||||
|
@ -72,7 +67,7 @@ public:
|
|||
* code points (not UTF-16 code units). The smallest possible QR Code version is automatically chosen for the output.
|
||||
* The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
|
||||
*/
|
||||
static QrCode encodeText(const char *text, const Ecc &ecl);
|
||||
public: static QrCode encodeText(const char *text, const Ecc &ecl);
|
||||
|
||||
|
||||
/*
|
||||
|
@ -81,7 +76,7 @@ public:
|
|||
* bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
|
||||
* The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
|
||||
*/
|
||||
static QrCode encodeBinary(const std::vector<uint8_t> &data, const Ecc &ecl);
|
||||
public: static QrCode encodeBinary(const std::vector<uint8_t> &data, const Ecc &ecl);
|
||||
|
||||
|
||||
/*
|
||||
|
@ -91,7 +86,7 @@ public:
|
|||
* between modes (such as alphanumeric and binary) to encode text more efficiently.
|
||||
* This function is considered to be lower level than simply encoding text or binary data.
|
||||
*/
|
||||
static QrCode encodeSegments(const std::vector<QrSegment> &segs, const Ecc &ecl,
|
||||
public: static QrCode encodeSegments(const std::vector<QrSegment> &segs, const Ecc &ecl,
|
||||
int minVersion=1, int maxVersion=40, int mask=-1, bool boostEcl=true); // All optional parameters
|
||||
|
||||
|
||||
|
@ -99,40 +94,36 @@ public:
|
|||
/*---- Instance fields ----*/
|
||||
|
||||
// Public immutable scalar parameters
|
||||
public:
|
||||
|
||||
/* This QR Code symbol's version number, which is always between 1 and 40 (inclusive). */
|
||||
const int version;
|
||||
public: const int version;
|
||||
|
||||
/* The width and height of this QR Code symbol, measured in modules.
|
||||
* Always equal to version × 4 + 17, in the range 21 to 177. */
|
||||
const int size;
|
||||
public: const int size;
|
||||
|
||||
/* The error correction level used in this QR Code symbol. */
|
||||
const Ecc &errorCorrectionLevel;
|
||||
public: const Ecc &errorCorrectionLevel;
|
||||
|
||||
/* The mask pattern used in this QR Code symbol, in the range 0 to 7 (i.e. unsigned 3-bit integer).
|
||||
* Note that even if a constructor was called with automatic masking requested
|
||||
* (mask = -1), the resulting object will still have a mask value between 0 and 7. */
|
||||
private:
|
||||
int mask;
|
||||
private: int mask;
|
||||
|
||||
// Private grids of modules/pixels (conceptually immutable)
|
||||
private:
|
||||
std::vector<std::vector<bool>> modules; // The modules of this QR Code symbol (false = white, true = black)
|
||||
std::vector<std::vector<bool>> isFunction; // Indicates function modules that are not subjected to masking
|
||||
private: std::vector<std::vector<bool>> modules; // The modules of this QR Code symbol (false = white, true = black)
|
||||
private: std::vector<std::vector<bool>> isFunction; // Indicates function modules that are not subjected to masking
|
||||
|
||||
|
||||
|
||||
/*---- Constructors ----*/
|
||||
public:
|
||||
|
||||
/*
|
||||
* Creates a new QR Code symbol with the given version number, error correction level, binary data array,
|
||||
* and mask number. This is a cumbersome low-level constructor that should not be invoked directly by the user.
|
||||
* To go one level up, see the encodeSegments() function.
|
||||
*/
|
||||
QrCode(int ver, const Ecc &ecl, const std::vector<uint8_t> &dataCodewords, int mask);
|
||||
public: QrCode(int ver, const Ecc &ecl, const std::vector<uint8_t> &dataCodewords, int mask);
|
||||
|
||||
|
||||
/*
|
||||
|
@ -140,21 +131,20 @@ public:
|
|||
* different mask pattern. The version, error correction level, codewords, etc. of the newly
|
||||
* created object are all identical to the argument object; only the mask may differ.
|
||||
*/
|
||||
QrCode(const QrCode &qr, int mask);
|
||||
public: QrCode(const QrCode &qr, int mask);
|
||||
|
||||
|
||||
|
||||
/*---- Public instance methods ----*/
|
||||
public:
|
||||
|
||||
int getMask() const;
|
||||
public: int getMask() const;
|
||||
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
int getModule(int x, int y) const;
|
||||
public: int getModule(int x, int y) const;
|
||||
|
||||
|
||||
/*
|
||||
|
@ -162,150 +152,141 @@ public:
|
|||
* string whose contents represents an SVG XML file that depicts this QR Code symbol.
|
||||
* Note that Unix newlines (\n) are always used, regardless of the platform.
|
||||
*/
|
||||
std::string toSvgString(int border) const;
|
||||
public: std::string toSvgString(int border) const;
|
||||
|
||||
|
||||
|
||||
/*---- Private helper methods for constructor: Drawing function modules ----*/
|
||||
private:
|
||||
|
||||
void drawFunctionPatterns();
|
||||
private: void drawFunctionPatterns();
|
||||
|
||||
|
||||
// Draws two copies of the format bits (with its own error correction code)
|
||||
// based on the given mask and this object's error correction level field.
|
||||
void drawFormatBits(int mask);
|
||||
private: void drawFormatBits(int mask);
|
||||
|
||||
|
||||
// Draws two copies of the version bits (with its own error correction code),
|
||||
// based on this object's version field (which only has an effect for 7 <= version <= 40).
|
||||
void drawVersion();
|
||||
private: void drawVersion();
|
||||
|
||||
|
||||
// Draws a 9*9 finder pattern including the border separator, with the center module at (x, y).
|
||||
void drawFinderPattern(int x, int y);
|
||||
private: void drawFinderPattern(int x, int y);
|
||||
|
||||
|
||||
// Draws a 5*5 alignment pattern, with the center module at (x, y).
|
||||
void drawAlignmentPattern(int x, int y);
|
||||
private: void drawAlignmentPattern(int x, int y);
|
||||
|
||||
|
||||
// Sets the color of a module and marks it as a function module.
|
||||
// Only used by the constructor. Coordinates must be in range.
|
||||
void setFunctionModule(int x, int y, bool isBlack);
|
||||
private: void setFunctionModule(int x, int y, bool isBlack);
|
||||
|
||||
|
||||
/*---- Private helper methods for constructor: Codewords and masking ----*/
|
||||
private:
|
||||
|
||||
// Returns a new byte string representing the given data with the appropriate error correction
|
||||
// codewords appended to it, based on this object's version and error correction level.
|
||||
std::vector<uint8_t> appendErrorCorrection(const std::vector<uint8_t> &data) const;
|
||||
private: std::vector<uint8_t> appendErrorCorrection(const std::vector<uint8_t> &data) const;
|
||||
|
||||
|
||||
// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
|
||||
// data area of this QR Code symbol. Function modules need to be marked off before this is called.
|
||||
void drawCodewords(const std::vector<uint8_t> &data);
|
||||
private: void drawCodewords(const std::vector<uint8_t> &data);
|
||||
|
||||
|
||||
// XORs the data modules in this QR Code with the given mask pattern. Due to XOR's mathematical
|
||||
// properties, calling applyMask(m) twice with the same value is equivalent to no change at all.
|
||||
// This means it is possible to apply a mask, undo it, and try another mask. Note that a final
|
||||
// well-formed QR Code symbol needs exactly one mask applied (not zero, not two, etc.).
|
||||
void applyMask(int mask);
|
||||
private: void applyMask(int mask);
|
||||
|
||||
|
||||
// A messy helper function for the constructors. This QR Code must be in an unmasked state when this
|
||||
// method is called. The given argument is the requested mask, which is -1 for auto or 0 to 7 for fixed.
|
||||
// This method applies and returns the actual mask chosen, from 0 to 7.
|
||||
int handleConstructorMasking(int mask);
|
||||
private: int handleConstructorMasking(int mask);
|
||||
|
||||
|
||||
// Calculates and returns the penalty score based on state of this QR Code's current modules.
|
||||
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
|
||||
int getPenaltyScore() const;
|
||||
private: int getPenaltyScore() const;
|
||||
|
||||
|
||||
|
||||
/*---- Private static helper functions ----*/
|
||||
private:
|
||||
|
||||
// Returns a set of positions of the alignment patterns in ascending order. These positions are
|
||||
// used on both the x and y axes. Each value in the resulting array is in the range [0, 177).
|
||||
// This stateless pure function could be implemented as table of 40 variable-length lists of unsigned bytes.
|
||||
static std::vector<int> getAlignmentPatternPositions(int ver);
|
||||
private: static std::vector<int> getAlignmentPatternPositions(int ver);
|
||||
|
||||
|
||||
// Returns the number of raw data modules (bits) available at the given version number.
|
||||
// These data modules are used for both user data codewords and error correction codewords.
|
||||
// This stateless pure function could be implemented as a 40-entry lookup table.
|
||||
static int getNumRawDataModules(int ver);
|
||||
private: static int getNumRawDataModules(int ver);
|
||||
|
||||
|
||||
// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
|
||||
// QR Code of the given version number and error correction level, with remainder bits discarded.
|
||||
// This stateless pure function could be implemented as a (40*4)-cell lookup table.
|
||||
static int getNumDataCodewords(int ver, const Ecc &ecl);
|
||||
private: static int getNumDataCodewords(int ver, const Ecc &ecl);
|
||||
|
||||
|
||||
/*---- Private tables of constants ----*/
|
||||
private:
|
||||
|
||||
// For use in getPenaltyScore(), when evaluating which mask is best.
|
||||
static const int PENALTY_N1;
|
||||
static const int PENALTY_N2;
|
||||
static const int PENALTY_N3;
|
||||
static const int PENALTY_N4;
|
||||
private: static const int PENALTY_N1;
|
||||
private: static const int PENALTY_N2;
|
||||
private: static const int PENALTY_N3;
|
||||
private: static const int PENALTY_N4;
|
||||
|
||||
static const int16_t NUM_ERROR_CORRECTION_CODEWORDS[4][41];
|
||||
static const int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41];
|
||||
private: static const int16_t NUM_ERROR_CORRECTION_CODEWORDS[4][41];
|
||||
private: static const int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41];
|
||||
|
||||
|
||||
|
||||
/*---- Private helper class ----*/
|
||||
private:
|
||||
|
||||
/*
|
||||
* Computes the Reed-Solomon error correction codewords for a sequence of data codewords
|
||||
* at a given degree. Objects are immutable, and the state only depends on the degree.
|
||||
* This class exists because the divisor polynomial does not need to be recalculated for every input.
|
||||
*/
|
||||
class ReedSolomonGenerator final {
|
||||
private: class ReedSolomonGenerator final {
|
||||
|
||||
/*-- Immutable field --*/
|
||||
private:
|
||||
|
||||
// Coefficients of the divisor polynomial, stored from highest to lowest power, excluding the leading term which
|
||||
// is always 1. For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.
|
||||
std::vector<uint8_t> coefficients;
|
||||
private: std::vector<uint8_t> coefficients;
|
||||
|
||||
|
||||
/*-- Constructor --*/
|
||||
public:
|
||||
|
||||
/*
|
||||
* Creates a Reed-Solomon ECC generator for the given degree. This could be implemented
|
||||
* as a lookup table over all possible parameter values, instead of as an algorithm.
|
||||
*/
|
||||
ReedSolomonGenerator(int degree);
|
||||
public: ReedSolomonGenerator(int degree);
|
||||
|
||||
|
||||
/*-- Method --*/
|
||||
public:
|
||||
|
||||
/*
|
||||
* Computes and returns the Reed-Solomon error correction codewords for the given sequence of data codewords.
|
||||
* The returned object is always a new byte array. This method does not alter this object's state (because it is immutable).
|
||||
*/
|
||||
std::vector<uint8_t> getRemainder(const std::vector<uint8_t> &data) const;
|
||||
public: std::vector<uint8_t> getRemainder(const std::vector<uint8_t> &data) const;
|
||||
|
||||
|
||||
/*-- Static function --*/
|
||||
private:
|
||||
|
||||
// Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
|
||||
// are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
|
||||
static uint8_t multiply(uint8_t x, uint8_t y);
|
||||
private: static uint8_t multiply(uint8_t x, uint8_t y);
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -45,32 +45,27 @@ class QrSegment final {
|
|||
/*
|
||||
* The mode field of a segment. Immutable. Provides methods to retrieve closely related values.
|
||||
*/
|
||||
public:
|
||||
class Mode final {
|
||||
public: class Mode final {
|
||||
|
||||
/*-- Constants --*/
|
||||
public:
|
||||
|
||||
static const Mode NUMERIC;
|
||||
static const Mode ALPHANUMERIC;
|
||||
static const Mode BYTE;
|
||||
static const Mode KANJI;
|
||||
public: static const Mode NUMERIC;
|
||||
public: static const Mode ALPHANUMERIC;
|
||||
public: static const Mode BYTE;
|
||||
public: static const Mode KANJI;
|
||||
|
||||
|
||||
/*-- Fields --*/
|
||||
|
||||
/* (Package-private) An unsigned 4-bit integer value (range 0 to 15) representing the mode indicator bits for this mode object. */
|
||||
public:
|
||||
const int modeBits;
|
||||
public: const int modeBits;
|
||||
|
||||
private:
|
||||
int numBitsCharCount[3];
|
||||
private: int numBitsCharCount[3];
|
||||
|
||||
|
||||
/*-- Constructor --*/
|
||||
|
||||
private:
|
||||
Mode(int mode, int cc0, int cc1, int cc2);
|
||||
private: Mode(int mode, int cc0, int cc1, int cc2);
|
||||
|
||||
|
||||
/*-- Method --*/
|
||||
|
@ -78,92 +73,86 @@ public:
|
|||
/*
|
||||
* (Package-private) Returns the bit width of the segment character count field for this mode object at the given version number.
|
||||
*/
|
||||
public:
|
||||
int numCharCountBits(int ver) const;
|
||||
public: int numCharCountBits(int ver) const;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*---- Public static factory functions ----*/
|
||||
public:
|
||||
|
||||
/*
|
||||
* Returns a segment representing the given binary data encoded in byte mode.
|
||||
*/
|
||||
static QrSegment makeBytes(const std::vector<uint8_t> &data);
|
||||
public: static QrSegment makeBytes(const std::vector<uint8_t> &data);
|
||||
|
||||
|
||||
/*
|
||||
* Returns a segment representing the given string of decimal digits encoded in numeric mode.
|
||||
*/
|
||||
static QrSegment makeNumeric(const char *digits);
|
||||
public: static QrSegment makeNumeric(const char *digits);
|
||||
|
||||
|
||||
/*
|
||||
* Returns a segment representing the given text string encoded in alphanumeric mode. The characters allowed are:
|
||||
* 0 to 9, A to Z (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
||||
*/
|
||||
static QrSegment makeAlphanumeric(const char *text);
|
||||
public: static QrSegment makeAlphanumeric(const char *text);
|
||||
|
||||
|
||||
/*
|
||||
* Returns a list of zero or more segments to represent the given text string.
|
||||
* The result may use various segment modes and switch modes to optimize the length of the bit stream.
|
||||
*/
|
||||
static std::vector<QrSegment> makeSegments(const char *text);
|
||||
public: static std::vector<QrSegment> makeSegments(const char *text);
|
||||
|
||||
|
||||
/*---- Public static helper functions ----*/
|
||||
public:
|
||||
|
||||
/*
|
||||
* Tests whether the given string can be encoded as a segment in alphanumeric mode.
|
||||
*/
|
||||
static bool isAlphanumeric(const char *text);
|
||||
public: static bool isAlphanumeric(const char *text);
|
||||
|
||||
|
||||
/*
|
||||
* Tests whether the given string can be encoded as a segment in numeric mode.
|
||||
*/
|
||||
static bool isNumeric(const char *text);
|
||||
public: static bool isNumeric(const char *text);
|
||||
|
||||
|
||||
|
||||
/*---- Instance fields ----*/
|
||||
public:
|
||||
|
||||
/* The mode indicator for this segment. */
|
||||
const Mode mode;
|
||||
public: const Mode mode;
|
||||
|
||||
/* The length of this segment's unencoded data, measured in characters. Always zero or positive. */
|
||||
const int numChars;
|
||||
public: const int numChars;
|
||||
|
||||
/* The bits of this segment packed into a byte array in big endian. */
|
||||
const std::vector<uint8_t> data;
|
||||
public: const std::vector<uint8_t> data;
|
||||
|
||||
/* The length of this segment's encoded data, measured in bits. Satisfies ceil(bitLength / 8) = data.size(). */
|
||||
const int bitLength;
|
||||
public: const int bitLength;
|
||||
|
||||
|
||||
/*---- Constructor ----*/
|
||||
public:
|
||||
|
||||
/*
|
||||
* Creates a new QR Code data segment with the given parameters and data.
|
||||
*/
|
||||
QrSegment(const Mode &md, int numCh, const std::vector<uint8_t> &b, int bitLen);
|
||||
public: QrSegment(const Mode &md, int numCh, const std::vector<uint8_t> &b, int bitLen);
|
||||
|
||||
|
||||
// Package-private helper function.
|
||||
static int getTotalBits(const std::vector<QrSegment> &segs, int version);
|
||||
public: static int getTotalBits(const std::vector<QrSegment> &segs, int version);
|
||||
|
||||
|
||||
/*---- Private constant ----*/
|
||||
private:
|
||||
|
||||
/* Maps shifted ASCII codes to alphanumeric mode character codes. */
|
||||
static const int8_t ALPHANUMERIC_ENCODING_TABLE[59];
|
||||
private: static const int8_t ALPHANUMERIC_ENCODING_TABLE[59];
|
||||
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue