From 432f3e0fede43c4f2bde42ba721ba49e0c7c7bbe Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Sat, 25 Aug 2018 23:05:31 +0000 Subject: [PATCH] Added helper function to Java QrSegmentAdvanced to clarify code. --- .../nayuki/qrcodegen/QrSegmentAdvanced.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java b/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java index 541d230..e7f421d 100644 --- a/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java +++ b/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java @@ -113,9 +113,9 @@ public final class QrSegmentAdvanced { result[2][j] = result[2][i] + 20; // 3.33 bits per digit // Switch modes, rounding up fractional bits - result[0][j] = Math.min((Math.min(result[1][j], result[2][j]) + 5) / 6 * 6 + bytesCost , result[0][j]); - result[1][j] = Math.min((Math.min(result[2][j], result[0][j]) + 5) / 6 * 6 + alphnumCost, result[1][j]); - result[2][j] = Math.min((Math.min(result[0][j], result[1][j]) + 5) / 6 * 6 + numberCost , result[2][j]); + result[0][j] = Math.min(roundUp6(Math.min(result[1][j], result[2][j])) + bytesCost , result[0][j]); + result[1][j] = Math.min(roundUp6(Math.min(result[2][j], result[0][j])) + alphnumCost, result[1][j]); + result[2][j] = Math.min(roundUp6(Math.min(result[0][j], result[1][j])) + numberCost , result[2][j]); } return result; } @@ -147,21 +147,21 @@ public final class QrSegmentAdvanced { if (curMode == NUMERIC) { if (isNumeric(c)) curMode = NUMERIC; - else if (isAlphanumeric(c) && (bitCosts[1][i] + 33 + 5) / 6 * 6 + numberCost == bitCosts[2][i + 1]) + else if (isAlphanumeric(c) && roundUp6(bitCosts[1][i] + 33) + numberCost == bitCosts[2][i + 1]) curMode = ALPHANUMERIC; else curMode = BYTE; } else if (curMode == ALPHANUMERIC) { - if (isNumeric(c) && (bitCosts[2][i] + 20 + 5) / 6 * 6 + alphnumCost == bitCosts[1][i + 1]) + if (isNumeric(c) && roundUp6(bitCosts[2][i] + 20) + alphnumCost == bitCosts[1][i + 1]) curMode = NUMERIC; else if (isAlphanumeric(c)) curMode = ALPHANUMERIC; else curMode = BYTE; } else if (curMode == BYTE) { - if (isNumeric(c) && (bitCosts[2][i] + 20 + 5) / 6 * 6 + bytesCost == bitCosts[0][i + 1]) + if (isNumeric(c) && roundUp6(bitCosts[2][i] + 20) + bytesCost == bitCosts[0][i + 1]) curMode = NUMERIC; - else if (isAlphanumeric(c) && (bitCosts[1][i] + 33 + 5) / 6 * 6 + bytesCost == bitCosts[0][i + 1]) + else if (isAlphanumeric(c) && roundUp6(bitCosts[1][i] + 33) + bytesCost == bitCosts[0][i + 1]) curMode = ALPHANUMERIC; else curMode = BYTE; @@ -224,6 +224,11 @@ public final class QrSegmentAdvanced { } + private static int roundUp6(int x) { + return (x + 5) / 6 * 6; + } + + /*---- Kanji mode segment encoder ----*/