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_CommonBitMatrix.h" |
Tom Sepez | 0804b3f | 2017-05-01 14:06:10 -0700 | [diff] [blame] | 24 | |
| 25 | #include "fxbarcode/common/BC_CommonBitArray.h" |
Dan Sinclair | e778668 | 2017-03-29 15:18:41 -0400 | [diff] [blame] | 26 | #include "fxbarcode/utils.h" |
Tom Sepez | 0804b3f | 2017-05-01 14:06:10 -0700 | [diff] [blame] | 27 | #include "third_party/base/ptr_util.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 28 | |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 29 | CBC_CommonBitMatrix::CBC_CommonBitMatrix() {} |
| 30 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 31 | void CBC_CommonBitMatrix::Init(int32_t dimension) { |
| 32 | m_width = dimension; |
| 33 | m_height = dimension; |
| 34 | int32_t rowSize = (m_height + 31) >> 5; |
| 35 | m_rowSize = rowSize; |
| 36 | m_bits = FX_Alloc2D(int32_t, m_rowSize, m_height); |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 37 | memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 38 | } |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 39 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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); |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 46 | memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 47 | } |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 48 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 49 | CBC_CommonBitMatrix::~CBC_CommonBitMatrix() { |
| 50 | FX_Free(m_bits); |
| 51 | } |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 52 | |
| 53 | bool CBC_CommonBitMatrix::Get(int32_t x, int32_t y) const { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 54 | int32_t offset = y * m_rowSize + (x >> 5); |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 55 | if (offset >= m_rowSize * m_height || offset < 0) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 56 | return false; |
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 | } |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 59 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 60 | void CBC_CommonBitMatrix::Set(int32_t x, int32_t y) { |
| 61 | int32_t offset = y * m_rowSize + (x >> 5); |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 62 | if (offset >= m_rowSize * m_height || offset < 0) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 63 | return; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 64 | m_bits[offset] |= 1 << (x & 0x1f); |
| 65 | } |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 66 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 67 | void CBC_CommonBitMatrix::Flip(int32_t x, int32_t y) { |
| 68 | int32_t offset = y * m_rowSize + (x >> 5); |
| 69 | m_bits[offset] ^= 1 << (x & 0x1f); |
| 70 | } |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 71 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 72 | void CBC_CommonBitMatrix::Clear() { |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 73 | memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 74 | } |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 75 | |
| 76 | bool CBC_CommonBitMatrix::SetRegion(int32_t left, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 77 | int32_t top, |
| 78 | int32_t width, |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 79 | int32_t height) { |
| 80 | if (top < 0 || left < 0 || height < 1 || width < 1) |
| 81 | return false; |
| 82 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 83 | int32_t right = left + width; |
| 84 | int32_t bottom = top + height; |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 85 | if (m_height < bottom || m_width < right) |
| 86 | return false; |
| 87 | |
| 88 | for (int32_t y = top; y < bottom; y++) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 89 | int32_t offset = y * m_rowSize; |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 90 | for (int32_t x = left; x < right; x++) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 91 | m_bits[offset + (x >> 5)] |= 1 << (x & 0x1f); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 92 | } |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 93 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 94 | } |
Lei Zhang | 1b22880 | 2017-04-05 18:42:22 -0700 | [diff] [blame] | 95 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 96 | void CBC_CommonBitMatrix::SetRow(int32_t y, CBC_CommonBitArray* row) { |
| 97 | int32_t l = y * m_rowSize; |
| 98 | for (int32_t i = 0; i < m_rowSize; i++) { |
| 99 | m_bits[l] = row->GetBitArray()[i]; |
| 100 | l++; |
| 101 | } |
| 102 | } |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame] | 103 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 104 | void CBC_CommonBitMatrix::SetCol(int32_t y, CBC_CommonBitArray* col) { |
Tom Sepez | 8b6186f | 2017-03-28 12:06:45 -0700 | [diff] [blame] | 105 | for (size_t i = 0; i < col->GetBits().size(); ++i) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 106 | m_bits[i * m_rowSize + y] = col->GetBitArray()[i]; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 107 | } |