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 2006 Jeremias Maerki
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 "fxbarcode/datamatrix/BC_SymbolInfo.h"
24
25 #include "fxbarcode/common/BC_CommonBitMatrix.h"
26 #include "fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h"
27 #include "fxbarcode/datamatrix/BC_Encoder.h"
28 #include "fxbarcode/utils.h"
29
30 namespace {
31
32 const size_t kSymbolsCount = 30;
33
34 CBC_SymbolInfo* g_symbols[kSymbolsCount] = {
35 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
36 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
37 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
38 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
39
40 } // namespace
41
Initialize()42 void CBC_SymbolInfo::Initialize() {
43 g_symbols[0] = new CBC_SymbolInfo(3, 5, 8, 8, 1);
44 g_symbols[1] = new CBC_SymbolInfo(5, 7, 10, 10, 1);
45 g_symbols[2] = new CBC_SymbolInfo(5, 7, 16, 6, 1);
46 g_symbols[3] = new CBC_SymbolInfo(8, 10, 12, 12, 1);
47 g_symbols[4] = new CBC_SymbolInfo(10, 11, 14, 6, 2);
48 g_symbols[5] = new CBC_SymbolInfo(12, 12, 14, 14, 1);
49 g_symbols[6] = new CBC_SymbolInfo(16, 14, 24, 10, 1);
50 g_symbols[7] = new CBC_SymbolInfo(18, 14, 16, 16, 1);
51 g_symbols[8] = new CBC_SymbolInfo(22, 18, 18, 18, 1);
52 g_symbols[9] = new CBC_SymbolInfo(22, 18, 16, 10, 2);
53 g_symbols[10] = new CBC_SymbolInfo(30, 20, 20, 20, 1);
54 g_symbols[11] = new CBC_SymbolInfo(32, 24, 16, 14, 2);
55 g_symbols[12] = new CBC_SymbolInfo(36, 24, 22, 22, 1);
56 g_symbols[13] = new CBC_SymbolInfo(44, 28, 24, 24, 1);
57 g_symbols[14] = new CBC_SymbolInfo(49, 28, 22, 14, 2);
58 g_symbols[15] = new CBC_SymbolInfo(62, 36, 14, 14, 4);
59 g_symbols[16] = new CBC_SymbolInfo(86, 42, 16, 16, 4);
60 g_symbols[17] = new CBC_SymbolInfo(114, 48, 18, 18, 4);
61 g_symbols[18] = new CBC_SymbolInfo(144, 56, 20, 20, 4);
62 g_symbols[19] = new CBC_SymbolInfo(174, 68, 22, 22, 4);
63 g_symbols[20] = new CBC_SymbolInfo(204, 84, 24, 24, 4, 102, 42);
64 g_symbols[21] = new CBC_SymbolInfo(280, 112, 14, 14, 16, 140, 56);
65 g_symbols[22] = new CBC_SymbolInfo(368, 144, 16, 16, 16, 92, 36);
66 g_symbols[23] = new CBC_SymbolInfo(456, 192, 18, 18, 16, 114, 48);
67 g_symbols[24] = new CBC_SymbolInfo(576, 224, 20, 20, 16, 144, 56);
68 g_symbols[25] = new CBC_SymbolInfo(696, 272, 22, 22, 16, 174, 68);
69 g_symbols[26] = new CBC_SymbolInfo(816, 336, 24, 24, 16, 136, 56);
70 g_symbols[27] = new CBC_SymbolInfo(1050, 408, 18, 18, 36, 175, 68);
71 g_symbols[28] = new CBC_SymbolInfo(1304, 496, 20, 20, 36, 163, 62);
72 g_symbols[29] = new CBC_DataMatrixSymbolInfo144();
73 }
74
Finalize()75 void CBC_SymbolInfo::Finalize() {
76 for (size_t i = 0; i < kSymbolsCount; i++) {
77 delete g_symbols[i];
78 g_symbols[i] = nullptr;
79 }
80 }
81
CBC_SymbolInfo(int32_t dataCapacity,int32_t errorCodewords,int32_t matrixWidth,int32_t matrixHeight,int32_t dataRegions)82 CBC_SymbolInfo::CBC_SymbolInfo(int32_t dataCapacity,
83 int32_t errorCodewords,
84 int32_t matrixWidth,
85 int32_t matrixHeight,
86 int32_t dataRegions)
87 : CBC_SymbolInfo(dataCapacity,
88 errorCodewords,
89 matrixWidth,
90 matrixHeight,
91 dataRegions,
92 dataCapacity,
93 errorCodewords) {}
94
CBC_SymbolInfo(int32_t dataCapacity,int32_t errorCodewords,int32_t matrixWidth,int32_t matrixHeight,int32_t dataRegions,int32_t rsBlockData,int32_t rsBlockError)95 CBC_SymbolInfo::CBC_SymbolInfo(int32_t dataCapacity,
96 int32_t errorCodewords,
97 int32_t matrixWidth,
98 int32_t matrixHeight,
99 int32_t dataRegions,
100 int32_t rsBlockData,
101 int32_t rsBlockError)
102 : m_rectangular(matrixWidth != matrixHeight),
103 m_dataCapacity(dataCapacity),
104 m_errorCodewords(errorCodewords),
105 m_matrixWidth(matrixWidth),
106 m_matrixHeight(matrixHeight),
107 m_dataRegions(dataRegions),
108 m_rsBlockData(rsBlockData),
109 m_rsBlockError(rsBlockError) {}
110
~CBC_SymbolInfo()111 CBC_SymbolInfo::~CBC_SymbolInfo() {}
112
lookup(int32_t dataCodewords,bool allowRectangular,int32_t & e)113 CBC_SymbolInfo* CBC_SymbolInfo::lookup(int32_t dataCodewords,
114 bool allowRectangular,
115 int32_t& e) {
116 for (size_t i = 0; i < kSymbolsCount; i++) {
117 CBC_SymbolInfo* symbol = g_symbols[i];
118 if (symbol->m_rectangular && !allowRectangular)
119 continue;
120
121 if (dataCodewords <= symbol->dataCapacity())
122 return symbol;
123 }
124 e = BCExceptionIllegalDataCodewords;
125 return nullptr;
126 }
127
getHorizontalDataRegions() const128 int32_t CBC_SymbolInfo::getHorizontalDataRegions() const {
129 switch (m_dataRegions) {
130 case 1:
131 return 1;
132 case 2:
133 return 2;
134 case 4:
135 return 2;
136 case 16:
137 return 4;
138 case 36:
139 return 6;
140 default:
141 NOTREACHED();
142 return 0;
143 }
144 }
145
getVerticalDataRegions() const146 int32_t CBC_SymbolInfo::getVerticalDataRegions() const {
147 switch (m_dataRegions) {
148 case 1:
149 return 1;
150 case 2:
151 return 1;
152 case 4:
153 return 2;
154 case 16:
155 return 4;
156 case 36:
157 return 6;
158 default:
159 NOTREACHED();
160 return 0;
161 }
162 }
163
getSymbolDataWidth() const164 int32_t CBC_SymbolInfo::getSymbolDataWidth() const {
165 return getHorizontalDataRegions() * m_matrixWidth;
166 }
167
getSymbolDataHeight() const168 int32_t CBC_SymbolInfo::getSymbolDataHeight() const {
169 return getVerticalDataRegions() * m_matrixHeight;
170 }
171
getSymbolWidth() const172 int32_t CBC_SymbolInfo::getSymbolWidth() const {
173 return getSymbolDataWidth() + (getHorizontalDataRegions() * 2);
174 }
175
getSymbolHeight() const176 int32_t CBC_SymbolInfo::getSymbolHeight() const {
177 return getSymbolDataHeight() + (getVerticalDataRegions() * 2);
178 }
179
getCodewordCount() const180 int32_t CBC_SymbolInfo::getCodewordCount() const {
181 return m_dataCapacity + m_errorCodewords;
182 }
183
getInterleavedBlockCount() const184 int32_t CBC_SymbolInfo::getInterleavedBlockCount() const {
185 return m_dataCapacity / m_rsBlockData;
186 }
187
getDataLengthForInterleavedBlock(int32_t index) const188 int32_t CBC_SymbolInfo::getDataLengthForInterleavedBlock(int32_t index) const {
189 return m_rsBlockData;
190 }
191
getErrorLengthForInterleavedBlock(int32_t index) const192 int32_t CBC_SymbolInfo::getErrorLengthForInterleavedBlock(int32_t index) const {
193 return m_rsBlockError;
194 }
195