Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 | |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 23 | #include "fxbarcode/common/BC_CommonBitArray.h" |
| 24 | #include "fxbarcode/common/BC_CommonBitMatrix.h" |
| 25 | #include "fxbarcode/utils.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 26 | |
| 27 | CBC_CommonBitMatrix::CBC_CommonBitMatrix() { |
| 28 | m_width = 0; |
| 29 | m_height = 0; |
| 30 | m_rowSize = 0; |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 31 | m_bits = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 32 | } |
| 33 | void CBC_CommonBitMatrix::Init(int32_t dimension) { |
| 34 | m_width = dimension; |
| 35 | m_height = dimension; |
| 36 | int32_t rowSize = (m_height + 31) >> 5; |
| 37 | m_rowSize = rowSize; |
| 38 | m_bits = FX_Alloc2D(int32_t, m_rowSize, m_height); |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame^] | 39 | memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 40 | } |
| 41 | void CBC_CommonBitMatrix::Init(int32_t width, int32_t height) { |
| 42 | m_width = width; |
| 43 | m_height = height; |
| 44 | int32_t rowSize = (width + 31) >> 5; |
| 45 | m_rowSize = rowSize; |
| 46 | m_bits = FX_Alloc2D(int32_t, m_rowSize, m_height); |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame^] | 47 | memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 48 | } |
| 49 | CBC_CommonBitMatrix::~CBC_CommonBitMatrix() { |
| 50 | FX_Free(m_bits); |
| 51 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 52 | bool CBC_CommonBitMatrix::Get(int32_t x, int32_t y) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 53 | int32_t offset = y * m_rowSize + (x >> 5); |
| 54 | if (offset >= m_rowSize * m_height || offset < 0) { |
| 55 | return false; |
| 56 | } |
tsepez | 736f28a | 2016-03-25 14:19:51 -0700 | [diff] [blame] | 57 | return ((((uint32_t)m_bits[offset]) >> (x & 0x1f)) & 1) != 0; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 58 | } |
| 59 | int32_t* CBC_CommonBitMatrix::GetBits() { |
| 60 | return m_bits; |
| 61 | } |
| 62 | void CBC_CommonBitMatrix::Set(int32_t x, int32_t y) { |
| 63 | int32_t offset = y * m_rowSize + (x >> 5); |
| 64 | if (offset >= m_rowSize * m_height || offset < 0) { |
| 65 | return; |
| 66 | } |
| 67 | m_bits[offset] |= 1 << (x & 0x1f); |
| 68 | } |
| 69 | void CBC_CommonBitMatrix::Flip(int32_t x, int32_t y) { |
| 70 | int32_t offset = y * m_rowSize + (x >> 5); |
| 71 | m_bits[offset] ^= 1 << (x & 0x1f); |
| 72 | } |
| 73 | void CBC_CommonBitMatrix::Clear() { |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame^] | 74 | memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 75 | } |
| 76 | void CBC_CommonBitMatrix::SetRegion(int32_t left, |
| 77 | int32_t top, |
| 78 | int32_t width, |
| 79 | int32_t height, |
| 80 | int32_t& e) { |
| 81 | if (top < 0 || left < 0) { |
| 82 | e = BCExceptionLeftAndTopMustBeNonnegative; |
| 83 | return; |
| 84 | } |
| 85 | if (height < 1 || width < 1) { |
| 86 | e = BCExceptionHeightAndWidthMustBeAtLeast1; |
| 87 | return; |
| 88 | } |
| 89 | int32_t right = left + width; |
| 90 | int32_t bottom = top + height; |
| 91 | if (m_height < bottom || m_width < right) { |
| 92 | e = BCExceptionRegionMustFitInsideMatrix; |
| 93 | return; |
| 94 | } |
| 95 | int32_t y; |
| 96 | for (y = top; y < bottom; y++) { |
| 97 | int32_t offset = y * m_rowSize; |
| 98 | int32_t x; |
| 99 | for (x = left; x < right; x++) { |
| 100 | m_bits[offset + (x >> 5)] |= 1 << (x & 0x1f); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | CBC_CommonBitArray* CBC_CommonBitMatrix::GetRow(int32_t y, |
| 105 | CBC_CommonBitArray* row) { |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 106 | CBC_CommonBitArray* rowArray = nullptr; |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame] | 107 | if (!row || static_cast<int32_t>(row->GetSize()) < m_width) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 108 | rowArray = new CBC_CommonBitArray(m_width); |
| 109 | } else { |
| 110 | rowArray = new CBC_CommonBitArray(row); |
| 111 | } |
| 112 | int32_t offset = y * m_rowSize; |
| 113 | int32_t x; |
| 114 | for (x = 0; x < m_rowSize; x++) { |
| 115 | rowArray->SetBulk(x << 5, m_bits[offset + x]); |
| 116 | } |
| 117 | return rowArray; |
| 118 | } |
| 119 | void CBC_CommonBitMatrix::SetRow(int32_t y, CBC_CommonBitArray* row) { |
| 120 | int32_t l = y * m_rowSize; |
| 121 | for (int32_t i = 0; i < m_rowSize; i++) { |
| 122 | m_bits[l] = row->GetBitArray()[i]; |
| 123 | l++; |
| 124 | } |
| 125 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame] | 126 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 127 | void CBC_CommonBitMatrix::SetCol(int32_t y, CBC_CommonBitArray* col) { |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame] | 128 | for (size_t i = 0; i < col->GetBits().size(); ++i) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 129 | m_bits[i * m_rowSize + y] = col->GetBitArray()[i]; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 130 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame] | 131 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 132 | int32_t CBC_CommonBitMatrix::GetWidth() { |
| 133 | return m_width; |
| 134 | } |
| 135 | int32_t CBC_CommonBitMatrix::GetHeight() { |
| 136 | return m_height; |
| 137 | } |
| 138 | int32_t CBC_CommonBitMatrix::GetRowSize() { |
| 139 | return m_rowSize; |
| 140 | } |
| 141 | int32_t CBC_CommonBitMatrix::GetDimension(int32_t& e) { |
| 142 | if (m_width != m_height) { |
| 143 | e = BCExceptionCanNotCallGetDimensionOnNonSquareMatrix; |
| 144 | return 0; |
| 145 | } |
| 146 | return m_width; |
| 147 | } |