A few inspection cleanups

git-svn-id: https://zxing.googlecode.com/svn/trunk@2660 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen@gmail.com 2013-04-18 11:26:13 +00:00
parent cdd033e582
commit 96c8b2a8b6
8 changed files with 441 additions and 539 deletions

View file

@ -326,10 +326,9 @@ final class QRCodeEncoder {
hints = new EnumMap<EncodeHintType,Object>(EncodeHintType.class); hints = new EnumMap<EncodeHintType,Object>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding); hints.put(EncodeHintType.CHARACTER_SET, encoding);
} }
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result; BitMatrix result;
try { try {
result = writer.encode(contentsToEncode, format, dimension, dimension, hints); result = new MultiFormatWriter().encode(contentsToEncode, format, dimension, dimension, hints);
} catch (IllegalArgumentException iae) { } catch (IllegalArgumentException iae) {
// Unsupported format // Unsupported format
return null; return null;

View file

@ -34,6 +34,45 @@ public final class BenchmarkActivity extends Activity {
private TextView textView; private TextView textView;
private Thread benchmarkThread; private Thread benchmarkThread;
private final Button.OnClickListener runBenchmark = new Button.OnClickListener() {
@Override
public void onClick(View v) {
if (benchmarkThread == null) {
runBenchmarkButton.setEnabled(false);
textView.setText(R.string.benchmark_running);
benchmarkThread = new Thread(new BenchmarkThread(BenchmarkActivity.this, PATH));
benchmarkThread.start();
}
}
};
private final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
if (message.what == R.id.benchmark_done) {
Iterable<BenchmarkItem> items = (Iterable<BenchmarkItem>) message.obj;
int count = 0;
int time = 0;
for (BenchmarkItem item : items) {
if (item != null) {
Log.v(TAG, item.toString());
count++;
time += item.getAverageTime();
}
}
String totals = "TOTAL: Decoded " + count + " images in " + time + " us";
Log.v(TAG, totals);
textView.setText(totals + "\n\n" + getString(R.string.benchmark_help));
benchmarkThread = null;
runBenchmarkButton.setEnabled(true);
}
}
};
Handler getHandler() {
return handler;
}
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
@ -47,47 +86,4 @@ public final class BenchmarkActivity extends Activity {
benchmarkThread = null; benchmarkThread = null;
} }
private final Button.OnClickListener runBenchmark = new Button.OnClickListener() {
@Override
public void onClick(View v) {
if (benchmarkThread == null) {
runBenchmarkButton.setEnabled(false);
textView.setText(R.string.benchmark_running);
benchmarkThread = new Thread(new BenchmarkThread(BenchmarkActivity.this, PATH));
benchmarkThread.start();
}
}
};
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
switch (message.what) {
case R.id.benchmark_done:
handleBenchmarkDone(message);
benchmarkThread = null;
runBenchmarkButton.setEnabled(true);
break;
default:
break;
}
}
};
private void handleBenchmarkDone(Message message) {
Iterable<BenchmarkItem> items = (Iterable<BenchmarkItem>) message.obj;
int count = 0;
int time = 0;
for (BenchmarkItem item : items) {
if (item != null) {
Log.v(TAG, item.toString());
count++;
time += item.getAverageTime();
}
}
String totals = "TOTAL: Decoded " + count + " images in " + time + " us";
Log.v(TAG, totals);
textView.setText(totals + "\n\n" + getString(R.string.benchmark_help));
}
} }

View file

@ -57,7 +57,7 @@ final class BenchmarkThread implements Runnable {
List<BenchmarkItem> items = new ArrayList<BenchmarkItem>(); List<BenchmarkItem> items = new ArrayList<BenchmarkItem>();
walkTree(path, items); walkTree(path, items);
Message message = Message.obtain(activity.handler, R.id.benchmark_done); Message message = Message.obtain(activity.getHandler(), R.id.benchmark_done);
message.obj = items; message.obj = items;
message.sendToTarget(); message.sendToTarget();
} }

View file

