xref: /aosp_15_r20/external/pdfium/fxbarcode/datamatrix/BC_SymbolInfo.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1*3ac0a46fSAndroid Build Coastguard Worker // Copyright 2014 The PDFium Authors
2*3ac0a46fSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*3ac0a46fSAndroid Build Coastguard Worker // found in the LICENSE file.
4*3ac0a46fSAndroid Build Coastguard Worker 
5*3ac0a46fSAndroid Build Coastguard Worker // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6*3ac0a46fSAndroid Build Coastguard Worker // Original code is licensed as follows:
7*3ac0a46fSAndroid Build Coastguard Worker /*
8*3ac0a46fSAndroid Build Coastguard Worker  * Copyright 2006 Jeremias Maerki
9*3ac0a46fSAndroid Build Coastguard Worker  *
10*3ac0a46fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
11*3ac0a46fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
12*3ac0a46fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
13*3ac0a46fSAndroid Build Coastguard Worker  *
14*3ac0a46fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
15*3ac0a46fSAndroid Build Coastguard Worker  *
16*3ac0a46fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
17*3ac0a46fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
18*3ac0a46fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19*3ac0a46fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
20*3ac0a46fSAndroid Build Coastguard Worker  * limitations under the License.
21*3ac0a46fSAndroid Build Coastguard Worker  */
22*3ac0a46fSAndroid Build Coastguard Worker 
23*3ac0a46fSAndroid Build Coastguard Worker #include "fxbarcode/datamatrix/BC_SymbolInfo.h"
24*3ac0a46fSAndroid Build Coastguard Worker 
25*3ac0a46fSAndroid Build Coastguard Worker #include <iterator>
26*3ac0a46fSAndroid Build Coastguard Worker 
27*3ac0a46fSAndroid Build Coastguard Worker #include "fxbarcode/common/BC_CommonBitMatrix.h"
28*3ac0a46fSAndroid Build Coastguard Worker #include "fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h"
29*3ac0a46fSAndroid Build Coastguard Worker #include "fxbarcode/datamatrix/BC_Encoder.h"
30*3ac0a46fSAndroid Build Coastguard Worker #include "third_party/base/notreached.h"
31*3ac0a46fSAndroid Build Coastguard Worker 
32*3ac0a46fSAndroid Build Coastguard Worker namespace {
33*3ac0a46fSAndroid Build Coastguard Worker 
34*3ac0a46fSAndroid Build Coastguard Worker constexpr size_t kSymbolsCount = 30;
35*3ac0a46fSAndroid Build Coastguard Worker 
36*3ac0a46fSAndroid Build Coastguard Worker CBC_SymbolInfo* g_symbols[kSymbolsCount] = {
37*3ac0a46fSAndroid Build Coastguard Worker     nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
38*3ac0a46fSAndroid Build Coastguard Worker     nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
39*3ac0a46fSAndroid Build Coastguard Worker     nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
40*3ac0a46fSAndroid Build Coastguard Worker     nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
41*3ac0a46fSAndroid Build Coastguard Worker 
42*3ac0a46fSAndroid Build Coastguard Worker constexpr CBC_SymbolInfo::Data kSymbolData[] = {
43*3ac0a46fSAndroid Build Coastguard Worker     {3, 5, 3, 5, 8, 8, 1},           {5, 7, 5, 7, 10, 10, 1},
44*3ac0a46fSAndroid Build Coastguard Worker     {5, 7, 5, 7, 16, 6, 1},          {8, 10, 8, 10, 12, 12, 1},
45*3ac0a46fSAndroid Build Coastguard Worker     {10, 11, 10, 11, 14, 6, 2},      {12, 12, 12, 12, 14, 14, 1},
46*3ac0a46fSAndroid Build Coastguard Worker     {16, 14, 16, 14, 24, 10, 1},     {18, 14, 18, 14, 16, 16, 1},
47*3ac0a46fSAndroid Build Coastguard Worker     {22, 18, 22, 18, 18, 18, 1},     {22, 18, 22, 18, 16, 10, 2},
48*3ac0a46fSAndroid Build Coastguard Worker     {30, 20, 30, 20, 20, 20, 1},     {32, 24, 32, 24, 16, 14, 2},
49*3ac0a46fSAndroid Build Coastguard Worker     {36, 24, 36, 24, 22, 22, 1},     {44, 28, 44, 28, 24, 24, 1},
50*3ac0a46fSAndroid Build Coastguard Worker     {49, 28, 49, 28, 22, 14, 2},     {62, 36, 62, 36, 14, 14, 4},
51*3ac0a46fSAndroid Build Coastguard Worker     {86, 42, 86, 42, 16, 16, 4},     {114, 48, 114, 48, 18, 18, 4},
52*3ac0a46fSAndroid Build Coastguard Worker     {144, 56, 144, 56, 20, 20, 4},   {174, 68, 174, 68, 22, 22, 4},
53*3ac0a46fSAndroid Build Coastguard Worker     {204, 84, 102, 42, 24, 24, 4},   {280, 112, 140, 56, 14, 14, 16},
54*3ac0a46fSAndroid Build Coastguard Worker     {368, 144, 92, 36, 16, 16, 16},  {456, 192, 114, 48, 18, 18, 16},
55*3ac0a46fSAndroid Build Coastguard Worker     {576, 224, 144, 56, 20, 20, 16}, {696, 272, 174, 68, 22, 22, 16},
56*3ac0a46fSAndroid Build Coastguard Worker     {816, 336, 136, 56, 24, 24, 16}, {1050, 408, 175, 68, 18, 18, 36},
57*3ac0a46fSAndroid Build Coastguard Worker     {1304, 496, 163, 62, 20, 20, 36}};
58*3ac0a46fSAndroid Build Coastguard Worker 
59*3ac0a46fSAndroid Build Coastguard Worker constexpr size_t kSymbolDataSize = std::size(kSymbolData);
60*3ac0a46fSAndroid Build Coastguard Worker static_assert(kSymbolDataSize + 1 == kSymbolsCount, "Wrong kSymbolDataSize");
61*3ac0a46fSAndroid Build Coastguard Worker 
62*3ac0a46fSAndroid Build Coastguard Worker }  // namespace
63*3ac0a46fSAndroid Build Coastguard Worker 
64*3ac0a46fSAndroid Build Coastguard Worker // static
Initialize()65*3ac0a46fSAndroid Build Coastguard Worker void CBC_SymbolInfo::Initialize() {
66*3ac0a46fSAndroid Build Coastguard Worker   for (size_t i = 0; i < kSymbolDataSize; ++i)
67*3ac0a46fSAndroid Build Coastguard Worker     g_symbols[i] = new CBC_SymbolInfo(&kSymbolData[i]);
68*3ac0a46fSAndroid Build Coastguard Worker   g_symbols[kSymbolDataSize] = new CBC_DataMatrixSymbolInfo144();
69*3ac0a46fSAndroid Build Coastguard Worker }
70*3ac0a46fSAndroid Build Coastguard Worker 
71*3ac0a46fSAndroid Build Coastguard Worker // static
Finalize()72*3ac0a46fSAndroid Build Coastguard Worker void CBC_SymbolInfo::Finalize() {
73*3ac0a46fSAndroid Build Coastguard Worker   for (size_t i = 0; i < kSymbolsCount; ++i) {
74*3ac0a46fSAndroid Build Coastguard Worker     delete g_symbols[i];
75*3ac0a46fSAndroid Build Coastguard Worker     g_symbols[i] = nullptr;
76*3ac0a46fSAndroid Build Coastguard Worker   }
77*3ac0a46fSAndroid Build Coastguard Worker }
78*3ac0a46fSAndroid Build Coastguard Worker 
CBC_SymbolInfo(const Data * data)79*3ac0a46fSAndroid Build Coastguard Worker CBC_SymbolInfo::CBC_SymbolInfo(const Data* data) : data_(data) {}
80*3ac0a46fSAndroid Build Coastguard Worker 
81*3ac0a46fSAndroid Build Coastguard Worker CBC_SymbolInfo::~CBC_SymbolInfo() = default;
82*3ac0a46fSAndroid Build Coastguard Worker 
Lookup(size_t data_codewords,bool allow_rectangular)83*3ac0a46fSAndroid Build Coastguard Worker const CBC_SymbolInfo* CBC_SymbolInfo::Lookup(size_t data_codewords,
84*3ac0a46fSAndroid Build Coastguard Worker                                              bool allow_rectangular) {
85*3ac0a46fSAndroid Build Coastguard Worker   for (size_t i = 0; i < kSymbolsCount; ++i) {
86*3ac0a46fSAndroid Build Coastguard Worker     CBC_SymbolInfo* symbol = g_symbols[i];
87*3ac0a46fSAndroid Build Coastguard Worker     if (symbol->is_rectangular() && !allow_rectangular)
88*3ac0a46fSAndroid Build Coastguard Worker       continue;
89*3ac0a46fSAndroid Build Coastguard Worker 
90*3ac0a46fSAndroid Build Coastguard Worker     if (data_codewords <= symbol->data_capacity())
91*3ac0a46fSAndroid Build Coastguard Worker       return symbol;
92*3ac0a46fSAndroid Build Coastguard Worker   }
93*3ac0a46fSAndroid Build Coastguard Worker   return nullptr;
94*3ac0a46fSAndroid Build Coastguard Worker }
95*3ac0a46fSAndroid Build Coastguard Worker 
GetHorizontalDataRegions() const96*3ac0a46fSAndroid Build Coastguard Worker int32_t CBC_SymbolInfo::GetHorizontalDataRegions() const {
97*3ac0a46fSAndroid Build Coastguard Worker   switch (data_->data_regions) {
98*3ac0a46fSAndroid Build Coastguard Worker     case 1:
99*3ac0a46fSAndroid Build Coastguard Worker       return 1;
100*3ac0a46fSAndroid Build Coastguard Worker     case 2:
101*3ac0a46fSAndroid Build Coastguard Worker       return 2;
102*3ac0a46fSAndroid Build Coastguard Worker     case 4:
103*3ac0a46fSAndroid Build Coastguard Worker       return 2;
104*3ac0a46fSAndroid Build Coastguard Worker     case 16:
105*3ac0a46fSAndroid Build Coastguard Worker       return 4;
106*3ac0a46fSAndroid Build Coastguard Worker     case 36:
107*3ac0a46fSAndroid Build Coastguard Worker       return 6;
108*3ac0a46fSAndroid Build Coastguard Worker     default:
109*3ac0a46fSAndroid Build Coastguard Worker       NOTREACHED_NORETURN();
110*3ac0a46fSAndroid Build Coastguard Worker   }
111*3ac0a46fSAndroid Build Coastguard Worker }
112*3ac0a46fSAndroid Build Coastguard Worker 
GetVerticalDataRegions() const113*3ac0a46fSAndroid Build Coastguard Worker int32_t CBC_SymbolInfo::GetVerticalDataRegions() const {
114*3ac0a46fSAndroid Build Coastguard Worker   switch (data_->data_regions) {
115*3ac0a46fSAndroid Build Coastguard Worker     case 1:
116*3ac0a46fSAndroid Build Coastguard Worker       return 1;
117*3ac0a46fSAndroid Build Coastguard Worker     case 2:
118*3ac0a46fSAndroid Build Coastguard Worker       return 1;
119*3ac0a46fSAndroid Build Coastguard Worker     case 4:
120*3ac0a46fSAndroid Build Coastguard Worker       return 2;
121*3ac0a46fSAndroid Build Coastguard Worker     case 16:
122*3ac0a46fSAndroid Build Coastguard Worker       return 4;
123*3ac0a46fSAndroid Build Coastguard Worker     case 36:
124*3ac0a46fSAndroid Build Coastguard Worker       return 6;
125*3ac0a46fSAndroid Build Coastguard Worker     default:
126*3ac0a46fSAndroid Build Coastguard Worker       NOTREACHED_NORETURN();
127*3ac0a46fSAndroid Build Coastguard Worker   }
128*3ac0a46fSAndroid Build Coastguard Worker }
129*3ac0a46fSAndroid Build Coastguard Worker 
GetSymbolDataWidth() const130*3ac0a46fSAndroid Build Coastguard Worker int32_t CBC_SymbolInfo::GetSymbolDataWidth() const {
131*3ac0a46fSAndroid Build Coastguard Worker   return GetHorizontalDataRegions() * data_->matrix_width;
132*3ac0a46fSAndroid Build Coastguard Worker }
133*3ac0a46fSAndroid Build Coastguard Worker 
GetSymbolDataHeight() const134*3ac0a46fSAndroid Build Coastguard Worker int32_t CBC_SymbolInfo::GetSymbolDataHeight() const {
135*3ac0a46fSAndroid Build Coastguard Worker   return GetVerticalDataRegions() * data_->matrix_height;
136*3ac0a46fSAndroid Build Coastguard Worker }
137*3ac0a46fSAndroid Build Coastguard Worker 
GetSymbolWidth() const138*3ac0a46fSAndroid Build Coastguard Worker int32_t CBC_SymbolInfo::GetSymbolWidth() const {
139*3ac0a46fSAndroid Build Coastguard Worker   return GetSymbolDataWidth() + (GetHorizontalDataRegions() * 2);
140*3ac0a46fSAndroid Build Coastguard Worker }
141*3ac0a46fSAndroid Build Coastguard Worker 
GetSymbolHeight() const142*3ac0a46fSAndroid Build Coastguard Worker int32_t CBC_SymbolInfo::GetSymbolHeight() const {
143*3ac0a46fSAndroid Build Coastguard Worker   return GetSymbolDataHeight() + (GetVerticalDataRegions() * 2);
144*3ac0a46fSAndroid Build Coastguard Worker }
145*3ac0a46fSAndroid Build Coastguard Worker 
GetInterleavedBlockCount() const146*3ac0a46fSAndroid Build Coastguard Worker size_t CBC_SymbolInfo::GetInterleavedBlockCount() const {
147*3ac0a46fSAndroid Build Coastguard Worker   return data_->data_capacity / data_->rs_block_data;
148*3ac0a46fSAndroid Build Coastguard Worker }
149*3ac0a46fSAndroid Build Coastguard Worker 
GetDataLengthForInterleavedBlock() const150*3ac0a46fSAndroid Build Coastguard Worker size_t CBC_SymbolInfo::GetDataLengthForInterleavedBlock() const {
151*3ac0a46fSAndroid Build Coastguard Worker   return data_->rs_block_data;
152*3ac0a46fSAndroid Build Coastguard Worker }
153*3ac0a46fSAndroid Build Coastguard Worker 
GetErrorLengthForInterleavedBlock() const154*3ac0a46fSAndroid Build Coastguard Worker size_t CBC_SymbolInfo::GetErrorLengthForInterleavedBlock() const {
155*3ac0a46fSAndroid Build Coastguard Worker   return data_->rs_block_error;
156*3ac0a46fSAndroid Build Coastguard Worker }
157