Issue 1758: revert last change, applying penalty at edge is necessary. Try new change from original code to avoid applying in the "2 1 3 1 1" case

git-svn-id: https://zxing.googlecode.com/svn/trunk@2867 59b500cc-1b3d-0410-9834-0bbf25fbcc57
diff --git a/core/src/com/google/zxing/qrcode/encoder/MaskUtil.java b/core/src/com/google/zxing/qrcode/encoder/MaskUtil.java
index 4b26963..aeed78f 100644
--- a/core/src/com/google/zxing/qrcode/encoder/MaskUtil.java
+++ b/core/src/com/google/zxing/qrcode/encoder/MaskUtil.java
@@ -75,44 +75,48 @@
     for (int y = 0; y < height; y++) {
       for (int x = 0; x < width; x++) {
         byte[] arrayY = array[y];  // We can at least optimize this access
-        if (x + 8 < width &&
-            arrayY[x]     == 0 &&
-            arrayY[x + 1] == 1 &&
-            arrayY[x + 2] == 0 &&
-            arrayY[x + 3] == 1 &&
-            arrayY[x + 4] == 1 &&
-            arrayY[x + 5] == 1 &&
-            arrayY[x + 6] == 0 &&
-            arrayY[x + 7] == 1 &&
-            arrayY[x + 8] == 0 &&
-            ((x + 11 < width &&
+        if (x + 6 < width &&
+            (x == 0 || arrayY[x-1] == 0) &&
+            arrayY[x] == 1 &&
+            arrayY[x +  1] == 0 &&
+            arrayY[x +  2] == 1 &&
+            arrayY[x +  3] == 1 &&
+            arrayY[x +  4] == 1 &&
+            arrayY[x +  5] == 0 &&
+            arrayY[x +  6] == 1 &&
+            (x + 7 >= width || arrayY[x+7] == 0) &&
+            ((x + 10 < width &&
+                arrayY[x +  7] == 0 &&
+                arrayY[x +  8] == 0 &&
                 arrayY[x +  9] == 0 &&
-                arrayY[x + 10] == 0 &&
-                arrayY[x + 11] == 0) ||
-             (x - 3 >= 0 &&
-                arrayY[x -  1] == 0 &&
-                arrayY[x -  2] == 0 &&
-                arrayY[x -  3] == 0))) {
+                arrayY[x + 10] == 0) ||
+                (x - 4 >= 0 &&
+                    arrayY[x -  1] == 0 &&
+                    arrayY[x -  2] == 0 &&
+                    arrayY[x -  3] == 0 &&
+                    arrayY[x -  4] == 0))) {
           numPenalties++;
         }
-        if (y + 8 < height &&
-            array[y][x]     == 0 &&
-            array[y + 1][x] == 1 &&
-            array[y + 2][x] == 0 &&
-            array[y + 3][x] == 1 &&
-            array[y + 4][x] == 1 &&
-            array[y + 5][x] == 1 &&
-            array[y + 6][x] == 0 &&
-            array[y + 7][x] == 1 &&
-            array[y + 8][x] == 0 &&
-            ((y + 11 < height &&
+        if (y + 6 < height &&
+            (y == 0 || array[y-1][x] == 0) &&
+            array[y][x] == 1  &&
+            array[y +  1][x] == 0  &&
+            array[y +  2][x] == 1  &&
+            array[y +  3][x] == 1  &&
+            array[y +  4][x] == 1  &&
+            array[y +  5][x] == 0  &&
+            array[y +  6][x] == 1 &&
+            (y + 7 >= height || array[y+7][x] == 0) &&
+            ((y + 10 < height &&
+                array[y +  7][x] == 0 &&
+                array[y +  8][x] == 0 &&
                 array[y +  9][x] == 0 &&
-                array[y + 10][x] == 0 &&
-                array[y + 11][x] == 0) ||
-             (y - 3 >= 0 &&
-                array[y -  1][x] == 0 &&
-                array[y -  2][x] == 0 &&
-                array[y -  3][x] == 0))) {
+                array[y + 10][x] == 0) ||
+                (y - 4 >= 0 &&
+                    array[y -  1][x] == 0 &&
+                    array[y -  2][x] == 0 &&
+                    array[y -  3][x] == 0 &&
+                    array[y -  4][x] == 0))) {
           numPenalties++;
         }
       }
diff --git a/core/test/data/golden/qrcode/renderer-test-02.png b/core/test/data/golden/qrcode/renderer-test-02.png
new file mode 100644
index 0000000..5fd2504
--- /dev/null
+++ b/core/test/data/golden/qrcode/renderer-test-02.png
Binary files differ
diff --git a/core/test/data/golden/qrcode/renderer-test-03.png b/core/test/data/golden/qrcode/renderer-test-03.png
new file mode 100644
index 0000000..884024d
--- /dev/null
+++ b/core/test/data/golden/qrcode/renderer-test-03.png
Binary files differ
diff --git a/core/test/src/com/google/zxing/qrcode/QRCodeWriterTestCase.java b/core/test/src/com/google/zxing/qrcode/QRCodeWriterTestCase.java
index 146025c..a5d9739 100644
--- a/core/test/src/com/google/zxing/qrcode/QRCodeWriterTestCase.java
+++ b/core/test/src/com/google/zxing/qrcode/QRCodeWriterTestCase.java
@@ -128,6 +128,18 @@
   public void testRegressionTest() throws Exception {
     compareToGoldenFile("http://www.google.com/", ErrorCorrectionLevel.M, 99,
         "renderer-test-01.png");
+
+    compareToGoldenFile("12345", ErrorCorrectionLevel.L, 58, "renderer-test-02.png");
+
+    // Test in Katakana in Shift_JIS.
+    // TODO: this test is bogus now that byte mode has been basically fixed to assuming ISO-8859-1 encoding
+    //  The real solution is to implement Kanji mode, in which case the golden file will be wrong again
+    /*
+    compareToGoldenFile(
+        new String(new byte[] {(byte)0x83, 0x65, (byte)0x83, 0x58, (byte)0x83, 0x67}, "Shift_JIS"),
+        ErrorCorrectionLevel.H, 145,
+        "renderer-test-03.png");
+     */
   }
 
 }
diff --git a/core/test/src/com/google/zxing/qrcode/encoder/EncoderTestCase.java b/core/test/src/com/google/zxing/qrcode/encoder/EncoderTestCase.java
index 04367ff..fe3d134 100644
--- a/core/test/src/com/google/zxing/qrcode/encoder/EncoderTestCase.java
+++ b/core/test/src/com/google/zxing/qrcode/encoder/EncoderTestCase.java
@@ -137,29 +137,29 @@
       " mode: BYTE\n" +
       " ecLevel: H\n" +
       " version: 1\n" +
-      " maskPattern: 0\n" +
+      " maskPattern: 3\n" +
       " matrix:\n" +
-      " 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 1 1 1 1 1 1\n" +
-      " 1 0 0 0 0 0 1 0 0 1 1 0 1 0 1 0 0 0 0 0 1\n" +
-      " 1 0 1 1 1 0 1 0 0 1 0 1 1 0 1 0 1 1 1 0 1\n" +
-      " 1 0 1 1 1 0 1 0 1 1 1 1 0 0 1 0 1 1 1 0 1\n" +
-      " 1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1\n" +
-      " 1 0 0 0 0 0 1 0 0 1 1 1 1 0 1 0 0 0 0 0 1\n" +
+      " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1\n" +
+      " 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 1\n" +
+      " 1 0 1 1 1 0 1 0 0 1 0 1 0 0 1 0 1 1 1 0 1\n" +
+      " 1 0 1 1 1 0 1 0 0 1 1 0 1 0 1 0 1 1 1 0 1\n" +
+      " 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1\n" +
+      " 1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1\n" +
       " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
-      " 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0\n" +
-      " 0 0 1 0 1 1 1 0 1 1 0 0 1 1 0 0 0 1 0 0 1\n" +
-      " 1 1 1 1 1 1 0 1 0 0 1 0 1 1 0 1 0 0 0 0 0\n" +
-      " 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 0 1 0 0 1 1\n" +
-      " 1 1 0 1 0 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 0\n" +
-      " 0 0 1 1 0 0 1 1 0 0 1 0 0 0 1 0 1 0 1 0 1\n" +
-      " 0 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 0 0 1 0\n" +
-      " 1 1 1 1 1 1 1 0 0 1 1 0 0 1 0 1 0 0 0 1 1\n" +
-      " 1 0 0 0 0 0 1 0 1 0 1 1 1 0 1 1 1 0 0 1 1\n" +
-      " 1 0 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 0 0\n" +
-      " 1 0 1 1 1 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 0\n" +
-      " 1 0 1 1 1 0 1 0 1 0 1 0 0 1 1 1 0 0 0 0 1\n" +
-      " 1 0 0 0 0 0 1 0 0 1 0 1 1 1 0 1 1 0 1 0 1\n" +
-      " 1 1 1 1 1 1 1 0 0 1 0 1 0 0 1 1 1 0 1 1 1\n" +
+      " 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0\n" +
+      " 0 0 1 1 0 0 1 1 1 1 0 0 0 1 1 0 1 0 0 0 0\n" +
+      " 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 1 0 1 1 1 0\n" +
+      " 0 1 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 1 1 1 1\n" +
+      " 1 1 0 0 1 0 0 1 1 0 0 1 1 1 1 0 1 0 1 1 0\n" +
+      " 0 0 0 0 1 0 1 1 1 1 0 0 0 0 0 1 0 0 1 0 0\n" +
+      " 0 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 0 0 0 1\n" +
+      " 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 0 0 1 0 0\n" +
+      " 1 0 0 0 0 0 1 0 0 0 1 0 0 1 1 1 1 1 1 0 1\n" +
+      " 1 0 1 1 1 0 1 0 0 1 0 0 0 0 1 1 0 0 0 0 0\n" +
+      " 1 0 1 1 1 0 1 0 1 1 1 0 1 0 0 0 1 1 0 0 0\n" +
+      " 1 0 1 1 1 0 1 0 1 1 0 0 0 1 0 0 1 0 0 0 0\n" +
+      " 1 0 0 0 0 0 1 0 0 0 0 1 1 0 1 0 1 0 1 1 0\n" +
+      " 1 1 1 1 1 1 1 0 0 1 0 1 1 1 0 1 1 0 0 0 0\n" +
       ">>\n";
     assertEquals(expected, qrCode.toString());
   }
diff --git a/core/test/src/com/google/zxing/qrcode/encoder/MaskUtilTestCase.java b/core/test/src/com/google/zxing/qrcode/encoder/MaskUtilTestCase.java
index 7af8abd..e350343 100644
--- a/core/test/src/com/google/zxing/qrcode/encoder/MaskUtilTestCase.java
+++ b/core/test/src/com/google/zxing/qrcode/encoder/MaskUtilTestCase.java
@@ -91,7 +91,7 @@
   @Test
   public void testApplyMaskPenaltyRule3() {
     // Horizontal 00001011101.
-    ByteMatrix matrix = new ByteMatrix(12, 1);
+    ByteMatrix matrix = new ByteMatrix(11, 1);
     matrix.set(0, 0, 0);
     matrix.set(1, 0, 0);
     matrix.set(2, 0, 0);
@@ -103,25 +103,23 @@
     matrix.set(8, 0, 1);
     matrix.set(9, 0, 0);
     matrix.set(10, 0, 1);
-    matrix.set(11, 0, 0);
     assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
     // Horizontal 10111010000.
-    matrix = new ByteMatrix(12, 1);
-    matrix.set(0, 0, 0);
-    matrix.set(1, 0, 1);
-    matrix.set(2, 0, 0);
+    matrix = new ByteMatrix(11, 1);
+    matrix.set(0, 0, 1);
+    matrix.set(1, 0, 0);
+    matrix.set(2, 0, 1);
     matrix.set(3, 0, 1);
     matrix.set(4, 0, 1);
-    matrix.set(5, 0, 1);
-    matrix.set(6, 0, 0);
-    matrix.set(7, 0, 1);
+    matrix.set(5, 0, 0);
+    matrix.set(6, 0, 1);
+    matrix.set(7, 0, 0);
     matrix.set(8, 0, 0);
     matrix.set(9, 0, 0);
     matrix.set(10, 0, 0);
-    matrix.set(11, 0, 0);
     assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
     // Vertical 00001011101.
-    matrix = new ByteMatrix(1, 12);
+    matrix = new ByteMatrix(1, 11);
     matrix.set(0, 0, 0);
     matrix.set(0, 1, 0);
     matrix.set(0, 2, 0);
@@ -133,22 +131,20 @@
     matrix.set(0, 8, 1);
     matrix.set(0, 9, 0);
     matrix.set(0, 10, 1);
-    matrix.set(0, 11, 0);
     assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
     // Vertical 10111010000.
-    matrix = new ByteMatrix(1, 12);
-    matrix.set(0, 0, 0);
-    matrix.set(0, 1, 1);
-    matrix.set(0, 2, 0);
+    matrix = new ByteMatrix(1, 11);
+    matrix.set(0, 0, 1);
+    matrix.set(0, 1, 0);
+    matrix.set(0, 2, 1);
     matrix.set(0, 3, 1);
     matrix.set(0, 4, 1);
-    matrix.set(0, 5, 1);
-    matrix.set(0, 6, 0);
-    matrix.set(0, 7, 1);
+    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);
-    matrix.set(0, 11, 0);
     assertEquals(40, MaskUtil.applyMaskPenaltyRule3(matrix));
   }