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 | |
| 23 | #include "xfa/fxbarcode/common/BC_CommonBitArray.h" |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 24 | |
| 25 | #include <utility> |
| 26 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 27 | #include "xfa/fxbarcode/utils.h" |
| 28 | |
| 29 | CBC_CommonBitArray::CBC_CommonBitArray(CBC_CommonBitArray* array) { |
| 30 | m_size = array->GetSize(); |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 31 | m_bits = array->GetBits(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 32 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 33 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 34 | CBC_CommonBitArray::CBC_CommonBitArray() { |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 35 | m_bits.resize(1); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 36 | m_size = 0; |
| 37 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 38 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 39 | CBC_CommonBitArray::CBC_CommonBitArray(int32_t size) { |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 40 | m_bits.resize((size + 31) >> 5); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 41 | m_size = size; |
| 42 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 43 | |
| 44 | CBC_CommonBitArray::~CBC_CommonBitArray() {} |
| 45 | |
| 46 | size_t CBC_CommonBitArray::GetSize() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 47 | return m_size; |
| 48 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 49 | |
| 50 | std::vector<int32_t>& CBC_CommonBitArray::GetBits() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 51 | return m_bits; |
| 52 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 53 | |
| 54 | size_t CBC_CommonBitArray::GetSizeInBytes() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 55 | return (m_size + 7) >> 3; |
| 56 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 57 | |
| 58 | bool CBC_CommonBitArray::Get(size_t i) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 59 | return (m_bits[i >> 5] & (1 << (i & 0x1f))) != 0; |
| 60 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 61 | |
| 62 | void CBC_CommonBitArray::Set(size_t i) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 63 | m_bits[i >> 5] |= 1 << (i & 0x1F); |
| 64 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 65 | |
| 66 | void CBC_CommonBitArray::Flip(size_t i) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 67 | m_bits[i >> 5] ^= 1 << (i & 0x1F); |
| 68 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 69 | |
| 70 | void CBC_CommonBitArray::SetBulk(size_t i, int32_t newBits) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 71 | m_bits[i >> 5] = newBits; |
| 72 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 73 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 74 | void CBC_CommonBitArray::Clear() { |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 75 | for (auto& value : m_bits) |
| 76 | value = 0; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 77 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 78 | |
| 79 | bool CBC_CommonBitArray::IsRange(size_t start, |
| 80 | size_t end, |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 81 | bool value, |
| 82 | int32_t& e) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 83 | if (end < start) { |
| 84 | e = BCExceptionEndLessThanStart; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 85 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 86 | } |
| 87 | if (end == start) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 88 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 89 | } |
| 90 | end--; |
| 91 | int32_t firstInt = start >> 5; |
| 92 | int32_t lastInt = end >> 5; |
| 93 | int32_t i; |
| 94 | for (i = firstInt; i <= lastInt; i++) { |
| 95 | int32_t firstBit = i > firstInt ? 0 : start & 0x1F; |
| 96 | int32_t lastBit = i < lastInt ? 31 : end & 0x1F; |
| 97 | int32_t mask; |
| 98 | if (firstBit == 0 && lastBit == 31) { |
| 99 | mask = -1; |
| 100 | } else { |
| 101 | mask = 0; |
| 102 | for (int32_t j = firstBit; j <= lastBit; j++) { |
| 103 | mask |= 1 << j; |
| 104 | } |
| 105 | } |
| 106 | if ((m_bits[i] & mask) != (value ? mask : 0)) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 107 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 108 | } |
| 109 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 110 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 111 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 112 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 113 | int32_t* CBC_CommonBitArray::GetBitArray() { |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 114 | return m_bits.data(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 115 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 116 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 117 | void CBC_CommonBitArray::Reverse() { |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 118 | std::vector<int32_t> newBits(m_bits.size()); |
| 119 | for (size_t i = 0; i < m_size; i++) { |
| 120 | if (Get(m_size - i - 1)) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 121 | newBits[i >> 5] |= 1 << (i & 0x1F); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 122 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame^] | 123 | m_bits = std::move(newBits); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 124 | } |