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_DefaultPlacement.h"
24
25 #include <utility>
26
27 #include "fxbarcode/datamatrix/BC_Encoder.h"
28
CBC_DefaultPlacement(WideString codewords,int32_t numcols,int32_t numrows)29 CBC_DefaultPlacement::CBC_DefaultPlacement(WideString codewords,
30 int32_t numcols,
31 int32_t numrows)
32 : m_codewords(std::move(codewords)),
33 m_numrows(numrows),
34 m_numcols(numcols) {
35 m_bits.resize(numcols * numrows);
36 for (int32_t i = 0; i < numcols * numrows; i++) {
37 m_bits[i] = (uint8_t)2;
38 }
39 }
40
~CBC_DefaultPlacement()41 CBC_DefaultPlacement::~CBC_DefaultPlacement() {}
42
getNumrows()43 int32_t CBC_DefaultPlacement::getNumrows() {
44 return m_numrows;
45 }
getNumcols()46 int32_t CBC_DefaultPlacement::getNumcols() {
47 return m_numcols;
48 }
getBits()49 std::vector<uint8_t>& CBC_DefaultPlacement::getBits() {
50 return m_bits;
51 }
getBit(int32_t col,int32_t row)52 bool CBC_DefaultPlacement::getBit(int32_t col, int32_t row) {
53 return m_bits[row * m_numcols + col] == 1;
54 }
setBit(int32_t col,int32_t row,bool bit)55 void CBC_DefaultPlacement::setBit(int32_t col, int32_t row, bool bit) {
56 m_bits[row * m_numcols + col] = bit ? (uint8_t)1 : (uint8_t)0;
57 }
hasBit(int32_t col,int32_t row)58 bool CBC_DefaultPlacement::hasBit(int32_t col, int32_t row) {
59 return m_bits[row * m_numcols + col] != 2;
60 }
place()61 void CBC_DefaultPlacement::place() {
62 int32_t pos = 0;
63 int32_t row = 4;
64 int32_t col = 0;
65 do {
66 if ((row == m_numrows) && (col == 0)) {
67 corner1(pos++);
68 }
69 if ((row == m_numrows - 2) && (col == 0) && ((m_numcols % 4) != 0)) {
70 corner2(pos++);
71 }
72 if ((row == m_numrows - 2) && (col == 0) && (m_numcols % 8 == 4)) {
73 corner3(pos++);
74 }
75 if ((row == m_numrows + 4) && (col == 2) && ((m_numcols % 8) == 0)) {
76 corner4(pos++);
77 }
78 do {
79 if ((row < m_numrows) && (col >= 0) && !hasBit(col, row)) {
80 utah(row, col, pos++);
81 }
82 row -= 2;
83 col += 2;
84 } while (row >= 0 && (col < m_numcols));
85 row++;
86 col += 3;
87 do {
88 if ((row >= 0) && (col < m_numcols) && !hasBit(col, row)) {
89 utah(row, col, pos++);
90 }
91 row += 2;
92 col -= 2;
93 } while ((row < m_numrows) && (col >= 0));
94 row += 3;
95 col++;
96 } while ((row < m_numrows) || (col < m_numcols));
97 if (!hasBit(m_numcols - 1, m_numrows - 1)) {
98 setBit(m_numcols - 1, m_numrows - 1, true);
99 setBit(m_numcols - 2, m_numrows - 2, true);
100 }
101 }
module(int32_t row,int32_t col,int32_t pos,int32_t bit)102 void CBC_DefaultPlacement::module(int32_t row,
103 int32_t col,
104 int32_t pos,
105 int32_t bit) {
106 if (row < 0) {
107 row += m_numrows;
108 col += 4 - ((m_numrows + 4) % 8);
109 }
110 if (col < 0) {
111 col += m_numcols;
112 row += 4 - ((m_numcols + 4) % 8);
113 }
114 int32_t v = m_codewords[pos];
115 v &= 1 << (8 - bit);
116 setBit(col, row, v != 0);
117 }
utah(int32_t row,int32_t col,int32_t pos)118 void CBC_DefaultPlacement::utah(int32_t row, int32_t col, int32_t pos) {
119 module(row - 2, col - 2, pos, 1);
120 module(row - 2, col - 1, pos, 2);
121 module(row - 1, col - 2, pos, 3);
122 module(row - 1, col - 1, pos, 4);
123 module(row - 1, col, pos, 5);
124 module(row, col - 2, pos, 6);
125 module(row, col - 1, pos, 7);
126 module(row, col, pos, 8);
127 }
corner1(int32_t pos)128 void CBC_DefaultPlacement::corner1(int32_t pos) {
129 module(m_numrows - 1, 0, pos, 1);
130 module(m_numrows - 1, 1, pos, 2);
131 module(m_numrows - 1, 2, pos, 3);
132 module(0, m_numcols - 2, pos, 4);
133 module(0, m_numcols - 1, pos, 5);
134 module(1, m_numcols - 1, pos, 6);
135 module(2, m_numcols - 1, pos, 7);
136 module(3, m_numcols - 1, pos, 8);
137 }
corner2(int32_t pos)138 void CBC_DefaultPlacement::corner2(int32_t pos) {
139 module(m_numrows - 3, 0, pos, 1);
140 module(m_numrows - 2, 0, pos, 2);
141 module(m_numrows - 1, 0, pos, 3);
142 module(0, m_numcols - 4, pos, 4);
143 module(0, m_numcols - 3, pos, 5);
144 module(0, m_numcols - 2, pos, 6);
145 module(0, m_numcols - 1, pos, 7);
146 module(1, m_numcols - 1, pos, 8);
147 }
corner3(int32_t pos)148 void CBC_DefaultPlacement::corner3(int32_t pos) {
149 module(m_numrows - 3, 0, pos, 1);
150 module(m_numrows - 2, 0, pos, 2);
151 module(m_numrows - 1, 0, pos, 3);
152 module(0, m_numcols - 2, pos, 4);
153 module(0, m_numcols - 1, pos, 5);
154 module(1, m_numcols - 1, pos, 6);
155 module(2, m_numcols - 1, pos, 7);
156 module(3, m_numcols - 1, pos, 8);
157 }
corner4(int32_t pos)158 void CBC_DefaultPlacement::corner4(int32_t pos) {
159 module(m_numrows - 1, 0, pos, 1);
160 module(m_numrows - 1, m_numcols - 1, pos, 2);
161 module(0, m_numcols - 3, pos, 3);
162 module(0, m_numcols - 2, pos, 4);
163 module(0, m_numcols - 1, pos, 5);
164 module(1, m_numcols - 3, pos, 6);
165 module(1, m_numcols - 2, pos, 7);
166 module(1, m_numcols - 1, pos, 8);
167 }
168