Lines Matching +full:- +full:y

5  * https://www.nayuki.io/page/qr-code-generator-library
13 * - The above copyright notice and this permission notice shall be included in
15 * - The Software is provided "as is", without warranty of any kind, express or
37 /*---- Forward declarations for private functions ----*/
40 // - They require all pointer/array arguments to be not null unless the array length is zero.
41 // - They only read input scalar/array arguments, write to output pointer/array
43 // - They don't read mutable global variables or write to any global variables.
44 // - They don't perform I/O, read the clock, print to console, etc.
45 // - They allocate a small and constant amount of stack memory.
46 // - They don't allocate or free any memory on the heap.
47 // - They don't recurse or mutually recurse. All the code
48 // could be inlined into the top-level public functions.
49 // - They run in at most quadratic time with respect to input arguments.
51 // There are no unbounded loops or non-obvious termination conditions.
52 // - They are completely thread-safe if the caller does not give the
64 testable uint8_t finiteFieldMultiply(uint8_t x, uint8_t y);
78 testable bool getModule(const uint8_t qrcode[], int x, int y);
79 testable void setModule(uint8_t qrcode[], int x, int y, bool isBlack);
80 testable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isBlack);
89 /*---- Private tables of constants ----*/
93 static const char *ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
99 …{-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 3…
100 …{-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 2…
101 …{-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 3…
102 …{-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 3…
111 …{-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12,…
112 …{-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23,…
113 …{-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34,…
114 …{-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37,…
125 /*---- High-level QR Code encoding functions ----*/
127 // Public function - see documentation comment in header file.
152 if (seg.bitLength == -1) in qrcodegen_encodeText()
165 // Public function - see documentation comment in header file.
172 if (seg.bitLength == -1) { in qrcodegen_encodeBinary()
182 // Appends the given number of low-order bits of the given value to the given byte-based
186 for (int i = numBits - 1; i >= 0; i--, (*bitLen)++) in appendBitsToBuffer()
187 buffer[*bitLen >> 3] |= ((val >> i) & 1) << (7 - (*bitLen & 7)); in appendBitsToBuffer()
192 /*---- Low-level QR Code encoding functions ----*/
194 // Public function - see documentation comment in header file.
198 qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, -1, true, tempBuffer, qrcode); in qrcodegen_encodeSegments()
202 // Public function - see documentation comment in header file.
207 LV_ASSERT(0 <= (int)ecl && (int)ecl <= 3 && -1 <= (int)mask && (int)mask <= 7); in qrcodegen_encodeSegmentsAdvanced()
214 if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits) in qrcodegen_encodeSegmentsAdvanced()
221 LV_ASSERT(dataUsedBits != -1); in qrcodegen_encodeSegmentsAdvanced()
234 appendBitsToBuffer((int)seg->mode, 4, qrcode, &bitLen); in qrcodegen_encodeSegmentsAdvanced()
235 appendBitsToBuffer(seg->numChars, numCharCountBits(seg->mode, version), qrcode, &bitLen); in qrcodegen_encodeSegmentsAdvanced()
236 for (int j = 0; j < seg->bitLength; j++) in qrcodegen_encodeSegmentsAdvanced()
237 appendBitsToBuffer((seg->data[j >> 3] >> (7 - (j & 7))) & 1, 1, qrcode, &bitLen); in qrcodegen_encodeSegmentsAdvanced()
244 int terminatorBits = dataCapacityBits - bitLen; in qrcodegen_encodeSegmentsAdvanced()
248 appendBitsToBuffer(0, (8 - bitLen % 8) % 8, qrcode, &bitLen); in qrcodegen_encodeSegmentsAdvanced()
285 /*---- Error correction code generation functions ----*/
298 int numShortBlocks = numBlocks - rawCodewords % numBlocks; in addEccAndInterleave()
299 int shortBlockDataLen = rawCodewords / numBlocks - blockEccLen; in addEccAndInterleave()
312 k -= numShortBlocks; in addEccAndInterleave()
322 // Returns the number of 8-bit codewords that can be used for storing data (not ECC),
328 - ECC_CODEWORDS_PER_BLOCK [e][v] in getNumDataCodewords()
335 // The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
341 result -= (25 * numAlign - 10) * numAlign - 55; in getNumRawDataModules()
343 result -= 36; in getNumRawDataModules()
350 /*---- Reed-Solomon ECC generator functions ----*/
352 // Calculates the Reed-Solomon generator polynomial of the given degree, storing in result[0 : degr…
357 result[degree - 1] = 1; in calcReedSolomonGenerator()
359 // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}), in calcReedSolomonGenerator()
364 // Multiply the current product by (x - r^i) in calcReedSolomonGenerator()
385 memmove(&result[0], &result[1], (degree - 1) * sizeof(result[0])); in calcReedSolomonRemainder()
386 result[degree - 1] = 0; in calcReedSolomonRemainder()
397 testable uint8_t finiteFieldMultiply(uint8_t x, uint8_t y) { in finiteFieldMultiply() argument
400 for (int i = 7; i >= 0; i--) { in finiteFieldMultiply()
402 z ^= ((y >> i) & 1) * x; in finiteFieldMultiply()
409 /*---- Drawing function modules ----*/
425 fillRectangle(qrsize - 8, 0, 8, 9, qrcode); in initializeFunctionModules()
426 fillRectangle(0, qrsize - 8, 9, 8, qrcode); in initializeFunctionModules()
434 if (!((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0))) in initializeFunctionModules()
435 fillRectangle(alignPatPos[i] - 2, alignPatPos[j] - 2, 5, 5, qrcode); in initializeFunctionModules()
441 fillRectangle(qrsize - 11, 0, 3, 6, qrcode); in initializeFunctionModules()
442 fillRectangle(0, qrsize - 11, 6, 3, qrcode); in initializeFunctionModules()
448 // non-function modules. This does not draw the format bits. This requires all function modules to …
453 for (int i = 7; i < qrsize - 7; i += 2) { in drawWhiteFunctionModules()
459 for (int dy = -4; dy <= 4; dy++) { in drawWhiteFunctionModules()
460 for (int dx = -4; dx <= 4; dx++) { in drawWhiteFunctionModules()
466 setModuleBounded(qrcode, qrsize - 4 + dx, 3 + dy, false); in drawWhiteFunctionModules()
467 setModuleBounded(qrcode, 3 + dx, qrsize - 4 + dy, false); in drawWhiteFunctionModules()
477 if ((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0)) in drawWhiteFunctionModules()
479 for (int dy = -1; dy <= 1; dy++) { in drawWhiteFunctionModules()
480 for (int dx = -1; dx <= 1; dx++) in drawWhiteFunctionModules()
498 int k = qrsize - 11 + j; in drawWhiteFunctionModules()
529 setModule(qrcode, 14 - i, 8, getBit(bits, i)); in drawFormatBits()
534 setModule(qrcode, qrsize - 1 - i, 8, getBit(bits, i)); in drawFormatBits()
536 setModule(qrcode, 8, qrsize - 15 + i, getBit(bits, i)); in drawFormatBits()
537 setModule(qrcode, 8, qrsize - 8, true); // Always black in drawFormatBits()
543 // Each position is in the range [0,177), and are used on both the x and y axes.
544 // This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.
550 (version*4 + numAlign*2 + 1) / (numAlign*2 - 2) * 2; in getAlignmentPatternPositions()
551 for (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step) in getAlignmentPatternPositions()
568 /*---- Drawing data modules and masking ----*/
576 …for (int right = qrsize - 1; right >= 1; right -= 2) { // Index of right column in each column pa… in drawCodewords()
581 int x = right - j; // Actual x coordinate in drawCodewords()
583 int y = upward ? qrsize - 1 - vert : vert; // Actual y coordinate in drawCodewords() local
584 if (!getModule(qrcode, x, y) && i < dataLen * 8) { in drawCodewords()
585 bool black = getBit(data[i >> 3], 7 - (i & 7)); in drawCodewords()
586 setModule(qrcode, x, y, black); in drawCodewords()
601 // the same mask value a second time will undo the mask. A final well-formed
606 for (int y = 0; y < qrsize; y++) { in applyMask() local
608 if (getModule(functionModules, x, y)) in applyMask()
612 case 0: invert = (x + y) % 2 == 0; break; in applyMask()
613 case 1: invert = y % 2 == 0; break; in applyMask()
615 case 3: invert = (x + y) % 3 == 0; break; in applyMask()
616 case 4: invert = (x / 3 + y / 2) % 2 == 0; break; in applyMask()
617 case 5: invert = x * y % 2 + x * y % 3 == 0; break; in applyMask()
618 case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break; in applyMask()
619 case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break; in applyMask()
622 bool val = getModule(qrcode, x, y); in applyMask()
623 setModule(qrcode, x, y, val ^ invert); in applyMask()
635 // Adjacent modules in row having same color, and finder-like patterns in getPenaltyScore()
636 for (int y = 0; y < qrsize; y++) { in getPenaltyScore() local
641 if (getModule(qrcode, x, y) == color) { in getPenaltyScore()
651 color = getModule(qrcode, x, y); in getPenaltyScore()
661 // Adjacent modules in column having same color, and finder-like patterns in getPenaltyScore()
666 for (int y = 0; y < qrsize; y++) { in getPenaltyScore() local
667 if (getModule(qrcode, x, y) == color) { in getPenaltyScore()
677 color = getModule(qrcode, x, y); in getPenaltyScore()
689 for (int y = 0; y < qrsize - 1; y++) { in getPenaltyScore() local
690 for (int x = 0; x < qrsize - 1; x++) { in getPenaltyScore()
691 bool color = getModule(qrcode, x, y); in getPenaltyScore()
692 if ( color == getModule(qrcode, x + 1, y) && in getPenaltyScore()
693 color == getModule(qrcode, x, y + 1) && in getPenaltyScore()
694 color == getModule(qrcode, x + 1, y + 1)) in getPenaltyScore()
701 for (int y = 0; y < qrsize; y++) { in getPenaltyScore() local
703 if (getModule(qrcode, x, y)) in getPenaltyScore()
708 // Compute the smallest integer k >= 0 such that (45-5k)% <= black/total <= (55+5k)% in getPenaltyScore()
709 int k = (int)((labs(black * 20L - total * 10L) + total - 1) / total) - 1; in getPenaltyScore()
736 /*---- Basic QR Code information ----*/
738 // Public function - see documentation comment in header file.
748 // Public function - see documentation comment in header file.
749 bool qrcodegen_getModule(const uint8_t qrcode[], int x, int y) { in qrcodegen_getModule() argument
752 return (0 <= x && x < qrsize && 0 <= y && y < qrsize) && getModule(qrcode, x, y); in qrcodegen_getModule()
757 testable bool getModule(const uint8_t qrcode[], int x, int y) { in getModule() argument
759 LV_ASSERT(21 <= qrsize && qrsize <= 177 && 0 <= x && x < qrsize && 0 <= y && y < qrsize); in getModule()
760 int index = y * qrsize + x; in getModule()
766 testable void setModule(uint8_t qrcode[], int x, int y, bool isBlack) { in setModule() argument
768 LV_ASSERT(21 <= qrsize && qrsize <= 177 && 0 <= x && x < qrsize && 0 <= y && y < qrsize); in setModule()
769 int index = y * qrsize + x; in setModule()
780 testable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isBlack) { in setModuleBounded() argument
782 if (0 <= x && x < qrsize && 0 <= y && y < qrsize) in setModuleBounded()
783 setModule(qrcode, x, y, isBlack); in setModuleBounded()
794 /*---- Segment handling ----*/
796 // Public function - see documentation comment in header file.
807 // Public function - see documentation comment in header file.
818 // Public function - see documentation comment in header file.
821 if (temp == -1) in qrcodegen_calcSegmentBufferSize()
830 // - Returns -1 on failure, i.e. numChars > INT16_MAX or
832 // - Otherwise, all valid results are in the range [0, INT16_MAX].
833 // - For byte mode, numChars measures the number of bytes, not Unicode code points.
834 // - For ECI mode, numChars must be 0, and the worst-case number of bits is returned.
835 // An actual ECI segment can have shorter data. For non-ECI modes, the result is exact.
839 return -1; in calcSegmentBitLength()
853 return -1; in calcSegmentBitLength()
857 return -1; in calcSegmentBitLength()
862 // Public function - see documentation comment in header file.
868 LV_ASSERT(result.bitLength != -1); in qrcodegen_makeBytes()
877 // Public function - see documentation comment in header file.
884 LV_ASSERT(bitLen != -1); in qrcodegen_makeNumeric()
895 accumData = accumData * 10 + (unsigned int)(c - '0'); in qrcodegen_makeNumeric()
911 // Public function - see documentation comment in header file.
918 LV_ASSERT(bitLen != -1); in qrcodegen_makeAlphanumeric()
929 accumData = accumData * 45 + (unsigned int)(temp - ALPHANUMERIC_CHARSET); in qrcodegen_makeAlphanumeric()
945 // Public function - see documentation comment in header file.
974 // Returns a non-negative number if successful. Otherwise returns -1 if a segment has too
987 return -1; // The segment's length doesn't fit the field's bit width in getTotalBits()
990 return -1; // The sum might overflow an int type in getTotalBits()
1008 default: LV_ASSERT(false); return -1; // Dummy value in numCharCountBits()
1022 if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits) in qrcodegen_getMinFitVersion()
1025 return -1; in qrcodegen_getMinFitVersion()
1031 return -1; in qrcodegen_version2size()
1034 return ((version - 1)*4 + 21); in qrcodegen_version2size()