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