blob: 4fff7b1a84bd30952376528b259ad5700b78bc0e [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
23#include "xfa/fxbarcode/common/BC_CommonBitArray.h"
24#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h"
25#include "xfa/fxbarcode/utils.h"
26
27CBC_CommonBitMatrix::CBC_CommonBitMatrix() {
28 m_width = 0;
29 m_height = 0;
30 m_rowSize = 0;
31 m_bits = NULL;
32}
33void 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);
39 FXSYS_memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t));
40}
41void 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);
47 FXSYS_memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t));
48}
49CBC_CommonBitMatrix::~CBC_CommonBitMatrix() {
50 FX_Free(m_bits);
51}
52FX_BOOL CBC_CommonBitMatrix::Get(int32_t x, int32_t y) {
53 int32_t offset = y * m_rowSize + (x >> 5);
54 if (offset >= m_rowSize * m_height || offset < 0) {
55 return false;
56 }
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}
59int32_t* CBC_CommonBitMatrix::GetBits() {
60 return m_bits;
61}
62void 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}
69void 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}
73void CBC_CommonBitMatrix::Clear() {
74 FXSYS_memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t));
75}
76void 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}
104CBC_CommonBitArray* CBC_CommonBitMatrix::GetRow(int32_t y,
105 CBC_CommonBitArray* row) {
106 CBC_CommonBitArray* rowArray = NULL;
107 if (row == NULL || row->GetSize() < m_width) {
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}
119void 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}
126void CBC_CommonBitMatrix::SetCol(int32_t y, CBC_CommonBitArray* col) {
127 for (int32_t i = 0; i < col->GetBits().GetSize(); i++) {
128 m_bits[i * m_rowSize + y] = col->GetBitArray()[i];
129 }
130}
131int32_t CBC_CommonBitMatrix::GetWidth() {
132 return m_width;
133}
134int32_t CBC_CommonBitMatrix::GetHeight() {
135 return m_height;
136}
137int32_t CBC_CommonBitMatrix::GetRowSize() {
138 return m_rowSize;
139}
140int32_t CBC_CommonBitMatrix::GetDimension(int32_t& e) {
141 if (m_width != m_height) {
142 e = BCExceptionCanNotCallGetDimensionOnNonSquareMatrix;
143 return 0;
144 }
145 return m_width;
146}