Skip to content

Commit 70bdc43

Browse files
committed
Simplify QR Code mask evaluation a bit (no performance impact)
1 parent 918cdca commit 70bdc43

File tree

1 file changed

+31
-51
lines changed

1 file changed

+31
-51
lines changed

src/main/java/uk/org/okapibarcode/backend/QrCode.java

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ private static int evaluate(byte[] eval, int size, int pattern, int best, String
15291529
byte state;
15301530
int dark_mods;
15311531
int percentage, k;
1532-
int afterCount, beforeCount;
1532+
boolean light;
15331533
byte[] local = new byte[size * size];
15341534

15351535
// all eight bit mask variants have been encoded in the 8 bits of the bytes
@@ -1550,8 +1550,8 @@ private static int evaluate(byte[] eval, int size, int pattern, int best, String
15501550
/* Vertical */
15511551
for (x = 0; x < size; x++) {
15521552
state = local[x];
1553-
block = 0;
1554-
for (y = 0; y < size; y++) {
1553+
block = 1;
1554+
for (y = 1; y < size; y++) {
15551555
i = (y * size) + x;
15561556
if (local[i] == state) {
15571557
block++;
@@ -1571,8 +1571,8 @@ private static int evaluate(byte[] eval, int size, int pattern, int best, String
15711571
/* Horizontal */
15721572
for (y = 0; y < size; y++) {
15731573
state = local[y * size];
1574-
block = 0;
1575-
for (x = 0; x < size; x++) {
1574+
block = 1;
1575+
for (x = 1; x < size; x++) {
15761576
i = (y * size) + x;
15771577
if (local[i] == state) {
15781578
block++;
@@ -1625,36 +1625,26 @@ private static int evaluate(byte[] eval, int size, int pattern, int best, String
16251625
local[((y + 4) * size) + x] == 1 &&
16261626
local[((y + 5) * size) + x] == 0 &&
16271627
local[((y + 6) * size) + x] == 1) {
1628-
/* Pattern found, check before and after */
1629-
beforeCount = 0;
1630-
for (i = (y - 4); i < y; i++) {
1631-
if (i < 0) {
1632-
beforeCount++;
1633-
} else {
1634-
if (local[(i * size) + x] == 0) {
1635-
beforeCount++;
1636-
} else {
1637-
beforeCount = 0;
1638-
}
1628+
// Pattern found, check before and after
1629+
light = true;
1630+
for (i = y - 4; i < y; i++) {
1631+
if (i >= 0 && local[(i * size) + x] == 1) {
1632+
light = false;
1633+
break;
16391634
}
16401635
}
1641-
if (beforeCount == 4) {
1636+
if (light) {
16421637
// Pattern is preceded by light area 4 modules wide
16431638
result += 40;
16441639
} else {
1645-
afterCount = 0;
1646-
for (i = (y + 7); i <= (y + 10); i++) {
1647-
if (i >= size) {
1648-
afterCount++;
1649-
} else {
1650-
if (local[(i * size) + x] == 0) {
1651-
afterCount++;
1652-
} else {
1653-
afterCount = 0;
1654-
}
1640+
light = true;
1641+
for (i = y + 7; i <= y + 10; i++) {
1642+
if (i < size && local[(i * size) + x] == 1) {
1643+
light = false;
1644+
break;
16551645
}
16561646
}
1657-
if (afterCount == 4) {
1647+
if (light) {
16581648
// Pattern is followed by light area 4 modules wide
16591649
result += 40;
16601650
}
@@ -1673,36 +1663,26 @@ private static int evaluate(byte[] eval, int size, int pattern, int best, String
16731663
local[(y * size) + x + 4] == 1 &&
16741664
local[(y * size) + x + 5] == 0 &&
16751665
local[(y * size) + x + 6] == 1) {
1676-
/* Pattern found, check before and after */
1677-
beforeCount = 0;
1678-
for (i = (x - 4); i < x; i++) {
1679-
if (i < 0) {
1680-
beforeCount++;
1681-
} else {
1682-
if (local[(y * size) + i] == 0) {
1683-
beforeCount++;
1684-
} else {
1685-
beforeCount = 0;
1686-
}
1666+
// Pattern found, check before and after
1667+
light = true;
1668+
for (i = x - 4; i < x; i++) {
1669+
if (i >= 0 && local[(y * size) + i] == 1) {
1670+
light = false;
1671+
break;
16871672
}
16881673
}
1689-
if (beforeCount == 4) {
1674+
if (light) {
16901675
// Pattern is preceded by light area 4 modules wide
16911676
result += 40;
16921677
} else {
1693-
afterCount = 0;
1694-
for (i = (x + 7); i <= (x + 10); i++) {
1695-
if (i >= size) {
1696-
afterCount++;
1697-
} else {
1698-
if (local[(y * size) + i] == 0) {
1699-
afterCount++;
1700-
} else {
1701-
afterCount = 0;
1702-
}
1678+
light = true;
1679+
for (i = x + 7; i <= x + 10; i++) {
1680+
if (i < size && local[(y * size) + i] == 1) {
1681+
light = false;
1682+
break;
17031683
}
17041684
}
1705-
if (afterCount == 4) {
1685+
if (light) {
17061686
// Pattern is followed by light area 4 modules wide
17071687
result += 40;
17081688
}

0 commit comments

Comments
 (0)