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_CommonBitArray.h" 25 #include "BC_CommonBitMatrix.h" 26 CBC_CommonBitMatrix::CBC_CommonBitMatrix() { 27 m_width = 0; 28 m_height = 0; 29 m_rowSize = 0; 30 m_bits = NULL; 31 } 32 void CBC_CommonBitMatrix::Init(int32_t dimension) { 33 m_width = dimension; 34 m_height = dimension; 35 int32_t rowSize = (m_height + 31) >> 5; 36 m_rowSize = rowSize; 37 m_bits = FX_Alloc2D(int32_t, m_rowSize, m_height); 38 FXSYS_memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); 39 } 40 void CBC_CommonBitMatrix::Init(int32_t width, int32_t height) { 41 m_width = width; 42 m_height = height; 43 int32_t rowSize = (width + 31) >> 5; 44 m_rowSize = rowSize; 45 m_bits = FX_Alloc2D(int32_t, m_rowSize, m_height); 46 FXSYS_memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); 47 } 48 CBC_CommonBitMatrix::~CBC_CommonBitMatrix() { 49 if (m_bits != NULL) { 50 FX_Free(m_bits); 51 } 52 m_bits = NULL; 53 m_height = m_width = m_rowSize = 0; 54 } 55 FX_BOOL CBC_CommonBitMatrix::Get(int32_t x, int32_t y) { 56 int32_t offset = y * m_rowSize + (x >> 5); 57 if (offset >= m_rowSize * m_height || offset < 0) { 58 return false; 59 } 60 return ((((FX_DWORD)m_bits[offset]) >> (x & 0x1f)) & 1) != 0; 61 } 62 int32_t* CBC_CommonBitMatrix::GetBits() { 63 return m_bits; 64 } 65 void CBC_CommonBitMatrix::Set(int32_t x, int32_t y) { 66 int32_t offset = y * m_rowSize + (x >> 5); 67 if (offset >= m_rowSize * m_height || offset < 0) { 68 return; 69 } 70 m_bits[offset] |= 1 << (x & 0x1f); 71 } 72 void CBC_CommonBitMatrix::Flip(int32_t x, int32_t y) { 73 int32_t offset = y * m_rowSize + (x >> 5); 74 m_bits[offset] ^= 1 << (x & 0x1f); 75 } 76 void CBC_CommonBitMatrix::Clear() { 77 FXSYS_memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); 78 } 79 void CBC_CommonBitMatrix::SetRegion(int32_t left, 80 int32_t top, 81 int32_t width, 82 int32_t height, 83 int32_t& e) { 84 if (top < 0 || left < 0) { 85 e = BCExceptionLeftAndTopMustBeNonnegative; 86 return; 87 } 88 if (height < 1 || width < 1) { 89 e = BCExceptionHeightAndWidthMustBeAtLeast1; 90 return; 91 } 92 int32_t right = left + width; 93 int32_t bottom = top + height; 94 if (m_height < bottom || m_width < right) { 95 e = BCExceptionRegionMustFitInsideMatrix; 96 return; 97 } 98 int32_t y; 99 for (y = top; y < bottom; y++) { 100 int32_t offset = y * m_rowSize; 101 int32_t x; 102 for (x = left; x < right; x++) { 103 m_bits[offset + (x >> 5)] |= 1 << (x & 0x1f); 104 } 105 } 106 } 107 CBC_CommonBitArray* CBC_CommonBitMatrix::GetRow(int32_t y, 108 CBC_CommonBitArray* row) { 109 CBC_CommonBitArray* rowArray = NULL; 110 if (row == NULL || row->GetSize() < m_width) { 111 rowArray = new CBC_CommonBitArray(m_width); 112 } else { 113 rowArray = new CBC_CommonBitArray(row); 114 } 115 int32_t offset = y * m_rowSize; 116 int32_t x; 117 for (x = 0; x < m_rowSize; x++) { 118 rowArray->SetBulk(x << 5, m_bits[offset + x]); 119 } 120 return rowArray; 121 } 122 void CBC_CommonBitMatrix::SetRow(int32_t y, CBC_CommonBitArray* row) { 123 int32_t l = y * m_rowSize; 124 for (int32_t i = 0; i < m_rowSize; i++) { 125 m_bits[l] = row->GetBitArray()[i]; 126 l++; 127 } 128 } 129 void CBC_CommonBitMatrix::SetCol(int32_t y, CBC_CommonBitArray* col) { 130 for (int32_t i = 0; i < col->GetBits().GetSize(); i++) { 131 m_bits[i * m_rowSize + y] = col->GetBitArray()[i]; 132 } 133 } 134 int32_t CBC_CommonBitMatrix::GetWidth() { 135 return m_width; 136 } 137 int32_t CBC_CommonBitMatrix::GetHeight() { 138 return m_height; 139 } 140 int32_t CBC_CommonBitMatrix::GetRowSize() { 141 return m_rowSize; 142 } 143 int32_t CBC_CommonBitMatrix::GetDimension(int32_t& e) { 144 if (m_width != m_height) { 145 e = BCExceptionCanNotCallGetDimensionOnNonSquareMatrix; 146 return 0; 147 } 148 return m_width; 149 } 150