OLD | NEW |
| (Empty) |
1 // Copyright 2014 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 // Original code is licensed as follows: | |
7 /* | |
8 * Copyright 2008 ZXing authors | |
9 * | |
10 * Licensed under the Apache License, Version 2.0 (the "License"); | |
11 * you may not use this file except in compliance with the License. | |
12 * You may obtain a copy of the License at | |
13 * | |
14 * http://www.apache.org/licenses/LICENSE-2.0 | |
15 * | |
16 * Unless required by applicable law or agreed to in writing, software | |
17 * distributed under the License is distributed on an "AS IS" BASIS, | |
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
19 * See the License for the specific language governing permissions and | |
20 * limitations under the License. | |
21 */ | |
22 | |
23 #include "barcode.h" | |
24 #include "include/BC_CommonByteMatrix.h" | |
25 #include "include/BC_QRCoderErrorCorrectionLevel.h" | |
26 #include "include/BC_QRCoder.h" | |
27 #include "include/BC_QRCoderMaskUtil.h" | |
28 CBC_QRCoderMaskUtil::CBC_QRCoderMaskUtil() | |
29 { | |
30 } | |
31 CBC_QRCoderMaskUtil::~CBC_QRCoderMaskUtil() | |
32 { | |
33 } | |
34 FX_INT32 CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule1(CBC_CommonByteMatrix* matrix
) | |
35 { | |
36 return ApplyMaskPenaltyRule1Internal(matrix, TRUE) + | |
37 ApplyMaskPenaltyRule1Internal(matrix, FALSE); | |
38 } | |
39 FX_INT32 CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule2(CBC_CommonByteMatrix* matrix
) | |
40 { | |
41 FX_INT32 penalty = 0; | |
42 FX_BYTE* array = matrix->GetArray(); | |
43 FX_INT32 width = matrix->GetWidth(); | |
44 FX_INT32 height = matrix->GetHeight(); | |
45 for(FX_INT32 y = 0; y < height - 1; y++) { | |
46 for(FX_INT32 x = 0; x < width - 1; x++) { | |
47 FX_INT32 value = array[y * width + x]; | |
48 if(value == array[y * width + x + 1] && | |
49 value == array[(y + 1) * width + x] && | |
50 value == array[(y + 1) * width + x + 1]) { | |
51 penalty ++; | |
52 } | |
53 } | |
54 } | |
55 return 3 * penalty; | |
56 } | |
57 FX_INT32 CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule3(CBC_CommonByteMatrix* matrix
) | |
58 { | |
59 FX_INT32 penalty = 0; | |
60 FX_BYTE* array = matrix->GetArray(); | |
61 FX_INT32 width = matrix->GetWidth(); | |
62 FX_INT32 height = matrix->GetHeight(); | |
63 for (FX_INT32 y = 0; y < height; ++y) { | |
64 for (FX_INT32 x = 0; x < width; ++x) { | |
65 if (x == 0 && ((y >= 0 && y <= 6) || (y >= height - 7 && y <= height
- 1))) { | |
66 continue; | |
67 } | |
68 if (x == width - 7 && (y >= 0 && y <= 6)) { | |
69 continue; | |
70 } | |
71 if (y == 0 && ((x >= 0 && x <= 6) || (x >= width - 7 && x <= width -
1))) { | |
72 continue; | |
73 } | |
74 if (y == height - 7 && (x >= 0 && x <= 6)) { | |
75 continue; | |
76 } | |
77 if (x + 6 < width && | |
78 array[y * width + x] == 1 && | |
79 array[y * width + x + 1] == 0 && | |
80 array[y * width + x + 2] == 1 && | |
81 array[y * width + x + 3] == 1 && | |
82 array[y * width + x + 4] == 1 && | |
83 array[y * width + x + 5] == 0 && | |
84 array[y * width + x + 6] == 1 && | |
85 ((x + 10 < width && | |
86 array[y * width + x + 7] == 0 && | |
87 array[y * width + x + 8] == 0 && | |
88 array[y * width + x + 9] == 0 && | |
89 array[y * width + x + 10] == 0) || | |
90 (x - 4 >= 0 && | |
91 array[y * width + x - 1] == 0 && | |
92 array[y * width + x - 2] == 0 && | |
93 array[y * width + x - 3] == 0 && | |
94 array[y * width + x - 4] == 0))) { | |
95 penalty += 40; | |
96 } | |
97 if (y + 6 < height && | |
98 array[y * width + x] == 1 && | |
99 array[(y + 1) * width + x] == 0 && | |
100 array[(y + 2) * width + x] == 1 && | |
101 array[(y + 3) * width + x] == 1 && | |
102 array[(y + 4) * width + x] == 1 && | |
103 array[(y + 5) * width + x] == 0 && | |
104 array[(y + 6) * width + x] == 1 && | |
105 ((y + 10 < height && | |
106 array[(y + 7) * width + x] == 0 && | |
107 array[(y + 8) * width + x] == 0 && | |
108 array[(y + 9) * width + x] == 0 && | |
109 array[(y + 10) * width + x] == 0) || | |
110 (y - 4 >= 0 && | |
111 array[(y - 1) * width + x] == 0 && | |
112 array[(y - 2) * width + x] == 0 && | |
113 array[(y - 3) * width + x] == 0 && | |
114 array[(y - 4) * width + x] == 0))) { | |
115 penalty += 40; | |
116 } | |
117 } | |
118 } | |
119 return penalty; | |
120 } | |
121 FX_INT32 CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule4(CBC_CommonByteMatrix* matrix
) | |
122 { | |
123 FX_INT32 numDarkCells = 0; | |
124 FX_BYTE* array = matrix->GetArray(); | |
125 FX_INT32 width = matrix->GetWidth(); | |
126 FX_INT32 height = matrix->GetHeight(); | |
127 for (FX_INT32 y = 0; y < height; ++y) { | |
128 for (FX_INT32 x = 0; x < width; ++x) { | |
129 if (array[y * width + x] == 1) { | |
130 numDarkCells += 1; | |
131 } | |
132 } | |
133 } | |
134 FX_INT32 numTotalCells = matrix->GetHeight() * matrix->GetWidth(); | |
135 double darkRatio = (double) numDarkCells / numTotalCells; | |
136 return abs( (FX_INT32) (darkRatio * 100 - 50) / 5 ) * 5 * 10; | |
137 } | |
138 FX_BOOL CBC_QRCoderMaskUtil::GetDataMaskBit(FX_INT32 maskPattern, FX_INT32 x, FX
_INT32 y, FX_INT32 &e) | |
139 { | |
140 if(!CBC_QRCoder::IsValidMaskPattern(maskPattern)) { | |
141 e = (BCExceptionInvalidateMaskPattern); | |
142 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); | |
143 } | |
144 FX_INT32 intermediate = 0, temp = 0; | |
145 switch(maskPattern) { | |
146 case 0: | |
147 intermediate = (y + x) & 0x1; | |
148 break; | |
149 case 1: | |
150 intermediate = y & 0x1; | |
151 break; | |
152 case 2: | |
153 intermediate = x % 3; | |
154 break; | |
155 case 3: | |
156 intermediate = (y + x) % 3; | |
157 break; | |
158 case 4: | |
159 intermediate = ((y >> 1) + (x / 3)) & 0x1; | |
160 break; | |
161 case 5: | |
162 temp = y * x; | |
163 intermediate = (temp & 0x1) + (temp % 3); | |
164 break; | |
165 case 6: | |
166 temp = y * x; | |
167 intermediate = (((temp & 0x1) + (temp % 3)) & 0x1); | |
168 break; | |
169 case 7: | |
170 temp = y * x; | |
171 intermediate = (((temp % 3) + ((y + x) & 0x1)) & 0x1); | |
172 break; | |
173 default: { | |
174 e = BCExceptionInvalidateMaskPattern; | |
175 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); | |
176 } | |
177 } | |
178 return intermediate == 0; | |
179 } | |
180 FX_INT32 CBC_QRCoderMaskUtil::ApplyMaskPenaltyRule1Internal(CBC_CommonByteMatrix
* matrix, FX_BOOL isHorizontal) | |
181 { | |
182 FX_INT32 penalty = 0; | |
183 FX_INT32 numSameBitCells = 0; | |
184 FX_INT32 prevBit = -1; | |
185 FX_INT32 width = matrix->GetWidth(); | |
186 FX_INT32 height = matrix->GetHeight(); | |
187 FX_INT32 iLimit = isHorizontal ? height : width; | |
188 FX_INT32 jLimit = isHorizontal ? width : height; | |
189 FX_BYTE* array = matrix->GetArray(); | |
190 for (FX_INT32 i = 0; i < iLimit; ++i) { | |
191 for (FX_INT32 j = 0; j < jLimit; ++j) { | |
192 FX_INT32 bit = isHorizontal ? array[i * width + j] : array[j * width
+ i]; | |
193 if (bit == prevBit) { | |
194 numSameBitCells += 1; | |
195 if (numSameBitCells == 5) { | |
196 penalty += 3; | |
197 } else if (numSameBitCells > 5) { | |
198 penalty += 1; | |
199 } | |
200 } else { | |
201 numSameBitCells = 1; | |
202 prevBit = bit; | |
203 } | |
204 } | |
205 numSameBitCells = 0; | |
206 } | |
207 return penalty; | |
208 } | |
OLD | NEW |