| 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 BITSTUFFER2_H |
| 25 | #define BITSTUFFER2_H |
| 26 | |
| 27 | #include <vector> |
| 28 | #include <cstring> |
| 29 | #include <utility> |
| 30 | #include "Defines.h" |
| 31 | |
| 32 | NAMESPACE_LERC_START |
| 33 | |
| 34 | /** Bit stuffer, for writing unsigned int arrays compressed lossless |
| 35 | * |
| 36 | */ |
| 37 | |
| 38 | class BitStuffer2 |
| 39 | { |
| 40 | public: |
| 41 | BitStuffer2() {} |
| 42 | ~BitStuffer2() {} |
| 43 | |
| 44 | // dst buffer is already allocated. byte ptr is moved like a file pointer. |
| 45 | bool EncodeSimple(Byte** ppByte, const std::vector<unsigned int>& dataVec, int lerc2Version) const; |
| 46 | bool EncodeLut(Byte** ppByte, const std::vector<std::pair<unsigned int, unsigned int> >& sortedDataVec, int lerc2Version) const; |
| 47 | bool Decode(const Byte** ppByte, size_t& nBytesRemaining, std::vector<unsigned int>& dataVec, size_t maxElementCount, int lerc2Version) const; |
| 48 | |
| 49 | static unsigned int ComputeNumBytesNeededSimple(unsigned int numElem, unsigned int maxElem); |
| 50 | static unsigned int ComputeNumBytesNeededLut(const std::vector<std::pair<unsigned int, unsigned int> >& sortedDataVec, bool& doLut); |
| 51 | |
| 52 | private: |
| 53 | mutable std::vector<unsigned int> m_tmpLutVec, m_tmpIndexVec, m_tmpBitStuffVec; |
| 54 | |
| 55 | static void BitStuff_Before_Lerc2v3(Byte** ppByte, const std::vector<unsigned int>& dataVec, int numBits); |
| 56 | bool BitUnStuff_Before_Lerc2v3(const Byte** ppByte, size_t& nBytesRemaining, std::vector<unsigned int>& dataVec, unsigned int numElements, int numBits) const; |
| 57 | void BitStuff(Byte** ppByte, const std::vector<unsigned int>& dataVec, int numBits) const; |
| 58 | bool BitUnStuff(const Byte** ppByte, size_t& nBytesRemaining, std::vector<unsigned int>& dataVec, unsigned int numElements, int numBits) const; |
| 59 | |
| 60 | static bool EncodeUInt(Byte** ppByte, unsigned int k, int numBytes); // numBytes = 1, 2, or 4 |
| 61 | static bool DecodeUInt(const Byte** ppByte, size_t& nBytesRemaining, unsigned int& k, int numBytes); |
| 62 | static int NumBytesUInt(unsigned int k) { return (k < 256) ? 1 : (k < (1 << 16)) ? 2 : 4; } |
| 63 | static unsigned int NumTailBytesNotNeeded(unsigned int numElem, int numBits); |
| 64 | }; |
| 65 | |
| 66 | // -------------------------------------------------------------------------- ; |
| 67 | |
| 68 | inline unsigned int BitStuffer2::ComputeNumBytesNeededSimple(unsigned int numElem, unsigned int maxElem) |
| 69 | { |
| 70 | int numBits = 0; |
| 71 | while ((numBits < 32) && (maxElem >> numBits)) |
| 72 | numBits++; |
| 73 | return 1 + NumBytesUInt(numElem) + ((numElem * numBits + 7) >> 3); |
| 74 | } |
| 75 | |
| 76 | // -------------------------------------------------------------------------- ; |
| 77 | |
| 78 | inline bool BitStuffer2::EncodeUInt(Byte** ppByte, unsigned int k, int numBytes) |
| 79 | { |
| 80 | Byte* ptr = *ppByte; |
| 81 | |
| 82 | if (numBytes == 1) |
| 83 | *ptr = (Byte)k; |
| 84 | else if (numBytes == 2) |
| 85 | { |
| 86 | unsigned short kShort = (unsigned short)k; |
| 87 | memcpy(ptr, &kShort, sizeof(unsigned short)); |
| 88 | } |
| 89 | else if (numBytes == 4) |
| 90 | memcpy(ptr, &k, sizeof(unsigned int)); |
| 91 | else |
| 92 | return false; |
| 93 | |
| 94 | *ppByte += numBytes; |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | // -------------------------------------------------------------------------- ; |
| 99 | |
| 100 | inline bool BitStuffer2::DecodeUInt(const Byte** ppByte, size_t& nBytesRemaining, unsigned int& k, int numBytes) |
| 101 | { |
| 102 | if (nBytesRemaining < (size_t)numBytes) |
| 103 | return false; |
| 104 | |
| 105 | const Byte* ptr = *ppByte; |
| 106 | |
| 107 | if (numBytes == 1) |
| 108 | k = *ptr; |
| 109 | else if (numBytes == 2) |
| 110 | { |
| 111 | unsigned short s; |
| 112 | memcpy(&s, ptr, sizeof(unsigned short)); |
| 113 | k = s; |
| 114 | } |
| 115 | else if (numBytes == 4) |
| 116 | memcpy(&k, ptr, sizeof(unsigned int)); |
| 117 | else |
| 118 | return false; |
| 119 | |
| 120 | *ppByte += numBytes; |
| 121 | nBytesRemaining -= numBytes; |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | // -------------------------------------------------------------------------- ; |
| 126 | |
| 127 | inline unsigned int BitStuffer2::NumTailBytesNotNeeded(unsigned int numElem, int numBits) |
| 128 | { |
| 129 | int numBitsTail = ((unsigned long long)numElem * numBits) & 31; |
| 130 | int numBytesTail = (numBitsTail + 7) >> 3; |
| 131 | return (numBytesTail > 0) ? 4 - numBytesTail : 0; |
| 132 | } |
| 133 | |
| 134 | // -------------------------------------------------------------------------- ; |
| 135 | |
| 136 | NAMESPACE_LERC_END |
| 137 | #endif |