Simplified and clarified small pieces of Java code, without changing behavior.

This commit is contained in:
Project Nayuki 2018-08-22 18:33:01 +00:00
parent 0e2ecff58e
commit bf62065700
4 changed files with 15 additions and 12 deletions

View File

@ -83,8 +83,10 @@ public final class BitBuffer implements Cloneable {
*/ */
public byte[] getBytes() { public byte[] getBytes() {
byte[] result = new byte[(bitLength + 7) >>> 3]; // Round up to whole byte, won't overflow byte[] result = new byte[(bitLength + 7) >>> 3]; // Round up to whole byte, won't overflow
for (int i = 0; i < bitLength; i++) for (int i = 0; i < bitLength; i++) {
result[i >>> 3] |= data.get(i) ? 1 << (7 - (i & 7)) : 0; if (data.get(i))
result[i >>> 3] |= 1 << (7 - (i & 7));
}
return result; return result;
} }

View File

@ -262,8 +262,8 @@ public final class QrCode {
BufferedImage result = new BufferedImage((size + border * 2) * scale, (size + border * 2) * scale, BufferedImage.TYPE_INT_RGB); BufferedImage result = new BufferedImage((size + border * 2) * scale, (size + border * 2) * scale, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < result.getHeight(); y++) { for (int y = 0; y < result.getHeight(); y++) {
for (int x = 0; x < result.getWidth(); x++) { for (int x = 0; x < result.getWidth(); x++) {
boolean val = getModule(x / scale - border, y / scale - border); boolean color = getModule(x / scale - border, y / scale - border);
result.setRGB(x, y, val ? 0x000000 : 0xFFFFFF); result.setRGB(x, y, color ? 0x000000 : 0xFFFFFF);
} }
} }
return result; return result;
@ -445,8 +445,8 @@ public final class QrCode {
ReedSolomonGenerator rs = new ReedSolomonGenerator(blockEccLen); ReedSolomonGenerator rs = new ReedSolomonGenerator(blockEccLen);
for (int i = 0, k = 0; i < numBlocks; i++) { for (int i = 0, k = 0; i < numBlocks; i++) {
byte[] dat = Arrays.copyOfRange(data, k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1)); byte[] dat = Arrays.copyOfRange(data, k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));
byte[] block = Arrays.copyOf(dat, shortBlockLen + 1);
k += dat.length; k += dat.length;
byte[] block = Arrays.copyOf(dat, shortBlockLen + 1);
byte[] ecc = rs.getRemainder(dat); byte[] ecc = rs.getRemainder(dat);
System.arraycopy(ecc, 0, block, block.length - blockEccLen, ecc.length); System.arraycopy(ecc, 0, block, block.length - blockEccLen, ecc.length);
blocks[i] = block; blocks[i] = block;
@ -668,7 +668,7 @@ public final class QrCode {
int size = ver * 4 + 17; int size = ver * 4 + 17;
int result = size * size; // Number of modules in the whole QR symbol square int result = size * size; // Number of modules in the whole QR symbol square
result -= 64 * 3; // Subtract the three finders with separators result -= 8 * 8 * 3; // Subtract the three finders with separators
result -= 15 * 2 + 1; // Subtract the format information and black module result -= 15 * 2 + 1; // Subtract the format information and black module
result -= (size - 16) * 2; // Subtract the timing patterns result -= (size - 16) * 2; // Subtract the timing patterns
// The five lines above are equivalent to: int result = (16 * ver + 128) * ver + 64; // The five lines above are equivalent to: int result = (16 * ver + 128) * ver + 64;

View File

@ -259,7 +259,7 @@ public final class QrSegment {
/*-- Constructor --*/ /*-- Constructor --*/
private Mode(int mode, int... ccbits) { private Mode(int mode, int... ccbits) {
this.modeBits = mode; modeBits = mode;
numBitsCharCount = ccbits; numBitsCharCount = ccbits;
} }

View File

@ -224,6 +224,7 @@ public final class QrSegmentAdvanced {
} }
/*---- Kanji mode segment encoder ----*/ /*---- Kanji mode segment encoder ----*/
/** /**
@ -384,17 +385,17 @@ public final class QrSegmentAdvanced {
"/////////////////////////////////////////////w=="; "/////////////////////////////////////////////w==";
private static short[] UNICODE_TO_QR_KANJI = new short[65536]; private static short[] UNICODE_TO_QR_KANJI = new short[1 << 16];
static { // Unpack the Shift JIS table into a more computation-friendly form static { // Unpack the Shift JIS table into a more computation-friendly form
Arrays.fill(UNICODE_TO_QR_KANJI, (short)-1); Arrays.fill(UNICODE_TO_QR_KANJI, (short)-1);
byte[] bytes = Base64.getDecoder().decode(PACKED_QR_KANJI_TO_UNICODE); byte[] bytes = Base64.getDecoder().decode(PACKED_QR_KANJI_TO_UNICODE);
for (int i = 0; i < bytes.length; i += 2) { for (int i = 0; i < bytes.length; i += 2) {
int j = ((bytes[i] & 0xFF) << 8) | (bytes[i + 1] & 0xFF); char c = (char)(((bytes[i] & 0xFF) << 8) | (bytes[i + 1] & 0xFF));
if (j == 0xFFFF) if (c == 0xFFFF)
continue; continue;
assert UNICODE_TO_QR_KANJI[j] == -1; assert UNICODE_TO_QR_KANJI[c] == -1;
UNICODE_TO_QR_KANJI[j] = (short)(i / 2); UNICODE_TO_QR_KANJI[c] = (short)(i / 2);
} }
} }