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