| 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 RLE_H |
| 25 | #define RLE_H |
| 26 | |
| 27 | #include <cstddef> |
| 28 | #include "Defines.h" |
| 29 | |
| 30 | NAMESPACE_LERC_START |
| 31 | |
| 32 | /** RLE: |
| 33 | * run length encode a byte array |
| 34 | * |
| 35 | * best case resize factor (all bytes are the same): |
| 36 | * (((n + 1) * 3 / 32767 + 2) / n) ~= (3 / 32767) ~= 0.00009 |
| 37 | * |
| 38 | * worst case resize factor (no stretch of same bytes): |
| 39 | * ((n + (n + 1) * 2 / 32767 + 2) / n) ~= (1 + 2 / 32767) ~= 1.00006 |
| 40 | */ |
| 41 | |
| 42 | class RLE |
| 43 | { |
| 44 | public: |
| 45 | RLE() : m_minNumEven(5) {} |
| 46 | ~RLE() {} |
| 47 | |
| 48 | size_t computeNumBytesRLE(const Byte* arr, size_t numBytes) const; |
| 49 | |
| 50 | // when done, call |
| 51 | // delete[] *arrRLE; |
| 52 | bool compress(const Byte* arr, size_t numBytes, |
| 53 | Byte** arrRLE, size_t& numBytesRLE, bool verify = false) const; |
| 54 | |
| 55 | // when done, call |
| 56 | // delete[] *arr; |
| 57 | static bool decompress(const Byte* arrRLE, size_t nBytesRemaining, Byte** arr, size_t& numBytes); |
| 58 | |
| 59 | // arr already allocated, just fill |
| 60 | static bool decompress(const Byte* arrRLE, size_t nBytesRemaining, Byte* arr, size_t arrSize); |
| 61 | |
| 62 | protected: |
| 63 | int m_minNumEven; |
| 64 | |
| 65 | static void writeCount(short cnt, Byte** ppCnt, Byte** ppDst); |
| 66 | static short readCount(const Byte** ppCnt); |
| 67 | |
| 68 | }; |
| 69 | |
| 70 | NAMESPACE_LERC_END |
| 71 | #endif |