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.put(EncodeHintType.CHARACTER_SET, encoding);
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result;
try {
result = writer.encode(contentsToEncode, format, dimension, dimension, hints);
result = new MultiFormatWriter().encode(contentsToEncode, format, dimension, dimension, hints);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;

View file

@ -34,19 +34,6 @@ public final class BenchmarkActivity extends Activity {
private TextView textView;
private Thread benchmarkThread;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.benchmark);
runBenchmarkButton = (Button) findViewById(R.id.benchmark_run);
runBenchmarkButton.setOnClickListener(runBenchmark);
textView = (TextView) findViewById(R.id.benchmark_help);
benchmarkThread = null;
}
private final Button.OnClickListener runBenchmark = new Button.OnClickListener() {
@Override
public void onClick(View v) {
@ -59,22 +46,10 @@ public final class BenchmarkActivity extends Activity {
}
};
final Handler handler = new Handler() {
private 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) {
if (message.what == R.id.benchmark_done) {
Iterable<BenchmarkItem> items = (Iterable<BenchmarkItem>) message.obj;
int count = 0;
int time = 0;
@ -88,6 +63,27 @@ public final class BenchmarkActivity extends Activity {
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
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.benchmark);
runBenchmarkButton = (Button) findViewById(R.id.benchmark_run);
runBenchmarkButton.setOnClickListener(runBenchmark);
textView = (TextView) findViewById(R.id.benchmark_help);
benchmarkThread = null;
}
}

View file

@ -57,7 +57,7 @@ final class BenchmarkThread implements Runnable {
List<BenchmarkItem> items = new ArrayList<BenchmarkItem>();
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.sendToTarget();
}

View file

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

View file

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

View file

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

View file

@ -173,53 +173,42 @@ public final class EncoderTestCase extends Assert {
@Test
public void testAppendLengthInfo() throws WriterException {
{
BitArray bits = new BitArray();
Encoder.appendLengthInfo(1, // 1 letter (1/1).
Version.getVersionForNumber(1),
Mode.NUMERIC,
bits);
assertEquals(" ........ .X", bits.toString()); // 10 bits.
}
{
BitArray bits = new BitArray();
bits = new BitArray();
Encoder.appendLengthInfo(2, // 2 letters (2/1).
Version.getVersionForNumber(10),
Mode.ALPHANUMERIC,
bits);
assertEquals(" ........ .X.", bits.toString()); // 11 bits.
}
{
BitArray bits = new BitArray();
bits = new BitArray();
Encoder.appendLengthInfo(255, // 255 letter (255/1).
Version.getVersionForNumber(27),
Mode.BYTE,
bits);
assertEquals(" ........ XXXXXXXX", bits.toString()); // 16 bits.
}
{
BitArray bits = new BitArray();
bits = new BitArray();
Encoder.appendLengthInfo(512, // 512 letters (1024/2).
Version.getVersionForNumber(40),
Mode.KANJI,
bits);
assertEquals(" ..X..... ....", bits.toString()); // 12 bits.
}
}
@Test
public void testAppendBytes() throws WriterException {
{
// Should use appendNumericBytes.
// 1 = 01 = 0001 in 4 bits.
BitArray bits = new BitArray();
Encoder.appendBytes("1", Mode.NUMERIC, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
assertEquals(" ...X" , bits.toString());
}
{
// Should use appendAlphanumericBytes.
// A = 10 = 0xa = 001010 in 6 bits
BitArray bits = new BitArray();
bits = new BitArray();
Encoder.appendBytes("A", Mode.ALPHANUMERIC, bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
assertEquals(" ..X.X." , bits.toString());
// Lower letters such as 'a' cannot be encoded in MODE_ALPHANUMERIC.
@ -228,67 +217,48 @@ public final class EncoderTestCase extends Assert {
} catch (WriterException we) {
// good
}
}
{
// Should use append8BitBytes.
// 0x61, 0x62, 0x63
BitArray bits = new 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();
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
public void testTerminateBits() throws WriterException {
{
BitArray v = new BitArray();
Encoder.terminateBits(0, v);
assertEquals("", v.toString());
}
{
BitArray v = new BitArray();
v = new BitArray();
Encoder.terminateBits(1, v);
assertEquals(" ........", v.toString());
}
{
BitArray v = new BitArray();
v = new BitArray();
v.appendBits(0, 3); // Append 000
Encoder.terminateBits(1, v);
assertEquals(" ........", v.toString());
}
{
BitArray v = new BitArray();
v = new BitArray();
v.appendBits(0, 5); // Append 00000
Encoder.terminateBits(1, v);
assertEquals(" ........", v.toString());
}
{
BitArray v = new BitArray();
v = new BitArray();
v.appendBits(0, 8); // Append 00000000
Encoder.terminateBits(1, v);
assertEquals(" ........", v.toString());
}
{
BitArray v = new BitArray();
v = new BitArray();
Encoder.terminateBits(2, v);
assertEquals(" ........ XXX.XX..", v.toString());
}
{
BitArray v = new BitArray();
v = new BitArray();
v.appendBits(0, 1); // Append 0
Encoder.terminateBits(3, v);
assertEquals(" ........ XXX.XX.. ...X...X", v.toString());
}
}
@Test
public void testGetNumDataBytesAndNumECBytesForBlockID() throws WriterException {
@ -329,7 +299,6 @@ public final class EncoderTestCase extends Assert {
@Test
public void testInterleaveWithECBytes() throws WriterException {
{
byte[] dataBytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
BitArray in = new BitArray();
for (byte dataByte: dataBytes) {
@ -350,10 +319,8 @@ public final class EncoderTestCase extends Assert {
for (int x = 0; x < expected.length; x++) {
assertEquals(expected[x], outArray[x]);
}
}
// Numbers are from http://www.swetake.com/qr/qr8.html
{
byte[] dataBytes = {
dataBytes = new byte[] {
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)151, (byte)166, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166,
@ -361,13 +328,13 @@ public final class EncoderTestCase extends Assert {
(byte)135, (byte)151, (byte)160, (byte)236, 17, (byte)236, 17, (byte)236, 17, (byte)236,
17
};
BitArray in = new BitArray();
in = new BitArray();
for (byte dataByte: dataBytes) {
in.appendBits(dataByte, 8);
}
BitArray out = Encoder.interleaveWithECBytes(in, 134, 62, 4);
byte[] expected = {
out = Encoder.interleaveWithECBytes(in, 134, 62, 4);
expected = new byte[] {
// Data bytes.
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,
@ -385,75 +352,55 @@ public final class EncoderTestCase extends Assert {
(byte)187, 49, (byte)156, (byte)214,
};
assertEquals(expected.length, out.getSizeInBytes());
byte[] outArray = new byte[expected.length];
outArray = new byte[expected.length];
out.toBytes(0, outArray, 0, expected.length);
for (int x = 0; x < expected.length; x++) {
assertEquals(expected[x], outArray[x]);
}
}
}
@Test
public void testAppendNumericBytes() {
{
// 1 = 01 = 0001 in 4 bits.
BitArray bits = new BitArray();
Encoder.appendNumericBytes("1", bits);
assertEquals(" ...X" , bits.toString());
}
{
// 12 = 0xc = 0001100 in 7 bits.
BitArray bits = new BitArray();
bits = new BitArray();
Encoder.appendNumericBytes("12", bits);
assertEquals(" ...XX.." , bits.toString());
}
{
// 123 = 0x7b = 0001111011 in 10 bits.
BitArray bits = new BitArray();
bits = new BitArray();
Encoder.appendNumericBytes("123", bits);
assertEquals(" ...XXXX. XX" , bits.toString());
}
{
// 1234 = "123" + "4" = 0001111011 + 0100
BitArray bits = new BitArray();
bits = new BitArray();
Encoder.appendNumericBytes("1234", bits);
assertEquals(" ...XXXX. XX.X.." , bits.toString());
}
{
// Empty.
BitArray bits = new BitArray();
bits = new BitArray();
Encoder.appendNumericBytes("", bits);
assertEquals("" , bits.toString());
}
}
@Test
public void testAppendAlphanumericBytes() throws WriterException {
{
// A = 10 = 0xa = 001010 in 6 bits
BitArray bits = new BitArray();
Encoder.appendAlphanumericBytes("A", bits);
assertEquals(" ..X.X." , bits.toString());
}
{
// AB = 10 * 45 + 11 = 461 = 0x1cd = 00111001101 in 11 bits
BitArray bits = new BitArray();
bits = new BitArray();
Encoder.appendAlphanumericBytes("AB", bits);
assertEquals(" ..XXX..X X.X", bits.toString());
}
{
// ABC = "AB" + "C" = 00111001101 + 001100
BitArray bits = new BitArray();
bits = new BitArray();
Encoder.appendAlphanumericBytes("ABC", bits);
assertEquals(" ..XXX..X X.X..XX. ." , bits.toString());
}
{
// Empty.
BitArray bits = new BitArray();
bits = new BitArray();
Encoder.appendAlphanumericBytes("", bits);
assertEquals("" , bits.toString());
}
{
// Invalid data.
try {
Encoder.appendAlphanumericBytes("abc", new BitArray());
@ -461,23 +408,18 @@ public final class EncoderTestCase extends Assert {
// good
}
}
}
@Test
public void testAppend8BitBytes() throws WriterException {
{
// 0x61, 0x62, 0x63
BitArray bits = new BitArray();
Encoder.append8BitBytes("abc", bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
assertEquals(" .XX....X .XX...X. .XX...XX", bits.toString());
}
{
// Empty.
BitArray bits = new BitArray();
bits = new BitArray();
Encoder.append8BitBytes("", bits, Encoder.DEFAULT_BYTE_MODE_ENCODING);
assertEquals("", bits.toString());
}
}
// Numbers are from page 21 of JISX0510:2004
@Test
@ -493,7 +435,6 @@ public final class EncoderTestCase extends Assert {
// http://www.swetake.com/qr/qr9.html
@Test
public void testGenerateECBytes() {
{
byte[] dataBytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
byte[] ecBytes = Encoder.generateECBytes(dataBytes, 17);
int[] expected = {
@ -503,24 +444,20 @@ public final class EncoderTestCase extends Assert {
for (int x = 0; x < expected.length; x++) {
assertEquals(expected[x], ecBytes[x] & 0xFF);
}
}
{
byte[] dataBytes = {67, 70, 22, 38, 54, 70, 86, 102, 118,
dataBytes = new byte[] {67, 70, 22, 38, 54, 70, 86, 102, 118,
(byte)134, (byte)150, (byte)166, (byte)182, (byte)198, (byte)214};
byte[] ecBytes = Encoder.generateECBytes(dataBytes, 18);
int[] expected = {
ecBytes = Encoder.generateECBytes(dataBytes, 18);
expected = new int[] {
175, 80, 155, 64, 178, 45, 214, 233, 65, 209, 12, 155, 117, 31, 140, 214, 27, 187
};
assertEquals(expected.length, ecBytes.length);
for (int x = 0; x < expected.length; x++) {
assertEquals(expected[x], ecBytes[x] & 0xFF);
}
}
{
// High-order zero coefficient case.
byte[] dataBytes = {32, 49, (byte)205, 69, 42, 20, 0, (byte)236, 17};
byte[] ecBytes = Encoder.generateECBytes(dataBytes, 17);
int[] expected = {
dataBytes = new byte[] {32, 49, (byte)205, 69, 42, 20, 0, (byte)236, 17};
ecBytes = Encoder.generateECBytes(dataBytes, 17);
expected = new int[] {
0, 3, 130, 179, 194, 0, 55, 211, 110, 79, 98, 72, 170, 96, 211, 137, 213
};
assertEquals(expected.length, ecBytes.length);
@ -528,7 +465,6 @@ public final class EncoderTestCase extends Assert {
assertEquals(expected[x], ecBytes[x] & 0xFF);
}
}
}
@Test
public void testBugInBitVectorNumBytes() throws WriterException {

View file

@ -23,21 +23,18 @@ import org.junit.Test;
* @author satorux@google.com (Satoru Takabayashi) - creator
* @author mysen@google.com (Chris Mysen) - ported from C++
*/
public final class MaskUtilTestCase extends Assert {
@Test
public void testApplyMaskPenaltyRule1() {
{
ByteMatrix matrix = new ByteMatrix(4, 1);
matrix.set(0, 0, 0);
matrix.set(1, 0, 0);
matrix.set(2, 0, 0);
matrix.set(3, 0, 0);
assertEquals(0, MaskUtil.applyMaskPenaltyRule1(matrix));
}
{ // Horizontal.
ByteMatrix matrix = new ByteMatrix(6, 1);
// Horizontal.
matrix = new ByteMatrix(6, 1);
matrix.set(0, 0, 0);
matrix.set(1, 0, 0);
matrix.set(2, 0, 0);
@ -47,9 +44,8 @@ public final class MaskUtilTestCase extends Assert {
assertEquals(3, MaskUtil.applyMaskPenaltyRule1(matrix));
matrix.set(5, 0, 0);
assertEquals(4, MaskUtil.applyMaskPenaltyRule1(matrix));
}
{ // Vertical.
ByteMatrix matrix = new ByteMatrix(1, 6);
// Vertical.
matrix = new ByteMatrix(1, 6);
matrix.set(0, 0, 0);
matrix.set(0, 1, 0);
matrix.set(0, 2, 0);
@ -60,33 +56,25 @@ public final class MaskUtilTestCase extends Assert {
matrix.set(0, 5, 0);
assertEquals(4, MaskUtil.applyMaskPenaltyRule1(matrix));
}
}
@Test
public void testApplyMaskPenaltyRule2() {
{
ByteMatrix matrix = new ByteMatrix(1, 1);
matrix.set(0, 0, 0);
assertEquals(0, MaskUtil.applyMaskPenaltyRule2(matrix));
}
{
ByteMatrix matrix = new ByteMatrix(2, 2);
matrix = new ByteMatrix(2, 2);
matrix.set(0, 0, 0);
matrix.set(1, 0, 0);
matrix.set(0, 1, 0);
matrix.set(1, 1, 1);
assertEquals(0, MaskUtil.applyMaskPenaltyRule2(matrix));
}
{
ByteMatrix matrix = new ByteMatrix(2, 2);
matrix = new ByteMatrix(2, 2);
matrix.set(0, 0, 0);
matrix.set(1, 0, 0);
matrix.set(0, 1, 0);
matrix.set(1, 1, 0);
assertEquals(3, MaskUtil.applyMaskPenaltyRule2(matrix));
}
{
ByteMatrix matrix = new ByteMatrix(3, 3);
matrix = new ByteMatrix(3, 3);
matrix.set(0, 0, 0);
matrix.set(1, 0, 0);
matrix.set(2, 0, 0);
@ -99,11 +87,9 @@ public final class MaskUtilTestCase extends Assert {
// Four instances of 2x2 blocks.
assertEquals(3 * 4, MaskUtil.applyMaskPenaltyRule2(matrix));
}
}
@Test
public void testApplyMaskPenaltyRule3() {
{
// Horizontal 00001011101.
ByteMatrix matrix = new ByteMatrix(11, 1);
matrix.set(0, 0, 0);
@ -118,10 +104,8 @@ public final class MaskUtilTestCase extends Assert {
matrix.set(9, 0, 0);
matrix.set(10, 0, 1);
assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
}
{
// Horizontal 10111010000.
ByteMatrix matrix = new ByteMatrix(11, 1);
matrix = new ByteMatrix(11, 1);
matrix.set(0, 0, 1);
matrix.set(1, 0, 0);
matrix.set(2, 0, 1);
@ -134,10 +118,8 @@ public final class MaskUtilTestCase extends Assert {
matrix.set(9, 0, 0);
matrix.set(10, 0, 0);
assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
}
{
// Vertical 00001011101.
ByteMatrix matrix = new ByteMatrix(1, 11);
matrix = new ByteMatrix(1, 11);
matrix.set(0, 0, 0);
matrix.set(0, 1, 0);
matrix.set(0, 2, 0);
@ -150,10 +132,8 @@ public final class MaskUtilTestCase extends Assert {
matrix.set(0, 9, 0);
matrix.set(0, 10, 1);
assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
}
{
// Vertical 10111010000.
ByteMatrix matrix = new ByteMatrix(1, 11);
matrix = new ByteMatrix(1, 11);
matrix.set(0, 0, 1);
matrix.set(0, 1, 0);
matrix.set(0, 2, 1);
@ -167,26 +147,20 @@ public final class MaskUtilTestCase extends Assert {
matrix.set(0, 10, 0);
assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
}
}
@Test
public void testApplyMaskPenaltyRule4() {
{
// Dark cell ratio = 0%
ByteMatrix matrix = new ByteMatrix(1, 1);
matrix.set(0, 0, 0);
assertEquals(100, MaskUtil.applyMaskPenaltyRule4(matrix));
}
{
// Dark cell ratio = 5%
ByteMatrix matrix = new ByteMatrix(2, 1);
matrix = new ByteMatrix(2, 1);
matrix.set(0, 0, 0);
matrix.set(0, 0, 1);
assertEquals(0, MaskUtil.applyMaskPenaltyRule4(matrix));
}
{
// Dark cell ratio = 66.67%
ByteMatrix matrix = new ByteMatrix(6, 1);
matrix = new ByteMatrix(6, 1);
matrix.set(0, 0, 0);
matrix.set(1, 0, 1);
matrix.set(2, 0, 1);
@ -195,14 +169,11 @@ public final class MaskUtilTestCase extends Assert {
matrix.set(5, 0, 0);
assertEquals(30, MaskUtil.applyMaskPenaltyRule4(matrix));
}
}
private static boolean TestGetDataMaskBitInternal(int maskPattern,
int[][] expected) {
private static boolean TestGetDataMaskBitInternal(int maskPattern, int[][] expected) {
for (int x = 0; x < 6; ++x) {
for (int y = 0; y < 6; ++y) {
if ((expected[y][x] == 1) !=
MaskUtil.getDataMaskBit(maskPattern, x, y)) {
if ((expected[y][x] == 1) != MaskUtil.getDataMaskBit(maskPattern, x, y)) {
return false;
}
}