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/barcode.h"
24#include "BC_DataMatrixVersion.h"
25CFX_PtrArray* CBC_DataMatrixVersion::VERSIONS = NULL;
26void CBC_DataMatrixVersion::Initialize() {
27  VERSIONS = new CFX_PtrArray();
28}
29void CBC_DataMatrixVersion::Finalize() {
30  for (int32_t i = 0; i < VERSIONS->GetSize(); i++) {
31    delete ((CBC_DataMatrixVersion*)(VERSIONS->GetAt(i)));
32  }
33  VERSIONS->RemoveAll();
34  delete VERSIONS;
35}
36CBC_DataMatrixVersion::CBC_DataMatrixVersion(int32_t versionNumber,
37                                             int32_t symbolSizeRows,
38                                             int32_t symbolSizeColumns,
39                                             int32_t dataRegionSizeRows,
40                                             int32_t dataRegionSizeColumns,
41                                             ECBlocks* ecBlocks) {
42  m_versionNumber = versionNumber;
43  m_symbolSizeRows = symbolSizeRows;
44  m_symbolSizeColumns = symbolSizeColumns;
45  m_dataRegionSizeRows = dataRegionSizeRows;
46  m_dataRegionSizeColumns = dataRegionSizeColumns;
47  m_ecBlocks = ecBlocks;
48  int32_t total = 0;
49  int32_t ecCodewords = ecBlocks->GetECCodewords();
50  const CFX_PtrArray& ecbArray = ecBlocks->GetECBlocks();
51  for (int32_t i = 0; i < ecbArray.GetSize(); i++) {
52    total += ((ECB*)ecbArray[i])->GetCount() *
53             (((ECB*)ecbArray[i])->GetDataCodewords() + ecCodewords);
54  }
55  m_totalCodewords = total;
56}
57CBC_DataMatrixVersion::~CBC_DataMatrixVersion() {
58  if (m_ecBlocks != NULL) {
59    delete m_ecBlocks;
60  }
61  m_ecBlocks = NULL;
62}
63int32_t CBC_DataMatrixVersion::GetVersionNumber() {
64  return m_versionNumber;
65}
66int32_t CBC_DataMatrixVersion::GetSymbolSizeRows() {
67  return m_symbolSizeRows;
68}
69int32_t CBC_DataMatrixVersion::GetSymbolSizeColumns() {
70  return m_symbolSizeColumns;
71}
72int32_t CBC_DataMatrixVersion::GetDataRegionSizeRows() {
73  return m_dataRegionSizeRows;
74}
75int32_t CBC_DataMatrixVersion::GetDataRegionSizeColumns() {
76  return m_dataRegionSizeColumns;
77}
78int32_t CBC_DataMatrixVersion::GetTotalCodewords() {
79  return m_totalCodewords;
80}
81ECBlocks* CBC_DataMatrixVersion::GetECBlocks() {
82  return m_ecBlocks;
83}
84void CBC_DataMatrixVersion::ReleaseAll() {
85  for (int32_t i = 0; i < VERSIONS->GetSize(); i++) {
86    delete (CBC_DataMatrixVersion*)VERSIONS->GetAt(i);
87  }
88  VERSIONS->RemoveAll();
89}
90CBC_DataMatrixVersion* CBC_DataMatrixVersion::GetVersionForDimensions(
91    int32_t numRows,
92    int32_t numColumns,
93    int32_t& e) {
94  if ((numRows & 0x01) != 0 || (numColumns & 0x01) != 0) {
95    e = BCExceptionNotFound;
96    return NULL;
97  }
98  if (VERSIONS->GetSize() == 0) {
99    VERSIONS->Add(new CBC_DataMatrixVersion(1, 10, 10, 8, 8,
100                                            new ECBlocks(5, new ECB(1, 3))));
101    VERSIONS->Add(new CBC_DataMatrixVersion(2, 12, 12, 10, 10,
102                                            new ECBlocks(7, new ECB(1, 5))));
103    VERSIONS->Add(new CBC_DataMatrixVersion(3, 14, 14, 12, 12,
104                                            new ECBlocks(10, new ECB(1, 8))));
105    VERSIONS->Add(new CBC_DataMatrixVersion(4, 16, 16, 14, 14,
106                                            new ECBlocks(12, new ECB(1, 12))));
107    VERSIONS->Add(new CBC_DataMatrixVersion(5, 18, 18, 16, 16,
108                                            new ECBlocks(14, new ECB(1, 18))));
109    VERSIONS->Add(new CBC_DataMatrixVersion(6, 20, 20, 18, 18,
110                                            new ECBlocks(18, new ECB(1, 22))));
111    VERSIONS->Add(new CBC_DataMatrixVersion(7, 22, 22, 20, 20,
112                                            new ECBlocks(20, new ECB(1, 30))));
113    VERSIONS->Add(new CBC_DataMatrixVersion(8, 24, 24, 22, 22,
114                                            new ECBlocks(24, new ECB(1, 36))));
115    VERSIONS->Add(new CBC_DataMatrixVersion(9, 26, 26, 24, 24,
116                                            new ECBlocks(28, new ECB(1, 44))));
117    VERSIONS->Add(new CBC_DataMatrixVersion(10, 32, 32, 14, 14,
118                                            new ECBlocks(36, new ECB(1, 62))));
119    VERSIONS->Add(new CBC_DataMatrixVersion(11, 36, 36, 16, 16,
120                                            new ECBlocks(42, new ECB(1, 86))));
121    VERSIONS->Add(new CBC_DataMatrixVersion(12, 40, 40, 18, 18,
122                                            new ECBlocks(48, new ECB(1, 114))));
123    VERSIONS->Add(new CBC_DataMatrixVersion(13, 44, 44, 20, 20,
124                                            new ECBlocks(56, new ECB(1, 144))));
125    VERSIONS->Add(new CBC_DataMatrixVersion(14, 48, 48, 22, 22,
126                                            new ECBlocks(68, new ECB(1, 174))));
127    VERSIONS->Add(new CBC_DataMatrixVersion(15, 52, 52, 24, 24,
128                                            new ECBlocks(42, new ECB(2, 102))));
129    VERSIONS->Add(new CBC_DataMatrixVersion(16, 64, 64, 14, 14,
130                                            new ECBlocks(56, new ECB(2, 140))));
131    VERSIONS->Add(new CBC_DataMatrixVersion(17, 72, 72, 16, 16,
132                                            new ECBlocks(36, new ECB(4, 92))));
133    VERSIONS->Add(new CBC_DataMatrixVersion(18, 80, 80, 18, 18,
134                                            new ECBlocks(48, new ECB(4, 114))));
135    VERSIONS->Add(new CBC_DataMatrixVersion(19, 88, 88, 20, 20,
136                                            new ECBlocks(56, new ECB(4, 144))));
137    VERSIONS->Add(new CBC_DataMatrixVersion(20, 96, 96, 22, 22,
138                                            new ECBlocks(68, new ECB(4, 174))));
139    VERSIONS->Add(new CBC_DataMatrixVersion(21, 104, 104, 24, 24,
140                                            new ECBlocks(56, new ECB(6, 136))));
141    VERSIONS->Add(new CBC_DataMatrixVersion(22, 120, 120, 18, 18,
142                                            new ECBlocks(68, new ECB(6, 175))));
143    VERSIONS->Add(new CBC_DataMatrixVersion(23, 132, 132, 20, 20,
144                                            new ECBlocks(62, new ECB(8, 163))));
145    VERSIONS->Add(new CBC_DataMatrixVersion(
146        24, 144, 144, 22, 22,
147        new ECBlocks(62, new ECB(8, 156), new ECB(2, 155))));
148    VERSIONS->Add(new CBC_DataMatrixVersion(25, 8, 18, 6, 16,
149                                            new ECBlocks(7, new ECB(1, 5))));
150    VERSIONS->Add(new CBC_DataMatrixVersion(26, 8, 32, 6, 14,
151                                            new ECBlocks(11, new ECB(1, 10))));
152    VERSIONS->Add(new CBC_DataMatrixVersion(27, 12, 26, 10, 24,
153                                            new ECBlocks(14, new ECB(1, 16))));
154    VERSIONS->Add(new CBC_DataMatrixVersion(28, 12, 36, 10, 16,
155                                            new ECBlocks(18, new ECB(1, 22))));
156    VERSIONS->Add(new CBC_DataMatrixVersion(29, 16, 36, 14, 16,
157                                            new ECBlocks(24, new ECB(1, 32))));
158    VERSIONS->Add(new CBC_DataMatrixVersion(30, 16, 48, 14, 22,
159                                            new ECBlocks(28, new ECB(1, 49))));
160  }
161  int32_t numVersions = VERSIONS->GetSize();
162  for (int32_t i = 0; i < numVersions; ++i) {
163    if (((CBC_DataMatrixVersion*)((*VERSIONS)[i]))->m_symbolSizeRows ==
164            numRows &&
165        ((CBC_DataMatrixVersion*)((*VERSIONS)[i]))->m_symbolSizeColumns ==
166            numColumns) {
167      return (CBC_DataMatrixVersion*)(*VERSIONS)[i];
168    }
169  }
170  e = BCExceptionNotFound;
171  return NULL;
172}
173