Wrote a Matrix class and fixed all uses of it, as well as other small fixes like StringBuffer. MaskUtil and QRCode now compile with no errors.

git-svn-id: https://zxing.googlecode.com/svn/trunk@697 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
dswitkin 2008-11-13 21:18:56 +00:00
parent 8666ac2f8f
commit 6f4898c68f
5 changed files with 229 additions and 174 deletions

View file

@ -325,8 +325,7 @@ public final class Encoder {
&final_bits); &final_bits);
// Step 7: Choose the mask pattern and set to "qr_code". // Step 7: Choose the mask pattern and set to "qr_code".
QRCodeMatrix* matrix = new QRCodeMatrix(qr_code.matrix_width(), Matrix matrix = new Matrix(qr_code.matrix_width(), qr_code.matrix_width());
qr_code.matrix_width());
qr_code.set_mask_pattern(ChooseMaskPattern(final_bits, qr_code.set_mask_pattern(ChooseMaskPattern(final_bits,
qr_code.ec_level(), qr_code.ec_level(),
qr_code.version(), qr_code.version(),
@ -398,13 +397,13 @@ public final class Encoder {
} }
private static int ChooseMaskPattern(final BitVector &bits, int ec_level, int version, private static int ChooseMaskPattern(final BitVector &bits, int ec_level, int version,
QRCodeMatrix *matrix) { Matrix matrix) {
if (!QRCode.IsValidMatrixWidth(matrix.width())) { if (!QRCode.IsValidMatrixWidth(matrix.width())) {
Debug.LOG_ERROR("Invalid matrix width: " + matrix.width()); Debug.LOG_ERROR("Invalid matrix width: " + matrix.width());
return -1; return -1;
} }
int min_penalty = INT_MAX; // Lower penalty is better. int min_penalty = Integer.MAX_VALUE; // Lower penalty is better.
int best_mask_pattern = -1; int best_mask_pattern = -1;
// We try all mask patterns to choose the best one. // We try all mask patterns to choose the best one.
for (int i = 0; i < kNumMaskPatterns; ++i) { for (int i = 0; i < kNumMaskPatterns; ++i) {
@ -413,7 +412,7 @@ public final class Encoder {
mask_pattern, matrix)) { mask_pattern, matrix)) {
return -1; return -1;
} }
final int penalty = MaskUtil.CalculateMaskPenalty(*matrix); final int penalty = MaskUtil.CalculateMaskPenalty(matrix);
Debug.LOG_INFO("mask_pattern: " + mask_pattern + ", " + "penalty: " + penalty); Debug.LOG_INFO("mask_pattern: " + mask_pattern + ", " + "penalty: " + penalty);
if (penalty < min_penalty) { if (penalty < min_penalty) {
min_penalty = penalty; min_penalty = penalty;

View file

@ -16,8 +16,6 @@
package com.google.zxing.qrcode.encoder; package com.google.zxing.qrcode.encoder;
// #include "util/array/array2d-inl.h"
/** /**
* @author satorux@google.com (Satoru Takabayashi) - creator * @author satorux@google.com (Satoru Takabayashi) - creator
* @author dswitkin@google.com (Daniel Switkin) - ported from C++ * @author dswitkin@google.com (Daniel Switkin) - ported from C++
@ -27,7 +25,7 @@ public final class MaskUtil {
// The mask penalty calculation is complicated. See Table 21 of // The mask penalty calculation is complicated. See Table 21 of
// JISX0510:2004 (p.45) for details. Basically it applies four // JISX0510:2004 (p.45) for details. Basically it applies four
// rules and summate all penalties. // rules and summate all penalties.
public static int CalculateMaskPenalty(final QRCodeMatrix &matrix) { public static int CalculateMaskPenalty(final Matrix matrix) {
int penalty = 0; int penalty = 0;
penalty += ApplyMaskPenaltyRule1(matrix); penalty += ApplyMaskPenaltyRule1(matrix);
penalty += ApplyMaskPenaltyRule2(matrix); penalty += ApplyMaskPenaltyRule2(matrix);
@ -39,7 +37,7 @@ public final class MaskUtil {
// Apply mask penalty rule 1 and return the penalty. // Apply mask penalty rule 1 and return the penalty.
// Find repetitive cells with the same color and give penalty to // Find repetitive cells with the same color and give penalty to
// them. Example: 00000 or 11111. // them. Example: 00000 or 11111.
public static int ApplyMaskPenaltyRule1(final QRCodeMatrix &matrix) { public static int ApplyMaskPenaltyRule1(final Matrix matrix) {
final int penalty = (ApplyMaskPenaltyRule1Internal(matrix, true) + final int penalty = (ApplyMaskPenaltyRule1Internal(matrix, true) +
ApplyMaskPenaltyRule1Internal(matrix, false)); ApplyMaskPenaltyRule1Internal(matrix, false));
Debug.LOG_INFO("\tApplyMaskPenaltyRule1: " + penalty); Debug.LOG_INFO("\tApplyMaskPenaltyRule1: " + penalty);
@ -48,13 +46,16 @@ public final class MaskUtil {
// Apply mask penalty rule 2 and return the penalty. // Apply mask penalty rule 2 and return the penalty.
// Find 2x2 blocks with the same color and give penalty to them. // Find 2x2 blocks with the same color and give penalty to them.
public static int ApplyMaskPenaltyRule2(final QRCodeMatrix &matrix) { //
// JAVAPORT: Consider using Matrix.getArray() instead.
public static int ApplyMaskPenaltyRule2(final Matrix matrix) {
int penalty = 0; int penalty = 0;
for (int y = 0; y < matrix.height() - 1; ++y) { for (int y = 0; y < matrix.height() - 1; ++y) {
for (int x = 0; x < matrix.width() - 1; ++x) { for (int x = 0; x < matrix.width() - 1; ++x) {
if (matrix(y, x) == matrix(y + 0, x + 1) && int value = matrix.get(y, x);
matrix(y, x) == matrix(y + 1, x + 0) && if (value == matrix.get(y + 0, x + 1) &&
matrix(y, x) == matrix(y + 1, x + 1)) { value == matrix.get(y + 1, x + 0) &&
value == matrix.get(y + 1, x + 1)) {
penalty += 3; penalty += 3;
} }
} }
@ -67,49 +68,52 @@ public final class MaskUtil {
// Find consecutive cells of 00001011101 or 10111010000, and give // Find consecutive cells of 00001011101 or 10111010000, and give
// penalty to them. If we find patterns like 000010111010000, we give // penalty to them. If we find patterns like 000010111010000, we give
// penalties twice (i.e. 40 * 2). // penalties twice (i.e. 40 * 2).
public static int ApplyMaskPenaltyRule3(final QRCodeMatrix &matrix) { //
// JAVAPORT: This many calls to Matrix.get() looks expensive. We should profile and consider
// adding a byte[][] Matrix.getArray() method, then using that array locally.
public static int ApplyMaskPenaltyRule3(final Matrix matrix) {
int penalty = 0; int penalty = 0;
for (int y = 0; y < matrix.height(); ++y) { for (int y = 0; y < matrix.height(); ++y) {
for (int x = 0; x < matrix.width(); ++x) { for (int x = 0; x < matrix.width(); ++x) {
// Tried to simplify following conditions but failed. // Tried to simplify following conditions but failed.
if (x + 6 < matrix.width() && if (x + 6 < matrix.width() &&
matrix(y, x + 0) == 1 && matrix.get(y, x + 0) == 1 &&
matrix(y, x + 1) == 0 && matrix.get(y, x + 1) == 0 &&
matrix(y, x + 2) == 1 && matrix.get(y, x + 2) == 1 &&
matrix(y, x + 3) == 1 && matrix.get(y, x + 3) == 1 &&
matrix(y, x + 4) == 1 && matrix.get(y, x + 4) == 1 &&
matrix(y, x + 5) == 0 && matrix.get(y, x + 5) == 0 &&
matrix(y, x + 6) == 1 && matrix.get(y, x + 6) == 1 &&
((x + 10 < matrix.width() && ((x + 10 < matrix.width() &&
matrix(y, x + 7) == 0 && matrix.get(y, x + 7) == 0 &&
matrix(y, x + 8) == 0 && matrix.get(y, x + 8) == 0 &&
matrix(y, x + 9) == 0 && matrix.get(y, x + 9) == 0 &&
matrix(y, x + 10) == 0) || matrix.get(y, x + 10) == 0) ||
(x - 4 >= 0 && (x - 4 >= 0 &&
matrix(y, x - 1) == 0 && matrix.get(y, x - 1) == 0 &&
matrix(y, x - 2) == 0 && matrix.get(y, x - 2) == 0 &&
matrix(y, x - 3) == 0 && matrix.get(y, x - 3) == 0 &&
matrix(y, x - 4) == 0))) { matrix.get(y, x - 4) == 0))) {
penalty += 40; penalty += 40;
} }
if (y + 6 < matrix.height() && if (y + 6 < matrix.height() &&
matrix(y + 0, x) == 1 && matrix.get(y + 0, x) == 1 &&
matrix(y + 1, x) == 0 && matrix.get(y + 1, x) == 0 &&
matrix(y + 2, x) == 1 && matrix.get(y + 2, x) == 1 &&
matrix(y + 3, x) == 1 && matrix.get(y + 3, x) == 1 &&
matrix(y + 4, x) == 1 && matrix.get(y + 4, x) == 1 &&
matrix(y + 5, x) == 0 && matrix.get(y + 5, x) == 0 &&
matrix(y + 6, x) == 1 && matrix.get(y + 6, x) == 1 &&
((y + 10 < matrix.height() && ((y + 10 < matrix.height() &&
matrix(y + 7, x) == 0 && matrix.get(y + 7, x) == 0 &&
matrix(y + 8, x) == 0 && matrix.get(y + 8, x) == 0 &&
matrix(y + 9, x) == 0 && matrix.get(y + 9, x) == 0 &&
matrix(y + 10, x) == 0) || matrix.get(y + 10, x) == 0) ||
(y - 4 >= 0 && (y - 4 >= 0 &&
matrix(y - 1, x) == 0 && matrix.get(y - 1, x) == 0 &&
matrix(y - 2, x) == 0 && matrix.get(y - 2, x) == 0 &&
matrix(y - 3, x) == 0 && matrix.get(y - 3, x) == 0 &&
matrix(y - 4, x) == 0))) { matrix.get(y - 4, x) == 0))) {
penalty += 40; penalty += 40;
} }
} }
@ -129,44 +133,43 @@ public final class MaskUtil {
// - 55% => 10 // - 55% => 10
// - 55% => 20 // - 55% => 20
// - 100% => 100 // - 100% => 100
public static int ApplyMaskPenaltyRule4(final QRCodeMatrix &matrix) { public static int ApplyMaskPenaltyRule4(final Matrix matrix) {
int num_dark_cells = 0; int num_dark_cells = 0;
for (int y = 0; y < matrix.height(); ++y) { for (int y = 0; y < matrix.height(); ++y) {
for (int x = 0; x < matrix.width(); ++x) { for (int x = 0; x < matrix.width(); ++x) {
if (matrix(y, x) == 1) { if (matrix.get(y, x) == 1) {
num_dark_cells += 1; num_dark_cells += 1;
} }
} }
} }
final int num_total_cells = matrix.height() * matrix.width(); final int num_total_cells = matrix.height() * matrix.width();
double dark_ratio = static_cast<double>(num_dark_cells) / num_total_cells; double dark_ratio = (double) num_dark_cells / num_total_cells;
final int penalty = abs(static_cast<int>(dark_ratio * 100 - 50)) / 5 * 10; final int penalty = Math.abs((int) (dark_ratio * 100 - 50)) / 5 * 10;
Debug.LOG_INFO("\tApplyMaskPenaltyRule4: " + penalty); Debug.LOG_INFO("\tApplyMaskPenaltyRule4: " + penalty);
return penalty; return penalty;
} }
// Return the mask bit for "mask_pattern" at "x" and "y". // Return the mask bit for "mask_pattern" at "x" and "y".
// See 8.8 of JISX0510:2004 for mask pattern conditions. // See 8.8 of JISX0510:2004 for mask pattern conditions.
public static int GetDataMaskBit(final int mask_pattern, public static int GetDataMaskBit(final int mask_pattern, final int x, final int y) {
final int x, final int y) {
Debug.DCHECK(QRCode.IsValidMaskPattern(mask_pattern)); Debug.DCHECK(QRCode.IsValidMaskPattern(mask_pattern));
switch (mask_pattern) { switch (mask_pattern) {
case 0: case 0:
return (y + x) % 2 == 0; return ((y + x) % 2 == 0) ? 1 : 0;
case 1: case 1:
return y % 2 == 0; return (y % 2 == 0) ? 1 : 0;
case 2: case 2:
return x % 3 == 0; return (x % 3 == 0) ? 1 : 0;
case 3: case 3:
return (y + x) % 3 == 0; return ((y + x) % 3 == 0) ? 1 : 0;
case 4: case 4:
return ((y / 2) + (x / 3)) % 2 == 0; return (((y / 2) + (x / 3)) % 2 == 0) ? 1 : 0;
case 5: case 5:
return ((y * x) % 2) + ((y * x) % 3) == 0; return (((y * x) % 2) + ((y * x) % 3) == 0) ? 1 : 0;
case 6: case 6:
return (((y * x) % 2) + ((y * x) % 3)) % 2 == 0; return ((((y * x) % 2) + ((y * x) % 3)) % 2 == 0) ? 1 : 0;
case 7: case 7:
return (((y * x) % 3) + ((y + x) % 2)) % 2 == 0; return ((((y * x) % 3) + ((y + x) % 2)) % 2 == 0) ? 1 : 0;
default: default:
; ;
} }
@ -177,24 +180,23 @@ public final class MaskUtil {
// Helper function for ApplyMaskPenaltyRule1. We need this for doing // Helper function for ApplyMaskPenaltyRule1. We need this for doing
// this calculation in both vertical and horizontal orders // this calculation in both vertical and horizontal orders
// respectively. // respectively.
private static int ApplyMaskPenaltyRule1Internal(final QRCodeMatrix &matrix, private static int ApplyMaskPenaltyRule1Internal(final Matrix matrix, boolean is_horizontal) {
boolean is_horizontal) {
int penalty = 0; int penalty = 0;
int num_same_bit_cells = 0; int num_same_bit_cells = 0;
int prev_bit = -1; int prev_bit = -1;
// Horizontal mode: // Horizontal mode:
// for (int i = 0; i < matrix.height(); ++i) { // for (int i = 0; i < matrix.height(); ++i) {
// for (int j = 0; j < matrix.width(); ++j) { // for (int j = 0; j < matrix.width(); ++j) {
// int bit = matrix(i, j); // int bit = matrix.get(i, j);
// Vertical mode: // Vertical mode:
// for (int i = 0; i < matrix.width(); ++i) { // for (int i = 0; i < matrix.width(); ++i) {
// for (int j = 0; j < matrix.height(); ++j) { // for (int j = 0; j < matrix.height(); ++j) {
// int bit = matrix(j, i); // int bit = matrix.get(j, i);
final int i_limit = is_horizontal ? matrix.height() : matrix.width(); final int i_limit = is_horizontal ? matrix.height() : matrix.width();
final int j_limit = is_horizontal ? matrix.width() : matrix.height(); final int j_limit = is_horizontal ? matrix.width() : matrix.height();
for (int i = 0; i < i_limit; ++i) { for (int i = 0; i < i_limit; ++i) {
for (int j = 0; j < j_limit; ++j) { for (int j = 0; j < j_limit; ++j) {
final int bit = is_horizontal ? matrix(i, j) : matrix(j, i); final int bit = is_horizontal ? matrix.get(i, j) : matrix.get(j, i);
if (bit == prev_bit) { if (bit == prev_bit) {
num_same_bit_cells += 1; num_same_bit_cells += 1;
// Found five repetitive cells with the same color (bit). // Found five repetitive cells with the same color (bit).

View file

@ -0,0 +1,61 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.qrcode.encoder;
/**
* A class which wraps a 2D array.
*
* JAVAPORT: I'm not happy about the argument ordering throughout the file, as I always like to have
* the horizontal component first, but this is for compatibility with the C++ code. The original
* code was a 2D array of ints, but since it only ever gets assigned zeros and ones, I'm going to
* use less memory and go with bytes.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
public final class Matrix {
private final byte[][] bytes;
private final int height;
private final int width;
public Matrix(int height, int width) {
bytes = new byte[height][width];
this.height = height;
this.width = width;
}
public final int height() {
return height;
}
public final int width() {
return width;
}
public final byte get(int y, int x) {
return bytes[y][x];
}
public final void set(int y, int x, byte value) {
bytes[y][x] = value;
}
public final void set(int y, int x, int value) {
bytes[y][x] = (byte) value;
}
}

View file

@ -123,20 +123,20 @@ public final class MatrixUtil {
// Set all cells to -1. -1 means that the cell is empty (not set // Set all cells to -1. -1 means that the cell is empty (not set
// yet). // yet).
public static void ClearMatrix(QRCodeMatrix *matrix) { public static void ClearMatrix(Matrix matrix) {
for (int y = 0; y < matrix.height(); ++y) { for (int y = 0; y < matrix.height(); ++y) {
for (int x = 0; x < matrix.width(); ++x) { for (int x = 0; x < matrix.width(); ++x) {
(*matrix)(y, x) = -1; matrix.set(y, x, -1);
} }
} }
} }
// Convert "matrix" to ASCII String for debugging. // Convert "matrix" to ASCII String for debugging.
public static String ToASCII(final QRCodeMatrix &matrix) { public static String ToASCII(final Matrix matrix) {
String result; StringBuffer result = new StringBuffer();
for (int y = 0; y < matrix.height(); ++y) { for (int y = 0; y < matrix.height(); ++y) {
for (int x = 0; x < matrix.width(); ++x) { for (int x = 0; x < matrix.width(); ++x) {
switch (matrix(y, x)) { switch (matrix.get(y, x)) {
case 0: case 0:
result.append(" 0"); result.append(" 0");
break; break;
@ -150,7 +150,7 @@ public final class MatrixUtil {
} }
result.append("\n"); result.append("\n");
} }
return result; return result.toString();
} }
// Build 2D matrix of QR Code from "data_bits" with "ec_level", // Build 2D matrix of QR Code from "data_bits" with "ec_level",
@ -160,7 +160,7 @@ public final class MatrixUtil {
int ec_level, int ec_level,
int version, int version,
int mask_pattern, int mask_pattern,
QRCodeMatrix *matrix) { Matrix matrix) {
MatrixUtil.ClearMatrix(matrix); MatrixUtil.ClearMatrix(matrix);
if (!EmbedBasicPatterns(version, matrix)) { if (!EmbedBasicPatterns(version, matrix)) {
return false; return false;
@ -183,8 +183,7 @@ public final class MatrixUtil {
// - Timing patterns // - Timing patterns
// - Dark dot at the left bottom corner // - Dark dot at the left bottom corner
// - Position adjustment patterns, if need be // - Position adjustment patterns, if need be
public static boolean EmbedBasicPatterns(int version, public static boolean EmbedBasicPatterns(int version, Matrix matrix) {
QRCodeMatrix *matrix) {
// Let's get started with embedding big squares at corners. // Let's get started with embedding big squares at corners.
EmbedPositionDetectionPatternsAndSeparators(matrix); EmbedPositionDetectionPatternsAndSeparators(matrix);
// Then, embed the dark dot at the left bottom corner. // Then, embed the dark dot at the left bottom corner.
@ -199,7 +198,7 @@ public final class MatrixUtil {
// Embed type information. On success, modify the matrix and return // Embed type information. On success, modify the matrix and return
// true. On error, return false. // true. On error, return false.
public static boolean EmbedTypeInfo(int ec_level, int mask_pattern, QRCodeMatrix *matrix) { public static boolean EmbedTypeInfo(int ec_level, int mask_pattern, Matrix matrix) {
BitVector type_info_bits; BitVector type_info_bits;
if (!MakeTypeInfoBits(ec_level, mask_pattern, &type_info_bits)) { if (!MakeTypeInfoBits(ec_level, mask_pattern, &type_info_bits)) {
return false; return false;
@ -215,18 +214,18 @@ public final class MatrixUtil {
// See 8.9 of JISX0510:2004 (p.46). // See 8.9 of JISX0510:2004 (p.46).
final int x1 = kTypeInfoCoordinates[i][0]; final int x1 = kTypeInfoCoordinates[i][0];
final int y1 = kTypeInfoCoordinates[i][1]; final int y1 = kTypeInfoCoordinates[i][1];
(*matrix)(y1, x1) = bit; matrix.set(y1, x1, bit);
if (i < 8) { if (i < 8) {
// Right top corner. // Right top corner.
final int x2 = matrix.width() - i - 1; final int x2 = matrix.width() - i - 1;
final int y2 = 8; final int y2 = 8;
(*matrix)(y2, x2) = bit; matrix.set(y2, x2, bit);
} else { } else {
// Left bottom corner. // Left bottom corner.
final int x2 = 8; final int x2 = 8;
final int y2 = matrix.height() - 7 + (i - 8); final int y2 = matrix.height() - 7 + (i - 8);
(*matrix)(y2, x2) = bit; matrix.set(y2, x2, bit);
} }
} }
return true; return true;
@ -236,8 +235,7 @@ public final class MatrixUtil {
// matrix and return true. On error, return false. // matrix and return true. On error, return false.
// See 8.10 of JISX0510:2004 (p.47) for how to embed version // See 8.10 of JISX0510:2004 (p.47) for how to embed version
// information. Return true on success. Return false otherwise. // information. Return true on success. Return false otherwise.
public static boolean MaybeEmbedVersionInfo(int version, public static boolean MaybeEmbedVersionInfo(int version, Matrix matrix) {
QRCodeMatrix *matrix) {
if (version < 7) { // Version info is necessary if version >= 7. if (version < 7) { // Version info is necessary if version >= 7.
return true; // Don't need version info. return true; // Don't need version info.
} }
@ -253,9 +251,9 @@ public final class MatrixUtil {
// Place bits in LSB (least significant bit) to MSB order. // Place bits in LSB (least significant bit) to MSB order.
final int bit = version_info_bits.at(bit_index--); final int bit = version_info_bits.at(bit_index--);
// Left bottom corner. // Left bottom corner.
(*matrix)(matrix.height() - 11 + j, i) = bit; matrix.set(matrix.height() - 11 + j, i, bit);
// Right bottom corner. // Right bottom corner.
(*matrix)(i, matrix.height() - 11 + j) = bit; matrix.set(i, matrix.height() - 11 + j, bit);
} }
} }
return true; return true;
@ -265,9 +263,7 @@ public final class MatrixUtil {
// matrix and return true. On error, return false. For debugging // matrix and return true. On error, return false. For debugging
// purpose, it skips masking process if "mask_pattern" is -1. // purpose, it skips masking process if "mask_pattern" is -1.
// See 8.7 of JISX0510:2004 (p.38) for how to embed data bits. // See 8.7 of JISX0510:2004 (p.38) for how to embed data bits.
public static boolean EmbedDataBits(final BitVector &data_bits, public static boolean EmbedDataBits(final BitVector &data_bits, int mask_pattern, Matrix matrix) {
int mask_pattern,
QRCodeMatrix *matrix) {
int bit_index = 0; int bit_index = 0;
int direction = -1; int direction = -1;
// Start from the right bottom cell. // Start from the right bottom cell.
@ -282,7 +278,7 @@ public final class MatrixUtil {
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
final int xx = x - i; final int xx = x - i;
// Skip the cell if it's not empty. // Skip the cell if it's not empty.
if (!IsEmpty((*matrix)(y, xx))) { if (!IsEmpty(matrix.get(y, xx))) {
continue; continue;
} }
int bit = -1; int bit = -1;
@ -303,7 +299,7 @@ public final class MatrixUtil {
Debug.DCHECK(mask == 0 || mask == 1); Debug.DCHECK(mask == 0 || mask == 1);
bit ^= mask; bit ^= mask;
} }
(*matrix)(y, xx) = bit; matrix.set(y, xx, bit);
} }
y += direction; y += direction;
} }
@ -434,94 +430,86 @@ public final class MatrixUtil {
value == 1); // Dark (black). value == 1); // Dark (black).
} }
private static void EmbedTimingPatterns(QRCodeMatrix *matrix) { private static void EmbedTimingPatterns(Matrix matrix) {
// -8 is for skipping position detection patterns (size 7), and // -8 is for skipping position detection patterns (size 7), and
// two horizontal/vertical separation patterns (size 1). // two horizontal/vertical separation patterns (size 1).
// Thus, 8 = 7 + 1. // Thus, 8 = 7 + 1.
for (int i = 8; i < matrix.width() - 8; ++i) { for (int i = 8; i < matrix.width() - 8; ++i) {
final int bit = (i + 1) % 2; final int bit = (i + 1) % 2;
// Horizontal line. // Horizontal line.
Debug.DCHECK(IsValidValue((*matrix)(6, i))); Debug.DCHECK(IsValidValue(matrix.get(6, i)));
if (IsEmpty((*matrix)(6, i))) { if (IsEmpty(matrix.get(6, i))) {
(*matrix)(6, i) = bit; matrix.set(6, i, bit);
} }
// Vertical line. // Vertical line.
Debug.DCHECK(IsValidValue((*matrix)(i, 6))); Debug.DCHECK(IsValidValue(matrix.get(i, 6)));
if (IsEmpty((*matrix)(i, 6))) { if (IsEmpty(matrix.get(i, 6))) {
(*matrix)(i, 6) = bit; matrix.set(i, 6, bit);
} }
} }
} }
// Embed the lonely dark dot at left bottom corner. // Embed the lonely dark dot at left bottom corner.
// JISX0510:2004 (p.46) // JISX0510:2004 (p.46)
private static void EmbedDarkDotAtLeftBottomCorner( private static void EmbedDarkDotAtLeftBottomCorner(Matrix matrix) {
QRCodeMatrix *matrix) { Debug.DCHECK(matrix.get(matrix.height() - 8, 8) != 0);
Debug.DCHECK((*matrix)(matrix.height() - 8, 8)); matrix.set(matrix.height() - 8, 8, 1);
(*matrix)(matrix.height() - 8, 8) = 1;
} }
private static void EmbedHorizontalSeparationPattern(final int x_start, private static void EmbedHorizontalSeparationPattern(final int x_start, final int y_start,
final int y_start, Matrix matrix) {
QRCodeMatrix *matrix) {
// We know the width and height. // We know the width and height.
Debug.DCHECK_EQ(8, arraysize(kHorizontalSeparationPattern[0])); Debug.DCHECK_EQ(8, arraysize(kHorizontalSeparationPattern[0]));
Debug.DCHECK_EQ(1, arraysize(kHorizontalSeparationPattern)); Debug.DCHECK_EQ(1, arraysize(kHorizontalSeparationPattern));
for (int x = 0; x < 8; ++x) { for (int x = 0; x < 8; ++x) {
Debug.DCHECK(IsEmpty((*matrix)(y_start, x_start + x))); Debug.DCHECK(IsEmpty(matrix.get(y_start, x_start + x)));
(*matrix)(y_start, x_start + x) = kHorizontalSeparationPattern[0][x]; matrix.set(y_start, x_start + x, kHorizontalSeparationPattern[0][x]);
} }
} }
private static void EmbedVerticalSeparationPattern(final int x_start, private static void EmbedVerticalSeparationPattern(final int x_start, final int y_start,
final int y_start, Matrix matrix) {
QRCodeMatrix *matrix) {
// We know the width and height. // We know the width and height.
Debug.DCHECK_EQ(1, arraysize(kVerticalSeparationPattern[0])); Debug.DCHECK_EQ(1, arraysize(kVerticalSeparationPattern[0]));
Debug.DCHECK_EQ(7, arraysize(kVerticalSeparationPattern)); Debug.DCHECK_EQ(7, arraysize(kVerticalSeparationPattern));
for (int y = 0; y < 7; ++y) { for (int y = 0; y < 7; ++y) {
Debug.DCHECK(IsEmpty((*matrix)(y_start + y, x_start))); Debug.DCHECK(IsEmpty(matrix.get(y_start + y, x_start)));
(*matrix)(y_start + y, x_start) = kVerticalSeparationPattern[y][0]; matrix.set(y_start + y, x_start, kVerticalSeparationPattern[y][0]);
} }
} }
// Note that we cannot unify the function with // Note that we cannot unify the function with EmbedPositionDetectionPattern() despite they are
// EmbedPositionDetectionPattern() despite they are almost // almost identical, since we cannot write a function that takes 2D arrays in different sizes in
// identical, since we cannot write a function that takes 2D arrays // C/C++. We should live with the fact.
// in different sizes in C/C++. We should live with the fact. private static void EmbedPositionAdjustmentPattern(final int x_start, final int y_start,
private static void EmbedPositionAdjustmentPattern(final int x_start, Matrix matrix) {
final int y_start,
QRCodeMatrix *matrix) {
// We know the width and height. // We know the width and height.
Debug.DCHECK_EQ(5, arraysize(kPositionAdjustmentPattern[0])); Debug.DCHECK_EQ(5, arraysize(kPositionAdjustmentPattern[0]));
Debug.DCHECK_EQ(5, arraysize(kPositionAdjustmentPattern)); Debug.DCHECK_EQ(5, arraysize(kPositionAdjustmentPattern));
for (int y = 0; y < 5; ++y) { for (int y = 0; y < 5; ++y) {
for (int x = 0; x < 5; ++x) { for (int x = 0; x < 5; ++x) {
Debug.DCHECK(IsEmpty((*matrix)(y_start + y, x_start + x))); Debug.DCHECK(IsEmpty(matrix.get(y_start + y, x_start + x)));
(*matrix)(y_start + y, x_start + x) = matrix.set(y_start + y, x_start + x, kPositionAdjustmentPattern[y][x]);
kPositionAdjustmentPattern[y][x];
} }
} }
} }
private static void EmbedPositionDetectionPattern(final int x_start, private static void EmbedPositionDetectionPattern(final int x_start, final int y_start,
final int y_start, Matrix matrix) {
QRCodeMatrix *matrix) {
// We know the width and height. // We know the width and height.
Debug.DCHECK_EQ(7, arraysize(kPositionDetectionPattern[0])); Debug.DCHECK_EQ(7, arraysize(kPositionDetectionPattern[0]));
Debug.DCHECK_EQ(7, arraysize(kPositionDetectionPattern)); Debug.DCHECK_EQ(7, arraysize(kPositionDetectionPattern));
for (int y = 0; y < 7; ++y) { for (int y = 0; y < 7; ++y) {
for (int x = 0; x < 7; ++x) { for (int x = 0; x < 7; ++x) {
Debug.DCHECK(IsEmpty((*matrix)(y_start + y, x_start + x))); Debug.DCHECK(IsEmpty(matrix.get(y_start + y, x_start + x)));
(*matrix)(y_start + y, x_start + x) = matrix.set(y_start + y, x_start + x, kPositionDetectionPattern[y][x]);
kPositionDetectionPattern[y][x];
} }
} }
} }
// Embed position detection patterns and surrounding // Embed position detection patterns and surrounding
// vertical/horizontal separators. // vertical/horizontal separators.
private static void EmbedPositionDetectionPatternsAndSeparators(QRCodeMatrix *matrix) { private static void EmbedPositionDetectionPatternsAndSeparators(Matrix matrix) {
// Embed three big squares at corners. // Embed three big squares at corners.
final int pdp_width = arraysize(kPositionDetectionPattern[0]); final int pdp_width = arraysize(kPositionDetectionPattern[0]);
// Left top corner. // Left top corner.
@ -553,8 +541,7 @@ public final class MatrixUtil {
} }
// Embed position adjustment patterns if need be. // Embed position adjustment patterns if need be.
private static void MaybeEmbedPositionAdjustmentPatterns(final int version, private static void MaybeEmbedPositionAdjustmentPatterns(final int version, Matrix matrix) {
QRCodeMatrix *matrix) {
if (version < 2) { // The patterns appear if version >= 2 if (version < 2) { // The patterns appear if version >= 2
return; return;
} }
@ -572,7 +559,7 @@ public final class MatrixUtil {
} }
// If the cell is unset, we embed the position adjustment // If the cell is unset, we embed the position adjustment
// pattern here. // pattern here.
if (IsEmpty((*matrix)(y, x))) { if (IsEmpty(matrix.get(y, x))) {
// -2 is necessary since the x/y coordinates point to the // -2 is necessary since the x/y coordinates point to the
// center of the pattern, not the left top corner. // center of the pattern, not the left top corner.
EmbedPositionAdjustmentPattern(x - 2, y - 2, matrix); EmbedPositionAdjustmentPattern(x - 2, y - 2, matrix);

View file

@ -16,12 +16,6 @@
package com.google.zxing.qrcode.encoder; package com.google.zxing.qrcode.encoder;
// JAVAPORT: QRCodeMatrix needs to be renamed Matrix and built as a new class.
//
// template <typename T> class Array2D;
// typedef Array2D<int> QRCodeMatrix;
// #include "util/array/array2d-inl.h"
/** /**
* @author satorux@google.com (Satoru Takabayashi) - creator * @author satorux@google.com (Satoru Takabayashi) - creator
* @author dswitkin@google.com (Daniel Switkin) - ported from C++ * @author dswitkin@google.com (Daniel Switkin) - ported from C++
@ -55,7 +49,7 @@ public final class QRCode {
private int num_data_bytes_; private int num_data_bytes_;
private int num_ec_bytes_; private int num_ec_bytes_;
private int num_rs_blocks_; private int num_rs_blocks_;
private QRCodeMatrix *matrix_; private Matrix matrix_;
// They call encoding "mode". The modes are defined in 8.3 of JISX0510:2004 (p.14). It's unlikely // They call encoding "mode". The modes are defined in 8.3 of JISX0510:2004 (p.14). It's unlikely
@ -125,7 +119,7 @@ public final class QRCode {
// Number of Reedsolomon blocks in the QR Code. // Number of Reedsolomon blocks in the QR Code.
public int num_rs_blocks() { return num_rs_blocks_; } public int num_rs_blocks() { return num_rs_blocks_; }
// Matrix data of the QR Code. // Matrix data of the QR Code.
public final QRCodeMatrix* matrix() { return matrix_; } public final Matrix matrix() { return matrix_; }
// Return the value of the module (cell) pointed by "x" and "y" in // Return the value of the module (cell) pointed by "x" and "y" in
// the matrix of the QR Code. They call cells in the matrix // the matrix of the QR Code. They call cells in the matrix
@ -143,12 +137,14 @@ public final class QRCode {
// //
public int at(int x, int y) { public int at(int x, int y) {
// The value must be zero or one. // The value must be zero or one.
Debug.DCHECK((*matrix_)(y, x) == 0 || (*matrix_)(y, x) == 1); int value = matrix_.get(y, x);
return (*matrix_)(y, x); Debug.DCHECK(value == 0 || value == 1);
return value;
} }
// Checks all the member variables are set properly. Returns true // Checks all the member variables are set properly. Returns true on success. Otherwise, returns
// on success. Otherwise, returns false. // false.
// JAVAPORT: Do not call EverythingIsBinary(matrix_) here as it is very expensive.
public boolean IsValid() { public boolean IsValid() {
return ( return (
// First check if all version are not uninitialized. // First check if all version are not uninitialized.
@ -173,31 +169,39 @@ public final class QRCode {
matrix_width_ == matrix_.width() && matrix_width_ == matrix_.width() &&
// See 7.3.1 of JISX0510:2004 (p.5). // See 7.3.1 of JISX0510:2004 (p.5).
matrix_width_ == kMinMatrixWidth + (version_ - 1) * 4 && matrix_width_ == kMinMatrixWidth + (version_ - 1) * 4 &&
matrix_.width() == matrix_.height() && // Must be square. matrix_.width() == matrix_.height()); // Must be square.
EverythingIsBinary(*matrix_));
} }
// Return debug String. // Return debug String.
public String DebugString() { public String DebugString() {
String result; StringBuffer result = new StringBuffer();
StringAppendF(&result, "<<QRCode\n"); result.append("<<QRCode\n");
StringAppendF(&result, " mode: %s\n", ModeToString(mode_)); result.append(" mode: ");
StringAppendF(&result, " ec_level: %s\n", ECLevelToString(ec_level_)); result.append(ModeToString(mode_));
StringAppendF(&result, " version: %d\n", version_); result.append("\n ec_level: ");
StringAppendF(&result, " matrix_width: %d\n", matrix_width_); result.append(ECLevelToString(ec_level_));
StringAppendF(&result, " mask_pattern: %d\n", mask_pattern_); result.append("\n version: ");
StringAppendF(&result, " num_total_bytes_: %d\n", num_total_bytes_); result.append(version_);
StringAppendF(&result, " num_data_bytes: %d\n", num_data_bytes_); result.append("\n matrix_width: ");
StringAppendF(&result, " num_ec_bytes: %d\n", num_ec_bytes_); result.append(matrix_width_);
StringAppendF(&result, " num_rs_blocks: %d\n", num_rs_blocks_); result.append("\n mask_pattern: ");
result.append(mask_pattern_);
result.append("\n num_total_bytes_: ");
result.append(num_total_bytes_);
result.append("\n num_data_bytes: ");
result.append(num_data_bytes_);
result.append("\n num_ec_bytes: ");
result.append(num_ec_bytes_);
result.append("\n num_rs_blocks: ");
result.append(num_rs_blocks_);
if (matrix_ == null) { if (matrix_ == null) {
StringAppendF(&result, " matrix: null\n"); result.append("\n matrix: null");
} else { } else {
StringAppendF(&result, " matrix:\n%s", result.append("\n matrix:");
MatrixUtil.ToASCII(*matrix_).c_str()); result.append(MatrixUtil.ToASCII(matrix_));
} }
StringAppendF(&result, ">>\n"); result.append("\n>>\n");
return result; return result.toString();
} }
public void set_mode(int value) { mode_ = value; } public void set_mode(int value) { mode_ = value; }
@ -211,7 +215,7 @@ public final class QRCode {
public void set_num_rs_blocks(int value) { num_rs_blocks_ = value; } public void set_num_rs_blocks(int value) { num_rs_blocks_ = value; }
// This takes ownership of the 2D array. The 2D array will be // This takes ownership of the 2D array. The 2D array will be
// deleted in the destructor of the class. // deleted in the destructor of the class.
public void set_matrix(QRCodeMatrix *value) { matrix_ = value; } public void set_matrix(Matrix value) { matrix_ = value; }
// Check if "version" is valid. // Check if "version" is valid.
@ -334,12 +338,14 @@ public final class QRCode {
return -1; return -1;
} }
// Return true if the all values in the matrix are binary numbers. // Return true if the all values in the matrix are binary numbers. Otherwise, return false.
// Otherwise, return false. // JAVAPORT: This is going to be super expensive and unnecessary, we should not call this in
private static boolean EverythingIsBinary(final Array2D<int> &matrix) { // production. I'm leaving it because it may be useful for testing.
private static boolean EverythingIsBinary(final Matrix matrix) {
for (int y = 0; y < matrix.height(); ++y) { for (int y = 0; y < matrix.height(); ++y) {
for (int x = 0; x < matrix.width(); ++x) { for (int x = 0; x < matrix.width(); ++x) {
if (!(matrix(y, x) == 0 || matrix(y, x) == 1)) { int value = matrix.get(y, x);
if (!(value == 0 || value == 1)) {
// Found non zero/one value. // Found non zero/one value.
return false; return false;
} }