| 1 | /* |
| 2 | Copyright 2021 - 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 | Analytical Raster Compression. |
| 22 | Original coding 2021 Yuriy Yakimenko |
| 23 | */ |
| 24 | |
| 25 | #ifndef FPL_LERC2_EXT_H |
| 26 | #define FPL_LERC2_EXT_H |
| 27 | |
| 28 | #include <cstdlib> |
| 29 | #include <vector> |
| 30 | #include "fpl_Predictor.h" |
| 31 | #include "fpl_UnitTypes.h" |
| 32 | #include "Defines.h" |
| 33 | |
| 34 | NAMESPACE_LERC_START |
| 35 | |
| 36 | class LosslessFPCompression |
| 37 | { |
| 38 | private: |
| 39 | |
| 40 | struct outBlockBuffer |
| 41 | { |
| 42 | char* compressed; |
| 43 | uint32_t compressed_size; // assuming that tile single byte-plane compressed size will not exceed 32 bits. |
| 44 | |
| 45 | unsigned char byte_index; // we store byte index in case we decide to parallelize processing |
| 46 | // and byte planes end up not in strict order. |
| 47 | unsigned char best_level; |
| 48 | |
| 49 | outBlockBuffer() |
| 50 | { |
| 51 | compressed = NULL; |
| 52 | compressed_size = 0; |
| 53 | byte_index = 0; |
| 54 | best_level = 0; |
| 55 | } |
| 56 | |
| 57 | outBlockBuffer(const outBlockBuffer&) = delete; // disable copy constructor |
| 58 | outBlockBuffer& operator=(const outBlockBuffer&) = delete; // disable assignment |
| 59 | |
| 60 | ~outBlockBuffer() |
| 61 | { |
| 62 | free(compressed); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | struct compressedDataSlice |
| 67 | { |
| 68 | std::vector<outBlockBuffer*> m_buffers; |
| 69 | unsigned char m_predictor_code; |
| 70 | |
| 71 | compressedDataSlice() |
| 72 | { |
| 73 | m_predictor_code = PREDICTOR_UNKNOWN; |
| 74 | } |
| 75 | |
| 76 | compressedDataSlice(const compressedDataSlice&) = delete; // disable copy constructor |
| 77 | compressedDataSlice& operator=(const compressedDataSlice&) = delete; // disable assignment |
| 78 | |
| 79 | ~compressedDataSlice() |
| 80 | { |
| 81 | for (auto b : m_buffers) |
| 82 | { |
| 83 | delete b; |
| 84 | } |
| 85 | |
| 86 | m_buffers.clear(); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | compressedDataSlice * m_data_slice; |
| 91 | |
| 92 | void selectInitialLinearOrCrossDelta(const UnitType type, void* pData, const int iWidth, const int iHeight, int& initial_delta, bool& use_cross, bool test_first_byte_delta, size_t* stats = NULL); |
| 93 | |
| 94 | bool ComputeHuffmanCodesFltSlice (const void* pInput, bool bIsDouble, int iCols, int iRows); |
| 95 | |
| 96 | static bool DecodeHuffmanFltSlice (const unsigned char** ppByte, size_t& nBytesRemainingInOut, void* pData, |
| 97 | bool bIsDouble, int iCols, int iRows); |
| 98 | |
| 99 | |
| 100 | public: |
| 101 | LosslessFPCompression() : m_data_slice (nullptr) { } |
| 102 | ~LosslessFPCompression(); |
| 103 | |
| 104 | bool ComputeHuffmanCodesFlt(const void * pInput, bool nIsDouble, int iCols, int iRows, int iDepth); |
| 105 | int compressedLength() const; |
| 106 | bool EncodeHuffmanFlt(unsigned char ** ppByte) ; |
| 107 | |
| 108 | static bool DecodeHuffmanFlt(const unsigned char** ppByte, size_t& nBytesRemainingInOut, void * pData, |
| 109 | bool bIsDouble, int iCols, int iRows, int iDepth); |
| 110 | }; |
| 111 | |
| 112 | NAMESPACE_LERC_END |
| 113 | #endif |