MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / applyMask

Function applyMask

modules/data/qrcode/qrcodegen.c:526–549  ·  view source on GitHub ↗

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.).

Source from the content-addressed store, hash-verified

524// This means it is possible to apply a mask, undo it, and try another mask. Note that a final
525// well-formed QR Code symbol needs exactly one mask applied (not zero, not two, etc.).
526static void applyMask(const uint8_t functionModules[], uint8_t qrcode[], enum qrcodegen_Mask mask) {
527 assert(0 <= (int)mask && (int)mask <= 7); // Disallows qrcodegen_Mask_AUTO
528 int qrsize = qrcodegen_getSize(qrcode);
529 for (int y = 0; y < qrsize; y++) {
530 for (int x = 0; x < qrsize; x++) {
531 if (getModule(functionModules, x, y))
532 continue;
533 bool invert = false; // Dummy value
534 switch ((int)mask) {
535 case 0: invert = (x + y) % 2 == 0; break;
536 case 1: invert = y % 2 == 0; break;
537 case 2: invert = x % 3 == 0; break;
538 case 3: invert = (x + y) % 3 == 0; break;
539 case 4: invert = (x / 3 + y / 2) % 2 == 0; break;
540 case 5: invert = x * y % 2 + x * y % 3 == 0; break;
541 case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break;
542 case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;
543 default: assert(false);
544 }
545 bool val = getModule(qrcode, x, y);
546 setModule(qrcode, x, y, val ^ invert);
547 }
548 }
549}
550
551
552// Calculates and returns the penalty score based on state of the given QR Code's current modules.

Callers 1

Calls 4

qrcodegen_getSizeFunction · 0.85
getModuleFunction · 0.85
setModuleFunction · 0.85
assertFunction · 0.50

Tested by

no test coverage detected