Got MatrixUtil to compile, and refactored some methods out of it, and into Matrix...
[zxing.git] / core / src / com / google / zxing / qrcode / encoder / MaskUtil.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.qrcode.encoder;
18
19 /**
20  * @author satorux@google.com (Satoru Takabayashi) - creator
21  * @author dswitkin@google.com (Daniel Switkin) - ported from C++
22  */
23 public final class MaskUtil {
24
25   // The mask penalty calculation is complicated.  See Table 21 of JISX0510:2004 (p.45) for details.
26   // Basically it applies four rules and summate all penalties.
27   public static int CalculateMaskPenalty(final Matrix matrix) {
28     int penalty = 0;
29     penalty += ApplyMaskPenaltyRule1(matrix);
30     penalty += ApplyMaskPenaltyRule2(matrix);
31     penalty += ApplyMaskPenaltyRule3(matrix);
32     penalty += ApplyMaskPenaltyRule4(matrix);
33     return penalty;
34   }
35
36   // Apply mask penalty rule 1 and return the penalty. Find repetitive cells with the same color and
37   // give penalty to them. Example: 00000 or 11111.
38   public static int ApplyMaskPenaltyRule1(final Matrix matrix) {
39     final int penalty = (ApplyMaskPenaltyRule1Internal(matrix, true) +
40         ApplyMaskPenaltyRule1Internal(matrix, false));
41     Debug.LOG_INFO("\tApplyMaskPenaltyRule1: " + penalty);
42     return penalty;
43   }
44
45   // Apply mask penalty rule 2 and return the penalty. Find 2x2 blocks with the same color and give
46   // penalty to them.
47   //
48   // JAVAPORT: Consider using Matrix.getArray() instead.
49   public static int ApplyMaskPenaltyRule2(final Matrix matrix) {
50     int penalty = 0;
51     for (int y = 0; y < matrix.height() - 1; ++y) {
52       for (int x = 0; x < matrix.width() - 1; ++x) {
53         int value = matrix.get(y, x);
54         if (value == matrix.get(y + 0, x + 1) &&
55             value == matrix.get(y + 1, x + 0) &&
56             value == matrix.get(y + 1, x + 1)) {
57           penalty += 3;
58         }
59       }
60     }
61     Debug.LOG_INFO("\tApplyMaskPenaltyRule2: " + penalty);
62     return penalty;
63   }
64
65   // Apply mask penalty rule 3 and return the penalty. Find consecutive cells of 00001011101 or
66   // 10111010000, and give penalty to them.  If we find patterns like 000010111010000, we give
67   // penalties twice (i.e. 40 * 2).
68   //
69   // JAVAPORT: This many calls to Matrix.get() looks expensive. We should profile and consider
70   // adding a byte[][] Matrix.getArray() method, then using that array locally.
71   public static int ApplyMaskPenaltyRule3(final Matrix matrix) {
72     int penalty = 0;
73     for (int y = 0; y < matrix.height(); ++y) {
74       for (int x = 0; x < matrix.width(); ++x) {
75         // Tried to simplify following conditions but failed.
76         if (x + 6 < matrix.width() &&
77             matrix.get(y, x +  0) == 1 &&
78             matrix.get(y, x +  1) == 0 &&
79             matrix.get(y, x +  2) == 1 &&
80             matrix.get(y, x +  3) == 1 &&
81             matrix.get(y, x +  4) == 1 &&
82             matrix.get(y, x +  5) == 0 &&
83             matrix.get(y, x +  6) == 1 &&
84             ((x + 10 < matrix.width() &&
85                 matrix.get(y, x +  7) == 0 &&
86                 matrix.get(y, x +  8) == 0 &&
87                 matrix.get(y, x +  9) == 0 &&
88                 matrix.get(y, x + 10) == 0) ||
89                 (x - 4 >= 0 &&
90                     matrix.get(y, x -  1) == 0 &&
91                     matrix.get(y, x -  2) == 0 &&
92                     matrix.get(y, x -  3) == 0 &&
93                     matrix.get(y, x -  4) == 0))) {
94           penalty += 40;
95         }
96         if (y + 6 < matrix.height() &&
97             matrix.get(y +  0, x) == 1  &&
98             matrix.get(y +  1, x) == 0  &&
99             matrix.get(y +  2, x) == 1  &&
100             matrix.get(y +  3, x) == 1  &&
101             matrix.get(y +  4, x) == 1  &&
102             matrix.get(y +  5, x) == 0  &&
103             matrix.get(y +  6, x) == 1 &&
104             ((y + 10 < matrix.height() &&
105                 matrix.get(y +  7, x) == 0 &&
106                 matrix.get(y +  8, x) == 0 &&
107                 matrix.get(y +  9, x) == 0 &&
108                 matrix.get(y + 10, x) == 0) ||
109                 (y - 4 >= 0 &&
110                     matrix.get(y -  1, x) == 0 &&
111                     matrix.get(y -  2, x) == 0 &&
112                     matrix.get(y -  3, x) == 0 &&
113                     matrix.get(y -  4, x) == 0))) {
114           penalty += 40;
115         }
116       }
117     }
118     Debug.LOG_INFO("\tApplyMaskPenaltyRule3: " + penalty);
119     return penalty;
120   }
121
122   // Apply mask penalty rule 4 and return the penalty. Calculate the ratio of dark cells and give
123   // penalty if the ratio is far from 50%. It gives 10 penalty for 5% distance. Examples:
124   // -   0% => 100
125   // -  40% =>  20
126   // -  45% =>  10
127   // -  50% =>   0
128   // -  55% =>  10
129   // -  55% =>  20
130   // - 100% => 100
131   public static int ApplyMaskPenaltyRule4(final Matrix matrix) {
132     int num_dark_cells = 0;
133     for (int y = 0; y < matrix.height(); ++y) {
134       for (int x = 0; x < matrix.width(); ++x) {
135         if (matrix.get(y, x) == 1) {
136           num_dark_cells += 1;
137         }
138       }
139     }
140     final int num_total_cells = matrix.height() * matrix.width();
141     double dark_ratio = (double) num_dark_cells / num_total_cells;
142     final int penalty = Math.abs((int) (dark_ratio * 100 - 50)) / 5 * 10;
143     Debug.LOG_INFO("\tApplyMaskPenaltyRule4: " + penalty);
144     return penalty;
145   }
146
147   // Return the mask bit for "mask_pattern" at "x" and "y". See 8.8 of JISX0510:2004 for mask
148   // pattern conditions.
149   public static int GetDataMaskBit(final int mask_pattern, final int x, final int y) {
150     Debug.DCHECK(QRCode.IsValidMaskPattern(mask_pattern));
151     switch (mask_pattern) {
152       case 0:
153         return ((y + x) % 2 == 0) ? 1 : 0;
154       case 1:
155         return (y % 2 == 0) ? 1 : 0;
156       case 2:
157         return (x % 3 == 0) ? 1 : 0;
158       case 3:
159         return ((y + x) % 3 == 0) ? 1 : 0;
160       case 4:
161         return (((y / 2) + (x / 3)) % 2 == 0) ? 1 : 0;
162       case 5:
163         return (((y * x) % 2) + ((y * x) % 3) == 0) ? 1 : 0;
164       case 6:
165         return ((((y * x) % 2) + ((y * x) % 3)) % 2 == 0) ? 1 : 0;
166       case 7:
167         return ((((y * x) % 3) + ((y + x) % 2)) % 2 == 0) ? 1 : 0;
168       default:
169         ;
170     }
171     Debug.LOG_ERROR("invalid mask pattern: " + mask_pattern);
172     return -1;
173   }
174
175   // Helper function for ApplyMaskPenaltyRule1. We need this for doing this calculation in both
176   // vertical and horizontal orders respectively.
177   private static int ApplyMaskPenaltyRule1Internal(final Matrix matrix, boolean is_horizontal) {
178     int penalty = 0;
179     int num_same_bit_cells = 0;
180     int prev_bit = -1;
181     // Horizontal mode:
182     //   for (int i = 0; i < matrix.height(); ++i) {
183     //     for (int j = 0; j < matrix.width(); ++j) {
184     //       int bit = matrix.get(i, j);
185     // Vertical mode:
186     //   for (int i = 0; i < matrix.width(); ++i) {
187     //     for (int j = 0; j < matrix.height(); ++j) {
188     //       int bit = matrix.get(j, i);
189     final int i_limit = is_horizontal ? matrix.height() : matrix.width();
190     final int j_limit = is_horizontal ? matrix.width() : matrix.height();
191     for (int i = 0; i < i_limit; ++i) {
192       for (int j = 0; j < j_limit; ++j) {
193         final int bit = is_horizontal ? matrix.get(i, j) : matrix.get(j, i);
194         if (bit == prev_bit) {
195           num_same_bit_cells += 1;
196           // Found five repetitive cells with the same color (bit).
197           // We'll give penalty of 3.
198           if (num_same_bit_cells == 5) {
199             penalty += 3;
200           } else if (num_same_bit_cells > 5) {
201             // After five repetitive cells, we'll add the penalty one
202             // by one.
203             penalty += 1;
204           }
205         } else {
206           num_same_bit_cells = 1;  // Include the cell itself.
207           prev_bit = bit;
208         }
209       }
210       num_same_bit_cells = 0;  // Clear at each row/column.
211     }
212     return penalty;
213   }
214
215 }