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