Skip to content

Commit

Permalink
QR Code: reduce encode time by about 10% (mask evaluation)
Browse files Browse the repository at this point in the history
  • Loading branch information
gredler committed Aug 8, 2024
1 parent 7545cca commit 918cdca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ and data.
#### Okapi Barcode 0.4.7
- Update build toolchain from Java 17 to Java 21 (minimum target runtime remains Java 8)
- SVG and EPS output: round up canvas dimensions when using decimal magnification factor
- MaxiCode: reduce memory use during encoding
- QR Code: reduce encode time by about 15%

#### Okapi Barcode 0.4.6
- QR Code: allow FNC1 escape sequences in user-provided content
Expand Down
43 changes: 20 additions & 23 deletions src/main/java/uk/org/okapibarcode/backend/QrCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1524,10 +1524,9 @@ private static void addFormatInfoEval(byte[] eval, int size, EccLevel ecc_level,

private static int evaluate(byte[] eval, int size, int pattern, int best, StringBuilder encodeInfo) {

int x, y, i, block, weight;
int x, y, i, block;
int result = 0;
byte state;
int p;
int dark_mods;
int percentage, k;
int afterCount, beforeCount;
Expand All @@ -1537,13 +1536,11 @@ private static int evaluate(byte[] eval, int size, int pattern, int best, String
// that make up the grid array; select them for evaluation according to the
// desired pattern
dark_mods = 0;
for (x = 0; x < size; x++) {
for (y = 0; y < size; y++) {
i = (y * size) + x;
if ((eval[i] & (0x01 << pattern)) != 0) {
local[i] = 1;
dark_mods++; // optimization: early prep for test 4 below
}
int mask = 0x01 << pattern;
for (i = 0; i < local.length; i++) {
if ((eval[i] & mask) != 0) {
local[i] = 1;
dark_mods++; // optimization: early prep for test 4 below
}
}

Expand Down Expand Up @@ -1621,13 +1618,13 @@ private static int evaluate(byte[] eval, int size, int pattern, int best, String
/* Vertical */
for (x = 0; x < size; x++) {
for (y = 0; y < (size - 7); y++) {
p = 0;
for (weight = 0; weight < 7; weight++) {
if (local[((y + weight) * size) + x] == 1) {
p += (0x40 >> weight);
}
}
if (p == 0x5d) {
if (local[((y + 0) * size) + x] == 1 &&
local[((y + 1) * size) + x] == 0 &&
local[((y + 2) * size) + x] == 1 &&
local[((y + 3) * size) + x] == 1 &&
local[((y + 4) * size) + x] == 1 &&
local[((y + 5) * size) + x] == 0 &&
local[((y + 6) * size) + x] == 1) {
/* Pattern found, check before and after */
beforeCount = 0;
for (i = (y - 4); i < y; i++) {
Expand Down Expand Up @@ -1669,13 +1666,13 @@ private static int evaluate(byte[] eval, int size, int pattern, int best, String
/* Horizontal */
for (y = 0; y < size; y++) {
for (x = 0; x < (size - 7); x++) {
p = 0;
for (weight = 0; weight < 7; weight++) {
if (local[(y * size) + x + weight] == 1) {
p += (0x40 >> weight);
}
}
if (p == 0x5d) {
if (local[(y * size) + x + 0] == 1 &&
local[(y * size) + x + 1] == 0 &&
local[(y * size) + x + 2] == 1 &&
local[(y * size) + x + 3] == 1 &&
local[(y * size) + x + 4] == 1 &&
local[(y * size) + x + 5] == 0 &&
local[(y * size) + x + 6] == 1) {
/* Pattern found, check before and after */
beforeCount = 0;
for (i = (x - 4); i < x; i++) {
Expand Down

0 comments on commit 918cdca

Please sign in to comment.