blob: 6fe447db6f808ad31fca5224cd3a3ba3b8731732 [file] [log] [blame]
Dan Sinclair1770c022016-03-14 14:14:16 -04001// 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 Sinclaire7786682017-03-29 15:18:41 -040023#include "fxbarcode/common/BC_CommonBitMatrix.h"
Tom Sepez0804b3f2017-05-01 14:06:10 -070024
25#include "fxbarcode/common/BC_CommonBitArray.h"
Dan Sinclaire7786682017-03-29 15:18:41 -040026#include "fxbarcode/utils.h"
Tom Sepez0804b3f2017-05-01 14:06:10 -070027#include "third_party/base/ptr_util.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040028
Lei Zhang1b228802017-04-05 18:42:22 -070029CBC_CommonBitMatrix::CBC_CommonBitMatrix() {}
30
Dan Sinclair1770c022016-03-14 14:14:16 -040031void 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 Sinclair1c5d0b42017-04-03 15:05:11 -040037 memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t));
Dan Sinclair1770c022016-03-14 14:14:16 -040038}
Lei Zhang1b228802017-04-05 18:42:22 -070039
Dan Sinclair1770c022016-03-14 14:14:16 -040040void 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 Sinclair1c5d0b42017-04-03 15:05:11 -040046 memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t));
Dan Sinclair1770c022016-03-14 14:14:16 -040047}
Lei Zhang1b228802017-04-05 18:42:22 -070048
Dan Sinclair1770c022016-03-14 14:14:16 -040049CBC_CommonBitMatrix::~CBC_CommonBitMatrix() {
50 FX_Free(m_bits);
51}
Lei Zhang1b228802017-04-05 18:42:22 -070052
53bool CBC_CommonBitMatrix::Get(int32_t x, int32_t y) const {
Dan Sinclair1770c022016-03-14 14:14:16 -040054 int32_t offset = y * m_rowSize + (x >> 5);
Lei Zhang1b228802017-04-05 18:42:22 -070055 if (offset >= m_rowSize * m_height || offset < 0)
Dan Sinclair1770c022016-03-14 14:14:16 -040056 return false;
tsepez736f28a2016-03-25 14:19:51 -070057 return ((((uint32_t)m_bits[offset]) >> (x & 0x1f)) & 1) != 0;
Dan Sinclair1770c022016-03-14 14:14:16 -040058}
Lei Zhang1b228802017-04-05 18:42:22 -070059
Dan Sinclair1770c022016-03-14 14:14:16 -040060void CBC_CommonBitMatrix::Set(int32_t x, int32_t y) {
61 int32_t offset = y * m_rowSize + (x >> 5);
Lei Zhang1b228802017-04-05 18:42:22 -070062 if (offset >= m_rowSize * m_height || offset < 0)
Dan Sinclair1770c022016-03-14 14:14:16 -040063 return;
Dan Sinclair1770c022016-03-14 14:14:16 -040064 m_bits[offset] |= 1 << (x & 0x1f);
65}
Lei Zhang1b228802017-04-05 18:42:22 -070066
Dan Sinclair1770c022016-03-14 14:14:16 -040067void 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 Zhang1b228802017-04-05 18:42:22 -070071
Dan Sinclair1770c022016-03-14 14:14:16 -040072void CBC_CommonBitMatrix::Clear() {
Dan Sinclair1c5d0b42017-04-03 15:05:11 -040073 memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t));
Dan Sinclair1770c022016-03-14 14:14:16 -040074}
Lei Zhang1b228802017-04-05 18:42:22 -070075
76bool CBC_CommonBitMatrix::SetRegion(int32_t left,
Dan Sinclair1770c022016-03-14 14:14:16 -040077 int32_t top,
78 int32_t width,
Lei Zhang1b228802017-04-05 18:42:22 -070079 int32_t height) {
80 if (top < 0 || left < 0 || height < 1 || width < 1)
81 return false;
82
Dan Sinclair1770c022016-03-14 14:14:16 -040083 int32_t right = left + width;
84 int32_t bottom = top + height;
Lei Zhang1b228802017-04-05 18:42:22 -070085 if (m_height < bottom || m_width < right)
86 return false;
87
88 for (int32_t y = top; y < bottom; y++) {
Dan Sinclair1770c022016-03-14 14:14:16 -040089 int32_t offset = y * m_rowSize;
Lei Zhang1b228802017-04-05 18:42:22 -070090 for (int32_t x = left; x < right; x++)
Dan Sinclair1770c022016-03-14 14:14:16 -040091 m_bits[offset + (x >> 5)] |= 1 << (x & 0x1f);
Dan Sinclair1770c022016-03-14 14:14:16 -040092 }
Lei Zhang1b228802017-04-05 18:42:22 -070093 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -040094}
Lei Zhang1b228802017-04-05 18:42:22 -070095
Dan Sinclair1770c022016-03-14 14:14:16 -040096void 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 Sepez8b6186f2017-03-28 12:06:45 -0700103
Dan Sinclair1770c022016-03-14 14:14:16 -0400104void CBC_CommonBitMatrix::SetCol(int32_t y, CBC_CommonBitArray* col) {
Tom Sepez8b6186f2017-03-28 12:06:45 -0700105 for (size_t i = 0; i < col->GetBits().size(); ++i)
Dan Sinclair1770c022016-03-14 14:14:16 -0400106 m_bits[i * m_rowSize + y] = col->GetBitArray()[i];
Dan Sinclair1770c022016-03-14 14:14:16 -0400107}