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 "fxbarcode/common/BC_CommonBitMatrix.h"
24 
25 #include "fxbarcode/common/BC_CommonBitArray.h"
26 #include "fxbarcode/utils.h"
27 #include "third_party/base/ptr_util.h"
28 
CBC_CommonBitMatrix()29 CBC_CommonBitMatrix::CBC_CommonBitMatrix() {}
30 
Init(int32_t dimension)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);
37   memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t));
38 }
39 
Init(int32_t width,int32_t height)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);
46   memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t));
47 }
48 
~CBC_CommonBitMatrix()49 CBC_CommonBitMatrix::~CBC_CommonBitMatrix() {
50   FX_Free(m_bits);
51 }
52 
Get(int32_t x,int32_t y) const53 bool CBC_CommonBitMatrix::Get(int32_t x, int32_t y) const {
54   int32_t offset = y * m_rowSize + (x >> 5);
55   if (offset >= m_rowSize * m_height || offset < 0)
56     return false;
57   return ((((uint32_t)m_bits[offset]) >> (x & 0x1f)) & 1) != 0;
58 }
59 
Set(int32_t x,int32_t y)60 void CBC_CommonBitMatrix::Set(int32_t x, int32_t y) {
61   int32_t offset = y * m_rowSize + (x >> 5);
62   if (offset >= m_rowSize * m_height || offset < 0)
63     return;
64   m_bits[offset] |= 1 << (x & 0x1f);
65 }
66 
Flip(int32_t x,int32_t y)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 }
71 
Clear()72 void CBC_CommonBitMatrix::Clear() {
73   memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t));
74 }
75 
SetRegion(int32_t left,int32_t top,int32_t width,int32_t height)76 bool CBC_CommonBitMatrix::SetRegion(int32_t left,
77                                     int32_t top,
78                                     int32_t width,
79                                     int32_t height) {
80   if (top < 0 || left < 0 || height < 1 || width < 1)
81     return false;
82 
83   int32_t right = left + width;
84   int32_t bottom = top + height;
85   if (m_height < bottom || m_width < right)
86     return false;
87 
88   for (int32_t y = top; y < bottom; y++) {
89     int32_t offset = y * m_rowSize;
90     for (int32_t x = left; x < right; x++)
91       m_bits[offset + (x >> 5)] |= 1 << (x & 0x1f);
92   }
93   return true;
94 }
95 
SetRow(int32_t y,CBC_CommonBitArray * row)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 }
103 
SetCol(int32_t y,CBC_CommonBitArray * col)104 void CBC_CommonBitMatrix::SetCol(int32_t y, CBC_CommonBitArray* col) {
105   for (size_t i = 0; i < col->GetBits().size(); ++i)
106     m_bits[i * m_rowSize + y] = col->GetBitArray()[i];
107 }
108