@ -164,26 +164,26 @@ public final class Detector {
float ratio = (2 * nbLayers + (nbLayers > 4 ? 1 : 0) + (nbLayers - 4) / 8) float ratio = (2 * nbLayers + (nbLayers > 4 ? 1 : 0) + (nbLayers - 4) / 8)
/ (2.0f * nbCenterLayers); / (2.0f * nbCenterLayers);
int dx = bullEyeCornerPoints[0].x-bullEyeCornerPoints[2].x; int dx = bullEyeCornerPoints[0].getX() - bullEyeCornerPoints[2].getX();
dx+=dx>0?1:-1; dx+=dx>0?1:-1;
int dy = bullEyeCornerPoints[0].y-bullEyeCornerPoints[2].y; int dy = bullEyeCornerPoints[0].getY() - bullEyeCornerPoints[2].getY();
dy+=dy>0?1:-1; dy+=dy>0?1:-1;
int targetcx = MathUtils.round(bullEyeCornerPoints[2].x - ratio * dx); int targetcx = MathUtils.round(bullEyeCornerPoints[2].getX() - ratio * dx);
int targetcy = MathUtils.round(bullEyeCornerPoints[2].y - ratio * dy); int targetcy = MathUtils.round(bullEyeCornerPoints[2].getY() - ratio * dy);
int targetax = MathUtils.round(bullEyeCornerPoints[0].x + ratio * dx); int targetax = MathUtils.round(bullEyeCornerPoints[0].getX() + ratio * dx);
int targetay = MathUtils.round(bullEyeCornerPoints[0].y + ratio * dy); int targetay = MathUtils.round(bullEyeCornerPoints[0].getY() + ratio * dy);
dx = bullEyeCornerPoints[1].x-bullEyeCornerPoints[3].x; dx = bullEyeCornerPoints[1].getX() - bullEyeCornerPoints[3].getX();
dx+=dx>0?1:-1; dx+=dx>0?1:-1;
dy = bullEyeCornerPoints[1].y-bullEyeCornerPoints[3].y; dy = bullEyeCornerPoints[1].getY() - bullEyeCornerPoints[3].getY();
dy+=dy>0?1:-1; dy+=dy>0?1:-1;
int targetdx = MathUtils.round(bullEyeCornerPoints[3].x - ratio * dx); int targetdx = MathUtils.round(bullEyeCornerPoints[3].getX() - ratio * dx);
int targetdy = MathUtils.round(bullEyeCornerPoints[3].y - ratio * dy); int targetdy = MathUtils.round(bullEyeCornerPoints[3].getY() - ratio * dy);
int targetbx = MathUtils.round(bullEyeCornerPoints[1].x + ratio * dx); int targetbx = MathUtils.round(bullEyeCornerPoints[1].getX() + ratio * dx);
int targetby = MathUtils.round(bullEyeCornerPoints[1].y+ratio*dy); int targetby = MathUtils.round(bullEyeCornerPoints[1].getY() +ratio*dy);
if (!isValid(targetax, targetay) || if (!isValid(targetax, targetay) ||
!isValid(targetbx, targetby) || !isValid(targetbx, targetby) ||
@ -299,20 +299,20 @@ public final class Detector {
float ratio = 0.75f*2/(2*nbCenterLayers-3); float ratio = 0.75f*2/(2*nbCenterLayers-3);
int dx = pina.x-pinc.x; int dx = pina.getX() - pinc.getX();
int dy = pina.y-pinc.y; int dy = pina.getY() - pinc.getY();
int targetcx = MathUtils.round(pinc.x-ratio*dx); int targetcx = MathUtils.round(pinc.getX() -ratio*dx);
int targetcy = MathUtils.round(pinc.y-ratio*dy); int targetcy = MathUtils.round(pinc.getY() -ratio*dy);
int targetax = MathUtils.round(pina.x+ratio*dx); int targetax = MathUtils.round(pina.getX() +ratio*dx);
int targetay = MathUtils.round(pina.y+ratio*dy); int targetay = MathUtils.round(pina.getY() +ratio*dy);
dx = pinb.x-pind.x; dx = pinb.getX() - pind.getX();
dy = pinb.y-pind.y; dy = pinb.getY() - pind.getY();
int targetdx = MathUtils.round(pind.x-ratio*dx); int targetdx = MathUtils.round(pind.getX() -ratio*dx);
int targetdy = MathUtils.round(pind.y-ratio*dy); int targetdy = MathUtils.round(pind.getY() -ratio*dy);
int targetbx = MathUtils.round(pinb.x+ratio*dx); int targetbx = MathUtils.round(pinb.getX() +ratio*dx);
int targetby = MathUtils.round(pinb.y+ratio*dy); int targetby = MathUtils.round(pinb.getY() +ratio*dy);
if (!isValid(targetax, targetay) || if (!isValid(targetax, targetay) ||
!isValid(targetbx, targetby) || !isValid(targetbx, targetby) ||
@ -484,11 +484,11 @@ public final class Detector {
boolean[] res = new boolean[size]; boolean[] res = new boolean[size];
float d = distance(p1,p2); float d = distance(p1,p2);
float moduleSize = d/(size-1); float moduleSize = d/(size-1);
float dx = moduleSize*(p2.x - p1.x)/d; float dx = moduleSize*(p2.getX() - p1.getX())/d;
float dy = moduleSize*(p2.y - p1.y)/d; float dy = moduleSize*(p2.getY() - p1.getY())/d;
float px = p1.x; float px = p1.getX();
float py = p1.y; float py = p1.getY();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
res[i] = image.get(MathUtils.round(px), MathUtils.round(py)); res[i] = image.get(MathUtils.round(px), MathUtils.round(py));
@ -510,10 +510,10 @@ public final class Detector {
int corr = 3; int corr = 3;
p1 = new Point(p1.x-corr, p1.y+corr); p1 = new Point(p1.getX() -corr, p1.getY() +corr);
p2 = new Point(p2.x-corr, p2.y-corr); p2 = new Point(p2.getX() -corr, p2.getY() -corr);
p3 = new Point(p3.x+corr, p3.y-corr); p3 = new Point(p3.getX() +corr, p3.getY() -corr);
p4 = new Point(p4.x+corr, p4.y+corr); p4 = new Point(p4.getX() +corr, p4.getY() +corr);
int cInit = getColor(p4, p1); int cInit = getColor(p4, p1);
@ -546,14 +546,14 @@ public final class Detector {
*/ */
private int getColor(Point p1, Point p2) { private int getColor(Point p1, Point p2) {
float d = distance(p1,p2); float d = distance(p1,p2);
float dx = (p2.x - p1.x)/d; float dx = (p2.getX() - p1.getX())/d;
float dy = (p2.y - p1.y)/d; float dy = (p2.getY() - p1.getY())/d;
int error = 0; int error = 0;
float px = p1.x; float px = p1.getX();
float py = p1.y; float py = p1.getY();
boolean colorModel = image.get(p1.x, p1.y); boolean colorModel = image.get(p1.getX(), p1.getY());
for (int i = 0; i < d; i++) { for (int i = 0; i < d; i++) {
px+=dx; px+=dx;
@ -576,8 +576,8 @@ public final class Detector {
* Gets the coordinate of the first point with a different color in the given direction * Gets the coordinate of the first point with a different color in the given direction
*/ */
private Point getFirstDifferent(Point init, boolean color, int dx, int dy) { private Point getFirstDifferent(Point init, boolean color, int dx, int dy) {
int x = init.x+dx; int x = init.getX() +dx;
int y = init.y+dy; int y = init.getY() +dy;
while(isValid(x,y) && image.get(x,y) == color) { while(isValid(x,y) && image.get(x,y) == color) {
x+=dx; x+=dx;
@ -605,21 +605,29 @@ public final class Detector {
} }
private static float distance(Point a, Point b) { private static float distance(Point a, Point b) {
return MathUtils.distance(a.x, a.y, b.x, b.y); return MathUtils.distance(a.getX(), a.getY(), b.getX(), b.getY());
} }
static final class Point { static final class Point {
final int x; private final int x;
final int y; private final int y;
ResultPoint toResultPoint() { ResultPoint toResultPoint() {
return new ResultPoint(x, y); return new ResultPoint(getX(), getY());
} }
Point(int x, int y) { Point(int x, int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
int getX() {
return x;
}
int getY() {
return y;
}
} }
} }

View file

@ -524,9 +524,7 @@ public final class LinesSampler {
return rowCount; return rowCount;
} }
private static final class VoteResult {
private static class VoteResult {
private boolean indecisive; private boolean indecisive;
private int vote; private int vote;
boolean isIndecisive() { boolean isIndecisive() {

View file

@ -84,24 +84,18 @@ public final class BitVectorTestCase extends Assert {
@Test @Test
public void testAppendBits() { public void testAppendBits() {
{ BitArray v = new BitArray();
BitArray v = new BitArray(); v.appendBits(0x1, 1);
v.appendBits(0x1, 1); assertEquals(1, v.getSize());
assertEquals(1, v.getSize()); assertEquals(0x80000000L, getUnsignedInt(v, 0));
assertEquals(0x80000000L, getUnsignedInt(v, 0)); v = new BitArray();
} v.appendBits(0xff, 8);
{ assertEquals(8, v.getSize());
BitArray v = new BitArray(); assertEquals(0xff000000L, getUnsignedInt(v, 0));
v.appendBits(0xff, 8); v = new BitArray();
assertEquals(8, v.getSize()); v.appendBits(0xff7, 12);
assertEquals(0xff000000L, getUnsignedInt(v, 0)); assertEquals(12, v.getSize());
} assertEquals(0xff700000L, getUnsignedInt(v, 0));
{
BitArray v = new BitArray();
v.appendBits(0xff7, 12);
assertEquals(12, v.getSize());
assertEquals(0xff700000L, getUnsignedInt(v, 0));
}
} }
@Test @Test

View file

@ -173,121 +173,91 @@ public final class EncoderTestCase extends Assert {
@Test @Test
public void testAppendLengthInfo() throws WriterException { public void testAppendLengthInfo() throws WriterException {
{ BitArray bits = new BitArray();
BitArray bits = new BitArray(); Encoder.appendLengthInfo(1, // 1 letter (1/1).
Encoder.appendLengthInfo(1, // 1 letter (1/1). Version.getVersionForNumber(1),
Version.getVersionForNumber(1), Mode.NUMERIC,
Mode.NUMERIC, bits);
bits); assertEquals(" ........ .X", bits.toString()); // 10 bits.
assertEquals(" ........ .X", bits.toString()); // 10 bits. bits = new BitArray();
} Encoder.appendLengthInfo(2, // 2 letters (2/1).
{ Version.getVersionForNumber(10),
BitArray bits = new BitArray(); Mode.ALPHANUMERIC,
Encoder.appendLengthInfo(2, // 2 letters (2/1). bits);
Version.getVersionForNumber(10), assertEquals(" ........ .X.", bits.toString()); // 11 bits.
Mode.ALPHANUMERIC, bits = new BitArray();
bits); Encoder.appendLengthInfo(255, // 255 letter (255/1).
assertEquals(" ........ .X.", bits.toString()); // 11 bits. Version.getVersionForNumber(27),
} Mode.BYTE,
{ bits);
BitArray bits = new BitArray(); assertEquals(" ........ XXXXXXXX", bits.toString()); // 16 bits.
Encoder.appendLengthInfo(255, // 255 letter (255/1). bits = new BitArray();
Version.getVersionForNumber(27), Encoder.appendLengthInfo(512, // 512 letters (1024/2).
Mode.BYTE, Version.getVersionForNumber(40),
bits); Mode.KANJI,
assertEquals(" ........ XXXXXXXX", bits.toString()); // 16 bits. bits);
} assertEquals(" ..X..... ....", bits.toString()); // 12 bits.
{
BitArray bits = new BitArray();
Encoder.appendLengthInfo(512, // 512 letters (1024/2).
Version.getVersionForNumber(40),
Mode.KANJI,
bits);
assertEquals(" ..X..... ....", bits.toString()); // 12 bits.
}
} }
@Test @Test
public void testAppendBytes() throws WriterException { public void testAppendBytes() throws WriterException {
{ // Should use appendNumericBytes.
// Should use appendNumericBytes. // 1 = 01 = 0001 in 4 bits.
// 1 = 01 = 0001 in 4 bits. BitArray bits = new BitArray();
BitArray bits = new BitArray(); Encoder.appendBytes("1", Mode.NUMERIC, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
Encoder.appendBytes("1", Mode.NUMERIC, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING); assertEquals(" ...X" , bits.toString());
assertEquals(" ...X" , bits.toString()); // Should use appendAlphanumericBytes.
} // A = 10 = 0xa = 001010 in 6 bits
{ bits = new BitArray();
// Should use appendAlphanumericBytes. Encoder.appendBytes("A", Mode.ALPHANUMERIC, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
// A = 10 = 0xa = 001010 in 6 bits assertEquals(" ..X.X." , bits.toString());
BitArray bits = new BitArray(); // Lower letters such as 'a' cannot be encoded in MODE_ALPHANUMERIC.
Encoder.appendBytes("A", Mode.ALPHANUMERIC, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING); try {
assertEquals(" ..X.X." , bits.toString()); Encoder.appendBytes("a", Mode.ALPHANUMERIC, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
// Lower letters such as 'a' cannot be encoded in MODE_ALPHANUMERIC. } catch (WriterException we) {
try { // good
Encoder.appendBytes("a", Mode.ALPHANUMERIC, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
} catch (WriterException we) {
// good
}
}
{
// Should use append8BitBytes.
// 0x61, 0x62, 0x63
BitArray bits = new BitArray();
Encoder.appendBytes("abc", Mode.BYTE, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
assertEquals(" .XX....X .XX...X. .XX...XX", bits.toString());
// Anything can be encoded in QRCode.MODE_8BIT_BYTE.
Encoder.appendBytes("\0", Mode.BYTE, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
}
{
// Should use appendKanjiBytes.
// 0x93, 0x5f
BitArray bits = new BitArray();
Encoder.appendBytes(shiftJISString(new byte[] {(byte)0x93,0x5f}), Mode.KANJI, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
assertEquals(" .XX.XX.. XXXXX", bits.toString());
} }
// Should use append8BitBytes.
// 0x61, 0x62, 0x63
bits = new BitArray();
Encoder.appendBytes("abc", Mode.BYTE, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
assertEquals(" .XX....X .XX...X. .XX...XX", bits.toString());
// Anything can be encoded in QRCode.MODE_8BIT_BYTE.
Encoder.appendBytes("\0", Mode.BYTE, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
// Should use appendKanjiBytes.
// 0x93, 0x5f
bits = new BitArray();
Encoder.appendBytes(shiftJISString(new byte[] {(byte)0x93,0x5f}), Mode.KANJI, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
assertEquals(" .XX.XX.. XXXXX", bits.toString());
} }
@Test @Test
public void testTerminateBits() throws WriterException { public void testTerminateBits() throws WriterException {
{ BitArray v = new BitArray();
BitArray v = new BitArray(); Encoder.terminateBits(0, v);
Encoder.terminateBits(0, v); assertEquals("", v.toString());
assertEquals("", v.toString()); v = new BitArray();
} Encoder.terminateBits(1, v);
{ assertEquals(" ........", v.toString());
BitArray v = new BitArray(); v = new BitArray();
Encoder.terminateBits(1, v); v.appendBits(0, 3); // Append 000
assertEquals(" ........", v.toString()); Encoder.terminateBits(1, v);
} assertEquals(" ........", v.toString());
{ v = new BitArray();
BitArray v = new BitArray(); v.appendBits(0, 5); // Append 00000
v.appendBits(0, 3); // Append 000 Encoder.terminateBits(1, v);
Encoder.terminateBits(1, v); assertEquals(" ........", v.toString());
assertEquals(" ........", v.toString()); v = new BitArray();
} v.appendBits(0, 8); // Append 00000000
{ Encoder.terminateBits(1, v);
BitArray v = new BitArray(); assertEquals(" ........", v.toString());
v.appendBits(0, 5); // Append 00000 v = new BitArray();
Encoder.terminateBits(1, v); Encoder.terminateBits(2, v);
assertEquals(" ........", v.toString()); assertEquals(" ........ XXX.XX..", v.toString());
} v = new BitArray();
{ v.appendBits(0, 1); // Append 0
BitArray v = new BitArray(); Encoder.terminateBits(3, v);
v.appendBits(0, 8); // Append 00000000 assertEquals(" ........ XXX.XX.. ...X...X", v.toString());
Encoder.terminateBits(1, v);
assertEquals(" ........", v.toString());
}
{
BitArray v = new BitArray();
Encoder.terminateBits(2, v);
assertEquals(" ........ XXX.XX..", v.toString());
}
{
BitArray v = new BitArray();
v.appendBits(0, 1); // Append 0
Encoder.terminateBits(3, v);
assertEquals(" ........ XXX.XX.. ...X...X", v.toString());
}
} }
@Test @Test
@ -329,154 +299,126 @@ public final class EncoderTestCase extends Assert {
@Test @Test
public void testInterleaveWithECBytes() throws WriterException { public void testInterleaveWithECBytes() throws WriterException {
{ byte[] dataBytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
byte[] dataBytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236}; BitArray in = new BitArray();
BitArray in = new BitArray(); for (byte dataByte: dataBytes) {
for (byte dataByte: dataBytes) { in.appendBits(dataByte, 8);
in.appendBits(dataByte, 8); }
} BitArray out = Encoder.interleaveWithECBytes(in, 26, 9, 1);
BitArray out = Encoder.interleaveWithECBytes(in, 26, 9, 1); byte[] expected = {
byte[] expected = { // Data bytes.
// Data bytes. 32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236,
32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236, // Error correction bytes.
// Error correction bytes. 42, (byte)159, 74, (byte)221, (byte)244, (byte)169, (byte)239, (byte)150, (byte)138, 70,
42, (byte)159, 74, (byte)221, (byte)244, (byte)169, (byte)239, (byte)150, (byte)138, 70, (byte)237, 85, (byte)224, 96, 74, (byte)219, 61,
(byte)237, 85, (byte)224, 96, 74, (byte)219, 61, };
}; assertEquals(expected.length, out.getSizeInBytes());
assertEquals(expected.length, out.getSizeInBytes()); byte[] outArray = new byte[expected.length];
byte[] outArray = new byte[expected.length]; out.toBytes(0, outArray, 0, expected.length);
out.toBytes(0, outArray, 0, expected.length); // Can't use Arrays.equals(), because outArray may be longer than out.sizeInBytes()
// Can't use Arrays.equals(), because outArray may be longer than out.sizeInBytes() for (int x = 0; x < expected.length; x++) {
for (int x = 0; x < expected.length; x++) { assertEquals(expected[x], outArray[x]);
assertEquals(expected[x], outArray[x]);
}
} }
// Numbers are from http://www.swetake.com/qr/qr8.html // Numbers are from http://www.swetake.com/qr/qr8.html
{ dataBytes = new byte[] {
byte[] dataBytes = { 67, 70, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166, (byte)182,
67, 70, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166, (byte)182, (byte)198, (byte)214, (byte)230, (byte)247, 7, 23, 39, 55, 71, 87, 103, 119, (byte)135,
(byte)198, (byte)214, (byte)230, (byte)247, 7, 23, 39, 55, 71, 87, 103, 119, (byte)135, (byte)151, (byte)166, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166,
(byte)151, (byte)166, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166, (byte)182, (byte)198, (byte)214, (byte)230, (byte)247, 7, 23, 39, 55, 71, 87, 103, 119,
(byte)182, (byte)198, (byte)214, (byte)230, (byte)247, 7, 23, 39, 55, 71, 87, 103, 119, (byte)135, (byte)151, (byte)160, (byte)236, 17, (byte)236, 17, (byte)236, 17, (byte)236,
(byte)135, (byte)151, (byte)160, (byte)236, 17, (byte)236, 17, (byte)236, 17, (byte)236, 17
17 };
}; in = new BitArray();
BitArray in = new BitArray(); for (byte dataByte: dataBytes) {
for (byte dataByte: dataBytes) { in.appendBits(dataByte, 8);
in.appendBits(dataByte, 8); }
}
BitArray out = Encoder.interleaveWithECBytes(in, 134, 62, 4); out = Encoder.interleaveWithECBytes(in, 134, 62, 4);
byte[] expected = { expected = new byte[] {
// Data bytes. // Data bytes.
67, (byte)230, 54, 55, 70, (byte)247, 70, 71, 22, 7, 86, 87, 38, 23, 102, 103, 54, 39, 67, (byte)230, 54, 55, 70, (byte)247, 70, 71, 22, 7, 86, 87, 38, 23, 102, 103, 54, 39,
118, 119, 70, 55, (byte)134, (byte)135, 86, 71, (byte)150, (byte)151, 102, 87, (byte)166, 118, 119, 70, 55, (byte)134, (byte)135, 86, 71, (byte)150, (byte)151, 102, 87, (byte)166,
(byte)160, 118, 103, (byte)182, (byte)236, (byte)134, 119, (byte)198, 17, (byte)150, (byte)160, 118, 103, (byte)182, (byte)236, (byte)134, 119, (byte)198, 17, (byte)150,
(byte)135, (byte)214, (byte)236, (byte)166, (byte)151, (byte)230, 17, (byte)182, (byte)135, (byte)214, (byte)236, (byte)166, (byte)151, (byte)230, 17, (byte)182,
(byte)166, (byte)247, (byte)236, (byte)198, 22, 7, 17, (byte)214, 38, 23, (byte)236, 39, (byte)166, (byte)247, (byte)236, (byte)198, 22, 7, 17, (byte)214, 38, 23, (byte)236, 39,
17, 17,
// Error correction bytes. // Error correction bytes.
(byte)175, (byte)155, (byte)245, (byte)236, 80, (byte)146, 56, 74, (byte)155, (byte)165, (byte)175, (byte)155, (byte)245, (byte)236, 80, (byte)146, 56, 74, (byte)155, (byte)165,
(byte)133, (byte)142, 64, (byte)183, (byte)132, 13, (byte)178, 54, (byte)132, 108, 45, (byte)133, (byte)142, 64, (byte)183, (byte)132, 13, (byte)178, 54, (byte)132, 108, 45,
113, 53, 50, (byte)214, 98, (byte)193, (byte)152, (byte)233, (byte)147, 50, 71, 65, 113, 53, 50, (byte)214, 98, (byte)193, (byte)152, (byte)233, (byte)147, 50, 71, 65,
(byte)190, 82, 51, (byte)209, (byte)199, (byte)171, 54, 12, 112, 57, 113, (byte)155, 117, (byte)190, 82, 51, (byte)209, (byte)199, (byte)171, 54, 12, 112, 57, 113, (byte)155, 117,
(byte)211, (byte)164, 117, 30, (byte)158, (byte)225, 31, (byte)190, (byte)242, 38, (byte)211, (byte)164, 117, 30, (byte)158, (byte)225, 31, (byte)190, (byte)242, 38,
(byte)140, 61, (byte)179, (byte)154, (byte)214, (byte)138, (byte)147, 87, 27, 96, 77, 47, (byte)140, 61, (byte)179, (byte)154, (byte)214, (byte)138, (byte)147, 87, 27, 96, 77, 47,
(byte)187, 49, (byte)156, (byte)214, (byte)187, 49, (byte)156, (byte)214,
}; };
assertEquals(expected.length, out.getSizeInBytes()); assertEquals(expected.length, out.getSizeInBytes());
byte[] outArray = new byte[expected.length]; outArray = new byte[expected.length];
out.toBytes(0, outArray, 0, expected.length); out.toBytes(0, outArray, 0, expected.length);
for (int x = 0; x < expected.length; x++) { for (int x = 0; x < expected.length; x++) {
assertEquals(expected[x], outArray[x]); assertEquals(expected[x], outArray[x]);
}
} }
} }
@Test @Test
public void testAppendNumericBytes() { public void testAppendNumericBytes() {
{ // 1 = 01 = 0001 in 4 bits.
// 1 = 01 = 0001 in 4 bits. BitArray bits = new BitArray();
BitArray bits = new BitArray(); Encoder.appendNumericBytes("1", bits);
Encoder.appendNumericBytes("1", bits); assertEquals(" ...X" , bits.toString());
assertEquals(" ...X" , bits.toString()); // 12 = 0xc = 0001100 in 7 bits.
} bits = new BitArray();
{ Encoder.appendNumericBytes("12", bits);
// 12 = 0xc = 0001100 in 7 bits. assertEquals(" ...XX.." , bits.toString());
BitArray bits = new BitArray(); // 123 = 0x7b = 0001111011 in 10 bits.
Encoder.appendNumericBytes("12", bits); bits = new BitArray();
assertEquals(" ...XX.." , bits.toString()); Encoder.appendNumericBytes("123", bits);
} assertEquals(" ...XXXX. XX" , bits.toString());
{ // 1234 = "123" + "4" = 0001111011 + 0100
// 123 = 0x7b = 0001111011 in 10 bits. bits = new BitArray();
BitArray bits = new BitArray(); Encoder.appendNumericBytes("1234", bits);
Encoder.appendNumericBytes("123", bits); assertEquals(" ...XXXX. XX.X.." , bits.toString());
assertEquals(" ...XXXX. XX" , bits.toString()); // Empty.
} bits = new BitArray();
{ Encoder.appendNumericBytes("", bits);
// 1234 = "123" + "4" = 0001111011 + 0100 assertEquals("" , bits.toString());
BitArray bits = new BitArray();
Encoder.appendNumericBytes("1234", bits);
assertEquals(" ...XXXX. XX.X.." , bits.toString());
}
{
// Empty.
BitArray bits = new BitArray();
Encoder.appendNumericBytes("", bits);
assertEquals("" , bits.toString());
}
} }
@Test @Test
public void testAppendAlphanumericBytes() throws WriterException { public void testAppendAlphanumericBytes() throws WriterException {
{ // A = 10 = 0xa = 001010 in 6 bits
// A = 10 = 0xa = 001010 in 6 bits BitArray bits = new BitArray();
BitArray bits = new BitArray(); Encoder.appendAlphanumericBytes("A", bits);
Encoder.appendAlphanumericBytes("A", bits); assertEquals(" ..X.X." , bits.toString());
assertEquals(" ..X.X." , bits.toString()); // AB = 10 * 45 + 11 = 461 = 0x1cd = 00111001101 in 11 bits
} bits = new BitArray();
{ Encoder.appendAlphanumericBytes("AB", bits);
// AB = 10 * 45 + 11 = 461 = 0x1cd = 00111001101 in 11 bits assertEquals(" ..XXX..X X.X", bits.toString());
BitArray bits = new BitArray(); // ABC = "AB" + "C" = 00111001101 + 001100
Encoder.appendAlphanumericBytes("AB", bits); bits = new BitArray();
assertEquals(" ..XXX..X X.X", bits.toString()); Encoder.appendAlphanumericBytes("ABC", bits);
} assertEquals(" ..XXX..X X.X..XX. ." , bits.toString());
{ // Empty.
// ABC = "AB" + "C" = 00111001101 + 001100 bits = new BitArray();
BitArray bits = new BitArray(); Encoder.appendAlphanumericBytes("", bits);
Encoder.appendAlphanumericBytes("ABC", bits); assertEquals("" , bits.toString());
assertEquals(" ..XXX..X X.X..XX. ." , bits.toString()); // Invalid data.
} try {
{ Encoder.appendAlphanumericBytes("abc", new BitArray());
// Empty. } catch (WriterException we) {
BitArray bits = new BitArray(); // good
Encoder.appendAlphanumericBytes("", bits);
assertEquals("" , bits.toString());
}
{
// Invalid data.
try {
Encoder.appendAlphanumericBytes("abc", new BitArray());
} catch (WriterException we) {
// good
}
} }
} }
@Test @Test
public void testAppend8BitBytes() throws WriterException { public void testAppend8BitBytes() throws WriterException {
{ // 0x61, 0x62, 0x63
// 0x61, 0x62, 0x63 BitArray bits = new BitArray();
BitArray bits = new BitArray(); Encoder.append8BitBytes("abc", bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
Encoder.append8BitBytes("abc", bits, Encoder.DEFAULT_BYTE_MODE_ENCODING); assertEquals(" .XX....X .XX...X. .XX...XX", bits.toString());
assertEquals(" .XX....X .XX...X. .XX...XX", bits.toString()); // Empty.
} bits = new BitArray();
{ Encoder.append8BitBytes("", bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
// Empty. assertEquals("", bits.toString());
BitArray bits = new BitArray();
Encoder.append8BitBytes("", bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
assertEquals("", bits.toString());
}
} }
// Numbers are from page 21 of JISX0510:2004 // Numbers are from page 21 of JISX0510:2004
@ -493,40 +435,34 @@ public final class EncoderTestCase extends Assert {
// http://www.swetake.com/qr/qr9.html // http://www.swetake.com/qr/qr9.html
@Test @Test
public void testGenerateECBytes() { public void testGenerateECBytes() {
{ byte[] dataBytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
byte[] dataBytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236}; byte[] ecBytes = Encoder.generateECBytes(dataBytes, 17);
byte[] ecBytes = Encoder.generateECBytes(dataBytes, 17); int[] expected = {
int[] expected = { 42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237, 85, 224, 96, 74, 219, 61
42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237, 85, 224, 96, 74, 219, 61 };
}; assertEquals(expected.length, ecBytes.length);
assertEquals(expected.length, ecBytes.length); for (int x = 0; x < expected.length; x++) {
for (int x = 0; x < expected.length; x++) { assertEquals(expected[x], ecBytes[x] & 0xFF);
assertEquals(expected[x], ecBytes[x] & 0xFF);
}
} }
{ dataBytes = new byte[] {67, 70, 22, 38, 54, 70, 86, 102, 118,
byte[] dataBytes = {67, 70, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166, (byte)182, (byte)198, (byte)214};
(byte)134, (byte)150, (byte)166, (byte)182, (byte)198, (byte)214}; ecBytes = Encoder.generateECBytes(dataBytes, 18);
byte[] ecBytes = Encoder.generateECBytes(dataBytes, 18); expected = new int[] {
int[] expected = { 175, 80, 155, 64, 178, 45, 214, 233, 65, 209, 12, 155, 117, 31, 140, 214, 27, 187
175, 80, 155, 64, 178, 45, 214, 233, 65, 209, 12, 155, 117, 31, 140, 214, 27, 187 };
}; assertEquals(expected.length, ecBytes.length);
assertEquals(expected.length, ecBytes.length); for (int x = 0; x < expected.length; x++) {
for (int x = 0; x < expected.length; x++) { assertEquals(expected[x], ecBytes[x] & 0xFF);
assertEquals(expected[x], ecBytes[x] & 0xFF);
}
} }
{ // High-order zero coefficient case.
// High-order zero coefficient case. dataBytes = new byte[] {32, 49, (byte)205, 69, 42, 20, 0, (byte)236, 17};
byte[] dataBytes = {32, 49, (byte)205, 69, 42, 20, 0, (byte)236, 17}; ecBytes = Encoder.generateECBytes(dataBytes, 17);
byte[] ecBytes = Encoder.generateECBytes(dataBytes, 17); expected = new int[] {
int[] expected = { 0, 3, 130, 179, 194, 0, 55, 211, 110, 79, 98, 72, 170, 96, 211, 137, 213
0, 3, 130, 179, 194, 0, 55, 211, 110, 79, 98, 72, 170, 96, 211, 137, 213 };
}; assertEquals(expected.length, ecBytes.length);
assertEquals(expected.length, ecBytes.length); for (int x = 0; x < expected.length; x++) {
for (int x = 0; x < expected.length; x++) { assertEquals(expected[x], ecBytes[x] & 0xFF);
assertEquals(expected[x], ecBytes[x] & 0xFF);
}
} }
} }

View file

@ -23,186 +23,157 @@ import org.junit.Test;
* @author satorux@google.com (Satoru Takabayashi) - creator * @author satorux@google.com (Satoru Takabayashi) - creator
* @author mysen@google.com (Chris Mysen) - ported from C++ * @author mysen@google.com (Chris Mysen) - ported from C++
*/ */
public final class MaskUtilTestCase extends Assert { public final class MaskUtilTestCase extends Assert {
@Test @Test
public void testApplyMaskPenaltyRule1() { public void testApplyMaskPenaltyRule1() {
{ ByteMatrix matrix = new ByteMatrix(4, 1);
ByteMatrix matrix = new ByteMatrix(4, 1); matrix.set(0, 0, 0);
matrix.set(0, 0, 0); matrix.set(1, 0, 0);
matrix.set(1, 0, 0); matrix.set(2, 0, 0);
matrix.set(2, 0, 0); matrix.set(3, 0, 0);
matrix.set(3, 0, 0); assertEquals(0, MaskUtil.applyMaskPenaltyRule1(matrix));
assertEquals(0, MaskUtil.applyMaskPenaltyRule1(matrix)); // Horizontal.
} matrix = new ByteMatrix(6, 1);
{ // Horizontal. matrix.set(0, 0, 0);
ByteMatrix matrix = new ByteMatrix(6, 1); matrix.set(1, 0, 0);
matrix.set(0, 0, 0); matrix.set(2, 0, 0);
matrix.set(1, 0, 0); matrix.set(3, 0, 0);
matrix.set(2, 0, 0); matrix.set(4, 0, 0);
matrix.set(3, 0, 0); matrix.set(5, 0, 1);
matrix.set(4, 0, 0); assertEquals(3, MaskUtil.applyMaskPenaltyRule1(matrix));
matrix.set(5, 0, 1); matrix.set(5, 0, 0);
assertEquals(3, MaskUtil.applyMaskPenaltyRule1(matrix)); assertEquals(4, MaskUtil.applyMaskPenaltyRule1(matrix));
matrix.set(5, 0, 0); // Vertical.
assertEquals(4, MaskUtil.applyMaskPenaltyRule1(matrix)); matrix = new ByteMatrix(1, 6);
} matrix.set(0, 0, 0);
{ // Vertical. matrix.set(0, 1, 0);
ByteMatrix matrix = new ByteMatrix(1, 6); matrix.set(0, 2, 0);
matrix.set(0, 0, 0); matrix.set(0, 3, 0);
matrix.set(0, 1, 0); matrix.set(0, 4, 0);
matrix.set(0, 2, 0); matrix.set(0, 5, 1);
matrix.set(0, 3, 0); assertEquals(3, MaskUtil.applyMaskPenaltyRule1(matrix));
matrix.set(0, 4, 0); matrix.set(0, 5, 0);
matrix.set(0, 5, 1); assertEquals(4, MaskUtil.applyMaskPenaltyRule1(matrix));
assertEquals(3, MaskUtil.applyMaskPenaltyRule1(matrix));
matrix.set(0, 5, 0);
assertEquals(4, MaskUtil.applyMaskPenaltyRule1(matrix));
}
} }
@Test @Test
public void testApplyMaskPenaltyRule2() { public void testApplyMaskPenaltyRule2() {
{ ByteMatrix matrix = new ByteMatrix(1, 1);
ByteMatrix matrix = new ByteMatrix(1, 1); matrix.set(0, 0, 0);
matrix.set(0, 0, 0); assertEquals(0, MaskUtil.applyMaskPenaltyRule2(matrix));
assertEquals(0, MaskUtil.applyMaskPenaltyRule2(matrix)); matrix = new ByteMatrix(2, 2);
} matrix.set(0, 0, 0);
{ matrix.set(1, 0, 0);
ByteMatrix matrix = new ByteMatrix(2, 2); matrix.set(0, 1, 0);
matrix.set(0, 0, 0); matrix.set(1, 1, 1);
matrix.set(1, 0, 0); assertEquals(0, MaskUtil.applyMaskPenaltyRule2(matrix));
matrix.set(0, 1, 0); matrix = new ByteMatrix(2, 2);
matrix.set(1, 1, 1); matrix.set(0, 0, 0);
assertEquals(0, MaskUtil.applyMaskPenaltyRule2(matrix)); matrix.set(1, 0, 0);
} matrix.set(0, 1, 0);
{ matrix.set(1, 1, 0);
ByteMatrix matrix = new ByteMatrix(2, 2); assertEquals(3, MaskUtil.applyMaskPenaltyRule2(matrix));
matrix.set(0, 0, 0); matrix = new ByteMatrix(3, 3);
matrix.set(1, 0, 0); matrix.set(0, 0, 0);
matrix.set(0, 1, 0); matrix.set(1, 0, 0);
matrix.set(1, 1, 0); matrix.set(2, 0, 0);
assertEquals(3, MaskUtil.applyMaskPenaltyRule2(matrix)); matrix.set(0, 1, 0);
} matrix.set(1, 1, 0);
{ matrix.set(2, 1, 0);
ByteMatrix matrix = new ByteMatrix(3, 3); matrix.set(0, 2, 0);
matrix.set(0, 0, 0); matrix.set(1, 2, 0);
matrix.set(1, 0, 0); matrix.set(2, 2, 0);
matrix.set(2, 0, 0); // Four instances of 2x2 blocks.
matrix.set(0, 1, 0); assertEquals(3 * 4, MaskUtil.applyMaskPenaltyRule2(matrix));
matrix.set(1, 1, 0);
matrix.set(2, 1, 0);
matrix.set(0, 2, 0);
matrix.set(1, 2, 0);
matrix.set(2, 2, 0);
// Four instances of 2x2 blocks.
assertEquals(3 * 4, MaskUtil.applyMaskPenaltyRule2(matrix));
}
} }
@Test @Test
public void testApplyMaskPenaltyRule3() { public void testApplyMaskPenaltyRule3() {
{ // Horizontal 00001011101.
// Horizontal 00001011101. ByteMatrix matrix = new ByteMatrix(11, 1);
ByteMatrix matrix = new ByteMatrix(11, 1); matrix.set(0, 0, 0);
matrix.set(0, 0, 0); matrix.set(1, 0, 0);
matrix.set(1, 0, 0); matrix.set(2, 0, 0);
matrix.set(2, 0, 0); matrix.set(3, 0, 0);
matrix.set(3, 0, 0); matrix.set(4, 0, 1);
matrix.set(4, 0, 1); matrix.set(5, 0, 0);
matrix.set(5, 0, 0); matrix.set(6, 0, 1);
matrix.set(6, 0, 1); matrix.set(7, 0, 1);
matrix.set(7, 0, 1); matrix.set(8, 0, 1);
matrix.set(8, 0, 1); matrix.set(9, 0, 0);
matrix.set(9, 0, 0); matrix.set(10, 0, 1);
matrix.set(10, 0, 1); assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix)); // Horizontal 10111010000.
} matrix = new ByteMatrix(11, 1);
{ matrix.set(0, 0, 1);
// Horizontal 10111010000. matrix.set(1, 0, 0);
ByteMatrix matrix = new ByteMatrix(11, 1); matrix.set(2, 0, 1);
matrix.set(0, 0, 1); matrix.set(3, 0, 1);
matrix.set(1, 0, 0); matrix.set(4, 0, 1);
matrix.set(2, 0, 1); matrix.set(5, 0, 0);
matrix.set(3, 0, 1); matrix.set(6, 0, 1);
matrix.set(4, 0, 1); matrix.set(7, 0, 0);
matrix.set(5, 0, 0); matrix.set(8, 0, 0);
matrix.set(6, 0, 1); matrix.set(9, 0, 0);
matrix.set(7, 0, 0); matrix.set(10, 0, 0);
matrix.set(8, 0, 0); assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
matrix.set(9, 0, 0); // Vertical 00001011101.
matrix.set(10, 0, 0); matrix = new ByteMatrix(1, 11);
assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix)); matrix.set(0, 0, 0);
} matrix.set(0, 1, 0);
{ matrix.set(0, 2, 0);
// Vertical 00001011101. matrix.set(0, 3, 0);
ByteMatrix matrix = new ByteMatrix(1, 11); matrix.set(0, 4, 1);
matrix.set(0, 0, 0); matrix.set(0, 5, 0);
matrix.set(0, 1, 0); matrix.set(0, 6, 1);
matrix.set(0, 2, 0); matrix.set(0, 7, 1);
matrix.set(0, 3, 0); matrix.set(0, 8, 1);
matrix.set(0, 4, 1); matrix.set(0, 9, 0);
matrix.set(0, 5, 0); matrix.set(0, 10, 1);
matrix.set(0, 6, 1); assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
matrix.set(0, 7, 1); // Vertical 10111010000.
matrix.set(0, 8, 1); matrix = new ByteMatrix(1, 11);
matrix.set(0, 9, 0); matrix.set(0, 0, 1);
matrix.set(0, 10, 1); matrix.set(0, 1, 0);
assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix)); matrix.set(0, 2, 1);
} matrix.set(0, 3, 1);
{ matrix.set(0, 4, 1);
// Vertical 10111010000. matrix.set(0, 5, 0);
ByteMatrix matrix = new ByteMatrix(1, 11); matrix.set(0, 6, 1);
matrix.set(0, 0, 1); matrix.set(0, 7, 0);
matrix.set(0, 1, 0); matrix.set(0, 8, 0);
matrix.set(0, 2, 1); matrix.set(0, 9, 0);
matrix.set(0, 3, 1); matrix.set(0, 10, 0);
matrix.set(0, 4, 1); assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
matrix.set(0, 5, 0);
matrix.set(0, 6, 1);
matrix.set(0, 7, 0);
matrix.set(0, 8, 0);
matrix.set(0, 9, 0);
matrix.set(0, 10, 0);
assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
}
} }
@Test @Test
public void testApplyMaskPenaltyRule4() { public void testApplyMaskPenaltyRule4() {
{ // Dark cell ratio = 0%
// Dark cell ratio = 0% ByteMatrix matrix = new ByteMatrix(1, 1);
ByteMatrix matrix = new ByteMatrix(1, 1); matrix.set(0, 0, 0);
matrix.set(0, 0, 0); assertEquals(100, MaskUtil.applyMaskPenaltyRule4(matrix));
assertEquals(100, MaskUtil.applyMaskPenaltyRule4(matrix)); // Dark cell ratio = 5%
} matrix = new ByteMatrix(2, 1);
{ matrix.set(0, 0, 0);
// Dark cell ratio = 5% matrix.set(0, 0, 1);
ByteMatrix matrix = new ByteMatrix(2, 1); assertEquals(0, MaskUtil.applyMaskPenaltyRule4(matrix));
matrix.set(0, 0, 0); // Dark cell ratio = 66.67%
matrix.set(0, 0, 1); matrix = new ByteMatrix(6, 1);
assertEquals(0, MaskUtil.applyMaskPenaltyRule4(matrix)); matrix.set(0, 0, 0);
} matrix.set(1, 0, 1);
{ matrix.set(2, 0, 1);
// Dark cell ratio = 66.67% matrix.set(3, 0, 1);
ByteMatrix matrix = new ByteMatrix(6, 1); matrix.set(4, 0, 1);
matrix.set(0, 0, 0); matrix.set(5, 0, 0);
matrix.set(1, 0, 1); assertEquals(30, MaskUtil.applyMaskPenaltyRule4(matrix));
matrix.set(2, 0, 1);
matrix.set(3, 0, 1);
matrix.set(4, 0, 1);
matrix.set(5, 0, 0);
assertEquals(30, MaskUtil.applyMaskPenaltyRule4(matrix));
}
} }
private static boolean TestGetDataMaskBitInternal(int maskPattern, private static boolean TestGetDataMaskBitInternal(int maskPattern, int[][] expected) {
int[][] expected) {
for (int x = 0; x < 6; ++x) { for (int x = 0; x < 6; ++x) {
for (int y = 0; y < 6; ++y) { for (int y = 0; y < 6; ++y) {
if ((expected[y][x] == 1) != if ((expected[y][x] == 1) != MaskUtil.getDataMaskBit(maskPattern, x, y)) {
MaskUtil.getDataMaskBit(maskPattern, x, y)) {
return false; return false;
} }
} }