66d9c61f58f7be3cf07008c327211d93f9f00906
[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
26   // JISX0510:2004 (p.45) for details.  Basically it applies four
27   // rules and summate all penalties.
28   public static int CalculateMaskPenalty(final Matrix matrix) {
29     int penalty = 0;
30     penalty += ApplyMaskPenaltyRule1(matrix);
31     penalty += ApplyMaskPenaltyRule2(matrix);
32     penalty += ApplyMaskPenaltyRule3(matrix);
33     penalty += ApplyMaskPenaltyRule4(matrix);
34     return penalty;
35   }
36
37   // Apply mask penalty rule 1 and return the penalty.
38   // Find repetitive cells with the same color and give penalty to
39   // them.  Example: 00000 or 11111.
40   public static int ApplyMaskPenaltyRule1(final Matrix matrix) {
41     final int penalty = (ApplyMaskPenaltyRule1Internal(matrix, true) +
42         ApplyMaskPenaltyRule1Internal(matrix, false));
43     Debug.LOG_INFO("\tApplyMaskPenaltyRule1: " + penalty);
44     return penalty;
45   }
46
47   // Apply mask penalty rule 2 and return the penalty.
48   // Find 2x2 blocks with the same color and give penalty to them.
49   //
50   // JAVAPORT: Consider using Matrix.getArray() instead.
51   public static int ApplyMaskPenaltyRule2(final Matrix matrix) {
52     int penalty = 0;
53     for (int y = 0; y < matrix.height() - 1; ++y) {
54       for (int x = 0; x < matrix.width() - 1; ++x) {
55         int value = matrix.get(y, x);
56         if (value == matrix.get(y + 0, x + 1) &&
57             value == matrix.get(y + 1, x + 0) &&
58             value == matrix.get(y + 1, x + 1)) {
59           penalty += 3;
60         }
61       }
62     }
63     Debug.LOG_INFO("\tApplyMaskPenaltyRule2: " + penalty);
64     return penalty;
65   }
66
67   // Apply mask penalty rule 3 and return the penalty.
68   // Find consecutive cells of 00001011101 or 10111010000, and give
69   // penalty to them.  If we find patterns like 000010111010000, we give
70   // penalties twice (i.e. 40 * 2).
71   //
72   // JAVAPORT: This many calls to Matrix.get() looks expensive. We should profile and consider
73   // adding a byte[][] Matrix.getArray() method, then using that array locally.
74   public static int ApplyMaskPenaltyRule3(final Matrix matrix) {
75     int penalty = 0;
76     for (int y = 0; y < matrix.height(); ++y) {
77       for (int x = 0; x < matrix.width(); ++x) {
78         // Tried to simplify following conditions but failed.
79         if (x + 6 < matrix.width() &&
80             matrix.get(y, x +  0) == 1 &&
81             matrix.get(y, x +  1) == 0 &&
82             matrix.get(y, x +  2) == 1 &&
83             matrix.get(y, x +  3) == 1 &&
84             matrix.get(y, x +  4) == 1 &&
85             matrix.get(y, x +  5) == 0 &&
86             matrix.get(y, x +  6) == 1 &&
87             ((x + 10 < matrix.width() &&
88                 matrix.get(y, x +  7) == 0 &&
89                 matrix.get(y, x +  8) == 0 &&
90                 matrix.get(y, x +  9) == 0 &&
91                 matrix.get(y, x + 10) == 0) ||
92                 (x - 4 >= 0 &&
93                     matrix.get(y, x -  1) == 0 &&
94                     matrix.get(y, x -  2) == 0 &&
95                     matrix.get(y, x -  3) == 0 &&
96                     matrix.get(y, x -  4) == 0))) {
97           penalty += 40;
98         }
99         if (y + 6 < matrix.height() &&
100             matrix.get(y +  0, x) == 1  &&
101             matrix.get(y +  1, x) == 0  &&
102             matrix.get(y +  2, x) == 1  &&
103             matrix.get(y +  3, x) == 1  &&
104             matrix.get(y +  4, x) == 1  &&
105             matrix.get(y +  5, x) == 0  &&
106             matrix.get(y +  6, x) == 1 &&
107             ((y + 10 < matrix.height() &&
108                 matrix.get(y +  7, x) == 0 &&
109                 matrix.get(y +  8, x) == 0 &&
110                 matrix.get(y +  9, x) == 0 &&
111                 matrix.get(y + 10, x) == 0) ||
112                 (y - 4 >= 0 &&
113                     matrix.get(y -  1, x) == 0 &&
114                     matrix.get(y -  2, x) == 0 &&
115                     matrix.get(y -  3, x) == 0 &&
116                     matrix.get(y -  4, x) == 0))) {
117           penalty += 40;
118         }
119       }
120     }
121     Debug.LOG_INFO("\tApplyMaskPenaltyRule3: " + penalty);
122     return penalty;
123   }
124
125   // Apply mask penalty rule 4 and return the penalty.
126   // Calculate the ratio of dark cells and give penalty if the ratio
127   // is far from 50%.  It gives 10 penalty for 5% distance.
128   // Examples:
129   // -   0% => 100
130   // -  40% =>  20
131   // -  45% =>  10
132   // -  50% =>   0
133   // -  55% =>  10
134   // -  55% =>  20
135   // - 100% => 100
136   public static int ApplyMaskPenaltyRule4(final Matrix matrix) {
137     int num_dark_cells = 0;
138     for (int y = 0; y < matrix.height(); ++y) {
139       for (int x = 0; x < matrix.width(); ++x) {
140         if (matrix.get(y, x) == 1) {
141           num_dark_cells += 1;
142         }
143       }
144     }
145     final int num_total_cells = matrix.height() * matrix.width();
146     double dark_ratio = (double) num_dark_cells / num_total_cells;
147     final int penalty = Math.abs((int) (dark_ratio * 100 - 50)) / 5 * 10;
148     Debug.LOG_INFO("\tApplyMaskPenaltyRule4: " + penalty);
149     return penalty;
150   }
151
152   // Return the mask bit for "mask_pattern" at "x" and "y".
153   // See 8.8 of JISX0510:2004 for mask pattern conditions.
154   public static int GetDataMaskBit(final int mask_pattern, final int x, final int y) {
155     Debug.DCHECK(QRCode.IsValidMaskPattern(mask_pattern));
156     switch (mask_pattern) {
157       case 0:
158         return ((y + x) % 2 == 0) ? 1 : 0;
159       case 1:
160         return (y % 2 == 0) ? 1 : 0;
161       case 2:
162         return (x % 3 == 0) ? 1 : 0;
163       case 3:
164         return ((y + x) % 3 == 0) ? 1 : 0;
165       case 4:
166         return (((y / 2) + (x / 3)) % 2 == 0) ? 1 : 0;
167       case 5:
168         return (((y * x) % 2) + ((y * x) % 3) == 0) ? 1 : 0;
169       case 6:
170         return ((((y * x) % 2) + ((y * x) % 3)) % 2 == 0) ? 1 : 0;
171       case 7:
172         return ((((y * x) % 3) + ((y + x) % 2)) % 2 == 0) ? 1 : 0;
173       default:
174         ;
175     }
176     Debug.LOG_ERROR("invalid mask pattern: " + mask_pattern);
177     return -1;
178   }
179
180   // Helper function for ApplyMaskPenaltyRule1.  We need this for doing
181   // this calculation in both vertical and horizontal orders
182   // respectively.
183   private static int ApplyMaskPenaltyRule1Internal(final Matrix matrix, boolean is_horizontal) {
184     int penalty = 0;
185     int num_same_bit_cells = 0;
186     int prev_bit = -1;
187     // Horizontal mode:
188     //   for (int i = 0; i < matrix.height(); ++i) {
189     //     for (int j = 0; j < matrix.width(); ++j) {
190     //       int bit = matrix.get(i, j);
191     // Vertical mode:
192     //   for (int i = 0; i < matrix.width(); ++i) {
193     //     for (int j = 0; j < matrix.height(); ++j) {
194     //       int bit = matrix.get(j, i);
195     final int i_limit = is_horizontal ? matrix.height() : matrix.width();
196     final int j_limit = is_horizontal ? matrix.width() : matrix.height();
197     for (int i = 0; i < i_limit; ++i) {
198       for (int j = 0; j < j_limit; ++j) {
199         final int bit = is_horizontal ? matrix.get(i, j) : matrix.get(j, i);
200         if (bit == prev_bit) {
201           num_same_bit_cells += 1;
202           // Found five repetitive cells with the same color (bit).
203           // We'll give penalty of 3.
204           if (num_same_bit_cells == 5) {
205             penalty += 3;
206           } else if (num_same_bit_cells > 5) {
207             // After five repetitive cells, we'll add the penalty one
208             // by one.
209             penalty += 1;
210           }
211         } else {
212           num_same_bit_cells = 1;  // Include the cell itself.
213           prev_bit = bit;
214         }
215       }
216       num_same_bit_cells = 0;  // Clear at each row/column.
217     }
218     return penalty;
219   }
220
221 }