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