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 #ifndef BITMASK_H
25 #define BITMASK_H
26 
27 #include "Defines.h"
28 
29 NAMESPACE_LERC_START
30 
31   typedef unsigned char Byte;
32 
33   /** BitMask - Convenient and fast access to binary mask bits
34   *
35   */
36 
37   class BitMask
38   {
39   public:
BitMask()40     BitMask() : m_pBits(nullptr), m_nCols(0), m_nRows(0)  {}
BitMask(int nCols,int nRows)41     BitMask(int nCols, int nRows) : m_pBits(nullptr), m_nCols(0), m_nRows(0) { SetSize(nCols, nRows); }
42     BitMask(const BitMask& src);
~BitMask()43     virtual ~BitMask()                        { Clear(); }
44 
45     BitMask& operator= (const BitMask& src);
46 
47     // 1: valid, 0: not valid
IsValid(int k)48     Byte IsValid(int k) const                 { return (m_pBits[k >> 3] & Bit(k)) > 0; }
IsValid(int row,int col)49     Byte IsValid(int row, int col) const      { return IsValid(row * m_nCols + col); }
50 
SetValid(int k)51     void SetValid(int k) const                { m_pBits[k >> 3] |= Bit(k); }
SetValid(int row,int col)52     void SetValid(int row, int col) const     { SetValid(row * m_nCols + col); }
53 
SetInvalid(int k)54     void SetInvalid(int k) const              { m_pBits[k >> 3] &= ~Bit(k); }
SetInvalid(int row,int col)55     void SetInvalid(int row, int col) const   { SetInvalid(row * m_nCols + col); }
56 
57     void SetAllValid() const;
58     void SetAllInvalid() const;
59 
60     bool SetSize(int nCols, int nRows);
61 
GetWidth()62     int GetWidth() const                      { return m_nCols; }
GetHeight()63     int GetHeight() const                     { return m_nRows; }
Size()64     int Size() const                          { return (m_nCols * m_nRows + 7) >> 3; }
Bits()65     const Byte* Bits() const                  { return m_pBits; }
Bits()66     Byte* Bits()                              { return m_pBits; }
Bit(int k)67     static Byte Bit(int k)                    { return (1 << 7) >> (k & 7); }
68 
69     int CountValidBits() const;
70     void Clear();
71 
72   private:
73     Byte*  m_pBits;
74     int    m_nCols, m_nRows;
75   };
76 NAMESPACE_LERC_END
77 
78 #endif
79