-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitMask.cpp
More file actions
111 lines (84 loc) · 2.71 KB
/
BitMask.cpp
File metadata and controls
111 lines (84 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Copyright 2015 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
A local copy of the license and additional notices are located with the
source distribution at:
http://github.com/Esri/lerc/
Contributors: Thomas Maurer
*/
#include "Defines.h"
#include "BitMask.h"
#include <cstring>
USING_NAMESPACE_LERC
// -------------------------------------------------------------------------- ;
BitMask::BitMask(const BitMask& src) : m_pBits(nullptr)
{
SetSize(src.m_nCols, src.m_nRows);
if (m_pBits && src.m_pBits)
memcpy(m_pBits, src.m_pBits, Size());
}
// -------------------------------------------------------------------------- ;
BitMask& BitMask::operator= (const BitMask& src)
{
if (this == &src) return *this;
SetSize(src.m_nCols, src.m_nRows);
if (m_pBits && src.m_pBits)
memcpy(m_pBits, src.m_pBits, Size());
return *this;
}
// -------------------------------------------------------------------------- ;
void BitMask::SetAllValid() const
{
memset(m_pBits, 255, Size());
}
void BitMask::SetAllInvalid() const
{
memset(m_pBits, 0, Size());
}
// -------------------------------------------------------------------------- ;
bool BitMask::SetSize(int nCols, int nRows)
{
if (nCols != m_nCols || nRows != m_nRows)
{
Clear();
m_pBits = new Byte[(nCols * nRows + 7) >> 3];
m_nCols = nCols;
m_nRows = nRows;
}
return m_pBits != nullptr;
}
// -------------------------------------------------------------------------- ;
int BitMask::CountValidBits() const
{
const Byte numBitsHB[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
const Byte* ptr = m_pBits;
int sum = 0;
int i = Size();
while (i--)
{
sum += numBitsHB[*ptr & 15] + numBitsHB[*ptr >> 4];
ptr++;
}
// subtract undefined bits potentially contained in the last byte
for (int k = m_nCols * m_nRows; k < Size() * 8; k++)
if (IsValid(k))
sum--;
return sum;
}
// -------------------------------------------------------------------------- ;
void BitMask::Clear()
{
delete[] m_pBits;
m_pBits = nullptr;
m_nCols = 0;
m_nRows = 0;
}
// -------------------------------------------------------------------------- ;