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 2007 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_CommonBitMatrix.h" | |
25 #include "include/BC_DataMatrixVersion.h" | |
26 #include "include/BC_DataMatrixBitMatrixParser.h" | |
27 CBC_DataMatrixBitMatrixParser::CBC_DataMatrixBitMatrixParser() | |
28 { | |
29 m_mappingBitMatrix = NULL; | |
30 m_version = NULL; | |
31 m_readMappingMatrix = NULL; | |
32 } | |
33 void CBC_DataMatrixBitMatrixParser::Init(CBC_CommonBitMatrix *bitMatrix, FX_INT3
2 &e) | |
34 { | |
35 FX_INT32 dimension = bitMatrix->GetHeight(); | |
36 if (dimension < 8 || dimension > 144 || (dimension & 0x01) != 0) { | |
37 e = BCExceptionFormatException; | |
38 return; | |
39 } | |
40 m_version = ReadVersion(bitMatrix, e); | |
41 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
42 m_mappingBitMatrix = ExtractDataRegion(bitMatrix, e); | |
43 BC_EXCEPTION_CHECK_ReturnVoid(e); | |
44 m_readMappingMatrix = FX_NEW CBC_CommonBitMatrix(); | |
45 m_readMappingMatrix->Init(m_mappingBitMatrix->GetWidth(), m_mappingBitMatrix
->GetHeight()); | |
46 } | |
47 CBC_DataMatrixBitMatrixParser::~CBC_DataMatrixBitMatrixParser() | |
48 { | |
49 if(m_mappingBitMatrix != NULL) { | |
50 delete m_mappingBitMatrix; | |
51 } | |
52 m_mappingBitMatrix = NULL; | |
53 if(m_readMappingMatrix != NULL) { | |
54 delete m_readMappingMatrix; | |
55 } | |
56 m_readMappingMatrix = NULL; | |
57 } | |
58 CBC_DataMatrixVersion *CBC_DataMatrixBitMatrixParser::GetVersion() | |
59 { | |
60 return m_version; | |
61 } | |
62 CBC_DataMatrixVersion *CBC_DataMatrixBitMatrixParser::ReadVersion(CBC_CommonBitM
atrix *bitMatrix, FX_INT32 &e) | |
63 { | |
64 FX_INT32 rows = bitMatrix->GetHeight(); | |
65 FX_INT32 columns = bitMatrix->GetWidth(); | |
66 CBC_DataMatrixVersion *temp = CBC_DataMatrixVersion::GetVersionForDimensions
(rows, columns, e); | |
67 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
68 return temp; | |
69 } | |
70 CFX_ByteArray *CBC_DataMatrixBitMatrixParser::ReadCodewords(FX_INT32 &e) | |
71 { | |
72 CBC_AutoPtr<CFX_ByteArray> result(FX_NEW CFX_ByteArray()); | |
73 result->SetSize(m_version->GetTotalCodewords()); | |
74 FX_INT32 resultOffset = 0; | |
75 FX_INT32 row = 4; | |
76 FX_INT32 column = 0; | |
77 FX_INT32 numRows = m_mappingBitMatrix->GetHeight(); | |
78 FX_INT32 numColumns = m_mappingBitMatrix->GetWidth(); | |
79 FX_BOOL corner1Read = FALSE; | |
80 FX_BOOL corner2Read = FALSE; | |
81 FX_BOOL corner3Read = FALSE; | |
82 FX_BOOL corner4Read = FALSE; | |
83 do { | |
84 if ((row == numRows) && (column == 0) && !corner1Read) { | |
85 (*result)[resultOffset++] = (FX_BYTE) ReadCorner1(numRows, numColumn
s); | |
86 row -= 2; | |
87 column += 2; | |
88 corner1Read = TRUE; | |
89 } else if ((row == numRows - 2) && (column == 0) && ((numColumns & 0x03)
!= 0) && !corner2Read) { | |
90 (*result)[resultOffset++] = (FX_BYTE) ReadCorner2(numRows, numColumn
s); | |
91 row -= 2; | |
92 column += 2; | |
93 corner2Read = TRUE; | |
94 } else if ((row == numRows + 4) && (column == 2) && ((numColumns & 0x07)
== 0) && !corner3Read) { | |
95 (*result)[resultOffset++] = (FX_BYTE) ReadCorner3(numRows, numColumn
s); | |
96 row -= 2; | |
97 column += 2; | |
98 corner3Read = TRUE; | |
99 } else if ((row == numRows - 2) && (column == 0) && ((numColumns & 0x07)
== 4) && !corner4Read) { | |
100 (*result)[resultOffset++] = (FX_BYTE) ReadCorner4(numRows, numColumn
s); | |
101 row -= 2; | |
102 column += 2; | |
103 corner4Read = TRUE; | |
104 } else { | |
105 do { | |
106 if ((row < numRows) && (column >= 0) && !m_readMappingMatrix->Ge
t(column, row)) { | |
107 if (resultOffset < (*result).GetSize() ) { | |
108 (*result)[resultOffset++] = (FX_BYTE) ReadUtah(row, colu
mn, numRows, numColumns); | |
109 } | |
110 } | |
111 row -= 2; | |
112 column += 2; | |
113 } while ((row >= 0) && (column < numColumns)); | |
114 row += 1; | |
115 column += 3; | |
116 do { | |
117 if ((row >= 0) && (column < numColumns) && !m_readMappingMatrix-
>Get(column, row)) { | |
118 if (resultOffset < (*result).GetSize() ) { | |
119 (*result)[resultOffset++] = (FX_BYTE) ReadUtah(row, colu
mn, numRows, numColumns); | |
120 } | |
121 } | |
122 row += 2; | |
123 column -= 2; | |
124 } while ((row < numRows) && (column >= 0)); | |
125 row += 3; | |
126 column += 1; | |
127 } | |
128 } while ((row < numRows) || (column < numColumns)); | |
129 if (resultOffset != m_version->GetTotalCodewords()) { | |
130 e = BCExceptionFormatException; | |
131 return NULL; | |
132 } | |
133 return result.release(); | |
134 } | |
135 FX_BOOL CBC_DataMatrixBitMatrixParser::ReadModule(FX_INT32 row, FX_INT32 column,
FX_INT32 numRows, FX_INT32 numColumns) | |
136 { | |
137 if (row < 0) { | |
138 row += numRows; | |
139 column += 4 - ((numRows + 4) & 0x07); | |
140 } | |
141 if (column < 0) { | |
142 column += numColumns; | |
143 row += 4 - ((numColumns + 4) & 0x07); | |
144 } | |
145 m_readMappingMatrix->Set(column, row); | |
146 return m_mappingBitMatrix->Get(column, row); | |
147 } | |
148 FX_INT32 CBC_DataMatrixBitMatrixParser::ReadUtah(FX_INT32 row, FX_INT32 column,
FX_INT32 numRows, FX_INT32 numColumns) | |
149 { | |
150 FX_INT32 currentByte = 0; | |
151 if (ReadModule(row - 2, column - 2, numRows, numColumns)) { | |
152 currentByte |= 1; | |
153 } | |
154 currentByte <<= 1; | |
155 if (ReadModule(row - 2, column - 1, numRows, numColumns)) { | |
156 currentByte |= 1; | |
157 } | |
158 currentByte <<= 1; | |
159 if (ReadModule(row - 1, column - 2, numRows, numColumns)) { | |
160 currentByte |= 1; | |
161 } | |
162 currentByte <<= 1; | |
163 if (ReadModule(row - 1, column - 1, numRows, numColumns)) { | |
164 currentByte |= 1; | |
165 } | |
166 currentByte <<= 1; | |
167 if (ReadModule(row - 1, column, numRows, numColumns)) { | |
168 currentByte |= 1; | |
169 } | |
170 currentByte <<= 1; | |
171 if (ReadModule(row, column - 2, numRows, numColumns)) { | |
172 currentByte |= 1; | |
173 } | |
174 currentByte <<= 1; | |
175 if (ReadModule(row, column - 1, numRows, numColumns)) { | |
176 currentByte |= 1; | |
177 } | |
178 currentByte <<= 1; | |
179 if (ReadModule(row, column, numRows, numColumns)) { | |
180 currentByte |= 1; | |
181 } | |
182 return currentByte; | |
183 } | |
184 FX_INT32 CBC_DataMatrixBitMatrixParser::ReadCorner1(FX_INT32 numRows, FX_INT32 n
umColumns) | |
185 { | |
186 FX_INT32 currentByte = 0; | |
187 if (ReadModule(numRows - 1, 0, numRows, numColumns)) { | |
188 currentByte |= 1; | |
189 } | |
190 currentByte <<= 1; | |
191 if (ReadModule(numRows - 1, 1, numRows, numColumns)) { | |
192 currentByte |= 1; | |
193 } | |
194 currentByte <<= 1; | |
195 if (ReadModule(numRows - 1, 2, numRows, numColumns)) { | |
196 currentByte |= 1; | |
197 } | |
198 currentByte <<= 1; | |
199 if (ReadModule(0, numColumns - 2, numRows, numColumns)) { | |
200 currentByte |= 1; | |
201 } | |
202 currentByte <<= 1; | |
203 if (ReadModule(0, numColumns - 1, numRows, numColumns)) { | |
204 currentByte |= 1; | |
205 } | |
206 currentByte <<= 1; | |
207 if (ReadModule(1, numColumns - 1, numRows, numColumns)) { | |
208 currentByte |= 1; | |
209 } | |
210 currentByte <<= 1; | |
211 if (ReadModule(2, numColumns - 1, numRows, numColumns)) { | |
212 currentByte |= 1; | |
213 } | |
214 currentByte <<= 1; | |
215 if (ReadModule(3, numColumns - 1, numRows, numColumns)) { | |
216 currentByte |= 1; | |
217 } | |
218 return currentByte; | |
219 } | |
220 FX_INT32 CBC_DataMatrixBitMatrixParser::ReadCorner2(FX_INT32 numRows, FX_INT32 n
umColumns) | |
221 { | |
222 FX_INT32 currentByte = 0; | |
223 if (ReadModule(numRows - 3, 0, numRows, numColumns)) { | |
224 currentByte |= 1; | |
225 } | |
226 currentByte <<= 1; | |
227 if (ReadModule(numRows - 2, 0, numRows, numColumns)) { | |
228 currentByte |= 1; | |
229 } | |
230 currentByte <<= 1; | |
231 if (ReadModule(numRows - 1, 0, numRows, numColumns)) { | |
232 currentByte |= 1; | |
233 } | |
234 currentByte <<= 1; | |
235 if (ReadModule(0, numColumns - 4, numRows, numColumns)) { | |
236 currentByte |= 1; | |
237 } | |
238 currentByte <<= 1; | |
239 if (ReadModule(0, numColumns - 3, numRows, numColumns)) { | |
240 currentByte |= 1; | |
241 } | |
242 currentByte <<= 1; | |
243 if (ReadModule(0, numColumns - 2, numRows, numColumns)) { | |
244 currentByte |= 1; | |
245 } | |
246 currentByte <<= 1; | |
247 if (ReadModule(0, numColumns - 1, numRows, numColumns)) { | |
248 currentByte |= 1; | |
249 } | |
250 currentByte <<= 1; | |
251 if (ReadModule(1, numColumns - 1, numRows, numColumns)) { | |
252 currentByte |= 1; | |
253 } | |
254 return currentByte; | |
255 } | |
256 FX_INT32 CBC_DataMatrixBitMatrixParser::ReadCorner3(FX_INT32 numRows, FX_INT32 n
umColumns) | |
257 { | |
258 FX_INT32 currentByte = 0; | |
259 if (ReadModule(numRows - 1, 0, numRows, numColumns)) { | |
260 currentByte |= 1; | |
261 } | |
262 currentByte <<= 1; | |
263 if (ReadModule(numRows - 1, numColumns - 1, numRows, numColumns)) { | |
264 currentByte |= 1; | |
265 } | |
266 currentByte <<= 1; | |
267 if (ReadModule(0, numColumns - 3, numRows, numColumns)) { | |
268 currentByte |= 1; | |
269 } | |
270 currentByte <<= 1; | |
271 if (ReadModule(0, numColumns - 2, numRows, numColumns)) { | |
272 currentByte |= 1; | |
273 } | |
274 currentByte <<= 1; | |
275 if (ReadModule(0, numColumns - 1, numRows, numColumns)) { | |
276 currentByte |= 1; | |
277 } | |
278 currentByte <<= 1; | |
279 if (ReadModule(1, numColumns - 3, numRows, numColumns)) { | |
280 currentByte |= 1; | |
281 } | |
282 currentByte <<= 1; | |
283 if (ReadModule(1, numColumns - 2, numRows, numColumns)) { | |
284 currentByte |= 1; | |
285 } | |
286 currentByte <<= 1; | |
287 if (ReadModule(1, numColumns - 1, numRows, numColumns)) { | |
288 currentByte |= 1; | |
289 } | |
290 return currentByte; | |
291 } | |
292 FX_INT32 CBC_DataMatrixBitMatrixParser::ReadCorner4(FX_INT32 numRows, FX_INT32 n
umColumns) | |
293 { | |
294 FX_INT32 currentByte = 0; | |
295 if (ReadModule(numRows - 3, 0, numRows, numColumns)) { | |
296 currentByte |= 1; | |
297 } | |
298 currentByte <<= 1; | |
299 if (ReadModule(numRows - 2, 0, numRows, numColumns)) { | |
300 currentByte |= 1; | |
301 } | |
302 currentByte <<= 1; | |
303 if (ReadModule(numRows - 1, 0, numRows, numColumns)) { | |
304 currentByte |= 1; | |
305 } | |
306 currentByte <<= 1; | |
307 if (ReadModule(0, numColumns - 2, numRows, numColumns)) { | |
308 currentByte |= 1; | |
309 } | |
310 currentByte <<= 1; | |
311 if (ReadModule(0, numColumns - 1, numRows, numColumns)) { | |
312 currentByte |= 1; | |
313 } | |
314 currentByte <<= 1; | |
315 if (ReadModule(1, numColumns - 1, numRows, numColumns)) { | |
316 currentByte |= 1; | |
317 } | |
318 currentByte <<= 1; | |
319 if (ReadModule(2, numColumns - 1, numRows, numColumns)) { | |
320 currentByte |= 1; | |
321 } | |
322 currentByte <<= 1; | |
323 if (ReadModule(3, numColumns - 1, numRows, numColumns)) { | |
324 currentByte |= 1; | |
325 } | |
326 return currentByte; | |
327 } | |
328 CBC_CommonBitMatrix *CBC_DataMatrixBitMatrixParser::ExtractDataRegion(CBC_Common
BitMatrix *bitMatrix , FX_INT32 &e) | |
329 { | |
330 FX_INT32 symbolSizeRows = m_version->GetSymbolSizeRows(); | |
331 FX_INT32 symbolSizeColumns = m_version->GetSymbolSizeColumns(); | |
332 if (bitMatrix->GetHeight() != symbolSizeRows) { | |
333 e = BCExceptionCanNotCallGetDimensionOnNonSquareMatrix; | |
334 return NULL; | |
335 } | |
336 FX_INT32 dataRegionSizeRows = m_version->GetDataRegionSizeRows(); | |
337 FX_INT32 dataRegionSizeColumns = m_version->GetDataRegionSizeColumns(); | |
338 FX_INT32 numDataRegionsRow = symbolSizeRows / dataRegionSizeRows; | |
339 FX_INT32 numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns; | |
340 FX_INT32 sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows; | |
341 FX_INT32 sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns
; | |
342 CBC_CommonBitMatrix *bitMatrixWithoutAlignment = FX_NEW CBC_CommonBitMatrix(
); | |
343 bitMatrixWithoutAlignment->Init(sizeDataRegionColumn, sizeDataRegionRow); | |
344 FX_INT32 dataRegionRow; | |
345 for (dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow)
{ | |
346 FX_INT32 dataRegionRowOffset = dataRegionRow * dataRegionSizeRows; | |
347 FX_INT32 dataRegionColumn; | |
348 for (dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; ++da
taRegionColumn) { | |
349 FX_INT32 dataRegionColumnOffset = dataRegionColumn * dataRegionSizeC
olumns; | |
350 FX_INT32 i; | |
351 for (i = 0; i < dataRegionSizeRows; ++i) { | |
352 FX_INT32 readRowOffset = dataRegionRow * (dataRegionSizeRows + 2
) + 1 + i; | |
353 FX_INT32 writeRowOffset = dataRegionRowOffset + i; | |
354 FX_INT32 j; | |
355 for (j = 0; j < dataRegionSizeColumns; ++j) { | |
356 FX_INT32 readColumnOffset = dataRegionColumn * (dataRegionSi
zeColumns + 2) + 1 + j; | |
357 if (bitMatrix->Get(readColumnOffset, readRowOffset)) { | |
358 FX_INT32 writeColumnOffset = dataRegionColumnOffset + j; | |
359 bitMatrixWithoutAlignment->Set(writeColumnOffset, writeR
owOffset); | |
360 } | |
361 } | |
362 } | |
363 } | |
364 } | |
365 return bitMatrixWithoutAlignment; | |
366 } | |
OLD | NEW |